35 #define CFACTOR_Y422 2
36 #define CFACTOR_Y444 3
38 #define MAX_MBS_PER_SLICE 8
40 #define MAX_PLANES 3 // should be increased to 4 when there's AV_PIX_FMT_YUV444AP10
59 4, 7, 9, 11, 13, 14, 15, 63,
60 7, 7, 11, 12, 14, 15, 63, 63,
61 9, 11, 13, 14, 15, 63, 63, 63,
62 11, 11, 13, 14, 63, 63, 63, 63,
63 11, 13, 14, 63, 63, 63, 63, 63,
64 13, 14, 63, 63, 63, 63, 63, 63,
65 13, 63, 63, 63, 63, 63, 63, 63,
66 63, 63, 63, 63, 63, 63, 63, 63,
69 4, 5, 6, 7, 9, 11, 13, 15,
70 5, 5, 7, 8, 11, 13, 15, 17,
71 6, 7, 9, 11, 13, 15, 15, 17,
72 7, 7, 9, 11, 13, 15, 17, 19,
73 7, 9, 11, 13, 14, 16, 19, 23,
74 9, 11, 13, 14, 16, 19, 23, 29,
75 9, 11, 13, 15, 17, 21, 28, 35,
76 11, 13, 16, 17, 21, 28, 35, 41,
79 4, 4, 5, 5, 6, 7, 7, 9,
80 4, 4, 5, 6, 7, 7, 9, 9,
81 5, 5, 6, 7, 7, 9, 9, 10,
82 5, 5, 6, 7, 7, 9, 9, 10,
83 5, 6, 7, 7, 8, 9, 10, 12,
84 6, 7, 7, 8, 9, 10, 12, 15,
85 6, 7, 7, 9, 10, 11, 14, 17,
86 7, 7, 9, 10, 11, 14, 17, 21,
89 4, 4, 4, 4, 4, 4, 4, 4,
90 4, 4, 4, 4, 4, 4, 4, 4,
91 4, 4, 4, 4, 4, 4, 4, 4,
92 4, 4, 4, 4, 4, 4, 4, 5,
93 4, 4, 4, 4, 4, 4, 5, 5,
94 4, 4, 4, 4, 4, 5, 5, 6,
95 4, 4, 4, 4, 5, 5, 6, 7,
96 4, 4, 4, 4, 5, 6, 7, 7,
99 4, 4, 4, 4, 4, 4, 4, 4,
100 4, 4, 4, 4, 4, 4, 4, 4,
101 4, 4, 4, 4, 4, 4, 4, 4,
102 4, 4, 4, 4, 4, 4, 4, 4,
103 4, 4, 4, 4, 4, 4, 4, 4,
104 4, 4, 4, 4, 4, 4, 4, 4,
105 4, 4, 4, 4, 4, 4, 4, 4,
106 4, 4, 4, 4, 4, 4, 4, 4,
110 #define NUM_MB_LIMITS 4
128 .tag =
MKTAG(
'a',
'p',
'c',
'o'),
131 .br_tab = { 300, 242, 220, 194 },
136 .tag =
MKTAG(
'a',
'p',
'c',
's'),
139 .br_tab = { 720, 560, 490, 440 },
143 .full_name =
"standard",
144 .tag =
MKTAG(
'a',
'p',
'c',
'n'),
147 .br_tab = { 1050, 808, 710, 632 },
151 .full_name =
"high quality",
152 .tag =
MKTAG(
'a',
'p',
'c',
'h'),
155 .br_tab = { 1566, 1216, 1070, 950 },
161 #define TRELLIS_WIDTH 16
162 #define SCORE_LIMIT INT_MAX / 2
171 #define MAX_STORED_Q 16
216 int linesize,
int x,
int y,
int w,
int h,
217 int16_t *blocks, uint16_t *emu_buf,
218 int mbs_per_slice,
int blocks_per_mb,
int is_chroma)
220 const uint16_t *esrc;
221 const int mb_width = 4 * blocks_per_mb;
225 for (i = 0; i < mbs_per_slice; i++, src += mb_width) {
227 memset(blocks, 0, 64 * (mbs_per_slice - i) * blocks_per_mb
231 if (x + mb_width <= w && y + 16 <= h) {
233 elinesize = linesize;
238 elinesize = 16 *
sizeof(*emu_buf);
240 bw =
FFMIN(w - x, mb_width);
241 bh =
FFMIN(h - y, 16);
243 for (j = 0; j < bh; j++) {
244 memcpy(emu_buf + j * 16,
245 (
const uint8_t*)src + j * linesize,
247 pix = emu_buf[j * 16 + bw - 1];
248 for (k = bw; k < mb_width; k++)
249 emu_buf[j * 16 + k] = pix;
252 memcpy(emu_buf + j * 16,
253 emu_buf + (bh - 1) * 16,
254 mb_width *
sizeof(*emu_buf));
257 ctx->
dsp.
fdct(esrc, elinesize, blocks);
259 if (blocks_per_mb > 2) {
260 ctx->
dsp.
fdct(esrc + 8, elinesize, blocks);
263 ctx->
dsp.
fdct(esrc + elinesize * 4, elinesize, blocks);
265 if (blocks_per_mb > 2) {
266 ctx->
dsp.
fdct(esrc + elinesize * 4 + 8, elinesize, blocks);
270 ctx->
dsp.
fdct(esrc, elinesize, blocks);
272 ctx->
dsp.
fdct(esrc + elinesize * 4, elinesize, blocks);
274 if (blocks_per_mb > 2) {
275 ctx->
dsp.
fdct(esrc + 8, elinesize, blocks);
277 ctx->
dsp.
fdct(esrc + elinesize * 4 + 8, elinesize, blocks);
291 unsigned int rice_order, exp_order, switch_bits, switch_val;
295 switch_bits = (codebook & 3) + 1;
296 rice_order = codebook >> 5;
297 exp_order = (codebook >> 2) & 7;
299 switch_val = switch_bits << rice_order;
301 if (val >= switch_val) {
302 val -= switch_val - (1 << exp_order);
305 put_bits(pb, exponent - exp_order + switch_bits, 0);
308 exponent = val >> rice_order;
318 #define GET_SIGN(x) ((x) >> 31)
319 #define MAKE_CODE(x) (((x) << 1) ^ GET_SIGN(x))
322 int blocks_per_slice,
int scale)
325 int codebook = 3, code,
dc, prev_dc,
delta, sign, new_sign;
327 prev_dc = (blocks[0] - 0x4000) / scale;
333 for (i = 1; i < blocks_per_slice; i++, blocks += 64) {
334 dc = (blocks[0] - 0x4000) / scale;
335 delta = dc - prev_dc;
337 delta = (delta ^ sign) - sign;
340 codebook = (code + (code & 1)) >> 1;
341 codebook =
FFMIN(codebook, 3);
348 int blocks_per_slice,
349 int plane_size_factor,
350 const uint8_t *scan,
const int16_t *qmat)
354 int max_coeffs, abs_level;
356 max_coeffs = blocks_per_slice << 6;
361 for (i = 1; i < 64; i++) {
362 for (idx = scan[i]; idx < max_coeffs; idx += 64) {
363 level = blocks[idx] / qmat[scan[i]];
365 abs_level =
FFABS(level);
382 const uint16_t *src,
int linesize,
383 int mbs_per_slice, int16_t *blocks,
384 int blocks_per_mb,
int plane_size_factor,
387 int blocks_per_slice, saved_pos;
390 blocks_per_slice = mbs_per_slice * blocks_per_mb;
392 encode_dcs(pb, blocks, blocks_per_slice, qmat[0]);
393 encode_acs(pb, blocks, blocks_per_slice, plane_size_factor,
409 int slice_width_factor =
av_log2(mbs_per_slice);
410 int num_cblocks, pwidth, linesize, line_add;
411 int plane_factor, is_chroma;
425 for (i = 0; i < 64; i++)
430 is_chroma = (i == 1 || i == 2);
431 plane_factor = slice_width_factor + 2;
438 pwidth = avctx->
width;
443 pwidth = avctx->
width >> 1;
447 src = (
const uint16_t*)(pic->
data[i] + yp * linesize +
453 mbs_per_slice, num_cblocks, is_chroma);
455 mbs_per_slice, ctx->
blocks[0],
456 num_cblocks, plane_factor,
458 total_size += sizes[i];
465 unsigned int rice_order, exp_order, switch_bits, switch_val;
469 switch_bits = (codebook & 3) + 1;
470 rice_order = codebook >> 5;
471 exp_order = (codebook >> 2) & 7;
473 switch_val = switch_bits << rice_order;
475 if (val >= switch_val) {
476 val -= switch_val - (1 << exp_order);
479 return exponent * 2 - exp_order + switch_bits + 1;
481 return (val >> rice_order) + rice_order + 1;
485 static int estimate_dcs(
int *error, int16_t *blocks,
int blocks_per_slice,
489 int codebook = 3, code,
dc, prev_dc,
delta, sign, new_sign;
492 prev_dc = (blocks[0] - 0x4000) / scale;
499 for (i = 1; i < blocks_per_slice; i++, blocks += 64) {
500 dc = (blocks[0] - 0x4000) / scale;
502 delta = dc - prev_dc;
504 delta = (delta ^ sign) - sign;
507 codebook = (code + (code & 1)) >> 1;
508 codebook =
FFMIN(codebook, 3);
516 static int estimate_acs(
int *error, int16_t *blocks,
int blocks_per_slice,
517 int plane_size_factor,
518 const uint8_t *scan,
const int16_t *qmat)
522 int max_coeffs, abs_level;
525 max_coeffs = blocks_per_slice << 6;
530 for (i = 1; i < 64; i++) {
531 for (idx = scan[i]; idx < max_coeffs; idx += 64) {
532 level = blocks[idx] / qmat[scan[i]];
533 *error +=
FFABS(blocks[idx]) % qmat[scan[i]];
535 abs_level =
FFABS(level);
553 const uint16_t *src,
int linesize,
555 int blocks_per_mb,
int plane_size_factor,
558 int blocks_per_slice;
561 blocks_per_slice = mbs_per_slice * blocks_per_mb;
571 int trellis_node,
int x,
int y,
int mbs_per_slice,
575 int i, q, pq, xp, yp;
577 int slice_width_factor =
av_log2(mbs_per_slice);
582 int error,
bits, bits_limit;
583 int mbs, prev, cur, new_score;
587 int linesize[4], line_add;
593 mbs = x + mbs_per_slice;
596 is_chroma[i] = (i == 1 || i == 2);
597 plane_factor[i] = slice_width_factor + 2;
604 pwidth = avctx->
width;
609 pwidth = avctx->
width >> 1;
613 src = (
const uint16_t*)(pic->
data[i] + yp * linesize[i] +
619 mbs_per_slice, num_cblocks[i], is_chroma[i]);
622 for (q = min_quant; q < max_quant + 2; q++) {
628 for (q = min_quant; q <=
max_quant; q++) {
635 num_cblocks[i], plane_factor[i],
638 if (bits > 65000 * 8) {
642 slice_bits[q] =
bits;
643 slice_score[q] = error;
645 if (slice_bits[max_quant] <= ctx->
bits_per_mb * mbs_per_slice) {
646 slice_bits[max_quant + 1] = slice_bits[
max_quant];
647 slice_score[max_quant + 1] = slice_score[
max_quant] + 1;
650 for (q = max_quant + 1; q < 128; q++) {
657 for (i = 0; i < 64; i++)
664 num_cblocks[i], plane_factor[i],
667 if (bits <= ctx->bits_per_mb * mbs_per_slice)
671 slice_bits[max_quant + 1] =
bits;
672 slice_score[max_quant + 1] = error;
675 td->
nodes[trellis_node + max_quant + 1].
quant = overquant;
678 for (pq = min_quant; pq < max_quant + 2; pq++) {
681 for (q = min_quant; q < max_quant + 2; q++) {
682 cur = trellis_node + q;
684 bits = td->
nodes[prev].
bits + slice_bits[q];
685 error = slice_score[q];
686 if (bits > bits_limit)
705 for (q = min_quant + 1; q < max_quant + 2; q++) {
706 if (td->
nodes[trellis_node + q].
score <= error) {
708 pq = trellis_node + q;
716 int jobnr,
int threadnr)
721 int x,
y = jobnr,
mb, q = 0;
723 for (x = mb = 0; x < ctx->
mb_width; x += mbs_per_slice, mb++) {
724 while (ctx->
mb_width - x < mbs_per_slice)
740 const AVFrame *pic,
int *got_packet)
743 uint8_t *orig_buf, *buf, *slice_hdr, *slice_sizes, *tmp;
746 int x,
y, i,
mb, q = 0;
747 int sizes[4] = { 0 };
748 int slice_hdr_size = 2 + 2 * (ctx->
num_planes - 1);
762 orig_buf = pkt->
data;
766 bytestream_put_be32 (&orig_buf,
FRAME_ID);
772 bytestream_put_be16 (&buf, 0);
774 bytestream_put_be16 (&buf, avctx->
width);
775 bytestream_put_be16 (&buf, avctx->
height);
780 bytestream_put_byte (&buf, frame_flags);
782 bytestream_put_byte (&buf, 0);
784 bytestream_put_byte (&buf, avctx->
color_trc);
785 bytestream_put_byte (&buf, avctx->
colorspace);
786 bytestream_put_byte (&buf, 0x40);
787 bytestream_put_byte (&buf, 0);
789 bytestream_put_byte (&buf, 0x03);
791 for (i = 0; i < 64; i++)
792 bytestream_put_byte(&buf, ctx->
quant_mat[i]);
794 for (i = 0; i < 64; i++)
795 bytestream_put_byte(&buf, ctx->
quant_mat[i]);
797 bytestream_put_byte (&buf, 0x00);
799 bytestream_put_be16 (&tmp, buf - orig_buf);
805 picture_size_pos = buf + 1;
806 bytestream_put_byte (&buf, 0x40);
825 for (x = mb = 0; x < ctx->
mb_width; x += mbs_per_slice, mb++) {
829 while (ctx->
mb_width - x < mbs_per_slice)
832 bytestream_put_byte(&buf, slice_hdr_size << 3);
834 buf += slice_hdr_size - 1;
836 encode_slice(avctx, pic, &pb, sizes, x, y, q, mbs_per_slice);
838 bytestream_put_byte(&slice_hdr, q);
839 slice_size = slice_hdr_size + sizes[ctx->
num_planes - 1];
841 bytestream_put_be16(&slice_hdr, sizes[i]);
842 slice_size += sizes[i];
844 bytestream_put_be16(&slice_sizes, slice_size);
845 buf += slice_size - slice_hdr_size;
849 picture_size = buf - (picture_size_pos - 1);
850 bytestream_put_be32(&picture_size_pos, picture_size);
854 frame_size = buf - orig_buf;
855 bytestream_put_be32(&orig_buf, frame_size);
900 if (mps & (mps - 1)) {
902 "there should be an integer power of two MBs per slice\n");
929 if (strlen(ctx->
vendor) != 4) {
950 for (j = 0; j < 64; j++)
974 for (i = min_quant; i < max_quant + 2; i++) {
988 for (j = 0; j < 64; j++) {
1009 "profile %d, %d slices, interlacing: %s, %d bits per MB\n",
1018 #define OFFSET(x) offsetof(ProresContext, x)
1019 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1022 {
"mbs_per_slice",
"macroblocks per slice",
OFFSET(mbs_per_slice),
1028 0, 0,
VE,
"profile" },
1030 0, 0,
VE,
"profile" },
1032 0, 0,
VE,
"profile" },
1034 0, 0,
VE,
"profile" },
1035 {
"vendor",
"vendor ID",
OFFSET(vendor),
1037 {
"bits_per_mb",
"desired bits per macroblock",
OFFSET(bits_per_mb),
1042 0, 0,
VE,
"quant_mat" },
1044 0, 0,
VE,
"quant_mat" },
1046 0, 0,
VE,
"quant_mat" },
1048 0, 0,
VE,
"quant_mat" },
1050 0, 0,
VE,
"quant_mat" },
1052 0, 0,
VE,
"quant_mat" },
1064 .
name =
"prores_kostya",