33 #define SSIZE_ROUND(b) (FFALIGN((b), s->size_scaler) + 4 + s->prefix_bytes)
36 #define SLICE_REDIST_TOTAL 150
93 typedef struct Plane {
186 int pbits = 0, bits = 0, topbit = 1, maxval = 1;
193 while (val > maxval) {
201 for (i = 0; i < bits; i++) {
208 put_bits(pb, bits*2 + 1, (pbits << 1) | 1);
213 int topbit = 1, maxval = 1;
218 while (val > maxval) {
230 uint32_t cur_pos, dist;
444 int level, orientation;
461 for (orientation = 0; orientation < 4; orientation++) {
470 for (orientation = 0; orientation < 4; orientation++) {
476 for (orientation = 0; orientation < 4; orientation++) {
524 #define QUANT(c, mul, add, shift) (((mul) * (c) + (add)) >> (shift))
532 const int left = b->
width * (sx+0) / s->
num_x;
533 const int right = b->
width * (sx+1) / s->
num_x;
538 const uint64_t q_m = ((uint64_t)(s->
qmagic_lut[quant][0])) << 2;
542 for (y = top; y < bottom; y++) {
543 for (x = left; x < right; x++) {
544 uint32_t c_abs =
QUANT(
FFABS(coeff[x]), q_m, q_a, q_s);
557 int bits = 0, p,
level, orientation;
560 if (slice->
cache[quant_idx])
561 return slice->
cache[quant_idx];
567 for (orientation = !!level; orientation < 4; orientation++)
568 quants[level][orientation] =
FFMAX(quant_idx - s->
quant[level][orientation], 0);
570 for (p = 0; p < 3; p++) {
571 int bytes_start, bytes_len, pad_s, pad_c;
572 bytes_start = bits >> 3;
575 for (orientation = !!level; orientation < 4; orientation++) {
578 const int q_idx = quants[
level][orientation];
579 const uint64_t q_m = ((uint64_t)s->
qmagic_lut[q_idx][0]) << 2;
584 const int right = b->
width *(slice->
x+1) / s->
num_x;
590 for (y = top; y < bottom; y++) {
591 for (x = left; x < right; x++) {
592 uint32_t c_abs =
QUANT(
FFABS(buf[x]), q_m, q_a, q_s);
600 bits +=
FFALIGN(bits, 8) - bits;
601 bytes_len = (bits >> 3) - bytes_start - 1;
607 slice->
cache[quant_idx] = bits;
621 int quant_buf[2] = {-1, -1};
624 while ((bits > top) || (bits < bottom)) {
625 const int signed_step = bits > top ? +step : -step;
626 quant = av_clip(quant + signed_step, 0, s->
q_ceil-1);
628 if (quant_buf[1] == quant) {
629 quant =
FFMAX(quant_buf[0], quant);
630 bits = quant == quant_buf[0] ? bits_last : bits;
633 step = av_clip(step/2, 1, (s->
q_ceil-1)/2);
634 quant_buf[1] = quant_buf[0];
635 quant_buf[0] =
quant;
645 int i, j, slice_x, slice_y, bytes_left = 0;
647 int64_t total_bytes_needed = 0;
654 for (slice_y = 0; slice_y < s->
num_y; slice_y++) {
655 for (slice_x = 0; slice_x < s->
num_x; slice_x++) {
672 bytes_left += args->
bytes;
673 for (j = 0; j < slice_redist_range; j++) {
674 if (args->
bytes > bytes_top[j]) {
675 bytes_top[j] = args->
bytes;
685 while (bytes_left > 0) {
687 for (i = 0; i < slice_redist_range; i++) {
689 int bits, bytes,
diff, prev_bytes, new_idx;
692 if (!top_loc[i] || !top_loc[i]->quant_idx)
695 prev_bytes = args->
bytes;
699 diff = bytes - prev_bytes;
700 if ((bytes_left - diff) > 0) {
713 total_bytes_needed += args->
bytes;
717 return total_bytes_needed;
726 const int slice_x = slice_dat->
x;
727 const int slice_y = slice_dat->
y;
728 const int quant_idx = slice_dat->
quant_idx;
729 const int slice_bytes_max = slice_dat->
bytes;
731 int p,
level, orientation;
741 for (orientation = !!level; orientation < 4; orientation++)
742 quants[level][orientation] =
FFMAX(quant_idx - s->
quant[level][orientation], 0);
745 for (p = 0; p < 3; p++) {
746 int bytes_start, bytes_len, pad_s, pad_c;
750 for (orientation = !!level; orientation < 4; orientation++) {
753 quants[level][orientation]);
766 pb->
buf[bytes_start] = pad_s;
780 int slice_x, slice_y, skip = 0;
787 for (slice_y = 0; slice_y < s->
num_y; slice_y++) {
788 for (slice_x = 0; slice_x < s->
num_x; slice_x++) {
842 const void *frame_data = transform_dat->
idata;
843 const ptrdiff_t linesize = transform_dat->
istride;
844 const int field = transform_dat->
field;
852 ptrdiff_t pix_stride = linesize >> (s->
bpp - 1);
857 }
else if (field == 2) {
866 for (y = 0; y < p->
height*skip; y+=skip) {
867 for (x = 0; x < p->
width; x++) {
874 const uint16_t *pix = (
const uint16_t *)frame_data + offset;
875 for (y = 0; y < p->
height*skip; y+=skip) {
876 for (x = 0; x < p->
width; x++) {
896 const char *aux_data,
const int header_size,
int field)
899 int64_t max_frame_bytes;
902 for (i = 0; i < 3; i++) {
953 int slice_ceil, sig_size = 256;
957 const int aux_data_size = bitexact ?
sizeof(
"Lavc") :
sizeof(
LIBAVCODEC_IDENT);
958 const int header_size = 100 + aux_data_size;
973 while (sig_size > 255) {
975 if (r_size > slice_ceil) {
989 ret =
encode_frame(s, avpkt, frame, aux_data, header_size, 2);
1009 for (i = 0; i < 3; i++) {
1089 "the specifications, decrease strictness to use it.\n");
1112 }
else if (depth == 10) {
1123 for (i = 0; i < 3; i++) {
1139 for (o = 0; o < 4; o++) {
1165 for (i = 0; i < 116; i++) {
1167 const uint32_t m =
av_log2(qf);
1168 const uint32_t t = (1ULL << (m + 32)) / qf;
1169 const uint32_t
r = (t*qf + qf) & UINT32_MAX;
1170 if (!(qf & (qf - 1))) {
1173 }
else if (r <= 1 << m) {
1190 #define VC2ENC_FLAGS (AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
1192 {
"tolerance",
"Max undershoot in percent", offsetof(
VC2EncContext, tolerance),
AV_OPT_TYPE_DOUBLE, {.dbl = 5.0f}, 0.0f, 45.0f,
VC2ENC_FLAGS,
"tolerance"},
1193 {
"slice_width",
"Slice width", offsetof(
VC2EncContext, slice_width),
AV_OPT_TYPE_INT, {.i64 = 32}, 32, 1024,
VC2ENC_FLAGS,
"slice_width"},
1194 {
"slice_height",
"Slice height", offsetof(
VC2EncContext, slice_height),
AV_OPT_TYPE_INT, {.i64 = 16}, 8, 1024,
VC2ENC_FLAGS,
"slice_height"},
1195 {
"wavelet_depth",
"Transform depth", offsetof(
VC2EncContext, wavelet_depth),
AV_OPT_TYPE_INT, {.i64 = 4}, 1, 5,
VC2ENC_FLAGS,
"wavelet_depth"},
1196 {
"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"},
1197 {
"9_7",
"Deslauriers-Dubuc (9,7)", 0,
AV_OPT_TYPE_CONST, {.i64 =
VC2_TRANSFORM_9_7}, INT_MIN, INT_MAX,
VC2ENC_FLAGS,
"wavelet_idx"},
1198 {
"5_3",
"LeGall (5,3)", 0,
AV_OPT_TYPE_CONST, {.i64 =
VC2_TRANSFORM_5_3}, INT_MIN, INT_MAX,
VC2ENC_FLAGS,
"wavelet_idx"},
1201 {
"qm",
"Custom quantization matrix", offsetof(
VC2EncContext, quant_matrix),
AV_OPT_TYPE_INT, {.i64 =
VC2_QM_DEF}, 0,
VC2_QM_NB,
VC2ENC_FLAGS,
"quant_matrix"},
1217 {
"b",
"600000000" },
1239 .priv_class = &vc2enc_class,
static void encode_parse_info(VC2EncContext *s, enum DiracParseCodes pcode)
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
const char const char void * val
const int32_t ff_dirac_qscale_tab[116]
static int encode_frame(VC2EncContext *s, AVPacket *avpkt, const AVFrame *frame, const char *aux_data, const int header_size, int field)
static void encode_wavelet_transform(VC2EncContext *s)
static void encode_aspect_ratio(VC2EncContext *s)
static int shift(int a, int b)
static av_cold int vc2_encode_init(AVCodecContext *avctx)
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
This structure describes decoded (raw) audio or video data.
av_cold void ff_vc2enc_free_transforms(VC2TransformContext *s)
"Linear transfer characteristics"
TransformArgs transform_args[3]
uint32_t next_parse_offset
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
#define AV_LOG_WARNING
Something somehow does not look correct.
int64_t bit_rate
the average bitrate
#define LIBAVUTIL_VERSION_INT
static void encode_source_params(VC2EncContext *s)
static av_cold int init(AVCodecContext *avctx)
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601 ...
static av_always_inline void put_vc2_ue_uint(PutBitContext *pb, uint32_t val)
enum AVColorRange color_range
MPEG vs JPEG YUV range.
const char * av_default_item_name(void *ptr)
Return the context name.
void avpriv_align_put_bits(PutBitContext *s)
Pad the bitstream with zeros up to the next byte boundary.
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 AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
#define AV_PIX_FMT_YUV420P12
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB)
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
static av_cold int vc2_encode_end(AVCodecContext *avctx)
const uint8_t vc2_qm_flat_tab[][4]
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
static void encode_transform_params(VC2EncContext *s)
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
enum DiracParseCodes last_parse_code
Interface to Dirac Decoder/Encoder.
static av_always_inline int count_vc2_ue_uint(uint32_t val)
static const int base_video_fmts_len
av_cold int ff_vc2enc_init_transforms(VC2TransformContext *s, int p_stride, int p_height, int slice_w, int slice_h)
#define AV_PIX_FMT_YUV422P12
static av_cold int vc2_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet)
static void init_quant_matrix(VC2EncContext *s)
Libavcodec version macros.
static void encode_clean_area(VC2EncContext *s)
const uint8_t vc2_qm_col_tab[][4]
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static uint8_t * put_bits_ptr(PutBitContext *s)
Return the pointer to the byte where the bitstream writer will put the next bit.
ITU-R BT1361 Extended Colour Gamut.
static int count_hq_slice(SliceArgs *slice, int quant_idx)
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.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static void encode_parse_params(VC2EncContext *s)
const uint8_t ff_dirac_default_qmat[7][4][4]
int flags
AV_CODEC_FLAG_*.
const char * name
Name of the codec implementation.
#define AV_PIX_FMT_YUV444P10
static const AVCodecDefault vc2enc_defaults[]
static const AVCodecDefault defaults[]
#define DIRAC_MAX_QUANT_INDEX
static const uint8_t offset[127][2]
static int put_bits_count(PutBitContext *s)
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
static void encode_seq_header(VC2EncContext *s)
SubBand band[DWT_LEVELS][4]
static void skip_put_bytes(PutBitContext *s, int n)
Skip the given number of bytes.
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
static void encode_scan_format(VC2EncContext *s)
#define QUANT(c, mul, add, shift)
int width
picture width / height.
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
static void encode_frame_size(VC2EncContext *s)
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
static void encode_quant_matrix(VC2EncContext *s)
#define FF_ARRAY_ELEMS(a)
the normal 2^n-1 "JPEG" YUV ranges
#define SLICE_REDIST_TOTAL
static void encode_slice_params(VC2EncContext *s)
static void encode_sample_fmt(VC2EncContext *s)
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
uint8_t quant[MAX_DWT_LEVELS][4]
static void encode_frame_rate(VC2EncContext *s)
static void encode_picture_header(VC2EncContext *s)
#define AV_LOG_INFO
Standard information.
static void encode_signal_range(VC2EncContext *s)
static int calc_slice_sizes(VC2EncContext *s)
static int dwt_plane(AVCodecContext *avctx, void *arg)
functionally identical to above
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
main external API structure.
DiracParseCodes
Parse code values:
static void encode_color_spec(VC2EncContext *s)
static int encode_hq_slice(AVCodecContext *avctx, void *arg)
#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...
static int encode_slices(VC2EncContext *s)
#define AV_PIX_FMT_YUV420P10
Describe the class of an AVClass context structure.
void(* vc2_subband_dwt[VC2_TRANSFORMS_NB])(struct VC2TransformContext *t, dwtcoef *data, ptrdiff_t stride, int width, int height)
enum AVColorSpace colorspace
YUV colorspace type.
Rational number (pair of numerator and denominator).
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
#define FF_COMPLIANCE_STRICT
Strictly conform to all the things in the spec no matter what consequences.
static void encode_subband(VC2EncContext *s, PutBitContext *pb, int sx, int sy, SubBand *b, int quant)
int cache[DIRAC_MAX_QUANT_INDEX]
static enum AVPixelFormat pix_fmts[]
#define AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV444P12
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
static enum AVPixelFormat allowed_pix_fmts[]
the normal 219*2^(n-8) "MPEG" YUV ranges
static const VC2BaseVideoFormat base_video_fmts[]
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
static const AVOption vc2enc_options[]
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
static av_always_inline int diff(const uint32_t a, const uint32_t b)
static int rate_control(AVCodecContext *avctx, void *arg)
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.
uint32_t qmagic_lut[116][2]
static const double coeff[2][5]
static void encode_picture_start(VC2EncContext *s)
void avpriv_put_string(PutBitContext *pb, const char *string, int terminate_string)
Put the string string in the bitstream.
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
enum AVFieldOrder field_order
Field order.
int depth
Number of bits in the component.
AVPixelFormat
Pixel format.
This structure stores compressed data.
int strict_std_compliance
strictly follow the standard (MPEG-4, ...).
static const AVClass vc2enc_class