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