46 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
47 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
48 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
49 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
50 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
51 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
52 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
53 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
54 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
55 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
56 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
57 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
58 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1,
59 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
60 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
61 -1, -1, -1, -1, -1, -1, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0,
65 0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
66 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
67 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
68 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
69 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
70 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
71 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
72 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
73 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
74 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
75 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
76 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
77 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
78 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
79 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
80 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -1,
84 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2,
85 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3,
86 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
87 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
88 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
89 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
90 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
91 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
92 -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
93 -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
94 -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
95 -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
96 -4, -4, -4, -4, -4, -4, -4, -4, -4, -3, -3, -3, -3, -3, -3, -3,
97 -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
98 -3, -3, -3, -3, -3, -3, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
99 -2, -2, -2, -2, -1, -1, -1, -1, -1, -1, -1, -1, -0, -0, -0, -0,
103 0, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
104 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
105 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
106 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
107 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
108 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
109 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
110 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
111 -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
112 -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
113 -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
114 -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
115 -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
116 -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -4, -4,
117 -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
118 -4, -4, -4, -4, -4, -3, -3, -3, -3, -3, -3, -3, -2, -2, -2, -1,
122 0, 10, 10, 10, 10, 16, 16, 16, 28, 16, 16, 29, 42, 49, 20, 49,
123 59, 25, 26, 26, 27, 31, 33, 33, 33, 34, 34, 37, 67, 38, 39, 39,
124 40, 40, 41, 79, 43, 44, 45, 45, 48, 48, 64, 50, 51, 52, 88, 52,
125 53, 74, 55, 57, 58, 58, 74, 60, 101, 61, 62, 84, 66, 66, 68, 69,
126 87, 82, 71, 97, 73, 73, 82, 75, 111, 77, 94, 78, 87, 81, 83, 97,
127 85, 83, 94, 86, 99, 89, 90, 99, 111, 92, 93, 134, 95, 98, 105, 98,
128 105, 110, 102, 108, 102, 118, 103, 106, 106, 113, 109, 112, 114, 112, 116, 125,
129 115, 116, 117, 117, 126, 119, 125, 121, 121, 123, 145, 124, 126, 131, 127, 129,
130 165, 130, 132, 138, 133, 135, 145, 136, 137, 139, 146, 141, 143, 142, 144, 148,
131 147, 155, 151, 149, 151, 150, 152, 157, 153, 154, 156, 168, 158, 162, 161, 160,
132 172, 163, 169, 164, 166, 184, 167, 170, 177, 174, 171, 173, 182, 176, 180, 178,
133 175, 189, 179, 181, 186, 183, 192, 185, 200, 187, 191, 188, 190, 197, 193, 196,
134 197, 194, 195, 196, 198, 202, 199, 201, 210, 203, 207, 204, 205, 206, 208, 214,
135 209, 211, 221, 212, 213, 215, 224, 216, 217, 218, 219, 220, 222, 228, 223, 225,
136 226, 224, 227, 229, 240, 230, 231, 232, 233, 234, 235, 236, 238, 239, 237, 242,
137 241, 243, 242, 244, 245, 246, 247, 248, 249, 250, 251, 252, 252, 253, 254, 255,
141 const uint8_t one_state[256])
146 for (
i = 1;
i < 256;
i++)
147 l2tab[
i] = -
log2(
i / 256.0) * ((1
U << 31) / 8);
149 for (
i = 0;
i < 256;
i++) {
150 uint64_t best_len[256];
152 for (j = 0; j < 256; j++)
153 best_len[j] = UINT64_MAX;
155 for (j =
FFMAX(
i - 10, 1); j <
FFMIN(
i + 11, 256); j++) {
156 uint32_t occ[256] = { 0 };
163 for (k = 0; k < 256; k++) {
164 uint32_t newocc[256] = { 0 };
165 for (m = 1; m < 256; m++)
167 len += (occ[m]*((
i *(uint64_t)l2tab[ m]
168 + (256-
i)*(uint64_t)l2tab[256-m])>>8)) >> 8;
170 if (
len < best_len[k]) {
172 best_state[
i][k] = j;
174 for (m = 1; m < 256; m++)
176 newocc[ one_state[ m]] += occ[m] * (uint64_t)
i >> 8;
177 newocc[256 - one_state[256 - m]] += occ[m] * (uint64_t)(256 -
i) >> 8;
179 memcpy(occ, newocc,
sizeof(occ));
186 uint8_t *
state,
int v,
188 uint64_t rc_stat[256][2],
189 uint64_t rc_stat2[32][2])
193 #define put_rac(C, S, B) \
196 rc_stat[*(S)][B]++; \
197 rc_stat2[(S) - state][B]++; \
203 const unsigned a = is_signed ?
FFABS(v) : v;
207 for (
i = 0;
i < e;
i++)
211 for (
i = e - 1;
i >= 0;
i--)
217 for (
i = 0;
i < e;
i++)
221 for (
i = e - 1;
i >= 0;
i--)
234 int v,
int is_signed)
248 while (i < state->error_sum) {
257 ff_dlog(
NULL,
"v:%d/%d bias:%d error:%d drift:%d count:%d k:%d\n", v,
code,
265 #define RENAME(name) name
271 #define RENAME(name) name ## 32
275 const uint8_t *
src,
int w,
int h,
276 int stride,
int plane_index,
int remap_index,
int pixel_stride,
int ac)
280 const int ring_size =
f->context_model ? 3 : 2;
288 for (y = 0; y <
h; y++) {
295 if (
f->bits_per_raw_sample <= 8) {
296 for (x = 0; x <
w; x++)
299 for (x = 0; x <
w; x++)
305 if (
f->packed_at_lsb) {
306 for (x = 0; x <
w; x++) {
310 for (x = 0; x <
w; x++) {
311 sample[0][x] = ((uint16_t*)(
src +
stride*y))[x * pixel_stride] >> (16 -
f->bits_per_raw_sample);
315 for (x = 0; x <
w; x++)
326 const uint8_t *
src,
int w,
int h,
327 int stride,
int remap_index,
int pixel_stride)
331 memset(sc->
fltmap[remap_index], 0, 65536 *
sizeof(*sc->
fltmap[remap_index]));
333 for (y = 0; y <
h; y++) {
334 if (
f->bits_per_raw_sample <= 8) {
335 for (x = 0; x <
w; x++)
338 if (
f->packed_at_lsb) {
339 for (x = 0; x <
w; x++)
340 sc->
fltmap[remap_index][ ((uint16_t*)(
src +
stride*y))[x * pixel_stride] ] = 1;
342 for (x = 0; x <
w; x++)
343 sc->
fltmap[remap_index][ ((uint16_t*)(
src +
stride*y))[x * pixel_stride] >> (16 -
f->bits_per_raw_sample) ] = 1;
368 for (
i = 0;
i < 5;
i++)
377 for (
int i = 0;
i < nb_contexts;
i++)
379 if (initial_state[
i][j] != 128)
392 if (
f->version < 2) {
396 for (
i = 1;
i < 256;
i++)
398 f->state_transition[
i] -
c->one_state[
i], 1);
409 }
else if (
f->version < 3) {
411 for (
i = 0;
i <
f->slice_count;
i++) {
414 (
fs->slice_x + 1) *
f->num_h_slices /
f->width, 0);
416 (
fs->slice_y + 1) *
f->num_v_slices /
f->height, 0);
418 (
fs->slice_width + 1) *
f->num_h_slices /
f->width - 1,
421 (
fs->slice_height + 1) *
f->num_v_slices /
f->height - 1,
423 for (j = 0; j <
f->plane_count; j++) {
425 av_assert0(
fs->plane[j].quant_table_index ==
f->context_model);
433 f->combined_version =
f->version << 16;
434 if (
f->version > 2) {
435 if (
f->version == 3) {
436 f->micro_version = 4;
437 }
else if (
f->version == 4) {
438 f->micro_version = 9;
442 f->combined_version +=
f->micro_version;
457 memset(state2, 128,
sizeof(state2));
460 f->avctx->extradata_size = 10000 + 4 +
461 (11 * 11 * 5 * 5 * 5 + 11 * 11 * 11) * 32;
463 if (!
f->avctx->extradata)
474 for (
i = 1;
i < 256;
i++)
487 for (
i = 0;
i <
f->quant_table_count;
i++)
490 for (
i = 0;
i <
f->quant_table_count;
i++) {
493 for (j = 0; j <
f->context_count[
i]; j++)
495 int pred = j ?
f->initial_states[
i][j - 1][k] : 128;
497 (int8_t)(
f->initial_states[
i][j][k] -
pred), 1);
504 if (
f->version > 2) {
511 AV_WL32(
f->avctx->extradata +
f->avctx->extradata_size, v);
512 f->avctx->extradata_size += 4;
519 int i, i2, changed,
print = 0;
523 for (
i = 12;
i < 244;
i++) {
524 for (i2 =
i + 1; i2 < 245 && i2 <
i + 4; i2++) {
526 #define COST(old, new) \
527 s->rc_stat[old][0] * -log2((256 - (new)) / 256.0) + \
528 s->rc_stat[old][1] * -log2((new) / 256.0)
530 #define COST2(old, new) \
531 COST(old, new) + COST(256 - (old), 256 - (new))
535 if (size0 - sizeX > size0*(1e-14) &&
i != 128 && i2 != 128) {
538 FFSWAP(
int,
s->rc_stat[
i][0],
s->rc_stat[i2][0]);
539 FFSWAP(
int,
s->rc_stat[
i][1],
s->rc_stat[i2][1]);
541 FFSWAP(
int, stt[256 -
i], stt[256 - i2]);
542 FFSWAP(
int,
s->rc_stat[256 -
i][0],
s->rc_stat[256 - i2][0]);
543 FFSWAP(
int,
s->rc_stat[256 -
i][1],
s->rc_stat[256 - i2][1]);
545 for (j = 1; j < 256; j++) {
548 else if (stt[j] == i2)
551 if (stt[256 - j] == 256 -
i)
552 stt[256 - j] = 256 - i2;
553 else if (stt[256 - j] == 256 - i2)
554 stt[256 - j] = 256 -
i;
569 int plane_count = 1 + 2*
s->chroma_planes +
s->transparency;
572 s->num_v_slices = (avctx->
width > 352 || avctx->
height > 288 || !avctx->
slices) ? 2 : 1;
573 s->num_v_slices =
FFMIN(
s->num_v_slices, max_v_slices);
574 for (;
s->num_v_slices <= 32;
s->num_v_slices++) {
575 for (
s->num_h_slices =
s->num_v_slices;
s->num_h_slices <= 2*
s->num_v_slices;
s->num_h_slices++) {
576 int maxw = (avctx->
width +
s->num_h_slices - 1) /
s->num_h_slices;
577 int maxh = (avctx->
height +
s->num_v_slices - 1) /
s->num_v_slices;
578 if (
s->num_h_slices > max_h_slices ||
s->num_v_slices > max_v_slices)
580 if (maxw * maxh * (
int64_t)(
s->bits_per_raw_sample+1) * plane_count > 8<<24)
588 if (maxw*maxh > 360*288)
595 "Unsupported number %d of slices requested, please specify a "
596 "supported number with -slices (ex:4,6,9,12,16, ...)\n",
608 s->version =
FFMAX(
s->version, 2);
617 s->version =
FFMAX(
s->version, 2);
619 if (avctx->
level <= 0 &&
s->version == 2) {
623 if (avctx->
level <
s->version) {
624 av_log(avctx,
AV_LOG_ERROR,
"Version %d needed for requested features but %d requested\n",
s->version, avctx->
level);
627 s->version = avctx->
level;
628 }
else if (
s->version < 3)
632 if (
s->version >= 4) {
634 }
else if (
s->version >= 3) {
642 s->version =
FFMAX(
s->version, 3);
644 s->version =
FFMAX(
s->version, 4);
645 s->crcref = 0x7a8c4079;
649 av_log(avctx,
AV_LOG_ERROR,
"Version 2 or 4 needed for requested features but version 2 or 4 is experimental and not enabled\n");
654 for (
i = 1;
i < 256;
i++)
659 for (
i = 1;
i < 256;
i++)
660 s->state_transition[
i] =
c.one_state[
i];
663 for (
i = 0;
i < 256;
i++) {
664 s->quant_table_count = 2;
665 if ((
s->qtable == -1 &&
s->bits_per_raw_sample <= 8) ||
s->qtable == 1) {
671 s->quant_tables[1][2][
i]= 11*11*
quant5 [
i];
672 s->quant_tables[1][3][
i]= 5*11*11*
quant5 [
i];
673 s->quant_tables[1][4][
i]= 5*5*11*11*
quant5 [
i];
674 s->context_count[0] = (11 * 11 * 11 + 1) / 2;
675 s->context_count[1] = (11 * 11 * 5 * 5 * 5 + 1) / 2;
685 s->context_count[0] = (9 * 9 * 9 + 1) / 2;
686 s->context_count[1] = (9 * 9 * 5 * 5 * 5 + 1) / 2;
693 if (!
s->transparency)
695 if (!
s->chroma_planes &&
s->version > 3)
698 s->picture_number = 0;
701 for (
i = 0;
i <
s->quant_table_count;
i++) {
703 sizeof(*
s->rc_stat2[
i]));
719 for (j = 0; j < 256; j++)
720 for (
i = 0;
i < 2;
i++) {
721 s->rc_stat[j][
i] = strtol(
p, &next, 0);
724 "2Pass file invalid at %d %d [%s]\n", j,
i,
p);
730 for (
i = 0;
i <
s->quant_table_count;
i++)
731 for (j = 0; j <
s->context_count[
i]; j++) {
732 for (k = 0; k < 32; k++)
733 for (m = 0; m < 2; m++) {
734 s->rc_stat2[
i][j][k][m] = strtol(
p, &next, 0);
737 "2Pass file invalid at %d %d %d %d [%s]\n",
745 gob_count = strtol(
p, &next, 0);
746 if (next ==
p || gob_count <= 0) {
752 while (*
p ==
'\n' || *
p ==
' ')
762 for (
i = 0;
i <
s->quant_table_count;
i++) {
763 for (k = 0; k < 32; k++) {
766 for (j = 0; j <
s->context_count[
i]; j++) {
768 if (
s->rc_stat2[
i][j][k][0] +
s->rc_stat2[
i][j][k][1] > 200 && j ||
a+
b > 200) {
770 p = 256.0 *
b / (
a +
b);
771 s->initial_states[
i][jp][k] =
773 for(jp++; jp<j; jp++)
774 s->initial_states[
i][jp][k] =
s->initial_states[
i][jp-1][k];
777 a +=
s->rc_stat2[
i][j][k][0];
778 b +=
s->rc_stat2[
i][j][k][1];
780 p = 256.0 *
b / (
a +
b);
782 s->initial_states[
i][j][k] =
790 if (
s->version <= 1) {
817 s->bits_per_raw_sample = 9;
828 s->bits_per_raw_sample = 10;
838 s->bits_per_raw_sample = 12;
845 s->bits_per_raw_sample = 14;
846 s->packed_at_lsb = 1;
861 s->bits_per_raw_sample = 16;
862 }
else if (!
s->bits_per_raw_sample) {
865 if (
s->bits_per_raw_sample <= 8) {
869 s->version =
FFMAX(
s->version, 1);
885 s->chroma_planes =
desc->nb_components < 3 ? 0 : 1;
889 s->bits_per_raw_sample = 8;
890 else if (!
s->bits_per_raw_sample)
891 s->bits_per_raw_sample = 8;
896 s->chroma_planes = 1;
897 s->bits_per_raw_sample = 8;
902 s->chroma_planes = 1;
903 s->bits_per_raw_sample = 16;
905 s->version =
FFMAX(
s->version, 1);
909 s->chroma_planes = 1;
910 s->bits_per_raw_sample = 16;
912 s->version =
FFMAX(
s->version, 1);
917 s->chroma_planes = 1;
918 s->bits_per_raw_sample = 8;
922 s->bits_per_raw_sample = 9;
929 s->bits_per_raw_sample = 10;
934 s->bits_per_raw_sample = 12;
939 s->bits_per_raw_sample = 14;
946 s->bits_per_raw_sample = 16;
951 s->bits_per_raw_sample = 32;
952 else if (!
s->bits_per_raw_sample)
956 s->chroma_planes = 1;
957 if (
s->bits_per_raw_sample >= 16) {
960 s->version =
FFMAX(
s->version, 1);
968 if (
s->flt ||
s->remap_mode > 0)
969 s->version =
FFMAX(
s->version, 4);
972 if (
s->remap_mode < 0)
973 s->remap_mode =
s->flt ? 2 : 0;
974 if (
s->remap_mode == 0 &&
s->bits_per_raw_sample == 32) {
978 if (
s->remap_mode == 2 &&
979 !((
s->bits_per_raw_sample == 16 ||
s->bits_per_raw_sample == 32 ||
s->bits_per_raw_sample == 64) &&
s->flt)) {
1004 if (
s->bits_per_raw_sample > (
s->version > 3 ? 16 : 8) && !
s->remap_mode) {
1007 "high bits_per_raw_sample, forcing range coder\n");
1017 if (
s->version > 1) {
1027 s->slice_count =
s->max_slice_count;
1029 for (
int j = 0; j <
s->slice_count; j++) {
1032 for (
int i = 0;
i <
s->plane_count;
i++) {
1035 p->quant_table_index =
s->context_model;
1036 p->context_count =
s->context_count[
p->quant_table_index];
1039 if (
s->remap_mode) {
1040 for (
int p = 0;
p < 1 + 2*
s->chroma_planes +
s->transparency ;
p++) {
1041 if (
s->bits_per_raw_sample == 32) {
1058 s->slices[j].remap =
s->remap_mode;
1064 #define STATS_OUT_SIZE 1024 * 1024 * 6
1069 for (
int i = 0;
i <
s->quant_table_count;
i++)
1070 for (
int j = 0; j <
s->max_slice_count; j++) {
1094 for (j=0; j<
f->plane_count; j++) {
1104 if (
f->version > 3) {
1118 const uint8_t *
src[3],
const int stride[3],
int w,
int h)
1120 #define NB_Y_COEFF 15
1121 static const int rct_y_coeff[15][2] = {
1141 int x, y,
i,
p, best;
1143 int lbd =
f->bits_per_raw_sample <= 8;
1144 int packed = !
src[1];
1145 int transparency =
f->transparency;
1146 int packed_size = (3 + transparency)*2;
1148 for (y = 0; y <
h; y++) {
1149 int lastr=0, lastg=0, lastb=0;
1150 for (
p = 0;
p < 3;
p++)
1153 for (x = 0; x <
w; x++) {
1157 unsigned v = *((
const uint32_t*)(
src[0] + x*4 +
stride[0]*y));
1159 g = (v >> 8) & 0xFF;
1160 r = (v >> 16) & 0xFF;
1161 }
else if (packed) {
1162 const uint16_t *
p = ((
const uint16_t*)(
src[0] + x*packed_size +
stride[0]*y));
1166 }
else if (
f->use32bit || transparency) {
1167 g = *((
const uint16_t *)(
src[0] + x*2 +
stride[0]*y));
1168 b = *((
const uint16_t *)(
src[1] + x*2 +
stride[1]*y));
1169 r = *((
const uint16_t *)(
src[2] + x*2 +
stride[2]*y));
1171 b = *((
const uint16_t*)(
src[0] + x*2 +
stride[0]*y));
1172 g = *((
const uint16_t*)(
src[1] + x*2 +
stride[1]*y));
1173 r = *((
const uint16_t*)(
src[2] + x*2 +
stride[2]*y));
1180 int bg = ag -
sample[0][x];
1181 int bb = ab -
sample[1][x];
1182 int br = ar -
sample[2][x];
1188 stat[
i] +=
FFABS(bg + ((br*rct_y_coeff[
i][0] + bb*rct_y_coeff[
i][1])>>2));
1204 if (stat[
i] < stat[best])
1214 int len = 1 <<
f->bits_per_raw_sample;
1217 for (
int p= 0;
p < 1 + 2*
f->chroma_planes +
f->transparency;
p++) {
1220 uint8_t
state[2][32];
1226 for (
int i= 0;
i<
len;
i++) {
1227 int ri =
i ^ ((
i&0x8000) ? 0 :
flip);
1248 const uint8_t *
src[4],
1252 int transparency =
f->transparency;
1255 for (y = 0; y <
h; y++) {
1256 for (x = 0; x <
w; x++) {
1259 g = *((
const uint32_t *)(
src[0] + x*4 +
stride[0]*y));
1260 b = *((
const uint32_t *)(
src[1] + x*4 +
stride[1]*y));
1261 r = *((
const uint32_t *)(
src[2] + x*4 +
stride[2]*y));
1263 a = *((
const uint32_t *)(
src[3] + x*4 +
stride[3]*y));
1265 if (sc->
remap == 2) {
1266 #define FLIP(f) (((f)&0x80000000) ? (f) : (f)^0x7FFFFFFF);
1291 #define CMP(A,B) ((A)->val - (int64_t)(B)->val)
1300 int p,
int mul_count,
int *mul_tab,
int update,
int final)
1303 uint8_t
state[2][3][32];
1309 int compact_index = -1;
1311 int current_mul_index = -1;
1314 int run1start_last_val;
1315 int run1start_mul_index;
1317 memcpy(mul, mul_tab,
sizeof(*mul_tab)*(mul_count+1));
1322 for (;
i < pixel_num+1;
i++) {
1323 int current_mul = current_mul_index < 0 ? 1 :
FFABS(mul[current_mul_index]);
1325 if (
i == pixel_num) {
1326 if (last_val == 0xFFFFFFFF && (!
run || run1final)) {
1329 val = last_val + ((1LL<<32) - last_val + current_mul - 1) / current_mul * current_mul;
1331 val += lu * current_mul;
1336 if (last_val !=
val) {
1349 run1start_i =
i - 1;
1350 run1start_last_val = last_val;
1351 run1start_mul_index= current_mul_index;
1365 last_val += current_mul;
1369 last_val = run1start_last_val;
1370 current_mul_index = run1start_mul_index;
1382 if (current_mul > 1)
1390 current_mul_index = ((last_val + 1) * mul_count) >> 32;
1391 if (!
run || run1final) {
1393 if (mul[ current_mul_index ] < 0) {
1395 mul[ current_mul_index ] *= -1;
1402 if (!
run || run1final)
1403 if (
final &&
i < pixel_num)
1415 const uint8_t *
src[4])
1418 const int max_log2_mul_count = ((
int[]){ 1, 1, 1, 9, 9, 10})[
f->remap_optimizer];
1419 const int log2_mul_count_step = ((
int[]){ 1, 1, 1, 9, 9, 1})[
f->remap_optimizer];
1420 const int max_log2_mul = ((
int[]){ 1, 8, 8, 9, 22, 22})[
f->remap_optimizer];
1421 const int log2_mul_step = ((
int[]){ 1, 8, 1, 1, 1, 1})[
f->remap_optimizer];
1422 const int bruteforce_count = ((
int[]){ 0, 0, 0, 1, 1, 1})[
f->remap_optimizer];
1423 const int stair_mode = ((
int[]){ 0, 0, 0, 1, 0, 0})[
f->remap_optimizer];
1424 const int magic_log2 = ((
int[]){ 1, 1, 1, 1, 0, 0})[
f->remap_optimizer];
1426 for (
int p= 0;
p < 1 + 2*
f->chroma_planes +
f->transparency;
p++) {
1427 int best_log2_mul_count = 0;
1428 float score_sum[11] = {0};
1429 int mul_all[11][1025];
1431 for (
int log2_mul_count= 0; log2_mul_count <= max_log2_mul_count; log2_mul_count += log2_mul_count_step) {
1432 float score_tab_all[1025][23] = {0};
1434 int *mul_tab = mul_all[log2_mul_count];
1435 int last_mul_index = -1;
1436 int mul_count = 1 << log2_mul_count;
1438 score_sum[log2_mul_count] = 2 * log2_mul_count;
1440 score_sum[log2_mul_count] =
av_float2int((
float)mul_count * mul_count);
1441 for (
int i= 0;
i<pixel_num;
i++) {
1443 int mul_index = (
val + 1LL)*mul_count >> 32;
1444 if (
val != last_val) {
1445 float *score_tab = score_tab_all[(last_val + 1LL)*mul_count >> 32];
1447 for(
int si= 0; si <= max_log2_mul; si += log2_mul_step) {
1454 }
else if (stair_mode && mul_count == 512 && si == max_log2_mul ) {
1455 if (mul_index >= 0x378/8 && mul_index <= 23 + 0x378/8) {
1456 mul = (0x800080 >> (mul_index - 0x378/8));
1460 mul = (0x10001LL)<<si >> 16;
1472 if (mul_index != last_mul_index)
1477 score_tab[si] +=
log2f(score);
1481 last_mul_index = mul_index;
1483 for(
int i= 0;
i<mul_count;
i++) {
1485 float *score_tab = score_tab_all[
i];
1486 for(
int si= 0; si <= max_log2_mul; si += log2_mul_step) {
1487 if (score_tab[si] < score_tab[ best_index ])
1490 if (stair_mode && mul_count == 512 && best_index == max_log2_mul ) {
1491 if (
i >= 0x378/8 &&
i <= 23 + 0x378/8) {
1492 mul_tab[
i] = -(0x800080 >> (
i - 0x378/8));
1496 mul_tab[
i] = -((0x10001LL)<<best_index >> 16);
1497 score_sum[log2_mul_count] += score_tab[ best_index ];
1499 mul_tab[mul_count] = 1;
1501 if (bruteforce_count)
1504 if (score_sum[log2_mul_count] < score_sum[best_log2_mul_count])
1505 best_log2_mul_count = log2_mul_count;
1513 const uint8_t *
src[4],
1514 int w,
int h,
const int stride[4],
int ac)
1517 const int ring_size =
f->context_model ? 3 : 2;
1521 int transparency =
f->transparency;
1533 for (y = 0; y <
h; y++) {
1538 for (x = 0; x <
w; x++) {
1559 for (
p = 0;
p < 3 + transparency;
p++) {
1564 bits[
p], ac, pass1);
1581 const AVFrame *
const p =
f->cur_enc_frame;
1587 const uint8_t *
planes[4] = {
p->data[0] + ps*x + y*
p->linesize[0],
1588 p->data[1] ?
p->data[1] + ps*x + y*
p->linesize[1] :
NULL,
1589 p->data[2] ?
p->data[2] + ps*x + y*
p->linesize[2] :
NULL,
1590 p->data[3] ?
p->data[3] + ps*x + y*
p->linesize[3] :
NULL};
1594 if (
f->version > 3 &&
f->colorspace == 1) {
1604 if (
f->version > 2) {
1612 if (
f->bits_per_raw_sample != 32) {
1614 const int cx = x >>
f->chroma_h_shift;
1615 const int cy = y >>
f->chroma_v_shift;
1622 if (
f->chroma_planes) {
1623 load_plane(
f, sc,
p->data[1] + ps*cx+cy*
p->linesize[1], chroma_width, chroma_height,
p->linesize[1], 1, 1);
1624 load_plane(
f, sc,
p->data[2] + ps*cx+cy*
p->linesize[2], chroma_width, chroma_height,
p->linesize[2], 2, 1);
1626 if (
f->transparency)
1631 }
else if (
f->use32bit) {
1651 const int cx = x >>
f->chroma_h_shift;
1652 const int cy = y >>
f->chroma_v_shift;
1656 if (
f->chroma_planes) {
1657 ret |=
encode_plane(
f, sc,
p->data[1] + ps*cx+cy*
p->linesize[1], chroma_width, chroma_height,
p->linesize[1], 1, 1, 1, ac);
1658 ret |=
encode_plane(
f, sc,
p->data[2] + ps*cx+cy*
p->linesize[2], chroma_width, chroma_height,
p->linesize[2], 1, 2, 1, ac);
1660 if (
f->transparency)
1665 }
else if (
f->bits_per_raw_sample == 32) {
1667 }
else if (
f->use32bit) {
1682 if (
f->version < 4) {
1700 int w = avctx->
width +
f->num_h_slices;
1701 int h = avctx->
height +
f->num_v_slices;
1702 size_t maxsize =
w*
h * (1 +
f->transparency);
1703 if (
f->chroma_planes)
1705 maxsize +=
f->slice_count * 800;
1706 if (
f->version > 3) {
1707 maxsize *=
f->bits_per_raw_sample + 1;
1709 maxsize +=
f->slice_count * 70000 * (1 + 2*
f->chroma_planes +
f->transparency);
1711 maxsize +=
f->slice_count * 2 * (avctx->
width + avctx->
height);
1712 maxsize *= 8*(2*
f->bits_per_raw_sample + 5);
1721 const AVFrame *pict,
int *got_packet)
1725 uint8_t keystate = 128;
1736 memset(
f->rc_stat, 0,
sizeof(
f->rc_stat));
1737 for (
i = 0;
i <
f->quant_table_count;
i++)
1738 memset(
f->rc_stat2[
i], 0,
f->context_count[
i] *
sizeof(*
f->rc_stat2[
i]));
1741 for (j = 0; j <
f->slice_count; j++) {
1743 for (
i = 0;
i < 256;
i++) {
1747 for (
i = 0;
i <
f->quant_table_count;
i++) {
1748 for (k = 0; k <
f->context_count[
i]; k++)
1749 for (m = 0; m < 32; m++) {
1750 f->rc_stat2[
i][k][m][0] += sc->
rc_stat2[
i][k][m][0];
1751 f->rc_stat2[
i][k][m][1] += sc->
rc_stat2[
i][k][m][1];
1756 for (j = 0; j < 256; j++) {
1757 snprintf(
p, end -
p,
"%" PRIu64
" %" PRIu64
" ",
1758 f->rc_stat[j][0],
f->rc_stat[j][1]);
1763 for (
i = 0;
i <
f->quant_table_count;
i++) {
1764 for (j = 0; j <
f->context_count[
i]; j++)
1765 for (m = 0; m < 32; m++) {
1766 snprintf(
p, end -
p,
"%" PRIu64
" %" PRIu64
" ",
1767 f->rc_stat2[
i][j][m][0],
f->rc_stat2[
i][j][m][1]);
1781 if (!
f->maxsize_warned) {
1782 av_log(avctx,
AV_LOG_WARNING,
"Cannot allocate worst case packet size, the encoding could fail\n");
1783 f->maxsize_warned++;
1794 f->cur_enc_frame = pict;
1808 for (
i = 1;
i < 256;
i++) {
1809 c->one_state[
i] =
f->state_transition[
i];
1810 c->zero_state[256 -
i] = 256 -
c->one_state[
i];
1814 for (
i = 0;
i <
f->slice_count;
i++) {
1827 f->slice_count,
sizeof(*
f->slices));
1830 for (
i = 0;
i <
f->slice_count;
i++) {
1833 if (
i > 0 ||
f->version > 2) {
1837 AV_WB24(buf_p + bytes, bytes);
1853 f->picture_number++;
1865 for (
int j = 0; j <
s->max_slice_count; j++) {
1868 for(
int p = 0;
p<4;
p++) {
1880 #define OFFSET(x) offsetof(FFV1Context, x)
1881 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1885 { .i64 = 0 }, -2, 2,
VE, .unit =
"coder" },
1892 {
"ac",
"Range with custom table (the ac option exists for compatibility and is deprecated)", 0,
AV_OPT_TYPE_CONST,
1893 { .i64 = 1 }, INT_MIN, INT_MAX,
VE, .unit =
"coder" },
1895 { .i64 = 0 }, 0, 1,
VE },
1897 { .i64 = -1 }, -1, 2,
VE , .unit =
"qtable"},
1901 { .i64 =
QTABLE_8BIT }, INT_MIN, INT_MAX,
VE, .unit =
"qtable" },
1904 {
"remap_mode",
"Remap Mode",
OFFSET(remap_mode),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 2,
VE, .unit =
"remap_mode" },
1906 { .i64 = -1 }, INT_MIN, INT_MAX,
VE, .unit =
"remap_mode" },
1908 { .i64 = 0 }, INT_MIN, INT_MAX,
VE, .unit =
"remap_mode" },
1910 { .i64 = 1 }, INT_MIN, INT_MAX,
VE, .unit =
"remap_mode" },
1912 { .i64 = 2 }, INT_MIN, INT_MAX,
VE, .unit =
"remap_mode" },
1913 {
"remap_optimizer",
"Remap Optimizer",
OFFSET(remap_optimizer),
AV_OPT_TYPE_INT, { .i64 = 3 }, 0, 5,
VE, .unit =
"remap_optimizer" },