2199 lines · 78.5 KB · C
RawDownload
/* * MPEG-4 ALS decoder * Copyright (c) 2009 Thilo Borgmann <thilo.borgmann _at_ mail.de> * * 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 */ /** * @file * MPEG-4 ALS decoder * @author Thilo Borgmann <thilo.borgmann _at_ mail.de> */ #include <inttypes.h> #include "avcodec.h"#include "get_bits.h"#include "unary.h"#include "mpeg4audio.h"#include "bgmc.h"#include "bswapdsp.h"#include "codec_internal.h"#include "decode.h"#include "internal.h"#include "mlz.h"#include "libavutil/mem.h"#include "libavutil/samplefmt.h"#include "libavutil/crc.h"#include "libavutil/softfloat_ieee754.h"#include "libavutil/intreadwrite.h" #include <stdint.h> /** Rice parameters and corresponding index offsets for decoding the *  indices of scaled PARCOR values. The table chosen is set globally *  by the encoder and stored in ALSSpecificConfig. */static const int8_t parcor_rice_table[3][20][2] = {    { {-52, 4}, {-29, 5}, {-31, 4}, { 19, 4}, {-16, 4},      { 12, 3}, { -7, 3}, {  9, 3}, { -5, 3}, {  6, 3},      { -4, 3}, {  3, 3}, { -3, 2}, {  3, 2}, { -2, 2},      {  3, 2}, { -1, 2}, {  2, 2}, { -1, 2}, {  2, 2} },    { {-58, 3}, {-42, 4}, {-46, 4}, { 37, 5}, {-36, 4},      { 29, 4}, {-29, 4}, { 25, 4}, {-23, 4}, { 20, 4},      {-17, 4}, { 16, 4}, {-12, 4}, { 12, 3}, {-10, 4},      {  7, 3}, { -4, 4}, {  3, 3}, { -1, 3}, {  1, 3} },    { {-59, 3}, {-45, 5}, {-50, 4}, { 38, 4}, {-39, 4},      { 32, 4}, {-30, 4}, { 25, 3}, {-23, 3}, { 20, 3},      {-20, 3}, { 16, 3}, {-13, 3}, { 10, 3}, { -7, 3},      {  3, 3}, {  0, 3}, { -1, 3}, {  2, 3}, { -1, 2} }};  /** Scaled PARCOR values used for the first two PARCOR coefficients. *  To be indexed by the Rice coded indices. *  Generated by: parcor_scaled_values[i] = 32 + ((i * (i+1)) << 7) - (1 << 20) *  Actual values are divided by 32 in order to be stored in 16 bits. */static const int16_t parcor_scaled_values[] = {    -1048544 / 32, -1048288 / 32, -1047776 / 32, -1047008 / 32,    -1045984 / 32, -1044704 / 32, -1043168 / 32, -1041376 / 32,    -1039328 / 32, -1037024 / 32, -1034464 / 32, -1031648 / 32,    -1028576 / 32, -1025248 / 32, -1021664 / 32, -1017824 / 32,    -1013728 / 32, -1009376 / 32, -1004768 / 32,  -999904 / 32,     -994784 / 32,  -989408 / 32,  -983776 / 32,  -977888 / 32,     -971744 / 32,  -965344 / 32,  -958688 / 32,  -951776 / 32,     -944608 / 32,  -937184 / 32,  -929504 / 32,  -921568 / 32,     -913376 / 32,  -904928 / 32,  -896224 / 32,  -887264 / 32,     -878048 / 32,  -868576 / 32,  -858848 / 32,  -848864 / 32,     -838624 / 32,  -828128 / 32,  -817376 / 32,  -806368 / 32,     -795104 / 32,  -783584 / 32,  -771808 / 32,  -759776 / 32,     -747488 / 32,  -734944 / 32,  -722144 / 32,  -709088 / 32,     -695776 / 32,  -682208 / 32,  -668384 / 32,  -654304 / 32,     -639968 / 32,  -625376 / 32,  -610528 / 32,  -595424 / 32,     -580064 / 32,  -564448 / 32,  -548576 / 32,  -532448 / 32,     -516064 / 32,  -499424 / 32,  -482528 / 32,  -465376 / 32,     -447968 / 32,  -430304 / 32,  -412384 / 32,  -394208 / 32,     -375776 / 32,  -357088 / 32,  -338144 / 32,  -318944 / 32,     -299488 / 32,  -279776 / 32,  -259808 / 32,  -239584 / 32,     -219104 / 32,  -198368 / 32,  -177376 / 32,  -156128 / 32,     -134624 / 32,  -112864 / 32,   -90848 / 32,   -68576 / 32,      -46048 / 32,   -23264 / 32,     -224 / 32,    23072 / 32,       46624 / 32,    70432 / 32,    94496 / 32,   118816 / 32,      143392 / 32,   168224 / 32,   193312 / 32,   218656 / 32,      244256 / 32,   270112 / 32,   296224 / 32,   322592 / 32,      349216 / 32,   376096 / 32,   403232 / 32,   430624 / 32,      458272 / 32,   486176 / 32,   514336 / 32,   542752 / 32,      571424 / 32,   600352 / 32,   629536 / 32,   658976 / 32,      688672 / 32,   718624 / 32,   748832 / 32,   779296 / 32,      810016 / 32,   840992 / 32,   872224 / 32,   903712 / 32,      935456 / 32,   967456 / 32,   999712 / 32,  1032224 / 32};  /** Gain values of p(0) for long-term prediction. *  To be indexed by the Rice coded indices. */static const uint8_t ltp_gain_values [4][4] = {    { 0,  8, 16,  24},    {32, 40, 48,  56},    {64, 70, 76,  82},    {88, 92, 96, 100}};  /** Inter-channel weighting factors for multi-channel correlation. *  To be indexed by the Rice coded indices. */static const int16_t mcc_weightings[] = {    204,  192,  179,  166,  153,  140,  128,  115,    102,   89,   76,   64,   51,   38,   25,   12,      0,  -12,  -25,  -38,  -51,  -64,  -76,  -89,   -102, -115, -128, -140, -153, -166, -179, -192};  /** Tail codes used in arithmetic coding using block Gilbert-Moore codes. */static const uint8_t tail_code[16][6] = {    { 74, 44, 25, 13,  7, 3},    { 68, 42, 24, 13,  7, 3},    { 58, 39, 23, 13,  7, 3},    {126, 70, 37, 19, 10, 5},    {132, 70, 37, 20, 10, 5},    {124, 70, 38, 20, 10, 5},    {120, 69, 37, 20, 11, 5},    {116, 67, 37, 20, 11, 5},    {108, 66, 36, 20, 10, 5},    {102, 62, 36, 20, 10, 5},    { 88, 58, 34, 19, 10, 5},    {162, 89, 49, 25, 13, 7},    {156, 87, 49, 26, 14, 7},    {150, 86, 47, 26, 14, 7},    {142, 84, 47, 26, 14, 7},    {131, 79, 46, 26, 14, 7}};  enum RA_Flag {    RA_FLAG_NONE,    RA_FLAG_FRAMES,    RA_FLAG_HEADER};  typedef struct ALSSpecificConfig {    uint32_t samples;         ///< number of samples, 0xFFFFFFFF if unknown    int resolution;           ///< 000 = 8-bit; 001 = 16-bit; 010 = 24-bit; 011 = 32-bit    int floating;             ///< 1 = IEEE 32-bit floating-point, 0 = integer    int msb_first;            ///< 1 = original CRC calculated on big-endian system, 0 = little-endian    int frame_length;         ///< frame length for each frame (last frame may differ)    int ra_distance;          ///< distance between RA frames (in frames, 0...255)    enum RA_Flag ra_flag;     ///< indicates where the size of ra units is stored    int adapt_order;          ///< adaptive order: 1 = on, 0 = off    int coef_table;           ///< table index of Rice code parameters    int long_term_prediction; ///< long term prediction (LTP): 1 = on, 0 = off    int max_order;            ///< maximum prediction order (0..1023)    int block_switching;      ///< number of block switching levels    int bgmc;                 ///< "Block Gilbert-Moore Code": 1 = on, 0 = off (Rice coding only)    int sb_part;              ///< sub-block partition    int joint_stereo;         ///< joint stereo: 1 = on, 0 = off    int mc_coding;            ///< extended inter-channel coding (multi channel coding): 1 = on, 0 = off    int chan_config;          ///< indicates that a chan_config_info field is present    int chan_sort;            ///< channel rearrangement: 1 = on, 0 = off    int rlslms;               ///< use "Recursive Least Square-Least Mean Square" predictor: 1 = on, 0 = off    int chan_config_info;     ///< mapping of channels to loudspeaker locations. Unused until setting channel configuration is implemented.    int *chan_pos;            ///< original channel positions    int crc_enabled;          ///< enable Cyclic Redundancy Checksum} ALSSpecificConfig;  typedef struct ALSChannelData {    int stop_flag;    int master_channel;    int time_diff_flag;    int time_diff_sign;    int time_diff_index;    int weighting[6];} ALSChannelData;  typedef struct ALSDecContext {    AVCodecContext *avctx;    ALSSpecificConfig sconf;    GetBitContext gb;    BswapDSPContext bdsp;    const AVCRC *crc_table;    uint32_t crc_org;               ///< CRC value of the original input data    uint32_t crc;                   ///< CRC value calculated from decoded data    unsigned int cur_frame_length;  ///< length of the current frame to decode    unsigned int frame_id;          ///< the frame ID / number of the current frame    unsigned int js_switch;         ///< if true, joint-stereo decoding is enforced    unsigned int cs_switch;         ///< if true, channel rearrangement is done    unsigned int num_blocks;        ///< number of blocks used in the current frame    unsigned int s_max;             ///< maximum Rice parameter allowed in entropy coding    uint8_t *bgmc_lut;              ///< pointer at lookup tables used for BGMC    int *bgmc_lut_status;           ///< pointer at lookup table status flags used for BGMC    int ltp_lag_length;             ///< number of bits used for ltp lag value    int *const_block;               ///< contains const_block flags for all channels    unsigned int *shift_lsbs;       ///< contains shift_lsbs flags for all channels    unsigned int *opt_order;        ///< contains opt_order flags for all channels    int *store_prev_samples;        ///< contains store_prev_samples flags for all channels    int *use_ltp;                   ///< contains use_ltp flags for all channels    int *ltp_lag;                   ///< contains ltp lag values for all channels    int **ltp_gain;                 ///< gain values for ltp 5-tap filter for a channel    int *ltp_gain_buffer;           ///< contains all gain values for ltp 5-tap filter    int32_t **quant_cof;            ///< quantized parcor coefficients for a channel    int32_t *quant_cof_buffer;      ///< contains all quantized parcor coefficients    int32_t **lpc_cof;              ///< coefficients of the direct form prediction filter for a channel    int32_t *lpc_cof_buffer;        ///< contains all coefficients of the direct form prediction filter    int32_t *lpc_cof_reversed_buffer; ///< temporary buffer to set up a reversed versio of lpc_cof_buffer    ALSChannelData **chan_data;     ///< channel data for multi-channel correlation    ALSChannelData *chan_data_buffer; ///< contains channel data for all channels    int *reverted_channels;         ///< stores a flag for each reverted channel    int32_t *prev_raw_samples;      ///< contains unshifted raw samples from the previous block    int32_t **raw_samples;          ///< decoded raw samples for each channel    int32_t *raw_buffer;            ///< contains all decoded raw samples including carryover samples    uint8_t *crc_buffer;            ///< buffer of byte order corrected samples used for CRC check    MLZ* mlz;                       ///< masked lz decompression structure    SoftFloat_IEEE754 *acf;         ///< contains common multiplier for all channels    int *last_acf_mantissa;         ///< contains the last acf mantissa data of common multiplier for all channels    int *shift_value;               ///< value by which the binary point is to be shifted for all channels    int *last_shift_value;          ///< contains last shift value for all channels    int **raw_mantissa;             ///< decoded mantissa bits of the difference signal    unsigned char *larray;          ///< buffer to store the output of masked lz decompression    int *nbits;                     ///< contains the number of bits to read for masked lz decompression for all samples    int highest_decoded_channel;} ALSDecContext;  typedef struct ALSBlockData {    unsigned int block_length;      ///< number of samples within the block    unsigned int ra_block;          ///< if true, this is a random access block    int          *const_block;      ///< if true, this is a constant value block    int          js_blocks;         ///< true if this block contains a difference signal    unsigned int *shift_lsbs;       ///< shift of values for this block    unsigned int *opt_order;        ///< prediction order of this block    int          *store_prev_samples;///< if true, carryover samples have to be stored    int          *use_ltp;          ///< if true, long-term prediction is used    int          *ltp_lag;          ///< lag value for long-term prediction    int          *ltp_gain;         ///< gain values for ltp 5-tap filter    int32_t      *quant_cof;        ///< quantized parcor coefficients    int32_t      *lpc_cof;          ///< coefficients of the direct form prediction    int32_t      *raw_samples;      ///< decoded raw samples / residuals for this block    int32_t      *prev_raw_samples; ///< contains unshifted raw samples from the previous block    int32_t      *raw_other;        ///< decoded raw samples of the other channel of a channel pair} ALSBlockData;  static av_cold void dprint_specific_config(ALSDecContext *ctx){#ifdef DEBUG    AVCodecContext *avctx    = ctx->avctx;    ALSSpecificConfig *sconf = &ctx->sconf;     ff_dlog(avctx, "resolution = %i\n",           sconf->resolution);    ff_dlog(avctx, "floating = %i\n",             sconf->floating);    ff_dlog(avctx, "frame_length = %i\n",         sconf->frame_length);    ff_dlog(avctx, "ra_distance = %i\n",          sconf->ra_distance);    ff_dlog(avctx, "ra_flag = %i\n",              sconf->ra_flag);    ff_dlog(avctx, "adapt_order = %i\n",          sconf->adapt_order);    ff_dlog(avctx, "coef_table = %i\n",           sconf->coef_table);    ff_dlog(avctx, "long_term_prediction = %i\n", sconf->long_term_prediction);    ff_dlog(avctx, "max_order = %i\n",            sconf->max_order);    ff_dlog(avctx, "block_switching = %i\n",      sconf->block_switching);    ff_dlog(avctx, "bgmc = %i\n",                 sconf->bgmc);    ff_dlog(avctx, "sb_part = %i\n",              sconf->sb_part);    ff_dlog(avctx, "joint_stereo = %i\n",         sconf->joint_stereo);    ff_dlog(avctx, "mc_coding = %i\n",            sconf->mc_coding);    ff_dlog(avctx, "chan_config = %i\n",          sconf->chan_config);    ff_dlog(avctx, "chan_sort = %i\n",            sconf->chan_sort);    ff_dlog(avctx, "RLSLMS = %i\n",               sconf->rlslms);    ff_dlog(avctx, "chan_config_info = %i\n",     sconf->chan_config_info);#endif}  /** Read an ALSSpecificConfig from a buffer into the output struct. */static av_cold int read_specific_config(ALSDecContext *ctx){    GetBitContext gb;    uint64_t ht_size;    int i, config_offset;    MPEG4AudioConfig m4ac = {0};    ALSSpecificConfig *sconf = &ctx->sconf;    AVCodecContext *avctx    = ctx->avctx;    uint32_t als_id, header_size, trailer_size;    int ret;     if ((ret = init_get_bits8(&gb, avctx->extradata, avctx->extradata_size)) < 0)        return ret;     config_offset = avpriv_mpeg4audio_get_config2(&m4ac, avctx->extradata,                                                  avctx->extradata_size, 1, avctx);     if (config_offset < 0)        return AVERROR_INVALIDDATA;     skip_bits_long(&gb, config_offset);     if (get_bits_left(&gb) < (30 << 3))        return AVERROR_INVALIDDATA;     // read the fixed items    als_id                      = get_bits_long(&gb, 32);    avctx->sample_rate          = m4ac.sample_rate;    skip_bits_long(&gb, 32); // sample rate already known    sconf->samples              = get_bits_long(&gb, 32);     if (avctx->ch_layout.nb_channels != m4ac.channels) {        av_channel_layout_uninit(&avctx->ch_layout);        avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;        avctx->ch_layout.nb_channels = m4ac.channels;    }     skip_bits(&gb, 16);      // number of channels already known    skip_bits(&gb, 3);       // skip file_type    sconf->resolution           = get_bits(&gb, 3);    sconf->floating             = get_bits1(&gb);    sconf->msb_first            = get_bits1(&gb);    sconf->frame_length         = get_bits(&gb, 16) + 1;    sconf->ra_distance          = get_bits(&gb, 8);    sconf->ra_flag              = get_bits(&gb, 2);    sconf->adapt_order          = get_bits1(&gb);    sconf->coef_table           = get_bits(&gb, 2);    sconf->long_term_prediction = get_bits1(&gb);    sconf->max_order            = get_bits(&gb, 10);    sconf->block_switching      = get_bits(&gb, 2);    sconf->bgmc                 = get_bits1(&gb);    sconf->sb_part              = get_bits1(&gb);    sconf->joint_stereo         = get_bits1(&gb);    sconf->mc_coding            = get_bits1(&gb);    sconf->chan_config          = get_bits1(&gb);    sconf->chan_sort            = get_bits1(&gb);    sconf->crc_enabled          = get_bits1(&gb);    sconf->rlslms               = get_bits1(&gb);    skip_bits(&gb, 5);       // skip 5 reserved bits    skip_bits1(&gb);         // skip aux_data_enabled      // check for ALSSpecificConfig struct    if (als_id != MKBETAG('A','L','S','\0'))        return AVERROR_INVALIDDATA;     if (avctx->ch_layout.nb_channels > FF_SANE_NB_CHANNELS) {        avpriv_request_sample(avctx, "Huge number of channels");        return AVERROR_PATCHWELCOME;    }     if (avctx->ch_layout.nb_channels == 0)        return AVERROR_INVALIDDATA;     ctx->cur_frame_length = sconf->frame_length;     // read channel config    if (sconf->chan_config)        sconf->chan_config_info = get_bits(&gb, 16);    // TODO: use this to set avctx->channel_layout      // read channel sorting    if (sconf->chan_sort && avctx->ch_layout.nb_channels > 1) {        int chan_pos_bits = av_ceil_log2(avctx->ch_layout.nb_channels);        int bits_needed  = avctx->ch_layout.nb_channels * chan_pos_bits + 7;        if (get_bits_left(&gb) < bits_needed)            return AVERROR_INVALIDDATA;         if (!(sconf->chan_pos = av_malloc_array(avctx->ch_layout.nb_channels, sizeof(*sconf->chan_pos))))            return AVERROR(ENOMEM);         ctx->cs_switch = 1;         for (i = 0; i < avctx->ch_layout.nb_channels; i++) {            sconf->chan_pos[i] = -1;        }         for (i = 0; i < avctx->ch_layout.nb_channels; i++) {            int idx;             idx = get_bits(&gb, chan_pos_bits);            if (idx >= avctx->ch_layout.nb_channels || sconf->chan_pos[idx] != -1) {                av_log(avctx, AV_LOG_WARNING, "Invalid channel reordering.\n");                ctx->cs_switch = 0;                break;            }            sconf->chan_pos[idx] = i;        }         align_get_bits(&gb);    }      // read fixed header and trailer sizes,    // if size = 0xFFFFFFFF then there is no data field!    if (get_bits_left(&gb) < 64)        return AVERROR_INVALIDDATA;     header_size  = get_bits_long(&gb, 32);    trailer_size = get_bits_long(&gb, 32);    if (header_size  == 0xFFFFFFFF)        header_size  = 0;    if (trailer_size == 0xFFFFFFFF)        trailer_size = 0;     ht_size = ((int64_t)(header_size) + (int64_t)(trailer_size)) << 3;      // skip the header and trailer data    if (get_bits_left(&gb) < ht_size)        return AVERROR_INVALIDDATA;     if (ht_size > INT32_MAX)        return AVERROR_PATCHWELCOME;     skip_bits_long(&gb, ht_size);      // initialize CRC calculation    if (sconf->crc_enabled) {        if (get_bits_left(&gb) < 32)            return AVERROR_INVALIDDATA;         if (avctx->err_recognition & (AV_EF_CRCCHECK|AV_EF_CAREFUL)) {            ctx->crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);            ctx->crc       = 0xFFFFFFFF;            ctx->crc_org   = ~get_bits_long(&gb, 32);        } else            skip_bits_long(&gb, 32);    }      // no need to read the rest of ALSSpecificConfig (ra_unit_size & aux data)     dprint_specific_config(ctx);     return 0;}  /** Check the ALSSpecificConfig for unsupported features. */static int check_specific_config(ALSDecContext *ctx){    ALSSpecificConfig *sconf = &ctx->sconf;    int error = 0;     // report unsupported feature and set error value    #define MISSING_ERR(cond, str, errval)              \    {                                                   \        if (cond) {                                     \            avpriv_report_missing_feature(ctx->avctx,   \                                          str);         \            error = errval;                             \        }                                               \    }     MISSING_ERR(sconf->rlslms,    "Adaptive RLS-LMS prediction", AVERROR_PATCHWELCOME);     return error;}  /** Parse the bs_info field to extract the block partitioning used in *  block switching mode, refer to ISO/IEC 14496-3, section 11.6.2. */static void parse_bs_info(const uint32_t bs_info, unsigned int n,                          unsigned int div, unsigned int **div_blocks,                          unsigned int *num_blocks){    if (n < 31 && ((bs_info << n) & 0x40000000)) {        // if the level is valid and the investigated bit n is set        // then recursively check both children at bits (2n+1) and (2n+2)        n   *= 2;        div += 1;        parse_bs_info(bs_info, n + 1, div, div_blocks, num_blocks);        parse_bs_info(bs_info, n + 2, div, div_blocks, num_blocks);    } else {        // else the bit is not set or the last level has been reached        // (bit implicitly not set)        **div_blocks = div;        (*div_blocks)++;        (*num_blocks)++;    }} 

Lines 1–500 of 2199.

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