FFmpeg  2.8.15
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
ffv1dec.c
Go to the documentation of this file.
1 /*
2  * FFV1 decoder
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) decoder
26  */
27 
28 #include "libavutil/avassert.h"
29 #include "libavutil/crc.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/pixdesc.h"
33 #include "libavutil/timer.h"
34 #include "avcodec.h"
35 #include "internal.h"
36 #include "get_bits.h"
37 #include "rangecoder.h"
38 #include "golomb.h"
39 #include "mathops.h"
40 #include "ffv1.h"
41 
43  int is_signed)
44 {
45  if (get_rac(c, state + 0))
46  return 0;
47  else {
48  int i, e;
49  unsigned a;
50  e = 0;
51  while (get_rac(c, state + 1 + FFMIN(e, 9))) { // 1..10
52  e++;
53  if (e > 31)
54  return AVERROR_INVALIDDATA;
55  }
56 
57  a = 1;
58  for (i = e - 1; i >= 0; i--)
59  a += a + get_rac(c, state + 22 + FFMIN(i, 9)); // 22..31
60 
61  e = -(is_signed && get_rac(c, state + 11 + FFMIN(e, 10))); // 11..21
62  return (a ^ e) - e;
63  }
64 }
65 
66 static av_noinline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed)
67 {
68  return get_symbol_inline(c, state, is_signed);
69 }
70 
71 static inline int get_vlc_symbol(GetBitContext *gb, VlcState *const state,
72  int bits)
73 {
74  int k, i, v, ret;
75 
76  i = state->count;
77  k = 0;
78  while (i < state->error_sum) { // FIXME: optimize
79  k++;
80  i += i;
81  }
82 
83  v = get_sr_golomb(gb, k, 12, bits);
84  ff_dlog(NULL, "v:%d bias:%d error:%d drift:%d count:%d k:%d",
85  v, state->bias, state->error_sum, state->drift, state->count, k);
86 
87 #if 0 // JPEG LS
88  if (k == 0 && 2 * state->drift <= -state->count)
89  v ^= (-1);
90 #else
91  v ^= ((2 * state->drift + state->count) >> 31);
92 #endif
93 
94  ret = fold(v + state->bias, bits);
95 
96  update_vlc_state(state, v);
97 
98  return ret;
99 }
100 
102  int16_t *sample[2],
103  int plane_index, int bits)
104 {
105  PlaneContext *const p = &s->plane[plane_index];
106  RangeCoder *const c = &s->c;
107  int x;
108  int run_count = 0;
109  int run_mode = 0;
110  int run_index = s->run_index;
111 
112  if (s->slice_coding_mode == 1) {
113  int i;
114  for (x = 0; x < w; x++) {
115  int v = 0;
116  for (i=0; i<bits; i++) {
117  uint8_t state = 128;
118  v += v + get_rac(c, &state);
119  }
120  sample[1][x] = v;
121  }
122  return;
123  }
124 
125  for (x = 0; x < w; x++) {
126  int diff, context, sign;
127 
128  context = get_context(p, sample[1] + x, sample[0] + x, sample[1] + x);
129  if (context < 0) {
130  context = -context;
131  sign = 1;
132  } else
133  sign = 0;
134 
135  av_assert2(context < p->context_count);
136 
137  if (s->ac) {
138  diff = get_symbol_inline(c, p->state[context], 1);
139  } else {
140  if (context == 0 && run_mode == 0)
141  run_mode = 1;
142 
143  if (run_mode) {
144  if (run_count == 0 && run_mode == 1) {
145  if (get_bits1(&s->gb)) {
146  run_count = 1 << ff_log2_run[run_index];
147  if (x + run_count <= w)
148  run_index++;
149  } else {
150  if (ff_log2_run[run_index])
151  run_count = get_bits(&s->gb, ff_log2_run[run_index]);
152  else
153  run_count = 0;
154  if (run_index)
155  run_index--;
156  run_mode = 2;
157  }
158  }
159  run_count--;
160  if (run_count < 0) {
161  run_mode = 0;
162  run_count = 0;
163  diff = get_vlc_symbol(&s->gb, &p->vlc_state[context],
164  bits);
165  if (diff >= 0)
166  diff++;
167  } else
168  diff = 0;
169  } else
170  diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
171 
172  ff_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
173  run_count, run_index, run_mode, x, get_bits_count(&s->gb));
174  }
175 
176  if (sign)
177  diff = -diff;
178 
179  sample[1][x] = av_mod_uintp2(predict(sample[1] + x, sample[0] + x) + diff, bits);
180  }
181  s->run_index = run_index;
182 }
183 
185  int w, int h, int stride, int plane_index)
186 {
187  int x, y;
188  int16_t *sample[2];
189  sample[0] = s->sample_buffer + 3;
190  sample[1] = s->sample_buffer + w + 6 + 3;
191 
192  s->run_index = 0;
193 
194  memset(s->sample_buffer, 0, 2 * (w + 6) * sizeof(*s->sample_buffer));
195 
196  for (y = 0; y < h; y++) {
197  int16_t *temp = sample[0]; // FIXME: try a normal buffer
198 
199  sample[0] = sample[1];
200  sample[1] = temp;
201 
202  sample[1][-1] = sample[0][0];
203  sample[0][w] = sample[0][w - 1];
204 
205 // { START_TIMER
206  if (s->avctx->bits_per_raw_sample <= 8) {
207  decode_line(s, w, sample, plane_index, 8);
208  for (x = 0; x < w; x++)
209  src[x + stride * y] = sample[1][x];
210  } else {
211  decode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
212  if (s->packed_at_lsb) {
213  for (x = 0; x < w; x++) {
214  ((uint16_t*)(src + stride*y))[x] = sample[1][x];
215  }
216  } else {
217  for (x = 0; x < w; x++) {
218  ((uint16_t*)(src + stride*y))[x] = sample[1][x] << (16 - s->avctx->bits_per_raw_sample);
219  }
220  }
221  }
222 // STOP_TIMER("decode-line") }
223  }
224 }
225 
226 static void decode_rgb_frame(FFV1Context *s, uint8_t *src[3], int w, int h, int stride[3])
227 {
228  int x, y, p;
229  int16_t *sample[4][2];
230  int lbd = s->avctx->bits_per_raw_sample <= 8;
231  int bits = s->avctx->bits_per_raw_sample > 0 ? s->avctx->bits_per_raw_sample : 8;
232  int offset = 1 << bits;
233 
234  for (x = 0; x < 4; x++) {
235  sample[x][0] = s->sample_buffer + x * 2 * (w + 6) + 3;
236  sample[x][1] = s->sample_buffer + (x * 2 + 1) * (w + 6) + 3;
237  }
238 
239  s->run_index = 0;
240 
241  memset(s->sample_buffer, 0, 8 * (w + 6) * sizeof(*s->sample_buffer));
242 
243  for (y = 0; y < h; y++) {
244  for (p = 0; p < 3 + s->transparency; p++) {
245  int16_t *temp = sample[p][0]; // FIXME: try a normal buffer
246 
247  sample[p][0] = sample[p][1];
248  sample[p][1] = temp;
249 
250  sample[p][1][-1]= sample[p][0][0 ];
251  sample[p][0][ w]= sample[p][0][w-1];
252  if (lbd && s->slice_coding_mode == 0)
253  decode_line(s, w, sample[p], (p + 1)/2, 9);
254  else
255  decode_line(s, w, sample[p], (p + 1)/2, bits + (s->slice_coding_mode != 1));
256  }
257  for (x = 0; x < w; x++) {
258  int g = sample[0][1][x];
259  int b = sample[1][1][x];
260  int r = sample[2][1][x];
261  int a = sample[3][1][x];
262 
263  if (s->slice_coding_mode != 1) {
264  b -= offset;
265  r -= offset;
266  g -= (b * s->slice_rct_by_coef + r * s->slice_rct_ry_coef) >> 2;
267  b += g;
268  r += g;
269  }
270 
271  if (lbd)
272  *((uint32_t*)(src[0] + x*4 + stride[0]*y)) = b + (g<<8) + (r<<16) + (a<<24);
273  else {
274  *((uint16_t*)(src[0] + x*2 + stride[0]*y)) = b;
275  *((uint16_t*)(src[1] + x*2 + stride[1]*y)) = g;
276  *((uint16_t*)(src[2] + x*2 + stride[2]*y)) = r;
277  }
278  }
279  }
280 }
281 
283 {
284  RangeCoder *c = &fs->c;
286  unsigned ps, i, context_count;
287  memset(state, 128, sizeof(state));
288 
289  av_assert0(f->version > 2);
290 
291  fs->slice_x = get_symbol(c, state, 0) * f->width ;
292  fs->slice_y = get_symbol(c, state, 0) * f->height;
293  fs->slice_width = (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
294  fs->slice_height = (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
295 
296  fs->slice_x /= f->num_h_slices;
297  fs->slice_y /= f->num_v_slices;
298  fs->slice_width = fs->slice_width /f->num_h_slices - fs->slice_x;
299  fs->slice_height = fs->slice_height/f->num_v_slices - fs->slice_y;
300  if ((unsigned)fs->slice_width > f->width || (unsigned)fs->slice_height > f->height)
301  return -1;
302  if ( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
303  || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
304  return -1;
305 
306  for (i = 0; i < f->plane_count; i++) {
307  PlaneContext * const p = &fs->plane[i];
308  int idx = get_symbol(c, state, 0);
309  if (idx >= (unsigned)f->quant_table_count) {
310  av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
311  return -1;
312  }
313  p->quant_table_index = idx;
314  memcpy(p->quant_table, f->quant_tables[idx], sizeof(p->quant_table));
315  context_count = f->context_count[idx];
316 
317  if (p->context_count < context_count) {
318  av_freep(&p->state);
319  av_freep(&p->vlc_state);
320  }
322  }
323 
324  ps = get_symbol(c, state, 0);
325  if (ps == 1) {
326  f->cur->interlaced_frame = 1;
327  f->cur->top_field_first = 1;
328  } else if (ps == 2) {
329  f->cur->interlaced_frame = 1;
330  f->cur->top_field_first = 0;
331  } else if (ps == 3) {
332  f->cur->interlaced_frame = 0;
333  }
334  f->cur->sample_aspect_ratio.num = get_symbol(c, state, 0);
335  f->cur->sample_aspect_ratio.den = get_symbol(c, state, 0);
336 
337  if (av_image_check_sar(f->width, f->height,
338  f->cur->sample_aspect_ratio) < 0) {
339  av_log(f->avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
342  f->cur->sample_aspect_ratio = (AVRational){ 0, 1 };
343  }
344 
345  if (fs->version > 3) {
346  fs->slice_reset_contexts = get_rac(c, state);
347  fs->slice_coding_mode = get_symbol(c, state, 0);
348  if (fs->slice_coding_mode != 1) {
349  fs->slice_rct_by_coef = get_symbol(c, state, 0);
350  fs->slice_rct_ry_coef = get_symbol(c, state, 0);
351  if ((uint64_t)fs->slice_rct_by_coef + (uint64_t)fs->slice_rct_ry_coef > 4) {
352  av_log(f->avctx, AV_LOG_ERROR, "slice_rct_y_coef out of range\n");
353  return AVERROR_INVALIDDATA;
354  }
355  }
356  }
357 
358  return 0;
359 }
360 
361 static int decode_slice(AVCodecContext *c, void *arg)
362 {
363  FFV1Context *fs = *(void **)arg;
364  FFV1Context *f = fs->avctx->priv_data;
365  int width, height, x, y, ret;
366  const int ps = av_pix_fmt_desc_get(c->pix_fmt)->comp[0].step_minus1 + 1;
367  AVFrame * const p = f->cur;
368  int i, si;
369 
370  for( si=0; fs != f->slice_context[si]; si ++)
371  ;
372 
373  if(f->fsrc && !p->key_frame)
375 
376  if(f->fsrc && !p->key_frame) {
377  FFV1Context *fssrc = f->fsrc->slice_context[si];
378  FFV1Context *fsdst = f->slice_context[si];
379  av_assert1(fsdst->plane_count == fssrc->plane_count);
380  av_assert1(fsdst == fs);
381 
382  if (!p->key_frame)
383  fsdst->slice_damaged |= fssrc->slice_damaged;
384 
385  for (i = 0; i < f->plane_count; i++) {
386  PlaneContext *psrc = &fssrc->plane[i];
387  PlaneContext *pdst = &fsdst->plane[i];
388 
389  av_free(pdst->state);
390  av_free(pdst->vlc_state);
391  memcpy(pdst, psrc, sizeof(*pdst));
392  pdst->state = NULL;
393  pdst->vlc_state = NULL;
394 
395  if (fssrc->ac) {
397  memcpy(pdst->state, psrc->state, CONTEXT_SIZE * psrc->context_count);
398  } else {
399  pdst->vlc_state = av_malloc_array(sizeof(*pdst->vlc_state), psrc->context_count);
400  memcpy(pdst->vlc_state, psrc->vlc_state, sizeof(*pdst->vlc_state) * psrc->context_count);
401  }
402  }
403  }
404 
405  fs->slice_rct_by_coef = 1;
406  fs->slice_rct_ry_coef = 1;
407 
408  if (f->version > 2) {
409  if (ff_ffv1_init_slice_state(f, fs) < 0)
410  return AVERROR(ENOMEM);
411  if (decode_slice_header(f, fs) < 0) {
412  fs->slice_x = fs->slice_y = fs->slice_height = fs->slice_width = 0;
413  fs->slice_damaged = 1;
414  return AVERROR_INVALIDDATA;
415  }
416  }
417  if ((ret = ff_ffv1_init_slice_state(f, fs)) < 0)
418  return ret;
419  if (f->cur->key_frame || fs->slice_reset_contexts)
421 
422  width = fs->slice_width;
423  height = fs->slice_height;
424  x = fs->slice_x;
425  y = fs->slice_y;
426 
427  if (!fs->ac) {
428  if (f->version == 3 && f->micro_version > 1 || f->version > 3)
429  get_rac(&fs->c, (uint8_t[]) { 129 });
430  fs->ac_byte_count = f->version > 2 || (!x && !y) ? fs->c.bytestream - fs->c.bytestream_start - 1 : 0;
431  init_get_bits(&fs->gb,
432  fs->c.bytestream_start + fs->ac_byte_count,
433  (fs->c.bytestream_end - fs->c.bytestream_start - fs->ac_byte_count) * 8);
434  }
435 
436  av_assert1(width && height);
437  if (f->colorspace == 0) {
438  const int chroma_width = FF_CEIL_RSHIFT(width, f->chroma_h_shift);
439  const int chroma_height = FF_CEIL_RSHIFT(height, f->chroma_v_shift);
440  const int cx = x >> f->chroma_h_shift;
441  const int cy = y >> f->chroma_v_shift;
442  decode_plane(fs, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0);
443 
444  if (f->chroma_planes) {
445  decode_plane(fs, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
446  decode_plane(fs, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1);
447  }
448  if (fs->transparency)
449  decode_plane(fs, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], (f->version >= 4 && !f->chroma_planes) ? 1 : 2);
450  } else {
451  uint8_t *planes[3] = { p->data[0] + ps * x + y * p->linesize[0],
452  p->data[1] + ps * x + y * p->linesize[1],
453  p->data[2] + ps * x + y * p->linesize[2] };
454  decode_rgb_frame(fs, planes, width, height, p->linesize);
455  }
456  if (fs->ac && f->version > 2) {
457  int v;
458  get_rac(&fs->c, (uint8_t[]) { 129 });
459  v = fs->c.bytestream_end - fs->c.bytestream - 2 - 5*f->ec;
460  if (v) {
461  av_log(f->avctx, AV_LOG_ERROR, "bytestream end mismatching by %d\n", v);
462  fs->slice_damaged = 1;
463  }
464  }
465 
466  emms_c();
467 
469 
470  return 0;
471 }
472 
473 static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale)
474 {
475  int v;
476  int i = 0;
478 
479  memset(state, 128, sizeof(state));
480 
481  for (v = 0; i < 128; v++) {
482  unsigned len = get_symbol(c, state, 0) + 1U;
483 
484  if (len > 128 - i || !len)
485  return AVERROR_INVALIDDATA;
486 
487  while (len--) {
488  quant_table[i] = scale * v;
489  i++;
490  }
491  }
492 
493  for (i = 1; i < 128; i++)
494  quant_table[256 - i] = -quant_table[i];
495  quant_table[128] = -quant_table[127];
496 
497  return 2 * v - 1;
498 }
499 
501  int16_t quant_table[MAX_CONTEXT_INPUTS][256])
502 {
503  int i;
504  int context_count = 1;
505 
506  for (i = 0; i < 5; i++) {
507  int ret = read_quant_table(c, quant_table[i], context_count);
508  if (ret < 0)
509  return ret;
510  context_count *= ret;
511  if (context_count > 32768U) {
512  return AVERROR_INVALIDDATA;
513  }
514  }
515  return (context_count + 1) / 2;
516 }
517 
519 {
520  RangeCoder *const c = &f->c;
522  int i, j, k, ret;
523  uint8_t state2[32][CONTEXT_SIZE];
524  unsigned crc = 0;
525 
526  memset(state2, 128, sizeof(state2));
527  memset(state, 128, sizeof(state));
528 
530  ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
531 
532  f->version = get_symbol(c, state, 0);
533  if (f->version < 2) {
534  av_log(f->avctx, AV_LOG_ERROR, "Invalid version in global header\n");
535  return AVERROR_INVALIDDATA;
536  }
537  if (f->version > 2) {
538  c->bytestream_end -= 4;
539  f->micro_version = get_symbol(c, state, 0);
540  if (f->micro_version < 0)
541  return AVERROR_INVALIDDATA;
542  }
543  f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
544  if (f->ac > 1) {
545  for (i = 1; i < 256; i++)
546  f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
547  }
548 
549  f->colorspace = get_symbol(c, state, 0); //YUV cs type
550  f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
551  f->chroma_planes = get_rac(c, state);
552  f->chroma_h_shift = get_symbol(c, state, 0);
553  f->chroma_v_shift = get_symbol(c, state, 0);
554  f->transparency = get_rac(c, state);
555  f->plane_count = 1 + (f->chroma_planes || f->version<4) + f->transparency;
556  f->num_h_slices = 1 + get_symbol(c, state, 0);
557  f->num_v_slices = 1 + get_symbol(c, state, 0);
558 
559  if (f->chroma_h_shift > 4U || f->chroma_v_shift > 4U) {
560  av_log(f->avctx, AV_LOG_ERROR, "chroma shift parameters %d %d are invalid\n",
562  return AVERROR_INVALIDDATA;
563  }
564 
565  if (f->num_h_slices > (unsigned)f->width || !f->num_h_slices ||
566  f->num_v_slices > (unsigned)f->height || !f->num_v_slices
567  ) {
568  av_log(f->avctx, AV_LOG_ERROR, "slice count invalid\n");
569  return AVERROR_INVALIDDATA;
570  }
571 
572  f->quant_table_count = get_symbol(c, state, 0);
573  if (f->quant_table_count > (unsigned)MAX_QUANT_TABLES || !f->quant_table_count) {
574  av_log(f->avctx, AV_LOG_ERROR, "quant table count %d is invalid\n", f->quant_table_count);
575  f->quant_table_count = 0;
576  return AVERROR_INVALIDDATA;
577  }
578 
579  for (i = 0; i < f->quant_table_count; i++) {
580  f->context_count[i] = read_quant_tables(c, f->quant_tables[i]);
581  if (f->context_count[i] < 0) {
582  av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
583  return AVERROR_INVALIDDATA;
584  }
585  }
586  if ((ret = ff_ffv1_allocate_initial_states(f)) < 0)
587  return ret;
588 
589  for (i = 0; i < f->quant_table_count; i++)
590  if (get_rac(c, state)) {
591  for (j = 0; j < f->context_count[i]; j++)
592  for (k = 0; k < CONTEXT_SIZE; k++) {
593  int pred = j ? f->initial_states[i][j - 1][k] : 128;
594  f->initial_states[i][j][k] =
595  (pred + get_symbol(c, state2[k], 1)) & 0xFF;
596  }
597  }
598 
599  if (f->version > 2) {
600  f->ec = get_symbol(c, state, 0);
601  if (f->micro_version > 2)
602  f->intra = get_symbol(c, state, 0);
603  }
604 
605  if (f->version > 2) {
606  unsigned v;
609  if (v || f->avctx->extradata_size < 4) {
610  av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", v);
611  return AVERROR_INVALIDDATA;
612  }
613  crc = AV_RB32(f->avctx->extradata + f->avctx->extradata_size - 4);
614  }
615 
616  if (f->avctx->debug & FF_DEBUG_PICT_INFO)
618  "global: ver:%d.%d, coder:%d, colorspace: %d bpr:%d chroma:%d(%d:%d), alpha:%d slices:%dx%d qtabs:%d ec:%d intra:%d CRC:0x%08X\n",
619  f->version, f->micro_version,
620  f->ac,
621  f->colorspace,
624  f->transparency,
625  f->num_h_slices, f->num_v_slices,
627  f->ec,
628  f->intra,
629  crc
630  );
631  return 0;
632 }
633 
634 static int read_header(FFV1Context *f)
635 {
637  int i, j, context_count = -1; //-1 to avoid warning
638  RangeCoder *const c = &f->slice_context[0]->c;
639 
640  memset(state, 128, sizeof(state));
641 
642  if (f->version < 2) {
644  unsigned v= get_symbol(c, state, 0);
645  if (v >= 2) {
646  av_log(f->avctx, AV_LOG_ERROR, "invalid version %d in ver01 header\n", v);
647  return AVERROR_INVALIDDATA;
648  }
649  f->version = v;
650  f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
651  if (f->ac > 1) {
652  for (i = 1; i < 256; i++)
653  f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
654  }
655 
656  colorspace = get_symbol(c, state, 0); //YUV cs type
657  bits_per_raw_sample = f->version > 0 ? get_symbol(c, state, 0) : f->avctx->bits_per_raw_sample;
658  chroma_planes = get_rac(c, state);
659  chroma_h_shift = get_symbol(c, state, 0);
660  chroma_v_shift = get_symbol(c, state, 0);
661  transparency = get_rac(c, state);
662  if (colorspace == 0 && f->avctx->skip_alpha)
663  transparency = 0;
664 
665  if (f->plane_count) {
666  if (colorspace != f->colorspace ||
667  bits_per_raw_sample != f->avctx->bits_per_raw_sample ||
668  chroma_planes != f->chroma_planes ||
669  chroma_h_shift != f->chroma_h_shift ||
670  chroma_v_shift != f->chroma_v_shift ||
671  transparency != f->transparency) {
672  av_log(f->avctx, AV_LOG_ERROR, "Invalid change of global parameters\n");
673  return AVERROR_INVALIDDATA;
674  }
675  }
676 
677  if (chroma_h_shift > 4U || chroma_v_shift > 4U) {
678  av_log(f->avctx, AV_LOG_ERROR, "chroma shift parameters %d %d are invalid\n",
679  chroma_h_shift, chroma_v_shift);
680  return AVERROR_INVALIDDATA;
681  }
682 
683  f->colorspace = colorspace;
689 
690  f->plane_count = 2 + f->transparency;
691  }
692 
693  if (f->colorspace == 0) {
694  if (!f->transparency && !f->chroma_planes) {
695  if (f->avctx->bits_per_raw_sample <= 8)
697  else
699  } else if (f->avctx->bits_per_raw_sample<=8 && !f->transparency) {
700  switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
701  case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P; break;
702  case 0x01: f->avctx->pix_fmt = AV_PIX_FMT_YUV440P; break;
703  case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P; break;
704  case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P; break;
705  case 0x20: f->avctx->pix_fmt = AV_PIX_FMT_YUV411P; break;
706  case 0x22: f->avctx->pix_fmt = AV_PIX_FMT_YUV410P; break;
707  }
708  } else if (f->avctx->bits_per_raw_sample <= 8 && f->transparency) {
709  switch(16*f->chroma_h_shift + f->chroma_v_shift) {
710  case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P; break;
711  case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P; break;
712  case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P; break;
713  }
714  } else if (f->avctx->bits_per_raw_sample == 9 && !f->transparency) {
715  f->packed_at_lsb = 1;
716  switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
717  case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P9; break;
718  case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P9; break;
719  case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P9; break;
720  }
721  } else if (f->avctx->bits_per_raw_sample == 9 && f->transparency) {
722  f->packed_at_lsb = 1;
723  switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
724  case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P9; break;
725  case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P9; break;
726  case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P9; break;
727  }
728  } else if (f->avctx->bits_per_raw_sample == 10 && !f->transparency) {
729  f->packed_at_lsb = 1;
730  switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
731  case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P10; break;
732  case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P10; break;
733  case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P10; break;
734  }
735  } else if (f->avctx->bits_per_raw_sample == 10 && f->transparency) {
736  f->packed_at_lsb = 1;
737  switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
738  case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P10; break;
739  case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P10; break;
740  case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P10; break;
741  }
742  } else if (f->avctx->bits_per_raw_sample == 16 && !f->transparency){
743  switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
744  case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P16; break;
745  case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P16; break;
746  case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P16; break;
747  }
748  } else if (f->avctx->bits_per_raw_sample == 16 && f->transparency){
749  switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
750  case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P16; break;
751  case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P16; break;
752  case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P16; break;
753  }
754  }
755  } else if (f->colorspace == 1) {
756  if (f->chroma_h_shift || f->chroma_v_shift) {
758  "chroma subsampling not supported in this colorspace\n");
759  return AVERROR(ENOSYS);
760  }
761  if ( f->avctx->bits_per_raw_sample == 9)
763  else if (f->avctx->bits_per_raw_sample == 10)
765  else if (f->avctx->bits_per_raw_sample == 12)
767  else if (f->avctx->bits_per_raw_sample == 14)
769  else
771  else f->avctx->pix_fmt = AV_PIX_FMT_0RGB32;
772  } else {
773  av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
774  return AVERROR(ENOSYS);
775  }
776  if (f->avctx->pix_fmt == AV_PIX_FMT_NONE) {
777  av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
778  return AVERROR(ENOSYS);
779  }
780 
781  ff_dlog(f->avctx, "%d %d %d\n",
783  if (f->version < 2) {
784  context_count = read_quant_tables(c, f->quant_table);
785  if (context_count < 0) {
786  av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
787  return AVERROR_INVALIDDATA;
788  }
790  } else if (f->version < 3) {
791  f->slice_count = get_symbol(c, state, 0);
792  } else {
793  const uint8_t *p = c->bytestream_end;
794  for (f->slice_count = 0;
795  f->slice_count < MAX_SLICES && 3 + 5*!!f->ec < p - c->bytestream_start;
796  f->slice_count++) {
797  int trailer = 3 + 5*!!f->ec;
798  int size = AV_RB24(p-trailer);
799  if (size + trailer > p - c->bytestream_start)
800  break;
801  p -= size + trailer;
802  }
803  }
804  if (f->slice_count > (unsigned)MAX_SLICES || f->slice_count <= 0 || f->slice_count > f->max_slice_count) {
805  av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid (max=%d)\n", f->slice_count, f->max_slice_count);
806  return AVERROR_INVALIDDATA;
807  }
808 
809  for (j = 0; j < f->slice_count; j++) {
810  FFV1Context *fs = f->slice_context[j];
811  fs->ac = f->ac;
812  fs->packed_at_lsb = f->packed_at_lsb;
813 
814  fs->slice_damaged = 0;
815 
816  if (f->version == 2) {
817  fs->slice_x = get_symbol(c, state, 0) * f->width ;
818  fs->slice_y = get_symbol(c, state, 0) * f->height;
819  fs->slice_width = (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
820  fs->slice_height = (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
821 
822  fs->slice_x /= f->num_h_slices;
823  fs->slice_y /= f->num_v_slices;
824  fs->slice_width = fs->slice_width / f->num_h_slices - fs->slice_x;
825  fs->slice_height = fs->slice_height / f->num_v_slices - fs->slice_y;
826  if ((unsigned)fs->slice_width > f->width ||
827  (unsigned)fs->slice_height > f->height)
828  return AVERROR_INVALIDDATA;
829  if ( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
830  || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
831  return AVERROR_INVALIDDATA;
832  }
833 
834  for (i = 0; i < f->plane_count; i++) {
835  PlaneContext *const p = &fs->plane[i];
836 
837  if (f->version == 2) {
838  int idx = get_symbol(c, state, 0);
839  if (idx > (unsigned)f->quant_table_count) {
841  "quant_table_index out of range\n");
842  return AVERROR_INVALIDDATA;
843  }
844  p->quant_table_index = idx;
845  memcpy(p->quant_table, f->quant_tables[idx],
846  sizeof(p->quant_table));
847  context_count = f->context_count[idx];
848  } else {
849  memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
850  }
851 
852  if (f->version <= 2) {
853  av_assert0(context_count >= 0);
854  if (p->context_count < context_count) {
855  av_freep(&p->state);
856  av_freep(&p->vlc_state);
857  }
859  }
860  }
861  }
862  return 0;
863 }
864 
866 {
867  FFV1Context *f = avctx->priv_data;
868  int ret;
869 
870  if ((ret = ff_ffv1_common_init(avctx)) < 0)
871  return ret;
872 
873  if (avctx->extradata && (ret = read_extra_header(f)) < 0)
874  return ret;
875 
876  if ((ret = ff_ffv1_init_slice_contexts(f)) < 0)
877  return ret;
878 
879  avctx->internal->allocate_progress = 1;
880 
881  return 0;
882 }
883 
884 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
885 {
886  uint8_t *buf = avpkt->data;
887  int buf_size = avpkt->size;
888  FFV1Context *f = avctx->priv_data;
889  RangeCoder *const c = &f->slice_context[0]->c;
890  int i, ret;
891  uint8_t keystate = 128;
892  uint8_t *buf_p;
893  AVFrame *p;
894 
895  if (f->last_picture.f)
898 
899  f->cur = p = f->picture.f;
900 
901  if (f->version < 3 && avctx->field_order > AV_FIELD_PROGRESSIVE) {
902  /* we have interlaced material flagged in container */
903  p->interlaced_frame = 1;
904  if (avctx->field_order == AV_FIELD_TT || avctx->field_order == AV_FIELD_TB)
905  p->top_field_first = 1;
906  }
907 
908  f->avctx = avctx;
909  ff_init_range_decoder(c, buf, buf_size);
910  ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
911 
912  p->pict_type = AV_PICTURE_TYPE_I; //FIXME I vs. P
913  if (get_rac(c, &keystate)) {
914  p->key_frame = 1;
915  f->key_frame_ok = 0;
916  if ((ret = read_header(f)) < 0)
917  return ret;
918  f->key_frame_ok = 1;
919  } else {
920  if (!f->key_frame_ok) {
921  av_log(avctx, AV_LOG_ERROR,
922  "Cannot decode non-keyframe without valid keyframe\n");
923  return AVERROR_INVALIDDATA;
924  }
925  p->key_frame = 0;
926  }
927 
928  if ((ret = ff_thread_get_buffer(avctx, &f->picture, AV_GET_BUFFER_FLAG_REF)) < 0)
929  return ret;
930 
931  if (avctx->debug & FF_DEBUG_PICT_INFO)
932  av_log(avctx, AV_LOG_DEBUG, "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
933  f->version, p->key_frame, f->ac, f->ec, f->slice_count, f->avctx->bits_per_raw_sample);
934 
935  ff_thread_finish_setup(avctx);
936 
937  buf_p = buf + buf_size;
938  for (i = f->slice_count - 1; i >= 0; i--) {
939  FFV1Context *fs = f->slice_context[i];
940  int trailer = 3 + 5*!!f->ec;
941  int v;
942 
943  if (i || f->version > 2) v = AV_RB24(buf_p-trailer) + trailer;
944  else v = buf_p - c->bytestream_start;
945  if (buf_p - c->bytestream_start < v) {
946  av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
947  ff_thread_report_progress(&f->picture, INT_MAX, 0);
948  return AVERROR_INVALIDDATA;
949  }
950  buf_p -= v;
951 
952  if (f->ec) {
953  unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, v);
954  if (crc) {
955  int64_t ts = avpkt->pts != AV_NOPTS_VALUE ? avpkt->pts : avpkt->dts;
956  av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!", crc);
957  if (ts != AV_NOPTS_VALUE && avctx->pkt_timebase.num) {
958  av_log(f->avctx, AV_LOG_ERROR, "at %f seconds\n", ts*av_q2d(avctx->pkt_timebase));
959  } else if (ts != AV_NOPTS_VALUE) {
960  av_log(f->avctx, AV_LOG_ERROR, "at %"PRId64"\n", ts);
961  } else {
962  av_log(f->avctx, AV_LOG_ERROR, "\n");
963  }
964  fs->slice_damaged = 1;
965  }
966  if (avctx->debug & FF_DEBUG_PICT_INFO) {
967  av_log(avctx, AV_LOG_DEBUG, "slice %d, CRC: 0x%08X\n", i, AV_RB32(buf_p + v - 4));
968  }
969  }
970 
971  if (i) {
972  ff_init_range_decoder(&fs->c, buf_p, v);
973  } else
974  fs->c.bytestream_end = buf_p + v;
975 
976  fs->avctx = avctx;
977  fs->cur = p;
978  }
979 
980  avctx->execute(avctx,
981  decode_slice,
982  &f->slice_context[0],
983  NULL,
984  f->slice_count,
985  sizeof(void*));
986 
987  for (i = f->slice_count - 1; i >= 0; i--) {
988  FFV1Context *fs = f->slice_context[i];
989  int j;
990  if (fs->slice_damaged && f->last_picture.f->data[0]) {
991  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
992  const uint8_t *src[4];
993  uint8_t *dst[4];
994  ff_thread_await_progress(&f->last_picture, INT_MAX, 0);
995  for (j = 0; j < desc->nb_components; j++) {
996  int sh = (j == 1 || j == 2) ? f->chroma_h_shift : 0;
997  int sv = (j == 1 || j == 2) ? f->chroma_v_shift : 0;
998  dst[j] = p->data[j] + p->linesize[j] *
999  (fs->slice_y >> sv) + (fs->slice_x >> sh);
1000  src[j] = f->last_picture.f->data[j] + f->last_picture.f->linesize[j] *
1001  (fs->slice_y >> sv) + (fs->slice_x >> sh);
1002 
1003  }
1004  if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
1005  desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
1006  dst[1] = p->data[1];
1007  src[1] = f->last_picture.f->data[1];
1008  }
1009  av_image_copy(dst, p->linesize, src,
1010  f->last_picture.f->linesize,
1011  avctx->pix_fmt,
1012  fs->slice_width,
1013  fs->slice_height);
1014  }
1015  }
1016  ff_thread_report_progress(&f->picture, INT_MAX, 0);
1017 
1018  f->picture_number++;
1019 
1020  if (f->last_picture.f)
1022  f->cur = NULL;
1023  if ((ret = av_frame_ref(data, f->picture.f)) < 0)
1024  return ret;
1025 
1026  *got_frame = 1;
1027 
1028  return buf_size;
1029 }
1030 
1032 {
1033  FFV1Context *f = avctx->priv_data;
1034  int i, ret;
1035 
1036  f->picture.f = NULL;
1037  f->last_picture.f = NULL;
1038  f->sample_buffer = NULL;
1039  f->max_slice_count = 0;
1040  f->slice_count = 0;
1041 
1042  for (i = 0; i < f->quant_table_count; i++) {
1043  av_assert0(f->version > 1);
1044  f->initial_states[i] = av_memdup(f->initial_states[i],
1045  f->context_count[i] * sizeof(*f->initial_states[i]));
1046  }
1047 
1048  f->picture.f = av_frame_alloc();
1049  f->last_picture.f = av_frame_alloc();
1050 
1051  if ((ret = ff_ffv1_init_slice_contexts(f)) < 0)
1052  return ret;
1053 
1054  return 0;
1055 }
1056 
1057 static void copy_fields(FFV1Context *fsdst, FFV1Context *fssrc, FFV1Context *fsrc)
1058 {
1059  fsdst->version = fsrc->version;
1060  fsdst->micro_version = fsrc->micro_version;
1061  fsdst->chroma_planes = fsrc->chroma_planes;
1062  fsdst->chroma_h_shift = fsrc->chroma_h_shift;
1063  fsdst->chroma_v_shift = fsrc->chroma_v_shift;
1064  fsdst->transparency = fsrc->transparency;
1065  fsdst->plane_count = fsrc->plane_count;
1066  fsdst->ac = fsrc->ac;
1067  fsdst->colorspace = fsrc->colorspace;
1068 
1069  fsdst->ec = fsrc->ec;
1070  fsdst->intra = fsrc->intra;
1071  fsdst->slice_damaged = fssrc->slice_damaged;
1072  fsdst->key_frame_ok = fsrc->key_frame_ok;
1073 
1075  fsdst->packed_at_lsb = fsrc->packed_at_lsb;
1076  fsdst->slice_count = fsrc->slice_count;
1077  if (fsrc->version<3){
1078  fsdst->slice_x = fssrc->slice_x;
1079  fsdst->slice_y = fssrc->slice_y;
1080  fsdst->slice_width = fssrc->slice_width;
1081  fsdst->slice_height = fssrc->slice_height;
1082  }
1083 }
1084 
1086 {
1087  FFV1Context *fsrc = src->priv_data;
1088  FFV1Context *fdst = dst->priv_data;
1089  int i, ret;
1090 
1091  if (dst == src)
1092  return 0;
1093 
1094  {
1098  memcpy(initial_states, fdst->initial_states, sizeof(fdst->initial_states));
1099  memcpy(slice_context, fdst->slice_context , sizeof(fdst->slice_context));
1100 
1101  memcpy(fdst, fsrc, sizeof(*fdst));
1102  memcpy(fdst->initial_states, initial_states, sizeof(fdst->initial_states));
1103  memcpy(fdst->slice_context, slice_context , sizeof(fdst->slice_context));
1104  fdst->picture = picture;
1105  fdst->last_picture = last_picture;
1106  for (i = 0; i<fdst->num_h_slices * fdst->num_v_slices; i++) {
1107  FFV1Context *fssrc = fsrc->slice_context[i];
1108  FFV1Context *fsdst = fdst->slice_context[i];
1109  copy_fields(fsdst, fssrc, fsrc);
1110  }
1111  av_assert0(!fdst->plane[0].state);
1112  av_assert0(!fdst->sample_buffer);
1113  }
1114 
1115  av_assert1(fdst->max_slice_count == fsrc->max_slice_count);
1116 
1117 
1118  ff_thread_release_buffer(dst, &fdst->picture);
1119  if (fsrc->picture.f->data[0]) {
1120  if ((ret = ff_thread_ref_frame(&fdst->picture, &fsrc->picture)) < 0)
1121  return ret;
1122  }
1123 
1124  fdst->fsrc = fsrc;
1125 
1126  return 0;
1127 }
1128 
1130  .name = "ffv1",
1131  .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
1132  .type = AVMEDIA_TYPE_VIDEO,
1133  .id = AV_CODEC_ID_FFV1,
1134  .priv_data_size = sizeof(FFV1Context),
1135  .init = decode_init,
1136  .close = ff_ffv1_close,
1137  .decode = decode_frame,
1139  .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1140  .capabilities = AV_CODEC_CAP_DR1 /*| AV_CODEC_CAP_DRAW_HORIZ_BAND*/ |
1142 };
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition: pixdesc.h:115
static av_always_inline int fold(int diff, int bits)
Definition: ffv1.h:142
#define NULL
Definition: coverity.c:32
const uint8_t ff_log2_run[41]
Definition: bitstream.c:39
float v
#define AV_PIX_FMT_YUVA422P16
Definition: pixfmt.h:415
const char * s
Definition: avisynth_c.h:631
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AV_PIX_FMT_YUVA422P9
Definition: pixfmt.h:409
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2129
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
#define AV_PIX_FMT_YUVA420P10
Definition: pixfmt.h:411
#define AV_PIX_FMT_YUVA422P10
Definition: pixfmt.h:412
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:68
misc image utilities
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:261
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
AVFrame * f
Definition: thread.h:36
int16_t quant_table[MAX_CONTEXT_INPUTS][256]
Definition: ffv1.h:64
int quant_table_count
Definition: ffv1.h:117
else temp
Definition: vf_mcdeint.c:257
const char * g
Definition: vf_curves.c:108
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static int decode_slice(AVCodecContext *c, void *arg)
Definition: ffv1dec.c:361
int slice_height
Definition: ffv1.h:125
#define MAX_CONTEXT_INPUTS
Definition: ffv1.h:54
int16_t * sample_buffer
Definition: ffv1.h:106
int version
Definition: ffv1.h:82
int micro_version
Definition: ffv1.h:83
Range coder.
uint8_t * bytestream_end
Definition: rangecoder.h:44
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:1434
const char * b
Definition: vf_curves.c:109
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:396
static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale)
Definition: ffv1dec.c:473
#define AV_RB24
Definition: intreadwrite.h:64
static av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state, int is_signed)
Definition: ffv1dec.c:42
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1732
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: ffv1dec.c:884
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:3013
FF Video Codec 1 (a lossless codec)
void ff_thread_await_progress(ThreadFrame *f, int n, int field)
Wait for earlier decoding threads to finish reference pictures.
static av_always_inline void predict(PredictorState *ps, float *coef, int output_enable)
Definition: aacdec.c:173
#define sample
int height
Definition: ffv1.h:84
AVCodec.
Definition: avcodec.h:3482
uint8_t one_state[256]
Definition: rangecoder.h:41
int slice_reset_contexts
Definition: ffv1.h:128
int slice_rct_by_coef
Definition: ffv1.h:130
int plane_count
Definition: ffv1.h:95
int slice_damaged
Definition: ffv1.h:110
ThreadFrame picture
Definition: ffv1.h:91
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:103
static int read_quant_tables(RangeCoder *c, int16_t quant_table[MAX_CONTEXT_INPUTS][256])
Definition: ffv1dec.c:500
uint8_t bits
Definition: crc.c:295
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:100
uint8_t
#define av_cold
Definition: attributes.h:74
static int get_rac(RangeCoder *c, uint8_t *const state)
Definition: rangecoder.h:115
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:135
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:63
AVOptions.
int8_t bias
Definition: ffv1.h:59
#define AV_RB32
Definition: intreadwrite.h:130
RangeCoder c
Definition: ffv1.h:77
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:366
#define emms_c()
Definition: internal.h:53
av_cold int ff_ffv1_common_init(AVCodecContext *avctx)
Definition: ffv1.c:42
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1627
#define AV_PIX_FMT_YUVA420P9
Definition: pixfmt.h:408
#define AV_PIX_FMT_GBRP9
Definition: pixfmt.h:395
int slice_y
Definition: ffv1.h:127
uint8_t(*[MAX_QUANT_TABLES] initial_states)[32]
Definition: ffv1.h:103
ThreadFrame last_picture
Definition: ffv1.h:91
av_cold int ff_ffv1_close(AVCodecContext *avctx)
Definition: ffv1.c:205
int av_image_check_sar(unsigned int w, unsigned int h, AVRational sar)
Check if the given sample aspect ratio of an image is valid.
Definition: imgutils.c:271
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:80
int coder_type
coder type
Definition: avcodec.h:2667
uint8_t * data
Definition: avcodec.h:1433
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:213
uint8_t count
Definition: ffv1.h:60
int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
Definition: utils.c:3774
#define ff_dlog(a,...)
bitstream reader API header.
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:393
static void decode_rgb_frame(FFV1Context *s, uint8_t *src[3], int w, int h, int stride[3])
Definition: ffv1dec.c:226
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:367
VlcState * vlc_state
Definition: ffv1.h:68
ptrdiff_t size
Definition: opengl_enc.c:101
void ff_thread_finish_setup(AVCodecContext *avctx)
If the codec defines update_thread_context(), call this when they are ready for the next thread to st...
#define AV_PIX_FMT_YUVA420P16
Definition: pixfmt.h:414
high precision timer, useful to profile code
#define av_log(a,...)
int bits_per_raw_sample
Definition: ffv1.h:113
int slice_width
Definition: ffv1.h:124
GetBitContext gb
Definition: ffv1.h:78
#define U(x)
Definition: vp56_arith.h:37
AVFrame * cur
Definition: ffv1.h:94
AVRational pkt_timebase
Timebase in which pkt_dts/pts and AVPacket.dts/pts are.
Definition: avcodec.h:3328
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:281
static int init_thread_copy(AVCodecContext *avctx)
Definition: ffv1dec.c:1031
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static int decode_slice_header(FFV1Context *f, FFV1Context *fs)
Definition: ffv1dec.c:282
void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
Wrapper around release_buffer() frame-for multithreaded codecs.
int16_t quant_tables[MAX_QUANT_TABLES][MAX_CONTEXT_INPUTS][256]
Definition: ffv1.h:100
#define AVERROR(e)
Definition: error.h:43
int skip_alpha
Skip processing alpha if supported by codec.
Definition: avcodec.h:3391
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:178
const char * r
Definition: vf_curves.c:107
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
int context_count
Definition: ffv1.h:66
#define AV_PIX_FMT_YUVA444P16
Definition: pixfmt.h:416
const char * arg
Definition: jacosubdec.c:66
simple assert() macros that are a bit more flexible than ISO C assert().
const char * name
Name of the codec implementation.
Definition: avcodec.h:3489
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:383
int ff_ffv1_allocate_initial_states(FFV1Context *f)
Definition: ffv1.c:162
#define MAX_SLICES
Definition: dxva2_hevc.c:28
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
Libavcodec external API header.
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:920
void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4], const uint8_t *src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Copy image in src_data to dst_data.
Definition: imgutils.c:307
static int get_vlc_symbol(GetBitContext *gb, VlcState *const state, int bits)
Definition: ffv1dec.c:71
uint8_t * bytestream
Definition: rangecoder.h:43
static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index)
Definition: ffv1dec.c:184
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:67
#define ONLY_IF_THREADS_ENABLED(x)
Define a function with only the non-default version specified.
Definition: internal.h:217
int ac
1=range coder <-> 0=golomb rice
Definition: ffv1.h:96
static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
Definition: ffv1dec.c:1085
int16_t quant_table[MAX_CONTEXT_INPUTS][256]
Definition: ffv1.h:99
int run_index
Definition: ffv1.h:104
Definition: ffv1.h:56
#define AV_PIX_FMT_YUV422P9
Definition: pixfmt.h:378
#define av_flatten
Definition: attributes.h:80
static av_noinline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed)
Definition: ffv1dec.c:66
uint8_t state_transition[256]
Definition: ffv1.h:102
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:71
static void copy_fields(FFV1Context *fsdst, FFV1Context *fssrc, FFV1Context *fsrc)
Definition: ffv1dec.c:1057
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:242
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:364
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
#define FFMIN(a, b)
Definition: common.h:92
float y
int num_h_slices
Definition: ffv1.h:123
#define MAX_QUANT_TABLES
Definition: ffv1.h:53
int colorspace
Definition: ffv1.h:105
#define FF_CEIL_RSHIFT(a, b)
Definition: common.h:57
#define AV_PIX_FMT_FLAG_PSEUDOPAL
The pixel format is "pseudo-paletted".
Definition: pixdesc.h:141
static float quant_table[96]
Definition: binkaudio.c:41
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
static struct @197 state
static int get_context(PlaneContext *p, int16_t *src, int16_t *last, int16_t *last2)
Definition: ffv1.h:164
static void update_vlc_state(VlcState *const state, const int v)
Definition: ffv1.h:186
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:356
int slice_count
Definition: ffv1.h:120
int max_slice_count
Definition: ffv1.h:121
void ff_build_rac_states(RangeCoder *c, int factor, int max_p)
Definition: rangecoder.c:62
av_cold int ff_ffv1_init_slice_contexts(FFV1Context *f)
Definition: ffv1.c:117
av_cold int ff_ffv1_init_slice_state(FFV1Context *f, FFV1Context *fs)
Definition: ffv1.c:67
#define AV_PIX_FMT_YUVA444P10
Definition: pixfmt.h:413
int ac_byte_count
number of bytes used for AC coding
Definition: ffv1.h:97
static av_always_inline void decode_line(FFV1Context *s, int w, int16_t *sample[2], int plane_index, int bits)
Definition: ffv1dec.c:101
int16_t drift
Definition: ffv1.h:57
int packed_at_lsb
Definition: ffv1.h:114
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:379
#define AV_PIX_FMT_GBRP14
Definition: pixfmt.h:398
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:634
static const float pred[4]
Definition: siprdata.h:259
void * av_memdup(const void *p, size_t size)
Duplicate the buffer p.
Definition: mem.c:299
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:391
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: avcodec.h:924
int context_count[MAX_QUANT_TABLES]
Definition: ffv1.h:101
AVS_Value src
Definition: avisynth_c.h:482
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:199
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:280
uint8_t flags
Definition: pixdesc.h:90
int debug
debug
Definition: avcodec.h:2852
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
main external API structure.
Definition: avcodec.h:1512
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:357
int intra
Definition: ffv1.h:109
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
Definition: frame.h:252
void * buf
Definition: avisynth_c.h:553
int extradata_size
Definition: avcodec.h:1628
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:305
BYTE int const BYTE int int int height
Definition: avisynth_c.h:676
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:380
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:69
rational number numerator/denominator
Definition: rational.h:43
av_cold void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size)
Definition: rangecoder.c:53
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:415
uint16_t step_minus1
Number of elements between 2 horizontally consecutive pixels minus 1.
Definition: pixdesc.h:40
int picture_number
Definition: ffv1.h:89
uint16_t error_sum
Definition: ffv1.h:58
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:377
int allocate_progress
Whether to allocate progress for frame threading.
Definition: internal.h:115
int key_frame_ok
Definition: ffv1.h:111
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:397
#define CONTEXT_SIZE
Definition: ffv1.h:51
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:381
int quant_table_index
Definition: ffv1.h:65
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
#define FF_DEBUG_PICT_INFO
Definition: avcodec.h:2853
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:342
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:521
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:105
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:63
Y , 8bpp.
Definition: pixfmt.h:71
common internal api header.
if(ret< 0)
Definition: vf_mcdeint.c:280
static double c[64]
void ff_ffv1_clear_slice_state(FFV1Context *f, FFV1Context *fs)
Definition: ffv1.c:177
#define AV_PIX_FMT_YUVA444P9
Definition: pixfmt.h:410
uint8_t(* state)[CONTEXT_SIZE]
Definition: ffv1.h:67
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:70
int den
denominator
Definition: rational.h:45
int slice_coding_mode
Definition: ffv1.h:129
uint8_t * bytestream_start
Definition: rangecoder.h:42
static av_cold int decode_init(AVCodecContext *avctx)
Definition: ffv1dec.c:865
void * priv_data
Definition: avcodec.h:1554
int chroma_h_shift
Definition: ffv1.h:86
PlaneContext plane[MAX_PLANES]
Definition: ffv1.h:98
int transparency
Definition: ffv1.h:87
static av_always_inline int diff(const uint32_t a, const uint32_t b)
#define av_free(p)
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:3083
struct FFV1Context * fsrc
Definition: ffv1.h:92
int chroma_v_shift
Definition: ffv1.h:86
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:372
int len
int chroma_planes
Definition: ffv1.h:85
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:1562
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:237
struct FFV1Context * slice_context[MAX_SLICES]
Definition: ffv1.h:119
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1432
#define av_noinline
Definition: attributes.h:54
#define av_freep(p)
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:101
enum AVFieldOrder field_order
Field order.
Definition: avcodec.h:2269
#define av_always_inline
Definition: attributes.h:37
#define av_malloc_array(a, b)
#define FFSWAP(type, a, b)
Definition: common.h:95
int ec
Definition: ffv1.h:108
enum AVColorSpace colorspace
Definition: dirac.c:102
static int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len)
read signed golomb rice code (ffv1).
Definition: golomb.h:367
int num_v_slices
Definition: ffv1.h:122
exp golomb vlc stuff
This structure stores compressed data.
Definition: avcodec.h:1410
static int read_extra_header(FFV1Context *f)
Definition: ffv1dec.c:518
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:1216
AVCodecContext * avctx
Definition: ffv1.h:76
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:857
int slice_x
Definition: ffv1.h:126
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:392
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1426
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:240
AVCodec ff_ffv1_decoder
Definition: ffv1dec.c:1129
int width
Definition: ffv1.h:84
#define AV_PIX_FMT_0RGB32
Definition: pixfmt.h:361
static int width
int slice_rct_ry_coef
Definition: ffv1.h:131