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.

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