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.

1049 lines
39KB

  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. if (e > 31)
  50. return AVERROR_INVALIDDATA;
  51. }
  52. a = 1;
  53. for (i = e - 1; i >= 0; i--)
  54. a += a + get_rac(c, state + 22 + FFMIN(i, 9)); // 22..31
  55. e = -(is_signed && get_rac(c, state + 11 + FFMIN(e, 10))); // 11..21
  56. return (a ^ e) - e;
  57. }
  58. }
  59. static av_noinline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed)
  60. {
  61. return get_symbol_inline(c, state, is_signed);
  62. }
  63. static inline int get_vlc_symbol(GetBitContext *gb, VlcState *const state,
  64. int bits)
  65. {
  66. int k, i, v, ret;
  67. i = state->count;
  68. k = 0;
  69. while (i < state->error_sum) { // FIXME: optimize
  70. k++;
  71. i += i;
  72. }
  73. v = get_sr_golomb(gb, k, 12, bits);
  74. ff_dlog(NULL, "v:%d bias:%d error:%d drift:%d count:%d k:%d",
  75. v, state->bias, state->error_sum, state->drift, state->count, k);
  76. v ^= ((2 * state->drift + state->count) >> 31);
  77. ret = fold(v + state->bias, bits);
  78. update_vlc_state(state, v);
  79. return ret;
  80. }
  81. #define TYPE int16_t
  82. #define RENAME(name) name
  83. #include "ffv1dec_template.c"
  84. #undef TYPE
  85. #undef RENAME
  86. #define TYPE int32_t
  87. #define RENAME(name) name ## 32
  88. #include "ffv1dec_template.c"
  89. static void decode_plane(FFV1Context *s, uint8_t *src,
  90. int w, int h, int stride, int plane_index,
  91. int pixel_stride)
  92. {
  93. int x, y;
  94. int16_t *sample[2];
  95. sample[0] = s->sample_buffer + 3;
  96. sample[1] = s->sample_buffer + w + 6 + 3;
  97. s->run_index = 0;
  98. memset(s->sample_buffer, 0, 2 * (w + 6) * sizeof(*s->sample_buffer));
  99. for (y = 0; y < h; y++) {
  100. int16_t *temp = sample[0]; // FIXME: try a normal buffer
  101. sample[0] = sample[1];
  102. sample[1] = temp;
  103. sample[1][-1] = sample[0][0];
  104. sample[0][w] = sample[0][w - 1];
  105. // { START_TIMER
  106. if (s->avctx->bits_per_raw_sample <= 8) {
  107. decode_line(s, w, sample, plane_index, 8);
  108. for (x = 0; x < w; x++)
  109. src[x*pixel_stride + stride * y] = sample[1][x];
  110. } else {
  111. decode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
  112. if (s->packed_at_lsb) {
  113. for (x = 0; x < w; x++) {
  114. ((uint16_t*)(src + stride*y))[x*pixel_stride] = sample[1][x];
  115. }
  116. } else {
  117. for (x = 0; x < w; x++) {
  118. ((uint16_t*)(src + stride*y))[x*pixel_stride] = sample[1][x] << (16 - s->avctx->bits_per_raw_sample) | ((uint16_t **)sample)[1][x] >> (2 * s->avctx->bits_per_raw_sample - 16);
  119. }
  120. }
  121. }
  122. // STOP_TIMER("decode-line") }
  123. }
  124. }
  125. static int decode_slice_header(FFV1Context *f, FFV1Context *fs)
  126. {
  127. RangeCoder *c = &fs->c;
  128. uint8_t state[CONTEXT_SIZE];
  129. unsigned ps, i, context_count;
  130. memset(state, 128, sizeof(state));
  131. av_assert0(f->version > 2);
  132. fs->slice_x = get_symbol(c, state, 0) * f->width ;
  133. fs->slice_y = get_symbol(c, state, 0) * f->height;
  134. fs->slice_width = (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
  135. fs->slice_height = (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
  136. fs->slice_x /= f->num_h_slices;
  137. fs->slice_y /= f->num_v_slices;
  138. fs->slice_width = fs->slice_width /f->num_h_slices - fs->slice_x;
  139. fs->slice_height = fs->slice_height/f->num_v_slices - fs->slice_y;
  140. if ((unsigned)fs->slice_width > f->width || (unsigned)fs->slice_height > f->height)
  141. return -1;
  142. if ( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
  143. || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
  144. return -1;
  145. for (i = 0; i < f->plane_count; i++) {
  146. PlaneContext * const p = &fs->plane[i];
  147. int idx = get_symbol(c, state, 0);
  148. if (idx >= (unsigned)f->quant_table_count) {
  149. av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
  150. return -1;
  151. }
  152. p->quant_table_index = idx;
  153. memcpy(p->quant_table, f->quant_tables[idx], sizeof(p->quant_table));
  154. context_count = f->context_count[idx];
  155. if (p->context_count < context_count) {
  156. av_freep(&p->state);
  157. av_freep(&p->vlc_state);
  158. }
  159. p->context_count = context_count;
  160. }
  161. ps = get_symbol(c, state, 0);
  162. if (ps == 1) {
  163. f->cur->interlaced_frame = 1;
  164. f->cur->top_field_first = 1;
  165. } else if (ps == 2) {
  166. f->cur->interlaced_frame = 1;
  167. f->cur->top_field_first = 0;
  168. } else if (ps == 3) {
  169. f->cur->interlaced_frame = 0;
  170. }
  171. f->cur->sample_aspect_ratio.num = get_symbol(c, state, 0);
  172. f->cur->sample_aspect_ratio.den = get_symbol(c, state, 0);
  173. if (av_image_check_sar(f->width, f->height,
  174. f->cur->sample_aspect_ratio) < 0) {
  175. av_log(f->avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  176. f->cur->sample_aspect_ratio.num,
  177. f->cur->sample_aspect_ratio.den);
  178. f->cur->sample_aspect_ratio = (AVRational){ 0, 1 };
  179. }
  180. if (fs->version > 3) {
  181. fs->slice_reset_contexts = get_rac(c, state);
  182. fs->slice_coding_mode = get_symbol(c, state, 0);
  183. if (fs->slice_coding_mode != 1) {
  184. fs->slice_rct_by_coef = get_symbol(c, state, 0);
  185. fs->slice_rct_ry_coef = get_symbol(c, state, 0);
  186. if ((uint64_t)fs->slice_rct_by_coef + (uint64_t)fs->slice_rct_ry_coef > 4) {
  187. av_log(f->avctx, AV_LOG_ERROR, "slice_rct_y_coef out of range\n");
  188. return AVERROR_INVALIDDATA;
  189. }
  190. }
  191. }
  192. return 0;
  193. }
  194. static int decode_slice(AVCodecContext *c, void *arg)
  195. {
  196. FFV1Context *fs = *(void **)arg;
  197. FFV1Context *f = fs->avctx->priv_data;
  198. int width, height, x, y, ret;
  199. const int ps = av_pix_fmt_desc_get(c->pix_fmt)->comp[0].step;
  200. AVFrame * const p = f->cur;
  201. int i, si;
  202. for( si=0; fs != f->slice_context[si]; si ++)
  203. ;
  204. if(f->fsrc && !p->key_frame)
  205. ff_thread_await_progress(&f->last_picture, si, 0);
  206. if(f->fsrc && !p->key_frame) {
  207. FFV1Context *fssrc = f->fsrc->slice_context[si];
  208. FFV1Context *fsdst = f->slice_context[si];
  209. av_assert1(fsdst->plane_count == fssrc->plane_count);
  210. av_assert1(fsdst == fs);
  211. if (!p->key_frame)
  212. fsdst->slice_damaged |= fssrc->slice_damaged;
  213. for (i = 0; i < f->plane_count; i++) {
  214. PlaneContext *psrc = &fssrc->plane[i];
  215. PlaneContext *pdst = &fsdst->plane[i];
  216. av_free(pdst->state);
  217. av_free(pdst->vlc_state);
  218. memcpy(pdst, psrc, sizeof(*pdst));
  219. pdst->state = NULL;
  220. pdst->vlc_state = NULL;
  221. if (fssrc->ac) {
  222. pdst->state = av_malloc_array(CONTEXT_SIZE, psrc->context_count);
  223. memcpy(pdst->state, psrc->state, CONTEXT_SIZE * psrc->context_count);
  224. } else {
  225. pdst->vlc_state = av_malloc_array(sizeof(*pdst->vlc_state), psrc->context_count);
  226. memcpy(pdst->vlc_state, psrc->vlc_state, sizeof(*pdst->vlc_state) * psrc->context_count);
  227. }
  228. }
  229. }
  230. fs->slice_rct_by_coef = 1;
  231. fs->slice_rct_ry_coef = 1;
  232. if (f->version > 2) {
  233. if (ff_ffv1_init_slice_state(f, fs) < 0)
  234. return AVERROR(ENOMEM);
  235. if (decode_slice_header(f, fs) < 0) {
  236. fs->slice_x = fs->slice_y = fs->slice_height = fs->slice_width = 0;
  237. fs->slice_damaged = 1;
  238. return AVERROR_INVALIDDATA;
  239. }
  240. }
  241. if ((ret = ff_ffv1_init_slice_state(f, fs)) < 0)
  242. return ret;
  243. if (f->cur->key_frame || fs->slice_reset_contexts)
  244. ff_ffv1_clear_slice_state(f, fs);
  245. width = fs->slice_width;
  246. height = fs->slice_height;
  247. x = fs->slice_x;
  248. y = fs->slice_y;
  249. if (fs->ac == AC_GOLOMB_RICE) {
  250. if (f->version == 3 && f->micro_version > 1 || f->version > 3)
  251. get_rac(&fs->c, (uint8_t[]) { 129 });
  252. fs->ac_byte_count = f->version > 2 || (!x && !y) ? fs->c.bytestream - fs->c.bytestream_start - 1 : 0;
  253. init_get_bits(&fs->gb,
  254. fs->c.bytestream_start + fs->ac_byte_count,
  255. (fs->c.bytestream_end - fs->c.bytestream_start - fs->ac_byte_count) * 8);
  256. }
  257. av_assert1(width && height);
  258. if (f->colorspace == 0 && (f->chroma_planes || !fs->transparency)) {
  259. const int chroma_width = AV_CEIL_RSHIFT(width, f->chroma_h_shift);
  260. const int chroma_height = AV_CEIL_RSHIFT(height, f->chroma_v_shift);
  261. const int cx = x >> f->chroma_h_shift;
  262. const int cy = y >> f->chroma_v_shift;
  263. decode_plane(fs, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 1);
  264. if (f->chroma_planes) {
  265. decode_plane(fs, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1, 1);
  266. decode_plane(fs, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1, 1);
  267. }
  268. if (fs->transparency)
  269. decode_plane(fs, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], (f->version >= 4 && !f->chroma_planes) ? 1 : 2, 1);
  270. } else if (f->colorspace == 0) {
  271. decode_plane(fs, p->data[0] + ps*x + y*p->linesize[0] , width, height, p->linesize[0], 0, 2);
  272. decode_plane(fs, p->data[0] + ps*x + y*p->linesize[0] + 1, width, height, p->linesize[0], 1, 2);
  273. } else if (f->use32bit) {
  274. uint8_t *planes[3] = { p->data[0] + ps * x + y * p->linesize[0],
  275. p->data[1] + ps * x + y * p->linesize[1],
  276. p->data[2] + ps * x + y * p->linesize[2] };
  277. decode_rgb_frame32(fs, planes, width, height, p->linesize);
  278. } else {
  279. uint8_t *planes[3] = { p->data[0] + ps * x + y * p->linesize[0],
  280. p->data[1] + ps * x + y * p->linesize[1],
  281. p->data[2] + ps * x + y * p->linesize[2] };
  282. decode_rgb_frame(fs, planes, width, height, p->linesize);
  283. }
  284. if (fs->ac != AC_GOLOMB_RICE && f->version > 2) {
  285. int v;
  286. get_rac(&fs->c, (uint8_t[]) { 129 });
  287. v = fs->c.bytestream_end - fs->c.bytestream - 2 - 5*f->ec;
  288. if (v) {
  289. av_log(f->avctx, AV_LOG_ERROR, "bytestream end mismatching by %d\n", v);
  290. fs->slice_damaged = 1;
  291. }
  292. }
  293. emms_c();
  294. ff_thread_report_progress(&f->picture, si, 0);
  295. return 0;
  296. }
  297. static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale)
  298. {
  299. int v;
  300. int i = 0;
  301. uint8_t state[CONTEXT_SIZE];
  302. memset(state, 128, sizeof(state));
  303. for (v = 0; i < 128; v++) {
  304. unsigned len = get_symbol(c, state, 0) + 1;
  305. if (len > 128 - i || !len)
  306. return AVERROR_INVALIDDATA;
  307. while (len--) {
  308. quant_table[i] = scale * v;
  309. i++;
  310. }
  311. }
  312. for (i = 1; i < 128; i++)
  313. quant_table[256 - i] = -quant_table[i];
  314. quant_table[128] = -quant_table[127];
  315. return 2 * v - 1;
  316. }
  317. static int read_quant_tables(RangeCoder *c,
  318. int16_t quant_table[MAX_CONTEXT_INPUTS][256])
  319. {
  320. int i;
  321. int context_count = 1;
  322. for (i = 0; i < 5; i++) {
  323. int ret = read_quant_table(c, quant_table[i], context_count);
  324. if (ret < 0)
  325. return ret;
  326. context_count *= ret;
  327. if (context_count > 32768U) {
  328. return AVERROR_INVALIDDATA;
  329. }
  330. }
  331. return (context_count + 1) / 2;
  332. }
  333. static int read_extra_header(FFV1Context *f)
  334. {
  335. RangeCoder *const c = &f->c;
  336. uint8_t state[CONTEXT_SIZE];
  337. int i, j, k, ret;
  338. uint8_t state2[32][CONTEXT_SIZE];
  339. unsigned crc = 0;
  340. memset(state2, 128, sizeof(state2));
  341. memset(state, 128, sizeof(state));
  342. ff_init_range_decoder(c, f->avctx->extradata, f->avctx->extradata_size);
  343. ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
  344. f->version = get_symbol(c, state, 0);
  345. if (f->version < 2) {
  346. av_log(f->avctx, AV_LOG_ERROR, "Invalid version in global header\n");
  347. return AVERROR_INVALIDDATA;
  348. }
  349. if (f->version > 2) {
  350. c->bytestream_end -= 4;
  351. f->micro_version = get_symbol(c, state, 0);
  352. if (f->micro_version < 0)
  353. return AVERROR_INVALIDDATA;
  354. }
  355. f->ac = get_symbol(c, state, 0);
  356. if (f->ac == AC_RANGE_CUSTOM_TAB) {
  357. for (i = 1; i < 256; i++)
  358. f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
  359. }
  360. f->colorspace = get_symbol(c, state, 0); //YUV cs type
  361. f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
  362. f->chroma_planes = get_rac(c, state);
  363. f->chroma_h_shift = get_symbol(c, state, 0);
  364. f->chroma_v_shift = get_symbol(c, state, 0);
  365. f->transparency = get_rac(c, state);
  366. f->plane_count = 1 + (f->chroma_planes || f->version<4) + f->transparency;
  367. f->num_h_slices = 1 + get_symbol(c, state, 0);
  368. f->num_v_slices = 1 + get_symbol(c, state, 0);
  369. if (f->chroma_h_shift > 4U || f->chroma_v_shift > 4U) {
  370. av_log(f->avctx, AV_LOG_ERROR, "chroma shift parameters %d %d are invalid\n",
  371. f->chroma_h_shift, f->chroma_v_shift);
  372. return AVERROR_INVALIDDATA;
  373. }
  374. if (f->num_h_slices > (unsigned)f->width || !f->num_h_slices ||
  375. f->num_v_slices > (unsigned)f->height || !f->num_v_slices
  376. ) {
  377. av_log(f->avctx, AV_LOG_ERROR, "slice count invalid\n");
  378. return AVERROR_INVALIDDATA;
  379. }
  380. f->quant_table_count = get_symbol(c, state, 0);
  381. if (f->quant_table_count > (unsigned)MAX_QUANT_TABLES || !f->quant_table_count) {
  382. av_log(f->avctx, AV_LOG_ERROR, "quant table count %d is invalid\n", f->quant_table_count);
  383. f->quant_table_count = 0;
  384. return AVERROR_INVALIDDATA;
  385. }
  386. for (i = 0; i < f->quant_table_count; i++) {
  387. f->context_count[i] = read_quant_tables(c, f->quant_tables[i]);
  388. if (f->context_count[i] < 0) {
  389. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  390. return AVERROR_INVALIDDATA;
  391. }
  392. }
  393. if ((ret = ff_ffv1_allocate_initial_states(f)) < 0)
  394. return ret;
  395. for (i = 0; i < f->quant_table_count; i++)
  396. if (get_rac(c, state)) {
  397. for (j = 0; j < f->context_count[i]; j++)
  398. for (k = 0; k < CONTEXT_SIZE; k++) {
  399. int pred = j ? f->initial_states[i][j - 1][k] : 128;
  400. f->initial_states[i][j][k] =
  401. (pred + get_symbol(c, state2[k], 1)) & 0xFF;
  402. }
  403. }
  404. if (f->version > 2) {
  405. f->ec = get_symbol(c, state, 0);
  406. if (f->micro_version > 2)
  407. f->intra = get_symbol(c, state, 0);
  408. }
  409. if (f->version > 2) {
  410. unsigned v;
  411. v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0,
  412. f->avctx->extradata, f->avctx->extradata_size);
  413. if (v || f->avctx->extradata_size < 4) {
  414. av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", v);
  415. return AVERROR_INVALIDDATA;
  416. }
  417. crc = AV_RB32(f->avctx->extradata + f->avctx->extradata_size - 4);
  418. }
  419. if (f->avctx->debug & FF_DEBUG_PICT_INFO)
  420. av_log(f->avctx, AV_LOG_DEBUG,
  421. "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 CRC:0x%08X\n",
  422. f->version, f->micro_version,
  423. f->ac,
  424. f->colorspace,
  425. f->avctx->bits_per_raw_sample,
  426. f->chroma_planes, f->chroma_h_shift, f->chroma_v_shift,
  427. f->transparency,
  428. f->num_h_slices, f->num_v_slices,
  429. f->quant_table_count,
  430. f->ec,
  431. f->intra,
  432. crc
  433. );
  434. return 0;
  435. }
  436. static int read_header(FFV1Context *f)
  437. {
  438. uint8_t state[CONTEXT_SIZE];
  439. int i, j, context_count = -1; //-1 to avoid warning
  440. RangeCoder *const c = &f->slice_context[0]->c;
  441. memset(state, 128, sizeof(state));
  442. if (f->version < 2) {
  443. int chroma_planes, chroma_h_shift, chroma_v_shift, transparency, colorspace, bits_per_raw_sample;
  444. unsigned v= get_symbol(c, state, 0);
  445. if (v >= 2) {
  446. av_log(f->avctx, AV_LOG_ERROR, "invalid version %d in ver01 header\n", v);
  447. return AVERROR_INVALIDDATA;
  448. }
  449. f->version = v;
  450. f->ac = get_symbol(c, state, 0);
  451. if (f->ac == AC_RANGE_CUSTOM_TAB) {
  452. for (i = 1; i < 256; i++)
  453. f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
  454. }
  455. colorspace = get_symbol(c, state, 0); //YUV cs type
  456. bits_per_raw_sample = f->version > 0 ? get_symbol(c, state, 0) : f->avctx->bits_per_raw_sample;
  457. chroma_planes = get_rac(c, state);
  458. chroma_h_shift = get_symbol(c, state, 0);
  459. chroma_v_shift = get_symbol(c, state, 0);
  460. transparency = get_rac(c, state);
  461. if (colorspace == 0 && f->avctx->skip_alpha)
  462. transparency = 0;
  463. if (f->plane_count) {
  464. if (colorspace != f->colorspace ||
  465. bits_per_raw_sample != f->avctx->bits_per_raw_sample ||
  466. chroma_planes != f->chroma_planes ||
  467. chroma_h_shift != f->chroma_h_shift ||
  468. chroma_v_shift != f->chroma_v_shift ||
  469. transparency != f->transparency) {
  470. av_log(f->avctx, AV_LOG_ERROR, "Invalid change of global parameters\n");
  471. return AVERROR_INVALIDDATA;
  472. }
  473. }
  474. if (chroma_h_shift > 4U || chroma_v_shift > 4U) {
  475. av_log(f->avctx, AV_LOG_ERROR, "chroma shift parameters %d %d are invalid\n",
  476. chroma_h_shift, chroma_v_shift);
  477. return AVERROR_INVALIDDATA;
  478. }
  479. f->colorspace = colorspace;
  480. f->avctx->bits_per_raw_sample = bits_per_raw_sample;
  481. f->chroma_planes = chroma_planes;
  482. f->chroma_h_shift = chroma_h_shift;
  483. f->chroma_v_shift = chroma_v_shift;
  484. f->transparency = transparency;
  485. f->plane_count = 2 + f->transparency;
  486. }
  487. if (f->colorspace == 0) {
  488. if (!f->transparency && !f->chroma_planes) {
  489. if (f->avctx->bits_per_raw_sample <= 8)
  490. f->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  491. else if (f->avctx->bits_per_raw_sample == 10) {
  492. f->packed_at_lsb = 1;
  493. f->avctx->pix_fmt = AV_PIX_FMT_GRAY10;
  494. } else if (f->avctx->bits_per_raw_sample == 12) {
  495. f->packed_at_lsb = 1;
  496. f->avctx->pix_fmt = AV_PIX_FMT_GRAY12;
  497. } else if (f->avctx->bits_per_raw_sample == 16) {
  498. f->packed_at_lsb = 1;
  499. f->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
  500. } else if (f->avctx->bits_per_raw_sample < 16) {
  501. f->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
  502. } else
  503. return AVERROR(ENOSYS);
  504. } else if (f->transparency && !f->chroma_planes) {
  505. if (f->avctx->bits_per_raw_sample <= 8)
  506. f->avctx->pix_fmt = AV_PIX_FMT_YA8;
  507. else
  508. return AVERROR(ENOSYS);
  509. } else if (f->avctx->bits_per_raw_sample<=8 && !f->transparency) {
  510. switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
  511. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P; break;
  512. case 0x01: f->avctx->pix_fmt = AV_PIX_FMT_YUV440P; break;
  513. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P; break;
  514. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P; break;
  515. case 0x20: f->avctx->pix_fmt = AV_PIX_FMT_YUV411P; break;
  516. case 0x22: f->avctx->pix_fmt = AV_PIX_FMT_YUV410P; break;
  517. }
  518. } else if (f->avctx->bits_per_raw_sample <= 8 && f->transparency) {
  519. switch(16*f->chroma_h_shift + f->chroma_v_shift) {
  520. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P; break;
  521. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P; break;
  522. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P; break;
  523. }
  524. } else if (f->avctx->bits_per_raw_sample == 9 && !f->transparency) {
  525. f->packed_at_lsb = 1;
  526. switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
  527. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P9; break;
  528. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P9; break;
  529. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P9; break;
  530. }
  531. } else if (f->avctx->bits_per_raw_sample == 9 && f->transparency) {
  532. f->packed_at_lsb = 1;
  533. switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
  534. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P9; break;
  535. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P9; break;
  536. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P9; break;
  537. }
  538. } else if (f->avctx->bits_per_raw_sample == 10 && !f->transparency) {
  539. f->packed_at_lsb = 1;
  540. switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
  541. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P10; break;
  542. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P10; break;
  543. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P10; break;
  544. }
  545. } else if (f->avctx->bits_per_raw_sample == 10 && f->transparency) {
  546. f->packed_at_lsb = 1;
  547. switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
  548. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P10; break;
  549. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P10; break;
  550. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P10; break;
  551. }
  552. } else if (f->avctx->bits_per_raw_sample == 12 && !f->transparency) {
  553. f->packed_at_lsb = 1;
  554. switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
  555. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P12; break;
  556. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P12; break;
  557. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P12; break;
  558. }
  559. } else if (f->avctx->bits_per_raw_sample == 16 && !f->transparency){
  560. f->packed_at_lsb = 1;
  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. }
  566. } else if (f->avctx->bits_per_raw_sample == 16 && f->transparency){
  567. f->packed_at_lsb = 1;
  568. switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
  569. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P16; break;
  570. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P16; break;
  571. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P16; break;
  572. }
  573. }
  574. } else if (f->colorspace == 1) {
  575. if (f->chroma_h_shift || f->chroma_v_shift) {
  576. av_log(f->avctx, AV_LOG_ERROR,
  577. "chroma subsampling not supported in this colorspace\n");
  578. return AVERROR(ENOSYS);
  579. }
  580. if ( f->avctx->bits_per_raw_sample <= 8 && !f->transparency)
  581. f->avctx->pix_fmt = AV_PIX_FMT_0RGB32;
  582. else if (f->avctx->bits_per_raw_sample <= 8 && f->transparency)
  583. f->avctx->pix_fmt = AV_PIX_FMT_RGB32;
  584. else if (f->avctx->bits_per_raw_sample == 9 && !f->transparency)
  585. f->avctx->pix_fmt = AV_PIX_FMT_GBRP9;
  586. else if (f->avctx->bits_per_raw_sample == 10 && !f->transparency)
  587. f->avctx->pix_fmt = AV_PIX_FMT_GBRP10;
  588. else if (f->avctx->bits_per_raw_sample == 12 && !f->transparency)
  589. f->avctx->pix_fmt = AV_PIX_FMT_GBRP12;
  590. else if (f->avctx->bits_per_raw_sample == 14 && !f->transparency)
  591. f->avctx->pix_fmt = AV_PIX_FMT_GBRP14;
  592. else if (f->avctx->bits_per_raw_sample == 16 && !f->transparency) {
  593. f->avctx->pix_fmt = AV_PIX_FMT_GBRP16;
  594. f->use32bit = 1;
  595. }
  596. } else {
  597. av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
  598. return AVERROR(ENOSYS);
  599. }
  600. if (f->avctx->pix_fmt == AV_PIX_FMT_NONE) {
  601. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  602. return AVERROR(ENOSYS);
  603. }
  604. ff_dlog(f->avctx, "%d %d %d\n",
  605. f->chroma_h_shift, f->chroma_v_shift, f->avctx->pix_fmt);
  606. if (f->version < 2) {
  607. context_count = read_quant_tables(c, f->quant_table);
  608. if (context_count < 0) {
  609. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  610. return AVERROR_INVALIDDATA;
  611. }
  612. f->slice_count = f->max_slice_count;
  613. } else if (f->version < 3) {
  614. f->slice_count = get_symbol(c, state, 0);
  615. } else {
  616. const uint8_t *p = c->bytestream_end;
  617. for (f->slice_count = 0;
  618. f->slice_count < MAX_SLICES && 3 < p - c->bytestream_start;
  619. f->slice_count++) {
  620. int trailer = 3 + 5*!!f->ec;
  621. int size = AV_RB24(p-trailer);
  622. if (size + trailer > p - c->bytestream_start)
  623. break;
  624. p -= size + trailer;
  625. }
  626. }
  627. if (f->slice_count > (unsigned)MAX_SLICES || f->slice_count <= 0 || f->slice_count > f->max_slice_count) {
  628. av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid (max=%d)\n", f->slice_count, f->max_slice_count);
  629. return AVERROR_INVALIDDATA;
  630. }
  631. for (j = 0; j < f->slice_count; j++) {
  632. FFV1Context *fs = f->slice_context[j];
  633. fs->ac = f->ac;
  634. fs->packed_at_lsb = f->packed_at_lsb;
  635. fs->slice_damaged = 0;
  636. if (f->version == 2) {
  637. fs->slice_x = get_symbol(c, state, 0) * f->width ;
  638. fs->slice_y = get_symbol(c, state, 0) * f->height;
  639. fs->slice_width = (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
  640. fs->slice_height = (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
  641. fs->slice_x /= f->num_h_slices;
  642. fs->slice_y /= f->num_v_slices;
  643. fs->slice_width = fs->slice_width / f->num_h_slices - fs->slice_x;
  644. fs->slice_height = fs->slice_height / f->num_v_slices - fs->slice_y;
  645. if ((unsigned)fs->slice_width > f->width ||
  646. (unsigned)fs->slice_height > f->height)
  647. return AVERROR_INVALIDDATA;
  648. if ( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
  649. || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
  650. return AVERROR_INVALIDDATA;
  651. }
  652. for (i = 0; i < f->plane_count; i++) {
  653. PlaneContext *const p = &fs->plane[i];
  654. if (f->version == 2) {
  655. int idx = get_symbol(c, state, 0);
  656. if (idx > (unsigned)f->quant_table_count) {
  657. av_log(f->avctx, AV_LOG_ERROR,
  658. "quant_table_index out of range\n");
  659. return AVERROR_INVALIDDATA;
  660. }
  661. p->quant_table_index = idx;
  662. memcpy(p->quant_table, f->quant_tables[idx],
  663. sizeof(p->quant_table));
  664. context_count = f->context_count[idx];
  665. } else {
  666. memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
  667. }
  668. if (f->version <= 2) {
  669. av_assert0(context_count >= 0);
  670. if (p->context_count < context_count) {
  671. av_freep(&p->state);
  672. av_freep(&p->vlc_state);
  673. }
  674. p->context_count = context_count;
  675. }
  676. }
  677. }
  678. return 0;
  679. }
  680. static av_cold int decode_init(AVCodecContext *avctx)
  681. {
  682. FFV1Context *f = avctx->priv_data;
  683. int ret;
  684. if ((ret = ff_ffv1_common_init(avctx)) < 0)
  685. return ret;
  686. if (avctx->extradata_size > 0 && (ret = read_extra_header(f)) < 0)
  687. return ret;
  688. if ((ret = ff_ffv1_init_slice_contexts(f)) < 0)
  689. return ret;
  690. avctx->internal->allocate_progress = 1;
  691. return 0;
  692. }
  693. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
  694. {
  695. uint8_t *buf = avpkt->data;
  696. int buf_size = avpkt->size;
  697. FFV1Context *f = avctx->priv_data;
  698. RangeCoder *const c = &f->slice_context[0]->c;
  699. int i, ret;
  700. uint8_t keystate = 128;
  701. uint8_t *buf_p;
  702. AVFrame *p;
  703. if (f->last_picture.f)
  704. ff_thread_release_buffer(avctx, &f->last_picture);
  705. FFSWAP(ThreadFrame, f->picture, f->last_picture);
  706. f->cur = p = f->picture.f;
  707. if (f->version < 3 && avctx->field_order > AV_FIELD_PROGRESSIVE) {
  708. /* we have interlaced material flagged in container */
  709. p->interlaced_frame = 1;
  710. if (avctx->field_order == AV_FIELD_TT || avctx->field_order == AV_FIELD_TB)
  711. p->top_field_first = 1;
  712. }
  713. f->avctx = avctx;
  714. ff_init_range_decoder(c, buf, buf_size);
  715. ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
  716. p->pict_type = AV_PICTURE_TYPE_I; //FIXME I vs. P
  717. if (get_rac(c, &keystate)) {
  718. p->key_frame = 1;
  719. f->key_frame_ok = 0;
  720. if ((ret = read_header(f)) < 0)
  721. return ret;
  722. f->key_frame_ok = 1;
  723. } else {
  724. if (!f->key_frame_ok) {
  725. av_log(avctx, AV_LOG_ERROR,
  726. "Cannot decode non-keyframe without valid keyframe\n");
  727. return AVERROR_INVALIDDATA;
  728. }
  729. p->key_frame = 0;
  730. }
  731. if ((ret = ff_thread_get_buffer(avctx, &f->picture, AV_GET_BUFFER_FLAG_REF)) < 0)
  732. return ret;
  733. if (avctx->debug & FF_DEBUG_PICT_INFO)
  734. av_log(avctx, AV_LOG_DEBUG, "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
  735. f->version, p->key_frame, f->ac, f->ec, f->slice_count, f->avctx->bits_per_raw_sample);
  736. ff_thread_finish_setup(avctx);
  737. buf_p = buf + buf_size;
  738. for (i = f->slice_count - 1; i >= 0; i--) {
  739. FFV1Context *fs = f->slice_context[i];
  740. int trailer = 3 + 5*!!f->ec;
  741. int v;
  742. if (i || f->version > 2) v = AV_RB24(buf_p-trailer) + trailer;
  743. else v = buf_p - c->bytestream_start;
  744. if (buf_p - c->bytestream_start < v) {
  745. av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
  746. ff_thread_report_progress(&f->picture, INT_MAX, 0);
  747. return AVERROR_INVALIDDATA;
  748. }
  749. buf_p -= v;
  750. if (f->ec) {
  751. unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, v);
  752. if (crc) {
  753. int64_t ts = avpkt->pts != AV_NOPTS_VALUE ? avpkt->pts : avpkt->dts;
  754. av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!", crc);
  755. if (ts != AV_NOPTS_VALUE && avctx->pkt_timebase.num) {
  756. av_log(f->avctx, AV_LOG_ERROR, "at %f seconds\n", ts*av_q2d(avctx->pkt_timebase));
  757. } else if (ts != AV_NOPTS_VALUE) {
  758. av_log(f->avctx, AV_LOG_ERROR, "at %"PRId64"\n", ts);
  759. } else {
  760. av_log(f->avctx, AV_LOG_ERROR, "\n");
  761. }
  762. fs->slice_damaged = 1;
  763. }
  764. if (avctx->debug & FF_DEBUG_PICT_INFO) {
  765. av_log(avctx, AV_LOG_DEBUG, "slice %d, CRC: 0x%08X\n", i, AV_RB32(buf_p + v - 4));
  766. }
  767. }
  768. if (i) {
  769. ff_init_range_decoder(&fs->c, buf_p, v);
  770. } else
  771. fs->c.bytestream_end = buf_p + v;
  772. fs->avctx = avctx;
  773. fs->cur = p;
  774. }
  775. avctx->execute(avctx,
  776. decode_slice,
  777. &f->slice_context[0],
  778. NULL,
  779. f->slice_count,
  780. sizeof(void*));
  781. for (i = f->slice_count - 1; i >= 0; i--) {
  782. FFV1Context *fs = f->slice_context[i];
  783. int j;
  784. if (fs->slice_damaged && f->last_picture.f->data[0]) {
  785. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  786. const uint8_t *src[4];
  787. uint8_t *dst[4];
  788. ff_thread_await_progress(&f->last_picture, INT_MAX, 0);
  789. for (j = 0; j < 4; j++) {
  790. int pixshift = desc->comp[j].depth > 8;
  791. int sh = (j == 1 || j == 2) ? f->chroma_h_shift : 0;
  792. int sv = (j == 1 || j == 2) ? f->chroma_v_shift : 0;
  793. dst[j] = p->data[j] + p->linesize[j] *
  794. (fs->slice_y >> sv) + ((fs->slice_x >> sh) << pixshift);
  795. src[j] = f->last_picture.f->data[j] + f->last_picture.f->linesize[j] *
  796. (fs->slice_y >> sv) + ((fs->slice_x >> sh) << pixshift);
  797. }
  798. av_image_copy(dst, p->linesize, src,
  799. f->last_picture.f->linesize,
  800. avctx->pix_fmt,
  801. fs->slice_width,
  802. fs->slice_height);
  803. }
  804. }
  805. ff_thread_report_progress(&f->picture, INT_MAX, 0);
  806. f->picture_number++;
  807. if (f->last_picture.f)
  808. ff_thread_release_buffer(avctx, &f->last_picture);
  809. f->cur = NULL;
  810. if ((ret = av_frame_ref(data, f->picture.f)) < 0)
  811. return ret;
  812. *got_frame = 1;
  813. return buf_size;
  814. }
  815. #if HAVE_THREADS
  816. static int init_thread_copy(AVCodecContext *avctx)
  817. {
  818. FFV1Context *f = avctx->priv_data;
  819. int i, ret;
  820. f->picture.f = NULL;
  821. f->last_picture.f = NULL;
  822. f->sample_buffer = NULL;
  823. f->max_slice_count = 0;
  824. f->slice_count = 0;
  825. for (i = 0; i < f->quant_table_count; i++) {
  826. av_assert0(f->version > 1);
  827. f->initial_states[i] = av_memdup(f->initial_states[i],
  828. f->context_count[i] * sizeof(*f->initial_states[i]));
  829. }
  830. f->picture.f = av_frame_alloc();
  831. f->last_picture.f = av_frame_alloc();
  832. if ((ret = ff_ffv1_init_slice_contexts(f)) < 0)
  833. return ret;
  834. return 0;
  835. }
  836. #endif
  837. static void copy_fields(FFV1Context *fsdst, FFV1Context *fssrc, FFV1Context *fsrc)
  838. {
  839. fsdst->version = fsrc->version;
  840. fsdst->micro_version = fsrc->micro_version;
  841. fsdst->chroma_planes = fsrc->chroma_planes;
  842. fsdst->chroma_h_shift = fsrc->chroma_h_shift;
  843. fsdst->chroma_v_shift = fsrc->chroma_v_shift;
  844. fsdst->transparency = fsrc->transparency;
  845. fsdst->plane_count = fsrc->plane_count;
  846. fsdst->ac = fsrc->ac;
  847. fsdst->colorspace = fsrc->colorspace;
  848. fsdst->ec = fsrc->ec;
  849. fsdst->intra = fsrc->intra;
  850. fsdst->slice_damaged = fssrc->slice_damaged;
  851. fsdst->key_frame_ok = fsrc->key_frame_ok;
  852. fsdst->bits_per_raw_sample = fsrc->bits_per_raw_sample;
  853. fsdst->packed_at_lsb = fsrc->packed_at_lsb;
  854. fsdst->slice_count = fsrc->slice_count;
  855. if (fsrc->version<3){
  856. fsdst->slice_x = fssrc->slice_x;
  857. fsdst->slice_y = fssrc->slice_y;
  858. fsdst->slice_width = fssrc->slice_width;
  859. fsdst->slice_height = fssrc->slice_height;
  860. }
  861. }
  862. #if HAVE_THREADS
  863. static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
  864. {
  865. FFV1Context *fsrc = src->priv_data;
  866. FFV1Context *fdst = dst->priv_data;
  867. int i, ret;
  868. if (dst == src)
  869. return 0;
  870. {
  871. ThreadFrame picture = fdst->picture, last_picture = fdst->last_picture;
  872. uint8_t (*initial_states[MAX_QUANT_TABLES])[32];
  873. struct FFV1Context *slice_context[MAX_SLICES];
  874. memcpy(initial_states, fdst->initial_states, sizeof(fdst->initial_states));
  875. memcpy(slice_context, fdst->slice_context , sizeof(fdst->slice_context));
  876. memcpy(fdst, fsrc, sizeof(*fdst));
  877. memcpy(fdst->initial_states, initial_states, sizeof(fdst->initial_states));
  878. memcpy(fdst->slice_context, slice_context , sizeof(fdst->slice_context));
  879. fdst->picture = picture;
  880. fdst->last_picture = last_picture;
  881. for (i = 0; i<fdst->num_h_slices * fdst->num_v_slices; i++) {
  882. FFV1Context *fssrc = fsrc->slice_context[i];
  883. FFV1Context *fsdst = fdst->slice_context[i];
  884. copy_fields(fsdst, fssrc, fsrc);
  885. }
  886. av_assert0(!fdst->plane[0].state);
  887. av_assert0(!fdst->sample_buffer);
  888. }
  889. av_assert1(fdst->max_slice_count == fsrc->max_slice_count);
  890. ff_thread_release_buffer(dst, &fdst->picture);
  891. if (fsrc->picture.f->data[0]) {
  892. if ((ret = ff_thread_ref_frame(&fdst->picture, &fsrc->picture)) < 0)
  893. return ret;
  894. }
  895. fdst->fsrc = fsrc;
  896. return 0;
  897. }
  898. #endif
  899. AVCodec ff_ffv1_decoder = {
  900. .name = "ffv1",
  901. .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
  902. .type = AVMEDIA_TYPE_VIDEO,
  903. .id = AV_CODEC_ID_FFV1,
  904. .priv_data_size = sizeof(FFV1Context),
  905. .init = decode_init,
  906. .close = ff_ffv1_close,
  907. .decode = decode_frame,
  908. .init_thread_copy = ONLY_IF_THREADS_ENABLED(init_thread_copy),
  909. .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
  910. .capabilities = AV_CODEC_CAP_DR1 /*| AV_CODEC_CAP_DRAW_HORIZ_BAND*/ |
  911. AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_SLICE_THREADS,
  912. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP
  913. };