Remove explicit uses of _GNU_SOURCE.
[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 ogg_afh_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 = ogg_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 struct vorbis_get_header_data {
83         ogg_stream_state os;
84         char *buf;
85         size_t len;
86 };
87
88 static void add_ogg_page(ogg_page *og, struct vorbis_get_header_data *vghd)
89 {
90         size_t old_len = vghd->len;
91         size_t new_len = vghd->len + og->header_len + og->body_len;
92         char *buf = para_realloc(vghd->buf, new_len), *p = buf + old_len;
93
94         memcpy(p, og->header, og->header_len);
95         memcpy(p + og->header_len, og->body, og->body_len);
96         vghd->buf = buf;
97         vghd->len = new_len;
98         PARA_DEBUG_LOG("header/body/old/new: %lu/%lu/%zu/%zu\n",
99                 og->header_len, og->body_len, old_len, new_len);
100 }
101
102 static int vorbis_get_header_callback(ogg_packet *packet, int packet_num,
103                 int serial, __a_unused struct afh_info *afhi, void *private_data)
104 {
105         int ret;
106         struct vorbis_get_header_data *vghd = private_data;
107         ogg_page og;
108         static unsigned char dummy_packet[] = {
109                 0x03,
110                 'v', 'o', 'r', 'b', 'i', 's',
111                 0x06, 0x00, 0x00, 0x00,
112                 'd', 'u', 'm', 'm', 'y', '\0',
113                 0x00, 0x00, 0x00, 0x00, /* no comment :) */
114                 0xff /* framing bit */
115         };
116
117         PARA_DEBUG_LOG("processing ogg packet #%d\n", packet_num);
118         if (packet_num > 2)
119                 return 0;
120         if (packet_num == 0) {
121                 ogg_stream_init(&vghd->os, serial);
122                 ret = ogg_stream_packetin(&vghd->os, packet);
123                 if (ret < 0)
124                         goto out;
125                 ret = -E_OGG_STREAM_FLUSH;
126                 if (ogg_stream_flush(&vghd->os, &og) == 0)
127                         goto out;
128                 add_ogg_page(&og, vghd);
129                 return 1;
130         }
131         if (packet_num == 1) {
132                 ogg_packet replacement = *packet;
133                 PARA_INFO_LOG("replacing metadata packet\n");
134                 replacement.packet = dummy_packet;
135                 replacement.bytes = sizeof(dummy_packet);
136                 ret = ogg_stream_packetin(&vghd->os, &replacement);
137                 if (ret >= 0)
138                         return 1;
139                 ret = -E_OGG_PACKET_IN;
140                 goto out;
141         }
142         ret = -E_OGG_PACKET_IN;
143         if (ogg_stream_packetin(&vghd->os, packet) < 0)
144                 goto out;
145         while (ogg_stream_flush(&vghd->os, &og))
146                 add_ogg_page(&og, vghd);
147         ret = 0;
148 out:
149         ogg_stream_clear(&vghd->os);
150         return ret;
151 }
152
153 static void vorbis_get_header(void *map, size_t mapsize, char **buf,
154                 size_t *len)
155 {
156         int ret;
157         struct vorbis_get_header_data vghd = {.len = 0};
158         struct ogg_afh_callback_info cb = {
159                 .packet_callback = vorbis_get_header_callback,
160                 .private_data = &vghd,
161         };
162
163         ret = ogg_get_file_info(map, mapsize, NULL, &cb);
164         if (ret < 0)
165                 goto fail;
166         *buf = vghd.buf;
167         *len = vghd.len;
168         PARA_INFO_LOG("created %zu byte ogg vorbis header\n", *len);
169         return;
170 fail:
171         PARA_ERROR_LOG("%s\n", para_strerror(-ret));
172 }
173
174 static int vorbis_make_meta_packet(struct taginfo *tags, ogg_packet *result)
175 {
176         vorbis_comment vc;
177         int ret;
178
179         vorbis_comment_init(&vc);
180         vorbis_comment_add_tag(&vc, "artist", tags->artist);
181         vorbis_comment_add_tag(&vc, "title", tags->title);
182         vorbis_comment_add_tag(&vc, "album", tags->album);
183         vorbis_comment_add_tag(&vc, "year", tags->year);
184         vorbis_comment_add_tag(&vc, "comment", tags->comment);
185         ret = vorbis_commentheader_out(&vc, result);
186         vorbis_comment_clear(&vc);
187         if (ret != 0)
188                 return -E_VORBIS_COMMENTHEADER;
189         return 1;
190 }
191
192 static int vorbis_rewrite_tags(const char *map, size_t mapsize,
193                 struct taginfo *tags, int output_fd,
194                 __a_unused const char *filename)
195 {
196         int ret;
197         ogg_packet packet;
198
199         ret = vorbis_make_meta_packet(tags, &packet);
200         if (ret < 0)
201                 return ret;
202         ret = ogg_rewrite_tags(map, mapsize, output_fd, (char *)packet.packet,
203                 packet.bytes);
204         free(packet.packet);
205         return ret;
206 }
207
208 static const char * const ogg_suffixes[] = {"ogg", NULL};
209
210 /**
211  * The init function of the ogg vorbis audio format handler.
212  *
213  * \param afh Pointer to the struct to initialize.
214  */
215 void ogg_init(struct audio_format_handler *afh)
216 {
217         afh->get_file_info = ogg_vorbis_get_file_info;
218         afh->get_header = vorbis_get_header;
219         afh->suffixes = ogg_suffixes;
220         afh->rewrite_tags = vorbis_rewrite_tags;
221 }