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.

978 lines
33KB

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