]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/adpcmenc.c
avformat/cdxl: improve frame rate guessing for standard cdxl
[ffmpeg] / libavcodec / adpcmenc.c
index 6ecdab96d6d1ab6799b1b3438100c8a8d997979c..78600735cf283388d5061d8ba28aa764051c91d0 100644 (file)
@@ -74,7 +74,12 @@ static av_cold int adpcm_encode_init(AVCodecContext *avctx)
         return AVERROR(EINVAL);
     }
 
-    if (s->block_size & (s->block_size - 1)) {
+    /*
+     * AMV's block size has to match that of the corresponding video
+     * stream. Relax the POT requirement.
+     */
+    if (avctx->codec->id != AV_CODEC_ID_ADPCM_IMA_AMV &&
+        (s->block_size & (s->block_size - 1))) {
         av_log(avctx, AV_LOG_ERROR, "block size must be power of 2\n");
         return AVERROR(EINVAL);
     }
@@ -153,13 +158,28 @@ static av_cold int adpcm_encode_init(AVCodecContext *avctx)
                    "22050 or 44100\n");
             return AVERROR(EINVAL);
         }
-        avctx->frame_size  = (s->block_size / 2) * (avctx->sample_rate / 11025);
+        avctx->frame_size  = 4096; /* Hardcoded according to the SWF spec. */
         avctx->block_align = (2 + avctx->channels * (22 + 4 * (avctx->frame_size - 1)) + 7) / 8;
         break;
     case AV_CODEC_ID_ADPCM_IMA_SSI:
+    case AV_CODEC_ID_ADPCM_IMA_ALP:
         avctx->frame_size  = s->block_size * 2 / avctx->channels;
         avctx->block_align = s->block_size;
         break;
+    case AV_CODEC_ID_ADPCM_IMA_AMV:
+        if (avctx->sample_rate != 22050) {
+            av_log(avctx, AV_LOG_ERROR, "Sample rate must be 22050\n");
+            return AVERROR(EINVAL);
+        }
+
+        if (avctx->channels != 1) {
+            av_log(avctx, AV_LOG_ERROR, "Only mono is supported\n");
+            return AVERROR(EINVAL);
+        }
+
+        avctx->frame_size  = s->block_size;
+        avctx->block_align = 8 + (FFALIGN(avctx->frame_size, 2) / 2);
+        break;
     case AV_CODEC_ID_ADPCM_IMA_APM:
         avctx->frame_size  = s->block_size * 2 / avctx->channels;
         avctx->block_align = s->block_size;
@@ -204,6 +224,25 @@ static inline uint8_t adpcm_ima_compress_sample(ADPCMChannelStatus *c,
     return nibble;
 }
 
+static inline uint8_t adpcm_ima_alp_compress_sample(ADPCMChannelStatus *c, int16_t sample)
+{
+    const int delta  = sample - c->prev_sample;
+    const int step   = ff_adpcm_step_table[c->step_index];
+    const int sign   = (delta < 0) * 8;
+
+    int nibble = FFMIN(abs(delta) * 4 / step, 7);
+    int diff   = (step * nibble) >> 2;
+    if (sign)
+        diff = -diff;
+
+    nibble = sign | nibble;
+
+    c->prev_sample += diff;
+    c->prev_sample  = av_clip_int16(c->prev_sample);
+    c->step_index   = av_clip(c->step_index + ff_adpcm_index_table[nibble], 0, 88);
+    return nibble;
+}
+
 static inline uint8_t adpcm_ima_qt_compress_sample(ADPCMChannelStatus *c,
                                                    int16_t sample)
 {
@@ -318,6 +357,7 @@ static void adpcm_compress_trellis(AVCodecContext *avctx,
     nodes[0]->sample2 = c->sample2;
     if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
         version == AV_CODEC_ID_ADPCM_IMA_QT  ||
+        version == AV_CODEC_ID_ADPCM_IMA_AMV ||
         version == AV_CODEC_ID_ADPCM_SWF)
         nodes[0]->sample1 = c->prev_sample;
     if (version == AV_CODEC_ID_ADPCM_MS)
@@ -422,6 +462,7 @@ static void adpcm_compress_trellis(AVCodecContext *avctx,
                 }
             } else if (version == AV_CODEC_ID_ADPCM_IMA_WAV ||
                        version == AV_CODEC_ID_ADPCM_IMA_QT  ||
+                       version == AV_CODEC_ID_ADPCM_IMA_AMV ||
                        version == AV_CODEC_ID_ADPCM_SWF) {
 #define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
                 const int predictor = nodes[j]->sample1;\
@@ -552,6 +593,7 @@ static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
     st = avctx->channels == 2;
 
     if (avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_SSI ||
+        avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_ALP ||
         avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_APM)
         pkt_size = (frame->nb_samples * avctx->channels) / 2;
     else
@@ -655,6 +697,24 @@ static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
         flush_put_bits(&pb);
         break;
     }
