New makefile solution: A single invocation of 'make' to build the entire tree. Fully...
[kugel-rb.git] / apps / plugins / mpegplayer / audio_thread.c
blob45226575c9914e29d9f98786a06e2acb5461b970
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * mpegplayer audio thread implementation
12 * Copyright (c) 2007 Michael Sevakis
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
23 #include "plugin.h"
24 #include "mpegplayer.h"
25 #include "codecs/libmad/bit.h"
26 #include "codecs/libmad/mad.h"
28 /** Audio stream and thread **/
29 struct pts_queue_slot;
30 struct audio_thread_data
32 struct queue_event ev; /* Our event queue to receive commands */
33 int state; /* Thread state */
34 int status; /* Media status (STREAM_PLAYING, etc.) */
35 int mad_errors; /* A count of the errors in each frame */
36 unsigned samplerate; /* Current stream sample rate */
37 int nchannels; /* Number of audio channels */
38 struct dsp_config *dsp; /* The DSP we're using */
41 /* The audio stack is stolen from the core codec thread (but not in uisim) */
42 /* Used for stealing codec thread's stack */
43 static uint32_t* audio_stack;
44 static size_t audio_stack_size; /* Keep gcc happy and init */
45 #define AUDIO_STACKSIZE (9*1024)
46 #ifndef SIMULATOR
47 static uint32_t codec_stack_copy[AUDIO_STACKSIZE / sizeof(uint32_t)];
48 #endif
49 static struct event_queue audio_str_queue SHAREDBSS_ATTR;
50 static struct queue_sender_list audio_str_queue_send SHAREDBSS_ATTR;
51 struct stream audio_str IBSS_ATTR;
53 /* libmad related definitions */
54 static struct mad_stream stream IBSS_ATTR;
55 static struct mad_frame frame IBSS_ATTR;
56 static struct mad_synth synth IBSS_ATTR;
58 /*sbsample buffer for mad_frame*/
59 mad_fixed_t sbsample[2][36][32];
61 /* 2567 bytes */
62 static unsigned char mad_main_data[MAD_BUFFER_MDLEN];
64 /* There isn't enough room for this in IRAM on PortalPlayer, but there
65 is for Coldfire. */
67 /* 4608 bytes */
68 #ifdef CPU_COLDFIRE
69 static mad_fixed_t mad_frame_overlap[2][32][18] IBSS_ATTR;
70 #else
71 static mad_fixed_t mad_frame_overlap[2][32][18];
72 #endif
74 /** A queue for saving needed information about MPEG audio packets **/
75 #define AUDIODESC_QUEUE_LEN (1 << 5) /* 32 should be way more than sufficient -
76 if not, the case is handled */
77 #define AUDIODESC_QUEUE_MASK (AUDIODESC_QUEUE_LEN-1)
78 struct audio_frame_desc
80 uint32_t time; /* Time stamp for packet in audio ticks */
81 ssize_t size; /* Number of unprocessed bytes left in packet */
84 /* This starts out wr == rd but will never be emptied to zero during
85 streaming again in order to support initializing the first packet's
86 timestamp without a special case */
87 struct
89 /* Compressed audio data */
90 uint8_t *start; /* Start of encoded audio buffer */
91 uint8_t *ptr; /* Pointer to next encoded audio data */
92 ssize_t used; /* Number of bytes in MPEG audio buffer */
93 /* Compressed audio data descriptors */
94 unsigned read, write;
95 struct audio_frame_desc *curr; /* Current slot */
96 struct audio_frame_desc descs[AUDIODESC_QUEUE_LEN];
97 } audio_queue;
99 static inline int audiodesc_queue_count(void)
101 return audio_queue.write - audio_queue.read;
104 static inline bool audiodesc_queue_full(void)
106 return audio_queue.used >= MPA_MAX_FRAME_SIZE + MAD_BUFFER_GUARD ||
107 audiodesc_queue_count() >= AUDIODESC_QUEUE_LEN;
110 /* Increments the queue tail postion - should be used to preincrement */
111 static inline void audiodesc_queue_add_tail(void)
113 if (audiodesc_queue_full())
115 DEBUGF("audiodesc_queue_add_tail: audiodesc queue full!\n");
116 return;
119 audio_queue.write++;
122 /* Increments the queue tail position - leaves one slot as current */
123 static inline bool audiodesc_queue_remove_head(void)
125 if (audio_queue.write == audio_queue.read)
126 return false;
128 audio_queue.read++;
129 return true;
132 /* Returns the "tail" at the index just behind the write index */
133 static inline struct audio_frame_desc * audiodesc_queue_tail(void)
135 return &audio_queue.descs[(audio_queue.write - 1) & AUDIODESC_QUEUE_MASK];
138 /* Returns a pointer to the current head */
139 static inline struct audio_frame_desc * audiodesc_queue_head(void)
141 return &audio_queue.descs[audio_queue.read & AUDIODESC_QUEUE_MASK];
144 /* Resets the pts queue - call when starting and seeking */
145 static void audio_queue_reset(void)
147 audio_queue.ptr = audio_queue.start;
148 audio_queue.used = 0;
149 audio_queue.read = 0;
150 audio_queue.write = 0;
151 rb->memset(audio_queue.descs, 0, sizeof (audio_queue.descs));
152 audio_queue.curr = audiodesc_queue_head();
155 static void audio_queue_advance_pos(ssize_t len)
157 audio_queue.ptr += len;
158 audio_queue.used -= len;
159 audio_queue.curr->size -= len;
162 static int audio_buffer(struct stream *str, enum stream_parse_mode type)
164 int ret = STREAM_OK;
166 /* Carry any overshoot to the next size since we're technically
167 -size bytes into it already. If size is negative an audio
168 frame was split across packets. Old has to be saved before
169 moving the head. */
170 if (audio_queue.curr->size <= 0 && audiodesc_queue_remove_head())
172 struct audio_frame_desc *old = audio_queue.curr;
173 audio_queue.curr = audiodesc_queue_head();
174 audio_queue.curr->size += old->size;
175 old->size = 0;
178 /* Add packets to compressed audio buffer until it's full or the
179 * timestamp queue is full - whichever happens first */
180 while (!audiodesc_queue_full())
182 ret = parser_get_next_data(str, type);
183 struct audio_frame_desc *curr;
184 ssize_t len;
186 if (ret != STREAM_OK)
187 break;
189 /* Get data from next audio packet */
190 len = str->curr_packet_end - str->curr_packet;
192 if (str->pkt_flags & PKT_HAS_TS)
194 audiodesc_queue_add_tail();
195 curr = audiodesc_queue_tail();
196 curr->time = TS_TO_TICKS(str->pts);
197 /* pts->size should have been zeroed when slot was
198 freed */
200 else
202 /* Add to the one just behind the tail - this may be
203 * the head or the previouly added tail - whether or
204 * not we'll ever reach this is quite in question
205 * since audio always seems to have every packet
206 * timestamped */
207 curr = audiodesc_queue_tail();
210 curr->size += len;
212 /* Slide any remainder over to beginning */
213 if (audio_queue.ptr > audio_queue.start && audio_queue.used > 0)
215 rb->memmove(audio_queue.start, audio_queue.ptr,
216 audio_queue.used);
219 /* Splice this packet onto any remainder */
220 rb->memcpy(audio_queue.start + audio_queue.used,
221 str->curr_packet, len);
223 audio_queue.used += len;
224 audio_queue.ptr = audio_queue.start;
226 rb->yield();
229 return ret;
232 /* Initialise libmad */
233 static void init_mad(void)
235 /*init the sbsample buffer*/
236 frame.sbsample = &sbsample;
237 frame.sbsample_prev = &sbsample;
239 mad_stream_init(&stream);
240 mad_frame_init(&frame);
241 mad_synth_init(&synth);
243 /* We do this so libmad doesn't try to call codec_calloc() */
244 rb->memset(mad_frame_overlap, 0, sizeof(mad_frame_overlap));
245 frame.overlap = (void *)mad_frame_overlap;
247 rb->memset(mad_main_data, 0, sizeof(mad_main_data));
248 stream.main_data = &mad_main_data;
251 /* Sync audio stream to a particular frame - see main decoder loop for
252 * detailed remarks */
253 static int audio_sync(struct audio_thread_data *td,
254 struct str_sync_data *sd)
256 int retval = STREAM_MATCH;
257 uint32_t sdtime = TS_TO_TICKS(clip_time(&audio_str, sd->time));
258 uint32_t time;
259 uint32_t duration = 0;
260 struct stream *str;
261 struct stream tmp_str;
262 struct mad_header header;
263 struct mad_stream stream;
265 if (td->ev.id == STREAM_SYNC)
267 /* Actually syncing for playback - use real stream */
268 time = 0;
269 str = &audio_str;
271 else
273 /* Probing - use temp stream */
274 time = INVALID_TIMESTAMP;
275 str = &tmp_str;
276 str->id = audio_str.id;
279 str->hdr.pos = sd->sk.pos;
280 str->hdr.limit = sd->sk.pos + sd->sk.len;
282 mad_stream_init(&stream);
283 mad_header_init(&header);
285 while (1)
287 if (audio_buffer(str, STREAM_PM_RANDOM_ACCESS) == STREAM_DATA_END)
289 DEBUGF("audio_sync:STR_DATA_END\n aqu:%ld swl:%ld swr:%ld\n",
290 audio_queue.used, str->hdr.win_left, str->hdr.win_right);
291 if (audio_queue.used <= MAD_BUFFER_GUARD)
292 goto sync_data_end;
295 stream.error = 0;
296 mad_stream_buffer(&stream, audio_queue.ptr, audio_queue.used);
298 if (stream.sync && mad_stream_sync(&stream) < 0)
300 DEBUGF(" audio: mad_stream_sync failed\n");
301 audio_queue_advance_pos(MAX(audio_queue.curr->size - 1, 1));
302 continue;
305 stream.sync = 0;
307 if (mad_header_decode(&header, &stream) < 0)
309 DEBUGF(" audio: mad_header_decode failed:%s\n",
310 mad_stream_errorstr(&stream));
311 audio_queue_advance_pos(1);
312 continue;
315 duration = 32*MAD_NSBSAMPLES(&header);
316 time = audio_queue.curr->time;
318 DEBUGF(" audio: ft:%u t:%u fe:%u nsamp:%u sampr:%u\n",
319 (unsigned)TICKS_TO_TS(time), (unsigned)sd->time,
320 (unsigned)TICKS_TO_TS(time + duration),
321 (unsigned)duration, header.samplerate);
323 audio_queue_advance_pos(stream.this_frame - audio_queue.ptr);
325 if (time <= sdtime && sdtime < time + duration)
327 DEBUGF(" audio: ft<=t<fe\n");
328 retval = STREAM_PERFECT_MATCH;
329 break;
331 else if (time > sdtime)
333 DEBUGF(" audio: ft>t\n");
334 break;
337 audio_queue_advance_pos(stream.next_frame - audio_queue.ptr);
338 audio_queue.curr->time += duration;
340 rb->yield();
343 sync_data_end:
344 if (td->ev.id == STREAM_FIND_END_TIME)
346 if (time != INVALID_TIMESTAMP)
348 time = TICKS_TO_TS(time);
349 duration = TICKS_TO_TS(duration);
350 sd->time = time + duration;
351 retval = STREAM_PERFECT_MATCH;
353 else
355 retval = STREAM_NOT_FOUND;
359 DEBUGF(" audio header: 0x%02X%02X%02X%02X\n",
360 (unsigned)audio_queue.ptr[0], (unsigned)audio_queue.ptr[1],
361 (unsigned)audio_queue.ptr[2], (unsigned)audio_queue.ptr[3]);
363 return retval;
364 (void)td;
367 static void audio_thread_msg(struct audio_thread_data *td)
369 while (1)
371 intptr_t reply = 0;
373 switch (td->ev.id)
375 case STREAM_PLAY:
376 td->status = STREAM_PLAYING;
378 switch (td->state)
380 case TSTATE_INIT:
381 td->state = TSTATE_DECODE;
382 case TSTATE_DECODE:
383 case TSTATE_RENDER_WAIT:
384 case TSTATE_RENDER_WAIT_END:
385 break;
387 case TSTATE_EOS:
388 /* At end of stream - no playback possible so fire the
389 * completion event */
390 stream_generate_event(&audio_str, STREAM_EV_COMPLETE, 0);
391 break;
394 break;
396 case STREAM_PAUSE:
397 td->status = STREAM_PAUSED;
398 reply = td->state != TSTATE_EOS;
399 break;
401 case STREAM_STOP:
402 if (td->state == TSTATE_DATA)
403 stream_clear_notify(&audio_str, DISK_BUF_DATA_NOTIFY);
405 td->status = STREAM_STOPPED;
406 td->state = TSTATE_EOS;
408 reply = true;
409 break;
411 case STREAM_RESET:
412 if (td->state == TSTATE_DATA)
413 stream_clear_notify(&audio_str, DISK_BUF_DATA_NOTIFY);
415 td->status = STREAM_STOPPED;
416 td->state = TSTATE_INIT;
417 td->samplerate = 0;
418 td->nchannels = 0;
420 init_mad();
421 td->mad_errors = 0;
423 audio_queue_reset();
425 reply = true;
426 break;
428 case STREAM_NEEDS_SYNC:
429 reply = true; /* Audio always needs to */
430 break;
432 case STREAM_SYNC:
433 case STREAM_FIND_END_TIME:
434 if (td->state != TSTATE_INIT)
435 break;
437 reply = audio_sync(td, (struct str_sync_data *)td->ev.data);
438 break;
440 case DISK_BUF_DATA_NOTIFY:
441 /* Our bun is done */
442 if (td->state != TSTATE_DATA)
443 break;
445 td->state = TSTATE_DECODE;
446 str_data_notify_received(&audio_str);
447 break;
449 case STREAM_QUIT:
450 /* Time to go - make thread exit */
451 td->state = TSTATE_EOS;
452 return;
455 str_reply_msg(&audio_str, reply);
457 if (td->status == STREAM_PLAYING)
459 switch (td->state)
461 case TSTATE_DECODE:
462 case TSTATE_RENDER_WAIT:
463 case TSTATE_RENDER_WAIT_END:
464 /* These return when in playing state */
465 return;
469 str_get_msg(&audio_str, &td->ev);
473 static void audio_thread(void)
475 struct audio_thread_data td;
477 rb->memset(&td, 0, sizeof (td));
478 td.status = STREAM_STOPPED;
479 td.state = TSTATE_EOS;
481 /* We need this here to init the EMAC for Coldfire targets */
482 init_mad();
484 td.dsp = (struct dsp_config *)rb->dsp_configure(NULL, DSP_MYDSP,
485 CODEC_IDX_AUDIO);
486 rb->sound_set_pitch(1000);
487 rb->dsp_configure(td.dsp, DSP_RESET, 0);
488 rb->dsp_configure(td.dsp, DSP_SET_SAMPLE_DEPTH, MAD_F_FRACBITS);
490 goto message_wait;
492 /* This is the decoding loop. */
493 while (1)
495 td.state = TSTATE_DECODE;
497 /* Check for any pending messages and process them */
498 if (str_have_msg(&audio_str))
500 message_wait:
501 /* Wait for a message to be queued */
502 str_get_msg(&audio_str, &td.ev);
504 message_process:
505 /* Process a message already dequeued */
506 audio_thread_msg(&td);
508 switch (td.state)
510 /* These states are the only ones that should return */
511 case TSTATE_DECODE: goto audio_decode;
512 case TSTATE_RENDER_WAIT: goto render_wait;
513 case TSTATE_RENDER_WAIT_END: goto render_wait_end;
514 /* Anything else is interpreted as an exit */
515 default: return;
519 audio_decode:
521 /** Buffering **/
522 switch (audio_buffer(&audio_str, STREAM_PM_STREAMING))
524 case STREAM_DATA_NOT_READY:
526 td.state = TSTATE_DATA;
527 goto message_wait;
528 } /* STREAM_DATA_NOT_READY: */
530 case STREAM_DATA_END:
532 if (audio_queue.used > MAD_BUFFER_GUARD)
533 break;
535 /* Used up remainder of compressed audio buffer.
536 * Force any residue to play if audio ended before
537 * reaching the threshold */
538 td.state = TSTATE_RENDER_WAIT_END;
539 audio_queue_reset();
541 render_wait_end:
542 pcm_output_drain();
544 while (pcm_output_used() > (ssize_t)PCMOUT_LOW_WM)
546 str_get_msg_w_tmo(&audio_str, &td.ev, 1);
547 if (td.ev.id != SYS_TIMEOUT)
548 goto message_process;
551 td.state = TSTATE_EOS;
552 if (td.status == STREAM_PLAYING)
553 stream_generate_event(&audio_str, STREAM_EV_COMPLETE, 0);
555 rb->yield();
556 goto message_wait;
557 } /* STREAM_DATA_END: */
560 /** Decoding **/
561 mad_stream_buffer(&stream, audio_queue.ptr, audio_queue.used);
563 int mad_stat = mad_frame_decode(&frame, &stream);
565 ssize_t len = stream.next_frame - audio_queue.ptr;
567 if (mad_stat != 0)
569 DEBUGF("audio: Stream error: %s\n",
570 mad_stream_errorstr(&stream));
572 /* If something's goofed - try to perform resync by moving
573 * at least one byte at a time */
574 audio_queue_advance_pos(MAX(len, 1));
576 if (stream.error == MAD_FLAG_INCOMPLETE
577 || stream.error == MAD_ERROR_BUFLEN)
579 /* This makes the codec support partially corrupted files */
580 if (++td.mad_errors <= MPA_MAX_FRAME_SIZE)
582 stream.error = 0;
583 rb->yield();
584 continue;
586 DEBUGF("audio: Too many errors\n");
588 else if (MAD_RECOVERABLE(stream.error))
590 /* libmad says it can recover - just keep on decoding */
591 rb->yield();
592 continue;
594 else
596 /* Some other unrecoverable error */
597 DEBUGF("audio: Unrecoverable error\n");
600 /* This is too hard - bail out */
601 td.state = TSTATE_EOS;
603 if (td.status == STREAM_PLAYING)
604 stream_generate_event(&audio_str, STREAM_EV_COMPLETE, 0);
606 td.status = STREAM_ERROR;
607 goto message_wait;
610 /* Adjust sizes by the frame size */
611 audio_queue_advance_pos(len);
612 td.mad_errors = 0; /* Clear errors */
614 /* Generate the pcm samples */
615 mad_synth_frame(&synth, &frame);
617 /** Output **/
618 if (frame.header.samplerate != td.samplerate)
620 td.samplerate = frame.header.samplerate;
621 rb->dsp_configure(td.dsp, DSP_SWITCH_FREQUENCY,
622 td.samplerate);
625 if (MAD_NCHANNELS(&frame.header) != td.nchannels)
627 td.nchannels = MAD_NCHANNELS(&frame.header);
628 rb->dsp_configure(td.dsp, DSP_SET_STEREO_MODE,
629 td.nchannels == 1 ?
630 STEREO_MONO : STEREO_NONINTERLEAVED);
633 td.state = TSTATE_RENDER_WAIT;
635 /* Add a frame of audio to the pcm buffer. Maximum is 1152 samples. */
636 render_wait:
637 if (synth.pcm.length > 0)
639 struct pcm_frame_header *dst_hdr = pcm_output_get_buffer();
640 const char *src[2] =
641 { (char *)synth.pcm.samples[0], (char *)synth.pcm.samples[1] };
642 int out_count = (synth.pcm.length * CLOCK_RATE
643 + (td.samplerate - 1)) / td.samplerate;
644 ssize_t size = sizeof(*dst_hdr) + out_count*4;
646 /* Wait for required amount of free buffer space */
647 while (pcm_output_free() < size)
649 /* Wait one frame */
650 int timeout = out_count*HZ / td.samplerate;
651 str_get_msg_w_tmo(&audio_str, &td.ev, MAX(timeout, 1));
652 if (td.ev.id != SYS_TIMEOUT)
653 goto message_process;
656 out_count = rb->dsp_process(td.dsp, dst_hdr->data, src,
657 synth.pcm.length);
659 if (out_count <= 0)
660 break;
662 dst_hdr->size = sizeof(*dst_hdr) + out_count*4;
663 dst_hdr->time = audio_queue.curr->time;
665 /* As long as we're on this timestamp, the time is just
666 incremented by the number of samples */
667 audio_queue.curr->time += out_count;
669 /* Make this data available to DMA */
670 pcm_output_add_data();
673 rb->yield();
674 } /* end decoding loop */
677 /* Initializes the audio thread resources and starts the thread */
678 bool audio_thread_init(void)
680 int i;
681 #ifdef SIMULATOR
682 /* The simulator thread implementation doesn't have stack buffers, and
683 these parameters are ignored. */
684 (void)i; /* Keep gcc happy */
685 audio_stack = NULL;
686 audio_stack_size = 0;
687 #else
688 /* Borrow the codec thread's stack (in IRAM on most targets) */
689 audio_stack = NULL;
690 for (i = 0; i < MAXTHREADS; i++)
692 if (rb->strcmp(rb->threads[i].name, "codec") == 0)
694 /* Wait to ensure the codec thread has blocked */
695 while (rb->threads[i].state != STATE_BLOCKED)
696 rb->yield();
698 /* Now we can steal the stack */
699 audio_stack = rb->threads[i].stack;
700 audio_stack_size = rb->threads[i].stack_size;
702 /* Backup the codec thread's stack */
703 rb->memcpy(codec_stack_copy, audio_stack, audio_stack_size);
704 break;
708 if (audio_stack == NULL)
710 /* This shouldn't happen, but deal with it anyway by using
711 the copy instead */
712 audio_stack = codec_stack_copy;
713 audio_stack_size = AUDIO_STACKSIZE;
715 #endif
717 /* Initialise the encoded audio buffer and its descriptors */
718 audio_queue.start = mpeg_malloc(AUDIOBUF_ALLOC_SIZE,
719 MPEG_ALLOC_AUDIOBUF);
720 if (audio_queue.start == NULL)
721 return false;
723 /* Start the audio thread */
724 audio_str.hdr.q = &audio_str_queue;
725 rb->queue_init(audio_str.hdr.q, false);
727 /* One-up on the priority since the core DSP over-yields internally */
728 audio_str.thread = rb->create_thread(
729 audio_thread, audio_stack, audio_stack_size, 0,
730 "mpgaudio" IF_PRIO(,PRIORITY_PLAYBACK-4) IF_COP(, CPU));
732 rb->queue_enable_queue_send(audio_str.hdr.q, &audio_str_queue_send,
733 audio_str.thread);
735 if (audio_str.thread == NULL)
736 return false;
738 /* Wait for thread to initialize */
739 str_send_msg(&audio_str, STREAM_NULL, 0);
741 return true;
744 /* Stops the audio thread */
745 void audio_thread_exit(void)
747 if (audio_str.thread != NULL)
749 str_post_msg(&audio_str, STREAM_QUIT, 0);
750 rb->thread_wait(audio_str.thread);
751 audio_str.thread = NULL;
754 #ifndef SIMULATOR
755 /* Restore the codec thread's stack */
756 rb->memcpy(audio_stack, codec_stack_copy, audio_stack_size);
757 #endif