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",
64 for (i = 0; i < 4; i++) {
76 memcpy(block, block - 8,
sizeof(*block) * 8);
77 memcpy(block + 8, block - 16,
sizeof(*block) * 8);
78 memcpy(block + 16, block - 24,
sizeof(*block) * 8);
79 memcpy(block + 24, block - 32,
sizeof(*block) * 8);
88 const uint16_t* pixels16 = (
const uint16_t*)pixels;
91 for (i = 0; i < 4; i++) {
92 block[0] = pixels16[0]; block[1] = pixels16[1];
93 block[2] = pixels16[2]; block[3] = pixels16[3];
94 block[4] = pixels16[4]; block[5] = pixels16[5];
95 block[6] = pixels16[6]; block[7] = pixels16[7];
96 pixels16 += line_size;
99 memcpy(block, block - 8,
sizeof(*block) * 8);
100 memcpy(block + 8, block - 16,
sizeof(*block) * 8);
101 memcpy(block + 16, block - 24,
sizeof(*block) * 8);
102 memcpy(block + 24, block - 32,
sizeof(*block) * 8);
106 int n,
int qscale,
int *overflow)
110 int last_non_zero = 0;
116 block[0] = (block[0] + 2) >> 2;
118 for (i = 1; i < 64; ++i) {
119 int j = scantable[i];
120 int sign = block[j] >> 31;
121 int level = (block[j] ^ sign) - sign;
123 block[j] = (level ^ sign) - sign;
128 return last_non_zero;
137 max_level, 4 *
sizeof(*ctx->
vlc_codes), fail);
139 max_level, 4 *
sizeof(*ctx->
vlc_bits), fail);
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, };
201 (ctx->
m.
avctx->
qmax + 1), 64 *
sizeof(
int), fail);
203 (ctx->
m.
avctx->
qmax + 1), 64 *
sizeof(
int), fail);
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");
378 ctx->
m.
mb_num *
sizeof(uint16_t), fail);
394 if (avctx->
qmax <= 1) {
413 static const uint8_t header_prefix[5] = { 0x00, 0x00, 0x02, 0x80, 0x01 };
417 memcpy(buf, header_prefix, 5);
437 ctx->
msip = buf + 0x170;
452 (diff & ((1 << nbits) - 1)));
457 int last_index,
int n)
459 int last_non_zero = 0;
465 for (i = 1; i <= last_index; i++) {
469 int run_level = i - last_non_zero - 1;
470 int rlevel = (slevel << 1) | !!run_level;
483 int qscale,
int last_index)
492 for (i = 1; i <= last_index; i++) {
497 level = (1 - 2 *
level) * qscale * weight_matrix[i];
499 if (weight_matrix[i] != 8)
503 if (weight_matrix[i] != 32)
509 level = (2 * level + 1) * qscale * weight_matrix[i];
511 if (weight_matrix[i] != 8)
515 if (weight_matrix[i] != 32)
529 for (i = 0; i < 64; i++)
530 score += (block[i] - qblock[i]) * (block[i] - qblock[i]);
537 int last_non_zero = 0;
540 for (i = 1; i <= last_index; i++) {
544 int run_level = i - last_non_zero - 1;
545 bits += ctx->
vlc_bits[(level << 1) |
546 !!run_level] + ctx->
run_bits[run_level];
557 const int bw = 1 << bs;
559 ((mb_y << 4) * ctx->
m.
linesize) + (mb_x << bs + 1);
606 const static uint8_t component[8]={0,0,1,2,0,0,1,2};
611 int jobnr,
int threadnr)
614 int mb_y = jobnr, mb_x;
617 ctx = ctx->
thread[threadnr];
623 for (mb_x = 0; mb_x < ctx->
m.
mb_width; mb_x++) {
632 for (i = 0; i < 8; i++) {
633 int16_t *src_block = ctx->
blocks[i];
634 int overflow, nbits, diff, last_index;
637 memcpy(
block, src_block, 64 *
sizeof(*
block));
660 ctx->
mb_rc[qscale][
mb].
bits = ac_bits + dc_bits + 12 +
667 int jobnr,
int threadnr)
670 int mb_y = jobnr, mb_x;
671 ctx = ctx->
thread[threadnr];
678 for (mb_x = 0; mb_x < ctx->
m.
mb_width; mb_x++) {
687 for (i = 0; i < 8; i++) {
707 for (mb_y = 0; mb_y < ctx->
m.
mb_height; mb_y++) {
711 for (mb_x = 0; mb_x < ctx->
m.
mb_width; mb_x++) {
718 offset += thread_size;
723 int jobnr,
int threadnr)
726 int mb_y = jobnr, mb_x, x,
y;
727 int partial_last_row = (mb_y == ctx->
m.
mb_height - 1) &&
730 ctx = ctx->
thread[threadnr];
733 for (mb_x = 0; mb_x < ctx->
m.
mb_width; ++mb_x, pix += 16) {
738 if (!partial_last_row && mb_x * 16 <= avctx->
width - 16) {
745 for (y = 0; y < bh; y++) {
746 for (x = 0; x < bw; x++) {
753 varc = (varc - (((unsigned) sum * sum) >> 8) + 128) >> 8;
759 int const linesize = ctx->
m.
linesize >> 1;
760 for (mb_x = 0; mb_x < ctx->
m.
mb_width; ++mb_x) {
761 uint16_t *pix = (uint16_t *)ctx->
thread[0]->
src[0] +
762 ((mb_y << 4) * linesize) + (mb_x << 4);
769 for (i = 0; i < 16; ++i) {
770 for (j = 0; j < 16; ++j) {
772 int const sample = (unsigned) pix[j] >> 6;
790 int lambda, up_step, down_step;
791 int last_lower = INT_MAX, last_higher = 0;
794 for (q = 1; q < avctx->
qmax; q++) {
805 if (lambda == last_higher) {
811 unsigned min = UINT_MAX;
814 for (q = 1; q < avctx->
qmax; q++) {
826 bits = (bits + 31) & ~31;
838 if (bits < ctx->frame_bits) {
839 last_lower =
FFMIN(lambda, last_lower);
840 if (last_higher != 0)
841 lambda = (lambda+last_higher)>>1;
844 down_step =
FFMIN((int64_t)down_step*5, INT_MAX);
846 lambda =
FFMAX(1, lambda);
847 if (lambda == last_lower)
850 last_higher =
FFMAX(lambda, last_higher);
851 if (last_lower != INT_MAX)
852 lambda = (lambda+last_lower)>>1;
853 else if ((int64_t)lambda + up_step > INT_MAX)
857 up_step =
FFMIN((int64_t)up_step*5, INT_MAX);
872 int last_lower = INT_MAX;
886 bits = (bits+31)&~31;
894 if (bits < ctx->frame_bits) {
897 if (last_higher == qscale - 1) {
898 qscale = last_higher;
901 last_lower =
FFMIN(qscale, last_lower);
902 if (last_higher != 0)
903 qscale = (qscale + last_higher) >> 1;
905 qscale -= down_step++;
910 if (last_lower == qscale + 1)
912 last_higher =
FFMAX(qscale, last_higher);
913 if (last_lower != INT_MAX)
914 qscale = (qscale + last_lower) >> 1;
927 #define BUCKET_BITS 8
928 #define RADIX_PASSES 4
929 #define NBUCKETS (1 << BUCKET_BITS)
942 memset(buckets, 0,
sizeof(buckets[0][0]) *
RADIX_PASSES * NBUCKETS);
943 for (i = 0; i <
size; i++) {
953 for (i = NBUCKETS - 1; i >= 0; i--)
954 buckets[j][i] = offset -= buckets[j][i];
964 for (i = 0; i <
size; i++) {
966 int pos = buckets[
v]++;
1057 for (i = 0; i < 3; i++) {
1071 "picture could not fit ratecontrol constraints, increase qmax\n");
1087 memset(buf + 640 + offset, 0,
1096 goto encode_coding_unit;