gui: Output "xxx tag not set" for unset tags rather than empty strings.
[paraslash.git] / mp3_afh.c
1 /*
2 * Copyright (C) 2003-2009 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file mp3_afh.c para_server's mp3 audio format handler */
8
9 /*
10 * This file is based in part on mp3tech.c and mp3tech.h, Copyright (C)
11 * 2000-2001 Cedric Tefft <cedric@earthling.net>, which in turn is based
12 * in part on
13 *
14 * * MP3Info 0.5 by Ricardo Cerqueira <rmc@rccn.net>
15 * * MP3Stat 0.9 by Ed Sweetman <safemode@voicenet.com> and
16 * Johannes Overmann <overmann@iname.com>
17 */
18
19 #include <regex.h>
20 #include <osl.h>
21
22 #include "para.h"
23 #include "error.h"
24 #include "afh.h"
25 #include "string.h"
26
27 /** \cond some defines and structs which are only used in this file */
28
29 /*
30 * MIN_CONSEC_GOOD_FRAMES defines how many consecutive valid MP3 frames we need
31 * to see before we decide we are looking at a real MP3 file
32 */
33 #define MIN_CONSEC_GOOD_FRAMES 4
34 #define FRAME_HEADER_SIZE 4
35 #define MIN_FRAME_SIZE 21
36
37 struct mp3header {
38 unsigned long sync;
39 unsigned int version;
40 unsigned int layer;
41 unsigned int crc;
42 unsigned int bitrate;
43 unsigned int freq;
44 unsigned int padding;
45 unsigned int mode;
46 unsigned int copyright;
47 unsigned int original;
48 unsigned int emphasis;
49 };
50
51 /** \endcond */
52 static const int frequencies[3][4] = {
53 {22050,24000,16000,50000}, /* MPEG 2.0 */
54 {44100,48000,32000,50000}, /* MPEG 1.0 */
55 {11025,12000,8000,50000} /* MPEG 2.5 */
56 };
57
58 static const int mp3info_bitrate[2][3][14] = {
59 { /* MPEG 2.0 */
60 {32,48,56,64,80,96,112,128,144,160,176,192,224,256}, /* layer 1 */
61 {8,16,24,32,40,48,56,64,80,96,112,128,144,160}, /* layer 2 */
62 {8,16,24,32,40,48,56,64,80,96,112,128,144,160} /* layer 3 */
63 },
64
65 { /* MPEG 1.0 */
66 {32,64,96,128,160,192,224,256,288,320,352,384,416,448}, /* layer 1 */
67 {32,48,56,64,80,96,112,128,160,192,224,256,320,384}, /* layer 2 */
68 {32,40,48,56,64,80,96,112,128,160,192,224,256,320} /* layer 3 */
69 }
70 };
71
72 static const int frame_size_index[] = {24000, 72000, 72000};
73 static const char *mode_text[] = {"stereo", "joint stereo", "dual channel", "mono", "invalid"};
74
75 #ifdef HAVE_LIBID3TAG
76
77 #include <id3tag.h>
78
79 static char *get_latin1(id3_ucs4_t const *string)
80 {
81 if (!string)
82 return NULL;
83 return (char *)id3_ucs4_latin1duplicate(string);
84 }
85
86 static char *get_stringlist(union id3_field *field)
87 {
88 unsigned int k, nstrings = id3_field_getnstrings(field);
89 char *result = NULL;
90
91 for (k = 0; k < nstrings; k++) {
92 char *tmp = (char *)get_latin1(id3_field_getstrings(field, k));
93 if (result) {
94 char *tmp2 = result;
95 result = make_message("%s %s", tmp2, tmp);
96 free(tmp);
97 free(tmp2);
98 } else
99 result = tmp;
100 }
101 return result;
102 }
103
104 static char *get_string(union id3_field *field)
105 {
106 id3_ucs4_t const *string = id3_field_getfullstring(field);
107
108 return get_latin1(string);
109 }
110
111 #define FOR_EACH_FIELD(f, j, fr) for (j = 0; j < (fr)->nfields && \
112 (f = id3_frame_field((fr), j)); j++)
113
114 static char *get_strings(struct id3_frame *fr)
115 {
116 int j;
117 union id3_field *field;
118
119 FOR_EACH_FIELD(field, j, fr) {
120 enum id3_field_type type = id3_field_type(field);
121
122 if (type == ID3_FIELD_TYPE_STRINGLIST)
123 return get_stringlist(field);
124 if (type == ID3_FIELD_TYPE_STRINGFULL)
125 return get_string(field);
126 }
127 return NULL;
128 }
129
130 static void mp3_get_id3(__a_unused unsigned char *map,
131 __a_unused size_t numbytes, int fd, struct taginfo *tags)
132 {
133 int i;
134 struct id3_tag *id3_t;
135 struct id3_file *id3_f = id3_file_fdopen(fd, ID3_FILE_MODE_READONLY);
136
137 if (!id3_f)
138 return;
139 id3_t = id3_file_tag(id3_f);
140 if (!id3_t) {
141 id3_file_close(id3_f);
142 return;
143 }
144 for (i = 0; i < id3_t->nframes; i++) {
145 struct id3_frame *fr = id3_t->frames[i];
146 if (!strcmp(fr->id, "TIT2")) {
147 if (!tags->title)
148 tags->title = get_strings(fr);
149 continue;
150 }
151 if (!strcmp(fr->id, "TPE1")) {
152 if (!tags->artist)
153 tags->artist = get_strings(fr);
154 continue;
155 }
156 if (!strcmp(fr->id, "TALB")) {
157 if (!tags->album)
158 tags->album = get_strings(fr);
159 continue;
160 }
161 if (!strcmp(fr->id, "TDRC")) {
162 if (!tags->year)
163 tags->year = get_strings(fr);
164 continue;
165 }
166 if (!strcmp(fr->id, "COMM")) {
167 if (!tags->comment)
168 tags->comment = get_strings(fr);
169 continue;
170 }
171 }
172 id3_file_close(id3_f);
173 }
174
175 #else /* HAVE_LIBID3TAG */
176
177 /*
178 * Remove trailing whitespace from the end of a string
179 */
180 static char *unpad(char *string)
181 {
182 char *pos = string + strlen(string) - 1;
183 while (para_isspace(pos[0]))
184 (pos--)[0] = 0;
185 return string;
186 }
187
188 static void mp3_get_id3(unsigned char *map, size_t numbytes, __a_unused int fd,
189 struct taginfo *tags)
190 {
191 char title[31], artist[31], album[31], year[5], comment[31];
192 off_t fpos;
193
194 if (numbytes < 128 || strncmp("TAG", (char *)map + numbytes - 128, 3)) {
195 PARA_DEBUG_LOG("no id3 v1 tag\n");
196 return;
197 }
198 fpos = numbytes - 125;
199 memcpy(title, map + fpos, 30);
200 fpos += 30;
201 title[30] = '\0';
202 memcpy(artist, map + fpos, 30);
203 fpos += 30;
204 artist[30] = '\0';
205 memcpy(album, map + fpos, 30);
206 fpos += 30;
207 album[30] = '\0';
208 memcpy(year, map + fpos, 4);
209 fpos += 4;
210 year[4] = '\0';
211 memcpy(comment, map + fpos, 30);
212 comment[30] = '\0';
213 unpad(title);
214 unpad(artist);
215 unpad(album);
216 unpad(year);
217 unpad(comment);
218 tags->artist = para_strdup(artist);
219 tags->title = para_strdup(title);
220 tags->year = para_strdup(year);
221 tags->album = para_strdup(album);
222 tags->comment = para_strdup(comment);
223 }
224 #endif /* HAVE_LIBID3TAG */
225
226 static int header_frequency(struct mp3header *h)
227 {
228 if (h->version > 2 || h->freq > 3)
229 return -E_HEADER_FREQ;
230 return frequencies[h->version][h->freq];
231 }
232
233 static const char *header_mode(struct mp3header *h)
234 {
235 if (h->mode > 4)
236 h->mode = 4; /* invalid */
237 return mode_text[h->mode];
238 }
239
240 static int header_channels(struct mp3header *h)
241 {
242 if (h->mode > 3)
243 return 0;
244 if (h->mode < 3)
245 return 2;
246 return 1;
247 }
248
249 static int header_bitrate(struct mp3header *h)
250 {
251 if (!h->layer || h->layer > 3 || h->bitrate > 14 || !h->bitrate)
252 return -E_HEADER_BITRATE;
253 return mp3info_bitrate[h->version & 1][3 - h->layer][h->bitrate - 1];
254 }
255
256 static int frame_length(struct mp3header *header)
257 {
258 int hb, hf = header_frequency(header);
259
260 if (hf < 0)
261 return hf;
262 hb = header_bitrate(header);
263 if (hb < 0)
264 return hb;
265 if (header->sync != 0xFFE || header->layer > 3)
266 return -E_FRAME;
267 return frame_size_index[3 - header->layer] *
268 ((header->version & 1) + 1) * hb / hf
269 + header->padding;
270 }
271
272 static int compare_headers(struct mp3header *h1,struct mp3header *h2)
273 {
274 if ((*(unsigned int*)h1) == (*(unsigned int*)h2))
275 return 1;
276 if ((h1->version == h2->version) &&
277 (h1->layer == h2->layer) &&
278 (h1->crc == h2->crc) &&
279 (h1->freq == h2->freq) &&
280 (h1->mode == h2->mode) &&
281 (h1->copyright == h2->copyright) &&
282 (h1->original == h2->original) &&
283 (h1->emphasis == h2->emphasis))
284 return 1;
285 return 0;
286 }
287
288 /*
289 * get next MP3 frame header.
290 *
291 * On success, the header frame length is returned and the given header
292 * structure that is filled in. A return value of zero means that we did not
293 * retrieve a valid frame header, and a negative return value indicates an
294 * error.
295 */
296 static int get_header(unsigned char *map, size_t numbytes, off_t *fpos,
297 struct mp3header *header)
298 {
299 int fl, ret;
300
301 if (*fpos + FRAME_HEADER_SIZE > numbytes) {
302 *fpos = numbytes - 1;
303 header->sync = 0;
304 return 0;
305 }
306 header->layer = (map[*fpos + 1] >> 1) & 3;
307 header->sync = (((int)map[*fpos]<<4) | ((int)(map[*fpos + 1]&0xE0)>>4));
308 if (map[*fpos + 1] & 0x10)
309 header->version = (map[*fpos + 1] >> 3) & 1;
310 else
311 header->version = 2;
312 if ((header->sync != 0xFFE) || (header->layer != 1)) {
313 ret = 0;
314 header->sync = 0;
315 goto out;
316 }
317 header->crc = map[*fpos + 1] & 1;
318 header->bitrate = (map[*fpos + 2] >> 4) & 0x0F;
319 header->freq = (map[*fpos + 2] >> 2) & 0x3;
320 header->padding = (map[*fpos + 2] >>1) & 0x1;
321 header->mode = (map[*fpos + 3] >> 6) & 0x3;
322 fl = frame_length(header);
323 ret = (fl >= MIN_FRAME_SIZE)? fl : -E_FRAME_LENGTH;
324 out:
325 *fpos += FRAME_HEADER_SIZE;
326 return ret;
327 }
328
329 /*
330 * find the next mp3 header
331 *
332 * Return the length of the next frame header or zero if the end of the file is
333 * reached.
334 */
335 static int mp3_seek_next_header(unsigned char *map, size_t numbytes, off_t *fpos,
336 struct mp3header *result)
337 {
338 int k, l = 0, first_len;
339 struct mp3header h, h2;
340 long valid_start = 0;
341
342 for (; *fpos < numbytes; (*fpos)++) {
343 if (map[*fpos] != 0xff)
344 continue;
345 valid_start = *fpos;
346 first_len = get_header(map, numbytes, fpos, &h);
347 if (first_len <= 0)
348 continue;
349 *fpos += first_len - FRAME_HEADER_SIZE;
350 for (k = 1; k < MIN_CONSEC_GOOD_FRAMES; k++) {
351 if ((l = get_header(map, numbytes, fpos, &h2)) <= 0)
352 break;
353 if (!compare_headers(&h, &h2))
354 break;
355 *fpos += l - FRAME_HEADER_SIZE;
356 }
357 if (k == MIN_CONSEC_GOOD_FRAMES) {
358 *fpos = valid_start;
359 *result = h2;
360 return first_len;
361 }
362 }
363 return 0;
364 }
365
366 static int find_valid_start(unsigned char *map, size_t numbytes, off_t *fpos,
367 struct mp3header *header)
368 {
369 int frame_len;
370
371 frame_len = get_header(map, numbytes, fpos, header);
372 if (frame_len < 0)
373 return frame_len;
374 if (!frame_len) {
375 frame_len = mp3_seek_next_header(map, numbytes, fpos, header);
376 if (frame_len <= 0)
377 return frame_len;
378 } else
379 *fpos -= FRAME_HEADER_SIZE;
380 if (frame_len <= 1)
381 return -E_FRAME_LENGTH;
382 return frame_len;
383 }
384
385 static int mp3_read_info(unsigned char *map, size_t numbytes, int fd,
386 struct afh_info *afhi)
387 {
388 uint64_t freq_sum = 0, br_sum = 0;
389 int fl = 0, ret, len = 0, old_br = -1, vbr = 0;
390 struct timeval total_time = {0, 0};
391 unsigned chunk_table_size = 1000; /* gets increased on demand */
392 off_t fpos = 0;
393 struct mp3header header;
394
395 afhi->chunks_total = 0;
396 afhi->chunk_table = para_malloc(chunk_table_size * sizeof(uint32_t));
397 while (1) {
398 int freq, br;
399 struct timeval tmp, cct; /* current chunk time */
400 fpos += len;
401 len = find_valid_start(map, numbytes, &fpos, &header);
402 if (len <= 0) {
403 size_t end;
404 ret = -E_MP3_INFO;
405 if (!afhi->chunks_total)
406 goto err_out;
407 end = afhi->chunk_table[afhi->chunks_total - 1] + fl;
408 afhi->chunk_table[afhi->chunks_total]
409 = PARA_MIN(end, numbytes);
410 break;
411 }
412 ret = header_frequency(&header);
413 if (ret < 0)
414 continue;
415 freq = ret;
416 ret = header_bitrate(&header);
417 if (ret < 0)
418 continue;
419 br = ret;
420 ret = frame_length(&header);
421 if (ret < 0)
422 continue;
423 fl = ret;
424 tmp.tv_sec = fl;
425 tmp.tv_usec = 0;
426 tv_divide(br * 125, &tmp, &cct);
427 tv_add(&cct, &total_time, &tmp);
428 total_time = tmp;
429 if (afhi->chunks_total >= chunk_table_size) {
430 chunk_table_size *= 2;
431 afhi->chunk_table = para_realloc(afhi->chunk_table,
432 chunk_table_size * sizeof(uint32_t));
433 }
434 afhi->chunk_table[afhi->chunks_total] = fpos;
435 afhi->chunks_total++;
436 freq_sum += freq;
437 br_sum += br;
438 if (afhi->chunks_total != 1 && old_br != br)
439 vbr = 1;
440 old_br = br;
441 }
442 ret = -E_MP3_INFO;
443 if (!freq_sum || !br_sum)
444 goto err_out;
445 afhi->bitrate = br_sum / afhi->chunks_total;
446 afhi->frequency = freq_sum / afhi->chunks_total;
447 afhi->channels = header_channels(&header);
448 afhi->seconds_total = (tv2ms(&total_time) + 500) / 1000;
449 tv_divide(afhi->chunks_total, &total_time, &afhi->chunk_tv);
450 PARA_DEBUG_LOG("%lu chunks, each %lums\n", afhi->chunks_total,
451 tv2ms(&afhi->chunk_tv));
452 afhi->techinfo = make_message("%cbr, %s", vbr? 'v' : 'c',
453 header_mode(&header));
454 mp3_get_id3(map, numbytes, fd, &afhi->tags);
455 return 1;
456 err_out:
457 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
458 free(afhi->chunk_table);
459 return ret;
460 }
461
462 /*
463 * Read mp3 information from audio file
464 */
465 static int mp3_get_file_info(char *map, size_t numbytes, int fd,
466 struct afh_info *afhi)
467 {
468 int ret;
469
470 ret = mp3_read_info((unsigned char *)map, numbytes, fd, afhi);
471 if (ret < 0)
472 return ret;
473 if (afhi->seconds_total < 2 || !afhi->chunks_total)
474 return -E_MP3_INFO;
475 return 1;
476 }
477
478 static const char* mp3_suffixes[] = {"mp3", NULL};
479
480 /**
481 * the init function of the mp3 audio format handler
482 *
483 * \param afh pointer to the struct to initialize
484 */
485 void mp3_init(struct audio_format_handler *afh)
486 {
487 afh->get_file_info = mp3_get_file_info;
488 afh->suffixes = mp3_suffixes;
489 }