49 for (i = 1; i < 256; i++)
50 l2tab[i] =
log2(i / 256.0);
52 for (i = 0; i < 256; i++) {
56 for (j = 0; j < 256; j++)
57 best_len[j] = 1 << 30;
59 for (j =
FFMAX(i - 10, 1); j <
FFMIN(i + 11, 256); j++) {
60 double occ[256] = { 0 };
63 for (k = 0; k < 256; k++) {
64 double newocc[256] = { 0 };
65 for (m = 1; m < 256; m++)
67 len -= occ[m] * (p * l2tab[m] +
68 (1 - p) * l2tab[256 - m]);
70 if (len < best_len[k]) {
74 for (m = 0; m < 256; m++)
76 newocc[one_state[m]] += occ[m] * p;
77 newocc[256 - one_state[256 - m]] += occ[m] * (1 - p);
79 memcpy(occ, newocc,
sizeof(occ));
93 #define put_rac(C, S, B) \
97 rc_stat2[(S) - state][B]++; \
107 for (i = 0; i < e; i++)
111 for (i = e - 1; i >= 0; i--)
112 put_rac(c, state + 22 + i, (a >> i) & 1);
115 put_rac(c, state + 11 + e, v < 0);
117 for (i = 0; i < e; i++)
121 for (i = e - 1; i >= 0; i--)
125 put_rac(c, state + 11 + 10, v < 0);
134 int v,
int is_signed)
147 while (i < state->error_sum) {
155 if (k == 0 && 2 * state->
drift <= -state->
count)
160 code = v ^ ((2 * state->
drift + state->
count) >> 31);
163 av_dlog(
NULL,
"v:%d/%d bias:%d error:%d drift:%d count:%d k:%d\n", v, code,
172 int plane_index,
int bits)
193 for (x = 0; x < w; x++) {
196 context =
get_context(p, sample[0] + x, sample[1] + x, sample[2] + x);
197 diff = sample[0][x] -
predict(sample[0] + x, sample[1] + x);
204 diff =
fold(diff, bits);
237 av_dlog(s->
avctx,
"count:%d index:%d, mode:%d, x:%d pos:%d\n",
238 run_count, run_index, run_mode, x,
261 int stride,
int plane_index)
270 for (y = 0; y < h; y++) {
271 for (i = 0; i < ring_size; i++)
272 sample[i] = s->
sample_buffer + (w + 6) * ((h + i - y) % ring_size) + 3;
274 sample[0][-1] = sample[1][0];
275 sample[1][w] = sample[1][w - 1];
278 for (x = 0; x < w; x++)
279 sample[0][x] = src[x + stride * y];
283 for (x = 0; x < w; x++)
284 sample[0][x] = ((uint16_t *)(src + stride * y))[x];
286 for (x = 0; x < w; x++)
306 int offset = 1 <<
bits;
313 for (y = 0; y < h; y++) {
314 for (i = 0; i < ring_size; i++)
318 ((h + i - y) % ring_size) * (w + 6) + 3;
320 for (x = 0; x < w; x++) {
323 unsigned v = *((uint32_t *)(src[0] + x * 4 + stride[0] * y));
326 r = (v >> 16) & 0xFF;
329 b = *((uint16_t *)(src[0] + x * 2 + stride[0] * y));
330 g = *((uint16_t *)(src[1] + x * 2 + stride[1] * y));
331 r = *((uint16_t *)(src[2] + x * 2 + stride[2] * y));
346 sample[p][0][-1] = sample[p][1][0];
347 sample[p][1][w] = sample[p][1][w - 1];
351 encode_line(s, w, sample[p], (p + 1) / 2, bits + 1);
362 memset(state, 128,
sizeof(state));
364 for (i = 1; i < 128; i++)
365 if (quant_table[i] != quant_table[i - 1]) {
376 for (i = 0; i < 5; i++)
386 memset(state, 128,
sizeof(state));
392 for (i = 1; i < 256; i++)
435 memset(state2, 128,
sizeof(state2));
436 memset(state, 128,
sizeof(state));
439 (11 * 11 * 5 * 5 * 5 + 11 * 11 * 11) * 32;
453 for (i = 1; i < 256; i++)
502 int i, i2, changed, print = 0;
506 for (i = 12; i < 244; i++) {
507 for (i2 = i + 1; i2 < 245 && i2 < i + 4; i2++) {
509 #define COST(old, new) \
510 s->rc_stat[old][0] * -log2((256 - (new)) / 256.0) + \
511 s->rc_stat[old][1] * -log2((new) / 256.0)
513 #define COST2(old, new) \
514 COST(old, new) + COST(256 - (old), 256 - (new))
518 if (sizeX < size0 && i != 128 && i2 != 128) {
520 FFSWAP(
int, stt[i], stt[i2]);
524 FFSWAP(
int, stt[256 - i], stt[256 - i2]);
528 for (j = 1; j < 256; j++) {
531 else if (stt[j] == i2)
534 if (stt[256 - j] == 256 - i)
535 stt[256 - j] = 256 - i2;
536 else if (stt[256 - j] == 256 - i2)
537 stt[256 - j] = 256 - i;
573 if (avctx->
level == 3) {
584 "Version %d requested, please set -strict experimental in "
585 "order to enable it\n",
620 "bits_per_raw_sample > 8, forcing coder 1\n");
626 "bits_per_raw_sample of more than 8 needs -coder 1 currently\n");
673 "Storing alpha plane, this will require a recent FFV1 decoder to playback!\n");
677 "Invalid context model %d, valid values are 0 and 1\n",
683 for (i = 1; i < 256; i++)
686 for (i = 0; i < 256; i++) {
750 for (j = 0; j < 256; j++)
751 for (i = 0; i < 2; i++) {
752 s->
rc_stat[j][i] = strtol(p, &next, 0);
755 "2Pass file invalid at %d %d [%s]\n", j, i, p);
762 for (k = 0; k < 32; k++)
763 for (m = 0; m < 2; m++) {
764 s->
rc_stat2[i][j][k][m] = strtol(p, &next, 0);
767 "2Pass file invalid at %d %d %d %d [%s]\n",
774 gob_count = strtol(p, &next, 0);
775 if (next == p || gob_count <= 0) {
780 while (*p ==
'\n' || *p ==
' ')
791 for (k = 0; k < 32; k++) {
794 p = 256.0 * s->
rc_stat2[i][j][k][1] /
798 best_state[av_clip(
round(p), 1, 255)][av_clip((s->
rc_stat2[i][j][k][0] +
813 "Unsupported number %d of slices requested, please specify a "
814 "supported number with -slices (ex:4,6,9,12,16, ...)\n",
826 #define STATS_OUT_SIZE 1024 * 1024 * 6
848 memset(state, 128,
sizeof(state));
895 if (f->colorspace == 0) {
896 const int chroma_width = -((-
width) >> f->chroma_h_shift);
897 const int chroma_height = -((-
height) >> f->chroma_v_shift);
898 const int cx = x >> f->chroma_h_shift;
899 const int cy = y >> f->chroma_v_shift;
901 encode_plane(fs, p->data[0] + ps * x + y * p->linesize[0],
904 if (f->chroma_planes) {
905 encode_plane(fs, p->data[1] + ps * cx + cy * p->linesize[1],
906 chroma_width, chroma_height, p->linesize[1], 1);
907 encode_plane(fs, p->data[2] + ps * cx + cy * p->linesize[2],
908 chroma_width, chroma_height, p->linesize[2], 1);
910 if (fs->transparency)
912 height, p->linesize[3], 2);
914 uint8_t *planes[3] = { p->data[0] + ps * x + y * p->linesize[0],
915 p->data[1] + ps * x + y * p->linesize[1],
916 p->data[2] + ps * x + y * p->linesize[2] };
925 const AVFrame *pict,
int *got_packet)
936 ((8 * 2 + 1 + 1) * 4) / 8 +
960 for (i = 1; i < 256; i++) {
1017 for (i = 0; i < 256; i++) {
1023 for (m = 0; m < 32; m++) {
1030 for (j = 0; j < 256; j++) {
1031 snprintf(p, end - p,
"%" PRIu64
" %" PRIu64
" ",
1035 snprintf(p, end - p,
"\n");
1039 for (m = 0; m < 32; m++) {
1040 snprintf(p, end - p,
"%" PRIu64
" %" PRIu64
" ",
1045 snprintf(p, end - p,
"%d\n", f->
gob_count);
1057 #define OFFSET(x) offsetof(FFV1Context, x)
1058 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1061 { .i64 = -1 }, -1, 1,
VE },
1065 static const AVClass class = {
1101 .priv_class = &
class,