]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/lagarith.c
lavc: add a null bitstream filter
[ffmpeg] / libavcodec / lagarith.c
index 1aa9ec305e2a9fd3516bd322dd08f53b8e28be53..d9667b7afced7e1d200abb3e913a2c9c66c41818 100644 (file)
  * @author Nathan Caldwell
  */
 
+#include <inttypes.h>
+
 #include "avcodec.h"
-#include "get_bits.h"
+#include "bitstream.h"
 #include "mathops.h"
-#include "dsputil.h"
+#include "huffyuvdsp.h"
 #include "lagarithrac.h"
+#include "thread.h"
 
 enum LagarithFrameType {
     FRAME_RAW           = 1,    /**< uncompressed */
@@ -47,20 +50,20 @@ enum LagarithFrameType {
 
 typedef struct LagarithContext {
     AVCodecContext *avctx;
-    AVFrame picture;
-    DSPContext dsp;
+    HuffYUVDSPContext hdsp;
     int zeros;                  /**< number of consecutive zero bytes encountered */
     int zeros_rem;              /**< number of zero bytes remaining to output */
     uint8_t *rgb_planes;
+    int      rgb_planes_allocated;
     int rgb_stride;
 } LagarithContext;
 
 /**
- * Compute the 52bit mantissa of 1/(double)denom.
+ * Compute the 52-bit mantissa of 1/(double)denom.
  * This crazy format uses floats in an entropy coder and we have to match x86
  * rounding exactly, thus ordinary floats aren't portable enough.
  * @param denom denominator
- * @return 52bit mantissa
+ * @return 52-bit mantissa
  * @see softfloat_mul
  */
 static uint64_t softfloat_reciprocal(uint32_t denom)
@@ -77,9 +80,9 @@ static uint64_t softfloat_reciprocal(uint32_t denom)
 /**
  * (uint32_t)(x*f), where f has the given mantissa, and exponent 0
  * Used in combination with softfloat_reciprocal computes x/(double)denom.
- * @param x 32bit integer factor
+ * @param x 32-bit integer factor
  * @param mantissa mantissa of f with exponent 0
- * @return 32bit integer value (x*f)
+ * @return 32-bit integer value (x*f)
  * @see softfloat_reciprocal
  */
 static uint32_t softfloat_mul(uint32_t x, uint64_t mantissa)
@@ -98,7 +101,7 @@ static uint8_t lag_calc_zero_run(int8_t x)
     return (x << 1) ^ (x >> 7);
 }
 
-static int lag_decode_prob(GetBitContext *gb, uint32_t *value)
+static int lag_decode_prob(BitstreamContext *bc, uint32_t *value)
 {
     static const uint8_t series[] = { 1, 2, 3, 5, 8, 13, 21 };
     int i;
@@ -111,7 +114,7 @@ static int lag_decode_prob(GetBitContext *gb, uint32_t *value)
         if (prevbit && bit)
             break;
         prevbit = bit;
-        bit = get_bits1(gb);
+        bit = bitstream_read_bit(bc);
         if (bit && !prevbit)
             bits += series[i];
     }
@@ -124,7 +127,7 @@ static int lag_decode_prob(GetBitContext *gb, uint32_t *value)
         return 0;
     }
 
-    val  = get_bits_long(gb, bits);
+    val  = bitstream_read(bc, bits);
     val |= 1 << bits;
 
     *value = val - 1;
@@ -132,7 +135,7 @@ static int lag_decode_prob(GetBitContext *gb, uint32_t *value)
     return 0;
 }
 
