FFmpeg  1.2.12
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 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 
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 "put_bits.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  v = get_sr_golomb(gb, k, 12, bits);
81  av_dlog(NULL, "v:%d bias:%d error:%d drift:%d count:%d k:%d",
82  v, state->bias, state->error_sum, state->drift, state->count, k);
83 
84 #if 0 // JPEG LS
85  if (k == 0 && 2 * state->drift <= -state->count)
86  v ^= (-1);
87 #else
88  v ^= ((2 * state->drift + state->count) >> 31);
89 #endif
90 
91  ret = fold(v + state->bias, bits);
92 
93  update_vlc_state(state, v);
94 
95  return ret;
96 }
97 
99  int16_t *sample[2],
100  int plane_index, int bits)
101 {
102  PlaneContext *const p = &s->plane[plane_index];
103  RangeCoder *const c = &s->c;
104  int x;
105  int run_count = 0;
106  int run_mode = 0;
107  int run_index = s->run_index;
108 
109  for (x = 0; x < w; x++) {
110  int diff, context, sign;
111 
112  context = get_context(p, sample[1] + x, sample[0] + x, sample[1] + x);
113  if (context < 0) {
114  context = -context;
115  sign = 1;
116  } else
117  sign = 0;
118 
119  av_assert2(context < p->context_count);
120 
121  if (s->ac) {
122  diff = get_symbol_inline(c, p->state[context], 1);
123  } else {
124  if (context == 0 && run_mode == 0)
125  run_mode = 1;
126 
127  if (run_mode) {
128  if (run_count == 0 && run_mode == 1) {
129  if (get_bits1(&s->gb)) {
130  run_count = 1 << ff_log2_run[run_index];
131  if (x + run_count <= w)
132  run_index++;
133  } else {
134  if (ff_log2_run[run_index])
135  run_count = get_bits(&s->gb, ff_log2_run[run_index]);
136  else
137  run_count = 0;
138  if (run_index)
139  run_index--;
140  run_mode = 2;
141  }
142  }
143  run_count--;
144  if (run_count < 0) {
145  run_mode = 0;
146  run_count = 0;
147  diff = get_vlc_symbol(&s->gb, &p->vlc_state[context],
148  bits);
149  if (diff >= 0)
150  diff++;
151  } else
152  diff = 0;
153  } else
154  diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
155 
156  av_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
157  run_count, run_index, run_mode, x, get_bits_count(&s->gb));
158  }
159 
160  if (sign)
161  diff = -diff;
162 
163  sample[1][x] = (predict(sample[1] + x, sample[0] + x) + diff) &
164  ((1 << bits) - 1);
165  }
166  s->run_index = run_index;
167 }
168 
169 static void decode_plane(FFV1Context *s, uint8_t *src,
170  int w, int h, int stride, int plane_index)
171 {
172  int x, y;
173  int16_t *sample[2];
174  sample[0] = s->sample_buffer + 3;
175  sample[1] = s->sample_buffer + w + 6 + 3;
176 
177  s->run_index = 0;
178 
179  memset(s->sample_buffer, 0, 2 * (w + 6) * sizeof(*s->sample_buffer));
180 
181  for (y = 0; y < h; y++) {
182  int16_t *temp = sample[0]; // FIXME: try a normal buffer
183 
184  sample[0] = sample[1];
185  sample[1] = temp;
186 
187  sample[1][-1] = sample[0][0];
188  sample[0][w] = sample[0][w - 1];
189 
190 // { START_TIMER
191  if (s->avctx->bits_per_raw_sample <= 8) {
192  decode_line(s, w, sample, plane_index, 8);
193  for (x = 0; x < w; x++)
194  src[x + stride * y] = sample[1][x];
195  } else {
196  decode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
197  if (s->packed_at_lsb) {
198  for (x = 0; x < w; x++) {
199  ((uint16_t*)(src + stride*y))[x] = sample[1][x];
200  }
201  } else {
202  for (x = 0; x < w; x++) {
203  ((uint16_t*)(src + stride*y))[x] = sample[1][x] << (16 - s->avctx->bits_per_raw_sample);
204  }
205  }
206  }
207 // STOP_TIMER("decode-line") }
208  }
209 }
210 
211 static void decode_rgb_frame(FFV1Context *s, uint8_t *src[3], int w, int h, int stride[3])
212 {
213  int x, y, p;
214  int16_t *sample[4][2];
215  int lbd = s->avctx->bits_per_raw_sample <= 8;
216  int bits = s->avctx->bits_per_raw_sample > 0 ? s->avctx->bits_per_raw_sample : 8;
217  int offset = 1 << bits;
218 
219  for (x = 0; x < 4; x++) {
220  sample[x][0] = s->sample_buffer + x * 2 * (w + 6) + 3;
221  sample[x][1] = s->sample_buffer + (x * 2 + 1) * (w + 6) + 3;
222  }
223 
224  s->run_index = 0;
225 
226  memset(s->sample_buffer, 0, 8 * (w + 6) * sizeof(*s->sample_buffer));
227 
228  for (y = 0; y < h; y++) {
229  for (p = 0; p < 3 + s->transparency; p++) {
230  int16_t *temp = sample[p][0]; // FIXME: try a normal buffer
231 
232  sample[p][0] = sample[p][1];
233  sample[p][1] = temp;
234 
235  sample[p][1][-1]= sample[p][0][0 ];
236  sample[p][0][ w]= sample[p][0][w-1];
237  if (lbd)
238  decode_line(s, w, sample[p], (p + 1)/2, 9);
239  else
240  decode_line(s, w, sample[p], (p + 1)/2, bits + 1);
241  }
242  for (x = 0; x < w; x++) {
243  int g = sample[0][1][x];
244  int b = sample[1][1][x];
245  int r = sample[2][1][x];
246  int a = sample[3][1][x];
247 
248  b -= offset;
249  r -= offset;
250  g -= (b + r) >> 2;
251  b += g;
252  r += g;
253 
254  if (lbd)
255  *((uint32_t*)(src[0] + x*4 + stride[0]*y)) = b + (g<<8) + (r<<16) + (a<<24);
256  else {
257  *((uint16_t*)(src[0] + x*2 + stride[0]*y)) = b;
258  *((uint16_t*)(src[1] + x*2 + stride[1]*y)) = g;
259  *((uint16_t*)(src[2] + x*2 + stride[2]*y)) = r;
260  }
261  }
262  }
263 }
264 
266 {
267  RangeCoder *c = &fs->c;
269  unsigned ps, i, context_count;
270  memset(state, 128, sizeof(state));
271 
272  av_assert0(f->version > 2);
273 
274  fs->slice_x = get_symbol(c, state, 0) * f->width ;
275  fs->slice_y = get_symbol(c, state, 0) * f->height;
276  fs->slice_width = (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
277  fs->slice_height = (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
278 
279  fs->slice_x /= f->num_h_slices;
280  fs->slice_y /= f->num_v_slices;
281  fs->slice_width = fs->slice_width /f->num_h_slices - fs->slice_x;
282  fs->slice_height = fs->slice_height/f->num_v_slices - fs->slice_y;
283  if ((unsigned)fs->slice_width > f->width || (unsigned)fs->slice_height > f->height)
284  return -1;
285  if ( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
286  || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
287  return -1;
288 
289  for (i = 0; i < f->plane_count; i++) {
290  PlaneContext * const p = &fs->plane[i];
291  int idx = get_symbol(c, state, 0);
292  if (idx > (unsigned)f->quant_table_count) {
293  av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
294  return -1;
295  }
296  p->quant_table_index = idx;
297  memcpy(p->quant_table, f->quant_tables[idx], sizeof(p->quant_table));
298  context_count = f->context_count[idx];
299 
300  if (p->context_count < context_count) {
301  av_freep(&p->state);
302  av_freep(&p->vlc_state);
303  }
305  }
306 
307  ps = get_symbol(c, state, 0);
308  if (ps == 1) {
309  f->picture.interlaced_frame = 1;
310  f->picture.top_field_first = 1;
311  } else if (ps == 2) {
312  f->picture.interlaced_frame = 1;
313  f->picture.top_field_first = 0;
314  } else if (ps == 3) {
315  f->picture.interlaced_frame = 0;
316  }
317  f->picture.sample_aspect_ratio.num = get_symbol(c, state, 0);
318  f->picture.sample_aspect_ratio.den = get_symbol(c, state, 0);
319 
320  return 0;
321 }
322 
323 static int decode_slice(AVCodecContext *c, void *arg)
324 {
325  FFV1Context *fs = *(void **)arg;
326  FFV1Context *f = fs->avctx->priv_data;
327  int width, height, x, y, ret;
328  const int ps = av_pix_fmt_desc_get(c->pix_fmt)->comp[0].step_minus1 + 1;
329  AVFrame * const p = &f->picture;
330 
331  if (f->version > 2) {
332  if (ffv1_init_slice_state(f, fs) < 0)
333  return AVERROR(ENOMEM);
334  if (decode_slice_header(f, fs) < 0) {
335  fs->slice_damaged = 1;
336  return AVERROR_INVALIDDATA;
337  }
338  }
339  if ((ret = ffv1_init_slice_state(f, fs)) < 0)
340  return ret;
341  if (f->picture.key_frame)
342  ffv1_clear_slice_state(f, fs);
343 
344  width = fs->slice_width;
345  height = fs->slice_height;
346  x = fs->slice_x;
347  y = fs->slice_y;
348 
349  if (!fs->ac) {
350  if (f->version == 3 && f->minor_version > 1 || f->version > 3)
351  get_rac(&fs->c, (uint8_t[]) { 129 });
352  fs->ac_byte_count = f->version > 2 || (!x && !y) ? fs->c.bytestream - fs->c.bytestream_start - 1 : 0;
353  init_get_bits(&fs->gb,
354  fs->c.bytestream_start + fs->ac_byte_count,
355  (fs->c.bytestream_end - fs->c.bytestream_start - fs->ac_byte_count) * 8);
356  }
357 
358  av_assert1(width && height);
359  if (f->colorspace == 0) {
360  const int chroma_width = -((-width) >> f->chroma_h_shift);
361  const int chroma_height = -((-height) >> f->chroma_v_shift);
362  const int cx = x >> f->chroma_h_shift;
363  const int cy = y >> f->chroma_v_shift;
364  decode_plane(fs, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0);
365 
366  if (f->chroma_planes) {
367  decode_plane(fs, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
368  decode_plane(fs, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1);
369  }
370  if (fs->transparency)
371  decode_plane(fs, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], 2);
372  } else {
373  uint8_t *planes[3] = { p->data[0] + ps * x + y * p->linesize[0],
374  p->data[1] + ps * x + y * p->linesize[1],
375  p->data[2] + ps * x + y * p->linesize[2] };
376  decode_rgb_frame(fs, planes, width, height, p->linesize);
377  }
378  if (fs->ac && f->version > 2) {
379  int v;
380  get_rac(&fs->c, (uint8_t[]) { 129 });
381  v = fs->c.bytestream_end - fs->c.bytestream - 2 - 5*f->ec;
382  if (v) {
383  av_log(f->avctx, AV_LOG_ERROR, "bytestream end mismatching by %d\n", v);
384  fs->slice_damaged = 1;
385  }
386  }
387 
388  emms_c();
389 
390  return 0;
391 }
392 
393 static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale)
394 {
395  int v;
396  int i = 0;
398 
399  memset(state, 128, sizeof(state));
400 
401  for (v = 0; i < 128; v++) {
402  unsigned len = get_symbol(c, state, 0) + 1;
403 
404  if (len > 128 - i)
405  return AVERROR_INVALIDDATA;
406 
407  while (len--) {
408  quant_table[i] = scale * v;
409  i++;
410  }
411  }
412 
413  for (i = 1; i < 128; i++)
414  quant_table[256 - i] = -quant_table[i];
415  quant_table[128] = -quant_table[127];
416 
417  return 2 * v - 1;
418 }
419 
421  int16_t quant_table[MAX_CONTEXT_INPUTS][256])
422 {
423  int i;
424  int context_count = 1;
425 
426  for (i = 0; i < 5; i++) {
427  context_count *= read_quant_table(c, quant_table[i], context_count);
428  if (context_count > 32768U) {
429  return AVERROR_INVALIDDATA;
430  }
431  }
432  return (context_count + 1) / 2;
433 }
434 
436 {
437  RangeCoder *const c = &f->c;
439  int i, j, k, ret;
440  uint8_t state2[32][CONTEXT_SIZE];
441 
442  memset(state2, 128, sizeof(state2));
443  memset(state, 128, sizeof(state));
444 
446  ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
447 
448  f->version = get_symbol(c, state, 0);
449  if (f->version < 2) {
450  av_log(f->avctx, AV_LOG_ERROR, "Invalid version in global header\n");
451  return AVERROR_INVALIDDATA;
452  }
453  if (f->version > 2) {
454  c->bytestream_end -= 4;
455  f->minor_version = get_symbol(c, state, 0);
456  }
457  f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
458  if (f->ac > 1) {
459  for (i = 1; i < 256; i++)
460  f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
461  }
462 
463  f->colorspace = get_symbol(c, state, 0); //YUV cs type
464  f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
465  f->chroma_planes = get_rac(c, state);
466  f->chroma_h_shift = get_symbol(c, state, 0);
467  f->chroma_v_shift = get_symbol(c, state, 0);
468  f->transparency = get_rac(c, state);
469  f->plane_count = 2 + f->transparency;
470  f->num_h_slices = 1 + get_symbol(c, state, 0);
471  f->num_v_slices = 1 + get_symbol(c, state, 0);
472 
473  if (f->num_h_slices > (unsigned)f->width || !f->num_h_slices ||
474  f->num_v_slices > (unsigned)f->height || !f->num_v_slices
475  ) {
476  av_log(f->avctx, AV_LOG_ERROR, "slice count invalid\n");
477  return AVERROR_INVALIDDATA;
478  }
479 
480  f->quant_table_count = get_symbol(c, state, 0);
481  if (f->quant_table_count > (unsigned)MAX_QUANT_TABLES)
482  return AVERROR_INVALIDDATA;
483 
484  for (i = 0; i < f->quant_table_count; i++) {
485  f->context_count[i] = read_quant_tables(c, f->quant_tables[i]);
486  if (f->context_count[i] < 0) {
487  av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
488  return AVERROR_INVALIDDATA;
489  }
490  }
491  if ((ret = ffv1_allocate_initial_states(f)) < 0)
492  return ret;
493 
494  for (i = 0; i < f->quant_table_count; i++)
495  if (get_rac(c, state)) {
496  for (j = 0; j < f->context_count[i]; j++)
497  for (k = 0; k < CONTEXT_SIZE; k++) {
498  int pred = j ? f->initial_states[i][j - 1][k] : 128;
499  f->initial_states[i][j][k] =
500  (pred + get_symbol(c, state2[k], 1)) & 0xFF;
501  }
502  }
503 
504  if (f->version > 2) {
505  f->ec = get_symbol(c, state, 0);
506  }
507 
508  if (f->version > 2) {
509  unsigned v;
512  if (v) {
513  av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", v);
514  return AVERROR_INVALIDDATA;
515  }
516  }
517 
518  return 0;
519 }
520 
521 static int read_header(FFV1Context *f)
522 {
524  int i, j, context_count = -1; //-1 to avoid warning
525  RangeCoder *const c = &f->slice_context[0]->c;
526 
527  memset(state, 128, sizeof(state));
528 
529  if (f->version < 2) {
531  unsigned v= get_symbol(c, state, 0);
532  if (v >= 2) {
533  av_log(f->avctx, AV_LOG_ERROR, "invalid version %d in ver01 header\n", v);
534  return AVERROR_INVALIDDATA;
535  }
536  f->version = v;
537  f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
538  if (f->ac > 1) {
539  for (i = 1; i < 256; i++)
540  f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
541  }
542 
543  colorspace = get_symbol(c, state, 0); //YUV cs type
544  bits_per_raw_sample = f->version > 0 ? get_symbol(c, state, 0) : f->avctx->bits_per_raw_sample;
545  chroma_planes = get_rac(c, state);
546  chroma_h_shift = get_symbol(c, state, 0);
547  chroma_v_shift = get_symbol(c, state, 0);
548  transparency = get_rac(c, state);
549 
550  if (f->plane_count) {
551  if ( colorspace != f->colorspace
552  || bits_per_raw_sample != f->avctx->bits_per_raw_sample
553  || chroma_planes != f->chroma_planes
554  || chroma_h_shift!= f->chroma_h_shift
555  || chroma_v_shift!= f->chroma_v_shift
556  || transparency != f->transparency) {
557  av_log(f->avctx, AV_LOG_ERROR, "Invalid change of global parameters\n");
558  return AVERROR_INVALIDDATA;
559  }
560  }
561 
562  f->colorspace = colorspace;
568 
569  f->plane_count = 2 + f->transparency;
570  }
571 
572  if (f->colorspace == 0) {
573  if (!f->transparency && !f->chroma_planes) {
574  if (f->avctx->bits_per_raw_sample <= 8)
576  else
578  } else if (f->avctx->bits_per_raw_sample<=8 && !f->transparency) {
579  switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
580  case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P; break;
581  case 0x01: f->avctx->pix_fmt = AV_PIX_FMT_YUV440P; break;
582  case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P; break;
583  case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P; break;
584  case 0x20: f->avctx->pix_fmt = AV_PIX_FMT_YUV411P; break;
585  case 0x22: f->avctx->pix_fmt = AV_PIX_FMT_YUV410P; break;
586  }
587  } else if (f->avctx->bits_per_raw_sample <= 8 && f->transparency) {
588  switch(16*f->chroma_h_shift + f->chroma_v_shift) {
589  case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P; break;
590  case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P; break;
591  case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P; break;
592  }
593  } else if (f->avctx->bits_per_raw_sample == 9 && !f->transparency) {
594  f->packed_at_lsb = 1;
595  switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
596  case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P9; break;
597  case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P9; break;
598  case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P9; break;
599  }
600  } else if (f->avctx->bits_per_raw_sample == 10 && !f->transparency) {
601  f->packed_at_lsb = 1;
602  switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
603  case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P10; break;
604  case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P10; break;
605  case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P10; break;
606  }
607  } else if (f->avctx->bits_per_raw_sample == 16 && !f->transparency){
608  switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
609  case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P16; break;
610  case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P16; break;
611  case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P16; break;
612  }
613  }
614  } else if (f->colorspace == 1) {
615  if (f->chroma_h_shift || f->chroma_v_shift) {
617  "chroma subsampling not supported in this colorspace\n");
618  return AVERROR(ENOSYS);
619  }
620  if ( f->avctx->bits_per_raw_sample == 9)
622  else if (f->avctx->bits_per_raw_sample == 10)
624  else if (f->avctx->bits_per_raw_sample == 12)
626  else if (f->avctx->bits_per_raw_sample == 14)
628  else
630  else f->avctx->pix_fmt = AV_PIX_FMT_0RGB32;
631  } else {
632  av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
633  return AVERROR(ENOSYS);
634  }
635  if (f->avctx->pix_fmt == AV_PIX_FMT_NONE) {
636  av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
637  return AVERROR(ENOSYS);
638  }
639 
640  av_dlog(f->avctx, "%d %d %d\n",
642  if (f->version < 2) {
643  context_count = read_quant_tables(c, f->quant_table);
644  if (context_count < 0) {
645  av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
646  return AVERROR_INVALIDDATA;
647  }
648  } else if (f->version < 3) {
649  f->slice_count = get_symbol(c, state, 0);
650  } else {
651  const uint8_t *p = c->bytestream_end;
652  for (f->slice_count = 0;
653  f->slice_count < MAX_SLICES && 3 < p - c->bytestream_start;
654  f->slice_count++) {
655  int trailer = 3 + 5*!!f->ec;
656  int size = AV_RB24(p-trailer);
657  if (size + trailer > p - c->bytestream_start)
658  break;
659  p -= size + trailer;
660  }
661  }
662  if (f->slice_count > (unsigned)MAX_SLICES || f->slice_count <= 0) {
663  av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid\n", f->slice_count);
664  return AVERROR_INVALIDDATA;
665  }
666 
667  for (j = 0; j < f->slice_count; j++) {
668  FFV1Context *fs = f->slice_context[j];
669  fs->ac = f->ac;
670  fs->packed_at_lsb = f->packed_at_lsb;
671 
672  fs->slice_damaged = 0;
673 
674  if (f->version == 2) {
675  fs->slice_x = get_symbol(c, state, 0) * f->width ;
676  fs->slice_y = get_symbol(c, state, 0) * f->height;
677  fs->slice_width = (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
678  fs->slice_height = (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
679 
680  fs->slice_x /= f->num_h_slices;
681  fs->slice_y /= f->num_v_slices;
682  fs->slice_width = fs->slice_width / f->num_h_slices - fs->slice_x;
683  fs->slice_height = fs->slice_height / f->num_v_slices - fs->slice_y;
684  if ((unsigned)fs->slice_width > f->width ||
685  (unsigned)fs->slice_height > f->height)
686  return AVERROR_INVALIDDATA;
687  if ( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
688  || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
689  return AVERROR_INVALIDDATA;
690  }
691 
692  for (i = 0; i < f->plane_count; i++) {
693  PlaneContext *const p = &fs->plane[i];
694 
695  if (f->version == 2) {
696  int idx = get_symbol(c, state, 0);
697  if (idx > (unsigned)f->quant_table_count) {
699  "quant_table_index out of range\n");
700  return AVERROR_INVALIDDATA;
701  }
702  p->quant_table_index = idx;
703  memcpy(p->quant_table, f->quant_tables[idx],
704  sizeof(p->quant_table));
705  context_count = f->context_count[idx];
706  } else {
707  memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
708  }
709 
710  if (f->version <= 2) {
711  av_assert0(context_count >= 0);
712  if (p->context_count < context_count) {
713  av_freep(&p->state);
714  av_freep(&p->vlc_state);
715  }
717  }
718  }
719  }
720  return 0;
721 }
722 
724 {
725  FFV1Context *f = avctx->priv_data;
726  int ret;
727 
728  if ((ret = ffv1_common_init(avctx)) < 0)
729  return ret;
730 
731  if (avctx->extradata && (ret = read_extra_header(f)) < 0)
732  return ret;
733 
734  if ((ret = ffv1_init_slice_contexts(f)) < 0)
735  return ret;
736 
737  return 0;
738 }
739 
740 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
741 {
742  const uint8_t *buf = avpkt->data;
743  int buf_size = avpkt->size;
744  FFV1Context *f = avctx->priv_data;
745  RangeCoder *const c = &f->slice_context[0]->c;
746  AVFrame *const p = &f->picture;
747  int i, ret;
748  uint8_t keystate = 128;
749  const uint8_t *buf_p;
750 
751  AVFrame *picture = data;
752 
753  /* release previously stored data */
754  if (p->data[0])
755  avctx->release_buffer(avctx, p);
756 
757  ff_init_range_decoder(c, buf, buf_size);
758  ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
759 
760  p->pict_type = AV_PICTURE_TYPE_I; //FIXME I vs. P
761  if (get_rac(c, &keystate)) {
762  p->key_frame = 1;
763  f->key_frame_ok = 0;
764  if ((ret = read_header(f)) < 0)
765  return ret;
766  f->key_frame_ok = 1;
767  } else {
768  if (!f->key_frame_ok) {
769  av_log(avctx, AV_LOG_ERROR,
770  "Cannot decode non-keyframe without valid keyframe\n");
771  return AVERROR_INVALIDDATA;
772  }
773  p->key_frame = 0;
774  }
775 
776  p->reference = 3; //for error concealment
777  if ((ret = ff_get_buffer(avctx, p)) < 0) {
778  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
779  return ret;
780  }
781 
782  if (avctx->debug & FF_DEBUG_PICT_INFO)
783  av_log(avctx, AV_LOG_DEBUG, "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
784  f->version, p->key_frame, f->ac, f->ec, f->slice_count, f->avctx->bits_per_raw_sample);
785 
786  buf_p = buf + buf_size;
787  for (i = f->slice_count - 1; i >= 0; i--) {
788  FFV1Context *fs = f->slice_context[i];
789  int trailer = 3 + 5*!!f->ec;
790  int v;
791 
792  if (i || f->version > 2) v = AV_RB24(buf_p-trailer) + trailer;
793  else v = buf_p - c->bytestream_start;
794  if (buf_p - c->bytestream_start < v) {
795  av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
796  return AVERROR_INVALIDDATA;
797  }
798  buf_p -= v;
799 
800  if (f->ec) {
801  unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, v);
802  if (crc) {
803  int64_t ts = avpkt->pts != AV_NOPTS_VALUE ? avpkt->pts : avpkt->dts;
804  av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!", crc);
805  if (ts != AV_NOPTS_VALUE && avctx->pkt_timebase.num) {
806  av_log(f->avctx, AV_LOG_ERROR, "at %f seconds\n", ts*av_q2d(avctx->pkt_timebase));
807  } else if (ts != AV_NOPTS_VALUE) {
808  av_log(f->avctx, AV_LOG_ERROR, "at %"PRId64"\n", ts);
809  } else {
810  av_log(f->avctx, AV_LOG_ERROR, "\n");
811  }
812  fs->slice_damaged = 1;
813  }
814  }
815 
816  if (i) {
817  ff_init_range_decoder(&fs->c, buf_p, v);
818  } else
819  fs->c.bytestream_end = (uint8_t *)(buf_p + v);
820  }
821 
822  avctx->execute(avctx,
823  decode_slice,
824  &f->slice_context[0],
825  NULL,
826  f->slice_count,
827  sizeof(void*));
828 
829  for (i = f->slice_count - 1; i >= 0; i--) {
830  FFV1Context *fs = f->slice_context[i];
831  int j;
832  if (fs->slice_damaged && f->last_picture.data[0]) {
833  const uint8_t *src[4];
834  uint8_t *dst[4];
835  for (j = 0; j < 4; j++) {
836  int sh = (j==1 || j==2) ? f->chroma_h_shift : 0;
837  int sv = (j==1 || j==2) ? f->chroma_v_shift : 0;
838  dst[j] = f->picture .data[j] + f->picture .linesize[j]*
839  (fs->slice_y>>sv) + (fs->slice_x>>sh);
840  src[j] = f->last_picture.data[j] + f->last_picture.linesize[j]*
841  (fs->slice_y>>sv) + (fs->slice_x>>sh);
842  }
843  av_image_copy(dst,
844  f->picture.linesize,
845  (const uint8_t **)src,
847  avctx->pix_fmt,
848  fs->slice_width,
849  fs->slice_height);
850  }
851  }
852 
853  f->picture_number++;
854 
855  *picture = *p;
856  *got_frame = 1;
857 
859 
860  return buf_size;
861 }
862 
864  .name = "ffv1",
865  .type = AVMEDIA_TYPE_VIDEO,
866  .id = AV_CODEC_ID_FFV1,
867  .priv_data_size = sizeof(FFV1Context),
868  .init = decode_init,
869  .close = ffv1_close,
870  .decode = decode_frame,
871  .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/ |
873  .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
874 };