[FFmpeg-devel] [PATCH] libopenjpegenc: recreate image data buffer after encoding frame

Andreas Cadhalpun andreas.cadhalpun at googlemail.com
Tue Oct 11 19:57:52 EEST 2016


openjpeg 2 sets the data pointers of the image components to NULL,
causing segfaults if the image is reused.

Signed-off-by: Andreas Cadhalpun <Andreas.Cadhalpun at googlemail.com>
---

The relevant openjpeg2 code is:
https://sources.debian.net/src/openjpeg2/2.1.2-1/src/lib/openjp2/j2k.c/?hl=10253#L10247

---
 libavcodec/libopenjpegenc.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/libavcodec/libopenjpegenc.c b/libavcodec/libopenjpegenc.c
index 023fdf4..2f0ab20 100644
--- a/libavcodec/libopenjpegenc.c
+++ b/libavcodec/libopenjpegenc.c
@@ -776,6 +776,16 @@ static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
         goto done;
     }
 
+    // openjpeg 2 sets the data pointers of the image components to NULL.
+    // Thus the image can't be reused.
+    opj_image_destroy(ctx->image);
+    ctx->image = mj2_create_image(avctx, &ctx->enc_params);
+    if (!ctx->image) {
+        av_log(avctx, AV_LOG_ERROR, "Error creating the mj2 image\n");
+        ret = AVERROR(EINVAL);
+        goto done;
+    }
+
     av_shrink_packet(pkt, writer.pos);
 #endif // OPENJPEG_MAJOR_VERSION == 1
 
-- 
2.9.3


More information about the ffmpeg-devel mailing list