2010-01-27 17:25:10 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Dave Chapman
|
|
|
|
* Copyright (C) 2009 Yoshihisa Uchida
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include "codeclib.h"
|
2010-02-20 02:04:56 +00:00
|
|
|
#include "ima_adpcm_common.h"
|
2010-02-24 19:02:14 +00:00
|
|
|
#include "support_formats.h"
|
2010-01-27 17:25:10 +00:00
|
|
|
|
|
|
|
/*
|
2010-02-20 02:04:56 +00:00
|
|
|
* Intel DVI ADPCM (IMA ADPCM)
|
|
|
|
*
|
|
|
|
* References
|
|
|
|
* [1] The IMA Digital Audio Focus and Technical Working Groups,
|
|
|
|
* Recommended Practices for Enhancing Digital Audio Compatibility
|
|
|
|
* in Multimedia Systems Revision 3.00, 1992
|
|
|
|
* [2] Microsoft Corporation, New Multimedia Data Types and Data Techniques,
|
|
|
|
* Revision:3.0, 1994
|
|
|
|
* [3] ffmpeg source code, libavcodec/adpcm.c
|
2010-01-27 17:25:10 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
static struct pcm_format *fmt;
|
|
|
|
|
2010-02-20 02:04:56 +00:00
|
|
|
static bool set_format(struct pcm_format *format)
|
2010-01-27 17:25:10 +00:00
|
|
|
{
|
|
|
|
fmt = format;
|
|
|
|
|
2010-02-20 02:04:56 +00:00
|
|
|
if (fmt->bitspersample < 2 || fmt->bitspersample > 5)
|
2010-01-27 17:25:10 +00:00
|
|
|
{
|
2010-02-20 02:04:56 +00:00
|
|
|
DEBUGF("CODEC_ERROR: dvi adpcm must be 2, 3, 4 or 5 bitspersample: %d\n",
|
|
|
|
fmt->bitspersample);
|
2010-01-27 17:25:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-02-20 02:04:56 +00:00
|
|
|
fmt->chunksize = fmt->blockalign;
|
2010-01-27 17:25:10 +00:00
|
|
|
|
2010-02-20 02:04:56 +00:00
|
|
|
init_ima_adpcm_decoder(fmt->bitspersample, NULL);
|
2010-01-27 17:25:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-03-22 10:02:05 +00:00
|
|
|
static struct pcm_pos *get_seek_pos(uint32_t seek_val, int seek_mode,
|
2010-02-20 02:04:56 +00:00
|
|
|
uint8_t *(*read_buffer)(size_t *realsize))
|
2010-01-27 17:25:10 +00:00
|
|
|
{
|
2010-02-20 02:04:56 +00:00
|
|
|
static struct pcm_pos newpos;
|
2010-03-22 10:02:05 +00:00
|
|
|
uint32_t newblock = (seek_mode == PCM_SEEK_TIME) ?
|
|
|
|
((uint64_t)seek_val * ci->id3->frequency / 1000LL)
|
|
|
|
/ fmt->samplesperblock :
|
|
|
|
seek_val / fmt->blockalign;
|
2010-01-27 17:25:10 +00:00
|
|
|
|
2010-02-20 02:04:56 +00:00
|
|
|
(void)read_buffer;
|
|
|
|
newpos.pos = newblock * fmt->blockalign;
|
|
|
|
newpos.samples = newblock * fmt->samplesperblock;
|
|
|
|
return &newpos;
|
2010-01-27 17:25:10 +00:00
|
|
|
}
|
|
|
|
|
2010-03-07 07:27:45 +00:00
|
|
|
static inline void decode_2bit(const uint8_t **inbuf, size_t inbufsize,
|
2010-02-20 02:04:56 +00:00
|
|
|
int32_t **outbuf, int *outbufcount)
|
2010-01-27 17:25:10 +00:00
|
|
|
{
|
2010-02-20 02:04:56 +00:00
|
|
|
int ch;
|
2010-01-27 17:25:10 +00:00
|
|
|
int i;
|
2010-02-20 02:04:56 +00:00
|
|
|
int32_t *pcmbuf;
|
|
|
|
int samples;
|
2010-01-27 17:25:10 +00:00
|
|
|
|
2010-03-07 07:27:45 +00:00
|
|
|
samples = inbufsize / (4 * fmt->channels) - 1;
|
2010-02-20 02:04:56 +00:00
|
|
|
*outbufcount += (samples << 4);
|
|
|
|
while (samples-- > 0)
|
|
|
|
{
|
|
|
|
for (ch = 0; ch < fmt->channels; ch++)
|
|
|
|
{
|
|
|
|
pcmbuf = *outbuf + ch;
|
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
{
|
2010-03-06 05:51:24 +00:00
|
|
|
*pcmbuf = create_pcmdata(ch, **inbuf ) << IMA_ADPCM_INC_DEPTH;
|
2010-02-20 02:04:56 +00:00
|
|
|
pcmbuf += fmt->channels;
|
2010-03-06 05:51:24 +00:00
|
|
|
*pcmbuf = create_pcmdata(ch, **inbuf >> 2) << IMA_ADPCM_INC_DEPTH;
|
2010-02-20 02:04:56 +00:00
|
|
|
pcmbuf += fmt->channels;
|
2010-03-06 05:51:24 +00:00
|
|
|
*pcmbuf = create_pcmdata(ch, **inbuf >> 4) << IMA_ADPCM_INC_DEPTH;
|
2010-02-20 02:04:56 +00:00
|
|
|
pcmbuf += fmt->channels;
|
2010-03-06 05:51:24 +00:00
|
|
|
*pcmbuf = create_pcmdata(ch, **inbuf >> 6) << IMA_ADPCM_INC_DEPTH;
|
2010-02-20 02:04:56 +00:00
|
|
|
pcmbuf += fmt->channels;
|
|
|
|
(*inbuf)++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*outbuf += 16 * fmt->channels;
|
2010-01-27 17:25:10 +00:00
|
|
|
}
|
2010-02-20 02:04:56 +00:00
|
|
|
}
|
2010-01-27 17:25:10 +00:00
|
|
|
|
2010-03-07 07:27:45 +00:00
|
|
|
static inline void decode_3bit(const uint8_t **inbuf, size_t inbufsize,
|
2010-02-20 02:04:56 +00:00
|
|
|
int32_t **outbuf, int *outbufcount)
|
|
|
|
{
|
|
|
|
const uint8_t *adpcmbuf;
|
|
|
|
uint32_t adpcms;
|
|
|
|
int ch;
|
|
|
|
int i;
|
|
|
|
int32_t *pcmbuf;
|
|
|
|
int samples;
|
2010-01-27 17:25:10 +00:00
|
|
|
|
2010-03-07 07:27:45 +00:00
|
|
|
samples = (inbufsize - 4 * fmt->channels) / (12 * fmt->channels);
|
2010-02-20 02:04:56 +00:00
|
|
|
*outbufcount += (samples << 5);
|
|
|
|
while (samples--)
|
|
|
|
{
|
|
|
|
for (ch = 0; ch < fmt->channels; ch++)
|
2010-01-27 17:25:10 +00:00
|
|
|
{
|
2010-02-20 02:04:56 +00:00
|
|
|
adpcmbuf = *inbuf + ch * 4;
|
|
|
|
pcmbuf = *outbuf + ch;
|
|
|
|
adpcms = *adpcmbuf++;
|
|
|
|
adpcms |= (*adpcmbuf++) << 8;
|
|
|
|
adpcms |= (*adpcmbuf++) << 16;
|
|
|
|
for (i = 0; i < 8; i++)
|
2010-01-27 17:25:10 +00:00
|
|
|
{
|
2010-03-06 05:51:24 +00:00
|
|
|
*pcmbuf = create_pcmdata(ch, adpcms >> (3 * i)) << IMA_ADPCM_INC_DEPTH;
|
2010-02-20 02:04:56 +00:00
|
|
|
pcmbuf += fmt->channels;
|
2010-01-27 17:25:10 +00:00
|
|
|
}
|
2010-02-20 02:04:56 +00:00
|
|
|
adpcms = *adpcmbuf++;
|
|
|
|
adpcmbuf += (fmt->channels - 1) * 4;
|
|
|
|
adpcms |= (*adpcmbuf++) << 8;
|
|
|
|
adpcms |= (*adpcmbuf++) << 16;
|
2010-01-27 17:25:10 +00:00
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
{
|
2010-03-06 05:51:24 +00:00
|
|
|
*pcmbuf = create_pcmdata(ch, adpcms >> (3 * i)) << IMA_ADPCM_INC_DEPTH;
|
2010-02-20 02:04:56 +00:00
|
|
|
pcmbuf += fmt->channels;
|
2010-01-27 17:25:10 +00:00
|
|
|
}
|
2010-02-20 02:04:56 +00:00
|
|
|
adpcms = *adpcmbuf++;
|
|
|
|
adpcms |= (*adpcmbuf++) << 8;
|
|
|
|
adpcmbuf += (fmt->channels - 1) * 4;
|
|
|
|
adpcms |= (*adpcmbuf++) << 16;
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
{
|
2010-03-06 05:51:24 +00:00
|
|
|
*pcmbuf = create_pcmdata(ch, adpcms >> (3 * i)) << IMA_ADPCM_INC_DEPTH;
|
2010-02-20 02:04:56 +00:00
|
|
|
pcmbuf += fmt->channels;
|
2010-01-27 17:25:10 +00:00
|
|
|
}
|
2010-02-20 02:04:56 +00:00
|
|
|
adpcms = *adpcmbuf++;
|
|
|
|
adpcms |= (*adpcmbuf++) << 8;
|
|
|
|
adpcms |= (*adpcmbuf++) << 16;
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
{
|
2010-03-06 05:51:24 +00:00
|
|
|
*pcmbuf = create_pcmdata(ch, adpcms >> (3 * i)) << IMA_ADPCM_INC_DEPTH;
|
2010-02-20 02:04:56 +00:00
|
|
|
pcmbuf += fmt->channels;
|
2010-01-27 17:25:10 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-20 02:04:56 +00:00
|
|
|
*outbuf += 32 * fmt->channels;
|
|
|
|
*inbuf += 12 * fmt->channels;
|
2010-01-27 17:25:10 +00:00
|
|
|
}
|
2010-02-20 02:04:56 +00:00
|
|
|
}
|
2010-01-27 17:25:10 +00:00
|
|
|
|
2010-03-07 07:27:45 +00:00
|
|
|
static inline void decode_4bit(const uint8_t **inbuf, size_t inbufsize,
|
2010-02-20 02:04:56 +00:00
|
|
|
int32_t **outbuf, int *outbufcount)
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
int i;
|
|
|
|
int32_t *pcmbuf;
|
|
|
|
int samples;
|
|
|
|
|
2010-03-07 07:27:45 +00:00
|
|
|
samples = inbufsize / (4 * fmt->channels) - 1;
|
2010-02-20 02:04:56 +00:00
|
|
|
*outbufcount += (samples << 3);
|
|
|
|
while (samples-- > 0)
|
|
|
|
{
|
|
|
|
for (ch = 0; ch < fmt->channels; ch++)
|
|
|
|
{
|
|
|
|
pcmbuf = *outbuf + ch;
|
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
{
|
2010-03-06 05:51:24 +00:00
|
|
|
*pcmbuf = create_pcmdata_size4(ch, **inbuf ) << IMA_ADPCM_INC_DEPTH;
|
2010-02-20 02:04:56 +00:00
|
|
|
pcmbuf += fmt->channels;
|
2010-03-06 05:51:24 +00:00
|
|
|
*pcmbuf = create_pcmdata_size4(ch, **inbuf >> 4) << IMA_ADPCM_INC_DEPTH;
|
2010-02-20 02:04:56 +00:00
|
|
|
pcmbuf += fmt->channels;
|
|
|
|
(*inbuf)++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*outbuf += 8 * fmt->channels;
|
2010-01-27 17:25:10 +00:00
|
|
|
}
|
2010-02-20 02:04:56 +00:00
|
|
|
}
|
|
|
|
|
2010-03-07 07:27:45 +00:00
|
|
|
static inline void decode_5bit(const uint8_t **inbuf, size_t inbufsize,
|
2010-02-20 02:04:56 +00:00
|
|
|
int32_t **outbuf, int *outbufcount)
|
|
|
|
{
|
|
|
|
const uint8_t *adpcmbuf;
|
|
|
|
uint64_t adpcms;
|
|
|
|
int ch;
|
|
|
|
int i;
|
|
|
|
int32_t *pcmbuf;
|
|
|
|
int samples;
|
|
|
|
|
2010-03-07 07:27:45 +00:00
|
|
|
samples = (inbufsize - 4 * fmt->channels) / (20 * fmt->channels);
|
2010-02-20 02:04:56 +00:00
|
|
|
*outbufcount += (samples << 5);
|
|
|
|
while (samples--)
|
|
|
|
{
|
|
|
|
for (ch = 0; ch < fmt->channels; ch++)
|
|
|
|
{
|
|
|
|
adpcmbuf = *inbuf + ch * 4;
|
|
|
|
pcmbuf = *outbuf + ch;
|
|
|
|
adpcms = *adpcmbuf++;
|
|
|
|
adpcms |= (*adpcmbuf++) << 8;
|
|
|
|
adpcms |= (*adpcmbuf++) << 16;
|
|
|
|
adpcms |= (uint64_t)(*adpcmbuf++) << 24;
|
|
|
|
adpcmbuf += (fmt->channels - 1) * 4;
|
|
|
|
adpcms |= (uint64_t)(*adpcmbuf++) << 32;
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
{
|
2010-03-06 05:51:24 +00:00
|
|
|
*pcmbuf = create_pcmdata(ch, adpcms >> (5 * i)) << IMA_ADPCM_INC_DEPTH;
|
2010-02-20 02:04:56 +00:00
|
|
|
pcmbuf += fmt->channels;
|
|
|
|
}
|
|
|
|
adpcms = *adpcmbuf++;
|
|
|
|
adpcms |= (*adpcmbuf++) << 8;
|
|
|
|
adpcms |= (*adpcmbuf++) << 16;
|
|
|
|
adpcmbuf += (fmt->channels - 1) * 4;
|
|
|
|
adpcms |= (uint64_t)(*adpcmbuf++) << 24;
|
|
|
|
adpcms |= (uint64_t)(*adpcmbuf++) << 32;
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
{
|
2010-03-06 05:51:24 +00:00
|
|
|
*pcmbuf = create_pcmdata(ch, adpcms >> (5 * i)) << IMA_ADPCM_INC_DEPTH;
|
2010-02-20 02:04:56 +00:00
|
|
|
pcmbuf += fmt->channels;
|
|
|
|
}
|
|
|
|
adpcms = *adpcmbuf++;
|
|
|
|
adpcms |= (*adpcmbuf++) << 8;
|
|
|
|
adpcmbuf += (fmt->channels - 1) * 4;
|
|
|
|
adpcms |= (*adpcmbuf++) << 16;
|
|
|
|
adpcms |= (uint64_t)(*adpcmbuf++) << 24;
|
|
|
|
adpcms |= (uint64_t)(*adpcmbuf++) << 32;
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
{
|
2010-03-06 05:51:24 +00:00
|
|
|
*pcmbuf = create_pcmdata(ch, adpcms >> (5 * i)) << IMA_ADPCM_INC_DEPTH;
|
2010-02-20 02:04:56 +00:00
|
|
|
pcmbuf += fmt->channels;
|
|
|
|
}
|
|
|
|
adpcms = *adpcmbuf++;
|
|
|
|
adpcmbuf += (fmt->channels - 1) * 4;
|
|
|
|
adpcms |= (*adpcmbuf++) << 8;
|
|
|
|
adpcms |= (*adpcmbuf++) << 16;
|
|
|
|
adpcms |= (uint64_t)(*adpcmbuf++) << 24;
|
|
|
|
adpcms |= (uint64_t)(*adpcmbuf++) << 32;
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
{
|
2010-03-06 05:51:24 +00:00
|
|
|
*pcmbuf = create_pcmdata(ch, adpcms >> (5 * i)) << IMA_ADPCM_INC_DEPTH;
|
2010-02-20 02:04:56 +00:00
|
|
|
pcmbuf += fmt->channels;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*outbuf += 32 * fmt->channels;
|
|
|
|
*inbuf += 20 * fmt->channels;
|
2010-01-27 17:25:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int decode(const uint8_t *inbuf, size_t inbufsize,
|
2010-02-20 02:04:56 +00:00
|
|
|
int32_t *outbuf, int *outbufcount)
|
2010-01-27 17:25:10 +00:00
|
|
|
{
|
2010-02-20 02:04:56 +00:00
|
|
|
int ch;
|
|
|
|
int32_t init_pcmdata[2];
|
|
|
|
int8_t init_index[2];
|
2010-01-27 17:25:10 +00:00
|
|
|
|
2010-02-20 02:04:56 +00:00
|
|
|
*outbufcount = 0;
|
2010-03-07 07:27:45 +00:00
|
|
|
for (ch = 0; ch < fmt->channels; ch++)
|
2010-01-27 17:25:10 +00:00
|
|
|
{
|
2010-03-07 07:27:45 +00:00
|
|
|
init_pcmdata[ch] = inbuf[0] | (inbuf[1] << 8);
|
|
|
|
if (init_pcmdata[ch] > 32767)
|
|
|
|
init_pcmdata[ch] -= 65536;
|
|
|
|
|
|
|
|
init_index[ch] = inbuf[2];
|
|
|
|
if (init_index[ch] > 88 || init_index[ch] < 0)
|
2010-02-20 02:04:56 +00:00
|
|
|
{
|
2010-03-07 07:27:45 +00:00
|
|
|
DEBUGF("CODEC_ERROR: dvi adpcm illegal step index=%d > 88\n",
|
|
|
|
init_index[ch]);
|
|
|
|
return CODEC_ERROR;
|
|
|
|
}
|
|
|
|
inbuf += 4;
|
2010-02-20 02:04:56 +00:00
|
|
|
|
2010-03-07 07:27:45 +00:00
|
|
|
*outbuf++ = init_pcmdata[ch] << IMA_ADPCM_INC_DEPTH;
|
|
|
|
}
|
2010-02-20 02:04:56 +00:00
|
|
|
|
2010-03-07 07:27:45 +00:00
|
|
|
*outbufcount += 1;
|
|
|
|
set_decode_parameters(fmt->channels, init_pcmdata, init_index);
|
2010-02-20 02:04:56 +00:00
|
|
|
|
2010-03-07 07:27:45 +00:00
|
|
|
if (fmt->bitspersample == 4)
|
|
|
|
decode_4bit(&inbuf, inbufsize, &outbuf, outbufcount);
|
|
|
|
else if (fmt->bitspersample == 3)
|
|
|
|
decode_3bit(&inbuf, inbufsize, &outbuf, outbufcount);
|
|
|
|
else if (fmt->bitspersample == 5)
|
|
|
|
decode_5bit(&inbuf, inbufsize, &outbuf, outbufcount);
|
|
|
|
else /* fmt->bitspersample == 2 */
|
|
|
|
decode_2bit(&inbuf, inbufsize, &outbuf, outbufcount);
|
2010-02-20 02:04:56 +00:00
|
|
|
|
2010-01-27 17:25:10 +00:00
|
|
|
return CODEC_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct pcm_codec codec = {
|
|
|
|
set_format,
|
|
|
|
get_seek_pos,
|
|
|
|
decode,
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct pcm_codec *get_dvi_adpcm_codec(void)
|
|
|
|
{
|
|
|
|
return &codec;
|
|
|
|
}
|