2 * Copyright (C) 2003-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file mp3_afh.c para_server's mp3 audio format handler */
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
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>
26 /** \cond some defines and structs which are only used in this file */
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
32 #define MIN_CONSEC_GOOD_FRAMES 4
33 #define FRAME_HEADER_SIZE 4
34 #define MIN_FRAME_SIZE 21
45 unsigned int copyright;
46 unsigned int original;
47 unsigned int emphasis;
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 */
57 static const int mp3info_bitrate[2][3][14] = {
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 */
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 */
71 static const int frame_size_index[] = {24000, 72000, 72000};
72 static const char *mode_text[] = {"stereo", "joint stereo", "dual channel", "mono", "invalid"};
78 static char *get_latin1(id3_ucs4_t const *string)
82 return (char *)id3_ucs4_latin1duplicate(string);
85 static char *get_stringlist(union id3_field *field)
87 unsigned int k, nstrings = id3_field_getnstrings(field);
90 for (k = 0; k < nstrings; k++) {
91 char *tmp = (char *)get_latin1(id3_field_getstrings(field, k));
94 result = make_message("%s %s", tmp2, tmp);
103 static char *get_string(union id3_field *field)
105 id3_ucs4_t const *string = id3_field_getfullstring(field);
107 return get_latin1(string);
110 #define FOR_EACH_FIELD(f, j, fr) for (j = 0; j < (fr)->nfields && \
111 (f = id3_frame_field((fr), j)); j++)
113 static char *get_strings(struct id3_frame *fr)
116 union id3_field *field;
118 FOR_EACH_FIELD(field, j, fr) {
119 enum id3_field_type type = id3_field_type(field);
121 if (type == ID3_FIELD_TYPE_STRINGLIST)
122 return get_stringlist(field);
123 if (type == ID3_FIELD_TYPE_STRINGFULL)
124 return get_string(field);
129 static void mp3_get_id3(__a_unused unsigned char *map,
130 __a_unused size_t numbytes, int fd, struct taginfo *tags)
133 struct id3_tag *id3_t;
134 struct id3_file *id3_f = id3_file_fdopen(fd, ID3_FILE_MODE_READONLY);
138 id3_t = id3_file_tag(id3_f);
140 id3_file_close(id3_f);
143 for (i = 0; i < id3_t->nframes; i++) {
144 struct id3_frame *fr = id3_t->frames[i];
145 if (!strcmp(fr->id, "TIT2")) {
147 tags->title = get_strings(fr);
150 if (!strcmp(fr->id, "TPE1")) {
152 tags->artist = get_strings(fr);
155 if (!strcmp(fr->id, "TALB")) {
157 tags->album = get_strings(fr);
160 if (!strcmp(fr->id, "TDRC")) {
162 tags->year = get_strings(fr);
165 if (!strcmp(fr->id, "COMM")) {
167 tags->comment = get_strings(fr);
171 id3_file_close(id3_f);
174 #else /* HAVE_LIBID3TAG */
177 * Remove trailing whitespace from the end of a string
179 static char *unpad(char *string)
181 char *pos = string + strlen(string) - 1;
182 while (para_isspace(pos[0]))
187 static void mp3_get_id3(unsigned char *map, size_t numbytes, __a_unused int fd,
188 struct taginfo *tags)
190 char title[31], artist[31], album[31], year[5], comment[31];
193 if (numbytes < 128 || strncmp("TAG", (char *)map + numbytes - 128, 3)) {
194 PARA_DEBUG_LOG("no id3 v1 tag\n");
197 fpos = numbytes - 125;
198 memcpy(title, map + fpos, 30);
201 memcpy(artist, map + fpos, 30);
204 memcpy(album, map + fpos, 30);
207 memcpy(year, map + fpos, 4);
210 memcpy(comment, map + fpos, 30);
217 tags->artist = para_strdup(artist);
218 tags->title = para_strdup(title);
219 tags->year = para_strdup(year);
220 tags->album = para_strdup(album);
221 tags->comment = para_strdup(comment);
223 #endif /* HAVE_LIBID3TAG */
225 static int header_frequency(struct mp3header *h)
227 if (h->version > 2 || h->freq > 3)
228 return -E_HEADER_FREQ;
229 return frequencies[h->version][h->freq];
232 static const char *header_mode(struct mp3header *h)
235 h->mode = 4; /* invalid */
236 return mode_text[h->mode];
239 static int header_channels(struct mp3header *h)
248 static int header_bitrate(struct mp3header *h)
250 if (!h->layer || h->layer > 3 || h->bitrate > 14 || !h->bitrate)
251 return -E_HEADER_BITRATE;
252 return mp3info_bitrate[h->version & 1][3 - h->layer][h->bitrate - 1];
255 static int frame_length(struct mp3header *header)
257 int hb, hf = header_frequency(header);
261 hb = header_bitrate(header);
264 if (header->sync != 0xFFE || header->layer > 3)
266 return frame_size_index[3 - header->layer] *
267 ((header->version & 1) + 1) * hb / hf
271 static int compare_headers(struct mp3header *h1,struct mp3header *h2)
273 if ((*(unsigned int*)h1) == (*(unsigned int*)h2))
275 if ((h1->version == h2->version) &&
276 (h1->layer == h2->layer) &&
277 (h1->crc == h2->crc) &&
278 (h1->freq == h2->freq) &&
279 (h1->mode == h2->mode) &&
280 (h1->copyright == h2->copyright) &&
281 (h1->original == h2->original) &&
282 (h1->emphasis == h2->emphasis))
288 * get next MP3 frame header.
290 * On success, the header frame length is returned and the given header
291 * structure that is filled in. A return value of zero means that we did not
292 * retrieve a valid frame header, and a negative return value indicates an
295 static int get_header(unsigned char *map, size_t numbytes, off_t *fpos,
296 struct mp3header *header)
300 if (*fpos + FRAME_HEADER_SIZE > numbytes) {
301 *fpos = numbytes - 1;
305 header->layer = (map[*fpos + 1] >> 1) & 3;
306 header->sync = (((int)map[*fpos]<<4) | ((int)(map[*fpos + 1]&0xE0)>>4));
307 if (map[*fpos + 1] & 0x10)
308 header->version = (map[*fpos + 1] >> 3) & 1;
311 if ((header->sync != 0xFFE) || (header->layer != 1)) {
316 header->crc = map[*fpos + 1] & 1;
317 header->bitrate = (map[*fpos + 2] >> 4) & 0x0F;
318 header->freq = (map[*fpos + 2] >> 2) & 0x3;
319 header->padding = (map[*fpos + 2] >>1) & 0x1;
320 header->mode = (map[*fpos + 3] >> 6) & 0x3;
321 fl = frame_length(header);
322 ret = (fl >= MIN_FRAME_SIZE)? fl : -E_FRAME_LENGTH;
324 *fpos += FRAME_HEADER_SIZE;
329 * find the next mp3 header
331 * Return the length of the next frame header or zero if the end of the file is
334 static int mp3_seek_next_header(unsigned char *map, size_t numbytes, off_t *fpos,
335 struct mp3header *result)
337 int k, l = 0, first_len;
338 struct mp3header h, h2;
339 long valid_start = 0;
341 for (; *fpos < numbytes; (*fpos)++) {
342 if (map[*fpos] != 0xff)
345 first_len = get_header(map, numbytes, fpos, &h);
348 *fpos += first_len - FRAME_HEADER_SIZE;
349 for (k = 1; k < MIN_CONSEC_GOOD_FRAMES; k++) {
350 if ((l = get_header(map, numbytes, fpos, &h2)) <= 0)
352 if (!compare_headers(&h, &h2))
354 *fpos += l - FRAME_HEADER_SIZE;
356 if (k == MIN_CONSEC_GOOD_FRAMES) {
365 static int find_valid_start(unsigned char *map, size_t numbytes, off_t *fpos,
366 struct mp3header *header)
370 frame_len = get_header(map, numbytes, fpos, header);
374 frame_len = mp3_seek_next_header(map, numbytes, fpos, header);
378 *fpos -= FRAME_HEADER_SIZE;
380 return -E_FRAME_LENGTH;
384 static int mp3_read_info(unsigned char *map, size_t numbytes, int fd,
385 struct afh_info *afhi)
387 uint64_t freq_sum = 0, br_sum = 0;
388 int fl = 0, ret, len = 0, old_br = -1, vbr = 0;
389 struct timeval total_time = {0, 0};
390 unsigned chunk_table_size = 1000; /* gets increased on demand */
392 struct mp3header header;
394 afhi->chunks_total = 0;
395 afhi->chunk_table = para_malloc(chunk_table_size * sizeof(uint32_t));
398 struct timeval tmp, cct; /* current chunk time */
400 len = find_valid_start(map, numbytes, &fpos, &header);
404 if (!afhi->chunks_total)
406 end = afhi->chunk_table[afhi->chunks_total - 1] + fl;
407 afhi->chunk_table[afhi->chunks_total]
408 = PARA_MIN(end, numbytes);
411 ret = header_frequency(&header);
415 ret = header_bitrate(&header);
419 ret = frame_length(&header);
425 tv_divide(br * 125, &tmp, &cct);
426 tv_add(&cct, &total_time, &tmp);
428 if (afhi->chunks_total >= chunk_table_size) {
429 chunk_table_size *= 2;
430 afhi->chunk_table = para_realloc(afhi->chunk_table,
431 chunk_table_size * sizeof(uint32_t));
433 afhi->chunk_table[afhi->chunks_total] = fpos;
434 afhi->chunks_total++;
437 if (afhi->chunks_total != 1 && old_br != br)
442 if (!freq_sum || !br_sum)
444 afhi->bitrate = br_sum / afhi->chunks_total;
445 afhi->frequency = freq_sum / afhi->chunks_total;
446 afhi->channels = header_channels(&header);
447 afhi->seconds_total = (tv2ms(&total_time) + 500) / 1000;
448 tv_divide(afhi->chunks_total, &total_time, &afhi->chunk_tv);
449 PARA_DEBUG_LOG("%lu chunks, each %lums\n", afhi->chunks_total,
450 tv2ms(&afhi->chunk_tv));
451 afhi->techinfo = make_message("%cbr, %s", vbr? 'v' : 'c',
452 header_mode(&header));
453 mp3_get_id3(map, numbytes, fd, &afhi->tags);
456 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
457 free(afhi->chunk_table);
462 * Read mp3 information from audio file
464 static int mp3_get_file_info(char *map, size_t numbytes, int fd,
465 struct afh_info *afhi)
469 ret = mp3_read_info((unsigned char *)map, numbytes, fd, afhi);
472 if (afhi->seconds_total < 2 || !afhi->chunks_total)
477 static const char* mp3_suffixes[] = {"mp3", NULL};
480 * the init function of the mp3 audio format handler
482 * \param afh pointer to the struct to initialize
484 void mp3_init(struct audio_format_handler *afh)
486 afh->get_file_info = mp3_get_file_info;
487 afh->suffixes = mp3_suffixes;