]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/jpeglsenc.c
avutil/mem: Also poison new av_realloc-allocated blocks
[ffmpeg] / libavcodec / jpeglsenc.c
index 363f7fe201788dff50cd18f043dd5902a8b6d094..3e73b04c0efac1ed25718c98de89a213c2b8b412 100644 (file)
@@ -141,21 +141,19 @@ static inline void ls_encode_run(JLSState *state, PutBitContext *pb, int run,
  * Encode one line of image
  */
 static inline void ls_encode_line(JLSState *state, PutBitContext *pb,
-                                  void *last, void *cur, const void *in, int last2, int w,
+                                  void *tmp, const void *in, int last2, int w,
                                   int stride, int comp, int bits)
 {
     int x = 0;
-    int Ra, Rb, Rc, Rd;
+    int Ra = R(tmp, 0), Rb, Rc = last2, Rd;
     int D0, D1, D2;
 
     while (x < w) {
         int err, pred, sign;
 
         /* compute gradients */
-        Ra = x ? R(cur, x - stride) : R(last, x);
-        Rb = R(last, x);
-        Rc = x ? R(last, x - stride) : last2;
-        Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
+        Rb = R(tmp, x);
+        Rd = (x >= w - stride) ? R(tmp, x) : R(tmp, x + stride);
         D0 = Rd - Rb;
         D1 = Rb - Rc;
         D2 = Rc - Ra;
@@ -170,13 +168,13 @@ static inline void ls_encode_line(JLSState *state, PutBitContext *pb,
             RUNval = Ra;
             while (x < w && (FFABS(R(in, x) - RUNval) <= state->near)) {
                 run++;
-                W(cur, x, Ra);
+                W(tmp, x, Ra);
                 x += stride;
             }
             ls_encode_run(state, pb, run, comp, x < w);
             if (x >= w)
                 return;
-            Rb     = R(last, x);
+            Rb     = R(tmp, x);
             RItype = FFABS(Ra - Rb) <= state->near;
             pred   = RItype ? Ra : Rb;
             err    = R(in, x) - pred;
@@ -194,9 +192,9 @@ static inline void ls_encode_line(JLSState *state, PutBitContext *pb,
                     Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
                 else
                     Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
-                W(cur, x, Ra);
             } else
-                W(cur, x, R(in, x));
+                Ra = R(in, x);
+            W(tmp, x, Ra);
 
             if (err < 0)
                 err += state->range;
@@ -236,12 +234,13 @@ static inline void ls_encode_line(JLSState *state, PutBitContext *pb,
                     Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
                 else
                     Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
-                W(cur, x, Ra);
             } else
-                W(cur, x, R(in, x));
+                Ra = R(in, x);
+            W(tmp, x, Ra);
 
             ls_encode_regular(state, pb, context, err);
         }
+        Rc = Rb;
         x += stride;
     }
 }
@@ -277,21 +276,12 @@ static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt,
     PutByteContext pb;
     PutBitContext pb2;
     GetBitContext gb;
-    uint8_t *buf2 = NULL;
-    uint8_t *zero = NULL;
     const uint8_t *in;
-    uint8_t *last, *cur;
-    JLSState *state = NULL;
+    uint8_t *last = NULL;
+    JLSState state = { 0 };
     int i, size, ret;
     int comps;
 
-#if FF_API_PRIVATE_OPT
-FF_DISABLE_DEPRECATION_WARNINGS
-    if (avctx->prediction_method)
-        ctx->pred = avctx->prediction_method;
-FF_ENABLE_DEPRECATION_WARNINGS
-#endif
-
     if (avctx->pix_fmt == AV_PIX_FMT_GRAY8 ||
         avctx->pix_fmt == AV_PIX_FMT_GRAY16)
         comps = 1;
@@ -302,12 +292,13 @@ FF_ENABLE_DEPRECATION_WARNINGS
                                 AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
         return ret;
 
-    buf2 = av_malloc(pkt->size);
-    if (!buf2)
-        goto memfail;
+    last = av_malloc((unsigned)pkt->size + FFABS(p->linesize[0]));
+    if (!last)
+        return AVERROR(ENOMEM);
+    memset(last, 0, FFABS(p->linesize[0]));
 
     bytestream2_init_writer(&pb, pkt->data, pkt->size);
-    init_put_bits(&pb2, buf2, pkt->size);
+    init_put_bits(&pb2, last + FFABS(p->linesize[0]), pkt->size);
 
     /* write our own JPEG header, can't use mjpeg_picture_header */
     put_marker_byteu(&pb, SOI);
@@ -334,40 +325,31 @@ FF_ENABLE_DEPRECATION_WARNINGS
     bytestream2_put_byteu(&pb, (comps > 1) ? 1 : 0);  // interleaving: 0 - plane, 1 - line
     bytestream2_put_byteu(&pb, 0);  // point transform: none
 
-    state = av_mallocz(sizeof(JLSState));
-    if (!state)
-        goto memfail;
-
     /* initialize JPEG-LS state from JPEG parameters */
-    state->near = ctx->pred;
-    state->bpp  = (avctx->pix_fmt == AV_PIX_FMT_GRAY16) ? 16 : 8;
-    ff_jpegls_reset_coding_parameters(state, 0);
-    ff_jpegls_init_state(state);
-
-    ls_store_lse(state, &pb);
+    state.near = ctx->pred;
+    state.bpp  = (avctx->pix_fmt == AV_PIX_FMT_GRAY16) ? 16 : 8;
+    ff_jpegls_reset_coding_parameters(&state, 0);
+    ff_jpegls_init_state(&state);
 
-    zero = last = av_calloc(FFABS(p->linesize[0]), 2);
-    if (!zero)
-        goto memfail;
-    cur = zero + FFABS(p->linesize[0]);
+    ls_store_lse(&state, &pb);
 
     in = p->data[0];
     if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
         int t = 0;
 
         for (i = 0; i < avctx->height; i++) {
-            ls_encode_line(state, &pb2, last, cur, in, t, avctx->width, 1, 0, 8);
-            t    = last[0];
-            FFSWAP(void *, last, cur);
+            int last0 = last[0];
+            ls_encode_line(&state, &pb2, last, in, t, avctx->width, 1, 0, 8);
+            t   = last0;
             in += p->linesize[0];
         }
     } else if (avctx->pix_fmt == AV_PIX_FMT_GRAY16) {
         int t = 0;
 
         for (i = 0; i < avctx->height; i++) {
-            ls_encode_line(state, &pb2, last, cur, in, t, avctx->width, 1, 0, 16);
-            t    = *((uint16_t *)last);
-            FFSWAP(void *, last, cur);
+            int last0 = *((uint16_t *)last);
+            ls_encode_line(&state, &pb2, last, in, t, avctx->width, 1, 0, 16);
+            t   = last0;
             in += p->linesize[0];
         }
     } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
