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.

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