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.

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