ffv1dec.c 41 KB

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