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.

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