28 #ifndef AVCODEC_AACENC_QUANTIZATION_H
29 #define AVCODEC_AACENC_QUANTIZATION_H
44 const float *scaled,
int size,
int scale_idx,
45 int cb,
const float lambda,
const float uplim,
46 int *
bits,
int BT_ZERO,
int BT_UNSIGNED,
47 int BT_PAIR,
int BT_ESC,
int BT_NOISE,
int BT_STEREO,
54 const float CLIPPED_ESCAPE = 165140.0f*IQ;
57 const int dim = BT_PAIR ? 2 : 4;
61 if (BT_ZERO || BT_NOISE || BT_STEREO) {
62 for (i = 0; i <
size; i++)
68 for (j = 0; j <
dim; j++)
83 for (i = 0; i <
size; i +=
dim) {
85 int *quants = s->
qcoefs + i;
88 float quantized, rd = 0.0f;
89 for (j = 0; j <
dim; j++) {
91 curidx += quants[j] + off;
96 for (j = 0; j <
dim; j++) {
97 float t = fabsf(in[i+j]);
99 if (BT_ESC && vec[j] == 64.0f) {
100 if (t >= CLIPPED_ESCAPE) {
101 quantized = CLIPPED_ESCAPE;
104 int c = av_clip_uintp2(
quant(t, Q, ROUNDING), 13);
105 quantized = c*
cbrtf(c)*IQ;
106 curbits +=
av_log2(c)*2 - 4 + 1;
109 quantized = vec[j]*IQ;
113 out[i+j] = in[i+j] >= 0 ? quantized : -quantized;
119 for (j = 0; j <
dim; j++) {
120 quantized = vec[j]*IQ;
122 out[i+j] = quantized;
123 rd += (in[i+j] - quantized)*(in[i+j] - quantized);
126 cost += rd * lambda + curbits;
133 for (j = 0; j <
dim; j++)
137 for (j = 0; j < 2; j++) {
139 int coef = av_clip_uintp2(
quant(fabsf(in[i+j]), Q, ROUNDING), 13);
142 put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
156 const float *
in,
float *
quant,
const float *scaled,
157 int size,
int scale_idx,
int cb,
158 const float lambda,
const float uplim,
164 #define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, ROUNDING) \
165 static float quantize_and_encode_band_cost_ ## NAME( \
166 struct AACEncContext *s, \
167 PutBitContext *pb, const float *in, float *quant, \
168 const float *scaled, int size, int scale_idx, \
169 int cb, const float lambda, const float uplim, \
171 return quantize_and_encode_band_cost_template( \
172 s, pb, in, quant, scaled, size, scale_idx, \
173 BT_ESC ? ESC_BT : cb, lambda, uplim, bits, \
174 BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, \
191 const
float *scaled,
int size,
int scale_idx,
192 int cb, const
float lambda, const
float uplim,
194 quantize_and_encode_band_cost_ZERO,
195 quantize_and_encode_band_cost_SQUAD,
196 quantize_and_encode_band_cost_SQUAD,
197 quantize_and_encode_band_cost_UQUAD,
198 quantize_and_encode_band_cost_UQUAD,
199 quantize_and_encode_band_cost_SPAIR,
200 quantize_and_encode_band_cost_SPAIR,
201 quantize_and_encode_band_cost_UPAIR,
202 quantize_and_encode_band_cost_UPAIR,
203 quantize_and_encode_band_cost_UPAIR,
204 quantize_and_encode_band_cost_UPAIR,
205 quantize_and_encode_band_cost_ESC,
207 quantize_and_encode_band_cost_NOISE,
208 quantize_and_encode_band_cost_STEREO,
209 quantize_and_encode_band_cost_STEREO,
215 const float *scaled,
int size,
int scale_idx,
216 int cb,
const float lambda,
const float uplim,
218 quantize_and_encode_band_cost_ZERO,
219 quantize_and_encode_band_cost_SQUAD,
220 quantize_and_encode_band_cost_SQUAD,
221 quantize_and_encode_band_cost_UQUAD,
222 quantize_and_encode_band_cost_UQUAD,
223 quantize_and_encode_band_cost_SPAIR,
224 quantize_and_encode_band_cost_SPAIR,
225 quantize_and_encode_band_cost_UPAIR,
226 quantize_and_encode_band_cost_UPAIR,
227 quantize_and_encode_band_cost_UPAIR,
228 quantize_and_encode_band_cost_UPAIR,
229 quantize_and_encode_band_cost_ESC_RTZ,
231 quantize_and_encode_band_cost_NOISE,
232 quantize_and_encode_band_cost_STEREO,
233 quantize_and_encode_band_cost_STEREO,
236 #define quantize_and_encode_band_cost( \
237 s, pb, in, quant, scaled, size, scale_idx, cb, \
238 lambda, uplim, bits, rtz) \
239 ((rtz) ? quantize_and_encode_band_cost_rtz_arr : quantize_and_encode_band_cost_arr)[cb]( \
240 s, pb, in, quant, scaled, size, scale_idx, cb, \
244 const float *scaled,
int size,
int scale_idx,
245 int cb,
const float lambda,
const float uplim,
249 cb, lambda, uplim, bits, rtz);
253 const float *
in,
float *
out,
int size,
int scale_idx,
254 int cb,
const float lambda,
int rtz)
static float quantize_and_encode_band_cost_NONE(struct AACEncContext *s, PutBitContext *pb, const float *in, float *quant, const float *scaled, int size, int scale_idx, int cb, const float lambda, const float uplim, int *bits)
static void put_sbits(PutBitContext *pb, int n, int32_t value)
static void abs_pow34_v(float *out, const float *in, const int size)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
#define quantize_and_encode_band_cost(s, pb, in, quant, scaled, size, scale_idx, cb, lambda, uplim, bits, rtz)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
static double cb(void *priv, double x, double y)
float ff_aac_pow34sf_tab[428]
int qcoefs[96]
quantized coefficients
static float(*const quantize_and_encode_band_cost_rtz_arr[])(struct AACEncContext *s, PutBitContext *pb, const float *in, float *quant, const float *scaled, int size, int scale_idx, int cb, const float lambda, const float uplim, int *bits)
const float *const ff_aac_codebook_vectors[]
#define POW_SF2_ZERO
ff_aac_pow2sf_tab index corresponding to pow(2, 0);
#define SCALE_DIV_512
scalefactor difference that corresponds to scale difference in 512 times
const uint8_t *const ff_aac_spectral_bits[11]
static const uint8_t aac_cb_range[12]
static void quantize_bands(int *out, const float *in, const float *scaled, int size, float Q34, int is_signed, int maxval, const float rounding)
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
static av_always_inline float cbrtf(float x)
static av_always_inline float quantize_and_encode_band_cost_template(struct AACEncContext *s, PutBitContext *pb, const float *in, float *out, const float *scaled, int size, int scale_idx, int cb, const float lambda, const float uplim, int *bits, int BT_ZERO, int BT_UNSIGNED, int BT_PAIR, int BT_ESC, int BT_NOISE, int BT_STEREO, const float ROUNDING)
Calculate rate distortion cost for quantizing with given codebook.
static float(*const quantize_and_encode_band_cost_arr[])(struct AACEncContext *s, PutBitContext *pb, const float *in, float *quant, const float *scaled, int size, int scale_idx, int cb, const float lambda, const float uplim, int *bits)
#define SCALE_ONE_POS
scalefactor index that corresponds to scale=1.0
static const uint8_t aac_cb_maxval[12]
float ff_aac_pow2sf_tab[428]
const uint16_t *const ff_aac_spectral_codes[11]
#define QUANTIZE_AND_ENCODE_BAND_COST_FUNC(NAME, BT_ZERO, BT_UNSIGNED, BT_PAIR, BT_ESC, BT_NOISE, BT_STEREO, ROUNDING)
static void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb, const float *in, float *out, int size, int scale_idx, int cb, const float lambda, int rtz)
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
float scoefs[1024]
scaled coefficients
static float quantize_band_cost(struct AACEncContext *s, const float *in, const float *scaled, int size, int scale_idx, int cb, const float lambda, const float uplim, int *bits, int rtz)