adpcm.c File Reference

#include "avcodec.h"
#include "bitstream.h"
#include "bytestream.h"

Go to the source code of this file.

Data Structures

struct  ADPCMChannelStatus
struct  ADPCMContext

Defines

#define BLKSIZE   1024
#define DK3_GET_NEXT_NIBBLE()
#define ADPCM_ENCODER(id, name, long_name_)
#define ADPCM_DECODER(id, name, long_name_)
#define ADPCM_CODEC(id, name, long_name_)   ADPCM_ENCODER(id,name,long_name_) ADPCM_DECODER(id,name,long_name_)

Functions

static av_cold int adpcm_decode_init (AVCodecContext *avctx)
static short adpcm_ima_expand_nibble (ADPCMChannelStatus *c, char nibble, int shift)
static short adpcm_ms_expand_nibble (ADPCMChannelStatus *c, char nibble)
static short adpcm_ct_expand_nibble (ADPCMChannelStatus *c, char nibble)
static short adpcm_sbpro_expand_nibble (ADPCMChannelStatus *c, char nibble, int size, int shift)
static short adpcm_yamaha_expand_nibble (ADPCMChannelStatus *c, unsigned char nibble)
static void xa_decode (short *out, const unsigned char *in, ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
static int adpcm_decode_frame (AVCodecContext *avctx, void *data, int *data_size, const uint8_t *buf, int buf_size)
 ADPCM_DECODER (CODEC_ID_ADPCM_4XM, adpcm_4xm,"4X Movie ADPCM")
 ADPCM_DECODER (CODEC_ID_ADPCM_CT, adpcm_ct,"Creative Technology ADPCM")
 ADPCM_DECODER (CODEC_ID_ADPCM_EA, adpcm_ea,"Electronic Arts ADPCM")
 ADPCM_DECODER (CODEC_ID_ADPCM_EA_MAXIS_XA, adpcm_ea_maxis_xa,"Electronic Arts Maxis CDROM XA ADPCM")
 ADPCM_DECODER (CODEC_ID_ADPCM_EA_R1, adpcm_ea_r1,"Electronic Arts R1 ADPCM")
 ADPCM_DECODER (CODEC_ID_ADPCM_EA_R2, adpcm_ea_r2,"Electronic Arts R2 ADPCM")
 ADPCM_DECODER (CODEC_ID_ADPCM_EA_R3, adpcm_ea_r3,"Electronic Arts R3 ADPCM")
 ADPCM_DECODER (CODEC_ID_ADPCM_EA_XAS, adpcm_ea_xas,"Electronic Arts XAS ADPCM")
 ADPCM_DECODER (CODEC_ID_ADPCM_IMA_AMV, adpcm_ima_amv,"IMA AMV ADPCM")
 ADPCM_DECODER (CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3,"IMA Duck DK3 ADPCM")
 ADPCM_DECODER (CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4,"IMA Duck DK4 ADPCM")
 ADPCM_DECODER (CODEC_ID_ADPCM_IMA_EA_EACS, adpcm_ima_ea_eacs,"IMA Electronic Arts EACS ADPCM")
 ADPCM_DECODER (CODEC_ID_ADPCM_IMA_EA_SEAD, adpcm_ima_ea_sead,"IMA Electronic Arts SEAD ADPCM")
 ADPCM_CODEC (CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt,"IMA QuickTime ADPCM")
 ADPCM_DECODER (CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg,"IMA Loki SDL MJPEG ADPCM")
 ADPCM_CODEC (CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav,"IMA Wav ADPCM")
 ADPCM_DECODER (CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws,"IMA Westwood ADPCM")
 ADPCM_CODEC (CODEC_ID_ADPCM_MS, adpcm_ms,"Microsoft ADPCM")
 ADPCM_DECODER (CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2,"Sound Blaster Pro 2-bit ADPCM")
 ADPCM_DECODER (CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3,"Sound Blaster Pro 2.6-bit ADPCM")
 ADPCM_DECODER (CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4,"Sound Blaster Pro 4-bit ADPCM")
 ADPCM_CODEC (CODEC_ID_ADPCM_SWF, adpcm_swf,"Shockwave Flash ADPCM")
 ADPCM_DECODER (CODEC_ID_ADPCM_THP, adpcm_thp,"Nintendo Gamecube THP ADPCM")
 ADPCM_DECODER (CODEC_ID_ADPCM_XA, adpcm_xa,"CDROM XA ADPCM")
 ADPCM_CODEC (CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha,"Yamaha ADPCM")

Variables

static const int index_table [16]
static const int step_table [89]
static const int AdaptationTable []
static const uint8_t AdaptCoeff1 []
static const int8_t AdaptCoeff2 []
static const int xa_adpcm_table [5][2]
static const int ea_adpcm_table []
static const int ct_adpcm_table [8]
static const int swf_index_tables [4][16]
static const int yamaha_indexscale []
static const int yamaha_difflookup []


Detailed Description

ADPCM codecs. First version by Francois Revol (revol@free.fr) Fringe ADPCM codecs (e.g., DK3, DK4, Westwood) by Mike Melanson (melanson@pcisys.net) CD-ROM XA ADPCM codec by BERO EA ADPCM decoder by Robin Kay (komadori@myrealbox.com) EA ADPCM R1/R2/R3 decoder by Peter Ross (pross@xvid.org) EA IMA EACS decoder by Peter Ross (pross@xvid.org) EA IMA SEAD decoder by Peter Ross (pross@xvid.org) EA ADPCM XAS decoder by Peter Ross (pross@xvid.org) MAXIS EA ADPCM decoder by Robert Marston (rmarston@gmail.com) THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)

