Just correct a typo in a comment.
[kugel-rb.git] / apps / codecs / mpa.c
blob7732622383b4a884de1eb293bd6c662585d94cdf
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Dave Chapman
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "codeclib.h"
23 #include <codecs/libmad/mad.h>
24 #include <inttypes.h>
26 CODEC_HEADER
28 #if NUM_CORES > 1 && !defined(MPEGPLAYER)
29 #define MPA_SYNTH_ON_COP
30 #endif
32 struct mad_stream stream IBSS_ATTR;
33 struct mad_frame frame IBSS_ATTR;
34 struct mad_synth synth IBSS_ATTR;
36 #ifdef MPA_SYNTH_ON_COP
37 volatile short die IBSS_ATTR = 0; /*thread should die*/
39 #if (CONFIG_CPU == PP5024) || (CONFIG_CPU == PP5022)
40 mad_fixed_t sbsample_prev[2][36][32] IBSS_ATTR;
41 #else
42 mad_fixed_t sbsample_prev[2][36][32] SHAREDBSS_ATTR;
43 #endif
45 struct semaphore synth_done_sem IBSS_ATTR;
46 struct semaphore synth_pending_sem IBSS_ATTR;
47 #endif
49 #define INPUT_CHUNK_SIZE 8192
51 mad_fixed_t mad_frame_overlap[2][32][18] IBSS_ATTR;
52 mad_fixed_t sbsample[2][36][32] IBSS_ATTR;
54 unsigned char mad_main_data[MAD_BUFFER_MDLEN] IBSS_ATTR;
55 /* TODO: what latency does layer 1 have? */
56 int mpeg_latency[3] = { 0, 481, 529 };
57 int mpeg_framesize[3] = {384, 1152, 1152};
59 void init_mad(void)
61 ci->memset(&stream, 0, sizeof(struct mad_stream));
62 ci->memset(&frame, 0, sizeof(struct mad_frame));
63 ci->memset(&synth, 0, sizeof(struct mad_synth));
65 ci->memset(&sbsample, 0, sizeof(sbsample));
67 #ifdef MPA_SYNTH_ON_COP
68 frame.sbsample_prev = &sbsample_prev;
69 ci->memset(&sbsample_prev, 0, sizeof(sbsample_prev));
70 #else
71 frame.sbsample_prev = &sbsample;
72 #endif
74 frame.sbsample=&sbsample;
76 mad_stream_init(&stream);
77 mad_frame_init(&frame);
78 mad_synth_init(&synth);
80 /* We do this so libmad doesn't try to call codec_calloc() */
81 ci->memset(mad_frame_overlap, 0, sizeof(mad_frame_overlap));
82 frame.overlap = &mad_frame_overlap;
83 stream.main_data = &mad_main_data;
88 int get_file_pos(int newtime)
90 int pos = -1;
91 struct mp3entry *id3 = ci->id3;
93 if (id3->vbr) {
94 if (id3->has_toc) {
95 /* Use the TOC to find the new position */
96 unsigned int percent, remainder;
97 int curtoc, nexttoc, plen;
99 percent = (newtime*100) / id3->length;
100 if (percent > 99)
101 percent = 99;
103 curtoc = id3->toc[percent];
105 if (percent < 99) {
106 nexttoc = id3->toc[percent+1];
107 } else {
108 nexttoc = 256;
111 pos = (id3->filesize/256)*curtoc;
113 /* Use the remainder to get a more accurate position */
114 remainder = (newtime*100) % id3->length;
115 remainder = (remainder*100) / id3->length;
116 plen = (nexttoc - curtoc)*(id3->filesize/256);
117 pos += (plen/100)*remainder;
118 } else {
119 /* No TOC exists, estimate the new position */
120 pos = (id3->filesize / (id3->length / 1000)) *
121 (newtime / 1000);
123 } else if (id3->bitrate) {
124 pos = newtime * (id3->bitrate / 8);
125 } else {
126 return -1;
129 pos += id3->first_frame_offset;
131 /* Don't seek right to the end of the file so that we can
132 transition properly to the next song */
133 if (pos >= (int)(id3->filesize - id3->id3v1len))
134 pos = id3->filesize - id3->id3v1len - 1;
136 return pos;
139 static void set_elapsed(struct mp3entry* id3)
141 unsigned long offset = id3->offset > id3->first_frame_offset ?
142 id3->offset - id3->first_frame_offset : 0;
144 if ( id3->vbr ) {
145 if ( id3->has_toc ) {
146 /* calculate elapsed time using TOC */
147 int i;
148 unsigned int remainder, plen, relpos, nextpos;
150 /* find wich percent we're at */
151 for (i=0; i<100; i++ )
152 if ( offset < id3->toc[i] * (id3->filesize / 256) )
153 break;
155 i--;
156 if (i < 0)
157 i = 0;
159 relpos = id3->toc[i];
161 if (i < 99)
162 nextpos = id3->toc[i+1];
163 else
164 nextpos = 256;
166 remainder = offset - (relpos * (id3->filesize / 256));
168 /* set time for this percent (divide before multiply to prevent
169 overflow on long files. loss of precision is negligible on
170 short files) */
171 id3->elapsed = i * (id3->length / 100);
173 /* calculate remainder time */
174 plen = (nextpos - relpos) * (id3->filesize / 256);
175 id3->elapsed += (((remainder * 100) / plen) *
176 (id3->length / 10000));
178 else {
179 /* no TOC exists. set a rough estimate using average bitrate */
180 int tpk = id3->length /
181 ((id3->filesize - id3->first_frame_offset - id3->id3v1len) /
182 1024);
183 id3->elapsed = offset / 1024 * tpk;
186 else
188 /* constant bitrate, use exact calculation */
189 if (id3->bitrate != 0)
190 id3->elapsed = offset / (id3->bitrate / 8);
194 #ifdef MPA_SYNTH_ON_COP
197 * Run the synthesis filter on the COProcessor
200 static int mad_synth_thread_stack[DEFAULT_STACK_SIZE/sizeof(int)/2] IBSS_ATTR;
202 static const unsigned char * const mad_synth_thread_name = "mp3dec";
203 static struct thread_entry *mad_synth_thread_p;
206 static void mad_synth_thread(void)
208 while(1) {
209 ci->semaphore_release(&synth_done_sem);
210 ci->semaphore_wait(&synth_pending_sem);
212 if(die)
213 break;
215 mad_synth_frame(&synth, &frame);
219 /* wait for the synth thread to go idle which indicates a PCM frame has been
220 * synthesized */
221 static inline void mad_synth_thread_wait_pcm(void)
223 ci->semaphore_wait(&synth_done_sem);
226 /* increment the done semaphore - used after a wait for idle to preserve the
227 * semaphore count */
228 static inline void mad_synth_thread_unwait_pcm(void)
230 ci->semaphore_release(&synth_done_sem);
233 /* after synth thread has gone idle - switch decoded frames and commence
234 * synthesis on it */
235 static void mad_synth_thread_ready(void)
237 mad_fixed_t (*temp)[2][36][32];
239 /*circular buffer that holds 2 frames' samples*/
240 temp=frame.sbsample;
241 frame.sbsample = frame.sbsample_prev;
242 frame.sbsample_prev=temp;
244 ci->semaphore_release(&synth_pending_sem);
247 static bool mad_synth_thread_create(void)
249 ci->semaphore_init(&synth_done_sem, 1, 0);
250 ci->semaphore_init(&synth_pending_sem, 1, 0);
252 mad_synth_thread_p = ci->create_thread(mad_synth_thread,
253 mad_synth_thread_stack,
254 sizeof(mad_synth_thread_stack), 0,
255 mad_synth_thread_name
256 IF_PRIO(, PRIORITY_PLAYBACK)
257 IF_COP(, COP));
259 if (mad_synth_thread_p == NULL)
260 return false;
262 return true;
265 static void mad_synth_thread_quit(void)
267 /*mop up COP thread*/
268 die=1;
269 ci->semaphore_release(&synth_pending_sem);
270 ci->thread_wait(mad_synth_thread_p);
271 invalidate_icache();
273 #else
274 static inline void mad_synth_thread_ready(void)
276 mad_synth_frame(&synth, &frame);
279 static inline bool mad_synth_thread_create(void)
281 return true;
284 static inline void mad_synth_thread_quit(void)
288 static inline void mad_synth_thread_wait_pcm(void)
292 static inline void mad_synth_thread_unwait_pcm(void)
295 #endif /* MPA_SYNTH_ON_COP */
297 /* this is the codec entry point */
298 enum codec_status codec_main(void)
300 int status;
301 size_t size;
302 int file_end;
303 int samples_to_skip; /* samples to skip in total for this file (at start) */
304 char *inputbuffer;
305 int64_t samplesdone;
306 int stop_skip, start_skip;
307 int current_stereo_mode = -1;
308 unsigned long current_frequency = 0;
309 int framelength;
310 int padding = MAD_BUFFER_GUARD; /* to help mad decode the last frame */
312 if (codec_init())
313 return CODEC_ERROR;
315 /* Create a decoder instance */
317 ci->configure(DSP_SET_SAMPLE_DEPTH, MAD_F_FRACBITS);
319 /*does nothing on 1 processor systems except return true*/
320 if(!mad_synth_thread_create())
321 return CODEC_ERROR;
323 next_track:
325 status = CODEC_OK;
327 /* Reinitializing seems to be necessary to avoid playback quircks when seeking. */
328 init_mad();
330 file_end = 0;
331 while (!*ci->taginfo_ready && !ci->stop_codec)
332 ci->sleep(1);
334 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
335 current_frequency = ci->id3->frequency;
336 codec_set_replaygain(ci->id3);
338 if (ci->id3->offset) {
339 ci->seek_buffer(ci->id3->offset);
340 set_elapsed(ci->id3);
342 else
343 ci->seek_buffer(ci->id3->first_frame_offset);
345 if (ci->id3->lead_trim >= 0 && ci->id3->tail_trim >= 0) {
346 stop_skip = ci->id3->tail_trim - mpeg_latency[ci->id3->layer];
347 if (stop_skip < 0) stop_skip = 0;
348 start_skip = ci->id3->lead_trim + mpeg_latency[ci->id3->layer];
349 } else {
350 stop_skip = 0;
351 /* We want to skip this amount anyway */
352 start_skip = mpeg_latency[ci->id3->layer];
355 /* Libmad will not decode the last frame without 8 bytes of extra padding
356 in the buffer. So, we can trick libmad into not decoding the last frame
357 if we are to skip it entirely and then cut the appropriate samples from
358 final frame that we did decode. Note, if all tags (ID3, APE) are not
359 properly stripped from the end of the file, this trick will not work. */
360 if (stop_skip >= mpeg_framesize[ci->id3->layer]) {
361 padding = 0;
362 stop_skip -= mpeg_framesize[ci->id3->layer];
363 } else {
364 padding = MAD_BUFFER_GUARD;
367 samplesdone = ((int64_t)ci->id3->elapsed) * current_frequency / 1000;
369 /* Don't skip any samples unless we start at the beginning. */
370 if (samplesdone > 0)
371 samples_to_skip = 0;
372 else
373 samples_to_skip = start_skip;
375 framelength = 0;
377 /* This is the decoding loop. */
378 while (1) {
379 ci->yield();
380 if (ci->stop_codec || ci->new_track)
381 break;
383 if (ci->seek_time) {
384 int newpos;
386 /*make sure the synth thread is idle before seeking - MT only*/
387 mad_synth_thread_wait_pcm();
388 mad_synth_thread_unwait_pcm();
390 samplesdone = ((int64_t)(ci->seek_time-1))*current_frequency/1000;
392 if (ci->seek_time-1 == 0) {
393 newpos = ci->id3->first_frame_offset;
394 samples_to_skip = start_skip;
395 } else {
396 newpos = get_file_pos(ci->seek_time-1);
397 samples_to_skip = 0;
400 if (!ci->seek_buffer(newpos))
401 break;
402 ci->seek_complete();
403 init_mad();
404 framelength = 0;
407 /* Lock buffers */
408 if (stream.error == 0) {
409 inputbuffer = ci->request_buffer(&size, INPUT_CHUNK_SIZE);
410 if (size == 0 || inputbuffer == NULL)
411 break;
412 mad_stream_buffer(&stream, (unsigned char *)inputbuffer,
413 size + padding);
416 if (mad_frame_decode(&frame, &stream)) {
417 if (stream.error == MAD_FLAG_INCOMPLETE
418 || stream.error == MAD_ERROR_BUFLEN) {
419 /* This makes the codec support partially corrupted files */
420 if (file_end == 30)
421 break;
423 /* Fill the buffer */
424 if (stream.next_frame)
425 ci->advance_buffer_loc((void *)stream.next_frame);
426 else
427 ci->advance_buffer(size);
428 stream.error = 0;
429 file_end++;
430 continue;
431 } else if (MAD_RECOVERABLE(stream.error)) {
432 continue;
433 } else {
434 /* Some other unrecoverable error */
435 status = CODEC_ERROR;
436 break;
438 break;
441 file_end = 0;
443 /* Do the pcmbuf insert here. Note, this is the PREVIOUS frame's pcm
444 data (not the one just decoded above). When we exit the decoding
445 loop we will need to process the final frame that was decoded. */
446 mad_synth_thread_wait_pcm();
448 if (framelength > 0) {
450 /* In case of a mono file, the second array will be ignored. */
451 ci->pcmbuf_insert(&synth.pcm.samples[0][samples_to_skip],
452 &synth.pcm.samples[1][samples_to_skip],
453 framelength);
455 /* Only skip samples for the first frame added. */
456 samples_to_skip = 0;
459 /* Initiate PCM synthesis on the COP (MT) or perform it here (ST) */
460 mad_synth_thread_ready();
462 /* Check if sample rate and stereo settings changed in this frame. */
463 if (frame.header.samplerate != current_frequency) {
464 current_frequency = frame.header.samplerate;
465 ci->configure(DSP_SWITCH_FREQUENCY, current_frequency);
467 if (MAD_NCHANNELS(&frame.header) == 2) {
468 if (current_stereo_mode != STEREO_NONINTERLEAVED) {
469 ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
470 current_stereo_mode = STEREO_NONINTERLEAVED;
472 } else {
473 if (current_stereo_mode != STEREO_MONO) {
474 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
475 current_stereo_mode = STEREO_MONO;
479 if (stream.next_frame)
480 ci->advance_buffer_loc((void *)stream.next_frame);
481 else
482 ci->advance_buffer(size);
484 framelength = synth.pcm.length - samples_to_skip;
485 if (framelength < 0) {
486 framelength = 0;
487 samples_to_skip -= synth.pcm.length;
490 samplesdone += framelength;
491 ci->set_elapsed(samplesdone / (current_frequency / 1000));
494 /* wait for synth idle - MT only*/
495 mad_synth_thread_wait_pcm();
496 mad_synth_thread_unwait_pcm();
498 /* Finish the remaining decoded frame.
499 Cut the required samples from the end. */
500 if (framelength > stop_skip){
501 ci->pcmbuf_insert(synth.pcm.samples[0], synth.pcm.samples[1],
502 framelength - stop_skip);
505 if (ci->request_next_track())
506 goto next_track;
508 /*mop up COP thread - MT only*/
509 mad_synth_thread_quit();
511 return status;