FFmpeg  4.4.9
ffv1enc.c
Go to the documentation of this file.
1 /*
2  * FFV1 encoder
3  *
4  * Copyright (c) 2003-2013 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 /**
24  * @file
25  * FF Video Codec 1 (a lossless codec) encoder
26  */
27 
28 #include "libavutil/attributes.h"
29 #include "libavutil/avassert.h"
30 #include "libavutil/crc.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/pixdesc.h"
34 
35 #include "avcodec.h"
36 #include "internal.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 const int8_t quant5_10bit[256] = {
44  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
45  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
46  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
47  1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
48  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
49  2, 2, 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, -1,
57  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
58  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
59  -1, -1, -1, -1, -1, -1, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0,
60 };
61 
62 static const int8_t quant5[256] = {
63  0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
64  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
65  2, 2, 2, 2, 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, -1, -1, -1,
79 };
80 
81 static const int8_t quant9_10bit[256] = {
82  0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2,
83  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3,
84  3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
85  3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
86  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
87  4, 4, 4, 4, 4, 4, 4, 4, 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, -3, -3, -3, -3, -3, -3, -3,
95  -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
96  -3, -3, -3, -3, -3, -3, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
97  -2, -2, -2, -2, -1, -1, -1, -1, -1, -1, -1, -1, -0, -0, -0, -0,
98 };
99 
100 static const int8_t quant11[256] = {
101  0, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
102  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
103  4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
104  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
105  5, 5, 5, 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, -4, -4,
115  -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
116  -4, -4, -4, -4, -4, -3, -3, -3, -3, -3, -3, -3, -2, -2, -2, -1,
117 };
118 
119 static const uint8_t ver2_state[256] = {
120  0, 10, 10, 10, 10, 16, 16, 16, 28, 16, 16, 29, 42, 49, 20, 49,
121  59, 25, 26, 26, 27, 31, 33, 33, 33, 34, 34, 37, 67, 38, 39, 39,
122  40, 40, 41, 79, 43, 44, 45, 45, 48, 48, 64, 50, 51, 52, 88, 52,
123  53, 74, 55, 57, 58, 58, 74, 60, 101, 61, 62, 84, 66, 66, 68, 69,
124  87, 82, 71, 97, 73, 73, 82, 75, 111, 77, 94, 78, 87, 81, 83, 97,
125  85, 83, 94, 86, 99, 89, 90, 99, 111, 92, 93, 134, 95, 98, 105, 98,
126  105, 110, 102, 108, 102, 118, 103, 106, 106, 113, 109, 112, 114, 112, 116, 125,
127  115, 116, 117, 117, 126, 119, 125, 121, 121, 123, 145, 124, 126, 131, 127, 129,
128  165, 130, 132, 138, 133, 135, 145, 136, 137, 139, 146, 141, 143, 142, 144, 148,
129  147, 155, 151, 149, 151, 150, 152, 157, 153, 154, 156, 168, 158, 162, 161, 160,
130  172, 163, 169, 164, 166, 184, 167, 170, 177, 174, 171, 173, 182, 176, 180, 178,
131  175, 189, 179, 181, 186, 183, 192, 185, 200, 187, 191, 188, 190, 197, 193, 196,
132  197, 194, 195, 196, 198, 202, 199, 201, 210, 203, 207, 204, 205, 206, 208, 214,
133  209, 211, 221, 212, 213, 215, 224, 216, 217, 218, 219, 220, 222, 228, 223, 225,
134  226, 224, 227, 229, 240, 230, 231, 232, 233, 234, 235, 236, 238, 239, 237, 242,
135  241, 243, 242, 244, 245, 246, 247, 248, 249, 250, 251, 252, 252, 253, 254, 255,
136 };
137 
138 static void find_best_state(uint8_t best_state[256][256],
139  const uint8_t one_state[256])
140 {
141  int i, j, k, m;
142  double l2tab[256];
143 
144  for (i = 1; i < 256; i++)
145  l2tab[i] = log2(i / 256.0);
146 
147  for (i = 0; i < 256; i++) {
148  double best_len[256];
149  double p = i / 256.0;
150 
151  for (j = 0; j < 256; j++)
152  best_len[j] = 1 << 30;
153 
154  for (j = FFMAX(i - 10, 1); j < FFMIN(i + 11, 256); j++) {
155  double occ[256] = { 0 };
156  double len = 0;
157  occ[j] = 1.0;
158 
159  if (!one_state[j])
160  continue;
161 
162  for (k = 0; k < 256; k++) {
163  double newocc[256] = { 0 };
164  for (m = 1; m < 256; m++)
165  if (occ[m]) {
166  len -=occ[m]*( p *l2tab[ m]
167  + (1-p)*l2tab[256-m]);
168  }
169  if (len < best_len[k]) {
170  best_len[k] = len;
171  best_state[i][k] = j;
172  }
173  for (m = 1; m < 256; m++)
174  if (occ[m]) {
175  newocc[ one_state[ m]] += occ[m] * p;
176  newocc[256 - one_state[256 - m]] += occ[m] * (1 - p);
177  }
178  memcpy(occ, newocc, sizeof(occ));
179  }
180  }
181  }
182 }
183 
185  uint8_t *state, int v,
186  int is_signed,
187  uint64_t rc_stat[256][2],
188  uint64_t rc_stat2[32][2])
189 {
190  int i;
191 
192 #define put_rac(C, S, B) \
193  do { \
194  if (rc_stat) { \
195  rc_stat[*(S)][B]++; \
196  rc_stat2[(S) - state][B]++; \
197  } \
198  put_rac(C, S, B); \
199  } while (0)
200 
201  if (v) {
202  const unsigned a = is_signed ? FFABS(v) : v;
203  const int e = av_log2(a);
204  put_rac(c, state + 0, 0);
205  if (e <= 9) {
206  for (i = 0; i < e; i++)
207  put_rac(c, state + 1 + i, 1); // 1..10
208  put_rac(c, state + 1 + i, 0);
209 
210  for (i = e - 1; i >= 0; i--)
211  put_rac(c, state + 22 + i, (a >> i) & 1); // 22..31
212 
213  if (is_signed)
214  put_rac(c, state + 11 + e, v < 0); // 11..21
215  } else {
216  for (i = 0; i < e; i++)
217  put_rac(c, state + 1 + FFMIN(i, 9), 1); // 1..10
218  put_rac(c, state + 1 + 9, 0);
219 
220  for (i = e - 1; i >= 0; i--)
221  put_rac(c, state + 22 + FFMIN(i, 9), (a >> i) & 1); // 22..31
222 
223  if (is_signed)
224  put_rac(c, state + 11 + 10, v < 0); // 11..21
225  }
226  } else {
227  put_rac(c, state + 0, 1);
228  }
229 #undef put_rac
230 }
231 
233  int v, int is_signed)
234 {
235  put_symbol_inline(c, state, v, is_signed, NULL, NULL);
236 }
237 
238 
239 static inline void put_vlc_symbol(PutBitContext *pb, VlcState *const state,
240  int v, int bits)
241 {
242  int i, k, code;
243  v = fold(v - state->bias, bits);
244 
245  i = state->count;
246  k = 0;
247  while (i < state->error_sum) { // FIXME: optimize
248  k++;
249  i += i;
250  }
251 
252  av_assert2(k <= 13);
253 
254  code = v ^ ((2 * state->drift + state->count) >> 31);
255 
256  ff_dlog(NULL, "v:%d/%d bias:%d error:%d drift:%d count:%d k:%d\n", v, code,
257  state->bias, state->error_sum, state->drift, state->count, k);
258  set_sr_golomb(pb, code, k, 12, bits);
259 
261 }
262 
263 #define TYPE int16_t
264 #define RENAME(name) name
265 #include "ffv1enc_template.c"
266 #undef TYPE
267 #undef RENAME
268 
269 #define TYPE int32_t
270 #define RENAME(name) name ## 32
271 #include "ffv1enc_template.c"
272 
273 static int encode_plane(FFV1Context *s, uint8_t *src, int w, int h,
274  int stride, int plane_index, int pixel_stride)
275 {
276  int x, y, i, ret;
277  const int ring_size = s->context_model ? 3 : 2;
278  int16_t *sample[3];
279  s->run_index = 0;
280 
281  sample[2] = s->sample_buffer; // dummy to avoid UB pointer arithmetic
282 
283  memset(s->sample_buffer, 0, ring_size * (w + 6) * sizeof(*s->sample_buffer));
284 
285  for (y = 0; y < h; y++) {
286  for (i = 0; i < ring_size; i++)
287  sample[i] = s->sample_buffer + (w + 6) * ((h + i - y) % ring_size) + 3;
288 
289  sample[0][-1]= sample[1][0 ];
290  sample[1][ w]= sample[1][w-1];
291  if (s->bits_per_raw_sample <= 8) {
292  for (x = 0; x < w; x++)
293  sample[0][x] = src[x * pixel_stride + stride * y];
294  if((ret = encode_line(s, w, sample, plane_index, 8)) < 0)
295  return ret;
296  } else {
297  if (s->packed_at_lsb) {
298  for (x = 0; x < w; x++) {
299  sample[0][x] = ((uint16_t*)(src + stride*y))[x];
300  }
301  } else {
302  for (x = 0; x < w; x++) {
303  sample[0][x] = ((uint16_t*)(src + stride*y))[x] >> (16 - s->bits_per_raw_sample);
304  }
305  }
306  if((ret = encode_line(s, w, sample, plane_index, s->bits_per_raw_sample)) < 0)
307  return ret;
308  }
309  }
310  return 0;
311 }
312 
313 static void write_quant_table(RangeCoder *c, int16_t *quant_table)
314 {
315  int last = 0;
316  int i;
318  memset(state, 128, sizeof(state));
319 
320  for (i = 1; i < 128; i++)
321  if (quant_table[i] != quant_table[i - 1]) {
322  put_symbol(c, state, i - last - 1, 0);
323  last = i;
324  }
325  put_symbol(c, state, i - last - 1, 0);
326 }
327 
329  int16_t quant_table[MAX_CONTEXT_INPUTS][256])
330 {
331  int i;
332  for (i = 0; i < 5; i++)
334 }
335 
336 static int contains_non_128(uint8_t (*initial_state)[CONTEXT_SIZE],
337  int nb_contexts)
338 {
339  if (!initial_state)
340  return 0;
341  for (int i = 0; i < nb_contexts; i++)
342  for (int j = 0; j < CONTEXT_SIZE; j++)
343  if (initial_state[i][j] != 128)
344  return 1;
345  return 0;
346 }
347 
349 {
351  int i, j;
352  RangeCoder *const c = &f->slice_context[0]->c;
353 
354  memset(state, 128, sizeof(state));
355 
356  if (f->version < 2) {
357  put_symbol(c, state, f->version, 0);
358  put_symbol(c, state, f->ac, 0);
359  if (f->ac == AC_RANGE_CUSTOM_TAB) {
360  for (i = 1; i < 256; i++)
361  put_symbol(c, state,
362  f->state_transition[i] - c->one_state[i], 1);
363  }
364  put_symbol(c, state, f->colorspace, 0); //YUV cs type
365  if (f->version > 0)
366  put_symbol(c, state, f->bits_per_raw_sample, 0);
367  put_rac(c, state, f->chroma_planes);
368  put_symbol(c, state, f->chroma_h_shift, 0);
369  put_symbol(c, state, f->chroma_v_shift, 0);
370  put_rac(c, state, f->transparency);
371 
372  write_quant_tables(c, f->quant_table);
373  } else if (f->version < 3) {
374  put_symbol(c, state, f->slice_count, 0);
375  for (i = 0; i < f->slice_count; i++) {
376  FFV1Context *fs = f->slice_context[i];
377  put_symbol(c, state,
378  (fs->slice_x + 1) * f->num_h_slices / f->width, 0);
379  put_symbol(c, state,
380  (fs->slice_y + 1) * f->num_v_slices / f->height, 0);
381  put_symbol(c, state,
382  (fs->slice_width + 1) * f->num_h_slices / f->width - 1,
383  0);
384  put_symbol(c, state,
385  (fs->slice_height + 1) * f->num_v_slices / f->height - 1,
386  0);
387  for (j = 0; j < f->plane_count; j++) {
388  put_symbol(c, state, f->plane[j].quant_table_index, 0);
389  av_assert0(f->plane[j].quant_table_index == f->context_model);
390  }
391  }
392  }
393 }
394 
396 {
397  RangeCoder *const c = &f->c;
399  int i, j, k;
400  uint8_t state2[32][CONTEXT_SIZE];
401  unsigned v;
402 
403  memset(state2, 128, sizeof(state2));
404  memset(state, 128, sizeof(state));
405 
406  f->avctx->extradata_size = 10000 + 4 +
407  (11 * 11 * 5 * 5 * 5 + 11 * 11 * 11) * 32;
408  f->avctx->extradata = av_malloc(f->avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
409  if (!f->avctx->extradata)
410  return AVERROR(ENOMEM);
411  ff_init_range_encoder(c, f->avctx->extradata, f->avctx->extradata_size);
412  ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
413 
414  put_symbol(c, state, f->version, 0);
415  if (f->version > 2) {
416  if (f->version == 3) {
417  f->micro_version = 4;
418  } else if (f->version == 4)
419  f->micro_version = 2;
420  put_symbol(c, state, f->micro_version, 0);
421  }
422 
423  put_symbol(c, state, f->ac, 0);
424  if (f->ac == AC_RANGE_CUSTOM_TAB)
425  for (i = 1; i < 256; i++)
426  put_symbol(c, state, f->state_transition[i] - c->one_state[i], 1);
427 
428  put_symbol(c, state, f->colorspace, 0); // YUV cs type
429  put_symbol(c, state, f->bits_per_raw_sample, 0);
430  put_rac(c, state, f->chroma_planes);
431  put_symbol(c, state, f->chroma_h_shift, 0);
432  put_symbol(c, state, f->chroma_v_shift, 0);
433  put_rac(c, state, f->transparency);
434  put_symbol(c, state, f->num_h_slices - 1, 0);
435  put_symbol(c, state, f->num_v_slices - 1, 0);
436 
437  put_symbol(c, state, f->quant_table_count, 0);
438  for (i = 0; i < f->quant_table_count; i++)
439  write_quant_tables(c, f->quant_tables[i]);
440 
441  for (i = 0; i < f->quant_table_count; i++) {
442  if (contains_non_128(f->initial_states[i], f->context_count[i])) {
443  put_rac(c, state, 1);
444  for (j = 0; j < f->context_count[i]; j++)
445  for (k = 0; k < CONTEXT_SIZE; k++) {
446  int pred = j ? f->initial_states[i][j - 1][k] : 128;
447  put_symbol(c, state2[k],
448  (int8_t)(f->initial_states[i][j][k] - pred), 1);
449  }
450  } else {
451  put_rac(c, state, 0);
452  }
453  }
454 
455  if (f->version > 2) {
456  put_symbol(c, state, f->ec, 0);
457  put_symbol(c, state, f->intra = (f->avctx->gop_size < 2), 0);
458  }
459 
460  f->avctx->extradata_size = ff_rac_terminate(c, 0);
461  v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, f->avctx->extradata, f->avctx->extradata_size);
462  AV_WL32(f->avctx->extradata + f->avctx->extradata_size, v);
463  f->avctx->extradata_size += 4;
464 
465  return 0;
466 }
467 
468 static int sort_stt(FFV1Context *s, uint8_t stt[256])
469 {
470  int i, i2, changed, print = 0;
471 
472  do {
473  changed = 0;
474  for (i = 12; i < 244; i++) {
475  for (i2 = i + 1; i2 < 245 && i2 < i + 4; i2++) {
476 
477 #define COST(old, new) \
478  s->rc_stat[old][0] * -log2((256 - (new)) / 256.0) + \
479  s->rc_stat[old][1] * -log2((new) / 256.0)
480 
481 #define COST2(old, new) \
482  COST(old, new) + COST(256 - (old), 256 - (new))
483 
484  double size0 = COST2(i, i) + COST2(i2, i2);
485  double sizeX = COST2(i, i2) + COST2(i2, i);
486  if (size0 - sizeX > size0*(1e-14) && i != 128 && i2 != 128) {
487  int j;
488  FFSWAP(int, stt[i], stt[i2]);
489  FFSWAP(int, s->rc_stat[i][0], s->rc_stat[i2][0]);
490  FFSWAP(int, s->rc_stat[i][1], s->rc_stat[i2][1]);
491  if (i != 256 - i2) {
492  FFSWAP(int, stt[256 - i], stt[256 - i2]);
493  FFSWAP(int, s->rc_stat[256 - i][0], s->rc_stat[256 - i2][0]);
494  FFSWAP(int, s->rc_stat[256 - i][1], s->rc_stat[256 - i2][1]);
495  }
496  for (j = 1; j < 256; j++) {
497  if (stt[j] == i)
498  stt[j] = i2;
499  else if (stt[j] == i2)
500  stt[j] = i;
501  if (i != 256 - i2) {
502  if (stt[256 - j] == 256 - i)
503  stt[256 - j] = 256 - i2;
504  else if (stt[256 - j] == 256 - i2)
505  stt[256 - j] = 256 - i;
506  }
507  }
508  print = changed = 1;
509  }
510  }
511  }
512  } while (changed);
513  return print;
514 }
515 
517 {
518  FFV1Context *s = avctx->priv_data;
520  int i, j, k, m, ret;
521 
522  if ((ret = ff_ffv1_common_init(avctx)) < 0)
523  return ret;
524 
525  s->version = 0;
526 
527  if ((avctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) ||
528  avctx->slices > 1)
529  s->version = FFMAX(s->version, 2);
530 
531  if ((avctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) && s->ac == AC_GOLOMB_RICE) {
532  av_log(avctx, AV_LOG_ERROR, "2 Pass mode is not possible with golomb coding\n");
533  return AVERROR(EINVAL);
534  }
535 
536  // Unspecified level & slices, we choose version 1.2+ to ensure multithreaded decodability
537  if (avctx->slices == 0 && avctx->level < 0 && avctx->width * avctx->height > 720*576)
538  s->version = FFMAX(s->version, 2);
539 
540  if (avctx->level <= 0 && s->version == 2) {
541  s->version = 3;
542  }
543  if (avctx->level >= 0 && avctx->level <= 4) {
544  if (avctx->level < s->version) {
545  av_log(avctx, AV_LOG_ERROR, "Version %d needed for requested features but %d requested\n", s->version, avctx->level);
546  return AVERROR(EINVAL);
547  }
548  s->version = avctx->level;
549  }
550 
551  if (s->ec < 0) {
552  s->ec = (s->version >= 3);
553  }
554 
555  // CRC requires version 3+
556  if (s->ec)
557  s->version = FFMAX(s->version, 3);
558 
559  if ((s->version == 2 || s->version>3) && avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
560  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");
561  return AVERROR_INVALIDDATA;
562  }
563 
564 #if FF_API_CODER_TYPE
566  if (avctx->coder_type != -1)
567  s->ac = avctx->coder_type > 0 ? AC_RANGE_CUSTOM_TAB : AC_GOLOMB_RICE;
568  else
570 #endif
571  if (s->ac == 1) // Compatbility with common command line usage
572  s->ac = AC_RANGE_CUSTOM_TAB;
573  else if (s->ac == AC_RANGE_DEFAULT_TAB_FORCE)
574  s->ac = AC_RANGE_DEFAULT_TAB;
575 
576  s->plane_count = 3;
577  switch(avctx->pix_fmt) {
578  case AV_PIX_FMT_GRAY9:
579  case AV_PIX_FMT_YUV444P9:
580  case AV_PIX_FMT_YUV422P9:
581  case AV_PIX_FMT_YUV420P9:
585  if (!avctx->bits_per_raw_sample)
586  s->bits_per_raw_sample = 9;
587  case AV_PIX_FMT_GRAY10:
595  if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
596  s->bits_per_raw_sample = 10;
597  case AV_PIX_FMT_GRAY12:
602  if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
603  s->bits_per_raw_sample = 12;
607  if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
608  s->bits_per_raw_sample = 14;
609  s->packed_at_lsb = 1;
610  case AV_PIX_FMT_GRAY16:
617  if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample) {
618  s->bits_per_raw_sample = 16;
619  } else if (!s->bits_per_raw_sample) {
620  s->bits_per_raw_sample = avctx->bits_per_raw_sample;
621  }
622  if (s->bits_per_raw_sample <= 8) {
623  av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample invalid\n");
624  return AVERROR_INVALIDDATA;
625  }
626  s->version = FFMAX(s->version, 1);
627  case AV_PIX_FMT_GRAY8:
628  case AV_PIX_FMT_YA8:
629  case AV_PIX_FMT_YUV444P:
630  case AV_PIX_FMT_YUV440P:
631  case AV_PIX_FMT_YUV422P:
632  case AV_PIX_FMT_YUV420P:
633  case AV_PIX_FMT_YUV411P:
634  case AV_PIX_FMT_YUV410P:
635  case AV_PIX_FMT_YUVA444P:
636  case AV_PIX_FMT_YUVA422P:
637  case AV_PIX_FMT_YUVA420P:
638  s->chroma_planes = desc->nb_components < 3 ? 0 : 1;
639  s->colorspace = 0;
640  s->transparency = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
641  if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
642  s->bits_per_raw_sample = 8;
643  else if (!s->bits_per_raw_sample)
644  s->bits_per_raw_sample = 8;
645  break;
646  case AV_PIX_FMT_RGB32:
647  s->colorspace = 1;
648  s->transparency = 1;
649  s->chroma_planes = 1;
650  s->bits_per_raw_sample = 8;
651  break;
652  case AV_PIX_FMT_RGBA64:
653  s->colorspace = 1;
654  s->transparency = 1;
655  s->chroma_planes = 1;
656  s->bits_per_raw_sample = 16;
657  s->use32bit = 1;
658  s->version = FFMAX(s->version, 1);
659  break;
660  case AV_PIX_FMT_RGB48:
661  s->colorspace = 1;
662  s->chroma_planes = 1;
663  s->bits_per_raw_sample = 16;
664  s->use32bit = 1;
665  s->version = FFMAX(s->version, 1);
666  break;
667  case AV_PIX_FMT_0RGB32:
668  s->colorspace = 1;
669  s->chroma_planes = 1;
670  s->bits_per_raw_sample = 8;
671  break;
672  case AV_PIX_FMT_GBRP9:
673  if (!avctx->bits_per_raw_sample)
674  s->bits_per_raw_sample = 9;
675  case AV_PIX_FMT_GBRP10:
676  case AV_PIX_FMT_GBRAP10:
677  if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
678  s->bits_per_raw_sample = 10;
679  case AV_PIX_FMT_GBRP12:
680  case AV_PIX_FMT_GBRAP12:
681  if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
682  s->bits_per_raw_sample = 12;
683  case AV_PIX_FMT_GBRP14:
684  if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
685  s->bits_per_raw_sample = 14;
686  case AV_PIX_FMT_GBRP16:
687  case AV_PIX_FMT_GBRAP16:
688  if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
689  s->bits_per_raw_sample = 16;
690  else if (!s->bits_per_raw_sample)
691  s->bits_per_raw_sample = avctx->bits_per_raw_sample;
692  s->transparency = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
693  s->colorspace = 1;
694  s->chroma_planes = 1;
695  if (s->bits_per_raw_sample >= 16) {
696  s->use32bit = 1;
697  }
698  s->version = FFMAX(s->version, 1);
699  break;
700  default:
701  av_log(avctx, AV_LOG_ERROR, "format not supported\n");
702  return AVERROR(ENOSYS);
703  }
704  av_assert0(s->bits_per_raw_sample >= 8);
705 
706  if (s->bits_per_raw_sample > 8) {
707  if (s->ac == AC_GOLOMB_RICE) {
708  av_log(avctx, AV_LOG_INFO,
709  "bits_per_raw_sample > 8, forcing range coder\n");
710  s->ac = AC_RANGE_CUSTOM_TAB;
711  }
712  }
713 #if FF_API_PRIVATE_OPT
715  if (avctx->context_model)
716  s->context_model = avctx->context_model;
717  if (avctx->context_model > 1U) {
718  av_log(avctx, AV_LOG_ERROR, "Invalid context model %d, valid values are 0 and 1\n", avctx->context_model);
719  return AVERROR(EINVAL);
720  }
722 #endif
723 
724  if (s->ac == AC_RANGE_CUSTOM_TAB) {
725  for (i = 1; i < 256; i++)
726  s->state_transition[i] = ver2_state[i];
727  } else {
728  RangeCoder c;
729  ff_build_rac_states(&c, 0.05 * (1LL << 32), 256 - 8);
730  for (i = 1; i < 256; i++)
731  s->state_transition[i] = c.one_state[i];
732  }
733 
734  for (i = 0; i < 256; i++) {
735  s->quant_table_count = 2;
736  if (s->bits_per_raw_sample <= 8) {
737  s->quant_tables[0][0][i]= quant11[i];
738  s->quant_tables[0][1][i]= 11*quant11[i];
739  s->quant_tables[0][2][i]= 11*11*quant11[i];
740  s->quant_tables[1][0][i]= quant11[i];
741  s->quant_tables[1][1][i]= 11*quant11[i];
742  s->quant_tables[1][2][i]= 11*11*quant5 [i];
743  s->quant_tables[1][3][i]= 5*11*11*quant5 [i];
744  s->quant_tables[1][4][i]= 5*5*11*11*quant5 [i];
745  s->context_count[0] = (11 * 11 * 11 + 1) / 2;
746  s->context_count[1] = (11 * 11 * 5 * 5 * 5 + 1) / 2;
747  } else {
748  s->quant_tables[0][0][i]= quant9_10bit[i];
749  s->quant_tables[0][1][i]= 9*quant9_10bit[i];
750  s->quant_tables[0][2][i]= 9*9*quant9_10bit[i];
751  s->quant_tables[1][0][i]= quant9_10bit[i];
752  s->quant_tables[1][1][i]= 9*quant9_10bit[i];
753  s->quant_tables[1][2][i]= 9*9*quant5_10bit[i];
754  s->quant_tables[1][3][i]= 5*9*9*quant5_10bit[i];
755  s->quant_tables[1][4][i]= 5*5*9*9*quant5_10bit[i];
756  s->context_count[0] = (9 * 9 * 9 + 1) / 2;
757  s->context_count[1] = (9 * 9 * 5 * 5 * 5 + 1) / 2;
758  }
759  }
760  memcpy(s->quant_table, s->quant_tables[s->context_model],
761  sizeof(s->quant_table));
762 
763  for (i = 0; i < s->plane_count; i++) {
764  PlaneContext *const p = &s->plane[i];
765 
766  memcpy(p->quant_table, s->quant_table, sizeof(p->quant_table));
767  p->quant_table_index = s->context_model;
768  p->context_count = s->context_count[p->quant_table_index];
769  }
770 
771  if ((ret = ff_ffv1_allocate_initial_states(s)) < 0)
772  return ret;
773 
774 #if FF_API_CODED_FRAME
778 #endif
779 
780  if (!s->transparency)
781  s->plane_count = 2;
782  if (!s->chroma_planes && s->version > 3)
783  s->plane_count--;
784 
785  ret = av_pix_fmt_get_chroma_sub_sample (avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
786  if (ret)
787  return ret;
788 
789  s->picture_number = 0;
790 
791  if (avctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) {
792  for (i = 0; i < s->quant_table_count; i++) {
793  s->rc_stat2[i] = av_mallocz(s->context_count[i] *
794  sizeof(*s->rc_stat2[i]));
795  if (!s->rc_stat2[i])
796  return AVERROR(ENOMEM);
797  }
798  }
799  if (avctx->stats_in) {
800  char *p = avctx->stats_in;
801  uint8_t (*best_state)[256] = av_malloc_array(256, 256);
802  int gob_count = 0;
803  char *next;
804  if (!best_state)
805  return AVERROR(ENOMEM);
806 
807  av_assert0(s->version >= 2);
808 
809  for (;;) {
810  for (j = 0; j < 256; j++)
811  for (i = 0; i < 2; i++) {
812  s->rc_stat[j][i] = strtol(p, &next, 0);
813  if (next == p) {
814  av_log(avctx, AV_LOG_ERROR,
815  "2Pass file invalid at %d %d [%s]\n", j, i, p);
816  av_freep(&best_state);
817  return AVERROR_INVALIDDATA;
818  }
819  p = next;
820  }
821  for (i = 0; i < s->quant_table_count; i++)
822  for (j = 0; j < s->context_count[i]; j++) {
823  for (k = 0; k < 32; k++)
824  for (m = 0; m < 2; m++) {
825  s->rc_stat2[i][j][k][m] = strtol(p, &next, 0);
826  if (next == p) {
827  av_log(avctx, AV_LOG_ERROR,
828  "2Pass file invalid at %d %d %d %d [%s]\n",
829  i, j, k, m, p);
830  av_freep(&best_state);
831  return AVERROR_INVALIDDATA;
832  }
833  p = next;
834  }
835  }
836  gob_count = strtol(p, &next, 0);
837  if (next == p || gob_count <= 0) {
838  av_log(avctx, AV_LOG_ERROR, "2Pass file invalid\n");
839  av_freep(&best_state);
840  return AVERROR_INVALIDDATA;
841  }
842  p = next;
843  while (*p == '\n' || *p == ' ')
844  p++;
845  if (p[0] == 0)
846  break;
847  }
848  if (s->ac == AC_RANGE_CUSTOM_TAB)
849  sort_stt(s, s->state_transition);
850 
851  find_best_state(best_state, s->state_transition);
852 
853  for (i = 0; i < s->quant_table_count; i++) {
854  for (k = 0; k < 32; k++) {
855  double a=0, b=0;
856  int jp = 0;
857  for (j = 0; j < s->context_count[i]; j++) {
858  double p = 128;
859  if (s->rc_stat2[i][j][k][0] + s->rc_stat2[i][j][k][1] > 200 && j || a+b > 200) {
860  if (a+b)
861  p = 256.0 * b / (a + b);
862  s->initial_states[i][jp][k] =
863  best_state[av_clip(round(p), 1, 255)][av_clip_uint8((a + b) / gob_count)];
864  for(jp++; jp<j; jp++)
865  s->initial_states[i][jp][k] = s->initial_states[i][jp-1][k];
866  a=b=0;
867  }
868  a += s->rc_stat2[i][j][k][0];
869  b += s->rc_stat2[i][j][k][1];
870  if (a+b) {
871  p = 256.0 * b / (a + b);
872  }
873  s->initial_states[i][j][k] =
874  best_state[av_clip(round(p), 1, 255)][av_clip_uint8((a + b) / gob_count)];
875  }
876  }
877  }
878  av_freep(&best_state);
879  }
880 
881  if (s->version > 1) {
882  int plane_count = 1 + 2*s->chroma_planes + s->transparency;
883  int max_h_slices = AV_CEIL_RSHIFT(avctx->width , s->chroma_h_shift);
884  int max_v_slices = AV_CEIL_RSHIFT(avctx->height, s->chroma_v_shift);
885  s->num_v_slices = (avctx->width > 352 || avctx->height > 288 || !avctx->slices) ? 2 : 1;
886 
887  s->num_v_slices = FFMIN(s->num_v_slices, max_v_slices);
888 
889  for (; s->num_v_slices < 32; s->num_v_slices++) {
890  for (s->num_h_slices = s->num_v_slices; s->num_h_slices < 2*s->num_v_slices; s->num_h_slices++) {
891  int maxw = (avctx->width + s->num_h_slices - 1) / s->num_h_slices;
892  int maxh = (avctx->height + s->num_v_slices - 1) / s->num_v_slices;
893  if (s->num_h_slices > max_h_slices || s->num_v_slices > max_v_slices)
894  continue;
895  if (maxw * maxh * (int64_t)(s->bits_per_raw_sample+1) * plane_count > 8<<24)
896  continue;
897  if (s->version < 4)
898  if ( ff_need_new_slices(avctx->width , s->num_h_slices, s->chroma_h_shift)
899  ||ff_need_new_slices(avctx->height, s->num_v_slices, s->chroma_v_shift))
900  continue;
901  if (avctx->slices == s->num_h_slices * s->num_v_slices && avctx->slices <= MAX_SLICES || !avctx->slices)
902  goto slices_ok;
903  }
904  }
905  av_log(avctx, AV_LOG_ERROR,
906  "Unsupported number %d of slices requested, please specify a "
907  "supported number with -slices (ex:4,6,9,12,16, ...)\n",
908  avctx->slices);
909  return AVERROR(ENOSYS);
910 slices_ok:
911  if ((ret = write_extradata(s)) < 0)
912  return ret;
913  }
914 
915  if ((ret = ff_ffv1_init_slice_contexts(s)) < 0)
916  return ret;
917  s->slice_count = s->max_slice_count;
918  if ((ret = ff_ffv1_init_slices_state(s)) < 0)
919  return ret;
920 
921 #define STATS_OUT_SIZE 1024 * 1024 * 6
922  if (avctx->flags & AV_CODEC_FLAG_PASS1) {
924  if (!avctx->stats_out)
925  return AVERROR(ENOMEM);
926  for (i = 0; i < s->quant_table_count; i++)
927  for (j = 0; j < s->max_slice_count; j++) {
928  FFV1Context *sf = s->slice_context[j];
929  av_assert0(!sf->rc_stat2[i]);
930  sf->rc_stat2[i] = av_mallocz(s->context_count[i] *
931  sizeof(*sf->rc_stat2[i]));
932  if (!sf->rc_stat2[i])
933  return AVERROR(ENOMEM);
934  }
935  }
936 
937  return 0;
938 }
939 
941 {
942  RangeCoder *c = &fs->c;
944  int j;
945  memset(state, 128, sizeof(state));
946 
947  put_symbol(c, state, (fs->slice_x +1)*f->num_h_slices / f->width , 0);
948  put_symbol(c, state, (fs->slice_y +1)*f->num_v_slices / f->height , 0);
949  put_symbol(c, state, 0, 0);
950  put_symbol(c, state, 0, 0);
951  for (j=0; j<f->plane_count; j++) {
952  put_symbol(c, state, f->plane[j].quant_table_index, 0);
953  av_assert0(f->plane[j].quant_table_index == f->context_model);
954  }
955  if (!f->picture.f->interlaced_frame)
956  put_symbol(c, state, 3, 0);
957  else
958  put_symbol(c, state, 1 + !f->picture.f->top_field_first, 0);
959  put_symbol(c, state, f->picture.f->sample_aspect_ratio.num, 0);
960  put_symbol(c, state, f->picture.f->sample_aspect_ratio.den, 0);
961  if (f->version > 3) {
962  put_rac(c, state, fs->slice_coding_mode == 1);
963  if (fs->slice_coding_mode == 1)
965  put_symbol(c, state, fs->slice_coding_mode, 0);
966  if (fs->slice_coding_mode != 1) {
967  put_symbol(c, state, fs->slice_rct_by_coef, 0);
968  put_symbol(c, state, fs->slice_rct_ry_coef, 0);
969  }
970  }
971 }
972 
973 static void choose_rct_params(FFV1Context *fs, const uint8_t *src[3], const int stride[3], int w, int h)
974 {
975 #define NB_Y_COEFF 15
976  static const int rct_y_coeff[15][2] = {
977  {0, 0}, // 4G
978  {1, 1}, // R + 2G + B
979  {2, 2}, // 2R + 2B
980  {0, 2}, // 2G + 2B
981  {2, 0}, // 2R + 2G
982  {4, 0}, // 4R
983  {0, 4}, // 4B
984 
985  {0, 3}, // 1G + 3B
986  {3, 0}, // 3R + 1G
987  {3, 1}, // 3R + B
988  {1, 3}, // R + 3B
989  {1, 2}, // R + G + 2B
990  {2, 1}, // 2R + G + B
991  {0, 1}, // 3G + B
992  {1, 0}, // R + 3G
993  };
994 
995  int stat[NB_Y_COEFF] = {0};
996  int x, y, i, p, best;
997  int16_t *sample[3];
998  int lbd = fs->bits_per_raw_sample <= 8;
999  int packed = !src[1];
1000  int transparency = fs->transparency;
1001  int packed_size = (3 + transparency)*2;
1002 
1003  for (y = 0; y < h; y++) {
1004  int lastr=0, lastg=0, lastb=0;
1005  for (p = 0; p < 3; p++)
1006  sample[p] = fs->sample_buffer + p*w;
1007 
1008  for (x = 0; x < w; x++) {
1009  int b, g, r;
1010  int ab, ag, ar;
1011  if (lbd) {
1012  unsigned v = *((const uint32_t*)(src[0] + x*4 + stride[0]*y));
1013  b = v & 0xFF;
1014  g = (v >> 8) & 0xFF;
1015  r = (v >> 16) & 0xFF;
1016  } else if (packed) {
1017  const uint16_t *p = ((const uint16_t*)(src[0] + x*packed_size + stride[0]*y));
1018  r = p[0];
1019  g = p[1];
1020  b = p[2];
1021  } else {
1022  b = *((const uint16_t*)(src[0] + x*2 + stride[0]*y));
1023  g = *((const uint16_t*)(src[1] + x*2 + stride[1]*y));
1024  r = *((const uint16_t*)(src[2] + x*2 + stride[2]*y));
1025  }
1026 
1027  ar = r - lastr;
1028  ag = g - lastg;
1029  ab = b - lastb;
1030  if (x && y) {
1031  int bg = ag - sample[0][x];
1032  int bb = ab - sample[1][x];
1033  int br = ar - sample[2][x];
1034 
1035  br -= bg;
1036  bb -= bg;
1037 
1038  for (i = 0; i<NB_Y_COEFF; i++) {
1039  stat[i] += FFABS(bg + ((br*rct_y_coeff[i][0] + bb*rct_y_coeff[i][1])>>2));
1040  }
1041 
1042  }
1043  sample[0][x] = ag;
1044  sample[1][x] = ab;
1045  sample[2][x] = ar;
1046 
1047  lastr = r;
1048  lastg = g;
1049  lastb = b;
1050  }
1051  }
1052 
1053  best = 0;
1054  for (i=1; i<NB_Y_COEFF; i++) {
1055  if (stat[i] < stat[best])
1056  best = i;
1057  }
1058 
1059  fs->slice_rct_by_coef = rct_y_coeff[best][1];
1060  fs->slice_rct_ry_coef = rct_y_coeff[best][0];
1061 }
1062 
1063 static int encode_slice(AVCodecContext *c, void *arg)
1064 {
1065  FFV1Context *fs = *(void **)arg;
1066  FFV1Context *f = fs->avctx->priv_data;
1067  int width = fs->slice_width;
1068  int height = fs->slice_height;
1069  int x = fs->slice_x;
1070  int y = fs->slice_y;
1071  const AVFrame *const p = f->picture.f;
1072  const int ps = av_pix_fmt_desc_get(c->pix_fmt)->comp[0].step;
1073  int ret;
1074  RangeCoder c_bak = fs->c;
1075  const uint8_t *planes[4] = {p->data[0] + ps*x + y*p->linesize[0],
1076  p->data[1] ? p->data[1] + ps*x + y*p->linesize[1] : NULL,
1077  p->data[2] ? p->data[2] + ps*x + y*p->linesize[2] : NULL,
1078  p->data[3] ? p->data[3] + ps*x + y*p->linesize[3] : NULL};
1079 
1080  fs->slice_coding_mode = 0;
1081  if (f->version > 3) {
1083  } else {
1084  fs->slice_rct_by_coef = 1;
1085  fs->slice_rct_ry_coef = 1;
1086  }
1087 
1088 retry:
1089  if (f->key_frame)
1091  if (f->version > 2) {
1093  }
1094  if (fs->ac == AC_GOLOMB_RICE) {
1095  fs->ac_byte_count = f->version > 2 || (!x && !y) ? ff_rac_terminate(&fs->c, f->version > 2) : 0;
1096  init_put_bits(&fs->pb,
1097  fs->c.bytestream_start + fs->ac_byte_count,
1098  fs->c.bytestream_end - fs->c.bytestream_start - fs->ac_byte_count);
1099  }
1100 
1101  if (f->colorspace == 0 && c->pix_fmt != AV_PIX_FMT_YA8) {
1102  const int chroma_width = AV_CEIL_RSHIFT(width, f->chroma_h_shift);
1103  const int chroma_height = AV_CEIL_RSHIFT(height, f->chroma_v_shift);
1104  const int cx = x >> f->chroma_h_shift;
1105  const int cy = y >> f->chroma_v_shift;
1106 
1107  ret = encode_plane(fs, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 1);
1108 
1109  if (f->chroma_planes) {
1110  ret |= encode_plane(fs, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1, 1);
1111  ret |= encode_plane(fs, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1, 1);
1112  }
1113  if (fs->transparency)
1114  ret |= encode_plane(fs, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], 2, 1);
1115  } else if (c->pix_fmt == AV_PIX_FMT_YA8) {
1116  ret = encode_plane(fs, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 2);
1117  ret |= encode_plane(fs, p->data[0] + 1 + ps*x + y*p->linesize[0], width, height, p->linesize[0], 1, 2);
1118  } else if (f->use32bit) {
1119  ret = encode_rgb_frame32(fs, planes, width, height, p->linesize);
1120  } else {
1122  }
1123  emms_c();
1124 
1125  if (ret < 0) {
1126  av_assert0(fs->slice_coding_mode == 0);
1127  if (fs->version < 4 || !fs->ac) {
1128  av_log(c, AV_LOG_ERROR, "Buffer too small\n");
1129  return ret;
1130  }
1131  av_log(c, AV_LOG_DEBUG, "Coding slice as PCM\n");
1132  fs->slice_coding_mode = 1;
1133  fs->c = c_bak;
1134  goto retry;
1135  }
1136 
1137  return 0;
1138 }
1139 
1141  const AVFrame *pict, int *got_packet)
1142 {
1143  FFV1Context *f = avctx->priv_data;
1144  RangeCoder *const c = &f->slice_context[0]->c;
1145  AVFrame *const p = f->picture.f;
1146  uint8_t keystate = 128;
1147  uint8_t *buf_p;
1148  int i, ret;
1150  + avctx->width*avctx->height*37LL*4;
1151 
1152  if(!pict) {
1153  if (avctx->flags & AV_CODEC_FLAG_PASS1) {
1154  int j, k, m;
1155  char *p = avctx->stats_out;
1156  char *end = p + STATS_OUT_SIZE;
1157 
1158  memset(f->rc_stat, 0, sizeof(f->rc_stat));
1159  for (i = 0; i < f->quant_table_count; i++)
1160  memset(f->rc_stat2[i], 0, f->context_count[i] * sizeof(*f->rc_stat2[i]));
1161 
1162  av_assert0(f->slice_count == f->max_slice_count);
1163  for (j = 0; j < f->slice_count; j++) {
1164  FFV1Context *fs = f->slice_context[j];
1165  for (i = 0; i < 256; i++) {
1166  f->rc_stat[i][0] += fs->rc_stat[i][0];
1167  f->rc_stat[i][1] += fs->rc_stat[i][1];
1168  }
1169  for (i = 0; i < f->quant_table_count; i++) {
1170  for (k = 0; k < f->context_count[i]; k++)
1171  for (m = 0; m < 32; m++) {
1172  f->rc_stat2[i][k][m][0] += fs->rc_stat2[i][k][m][0];
1173  f->rc_stat2[i][k][m][1] += fs->rc_stat2[i][k][m][1];
1174  }
1175  }
1176  }
1177 
1178  for (j = 0; j < 256; j++) {
1179  snprintf(p, end - p, "%" PRIu64 " %" PRIu64 " ",
1180  f->rc_stat[j][0], f->rc_stat[j][1]);
1181  p += strlen(p);
1182  }
1183  snprintf(p, end - p, "\n");
1184 
1185  for (i = 0; i < f->quant_table_count; i++) {
1186  for (j = 0; j < f->context_count[i]; j++)
1187  for (m = 0; m < 32; m++) {
1188  snprintf(p, end - p, "%" PRIu64 " %" PRIu64 " ",
1189  f->rc_stat2[i][j][m][0], f->rc_stat2[i][j][m][1]);
1190  p += strlen(p);
1191  }
1192  }
1193  snprintf(p, end - p, "%d\n", f->gob_count);
1194  }
1195  return 0;
1196  }
1197 
1198  if (f->version > 3)
1199  maxsize = AV_INPUT_BUFFER_MIN_SIZE + avctx->width*avctx->height*3LL*4;
1200 
1201  if (maxsize > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - 32) {
1202  av_log(avctx, AV_LOG_WARNING, "Cannot allocate worst case packet size, the encoding could fail\n");
1203  maxsize = INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - 32;
1204  }
1205 
1206  if ((ret = ff_alloc_packet2(avctx, pkt, maxsize, 0)) < 0)
1207  return ret;
1208 
1210  ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
1211 
1212  av_frame_unref(p);
1213  if ((ret = av_frame_ref(p, pict)) < 0)
1214  return ret;
1215 #if FF_API_CODED_FRAME
1219 #endif
1220 
1221  if (avctx->gop_size == 0 || f->picture_number % avctx->gop_size == 0) {
1222  put_rac(c, &keystate, 1);
1223  f->key_frame = 1;
1224  f->gob_count++;
1225  write_header(f);
1226  } else {
1227  put_rac(c, &keystate, 0);
1228  f->key_frame = 0;
1229  }
1230 
1231  if (f->ac == AC_RANGE_CUSTOM_TAB) {
1232  int i;
1233  for (i = 1; i < 256; i++) {
1234  c->one_state[i] = f->state_transition[i];
1235  c->zero_state[256 - i] = 256 - c->one_state[i];
1236  }
1237  }
1238 
1239  for (i = 0; i < f->slice_count; i++) {
1240  FFV1Context *fs = f->slice_context[i];
1241  uint8_t *start = pkt->data + pkt->size * (int64_t)i / f->slice_count;
1242  int len = pkt->size / f->slice_count;
1243  if (i) {
1244  ff_init_range_encoder(&fs->c, start, len);
1245  } else {
1246  av_assert0(fs->c.bytestream_end >= fs->c.bytestream_start + len);
1247  av_assert0(fs->c.bytestream < fs->c.bytestream_start + len);
1248  fs->c.bytestream_end = fs->c.bytestream_start + len;
1249  }
1250  }
1251  avctx->execute(avctx, encode_slice, &f->slice_context[0], NULL,
1252  f->slice_count, sizeof(void *));
1253 
1254  buf_p = pkt->data;
1255  for (i = 0; i < f->slice_count; i++) {
1256  FFV1Context *fs = f->slice_context[i];
1257  int bytes;
1258 
1259  if (fs->ac != AC_GOLOMB_RICE) {
1260  bytes = ff_rac_terminate(&fs->c, 1);
1261  } else {
1262  flush_put_bits(&fs->pb); // FIXME: nicer padding
1263  bytes = fs->ac_byte_count + (put_bits_count(&fs->pb) + 7) / 8;
1264  }
1265  if (i > 0 || f->version > 2) {
1266  av_assert0(bytes < pkt->size / f->slice_count);
1267  memmove(buf_p, fs->c.bytestream_start, bytes);
1268  av_assert0(bytes < (1 << 24));
1269  AV_WB24(buf_p + bytes, bytes);
1270  bytes += 3;
1271  }
1272  if (f->ec) {
1273  unsigned v;
1274  buf_p[bytes++] = 0;
1275  v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, bytes);
1276  AV_WL32(buf_p + bytes, v);
1277  bytes += 4;
1278  }
1279  buf_p += bytes;
1280  }
1281 
1282  if (avctx->flags & AV_CODEC_FLAG_PASS1)
1283  avctx->stats_out[0] = '\0';
1284 
1285 #if FF_API_CODED_FRAME
1287  avctx->coded_frame->key_frame = f->key_frame;
1289 #endif
1290 
1291  f->picture_number++;
1292  pkt->size = buf_p - pkt->data;
1293  pkt->pts =
1294  pkt->dts = pict->pts;
1295  pkt->flags |= AV_PKT_FLAG_KEY * f->key_frame;
1296  *got_packet = 1;
1297 
1298  return 0;
1299 }
1300 
1302 {
1303  ff_ffv1_close(avctx);
1304  return 0;
1305 }
1306 
1307 #define OFFSET(x) offsetof(FFV1Context, x)
1308 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1309 static const AVOption options[] = {
1310  { "slicecrc", "Protect slices with CRCs", OFFSET(ec), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE },
1311  { "coder", "Coder type", OFFSET(ac), AV_OPT_TYPE_INT,
1312  { .i64 = 0 }, -2, 2, VE, "coder" },
1313  { "rice", "Golomb rice", 0, AV_OPT_TYPE_CONST,
1314  { .i64 = AC_GOLOMB_RICE }, INT_MIN, INT_MAX, VE, "coder" },
1315  { "range_def", "Range with default table", 0, AV_OPT_TYPE_CONST,
1316  { .i64 = AC_RANGE_DEFAULT_TAB_FORCE }, INT_MIN, INT_MAX, VE, "coder" },
1317  { "range_tab", "Range with custom table", 0, AV_OPT_TYPE_CONST,
1318  { .i64 = AC_RANGE_CUSTOM_TAB }, INT_MIN, INT_MAX, VE, "coder" },
1319  { "ac", "Range with custom table (the ac option exists for compatibility and is deprecated)", 0, AV_OPT_TYPE_CONST,
1320  { .i64 = 1 }, INT_MIN, INT_MAX, VE, "coder" },
1321  { "context", "Context model", OFFSET(context_model), AV_OPT_TYPE_INT,
1322  { .i64 = 0 }, 0, 1, VE },
1323 
1324  { NULL }
1325 };
1326 
1327 static const AVClass ffv1_class = {
1328  .class_name = "ffv1 encoder",
1329  .item_name = av_default_item_name,
1330  .option = options,
1331  .version = LIBAVUTIL_VERSION_INT,
1332 };
1333 
1334 #if FF_API_CODER_TYPE
1335 static const AVCodecDefault ffv1_defaults[] = {
1336  { "coder", "-1" },
1337  { NULL },
1338 };
1339 #endif
1340 
1342  .name = "ffv1",
1343  .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
1344  .type = AVMEDIA_TYPE_VIDEO,
1345  .id = AV_CODEC_ID_FFV1,
1346  .priv_data_size = sizeof(FFV1Context),
1347  .init = encode_init,
1348  .encode2 = encode_frame,
1349  .close = encode_close,
1351  .pix_fmts = (const enum AVPixelFormat[]) {
1372 
1373  },
1374 #if FF_API_CODER_TYPE
1375  .defaults = ffv1_defaults,
1376 #endif
1377  .priv_class = &ffv1_class,
1378  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1379 };
static int ring_size(RingBuffer *ring)
Definition: async.c:105
Macro definitions for various function/variable attributes.
#define av_always_inline
Definition: attributes.h:45
#define av_noinline
Definition: attributes.h:68
#define av_flatten
Definition: attributes.h:94
#define av_cold
Definition: attributes.h:88
uint8_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
Libavcodec external API header.
#define FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
Definition: avcodec.h:1610
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
#define s(width, name)
Definition: cbs_vp9.c:257
#define f(width, name)
Definition: cbs_vp9.c:255
#define fs(width, name, subs,...)
Definition: cbs_vp9.c:259
static struct @322 state
#define FFSWAP(type, a, b)
Definition: common.h:108
#define FFMIN(a, b)
Definition: common.h:105
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58
#define av_clip
Definition: common.h:122
#define FFMAX(a, b)
Definition: common.h:103
#define av_clip_uint8
Definition: common.h:128
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
Public header for CRC hash function implementation.
#define MAX_SLICES
Definition: dxva2_hevc.c:29
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:34
av_cold int ff_ffv1_common_init(AVCodecContext *avctx)
Definition: ffv1.c:41
av_cold int ff_ffv1_close(AVCodecContext *avctx)
Definition: ffv1.c:208
av_cold int ff_ffv1_init_slices_state(FFV1Context *f)
Definition: ffv1.c:105
int ff_need_new_slices(int width, int num_h_slices, int chroma_shift)
Definition: ffv1.c:116
int ff_ffv1_allocate_initial_states(FFV1Context *f)
Definition: ffv1.c:165
av_cold int ff_ffv1_init_slice_contexts(FFV1Context *f)
Definition: ffv1.c:123
void ff_ffv1_clear_slice_state(FFV1Context *f, FFV1Context *fs)
Definition: ffv1.c:180
FF Video Codec 1 (a lossless codec)
#define CONTEXT_SIZE
Definition: ffv1.h:50
static void update_vlc_state(VlcState *const state, const int v)
Definition: ffv1.h:162
#define AC_GOLOMB_RICE
Definition: ffv1.h:55
#define AC_RANGE_DEFAULT_TAB_FORCE
Definition: ffv1.h:58
#define AC_RANGE_CUSTOM_TAB
Definition: ffv1.h:57
#define AC_RANGE_DEFAULT_TAB
Definition: ffv1.h:56
static av_always_inline int fold(int diff, int bits)
Definition: ffv1.h:151
#define MAX_CONTEXT_INPUTS
Definition: ffv1.h:53
static void write_quant_table(RangeCoder *c, int16_t *quant_table)
Definition: ffv1enc.c:313
static av_noinline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed)
Definition: ffv1enc.c:232
#define COST2(old, new)
AVCodec ff_ffv1_encoder
Definition: ffv1enc.c:1341
static int contains_non_128(uint8_t(*initial_state)[CONTEXT_SIZE], int nb_contexts)
Definition: ffv1enc.c:336
static int encode_slice(AVCodecContext *c, void *arg)
Definition: ffv1enc.c:1063
static const AVOption options[]
Definition: ffv1enc.c:1309
#define VE
Definition: ffv1enc.c:1308
static void choose_rct_params(FFV1Context *fs, const uint8_t *src[3], const int stride[3], int w, int h)
Definition: ffv1enc.c:973
static void put_vlc_symbol(PutBitContext *pb, VlcState *const state, int v, int bits)
Definition: ffv1enc.c:239
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:348
#define put_rac(C, S, B)
static av_cold int encode_init(AVCodecContext *avctx)
Definition: ffv1enc.c:516
static av_cold int encode_close(AVCodecContext *avctx)
Definition: ffv1enc.c:1301
static const int8_t quant9_10bit[256]
Definition: ffv1enc.c:81
static const int8_t quant5[256]
Definition: ffv1enc.c:62
#define NB_Y_COEFF
static void encode_slice_header(FFV1Context *f, FFV1Context *fs)
Definition: ffv1enc.c:940
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: ffv1enc.c:1140
static const int8_t quant5_10bit[256]
Definition: ffv1enc.c:43
#define STATS_OUT_SIZE
static int sort_stt(FFV1Context *s, uint8_t stt[256])
Definition: ffv1enc.c:468
static void write_quant_tables(RangeCoder *c, int16_t quant_table[MAX_CONTEXT_INPUTS][256])
Definition: ffv1enc.c:328
static const AVClass ffv1_class
Definition: ffv1enc.c:1327
static int encode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index, int pixel_stride)
Definition: ffv1enc.c:273
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:184
#define OFFSET(x)
Definition: ffv1enc.c:1307
static int write_extradata(FFV1Context *f)
Definition: ffv1enc.c:395
static const int8_t quant11[256]
Definition: ffv1enc.c:100
static const AVCodecDefault ffv1_defaults[]
Definition: ffv1enc.c:1335
static void find_best_state(uint8_t best_state[256][256], const uint8_t one_state[256])
Definition: ffv1enc.c:138
static const uint8_t ver2_state[256]
Definition: ffv1enc.c:119
static int RENAME() encode_rgb_frame(FFV1Context *s, const uint8_t *src[4], int w, int h, const int stride[4])
static av_always_inline int RENAME() encode_line(FFV1Context *s, int w, TYPE *const sample[3], int plane_index, int bits)
#define sample
exp golomb vlc stuff
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:729
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
@ AV_OPT_TYPE_INT
Definition: opt.h:225
@ AV_OPT_TYPE_BOOL
Definition: opt.h:242
#define AV_CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
Definition: avcodec.h:300
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: codec.h:77
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: codec.h:112
#define AV_CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
Definition: avcodec.h:296
@ AV_CODEC_ID_FFV1
Definition: codec_id.h:82
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition: avcodec.h:215
#define AV_INPUT_BUFFER_MIN_SIZE
minimum encoding buffer size Used to avoid some checks during header writing.
Definition: avcodec.h:222
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:410
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:374
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:394
@ AV_CRC_32_IEEE
Definition: crc.h:53
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR(e)
Definition: error.h:43
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:553
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:443
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_INFO
Standard information.
Definition: log.h:205
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
for(j=16;j >0;--j)
misc image utilities
int i
Definition: input.c:407
#define av_log2
Definition: intmath.h:83
static const int16_t quant_table[64]
Definition: intrax8.c:522
#define AV_WB24(p, d)
Definition: intreadwrite.h:450
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:49
const char * arg
Definition: jacosubdec.c:67
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:83
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
#define emms_c()
Definition: internal.h:54
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:309
#define log2(x)
Definition: libm.h:404
static av_always_inline av_const double round(double x)
Definition: libm.h:444
const char * desc
Definition: libsvtav1.c:83
uint8_t w
Definition: llviddspenc.c:39
static const struct @323 planes[]
int stride
Definition: mace.c:144
AVOptions.
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:2601
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
#define AV_PIX_FMT_FLAG_ALPHA
The pixel format has an alpha channel.
Definition: pixdesc.h:179
#define AV_PIX_FMT_0RGB32
Definition: pixfmt.h:376
#define AV_PIX_FMT_GBRAP12
Definition: pixfmt.h:420
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:410
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:406
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:398
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:399
#define AV_PIX_FMT_YUV440P12
Definition: pixfmt.h:405
#define AV_PIX_FMT_GRAY9
Definition: pixfmt.h:379
#define AV_PIX_FMT_GBRAP16
Definition: pixfmt.h:421
#define AV_PIX_FMT_GBRP9
Definition: pixfmt.h:414
#define AV_PIX_FMT_YUV422P9
Definition: pixfmt.h:397
#define AV_PIX_FMT_YUVA444P10
Definition: pixfmt.h:438
#define AV_PIX_FMT_YUVA420P16
Definition: pixfmt.h:441
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:403
#define AV_PIX_FMT_YUVA420P10
Definition: pixfmt.h:436
#define AV_PIX_FMT_YUVA422P9
Definition: pixfmt.h:434
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:404
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:415
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:400
#define AV_PIX_FMT_GRAY12
Definition: pixfmt.h:381
#define AV_PIX_FMT_RGBA64
Definition: pixfmt.h:389
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:416
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:396
#define AV_PIX_FMT_RGB48
Definition: pixfmt.h:385
#define AV_PIX_FMT_YUVA420P9
Definition: pixfmt.h:433
#define AV_PIX_FMT_YUVA422P10
Definition: pixfmt.h:437
#define AV_PIX_FMT_YUV420P14
Definition: pixfmt.h:407
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:99
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:101
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:72
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:73
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:177
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:176
@ AV_PIX_FMT_YA8
8 bits gray, 8 bits alpha
Definition: pixfmt.h:143
#define AV_PIX_FMT_YUV422P14
Definition: pixfmt.h:408
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:380
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:411
#define AV_PIX_FMT_YUV440P10
Definition: pixfmt.h:401
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:383
#define AV_PIX_FMT_GBRAP10
Definition: pixfmt.h:419
#define AV_PIX_FMT_YUVA444P16
Definition: pixfmt.h:443
#define AV_PIX_FMT_YUVA422P16
Definition: pixfmt.h:442
#define AV_PIX_FMT_GBRP16
Definition: pixfmt.h:418
#define AV_PIX_FMT_YUV444P14
Definition: pixfmt.h:409
#define AV_PIX_FMT_YUVA444P9
Definition: pixfmt.h:435
#define AV_PIX_FMT_GBRP14
Definition: pixfmt.h:417
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:412
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:372
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:402
bitstream writer API
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:57
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:76
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:110
int ff_rac_terminate(RangeCoder *c, int version)
Terminates the range coder.
Definition: rangecoder.c:109
void ff_build_rac_states(RangeCoder *c, int factor, int max_p)
Definition: rangecoder.c:68
av_cold void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size)
Definition: rangecoder.c:42
Range coder.
static const float pred[4]
Definition: siprdata.h:259
#define snprintf
Definition: snprintf.h:34
const uint8_t * code
Definition: spdifenc.c:413
Describe the class of an AVClass context structure.
Definition: log.h:67
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:72
main external API structure.
Definition: avcodec.h:536
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:746
int width
picture width / height.
Definition: avcodec.h:709
char * stats_out
pass1 encoding statistics output buffer
Definition: avcodec.h:1561
attribute_deprecated int coder_type
Definition: avcodec.h:1459
int strict_std_compliance
strictly follow the standard (MPEG-4, ...).
Definition: avcodec.h:1605
char * stats_in
pass2 encoding statistics input buffer Concatenated stuff from stats_out of pass1 should be placed he...
Definition: avcodec.h:1569
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:1768
int level
level
Definition: avcodec.h:1988
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:1828
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1751
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:731
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:616
attribute_deprecated int context_model
Definition: avcodec.h:1465
void * priv_data
Definition: avcodec.h:563
int slices
Number of slices.
Definition: avcodec.h:1187
AVCodec.
Definition: codec.h:197
const char * name
Name of the codec implementation.
Definition: codec.h:204
int step
Number of elements between 2 horizontally consecutive pixels.
Definition: pixdesc.h:41
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:411
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:396
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:401
AVOption.
Definition: opt.h:248
This structure stores compressed data.
Definition: packet.h:346
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:375
int size
Definition: packet.h:370
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:362
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:368
uint8_t * data
Definition: packet.h:369
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
uint64_t(*[MAX_QUANT_TABLES] rc_stat2)[32][2]
Definition: ffv1.h:85
int16_t quant_table[MAX_CONTEXT_INPUTS][256]
Definition: ffv1.h:68
int quant_table_index
Definition: ffv1.h:69
int context_count
Definition: ffv1.h:70
Definition: ffv1.h:60
#define av_malloc_array(a, b)
#define ff_dlog(a,...)
#define av_freep(p)
#define av_malloc(s)
#define av_log(a,...)
#define src
Definition: vp8dsp.c:255
AVPacket * pkt
Definition: movenc.c:59
#define height
#define width
static void print(AVTreeNode *t, int depth)
Definition: tree.c:44
int size
const char * b
Definition: vf_curves.c:119
const char * g
Definition: vf_curves.c:118
const char * r
Definition: vf_curves.c:117
if(ret< 0)
Definition: vf_mcdeint.c:282
int len
uint8_t bits
Definition: vp3data.h:141
static double c[64]