Merge branch 'refs/heads/t/mp'
[paraslash.git] / opus_afh.c
1 /*
2 * Copyright (C) 2012 Andre Noll <maan@tuebingen.mpg.de>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file opus_afh.c Audio format handler for ogg/opus files. */
8
9 #include <ogg/ogg.h>
10 #include <regex.h>
11
12 #include "para.h"
13 #include "afh.h"
14 #include "error.h"
15 #include "portable_io.h"
16 #include "string.h"
17 #include "opus_common.h"
18 #include "ogg_afh_common.h"
19
20 static const char * const opus_suffixes[] = {"opus", NULL};
21
22 #define OPUS_COMMENT_HEADER "OpusTags"
23
24 static bool copy_if_tag_type(const char *tag, int taglen, const char *type,
25 char **p)
26 {
27 char *q = key_value_copy(tag, taglen, type);
28 if (!q)
29 return false;
30 free(*p);
31 *p = q;
32 return true;
33 }
34
35 static int opus_get_comments(char *comments, int length,
36 struct taginfo *tags)
37 {
38 char *p = comments, *end = comments + length;
39 int i;
40 uint32_t val, ntags;
41
42 /* min size of a opus header is 16 bytes */
43 if (length < 16)
44 return -E_OPUS_COMMENT;
45 if (memcmp(p, OPUS_COMMENT_HEADER, strlen(OPUS_COMMENT_HEADER)) != 0)
46 return -E_OPUS_COMMENT;
47 p += 8;
48 val = read_u32(p);
49 p += 4;
50 if (p + val > end)
51 return -E_OPUS_COMMENT;
52 tags->comment = safe_strdup(p, val);
53 p += val;
54 ntags = read_u32(p);
55 p += 4;
56 if (p + ntags * 4 > end)
57 return -E_OPUS_COMMENT;
58 PARA_INFO_LOG("found %u tag(s)\n", ntags);
59 for (i = 0; i < ntags; i++, p += val) {
60 char *tag;
61
62 if (p + 4 > end)
63 return -E_OPUS_COMMENT;
64 val = read_u32(p);
65 p += 4;
66 if (p + val > end)
67 return -E_OPUS_COMMENT;
68 if (copy_if_tag_type(p, val, "author", &tags->artist))
69 continue;
70 if (copy_if_tag_type(p, val, "artist", &tags->artist))
71 continue;
72 if (copy_if_tag_type(p, val, "title", &tags->title))
73 continue;
74 if (copy_if_tag_type(p, val, "album", &tags->album))
75 continue;
76 if (copy_if_tag_type(p, val, "year", &tags->year))
77 continue;
78 if (copy_if_tag_type(p, val, "comment", &tags->comment))
79 continue;
80 tag = safe_strdup(p, val);
81 PARA_NOTICE_LOG("unrecognized tag: %s\n", tag);
82 free(tag);
83 }
84 return 1;
85 }
86
87 /*
88 * Ogg/Opus has two mandatory header packets:
89 *
90 * 1. ID header (identifies the stream as Opus). Dedicated "BOS" ogg page.
91 * 2. Comment header (metadata). May span multiple pages.
92 *
93 * See doc/draft-ietf-codec-oggopus.xml in the opus source tree for details.
94 */
95 static int opus_packet_callback(ogg_packet *packet, int packet_num,
96 __a_unused int serial, struct afh_info *afhi,
97 void *private_data)
98 {
99 int ret;
100 struct opus_header *oh = private_data;
101
102 if (packet_num == 0) {
103 ret = opus_parse_header((char *)packet->packet, packet->bytes, oh);
104 if (ret < 0)
105 return ret;
106 afhi->channels = oh->channels;
107 afhi->techinfo = make_message(
108 "header version %d, input sample rate: %" PRIu32 "Hz",
109 oh->version, oh->input_sample_rate);
110 /*
111 * The input sample rate is irrelevant for afhi->frequency as
112 * we always decode to 48kHz.
113 */
114 afhi->frequency = 48000;
115 return 1;
116 }
117 if (packet_num == 1) {
118 ret = opus_get_comments((char *)packet->packet, packet->bytes,
119 &afhi->tags);
120 if (ret < 0)
121 return ret;
122 return 0; /* header complete */
123 }
124 /* never reached */
125 assert(0);
126 }
127
128 static int opus_get_file_info(char *map, size_t numbytes, __a_unused int fd,
129 struct afh_info *afhi)
130 {
131 int ret, ms;
132 struct opus_header oh = {.version = 0};
133
134 struct oac_callback_info opus_callback_info = {
135 .packet_callback = opus_packet_callback,
136 .private_data = &oh,
137 };
138 ret = oac_get_file_info(map, numbytes, afhi, &opus_callback_info);
139 if (ret < 0)
140 return ret;
141 ret = (afhi->chunk_table[afhi->chunks_total] - afhi->chunk_table[0]) * 8; /* bits */
142 ms = tv2ms(&afhi->chunk_tv) * afhi->chunks_total;
143 afhi->bitrate = ret / ms;
144 return 1;
145 }
146
147 static size_t opus_make_meta_packet(struct taginfo *tags, char **result)
148 {
149 size_t sz;
150 char *buf, *p;
151 size_t comment_len = strlen(tags->comment),
152 artist_len = strlen(tags->artist),
153 title_len = strlen(tags->title),
154 album_len = strlen(tags->album),
155 year_len = strlen(tags->year);
156 uint32_t comment_sz = comment_len,
157 artist_sz = artist_len + strlen("artist="),
158 title_sz = title_len + strlen("title="),
159 album_sz = album_len + strlen("album="),
160 year_sz = year_len + strlen("year=");
161 uint32_t num_tags;
162
163 sz = strlen(OPUS_COMMENT_HEADER)
164 + 4 /* comment length (always present) */
165 + comment_sz
166 + 4; /* number of tags */
167 num_tags = 0;
168 if (artist_len) {
169 num_tags++;
170 sz += 4 + artist_sz;
171 }
172 if (title_len) {
173 num_tags++;
174 sz += 4 + title_sz;
175 }
176 if (album_len) {
177 num_tags++;
178 sz += 4 + album_sz;
179 }
180 if (year_len) {
181 num_tags++;
182 sz += 4 + year_sz;
183 }
184 PARA_DEBUG_LOG("meta packet size: %zu bytes\n", sz);
185 /* terminating zero byte for the last sprintf() */
186 buf = p = para_malloc(sz + 1);
187 memcpy(p, OPUS_COMMENT_HEADER, strlen(OPUS_COMMENT_HEADER));
188 p += strlen(OPUS_COMMENT_HEADER);
189 write_u32(p, comment_sz);
190 p += 4;
191 strcpy(p, tags->comment);
192 p += comment_sz;
193 write_u32(p, num_tags);
194 p += 4;
195 if (artist_len) {
196 write_u32(p, artist_sz);
197 p += 4;
198 sprintf(p, "artist=%s", tags->artist);
199 p += artist_sz;
200 }
201 if (title_len) {
202 write_u32(p, title_sz);
203 p += 4;
204 sprintf(p, "title=%s", tags->title);
205 p += title_sz;
206 }
207 if (album_len) {
208 write_u32(p, album_sz);
209 p += 4;
210 sprintf(p, "album=%s", tags->album);
211 p += album_sz;
212 }
213 if (year_len) {
214 write_u32(p, year_sz);
215 p += 4;
216 sprintf(p, "year=%s", tags->year);
217 p += year_sz;
218 }
219 assert(p == buf + sz);
220 *result = buf;
221 return sz;
222 }
223
224 static int opus_rewrite_tags(const char *map, size_t mapsize,
225 struct taginfo *tags, int output_fd,
226 __a_unused const char *filename)
227 {
228 char *meta_packet;
229 size_t meta_sz;
230 int ret;
231
232 meta_sz = opus_make_meta_packet(tags, &meta_packet);
233 ret = oac_rewrite_tags(map, mapsize, output_fd, meta_packet, meta_sz);
234 free(meta_packet);
235 return ret;
236 }
237
238 /*
239 * See doc/draft-ietf-codec-oggopus.xml in the opus source tree for details
240 * about the format of the comment header.
241 */
242 static int opus_get_header_callback(ogg_packet *packet, int packet_num,
243 int serial, __a_unused struct afh_info *afhi, void *private_data)
244 {
245 struct oac_custom_header *h = private_data;
246 int ret;
247 static unsigned char dummy_tags[] = { /* a minimal comment header */
248 'O', 'p', 'u', 's', 'T', 'a', 'g', 's',
249 0x06, 0x00, 0x00, 0x00, /* vendor string length */
250 'd', 'u', 'm', 'm', 'y', '\0', /* vendor string */
251 0x00, 0x00, 0x00, 0x00, /* user comment list length */
252 };
253 ogg_packet replacement;
254
255 if (packet_num == 0) {
256 oac_custom_header_init(serial, h);
257 ret = oac_custom_header_append(packet, h);
258 if (ret < 0)
259 return ret;
260 oac_custom_header_flush(h);
261 return 1;
262 }
263 assert(packet_num == 1);
264 PARA_INFO_LOG("replacing metadata packet\n");
265 replacement = *packet;
266 replacement.packet = dummy_tags;
267 replacement.bytes = sizeof(dummy_tags);
268 ret = oac_custom_header_append(&replacement, h);
269 if (ret < 0)
270 return ret;
271 oac_custom_header_flush(h);
272 return 0;
273 }
274
275 static void opus_get_header(void *map, size_t mapsize, char **buf,
276 size_t *len)
277 {
278 int ret;
279 struct oac_custom_header *h = oac_custom_header_new();
280 struct oac_callback_info cb = {
281 .packet_callback = opus_get_header_callback,
282 .private_data = h,
283 };
284
285 ret = oac_get_file_info(map, mapsize, NULL, &cb);
286 *len = oac_custom_header_get(buf, h);
287 if (ret < 0) {
288 PARA_ERROR_LOG("could not create custom header: %s\n",
289 para_strerror(-ret));
290 free(*buf);
291 *buf = NULL;
292 *len = 0;
293 } else
294 PARA_INFO_LOG("created %zu byte ogg/opus header\n", *len);
295 }
296
297 /**
298 * The init function of the ogg/opus audio format handler.
299 *
300 * \param afh Pointer to the struct to initialize.
301 */
302 void opus_afh_init(struct audio_format_handler *afh)
303 {
304 afh->get_file_info = opus_get_file_info,
305 afh->get_header = opus_get_header;
306 afh->suffixes = opus_suffixes;
307 afh->rewrite_tags = opus_rewrite_tags;
308 }