]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/atrac3.c
dxva2: Keep code shared between dxva2 and d3d11va under the correct #if
[ffmpeg] / libavcodec / atrac3.c
index 5bf992f39547805c555686e8a33bd5989d87a909..be32a0ec864d8a1d73a99a2d0339cd00a8f1d6b6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Atrac 3 compatible decoder
+ * ATRAC3 compatible decoder
  * Copyright (c) 2006-2008 Maxim Poliakovski
  * Copyright (c) 2006-2008 Benjamin Larsson
  *
 
 /**
  * @file
- * Atrac 3 compatible decoder.
+ * ATRAC3 compatible decoder.
  * This decoder handles Sony's ATRAC3 data.
  *
- * Container formats used to store atrac 3 data:
+ * Container formats used to store ATRAC3 data:
  * RealMedia (.rm), RIFF WAV (.wav, .at3), Sony OpenMG (.oma, .aa3).
  *
  * To use this decoder, a calling application must supply the extradata
 #include <stddef.h>
 #include <stdio.h>
 
+#include "libavutil/attributes.h"
 #include "libavutil/float_dsp.h"
+
 #include "avcodec.h"
+#include "bitstream.h"
 #include "bytestream.h"
 #include "fft.h"
-#include "fmtconvert.h"
-#include "get_bits.h"
+#include "internal.h"
 
 #include "atrac.h"
 #include "atrac3data.h"
 #define SAMPLES_PER_FRAME 1024
 #define MDCT_SIZE          512
 
-typedef struct GainInfo {
-    int num_gain_data;
-    int lev_code[8];
-    int loc_code[8];
-} GainInfo;
-
 typedef struct GainBlock {
-    GainInfo g_block[4];
+    AtracGainInfo g_block[4];
 } GainBlock;
 
 typedef struct TonalComponent {
@@ -85,8 +81,7 @@ typedef struct ChannelUnit {
 } ChannelUnit;
 
 typedef struct ATRAC3Context {
-    AVFrame frame;
-    GetBitContext gb;
+    BitstreamContext bc;
     //@{
     /** stream data */
     int coding_mode;
@@ -110,19 +105,16 @@ typedef struct ATRAC3Context {
     int scrambled_stream;
     //@}
 
+    AtracGCContext  gainc_ctx;
     FFTContext mdct_ctx;
-    FmtConvertContext fmt_conv;
     AVFloatDSPContext fdsp;
 } ATRAC3Context;
 
 static DECLARE_ALIGNED(32, float, mdct_window)[MDCT_SIZE];
 static VLC_TYPE atrac3_vlc_table[4096][2];
 static VLC   spectral_coeff_tab[7];
-static float gain_tab1[16];
-static float gain_tab2[31];
-
 
-/*
+/**
  * Regular 512 points IMDCT without overlapping, with the exception of the
  * swapping of odd bands caused by the reverse spectra of the QMF.
  *
@@ -163,18 +155,21 @@ static int decode_bytes(const uint8_t *input, uint8_t *out, int bytes)
 
     off = (intptr_t)input & 3;
     buf = (const uint32_t *)(input - off);
-    c   = av_be2ne32((0x537F6103 >> (off * 8)) | (0x537F6103 << (32 - (off * 8))));
+    if (off)
+        c = av_be2ne32((0x537F6103U >> (off * 8)) | (0x537F6103U << (32 - (off * 8))));
+    else
+        c = av_be2ne32(0x537F6103U);
     bytes += 3 + off;
     for (i = 0; i < bytes / 4; i++)
         output[i] = c ^ buf[i];
 
     if (off)
-        av_log_ask_for_sample(NULL, "Offset of %d not handled.\n", off);
+        avpriv_request_sample(NULL, "Offset of %d", off);
 
     return off;
 }
 
-static av_cold void init_atrac3_window(void)
+static av_cold void init_imdct_window(void)
 {
     int i, j;
 
@@ -201,7 +196,7 @@ static av_cold int atrac3_decode_close(AVCodecContext *avctx)
     return 0;
 }
 
-/*
+/**
  * Mantissa decoding
  *
  * @param selector     which table the output values are coded with
@@ -209,7 +204,7 @@ static av_cold int atrac3_decode_close(AVCodecContext *avctx)
  * @param mantissas    mantissa output table
  * @param num_codes    number of values to get
  */
