Libav
ffv1enc.c
Go to the documentation of this file.
1 /*
2  * FFV1 encoder for libavcodec
3  *
4  * Copyright (c) 2003-2012 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of Libav.
7  *
8  * Libav is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * Libav is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
28 #include "libavutil/attributes.h"
29 #include "libavutil/avassert.h"
30 #include "libavutil/pixdesc.h"
31 #include "libavutil/crc.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/imgutils.h"
34 #include "avcodec.h"
35 #include "internal.h"
36 #include "get_bits.h"
37 #include "put_bits.h"
38 #include "rangecoder.h"
39 #include "golomb.h"
40 #include "mathops.h"
41 #include "ffv1.h"
42 
43 static void find_best_state(uint8_t best_state[256][256],
44  const uint8_t one_state[256])
45 {
46  int i, j, k, m;
47  double l2tab[256];
48 
49  for (i = 1; i < 256; i++)
50  l2tab[i] = log2(i / 256.0);
51 
52  for (i = 0; i < 256; i++) {
53  double best_len[256];
54  double p = i / 256.0;
55 
56  for (j = 0; j < 256; j++)
57  best_len[j] = 1 << 30;
58 
59  for (j = FFMAX(i - 10, 1); j < FFMIN(i + 11, 256); j++) {
60  double occ[256] = { 0 };
61  double len = 0;
62  occ[j] = 1.0;
63  for (k = 0; k < 256; k++) {
64  double newocc[256] = { 0 };
65  for (m = 1; m < 256; m++)
66  if (occ[m]) {
67  len -= occ[m] * (p * l2tab[m] +
68  (1 - p) * l2tab[256 - m]);
69  }
70  if (len < best_len[k]) {
71  best_len[k] = len;
72  best_state[i][k] = j;
73  }
74  for (m = 1; m < 256; m++)
75  if (occ[m]) {
76  newocc[one_state[m]] += occ[m] * p;
77  newocc[256 - one_state[256 - m]] += occ[m] * (1 - p);
78  }
79  memcpy(occ, newocc, sizeof(occ));
80  }
81  }
82  }
83 }
84 
86  uint8_t *state, int v,
87  int is_signed,
88  uint64_t rc_stat[256][2],
89  uint64_t rc_stat2[32][2])
90 {
91  int i;
92 
93 #define put_rac(C, S, B) \
94  do { \
95  if (rc_stat) { \
96  rc_stat[*(S)][B]++; \
97  rc_stat2[(S) - state][B]++; \
98  } \
99  put_rac(C, S, B); \
100  } while (0)
101 
102  if (v) {
103  const int a = FFABS(v);
104  const int e = av_log2(a);
105  put_rac(c, state + 0, 0);
106  if (e <= 9) {
107  for (i = 0; i < e; i++)
108  put_rac(c, state + 1 + i, 1); // 1..10
109  put_rac(c, state + 1 + i, 0);
110 
111  for (i = e - 1; i >= 0; i--)
112  put_rac(c, state + 22 + i, (a >> i) & 1); // 22..31
113 
114  if (is_signed)
115  put_rac(c, state + 11 + e, v < 0); // 11..21
116  } else {
117  for (i = 0; i < e; i++)
118  put_rac(c, state + 1 + FFMIN(i, 9), 1); // 1..10
119  put_rac(c, state + 1 + 9, 0);
120 
121  for (i = e - 1; i >= 0; i--)
122  put_rac(c, state + 22 + FFMIN(i, 9), (a >> i) & 1); // 22..31
123 
124  if (is_signed)
125  put_rac(c, state + 11 + 10, v < 0); // 11..21
126  }
127  } else {
128  put_rac(c, state + 0, 1);
129  }
130 #undef put_rac
131 }
132 
134  int v, int is_signed)
135 {
136  put_symbol_inline(c, state, v, is_signed, NULL, NULL);
137 }
138 
139 static inline void put_vlc_symbol(PutBitContext *pb, VlcState *const state,
140  int v, int bits)
141 {
142  int i, k, code;
143  v = fold(v - state->bias, bits);
144 
145  i = state->count;
146  k = 0;
147  while (i < state->error_sum) { // FIXME: optimize
148  k++;
149  i += i;
150  }
151 
152  assert(k <= 13);
153 
154 #if 0 // JPEG LS
155  if (k == 0 && 2 * state->drift <= -state->count)
156  code = v ^ (-1);
157  else
158  code = v;
159 #else
160  code = v ^ ((2 * state->drift + state->count) >> 31);
161 #endif
162 
163  av_dlog(NULL, "v:%d/%d bias:%d error:%d drift:%d count:%d k:%d\n", v, code,
164  state->bias, state->error_sum, state->drift, state->count, k);
165  set_sr_golomb(pb, code, k, 12, bits);
166 
167  update_vlc_state(state, v);
168 }
169 
171  int16_t *sample[3],
172  int plane_index, int bits)
173 {
174  PlaneContext *const p = &s->plane[plane_index];
175  RangeCoder *const c = &s->c;
176  int x;
177  int run_index = s->run_index;
178  int run_count = 0;
179  int run_mode = 0;
180 
181  if (s->ac) {
182  if (c->bytestream_end - c->bytestream < w * 20) {
183  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
184  return AVERROR_INVALIDDATA;
185  }
186  } else {
187  if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < w * 4) {
188  av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
189  return AVERROR_INVALIDDATA;
190  }
191  }
192 
193  for (x = 0; x < w; x++) {
194  int diff, context;
195 
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);
198 
199  if (context < 0) {
200  context = -context;
201  diff = -diff;
202  }
203 
204  diff = fold(diff, bits);
205 
206  if (s->ac) {
207  if (s->flags & CODEC_FLAG_PASS1) {
208  put_symbol_inline(c, p->state[context], diff, 1, s->rc_stat,
209  s->rc_stat2[p->quant_table_index][context]);
210  } else {
211  put_symbol_inline(c, p->state[context], diff, 1, NULL, NULL);
212  }
213  } else {
214  if (context == 0)
215  run_mode = 1;
216 
217  if (run_mode) {
218  if (diff) {
219  while (run_count >= 1 << ff_log2_run[run_index]) {
220  run_count -= 1 << ff_log2_run[run_index];
221  run_index++;
222  put_bits(&s->pb, 1, 1);
223  }
224 
225  put_bits(&s->pb, 1 + ff_log2_run[run_index], run_count);
226  if (run_index)
227  run_index--;
228  run_count = 0;
229  run_mode = 0;
230  if (diff > 0)
231  diff--;
232  } else {
233  run_count++;
234  }
235  }
236 
237  av_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
238  run_count, run_index, run_mode, x,
239  (int)put_bits_count(&s->pb));
240 
241  if (run_mode == 0)
242  put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
243  }
244  }
245  if (run_mode) {
246  while (run_count >= 1 << ff_log2_run[run_index]) {
247  run_count -= 1 << ff_log2_run[run_index];
248  run_index++;
249  put_bits(&s->pb, 1, 1);
250  }
251 
252  if (run_count)
253  put_bits(&s->pb, 1, 1);
254  }
255  s->run_index = run_index;
256 
257  return 0;
258 }
259 
260 static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h,
261  int stride, int plane_index)
262 {
263  int x, y, i;
264  const int ring_size = s->avctx->context_model ? 3 : 2;
265  int16_t *sample[3];
266  s->run_index = 0;
267 
268  memset(s->sample_buffer, 0, ring_size * (w + 6) * sizeof(*s->sample_buffer));
269 
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;
273 
274  sample[0][-1] = sample[1][0];
275  sample[1][w] = sample[1][w - 1];
276 // { START_TIMER
277  if (s->bits_per_raw_sample <= 8) {
278  for (x = 0; x < w; x++)
279  sample[0][x] = src[x + stride * y];
280  encode_line(s, w, sample, plane_index, 8);
281  } else {
282  if (s->packed_at_lsb) {
283  for (x = 0; x < w; x++)
284  sample[0][x] = ((uint16_t *)(src + stride * y))[x];
285  } else {
286  for (x = 0; x < w; x++)
287  sample[0][x] =
288  ((uint16_t *)(src + stride * y))[x] >> (16 - s->bits_per_raw_sample);
289  }
290  encode_line(s, w, sample, plane_index, s->bits_per_raw_sample);
291  }
292 // STOP_TIMER("encode line") }
293  }
294 }
295 
296 static void encode_rgb_frame(FFV1Context *s, uint8_t *src[3], int w, int h,
297  int stride[3])
298 {
299  int x, y, p, i;
300  const int ring_size = s->avctx->context_model ? 3 : 2;
301  int16_t *sample[MAX_PLANES][3];
302  int lbd = s->avctx->bits_per_raw_sample <= 8;
303  int bits = s->avctx->bits_per_raw_sample > 0
305  : 8;
306  int offset = 1 << bits;
307 
308  s->run_index = 0;
309 
310  memset(s->sample_buffer, 0, ring_size * MAX_PLANES *
311  (w + 6) * sizeof(*s->sample_buffer));
312 
313  for (y = 0; y < h; y++) {
314  for (i = 0; i < ring_size; i++)
315  for (p = 0; p < MAX_PLANES; p++)
316  sample[p][i] = s->sample_buffer + p * ring_size *
317  (w + 6) +
318  ((h + i - y) % ring_size) * (w + 6) + 3;
319 
320  for (x = 0; x < w; x++) {
321  int b, g, r, av_uninit(a);
322  if (lbd) {
323  unsigned v = *((uint32_t *)(src[0] + x * 4 + stride[0] * y));
324  b = v & 0xFF;
325  g = (v >> 8) & 0xFF;
326  r = (v >> 16) & 0xFF;
327  a = v >> 24;
328  } else {
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));
332  }
333 
334  b -= g;
335  r -= g;
336  g += (b + r) >> 2;
337  b += offset;
338  r += offset;
339 
340  sample[0][0][x] = g;
341  sample[1][0][x] = b;
342  sample[2][0][x] = r;
343  sample[3][0][x] = a;
344  }
345  for (p = 0; p < 3 + s->transparency; p++) {
346  sample[p][0][-1] = sample[p][1][0];
347  sample[p][1][w] = sample[p][1][w - 1];
348  if (lbd)
349  encode_line(s, w, sample[p], (p + 1) / 2, 9);
350  else
351  encode_line(s, w, sample[p], (p + 1) / 2, bits + 1);
352  }
353  }
354 }
355 
356 
357 static void write_quant_table(RangeCoder *c, int16_t *quant_table)
358 {
359  int last = 0;
360  int i;
362  memset(state, 128, sizeof(state));
363 
364  for (i = 1; i < 128; i++)
365  if (quant_table[i] != quant_table[i - 1]) {
366  put_symbol(c, state, i - last - 1, 0);
367  last = i;
368  }
369  put_symbol(c, state, i - last - 1, 0);
370 }
371 
373  int16_t quant_table[MAX_CONTEXT_INPUTS][256])
374 {
375  int i;
376  for (i = 0; i < 5; i++)
378 }
379 
380 static void write_header(FFV1Context *f)
381 {
383  int i, j;
384  RangeCoder *const c = &f->slice_context[0]->c;
385 
386  memset(state, 128, sizeof(state));
387 
388  if (f->version < 2) {
389  put_symbol(c, state, f->version, 0);
390  put_symbol(c, state, f->ac, 0);
391  if (f->ac > 1) {
392  for (i = 1; i < 256; i++)
393  put_symbol(c, state,
394  f->state_transition[i] - c->one_state[i], 1);
395  }
396  put_symbol(c, state, f->colorspace, 0); // YUV cs type
397  if (f->version > 0)
398  put_symbol(c, state, f->bits_per_raw_sample, 0);
399  put_rac(c, state, f->chroma_planes);
400  put_symbol(c, state, f->chroma_h_shift, 0);
401  put_symbol(c, state, f->chroma_v_shift, 0);
402  put_rac(c, state, f->transparency);
403 
405  } else if (f->version < 3) {
406  put_symbol(c, state, f->slice_count, 0);
407  for (i = 0; i < f->slice_count; i++) {
408  FFV1Context *fs = f->slice_context[i];
409  put_symbol(c, state,
410  (fs->slice_x + 1) * f->num_h_slices / f->width, 0);
411  put_symbol(c, state,
412  (fs->slice_y + 1) * f->num_v_slices / f->height, 0);
413  put_symbol(c, state,
414  (fs->slice_width + 1) * f->num_h_slices / f->width - 1,
415  0);
416  put_symbol(c, state,
417  (fs->slice_height + 1) * f->num_v_slices / f->height - 1,
418  0);
419  for (j = 0; j < f->plane_count; j++) {
420  put_symbol(c, state, f->plane[j].quant_table_index, 0);
422  }
423  }
424  }
425 }
426 
428 {
429  RangeCoder *const c = &f->c;
431  int i, j, k;
432  uint8_t state2[32][CONTEXT_SIZE];
433  unsigned v;
434 
435  memset(state2, 128, sizeof(state2));
436  memset(state, 128, sizeof(state));
437 
438  f->avctx->extradata_size = 10000 + 4 +
439  (11 * 11 * 5 * 5 * 5 + 11 * 11 * 11) * 32;
442  ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
443 
444  put_symbol(c, state, f->version, 0);
445  if (f->version > 2) {
446  if (f->version == 3)
447  f->minor_version = 2;
448  put_symbol(c, state, f->minor_version, 0);
449  }
450 
451  put_symbol(c, state, f->ac, 0);
452  if (f->ac > 1)
453  for (i = 1; i < 256; i++)
454  put_symbol(c, state, f->state_transition[i] - c->one_state[i], 1);
455 
456  put_symbol(c, state, f->colorspace, 0); // YUV cs type
457  put_symbol(c, state, f->bits_per_raw_sample, 0);
458  put_rac(c, state, f->chroma_planes);
459  put_symbol(c, state, f->chroma_h_shift, 0);
460  put_symbol(c, state, f->chroma_v_shift, 0);
461  put_rac(c, state, f->transparency);
462  put_symbol(c, state, f->num_h_slices - 1, 0);
463  put_symbol(c, state, f->num_v_slices - 1, 0);
464 
465  put_symbol(c, state, f->quant_table_count, 0);
466  for (i = 0; i < f->quant_table_count; i++)
468 
469  for (i = 0; i < f->quant_table_count; i++) {
470  for (j = 0; j < f->context_count[i] * CONTEXT_SIZE; j++)
471  if (f->initial_states[i] && f->initial_states[i][0][j] != 128)
472  break;
473  if (j < f->context_count[i] * CONTEXT_SIZE) {
474  put_rac(c, state, 1);
475  for (j = 0; j < f->context_count[i]; j++)
476  for (k = 0; k < CONTEXT_SIZE; k++) {
477  int pred = j ? f->initial_states[i][j - 1][k] : 128;
478  put_symbol(c, state2[k],
479  (int8_t)(f->initial_states[i][j][k] - pred), 1);
480  }
481  } else {
482  put_rac(c, state, 0);
483  }
484  }
485 
486  if (f->version > 2) {
487  put_symbol(c, state, f->ec, 0);
488  }
489 
491 
495  f->avctx->extradata_size += 4;
496 
497  return 0;
498 }
499 
500 static int sort_stt(FFV1Context *s, uint8_t stt[256])
501 {
502  int i, i2, changed, print = 0;
503 
504  do {
505  changed = 0;
506  for (i = 12; i < 244; i++) {
507  for (i2 = i + 1; i2 < 245 && i2 < i + 4; i2++) {
508 
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)
512 
513 #define COST2(old, new) \
514  COST(old, new) + COST(256 - (old), 256 - (new))
515 
516  double size0 = COST2(i, i) + COST2(i2, i2);
517  double sizeX = COST2(i, i2) + COST2(i2, i);
518  if (sizeX < size0 && i != 128 && i2 != 128) {
519  int j;
520  FFSWAP(int, stt[i], stt[i2]);
521  FFSWAP(int, s->rc_stat[i][0], s->rc_stat[i2][0]);
522  FFSWAP(int, s->rc_stat[i][1], s->rc_stat[i2][1]);
523  if (i != 256 - i2) {
524  FFSWAP(int, stt[256 - i], stt[256 - i2]);
525  FFSWAP(int, s->rc_stat[256 - i][0], s->rc_stat[256 - i2][0]);
526  FFSWAP(int, s->rc_stat[256 - i][1], s->rc_stat[256 - i2][1]);
527  }
528  for (j = 1; j < 256; j++) {
529  if (stt[j] == i)
530  stt[j] = i2;
531  else if (stt[j] == i2)
532  stt[j] = i;
533  if (i != 256 - 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;
538  }
539  }
540  print = changed = 1;
541  }
542  }
543  }
544  } while (changed);
545  return print;
546 }
547 
549 {
550  int i, ret;
551  for (i = 0; i < f->slice_count; i++) {
552  FFV1Context *fs = f->slice_context[i];
553  if ((ret = ffv1_init_slice_state(f, fs)) < 0)
554  return AVERROR(ENOMEM);
555  }
556  return 0;
557 }
558 
560 {
561  FFV1Context *s = avctx->priv_data;
562  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
563  int i, j, k, m, ret;
564 
565  ffv1_common_init(avctx);
566 
567  s->version = 0;
568 
569  if ((avctx->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2)) ||
570  avctx->slices > 1)
571  s->version = FFMAX(s->version, 2);
572 
573  if (avctx->level == 3) {
574  s->version = 3;
575  }
576 
577  if (s->ec < 0) {
578  s->ec = (s->version >= 3);
579  }
580 
581  if (s->version >= 2 &&
583  av_log(avctx, AV_LOG_ERROR,
584  "Version %d requested, please set -strict experimental in "
585  "order to enable it\n",
586  s->version);
587  return AVERROR(ENOSYS);
588  }
589 
590  s->ac = avctx->coder_type > 0 ? 2 : 0;
591 
592  s->plane_count = 3;
593  switch (avctx->pix_fmt) {
594  case AV_PIX_FMT_YUV444P9:
595  case AV_PIX_FMT_YUV422P9:
596  case AV_PIX_FMT_YUV420P9:
597  if (!avctx->bits_per_raw_sample)
598  s->bits_per_raw_sample = 9;
602  s->packed_at_lsb = 1;
603  if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
604  s->bits_per_raw_sample = 10;
605  case AV_PIX_FMT_GRAY16:
609  if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample) {
610  s->bits_per_raw_sample = 16;
611  } else if (!s->bits_per_raw_sample) {
613  }
614  if (s->bits_per_raw_sample <= 8) {
615  av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample invalid\n");
616  return AVERROR_INVALIDDATA;
617  }
618  if (!s->ac && avctx->coder_type == -1) {
619  av_log(avctx, AV_LOG_INFO,
620  "bits_per_raw_sample > 8, forcing coder 1\n");
621  s->ac = 2;
622  }
623  if (!s->ac) {
624  av_log(
625  avctx, AV_LOG_ERROR,
626  "bits_per_raw_sample of more than 8 needs -coder 1 currently\n");
627  return AVERROR_INVALIDDATA;
628  }
629  s->version = FFMAX(s->version, 1);
630  case AV_PIX_FMT_GRAY8:
631  case AV_PIX_FMT_YUV444P:
632  case AV_PIX_FMT_YUV440P:
633  case AV_PIX_FMT_YUV422P:
634  case AV_PIX_FMT_YUV420P:
635  case AV_PIX_FMT_YUV411P:
636  case AV_PIX_FMT_YUV410P:
637  s->chroma_planes = desc->nb_components < 3 ? 0 : 1;
638  s->colorspace = 0;
639  break;
640  case AV_PIX_FMT_YUVA444P:
641  case AV_PIX_FMT_YUVA422P:
642  case AV_PIX_FMT_YUVA420P:
643  s->chroma_planes = 1;
644  s->colorspace = 0;
645  s->transparency = 1;
646  break;
647  case AV_PIX_FMT_RGB32:
648  s->colorspace = 1;
649  s->transparency = 1;
650  break;
651  case AV_PIX_FMT_GBRP9:
652  if (!avctx->bits_per_raw_sample)
653  s->bits_per_raw_sample = 9;
654  case AV_PIX_FMT_GBRP10:
655  if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
656  s->bits_per_raw_sample = 10;
657  case AV_PIX_FMT_GBRP16:
658  if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
659  s->bits_per_raw_sample = 16;
660  else if (!s->bits_per_raw_sample)
662  s->colorspace = 1;
663  s->chroma_planes = 1;
664  s->version = FFMAX(s->version, 1);
665  break;
666  default:
667  av_log(avctx, AV_LOG_ERROR, "format not supported\n");
668  return AVERROR_INVALIDDATA;
669  }
670  if (s->transparency) {
671  av_log(
672  avctx, AV_LOG_WARNING,
673  "Storing alpha plane, this will require a recent FFV1 decoder to playback!\n");
674  }
675  if (avctx->context_model > 1U) {
676  av_log(avctx, AV_LOG_ERROR,
677  "Invalid context model %d, valid values are 0 and 1\n",
678  avctx->context_model);
679  return AVERROR(EINVAL);
680  }
681 
682  if (s->ac > 1)
683  for (i = 1; i < 256; i++)
685 
686  for (i = 0; i < 256; i++) {
687  s->quant_table_count = 2;
688  if (s->bits_per_raw_sample <= 8) {
689  s->quant_tables[0][0][i] = ffv1_quant11[i];
690  s->quant_tables[0][1][i] = ffv1_quant11[i] * 11;
691  s->quant_tables[0][2][i] = ffv1_quant11[i] * 11 * 11;
692  s->quant_tables[1][0][i] = ffv1_quant11[i];
693  s->quant_tables[1][1][i] = ffv1_quant11[i] * 11;
694  s->quant_tables[1][2][i] = ffv1_quant5[i] * 11 * 11;
695  s->quant_tables[1][3][i] = ffv1_quant5[i] * 5 * 11 * 11;
696  s->quant_tables[1][4][i] = ffv1_quant5[i] * 5 * 5 * 11 * 11;
697  } else {
698  s->quant_tables[0][0][i] = ffv1_quant9_10bit[i];
699  s->quant_tables[0][1][i] = ffv1_quant9_10bit[i] * 11;
700  s->quant_tables[0][2][i] = ffv1_quant9_10bit[i] * 11 * 11;
701  s->quant_tables[1][0][i] = ffv1_quant9_10bit[i];
702  s->quant_tables[1][1][i] = ffv1_quant9_10bit[i] * 11;
703  s->quant_tables[1][2][i] = ffv1_quant5_10bit[i] * 11 * 11;
704  s->quant_tables[1][3][i] = ffv1_quant5_10bit[i] * 5 * 11 * 11;
705  s->quant_tables[1][4][i] = ffv1_quant5_10bit[i] * 5 * 5 * 11 * 11;
706  }
707  }
708  s->context_count[0] = (11 * 11 * 11 + 1) / 2;
709  s->context_count[1] = (11 * 11 * 5 * 5 * 5 + 1) / 2;
710  memcpy(s->quant_table, s->quant_tables[avctx->context_model],
711  sizeof(s->quant_table));
712 
713  for (i = 0; i < s->plane_count; i++) {
714  PlaneContext *const p = &s->plane[i];
715 
716  memcpy(p->quant_table, s->quant_table, sizeof(p->quant_table));
717  p->quant_table_index = avctx->context_model;
719  }
720 
721  if ((ret = ffv1_allocate_initial_states(s)) < 0)
722  return ret;
723 
724  avctx->coded_frame = av_frame_alloc();
725  if (!avctx->coded_frame)
726  return AVERROR(ENOMEM);
727 
729 
730  if (!s->transparency)
731  s->plane_count = 2;
732 
734  &s->chroma_v_shift);
735 
736  s->picture_number = 0;
737 
738  if (avctx->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2)) {
739  for (i = 0; i < s->quant_table_count; i++) {
740  s->rc_stat2[i] = av_mallocz(s->context_count[i] *
741  sizeof(*s->rc_stat2[i]));
742  if (!s->rc_stat2[i])
743  return AVERROR(ENOMEM);
744  }
745  }
746  if (avctx->stats_in) {
747  char *p = avctx->stats_in;
748  uint8_t best_state[256][256];
749  int gob_count = 0;
750  char *next;
751 
752  av_assert0(s->version >= 2);
753 
754  for (;; ) {
755  for (j = 0; j < 256; j++)
756  for (i = 0; i < 2; i++) {
757  s->rc_stat[j][i] = strtol(p, &next, 0);
758  if (next == p) {
759  av_log(avctx, AV_LOG_ERROR,
760  "2Pass file invalid at %d %d [%s]\n", j, i, p);
761  return AVERROR_INVALIDDATA;
762  }
763  p = next;
764  }
765  for (i = 0; i < s->quant_table_count; i++)
766  for (j = 0; j < s->context_count[i]; j++) {
767  for (k = 0; k < 32; k++)
768  for (m = 0; m < 2; m++) {
769  s->rc_stat2[i][j][k][m] = strtol(p, &next, 0);
770  if (next == p) {
771  av_log(avctx, AV_LOG_ERROR,
772  "2Pass file invalid at %d %d %d %d [%s]\n",
773  i, j, k, m, p);
774  return AVERROR_INVALIDDATA;
775  }
776  p = next;
777  }
778  }
779  gob_count = strtol(p, &next, 0);
780  if (next == p || gob_count <= 0) {
781  av_log(avctx, AV_LOG_ERROR, "2Pass file invalid\n");
782  return AVERROR_INVALIDDATA;
783  }
784  p = next;
785  while (*p == '\n' || *p == ' ')
786  p++;
787  if (p[0] == 0)
788  break;
789  }
790  sort_stt(s, s->state_transition);
791 
792  find_best_state(best_state, s->state_transition);
793 
794  for (i = 0; i < s->quant_table_count; i++) {
795  for (j = 0; j < s->context_count[i]; j++)
796  for (k = 0; k < 32; k++) {
797  double p = 128;
798  if (s->rc_stat2[i][j][k][0] + s->rc_stat2[i][j][k][1]) {
799  p = 256.0 * s->rc_stat2[i][j][k][1] /
800  (s->rc_stat2[i][j][k][0] + s->rc_stat2[i][j][k][1]);
801  }
802  s->initial_states[i][j][k] =
803  best_state[av_clip(round(p), 1, 255)][av_clip((s->rc_stat2[i][j][k][0] +
804  s->rc_stat2[i][j][k][1]) /
805  gob_count, 0, 255)];
806  }
807  }
808  }
809 
810  if (s->version > 1) {
811  for (s->num_v_slices = 2; s->num_v_slices < 9; s->num_v_slices++)
812  for (s->num_h_slices = s->num_v_slices;
813  s->num_h_slices < 2 * s->num_v_slices; s->num_h_slices++)
814  if (avctx->slices == s->num_h_slices * s->num_v_slices &&
815  avctx->slices <= 64 || !avctx->slices)
816  goto slices_ok;
817  av_log(avctx, AV_LOG_ERROR,
818  "Unsupported number %d of slices requested, please specify a "
819  "supported number with -slices (ex:4,6,9,12,16, ...)\n",
820  avctx->slices);
821  return AVERROR(ENOSYS);
822 slices_ok:
823  write_extradata(s);
824  }
825 
826  if ((ret = ffv1_init_slice_contexts(s)) < 0)
827  return ret;
828  if ((ret = init_slices_state(s)) < 0)
829  return ret;
830 
831 #define STATS_OUT_SIZE 1024 * 1024 * 6
832  if (avctx->flags & CODEC_FLAG_PASS1) {
834  for (i = 0; i < s->quant_table_count; i++)
835  for (j = 0; j < s->slice_count; j++) {
836  FFV1Context *sf = s->slice_context[j];
837  av_assert0(!sf->rc_stat2[i]);
838  sf->rc_stat2[i] = av_mallocz(s->context_count[i] *
839  sizeof(*sf->rc_stat2[i]));
840  if (!sf->rc_stat2[i])
841  return AVERROR(ENOMEM);
842  }
843  }
844 
845  return 0;
846 }
847 
849 {
850  RangeCoder *c = &fs->c;
852  int j;
853  memset(state, 128, sizeof(state));
854 
855  put_symbol(c, state, (fs->slice_x + 1) * f->num_h_slices / f->width, 0);
856  put_symbol(c, state, (fs->slice_y + 1) * f->num_v_slices / f->height, 0);
857  put_symbol(c, state, (fs->slice_width + 1) * f->num_h_slices / f->width - 1,
858  0);
859  put_symbol(c, state,
860  (fs->slice_height + 1) * f->num_v_slices / f->height - 1,
861  0);
862  for (j = 0; j < f->plane_count; j++) {
863  put_symbol(c, state, f->plane[j].quant_table_index, 0);
865  }
867  put_symbol(c, state, 3, 0);
868  else
869  put_symbol(c, state, 1 + !f->avctx->coded_frame->top_field_first, 0);
872 }
873 
874 static int encode_slice(AVCodecContext *c, void *arg)
875 {
876  FFV1Context *fs = *(void **)arg;
877  FFV1Context *f = fs->avctx->priv_data;
878  int width = fs->slice_width;
879  int height = fs->slice_height;
880  int x = fs->slice_x;
881  int y = fs->slice_y;
882  const AVFrame *const p = f->frame;
884  ? (f->bits_per_raw_sample > 8) + 1
885  : 4;
886 
887  if (c->coded_frame->key_frame)
888  ffv1_clear_slice_state(f, fs);
889  if (f->version > 2) {
890  encode_slice_header(f, fs);
891  }
892  if (!fs->ac) {
893  if (f->version > 2)
894  put_rac(&fs->c, (uint8_t[]) { 129 }, 0);
895  fs->ac_byte_count = f->version > 2 || (!x && !y) ? ff_rac_terminate( &fs->c) : 0;
898  }
899 
900  if (f->colorspace == 0) {
901  const int chroma_width = -((-width) >> f->chroma_h_shift);
902  const int chroma_height = -((-height) >> f->chroma_v_shift);
903  const int cx = x >> f->chroma_h_shift;
904  const int cy = y >> f->chroma_v_shift;
905 
906  encode_plane(fs, p->data[0] + ps * x + y * p->linesize[0],
907  width, height, p->linesize[0], 0);
908 
909  if (f->chroma_planes) {
910  encode_plane(fs, p->data[1] + ps * cx + cy * p->linesize[1],
911  chroma_width, chroma_height, p->linesize[1], 1);
912  encode_plane(fs, p->data[2] + ps * cx + cy * p->linesize[2],
913  chroma_width, chroma_height, p->linesize[2], 1);
914  }
915  if (fs->transparency)
916  encode_plane(fs, p->data[3] + ps * x + y * p->linesize[3], width,
917  height, p->linesize[3], 2);
918  } else {
919  uint8_t *planes[3] = { p->data[0] + ps * x + y * p->linesize[0],
920  p->data[1] + ps * x + y * p->linesize[1],
921  p->data[2] + ps * x + y * p->linesize[2] };
922  encode_rgb_frame(fs, planes, width, height, p->linesize);
923  }
924  emms_c();
925 
926  return 0;
927 }
928 
930  const AVFrame *pict, int *got_packet)
931 {
932  FFV1Context *f = avctx->priv_data;
933  RangeCoder *const c = &f->slice_context[0]->c;
934  AVFrame *const p = avctx->coded_frame;
935  int used_count = 0;
936  uint8_t keystate = 128;
937  uint8_t *buf_p;
938  int i, ret;
939 
940  f->frame = pict;
941 
942  if ((ret = ff_alloc_packet(pkt, avctx->width * avctx->height *
943  ((8 * 2 + 1 + 1) * 4) / 8 +
944  FF_MIN_BUFFER_SIZE)) < 0) {
945  av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
946  return ret;
947  }
948 
949  ff_init_range_encoder(c, pkt->data, pkt->size);
950  ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
951 
952  if (avctx->gop_size == 0 || f->picture_number % avctx->gop_size == 0) {
953  put_rac(c, &keystate, 1);
954  p->key_frame = 1;
955  f->gob_count++;
956  write_header(f);
957  } else {
958  put_rac(c, &keystate, 0);
959  p->key_frame = 0;
960  }
961 
962  if (f->ac > 1) {
963  int i;
964  for (i = 1; i < 256; i++) {
965  c->one_state[i] = f->state_transition[i];
966  c->zero_state[256 - i] = 256 - c->one_state[i];
967  }
968  }
969 
970  for (i = 1; i < f->slice_count; i++) {
971  FFV1Context *fs = f->slice_context[i];
972  uint8_t *start = pkt->data +
973  (pkt->size - used_count) * (int64_t)i / f->slice_count;
974  int len = pkt->size / f->slice_count;
975  ff_init_range_encoder(&fs->c, start, len);
976  }
977  avctx->execute(avctx, encode_slice, &f->slice_context[0], NULL,
978  f->slice_count, sizeof(void *));
979 
980  buf_p = pkt->data;
981  for (i = 0; i < f->slice_count; i++) {
982  FFV1Context *fs = f->slice_context[i];
983  int bytes;
984 
985  if (fs->ac) {
986  uint8_t state = 129;
987  put_rac(&fs->c, &state, 0);
988  bytes = ff_rac_terminate(&fs->c);
989  } else {
990  flush_put_bits(&fs->pb); // FIXME: nicer padding
991  bytes = fs->ac_byte_count + (put_bits_count(&fs->pb) + 7) / 8;
992  }
993  if (i > 0 || f->version > 2) {
994  av_assert0(bytes < pkt->size / f->slice_count);
995  memmove(buf_p, fs->c.bytestream_start, bytes);
996  av_assert0(bytes < (1 << 24));
997  AV_WB24(buf_p + bytes, bytes);
998  bytes += 3;
999  }
1000  if (f->ec) {
1001  unsigned v;
1002  buf_p[bytes++] = 0;
1003  v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, bytes);
1004  AV_WL32(buf_p + bytes, v);
1005  bytes += 4;
1006  }
1007  buf_p += bytes;
1008  }
1009 
1010  if ((avctx->flags & CODEC_FLAG_PASS1) && (f->picture_number & 31) == 0) {
1011  int j, k, m;
1012  char *p = avctx->stats_out;
1013  char *end = p + STATS_OUT_SIZE;
1014 
1015  memset(f->rc_stat, 0, sizeof(f->rc_stat));
1016  for (i = 0; i < f->quant_table_count; i++)
1017  memset(f->rc_stat2[i], 0, f->context_count[i] * sizeof(*f->rc_stat2[i]));
1018 
1019  for (j = 0; j < f->slice_count; j++) {
1020  FFV1Context *fs = f->slice_context[j];
1021  for (i = 0; i < 256; i++) {
1022  f->rc_stat[i][0] += fs->rc_stat[i][0];
1023  f->rc_stat[i][1] += fs->rc_stat[i][1];
1024  }
1025  for (i = 0; i < f->quant_table_count; i++) {
1026  for (k = 0; k < f->context_count[i]; k++)
1027  for (m = 0; m < 32; m++) {
1028  f->rc_stat2[i][k][m][0] += fs->rc_stat2[i][k][m][0];
1029  f->rc_stat2[i][k][m][1] += fs->rc_stat2[i][k][m][1];
1030  }
1031  }
1032  }
1033 
1034  for (j = 0; j < 256; j++) {
1035  snprintf(p, end - p, "%" PRIu64 " %" PRIu64 " ",
1036  f->rc_stat[j][0], f->rc_stat[j][1]);
1037  p += strlen(p);
1038  }
1039  snprintf(p, end - p, "\n");
1040 
1041  for (i = 0; i < f->quant_table_count; i++) {
1042  for (j = 0; j < f->context_count[i]; j++)
1043  for (m = 0; m < 32; m++) {
1044  snprintf(p, end - p, "%" PRIu64 " %" PRIu64 " ",
1045  f->rc_stat2[i][j][m][0], f->rc_stat2[i][j][m][1]);
1046  p += strlen(p);
1047  }
1048  }
1049  snprintf(p, end - p, "%d\n", f->gob_count);
1050  } else if (avctx->flags & CODEC_FLAG_PASS1)
1051  avctx->stats_out[0] = '\0';
1052 
1053  f->picture_number++;
1054  pkt->size = buf_p - pkt->data;
1055  pkt->flags |= AV_PKT_FLAG_KEY * p->key_frame;
1056  *got_packet = 1;
1057 
1058  return 0;
1059 }
1060 
1062 {
1063  av_frame_free(&avctx->coded_frame);
1064  ffv1_close(avctx);
1065  return 0;
1066 }
1067 
1068 #define OFFSET(x) offsetof(FFV1Context, x)
1069 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1070 static const AVOption options[] = {
1071  { "slicecrc", "Protect slices with CRCs", OFFSET(ec), AV_OPT_TYPE_INT,
1072  { .i64 = -1 }, -1, 1, VE },
1073  { NULL }
1074 };
1075 
1076 static const AVClass class = {
1077  .class_name = "ffv1 encoder",
1078  .item_name = av_default_item_name,
1079  .option = options,
1081 };
1082 
1083 static const AVCodecDefault ffv1_defaults[] = {
1084  { "coder", "-1" },
1085  { NULL },
1086 };
1087 
1089  .name = "ffv1",
1090  .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
1091  .type = AVMEDIA_TYPE_VIDEO,
1092  .id = AV_CODEC_ID_FFV1,
1093  .priv_data_size = sizeof(FFV1Context),
1095  .encode2 = ffv1_encode_frame,
1097  .capabilities = CODEC_CAP_SLICE_THREADS,
1098  .pix_fmts = (const enum AVPixelFormat[]) {
1109 
1110  },
1111  .defaults = ffv1_defaults,
1112  .priv_class = &class,
1113 };
static av_always_inline int fold(int diff, int bits)
Definition: ffv1.h:118
#define FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
Definition: avcodec.h:2346
int ffv1_allocate_initial_states(FFV1Context *f)
Definition: ffv1.c:223
const uint8_t ff_log2_run[41]
Definition: bitstream.c:36
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
const int8_t ffv1_quant5[256]
Definition: ffv1.c:57
static const AVCodecDefault ffv1_defaults[]
Definition: ffv1enc.c:1083
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index)
Definition: ffv1enc.c:260
int size
static void encode_rgb_frame(FFV1Context *s, uint8_t *src[3], int w, int h, int stride[3])
Definition: ffv1enc.c:296
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1599
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition: crc.c:311
AVOption.
Definition: opt.h:234
static void encode_slice_header(FFV1Context *f, FFV1Context *fs)
Definition: ffv1enc.c:848
av_cold int ffv1_common_init(AVCodecContext *avctx)
Definition: ffv1.c:134
static av_cold int ffv1_encode_init(AVCodecContext *avctx)
Definition: ffv1enc.c:559
#define CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
Definition: avcodec.h:636
int flags
Definition: ffv1.h:79
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:70
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
int16_t quant_table[MAX_CONTEXT_INPUTS][256]
Definition: ffv1.h:55
int quant_table_count
Definition: ffv1.h:106
int ffv1_init_slice_state(FFV1Context *f, FFV1Context *fs)
Definition: ffv1.c:154
#define CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
Definition: avcodec.h:635
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2532
int slice_height
Definition: ffv1.h:113
#define MAX_CONTEXT_INPUTS
Definition: ffv1.h:37
int16_t * sample_buffer
Definition: ffv1.h:96
int version
Definition: ffv1.h:73
uint8_t zero_state[256]
Definition: rangecoder.h:40
Range coder.
uint8_t * bytestream_end
Definition: rangecoder.h:44
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:974
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:251
static int ffv1_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: ffv1enc.c:929
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1254
char * stats_in
pass2 encoding statistics input buffer Concatenated stuff from stats_out of pass1 should be placed he...
Definition: avcodec.h:2300
static av_always_inline av_flatten void put_symbol_inline(RangeCoder *c, uint8_t *state, int v, int is_signed, uint64_t rc_stat[256][2], uint64_t rc_stat2[32][2])
Definition: ffv1enc.c:85
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
uint64_t(*[MAX_QUANT_TABLES] rc_stat2)[32][2]
Definition: ffv1.h:72
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2514
static av_always_inline void predict(PredictorState *ps, float *coef, int output_enable)
Definition: aacdec.c:1748
#define sample
int height
Definition: ffv1.h:75
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:2796
uint8_t one_state[256]
Definition: rangecoder.h:41
Macro definitions for various function/variable attributes.
#define log2(x)
Definition: libm.h:111
int plane_count
Definition: ffv1.h:85
int ff_rac_terminate(RangeCoder *c)
Definition: rangecoder.c:102
static void write_quant_tables(RangeCoder *c, int16_t quant_table[MAX_CONTEXT_INPUTS][256])
Definition: ffv1enc.c:372
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:38
int context_model
context model
Definition: avcodec.h:2185
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
const int8_t ffv1_quant11[256]
Definition: ffv1.c:95
static av_cold int ffv1_encode_close(AVCodecContext *avctx)
Definition: ffv1enc.c:1061
uint64_t rc_stat[256][2]
Definition: ffv1.h:71
PutBitContext pb
Definition: ffv1.h:70
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:104
const int8_t ffv1_quant5_10bit[256]
Definition: ffv1.c:38
uint8_t bits
Definition: crc.c:251
uint8_t
#define av_cold
Definition: attributes.h:66
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:57
static av_noinline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed)
Definition: ffv1enc.c:133
AVOptions.
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:297
int8_t bias
Definition: ffv1.h:50
#define b
Definition: input.c:52
RangeCoder c
Definition: ffv1.h:68
#define AV_WL32(p, d)
Definition: intreadwrite.h:255
#define emms_c()
Definition: internal.h:47
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1164
#define AV_PIX_FMT_GBRP9
Definition: pixfmt.h:250
int slice_y
Definition: ffv1.h:115
uint8_t(*[MAX_QUANT_TABLES] initial_states)[32]
Definition: ffv1.h:93
int coder_type
coder type
Definition: avcodec.h:2178
uint8_t * data
Definition: avcodec.h:973
uint8_t count
Definition: ffv1.h:51
static int encode_slice(AVCodecContext *c, void *arg)
Definition: ffv1enc.c:874
bitstream reader API header.
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:248
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:320
VlcState * vlc_state
Definition: ffv1.h:59
char * stats_out
pass1 encoding statistics output buffer
Definition: avcodec.h:2292
int minor_version
Definition: ffv1.h:74
static int write_extradata(FFV1Context *f)
Definition: ffv1enc.c:427
int bits_per_raw_sample
Definition: ffv1.h:102
int slice_width
Definition: ffv1.h:112
#define r
Definition: input.c:51
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1019
static int sort_stt(FFV1Context *s, uint8_t stt[256])
Definition: ffv1enc.c:500
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:170
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
int16_t quant_tables[MAX_QUANT_TABLES][MAX_CONTEXT_INPUTS][256]
Definition: ffv1.h:90
const int8_t ffv1_quant9_10bit[256]
Definition: ffv1.c:76
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:69
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
g
Definition: yuv2rgb.c:535
int context_count
Definition: ffv1.h:57
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1144
uint8_t * buf
Definition: put_bits.h:38
simple assert() macros that are a bit more flexible than ISO C assert().
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:168
const char * name
Name of the codec implementation.
Definition: avcodec.h:2803
static av_always_inline av_const double round(double x)
Definition: libm.h:151
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:245
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
Definition: put_bits.h:134
#define FFMAX(a, b)
Definition: common.h:55
uint8_t * bytestream
Definition: rangecoder.h:43
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:979
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:67
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:69
static av_always_inline int encode_line(FFV1Context *s, int w, int16_t *sample[3], int plane_index, int bits)
Definition: ffv1enc.c:170
int ac
Definition: ffv1.h:86
int16_t quant_table[MAX_CONTEXT_INPUTS][256]
Definition: ffv1.h:89
int run_index
Definition: ffv1.h:94
Definition: ffv1.h:47
#define AV_PIX_FMT_YUV422P9
Definition: pixfmt.h:241
#define av_flatten
Definition: attributes.h:72
uint8_t state_transition[256]
Definition: ffv1.h:92
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:71
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:196
#define AV_PIX_FMT_GBRP16
Definition: pixfmt.h:252
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:227
#define FFMIN(a, b)
Definition: common.h:57
#define FF_MIN_BUFFER_SIZE
minimum encoding buffer size Used to avoid some checks during header writing.
Definition: avcodec.h:538
int num_h_slices
Definition: ffv1.h:111
int width
picture width / height.
Definition: avcodec.h:1224
int colorspace
Definition: ffv1.h:95
static float quant_table[96]
Definition: binkaudio.c:42
const uint8_t ffv1_ver2_state[256]
Definition: ffv1.c:114
static int get_context(PlaneContext *p, int16_t *src, int16_t *last, int16_t *last2)
Definition: ffv1.h:140
#define MAX_PLANES
Definition: ffv1.h:33
static void update_vlc_state(VlcState *const state, const int v)
Definition: ffv1.h:162
int slice_count
Definition: ffv1.h:109
#define AV_WB24(p, d)
Definition: intreadwrite.h:412
AVCodec ff_ffv1_encoder
Definition: ffv1enc.c:1088
void ff_build_rac_states(RangeCoder *c, int factor, int max_p)
Definition: rangecoder.c:61
#define FFABS(a)
Definition: common.h:52
int level
level
Definition: avcodec.h:2705
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1245
int ac_byte_count
Definition: ffv1.h:87
int16_t drift
Definition: ffv1.h:48
int packed_at_lsb
Definition: ffv1.h:103
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:242
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
av_cold int ffv1_init_slice_contexts(FFV1Context *f)
Definition: ffv1.c:188
#define VE
Definition: ffv1enc.c:1069
static const AVOption options[]
Definition: ffv1enc.c:1070
static const float pred[4]
Definition: siprdata.h:259
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:246
NULL
Definition: eval.c:55
static int width
Definition: utils.c:156
#define AV_LOG_INFO
Standard information.
Definition: log.h:134
int context_count[MAX_QUANT_TABLES]
Definition: ffv1.h:91
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:153
#define STATS_OUT_SIZE
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:171
av_default_item_name
Definition: dnxhdenc.c:52
uint8_t flags
Definition: pixdesc.h:90
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
main external API structure.
Definition: avcodec.h:1050
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:490
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:222
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
Definition: frame.h:206
uint8_t * buf_end
Definition: put_bits.h:38
static void find_best_state(uint8_t best_state[256][256], const uint8_t one_state[256])
Definition: ffv1enc.c:43
int extradata_size
Definition: avcodec.h:1165
void ffv1_clear_slice_state(FFV1Context *f, FFV1Context *fs)
Definition: ffv1.c:238
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:243
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:71
static void write_quant_table(RangeCoder *c, int16_t *quant_table)
Definition: ffv1enc.c:357
Describe the class of an AVClass context structure.
Definition: log.h:33
av_cold void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size)
Definition: rangecoder.c:41
static void put_vlc_symbol(PutBitContext *pb, VlcState *const state, int v, int bits)
Definition: ffv1enc.c:139
static void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len)
write signed golomb rice code (ffv1).
Definition: golomb.h:570
static av_cold int init_slices_state(FFV1Context *f)
Definition: ffv1enc.c:548
int picture_number
Definition: ffv1.h:80
uint16_t error_sum
Definition: ffv1.h:49
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:240
static uint32_t state
Definition: trasher.c:27
#define CONTEXT_SIZE
Definition: ffv1.h:34
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:244
int gob_count
Definition: ffv1.h:105
int quant_table_index
Definition: ffv1.h:56
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
#define OFFSET(x)
Definition: ffv1enc.c:1068
int height
Definition: gxfenc.c:72
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:1245
#define COST2(old, new)
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
Y , 8bpp.
Definition: pixfmt.h:73
#define CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: avcodec.h:759
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:83
#define put_rac(C, S, B)
FF_ENABLE_DEPRECATION_WARNINGS int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition: pixdesc.c:1625
uint8_t(* state)[CONTEXT_SIZE]
Definition: ffv1.h:58
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:72
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
int den
denominator
Definition: rational.h:45
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:499
uint8_t * bytestream_start
Definition: rangecoder.h:42
int slices
Number of slices.
Definition: avcodec.h:1782
void * priv_data
Definition: avcodec.h:1092
int chroma_h_shift
Definition: ffv1.h:77
PlaneContext plane[MAX_PLANES]
Definition: ffv1.h:88
int transparency
Definition: ffv1.h:78
int(* execute)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg), void *arg2, int *ret, int count, int size)
The codec may call this to execute several independent things.
Definition: avcodec.h:2580
int chroma_v_shift
Definition: ffv1.h:77
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:325
int len
int chroma_planes
Definition: ffv1.h:76
#define av_log2
Definition: intmath.h:85
av_cold int ffv1_close(AVCodecContext *avctx)
Definition: ffv1.c:265
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:191
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:380
struct FFV1Context * slice_context[MAX_SLICES]
Definition: ffv1.h:108
#define av_uninit(x)
Definition: attributes.h:109
AVFrame * frame
Definition: ffv1.h:81
#define av_noinline
Definition: attributes.h:48
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:102
#define av_always_inline
Definition: attributes.h:40
#define FFSWAP(type, a, b)
Definition: common.h:60
int ec
Definition: ffv1.h:98
int num_v_slices
Definition: ffv1.h:110
exp golomb vlc stuff
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
This structure stores compressed data.
Definition: avcodec.h:950
AVCodecContext * avctx
Definition: ffv1.h:67
#define AV_PIX_FMT_FLAG_PLANAR
At least one pixel component is not in the first data plane.
Definition: pixdesc.h:124
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
int strict_std_compliance
strictly follow the standard (MPEG4, ...).
Definition: avcodec.h:2341
int slice_x
Definition: ffv1.h:114
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:247
for(j=16;j >0;--j)
int width
Definition: ffv1.h:75
Definition: vf_drawbox.c:37
bitstream writer API