FFmpeg  4.4.7
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 "avcodec.h"
34 #include "internal.h"
35 #include "get_bits.h"
36 #include "rangecoder.h"
37 #include "golomb.h"
38 #include "mathops.h"
39 #include "ffv1.h"
40 
42  int is_signed)
43 {
44  if (get_rac(c, state + 0))
45  return 0;
46  else {
47  int i, e;
48  unsigned a;
49  e = 0;
50  while (get_rac(c, state + 1 + FFMIN(e, 9))) { // 1..10
51  e++;
52  if (e > 31)
53  return AVERROR_INVALIDDATA;
54  }
55 
56  a = 1;
57  for (i = e - 1; i >= 0; i--)
58  a += a + get_rac(c, state + 22 + FFMIN(i, 9)); // 22..31
59 
60  e = -(is_signed && get_rac(c, state + 11 + FFMIN(e, 10))); // 11..21
61  return (a ^ e) - e;
62  }
63 }
64 
65 static av_noinline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed)
66 {
67  return get_symbol_inline(c, state, is_signed);
68 }
69 
70 static inline int get_vlc_symbol(GetBitContext *gb, VlcState *const state,
71  int bits)
72 {
73  int k, i, v, ret;
74 
75  i = state->count;
76  k = 0;
77  while (i < state->error_sum) { // FIXME: optimize
78  k++;
79  i += i;
80  }
81  if (k > bits) {
82  ff_dlog(NULL, "k-overflow bias:%d error:%d drift:%d count:%d k:%d",
83  state->bias, state->error_sum, state->drift, state->count, k);
84  k = bits;
85  }
86 
87  v = get_sr_golomb(gb, k, 12, bits);
88  ff_dlog(NULL, "v:%d bias:%d error:%d drift:%d count:%d k:%d",
89  v, state->bias, state->error_sum, state->drift, state->count, k);
90 
91  v ^= ((2 * state->drift + state->count) >> 31);
92 
93  ret = fold(v + state->bias, bits);
94 
96 
97  return ret;
98 }
99 
101 {
102  if (s->ac != AC_GOLOMB_RICE) {
103  RangeCoder *const c = &s->c;
104  if (c->overread > MAX_OVERREAD)
105  return AVERROR_INVALIDDATA;
106  } else {
107  if (get_bits_left(&s->gb) < 1)
108  return AVERROR_INVALIDDATA;
109  }
110  return 0;
111 }
112 
113 #define TYPE int16_t
114 #define RENAME(name) name
115 #include "ffv1dec_template.c"
116 #undef TYPE
117 #undef RENAME
118 
119 #define TYPE int32_t
120 #define RENAME(name) name ## 32
121 #include "ffv1dec_template.c"
122 
124  int w, int h, int stride, int plane_index,
125  int pixel_stride)
126 {
127  int x, y;
128  int16_t *sample[2];
129  sample[0] = s->sample_buffer + 3;
130  sample[1] = s->sample_buffer + w + 6 + 3;
131 
132  s->run_index = 0;
133 
134  memset(s->sample_buffer, 0, 2 * (w + 6) * sizeof(*s->sample_buffer));
135 
136  for (y = 0; y < h; y++) {
137  int16_t *temp = sample[0]; // FIXME: try a normal buffer
138 
139  sample[0] = sample[1];
140  sample[1] = temp;
141 
142  sample[1][-1] = sample[0][0];
143  sample[0][w] = sample[0][w - 1];
144 
145  if (s->avctx->bits_per_raw_sample <= 8) {
146  int ret = decode_line(s, w, sample, plane_index, 8);
147  if (ret < 0)
148  return ret;
149  for (x = 0; x < w; x++)
150  src[x*pixel_stride + stride * y] = sample[1][x];
151  } else {
152  int ret = decode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
153  if (ret < 0)
154  return ret;
155  if (s->packed_at_lsb) {
156  for (x = 0; x < w; x++) {
157  ((uint16_t*)(src + stride*y))[x*pixel_stride] = sample[1][x];
158  }
159  } else {
160  for (x = 0; x < w; x++) {
161  ((uint16_t*)(src + stride*y))[x*pixel_stride] = sample[1][x] << (16 - s->avctx->bits_per_raw_sample) | ((uint16_t **)sample)[1][x] >> (2 * s->avctx->bits_per_raw_sample - 16);
162  }
163  }
164  }
165  }
166  return 0;
167 }
168 
170 {
171  RangeCoder *c = &fs->c;
173  unsigned ps, i, context_count;
174  int sx, sy, sw, sh;
175 
176  memset(state, 128, sizeof(state));
177  sx = get_symbol(c, state, 0);
178  sy = get_symbol(c, state, 0);
179  sw = get_symbol(c, state, 0) + 1U;
180  sh = get_symbol(c, state, 0) + 1U;
181 
182  av_assert0(f->version > 2);
183 
184 
185  if (sx < 0 || sy < 0 || sw <= 0 || sh <= 0)
186  return AVERROR_INVALIDDATA;
187  if (sx > f->num_h_slices - sw || sy > f->num_v_slices - sh)
188  return AVERROR_INVALIDDATA;
189 
190  fs->slice_x = sx * (int64_t)f->width / f->num_h_slices;
191  fs->slice_y = sy * (int64_t)f->height / f->num_v_slices;
192  fs->slice_width = (sx + sw) * (int64_t)f->width / f->num_h_slices - fs->slice_x;
193  fs->slice_height = (sy + sh) * (int64_t)f->height / f->num_v_slices - fs->slice_y;
194 
195  av_assert0((unsigned)fs->slice_width <= f->width &&
196  (unsigned)fs->slice_height <= f->height);
197  av_assert0 ( (unsigned)fs->slice_x + (uint64_t)fs->slice_width <= f->width
198  && (unsigned)fs->slice_y + (uint64_t)fs->slice_height <= f->height);
199 
200  if (fs->ac == AC_GOLOMB_RICE && fs->slice_width >= (1<<23))
201  return AVERROR_INVALIDDATA;
202 
203  for (i = 0; i < f->plane_count; i++) {
204  PlaneContext * const p = &fs->plane[i];
205  int idx = get_symbol(c, state, 0);
206  if (idx >= (unsigned)f->quant_table_count) {
207  av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
208  return -1;
209  }
210  p->quant_table_index = idx;
211  memcpy(p->quant_table, f->quant_tables[idx], sizeof(p->quant_table));
212  context_count = f->context_count[idx];
213 
214  if (p->context_count < context_count) {
215  av_freep(&p->state);
216  av_freep(&p->vlc_state);
217  }
218  p->context_count = context_count;
219  }
220 
221  ps = get_symbol(c, state, 0);
222  if (ps == 1) {
223  f->cur->interlaced_frame = 1;
224  f->cur->top_field_first = 1;
225  } else if (ps == 2) {
226  f->cur->interlaced_frame = 1;
227  f->cur->top_field_first = 0;
228  } else if (ps == 3) {
229  f->cur->interlaced_frame = 0;
230  }
231  f->cur->sample_aspect_ratio.num = get_symbol(c, state, 0);
232  f->cur->sample_aspect_ratio.den = get_symbol(c, state, 0);
233 
234  if (av_image_check_sar(f->width, f->height,
235  f->cur->sample_aspect_ratio) < 0) {
236  av_log(f->avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
237  f->cur->sample_aspect_ratio.num,
238  f->cur->sample_aspect_ratio.den);
239  f->cur->sample_aspect_ratio = (AVRational){ 0, 1 };
240  }
241 
242  if (fs->version > 3) {
243  fs->slice_reset_contexts = get_rac(c, state);
244  fs->slice_coding_mode = get_symbol(c, state, 0);
245  if (fs->slice_coding_mode != 1) {
246  fs->slice_rct_by_coef = get_symbol(c, state, 0);
247  fs->slice_rct_ry_coef = get_symbol(c, state, 0);
248  if ((uint64_t)fs->slice_rct_by_coef + (uint64_t)fs->slice_rct_ry_coef > 4) {
249  av_log(f->avctx, AV_LOG_ERROR, "slice_rct_y_coef out of range\n");
250  return AVERROR_INVALIDDATA;
251  }
252  }
253  }
254 
255  return 0;
256 }
257 
258 static int decode_slice(AVCodecContext *c, void *arg)
259 {
260  FFV1Context *fs = *(void **)arg;
261  FFV1Context *f = fs->avctx->priv_data;
262  int width, height, x, y, ret;
263  const int ps = av_pix_fmt_desc_get(c->pix_fmt)->comp[0].step;
264  AVFrame * const p = f->cur;
265  int i, si;
266 
267  for( si=0; fs != f->slice_context[si]; si ++)
268  ;
269 
270  if(f->fsrc && !p->key_frame)
271  ff_thread_await_progress(&f->last_picture, si, 0);
272 
273  if(f->fsrc && !p->key_frame) {
274  FFV1Context *fssrc = f->fsrc->slice_context[si];
275  FFV1Context *fsdst = f->slice_context[si];
276  av_assert1(fsdst->plane_count == fssrc->plane_count);
277  av_assert1(fsdst == fs);
278 
279  if (!p->key_frame)
280  fsdst->slice_damaged |= fssrc->slice_damaged;
281 
282  for (i = 0; i < f->plane_count; i++) {
283  PlaneContext *psrc = &fssrc->plane[i];
284  PlaneContext *pdst = &fsdst->plane[i];
285 
286  av_free(pdst->state);
287  av_free(pdst->vlc_state);
288  memcpy(pdst, psrc, sizeof(*pdst));
289  pdst->state = NULL;
290  pdst->vlc_state = NULL;
291 
292  if (fssrc->ac) {
294  memcpy(pdst->state, psrc->state, CONTEXT_SIZE * psrc->context_count);
295  } else {
296  pdst->vlc_state = av_malloc_array(sizeof(*pdst->vlc_state), psrc->context_count);
297  memcpy(pdst->vlc_state, psrc->vlc_state, sizeof(*pdst->vlc_state) * psrc->context_count);
298  }
299  }
300  }
301 
302  fs->slice_rct_by_coef = 1;
303  fs->slice_rct_ry_coef = 1;
304 
305  if (f->version > 2) {
306  if (ff_ffv1_init_slice_state(f, fs) < 0)
307  return AVERROR(ENOMEM);
308  if (decode_slice_header(f, fs) < 0) {
309  fs->slice_x = fs->slice_y = fs->slice_height = fs->slice_width = 0;
310  fs->slice_damaged = 1;
311  return AVERROR_INVALIDDATA;
312  }
313  }
314  if ((ret = ff_ffv1_init_slice_state(f, fs)) < 0)
315  return ret;
316  if (f->cur->key_frame || fs->slice_reset_contexts) {
318  } else if (fs->slice_damaged) {
319  return AVERROR_INVALIDDATA;
320  }
321 
322  width = fs->slice_width;
323  height = fs->slice_height;
324  x = fs->slice_x;
325  y = fs->slice_y;
326 
327  if (fs->ac == AC_GOLOMB_RICE) {
328  if (f->version == 3 && f->micro_version > 1 || f->version > 3)
329  get_rac(&fs->c, (uint8_t[]) { 129 });
330  fs->ac_byte_count = f->version > 2 || (!x && !y) ? fs->c.bytestream - fs->c.bytestream_start - 1 : 0;
331  init_get_bits(&fs->gb,
332  fs->c.bytestream_start + fs->ac_byte_count,
333  (fs->c.bytestream_end - fs->c.bytestream_start - fs->ac_byte_count) * 8);
334  }
335 
336  av_assert1(width && height);
337  if (f->colorspace == 0 && (f->chroma_planes || !fs->transparency)) {
338  const int chroma_width = AV_CEIL_RSHIFT(width, f->chroma_h_shift);
339  const int chroma_height = AV_CEIL_RSHIFT(height, f->chroma_v_shift);
340  const int cx = x >> f->chroma_h_shift;
341  const int cy = y >> f->chroma_v_shift;
342  decode_plane(fs, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 1);
343 
344  if (f->chroma_planes) {
345  decode_plane(fs, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1, 1);
346  decode_plane(fs, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1, 1);
347  }
348  if (fs->transparency)
349  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, 1);
350  } else if (f->colorspace == 0) {
351  decode_plane(fs, p->data[0] + ps*x + y*p->linesize[0] , width, height, p->linesize[0], 0, 2);
352  decode_plane(fs, p->data[0] + ps*x + y*p->linesize[0] + 1, width, height, p->linesize[0], 1, 2);
353  } else if (f->use32bit) {
354  uint8_t *planes[4] = { p->data[0] + ps * x + y * p->linesize[0],
355  p->data[1] + ps * x + y * p->linesize[1],
356  p->data[2] + ps * x + y * p->linesize[2],
357  p->data[3] + ps * x + y * p->linesize[3] };
358  decode_rgb_frame32(fs, planes, width, height, p->linesize);
359  } else {
360  uint8_t *planes[4] = { p->data[0] + ps * x + y * p->linesize[0],
361  p->data[1] + ps * x + y * p->linesize[1],
362  p->data[2] + ps * x + y * p->linesize[2],
363  p->data[3] + ps * x + y * p->linesize[3] };
365  }
366  if (fs->ac != AC_GOLOMB_RICE && f->version > 2) {
367  int v;
368  get_rac(&fs->c, (uint8_t[]) { 129 });
369  v = fs->c.bytestream_end - fs->c.bytestream - 2 - 5*!!f->ec;
370  if (v) {
371  av_log(f->avctx, AV_LOG_ERROR, "bytestream end mismatching by %d\n", v);
372  fs->slice_damaged = 1;
373  }
374  }
375 
376  emms_c();
377 
378  ff_thread_report_progress(&f->picture, si, 0);
379 
380  return 0;
381 }
382 
383 static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale)
384 {
385  int v;
386  int i = 0;
388 
389  memset(state, 128, sizeof(state));
390 
391  for (v = 0; i < 128; v++) {
392  unsigned len = get_symbol(c, state, 0) + 1U;
393 
394  if (len > 128 - i || !len)
395  return AVERROR_INVALIDDATA;
396 
397  while (len--) {
398  quant_table[i] = scale * v;
399  i++;
400  }
401  }
402 
403  for (i = 1; i < 128; i++)
404  quant_table[256 - i] = -quant_table[i];
405  quant_table[128] = -quant_table[127];
406 
407  return 2 * v - 1;
408 }
409 
411  int16_t quant_table[MAX_CONTEXT_INPUTS][256])
412 {
413  int i;
414  int context_count = 1;
415 
416  for (i = 0; i < 5; i++) {
417  int ret = read_quant_table(c, quant_table[i], context_count);
418  if (ret < 0)
419  return ret;
420  context_count *= ret;
421  if (context_count > 32768U) {
422  return AVERROR_INVALIDDATA;
423  }
424  }
425  return (context_count + 1) / 2;
426 }
427 
429 {
430  RangeCoder *const c = &f->c;
432  int i, j, k, ret;
433  uint8_t state2[32][CONTEXT_SIZE];
434  unsigned crc = 0;
435 
436  memset(state2, 128, sizeof(state2));
437  memset(state, 128, sizeof(state));
438 
439  ff_init_range_decoder(c, f->avctx->extradata, f->avctx->extradata_size);
440  ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
441 
442  f->version = get_symbol(c, state, 0);
443  if (f->version < 2) {
444  av_log(f->avctx, AV_LOG_ERROR, "Invalid version in global header\n");
445  return AVERROR_INVALIDDATA;
446  }
447  if (f->version > 2) {
448  c->bytestream_end -= 4;
449  f->micro_version = get_symbol(c, state, 0);
450  if (f->micro_version < 0)
451  return AVERROR_INVALIDDATA;
452  }
453  f->ac = get_symbol(c, state, 0);
454 
455  if (f->ac == AC_RANGE_CUSTOM_TAB) {
456  for (i = 1; i < 256; i++)
457  f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
458  }
459 
460  f->colorspace = get_symbol(c, state, 0); //YUV cs type
461  f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
462  f->chroma_planes = get_rac(c, state);
463  f->chroma_h_shift = get_symbol(c, state, 0);
464  f->chroma_v_shift = get_symbol(c, state, 0);
465  f->transparency = get_rac(c, state);
466  f->plane_count = 1 + (f->chroma_planes || f->version<4) + f->transparency;
467  f->num_h_slices = 1 + get_symbol(c, state, 0);
468  f->num_v_slices = 1 + get_symbol(c, state, 0);
469 
470  if (f->chroma_h_shift > 4U || f->chroma_v_shift > 4U) {
471  av_log(f->avctx, AV_LOG_ERROR, "chroma shift parameters %d %d are invalid\n",
472  f->chroma_h_shift, f->chroma_v_shift);
473  return AVERROR_INVALIDDATA;
474  }
475 
476  if (f->num_h_slices > (unsigned)f->width || !f->num_h_slices ||
477  f->num_v_slices > (unsigned)f->height || !f->num_v_slices
478  ) {
479  av_log(f->avctx, AV_LOG_ERROR, "slice count invalid\n");
480  return AVERROR_INVALIDDATA;
481  }
482 
483  if (f->num_h_slices > MAX_SLICES / f->num_v_slices) {
484  av_log(f->avctx, AV_LOG_ERROR, "slice count unsupported\n");
485  return AVERROR_PATCHWELCOME;
486  }
487 
488  f->quant_table_count = get_symbol(c, state, 0);
489  if (f->quant_table_count > (unsigned)MAX_QUANT_TABLES || !f->quant_table_count) {
490  av_log(f->avctx, AV_LOG_ERROR, "quant table count %d is invalid\n", f->quant_table_count);
491  f->quant_table_count = 0;
492  return AVERROR_INVALIDDATA;
493  }
494 
495  for (i = 0; i < f->quant_table_count; i++) {
496  f->context_count[i] = read_quant_tables(c, f->quant_tables[i]);
497  if (f->context_count[i] < 0) {
498  av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
499  return AVERROR_INVALIDDATA;
500  }
501  }
502  if ((ret = ff_ffv1_allocate_initial_states(f)) < 0)
503  return ret;
504 
505  for (i = 0; i < f->quant_table_count; i++)
506  if (get_rac(c, state)) {
507  for (j = 0; j < f->context_count[i]; j++)
508  for (k = 0; k < CONTEXT_SIZE; k++) {
509  int pred = j ? f->initial_states[i][j - 1][k] : 128;
510  f->initial_states[i][j][k] =
511  (pred + get_symbol(c, state2[k], 1)) & 0xFF;
512  }
513  }
514 
515  if (f->version > 2) {
516  f->ec = get_symbol(c, state, 0);
517  if (f->micro_version > 2)
518  f->intra = get_symbol(c, state, 0);
519  }
520 
521  if (f->version > 2) {
522  unsigned v;
524  f->avctx->extradata, f->avctx->extradata_size);
525  if (v || f->avctx->extradata_size < 4) {
526  av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", v);
527  return AVERROR_INVALIDDATA;
528  }
529  crc = AV_RB32(f->avctx->extradata + f->avctx->extradata_size - 4);
530  }
531 
532  if (f->avctx->debug & FF_DEBUG_PICT_INFO)
533  av_log(f->avctx, AV_LOG_DEBUG,
534  "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",
535  f->version, f->micro_version,
536  f->ac,
537  f->colorspace,
538  f->avctx->bits_per_raw_sample,
539  f->chroma_planes, f->chroma_h_shift, f->chroma_v_shift,
540  f->transparency,
541  f->num_h_slices, f->num_v_slices,
542  f->quant_table_count,
543  f->ec,
544  f->intra,
545  crc
546  );
547  return 0;
548 }
549 
551 {
553  int i, j, context_count = -1; //-1 to avoid warning
554  RangeCoder *const c = &f->slice_context[0]->c;
555 
556  memset(state, 128, sizeof(state));
557 
558  if (f->version < 2) {
559  int chroma_planes, chroma_h_shift, chroma_v_shift, transparency, colorspace, bits_per_raw_sample;
560  unsigned v= get_symbol(c, state, 0);
561  if (v >= 2) {
562  av_log(f->avctx, AV_LOG_ERROR, "invalid version %d in ver01 header\n", v);
563  return AVERROR_INVALIDDATA;
564  }
565  f->version = v;
566  f->ac = get_symbol(c, state, 0);
567 
568  if (f->ac == AC_RANGE_CUSTOM_TAB) {
569  for (i = 1; i < 256; i++) {
570  int st = get_symbol(c, state, 1) + c->one_state[i];
571  if (st < 1 || st > 255) {
572  av_log(f->avctx, AV_LOG_ERROR, "invalid state transition %d\n", st);
573  return AVERROR_INVALIDDATA;
574  }
575  f->state_transition[i] = st;
576  }
577  }
578 
579  colorspace = get_symbol(c, state, 0); //YUV cs type
580  bits_per_raw_sample = f->version > 0 ? get_symbol(c, state, 0) : f->avctx->bits_per_raw_sample;
581  chroma_planes = get_rac(c, state);
582  chroma_h_shift = get_symbol(c, state, 0);
583  chroma_v_shift = get_symbol(c, state, 0);
584  transparency = get_rac(c, state);
585  if (colorspace == 0 && f->avctx->skip_alpha)
586  transparency = 0;
587 
588  if (f->plane_count) {
589  if (colorspace != f->colorspace ||
590  bits_per_raw_sample != f->avctx->bits_per_raw_sample ||
591  chroma_planes != f->chroma_planes ||
592  chroma_h_shift != f->chroma_h_shift ||
593  chroma_v_shift != f->chroma_v_shift ||
594  transparency != f->transparency) {
595  av_log(f->avctx, AV_LOG_ERROR, "Invalid change of global parameters\n");
596  return AVERROR_INVALIDDATA;
597  }
598  }
599 
600  if (chroma_h_shift > 4U || chroma_v_shift > 4U) {
601  av_log(f->avctx, AV_LOG_ERROR, "chroma shift parameters %d %d are invalid\n",
602  chroma_h_shift, chroma_v_shift);
603  return AVERROR_INVALIDDATA;
604  }
605 
606  f->colorspace = colorspace;
607  f->avctx->bits_per_raw_sample = bits_per_raw_sample;
608  f->chroma_planes = chroma_planes;
609  f->chroma_h_shift = chroma_h_shift;
610  f->chroma_v_shift = chroma_v_shift;
611  f->transparency = transparency;
612 
613  f->plane_count = 2 + f->transparency;
614  }
615 
616  if (f->colorspace == 0) {
617  if (!f->transparency && !f->chroma_planes) {
618  if (f->avctx->bits_per_raw_sample <= 8)
619  f->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
620  else if (f->avctx->bits_per_raw_sample == 9) {
621  f->packed_at_lsb = 1;
622  f->avctx->pix_fmt = AV_PIX_FMT_GRAY9;
623  } else if (f->avctx->bits_per_raw_sample == 10) {
624  f->packed_at_lsb = 1;
625  f->avctx->pix_fmt = AV_PIX_FMT_GRAY10;
626  } else if (f->avctx->bits_per_raw_sample == 12) {
627  f->packed_at_lsb = 1;
628  f->avctx->pix_fmt = AV_PIX_FMT_GRAY12;
629  } else if (f->avctx->bits_per_raw_sample == 16) {
630  f->packed_at_lsb = 1;
631  f->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
632  } else if (f->avctx->bits_per_raw_sample < 16) {
633  f->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
634  } else
635  return AVERROR(ENOSYS);
636  } else if (f->transparency && !f->chroma_planes) {
637  if (f->avctx->bits_per_raw_sample <= 8)
638  f->avctx->pix_fmt = AV_PIX_FMT_YA8;
639  else
640  return AVERROR(ENOSYS);
641  } else if (f->avctx->bits_per_raw_sample<=8 && !f->transparency) {
642  switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
643  case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P; break;
644  case 0x01: f->avctx->pix_fmt = AV_PIX_FMT_YUV440P; break;
645  case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P; break;
646  case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P; break;
647  case 0x20: f->avctx->pix_fmt = AV_PIX_FMT_YUV411P; break;
648  case 0x22: f->avctx->pix_fmt = AV_PIX_FMT_YUV410P; break;
649  }
650  } else if (f->avctx->bits_per_raw_sample <= 8 && f->transparency) {
651  switch(16*f->chroma_h_shift + f->chroma_v_shift) {
652  case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P; break;
653  case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P; break;
654  case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P; break;
655  }
656  } else if (f->avctx->bits_per_raw_sample == 9 && !f->transparency) {
657  f->packed_at_lsb = 1;
658  switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
659  case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P9; break;
660  case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P9; break;
661  case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P9; break;
662  }
663  } else if (f->avctx->bits_per_raw_sample == 9 && f->transparency) {
664  f->packed_at_lsb = 1;
665  switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
666  case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P9; break;
667  case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P9; break;
668  case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P9; break;
669  }
670  } else if (f->avctx->bits_per_raw_sample == 10 && !f->transparency) {
671  f->packed_at_lsb = 1;
672  switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
673  case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P10; break;
674  case 0x01: f->avctx->pix_fmt = AV_PIX_FMT_YUV440P10; break;
675  case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P10; break;
676  case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P10; break;
677  }
678  } else if (f->avctx->bits_per_raw_sample == 10 && f->transparency) {
679  f->packed_at_lsb = 1;
680  switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
681  case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P10; break;
682  case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P10; break;
683  case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P10; break;
684  }
685  } else if (f->avctx->bits_per_raw_sample == 12 && !f->transparency) {
686  f->packed_at_lsb = 1;
687  switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
688  case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P12; break;
689  case 0x01: f->avctx->pix_fmt = AV_PIX_FMT_YUV440P12; break;
690  case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P12; break;
691  case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P12; break;
692  }
693  } else if (f->avctx->bits_per_raw_sample == 14 && !f->transparency) {
694  f->packed_at_lsb = 1;
695  switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
696  case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P14; break;
697  case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P14; break;
698  case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P14; break;
699  }
700  } else if (f->avctx->bits_per_raw_sample == 16 && !f->transparency){
701  f->packed_at_lsb = 1;
702  switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
703  case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P16; break;
704  case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P16; break;
705  case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P16; break;
706  }
707  } else if (f->avctx->bits_per_raw_sample == 16 && f->transparency){
708  f->packed_at_lsb = 1;
709  switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
710  case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P16; break;
711  case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P16; break;
712  case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P16; break;
713  }
714  }
715  } else if (f->colorspace == 1) {
716  if (f->chroma_h_shift || f->chroma_v_shift) {
717  av_log(f->avctx, AV_LOG_ERROR,
718  "chroma subsampling not supported in this colorspace\n");
719  return AVERROR(ENOSYS);
720  }
721  if ( f->avctx->bits_per_raw_sample <= 8 && !f->transparency)
722  f->avctx->pix_fmt = AV_PIX_FMT_0RGB32;
723  else if (f->avctx->bits_per_raw_sample <= 8 && f->transparency)
724  f->avctx->pix_fmt = AV_PIX_FMT_RGB32;
725  else if (f->avctx->bits_per_raw_sample == 9 && !f->transparency)
726  f->avctx->pix_fmt = AV_PIX_FMT_GBRP9;
727  else if (f->avctx->bits_per_raw_sample == 10 && !f->transparency)
728  f->avctx->pix_fmt = AV_PIX_FMT_GBRP10;
729  else if (f->avctx->bits_per_raw_sample == 10 && f->transparency)
730  f->avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
731  else if (f->avctx->bits_per_raw_sample == 12 && !f->transparency)
732  f->avctx->pix_fmt = AV_PIX_FMT_GBRP12;
733  else if (f->avctx->bits_per_raw_sample == 12 && f->transparency)
734  f->avctx->pix_fmt = AV_PIX_FMT_GBRAP12;
735  else if (f->avctx->bits_per_raw_sample == 14 && !f->transparency)
736  f->avctx->pix_fmt = AV_PIX_FMT_GBRP14;
737  else if (f->avctx->bits_per_raw_sample == 16 && !f->transparency) {
738  f->avctx->pix_fmt = AV_PIX_FMT_GBRP16;
739  f->use32bit = 1;
740  }
741  else if (f->avctx->bits_per_raw_sample == 16 && f->transparency) {
742  f->avctx->pix_fmt = AV_PIX_FMT_GBRAP16;
743  f->use32bit = 1;
744  }
745  } else {
746  av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
747  return AVERROR(ENOSYS);
748  }
749  if (f->avctx->pix_fmt == AV_PIX_FMT_NONE) {
750  av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
751  return AVERROR(ENOSYS);
752  }
753 
754  ff_dlog(f->avctx, "%d %d %d\n",
755  f->chroma_h_shift, f->chroma_v_shift, f->avctx->pix_fmt);
756  if (f->version < 2) {
757  context_count = read_quant_tables(c, f->quant_table);
758  if (context_count < 0) {
759  av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
760  return AVERROR_INVALIDDATA;
761  }
762  f->slice_count = f->max_slice_count;
763  } else if (f->version < 3) {
764  f->slice_count = get_symbol(c, state, 0);
765  } else {
766  const uint8_t *p = c->bytestream_end;
767  for (f->slice_count = 0;
768  f->slice_count < MAX_SLICES && 3 + 5*!!f->ec < p - c->bytestream_start;
769  f->slice_count++) {
770  int trailer = 3 + 5*!!f->ec;
771  int size = AV_RB24(p-trailer);
772  if (size + trailer > p - c->bytestream_start)
773  break;
774  p -= size + trailer;
775  }
776  }
777  if (f->slice_count > (unsigned)MAX_SLICES || f->slice_count <= 0 || f->slice_count > f->max_slice_count) {
778  av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid (max=%d)\n", f->slice_count, f->max_slice_count);
779  return AVERROR_INVALIDDATA;
780  }
781 
782  for (j = 0; j < f->slice_count; j++) {
783  FFV1Context *fs = f->slice_context[j];
784  fs->ac = f->ac;
785  fs->packed_at_lsb = f->packed_at_lsb;
786 
787  fs->slice_damaged = 0;
788 
789  if (f->version == 2) {
790  int sx = get_symbol(c, state, 0);
791  int sy = get_symbol(c, state, 0);
792  int sw = get_symbol(c, state, 0) + 1U;
793  int sh = get_symbol(c, state, 0) + 1U;
794 
795  if (sx < 0 || sy < 0 || sw <= 0 || sh <= 0)
796  return AVERROR_INVALIDDATA;
797  if (sx > f->num_h_slices - sw || sy > f->num_v_slices - sh)
798  return AVERROR_INVALIDDATA;
799 
800  fs->slice_x = sx * (int64_t)f->width / f->num_h_slices;
801  fs->slice_y = sy * (int64_t)f->height / f->num_v_slices;
802  fs->slice_width = (sx + sw) * (int64_t)f->width / f->num_h_slices - fs->slice_x;
803  fs->slice_height = (sy + sh) * (int64_t)f->height / f->num_v_slices - fs->slice_y;
804 
805  av_assert0((unsigned)fs->slice_width <= f->width &&
806  (unsigned)fs->slice_height <= f->height);
807  av_assert0 ( (unsigned)fs->slice_x + (uint64_t)fs->slice_width <= f->width
808  && (unsigned)fs->slice_y + (uint64_t)fs->slice_height <= f->height);
809  }
810 
811  for (i = 0; i < f->plane_count; i++) {
812  PlaneContext *const p = &fs->plane[i];
813 
814  if (f->version == 2) {
815  int idx = get_symbol(c, state, 0);
816  if (idx >= (unsigned)f->quant_table_count) {
817  av_log(f->avctx, AV_LOG_ERROR,
818  "quant_table_index out of range\n");
819  return AVERROR_INVALIDDATA;
820  }
821  p->quant_table_index = idx;
822  memcpy(p->quant_table, f->quant_tables[idx],
823  sizeof(p->quant_table));
824  context_count = f->context_count[idx];
825  } else {
826  memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
827  }
828 
829  if (f->version <= 2) {
830  av_assert0(context_count >= 0);
831  if (p->context_count < context_count) {
832  av_freep(&p->state);
833  av_freep(&p->vlc_state);
834  }
835  p->context_count = context_count;
836  }
837  }
838  }
839  return 0;
840 }
841 
843 {
844  FFV1Context *f = avctx->priv_data;
845  int ret;
846 
847  if ((ret = ff_ffv1_common_init(avctx)) < 0)
848  return ret;
849 
850  if (avctx->extradata_size > 0 && (ret = read_extra_header(f)) < 0)
851  return ret;
852 
853  if ((ret = ff_ffv1_init_slice_contexts(f)) < 0)
854  return ret;
855 
856  return 0;
857 }
858 
859 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
860 {
861  uint8_t *buf = avpkt->data;
862  int buf_size = avpkt->size;
863  FFV1Context *f = avctx->priv_data;
864  RangeCoder *const c = &f->slice_context[0]->c;
865  int i, ret;
866  uint8_t keystate = 128;
867  uint8_t *buf_p;
868  AVFrame *p;
869 
870  if (f->last_picture.f)
871  ff_thread_release_buffer(avctx, &f->last_picture);
872  FFSWAP(ThreadFrame, f->picture, f->last_picture);
873 
874  f->cur = p = f->picture.f;
875 
876  if (f->version < 3 && avctx->field_order > AV_FIELD_PROGRESSIVE) {
877  /* we have interlaced material flagged in container */
878  p->interlaced_frame = 1;
879  if (avctx->field_order == AV_FIELD_TT || avctx->field_order == AV_FIELD_TB)
880  p->top_field_first = 1;
881  }
882 
883  f->avctx = avctx;
884  ff_init_range_decoder(c, buf, buf_size);
885  ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
886 
887  p->pict_type = AV_PICTURE_TYPE_I; //FIXME I vs. P
888  if (get_rac(c, &keystate)) {
889  p->key_frame = 1;
890  f->key_frame_ok = 0;
891  if ((ret = read_header(f)) < 0)
892  return ret;
893  f->key_frame_ok = 1;
894  } else {
895  if (!f->key_frame_ok) {
896  av_log(avctx, AV_LOG_ERROR,
897  "Cannot decode non-keyframe without valid keyframe\n");
898  return AVERROR_INVALIDDATA;
899  }
900  p->key_frame = 0;
901  }
902 
903  if ((ret = ff_thread_get_buffer(avctx, &f->picture, AV_GET_BUFFER_FLAG_REF)) < 0)
904  return ret;
905 
906  if (avctx->debug & FF_DEBUG_PICT_INFO)
907  av_log(avctx, AV_LOG_DEBUG, "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
908  f->version, p->key_frame, f->ac, f->ec, f->slice_count, f->avctx->bits_per_raw_sample);
909 
910  ff_thread_finish_setup(avctx);
911 
912  buf_p = buf + buf_size;
913  for (i = f->slice_count - 1; i >= 0; i--) {
914  FFV1Context *fs = f->slice_context[i];
915  int trailer = 3 + 5*!!f->ec;
916  int v;
917 
918  if (i || f->version > 2) {
919  if (trailer > buf_p - buf) v = INT_MAX;
920  else v = AV_RB24(buf_p-trailer) + trailer;
921  } else v = buf_p - c->bytestream_start;
922  if (buf_p - c->bytestream_start < v) {
923  av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
924  ff_thread_report_progress(&f->picture, INT_MAX, 0);
925  return AVERROR_INVALIDDATA;
926  }
927  buf_p -= v;
928 
929  if (f->ec) {
930  unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, v);
931  if (crc) {
932  int64_t ts = avpkt->pts != AV_NOPTS_VALUE ? avpkt->pts : avpkt->dts;
933  av_log(f->avctx, AV_LOG_ERROR, "slice CRC mismatch %X!", crc);
934  if (ts != AV_NOPTS_VALUE && avctx->pkt_timebase.num) {
935  av_log(f->avctx, AV_LOG_ERROR, "at %f seconds\n", ts*av_q2d(avctx->pkt_timebase));
936  } else if (ts != AV_NOPTS_VALUE) {
937  av_log(f->avctx, AV_LOG_ERROR, "at %"PRId64"\n", ts);
938  } else {
939  av_log(f->avctx, AV_LOG_ERROR, "\n");
940  }
941  fs->slice_damaged = 1;
942  }
943  if (avctx->debug & FF_DEBUG_PICT_INFO) {
944  av_log(avctx, AV_LOG_DEBUG, "slice %d, CRC: 0x%08"PRIX32"\n", i, AV_RB32(buf_p + v - 4));
945  }
946  }
947 
948  if (i) {
949  ff_init_range_decoder(&fs->c, buf_p, v);
950  } else
951  fs->c.bytestream_end = buf_p + v;
952 
953  fs->avctx = avctx;
954  fs->cur = p;
955  }
956 
957  avctx->execute(avctx,
958  decode_slice,
959  &f->slice_context[0],
960  NULL,
961  f->slice_count,
962  sizeof(void*));
963 
964  for (i = f->slice_count - 1; i >= 0; i--) {
965  FFV1Context *fs = f->slice_context[i];
966  int j;
967  if (fs->slice_damaged && f->last_picture.f->data[0]) {
969  const uint8_t *src[4];
970  uint8_t *dst[4];
971  ff_thread_await_progress(&f->last_picture, INT_MAX, 0);
972  for (j = 0; j < desc->nb_components; j++) {
973  int pixshift = desc->comp[j].depth > 8;
974  int sh = (j == 1 || j == 2) ? f->chroma_h_shift : 0;
975  int sv = (j == 1 || j == 2) ? f->chroma_v_shift : 0;
976  dst[j] = p->data[j] + p->linesize[j] *
977  (fs->slice_y >> sv) + ((fs->slice_x >> sh) << pixshift);
978  src[j] = f->last_picture.f->data[j] + f->last_picture.f->linesize[j] *
979  (fs->slice_y >> sv) + ((fs->slice_x >> sh) << pixshift);
980 
981  }
982  if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
983  desc->flags & FF_PSEUDOPAL) {
984  dst[1] = p->data[1];
985  src[1] = f->last_picture.f->data[1];
986  }
987  av_image_copy(dst, p->linesize, src,
988  f->last_picture.f->linesize,
989  avctx->pix_fmt,
990  fs->slice_width,
991  fs->slice_height);
992  }
993  }
994  ff_thread_report_progress(&f->picture, INT_MAX, 0);
995 
996  f->picture_number++;
997 
998  if (f->last_picture.f)
999  ff_thread_release_buffer(avctx, &f->last_picture);
1000  f->cur = NULL;
1001  if ((ret = av_frame_ref(data, f->picture.f)) < 0)
1002  return ret;
1003 
1004  *got_frame = 1;
1005 
1006  return buf_size;
1007 }
1008 
1009 static void copy_fields(FFV1Context *fsdst, FFV1Context *fssrc, FFV1Context *fsrc)
1010 {
1011  fsdst->version = fsrc->version;
1012  fsdst->micro_version = fsrc->micro_version;
1013  fsdst->chroma_planes = fsrc->chroma_planes;
1014  fsdst->chroma_h_shift = fsrc->chroma_h_shift;
1015  fsdst->chroma_v_shift = fsrc->chroma_v_shift;
1016  fsdst->transparency = fsrc->transparency;
1017  fsdst->plane_count = fsrc->plane_count;
1018  fsdst->ac = fsrc->ac;
1019  fsdst->colorspace = fsrc->colorspace;
1020 
1021  fsdst->ec = fsrc->ec;
1022  fsdst->intra = fsrc->intra;
1023  fsdst->slice_damaged = fssrc->slice_damaged;
1024  fsdst->key_frame_ok = fsrc->key_frame_ok;
1025 
1027  fsdst->packed_at_lsb = fsrc->packed_at_lsb;
1028  fsdst->slice_count = fsrc->slice_count;
1029  if (fsrc->version<3){
1030  fsdst->slice_x = fssrc->slice_x;
1031  fsdst->slice_y = fssrc->slice_y;
1032  fsdst->slice_width = fssrc->slice_width;
1033  fsdst->slice_height = fssrc->slice_height;
1034  }
1035 }
1036 
1037 #if HAVE_THREADS
1038 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1039 {
1040  FFV1Context *fsrc = src->priv_data;
1041  FFV1Context *fdst = dst->priv_data;
1042  int i, ret;
1043 
1044  if (dst == src)
1045  return 0;
1046 
1047  {
1048  ThreadFrame picture = fdst->picture, last_picture = fdst->last_picture;
1049  uint8_t (*initial_states[MAX_QUANT_TABLES])[32];
1051  memcpy(initial_states, fdst->initial_states, sizeof(fdst->initial_states));
1052  memcpy(slice_context, fdst->slice_context , sizeof(fdst->slice_context));
1053 
1054  memcpy(fdst, fsrc, sizeof(*fdst));
1055  memcpy(fdst->initial_states, initial_states, sizeof(fdst->initial_states));
1056  memcpy(fdst->slice_context, slice_context , sizeof(fdst->slice_context));
1057  fdst->picture = picture;
1058  fdst->last_picture = last_picture;
1059  for (i = 0; i<fdst->num_h_slices * fdst->num_v_slices; i++) {
1060  FFV1Context *fssrc = fsrc->slice_context[i];
1061  FFV1Context *fsdst = fdst->slice_context[i];
1062  copy_fields(fsdst, fssrc, fsrc);
1063  }
1064  av_assert0(!fdst->plane[0].state);
1065  av_assert0(!fdst->sample_buffer);
1066  }
1067 
1069 
1070 
1071  ff_thread_release_buffer(dst, &fdst->picture);
1072  if (fsrc->picture.f->data[0]) {
1073  if ((ret = ff_thread_ref_frame(&fdst->picture, &fsrc->picture)) < 0)
1074  return ret;
1075  }
1076 
1077  fdst->fsrc = fsrc;
1078 
1079  return 0;
1080 }
1081 #endif
1082 
1084  .name = "ffv1",
1085  .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
1086  .type = AVMEDIA_TYPE_VIDEO,
1087  .id = AV_CODEC_ID_FFV1,
1088  .priv_data_size = sizeof(FFV1Context),
1089  .init = decode_init,
1090  .close = ff_ffv1_close,
1091  .decode = decode_frame,
1092  .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1093  .capabilities = AV_CODEC_CAP_DR1 /*| AV_CODEC_CAP_DRAW_HORIZ_BAND*/ |
1095  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1096 };
#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_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
Libavcodec external API header.
#define FF_DEBUG_PICT_INFO
Definition: avcodec.h:1628
#define AV_RB24
Definition: intreadwrite.h:64
#define AV_RB32
Definition: intreadwrite.h:130
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 @321 state
@ AV_FIELD_TT
Definition: codec_par.h:39
@ AV_FIELD_PROGRESSIVE
Definition: codec_par.h:38
@ AV_FIELD_TB
Definition: codec_par.h:41
#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 NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
Public header for CRC hash function implementation.
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
#define MAX_SLICES
Definition: dxva2_hevc.c:29
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
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
av_cold int ff_ffv1_init_slice_state(FFV1Context *f, FFV1Context *fs)
Definition: ffv1.c:66
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_CUSTOM_TAB
Definition: ffv1.h:57
#define MAX_QUANT_TABLES
Definition: ffv1.h:52
static av_always_inline int fold(int diff, int bits)
Definition: ffv1.h:151
#define MAX_CONTEXT_INPUTS
Definition: ffv1.h:53
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:550
static void copy_fields(FFV1Context *fsdst, FFV1Context *fssrc, FFV1Context *fsrc)
Definition: ffv1dec.c:1009
AVCodec ff_ffv1_decoder
Definition: ffv1dec.c:1083
static int read_quant_tables(RangeCoder *c, int16_t quant_table[MAX_CONTEXT_INPUTS][256])
Definition: ffv1dec.c:410
static int read_extra_header(FFV1Context *f)
Definition: ffv1dec.c:428
static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale)
Definition: ffv1dec.c:383
static int get_vlc_symbol(GetBitContext *gb, VlcState *const state, int bits)
Definition: ffv1dec.c:70
static av_noinline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed)
Definition: ffv1dec.c:65
static int decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index, int pixel_stride)
Definition: ffv1dec.c:123
static av_cold int decode_init(AVCodecContext *avctx)
Definition: ffv1dec.c:842
static int decode_slice(AVCodecContext *c, void *arg)
Definition: ffv1dec.c:258
static int decode_slice_header(FFV1Context *f, FFV1Context *fs)
Definition: ffv1dec.c:169
static av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state, int is_signed)
Definition: ffv1dec.c:41
static int is_input_end(FFV1Context *s)
Definition: ffv1dec.c:100
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: ffv1dec.c:859
static av_always_inline int RENAME() decode_line(FFV1Context *s, int w, TYPE *sample[2], int plane_index, int bits)
static int RENAME() decode_rgb_frame(FFV1Context *s, uint8_t *src[4], int w, int h, int stride[4])
#define sample
bitstream reader API header.
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:659
exp golomb vlc stuff
static int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len)
read signed golomb rice code (ffv1).
Definition: golomb.h:534
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:514
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: codec.h:112
@ AV_CODEC_ID_FFV1
Definition: codec_id.h:82
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:392
@ AV_CRC_32_IEEE
Definition: crc.h:53
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR(e)
Definition: error.h:43
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_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
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:422
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:322
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
misc image utilities
int i
Definition: input.c:407
static const int16_t quant_table[64]
Definition: intrax8.c:522
#define MAX_OVERREAD
Definition: lagarithrac.h:51
#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:66
int ff_thread_ref_frame(ThreadFrame *dst, const ThreadFrame *src)
Definition: utils.c:943
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_PSEUDOPAL
Definition: internal.h:299
#define ONLY_IF_THREADS_ENABLED(x)
Define a function with only the non-default version specified.
Definition: internal.h:156
#define emms_c()
Definition: internal.h:54
const char * desc
Definition: libsvtav1.c:79
uint8_t w
Definition: llviddspenc.c:39
static const struct @322 planes[]
int stride
Definition: mace.c:144
const char data[16]
Definition: mxf.c:142
AVOptions.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition: pixdesc.h:132
#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_GBRP12
Definition: pixfmt.h:416
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:396
#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
@ 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
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
FF_ENABLE_DEPRECATION_WARNINGS int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
void ff_thread_await_progress(ThreadFrame *f, int n, int field)
Wait for earlier decoding threads to finish reference pictures.
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...
void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
Wrapper around release_buffer() frame-for multithreaded codecs.
av_cold void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size)
Definition: rangecoder.c:53
void ff_build_rac_states(RangeCoder *c, int factor, int max_p)
Definition: rangecoder.c:68
Range coder.
static int get_rac(RangeCoder *c, uint8_t *const state)
Definition: rangecoder.h:126
static const float pred[4]
Definition: siprdata.h:259
main external API structure.
Definition: avcodec.h:536
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:746
int debug
debug
Definition: avcodec.h:1627
AVRational pkt_timebase
Timebase in which pkt_dts/pts and AVPacket.dts/pts are.
Definition: avcodec.h:2089
enum AVFieldOrder field_order
Field order.
Definition: avcodec.h:1193
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 extradata_size
Definition: avcodec.h:638
void * priv_data
Definition: avcodec.h:563
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
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 top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:470
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:465
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
This structure stores compressed data.
Definition: packet.h:346
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
Rational number (pair of numerator and denominator).
Definition: rational.h:58
int num
Numerator.
Definition: rational.h:59
int ac
1=range coder <-> 0=golomb rice
Definition: ffv1.h:100
int colorspace
Definition: ffv1.h:109
ThreadFrame picture
Definition: ffv1.h:95
int version
Definition: ffv1.h:86
int transparency
Definition: ffv1.h:91
int slice_damaged
Definition: ffv1.h:117
int num_v_slices
Definition: ffv1.h:130
int max_slice_count
Definition: ffv1.h:129
struct FFV1Context * fsrc
Definition: ffv1.h:96
int chroma_planes
Definition: ffv1.h:89
int num_h_slices
Definition: ffv1.h:131
int plane_count
Definition: ffv1.h:99
PlaneContext plane[MAX_PLANES]
Definition: ffv1.h:102
int slice_count
Definition: ffv1.h:128
int micro_version
Definition: ffv1.h:87
int bits_per_raw_sample
Definition: ffv1.h:121
ThreadFrame last_picture
Definition: ffv1.h:95
int16_t * sample_buffer
Definition: ffv1.h:110
struct FFV1Context * slice_context[MAX_SLICES]
Definition: ffv1.h:127
int chroma_h_shift
Definition: ffv1.h:90
int key_frame_ok
Definition: ffv1.h:118
int slice_x
Definition: ffv1.h:134
int packed_at_lsb
Definition: ffv1.h:122
int slice_height
Definition: ffv1.h:133
int chroma_v_shift
Definition: ffv1.h:90
int ec
Definition: ffv1.h:115
int slice_y
Definition: ffv1.h:135
int intra
Definition: ffv1.h:116
int slice_width
Definition: ffv1.h:132
uint8_t(*[MAX_QUANT_TABLES] initial_states)[32]
Definition: ffv1.h:107
VlcState * vlc_state
Definition: ffv1.h:72
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
uint8_t(* state)[CONTEXT_SIZE]
Definition: ffv1.h:71
AVFrame * f
Definition: thread.h:35
Definition: ffv1.h:60
#define av_free(p)
#define av_malloc_array(a, b)
#define ff_dlog(a,...)
#define av_freep(p)
#define av_log(a,...)
#define src
Definition: vp8dsp.c:255
enum AVPictureType last_picture
Definition: movenc.c:69
#define height
#define width
int size
else temp
Definition: vf_mcdeint.c:259
int len
uint8_t bits
Definition: vp3data.h:141
static double c[64]