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 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];
55 static unsigned char mad_main_data
[MAD_BUFFER_MDLEN
];
57 /* There isn't enough room for this in IRAM on PortalPlayer, but there
61 #if defined(CPU_COLDFIRE) || defined(CPU_S5L870X)
62 static mad_fixed_t mad_frame_overlap
[2][32][18] IBSS_ATTR
;
64 static mad_fixed_t mad_frame_overlap
[2][32][18];
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 */
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 */
88 struct audio_frame_desc
*curr
; /* Current slot */
89 struct audio_frame_desc descs
[AUDIODESC_QUEUE_LEN
];
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");
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
)
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
)
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
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
;
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
;
179 if (ret
!= STREAM_OK
)
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
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
200 curr
= audiodesc_queue_tail();
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
,
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
;
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
));
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 (long)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
;
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);
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 */
482 td
.dsp
= (struct dsp_config
*)rb
->dsp_configure(NULL
, DSP_MYDSP
,
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
);
490 /* This is the decoding loop. */
493 td
.state
= TSTATE_DECODE
;
495 /* Check for any pending messages and process them */
496 if (str_have_msg(&audio_str
))
499 /* Wait for a message to be queued */
500 str_get_msg(&audio_str
, &td
.ev
);
503 /* Process a message already dequeued */
504 audio_thread_msg(&td
);
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 */
515 #ifdef HAVE_PRIORITY_SCHEDULING
516 rb
->thread_set_priority(THREAD_ID_CURRENT
, old_priority
);
526 switch (audio_buffer(&audio_str
, STREAM_PM_STREAMING
))
528 case STREAM_DATA_NOT_READY
:
530 td
.state
= TSTATE_DATA
;
532 } /* STREAM_DATA_NOT_READY: */
534 case STREAM_DATA_END
:
536 if (audio_queue
.used
> MAD_BUFFER_GUARD
)
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
;
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);
561 } /* STREAM_DATA_END: */
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
;
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
)
590 DEBUGF("audio: Too many errors\n");
592 else if (MAD_RECOVERABLE(stream
.error
))
594 /* libmad says it can recover - just keep on decoding */
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
;
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
);
622 if (frame
.header
.samplerate
!= td
.samplerate
)
624 td
.samplerate
= frame
.header
.samplerate
;
625 rb
->dsp_configure(td
.dsp
, DSP_SWITCH_FREQUENCY
,
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
,
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. */
641 if (synth
.pcm
.length
> 0)
643 struct pcm_frame_header
*dst_hdr
= pcm_output_get_buffer();
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
)
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
,
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();
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
)
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
,
700 /* Wait for thread to initialize */
701 str_send_msg(&audio_str
, STREAM_NULL
, 0);
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;