1758 lines · 67.8 KB · C
RawDownload
/* * Copyright (c) 2001-2003 The FFmpeg project * * 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) * * This file is part of FFmpeg. * * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */#include "avcodec.h"#include "get_bits.h"#include "bytestream.h"#include "adpcm.h"#include "adpcm_data.h"#include "internal.h" /** * @file * ADPCM decoders * Features and limitations: * * Reference documents: * http://wiki.multimedia.cx/index.php?title=Category:ADPCM_Audio_Codecs * http://www.pcisys.net/~melanson/codecs/simpleaudio.html [dead] * http://www.geocities.com/SiliconValley/8682/aud3.txt [dead] * http://openquicktime.sourceforge.net/ * XAnim sources (xa_codec.c) http://xanim.polter.net/ * http://www.cs.ucla.edu/~leec/mediabench/applications.html [dead] * SoX source code http://sox.sourceforge.net/ * * CD-ROM XA: * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html [dead] * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html [dead] * readstr http://www.geocities.co.jp/Playtown/2004/ */ /* These are for CD-ROM XA ADPCM */static const int xa_adpcm_table[5][2] = {    {   0,   0 },    {  60,   0 },    { 115, -52 },    {  98, -55 },    { 122, -60 }}; static const int ea_adpcm_table[] = {    0,  240,  460,  392,    0,    0, -208, -220,    0,    1,    3,    4,    7,    8,   10,   11,    0,   -1,   -3,   -4}; // padded to zero where table size is less then 16static const int swf_index_tables[4][16] = {    /*2*/ { -1, 2 },    /*3*/ { -1, -1, 2, 4 },    /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },    /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }}; /* end of tables */ typedef struct ADPCMDecodeContext {    ADPCMChannelStatus status[14];    int vqa_version;                /**< VQA version. Used for ADPCM_IMA_WS */    int has_status;} ADPCMDecodeContext; static av_cold int adpcm_decode_init(AVCodecContext * avctx){    ADPCMDecodeContext *c = avctx->priv_data;    unsigned int min_channels = 1;    unsigned int max_channels = 2;     switch(avctx->codec->id) {    case AV_CODEC_ID_ADPCM_DTK:    case AV_CODEC_ID_ADPCM_EA:        min_channels = 2;        break;    case AV_CODEC_ID_ADPCM_AFC:    case AV_CODEC_ID_ADPCM_EA_R1:    case AV_CODEC_ID_ADPCM_EA_R2:    case AV_CODEC_ID_ADPCM_EA_R3:    case AV_CODEC_ID_ADPCM_EA_XAS:        max_channels = 6;        break;    case AV_CODEC_ID_ADPCM_MTAF:        min_channels = 2;        max_channels = 8;        break;    case AV_CODEC_ID_ADPCM_PSX:        max_channels = 8;        break;    case AV_CODEC_ID_ADPCM_IMA_DAT4:    case AV_CODEC_ID_ADPCM_THP:    case AV_CODEC_ID_ADPCM_THP_LE:        max_channels = 14;        break;    }    if (avctx->channels < min_channels || avctx->channels > max_channels) {        av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");        return AVERROR(EINVAL);    }     switch(avctx->codec->id) {    case AV_CODEC_ID_ADPCM_CT:        c->status[0].step = c->status[1].step = 511;        break;    case AV_CODEC_ID_ADPCM_IMA_WAV:        if (avctx->bits_per_coded_sample < 2 || avctx->bits_per_coded_sample > 5)            return AVERROR_INVALIDDATA;        break;    case AV_CODEC_ID_ADPCM_IMA_APC:        if (avctx->extradata && avctx->extradata_size >= 8) {            c->status[0].predictor = AV_RL32(avctx->extradata);            c->status[1].predictor = AV_RL32(avctx->extradata + 4);        }        break;    case AV_CODEC_ID_ADPCM_IMA_WS:        if (avctx->extradata && avctx->extradata_size >= 2)            c->vqa_version = AV_RL16(avctx->extradata);        break;    default:        break;    }     switch(avctx->codec->id) {        case AV_CODEC_ID_ADPCM_AICA:        case AV_CODEC_ID_ADPCM_IMA_DAT4:        case AV_CODEC_ID_ADPCM_IMA_QT:        case AV_CODEC_ID_ADPCM_IMA_WAV:        case AV_CODEC_ID_ADPCM_4XM:        case AV_CODEC_ID_ADPCM_XA:        case AV_CODEC_ID_ADPCM_EA_R1:        case AV_CODEC_ID_ADPCM_EA_R2:        case AV_CODEC_ID_ADPCM_EA_R3:        case AV_CODEC_ID_ADPCM_EA_XAS:        case AV_CODEC_ID_ADPCM_THP:        case AV_CODEC_ID_ADPCM_THP_LE:        case AV_CODEC_ID_ADPCM_AFC:        case AV_CODEC_ID_ADPCM_DTK:        case AV_CODEC_ID_ADPCM_PSX:        case AV_CODEC_ID_ADPCM_MTAF:            avctx->sample_fmt = AV_SAMPLE_FMT_S16P;            break;        case AV_CODEC_ID_ADPCM_IMA_WS:            avctx->sample_fmt = c->vqa_version == 3 ? AV_SAMPLE_FMT_S16P :                                                      AV_SAMPLE_FMT_S16;            break;        default:            avctx->sample_fmt = AV_SAMPLE_FMT_S16;    }     return 0;} static inline int16_t adpcm_ima_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int shift){    int step_index;    int predictor;    int sign, delta, diff, step;     step = ff_adpcm_step_table[c->step_index];    step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];    step_index = av_clip(step_index, 0, 88);     sign = nibble & 8;    delta = nibble & 7;    /* perform direct multiplication instead of series of jumps proposed by     * the reference ADPCM implementation since modern CPUs can do the mults     * quickly enough */    diff = ((2 * delta + 1) * step) >> shift;    predictor = c->predictor;    if (sign) predictor -= diff;    else predictor += diff;     c->predictor = av_clip_int16(predictor);    c->step_index = step_index;     return (int16_t)c->predictor;} static inline int16_t adpcm_ima_wav_expand_nibble(ADPCMChannelStatus *c, GetBitContext *gb, int bps){    int nibble, step_index, predictor, sign, delta, diff, step, shift;     shift = bps - 1;    nibble = get_bits_le(gb, bps),    step = ff_adpcm_step_table[c->step_index];    step_index = c->step_index + ff_adpcm_index_tables[bps - 2][nibble];    step_index = av_clip(step_index, 0, 88);     sign = nibble & (1 << shift);    delta = av_mod_uintp2(nibble, shift);    diff = ((2 * delta + 1) * step) >> shift;    predictor = c->predictor;    if (sign) predictor -= diff;    else predictor += diff;     c->predictor = av_clip_int16(predictor);    c->step_index = step_index;     return (int16_t)c->predictor;} static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble, int shift){    int step_index;    int predictor;    int diff, step;     step = ff_adpcm_step_table[c->step_index];    step_index = c->step_index + ff_adpcm_index_table[nibble];    step_index = av_clip(step_index, 0, 88);     diff = step >> 3;    if (nibble & 4) diff += step;    if (nibble & 2) diff += step >> 1;    if (nibble & 1) diff += step >> 2;     if (nibble & 8)        predictor = c->predictor - diff;    else        predictor = c->predictor + diff;     c->predictor = av_clip_int16(predictor);    c->step_index = step_index;     return c->predictor;} static inline int16_t adpcm_ms_expand_nibble(ADPCMChannelStatus *c, int nibble){    int predictor;     predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;    predictor += ((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;     c->sample2 = c->sample1;    c->sample1 = av_clip_int16(predictor);    c->idelta = (ff_adpcm_AdaptationTable[(int)nibble] * c->idelta) >> 8;    if (c->idelta < 16) c->idelta = 16;    if (c->idelta > INT_MAX/768) {        av_log(NULL, AV_LOG_WARNING, "idelta overflow\n");        c->idelta = INT_MAX/768;    }     return c->sample1;} static inline int16_t adpcm_ima_oki_expand_nibble(ADPCMChannelStatus *c, int nibble){    int step_index, predictor, sign, delta, diff, step;     step = ff_adpcm_oki_step_table[c->step_index];    step_index = c->step_index + ff_adpcm_index_table[(unsigned)nibble];    step_index = av_clip(step_index, 0, 48);     sign = nibble & 8;    delta = nibble & 7;    diff = ((2 * delta + 1) * step) >> 3;    predictor = c->predictor;    if (sign) predictor -= diff;    else predictor += diff;     c->predictor = av_clip_intp2(predictor, 11);    c->step_index = step_index;     return c->predictor << 4;} static inline int16_t adpcm_ct_expand_nibble(ADPCMChannelStatus *c, int8_t nibble){    int sign, delta, diff;    int new_step;     sign = nibble & 8;    delta = nibble & 7;    /* perform direct multiplication instead of series of jumps proposed by     * the reference ADPCM implementation since modern CPUs can do the mults     * quickly enough */    diff = ((2 * delta + 1) * c->step) >> 3;    /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */    c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);    c->predictor = av_clip_int16(c->predictor);    /* calculate new step and clamp it to range 511..32767 */    new_step = (ff_adpcm_AdaptationTable[nibble & 7] * c->step) >> 8;    c->step = av_clip(new_step, 511, 32767);     return (int16_t)c->predictor;} static inline int16_t adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, int8_t nibble, int size, int shift){    int sign, delta, diff;     sign = nibble & (1<<(size-1));    delta = nibble & ((1<<(size-1))-1);    diff = delta << (7 + c->step + shift);     /* clamp result */    c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);     /* calculate new step */    if (delta >= (2*size - 3) && c->step < 3)        c->step++;    else if (delta == 0 && c->step > 0)        c->step--;     return (int16_t) c->predictor;} static inline int16_t adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble){    if(!c->step) {        c->predictor = 0;        c->step = 127;    }     c->predictor += (c->step * ff_adpcm_yamaha_difflookup[nibble]) / 8;    c->predictor = av_clip_int16(c->predictor);    c->step = (c->step * ff_adpcm_yamaha_indexscale[nibble]) >> 8;    c->step = av_clip(c->step, 127, 24576);    return c->predictor;} static inline int16_t adpcm_mtaf_expand_nibble(ADPCMChannelStatus *c, uint8_t nibble){    c->predictor += ff_adpcm_mtaf_stepsize[c->step][nibble];    c->predictor = av_clip_int16(c->predictor);    c->step += ff_adpcm_index_table[nibble];    c->step = av_clip_uintp2(c->step, 5);    return c->predictor;} static int xa_decode(AVCodecContext *avctx, int16_t *out0, int16_t *out1,                     const uint8_t *in, ADPCMChannelStatus *left,                     ADPCMChannelStatus *right, int channels, int sample_offset){    int i, j;    int shift,filter,f0,f1;    int s_1,s_2;    int d,s,t;     out0 += sample_offset;    if (channels == 1)        out1 = out0 + 28;    else        out1 += sample_offset;     for(i=0;i<4;i++) {        shift  = 12 - (in[4+i*2] & 15);        filter = in[4+i*2] >> 4;        if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {            avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);            filter=0;        }        f0 = xa_adpcm_table[filter][0];        f1 = xa_adpcm_table[filter][1];         s_1 = left->sample1;        s_2 = left->sample2;         for(j=0;j<28;j++) {            d = in[16+i+j*4];             t = sign_extend(d, 4);            s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);            s_2 = s_1;            s_1 = av_clip_int16(s);            out0[j] = s_1;        }         if (channels == 2) {            left->sample1 = s_1;            left->sample2 = s_2;            s_1 = right->sample1;            s_2 = right->sample2;        }         shift  = 12 - (in[5+i*2] & 15);        filter = in[5+i*2] >> 4;        if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {            avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);            filter=0;        }         f0 = xa_adpcm_table[filter][0];        f1 = xa_adpcm_table[filter][1];         for(j=0;j<28;j++) {            d = in[16+i+j*4];             t = sign_extend(d >> 4, 4);            s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);            s_2 = s_1;            s_1 = av_clip_int16(s);            out1[j] = s_1;        }         if (channels == 2) {            right->sample1 = s_1;            right->sample2 = s_2;        } else {            left->sample1 = s_1;            left->sample2 = s_2;        }         out0 += 28 * (3 - channels);        out1 += 28 * (3 - channels);    }     return 0;} static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int16_t *samples){    ADPCMDecodeContext *c = avctx->priv_data;    GetBitContext gb;    const int *table;    int k0, signmask, nb_bits, count;    int size = buf_size*8;    int i;     init_get_bits(&gb, buf, size);     //read bits & initial values    nb_bits = get_bits(&gb, 2)+2;    table = swf_index_tables[nb_bits-2];    k0 = 1 << (nb_bits-2);    signmask = 1 << (nb_bits-1);     while (get_bits_count(&gb) <= size - 22*avctx->channels) {        for (i = 0; i < avctx->channels; i++) {            *samples++ = c->status[i].predictor = get_sbits(&gb, 16);            c->status[i].step_index = get_bits(&gb, 6);        }         for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {            int i;             for (i = 0; i < avctx->channels; i++) {                // similar to IMA adpcm                int delta = get_bits(&gb, nb_bits);                int step = ff_adpcm_step_table[c->status[i].step_index];                int vpdiff = 0; // vpdiff = (delta+0.5)*step/4                int k = k0;                 do {                    if (delta & k)                        vpdiff += step;                    step >>= 1;                    k >>= 1;                } while(k);                vpdiff += step;                 if (delta & signmask)                    c->status[i].predictor -= vpdiff;                else                    c->status[i].predictor += vpdiff;                 c->status[i].step_index += table[delta & (~signmask)];                 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);                c->status[i].predictor = av_clip_int16(c->status[i].predictor);                 *samples++ = c->status[i].predictor;            }        }    }} /** * Get the number of samples that will be decoded from the packet. * In one case, this is actually the maximum number of samples possible to * decode with the given buf_size. *

Lines 1–500 of 1758.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t