⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mlpdec.c

📁 ffmpeg移植到symbian的全部源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * MLP decoder * Copyright (c) 2007-2008 Ian Caulfield * * 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 mlpdec.c * MLP decoder */#include <stdint.h>#include "avcodec.h"#include "libavutil/intreadwrite.h"#include "bitstream.h"#include "libavutil/crc.h"#include "parser.h"#include "mlp_parser.h"/** Maximum number of channels that can be decoded. */#define MAX_CHANNELS        16/** Maximum number of matrices used in decoding; most streams have one matrix *  per output channel, but some rematrix a channel (usually 0) more than once. */#define MAX_MATRICES        15/** Maximum number of substreams that can be decoded. This could also be set *  higher, but I haven't seen any examples with more than two. */#define MAX_SUBSTREAMS      2/** maximum sample frequency seen in files */#define MAX_SAMPLERATE      192000/** maximum number of audio samples within one access unit */#define MAX_BLOCKSIZE       (40 * (MAX_SAMPLERATE / 48000))/** next power of two greater than MAX_BLOCKSIZE */#define MAX_BLOCKSIZE_POW2  (64 * (MAX_SAMPLERATE / 48000))/** number of allowed filters */#define NUM_FILTERS         2/** The maximum number of taps in either the IIR or FIR filter; *  I believe MLP actually specifies the maximum order for IIR filters as four, *  and that the sum of the orders of both filters must be <= 8. */#define MAX_FILTER_ORDER    8/** number of bits used for VLC lookup - longest Huffman code is 9 */#define VLC_BITS            9static const char* sample_message =    "Please file a bug report following the instructions at "    "http://ffmpeg.mplayerhq.hu/bugreports.html and include "    "a sample of this file.";typedef struct SubStream {    //! Set if a valid restart header has been read. Otherwise the substream cannot be decoded.    uint8_t     restart_seen;    //@{    /** restart header data */    //! The type of noise to be used in the rematrix stage.    uint16_t    noise_type;    //! The index of the first channel coded in this substream.    uint8_t     min_channel;    //! The index of the last channel coded in this substream.    uint8_t     max_channel;    //! The number of channels input into the rematrix stage.    uint8_t     max_matrix_channel;    //! The left shift applied to random noise in 0x31ea substreams.    uint8_t     noise_shift;    //! The current seed value for the pseudorandom noise generator(s).    uint32_t    noisegen_seed;    //! Set if the substream contains extra info to check the size of VLC blocks.    uint8_t     data_check_present;    //! Bitmask of which parameter sets are conveyed in a decoding parameter block.    uint8_t     param_presence_flags;#define PARAM_BLOCKSIZE     (1 << 7)#define PARAM_MATRIX        (1 << 6)#define PARAM_OUTSHIFT      (1 << 5)#define PARAM_QUANTSTEP     (1 << 4)#define PARAM_FIR           (1 << 3)#define PARAM_IIR           (1 << 2)#define PARAM_HUFFOFFSET    (1 << 1)    //@}    //@{    /** matrix data */    //! Number of matrices to be applied.    uint8_t     num_primitive_matrices;    //! matrix output channel    uint8_t     matrix_out_ch[MAX_MATRICES];    //! Whether the LSBs of the matrix output are encoded in the bitstream.    uint8_t     lsb_bypass[MAX_MATRICES];    //! Matrix coefficients, stored as 2.14 fixed point.    int32_t     matrix_coeff[MAX_MATRICES][MAX_CHANNELS+2];    //! Left shift to apply to noise values in 0x31eb substreams.    uint8_t     matrix_noise_shift[MAX_MATRICES];    //@}    //! Left shift to apply to Huffman-decoded residuals.    uint8_t     quant_step_size[MAX_CHANNELS];    //! number of PCM samples in current audio block    uint16_t    blocksize;    //! Number of PCM samples decoded so far in this frame.    uint16_t    blockpos;    //! Left shift to apply to decoded PCM values to get final 24-bit output.    int8_t      output_shift[MAX_CHANNELS];    //! Running XOR of all output samples.    int32_t     lossless_check_data;} SubStream;typedef struct MLPDecodeContext {    AVCodecContext *avctx;    //! Set if a valid major sync block has been read. Otherwise no decoding is possible.    uint8_t     params_valid;    //! Number of substreams contained within this stream.    uint8_t     num_substreams;    //! Index of the last substream to decode - further substreams are skipped.    uint8_t     max_decoded_substream;    //! number of PCM samples contained in each frame    int         access_unit_size;    //! next power of two above the number of samples in each frame    int         access_unit_size_pow2;    SubStream   substream[MAX_SUBSTREAMS];    //@{    /** filter data */#define FIR 0#define IIR 1    //! number of taps in filter    uint8_t     filter_order[MAX_CHANNELS][NUM_FILTERS];    //! Right shift to apply to output of filter.    uint8_t     filter_shift[MAX_CHANNELS][NUM_FILTERS];    int32_t     filter_coeff[MAX_CHANNELS][NUM_FILTERS][MAX_FILTER_ORDER];    int32_t     filter_state[MAX_CHANNELS][NUM_FILTERS][MAX_FILTER_ORDER];    //@}    //@{    /** sample data coding information */    //! Offset to apply to residual values.    int16_t     huff_offset[MAX_CHANNELS];    //! sign/rounding-corrected version of huff_offset    int32_t     sign_huff_offset[MAX_CHANNELS];    //! Which VLC codebook to use to read residuals.    uint8_t     codebook[MAX_CHANNELS];    //! Size of residual suffix not encoded using VLC.    uint8_t     huff_lsbs[MAX_CHANNELS];    //@}    int8_t      noise_buffer[MAX_BLOCKSIZE_POW2];    int8_t      bypassed_lsbs[MAX_BLOCKSIZE][MAX_CHANNELS];    int32_t     sample_buffer[MAX_BLOCKSIZE][MAX_CHANNELS+2];} MLPDecodeContext;/** Tables defining the Huffman codes. *  There are three entropy coding methods used in MLP (four if you count *  "none" as a method). These use the same sequences for codes starting with *  00 or 01, but have different codes starting with 1. */static const uint8_t huffman_tables[3][18][2] = {    {    /* Huffman table 0, -7 - +10 */        {0x01, 9}, {0x01, 8}, {0x01, 7}, {0x01, 6}, {0x01, 5}, {0x01, 4}, {0x01, 3},        {0x04, 3}, {0x05, 3}, {0x06, 3}, {0x07, 3},        {0x03, 3}, {0x05, 4}, {0x09, 5}, {0x11, 6}, {0x21, 7}, {0x41, 8}, {0x81, 9},    }, { /* Huffman table 1, -7 - +8 */        {0x01, 9}, {0x01, 8}, {0x01, 7}, {0x01, 6}, {0x01, 5}, {0x01, 4}, {0x01, 3},        {0x02, 2}, {0x03, 2},        {0x03, 3}, {0x05, 4}, {0x09, 5}, {0x11, 6}, {0x21, 7}, {0x41, 8}, {0x81, 9},    }, { /* Huffman table 2, -7 - +7 */        {0x01, 9}, {0x01, 8}, {0x01, 7}, {0x01, 6}, {0x01, 5}, {0x01, 4}, {0x01, 3},        {0x01, 1},        {0x03, 3}, {0x05, 4}, {0x09, 5}, {0x11, 6}, {0x21, 7}, {0x41, 8}, {0x81, 9},    }};static VLC huff_vlc[3];static int crc_init = 0;static AVCRC crc_63[1024];static AVCRC crc_1D[1024];/** Initialize static data, constant between all invocations of the codec. */static av_cold void init_static(){    INIT_VLC_STATIC(&huff_vlc[0], VLC_BITS, 18,                &huffman_tables[0][0][1], 2, 1,                &huffman_tables[0][0][0], 2, 1, 512);    INIT_VLC_STATIC(&huff_vlc[1], VLC_BITS, 16,                &huffman_tables[1][0][1], 2, 1,                &huffman_tables[1][0][0], 2, 1, 512);    INIT_VLC_STATIC(&huff_vlc[2], VLC_BITS, 15,                &huffman_tables[2][0][1], 2, 1,                &huffman_tables[2][0][0], 2, 1, 512);    if (!crc_init) {        av_crc_init(crc_63, 0,  8,   0x63, sizeof(crc_63));        av_crc_init(crc_1D, 0,  8,   0x1D, sizeof(crc_1D));        crc_init = 1;    }}/** MLP uses checksums that seem to be based on the standard CRC algorithm, but *  are not (in implementation terms, the table lookup and XOR are reversed). *  We can implement this behavior using a standard av_crc on all but the *  last element, then XOR that with the last element. */static uint8_t mlp_checksum8(const uint8_t *buf, unsigned int buf_size){    uint8_t checksum = av_crc(crc_63, 0x3c, buf, buf_size - 1); // crc_63[0xa2] == 0x3c    checksum ^= buf[buf_size-1];    return checksum;}/** Calculate an 8-bit checksum over a restart header -- a non-multiple-of-8 *  number of bits, starting two bits into the first byte of buf. */static uint8_t mlp_restart_checksum(const uint8_t *buf, unsigned int bit_size){    int i;    int num_bytes = (bit_size + 2) / 8;    int crc = crc_1D[buf[0] & 0x3f];    crc = av_crc(crc_1D, crc, buf + 1, num_bytes - 2);    crc ^= buf[num_bytes - 1];    for (i = 0; i < ((bit_size + 2) & 7); i++) {        crc <<= 1;        if (crc & 0x100)            crc ^= 0x11D;        crc ^= (buf[num_bytes] >> (7 - i)) & 1;    }    return crc;}static inline int32_t calculate_sign_huff(MLPDecodeContext *m,                                          unsigned int substr, unsigned int ch){    SubStream *s = &m->substream[substr];    int lsb_bits = m->huff_lsbs[ch] - s->quant_step_size[ch];    int sign_shift = lsb_bits + (m->codebook[ch] ? 2 - m->codebook[ch] : -1);    int32_t sign_huff_offset = m->huff_offset[ch];    if (m->codebook[ch] > 0)        sign_huff_offset -= 7 << lsb_bits;    if (sign_shift >= 0)        sign_huff_offset -= 1 << sign_shift;    return sign_huff_offset;}/** Read a sample, consisting of either, both or neither of entropy-coded MSBs *  and plain LSBs. */static inline int read_huff_channels(MLPDecodeContext *m, GetBitContext *gbp,                                     unsigned int substr, unsigned int pos){    SubStream *s = &m->substream[substr];    unsigned int mat, channel;    for (mat = 0; mat < s->num_primitive_matrices; mat++)        if (s->lsb_bypass[mat])            m->bypassed_lsbs[pos + s->blockpos][mat] = get_bits1(gbp);    for (channel = s->min_channel; channel <= s->max_channel; channel++) {        int codebook = m->codebook[channel];        int quant_step_size = s->quant_step_size[channel];        int lsb_bits = m->huff_lsbs[channel] - quant_step_size;        int result = 0;        if (codebook > 0)            result = get_vlc2(gbp, huff_vlc[codebook-1].table,                            VLC_BITS, (9 + VLC_BITS - 1) / VLC_BITS);        if (result < 0)            return -1;        if (lsb_bits > 0)            result = (result << lsb_bits) + get_bits(gbp, lsb_bits);        result  += m->sign_huff_offset[channel];        result <<= quant_step_size;        m->sample_buffer[pos + s->blockpos][channel] = result;    }    return 0;}static av_cold int mlp_decode_init(AVCodecContext *avctx){    MLPDecodeContext *m = avctx->priv_data;    int substr;    init_static();    m->avctx = avctx;    for (substr = 0; substr < MAX_SUBSTREAMS; substr++)        m->substream[substr].lossless_check_data = 0xffffffff;    return 0;}/** Read a major sync info header - contains high level information about *  the stream - sample rate, channel arrangement etc. Most of this *  information is not actually necessary for decoding, only for playback. */static int read_major_sync(MLPDecodeContext *m, GetBitContext *gb){    MLPHeaderInfo mh;    int substr;    if (ff_mlp_read_major_sync(m->avctx, &mh, gb) != 0)        return -1;    if (mh.group1_bits == 0) {        av_log(m->avctx, AV_LOG_ERROR, "invalid/unknown bits per sample\n");        return -1;    }    if (mh.group2_bits > mh.group1_bits) {        av_log(m->avctx, AV_LOG_ERROR,               "Channel group 2 cannot have more bits per sample than group 1.\n");        return -1;    }    if (mh.group2_samplerate && mh.group2_samplerate != mh.group1_samplerate) {        av_log(m->avctx, AV_LOG_ERROR,               "Channel groups with differing sample rates are not currently supported.\n");        return -1;    }    if (mh.group1_samplerate == 0) {        av_log(m->avctx, AV_LOG_ERROR, "invalid/unknown sampling rate\n");        return -1;    }    if (mh.group1_samplerate > MAX_SAMPLERATE) {        av_log(m->avctx, AV_LOG_ERROR,               "Sampling rate %d is greater than the supported maximum (%d).\n",               mh.group1_samplerate, MAX_SAMPLERATE);        return -1;    }    if (mh.access_unit_size > MAX_BLOCKSIZE) {        av_log(m->avctx, AV_LOG_ERROR,               "Block size %d is greater than the supported maximum (%d).\n",               mh.access_unit_size, MAX_BLOCKSIZE);        return -1;    }    if (mh.access_unit_size_pow2 > MAX_BLOCKSIZE_POW2) {        av_log(m->avctx, AV_LOG_ERROR,               "Block size pow2 %d is greater than the supported maximum (%d).\n",               mh.access_unit_size_pow2, MAX_BLOCKSIZE_POW2);        return -1;    }    if (mh.num_substreams == 0)        return -1;    if (mh.num_substreams > MAX_SUBSTREAMS) {        av_log(m->avctx, AV_LOG_ERROR,               "Number of substreams %d is larger than the maximum supported "               "by the decoder. %s\n", mh.num_substreams, sample_message);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -