1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
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)
47 static uint32_t codec_stack_copy
[AUDIO_STACKSIZE
/ sizeof(uint32_t)];
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
;
59 static unsigned char mad_main_data
[MAD_BUFFER_MDLEN
];
61 /* There isn't enough room for this in IRAM on PortalPlayer, but there
66 static mad_fixed_t mad_frame_overlap
[2][32][18] IBSS_ATTR
;
68 static mad_fixed_t mad_frame_overlap
[2][32][18];
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 */
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 */
92 struct audio_frame_desc
*curr
; /* Current slot */
93 struct audio_frame_desc descs
[AUDIODESC_QUEUE_LEN
];
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");
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
)
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
)
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
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
;
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
;
183 if (ret
!= STREAM_OK
)
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
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
204 curr
= audiodesc_queue_tail();
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
,
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
;
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
));
252 uint32_t duration
= 0;
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 */
266 /* Probing - use temp stream */
267 time
= INVALID_TIMESTAMP
;
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
);
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
)
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));
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);
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
;
324 else if (time
> sdtime
)
326 DEBUGF(" audio: ft>t\n");
330 audio_queue_advance_pos(stream
.next_frame
- audio_queue
.ptr
);
331 audio_queue
.curr
->time
+= duration
;
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
;
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]);
360 static void audio_thread_msg(struct audio_thread_data
*td
)
369 td
->status
= STREAM_PLAYING
;
374 td
->state
= TSTATE_DECODE
;
376 case TSTATE_RENDER_WAIT
:
377 case TSTATE_RENDER_WAIT_END
:
381 /* At end of stream - no playback possible so fire the
382 * completion event */
383 stream_generate_event(&audio_str
, STREAM_EV_COMPLETE
, 0);
390 td
->status
= STREAM_PAUSED
;
391 reply
= td
->state
!= TSTATE_EOS
;
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
;
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
;
421 case STREAM_NEEDS_SYNC
:
422 reply
= true; /* Audio always needs to */
426 case STREAM_FIND_END_TIME
:
427 if (td
->state
!= TSTATE_INIT
)
430 reply
= audio_sync(td
, (struct str_sync_data
*)td
->ev
.data
);
433 case DISK_BUF_DATA_NOTIFY
:
434 /* Our bun is done */
435 if (td
->state
!= TSTATE_DATA
)
438 td
->state
= TSTATE_DECODE
;
439 str_data_notify_received(&audio_str
);
443 /* Time to go - make thread exit */
444 td
->state
= TSTATE_EOS
;
448 str_reply_msg(&audio_str
, reply
);
450 if (td
->status
== STREAM_PLAYING
)
455 case TSTATE_RENDER_WAIT
:
456 case TSTATE_RENDER_WAIT_END
:
457 /* These return when in playing state */
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 */
477 td
.dsp
= (struct dsp_config
*)rb
->dsp_configure(NULL
, DSP_MYDSP
,
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
);
485 /* This is the decoding loop. */
488 td
.state
= TSTATE_DECODE
;
490 /* Check for any pending messages and process them */
491 if (str_have_msg(&audio_str
))
494 /* Wait for a message to be queued */
495 str_get_msg(&audio_str
, &td
.ev
);
498 /* Process a message already dequeued */
499 audio_thread_msg(&td
);
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 */
515 switch (audio_buffer(&audio_str
, STREAM_PM_STREAMING
))
517 case STREAM_DATA_NOT_READY
:
519 td
.state
= TSTATE_DATA
;
521 } /* STREAM_DATA_NOT_READY: */
523 case STREAM_DATA_END
:
525 if (audio_queue
.used
> MAD_BUFFER_GUARD
)
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
;
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);
550 } /* STREAM_DATA_END: */
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
;
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
)
579 DEBUGF("audio: Too many errors\n");
581 else if (MAD_RECOVERABLE(stream
.error
))
583 /* libmad says it can recover - just keep on decoding */
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
;
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
);
611 if (frame
.header
.samplerate
!= td
.samplerate
)
613 td
.samplerate
= frame
.header
.samplerate
;
614 rb
->dsp_configure(td
.dsp
, DSP_SWITCH_FREQUENCY
,
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
,
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. */
630 if (synth
.pcm
.length
> 0)
632 struct pcm_frame_header
*dst_hdr
= pcm_output_get_buffer();
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
)
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
,
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();
667 } /* end decoding loop */
670 /* Initializes the audio thread resources and starts the thread */
671 bool audio_thread_init(void)
675 /* The simulator thread implementation doesn't have stack buffers, and
676 these parameters are ignored. */
677 (void)i
; /* Keep gcc happy */
679 audio_stack_size
= 0;
681 /* Borrow the codec thread's stack (in IRAM on most targets) */
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
)
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
);
701 if (audio_stack
== NULL
)
703 /* This shouldn't happen, but deal with it anyway by using
705 audio_stack
= codec_stack_copy
;
706 audio_stack_size
= AUDIO_STACKSIZE
;
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
)
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
,
728 if (audio_str
.thread
== NULL
)
731 /* Wait for thread to initialize */
732 str_send_msg(&audio_str
, STREAM_NULL
, 0);
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
;
748 /* Restore the codec thread's stack */
749 rb
->memcpy(audio_stack
, codec_stack_copy
, audio_stack_size
);