@@ -390,6 +390,26 @@ typedef struct mant_groups {
390390 int b4 ;
391391} mant_groups ;
392392
393+ static av_always_inline int dequantize_coeff (int mantissa , int exponent ,
394+ int coeff_bits )
395+ {
396+ #if USE_FIXED
397+ return (mantissa * (1 << coeff_bits )) >> exponent ;
398+ #else
399+ return mantissa >> exponent ;
400+ #endif
401+ }
402+
403+ #if USE_FIXED
404+ static av_always_inline int dequantize_dexp24_dither (int mantissa )
405+ {
406+ int scaled = mantissa * (1 << AC3_FIXED_COEFF_BITS );
407+ int round = 1 << (AC3_FIXED_EXPONENT_MAX - 1 );
408+
409+ return (scaled + round - (scaled < 0 )) >> AC3_FIXED_EXPONENT_MAX ;
410+ }
411+ #endif
412+
393413/**
394414 * Decode the transform coefficients for a particular channel
395415 * reference: Section 7.3 Quantization and Decoding of Mantissas
@@ -402,6 +422,11 @@ static void ac3_decode_transform_coeffs_ch(AC3DecodeContext *s, int ch_index, ma
402422 int8_t * exps = s -> dexps [ch_index ];
403423 int32_t * coeffs = s -> fixed_coeffs [ch_index ];
404424 int dither = (ch_index == CPL_CH ) || s -> dither_flag [ch_index ];
425+ #if USE_FIXED
426+ int coeff_bits = fixed_coeff_bits (s );
427+ #else
428+ int coeff_bits = 0 ;
429+ #endif
405430 GetBitContext * gbc = & s -> gbc ;
406431 int freq ;
407432
@@ -411,10 +436,19 @@ static void ac3_decode_transform_coeffs_ch(AC3DecodeContext *s, int ch_index, ma
411436 switch (bap ) {
412437 case 0 :
413438 /* random noise with approximate range of -0.707 to 0.707 */
414- if (dither )
439+ if (dither ) {
415440 mantissa = (((av_lfg_get (& s -> dith_state )>>8 )* 181 )>>8 ) - 5931008 ;
416- else
441+ #if USE_FIXED
442+ /* At dexp 24 the dither is below half a Q0 step. Keep two
443+ * fractional bits so it is not truncated to -1 or 0. */
444+ if (coeff_bits && exps [freq ] == AC3_FIXED_EXPONENT_MAX ) {
445+ coeffs [freq ] = dequantize_dexp24_dither (mantissa );
446+ continue ;
447+ }
448+ #endif
449+ } else {
417450 mantissa = 0 ;
451+ }
418452 break ;
419453 case 1 :
420454 if (m -> b1 ) {
@@ -466,7 +500,7 @@ static void ac3_decode_transform_coeffs_ch(AC3DecodeContext *s, int ch_index, ma
466500 mantissa = (unsigned )get_sbits (gbc , ff_ac3_quantization_tab [bap ]) << (24 - ff_ac3_quantization_tab [bap ]);
467501 break ;
468502 }
469- coeffs [freq ] = mantissa >> exps [freq ];
503+ coeffs [freq ] = dequantize_coeff ( mantissa , exps [freq ], coeff_bits ) ;
470504 }
471505}
472506
@@ -500,7 +534,8 @@ static inline void decode_transform_coeffs_ch(AC3DecodeContext *s, int blk,
500534 if (CONFIG_EAC3_DECODER && !blk )
501535 ff_eac3_decode_transform_coeffs_aht_ch (s , ch );
502536 for (bin = s -> start_freq [ch ]; bin < s -> end_freq [ch ]; bin ++ ) {
503- s -> fixed_coeffs [ch ][bin ] = s -> pre_mantissa [ch ][bin ][blk ] >> s -> dexps [ch ][bin ];
537+ s -> fixed_coeffs [ch ][bin ] = dequantize_coeff (
538+ s -> pre_mantissa [ch ][bin ][blk ], s -> dexps [ch ][bin ], 0 );
504539 }
505540 }
506541}
@@ -571,6 +606,9 @@ static void do_rematrixing(AC3DecodeContext *s)
571606static inline void do_imdct (AC3DecodeContext * s , int channels , int offset )
572607{
573608 int ch ;
609+ #if USE_FIXED
610+ int window_bits = 8 + fixed_coeff_bits (s );
611+ #endif
574612
575613 for (ch = 1 ; ch <= channels ; ch ++ ) {
576614 if (s -> block_switch [ch ]) {
@@ -581,7 +619,7 @@ static inline void do_imdct(AC3DecodeContext *s, int channels, int offset)
581619 s -> tx_fn_128 (s -> tx_128 , s -> tmp_output , x , sizeof (INTFLOAT ));
582620#if USE_FIXED
583621 s -> fdsp -> vector_fmul_window_scaled (s -> outptr [ch - 1 ], s -> delay [ch - 1 + offset ],
584- s -> tmp_output , s -> window , 128 , 8 );
622+ s -> tmp_output , s -> window , 128 , window_bits );
585623#else
586624 s -> fdsp -> vector_fmul_window (s -> outptr [ch - 1 ], s -> delay [ch - 1 + offset ],
587625 s -> tmp_output , s -> window , 128 );
@@ -593,7 +631,7 @@ static inline void do_imdct(AC3DecodeContext *s, int channels, int offset)
593631 s -> tx_fn_256 (s -> tx_256 , s -> tmp_output , s -> transform_coeffs [ch ], sizeof (INTFLOAT ));
594632#if USE_FIXED
595633 s -> fdsp -> vector_fmul_window_scaled (s -> outptr [ch - 1 ], s -> delay [ch - 1 + offset ],
596- s -> tmp_output , s -> window , 128 , 8 );
634+ s -> tmp_output , s -> window , 128 , window_bits );
597635#else
598636 s -> fdsp -> vector_fmul_window (s -> outptr [ch - 1 ], s -> delay [ch - 1 + offset ],
599637 s -> tmp_output , s -> window , 128 );
@@ -1287,7 +1325,11 @@ static int decode_audio_block(AC3DecodeContext *s, int blk, int offset)
12871325 gain = s -> dynamic_range [audio_channel ];
12881326
12891327#if USE_FIXED
1290- scale_coefs (s -> transform_coeffs [ch ], s -> fixed_coeffs [ch ], gain , 256 );
1328+ if (fixed_coeff_bits (s ))
1329+ scale_coefs_q2 (s -> transform_coeffs [ch ], s -> fixed_coeffs [ch ], gain ,
1330+ 256 );
1331+ else
1332+ scale_coefs (s -> transform_coeffs [ch ], s -> fixed_coeffs [ch ], gain , 256 );
12911333#else
12921334 if (s -> target_level != 0 )
12931335 gain = gain * s -> level_gain [audio_channel ];
@@ -1356,6 +1398,9 @@ static int ac3_decode_frame(AVCodecContext *avctx, AVFrame *frame,
13561398 AC3DecodeContext * s = avctx -> priv_data ;
13571399 int blk , ch , err , offset , ret ;
13581400 int i ;
1401+ #if USE_FIXED
1402+ int previous_coeff_bits ;
1403+ #endif
13591404 int skip = 0 , got_independent_frame = 0 ;
13601405 const uint8_t * channel_map ;
13611406 uint8_t extended_channel_map [EAC3_MAX_CHANNELS ];
@@ -1390,13 +1435,23 @@ static int ac3_decode_frame(AVCodecContext *avctx, AVFrame *frame,
13901435
13911436 buf = s -> input_buffer ;
13921437dependent_frame :
1438+ #if USE_FIXED
1439+ previous_coeff_bits = fixed_coeff_bits (s );
1440+ #endif
13931441 /* initialize the GetBitContext with the start of valid AC-3 Frame */
13941442 if ((ret = init_get_bits8 (& s -> gbc , buf , buf_size )) < 0 )
13951443 return ret ;
13961444
13971445 /* parse the syncinfo */
13981446 err = parse_frame_header (s );
13991447
1448+ #if USE_FIXED
1449+ /* Do not mix Q0 and Q2 overlap samples if a malformed or explicitly
1450+ * forced stream switches between E-AC-3 and AC-3. */
1451+ if (!err && previous_coeff_bits != fixed_coeff_bits (s ))
1452+ memset (s -> delay , 0 , sizeof (s -> delay ));
1453+ #endif
1454+
14001455 if (err ) {
14011456 switch (err ) {
14021457 case AC3_PARSE_ERROR_SYNC :
0 commit comments