[FFmpeg-devel,1/3] avcodec/j2kenc: reinitialize state if frame size changes

Submitted by Aman Gupta on Nov. 18, 2017, 12:36 a.m.

Details

Message ID 20171118003648.33514-2-ffmpeg@tmm1.net
State New
Headers show

Commit Message

Aman Gupta Nov. 18, 2017, 12:36 a.m.
From: Aman Gupta <aman@tmm1.net>

---
 libavcodec/j2kenc.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

Patch hide | download patch | download mbox

diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
index 2135ece540..1eb091af3b 100644
--- a/libavcodec/j2kenc.c
+++ b/libavcodec/j2kenc.c
@@ -995,7 +995,15 @@  static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
     Jpeg2000EncoderContext *s = avctx->priv_data;
     uint8_t *chunkstart, *jp2cstart, *jp2hstart;
 
-    if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*9 + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
+    if (pict->height != s->height || pict->width != s->width) {
+        s->height = pict->height;
+        s->width = pict->width;
+        cleanup(s);
+        if ((ret=init_tiles(s)) < 0)
+            return ret;
+    }
+
+    if ((ret = ff_alloc_packet2(avctx, pkt, s->width*s->height*9 + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
         return ret;
 
     // init:
@@ -1031,8 +1039,8 @@  static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
         chunkstart = s->buf;
         bytestream_put_be32(&s->buf, 0);
         bytestream_put_buffer(&s->buf, "ihdr", 4);
-        bytestream_put_be32(&s->buf, avctx->height);
-        bytestream_put_be32(&s->buf, avctx->width);
+        bytestream_put_be32(&s->buf, s->height);
+        bytestream_put_be32(&s->buf, s->width);
         bytestream_put_be16(&s->buf, s->ncomponents);
         bytestream_put_byte(&s->buf, s->cbps[0]);
         bytestream_put_byte(&s->buf, 7);