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.

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