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.

964 lines
32KB

  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. int chroma_planes, chroma_h_shift, chroma_v_shift, transparency, colorspace, bits_per_raw_sample;
  474. unsigned v = get_symbol(c, state, 0);
  475. if (v > 1) {
  476. av_log(f->avctx, AV_LOG_ERROR,
  477. "invalid version %d in version 1 header\n", v);
  478. return AVERROR_INVALIDDATA;
  479. }
  480. f->version = v;
  481. f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
  482. if (f->ac > 1) {
  483. for (i = 1; i < 256; i++)
  484. f->state_transition[i] =
  485. get_symbol(c, state, 1) + c->one_state[i];
  486. }
  487. colorspace = get_symbol(c, state, 0); //YUV cs type
  488. bits_per_raw_sample = f->version > 0 ? get_symbol(c, state, 0) : f->avctx->bits_per_raw_sample;
  489. chroma_planes = get_rac(c, state);
  490. chroma_h_shift = get_symbol(c, state, 0);
  491. chroma_v_shift = get_symbol(c, state, 0);
  492. transparency = get_rac(c, state);
  493. if (f->plane_count) {
  494. if (colorspace != f->colorspace ||
  495. bits_per_raw_sample != f->avctx->bits_per_raw_sample ||
  496. chroma_planes != f->chroma_planes ||
  497. chroma_h_shift != f->chroma_h_shift ||
  498. chroma_v_shift != f->chroma_v_shift ||
  499. transparency != f->transparency) {
  500. av_log(f->avctx, AV_LOG_ERROR, "Invalid change of global parameters\n");
  501. return AVERROR_INVALIDDATA;
  502. }
  503. }
  504. f->colorspace = colorspace;
  505. f->avctx->bits_per_raw_sample = bits_per_raw_sample;
  506. f->chroma_planes = chroma_planes;
  507. f->chroma_h_shift = chroma_h_shift;
  508. f->chroma_v_shift = chroma_v_shift;
  509. f->transparency = transparency;
  510. f->plane_count = 2 + f->transparency;
  511. }
  512. if (f->colorspace == 0) {
  513. if (!f->transparency && !f->chroma_planes) {
  514. if (f->avctx->bits_per_raw_sample <= 8)
  515. f->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  516. else
  517. f->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
  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_YUV444P;
  522. break;
  523. case 0x01:
  524. f->avctx->pix_fmt = AV_PIX_FMT_YUV440P;
  525. break;
  526. case 0x10:
  527. f->avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  528. break;
  529. case 0x11:
  530. f->avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  531. break;
  532. case 0x20:
  533. f->avctx->pix_fmt = AV_PIX_FMT_YUV411P;
  534. break;
  535. case 0x22:
  536. f->avctx->pix_fmt = AV_PIX_FMT_YUV410P;
  537. break;
  538. default:
  539. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  540. return AVERROR(ENOSYS);
  541. }
  542. } else if (f->avctx->bits_per_raw_sample <= 8 && f->transparency) {
  543. switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
  544. case 0x00:
  545. f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
  546. break;
  547. case 0x10:
  548. f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
  549. break;
  550. case 0x11:
  551. f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
  552. break;
  553. default:
  554. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  555. return AVERROR(ENOSYS);
  556. }
  557. } else if (f->avctx->bits_per_raw_sample == 9) {
  558. f->packed_at_lsb = 1;
  559. switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
  560. case 0x00:
  561. f->avctx->pix_fmt = AV_PIX_FMT_YUV444P9;
  562. break;
  563. case 0x10:
  564. f->avctx->pix_fmt = AV_PIX_FMT_YUV422P9;
  565. break;
  566. case 0x11:
  567. f->avctx->pix_fmt = AV_PIX_FMT_YUV420P9;
  568. break;
  569. default:
  570. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  571. return AVERROR(ENOSYS);
  572. }
  573. } else if (f->avctx->bits_per_raw_sample == 10) {
  574. f->packed_at_lsb = 1;
  575. switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
  576. case 0x00:
  577. f->avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
  578. break;
  579. case 0x10:
  580. f->avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
  581. break;
  582. case 0x11:
  583. f->avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
  584. break;
  585. default:
  586. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  587. return AVERROR(ENOSYS);
  588. }
  589. } else {
  590. switch (16 * f->chroma_h_shift + f->chroma_v_shift) {
  591. case 0x00:
  592. f->avctx->pix_fmt = AV_PIX_FMT_YUV444P16;
  593. break;
  594. case 0x10:
  595. f->avctx->pix_fmt = AV_PIX_FMT_YUV422P16;
  596. break;
  597. case 0x11:
  598. f->avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
  599. break;
  600. default:
  601. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  602. return AVERROR(ENOSYS);
  603. }
  604. }
  605. } else if (f->colorspace == 1) {
  606. if (f->chroma_h_shift || f->chroma_v_shift) {
  607. av_log(f->avctx, AV_LOG_ERROR,
  608. "chroma subsampling not supported in this colorspace\n");
  609. return AVERROR(ENOSYS);
  610. }
  611. switch (f->avctx->bits_per_raw_sample) {
  612. case 0:
  613. case 8:
  614. f->avctx->pix_fmt = AV_PIX_FMT_RGB32;
  615. break;
  616. case 9:
  617. f->avctx->pix_fmt = AV_PIX_FMT_GBRP9;
  618. break;
  619. case 10:
  620. f->avctx->pix_fmt = AV_PIX_FMT_GBRP10;
  621. break;
  622. default:
  623. av_log(f->avctx, AV_LOG_ERROR,
  624. "bit depth %d not supported\n",
  625. f->avctx->bits_per_raw_sample);
  626. return AVERROR(ENOSYS);
  627. }
  628. } else {
  629. av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
  630. return AVERROR(ENOSYS);
  631. }
  632. av_dlog(f->avctx, "%d %d %d\n",
  633. f->chroma_h_shift, f->chroma_v_shift, f->avctx->pix_fmt);
  634. if (f->version < 2) {
  635. context_count = read_quant_tables(c, f->quant_table);
  636. if (context_count < 0) {
  637. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  638. return AVERROR_INVALIDDATA;
  639. }
  640. } else if (f->version < 3) {
  641. f->slice_count = get_symbol(c, state, 0);
  642. } else {
  643. const uint8_t *p = c->bytestream_end;
  644. for (f->slice_count = 0;
  645. f->slice_count < MAX_SLICES && 3 < p - c->bytestream_start;
  646. f->slice_count++) {
  647. int trailer = 3 + 5 * !!f->ec;
  648. int size = AV_RB24(p - trailer);
  649. if (size + trailer > p - c->bytestream_start)
  650. break;
  651. p -= size + trailer;
  652. }
  653. }
  654. if (f->slice_count > (unsigned)MAX_SLICES || f->slice_count <= 0) {
  655. av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid\n",
  656. f->slice_count);
  657. return AVERROR_INVALIDDATA;
  658. }
  659. for (j = 0; j < f->slice_count; j++) {
  660. FFV1Context *fs = f->slice_context[j];
  661. fs->ac = f->ac;
  662. fs->packed_at_lsb = f->packed_at_lsb;
  663. fs->slice_damaged = 0;
  664. if (f->version == 2) {
  665. fs->slice_x = get_symbol(c, state, 0) * f->width;
  666. fs->slice_y = get_symbol(c, state, 0) * f->height;
  667. fs->slice_width =
  668. (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
  669. fs->slice_height =
  670. (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
  671. fs->slice_x /= f->num_h_slices;
  672. fs->slice_y /= f->num_v_slices;
  673. fs->slice_width = fs->slice_width / f->num_h_slices - fs->slice_x;
  674. fs->slice_height = fs->slice_height / f->num_v_slices - fs->slice_y;
  675. if ((unsigned)fs->slice_width > f->width ||
  676. (unsigned)fs->slice_height > f->height)
  677. return AVERROR_INVALIDDATA;
  678. if ((unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
  679. || (unsigned)fs->slice_y + (uint64_t)fs->slice_height >
  680. f->height)
  681. return AVERROR_INVALIDDATA;
  682. }
  683. for (i = 0; i < f->plane_count; i++) {
  684. PlaneContext *const p = &fs->plane[i];
  685. if (f->version == 2) {
  686. int idx = get_symbol(c, state, 0);
  687. if (idx > (unsigned)f->quant_table_count) {
  688. av_log(f->avctx, AV_LOG_ERROR,
  689. "quant_table_index out of range\n");
  690. return AVERROR_INVALIDDATA;
  691. }
  692. p->quant_table_index = idx;
  693. memcpy(p->quant_table, f->quant_tables[idx],
  694. sizeof(p->quant_table));
  695. context_count = f->context_count[idx];
  696. } else {
  697. memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
  698. }
  699. if (f->version <= 2) {
  700. av_assert0(context_count >= 0);
  701. if (p->context_count < context_count) {
  702. av_freep(&p->state);
  703. av_freep(&p->vlc_state);
  704. }
  705. p->context_count = context_count;
  706. }
  707. }
  708. }
  709. return 0;
  710. }
  711. static av_cold int ffv1_decode_init(AVCodecContext *avctx)
  712. {
  713. FFV1Context *f = avctx->priv_data;
  714. int ret;
  715. ffv1_common_init(avctx);
  716. f->last_picture = av_frame_alloc();
  717. if (!f->last_picture)
  718. return AVERROR(ENOMEM);
  719. if (avctx->extradata && (ret = read_extra_header(f)) < 0)
  720. return ret;
  721. if ((ret = ffv1_init_slice_contexts(f)) < 0)
  722. return ret;
  723. return 0;
  724. }
  725. static int ffv1_decode_frame(AVCodecContext *avctx, void *data,
  726. int *got_frame, AVPacket *avpkt)
  727. {
  728. const uint8_t *buf = avpkt->data;
  729. int buf_size = avpkt->size;
  730. FFV1Context *f = avctx->priv_data;
  731. RangeCoder *const c = &f->slice_context[0]->c;
  732. int i, ret;
  733. uint8_t keystate = 128;
  734. const uint8_t *buf_p;
  735. AVFrame *const p = data;
  736. f->cur = p;
  737. ff_init_range_decoder(c, buf, buf_size);
  738. ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
  739. p->pict_type = AV_PICTURE_TYPE_I; //FIXME I vs. P
  740. if (get_rac(c, &keystate)) {
  741. p->key_frame = 1;
  742. f->key_frame_ok = 0;
  743. if ((ret = read_header(f)) < 0)
  744. return ret;
  745. f->key_frame_ok = 1;
  746. } else {
  747. if (!f->key_frame_ok) {
  748. av_log(avctx, AV_LOG_ERROR,
  749. "Cannot decode non-keyframe without valid keyframe\n");
  750. return AVERROR_INVALIDDATA;
  751. }
  752. p->key_frame = 0;
  753. }
  754. if ((ret = ff_get_buffer(avctx, p, AV_GET_BUFFER_FLAG_REF)) < 0) {
  755. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  756. return ret;
  757. }
  758. if (avctx->debug & FF_DEBUG_PICT_INFO)
  759. av_log(avctx, AV_LOG_DEBUG,
  760. "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
  761. f->version, p->key_frame, f->ac, f->ec, f->slice_count,
  762. f->avctx->bits_per_raw_sample);
  763. buf_p = buf + buf_size;
  764. for (i = f->slice_count - 1; i >= 0; i--) {
  765. FFV1Context *fs = f->slice_context[i];
  766. int trailer = 3 + 5 * !!f->ec;
  767. int v;
  768. if (i || f->version > 2)
  769. v = AV_RB24(buf_p - trailer) + trailer;
  770. else
  771. v = buf_p - c->bytestream_start;
  772. if (buf_p - c->bytestream_start < v) {
  773. av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
  774. return AVERROR_INVALIDDATA;
  775. }
  776. buf_p -= v;
  777. if (f->ec) {
  778. unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, v);
  779. if (crc) {
  780. av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", crc);
  781. fs->slice_damaged = 1;
  782. }
  783. }
  784. if (i) {
  785. ff_init_range_decoder(&fs->c, buf_p, v);
  786. } else
  787. fs->c.bytestream_end = (uint8_t *)(buf_p + v);
  788. fs->cur = p;
  789. }
  790. avctx->execute(avctx, decode_slice, &f->slice_context[0], NULL,
  791. f->slice_count,
  792. sizeof(void *));
  793. for (i = f->slice_count - 1; i >= 0; i--) {
  794. FFV1Context *fs = f->slice_context[i];
  795. int j;
  796. if (fs->slice_damaged && f->last_picture->data[0]) {
  797. const uint8_t *src[4];
  798. uint8_t *dst[4];
  799. for (j = 0; j < 4; j++) {
  800. int sh = (j == 1 || j == 2) ? f->chroma_h_shift : 0;
  801. int sv = (j == 1 || j == 2) ? f->chroma_v_shift : 0;
  802. dst[j] = p->data[j] + p->linesize[j] *
  803. (fs->slice_y >> sv) + (fs->slice_x >> sh);
  804. src[j] = f->last_picture->data[j] +
  805. f->last_picture->linesize[j] *
  806. (fs->slice_y >> sv) + (fs->slice_x >> sh);
  807. }
  808. av_image_copy(dst, p->linesize, (const uint8_t **)src,
  809. f->last_picture->linesize,
  810. avctx->pix_fmt, fs->slice_width,
  811. fs->slice_height);
  812. }
  813. }
  814. f->picture_number++;
  815. av_frame_unref(f->last_picture);
  816. if ((ret = av_frame_ref(f->last_picture, p)) < 0)
  817. return ret;
  818. f->cur = NULL;
  819. *got_frame = 1;
  820. return buf_size;
  821. }
  822. static av_cold int ffv1_decode_close(AVCodecContext *avctx)
  823. {
  824. FFV1Context *s = avctx->priv_data;;
  825. av_frame_free(&s->last_picture);
  826. ffv1_close(avctx);
  827. return 0;
  828. }
  829. AVCodec ff_ffv1_decoder = {
  830. .name = "ffv1",
  831. .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
  832. .type = AVMEDIA_TYPE_VIDEO,
  833. .id = AV_CODEC_ID_FFV1,
  834. .priv_data_size = sizeof(FFV1Context),
  835. .init = ffv1_decode_init,
  836. .close = ffv1_decode_close,
  837. .decode = ffv1_decode_frame,
  838. .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/ |
  839. CODEC_CAP_SLICE_THREADS,
  840. };