]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/ra144enc.c
utvideodec: Convert to the new bitstream reader
[ffmpeg] / libavcodec / ra144enc.c
index 788660247746cce000462ba0bf86cb5806b44989..678e6684baa817df20d0569fc1cea21329dffd10 100644 (file)
@@ -2,20 +2,20 @@
  * Real Audio 1.0 (14.4K) encoder
  * Copyright (c) 2010 Francesco Lavra <francescolavra@interfree.it>
  *
- * This file is part of FFmpeg.
+ * This file is part of Libav.
  *
- * FFmpeg is free software; you can redistribute it and/or
+ * Libav is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
- * FFmpeg is distributed in the hope that it will be useful,
+ * Libav is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
+ * License along with Libav; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 #include <float.h>
 
 #include "avcodec.h"
-#include "put_bits.h"
-#include "lpc.h"
+#include "audio_frame_queue.h"
 #include "celp_filters.h"
+#include "internal.h"
+#include "mathops.h"
+#include "put_bits.h"
 #include "ra144.h"
 
 
+static av_cold int ra144_encode_close(AVCodecContext *avctx)
+{
+    RA144Context *ractx = avctx->priv_data;
+    ff_lpc_end(&ractx->lpc_ctx);
+    ff_af_queue_close(&ractx->afq);
+    return 0;
+}
+
+
 static av_cold int ra144_encode_init(AVCodecContext * avctx)
 {
     RA144Context *ractx;
+    int ret;
 
-    if (avctx->sample_fmt != SAMPLE_FMT_S16) {
-        av_log(avctx, AV_LOG_ERROR, "invalid sample format\n");
-        return -1;
-    }
     if (avctx->channels != 1) {
         av_log(avctx, AV_LOG_ERROR, "invalid number of channels: %d\n",
                avctx->channels);
         return -1;
     }
     avctx->frame_size = NBLOCKS * BLOCKSIZE;
+    avctx->initial_padding = avctx->frame_size;
     avctx->bit_rate = 8000;
     ractx = avctx->priv_data;
     ractx->lpc_coef[0] = ractx->lpc_tables[0];
     ractx->lpc_coef[1] = ractx->lpc_tables[1];
     ractx->avctx = avctx;
-    dsputil_init(&ractx->dsp, avctx);
+    ret = ff_lpc_init(&ractx->lpc_ctx, avctx->frame_size, LPC_ORDER,
+                      FF_LPC_TYPE_LEVINSON);
+    if (ret < 0)
+        goto error;
+
+    ff_af_queue_init(avctx, &ractx->afq);
+
     return 0;
+error:
+    ra144_encode_close(avctx);
+    return ret;
 }
 
 
@@ -110,7 +128,7 @@ static void orthogonalize(float *v, const float *u)
 
 /**
  * Calculate match score and gain of an LPC-filtered vector with respect to
- * input data, possibly othogonalizing it to up to 2 other vectors
+ * input data, possibly orthogonalizing it to up to two other vectors.
  *
  * @param work array used to calculate the filtered vector
  * @param coefs coefficients of the LPC filter
@@ -205,14 +223,14 @@ static int adaptive_cb_search(const int16_t *adapt_cb, float *work,
     ff_celp_lp_synthesis_filterf(work, coefs, exc, BLOCKSIZE, LPC_ORDER);
     for (i = 0; i < BLOCKSIZE; i++)
         data[i] -= best_gain * work[i];
-    return (best_vect - BLOCKSIZE / 2 + 1);
+    return best_vect - BLOCKSIZE / 2 + 1;
 }
 
 
 /**
  * Find the best vector of a fixed codebook by applying an LPC filter to
- * codebook entries, possibly othogonalizing them to up to 2 other vectors and
- * matching the results with input data
+ * codebook entries, possibly orthogonalizing them to up to two other vectors
+ * and matching the results with input data.
  *
  * @param work array used to calculate the filtered vectors
  * @param coefs coefficients of the LPC filter
@@ -314,7 +332,7 @@ static void ra144_encode_subblock(RA144Context *ractx,
                                   const int16_t *lpc_coefs, unsigned int rms,
                                   PutBitContext *pb)
 {
-    float data[BLOCKSIZE], work[LPC_ORDER + BLOCKSIZE];
+    float data[BLOCKSIZE] = { 0 }, work[LPC_ORDER + BLOCKSIZE];
     float coefs[LPC_ORDER];
     float zero[BLOCKSIZE], cba[BLOCKSIZE], cb1[BLOCKSIZE], cb2[BLOCKSIZE];
     int16_t cba_vect[BLOCKSIZE];
@@ -332,7 +350,6 @@ static void ra144_encode_subblock(RA144Context *ractx,
      * Calculate the zero-input response of the LPC filter and subtract it from
      * input data.
      */
