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.

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