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.

861 lines
29KB

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