]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/atrac3.c
flacenc: remove wasted trailing 0 bits
[ffmpeg] / libavcodec / atrac3.c
index 3a6d4e84d630419e36c0c2b0e5ae126b1176ca94..5bf992f39547805c555686e8a33bd5989d87a909 100644 (file)
@@ -90,8 +90,6 @@ typedef struct ATRAC3Context {
     //@{
     /** stream data */
     int coding_mode;
-    int bit_rate;
-    int sample_rate;
 
     ChannelUnit *units;
     //@}
@@ -110,7 +108,6 @@ typedef struct ATRAC3Context {
     //@{
     /** extradata */
     int scrambled_stream;
-    int frame_factor;
     //@}
 
     FFTContext mdct_ctx;
@@ -119,6 +116,7 @@ typedef struct ATRAC3Context {
 } 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];
@@ -176,27 +174,19 @@ static int decode_bytes(const uint8_t *input, uint8_t *out, int bytes)
     return off;
 }
 
-static av_cold int init_atrac3_transforms(ATRAC3Context *q)
+static av_cold void init_atrac3_window(void)
 {
-    float enc_window[256];
-    int i;
+    int i, j;
 
     /* generate the mdct window, for details see
      * http://wiki.multimedia.cx/index.php?title=RealAudio_atrc#Windows */
-    for (i = 0; i < 256; i++)
-        enc_window[i] = (sin(((i + 0.5) / 256.0 - 0.5) * M_PI) + 1.0) * 0.5;
-
-    if (!mdct_window[0]) {
-        for (i = 0; i < 256; i++) {
-            mdct_window[i] = enc_window[i] /
-                             (enc_window[      i] * enc_window[      i] +
-                              enc_window[255 - i] * enc_window[255 - i]);
-            mdct_window[511 - i] = mdct_window[i];
-        }
+    for (i = 0, j = 255; i < 128; i++, j--) {
+        float wi = sin(((i + 0.5) / 256.0 - 0.5) * M_PI) + 1.0;
+        float wj = sin(((j + 0.5) / 256.0 - 0.5) * M_PI) + 1.0;
+        float w  = 0.5 * (wi * wi + wj * wj);
+        mdct_window[i] = mdct_window[511 - i] = wi / w;
+        mdct_window[j] = mdct_window[511 - j] = wj / w;
     }
-
-    /* initialize the MDCT transform */
-    return ff_mdct_init(&q->mdct_ctx, 9, 1, 1.0 / 32768);
 }
 
 static av_cold int atrac3_decode_close(AVCodecContext *avctx)
@@ -320,13 +310,13 @@ static int decode_spectrum(GetBitContext *gb, float *output)
                 output[first] = mantissas[j] * scale_factor;
         } else {
             /* this subband was not coded, so zero the entire subband */
-            memset(output + first, 0, subband_size * sizeof(float));
+            memset(output + first, 0, subband_size * sizeof(*output));
         }
     }
 
     /* clear the subbands that were not coded */
     first = subband_tab[i];
-    memset(output + first, 0, (SAMPLES_PER_FRAME - first) * sizeof(float));
+    memset(output + first, 0, (SAMPLES_PER_FRAME - first) * sizeof(*output));
     return num_subbands;
 }
 
@@ -503,7 +493,7 @@ static void gain_compensate_and_overlap(float *input, float *prev,
     }
 
     /* Delay for the overlapping part. */
