CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

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