From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> To: ffmpeg-devel@ffmpeg.org Cc: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> Subject: [FFmpeg-devel] [PATCH v2 8/9] avcodec/huffyuvdec: Use bytestream API for byte-aligned reads Date: Thu, 4 Apr 2024 21:33:08 +0200 Message-ID: <GV1P250MB073733E38C4C278E54B3DBE98F3C2@GV1P250MB0737.EURP250.PROD.OUTLOOK.COM> (raw) In-Reply-To: <GV1P250MB07373EAE6095C91504AB56B98F3C2@GV1P250MB0737.EURP250.PROD.OUTLOOK.COM> This also allows to remove the padding from these buffers. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> --- libavcodec/huffyuvdec.c | 53 ++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/libavcodec/huffyuvdec.c b/libavcodec/huffyuvdec.c index 12ecfcb933..e35d55c8ad 100644 --- a/libavcodec/huffyuvdec.c +++ b/libavcodec/huffyuvdec.c @@ -36,6 +36,7 @@ #include "avcodec.h" #include "bswapdsp.h" +#include "bytestream.h" #include "codec_internal.h" #include "get_bits.h" #include "huffyuv.h" @@ -86,21 +87,17 @@ typedef struct HYuvDecContext { } HYuvDecContext; -#define classic_shift_luma_table_size 42 -static const unsigned char classic_shift_luma[classic_shift_luma_table_size + AV_INPUT_BUFFER_PADDING_SIZE] = { +static const uint8_t classic_shift_luma[] = { 34, 36, 35, 69, 135, 232, 9, 16, 10, 24, 11, 23, 12, 16, 13, 10, 14, 8, 15, 8, 16, 8, 17, 20, 16, 10, 207, 206, 205, 236, 11, 8, - 10, 21, 9, 23, 8, 8, 199, 70, 69, 68, 0, - 0,0,0,0,0,0,0,0, + 10, 21, 9, 23, 8, 8, 199, 70, 69, 68, }; -#define classic_shift_chroma_table_size 59 -static const unsigned char classic_shift_chroma[classic_shift_chroma_table_size + AV_INPUT_BUFFER_PADDING_SIZE] = { +static const uint8_t classic_shift_chroma[] = { 66, 36, 37, 38, 39, 40, 41, 75, 76, 77, 110, 239, 144, 81, 82, 83, 84, 85, 118, 183, 56, 57, 88, 89, 56, 89, 154, 57, 58, 57, 26, 141, 57, 56, 58, 57, 58, 57, 184, 119, 214, 245, 116, 83, 82, 49, 80, 79, - 78, 77, 44, 75, 41, 40, 39, 38, 37, 36, 34, 0, - 0,0,0,0,0,0,0,0, + 78, 77, 44, 75, 41, 40, 39, 38, 37, 36, 34, }; static const unsigned char classic_add_luma[256] = { @@ -141,23 +138,30 @@ static const unsigned char classic_add_chroma[256] = { 6, 12, 8, 10, 7, 9, 6, 4, 6, 2, 2, 3, 3, 3, 3, 2, }; -static int read_len_table(uint8_t *dst, GetBitContext *gb, int n) +static int read_len_table(uint8_t *dst, GetByteContext *gb, int n) { int i, val, repeat; for (i = 0; i < n;) { - repeat = get_bits(gb, 3); - val = get_bits(gb, 5); - if (repeat == 0) - repeat = get_bits(gb, 8); - if (i + repeat > n || get_bits_left(gb) < 0) { - av_log(NULL, AV_LOG_ERROR, "Error reading huffman table\n"); - return AVERROR_INVALIDDATA; + if (bytestream2_get_bytes_left(gb) <= 0) + goto error; + repeat = bytestream2_peek_byteu(gb) >> 5; + val = bytestream2_get_byteu(gb) & 0x1F; + if (repeat == 0) { + if (bytestream2_get_bytes_left(gb) <= 0) + goto error; + repeat = bytestream2_get_byteu(gb); } + if (i + repeat > n) + goto error; while (repeat--) dst[i++] = val; } return 0; + +error: + av_log(NULL, AV_LOG_ERROR, "Error reading huffman table\n"); + return AVERROR_INVALIDDATA; } static int generate_joint_tables(HYuvDecContext *s) @@ -253,12 +257,11 @@ out: static int read_huffman_tables(HYuvDecContext *s, const uint8_t *src, int length) { - GetBitContext gb; + GetByteContext gb; int i, ret; int count = 3; - if ((ret = init_get_bits(&gb, src, length * 8)) < 0) - return ret; + bytestream2_init(&gb, src, length); if (s->version > 2) count = 1 + s->alpha + 2*s->chroma; @@ -277,21 +280,21 @@ static int read_huffman_tables(HYuvDecContext *s, const uint8_t *src, int length if ((ret = generate_joint_tables(s)) < 0) return ret; - return (get_bits_count(&gb) + 7) / 8; + return bytestream2_tell(&gb); } static int read_old_huffman_tables(HYuvDecContext *s) { - GetBitContext gb; + GetByteContext gb; int i, ret; - init_get_bits(&gb, classic_shift_luma, - classic_shift_luma_table_size * 8); + bytestream2_init(&gb, classic_shift_luma, + sizeof(classic_shift_luma)); if ((ret = read_len_table(s->len[0], &gb, 256)) < 0) return ret; - init_get_bits(&gb, classic_shift_chroma, - classic_shift_chroma_table_size * 8); + bytestream2_init(&gb, classic_shift_chroma, + sizeof(classic_shift_chroma)); if ((ret = read_len_table(s->len[1], &gb, 256)) < 0) return ret; -- 2.40.1 _______________________________________________ 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:[~2024-04-04 19:33 UTC|newest] Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top 2024-04-04 4:59 [FFmpeg-devel] [PATCH 1/6] avcodec/huffyuvdec: Don't zero unnecessarily Andreas Rheinhardt 2024-04-04 5:02 ` [FFmpeg-devel] [PATCH 2/6] avcodec/huffyuv: Inline common alloc/free functions in their callers Andreas Rheinhardt 2024-04-04 5:02 ` [FFmpeg-devel] [PATCH 3/6] avcodec/huffyuv(dec|enc): Use union for temp/temp16 Andreas Rheinhardt 2024-04-04 5:02 ` [FFmpeg-devel] [PATCH 4/6] avcodec/huffyuv: Return proper error code Andreas Rheinhardt 2024-04-04 5:02 ` [FFmpeg-devel] [PATCH 5/6] avcodec/huffyuvenc: Avoid duplicate variables Andreas Rheinhardt 2024-04-04 5:02 ` [FFmpeg-devel] [PATCH 6/6] avcodec/huffyuvenc: Avoid code duplication Andreas Rheinhardt 2024-04-04 17:52 ` [FFmpeg-devel] [PATCH 7/7] avcodec/huffyuvenc: Deduplicate options Andreas Rheinhardt 2024-04-04 19:30 ` [FFmpeg-devel] [PATCH 8/9] avcodec/huffyuvdec: Use assert to check for things that can't fail Andreas Rheinhardt 2024-04-04 19:30 ` [FFmpeg-devel] [PATCH 9/9] avcodec/dv: Don't pretend initializing work chunks can fail Andreas Rheinhardt 2024-04-04 19:33 ` Andreas Rheinhardt 2024-04-04 19:33 ` Andreas Rheinhardt [this message] 2024-04-04 19:33 ` [FFmpeg-devel] [PATCH v2 9/9] avcodec/huffyuvdec: Use assert to check for things that can't fail Andreas Rheinhardt 2024-04-05 22:23 ` [FFmpeg-devel] [PATCH 1/6] avcodec/huffyuvdec: Don't zero unnecessarily Andreas Rheinhardt
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=GV1P250MB073733E38C4C278E54B3DBE98F3C2@GV1P250MB0737.EURP250.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