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.

946 lines
31KB

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