3630075941a2fccd355037b3b28af8486f80be41
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"
38 /** \cond some defines and structs which are only used in this file */
41 * MIN_CONSEC_GOOD_FRAMES defines how many consecutive valid MP3 frames we need
42 * to see before we decide we are looking at a real MP3 file
44 #define MIN_CONSEC_GOOD_FRAMES 4
45 #define FRAME_HEADER_SIZE 4
46 #define MIN_FRAME_SIZE 21
57 unsigned int copyright
;
58 unsigned int original
;
59 unsigned int emphasis
;
71 struct mp3header header
;
75 long unsigned br_average
;
80 static const int frequencies
[3][4] = {
81 {22050,24000,16000,50000}, /* MPEG 2.0 */
82 {44100,48000,32000,50000}, /* MPEG 1.0 */
83 {11025,12000,8000,50000} /* MPEG 2.5 */
86 static const int mp3info_bitrate
[2][3][14] = {
88 {32,48,56,64,80,96,112,128,144,160,176,192,224,256}, /* layer 1 */
89 {8,16,24,32,40,48,56,64,80,96,112,128,144,160}, /* layer 2 */
90 {8,16,24,32,40,48,56,64,80,96,112,128,144,160} /* layer 3 */
94 {32,64,96,128,160,192,224,256,288,320,352,384,416,448}, /* layer 1 */
95 {32,48,56,64,80,96,112,128,160,192,224,256,320,384}, /* layer 2 */
96 {32,40,48,56,64,80,96,112,128,160,192,224,256,320} /* layer 3 */
100 static const int frame_size_index
[] = {24000, 72000, 72000};
101 static const char *mode_text
[] = {"stereo", "joint stereo", "dual channel", "mono", "invalid"};
103 static struct mp3info mp3
;
105 static int header_frequency(struct mp3header
*h
)
107 if (h
->version
> 2 || h
->freq
> 3)
108 return -E_HEADER_FREQ
;
109 return frequencies
[h
->version
][h
->freq
];
112 static const char *header_mode(struct mp3header
*h
)
115 h
->mode
= 4; /* invalid */
116 return mode_text
[h
->mode
];
118 static int header_bitrate(struct mp3header
*h
)
120 if (h
->layer
> 3 || h
->bitrate
> 14)
121 return -E_HEADER_BITRATE
;
122 return mp3info_bitrate
[h
->version
& 1][3 - h
->layer
][h
->bitrate
- 1];
125 static int frame_length(struct mp3header
*header
)
127 int hb
, hf
= header_frequency(header
);
131 hb
= header_bitrate(header
);
134 if (header
->sync
!= 0xFFE || header
->layer
> 3)
136 return frame_size_index
[3 - header
->layer
] *
137 ((header
->version
& 1) + 1) * hb
/ hf
141 static void write_info_str(struct audio_format_info
*afi
)
143 int v
= mp3
.id3_isvalid
;
145 snprintf(afi
->info_string
, MMD_INFO_SIZE
,
146 "audio_file_info1:%lu x %lums, %lu kbit/s (%cbr) %i KHz %s\n"
147 "audio_file_info2:%s, by %s\n"
148 "audio_file_info3:A: %s, Y: %s, C: %s\n",
150 tv2ms(&afi
->chunk_tv
),
154 header_mode(&mp3
.header
),
155 v
&& *mp3
.id3
.title
? mp3
.id3
.title
: "(title tag not set)",
156 v
&& *mp3
.id3
.artist
? mp3
.id3
.artist
: "(artist tag not set)",
157 v
&& *mp3
.id3
.album
? mp3
.id3
.album
: "(album tag not set)",
158 v
&& *mp3
.id3
.year
? mp3
.id3
.year
: "????",
159 v
&& *mp3
.id3
.comment
? mp3
.id3
.comment
: "(comment tag not set)"
164 * Remove trailing whitespace from the end of a string
166 static char *unpad(char *string
)
168 char *pos
= string
+ strlen(string
) - 1;
169 while (isspace(pos
[0]))
174 static int compare_headers(struct mp3header
*h1
,struct mp3header
*h2
)
176 if ((*(uint
*)h1
) == (*(uint
*)h2
))
178 if ((h1
->version
== h2
->version
) &&
179 (h1
->layer
== h2
->layer
) &&
180 (h1
->crc
== h2
->crc
) &&
181 (h1
->freq
== h2
->freq
) &&
182 (h1
->mode
== h2
->mode
) &&
183 (h1
->copyright
== h2
->copyright
) &&
184 (h1
->original
== h2
->original
) &&
185 (h1
->emphasis
== h2
->emphasis
))
191 * get next MP3 frame header.
193 * \param stream to read the header from
194 * \param header structure that gets filled in by get_header()
196 * \return On success, the header frame length is returned. A return value of
197 * zero means that we did not retrieve a valid frame header, and a negative
198 * return value indicates an error.
200 static int get_header(FILE *file
, struct mp3header
*header
)
202 unsigned char buffer
[FRAME_HEADER_SIZE
];
205 if (!file
|| !header
)
206 return -E_MP3_NO_FILE
;
207 ret
= para_fread(buffer
, FRAME_HEADER_SIZE
, 1, file
);
208 if (ret
< FRAME_HEADER_SIZE
) {
210 return ret
< 0? ret
: 0;
212 header
->layer
= (buffer
[1] >> 1) & 3;
213 header
->sync
= (((int)buffer
[0]<<4) | ((int)(buffer
[1]&0xE0)>>4));
214 if (buffer
[1] & 0x10)
215 header
->version
= (buffer
[1] >> 3) & 1;
218 if ((header
->sync
!= 0xFFE) || (header
->layer
!= 1)) {
220 // PARA_DEBUG_LOG("%s: header not found\n", __func__);
223 header
->crc
= buffer
[1] & 1;
224 header
->bitrate
= (buffer
[2] >> 4) & 0x0F;
225 // PARA_DEBUG_LOG("%s: found header, bitrate: %u\n", __func__,
227 header
->freq
= (buffer
[2] >> 2) & 0x3;
228 header
->padding
= (buffer
[2] >>1) & 0x1;
229 header
->mode
= (buffer
[3] >> 6) & 0x3;
230 fl
= frame_length(header
);
231 return (fl
>= MIN_FRAME_SIZE
)? fl
: -E_FRAME_LENGTH
;
235 * find the next mp3 header
237 * \return On success, the length of the next frame header. If the end of the
238 * file was reached, the function returns zero. On errors, a negative value is
242 static int mp3_seek_next_header(FILE *file
)
244 int k
, l
= 0, c
, first_len
, ret
;
245 struct mp3header h
, h2
;
246 long valid_start
= 0;
249 while ((c
= fgetc(file
)) != 255 && (c
!= EOF
))
254 valid_start
= ftell(file
);
255 first_len
= get_header(file
, &h
);
258 ret
= para_fseek(file
, first_len
- FRAME_HEADER_SIZE
, SEEK_CUR
);
261 for (k
= 1; k
< MIN_CONSEC_GOOD_FRAMES
; k
++) {
262 if ((l
= get_header(file
, &h2
)) <= 0)
264 if (!compare_headers(&h
, &h2
))
266 ret
= para_fseek(file
, l
- FRAME_HEADER_SIZE
, SEEK_CUR
);
270 if (k
== MIN_CONSEC_GOOD_FRAMES
) {
271 ret
= para_fseek(file
, valid_start
, SEEK_SET
);
274 memcpy(&(mp3
.header
), &h2
, sizeof(struct mp3header
));
280 static int mp3_get_id3(FILE *file
)
286 mp3
.id3
.title
[0] = '\0';
287 mp3
.id3
.artist
[0] = '\0';
288 mp3
.id3
.album
[0] = '\0';
289 mp3
.id3
.comment
[0] = '\0';
290 mp3
.id3
.year
[0] = '\0';
291 ret
= para_fseek(file
, -128, SEEK_END
);
294 if (para_fread(fbuf
, 1, 3, file
) < 0)
297 if (strcmp("TAG", fbuf
)) {
298 PARA_INFO_LOG("%s", "no id3 tag\n");
301 ret
= para_fseek(file
, -125, SEEK_END
);
304 if (para_fread(mp3
.id3
.title
, 1, 30, file
) != 30)
306 mp3
.id3
.title
[30] = '\0';
307 if (para_fread(mp3
.id3
.artist
, 1, 30, file
) != 30)
309 mp3
.id3
.artist
[30] = '\0';
310 if (para_fread(mp3
.id3
.album
, 1, 30, file
) != 30)
312 mp3
.id3
.album
[30] = '\0';
313 if (para_fread(mp3
.id3
.year
, 1, 4, file
) != 4)
315 mp3
.id3
.year
[4] = '\0';
316 if (para_fread(mp3
.id3
.comment
, 1, 30, file
) != 30)
318 mp3
.id3
.comment
[30] = '\0';
320 unpad(mp3
.id3
.title
);
321 unpad(mp3
.id3
.artist
);
322 unpad(mp3
.id3
.album
);
324 unpad(mp3
.id3
.comment
);
328 static int find_valid_start(FILE *file
)
332 frame_len
= get_header(file
, &mp3
.header
);
336 frame_len
= mp3_seek_next_header(file
);
340 ret
= para_fseek(file
, -FRAME_HEADER_SIZE
, SEEK_CUR
);
345 return -E_FRAME_LENGTH
;
349 static int mp3_read_info(FILE *file
, struct audio_format_info
*afi
)
351 long fl_avg
= 0, freq_avg
= 0, br_avg
= 0;
352 int ret
, len
= 0, old_br
= -1;
353 struct timeval total_time
= {0, 0};
354 unsigned chunk_table_size
= 1000; /* gets increased on demand */
356 afi
->chunks_total
= 0;
357 afi
->chunk_table
= para_malloc(chunk_table_size
* sizeof(size_t));
358 ret
= mp3_get_id3(file
);
366 struct timeval tmp
, cct
; /* current chunk time */
368 ret
= para_fseek(file
, len
, SEEK_CUR
);
372 len
= find_valid_start(file
);
375 freq
= header_frequency(&mp3
.header
);
376 br
= header_bitrate(&mp3
.header
);
377 fl
= frame_length(&mp3
.header
);
378 if (freq
< 0 || br
< 0 || fl
< 0)
382 tv_divide(br
* 125, &tmp
, &cct
);
383 tv_add(&cct
, &total_time
, &tmp
);
385 //PARA_DEBUG_LOG("%s: br: %d, freq: %d, fl: %d, cct: %lu\n", __func__, br, freq, fl, cct.tv_usec);
386 if (afi
->chunks_total
>= chunk_table_size
) {
387 chunk_table_size
*= 2;
388 afi
->chunk_table
= para_realloc(afi
->chunk_table
,
389 chunk_table_size
* sizeof(size_t));
391 afi
->chunk_table
[afi
->chunks_total
] = ftell(file
);
392 if (afi
->chunks_total
< 10 || !(afi
->chunks_total
% 1000))
393 PARA_INFO_LOG("chunk #%lu: %zd\n", afi
->chunks_total
,
394 afi
->chunk_table
[afi
->chunks_total
]);
396 if (afi
->chunks_total
== 1) {
403 freq_avg
+= (freq
- freq_avg
) / (afi
->chunks_total
+ 1);
404 fl_avg
+= (fl
- fl_avg
) / (afi
->chunks_total
+ 1);
405 br_avg
+= (br
- br_avg
) / (afi
->chunks_total
+ 1);
411 if (!afi
->chunks_total
|| !freq_avg
|| !br_avg
)
413 ret
= para_fseek(file
, 0, SEEK_END
);
416 afi
->chunk_table
[afi
->chunks_total
] = ftell(file
);
417 mp3
.br_average
= br_avg
;
419 afi
->seconds_total
= (tv2ms(&total_time
) + 500) / 1000;
420 tv_divide(afi
->chunks_total
, &total_time
, &afi
->chunk_tv
);
422 PARA_DEBUG_LOG("%lu chunks, each %lums\n", afi
->chunks_total
,
423 tv2ms(&afi
->chunk_tv
));
424 tv_scale(3, &afi
->chunk_tv
, &afi
->eof_tv
);
425 PARA_DEBUG_LOG("eof timeout: %lu\n", tv2ms(&afi
->eof_tv
));
428 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));
429 free(afi
->chunk_table
);
434 * Read mp3 information from audio file
436 static int mp3_get_file_info(FILE *file
, struct audio_format_info
*afi
)
440 ret
= mp3_read_info(file
, afi
);
444 if (afi
->seconds_total
< 2 || !afi
->chunks_total
)
449 static const char* mp3_suffixes
[] = {"mp3", NULL
};
452 * the init function of the mp3 audio format handler
454 * \param afh pointer to the struct to initialize
456 void mp3_init(struct audio_format_handler
*afh
)
458 afh
->get_file_info
= mp3_get_file_info
;
459 afh
->suffixes
= mp3_suffixes
;