Features and limitations:

Reference documents: http://www.pcisys.net/~melanson/codecs/simpleaudio.html http://www.geocities.com/SiliconValley/8682/aud3.txt http://openquicktime.sourceforge.net/plugins.htm XAnim sources (xa_codec.c) http://www.rasnaimaging.com/people/lapus/download.html http://www.cs.ucla.edu/~leec/mediabench/applications.html SoX source code http://home.sprynet.com/~cbagwell/sox.html

CD-ROM XA: http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html readstr http://www.geocities.co.jp/Playtown/2004/

Definition in file adpcm.c.


Define Documentation

#define ADPCM_CODEC ( id,
name,
long_name_   )     ADPCM_ENCODER(id,name,long_name_) ADPCM_DECODER(id,name,long_name_)

Definition at line 1625 of file adpcm.c.

#define ADPCM_DECODER ( id,
name,
long_name_   ) 

Definition at line 1622 of file adpcm.c.

#define ADPCM_ENCODER ( id,
name,
long_name_   ) 

Definition at line 1605 of file adpcm.c.

#define BLKSIZE   1024

Definition at line 56 of file adpcm.c.

 
#define DK3_GET_NEXT_NIBBLE (  ) 

Value:

if (decode_top_nibble_next) \
    { \
        nibble = last_byte >> 4; \
        decode_top_nibble_next = 0; \
    } \
    else \
    { \
        last_byte = *src++; \
        if (src >= buf + buf_size) break; \
        nibble = last_byte & 0x0F; \
        decode_top_nibble_next = 1; \
    }

Definition at line 868 of file adpcm.c.


Function Documentation

ADPCM_CODEC ( CODEC_ID_ADPCM_YAMAHA  ,
adpcm_yamaha  ,
"Yamaha ADPCM"   
)

ADPCM_CODEC ( CODEC_ID_ADPCM_SWF  ,
adpcm_swf  ,
"Shockwave Flash ADPCM"   
)

ADPCM_CODEC ( CODEC_ID_ADPCM_MS  ,
adpcm_ms  ,
"Microsoft ADPCM"   
)

ADPCM_CODEC ( CODEC_ID_ADPCM_IMA_WAV  ,
adpcm_ima_wav  ,
"IMA Wav ADPCM"   
)

ADPCM_CODEC ( CODEC_ID_ADPCM_IMA_QT  ,
adpcm_ima_qt  ,
"IMA QuickTime ADPCM"   
)

static short adpcm_ct_expand_nibble ( ADPCMChannelStatus c,
char  nibble 
) [inline, static]

Definition at line 746 of file adpcm.c.

References av_clip(), av_clip_int16(), c, and ct_adpcm_table.

00747 {
00748     int sign, delta, diff;
00749     int new_step;
00750 
00751     sign = nibble & 8;
00752     delta = nibble & 7;
00753     /* perform direct multiplication instead of series of jumps proposed by
00754      * the reference ADPCM implementation since modern CPUs can do the mults
00755      * quickly enough */
00756     diff = ((2 * delta + 1) * c->step) >> 3;
00757     /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
00758     c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
00759     c->predictor = av_clip_int16(c->predictor);
00760     /* calculate new step and clamp it to range 511..32767 */
00761     new_step = (ct_adpcm_table[nibble & 7] * c->step) >> 8;
00762     c->step = av_clip(new_step, 511, 32767);
00763 
00764     return (short)c->predictor;
00765 }

static int adpcm_decode_frame ( AVCodecContext avctx,
void *  data,
int *  data_size,
const uint8_t *  buf,
int  buf_size 
) [static]

Definition at line 882 of file adpcm.c.

References adpcm_ima_expand_nibble(), av_clip_int16(), av_log(), AV_LOG_ERROR, AVCodecContext::block_align, c, AVCodecContext::channels, AVCodecContext::codec, CODEC_ID_ADPCM_IMA_QT, CODEC_ID_ADPCM_IMA_WAV, AVCodec::id, ADPCMChannelStatus::predictor, AVCodecContext::priv_data, shift(), src, ADPCMChannelStatus::step, ADPCMChannelStatus::step_index, and step_table.

