41 #define DNX10BIT_QMAT_SHIFT 18
42 #define RC_VARIANCE 1 // use variance or ssd for fast rc
43 #define LAMBDA_FRAC_BITS 10
45 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
47 {
"nitris_compat",
"encode with Avid Nitris compatibility",
49 {
"ibias",
"intra quant bias",
51 { .i64 = 0 }, INT_MIN, INT_MAX,
VE },
67 for (i = 0; i < 4; i++) {
79 memcpy(block, block - 8,
sizeof(*block) * 8);
80 memcpy(block + 8, block - 16,
sizeof(*block) * 8);
81 memcpy(block + 16, block - 24,
sizeof(*block) * 8);
82 memcpy(block + 24, block - 32,
sizeof(*block) * 8);
90 memcpy(block + 0 * 8, pixels + 0 * line_size, 8 *
sizeof(*block));
91 memcpy(block + 7 * 8, pixels + 0 * line_size, 8 *
sizeof(*block));
92 memcpy(block + 1 * 8, pixels + 1 * line_size, 8 *
sizeof(*block));
93 memcpy(block + 6 * 8, pixels + 1 * line_size, 8 *
sizeof(*block));
94 memcpy(block + 2 * 8, pixels + 2 * line_size, 8 *
sizeof(*block));
95 memcpy(block + 5 * 8, pixels + 2 * line_size, 8 *
sizeof(*block));
96 memcpy(block + 3 * 8, pixels + 3 * line_size, 8 *
sizeof(*block));
97 memcpy(block + 4 * 8, pixels + 3 * line_size, 8 *
sizeof(*block));
101 int n,
int qscale,
int *overflow)
105 int last_non_zero = 0;
111 block[0] = (block[0] + 2) >> 2;
113 for (i = 1; i < 64; ++i) {
114 int j = scantable[i];
116 int level = (block[j] ^ sign) - sign;
118 block[j] = (level ^ sign) - sign;
126 scantable, last_non_zero);
128 return last_non_zero;
147 for (level = -max_level; level < max_level; level++) {
148 for (run = 0; run < 2; run++) {
149 int index = (level << 1) | run;
154 offset = (alevel - 1) >> 6;
155 alevel -= offset << 6;
157 for (j = 0; j < 257; j++) {
181 for (i = 0; i < 62; i++) {
195 uint16_t weight_matrix[64] = { 1, };
205 (ctx->
m.
avctx->
qmax + 1), 64 * 2 *
sizeof(uint16_t),
208 (ctx->
m.
avctx->
qmax + 1), 64 * 2 *
sizeof(uint16_t),
212 for (i = 1; i < 64; i++) {
219 for (i = 1; i < 64; i++) {
227 for (qscale = 1; qscale <= ctx->
m.
avctx->
qmax; qscale++) {
228 for (i = 0; i < 64; i++) {
239 for (qscale = 1; qscale <= ctx->
m.
avctx->
qmax; qscale++) {
240 for (i = 1; i < 64; i++) {
255 (qscale * luma_weight_table[i]);
257 (qscale * chroma_weight_table[i]);
302 "pixel format is incompatible with DNxHD\n");
309 "video parameters incompatible with DNxHD. Valid DNxHD profiles:\n");
358 #if FF_API_QUANT_BIAS
387 #if FF_API_CODED_FRAME
399 if (avctx->
qmax <= 1) {
418 static const uint8_t header_prefix[5] = { 0x00, 0x00, 0x02, 0x80, 0x01 };
422 memcpy(buf, header_prefix, 5);
442 ctx->
msip = buf + 0x170;
457 av_mod_uintp2(diff, nbits));
462 int last_index,
int n)
464 int last_non_zero = 0;
470 for (i = 1; i <= last_index; i++) {
474 int run_level = i - last_non_zero - 1;
475 int rlevel = (slevel << 1) | !!run_level;
488 int qscale,
int last_index)
497 for (i = 1; i <= last_index; i++) {
502 level = (1 - 2 *
level) * qscale * weight_matrix[i];
504 if (weight_matrix[i] != 8)
508 if (weight_matrix[i] != 32)
514 level = (2 * level + 1) * qscale * weight_matrix[i];
516 if (weight_matrix[i] != 8)
520 if (weight_matrix[i] != 32)
534 for (i = 0; i < 64; i++)
535 score += (block[i] - qblock[i]) * (block[i] - qblock[i]);
542 int last_non_zero = 0;
545 for (i = 1; i <= last_index; i++) {
549 int run_level = i - last_non_zero - 1;
550 bits += ctx->
vlc_bits[(level << 1) |
551 !!run_level] + ctx->
run_bits[run_level];
562 const int bw = 1 << bs;
564 ((mb_y << 4) * ctx->
m.
linesize) + (mb_x << bs + 1);
611 const static uint8_t component[8]={0,0,1,2,0,0,1,2};
616 int jobnr,
int threadnr)
619 int mb_y = jobnr, mb_x;
622 ctx = ctx->
thread[threadnr];
628 for (mb_x = 0; mb_x < ctx->
m.
mb_width; mb_x++) {
637 for (i = 0; i < 8; i++) {
638 int16_t *src_block = ctx->
blocks[i];
639 int overflow, nbits,
diff, last_index;
642 memcpy(
block, src_block, 64 *
sizeof(*
block));
665 ctx->
mb_rc[qscale][
mb].
bits = ac_bits + dc_bits + 12 +
672 int jobnr,
int threadnr)
675 int mb_y = jobnr, mb_x;
676 ctx = ctx->
thread[threadnr];
683 for (mb_x = 0; mb_x < ctx->
m.
mb_width; mb_x++) {
692 for (i = 0; i < 8; i++) {
712 for (mb_y = 0; mb_y < ctx->
m.
mb_height; mb_y++) {
716 for (mb_x = 0; mb_x < ctx->
m.
mb_width; mb_x++) {
723 offset += thread_size;
728 int jobnr,
int threadnr)
731 int mb_y = jobnr, mb_x, x, y;
732 int partial_last_row = (mb_y == ctx->
m.
mb_height - 1) &&
735 ctx = ctx->
thread[threadnr];
738 for (mb_x = 0; mb_x < ctx->
m.
mb_width; ++mb_x, pix += 16) {
743 if (!partial_last_row && mb_x * 16 <= avctx->
width - 16) {
750 for (y = 0; y < bh; y++) {
751 for (x = 0; x < bw; x++) {
758 varc = (varc - (((unsigned) sum * sum) >> 8) + 128) >> 8;
764 int const linesize = ctx->
m.
linesize >> 1;
765 for (mb_x = 0; mb_x < ctx->
m.
mb_width; ++mb_x) {
766 uint16_t *pix = (uint16_t *)ctx->
thread[0]->
src[0] +
767 ((mb_y << 4) * linesize) + (mb_x << 4);
776 for (i = 0; i < bh; ++i) {
777 for (j = 0; j < bw; ++j) {
779 int const sample = (unsigned) pix[j] >> 6;
797 int lambda, up_step, down_step;
798 int last_lower = INT_MAX, last_higher = 0;
801 for (q = 1; q < avctx->
qmax; q++) {
812 if (lambda == last_higher) {
818 unsigned min = UINT_MAX;
821 for (q = 1; q < avctx->
qmax; q++) {
833 bits = (bits + 31) & ~31;
845 if (bits < ctx->frame_bits) {
846 last_lower =
FFMIN(lambda, last_lower);
847 if (last_higher != 0)
848 lambda = (lambda+last_higher)>>1;
851 down_step =
FFMIN((int64_t)down_step*5, INT_MAX);
853 lambda =
FFMAX(1, lambda);
854 if (lambda == last_lower)
857 last_higher =
FFMAX(lambda, last_higher);
858 if (last_lower != INT_MAX)
859 lambda = (lambda+last_lower)>>1;
860 else if ((int64_t)lambda + up_step > INT_MAX)
864 up_step =
FFMIN((int64_t)up_step*5, INT_MAX);
879 int last_lower = INT_MAX;
893 bits = (bits+31)&~31;
901 if (bits < ctx->frame_bits) {
904 if (last_higher == qscale - 1) {
905 qscale = last_higher;
908 last_lower =
FFMIN(qscale, last_lower);
909 if (last_higher != 0)
910 qscale = (qscale + last_higher) >> 1;
912 qscale -= down_step++;
917 if (last_lower == qscale + 1)
919 last_higher =
FFMAX(qscale, last_higher);
920 if (last_lower != INT_MAX)
921 qscale = (qscale + last_lower) >> 1;
934 #define BUCKET_BITS 8
935 #define RADIX_PASSES 4
936 #define NBUCKETS (1 << BUCKET_BITS)
949 memset(buckets, 0,
sizeof(buckets[0][0]) *
RADIX_PASSES * NBUCKETS);
950 for (i = 0; i <
size; i++) {
951 int v = data[i].
value;
960 for (i = NBUCKETS - 1; i >= 0; i--)
961 buckets[j][i] = offset -= buckets[j][i];
971 for (i = 0; i <
size; i++) {
973 int pos = buckets[v]++;
1045 #if FF_API_CODED_FRAME
1068 for (i = 0; i < 3; i++) {
1082 "picture could not fit ratecontrol constraints, increase qmax\n");
1098 memset(buf + 640 + offset, 0,
1107 goto encode_coding_unit;
1110 #if FF_API_CODED_FRAME
static av_always_inline int dnxhd_ssd_block(int16_t *qblock, int16_t *block)
#define MASK_ABS(mask, level)
static void radix_count(const RCCMPEntry *data, int size, int buckets[RADIX_PASSES][NBUCKETS])
const char const char void * val
attribute_deprecated int intra_quant_bias
static av_always_inline void dnxhd_get_blocks(DNXHDEncContext *ctx, int mb_x, int mb_y)
static int shift(int a, int b)
This structure describes decoded (raw) audio or video data.
#define FF_ALLOCZ_ARRAY_OR_GOTO(ctx, p, nelem, elsize, label)
int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
ptrdiff_t const GLvoid * data
void(* clear_block)(int16_t *block)
#define AV_CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
const uint8_t * luma_weight
#define LIBAVUTIL_VERSION_INT
static av_cold int init(AVCodecContext *avctx)
uint16_t(* q_chroma_intra_matrix16)[2][64]
const CIDEntry ff_dnxhd_cid_table[]
const uint16_t * run_codes
static const AVClass dnxhd_class
void ff_convert_matrix(MpegEncContext *s, int(*qmat)[64], uint16_t(*qmat16)[2][64], const uint16_t *quant_matrix, int bias, int qmin, int qmax, int intra)
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
static av_always_inline void dnxhd_encode_dc(DNXHDEncContext *ctx, int diff)
static av_cold int dnxhd_init_vlc(DNXHDEncContext *ctx)
int ff_dnxhd_find_cid(AVCodecContext *avctx, int bit_depth)
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
int mb_num
number of MBs of a picture
av_cold void ff_fdctdsp_init(FDCTDSPContext *c, AVCodecContext *avctx)
struct DNXHDEncContext * thread[MAX_THREADS]
int av_log2_16bit(unsigned v)
static av_always_inline int dnxhd_calc_ac_bits(DNXHDEncContext *ctx, int16_t *block, int last_index)
int h263_aic
Advanded INTRA Coding (AIC)
Macro definitions for various function/variable attributes.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#define av_assert0(cond)
assert() equivalent, that is always enabled.
void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last)
Permute an 8x8 block according to permuatation.
av_cold void ff_mpegvideoencdsp_init(MpegvideoEncDSPContext *c, AVCodecContext *avctx)
void(* get_pixels_8x4_sym)(int16_t *, const uint8_t *, ptrdiff_t)
void(* get_pixels)(int16_t *block, const uint8_t *pixels, ptrdiff_t line_size)
static int get_bucket(int value, int shift)
static av_cold int end(AVCodecContext *avctx)
static int dnxhd_encode_fast(AVCodecContext *avctx, DNXHDEncContext *ctx)
int(* q_chroma_intra_matrix)[64]
static void radix_sort_pass(RCCMPEntry *dst, const RCCMPEntry *data, int size, int buckets[NBUCKETS], int pass)
const uint8_t * scantable
int interlaced_frame
The content of the picture is interlaced.
av_cold void ff_mpv_idct_init(MpegEncContext *s)
int mb_height
number of MBs horizontally & vertically
static av_cold int dnxhd_init_qmat(DNXHDEncContext *ctx, int lbias, int cbias)
unsigned int coding_unit_size
high precision timer, useful to profile code
static int first_field(const struct video_data *s)
static av_cold int dnxhd_encode_end(AVCodecContext *avctx)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
int(* q_intra_matrix)[64]
precomputed matrix (combine qscale and DCT renorm)
static av_always_inline int dnxhd_switch_matrix(DNXHDEncContext *ctx, int i)
const uint16_t * ac_codes
static int dnxhd_calc_bits_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int ff_dnxhd_get_cid_table(int cid)
int last_dc[3]
last DC values for MPEG1
static int dnxhd_find_qscale(DNXHDEncContext *ctx)
int qmax
maximum quantizer
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static av_always_inline void dnxhd_unquantize_c(DNXHDEncContext *ctx, int16_t *block, int n, int qscale, int last_index)
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
int flags
AV_CODEC_FLAG_*.
static int dnxhd_write_header(AVCodecContext *avctx, uint8_t *buf)
MpegvideoEncDSPContext mpvencdsp
const char * name
Name of the codec implementation.
static const uint8_t offset[127][2]
av_cold void ff_pixblockdsp_init(PixblockDSPContext *c, AVCodecContext *avctx)
int flags
A combination of AV_PKT_FLAG values.
static int put_bits_count(PutBitContext *s)
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
const uint8_t * chroma_weight
common internal API header
static const AVOption options[]
static void dnxhd_8bit_get_pixels_8x4_sym(int16_t *av_restrict block, const uint8_t *pixels, ptrdiff_t line_size)
enum AVPictureType pict_type
Picture type of the frame.
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
static const AVCodecDefault dnxhd_defaults[]
MpegEncContext m
Used for quantization dsp functions.
static av_always_inline void dnxhd_encode_block(DNXHDEncContext *ctx, int16_t *block, int last_index, int n)
int width
picture width / height.
int(* pix_sum)(uint8_t *pix, int line_size)
GLsizei GLboolean const GLfloat * value
static av_always_inline void dnxhd_10bit_get_pixels_8x4_sym(int16_t *av_restrict block, const uint8_t *pixels, ptrdiff_t line_size)
uint16_t(* q_intra_matrix16)[2][64]
identical to the above but for MMX & these are not permutated, second 64 entries are bias ...
uint16_t(* qmatrix_l16)[2][64]
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
uint8_t * msip
Macroblock Scan Indexes Payload.
uint8_t idct_permutation[64]
IDCT input permutation.
int mb_decision
macroblock decision mode
void ff_dnxhd_print_profiles(AVCodecContext *avctx, int loglevel)
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
int ff_dct_quantize_c(MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow)
static void radix_sort(RCCMPEntry *data, int size)
static int dnxhd_encode_rdo(AVCodecContext *avctx, DNXHDEncContext *ctx)
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Libavcodec external API header.
static int dnxhd_10bit_dct_quantize(MpegEncContext *ctx, int16_t *block, int n, int qscale, int *overflow)
ptrdiff_t linesize
line size, in bytes, may be different from width
av_cold void ff_blockdsp_init(BlockDSPContext *c, AVCodecContext *avctx)
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
static int dnxhd_encode_picture(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
void(* fdct)(int16_t *block)
main external API structure.
ScanTable intra_scantable
#define FF_DEFAULT_QUANT_BIAS
int ff_dct_encode_init(MpegEncContext *s)
static av_cold int dnxhd_init_rc(DNXHDEncContext *ctx)
void ff_dnxhdenc_init_x86(DNXHDEncContext *ctx)
Describe the class of an AVClass context structure.
int(* pix_norm1)(uint8_t *pix, int line_size)
#define FF_MB_DECISION_RD
rate distortion
const uint8_t ff_zigzag_direct[64]
ptrdiff_t uvlinesize
line size, for chroma in bytes, may be different from width
static void dnxhd_setup_threads_slices(DNXHDEncContext *ctx)
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
const CIDEntry * cid_table
uint16_t(* qmatrix_c16)[2][64]
static enum AVPixelFormat pix_fmts[]
static void dnxhd_load_picture(DNXHDEncContext *ctx, const AVFrame *frame)
#define AV_PIX_FMT_YUV422P10
#define DNX10BIT_QMAT_SHIFT
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
struct AVCodecContext * avctx
PutBitContext pb
bit output
static int dnxhd_mb_var_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
static int dnxhd_encode_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
static av_cold int dnxhd_encode_init(AVCodecContext *avctx)
#define FF_DISABLE_DEPRECATION_WARNINGS
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
static unsigned bit_depth(uint64_t mask)
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
static av_always_inline int diff(const uint32_t a, const uint32_t b)
#define FF_ENABLE_DEPRECATION_WARNINGS
int top_field_first
If the content is interlaced, is top field displayed first.
int(* execute2)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg, int jobnr, int threadnr), void *arg2, int *ret, int count)
The codec may call this to execute several independent things.
int key_frame
1 -> keyframe, 0-> not
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
const CIDEntry * cid_table
#define LOCAL_ALIGNED_16(t, v,...)
#define av_malloc_array(a, b)
int(* dct_quantize)(struct MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow)
AVPixelFormat
Pixel format.
This structure stores compressed data.
#define FF_ALLOCZ_OR_GOTO(ctx, p, size, label)
enum idct_permutation_type perm_type
void(* idct)(int16_t *block)