ffv1dec.c
Go to the documentation of this file.
1 /*
2  * FFV1 decoder
3  *
4  * Copyright (c) 2003-2012 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of Libav.
7  *
8  * Libav is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * Libav is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
28 #include "libavutil/avassert.h"
29 #include "libavutil/pixdesc.h"
30 #include "libavutil/crc.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/imgutils.h"
33 #include "avcodec.h"
34 #include "internal.h"
35 #include "get_bits.h"
36 #include "put_bits.h"
37 #include "dsputil.h"
38 #include "rangecoder.h"
39 #include "golomb.h"
40 #include "mathops.h"
41 #include "ffv1.h"
42 
44  int is_signed)
45 {
46  if (get_rac(c, state + 0))
47  return 0;
48  else {
49  int i, e, a;
50  e = 0;
51  while (get_rac(c, state + 1 + FFMIN(e, 9))) // 1..10
52  e++;
53 
54  a = 1;
55  for (i = e - 1; i >= 0; i--)
56  a += a + get_rac(c, state + 22 + FFMIN(i, 9)); // 22..31
57 
58  e = -(is_signed && get_rac(c, state + 11 + FFMIN(e, 10))); // 11..21
59  return (a ^ e) - e;
60  }
61 }
62 
63 static av_noinline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed)
64 {
65  return get_symbol_inline(c, state, is_signed);
66 }
67 
68 static inline int get_vlc_symbol(GetBitContext *gb, VlcState *const state,
69  int bits)
70 {
71  int k, i, v, ret;
72 
73  i = state->count;
74  k = 0;
75  while (i < state->error_sum) { // FIXME: optimize
76  k++;
77  i += i;
78  }
79 
80  assert(k <= 8);
81 
82  v = get_sr_golomb(gb, k, 12, bits);
83  av_dlog(NULL, "v:%d bias:%d error:%d drift:%d count:%d k:%d",
84  v, state->bias, state->error_sum, state->drift, state->count, k);
85 
86 #if 0 // JPEG LS
87  if (k == 0 && 2 * state->drift <= -state->count)
88  v ^= (-1);
89 #else
90  v ^= ((2 * state->drift + state->count) >> 31);
91 #endif
92 
93  ret = fold(v + state->bias, bits);
94 
95  update_vlc_state(state, v);
96 
97  return ret;
98 }
99 
101  int16_t *sample[2],
102  int plane_index, int bits)
103 {
104  PlaneContext *const p = &s->plane[plane_index];
105  RangeCoder *const c = &s->c;
106  int x;
107  int run_count = 0;
108  int run_mode = 0;
109  int run_index = s->run_index;
110 
111  for (x = 0; x < w; x++) {
112  int diff, context, sign;
113 
114  context = get_context(p, sample[1] + x, sample[0] + x, sample[1] + x);
115  if (context < 0) {
116  context = -context;
117  sign = 1;
118  } else
119  sign = 0;
120 
121  av_assert2(context < p->context_count);
122 
123  if (s->ac) {
124  diff = get_symbol_inline(c, p->state[context], 1);
125  } else {
126  if (context == 0 && run_mode == 0)
127  run_mode = 1;
128 
129  if (run_mode) {
130  if (run_count == 0 && run_mode == 1) {
131  if (get_bits1(&s->gb)) {
132  run_count = 1 << ff_log2_run[run_index];
133  if (x + run_count <= w)
134  run_index++;
135  } else {
136  if (ff_log2_run[run_index])
137  run_count = get_bits(&s->gb, ff_log2_run[run_index]);
138  else
139  run_count = 0;
140  if (run_index)
141  run_index--;
142  run_mode = 2;
143  }
144  }
145  run_count--;
146  if (run_count < 0) {
147  run_mode = 0;
148  run_count = 0;
149  diff = get_vlc_symbol(&s->gb, &p->vlc_state[context],
150  bits);
151  if (diff >= 0)
152  diff++;
153  } else
154  diff = 0;
155  } else
156  diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
157 
158  av_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
159  run_count, run_index, run_mode, x, get_bits_count(&s->gb));
160  }
161 
162  if (sign)
163  diff = -diff;
164 
165  sample[1][x] = (predict(sample[1] + x, sample[0] + x) + diff) &
166  ((1 << bits) - 1);
167  }
168  s->run_index = run_index;
169 }
170 
171 static void decode_plane(FFV1Context *s, uint8_t *src,
172  int w, int h, int stride, int plane_index)
173 {
174  int x, y;
175  int16_t *sample[2];
176  sample[0] = s->sample_buffer + 3;
177  sample[1] = s->sample_buffer + w + 6 + 3;
178 
179  s->run_index = 0;
180 
181  memset(s->sample_buffer, 0, 2 * (w + 6) * sizeof(*s->sample_buffer));
182 
183  for (y = 0; y < h; y++) {
184  int16_t *temp = sample[0]; // FIXME: try a normal buffer
185 
186  sample[0] = sample[1];
187  sample[1] = temp;
188 
189  sample[1][-1] = sample[0][0];
190  sample[0][w] = sample[0][w - 1];
191 
192 // { START_TIMER
193  if (s->avctx->bits_per_raw_sample <= 8) {
194  decode_line(s, w, sample, plane_index, 8);
195  for (x = 0; x < w; x++)
196  src[x + stride * y] = sample[1][x];
197  } else {
198  decode_line(s, w, sample, plane_index,
200  if (s->packed_at_lsb) {
201  for (x = 0; x < w; x++)
202  ((uint16_t *)(src + stride * y))[x] = sample[1][x];
203  } else {
204  for (x = 0; x < w; x++)
205  ((uint16_t *)(src + stride * y))[x] = sample[1][x] << (16 - s->avctx->bits_per_raw_sample);
206  }
207  }
208 // STOP_TIMER("decode-line") }
209  }
210 }
211 
212 static void decode_rgb_frame(FFV1Context *s, uint8_t *src[3], int w, int h,
213  int stride[3])
214 {
215  int x, y, p;
216  int16_t *sample[4][2];
217  int lbd = s->avctx->bits_per_raw_sample <= 8;
218  int bits = s->avctx->bits_per_raw_sample > 0
220  : 8;
221  int offset = 1 << bits;
222 
223  for (x = 0; x < 4; x++) {
224  sample[x][0] = s->sample_buffer + x * 2 * (w + 6) + 3;
225  sample[x][1] = s->sample_buffer + (x * 2 + 1) * (w + 6) + 3;
226  }
227 
228  s->run_index = 0;
229 
230  memset(s->sample_buffer, 0, 8 * (w + 6) * sizeof(*s->sample_buffer));
231 
232  for (y = 0; y < h; y++) {
233  for (p = 0; p < 3 + s->transparency; p++) {
234  int16_t *temp = sample[p][0]; //FIXME try a normal buffer
235 
236  sample[p][0] = sample[p][1];
237  sample[p][1] = temp;
238 
239  sample[p][1][-1] = sample[p][0][0];
240  sample[p][0][w] = sample[p][0][w - 1];
241  if (lbd)
242  decode_line(s, w, sample[p], (p + 1) / 2, 9);
243  else
244  decode_line(s, w, sample[p], (p + 1) / 2, bits + 1);
245  }
246  for (x = 0; x < w; x++) {
247  int g = sample[0][1][x];
248  int b = sample[1][1][x];
249  int r = sample[2][1][x];
250  int a = sample[3][1][x];
251 
252  b -= offset;
253  r -= offset;
254  g -= (b + r) >> 2;
255  b += g;
256  r += g;
257 
258  if (lbd)
259  *((uint32_t *)(src[0] + x * 4 + stride[0] * y)) = b +
260  (g << 8) + (r << 16) + (a << 24);
261  else {
262  *((uint16_t *)(src[0] + x * 2 + stride[0] * y)) = b;
263  *((uint16_t *)(src[1] + x * 2 + stride[1] * y)) = g;
264  *((uint16_t *)(src[2] + x * 2 + stride[2] * y)) = r;
265  }
266  }
267  }
268 }
269 
271 {
272  RangeCoder *c = &fs->c;
274  unsigned ps, i, context_count;
275  memset(state, 128, sizeof(state));
276 
277  if (fs->ac > 1) {
278  for (i = 1; i < 256; i++) {
279  fs->c.one_state[i] = f->state_transition[i];
280  fs->c.zero_state[256 - i] = 256 - fs->c.one_state[i];
281  }
282  }
283 
284  fs->slice_x = get_symbol(c, state, 0) * f->width;
285  fs->slice_y = get_symbol(c, state, 0) * f->height;
286  fs->slice_width = (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
287  fs->slice_height = (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
288 
289  fs->slice_x /= f->num_h_slices;
290  fs->slice_y /= f->num_v_slices;
291  fs->slice_width = fs->slice_width / f->num_h_slices - fs->slice_x;
292  fs->slice_height = fs->slice_height / f->num_v_slices - fs->slice_y;
293  if ((unsigned)fs->slice_width > f->width ||
294  (unsigned)fs->slice_height > f->height)
295  return AVERROR_INVALIDDATA;
296  if ((unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width ||
297  (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
298  return AVERROR_INVALIDDATA;
299 
300  for (i = 0; i < f->plane_count; i++) {
301  PlaneContext *const p = &fs->plane[i];
302  int idx = get_symbol(c, state, 0);
303  if (idx > (unsigned)f->quant_table_count) {
304  av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
305  return AVERROR_INVALIDDATA;
306  }
307  p->quant_table_index = idx;
308  memcpy(p->quant_table, f->quant_tables[idx], sizeof(p->quant_table));
309  context_count = f->context_count[idx];
310 
311  if (p->context_count < context_count) {
312  av_freep(&p->state);
313  av_freep(&p->vlc_state);
314  }
316  }
317 
318  ps = get_symbol(c, state, 0);
319  if (ps == 1) {
320  f->picture.interlaced_frame = 1;
321  f->picture.top_field_first = 1;
322  } else if (ps == 2) {
323  f->picture.interlaced_frame = 1;
324  f->picture.top_field_first = 0;
325  } else if (ps == 3) {
326  f->picture.interlaced_frame = 0;
327  }
328  f->picture.sample_aspect_ratio.num = get_symbol(c, state, 0);
329  f->picture.sample_aspect_ratio.den = get_symbol(c, state, 0);
330 
331  return 0;
332 }
333 
334 static int decode_slice(AVCodecContext *c, void *arg)
335 {
336  FFV1Context *fs = *(void **)arg;
337  FFV1Context *f = fs->avctx->priv_data;
338  int width, height, x, y, ret;
339  const int ps = (av_pix_fmt_desc_get(c->pix_fmt)->flags & PIX_FMT_PLANAR)
340  ? (c->bits_per_raw_sample > 8) + 1
341  : 4;
342  AVFrame *const p = &f->picture;
343 
344  if (f->version > 2) {
345  if (decode_slice_header(f, fs) < 0) {
346  fs->slice_damaged = 1;
347  return AVERROR_INVALIDDATA;
348  }
349  }
350  if ((ret = ffv1_init_slice_state(f, fs)) < 0)
351  return ret;
352  if (f->picture.key_frame)
353  ffv1_clear_slice_state(f, fs);
354  width = fs->slice_width;
355  height = fs->slice_height;
356  x = fs->slice_x;
357  y = fs->slice_y;
358 
359  if (!fs->ac) {
360  if (f->version == 3 && f->minor_version > 1 || f->version > 3)
361  get_rac(&fs->c, (uint8_t[]) { 129 });
362  fs->ac_byte_count = f->version > 2 || (!x && !y) ? fs->c.bytestream - fs->c.bytestream_start - 1 : 0;
364  (fs->c.bytestream_end - fs->c.bytestream_start -
365  fs->ac_byte_count) * 8);
366  }
367 
368  av_assert1(width && height);
369  if (f->colorspace == 0) {
370  const int chroma_width = -((-width) >> f->chroma_h_shift);
371  const int chroma_height = -((-height) >> f->chroma_v_shift);
372  const int cx = x >> f->chroma_h_shift;
373  const int cy = y >> f->chroma_v_shift;
374  decode_plane(fs, p->data[0] + ps * x + y * p->linesize[0], width,
375  height, p->linesize[0],
376  0);
377 
378  if (f->chroma_planes) {
379  decode_plane(fs, p->data[1] + ps * cx + cy * p->linesize[1],
380  chroma_width, chroma_height, p->linesize[1],
381  1);
382  decode_plane(fs, p->data[2] + ps * cx + cy * p->linesize[2],
383  chroma_width, chroma_height, p->linesize[2],
384  1);
385  }
386  if (fs->transparency)
387  decode_plane(fs, p->data[3] + ps * x + y * p->linesize[3], width,
388  height, p->linesize[3],
389  2);
390  } else {
391  uint8_t *planes[3] = { p->data[0] + ps * x + y * p->linesize[0],
392  p->data[1] + ps * x + y * p->linesize[1],
393  p->data[2] + ps * x + y * p->linesize[2] };
394  decode_rgb_frame(fs, planes, width, height, p->linesize);
395  }
396  if (fs->ac && f->version > 2) {
397  int v;
398  get_rac(&fs->c, (uint8_t[]) { 129 });
399  v = fs->c.bytestream_end - fs->c.bytestream - 2 - 5 * f->ec;
400  if (v) {
401  av_log(f->avctx, AV_LOG_ERROR, "bytestream end mismatching by %d\n",
402  v);
403  fs->slice_damaged = 1;
404  }
405  }
406 
407  emms_c();
408 
409  return 0;
410 }
411 
412 static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale)
413 {
414  int v;
415  int i = 0;
417 
418  memset(state, 128, sizeof(state));
419 
420  for (v = 0; i < 128; v++) {
421  unsigned len = get_symbol(c, state, 0) + 1;
422 
423  if (len > 128 - i)
424  return -1;
425 
426  while (len--) {
427  quant_table[i] = scale * v;
428  i++;
429  }
430  }
431 
432  for (i = 1; i < 128; i++)
433  quant_table[256 - i] = -quant_table[i];
434  quant_table[128] = -quant_table[127];
435 
436  return 2 * v - 1;
437 }
438 
440  int16_t quant_table[MAX_CONTEXT_INPUTS][256])
441 {
442  int i;
443  int context_count = 1;
444 
445  for (i = 0; i < 5; i++) {
446  context_count *= read_quant_table(c, quant_table[i], context_count);
447  if (context_count > 32768U) {
448  return -1;
449  }
450  }
451  return (context_count + 1) / 2;
452 }
453 
455 {
456  RangeCoder *const c = &f->c;
458  int i, j, k, ret;
459  uint8_t state2[32][CONTEXT_SIZE];
460 
461  memset(state2, 128, sizeof(state2));
462  memset(state, 128, sizeof(state));
463 
465  ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
466 
467  f->version = get_symbol(c, state, 0);
468  if (f->version > 2) {
469  c->bytestream_end -= 4;
470  f->minor_version = get_symbol(c, state, 0);
471  }
472  f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
473 
474  if (f->ac > 1) {
475  for (i = 1; i < 256; i++)
476  f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
477  }
478 
479  f->colorspace = get_symbol(c, state, 0); //YUV cs type
480  f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
481  f->chroma_planes = get_rac(c, state);
482  f->chroma_h_shift = get_symbol(c, state, 0);
483  f->chroma_v_shift = get_symbol(c, state, 0);
484  f->transparency = get_rac(c, state);
485  f->plane_count = 2 + f->transparency;
486  f->num_h_slices = 1 + get_symbol(c, state, 0);
487  f->num_v_slices = 1 + get_symbol(c, state, 0);
488 
489  if (f->num_h_slices > (unsigned)f->width ||
490  f->num_v_slices > (unsigned)f->height) {
491  av_log(f->avctx, AV_LOG_ERROR, "too many slices\n");
492  return AVERROR_INVALIDDATA;
493  }
494 
495  f->quant_table_count = get_symbol(c, state, 0);
496  if (f->quant_table_count > (unsigned)MAX_QUANT_TABLES)
497  return AVERROR_INVALIDDATA;
498  for (i = 0; i < f->quant_table_count; i++) {
499  f->context_count[i] = read_quant_tables(c, f->quant_tables[i]);
500  if (f->context_count[i] < 0) {
501  av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
502  return AVERROR_INVALIDDATA;
503  }
504  }
505  if ((ret = ffv1_allocate_initial_states(f)) < 0)
506  return ret;
507 
508  for (i = 0; i < f->quant_table_count; i++)
509  if (get_rac(c, state)) {
510  for (j = 0; j < f->context_count[i]; j++)
511  for (k = 0; k < CONTEXT_SIZE; k++) {
512  int pred = j ? f->initial_states[i][j - 1][k] : 128;
513  f->initial_states[i][j][k] =
514  (pred + get_symbol(c, state2[k], 1)) & 0xFF;
515  }
516  }
517 
518  if (f->version > 2) {
519  f->ec = get_symbol(c, state, 0);
520  }
521 
522  if (f->version > 2) {
523  unsigned v;
526  if (v) {
527  av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", v);
528  return AVERROR_INVALIDDATA;
529  }
530  }
531 
532  return 0;
533 }
534 
535 
536 static int read_header(FFV1Context *f)
537 {
539  int i, j, context_count = -1;
540  RangeCoder *const c = &f->slice_context[0]->c;
541 
542  memset(state, 128, sizeof(state));
543 
544  if (f->version < 2) {
546  unsigned v = get_symbol(c, state, 0);
547  if (v > 1) {
549  "invalid version %d in version 1 header\n", v);
550  return AVERROR_INVALIDDATA;
551  }
552  f->version = v;
553 
554  f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
555 
556  if (f->ac > 1) {
557  for (i = 1; i < 256; i++)
558  f->state_transition[i] =
559  get_symbol(c, state, 1) + c->one_state[i];
560  }
561 
562  colorspace = get_symbol(c, state, 0); //YUV cs type
563  bits_per_raw_sample = f->version > 0 ? get_symbol(c, state, 0) : f->avctx->bits_per_raw_sample;
564  chroma_planes = get_rac(c, state);
565  chroma_h_shift = get_symbol(c, state, 0);
566  chroma_v_shift = get_symbol(c, state, 0);
567  transparency = get_rac(c, state);
568 
569  if (f->plane_count) {
570  if (colorspace != f->colorspace ||
571  bits_per_raw_sample != f->avctx->bits_per_raw_sample ||
572  chroma_planes != f->chroma_planes ||
573  chroma_h_shift != f->chroma_h_shift ||
574  chroma_v_shift != f->chroma_v_shift ||
575  transparency != f->transparency) {
576  av_log(f->avctx, AV_LOG_ERROR, "Invalid change of global parameters\n");
577  return AVERROR_INVALIDDATA;
578  }
579  }
580 
581  f->colorspace = colorspace;
587 
588  f->plane_count = 2 + f->transparency;
589  }
590 
591  if (f->colorspace == 0) {
592  if (!f->transparency && !f->chroma_planes) {
593  if (f->avctx->bits_per_raw_sample <= 8)
595  else
597  } else if (f->avctx->bits_per_raw_sample <= 8 && !f->transparency) {
598  switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
599  case 0x00:
601  break;
602  case 0x01:
604  break;
605  case 0x10:
607  break;
608  case 0x11:
610  break;
611  case 0x20:
613  break;
614  case 0x22:
616  break;
617  default:
618  av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
619  return AVERROR(ENOSYS);
620  }
621  } else if (f->avctx->bits_per_raw_sample <= 8 && f->transparency) {
622  switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
623  case 0x00:
625  break;
626  case 0x10:
628  break;
629  case 0x11:
631  break;
632  default:
633  av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
634  return AVERROR(ENOSYS);
635  }
636  } else if (f->avctx->bits_per_raw_sample == 9) {
637  f->packed_at_lsb = 1;
638  switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
639  case 0x00:
641  break;
642  case 0x10:
644  break;
645  case 0x11:
647  break;
648  default:
649  av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
650  return AVERROR(ENOSYS);
651  }
652  } else if (f->avctx->bits_per_raw_sample == 10) {
653  f->packed_at_lsb = 1;
654  switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
655  case 0x00:
657  break;
658  case 0x10:
660  break;
661  case 0x11:
663  break;
664  default:
665  av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
666  return AVERROR(ENOSYS);
667  }
668  } else {
669  switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
670  case 0x00:
672  break;
673  case 0x10:
675  break;
676  case 0x11:
678  break;
679  default:
680  av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
681  return AVERROR(ENOSYS);
682  }
683  }
684  } else if (f->colorspace == 1) {
685  if (f->chroma_h_shift || f->chroma_v_shift) {
687  "chroma subsampling not supported in this colorspace\n");
688  return AVERROR(ENOSYS);
689  }
690  switch (f->avctx->bits_per_raw_sample) {
691  case 0:
692  case 8:
694  break;
695  case 9:
697  break;
698  case 10:
700  break;
701  default:
703  "bit depth %d not supported\n",
705  return AVERROR(ENOSYS);
706  }
707  } else {
708  av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
709  return AVERROR(ENOSYS);
710  }
711 
712  av_dlog(f->avctx, "%d %d %d\n",
714  if (f->version < 2) {
715  context_count = read_quant_tables(c, f->quant_table);
716  if (context_count < 0) {
717  av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
718  return AVERROR_INVALIDDATA;
719  }
720  } else if (f->version < 3) {
721  f->slice_count = get_symbol(c, state, 0);
722  } else {
723  const uint8_t *p = c->bytestream_end;
724  for (f->slice_count = 0;
725  f->slice_count < MAX_SLICES && 3 < p - c->bytestream_start;
726  f->slice_count++) {
727  int trailer = 3 + 5 * !!f->ec;
728  int size = AV_RB24(p - trailer);
729  if (size + trailer > p - c->bytestream_start)
730  break;
731  p -= size + trailer;
732  }
733  }
734  if (f->slice_count > (unsigned)MAX_SLICES || f->slice_count <= 0) {
735  av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid\n",
736  f->slice_count);
737  return AVERROR_INVALIDDATA;
738  }
739 
740  for (j = 0; j < f->slice_count; j++) {
741  FFV1Context *fs = f->slice_context[j];
742  fs->ac = f->ac;
743  fs->packed_at_lsb = f->packed_at_lsb;
744 
745  fs->slice_damaged = 0;
746 
747  if (f->version == 2) {
748  fs->slice_x = get_symbol(c, state, 0) * f->width;
749  fs->slice_y = get_symbol(c, state, 0) * f->height;
750  fs->slice_width =
751  (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
752  fs->slice_height =
753  (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
754 
755  fs->slice_x /= f->num_h_slices;
756  fs->slice_y /= f->num_v_slices;
757  fs->slice_width = fs->slice_width / f->num_h_slices - fs->slice_x;
758  fs->slice_height = fs->slice_height / f->num_v_slices - fs->slice_y;
759  if ((unsigned)fs->slice_width > f->width ||
760  (unsigned)fs->slice_height > f->height)
761  return AVERROR_INVALIDDATA;
762  if ((unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
763  || (unsigned)fs->slice_y + (uint64_t)fs->slice_height >
764  f->height)
765  return AVERROR_INVALIDDATA;
766  }
767 
768  for (i = 0; i < f->plane_count; i++) {
769  PlaneContext *const p = &fs->plane[i];
770 
771  if (f->version == 2) {
772  int idx = get_symbol(c, state, 0);
773  if (idx > (unsigned)f->quant_table_count) {
775  "quant_table_index out of range\n");
776  return AVERROR_INVALIDDATA;
777  }
778  p->quant_table_index = idx;
779  memcpy(p->quant_table, f->quant_tables[idx],
780  sizeof(p->quant_table));
781  context_count = f->context_count[idx];
782  } else {
783  memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
784  }
785 
786  if (f->version <= 2) {
787  av_assert0(context_count >= 0);
788  if (p->context_count < context_count) {
789  av_freep(&p->state);
790  av_freep(&p->vlc_state);
791  }
793  }
794  }
795  }
796  return 0;
797 }
798 
800 {
801  FFV1Context *f = avctx->priv_data;
802  int ret;
803 
804  ffv1_common_init(avctx);
805 
806  if (avctx->extradata && (ret = read_extra_header(f)) < 0)
807  return ret;
808 
809  if ((ret = ffv1_init_slice_contexts(f)) < 0)
810  return ret;
811 
812  return 0;
813 }
814 
816  int *got_frame, AVPacket *avpkt)
817 {
818  const uint8_t *buf = avpkt->data;
819  int buf_size = avpkt->size;
820  FFV1Context *f = avctx->priv_data;
821  RangeCoder *const c = &f->slice_context[0]->c;
822  AVFrame *const p = &f->picture;
823  int i, ret;
824  uint8_t keystate = 128;
825  const uint8_t *buf_p;
826 
827  AVFrame *picture = data;
828 
829  /* release previously stored data */
830  if (p->data[0])
831  avctx->release_buffer(avctx, p);
832 
833  ff_init_range_decoder(c, buf, buf_size);
834  ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
835 
836  p->pict_type = AV_PICTURE_TYPE_I; //FIXME I vs. P
837  if (get_rac(c, &keystate)) {
838  p->key_frame = 1;
839  f->key_frame_ok = 0;
840  if ((ret = read_header(f)) < 0)
841  return ret;
842  f->key_frame_ok = 1;
843  } else {
844  if (!f->key_frame_ok) {
845  av_log(avctx, AV_LOG_ERROR,
846  "Cannot decode non-keyframe without valid keyframe\n");
847  return AVERROR_INVALIDDATA;
848  }
849  p->key_frame = 0;
850  }
851 
852  p->reference = 3; //for error concealment
853  if ((ret = ff_get_buffer(avctx, p)) < 0) {
854  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
855  return ret;
856  }
857 
858  if (avctx->debug & FF_DEBUG_PICT_INFO)
859  av_log(avctx, AV_LOG_DEBUG,
860  "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
861  f->version, p->key_frame, f->ac, f->ec, f->slice_count,
863 
864  buf_p = buf + buf_size;
865  for (i = f->slice_count - 1; i >= 0; i--) {
866  FFV1Context *fs = f->slice_context[i];
867  int trailer = 3 + 5 * !!f->ec;
868  int v;
869 
870  if (i || f->version > 2)
871  v = AV_RB24(buf_p - trailer) + trailer;
872  else
873  v = buf_p - c->bytestream_start;
874  if (buf_p - c->bytestream_start < v) {
875  av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
876  return AVERROR_INVALIDDATA;
877  }
878  buf_p -= v;
879 
880  if (f->ec) {
881  unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, v);
882  if (crc) {
883  av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", crc);
884  fs->slice_damaged = 1;
885  }
886  }
887 
888  if (i) {
889  ff_init_range_decoder(&fs->c, buf_p, v);
890  } else
891  fs->c.bytestream_end = (uint8_t *)(buf_p + v);
892  }
893 
894  avctx->execute(avctx, decode_slice, &f->slice_context[0], NULL,
895  f->slice_count,
896  sizeof(void *));
897 
898  for (i = f->slice_count - 1; i >= 0; i--) {
899  FFV1Context *fs = f->slice_context[i];
900  int j;
901  if (fs->slice_damaged && f->last_picture.data[0]) {
902  const uint8_t *src[4];
903  uint8_t *dst[4];
904  for (j = 0; j < 4; j++) {
905  int sh = (j == 1 || j == 2) ? f->chroma_h_shift : 0;
906  int sv = (j == 1 || j == 2) ? f->chroma_v_shift : 0;
907  dst[j] = f->picture.data[j] + f->picture.linesize[j] *
908  (fs->slice_y >> sv) + (fs->slice_x >> sh);
909  src[j] = f->last_picture.data[j] +
910  f->last_picture.linesize[j] *
911  (fs->slice_y >> sv) + (fs->slice_x >> sh);
912  }
913  av_image_copy(dst, f->picture.linesize, (const uint8_t **)src,
915  avctx->pix_fmt, fs->slice_width,
916  fs->slice_height);
917  }
918  }
919 
920  f->picture_number++;
921 
922  *picture = *p;
923  *got_frame = 1;
924 
926 
927  return buf_size;
928 }
929 
931  .name = "ffv1",
932  .type = AVMEDIA_TYPE_VIDEO,
933  .id = AV_CODEC_ID_FFV1,
934  .priv_data_size = sizeof(FFV1Context),
936  .close = ffv1_close,
938  .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/ |
940  .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
941 };
static av_always_inline int fold(int diff, int bits)
Definition: ffv1.h:118
int ffv1_allocate_initial_states(FFV1Context *f)
Definition: ffv1.c:227
const uint8_t ff_log2_run[41]
Definition: bitstream.c:36
int size
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1435
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
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:90
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:2259
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:70
misc image utilities
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:237
int16_t quant_table[MAX_CONTEXT_INPUTS][256]
Definition: ffv1.h:56
int quant_table_count
Definition: ffv1.h:104
int ffv1_init_slice_state(FFV1Context *f, FFV1Context *fs)
Definition: ffv1.c:158
static int decode_slice(AVCodecContext *c, void *arg)
Definition: ffv1dec.c:334
int slice_height
Definition: ffv1.h:113
#define MAX_CONTEXT_INPUTS
Definition: ffv1.h:38
int16_t * sample_buffer
Definition: ffv1.h:94
int version
Definition: ffv1.h:74
uint8_t zero_state[256]
Definition: rangecoder.h:40
Range coder.
uint8_t * bytestream_end
Definition: rangecoder.h:44
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:916
static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale)
Definition: ffv1dec.c:412
#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:43
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2711
static av_always_inline void predict(PredictorState *ps, float *coef, int output_enable)
Definition: aacdec.c:1631
#define sample
int height
Definition: ffv1.h:76
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:2960
uint8_t one_state[256]
Definition: rangecoder.h:41
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:151
int plane_count
Definition: ffv1.h:83
int slice_damaged
Definition: ffv1.h:97
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:102
static int read_quant_tables(RangeCoder *c, int16_t quant_table[MAX_CONTEXT_INPUTS][256])
Definition: ffv1dec.c:439
uint8_t bits
Definition: crc.c:31
uint8_t
static int get_rac(RangeCoder *c, uint8_t *const state)
Definition: rangecoder.h:115
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:63
AVOptions.
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:76
int8_t bias
Definition: ffv1.h:51
int ffv1_common_init(AVCodecContext *avctx)
Definition: ffv1.c:134
#define b
Definition: input.c:52
RangeCoder c
Definition: ffv1.h:69
#define emms_c()
Definition: internal.h:145
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1454
int slice_y
Definition: ffv1.h:115
uint8_t(*[MAX_QUANT_TABLES] initial_states)[32]
Definition: ffv1.h:91
const char data[16]
Definition: mxf.c:66
int coder_type
coder type
Definition: avcodec.h:2388
uint8_t * data
Definition: avcodec.h:915
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:192
uint8_t count
Definition: ffv1.h:52
bitstream reader API header.
static void decode_rgb_frame(FFV1Context *s, uint8_t *src[3], int w, int h, int stride[3])
Definition: ffv1dec.c:212
int interlaced_frame
The content of the picture is interlaced.
Definition: avcodec.h:1232
VlcState * vlc_state
Definition: ffv1.h:60
int minor_version
Definition: ffv1.h:75
struct FFV1Context FFV1Context
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
int bits_per_raw_sample
Definition: ffv1.h:100
int slice_width
Definition: ffv1.h:112
#define r
Definition: input.c:51
GetBitContext gb
Definition: ffv1.h:70
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:161
static int decode_slice_header(FFV1Context *f, FFV1Context *fs)
Definition: ffv1dec.c:270
int16_t quant_tables[MAX_QUANT_TABLES][MAX_CONTEXT_INPUTS][256]
Definition: ffv1.h:88
int reference
is this picture used as reference The values for this are the same as the MpegEncContext.picture_structure variable, that is 1->top field, 2->bottom field, 3->frame/both fields.
Definition: avcodec.h:1132
static int ffv1_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: ffv1dec.c:815
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
static av_cold int ffv1_decode_init(AVCodecContext *avctx)
Definition: ffv1dec.c:799
g
Definition: yuv2rgb.c:540
int context_count
Definition: ffv1.h:58
simple assert() macros that are a bit more flexible than ISO C assert().
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
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:244
static int get_vlc_symbol(GetBitContext *gb, VlcState *const state, int bits)
Definition: ffv1dec.c:68
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:171
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:69
int ac
Definition: ffv1.h:84
int16_t quant_table[MAX_CONTEXT_INPUTS][256]
Definition: ffv1.h:87
int run_index
Definition: ffv1.h:92
Definition: ffv1.h:48
static av_noinline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed)
Definition: ffv1dec.c:63
uint8_t state_transition[256]
Definition: ffv1.h:90
AVFrame last_picture
Definition: ffv1.h:82
static AVFrame * picture
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
Definition: avcodec.h:1065
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
int num_h_slices
Definition: ffv1.h:111
#define MAX_QUANT_TABLES
Definition: ffv1.h:37
int colorspace
Definition: ffv1.h:93
static float quant_table[96]
Definition: binkaudio.c:44
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Get a buffer for a frame.
Definition: utils.c:464
static int get_context(PlaneContext *p, int16_t *src, int16_t *last, int16_t *last2)
Definition: ffv1.h:140
static void update_vlc_state(VlcState *const state, const int v)
Definition: ffv1.h:162
int slice_count
Definition: ffv1.h:109
void ff_build_rac_states(RangeCoder *c, int factor, int max_p)
Definition: rangecoder.c:59
int ac_byte_count
Definition: ffv1.h:85
static av_always_inline void decode_line(FFV1Context *s, int w, int16_t *sample[2], int plane_index, int bits)
Definition: ffv1dec.c:100
int16_t drift
Definition: ffv1.h:49
int packed_at_lsb
Definition: ffv1.h:101
av_cold int ffv1_init_slice_contexts(FFV1Context *f)
Definition: ffv1.c:192
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:536
static const float pred[4]
Definition: siprdata.h:259
NULL
Definition: eval.c:52
static int width
Definition: utils.c:156
int context_count[MAX_QUANT_TABLES]
Definition: ffv1.h:89
external API header
#define PIX_FMT_PLANAR
At least one pixel component is not in the first data plane.
Definition: pixdesc.h:90
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:162
uint8_t flags
Definition: pixdesc.h:76
int debug
debug
Definition: avcodec.h:2568
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
AVRational sample_aspect_ratio
sample aspect ratio for the video frame, 0/1 if unknown/unspecified
Definition: avcodec.h:1080
int extradata_size
Definition: avcodec.h:1455
void ffv1_clear_slice_state(FFV1Context *f, FFV1Context *fs)
Definition: ffv1.c:242
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:268
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:71
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:372
AVFrame picture
Definition: ffv1.h:82
int picture_number
Definition: ffv1.h:81
uint16_t error_sum
Definition: ffv1.h:50
void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size)
Definition: rangecoder.c:51
int key_frame_ok
Definition: ffv1.h:98
static uint32_t state
Definition: trasher.c:27
#define CONTEXT_SIZE
Definition: ffv1.h:35
static const uint16_t scale[4]
int quant_table_index
Definition: ffv1.h:57
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
Definition: vf_drawbox.c:36
int height
Definition: gxfenc.c:72
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
Y , 8bpp.
Definition: pixfmt.h:73
common internal api header.
#define MAX_SLICES
Definition: dxva2_mpeg2.c:25
uint8_t(* state)[CONTEXT_SIZE]
Definition: ffv1.h:59
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:72
int den
denominator
Definition: rational.h:45
DSP utils.
uint8_t * bytestream_start
Definition: rangecoder.h:42
void * priv_data
Definition: avcodec.h:1382
int chroma_h_shift
Definition: ffv1.h:78
PlaneContext plane[MAX_PLANES]
Definition: ffv1.h:86
int transparency
Definition: ffv1.h:79
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:2773
int chroma_v_shift
Definition: ffv1.h:78
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: avcodec.h:1239
int len
int chroma_planes
Definition: ffv1.h:77
av_cold int ffv1_close(AVCodecContext *avctx)
Definition: ffv1.c:269
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1058
struct FFV1Context * slice_context[MAX_SLICES]
Definition: ffv1.h:108
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:100
int ec
Definition: ffv1.h:96
enum AVColorSpace colorspace
Definition: dirac.c:99
static int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len)
read signed golomb rice code (ffv1).
Definition: golomb.h:335
int num_v_slices
Definition: ffv1.h:110
exp golomb vlc stuff
This structure stores compressed data.
Definition: avcodec.h:898
static int read_extra_header(FFV1Context *f)
Definition: ffv1dec.c:454
AVCodecContext * avctx
Definition: ffv1.h:68
int slice_x
Definition: ffv1.h:114
AVCodec ff_ffv1_decoder
Definition: ffv1dec.c:930
int width
Definition: ffv1.h:76
if(!(ptr_align%ac->ptr_align)&&samples_align >=aligned_len)
bitstream writer API