From: Paul B Mahol <onemda@gmail.com> To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org> Subject: [FFmpeg-devel] [PATCH] avcodec/8bps: switch to planar RGB formats Date: Sun, 10 Sep 2023 22:48:05 +0200 Message-ID: <CAPYw7P41mHRcm5OdTg3_JrtCoYJbTAqXb2D_YaQGvvqqiz05zQ@mail.gmail.com> (raw) [-- Attachment #1: Type: text/plain, Size: 10 bytes --] Attached. [-- Attachment #2: 0002-avcodec-8bps-always-decode-to-planar-formats-directl.patch --] [-- Type: text/x-patch, Size: 5550 bytes --] From 8ee65119916a849a37b39b3a8c12ca8af3b456c5 Mon Sep 17 00:00:00 2001 From: Paul B Mahol <onemda@gmail.com> Date: Sun, 10 Sep 2023 22:42:11 +0200 Subject: [PATCH 2/2] avcodec/8bps: always decode to planar formats directly Signed-off-by: Paul B Mahol <onemda@gmail.com> --- libavcodec/8bps.c | 60 ++++++++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 35 deletions(-) diff --git a/libavcodec/8bps.c b/libavcodec/8bps.c index 15c236f114..0becaa9320 100644 --- a/libavcodec/8bps.c +++ b/libavcodec/8bps.c @@ -26,22 +26,18 @@ * http://www.pcisys.net/~melanson/codecs/ * * Supports: PAL8 (RGB 8bpp, paletted) - * : BGR24 (RGB 24bpp) (can also output it as RGB32) - * : RGB32 (RGB 32bpp, 4th plane is alpha) + * : GBRP (RGB 24bpp) + * : GBRAP (RGB 32bpp, 4th plane is alpha) */ #include <string.h> -#include "libavutil/bswap.h" +#include "libavutil/intreadwrite.h" #include "libavutil/internal.h" #include "avcodec.h" #include "codec_internal.h" #include "decode.h" - -static const enum AVPixelFormat pixfmt_rgb24[] = { - AV_PIX_FMT_BGR24, AV_PIX_FMT_0RGB32, AV_PIX_FMT_NONE }; - typedef struct EightBpsContext { AVCodecContext *avctx; @@ -61,9 +57,8 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *frame, unsigned int dlen, p, row; const uint8_t *lp, *dp, *ep; uint8_t count; - unsigned int px_inc; - unsigned int planes = c->planes; - uint8_t *planemap = c->planemap; + const uint8_t *planemap = c->planemap; + unsigned int planes = c->planes; int ret; if (buf_size < planes * height * 2) @@ -77,19 +72,18 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *frame, /* Set data pointer after line lengths */ dp = encoded + planes * (height << 1); - px_inc = planes + (avctx->pix_fmt == AV_PIX_FMT_0RGB32); - for (p = 0; p < planes; p++) { + const int pi = planemap[p]; /* Lines length pointer for this plane */ lp = encoded + p * (height << 1); /* Decode a plane */ for (row = 0; row < height; row++) { - pixptr = frame->data[0] + row * frame->linesize[0] + planemap[p]; - pixptr_end = pixptr + frame->linesize[0]; + pixptr = frame->data[pi] + row * frame->linesize[pi]; + pixptr_end = pixptr + frame->linesize[pi]; if (ep - lp < row * 2 + 2) return AVERROR_INVALIDDATA; - dlen = av_be2ne16(*(const uint16_t *)(lp + row * 2)); + dlen = AV_RB16(lp + row * 2); /* Decode a row of this plane */ while (dlen > 0) { if (ep - dp <= 1) @@ -97,22 +91,19 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *frame, if ((count = *dp++) <= 127) { count++; dlen -= count + 1; - if (pixptr_end - pixptr < count * px_inc) + if (pixptr_end - pixptr < count) break; if (ep - dp < count) return AVERROR_INVALIDDATA; - while (count--) { - *pixptr = *dp++; - pixptr += px_inc; - } + memcpy(pixptr, dp, count); + pixptr += count; + dp += count; } else { count = 257 - count; - if (pixptr_end - pixptr < count * px_inc) + if (pixptr_end - pixptr < count) break; - while (count--) { - *pixptr = *dp; - pixptr += px_inc; - } + memset(pixptr, dp[0], count); + pixptr += count; dp++; dlen -= 2; } @@ -150,16 +141,15 @@ static av_cold int decode_init(AVCodecContext *avctx) c->planemap[0] = 0; // 1st plane is palette indexes break; case 24: - avctx->pix_fmt = ff_get_format(avctx, pixfmt_rgb24); + avctx->pix_fmt = AV_PIX_FMT_GBRP; c->planes = 3; c->planemap[0] = 2; // 1st plane is red - c->planemap[1] = 1; // 2nd plane is green - c->planemap[2] = 0; // 3rd plane is blue + c->planemap[1] = 0; // 2nd plane is green + c->planemap[2] = 1; // 3rd plane is blue break; case 32: - avctx->pix_fmt = AV_PIX_FMT_RGB32; + avctx->pix_fmt = AV_PIX_FMT_GBRAP; c->planes = 4; - /* handle planemap setup later for decoding rgb24 data as rbg32 */ break; default: av_log(avctx, AV_LOG_ERROR, "Error: Unsupported color depth: %u.\n", @@ -167,11 +157,11 @@ static av_cold int decode_init(AVCodecContext *avctx) return AVERROR_INVALIDDATA; } - if (avctx->pix_fmt == AV_PIX_FMT_RGB32) { - c->planemap[0] = HAVE_BIGENDIAN ? 1 : 2; // 1st plane is red - c->planemap[1] = HAVE_BIGENDIAN ? 2 : 1; // 2nd plane is green - c->planemap[2] = HAVE_BIGENDIAN ? 3 : 0; // 3rd plane is blue - c->planemap[3] = HAVE_BIGENDIAN ? 0 : 3; // 4th plane is alpha + if (avctx->pix_fmt == AV_PIX_FMT_GBRAP) { + c->planemap[0] = 2; // 1st plane is red + c->planemap[1] = 0; // 2nd plane is green + c->planemap[2] = 1; // 3rd plane is blue + c->planemap[3] = 3; // 4th plane is alpha } return 0; } -- 2.39.1 [-- Attachment #3: 0001-avcodec-8bps-use-uint8-uint16-where-possible.patch --] [-- Type: text/x-patch, Size: 2089 bytes --] From 3b7f86ff244692b77d5f9f6e48f70138b0405181 Mon Sep 17 00:00:00 2001 From: Paul B Mahol <onemda@gmail.com> Date: Sun, 10 Sep 2023 22:19:19 +0200 Subject: [PATCH 1/2] avcodec/8bps: use uint8/uint16 where possible Signed-off-by: Paul B Mahol <onemda@gmail.com> --- libavcodec/8bps.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/libavcodec/8bps.c b/libavcodec/8bps.c index af98f62fad..15c236f114 100644 --- a/libavcodec/8bps.c +++ b/libavcodec/8bps.c @@ -45,8 +45,8 @@ static const enum AVPixelFormat pixfmt_rgb24[] = { typedef struct EightBpsContext { AVCodecContext *avctx; - unsigned char planes; - unsigned char planemap[4]; + uint8_t planes; + uint8_t planemap[4]; } EightBpsContext; static int decode_frame(AVCodecContext *avctx, AVFrame *frame, @@ -55,15 +55,15 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *frame, const uint8_t *buf = avpkt->data; int buf_size = avpkt->size; EightBpsContext * const c = avctx->priv_data; - const unsigned char *encoded = buf; - unsigned char *pixptr, *pixptr_end; + const uint8_t *encoded = buf; + uint8_t *pixptr, *pixptr_end; unsigned int height = avctx->height; // Real image height unsigned int dlen, p, row; - const unsigned char *lp, *dp, *ep; - unsigned char count; + const uint8_t *lp, *dp, *ep; + uint8_t count; unsigned int px_inc; unsigned int planes = c->planes; - unsigned char *planemap = c->planemap; + uint8_t *planemap = c->planemap; int ret; if (buf_size < planes * height * 2) @@ -89,7 +89,7 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *frame, pixptr_end = pixptr + frame->linesize[0]; if (ep - lp < row * 2 + 2) return AVERROR_INVALIDDATA; - dlen = av_be2ne16(*(const unsigned short *)(lp + row * 2)); + dlen = av_be2ne16(*(const uint16_t *)(lp + row * 2)); /* Decode a row of this plane */ while (dlen > 0) { if (ep - dp <= 1) -- 2.39.1 [-- Attachment #4: Type: text/plain, Size: 251 bytes --] _______________________________________________ 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 reply other threads:[~2023-09-10 20:40 UTC|newest] Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top 2023-09-10 20:48 Paul B Mahol [this message] 2023-09-11 14:01 ` 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=CAPYw7P41mHRcm5OdTg3_JrtCoYJbTAqXb2D_YaQGvvqqiz05zQ@mail.gmail.com \ --to=onemda@gmail.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