2 * Copyright (C) 2003-2007 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;
59 struct mp3header header;
66 static const int frequencies[3][4] = {
67 {22050,24000,16000,50000}, /* MPEG 2.0 */
68 {44100,48000,32000,50000}, /* MPEG 1.0 */
69 {11025,12000,8000,50000} /* MPEG 2.5 */
72 static const int mp3info_bitrate[2][3][14] = {
74 {32,48,56,64,80,96,112,128,144,160,176,192,224,256}, /* layer 1 */
75 {8,16,24,32,40,48,56,64,80,96,112,128,144,160}, /* layer 2 */
76 {8,16,24,32,40,48,56,64,80,96,112,128,144,160} /* layer 3 */
80 {32,64,96,128,160,192,224,256,288,320,352,384,416,448}, /* layer 1 */
81 {32,48,56,64,80,96,112,128,160,192,224,256,320,384}, /* layer 2 */
82 {32,40,48,56,64,80,96,112,128,160,192,224,256,320} /* layer 3 */
86 static const int frame_size_index[] = {24000, 72000, 72000};
87 static const char *mode_text[] = {"stereo", "joint stereo", "dual channel", "mono", "invalid"};
89 static struct mp3info mp3;
91 static int header_frequency(struct mp3header *h)
93 if (h->version > 2 || h->freq > 3)
94 return -E_HEADER_FREQ;
95 return frequencies[h->version][h->freq];
98 static const char *header_mode(struct mp3header *h)
101 h->mode = 4; /* invalid */
102 return mode_text[h->mode];
105 static int header_channels(struct mp3header *h)
114 static int header_bitrate(struct mp3header *h)
116 if (!h->layer || h->layer > 3 || h->bitrate > 14 || !h->bitrate)
117 return -E_HEADER_BITRATE;
118 return mp3info_bitrate[h->version & 1][3 - h->layer][h->bitrate - 1];
121 static int frame_length(struct mp3header *header)
123 int hb, hf = header_frequency(header);
127 hb = header_bitrate(header);
130 if (header->sync != 0xFFE || header->layer > 3)
132 return frame_size_index[3 - header->layer] *
133 ((header->version & 1) + 1) * hb / hf
137 static void write_info_str(struct afh_info *afhi)
139 int v = mp3.id3_isvalid;
141 snprintf(afhi->info_string, MMD_INFO_SIZE,
142 "%s: %cbr, %s\n" /* audio file info*/
143 "%s: %s, by %s\n" /* taginfo1 */
144 "%s: A: %s, Y: %s, C: %s\n", /* taginfo 2*/
145 status_item_list[SI_AUDIO_FILE_INFO], mp3.vbr? 'v' : 'c',
146 header_mode(&mp3.header),
147 status_item_list[SI_TAGINFO1], v && *mp3.id3.title?
148 mp3.id3.title : "(title tag not set)",
149 v && *mp3.id3.artist?
150 mp3.id3.artist : "(artist tag not set)",
151 status_item_list[SI_TAGINFO2],
153 mp3.id3.album : "(album tag not set)",
154 v && *mp3.id3.year? mp3.id3.year : "????",
155 v && *mp3.id3.comment?
156 mp3.id3.comment : "(comment tag not set)"
161 * Remove trailing whitespace from the end of a string
163 static char *unpad(char *string)
165 char *pos = string + strlen(string) - 1;
166 while (para_isspace(pos[0]))
171 static int compare_headers(struct mp3header *h1,struct mp3header *h2)
173 if ((*(uint*)h1) == (*(uint*)h2))
175 if ((h1->version == h2->version) &&
176 (h1->layer == h2->layer) &&
177 (h1->crc == h2->crc) &&
178 (h1->freq == h2->freq) &&
179 (h1->mode == h2->mode) &&
180 (h1->copyright == h2->copyright) &&
181 (h1->original == h2->original) &&
182 (h1->emphasis == h2->emphasis))
188 * get next MP3 frame header.
190 * On success, the header frame length is returned and the given header
191 * structure that is filled in. A return value of zero means that we did not
192 * retrieve a valid frame header, and a negative return value indicates an
195 static int get_header(unsigned char *map, size_t numbytes, off_t *fpos,
196 struct mp3header *header)
200 if (*fpos + FRAME_HEADER_SIZE > numbytes) {
201 *fpos = numbytes - 1;
205 header->layer = (map[*fpos + 1] >> 1) & 3;
206 header->sync = (((int)map[*fpos]<<4) | ((int)(map[*fpos + 1]&0xE0)>>4));
207 if (map[*fpos + 1] & 0x10)
208 header->version = (map[*fpos + 1] >> 3) & 1;
211 if ((header->sync != 0xFFE) || (header->layer != 1)) {
216 header->crc = map[*fpos + 1] & 1;
217 header->bitrate = (map[*fpos + 2] >> 4) & 0x0F;
218 header->freq = (map[*fpos + 2] >> 2) & 0x3;
219 header->padding = (map[*fpos + 2] >>1) & 0x1;
220 header->mode = (map[*fpos + 3] >> 6) & 0x3;
221 fl = frame_length(header);
222 ret = (fl >= MIN_FRAME_SIZE)? fl : -E_FRAME_LENGTH;
224 *fpos += FRAME_HEADER_SIZE;
229 * find the next mp3 header
231 * Return the length of the next frame header or zero if the end of the file is
234 static int mp3_seek_next_header(unsigned char *map, size_t numbytes, off_t *fpos)
236 int k, l = 0, first_len;
237 struct mp3header h, h2;
238 long valid_start = 0;
240 for (; *fpos < numbytes; (*fpos)++) {
241 if (map[*fpos] != 0xff)
244 first_len = get_header(map, numbytes, fpos, &h);
247 *fpos += first_len - FRAME_HEADER_SIZE;
248 for (k = 1; k < MIN_CONSEC_GOOD_FRAMES; k++) {
249 if ((l = get_header(map, numbytes, fpos, &h2)) <= 0)
251 if (!compare_headers(&h, &h2))
253 *fpos += l - FRAME_HEADER_SIZE;
255 if (k == MIN_CONSEC_GOOD_FRAMES) {
257 memcpy(&(mp3.header), &h2, sizeof(struct mp3header));
264 static void mp3_get_id3(unsigned char *map, size_t numbytes, off_t *fpos)
267 mp3.id3.title[0] = '\0';
268 mp3.id3.artist[0] = '\0';
269 mp3.id3.album[0] = '\0';
270 mp3.id3.comment[0] = '\0';
271 mp3.id3.year[0] = '\0';
274 *fpos = numbytes - 128;
275 if (strncmp("TAG", (char *) map + *fpos, 3)) {
276 PARA_DEBUG_LOG("%s", "no id3 tag\n");
279 *fpos = numbytes - 125;
280 memcpy(mp3.id3.title, map + *fpos, 30);
282 mp3.id3.title[30] = '\0';
283 memcpy(mp3.id3.artist, map + *fpos, 30);
285 mp3.id3.artist[30] = '\0';
286 memcpy(mp3.id3.album, map + *fpos, 30);
288 mp3.id3.album[30] = '\0';
289 memcpy(mp3.id3.year, map + *fpos, 4);
291 mp3.id3.year[4] = '\0';
292 memcpy(mp3.id3.comment, map + *fpos, 30);
293 mp3.id3.comment[30] = '\0';
295 unpad(mp3.id3.title);
296 unpad(mp3.id3.artist);
297 unpad(mp3.id3.album);
299 unpad(mp3.id3.comment);
302 static int find_valid_start(unsigned char *map, size_t numbytes, off_t *fpos)
306 frame_len = get_header(map, numbytes, fpos, &mp3.header);
310 frame_len = mp3_seek_next_header(map, numbytes, fpos);
314 *fpos -= FRAME_HEADER_SIZE;
316 return -E_FRAME_LENGTH;
320 static int mp3_read_info(unsigned char *map, size_t numbytes,
321 struct afh_info *afhi)
323 long fl_avg = 0, freq_avg = 0, br_avg = 0;
324 int ret, len = 0, old_br = -1;
325 struct timeval total_time = {0, 0};
326 unsigned chunk_table_size = 1000; /* gets increased on demand */
329 afhi->chunks_total = 0;
330 afhi->chunk_table = para_malloc(chunk_table_size * sizeof(size_t));
331 mp3_get_id3(map, numbytes, &fpos);
335 unsigned long freq, br, fl;
336 struct timeval tmp, cct; /* current chunk time */
338 len = find_valid_start(map, numbytes, &fpos);
341 ret = header_frequency(&mp3.header);
345 ret = header_bitrate(&mp3.header);
349 ret = frame_length(&mp3.header);
355 tv_divide(br * 125, &tmp, &cct);
356 tv_add(&cct, &total_time, &tmp);
358 //PARA_DEBUG_LOG("%s: br: %d, freq: %d, fl: %d, cct: %lu\n", __func__, br, freq, fl, cct.tv_usec);
359 if (afhi->chunks_total >= chunk_table_size) {
360 chunk_table_size *= 2;
361 afhi->chunk_table = para_realloc(afhi->chunk_table,
362 chunk_table_size * sizeof(size_t));
364 afhi->chunk_table[afhi->chunks_total] = fpos;
365 // if (afhi->chunks_total < 10 || !(afhi->chunks_total % 1000))
366 // PARA_INFO_LOG("chunk #%lu: %zd\n", afhi->chunks_total,
367 // afhi->chunk_table[afhi->chunks_total]);
368 afhi->chunks_total++;
369 if (afhi->chunks_total == 1) {
376 freq_avg += ((long)freq - freq_avg) / ((long)afhi->chunks_total + 1);
377 fl_avg += ((long)fl - fl_avg) / ((long)afhi->chunks_total + 1);
378 br_avg += ((long)br - br_avg) / ((long)afhi->chunks_total + 1);
384 if (!afhi->chunks_total || !freq_avg || !br_avg)
386 afhi->chunk_table[afhi->chunks_total] = numbytes - 1;
387 afhi->bitrate = br_avg;
388 afhi->frequency = freq_avg;
389 afhi->channels = header_channels(&mp3.header);
390 afhi->seconds_total = (tv2ms(&total_time) + 500) / 1000;
391 tv_divide(afhi->chunks_total, &total_time, &afhi->chunk_tv);
392 PARA_DEBUG_LOG("%lu chunks, each %lums\n", afhi->chunks_total,
393 tv2ms(&afhi->chunk_tv));
394 tv_scale(3, &afhi->chunk_tv, &afhi->eof_tv);
395 PARA_DEBUG_LOG("eof timeout: %lu\n", tv2ms(&afhi->eof_tv));
398 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
399 free(afhi->chunk_table);
404 * Read mp3 information from audio file
406 int mp3_get_file_info(char *map, size_t numbytes,
407 struct afh_info *afhi)
411 ret = mp3_read_info((unsigned char *)map, numbytes, afhi);
414 write_info_str(afhi);
415 if (afhi->seconds_total < 2 || !afhi->chunks_total)
420 static const char* mp3_suffixes[] = {"mp3", NULL};
423 * the init function of the mp3 audio format handler
425 * \param afh pointer to the struct to initialize
427 void mp3_init(struct audio_format_handler *afh)
429 afh->get_file_info = mp3_get_file_info;
430 afh->suffixes = mp3_suffixes;