]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/dpcm.c
dds: validate compressed source buffer size
[ffmpeg] / libavcodec / dpcm.c
index f9aff98417e4052198b3358113348713c8217748..b9f5b93ebcb94a2a21d54a1b6ffd097c5ae46dd5 100644 (file)
 #include "libavutil/intreadwrite.h"
 #include "avcodec.h"
 #include "bytestream.h"
+#include "internal.h"
 #include "mathops.h"
 
 typedef struct DPCMContext {
-    AVFrame frame;
-    int channels;
     int16_t roq_square_array[256];
     int sample[2];                  ///< previous sample (for SOL_DPCM)
     const int8_t *sol_table;        ///< delta table for SOL_DPCM
@@ -123,7 +122,6 @@ static av_cold int dpcm_decode_init(AVCodecContext *avctx)
         return AVERROR(EINVAL);
     }
 
-    s->channels = avctx->channels;
     s->sample[0] = s->sample[1] = 0;
 
     switch(avctx->codec->id) {
@@ -164,9 +162,6 @@ static av_cold int dpcm_decode_init(AVCodecContext *avctx)
     else
         avctx->sample_fmt = AV_SAMPLE_FMT_S16;
 
-    avcodec_get_frame_defaults(&s->frame);
-    avctx->coded_frame = &s->frame;
-
     return 0;
 }
 
@@ -176,10 +171,11 @@ static int dpcm_decode_frame(AVCodecContext *avctx, void *data,
 {
     int buf_size = avpkt->size;
     DPCMContext *s = avctx->priv_data;
+    AVFrame *frame = data;
     int out = 0, ret;
     int predictor[2];
     int ch = 0;
-    int stereo = s->channels - 1;
+    int stereo = avctx->channels - 1;
     int16_t *output_samples, *samples_end;
     GetByteContext gb;
 
@@ -193,10 +189,10 @@ static int dpcm_decode_frame(AVCodecContext *avctx, void *data,
         out = buf_size - 8;
         break;
     case AV_CODEC_ID_INTERPLAY_DPCM:
-        out = buf_size - 6 - s->channels;
+        out = buf_size - 6 - avctx->channels;
         break;
     case AV_CODEC_ID_XAN_DPCM:
-        out = buf_size - 2 * s->channels;
+        out = buf_size - 2 * avctx->channels;
         break;
     case AV_CODEC_ID_SOL_DPCM:
         if (avctx->codec_tag != 3)
@@ -211,12 +207,12 @@ static int dpcm_decode_frame(AVCodecContext *avctx, void *data,
     }
 
     /* get output buffer */
-    s->frame.nb_samples = out / s->channels;
-    if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
+    frame->nb_samples = out / avctx->channels;
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
         return ret;
     }
-    output_samples = (int16_t *)s->frame.data[0];
+    output_samples = (int16_t *)frame->data[0];
     samples_end = output_samples + out;
 
     switch(avctx->codec->id) {
@@ -245,7 +241,7 @@ static int dpcm_decode_frame(AVCodecContext *avctx, void *data,
     case AV_CODEC_ID_INTERPLAY_DPCM:
         bytestream2_skipu(&gb, 6);  /* skip over the stream mask and stream length */
 
-        for (ch = 0; ch < s->channels; ch++) {
+        for (ch = 0; ch < avctx->channels; ch++) {
             predictor[ch] = sign_extend(bytestream2_get_le16u(&gb), 16);
             *output_samples++ = predictor[ch];
         }
@@ -265,7 +261,7 @@ static int dpcm_decode_frame(AVCodecContext *avctx, void *data,
     {
         int shift[2] = { 4, 4 };
 
-        for (ch = 0; ch < s->channels; ch++)
+        for (ch = 0; ch < avctx->channels; ch++)
             predictor[ch] = sign_extend(bytestream2_get_le16u(&gb), 16);
 
         ch = 0;
@@ -296,7 +292,7 @@ static int dpcm_decode_frame(AVCodecContext *avctx, void *data,
     }
     case AV_CODEC_ID_SOL_DPCM:
         if (avctx->codec_tag != 3) {
-            uint8_t *output_samples_u8 = s->frame.data[0],
+            uint8_t *output_samples_u8 = frame->data[0],
                     *samples_end_u8 = output_samples_u8 + out;
             while (output_samples_u8 < samples_end_u8) {
                 int n = bytestream2_get_byteu(&gb);
@@ -323,8 +319,7 @@ static int dpcm_decode_frame(AVCodecContext *avctx, void *data,
         break;
     }
 
-    *got_frame_ptr   = 1;
-    *(AVFrame *)data = s->frame;
+    *got_frame_ptr = 1;
 
     return avpkt->size;
 }
@@ -332,13 +327,13 @@ static int dpcm_decode_frame(AVCodecContext *avctx, void *data,
 #define DPCM_DECODER(id_, name_, long_name_)                \
 AVCodec ff_ ## name_ ## _decoder = {                        \
     .name           = #name_,                               \
+    .long_name      = NULL_IF_CONFIG_SMALL(long_name_),     \
     .type           = AVMEDIA_TYPE_AUDIO,                   \
     .id             = id_,                                  \
     .priv_data_size = sizeof(DPCMContext),                  \
     .init           = dpcm_decode_init,                     \
     .decode         = dpcm_decode_frame,                    \
-    .capabilities   = CODEC_CAP_DR1,                        \
-    .long_name      = NULL_IF_CONFIG_SMALL(long_name_),     \
+    .capabilities   = AV_CODEC_CAP_DR1,                     \
 }
 
 DPCM_DECODER(AV_CODEC_ID_INTERPLAY_DPCM, interplay_dpcm, "DPCM Interplay");