Updated our source code header to explicitly mention that we are GPL v2 or
[Rockbox.git] / apps / plugins / mpegplayer / audio_thread.c
blob838dcad6997e9820acb114238441c945256b26a4
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 /* 2567 bytes */
59 static unsigned char mad_main_data[MAD_BUFFER_MDLEN];
61 /* There isn't enough room for this in IRAM on PortalPlayer, but there
62 is for Coldfire. */
64 /* 4608 bytes */
65 #ifdef CPU_COLDFIRE
66 static mad_fixed_t mad_frame_overlap[2][32][18] IBSS_ATTR;
67 #else
68 static mad_fixed_t mad_frame_overlap[2][32][18];
69 #endif
71 /** A queue for saving needed information about MPEG audio packets **/
72 #define AUDIODESC_QUEUE_LEN (1 << 5) /* 32 should be way more than sufficient -
73 if not, the case is handled */
74 #define AUDIODESC_QUEUE_MASK (AUDIODESC_QUEUE_LEN-1)
75 struct audio_frame_desc
77 uint32_t time; /* Time stamp for packet in audio ticks */
78 ssize_t size; /* Number of unprocessed bytes left in packet */
81 /* This starts out wr == rd but will never be emptied to zero during
82 streaming again in order to support initializing the first packet's
83 timestamp without a special case */
84 struct
86 /* Compressed audio data */
87 uint8_t *start; /* Start of encoded audio buffer */
88 uint8_t *ptr; /* Pointer to next encoded audio data */
89 ssize_t used; /* Number of bytes in MPEG audio buffer */
90 /* Compressed audio data descriptors */
91 unsigned read, write;
92 struct audio_frame_desc *curr; /* Current slot */
93 struct audio_frame_desc descs[AUDIODESC_QUEUE_LEN];
94 } audio_queue;
96 static inline int audiodesc_queue_count(void)
98 return audio_queue.write - audio_queue.read;
101 static inline bool audiodesc_queue_full(void)
103 return audio_queue.used >= MPA_MAX_FRAME_SIZE + MAD_BUFFER_GUARD ||
104 audiodesc_queue_count() >= AUDIODESC_QUEUE_LEN;
107 /* Increments the queue tail postion - should be used to preincrement */
108 static inline void audiodesc_queue_add_tail(void)
110 if (audiodesc_queue_full())
112 DEBUGF("audiodesc_queue_add_tail: audiodesc queue full!\n");
113 return;
116 audio_queue.write++;
119 /* Increments the queue tail position - leaves one slot as current */
120 static inline bool audiodesc_queue_remove_head(void)
122 if (audio_queue.write == audio_queue.read)
123 return false;
125 audio_queue.read++;
126 return true;
129 /* Returns the "tail" at the index just behind the write index */
130 static inline struct audio_frame_desc * audiodesc_queue_tail(void)
132 return &audio_queue.descs[(audio_queue.write - 1) & AUDIODESC_QUEUE_MASK];
135 /* Returns a pointer to the current head */
136 static inline struct audio_frame_desc * audiodesc_queue_head(void)
138 return &audio_queue.descs[audio_queue.read & AUDIODESC_QUEUE_MASK];
141 /* Resets the pts queue - call when starting and seeking */
142 static void audio_queue_reset(void)
144 audio_queue.ptr = audio_queue.start;
145 audio_queue.used = 0;
146 audio_queue.read = 0;
147 audio_queue.write = 0;
148 rb->memset(audio_queue.descs, 0, sizeof (audio_queue.descs));
149 audio_queue.curr = audiodesc_queue_head();
152 static void audio_queue_advance_pos(ssize_t len)
154 audio_queue.ptr += len;
155 audio_queue.used -= len;
156 audio_queue.curr->size -= len;
159 static int audio_buffer(struct stream *str, enum stream_parse_mode type)
161 int ret = STREAM_OK;
163 /* Carry any overshoot to the next size since we're technically
164 -size bytes into it already. If size is negative an audio
165 frame was split across packets. Old has to be saved before
166 moving the head. */
167 if (audio_queue.curr->size <= 0 && audiodesc_queue_remove_head())
169 struct audio_frame_desc *old = audio_queue.curr;
170 audio_queue.curr = audiodesc_queue_head();
171 audio_queue.curr->size += old->size;
172 old->size = 0;
175 /* Add packets to compressed audio buffer until it's full or the
176 * timestamp queue is full - whichever happens first */
177 while (!audiodesc_queue_full())
179 ret = parser_get_next_data(str, type);
180 struct audio_frame_desc *curr;
181 ssize_t len;
183 if (ret != STREAM_OK)
184 break;
186 /* Get data from next audio packet */
187 len = str->curr_packet_end - str->curr_packet;
189 if (str->pkt_flags & PKT_HAS_TS)
191 audiodesc_queue_add_tail();
192 curr = audiodesc_queue_tail();
193 curr->time = TS_TO_TICKS(str->pts);
194 /* pts->size should have been zeroed when slot was
195 freed */
197 else
199 /* Add to the one just behind the tail - this may be
200 * the head or the previouly added tail - whether or
201 * not we'll ever reach this is quite in question
202 * since audio always seems to have every packet
203 * timestamped */
204 curr = audiodesc_queue_tail();
207 curr->size += len;
209 /* Slide any remainder over to beginning */
210 if (audio_queue.ptr > audio_queue.start && audio_queue.used > 0)
212 rb->memmove(audio_queue.start, audio_queue.ptr,
213 audio_queue.used);
216 /* Splice this packet onto any remainder */
217 rb->memcpy(audio_queue.start + audio_queue.used,
218 str->curr_packet, len);
220 audio_queue.used += len;
221 audio_queue.ptr = audio_queue.start;
223 rb->yield();
226 return ret;
229 /* Initialise libmad */
230 static void init_mad(void)
232 mad_stream_init(&stream);
233 mad_frame_init(&frame);
234 mad_synth_init(&synth);
236 /* We do this so libmad doesn't try to call codec_calloc() */
237 rb->memset(mad_frame_overlap, 0, sizeof(mad_frame_overlap));
238 frame.overlap = (void *)mad_frame_overlap;
240 rb->memset(mad_main_data, 0, sizeof(mad_main_data));
241 stream.main_data = &mad_main_data;
244 /* Sync audio stream to a particular frame - see main decoder loop for
245 * detailed remarks */
246 static int audio_sync(struct audio_thread_data *td,
247 struct str_sync_data *sd)
249 int retval = STREAM_MATCH;
250 uint32_t sdtime = TS_TO_TICKS(clip_time(&audio_str, sd->time));
251 uint32_t time;
252 uint32_t duration = 0;
253 struct stream *str;
254 struct stream tmp_str;
255 struct mad_header header;
256 struct mad_stream stream;
258 if (td->ev.id == STREAM_SYNC)
260 /* Actually syncing for playback - use real stream */
261 time = 0;
262 str = &audio_str;
264 else
266 /* Probing - use temp stream */
267 time = INVALID_TIMESTAMP;
268 str = &tmp_str;
269 str->id = audio_str.id;
272 str->hdr.pos = sd->sk.pos;
273 str->hdr.limit = sd->sk.pos + sd->sk.len;
275 mad_stream_init(&stream);
276 mad_header_init(&header);
278 while (1)
280 if (audio_buffer(str, STREAM_PM_RANDOM_ACCESS) == STREAM_DATA_END)
282 DEBUGF("audio_sync:STR_DATA_END\n aqu:%ld swl:%ld swr:%ld\n",
283 audio_queue.used, str->hdr.win_left, str->hdr.win_right);
284 if (audio_queue.used <= MAD_BUFFER_GUARD)
285 goto sync_data_end;
288 stream.error = 0;
289 mad_stream_buffer(&stream, audio_queue.ptr, audio_queue.used);
291 if (stream.sync && mad_stream_sync(&stream) < 0)
293 DEBUGF(" audio: mad_stream_sync failed\n");
294 audio_queue_advance_pos(MAX(audio_queue.curr->size - 1, 1));
295 continue;
298 stream.sync = 0;
300 if (mad_header_decode(&header, &stream) < 0)
302 DEBUGF(" audio: mad_header_decode failed:%s\n",
303 mad_stream_errorstr(&stream));
304 audio_queue_advance_pos(1);
305 continue;
308 duration = 32*MAD_NSBSAMPLES(&header);
309 time = audio_queue.curr->time;
311 DEBUGF(" audio: ft:%u t:%u fe:%u nsamp:%u sampr:%u\n",
312 (unsigned)TICKS_TO_TS(time), (unsigned)sd->time,
313 (unsigned)TICKS_TO_TS(time + duration),
314 (unsigned)duration, header.samplerate);
316 audio_queue_advance_pos(stream.this_frame - audio_queue.ptr);
318 if (time <= sdtime && sdtime < time + duration)
320 DEBUGF(" audio: ft<=t<fe\n");
321 retval = STREAM_PERFECT_MATCH;
322 break;
324 else if (time > sdtime)
326 DEBUGF(" audio: ft>t\n");
327 break;
330 audio_queue_advance_pos(stream.next_frame - audio_queue.ptr);
331 audio_queue.curr->time += duration;
333 rb->yield();
336 sync_data_end:
337 if (td->ev.id == STREAM_FIND_END_TIME)
339 if (time != INVALID_TIMESTAMP)
341 time = TICKS_TO_TS(time);
342 duration = TICKS_TO_TS(duration);
343 sd->time = time + duration;
344 retval = STREAM_PERFECT_MATCH;
346 else
348 retval = STREAM_NOT_FOUND;
352 DEBUGF(" audio header: 0x%02X%02X%02X%02X\n",
353 (unsigned)audio_queue.ptr[0], (unsigned)audio_queue.ptr[1],
354 (unsigned)audio_queue.ptr[2], (unsigned)audio_queue.ptr[3]);
356 return retval;
357 (void)td;
360 static void audio_thread_msg(struct audio_thread_data *td)
362 while (1)
364 intptr_t reply = 0;
366 switch (td->ev.id)
368 case STREAM_PLAY:
369 td->status = STREAM_PLAYING;
371 switch (td->state)
373 case TSTATE_INIT:
374 td->state = TSTATE_DECODE;
375 case TSTATE_DECODE:
376 case TSTATE_RENDER_WAIT:
377 case TSTATE_RENDER_WAIT_END:
378 break;
380 case TSTATE_EOS:
381 /* At end of stream - no playback possible so fire the
382 * completion event */
383 stream_generate_event(&audio_str, STREAM_EV_COMPLETE, 0);
384 break;
387 break;
389 case STREAM_PAUSE:
390 td->status = STREAM_PAUSED;
391 reply = td->state != TSTATE_EOS;
392 break;
394 case STREAM_STOP:
395 if (td->state == TSTATE_DATA)
396 stream_clear_notify(&audio_str, DISK_BUF_DATA_NOTIFY);
398 td->status = STREAM_STOPPED;
399 td->state = TSTATE_EOS;
401 reply = true;
402 break;
404 case STREAM_RESET:
405 if (td->state == TSTATE_DATA)
406 stream_clear_notify(&audio_str, DISK_BUF_DATA_NOTIFY);
408 td->status = STREAM_STOPPED;
409 td->state = TSTATE_INIT;
410 td->samplerate = 0;
411 td->nchannels = 0;
413 init_mad();
414 td->mad_errors = 0;
416 audio_queue_reset();
418 reply = true;
419 break;
421 case STREAM_NEEDS_SYNC:
422 reply = true; /* Audio always needs to */
423 break;
425 case STREAM_SYNC:
426 case STREAM_FIND_END_TIME:
427 if (td->state != TSTATE_INIT)
428 break;
430 reply = audio_sync(td, (struct str_sync_data *)td->ev.data);
431 break;
433 case DISK_BUF_DATA_NOTIFY:
434 /* Our bun is done */
435 if (td->state != TSTATE_DATA)
436 break;
438 td->state = TSTATE_DECODE;
439 str_data_notify_received(&audio_str);
440 break;
442 case STREAM_QUIT:
443 /* Time to go - make thread exit */
444 td->state = TSTATE_EOS;
445 return;
448 str_reply_msg(&audio_str, reply);
450 if (td->status == STREAM_PLAYING)
452 switch (td->state)
454 case TSTATE_DECODE:
455 case TSTATE_RENDER_WAIT:
456 case TSTATE_RENDER_WAIT_END:
457 /* These return when in playing state */
458 return;
462 str_get_msg(&audio_str, &td->ev);
466 static void audio_thread(void)
468 struct audio_thread_data td;
470 rb->memset(&td, 0, sizeof (td));
471 td.status = STREAM_STOPPED;
472 td.state = TSTATE_EOS;
474 /* We need this here to init the EMAC for Coldfire targets */
475 init_mad();
477 td.dsp = (struct dsp_config *)rb->dsp_configure(NULL, DSP_MYDSP,
478 CODEC_IDX_AUDIO);
479 rb->sound_set_pitch(1000);
480 rb->dsp_configure(td.dsp, DSP_RESET, 0);
481 rb->dsp_configure(td.dsp, DSP_SET_SAMPLE_DEPTH, MAD_F_FRACBITS);
483 goto message_wait;
485 /* This is the decoding loop. */
486 while (1)
488 td.state = TSTATE_DECODE;
490 /* Check for any pending messages and process them */
491 if (str_have_msg(&audio_str))
493 message_wait:
494 /* Wait for a message to be queued */
495 str_get_msg(&audio_str, &td.ev);
497 message_process:
498 /* Process a message already dequeued */
499 audio_thread_msg(&td);
501 switch (td.state)
503 /* These states are the only ones that should return */
504 case TSTATE_DECODE: goto audio_decode;
505 case TSTATE_RENDER_WAIT: goto render_wait;
506 case TSTATE_RENDER_WAIT_END: goto render_wait_end;
507 /* Anything else is interpreted as an exit */
508 default: return;
512 audio_decode:
514 /** Buffering **/
515 switch (audio_buffer(&audio_str, STREAM_PM_STREAMING))
517 case STREAM_DATA_NOT_READY:
519 td.state = TSTATE_DATA;
520 goto message_wait;
521 } /* STREAM_DATA_NOT_READY: */
523 case STREAM_DATA_END:
525 if (audio_queue.used > MAD_BUFFER_GUARD)
526 break;
528 /* Used up remainder of compressed audio buffer.
529 * Force any residue to play if audio ended before
530 * reaching the threshold */
531 td.state = TSTATE_RENDER_WAIT_END;
532 audio_queue_reset();
534 render_wait_end:
535 pcm_output_drain();
537 while (pcm_output_used() > (ssize_t)PCMOUT_LOW_WM)
539 str_get_msg_w_tmo(&audio_str, &td.ev, 1);
540 if (td.ev.id != SYS_TIMEOUT)
541 goto message_process;
544 td.state = TSTATE_EOS;
545 if (td.status == STREAM_PLAYING)
546 stream_generate_event(&audio_str, STREAM_EV_COMPLETE, 0);
548 rb->yield();
549 goto message_wait;
550 } /* STREAM_DATA_END: */
553 /** Decoding **/
554 mad_stream_buffer(&stream, audio_queue.ptr, audio_queue.used);
556 int mad_stat = mad_frame_decode(&frame, &stream);
558 ssize_t len = stream.next_frame - audio_queue.ptr;
560 if (mad_stat != 0)
562 DEBUGF("audio: Stream error: %s\n",
563 mad_stream_errorstr(&stream));
565 /* If something's goofed - try to perform resync by moving
566 * at least one byte at a time */
567 audio_queue_advance_pos(MAX(len, 1));
569 if (stream.error == MAD_FLAG_INCOMPLETE
570 || stream.error == MAD_ERROR_BUFLEN)
572 /* This makes the codec support partially corrupted files */
573 if (++td.mad_errors <= MPA_MAX_FRAME_SIZE)
575 stream.error = 0;
576 rb->yield();
577 continue;
579 DEBUGF("audio: Too many errors\n");
581 else if (MAD_RECOVERABLE(stream.error))
583 /* libmad says it can recover - just keep on decoding */
584 rb->yield();
585 continue;
587 else
589 /* Some other unrecoverable error */
590 DEBUGF("audio: Unrecoverable error\n");
593 /* This is too hard - bail out */
594 td.state = TSTATE_EOS;
596 if (td.status == STREAM_PLAYING)
597 stream_generate_event(&audio_str, STREAM_EV_COMPLETE, 0);
599 td.status = STREAM_ERROR;
600 goto message_wait;
603 /* Adjust sizes by the frame size */
604 audio_queue_advance_pos(len);
605 td.mad_errors = 0; /* Clear errors */
607 /* Generate the pcm samples */
608 mad_synth_frame(&synth, &frame);
610 /** Output **/
611 if (frame.header.samplerate != td.samplerate)
613 td.samplerate = frame.header.samplerate;
614 rb->dsp_configure(td.dsp, DSP_SWITCH_FREQUENCY,
615 td.samplerate);
618 if (MAD_NCHANNELS(&frame.header) != td.nchannels)
620 td.nchannels = MAD_NCHANNELS(&frame.header);
621 rb->dsp_configure(td.dsp, DSP_SET_STEREO_MODE,
622 td.nchannels == 1 ?
623 STEREO_MONO : STEREO_NONINTERLEAVED);
626 td.state = TSTATE_RENDER_WAIT;
628 /* Add a frame of audio to the pcm buffer. Maximum is 1152 samples. */
629 render_wait:
630 if (synth.pcm.length > 0)
632 struct pcm_frame_header *dst_hdr = pcm_output_get_buffer();
633 const char *src[2] =
634 { (char *)synth.pcm.samples[0], (char *)synth.pcm.samples[1] };
635 int out_count = (synth.pcm.length * CLOCK_RATE
636 + (td.samplerate - 1)) / td.samplerate;
637 ssize_t size = sizeof(*dst_hdr) + out_count*4;
639 /* Wait for required amount of free buffer space */
640 while (pcm_output_free() < size)
642 /* Wait one frame */
643 int timeout = out_count*HZ / td.samplerate;
644 str_get_msg_w_tmo(&audio_str, &td.ev, MAX(timeout, 1));
645 if (td.ev.id != SYS_TIMEOUT)
646 goto message_process;
649 out_count = rb->dsp_process(td.dsp, dst_hdr->data, src,
650 synth.pcm.length);
652 if (out_count <= 0)
653 break;
655 dst_hdr->size = sizeof(*dst_hdr) + out_count*4;
656 dst_hdr->time = audio_queue.curr->time;
658 /* As long as we're on this timestamp, the time is just
659 incremented by the number of samples */
660 audio_queue.curr->time += out_count;
662 /* Make this data available to DMA */
663 pcm_output_add_data();
666 rb->yield();
667 } /* end decoding loop */
670 /* Initializes the audio thread resources and starts the thread */
671 bool audio_thread_init(void)
673 int i;
674 #ifdef SIMULATOR
675 /* The simulator thread implementation doesn't have stack buffers, and
676 these parameters are ignored. */
677 (void)i; /* Keep gcc happy */
678 audio_stack = NULL;
679 audio_stack_size = 0;
680 #else
681 /* Borrow the codec thread's stack (in IRAM on most targets) */
682 audio_stack = NULL;
683 for (i = 0; i < MAXTHREADS; i++)
685 if (rb->strcmp(rb->threads[i].name, "codec") == 0)
687 /* Wait to ensure the codec thread has blocked */
688 while (rb->threads[i].state != STATE_BLOCKED)
689 rb->yield();
691 /* Now we can steal the stack */
692 audio_stack = rb->threads[i].stack;
693 audio_stack_size = rb->threads[i].stack_size;
695 /* Backup the codec thread's stack */
696 rb->memcpy(codec_stack_copy, audio_stack, audio_stack_size);
697 break;
701 if (audio_stack == NULL)
703 /* This shouldn't happen, but deal with it anyway by using
704 the copy instead */
705 audio_stack = codec_stack_copy;
706 audio_stack_size = AUDIO_STACKSIZE;
708 #endif
710 /* Initialise the encoded audio buffer and its descriptors */
711 audio_queue.start = mpeg_malloc(AUDIOBUF_ALLOC_SIZE,
712 MPEG_ALLOC_AUDIOBUF);
713 if (audio_queue.start == NULL)
714 return false;
716 /* Start the audio thread */
717 audio_str.hdr.q = &audio_str_queue;
718 rb->queue_init(audio_str.hdr.q, false);
720 /* One-up on the priority since the core DSP over-yields internally */
721 audio_str.thread = rb->create_thread(
722 audio_thread, audio_stack, audio_stack_size, 0,
723 "mpgaudio" IF_PRIO(,PRIORITY_PLAYBACK-4) IF_COP(, CPU));
725 rb->queue_enable_queue_send(audio_str.hdr.q, &audio_str_queue_send,
726 audio_str.thread);
728 if (audio_str.thread == NULL)
729 return false;
731 /* Wait for thread to initialize */
732 str_send_msg(&audio_str, STREAM_NULL, 0);
734 return true;
737 /* Stops the audio thread */
738 void audio_thread_exit(void)
740 if (audio_str.thread != NULL)
742 str_post_msg(&audio_str, STREAM_QUIT, 0);
743 rb->thread_wait(audio_str.thread);
744 audio_str.thread = NULL;
747 #ifndef SIMULATOR
748 /* Restore the codec thread's stack */
749 rb->memcpy(audio_stack, codec_stack_copy, audio_stack_size);
750 #endif