00885 {
00886     ADPCMContext *c = avctx->priv_data;
00887     ADPCMChannelStatus *cs;
00888     int n, m, channel, i;
00889     int block_predictor[2];
00890     short *samples;
00891     short *samples_end;
00892     const uint8_t *src;
00893     int st; /* stereo */
00894 
00895     /* DK3 ADPCM accounting variables */
00896     unsigned char last_byte = 0;
00897     unsigned char nibble;
00898     int decode_top_nibble_next = 0;
00899     int diff_channel;
00900 
00901     /* EA ADPCM state variables */
00902     uint32_t samples_in_chunk;
00903     int32_t previous_left_sample, previous_right_sample;
00904     int32_t current_left_sample, current_right_sample;
00905     int32_t next_left_sample, next_right_sample;
00906     int32_t coeff1l, coeff2l, coeff1r, coeff2r;
00907     uint8_t shift_left, shift_right;
00908     int count1, count2;
00909     int coeff[2][2], shift[2];//used in EA MAXIS ADPCM
00910 
00911     if (!buf_size)
00912         return 0;
00913 
00914     //should protect all 4bit ADPCM variants
00915     //8 is needed for CODEC_ID_ADPCM_IMA_WAV with 2 channels
00916     //
00917     if(*data_size/4 < buf_size + 8)
00918         return -1;
00919 
00920     samples = data;
00921     samples_end= samples + *data_size/2;
00922     *data_size= 0;
00923     src = buf;
00924 
00925     st = avctx->channels == 2 ? 1 : 0;
00926 
00927     switch(avctx->codec->id) {
00928     case CODEC_ID_ADPCM_IMA_QT:
00929         n = buf_size - 2*avctx->channels;
00930         for (channel = 0; channel < avctx->channels; channel++) {
00931             cs = &(c->status[channel]);
00932             /* (pppppp) (piiiiiii) */
00933 
00934             /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
00935             cs->predictor = (*src++) << 8;
00936             cs->predictor |= (*src & 0x80);
00937             cs->predictor &= 0xFF80;
00938 
00939             /* sign extension */
00940             if(cs->predictor & 0x8000)
00941                 cs->predictor -= 0x10000;
00942 
00943             cs->predictor = av_clip_int16(cs->predictor);
00944 
00945             cs->step_index = (*src++) & 0x7F;
00946 
00947             if (cs->step_index > 88){
00948                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
00949                 cs->step_index = 88;
00950             }
00951 
00952             cs->step = step_table[cs->step_index];
00953 
00954             samples = (short*)data + channel;
00955 
00956             for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
00957                 *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F, 3);
00958                 samples += avctx->channels;
00959                 *samples = adpcm_ima_expand_nibble(cs, src[0] >> 4  , 3);
00960                 samples += avctx->channels;
00961                 src ++;
00962             }
00963         }
00964         if (st)
00965             samples--;
00966         break;
00967     case CODEC_ID_ADPCM_IMA_WAV:
00968         if (avctx->block_align != 0 && buf_size > avctx->block_align)
00969             buf_size = avctx->block_align;
00970 
00971 //        samples_per_block= (block_align-4*chanels)*8 / (bits_per_sample * chanels) + 1;
00972 
00973         for(i=0; i<avctx->channels; i++){
00974             cs = &(c->status[i]);
00975             cs->predictor = *samples++ = (int16_t)bytestream_get_le16(&src);
00976 
00977             cs->step_index = *src++;
00978             if (cs->step_index > 88){
00979                 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
00980                 cs->step_index = 88;
00981             }
00982             if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null but is %d!!\n", src[-1]); /* unused */
00983         }
00984 
00985         while(src < buf + buf_size){
00986             for(m=0; m<4; m++){
00987                 for(i=0; i<=st; i++)
00988                     *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] & 0x0F, 3);
00989                 for(i=0; i<=st; i++)
00990                     *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] >> 4  , 3);
00991                 src++;
00992             }
00993             src += 4*st;
00994         }
00995         break;
00996     case CODEC_ID_ADPCM_4XM:
00997         cs = &(c->status[0]);
00998         c->status[0].predictor= (int16_t)bytestream_get_le16(&src);
00999         if(st){
01000             c->status[1].predictor= (int16_t)bytestream_get_le16(&src);
01001         }
01002         c->status[0].step_index= (int16_t)bytestream_get_le16(&src);
01003         if(st){
01004             c->status[1].step_index= (int16_t)bytestream_get_le16(&src);
01005         }
01006         if (cs->step_index < 0) cs->step_index = 0;
01007         if (cs->step_index > 88) cs->step_index = 88;
01008 
01009         m= (buf_size - (src - buf))>>st;
01010         for(i=0; i<m; i++) {
01011             *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4);
01012             if (st)
01013                 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4);
01014             *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4);
01015             if (st)
01016                 *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4);
01017         }
01018 
01019         src += m<<st;
01020 
01021         break;
01022     case CODEC_ID_ADPCM_MS:
01023         if (avctx->block_align != 0 && buf_size > avctx->block_align)
01024             buf_size = avctx->block_align;
01025         n = buf_size - 7 * avctx->channels;
01026         if (n < 0)
01027             return -1;
01028         block_predictor[0] = av_clip(*src++, 0, 6);
01029         block_predictor[1] = 0;
01030         if (st)
01031             block_predictor[1] = av_clip(*src++, 0, 6);
01032         c->status[0].idelta = (int16_t)bytestream_get_le16(&src);
01033         if (st){
01034             c->status[1].idelta = (int16_t)bytestream_get_le16(&src);
01035         }
01036         c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
01037         c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
01038         c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
01039         c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
01040 
01041         c->status[0].sample1 = bytestream_get_le16(&src);
01042         if (st) c->status[1].sample1 = bytestream_get_le16(&src);
01043         c->status[0].sample2 = bytestream_get_le16(&src);
01044         if (st) c->status[1].sample2 = bytestream_get_le16(&src);
01045 
01046         *samples++ = c->status[0].sample2;
01047         if (st) *samples++ = c->status[1].sample2;
01048         *samples++ = c->status[0].sample1;
01049         if (st) *samples++ = c->status[1].sample1;
01050         for(;n>0;n--) {
01051             *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], src[0] >> 4  );
01052             *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
01053             src ++;
01054         }
01055         break;
01056     case CODEC_ID_ADPCM_IMA_DK4:
01057         if (avctx->block_align != 0 && buf_size > avctx->block_align)
01058             buf_size = avctx->block_align;
01059 
01060         c->status[0].predictor  = (int16_t)bytestream_get_le16(&src);
01061         c->status[0].step_index = *src++;
01062         src++;
01063         *samples++ = c->status[0].predictor;
01064         if (st) {
01065             c->status[1].predictor  = (int16_t)bytestream_get_le16(&src);
01066             c->status[1].step_index = *src++;
01067             src++;
01068             *samples++ = c->status[1].predictor;
01069         }
01070         while (src < buf + buf_size) {
01071 
01072             /* take care of the top nibble (always left or mono channel) */
01073             *samples++ = adpcm_ima_expand_nibble(&c->status[0],
01074                 src[0] >> 4, 3);
01075 
01076             /* take care of the bottom nibble, which is right sample for
01077              * stereo, or another mono sample */
01078             if (st)
01079                 *samples++ = adpcm_ima_expand_nibble(&c->status[1],
01080                     src[0] & 0x0F, 3);
01081             else
01082                 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
01083                     src[0] & 0x0F, 3);
01084 
01085             src++;
01086         }
01087         break;
01088     case CODEC_ID_ADPCM_IMA_DK3:
01089         if (avctx->block_align != 0 && buf_size > avctx->block_align)
01090             buf_size = avctx->block_align;
01091 
01092         if(buf_size + 16 > (samples_end - samples)*3/8)
01093             return -1;
01094 
01095         c->status[0].predictor  = (int16_t)AV_RL16(src + 10);
01096         c->status[1].predictor  = (int16_t)AV_RL16(src + 12);
01097         c->status[0].step_index = src[14];
01098         c->status[1].step_index = src[15];
01099         /* sign extend the predictors */
01100         src += 16;
01101         diff_channel = c->status[1].predictor;
01102 
01103         /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
01104          * the buffer is consumed */
01105         while (1) {
01106 
01107             /* for this algorithm, c->status[0] is the sum channel and
01108              * c->status[1] is the diff channel */
01109 
01110             /* process the first predictor of the sum channel */
01111             DK3_GET_NEXT_NIBBLE();
01112             adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
01113 
01114             /* process the diff channel predictor */
01115             DK3_GET_NEXT_NIBBLE();
01116             adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
01117 
01118             /* process the first pair of stereo PCM samples */
01119             diff_channel = (diff_channel + c->status[1].predictor) / 2;
01120             *samples++ = c->status[0].predictor + c->status[1].predictor;
01121             *samples++ = c->status[0].predictor - c->status[1].predictor;
01122 
01123             /* process the second predictor of the sum channel */
01124             DK3_GET_NEXT_NIBBLE();
01125             adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
01126 
01127             /* process the second pair of stereo PCM samples */
01128             diff_channel = (diff_channel + c->status[1].predictor) / 2;
01129             *samples++ = c->status[0].predictor + c->status[1].predictor;
01130             *samples++ = c->status[0].predictor - c->status[1].predictor;
01131         }
01132         break;
01133     case CODEC_ID_ADPCM_IMA_WS:
01134         /* no per-block initialization; just start decoding the data */
01135         while (src < buf + buf_size) {
01136 
01137             if (st) {
01138                 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
01139                     src[0] >> 4  , 3);
01140                 *samples++ = adpcm_ima_expand_nibble(&c->status[1],
01141                     src[0] & 0x0F, 3);
01142             } else {
01143                 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
01144                     src[0] >> 4  , 3);
01145                 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
01146                     src[0] & 0x0F, 3);
01147             }
01148 
01149             src++;
01150         }
01151         break;
01152     case CODEC_ID_ADPCM_XA:
01153         while (buf_size >= 128) {
01154             xa_decode(samples, src, &c->status[0], &c->status[1],
01155                 avctx->channels);
01156             src += 128;
01157             samples += 28 * 8;
01158             buf_size -= 128;
01159         }
01160         break;
01161     case CODEC_ID_ADPCM_IMA_EA_EACS:
01162         samples_in_chunk = bytestream_get_le32(&src) >> (1-st);
01163 
01164         if (samples_in_chunk > buf_size-4-(8<<st)) {
01165             src += buf_size - 4;
01166             break;
01167         }
01168 
01169         for (i=0; i<=st; i++)
01170             c->status[i].step_index = bytestream_get_le32(&src);
01171         for (i=0; i<=st; i++)
01172             c->status[i].predictor  = bytestream_get_le32(&src);
01173 
01174         for (; samples_in_chunk; samples_in_chunk--, src++) {
01175             *samples++ = adpcm_ima_expand_nibble(&c->status[0],  *src>>4,   3);
01176             *samples++ = adpcm_ima_expand_nibble(&c->status[st], *src&0x0F, 3);
01177         }
01178         break;
01179     case CODEC_ID_ADPCM_IMA_EA_SEAD:
01180         for (; src < buf+buf_size; src++) {
01181             *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] >> 4, 6);
01182             *samples++ = adpcm_ima_expand_nibble(&c->status[st],src[0]&0x0F, 6);
01183         }
01184         break;
01185     case CODEC_ID_ADPCM_EA:
01186         samples_in_chunk = AV_RL32(src);
01187         if (samples_in_chunk >= ((buf_size - 12) * 2)) {
01188             src += buf_size;
01189             break;
01190         }
01191         src += 4;
01192         current_left_sample   = (int16_t)bytestream_get_le16(&src);
01193         previous_left_sample  = (int16_t)bytestream_get_le16(&src);
01194         current_right_sample  = (int16_t)bytestream_get_le16(&src);
01195         previous_right_sample = (int16_t)bytestream_get_le16(&src);
01196 
01197         for (count1 = 0; count1 < samples_in_chunk/28;count1++) {
01198             coeff1l = ea_adpcm_table[ *src >> 4       ];
01199             coeff2l = ea_adpcm_table[(*src >> 4  ) + 4];
01200             coeff1r = ea_adpcm_table[*src & 0x0F];
01201             coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
01202             src++;
01203 
01204             shift_left  = (*src >> 4  ) + 8;
01205             shift_right = (*src & 0x0F) + 8;
01206             src++;
01207 
01208             for (count2 = 0; count2 < 28; count2++) {
01209                 next_left_sample  = (int32_t)((*src & 0xF0) << 24) >> shift_left;
01210                 next_right_sample = (int32_t)((*src & 0x0F) << 28) >> shift_right;
01211                 src++;
01212 
01213                 next_left_sample = (next_left_sample +
01214                     (current_left_sample * coeff1l) +
01215                     (previous_left_sample * coeff2l) + 0x80) >> 8;
01216                 next_right_sample = (next_right_sample +
01217                     (current_right_sample * coeff1r) +
01218                     (previous_right_sample * coeff2r) + 0x80) >> 8;
01219 
01220                 previous_left_sample = current_left_sample;
01221                 current_left_sample = av_clip_int16(next_left_sample);
01222                 previous_right_sample = current_right_sample;
01223                 current_right_sample = av_clip_int16(next_right_sample);
01224                 *samples++ = (unsigned short)current_left_sample;
01225                 *samples++ = (unsigned short)current_right_sample;
01226             }
01227         }
01228         break;
01229     case CODEC_ID_ADPCM_EA_MAXIS_XA:
01230         for(channel = 0; channel < avctx->channels; channel++) {
01231             for (i=0; i<2; i++)
01232                 coeff[channel][i] = ea_adpcm_table[(*src >> 4) + 4*i];
01233             shift[channel] = (*src & 0x0F) + 8;
01234             src++;
01235         }
01236         for (count1 = 0; count1 < (buf_size - avctx->channels) / avctx->channels; count1++) {
01237             for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
01238                 for(channel = 0; channel < avctx->channels; channel++) {
01239                     int32_t sample = (int32_t)(((*(src+channel) >> i) & 0x0F) << 0x1C) >> shift[channel];
01240                     sample = (sample +
01241                              c->status[channel].sample1 * coeff[channel][0] +
01242                              c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
01243                     c->status[channel].sample2 = c->status[channel].sample1;
01244                     c->status[channel].sample1 = av_clip_int16(sample);
01245                     *samples++ = c->status[channel].sample1;
01246                 }
01247             }
01248             src+=avctx->channels;
01249         }
01250         break;
01251     case CODEC_ID_ADPCM_EA_R1:
01252     case CODEC_ID_ADPCM_EA_R2:
01253     case CODEC_ID_ADPCM_EA_R3: {
01254         /* channel numbering
01255            2chan: 0=fl, 1=fr
01256            4chan: 0=fl, 1=rl, 2=fr, 3=rr
01257            6chan: 0=fl, 1=c,  2=fr, 3=rl,  4=rr, 5=sub */
01258         const int big_endian = avctx->codec->id == CODEC_ID_ADPCM_EA_R3;
01259         int32_t previous_sample, current_sample, next_sample;
01260         int32_t coeff1, coeff2;
01261         uint8_t shift;
01262         unsigned int channel;
01263         uint16_t *samplesC;
01264         const uint8_t *srcC;
01265 
01266         samples_in_chunk = (big_endian ? bytestream_get_be32(&src)
01267                                        : bytestream_get_le32(&src)) / 28;
01268         if (samples_in_chunk > UINT32_MAX/(28*avctx->channels) ||
01269             28*samples_in_chunk*avctx->channels > samples_end-samples) {
01270             src += buf_size - 4;
01271             break;
01272         }
01273 
01274         for (channel=0; channel<avctx->channels; channel++) {
01275             srcC = src + (big_endian ? bytestream_get_be32(&src)
01276                                      : bytestream_get_le32(&src))
01277                        + (avctx->channels-channel-1) * 4;
01278             samplesC = samples + channel;
01279 
01280             if (avctx->codec->id == CODEC_ID_ADPCM_EA_R1) {
01281                 current_sample  = (int16_t)bytestream_get_le16(&srcC);
01282                 previous_sample = (int16_t)bytestream_get_le16(&srcC);
01283             } else {
01284                 current_sample  = c->status[channel].predictor;
01285                 previous_sample = c->status[channel].prev_sample;
01286             }
01287 
01288             for (count1=0; count1<samples_in_chunk; count1++) {
01289                 if (*srcC == 0xEE) {  /* only seen in R2 and R3 */
01290                     srcC++;
01291                     current_sample  = (int16_t)bytestream_get_be16(&srcC);
01292                     previous_sample = (int16_t)bytestream_get_be16(&srcC);
01293 
01294                     for (count2=0; count2<28; count2++) {
01295                         *samplesC = (int16_t)bytestream_get_be16(&srcC);
01296                         samplesC += avctx->channels;
01297                     }
01298                 } else {
01299                     coeff1 = ea_adpcm_table[ *srcC>>4     ];
01300                     coeff2 = ea_adpcm_table[(*srcC>>4) + 4];
01301                     shift = (*srcC++ & 0x0F) + 8;
01302 
01303                     for (count2=0; count2<28; count2++) {
01304                         if (count2 & 1)
01305                             next_sample = (int32_t)((*srcC++ & 0x0F) << 28) >> shift;
01306                         else
01307                             next_sample = (int32_t)((*srcC   & 0xF0) << 24) >> shift;
01308 
01309                         next_sample += (current_sample  * coeff1) +
01310                                        (previous_sample * coeff2);
01311                         next_sample = av_clip_int16(next_sample >> 8);
01312 
01313                         previous_sample = current_sample;
01314                         current_sample  = next_sample;
01315                         *samplesC = current_sample;
01316                         samplesC += avctx->channels;
01317                     }
01318                 }
01319             }
01320 
01321             if (avctx->codec->id != CODEC_ID_ADPCM_EA_R1) {
01322                 c->status[channel].predictor   = current_sample;
01323                 c->status[channel].prev_sample = previous_sample;
01324             }
01325         }
01326 
01327         src = src + buf_size - (4 + 4*avctx->channels);
01328         samples += 28 * samples_in_chunk * avctx->channels;
01329         break;
01330     }
01331     case CODEC_ID_ADPCM_EA_XAS:
01332         if (samples_end-samples < 32*4*avctx->channels
01333             || buf_size < (4+15)*4*avctx->channels) {
01334             src += buf_size;
01335             break;
01336         }
01337         for (channel=0; channel<avctx->channels; channel++) {
01338             int coeff[2][4], shift[4];
01339             short *s2, *s = &samples[channel];
01340             for (n=0; n<4; n++, s+=32*avctx->channels) {
01341                 for (i=0; i<2; i++)
01342                     coeff[i][n] = ea_adpcm_table[(src[0]&0x0F)+4*i];
01343                 shift[n] = (src[2]&0x0F) + 8;
01344                 for (s2=s, i=0; i<2; i++, src+=2, s2+=avctx->channels)
01345                     s2[0] = (src[0]&0xF0) + (src[1]<<8);
01346             }
01347 
01348             for (m=2; m<32; m+=2) {
01349                 s = &samples[m*avctx->channels + channel];
01350                 for (n=0; n<4; n++, src++, s+=32*avctx->channels) {
01351                     for (s2=s, i=0; i<8; i+=4, s2+=avctx->channels) {
01352                         int level = (int32_t)((*src & (0xF0>>i)) << (24+i)) >> shift[n];
01353                         int pred  = s2[-1*avctx->channels] * coeff[0][n]
01354                                   + s2[-2*avctx->channels] * coeff[1][n];
01355                         s2[0] = av_clip_int16((level + pred + 0x80) >> 8);
01356                     }
01357                 }
01358             }
01359         }
01360         samples += 32*4*avctx->channels;
01361         break;
01362     case CODEC_ID_ADPCM_IMA_AMV:
01363     case CODEC_ID_ADPCM_IMA_SMJPEG:
01364         c->status[0].predictor = (int16_t)bytestream_get_le16(&src);
01365         c->status[0].step_index = bytestream_get_le16(&src);
01366 
01367         if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
01368             src+=4;
01369 
01370         while (src < buf + buf_size) {
01371             char hi, lo;
01372             lo = *src & 0x0F;
01373             hi = *src >> 4;
01374 
01375             if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
01376                 FFSWAP(char, hi, lo);
01377 
01378             *samples++ = adpcm_ima_expand_nibble(&c->status[0],
01379                 lo, 3);
01380             *samples++ = adpcm_ima_expand_nibble(&c->status[0],
01381                 hi, 3);
01382             src++;
01383         }
01384         break;
01385     case CODEC_ID_ADPCM_CT:
01386         while (src < buf + buf_size) {
01387             if (st) {
01388                 *samples++ = adpcm_ct_expand_nibble(&c->status[0],
01389                     src[0] >> 4);
01390                 *samples++ = adpcm_ct_expand_nibble(&c->status[1],
01391                     src[0] & 0x0F);
01392             } else {
01393                 *samples++ = adpcm_ct_expand_nibble(&c->status[0],
01394                     src[0] >> 4);
01395                 *samples++ = adpcm_ct_expand_nibble(&c->status[0],
01396                     src[0] & 0x0F);
01397             }
01398             src++;
01399         }
01400         break;
01401     case CODEC_ID_ADPCM_SBPRO_4:
01402     case CODEC_ID_ADPCM_SBPRO_3:
01403     case CODEC_ID_ADPCM_SBPRO_2:
01404         if (!c->status[0].step_index) {
01405             /* the first byte is a raw sample */
01406             *samples++ = 128 * (*src++ - 0x80);
01407             if (st)
01408               *samples++ = 128 * (*src++ - 0x80);
01409             c->status[0].step_index = 1;
01410         }
01411         if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_4) {
01412             while (src < buf + buf_size) {
01413                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
01414                     src[0] >> 4, 4, 0);
01415                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
01416                     src[0] & 0x0F, 4, 0);
01417                 src++;
01418             }
01419         } else if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_3) {
01420             while (src < buf + buf_size && samples + 2 < samples_end) {
01421                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
01422                      src[0] >> 5        , 3, 0);
01423                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
01424                     (src[0] >> 2) & 0x07, 3, 0);
01425                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
01426                     src[0] & 0x03, 2, 0);
01427                 src++;
01428             }
01429         } else {
01430             while (src < buf + buf_size && samples + 3 < samples_end) {
01431                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
01432                      src[0] >> 6        , 2, 2);
01433                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
01434                     (src[0] >> 4) & 0x03, 2, 2);
01435                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
01436                     (src[0] >> 2) & 0x03, 2, 2);
01437                 *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
01438                     src[0] & 0x03, 2, 2);
01439                 src++;
01440             }
01441         }
01442         break;
01443     case CODEC_ID_ADPCM_SWF:
01444     {
01445         GetBitContext gb;
01446         const int *table;
01447         int k0, signmask, nb_bits, count;
01448         int size = buf_size*8;
01449 
01450         init_get_bits(&gb, buf, size);
01451 
01452         //read bits & initial values
01453         nb_bits = get_bits(&gb, 2)+2;
01454         //av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", nb_bits);
01455         table = swf_index_tables[nb_bits-2];
01456         k0 = 1 << (nb_bits-2);
01457         signmask = 1 << (nb_bits-1);
01458 
01459         while (get_bits_count(&gb) <= size - 22*avctx->channels) {
01460             for (i = 0; i < avctx->channels; i++) {
01461                 *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
01462                 c->status[i].step_index = get_bits(&gb, 6);
01463             }
01464 
01465             for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
01466                 int i;
01467 
01468                 for (i = 0; i < avctx->channels; i++) {
01469                     // similar to IMA adpcm
01470                     int delta = get_bits(&gb, nb_bits);
01471                     int step = step_table[c->status[i].step_index];
01472                     long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
01473                     int k = k0;
01474 
01475                     do {
01476                         if (delta & k)
01477                             vpdiff += step;
01478                         step >>= 1;
01479                         k >>= 1;
01480                     } while(k);
01481                     vpdiff += step;
01482 
01483                     if (delta & signmask)
01484                         c->status[i].predictor -= vpdiff;
01485                     else
01486                         c->status[i].predictor += vpdiff;
01487 
01488                     c->status[i].step_index += table[delta & (~signmask)];
01489 
01490                     c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
01491                     c->status[i].predictor = av_clip_int16(c->status[i].predictor);
01492 
01493                     *samples++ = c->status[i].predictor;
01494                     if (samples >= samples_end) {
01495                         av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
01496                         return -1;
01497                     }
01498                 }
01499             }
01500         }
01501         src += buf_size;
01502         break;
01503     }
01504     case CODEC_ID_ADPCM_YAMAHA:
01505         while (src < buf + buf_size) {
01506             if (st) {
01507                 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
01508                         src[0] & 0x0F);
01509                 *samples++ = adpcm_yamaha_expand_nibble(&c->status[1],
01510                         src[0] >> 4  );
01511             } else {
01512                 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
01513                         src[0] & 0x0F);
01514                 *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
01515                         src[0] >> 4  );
01516             }
01517             src++;
01518         }
01519         break;
01520     case CODEC_ID_ADPCM_THP:
01521     {
01522         int table[2][16];
01523         unsigned int samplecnt;
01524         int prev[2][2];
01525         int ch;
01526 
01527         if (buf_size < 80) {
01528             av_log(avctx, AV_LOG_ERROR, "frame too small\n");
01529             return -1;
01530         }
01531 
01532         src+=4;
01533         samplecnt = bytestream_get_be32(&src);
01534 
01535         for (i = 0; i < 32; i++)
01536             table[0][i] = (int16_t)bytestream_get_be16(&src);
01537 
01538         /* Initialize the previous sample.  */
01539         for (i = 0; i < 4; i++)
01540             prev[0][i] = (int16_t)bytestream_get_be16(&src);
01541 
01542         if (samplecnt >= (samples_end - samples) /  (st + 1)) {
01543             av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
01544             return -1;
01545         }
01546 
01547         for (ch = 0; ch <= st; ch++) {
01548             samples = (unsigned short *) data + ch;
01549 
01550             /* Read in every sample for this channel.  */
01551             for (i = 0; i < samplecnt / 14; i++) {
01552                 int index = (*src >> 4) & 7;
01553                 unsigned int exp = 28 - (*src++ & 15);
01554                 int factor1 = table[ch][index * 2];
01555                 int factor2 = table[ch][index * 2 + 1];
01556 
01557                 /* Decode 14 samples.  */
01558                 for (n = 0; n < 14; n++) {
01559                     int32_t sampledat;
01560                     if(n&1) sampledat=  *src++    <<28;
01561                     else    sampledat= (*src&0xF0)<<24;
01562 
01563                     sampledat = ((prev[ch][0]*factor1
01564                                 + prev[ch][1]*factor2) >> 11) + (sampledat>>exp);
01565                     *samples = av_clip_int16(sampledat);
01566                     prev[ch][1] = prev[ch][0];
01567                     prev[ch][0] = *samples++;
01568 
01569                     /* In case of stereo, skip one sample, this sample
01570                        is for the other channel.  */
01571                     samples += st;
01572                 }
01573             }
01574         }
01575 
01576         /* In the previous loop, in case stereo is used, samples is
01577            increased exactly one time too often.  */
01578         samples -= st;
01579         break;
01580     }
01581 
01582     default:
01583         return -1;
01584     }
01585     *data_size = (uint8_t *)samples - (uint8_t *)data;
01586     return src - buf;
01587 }

