From 1afa8a7568ba57ad0cd2923f6e641e10d32cded4 Mon Sep 17 00:00:00 2001 From: Kostya Shishkov Date: Sun, 20 Oct 2013 16:15:03 +0200 Subject: twinvq: support multiple frames per packet Signed-off-by: Diego Biurrun --- libavcodec/twinvq.c | 51 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 14 deletions(-) (limited to 'libavcodec/twinvq.c') diff --git a/libavcodec/twinvq.c b/libavcodec/twinvq.c index 24f57478f8..4f79679c21 100644 --- a/libavcodec/twinvq.c +++ b/libavcodec/twinvq.c @@ -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; @@ -500,11 +503,17 @@ int ff_twinvq_decode_frame(AVCodecContext *avctx, void *data, 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++; @@ -764,6 +773,20 @@ av_cold int ff_twinvq_decode_init(AVCodecContext *avctx) tctx->avctx = avctx; avctx->sample_fmt = AV_SAMPLE_FMT_FLTP; + 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 & CODEC_FLAG_BITEXACT); if ((ret = init_mdct_win(tctx))) { av_log(avctx, AV_LOG_ERROR, "Error initializing MDCT\n"); -- cgit v1.2.3