Merge commit 'a1a143adb0fd11c474221431417cff25db7d920f'
[ffmpeg.git] / libavcodec / pictordec.c
index b3b5f7e..b29a484 100644 (file)
 #include "avcodec.h"
 #include "bytestream.h"
 #include "cga_data.h"
+#include "internal.h"
 
 typedef struct PicContext {
-    AVFrame frame;
     int width, height;
     int nb_planes;
+    GetByteContext g;
 } PicContext;
 
-static void picmemset_8bpp(PicContext *s, int value, int run, int *x, int *y)
+static void picmemset_8bpp(PicContext *s, AVFrame *frame, int value, int run,
+                           int *x, int *y)
 {
     while (run > 0) {
-        uint8_t *d = s->frame.data[0] + *y * s->frame.linesize[0];
+        uint8_t *d = frame->data[0] + *y * frame->linesize[0];
         if (*x + run >= s->width) {
             int n = s->width - *x;
             memset(d + *x, value, n);
@@ -55,17 +57,18 @@ static void picmemset_8bpp(PicContext *s, int value, int run, int *x, int *y)
     }
 }
 
-static void picmemset(PicContext *s, int value, int run, int *x, int *y, int *plane, int bits_per_plane)
+static void picmemset(PicContext *s, AVFrame *frame, unsigned value, int run,
+                      int *x, int *y, int *plane, int bits_per_plane)
 {
     uint8_t *d;
     int shift = *plane * bits_per_plane;
-    int mask  = ((1 << bits_per_plane) - 1) << shift;
+    unsigned mask  = ((1U << bits_per_plane) - 1) << shift;
     value   <<= shift;
 
     while (run > 0) {
         int j;
         for (j = 8-bits_per_plane; j >= 0; j -= bits_per_plane) {
-            d = s->frame.data[0] + *y * s->frame.linesize[0];
+            d = frame->data[0] + *y * frame->linesize[0];
             d[*x] |= (value >> j) & mask;
             *x += 1;
             if (*x == s->width) {
@@ -74,10 +77,10 @@ static void picmemset(PicContext *s, int value, int run, int *x, int *y, int *pl
                 if (*y < 0) {
                    *y = s->height - 1;
                    *plane += 1;
+                   if (*plane >= s->nb_planes)
+                       return;
                    value <<= bits_per_plane;
                    mask  <<= bits_per_plane;
-                   if (*plane >= s->nb_planes)
-                       break;
                 }
             }
         }
@@ -94,90 +97,87 @@ static const uint8_t cga_mode45_index[6][4] = {
     [5] = { 0, 11, 12, 15 }, // mode5, high intensity
 };
 
-static av_cold int decode_init(AVCodecContext *avctx)
-{
-    PicContext *s = avctx->priv_data;
-
-    avcodec_get_frame_defaults(&s->frame);
-    return 0;
-}
-
 static int decode_frame(AVCodecContext *avctx,
-                        void *data, int *data_size,
+                        void *data, int *got_frame,
                         AVPacket *avpkt)
 {
     PicContext *s = avctx->priv_data;
-    int buf_size = avpkt->size;
-    const uint8_t *buf = avpkt->data;
-    const uint8_t *buf_end = avpkt->data + buf_size;
+    AVFrame *frame = data;
     uint32_t *palette;
-    int bits_per_plane, bpp, etype, esize, npal;
-    int i, x, y, plane;
+    int bits_per_plane, bpp, etype, esize, npal, pos_after_pal;
+    int i, x, y, plane, tmp, ret, val;
 
-    if (buf_size < 11)
+    bytestream2_init(&s->g, avpkt->data, avpkt->size);
+
+    if (bytestream2_get_bytes_left(&s->g) < 11)
         return AVERROR_INVALIDDATA;
 
-    if (bytestream_get_le16(&buf) != 0x1234)
+    if (bytestream2_get_le16u(&s->g) != 0x1234)
         return AVERROR_INVALIDDATA;
-    s->width  = bytestream_get_le16(&buf);
-    s->height = bytestream_get_le16(&buf);
-    buf += 4;
-    bits_per_plane    = *buf & 0xF;
-    s->nb_planes      = (*buf++ >> 4) + 1;
-    bpp               = s->nb_planes ? bits_per_plane*s->nb_planes : bits_per_plane;
+
+    s->width       = bytestream2_get_le16u(&s->g);
+    s->height      = bytestream2_get_le16u(&s->g);
+    bytestream2_skip(&s->g, 4);
+    tmp            = bytestream2_get_byteu(&s->g);
+    bits_per_plane = tmp & 0xF;
+    s->nb_planes   = (tmp >> 4) + 1;
+    bpp            = bits_per_plane * s->nb_planes;
     if (bits_per_plane > 8 || bpp < 1 || bpp > 32) {
-        av_log_ask_for_sample(avctx, "unsupported bit depth\n");
-        return AVERROR_INVALIDDATA;
+        avpriv_request_sample(avctx, "Unsupported bit depth");
+        return AVERROR_PATCHWELCOME;
     }
 
-    if (*buf == 0xFF || bpp == 8) {
-        buf += 2;
-        etype  = bytestream_get_le16(&buf);
-        esize  = bytestream_get_le16(&buf);
-        if (buf_end - buf < esize)
+    if (bytestream2_peek_byte(&s->g) == 0xFF || bpp == 1 || bpp == 4 || bpp == 8) {
+        bytestream2_skip(&s->g, 2);
+        etype = bytestream2_get_le16(&s->g);
+        esize = bytestream2_get_le16(&s->g);
+        if (bytestream2_get_bytes_left(&s->g) < esize)
             return AVERROR_INVALIDDATA;
     } else {
         etype = -1;
         esize = 0;
     }
 
-    avctx->pix_fmt = PIX_FMT_PAL8;
+    avctx->pix_fmt = AV_PIX_FMT_PAL8;
 
-    if (s->width != avctx->width && s->height != avctx->height) {
-        if (av_image_check_size(s->width, s->height, 0, avctx) < 0)
-            return -1;
-        avcodec_set_dimensions(avctx, s->width, s->height);
-        if (s->frame.data[0])
-            avctx->release_buffer(avctx, &s->frame);
-    }
-
-    if (avctx->get_buffer(avctx, &s->frame) < 0){
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if (av_image_check_size(s->width, s->height, 0, avctx) < 0)
         return -1;
+    if (s->width != avctx->width || s->height != avctx->height) {
+        ret = ff_set_dimensions(avctx, s->width, s->height);
+        if (ret < 0)
+            return ret;
     }
-    memset(s->frame.data[0], 0, s->height * s->frame.linesize[0]);
-    s->frame.pict_type           = AV_PICTURE_TYPE_I;
-    s->frame.palette_has_changed = 1;
 
-    palette = (uint32_t*)s->frame.data[1];
-    if (etype == 1 && esize > 1 && *buf < 6) {
-        int idx = *buf;
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
+        return ret;
+    memset(frame->data[0], 0, s->height * frame->linesize[0]);
+    frame->pict_type           = AV_PICTURE_TYPE_I;
+    frame->palette_has_changed = 1;
+
+    pos_after_pal = bytestream2_tell(&s->g) + esize;
+    palette = (uint32_t*)frame->data[1];
+    if (etype == 1 && esize > 1 && bytestream2_peek_byte(&s->g) < 6) {
+        int idx = bytestream2_get_byte(&s->g);
         npal = 4;
         for (i = 0; i < npal; i++)
             palette[i] = ff_cga_palette[ cga_mode45_index[idx][i] ];
     } else if (etype == 2) {
         npal = FFMIN(esize, 16);
-        for (i = 0; i < npal; i++)
-            palette[i] = ff_cga_palette[ FFMIN(buf[i], 16)];
+        for (i = 0; i < npal; i++) {
+            int pal_idx = bytestream2_get_byte(&s->g);
+            palette[i]  = ff_cga_palette[FFMIN(pal_idx, 15)];
+        }
     } else if (etype == 3) {
         npal = FFMIN(esize, 16);
-        for (i = 0; i < npal; i++)
-            palette[i] = ff_ega_palette[ FFMIN(buf[i], 63)];
+        for (i = 0; i < npal; i++) {
+            int pal_idx = bytestream2_get_byte(&s->g);
+            palette[i]  = ff_ega_palette[FFMIN(pal_idx, 63)];
+        }
     } else if (etype == 4 || etype == 5) {
         npal = FFMIN(esize / 3, 256);
         for (i = 0; i < npal; i++) {
-            palette[i] = AV_RB24(buf + i*3) << 2;
-            palette[i] |= 0xFF << 24 | palette[i] >> 6 & 0x30303;
+            palette[i] = bytestream2_get_be24(&s->g) << 2;
+            palette[i] |= 0xFFU << 24 | palette[i] >> 6 & 0x30303;
         }
     } else {
         if (bpp == 1) {
@@ -195,67 +195,73 @@ static int decode_frame(AVCodecContext *avctx,
     }
     // fill remaining palette entries
     memset(palette + npal, 0, AVPALETTE_SIZE - npal * 4);
-    buf += esize;
-
+    // skip remaining palette bytes
+    bytestream2_seek(&s->g, pos_after_pal, SEEK_SET);
 
+    val = 0;
     y = s->height - 1;
-    if (bytestream_get_le16(&buf)) {
+    if (bytestream2_get_le16(&s->g)) {
         x = 0;
         plane = 0;
-        while (y >= 0 && buf_end - buf >= 6) {
-            const uint8_t *buf_pend = buf + FFMIN(AV_RL16(buf), buf_end - buf);
-            //ignore uncompressed block size reported at buf[2]
-            int marker = buf[4];
-            buf += 5;
+        while (bytestream2_get_bytes_left(&s->g) >= 6) {
+            int stop_size, marker, t1, t2;
+
+            t1        = bytestream2_get_bytes_left(&s->g);
+            t2        = bytestream2_get_le16(&s->g);
+            stop_size = t1 - FFMIN(t1, t2);
+            // ignore uncompressed block size
+            bytestream2_skip(&s->g, 2);
+            marker    = bytestream2_get_byte(&s->g);
 
-            while (plane < s->nb_planes && y >= 0 && buf_pend - buf >= 1) {
+            while (plane < s->nb_planes &&
+                   bytestream2_get_bytes_left(&s->g) > stop_size) {
                 int run = 1;
-                int val = *buf++;
+                val = bytestream2_get_byte(&s->g);
                 if (val == marker) {
-                    run = *buf++;
+                    run = bytestream2_get_byte(&s->g);
                     if (run == 0)
-                        run = bytestream_get_le16(&buf);
-                    val = *buf++;
+                        run = bytestream2_get_le16(&s->g);
+                    val = bytestream2_get_byte(&s->g);
                 }
-                if (buf > buf_end)
+                if (!bytestream2_get_bytes_left(&s->g))
                     break;
 
                 if (bits_per_plane == 8) {
-                    picmemset_8bpp(s, val, run, &x, &y);
+                    picmemset_8bpp(s, frame, val, run, &x, &y);
+                    if (y < 0)
+                        goto finish;
                 } else {
-                    picmemset(s, val, run, &x, &y, &plane, bits_per_plane);
+                    picmemset(s, frame, val, run, &x, &y, &plane, bits_per_plane);
                 }
             }
         }
+
+        if (plane < s->nb_planes && x < avctx->width) {
+            int run = (y + 1) * avctx->width - x;
+            if (bits_per_plane == 8)
+                picmemset_8bpp(s, frame, val, run, &x, &y);
+            else
+                picmemset(s, frame, val, run / (8 / bits_per_plane), &x, &y, &plane, bits_per_plane);
+        }
     } else {
-        while (y >= 0 && buf < buf_end) {
-            memcpy(s->frame.data[0] + y * s->frame.linesize[0], buf, FFMIN(avctx->width, buf_end - buf));
-            buf += avctx->width;
+        while (y >= 0 && bytestream2_get_bytes_left(&s->g) > 0) {
+            memcpy(frame->data[0] + y * frame->linesize[0], s->g.buffer, FFMIN(avctx->width, bytestream2_get_bytes_left(&s->g)));
+            bytestream2_skip(&s->g, avctx->width);
             y--;
         }
     }
+finish:
 
-    *data_size = sizeof(AVFrame);
-    *(AVFrame*)data = s->frame;
-    return buf_size;
-}
-
-static av_cold int decode_end(AVCodecContext *avctx)
-{
-    PicContext *s = avctx->priv_data;
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
-    return 0;
+    *got_frame      = 1;
+    return avpkt->size;
 }
 
 AVCodec ff_pictor_decoder = {
     .name           = "pictor",
+    .long_name      = NULL_IF_CONFIG_SMALL("Pictor/PC Paint"),
     .type           = AVMEDIA_TYPE_VIDEO,
-    .id             = CODEC_ID_PICTOR,
+    .id             = AV_CODEC_ID_PICTOR,
     .priv_data_size = sizeof(PicContext),
-    .init           = decode_init,
-    .close          = decode_end,
     .decode         = decode_frame,
-    .capabilities   = CODEC_CAP_DR1,
-    .long_name = NULL_IF_CONFIG_SMALL("Pictor/PC Paint"),
+    .capabilities   = AV_CODEC_CAP_DR1,
 };