2460 lines · 96.9 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) * Argonaut Games ADPCM decoder by Zane van Iperen (zane@zanevaniperen.com) * Simon & Schuster Interactive ADPCM decoder by Zane van Iperen (zane@zanevaniperen.com) * Ubisoft ADPCM decoder by Zane van Iperen (zane@zanevaniperen.com) * High Voltage Software ALP decoder by Zane van Iperen (zane@zanevaniperen.com) * Cunning Developments decoder by Zane van Iperen (zane@zanevaniperen.com) * * 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 "config_components.h" #include "avcodec.h"#include "get_bits.h"#include "bytestream.h"#include "adpcm.h"#include "adpcm_data.h"#include "codec_internal.h"#include "decode.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/ */ #define CASE_0(codec_id, ...)#define CASE_1(codec_id, ...) \    case codec_id:            \    { __VA_ARGS__ }           \    break;#define CASE_2(enabled, codec_id, ...) \        CASE_ ## enabled(codec_id, __VA_ARGS__)#define CASE_3(config, codec_id, ...) \        CASE_2(config, codec_id, __VA_ARGS__)#define CASE(codec, ...) \        CASE_3(CONFIG_ ## codec ## _DECODER, AV_CODEC_ID_ ## codec, __VA_ARGS__) /* These are for CD-ROM XA ADPCM */static const int8_t xa_adpcm_table[5][2] = {    {   0,   0 },    {  60,   0 },    { 115, -52 },    {  98, -55 },    { 122, -60 }}; static const int16_t afc_coeffs[2][16] = {    { 0, 2048, 0, 1024, 4096, 3584, 3072, 4608, 4200, 4800, 5120, 2048, 1024, -1024, -1024, -2048 },    { 0, 0, 2048, 1024, -2048, -1536, -1024, -2560, -2248, -2300, -3072, -2048, -1024, 1024, 0, 0 }}; static const int16_t ea_adpcm_table[] = {    0,  240,  460,  392,    0,    0, -208, -220,    0,    1,    3,    4,    7,    8,   10,   11,    0,   -1,   -3,   -4}; /* * Dumped from the binaries: * - FantasticJourney.exe - 0x794D2, DGROUP:0x47A4D2 * - BigRaceUSA.exe       - 0x9B8AA, DGROUP:0x49C4AA * - Timeshock!.exe       - 0x8506A, DGROUP:0x485C6A */static const int8_t ima_cunning_index_table[9] = {    -1, -1, -1, -1, 1, 2, 3, 4, -1}; /* * Dumped from the binaries: * - FantasticJourney.exe - 0x79458, DGROUP:0x47A458 * - BigRaceUSA.exe       - 0x9B830, DGROUP:0x49C430 * - Timeshock!.exe       - 0x84FF0, DGROUP:0x485BF0 */static const int16_t ima_cunning_step_table[61] = {       1,    1,   1,      1,     2,     2,     3,     3,    4,      5,       6,    7,   8,     10,    12,    14,    16,    20,    24,    28,      32,   40,  48,     56,    64,    80,    96,   112,   128,   160,     192,  224,  256,   320,   384,   448,   512,   640,   768,   896,    1024, 1280, 1536,  1792,  2048,  2560,  3072,  3584,  4096,  5120,    6144, 7168, 8192, 10240, 12288, 14336, 16384, 20480, 24576, 28672, 0}; static const int8_t adpcm_index_table2[4] = {    -1,  2,    -1,  2,}; static const int8_t adpcm_index_table3[8] = {    -1, -1,  1,  2,    -1, -1,  1,  2,}; static const int8_t adpcm_index_table5[32] = {    -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16,    -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16,}; static const int8_t * const adpcm_index_tables[4] = {    &adpcm_index_table2[0],    &adpcm_index_table3[0],    &ff_adpcm_index_table[0],    &adpcm_index_table5[0],}; static const int16_t mtaf_stepsize[32][16] = {    {     1,     5,     9,    13,    16,    20,    24,    28,         -1,    -5,    -9,   -13,   -16,   -20,   -24,   -28, },    {     2,     6,    11,    15,    20,    24,    29,    33,         -2,    -6,   -11,   -15,   -20,   -24,   -29,   -33, },    {     2,     7,    13,    18,    23,    28,    34,    39,         -2,    -7,   -13,   -18,   -23,   -28,   -34,   -39, },    {     3,     9,    15,    21,    28,    34,    40,    46,         -3,    -9,   -15,   -21,   -28,   -34,   -40,   -46, },    {     3,    11,    18,    26,    33,    41,    48,    56,         -3,   -11,   -18,   -26,   -33,   -41,   -48,   -56, },    {     4,    13,    22,    31,    40,    49,    58,    67,         -4,   -13,   -22,   -31,   -40,   -49,   -58,   -67, },    {     5,    16,    26,    37,    48,    59,    69,    80,         -5,   -16,   -26,   -37,   -48,   -59,   -69,   -80, },    {     6,    19,    31,    44,    57,    70,    82,    95,         -6,   -19,   -31,   -44,   -57,   -70,   -82,   -95, },    {     7,    22,    38,    53,    68,    83,    99,   114,         -7,   -22,   -38,   -53,   -68,   -83,   -99,  -114, },    {     9,    27,    45,    63,    81,    99,   117,   135,         -9,   -27,   -45,   -63,   -81,   -99,  -117,  -135, },    {    10,    32,    53,    75,    96,   118,   139,   161,        -10,   -32,   -53,   -75,   -96,  -118,  -139,  -161, },    {    12,    38,    64,    90,   115,   141,   167,   193,        -12,   -38,   -64,   -90,  -115,  -141,  -167,  -193, },    {    15,    45,    76,   106,   137,   167,   198,   228,        -15,   -45,   -76,  -106,  -137,  -167,  -198,  -228, },    {    18,    54,    91,   127,   164,   200,   237,   273,        -18,   -54,   -91,  -127,  -164,  -200,  -237,  -273, },    {    21,    65,   108,   152,   195,   239,   282,   326,        -21,   -65,  -108,  -152,  -195,  -239,  -282,  -326, },    {    25,    77,   129,   181,   232,   284,   336,   388,        -25,   -77,  -129,  -181,  -232,  -284,  -336,  -388, },    {    30,    92,   153,   215,   276,   338,   399,   461,        -30,   -92,  -153,  -215,  -276,  -338,  -399,  -461, },    {    36,   109,   183,   256,   329,   402,   476,   549,        -36,  -109,  -183,  -256,  -329,  -402,  -476,  -549, },    {    43,   130,   218,   305,   392,   479,   567,   654,        -43,  -130,  -218,  -305,  -392,  -479,  -567,  -654, },    {    52,   156,   260,   364,   468,   572,   676,   780,        -52,  -156,  -260,  -364,  -468,  -572,  -676,  -780, },    {    62,   186,   310,   434,   558,   682,   806,   930,        -62,  -186,  -310,  -434,  -558,  -682,  -806,  -930, },    {    73,   221,   368,   516,   663,   811,   958,  1106,        -73,  -221,  -368,  -516,  -663,  -811,  -958, -1106, },    {    87,   263,   439,   615,   790,   966,  1142,  1318,        -87,  -263,  -439,  -615,  -790,  -966, -1142, -1318, },    {   104,   314,   523,   733,   942,  1152,  1361,  1571,       -104,  -314,  -523,  -733,  -942, -1152, -1361, -1571, },    {   124,   374,   623,   873,  1122,  1372,  1621,  1871,       -124,  -374,  -623,  -873, -1122, -1372, -1621, -1871, },    {   148,   445,   743,  1040,  1337,  1634,  1932,  2229,       -148,  -445,  -743, -1040, -1337, -1634, -1932, -2229, },    {   177,   531,   885,  1239,  1593,  1947,  2301,  2655,       -177,  -531,  -885, -1239, -1593, -1947, -2301, -2655, },    {   210,   632,  1053,  1475,  1896,  2318,  2739,  3161,       -210,  -632, -1053, -1475, -1896, -2318, -2739, -3161, },    {   251,   753,  1255,  1757,  2260,  2762,  3264,  3766,       -251,  -753, -1255, -1757, -2260, -2762, -3264, -3766, },    {   299,   897,  1495,  2093,  2692,  3290,  3888,  4486,       -299,  -897, -1495, -2093, -2692, -3290, -3888, -4486, },    {   356,  1068,  1781,  2493,  3206,  3918,  4631,  5343,       -356, -1068, -1781, -2493, -3206, -3918, -4631, -5343, },    {   424,  1273,  2121,  2970,  3819,  4668,  5516,  6365,       -424, -1273, -2121, -2970, -3819, -4668, -5516, -6365, },}; static const int16_t oki_step_table[49] = {     16,  17,  19,  21,   23,   25,   28,   31,   34,  37,     41,  45,  50,  55,   60,   66,   73,   80,   88,  97,    107, 118, 130, 143,  157,  173,  190,  209,  230, 253,    279, 307, 337, 371,  408,  449,  494,  544,  598, 658,    724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552}; // padded to zero where table size is less then 16static const int8_t 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 }}; static const int8_t zork_index_table[8] = {    -1, -1, -1, 1, 4, 7, 10, 12,}; static const int8_t mtf_index_table[16] = {     8,  6,  4,  2, -1, -1, -1, -1,    -1, -1, -1, -1,  2,  4,  6,  8,}; /* end of tables */ typedef struct ADPCMDecodeContext {    ADPCMChannelStatus status[14];    int vqa_version;                /**< VQA version. Used for ADPCM_IMA_WS */    int has_status;                 /**< Status flag. Reset to 0 after a flush. */} ADPCMDecodeContext; static void adpcm_flush(AVCodecContext *avctx); static av_cold int adpcm_decode_init(AVCodecContext * avctx){    ADPCMDecodeContext *c = avctx->priv_data;    unsigned int min_channels = 1;    unsigned int max_channels = 2;     adpcm_flush(avctx);     switch(avctx->codec->id) {    case AV_CODEC_ID_ADPCM_IMA_AMV:        max_channels = 1;        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:    case AV_CODEC_ID_ADPCM_MS:        max_channels = 6;        break;    case AV_CODEC_ID_ADPCM_MTAF:        min_channels = 2;        max_channels = 8;        if (avctx->ch_layout.nb_channels & 1) {            avpriv_request_sample(avctx, "channel count %d", avctx->ch_layout.nb_channels);            return AVERROR_PATCHWELCOME;        }        break;    case AV_CODEC_ID_ADPCM_DTK:        min_channels = 2;        break;    case AV_CODEC_ID_ADPCM_PSX:        max_channels = 8;        if (avctx->ch_layout.nb_channels <= 0 ||            avctx->block_align % (16 * avctx->ch_layout.nb_channels))            return AVERROR_INVALIDDATA;        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->ch_layout.nb_channels < min_channels ||        avctx->ch_layout.nb_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_IMA_WAV:        if (avctx->bits_per_coded_sample < 2 || avctx->bits_per_coded_sample > 5)            return AVERROR_INVALIDDATA;        break;    case AV_CODEC_ID_ADPCM_ARGO:        if (avctx->bits_per_coded_sample != 4 ||            avctx->block_align != 17 * avctx->ch_layout.nb_channels)            return AVERROR_INVALIDDATA;        break;    case AV_CODEC_ID_ADPCM_IMA_XBOX:        if (avctx->bits_per_coded_sample != 4)            return AVERROR_INVALIDDATA;        break;    case AV_CODEC_ID_ADPCM_ZORK:        if (avctx->bits_per_coded_sample != 8)            return AVERROR_INVALIDDATA;        break;    default:        break;    }     switch (avctx->codec->id) {    case AV_CODEC_ID_ADPCM_AICA:    case AV_CODEC_ID_ADPCM_IMA_CUNNING:    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_IMA_XBOX:    case AV_CODEC_ID_ADPCM_4XM:    case AV_CODEC_ID_ADPCM_XA:    case AV_CODEC_ID_ADPCM_XMD:    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:    case AV_CODEC_ID_ADPCM_ARGO:    case AV_CODEC_ID_ADPCM_IMA_MOFLEX:        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;    case AV_CODEC_ID_ADPCM_MS:        avctx->sample_fmt = avctx->ch_layout.nb_channels > 2 ? AV_SAMPLE_FMT_S16P :                                                  AV_SAMPLE_FMT_S16;        break;    default:        avctx->sample_fmt = AV_SAMPLE_FMT_S16;    }    return 0;} static inline int16_t adpcm_agm_expand_nibble(ADPCMChannelStatus *c, int8_t nibble){    int delta, pred, step, add;     pred = c->predictor;    delta = nibble & 7;    step = c->step;    add = (delta * 2 + 1) * step;    if (add < 0)        add = add + 7;     if ((nibble & 8) == 0)        pred = av_clip(pred + (add >> 3), -32767, 32767);    else        pred = av_clip(pred - (add >> 3), -32767, 32767);     switch (delta) {    case 7:        step *= 0x99;        break;    case 6:        c->step = av_clip(c->step * 2, 127, 24576);        c->predictor = pred;        return pred;    case 5:        step *= 0x66;        break;    case 4:        step *= 0x4d;        break;    default:        step *= 0x39;        break;    }     if (step < 0)        step += 0x3f;     c->step = step >> 6;    c->step = av_clip(c->step, 127, 24576);    c->predictor = pred;    return pred;} 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_alp_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;    diff = (delta * 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_mtf_expand_nibble(ADPCMChannelStatus *c, int nibble){    int step_index, step, delta, predictor;     step = ff_adpcm_step_table[c->step_index];     delta = step * (2 * nibble - 15);    predictor = c->predictor + delta;     step_index = c->step_index + mtf_index_table[(unsigned)nibble];    c->predictor = av_clip_int16(predictor >> 4);    c->step_index = av_clip(step_index, 0, 88);     return (int16_t)c->predictor;} static inline int16_t adpcm_ima_cunning_expand_nibble(ADPCMChannelStatus *c, int8_t nibble){    int step_index;    int predictor;    int step;     nibble = sign_extend(nibble & 0xF, 4);     step = ima_cunning_step_table[c->step_index];    step_index = c->step_index + ima_cunning_index_table[abs(nibble)];    step_index = av_clip(step_index, 0, 60);     predictor = c->predictor + step * nibble;     c->predictor = av_clip_int16(predictor);    c->step_index = step_index;     return 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 + adpcm_index_tables[bps - 2][nibble];    step_index = av_clip(step_index, 0, 88);     sign = nibble & (1 << shift);    delta = av_zero_extend(nibble, shift);

Lines 1–500 of 2460.

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