A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita
audio
rust
zig
deno
mpris
rockbox
mpd
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * mpegplayer audio thread implementation
11 *
12 * Copyright (c) 2007 Michael Sevakis
13 *
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.
18 *
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
21 *
22 ****************************************************************************/
23#include "plugin.h"
24#include "mpegplayer.h"
25#include "codecs/libmad/bit.h"
26#include "codecs/libmad/mad.h"
27
28/** Audio stream and thread **/
29struct pts_queue_slot;
30struct audio_thread_data
31{
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 */
39 struct dsp_buffer src; /* Current audio data for DSP processing */
40};
41
42/* The audio thread is stolen from the core codec thread */
43static struct event_queue audio_str_queue SHAREDBSS_ATTR;
44static struct queue_sender_list audio_str_queue_send SHAREDBSS_ATTR;
45struct stream audio_str IBSS_ATTR;
46
47/* libmad related definitions */
48static struct mad_stream stream IBSS_ATTR;
49static struct mad_frame frame IBSS_ATTR;
50static struct mad_synth synth IBSS_ATTR;
51
52/*sbsample buffer for mad_frame*/
53mad_fixed_t sbsample[2][36][32];
54
55/* 2567 bytes */
56static unsigned char mad_main_data[MAD_BUFFER_MDLEN];
57
58/* There isn't enough room for this in IRAM on PortalPlayer, but there
59 is for Coldfire. */
60
61/* 4608 bytes */
62#if defined(CPU_COLDFIRE) || defined(CPU_S5L87XX)
63static mad_fixed_t mad_frame_overlap[2][32][18] IBSS_ATTR;
64#else
65static mad_fixed_t mad_frame_overlap[2][32][18];
66#endif
67
68/** A queue for saving needed information about MPEG audio packets **/
69#define AUDIODESC_QUEUE_LEN (1 << 5) /* 32 should be way more than sufficient -
70 if not, the case is handled */
71#define AUDIODESC_QUEUE_MASK (AUDIODESC_QUEUE_LEN-1)
72struct audio_frame_desc
73{
74 uint32_t time; /* Time stamp for packet in audio ticks */
75 ssize_t size; /* Number of unprocessed bytes left in packet */
76};
77
78 /* This starts out wr == rd but will never be emptied to zero during
79 streaming again in order to support initializing the first packet's
80 timestamp without a special case */
81struct
82{
83 /* Compressed audio data */
84 uint8_t *start; /* Start of encoded audio buffer */
85 uint8_t *ptr; /* Pointer to next encoded audio data */
86 ssize_t used; /* Number of bytes in MPEG audio buffer */
87 /* Compressed audio data descriptors */
88 unsigned read, write;
89 struct audio_frame_desc *curr; /* Current slot */
90 struct audio_frame_desc descs[AUDIODESC_QUEUE_LEN];
91} audio_queue;
92
93static inline int audiodesc_queue_count(void)
94{
95 return audio_queue.write - audio_queue.read;
96}
97
98static inline bool audiodesc_queue_full(void)
99{
100 return audio_queue.used >= MPA_MAX_FRAME_SIZE + MAD_BUFFER_GUARD ||
101 audiodesc_queue_count() >= AUDIODESC_QUEUE_LEN;
102}
103
104/* Increments the queue tail postion - should be used to preincrement */
105static inline void audiodesc_queue_add_tail(void)
106{
107 if (audiodesc_queue_full())
108 {
109 DEBUGF("audiodesc_queue_add_tail: audiodesc queue full!\n");
110 return;
111 }
112
113 audio_queue.write++;
114}
115
116/* Increments the queue head position - leaves one slot as current */
117static inline bool audiodesc_queue_remove_head(void)
118{
119 if (audio_queue.write == audio_queue.read)
120 return false;
121
122 audio_queue.read++;
123 return true;
124}
125
126/* Returns the "tail" at the index just behind the write index */
127static inline struct audio_frame_desc * audiodesc_queue_tail(void)
128{
129 return &audio_queue.descs[(audio_queue.write - 1) & AUDIODESC_QUEUE_MASK];
130}
131
132/* Returns a pointer to the current head */
133static inline struct audio_frame_desc * audiodesc_queue_head(void)
134{
135 return &audio_queue.descs[audio_queue.read & AUDIODESC_QUEUE_MASK];
136}
137
138/* Resets the pts queue - call when starting and seeking */
139static void audio_queue_reset(void)
140{
141 audio_queue.ptr = audio_queue.start;
142 audio_queue.used = 0;
143 audio_queue.read = 0;
144 audio_queue.write = 0;
145 rb->memset(audio_queue.descs, 0, sizeof (audio_queue.descs));
146 audio_queue.curr = audiodesc_queue_head();
147}
148
149static void audio_queue_advance_pos(ssize_t len)
150{
151 audio_queue.ptr += len;
152 audio_queue.used -= len;
153 audio_queue.curr->size -= len;
154}
155
156static int audio_buffer(struct stream *str, enum stream_parse_mode type)
157{
158 int ret = STREAM_OK;
159
160 /* Carry any overshoot to the next size since we're technically
161 -size bytes into it already. If size is negative an audio
162 frame was split across packets. Old has to be saved before
163 moving the head. */
164 if (audio_queue.curr->size <= 0 && audiodesc_queue_remove_head())
165 {
166 struct audio_frame_desc *old = audio_queue.curr;
167 audio_queue.curr = audiodesc_queue_head();
168 audio_queue.curr->size += old->size;
169 old->size = 0;
170 }
171
172 /* Add packets to compressed audio buffer until it's full or the
173 * timestamp queue is full - whichever happens first */
174 while (!audiodesc_queue_full())
175 {
176 ret = parser_get_next_data(str, type);
177 struct audio_frame_desc *curr;
178 ssize_t len;
179
180 if (ret != STREAM_OK)
181 break;
182
183 /* Get data from next audio packet */
184 len = str->curr_packet_end - str->curr_packet;
185
186 if (str->pkt_flags & PKT_HAS_TS)
187 {
188 audiodesc_queue_add_tail();
189 curr = audiodesc_queue_tail();
190 curr->time = TS_TO_TICKS(str->pts);
191 /* pts->size should have been zeroed when slot was
192 freed */
193 }
194 else
195 {
196 /* Add to the one just behind the tail - this may be
197 * the head or the previouly added tail - whether or
198 * not we'll ever reach this is quite in question
199 * since audio always seems to have every packet
200 * timestamped */
201 curr = audiodesc_queue_tail();
202 }
203
204 curr->size += len;
205
206 /* Slide any remainder over to beginning */
207 if (audio_queue.ptr > audio_queue.start && audio_queue.used > 0)
208 {
209 rb->memmove(audio_queue.start, audio_queue.ptr,
210 audio_queue.used);
211 }
212
213 /* Splice this packet onto any remainder */
214 rb->memcpy(audio_queue.start + audio_queue.used,
215 str->curr_packet, len);
216
217 audio_queue.used += len;
218 audio_queue.ptr = audio_queue.start;
219
220 rb->yield();
221 }
222
223 return ret;
224}
225
226/* Initialise libmad */
227static void init_mad(void)
228{
229 /* init the sbsample buffer */
230 frame.sbsample_prev = &sbsample;
231 frame.sbsample = &sbsample;
232
233 /* We do this so libmad doesn't try to call codec_calloc(). This needs to
234 * be called before mad_stream_init(), mad_frame_inti() and
235 * mad_synth_init(). */
236 frame.overlap = &mad_frame_overlap;
237 stream.main_data = &mad_main_data;
238
239 /* Call mad initialization. Those will zero the arrays frame.overlap,
240 * frame.sbsample and frame.sbsample_prev. Therefore there is no need to
241 * zero them here. */
242 mad_stream_init(&stream);
243 mad_frame_init(&frame);
244 mad_synth_init(&synth);
245}
246
247/* Sync audio stream to a particular frame - see main decoder loop for
248 * detailed remarks */
249static int audio_sync(struct audio_thread_data *td,
250 struct str_sync_data *sd)
251{
252 int retval = STREAM_MATCH;
253 uint32_t sdtime = TS_TO_TICKS(clip_time(&audio_str, sd->time));
254 uint32_t time;
255 uint32_t duration = 0;
256 struct stream *str;
257 struct stream tmp_str;
258 struct mad_header header;
259 struct mad_stream stream;
260
261 if (td->ev.id == STREAM_SYNC)
262 {
263 /* Actually syncing for playback - use real stream */
264 time = 0;
265 str = &audio_str;
266 }
267 else
268 {
269 /* Probing - use temp stream */
270 time = INVALID_TIMESTAMP;
271 str = &tmp_str;
272 str->id = audio_str.id;
273 }
274
275 str->hdr.pos = sd->sk.pos;
276 str->hdr.limit = sd->sk.pos + sd->sk.len;
277
278 mad_stream_init(&stream);
279 mad_header_init(&header);
280
281 while (1)
282 {
283 if (audio_buffer(str, STREAM_PM_RANDOM_ACCESS) == STREAM_DATA_END)
284 {
285 DEBUGF("audio_sync:STR_DATA_END\n aqu:%ld swl:%jd swr:%jd\n",
286 (long)audio_queue.used, (intmax_t) str->hdr.win_left,
287 (intmax_t) str->hdr.win_right);
288 if (audio_queue.used <= MAD_BUFFER_GUARD)
289 goto sync_data_end;
290 }
291
292 stream.error = 0;
293 mad_stream_buffer(&stream, audio_queue.ptr, audio_queue.used);
294
295 if (stream.sync && mad_stream_sync(&stream) < 0)
296 {
297 DEBUGF(" audio: mad_stream_sync failed\n");
298 audio_queue_advance_pos(MAX(audio_queue.curr->size - 1, 1));
299 continue;
300 }
301
302 stream.sync = 0;
303
304 if (mad_header_decode(&header, &stream) < 0)
305 {
306 DEBUGF(" audio: mad_header_decode failed:%s\n",
307 mad_stream_errorstr(&stream));
308 audio_queue_advance_pos(1);
309 continue;
310 }
311
312 duration = 32*MAD_NSBSAMPLES(&header);
313 time = audio_queue.curr->time;
314
315 DEBUGF(" audio: ft:%u t:%u fe:%u nsamp:%u sampr:%u\n",
316 (unsigned)TICKS_TO_TS(time), (unsigned)sd->time,
317 (unsigned)TICKS_TO_TS(time + duration),
318 (unsigned)duration, header.samplerate);
319
320 audio_queue_advance_pos(stream.this_frame - audio_queue.ptr);
321
322 if (time <= sdtime && sdtime < time + duration)
323 {
324 DEBUGF(" audio: ft<=t<fe\n");
325 retval = STREAM_PERFECT_MATCH;
326 break;
327 }
328 else if (time > sdtime)
329 {
330 DEBUGF(" audio: ft>t\n");
331 break;
332 }
333
334 audio_queue_advance_pos(stream.next_frame - audio_queue.ptr);
335 audio_queue.curr->time += duration;
336
337 rb->yield();
338 }
339
340sync_data_end:
341 if (td->ev.id == STREAM_FIND_END_TIME)
342 {
343 if (time != INVALID_TIMESTAMP)
344 {
345 time = TICKS_TO_TS(time);
346 duration = TICKS_TO_TS(duration);
347 sd->time = time + duration;
348 retval = STREAM_PERFECT_MATCH;
349 }
350 else
351 {
352 retval = STREAM_NOT_FOUND;
353 }
354 }
355
356 DEBUGF(" audio header: 0x%02X%02X%02X%02X\n",
357 (unsigned)audio_queue.ptr[0], (unsigned)audio_queue.ptr[1],
358 (unsigned)audio_queue.ptr[2], (unsigned)audio_queue.ptr[3]);
359
360 return retval;
361 (void)td;
362}
363
364static void audio_thread_msg(struct audio_thread_data *td)
365{
366 while (1)
367 {
368 intptr_t reply = 0;
369
370 switch (td->ev.id)
371 {
372 case STREAM_PLAY:
373 td->status = STREAM_PLAYING;
374
375 switch (td->state)
376 {
377 case TSTATE_INIT:
378 td->state = TSTATE_DECODE;
379 case TSTATE_DECODE:
380 case TSTATE_RENDER_WAIT:
381 break;
382
383 case TSTATE_EOS:
384 /* At end of stream - no playback possible so fire the
385 * completion event */
386 stream_generate_event(&audio_str, STREAM_EV_COMPLETE, 0);
387 break;
388 }
389
390 break;
391
392 case STREAM_PAUSE:
393 td->status = STREAM_PAUSED;
394 reply = td->state != TSTATE_EOS;
395 break;
396
397 case STREAM_STOP:
398 if (td->state == TSTATE_DATA)
399 stream_clear_notify(&audio_str, DISK_BUF_DATA_NOTIFY);
400
401 td->status = STREAM_STOPPED;
402 td->state = TSTATE_EOS;
403
404 reply = true;
405 break;
406
407 case STREAM_RESET:
408 if (td->state == TSTATE_DATA)
409 stream_clear_notify(&audio_str, DISK_BUF_DATA_NOTIFY);
410
411 td->status = STREAM_STOPPED;
412 td->state = TSTATE_INIT;
413 td->samplerate = 0;
414 td->nchannels = 0;
415
416 init_mad();
417 td->mad_errors = 0;
418
419 audio_queue_reset();
420
421 reply = true;
422 break;
423
424 case STREAM_NEEDS_SYNC:
425 reply = true; /* Audio always needs to */
426 break;
427
428 case STREAM_SYNC:
429 case STREAM_FIND_END_TIME:
430 if (td->state != TSTATE_INIT)
431 break;
432
433 reply = audio_sync(td, (struct str_sync_data *)td->ev.data);
434 break;
435
436 case DISK_BUF_DATA_NOTIFY:
437 /* Our bun is done */
438 if (td->state != TSTATE_DATA)
439 break;
440
441 td->state = TSTATE_DECODE;
442 str_data_notify_received(&audio_str);
443 break;
444
445 case STREAM_QUIT:
446 /* Time to go - make thread exit */
447 td->state = TSTATE_EOS;
448 return;
449 }
450
451 str_reply_msg(&audio_str, reply);
452
453 if (td->status == STREAM_PLAYING)
454 {
455 switch (td->state)
456 {
457 case TSTATE_DECODE:
458 case TSTATE_RENDER_WAIT:
459 /* These return when in playing state */
460 return;
461 }
462 }
463
464 str_get_msg(&audio_str, &td->ev);
465 }
466}
467
468static void audio_thread(void)
469{
470 struct audio_thread_data td;
471#ifdef HAVE_PRIORITY_SCHEDULING
472 /* Up the priority since the core DSP over-yields internally */
473 int old_priority = rb->thread_set_priority(rb->thread_self(),
474 PRIORITY_PLAYBACK-4);
475#endif
476
477 rb->memset(&td, 0, sizeof (td));
478 td.status = STREAM_STOPPED;
479 td.state = TSTATE_EOS;
480
481 /* We need this here to init the EMAC for Coldfire targets */
482 init_mad();
483
484 td.dsp = rb->dsp_get_config(CODEC_IDX_AUDIO);
485 rb->dsp_configure(td.dsp, DSP_SET_OUT_FREQUENCY, CLOCK_RATE);
486#ifdef HAVE_PITCHCONTROL
487 rb->sound_set_pitch(PITCH_SPEED_100);
488 rb->dsp_set_timestretch(PITCH_SPEED_100);
489#endif
490 rb->dsp_configure(td.dsp, DSP_RESET, 0);
491 rb->dsp_configure(td.dsp, DSP_FLUSH, 0);
492 rb->dsp_configure(td.dsp, DSP_SET_SAMPLE_DEPTH, MAD_F_FRACBITS);
493
494 goto message_wait;
495
496 /* This is the decoding loop. */
497 while (1)
498 {
499 td.state = TSTATE_DECODE;
500
501 /* Check for any pending messages and process them */
502 if (str_have_msg(&audio_str))
503 {
504 message_wait:
505 /* Wait for a message to be queued */
506 str_get_msg(&audio_str, &td.ev);
507
508 message_process:
509 /* Process a message already dequeued */
510 audio_thread_msg(&td);
511
512 switch (td.state)
513 {
514 /* These states are the only ones that should return */
515 case TSTATE_DECODE: goto audio_decode;
516 case TSTATE_RENDER_WAIT: goto render_wait;
517 /* Anything else is interpreted as an exit */
518 default:
519 {
520#ifdef HAVE_PRIORITY_SCHEDULING
521 rb->thread_set_priority(rb->thread_self(), old_priority);
522#endif
523 return;
524 }
525 }
526 }
527
528 audio_decode:
529
530 /** Buffering **/
531 switch (audio_buffer(&audio_str, STREAM_PM_STREAMING))
532 {
533 case STREAM_DATA_NOT_READY:
534 {
535 td.state = TSTATE_DATA;
536 goto message_wait;
537 } /* STREAM_DATA_NOT_READY: */
538
539 case STREAM_DATA_END:
540 {
541 if (audio_queue.used > MAD_BUFFER_GUARD)
542 break; /* Still have frames to decode */
543
544 /* Used up remainder of compressed audio buffer. Wait for
545 * samples on PCM buffer to finish playing. */
546 audio_queue_reset();
547
548 while (1)
549 {
550 if (pcm_output_empty())
551 {
552 td.state = TSTATE_EOS;
553 stream_generate_event(&audio_str, STREAM_EV_COMPLETE, 0);
554 break;
555 }
556
557 pcm_output_drain();
558 str_get_msg_w_tmo(&audio_str, &td.ev, 1);
559
560 if (td.ev.id != SYS_TIMEOUT)
561 break;
562 }
563
564 goto message_wait;
565 } /* STREAM_DATA_END: */
566 }
567
568 /** Decoding **/
569 mad_stream_buffer(&stream, audio_queue.ptr, audio_queue.used);
570
571 int mad_stat = mad_frame_decode(&frame, &stream);
572
573 ssize_t len = stream.next_frame - audio_queue.ptr;
574
575 if (mad_stat != 0)
576 {
577 DEBUGF("audio: Stream error: %s\n",
578 mad_stream_errorstr(&stream));
579
580 /* If something's goofed - try to perform resync by moving
581 * at least one byte at a time */
582 audio_queue_advance_pos(MAX(len, 1));
583
584 if (stream.error == MAD_ERROR_BUFLEN)
585 {
586 /* This makes the codec support partially corrupted files */
587 if (++td.mad_errors <= MPA_MAX_FRAME_SIZE)
588 {
589 stream.error = 0;
590 rb->yield();
591 continue;
592 }
593 DEBUGF("audio: Too many errors\n");
594 }
595 else if (MAD_RECOVERABLE(stream.error))
596 {
597 /* libmad says it can recover - just keep on decoding */
598 rb->yield();
599 continue;
600 }
601 else
602 {
603 /* Some other unrecoverable error */
604 DEBUGF("audio: Unrecoverable error\n");
605 }
606
607 /* This is too hard - bail out */
608 td.state = TSTATE_EOS;
609 td.status = STREAM_ERROR;
610 stream_generate_event(&audio_str, STREAM_EV_COMPLETE, 0);
611
612 goto message_wait;
613 }
614
615 /* Adjust sizes by the frame size */
616 audio_queue_advance_pos(len);
617 td.mad_errors = 0; /* Clear errors */
618
619 /* Generate the pcm samples */
620 mad_synth_frame(&synth, &frame);
621
622 /** Output **/
623 if (frame.header.samplerate != td.samplerate)
624 {
625 td.samplerate = frame.header.samplerate;
626 rb->dsp_configure(td.dsp, DSP_SET_FREQUENCY,
627 td.samplerate);
628 }
629
630 if (MAD_NCHANNELS(&frame.header) != td.nchannels)
631 {
632 td.nchannels = MAD_NCHANNELS(&frame.header);
633 rb->dsp_configure(td.dsp, DSP_SET_STEREO_MODE,
634 td.nchannels == 1 ?
635 STEREO_MONO : STEREO_NONINTERLEAVED);
636 }
637
638 td.src.remcount = synth.pcm.length;
639 td.src.pin[0] = synth.pcm.samples[0];
640 td.src.pin[1] = synth.pcm.samples[1];
641 td.src.proc_mask = 0;
642
643 td.state = TSTATE_RENDER_WAIT;
644
645 /* Add a frame of audio to the pcm buffer. Maximum is 1152 samples. */
646 render_wait:
647 rb->yield();
648
649 while (1)
650 {
651 struct dsp_buffer dst;
652 dst.remcount = 0;
653 dst.bufcount = MAX(td.src.remcount, 1024);
654
655 ssize_t size = dst.bufcount * 2 * sizeof(int16_t);
656
657 /* Wait for required amount of free buffer space */
658 while ((dst.p16out = pcm_output_get_buffer(&size)) == NULL)
659 {
660 /* Wait one frame */
661 int timeout = dst.bufcount*HZ / td.samplerate;
662 str_get_msg_w_tmo(&audio_str, &td.ev, MAX(timeout, 1));
663 if (td.ev.id != SYS_TIMEOUT)
664 goto message_process;
665 }
666
667 dst.bufcount = size / (2 * sizeof (int16_t));
668 rb->dsp_process(td.dsp, &td.src, &dst, true);
669
670 if (dst.remcount > 0)
671 {
672 /* Make this data available to DMA */
673 pcm_output_commit_data(dst.remcount * 2 * sizeof(int16_t),
674 audio_queue.curr->time);
675
676 /* As long as we're on this timestamp, the time is just
677 incremented by the number of samples */
678 audio_queue.curr->time += dst.remcount;
679 }
680 else if (td.src.remcount <= 0)
681 {
682 break;
683 }
684 }
685 } /* end decoding loop */
686}
687
688/* Initializes the audio thread resources and starts the thread */
689bool audio_thread_init(void)
690{
691 /* Initialise the encoded audio buffer and its descriptors */
692 audio_queue.start = mpeg_malloc(AUDIOBUF_ALLOC_SIZE,
693 MPEG_ALLOC_AUDIOBUF);
694 if (audio_queue.start == NULL)
695 return false;
696
697 /* Start the audio thread */
698 audio_str.hdr.q = &audio_str_queue;
699 rb->queue_init(audio_str.hdr.q, false);
700
701 /* We steal the codec thread for audio */
702 rb->codec_thread_do_callback(audio_thread, &audio_str.thread);
703
704 rb->queue_enable_queue_send(audio_str.hdr.q, &audio_str_queue_send,
705 audio_str.thread);
706
707 /* Wait for thread to initialize */
708 str_send_msg(&audio_str, STREAM_NULL, 0);
709
710 return true;
711}
712
713/* Stops the audio thread */
714void audio_thread_exit(void)
715{
716 if (audio_str.thread != 0)
717 {
718 str_post_msg(&audio_str, STREAM_QUIT, 0);
719 rb->codec_thread_do_callback(NULL, NULL);
720 audio_str.thread = 0;
721 }
722}