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.

921 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 "internal.h"
  33. #include "get_bits.h"
  34. #include "put_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. 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. if (fs->ac > 1) {
  236. for (i = 1; i < 256; i++) {
  237. fs->c.one_state[i] = f->state_transition[i];
  238. fs->c.zero_state[256 - i] = 256 - fs->c.one_state[i];
  239. }
  240. }
  241. fs->slice_x = get_symbol(c, state, 0) * f->width;
  242. fs->slice_y = get_symbol(c, state, 0) * f->height;
  243. fs->slice_width = (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
  244. fs->slice_height = (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
  245. fs->slice_x /= f->num_h_slices;
  246. fs->slice_y /= f->num_v_slices;
  247. fs->slice_width = fs->slice_width / f->num_h_slices - fs->slice_x;
  248. fs->slice_height = fs->slice_height / f->num_v_slices - fs->slice_y;
  249. if ((unsigned)fs->slice_width > f->width ||
  250. (unsigned)fs->slice_height > f->height)
  251. return AVERROR_INVALIDDATA;
  252. if ((unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width ||
  253. (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
  254. return AVERROR_INVALIDDATA;
  255. for (i = 0; i < f->plane_count; i++) {
  256. PlaneContext *const p = &fs->plane[i];
  257. int idx = get_symbol(c, state, 0);
  258. if (idx > (unsigned)f->quant_table_count) {
  259. av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
  260. return AVERROR_INVALIDDATA;
  261. }
  262. p->quant_table_index = idx;
  263. memcpy(p->quant_table, f->quant_tables[idx], sizeof(p->quant_table));
  264. context_count = f->context_count[idx];
  265. if (p->context_count < context_count) {
  266. av_freep(&p->state);
  267. av_freep(&p->vlc_state);
  268. }
  269. p->context_count = context_count;
  270. }
  271. ps = get_symbol(c, state, 0);
  272. if (ps == 1) {
  273. f->cur->interlaced_frame = 1;
  274. f->cur->top_field_first = 1;
  275. } else if (ps == 2) {
  276. f->cur->interlaced_frame = 1;
  277. f->cur->top_field_first = 0;
  278. } else if (ps == 3) {
  279. f->cur->interlaced_frame = 0;
  280. }
  281. f->cur->sample_aspect_ratio.num = get_symbol(c, state, 0);
  282. f->cur->sample_aspect_ratio.den = get_symbol(c, state, 0);
  283. return 0;
  284. }
  285. static int decode_slice(AVCodecContext *c, void *arg)
  286. {
  287. FFV1Context *fs = *(void **)arg;
  288. FFV1Context *f = fs->avctx->priv_data;
  289. int width, height, x, y, ret;
  290. const int ps = (av_pix_fmt_desc_get(c->pix_fmt)->flags & AV_PIX_FMT_FLAG_PLANAR)
  291. ? (c->bits_per_raw_sample > 8) + 1
  292. : 4;
  293. AVFrame *const p = f->cur;
  294. if (f->version > 2) {
  295. if (decode_slice_header(f, fs) < 0) {
  296. fs->slice_damaged = 1;
  297. return AVERROR_INVALIDDATA;
  298. }
  299. }
  300. if ((ret = ffv1_init_slice_state(f, fs)) < 0)
  301. return ret;
  302. if (f->cur->key_frame)
  303. ffv1_clear_slice_state(f, fs);
  304. width = fs->slice_width;
  305. height = fs->slice_height;
  306. x = fs->slice_x;
  307. y = fs->slice_y;
  308. if (!fs->ac) {
  309. if (f->version == 3 && f->minor_version > 1 || f->version > 3)
  310. get_rac(&fs->c, (uint8_t[]) { 129 });
  311. fs->ac_byte_count = f->version > 2 || (!x && !y) ? fs->c.bytestream - fs->c.bytestream_start - 1 : 0;
  312. init_get_bits(&fs->gb, fs->c.bytestream_start + fs->ac_byte_count,
  313. (fs->c.bytestream_end - fs->c.bytestream_start -
  314. fs->ac_byte_count) * 8);
  315. }
  316. av_assert1(width && height);
  317. if (f->colorspace == 0) {
  318. const int chroma_width = -((-width) >> f->chroma_h_shift);
  319. const int chroma_height = -((-height) >> f->chroma_v_shift);
  320. const int cx = x >> f->chroma_h_shift;
  321. const int cy = y >> f->chroma_v_shift;
  322. decode_plane(fs, p->data[0] + ps * x + y * p->linesize[0], width,
  323. height, p->linesize[0],
  324. 0);
  325. if (f->chroma_planes) {
  326. decode_plane(fs, p->data[1] + ps * cx + cy * p->linesize[1],
  327. chroma_width, chroma_height, p->linesize[1],
  328. 1);
  329. decode_plane(fs, p->data[2] + ps * cx + cy * p->linesize[2],
  330. chroma_width, chroma_height, p->linesize[2],
  331. 1);
  332. }
  333. if (fs->transparency)
  334. decode_plane(fs, p->data[3] + ps * x + y * p->linesize[3], width,
  335. height, p->linesize[3],
  336. 2);
  337. } else {
  338. uint8_t *planes[3] = { p->data[0] + ps * x + y * p->linesize[0],
  339. p->data[1] + ps * x + y * p->linesize[1],
  340. p->data[2] + ps * x + y * p->linesize[2] };
  341. decode_rgb_frame(fs, planes, width, height, p->linesize);
  342. }
  343. if (fs->ac && f->version > 2) {
  344. int v;
  345. get_rac(&fs->c, (uint8_t[]) { 129 });
  346. v = fs->c.bytestream_end - fs->c.bytestream - 2 - 5 * f->ec;
  347. if (v) {
  348. av_log(f->avctx, AV_LOG_ERROR, "bytestream end mismatching by %d\n",
  349. v);
  350. fs->slice_damaged = 1;
  351. }
  352. }
  353. emms_c();
  354. return 0;
  355. }
  356. static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale)
  357. {
  358. int v;
  359. int i = 0;
  360. uint8_t state[CONTEXT_SIZE];
  361. memset(state, 128, sizeof(state));
  362. for (v = 0; i < 128; v++) {
  363. unsigned len = get_symbol(c, state, 0) + 1;
  364. if (len > 128 - i)
  365. return -1;
  366. while (len--) {
  367. quant_table[i] = scale * v;
  368. i++;
  369. }
  370. }
  371. for (i = 1; i < 128; i++)
  372. quant_table[256 - i] = -quant_table[i];
  373. quant_table[128] = -quant_table[127];
  374. return 2 * v - 1;
  375. }
  376. static int read_quant_tables(RangeCoder *c,
  377. int16_t quant_table[MAX_CONTEXT_INPUTS][256])
  378. {
  379. int i;
  380. int context_count = 1;
  381. for (i = 0; i < 5; i++) {
  382. context_count *= read_quant_table(c, quant_table[i], context_count);
  383. if (context_count > 32768U) {
  384. return -1;
  385. }
  386. }
  387. return (context_count + 1) / 2;
  388. }
  389. static int read_extra_header(FFV1Context *f)
  390. {
  391. RangeCoder *const c = &f->c;
  392. uint8_t state[CONTEXT_SIZE];
  393. int i, j, k, ret;
  394. uint8_t state2[32][CONTEXT_SIZE];
  395. memset(state2, 128, sizeof(state2));
  396. memset(state, 128, sizeof(state));
  397. ff_init_range_decoder(c, f->avctx->extradata, f->avctx->extradata_size);
  398. ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
  399. f->version = get_symbol(c, state, 0);
  400. if (f->version > 2) {
  401. c->bytestream_end -= 4;
  402. f->minor_version = get_symbol(c, state, 0);
  403. }
  404. f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
  405. if (f->ac > 1) {
  406. for (i = 1; i < 256; i++)
  407. f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
  408. }
  409. f->colorspace = get_symbol(c, state, 0); //YUV cs type
  410. f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
  411. f->chroma_planes = get_rac(c, state);
  412. f->chroma_h_shift = get_symbol(c, state, 0);
  413. f->chroma_v_shift = get_symbol(c, state, 0);
  414. f->transparency = get_rac(c, state);
  415. f->plane_count = 2 + f->transparency;
  416. f->num_h_slices = 1 + get_symbol(c, state, 0);
  417. f->num_v_slices = 1 + get_symbol(c, state, 0);
  418. if (f->num_h_slices > (unsigned)f->width ||
  419. f->num_v_slices > (unsigned)f->height) {
  420. av_log(f->avctx, AV_LOG_ERROR, "too many slices\n");
  421. return AVERROR_INVALIDDATA;
  422. }
  423. f->quant_table_count = get_symbol(c, state, 0);
  424. if (f->quant_table_count > (unsigned)MAX_QUANT_TABLES)
  425. return AVERROR_INVALIDDATA;
  426. for (i = 0; i < f->quant_table_count; i++) {
  427. f->context_count[i] = read_quant_tables(c, f->quant_tables[i]);
  428. if (f->context_count[i] < 0) {
  429. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  430. return AVERROR_INVALIDDATA;
  431. }
  432. }
  433. if ((ret = ffv1_allocate_initial_states(f)) < 0)
  434. return ret;
  435. for (i = 0; i < f->quant_table_count; i++)
  436. if (get_rac(c, state)) {
  437. for (j = 0; j < f->context_count[i]; j++)
  438. for (k = 0; k < CONTEXT_SIZE; k++) {
  439. int pred = j ? f->initial_states[i][j - 1][k] : 128;
  440. f->initial_states[i][j][k] =
  441. (pred + get_symbol(c, state2[k], 1)) & 0xFF;
  442. }
  443. }
  444. if (f->version > 2) {
  445. f->ec = get_symbol(c, state, 0);
  446. }
  447. if (f->version > 2) {
  448. unsigned v;
  449. v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0,
  450. f->avctx->extradata, f->avctx->extradata_size);
  451. if (v) {
  452. av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", v);
  453. return AVERROR_INVALIDDATA;
  454. }
  455. }
  456. return 0;
  457. }
  458. static int read_header(FFV1Context *f)
  459. {
  460. uint8_t state[CONTEXT_SIZE];
  461. int i, j, context_count = -1;
  462. RangeCoder *const c = &f->slice_context[0]->c;
  463. memset(state, 128, sizeof(state));
  464. if (f->version < 2) {
  465. unsigned v = get_symbol(c, state, 0);
  466. if (v > 1) {
  467. av_log(f->avctx, AV_LOG_ERROR,
  468. "invalid version %d in version 1 header\n", v);
  469. return AVERROR_INVALIDDATA;
  470. }
  471. f->version = v;
  472. f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
  473. if (f->ac > 1) {
  474. for (i = 1; i < 256; i++)
  475. f->state_transition[i] =
  476. get_symbol(c, state, 1) + c->one_state[i];
  477. }
  478. f->colorspace = get_symbol(c, state, 0); //YUV cs type
  479. if (f->version > 0)
  480. f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
  481. f->chroma_planes = get_rac(c, state);
  482. f->chroma_h_shift = get_symbol(c, state, 0);
  483. f->chroma_v_shift = get_symbol(c, state, 0);
  484. f->transparency = get_rac(c, state);
  485. f->plane_count = 2 + f->transparency;
  486. }
  487. if (f->colorspace == 0) {
  488. if (!f->transparency && !f->chroma_planes) {
  489. if (f->avctx->bits_per_raw_sample <= 8)
  490. f->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  491. else
  492. f->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
  493. } else if (f->avctx->bits_per_raw_sample <= 8 && !f->transparency) {
  494. switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
  495. case 0x00:
  496. f->avctx->pix_fmt = AV_PIX_FMT_YUV444P;
  497. break;
  498. case 0x01:
  499. f->avctx->pix_fmt = AV_PIX_FMT_YUV440P;
  500. break;
  501. case 0x10:
  502. f->avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  503. break;
  504. case 0x11:
  505. f->avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  506. break;
  507. case 0x20:
  508. f->avctx->pix_fmt = AV_PIX_FMT_YUV411P;
  509. break;
  510. case 0x22:
  511. f->avctx->pix_fmt = AV_PIX_FMT_YUV410P;
  512. break;
  513. default:
  514. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  515. return AVERROR(ENOSYS);
  516. }
  517. } else if (f->avctx->bits_per_raw_sample <= 8 && f->transparency) {
  518. switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
  519. case 0x00:
  520. f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
  521. break;
  522. case 0x10:
  523. f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
  524. break;
  525. case 0x11:
  526. f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
  527. break;
  528. default:
  529. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  530. return AVERROR(ENOSYS);
  531. }
  532. } else if (f->avctx->bits_per_raw_sample == 9) {
  533. f->packed_at_lsb = 1;
  534. switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
  535. case 0x00:
  536. f->avctx->pix_fmt = AV_PIX_FMT_YUV444P9;
  537. break;
  538. case 0x10:
  539. f->avctx->pix_fmt = AV_PIX_FMT_YUV422P9;
  540. break;
  541. case 0x11:
  542. f->avctx->pix_fmt = AV_PIX_FMT_YUV420P9;
  543. break;
  544. default:
  545. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  546. return AVERROR(ENOSYS);
  547. }
  548. } else if (f->avctx->bits_per_raw_sample == 10) {
  549. f->packed_at_lsb = 1;
  550. switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
  551. case 0x00:
  552. f->avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
  553. break;
  554. case 0x10:
  555. f->avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
  556. break;
  557. case 0x11:
  558. f->avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
  559. break;
  560. default:
  561. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  562. return AVERROR(ENOSYS);
  563. }
  564. } else {
  565. switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
  566. case 0x00:
  567. f->avctx->pix_fmt = AV_PIX_FMT_YUV444P16;
  568. break;
  569. case 0x10:
  570. f->avctx->pix_fmt = AV_PIX_FMT_YUV422P16;
  571. break;
  572. case 0x11:
  573. f->avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
  574. break;
  575. default:
  576. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  577. return AVERROR(ENOSYS);
  578. }
  579. }
  580. } else if (f->colorspace == 1) {
  581. if (f->chroma_h_shift || f->chroma_v_shift) {
  582. av_log(f->avctx, AV_LOG_ERROR,
  583. "chroma subsampling not supported in this colorspace\n");
  584. return AVERROR(ENOSYS);
  585. }
  586. switch (f->avctx->bits_per_raw_sample) {
  587. case 8:
  588. f->avctx->pix_fmt = AV_PIX_FMT_RGB32;
  589. break;
  590. case 9:
  591. f->avctx->pix_fmt = AV_PIX_FMT_GBRP9;
  592. break;
  593. case 10:
  594. f->avctx->pix_fmt = AV_PIX_FMT_GBRP10;
  595. break;
  596. default:
  597. av_log(f->avctx, AV_LOG_ERROR,
  598. "bit depth %d not supported\n",
  599. f->avctx->bits_per_raw_sample);
  600. return AVERROR(ENOSYS);
  601. }
  602. } else {
  603. av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
  604. return AVERROR(ENOSYS);
  605. }
  606. av_dlog(f->avctx, "%d %d %d\n",
  607. f->chroma_h_shift, f->chroma_v_shift, f->avctx->pix_fmt);
  608. if (f->version < 2) {
  609. context_count = read_quant_tables(c, f->quant_table);
  610. if (context_count < 0) {
  611. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  612. return AVERROR_INVALIDDATA;
  613. }
  614. } else if (f->version < 3) {
  615. f->slice_count = get_symbol(c, state, 0);
  616. } else {
  617. const uint8_t *p = c->bytestream_end;
  618. for (f->slice_count = 0;
  619. f->slice_count < MAX_SLICES && 3 < p - c->bytestream_start;
  620. f->slice_count++) {
  621. int trailer = 3 + 5 * !!f->ec;
  622. int size = AV_RB24(p - trailer);
  623. if (size + trailer > p - c->bytestream_start)
  624. break;
  625. p -= size + trailer;
  626. }
  627. }
  628. if (f->slice_count > (unsigned)MAX_SLICES || f->slice_count <= 0) {
  629. av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid\n",
  630. f->slice_count);
  631. return AVERROR_INVALIDDATA;
  632. }
  633. for (j = 0; j < f->slice_count; j++) {
  634. FFV1Context *fs = f->slice_context[j];
  635. fs->ac = f->ac;
  636. fs->packed_at_lsb = f->packed_at_lsb;
  637. fs->slice_damaged = 0;
  638. if (f->version == 2) {
  639. fs->slice_x = get_symbol(c, state, 0) * f->width;
  640. fs->slice_y = get_symbol(c, state, 0) * f->height;
  641. fs->slice_width =
  642. (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
  643. fs->slice_height =
  644. (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
  645. fs->slice_x /= f->num_h_slices;
  646. fs->slice_y /= f->num_v_slices;
  647. fs->slice_width = fs->slice_width / f->num_h_slices - fs->slice_x;
  648. fs->slice_height = fs->slice_height / f->num_v_slices - fs->slice_y;
  649. if ((unsigned)fs->slice_width > f->width ||
  650. (unsigned)fs->slice_height > f->height)
  651. return AVERROR_INVALIDDATA;
  652. if ((unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
  653. || (unsigned)fs->slice_y + (uint64_t)fs->slice_height >
  654. f->height)
  655. return AVERROR_INVALIDDATA;
  656. }
  657. for (i = 0; i < f->plane_count; i++) {
  658. PlaneContext *const p = &fs->plane[i];
  659. if (f->version == 2) {
  660. int idx = get_symbol(c, state, 0);
  661. if (idx > (unsigned)f->quant_table_count) {
  662. av_log(f->avctx, AV_LOG_ERROR,
  663. "quant_table_index out of range\n");
  664. return AVERROR_INVALIDDATA;
  665. }
  666. p->quant_table_index = idx;
  667. memcpy(p->quant_table, f->quant_tables[idx],
  668. sizeof(p->quant_table));
  669. context_count = f->context_count[idx];
  670. } else {
  671. memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
  672. }
  673. if (f->version <= 2) {
  674. av_assert0(context_count >= 0);
  675. if (p->context_count < context_count) {
  676. av_freep(&p->state);
  677. av_freep(&p->vlc_state);
  678. }
  679. p->context_count = context_count;
  680. }
  681. }
  682. }
  683. return 0;
  684. }
  685. static av_cold int ffv1_decode_init(AVCodecContext *avctx)
  686. {
  687. FFV1Context *f = avctx->priv_data;
  688. int ret;
  689. ffv1_common_init(avctx);
  690. if (avctx->extradata && (ret = read_extra_header(f)) < 0)
  691. return ret;
  692. if ((ret = ffv1_init_slice_contexts(f)) < 0)
  693. return ret;
  694. return 0;
  695. }
  696. static int ffv1_decode_frame(AVCodecContext *avctx, void *data,
  697. int *got_frame, AVPacket *avpkt)
  698. {
  699. const uint8_t *buf = avpkt->data;
  700. int buf_size = avpkt->size;
  701. FFV1Context *f = avctx->priv_data;
  702. RangeCoder *const c = &f->slice_context[0]->c;
  703. int i, ret;
  704. uint8_t keystate = 128;
  705. const uint8_t *buf_p;
  706. AVFrame *const p = data;
  707. f->cur = p;
  708. ff_init_range_decoder(c, buf, buf_size);
  709. ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
  710. p->pict_type = AV_PICTURE_TYPE_I; //FIXME I vs. P
  711. if (get_rac(c, &keystate)) {
  712. p->key_frame = 1;
  713. f->key_frame_ok = 0;
  714. if ((ret = read_header(f)) < 0)
  715. return ret;
  716. f->key_frame_ok = 1;
  717. } else {
  718. if (!f->key_frame_ok) {
  719. av_log(avctx, AV_LOG_ERROR,
  720. "Cannot decode non-keyframe without valid keyframe\n");
  721. return AVERROR_INVALIDDATA;
  722. }
  723. p->key_frame = 0;
  724. }
  725. if ((ret = ff_get_buffer(avctx, p, AV_GET_BUFFER_FLAG_REF)) < 0) {
  726. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  727. return ret;
  728. }
  729. if (avctx->debug & FF_DEBUG_PICT_INFO)
  730. av_log(avctx, AV_LOG_DEBUG,
  731. "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
  732. f->version, p->key_frame, f->ac, f->ec, f->slice_count,
  733. f->avctx->bits_per_raw_sample);
  734. buf_p = buf + buf_size;
  735. for (i = f->slice_count - 1; i >= 0; i--) {
  736. FFV1Context *fs = f->slice_context[i];
  737. int trailer = 3 + 5 * !!f->ec;
  738. int v;
  739. if (i || f->version > 2)
  740. v = AV_RB24(buf_p - trailer) + trailer;
  741. else
  742. v = buf_p - c->bytestream_start;
  743. if (buf_p - c->bytestream_start < v) {
  744. av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
  745. return AVERROR_INVALIDDATA;
  746. }
  747. buf_p -= v;
  748. if (f->ec) {
  749. unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, v);
  750. if (crc) {
  751. av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", crc);
  752. fs->slice_damaged = 1;
  753. }
  754. }
  755. if (i) {
  756. ff_init_range_decoder(&fs->c, buf_p, v);
  757. } else
  758. fs->c.bytestream_end = (uint8_t *)(buf_p + v);
  759. fs->cur = p;
  760. }
  761. avctx->execute(avctx, decode_slice, &f->slice_context[0], NULL,
  762. f->slice_count,
  763. sizeof(void *));
  764. for (i = f->slice_count - 1; i >= 0; i--) {
  765. FFV1Context *fs = f->slice_context[i];
  766. int j;
  767. if (fs->slice_damaged && f->last_picture.data[0]) {
  768. const uint8_t *src[4];
  769. uint8_t *dst[4];
  770. for (j = 0; j < 4; j++) {
  771. int sh = (j == 1 || j == 2) ? f->chroma_h_shift : 0;
  772. int sv = (j == 1 || j == 2) ? f->chroma_v_shift : 0;
  773. dst[j] = p->data[j] + p->linesize[j] *
  774. (fs->slice_y >> sv) + (fs->slice_x >> sh);
  775. src[j] = f->last_picture.data[j] +
  776. f->last_picture.linesize[j] *
  777. (fs->slice_y >> sv) + (fs->slice_x >> sh);
  778. }
  779. av_image_copy(dst, p->linesize, (const uint8_t **)src,
  780. f->last_picture.linesize,
  781. avctx->pix_fmt, fs->slice_width,
  782. fs->slice_height);
  783. }
  784. }
  785. f->picture_number++;
  786. av_frame_unref(&f->last_picture);
  787. if ((ret = av_frame_ref(&f->last_picture, p)) < 0)
  788. return ret;
  789. f->cur = NULL;
  790. *got_frame = 1;
  791. return buf_size;
  792. }
  793. AVCodec ff_ffv1_decoder = {
  794. .name = "ffv1",
  795. .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
  796. .type = AVMEDIA_TYPE_VIDEO,
  797. .id = AV_CODEC_ID_FFV1,
  798. .priv_data_size = sizeof(FFV1Context),
  799. .init = ffv1_decode_init,
  800. .close = ffv1_close,
  801. .decode = ffv1_decode_frame,
  802. .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/ |
  803. CODEC_CAP_SLICE_THREADS,
  804. };