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.

867 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. 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, ret;
  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 ((ret = ffv1_init_slice_state(f, fs)) < 0)
  294. return ret;
  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, ret;
  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. }
  393. f->colorspace = get_symbol(c, state, 0); //YUV cs type
  394. f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
  395. f->chroma_planes = get_rac(c, state);
  396. f->chroma_h_shift = get_symbol(c, state, 0);
  397. f->chroma_v_shift = get_symbol(c, state, 0);
  398. f->transparency = get_rac(c, state);
  399. f->plane_count = 2 + f->transparency;
  400. f->num_h_slices = 1 + get_symbol(c, state, 0);
  401. f->num_v_slices = 1 + get_symbol(c, state, 0);
  402. if (f->num_h_slices > (unsigned)f->width || !f->num_h_slices ||
  403. f->num_v_slices > (unsigned)f->height || !f->num_v_slices
  404. ) {
  405. av_log(f->avctx, AV_LOG_ERROR, "slice count invalid\n");
  406. return AVERROR_INVALIDDATA;
  407. }
  408. f->quant_table_count = get_symbol(c, state, 0);
  409. if (f->quant_table_count > (unsigned)MAX_QUANT_TABLES)
  410. return AVERROR_INVALIDDATA;
  411. for (i = 0; i < f->quant_table_count; i++) {
  412. f->context_count[i] = read_quant_tables(c, f->quant_tables[i]);
  413. if (f->context_count[i] < 0) {
  414. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  415. return AVERROR_INVALIDDATA;
  416. }
  417. }
  418. if ((ret = ffv1_allocate_initial_states(f)) < 0)
  419. return ret;
  420. for (i = 0; i < f->quant_table_count; i++)
  421. if (get_rac(c, state)) {
  422. for (j = 0; j < f->context_count[i]; j++)
  423. for (k = 0; k < CONTEXT_SIZE; k++) {
  424. int pred = j ? f->initial_states[i][j - 1][k] : 128;
  425. f->initial_states[i][j][k] =
  426. (pred + get_symbol(c, state2[k], 1)) & 0xFF;
  427. }
  428. }
  429. if (f->version > 2) {
  430. f->ec = get_symbol(c, state, 0);
  431. }
  432. if (f->version > 2) {
  433. unsigned v;
  434. v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0,
  435. f->avctx->extradata, f->avctx->extradata_size);
  436. if (v) {
  437. av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", v);
  438. return AVERROR_INVALIDDATA;
  439. }
  440. }
  441. return 0;
  442. }
  443. static int read_header(FFV1Context *f)
  444. {
  445. uint8_t state[CONTEXT_SIZE];
  446. int i, j, context_count = -1; //-1 to avoid warning
  447. RangeCoder *const c = &f->slice_context[0]->c;
  448. memset(state, 128, sizeof(state));
  449. if (f->version < 2) {
  450. unsigned v= get_symbol(c, state, 0);
  451. if (v >= 2) {
  452. av_log(f->avctx, AV_LOG_ERROR, "invalid version %d in ver01 header\n", v);
  453. return AVERROR_INVALIDDATA;
  454. }
  455. f->version = v;
  456. f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
  457. if (f->ac > 1) {
  458. for (i = 1; i < 256; i++)
  459. f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
  460. }
  461. f->colorspace = get_symbol(c, state, 0); //YUV cs type
  462. if (f->version > 0)
  463. f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
  464. f->chroma_planes = get_rac(c, state);
  465. f->chroma_h_shift = get_symbol(c, state, 0);
  466. f->chroma_v_shift = get_symbol(c, state, 0);
  467. f->transparency = get_rac(c, state);
  468. f->plane_count = 2 + f->transparency;
  469. }
  470. if (f->colorspace == 0) {
  471. if (!f->transparency && !f->chroma_planes) {
  472. if (f->avctx->bits_per_raw_sample <= 8)
  473. f->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  474. else
  475. f->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
  476. } else if (f->avctx->bits_per_raw_sample<=8 && !f->transparency) {
  477. switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
  478. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P; break;
  479. case 0x01: f->avctx->pix_fmt = AV_PIX_FMT_YUV440P; break;
  480. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P; break;
  481. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P; break;
  482. case 0x20: f->avctx->pix_fmt = AV_PIX_FMT_YUV411P; break;
  483. case 0x22: f->avctx->pix_fmt = AV_PIX_FMT_YUV410P; break;
  484. default:
  485. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  486. return AVERROR(ENOSYS);
  487. }
  488. } else if (f->avctx->bits_per_raw_sample <= 8 && f->transparency) {
  489. switch(16*f->chroma_h_shift + f->chroma_v_shift) {
  490. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P; break;
  491. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P; break;
  492. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P; break;
  493. default:
  494. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  495. return AVERROR(ENOSYS);
  496. }
  497. } else if (f->avctx->bits_per_raw_sample == 9) {
  498. f->packed_at_lsb = 1;
  499. switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
  500. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P9; break;
  501. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P9; break;
  502. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P9; break;
  503. default:
  504. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  505. return AVERROR(ENOSYS);
  506. }
  507. } else if (f->avctx->bits_per_raw_sample == 10) {
  508. f->packed_at_lsb = 1;
  509. switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
  510. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P10; break;
  511. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P10; break;
  512. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P10; break;
  513. default:
  514. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  515. return AVERROR(ENOSYS);
  516. }
  517. } else {
  518. switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
  519. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P16; break;
  520. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P16; break;
  521. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P16; break;
  522. default:
  523. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  524. return AVERROR(ENOSYS);
  525. }
  526. }
  527. } else if (f->colorspace == 1) {
  528. if (f->chroma_h_shift || f->chroma_v_shift) {
  529. av_log(f->avctx, AV_LOG_ERROR,
  530. "chroma subsampling not supported in this colorspace\n");
  531. return AVERROR(ENOSYS);
  532. }
  533. if ( f->avctx->bits_per_raw_sample == 9)
  534. f->avctx->pix_fmt = AV_PIX_FMT_GBRP9;
  535. else if (f->avctx->bits_per_raw_sample == 10)
  536. f->avctx->pix_fmt = AV_PIX_FMT_GBRP10;
  537. else if (f->avctx->bits_per_raw_sample == 12)
  538. f->avctx->pix_fmt = AV_PIX_FMT_GBRP12;
  539. else if (f->avctx->bits_per_raw_sample == 14)
  540. f->avctx->pix_fmt = AV_PIX_FMT_GBRP14;
  541. else
  542. if (f->transparency) f->avctx->pix_fmt = AV_PIX_FMT_RGB32;
  543. else f->avctx->pix_fmt = AV_PIX_FMT_0RGB32;
  544. } else {
  545. av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
  546. return AVERROR(ENOSYS);
  547. }
  548. av_dlog(f->avctx, "%d %d %d\n",
  549. f->chroma_h_shift, f->chroma_v_shift, f->avctx->pix_fmt);
  550. if (f->version < 2) {
  551. context_count = read_quant_tables(c, f->quant_table);
  552. if (context_count < 0) {
  553. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  554. return AVERROR_INVALIDDATA;
  555. }
  556. } else if (f->version < 3) {
  557. f->slice_count = get_symbol(c, state, 0);
  558. } else {
  559. const uint8_t *p = c->bytestream_end;
  560. for (f->slice_count = 0;
  561. f->slice_count < MAX_SLICES && 3 < p - c->bytestream_start;
  562. f->slice_count++) {
  563. int trailer = 3 + 5*!!f->ec;
  564. int size = AV_RB24(p-trailer);
  565. if (size + trailer > p - c->bytestream_start)
  566. break;
  567. p -= size + trailer;
  568. }
  569. }
  570. if (f->slice_count > (unsigned)MAX_SLICES || f->slice_count <= 0) {
  571. av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid\n", f->slice_count);
  572. return AVERROR_INVALIDDATA;
  573. }
  574. for (j = 0; j < f->slice_count; j++) {
  575. FFV1Context *fs = f->slice_context[j];
  576. fs->ac = f->ac;
  577. fs->packed_at_lsb = f->packed_at_lsb;
  578. fs->slice_damaged = 0;
  579. if (f->version == 2) {
  580. fs->slice_x = get_symbol(c, state, 0) * f->width ;
  581. fs->slice_y = get_symbol(c, state, 0) * f->height;
  582. fs->slice_width = (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
  583. fs->slice_height = (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
  584. fs->slice_x /= f->num_h_slices;
  585. fs->slice_y /= f->num_v_slices;
  586. fs->slice_width = fs->slice_width / f->num_h_slices - fs->slice_x;
  587. fs->slice_height = fs->slice_height / f->num_v_slices - fs->slice_y;
  588. if ((unsigned)fs->slice_width > f->width ||
  589. (unsigned)fs->slice_height > f->height)
  590. return AVERROR_INVALIDDATA;
  591. if ( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
  592. || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
  593. return AVERROR_INVALIDDATA;
  594. }
  595. for (i = 0; i < f->plane_count; i++) {
  596. PlaneContext *const p = &fs->plane[i];
  597. if (f->version == 2) {
  598. int idx = get_symbol(c, state, 0);
  599. if (idx > (unsigned)f->quant_table_count) {
  600. av_log(f->avctx, AV_LOG_ERROR,
  601. "quant_table_index out of range\n");
  602. return AVERROR_INVALIDDATA;
  603. }
  604. p->quant_table_index = idx;
  605. memcpy(p->quant_table, f->quant_tables[idx],
  606. sizeof(p->quant_table));
  607. context_count = f->context_count[idx];
  608. } else {
  609. memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
  610. }
  611. if (f->version <= 2) {
  612. av_assert0(context_count >= 0);
  613. if (p->context_count < context_count) {
  614. av_freep(&p->state);
  615. av_freep(&p->vlc_state);
  616. }
  617. p->context_count = context_count;
  618. }
  619. }
  620. }
  621. return 0;
  622. }
  623. static av_cold int decode_init(AVCodecContext *avctx)
  624. {
  625. FFV1Context *f = avctx->priv_data;
  626. int ret;
  627. if ((ret = ffv1_common_init(avctx)) < 0)
  628. return ret;
  629. if (avctx->extradata && (ret = read_extra_header(f)) < 0)
  630. return ret;
  631. if ((ret = ffv1_init_slice_contexts(f)) < 0)
  632. return ret;
  633. return 0;
  634. }
  635. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
  636. {
  637. const uint8_t *buf = avpkt->data;
  638. int buf_size = avpkt->size;
  639. FFV1Context *f = avctx->priv_data;
  640. RangeCoder *const c = &f->slice_context[0]->c;
  641. AVFrame *const p = &f->picture;
  642. int i, ret;
  643. uint8_t keystate = 128;
  644. const uint8_t *buf_p;
  645. AVFrame *picture = data;
  646. /* release previously stored data */
  647. if (p->data[0])
  648. avctx->release_buffer(avctx, p);
  649. ff_init_range_decoder(c, buf, buf_size);
  650. ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
  651. p->pict_type = AV_PICTURE_TYPE_I; //FIXME I vs. P
  652. if (get_rac(c, &keystate)) {
  653. p->key_frame = 1;
  654. f->key_frame_ok = 0;
  655. if ((ret = read_header(f)) < 0)
  656. return ret;
  657. f->key_frame_ok = 1;
  658. } else {
  659. if (!f->key_frame_ok) {
  660. av_log(avctx, AV_LOG_ERROR,
  661. "Cant decode non keyframe without valid keyframe\n");
  662. return AVERROR_INVALIDDATA;
  663. }
  664. p->key_frame = 0;
  665. }
  666. p->reference = 3; //for error concealment
  667. if ((ret = avctx->get_buffer(avctx, p)) < 0) {
  668. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  669. return ret;
  670. }
  671. if (avctx->debug & FF_DEBUG_PICT_INFO)
  672. av_log(avctx, AV_LOG_DEBUG, "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
  673. f->version, p->key_frame, f->ac, f->ec, f->slice_count, f->avctx->bits_per_raw_sample);
  674. buf_p = buf + buf_size;
  675. for (i = f->slice_count - 1; i >= 0; i--) {
  676. FFV1Context *fs = f->slice_context[i];
  677. int trailer = 3 + 5*!!f->ec;
  678. int v;
  679. if (i || f->version > 2) v = AV_RB24(buf_p-trailer) + trailer;
  680. else v = buf_p - c->bytestream_start;
  681. if (buf_p - c->bytestream_start < v) {
  682. av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
  683. return AVERROR_INVALIDDATA;
  684. }
  685. buf_p -= v;
  686. if (f->ec) {
  687. unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, v);
  688. if (crc) {
  689. int64_t ts = avpkt->pts != AV_NOPTS_VALUE ? avpkt->pts : avpkt->dts;
  690. av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!", crc);
  691. if (ts != AV_NOPTS_VALUE && avctx->pkt_timebase.num) {
  692. av_log(f->avctx, AV_LOG_ERROR, "at %f seconds\n", ts*av_q2d(avctx->pkt_timebase));
  693. } else if (ts != AV_NOPTS_VALUE) {
  694. av_log(f->avctx, AV_LOG_ERROR, "at %"PRId64"\n", ts);
  695. } else {
  696. av_log(f->avctx, AV_LOG_ERROR, "\n");
  697. }
  698. fs->slice_damaged = 1;
  699. }
  700. }
  701. if (i) {
  702. ff_init_range_decoder(&fs->c, buf_p, v);
  703. } else
  704. fs->c.bytestream_end = (uint8_t *)(buf_p + v);
  705. }
  706. avctx->execute(avctx,
  707. decode_slice,
  708. &f->slice_context[0],
  709. NULL,
  710. f->slice_count,
  711. sizeof(void*));
  712. for (i = f->slice_count - 1; i >= 0; i--) {
  713. FFV1Context *fs = f->slice_context[i];
  714. int j;
  715. if (fs->slice_damaged && f->last_picture.data[0]) {
  716. const uint8_t *src[4];
  717. uint8_t *dst[4];
  718. for (j = 0; j < 4; j++) {
  719. int sh = (j==1 || j==2) ? f->chroma_h_shift : 0;
  720. int sv = (j==1 || j==2) ? f->chroma_v_shift : 0;
  721. dst[j] = f->picture .data[j] + f->picture .linesize[j]*
  722. (fs->slice_y>>sv) + (fs->slice_x>>sh);
  723. src[j] = f->last_picture.data[j] + f->last_picture.linesize[j]*
  724. (fs->slice_y>>sv) + (fs->slice_x>>sh);
  725. }
  726. av_image_copy(dst,
  727. f->picture.linesize,
  728. (const uint8_t **)src,
  729. f->last_picture.linesize,
  730. avctx->pix_fmt,
  731. fs->slice_width,
  732. fs->slice_height);
  733. }
  734. }
  735. f->picture_number++;
  736. *picture = *p;
  737. *data_size = sizeof(AVFrame);
  738. FFSWAP(AVFrame, f->picture, f->last_picture);
  739. return buf_size;
  740. }
  741. AVCodec ff_ffv1_decoder = {
  742. .name = "ffv1",
  743. .type = AVMEDIA_TYPE_VIDEO,
  744. .id = AV_CODEC_ID_FFV1,
  745. .priv_data_size = sizeof(FFV1Context),
  746. .init = decode_init,
  747. .close = ffv1_close,
  748. .decode = decode_frame,
  749. .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/ |
  750. CODEC_CAP_SLICE_THREADS,
  751. .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
  752. };