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.

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