History

avcodec/ffv1: Store slices*planes with the minimum bits needed after remap

This also means that if a plane*slice has only 1 color nothing is stored after the remap table This also corrects the RCT offset to the exact value after remap not a fixed 65536

Michael Niedermayer committed 1 year ago

Commit feceed91 parent 80b77e86 files · +91 −24Browse files

Unified · Split

6 files
  1. libavcodec/ffv1.c +37 −0
  2. libavcodec/ffv1.h +2 −0
  3. libavcodec/ffv1dec.c +14 −3
  4. libavcodec/ffv1dec_template.c +20 −12
  5. libavcodec/ffv1enc.c +10 −5
  6. libavcodec/ffv1enc_template.c +8 −4
libavcodec/ffv1.c+37−0
@@ -219,6 +219,43 @@ void ff_ffv1_clear_slice_state(const FFV1Context *f, FFV1SliceContext *sc) · 218 unmodified lines
219219 }
220220 }
221221
222+void ff_ffv1_compute_bits_per_plane(const FFV1Context *f, FFV1SliceContext *sc, int bits[4], int *offset, int mask[4], int bits_per_raw_sample)
223+{
224+ // to simplify we use the remap_count as the symbol range in each plane
225+ if (!sc->remap) {
226+ sc->remap_count[0] =
227+ sc->remap_count[1] =
228+ sc->remap_count[2] =
229+ sc->remap_count[3] = 1 << (bits_per_raw_sample > 0 ? bits_per_raw_sample : 8);
230+ }
231+
232+ if (sc->remap)
233+ av_assert0(bits_per_raw_sample > 8); //breaks with lbd, needs review if added
234+
235+ //bits with no RCT
236+ for (int p=0; p<3+f->transparency; p++) {
237+ bits[p] = av_ceil_log2(sc->remap_count[p]);
238+ if (mask)
239+ mask[p] = (1<<bits[p]) - 1;
240+ }
241+
242+ //RCT
243+ if (sc->slice_coding_mode == 0) {
244+ *offset = sc->remap_count[0];
245+
246+ bits[0] = av_ceil_log2(FFMAX3(sc->remap_count[0], sc->remap_count[1], sc->remap_count[2]));
247+ bits[1] = av_ceil_log2(sc->remap_count[0] + sc->remap_count[1]);
248+ bits[2] = av_ceil_log2(sc->remap_count[0] + sc->remap_count[2]);
249+
250+ //old version coded a bit more than needed
251+ if (f->combined_version < 0x40008) {
252+ bits[0]++;
253+ if(f->transparency)
254+ bits[3]++;
255+ }
256+ }
257+}
258+
222259 int ff_ffv1_get_symbol(RangeCoder *c, uint8_t *state, int is_signed)
223260 {
224261 return get_symbol_inline(c, state, is_signed);
libavcodec/ffv1.h+2−0
@@ -106,6 +106,7 @@ typedef struct FFV1SliceContext { · 105 unmodified lines
106106 uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2];
107107 };
108108 };
109+ int remap_count[4];
109110
110111 uint16_t *bitmap [4]; //float encode
111112 uint16_t *fltmap [4]; //halffloat encode & decode
@@ -199,6 +200,7 @@ int ff_ffv1_parse_header(FFV1Context *f, RangeCoder *c, uint8_t *state); · 87 unmodified lines
199200 int ff_ffv1_read_extra_header(FFV1Context *f);
200201 int ff_ffv1_read_quant_tables(RangeCoder *c,
201202 int16_t quant_table[MAX_CONTEXT_INPUTS][256]);
203+void ff_ffv1_compute_bits_per_plane(const FFV1Context *f, FFV1SliceContext *sc, int bits[4], int offset[1], int mask[4], int bits_per_raw_sample);
202204 int ff_ffv1_get_symbol(RangeCoder *c, uint8_t *state, int is_signed);
203205
204206 /**
libavcodec/ffv1dec.c+14−3
@@ -99,6 +99,16 @@ static int decode_plane(FFV1Context *f, FFV1SliceContext *sc, · 98 unmodified lines
9999 {
100100 int x, y;
101101 int16_t *sample[2];
102+ int bits;
103+ unsigned mask;
104+
105+ if (sc->remap) {
106+ bits = av_ceil_log2(sc->remap_count[remap_index]);
107+ mask = (1<<bits)-1;
108+ } else {
109+ bits = f->avctx->bits_per_raw_sample;
110+ }
111+
102112 sample[0] = sc->sample_buffer + 3;
103113 sample[1] = sc->sample_buffer + w + 6 + 3;
104114
@@ -125,18 +135,18 @@ static int decode_plane(FFV1Context *f, FFV1SliceContext *sc, · 20 unmodified lines
125135 for (x = 0; x < w; x++)
126136 src[x*pixel_stride + stride * y] = sample[1][x];
127137 } else {
128− int ret = decode_line(f, sc, gb, w, sample, plane_index, f->avctx->bits_per_raw_sample, ac);
138+ int ret = decode_line(f, sc, gb, w, sample, plane_index, bits, ac);
129139 if (ret < 0)
130140 return ret;
131141
132142 if (sc->remap) {
133143 if (f->packed_at_lsb || f->avctx->bits_per_raw_sample == 16) {
134144 for (x = 0; x < w; x++) {
135− ((uint16_t*)(src + stride*y))[x*pixel_stride] = sc->fltmap[remap_index][sample[1][x] & 0xFFFF];
145+ ((uint16_t*)(src + stride*y))[x*pixel_stride] = sc->fltmap[remap_index][sample[1][x] & mask];
136146 }
137147 } else {
138148 for (x = 0; x < w; x++) {
139− int v = sc->fltmap[remap_index][sample[1][x] & 0xFFFF];
149+ int v = sc->fltmap[remap_index][sample[1][x] & mask];
140150 ((uint16_t*)(src + stride*y))[x*pixel_stride] = v << (16 - f->avctx->bits_per_raw_sample) | v >> (2 * f->avctx->bits_per_raw_sample - 16);
141151 }
142152 }
@@ -337,6 +347,7 @@ static int decode_remap(FFV1Context *f, FFV1SliceContext *sc) · 194 unmodified lines
337347 }
338348 lu ^= !run;
339349 }
350+ sc->remap_count[p] = j;
340351 }
341352 return 0;
342353 }
libavcodec/ffv1dec_template.c+20−12
@@ -36,6 +36,12 @@ RENAME(decode_line)(FFV1Context *f, FFV1SliceContext *sc, · 35 unmodified lines
3636 int run_mode = 0;
3737 int run_index = sc->run_index;
3838
39+ if (bits == 0) {
40+ for (x = 0; x < w; x++)
41+ sample[1][x] = 0;
42+ return 0;
43+ }
44+
3945 if (is_input_end(c, gb, ac))
4046 return AVERROR_INVALIDDATA;
4147
@@ -138,10 +144,12 @@ static int RENAME(decode_rgb_frame)(FFV1Context *f, FFV1SliceContext *sc, · 96 unmodified lines
138144 int x, y, p;
139145 TYPE *sample[4][2];
140146 int lbd = f->avctx->bits_per_raw_sample <= 8;
141− int bits = f->avctx->bits_per_raw_sample > 0 ? FFMIN(f->avctx->bits_per_raw_sample, 16) : 8;
142− int offset = 1 << bits;
147+ int bits[4], offset;
143148 int transparency = f->transparency;
144149 int ac = f->ac;
150+ unsigned mask[4];
151+
152+ ff_ffv1_compute_bits_per_plane(f, sc, bits, &offset, mask, f->avctx->bits_per_raw_sample);
145153
146154 if (sc->slice_coding_mode == 1)
147155 ac = 1;
@@ -165,10 +173,10 @@ static int RENAME(decode_rgb_frame)(FFV1Context *f, FFV1SliceContext *sc, · 17 unmodified lines
165173
166174 sample[p][1][-1]= sample[p][0][0 ];
167175 sample[p][0][ w]= sample[p][0][w-1];
168− if (lbd && sc->slice_coding_mode == 0)
176+ if (bits[p] == 9)
169177 ret = RENAME(decode_line)(f, sc, gb, w, sample[p], (p + 1)/2, 9, ac);
170178 else
171− ret = RENAME(decode_line)(f, sc, gb, w, sample[p], (p + 1)/2, bits + (sc->slice_coding_mode != 1), ac);
179+ ret = RENAME(decode_line)(f, sc, gb, w, sample[p], (p + 1)/2, bits[p], ac);
172180 if (ret < 0)
173181 return ret;
174182 }
@@ -187,17 +195,17 @@ static int RENAME(decode_rgb_frame)(FFV1Context *f, FFV1SliceContext *sc, · 12 unmodified lines
187195 }
188196 if (sc->remap) {
189197 if (f->avctx->bits_per_raw_sample == 32) {
190− g = sc->fltmap32[0][g & 0xFFFF];
191− b = sc->fltmap32[1][b & 0xFFFF];
192− r = sc->fltmap32[2][r & 0xFFFF];
198+ g = sc->fltmap32[0][g & mask[0]];
199+ b = sc->fltmap32[1][b & mask[1]];
200+ r = sc->fltmap32[2][r & mask[2]];
193201 if (transparency)
194− a = sc->fltmap32[3][a & 0xFFFF];
202+ a = sc->fltmap32[3][a & mask[3]];
195203 } else {
196− g = sc->fltmap[0][g & 0xFFFF];
197− b = sc->fltmap[1][b & 0xFFFF];
198− r = sc->fltmap[2][r & 0xFFFF];
204+ g = sc->fltmap[0][g & mask[0]];
205+ b = sc->fltmap[1][b & mask[1]];
206+ r = sc->fltmap[2][r & mask[2]];
199207 if (transparency)
200− a = sc->fltmap[3][a & 0xFFFF];
208+ a = sc->fltmap[3][a & mask[3]];
201209 }
202210 }
203211
libavcodec/ffv1enc.c+10−5
@@ -433,7 +433,7 @@ static void set_micro_version(FFV1Context *f) · 432 unmodified lines
433433 if (f->version == 3) {
434434 f->micro_version = 4;
435435 } else if (f->version == 4) {
436− f->micro_version = 7;
436+ f->micro_version = 8;
437437 } else
438438 av_assert0(0);
439439
@@ -1216,6 +1216,7 @@ static void encode_histogram_remap(FFV1Context *f, FFV1SliceContext *sc) · 776 unmodified lines
12161216 }
12171217 if (run)
12181218 put_symbol(&sc->c, state[lu], run, 0);
1219+ sc->remap_count[p] = j;
12191220 }
12201221 }
12211222
@@ -1370,7 +1371,8 @@ static int encode_float32_remap_segment(FFV1SliceContext *sc, · 148 unmodified lines
13701371 mul[ current_mul_index ] *= -1;
13711372 put_symbol_inline(&rc, state[0][2], mul[ current_mul_index ], 0, NULL, NULL);
13721373 }
1373− compact_index ++;
1374+ if (i < pixel_num)
1375+ compact_index ++;
13741376 }
13751377 }
13761378 if (!run || run1final)
@@ -1380,6 +1382,8 @@ static int encode_float32_remap_segment(FFV1SliceContext *sc, · 3 unmodified lines
13801382
13811383 if (update) {
13821384 sc->c = rc;
1385+ av_assert0(compact_index <= 65535);
1386+ sc->remap_count[p] = compact_index + 1;
13831387 }
13841388 return get_rac_count(&rc);
13851389 }
@@ -1492,10 +1496,11 @@ static int encode_float32_rgb_frame(FFV1Context *f, FFV1SliceContext *sc, · 106 unmodified lines
14921496 const int ring_size = f->context_model ? 3 : 2;
14931497 int32_t *sample[4][3];
14941498 const int pass1 = !!(f->avctx->flags & AV_CODEC_FLAG_PASS1);
1495− int bits = 16; //TODO explain this in the specifciation, we have 32bits in but really encode max 16
1496− int offset = 1 << bits;
1499+ int bits[4], offset;
14971500 int transparency = f->transparency;
14981501
1502+ ff_ffv1_compute_bits_per_plane(f, sc, bits, &offset, NULL, f->bits_per_raw_sample);
1503+
14991504 sc->run_index = 0;
15001505
15011506 memset(RENAME(sc->sample_buffer), 0, ring_size * MAX_PLANES *
@@ -1532,7 +1537,7 @@ static int encode_float32_rgb_frame(FFV1Context *f, FFV1SliceContext *sc, · 30 unmodified lines
15321537 sample[p][0][-1] = sample[p][1][0 ];
15331538 sample[p][1][ w] = sample[p][1][w-1];
15341539 ret = encode_line32(f, sc, f->avctx, w, sample[p], (p + 1) / 2,
1535− bits + (sc->slice_coding_mode != 1), ac, pass1);
1540+ bits[p], ac, pass1);
15361541 if (ret < 0)
15371542 return ret;
15381543 }
libavcodec/ffv1enc_template.c+8−4
@@ -35,6 +35,9 @@ RENAME(encode_line)(FFV1Context *f, FFV1SliceContext *sc, · 34 unmodified lines
3535 int run_count = 0;
3636 int run_mode = 0;
3737
38+ if (bits == 0)
39+ return 0;
40+
3841 if (ac != AC_GOLOMB_RICE) {
3942 if (c->bytestream_end - c->bytestream < w * 35) {
4043 av_log(logctx, AV_LOG_ERROR, "encoded Range Coder frame too large\n");
@@ -172,11 +175,12 @@ static int RENAME(encode_rgb_frame)(FFV1Context *f, FFV1SliceContext *sc, · 131 unmodified lines
172175 const int pass1 = !!(f->avctx->flags & AV_CODEC_FLAG_PASS1);
173176 int lbd = f->bits_per_raw_sample <= 8;
174177 int packed = !src[1];
175− int bits = f->bits_per_raw_sample > 0 ? f->bits_per_raw_sample : 8;
176− int offset = 1 << bits;
178+ int bits[4], offset;
177179 int transparency = f->transparency;
178180 int packed_size = (3 + transparency)*2;
179181
182+ ff_ffv1_compute_bits_per_plane(f, sc, bits, &offset, NULL, f->bits_per_raw_sample);
183+
180184 sc->run_index = 0;
181185
182186 memset(RENAME(sc->sample_buffer), 0, ring_size * MAX_PLANES *
@@ -239,11 +243,11 @@ static int RENAME(encode_rgb_frame)(FFV1Context *f, FFV1SliceContext *sc, · 56 unmodified lines
239243 int ret;
240244 sample[p][0][-1] = sample[p][1][0 ];
241245 sample[p][1][ w] = sample[p][1][w-1];
242− if (lbd && sc->slice_coding_mode == 0)
246+ if (bits[p] == 9)
243247 ret = RENAME(encode_line)(f, sc, f->avctx, w, sample[p], (p + 1) / 2, 9, ac, pass1);
244248 else
245249 ret = RENAME(encode_line)(f, sc, f->avctx, w, sample[p], (p + 1) / 2,
246− bits + (sc->slice_coding_mode != 1), ac, pass1);
250+ bits[p], ac, pass1);
247251 if (ret < 0)
248252 return ret;
249253 }

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