26 #define RC_VARIANCE 1 // use variance or ssd for fast rc
37 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
38 #define DNX10BIT_QMAT_SHIFT 18 // The largest value that will not lead to overflow for 10bit samples.
52 #define LAMBDA_FRAC_BITS 10
57 for (i = 0; i < 4; i++) {
58 block[0] = pixels[0]; block[1] = pixels[1];
59 block[2] = pixels[2]; block[3] = pixels[3];
60 block[4] = pixels[4]; block[5] = pixels[5];
61 block[6] = pixels[6]; block[7] = pixels[7];
65 memcpy(block, block - 8,
sizeof(*block) * 8);
66 memcpy(block + 8, block - 16,
sizeof(*block) * 8);
67 memcpy(block + 16, block - 24,
sizeof(*block) * 8);
68 memcpy(block + 24, block - 32,
sizeof(*block) * 8);
74 const uint16_t* pixels16 = (
const uint16_t*)pixels;
77 for (i = 0; i < 4; i++) {
78 block[0] = pixels16[0]; block[1] = pixels16[1];
79 block[2] = pixels16[2]; block[3] = pixels16[3];
80 block[4] = pixels16[4]; block[5] = pixels16[5];
81 block[6] = pixels16[6]; block[7] = pixels16[7];
82 pixels16 += line_size;
85 memcpy(block, block - 8,
sizeof(*block) * 8);
86 memcpy(block + 8, block - 16,
sizeof(*block) * 8);
87 memcpy(block + 16, block - 24,
sizeof(*block) * 8);
88 memcpy(block + 24, block - 32,
sizeof(*block) * 8);
92 int n,
int qscale,
int *overflow)
96 int last_non_zero = 0;
102 block[0] = (block[0] + 2) >> 2;
104 for (i = 1; i < 64; ++i) {
105 int j = scantable[i];
106 int sign = block[j] >> 31;
107 int level = (block[j] ^ sign) - sign;
109 block[j] = (level ^ sign) - sign;
114 return last_non_zero;
129 for (level = -max_level; level < max_level; level++) {
130 for (run = 0; run < 2; run++) {
131 int index = (level<<1)|run;
136 offset = (alevel-1)>>6;
139 for (j = 0; j < 257; j++) {
161 for (i = 0; i < 62; i++) {
175 uint16_t weight_matrix[64] = {1,};
186 for (i = 1; i < 64; i++) {
192 for (i = 1; i < 64; i++) {
199 for (qscale = 1; qscale <= ctx->
m.
avctx->
qmax; qscale++) {
200 for (i = 0; i < 64; i++) {
208 for (qscale = 1; qscale <= ctx->
m.
avctx->
qmax; qscale++) {
209 for (i = 1; i < 64; i++) {
269 av_log(avctx,
AV_LOG_ERROR,
"video parameters incompatible with DNxHD. Valid DNxHD profiles:\n");
358 static const uint8_t header_prefix[5] = { 0x00,0x00,0x02,0x80,0x01 };
362 memcpy(buf, header_prefix, 5);
382 ctx->
msip = buf + 0x170;
401 int last_non_zero = 0;
407 for (i = 1; i <= last_index; i++) {
411 int run_level = i - last_non_zero - 1;
412 int rlevel = (slevel<<1)|!!run_level;
430 for (i = 1; i <= last_index; i++) {
435 level = (1-2*
level) * qscale * weight_matrix[i];
437 if (weight_matrix[i] != 8)
441 if (weight_matrix[i] != 32)
447 level = (2*level+1) * qscale * weight_matrix[i];
449 if (weight_matrix[i] != 8)
453 if (weight_matrix[i] != 32)
467 for (i = 0; i < 64; i++)
468 score += (block[i] - qblock[i]) * (block[i] - qblock[i]);
474 int last_non_zero = 0;
477 for (i = 1; i <= last_index; i++) {
481 int run_level = i - last_non_zero - 1;
492 const int bw = 1 << bs;
525 const static uint8_t component[8]={0,0,1,2,0,0,1,2};
532 int mb_y = jobnr, mb_x;
535 ctx = ctx->
thread[threadnr];
541 for (mb_x = 0; mb_x < ctx->
m.
mb_width; mb_x++) {
550 for (i = 0; i < 8; i++) {
551 int16_t *src_block = ctx->
blocks[i];
552 int overflow, nbits, diff, last_index;
583 int mb_y = jobnr, mb_x;
584 ctx = ctx->
thread[threadnr];
590 for (mb_x = 0; mb_x < ctx->
m.
mb_width; mb_x++) {
599 for (i = 0; i < 8; i++) {
602 int last_index = ctx->
m.
dct_quantize(&ctx->
m, block, 4&(2*i), qscale, &overflow);
618 for (mb_y = 0; mb_y < ctx->
m.
mb_height; mb_y++) {
622 for (mb_x = 0; mb_x < ctx->
m.
mb_width; mb_x++) {
629 offset += thread_size;
636 int mb_y = jobnr, mb_x, x,
y;
637 int partial_last_row = (mb_y == ctx->
m.
mb_height - 1) &&
640 ctx = ctx->
thread[threadnr];
643 for (mb_x = 0; mb_x < ctx->
m.
mb_width; ++mb_x, pix += 16) {
648 if (!partial_last_row && mb_x * 16 <= avctx->
width - 16) {
655 for (y = 0; y < bh; y++) {
656 for (x = 0; x < bw; x++) {
663 varc = (varc - (((unsigned)sum * sum) >> 8) + 128) >> 8;
669 int const linesize = ctx->
m.
linesize >> 1;
670 for (mb_x = 0; mb_x < ctx->
m.
mb_width; ++mb_x) {
671 uint16_t *pix = (uint16_t*)ctx->
thread[0]->
src[0] + ((mb_y << 4) * linesize) + (mb_x << 4);
678 for (i = 0; i < 16; ++i) {
679 for (j = 0; j < 16; ++j) {
681 int const sample = (unsigned)pix[j] >> 6;
699 int lambda, up_step, down_step;
700 int last_lower = INT_MAX, last_higher = 0;
703 for (q = 1; q < avctx->
qmax; q++) {
713 if (lambda == last_higher) {
719 unsigned min = UINT_MAX;
722 for (q = 1; q < avctx->
qmax; q++) {
734 bits = (bits+31)&~31;
745 if (bits < ctx->frame_bits) {
746 last_lower =
FFMIN(lambda, last_lower);
747 if (last_higher != 0)
748 lambda = (lambda+last_higher)>>1;
751 down_step =
FFMIN((int64_t)down_step*5, INT_MAX);
753 lambda =
FFMAX(1, lambda);
754 if (lambda == last_lower)
757 last_higher =
FFMAX(lambda, last_higher);
758 if (last_lower != INT_MAX)
759 lambda = (lambda+last_lower)>>1;
760 else if ((int64_t)lambda + up_step > INT_MAX)
764 up_step =
FFMIN((int64_t)up_step*5, INT_MAX);
779 int last_lower = INT_MAX;
792 bits = (bits+31)&~31;
798 if (bits < ctx->frame_bits) {
801 if (last_higher == qscale - 1) {
802 qscale = last_higher;
805 last_lower =
FFMIN(qscale, last_lower);
806 if (last_higher != 0)
807 qscale = (qscale+last_higher)>>1;
809 qscale -= down_step++;
814 if (last_lower == qscale + 1)
816 last_higher =
FFMAX(qscale, last_higher);
817 if (last_lower != INT_MAX)
818 qscale = (qscale+last_lower)>>1;
831 #define BUCKET_BITS 8
832 #define RADIX_PASSES 4
833 #define NBUCKETS (1 << BUCKET_BITS)
845 memset(buckets, 0,
sizeof(buckets[0][0]) *
RADIX_PASSES * NBUCKETS);
846 for (i = 0; i <
size; i++) {
856 for (i = NBUCKETS - 1; i >= 0; i--)
857 buckets[j][i] = offset -= buckets[j][i];
866 for (i = 0; i <
size; i++) {
868 int pos = buckets[
v]++;
954 for (i = 0; i < 3; i++) {
968 "picture could not fit ratecontrol constraints, increase qmax\n");
992 goto encode_coding_unit;