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.

1153 lines
41KB

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