1cabdff1aSopenharmony_ci/*
2cabdff1aSopenharmony_ci * WMA compatible codec
3cabdff1aSopenharmony_ci * Copyright (c) 2002-2007 The FFmpeg Project
4cabdff1aSopenharmony_ci *
5cabdff1aSopenharmony_ci * This file is part of FFmpeg.
6cabdff1aSopenharmony_ci *
7cabdff1aSopenharmony_ci * FFmpeg is free software; you can redistribute it and/or
8cabdff1aSopenharmony_ci * modify it under the terms of the GNU Lesser General Public
9cabdff1aSopenharmony_ci * License as published by the Free Software Foundation; either
10cabdff1aSopenharmony_ci * version 2.1 of the License, or (at your option) any later version.
11cabdff1aSopenharmony_ci *
12cabdff1aSopenharmony_ci * FFmpeg is distributed in the hope that it will be useful,
13cabdff1aSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
14cabdff1aSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15cabdff1aSopenharmony_ci * Lesser General Public License for more details.
16cabdff1aSopenharmony_ci *
17cabdff1aSopenharmony_ci * You should have received a copy of the GNU Lesser General Public
18cabdff1aSopenharmony_ci * License along with FFmpeg; if not, write to the Free Software
19cabdff1aSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20cabdff1aSopenharmony_ci */
21cabdff1aSopenharmony_ci
22cabdff1aSopenharmony_ci#ifndef AVCODEC_WMA_H
23cabdff1aSopenharmony_ci#define AVCODEC_WMA_H
24cabdff1aSopenharmony_ci
25cabdff1aSopenharmony_ci#include "libavutil/float_dsp.h"
26cabdff1aSopenharmony_ci#include "libavutil/mem_internal.h"
27cabdff1aSopenharmony_ci
28cabdff1aSopenharmony_ci#include "avcodec.h"
29cabdff1aSopenharmony_ci#include "fft.h"
30cabdff1aSopenharmony_ci#include "get_bits.h"
31cabdff1aSopenharmony_ci#include "put_bits.h"
32cabdff1aSopenharmony_ci
33cabdff1aSopenharmony_ci/* size of blocks */
34cabdff1aSopenharmony_ci#define BLOCK_MIN_BITS 7
35cabdff1aSopenharmony_ci#define BLOCK_MAX_BITS 11
36cabdff1aSopenharmony_ci#define BLOCK_MAX_SIZE (1 << BLOCK_MAX_BITS)
37cabdff1aSopenharmony_ci
38cabdff1aSopenharmony_ci#define BLOCK_NB_SIZES (BLOCK_MAX_BITS - BLOCK_MIN_BITS + 1)
39cabdff1aSopenharmony_ci
40cabdff1aSopenharmony_ci/* XXX: find exact max size */
41cabdff1aSopenharmony_ci#define HIGH_BAND_MAX_SIZE 16
42cabdff1aSopenharmony_ci
43cabdff1aSopenharmony_ci#define NB_LSP_COEFS 10
44cabdff1aSopenharmony_ci
45cabdff1aSopenharmony_ci/* XXX: is it a suitable value ? */
46cabdff1aSopenharmony_ci#define MAX_CODED_SUPERFRAME_SIZE 32768
47cabdff1aSopenharmony_ci
48cabdff1aSopenharmony_ci#define MAX_CHANNELS 2
49cabdff1aSopenharmony_ci
50cabdff1aSopenharmony_ci#define NOISE_TAB_SIZE 8192
51cabdff1aSopenharmony_ci
52cabdff1aSopenharmony_ci#define LSP_POW_BITS 7
53cabdff1aSopenharmony_ci
54cabdff1aSopenharmony_ci// FIXME should be in wmadec
55cabdff1aSopenharmony_ci#define VLCBITS 9
56cabdff1aSopenharmony_ci#define VLCMAX ((22 + VLCBITS - 1) / VLCBITS)
57cabdff1aSopenharmony_ci
58cabdff1aSopenharmony_citypedef float WMACoef;          ///< type for decoded coefficients, int16_t would be enough for wma 1/2
59cabdff1aSopenharmony_ci
60cabdff1aSopenharmony_citypedef struct CoefVLCTable {
61cabdff1aSopenharmony_ci    int n;                      ///< total number of codes
62cabdff1aSopenharmony_ci    int max_level;
63cabdff1aSopenharmony_ci    const uint32_t *huffcodes;  ///< VLC bit values
64cabdff1aSopenharmony_ci    const uint8_t *huffbits;    ///< VLC bit size
65cabdff1aSopenharmony_ci    const uint16_t *levels;     ///< table to build run/level tables
66cabdff1aSopenharmony_ci} CoefVLCTable;
67cabdff1aSopenharmony_ci
68cabdff1aSopenharmony_citypedef struct WMACodecContext {
69cabdff1aSopenharmony_ci    AVCodecContext *avctx;
70cabdff1aSopenharmony_ci    GetBitContext gb;
71cabdff1aSopenharmony_ci    PutBitContext pb;
72cabdff1aSopenharmony_ci    int version;                            ///< 1 = 0x160 (WMAV1), 2 = 0x161 (WMAV2)
73cabdff1aSopenharmony_ci    int use_bit_reservoir;
74cabdff1aSopenharmony_ci    int use_variable_block_len;
75cabdff1aSopenharmony_ci    int use_exp_vlc;                        ///< exponent coding: 0 = lsp, 1 = vlc + delta
76cabdff1aSopenharmony_ci    int use_noise_coding;                   ///< true if perceptual noise is added
77cabdff1aSopenharmony_ci    int byte_offset_bits;
78cabdff1aSopenharmony_ci    VLC exp_vlc;
79cabdff1aSopenharmony_ci    int exponent_sizes[BLOCK_NB_SIZES];
80cabdff1aSopenharmony_ci    uint16_t exponent_bands[BLOCK_NB_SIZES][25];
81cabdff1aSopenharmony_ci    int high_band_start[BLOCK_NB_SIZES];    ///< index of first coef in high band
82cabdff1aSopenharmony_ci    int coefs_start;                        ///< first coded coef
83cabdff1aSopenharmony_ci    int coefs_end[BLOCK_NB_SIZES];          ///< max number of coded coefficients
84cabdff1aSopenharmony_ci    int exponent_high_sizes[BLOCK_NB_SIZES];
85cabdff1aSopenharmony_ci    int exponent_high_bands[BLOCK_NB_SIZES][HIGH_BAND_MAX_SIZE];
86cabdff1aSopenharmony_ci    VLC hgain_vlc;
87cabdff1aSopenharmony_ci
88cabdff1aSopenharmony_ci    /* coded values in high bands */
89cabdff1aSopenharmony_ci    int high_band_coded[MAX_CHANNELS][HIGH_BAND_MAX_SIZE];
90cabdff1aSopenharmony_ci    int high_band_values[MAX_CHANNELS][HIGH_BAND_MAX_SIZE];
91cabdff1aSopenharmony_ci
92cabdff1aSopenharmony_ci    /* there are two possible tables for spectral coefficients */
93cabdff1aSopenharmony_ci// FIXME the following 3 tables should be shared between decoders
94cabdff1aSopenharmony_ci    VLC coef_vlc[2];
95cabdff1aSopenharmony_ci    uint16_t *run_table[2];
96cabdff1aSopenharmony_ci    float *level_table[2];
97cabdff1aSopenharmony_ci    uint16_t *int_table[2];
98cabdff1aSopenharmony_ci    const CoefVLCTable *coef_vlcs[2];
99cabdff1aSopenharmony_ci    /* frame info */
100cabdff1aSopenharmony_ci    int frame_len;                          ///< frame length in samples
101cabdff1aSopenharmony_ci    int frame_len_bits;                     ///< frame_len = 1 << frame_len_bits
102cabdff1aSopenharmony_ci    int nb_block_sizes;                     ///< number of block sizes
103cabdff1aSopenharmony_ci    /* block info */
104cabdff1aSopenharmony_ci    int reset_block_lengths;
105cabdff1aSopenharmony_ci    int block_len_bits;                     ///< log2 of current block length
106cabdff1aSopenharmony_ci    int next_block_len_bits;                ///< log2 of next block length
107cabdff1aSopenharmony_ci    int prev_block_len_bits;                ///< log2 of prev block length
108cabdff1aSopenharmony_ci    int block_len;                          ///< block length in samples
109cabdff1aSopenharmony_ci    int block_num;                          ///< block number in current frame
110cabdff1aSopenharmony_ci    int block_pos;                          ///< current position in frame
111cabdff1aSopenharmony_ci    uint8_t ms_stereo;                      ///< true if mid/side stereo mode
112cabdff1aSopenharmony_ci    uint8_t channel_coded[MAX_CHANNELS];    ///< true if channel is coded
113cabdff1aSopenharmony_ci    int exponents_bsize[MAX_CHANNELS];      ///< log2 ratio frame/exp. length
114cabdff1aSopenharmony_ci    DECLARE_ALIGNED(32, float, exponents)[MAX_CHANNELS][BLOCK_MAX_SIZE];
115cabdff1aSopenharmony_ci    float max_exponent[MAX_CHANNELS];
116cabdff1aSopenharmony_ci    WMACoef coefs1[MAX_CHANNELS][BLOCK_MAX_SIZE];
117cabdff1aSopenharmony_ci    DECLARE_ALIGNED(32, float, coefs)[MAX_CHANNELS][BLOCK_MAX_SIZE];
118cabdff1aSopenharmony_ci    DECLARE_ALIGNED(32, FFTSample, output)[BLOCK_MAX_SIZE * 2];
119cabdff1aSopenharmony_ci    FFTContext mdct_ctx[BLOCK_NB_SIZES];
120cabdff1aSopenharmony_ci    const float *windows[BLOCK_NB_SIZES];
121cabdff1aSopenharmony_ci    /* output buffer for one frame and the last for IMDCT windowing */
122cabdff1aSopenharmony_ci    DECLARE_ALIGNED(32, float, frame_out)[MAX_CHANNELS][BLOCK_MAX_SIZE * 2];
123cabdff1aSopenharmony_ci    /* last frame info */
124cabdff1aSopenharmony_ci    uint8_t last_superframe[MAX_CODED_SUPERFRAME_SIZE + AV_INPUT_BUFFER_PADDING_SIZE]; /* padding added */
125cabdff1aSopenharmony_ci    int last_bitoffset;
126cabdff1aSopenharmony_ci    int last_superframe_len;
127cabdff1aSopenharmony_ci    int exponents_initialized[MAX_CHANNELS];
128cabdff1aSopenharmony_ci    float noise_table[NOISE_TAB_SIZE];
129cabdff1aSopenharmony_ci    int noise_index;
130cabdff1aSopenharmony_ci    float noise_mult; /* XXX: suppress that and integrate it in the noise array */
131cabdff1aSopenharmony_ci    /* lsp_to_curve tables */
132cabdff1aSopenharmony_ci    float lsp_cos_table[BLOCK_MAX_SIZE];
133cabdff1aSopenharmony_ci    float lsp_pow_e_table[256];
134cabdff1aSopenharmony_ci    float lsp_pow_m_table1[(1 << LSP_POW_BITS)];
135cabdff1aSopenharmony_ci    float lsp_pow_m_table2[(1 << LSP_POW_BITS)];
136cabdff1aSopenharmony_ci    AVFloatDSPContext *fdsp;
137cabdff1aSopenharmony_ci
138cabdff1aSopenharmony_ci    int eof_done; /* decode flag to output remaining samples after EOF */
139cabdff1aSopenharmony_ci
140cabdff1aSopenharmony_ci#ifdef TRACE
141cabdff1aSopenharmony_ci    int frame_count;
142cabdff1aSopenharmony_ci#endif /* TRACE */
143cabdff1aSopenharmony_ci} WMACodecContext;
144cabdff1aSopenharmony_ci
145cabdff1aSopenharmony_ciextern const uint8_t ff_wma_hgain_hufftab[37][2];
146cabdff1aSopenharmony_ciextern const float ff_wma_lsp_codebook[NB_LSP_COEFS][16];
147cabdff1aSopenharmony_ciextern const uint32_t ff_aac_scalefactor_code[121];
148cabdff1aSopenharmony_ciextern const uint8_t  ff_aac_scalefactor_bits[121];
149cabdff1aSopenharmony_ci
150cabdff1aSopenharmony_ciav_warn_unused_result
151cabdff1aSopenharmony_ciint ff_wma_init(AVCodecContext *avctx, int flags2);
152cabdff1aSopenharmony_ci
153cabdff1aSopenharmony_ciint ff_wma_total_gain_to_bits(int total_gain);
154cabdff1aSopenharmony_ciint ff_wma_end(AVCodecContext *avctx);
155cabdff1aSopenharmony_ciunsigned int ff_wma_get_large_val(GetBitContext *gb);
156cabdff1aSopenharmony_ciint ff_wma_run_level_decode(AVCodecContext *avctx, GetBitContext *gb,
157cabdff1aSopenharmony_ci                            VLC *vlc, const float *level_table,
158cabdff1aSopenharmony_ci                            const uint16_t *run_table, int version,
159cabdff1aSopenharmony_ci                            WMACoef *ptr, int offset, int num_coefs,
160cabdff1aSopenharmony_ci                            int block_len, int frame_len_bits,
161cabdff1aSopenharmony_ci                            int coef_nb_bits);
162cabdff1aSopenharmony_ci
163cabdff1aSopenharmony_ci#endif /* AVCODEC_WMA_H */
164