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.

865 lines
30KB

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