2 * Copyright (C) 2003-2007 Andre Noll <maan@systemlinux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /** \file mp3_afh.c para_server's mp3 audio format handler */
22 * This file is based in part on mp3tech.c and mp3tech.h, Copyright (C)
23 * 2000-2001 Cedric Tefft <cedric@earthling.net>, which in turn is based
26 * * MP3Info 0.5 by Ricardo Cerqueira <rmc@rccn.net>
27 * * MP3Stat 0.9 by Ed Sweetman <safemode@voicenet.com> and
28 * Johannes Overmann <overmann@iname.com>
31 #include "server.cmdline.h"
39 /** \cond some defines and structs which are only used in this file */
42 * MIN_CONSEC_GOOD_FRAMES defines how many consecutive valid MP3 frames we need
43 * to see before we decide we are looking at a real MP3 file
45 #define MIN_CONSEC_GOOD_FRAMES 4
47 #define FRAME_HEADER_SIZE 4
48 #define MIN_FRAME_SIZE 21
49 #define DEFAULT_INBUF_SIZE 8192
60 unsigned int copyright;
61 unsigned int original;
62 unsigned int emphasis;
75 struct mp3header header;
79 long unsigned br_average;
80 long unsigned seconds;
85 static int frequencies[3][4] = {
86 {22050,24000,16000,50000}, /* MPEG 2.0 */
87 {44100,48000,32000,50000}, /* MPEG 1.0 */
88 {11025,12000,8000,50000} /* MPEG 2.5 */
91 static int mp3info_bitrate[2][3][14] = {
93 {32,48,56,64,80,96,112,128,144,160,176,192,224,256}, /* layer 1 */
94 {8,16,24,32,40,48,56,64,80,96,112,128,144,160}, /* layer 2 */
95 {8,16,24,32,40,48,56,64,80,96,112,128,144,160} /* layer 3 */
99 {32,64,96,128,160,192,224,256,288,320,352,384,416,448}, /* layer 1 */
100 {32,48,56,64,80,96,112,128,160,192,224,256,320,384}, /* layer 2 */
101 {32,40,48,56,64,80,96,112,128,160,192,224,256,320} /* layer 3 */
105 static int frame_size_index[] = {24000, 72000, 72000};
106 static const char *mode_text[] = {"stereo", "joint stereo", "dual channel", "mono", "invalid"};
109 static struct mp3info mp3;
111 static size_t inbuf_size;
112 static struct audio_format_handler *af;
113 static ssize_t *chunk_table, num_chunks;
115 static int header_frequency(struct mp3header *h)
117 if (h->version > 2 || h->freq > 3)
118 return -E_HEADER_FREQ;
119 return frequencies[h->version][h->freq];
122 static const char *header_mode(struct mp3header *h)
125 h->mode = 4; /* invalid */
126 return mode_text[h->mode];
128 static int header_bitrate(struct mp3header *h)
130 if (h->layer > 3 || h->bitrate > 14)
131 return -E_HEADER_BITRATE;
132 return mp3info_bitrate[h->version & 1][3 - h->layer][h->bitrate - 1];
135 static int frame_length(struct mp3header *header)
137 int hb, hf = header_frequency(header);
141 hb = header_bitrate(header);
144 if (header->sync != 0xFFE || header->layer > 3)
146 return frame_size_index[3 - header->layer] *
147 ((header->version & 1) + 1) * hb / hf
151 static void write_info_str(char *info_str)
153 int v = mp3.id3_isvalid;
155 snprintf(info_str, MMD_INFO_SIZE,
156 "audio_file_info1:%d x %lums, %lu kbit/s (%cbr) %i KHz %s\n"
157 "audio_file_info2:%s, by %s\n"
158 "audio_file_info3:A: %s, Y: %s, C: %s\n",
160 tv2ms(&af->chunk_tv),
164 header_mode(&mp3.header),
165 v && *mp3.id3.title? mp3.id3.title : "(title tag not set)",
166 v && *mp3.id3.artist? mp3.id3.artist : "(artist tag not set)",
167 v && *mp3.id3.album? mp3.id3.album : "(album tag not set)",
168 v && *mp3.id3.year? mp3.id3.year : "????",
169 v && *mp3.id3.comment? mp3.id3.comment : "(comment tag not set)"
174 * Remove trailing whitespace from the end of a string
176 static char *unpad(char *string)
178 char *pos = string + strlen(string) - 1;
179 while (isspace(pos[0]))
184 static int compare_headers(struct mp3header *h1,struct mp3header *h2)
186 if ((*(uint*)h1) == (*(uint*)h2))
188 if ((h1->version == h2->version) &&
189 (h1->layer == h2->layer) &&
190 (h1->crc == h2->crc) &&
191 (h1->freq == h2->freq) &&
192 (h1->mode == h2->mode) &&
193 (h1->copyright == h2->copyright) &&
194 (h1->original == h2->original) &&
195 (h1->emphasis == h2->emphasis))
202 * get next MP3 frame header.
204 * \param stream to read the header from
205 * \param header structure that gets filled in by get_header()
207 * \return On success, the header frame length is returned. A return value of
208 * zero means that we did not retrieve a valid frame header, and a negative
209 * return value indicates an error.
211 static int get_header(FILE *file, struct mp3header *header)
213 unsigned char buffer[FRAME_HEADER_SIZE];
216 if (!file || !header)
217 return -E_MP3_NO_FILE;
218 ret = para_fread(buffer, FRAME_HEADER_SIZE, 1, file);
219 if (ret < FRAME_HEADER_SIZE) {
221 return ret < 0? ret : 0;
223 header->layer = (buffer[1] >> 1) & 3;
224 header->sync = (((int)buffer[0]<<4) | ((int)(buffer[1]&0xE0)>>4));
225 if (buffer[1] & 0x10)
226 header->version = (buffer[1] >> 3) & 1;
229 if ((header->sync != 0xFFE) || (header->layer != 1)) {
231 // PARA_DEBUG_LOG("%s: header not found\n", __func__);
234 header->crc = buffer[1] & 1;
235 header->bitrate = (buffer[2] >> 4) & 0x0F;
236 // PARA_DEBUG_LOG("%s: found header, bitrate: %u\n", __func__,
238 header->freq = (buffer[2] >> 2) & 0x3;
239 header->padding = (buffer[2] >>1) & 0x1;
240 header->mode = (buffer[3] >> 6) & 0x3;
241 fl = frame_length(header);
242 return (fl >= MIN_FRAME_SIZE)? fl : -E_FRAME_LENGTH;
246 * find the next mp3 header
248 * \return On success, the length of the next frame header. If the end of the
249 * file was reached, the function returns zero. On errors, a negative value is
253 static int mp3_seek_next_header(void)
255 int k, l = 0, c, first_len, ret;
256 struct mp3header h, h2;
257 long valid_start = 0;
260 while ((c = fgetc(infile)) != 255 && (c != EOF))
265 valid_start = ftell(infile);
266 first_len = get_header(infile, &h);
269 ret = para_fseek(infile, first_len - FRAME_HEADER_SIZE, SEEK_CUR);
272 for (k = 1; k < MIN_CONSEC_GOOD_FRAMES; k++) {
273 if ((l = get_header(infile, &h2)) <= 0)
275 if (!compare_headers(&h, &h2))
277 ret = para_fseek(infile, l - FRAME_HEADER_SIZE, SEEK_CUR);
281 if (k == MIN_CONSEC_GOOD_FRAMES) {
282 ret = para_fseek(infile, valid_start, SEEK_SET);
285 memcpy(&(mp3.header), &h2, sizeof(struct mp3header));
291 static int mp3_get_id3(void)
297 mp3.id3.title[0] = '\0';
298 mp3.id3.artist[0] = '\0';
299 mp3.id3.album[0] = '\0';
300 mp3.id3.comment[0] = '\0';
301 mp3.id3.year[0] = '\0';
302 ret = para_fseek(infile, -128, SEEK_END);
305 if (para_fread(fbuf, 1, 3, infile) < 0)
308 if (strcmp("TAG", fbuf)) {
309 PARA_INFO_LOG("%s", "no id3 tag\n");
312 ret = para_fseek(infile, -125, SEEK_END);
315 if (para_fread(mp3.id3.title, 1, 30, infile) != 30)
317 mp3.id3.title[30] = '\0';
318 if (para_fread(mp3.id3.artist, 1, 30, infile) != 30)
320 mp3.id3.artist[30] = '\0';
321 if (para_fread(mp3.id3.album, 1, 30, infile) != 30)
323 mp3.id3.album[30] = '\0';
324 if (para_fread(mp3.id3.year, 1, 4, infile) != 4)
326 mp3.id3.year[4] = '\0';
327 if (para_fread(mp3.id3.comment, 1, 30, infile) != 30)
329 mp3.id3.comment[30] = '\0';
331 unpad(mp3.id3.title);
332 unpad(mp3.id3.artist);
333 unpad(mp3.id3.album);
335 unpad(mp3.id3.comment);
339 static int find_valid_start(void)
344 return -E_MP3_NO_FILE;
345 frame_len = get_header(infile, &mp3.header);
349 frame_len = mp3_seek_next_header();
353 ret = para_fseek(infile, -FRAME_HEADER_SIZE, SEEK_CUR);
358 return -E_FRAME_LENGTH;
362 static int mp3_read_info(void)
364 long fl_avg = 0, freq_avg = 0, br_avg = 0;
365 int ret, len = 0, old_br = -1;
366 struct timeval total_time = {0, 0};
367 unsigned chunk_table_size = 1000; /* gets increased on demand */
370 inbuf = para_malloc(DEFAULT_INBUF_SIZE);
371 inbuf_size = DEFAULT_INBUF_SIZE;
372 chunk_table = para_malloc(chunk_table_size * sizeof(size_t));
381 struct timeval tmp, cct; /* current chunk time */
383 ret = para_fseek(infile, len, SEEK_CUR);
387 len = find_valid_start();
390 freq = header_frequency(&mp3.header);
391 br = header_bitrate(&mp3.header);
392 fl = frame_length(&mp3.header);
393 if (freq < 0 || br < 0 || fl < 0)
397 tv_divide(br * 125, &tmp, &cct);
398 tv_add(&cct, &total_time, &tmp);
400 //PARA_DEBUG_LOG("%s: br: %d, freq: %d, fl: %d, cct: %lu\n", __func__, br, freq, fl, cct.tv_usec);
401 if (num_chunks >= chunk_table_size) {
402 chunk_table_size *= 2;
403 chunk_table = para_realloc(chunk_table,
404 chunk_table_size * sizeof(size_t));
406 chunk_table[num_chunks] = ftell(infile);
407 if (num_chunks < 10 || !(num_chunks % 1000))
408 PARA_INFO_LOG("chunk #%d: %zd\n", num_chunks,
409 chunk_table[num_chunks]);
411 if (num_chunks == 1) {
412 // entry = ftell(infile);
413 // PARA_INFO_LOG("entry: %zd\n", entry);
420 freq_avg += (freq - freq_avg) / (num_chunks + 1);
421 fl_avg += (fl - fl_avg) / (num_chunks + 1);
422 br_avg += (br - br_avg) / (num_chunks + 1);
428 if (!num_chunks || !freq_avg || !br_avg)
430 ret= para_fseek(infile, 0, SEEK_END);
433 chunk_table[num_chunks] = ftell(infile);
434 mp3.br_average = br_avg;
436 mp3.seconds = (tv2ms(&total_time) + 500) / 1000;
437 tv_divide(num_chunks, &total_time, &af->chunk_tv);
439 PARA_DEBUG_LOG("%zu chunks, each %lums\n", num_chunks, tv2ms(&af->chunk_tv));
440 tv_scale(3, &af->chunk_tv, &af->eof_tv);
441 PARA_DEBUG_LOG("eof timeout: %lu\n", tv2ms(&af->eof_tv));
444 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
451 * Read mp3 information from audio file
453 static int mp3_get_file_info(FILE *audio_file, char *info_str,
454 long unsigned *frames, int *seconds, size_t **vss_chunk_table)
459 return -E_MP3_NO_FILE;
461 ret = mp3_read_info();
466 write_info_str(info_str);
467 *frames = num_chunks;
468 *seconds = mp3.seconds;
469 *vss_chunk_table = chunk_table;
470 if (*seconds < 2 || !*frames)
475 static void mp3_close_audio_file(void)
485 static const char* mp3_suffixes[] = {"mp3", NULL};
488 * the init function of the mp3 audio format handler
490 * \param p pointer to the struct to initialize
492 void mp3_init(struct audio_format_handler *p)
495 af->get_file_info = mp3_get_file_info;
496 af->close_audio_file = mp3_close_audio_file;
497 af->get_header_info = NULL;
498 /* eof_tv gets overwritten in mp3_get_file_info() */
499 af->eof_tv.tv_sec = 0;
500 af->eof_tv.tv_usec = 100 * 1000;
501 af->suffixes = mp3_suffixes;