From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
To: ffmpeg-devel@ffmpeg.org
Cc: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Subject: [FFmpeg-devel] [PATCH 4/5] avcodec/libopenjpegenc: Don't clone AVFrame unnecessarily
Date: Wed, 23 Feb 2022 00:16:40 +0100
Message-ID: <AM7PR03MB6660946335FC54152AACE5778F3B9@AM7PR03MB6660.eurprd03.prod.outlook.com> (raw)
In-Reply-To: <AM7PR03MB66602D3222786A979F95023B8F3B9@AM7PR03MB6660.eurprd03.prod.outlook.com>
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".
next prev parent reply other threads:[~2022-02-22 23:17 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
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 ` Andreas Rheinhardt [this message]
2022-02-24 9:44 ` [FFmpeg-devel] [PATCH 4/5] avcodec/libopenjpegenc: Don't clone AVFrame unnecessarily 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=AM7PR03MB6660946335FC54152AACE5778F3B9@AM7PR03MB6660.eurprd03.prod.outlook.com \
--to=andreas.rheinhardt@outlook.com \
--cc=ffmpeg-devel@ffmpeg.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
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