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.

1101 lines
39KB

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