static av_cold int adpcm_decode_init ( AVCodecContext avctx  )  [static]

Definition at line 672 of file adpcm.c.

References AV_RL32, c, AVCodecContext::channels, AVCodecContext::codec, CODEC_ID_ADPCM_CT, CODEC_ID_ADPCM_EA_R1, CODEC_ID_ADPCM_EA_R2, CODEC_ID_ADPCM_EA_R3, CODEC_ID_ADPCM_IMA_WS, AVCodecContext::extradata, AVCodecContext::extradata_size, AVCodec::id, and AVCodecContext::priv_data.

00673 {
00674     ADPCMContext *c = avctx->priv_data;
00675     unsigned int max_channels = 2;
00676 
00677     switch(avctx->codec->id) {
00678     case CODEC_ID_ADPCM_EA_R1:
00679     case CODEC_ID_ADPCM_EA_R2:
00680     case CODEC_ID_ADPCM_EA_R3:
00681         max_channels = 6;
00682         break;
00683     }
00684     if(avctx->channels > max_channels){
00685         return -1;
00686     }
00687 
00688     switch(avctx->codec->id) {
00689     case CODEC_ID_ADPCM_CT:
00690         c->status[0].step = c->status[1].step = 511;
00691         break;
00692     case CODEC_ID_ADPCM_IMA_WS:
00693         if (avctx->extradata && avctx->extradata_size == 2 * 4) {
00694             c->status[0].predictor = AV_RL32(avctx->extradata);
00695             c->status[1].predictor = AV_RL32(avctx->extradata + 4);
00696         }
00697         break;
00698     default:
00699         break;
00700     }
00701     return 0;
00702 }

ADPCM_DECODER ( CODEC_ID_ADPCM_XA  ,
adpcm_xa  ,
"CDROM XA ADPCM"   
)

ADPCM_DECODER ( CODEC_ID_ADPCM_THP  ,
adpcm_thp  ,
"Nintendo Gamecube THP ADPCM"   
)

ADPCM_DECODER ( CODEC_ID_ADPCM_SBPRO_4  ,
adpcm_sbpro_4  ,
"Sound Blaster Pro 4-bit ADPCM"   
)