History

avcodec/ffv1enc: reduce stack usage

Fixes failures in some systems since 171060d5dc54d1526abde736a711ce234df632f2. This can be further improved by only allocating the arrays when needed.

James Almer committed 1 year ago

Commit 702239b1 parent fee5b0a2 files · +26 −29Browse files

Unified · Split

2 files
  1. libavcodec/ffv1.h +4 −0
  2. libavcodec/ffv1enc.c +22 −29
libavcodec/ffv1.h+4−0
@@ -111,6 +111,10 @@ typedef struct FFV1SliceContext { · 110 unmodified lines
111111 uint16_t fltmap [4][65536]; //halffloat encode & decode
112112 uint32_t fltmap32[4][65536]; //float decode
113113 };
114+ struct Unit {
115+ uint32_t val; //this is unneeded if you accept a dereference on each access
116+ uint16_t ndx;
117+ } unit[4][65536];
114118 } FFV1SliceContext;
115119
116120 typedef struct FFV1Context {
libavcodec/ffv1enc.c+22−29
@@ -1194,15 +1194,9 @@ static void encode_histogram_remap(FFV1Context *f, FFV1SliceContext *sc) · 1193 unmodified lines
11941194 }
11951195 }
11961196
1197−typedef struct Unit {
1198− uint32_t val; //this is unneeded if you accept a dereference on each access
1199− uint16_t ndx;
1200−} Unit;
1201−
12021197 static void load_rgb_float32_frame(FFV1Context *f, FFV1SliceContext *sc,
12031198 const uint8_t *src[4],
1204− int w, int h, const int stride[4],
1205− Unit unit[4][65536])
1199+ int w, int h, const int stride[4])
12061200 {
12071201 int x, y;
12081202 int transparency = f->transparency;
@@ -1226,18 +1220,18 @@ static void load_rgb_float32_frame(FFV1Context *f, FFV1SliceContext *sc, · 17 unmodified lines
12261220 }
12271221 // We cannot build a histogram as we do for 16bit, we need a bit of magic here
12281222 // Its possible to reduce the memory needed at the cost of more dereferencing
1229− unit[0][i].val = g;
1230− unit[0][i].ndx = x + y*w;
1223+ sc->unit[0][i].val = g;
1224+ sc->unit[0][i].ndx = x + y*w;
12311225
1232− unit[1][i].val = b;
1233− unit[1][i].ndx = x + y*w;
1226+ sc->unit[1][i].val = b;
1227+ sc->unit[1][i].ndx = x + y*w;
12341228
1235− unit[2][i].val = r;
1236− unit[2][i].ndx = x + y*w;
1229+ sc->unit[2][i].val = r;
1230+ sc->unit[2][i].ndx = x + y*w;
12371231
12381232 if (transparency) {
1239− unit[3][i].val = a;
1240− unit[3][i].ndx = x + y*w;
1233+ sc->unit[3][i].val = a;
1234+ sc->unit[3][i].ndx = x + y*w;
12411235 }
12421236 i++;
12431237 }
@@ -1245,11 +1239,11 @@ static void load_rgb_float32_frame(FFV1Context *f, FFV1SliceContext *sc, · 1 unmodified line
12451239
12461240 //TODO switch to radix sort
12471241 #define CMP(A,B) ((A)->val - (int64_t)(B)->val)
1248− AV_QSORT(unit[0], i, Unit, CMP);
1249− AV_QSORT(unit[1], i, Unit, CMP);
1250− AV_QSORT(unit[2], i, Unit, CMP);
1242+ AV_QSORT(sc->unit[0], i, struct Unit, CMP);
1243+ AV_QSORT(sc->unit[1], i, struct Unit, CMP);
1244+ AV_QSORT(sc->unit[2], i, struct Unit, CMP);
12511245 if (transparency)
1252− AV_QSORT(unit[3], i, Unit, CMP);
1246+ AV_QSORT(sc->unit[3], i, struct Unit, CMP);
12531247 }
12541248
12551249 typedef struct RemapEncoderState {
@@ -1296,7 +1290,7 @@ static inline void encode_mul(RemapEncoderState *s, int mul_index) · 40 unmodified lines
12961290 }
12971291 }
12981292
1299−static int encode_float32_remap_segment(FFV1SliceContext *sc, Unit unit[4][65536],
1293+static int encode_float32_remap_segment(FFV1SliceContext *sc,
13001294 RemapEncoderState *state_arg, int update, int final)
13011295 {
13021296 RemapEncoderState s;
@@ -1324,7 +1318,7 @@ static int encode_float32_remap_segment(FFV1SliceContext *sc, Unit unit[4][65536 · 21 unmodified lines
13241318 val = 1LL<<32;
13251319 }
13261320 } else
1327− val = unit[s.p][s.i].val;
1321+ val = sc->unit[s.p][s.i].val;
13281322
13291323 if (s.last_val != val) {
13301324 int64_t delta = 0;
@@ -1376,7 +1370,7 @@ static int encode_float32_remap_segment(FFV1SliceContext *sc, Unit unit[4][65536 · 45 unmodified lines
13761370
13771371 av_assert2(s.i == s.pixel_num || s.last_val + (val - s.last_val) * current_mul + delta == unit[s.p][s.i].val);
13781372 if (s.i < s.pixel_num)
1379− s.last_val = unit[s.p][s.i].val;
1373+ s.last_val = sc->unit[s.p][s.i].val;
13801374 }
13811375 s.current_mul_index = ((s.last_val + 1) * s.mul_count) >> 32;
13821376 if (!s.run)
@@ -1384,7 +1378,7 @@ static int encode_float32_remap_segment(FFV1SliceContext *sc, Unit unit[4][65536 · 1 unmodified line
13841378 s.compact_index ++;
13851379 }
13861380 if (final && s.i < s.pixel_num)
1387− sc->bitmap[s.p][unit[s.p][s.i].ndx] = s.compact_index;
1381+ sc->bitmap[s.p][sc->unit[s.p][s.i].ndx] = s.compact_index;
13881382 }
13891383
13901384 if (update) {
@@ -1394,7 +1388,7 @@ static int encode_float32_remap_segment(FFV1SliceContext *sc, Unit unit[4][65536 · 3 unmodified lines
13941388 }
13951389
13961390 static void encode_float32_remap(FFV1Context *f, FFV1SliceContext *sc,
1397− const uint8_t *src[4], Unit unit[4][65536])
1391+ const uint8_t *src[4])
13981392 {
13991393 RemapEncoderState s;
14001394 s.pixel_num = sc->slice_width * sc->slice_height;
@@ -1411,7 +1405,7 @@ static void encode_float32_remap(FFV1Context *f, FFV1SliceContext *sc, · 10 unmodified lines
14111405 s.mul_count = 1;
14121406
14131407 for (int i= 0; i<s.pixel_num; i++) {
1414− int64_t val = unit[p][i].val;
1408+ int64_t val = sc->unit[p][i].val;
14151409 if (val != last_val) {
14161410 av_assert2(last_val < val);
14171411 for(int si= 0; si < FF_ARRAY_ELEMS(score_tab); si++) {
@@ -1431,7 +1425,7 @@ static void encode_float32_remap(FFV1Context *f, FFV1SliceContext *sc, · 13 unmodified lines
14311425 s.mul[0] = -1 << best_index;
14321426 s.mul[s.mul_count] = 1;
14331427
1434− encode_float32_remap_segment(sc, unit, &s, 1, 1);
1428+ encode_float32_remap_segment(sc, &s, 1, 1);
14351429
14361430 sc->c = s.rc;
14371431 }
@@ -1559,9 +1553,8 @@ retry: · 121 unmodified lines
15591553
15601554 encode_histogram_remap(f, sc);
15611555 } else {
1562− Unit pairs[4][65536];
1563− load_rgb_float32_frame(f, sc, planes, width, height, p->linesize, pairs);
1564− encode_float32_remap(f, sc, planes, pairs);
1556+ load_rgb_float32_frame(f, sc, planes, width, height, p->linesize);
1557+ encode_float32_remap(f, sc, planes);
15651558 }
15661559 }
15671560

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