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