]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/adpcmenc.c
avcodec: Constify AVCodecs
[ffmpeg] / libavcodec / adpcmenc.c
index 4bab004dacbc68c91ae349883a569364d2e9a27d..1c62ca5a83289a30c5eaf073d8f5a94a0e18d8d1 100644 (file)
@@ -94,7 +94,8 @@ static av_cold int adpcm_encode_init(AVCodecContext *avctx)
 
         if (avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_SSI ||
             avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_APM ||
-            avctx->codec->id == AV_CODEC_ID_ADPCM_ARGO) {
+            avctx->codec->id == AV_CODEC_ID_ADPCM_ARGO    ||
+            avctx->codec->id == AV_CODEC_ID_ADPCM_IMA_WS) {
             /*
              * The current trellis implementation doesn't work for extended
              * runs of samples without periodic resets. Disallow it.
@@ -192,6 +193,11 @@ static av_cold int adpcm_encode_init(AVCodecContext *avctx)
         avctx->frame_size = 32;
         avctx->block_align = 17 * avctx->channels;
         break;
+    case AV_CODEC_ID_ADPCM_IMA_WS:
+        /* each 16 bits sample gives one nibble */
+        avctx->frame_size = s->block_size * 2 / avctx->channels;
+        avctx->block_align = s->block_size;
+        break;
     default:
         return AVERROR(EINVAL);
     }
@@ -594,7 +600,8 @@ static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
 
     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)
+        avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_APM ||
+        avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_WS)
         pkt_size = (frame->nb_samples * avctx->channels) / 2;
     else
         pkt_size = avctx->block_align;
@@ -722,6 +729,9 @@ static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
 
         n = frame->nb_samples - 1;
 
+        /* NB: This is safe as we don't have AV_CODEC_CAP_SMALL_LAST_FRAME. */
+        av_assert0(n == 4095);
+
         // store AdpcmCodeSize
         put_bits(&pb, 2, 2);    // set 4-bit flash adpcm format
 
@@ -735,8 +745,7 @@ static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
         }
 
         if (avctx->trellis > 0) {
-            if (!(buf = av_malloc(2 * n)))
-                return AVERROR(ENOMEM);
+            uint8_t buf[8190 /* = 2 * n */];
             adpcm_compress_trellis(avctx, samples + avctx->channels, buf,
                                    &c->status[0], n, avctx->channels);
             if (avctx->channels == 2)
@@ -748,7 +757,6 @@ static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
                 if (avctx->channels == 2)
                     put_bits(&pb, 4, buf[n + i]);
             }
-            av_free(buf);
         } else {
             for (i = 1; i < frame->nb_samples; i++) {
                 put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[0],
@@ -928,6 +936,26 @@ static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
         flush_put_bits(&pb);
         break;
     }
+    case AV_CODEC_ID_ADPCM_IMA_WS:
+    {
+        PutBitContext pb;
+        init_put_bits(&pb, dst, pkt_size);
+
+        av_assert0(avctx->trellis == 0);
+        for (n = frame->nb_samples / 2; n > 0; n--) {
+            /* stereo: 1 byte (2 samples) for left, 1 byte for right */
+            for (ch = 0; ch < avctx->channels; ch++) {
+                int t1, t2;
+                t1 = adpcm_ima_compress_sample(&c->status[ch], *samples++);
+                t2 = adpcm_ima_compress_sample(&c->status[ch], samples[st]);
+                put_bits(&pb, 4, t2);
+                put_bits(&pb, 4, t1);
+            }
+            samples += avctx->channels;
+        }
+        flush_put_bits(&pb);
+        break;
+    }
     default:
         return AVERROR(EINVAL);
     }
@@ -959,15 +987,15 @@ static const AVOption options[] = {
     { NULL }
 };
 
-static const AVClass adpcm_encoder_class = {
-    .class_name = "ADPCM Encoder",
-    .item_name  = av_default_item_name,
-    .option     = options,
-    .version    = LIBAVUTIL_VERSION_INT,
-};
-
 #define ADPCM_ENCODER(id_, name_, sample_fmts_, capabilities_, long_name_) \
-AVCodec ff_ ## name_ ## _encoder = {                                       \
+static const AVClass name_ ## _encoder_class = {                           \
+    .class_name = #name_,                                                  \
+    .item_name  = av_default_item_name,                                    \
+    .option     = options,                                                 \
+    .version    = LIBAVUTIL_VERSION_INT,                                   \
+};                                                                         \
+                                                                           \
+const AVCodec ff_ ## name_ ## _encoder = {                                 \
     .name           = #name_,                                              \
     .long_name      = NULL_IF_CONFIG_SMALL(long_name_),                    \
     .type           = AVMEDIA_TYPE_AUDIO,                                  \
@@ -978,8 +1006,8 @@ AVCodec ff_ ## name_ ## _encoder = {                                       \
     .close          = adpcm_encode_close,                                  \
     .sample_fmts    = sample_fmts_,                                        \
     .capabilities   = capabilities_,                                       \
-    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,                           \
-    .priv_class     = &adpcm_encoder_class,                                \
+    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_INIT_THREADSAFE, \
+    .priv_class     = &name_ ## _encoder_class,                            \
 }
 
 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_ARGO,    adpcm_argo,    sample_fmts_p, 0,                             "ADPCM Argonaut Games");
@@ -989,6 +1017,7 @@ ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_ALP, adpcm_ima_alp, sample_fmts,   AV_CODEC_
 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");
+ADPCM_ENCODER(AV_CODEC_ID_ADPCM_IMA_WS,  adpcm_ima_ws,  sample_fmts,   AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA Westwood");
 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_MS,      adpcm_ms,      sample_fmts,   0,                             "ADPCM Microsoft");
 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_SWF,     adpcm_swf,     sample_fmts,   0,                             "ADPCM Shockwave Flash");
 ADPCM_ENCODER(AV_CODEC_ID_ADPCM_YAMAHA,  adpcm_yamaha,  sample_fmts,   0,                             "ADPCM Yamaha");