From: Michael Niedermayer <michael@niedermayer.cc> To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org> Subject: [FFmpeg-devel] [PATCH] avcodec/ffv1: Only allocate fltmap* and bitmap when needed Date: Wed, 2 Apr 2025 16:49:20 +0200 Message-ID: <20250402144920.1729875-1-michael@niedermayer.cc> (raw) This reduces memory requirements Signed-off-by: Michael Niedermayer <michael@niedermayer.cc> --- libavcodec/ffv1.c | 4 ++++ libavcodec/ffv1.h | 9 ++++----- libavcodec/ffv1dec.c | 18 +++++++++++++++++- libavcodec/ffv1enc.c | 23 +++++++++++++++++------ libavcodec/ffv1enc_template.c | 3 ++- 5 files changed, 44 insertions(+), 13 deletions(-) diff --git a/libavcodec/ffv1.c b/libavcodec/ffv1.c index 54c2e7a8c63..cff16c5cc76 100644 --- a/libavcodec/ffv1.c +++ b/libavcodec/ffv1.c @@ -233,6 +233,10 @@ av_cold void ff_ffv1_close(FFV1Context *s) av_freep(&sc->sample_buffer); av_freep(&sc->sample_buffer32); + for(int p = 0; p < 4 ; p++) { + av_freep(&sc->fltmap[p]); + av_freep(&sc->fltmap32[p]); + } av_refstruct_unref(&sc->plane); } diff --git a/libavcodec/ffv1.h b/libavcodec/ffv1.h index 9e71e0709d1..24cbb694d41 100644 --- a/libavcodec/ffv1.h +++ b/libavcodec/ffv1.h @@ -106,11 +106,10 @@ typedef struct FFV1SliceContext { uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2]; }; }; - union { - uint16_t bitmap [4][65536]; //float encode - uint16_t fltmap [4][65536]; //halffloat encode & decode - uint32_t fltmap32[4][65536]; //float decode - }; + + uint16_t *bitmap [4]; //float encode + uint16_t *fltmap [4]; //halffloat encode & decode + uint32_t *fltmap32[4]; //float decode struct Unit { uint32_t val; //this is unneeded if you accept a dereference on each access uint16_t ndx; diff --git a/libavcodec/ffv1dec.c b/libavcodec/ffv1dec.c index 3395f514f42..ff7935ed722 100644 --- a/libavcodec/ffv1dec.c +++ b/libavcodec/ffv1dec.c @@ -326,7 +326,7 @@ static int decode_remap(FFV1Context *f, FFV1SliceContext *sc) } if (i - 1 >= end) break; - if (j >= FF_ARRAY_ELEMS(sc->fltmap[p])) + if (j >= 65536 /*FF_ARRAY_ELEMS(sc->fltmap[p])*/) return AVERROR_INVALIDDATA; if (end <= 0xFFFF) { sc->fltmap [p][j++] = i ^ ((i& 0x8000) ? 0 : flip); @@ -387,6 +387,22 @@ static int decode_slice(AVCodecContext *c, void *arg) y = sc->slice_y; if (sc->remap) { + for(int p = 0; p < 1 + 2*f->chroma_planes + f->transparency ; p++) { + if (f->avctx->bits_per_raw_sample == 32) { + if (!sc->fltmap32[p]) { + sc->fltmap32[p] = av_malloc_array(65536, sizeof(*sc->fltmap32[p])); + if (!sc->fltmap32[p]) + return AVERROR(ENOMEM); + } + } else { + if (!sc->fltmap[p]) { + sc->fltmap[p] = av_malloc_array(65536, sizeof(*sc->fltmap[p])); + if (!sc->fltmap[p]) + return AVERROR(ENOMEM); + } + } + } + ret = decode_remap(f, sc); if (ret < 0) return ret; diff --git a/libavcodec/ffv1enc.c b/libavcodec/ffv1enc.c index 852eb0c02d2..b704cec9f3a 100644 --- a/libavcodec/ffv1enc.c +++ b/libavcodec/ffv1enc.c @@ -326,7 +326,7 @@ static void load_plane(FFV1Context *f, FFV1SliceContext *sc, { int x, y; - memset(sc->fltmap[remap_index], 0, sizeof(sc->fltmap[remap_index])); + memset(sc->fltmap[remap_index], 0, 65536 * sizeof(*sc->fltmap[remap_index])); for (y = 0; y < h; y++) { if (f->bits_per_raw_sample <= 8) { @@ -1012,11 +1012,20 @@ static av_cold int encode_init_internal(AVCodecContext *avctx) p->context_count = s->context_count[p->quant_table_index]; } av_assert0(s->remap_mode >= 0); - if (s->remap_mode && s->bits_per_raw_sample == 32) { + if (s->remap_mode) { for (int p = 0; p < 1 + 2*s->chroma_planes + s->transparency ; p++) { - sc->unit[p] = av_malloc_array(sc->slice_width, sc->slice_height * sizeof(**sc->unit)); - if (!sc->unit[p]) - return AVERROR(ENOMEM); + if (s->bits_per_raw_sample == 32) { + sc->unit[p] = av_malloc_array(sc->slice_width, sc->slice_height * sizeof(**sc->unit)); + if (!sc->unit[p]) + return AVERROR(ENOMEM); + sc->bitmap[p] = av_malloc_array(sc->slice_width * sc->slice_height, sizeof(*sc->bitmap[p])); + if (!sc->bitmap[p]) + return AVERROR(ENOMEM); + } else { + sc->fltmap[p] = av_malloc_array(65536, sizeof(*sc->fltmap[p])); + if (!sc->fltmap[p]) + return AVERROR(ENOMEM); + } } } @@ -1821,8 +1830,10 @@ static av_cold int encode_close(AVCodecContext *avctx) for (int j = 0; j < s->max_slice_count; j++) { FFV1SliceContext *sc = &s->slices[j]; - for(int p = 0; p<4; p++) + for(int p = 0; p<4; p++) { av_freep(&sc->unit[p]); + av_freep(&sc->bitmap[p]); + } } av_freep(&avctx->stats_out); diff --git a/libavcodec/ffv1enc_template.c b/libavcodec/ffv1enc_template.c index 29dcedd1b6e..85d09da99a5 100644 --- a/libavcodec/ffv1enc_template.c +++ b/libavcodec/ffv1enc_template.c @@ -134,7 +134,8 @@ static void RENAME(load_rgb_frame)(FFV1Context *f, FFV1SliceContext *sc, int x, y; int transparency = f->transparency; - memset(sc->fltmap, 0, sizeof(sc->fltmap)); + for (int p = 0; p<3 + transparency; p++) + memset(sc->fltmap[p], 0, 65536 * sizeof(**sc->fltmap)); for (y = 0; y < h; y++) { for (x = 0; x < w; x++) { -- 2.49.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 reply other threads:[~2025-04-02 15:19 UTC|newest] Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top 2025-04-02 14:49 Michael Niedermayer [this message] 2025-04-04 17:50 ` Michael Niedermayer
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=20250402144920.1729875-1-michael@niedermayer.cc \ --to=michael@niedermayer.cc \ --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