26 #include "libavutil/opt.h"
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];
461 "required buffer size");
470 unsigned int rice_order, exp_order, switch_bits, switch_val;
474 switch_bits = (codebook & 3) + 1;
475 rice_order = codebook >> 5;
476 exp_order = (codebook >> 2) & 7;
478 switch_val = switch_bits << rice_order;
480 if (val >= switch_val) {
481 val -= switch_val - (1 << exp_order);
484 return exponent * 2 - exp_order + switch_bits + 1;
486 return (val >> rice_order) + rice_order + 1;
490 static int estimate_dcs(
int *error, int16_t *blocks,
int blocks_per_slice,
494 int codebook = 3, code,
dc, prev_dc,
delta, sign, new_sign;
497 prev_dc = (blocks[0] - 0x4000) / scale;
504 for (i = 1; i < blocks_per_slice; i++, blocks += 64) {
505 dc = (blocks[0] - 0x4000) / scale;
507 delta = dc - prev_dc;
509 delta = (delta ^ sign) - sign;
512 codebook = (code + (code & 1)) >> 1;
513 codebook =
FFMIN(codebook, 3);
521 static int estimate_acs(
int *error, int16_t *blocks,
int blocks_per_slice,
522 int plane_size_factor,
523 const uint8_t *scan,
const int16_t *qmat)
527 int max_coeffs, abs_level;
530 max_coeffs = blocks_per_slice << 6;
535 for (i = 1; i < 64; i++) {
536 for (idx = scan[i]; idx < max_coeffs; idx += 64) {
537 level = blocks[idx] / qmat[scan[i]];
538 *error +=
FFABS(blocks[idx]) % qmat[scan[i]];
540 abs_level =
FFABS(level);
558 const uint16_t *src,
int linesize,
560 int blocks_per_mb,
int plane_size_factor,
563 int blocks_per_slice;
566 blocks_per_slice = mbs_per_slice * blocks_per_mb;
576 int trellis_node,
int x,
int y,
int mbs_per_slice,
580 int i, q, pq, xp, yp;
582 int slice_width_factor =
av_log2(mbs_per_slice);
587 int error,
bits, bits_limit;
588 int mbs, prev, cur, new_score;
592 int linesize[4], line_add;
598 mbs = x + mbs_per_slice;
601 is_chroma[i] = (i == 1 || i == 2);
602 plane_factor[i] = slice_width_factor + 2;
609 pwidth = avctx->
width;
614 pwidth = avctx->
width >> 1;
618 src = (
const uint16_t*)(pic->
data[i] + yp * linesize[i] +
624 mbs_per_slice, num_cblocks[i], is_chroma[i]);
627 for (q = min_quant; q < max_quant + 2; q++) {
633 for (q = min_quant; q <=
max_quant; q++) {
640 num_cblocks[i], plane_factor[i],
643 if (bits > 65000 * 8) {
647 slice_bits[q] =
bits;
648 slice_score[q] = error;
650 if (slice_bits[max_quant] <= ctx->
bits_per_mb * mbs_per_slice) {
651 slice_bits[max_quant + 1] = slice_bits[
max_quant];
652 slice_score[max_quant + 1] = slice_score[
max_quant] + 1;
655 for (q = max_quant + 1; q < 128; q++) {
662 for (i = 0; i < 64; i++)
669 num_cblocks[i], plane_factor[i],
672 if (bits <= ctx->bits_per_mb * mbs_per_slice)
676 slice_bits[max_quant + 1] =
bits;
677 slice_score[max_quant + 1] = error;
680 td->
nodes[trellis_node + max_quant + 1].
quant = overquant;
683 for (pq = min_quant; pq < max_quant + 2; pq++) {
686 for (q = min_quant; q < max_quant + 2; q++) {
687 cur = trellis_node + q;
689 bits = td->
nodes[prev].
bits + slice_bits[q];
690 error = slice_score[q];
691 if (bits > bits_limit)
710 for (q = min_quant + 1; q < max_quant + 2; q++) {
711 if (td->
nodes[trellis_node + q].
score <= error) {
713 pq = trellis_node + q;
721 int jobnr,
int threadnr)
726 int x,
y = jobnr,
mb, q = 0;
728 for (x = mb = 0; x < ctx->
mb_width; x += mbs_per_slice, mb++) {
729 while (ctx->
mb_width - x < mbs_per_slice)
745 const AVFrame *pic,
int *got_packet)
748 uint8_t *orig_buf, *buf, *slice_hdr, *slice_sizes, *tmp;
751 int x,
y, i,
mb, q = 0;
752 int sizes[4] = { 0 };
753 int slice_hdr_size = 2 + 2 * (ctx->
num_planes - 1);
767 orig_buf = pkt->
data;
771 bytestream_put_be32 (&orig_buf,
FRAME_ID);
777 bytestream_put_be16 (&buf, 0);
779 bytestream_put_be16 (&buf, avctx->
width);
780 bytestream_put_be16 (&buf, avctx->
height);
785 bytestream_put_byte (&buf, frame_flags);
787 bytestream_put_byte (&buf, 0);
789 bytestream_put_byte (&buf, avctx->
color_trc);
790 bytestream_put_byte (&buf, avctx->
colorspace);
791 bytestream_put_byte (&buf, 0x40);
792 bytestream_put_byte (&buf, 0);
794 bytestream_put_byte (&buf, 0x03);
796 for (i = 0; i < 64; i++)
797 bytestream_put_byte(&buf, ctx->
quant_mat[i]);
799 for (i = 0; i < 64; i++)
800 bytestream_put_byte(&buf, ctx->
quant_mat[i]);
802 bytestream_put_byte (&buf, 0x00);
804 bytestream_put_be16 (&tmp, buf - orig_buf);
810 picture_size_pos = buf + 1;
811 bytestream_put_byte (&buf, 0x40);
830 for (x = mb = 0; x < ctx->
mb_width; x += mbs_per_slice, mb++) {
834 while (ctx->
mb_width - x < mbs_per_slice)
837 bytestream_put_byte(&buf, slice_hdr_size << 3);
839 buf += slice_hdr_size - 1;
841 ret =
encode_slice(avctx, pic, &pb, sizes, x, y, q, mbs_per_slice);
845 bytestream_put_byte(&slice_hdr, q);
846 slice_size = slice_hdr_size + sizes[ctx->
num_planes - 1];
848 bytestream_put_be16(&slice_hdr, sizes[i]);
849 slice_size += sizes[i];
851 bytestream_put_be16(&slice_sizes, slice_size);
852 buf += slice_size - slice_hdr_size;
856 picture_size = buf - (picture_size_pos - 1);
857 bytestream_put_be32(&picture_size_pos, picture_size);
861 frame_size = buf - orig_buf;
862 bytestream_put_be32(&orig_buf, frame_size);
907 if (mps & (mps - 1)) {
909 "there should be an integer power of two MBs per slice\n");
936 if (strlen(ctx->
vendor) != 4) {
957 for (j = 0; j < 64; j++)
981 for (i = min_quant; i < max_quant + 2; i++) {
995 for (j = 0; j < 64; j++) {
1016 "profile %d, %d slices, interlacing: %s, %d bits per MB\n",
1025 #define OFFSET(x) offsetof(ProresContext, x)
1026 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1029 {
"mbs_per_slice",
"macroblocks per slice",
OFFSET(mbs_per_slice),
1035 0, 0,
VE,
"profile" },
1037 0, 0,
VE,
"profile" },
1039 0, 0,
VE,
"profile" },
1041 0, 0,
VE,
"profile" },
1042 {
"vendor",
"vendor ID",
OFFSET(vendor),
1044 {
"bits_per_mb",
"desired bits per macroblock",
OFFSET(bits_per_mb),
1049 0, 0,
VE,
"quant_mat" },
1051 0, 0,
VE,
"quant_mat" },
1053 0, 0,
VE,
"quant_mat" },
1055 0, 0,
VE,
"quant_mat" },
1057 0, 0,
VE,
"quant_mat" },
1059 0, 0,
VE,
"quant_mat" },
1071 .
name =
"prores_kostya",