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.

978 lines
33KB

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