* [FFmpeg-devel] [PATCH 1/5] avcodec/magicyuvenc: Avoid unnecessary av_frame_clone() @ 2022-02-22 23:10 Andreas Rheinhardt 2022-02-22 23:14 ` [FFmpeg-devel] [PATCH 2/5] avcodec/magicyuvenc: Add const where appropriate Andreas Rheinhardt ` (4 more replies) 0 siblings, 5 replies; 10+ messages in thread From: Andreas Rheinhardt @ 2022-02-22 23:10 UTC (permalink / raw) To: ffmpeg-devel; +Cc: Andreas Rheinhardt It is unnecessary and unchecked; the intention seems to be to ensure that the frame's data is writable, but it does not provide this. This will be fixed in a latter commit. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> --- libavcodec/magicyuvenc.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/libavcodec/magicyuvenc.c b/libavcodec/magicyuvenc.c index ab32d4cee3..4440ab3ce2 100644 --- a/libavcodec/magicyuvenc.c +++ b/libavcodec/magicyuvenc.c @@ -452,32 +452,30 @@ static int magy_encode_frame(AVCodecContext *avctx, AVPacket *pkt, } if (s->correlate) { + uint8_t *const data[4] = { frame->data[1], frame->data[0], + frame->data[2], frame->data[3] }; + const int linesize[4] = { frame->linesize[1], frame->linesize[0], + frame->linesize[2], frame->linesize[3] }; uint8_t *r, *g, *b; - AVFrame *p = av_frame_clone(frame); - g = p->data[0]; - b = p->data[1]; - r = p->data[2]; + g = frame->data[0]; + b = frame->data[1]; + r = frame->data[2]; for (i = 0; i < height; i++) { s->llvidencdsp.diff_bytes(b, b, g, width); s->llvidencdsp.diff_bytes(r, r, g, width); - g += p->linesize[0]; - b += p->linesize[1]; - r += p->linesize[2]; + g += frame->linesize[0]; + b += frame->linesize[1]; + r += frame->linesize[2]; } - FFSWAP(uint8_t*, p->data[0], p->data[1]); - FFSWAP(int, p->linesize[0], p->linesize[1]); - for (i = 0; i < s->planes; i++) { for (slice = 0; slice < s->nb_slices; slice++) { - s->predict(s, p->data[i], s->slices[i], p->linesize[i], - p->width, p->height); + s->predict(s, data[i], s->slices[i], linesize[i], + frame->width, frame->height); } } - - av_frame_free(&p); } else { for (i = 0; i < s->planes; i++) { for (slice = 0; slice < s->nb_slices; slice++) { -- 2.32.0 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe". ^ permalink raw reply [flat|nested] 10+ messages in thread
* [FFmpeg-devel] [PATCH 2/5] avcodec/magicyuvenc: Add const where appropriate 2022-02-22 23:10 [FFmpeg-devel] [PATCH 1/5] avcodec/magicyuvenc: Avoid unnecessary av_frame_clone() Andreas Rheinhardt @ 2022-02-22 23:14 ` Andreas Rheinhardt 2022-02-23 8:56 ` Paul B Mahol 2022-02-22 23:16 ` [FFmpeg-devel] [PATCH 3/5] avcodec/magicyuvenc: Don't modify input frame Andreas Rheinhardt ` (3 subsequent siblings) 4 siblings, 1 reply; 10+ messages in thread From: Andreas Rheinhardt @ 2022-02-22 23:14 UTC (permalink / raw) To: ffmpeg-devel; +Cc: Andreas Rheinhardt Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> --- libavcodec/magicyuvenc.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libavcodec/magicyuvenc.c b/libavcodec/magicyuvenc.c index 4440ab3ce2..ffb0be1d14 100644 --- a/libavcodec/magicyuvenc.c +++ b/libavcodec/magicyuvenc.c @@ -69,12 +69,12 @@ typedef struct MagicYUVContext { unsigned tables_size; HuffEntry he[4][256]; LLVidEncDSPContext llvidencdsp; - void (*predict)(struct MagicYUVContext *s, uint8_t *src, uint8_t *dst, + void (*predict)(struct MagicYUVContext *s, const uint8_t *src, uint8_t *dst, ptrdiff_t stride, int width, int height); } MagicYUVContext; static void left_predict(MagicYUVContext *s, - uint8_t *src, uint8_t *dst, ptrdiff_t stride, + const uint8_t *src, uint8_t *dst, ptrdiff_t stride, int width, int height) { uint8_t prev = 0; @@ -98,7 +98,7 @@ static void left_predict(MagicYUVContext *s, } static void gradient_predict(MagicYUVContext *s, - uint8_t *src, uint8_t *dst, ptrdiff_t stride, + const uint8_t *src, uint8_t *dst, ptrdiff_t stride, int width, int height) { int left = 0, top, lefttop; @@ -126,7 +126,7 @@ static void gradient_predict(MagicYUVContext *s, } static void median_predict(MagicYUVContext *s, - uint8_t *src, uint8_t *dst, ptrdiff_t stride, + const uint8_t *src, uint8_t *dst, ptrdiff_t stride, int width, int height) { int left = 0, lefttop; -- 2.32.0 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe". ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [FFmpeg-devel] [PATCH 2/5] avcodec/magicyuvenc: Add const where appropriate 2022-02-22 23:14 ` [FFmpeg-devel] [PATCH 2/5] avcodec/magicyuvenc: Add const where appropriate Andreas Rheinhardt @ 2022-02-23 8:56 ` Paul B Mahol 0 siblings, 0 replies; 10+ messages in thread From: Paul B Mahol @ 2022-02-23 8:56 UTC (permalink / raw) To: FFmpeg development discussions and patches; +Cc: Andreas Rheinhardt lgtm _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe". ^ permalink raw reply [flat|nested] 10+ messages in thread
* [FFmpeg-devel] [PATCH 3/5] avcodec/magicyuvenc: Don't modify input frame 2022-02-22 23:10 [FFmpeg-devel] [PATCH 1/5] avcodec/magicyuvenc: Avoid unnecessary av_frame_clone() Andreas Rheinhardt 2022-02-22 23:14 ` [FFmpeg-devel] [PATCH 2/5] avcodec/magicyuvenc: Add const where appropriate Andreas Rheinhardt @ 2022-02-22 23:16 ` Andreas Rheinhardt 2022-02-23 8:54 ` Paul B Mahol 2022-02-22 23:16 ` [FFmpeg-devel] [PATCH 4/5] avcodec/libopenjpegenc: Don't clone AVFrame unnecessarily Andreas Rheinhardt ` (2 subsequent siblings) 4 siblings, 1 reply; 10+ messages in thread From: Andreas Rheinhardt @ 2022-02-22 23:16 UTC (permalink / raw) To: ffmpeg-devel; +Cc: Andreas Rheinhardt It need not be writable at all. Instead, use temporary buffers for decorrelation. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> --- The prediction functions could be rewritten to allow src and dst to alias each other; yet LLVidEncDSPContext.diff_bytes has the requirement that dst has an alignment of 16. libavcodec/magicyuvenc.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/libavcodec/magicyuvenc.c b/libavcodec/magicyuvenc.c index ffb0be1d14..5a37a49d63 100644 --- a/libavcodec/magicyuvenc.c +++ b/libavcodec/magicyuvenc.c @@ -67,6 +67,7 @@ typedef struct MagicYUVContext { uint8_t *slices[4]; unsigned slice_pos[4]; unsigned tables_size; + uint8_t *decorrelate_buf[2]; HuffEntry he[4][256]; LLVidEncDSPContext llvidencdsp; void (*predict)(struct MagicYUVContext *s, const uint8_t *src, uint8_t *dst, @@ -190,6 +191,12 @@ static av_cold int magy_encode_init(AVCodecContext *avctx) s->format = 0x6b; break; } + if (s->correlate) { + s->decorrelate_buf[0] = av_calloc(2U * avctx->height, FFALIGN(avctx->width, 16)); + if (!s->decorrelate_buf[0]) + return AVERROR(ENOMEM); + s->decorrelate_buf[1] = s->decorrelate_buf[0] + avctx->height * FFALIGN(avctx->width, 16); + } ff_llvidencdsp_init(&s->llvidencdsp); @@ -452,22 +459,26 @@ static int magy_encode_frame(AVCodecContext *avctx, AVPacket *pkt, } if (s->correlate) { - uint8_t *const data[4] = { frame->data[1], frame->data[0], - frame->data[2], frame->data[3] }; - const int linesize[4] = { frame->linesize[1], frame->linesize[0], - frame->linesize[2], frame->linesize[3] }; - uint8_t *r, *g, *b; + uint8_t *r, *g, *b, *decorrelated[2] = { s->decorrelate_buf[0], + s->decorrelate_buf[1] }; + const int decorrelate_linesize = FFALIGN(width, 16); + const uint8_t *const data[4] = { decorrelated[0], frame->data[0], + decorrelated[1], frame->data[3] }; + const int linesize[4] = { decorrelate_linesize, frame->linesize[0], + decorrelate_linesize, frame->linesize[3] }; g = frame->data[0]; b = frame->data[1]; r = frame->data[2]; for (i = 0; i < height; i++) { - s->llvidencdsp.diff_bytes(b, b, g, width); - s->llvidencdsp.diff_bytes(r, r, g, width); + s->llvidencdsp.diff_bytes(decorrelated[0], b, g, width); + s->llvidencdsp.diff_bytes(decorrelated[1], r, g, width); g += frame->linesize[0]; b += frame->linesize[1]; r += frame->linesize[2]; + decorrelated[0] += decorrelate_linesize; + decorrelated[1] += decorrelate_linesize; } for (i = 0; i < s->planes; i++) { @@ -531,6 +542,7 @@ static av_cold int magy_encode_close(AVCodecContext *avctx) for (i = 0; i < s->planes; i++) av_freep(&s->slices[i]); + av_freep(&s->decorrelate_buf); return 0; } -- 2.32.0 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe". ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [FFmpeg-devel] [PATCH 3/5] avcodec/magicyuvenc: Don't modify input frame 2022-02-22 23:16 ` [FFmpeg-devel] [PATCH 3/5] avcodec/magicyuvenc: Don't modify input frame Andreas Rheinhardt @ 2022-02-23 8:54 ` Paul B Mahol 0 siblings, 0 replies; 10+ messages in thread From: Paul B Mahol @ 2022-02-23 8:54 UTC (permalink / raw) To: FFmpeg development discussions and patches; +Cc: Andreas Rheinhardt lgtm _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe". ^ permalink raw reply [flat|nested] 10+ messages in thread
* [FFmpeg-devel] [PATCH 4/5] avcodec/libopenjpegenc: Don't clone AVFrame unnecessarily 2022-02-22 23:10 [FFmpeg-devel] [PATCH 1/5] avcodec/magicyuvenc: Avoid unnecessary av_frame_clone() Andreas Rheinhardt 2022-02-22 23:14 ` [FFmpeg-devel] [PATCH 2/5] avcodec/magicyuvenc: Add const where appropriate Andreas Rheinhardt 2022-02-22 23:16 ` [FFmpeg-devel] [PATCH 3/5] avcodec/magicyuvenc: Don't modify input frame Andreas Rheinhardt @ 2022-02-22 23:16 ` Andreas Rheinhardt 2022-02-24 9:44 ` Andreas Rheinhardt 2022-02-22 23:16 ` [FFmpeg-devel] [PATCH 5/5] avcodec/magicyuvenc: Remove unused context variable Andreas Rheinhardt 2022-02-23 8:56 ` [FFmpeg-devel] [PATCH 1/5] avcodec/magicyuvenc: Avoid unnecessary av_frame_clone() Paul B Mahol 4 siblings, 1 reply; 10+ messages in thread From: Andreas Rheinhardt @ 2022-02-22 23:16 UTC (permalink / raw) To: ffmpeg-devel; +Cc: Andreas Rheinhardt Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> --- libavcodec/libopenjpegenc.c | 83 +++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 41 deletions(-) diff --git a/libavcodec/libopenjpegenc.c b/libavcodec/libopenjpegenc.c index 3e52bcd4e9..08b7b6a152 100644 --- a/libavcodec/libopenjpegenc.c +++ b/libavcodec/libopenjpegenc.c @@ -345,7 +345,8 @@ static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx) return 0; } -static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image) +static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const uint8_t *src[4], + const int linesize[4], opj_image_t *image) { int compno; int x; @@ -355,7 +356,7 @@ static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const AVFrame *frame, const int numcomps = image->numcomps; for (compno = 0; compno < numcomps; ++compno) { - if (image->comps[compno].w > frame->linesize[0] / numcomps) { + if (image->comps[compno].w > linesize[0] / numcomps) { av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n"); return 0; } @@ -364,9 +365,9 @@ static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const AVFrame *frame, for (compno = 0; compno < numcomps; ++compno) { for (y = 0; y < avctx->height; ++y) { image_line = image->comps[compno].data + y * image->comps[compno].w; - frame_index = y * frame->linesize[0] + compno; + frame_index = y * linesize[0] + compno; for (x = 0; x < avctx->width; ++x) { - image_line[x] = frame->data[0][frame_index]; + image_line[x] = src[0][frame_index]; frame_index += numcomps; } for (; x < image->comps[compno].w; ++x) { @@ -385,17 +386,18 @@ static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const AVFrame *frame, } // for XYZ 12 bit -static int libopenjpeg_copy_packed12(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image) +static int libopenjpeg_copy_packed12(AVCodecContext *avctx, const uint8_t *src[4], + const int linesize[4], opj_image_t *image) { int compno; int x, y; int *image_line; int frame_index; const int numcomps = image->numcomps; - uint16_t *frame_ptr = (uint16_t *)frame->data[0]; + const uint16_t *frame_ptr = (const uint16_t *)src[0]; for (compno = 0; compno < numcomps; ++compno) { - if (image->comps[compno].w > frame->linesize[0] / numcomps) { + if (image->comps[compno].w > linesize[0] / numcomps) { av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n"); return 0; } @@ -404,7 +406,7 @@ static int libopenjpeg_copy_packed12(AVCodecContext *avctx, const AVFrame *frame for (compno = 0; compno < numcomps; ++compno) { for (y = 0; y < avctx->height; ++y) { image_line = image->comps[compno].data + y * image->comps[compno].w; - frame_index = y * (frame->linesize[0] / 2) + compno; + frame_index = y * (linesize[0] / 2) + compno; for (x = 0; x < avctx->width; ++x) { image_line[x] = frame_ptr[frame_index] >> 4; frame_index += numcomps; @@ -424,7 +426,8 @@ static int libopenjpeg_copy_packed12(AVCodecContext *avctx, const AVFrame *frame return 1; } -static int libopenjpeg_copy_packed16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image) +static int libopenjpeg_copy_packed16(AVCodecContext *avctx, const uint8_t *src[4], + const int linesize[4], opj_image_t *image) { int compno; int x; @@ -432,10 +435,10 @@ static int libopenjpeg_copy_packed16(AVCodecContext *avctx, const AVFrame *frame int *image_line; int frame_index; const int numcomps = image->numcomps; - uint16_t *frame_ptr = (uint16_t*)frame->data[0]; + const uint16_t *frame_ptr = (const uint16_t*)src[0]; for (compno = 0; compno < numcomps; ++compno) { - if (image->comps[compno].w > frame->linesize[0] / numcomps) { + if (image->comps[compno].w > linesize[0] / numcomps) { av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n"); return 0; } @@ -444,7 +447,7 @@ static int libopenjpeg_copy_packed16(AVCodecContext *avctx, const AVFrame *frame for (compno = 0; compno < numcomps; ++compno) { for (y = 0; y < avctx->height; ++y) { image_line = image->comps[compno].data + y * image->comps[compno].w; - frame_index = y * (frame->linesize[0] / 2) + compno; + frame_index = y * (linesize[0] / 2) + compno; for (x = 0; x < avctx->width; ++x) { image_line[x] = frame_ptr[frame_index]; frame_index += numcomps; @@ -464,7 +467,8 @@ static int libopenjpeg_copy_packed16(AVCodecContext *avctx, const AVFrame *frame return 1; } -static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image) +static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, const uint8_t *src[4], + const int linesize[4], opj_image_t *image) { int compno; int x; @@ -476,7 +480,7 @@ static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, const AVFrame *fram const int numcomps = image->numcomps; for (compno = 0; compno < numcomps; ++compno) { - if (image->comps[compno].w > frame->linesize[compno]) { + if (image->comps[compno].w > linesize[compno]) { av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n"); return 0; } @@ -487,9 +491,9 @@ static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, const AVFrame *fram height = (avctx->height + image->comps[compno].dy - 1) / image->comps[compno].dy; for (y = 0; y < height; ++y) { image_line = image->comps[compno].data + y * image->comps[compno].w; - frame_index = y * frame->linesize[compno]; + frame_index = y * linesize[compno]; for (x = 0; x < width; ++x) - image_line[x] = frame->data[compno][frame_index++]; + image_line[x] = src[compno][frame_index++]; for (; x < image->comps[compno].w; ++x) { image_line[x] = image_line[x - 1]; } @@ -505,7 +509,8 @@ static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, const AVFrame *fram return 1; } -static int libopenjpeg_copy_unpacked16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image) +static int libopenjpeg_copy_unpacked16(AVCodecContext *avctx, const uint8_t *src[4], + const int linesize[4], opj_image_t *image) { int compno; int x; @@ -515,22 +520,21 @@ static int libopenjpeg_copy_unpacked16(AVCodecContext *avctx, const AVFrame *fra int *image_line; int frame_index; const int numcomps = image->numcomps; - uint16_t *frame_ptr; for (compno = 0; compno < numcomps; ++compno) { - if (image->comps[compno].w > frame->linesize[compno]) { + if (image->comps[compno].w > linesize[compno]) { av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n"); return 0; } } for (compno = 0; compno < numcomps; ++compno) { + const uint16_t *frame_ptr = (const uint16_t *)src[compno]; width = (avctx->width + image->comps[compno].dx - 1) / image->comps[compno].dx; height = (avctx->height + image->comps[compno].dy - 1) / image->comps[compno].dy; - frame_ptr = (uint16_t *)frame->data[compno]; for (y = 0; y < height; ++y) { image_line = image->comps[compno].data + y * image->comps[compno].w; - frame_index = y * (frame->linesize[compno] / 2); + frame_index = y * (linesize[compno] / 2); for (x = 0; x < width; ++x) image_line[x] = frame_ptr[frame_index++]; for (; x < image->comps[compno].w; ++x) { @@ -553,12 +557,15 @@ static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt, { LibOpenJPEGContext *ctx = avctx->priv_data; int ret; - AVFrame *gbrframe; int cpyresult = 0; PacketWriter writer = { 0 }; opj_codec_t *compress = NULL; opj_stream_t *stream = NULL; opj_image_t *image = mj2_create_image(avctx, &ctx->enc_params); + const uint8_t *data[4] = { frame->data[0], frame->data[1], + frame->data[2], frame->data[3] }; + int linesize[4] = { frame->linesize[0], frame->linesize[1], + frame->linesize[2], frame->linesize[3] }; if (!image) { av_log(avctx, AV_LOG_ERROR, "Error creating the mj2 image\n"); ret = AVERROR(EINVAL); @@ -569,15 +576,15 @@ static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt, case AV_PIX_FMT_RGB24: case AV_PIX_FMT_RGBA: case AV_PIX_FMT_YA8: - cpyresult = libopenjpeg_copy_packed8(avctx, frame, image); + cpyresult = libopenjpeg_copy_packed8(avctx, data, linesize, image); break; case AV_PIX_FMT_XYZ12: - cpyresult = libopenjpeg_copy_packed12(avctx, frame, image); + cpyresult = libopenjpeg_copy_packed12(avctx, data, linesize, image); break; case AV_PIX_FMT_RGB48: case AV_PIX_FMT_RGBA64: case AV_PIX_FMT_YA16: - cpyresult = libopenjpeg_copy_packed16(avctx, frame, image); + cpyresult = libopenjpeg_copy_packed16(avctx, data, linesize, image); break; case AV_PIX_FMT_GBR24P: case AV_PIX_FMT_GBRP9: @@ -585,23 +592,17 @@ static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt, case AV_PIX_FMT_GBRP12: case AV_PIX_FMT_GBRP14: case AV_PIX_FMT_GBRP16: - gbrframe = av_frame_clone(frame); - if (!gbrframe) { - ret = AVERROR(ENOMEM); - goto done; - } - gbrframe->data[0] = frame->data[2]; // swap to be rgb - gbrframe->data[1] = frame->data[0]; - gbrframe->data[2] = frame->data[1]; - gbrframe->linesize[0] = frame->linesize[2]; - gbrframe->linesize[1] = frame->linesize[0]; - gbrframe->linesize[2] = frame->linesize[1]; + data[0] = frame->data[2]; // swap to be rgb + data[1] = frame->data[0]; + data[2] = frame->data[1]; + linesize[0] = frame->linesize[2]; + linesize[1] = frame->linesize[0]; + linesize[2] = frame->linesize[1]; if (avctx->pix_fmt == AV_PIX_FMT_GBR24P) { - cpyresult = libopenjpeg_copy_unpacked8(avctx, gbrframe, image); + cpyresult = libopenjpeg_copy_unpacked8(avctx, data, linesize, image); } else { - cpyresult = libopenjpeg_copy_unpacked16(avctx, gbrframe, image); + cpyresult = libopenjpeg_copy_unpacked16(avctx, data, linesize, image); } - av_frame_free(&gbrframe); break; case AV_PIX_FMT_GRAY8: case AV_PIX_FMT_YUV410P: @@ -613,7 +614,7 @@ static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt, case AV_PIX_FMT_YUVA420P: case AV_PIX_FMT_YUVA422P: case AV_PIX_FMT_YUVA444P: - cpyresult = libopenjpeg_copy_unpacked8(avctx, frame, image); + cpyresult = libopenjpeg_copy_unpacked8(avctx, data, linesize, image); break; case AV_PIX_FMT_GRAY10: case AV_PIX_FMT_GRAY12: @@ -643,7 +644,7 @@ static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt, case AV_PIX_FMT_YUVA444P16: case AV_PIX_FMT_YUVA422P16: case AV_PIX_FMT_YUVA420P16: - cpyresult = libopenjpeg_copy_unpacked16(avctx, frame, image); + cpyresult = libopenjpeg_copy_unpacked16(avctx, data, linesize, image); break; default: av_log(avctx, AV_LOG_ERROR, -- 2.32.0 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe". ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [FFmpeg-devel] [PATCH 4/5] avcodec/libopenjpegenc: Don't clone AVFrame unnecessarily 2022-02-22 23:16 ` [FFmpeg-devel] [PATCH 4/5] avcodec/libopenjpegenc: Don't clone AVFrame unnecessarily Andreas Rheinhardt @ 2022-02-24 9:44 ` Andreas Rheinhardt 2022-02-24 14:30 ` Michael Bradshaw 0 siblings, 1 reply; 10+ messages in thread From: Andreas Rheinhardt @ 2022-02-24 9:44 UTC (permalink / raw) To: ffmpeg-devel Andreas Rheinhardt: > Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> > --- > libavcodec/libopenjpegenc.c | 83 +++++++++++++++++++------------------ > 1 file changed, 42 insertions(+), 41 deletions(-) > > diff --git a/libavcodec/libopenjpegenc.c b/libavcodec/libopenjpegenc.c > index 3e52bcd4e9..08b7b6a152 100644 > --- a/libavcodec/libopenjpegenc.c > +++ b/libavcodec/libopenjpegenc.c > @@ -345,7 +345,8 @@ static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx) > return 0; > } > > -static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image) > +static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const uint8_t *src[4], > + const int linesize[4], opj_image_t *image) > { > int compno; > int x; > @@ -355,7 +356,7 @@ static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const AVFrame *frame, > const int numcomps = image->numcomps; > > for (compno = 0; compno < numcomps; ++compno) { > - if (image->comps[compno].w > frame->linesize[0] / numcomps) { > + if (image->comps[compno].w > linesize[0] / numcomps) { > av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n"); > return 0; > } > @@ -364,9 +365,9 @@ static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const AVFrame *frame, > for (compno = 0; compno < numcomps; ++compno) { > for (y = 0; y < avctx->height; ++y) { > image_line = image->comps[compno].data + y * image->comps[compno].w; > - frame_index = y * frame->linesize[0] + compno; > + frame_index = y * linesize[0] + compno; > for (x = 0; x < avctx->width; ++x) { > - image_line[x] = frame->data[0][frame_index]; > + image_line[x] = src[0][frame_index]; > frame_index += numcomps; > } > for (; x < image->comps[compno].w; ++x) { > @@ -385,17 +386,18 @@ static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const AVFrame *frame, > } > > // for XYZ 12 bit > -static int libopenjpeg_copy_packed12(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image) > +static int libopenjpeg_copy_packed12(AVCodecContext *avctx, const uint8_t *src[4], > + const int linesize[4], opj_image_t *image) > { > int compno; > int x, y; > int *image_line; > int frame_index; > const int numcomps = image->numcomps; > - uint16_t *frame_ptr = (uint16_t *)frame->data[0]; > + const uint16_t *frame_ptr = (const uint16_t *)src[0]; > > for (compno = 0; compno < numcomps; ++compno) { > - if (image->comps[compno].w > frame->linesize[0] / numcomps) { > + if (image->comps[compno].w > linesize[0] / numcomps) { > av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n"); > return 0; > } > @@ -404,7 +406,7 @@ static int libopenjpeg_copy_packed12(AVCodecContext *avctx, const AVFrame *frame > for (compno = 0; compno < numcomps; ++compno) { > for (y = 0; y < avctx->height; ++y) { > image_line = image->comps[compno].data + y * image->comps[compno].w; > - frame_index = y * (frame->linesize[0] / 2) + compno; > + frame_index = y * (linesize[0] / 2) + compno; > for (x = 0; x < avctx->width; ++x) { > image_line[x] = frame_ptr[frame_index] >> 4; > frame_index += numcomps; > @@ -424,7 +426,8 @@ static int libopenjpeg_copy_packed12(AVCodecContext *avctx, const AVFrame *frame > return 1; > } > > -static int libopenjpeg_copy_packed16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image) > +static int libopenjpeg_copy_packed16(AVCodecContext *avctx, const uint8_t *src[4], > + const int linesize[4], opj_image_t *image) > { > int compno; > int x; > @@ -432,10 +435,10 @@ static int libopenjpeg_copy_packed16(AVCodecContext *avctx, const AVFrame *frame > int *image_line; > int frame_index; > const int numcomps = image->numcomps; > - uint16_t *frame_ptr = (uint16_t*)frame->data[0]; > + const uint16_t *frame_ptr = (const uint16_t*)src[0]; > > for (compno = 0; compno < numcomps; ++compno) { > - if (image->comps[compno].w > frame->linesize[0] / numcomps) { > + if (image->comps[compno].w > linesize[0] / numcomps) { > av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n"); > return 0; > } > @@ -444,7 +447,7 @@ static int libopenjpeg_copy_packed16(AVCodecContext *avctx, const AVFrame *frame > for (compno = 0; compno < numcomps; ++compno) { > for (y = 0; y < avctx->height; ++y) { > image_line = image->comps[compno].data + y * image->comps[compno].w; > - frame_index = y * (frame->linesize[0] / 2) + compno; > + frame_index = y * (linesize[0] / 2) + compno; > for (x = 0; x < avctx->width; ++x) { > image_line[x] = frame_ptr[frame_index]; > frame_index += numcomps; > @@ -464,7 +467,8 @@ static int libopenjpeg_copy_packed16(AVCodecContext *avctx, const AVFrame *frame > return 1; > } > > -static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image) > +static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, const uint8_t *src[4], > + const int linesize[4], opj_image_t *image) > { > int compno; > int x; > @@ -476,7 +480,7 @@ static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, const AVFrame *fram > const int numcomps = image->numcomps; > > for (compno = 0; compno < numcomps; ++compno) { > - if (image->comps[compno].w > frame->linesize[compno]) { > + if (image->comps[compno].w > linesize[compno]) { > av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n"); > return 0; > } > @@ -487,9 +491,9 @@ static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, const AVFrame *fram > height = (avctx->height + image->comps[compno].dy - 1) / image->comps[compno].dy; > for (y = 0; y < height; ++y) { > image_line = image->comps[compno].data + y * image->comps[compno].w; > - frame_index = y * frame->linesize[compno]; > + frame_index = y * linesize[compno]; > for (x = 0; x < width; ++x) > - image_line[x] = frame->data[compno][frame_index++]; > + image_line[x] = src[compno][frame_index++]; > for (; x < image->comps[compno].w; ++x) { > image_line[x] = image_line[x - 1]; > } > @@ -505,7 +509,8 @@ static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, const AVFrame *fram > return 1; > } > > -static int libopenjpeg_copy_unpacked16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image) > +static int libopenjpeg_copy_unpacked16(AVCodecContext *avctx, const uint8_t *src[4], > + const int linesize[4], opj_image_t *image) > { > int compno; > int x; > @@ -515,22 +520,21 @@ static int libopenjpeg_copy_unpacked16(AVCodecContext *avctx, const AVFrame *fra > int *image_line; > int frame_index; > const int numcomps = image->numcomps; > - uint16_t *frame_ptr; > > for (compno = 0; compno < numcomps; ++compno) { > - if (image->comps[compno].w > frame->linesize[compno]) { > + if (image->comps[compno].w > linesize[compno]) { > av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n"); > return 0; > } > } > > for (compno = 0; compno < numcomps; ++compno) { > + const uint16_t *frame_ptr = (const uint16_t *)src[compno]; > width = (avctx->width + image->comps[compno].dx - 1) / image->comps[compno].dx; > height = (avctx->height + image->comps[compno].dy - 1) / image->comps[compno].dy; > - frame_ptr = (uint16_t *)frame->data[compno]; > for (y = 0; y < height; ++y) { > image_line = image->comps[compno].data + y * image->comps[compno].w; > - frame_index = y * (frame->linesize[compno] / 2); > + frame_index = y * (linesize[compno] / 2); > for (x = 0; x < width; ++x) > image_line[x] = frame_ptr[frame_index++]; > for (; x < image->comps[compno].w; ++x) { > @@ -553,12 +557,15 @@ static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt, > { > LibOpenJPEGContext *ctx = avctx->priv_data; > int ret; > - AVFrame *gbrframe; > int cpyresult = 0; > PacketWriter writer = { 0 }; > opj_codec_t *compress = NULL; > opj_stream_t *stream = NULL; > opj_image_t *image = mj2_create_image(avctx, &ctx->enc_params); > + const uint8_t *data[4] = { frame->data[0], frame->data[1], > + frame->data[2], frame->data[3] }; > + int linesize[4] = { frame->linesize[0], frame->linesize[1], > + frame->linesize[2], frame->linesize[3] }; > if (!image) { > av_log(avctx, AV_LOG_ERROR, "Error creating the mj2 image\n"); > ret = AVERROR(EINVAL); > @@ -569,15 +576,15 @@ static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt, > case AV_PIX_FMT_RGB24: > case AV_PIX_FMT_RGBA: > case AV_PIX_FMT_YA8: > - cpyresult = libopenjpeg_copy_packed8(avctx, frame, image); > + cpyresult = libopenjpeg_copy_packed8(avctx, data, linesize, image); > break; > case AV_PIX_FMT_XYZ12: > - cpyresult = libopenjpeg_copy_packed12(avctx, frame, image); > + cpyresult = libopenjpeg_copy_packed12(avctx, data, linesize, image); > break; > case AV_PIX_FMT_RGB48: > case AV_PIX_FMT_RGBA64: > case AV_PIX_FMT_YA16: > - cpyresult = libopenjpeg_copy_packed16(avctx, frame, image); > + cpyresult = libopenjpeg_copy_packed16(avctx, data, linesize, image); > break; > case AV_PIX_FMT_GBR24P: > case AV_PIX_FMT_GBRP9: > @@ -585,23 +592,17 @@ static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt, > case AV_PIX_FMT_GBRP12: > case AV_PIX_FMT_GBRP14: > case AV_PIX_FMT_GBRP16: > - gbrframe = av_frame_clone(frame); > - if (!gbrframe) { > - ret = AVERROR(ENOMEM); > - goto done; > - } > - gbrframe->data[0] = frame->data[2]; // swap to be rgb > - gbrframe->data[1] = frame->data[0]; > - gbrframe->data[2] = frame->data[1]; > - gbrframe->linesize[0] = frame->linesize[2]; > - gbrframe->linesize[1] = frame->linesize[0]; > - gbrframe->linesize[2] = frame->linesize[1]; > + data[0] = frame->data[2]; // swap to be rgb > + data[1] = frame->data[0]; > + data[2] = frame->data[1]; > + linesize[0] = frame->linesize[2]; > + linesize[1] = frame->linesize[0]; > + linesize[2] = frame->linesize[1]; > if (avctx->pix_fmt == AV_PIX_FMT_GBR24P) { > - cpyresult = libopenjpeg_copy_unpacked8(avctx, gbrframe, image); > + cpyresult = libopenjpeg_copy_unpacked8(avctx, data, linesize, image); > } else { > - cpyresult = libopenjpeg_copy_unpacked16(avctx, gbrframe, image); > + cpyresult = libopenjpeg_copy_unpacked16(avctx, data, linesize, image); > } > - av_frame_free(&gbrframe); > break; > case AV_PIX_FMT_GRAY8: > case AV_PIX_FMT_YUV410P: > @@ -613,7 +614,7 @@ static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt, > case AV_PIX_FMT_YUVA420P: > case AV_PIX_FMT_YUVA422P: > case AV_PIX_FMT_YUVA444P: > - cpyresult = libopenjpeg_copy_unpacked8(avctx, frame, image); > + cpyresult = libopenjpeg_copy_unpacked8(avctx, data, linesize, image); > break; > case AV_PIX_FMT_GRAY10: > case AV_PIX_FMT_GRAY12: > @@ -643,7 +644,7 @@ static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt, > case AV_PIX_FMT_YUVA444P16: > case AV_PIX_FMT_YUVA422P16: > case AV_PIX_FMT_YUVA420P16: > - cpyresult = libopenjpeg_copy_unpacked16(avctx, frame, image); > + cpyresult = libopenjpeg_copy_unpacked16(avctx, data, linesize, image); > break; > default: > av_log(avctx, AV_LOG_ERROR, Will apply this tomorrow unless there are objections. - Andreas _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe". ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [FFmpeg-devel] [PATCH 4/5] avcodec/libopenjpegenc: Don't clone AVFrame unnecessarily 2022-02-24 9:44 ` Andreas Rheinhardt @ 2022-02-24 14:30 ` Michael Bradshaw 0 siblings, 0 replies; 10+ messages in thread From: Michael Bradshaw @ 2022-02-24 14:30 UTC (permalink / raw) To: FFmpeg development discussions and patches On Thu, Feb 24, 2022 at 2:44 AM Andreas Rheinhardt < andreas.rheinhardt@outlook.com> wrote: > Will apply this tomorrow unless there are objections. > No objections from me. Thanks for the improvement! _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe". ^ permalink raw reply [flat|nested] 10+ messages in thread
* [FFmpeg-devel] [PATCH 5/5] avcodec/magicyuvenc: Remove unused context variable 2022-02-22 23:10 [FFmpeg-devel] [PATCH 1/5] avcodec/magicyuvenc: Avoid unnecessary av_frame_clone() Andreas Rheinhardt ` (2 preceding siblings ...) 2022-02-22 23:16 ` [FFmpeg-devel] [PATCH 4/5] avcodec/libopenjpegenc: Don't clone AVFrame unnecessarily Andreas Rheinhardt @ 2022-02-22 23:16 ` Andreas Rheinhardt 2022-02-23 8:56 ` [FFmpeg-devel] [PATCH 1/5] avcodec/magicyuvenc: Avoid unnecessary av_frame_clone() Paul B Mahol 4 siblings, 0 replies; 10+ messages in thread From: Andreas Rheinhardt @ 2022-02-22 23:16 UTC (permalink / raw) To: ffmpeg-devel; +Cc: Andreas Rheinhardt Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> --- libavcodec/magicyuvenc.c | 1 - 1 file changed, 1 deletion(-) diff --git a/libavcodec/magicyuvenc.c b/libavcodec/magicyuvenc.c index 5a37a49d63..7fb7578327 100644 --- a/libavcodec/magicyuvenc.c +++ b/libavcodec/magicyuvenc.c @@ -58,7 +58,6 @@ typedef struct MagicYUVContext { PutBitContext pb; int planes; uint8_t format; - AVFrame *p; int slice_height; int nb_slices; int correlate; -- 2.32.0 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe". ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [FFmpeg-devel] [PATCH 1/5] avcodec/magicyuvenc: Avoid unnecessary av_frame_clone() 2022-02-22 23:10 [FFmpeg-devel] [PATCH 1/5] avcodec/magicyuvenc: Avoid unnecessary av_frame_clone() Andreas Rheinhardt ` (3 preceding siblings ...) 2022-02-22 23:16 ` [FFmpeg-devel] [PATCH 5/5] avcodec/magicyuvenc: Remove unused context variable Andreas Rheinhardt @ 2022-02-23 8:56 ` Paul B Mahol 4 siblings, 0 replies; 10+ messages in thread From: Paul B Mahol @ 2022-02-23 8:56 UTC (permalink / raw) To: FFmpeg development discussions and patches; +Cc: Andreas Rheinhardt lgtm _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe". ^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2022-02-24 14:30 UTC | newest] Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2022-02-22 23:10 [FFmpeg-devel] [PATCH 1/5] avcodec/magicyuvenc: Avoid unnecessary av_frame_clone() Andreas Rheinhardt 2022-02-22 23:14 ` [FFmpeg-devel] [PATCH 2/5] avcodec/magicyuvenc: Add const where appropriate Andreas Rheinhardt 2022-02-23 8:56 ` Paul B Mahol 2022-02-22 23:16 ` [FFmpeg-devel] [PATCH 3/5] avcodec/magicyuvenc: Don't modify input frame Andreas Rheinhardt 2022-02-23 8:54 ` Paul B Mahol 2022-02-22 23:16 ` [FFmpeg-devel] [PATCH 4/5] avcodec/libopenjpegenc: Don't clone AVFrame unnecessarily Andreas Rheinhardt 2022-02-24 9:44 ` Andreas Rheinhardt 2022-02-24 14:30 ` Michael Bradshaw 2022-02-22 23:16 ` [FFmpeg-devel] [PATCH 5/5] avcodec/magicyuvenc: Remove unused context variable Andreas Rheinhardt 2022-02-23 8:56 ` [FFmpeg-devel] [PATCH 1/5] avcodec/magicyuvenc: Avoid unnecessary av_frame_clone() Paul B Mahol
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel This inbox may be cloned and mirrored by anyone: git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \ ffmpegdev@gitmailbox.com public-inbox-index ffmpegdev Example config snippet for mirrors. AGPL code for this site: git clone https://public-inbox.org/public-inbox.git