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.

1041 lines
38KB

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