-    memcpy(prev, &input[256], 256 * sizeof(float));
+    memcpy(prev, &input[256], 256 * sizeof(*prev));
 }
 
 /*
@@ -693,7 +683,7 @@ static int decode_channel_sound_unit(ATRAC3Context *q, GetBitContext *gb,
         if (band <= num_bands)
             imlt(q, &snd->spectrum[band * 256], snd->imdct_buf, band & 1);
         else
-            memset(snd->imdct_buf, 0, 512 * sizeof(float));
+            memset(snd->imdct_buf, 0, 512 * sizeof(*snd->imdct_buf));
 
         /* gain compensation and overlapping */
         gain_compensate_and_overlap(snd->imdct_buf,
@@ -751,7 +741,8 @@ static int decode_frame(AVCodecContext *avctx, const uint8_t *databuf,
         init_get_bits(&q->gb, ptr1, avctx->block_align * 8);
 
         /* Fill the Weighting coeffs delay buffer */
-        memmove(q->weighting_delay, &q->weighting_delay[2], 4 * sizeof(int));
+        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);
 
@@ -845,18 +836,37 @@ static int atrac3_decode_frame(AVCodecContext *avctx, void *data,
     return avctx->block_align;
 }
 
+static void atrac3_init_static_data(AVCodec *codec)
+{
+    int i;
+
+    init_atrac3_window();
+    ff_atrac_generate_tables();
+
+    /* Initialize the VLC tables. */
+    for (i = 0; i < 7; i++) {
+        spectral_coeff_tab[i].table = &atrac3_vlc_table[atrac3_vlc_offs[i]];
+        spectral_coeff_tab[i].table_allocated = atrac3_vlc_offs[i + 1] -
+                                                atrac3_vlc_offs[i    ];
+        init_vlc(&spectral_coeff_tab[i], 9, huff_tab_sizes[i],
+                 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)
 {
     int i, ret;
-    int version, delay, samples_per_frame;
+    int version, delay, samples_per_frame, frame_factor;
     const uint8_t *edata_ptr = avctx->extradata;
     ATRAC3Context *q = avctx->priv_data;
-    static VLC_TYPE atrac3_vlc_table[4096][2];
-    static int vlcs_initialized = 0;
-
-    /* Take data from the AVCodecContext (RM container). */
-    q->sample_rate     = avctx->sample_rate;
-    q->bit_rate        = avctx->bit_rate;
 
     if (avctx->channels <= 0 || avctx->channels > 2) {
         av_log(avctx, AV_LOG_ERROR, "Channel configuration error!\n");
@@ -872,7 +882,7 @@ static av_cold int atrac3_decode_init(AVCodecContext *avctx)
         q->coding_mode = bytestream_get_le16(&edata_ptr);
         av_log(avctx, AV_LOG_DEBUG,"[8-9] %d\n",
                bytestream_get_le16(&edata_ptr));  //Dupe of coding mode
-        q->frame_factor = bytestream_get_le16(&edata_ptr);  // Unknown always 1
+        frame_factor = bytestream_get_le16(&edata_ptr);  // Unknown always 1
         av_log(avctx, AV_LOG_DEBUG,"[12-13] %d\n",
                bytestream_get_le16(&edata_ptr));  // Unknown always 0
 
@@ -883,12 +893,12 @@ static av_cold int atrac3_decode_init(AVCodecContext *avctx)
         q->coding_mode       = q->coding_mode ? JOINT_STEREO : STEREO;
         q->scrambled_stream  = 0;
 
-        if (avctx->block_align !=  96 * avctx->channels * q->frame_factor &&
-            avctx->block_align != 152 * avctx->channels * q->frame_factor &&
-            avctx->block_align != 192 * avctx->channels * q->frame_factor) {
+        if (avctx->block_align !=  96 * avctx->channels * frame_factor &&
+            avctx->block_align != 152 * avctx->channels * frame_factor &&
+            avctx->block_align != 192 * avctx->channels * frame_factor) {
             av_log(avctx, AV_LOG_ERROR, "Unknown frame/channel/frame_factor "
                    "configuration %d/%d/%d\n", avctx->block_align,
-                   avctx->channels, q->frame_factor);
+                   avctx->channels, frame_factor);
             return AVERROR_INVALIDDATA;
         }
     } else if (avctx->extradata_size == 10) {
@@ -902,6 +912,7 @@ static av_cold int atrac3_decode_init(AVCodecContext *avctx)
     } else {
         av_log(NULL, AV_LOG_ERROR, "Unknown extradata size %d.\n",
                avctx->extradata_size);
+        return AVERROR(EINVAL);
     }
 
     /* Check the extradata */
@@ -937,43 +948,20 @@ static av_cold int atrac3_decode_init(AVCodecContext *avctx)
     if (avctx->block_align >= UINT_MAX / 2)
         return AVERROR(EINVAL);
 
-    q->decoded_bytes_buffer = av_mallocz(avctx->block_align +
-                                         (4 - avctx->block_align % 4) +
+    q->decoded_bytes_buffer = av_mallocz(FFALIGN(avctx->block_align, 4) +
                                          FF_INPUT_BUFFER_PADDING_SIZE);
     if (q->decoded_bytes_buffer == NULL)
         return AVERROR(ENOMEM);
 
-
-    /* Initialize the VLC tables. */
-    if (!vlcs_initialized) {
-        for (i = 0; i < 7; i++) {
-            spectral_coeff_tab[i].table = &atrac3_vlc_table[atrac3_vlc_offs[i]];
-            spectral_coeff_tab[i].table_allocated = atrac3_vlc_offs[i + 1] -
-                                                    atrac3_vlc_offs[i    ];
-            init_vlc(&spectral_coeff_tab[i], 9, huff_tab_sizes[i],
-                     huff_bits[i], 1, 1,
-                     huff_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
-        }
-        vlcs_initialized = 1;
-    }
-
     avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
 
-    if ((ret = init_atrac3_transforms(q))) {
+    /* initialize the MDCT transform */
+    if ((ret = ff_mdct_init(&q->mdct_ctx, 9, 1, 1.0 / 32768)) < 0) {
         av_log(avctx, AV_LOG_ERROR, "Error initializing MDCT\n");
         av_freep(&q->decoded_bytes_buffer);
         return ret;
     }
 
-    ff_atrac_generate_tables();
-
-    /* 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);
-
     /* init the joint-stereo decoding data */
     q->weighting_delay[0] = 0;
     q->weighting_delay[1] = 7;
@@ -991,7 +979,7 @@ static av_cold int atrac3_decode_init(AVCodecContext *avctx)
     avpriv_float_dsp_init(&q->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
     ff_fmt_convert_init(&q->fmt_conv, avctx);
 
-    q->units = av_mallocz(sizeof(ChannelUnit) * avctx->channels);
+    q->units = av_mallocz(sizeof(*q->units) * avctx->channels);
     if (!q->units) {
         atrac3_decode_close(avctx);
         return AVERROR(ENOMEM);
@@ -1009,6 +997,7 @@ AVCodec ff_atrac3_decoder = {
     .id               = AV_CODEC_ID_ATRAC3,
     .priv_data_size   = sizeof(ATRAC3Context),
     .init             = atrac3_decode_init,
+    .init_static_data = atrac3_init_static_data,
     .close            = atrac3_decode_close,
     .decode           = atrac3_decode_frame,
     .capabilities     = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1,