1cabdff1aSopenharmony_ci/* 2cabdff1aSopenharmony_ci * ATRAC3+ compatible decoder 3cabdff1aSopenharmony_ci * 4cabdff1aSopenharmony_ci * Copyright (c) 2010-2013 Maxim Poliakovski 5cabdff1aSopenharmony_ci * 6cabdff1aSopenharmony_ci * This file is part of FFmpeg. 7cabdff1aSopenharmony_ci * 8cabdff1aSopenharmony_ci * FFmpeg is free software; you can redistribute it and/or 9cabdff1aSopenharmony_ci * modify it under the terms of the GNU Lesser General Public 10cabdff1aSopenharmony_ci * License as published by the Free Software Foundation; either 11cabdff1aSopenharmony_ci * version 2.1 of the License, or (at your option) any later version. 12cabdff1aSopenharmony_ci * 13cabdff1aSopenharmony_ci * FFmpeg is distributed in the hope that it will be useful, 14cabdff1aSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 15cabdff1aSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16cabdff1aSopenharmony_ci * Lesser General Public License for more details. 17cabdff1aSopenharmony_ci * 18cabdff1aSopenharmony_ci * You should have received a copy of the GNU Lesser General Public 19cabdff1aSopenharmony_ci * License along with FFmpeg; if not, write to the Free Software 20cabdff1aSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 21cabdff1aSopenharmony_ci */ 22cabdff1aSopenharmony_ci 23cabdff1aSopenharmony_ci/** 24cabdff1aSopenharmony_ci * @file 25cabdff1aSopenharmony_ci * Global structures, constants and data for ATRAC3+ decoder. 26cabdff1aSopenharmony_ci */ 27cabdff1aSopenharmony_ci 28cabdff1aSopenharmony_ci#ifndef AVCODEC_ATRAC3PLUS_H 29cabdff1aSopenharmony_ci#define AVCODEC_ATRAC3PLUS_H 30cabdff1aSopenharmony_ci 31cabdff1aSopenharmony_ci#include <stdint.h> 32cabdff1aSopenharmony_ci 33cabdff1aSopenharmony_ci#include "libavutil/float_dsp.h" 34cabdff1aSopenharmony_ci#include "libavutil/mem_internal.h" 35cabdff1aSopenharmony_ci 36cabdff1aSopenharmony_ci#include "atrac.h" 37cabdff1aSopenharmony_ci#include "avcodec.h" 38cabdff1aSopenharmony_ci#include "fft.h" 39cabdff1aSopenharmony_ci#include "get_bits.h" 40cabdff1aSopenharmony_ci 41cabdff1aSopenharmony_ci/** Global unit sizes */ 42cabdff1aSopenharmony_ci#define ATRAC3P_SUBBANDS 16 ///< number of PQF subbands 43cabdff1aSopenharmony_ci#define ATRAC3P_SUBBAND_SAMPLES 128 ///< number of samples per subband 44cabdff1aSopenharmony_ci#define ATRAC3P_FRAME_SAMPLES (ATRAC3P_SUBBAND_SAMPLES * ATRAC3P_SUBBANDS) 45cabdff1aSopenharmony_ci 46cabdff1aSopenharmony_ci#define ATRAC3P_PQF_FIR_LEN 12 ///< length of the prototype FIR of the PQF 47cabdff1aSopenharmony_ci 48cabdff1aSopenharmony_ci/** Global constants */ 49cabdff1aSopenharmony_ci#define ATRAC3P_POWER_COMP_OFF 15 ///< disable power compensation 50cabdff1aSopenharmony_ci 51cabdff1aSopenharmony_ci/** ATRAC3+ channel unit types */ 52cabdff1aSopenharmony_cienum Atrac3pChannelUnitTypes { 53cabdff1aSopenharmony_ci CH_UNIT_MONO = 0, ///< unit containing one coded channel 54cabdff1aSopenharmony_ci CH_UNIT_STEREO = 1, ///< unit containing two jointly-coded channels 55cabdff1aSopenharmony_ci CH_UNIT_EXTENSION = 2, ///< unit containing extension information 56cabdff1aSopenharmony_ci CH_UNIT_TERMINATOR = 3 ///< unit sequence terminator 57cabdff1aSopenharmony_ci}; 58cabdff1aSopenharmony_ci 59cabdff1aSopenharmony_ci/** Per-channel IPQF history */ 60cabdff1aSopenharmony_citypedef struct Atrac3pIPQFChannelCtx { 61cabdff1aSopenharmony_ci DECLARE_ALIGNED(32, float, buf1)[ATRAC3P_PQF_FIR_LEN * 2][8]; 62cabdff1aSopenharmony_ci DECLARE_ALIGNED(32, float, buf2)[ATRAC3P_PQF_FIR_LEN * 2][8]; 63cabdff1aSopenharmony_ci int pos; 64cabdff1aSopenharmony_ci} Atrac3pIPQFChannelCtx; 65cabdff1aSopenharmony_ci 66cabdff1aSopenharmony_ci/** Amplitude envelope of a group of sine waves */ 67cabdff1aSopenharmony_citypedef struct Atrac3pWaveEnvelope { 68cabdff1aSopenharmony_ci int has_start_point; ///< indicates start point within the GHA window 69cabdff1aSopenharmony_ci int has_stop_point; ///< indicates stop point within the GHA window 70cabdff1aSopenharmony_ci int start_pos; ///< start position expressed in n*4 samples 71cabdff1aSopenharmony_ci int stop_pos; ///< stop position expressed in n*4 samples 72cabdff1aSopenharmony_ci} Atrac3pWaveEnvelope; 73cabdff1aSopenharmony_ci 74cabdff1aSopenharmony_ci/** Parameters of a group of sine waves */ 75cabdff1aSopenharmony_citypedef struct Atrac3pWavesData { 76cabdff1aSopenharmony_ci Atrac3pWaveEnvelope pend_env; ///< pending envelope from the previous frame 77cabdff1aSopenharmony_ci Atrac3pWaveEnvelope curr_env; ///< group envelope from the current frame 78cabdff1aSopenharmony_ci int num_wavs; ///< number of sine waves in the group 79cabdff1aSopenharmony_ci int start_index; ///< start index into global tones table for that subband 80cabdff1aSopenharmony_ci} Atrac3pWavesData; 81cabdff1aSopenharmony_ci 82cabdff1aSopenharmony_ci/** Parameters of a single sine wave */ 83cabdff1aSopenharmony_citypedef struct Atrac3pWaveParam { 84cabdff1aSopenharmony_ci int freq_index; ///< wave frequency index 85cabdff1aSopenharmony_ci int amp_sf; ///< quantized amplitude scale factor 86cabdff1aSopenharmony_ci int amp_index; ///< quantized amplitude index 87cabdff1aSopenharmony_ci int phase_index; ///< quantized phase index 88cabdff1aSopenharmony_ci} Atrac3pWaveParam; 89cabdff1aSopenharmony_ci 90cabdff1aSopenharmony_ci/** Sound channel parameters */ 91cabdff1aSopenharmony_citypedef struct Atrac3pChanParams { 92cabdff1aSopenharmony_ci int ch_num; 93cabdff1aSopenharmony_ci int num_coded_vals; ///< number of transmitted quant unit values 94cabdff1aSopenharmony_ci int fill_mode; 95cabdff1aSopenharmony_ci int split_point; 96cabdff1aSopenharmony_ci int table_type; ///< table type: 0 - tone?, 1- noise? 97cabdff1aSopenharmony_ci int qu_wordlen[32]; ///< array of word lengths for each quant unit 98cabdff1aSopenharmony_ci int qu_sf_idx[32]; ///< array of scale factor indexes for each quant unit 99cabdff1aSopenharmony_ci int qu_tab_idx[32]; ///< array of code table indexes for each quant unit 100cabdff1aSopenharmony_ci int16_t spectrum[2048]; ///< decoded IMDCT spectrum 101cabdff1aSopenharmony_ci uint8_t power_levs[5]; ///< power compensation levels 102cabdff1aSopenharmony_ci 103cabdff1aSopenharmony_ci /* imdct window shape history (2 frames) for overlapping. */ 104cabdff1aSopenharmony_ci uint8_t wnd_shape_hist[2][ATRAC3P_SUBBANDS]; ///< IMDCT window shape, 0=sine/1=steep 105cabdff1aSopenharmony_ci uint8_t *wnd_shape; ///< IMDCT window shape for current frame 106cabdff1aSopenharmony_ci uint8_t *wnd_shape_prev; ///< IMDCT window shape for previous frame 107cabdff1aSopenharmony_ci 108cabdff1aSopenharmony_ci /* gain control data history (2 frames) for overlapping. */ 109cabdff1aSopenharmony_ci AtracGainInfo gain_data_hist[2][ATRAC3P_SUBBANDS]; ///< gain control data for all subbands 110cabdff1aSopenharmony_ci AtracGainInfo *gain_data; ///< gain control data for next frame 111cabdff1aSopenharmony_ci AtracGainInfo *gain_data_prev; ///< gain control data for previous frame 112cabdff1aSopenharmony_ci int num_gain_subbands; ///< number of subbands with gain control data 113cabdff1aSopenharmony_ci 114cabdff1aSopenharmony_ci /* tones data history (2 frames) for overlapping. */ 115cabdff1aSopenharmony_ci Atrac3pWavesData tones_info_hist[2][ATRAC3P_SUBBANDS]; 116cabdff1aSopenharmony_ci Atrac3pWavesData *tones_info; 117cabdff1aSopenharmony_ci Atrac3pWavesData *tones_info_prev; 118cabdff1aSopenharmony_ci} Atrac3pChanParams; 119cabdff1aSopenharmony_ci 120cabdff1aSopenharmony_ci/* Per-unit sine wave parameters */ 121cabdff1aSopenharmony_citypedef struct Atrac3pWaveSynthParams { 122cabdff1aSopenharmony_ci int tones_present; ///< 1 - tones info present 123cabdff1aSopenharmony_ci int amplitude_mode; ///< 1 - low range, 0 - high range 124cabdff1aSopenharmony_ci int num_tone_bands; ///< number of PQF bands with tones 125cabdff1aSopenharmony_ci uint8_t tone_sharing[ATRAC3P_SUBBANDS]; ///< 1 - subband-wise tone sharing flags 126cabdff1aSopenharmony_ci uint8_t tone_master[ATRAC3P_SUBBANDS]; ///< 1 - subband-wise tone channel swapping 127cabdff1aSopenharmony_ci uint8_t invert_phase[ATRAC3P_SUBBANDS]; ///< 1 - subband-wise phase inversion 128cabdff1aSopenharmony_ci int tones_index; ///< total sum of tones in this unit 129cabdff1aSopenharmony_ci Atrac3pWaveParam waves[48]; 130cabdff1aSopenharmony_ci} Atrac3pWaveSynthParams; 131cabdff1aSopenharmony_ci 132cabdff1aSopenharmony_ci/** Channel unit parameters */ 133cabdff1aSopenharmony_citypedef struct Atrac3pChanUnitCtx { 134cabdff1aSopenharmony_ci /* channel unit variables */ 135cabdff1aSopenharmony_ci int unit_type; ///< unit type (mono/stereo) 136cabdff1aSopenharmony_ci int num_quant_units; 137cabdff1aSopenharmony_ci int num_subbands; 138cabdff1aSopenharmony_ci int used_quant_units; ///< number of quant units with coded spectrum 139cabdff1aSopenharmony_ci int num_coded_subbands; ///< number of subbands with coded spectrum 140cabdff1aSopenharmony_ci int mute_flag; ///< mute flag 141cabdff1aSopenharmony_ci int use_full_table; ///< 1 - full table list, 0 - restricted one 142cabdff1aSopenharmony_ci int noise_present; ///< 1 - global noise info present 143cabdff1aSopenharmony_ci int noise_level_index; ///< global noise level index 144cabdff1aSopenharmony_ci int noise_table_index; ///< global noise RNG table index 145cabdff1aSopenharmony_ci uint8_t swap_channels[ATRAC3P_SUBBANDS]; ///< 1 - perform subband-wise channel swapping 146cabdff1aSopenharmony_ci uint8_t negate_coeffs[ATRAC3P_SUBBANDS]; ///< 1 - subband-wise IMDCT coefficients negation 147cabdff1aSopenharmony_ci Atrac3pChanParams channels[2]; 148cabdff1aSopenharmony_ci 149cabdff1aSopenharmony_ci /* Variables related to GHA tones */ 150cabdff1aSopenharmony_ci Atrac3pWaveSynthParams wave_synth_hist[2]; ///< waves synth history for two frames 151cabdff1aSopenharmony_ci Atrac3pWaveSynthParams *waves_info; 152cabdff1aSopenharmony_ci Atrac3pWaveSynthParams *waves_info_prev; 153cabdff1aSopenharmony_ci 154cabdff1aSopenharmony_ci Atrac3pIPQFChannelCtx ipqf_ctx[2]; 155cabdff1aSopenharmony_ci DECLARE_ALIGNED(32, float, prev_buf)[2][ATRAC3P_FRAME_SAMPLES]; ///< overlapping buffer 156cabdff1aSopenharmony_ci} Atrac3pChanUnitCtx; 157cabdff1aSopenharmony_ci 158cabdff1aSopenharmony_ci/** 159cabdff1aSopenharmony_ci * Initialize VLC tables for bitstream parsing. 160cabdff1aSopenharmony_ci */ 161cabdff1aSopenharmony_civoid ff_atrac3p_init_vlcs(void); 162cabdff1aSopenharmony_ci 163cabdff1aSopenharmony_ci/** 164cabdff1aSopenharmony_ci * Decode bitstream data of a channel unit. 165cabdff1aSopenharmony_ci * 166cabdff1aSopenharmony_ci * @param[in] gb the GetBit context 167cabdff1aSopenharmony_ci * @param[in,out] ctx ptr to the channel unit context 168cabdff1aSopenharmony_ci * @param[in] num_channels number of channels to process 169cabdff1aSopenharmony_ci * @param[in] avctx ptr to the AVCodecContext 170cabdff1aSopenharmony_ci * @return result code: 0 = OK, otherwise - error code 171cabdff1aSopenharmony_ci */ 172cabdff1aSopenharmony_ciint ff_atrac3p_decode_channel_unit(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, 173cabdff1aSopenharmony_ci int num_channels, AVCodecContext *avctx); 174cabdff1aSopenharmony_ci 175cabdff1aSopenharmony_ci/** 176cabdff1aSopenharmony_ci * Initialize IMDCT transform. 177cabdff1aSopenharmony_ci * 178cabdff1aSopenharmony_ci * @param[in] avctx ptr to the AVCodecContext 179cabdff1aSopenharmony_ci * @param[in] mdct_ctx pointer to MDCT transform context 180cabdff1aSopenharmony_ci */ 181cabdff1aSopenharmony_civoid ff_atrac3p_init_imdct(AVCodecContext *avctx, FFTContext *mdct_ctx); 182cabdff1aSopenharmony_ci 183cabdff1aSopenharmony_ci/** 184cabdff1aSopenharmony_ci * Initialize sine waves synthesizer and ff_sine_* tables. 185cabdff1aSopenharmony_ci */ 186cabdff1aSopenharmony_civoid ff_atrac3p_init_dsp_static(void); 187cabdff1aSopenharmony_ci 188cabdff1aSopenharmony_ci/** 189cabdff1aSopenharmony_ci * Synthesize sine waves for a particular subband. 190cabdff1aSopenharmony_ci * 191cabdff1aSopenharmony_ci * @param[in] ch_unit pointer to the channel unit context 192cabdff1aSopenharmony_ci * @param[in] fdsp pointer to float DSP context 193cabdff1aSopenharmony_ci * @param[in] ch_num which channel to process 194cabdff1aSopenharmony_ci * @param[in] sb which subband to process 195cabdff1aSopenharmony_ci * @param[out] out receives processed data 196cabdff1aSopenharmony_ci */ 197cabdff1aSopenharmony_civoid ff_atrac3p_generate_tones(Atrac3pChanUnitCtx *ch_unit, AVFloatDSPContext *fdsp, 198cabdff1aSopenharmony_ci int ch_num, int sb, float *out); 199cabdff1aSopenharmony_ci 200cabdff1aSopenharmony_ci/** 201cabdff1aSopenharmony_ci * Perform power compensation aka noise dithering. 202cabdff1aSopenharmony_ci * 203cabdff1aSopenharmony_ci * @param[in] ctx ptr to the channel context 204cabdff1aSopenharmony_ci * @param[in] fdsp pointer to float DSP context 205cabdff1aSopenharmony_ci * @param[in] ch_index which channel to process 206cabdff1aSopenharmony_ci * @param[in,out] sp ptr to channel spectrum to process 207cabdff1aSopenharmony_ci * @param[in] rng_index indicates which RNG table to use 208cabdff1aSopenharmony_ci * @param[in] sb_num which subband to process 209cabdff1aSopenharmony_ci */ 210cabdff1aSopenharmony_civoid ff_atrac3p_power_compensation(Atrac3pChanUnitCtx *ctx, AVFloatDSPContext *fdsp, 211cabdff1aSopenharmony_ci int ch_index, float *sp, int rng_index, int sb_num); 212cabdff1aSopenharmony_ci 213cabdff1aSopenharmony_ci/** 214cabdff1aSopenharmony_ci * Regular IMDCT and windowing without overlapping, 215cabdff1aSopenharmony_ci * with spectrum reversal in the odd subbands. 216cabdff1aSopenharmony_ci * 217cabdff1aSopenharmony_ci * @param[in] fdsp pointer to float DSP context 218cabdff1aSopenharmony_ci * @param[in] mdct_ctx pointer to MDCT transform context 219cabdff1aSopenharmony_ci * @param[in] pIn float input 220cabdff1aSopenharmony_ci * @param[out] pOut float output 221cabdff1aSopenharmony_ci * @param[in] wind_id which MDCT window to apply 222cabdff1aSopenharmony_ci * @param[in] sb subband number 223cabdff1aSopenharmony_ci */ 224cabdff1aSopenharmony_civoid ff_atrac3p_imdct(AVFloatDSPContext *fdsp, FFTContext *mdct_ctx, float *pIn, 225cabdff1aSopenharmony_ci float *pOut, int wind_id, int sb); 226cabdff1aSopenharmony_ci 227cabdff1aSopenharmony_ci/** 228cabdff1aSopenharmony_ci * Subband synthesis filter based on the polyphase quadrature (pseudo-QMF) 229cabdff1aSopenharmony_ci * filter bank. 230cabdff1aSopenharmony_ci * 231cabdff1aSopenharmony_ci * @param[in] dct_ctx ptr to the pre-initialized IDCT context 232cabdff1aSopenharmony_ci * @param[in,out] hist ptr to the filter history 233cabdff1aSopenharmony_ci * @param[in] in input data to process 234cabdff1aSopenharmony_ci * @param[out] out receives processed data 235cabdff1aSopenharmony_ci */ 236cabdff1aSopenharmony_civoid ff_atrac3p_ipqf(FFTContext *dct_ctx, Atrac3pIPQFChannelCtx *hist, 237cabdff1aSopenharmony_ci const float *in, float *out); 238cabdff1aSopenharmony_ci 239cabdff1aSopenharmony_ciextern const uint16_t ff_atrac3p_qu_to_spec_pos[33]; 240cabdff1aSopenharmony_ciextern const float ff_atrac3p_sf_tab[64]; 241cabdff1aSopenharmony_ciextern const float ff_atrac3p_mant_tab[8]; 242cabdff1aSopenharmony_ci 243cabdff1aSopenharmony_ci#endif /* AVCODEC_ATRAC3PLUS_H */ 244