]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/8svx.c
zmbv: Fix keyframe fault protextion.
[ffmpeg] / libavcodec / 8svx.c
index 3864d61857e908cca02e24365ec8b1523c6c607e..b4fc899a2297bfdb2a53e814ed49cb560f57bc2f 100644 (file)
@@ -41,6 +41,7 @@
 
 /** decoder context */
 typedef struct EightSvxContext {
+    AVFrame frame;
     const int8_t *table;
 
     /* buffer used to store the whole audio decoded/interleaved chunk,
@@ -99,18 +100,20 @@ static int delta_decode(int8_t *dst, const uint8_t *src, int src_size,
     return dst-dst0;
 }
 
-static int eightsvx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
-                                 AVPacket *avpkt)
+/** decode a frame */
+static int eightsvx_decode_frame(AVCodecContext *avctx, void *data,
+                                 int *got_frame_ptr, AVPacket *avpkt)
 {
     EightSvxContext *esc = avctx->priv_data;
-    int out_data_size, n;
+    int n, out_data_size, ret;
+    uint8_t *out_date;
     uint8_t *src, *dst;
 
     /* decode and interleave the first packet */
     if (!esc->samples && avpkt) {
         uint8_t *deinterleaved_samples;
 
-        esc->samples_size = avctx->codec->id == CODEC_ID_8SVX_RAW ?
+        esc->samples_size = avctx->codec->id == CODEC_ID_8SVX_RAW || avctx->codec->id ==CODEC_ID_PCM_S8_PLANAR?
             avpkt->size : avctx->channels + (avpkt->size-avctx->channels) * 2;
         if (!(esc->samples = av_malloc(esc->samples_size)))
             return AVERROR(ENOMEM);
@@ -121,6 +124,10 @@ static int eightsvx_decode_frame(AVCodecContext *avctx, void *data, int *data_si
             int buf_size = avpkt->size;
             int n = esc->samples_size;
 
+            if (buf_size < 2) {
+                av_log(avctx, AV_LOG_ERROR, "packet size is too small\n");
+                return AVERROR(EINVAL);
+            }
             if (!(deinterleaved_samples = av_mallocz(n)))
                 return AVERROR(ENOMEM);
 
@@ -141,19 +148,22 @@ static int eightsvx_decode_frame(AVCodecContext *avctx, void *data, int *data_si
             memcpy(esc->samples, deinterleaved_samples, esc->samples_size);
     }
 
-    /* return single packed with fixed size */
-    out_data_size = FFMIN(MAX_FRAME_SIZE, esc->samples_size - esc->samples_idx);
-    if (*data_size < out_data_size) {
-        av_log(avctx, AV_LOG_ERROR, "Provided buffer with size %d is too small.\n", *data_size);
-        return AVERROR(EINVAL);
+    /* get output buffer */
+    esc->frame.nb_samples = (FFMIN(MAX_FRAME_SIZE, esc->samples_size - esc->samples_idx) +avctx->channels-1)  / avctx->channels;
+    if ((ret = avctx->get_buffer(avctx, &esc->frame)) < 0) {
+        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+        return ret;
     }
 
-    *data_size = out_data_size;
-    dst = data;
+    *got_frame_ptr   = 1;
+    *(AVFrame *)data = esc->frame;
+
+    dst = esc->frame.data[0];
     src = esc->samples + esc->samples_idx;
+    out_data_size = esc->frame.nb_samples * avctx->channels;
     for (n = out_data_size; n > 0; n--)
         *dst++ = *src++ + 128;
-    esc->samples_idx += *data_size;
+    esc->samples_idx += out_data_size;
 
     return avctx->codec->id == CODEC_ID_8SVX_FIB || avctx->codec->id == CODEC_ID_8SVX_EXP ?
         (avctx->frame_number == 0)*2 + out_data_size / 2 :
@@ -164,7 +174,7 @@ static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
 {
     EightSvxContext *esc = avctx->priv_data;
 
-    if (avctx->channels > 2) {
+    if (avctx->channels < 1 || avctx->channels > 2) {
         av_log(avctx, AV_LOG_ERROR, "8SVX does not support more than 2 channels\n");
         return AVERROR_INVALIDDATA;
     }
@@ -172,6 +182,7 @@ static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
     switch (avctx->codec->id) {
     case CODEC_ID_8SVX_FIB: esc->table = fibonacci;    break;
     case CODEC_ID_8SVX_EXP: esc->table = exponential;  break;
+    case CODEC_ID_PCM_S8_PLANAR:
     case CODEC_ID_8SVX_RAW: esc->table = NULL;         break;
     default:
         av_log(avctx, AV_LOG_ERROR, "Invalid codec id %d.\n", avctx->codec->id);
@@ -179,6 +190,9 @@ static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
     }
     avctx->sample_fmt = AV_SAMPLE_FMT_U8;
 
+    avcodec_get_frame_defaults(&esc->frame);
+    avctx->coded_frame = &esc->frame;
+
     return 0;
 }
 
@@ -201,6 +215,7 @@ AVCodec ff_eightsvx_fib_decoder = {
   .init           = eightsvx_decode_init,
   .decode         = eightsvx_decode_frame,
   .close          = eightsvx_decode_close,
+  .capabilities   = CODEC_CAP_DR1,
   .long_name      = NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
 };
 
@@ -212,16 +227,18 @@ AVCodec ff_eightsvx_exp_decoder = {
   .init           = eightsvx_decode_init,
   .decode         = eightsvx_decode_frame,
   .close          = eightsvx_decode_close,
+  .capabilities   = CODEC_CAP_DR1,
   .long_name      = NULL_IF_CONFIG_SMALL("8SVX exponential"),
 };
 
-AVCodec ff_eightsvx_raw_decoder = {
-  .name           = "8svx_raw",
-  .type           = AVMEDIA_TYPE_AUDIO,
-  .id             = CODEC_ID_8SVX_RAW,
-  .priv_data_size = sizeof(EightSvxContext),
-  .init           = eightsvx_decode_init,
-  .decode         = eightsvx_decode_frame,
-  .close          = eightsvx_decode_close,
-  .long_name      = NULL_IF_CONFIG_SMALL("8SVX rawaudio"),
+AVCodec ff_pcm_s8_planar_decoder = {
+    .name           = "pcm_s8_planar",
+    .type           = AVMEDIA_TYPE_AUDIO,
+    .id             = CODEC_ID_PCM_S8_PLANAR,
+    .priv_data_size = sizeof(EightSvxContext),
+    .init           = eightsvx_decode_init,
+    .close          = eightsvx_decode_close,
+    .decode         = eightsvx_decode_frame,
+    .capabilities   = CODEC_CAP_DR1,
+    .long_name      = NULL_IF_CONFIG_SMALL("PCM signed 8-bit planar"),
 };