0eb1188eda0f578fbe2382a99514eec81737a9a4
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>
23 /** \cond some defines and structs which are only used in this file */
26 * MIN_CONSEC_GOOD_FRAMES defines how many consecutive valid MP3 frames we need
27 * to see before we decide we are looking at a real MP3 file
29 #define MIN_CONSEC_GOOD_FRAMES 4
30 #define FRAME_HEADER_SIZE 4
31 #define MIN_FRAME_SIZE 21
42 unsigned int copyright
;
43 unsigned int original
;
44 unsigned int emphasis
;
56 struct mp3header header
;
63 static const int frequencies
[3][4] = {
64 {22050,24000,16000,50000}, /* MPEG 2.0 */
65 {44100,48000,32000,50000}, /* MPEG 1.0 */
66 {11025,12000,8000,50000} /* MPEG 2.5 */
69 static const int mp3info_bitrate
[2][3][14] = {
71 {32,48,56,64,80,96,112,128,144,160,176,192,224,256}, /* layer 1 */
72 {8,16,24,32,40,48,56,64,80,96,112,128,144,160}, /* layer 2 */
73 {8,16,24,32,40,48,56,64,80,96,112,128,144,160} /* layer 3 */
77 {32,64,96,128,160,192,224,256,288,320,352,384,416,448}, /* layer 1 */
78 {32,48,56,64,80,96,112,128,160,192,224,256,320,384}, /* layer 2 */
79 {32,40,48,56,64,80,96,112,128,160,192,224,256,320} /* layer 3 */
83 static const int frame_size_index
[] = {24000, 72000, 72000};
84 static const char *mode_text
[] = {"stereo", "joint stereo", "dual channel", "mono", "invalid"};
86 static struct mp3info mp3
;
88 static int header_frequency(struct mp3header
*h
)
90 if (h
->version
> 2 || h
->freq
> 3)
91 return -E_HEADER_FREQ
;
92 return frequencies
[h
->version
][h
->freq
];
95 static const char *header_mode(struct mp3header
*h
)
98 h
->mode
= 4; /* invalid */
99 return mode_text
[h
->mode
];
102 static int header_channels(struct mp3header
*h
)
111 static int header_bitrate(struct mp3header
*h
)
113 if (!h
->layer
|| h
->layer
> 3 || h
->bitrate
> 14 || !h
->bitrate
)
114 return -E_HEADER_BITRATE
;
115 return mp3info_bitrate
[h
->version
& 1][3 - h
->layer
][h
->bitrate
- 1];
118 static int frame_length(struct mp3header
*header
)
120 int hb
, hf
= header_frequency(header
);
124 hb
= header_bitrate(header
);
127 if (header
->sync
!= 0xFFE || header
->layer
> 3)
129 return frame_size_index
[3 - header
->layer
] *
130 ((header
->version
& 1) + 1) * hb
/ hf
134 static void write_info_str(struct audio_format_info
*afi
)
136 int v
= mp3
.id3_isvalid
;
138 snprintf(afi
->info_string
, MMD_INFO_SIZE
,
139 "audio_file_info1:%lu x %lums, %u kbit/s (%cbr) %i KHz %s\n"
140 "audio_file_info2:%s, by %s\n"
141 "audio_file_info3:A: %s, Y: %s, C: %s\n",
143 tv2ms(&afi
->chunk_tv
),
146 afi
->frequency
/ 1000,
147 header_mode(&mp3
.header
),
148 v
&& *mp3
.id3
.title
? mp3
.id3
.title
: "(title tag not set)",
149 v
&& *mp3
.id3
.artist
? mp3
.id3
.artist
: "(artist tag not set)",
150 v
&& *mp3
.id3
.album
? mp3
.id3
.album
: "(album tag not set)",
151 v
&& *mp3
.id3
.year
? mp3
.id3
.year
: "????",
152 v
&& *mp3
.id3
.comment
? mp3
.id3
.comment
: "(comment tag not set)"
157 * Remove trailing whitespace from the end of a string
159 static char *unpad(char *string
)
161 char *pos
= string
+ strlen(string
) - 1;
162 while (isspace(pos
[0]))
167 static int compare_headers(struct mp3header
*h1
,struct mp3header
*h2
)
169 if ((*(uint
*)h1
) == (*(uint
*)h2
))
171 if ((h1
->version
== h2
->version
) &&
172 (h1
->layer
== h2
->layer
) &&
173 (h1
->crc
== h2
->crc
) &&
174 (h1
->freq
== h2
->freq
) &&
175 (h1
->mode
== h2
->mode
) &&
176 (h1
->copyright
== h2
->copyright
) &&
177 (h1
->original
== h2
->original
) &&
178 (h1
->emphasis
== h2
->emphasis
))
184 * get next MP3 frame header.
186 * On success, the header frame length is returned and the given header
187 * structure that is filled in. A return value of zero means that we did not
188 * retrieve a valid frame header, and a negative return value indicates an
191 static int get_header(unsigned char *map
, size_t numbytes
, off_t
*fpos
,
192 struct mp3header
*header
)
196 if (*fpos
+ FRAME_HEADER_SIZE
> numbytes
) {
197 *fpos
= numbytes
- 1;
201 header
->layer
= (map
[*fpos
+ 1] >> 1) & 3;
202 header
->sync
= (((int)map
[*fpos
]<<4) | ((int)(map
[*fpos
+ 1]&0xE0)>>4));
203 if (map
[*fpos
+ 1] & 0x10)
204 header
->version
= (map
[*fpos
+ 1] >> 3) & 1;
207 if ((header
->sync
!= 0xFFE) || (header
->layer
!= 1)) {
212 header
->crc
= map
[*fpos
+ 1] & 1;
213 header
->bitrate
= (map
[*fpos
+ 2] >> 4) & 0x0F;
214 header
->freq
= (map
[*fpos
+ 2] >> 2) & 0x3;
215 header
->padding
= (map
[*fpos
+ 2] >>1) & 0x1;
216 header
->mode
= (map
[*fpos
+ 3] >> 6) & 0x3;
217 fl
= frame_length(header
);
218 ret
= (fl
>= MIN_FRAME_SIZE
)? fl
: -E_FRAME_LENGTH
;
220 *fpos
+= FRAME_HEADER_SIZE
;
225 * find the next mp3 header
227 * Return the length of the next frame header or zero if the end of the file is
230 static int mp3_seek_next_header(unsigned char *map
, size_t numbytes
, off_t
*fpos
)
232 int k
, l
= 0, first_len
;
233 struct mp3header h
, h2
;
234 long valid_start
= 0;
236 for (; *fpos
< numbytes
; (*fpos
)++) {
237 if (map
[*fpos
] != 0xff)
240 first_len
= get_header(map
, numbytes
, fpos
, &h
);
243 *fpos
+= first_len
- FRAME_HEADER_SIZE
;
244 for (k
= 1; k
< MIN_CONSEC_GOOD_FRAMES
; k
++) {
245 if ((l
= get_header(map
, numbytes
, fpos
, &h2
)) <= 0)
247 if (!compare_headers(&h
, &h2
))
249 *fpos
+= l
- FRAME_HEADER_SIZE
;
251 if (k
== MIN_CONSEC_GOOD_FRAMES
) {
253 memcpy(&(mp3
.header
), &h2
, sizeof(struct mp3header
));
260 static void mp3_get_id3(unsigned char *map
, size_t numbytes
, off_t
*fpos
)
263 mp3
.id3
.title
[0] = '\0';
264 mp3
.id3
.artist
[0] = '\0';
265 mp3
.id3
.album
[0] = '\0';
266 mp3
.id3
.comment
[0] = '\0';
267 mp3
.id3
.year
[0] = '\0';
270 *fpos
= numbytes
- 128;
271 if (strncmp("TAG", (char *) map
+ *fpos
, 3)) {
272 PARA_INFO_LOG("%s", "no id3 tag\n");
275 *fpos
= numbytes
- 125;
276 memcpy(mp3
.id3
.title
, map
+ *fpos
, 30);
278 mp3
.id3
.title
[30] = '\0';
279 memcpy(mp3
.id3
.artist
, map
+ *fpos
, 30);
281 mp3
.id3
.artist
[30] = '\0';
282 memcpy(mp3
.id3
.album
, map
+ *fpos
, 30);
284 mp3
.id3
.album
[30] = '\0';
285 memcpy(mp3
.id3
.year
, map
+ *fpos
, 4);
287 mp3
.id3
.year
[4] = '\0';
288 memcpy(mp3
.id3
.comment
, map
+ *fpos
, 30);
289 mp3
.id3
.comment
[30] = '\0';
291 unpad(mp3
.id3
.title
);
292 unpad(mp3
.id3
.artist
);
293 unpad(mp3
.id3
.album
);
295 unpad(mp3
.id3
.comment
);
298 static int find_valid_start(unsigned char *map
, size_t numbytes
, off_t
*fpos
)
302 frame_len
= get_header(map
, numbytes
, fpos
, &mp3
.header
);
306 frame_len
= mp3_seek_next_header(map
, numbytes
, fpos
);
310 *fpos
-= FRAME_HEADER_SIZE
;
312 return -E_FRAME_LENGTH
;
316 static int mp3_read_info(unsigned char *map
, size_t numbytes
,
317 struct audio_format_info
*afi
)
319 long fl_avg
= 0, freq_avg
= 0, br_avg
= 0;
320 int ret
, len
= 0, old_br
= -1;
321 struct timeval total_time
= {0, 0};
322 unsigned chunk_table_size
= 1000; /* gets increased on demand */
325 afi
->chunks_total
= 0;
326 afi
->chunk_table
= para_malloc(chunk_table_size
* sizeof(size_t));
327 mp3_get_id3(map
, numbytes
, &fpos
);
331 unsigned long freq
, br
, fl
;
332 struct timeval tmp
, cct
; /* current chunk time */
334 len
= find_valid_start(map
, numbytes
, &fpos
);
337 ret
= header_frequency(&mp3
.header
);
341 ret
= header_bitrate(&mp3
.header
);
345 ret
= frame_length(&mp3
.header
);
351 tv_divide(br
* 125, &tmp
, &cct
);
352 tv_add(&cct
, &total_time
, &tmp
);
354 //PARA_DEBUG_LOG("%s: br: %d, freq: %d, fl: %d, cct: %lu\n", __func__, br, freq, fl, cct.tv_usec);
355 if (afi
->chunks_total
>= chunk_table_size
) {
356 chunk_table_size
*= 2;
357 afi
->chunk_table
= para_realloc(afi
->chunk_table
,
358 chunk_table_size
* sizeof(size_t));
360 afi
->chunk_table
[afi
->chunks_total
] = fpos
;
361 // if (afi->chunks_total < 10 || !(afi->chunks_total % 1000))
362 // PARA_INFO_LOG("chunk #%lu: %zd\n", afi->chunks_total,
363 // afi->chunk_table[afi->chunks_total]);
365 if (afi
->chunks_total
== 1) {
372 freq_avg
+= ((long)freq
- freq_avg
) / ((long)afi
->chunks_total
+ 1);
373 fl_avg
+= ((long)fl
- fl_avg
) / ((long)afi
->chunks_total
+ 1);
374 br_avg
+= ((long)br
- br_avg
) / ((long)afi
->chunks_total
+ 1);
380 if (!afi
->chunks_total
|| !freq_avg
|| !br_avg
)
382 afi
->chunk_table
[afi
->chunks_total
] = numbytes
- 1;
383 afi
->bitrate
= br_avg
;
384 afi
->frequency
= freq_avg
;
385 afi
->channels
= header_channels(&mp3
.header
);
386 afi
->seconds_total
= (tv2ms(&total_time
) + 500) / 1000;
387 tv_divide(afi
->chunks_total
, &total_time
, &afi
->chunk_tv
);
388 PARA_DEBUG_LOG("%lu chunks, each %lums\n", afi
->chunks_total
,
389 tv2ms(&afi
->chunk_tv
));
390 tv_scale(3, &afi
->chunk_tv
, &afi
->eof_tv
);
391 PARA_DEBUG_LOG("eof timeout: %lu\n", tv2ms(&afi
->eof_tv
));
394 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));
395 free(afi
->chunk_table
);
400 * Read mp3 information from audio file
402 static int mp3_get_file_info(char *map
, size_t numbytes
,
403 struct audio_format_info
*afi
)
407 ret
= mp3_read_info((unsigned char *)map
, numbytes
, afi
);
411 if (afi
->seconds_total
< 2 || !afi
->chunks_total
)
416 static const char* mp3_suffixes
[] = {"mp3", NULL
};
419 * the init function of the mp3 audio format handler
421 * \param afh pointer to the struct to initialize
423 void mp3_init(struct audio_format_handler
*afh
)
425 afh
->get_file_info
= mp3_get_file_info
;
426 afh
->suffixes
= mp3_suffixes
;