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.

917 lines
30KB

  1. /*
  2. * FFV1 decoder
  3. *
  4. * Copyright (c) 2003-2012 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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/pixdesc.h"
  28. #include "libavutil/crc.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/imgutils.h"
  31. #include "avcodec.h"
  32. #include "get_bits.h"
  33. #include "put_bits.h"
  34. #include "dsputil.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. assert(k <= 8);
  71. v = get_sr_golomb(gb, k, 12, bits);
  72. av_dlog(NULL, "v:%d bias:%d error:%d drift:%d count:%d k:%d",
  73. v, state->bias, state->error_sum, state->drift, state->count, k);
  74. #if 0 // JPEG LS
  75. if (k == 0 && 2 * state->drift <= -state->count)
  76. v ^= (-1);
  77. #else
  78. v ^= ((2 * state->drift + state->count) >> 31);
  79. #endif
  80. ret = fold(v + state->bias, bits);
  81. update_vlc_state(state, v);
  82. return ret;
  83. }
  84. static av_always_inline void decode_line(FFV1Context *s, int w,
  85. int16_t *sample[2],
  86. int plane_index, int bits)
  87. {
  88. PlaneContext *const p = &s->plane[plane_index];
  89. RangeCoder *const c = &s->c;
  90. int x;
  91. int run_count = 0;
  92. int run_mode = 0;
  93. int run_index = s->run_index;
  94. for (x = 0; x < w; x++) {
  95. int diff, context, sign;
  96. context = get_context(p, sample[1] + x, sample[0] + x, sample[1] + x);
  97. if (context < 0) {
  98. context = -context;
  99. sign = 1;
  100. } else
  101. sign = 0;
  102. av_assert2(context < p->context_count);
  103. if (s->ac) {
  104. diff = get_symbol_inline(c, p->state[context], 1);
  105. } else {
  106. if (context == 0 && run_mode == 0)
  107. run_mode = 1;
  108. if (run_mode) {
  109. if (run_count == 0 && run_mode == 1) {
  110. if (get_bits1(&s->gb)) {
  111. run_count = 1 << ff_log2_run[run_index];
  112. if (x + run_count <= w)
  113. run_index++;
  114. } else {
  115. if (ff_log2_run[run_index])
  116. run_count = get_bits(&s->gb, ff_log2_run[run_index]);
  117. else
  118. run_count = 0;
  119. if (run_index)
  120. run_index--;
  121. run_mode = 2;
  122. }
  123. }
  124. run_count--;
  125. if (run_count < 0) {
  126. run_mode = 0;
  127. run_count = 0;
  128. diff = get_vlc_symbol(&s->gb, &p->vlc_state[context],
  129. bits);
  130. if (diff >= 0)
  131. diff++;
  132. } else
  133. diff = 0;
  134. } else
  135. diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  136. av_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
  137. run_count, run_index, run_mode, x, get_bits_count(&s->gb));
  138. }
  139. if (sign)
  140. diff = -diff;
  141. sample[1][x] = (predict(sample[1] + x, sample[0] + x) + diff) &
  142. ((1 << bits) - 1);
  143. }
  144. s->run_index = run_index;
  145. }
  146. static void decode_plane(FFV1Context *s, uint8_t *src,
  147. int w, int h, int stride, int plane_index)
  148. {
  149. int x, y;
  150. int16_t *sample[2];
  151. sample[0] = s->sample_buffer + 3;
  152. sample[1] = s->sample_buffer + w + 6 + 3;
  153. s->run_index = 0;
  154. memset(s->sample_buffer, 0, 2 * (w + 6) * sizeof(*s->sample_buffer));
  155. for (y = 0; y < h; y++) {
  156. int16_t *temp = sample[0]; // FIXME: try a normal buffer
  157. sample[0] = sample[1];
  158. sample[1] = temp;
  159. sample[1][-1] = sample[0][0];
  160. sample[0][w] = sample[0][w - 1];
  161. // { START_TIMER
  162. if (s->avctx->bits_per_raw_sample <= 8) {
  163. decode_line(s, w, sample, plane_index, 8);
  164. for (x = 0; x < w; x++)
  165. src[x + stride * y] = sample[1][x];
  166. } else {
  167. decode_line(s, w, sample, plane_index,
  168. s->avctx->bits_per_raw_sample);
  169. if (s->packed_at_lsb) {
  170. for (x = 0; x < w; x++)
  171. ((uint16_t *)(src + stride * y))[x] = sample[1][x];
  172. } else {
  173. for (x = 0; x < w; x++)
  174. ((uint16_t *)(src + stride * y))[x] = sample[1][x] << (16 - s->avctx->bits_per_raw_sample);
  175. }
  176. }
  177. // STOP_TIMER("decode-line") }
  178. }
  179. }
  180. static void decode_rgb_frame(FFV1Context *s, uint8_t *src[3], int w, int h,
  181. int stride[3])
  182. {
  183. int x, y, p;
  184. int16_t *sample[4][2];
  185. int lbd = s->avctx->bits_per_raw_sample <= 8;
  186. int bits = s->avctx->bits_per_raw_sample > 0
  187. ? s->avctx->bits_per_raw_sample
  188. : 8;
  189. int offset = 1 << bits;
  190. for (x = 0; x < 4; x++) {
  191. sample[x][0] = s->sample_buffer + x * 2 * (w + 6) + 3;
  192. sample[x][1] = s->sample_buffer + (x * 2 + 1) * (w + 6) + 3;
  193. }
  194. s->run_index = 0;
  195. memset(s->sample_buffer, 0, 8 * (w + 6) * sizeof(*s->sample_buffer));
  196. for (y = 0; y < h; y++) {
  197. for (p = 0; p < 3 + s->transparency; p++) {
  198. int16_t *temp = sample[p][0]; //FIXME try a normal buffer
  199. sample[p][0] = sample[p][1];
  200. sample[p][1] = temp;
  201. sample[p][1][-1] = sample[p][0][0];
  202. sample[p][0][w] = sample[p][0][w - 1];
  203. if (lbd)
  204. decode_line(s, w, sample[p], (p + 1) / 2, 9);
  205. else
  206. decode_line(s, w, sample[p], (p + 1) / 2, bits + 1);
  207. }
  208. for (x = 0; x < w; x++) {
  209. int g = sample[0][1][x];
  210. int b = sample[1][1][x];
  211. int r = sample[2][1][x];
  212. int a = sample[3][1][x];
  213. b -= offset;
  214. r -= offset;
  215. g -= (b + r) >> 2;
  216. b += g;
  217. r += g;
  218. if (lbd)
  219. *((uint32_t *)(src[0] + x * 4 + stride[0] * y)) = b +
  220. (g << 8) + (r << 16) + (a << 24);
  221. else {
  222. *((uint16_t *)(src[0] + x * 2 + stride[0] * y)) = b;
  223. *((uint16_t *)(src[1] + x * 2 + stride[1] * y)) = g;
  224. *((uint16_t *)(src[2] + x * 2 + stride[2] * y)) = r;
  225. }
  226. }
  227. }
  228. }
  229. static int decode_slice_header(FFV1Context *f, FFV1Context *fs)
  230. {
  231. RangeCoder *c = &fs->c;
  232. uint8_t state[CONTEXT_SIZE];
  233. unsigned ps, i, context_count;
  234. memset(state, 128, sizeof(state));
  235. av_assert0(f->version > 2);
  236. fs->slice_x = get_symbol(c, state, 0) * f->width;
  237. fs->slice_y = get_symbol(c, state, 0) * f->height;
  238. fs->slice_width = (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
  239. fs->slice_height = (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
  240. fs->slice_x /= f->num_h_slices;
  241. fs->slice_y /= f->num_v_slices;
  242. fs->slice_width = fs->slice_width / f->num_h_slices - fs->slice_x;
  243. fs->slice_height = fs->slice_height / f->num_v_slices - fs->slice_y;
  244. if ((unsigned)fs->slice_width > f->width ||
  245. (unsigned)fs->slice_height > f->height)
  246. return AVERROR_INVALIDDATA;
  247. if ((unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width ||
  248. (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
  249. return AVERROR_INVALIDDATA;
  250. for (i = 0; i < f->plane_count; i++) {
  251. PlaneContext *const p = &fs->plane[i];
  252. int idx = get_symbol(c, state, 0);
  253. if (idx > (unsigned)f->quant_table_count) {
  254. av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
  255. return AVERROR_INVALIDDATA;
  256. }
  257. p->quant_table_index = idx;
  258. memcpy(p->quant_table, f->quant_tables[idx], sizeof(p->quant_table));
  259. context_count = f->context_count[idx];
  260. if (p->context_count < context_count) {
  261. av_freep(&p->state);
  262. av_freep(&p->vlc_state);
  263. }
  264. p->context_count = context_count;
  265. }
  266. ps = get_symbol(c, state, 0);
  267. if (ps == 1) {
  268. f->picture.interlaced_frame = 1;
  269. f->picture.top_field_first = 1;
  270. } else if (ps == 2) {
  271. f->picture.interlaced_frame = 1;
  272. f->picture.top_field_first = 0;
  273. } else if (ps == 3) {
  274. f->picture.interlaced_frame = 0;
  275. }
  276. f->picture.sample_aspect_ratio.num = get_symbol(c, state, 0);
  277. f->picture.sample_aspect_ratio.den = get_symbol(c, state, 0);
  278. return 0;
  279. }
  280. static int decode_slice(AVCodecContext *c, void *arg)
  281. {
  282. FFV1Context *fs = *(void **)arg;
  283. FFV1Context *f = fs->avctx->priv_data;
  284. int width, height, x, y, ret;
  285. const int ps = (av_pix_fmt_desc_get(c->pix_fmt)->flags & PIX_FMT_PLANAR)
  286. ? (c->bits_per_raw_sample > 8) + 1
  287. : 4;
  288. AVFrame *const p = &f->picture;
  289. if (f->version > 2) {
  290. if (decode_slice_header(f, fs) < 0) {
  291. fs->slice_damaged = 1;
  292. return AVERROR_INVALIDDATA;
  293. }
  294. }
  295. if ((ret = ffv1_init_slice_state(f, fs)) < 0)
  296. return ret;
  297. if (f->picture.key_frame)
  298. ffv1_clear_slice_state(f, fs);
  299. width = fs->slice_width;
  300. height = fs->slice_height;
  301. x = fs->slice_x;
  302. y = fs->slice_y;
  303. if (!fs->ac) {
  304. if (f->version == 3 && f->minor_version > 1 || f->version > 3)
  305. get_rac(&fs->c, (uint8_t[]) { 129 });
  306. fs->ac_byte_count = f->version > 2 || (!x && !y) ? fs->c.bytestream - fs->c.bytestream_start - 1 : 0;
  307. init_get_bits(&fs->gb, fs->c.bytestream_start + fs->ac_byte_count,
  308. (fs->c.bytestream_end - fs->c.bytestream_start -
  309. fs->ac_byte_count) * 8);
  310. }
  311. av_assert1(width && height);
  312. if (f->colorspace == 0) {
  313. const int chroma_width = -((-width) >> f->chroma_h_shift);
  314. const int chroma_height = -((-height) >> f->chroma_v_shift);
  315. const int cx = x >> f->chroma_h_shift;
  316. const int cy = y >> f->chroma_v_shift;
  317. decode_plane(fs, p->data[0] + ps * x + y * p->linesize[0], width,
  318. height, p->linesize[0],
  319. 0);
  320. if (f->chroma_planes) {
  321. decode_plane(fs, p->data[1] + ps * cx + cy * p->linesize[1],
  322. chroma_width, chroma_height, p->linesize[1],
  323. 1);
  324. decode_plane(fs, p->data[2] + ps * cx + cy * p->linesize[2],
  325. chroma_width, chroma_height, p->linesize[2],
  326. 1);
  327. }
  328. if (fs->transparency)
  329. decode_plane(fs, p->data[3] + ps * x + y * p->linesize[3], width,
  330. height, p->linesize[3],
  331. 2);
  332. } else {
  333. uint8_t *planes[3] = { p->data[0] + ps * x + y * p->linesize[0],
  334. p->data[1] + ps * x + y * p->linesize[1],
  335. p->data[2] + ps * x + y * p->linesize[2] };
  336. decode_rgb_frame(fs, planes, width, height, p->linesize);
  337. }
  338. if (fs->ac && f->version > 2) {
  339. int v;
  340. get_rac(&fs->c, (uint8_t[]) { 129 });
  341. v = fs->c.bytestream_end - fs->c.bytestream - 2 - 5 * f->ec;
  342. if (v) {
  343. av_log(f->avctx, AV_LOG_ERROR, "bytestream end mismatching by %d\n",
  344. v);
  345. fs->slice_damaged = 1;
  346. }
  347. }
  348. emms_c();
  349. return 0;
  350. }
  351. static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale)
  352. {
  353. int v;
  354. int i = 0;
  355. uint8_t state[CONTEXT_SIZE];
  356. memset(state, 128, sizeof(state));
  357. for (v = 0; i < 128; v++) {
  358. unsigned len = get_symbol(c, state, 0) + 1;
  359. if (len > 128 - i)
  360. return -1;
  361. while (len--) {
  362. quant_table[i] = scale * v;
  363. i++;
  364. }
  365. }
  366. for (i = 1; i < 128; i++)
  367. quant_table[256 - i] = -quant_table[i];
  368. quant_table[128] = -quant_table[127];
  369. return 2 * v - 1;
  370. }
  371. static int read_quant_tables(RangeCoder *c,
  372. int16_t quant_table[MAX_CONTEXT_INPUTS][256])
  373. {
  374. int i;
  375. int context_count = 1;
  376. for (i = 0; i < 5; i++) {
  377. context_count *= read_quant_table(c, quant_table[i], context_count);
  378. if (context_count > 32768U) {
  379. return -1;
  380. }
  381. }
  382. return (context_count + 1) / 2;
  383. }
  384. static int read_extra_header(FFV1Context *f)
  385. {
  386. RangeCoder *const c = &f->c;
  387. uint8_t state[CONTEXT_SIZE];
  388. int i, j, k, ret;
  389. uint8_t state2[32][CONTEXT_SIZE];
  390. memset(state2, 128, sizeof(state2));
  391. memset(state, 128, sizeof(state));
  392. ff_init_range_decoder(c, f->avctx->extradata, f->avctx->extradata_size);
  393. ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
  394. f->version = get_symbol(c, state, 0);
  395. if (f->version > 2) {
  396. c->bytestream_end -= 4;
  397. f->minor_version = get_symbol(c, state, 0);
  398. }
  399. f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
  400. if (f->ac > 1) {
  401. for (i = 1; i < 256; i++)
  402. f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
  403. }
  404. f->colorspace = get_symbol(c, state, 0); //YUV cs type
  405. f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
  406. f->chroma_planes = get_rac(c, state);
  407. f->chroma_h_shift = get_symbol(c, state, 0);
  408. f->chroma_v_shift = get_symbol(c, state, 0);
  409. f->transparency = get_rac(c, state);
  410. f->plane_count = 2 + f->transparency;
  411. f->num_h_slices = 1 + get_symbol(c, state, 0);
  412. f->num_v_slices = 1 + get_symbol(c, state, 0);
  413. if (f->num_h_slices > (unsigned)f->width ||
  414. f->num_v_slices > (unsigned)f->height) {
  415. av_log(f->avctx, AV_LOG_ERROR, "too many slices\n");
  416. return AVERROR_INVALIDDATA;
  417. }
  418. f->quant_table_count = get_symbol(c, state, 0);
  419. if (f->quant_table_count > (unsigned)MAX_QUANT_TABLES)
  420. return AVERROR_INVALIDDATA;
  421. for (i = 0; i < f->quant_table_count; i++) {
  422. f->context_count[i] = read_quant_tables(c, f->quant_tables[i]);
  423. if (f->context_count[i] < 0) {
  424. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  425. return AVERROR_INVALIDDATA;
  426. }
  427. }
  428. if ((ret = ffv1_allocate_initial_states(f)) < 0)
  429. return ret;
  430. for (i = 0; i < f->quant_table_count; i++)
  431. if (get_rac(c, state)) {
  432. for (j = 0; j < f->context_count[i]; j++)
  433. for (k = 0; k < CONTEXT_SIZE; k++) {
  434. int pred = j ? f->initial_states[i][j - 1][k] : 128;
  435. f->initial_states[i][j][k] =
  436. (pred + get_symbol(c, state2[k], 1)) & 0xFF;
  437. }
  438. }
  439. if (f->version > 2) {
  440. f->ec = get_symbol(c, state, 0);
  441. }
  442. if (f->version > 2) {
  443. unsigned v;
  444. v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0,
  445. f->avctx->extradata, f->avctx->extradata_size);
  446. if (v) {
  447. av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", v);
  448. return AVERROR_INVALIDDATA;
  449. }
  450. }
  451. return 0;
  452. }
  453. static int read_header(FFV1Context *f)
  454. {
  455. uint8_t state[CONTEXT_SIZE];
  456. int i, j, context_count = -1;
  457. RangeCoder *const c = &f->slice_context[0]->c;
  458. memset(state, 128, sizeof(state));
  459. if (f->version < 2) {
  460. unsigned v = get_symbol(c, state, 0);
  461. if (v > 1) {
  462. av_log(f->avctx, AV_LOG_ERROR,
  463. "invalid version %d in version 1 header\n", v);
  464. return AVERROR_INVALIDDATA;
  465. }
  466. f->version = v;
  467. f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
  468. if (f->ac > 1) {
  469. for (i = 1; i < 256; i++)
  470. f->state_transition[i] =
  471. get_symbol(c, state, 1) + c->one_state[i];
  472. }
  473. f->colorspace = get_symbol(c, state, 0); //YUV cs type
  474. if (f->version > 0)
  475. f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
  476. f->chroma_planes = get_rac(c, state);
  477. f->chroma_h_shift = get_symbol(c, state, 0);
  478. f->chroma_v_shift = get_symbol(c, state, 0);
  479. f->transparency = get_rac(c, state);
  480. f->plane_count = 2 + f->transparency;
  481. }
  482. if (f->colorspace == 0) {
  483. if (!f->transparency && !f->chroma_planes) {
  484. if (f->avctx->bits_per_raw_sample <= 8)
  485. f->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  486. else
  487. f->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
  488. } else if (f->avctx->bits_per_raw_sample <= 8 && !f->transparency) {
  489. switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
  490. case 0x00:
  491. f->avctx->pix_fmt = AV_PIX_FMT_YUV444P;
  492. break;
  493. case 0x01:
  494. f->avctx->pix_fmt = AV_PIX_FMT_YUV440P;
  495. break;
  496. case 0x10:
  497. f->avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  498. break;
  499. case 0x11:
  500. f->avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  501. break;
  502. case 0x20:
  503. f->avctx->pix_fmt = AV_PIX_FMT_YUV411P;
  504. break;
  505. case 0x22:
  506. f->avctx->pix_fmt = AV_PIX_FMT_YUV410P;
  507. break;
  508. default:
  509. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  510. return AVERROR(ENOSYS);
  511. }
  512. } else if (f->avctx->bits_per_raw_sample <= 8 && f->transparency) {
  513. switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
  514. case 0x00:
  515. f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
  516. break;
  517. case 0x10:
  518. f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
  519. break;
  520. case 0x11:
  521. f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
  522. break;
  523. default:
  524. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  525. return AVERROR(ENOSYS);
  526. }
  527. } else if (f->avctx->bits_per_raw_sample == 9) {
  528. f->packed_at_lsb = 1;
  529. switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
  530. case 0x00:
  531. f->avctx->pix_fmt = AV_PIX_FMT_YUV444P9;
  532. break;
  533. case 0x10:
  534. f->avctx->pix_fmt = AV_PIX_FMT_YUV422P9;
  535. break;
  536. case 0x11:
  537. f->avctx->pix_fmt = AV_PIX_FMT_YUV420P9;
  538. break;
  539. default:
  540. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  541. return AVERROR(ENOSYS);
  542. }
  543. } else if (f->avctx->bits_per_raw_sample == 10) {
  544. f->packed_at_lsb = 1;
  545. switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
  546. case 0x00:
  547. f->avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
  548. break;
  549. case 0x10:
  550. f->avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
  551. break;
  552. case 0x11:
  553. f->avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
  554. break;
  555. default:
  556. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  557. return AVERROR(ENOSYS);
  558. }
  559. } else {
  560. switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
  561. case 0x00:
  562. f->avctx->pix_fmt = AV_PIX_FMT_YUV444P16;
  563. break;
  564. case 0x10:
  565. f->avctx->pix_fmt = AV_PIX_FMT_YUV422P16;
  566. break;
  567. case 0x11:
  568. f->avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
  569. break;
  570. default:
  571. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  572. return AVERROR(ENOSYS);
  573. }
  574. }
  575. } else if (f->colorspace == 1) {
  576. if (f->chroma_h_shift || f->chroma_v_shift) {
  577. av_log(f->avctx, AV_LOG_ERROR,
  578. "chroma subsampling not supported in this colorspace\n");
  579. return AVERROR(ENOSYS);
  580. }
  581. switch (f->avctx->bits_per_raw_sample) {
  582. case 8:
  583. f->avctx->pix_fmt = AV_PIX_FMT_RGB32;
  584. break;
  585. case 9:
  586. f->avctx->pix_fmt = AV_PIX_FMT_GBRP9;
  587. break;
  588. case 10:
  589. f->avctx->pix_fmt = AV_PIX_FMT_GBRP10;
  590. break;
  591. default:
  592. av_log(f->avctx, AV_LOG_ERROR,
  593. "bit depth %d not supported\n",
  594. f->avctx->bits_per_raw_sample);
  595. return AVERROR(ENOSYS);
  596. }
  597. } else {
  598. av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
  599. return AVERROR(ENOSYS);
  600. }
  601. av_dlog(f->avctx, "%d %d %d\n",
  602. f->chroma_h_shift, f->chroma_v_shift, f->avctx->pix_fmt);
  603. if (f->version < 2) {
  604. context_count = read_quant_tables(c, f->quant_table);
  605. if (context_count < 0) {
  606. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  607. return AVERROR_INVALIDDATA;
  608. }
  609. } else if (f->version < 3) {
  610. f->slice_count = get_symbol(c, state, 0);
  611. } else {
  612. const uint8_t *p = c->bytestream_end;
  613. for (f->slice_count = 0;
  614. f->slice_count < MAX_SLICES && 3 < p - c->bytestream_start;
  615. f->slice_count++) {
  616. int trailer = 3 + 5 * !!f->ec;
  617. int size = AV_RB24(p - trailer);
  618. if (size + trailer > p - c->bytestream_start)
  619. break;
  620. p -= size + trailer;
  621. }
  622. }
  623. if (f->slice_count > (unsigned)MAX_SLICES || f->slice_count <= 0) {
  624. av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid\n",
  625. f->slice_count);
  626. return AVERROR_INVALIDDATA;
  627. }
  628. for (j = 0; j < f->slice_count; j++) {
  629. FFV1Context *fs = f->slice_context[j];
  630. fs->ac = f->ac;
  631. fs->packed_at_lsb = f->packed_at_lsb;
  632. fs->slice_damaged = 0;
  633. if (f->version == 2) {
  634. fs->slice_x = get_symbol(c, state, 0) * f->width;
  635. fs->slice_y = get_symbol(c, state, 0) * f->height;
  636. fs->slice_width =
  637. (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
  638. fs->slice_height =
  639. (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
  640. fs->slice_x /= f->num_h_slices;
  641. fs->slice_y /= f->num_v_slices;
  642. fs->slice_width /= f->num_h_slices - fs->slice_x;
  643. fs->slice_height /= f->num_v_slices - fs->slice_y;
  644. if ((unsigned)fs->slice_width > f->width ||
  645. (unsigned)fs->slice_height > f->height)
  646. return AVERROR_INVALIDDATA;
  647. if ((unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
  648. || (unsigned)fs->slice_y + (uint64_t)fs->slice_height >
  649. f->height)
  650. return AVERROR_INVALIDDATA;
  651. }
  652. for (i = 0; i < f->plane_count; i++) {
  653. PlaneContext *const p = &fs->plane[i];
  654. if (f->version == 2) {
  655. int idx = get_symbol(c, state, 0);
  656. if (idx > (unsigned)f->quant_table_count) {
  657. av_log(f->avctx, AV_LOG_ERROR,
  658. "quant_table_index out of range\n");
  659. return AVERROR_INVALIDDATA;
  660. }
  661. p->quant_table_index = idx;
  662. memcpy(p->quant_table, f->quant_tables[idx],
  663. sizeof(p->quant_table));
  664. context_count = f->context_count[idx];
  665. } else {
  666. memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
  667. }
  668. if (f->version <= 2) {
  669. av_assert0(context_count >= 0);
  670. if (p->context_count < context_count) {
  671. av_freep(&p->state);
  672. av_freep(&p->vlc_state);
  673. }
  674. p->context_count = context_count;
  675. }
  676. }
  677. }
  678. return 0;
  679. }
  680. static av_cold int ffv1_decode_init(AVCodecContext *avctx)
  681. {
  682. FFV1Context *f = avctx->priv_data;
  683. int ret;
  684. ffv1_common_init(avctx);
  685. if (avctx->extradata && (ret = read_extra_header(f)) < 0)
  686. return ret;
  687. if ((ret = ffv1_init_slice_contexts(f)) < 0)
  688. return ret;
  689. return 0;
  690. }
  691. static int ffv1_decode_frame(AVCodecContext *avctx, void *data,
  692. int *data_size, AVPacket *avpkt)
  693. {
  694. const uint8_t *buf = avpkt->data;
  695. int buf_size = avpkt->size;
  696. FFV1Context *f = avctx->priv_data;
  697. RangeCoder *const c = &f->slice_context[0]->c;
  698. AVFrame *const p = &f->picture;
  699. int i, ret;
  700. uint8_t keystate = 128;
  701. const uint8_t *buf_p;
  702. AVFrame *picture = data;
  703. /* release previously stored data */
  704. if (p->data[0])
  705. avctx->release_buffer(avctx, p);
  706. ff_init_range_decoder(c, buf, buf_size);
  707. ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
  708. p->pict_type = AV_PICTURE_TYPE_I; //FIXME I vs. P
  709. if (get_rac(c, &keystate)) {
  710. p->key_frame = 1;
  711. f->key_frame_ok = 0;
  712. if ((ret = read_header(f)) < 0)
  713. return ret;
  714. f->key_frame_ok = 1;
  715. } else {
  716. if (!f->key_frame_ok) {
  717. av_log(avctx, AV_LOG_ERROR,
  718. "Cant decode non keyframe without valid keyframe\n");
  719. return AVERROR_INVALIDDATA;
  720. }
  721. p->key_frame = 0;
  722. }
  723. p->reference = 3; //for error concealment
  724. if ((ret = avctx->get_buffer(avctx, p)) < 0) {
  725. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  726. return ret;
  727. }
  728. if (avctx->debug & FF_DEBUG_PICT_INFO)
  729. av_log(avctx, AV_LOG_DEBUG,
  730. "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
  731. f->version, p->key_frame, f->ac, f->ec, f->slice_count,
  732. f->avctx->bits_per_raw_sample);
  733. buf_p = buf + buf_size;
  734. for (i = f->slice_count - 1; i >= 0; i--) {
  735. FFV1Context *fs = f->slice_context[i];
  736. int trailer = 3 + 5 * !!f->ec;
  737. int v;
  738. if (i || f->version > 2)
  739. v = AV_RB24(buf_p - trailer) + trailer;
  740. else
  741. v = buf_p - c->bytestream_start;
  742. if (buf_p - c->bytestream_start < v) {
  743. av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
  744. return AVERROR_INVALIDDATA;
  745. }
  746. buf_p -= v;
  747. if (f->ec) {
  748. unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, v);
  749. if (crc) {
  750. av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", crc);
  751. fs->slice_damaged = 1;
  752. }
  753. }
  754. if (i) {
  755. ff_init_range_decoder(&fs->c, buf_p, v);
  756. } else
  757. fs->c.bytestream_end = (uint8_t *)(buf_p + v);
  758. }
  759. avctx->execute(avctx, decode_slice, &f->slice_context[0], NULL,
  760. f->slice_count,
  761. sizeof(void *));
  762. for (i = f->slice_count - 1; i >= 0; i--) {
  763. FFV1Context *fs = f->slice_context[i];
  764. int j;
  765. if (fs->slice_damaged && f->last_picture.data[0]) {
  766. const uint8_t *src[4];
  767. uint8_t *dst[4];
  768. for (j = 0; j < 4; j++) {
  769. int sh = (j == 1 || j == 2) ? f->chroma_h_shift : 0;
  770. int sv = (j == 1 || j == 2) ? f->chroma_v_shift : 0;
  771. dst[j] = f->picture.data[j] + f->picture.linesize[j] *
  772. (fs->slice_y >> sv) + (fs->slice_x >> sh);
  773. src[j] = f->last_picture.data[j] +
  774. f->last_picture.linesize[j] *
  775. (fs->slice_y >> sv) + (fs->slice_x >> sh);
  776. }
  777. av_image_copy(dst, f->picture.linesize, (const uint8_t **)src,
  778. f->last_picture.linesize,
  779. avctx->pix_fmt, fs->slice_width,
  780. fs->slice_height);
  781. }
  782. }
  783. f->picture_number++;
  784. *picture = *p;
  785. *data_size = sizeof(AVFrame);
  786. FFSWAP(AVFrame, f->picture, f->last_picture);
  787. return buf_size;
  788. }
  789. AVCodec ff_ffv1_decoder = {
  790. .name = "ffv1",
  791. .type = AVMEDIA_TYPE_VIDEO,
  792. .id = AV_CODEC_ID_FFV1,
  793. .priv_data_size = sizeof(FFV1Context),
  794. .init = ffv1_decode_init,
  795. .close = ffv1_close,
  796. .decode = ffv1_decode_frame,
  797. .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/ |
  798. CODEC_CAP_SLICE_THREADS,
  799. .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
  800. };