daemon: Improve color error message.
[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 %d 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 static int opus_packet_callback(ogg_packet *packet, int packet_num,
88 __a_unused int serial, struct afh_info *afhi,
89 void *private_data)
90 {
91 int ret;
92 struct opus_header *oh = private_data;
93
94 if (packet_num == 0) {
95 ret = opus_parse_header((char *)packet->packet, packet->bytes, oh);
96 if (ret < 0)
97 return ret;
98 afhi->channels = oh->channels;
99 afhi->techinfo = make_message(
100 "header version %d, input sample rate: %" PRIu32 "Hz",
101 oh->version, oh->input_sample_rate);
102 /*
103 * The input sample rate is irrelevant for afhi->frequency as
104 * we always decode to 48kHz.
105 */
106 afhi->frequency = 48000;
107 return 1;
108 }
109 if (packet_num == 1) {
110 ret = opus_get_comments((char *)packet->packet, packet->bytes,
111 &afhi->tags);
112 if (ret < 0)
113 return ret;
114 return 0; /* header complete */
115 }
116 /* never reached */
117 assert(0);
118 }
119
120 static int opus_get_file_info(char *map, size_t numbytes, __a_unused int fd,
121 struct afh_info *afhi)
122 {
123 int ret, ms;
124 struct opus_header oh = {.version = 0};
125
126 struct ogg_afh_callback_info opus_callback_info = {
127 .packet_callback = opus_packet_callback,
128 .private_data = &oh,
129 };
130 ret = ogg_get_file_info(map, numbytes, afhi, &opus_callback_info);
131 if (ret < 0)
132 return ret;
133 ret = (afhi->chunk_table[afhi->chunks_total] - afhi->chunk_table[0]) * 8; /* bits */
134 ms = tv2ms(&afhi->chunk_tv) * afhi->chunks_total;
135 afhi->bitrate = ret / ms;
136 return 1;
137 }
138
139 static size_t opus_make_meta_packet(struct taginfo *tags, char **result)
140 {
141 size_t sz;
142 char *buf, *p;
143 size_t comment_len = strlen(tags->comment),
144 artist_len = strlen(tags->artist),
145 title_len = strlen(tags->title),
146 album_len = strlen(tags->album),
147 year_len = strlen(tags->year);
148 uint32_t comment_sz = comment_len,
149 artist_sz = artist_len + strlen("artist="),
150 title_sz = title_len + strlen("title="),
151 album_sz = album_len + strlen("album="),
152 year_sz = year_len + strlen("year=");
153 uint32_t num_tags;
154
155 sz = strlen(OPUS_COMMENT_HEADER)
156 + 4 /* comment length (always present) */
157 + comment_sz
158 + 4; /* number of tags */
159 num_tags = 0;
160 if (artist_len) {
161 num_tags++;
162 sz += 4 + artist_sz;
163 }
164 if (title_len) {
165 num_tags++;
166 sz += 4 + title_sz;
167 }
168 if (album_len) {
169 num_tags++;
170 sz += 4 + album_sz;
171 }
172 if (year_len) {
173 num_tags++;
174 sz += 4 + year_sz;
175 }
176 PARA_DEBUG_LOG("meta packet size: %zu bytes\n", sz);
177 /* terminating zero byte for the last sprintf() */
178 buf = p = para_malloc(sz + 1);
179 memcpy(p, OPUS_COMMENT_HEADER, strlen(OPUS_COMMENT_HEADER));
180 p += strlen(OPUS_COMMENT_HEADER);
181 write_u32(p, comment_sz);
182 p += 4;
183 strcpy(p, tags->comment);
184 p += comment_sz;
185 write_u32(p, num_tags);
186 p += 4;
187 if (artist_len) {
188 write_u32(p, artist_sz);
189 p += 4;
190 sprintf(p, "artist=%s", tags->artist);
191 p += artist_sz;
192 }
193 if (title_len) {
194 write_u32(p, title_sz);
195 p += 4;
196 sprintf(p, "title=%s", tags->title);
197 p += title_sz;
198 }
199 if (album_len) {
200 write_u32(p, album_sz);
201 p += 4;
202 sprintf(p, "album=%s", tags->album);
203 p += album_sz;
204 }
205 if (year_len) {
206 write_u32(p, year_sz);
207 p += 4;
208 sprintf(p, "year=%s", tags->year);
209 p += year_sz;
210 }
211 assert(p == buf + sz);
212 *result = buf;
213 return sz;
214 }
215
216 static int opus_rewrite_tags(const char *map, size_t mapsize,
217 struct taginfo *tags, int output_fd,
218 __a_unused const char *filename)
219 {
220 char *meta_packet;
221 size_t meta_sz;
222 int ret;
223
224 meta_sz = opus_make_meta_packet(tags, &meta_packet);
225 ret = ogg_rewrite_tags(map, mapsize, output_fd, meta_packet, meta_sz);
226 free(meta_packet);
227 return ret;
228 }
229
230 /**
231 * The init function of the ogg/opus audio format handler.
232 *
233 * \param afh Pointer to the struct to initialize.
234 */
235 void opus_afh_init(struct audio_format_handler *afh)
236 {
237 afh->get_file_info = opus_get_file_info,
238 afh->suffixes = opus_suffixes;
239 afh->rewrite_tags = opus_rewrite_tags;
240 }