fixed merging
[vlc.git] / extras / contrib / src / Patches / eac3_spectral.patch
1 Index: ffmpeg/libavcodec/ac3dec.c
2 ===================================================================
3 --- ffmpeg/libavcodec/ac3dec.c  (revision 19357)
4 +++ ffmpeg/libavcodec/ac3dec.c  (working copy)
5 @@ -819,15 +819,100 @@
6  
7      /* spectral extension strategy */
8      if (s->eac3 && (!blk || get_bits1(gbc))) {
9 +        s->spx_in_use = get_bits1(gbc);
10 +        if (s->spx_in_use) {
11 +            int begf, endf;
12 +            int spx_end_subband;
13 +
14 +            /* determine which channels use spx */
15 +            if (s->channel_mode == AC3_CHMODE_MONO) {
16 +                s->channel_in_spx[1] = 1;
17 +            } else {
18 +                for (ch = 1; ch <= fbw_channels; ch++)
19 +                    s->channel_in_spx[ch] = get_bits1(gbc);
20 +            }
21 +
22 +            s->spx_copy_start_freq = get_bits(gbc, 2) * 12 + 25;
23 +            begf = get_bits(gbc, 3);
24 +            endf = get_bits(gbc, 3);
25 +            s->spx_start_subband = begf < 6 ? begf+2 : 2*begf-3;
26 +            spx_end_subband      = endf < 4 ? endf+5 : 2*endf+3;
27 +            if (s->spx_start_subband >= spx_end_subband) {
28 +                av_log(s->avctx, AV_LOG_ERROR, "invalid spectral extension range (%d >= %d)\n",
29 +                       s->spx_start_subband, spx_end_subband);
30 +                return -1;
31 +            }
32 +            s->spx_start_freq    = s->spx_start_subband * 12 + 25;
33 +            s->spx_end_freq      = spx_end_subband      * 12 + 25;
34 +            if (s->spx_copy_start_freq >= s->spx_start_freq) {
35 +                av_log(s->avctx, AV_LOG_ERROR, "invalid spectral extension copy start bin (%d >= %d)\n",
36 +                       s->spx_copy_start_freq, s->spx_start_freq);
37 +                return -1;
38 +            }
39 +            decode_band_structure(gbc, blk, s->eac3, 0,
40 +                                  s->spx_start_subband, spx_end_subband,
41 +                                  ff_eac3_default_spx_band_struct,
42 +                                  s->spx_band_struct, &s->num_spx_bands,
43 +                                  s->spx_band_sizes);
44 +        } else {
45 +            for (ch = 1; ch <= fbw_channels; ch++) {
46 +                s->channel_in_spx[ch] = 0;
47 +                s->first_spx_coords[ch] = 1;
48 +            }
49 +        }
50          if (get_bits1(gbc)) {
51              av_log_missing_feature(s->avctx, "Spectral extension", 1);
52              return -1;
53          }
54 -        /* TODO: parse spectral extension strategy info */
55      }
56  
57 -    /* TODO: spectral extension coordinates */
58 +    /* spectral extension coordinates */
59 +    if (s->spx_in_use) {
60 +        for (ch = 1; ch <= fbw_channels; ch++) {
61 +            if (s->channel_in_spx[ch]) {
62 +                if (s->first_spx_coords[ch] || get_bits1(gbc)) {
63 +                    float spx_blend;
64 +                    int bin, master_spx_coord;
65  
66 +                    s->first_spx_coords[ch] = 0;
67 +                    spx_blend = get_bits(gbc, 5) * (1.0f/32);
68 +                    master_spx_coord = get_bits(gbc, 2) * 3;
69 +
70 +                    bin = s->spx_start_freq;
71 +                    for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
72 +                        int bandsize;
73 +                        int spx_coord_exp, spx_coord_mant;
74 +                        float nratio, sblend, nblend, spx_coord;
75 +
76 +                        /* calculate blending factors */
77 +                        bandsize = s->spx_band_sizes[bnd];
78 +                        nratio = ((float)((bin + (bandsize >> 1))) / s->spx_end_freq) - spx_blend;
79 +                        nratio = av_clipf(nratio, 0.0f, 1.0f);
80 +                        nblend = sqrtf(3.0f * nratio); // noise is scaled by sqrt(3) to give unity variance
81 +                        sblend = sqrtf(1.0f - nratio);
82 +                        bin += bandsize;
83 +
84 +                        /* decode spx coordinates */
85 +                        spx_coord_exp  = get_bits(gbc, 4);
86 +                        spx_coord_mant = get_bits(gbc, 2);
87 +                        if (spx_coord_exp == 15)
88 +                            spx_coord_mant <<= 1;
89 +                        else
90 +                            spx_coord_mant += 4;
91 +                        spx_coord_mant <<= (25 - spx_coord_exp - master_spx_coord);
92 +                        spx_coord = spx_coord_mant * (1.0f/(1<<23));
93 +
94 +                        /* multiply noise and signal blending factors by spx coordinate */
95 +                        s->spx_noise_blend [ch][bnd] = nblend * spx_coord;
96 +                        s->spx_signal_blend[ch][bnd] = sblend * spx_coord;
97 +                    }
98 +                }
99 +            } else {
100 +                s->first_spx_coords[ch] = 1;
101 +            }
102 +        }
103 +    }
104 +
105      /* coupling strategy */
106      if (s->eac3 ? s->cpl_strategy_exists[blk] : get_bits1(gbc)) {
107          memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
108 @@ -863,9 +948,9 @@
109                  s->phase_flags_in_use = get_bits1(gbc);
110  
111              /* coupling frequency range */
112 -            /* TODO: modify coupling end freq if spectral extension is used */
113              cpl_start_subband = get_bits(gbc, 4);
114 -            cpl_end_subband   = get_bits(gbc, 4) + 3;
115 +            cpl_end_subband = s->spx_in_use ? s->spx_start_subband - 1 :
116 +                                              get_bits(gbc, 4) + 3;
117              if (cpl_start_subband >= cpl_end_subband) {
118                  av_log(s->avctx, AV_LOG_ERROR, "invalid coupling range (%d >= %d)\n",
119                         cpl_start_subband, cpl_end_subband);
120 @@ -938,8 +1023,11 @@
121      if (channel_mode == AC3_CHMODE_STEREO) {
122          if ((s->eac3 && !blk) || get_bits1(gbc)) {
123              s->num_rematrixing_bands = 4;
124 -            if(cpl_in_use && s->start_freq[CPL_CH] <= 61)
125 +            if (cpl_in_use && s->start_freq[CPL_CH] <= 61) {
126                  s->num_rematrixing_bands -= 1 + (s->start_freq[CPL_CH] == 37);
127 +            } else if (s->spx_in_use && s->spx_start_freq <= 61) {
128 +                s->num_rematrixing_bands -= 1;
129 +            }
130              for(bnd=0; bnd<s->num_rematrixing_bands; bnd++)
131                  s->rematrixing_flags[bnd] = get_bits1(gbc);
132          } else if (!blk) {
133 @@ -964,6 +1052,8 @@
134              int prev = s->end_freq[ch];
135              if (s->channel_in_cpl[ch])
136                  s->end_freq[ch] = s->start_freq[CPL_CH];
137 +            else if (s->channel_in_spx[ch])
138 +                s->end_freq[ch] = s->spx_start_freq;
139              else {
140                  int bandwidth_code = get_bits(gbc, 6);
141                  if (bandwidth_code > 60) {
142 @@ -1160,8 +1254,6 @@
143  
144      /* TODO: generate enhanced coupling coordinates and uncouple */
145  
146 -    /* TODO: apply spectral extension */
147 -
148      /* recover coefficients if rematrixing is in use */
149      if(s->channel_mode == AC3_CHMODE_STEREO)
150          do_rematrixing(s);
151 @@ -1177,6 +1269,10 @@
152          s->dsp.int32_to_float_fmul_scalar(s->transform_coeffs[ch], s->fixed_coeffs[ch], gain, 256);
153      }
154  
155 +    /* apply spectral extension to high frequency bins */
156 +    if (s->spx_in_use)
157 +        ff_eac3_apply_spectral_extension(s);
158 +
159      /* downmix and MDCT. order depends on whether block switching is used for
160         any channel in this block. this is because coefficients for the long
161         and short transforms cannot be mixed. */
162 Index: ffmpeg/libavcodec/ac3dec.h
163 ===================================================================
164 --- ffmpeg/libavcodec/ac3dec.h  (revision 19357)
165 +++ ffmpeg/libavcodec/ac3dec.h  (working copy)
166 @@ -42,6 +42,7 @@
167  #define AC3_MAX_COEFS   256
168  #define AC3_BLOCK_SIZE  256
169  #define MAX_BLOCKS        6
170 +#define SPX_MAX_BANDS    17
171  
172  typedef struct {
173      AVCodecContext *avctx;                  ///< parent context
174 @@ -88,6 +89,23 @@
175      int cpl_coords[AC3_MAX_CHANNELS][18];   ///< coupling coordinates                   (cplco)
176  ///@}
177  
178 +///@defgroup spx spectral extension
179 +///@{
180 +    int spx_in_use;                             ///< spectral extension in use              (spxinu)
181 +    uint8_t channel_in_spx[AC3_MAX_CHANNELS];   ///< channel in spectral extension          (chinspx)
182 +    int8_t spx_atten_code[AC3_MAX_CHANNELS];    ///< spx attenuation code                   (spxattencod)
183 +    int spx_start_subband;                      ///< spx beginning frequency band           (spxbegf)
184 +    int spx_start_freq;                         ///< spx start frequency bin
185 +    int spx_end_freq;                           ///< spx end frequency bin
186 +    int spx_copy_start_freq;                    ///< spx starting frequency for copying     (copystartmant)
187 +    int num_spx_bands;                          ///< number of spx bands                    (nspxbnds)
188 +    uint8_t spx_band_struct[SPX_MAX_BANDS];     ///< spectral extension band structure      (spxbndstrc)
189 +    uint8_t spx_band_sizes[SPX_MAX_BANDS];      ///< number of bins in each band            (spxbndsztab)
190 +    uint8_t first_spx_coords[AC3_MAX_CHANNELS]; ///< first spx coordinates states           (firstspxcos)
191 +    float spx_noise_blend[AC3_MAX_CHANNELS][SPX_MAX_BANDS]; ///< spx noise blending factor  (nblendfact)
192 +    float spx_signal_blend[AC3_MAX_CHANNELS][SPX_MAX_BANDS];///< spx signal blending factor (sblendfact)
193 +///@}
194 +
195  ///@defgroup aht adaptive hybrid transform
196      int channel_uses_aht[AC3_MAX_CHANNELS];                         ///< channel AHT in use (chahtinu)
197      int pre_mantissa[AC3_MAX_CHANNELS][AC3_MAX_COEFS][MAX_BLOCKS];  ///< pre-IDCT mantissas
198 @@ -179,4 +197,11 @@
199   */
200  void ff_eac3_decode_transform_coeffs_aht_ch(AC3DecodeContext *s, int ch);
201  
202 +/**
203 + * Apply spectral extension to each channel by copying lower frequency
204 + * coefficients to higher frequency bins and applying side information to
205 + * approximate the original high frequency signal.
206 + */
207 +void ff_eac3_apply_spectral_extension(AC3DecodeContext *s);
208 +
209  #endif /* AVCODEC_AC3DEC_H */
210 Index: ffmpeg/libavcodec/eac3dec.c
211 ===================================================================
212 --- ffmpeg/libavcodec/eac3dec.c (revision 19357)
213 +++ ffmpeg/libavcodec/eac3dec.c (working copy)
214 @@ -23,10 +23,6 @@
215  /*
216   * There are several features of E-AC-3 that this decoder does not yet support.
217   *
218 - * Spectral Extension
219 - *     There is a patch to get this working for the two samples we have that
220 - *     use it, but it needs some minor changes in order to be accepted.
221 - *
222   * Enhanced Coupling
223   *     No known samples exist.  If any ever surface, this feature should not be
224   *     too difficult to implement.
225 @@ -66,6 +62,95 @@
226  
227  #define EAC3_SR_CODE_REDUCED  3
228  
229 +void ff_eac3_apply_spectral_extension(AC3DecodeContext *s)
230 +{
231 +    int bin, bnd, ch, i;
232 +    uint8_t wrapflag[SPX_MAX_BANDS]={1,0,}, num_copy_sections, copy_sizes[SPX_MAX_BANDS];
233 +    float rms_energy[SPX_MAX_BANDS];
234 +
235 +    /* Set copy index mapping table. Set wrap flags to apply a notch filter at
236 +       wrap points later on. */
237 +    bin = s->spx_copy_start_freq;
238 +    num_copy_sections = 0;
239 +    for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
240 +        int copysize;
241 +        int bandsize = s->spx_band_sizes[bnd];
242 +        if (bin + bandsize > s->spx_start_freq) {
243 +            copy_sizes[num_copy_sections++] = bin - s->spx_copy_start_freq;
244 +            bin = s->spx_copy_start_freq;
245 +            wrapflag[bnd] = 1;
246 +        }
247 +        for (i = 0; i < bandsize; i += copysize) {
248 +            if (bin == s->spx_start_freq) {
249 +                copy_sizes[num_copy_sections++] = bin - s->spx_copy_start_freq;
250 +                bin = s->spx_copy_start_freq;
251 +            }
252 +            copysize = FFMIN(bandsize - i, s->spx_start_freq - bin);
253 +            bin += copysize;
254 +        }
255 +    }
256 +    copy_sizes[num_copy_sections++] = bin - s->spx_copy_start_freq;
257 +
258 +    for (ch = 1; ch <= s->fbw_channels; ch++) {
259 +        if (!s->channel_in_spx[ch])
260 +            continue;
261 +
262 +        /* Copy coeffs from normal bands to extension bands */
263 +        bin = s->spx_start_freq;
264 +        for (i = 0; i < num_copy_sections; i++) {
265 +            memcpy(&s->transform_coeffs[ch][bin],
266 +                   &s->transform_coeffs[ch][s->spx_copy_start_freq],
267 +                   copy_sizes[i]*sizeof(float));
268 +            bin += copy_sizes[i];
269 +        }
270 +
271 +        /* Calculate RMS energy for each SPX band. */
272 +        bin = s->spx_start_freq;
273 +        for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
274 +            int bandsize = s->spx_band_sizes[bnd];
275 +            float accum = 0.0f;
276 +            for (i = 0; i < bandsize; i++) {
277 +                float coeff = s->transform_coeffs[ch][bin++];
278 +                accum += coeff * coeff;
279 +            }
280 +            rms_energy[bnd] = sqrtf(accum / bandsize);
281 +        }
282 +
283 +        /* Apply a notch filter at transitions between normal and extension
284 +           bands and at all wrap points. */
285 +        if (s->spx_atten_code[ch] >= 0) {
286 +            const float *atten_tab = ff_eac3_spx_atten_tab[s->spx_atten_code[ch]];
287 +            bin = s->spx_start_freq - 2;
288 +            for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
289 +                if (wrapflag[bnd]) {
290 +                    float *coeffs = &s->transform_coeffs[ch][bin];
291 +                    coeffs[0] *= atten_tab[0];
292 +                    coeffs[1] *= atten_tab[1];
293 +                    coeffs[2] *= atten_tab[2];
294 +                    coeffs[3] *= atten_tab[1];
295 +                    coeffs[4] *= atten_tab[0];
296 +                }
297 +                bin += s->spx_band_sizes[bnd];
298 +            }
299 +        }
300 +
301 +        /* Apply noise-blended coefficient scaling based on previously
302 +           calculated RMS energy, blending factors, and SPX coordinates for
303 +           each band. */
304 +        bin = s->spx_start_freq;
305 +        for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
306 +            float nscale = s->spx_noise_blend[ch][bnd] * rms_energy[bnd] * (1.0f/(1<<31));
307 +            float sscale = s->spx_signal_blend[ch][bnd];
308 +            for (i = 0; i < s->spx_band_sizes[bnd]; i++) {
309 +                float noise  = nscale * (int)av_lfg_get(&s->dith_state);
310 +                s->transform_coeffs[ch][bin]   *= sscale;
311 +                s->transform_coeffs[ch][bin++] += noise;
312 +            }
313 +        }
314 +    }
315 +}
316 +
317 +
318  /** lrint(M_SQRT2*cos(2*M_PI/12)*(1<<23)) */
319  #define COEFF_0 10273905LL
320  
321 @@ -494,9 +579,10 @@
322      if (parse_spx_atten_data) {
323          av_log_missing_feature(s->avctx, "Spectral extension attenuation", 1);
324          for (ch = 1; ch <= s->fbw_channels; ch++) {
325 -            if (get_bits1(gbc)) { // channel has spx attenuation
326 -                skip_bits(gbc, 5); // skip spx attenuation code
327 -            }
328 +        if (parse_spx_atten_data && get_bits1(gbc))
329 +            s->spx_atten_code[ch] = get_bits(gbc, 5);
330 +        else
331 +            s->spx_atten_code[ch] = -1;
332          }
333      }
334  
335 @@ -513,6 +599,7 @@
336  
337      /* syntax state initialization */
338      for (ch = 1; ch <= s->fbw_channels; ch++) {
339 +        s->first_spx_coords[ch] = 1;
340          s->first_cpl_coords[ch] = 1;
341      }
342      s->first_cpl_leak = 1;
343 Index: ffmpeg/libavcodec/ac3dec_data.c
344 ===================================================================
345 --- ffmpeg/libavcodec/ac3dec_data.c     (revision 19357)
346 +++ ffmpeg/libavcodec/ac3dec_data.c     (working copy)
347 @@ -1127,6 +1127,51 @@
348  { 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1 };
349  
350  /**
351 + * Table E2.15 Default Spectral Extension Banding Structure
352 + */
353 +const uint8_t ff_eac3_default_spx_band_struct[17] =
354 +{ 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 };
355 +
356 +/**
357 + * Table E.25: Spectral Extension Attenuation Table
358 + * ff_eac3_spx_atten_tab[code][bin]=pow(2.0,(bin+1)*(code+1)/-15.0);
359 + */
360 +const float ff_eac3_spx_atten_tab[32][3] = {
361 +    { 0.954841603910416503f, 0.911722488558216804f, 0.870550563296124125f },
362 +    { 0.911722488558216804f, 0.831237896142787758f, 0.757858283255198995f },
363 +    { 0.870550563296124125f, 0.757858283255198995f, 0.659753955386447100f },
364 +    { 0.831237896142787758f, 0.690956439983888004f, 0.574349177498517438f },
365 +    { 0.793700525984099792f, 0.629960524947436595f, 0.500000000000000000f },
366 +    { 0.757858283255198995f, 0.574349177498517438f, 0.435275281648062062f },
367 +    { 0.723634618720189082f, 0.523647061410313364f, 0.378929141627599553f },
368 +    { 0.690956439983888004f, 0.477420801955208307f, 0.329876977693223550f },
369 +    { 0.659753955386447100f, 0.435275281648062062f, 0.287174588749258719f },
370 +    { 0.629960524947436595f, 0.396850262992049896f, 0.250000000000000000f },
371 +    { 0.601512518041058319f, 0.361817309360094541f, 0.217637640824031003f },
372 +    { 0.574349177498517438f, 0.329876977693223550f, 0.189464570813799776f },
373 +    { 0.548412489847312945f, 0.300756259020529160f, 0.164938488846611775f },
374 +    { 0.523647061410313364f, 0.274206244923656473f, 0.143587294374629387f },
375 +    { 0.500000000000000000f, 0.250000000000000000f, 0.125000000000000000f },
376 +    { 0.477420801955208307f, 0.227930622139554201f, 0.108818820412015502f },
377 +    { 0.455861244279108402f, 0.207809474035696939f, 0.094732285406899888f },
378 +    { 0.435275281648062062f, 0.189464570813799776f, 0.082469244423305887f },
379 +    { 0.415618948071393879f, 0.172739109995972029f, 0.071793647187314694f },
380 +    { 0.396850262992049896f, 0.157490131236859149f, 0.062500000000000000f },
381 +    { 0.378929141627599553f, 0.143587294374629387f, 0.054409410206007751f },
382 +    { 0.361817309360094541f, 0.130911765352578369f, 0.047366142703449930f },
383 +    { 0.345478219991944002f, 0.119355200488802049f, 0.041234622211652958f },
384 +    { 0.329876977693223550f, 0.108818820412015502f, 0.035896823593657347f },
385 +    { 0.314980262473718298f, 0.099212565748012460f, 0.031250000000000000f },
386 +    { 0.300756259020529160f, 0.090454327340023621f, 0.027204705103003875f },
387 +    { 0.287174588749258719f, 0.082469244423305887f, 0.023683071351724965f },
388 +    { 0.274206244923656473f, 0.075189064755132290f, 0.020617311105826479f },
389 +    { 0.261823530705156682f, 0.068551561230914118f, 0.017948411796828673f },
390 +    { 0.250000000000000000f, 0.062500000000000000f, 0.015625000000000000f },
391 +    { 0.238710400977604098f, 0.056982655534888536f, 0.013602352551501938f },
392 +    { 0.227930622139554201f, 0.051952368508924235f, 0.011841535675862483f }
393 +};
394 +
395 +/**
396   * Table of bin locations for rematrixing bands
397   * reference: Section 7.5.2 Rematrixing : Frequency Band Definitions
398   */
399 Index: ffmpeg/libavcodec/ac3dec_data.h
400 ===================================================================
401 --- ffmpeg/libavcodec/ac3dec_data.h     (revision 19357)
402 +++ ffmpeg/libavcodec/ac3dec_data.h     (working copy)
403 @@ -34,6 +34,8 @@
404  extern const int16_t (* const ff_eac3_mantissa_vq[8])[6];
405  extern const uint8_t ff_eac3_frm_expstr[32][6];
406  extern const uint8_t ff_eac3_default_cpl_band_struct[18];
407 +extern const uint8_t ff_eac3_default_spx_band_struct[17];
408 +extern const float   ff_eac3_spx_atten_tab[32][3];
409  
410  extern const uint8_t ff_ac3_rematrix_band_tab[5];
411