Clean up multiple definitions of RAM size. Remove -DMEM (make) and MEM (code), use...
[maemo-rb.git] / apps / plugins / mpegplayer / audio_thread.c
blob45fa7257ef5b64d62aeef839de90d5c14800cce4
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 head 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_prev = &sbsample;
230 frame.sbsample = &sbsample;
232 /* We do this so libmad doesn't try to call codec_calloc(). This needs to
233 * be called before mad_stream_init(), mad_frame_inti() and
234 * mad_synth_init(). */
235 frame.overlap = &mad_frame_overlap;
236 stream.main_data = &mad_main_data;
238 /* Call mad initialization. Those will zero the arrays frame.overlap,
239 * frame.sbsample and frame.sbsample_prev. Therefore there is no need to
240 * zero them here. */
241 mad_stream_init(&stream);
242 mad_frame_init(&frame);
243 mad_synth_init(&synth);
246 /* Sync audio stream to a particular frame - see main decoder loop for
247 * detailed remarks */
248 static int audio_sync(struct audio_thread_data *td,
249 struct str_sync_data *sd)
251 int retval = STREAM_MATCH;
252 uint32_t sdtime = TS_TO_TICKS(clip_time(&audio_str, sd->time));
253 uint32_t time;
254 uint32_t duration = 0;
255 struct stream *str;
256 struct stream tmp_str;
257 struct mad_header header;
258 struct mad_stream stream;
260 if (td->ev.id == STREAM_SYNC)
262 /* Actually syncing for playback - use real stream */
263 time = 0;
264 str = &audio_str;
266 else
268 /* Probing - use temp stream */
269 time = INVALID_TIMESTAMP;
270 str = &tmp_str;
271 str->id = audio_str.id;
274 str->hdr.pos = sd->sk.pos;
275 str->hdr.limit = sd->sk.pos + sd->sk.len;
277 mad_stream_init(&stream);
278 mad_header_init(&header);
280 while (1)
282 if (audio_buffer(str, STREAM_PM_RANDOM_ACCESS) == STREAM_DATA_END)
284 DEBUGF("audio_sync:STR_DATA_END\n aqu:%ld swl:%ld swr:%ld\n",
285 (long)audio_queue.used, str->hdr.win_left, str->hdr.win_right);
286 if (audio_queue.used <= MAD_BUFFER_GUARD)
287 goto sync_data_end;
290 stream.error = 0;
291 mad_stream_buffer(&stream, audio_queue.ptr, audio_queue.used);
293 if (stream.sync && mad_stream_sync(&stream) < 0)
295 DEBUGF(" audio: mad_stream_sync failed\n");
296 audio_queue_advance_pos(MAX(audio_queue.curr->size - 1, 1));
297 continue;
300 stream.sync = 0;
302 if (mad_header_decode(&header, &stream) < 0)
304 DEBUGF(" audio: mad_header_decode failed:%s\n",
305 mad_stream_errorstr(&stream));
306 audio_queue_advance_pos(1);
307 continue;
310 duration = 32*MAD_NSBSAMPLES(&header);
311 time = audio_queue.curr->time;
313 DEBUGF(" audio: ft:%u t:%u fe:%u nsamp:%u sampr:%u\n",
314 (unsigned)TICKS_TO_TS(time), (unsigned)sd->time,
315 (unsigned)TICKS_TO_TS(time + duration),
316 (unsigned)duration, header.samplerate);
318 audio_queue_advance_pos(stream.this_frame - audio_queue.ptr);
320 if (time <= sdtime && sdtime < time + duration)
322 DEBUGF(" audio: ft<=t<fe\n");
323 retval = STREAM_PERFECT_MATCH;
324 break;
326 else if (time > sdtime)
328 DEBUGF(" audio: ft>t\n");
329 break;
332 audio_queue_advance_pos(stream.next_frame - audio_queue.ptr);
333 audio_queue.curr->time += duration;
335 rb->yield();
338 sync_data_end:
339 if (td->ev.id == STREAM_FIND_END_TIME)
341 if (time != INVALID_TIMESTAMP)
343 time = TICKS_TO_TS(time);
344 duration = TICKS_TO_TS(duration);
345 sd->time = time + duration;
346 retval = STREAM_PERFECT_MATCH;
348 else
350 retval = STREAM_NOT_FOUND;
354 DEBUGF(" audio header: 0x%02X%02X%02X%02X\n",
355 (unsigned)audio_queue.ptr[0], (unsigned)audio_queue.ptr[1],
356 (unsigned)audio_queue.ptr[2], (unsigned)audio_queue.ptr[3]);
358 return retval;
359 (void)td;
362 static void audio_thread_msg(struct audio_thread_data *td)
364 while (1)
366 intptr_t reply = 0;
368 switch (td->ev.id)
370 case STREAM_PLAY:
371 td->status = STREAM_PLAYING;
373 switch (td->state)
375 case TSTATE_INIT:
376 td->state = TSTATE_DECODE;
377 case TSTATE_DECODE:
378 case TSTATE_RENDER_WAIT:
379 break;
381 case TSTATE_EOS:
382 /* At end of stream - no playback possible so fire the
383 * completion event */
384 stream_generate_event(&audio_str, STREAM_EV_COMPLETE, 0);
385 break;
388 break;
390 case STREAM_PAUSE:
391 td->status = STREAM_PAUSED;
392 reply = td->state != TSTATE_EOS;
393 break;
395 case STREAM_STOP:
396 if (td->state == TSTATE_DATA)
397 stream_clear_notify(&audio_str, DISK_BUF_DATA_NOTIFY);
399 td->status = STREAM_STOPPED;
400 td->state = TSTATE_EOS;
402 reply = true;
403 break;
405 case STREAM_RESET:
406 if (td->state == TSTATE_DATA)
407 stream_clear_notify(&audio_str, DISK_BUF_DATA_NOTIFY);
409 td->status = STREAM_STOPPED;
410 td->state = TSTATE_INIT;
411 td->samplerate = 0;
412 td->nchannels = 0;
414 init_mad();
415 td->mad_errors = 0;
417 audio_queue_reset();
419 reply = true;
420 break;
422 case STREAM_NEEDS_SYNC:
423 reply = true; /* Audio always needs to */
424 break;
426 case STREAM_SYNC:
427 case STREAM_FIND_END_TIME:
428 if (td->state != TSTATE_INIT)
429 break;
431 reply = audio_sync(td, (struct str_sync_data *)td->ev.data);
432 break;
434 case DISK_BUF_DATA_NOTIFY:
435 /* Our bun is done */
436 if (td->state != TSTATE_DATA)
437 break;
439 td->state = TSTATE_DECODE;
440 str_data_notify_received(&audio_str);
441 break;
443 case STREAM_QUIT:
444 /* Time to go - make thread exit */
445 td->state = TSTATE_EOS;
446 return;
449 str_reply_msg(&audio_str, reply);
451 if (td->status == STREAM_PLAYING)
453 switch (td->state)
455 case TSTATE_DECODE:
456 case TSTATE_RENDER_WAIT:
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 #ifdef HAVE_PITCHSCREEN
485 rb->sound_set_pitch(PITCH_SPEED_100);
486 #endif
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 /* Anything else is interpreted as an exit */
514 default:
516 #ifdef HAVE_PRIORITY_SCHEDULING
517 rb->thread_set_priority(THREAD_ID_CURRENT, old_priority);
518 #endif
519 return;
524 audio_decode:
526 /** Buffering **/
527 switch (audio_buffer(&audio_str, STREAM_PM_STREAMING))
529 case STREAM_DATA_NOT_READY:
531 td.state = TSTATE_DATA;
532 goto message_wait;
533 } /* STREAM_DATA_NOT_READY: */
535 case STREAM_DATA_END:
537 if (audio_queue.used > MAD_BUFFER_GUARD)
538 break; /* Still have frames to decode */
540 /* Used up remainder of compressed audio buffer. Wait for
541 * samples on PCM buffer to finish playing. */
542 audio_queue_reset();
544 while (1)
546 if (pcm_output_empty())
548 td.state = TSTATE_EOS;
549 stream_generate_event(&audio_str, STREAM_EV_COMPLETE, 0);
550 break;
553 pcm_output_drain();
554 str_get_msg_w_tmo(&audio_str, &td.ev, 1);
556 if (td.ev.id != SYS_TIMEOUT)
557 break;
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_ERROR_BUFLEN)
582 /* This makes the codec support partially corrupted files */
583 if (++td.mad_errors <= MPA_MAX_FRAME_SIZE)
585 stream.error = 0;
586 rb->yield();
587 continue;
589 DEBUGF("audio: Too many errors\n");
591 else if (MAD_RECOVERABLE(stream.error))
593 /* libmad says it can recover - just keep on decoding */
594 rb->yield();
595 continue;
597 else
599 /* Some other unrecoverable error */
600 DEBUGF("audio: Unrecoverable error\n");
603 /* This is too hard - bail out */
604 td.state = TSTATE_EOS;
605 td.status = STREAM_ERROR;
606 stream_generate_event(&audio_str, STREAM_EV_COMPLETE, 0);
608 goto message_wait;
611 /* Adjust sizes by the frame size */
612 audio_queue_advance_pos(len);
613 td.mad_errors = 0; /* Clear errors */
615 /* Generate the pcm samples */
616 mad_synth_frame(&synth, &frame);
618 /** Output **/
619 if (frame.header.samplerate != td.samplerate)
621 td.samplerate = frame.header.samplerate;
622 rb->dsp_configure(td.dsp, DSP_SWITCH_FREQUENCY,
623 td.samplerate);
626 if (MAD_NCHANNELS(&frame.header) != td.nchannels)
628 td.nchannels = MAD_NCHANNELS(&frame.header);
629 rb->dsp_configure(td.dsp, DSP_SET_STEREO_MODE,
630 td.nchannels == 1 ?
631 STEREO_MONO : STEREO_NONINTERLEAVED);
634 td.state = TSTATE_RENDER_WAIT;
636 /* Add a frame of audio to the pcm buffer. Maximum is 1152 samples. */
637 render_wait:
638 if (synth.pcm.length > 0)
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 unsigned char *out_buf;
645 ssize_t size = out_count*4;
647 /* Wait for required amount of free buffer space */
648 while ((out_buf = pcm_output_get_buffer(&size)) == NULL)
650 /* Wait one frame */
651 int timeout = out_count*HZ / td.samplerate;
652 str_get_msg_w_tmo(&audio_str, &td.ev, MAX(timeout, 1));
653 if (td.ev.id != SYS_TIMEOUT)
654 goto message_process;
657 out_count = rb->dsp_process(td.dsp, out_buf, src, synth.pcm.length);
659 if (out_count <= 0)
660 break;
662 /* Make this data available to DMA */
663 pcm_output_commit_data(out_count*4, 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;
670 rb->yield();
671 } /* end decoding loop */
674 /* Initializes the audio thread resources and starts the thread */
675 bool audio_thread_init(void)
677 /* Initialise the encoded audio buffer and its descriptors */
678 audio_queue.start = mpeg_malloc(AUDIOBUF_ALLOC_SIZE,
679 MPEG_ALLOC_AUDIOBUF);
680 if (audio_queue.start == NULL)
681 return false;
683 /* Start the audio thread */
684 audio_str.hdr.q = &audio_str_queue;
685 rb->queue_init(audio_str.hdr.q, false);
687 /* We steal the codec thread for audio */
688 rb->codec_thread_do_callback(audio_thread, &audio_str.thread);
690 rb->queue_enable_queue_send(audio_str.hdr.q, &audio_str_queue_send,
691 audio_str.thread);
693 /* Wait for thread to initialize */
694 str_send_msg(&audio_str, STREAM_NULL, 0);
696 return true;
699 /* Stops the audio thread */
700 void audio_thread_exit(void)
702 if (audio_str.thread != 0)
704 str_post_msg(&audio_str, STREAM_QUIT, 0);
705 rb->codec_thread_do_callback(NULL, NULL);
706 audio_str.thread = 0;