26 #define RC_VARIANCE 1 // use variance or ssd for fast rc
38 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
39 #define DNX10BIT_QMAT_SHIFT 18 // The largest value that will not lead to overflow for 10bit samples.
53 #define LAMBDA_FRAC_BITS 10
58 for (i = 0; i < 4; i++) {
59 block[0] = pixels[0]; block[1] = pixels[1];
60 block[2] = pixels[2]; block[3] = pixels[3];
61 block[4] = pixels[4]; block[5] = pixels[5];
62 block[6] = pixels[6]; block[7] = pixels[7];
66 memcpy(block, block - 8,
sizeof(*block) * 8);
67 memcpy(block + 8, block - 16,
sizeof(*block) * 8);
68 memcpy(block + 16, block - 24,
sizeof(*block) * 8);
69 memcpy(block + 24, block - 32,
sizeof(*block) * 8);
75 const uint16_t* pixels16 = (
const uint16_t*)pixels;
78 for (i = 0; i < 4; i++) {
79 block[0] = pixels16[0]; block[1] = pixels16[1];
80 block[2] = pixels16[2]; block[3] = pixels16[3];
81 block[4] = pixels16[4]; block[5] = pixels16[5];
82 block[6] = pixels16[6]; block[7] = pixels16[7];
83 pixels16 += line_size;
86 memcpy(block, block - 8,
sizeof(*block) * 8);
87 memcpy(block + 8, block - 16,
sizeof(*block) * 8);
88 memcpy(block + 16, block - 24,
sizeof(*block) * 8);
89 memcpy(block + 24, block - 32,
sizeof(*block) * 8);
93 int n,
int qscale,
int *overflow)
97 int last_non_zero = 0;
103 block[0] = (block[0] + 2) >> 2;
105 for (i = 1; i < 64; ++i) {
106 int j = scantable[i];
107 int sign = block[j] >> 31;
108 int level = (block[j] ^ sign) - sign;
110 block[j] = (level ^ sign) - sign;
115 return last_non_zero;
130 for (level = -max_level; level < max_level; level++) {
131 for (run = 0; run < 2; run++) {
132 int index = (level<<1)|run;
137 offset = (alevel-1)>>6;
140 for (j = 0; j < 257; j++) {
162 for (i = 0; i < 62; i++) {
176 uint16_t weight_matrix[64] = {1,};
187 for (i = 1; i < 64; i++) {
193 for (i = 1; i < 64; i++) {
200 for (qscale = 1; qscale <= ctx->
m.
avctx->
qmax; qscale++) {
201 for (i = 0; i < 64; i++) {
209 for (qscale = 1; qscale <= ctx->
m.
avctx->
qmax; qscale++) {
210 for (i = 1; i < 64; i++) {
270 av_log(avctx,
AV_LOG_ERROR,
"video parameters incompatible with DNxHD. Valid DNxHD profiles:\n");
345 if (avctx->
qmax <= 1) {
364 static const uint8_t header_prefix[5] = { 0x00,0x00,0x02,0x80,0x01 };
368 memcpy(buf, header_prefix, 5);
388 ctx->
msip = buf + 0x170;
407 int last_non_zero = 0;
413 for (i = 1; i <= last_index; i++) {
417 int run_level = i - last_non_zero - 1;
418 int rlevel = (slevel<<1)|!!run_level;
436 for (i = 1; i <= last_index; i++) {
441 level = (1-2*
level) * qscale * weight_matrix[i];
443 if (weight_matrix[i] != 8)
447 if (weight_matrix[i] != 32)
453 level = (2*level+1) * qscale * weight_matrix[i];
455 if (weight_matrix[i] != 8)
459 if (weight_matrix[i] != 32)
473 for (i = 0; i < 64; i++)
474 score += (block[i] - qblock[i]) * (block[i] - qblock[i]);
480 int last_non_zero = 0;
483 for (i = 1; i <= last_index; i++) {
487 int run_level = i - last_non_zero - 1;
498 const int bw = 1 << bs;
531 const static uint8_t component[8]={0,0,1,2,0,0,1,2};
538 int mb_y = jobnr, mb_x;
541 ctx = ctx->
thread[threadnr];
547 for (mb_x = 0; mb_x < ctx->
m.
mb_width; mb_x++) {
556 for (i = 0; i < 8; i++) {
557 int16_t *src_block = ctx->
blocks[i];
558 int overflow, nbits, diff, last_index;
589 int mb_y = jobnr, mb_x;
590 ctx = ctx->
thread[threadnr];
596 for (mb_x = 0; mb_x < ctx->
m.
mb_width; mb_x++) {
605 for (i = 0; i < 8; i++) {
608 int last_index = ctx->
m.
dct_quantize(&ctx->
m, block, 4&(2*i), qscale, &overflow);
624 for (mb_y = 0; mb_y < ctx->
m.
mb_height; mb_y++) {
628 for (mb_x = 0; mb_x < ctx->
m.
mb_width; mb_x++) {
635 offset += thread_size;
642 int mb_y = jobnr, mb_x, x,
y;
643 int partial_last_row = (mb_y == ctx->
m.
mb_height - 1) &&
646 ctx = ctx->
thread[threadnr];
649 for (mb_x = 0; mb_x < ctx->
m.
mb_width; ++mb_x, pix += 16) {
654 if (!partial_last_row && mb_x * 16 <= avctx->
width - 16) {
661 for (y = 0; y < bh; y++) {
662 for (x = 0; x < bw; x++) {
669 varc = (varc - (((unsigned)sum * sum) >> 8) + 128) >> 8;
675 int const linesize = ctx->
m.
linesize >> 1;
676 for (mb_x = 0; mb_x < ctx->
m.
mb_width; ++mb_x) {
677 uint16_t *pix = (uint16_t*)ctx->
thread[0]->
src[0] + ((mb_y << 4) * linesize) + (mb_x << 4);
684 for (i = 0; i < 16; ++i) {
685 for (j = 0; j < 16; ++j) {
687 int const sample = (unsigned)pix[j] >> 6;
705 int lambda, up_step, down_step;
706 int last_lower = INT_MAX, last_higher = 0;
709 for (q = 1; q < avctx->
qmax; q++) {
719 if (lambda == last_higher) {
725 unsigned min = UINT_MAX;
728 for (q = 1; q < avctx->
qmax; q++) {
740 bits = (bits+31)&~31;
751 if (bits < ctx->frame_bits) {
752 last_lower =
FFMIN(lambda, last_lower);
753 if (last_higher != 0)
754 lambda = (lambda+last_higher)>>1;
757 down_step =
FFMIN((int64_t)down_step*5, INT_MAX);
759 lambda =
FFMAX(1, lambda);
760 if (lambda == last_lower)
763 last_higher =
FFMAX(lambda, last_higher);
764 if (last_lower != INT_MAX)
765 lambda = (lambda+last_lower)>>1;
766 else if ((int64_t)lambda + up_step > INT_MAX)
770 up_step =
FFMIN((int64_t)up_step*5, INT_MAX);
785 int last_lower = INT_MAX;
798 bits = (bits+31)&~31;
804 if (bits < ctx->frame_bits) {
807 if (last_higher == qscale - 1) {
808 qscale = last_higher;
811 last_lower =
FFMIN(qscale, last_lower);
812 if (last_higher != 0)
813 qscale = (qscale+last_higher)>>1;
815 qscale -= down_step++;
820 if (last_lower == qscale + 1)
822 last_higher =
FFMAX(qscale, last_higher);
823 if (last_lower != INT_MAX)
824 qscale = (qscale+last_lower)>>1;
837 #define BUCKET_BITS 8
838 #define RADIX_PASSES 4
839 #define NBUCKETS (1 << BUCKET_BITS)
851 memset(buckets, 0,
sizeof(buckets[0][0]) *
RADIX_PASSES * NBUCKETS);
852 for (i = 0; i <
size; i++) {
862 for (i = NBUCKETS - 1; i >= 0; i--)
863 buckets[j][i] = offset -= buckets[j][i];
872 for (i = 0; i <
size; i++) {
874 int pos = buckets[
v]++;
960 for (i = 0; i < 3; i++) {
974 "picture could not fit ratecontrol constraints, increase qmax\n");
998 goto encode_coding_unit;