33 #define SSIZE_ROUND(b) (FFALIGN((b), s->size_scaler) + 4 + s->prefix_bytes)
36 #define SLICE_REDIST_TOTAL 150
93 typedef struct Plane {
187 unsigned topbit = 1, maxval = 1;
195 while (
val > maxval) {
216 int topbit = 1, maxval = 1;
221 while (
val > maxval) {
233 uint32_t cur_pos, dist;
246 dist = cur_pos -
s->next_parse_offset;
247 AV_WB32(
s->pb.buf +
s->next_parse_offset + 5, dist);
248 s->next_parse_offset = cur_pos;
254 s->last_parse_code = pcode;
275 if (!
s->strict_compliance) {
286 if (!
s->strict_compliance) {
288 if (
s->chroma_x_shift == 1 &&
s->chroma_y_shift == 0)
290 else if (
s->chroma_x_shift == 1 &&
s->chroma_y_shift == 1)
302 if (!
s->strict_compliance)
310 if (!
s->strict_compliance) {
322 if (!
s->strict_compliance) {
340 if (!
s->strict_compliance)
349 if (!
s->strict_compliance) {
447 int level, orientation;
449 if (
s->wavelet_depth <= 4 &&
s->quant_matrix ==
VC2_QM_DEF) {
450 s->custom_quant_matrix = 0;
460 s->custom_quant_matrix = 1;
464 for (orientation = 0; orientation < 4; orientation++) {
473 for (orientation = 0; orientation < 4; orientation++) {
479 for (orientation = 0; orientation < 4; orientation++) {
491 if (
s->custom_quant_matrix) {
527 #define QUANT(c, mul, add, shift) (((mul) * (c) + (add)) >> (shift))
535 const int left =
b->width * (sx+0) /
s->num_x;
536 const int right =
b->width * (sx+1) /
s->num_x;
537 const int top =
b->height * (sy+0) /
s->num_y;
538 const int bottom =
b->height * (sy+1) /
s->num_y;
541 const uint64_t q_m = ((uint64_t)(
s->qmagic_lut[
quant][0])) << 2;
542 const uint64_t q_a =
s->qmagic_lut[
quant][1];
545 for (y = top; y < bottom; y++) {
546 for (x = left; x < right; x++) {
563 if (slice->
cache[quant_idx])
564 return slice->
cache[quant_idx];
566 bits += 8*
s->prefix_bytes;
570 for (orientation = !!
level; orientation < 4; orientation++)
571 quants[
level][orientation] =
FFMAX(quant_idx -
s->quant[
level][orientation], 0);
573 for (p = 0; p < 3; p++) {
574 int bytes_start, bytes_len, pad_s, pad_c;
575 bytes_start =
bits >> 3;
578 for (orientation = !!
level; orientation < 4; orientation++) {
581 const int q_idx = quants[
level][orientation];
582 const uint64_t q_m = ((uint64_t)
s->qmagic_lut[q_idx][0]) << 2;
583 const uint64_t q_a =
s->qmagic_lut[q_idx][1];
586 const int left =
b->width * slice->
x /
s->num_x;
587 const int right =
b->width *(slice->
x+1) /
s->num_x;
588 const int top =
b->height * slice->
y /
s->num_y;
589 const int bottom =
b->height *(slice->
y+1) /
s->num_y;
593 for (y = top; y < bottom; y++) {
594 for (x = left; x < right; x++) {
595 uint32_t c_abs =
QUANT(
FFABS(buf[x]), q_m, q_a, q_s);
604 bytes_len = (
bits >> 3) - bytes_start - 1;
605 pad_s =
FFALIGN(bytes_len,
s->size_scaler)/
s->size_scaler;
606 pad_c = (pad_s*
s->size_scaler) - bytes_len;
624 int quant_buf[2] = {-1, -1};
627 while ((
bits > top) || (
bits < bottom)) {
628 const int signed_step =
bits > top ? +step : -step;
631 if (quant_buf[1] ==
quant) {
636 step =
av_clip(step/2, 1, (
s->q_ceil-1)/2);
637 quant_buf[1] = quant_buf[0];
638 quant_buf[0] =
quant;
648 int i, j, slice_x, slice_y, bytes_left = 0;
650 int64_t total_bytes_needed = 0;
657 for (slice_y = 0; slice_y <
s->num_y; slice_y++) {
658 for (slice_x = 0; slice_x <
s->num_x; slice_x++) {
659 SliceArgs *args = &enc_args[
s->num_x*slice_y + slice_x];
665 memset(args->
cache, 0,
s->q_ceil*
sizeof(*args->
cache));
673 for (
i = 0;
i <
s->num_x*
s->num_y;
i++) {
675 bytes_left += args->
bytes;
676 for (j = 0; j < slice_redist_range; j++) {
677 if (args->
bytes > bytes_top[j]) {
678 bytes_top[j] = args->
bytes;
685 bytes_left =
s->frame_max_bytes - bytes_left;
688 while (bytes_left > 0) {
690 for (
i = 0;
i < slice_redist_range;
i++) {
692 int bits, bytes,
diff, prev_bytes, new_idx;
695 if (!top_loc[
i] || !top_loc[
i]->quant_idx)
698 prev_bytes = args->
bytes;
702 diff = bytes - prev_bytes;
703 if ((bytes_left -
diff) > 0) {
714 for (
i = 0;
i <
s->num_x*
s->num_y;
i++) {
716 total_bytes_needed += args->
bytes;
720 return total_bytes_needed;
729 const int slice_x = slice_dat->
x;
730 const int slice_y = slice_dat->
y;
731 const int quant_idx = slice_dat->
quant_idx;
732 const int slice_bytes_max = slice_dat->
bytes;
734 int p,
level, orientation;
744 for (orientation = !!
level; orientation < 4; orientation++)
745 quants[
level][orientation] =
FFMAX(quant_idx -
s->quant[
level][orientation], 0);
748 for (p = 0; p < 3; p++) {
749 int bytes_start, bytes_len, pad_s, pad_c;
753 for (orientation = !!
level; orientation < 4; orientation++) {
755 &
s->plane[p].band[
level][orientation],
756 quants[
level][orientation]);
763 pad_s =
FFALIGN((bytes_len + len_diff),
s->size_scaler)/
s->size_scaler;
764 pad_c = (pad_s*
s->size_scaler) - bytes_len;
766 pad_s =
FFALIGN(bytes_len,
s->size_scaler)/
s->size_scaler;
767 pad_c = (pad_s*
s->size_scaler) - bytes_len;
769 pb->
buf[bytes_start] = pad_s;
783 int slice_x, slice_y, skip = 0;
789 for (slice_y = 0; slice_y <
s->num_y; slice_y++) {
790 for (slice_x = 0; slice_x <
s->num_x; slice_x++) {
791 SliceArgs *args = &enc_args[
s->num_x*slice_y + slice_x];
844 const void *frame_data = transform_dat->
idata;
845 const ptrdiff_t linesize = transform_dat->
istride;
846 const int field = transform_dat->
field;
850 const int idx =
s->wavelet_idx;
851 const int skip = 1 +
s->interlaced;
854 ptrdiff_t pix_stride = linesize >> (
s->bpp - 1);
859 }
else if (field == 2) {
868 for (y = 0; y < p->
height*skip; y+=skip) {
869 for (x = 0; x < p->
width; x++) {
870 buf[x] = pix[x] -
s->diff_offset;
877 const uint16_t *pix = (
const uint16_t *)frame_data +
offset;
878 for (y = 0; y < p->
height*skip; y+=skip) {
879 for (x = 0; x < p->
width; x++) {
880 buf[x] = pix[x] -
s->diff_offset;
893 b->width,
b->height);
900 const char *aux_data,
const int header_size,
int field)
906 for (
i = 0;
i < 3;
i++) {
907 s->transform_args[
i].ctx =
s;
908 s->transform_args[
i].field = field;
909 s->transform_args[
i].plane = &
s->plane[
i];
921 max_frame_bytes <<
s->interlaced,
922 max_frame_bytes <<
s->interlaced);
957 int slice_ceil, sig_size = 256;
961 const int aux_data_size = bitexact ?
sizeof(
"Lavc") :
sizeof(
LIBAVCODEC_IDENT);
962 const int header_size = 100 + aux_data_size;
968 s->last_parse_code = 0;
969 s->next_parse_offset = 0;
972 s->frame_max_bytes = (
av_rescale(r_bitrate,
s->avctx->time_base.num,
973 s->avctx->time_base.den) >> 3) - header_size;
974 s->slice_max_bytes = slice_ceil =
av_rescale(
s->frame_max_bytes, 1,
s->num_x*
s->num_y);
977 while (sig_size > 255) {
979 if (r_size > slice_ceil) {
980 s->slice_max_bytes -= r_size - slice_ceil;
983 sig_size = r_size/
s->size_scaler;
984 s->size_scaler <<= 1;
987 s->slice_min_bytes =
s->slice_max_bytes -
s->slice_max_bytes*(
s->tolerance/100.0f);
988 if (
s->slice_min_bytes < 0)
1015 for (
i = 0;
i < 3;
i++) {
1034 s->picture_number = 0;
1045 s->strict_compliance = 1;
1048 s->slice_max_bytes = 0;
1049 s->slice_min_bytes = 0;
1077 if ((
s->slice_width & (
s->slice_width - 1)) ||
1078 (
s->slice_height & (
s->slice_height - 1))) {
1083 if ((
s->slice_width > avctx->
width) ||
1084 (
s->slice_height > avctx->
height)) {
1089 if (
s->base_vf <= 0) {
1091 s->strict_compliance =
s->base_vf = 0;
1095 "the specifications, decrease strictness to use it.\n");
1112 s->diff_offset = 128;
1117 s->diff_offset = 128;
1118 }
else if (depth == 10) {
1121 s->diff_offset = 512;
1125 s->diff_offset = 2048;
1129 for (
i = 0;
i < 3;
i++) {
1132 p->
width = avctx->
width >> (
i ?
s->chroma_x_shift : 0);
1145 for (o = 0; o < 4; o++) {
1150 shift = (o > 1)*
b->height*
b->stride + (o & 1)*
b->width;
1157 s->plane[
i].coef_stride,
1158 s->plane[
i].dwt_height,
1159 s->slice_width,
s->slice_height))
1164 s->num_x =
s->plane[0].dwt_width/
s->slice_width;
1165 s->num_y =
s->plane[0].dwt_height/
s->slice_height;
1171 for (
i = 0;
i < 116;
i++) {
1173 const uint32_t m =
av_log2(qf);
1174 const uint32_t t = (1ULL << (m + 32)) / qf;
1175 const uint32_t
r = (t*qf + qf) & UINT32_MAX;
1176 if (!(qf & (qf - 1))) {
1177 s->qmagic_lut[
i][0] = 0xFFFFFFFF;
1178 s->qmagic_lut[
i][1] = 0xFFFFFFFF;
1179 }
else if (
r <= 1 << m) {
1180 s->qmagic_lut[
i][0] = t + 1;
1181 s->qmagic_lut[
i][1] = 0;
1183 s->qmagic_lut[
i][0] = t;
1184 s->qmagic_lut[
i][1] = t;
1196 #define VC2ENC_FLAGS (AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
1198 {
"tolerance",
"Max undershoot in percent", offsetof(
VC2EncContext, tolerance),
AV_OPT_TYPE_DOUBLE, {.dbl = 5.0f}, 0.0f, 45.0f,
VC2ENC_FLAGS,
"tolerance"},
1199 {
"slice_width",
"Slice width", offsetof(
VC2EncContext, slice_width),
AV_OPT_TYPE_INT, {.i64 = 32}, 32, 1024,
VC2ENC_FLAGS,
"slice_width"},
1200 {
"slice_height",
"Slice height", offsetof(
VC2EncContext, slice_height),
AV_OPT_TYPE_INT, {.i64 = 16}, 8, 1024,
VC2ENC_FLAGS,
"slice_height"},
1201 {
"wavelet_depth",
"Transform depth", offsetof(
VC2EncContext, wavelet_depth),
AV_OPT_TYPE_INT, {.i64 = 4}, 1, 5,
VC2ENC_FLAGS,
"wavelet_depth"},
1202 {
"wavelet_type",
"Transform type", offsetof(
VC2EncContext, wavelet_idx),
AV_OPT_TYPE_INT, {.i64 =
VC2_TRANSFORM_9_7}, 0,
VC2_TRANSFORMS_NB,
VC2ENC_FLAGS,
"wavelet_idx"},
1203 {
"9_7",
"Deslauriers-Dubuc (9,7)", 0,
AV_OPT_TYPE_CONST, {.i64 =
VC2_TRANSFORM_9_7}, INT_MIN, INT_MAX,
VC2ENC_FLAGS,
"wavelet_idx"},
1204 {
"5_3",
"LeGall (5,3)", 0,
AV_OPT_TYPE_CONST, {.i64 =
VC2_TRANSFORM_5_3}, INT_MIN, INT_MAX,
VC2ENC_FLAGS,
"wavelet_idx"},
1207 {
"qm",
"Custom quantization matrix", offsetof(
VC2EncContext, quant_matrix),
AV_OPT_TYPE_INT, {.i64 =
VC2_QM_DEF}, 0,
VC2_QM_NB,
VC2ENC_FLAGS,
"quant_matrix"},
1223 {
"b",
"600000000" },
static double val(void *priv, double ch)
static const AVCodecDefault defaults[]
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
#define FF_COMPLIANCE_STRICT
Strictly conform to all the things in the spec no matter what consequences.
static av_cold int init(AVCodecContext *avctx)
void ff_put_string(PutBitContext *pb, const char *string, int terminate_string)
Put the string string in the bitstream.
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Interface to Dirac Decoder/Encoder.
DiracParseCodes
Parse code values:
#define MAX_DWT_LEVELS
The spec limits the number of wavelet decompositions to 4 for both level 1 (VC-2) and 128 (long-gop d...
const int32_t ff_dirac_qscale_tab[116]
const uint8_t ff_dirac_default_qmat[7][4][4]
#define DIRAC_MAX_QUANT_INDEX
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_INFO
Standard information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
#define LIBAVUTIL_VERSION_INT
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static enum AVPixelFormat pix_fmts[]
@ AV_CLASS_CATEGORY_ENCODER
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
#define AV_PIX_FMT_YUV444P12
#define AV_PIX_FMT_YUV420P10
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
@ AVCOL_RANGE_UNSPECIFIED
@ AVCOL_RANGE_JPEG
Full range content.
#define AV_PIX_FMT_YUV420P12
#define AV_PIX_FMT_YUV422P12
#define AV_PIX_FMT_YUV422P10
AVPixelFormat
Pixel format.
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
@ AVCOL_PRI_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM
@ AVCOL_PRI_SMPTE240M
functionally identical to above
@ AVCOL_PRI_SMPTE170M
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
@ AVCOL_TRC_BT1361_ECG
ITU-R BT1361 Extended Colour Gamut.
@ AVCOL_TRC_LINEAR
"Linear transfer characteristics"
#define AV_PIX_FMT_YUV444P10
@ AVCOL_SPC_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601
@ AVCOL_SPC_RGB
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB)
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
static void put_bits64(PutBitContext *s, int n, uint64_t value)
Write up to 64 bits into a bitstream.
static uint8_t * put_bits_ptr(PutBitContext *s)
Return the pointer to the byte where the bitstream writer will put the next bit.
static int put_bits_count(PutBitContext *s)
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
static void skip_put_bytes(PutBitContext *s, int n)
Skip the given number of bytes.
static void align_put_bits(PutBitContext *s)
Pad the bitstream with zeros up to the next byte boundary.
#define FF_ARRAY_ELEMS(a)
static int shift(int a, int b)
Describe the class of an AVClass context structure.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
main external API structure.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
int width
picture width / height.
enum AVColorRange color_range
MPEG vs JPEG YUV range.
int strict_std_compliance
strictly follow the standard (MPEG-4, ...).
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel.
enum AVFieldOrder field_order
Field order.
int64_t bit_rate
the average bitrate
enum AVColorSpace colorspace
YUV colorspace type.
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
int flags
AV_CODEC_FLAG_*.
const char * name
Name of the codec implementation.
int depth
Number of bits in the component.
This structure describes decoded (raw) audio or video data.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
This structure stores compressed data.
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).
SubBand band[DWT_LEVELS_3D][4]
int cache[DIRAC_MAX_QUANT_INDEX]
TransformArgs transform_args[3]
uint8_t quant[MAX_DWT_LEVELS][4]
uint32_t next_parse_offset
uint32_t qmagic_lut[116][2]
enum DiracParseCodes last_parse_code
void(* vc2_subband_dwt[VC2_TRANSFORMS_NB])(struct VC2TransformContext *t, dwtcoef *data, ptrdiff_t stride, int width, int height)
static void encode_parse_info(VC2EncContext *s, enum DiracParseCodes pcode)
static int rate_control(AVCodecContext *avctx, void *arg)
static void encode_frame_rate(VC2EncContext *s)
static int calc_slice_sizes(VC2EncContext *s)
static void encode_source_params(VC2EncContext *s)
static void encode_frame_size(VC2EncContext *s)
static void encode_transform_params(VC2EncContext *s)
static const AVOption vc2enc_options[]
static void encode_signal_range(VC2EncContext *s)
static av_cold int vc2_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet)
static av_always_inline void put_vc2_ue_uint(PutBitContext *pb, uint32_t val)
static void encode_seq_header(VC2EncContext *s)
static void encode_quant_matrix(VC2EncContext *s)
static av_cold int vc2_encode_init(AVCodecContext *avctx)
static void encode_color_spec(VC2EncContext *s)
static const int base_video_fmts_len
static void encode_subband(VC2EncContext *s, PutBitContext *pb, int sx, int sy, SubBand *b, int quant)
static av_always_inline int count_vc2_ue_uint(uint32_t val)
static void encode_picture_header(VC2EncContext *s)
static int encode_slices(VC2EncContext *s)
static void encode_slice_params(VC2EncContext *s)
static void encode_picture_start(VC2EncContext *s)
static av_cold int vc2_encode_end(AVCodecContext *avctx)
static void encode_wavelet_transform(VC2EncContext *s)
static void encode_parse_params(VC2EncContext *s)
#define SLICE_REDIST_TOTAL
#define QUANT(c, mul, add, shift)
static const AVCodecDefault vc2enc_defaults[]
static int encode_hq_slice(AVCodecContext *avctx, void *arg)
static void init_quant_matrix(VC2EncContext *s)
static const VC2BaseVideoFormat base_video_fmts[]
static int encode_frame(VC2EncContext *s, AVPacket *avpkt, const AVFrame *frame, const char *aux_data, const int header_size, int field)
static void encode_aspect_ratio(VC2EncContext *s)
static const uint8_t vc2_qm_col_tab[][4]
static void encode_scan_format(VC2EncContext *s)
static const AVClass vc2enc_class
static enum AVPixelFormat allowed_pix_fmts[]
static void encode_clean_area(VC2EncContext *s)
static int count_hq_slice(SliceArgs *slice, int quant_idx)
static const uint8_t vc2_qm_flat_tab[][4]
static void encode_sample_fmt(VC2EncContext *s)
static int dwt_plane(AVCodecContext *avctx, void *arg)
av_cold int ff_vc2enc_init_transforms(VC2TransformContext *s, int p_stride, int p_height, int slice_w, int slice_h)
av_cold void ff_vc2enc_free_transforms(VC2TransformContext *s)
static const double coeff[2][5]
static av_always_inline int diff(const uint32_t a, const uint32_t b)
static const uint8_t offset[127][2]