2005-06-11 10:08:17 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Dave Chapman
|
|
|
|
*
|
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.
|
2005-06-11 10:08:17 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2005-10-13 11:32:52 +00:00
|
|
|
#include "codeclib.h"
|
2005-06-11 10:08:17 +00:00
|
|
|
#include <inttypes.h> /* Needed by a52.h */
|
|
|
|
#include <codecs/liba52/config-a52.h>
|
|
|
|
#include <codecs/liba52/a52.h>
|
|
|
|
|
2006-01-18 00:05:14 +00:00
|
|
|
CODEC_HEADER
|
|
|
|
|
2005-06-11 10:08:17 +00:00
|
|
|
#define BUFFER_SIZE 4096
|
|
|
|
|
2005-10-22 09:10:51 +00:00
|
|
|
#define A52_SAMPLESPERFRAME (6*256)
|
|
|
|
|
2005-10-10 23:37:30 +00:00
|
|
|
static a52_state_t *state;
|
2010-07-25 22:24:53 +00:00
|
|
|
static unsigned long samplesdone;
|
|
|
|
static unsigned long frequency;
|
2005-06-11 10:08:17 +00:00
|
|
|
|
2005-10-10 23:37:30 +00:00
|
|
|
/* used outside liba52 */
|
2005-10-27 11:32:02 +00:00
|
|
|
static uint8_t buf[3840] IBSS_ATTR;
|
2018-12-16 00:27:20 +00:00
|
|
|
static uint8_t *bufptr = buf;
|
|
|
|
static uint8_t *bufpos = buf + 7;
|
|
|
|
|
|
|
|
static void a52_decoder_reset(void)
|
|
|
|
{
|
|
|
|
bufptr = buf;
|
|
|
|
bufpos = buf + 7;
|
|
|
|
}
|
2005-06-11 10:08:17 +00:00
|
|
|
|
2007-02-07 00:51:50 +00:00
|
|
|
static inline void output_audio(sample_t *samples)
|
2005-06-11 10:08:17 +00:00
|
|
|
{
|
2007-02-07 01:30:05 +00:00
|
|
|
ci->yield();
|
2007-02-07 00:51:50 +00:00
|
|
|
ci->pcmbuf_insert(&samples[0], &samples[256], 256);
|
2005-06-11 10:08:17 +00:00
|
|
|
}
|
|
|
|
|
2018-12-16 00:27:20 +00:00
|
|
|
static size_t a52_decode_data(uint8_t *start, uint8_t *end)
|
2005-06-11 10:08:17 +00:00
|
|
|
{
|
2005-10-10 23:37:30 +00:00
|
|
|
/*
|
|
|
|
* sample_rate and flags are static because this routine could
|
|
|
|
* exit between the a52_syncinfo() and the ao_setup(), and we want
|
|
|
|
* to have the same values when we get back !
|
|
|
|
*/
|
|
|
|
static int sample_rate;
|
|
|
|
static int flags;
|
|
|
|
int bit_rate;
|
|
|
|
int len;
|
2018-12-16 00:27:20 +00:00
|
|
|
size_t consumed = 0;
|
2005-10-10 23:37:30 +00:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
len = end - start;
|
|
|
|
if (!len)
|
|
|
|
break;
|
|
|
|
if (len > bufpos - bufptr)
|
|
|
|
len = bufpos - bufptr;
|
|
|
|
memcpy(bufptr, start, len);
|
|
|
|
bufptr += len;
|
|
|
|
start += len;
|
2018-12-16 00:27:20 +00:00
|
|
|
consumed += len;
|
2005-10-10 23:37:30 +00:00
|
|
|
if (bufptr == bufpos) {
|
|
|
|
if (bufpos == buf + 7) {
|
|
|
|
int length;
|
|
|
|
|
|
|
|
length = a52_syncinfo(buf, &flags, &sample_rate, &bit_rate);
|
|
|
|
if (!length) {
|
|
|
|
//DEBUGF("skip\n");
|
|
|
|
for (bufptr = buf; bufptr < buf + 6; bufptr++)
|
|
|
|
bufptr[0] = bufptr[1];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
bufpos = buf + length;
|
|
|
|
} else {
|
2006-02-13 19:51:53 +00:00
|
|
|
/* Unity gain is 1 << 26, and we want to end up on 28 bits
|
|
|
|
of precision instead of the default 30.
|
|
|
|
*/
|
|
|
|
level_t level = 1 << 24;
|
2005-10-10 23:37:30 +00:00
|
|
|
sample_t bias = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* This is the configuration for the downmixing: */
|
2005-10-22 11:37:16 +00:00
|
|
|
flags = A52_STEREO | A52_ADJUST_LEVEL;
|
2005-10-10 23:37:30 +00:00
|
|
|
|
|
|
|
if (a52_frame(state, buf, &flags, &level, bias))
|
|
|
|
goto error;
|
2005-10-11 23:06:32 +00:00
|
|
|
a52_dynrng(state, NULL, NULL);
|
2005-10-10 23:37:30 +00:00
|
|
|
frequency = sample_rate;
|
|
|
|
|
|
|
|
/* An A52 frame consists of 6 blocks of 256 samples
|
|
|
|
So we decode and output them one block at a time */
|
|
|
|
for (i = 0; i < 6; i++) {
|
|
|
|
if (a52_block(state))
|
|
|
|
goto error;
|
2005-10-22 11:37:16 +00:00
|
|
|
output_audio(a52_samples(state));
|
2005-10-10 23:37:30 +00:00
|
|
|
samplesdone += 256;
|
|
|
|
}
|
|
|
|
ci->set_elapsed(samplesdone/(frequency/1000));
|
|
|
|
bufptr = buf;
|
|
|
|
bufpos = buf + 7;
|
2018-12-16 00:27:20 +00:00
|
|
|
break;
|
2005-10-10 23:37:30 +00:00
|
|
|
error:
|
|
|
|
//logf("Error decoding A52 stream\n");
|
|
|
|
bufptr = buf;
|
|
|
|
bufpos = buf + 7;
|
|
|
|
}
|
|
|
|
}
|
2005-06-11 10:08:17 +00:00
|
|
|
}
|
2018-12-16 00:27:20 +00:00
|
|
|
return consumed;
|
2005-06-11 10:08:17 +00:00
|
|
|
}
|
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
/* this is the codec entry point */
|
2011-04-27 03:08:23 +00:00
|
|
|
enum codec_status codec_main(enum codec_entry_call_reason reason)
|
|
|
|
{
|
|
|
|
if (reason == CODEC_LOAD) {
|
|
|
|
/* Generic codec initialisation */
|
|
|
|
ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
|
|
|
|
ci->configure(DSP_SET_SAMPLE_DEPTH, 28);
|
|
|
|
}
|
|
|
|
else if (reason == CODEC_UNLOAD) {
|
|
|
|
if (state)
|
|
|
|
a52_free(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
return CODEC_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this is called for each file to process */
|
|
|
|
enum codec_status codec_run(void)
|
2005-06-11 10:08:17 +00:00
|
|
|
{
|
2018-12-16 00:27:20 +00:00
|
|
|
size_t n, consumed = 0, rest = 0;
|
2005-10-10 23:37:30 +00:00
|
|
|
unsigned char *filebuf;
|
2005-10-22 09:10:51 +00:00
|
|
|
int sample_loc;
|
2011-04-27 03:08:23 +00:00
|
|
|
intptr_t param;
|
2005-10-10 23:37:30 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
if (codec_init())
|
|
|
|
return CODEC_ERROR;
|
2007-02-26 17:15:04 +00:00
|
|
|
|
2013-05-05 04:48:40 +00:00
|
|
|
ci->configure(DSP_SET_FREQUENCY, ci->id3->frequency);
|
2007-02-26 17:15:04 +00:00
|
|
|
codec_set_replaygain(ci->id3);
|
2005-06-26 19:41:29 +00:00
|
|
|
|
2018-12-16 00:27:20 +00:00
|
|
|
/* Initialise the A52 decoder and check for success */
|
2005-10-10 23:37:30 +00:00
|
|
|
state = a52_init(0);
|
2005-06-11 10:08:17 +00:00
|
|
|
|
2012-02-07 17:20:49 +00:00
|
|
|
samplesdone = 0;
|
|
|
|
|
2006-07-26 19:08:16 +00:00
|
|
|
if (ci->id3->offset) {
|
2013-07-14 11:59:39 +00:00
|
|
|
sample_loc = (ci->id3->offset / ci->id3->bytesperframe) *
|
|
|
|
A52_SAMPLESPERFRAME;
|
|
|
|
param = ci->id3->offset;
|
|
|
|
}
|
|
|
|
else if (ci->id3->elapsed) {
|
|
|
|
sample_loc = ci->id3->elapsed/1000 * ci->id3->frequency;
|
|
|
|
param = sample_loc/A52_SAMPLESPERFRAME*ci->id3->bytesperframe;
|
2006-07-26 19:08:16 +00:00
|
|
|
}
|
|
|
|
else {
|
2013-07-14 11:59:39 +00:00
|
|
|
sample_loc = 0;
|
|
|
|
param = ci->id3->first_frame_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ci->seek_buffer(param)) {
|
|
|
|
samplesdone = sample_loc;
|
2006-07-26 19:08:16 +00:00
|
|
|
}
|
|
|
|
|
2013-07-14 11:59:39 +00:00
|
|
|
ci->set_elapsed(samplesdone/(ci->id3->frequency/1000));
|
|
|
|
|
|
|
|
/* The main decoding loop */
|
|
|
|
|
2005-10-10 23:37:30 +00:00
|
|
|
while (1) {
|
2017-12-07 18:21:57 +00:00
|
|
|
long action = ci->get_command(¶m);
|
2011-04-27 03:08:23 +00:00
|
|
|
|
|
|
|
if (action == CODEC_ACTION_HALT)
|
2005-10-10 23:37:30 +00:00
|
|
|
break;
|
2005-06-11 10:08:17 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
if (action == CODEC_ACTION_SEEK_TIME) {
|
|
|
|
sample_loc = param/1000 * ci->id3->frequency;
|
2005-10-22 09:10:51 +00:00
|
|
|
|
2013-07-14 11:59:39 +00:00
|
|
|
if (ci->seek_buffer((sample_loc/A52_SAMPLESPERFRAME)*
|
|
|
|
ci->id3->bytesperframe)) {
|
2005-10-22 09:10:51 +00:00
|
|
|
samplesdone = sample_loc;
|
|
|
|
ci->set_elapsed(samplesdone/(ci->id3->frequency/1000));
|
|
|
|
}
|
2005-11-02 00:09:42 +00:00
|
|
|
ci->seek_complete();
|
2018-12-16 00:27:20 +00:00
|
|
|
a52_decoder_reset();
|
2005-10-22 09:10:51 +00:00
|
|
|
}
|
|
|
|
|
2005-10-10 23:37:30 +00:00
|
|
|
filebuf = ci->request_buffer(&n, BUFFER_SIZE);
|
2005-06-11 10:08:17 +00:00
|
|
|
|
2005-10-10 23:37:30 +00:00
|
|
|
if (n == 0) /* End of Stream */
|
2018-12-16 00:27:20 +00:00
|
|
|
{
|
|
|
|
filebuf += consumed;
|
|
|
|
for (n = ci->curpos + consumed; rest; rest -= consumed, n += consumed)
|
|
|
|
{
|
|
|
|
ci->set_offset(n);
|
|
|
|
consumed = a52_decode_data(filebuf, filebuf + rest);
|
|
|
|
}
|
2005-10-10 23:37:30 +00:00
|
|
|
break;
|
2018-12-16 00:27:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
consumed = a52_decode_data(filebuf, filebuf + n);
|
|
|
|
rest = n - consumed;
|
|
|
|
ci->advance_buffer(consumed);
|
2005-10-10 23:37:30 +00:00
|
|
|
}
|
2006-04-22 14:40:13 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_OK;
|
2005-06-11 10:08:17 +00:00
|
|
|
}
|