@@ -377,11 +359,11 @@ FF_ENABLE_DEPRECATION_WARNINGS
         width = avctx->width * 3;
         for (i = 0; i < avctx->height; i++) {
             for (j = 0; j < 3; j++) {
-                ls_encode_line(state, &pb2, last + j, cur + j, in + j, Rc[j],
+                int last0 = last[j];
+                ls_encode_line(&state, &pb2, last + j, in + j, Rc[j],
                                width, 3, j, 8);
-                Rc[j] = last[j];
+                Rc[j] = last0;
             }
-            FFSWAP(void *, last, cur);
             in += p->linesize[0];
         }
     } else if (avctx->pix_fmt == AV_PIX_FMT_BGR24) {
@@ -391,18 +373,15 @@ FF_ENABLE_DEPRECATION_WARNINGS
         width = avctx->width * 3;
         for (i = 0; i < avctx->height; i++) {
             for (j = 2; j >= 0; j--) {
-                ls_encode_line(state, &pb2, last + j, cur + j, in + j, Rc[j],
+                int last0 = last[j];
+                ls_encode_line(&state, &pb2, last + j, in + j, Rc[j],
                                width, 3, j, 8);
-                Rc[j] = last[j];
+                Rc[j] = last0;
             }
-            FFSWAP(void *, last, cur);
             in += p->linesize[0];
         }
     }
 
-    av_freep(&zero);
-    av_freep(&state);
-
     /* the specification says that after doing 0xff escaping unused bits in
      * the last byte must be set to 0, so just append 7 "optional" zero bits
      * to avoid special-casing. */
@@ -410,7 +389,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
     size = put_bits_count(&pb2);
     flush_put_bits(&pb2);
     /* do escape coding */
-    init_get_bits(&gb, buf2, size);
+    init_get_bits(&gb, pb2.buf, size);
     size -= 7;
     while (get_bits_count(&gb) < size) {
         int v;
@@ -421,7 +400,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
             bytestream2_put_byte(&pb, v);
         }
     }
-    av_freep(&buf2);
+    av_freep(&last);
 
     /* End of image */
     put_marker_byte(&pb, EOI);
@@ -432,32 +411,6 @@ FF_ENABLE_DEPRECATION_WARNINGS
     pkt->flags |= AV_PKT_FLAG_KEY;
     *got_packet = 1;
     return 0;
-
-memfail:
-    av_freep(&buf2);
-    av_freep(&state);
-    av_freep(&zero);
-    return AVERROR(ENOMEM);
-}
-
-static av_cold int encode_init_ls(AVCodecContext *ctx)
-{
-#if FF_API_CODED_FRAME
-FF_DISABLE_DEPRECATION_WARNINGS
-    ctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
-    ctx->coded_frame->key_frame = 1;
-FF_ENABLE_DEPRECATION_WARNINGS
-#endif
-
-    if (ctx->pix_fmt != AV_PIX_FMT_GRAY8  &&
-        ctx->pix_fmt != AV_PIX_FMT_GRAY16 &&
-        ctx->pix_fmt != AV_PIX_FMT_RGB24  &&
-        ctx->pix_fmt != AV_PIX_FMT_BGR24) {
-        av_log(ctx, AV_LOG_ERROR,
-               "Only grayscale and RGB24/BGR24 images are supported\n");
-        return -1;
-    }
-    return 0;
 }
 
 #define OFFSET(x) offsetof(JPEGLSContext, x)
@@ -478,14 +431,13 @@ static const AVClass jpegls_class = {
     .version    = LIBAVUTIL_VERSION_INT,
 };
 
-AVCodec ff_jpegls_encoder = {
+const AVCodec ff_jpegls_encoder = {
     .name           = "jpegls",
     .long_name      = NULL_IF_CONFIG_SMALL("JPEG-LS"),
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_JPEGLS,
     .priv_data_size = sizeof(JPEGLSContext),
     .priv_class     = &jpegls_class,
-    .init           = encode_init_ls,
     .capabilities   = AV_CODEC_CAP_FRAME_THREADS,
     .encode2        = encode_picture_ls,
     .pix_fmts       = (const enum AVPixelFormat[]) {