1716 lines · 65.5 KB · C
RawDownload
/* * ATRAC3+ compatible decoder * * Copyright (c) 2010-2013 Maxim Poliakovski * * 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 * Bitstream parser for ATRAC3+ decoder. */ #include "libavutil/avassert.h"#include "avcodec.h"#include "get_bits.h"#include "atrac3plus.h"#include "atrac3plus_data.h" static VLCElem tables_data[154276];static VLC wl_vlc_tabs[4];static VLC sf_vlc_tabs[8];static VLC ct_vlc_tabs[4];static VLC spec_vlc_tabs[112];static VLC gain_vlc_tabs[11];static VLC tone_vlc_tabs[7]; /** * Generate canonical VLC table from given descriptor. * * @param[in]     cb          ptr to codebook descriptor * @param[in,out] xlat        ptr to ptr to translation table * @param[in,out] tab_offset  starting offset to the generated vlc table * @param[out]    out_vlc     ptr to vlc table to be generated */static av_cold void build_canonical_huff(const uint8_t *cb, const uint8_t **xlat,                                         int *tab_offset, VLC *out_vlc){    int i, max_len;    uint8_t bits[256];    int index = 0;     for (int b = 1; b <= 12; b++) {        for (i = *cb++; i > 0; i--) {            av_assert0(index < 256);            bits[index]  = b;            index++;        }    }    max_len = bits[index - 1];     out_vlc->table = &tables_data[*tab_offset];    out_vlc->table_allocated = 1 << max_len;     ff_vlc_init_from_lengths(out_vlc, max_len, index, bits, 1,                             *xlat, 1, 1, 0, VLC_INIT_USE_STATIC, NULL);     *tab_offset += 1 << max_len;    *xlat       += index;} av_cold void ff_atrac3p_init_vlcs(void){    int i, tab_offset = 0;    const uint8_t *xlats;     xlats = atrac3p_wl_ct_xlats;    for (int i = 0; i < 4; i++) {        build_canonical_huff(atrac3p_wl_cbs[i], &xlats,                             &tab_offset, &wl_vlc_tabs[i]);        build_canonical_huff(atrac3p_ct_cbs[i], &xlats,                             &tab_offset, &ct_vlc_tabs[i]);    }     xlats = atrac3p_sf_xlats;    for (int i = 0; i < 8; i++)        build_canonical_huff(atrac3p_sf_cbs[i], &xlats,                             &tab_offset, &sf_vlc_tabs[i]);     /* build huffman tables for spectrum decoding */    xlats = atrac3p_spectra_xlats;    for (i = 0; i < 112; i++) {        if (atrac3p_spectra_cbs[i][0] >= 0)            build_canonical_huff(atrac3p_spectra_cbs[i],                                 &xlats, &tab_offset, &spec_vlc_tabs[i]);        else /* Reuse already initialized VLC table */            spec_vlc_tabs[i] = spec_vlc_tabs[-atrac3p_spectra_cbs[i][0]];    }     /* build huffman tables for gain data decoding */    xlats = atrac3p_gain_xlats;    for (i = 0; i < 11; i++)        build_canonical_huff(atrac3p_gain_cbs[i], &xlats,                             &tab_offset, &gain_vlc_tabs[i]);     /* build huffman tables for tone decoding */    xlats = atrac3p_tone_xlats;    for (i = 0; i < 7; i++)        build_canonical_huff(atrac3p_tone_cbs[i], &xlats,                             &tab_offset, &tone_vlc_tabs[i]);} /** * Decode number of coded quantization units. * * @param[in]     gb            the GetBit context * @param[in,out] chan          ptr to the channel parameters * @param[in,out] ctx           ptr to the channel unit context * @param[in]     avctx         ptr to the AVCodecContext * @return result code: 0 = OK, otherwise - error code */static int num_coded_units(GetBitContext *gb, Atrac3pChanParams *chan,                           Atrac3pChanUnitCtx *ctx, AVCodecContext *avctx){    chan->fill_mode = get_bits(gb, 2);    if (!chan->fill_mode) {        chan->num_coded_vals = ctx->num_quant_units;    } else {        chan->num_coded_vals = get_bits(gb, 5);        if (chan->num_coded_vals > ctx->num_quant_units) {            av_log(avctx, AV_LOG_ERROR,                   "Invalid number of transmitted units!\n");            return AVERROR_INVALIDDATA;        }         if (chan->fill_mode == 3)            chan->split_point = get_bits(gb, 2) + (chan->ch_num << 1) + 1;    }     return 0;} /** * Add weighting coefficients to the decoded word-length information. * * @param[in,out] ctx           ptr to the channel unit context * @param[in,out] chan          ptr to the channel parameters * @param[in]     wtab_idx      index of the table of weights * @param[in]     avctx         ptr to the AVCodecContext * @return result code: 0 = OK, otherwise - error code */static int add_wordlen_weights(Atrac3pChanUnitCtx *ctx,                               Atrac3pChanParams *chan, int wtab_idx,                               AVCodecContext *avctx){    int i;    const int8_t *weights_tab =        &atrac3p_wl_weights[chan->ch_num * 3 + wtab_idx - 1][0];     for (i = 0; i < ctx->num_quant_units; i++) {        chan->qu_wordlen[i] += weights_tab[i];        if (chan->qu_wordlen[i] < 0 || chan->qu_wordlen[i] > 7) {            av_log(avctx, AV_LOG_ERROR,                   "WL index out of range: pos=%d, val=%d!\n",                   i, chan->qu_wordlen[i]);            return AVERROR_INVALIDDATA;        }    }     return 0;} /** * Subtract weighting coefficients from decoded scalefactors. * * @param[in,out] ctx           ptr to the channel unit context * @param[in,out] chan          ptr to the channel parameters * @param[in]     wtab_idx      index of table of weights * @param[in]     avctx         ptr to the AVCodecContext * @return result code: 0 = OK, otherwise - error code */static int subtract_sf_weights(Atrac3pChanUnitCtx *ctx,                               Atrac3pChanParams *chan, int wtab_idx,                               AVCodecContext *avctx){    int i;    const int8_t *weights_tab = &atrac3p_sf_weights[wtab_idx - 1][0];     for (i = 0; i < ctx->used_quant_units; i++) {        chan->qu_sf_idx[i] -= weights_tab[i];        if (chan->qu_sf_idx[i] < 0 || chan->qu_sf_idx[i] > 63) {            av_log(avctx, AV_LOG_ERROR,                   "SF index out of range: pos=%d, val=%d!\n",                   i, chan->qu_sf_idx[i]);            return AVERROR_INVALIDDATA;        }    }     return 0;} /** * Unpack vector quantization tables. * * @param[in]    start_val    start value for the unpacked table * @param[in]    shape_vec    ptr to table to unpack * @param[out]   dst          ptr to output array * @param[in]    num_values   number of values to unpack */static inline void unpack_vq_shape(int start_val, const int8_t *shape_vec,                                   int *dst, int num_values){    int i;     if (num_values) {        dst[0] = dst[1] = dst[2] = start_val;        for (i = 3; i < num_values; i++)            dst[i] = start_val - shape_vec[atrac3p_qu_num_to_seg[i] - 1];    }} #define UNPACK_SF_VQ_SHAPE(gb, dst, num_vals)                            \    start_val = get_bits((gb), 6);                                       \    unpack_vq_shape(start_val, &atrac3p_sf_shapes[get_bits((gb), 6)][0], \                    (dst), (num_vals)) /** * Decode word length for each quantization unit of a channel. * * @param[in]     gb            the GetBit context * @param[in,out] ctx           ptr to the channel unit context * @param[in]     ch_num        channel to process * @param[in]     avctx         ptr to the AVCodecContext * @return result code: 0 = OK, otherwise - error code */static int decode_channel_wordlen(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,                                  int ch_num, AVCodecContext *avctx){    int i, weight_idx = 0, delta, diff, pos, delta_bits, min_val, flag,        ret, start_val;    VLC *vlc_tab;    Atrac3pChanParams *chan     = &ctx->channels[ch_num];    Atrac3pChanParams *ref_chan = &ctx->channels[0];     chan->fill_mode = 0;     switch (get_bits(gb, 2)) { /* switch according to coding mode */    case 0: /* coded using constant number of bits */        for (i = 0; i < ctx->num_quant_units; i++)            chan->qu_wordlen[i] = get_bits(gb, 3);        break;    case 1:        if (ch_num) {            if ((ret = num_coded_units(gb, chan, ctx, avctx)) < 0)                return ret;             if (chan->num_coded_vals) {                vlc_tab = &wl_vlc_tabs[get_bits(gb, 2)];                 for (i = 0; i < chan->num_coded_vals; i++) {                    delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);                    chan->qu_wordlen[i] = (ref_chan->qu_wordlen[i] + delta) & 7;                }            }        } else {            weight_idx = get_bits(gb, 2);            if ((ret = num_coded_units(gb, chan, ctx, avctx)) < 0)                return ret;             if (chan->num_coded_vals) {                pos = get_bits(gb, 5);                if (pos > chan->num_coded_vals) {                    av_log(avctx, AV_LOG_ERROR,                           "WL mode 1: invalid position!\n");                    return AVERROR_INVALIDDATA;                }                 delta_bits = get_bits(gb, 2);                min_val    = get_bits(gb, 3);                 for (i = 0; i < pos; i++)                    chan->qu_wordlen[i] = get_bits(gb, 3);                 for (i = pos; i < chan->num_coded_vals; i++)                    chan->qu_wordlen[i] = (min_val + get_bitsz(gb, delta_bits)) & 7;            }        }        break;    case 2:        if ((ret = num_coded_units(gb, chan, ctx, avctx)) < 0)            return ret;         if (ch_num && chan->num_coded_vals) {            vlc_tab = &wl_vlc_tabs[get_bits(gb, 2)];            delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);            chan->qu_wordlen[0] = (ref_chan->qu_wordlen[0] + delta) & 7;             for (i = 1; i < chan->num_coded_vals; i++) {                diff = ref_chan->qu_wordlen[i] - ref_chan->qu_wordlen[i - 1];                delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);                chan->qu_wordlen[i] = (chan->qu_wordlen[i - 1] + diff + delta) & 7;            }        } else if (chan->num_coded_vals) {            flag    = get_bits(gb, 1);            vlc_tab = &wl_vlc_tabs[get_bits(gb, 1)];             start_val = get_bits(gb, 3);            unpack_vq_shape(start_val,                            &atrac3p_wl_shapes[start_val][get_bits(gb, 4)][0],                            chan->qu_wordlen, chan->num_coded_vals);             if (!flag) {                for (i = 0; i < chan->num_coded_vals; i++) {                    delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);                    chan->qu_wordlen[i] = (chan->qu_wordlen[i] + delta) & 7;                }            } else {                for (i = 0; i < (chan->num_coded_vals & - 2); i += 2)                    if (!get_bits1(gb)) {                        chan->qu_wordlen[i]     = (chan->qu_wordlen[i] +                                                   get_vlc2(gb, vlc_tab->table,                                                            vlc_tab->bits, 1)) & 7;                        chan->qu_wordlen[i + 1] = (chan->qu_wordlen[i + 1] +                                                   get_vlc2(gb, vlc_tab->table,                                                            vlc_tab->bits, 1)) & 7;                    }                 if (chan->num_coded_vals & 1)                    chan->qu_wordlen[i] = (chan->qu_wordlen[i] +                                           get_vlc2(gb, vlc_tab->table,                                                    vlc_tab->bits, 1)) & 7;            }        }        break;    case 3:        weight_idx = get_bits(gb, 2);        if ((ret = num_coded_units(gb, chan, ctx, avctx)) < 0)            return ret;         if (chan->num_coded_vals) {            vlc_tab = &wl_vlc_tabs[get_bits(gb, 2)];             /* first coefficient is coded directly */            chan->qu_wordlen[0] = get_bits(gb, 3);             for (i = 1; i < chan->num_coded_vals; i++) {                delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);                chan->qu_wordlen[i] = (chan->qu_wordlen[i - 1] + delta) & 7;            }        }        break;    }     if (chan->fill_mode == 2) {        for (i = chan->num_coded_vals; i < ctx->num_quant_units; i++)            chan->qu_wordlen[i] = ch_num ? get_bits1(gb) : 1;    } else if (chan->fill_mode == 3) {        pos = ch_num ? chan->num_coded_vals + chan->split_point                     : ctx->num_quant_units - chan->split_point;        if (pos > FF_ARRAY_ELEMS(chan->qu_wordlen)) {            av_log(avctx, AV_LOG_ERROR, "Split point beyond array\n");            pos = FF_ARRAY_ELEMS(chan->qu_wordlen);        }        for (i = chan->num_coded_vals; i < pos; i++)            chan->qu_wordlen[i] = 1;    }     if (weight_idx)        return add_wordlen_weights(ctx, chan, weight_idx, avctx);     return 0;} /** * Decode scale factor indexes for each quant unit of a channel. * * @param[in]     gb            the GetBit context * @param[in,out] ctx           ptr to the channel unit context * @param[in]     ch_num        channel to process * @param[in]     avctx         ptr to the AVCodecContext * @return result code: 0 = OK, otherwise - error code */static int decode_channel_sf_idx(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,                                 int ch_num, AVCodecContext *avctx){    int i, weight_idx = 0, delta, diff, num_long_vals,        delta_bits, min_val, vlc_sel, start_val;    VLC *vlc_tab;    Atrac3pChanParams *chan     = &ctx->channels[ch_num];    Atrac3pChanParams *ref_chan = &ctx->channels[0];     switch (get_bits(gb, 2)) { /* switch according to coding mode */    case 0: /* coded using constant number of bits */        for (i = 0; i < ctx->used_quant_units; i++)            chan->qu_sf_idx[i] = get_bits(gb, 6);        break;    case 1:        if (ch_num) {            vlc_tab = &sf_vlc_tabs[get_bits(gb, 2)];             for (i = 0; i < ctx->used_quant_units; i++) {                delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);                chan->qu_sf_idx[i] = (ref_chan->qu_sf_idx[i] + delta) & 0x3F;            }        } else {            weight_idx = get_bits(gb, 2);            if (weight_idx == 3) {                UNPACK_SF_VQ_SHAPE(gb, chan->qu_sf_idx, ctx->used_quant_units);                 num_long_vals = get_bits(gb, 5);                delta_bits    = get_bits(gb, 2);                min_val       = get_bits(gb, 4) - 7;                 for (i = 0; i < num_long_vals; i++)                    chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] +                                          get_bits(gb, 4) - 7) & 0x3F;                 /* all others are: min_val + delta */                for (i = num_long_vals; i < ctx->used_quant_units; i++)                    chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] + min_val +                                          get_bitsz(gb, delta_bits)) & 0x3F;            } else {                num_long_vals = get_bits(gb, 5);                delta_bits    = get_bits(gb, 3);                min_val       = get_bits(gb, 6);                if (num_long_vals > ctx->used_quant_units || delta_bits == 7) {                    av_log(avctx, AV_LOG_ERROR,                           "SF mode 1: invalid parameters!\n");                    return AVERROR_INVALIDDATA;                }                 /* read full-precision SF indexes */                for (i = 0; i < num_long_vals; i++)                    chan->qu_sf_idx[i] = get_bits(gb, 6);                 /* all others are: min_val + delta */                for (i = num_long_vals; i < ctx->used_quant_units; i++)                    chan->qu_sf_idx[i] = (min_val +                                          get_bitsz(gb, delta_bits)) & 0x3F;            }        }        break;    case 2:        if (ch_num) {            vlc_tab = &sf_vlc_tabs[get_bits(gb, 2)];             delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);            chan->qu_sf_idx[0] = (ref_chan->qu_sf_idx[0] + delta) & 0x3F;             for (i = 1; i < ctx->used_quant_units; i++) {                diff  = ref_chan->qu_sf_idx[i] - ref_chan->qu_sf_idx[i - 1];                delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);                chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + diff + delta) & 0x3F;            }        } else {            vlc_tab = &sf_vlc_tabs[get_bits(gb, 2) + 4];             UNPACK_SF_VQ_SHAPE(gb, chan->qu_sf_idx, ctx->used_quant_units);             for (i = 0; i < ctx->used_quant_units; i++) {                delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);                chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] +                                      sign_extend(delta, 4)) & 0x3F;            }        }        break;    case 3:        if (ch_num) {            /* copy coefficients from reference channel */            for (i = 0; i < ctx->used_quant_units; i++)                chan->qu_sf_idx[i] = ref_chan->qu_sf_idx[i];        } else {            weight_idx = get_bits(gb, 2);            vlc_sel    = get_bits(gb, 2);            vlc_tab    = &sf_vlc_tabs[vlc_sel];             if (weight_idx == 3) {                vlc_tab = &sf_vlc_tabs[vlc_sel + 4];                 UNPACK_SF_VQ_SHAPE(gb, chan->qu_sf_idx, ctx->used_quant_units);                 diff               = (get_bits(gb, 4)    + 56)   & 0x3F;                chan->qu_sf_idx[0] = (chan->qu_sf_idx[0] + diff) & 0x3F;                 for (i = 1; i < ctx->used_quant_units; i++) {                    delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);                    diff               = (diff + sign_extend(delta, 4)) & 0x3F;                    chan->qu_sf_idx[i] = (diff + chan->qu_sf_idx[i])    & 0x3F;                }            } else {                /* 1st coefficient is coded directly */                chan->qu_sf_idx[0] = get_bits(gb, 6);                 for (i = 1; i < ctx->used_quant_units; i++) {                    delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);                    chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + delta) & 0x3F;

Lines 1–500 of 1716.

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