2 * Copyright (C) 2012 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file opus_afh.c Audio format handler for ogg/opus files. */
15 #include "portable_io.h"
17 #include "opus_common.h"
18 #include "ogg_afh_common.h"
20 static const char * const opus_suffixes[] = {"opus", NULL};
22 #define OPUS_COMMENT_HEADER "OpusTags"
24 static bool copy_if_tag_type(const char *tag, int taglen, const char *type,
27 char *q = key_value_copy(tag, taglen, type);
35 static int opus_get_comments(char *comments, int length,
38 char *p = comments, *end = comments + length;
42 /* min size of a opus header is 16 bytes */
44 return -E_OPUS_COMMENT;
45 if (memcmp(p, OPUS_COMMENT_HEADER, strlen(OPUS_COMMENT_HEADER)) != 0)
46 return -E_OPUS_COMMENT;
51 return -E_OPUS_COMMENT;
52 tags->comment = safe_strdup(p, val);
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) {
63 return -E_OPUS_COMMENT;
67 return -E_OPUS_COMMENT;
68 if (copy_if_tag_type(p, val, "author", &tags->artist))
70 if (copy_if_tag_type(p, val, "artist", &tags->artist))
72 if (copy_if_tag_type(p, val, "title", &tags->title))
74 if (copy_if_tag_type(p, val, "album", &tags->album))
76 if (copy_if_tag_type(p, val, "year", &tags->year))
78 if (copy_if_tag_type(p, val, "comment", &tags->comment))
80 tag = safe_strdup(p, val);
81 PARA_NOTICE_LOG("unrecognized tag: %s\n", tag);
88 * Ogg/Opus has two mandatory header packets:
90 * 1. ID header (identifies the stream as Opus). Dedicated "BOS" ogg page.
91 * 2. Comment header (metadata). May span multiple pages.
93 * See doc/draft-ietf-codec-oggopus.xml in the opus source tree for details.
95 static int opus_packet_callback(ogg_packet *packet, int packet_num,
96 __a_unused int serial, struct afh_info *afhi,
100 struct opus_header *oh = private_data;
102 if (packet_num == 0) {
103 ret = opus_parse_header((char *)packet->packet, packet->bytes, oh);
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);
111 * The input sample rate is irrelevant for afhi->frequency as
112 * we always decode to 48kHz.
114 afhi->frequency = 48000;
117 if (packet_num == 1) {
118 ret = opus_get_comments((char *)packet->packet, packet->bytes,
122 return 0; /* header complete */
128 static int opus_get_file_info(char *map, size_t numbytes, __a_unused int fd,
129 struct afh_info *afhi)
132 struct opus_header oh = {.version = 0};
134 struct oac_callback_info opus_callback_info = {
135 .packet_callback = opus_packet_callback,
138 ret = oac_get_file_info(map, numbytes, afhi, &opus_callback_info);
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;
147 static size_t opus_make_meta_packet(struct taginfo *tags, char **result)
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=");
163 sz = strlen(OPUS_COMMENT_HEADER)
164 + 4 /* comment length (always present) */
166 + 4; /* number of tags */
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);
191 strcpy(p, tags->comment);
193 write_u32(p, num_tags);
196 write_u32(p, artist_sz);
198 sprintf(p, "artist=%s", tags->artist);
202 write_u32(p, title_sz);
204 sprintf(p, "title=%s", tags->title);
208 write_u32(p, album_sz);
210 sprintf(p, "album=%s", tags->album);
214 write_u32(p, year_sz);
216 sprintf(p, "year=%s", tags->year);
219 assert(p == buf + sz);
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)
232 meta_sz = opus_make_meta_packet(tags, &meta_packet);
233 ret = oac_rewrite_tags(map, mapsize, output_fd, meta_packet, meta_sz);
239 * See doc/draft-ietf-codec-oggopus.xml in the opus source tree for details
240 * about the format of the comment header.
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)
245 struct oac_custom_header *h = private_data;
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 */
253 ogg_packet replacement;
255 if (packet_num == 0) {
256 oac_custom_header_init(serial, h);
257 ret = oac_custom_header_append(packet, h);
260 oac_custom_header_flush(h);
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);
271 oac_custom_header_flush(h);
275 static void opus_get_header(void *map, size_t mapsize, char **buf,
279 struct oac_custom_header *h = oac_custom_header_new();
280 struct oac_callback_info cb = {
281 .packet_callback = opus_get_header_callback,
285 ret = oac_get_file_info(map, mapsize, NULL, &cb);
286 *len = oac_custom_header_get(buf, h);
288 PARA_ERROR_LOG("could not create custom header: %s\n",
289 para_strerror(-ret));
294 PARA_INFO_LOG("created %zu byte ogg/opus header\n", *len);
298 * The init function of the ogg/opus audio format handler.
300 * \param afh Pointer to the struct to initialize.
302 void opus_afh_init(struct audio_format_handler *afh)
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;