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

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
233 233
234 av_freep(&sc->sample_buffer);234 av_freep(&sc->sample_buffer);
235 av_freep(&sc->sample_buffer32);235 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 }
236 240
237 av_refstruct_unref(&sc->plane);241 av_refstruct_unref(&sc->plane);
238 }242 }
libavcodec/ffv1.h+4−5
@@ -106,11 +106,10 @@ typedef struct FFV1SliceContext { · 105 unmodified lines
106 uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2];106 uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2];
107 };107 };
108 };108 };
109 union {109
110 uint16_t bitmap [4][65536]; //float encode110 uint16_t *bitmap [4]; //float encode
111 uint16_t fltmap [4][65536]; //halffloat encode & decode111 uint16_t *fltmap [4]; //halffloat encode & decode
112 uint32_t fltmap32[4][65536]; //float decode112 uint32_t *fltmap32[4]; //float decode
113 };
114 struct Unit {113 struct Unit {
115 uint32_t val; //this is unneeded if you accept a dereference on each access114 uint32_t val; //this is unneeded if you accept a dereference on each access
116 uint16_t ndx;115 uint16_t ndx;
libavcodec/ffv1dec.c+17−1
@@ -326,7 +326,7 @@ static int decode_remap(FFV1Context *f, FFV1SliceContext *sc) · 325 unmodified lines
326 }326 }
327 if (i - 1 >= end)327 if (i - 1 >= end)
328 break;328 break;
329 if (j >= FF_ARRAY_ELEMS(sc->fltmap[p]))329 if (j >= 65536 /*FF_ARRAY_ELEMS(sc->fltmap[p])*/)
330 return AVERROR_INVALIDDATA;330 return AVERROR_INVALIDDATA;
331 if (end <= 0xFFFF) {331 if (end <= 0xFFFF) {
332 sc->fltmap [p][j++] = i ^ ((i& 0x8000) ? 0 : flip);332 sc->fltmap [p][j++] = i ^ ((i& 0x8000) ? 0 : flip);
@@ -387,6 +387,22 @@ static int decode_slice(AVCodecContext *c, void *arg) · 54 unmodified lines
387 y = sc->slice_y;387 y = sc->slice_y;
388 388
389 if (sc->remap) {389 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
390 ret = decode_remap(f, sc);406 ret = decode_remap(f, sc);
391 if (ret < 0)407 if (ret < 0)
392 return ret;408 return ret;
libavcodec/ffv1enc.c+17−6
@@ -326,7 +326,7 @@ static void load_plane(FFV1Context *f, FFV1SliceContext *sc, · 325 unmodified lines
326{326{
327 int x, y;327 int x, y;
328 328
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]));
330 330
331 for (y = 0; y < h; y++) {331 for (y = 0; y < h; y++) {
332 if (f->bits_per_raw_sample <= 8) {332 if (f->bits_per_raw_sample <= 8) {
@@ -1012,11 +1012,20 @@ static av_cold int encode_init_internal(AVCodecContext *avctx) · 679 unmodified lines
1012 p->context_count = s->context_count[p->quant_table_index];1012 p->context_count = s->context_count[p->quant_table_index];
1013 }1013 }
1014 av_assert0(s->remap_mode >= 0);1014 av_assert0(s->remap_mode >= 0);
1015 if (s->remap_mode && s->bits_per_raw_sample == 32) {1015 if (s->remap_mode) {
1016 for (int p = 0; p < 1 + 2*s->chroma_planes + s->transparency ; p++) {1016 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));1017 if (s->bits_per_raw_sample == 32) {
1018 if (!sc->unit[p])1018 sc->unit[p] = av_malloc_array(sc->slice_width, sc->slice_height * sizeof(**sc->unit));
1019 return AVERROR(ENOMEM);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 }
1020 }1029 }
1021 }1030 }
1022 1031
@@ -1821,8 +1830,10 @@ static av_cold int encode_close(AVCodecContext *avctx) · 798 unmodified lines
1821 for (int j = 0; j < s->max_slice_count; j++) {1830 for (int j = 0; j < s->max_slice_count; j++) {
1822 FFV1SliceContext *sc = &s->slices[j];1831 FFV1SliceContext *sc = &s->slices[j];
1823 1832
1824 for(int p = 0; p<4; p++)1833 for(int p = 0; p<4; p++) {
1825 av_freep(&sc->unit[p]);1834 av_freep(&sc->unit[p]);
1835 av_freep(&sc->bitmap[p]);
1836 }
1826 }1837 }
1827 1838
1828 av_freep(&avctx->stats_out);1839 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
134 int x, y;134 int x, y;
135 int transparency = f->transparency;135 int transparency = f->transparency;
136 136
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));
138 139
139 for (y = 0; y < h; y++) {140 for (y = 0; y < h; y++) {
140 for (x = 0; x < w; x++) {141 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