Merge branch 'refs/heads/t/sqrt'
[paraslash.git] / ogg_afh.c
1 /*
2  * Copyright (C) 2004 Andre Noll <maan@tuebingen.mpg.de>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file ogg_afh.c Audio format handler for ogg/vorbis files. */
8
9 #include <vorbis/codec.h>
10 #include <regex.h>
11
12 #include "para.h"
13 #include "afh.h"
14 #include "error.h"
15 #include "string.h"
16 #include "ogg_afh_common.h"
17
18 struct private_vorbis_data {
19         vorbis_info vi;
20         vorbis_comment vc;
21 };
22
23 /*
24  * Vorbis uses three header packets, all of which are required: the
25  * identification header, the comments header, and the setup header.
26  *
27  * The identification header identifies the bitstream as Vorbis. It contains
28  * the Vorbis version and simple audio characteristics of the stream such as
29  * sample rate and number of channels.
30  *
31  * The comment header includes user text comments (tags) and a vendor string
32  * for the application/library that produced the bitstream.
33  *
34  * The setup header includes extensive CODEC setup information as well as the
35  * complete VQ and Huffman codebooks needed for decoding.
36  */
37 static int vorbis_packet_callback(ogg_packet *packet, int packet_num,
38                 __a_unused int serial, struct afh_info *afhi, void *private_data)
39 {
40         struct private_vorbis_data *pvd = private_data;
41
42         if (vorbis_synthesis_headerin(&pvd->vi, &pvd->vc, packet) < 0)
43                 return -E_VORBIS;
44         if (packet_num == 0) {
45                 if (pvd->vi.rate == 0)
46                         return -E_VORBIS;
47                 afhi->channels = pvd->vi.channels;
48                 afhi->frequency = pvd->vi.rate;
49                 afhi->bitrate = pvd->vi.bitrate_nominal / 1000;
50                 PARA_DEBUG_LOG("channels: %i, sampling rate: %i, bitrate: %i\n",
51                         afhi->channels, afhi->frequency, afhi->bitrate);
52                 return 1;
53         }
54         if (packet_num == 1)
55                 return 1; /* we also want to have packet #2 */
56         afhi->tags.artist = para_strdup(vorbis_comment_query(&pvd->vc, "artist", 0));
57         afhi->tags.title = para_strdup(vorbis_comment_query(&pvd->vc, "title", 0));
58         afhi->tags.album = para_strdup(vorbis_comment_query(&pvd->vc, "album", 0));
59         afhi->tags.year = para_strdup(vorbis_comment_query(&pvd->vc, "year", 0));
60         afhi->tags.comment = para_strdup(vorbis_comment_query(&pvd->vc, "comment", 0));
61         return 0;
62 }
63
64 static int ogg_vorbis_get_file_info(char *map, size_t numbytes, __a_unused int fd,
65                 struct afh_info *afhi)
66 {
67         int ret;
68         struct private_vorbis_data pvd;
69         struct oac_callback_info vorbis_callback_info = {
70                 .packet_callback = vorbis_packet_callback,
71                 .private_data = &pvd,
72         };
73
74         vorbis_info_init(&pvd.vi);
75         vorbis_comment_init(&pvd.vc);
76         ret = oac_get_file_info(map, numbytes, afhi, &vorbis_callback_info);
77         vorbis_info_clear(&pvd.vi);
78         vorbis_comment_clear(&pvd.vc);
79         return ret;
80 }
81
82 static int vorbis_get_header_callback(ogg_packet *packet, int packet_num,
83                 int serial, __a_unused struct afh_info *afhi, void *private_data)
84 {
85         int ret;
86         struct oac_custom_header *h = private_data;
87         static unsigned char dummy_packet[] = {
88                 0x03,
89                 'v', 'o', 'r', 'b', 'i', 's',
90                 0x06, 0x00, 0x00, 0x00,
91                 'd', 'u', 'm', 'm', 'y', '\0',
92                 0x00, 0x00, 0x00, 0x00, /* no comment :) */
93                 0xff /* framing bit */
94         };
95
96         PARA_DEBUG_LOG("processing ogg packet #%d\n", packet_num);
97         if (packet_num == 0) {
98                 oac_custom_header_init(serial, h);
99                 ret = oac_custom_header_append(packet, h);
100                 if (ret < 0)
101                         return ret;
102                 oac_custom_header_flush(h);
103                 return 1;
104         }
105         if (packet_num == 1) {
106                 ogg_packet replacement = *packet;
107                 PARA_INFO_LOG("replacing metadata packet\n");
108                 replacement.packet = dummy_packet;
109                 replacement.bytes = sizeof(dummy_packet);
110                 ret = oac_custom_header_append(&replacement, h);
111                 return ret < 0? ret : 1;
112         }
113         assert(packet_num == 2);
114         ret = oac_custom_header_append(packet, h);
115         if (ret < 0)
116                 return ret;
117         oac_custom_header_flush(h);
118         return 0;
119 }
120
121 static void vorbis_get_header(void *map, size_t mapsize, char **buf,
122                 size_t *len)
123 {
124         int ret;
125         struct oac_custom_header *h = oac_custom_header_new();
126         struct oac_callback_info cb = {
127                 .packet_callback = vorbis_get_header_callback,
128                 .private_data = h,
129         };
130
131         ret = oac_get_file_info(map, mapsize, NULL, &cb);
132         *len = oac_custom_header_get(buf, h);
133         if (ret < 0) {
134                 PARA_ERROR_LOG("could not create ogg/vorbis header: %s\n",
135                         para_strerror(-ret));
136                 free(*buf);
137                 *buf = NULL;
138                 *len = 0;
139         } else
140                 PARA_INFO_LOG("created %zu byte ogg vorbis header\n", *len);
141 }
142
143 static int vorbis_make_meta_packet(struct taginfo *tags, ogg_packet *result)
144 {
145         vorbis_comment vc;
146         int ret;
147
148         vorbis_comment_init(&vc);
149         vorbis_comment_add_tag(&vc, "artist", tags->artist);
150         vorbis_comment_add_tag(&vc, "title", tags->title);
151         vorbis_comment_add_tag(&vc, "album", tags->album);
152         vorbis_comment_add_tag(&vc, "year", tags->year);
153         vorbis_comment_add_tag(&vc, "comment", tags->comment);
154         ret = vorbis_commentheader_out(&vc, result);
155         vorbis_comment_clear(&vc);
156         if (ret != 0)
157                 return -E_VORBIS_COMMENTHEADER;
158         return 1;
159 }
160
161 static int vorbis_rewrite_tags(const char *map, size_t mapsize,
162                 struct taginfo *tags, int output_fd,
163                 __a_unused const char *filename)
164 {
165         int ret;
166         ogg_packet packet;
167
168         ret = vorbis_make_meta_packet(tags, &packet);
169         if (ret < 0)
170                 return ret;
171         ret = oac_rewrite_tags(map, mapsize, output_fd, (char *)packet.packet,
172                 packet.bytes);
173         free(packet.packet);
174         return ret;
175 }
176
177 static const char * const ogg_suffixes[] = {"ogg", NULL};
178
179 /**
180  * The init function of the ogg vorbis audio format handler.
181  *
182  * \param afh Pointer to the struct to initialize.
183  */
184 void ogg_afh_init(struct audio_format_handler *afh)
185 {
186         afh->get_file_info = ogg_vorbis_get_file_info;
187         afh->get_header = vorbis_get_header;
188         afh->suffixes = ogg_suffixes;
189         afh->rewrite_tags = vorbis_rewrite_tags;
190 }