You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1129 lines
40KB

  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. * @file
  24. * FF Video Codec 1 (a lossless codec) decoder
  25. */
  26. #include "libavutil/avassert.h"
  27. #include "libavutil/crc.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/imgutils.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "libavutil/timer.h"
  32. #include "avcodec.h"
  33. #include "internal.h"
  34. #include "get_bits.h"
  35. #include "rangecoder.h"
  36. #include "golomb.h"
  37. #include "mathops.h"
  38. #include "ffv1.h"
  39. static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state,
  40. int is_signed)
  41. {
  42. if (get_rac(c, state + 0))
  43. return 0;
  44. else {
  45. int i, e, a;
  46. e = 0;
  47. while (get_rac(c, state + 1 + FFMIN(e, 9))) { // 1..10
  48. e++;
  49. if (e > 31)
  50. return AVERROR_INVALIDDATA;
  51. }
  52. a = 1;
  53. for (i = e - 1; i >= 0; i--)
  54. a += a + get_rac(c, state + 22 + FFMIN(i, 9)); // 22..31
  55. e = -(is_signed && get_rac(c, state + 11 + FFMIN(e, 10))); // 11..21
  56. return (a ^ e) - e;
  57. }
  58. }
  59. static av_noinline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed)
  60. {
  61. return get_symbol_inline(c, state, is_signed);
  62. }
  63. static inline int get_vlc_symbol(GetBitContext *gb, VlcState *const state,
  64. int bits)
  65. {
  66. int k, i, v, ret;
  67. i = state->count;
  68. k = 0;
  69. while (i < state->error_sum) { // FIXME: optimize
  70. k++;
  71. i += i;
  72. }
  73. v = get_sr_golomb(gb, k, 12, bits);
  74. ff_dlog(NULL, "v:%d bias:%d error:%d drift:%d count:%d k:%d",
  75. v, state->bias, state->error_sum, state->drift, state->count, k);
  76. #if 0 // JPEG LS
  77. if (k == 0 && 2 * state->drift <= -state->count)
  78. v ^= (-1);
  79. #else
  80. v ^= ((2 * state->drift + state->count) >> 31);
  81. #endif
  82. ret = fold(v + state->bias, bits);
  83. update_vlc_state(state, v);
  84. return ret;
  85. }
  86. static av_always_inline void decode_line(FFV1Context *s, int w,
  87. int16_t *sample[2],
  88. int plane_index, int bits)
  89. {
  90. PlaneContext *const p = &s->plane[plane_index];
  91. RangeCoder *const c = &s->c;
  92. int x;
  93. int run_count = 0;
  94. int run_mode = 0;
  95. int run_index = s->run_index;
  96. if (s->slice_coding_mode == 1) {
  97. int i;
  98. for (x = 0; x < w; x++) {
  99. int v = 0;
  100. for (i=0; i<bits; i++) {
  101. uint8_t state = 128;
  102. v += v + get_rac(c, &state);
  103. }
  104. sample[1][x] = v;
  105. }
  106. return;
  107. }
  108. for (x = 0; x < w; x++) {
  109. int diff, context, sign;
  110. context = get_context(p, sample[1] + x, sample[0] + x, sample[1] + x);
  111. if (context < 0) {
  112. context = -context;
  113. sign = 1;
  114. } else
  115. sign = 0;
  116. av_assert2(context < p->context_count);
  117. if (s->ac) {
  118. diff = get_symbol_inline(c, p->state[context], 1);
  119. } else {
  120. if (context == 0 && run_mode == 0)
  121. run_mode = 1;
  122. if (run_mode) {
  123. if (run_count == 0 && run_mode == 1) {
  124. if (get_bits1(&s->gb)) {
  125. run_count = 1 << ff_log2_run[run_index];
  126. if (x + run_count <= w)
  127. run_index++;
  128. } else {
  129. if (ff_log2_run[run_index])
  130. run_count = get_bits(&s->gb, ff_log2_run[run_index]);
  131. else
  132. run_count = 0;
  133. if (run_index)
  134. run_index--;
  135. run_mode = 2;
  136. }
  137. }
  138. run_count--;
  139. if (run_count < 0) {
  140. run_mode = 0;
  141. run_count = 0;
  142. diff = get_vlc_symbol(&s->gb, &p->vlc_state[context],
  143. bits);
  144. if (diff >= 0)
  145. diff++;
  146. } else
  147. diff = 0;
  148. } else
  149. diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  150. ff_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
  151. run_count, run_index, run_mode, x, get_bits_count(&s->gb));
  152. }
  153. if (sign)
  154. diff = -diff;
  155. sample[1][x] = av_mod_uintp2(predict(sample[1] + x, sample[0] + x) + diff, bits);
  156. }
  157. s->run_index = run_index;
  158. }
  159. static void decode_plane(FFV1Context *s, uint8_t *src,
  160. int w, int h, int stride, int plane_index)
  161. {
  162. int x, y;
  163. int16_t *sample[2];
  164. sample[0] = s->sample_buffer + 3;
  165. sample[1] = s->sample_buffer + w + 6 + 3;
  166. s->run_index = 0;
  167. memset(s->sample_buffer, 0, 2 * (w + 6) * sizeof(*s->sample_buffer));
  168. for (y = 0; y < h; y++) {
  169. int16_t *temp = sample[0]; // FIXME: try a normal buffer
  170. sample[0] = sample[1];
  171. sample[1] = temp;
  172. sample[1][-1] = sample[0][0];
  173. sample[0][w] = sample[0][w - 1];
  174. // { START_TIMER
  175. if (s->avctx->bits_per_raw_sample <= 8) {
  176. decode_line(s, w, sample, plane_index, 8);
  177. for (x = 0; x < w; x++)
  178. src[x + stride * y] = sample[1][x];
  179. } else {
  180. decode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
  181. if (s->packed_at_lsb) {
  182. for (x = 0; x < w; x++) {
  183. ((uint16_t*)(src + stride*y))[x] = sample[1][x];
  184. }
  185. } else {
  186. for (x = 0; x < w; x++) {
  187. ((uint16_t*)(src + stride*y))[x] = sample[1][x] << (16 - s->avctx->bits_per_raw_sample);
  188. }
  189. }
  190. }
  191. // STOP_TIMER("decode-line") }
  192. }
  193. }
  194. static void decode_rgb_frame(FFV1Context *s, uint8_t *src[3], int w, int h, int stride[3])
  195. {
  196. int x, y, p;
  197. int16_t *sample[4][2];
  198. int lbd = s->avctx->bits_per_raw_sample <= 8;
  199. int bits = s->avctx->bits_per_raw_sample > 0 ? s->avctx->bits_per_raw_sample : 8;
  200. int offset = 1 << bits;
  201. for (x = 0; x < 4; x++) {
  202. sample[x][0] = s->sample_buffer + x * 2 * (w + 6) + 3;
  203. sample[x][1] = s->sample_buffer + (x * 2 + 1) * (w + 6) + 3;
  204. }
  205. s->run_index = 0;
  206. memset(s->sample_buffer, 0, 8 * (w + 6) * sizeof(*s->sample_buffer));
  207. for (y = 0; y < h; y++) {
  208. for (p = 0; p < 3 + s->transparency; p++) {
  209. int16_t *temp = sample[p][0]; // FIXME: try a normal buffer
  210. sample[p][0] = sample[p][1];
  211. sample[p][1] = temp;
  212. sample[p][1][-1]= sample[p][0][0 ];
  213. sample[p][0][ w]= sample[p][0][w-1];
  214. if (lbd && s->slice_coding_mode == 0)
  215. decode_line(s, w, sample[p], (p + 1)/2, 9);
  216. else
  217. decode_line(s, w, sample[p], (p + 1)/2, bits + (s->slice_coding_mode != 1));
  218. }
  219. for (x = 0; x < w; x++) {
  220. int g = sample[0][1][x];
  221. int b = sample[1][1][x];
  222. int r = sample[2][1][x];
  223. int a = sample[3][1][x];
  224. if (s->slice_coding_mode != 1) {
  225. b -= offset;
  226. r -= offset;
  227. g -= (b * s->slice_rct_by_coef + r * s->slice_rct_ry_coef) >> 2;
  228. b += g;
  229. r += g;
  230. }
  231. if (lbd)
  232. *((uint32_t*)(src[0] + x*4 + stride[0]*y)) = b + (g<<8) + (r<<16) + (a<<24);
  233. else {
  234. *((uint16_t*)(src[0] + x*2 + stride[0]*y)) = b;
  235. *((uint16_t*)(src[1] + x*2 + stride[1]*y)) = g;
  236. *((uint16_t*)(src[2] + x*2 + stride[2]*y)) = r;
  237. }
  238. }
  239. }
  240. }
  241. static int decode_slice_header(FFV1Context *f, FFV1Context *fs)
  242. {
  243. RangeCoder *c = &fs->c;
  244. uint8_t state[CONTEXT_SIZE];
  245. unsigned ps, i, context_count;
  246. memset(state, 128, sizeof(state));
  247. av_assert0(f->version > 2);
  248. fs->slice_x = get_symbol(c, state, 0) * f->width ;
  249. fs->slice_y = get_symbol(c, state, 0) * f->height;
  250. fs->slice_width = (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
  251. fs->slice_height = (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
  252. fs->slice_x /= f->num_h_slices;
  253. fs->slice_y /= f->num_v_slices;
  254. fs->slice_width = fs->slice_width /f->num_h_slices - fs->slice_x;
  255. fs->slice_height = fs->slice_height/f->num_v_slices - fs->slice_y;
  256. if ((unsigned)fs->slice_width > f->width || (unsigned)fs->slice_height > f->height)
  257. return -1;
  258. if ( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
  259. || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
  260. return -1;
  261. for (i = 0; i < f->plane_count; i++) {
  262. PlaneContext * const p = &fs->plane[i];
  263. int idx = get_symbol(c, state, 0);
  264. if (idx > (unsigned)f->quant_table_count) {
  265. av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
  266. return -1;
  267. }
  268. p->quant_table_index = idx;
  269. memcpy(p->quant_table, f->quant_tables[idx], sizeof(p->quant_table));
  270. context_count = f->context_count[idx];
  271. if (p->context_count < context_count) {
  272. av_freep(&p->state);
  273. av_freep(&p->vlc_state);
  274. }
  275. p->context_count = context_count;
  276. }
  277. ps = get_symbol(c, state, 0);
  278. if (ps == 1) {
  279. f->cur->interlaced_frame = 1;
  280. f->cur->top_field_first = 1;
  281. } else if (ps == 2) {
  282. f->cur->interlaced_frame = 1;
  283. f->cur->top_field_first = 0;
  284. } else if (ps == 3) {
  285. f->cur->interlaced_frame = 0;
  286. }
  287. f->cur->sample_aspect_ratio.num = get_symbol(c, state, 0);
  288. f->cur->sample_aspect_ratio.den = get_symbol(c, state, 0);
  289. if (av_image_check_sar(f->width, f->height,
  290. f->cur->sample_aspect_ratio) < 0) {
  291. av_log(f->avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  292. f->cur->sample_aspect_ratio.num,
  293. f->cur->sample_aspect_ratio.den);
  294. f->cur->sample_aspect_ratio = (AVRational){ 0, 1 };
  295. }
  296. if (fs->version > 3) {
  297. fs->slice_reset_contexts = get_rac(c, state);
  298. fs->slice_coding_mode = get_symbol(c, state, 0);
  299. if (fs->slice_coding_mode != 1) {
  300. fs->slice_rct_by_coef = get_symbol(c, state, 0);
  301. fs->slice_rct_ry_coef = get_symbol(c, state, 0);
  302. if ((uint64_t)fs->slice_rct_by_coef + (uint64_t)fs->slice_rct_ry_coef > 4) {
  303. av_log(f->avctx, AV_LOG_ERROR, "slice_rct_y_coef out of range\n");
  304. return AVERROR_INVALIDDATA;
  305. }
  306. }
  307. }
  308. return 0;
  309. }
  310. static int decode_slice(AVCodecContext *c, void *arg)
  311. {
  312. FFV1Context *fs = *(void **)arg;
  313. FFV1Context *f = fs->avctx->priv_data;
  314. int width, height, x, y, ret;
  315. const int ps = av_pix_fmt_desc_get(c->pix_fmt)->comp[0].step;
  316. AVFrame * const p = f->cur;
  317. int i, si;
  318. for( si=0; fs != f->slice_context[si]; si ++)
  319. ;
  320. if(f->fsrc && !p->key_frame)
  321. ff_thread_await_progress(&f->last_picture, si, 0);
  322. if(f->fsrc && !p->key_frame) {
  323. FFV1Context *fssrc = f->fsrc->slice_context[si];
  324. FFV1Context *fsdst = f->slice_context[si];
  325. av_assert1(fsdst->plane_count == fssrc->plane_count);
  326. av_assert1(fsdst == fs);
  327. if (!p->key_frame)
  328. fsdst->slice_damaged |= fssrc->slice_damaged;
  329. for (i = 0; i < f->plane_count; i++) {
  330. PlaneContext *psrc = &fssrc->plane[i];
  331. PlaneContext *pdst = &fsdst->plane[i];
  332. av_free(pdst->state);
  333. av_free(pdst->vlc_state);
  334. memcpy(pdst, psrc, sizeof(*pdst));
  335. pdst->state = NULL;
  336. pdst->vlc_state = NULL;
  337. if (fssrc->ac) {
  338. pdst->state = av_malloc_array(CONTEXT_SIZE, psrc->context_count);
  339. memcpy(pdst->state, psrc->state, CONTEXT_SIZE * psrc->context_count);
  340. } else {
  341. pdst->vlc_state = av_malloc_array(sizeof(*pdst->vlc_state), psrc->context_count);
  342. memcpy(pdst->vlc_state, psrc->vlc_state, sizeof(*pdst->vlc_state) * psrc->context_count);
  343. }
  344. }
  345. }
  346. fs->slice_rct_by_coef = 1;
  347. fs->slice_rct_ry_coef = 1;
  348. if (f->version > 2) {
  349. if (ff_ffv1_init_slice_state(f, fs) < 0)
  350. return AVERROR(ENOMEM);
  351. if (decode_slice_header(f, fs) < 0) {
  352. fs->slice_damaged = 1;
  353. return AVERROR_INVALIDDATA;
  354. }
  355. }
  356. if ((ret = ff_ffv1_init_slice_state(f, fs)) < 0)
  357. return ret;
  358. if (f->cur->key_frame || fs->slice_reset_contexts)
  359. ff_ffv1_clear_slice_state(f, fs);
  360. width = fs->slice_width;
  361. height = fs->slice_height;
  362. x = fs->slice_x;
  363. y = fs->slice_y;
  364. if (!fs->ac) {
  365. if (f->version == 3 && f->micro_version > 1 || f->version > 3)
  366. get_rac(&fs->c, (uint8_t[]) { 129 });
  367. fs->ac_byte_count = f->version > 2 || (!x && !y) ? fs->c.bytestream - fs->c.bytestream_start - 1 : 0;
  368. init_get_bits(&fs->gb,
  369. fs->c.bytestream_start + fs->ac_byte_count,
  370. (fs->c.bytestream_end - fs->c.bytestream_start - fs->ac_byte_count) * 8);
  371. }
  372. av_assert1(width && height);
  373. if (f->colorspace == 0) {
  374. const int chroma_width = FF_CEIL_RSHIFT(width, f->chroma_h_shift);
  375. const int chroma_height = FF_CEIL_RSHIFT(height, f->chroma_v_shift);
  376. const int cx = x >> f->chroma_h_shift;
  377. const int cy = y >> f->chroma_v_shift;
  378. decode_plane(fs, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0);
  379. if (f->chroma_planes) {
  380. decode_plane(fs, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
  381. decode_plane(fs, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1);
  382. }
  383. if (fs->transparency)
  384. decode_plane(fs, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], (f->version >= 4 && !f->chroma_planes) ? 1 : 2);
  385. } else {
  386. uint8_t *planes[3] = { p->data[0] + ps * x + y * p->linesize[0],
  387. p->data[1] + ps * x + y * p->linesize[1],
  388. p->data[2] + ps * x + y * p->linesize[2] };
  389. decode_rgb_frame(fs, planes, width, height, p->linesize);
  390. }
  391. if (fs->ac && f->version > 2) {
  392. int v;
  393. get_rac(&fs->c, (uint8_t[]) { 129 });
  394. v = fs->c.bytestream_end - fs->c.bytestream - 2 - 5*f->ec;
  395. if (v) {
  396. av_log(f->avctx, AV_LOG_ERROR, "bytestream end mismatching by %d\n", v);
  397. fs->slice_damaged = 1;
  398. }
  399. }
  400. emms_c();
  401. ff_thread_report_progress(&f->picture, si, 0);
  402. return 0;
  403. }
  404. static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale)
  405. {
  406. int v;
  407. int i = 0;
  408. uint8_t state[CONTEXT_SIZE];
  409. memset(state, 128, sizeof(state));
  410. for (v = 0; i < 128; v++) {
  411. unsigned len = get_symbol(c, state, 0) + 1;
  412. if (len > 128 - i || !len)
  413. return AVERROR_INVALIDDATA;
  414. while (len--) {
  415. quant_table[i] = scale * v;
  416. i++;
  417. }
  418. }
  419. for (i = 1; i < 128; i++)
  420. quant_table[256 - i] = -quant_table[i];
  421. quant_table[128] = -quant_table[127];
  422. return 2 * v - 1;
  423. }
  424. static int read_quant_tables(RangeCoder *c,
  425. int16_t quant_table[MAX_CONTEXT_INPUTS][256])
  426. {
  427. int i;
  428. int context_count = 1;
  429. for (i = 0; i < 5; i++) {
  430. context_count *= read_quant_table(c, quant_table[i], context_count);
  431. if (context_count > 32768U) {
  432. return AVERROR_INVALIDDATA;
  433. }
  434. }
  435. return (context_count + 1) / 2;
  436. }
  437. static int read_extra_header(FFV1Context *f)
  438. {
  439. RangeCoder *const c = &f->c;
  440. uint8_t state[CONTEXT_SIZE];
  441. int i, j, k, ret;
  442. uint8_t state2[32][CONTEXT_SIZE];
  443. unsigned crc = 0;
  444. memset(state2, 128, sizeof(state2));
  445. memset(state, 128, sizeof(state));
  446. ff_init_range_decoder(c, f->avctx->extradata, f->avctx->extradata_size);
  447. ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
  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->micro_version = get_symbol(c, state, 0);
  456. if (f->micro_version < 0)
  457. return AVERROR_INVALIDDATA;
  458. }
  459. f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
  460. if (f->ac > 1) {
  461. for (i = 1; i < 256; i++)
  462. f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
  463. }
  464. f->colorspace = get_symbol(c, state, 0); //YUV cs type
  465. f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
  466. f->chroma_planes = get_rac(c, state);
  467. f->chroma_h_shift = get_symbol(c, state, 0);
  468. f->chroma_v_shift = get_symbol(c, state, 0);
  469. f->transparency = get_rac(c, state);
  470. f->plane_count = 1 + (f->chroma_planes || f->version<4) + f->transparency;
  471. f->num_h_slices = 1 + get_symbol(c, state, 0);
  472. f->num_v_slices = 1 + get_symbol(c, state, 0);
  473. if (f->chroma_h_shift > 4U || f->chroma_v_shift > 4U) {
  474. av_log(f->avctx, AV_LOG_ERROR, "chroma shift parameters %d %d are invalid\n",
  475. f->chroma_h_shift, f->chroma_v_shift);
  476. return AVERROR_INVALIDDATA;
  477. }
  478. if (f->num_h_slices > (unsigned)f->width || !f->num_h_slices ||
  479. f->num_v_slices > (unsigned)f->height || !f->num_v_slices
  480. ) {
  481. av_log(f->avctx, AV_LOG_ERROR, "slice count invalid\n");
  482. return AVERROR_INVALIDDATA;
  483. }
  484. f->quant_table_count = get_symbol(c, state, 0);
  485. if (f->quant_table_count > (unsigned)MAX_QUANT_TABLES)
  486. return AVERROR_INVALIDDATA;
  487. for (i = 0; i < f->quant_table_count; i++) {
  488. f->context_count[i] = read_quant_tables(c, f->quant_tables[i]);
  489. if (f->context_count[i] < 0) {
  490. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  491. return AVERROR_INVALIDDATA;
  492. }
  493. }
  494. if ((ret = ff_ffv1_allocate_initial_states(f)) < 0)
  495. return ret;
  496. for (i = 0; i < f->quant_table_count; i++)
  497. if (get_rac(c, state)) {
  498. for (j = 0; j < f->context_count[i]; j++)
  499. for (k = 0; k < CONTEXT_SIZE; k++) {
  500. int pred = j ? f->initial_states[i][j - 1][k] : 128;
  501. f->initial_states[i][j][k] =
  502. (pred + get_symbol(c, state2[k], 1)) & 0xFF;
  503. }
  504. }
  505. if (f->version > 2) {
  506. f->ec = get_symbol(c, state, 0);
  507. if (f->micro_version > 2)
  508. f->intra = get_symbol(c, state, 0);
  509. }
  510. if (f->version > 2) {
  511. unsigned v;
  512. v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0,
  513. f->avctx->extradata, f->avctx->extradata_size);
  514. if (v || f->avctx->extradata_size < 4) {
  515. av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", v);
  516. return AVERROR_INVALIDDATA;
  517. }
  518. crc = AV_RB32(f->avctx->extradata + f->avctx->extradata_size - 4);
  519. }
  520. if (f->avctx->debug & FF_DEBUG_PICT_INFO)
  521. av_log(f->avctx, AV_LOG_DEBUG,
  522. "global: ver:%d.%d, coder:%d, colorspace: %d bpr:%d chroma:%d(%d:%d), alpha:%d slices:%dx%d qtabs:%d ec:%d intra:%d CRC:0x%08X\n",
  523. f->version, f->micro_version,
  524. f->ac,
  525. f->colorspace,
  526. f->avctx->bits_per_raw_sample,
  527. f->chroma_planes, f->chroma_h_shift, f->chroma_v_shift,
  528. f->transparency,
  529. f->num_h_slices, f->num_v_slices,
  530. f->quant_table_count,
  531. f->ec,
  532. f->intra,
  533. crc
  534. );
  535. return 0;
  536. }
  537. static int read_header(FFV1Context *f)
  538. {
  539. uint8_t state[CONTEXT_SIZE];
  540. int i, j, context_count = -1; //-1 to avoid warning
  541. RangeCoder *const c = &f->slice_context[0]->c;
  542. memset(state, 128, sizeof(state));
  543. if (f->version < 2) {
  544. int chroma_planes, chroma_h_shift, chroma_v_shift, transparency, colorspace, bits_per_raw_sample;
  545. unsigned v= get_symbol(c, state, 0);
  546. if (v >= 2) {
  547. av_log(f->avctx, AV_LOG_ERROR, "invalid version %d in ver01 header\n", v);
  548. return AVERROR_INVALIDDATA;
  549. }
  550. f->version = v;
  551. f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
  552. if (f->ac > 1) {
  553. for (i = 1; i < 256; i++)
  554. f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
  555. }
  556. colorspace = get_symbol(c, state, 0); //YUV cs type
  557. bits_per_raw_sample = f->version > 0 ? get_symbol(c, state, 0) : f->avctx->bits_per_raw_sample;
  558. chroma_planes = get_rac(c, state);
  559. chroma_h_shift = get_symbol(c, state, 0);
  560. chroma_v_shift = get_symbol(c, state, 0);
  561. transparency = get_rac(c, state);
  562. if (colorspace == 0 && f->avctx->skip_alpha)
  563. transparency = 0;
  564. if (f->plane_count) {
  565. if (colorspace != f->colorspace ||
  566. bits_per_raw_sample != f->avctx->bits_per_raw_sample ||
  567. chroma_planes != f->chroma_planes ||
  568. chroma_h_shift != f->chroma_h_shift ||
  569. chroma_v_shift != f->chroma_v_shift ||
  570. transparency != f->transparency) {
  571. av_log(f->avctx, AV_LOG_ERROR, "Invalid change of global parameters\n");
  572. return AVERROR_INVALIDDATA;
  573. }
  574. }
  575. if (chroma_h_shift > 4U || chroma_v_shift > 4U) {
  576. av_log(f->avctx, AV_LOG_ERROR, "chroma shift parameters %d %d are invalid\n",
  577. chroma_h_shift, chroma_v_shift);
  578. return AVERROR_INVALIDDATA;
  579. }
  580. f->colorspace = colorspace;
  581. f->avctx->bits_per_raw_sample = bits_per_raw_sample;
  582. f->chroma_planes = chroma_planes;
  583. f->chroma_h_shift = chroma_h_shift;
  584. f->chroma_v_shift = chroma_v_shift;
  585. f->transparency = transparency;
  586. f->plane_count = 2 + f->transparency;
  587. }
  588. if (f->colorspace == 0) {
  589. if (!f->transparency && !f->chroma_planes) {
  590. if (f->avctx->bits_per_raw_sample <= 8)
  591. f->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  592. else
  593. f->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
  594. } else if (f->avctx->bits_per_raw_sample<=8 && !f->transparency) {
  595. switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
  596. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P; break;
  597. case 0x01: f->avctx->pix_fmt = AV_PIX_FMT_YUV440P; break;
  598. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P; break;
  599. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P; break;
  600. case 0x20: f->avctx->pix_fmt = AV_PIX_FMT_YUV411P; break;
  601. case 0x22: f->avctx->pix_fmt = AV_PIX_FMT_YUV410P; break;
  602. }
  603. } else if (f->avctx->bits_per_raw_sample <= 8 && f->transparency) {
  604. switch(16*f->chroma_h_shift + f->chroma_v_shift) {
  605. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P; break;
  606. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P; break;
  607. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P; break;
  608. }
  609. } else if (f->avctx->bits_per_raw_sample == 9 && !f->transparency) {
  610. f->packed_at_lsb = 1;
  611. switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
  612. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P9; break;
  613. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P9; break;
  614. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P9; break;
  615. }
  616. } else if (f->avctx->bits_per_raw_sample == 9 && f->transparency) {
  617. f->packed_at_lsb = 1;
  618. switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
  619. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P9; break;
  620. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P9; break;
  621. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P9; break;
  622. }
  623. } else if (f->avctx->bits_per_raw_sample == 10 && !f->transparency) {
  624. f->packed_at_lsb = 1;
  625. switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
  626. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P10; break;
  627. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P10; break;
  628. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P10; break;
  629. }
  630. } else if (f->avctx->bits_per_raw_sample == 10 && f->transparency) {
  631. f->packed_at_lsb = 1;
  632. switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
  633. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P10; break;
  634. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P10; break;
  635. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P10; break;
  636. }
  637. } else if (f->avctx->bits_per_raw_sample == 16 && !f->transparency){
  638. switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
  639. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P16; break;
  640. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P16; break;
  641. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P16; break;
  642. }
  643. } else if (f->avctx->bits_per_raw_sample == 16 && f->transparency){
  644. switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
  645. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P16; break;
  646. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P16; break;
  647. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P16; break;
  648. }
  649. }
  650. } else if (f->colorspace == 1) {
  651. if (f->chroma_h_shift || f->chroma_v_shift) {
  652. av_log(f->avctx, AV_LOG_ERROR,
  653. "chroma subsampling not supported in this colorspace\n");
  654. return AVERROR(ENOSYS);
  655. }
  656. if ( f->avctx->bits_per_raw_sample == 9)
  657. f->avctx->pix_fmt = AV_PIX_FMT_GBRP9;
  658. else if (f->avctx->bits_per_raw_sample == 10)
  659. f->avctx->pix_fmt = AV_PIX_FMT_GBRP10;
  660. else if (f->avctx->bits_per_raw_sample == 12)
  661. f->avctx->pix_fmt = AV_PIX_FMT_GBRP12;
  662. else if (f->avctx->bits_per_raw_sample == 14)
  663. f->avctx->pix_fmt = AV_PIX_FMT_GBRP14;
  664. else
  665. if (f->transparency) f->avctx->pix_fmt = AV_PIX_FMT_RGB32;
  666. else f->avctx->pix_fmt = AV_PIX_FMT_0RGB32;
  667. } else {
  668. av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
  669. return AVERROR(ENOSYS);
  670. }
  671. if (f->avctx->pix_fmt == AV_PIX_FMT_NONE) {
  672. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  673. return AVERROR(ENOSYS);
  674. }
  675. ff_dlog(f->avctx, "%d %d %d\n",
  676. f->chroma_h_shift, f->chroma_v_shift, f->avctx->pix_fmt);
  677. if (f->version < 2) {
  678. context_count = read_quant_tables(c, f->quant_table);
  679. if (context_count < 0) {
  680. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  681. return AVERROR_INVALIDDATA;
  682. }
  683. f->slice_count = f->max_slice_count;
  684. } else if (f->version < 3) {
  685. f->slice_count = get_symbol(c, state, 0);
  686. } else {
  687. const uint8_t *p = c->bytestream_end;
  688. for (f->slice_count = 0;
  689. f->slice_count < MAX_SLICES && 3 < p - c->bytestream_start;
  690. f->slice_count++) {
  691. int trailer = 3 + 5*!!f->ec;
  692. int size = AV_RB24(p-trailer);
  693. if (size + trailer > p - c->bytestream_start)
  694. break;
  695. p -= size + trailer;
  696. }
  697. }
  698. if (f->slice_count > (unsigned)MAX_SLICES || f->slice_count <= 0 || f->slice_count > f->max_slice_count) {
  699. av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid (max=%d)\n", f->slice_count, f->max_slice_count);
  700. return AVERROR_INVALIDDATA;
  701. }
  702. for (j = 0; j < f->slice_count; j++) {
  703. FFV1Context *fs = f->slice_context[j];
  704. fs->ac = f->ac;
  705. fs->packed_at_lsb = f->packed_at_lsb;
  706. fs->slice_damaged = 0;
  707. if (f->version == 2) {
  708. fs->slice_x = get_symbol(c, state, 0) * f->width ;
  709. fs->slice_y = get_symbol(c, state, 0) * f->height;
  710. fs->slice_width = (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
  711. fs->slice_height = (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
  712. fs->slice_x /= f->num_h_slices;
  713. fs->slice_y /= f->num_v_slices;
  714. fs->slice_width = fs->slice_width / f->num_h_slices - fs->slice_x;
  715. fs->slice_height = fs->slice_height / f->num_v_slices - fs->slice_y;
  716. if ((unsigned)fs->slice_width > f->width ||
  717. (unsigned)fs->slice_height > f->height)
  718. return AVERROR_INVALIDDATA;
  719. if ( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
  720. || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
  721. return AVERROR_INVALIDDATA;
  722. }
  723. for (i = 0; i < f->plane_count; i++) {
  724. PlaneContext *const p = &fs->plane[i];
  725. if (f->version == 2) {
  726. int idx = get_symbol(c, state, 0);
  727. if (idx > (unsigned)f->quant_table_count) {
  728. av_log(f->avctx, AV_LOG_ERROR,
  729. "quant_table_index out of range\n");
  730. return AVERROR_INVALIDDATA;
  731. }
  732. p->quant_table_index = idx;
  733. memcpy(p->quant_table, f->quant_tables[idx],
  734. sizeof(p->quant_table));
  735. context_count = f->context_count[idx];
  736. } else {
  737. memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
  738. }
  739. if (f->version <= 2) {
  740. av_assert0(context_count >= 0);
  741. if (p->context_count < context_count) {
  742. av_freep(&p->state);
  743. av_freep(&p->vlc_state);
  744. }
  745. p->context_count = context_count;
  746. }
  747. }
  748. }
  749. return 0;
  750. }
  751. static av_cold int decode_init(AVCodecContext *avctx)
  752. {
  753. FFV1Context *f = avctx->priv_data;
  754. int ret;
  755. if ((ret = ff_ffv1_common_init(avctx)) < 0)
  756. return ret;
  757. if (avctx->extradata && (ret = read_extra_header(f)) < 0)
  758. return ret;
  759. if ((ret = ff_ffv1_init_slice_contexts(f)) < 0)
  760. return ret;
  761. avctx->internal->allocate_progress = 1;
  762. return 0;
  763. }
  764. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
  765. {
  766. uint8_t *buf = avpkt->data;
  767. int buf_size = avpkt->size;
  768. FFV1Context *f = avctx->priv_data;
  769. RangeCoder *const c = &f->slice_context[0]->c;
  770. int i, ret;
  771. uint8_t keystate = 128;
  772. uint8_t *buf_p;
  773. AVFrame *p;
  774. if (f->last_picture.f)
  775. ff_thread_release_buffer(avctx, &f->last_picture);
  776. FFSWAP(ThreadFrame, f->picture, f->last_picture);
  777. f->cur = p = f->picture.f;
  778. if (f->version < 3 && avctx->field_order > AV_FIELD_PROGRESSIVE) {
  779. /* we have interlaced material flagged in container */
  780. p->interlaced_frame = 1;
  781. if (avctx->field_order == AV_FIELD_TT || avctx->field_order == AV_FIELD_TB)
  782. p->top_field_first = 1;
  783. }
  784. f->avctx = avctx;
  785. ff_init_range_decoder(c, buf, buf_size);
  786. ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
  787. p->pict_type = AV_PICTURE_TYPE_I; //FIXME I vs. P
  788. if (get_rac(c, &keystate)) {
  789. p->key_frame = 1;
  790. f->key_frame_ok = 0;
  791. if ((ret = read_header(f)) < 0)
  792. return ret;
  793. f->key_frame_ok = 1;
  794. } else {
  795. if (!f->key_frame_ok) {
  796. av_log(avctx, AV_LOG_ERROR,
  797. "Cannot decode non-keyframe without valid keyframe\n");
  798. return AVERROR_INVALIDDATA;
  799. }
  800. p->key_frame = 0;
  801. }
  802. if ((ret = ff_thread_get_buffer(avctx, &f->picture, AV_GET_BUFFER_FLAG_REF)) < 0)
  803. return ret;
  804. if (avctx->debug & FF_DEBUG_PICT_INFO)
  805. av_log(avctx, AV_LOG_DEBUG, "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
  806. f->version, p->key_frame, f->ac, f->ec, f->slice_count, f->avctx->bits_per_raw_sample);
  807. ff_thread_finish_setup(avctx);
  808. buf_p = buf + buf_size;
  809. for (i = f->slice_count - 1; i >= 0; i--) {
  810. FFV1Context *fs = f->slice_context[i];
  811. int trailer = 3 + 5*!!f->ec;
  812. int v;
  813. if (i || f->version > 2) v = AV_RB24(buf_p-trailer) + trailer;
  814. else v = buf_p - c->bytestream_start;
  815. if (buf_p - c->bytestream_start < v) {
  816. av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
  817. return AVERROR_INVALIDDATA;
  818. }
  819. buf_p -= v;
  820. if (f->ec) {
  821. unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, v);
  822. if (crc) {
  823. int64_t ts = avpkt->pts != AV_NOPTS_VALUE ? avpkt->pts : avpkt->dts;
  824. av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!", crc);
  825. if (ts != AV_NOPTS_VALUE && avctx->pkt_timebase.num) {
  826. av_log(f->avctx, AV_LOG_ERROR, "at %f seconds\n", ts*av_q2d(avctx->pkt_timebase));
  827. } else if (ts != AV_NOPTS_VALUE) {
  828. av_log(f->avctx, AV_LOG_ERROR, "at %"PRId64"\n", ts);
  829. } else {
  830. av_log(f->avctx, AV_LOG_ERROR, "\n");
  831. }
  832. fs->slice_damaged = 1;
  833. }
  834. if (avctx->debug & FF_DEBUG_PICT_INFO) {
  835. av_log(avctx, AV_LOG_DEBUG, "slice %d, CRC: 0x%08X\n", i, AV_RB32(buf_p + v - 4));
  836. }
  837. }
  838. if (i) {
  839. ff_init_range_decoder(&fs->c, buf_p, v);
  840. } else
  841. fs->c.bytestream_end = buf_p + v;
  842. fs->avctx = avctx;
  843. fs->cur = p;
  844. }
  845. avctx->execute(avctx,
  846. decode_slice,
  847. &f->slice_context[0],
  848. NULL,
  849. f->slice_count,
  850. sizeof(void*));
  851. for (i = f->slice_count - 1; i >= 0; i--) {
  852. FFV1Context *fs = f->slice_context[i];
  853. int j;
  854. if (fs->slice_damaged && f->last_picture.f->data[0]) {
  855. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  856. const uint8_t *src[4];
  857. uint8_t *dst[4];
  858. ff_thread_await_progress(&f->last_picture, INT_MAX, 0);
  859. for (j = 0; j < 4; j++) {
  860. int pixshift = desc->comp[j].depth > 8;
  861. int sh = (j == 1 || j == 2) ? f->chroma_h_shift : 0;
  862. int sv = (j == 1 || j == 2) ? f->chroma_v_shift : 0;
  863. dst[j] = p->data[j] + p->linesize[j] *
  864. (fs->slice_y >> sv) + ((fs->slice_x >> sh) << pixshift);
  865. src[j] = f->last_picture.f->data[j] + f->last_picture.f->linesize[j] *
  866. (fs->slice_y >> sv) + ((fs->slice_x >> sh) << pixshift);
  867. }
  868. av_image_copy(dst, p->linesize, src,
  869. f->last_picture.f->linesize,
  870. avctx->pix_fmt,
  871. fs->slice_width,
  872. fs->slice_height);
  873. }
  874. }
  875. ff_thread_report_progress(&f->picture, INT_MAX, 0);
  876. f->picture_number++;
  877. if (f->last_picture.f)
  878. ff_thread_release_buffer(avctx, &f->last_picture);
  879. f->cur = NULL;
  880. if ((ret = av_frame_ref(data, f->picture.f)) < 0)
  881. return ret;
  882. *got_frame = 1;
  883. return buf_size;
  884. }
  885. static int init_thread_copy(AVCodecContext *avctx)
  886. {
  887. FFV1Context *f = avctx->priv_data;
  888. int i, ret;
  889. f->picture.f = NULL;
  890. f->last_picture.f = NULL;
  891. f->sample_buffer = NULL;
  892. f->max_slice_count = 0;
  893. f->slice_count = 0;
  894. for (i = 0; i < f->quant_table_count; i++) {
  895. av_assert0(f->version > 1);
  896. f->initial_states[i] = av_memdup(f->initial_states[i],
  897. f->context_count[i] * sizeof(*f->initial_states[i]));
  898. }
  899. f->picture.f = av_frame_alloc();
  900. f->last_picture.f = av_frame_alloc();
  901. if ((ret = ff_ffv1_init_slice_contexts(f)) < 0)
  902. return ret;
  903. return 0;
  904. }
  905. static void copy_fields(FFV1Context *fsdst, FFV1Context *fssrc, FFV1Context *fsrc)
  906. {
  907. fsdst->version = fsrc->version;
  908. fsdst->micro_version = fsrc->micro_version;
  909. fsdst->chroma_planes = fsrc->chroma_planes;
  910. fsdst->chroma_h_shift = fsrc->chroma_h_shift;
  911. fsdst->chroma_v_shift = fsrc->chroma_v_shift;
  912. fsdst->transparency = fsrc->transparency;
  913. fsdst->plane_count = fsrc->plane_count;
  914. fsdst->ac = fsrc->ac;
  915. fsdst->colorspace = fsrc->colorspace;
  916. fsdst->ec = fsrc->ec;
  917. fsdst->intra = fsrc->intra;
  918. fsdst->slice_damaged = fssrc->slice_damaged;
  919. fsdst->key_frame_ok = fsrc->key_frame_ok;
  920. fsdst->bits_per_raw_sample = fsrc->bits_per_raw_sample;
  921. fsdst->packed_at_lsb = fsrc->packed_at_lsb;
  922. fsdst->slice_count = fsrc->slice_count;
  923. if (fsrc->version<3){
  924. fsdst->slice_x = fssrc->slice_x;
  925. fsdst->slice_y = fssrc->slice_y;
  926. fsdst->slice_width = fssrc->slice_width;
  927. fsdst->slice_height = fssrc->slice_height;
  928. }
  929. }
  930. static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
  931. {
  932. FFV1Context *fsrc = src->priv_data;
  933. FFV1Context *fdst = dst->priv_data;
  934. int i, ret;
  935. if (dst == src)
  936. return 0;
  937. {
  938. ThreadFrame picture = fdst->picture, last_picture = fdst->last_picture;
  939. uint8_t (*initial_states[MAX_QUANT_TABLES])[32];
  940. struct FFV1Context *slice_context[MAX_SLICES];
  941. memcpy(initial_states, fdst->initial_states, sizeof(fdst->initial_states));
  942. memcpy(slice_context, fdst->slice_context , sizeof(fdst->slice_context));
  943. memcpy(fdst, fsrc, sizeof(*fdst));
  944. memcpy(fdst->initial_states, initial_states, sizeof(fdst->initial_states));
  945. memcpy(fdst->slice_context, slice_context , sizeof(fdst->slice_context));
  946. fdst->picture = picture;
  947. fdst->last_picture = last_picture;
  948. for (i = 0; i<fdst->num_h_slices * fdst->num_v_slices; i++) {
  949. FFV1Context *fssrc = fsrc->slice_context[i];
  950. FFV1Context *fsdst = fdst->slice_context[i];
  951. copy_fields(fsdst, fssrc, fsrc);
  952. }
  953. av_assert0(!fdst->plane[0].state);
  954. av_assert0(!fdst->sample_buffer);
  955. }
  956. av_assert1(fdst->max_slice_count == fsrc->max_slice_count);
  957. ff_thread_release_buffer(dst, &fdst->picture);
  958. if (fsrc->picture.f->data[0]) {
  959. if ((ret = ff_thread_ref_frame(&fdst->picture, &fsrc->picture)) < 0)
  960. return ret;
  961. }
  962. fdst->fsrc = fsrc;
  963. return 0;
  964. }
  965. AVCodec ff_ffv1_decoder = {
  966. .name = "ffv1",
  967. .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
  968. .type = AVMEDIA_TYPE_VIDEO,
  969. .id = AV_CODEC_ID_FFV1,
  970. .priv_data_size = sizeof(FFV1Context),
  971. .init = decode_init,
  972. .close = ff_ffv1_close,
  973. .decode = decode_frame,
  974. .init_thread_copy = ONLY_IF_THREADS_ENABLED(init_thread_copy),
  975. .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
  976. .capabilities = AV_CODEC_CAP_DR1 /*| AV_CODEC_CAP_DRAW_HORIZ_BAND*/ |
  977. AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_SLICE_THREADS,
  978. };