FFmpeg  2.6.9
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ffv1dec.c
Go to the documentation of this file.
1 /*
2  * FFV1 decoder
3  *
4  * Copyright (c) 2003-2013 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 /**
24  * @file
25  * FF Video Codec 1 (a lossless codec) decoder
26  */
27 
28 #include "libavutil/avassert.h"
29 #include "libavutil/crc.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/pixdesc.h"
33 #include "libavutil/timer.h"
34 #include "avcodec.h"
35 #include "internal.h"
36 #include "get_bits.h"
37 #include "rangecoder.h"
38 #include "golomb.h"
39 #include "mathops.h"
40 #include "ffv1.h"
41 
43  int is_signed)
44 {
45  if (get_rac(c, state + 0))
46  return 0;
47  else {
48  int i, e, 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 
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  if (s->slice_coding_mode == 1) {
112  int i;
113  for (x = 0; x < w; x++) {
114  int v = 0;
115  for (i=0; i<bits; i++) {
116  uint8_t state = 128;
117  v += v + get_rac(c, &state);
118  }
119  sample[1][x] = v;
120  }
121  return;
122  }
123 
124  for (x = 0; x < w; x++) {
125  int diff, context, sign;
126 
127  context = get_context(p, sample[1] + x, sample[0] + x, sample[1] + x);
128  if (context < 0) {
129  context = -context;
130  sign = 1;
131  } else
132  sign = 0;
133 
134  av_assert2(context < p->context_count);
135 
136  if (s->ac) {
137  diff = get_symbol_inline(c, p->state[context], 1);
138  } else {
139  if (context == 0 && run_mode == 0)
140  run_mode = 1;
141 
142  if (run_mode) {
143  if (run_count == 0 && run_mode == 1) {
144  if (get_bits1(&s->gb)) {
145  run_count = 1 << ff_log2_run[run_index];
146  if (x + run_count <= w)
147  run_index++;
148  } else {
149  if (ff_log2_run[run_index])
150  run_count = get_bits(&s->gb, ff_log2_run[run_index]);
151  else
152  run_count = 0;
153  if (run_index)
154  run_index--;
155  run_mode = 2;
156  }
157  }
158  run_count--;
159  if (run_count < 0) {
160  run_mode = 0;
161  run_count = 0;
162  diff = get_vlc_symbol(&s->gb, &p->vlc_state[context],
163  bits);
164  if (diff >= 0)
165  diff++;
166  } else
167  diff = 0;
168  } else
169  diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
170 
171  av_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
172  run_count, run_index, run_mode, x, get_bits_count(&s->gb));
173  }
174 
175  if (sign)
176  diff = -diff;
177 
178  sample[1][x] = (predict(sample[1] + x, sample[0] + x) + diff) &
179  ((1 << bits) - 1);
180  }
181  s->run_index = run_index;
182 }
183 
185  int w, int h, int stride, int plane_index)
186 {
187  int x, y;
188  int16_t *sample[2];
189  sample[0] = s->sample_buffer + 3;
190  sample[1] = s->sample_buffer + w + 6 + 3;
191 
192  s->run_index = 0;
193 
194  memset(s->sample_buffer, 0, 2 * (w + 6) * sizeof(*s->sample_buffer));
195 
196  for (y = 0; y < h; y++) {
197  int16_t *temp = sample[0]; // FIXME: try a normal buffer
198 
199  sample[0] = sample[1];
200  sample[1] = temp;
201 
202  sample[1][-1] = sample[0][0];
203  sample[0][w] = sample[0][w - 1];
204 
205 // { START_TIMER
206  if (s->avctx->bits_per_raw_sample <= 8) {
207  decode_line(s, w, sample, plane_index, 8);
208  for (x = 0; x < w; x++)
209  src[x + stride * y] = sample[1][x];
210  } else {
211  decode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
212  if (s->packed_at_lsb) {
213  for (x = 0; x < w; x++) {
214  ((uint16_t*)(src + stride*y))[x] = sample[1][x];
215  }
216  } else {
217  for (x = 0; x < w; x++) {
218  ((uint16_t*)(src + stride*y))[x] = sample[1][x] << (16 - s->avctx->bits_per_raw_sample);
219  }
220  }
221  }
222 // STOP_TIMER("decode-line") }
223  }
224 }
225 
226 static void decode_rgb_frame(FFV1Context *s, uint8_t *src[3], int w, int h, int stride[3])
227 {
228  int x, y, p;
229  int16_t *sample[4][2];
230  int lbd = s->avctx->bits_per_raw_sample <= 8;
231  int bits = s->avctx->bits_per_raw_sample > 0 ? s->avctx->bits_per_raw_sample : 8;
232  int offset = 1 << bits;
233 
234  for (x = 0; x < 4; x++) {
235  sample[x][0] = s->sample_buffer + x * 2 * (w + 6) + 3;
236  sample[x][1] = s->sample_buffer + (x * 2 + 1) * (w + 6) + 3;
237  }
238 
239  s->run_index = 0;
240 
241  memset(s->sample_buffer, 0, 8 * (w + 6) * sizeof(*s->sample_buffer));
242 
243  for (y = 0; y < h; y++) {
244  for (p = 0; p < 3 + s->transparency; p++) {
245  int16_t *temp = sample[p][0]; // FIXME: try a normal buffer
246 
247  sample[p][0] = sample[p][1];
248  sample[p][1] = temp;
249 
250  sample[p][1][-1]= sample[p][0][0 ];
251  sample[p][0][ w]= sample[p][0][w-1];
252  if (lbd && s->slice_coding_mode == 0)
253  decode_line(s, w, sample[p], (p + 1)/2, 9);
254  else
255  decode_line(s, w, sample[p], (p + 1)/2, bits + (s->slice_coding_mode != 1));
256  }
257  for (x = 0; x < w; x++) {
258  int g = sample[0][1][x];
259  int b = sample[1][1][x];
260  int r = sample[2][1][x];
261  int a = sample[3][1][x];
262 
263  if (s->slice_coding_mode != 1) {
264  b -= offset;
265  r -= offset;
266  g -= (b * s->slice_rct_by_coef + r * s->slice_rct_ry_coef) >> 2;
267  b += g;
268  r += g;
269  }
270 
271  if (lbd)
272  *((uint32_t*)(src[0] + x*4 + stride[0]*y)) = b + (g<<8) + (r<<16) + (a<<24);
273  else {
274  *((uint16_t*)(src[0] + x*2 + stride[0]*y)) = b;
275  *((uint16_t*)(src[1] + x*2 + stride[1]*y)) = g;
276  *((uint16_t*)(src[2] + x*2 + stride[2]*y)) = r;
277  }
278  }
279  }
280 }
281 
283 {
284  RangeCoder *c = &fs->c;
286  unsigned ps, i, context_count;
287  memset(state, 128, sizeof(state));
288 
289  av_assert0(f->version > 2);
290 
291  fs->slice_x = get_symbol(c, state, 0) * f->width ;
292  fs->slice_y = get_symbol(c, state, 0) * f->height;
293  fs->slice_width = (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
294  fs->slice_height = (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
295 
296  fs->slice_x /= f->num_h_slices;
297  fs->slice_y /= f->num_v_slices;
298  fs->slice_width = fs->slice_width /f->num_h_slices - fs->slice_x;
299  fs->slice_height = fs->slice_height/f->num_v_slices - fs->slice_y;
300  if ((unsigned)fs->slice_width > f->width || (unsigned)fs->slice_height > f->height)
301  return -1;
302  if ( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
303  || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
304  return -1;
305 
306  for (i = 0; i < f->plane_count; i++) {
307  PlaneContext * const p = &fs->plane[i];
308  int idx = get_symbol(c, state, 0);
309  if (idx >= (unsigned)f->quant_table_count) {
310  av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
311  return -1;
312  }
313  p->quant_table_index = idx;
314  memcpy(p->quant_table, f->quant_tables[idx], sizeof(p->quant_table));
315  context_count = f->context_count[idx];
316 
317  if (p->context_count < context_count) {
318  av_freep(&p->state);
319  av_freep(&p->vlc_state);
320  }
322  }
323 
324  ps = get_symbol(c, state, 0);
325  if (ps == 1) {
326  f->cur->interlaced_frame = 1;
327  f->cur->top_field_first = 1;
328  } else if (ps == 2) {
329  f->cur->interlaced_frame = 1;
330  f->cur->top_field_first = 0;
331  } else if (ps == 3) {
332  f->cur->interlaced_frame = 0;
333  }
334  f->cur->sample_aspect_ratio.num = get_symbol(c, state, 0);
335  f->cur->sample_aspect_ratio.den = get_symbol(c, state, 0);
336 
337  if (av_image_check_sar(f->width, f->height,
338  f->cur->sample_aspect_ratio) < 0) {
339  av_log(f->avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
342  f->cur->sample_aspect_ratio = (AVRational){ 0, 1 };
343  }
344 
345  if (fs->version > 3) {
346  fs->slice_reset_contexts = get_rac(c, state);
347  fs->slice_coding_mode = get_symbol(c, state, 0);
348  if (fs->slice_coding_mode != 1) {
349  fs->slice_rct_by_coef = get_symbol(c, state, 0);
350  fs->slice_rct_ry_coef = get_symbol(c, state, 0);
351  if ((uint64_t)fs->slice_rct_by_coef + (uint64_t)fs->slice_rct_ry_coef > 4) {
352  av_log(f->avctx, AV_LOG_ERROR, "slice_rct_y_coef out of range\n");
353  return AVERROR_INVALIDDATA;
354  }
355  }
356  }
357 
358  return 0;
359 }
360 
361 static int decode_slice(AVCodecContext *c, void *arg)
362 {
363  FFV1Context *fs = *(void **)arg;
364  FFV1Context *f = fs->avctx->priv_data;
365  int width, height, x, y, ret;
366  const int ps = av_pix_fmt_desc_get(c->pix_fmt)->comp[0].step_minus1 + 1;
367  AVFrame * const p = f->cur;
368  int i, si;
369 
370  for( si=0; fs != f->slice_context[si]; si ++)
371  ;
372 
373  if(f->fsrc && !p->key_frame)
375 
376  if(f->fsrc && !p->key_frame) {
377  FFV1Context *fssrc = f->fsrc->slice_context[si];
378  FFV1Context *fsdst = f->slice_context[si];
379  av_assert1(fsdst->plane_count == fssrc->plane_count);
380  av_assert1(fsdst == fs);
381 
382  if (!p->key_frame)
383  fsdst->slice_damaged |= fssrc->slice_damaged;
384 
385  for (i = 0; i < f->plane_count; i++) {
386  PlaneContext *psrc = &fssrc->plane[i];
387  PlaneContext *pdst = &fsdst->plane[i];
388 
389  av_free(pdst->state);
390  av_free(pdst->vlc_state);
391  memcpy(pdst, psrc, sizeof(*pdst));
392  pdst->state = NULL;
393  pdst->vlc_state = NULL;
394 
395  if (fssrc->ac) {
397  memcpy(pdst->state, psrc->state, CONTEXT_SIZE * psrc->context_count);
398  } else {
399  pdst->vlc_state = av_malloc_array(sizeof(*pdst->vlc_state), psrc->context_count);
400  memcpy(pdst->vlc_state, psrc->vlc_state, sizeof(*pdst->vlc_state) * psrc->context_count);
401  }
402  }
403  }
404 
405  fs->slice_rct_by_coef = 1;
406  fs->slice_rct_ry_coef = 1;
407 
408  if (f->version > 2) {
409  if (ffv1_init_slice_state(f, fs) < 0)
410  return AVERROR(ENOMEM);
411  if (decode_slice_header(f, fs) < 0) {
412  fs->slice_x = fs->slice_y = fs->slice_height = fs->slice_width = 0;
413  fs->slice_damaged = 1;
414  return AVERROR_INVALIDDATA;
415  }
416  }
417  if ((ret = ffv1_init_slice_state(f, fs)) < 0)
418  return ret;
419  if (f->cur->key_frame || fs->slice_reset_contexts)
420  ffv1_clear_slice_state(f, fs);
421 
422  width = fs->slice_width;
423  height = fs->slice_height;
424  x = fs->slice_x;
425  y = fs->slice_y;
426 
427  if (!fs->ac) {
428  if (f->version == 3 && f->micro_version > 1 || f->version > 3)
429  get_rac(&fs->c, (uint8_t[]) { 129 });
430  fs->ac_byte_count = f->version > 2 || (!x && !y) ? fs->c.bytestream - fs->c.bytestream_start - 1 : 0;
431  init_get_bits(&fs->gb,
432  fs->c.bytestream_start + fs->ac_byte_count,
433  (fs->c.bytestream_end - fs->c.bytestream_start - fs->ac_byte_count) * 8);
434  }
435 
436  av_assert1(width && height);
437  if (f->colorspace == 0) {
438  const int chroma_width = FF_CEIL_RSHIFT(width, f->chroma_h_shift);
439  const int chroma_height = FF_CEIL_RSHIFT(height, f->chroma_v_shift);
440  const int cx = x >> f->chroma_h_shift;
441  const int cy = y >> f->chroma_v_shift;
442  decode_plane(fs, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0);
443 
444  if (f->chroma_planes) {
445  decode_plane(fs, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
446  decode_plane(fs, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1);
447  }
448  if (fs->transparency)
449  decode_plane(fs, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], 2);
450  } else {
451  uint8_t *planes[3] = { p->data[0] + ps * x + y * p->linesize[0],
452  p->data[1] + ps * x + y * p->linesize[1],
453  p->data[2] + ps * x + y * p->linesize[2] };
454  decode_rgb_frame(fs, planes, width, height, p->linesize);
455  }
456  if (fs->ac && f->version > 2) {
457  int v;
458  get_rac(&fs->c, (uint8_t[]) { 129 });
459  v = fs->c.bytestream_end - fs->c.bytestream - 2 - 5*f->ec;
460  if (v) {
461  av_log(f->avctx, AV_LOG_ERROR, "bytestream end mismatching by %d\n", v);
462  fs->slice_damaged = 1;
463  }
464  }
465 
466  emms_c();
467 
468  ff_thread_report_progress(&f->picture, si, 0);
469 
470  return 0;
471 }
472 
473 static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale)
474 {
475  int v;
476  int i = 0;
478 
479  memset(state, 128, sizeof(state));
480 
481  for (v = 0; i < 128; v++) {
482  unsigned len = get_symbol(c, state, 0) + 1;
483 
484  if (len > 128 - i)
485  return AVERROR_INVALIDDATA;
486 
487  while (len--) {
488  quant_table[i] = scale * v;
489  i++;
490  }
491  }
492 
493  for (i = 1; i < 128; i++)
494  quant_table[256 - i] = -quant_table[i];
495  quant_table[128] = -quant_table[127];
496 
497  return 2 * v - 1;
498 }
499 
501  int16_t quant_table[MAX_CONTEXT_INPUTS][256])
502 {
503  int i;
504  int context_count = 1;
505 
506  for (i = 0; i < 5; i++) {
507  int ret = read_quant_table(c, quant_table[i], context_count);
508  if (ret < 0)
509  return ret;
510  context_count *= ret;
511  if (context_count > 32768U) {
512  return AVERROR_INVALIDDATA;
513  }
514  }
515  return (context_count + 1) / 2;
516 }
517 
519 {
520  RangeCoder *const c = &f->c;
522  int i, j, k, ret;
523  uint8_t state2[32][CONTEXT_SIZE];
524 
525  memset(state2, 128, sizeof(state2));
526  memset(state, 128, sizeof(state));
527 
529  ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
530 
531  f->version = get_symbol(c, state, 0);
532  if (f->version < 2) {
533  av_log(f->avctx, AV_LOG_ERROR, "Invalid version in global header\n");
534  return AVERROR_INVALIDDATA;
535  }
536  if (f->version > 2) {
537  c->bytestream_end -= 4;
538  f->micro_version = get_symbol(c, state, 0);
539  }
540  f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
541  if (f->ac > 1) {
542  for (i = 1; i < 256; i++)
543  f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
544  }
545 
546  f->colorspace = get_symbol(c, state, 0); //YUV cs type
547  f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
548  f->chroma_planes = get_rac(c, state);
549  f->chroma_h_shift = get_symbol(c, state, 0);
550  f->chroma_v_shift = get_symbol(c, state, 0);
551  f->transparency = get_rac(c, state);
552  f->plane_count = 1 + (f->chroma_planes || f->version<4) + f->transparency;
553  f->num_h_slices = 1 + get_symbol(c, state, 0);
554  f->num_v_slices = 1 + get_symbol(c, state, 0);
555 
556  if (f->chroma_h_shift > 4U || f->chroma_v_shift > 4U) {
557  av_log(f->avctx, AV_LOG_ERROR, "chroma shift parameters %d %d are invalid\n",
559  return AVERROR_INVALIDDATA;
560  }
561 
562  if (f->num_h_slices > (unsigned)f->width || !f->num_h_slices ||
563  f->num_v_slices > (unsigned)f->height || !f->num_v_slices
564  ) {
565  av_log(f->avctx, AV_LOG_ERROR, "slice count invalid\n");
566  return AVERROR_INVALIDDATA;
567  }
568 
569  f->quant_table_count = get_symbol(c, state, 0);
570  if (f->quant_table_count > (unsigned)MAX_QUANT_TABLES || !f->quant_table_count) {
571  av_log(f->avctx, AV_LOG_ERROR, "quant table count %d is invalid\n", f->quant_table_count);
572  f->quant_table_count = 0;
573  return AVERROR_INVALIDDATA;
574  }
575 
576  for (i = 0; i < f->quant_table_count; i++) {
577  f->context_count[i] = read_quant_tables(c, f->quant_tables[i]);
578  if (f->context_count[i] < 0) {
579  av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
580  return AVERROR_INVALIDDATA;
581  }
582  }
583  if ((ret = ffv1_allocate_initial_states(f)) < 0)
584  return ret;
585 
586  for (i = 0; i < f->quant_table_count; i++)
587  if (get_rac(c, state)) {
588  for (j = 0; j < f->context_count[i]; j++)
589  for (k = 0; k < CONTEXT_SIZE; k++) {
590  int pred = j ? f->initial_states[i][j - 1][k] : 128;
591  f->initial_states[i][j][k] =
592  (pred + get_symbol(c, state2[k], 1)) & 0xFF;
593  }
594  }
595 
596  if (f->version > 2) {
597  f->ec = get_symbol(c, state, 0);
598  if (f->micro_version > 2)
599  f->intra = get_symbol(c, state, 0);
600  }
601 
602  if (f->version > 2) {
603  unsigned v;
606  if (v) {
607  av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", v);
608  return AVERROR_INVALIDDATA;
609  }
610  }
611 
612  if (f->avctx->debug & FF_DEBUG_PICT_INFO)
614  "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\n",
615  f->version, f->micro_version,
616  f->ac,
617  f->colorspace,
620  f->transparency,
621  f->num_h_slices, f->num_v_slices,
623  f->ec,
624  f->intra
625  );
626  return 0;
627 }
628 
629 static int read_header(FFV1Context *f)
630 {
632  int i, j, context_count = -1; //-1 to avoid warning
633  RangeCoder *const c = &f->slice_context[0]->c;
634 
635  memset(state, 128, sizeof(state));
636 
637  if (f->version < 2) {
639  unsigned v= get_symbol(c, state, 0);
640  if (v >= 2) {
641  av_log(f->avctx, AV_LOG_ERROR, "invalid version %d in ver01 header\n", v);
642  return AVERROR_INVALIDDATA;
643  }
644  f->version = v;
645  f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
646  if (f->ac > 1) {
647  for (i = 1; i < 256; i++)
648  f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
649  }
650 
651  colorspace = get_symbol(c, state, 0); //YUV cs type
652  bits_per_raw_sample = f->version > 0 ? get_symbol(c, state, 0) : f->avctx->bits_per_raw_sample;
653  chroma_planes = get_rac(c, state);
654  chroma_h_shift = get_symbol(c, state, 0);
655  chroma_v_shift = get_symbol(c, state, 0);
656  transparency = get_rac(c, state);
657 
658  if (f->plane_count) {
659  if (colorspace != f->colorspace ||
660  bits_per_raw_sample != f->avctx->bits_per_raw_sample ||
661  chroma_planes != f->chroma_planes ||
662  chroma_h_shift != f->chroma_h_shift ||
663  chroma_v_shift != f->chroma_v_shift ||
664  transparency != f->transparency) {
665  av_log(f->avctx, AV_LOG_ERROR, "Invalid change of global parameters\n");
666  return AVERROR_INVALIDDATA;
667  }
668  }
669 
670  if (chroma_h_shift > 4U || chroma_v_shift > 4U) {
671  av_log(f->avctx, AV_LOG_ERROR, "chroma shift parameters %d %d are invalid\n",
672  chroma_h_shift, chroma_v_shift);
673  return AVERROR_INVALIDDATA;
674  }
675 
676  f->colorspace = colorspace;
682 
683  f->plane_count = 2 + f->transparency;
684  }
685 
686  if (f->colorspace == 0) {
687  if (f->avctx->skip_alpha) f->transparency = 0;
688  if (!f->transparency && !f->chroma_planes) {
689  if (f->avctx->bits_per_raw_sample <= 8)
691  else
693  } else if (f->avctx->bits_per_raw_sample<=8 && !f->transparency) {
694  switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
695  case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P; break;
696  case 0x01: f->avctx->pix_fmt = AV_PIX_FMT_YUV440P; break;
697  case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P; break;
698  case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P; break;
699  case 0x20: f->avctx->pix_fmt = AV_PIX_FMT_YUV411P; break;
700  case 0x22: f->avctx->pix_fmt = AV_PIX_FMT_YUV410P; break;
701  }
702  } else if (f->avctx->bits_per_raw_sample <= 8 && f->transparency) {
703  switch(16*f->chroma_h_shift + f->chroma_v_shift) {
704  case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P; break;
705  case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P; break;
706  case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P; break;
707  }
708  } else if (f->avctx->bits_per_raw_sample == 9 && !f->transparency) {
709  f->packed_at_lsb = 1;
710  switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
711  case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P9; break;
712  case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P9; break;
713  case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P9; break;
714  }
715  } else if (f->avctx->bits_per_raw_sample == 9 && f->transparency) {
716  f->packed_at_lsb = 1;
717  switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
718  case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P9; break;
719  case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P9; break;
720  case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P9; break;
721  }
722  } else if (f->avctx->bits_per_raw_sample == 10 && !f->transparency) {
723  f->packed_at_lsb = 1;
724  switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
725  case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P10; break;
726  case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P10; break;
727  case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P10; break;
728  }
729  } else if (f->avctx->bits_per_raw_sample == 10 && f->transparency) {
730  f->packed_at_lsb = 1;
731  switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
732  case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P10; break;
733  case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P10; break;
734  case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P10; break;
735  }
736  } else if (f->avctx->bits_per_raw_sample == 16 && !f->transparency){
737  switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
738  case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P16; break;
739  case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P16; break;
740  case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P16; break;
741  }
742  } else if (f->avctx->bits_per_raw_sample == 16 && f->transparency){
743  switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
744  case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P16; break;
745  case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P16; break;
746  case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P16; break;
747  }
748  }
749  } else if (f->colorspace == 1) {
750  if (f->chroma_h_shift || f->chroma_v_shift) {
752  "chroma subsampling not supported in this colorspace\n");
753  return AVERROR(ENOSYS);
754  }
755  if ( f->avctx->bits_per_raw_sample == 9)
757  else if (f->avctx->bits_per_raw_sample == 10)
759  else if (f->avctx->bits_per_raw_sample == 12)
761  else if (f->avctx->bits_per_raw_sample == 14)
763  else
765  else f->avctx->pix_fmt = AV_PIX_FMT_0RGB32;
766  } else {
767  av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
768  return AVERROR(ENOSYS);
769  }
770  if (f->avctx->pix_fmt == AV_PIX_FMT_NONE) {
771  av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
772  return AVERROR(ENOSYS);
773  }
774 
775  av_dlog(f->avctx, "%d %d %d\n",
777  if (f->version < 2) {
778  context_count = read_quant_tables(c, f->quant_table);
779  if (context_count < 0) {
780  av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
781  return AVERROR_INVALIDDATA;
782  }
784  } else if (f->version < 3) {
785  f->slice_count = get_symbol(c, state, 0);
786  } else {
787  const uint8_t *p = c->bytestream_end;
788  for (f->slice_count = 0;
789  f->slice_count < MAX_SLICES && 3 < p - c->bytestream_start;
790  f->slice_count++) {
791  int trailer = 3 + 5*!!f->ec;
792  int size = AV_RB24(p-trailer);
793  if (size + trailer > p - c->bytestream_start)
794  break;
795  p -= size + trailer;
796  }
797  }
798  if (f->slice_count > (unsigned)MAX_SLICES || f->slice_count <= 0 || f->slice_count > f->max_slice_count) {
799  av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid (max=%d)\n", f->slice_count, f->max_slice_count);
800  return AVERROR_INVALIDDATA;
801  }
802 
803  for (j = 0; j < f->slice_count; j++) {
804  FFV1Context *fs = f->slice_context[j];
805  fs->ac = f->ac;
806  fs->packed_at_lsb = f->packed_at_lsb;
807 
808  fs->slice_damaged = 0;
809 
810  if (f->version == 2) {
811  fs->slice_x = get_symbol(c, state, 0) * f->width ;
812  fs->slice_y = get_symbol(c, state, 0) * f->height;
813  fs->slice_width = (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
814  fs->slice_height = (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
815 
816  fs->slice_x /= f->num_h_slices;
817  fs->slice_y /= f->num_v_slices;
818  fs->slice_width = fs->slice_width / f->num_h_slices - fs->slice_x;
819  fs->slice_height = fs->slice_height / f->num_v_slices - fs->slice_y;
820  if ((unsigned)fs->slice_width > f->width ||
821  (unsigned)fs->slice_height > f->height)
822  return AVERROR_INVALIDDATA;
823  if ( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
824  || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
825  return AVERROR_INVALIDDATA;
826  }
827 
828  for (i = 0; i < f->plane_count; i++) {
829  PlaneContext *const p = &fs->plane[i];
830 
831  if (f->version == 2) {
832  int idx = get_symbol(c, state, 0);
833  if (idx > (unsigned)f->quant_table_count) {
835  "quant_table_index out of range\n");
836  return AVERROR_INVALIDDATA;
837  }
838  p->quant_table_index = idx;
839  memcpy(p->quant_table, f->quant_tables[idx],
840  sizeof(p->quant_table));
841  context_count = f->context_count[idx];
842  } else {
843  memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
844  }
845 
846  if (f->version <= 2) {
847  av_assert0(context_count >= 0);
848  if (p->context_count < context_count) {
849  av_freep(&p->state);
850  av_freep(&p->vlc_state);
851  }
853  }
854  }
855  }
856  return 0;
857 }
858 
860 {
861  FFV1Context *f = avctx->priv_data;
862  int ret;
863 
864  if ((ret = ffv1_common_init(avctx)) < 0)
865  return ret;
866 
867  if (avctx->extradata && (ret = read_extra_header(f)) < 0)
868  return ret;
869 
870  if ((ret = ffv1_init_slice_contexts(f)) < 0)
871  return ret;
872 
873  avctx->internal->allocate_progress = 1;
874 
875  return 0;
876 }
877 
878 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
879 {
880  uint8_t *buf = avpkt->data;
881  int buf_size = avpkt->size;
882  FFV1Context *f = avctx->priv_data;
883  RangeCoder *const c = &f->slice_context[0]->c;
884  int i, ret;
885  uint8_t keystate = 128;
886  uint8_t *buf_p;
887  AVFrame *p;
888 
889  if (f->last_picture.f)
892 
893  f->cur = p = f->picture.f;
894 
895  if (f->version < 3 && avctx->field_order > AV_FIELD_PROGRESSIVE) {
896  /* we have interlaced material flagged in container */
897  p->interlaced_frame = 1;
898  if (avctx->field_order == AV_FIELD_TT || avctx->field_order == AV_FIELD_TB)
899  p->top_field_first = 1;
900  }
901 
902  f->avctx = avctx;
903  ff_init_range_decoder(c, buf, buf_size);
904  ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
905 
906  p->pict_type = AV_PICTURE_TYPE_I; //FIXME I vs. P
907  if (get_rac(c, &keystate)) {
908  p->key_frame = 1;
909  f->key_frame_ok = 0;
910  if ((ret = read_header(f)) < 0)
911  return ret;
912  f->key_frame_ok = 1;
913  } else {
914  if (!f->key_frame_ok) {
915  av_log(avctx, AV_LOG_ERROR,
916  "Cannot decode non-keyframe without valid keyframe\n");
917  return AVERROR_INVALIDDATA;
918  }
919  p->key_frame = 0;
920  }
921 
922  if ((ret = ff_thread_get_buffer(avctx, &f->picture, AV_GET_BUFFER_FLAG_REF)) < 0)
923  return ret;
924 
925  if (avctx->debug & FF_DEBUG_PICT_INFO)
926  av_log(avctx, AV_LOG_DEBUG, "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
927  f->version, p->key_frame, f->ac, f->ec, f->slice_count, f->avctx->bits_per_raw_sample);
928 
929  ff_thread_finish_setup(avctx);
930 
931  buf_p = buf + buf_size;
932  for (i = f->slice_count - 1; i >= 0; i--) {
933  FFV1Context *fs = f->slice_context[i];
934  int trailer = 3 + 5*!!f->ec;
935  int v;
936 
937  if (i || f->version > 2) v = AV_RB24(buf_p-trailer) + trailer;
938  else v = buf_p - c->bytestream_start;
939  if (buf_p - c->bytestream_start < v) {
940  av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
941  ff_thread_report_progress(&f->picture, INT_MAX, 0);
942  return AVERROR_INVALIDDATA;
943  }
944  buf_p -= v;
945 
946  if (f->ec) {
947  unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, v);
948  if (crc) {
949  int64_t ts = avpkt->pts != AV_NOPTS_VALUE ? avpkt->pts : avpkt->dts;
950  av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!", crc);
951  if (ts != AV_NOPTS_VALUE && avctx->pkt_timebase.num) {
952  av_log(f->avctx, AV_LOG_ERROR, "at %f seconds\n", ts*av_q2d(avctx->pkt_timebase));
953  } else if (ts != AV_NOPTS_VALUE) {
954  av_log(f->avctx, AV_LOG_ERROR, "at %"PRId64"\n", ts);
955  } else {
956  av_log(f->avctx, AV_LOG_ERROR, "\n");
957  }
958  fs->slice_damaged = 1;
959  }
960  }
961 
962  if (i) {
963  ff_init_range_decoder(&fs->c, buf_p, v);
964  } else
965  fs->c.bytestream_end = buf_p + v;
966 
967  fs->avctx = avctx;
968  fs->cur = p;
969  }
970 
971  avctx->execute(avctx,
972  decode_slice,
973  &f->slice_context[0],
974  NULL,
975  f->slice_count,
976  sizeof(void*));
977 
978  for (i = f->slice_count - 1; i >= 0; i--) {
979  FFV1Context *fs = f->slice_context[i];
980  int j;
981  if (fs->slice_damaged && f->last_picture.f->data[0]) {
982  const uint8_t *src[4];
983  uint8_t *dst[4];
984  ff_thread_await_progress(&f->last_picture, INT_MAX, 0);
985  for (j = 0; j < 4; j++) {
986  int sh = (j == 1 || j == 2) ? f->chroma_h_shift : 0;
987  int sv = (j == 1 || j == 2) ? f->chroma_v_shift : 0;
988  dst[j] = p->data[j] + p->linesize[j] *
989  (fs->slice_y >> sv) + (fs->slice_x >> sh);
990  src[j] = f->last_picture.f->data[j] + f->last_picture.f->linesize[j] *
991  (fs->slice_y >> sv) + (fs->slice_x >> sh);
992  }
993  av_image_copy(dst, p->linesize, src,
994  f->last_picture.f->linesize,
995  avctx->pix_fmt,
996  fs->slice_width,
997  fs->slice_height);
998  }
999  }
1000  ff_thread_report_progress(&f->picture, INT_MAX, 0);
1001 
1002  f->picture_number++;
1003 
1004  if (f->last_picture.f)
1006  f->cur = NULL;
1007  if ((ret = av_frame_ref(data, f->picture.f)) < 0)
1008  return ret;
1009 
1010  *got_frame = 1;
1011 
1012  return buf_size;
1013 }
1014 
1016 {
1017  FFV1Context *f = avctx->priv_data;
1018  int i, ret;
1019 
1020  f->picture.f = NULL;
1021  f->last_picture.f = NULL;
1022  f->sample_buffer = NULL;
1023  f->max_slice_count = 0;
1024  f->slice_count = 0;
1025 
1026  for (i = 0; i < f->quant_table_count; i++) {
1027  av_assert0(f->version > 1);
1028  f->initial_states[i] = av_memdup(f->initial_states[i],
1029  f->context_count[i] * sizeof(*f->initial_states[i]));
1030  }
1031 
1032  f->picture.f = av_frame_alloc();
1033  f->last_picture.f = av_frame_alloc();
1034 
1035  if ((ret = ffv1_init_slice_contexts(f)) < 0)
1036  return ret;
1037 
1038  return 0;
1039 }
1040 
1041 static void copy_fields(FFV1Context *fsdst, FFV1Context *fssrc, FFV1Context *fsrc)
1042 {
1043  fsdst->version = fsrc->version;
1044  fsdst->micro_version = fsrc->micro_version;
1045  fsdst->chroma_planes = fsrc->chroma_planes;
1046  fsdst->chroma_h_shift = fsrc->chroma_h_shift;
1047  fsdst->chroma_v_shift = fsrc->chroma_v_shift;
1048  fsdst->transparency = fsrc->transparency;
1049  fsdst->plane_count = fsrc->plane_count;
1050  fsdst->ac = fsrc->ac;
1051  fsdst->colorspace = fsrc->colorspace;
1052 
1053  fsdst->ec = fsrc->ec;
1054  fsdst->intra = fsrc->intra;
1055  fsdst->slice_damaged = fssrc->slice_damaged;
1056  fsdst->key_frame_ok = fsrc->key_frame_ok;
1057 
1059  fsdst->packed_at_lsb = fsrc->packed_at_lsb;
1060  fsdst->slice_count = fsrc->slice_count;
1061  if (fsrc->version<3){
1062  fsdst->slice_x = fssrc->slice_x;
1063  fsdst->slice_y = fssrc->slice_y;
1064  fsdst->slice_width = fssrc->slice_width;
1065  fsdst->slice_height = fssrc->slice_height;
1066  }
1067 }
1068 
1070 {
1071  FFV1Context *fsrc = src->priv_data;
1072  FFV1Context *fdst = dst->priv_data;
1073  int i, ret;
1074 
1075  if (dst == src)
1076  return 0;
1077 
1078  {
1082  memcpy(initial_states, fdst->initial_states, sizeof(fdst->initial_states));
1083  memcpy(slice_context, fdst->slice_context , sizeof(fdst->slice_context));
1084 
1085  memcpy(fdst, fsrc, sizeof(*fdst));
1086  memcpy(fdst->initial_states, initial_states, sizeof(fdst->initial_states));
1087  memcpy(fdst->slice_context, slice_context , sizeof(fdst->slice_context));
1088  fdst->picture = picture;
1089  fdst->last_picture = last_picture;
1090  for (i = 0; i<fdst->num_h_slices * fdst->num_v_slices; i++) {
1091  FFV1Context *fssrc = fsrc->slice_context[i];
1092  FFV1Context *fsdst = fdst->slice_context[i];
1093  copy_fields(fsdst, fssrc, fsrc);
1094  }
1095  av_assert0(!fdst->plane[0].state);
1096  av_assert0(!fdst->sample_buffer);
1097  }
1098 
1099  av_assert1(fdst->max_slice_count == fsrc->max_slice_count);
1100 
1101 
1102  ff_thread_release_buffer(dst, &fdst->picture);
1103  if (fsrc->picture.f->data[0]) {
1104  if ((ret = ff_thread_ref_frame(&fdst->picture, &fsrc->picture)) < 0)
1105  return ret;
1106  }
1107 
1108  fdst->fsrc = fsrc;
1109 
1110  return 0;
1111 }
1112 
1114  .name = "ffv1",
1115  .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
1116  .type = AVMEDIA_TYPE_VIDEO,
1117  .id = AV_CODEC_ID_FFV1,
1118  .priv_data_size = sizeof(FFV1Context),
1119  .init = decode_init,
1120  .close = ffv1_close,
1121  .decode = decode_frame,
1123  .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1124  .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/ |
1126 };