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.

1014 lines
35KB

  1. /*
  2. * FFV1 decoder
  3. *
  4. * Copyright (c) 2003-2013 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. int i, si;
  283. for( si=0; fs != f->slice_context[si]; si ++)
  284. ;
  285. if(f->fsrc && !p->key_frame)
  286. ff_thread_await_progress(&f->last_picture, si, 0);
  287. if(f->fsrc && !p->key_frame) {
  288. FFV1Context *fssrc = f->fsrc->slice_context[si];
  289. FFV1Context *fsdst = f->slice_context[si];
  290. av_assert1(fsdst->plane_count == fssrc->plane_count);
  291. av_assert1(fsdst == fs);
  292. if (!p->key_frame)
  293. fsdst->slice_damaged |= fssrc->slice_damaged;
  294. for (i = 0; i < f->plane_count; i++) {
  295. PlaneContext *psrc = &fssrc->plane[i];
  296. PlaneContext *pdst = &fsdst->plane[i];
  297. av_free(pdst->state);
  298. av_free(pdst->vlc_state);
  299. memcpy(pdst, psrc, sizeof(*pdst));
  300. pdst->state = NULL;
  301. pdst->vlc_state = NULL;
  302. if (fssrc->ac) {
  303. pdst->state = av_malloc(CONTEXT_SIZE * psrc->context_count);
  304. memcpy(pdst->state, psrc->state, CONTEXT_SIZE * psrc->context_count);
  305. } else {
  306. pdst->vlc_state = av_malloc(sizeof(*pdst->vlc_state) * psrc->context_count);
  307. memcpy(pdst->vlc_state, psrc->vlc_state, sizeof(*pdst->vlc_state) * psrc->context_count);
  308. }
  309. }
  310. }
  311. if (f->version > 2) {
  312. if (ffv1_init_slice_state(f, fs) < 0)
  313. return AVERROR(ENOMEM);
  314. if (decode_slice_header(f, fs) < 0) {
  315. fs->slice_damaged = 1;
  316. return AVERROR_INVALIDDATA;
  317. }
  318. }
  319. if ((ret = ffv1_init_slice_state(f, fs)) < 0)
  320. return ret;
  321. if (f->cur->key_frame)
  322. ffv1_clear_slice_state(f, fs);
  323. width = fs->slice_width;
  324. height = fs->slice_height;
  325. x = fs->slice_x;
  326. y = fs->slice_y;
  327. if (!fs->ac) {
  328. if (f->version == 3 && f->micro_version > 1 || f->version > 3)
  329. get_rac(&fs->c, (uint8_t[]) { 129 });
  330. fs->ac_byte_count = f->version > 2 || (!x && !y) ? fs->c.bytestream - fs->c.bytestream_start - 1 : 0;
  331. init_get_bits(&fs->gb,
  332. fs->c.bytestream_start + fs->ac_byte_count,
  333. (fs->c.bytestream_end - fs->c.bytestream_start - fs->ac_byte_count) * 8);
  334. }
  335. av_assert1(width && height);
  336. if (f->colorspace == 0) {
  337. const int chroma_width = FF_CEIL_RSHIFT(width, f->chroma_h_shift);
  338. const int chroma_height = FF_CEIL_RSHIFT(height, f->chroma_v_shift);
  339. const int cx = x >> f->chroma_h_shift;
  340. const int cy = y >> f->chroma_v_shift;
  341. decode_plane(fs, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0);
  342. if (f->chroma_planes) {
  343. decode_plane(fs, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
  344. decode_plane(fs, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1);
  345. }
  346. if (fs->transparency)
  347. decode_plane(fs, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], 2);
  348. } else {
  349. uint8_t *planes[3] = { p->data[0] + ps * x + y * p->linesize[0],
  350. p->data[1] + ps * x + y * p->linesize[1],
  351. p->data[2] + ps * x + y * p->linesize[2] };
  352. decode_rgb_frame(fs, planes, width, height, p->linesize);
  353. }
  354. if (fs->ac && f->version > 2) {
  355. int v;
  356. get_rac(&fs->c, (uint8_t[]) { 129 });
  357. v = fs->c.bytestream_end - fs->c.bytestream - 2 - 5*f->ec;
  358. if (v) {
  359. av_log(f->avctx, AV_LOG_ERROR, "bytestream end mismatching by %d\n", v);
  360. fs->slice_damaged = 1;
  361. }
  362. }
  363. emms_c();
  364. ff_thread_report_progress(&f->picture, si, 0);
  365. return 0;
  366. }
  367. static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale)
  368. {
  369. int v;
  370. int i = 0;
  371. uint8_t state[CONTEXT_SIZE];
  372. memset(state, 128, sizeof(state));
  373. for (v = 0; i < 128; v++) {
  374. unsigned len = get_symbol(c, state, 0) + 1;
  375. if (len > 128 - i)
  376. return AVERROR_INVALIDDATA;
  377. while (len--) {
  378. quant_table[i] = scale * v;
  379. i++;
  380. }
  381. }
  382. for (i = 1; i < 128; i++)
  383. quant_table[256 - i] = -quant_table[i];
  384. quant_table[128] = -quant_table[127];
  385. return 2 * v - 1;
  386. }
  387. static int read_quant_tables(RangeCoder *c,
  388. int16_t quant_table[MAX_CONTEXT_INPUTS][256])
  389. {
  390. int i;
  391. int context_count = 1;
  392. for (i = 0; i < 5; i++) {
  393. context_count *= read_quant_table(c, quant_table[i], context_count);
  394. if (context_count > 32768U) {
  395. return AVERROR_INVALIDDATA;
  396. }
  397. }
  398. return (context_count + 1) / 2;
  399. }
  400. static int read_extra_header(FFV1Context *f)
  401. {
  402. RangeCoder *const c = &f->c;
  403. uint8_t state[CONTEXT_SIZE];
  404. int i, j, k, ret;
  405. uint8_t state2[32][CONTEXT_SIZE];
  406. memset(state2, 128, sizeof(state2));
  407. memset(state, 128, sizeof(state));
  408. ff_init_range_decoder(c, f->avctx->extradata, f->avctx->extradata_size);
  409. ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
  410. f->version = get_symbol(c, state, 0);
  411. if (f->version < 2) {
  412. av_log(f->avctx, AV_LOG_ERROR, "Invalid version in global header\n");
  413. return AVERROR_INVALIDDATA;
  414. }
  415. if (f->version > 2) {
  416. c->bytestream_end -= 4;
  417. f->micro_version = get_symbol(c, state, 0);
  418. }
  419. f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
  420. if (f->ac > 1) {
  421. for (i = 1; i < 256; i++)
  422. f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
  423. }
  424. f->colorspace = get_symbol(c, state, 0); //YUV cs type
  425. f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
  426. f->chroma_planes = get_rac(c, state);
  427. f->chroma_h_shift = get_symbol(c, state, 0);
  428. f->chroma_v_shift = get_symbol(c, state, 0);
  429. f->transparency = get_rac(c, state);
  430. f->plane_count = 1 + (f->chroma_planes || f->version<4) + f->transparency;
  431. f->num_h_slices = 1 + get_symbol(c, state, 0);
  432. f->num_v_slices = 1 + get_symbol(c, state, 0);
  433. if (f->num_h_slices > (unsigned)f->width || !f->num_h_slices ||
  434. f->num_v_slices > (unsigned)f->height || !f->num_v_slices
  435. ) {
  436. av_log(f->avctx, AV_LOG_ERROR, "slice count invalid\n");
  437. return AVERROR_INVALIDDATA;
  438. }
  439. f->quant_table_count = get_symbol(c, state, 0);
  440. if (f->quant_table_count > (unsigned)MAX_QUANT_TABLES)
  441. return AVERROR_INVALIDDATA;
  442. for (i = 0; i < f->quant_table_count; i++) {
  443. f->context_count[i] = read_quant_tables(c, f->quant_tables[i]);
  444. if (f->context_count[i] < 0) {
  445. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  446. return AVERROR_INVALIDDATA;
  447. }
  448. }
  449. if ((ret = ffv1_allocate_initial_states(f)) < 0)
  450. return ret;
  451. for (i = 0; i < f->quant_table_count; i++)
  452. if (get_rac(c, state)) {
  453. for (j = 0; j < f->context_count[i]; j++)
  454. for (k = 0; k < CONTEXT_SIZE; k++) {
  455. int pred = j ? f->initial_states[i][j - 1][k] : 128;
  456. f->initial_states[i][j][k] =
  457. (pred + get_symbol(c, state2[k], 1)) & 0xFF;
  458. }
  459. }
  460. if (f->version > 2) {
  461. f->ec = get_symbol(c, state, 0);
  462. if (f->micro_version > 2)
  463. f->intra = get_symbol(c, state, 0);
  464. }
  465. if (f->version > 2) {
  466. unsigned v;
  467. v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0,
  468. f->avctx->extradata, f->avctx->extradata_size);
  469. if (v) {
  470. av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", v);
  471. return AVERROR_INVALIDDATA;
  472. }
  473. }
  474. if (f->avctx->debug & FF_DEBUG_PICT_INFO)
  475. av_log(f->avctx, AV_LOG_DEBUG,
  476. "global: ver:%d.%d, coder:%d, colorspace: %d bpr:%d chroma:%d(%d:%d), alpha:%d slices:%dx%d qtabs:%d ec:%d intra:%d\n",
  477. f->version, f->micro_version,
  478. f->ac,
  479. f->colorspace,
  480. f->avctx->bits_per_raw_sample,
  481. f->chroma_planes, f->chroma_h_shift, f->chroma_v_shift,
  482. f->transparency,
  483. f->num_h_slices, f->num_v_slices,
  484. f->quant_table_count,
  485. f->ec,
  486. f->intra
  487. );
  488. return 0;
  489. }
  490. static int read_header(FFV1Context *f)
  491. {
  492. uint8_t state[CONTEXT_SIZE];
  493. int i, j, context_count = -1; //-1 to avoid warning
  494. RangeCoder *const c = &f->slice_context[0]->c;
  495. memset(state, 128, sizeof(state));
  496. if (f->version < 2) {
  497. int chroma_planes, chroma_h_shift, chroma_v_shift, transparency, colorspace, bits_per_raw_sample;
  498. unsigned v= get_symbol(c, state, 0);
  499. if (v >= 2) {
  500. av_log(f->avctx, AV_LOG_ERROR, "invalid version %d in ver01 header\n", v);
  501. return AVERROR_INVALIDDATA;
  502. }
  503. f->version = v;
  504. f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
  505. if (f->ac > 1) {
  506. for (i = 1; i < 256; i++)
  507. f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
  508. }
  509. colorspace = get_symbol(c, state, 0); //YUV cs type
  510. bits_per_raw_sample = f->version > 0 ? get_symbol(c, state, 0) : f->avctx->bits_per_raw_sample;
  511. chroma_planes = get_rac(c, state);
  512. chroma_h_shift = get_symbol(c, state, 0);
  513. chroma_v_shift = get_symbol(c, state, 0);
  514. transparency = get_rac(c, state);
  515. if (f->plane_count) {
  516. if ( colorspace != f->colorspace
  517. || bits_per_raw_sample != f->avctx->bits_per_raw_sample
  518. || chroma_planes != f->chroma_planes
  519. || chroma_h_shift!= f->chroma_h_shift
  520. || chroma_v_shift!= f->chroma_v_shift
  521. || transparency != f->transparency) {
  522. av_log(f->avctx, AV_LOG_ERROR, "Invalid change of global parameters\n");
  523. return AVERROR_INVALIDDATA;
  524. }
  525. }
  526. f->colorspace = colorspace;
  527. f->avctx->bits_per_raw_sample = bits_per_raw_sample;
  528. f->chroma_planes = chroma_planes;
  529. f->chroma_h_shift = chroma_h_shift;
  530. f->chroma_v_shift = chroma_v_shift;
  531. f->transparency = transparency;
  532. f->plane_count = 2 + f->transparency;
  533. }
  534. if (f->colorspace == 0) {
  535. if (!f->transparency && !f->chroma_planes) {
  536. if (f->avctx->bits_per_raw_sample <= 8)
  537. f->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  538. else
  539. f->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
  540. } else if (f->avctx->bits_per_raw_sample<=8 && !f->transparency) {
  541. switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
  542. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P; break;
  543. case 0x01: f->avctx->pix_fmt = AV_PIX_FMT_YUV440P; break;
  544. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P; break;
  545. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P; break;
  546. case 0x20: f->avctx->pix_fmt = AV_PIX_FMT_YUV411P; break;
  547. case 0x22: f->avctx->pix_fmt = AV_PIX_FMT_YUV410P; break;
  548. default:
  549. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  550. return AVERROR(ENOSYS);
  551. }
  552. } else if (f->avctx->bits_per_raw_sample <= 8 && f->transparency) {
  553. switch(16*f->chroma_h_shift + f->chroma_v_shift) {
  554. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P; break;
  555. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P; break;
  556. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P; break;
  557. default:
  558. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  559. return AVERROR(ENOSYS);
  560. }
  561. } else if (f->avctx->bits_per_raw_sample == 9) {
  562. f->packed_at_lsb = 1;
  563. switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
  564. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P9; break;
  565. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P9; break;
  566. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P9; break;
  567. default:
  568. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  569. return AVERROR(ENOSYS);
  570. }
  571. } else if (f->avctx->bits_per_raw_sample == 10) {
  572. f->packed_at_lsb = 1;
  573. switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
  574. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P10; break;
  575. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P10; break;
  576. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P10; break;
  577. default:
  578. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  579. return AVERROR(ENOSYS);
  580. }
  581. } else {
  582. switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
  583. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P16; break;
  584. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P16; break;
  585. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P16; break;
  586. default:
  587. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  588. return AVERROR(ENOSYS);
  589. }
  590. }
  591. } else if (f->colorspace == 1) {
  592. if (f->chroma_h_shift || f->chroma_v_shift) {
  593. av_log(f->avctx, AV_LOG_ERROR,
  594. "chroma subsampling not supported in this colorspace\n");
  595. return AVERROR(ENOSYS);
  596. }
  597. if ( f->avctx->bits_per_raw_sample == 9)
  598. f->avctx->pix_fmt = AV_PIX_FMT_GBRP9;
  599. else if (f->avctx->bits_per_raw_sample == 10)
  600. f->avctx->pix_fmt = AV_PIX_FMT_GBRP10;
  601. else if (f->avctx->bits_per_raw_sample == 12)
  602. f->avctx->pix_fmt = AV_PIX_FMT_GBRP12;
  603. else if (f->avctx->bits_per_raw_sample == 14)
  604. f->avctx->pix_fmt = AV_PIX_FMT_GBRP14;
  605. else
  606. if (f->transparency) f->avctx->pix_fmt = AV_PIX_FMT_RGB32;
  607. else f->avctx->pix_fmt = AV_PIX_FMT_0RGB32;
  608. } else {
  609. av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
  610. return AVERROR(ENOSYS);
  611. }
  612. av_dlog(f->avctx, "%d %d %d\n",
  613. f->chroma_h_shift, f->chroma_v_shift, f->avctx->pix_fmt);
  614. if (f->version < 2) {
  615. context_count = read_quant_tables(c, f->quant_table);
  616. if (context_count < 0) {
  617. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  618. return AVERROR_INVALIDDATA;
  619. }
  620. } else if (f->version < 3) {
  621. f->slice_count = get_symbol(c, state, 0);
  622. } else {
  623. const uint8_t *p = c->bytestream_end;
  624. for (f->slice_count = 0;
  625. f->slice_count < MAX_SLICES && 3 < p - c->bytestream_start;
  626. f->slice_count++) {
  627. int trailer = 3 + 5*!!f->ec;
  628. int size = AV_RB24(p-trailer);
  629. if (size + trailer > p - c->bytestream_start)
  630. break;
  631. p -= size + trailer;
  632. }
  633. }
  634. if (f->slice_count > (unsigned)MAX_SLICES || f->slice_count <= 0) {
  635. av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid\n", f->slice_count);
  636. return AVERROR_INVALIDDATA;
  637. }
  638. for (j = 0; j < f->slice_count; j++) {
  639. FFV1Context *fs = f->slice_context[j];
  640. fs->ac = f->ac;
  641. fs->packed_at_lsb = f->packed_at_lsb;
  642. fs->slice_damaged = 0;
  643. if (f->version == 2) {
  644. fs->slice_x = get_symbol(c, state, 0) * f->width ;
  645. fs->slice_y = get_symbol(c, state, 0) * f->height;
  646. fs->slice_width = (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
  647. fs->slice_height = (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
  648. fs->slice_x /= f->num_h_slices;
  649. fs->slice_y /= f->num_v_slices;
  650. fs->slice_width = fs->slice_width / f->num_h_slices - fs->slice_x;
  651. fs->slice_height = fs->slice_height / f->num_v_slices - fs->slice_y;
  652. if ((unsigned)fs->slice_width > f->width ||
  653. (unsigned)fs->slice_height > f->height)
  654. return AVERROR_INVALIDDATA;
  655. if ( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
  656. || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
  657. return AVERROR_INVALIDDATA;
  658. }
  659. for (i = 0; i < f->plane_count; i++) {
  660. PlaneContext *const p = &fs->plane[i];
  661. if (f->version == 2) {
  662. int idx = get_symbol(c, state, 0);
  663. if (idx > (unsigned)f->quant_table_count) {
  664. av_log(f->avctx, AV_LOG_ERROR,
  665. "quant_table_index out of range\n");
  666. return AVERROR_INVALIDDATA;
  667. }
  668. p->quant_table_index = idx;
  669. memcpy(p->quant_table, f->quant_tables[idx],
  670. sizeof(p->quant_table));
  671. context_count = f->context_count[idx];
  672. } else {
  673. memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
  674. }
  675. if (f->version <= 2) {
  676. av_assert0(context_count >= 0);
  677. if (p->context_count < context_count) {
  678. av_freep(&p->state);
  679. av_freep(&p->vlc_state);
  680. }
  681. p->context_count = context_count;
  682. }
  683. }
  684. }
  685. return 0;
  686. }
  687. static av_cold int decode_init(AVCodecContext *avctx)
  688. {
  689. FFV1Context *f = avctx->priv_data;
  690. int ret;
  691. if ((ret = ffv1_common_init(avctx)) < 0)
  692. return ret;
  693. if (avctx->extradata && (ret = read_extra_header(f)) < 0)
  694. return ret;
  695. if ((ret = ffv1_init_slice_contexts(f)) < 0)
  696. return ret;
  697. avctx->internal->allocate_progress = 1;
  698. return 0;
  699. }
  700. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
  701. {
  702. const uint8_t *buf = avpkt->data;
  703. int buf_size = avpkt->size;
  704. FFV1Context *f = avctx->priv_data;
  705. RangeCoder *const c = &f->slice_context[0]->c;
  706. int i, ret;
  707. uint8_t keystate = 128;
  708. const uint8_t *buf_p;
  709. AVFrame *p;
  710. if (f->last_picture.f)
  711. ff_thread_release_buffer(avctx, &f->last_picture);
  712. FFSWAP(ThreadFrame, f->picture, f->last_picture);
  713. f->cur = p = f->picture.f;
  714. if (f->version < 3 && avctx->field_order > AV_FIELD_PROGRESSIVE) {
  715. /* we have interlaced material flagged in container */
  716. p->interlaced_frame = 1;
  717. if (avctx->field_order == AV_FIELD_TT || avctx->field_order == AV_FIELD_TB)
  718. p->top_field_first = 1;
  719. }
  720. f->avctx = avctx;
  721. ff_init_range_decoder(c, buf, buf_size);
  722. ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
  723. p->pict_type = AV_PICTURE_TYPE_I; //FIXME I vs. P
  724. if (get_rac(c, &keystate)) {
  725. p->key_frame = 1;
  726. f->key_frame_ok = 0;
  727. if ((ret = read_header(f)) < 0)
  728. return ret;
  729. f->key_frame_ok = 1;
  730. } else {
  731. if (!f->key_frame_ok) {
  732. av_log(avctx, AV_LOG_ERROR,
  733. "Cannot decode non-keyframe without valid keyframe\n");
  734. return AVERROR_INVALIDDATA;
  735. }
  736. p->key_frame = 0;
  737. }
  738. if ((ret = ff_thread_get_buffer(avctx, &f->picture, AV_GET_BUFFER_FLAG_REF)) < 0)
  739. return ret;
  740. if (avctx->debug & FF_DEBUG_PICT_INFO)
  741. av_log(avctx, AV_LOG_DEBUG, "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
  742. f->version, p->key_frame, f->ac, f->ec, f->slice_count, f->avctx->bits_per_raw_sample);
  743. ff_thread_finish_setup(avctx);
  744. buf_p = buf + buf_size;
  745. for (i = f->slice_count - 1; i >= 0; i--) {
  746. FFV1Context *fs = f->slice_context[i];
  747. int trailer = 3 + 5*!!f->ec;
  748. int v;
  749. if (i || f->version > 2) v = AV_RB24(buf_p-trailer) + trailer;
  750. else v = buf_p - c->bytestream_start;
  751. if (buf_p - c->bytestream_start < v) {
  752. av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
  753. return AVERROR_INVALIDDATA;
  754. }
  755. buf_p -= v;
  756. if (f->ec) {
  757. unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, v);
  758. if (crc) {
  759. int64_t ts = avpkt->pts != AV_NOPTS_VALUE ? avpkt->pts : avpkt->dts;
  760. av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!", crc);
  761. if (ts != AV_NOPTS_VALUE && avctx->pkt_timebase.num) {
  762. av_log(f->avctx, AV_LOG_ERROR, "at %f seconds\n", ts*av_q2d(avctx->pkt_timebase));
  763. } else if (ts != AV_NOPTS_VALUE) {
  764. av_log(f->avctx, AV_LOG_ERROR, "at %"PRId64"\n", ts);
  765. } else {
  766. av_log(f->avctx, AV_LOG_ERROR, "\n");
  767. }
  768. fs->slice_damaged = 1;
  769. }
  770. }
  771. if (i) {
  772. ff_init_range_decoder(&fs->c, buf_p, v);
  773. } else
  774. fs->c.bytestream_end = (uint8_t *)(buf_p + v);
  775. fs->avctx = avctx;
  776. fs->cur = p;
  777. }
  778. avctx->execute(avctx,
  779. decode_slice,
  780. &f->slice_context[0],
  781. NULL,
  782. f->slice_count,
  783. sizeof(void*));
  784. for (i = f->slice_count - 1; i >= 0; i--) {
  785. FFV1Context *fs = f->slice_context[i];
  786. int j;
  787. if (fs->slice_damaged && f->last_picture.f->data[0]) {
  788. const uint8_t *src[4];
  789. uint8_t *dst[4];
  790. ff_thread_await_progress(&f->last_picture, INT_MAX, 0);
  791. for (j = 0; j < 4; j++) {
  792. int sh = (j==1 || j==2) ? f->chroma_h_shift : 0;
  793. int sv = (j==1 || j==2) ? f->chroma_v_shift : 0;
  794. dst[j] = p->data[j] + p->linesize[j]*
  795. (fs->slice_y>>sv) + (fs->slice_x>>sh);
  796. src[j] = f->last_picture.f->data[j] + f->last_picture.f->linesize[j]*
  797. (fs->slice_y>>sv) + (fs->slice_x>>sh);
  798. }
  799. av_image_copy(dst, p->linesize, (const uint8_t **)src,
  800. f->last_picture.f->linesize,
  801. avctx->pix_fmt,
  802. fs->slice_width,
  803. fs->slice_height);
  804. }
  805. }
  806. ff_thread_report_progress(&f->picture, INT_MAX, 0);
  807. f->picture_number++;
  808. if (f->last_picture.f)
  809. ff_thread_release_buffer(avctx, &f->last_picture);
  810. f->cur = NULL;
  811. if ((ret = av_frame_ref(data, f->picture.f)) < 0)
  812. return ret;
  813. *got_frame = 1;
  814. return buf_size;
  815. }
  816. static int init_thread_copy(AVCodecContext *avctx)
  817. {
  818. FFV1Context *f = avctx->priv_data;
  819. f->picture.f = NULL;
  820. f->last_picture.f = NULL;
  821. f->sample_buffer = NULL;
  822. f->quant_table_count = 0;
  823. f->slice_count = 0;
  824. return 0;
  825. }
  826. static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
  827. {
  828. FFV1Context *fsrc = src->priv_data;
  829. FFV1Context *fdst = dst->priv_data;
  830. int i, ret;
  831. if (dst == src)
  832. return 0;
  833. if (!fdst->picture.f) {
  834. memcpy(fdst, fsrc, sizeof(*fdst));
  835. for (i = 0; i < fdst->quant_table_count; i++) {
  836. fdst->initial_states[i] = av_malloc(fdst->context_count[i] * sizeof(*fdst->initial_states[i]));
  837. memcpy(fdst->initial_states[i], fsrc->initial_states[i], fdst->context_count[i] * sizeof(*fdst->initial_states[i]));
  838. }
  839. fdst->picture.f = av_frame_alloc();
  840. fdst->last_picture.f = av_frame_alloc();
  841. if ((ret = ffv1_init_slice_contexts(fdst)) < 0)
  842. return ret;
  843. }
  844. av_assert1(fdst->slice_count == fsrc->slice_count);
  845. fdst->key_frame_ok = fsrc->key_frame_ok;
  846. ff_thread_release_buffer(dst, &fdst->picture);
  847. if (fsrc->picture.f->data[0]) {
  848. if ((ret = ff_thread_ref_frame(&fdst->picture, &fsrc->picture)) < 0)
  849. return ret;
  850. }
  851. for (i = 0; i < fdst->slice_count; i++) {
  852. FFV1Context *fsdst = fdst->slice_context[i];
  853. FFV1Context *fssrc = fsrc->slice_context[i];
  854. fsdst->slice_damaged = fssrc->slice_damaged;
  855. }
  856. fdst->fsrc = fsrc;
  857. return 0;
  858. }
  859. AVCodec ff_ffv1_decoder = {
  860. .name = "ffv1",
  861. .type = AVMEDIA_TYPE_VIDEO,
  862. .id = AV_CODEC_ID_FFV1,
  863. .priv_data_size = sizeof(FFV1Context),
  864. .init = decode_init,
  865. .close = ffv1_close,
  866. .decode = decode_frame,
  867. .init_thread_copy = ONLY_IF_THREADS_ENABLED(init_thread_copy),
  868. .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
  869. .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/ |
  870. CODEC_CAP_FRAME_THREADS | CODEC_CAP_SLICE_THREADS,
  871. .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
  872. };