History

avcodec/ffv1: Only allocate fltmap* and bitmap when needed

This reduces memory requirements

Michael Niedermayer committed 1 year ago

Commit 06c00f91 parent b67cf795 files · +44 −13Browse files

Showing the changes to libavcodec (5 of 5 files). Show all

Unified · Split

5 files
  1. libavcodec/ffv1.c +4 −0
  2. libavcodec/ffv1.h +4 −5
  3. libavcodec/ffv1dec.c +17 −1
  4. libavcodec/ffv1enc.c +17 −6
  5. libavcodec/ffv1enc_template.c +2 −1
libavcodec/ffv1.c+4−0
@@ -233,6 +233,10 @@ av_cold void ff_ffv1_close(FFV1Context *s) · 232 unmodified lines
233233
234234 av_freep(&sc->sample_buffer);
235235 av_freep(&sc->sample_buffer32);
236+ for(int p = 0; p < 4 ; p++) {
237+ av_freep(&sc->fltmap[p]);
238+ av_freep(&sc->fltmap32[p]);
239+ }
236240
237241 av_refstruct_unref(&sc->plane);
238242 }
libavcodec/ffv1.h+4−5
@@ -106,11 +106,10 @@ typedef struct FFV1SliceContext { · 105 unmodified lines
106106 uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2];
107107 };
108108 };
109− union {
110− uint16_t bitmap [4][65536]; //float encode
111− uint16_t fltmap [4][65536]; //halffloat encode & decode
112− uint32_t fltmap32[4][65536]; //float decode
113− };
109+
110+ uint16_t *bitmap [4]; //float encode
111+ uint16_t *fltmap [4]; //halffloat encode & decode
112+ uint32_t *fltmap32[4]; //float decode
114113 struct Unit {
115114 uint32_t val; //this is unneeded if you accept a dereference on each access
116115 uint16_t ndx;
libavcodec/ffv1dec.c+17−1
@@ -326,7 +326,7 @@ static int decode_remap(FFV1Context *f, FFV1SliceContext *sc) · 325 unmodified lines
326326 }
327327 if (i - 1 >= end)
328328 break;
329− if (j >= FF_ARRAY_ELEMS(sc->fltmap[p]))
329+ if (j >= 65536 /*FF_ARRAY_ELEMS(sc->fltmap[p])*/)
330330 return AVERROR_INVALIDDATA;
331331 if (end <= 0xFFFF) {
332332 sc->fltmap [p][j++] = i ^ ((i& 0x8000) ? 0 : flip);
@@ -387,6 +387,22 @@ static int decode_slice(AVCodecContext *c, void *arg) · 54 unmodified lines
387387 y = sc->slice_y;
388388
389389 if (sc->remap) {
390+ for(int p = 0; p < 1 + 2*f->chroma_planes + f->transparency ; p++) {
391+ if (f->avctx->bits_per_raw_sample == 32) {
392+ if (!sc->fltmap32[p]) {
393+ sc->fltmap32[p] = av_malloc_array(65536, sizeof(*sc->fltmap32[p]));
394+ if (!sc->fltmap32[p])
395+ return AVERROR(ENOMEM);
396+ }
397+ } else {
398+ if (!sc->fltmap[p]) {
399+ sc->fltmap[p] = av_malloc_array(65536, sizeof(*sc->fltmap[p]));
400+ if (!sc->fltmap[p])
401+ return AVERROR(ENOMEM);
402+ }
403+ }
404+ }
405+
390406 ret = decode_remap(f, sc);
391407 if (ret < 0)
392408 return ret;
libavcodec/ffv1enc.c+17−6
@@ -326,7 +326,7 @@ static void load_plane(FFV1Context *f, FFV1SliceContext *sc, · 325 unmodified lines
326326 {
327327 int x, y;
328328
329− memset(sc->fltmap[remap_index], 0, sizeof(sc->fltmap[remap_index]));
329+ memset(sc->fltmap[remap_index], 0, 65536 * sizeof(*sc->fltmap[remap_index]));
330330
331331 for (y = 0; y < h; y++) {
332332 if (f->bits_per_raw_sample <= 8) {
@@ -1012,11 +1012,20 @@ static av_cold int encode_init_internal(AVCodecContext *avctx) · 679 unmodified lines
10121012 p->context_count = s->context_count[p->quant_table_index];
10131013 }
10141014 av_assert0(s->remap_mode >= 0);
1015− if (s->remap_mode && s->bits_per_raw_sample == 32) {
1015+ if (s->remap_mode) {
10161016 for (int p = 0; p < 1 + 2*s->chroma_planes + s->transparency ; p++) {
1017− sc->unit[p] = av_malloc_array(sc->slice_width, sc->slice_height * sizeof(**sc->unit));
1018− if (!sc->unit[p])
1019− return AVERROR(ENOMEM);
1017+ if (s->bits_per_raw_sample == 32) {
1018+ sc->unit[p] = av_malloc_array(sc->slice_width, sc->slice_height * sizeof(**sc->unit));
1019+ if (!sc->unit[p])
1020+ return AVERROR(ENOMEM);
1021+ sc->bitmap[p] = av_malloc_array(sc->slice_width * sc->slice_height, sizeof(*sc->bitmap[p]));
1022+ if (!sc->bitmap[p])
1023+ return AVERROR(ENOMEM);
1024+ } else {
1025+ sc->fltmap[p] = av_malloc_array(65536, sizeof(*sc->fltmap[p]));
1026+ if (!sc->fltmap[p])
1027+ return AVERROR(ENOMEM);
1028+ }
10201029 }
10211030 }
10221031
@@ -1821,8 +1830,10 @@ static av_cold int encode_close(AVCodecContext *avctx) · 798 unmodified lines
18211830 for (int j = 0; j < s->max_slice_count; j++) {
18221831 FFV1SliceContext *sc = &s->slices[j];
18231832
1824− for(int p = 0; p<4; p++)
1833+ for(int p = 0; p<4; p++) {
18251834 av_freep(&sc->unit[p]);
1835+ av_freep(&sc->bitmap[p]);
1836+ }
18261837 }
18271838
18281839 av_freep(&avctx->stats_out);
libavcodec/ffv1enc_template.c+2−1
@@ -134,7 +134,8 @@ static void RENAME(load_rgb_frame)(FFV1Context *f, FFV1SliceContext *sc, · 133 unmodified lines
134134 int x, y;
135135 int transparency = f->transparency;
136136
137− memset(sc->fltmap, 0, sizeof(sc->fltmap));
137+ for (int p = 0; p<3 + transparency; p++)
138+ memset(sc->fltmap[p], 0, 65536 * sizeof(**sc->fltmap));
138139
139140 for (y = 0; y < h; y++) {
140141 for (x = 0; x < w; x++) {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t