+    case AV_CODEC_ID_ADPCM_IMA_ALP:
+    {
+        PutBitContext pb;
+        init_put_bits(&pb, dst, pkt_size);
+
+        av_assert0(avctx->trellis == 0);
+
+        for (n = frame->nb_samples / 2; n > 0; n--) {
+            for (ch = 0; ch < avctx->channels; ch++) {
+                put_bits(&pb, 4, adpcm_ima_alp_compress_sample(c->status + ch, *samples++));
+                put_bits(&pb, 4, adpcm_ima_alp_compress_sample(c->status + ch, samples[st]));
+            }
+            samples += avctx->channels;
+        }
+
+        flush_put_bits(&pb);
+        break;
+    }
     case AV_CODEC_ID_ADPCM_SWF:
     {
         PutBitContext pb;
@@ -795,6 +855,41 @@ static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
         flush_put_bits(&pb);
         break;
     }
+    case AV_CODEC_ID_ADPCM_IMA_AMV:
+    {
+        av_assert0(avctx->channels == 1);
+
+        c->status[0].prev_sample = *samples;
+        bytestream_put_le16(&dst, c->status[0].prev_sample);
+        bytestream_put_byte(&dst, c->status[0].step_index);
+        bytestream_put_byte(&dst, 0);
+        bytestream_put_le32(&dst, avctx->frame_size);
+
+        if (avctx->trellis > 0) {
+            n = frame->nb_samples >> 1;
+
+            if (!(buf = av_malloc(2 * n)))
+                return AVERROR(ENOMEM);
+
+            adpcm_compress_trellis(avctx, samples, buf, &c->status[0], 2 * n, avctx->channels);
+            for (i = 0; i < n; i++)
+                bytestream_put_byte(&dst, (buf[2 * i] << 4) | buf[2 * i + 1]);
+
+            samples += 2 * n;
+            av_free(buf);
+        } else for (n = frame->nb_samples >> 1; n > 0; n--) {
+            int nibble;
+            nibble  = adpcm_ima_compress_sample(&c->status[0], *samples++) << 4;
+            nibble |= adpcm_ima_compress_sample(&c->status[0], *samples++) & 0x0F;
+            bytestream_put_byte(&dst, nibble);
+        }
+
+        if (avctx->frame_size & 1) {
+            int nibble = adpcm_ima_compress_sample(&c->status[0], *samples++) << 4;
+            bytestream_put_byte(&dst, nibble);
+        }
+        break;
+    }
     case AV_CODEC_ID_ADPCM_ARGO:
     {
         PutBitContext pb;
@@ -883,12 +978,14 @@ AVCodec ff_ ## name_ ## _encoder = {                                       \
     .close          = adpcm_encode_close,                                  \
     .sample_fmts    = sample_fmts_,                                        \
     .capabilities   = capabilities_,                                       \
-    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,                           \
+    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_INIT_THREADSAFE, \
     .priv_class     = &adpcm_encoder_class,                                \
 }
 
 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_ARGO,    adpcm_argo,    sample_fmts_p, 0,                             "ADPCM Argonaut Games");
+ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_AMV, adpcm_ima_amv, sample_fmts,   0,                             "ADPCM IMA AMV");
 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_APM, adpcm_ima_apm, sample_fmts,   AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA Ubisoft APM");
+ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_ALP, adpcm_ima_alp, sample_fmts,   AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA High Voltage Software ALP");
 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_QT,  adpcm_ima_qt,  sample_fmts_p, 0,                             "ADPCM IMA QuickTime");
 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_SSI, adpcm_ima_ssi, sample_fmts,   AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA Simon & Schuster Interactive");
 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, sample_fmts_p, 0,                             "ADPCM IMA WAV");