[FFmpeg-devel] [PATCH] E-AC-3 spectral extension

Michael Niedermayer michaelni
Mon Aug 3 16:45:39 CEST 2009


On Sun, Aug 02, 2009 at 12:17:39PM -0400, Justin Ruggles wrote:
[...]
> --- a/libavcodec/ac3dec.c
> +++ b/libavcodec/ac3dec.c
> @@ -822,14 +822,105 @@ static int decode_audio_block(AC3DecodeContext *s, int blk)
>  
>      /* spectral extension strategy */
>      if (s->eac3 && (!blk || get_bits1(gbc))) {
> -        if (get_bits1(gbc)) {
> -            av_log_missing_feature(s->avctx, "Spectral extension", 1);
> -            return -1;
> +        s->spx_in_use = get_bits1(gbc);
> +        if (s->spx_in_use) {
> +            int copy_start, start_subband, end_subband, start_freq, end_freq;
> +
> +            /* determine which channels use spx */
> +            if (s->channel_mode == AC3_CHMODE_MONO) {
> +                s->channel_in_spx[1] = 1;
> +            } else {
> +                for (ch = 1; ch <= fbw_channels; ch++)
> +                    s->channel_in_spx[ch] = get_bits1(gbc);
> +            }
> +
> +            /* get the frequency bins of the spx copy region and the spx start
> +               and end subbands */
> +            copy_start    = get_bits(gbc, 2);
> +            start_subband = get_bits(gbc, 3);
> +            end_subband   = get_bits(gbc, 3);
> +            start_subband = start_subband + 2 + (start_subband >= 6 ? start_subband-5 : 0);

start_subband = get_bits(gbc, 3) + 2;
if(start_subband > 7)
    start_subband += start_subband - 7;


> +            end_subband   = end_subband < 4 ? end_subband+5 : 2*end_subband+3;

same
(both just a suggestion of course, i think its more readable)


> +            copy_start    = copy_start    * 12 + 25;
> +            start_freq    = start_subband * 12 + 25;
> +            end_freq      = end_subband   * 12 + 25;
> +
> +            /* check validity of spx ranges */
> +            if (start_subband >= end_subband) {
> +                av_log(s->avctx, AV_LOG_ERROR, "invalid spectral extension "
> +                       "range (%d >= %d)\n", start_subband, end_subband);
> +                return -1;
> +            }
> +            if (copy_start >= start_freq) {
> +                av_log(s->avctx, AV_LOG_ERROR, "invalid spectral extension "
> +                       "copy start bin (%d >= %d)\n", copy_start, start_freq);
> +                return -1;
> +            }
> +
> +            s->spx_copy_start_freq = copy_start;
> +            s->spx_start_subband   = start_subband;
> +            s->spx_start_freq      = start_freq;
> +            s->spx_end_freq        = end_freq;
> +
> +            decode_band_structure(gbc, blk, s->eac3, 0,
> +                                  start_subband, end_subband,
> +                                  ff_eac3_default_spx_band_struct,
> +                                  s->spx_band_struct, &s->num_spx_bands,
> +                                  s->spx_band_sizes);
> +        } else {
> +            for (ch = 1; ch <= fbw_channels; ch++) {
> +                s->channel_in_spx[ch] = 0;
> +                s->first_spx_coords[ch] = 1;
> +            }
>          }
> -        /* TODO: parse spectral extension strategy info */
>      }
>  
> -    /* TODO: spectral extension coordinates */
> +    /* spectral extension coordinates */
> +    if (s->spx_in_use) {
> +        for (ch = 1; ch <= fbw_channels; ch++) {
> +            if (s->channel_in_spx[ch]) {
> +                if (s->first_spx_coords[ch] || get_bits1(gbc)) {
> +                    float spx_blend;
> +                    int bin, master_spx_coord;
> +
> +                    s->first_spx_coords[ch] = 0;
> +                    spx_blend = get_bits(gbc, 5) * (1.0f/32);
> +                    master_spx_coord = get_bits(gbc, 2) * 3;
> +
> +                    bin = s->spx_start_freq;
> +                    for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
> +                        int bandsize;
> +                        int spx_coord_exp, spx_coord_mant;
> +                        float nratio, sblend, nblend, spx_coord;
> +
> +                        /* calculate blending factors */
> +                        bandsize = s->spx_band_sizes[bnd];
> +                        nratio = ((float)((bin + (bandsize >> 1))) / s->spx_end_freq) - spx_blend;
> +                        nratio = av_clipf(nratio, 0.0f, 1.0f);
> +                        nblend = sqrtf(3.0f * nratio); // noise is scaled by sqrt(3) to give unity variance
> +                        sblend = sqrtf(1.0f - nratio);
> +                        bin += bandsize;
> +
> +                        /* decode spx coordinates */
> +                        spx_coord_exp  = get_bits(gbc, 4);
> +                        spx_coord_mant = get_bits(gbc, 2);

> +                        if (spx_coord_exp == 15)
> +                            spx_coord_mant <<= 1;
> +                        else
> +                            spx_coord_mant += 4;

i would format that like:

 if (spx_coord_exp == 15) spx_coord_mant <<= 1;
 else                     spx_coord_mant += 4;



[...]
> diff --git a/libavcodec/ac3dec.h b/libavcodec/ac3dec.h
> index 38c2deb..79ed731 100644
> --- a/libavcodec/ac3dec.h
> +++ b/libavcodec/ac3dec.h
> @@ -42,6 +42,7 @@
>  #define AC3_MAX_COEFS   256
>  #define AC3_BLOCK_SIZE  256
>  #define MAX_BLOCKS        6
> +#define SPX_MAX_BANDS    17
>  
>  typedef struct {
>      AVCodecContext *avctx;                  ///< parent context
> @@ -88,6 +89,23 @@ typedef struct {
>      int cpl_coords[AC3_MAX_CHANNELS][18];   ///< coupling coordinates                   (cplco)
>  ///@}
>  
> +///@defgroup spx spectral extension
> +///@{
> +    int spx_in_use;                             ///< spectral extension in use              (spxinu)

> +    uint8_t channel_in_spx[AC3_MAX_CHANNELS];   ///< channel in spectral extension          (chinspx)

channel uses spectral extension ?


> +    int8_t spx_atten_code[AC3_MAX_CHANNELS];    ///< spx attenuation code                   (spxattencod)
> +    int spx_start_subband;                      ///< spx beginning frequency band           (spxbegf)

> +    int spx_start_freq;                         ///< spx start frequency bin
> +    int spx_end_freq;                           ///< spx end frequency bin

what are frequency bins?


> +    int spx_copy_start_freq;                    ///< spx starting frequency for copying     (copystartmant)

frequency in hz ?


> +    int num_spx_bands;                          ///< number of spx bands                    (nspxbnds)

> +    uint8_t spx_band_struct[SPX_MAX_BANDS];     ///< spectral extension band structure      (spxbndstrc)

what is a spectral extension band structure?


[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

No great genius has ever existed without some touch of madness. -- Aristotle
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/attachments/20090803/daea89f8/attachment.pgp>



More information about the ffmpeg-devel mailing list