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.

1087 lines
38KB

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