-static int lag_read_prob_header(lag_rac *rac, GetBitContext *gb)
+static int lag_read_prob_header(lag_rac *rac, BitstreamContext *bc)
 {
     int i, j, scale_factor;
     unsigned prob, cumulative_target;
@@ -143,7 +146,7 @@ static int lag_read_prob_header(lag_rac *rac, GetBitContext *gb)
     rac->prob[257] = UINT_MAX;
     /* Read probabilities from bitstream */
     for (i = 1; i < 257; i++) {
-        if (lag_decode_prob(gb, &rac->prob[i]) < 0) {
+        if (lag_decode_prob(bc, &rac->prob[i]) < 0) {
             av_log(rac->avctx, AV_LOG_ERROR, "Invalid probability encountered.\n");
             return -1;
         }
@@ -153,7 +156,7 @@ static int lag_read_prob_header(lag_rac *rac, GetBitContext *gb)
         }
         cumul_prob += rac->prob[i];
         if (!rac->prob[i]) {
-            if (lag_decode_prob(gb, &prob)) {
+            if (lag_decode_prob(bc, &prob)) {
                 av_log(rac->avctx, AV_LOG_ERROR, "Invalid probability run encountered.\n");
                 return -1;
             }
@@ -197,8 +200,8 @@ static int lag_read_prob_header(lag_rac *rac, GetBitContext *gb)
             }
             /* Comment from reference source:
              * if (b & 0x80 == 0) {     // order of operations is 'wrong'; it has been left this way
-             *                          // since the compression change is negligable and fixing it
-             *                          // breaks backwards compatibilty
+             *                          // since the compression change is negligible and fixing it
+             *                          // breaks backwards compatibility
              *      b =- (signed int)b;
              *      b &= 0xFF;
              * } else {
@@ -222,8 +225,8 @@ static void add_lag_median_prediction(uint8_t *dst, uint8_t *src1,
                                       uint8_t *diff, int w, int *left,
                                       int *left_top)
 {
-    /* This is almost identical to add_hfyu_median_prediction in dsputil.h.
-     * However the &0xFF on the gradient predictor yealds incorrect output
+    /* This is almost identical to add_hfyu_median_pred in huffyuvdsp.h.
+     * However the &0xFF on the gradient predictor yields incorrect output
      * for lagarith.
      */
     int i;
@@ -247,24 +250,71 @@ static void lag_pred_line(LagarithContext *l, uint8_t *buf,
 {
     int L, TL;
 
-    /* Left pixel is actually prev_row[width] */
-    L = buf[width - stride - 1];
     if (!line) {
+        int i, align_width = (width - 1) & ~31;
         /* Left prediction only for first line */
-        L = l->dsp.add_hfyu_left_prediction(buf + 1, buf + 1,
-                                            width - 1, buf[0]);
+        L = l->hdsp.add_hfyu_left_pred(buf + 1, buf + 1, align_width, buf[0]);
+        for (i = align_width + 1; i < width; i++)
+            buf[i] += buf[i - 1];
+    } else {
+        /* Left pixel is actually prev_row[width] */
+        L = buf[width - stride - 1];
+
+        if (line == 1) {
+            /* Second line, left predict first pixel, the rest of the line is median predicted
+             * NOTE: In the case of RGB this pixel is top predicted */
+            TL = l->avctx->pix_fmt == AV_PIX_FMT_YUV420P ? buf[-stride] : L;
+        } else {
+            /* Top left is 2 rows back, last pixel */
+            TL = buf[width - (2 * stride) - 1];
+        }
+
+        add_lag_median_prediction(buf, buf - stride, buf,
+                                  width, &L, &TL);
+    }
+}
+
+static void lag_pred_line_yuy2(LagarithContext *l, uint8_t *buf,
+                               int width, int stride, int line,
+                               int is_luma)
+{
+    int L, TL;
+
+    if (!line) {
+        int i, align_width;
+        if (is_luma) {
+            buf++;
+            width--;
+        }
+
+        align_width = (width - 1) & ~31;
+        l->hdsp.add_hfyu_left_pred(buf + 1, buf + 1, align_width, buf[0]);
+
+        for (i = align_width + 1; i < width; i++)
+            buf[i] += buf[i - 1];
+
         return;
-    } else if (line == 1) {
-        /* Second line, left predict first pixel, the rest of the line is median predicted
-         * NOTE: In the case of RGB this pixel is top predicted */
-        TL = l->avctx->pix_fmt == PIX_FMT_YUV420P ? buf[-stride] : L;
+    }
+    if (line == 1) {
+        const int HEAD = is_luma ? 4 : 2;
+        int i;
+
+        L  = buf[width - stride - 1];
+        TL = buf[HEAD  - stride - 1];
+        for (i = 0; i < HEAD; i++) {
+            L += buf[i];
+            buf[i] = L;
+        }
+        for (; i < width; i++) {
+            L      = mid_pred(L & 0xFF, buf[i - stride], (L + buf[i - stride] - TL) & 0xFF) + buf[i];
+            TL     = buf[i - stride];
+            buf[i] = L;
+        }
     } else {
-        /* Top left is 2 rows back, last pixel */
         TL = buf[width - (2 * stride) - 1];
+        L  = buf[width - stride - 1];
+        l->hdsp.add_hfyu_median_pred(buf, buf - stride, buf, width, &L, &TL);
     }
-
-    add_lag_median_prediction(buf, buf - stride, buf,
-                              width, &L, &TL);
 }
 
 static int lag_decode_line(LagarithContext *l, lag_rac *rac,
@@ -310,13 +360,13 @@ handle_zeros:
 }
 
 static int lag_decode_zero_run_line(LagarithContext *l, uint8_t *dst,
-                                    const uint8_t *src, int width,
-                                    int esc_count)
+                                    const uint8_t *src, const uint8_t *src_end,
+                                    int width, int esc_count)
 {
     int i = 0;
     int count;
     uint8_t zero_run = 0;
-    const uint8_t *start = src;
+    const uint8_t *src_start = src;
     uint8_t mask1 = -(esc_count < 2);
     uint8_t mask2 = -(esc_count < 3);
     uint8_t *end = dst + (width - 2);
@@ -324,6 +374,11 @@ static int lag_decode_zero_run_line(LagarithContext *l, uint8_t *dst,
 output_zeros:
     if (l->zeros_rem) {
         count = FFMIN(l->zeros_rem, width - i);
+        if (end - dst < count) {
+            av_log(l->avctx, AV_LOG_ERROR, "Too many zeros remaining.\n");
+            return AVERROR_INVALIDDATA;
+        }
+
         memset(dst, 0, count);
         l->zeros_rem -= count;
         dst += count;
@@ -333,6 +388,8 @@ output_zeros:
         i = 0;
         while (!zero_run && dst + i < end) {
             i++;
+            if (src + i >= src_end)
+                return AVERROR_INVALIDDATA;
             zero_run =
                 !(src[i] | (src[i + 1] & mask1) | (src[i + 2] & mask2));
         }
@@ -348,9 +405,10 @@ output_zeros:
         } else {
             memcpy(dst, src, i);
             src += i;
+            dst += i;
         }
     }
-    return start - src;
+    return src_start - src;
 }
 
 
@@ -364,8 +422,9 @@ static int lag_decode_arith_plane(LagarithContext *l, uint8_t *dst,
     uint32_t length;
     uint32_t offset = 1;
     int esc_count = src[0];
-    GetBitContext gb;
+    BitstreamContext bc;
     lag_rac rac;
+    const uint8_t *src_end = src + src_size;
 
     rac.avctx = l->avctx;
     l->zeros = 0;
@@ -377,12 +436,12 @@ static int lag_decode_arith_plane(LagarithContext *l, uint8_t *dst,
             offset += 4;
         }
 
-        init_get_bits(&gb, src + offset, src_size * 8);
+        bitstream_init(&bc, src + offset, src_size * 8);
 
-        if (lag_read_prob_header(&rac, &gb) < 0)
+        if (lag_read_prob_header(&rac, &bc) < 0)
             return -1;
 
-        lag_rac_init(&rac, &gb, length - stride);
+        ff_lag_rac_init(&rac, &bc, length - stride);
 
         for (i = 0; i < height; i++)
             read += lag_decode_line(l, &rac, dst + (i * stride), width,
@@ -390,16 +449,22 @@ static int lag_decode_arith_plane(LagarithContext *l, uint8_t *dst,
 
         if (read > length)
             av_log(l->avctx, AV_LOG_WARNING,
-                   "Output more bytes than length (%d of %d)\n", read,
+                   "Output more bytes than length (%d of %"PRIu32")\n", read,
                    length);
     } else if (esc_count < 8) {
         esc_count -= 4;
         if (esc_count > 0) {
             /* Zero run coding only, no range coding. */
-            for (i = 0; i < height; i++)
-                src += lag_decode_zero_run_line(l, dst + (i * stride), src,
-                                                width, esc_count);
+            for (i = 0; i < height; i++) {
+                int res = lag_decode_zero_run_line(l, dst + (i * stride), src,
+                                                   src_end, width, esc_count);
+                if (res < 0)
+                    return res;
+                src += res;
+            }
         } else {
+            if (src_size < width * height)
+                return AVERROR_INVALIDDATA; // buffer not big enough
             /* Plane is stored uncompressed */
             for (i = 0; i < height; i++) {
                 memcpy(dst + (i * stride), src, width);
@@ -420,9 +485,17 @@ static int lag_decode_arith_plane(LagarithContext *l, uint8_t *dst,
         return -1;
     }
 
-    for (i = 0; i < height; i++) {
-        lag_pred_line(l, dst, width, stride, i);
-        dst += stride;
+    if (l->avctx->pix_fmt != AV_PIX_FMT_YUV422P) {
+        for (i = 0; i < height; i++) {
+            lag_pred_line(l, dst, width, stride, i);
+            dst += stride;
+        }
+    } else {
+        for (i = 0; i < height; i++) {
+            lag_pred_line_yuy2(l, dst, width, stride, i,
+                               width == l->avctx->width);
+            dst += stride;
+        }
     }
 
     return 0;
@@ -437,24 +510,19 @@ static int lag_decode_arith_plane(LagarithContext *l, uint8_t *dst,
  * @return number of consumed bytes on success or negative if decode fails
  */
 static int lag_decode_frame(AVCodecContext *avctx,
-                            void *data, int *data_size, AVPacket *avpkt)
+                            void *data, int *got_frame, AVPacket *avpkt)
 {
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
     LagarithContext *l = avctx->priv_data;
-    AVFrame *const p = &l->picture;
+    ThreadFrame frame = { .f = data };
+    AVFrame *const p  = data;
     uint8_t frametype = 0;
     uint32_t offset_gu = 0, offset_bv = 0, offset_ry = 9;
-    int offs[4];
+    uint32_t offs[4];
     uint8_t *srcs[4], *dst;
-    int i, j;
+    int i, j, planes = 3;
 
-    AVFrame *picture = data;
-
-    if (p->data[0])
-        avctx->release_buffer(avctx, p);
-
-    p->reference = 0;
     p->key_frame = 1;
 
     frametype = buf[0];
@@ -464,9 +532,9 @@ static int lag_decode_frame(AVCodecContext *avctx,
 
     switch (frametype) {
     case FRAME_SOLID_RGBA:
-        avctx->pix_fmt = PIX_FMT_RGB32;
+        avctx->pix_fmt = AV_PIX_FMT_RGB32;
 
-        if (avctx->get_buffer(avctx, p) < 0) {
+        if (ff_thread_get_buffer(avctx, &frame, 0) < 0) {
             av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
             return -1;
         }
@@ -479,34 +547,48 @@ static int lag_decode_frame(AVCodecContext *avctx,
         }
         break;
     case FRAME_ARITH_RGBA:
-        avctx->pix_fmt = PIX_FMT_RGB32;
+        avctx->pix_fmt = AV_PIX_FMT_RGB32;
+        planes = 4;
+        offset_ry += 4;
+        offs[3] = AV_RL32(buf + 9);
+    case FRAME_ARITH_RGB24:
+    case FRAME_U_RGB24:
+        if (frametype == FRAME_ARITH_RGB24 || frametype == FRAME_U_RGB24)
+            avctx->pix_fmt = AV_PIX_FMT_RGB24;
 
-        if (avctx->get_buffer(avctx, p) < 0) {
+        if (ff_thread_get_buffer(avctx, &frame, 0) < 0) {
             av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
             return -1;
         }
+
         offs[0] = offset_bv;
         offs[1] = offset_gu;
-        offs[2] = 13;
-        offs[3] = AV_RL32(buf + 9);
+        offs[2] = offset_ry;
 
+        l->rgb_stride = FFALIGN(avctx->width, 16);
+        av_fast_malloc(&l->rgb_planes, &l->rgb_planes_allocated,
+                       l->rgb_stride * avctx->height * planes + 1);
         if (!l->rgb_planes) {
-            l->rgb_stride = FFALIGN(avctx->width, 16);
-            l->rgb_planes = av_malloc(l->rgb_stride * avctx->height * 4);
-            if (!l->rgb_planes) {
-                av_log(avctx, AV_LOG_ERROR, "cannot allocate temporary buffer\n");
-                return AVERROR(ENOMEM);
-            }
+            av_log(avctx, AV_LOG_ERROR, "cannot allocate temporary buffer\n");
+            return AVERROR(ENOMEM);
         }
-        for (i = 0; i < 4; i++)
+        for (i = 0; i < planes; i++)
             srcs[i] = l->rgb_planes + (i + 1) * l->rgb_stride * avctx->height - l->rgb_stride;
-        for (i = 0; i < 4; i++)
+        if (offset_ry >= buf_size ||
+            offset_gu >= buf_size ||
+            offset_bv >= buf_size ||
+            (planes == 4 && offs[3] >= buf_size)) {
+            av_log(avctx, AV_LOG_ERROR,
+                    "Invalid frame offsets\n");
+            return AVERROR_INVALIDDATA;
+        }
+        for (i = 0; i < planes; i++)
             lag_decode_arith_plane(l, srcs[i],
                                    avctx->width, avctx->height,
                                    -l->rgb_stride, buf + offs[i],
-                                   buf_size);
+                                   buf_size - offs[i]);
         dst = p->data[0];
-        for (i = 0; i < 4; i++)
+        for (i = 0; i < planes; i++)
             srcs[i] = l->rgb_planes + i * l->rgb_stride * avctx->height;
         for (j = 0; j < avctx->height; j++) {
             for (i = 0; i < avctx->width; i++) {
@@ -514,42 +596,81 @@ static int lag_decode_frame(AVCodecContext *avctx,
                 r = srcs[0][i];
                 g = srcs[1][i];
                 b = srcs[2][i];
-                a = srcs[3][i];
                 r += g;
                 b += g;
-                AV_WN32(dst + i * 4, MKBETAG(a, r, g, b));
+                if (frametype == FRAME_ARITH_RGBA) {
+                    a = srcs[3][i];
+                    AV_WN32(dst + i * 4, MKBETAG(a, r, g, b));
+                } else {
+                    dst[i * 3 + 0] = r;
+                    dst[i * 3 + 1] = g;
+                    dst[i * 3 + 2] = b;
+                }
             }
             dst += p->linesize[0];
-            for (i = 0; i < 4; i++)
+            for (i = 0; i < planes; i++)
                 srcs[i] += l->rgb_stride;
         }
         break;
+    case FRAME_ARITH_YUY2:
+        avctx->pix_fmt = AV_PIX_FMT_YUV422P;
+
+        if (ff_thread_get_buffer(avctx, &frame, 0) < 0) {
+            av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+            return -1;
+        }
+
+        if (offset_ry >= buf_size ||
+            offset_gu >= buf_size ||
+            offset_bv >= buf_size) {
+            av_log(avctx, AV_LOG_ERROR,
+                   "Invalid frame offsets\n");
+            return AVERROR_INVALIDDATA;
+        }
+
+        lag_decode_arith_plane(l, p->data[0], avctx->width, avctx->height,
+                               p->linesize[0], buf + offset_ry,
+                               buf_size - offset_ry);
+        lag_decode_arith_plane(l, p->data[1], avctx->width / 2,
+                               avctx->height, p->linesize[1],
+                               buf + offset_gu, buf_size - offset_gu);
+        lag_decode_arith_plane(l, p->data[2], avctx->width / 2,
+                               avctx->height, p->linesize[2],
+                               buf + offset_bv, buf_size - offset_bv);
+        break;
     case FRAME_ARITH_YV12:
-        avctx->pix_fmt = PIX_FMT_YUV420P;
+        avctx->pix_fmt = AV_PIX_FMT_YUV420P;
 
-        if (avctx->get_buffer(avctx, p) < 0) {
+        if (ff_thread_get_buffer(avctx, &frame, 0) < 0) {
             av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
             return -1;
         }
 
+        if (offset_ry >= buf_size ||
+            offset_gu >= buf_size ||
+            offset_bv >= buf_size) {
+            av_log(avctx, AV_LOG_ERROR,
+                   "Invalid frame offsets\n");
+            return AVERROR_INVALIDDATA;
+        }
+
         lag_decode_arith_plane(l, p->data[0], avctx->width, avctx->height,
                                p->linesize[0], buf + offset_ry,
-                               buf_size);
+                               buf_size - offset_ry);
         lag_decode_arith_plane(l, p->data[2], avctx->width / 2,
                                avctx->height / 2, p->linesize[2],
-                               buf + offset_gu, buf_size);
+                               buf + offset_gu, buf_size - offset_gu);
         lag_decode_arith_plane(l, p->data[1], avctx->width / 2,
                                avctx->height / 2, p->linesize[1],
-                               buf + offset_bv, buf_size);
+                               buf + offset_bv, buf_size - offset_bv);
         break;
     default:
         av_log(avctx, AV_LOG_ERROR,
-               "Unsupported Lagarith frame type: %#x\n", frametype);
+               "Unsupported Lagarith frame type: %#"PRIx8"\n", frametype);
         return -1;
     }
 
-    *picture = *p;
-    *data_size = sizeof(AVFrame);
+    *got_frame = 1;
 
     return buf_size;
 }
@@ -559,7 +680,7 @@ static av_cold int lag_decode_init(AVCodecContext *avctx)
     LagarithContext *l = avctx->priv_data;
     l->avctx = avctx;
 
-    dsputil_init(&l->dsp, avctx);
+    ff_huffyuvdsp_init(&l->hdsp);
 
     return 0;
 }
@@ -568,8 +689,6 @@ static av_cold int lag_decode_end(AVCodecContext *avctx)
 {
     LagarithContext *l = avctx->priv_data;
 
-    if (l->picture.data[0])
-        avctx->release_buffer(avctx, &l->picture);
     av_freep(&l->rgb_planes);
 
     return 0;
@@ -577,12 +696,12 @@ static av_cold int lag_decode_end(AVCodecContext *avctx)
 
 AVCodec ff_lagarith_decoder = {
     .name           = "lagarith",
+    .long_name      = NULL_IF_CONFIG_SMALL("Lagarith lossless"),
     .type           = AVMEDIA_TYPE_VIDEO,
-    .id             = CODEC_ID_LAGARITH,
+    .id             = AV_CODEC_ID_LAGARITH,
     .priv_data_size = sizeof(LagarithContext),
     .init           = lag_decode_init,
     .close          = lag_decode_end,
     .decode         = lag_decode_frame,
-    .capabilities   = CODEC_CAP_DR1,
-    .long_name = NULL_IF_CONFIG_SMALL("Lagarith lossless"),
+    .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
 };