2007-12-29 19:46:35 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* PCM output buffer definitions
|
|
|
|
*
|
|
|
|
* Copyright (c) 2007 Michael Sevakis
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2007-12-29 19:46:35 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include "plugin.h"
|
|
|
|
#include "mpegplayer.h"
|
|
|
|
|
2011-07-08 22:31:15 +00:00
|
|
|
/* PCM channel we're using */
|
|
|
|
#define MPEG_PCM_CHANNEL PCM_MIXER_CHAN_PLAYBACK
|
|
|
|
|
2007-12-29 19:46:35 +00:00
|
|
|
/* Pointers */
|
|
|
|
|
|
|
|
/* Start of buffer */
|
|
|
|
static struct pcm_frame_header * ALIGNED_ATTR(4) pcm_buffer;
|
|
|
|
/* End of buffer (not guard) */
|
|
|
|
static struct pcm_frame_header * ALIGNED_ATTR(4) pcmbuf_end;
|
|
|
|
/* Read pointer */
|
|
|
|
static struct pcm_frame_header * ALIGNED_ATTR(4) pcmbuf_head IBSS_ATTR;
|
|
|
|
/* Write pointer */
|
|
|
|
static struct pcm_frame_header * ALIGNED_ATTR(4) pcmbuf_tail IBSS_ATTR;
|
|
|
|
|
|
|
|
/* Bytes */
|
2008-02-01 02:25:15 +00:00
|
|
|
static ssize_t pcmbuf_curr_size IBSS_ATTR; /* Size of currently playing frame */
|
2011-01-07 05:17:28 +00:00
|
|
|
static ssize_t pcmbuf_read IBSS_ATTR; /* Number of bytes read by DMA */
|
|
|
|
static ssize_t pcmbuf_written IBSS_ATTR; /* Number of bytes written by source */
|
2007-12-29 19:46:35 +00:00
|
|
|
static ssize_t pcmbuf_threshold IBSS_ATTR; /* Non-silence threshold */
|
|
|
|
|
|
|
|
/* Clock */
|
|
|
|
static uint32_t clock_start IBSS_ATTR; /* Clock at playback start */
|
2011-01-07 05:17:28 +00:00
|
|
|
static uint32_t volatile clock_tick IBSS_ATTR; /* Our base clock */
|
|
|
|
static uint32_t volatile clock_time IBSS_ATTR; /* Timestamp adjusted */
|
2007-12-29 19:46:35 +00:00
|
|
|
|
2011-01-07 05:17:28 +00:00
|
|
|
static int pcm_skipped = 0;
|
|
|
|
static int pcm_underruns = 0;
|
2008-02-01 02:25:15 +00:00
|
|
|
|
2007-12-29 19:46:35 +00:00
|
|
|
/* Small silence clip. ~5.80ms @ 44.1kHz */
|
|
|
|
static int16_t silence[256*2] ALIGNED_ATTR(4) = { 0 };
|
|
|
|
|
2011-01-07 05:17:28 +00:00
|
|
|
/* Delete all buffer contents */
|
|
|
|
static void pcm_reset_buffer(void)
|
|
|
|
{
|
|
|
|
pcmbuf_threshold = PCMOUT_PLAY_WM;
|
|
|
|
pcmbuf_curr_size = pcmbuf_read = pcmbuf_written = 0;
|
|
|
|
pcmbuf_head = pcmbuf_tail = pcm_buffer;
|
|
|
|
pcm_skipped = pcm_underruns = 0;
|
|
|
|
}
|
|
|
|
|
2007-12-29 19:46:35 +00:00
|
|
|
/* Advance a PCM buffer pointer by size bytes circularly */
|
|
|
|
static inline void pcm_advance_buffer(struct pcm_frame_header **p,
|
|
|
|
size_t size)
|
|
|
|
{
|
|
|
|
*p = SKIPBYTES(*p, size);
|
|
|
|
if (*p >= pcmbuf_end)
|
2008-02-01 02:25:15 +00:00
|
|
|
*p = SKIPBYTES(*p, -PCMOUT_BUFSIZE);
|
2007-12-29 19:46:35 +00:00
|
|
|
}
|
|
|
|
|
2011-01-07 05:17:28 +00:00
|
|
|
/* Return physical space used */
|
|
|
|
static inline ssize_t pcm_output_bytes_used(void)
|
2007-12-29 19:46:35 +00:00
|
|
|
{
|
2011-01-07 05:17:28 +00:00
|
|
|
return pcmbuf_written - pcmbuf_read; /* wrap-safe */
|
2007-12-29 19:46:35 +00:00
|
|
|
}
|
|
|
|
|
2011-01-07 05:17:28 +00:00
|
|
|
/* Return physical space free */
|
|
|
|
static inline ssize_t pcm_output_bytes_free(void)
|
2007-12-29 19:46:35 +00:00
|
|
|
{
|
2011-01-07 05:17:28 +00:00
|
|
|
return PCMOUT_BUFSIZE - pcm_output_bytes_used();
|
2007-12-29 19:46:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Audio DMA handler */
|
2012-02-23 13:14:46 +00:00
|
|
|
static void get_more(const void **start, size_t *size)
|
2007-12-29 19:46:35 +00:00
|
|
|
{
|
2008-02-01 02:25:15 +00:00
|
|
|
ssize_t sz;
|
|
|
|
|
|
|
|
/* Free-up the last frame played frame if any */
|
|
|
|
pcmbuf_read += pcmbuf_curr_size;
|
|
|
|
pcmbuf_curr_size = 0;
|
|
|
|
|
2011-01-07 05:17:28 +00:00
|
|
|
sz = pcm_output_bytes_used();
|
2007-12-29 19:46:35 +00:00
|
|
|
|
|
|
|
if (sz > pcmbuf_threshold)
|
|
|
|
{
|
|
|
|
pcmbuf_threshold = PCMOUT_LOW_WM;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
uint32_t time = pcmbuf_head->time;
|
2011-01-07 05:17:28 +00:00
|
|
|
int32_t offset = time - clock_time;
|
2007-12-29 19:46:35 +00:00
|
|
|
|
|
|
|
sz = pcmbuf_head->size;
|
|
|
|
|
2011-01-07 05:17:28 +00:00
|
|
|
if (sz < (ssize_t)(PCM_HDR_SIZE + 4) ||
|
2007-12-29 19:46:35 +00:00
|
|
|
(sz & 3) != 0)
|
|
|
|
{
|
|
|
|
/* Just show a warning about this - will never happen
|
2011-01-07 05:17:28 +00:00
|
|
|
* without a corrupted buffer */
|
2010-05-15 03:34:31 +00:00
|
|
|
DEBUGF("get_more: invalid size (%ld)\n", (long)sz);
|
2007-12-29 19:46:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (offset < -100*CLOCK_RATE/1000)
|
|
|
|
{
|
|
|
|
/* Frame more than 100ms late - drop it */
|
|
|
|
pcm_advance_buffer(&pcmbuf_head, sz);
|
|
|
|
pcmbuf_read += sz;
|
2008-02-01 02:25:15 +00:00
|
|
|
pcm_skipped++;
|
2011-01-07 05:17:28 +00:00
|
|
|
if (pcm_output_bytes_used() > 0)
|
2007-12-29 19:46:35 +00:00
|
|
|
continue;
|
2008-02-01 02:25:15 +00:00
|
|
|
|
|
|
|
/* Ran out so revert to default watermark */
|
|
|
|
pcmbuf_threshold = PCMOUT_PLAY_WM;
|
2011-01-07 05:17:28 +00:00
|
|
|
pcm_underruns++;
|
2007-12-29 19:46:35 +00:00
|
|
|
}
|
|
|
|
else if (offset < 100*CLOCK_RATE/1000)
|
|
|
|
{
|
|
|
|
/* Frame less than 100ms early - play it */
|
2011-01-25 17:18:20 +00:00
|
|
|
struct pcm_frame_header *head = pcmbuf_head;
|
2007-12-29 19:46:35 +00:00
|
|
|
|
|
|
|
pcm_advance_buffer(&pcmbuf_head, sz);
|
2008-02-01 02:25:15 +00:00
|
|
|
pcmbuf_curr_size = sz;
|
2007-12-29 19:46:35 +00:00
|
|
|
|
2011-01-07 05:17:28 +00:00
|
|
|
sz -= PCM_HDR_SIZE;
|
2007-12-29 19:46:35 +00:00
|
|
|
|
|
|
|
/* Audio is time master - keep clock synchronized */
|
2011-01-07 05:17:28 +00:00
|
|
|
clock_time = time + (sz >> 2);
|
2007-12-29 19:46:35 +00:00
|
|
|
|
|
|
|
/* Update base clock */
|
2011-01-07 05:17:28 +00:00
|
|
|
clock_tick += sz >> 2;
|
2011-01-25 17:18:20 +00:00
|
|
|
|
|
|
|
*start = head->data;
|
|
|
|
*size = sz;
|
2007-12-29 19:46:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Frame will be dropped - play silence clip */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Ran out so revert to default watermark */
|
2008-02-01 02:25:15 +00:00
|
|
|
if (pcmbuf_threshold == PCMOUT_LOW_WM)
|
|
|
|
pcm_underruns++;
|
|
|
|
|
2007-12-29 19:46:35 +00:00
|
|
|
pcmbuf_threshold = PCMOUT_PLAY_WM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Keep clock going at all times */
|
2011-01-25 17:18:20 +00:00
|
|
|
clock_time += sizeof (silence) / 4;
|
|
|
|
clock_tick += sizeof (silence) / 4;
|
|
|
|
|
2007-12-29 19:46:35 +00:00
|
|
|
*start = (unsigned char *)silence;
|
|
|
|
*size = sizeof (silence);
|
|
|
|
|
2011-01-07 05:17:28 +00:00
|
|
|
if (sz < 0)
|
2007-12-29 19:46:35 +00:00
|
|
|
pcmbuf_read = pcmbuf_written;
|
|
|
|
}
|
|
|
|
|
2011-01-07 05:17:28 +00:00
|
|
|
/** Public interface **/
|
|
|
|
|
|
|
|
/* Return a buffer pointer if at least size bytes are available and if so,
|
|
|
|
* give the actual free space */
|
|
|
|
unsigned char * pcm_output_get_buffer(ssize_t *size)
|
2007-12-29 19:46:35 +00:00
|
|
|
{
|
2011-01-07 05:17:28 +00:00
|
|
|
ssize_t sz = *size;
|
|
|
|
ssize_t free = pcm_output_bytes_free() - PCM_HDR_SIZE;
|
|
|
|
|
|
|
|
if (sz >= 0 && free >= sz)
|
|
|
|
{
|
|
|
|
*size = free; /* return actual free space (- header) */
|
|
|
|
return pcmbuf_tail->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Leave *size alone so caller doesn't have to reinit */
|
|
|
|
return NULL;
|
2007-12-29 19:46:35 +00:00
|
|
|
}
|
|
|
|
|
2011-01-07 05:17:28 +00:00
|
|
|
/* Commit the buffer returned by pcm_ouput_get_buffer; timestamp is PCM
|
|
|
|
* clock time units, not video format time units */
|
|
|
|
bool pcm_output_commit_data(ssize_t size, uint32_t timestamp)
|
2007-12-29 19:46:35 +00:00
|
|
|
{
|
2011-01-07 05:17:28 +00:00
|
|
|
if (size <= 0 || (size & 3))
|
|
|
|
return false; /* invalid */
|
|
|
|
|
|
|
|
size += PCM_HDR_SIZE;
|
|
|
|
|
|
|
|
if (size > pcm_output_bytes_free())
|
|
|
|
return false; /* too big */
|
|
|
|
|
|
|
|
pcmbuf_tail->size = size;
|
|
|
|
pcmbuf_tail->time = timestamp;
|
|
|
|
|
2007-12-29 19:46:35 +00:00
|
|
|
pcm_advance_buffer(&pcmbuf_tail, size);
|
|
|
|
pcmbuf_written += size;
|
2011-01-07 05:17:28 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns 'true' if the buffer is completely empty */
|
|
|
|
bool pcm_output_empty(void)
|
|
|
|
{
|
|
|
|
return pcm_output_bytes_used() <= 0;
|
2007-12-29 19:46:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Flushes the buffer - clock keeps counting */
|
|
|
|
void pcm_output_flush(void)
|
|
|
|
{
|
|
|
|
rb->pcm_play_lock();
|
|
|
|
|
2011-07-08 22:31:15 +00:00
|
|
|
enum channel_status status = rb->mixer_channel_status(MPEG_PCM_CHANNEL);
|
2008-01-07 14:58:23 +00:00
|
|
|
|
|
|
|
/* Stop PCM to clear current buffer */
|
2011-07-08 22:31:15 +00:00
|
|
|
if (status != CHANNEL_STOPPED)
|
|
|
|
rb->mixer_channel_stop(MPEG_PCM_CHANNEL);
|
|
|
|
|
|
|
|
rb->pcm_play_unlock();
|
2008-01-07 14:58:23 +00:00
|
|
|
|
2011-01-07 05:17:28 +00:00
|
|
|
pcm_reset_buffer();
|
2007-12-29 19:46:35 +00:00
|
|
|
|
2008-01-07 14:58:23 +00:00
|
|
|
/* Restart if playing state was current */
|
2011-07-08 22:31:15 +00:00
|
|
|
if (status == CHANNEL_PLAYING)
|
|
|
|
rb->mixer_channel_play_data(MPEG_PCM_CHANNEL,
|
|
|
|
get_more, NULL, 0);
|
2007-12-29 19:46:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Seek the reference clock to the specified time - next audio data ready to
|
|
|
|
go to DMA should be on the buffer with the same time index or else the PCM
|
|
|
|
buffer should be empty */
|
|
|
|
void pcm_output_set_clock(uint32_t time)
|
|
|
|
{
|
|
|
|
rb->pcm_play_lock();
|
|
|
|
|
|
|
|
clock_start = time;
|
2011-01-07 05:17:28 +00:00
|
|
|
clock_tick = time;
|
|
|
|
clock_time = time;
|
2007-12-29 19:46:35 +00:00
|
|
|
|
|
|
|
rb->pcm_play_unlock();
|
|
|
|
}
|
|
|
|
|
2011-01-07 05:17:28 +00:00
|
|
|
/* Return the clock as synchronized by audio frame timestamps */
|
2007-12-29 19:46:35 +00:00
|
|
|
uint32_t pcm_output_get_clock(void)
|
|
|
|
{
|
2011-01-07 05:17:28 +00:00
|
|
|
uint32_t time, rem;
|
|
|
|
|
|
|
|
/* Reread if data race detected - rem will be 0 if driver hasn't yet
|
|
|
|
* updated to the new buffer size. Also be sure pcm state doesn't
|
|
|
|
* cause indefinite loop.
|
|
|
|
*
|
|
|
|
* FYI: NOT scrutinized for rd/wr reordering on different cores. */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
time = clock_time;
|
2011-07-08 22:31:15 +00:00
|
|
|
rem = rb->mixer_channel_get_bytes_waiting(MPEG_PCM_CHANNEL) >> 2;
|
2011-01-07 05:17:28 +00:00
|
|
|
}
|
|
|
|
while (UNLIKELY(time != clock_time ||
|
2011-07-08 22:31:15 +00:00
|
|
|
(rem == 0 &&
|
|
|
|
rb->mixer_channel_status(MPEG_PCM_CHANNEL) == CHANNEL_PLAYING))
|
|
|
|
);
|
2011-01-07 05:17:28 +00:00
|
|
|
|
|
|
|
return time - rem;
|
|
|
|
|
2007-12-29 19:46:35 +00:00
|
|
|
}
|
|
|
|
|
2011-01-07 05:17:28 +00:00
|
|
|
/* Return the raw clock as counted from the last pcm_output_set_clock
|
|
|
|
* call */
|
2007-12-29 19:46:35 +00:00
|
|
|
uint32_t pcm_output_get_ticks(uint32_t *start)
|
|
|
|
{
|
2011-01-07 05:17:28 +00:00
|
|
|
uint32_t tick, rem;
|
|
|
|
|
|
|
|
/* Same procedure as pcm_output_get_clock */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
tick = clock_tick;
|
2011-07-08 22:31:15 +00:00
|
|
|
rem = rb->mixer_channel_get_bytes_waiting(MPEG_PCM_CHANNEL) >> 2;
|
2011-01-07 05:17:28 +00:00
|
|
|
}
|
|
|
|
while (UNLIKELY(tick != clock_tick ||
|
2011-07-08 22:31:15 +00:00
|
|
|
(rem == 0 &&
|
|
|
|
rb->mixer_channel_status(MPEG_PCM_CHANNEL) == CHANNEL_PLAYING))
|
|
|
|
);
|
2011-01-07 05:17:28 +00:00
|
|
|
|
2007-12-29 19:46:35 +00:00
|
|
|
if (start)
|
|
|
|
*start = clock_start;
|
|
|
|
|
2011-01-07 05:17:28 +00:00
|
|
|
return tick - rem;
|
2007-12-29 19:46:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Pauses/Starts pcm playback - and the clock */
|
|
|
|
void pcm_output_play_pause(bool play)
|
|
|
|
{
|
|
|
|
rb->pcm_play_lock();
|
|
|
|
|
2011-07-08 22:31:15 +00:00
|
|
|
if (rb->mixer_channel_status(MPEG_PCM_CHANNEL) != CHANNEL_STOPPED)
|
2007-12-29 19:46:35 +00:00
|
|
|
{
|
2011-07-08 22:31:15 +00:00
|
|
|
rb->mixer_channel_play_pause(MPEG_PCM_CHANNEL, play);
|
|
|
|
rb->pcm_play_unlock();
|
2007-12-29 19:46:35 +00:00
|
|
|
}
|
2011-07-08 22:31:15 +00:00
|
|
|
else
|
2007-12-29 19:46:35 +00:00
|
|
|
{
|
2011-07-08 22:31:15 +00:00
|
|
|
rb->pcm_play_unlock();
|
2007-12-29 19:46:35 +00:00
|
|
|
|
2011-07-08 22:31:15 +00:00
|
|
|
if (play)
|
|
|
|
{
|
|
|
|
rb->mixer_channel_set_amplitude(MPEG_PCM_CHANNEL, MIX_AMP_UNITY);
|
|
|
|
rb->mixer_channel_play_data(MPEG_PCM_CHANNEL,
|
|
|
|
get_more, NULL, 0);
|
|
|
|
}
|
|
|
|
}
|
2007-12-29 19:46:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Stops all playback and resets the clock */
|
|
|
|
void pcm_output_stop(void)
|
|
|
|
{
|
|
|
|
rb->pcm_play_lock();
|
|
|
|
|
2011-07-08 22:31:15 +00:00
|
|
|
if (rb->mixer_channel_status(MPEG_PCM_CHANNEL) != CHANNEL_STOPPED)
|
|
|
|
rb->mixer_channel_stop(MPEG_PCM_CHANNEL);
|
|
|
|
|
|
|
|
rb->pcm_play_unlock();
|
2007-12-29 19:46:35 +00:00
|
|
|
|
|
|
|
pcm_output_flush();
|
|
|
|
pcm_output_set_clock(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Drains any data if the start threshold hasn't been reached */
|
|
|
|
void pcm_output_drain(void)
|
|
|
|
{
|
|
|
|
rb->pcm_play_lock();
|
|
|
|
pcmbuf_threshold = PCMOUT_LOW_WM;
|
|
|
|
rb->pcm_play_unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool pcm_output_init(void)
|
|
|
|
{
|
|
|
|
pcm_buffer = mpeg_malloc(PCMOUT_ALLOC_SIZE, MPEG_ALLOC_PCMOUT);
|
|
|
|
if (pcm_buffer == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
pcmbuf_end = SKIPBYTES(pcm_buffer, PCMOUT_BUFSIZE);
|
|
|
|
|
2011-01-07 05:17:28 +00:00
|
|
|
pcm_reset_buffer();
|
|
|
|
|
2007-12-29 19:46:35 +00:00
|
|
|
#if INPUT_SRC_CAPS != 0
|
|
|
|
/* Select playback */
|
|
|
|
rb->audio_set_input_source(AUDIO_SRC_PLAYBACK, SRCF_PLAYBACK);
|
|
|
|
rb->audio_set_output_source(AUDIO_SRC_PLAYBACK);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if SILENCE_TEST_TONE
|
|
|
|
/* Make the silence clip a square wave */
|
2008-02-01 02:25:15 +00:00
|
|
|
const int16_t silence_amp = INT16_MAX / 16;
|
2007-12-29 19:46:35 +00:00
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAYLEN(silence); i += 2)
|
|
|
|
{
|
|
|
|
if (i < ARRAYLEN(silence)/2)
|
|
|
|
{
|
|
|
|
silence[i] = silence_amp;
|
|
|
|
silence[i+1] = silence_amp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
silence[i] = -silence_amp;
|
|
|
|
silence[i+1] = -silence_amp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pcm_output_exit(void)
|
|
|
|
{
|
|
|
|
}
|