Re: [FFmpeg-devel] [PATCH 2/3] lavc/libopenjpegenc: add layerrates parameter to allow different compression rates per layer
From fe0afff9c7b8c6f78dcad0720965c6f6a50e7813 Mon Sep 17 00:00:00 2001 From: Jean First jeanfi...@gmail.com Date: Mon, 2 Feb 2015 12:57:03 +0100 Subject: [PATCH] lavc/libopenjpegenc: move opj_create_compress, opj_cio_open and opj_set_event_mgr to libopenjpeg_encode_frame libopenjpegenc crashes with pointer being freed was not allocated when threading is enabled with: ffmpeg -i tests/vsynth1/01.pgm -vcodec libopenjpeg file.j2k Signed-off-by: Jean First jeanfi...@gmail.com --- libavcodec/libopenjpegenc.c | 42 -- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/libavcodec/libopenjpegenc.c b/libavcodec/libopenjpegenc.c index 4039663..53d0fe9 100644 --- a/libavcodec/libopenjpegenc.c +++ b/libavcodec/libopenjpegenc.c @@ -43,9 +43,7 @@ typedef struct { AVClass *avclass; opj_image_t *image; -opj_cio_t *stream; opj_cparameters_t enc_params; -opj_cinfo_t *compress; opj_event_mgr_t event_mgr; int format; int profile; @@ -221,12 +219,6 @@ static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx) ctx-enc_params.tp_on = 1; } -ctx-compress = opj_create_compress(ctx-format); -if (!ctx-compress) { -av_log(avctx, AV_LOG_ERROR, Error creating the compressor\n); -return AVERROR(ENOMEM); -} - ctx-image = mj2_create_image(avctx, ctx-enc_params); if (!ctx-image) { av_log(avctx, AV_LOG_ERROR, Error creating the mj2 image\n); @@ -240,17 +232,9 @@ static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx) goto fail; } -memset(ctx-event_mgr, 0, sizeof(opj_event_mgr_t)); -ctx-event_mgr.info_handler= info_callback; -ctx-event_mgr.error_handler = error_callback; -ctx-event_mgr.warning_handler = warning_callback; -opj_set_event_mgr((opj_common_ptr) ctx-compress, ctx-event_mgr, avctx); - return 0; fail: -opj_destroy_compress(ctx-compress); -ctx-compress = NULL; opj_image_destroy(ctx-image); ctx-image = NULL; av_freep(avctx-coded_frame); @@ -464,9 +448,9 @@ static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet) { LibOpenJPEGContext *ctx = avctx-priv_data; -opj_cinfo_t *compress = ctx-compress; opj_image_t *image = ctx-image; -opj_cio_t *stream = ctx-stream; +opj_cinfo_t *compress = NULL; +opj_cio_t *stream = NULL; int cpyresult = 0; int ret, len; AVFrame *gbrframe; @@ -559,6 +543,12 @@ static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt, return -1; } +compress = opj_create_compress(ctx-format); +if (!compress) { +av_log(avctx, AV_LOG_ERROR, Error creating the compressor\n); +return AVERROR(ENOMEM); +} + opj_setup_encoder(compress, ctx-enc_params, image); stream = opj_cio_open((opj_common_ptr) compress, NULL, 0); @@ -567,6 +557,12 @@ static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt, return AVERROR(ENOMEM); } +memset(ctx-event_mgr, 0, sizeof(opj_event_mgr_t)); +ctx-event_mgr.info_handler= info_callback; +ctx-event_mgr.error_handler = error_callback; +ctx-event_mgr.warning_handler = warning_callback; +opj_set_event_mgr((opj_common_ptr) compress, ctx-event_mgr, avctx); + if (!opj_encode(compress, stream, image, NULL)) { av_log(avctx, AV_LOG_ERROR, Error during the opj encode\n); return -1; @@ -580,6 +576,12 @@ static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt, memcpy(pkt-data, stream-buffer, len); pkt-flags |= AV_PKT_FLAG_KEY; *got_packet = 1; + +opj_cio_close(stream); +stream = NULL; +opj_destroy_compress(compress); +compress = NULL; + return 0; } @@ -587,10 +589,6 @@ static av_cold int libopenjpeg_encode_close(AVCodecContext *avctx) { LibOpenJPEGContext *ctx = avctx-priv_data; -opj_cio_close(ctx-stream); -ctx-stream = NULL; -opj_destroy_compress(ctx-compress); -ctx-compress = NULL; opj_image_destroy(ctx-image); ctx-image = NULL; av_freep(avctx-coded_frame); -- 2.2.2 ___ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
Re: [FFmpeg-devel] [PATCH 2/3] lavc/libopenjpegenc: add layerrates parameter to allow different compression rates per layer
On Mon Feb 02 2015 17:20:44 GMT+0100 (CET), Michael Niedermayer wrote: On Mon, Feb 02, 2015 at 01:18:35PM +0100, Jean First wrote: libopenjpegenc.c | 42 -- 1 file changed, 20 insertions(+), 22 deletions(-) 6bed682adf441fa060b9fef84df173cd758320ed 0001-lavc-libopenjpegenc-move-opj_create_compress-opj_cio.patch From fe0afff9c7b8c6f78dcad0720965c6f6a50e7813 Mon Sep 17 00:00:00 2001 From: Jean First jeanfi...@gmail.com Date: Mon, 2 Feb 2015 12:57:03 +0100 Subject: [PATCH] lavc/libopenjpegenc: move opj_create_compress, opj_cio_open and opj_set_event_mgr to libopenjpeg_encode_frame libopenjpegenc crashes with pointer being freed was not allocated when threading is enabled with: ffmpeg -i tests/vsynth1/01.pgm -vcodec libopenjpeg file.j2k cannot reproduce do i assume correctly that this is a bug in libopenjpeg and unlreated to this patch ? Yes, that's my guess. Any other implementation I saw using libopenjpeg did initialise and encode the date in the same thread. Jean ___ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
Re: [FFmpeg-devel] [PATCH 2/3] lavc/libopenjpegenc: add layerrates parameter to allow different compression rates per layer
On Mon, Feb 02, 2015 at 01:18:35PM +0100, Jean First wrote: libopenjpegenc.c | 42 -- 1 file changed, 20 insertions(+), 22 deletions(-) 6bed682adf441fa060b9fef84df173cd758320ed 0001-lavc-libopenjpegenc-move-opj_create_compress-opj_cio.patch From fe0afff9c7b8c6f78dcad0720965c6f6a50e7813 Mon Sep 17 00:00:00 2001 From: Jean First jeanfi...@gmail.com Date: Mon, 2 Feb 2015 12:57:03 +0100 Subject: [PATCH] lavc/libopenjpegenc: move opj_create_compress, opj_cio_open and opj_set_event_mgr to libopenjpeg_encode_frame libopenjpegenc crashes with pointer being freed was not allocated when threading is enabled with: ffmpeg -i tests/vsynth1/01.pgm -vcodec libopenjpeg file.j2k cannot reproduce do i assume correctly that this is a bug in libopenjpeg and unlreated to this patch ? [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Dictatorship naturally arises out of democracy, and the most aggravated form of tyranny and slavery out of the most extreme liberty. -- Plato signature.asc Description: Digital signature ___ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
Re: [FFmpeg-devel] [PATCH 2/3] lavc/libopenjpegenc: add layerrates parameter to allow different compression rates per layer
On Mon, Feb 02, 2015 at 05:31:07PM +0100, Jean First wrote: On Mon Feb 02 2015 17:20:44 GMT+0100 (CET), Michael Niedermayer wrote: On Mon, Feb 02, 2015 at 01:18:35PM +0100, Jean First wrote: libopenjpegenc.c | 42 -- 1 file changed, 20 insertions(+), 22 deletions(-) 6bed682adf441fa060b9fef84df173cd758320ed 0001-lavc-libopenjpegenc-move-opj_create_compress-opj_cio.patch From fe0afff9c7b8c6f78dcad0720965c6f6a50e7813 Mon Sep 17 00:00:00 2001 From: Jean First jeanfi...@gmail.com Date: Mon, 2 Feb 2015 12:57:03 +0100 Subject: [PATCH] lavc/libopenjpegenc: move opj_create_compress, opj_cio_open and opj_set_event_mgr to libopenjpeg_encode_frame libopenjpegenc crashes with pointer being freed was not allocated when threading is enabled with: ffmpeg -i tests/vsynth1/01.pgm -vcodec libopenjpeg file.j2k cannot reproduce do i assume correctly that this is a bug in libopenjpeg and unlreated to this patch ? Yes, that's my guess. Any other implementation I saw using libopenjpeg did initialise and encode the date in the same thread. ok, patch applied with slightly updated commit message to clarify this also this should be reported to libopenjpeg if its intended to work or we should disable multiple threads or both Thanks [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB I do not agree with what you have to say, but I'll defend to the death your right to say it. -- Voltaire signature.asc Description: Digital signature ___ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
Re: [FFmpeg-devel] [PATCH 2/3] lavc/libopenjpegenc: add layerrates parameter to allow different compression rates per layer
On Wed, Jan 28, 2015 at 04:41:25PM +0100, Jean First wrote: syntax is: 20,10,2 this adds 3 layers, the first with a 20x, the second with 10x and a third with 2x compression. Layers define the progression by image quality within the code stream and, although not defined by the JPEG 2000 standard, in general codecs try to build layers in such a way that the image quality will increase monotonically with each layer. Signed-off-by: Jean First jeanfi...@gmail.com --- it remove the numlayers parameter, but i'm not sure it ever worked. Unfortunately the j2k_dump provided by openjpeg won't show the individual rates per layer - I also tried the files provided in http://samples.ffmpeg.org/jpeg2000/fdis_j2kp4files.zip and the rates shown are always 0.0 TODO: Write the libopenjpegenc documentation libavcodec/libopenjpegenc.c | 21 ++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/libavcodec/libopenjpegenc.c b/libavcodec/libopenjpegenc.c index bbf6190..b9a8bac 100644 --- a/libavcodec/libopenjpegenc.c +++ b/libavcodec/libopenjpegenc.c @@ -56,6 +56,8 @@ typedef struct { int disto_alloc; int fixed_alloc; int fixed_quality; +char *layerrates; +float tcp_rates[100]; /** User specified rate stored in case of cinema option */ /** ... } LibOpenJPEGContext; static void error_callback(const char *msg, void *data) @@ -224,8 +226,21 @@ static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx) ctx-enc_params.cp_disto_alloc = ctx-disto_alloc; ctx-enc_params.cp_fixed_alloc = ctx-fixed_alloc; ctx-enc_params.cp_fixed_quality = ctx-fixed_quality; -ctx-enc_params.tcp_numlayers = ctx-numlayers; -ctx-enc_params.tcp_rates[0] = FFMAX(avctx-compression_level, 0) * 2; +ctx-enc_params.tcp_numlayers = 0; + +char *s = ctx-layerrates; mixing delaration and statement +while (sscanf(s, %f, ctx-tcp_rates[ctx-enc_params.tcp_numlayers]) == 1) { +ctx-enc_params.tcp_numlayers++; +while (*s *s != ',') +s++; +if (!*s) +break; +s++; +} missing tcp_rates[] size check also auto also this crashes: ./ffmpeg -i tests/vsynth1/01.pgm -vcodec libopenjpeg file.j2k ==11538== Invalid free() / delete / delete[] / realloc() ==11538==at 0x4C2B5D9: free (vg_replace_malloc.c:446) ==11538==by 0xE09663: av_free (mem.c:232) ==11538==by 0xE09688: av_freep (mem.c:239) ==11538==by 0xE0F5C8: av_opt_free (opt.c:1441) ==11538==by 0xA8CCA6: avcodec_close (utils.c:2858) ==11538==by 0xC976E0: worker (frame_thread_encoder.c:113) ==11538==by 0xD1BFE99: start_thread (pthread_create.c:308) ==11538==by 0xD4C92EC: clone (clone.S:112) ==11538== Address 0x10ee24f0 is 0 bytes inside a block of size 5 free'd ==11538==at 0x4C2B5D9: free (vg_replace_malloc.c:446) ==11538==by 0xE09663: av_free (mem.c:232) ==11538==by 0xE09688: av_freep (mem.c:239) ==11538==by 0xE0F5C8: av_opt_free (opt.c:1441) ==11538==by 0xA8CCA6: avcodec_close (utils.c:2858) ==11538==by 0xC976E0: worker (frame_thread_encoder.c:113) ==11538==by 0xD1BFE99: start_thread (pthread_create.c:308) ==11538==by 0xD4C92EC: clone (clone.S:112) [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB I am the wisest man alive, for I know one thing, and that is that I know nothing. -- Socrates signature.asc Description: Digital signature ___ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel