]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/twinvq.c
lavc: mark the old audio/video encoding API as deprecated
[ffmpeg] / libavcodec / twinvq.c
index 94cc9da73526e07b000d853a06f6060b630a297e..940def45f579298fd56d609898ed28f8dcd3acf8 100644 (file)
@@ -215,7 +215,7 @@ static void dec_gain(TwinVQContext *tctx,
                      enum TwinVQFrameType ftype, float *out)
 {
     const TwinVQModeTab   *mtab =  tctx->mtab;
-    const TwinVQFrameData *bits = &tctx->bits;
+    const TwinVQFrameData *bits = &tctx->bits[tctx->cur_frame];
     int i, j;
     int sub        = mtab->fmode[ftype].sub;
     float step     = TWINVQ_AMP_MAX     / ((1 << TWINVQ_GAIN_BITS)     - 1);
@@ -376,11 +376,12 @@ static void imdct_and_window(TwinVQContext *tctx, enum TwinVQFrameType ftype,
 }
 
 static void imdct_output(TwinVQContext *tctx, enum TwinVQFrameType ftype,
-                         int wtype, float **out)
+                         int wtype, float **out, int offset)
 {
     const TwinVQModeTab *mtab = tctx->mtab;
     float *prev_buf           = tctx->prev_frame + tctx->last_block_pos[0];
     int size1, size2, i;
+    float *out1, *out2;
 
     for (i = 0; i < tctx->avctx->channels; i++)
         imdct_and_window(tctx, ftype, wtype,
@@ -394,15 +395,17 @@ static void imdct_output(TwinVQContext *tctx, enum TwinVQFrameType ftype,
     size2 = tctx->last_block_pos[0];
     size1 = mtab->size - size2;
 
-    memcpy(&out[0][0],     prev_buf,         size1 * sizeof(out[0][0]));
-    memcpy(&out[0][size1], tctx->curr_frame, size2 * sizeof(out[0][0]));
+    out1 = &out[0][0] + offset;
+    memcpy(out1,         prev_buf,         size1 * sizeof(*out1));
+    memcpy(out1 + size1, tctx->curr_frame, size2 * sizeof(*out1));
 
     if (tctx->avctx->channels == 2) {
-        memcpy(&out[1][0], &prev_buf[2 * mtab->size],
-               size1 * sizeof(out[1][0]));
-        memcpy(&out[1][size1], &tctx->curr_frame[2 * mtab->size],
-               size2 * sizeof(out[1][0]));
-        tctx->fdsp.butterflies_float(out[0], out[1], mtab->size);
+        out2 = &out[1][0] + offset;
+        memcpy(out2, &prev_buf[2 * mtab->size],
+               size1 * sizeof(*out2));
+        memcpy(out2 + size1, &tctx->curr_frame[2 * mtab->size],
+               size2 * sizeof(*out2));
+        tctx->fdsp.butterflies_float(out1, out2, mtab->size);
     }
 }
 
@@ -410,7 +413,7 @@ static void read_and_decode_spectrum(TwinVQContext *tctx, float *out,
                                      enum TwinVQFrameType ftype)
 {
     const TwinVQModeTab *mtab = tctx->mtab;
-    TwinVQFrameData *bits     = &tctx->bits;
+    TwinVQFrameData *bits     = &tctx->bits[tctx->cur_frame];
     int channels              = tctx->avctx->channels;
     int sub        = mtab->fmode[ftype].sub;
     int block_size = mtab->size / sub;
@@ -483,7 +486,7 @@ int ff_twinvq_decode_frame(AVCodecContext *avctx, void *data,
 
     /* get output buffer */
     if (tctx->discarded_packets >= 2) {
-        frame->nb_samples = mtab->size;
+        frame->nb_samples = mtab->size * tctx->frames_per_packet;
         if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
             av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
             return ret;
@@ -491,14 +494,26 @@ int ff_twinvq_decode_frame(AVCodecContext *avctx, void *data,
         out = (float **)frame->extended_data;
     }
 
+    if (buf_size < avctx->block_align) {
+        av_log(avctx, AV_LOG_ERROR,
+               "Frame too small (%d bytes). Truncated file?\n", buf_size);
+        return AVERROR(EINVAL);
+    }
+
     if ((ret = tctx->read_bitstream(avctx, tctx, buf, buf_size)) < 0)
         return ret;
 
-    read_and_decode_spectrum(tctx, tctx->spectrum, tctx->bits.ftype);
+    for (tctx->cur_frame = 0; tctx->cur_frame < tctx->frames_per_packet;
+         tctx->cur_frame++) {
+        read_and_decode_spectrum(tctx, tctx->spectrum,
+                                 tctx->bits[tctx->cur_frame].ftype);
 
-    imdct_output(tctx, tctx->bits.ftype, tctx->bits.window_type, out);
+        imdct_output(tctx, tctx->bits[tctx->cur_frame].ftype,
+                     tctx->bits[tctx->cur_frame].window_type, out,
+                     tctx->cur_frame * mtab->size);
 
-    FFSWAP(float *, tctx->curr_frame, tctx->prev_frame);
+        FFSWAP(float *, tctx->curr_frame, tctx->prev_frame);
+    }
 
     if (tctx->discarded_packets < 2) {
         tctx->discarded_packets++;
@@ -508,7 +523,10 @@ int ff_twinvq_decode_frame(AVCodecContext *avctx, void *data,
 
     *got_frame_ptr = 1;
 
-    return buf_size;
+    // VQF can deliver packets 1 byte greater than block align
+    if (buf_size == avctx->block_align + 1)
+        return buf_size;
+    return avctx->block_align;
 }
 
 /**
@@ -688,6 +706,11 @@ static av_cold void init_bitstream_params(TwinVQContext *tctx)
             TWINVQ_WINDOW_TYPE_BITS +
             mtab->fmode[i].sub * (bse_bits[i] + n_ch * TWINVQ_SUB_GAIN_BITS);
 
+    if (tctx->codec == TWINVQ_CODEC_METASOUND && !tctx->is_6kbps) {
+        bsize_no_main_cb[1] += 2;
+        bsize_no_main_cb[2] += 2;
+    }
+
     // The remaining bits are all used for the main spectrum coefficients
     for (i = 0; i < 4; i++) {
         int bit_size, vect_size;
@@ -753,7 +776,21 @@ av_cold int ff_twinvq_decode_init(AVCodecContext *avctx)
     tctx->avctx       = avctx;
     avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
 
-    avpriv_float_dsp_init(&tctx->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
+    if (!avctx->block_align) {
+        avctx->block_align = tctx->frame_size + 7 >> 3;
+    } else if (avctx->block_align * 8 < tctx->frame_size) {
+        av_log(avctx, AV_LOG_ERROR, "Block align is %d bits, expected %d\n",
+               avctx->block_align * 8, tctx->frame_size);
+        return AVERROR_INVALIDDATA;
+    }
+    tctx->frames_per_packet = avctx->block_align * 8 / tctx->frame_size;
+    if (tctx->frames_per_packet > TWINVQ_MAX_FRAMES_PER_PACKET) {
+        av_log(avctx, AV_LOG_ERROR, "Too many frames per packet (%d)\n",
+               tctx->frames_per_packet);
+        return AVERROR_INVALIDDATA;
+    }
+
+    avpriv_float_dsp_init(&tctx->fdsp, avctx->flags & AV_CODEC_FLAG_BITEXACT);
     if ((ret = init_mdct_win(tctx))) {
         av_log(avctx, AV_LOG_ERROR, "Error initializing MDCT\n");
         ff_twinvq_decode_close(avctx);