]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/zmbv.c
lavc: add Intel libmfx-based MPEG2 decoder.
[ffmpeg] / libavcodec / zmbv.c
index a4546356eccaf075c907785cc755f0327f938371..f49cbdb014f72c438260069a8a851a9e80afe6f8 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "libavutil/common.h"
 #include "libavutil/intreadwrite.h"
 #include "avcodec.h"
+#include "internal.h"
 
 #include <zlib.h>
 
@@ -52,7 +54,6 @@ enum ZmbvFormat {
  */
 typedef struct ZmbvContext {
     AVCodecContext *avctx;
-    AVFrame pic;
 
     int bpp;
     unsigned int decomp_size;
@@ -396,24 +397,20 @@ static int zmbv_decode_intra(ZmbvContext *c)
     return 0;
 }
 
-static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
+static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
 {
+    AVFrame *frame = data;
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
     ZmbvContext * const c = avctx->priv_data;
     int zret = Z_OK; // Zlib return code
     int len = buf_size;
-    int hi_ver, lo_ver;
+    int hi_ver, lo_ver, ret;
     uint8_t *tmp;
 
-    if (c->pic.data[0])
-            avctx->release_buffer(avctx, &c->pic);
-
-    c->pic.reference = 1;
-    c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
-    if (avctx->get_buffer(avctx, &c->pic) < 0) {
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
-        return -1;
+        return ret;
     }
 
     /* parse header */
@@ -426,6 +423,8 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPac
         c->fmt = buf[3];
         c->bw = buf[4];
         c->bh = buf[5];
+        c->decode_intra = NULL;
+        c->decode_xor = NULL;
 
         buf += 6;
         len -= 6;
@@ -433,19 +432,16 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPac
                "Flags=%X ver=%i.%i comp=%i fmt=%i blk=%ix%i\n",
                c->flags,hi_ver,lo_ver,c->comp,c->fmt,c->bw,c->bh);
         if (hi_ver != 0 || lo_ver != 1) {
-            av_log(avctx, AV_LOG_ERROR, "Unsupported version %i.%i\n",
-            hi_ver, lo_ver);
-            return -1;
+            avpriv_request_sample(avctx, "Version %i.%i", hi_ver, lo_ver);
+            return AVERROR_PATCHWELCOME;
         }
         if (c->bw == 0 || c->bh == 0) {
-            av_log(avctx, AV_LOG_ERROR, "Unsupported block size %ix%i\n",
-                   c->bw, c->bh);
-            return -1;
+            avpriv_request_sample(avctx, "Block size %ix%i", c->bw, c->bh);
+            return AVERROR_PATCHWELCOME;
         }
         if (c->comp != 0 && c->comp != 1) {
-            av_log(avctx, AV_LOG_ERROR, "Unsupported compression type %i\n",
-                   c->comp);
-            return -1;
+            avpriv_request_sample(avctx, "Compression type %i", c->comp);
+            return AVERROR_PATCHWELCOME;
         }
 
         switch (c->fmt) {
@@ -475,53 +471,59 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPac
         default:
             c->decode_intra = NULL;
             c->decode_xor = NULL;
-            av_log(avctx, AV_LOG_ERROR,
-                   "Unsupported (for now) format %i\n", c->fmt);
-            return -1;
+            avpriv_request_sample(avctx, "Format %i", c->fmt);
+            return AVERROR_PATCHWELCOME;
         }
 
         zret = inflateReset(&c->zstream);
         if (zret != Z_OK) {
             av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
-            return -1;
-         }
-
-         tmp = av_realloc(c->cur,  avctx->width * avctx->height * (c->bpp / 8));
-         if (!tmp)
-             return AVERROR(ENOMEM);
-         c->cur = tmp;
-         tmp = av_realloc(c->prev, avctx->width * avctx->height * (c->bpp / 8));
-         if (!tmp)
-             return AVERROR(ENOMEM);
-         c->prev = tmp;
-         c->bx = (c->width + c->bw - 1) / c->bw;
-         c->by = (c->height+ c->bh - 1) / c->bh;
-     }
-
-     if (c->decode_intra == NULL) {
-         av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n");
-         return -1;
-     }
-
-     if (c->comp == 0) { //Uncompressed data
-         memcpy(c->decomp_buf, buf, len);
-         c->decomp_size = 1;
-     } else { // ZLIB-compressed data
+            return AVERROR_UNKNOWN;
+        }
+
+        tmp = av_realloc(c->cur,  avctx->width * avctx->height * (c->bpp / 8));
+        if (!tmp)
+            return AVERROR(ENOMEM);
+        c->cur = tmp;
+        tmp = av_realloc(c->prev, avctx->width * avctx->height * (c->bpp / 8));
+        if (!tmp)
+            return AVERROR(ENOMEM);
+        c->prev = tmp;
+        c->bx   = (c->width  + c->bw - 1) / c->bw;
+        c->by   = (c->height + c->bh - 1) / c->bh;
+    }
+
+    if (!c->decode_intra) {
+        av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n");
+        return AVERROR_INVALIDDATA;
+    }
+
+    if (c->comp == 0) { //Uncompressed data
+        if (c->decomp_size < len) {
+            av_log(avctx, AV_LOG_ERROR, "Buffer too small\n");
+            return AVERROR_INVALIDDATA;
+        }
+        memcpy(c->decomp_buf, buf, len);
+    } else { // ZLIB-compressed data
         c->zstream.total_in = c->zstream.total_out = 0;
         c->zstream.next_in = buf;
         c->zstream.avail_in = len;
         c->zstream.next_out = c->decomp_buf;
         c->zstream.avail_out = c->decomp_size;
-        inflate(&c->zstream, Z_FINISH);
+        zret = inflate(&c->zstream, Z_SYNC_FLUSH);
+        if (zret != Z_OK && zret != Z_STREAM_END) {
+            av_log(avctx, AV_LOG_ERROR, "inflate error %d\n", zret);
+            return AVERROR_INVALIDDATA;
+        }
         c->decomp_len = c->zstream.total_out;
     }
     if (c->flags & ZMBV_KEYFRAME) {
-        c->pic.key_frame = 1;
-        c->pic.pict_type = AV_PICTURE_TYPE_I;
+        frame->key_frame = 1;
+        frame->pict_type = AV_PICTURE_TYPE_I;
         c->decode_intra(c);
     } else {
-        c->pic.key_frame = 0;
-        c->pic.pict_type = AV_PICTURE_TYPE_P;
+        frame->key_frame = 0;
+        frame->pict_type = AV_PICTURE_TYPE_P;
         if (c->decomp_len)
             c->decode_xor(c);
     }
