2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "libavutil/crc.h"
20 #include "libavutil/mem.h"
21 #include "libavutil/pixdesc.h"
22 #include "rangecoder.h"
25 static int read_quant_table(RangeCoder
*c
, int16_t *quant_table
, int scale
)
29 uint8_t state
[CONTEXT_SIZE
];
31 memset(state
, 128, sizeof(state
));
33 for (v
= 0; i
< 128; v
++) {
34 unsigned len
= ff_ffv1_get_symbol(c
, state
, 0) + 1U;
36 if (len
> 128 - i
|| !len
)
37 return AVERROR_INVALIDDATA
;
40 quant_table
[i
] = scale
* v
;
45 for (i
= 1; i
< 128; i
++)
46 quant_table
[256 - i
] = -quant_table
[i
];
47 quant_table
[128] = -quant_table
[127];
52 int ff_ffv1_read_quant_tables(RangeCoder
*c
,
53 int16_t quant_table
[MAX_CONTEXT_INPUTS
][256])
56 int context_count
= 1;
58 for (i
= 0; i
< 5; i
++) {
59 int ret
= read_quant_table(c
, quant_table
[i
], context_count
);
63 if (context_count
> 32768U) {
64 return AVERROR_INVALIDDATA
;
67 return (context_count
+ 1) / 2;
70 int ff_ffv1_read_extra_header(FFV1Context
*f
)
73 uint8_t state
[CONTEXT_SIZE
];
75 uint8_t state2
[32][CONTEXT_SIZE
];
78 memset(state2
, 128, sizeof(state2
));
79 memset(state
, 128, sizeof(state
));
81 ff_init_range_decoder(&c
, f
->avctx
->extradata
, f
->avctx
->extradata_size
);
82 ff_build_rac_states(&c
, 0.05 * (1LL << 32), 256 - 8);
84 f
->version
= ff_ffv1_get_symbol(&c
, state
, 0);
86 av_log(f
->avctx
, AV_LOG_ERROR
, "Invalid version in global header\n");
87 return AVERROR_INVALIDDATA
;
90 av_log(f
->avctx
, AV_LOG_ERROR
, "unsupported version %d\n",
92 return AVERROR_PATCHWELCOME
;
94 f
->combined_version
= f
->version
<< 16;
96 c
.bytestream_end
-= 4;
97 f
->micro_version
= ff_ffv1_get_symbol(&c
, state
, 0);
98 if (f
->micro_version
< 0 || f
->micro_version
> 65535)
99 return AVERROR_INVALIDDATA
;
100 f
->combined_version
+= f
->micro_version
;
102 f
->ac
= ff_ffv1_get_symbol(&c
, state
, 0);
104 if (f
->ac
== AC_RANGE_CUSTOM_TAB
) {
105 for (int i
= 1; i
< 256; i
++)
106 f
->state_transition
[i
] = ff_ffv1_get_symbol(&c
, state
, 1) + c
.one_state
[i
];
109 f
->colorspace
= ff_ffv1_get_symbol(&c
, state
, 0); //YUV cs type
110 f
->avctx
->bits_per_raw_sample
= ff_ffv1_get_symbol(&c
, state
, 0);
111 f
->chroma_planes
= get_rac(&c
, state
);
112 f
->chroma_h_shift
= ff_ffv1_get_symbol(&c
, state
, 0);
113 f
->chroma_v_shift
= ff_ffv1_get_symbol(&c
, state
, 0);
114 f
->transparency
= get_rac(&c
, state
);
115 f
->plane_count
= 1 + (f
->chroma_planes
|| f
->version
<4) + f
->transparency
;
116 f
->num_h_slices
= 1 + ff_ffv1_get_symbol(&c
, state
, 0);
117 f
->num_v_slices
= 1 + ff_ffv1_get_symbol(&c
, state
, 0);
119 if (f
->chroma_h_shift
> 4U || f
->chroma_v_shift
> 4U) {
120 av_log(f
->avctx
, AV_LOG_ERROR
, "chroma shift parameters %d %d are invalid\n",
121 f
->chroma_h_shift
, f
->chroma_v_shift
);
122 return AVERROR_INVALIDDATA
;
125 if (f
->num_h_slices
> (unsigned)f
->width
|| !f
->num_h_slices
||
126 f
->num_v_slices
> (unsigned)f
->height
|| !f
->num_v_slices
128 av_log(f
->avctx
, AV_LOG_ERROR
, "slice count invalid\n");
129 return AVERROR_INVALIDDATA
;
132 if (f
->num_h_slices
> MAX_SLICES
/ f
->num_v_slices
) {
133 av_log(f
->avctx
, AV_LOG_ERROR
, "slice count unsupported\n");
134 return AVERROR_PATCHWELCOME
;
137 f
->quant_table_count
= ff_ffv1_get_symbol(&c
, state
, 0);
138 if (f
->quant_table_count
> (unsigned)MAX_QUANT_TABLES
|| !f
->quant_table_count
) {
139 av_log(f
->avctx
, AV_LOG_ERROR
, "quant table count %d is invalid\n", f
->quant_table_count
);
140 f
->quant_table_count
= 0;
141 return AVERROR_INVALIDDATA
;
144 for (int i
= 0; i
< f
->quant_table_count
; i
++) {
145 f
->context_count
[i
] = ff_ffv1_read_quant_tables(&c
, f
->quant_tables
[i
]);
146 if (f
->context_count
[i
] < 0) {
147 av_log(f
->avctx
, AV_LOG_ERROR
, "read_quant_table error\n");
148 return AVERROR_INVALIDDATA
;
151 if ((ret
= ff_ffv1_allocate_initial_states(f
)) < 0)
154 for (int i
= 0; i
< f
->quant_table_count
; i
++)
155 if (get_rac(&c
, state
)) {
156 for (int j
= 0; j
< f
->context_count
[i
]; j
++)
157 for (int k
= 0; k
< CONTEXT_SIZE
; k
++) {
158 int pred
= j
? f
->initial_states
[i
][j
- 1][k
] : 128;
159 f
->initial_states
[i
][j
][k
] =
160 (pred
+ ff_ffv1_get_symbol(&c
, state2
[k
], 1)) & 0xFF;
164 if (f
->version
> 2) {
165 f
->ec
= ff_ffv1_get_symbol(&c
, state
, 0);
167 f
->crcref
= 0x7a8c4079;
168 if (f
->combined_version
>= 0x30003)
169 f
->intra
= ff_ffv1_get_symbol(&c
, state
, 0);
172 if (f
->version
> 2) {
174 v
= av_crc(av_crc_get_table(AV_CRC_32_IEEE
), f
->crcref
,
175 f
->avctx
->extradata
, f
->avctx
->extradata_size
);
176 if (v
!= f
->crcref
|| f
->avctx
->extradata_size
< 4) {
177 av_log(f
->avctx
, AV_LOG_ERROR
, "CRC mismatch %X!\n", v
);
178 return AVERROR_INVALIDDATA
;
180 crc
= AV_RB32(f
->avctx
->extradata
+ f
->avctx
->extradata_size
- 4);
183 if (f
->avctx
->debug
& FF_DEBUG_PICT_INFO
)
184 av_log(f
->avctx
, AV_LOG_DEBUG
,
185 "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",
186 f
->version
, f
->micro_version
,
189 f
->avctx
->bits_per_raw_sample
,
190 f
->chroma_planes
, f
->chroma_h_shift
, f
->chroma_v_shift
,
192 f
->num_h_slices
, f
->num_v_slices
,
193 f
->quant_table_count
,
201 int ff_ffv1_parse_header(FFV1Context
*f
, RangeCoder
*c
, uint8_t *state
)
203 if (f
->version
< 2) {
204 int chroma_planes
, chroma_h_shift
, chroma_v_shift
, transparency
, colorspace
, bits_per_raw_sample
;
205 unsigned v
= ff_ffv1_get_symbol(c
, state
, 0);
207 av_log(f
->avctx
, AV_LOG_ERROR
, "invalid version %d in ver01 header\n", v
);
208 return AVERROR_INVALIDDATA
;
211 f
->ac
= ff_ffv1_get_symbol(c
, state
, 0);
213 if (f
->ac
== AC_RANGE_CUSTOM_TAB
) {
214 for (int i
= 1; i
< 256; i
++) {
215 int st
= ff_ffv1_get_symbol(c
, state
, 1) + c
->one_state
[i
];
216 if (st
< 1 || st
> 255) {
217 av_log(f
->avctx
, AV_LOG_ERROR
, "invalid state transition %d\n", st
);
218 return AVERROR_INVALIDDATA
;
220 f
->state_transition
[i
] = st
;
224 colorspace
= ff_ffv1_get_symbol(c
, state
, 0); //YUV cs type
225 bits_per_raw_sample
= f
->version
> 0 ? ff_ffv1_get_symbol(c
, state
, 0) : f
->avctx
->bits_per_raw_sample
;
226 chroma_planes
= get_rac(c
, state
);
227 chroma_h_shift
= ff_ffv1_get_symbol(c
, state
, 0);
228 chroma_v_shift
= ff_ffv1_get_symbol(c
, state
, 0);
229 transparency
= get_rac(c
, state
);
230 if (colorspace
== 0 && f
->avctx
->skip_alpha
)
233 if (f
->plane_count
) {
234 if (colorspace
!= f
->colorspace
||
235 bits_per_raw_sample
!= f
->avctx
->bits_per_raw_sample
||
236 chroma_planes
!= f
->chroma_planes
||
237 chroma_h_shift
!= f
->chroma_h_shift
||
238 chroma_v_shift
!= f
->chroma_v_shift
||
239 transparency
!= f
->transparency
) {
240 av_log(f
->avctx
, AV_LOG_ERROR
, "Invalid change of global parameters\n");
241 return AVERROR_INVALIDDATA
;
245 if (chroma_h_shift
> 4U || chroma_v_shift
> 4U) {
246 av_log(f
->avctx
, AV_LOG_ERROR
, "chroma shift parameters %d %d are invalid\n",
247 chroma_h_shift
, chroma_v_shift
);
248 return AVERROR_INVALIDDATA
;
251 f
->colorspace
= colorspace
;
252 f
->avctx
->bits_per_raw_sample
= bits_per_raw_sample
;
253 f
->chroma_planes
= chroma_planes
;
254 f
->chroma_h_shift
= chroma_h_shift
;
255 f
->chroma_v_shift
= chroma_v_shift
;
256 f
->transparency
= transparency
;
258 f
->plane_count
= 2 + f
->transparency
;
261 if (f
->colorspace
== 0) {
262 if (!f
->transparency
&& !f
->chroma_planes
) {
263 if (f
->avctx
->bits_per_raw_sample
<= 8)
264 f
->pix_fmt
= AV_PIX_FMT_GRAY8
;
265 else if (f
->avctx
->bits_per_raw_sample
== 9) {
266 f
->packed_at_lsb
= 1;
267 f
->pix_fmt
= AV_PIX_FMT_GRAY9
;
268 } else if (f
->avctx
->bits_per_raw_sample
== 10) {
269 f
->packed_at_lsb
= 1;
270 f
->pix_fmt
= AV_PIX_FMT_GRAY10
;
271 } else if (f
->avctx
->bits_per_raw_sample
== 12) {
272 f
->packed_at_lsb
= 1;
273 f
->pix_fmt
= AV_PIX_FMT_GRAY12
;
274 } else if (f
->avctx
->bits_per_raw_sample
== 14) {
275 f
->packed_at_lsb
= 1;
276 f
->pix_fmt
= AV_PIX_FMT_GRAY14
;
277 } else if (f
->avctx
->bits_per_raw_sample
== 16) {
278 f
->packed_at_lsb
= 1;
279 f
->pix_fmt
= AV_PIX_FMT_GRAY16
;
280 } else if (f
->avctx
->bits_per_raw_sample
< 16) {
281 f
->pix_fmt
= AV_PIX_FMT_GRAY16
;
283 return AVERROR(ENOSYS
);
284 } else if (f
->transparency
&& !f
->chroma_planes
) {
285 if (f
->avctx
->bits_per_raw_sample
<= 8)
286 f
->pix_fmt
= AV_PIX_FMT_YA8
;
288 return AVERROR(ENOSYS
);
289 } else if (f
->avctx
->bits_per_raw_sample
<=8 && !f
->transparency
) {
290 switch(16 * f
->chroma_h_shift
+ f
->chroma_v_shift
) {
291 case 0x00: f
->pix_fmt
= AV_PIX_FMT_YUV444P
; break;
292 case 0x01: f
->pix_fmt
= AV_PIX_FMT_YUV440P
; break;
293 case 0x10: f
->pix_fmt
= AV_PIX_FMT_YUV422P
; break;
294 case 0x11: f
->pix_fmt
= AV_PIX_FMT_YUV420P
; break;
295 case 0x20: f
->pix_fmt
= AV_PIX_FMT_YUV411P
; break;
296 case 0x22: f
->pix_fmt
= AV_PIX_FMT_YUV410P
; break;
298 } else if (f
->avctx
->bits_per_raw_sample
<= 8 && f
->transparency
) {
299 switch(16*f
->chroma_h_shift
+ f
->chroma_v_shift
) {
300 case 0x00: f
->pix_fmt
= AV_PIX_FMT_YUVA444P
; break;
301 case 0x10: f
->pix_fmt
= AV_PIX_FMT_YUVA422P
; break;
302 case 0x11: f
->pix_fmt
= AV_PIX_FMT_YUVA420P
; break;
304 } else if (f
->avctx
->bits_per_raw_sample
== 9 && !f
->transparency
) {
305 f
->packed_at_lsb
= 1;
306 switch(16 * f
->chroma_h_shift
+ f
->chroma_v_shift
) {
307 case 0x00: f
->pix_fmt
= AV_PIX_FMT_YUV444P9
; break;
308 case 0x10: f
->pix_fmt
= AV_PIX_FMT_YUV422P9
; break;
309 case 0x11: f
->pix_fmt
= AV_PIX_FMT_YUV420P9
; break;
311 } else if (f
->avctx
->bits_per_raw_sample
== 9 && f
->transparency
) {
312 f
->packed_at_lsb
= 1;
313 switch(16 * f
->chroma_h_shift
+ f
->chroma_v_shift
) {
314 case 0x00: f
->pix_fmt
= AV_PIX_FMT_YUVA444P9
; break;
315 case 0x10: f
->pix_fmt
= AV_PIX_FMT_YUVA422P9
; break;
316 case 0x11: f
->pix_fmt
= AV_PIX_FMT_YUVA420P9
; break;
318 } else if (f
->avctx
->bits_per_raw_sample
== 10 && !f
->transparency
) {
319 f
->packed_at_lsb
= 1;
320 switch(16 * f
->chroma_h_shift
+ f
->chroma_v_shift
) {
321 case 0x00: f
->pix_fmt
= AV_PIX_FMT_YUV444P10
; break;
322 case 0x01: f
->pix_fmt
= AV_PIX_FMT_YUV440P10
; break;
323 case 0x10: f
->pix_fmt
= AV_PIX_FMT_YUV422P10
; break;
324 case 0x11: f
->pix_fmt
= AV_PIX_FMT_YUV420P10
; break;
326 } else if (f
->avctx
->bits_per_raw_sample
== 10 && f
->transparency
) {
327 f
->packed_at_lsb
= 1;
328 switch(16 * f
->chroma_h_shift
+ f
->chroma_v_shift
) {
329 case 0x00: f
->pix_fmt
= AV_PIX_FMT_YUVA444P10
; break;
330 case 0x10: f
->pix_fmt
= AV_PIX_FMT_YUVA422P10
; break;
331 case 0x11: f
->pix_fmt
= AV_PIX_FMT_YUVA420P10
; break;
333 } else if (f
->avctx
->bits_per_raw_sample
== 12 && !f
->transparency
) {
334 f
->packed_at_lsb
= 1;
335 switch(16 * f
->chroma_h_shift
+ f
->chroma_v_shift
) {
336 case 0x00: f
->pix_fmt
= AV_PIX_FMT_YUV444P12
; break;
337 case 0x01: f
->pix_fmt
= AV_PIX_FMT_YUV440P12
; break;
338 case 0x10: f
->pix_fmt
= AV_PIX_FMT_YUV422P12
; break;
339 case 0x11: f
->pix_fmt
= AV_PIX_FMT_YUV420P12
; break;
341 } else if (f
->avctx
->bits_per_raw_sample
== 12 && f
->transparency
) {
342 f
->packed_at_lsb
= 1;
343 switch(16 * f
->chroma_h_shift
+ f
->chroma_v_shift
) {
344 case 0x00: f
->pix_fmt
= AV_PIX_FMT_YUVA444P12
; break;
345 case 0x10: f
->pix_fmt
= AV_PIX_FMT_YUVA422P12
; break;
347 } else if (f
->avctx
->bits_per_raw_sample
== 14 && !f
->transparency
) {
348 f
->packed_at_lsb
= 1;
349 switch(16 * f
->chroma_h_shift
+ f
->chroma_v_shift
) {
350 case 0x00: f
->pix_fmt
= AV_PIX_FMT_YUV444P14
; break;
351 case 0x10: f
->pix_fmt
= AV_PIX_FMT_YUV422P14
; break;
352 case 0x11: f
->pix_fmt
= AV_PIX_FMT_YUV420P14
; break;
354 } else if (f
->avctx
->bits_per_raw_sample
== 16 && !f
->transparency
){
355 f
->packed_at_lsb
= 1;
356 switch(16 * f
->chroma_h_shift
+ f
->chroma_v_shift
) {
357 case 0x00: f
->pix_fmt
= AV_PIX_FMT_YUV444P16
; break;
358 case 0x10: f
->pix_fmt
= AV_PIX_FMT_YUV422P16
; break;
359 case 0x11: f
->pix_fmt
= AV_PIX_FMT_YUV420P16
; break;
361 } else if (f
->avctx
->bits_per_raw_sample
== 16 && f
->transparency
){
362 f
->packed_at_lsb
= 1;
363 switch(16 * f
->chroma_h_shift
+ f
->chroma_v_shift
) {
364 case 0x00: f
->pix_fmt
= AV_PIX_FMT_YUVA444P16
; break;
365 case 0x10: f
->pix_fmt
= AV_PIX_FMT_YUVA422P16
; break;
366 case 0x11: f
->pix_fmt
= AV_PIX_FMT_YUVA420P16
; break;
369 } else if (f
->colorspace
== 1) {
370 if (f
->chroma_h_shift
|| f
->chroma_v_shift
) {
371 av_log(f
->avctx
, AV_LOG_ERROR
,
372 "chroma subsampling not supported in this colorspace\n");
373 return AVERROR(ENOSYS
);
375 if ( f
->avctx
->bits_per_raw_sample
<= 8 && !f
->transparency
)
376 f
->pix_fmt
= AV_PIX_FMT_0RGB32
;
377 else if (f
->avctx
->bits_per_raw_sample
<= 8 && f
->transparency
)
378 f
->pix_fmt
= AV_PIX_FMT_RGB32
;
379 else if (f
->avctx
->bits_per_raw_sample
== 9 && !f
->transparency
)
380 f
->pix_fmt
= AV_PIX_FMT_GBRP9
;
381 else if (f
->avctx
->bits_per_raw_sample
== 10 && !f
->transparency
)
382 f
->pix_fmt
= AV_PIX_FMT_GBRP10
;
383 else if (f
->avctx
->bits_per_raw_sample
== 10 && f
->transparency
)
384 f
->pix_fmt
= AV_PIX_FMT_GBRAP10
;
385 else if (f
->avctx
->bits_per_raw_sample
== 12 && !f
->transparency
)
386 f
->pix_fmt
= AV_PIX_FMT_GBRP12
;
387 else if (f
->avctx
->bits_per_raw_sample
== 12 && f
->transparency
)
388 f
->pix_fmt
= AV_PIX_FMT_GBRAP12
;
389 else if (f
->avctx
->bits_per_raw_sample
== 14 && !f
->transparency
)
390 f
->pix_fmt
= AV_PIX_FMT_GBRP14
;
391 else if (f
->avctx
->bits_per_raw_sample
== 14 && f
->transparency
)
392 f
->pix_fmt
= AV_PIX_FMT_GBRAP14
;
393 else if (f
->avctx
->bits_per_raw_sample
== 16 && !f
->transparency
) {
394 f
->pix_fmt
= AV_PIX_FMT_GBRP16
;
396 } else if (f
->avctx
->bits_per_raw_sample
== 16 && f
->transparency
) {
397 f
->pix_fmt
= AV_PIX_FMT_GBRAP16
;
401 av_log(f
->avctx
, AV_LOG_ERROR
, "colorspace not supported\n");
402 return AVERROR(ENOSYS
);
404 if (f
->pix_fmt
== AV_PIX_FMT_NONE
) {
405 av_log(f
->avctx
, AV_LOG_ERROR
, "format not supported\n");
406 return AVERROR(ENOSYS
);