27 #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++) {
253 int i,
index, bit_depth;
354 const uint8_t header_prefix[5] = { 0x00,0x00,0x02,0x80,0x01 };
358 memcpy(buf, header_prefix, 5);
378 ctx->
msip = buf + 0x170;
397 int last_non_zero = 0;
403 for (i = 1; i <= last_index; i++) {
407 int run_level = i - last_non_zero - 1;
408 int rlevel = (slevel<<1)|!!run_level;
426 for (i = 1; i <= last_index; i++) {
431 level = (1-2*
level) * qscale * weight_matrix[i];
433 if (weight_matrix[i] != 8)
437 if (weight_matrix[i] != 32)
443 level = (2*level+1) * qscale * weight_matrix[i];
445 if (weight_matrix[i] != 8)
449 if (weight_matrix[i] != 32)
463 for (i = 0; i < 64; i++)
464 score += (block[i] - qblock[i]) * (block[i] - qblock[i]);
470 int last_non_zero = 0;
473 for (i = 1; i <= last_index; i++) {
477 int run_level = i - last_non_zero - 1;
488 const int bw = 1 << bs;
528 int mb_y = jobnr, mb_x;
531 ctx = ctx->
thread[threadnr];
537 for (mb_x = 0; mb_x < ctx->
m.
mb_width; mb_x++) {
546 for (i = 0; i < 8; i++) {
548 int overflow, nbits,
diff, last_index;
559 av_assert1(nbits < ctx->cid_table->bit_depth + 4);
579 int mb_y = jobnr, mb_x;
580 ctx = ctx->
thread[threadnr];
586 for (mb_x = 0; mb_x < ctx->
m.
mb_width; mb_x++) {
595 for (i = 0; i < 8; i++) {
598 int last_index = ctx->
m.
dct_quantize(&ctx->
m, block, 4&(2*i), qscale, &overflow);
614 for (mb_y = 0; mb_y < ctx->
m.
mb_height; mb_y++) {
618 for (mb_x = 0; mb_x < ctx->
m.
mb_width; mb_x++) {
625 offset += thread_size;
632 int mb_y = jobnr, mb_x, x, y;
633 int partial_last_row = (mb_y == ctx->
m.
mb_height - 1) &&
636 ctx = ctx->
thread[threadnr];
639 for (mb_x = 0; mb_x < ctx->
m.
mb_width; ++mb_x, pix += 16) {
644 if (!partial_last_row && mb_x * 16 <= avctx->
width - 16) {
651 for (y = 0; y < bh; y++) {
652 for (x = 0; x < bw; x++) {
659 varc = (varc - (((unsigned)sum * sum) >> 8) + 128) >> 8;
665 int const linesize = ctx->
m.
linesize >> 1;
666 for (mb_x = 0; mb_x < ctx->
m.
mb_width; ++mb_x) {
667 uint16_t *pix = (uint16_t*)ctx->
thread[0]->
src[0] + ((mb_y << 4) * linesize) + (mb_x << 4);
674 for (i = 0; i < 16; ++i) {
675 for (j = 0; j < 16; ++j) {
677 int const sample = (unsigned)pix[j] >> 6;
695 int lambda, up_step, down_step;
696 int last_lower = INT_MAX, last_higher = 0;
699 for (q = 1; q < avctx->
qmax; q++) {
709 if (lambda == last_higher) {
715 unsigned min = UINT_MAX;
718 for (q = 1; q < avctx->
qmax; q++) {
730 bits = (bits+31)&~31;
741 if (bits < ctx->frame_bits) {
742 last_lower =
FFMIN(lambda, last_lower);
743 if (last_higher != 0)
744 lambda = (lambda+last_higher)>>1;
747 down_step =
FFMIN((int64_t)down_step*5, INT_MAX);
749 lambda =
FFMAX(1, lambda);
750 if (lambda == last_lower)
753 last_higher =
FFMAX(lambda, last_higher);
754 if (last_lower != INT_MAX)
755 lambda = (lambda+last_lower)>>1;
756 else if ((int64_t)lambda + up_step > INT_MAX)
760 up_step =
FFMIN((int64_t)up_step*5, INT_MAX);
775 int last_lower = INT_MAX;
788 bits = (bits+31)&~31;
794 if (bits < ctx->frame_bits) {
797 if (last_higher == qscale - 1) {
798 qscale = last_higher;
801 last_lower =
FFMIN(qscale, last_lower);
802 if (last_higher != 0)
803 qscale = (qscale+last_higher)>>1;
805 qscale -= down_step++;
810 if (last_lower == qscale + 1)
812 last_higher =
FFMAX(qscale, last_higher);
813 if (last_lower != INT_MAX)
814 qscale = (qscale+last_lower)>>1;
827 #define BUCKET_BITS 8
828 #define RADIX_PASSES 4
829 #define NBUCKETS (1 << BUCKET_BITS)
841 memset(buckets, 0,
sizeof(buckets[0][0]) *
RADIX_PASSES * NBUCKETS);
842 for (i = 0; i <
size; i++) {
843 int v = data[i].
value;
852 for (i = NBUCKETS - 1; i >= 0; i--)
853 buckets[j][i] = offset -= buckets[j][i];
862 for (i = 0; i <
size; i++) {
864 int pos = buckets[v]++;
924 for (i = 0; i < 3; i++) {
955 for (i = 0; i < 3; i++) {
969 "picture could not fit ratecontrol constraints, increase qmax\n");
993 goto encode_coding_unit;
1050 .priv_class = &
class,