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* 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 %d 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);
87 static int opus_packet_callback(ogg_packet *packet, int packet_num,
88 __a_unused int serial, struct afh_info *afhi,
92 struct opus_header *oh = private_data;
94 if (packet_num == 0) {
95 ret = opus_parse_header((char *)packet->packet, packet->bytes, oh);
98 afhi->channels = oh->channels;
99 afhi->techinfo = make_message("header version %d, input sample rate: %dHz",
100 oh->version, oh->input_sample_rate);
102 * The input sample rate is irrelevant for afhi->frequency as
103 * we always decode to 48kHz.
105 afhi->frequency = 48000;
108 if (packet_num == 1) {
109 ret = opus_get_comments((char *)packet->packet, packet->bytes,
113 return 0; /* header complete */
119 static int opus_get_file_info(char *map, size_t numbytes, __a_unused int fd,
120 struct afh_info *afhi)
123 struct opus_header oh = {.version = 0};
125 struct ogg_afh_callback_info opus_callback_info = {
126 .packet_callback = opus_packet_callback,
129 ret = ogg_get_file_info(map, numbytes, afhi, &opus_callback_info);
132 ret = (afhi->chunk_table[afhi->chunks_total] - afhi->chunk_table[0]) * 8; /* bits */
133 ms = tv2ms(&afhi->chunk_tv) * afhi->chunks_total;
134 afhi->bitrate = ret / ms;
138 static size_t opus_make_meta_packet(struct taginfo *tags, char **result)
142 size_t comment_len = strlen(tags->comment),
143 artist_len = strlen(tags->artist),
144 title_len = strlen(tags->title),
145 album_len = strlen(tags->album),
146 year_len = strlen(tags->year);
147 uint32_t comment_sz = comment_len,
148 artist_sz = artist_len + strlen("artist="),
149 title_sz = title_len + strlen("title="),
150 album_sz = album_len + strlen("album="),
151 year_sz = year_len + strlen("year=");
154 sz = strlen(OPUS_COMMENT_HEADER)
155 + 4 /* comment length (always present) */
157 + 4; /* number of tags */
175 PARA_DEBUG_LOG("meta packet size: %zu bytes\n", sz);
176 /* terminating zero byte for the last sprintf() */
177 buf = p = para_malloc(sz + 1);
178 memcpy(p, OPUS_COMMENT_HEADER, strlen(OPUS_COMMENT_HEADER));
179 p += strlen(OPUS_COMMENT_HEADER);
180 write_u32(p, comment_sz);
182 strcpy(p, tags->comment);
184 write_u32(p, num_tags);
187 write_u32(p, artist_sz);
189 sprintf(p, "artist=%s", tags->artist);
193 write_u32(p, title_sz);
195 sprintf(p, "title=%s", tags->title);
199 write_u32(p, album_sz);
201 sprintf(p, "album=%s", tags->album);
205 write_u32(p, year_sz);
207 sprintf(p, "year=%s", tags->year);
210 assert(p == buf + sz);
215 static int opus_rewrite_tags(const char *map, size_t mapsize,
216 struct taginfo *tags, int output_fd,
217 __a_unused const char *filename)
223 meta_sz = opus_make_meta_packet(tags, &meta_packet);
224 ret = ogg_rewrite_tags(map, mapsize, output_fd, meta_packet, meta_sz);
230 * The init function of the ogg/opus audio format handler.
232 * \param afh Pointer to the struct to initialize.
234 void opus_afh_init(struct audio_format_handler *afh)
236 afh->get_file_info = opus_get_file_info,
237 afh->suffixes = opus_suffixes;
238 afh->rewrite_tags = opus_rewrite_tags;