46#include "config_components.h"
62 ff_dlog(
NULL,
"k-overflow bias:%d error:%d drift:%d count:%d k:%d",
68 ff_dlog(
NULL,
"v:%d bias:%d error:%d drift:%d count:%d k:%d",
71 v ^= ((2 *
state->drift +
state->count) >> 31);
93#define RENAME(name) name
99#define RENAME(name) name ## 32
104 uint8_t *
src,
int w,
int h,
int stride,
int plane_index,
105 int remap_index,
int pixel_stride,
int ac)
118 bits =
f->avctx->bits_per_raw_sample;
128 for (y = 0; y <
h; y++) {
137 if (
f->avctx->bits_per_raw_sample <= 8) {
138 int ret = decode_line(
f, sc, gb,
w,
sample, plane_index, 8, ac);
142 for (x = 0; x <
w; x++)
144 for (x = 0; x <
w; x++)
152 if (
f->packed_at_lsb ||
f->avctx->bits_per_raw_sample == 16) {
153 for (x = 0; x <
w; x++) {
157 for (x = 0; x <
w; x++) {
159 ((uint16_t*)(
src +
stride*y))[x*pixel_stride] = v << (16 -
f->avctx->bits_per_raw_sample) | v >> (2 *
f->avctx->bits_per_raw_sample - 16);
163 if (
f->packed_at_lsb ||
f->avctx->bits_per_raw_sample == 16) {
164 for (x = 0; x <
w; x++) {
168 for (x = 0; x <
w; x++) {
169 ((uint16_t*)(
src +
stride*y))[x*pixel_stride] =
sample[1][x] << (16 -
f->avctx->bits_per_raw_sample) | ((uint16_t **)
sample)[1][x] >> (2 *
f->avctx->bits_per_raw_sample - 16);
183 unsigned ps, context_count;
195 if (sx < 0 || sy < 0 || sw <= 0 || sh <= 0)
197 if (sx >
f->num_h_slices - sw || sy >
f->num_v_slices - sh)
213 for (
unsigned i = 0;
i <
f->plane_count;
i++) {
216 if (idx >= (
unsigned)
f->quant_table_count) {
220 p->quant_table_index = idx;
221 context_count =
f->context_count[idx];
223 if (p->context_count < context_count) {
227 p->context_count = context_count;
234 }
else if (ps == 2) {
237 }
else if (ps == 3) {
244 frame->sample_aspect_ratio) < 0) {
246 frame->sample_aspect_ratio.num,
247 frame->sample_aspect_ratio.den);
251 if (
f->version > 3) {
262 if (
f->combined_version >= 0x40004) {
264 if (sc->
remap > 2U ||
271 if (
f->avctx->bits_per_raw_sample == 32) {
288 f->frame_damaged = 1;
293 int ndx = (
i * mul_count) >> 32;
304 unsigned int end = (1LL<<
f->avctx->bits_per_raw_sample) - 1;
305 int flip = sc->
remap == 2 ? (end>>1) : 0;
307 for (
int p = 0; p < 1 + 2*
f->chroma_planes +
f->transparency; p++) {
310 uint8_t
state[2][3][32];
315 const int is_chroma = (p == 1 || p == 2) &&
f->chroma_planes;
318 const int pixel_num =
FFMIN(plane_width * plane_height, end + 1LL);
319 const size_t fltmap_ceil = 1ULL <<
av_ceil_log2(pixel_num);
321 if (
f->avctx->bits_per_raw_sample == 32) {
334 if (mul_count > 4096U)
336 for (
int i = 0;
i<mul_count;
i++) {
344 for (
i=0;
i <= end ;) {
346 unsigned run0 = lu ? 0 :
run;
347 unsigned run1 = lu ?
run : 1;
349 i += run0 * current_mul;
352 if (current_mul > 1) {
356 i += current_mul - 1 +
delta;
398 for (x = 0; x < 4; x++) {
407 for (y = 0; y <
h; y += 2) {
408 for (p = 0; p < 4; p++) {
418 p == 1 ? 2 : (p > 1),
bits[p], ac);
423 for (x = 0; x <
w; x++) {
424 int g_r =
sample[0][1][x];
425 int g_b =
sample[1][1][x];
438 g_b = g_r - (gd >> 1);
442 *((uint16_t*)(
src + (x*2 + 0)*2 +
stride*(y + 0))) =
r;
443 *((uint16_t*)(
src + (x*2 + 1)*2 +
stride*(y + 0))) = g_r;
444 *((uint16_t*)(
src + (x*2 + 0)*2 +
stride*(y + 1))) = g_b;
445 *((uint16_t*)(
src + (x*2 + 1)*2 +
stride*(y + 1))) =
b;
458 const int si = sc -
f->slices;
465 if (
f->slice_damaged[si])
471 if (
f->version > 2) {
502 if (
f->combined_version >= 0x30002)
503 get_rac(&sc->
c, (uint8_t[]) { 129 });
511 if (
f->colorspace == 0 && (
f->chroma_planes || !
f->transparency)) {
514 const int cx = x >>
f->chroma_h_shift;
515 const int cy = y >>
f->chroma_v_shift;
516 decode_plane(
f, sc, &gb, p->data[0] + ps*x + y*p->linesize[0],
width,
height, p->linesize[0], 0, 0, 1, ac);
518 if (
f->chroma_planes) {
519 decode_plane(
f, sc, &gb, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1, 1, 1, ac);
520 decode_plane(
f, sc, &gb, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1, 2, 1, ac);
523 decode_plane(
f, sc, &gb, p->data[3] + ps*x + y*p->linesize[3],
width,
height, p->linesize[3], (
f->version >= 4 && !
f->chroma_planes) ? 1 : 2,
524 (
f->version >= 4 && !
f->chroma_planes) ? 1 : 3, 1, ac);
525 }
else if (
f->colorspace == 0) {
526 decode_plane(
f, sc, &gb, p->data[0] + ps*x + y*p->linesize[0] ,
width,
height, p->linesize[0], 0, 0, 2, ac);
527 decode_plane(
f, sc, &gb, p->data[0] + ps*x + y*p->linesize[0] + (ps>>1),
width,
height, p->linesize[0], 1, 1, 2, ac);
528 }
else if (
f->bayer) {
531 }
else if (
f->use32bit) {
532 uint8_t *
planes[4] = { p->data[0] + ps * x + y * p->linesize[0],
533 p->data[1] + ps * x + y * p->linesize[1],
534 p->data[2] + ps * x + y * p->linesize[2] };
536 planes[3] = p->data[3] + ps * x + y * p->linesize[3];
539 uint8_t *
planes[4] = { p->data[0] + ps * x + y * p->linesize[0] };
540 if (
f->avctx->bits_per_raw_sample > 8) {
541 planes[1] = p->data[1] + ps * x + y * p->linesize[1];
542 planes[2] = p->data[2] + ps * x + y * p->linesize[2];
544 planes[3] = p->data[3] + ps * x + y * p->linesize[3];
550 get_rac(&sc->
c, (uint8_t[]) { 129 });
570#if CONFIG_FFV1_VULKAN_HWACCEL
583 int context_count = -1;
592 if (
f->bayer &&
f->combined_version <= 0x40002) {
594 "Bayer requires aligned slice coordinates (combined_version > 0x40002)\n");
598 if (
f->configured_pix_fmt !=
f->pix_fmt ||
599 f->configured_width !=
f->width ||
600 f->configured_height !=
f->height ||
601 f->configured_ac !=
f->ac) {
603 if (
f->avctx->pix_fmt < 0)
605 f->configured_pix_fmt =
f->pix_fmt;
606 f->configured_width =
f->width;
607 f->configured_height =
f->height;
608 f->configured_ac =
f->ac;
612 f->chroma_h_shift,
f->chroma_v_shift,
f->pix_fmt);
613 if (
f->version < 2) {
615 if (context_count < 0) {
619 f->slice_count =
f->max_slice_count;
620 }
else if (
f->version < 3) {
623 const uint8_t *p =
c->bytestream_end;
624 for (
f->slice_count = 0;
625 f->slice_count <
MAX_SLICES && 3 + 5*!!
f->ec < p -
c->bytestream_start;
627 int trailer = 3 + 5*!!
f->ec;
629 if (
size + trailer > p -
c->bytestream_start)
634 if (
f->slice_count > (
unsigned)
MAX_SLICES ||
f->slice_count <= 0 ||
f->slice_count >
f->max_slice_count) {
635 av_log(
f->avctx,
AV_LOG_ERROR,
"slice count %d is invalid (max=%d)\n",
f->slice_count,
f->max_slice_count);
641 if (!
f->slice_damaged)
644 for (
int j = 0; j <
f->slice_count; j++) {
647 if (
f->version == 2) {
653 if (sx < 0 || sy < 0 || sw <= 0 || sh <= 0)
655 if (sx >
f->num_h_slices - sw || sy >
f->num_v_slices - sh)
674 for (
int i = 0;
i <
f->plane_count;
i++) {
677 if (
f->version == 2) {
679 if (idx >= (
unsigned)
f->quant_table_count) {
681 "quant_table_index out of range\n");
684 p->quant_table_index = idx;
685 context_count =
f->context_count[idx];
688 if (
f->version <= 2) {
690 p->context_count = context_count;
718 uint8_t *buf, uint8_t *buf_end,
int idx,
719 uint8_t **
pos, uint32_t *
len)
724 uint32_t v = buf_end - buf;
725 if (idx ||
f->version > 2) {
727 uint32_t trailer = 3 + 5*!!
f->ec;
728 if (trailer > buf_end - buf)
731 v =
AV_RB24(buf_end - trailer) + trailer;
734 if (buf_end - buf < v) {
750 uint8_t *buf,
size_t buf_size)
755 uint8_t keystate = 128;
766 if (!
f->key_frame_ok) {
768 "Cannot decode non-keyframe without valid keyframe\n");
775 if (buf_size < avctx->
width * avctx->
height / (128*8))
779 int s = 1 +
w / (1<<23);
786 if (buf_size < (avctx->
height +
i + 6) / 8 *
s)
799 uint8_t *buf = avpkt->
data;
800 size_t buf_size = avpkt->
size;
801 uint8_t *buf_end = buf + buf_size;
803 for (
int i =
f->slice_count - 1;
i >= 0;
i--) {
819 if (crc !=
f->crcref) {
852 for (
int i =
f->slice_count - 1;
i >= 0;
i--) {
856 const uint8_t *
src[4];
859 for (
int j = 0; j <
desc->nb_components; j++) {
860 int pixshift =
desc->comp[j].depth > 8;
861 int sh = (j == 1 || j == 2) ?
f->chroma_h_shift : 0;
862 int sv = (j == 1 || j == 2) ?
f->chroma_v_shift : 0;
863 dst[j] = p->data[j] + p->linesize[j] *
865 src[j] =
f->last_picture.f->data[j] +
f->last_picture.f->linesize[j] *
871 f->last_picture.f->linesize,
876 f->slice_damaged[
i] = 1;
898 FFSWAP(
void *,
f->hwaccel_picture_private,
f->hwaccel_last_picture_private);
901 f->frame_damaged = 0;
909 f->version, !!
f->key_frame,
f->ac,
f->ec,
f->slice_count,
f->avctx->bits_per_raw_sample);
949 uint8_t *buf_end = avpkt->
data + avpkt->
size;
954 for (
int i =
f->slice_count - 1;
i >= 0;
i--) {
1010 fdst->
ac = fsrc->
ac;
1018 fdst->
ec = fsrc->
ec;
1090#if CONFIG_FFV1_VULKAN_HWACCEL
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
const FFCodec ff_ffv1_decoder
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Libavcodec external API header.
#define FF_THREAD_FRAME
Decode more than one frame at once.
#define FF_DEBUG_PICT_INFO
#define i(width, name, range_min, range_max)
#define UPDATE_THREAD_CONTEXT(func)
#define FF_CODEC_CAP_USES_PROGRESSFRAMES
The decoder might make use of the ProgressFrame API.
#define FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM
The decoder extracts and fills its parameters if all frames are skipped due to the skip_frame setting...
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define AV_CEIL_RSHIFT(a, b)
Public header for CRC hash function implementation.
void ff_progress_frame_replace(ProgressFrame *dst, const ProgressFrame *src)
Do nothing if dst and src already refer to the same AVFrame; otherwise unreference dst and if src is ...
void ff_progress_frame_await(const ProgressFrame *f, int n)
Wait for earlier decoding threads to finish reference frames.
void ff_progress_frame_report(ProgressFrame *f, int n)
Notify later decoding threads when part of their reference frame is ready.
int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Select the (possibly hardware accelerated) pixel format.
int ff_hwaccel_frame_priv_alloc(AVCodecContext *avctx, void **hwaccel_picture_private)
Allocate a hwaccel frame private data if the provided avctx uses a hwaccel method that needs it.
int ff_progress_frame_get_buffer(AVCodecContext *avctx, ProgressFrame *f, int flags)
Wrapper around ff_progress_frame_alloc() and ff_thread_get_buffer().
void ff_progress_frame_unref(ProgressFrame *f)
Give up a reference to the underlying frame contained in a ProgressFrame and reset the ProgressFrame,...
#define AV_EF_EXPLODE
abort decoding on minor error detection
@ AV_FIELD_TT
Top coded_first, top displayed first.
@ AV_FIELD_TB
Top coded first, bottom displayed first.
int(* init)(AVBSFContext *ctx)
static const uint8_t bits[8]
static const char * hwaccel
av_cold int ff_ffv1_init_slice_state(const FFV1Context *f, FFV1SliceContext *sc)
av_cold int ff_ffv1_common_init(AVCodecContext *avctx, FFV1Context *s)
PlaneContext * ff_ffv1_planes_alloc(void)
int ff_slice_coord(const FFV1Context *f, int width, int sx, int num_h_slices, int chroma_shift)
This is intended for both width and height.
av_cold void ff_ffv1_close(FFV1Context *s)
void ff_ffv1_clear_slice_state(const FFV1Context *f, FFV1SliceContext *sc)
void ff_ffv1_compute_bits_per_plane(const FFV1Context *f, FFV1SliceContext *sc, int bits[4], int *offset, int mask[4], int bits_per_raw_sample)
av_cold int ff_ffv1_init_slice_contexts(FFV1Context *f)
int ff_ffv1_get_symbol(RangeCoder *c, uint8_t *state, int is_signed)
FF Video Codec 1 (a lossless codec)
int ff_ffv1_read_extra_header(FFV1Context *f)
static void update_vlc_state(VlcState *const state, const int v)
int ff_ffv1_parse_header(FFV1Context *f, RangeCoder *c, uint8_t *state)
int ff_ffv1_read_quant_tables(RangeCoder *c, int16_t quant_table[MAX_CONTEXT_INPUTS][256])
static av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state, int is_signed)
static av_always_inline int fold(int diff, int bits)
static int decode_slice_header(const FFV1Context *f, FFV1SliceContext *sc, AVFrame *frame)
static int decode_frame(AVCodecContext *avctx, AVFrame *rframe, int *got_frame, AVPacket *avpkt)
static int get_vlc_symbol(GetBitContext *gb, VlcState *const state, int bits)
static av_cold int ffv1_decode_close(AVCodecContext *avctx)
static int is_input_end(RangeCoder *c, GetBitContext *gb, int ac)
static int decode_slices(AVCodecContext *avctx, RangeCoder c, AVPacket *avpkt)
static int decode_plane(FFV1Context *f, FFV1SliceContext *sc, GetBitContext *gb, uint8_t *src, int w, int h, int stride, int plane_index, int remap_index, int pixel_stride, int ac)
static av_cold int decode_init(AVCodecContext *avctx)
static int decode_slice(AVCodecContext *c, void *arg)
static void slice_set_damaged(FFV1Context *f, FFV1SliceContext *sc)
static int decode_remap(FFV1Context *f, FFV1SliceContext *sc)
static int decode_header(AVCodecContext *avctx, RangeCoder *c, uint8_t *buf, size_t buf_size)
static int find_next_slice(AVCodecContext *avctx, uint8_t *buf, uint8_t *buf_end, int idx, uint8_t **pos, uint32_t *len)
static int decode_bayer_frame(FFV1Context *f, FFV1SliceContext *sc, GetBitContext *gb, uint8_t *src, int w, int h, int stride)
static enum AVPixelFormat get_pixel_format(FFV1Context *f)
static int decode_current_mul(RangeCoder *rc, uint8_t state[32], int *mul, int mul_count, int64_t i)
static int read_header(FFV1Context *f, RangeCoder *c)
bitstream reader API header.
static int get_bits_left(GetBitContext *gb)
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
static int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len)
read signed golomb rice code (ffv1).
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
@ AVDISCARD_ALL
discard all
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AV_FRAME_FLAG_INTERLACED
A flag to mark frames whose content is interlaced.
#define AV_FRAME_FLAG_TOP_FIELD_FIRST
A flag to mark frames where the top field is displayed first if the content is interlaced.
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static double av_q2d(AVRational a)
Convert an AVRational to a double.
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
void av_image_copy(uint8_t *const dst_data[4], const int dst_linesizes[4], const uint8_t *const src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Copy image in src_data to dst_data.
int av_image_check_sar(unsigned int w, unsigned int h, AVRational sar)
Check if the given sample aspect ratio of an image is valid.
@ AV_PICTURE_TYPE_I
Intra.
#define AV_NOPTS_VALUE
Undefined timestamp value.
static const FFHWAccel * ffhwaccel(const AVHWAccel *codec)
#define HWACCEL_VULKAN(codec)
static av_cold int decode_init(AVCodecContext *avctx)
static int decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_frame, AVPacket *avpkt)
av_cold void CBS_FUNC close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
static void flip(AVCodecContext *avctx, AVFrame *frame)
Multithreading API for decoders.
static enum AVPixelFormat pix_fmts[]
static const struct @257111027162314367033347246032313251342043035002 planes[]
static const uint16_t mask[17]
#define FFSWAP(type, a, b)
const uint8_t ff_log2_run[41]
Memory handling functions.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
AVPixelFormat
Pixel format.
@ AV_PIX_FMT_VULKAN
Vulkan hardware images.
void ff_thread_finish_setup(AVCodecContext *avctx)
If the codec defines update_thread_context(), call this when they are ready for the next thread to st...
av_cold void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size)
void ff_build_rac_states(RangeCoder *c, int factor, int max_p)
static int get_rac(RangeCoder *c, uint8_t *const state)
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
void av_refstruct_replace(void *dstp, const void *src)
Ensure *dstp refers to the same object as src.
static void * av_refstruct_allocz(size_t size)
Equivalent to av_refstruct_alloc_ext(size, 0, NULL, NULL)
main external API structure.
int width
picture width / height.
AVRational pkt_timebase
Timebase in which pkt_dts/pts and AVPacket.dts/pts are expressed.
enum AVFieldOrder field_order
Field order.
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
int(* execute)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg), void *arg2, int *ret, int count, int size)
The codec may call this to execute several independent things.
enum AVDiscard skip_frame
Skip decoding for selected frames.
int step
Number of elements between 2 horizontally consecutive pixels.
This structure describes decoded (raw) audio or video data.
This structure stores compressed data.
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Rational number (pair of numerator and denominator).
int ac
1=range coder <-> 0=golomb rice
enum AVPixelFormat configured_pix_fmt
void * hwaccel_picture_private
FFV1SliceContext * slices
uint8_t state_transition[256]
int16_t quant_tables[MAX_QUANT_TABLES][MAX_CONTEXT_INPUTS][MAX_QUANT_TABLE_SIZE]
enum AVPixelFormat pix_fmt
unsigned int fltmap32_size[4]
int ac_byte_count
number of bytes used for AC coding
unsigned int fltmap_size[4]
The ProgressFrame structure.
uint8_t * bytestream_start
static struct @232240247175332074307271266321204261331170211232 state