]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/takdec.c
mjpegdec: Properly fail on malloc failure
[ffmpeg] / libavcodec / takdec.c
index 1a2cc3bed76b175e8134903023f36d7ef3690d86..93098be7e4ed68d111810daf4709c2ae732ffcb4 100644 (file)
@@ -28,8 +28,8 @@
 #include "libavutil/internal.h"
 #include "libavutil/samplefmt.h"
 #include "tak.h"
+#include "audiodsp.h"
 #include "avcodec.h"
-#include "dsputil.h"
 #include "internal.h"
 #include "unary.h"
 
@@ -45,8 +45,7 @@ typedef struct MCDParam {
 
 typedef struct TAKDecContext {
     AVCodecContext *avctx;                          // parent AVCodecContext
-    AVFrame         frame;                          // AVFrame for decoded output
-    DSPContext      dsp;
+    AudioDSPContext adsp;
     TAKStreamInfo   ti;
     GetBitContext   gb;                             // bitstream reader initialized to start at the current frame
 
@@ -173,11 +172,9 @@ static av_cold int tak_decode_init(AVCodecContext *avctx)
 {
     TAKDecContext *s = avctx->priv_data;
 
-    ff_dsputil_init(&s->dsp, avctx);
+    ff_audiodsp_init(&s->adsp);
 
     s->avctx = avctx;
-    avcodec_get_frame_defaults(&s->frame);
-    avctx->coded_frame = &s->frame;
 
     set_sample_rate_params(avctx);
 
@@ -487,10 +484,10 @@ static int decode_subframe(TAKDecContext *s, int32_t *decoded,
     for (i = 0; i < subframe_size - filter_order; i++) {
         int v = 1 << (filter_quant - 1);
 
-        v += s->dsp.scalarproduct_int16(&s->residues[i], filter,
-                                        FFALIGN(filter_order, 16));
+        v += s->adsp.scalarproduct_int16(&s->residues[i], filter,
+                                         FFALIGN(filter_order, 16));
 
-        v = (av_clip(v >> filter_quant, -8192, 8191) << dshift) - *decoded;
+        v = (av_clip_intp2(v >> filter_quant, 13) << dshift) - *decoded;
         *decoded++ = v;
         s->residues[filter_order + i] = v >> dshift;
     }
@@ -657,10 +654,10 @@ static int decorrelate(TAKDecContext *s, int c1, int c2, int length)
         for (i = 0; i < length2; i++) {
             int v = 1 << 9;
 
-            v += s->dsp.scalarproduct_int16(&s->residues[i], filter,
-                                            FFALIGN(filter_order, 16));
+            v += s->adsp.scalarproduct_int16(&s->residues[i], filter,
+                                             FFALIGN(filter_order, 16));
 
-            p1[i] = (av_clip(v >> 10, -8192, 8191) << dshift) - p1[i];
+            p1[i] = (av_clip_intp2(v >> 10, 13) << dshift) - p1[i];
         }
 
         emms_c();
@@ -675,6 +672,7 @@ static int tak_decode_frame(AVCodecContext *avctx, void *data,
                             int *got_frame_ptr, AVPacket *pkt)
 {
     TAKDecContext *s  = avctx->priv_data;
+    AVFrame *frame    = data;
     GetBitContext *gb = &s->gb;
     int chan, i, ret, hsize;
 
@@ -687,7 +685,7 @@ static int tak_decode_frame(AVCodecContext *avctx, void *data,
         return ret;
 
     if (s->ti.flags & TAK_FRAME_FLAG_HAS_METADATA) {
-        av_log_missing_feature(avctx, "frame metadata", 1);
+        avpriv_request_sample(avctx, "Frame metadata");
         return AVERROR_PATCHWELCOME;
     }
 
@@ -695,7 +693,8 @@ static int tak_decode_frame(AVCodecContext *avctx, void *data,
     if (avctx->err_recognition & AV_EF_CRCCHECK) {
         if (ff_tak_check_crc(pkt->data, hsize)) {
             av_log(avctx, AV_LOG_ERROR, "CRC error\n");
-            return AVERROR_INVALIDDATA;
+            if (avctx->err_recognition & AV_EF_EXPLODE)
+                return AVERROR_INVALIDDATA;
         }
     }
 
@@ -741,14 +740,16 @@ static int tak_decode_frame(AVCodecContext *avctx, void *data,
     s->nb_samples = s->ti.last_frame_samples ? s->ti.last_frame_samples
                                              : s->ti.frame_samples;
 
-    s->frame.nb_samples = s->nb_samples;
-    if ((ret = ff_get_buffer(avctx, &s->frame)) < 0)
+    frame->nb_samples = s->nb_samples;
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
 
     if (avctx->bits_per_coded_sample <= 16) {
         int buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
                                                   s->nb_samples,
                                                   AV_SAMPLE_FMT_S32P, 0);
+        if (buf_size < 0)
+            return buf_size;
         av_fast_malloc(&s->decode_buffer, &s->decode_buffer_size, buf_size);
         if (!s->decode_buffer)
             return AVERROR(ENOMEM);
@@ -759,7 +760,7 @@ static int tak_decode_frame(AVCodecContext *avctx, void *data,
             return ret;
     } else {
         for (chan = 0; chan < avctx->channels; chan++)
-            s->decoded[chan] = (int32_t *)s->frame.extended_data[chan];
+            s->decoded[chan] = (int32_t *)frame->extended_data[chan];
     }
 
     if (s->nb_samples < 16) {
@@ -805,6 +806,12 @@ static int tak_decode_frame(AVCodecContext *avctx, void *data,
                     if (s->mcdparams[i].present) {
                         s->mcdparams[i].index = get_bits(gb, 2);
                         s->mcdparams[i].chan2 = get_bits(gb, 4);
+                        if (s->mcdparams[i].chan2 >= avctx->channels) {
+                            av_log(avctx, AV_LOG_ERROR,
+                                   "invalid channel 2 (%d) for %d channel(s)\n",
+                                   s->mcdparams[i].chan2, avctx->channels);
+                            return AVERROR_INVALIDDATA;
+                        }
                         if (s->mcdparams[i].index == 1) {
                             if ((nbit == s->mcdparams[i].chan2) ||
                                 (ch_mask & 1 << s->mcdparams[i].chan2))
@@ -869,7 +876,8 @@ static int tak_decode_frame(AVCodecContext *avctx, void *data,
         if (ff_tak_check_crc(pkt->data + hsize,
                              get_bits_count(gb) / 8 - hsize)) {
             av_log(avctx, AV_LOG_ERROR, "CRC error\n");
-            return AVERROR_INVALIDDATA;
+            if (avctx->err_recognition & AV_EF_EXPLODE)
+                return AVERROR_INVALIDDATA;
         }
     }
 
@@ -877,7 +885,7 @@ static int tak_decode_frame(AVCodecContext *avctx, void *data,
     switch (avctx->sample_fmt) {
     case AV_SAMPLE_FMT_U8P:
         for (chan = 0; chan < avctx->channels; chan++) {
-            uint8_t *samples = (uint8_t *)s->frame.extended_data[chan];
+            uint8_t *samples = (uint8_t *)frame->extended_data[chan];
             int32_t *decoded = s->decoded[chan];
             for (i = 0; i < s->nb_samples; i++)
                 samples[i] = decoded[i] + 0x80;
@@ -885,7 +893,7 @@ static int tak_decode_frame(AVCodecContext *avctx, void *data,
         break;
     case AV_SAMPLE_FMT_S16P:
         for (chan = 0; chan < avctx->channels; chan++) {
-            int16_t *samples = (int16_t *)s->frame.extended_data[chan];
+            int16_t *samples = (int16_t *)frame->extended_data[chan];
             int32_t *decoded = s->decoded[chan];
             for (i = 0; i < s->nb_samples; i++)
                 samples[i] = decoded[i];
@@ -893,15 +901,14 @@ static int tak_decode_frame(AVCodecContext *avctx, void *data,
         break;
     case AV_SAMPLE_FMT_S32P:
         for (chan = 0; chan < avctx->channels; chan++) {
-            int32_t *samples = (int32_t *)s->frame.extended_data[chan];
+            int32_t *samples = (int32_t *)frame->extended_data[chan];
             for (i = 0; i < s->nb_samples; i++)
                 samples[i] <<= 8;
         }
         break;
     }
 
-    *got_frame_ptr   = 1;
-    *(AVFrame *)data = s->frame;
+    *got_frame_ptr = 1;
 
     return pkt->size;
 }
@@ -918,6 +925,7 @@ static av_cold int tak_decode_close(AVCodecContext *avctx)
 
 AVCodec ff_tak_decoder = {
     .name             = "tak",
+    .long_name        = NULL_IF_CONFIG_SMALL("TAK (Tom's lossless Audio Kompressor)"),
     .type             = AVMEDIA_TYPE_AUDIO,
     .id               = AV_CODEC_ID_TAK,
     .priv_data_size   = sizeof(TAKDecContext),
@@ -925,8 +933,7 @@ AVCodec ff_tak_decoder = {
     .init_static_data = tak_init_static_data,
     .close            = tak_decode_close,
     .decode           = tak_decode_frame,
-    .capabilities     = CODEC_CAP_DR1,
-    .long_name        = NULL_IF_CONFIG_SMALL("TAK (Tom's lossless Audio Kompressor)"),
+    .capabilities     = AV_CODEC_CAP_DR1,
     .sample_fmts      = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
                                                         AV_SAMPLE_FMT_S16P,
                                                         AV_SAMPLE_FMT_S32P,