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.

992 lines
34KB

  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. 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->minor_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. c->bytestream_end -= 4;
  413. f->minor_version = get_symbol(c, state, 0);
  414. }
  415. f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
  416. if (f->ac > 1) {
  417. for (i = 1; i < 256; i++)
  418. f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
  419. }
  420. f->colorspace = get_symbol(c, state, 0); //YUV cs type
  421. f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
  422. f->chroma_planes = get_rac(c, state);
  423. f->chroma_h_shift = get_symbol(c, state, 0);
  424. f->chroma_v_shift = get_symbol(c, state, 0);
  425. f->transparency = get_rac(c, state);
  426. f->plane_count = 1 + (f->chroma_planes || f->version<4) + f->transparency;
  427. f->num_h_slices = 1 + get_symbol(c, state, 0);
  428. f->num_v_slices = 1 + get_symbol(c, state, 0);
  429. if (f->num_h_slices > (unsigned)f->width || !f->num_h_slices ||
  430. f->num_v_slices > (unsigned)f->height || !f->num_v_slices
  431. ) {
  432. av_log(f->avctx, AV_LOG_ERROR, "slice count invalid\n");
  433. return AVERROR_INVALIDDATA;
  434. }
  435. f->quant_table_count = get_symbol(c, state, 0);
  436. if (f->quant_table_count > (unsigned)MAX_QUANT_TABLES)
  437. return AVERROR_INVALIDDATA;
  438. for (i = 0; i < f->quant_table_count; i++) {
  439. f->context_count[i] = read_quant_tables(c, f->quant_tables[i]);
  440. if (f->context_count[i] < 0) {
  441. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  442. return AVERROR_INVALIDDATA;
  443. }
  444. }
  445. if ((ret = ffv1_allocate_initial_states(f)) < 0)
  446. return ret;
  447. for (i = 0; i < f->quant_table_count; i++)
  448. if (get_rac(c, state)) {
  449. for (j = 0; j < f->context_count[i]; j++)
  450. for (k = 0; k < CONTEXT_SIZE; k++) {
  451. int pred = j ? f->initial_states[i][j - 1][k] : 128;
  452. f->initial_states[i][j][k] =
  453. (pred + get_symbol(c, state2[k], 1)) & 0xFF;
  454. }
  455. }
  456. if (f->version > 2) {
  457. f->ec = get_symbol(c, state, 0);
  458. if (f->minor_version > 2)
  459. f->intra = get_symbol(c, state, 0);
  460. }
  461. if (f->version > 2) {
  462. unsigned v;
  463. v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0,
  464. f->avctx->extradata, f->avctx->extradata_size);
  465. if (v) {
  466. av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", v);
  467. return AVERROR_INVALIDDATA;
  468. }
  469. }
  470. if (f->avctx->debug & FF_DEBUG_PICT_INFO)
  471. av_log(f->avctx, AV_LOG_DEBUG,
  472. "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",
  473. f->version, f->minor_version,
  474. f->ac,
  475. f->colorspace,
  476. f->avctx->bits_per_raw_sample,
  477. f->chroma_planes, f->chroma_h_shift, f->chroma_v_shift,
  478. f->transparency,
  479. f->num_h_slices, f->num_v_slices,
  480. f->quant_table_count,
  481. f->ec,
  482. f->intra
  483. );
  484. return 0;
  485. }
  486. static int read_header(FFV1Context *f)
  487. {
  488. uint8_t state[CONTEXT_SIZE];
  489. int i, j, context_count = -1; //-1 to avoid warning
  490. RangeCoder *const c = &f->slice_context[0]->c;
  491. memset(state, 128, sizeof(state));
  492. if (f->version < 2) {
  493. unsigned v= get_symbol(c, state, 0);
  494. if (v >= 2) {
  495. av_log(f->avctx, AV_LOG_ERROR, "invalid version %d in ver01 header\n", v);
  496. return AVERROR_INVALIDDATA;
  497. }
  498. f->version = v;
  499. f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
  500. if (f->ac > 1) {
  501. for (i = 1; i < 256; i++)
  502. f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
  503. }
  504. f->colorspace = get_symbol(c, state, 0); //YUV cs type
  505. if (f->version > 0)
  506. f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
  507. f->chroma_planes = get_rac(c, state);
  508. f->chroma_h_shift = get_symbol(c, state, 0);
  509. f->chroma_v_shift = get_symbol(c, state, 0);
  510. f->transparency = get_rac(c, state);
  511. f->plane_count = 2 + f->transparency;
  512. }
  513. if (f->colorspace == 0) {
  514. if (!f->transparency && !f->chroma_planes) {
  515. if (f->avctx->bits_per_raw_sample <= 8)
  516. f->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  517. else
  518. f->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
  519. } else if (f->avctx->bits_per_raw_sample<=8 && !f->transparency) {
  520. switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
  521. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P; break;
  522. case 0x01: f->avctx->pix_fmt = AV_PIX_FMT_YUV440P; break;
  523. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P; break;
  524. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P; break;
  525. case 0x20: f->avctx->pix_fmt = AV_PIX_FMT_YUV411P; break;
  526. case 0x22: f->avctx->pix_fmt = AV_PIX_FMT_YUV410P; break;
  527. default:
  528. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  529. return AVERROR(ENOSYS);
  530. }
  531. } else if (f->avctx->bits_per_raw_sample <= 8 && f->transparency) {
  532. switch(16*f->chroma_h_shift + f->chroma_v_shift) {
  533. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P; break;
  534. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P; break;
  535. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P; break;
  536. default:
  537. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  538. return AVERROR(ENOSYS);
  539. }
  540. } else if (f->avctx->bits_per_raw_sample == 9) {
  541. f->packed_at_lsb = 1;
  542. switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
  543. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P9; break;
  544. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P9; break;
  545. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P9; break;
  546. default:
  547. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  548. return AVERROR(ENOSYS);
  549. }
  550. } else if (f->avctx->bits_per_raw_sample == 10) {
  551. f->packed_at_lsb = 1;
  552. switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
  553. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P10; break;
  554. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P10; break;
  555. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P10; break;
  556. default:
  557. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  558. return AVERROR(ENOSYS);
  559. }
  560. } else {
  561. switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
  562. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P16; break;
  563. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P16; break;
  564. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P16; break;
  565. default:
  566. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  567. return AVERROR(ENOSYS);
  568. }
  569. }
  570. } else if (f->colorspace == 1) {
  571. if (f->chroma_h_shift || f->chroma_v_shift) {
  572. av_log(f->avctx, AV_LOG_ERROR,
  573. "chroma subsampling not supported in this colorspace\n");
  574. return AVERROR(ENOSYS);
  575. }
  576. if ( f->avctx->bits_per_raw_sample == 9)
  577. f->avctx->pix_fmt = AV_PIX_FMT_GBRP9;
  578. else if (f->avctx->bits_per_raw_sample == 10)
  579. f->avctx->pix_fmt = AV_PIX_FMT_GBRP10;
  580. else if (f->avctx->bits_per_raw_sample == 12)
  581. f->avctx->pix_fmt = AV_PIX_FMT_GBRP12;
  582. else if (f->avctx->bits_per_raw_sample == 14)
  583. f->avctx->pix_fmt = AV_PIX_FMT_GBRP14;
  584. else
  585. if (f->transparency) f->avctx->pix_fmt = AV_PIX_FMT_RGB32;
  586. else f->avctx->pix_fmt = AV_PIX_FMT_0RGB32;
  587. } else {
  588. av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
  589. return AVERROR(ENOSYS);
  590. }
  591. av_dlog(f->avctx, "%d %d %d\n",
  592. f->chroma_h_shift, f->chroma_v_shift, f->avctx->pix_fmt);
  593. if (f->version < 2) {
  594. context_count = read_quant_tables(c, f->quant_table);
  595. if (context_count < 0) {
  596. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  597. return AVERROR_INVALIDDATA;
  598. }
  599. } else if (f->version < 3) {
  600. f->slice_count = get_symbol(c, state, 0);
  601. } else {
  602. const uint8_t *p = c->bytestream_end;
  603. for (f->slice_count = 0;
  604. f->slice_count < MAX_SLICES && 3 < p - c->bytestream_start;
  605. f->slice_count++) {
  606. int trailer = 3 + 5*!!f->ec;
  607. int size = AV_RB24(p-trailer);
  608. if (size + trailer > p - c->bytestream_start)
  609. break;
  610. p -= size + trailer;
  611. }
  612. }
  613. if (f->slice_count > (unsigned)MAX_SLICES || f->slice_count <= 0) {
  614. av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid\n", f->slice_count);
  615. return AVERROR_INVALIDDATA;
  616. }
  617. for (j = 0; j < f->slice_count; j++) {
  618. FFV1Context *fs = f->slice_context[j];
  619. fs->ac = f->ac;
  620. fs->packed_at_lsb = f->packed_at_lsb;
  621. fs->slice_damaged = 0;
  622. if (f->version == 2) {
  623. fs->slice_x = get_symbol(c, state, 0) * f->width ;
  624. fs->slice_y = get_symbol(c, state, 0) * f->height;
  625. fs->slice_width = (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
  626. fs->slice_height = (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
  627. fs->slice_x /= f->num_h_slices;
  628. fs->slice_y /= f->num_v_slices;
  629. fs->slice_width = fs->slice_width / f->num_h_slices - fs->slice_x;
  630. fs->slice_height = fs->slice_height / f->num_v_slices - fs->slice_y;
  631. if ((unsigned)fs->slice_width > f->width ||
  632. (unsigned)fs->slice_height > f->height)
  633. return AVERROR_INVALIDDATA;
  634. if ( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
  635. || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
  636. return AVERROR_INVALIDDATA;
  637. }
  638. for (i = 0; i < f->plane_count; i++) {
  639. PlaneContext *const p = &fs->plane[i];
  640. if (f->version == 2) {
  641. int idx = get_symbol(c, state, 0);
  642. if (idx > (unsigned)f->quant_table_count) {
  643. av_log(f->avctx, AV_LOG_ERROR,
  644. "quant_table_index out of range\n");
  645. return AVERROR_INVALIDDATA;
  646. }
  647. p->quant_table_index = idx;
  648. memcpy(p->quant_table, f->quant_tables[idx],
  649. sizeof(p->quant_table));
  650. context_count = f->context_count[idx];
  651. } else {
  652. memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
  653. }
  654. if (f->version <= 2) {
  655. av_assert0(context_count >= 0);
  656. if (p->context_count < context_count) {
  657. av_freep(&p->state);
  658. av_freep(&p->vlc_state);
  659. }
  660. p->context_count = context_count;
  661. }
  662. }
  663. }
  664. return 0;
  665. }
  666. static av_cold int decode_init(AVCodecContext *avctx)
  667. {
  668. FFV1Context *f = avctx->priv_data;
  669. int ret;
  670. if ((ret = ffv1_common_init(avctx)) < 0)
  671. return ret;
  672. if (avctx->extradata && (ret = read_extra_header(f)) < 0)
  673. return ret;
  674. if ((ret = ffv1_init_slice_contexts(f)) < 0)
  675. return ret;
  676. avctx->internal->allocate_progress = 1;
  677. return 0;
  678. }
  679. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
  680. {
  681. const uint8_t *buf = avpkt->data;
  682. int buf_size = avpkt->size;
  683. FFV1Context *f = avctx->priv_data;
  684. RangeCoder *const c = &f->slice_context[0]->c;
  685. int i, ret;
  686. uint8_t keystate = 128;
  687. const uint8_t *buf_p;
  688. AVFrame *p;
  689. if (f->last_picture.f)
  690. ff_thread_release_buffer(avctx, &f->last_picture);
  691. FFSWAP(ThreadFrame, f->picture, f->last_picture);
  692. f->cur = p = f->picture.f;
  693. if (f->version < 3 && avctx->field_order > AV_FIELD_PROGRESSIVE) {
  694. /* we have interlaced material flagged in container */
  695. p->interlaced_frame = 1;
  696. if (avctx->field_order == AV_FIELD_TT || avctx->field_order == AV_FIELD_TB)
  697. p->top_field_first = 1;
  698. }
  699. f->avctx = avctx;
  700. ff_init_range_decoder(c, buf, buf_size);
  701. ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
  702. p->pict_type = AV_PICTURE_TYPE_I; //FIXME I vs. P
  703. if (get_rac(c, &keystate)) {
  704. p->key_frame = 1;
  705. f->key_frame_ok = 0;
  706. if ((ret = read_header(f)) < 0)
  707. return ret;
  708. f->key_frame_ok = 1;
  709. } else {
  710. if (!f->key_frame_ok) {
  711. av_log(avctx, AV_LOG_ERROR,
  712. "Cannot decode non-keyframe without valid keyframe\n");
  713. return AVERROR_INVALIDDATA;
  714. }
  715. p->key_frame = 0;
  716. }
  717. if ((ret = ff_thread_get_buffer(avctx, &f->picture, AV_GET_BUFFER_FLAG_REF)) < 0)
  718. return ret;
  719. if (avctx->debug & FF_DEBUG_PICT_INFO)
  720. av_log(avctx, AV_LOG_DEBUG, "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
  721. f->version, p->key_frame, f->ac, f->ec, f->slice_count, f->avctx->bits_per_raw_sample);
  722. ff_thread_finish_setup(avctx);
  723. buf_p = buf + buf_size;
  724. for (i = f->slice_count - 1; i >= 0; i--) {
  725. FFV1Context *fs = f->slice_context[i];
  726. int trailer = 3 + 5*!!f->ec;
  727. int v;
  728. if (i || f->version > 2) v = AV_RB24(buf_p-trailer) + trailer;
  729. else v = buf_p - c->bytestream_start;
  730. if (buf_p - c->bytestream_start < v) {
  731. av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
  732. return AVERROR_INVALIDDATA;
  733. }
  734. buf_p -= v;
  735. if (f->ec) {
  736. unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, v);
  737. if (crc) {
  738. int64_t ts = avpkt->pts != AV_NOPTS_VALUE ? avpkt->pts : avpkt->dts;
  739. av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!", crc);
  740. if (ts != AV_NOPTS_VALUE && avctx->pkt_timebase.num) {
  741. av_log(f->avctx, AV_LOG_ERROR, "at %f seconds\n", ts*av_q2d(avctx->pkt_timebase));
  742. } else if (ts != AV_NOPTS_VALUE) {
  743. av_log(f->avctx, AV_LOG_ERROR, "at %"PRId64"\n", ts);
  744. } else {
  745. av_log(f->avctx, AV_LOG_ERROR, "\n");
  746. }
  747. fs->slice_damaged = 1;
  748. }
  749. }
  750. if (i) {
  751. ff_init_range_decoder(&fs->c, buf_p, v);
  752. } else
  753. fs->c.bytestream_end = (uint8_t *)(buf_p + v);
  754. fs->avctx = avctx;
  755. fs->cur = p;
  756. }
  757. avctx->execute(avctx,
  758. decode_slice,
  759. &f->slice_context[0],
  760. NULL,
  761. f->slice_count,
  762. sizeof(void*));
  763. for (i = f->slice_count - 1; i >= 0; i--) {
  764. FFV1Context *fs = f->slice_context[i];
  765. int j;
  766. if (fs->slice_damaged && f->last_picture.f->data[0]) {
  767. const uint8_t *src[4];
  768. uint8_t *dst[4];
  769. ff_thread_await_progress(&f->last_picture, INT_MAX, 0);
  770. for (j = 0; j < 4; j++) {
  771. int sh = (j==1 || j==2) ? f->chroma_h_shift : 0;
  772. int sv = (j==1 || j==2) ? f->chroma_v_shift : 0;
  773. dst[j] = p->data[j] + p->linesize[j]*
  774. (fs->slice_y>>sv) + (fs->slice_x>>sh);
  775. src[j] = f->last_picture.f->data[j] + f->last_picture.f->linesize[j]*
  776. (fs->slice_y>>sv) + (fs->slice_x>>sh);
  777. }
  778. av_image_copy(dst, p->linesize, (const uint8_t **)src,
  779. f->last_picture.f->linesize,
  780. avctx->pix_fmt,
  781. fs->slice_width,
  782. fs->slice_height);
  783. }
  784. }
  785. ff_thread_report_progress(&f->picture, INT_MAX, 0);
  786. f->picture_number++;
  787. if (f->last_picture.f)
  788. ff_thread_release_buffer(avctx, &f->last_picture);
  789. f->cur = NULL;
  790. if ((ret = av_frame_ref(data, f->picture.f)) < 0)
  791. return ret;
  792. *got_frame = 1;
  793. return buf_size;
  794. }
  795. static int init_thread_copy(AVCodecContext *avctx)
  796. {
  797. FFV1Context *f = avctx->priv_data;
  798. f->picture.f = NULL;
  799. f->last_picture.f = NULL;
  800. f->sample_buffer = NULL;
  801. f->quant_table_count = 0;
  802. f->slice_count = 0;
  803. return 0;
  804. }
  805. static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
  806. {
  807. FFV1Context *fsrc = src->priv_data;
  808. FFV1Context *fdst = dst->priv_data;
  809. int i, ret;
  810. if (dst == src)
  811. return 0;
  812. if (!fdst->picture.f) {
  813. memcpy(fdst, fsrc, sizeof(*fdst));
  814. for (i = 0; i < fdst->quant_table_count; i++) {
  815. fdst->initial_states[i] = av_malloc(fdst->context_count[i] * sizeof(*fdst->initial_states[i]));
  816. memcpy(fdst->initial_states[i], fsrc->initial_states[i], fdst->context_count[i] * sizeof(*fdst->initial_states[i]));
  817. }
  818. fdst->picture.f = av_frame_alloc();
  819. fdst->last_picture.f = av_frame_alloc();
  820. if ((ret = ffv1_init_slice_contexts(fdst)) < 0)
  821. return ret;
  822. }
  823. av_assert1(fdst->slice_count == fsrc->slice_count);
  824. fdst->key_frame_ok = fsrc->key_frame_ok;
  825. ff_thread_release_buffer(dst, &fdst->picture);
  826. if (fsrc->picture.f->data[0]) {
  827. if ((ret = ff_thread_ref_frame(&fdst->picture, &fsrc->picture)) < 0)
  828. return ret;
  829. }
  830. for (i = 0; i < fdst->slice_count; i++) {
  831. FFV1Context *fsdst = fdst->slice_context[i];
  832. FFV1Context *fssrc = fsrc->slice_context[i];
  833. fsdst->slice_damaged = fssrc->slice_damaged;
  834. }
  835. fdst->fsrc = fsrc;
  836. return 0;
  837. }
  838. AVCodec ff_ffv1_decoder = {
  839. .name = "ffv1",
  840. .type = AVMEDIA_TYPE_VIDEO,
  841. .id = AV_CODEC_ID_FFV1,
  842. .priv_data_size = sizeof(FFV1Context),
  843. .init = decode_init,
  844. .close = ffv1_close,
  845. .decode = decode_frame,
  846. .init_thread_copy = ONLY_IF_THREADS_ENABLED(init_thread_copy),
  847. .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
  848. .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/ |
  849. CODEC_CAP_FRAME_THREADS | CODEC_CAP_SLICE_THREADS,
  850. .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
  851. };