-static void read_quant_spectral_coeffs(GetBitContext *gb, int selector,
+static void read_quant_spectral_coeffs(BitstreamContext *bc, int selector,
                                        int coding_flag, int *mantissas,
                                        int num_codes)
 {
@@ -225,7 +220,7 @@ static void read_quant_spectral_coeffs(GetBitContext *gb, int selector,
         if (selector > 1) {
             for (i = 0; i < num_codes; i++) {
                 if (num_bits)
-                    code = get_sbits(gb, num_bits);
+                    code = bitstream_read_signed(bc, num_bits);
                 else
                     code = 0;
                 mantissas[i] = code;
@@ -233,7 +228,7 @@ static void read_quant_spectral_coeffs(GetBitContext *gb, int selector,
         } else {
             for (i = 0; i < num_codes; i++) {
                 if (num_bits)
-                    code = get_bits(gb, num_bits); // num_bits is always 4 in this case
+                    code = bitstream_read(bc, num_bits); // num_bits is always 4 in this case
                 else
                     code = 0;
                 mantissas[i * 2    ] = mantissa_clc_tab[code >> 2];
@@ -244,8 +239,8 @@ static void read_quant_spectral_coeffs(GetBitContext *gb, int selector,
         /* variable length coding (VLC) */
         if (selector != 1) {
             for (i = 0; i < num_codes; i++) {
-                huff_symb = get_vlc2(gb, spectral_coeff_tab[selector-1].table,
-                                     spectral_coeff_tab[selector-1].bits, 3);
+                huff_symb = bitstream_read_vlc(bc, spectral_coeff_tab[selector-1].table,
+                                               spectral_coeff_tab[selector-1].bits, 3);
                 huff_symb += 1;
                 code = huff_symb >> 1;
                 if (huff_symb & 1)
@@ -254,8 +249,8 @@ static void read_quant_spectral_coeffs(GetBitContext *gb, int selector,
             }
         } else {
             for (i = 0; i < num_codes; i++) {
-                huff_symb = get_vlc2(gb, spectral_coeff_tab[selector - 1].table,
-                                     spectral_coeff_tab[selector - 1].bits, 3);
+                huff_symb = bitstream_read_vlc(bc, spectral_coeff_tab[selector - 1].table,
+                                               spectral_coeff_tab[selector - 1].bits, 3);
                 mantissas[i * 2    ] = mantissa_vlc_tab[huff_symb * 2    ];
                 mantissas[i * 2 + 1] = mantissa_vlc_tab[huff_symb * 2 + 1];
             }
@@ -263,29 +258,29 @@ static void read_quant_spectral_coeffs(GetBitContext *gb, int selector,
     }
 }
 
-/*
+/**
  * Restore the quantized band spectrum coefficients
  *
  * @return subband count, fix for broken specification/files
  */
-static int decode_spectrum(GetBitContext *gb, float *output)
+static int decode_spectrum(BitstreamContext *bc, float *output)
 {
     int num_subbands, coding_mode, i, j, first, last, subband_size;
     int subband_vlc_index[32], sf_index[32];
     int mantissas[128];
     float scale_factor;
 
-    num_subbands = get_bits(gb, 5);  // number of coded subbands
-    coding_mode  = get_bits1(gb);    // coding Mode: 0 - VLC/ 1-CLC
+    num_subbands = bitstream_read(bc, 5);   // number of coded subbands
+    coding_mode  = bitstream_read_bit(bc);  // coding Mode: 0 - VLC/ 1 - CLC
 
     /* get the VLC selector table for the subbands, 0 means not coded */
     for (i = 0; i <= num_subbands; i++)
-        subband_vlc_index[i] = get_bits(gb, 3);
+        subband_vlc_index[i] = bitstream_read(bc, 3);
 
     /* read the scale factor indexes from the stream */
     for (i = 0; i <= num_subbands; i++) {
         if (subband_vlc_index[i] != 0)
-            sf_index[i] = get_bits(gb, 6);
+            sf_index[i] = bitstream_read(bc, 6);
     }
 
     for (i = 0; i <= num_subbands; i++) {
@@ -298,7 +293,7 @@ static int decode_spectrum(GetBitContext *gb, float *output)
             /* decode spectral coefficients for this subband */
             /* TODO: This can be done faster is several blocks share the
              * same VLC selector (subband_vlc_index) */
-            read_quant_spectral_coeffs(gb, subband_vlc_index[i], coding_mode,
+            read_quant_spectral_coeffs(bc, subband_vlc_index[i], coding_mode,
                                        mantissas, subband_size);
 
             /* decode the scale factor for this subband */
@@ -320,13 +315,13 @@ static int decode_spectrum(GetBitContext *gb, float *output)
     return num_subbands;
 }
 
-/*
+/**
  * Restore the quantized tonal components
  *
  * @param components tonal components
  * @param num_bands  number of coded bands
  */
-static int decode_tonal_components(GetBitContext *gb,
+static int decode_tonal_components(BitstreamContext *bc,
                                    TonalComponent *components, int num_bands)
 {
     int i, b, c, m;
@@ -334,13 +329,13 @@ static int decode_tonal_components(GetBitContext *gb,
     int band_flags[4], mantissa[8];
     int component_count = 0;
 
-    nb_components = get_bits(gb, 5);
+    nb_components = bitstream_read(bc, 5);
 
     /* no tonal components */
     if (nb_components == 0)
         return 0;
 
-    coding_mode_selector = get_bits(gb, 2);
+    coding_mode_selector = bitstream_read(bc, 2);
     if (coding_mode_selector == 2)
         return AVERROR_INVALIDDATA;
 
@@ -350,16 +345,16 @@ static int decode_tonal_components(GetBitContext *gb,
         int coded_values_per_component, quant_step_index;
 
         for (b = 0; b <= num_bands; b++)
-            band_flags[b] = get_bits1(gb);
+            band_flags[b] = bitstream_read_bit(bc);
 
-        coded_values_per_component = get_bits(gb, 3);
+        coded_values_per_component = bitstream_read(bc, 3);
 
-        quant_step_index = get_bits(gb, 3);
+        quant_step_index = bitstream_read(bc, 3);
         if (quant_step_index <= 1)
             return AVERROR_INVALIDDATA;
 
         if (coding_mode_selector == 3)
-            coding_mode = get_bits1(gb);
+            coding_mode = bitstream_read_bit(bc);
 
         for (b = 0; b < (num_bands + 1) * 4; b++) {
             int coded_components;
@@ -367,18 +362,18 @@ static int decode_tonal_components(GetBitContext *gb,
             if (band_flags[b >> 2] == 0)
                 continue;
 
-            coded_components = get_bits(gb, 3);
+            coded_components = bitstream_read(bc, 3);
 
             for (c = 0; c < coded_components; c++) {
                 TonalComponent *cmp = &components[component_count];
                 int sf_index, coded_values, max_coded_values;
                 float scale_factor;
 
-                sf_index = get_bits(gb, 6);
+                sf_index = bitstream_read(bc, 6);
                 if (component_count >= 64)
                     return AVERROR_INVALIDDATA;
 
-                cmp->pos = b * 64 + get_bits(gb, 6);
+                cmp->pos = b * 64 + bitstream_read(bc, 6);
 
                 max_coded_values = SAMPLES_PER_FRAME - cmp->pos;
                 coded_values     = coded_values_per_component + 1;
@@ -387,7 +382,7 @@ static int decode_tonal_components(GetBitContext *gb,
                 scale_factor = ff_atrac_sf_table[sf_index] *
                                inv_max_quant[quant_step_index];
 
-                read_quant_spectral_coeffs(gb, quant_step_index, coding_mode,
+                read_quant_spectral_coeffs(bc, quant_step_index, coding_mode,
                                            mantissa, coded_values);
 
                 cmp->num_coefs = coded_values;
@@ -404,99 +399,41 @@ static int decode_tonal_components(GetBitContext *gb,
     return component_count;
 }
 
-/*
+/**
  * Decode gain parameters for the coded bands
  *
  * @param block      the gainblock for the current band
  * @param num_bands  amount of coded bands
  */
-static int decode_gain_control(GetBitContext *gb, GainBlock *block,
+static int decode_gain_control(BitstreamContext *bc, GainBlock *block,
                                int num_bands)
 {
-    int i, cf, num_data;
+    int i, j;
     int *level, *loc;
 
-    GainInfo *gain = block->g_block;
+    AtracGainInfo *gain = block->g_block;
 
     for (i = 0; i <= num_bands; i++) {
-        num_data              = get_bits(gb, 3);
-        gain[i].num_gain_data = num_data;
+        gain[i].num_points    = bitstream_read(bc, 3);
         level                 = gain[i].lev_code;
         loc                   = gain[i].loc_code;
 
-        for (cf = 0; cf < gain[i].num_gain_data; cf++) {
-            level[cf] = get_bits(gb, 4);
-            loc  [cf] = get_bits(gb, 5);
-            if (cf && loc[cf] <= loc[cf - 1])
+        for (j = 0; j < gain[i].num_points; j++) {
+            level[j] = bitstream_read(bc, 4);
+            loc[j]   = bitstream_read(bc, 5);
+            if (j && loc[j] <= loc[j - 1])
                 return AVERROR_INVALIDDATA;
         }
     }
 
     /* Clear the unused blocks. */
     for (; i < 4 ; i++)
-        gain[i].num_gain_data = 0;
+        gain[i].num_points = 0;
 
     return 0;
 }
 
-/*
- * Apply gain parameters and perform the MDCT overlapping part
- *
- * @param input   input buffer
- * @param prev    previous buffer to perform overlap against
- * @param output  output buffer
- * @param gain1   current band gain info
- * @param gain2   next band gain info
- */
-static void gain_compensate_and_overlap(float *input, float *prev,
-                                        float *output, GainInfo *gain1,
-                                        GainInfo *gain2)
-{
-    float g1, g2, gain_inc;
-    int i, j, num_data, start_loc, end_loc;
-
-
-    if (gain2->num_gain_data == 0)
-        g1 = 1.0;
-    else
-        g1 = gain_tab1[gain2->lev_code[0]];
-
-    if (gain1->num_gain_data == 0) {
-        for (i = 0; i < 256; i++)
-            output[i] = input[i] * g1 + prev[i];
-    } else {
-        num_data = gain1->num_gain_data;
-        gain1->loc_code[num_data] = 32;
-        gain1->lev_code[num_data] = 4;
-
-        for (i = 0, j = 0; i < num_data; i++) {
-            start_loc = gain1->loc_code[i] * 8;
-            end_loc   = start_loc + 8;
-
-            g2       = gain_tab1[gain1->lev_code[i]];
-            gain_inc = gain_tab2[gain1->lev_code[i + 1] -
-                                 gain1->lev_code[i    ] + 15];
-
-            /* interpolate */
-            for (; j < start_loc; j++)
-                output[j] = (input[j] * g1 + prev[j]) * g2;
-
-            /* interpolation is done over eight samples */
-            for (; j < end_loc; j++) {
-                output[j] = (input[j] * g1 + prev[j]) * g2;
-                g2 *= gain_inc;
-            }
-        }
-
-        for (; j < 256; j++)
-            output[j] = input[j] * g1 + prev[j];
-    }
-
-    /* Delay for the overlapping part. */
-    memcpy(prev, &input[256], 256 * sizeof(*prev));
-}
-
-/*
+/**
  * Combine the tonal band spectrum and regular band spectrum
  *
  * @param spectrum        output spectrum buffer
@@ -516,7 +453,7 @@ static int add_tonal_components(float *spectrum, int num_components,
         output   = &spectrum[components[i].pos];
 
         for (j = 0; j < components[i].num_coefs; j++)
-            output[i] += input[i];
+            output[j] += input[j];
     }
 
     return last_pos;
@@ -623,7 +560,7 @@ static void channel_weighting(float *su1, float *su2, int *p3)
     }
 }
 
-/*
+/**
  * Decode a Sound Unit
  *
  * @param snd           the channel unit to be used
@@ -631,7 +568,7 @@ static void channel_weighting(float *su1, float *su2, int *p3)
  * @param channel_num   channel number
  * @param coding_mode   the coding mode (JOINT_STEREO or regular stereo/mono)
  */
-static int decode_channel_sound_unit(ATRAC3Context *q, GetBitContext *gb,
+static int decode_channel_sound_unit(ATRAC3Context *q, BitstreamContext *bc,
                                      ChannelUnit *snd, float *output,
                                      int channel_num, int coding_mode)
 {
@@ -640,30 +577,30 @@ static int decode_channel_sound_unit(ATRAC3Context *q, GetBitContext *gb,
     GainBlock *gain2 = &snd->gain_block[1 - snd->gc_blk_switch];
 
     if (coding_mode == JOINT_STEREO && channel_num == 1) {
-        if (get_bits(gb, 2) != 3) {
+        if (bitstream_read(bc, 2) != 3) {
             av_log(NULL,AV_LOG_ERROR,"JS mono Sound Unit id != 3.\n");
             return AVERROR_INVALIDDATA;
         }
     } else {
-        if (get_bits(gb, 6) != 0x28) {
+        if (bitstream_read(bc, 6) != 0x28) {
             av_log(NULL,AV_LOG_ERROR,"Sound Unit id != 0x28.\n");
             return AVERROR_INVALIDDATA;
         }
     }
 
     /* number of coded QMF bands */
-    snd->bands_coded = get_bits(gb, 2);
+    snd->bands_coded = bitstream_read(bc, 2);
 
-    ret = decode_gain_control(gb, gain2, snd->bands_coded);
+    ret = decode_gain_control(bc, gain2, snd->bands_coded);
     if (ret)
         return ret;
 
-    snd->num_components = decode_tonal_components(gb, snd->components,
+    snd->num_components = decode_tonal_components(bc, snd->components,
                                                   snd->bands_coded);
-    if (snd->num_components == -1)
-        return -1;
+    if (snd->num_components < 0)
+        return snd->num_components;
 
-    num_subbands = decode_spectrum(gb, snd->spectrum);
+    num_subbands = decode_spectrum(bc, snd->spectrum);
 
     /* Merge the decoded spectrum and tonal components. */
     last_tonal = add_tonal_components(snd->spectrum, snd->num_components,
@@ -686,11 +623,10 @@ static int decode_channel_sound_unit(ATRAC3Context *q, GetBitContext *gb,
             memset(snd->imdct_buf, 0, 512 * sizeof(*snd->imdct_buf));
 
         /* gain compensation and overlapping */
-        gain_compensate_and_overlap(snd->imdct_buf,
-                                    &snd->prev_frame[band * 256],
-                                    &output[band * 256],
-                                    &gain1->g_block[band],
-                                    &gain2->g_block[band]);
+        ff_atrac_gain_compensation(&q->gainc_ctx, snd->imdct_buf,
+                                   &snd->prev_frame[band * 256],
+                                   &gain1->g_block[band], &gain2->g_block[band],
+                                   256, &output[band * 256]);
     }
 
     /* Swap the gain control buffers for the next frame. */
@@ -709,9 +645,9 @@ static int decode_frame(AVCodecContext *avctx, const uint8_t *databuf,
     if (q->coding_mode == JOINT_STEREO) {
         /* channel coupling mode */
         /* decode Sound Unit 1 */
-        init_get_bits(&q->gb, databuf, avctx->block_align * 8);
+        bitstream_init(&q->bc, databuf, avctx->block_align * 8);
 
-        ret = decode_channel_sound_unit(q, &q->gb, q->units, out_samples[0], 0,
+        ret = decode_channel_sound_unit(q, &q->bc, q->units, out_samples[0], 0,
                                         JOINT_STEREO);
         if (ret != 0)
             return ret;
@@ -738,22 +674,22 @@ static int decode_frame(AVCodecContext *avctx, const uint8_t *databuf,
 
 
         /* set the bitstream reader at the start of the second Sound Unit*/
-        init_get_bits(&q->gb, ptr1, avctx->block_align * 8);
+        bitstream_init(&q->bc, ptr1, (avctx->block_align - i) * 8);
 
         /* Fill the Weighting coeffs delay buffer */
         memmove(q->weighting_delay, &q->weighting_delay[2],
                 4 * sizeof(*q->weighting_delay));
-        q->weighting_delay[4] = get_bits1(&q->gb);
-        q->weighting_delay[5] = get_bits(&q->gb, 3);
+        q->weighting_delay[4] = bitstream_read_bit(&q->bc);
+        q->weighting_delay[5] = bitstream_read(&q->bc, 3);
 
         for (i = 0; i < 4; i++) {
             q->matrix_coeff_index_prev[i] = q->matrix_coeff_index_now[i];
             q->matrix_coeff_index_now[i]  = q->matrix_coeff_index_next[i];
-            q->matrix_coeff_index_next[i] = get_bits(&q->gb, 2);
+            q->matrix_coeff_index_next[i] = bitstream_read(&q->bc, 2);
         }
 
         /* Decode Sound Unit 2. */
-        ret = decode_channel_sound_unit(q, &q->gb, &q->units[1],
+        ret = decode_channel_sound_unit(q, &q->bc, &q->units[1],
                                         out_samples[1], 1, JOINT_STEREO);
         if (ret != 0)
             return ret;
@@ -769,11 +705,11 @@ static int decode_frame(AVCodecContext *avctx, const uint8_t *databuf,
         /* Decode the channel sound units. */
         for (i = 0; i < avctx->channels; i++) {
             /* Set the bitstream reader at the start of a channel sound unit. */
-            init_get_bits(&q->gb,
-                          databuf + i * avctx->block_align / avctx->channels,
-                          avctx->block_align * 8 / avctx->channels);
+            bitstream_init(&q->bc,
+                           databuf + i * avctx->block_align / avctx->channels,
+                           avctx->block_align * 8 / avctx->channels);
 
-            ret = decode_channel_sound_unit(q, &q->gb, &q->units[i],
+            ret = decode_channel_sound_unit(q, &q->bc, &q->units[i],
                                             out_samples[i], i, q->coding_mode);
             if (ret != 0)
                 return ret;
@@ -797,6 +733,7 @@ static int decode_frame(AVCodecContext *avctx, const uint8_t *databuf,
 static int atrac3_decode_frame(AVCodecContext *avctx, void *data,
                                int *got_frame_ptr, AVPacket *avpkt)
 {
+    AVFrame *frame     = data;
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
     ATRAC3Context *q = avctx->priv_data;
@@ -810,8 +747,8 @@ static int atrac3_decode_frame(AVCodecContext *avctx, void *data,
     }
 
     /* get output buffer */
-    q->frame.nb_samples = SAMPLES_PER_FRAME;
-    if ((ret = avctx->get_buffer(avctx, &q->frame)) < 0) {
+    frame->nb_samples = SAMPLES_PER_FRAME;
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
         return ret;
     }
@@ -824,23 +761,22 @@ static int atrac3_decode_frame(AVCodecContext *avctx, void *data,
         databuf = buf;
     }
 
-    ret = decode_frame(avctx, databuf, (float **)q->frame.extended_data);
+    ret = decode_frame(avctx, databuf, (float **)frame->extended_data);
     if (ret) {
         av_log(NULL, AV_LOG_ERROR, "Frame decoding error!\n");
         return ret;
     }
 
-    *got_frame_ptr   = 1;
-    *(AVFrame *)data = q->frame;
+    *got_frame_ptr = 1;
 
     return avctx->block_align;
 }
 
-static void atrac3_init_static_data(AVCodec *codec)
+static av_cold void atrac3_init_static_data(AVCodec *codec)
 {
     int i;
 
-    init_atrac3_window();
+    init_imdct_window();
     ff_atrac_generate_tables();
 
     /* Initialize the VLC tables. */
@@ -852,13 +788,6 @@ static void atrac3_init_static_data(AVCodec *codec)
                  huff_bits[i],  1, 1,
                  huff_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
     }
-
-    /* Generate gain tables */
-    for (i = 0; i < 16; i++)
-        gain_tab1[i] = powf(2.0, (4 - i));
-
-    for (i = -15; i < 16; i++)
-        gain_tab2[i + 15] = powf(2.0, i * -0.125);
 }
 
 static av_cold int atrac3_decode_init(AVCodecContext *avctx)
@@ -937,9 +866,11 @@ static av_cold int atrac3_decode_init(AVCodecContext *avctx)
 
     if (q->coding_mode == STEREO)
         av_log(avctx, AV_LOG_DEBUG, "Normal stereo detected.\n");
-    else if (q->coding_mode == JOINT_STEREO)
+    else if (q->coding_mode == JOINT_STEREO) {
+        if (avctx->channels != 2)
+            return AVERROR_INVALIDDATA;
         av_log(avctx, AV_LOG_DEBUG, "Joint stereo detected.\n");
-    else {
+    else {
         av_log(avctx, AV_LOG_ERROR, "Unknown channel coding mode %x!\n",
                q->coding_mode);
         return AVERROR_INVALIDDATA;
@@ -949,8 +880,8 @@ static av_cold int atrac3_decode_init(AVCodecContext *avctx)
         return AVERROR(EINVAL);
 
     q->decoded_bytes_buffer = av_mallocz(FFALIGN(avctx->block_align, 4) +
-                                         FF_INPUT_BUFFER_PADDING_SIZE);
-    if (q->decoded_bytes_buffer == NULL)
+                                         AV_INPUT_BUFFER_PADDING_SIZE);
+    if (!q->decoded_bytes_buffer)
         return AVERROR(ENOMEM);
 
     avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
@@ -976,8 +907,8 @@ static av_cold int atrac3_decode_init(AVCodecContext *avctx)
         q->matrix_coeff_index_next[i] = 3;
     }
 
-    avpriv_float_dsp_init(&q->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
-    ff_fmt_convert_init(&q->fmt_conv, avctx);
+    ff_atrac_init_gain_compensation(&q->gainc_ctx, 4, 3);
+    avpriv_float_dsp_init(&q->fdsp, avctx->flags & AV_CODEC_FLAG_BITEXACT);
 
     q->units = av_mallocz(sizeof(*q->units) * avctx->channels);
     if (!q->units) {
@@ -985,14 +916,12 @@ static av_cold int atrac3_decode_init(AVCodecContext *avctx)
         return AVERROR(ENOMEM);
     }
 
-    avcodec_get_frame_defaults(&q->frame);
-    avctx->coded_frame = &q->frame;
-
     return 0;
 }
 
 AVCodec ff_atrac3_decoder = {
     .name             = "atrac3",
+    .long_name        = NULL_IF_CONFIG_SMALL("ATRAC3 (Adaptive TRansform Acoustic Coding 3)"),
     .type             = AVMEDIA_TYPE_AUDIO,
     .id               = AV_CODEC_ID_ATRAC3,
     .priv_data_size   = sizeof(ATRAC3Context),
@@ -1000,8 +929,7 @@ AVCodec ff_atrac3_decoder = {
     .init_static_data = atrac3_init_static_data,
     .close            = atrac3_decode_close,
     .decode           = atrac3_decode_frame,
-    .capabilities     = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1,
-    .long_name        = NULL_IF_CONFIG_SMALL("Atrac 3 (Adaptive TRansform Acoustic Coding 3)"),
+    .capabilities     = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DR1,
     .sample_fmts      = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
                                                         AV_SAMPLE_FMT_NONE },
 };