FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
aacenc_tns.c
Go to the documentation of this file.
1 /*
2  * AAC encoder TNS
3  * Copyright (C) 2015 Rostislav Pehlivanov
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * AAC encoder temporal noise shaping
25  * @author Rostislav Pehlivanov ( atomnuker gmail com )
26  */
27 
28 #include "aacenc.h"
29 #include "aacenc_tns.h"
30 #include "aactab.h"
31 #include "aacenc_utils.h"
32 #include "aacenc_quantization.h"
33 
34 /**
35  * Encode TNS data.
36  * Coefficient compression saves a single bit per coefficient.
37  */
39 {
40  uint8_t u_coef;
41  const uint8_t coef_res = TNS_Q_BITS == 4;
42  int i, w, filt, coef_len, coef_compress = 0;
43  const int is8 = sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE;
44  TemporalNoiseShaping *tns = &sce->tns;
45 
46  if (!sce->tns.present)
47  return;
48 
49  for (i = 0; i < sce->ics.num_windows; i++) {
50  put_bits(&s->pb, 2 - is8, sce->tns.n_filt[i]);
51  if (tns->n_filt[i]) {
52  put_bits(&s->pb, 1, coef_res);
53  for (filt = 0; filt < tns->n_filt[i]; filt++) {
54  put_bits(&s->pb, 6 - 2 * is8, tns->length[i][filt]);
55  put_bits(&s->pb, 5 - 2 * is8, tns->order[i][filt]);
56  if (tns->order[i][filt]) {
57  put_bits(&s->pb, 1, !!tns->direction[i][filt]);
58  put_bits(&s->pb, 1, !!coef_compress);
59  coef_len = coef_res + 3 - coef_compress;
60  for (w = 0; w < tns->order[i][filt]; w++) {
61  u_coef = (tns->coef_idx[i][filt][w])&(~(~0<<coef_len));
62  put_bits(&s->pb, coef_len, u_coef);
63  }
64  }
65  }
66  }
67  }
68 }
69 
70 static inline void quantize_coefs(double *coef, int *idx, float *lpc, int order)
71 {
72  int i;
73  uint8_t u_coef;
74  const float *quant_arr = tns_tmp2_map[TNS_Q_BITS == 4];
75  const double iqfac_p = ((1 << (TNS_Q_BITS-1)) - 0.5)/(M_PI/2.0);
76  const double iqfac_m = ((1 << (TNS_Q_BITS-1)) + 0.5)/(M_PI/2.0);
77  for (i = 0; i < order; i++) {
78  idx[i] = ceilf(asin(coef[i])*((coef[i] >= 0) ? iqfac_p : iqfac_m));
79  u_coef = (idx[i])&(~(~0<<TNS_Q_BITS));
80  lpc[i] = quant_arr[u_coef];
81  }
82 }
83 
84 /* Apply TNS filter */
86 {
87  TemporalNoiseShaping *tns = &sce->tns;
88  IndividualChannelStream *ics = &sce->ics;
89  int w, filt, m, i, top, order, bottom, start, end, size, inc;
90  const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb);
91  float lpc[TNS_MAX_ORDER];
92 
93  for (w = 0; w < ics->num_windows; w++) {
94  bottom = ics->num_swb;
95  for (filt = 0; filt < tns->n_filt[w]; filt++) {
96  top = bottom;
97  bottom = FFMAX(0, top - tns->length[w][filt]);
98  order = tns->order[w][filt];
99  if (order == 0)
100  continue;
101 
102  // tns_decode_coef
103  compute_lpc_coefs(tns->coef[w][filt], order, lpc, 0, 0, 0);
104 
105  start = ics->swb_offset[FFMIN(bottom, mmm)];
106  end = ics->swb_offset[FFMIN( top, mmm)];
107  if ((size = end - start) <= 0)
108  continue;
109  if (tns->direction[w][filt]) {
110  inc = -1;
111  start = end - 1;
112  } else {
113  inc = 1;
114  }
115  start += w * 128;
116 
117  // ar filter
118  for (m = 0; m < size; m++, start += inc)
119  for (i = 1; i <= FFMIN(m, order); i++)
120  sce->coeffs[start] += lpc[i-1]*sce->pcoeffs[start - i*inc];
121  }
122  }
123 }
124 
126 {
127  TemporalNoiseShaping *tns = &sce->tns;
128  int w, w2, g, count = 0;
129  const int mmm = FFMIN(sce->ics.tns_max_bands, sce->ics.max_sfb);
130  const int is8 = sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE;
131  const int order = is8 ? 7 : s->profile == FF_PROFILE_AAC_LOW ? 12 : TNS_MAX_ORDER;
132 
133  int sfb_start = av_clip(tns_min_sfb[is8][s->samplerate_index], 0, mmm);
134  int sfb_end = av_clip(sce->ics.num_swb, 0, mmm);
135 
136  for (w = 0; w < sce->ics.num_windows; w++) {
137  float e_ratio = 0.0f, threshold = 0.0f, spread = 0.0f, en[2] = {0.0, 0.0f};
138  double gain = 0.0f, coefs[MAX_LPC_ORDER] = {0};
139  int coef_start = w*sce->ics.num_swb + sce->ics.swb_offset[sfb_start];
140  int coef_len = sce->ics.swb_offset[sfb_end] - sce->ics.swb_offset[sfb_start];
141 
142  for (g = 0; g < sce->ics.num_swb; g++) {
143  if (w*16+g < sfb_start || w*16+g > sfb_end)
144  continue;
145  for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
146  FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
147  if ((w+w2)*16+g > sfb_start + ((sfb_end - sfb_start)/2))
148  en[1] += band->energy;
149  else
150  en[0] += band->energy;
151  threshold += band->threshold;
152  spread += band->spread;
153  }
154  }
155 
156  if (coef_len <= 0 || (sfb_end - sfb_start) <= 0)
157  continue;
158  else
159  e_ratio = en[0]/en[1];
160 
161  /* LPC */
162  gain = ff_lpc_calc_ref_coefs_f(&s->lpc, &sce->coeffs[coef_start],
163  coef_len, order, coefs);
164 
165  if (gain > TNS_GAIN_THRESHOLD_LOW && gain < TNS_GAIN_THRESHOLD_HIGH &&
166  (en[0]+en[1]) > TNS_GAIN_THRESHOLD_LOW*threshold &&
167  spread < TNS_SPREAD_THRESHOLD && order) {
168  if (is8 || order < 2 || (e_ratio > TNS_E_RATIO_LOW && e_ratio < TNS_E_RATIO_HIGH)) {
169  tns->n_filt[w] = 1;
170  for (g = 0; g < tns->n_filt[w]; g++) {
171  tns->length[w][g] = sfb_end - sfb_start;
172  tns->direction[w][g] = en[0] < en[1];
173  tns->order[w][g] = order;
174  quantize_coefs(coefs, tns->coef_idx[w][g], tns->coef[w][g],
175  order);
176  }
177  } else { /* 2 filters due to energy disbalance */
178  tns->n_filt[w] = 2;
179  for (g = 0; g < tns->n_filt[w]; g++) {
180  tns->direction[w][g] = en[g] < en[!g];
181  tns->order[w][g] = !g ? order/2 : order - tns->order[w][g-1];
182  tns->length[w][g] = !g ? (sfb_end - sfb_start)/2 : \
183  (sfb_end - sfb_start) - tns->length[w][g-1];
184  quantize_coefs(&coefs[!g ? 0 : order - tns->order[w][g-1]],
185  tns->coef_idx[w][g], tns->coef[w][g],
186  tns->order[w][g]);
187  }
188  }
189  count++;
190  }
191  }
192 
193  sce->tns.present = !!count;
194 }
static const uint8_t *const tns_min_sfb[2]
Definition: aacenctab.h:97
const char * s
Definition: avisynth_c.h:631
#define TNS_SPREAD_THRESHOLD
Definition: aacenc_tns.h:46
int coef_idx[8][4][TNS_MAX_ORDER]
Definition: aac.h:201
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:167
float pcoeffs[1024]
coefficients for IMDCT, pristine
Definition: aac.h:257
#define MAX_LPC_ORDER
Definition: lpc.h:38
const char * g
Definition: vf_curves.c:108
FFPsyBand psy_bands[PSY_MAX_BANDS]
channel bands information
Definition: psymodel.h:48
void ff_aac_encode_tns_info(AACEncContext *s, SingleChannelElement *sce)
Encode TNS data.
Definition: aacenc_tns.c:38
#define TNS_GAIN_THRESHOLD_LOW
Definition: aacenc_tns.h:37
AAC encoder quantizer.
const uint16_t * swb_offset
table of offsets to the lowest spectral coefficient of a scalefactor band, sfb, for a particular wind...
Definition: aac.h:178
AAC encoder context.
Definition: aacenc.h:80
uint8_t
LPCContext lpc
used by TNS
Definition: aacenc.h:90
int samplerate_index
MPEG-4 samplerate index.
Definition: aacenc.h:91
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
TemporalNoiseShaping tns
Definition: aac.h:247
int n_filt[8]
Definition: aac.h:197
single band psychoacoustic information
Definition: psymodel.h:37
int profile
copied from avctx
Definition: aacenc.h:89
ptrdiff_t size
Definition: opengl_enc.c:101
unsigned m
Definition: audioconvert.c:187
#define TNS_E_RATIO_LOW
Definition: aacenc_tns.h:42
float coeffs[1024]
coefficients for IMDCT, maybe processed
Definition: aac.h:258
void ff_aac_search_for_tns(AACEncContext *s, SingleChannelElement *sce)
Definition: aacenc_tns.c:125
void ff_aac_apply_tns(AACEncContext *s, SingleChannelElement *sce)
Definition: aacenc_tns.c:85
uint8_t max_sfb
number of scalefactor bands per group
Definition: aac.h:172
float energy
Definition: psymodel.h:39
GLsizei count
Definition: opengl_enc.c:109
int num_swb
number of scalefactor window bands
Definition: aac.h:180
#define FFMAX(a, b)
Definition: common.h:79
enum WindowSequence window_sequence[2]
Definition: aac.h:173
int cur_channel
Definition: aacenc.h:99
#define FFMIN(a, b)
Definition: common.h:81
int length[8][4]
Definition: aac.h:198
static int AAC_RENAME() compute_lpc_coefs(const LPC_TYPE *autoc, int max_order, LPC_TYPE *lpc, int lpc_stride, int fail, int normalize)
Levinson-Durbin recursion.
Definition: lpc.h:163
PutBitContext pb
Definition: aacenc.h:83
static const INTFLOAT *const tns_tmp2_map[4]
Definition: aactab.h:71
#define TNS_Q_BITS
Definition: aacenc_tns.h:34
int order[8][4]
Definition: aac.h:200
Temporal Noise Shaping.
Definition: aac.h:195
#define TNS_GAIN_THRESHOLD_HIGH
Definition: aacenc_tns.h:38
#define FF_PROFILE_AAC_LOW
Definition: avcodec.h:3120
IndividualChannelStream ics
Definition: aac.h:246
uint8_t group_len[8]
Definition: aac.h:176
double ff_lpc_calc_ref_coefs_f(LPCContext *s, const float *samples, int len, int order, double *ref)
Definition: lpc.c:170
#define TNS_MAX_ORDER
Definition: aac.h:50
FFPsyContext psy
Definition: aacenc.h:96
static const int8_t filt[NUMTAPS]
Definition: af_earwax.c:39
INTFLOAT coef[8][4][TNS_MAX_ORDER]
Definition: aac.h:202
int direction[8][4]
Definition: aac.h:199
AAC encoder utilities.
Single Channel Element - used for both SCE and LFE elements.
Definition: aac.h:245
Individual Channel Stream.
Definition: aac.h:171
#define TNS_E_RATIO_HIGH
Definition: aacenc_tns.h:43
FFPsyChannel * ch
single channel information
Definition: psymodel.h:80
AAC encoder temporal noise shaping.
void INT64 start
Definition: avisynth_c.h:553
#define M_PI
Definition: mathematics.h:46
float threshold
Definition: psymodel.h:40
AAC data declarations.
static void quantize_coefs(double *coef, int *idx, float *lpc, int order)
Definition: aacenc_tns.c:70
float spread
Definition: psymodel.h:41