MPEGPlayer: Add a second layer of caching to help speed up byte-wise scanning and...
[kugel-rb.git] / apps / plugins / mpegplayer / audio_thread.c
blobd626df66814a8c2cd8b8c3021e0a26f7bbbfdbfa
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 thread is stolen from the core codec thread */
42 static struct event_queue audio_str_queue SHAREDBSS_ATTR;
43 static struct queue_sender_list audio_str_queue_send SHAREDBSS_ATTR;
44 struct stream audio_str IBSS_ATTR;
46 /* libmad related definitions */
47 static struct mad_stream stream IBSS_ATTR;
48 static struct mad_frame frame IBSS_ATTR;
49 static struct mad_synth synth IBSS_ATTR;
51 /*sbsample buffer for mad_frame*/
52 mad_fixed_t sbsample[2][36][32];
54 /* 2567 bytes */
55 static unsigned char mad_main_data[MAD_BUFFER_MDLEN];
57 /* There isn't enough room for this in IRAM on PortalPlayer, but there
58 is for Coldfire. */
60 /* 4608 bytes */
61 #if defined(CPU_COLDFIRE) || defined(CPU_S5L870X)
62 static mad_fixed_t mad_frame_overlap[2][32][18] IBSS_ATTR;
63 #else
64 static mad_fixed_t mad_frame_overlap[2][32][18];
65 #endif
67 /** A queue for saving needed information about MPEG audio packets **/
68 #define AUDIODESC_QUEUE_LEN (1 << 5) /* 32 should be way more than sufficient -
69 if not, the case is handled */
70 #define AUDIODESC_QUEUE_MASK (AUDIODESC_QUEUE_LEN-1)
71 struct audio_frame_desc
73 uint32_t time; /* Time stamp for packet in audio ticks */
74 ssize_t size; /* Number of unprocessed bytes left in packet */
77 /* This starts out wr == rd but will never be emptied to zero during
78 streaming again in order to support initializing the first packet's
79 timestamp without a special case */
80 struct
82 /* Compressed audio data */
83 uint8_t *start; /* Start of encoded audio buffer */
84 uint8_t *ptr; /* Pointer to next encoded audio data */
85 ssize_t used; /* Number of bytes in MPEG audio buffer */
86 /* Compressed audio data descriptors */
87 unsigned read, write;
88 struct audio_frame_desc *curr; /* Current slot */
89 struct audio_frame_desc descs[AUDIODESC_QUEUE_LEN];
90 } audio_queue;
92 static inline int audiodesc_queue_count(void)
94 return audio_queue.write - audio_queue.read;
97 static inline bool audiodesc_queue_full(void)
99 return audio_queue.used >= MPA_MAX_FRAME_SIZE + MAD_BUFFER_GUARD ||
100 audiodesc_queue_count() >= AUDIODESC_QUEUE_LEN;
103 /* Increments the queue tail postion - should be used to preincrement */
104 static inline void audiodesc_queue_add_tail(void)
106 if (audiodesc_queue_full())
108 DEBUGF("audiodesc_queue_add_tail: audiodesc queue full!\n");
109 return;
112 audio_queue.write++;
115 /* Increments the queue tail position - leaves one slot as current */
116 static inline bool audiodesc_queue_remove_head(void)
118 if (audio_queue.write == audio_queue.read)
119 return false;
121 audio_queue.read++;
122 return true;
125 /* Returns the "tail" at the index just behind the write index */
126 static inline struct audio_frame_desc * audiodesc_queue_tail(void)
128 return &audio_queue.descs[(audio_queue.write - 1) & AUDIODESC_QUEUE_MASK];
131 /* Returns a pointer to the current head */
132 static inline struct audio_frame_desc * audiodesc_queue_head(void)
134 return &audio_queue.descs[audio_queue.read & AUDIODESC_QUEUE_MASK];
137 /* Resets the pts queue - call when starting and seeking */
138 static void audio_queue_reset(void)
140 audio_queue.ptr = audio_queue.start;
141 audio_queue.used = 0;
142 audio_queue.read = 0;
143 audio_queue.write = 0;
144 rb->memset(audio_queue.descs, 0, sizeof (audio_queue.descs));
145 audio_queue.curr = audiodesc_queue_head();
148 static void audio_queue_advance_pos(ssize_t len)
150 audio_queue.ptr += len;
151 audio_queue.used -= len;
152 audio_queue.curr->size -= len;
155 static int audio_buffer(struct stream *str, enum stream_parse_mode type)
157 int ret = STREAM_OK;
159 /* Carry any overshoot to the next size since we're technically
160 -size bytes into it already. If size is negative an audio
161 frame was split across packets. Old has to be saved before
162 moving the head. */
163 if (audio_queue.curr->size <= 0 && audiodesc_queue_remove_head())
165 struct audio_frame_desc *old = audio_queue.curr;
166 audio_queue.curr = audiodesc_queue_head();
167 audio_queue.curr->size += old->size;
168 old->size = 0;
171 /* Add packets to compressed audio buffer until it's full or the
172 * timestamp queue is full - whichever happens first */
173 while (!audiodesc_queue_full())
175 ret = parser_get_next_data(str, type);
176 struct audio_frame_desc *curr;
177 ssize_t len;
179 if (ret != STREAM_OK)
180 break;
182 /* Get data from next audio packet */
183 len = str->curr_packet_end - str->curr_packet;
185 if (str->pkt_flags & PKT_HAS_TS)
187 audiodesc_queue_add_tail();
188 curr = audiodesc_queue_tail();
189 curr->time = TS_TO_TICKS(str->pts);
190 /* pts->size should have been zeroed when slot was
191 freed */
193 else
195 /* Add to the one just behind the tail - this may be
196 * the head or the previouly added tail - whether or
197 * not we'll ever reach this is quite in question
198 * since audio always seems to have every packet
199 * timestamped */
200 curr = audiodesc_queue_tail();
203 curr->size += len;
205 /* Slide any remainder over to beginning */
206 if (audio_queue.ptr > audio_queue.start && audio_queue.used > 0)
208 rb->memmove(audio_queue.start, audio_queue.ptr,
209 audio_queue.used);
212 /* Splice this packet onto any remainder */
213 rb->memcpy(audio_queue.start + audio_queue.used,
214 str->curr_packet, len);
216 audio_queue.used += len;
217 audio_queue.ptr = audio_queue.start;
219 rb->yield();
222 return ret;
225 /* Initialise libmad */
226 static void init_mad(void)
228 /*init the sbsample buffer*/
229 frame.sbsample = &sbsample;
230 frame.sbsample_prev = &sbsample;
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 (long)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;
469 #ifdef HAVE_PRIORITY_SCHEDULING
470 /* Up the priority since the core DSP over-yields internally */
471 int old_priority = rb->thread_set_priority(THREAD_ID_CURRENT,
472 PRIORITY_PLAYBACK-4);
473 #endif
475 rb->memset(&td, 0, sizeof (td));
476 td.status = STREAM_STOPPED;
477 td.state = TSTATE_EOS;
479 /* We need this here to init the EMAC for Coldfire targets */
480 init_mad();
482 td.dsp = (struct dsp_config *)rb->dsp_configure(NULL, DSP_MYDSP,
483 CODEC_IDX_AUDIO);
484 rb->sound_set_pitch(PITCH_SPEED_100);
485 rb->dsp_configure(td.dsp, DSP_RESET, 0);
486 rb->dsp_configure(td.dsp, DSP_SET_SAMPLE_DEPTH, MAD_F_FRACBITS);
488 goto message_wait;
490 /* This is the decoding loop. */
491 while (1)
493 td.state = TSTATE_DECODE;
495 /* Check for any pending messages and process them */
496 if (str_have_msg(&audio_str))
498 message_wait:
499 /* Wait for a message to be queued */
500 str_get_msg(&audio_str, &td.ev);
502 message_process:
503 /* Process a message already dequeued */
504 audio_thread_msg(&td);
506 switch (td.state)
508 /* These states are the only ones that should return */
509 case TSTATE_DECODE: goto audio_decode;
510 case TSTATE_RENDER_WAIT: goto render_wait;
511 case TSTATE_RENDER_WAIT_END: goto render_wait_end;
512 /* Anything else is interpreted as an exit */
513 default:
515 #ifdef HAVE_PRIORITY_SCHEDULING
516 rb->thread_set_priority(THREAD_ID_CURRENT, old_priority);
517 #endif
518 return;
523 audio_decode:
525 /** Buffering **/
526 switch (audio_buffer(&audio_str, STREAM_PM_STREAMING))
528 case STREAM_DATA_NOT_READY:
530 td.state = TSTATE_DATA;
531 goto message_wait;
532 } /* STREAM_DATA_NOT_READY: */
534 case STREAM_DATA_END:
536 if (audio_queue.used > MAD_BUFFER_GUARD)
537 break;
539 /* Used up remainder of compressed audio buffer.
540 * Force any residue to play if audio ended before
541 * reaching the threshold */
542 td.state = TSTATE_RENDER_WAIT_END;
543 audio_queue_reset();
545 render_wait_end:
546 pcm_output_drain();
548 while (pcm_output_used() > (ssize_t)PCMOUT_LOW_WM)
550 str_get_msg_w_tmo(&audio_str, &td.ev, 1);
551 if (td.ev.id != SYS_TIMEOUT)
552 goto message_process;
555 td.state = TSTATE_EOS;
556 if (td.status == STREAM_PLAYING)
557 stream_generate_event(&audio_str, STREAM_EV_COMPLETE, 0);
559 rb->yield();
560 goto message_wait;
561 } /* STREAM_DATA_END: */
564 /** Decoding **/
565 mad_stream_buffer(&stream, audio_queue.ptr, audio_queue.used);
567 int mad_stat = mad_frame_decode(&frame, &stream);
569 ssize_t len = stream.next_frame - audio_queue.ptr;
571 if (mad_stat != 0)
573 DEBUGF("audio: Stream error: %s\n",
574 mad_stream_errorstr(&stream));
576 /* If something's goofed - try to perform resync by moving
577 * at least one byte at a time */
578 audio_queue_advance_pos(MAX(len, 1));
580 if (stream.error == MAD_FLAG_INCOMPLETE
581 || stream.error == MAD_ERROR_BUFLEN)
583 /* This makes the codec support partially corrupted files */
584 if (++td.mad_errors <= MPA_MAX_FRAME_SIZE)
586 stream.error = 0;
587 rb->yield();
588 continue;
590 DEBUGF("audio: Too many errors\n");
592 else if (MAD_RECOVERABLE(stream.error))
594 /* libmad says it can recover - just keep on decoding */
595 rb->yield();
596 continue;
598 else
600 /* Some other unrecoverable error */
601 DEBUGF("audio: Unrecoverable error\n");
604 /* This is too hard - bail out */
605 td.state = TSTATE_EOS;
607 if (td.status == STREAM_PLAYING)
608 stream_generate_event(&audio_str, STREAM_EV_COMPLETE, 0);
610 td.status = STREAM_ERROR;
611 goto message_wait;
614 /* Adjust sizes by the frame size */
615 audio_queue_advance_pos(len);
616 td.mad_errors = 0; /* Clear errors */
618 /* Generate the pcm samples */
619 mad_synth_frame(&synth, &frame);
621 /** Output **/
622 if (frame.header.samplerate != td.samplerate)
624 td.samplerate = frame.header.samplerate;
625 rb->dsp_configure(td.dsp, DSP_SWITCH_FREQUENCY,
626 td.samplerate);
629 if (MAD_NCHANNELS(&frame.header) != td.nchannels)
631 td.nchannels = MAD_NCHANNELS(&frame.header);
632 rb->dsp_configure(td.dsp, DSP_SET_STEREO_MODE,
633 td.nchannels == 1 ?
634 STEREO_MONO : STEREO_NONINTERLEAVED);
637 td.state = TSTATE_RENDER_WAIT;
639 /* Add a frame of audio to the pcm buffer. Maximum is 1152 samples. */
640 render_wait:
641 if (synth.pcm.length > 0)
643 struct pcm_frame_header *dst_hdr = pcm_output_get_buffer();
644 const char *src[2] =
645 { (char *)synth.pcm.samples[0], (char *)synth.pcm.samples[1] };
646 int out_count = (synth.pcm.length * CLOCK_RATE
647 + (td.samplerate - 1)) / td.samplerate;
648 ssize_t size = sizeof(*dst_hdr) + out_count*4;
650 /* Wait for required amount of free buffer space */
651 while (pcm_output_free() < size)
653 /* Wait one frame */
654 int timeout = out_count*HZ / td.samplerate;
655 str_get_msg_w_tmo(&audio_str, &td.ev, MAX(timeout, 1));
656 if (td.ev.id != SYS_TIMEOUT)
657 goto message_process;
660 out_count = rb->dsp_process(td.dsp, dst_hdr->data, src,
661 synth.pcm.length);
663 if (out_count <= 0)
664 break;
666 dst_hdr->size = sizeof(*dst_hdr) + out_count*4;
667 dst_hdr->time = audio_queue.curr->time;
669 /* As long as we're on this timestamp, the time is just
670 incremented by the number of samples */
671 audio_queue.curr->time += out_count;
673 /* Make this data available to DMA */
674 pcm_output_add_data();
677 rb->yield();
678 } /* end decoding loop */
681 /* Initializes the audio thread resources and starts the thread */
682 bool audio_thread_init(void)
684 /* Initialise the encoded audio buffer and its descriptors */
685 audio_queue.start = mpeg_malloc(AUDIOBUF_ALLOC_SIZE,
686 MPEG_ALLOC_AUDIOBUF);
687 if (audio_queue.start == NULL)
688 return false;
690 /* Start the audio thread */
691 audio_str.hdr.q = &audio_str_queue;
692 rb->queue_init(audio_str.hdr.q, false);
694 /* We steal the codec thread for audio */
695 rb->codec_thread_do_callback(audio_thread, &audio_str.thread);
697 rb->queue_enable_queue_send(audio_str.hdr.q, &audio_str_queue_send,
698 audio_str.thread);
700 /* Wait for thread to initialize */
701 str_send_msg(&audio_str, STREAM_NULL, 0);
703 return true;
706 /* Stops the audio thread */
707 void audio_thread_exit(void)
709 if (audio_str.thread != 0)
711 str_post_msg(&audio_str, STREAM_QUIT, 0);
712 rb->codec_thread_do_callback(NULL, NULL);
713 audio_str.thread = 0;