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.

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