-    memset(data, 0, sizeof(data));
     ff_celp_lp_synthesis_filterf(work + LPC_ORDER, coefs, data, BLOCKSIZE,
                                  LPC_ORDER);
     for (i = 0; i < BLOCKSIZE; i++) {
@@ -410,12 +427,12 @@ static void ra144_encode_subblock(RA144Context *ractx,
 }
 
 
-static int ra144_encode_frame(AVCodecContext *avctx, uint8_t *frame,
-                              int buf_size, void *data)
+static int ra144_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
+                              const AVFrame *frame, int *got_packet_ptr)
 {
     static const uint8_t sizes[LPC_ORDER] = {64, 32, 32, 16, 16, 8, 8, 8, 8, 4};
     static const uint8_t bit_sizes[LPC_ORDER] = {6, 5, 5, 4, 4, 3, 3, 3, 3, 2};
-    RA144Context *ractx;
+    RA144Context *ractx = avctx->priv_data;
     PutBitContext pb;
     int32_t lpc_data[NBLOCKS * BLOCKSIZE];
     int32_t lpc_coefs[LPC_ORDER][MAX_LPC_ORDER];
@@ -423,14 +440,17 @@ static int ra144_encode_frame(AVCodecContext *avctx, uint8_t *frame,
     int16_t block_coefs[NBLOCKS][LPC_ORDER];
     int lpc_refl[LPC_ORDER];    /**< reflection coefficients of the frame */
     unsigned int refl_rms[NBLOCKS]; /**< RMS of the reflection coefficients */
+    const int16_t *samples = frame ? (const int16_t *)frame->data[0] : NULL;
     int energy = 0;
-    int i, idx;
+    int i, idx, ret;
 
-    if (buf_size < FRAMESIZE) {
-        av_log(avctx, AV_LOG_ERROR, "output buffer too small\n");
+    if (ractx->last_frame)
         return 0;
+
+    if ((ret = ff_alloc_packet(avpkt, FRAMESIZE))) {
+        av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
+        return ret;
     }
-    ractx = avctx->priv_data;
 
     /**
      * Since the LPC coefficients are calculated on a frame centered over the
@@ -443,17 +463,21 @@ static int ra144_encode_frame(AVCodecContext *avctx, uint8_t *frame,
         lpc_data[i] = ractx->curr_block[BLOCKSIZE + BLOCKSIZE / 2 + i];
         energy += (lpc_data[i] * lpc_data[i]) >> 4;
     }
-    for (i = 2 * BLOCKSIZE + BLOCKSIZE / 2; i < NBLOCKS * BLOCKSIZE; i++) {
-        lpc_data[i] = *((int16_t *)data + i - 2 * BLOCKSIZE - BLOCKSIZE / 2) >>
-                      2;
-        energy += (lpc_data[i] * lpc_data[i]) >> 4;
+    if (frame) {
+        int j;
+        for (j = 0; j < frame->nb_samples && i < NBLOCKS * BLOCKSIZE; i++, j++) {
+            lpc_data[i] = samples[j] >> 2;
+            energy += (lpc_data[i] * lpc_data[i]) >> 4;
+        }
     }
+    if (i < NBLOCKS * BLOCKSIZE)
+        memset(&lpc_data[i], 0, (NBLOCKS * BLOCKSIZE - i) * sizeof(*lpc_data));
     energy = ff_energy_tab[quantize(ff_t_sqrt(energy >> 5) >> 10, ff_energy_tab,
                                     32)];
 
-    ff_lpc_calc_coefs(&ractx->dsp, lpc_data, NBLOCKS * BLOCKSIZE, LPC_ORDER,
-                      LPC_ORDER, 16, lpc_coefs, shift, 1, ORDER_METHOD_EST, 12,
-                      0);
+    ff_lpc_calc_coefs(&ractx->lpc_ctx, lpc_data, NBLOCKS * BLOCKSIZE, LPC_ORDER,
+                      LPC_ORDER, 16, lpc_coefs, shift, FF_LPC_TYPE_LEVINSON,
+                      0, ORDER_METHOD_EST, 12, 0);
     for (i = 0; i < LPC_ORDER; i++)
         block_coefs[NBLOCKS - 1][i] = -(lpc_coefs[LPC_ORDER - 1][i] <<
                                         (12 - shift[LPC_ORDER - 1]));
@@ -468,9 +492,12 @@ static int ra144_encode_frame(AVCodecContext *avctx, uint8_t *frame,
          * The filter is unstable: use the coefficients of the previous frame.
          */
         ff_int_to_int16(block_coefs[NBLOCKS - 1], ractx->lpc_coef[1]);
-        ff_eval_refl(lpc_refl, block_coefs[NBLOCKS - 1], avctx);
+        if (ff_eval_refl(lpc_refl, block_coefs[NBLOCKS - 1], avctx)) {
+            /* the filter is still unstable. set reflection coeffs to zero. */
+            memset(lpc_refl, 0, sizeof(lpc_refl));
+        }
     }
-    init_put_bits(&pb, frame, buf_size);
+    init_put_bits(&pb, avpkt->data, avpkt->size);
     for (i = 0; i < LPC_ORDER; i++) {
         idx = quantize(lpc_refl[i], ff_lpc_refl_cb[i], sizes[i]);
         put_bits(&pb, bit_sizes[i], idx);
@@ -493,19 +520,40 @@ static int ra144_encode_frame(AVCodecContext *avctx, uint8_t *frame,
     ractx->old_energy = energy;
     ractx->lpc_refl_rms[1] = ractx->lpc_refl_rms[0];
     FFSWAP(unsigned int *, ractx->lpc_coef[0], ractx->lpc_coef[1]);
-    for (i = 0; i < NBLOCKS * BLOCKSIZE; i++)
-        ractx->curr_block[i] = *((int16_t *)data + i) >> 2;
-    return FRAMESIZE;
+
+    /* copy input samples to current block for processing in next call */
+    i = 0;
+    if (frame) {
+        for (; i < frame->nb_samples; i++)
+            ractx->curr_block[i] = samples[i] >> 2;
+
+        if ((ret = ff_af_queue_add(&ractx->afq, frame)) < 0)
+            return ret;
+    } else
+        ractx->last_frame = 1;
+    memset(&ractx->curr_block[i], 0,
+           (NBLOCKS * BLOCKSIZE - i) * sizeof(*ractx->curr_block));
+
+    /* Get the next frame pts/duration */
+    ff_af_queue_remove(&ractx->afq, avctx->frame_size, &avpkt->pts,
+                       &avpkt->duration);
+
+    avpkt->size = FRAMESIZE;
+    *got_packet_ptr = 1;
+    return 0;
 }
 
 
-AVCodec ra_144_encoder =
-{
-    "real_144",
-    CODEC_TYPE_AUDIO,
-    CODEC_ID_RA_144,
-    sizeof(RA144Context),
-    ra144_encode_init,
-    ra144_encode_frame,
-    .long_name = NULL_IF_CONFIG_SMALL("RealAudio 1.0 (14.4K) encoder"),
+AVCodec ff_ra_144_encoder = {
+    .name           = "real_144",
+    .long_name      = NULL_IF_CONFIG_SMALL("RealAudio 1.0 (14.4K)"),
+    .type           = AVMEDIA_TYPE_AUDIO,
+    .id             = AV_CODEC_ID_RA_144,
+    .priv_data_size = sizeof(RA144Context),
+    .init           = ra144_encode_init,
+    .encode2        = ra144_encode_frame,
+    .close          = ra144_encode_close,
+    .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SMALL_LAST_FRAME,
+    .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
+                                                     AV_SAMPLE_FMT_NONE },
 };