Merge branch 'master' into android-test-plugins
[kugel-rb.git] / apps / codecs / mpa.c
blobc9e2131450992168d74b9723f84d45564012fe31
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 static struct mad_stream stream IBSS_ATTR;
33 static struct mad_frame frame IBSS_ATTR;
34 static struct mad_synth synth IBSS_ATTR;
36 #ifdef MPA_SYNTH_ON_COP
37 static volatile short die IBSS_ATTR = 0; /*thread should die*/
39 #if (CONFIG_CPU == PP5024) || (CONFIG_CPU == PP5022)
40 static mad_fixed_t sbsample_prev[2][36][32] IBSS_ATTR;
41 #else
42 static mad_fixed_t sbsample_prev[2][36][32] SHAREDBSS_ATTR;
43 #endif
45 static struct semaphore synth_done_sem IBSS_ATTR;
46 static struct semaphore synth_pending_sem IBSS_ATTR;
47 #endif
49 #define INPUT_CHUNK_SIZE 8192
51 static mad_fixed_t mad_frame_overlap[2][32][18] IBSS_ATTR;
52 static mad_fixed_t sbsample[2][36][32] IBSS_ATTR;
54 static unsigned char mad_main_data[MAD_BUFFER_MDLEN] IBSS_ATTR;
55 /* TODO: what latency does layer 1 have? */
56 static int mpeg_latency[3] = { 0, 481, 529 };
57 static int mpeg_framesize[3] = {384, 1152, 1152};
59 static 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 #ifdef MPA_SYNTH_ON_COP
66 frame.sbsample_prev = &sbsample_prev;
67 frame.sbsample = &sbsample;
68 #else
69 frame.sbsample_prev = &sbsample;
70 frame.sbsample = &sbsample;
71 #endif
73 /* We do this so libmad doesn't try to call codec_calloc(). This needs to
74 * be called before mad_stream_init(), mad_frame_inti() and
75 * mad_synth_init(). */
76 frame.overlap = &mad_frame_overlap;
77 stream.main_data = &mad_main_data;
79 /* Call mad initialization. Those will zero the arrays frame.overlap,
80 * frame.sbsample and frame.sbsample_prev. Therefore there is no need to
81 * zero them here. */
82 mad_stream_init(&stream);
83 mad_frame_init(&frame);
84 mad_synth_init(&synth);
87 static int get_file_pos(int newtime)
89 int pos = -1;
90 struct mp3entry *id3 = ci->id3;
92 if (id3->vbr) {
93 /* Convert newtime and id3->length to seconds to
94 * avoid overflow */
95 unsigned int newtime_s = newtime/1000;
96 unsigned int length_s = id3->length/1000;
98 if (id3->has_toc) {
99 /* Use the TOC to find the new position */
100 unsigned int percent, remainder;
101 int curtoc, nexttoc, plen;
103 percent = (newtime_s*100) / length_s;
104 if (percent > 99)
105 percent = 99;
107 curtoc = id3->toc[percent];
109 if (percent < 99) {
110 nexttoc = id3->toc[percent+1];
111 } else {
112 nexttoc = 256;
115 pos = (id3->filesize/256)*curtoc;
117 /* Use the remainder to get a more accurate position */
118 remainder = (newtime_s*100) % length_s;
119 remainder = (remainder*100) / length_s;
120 plen = (nexttoc - curtoc)*(id3->filesize/256);
121 pos += (plen/100)*remainder;
122 } else {
123 /* No TOC exists, estimate the new position */
124 pos = (id3->filesize / length_s) * newtime_s;
126 } else if (id3->bitrate) {
127 pos = newtime * (id3->bitrate / 8);
128 } else {
129 return -1;
132 /* Don't seek right to the end of the file so that we can
133 transition properly to the next song */
134 if (pos >= (int)(id3->filesize - id3->id3v1len))
135 pos = id3->filesize - id3->id3v1len - 1;
137 /* id3->filesize excludes id3->first_frame_offset, so add it now */
138 pos += id3->first_frame_offset;
140 return pos;
143 static void set_elapsed(struct mp3entry* id3)
145 unsigned long offset = id3->offset > id3->first_frame_offset ?
146 id3->offset - id3->first_frame_offset : 0;
148 if ( id3->vbr ) {
149 if ( id3->has_toc ) {
150 /* calculate elapsed time using TOC */
151 int i;
152 unsigned int remainder, plen, relpos, nextpos;
154 /* find wich percent we're at */
155 for (i=0; i<100; i++ )
156 if ( offset < id3->toc[i] * (id3->filesize / 256) )
157 break;
159 i--;
160 if (i < 0)
161 i = 0;
163 relpos = id3->toc[i];
165 if (i < 99)
166 nextpos = id3->toc[i+1];
167 else
168 nextpos = 256;
170 remainder = offset - (relpos * (id3->filesize / 256));
172 /* set time for this percent (divide before multiply to prevent
173 overflow on long files. loss of precision is negligible on
174 short files) */
175 id3->elapsed = i * (id3->length / 100);
177 /* calculate remainder time */
178 plen = (nextpos - relpos) * (id3->filesize / 256);
179 id3->elapsed += (((remainder * 100) / plen) *
180 (id3->length / 10000));
182 else {
183 /* no TOC exists. set a rough estimate using average bitrate */
184 int tpk = id3->length /
185 ((id3->filesize - id3->first_frame_offset - id3->id3v1len) /
186 1024);
187 id3->elapsed = offset / 1024 * tpk;
190 else
192 /* constant bitrate, use exact calculation */
193 if (id3->bitrate != 0)
194 id3->elapsed = offset / (id3->bitrate / 8);
198 #ifdef MPA_SYNTH_ON_COP
201 * Run the synthesis filter on the COProcessor
204 static int mad_synth_thread_stack[DEFAULT_STACK_SIZE/sizeof(int)] IBSS_ATTR;
206 static const unsigned char * const mad_synth_thread_name = "mp3dec";
207 static unsigned int mad_synth_thread_id = 0;
210 static void mad_synth_thread(void)
212 while(1) {
213 ci->semaphore_release(&synth_done_sem);
214 ci->semaphore_wait(&synth_pending_sem, TIMEOUT_BLOCK);
216 if(die)
217 break;
219 mad_synth_frame(&synth, &frame);
223 /* wait for the synth thread to go idle which indicates a PCM frame has been
224 * synthesized */
225 static inline void mad_synth_thread_wait_pcm(void)
227 ci->semaphore_wait(&synth_done_sem, TIMEOUT_BLOCK);
230 /* increment the done semaphore - used after a wait for idle to preserve the
231 * semaphore count */
232 static inline void mad_synth_thread_unwait_pcm(void)
234 ci->semaphore_release(&synth_done_sem);
237 /* after synth thread has gone idle - switch decoded frames and commence
238 * synthesis on it */
239 static void mad_synth_thread_ready(void)
241 mad_fixed_t (*temp)[2][36][32];
243 /*circular buffer that holds 2 frames' samples*/
244 temp=frame.sbsample;
245 frame.sbsample = frame.sbsample_prev;
246 frame.sbsample_prev=temp;
248 ci->semaphore_release(&synth_pending_sem);
251 static bool mad_synth_thread_create(void)
253 ci->semaphore_init(&synth_done_sem, 1, 0);
254 ci->semaphore_init(&synth_pending_sem, 1, 0);
256 mad_synth_thread_id = ci->create_thread(mad_synth_thread,
257 mad_synth_thread_stack,
258 sizeof(mad_synth_thread_stack), 0,
259 mad_synth_thread_name
260 IF_PRIO(, PRIORITY_PLAYBACK)
261 IF_COP(, COP));
263 if (mad_synth_thread_id == 0)
264 return false;
266 return true;
269 static void mad_synth_thread_quit(void)
271 /* mop up COP thread */
272 die = 1;
273 ci->semaphore_release(&synth_pending_sem);
274 ci->thread_wait(mad_synth_thread_id);
275 ci->cpucache_invalidate();
277 #else
278 static inline void mad_synth_thread_ready(void)
280 mad_synth_frame(&synth, &frame);
283 static inline bool mad_synth_thread_create(void)
285 return true;
288 static inline void mad_synth_thread_quit(void)
292 static inline void mad_synth_thread_wait_pcm(void)
296 static inline void mad_synth_thread_unwait_pcm(void)
299 #endif /* MPA_SYNTH_ON_COP */
301 /* this is the codec entry point */
302 enum codec_status codec_main(enum codec_entry_call_reason reason)
304 if (reason == CODEC_LOAD) {
305 /* Create a decoder instance */
306 if (codec_init())
307 return CODEC_ERROR;
309 ci->configure(DSP_SET_SAMPLE_DEPTH, MAD_F_FRACBITS);
311 /* does nothing on 1 processor systems except return true */
312 if(!mad_synth_thread_create())
313 return CODEC_ERROR;
315 else if (reason == CODEC_UNLOAD) {
316 /* mop up COP thread - MT only */
317 mad_synth_thread_quit();
320 return CODEC_OK;
323 /* this is called for each file to process */
324 enum codec_status codec_run(void)
326 size_t size;
327 int file_end;
328 int samples_to_skip; /* samples to skip in total for this file (at start) */
329 char *inputbuffer;
330 int64_t samplesdone;
331 int stop_skip, start_skip;
332 int current_stereo_mode = -1;
333 unsigned long current_frequency = 0;
334 int framelength;
335 int padding = MAD_BUFFER_GUARD; /* to help mad decode the last frame */
336 intptr_t param;
338 /* Reinitializing seems to be necessary to avoid playback quircks when seeking. */
339 init_mad();
341 file_end = 0;
343 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
344 current_frequency = ci->id3->frequency;
345 codec_set_replaygain(ci->id3);
347 if (ci->id3->offset) {
348 ci->seek_buffer(ci->id3->offset);
349 set_elapsed(ci->id3);
351 else
352 ci->seek_buffer(ci->id3->first_frame_offset);
354 if (ci->id3->lead_trim >= 0 && ci->id3->tail_trim >= 0) {
355 stop_skip = ci->id3->tail_trim - mpeg_latency[ci->id3->layer];
356 if (stop_skip < 0) stop_skip = 0;
357 start_skip = ci->id3->lead_trim + mpeg_latency[ci->id3->layer];
358 } else {
359 stop_skip = 0;
360 /* We want to skip this amount anyway */
361 start_skip = mpeg_latency[ci->id3->layer];
364 /* Libmad will not decode the last frame without 8 bytes of extra padding
365 in the buffer. So, we can trick libmad into not decoding the last frame
366 if we are to skip it entirely and then cut the appropriate samples from
367 final frame that we did decode. Note, if all tags (ID3, APE) are not
368 properly stripped from the end of the file, this trick will not work. */
369 if (stop_skip >= mpeg_framesize[ci->id3->layer]) {
370 padding = 0;
371 stop_skip -= mpeg_framesize[ci->id3->layer];
372 } else {
373 padding = MAD_BUFFER_GUARD;
376 samplesdone = ((int64_t)ci->id3->elapsed) * current_frequency / 1000;
378 /* Don't skip any samples unless we start at the beginning. */
379 if (samplesdone > 0)
380 samples_to_skip = 0;
381 else
382 samples_to_skip = start_skip;
384 framelength = 0;
386 /* This is the decoding loop. */
387 while (1) {
388 enum codec_command_action action = ci->get_command(&param);
390 if (action == CODEC_ACTION_HALT)
391 break;
393 if (action == CODEC_ACTION_SEEK_TIME) {
394 int newpos;
396 /*make sure the synth thread is idle before seeking - MT only*/
397 mad_synth_thread_wait_pcm();
398 mad_synth_thread_unwait_pcm();
400 samplesdone = ((int64_t)param)*current_frequency/1000;
402 if (param == 0) {
403 newpos = ci->id3->first_frame_offset;
404 samples_to_skip = start_skip;
405 } else {
406 newpos = get_file_pos(param);
407 samples_to_skip = 0;
410 if (!ci->seek_buffer(newpos))
412 ci->seek_complete();
413 break;
416 ci->set_elapsed((samplesdone * 1000) / current_frequency);
417 ci->seek_complete();
418 init_mad();
419 framelength = 0;
422 /* Lock buffers */
423 if (stream.error == 0) {
424 inputbuffer = ci->request_buffer(&size, INPUT_CHUNK_SIZE);
425 if (size == 0 || inputbuffer == NULL)
426 break;
427 mad_stream_buffer(&stream, (unsigned char *)inputbuffer,
428 size + padding);
431 if (mad_frame_decode(&frame, &stream)) {
432 if (stream.error == MAD_ERROR_BUFLEN) {
433 /* This makes the codec support partially corrupted files */
434 if (file_end == 30)
435 break;
437 /* Fill the buffer */
438 if (stream.next_frame)
439 ci->advance_buffer(stream.next_frame - stream.buffer);
440 else
441 ci->advance_buffer(size);
442 stream.error = 0; /* Must get new inputbuffer next time */
443 file_end++;
444 continue;
445 } else if (MAD_RECOVERABLE(stream.error)) {
446 /* Probably syncing after a seek */
447 continue;
448 } else {
449 /* Some other unrecoverable error */
450 return CODEC_ERROR;
454 /* Do the pcmbuf insert here. Note, this is the PREVIOUS frame's pcm
455 data (not the one just decoded above). When we exit the decoding
456 loop we will need to process the final frame that was decoded. */
457 mad_synth_thread_wait_pcm();
459 if (framelength > 0) {
461 /* In case of a mono file, the second array will be ignored. */
462 ci->pcmbuf_insert(&synth.pcm.samples[0][samples_to_skip],
463 &synth.pcm.samples[1][samples_to_skip],
464 framelength);
466 /* Only skip samples for the first frame added. */
467 samples_to_skip = 0;
470 /* Initiate PCM synthesis on the COP (MT) or perform it here (ST) */
471 mad_synth_thread_ready();
473 /* Check if sample rate and stereo settings changed in this frame. */
474 if (frame.header.samplerate != current_frequency) {
475 current_frequency = frame.header.samplerate;
476 ci->configure(DSP_SWITCH_FREQUENCY, current_frequency);
478 if (MAD_NCHANNELS(&frame.header) == 2) {
479 if (current_stereo_mode != STEREO_NONINTERLEAVED) {
480 ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
481 current_stereo_mode = STEREO_NONINTERLEAVED;
483 } else {
484 if (current_stereo_mode != STEREO_MONO) {
485 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
486 current_stereo_mode = STEREO_MONO;
490 if (stream.next_frame)
491 ci->advance_buffer(stream.next_frame - stream.buffer);
492 else
493 ci->advance_buffer(size);
494 stream.error = 0; /* Must get new inputbuffer next time */
495 file_end = 0;
497 framelength = synth.pcm.length - samples_to_skip;
498 if (framelength < 0) {
499 framelength = 0;
500 samples_to_skip -= synth.pcm.length;
503 samplesdone += framelength;
504 ci->set_elapsed((samplesdone * 1000) / current_frequency);
507 /* wait for synth idle - MT only*/
508 mad_synth_thread_wait_pcm();
509 mad_synth_thread_unwait_pcm();
511 /* Finish the remaining decoded frame.
512 Cut the required samples from the end. */
513 if (framelength > stop_skip){
514 ci->pcmbuf_insert(synth.pcm.samples[0], synth.pcm.samples[1],
515 framelength - stop_skip);
518 return CODEC_OK;