@@ -531,7 +533,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPac
         uint8_t *out, *src;
         int i, j;
 
-        out = c->pic.data[0];
+        out = frame->data[0];
         src = c->cur;
         switch (c->fmt) {
         case ZMBV_FMT_8BPP:
@@ -542,7 +544,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPac
                     out[i * 3 + 2] = c->pal[(*src) * 3 + 2];
                     src++;
                 }
-                out += c->pic.linesize[0];
+                out += frame->linesize[0];
             }
             break;
         case ZMBV_FMT_15BPP:
@@ -554,7 +556,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPac
                     out[i * 3 + 1] = (tmp & 0x03E0) >> 2;
                     out[i * 3 + 2] = (tmp & 0x001F) << 3;
                 }
-                out += c->pic.linesize[0];
+                out += frame->linesize[0];
             }
             break;
         case ZMBV_FMT_16BPP:
@@ -566,7 +568,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPac
                     out[i * 3 + 1] = (tmp & 0x07E0) >> 3;
                     out[i * 3 + 2] = (tmp & 0x001F) << 3;
                 }
-                out += c->pic.linesize[0];
+                out += frame->linesize[0];
             }
             break;
 #ifdef ZMBV_ENABLE_24BPP
@@ -574,7 +576,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPac
             for (j = 0; j < c->height; j++) {
                 memcpy(out, src, c->width * 3);
                 src += c->width * 3;
-                out += c->pic.linesize[0];
+                out += frame->linesize[0];
             }
             break;
 #endif //ZMBV_ENABLE_24BPP
@@ -585,7 +587,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPac
                     src += 4;
                     AV_WB24(out+(i*3), tmp);
                 }
-                out += c->pic.linesize[0];
+                out += frame->linesize[0];
             }
             break;
         default:
@@ -593,20 +595,12 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPac
         }
         FFSWAP(uint8_t *, c->cur, c->prev);
     }
-    *data_size = sizeof(AVFrame);
-    *(AVFrame*)data = c->pic;
+    *got_frame = 1;
 
     /* always report that the buffer was completely consumed */
     return buf_size;
 }
 
-
-
-/*
- *
- * Init zmbv decoder
- *
- */
 static av_cold int decode_init(AVCodecContext *avctx)
 {
     ZmbvContext * const c = avctx->priv_data;
@@ -622,15 +616,15 @@ static av_cold int decode_init(AVCodecContext *avctx)
     // Needed if zlib unused or init aborted before inflateInit
     memset(&c->zstream, 0, sizeof(z_stream));
 
-    avctx->pix_fmt = PIX_FMT_RGB24;
+    avctx->pix_fmt = AV_PIX_FMT_RGB24;
     c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64);
 
     /* Allocate decompression buffer */
     if (c->decomp_size) {
-        if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
+        if (!(c->decomp_buf = av_malloc(c->decomp_size))) {
             av_log(avctx, AV_LOG_ERROR,
                    "Can't allocate decompression buffer.\n");
-            return 1;
+            return AVERROR(ENOMEM);
         }
     }
 
@@ -640,27 +634,18 @@ static av_cold int decode_init(AVCodecContext *avctx)
     zret = inflateInit(&c->zstream);
     if (zret != Z_OK) {
         av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
-        return 1;
+        return AVERROR_UNKNOWN;
     }
 
     return 0;
 }
 
-
-
-/*
- *
- * Uninit zmbv decoder
- *
- */
 static av_cold int decode_end(AVCodecContext *avctx)
 {
     ZmbvContext * const c = avctx->priv_data;
 
     av_freep(&c->decomp_buf);
 
-    if (c->pic.data[0])
-        avctx->release_buffer(avctx, &c->pic);
     inflateEnd(&c->zstream);
     av_freep(&c->cur);
     av_freep(&c->prev);
@@ -670,12 +655,12 @@ static av_cold int decode_end(AVCodecContext *avctx)
 
 AVCodec ff_zmbv_decoder = {
     .name           = "zmbv",
+    .long_name      = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
     .type           = AVMEDIA_TYPE_VIDEO,
-    .id             = CODEC_ID_ZMBV,
+    .id             = AV_CODEC_ID_ZMBV,
     .priv_data_size = sizeof(ZmbvContext),
     .init           = decode_init,
     .close          = decode_end,
     .decode         = decode_frame,
     .capabilities   = CODEC_CAP_DR1,
-    .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
 };