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.

1160 lines
42KB

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