34 #define CFACTOR_Y422 2
35 #define CFACTOR_Y444 3
37 #define MAX_MBS_PER_SLICE 8
39 #define MAX_PLANES 3 // should be increased to 4 when there's AV_PIX_FMT_YUV444AP10
58 4, 7, 9, 11, 13, 14, 15, 63,
59 7, 7, 11, 12, 14, 15, 63, 63,
60 9, 11, 13, 14, 15, 63, 63, 63,
61 11, 11, 13, 14, 63, 63, 63, 63,
62 11, 13, 14, 63, 63, 63, 63, 63,
63 13, 14, 63, 63, 63, 63, 63, 63,
64 13, 63, 63, 63, 63, 63, 63, 63,
65 63, 63, 63, 63, 63, 63, 63, 63,
68 4, 5, 6, 7, 9, 11, 13, 15,
69 5, 5, 7, 8, 11, 13, 15, 17,
70 6, 7, 9, 11, 13, 15, 15, 17,
71 7, 7, 9, 11, 13, 15, 17, 19,
72 7, 9, 11, 13, 14, 16, 19, 23,
73 9, 11, 13, 14, 16, 19, 23, 29,
74 9, 11, 13, 15, 17, 21, 28, 35,
75 11, 13, 16, 17, 21, 28, 35, 41,
78 4, 4, 5, 5, 6, 7, 7, 9,
79 4, 4, 5, 6, 7, 7, 9, 9,
80 5, 5, 6, 7, 7, 9, 9, 10,
81 5, 5, 6, 7, 7, 9, 9, 10,
82 5, 6, 7, 7, 8, 9, 10, 12,
83 6, 7, 7, 8, 9, 10, 12, 15,
84 6, 7, 7, 9, 10, 11, 14, 17,
85 7, 7, 9, 10, 11, 14, 17, 21,
88 4, 4, 4, 4, 4, 4, 4, 4,
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, 5,
92 4, 4, 4, 4, 4, 4, 5, 5,
93 4, 4, 4, 4, 4, 5, 5, 6,
94 4, 4, 4, 4, 5, 5, 6, 7,
95 4, 4, 4, 4, 5, 6, 7, 7,
98 4, 4, 4, 4, 4, 4, 4, 4,
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,
109 #define NUM_MB_LIMITS 4
127 .tag =
MKTAG(
'a',
'p',
'c',
'o'),
130 .br_tab = { 300, 242, 220, 194 },
135 .tag =
MKTAG(
'a',
'p',
'c',
's'),
138 .br_tab = { 720, 560, 490, 440 },
142 .full_name =
"standard",
143 .tag =
MKTAG(
'a',
'p',
'c',
'n'),
146 .br_tab = { 1050, 808, 710, 632 },
150 .full_name =
"high quality",
151 .tag =
MKTAG(
'a',
'p',
'c',
'h'),
154 .br_tab = { 1566, 1216, 1070, 950 },
160 #define TRELLIS_WIDTH 16
161 #define SCORE_LIMIT INT_MAX / 2
170 #define MAX_STORED_Q 16
215 int linesize,
int x,
int y,
int w,
int h,
216 DCTELEM *blocks, uint16_t *emu_buf,
217 int mbs_per_slice,
int blocks_per_mb,
int is_chroma)
219 const uint16_t *esrc;
220 const int mb_width = 4 * blocks_per_mb;
224 for (i = 0; i < mbs_per_slice; i++, src += mb_width) {
226 memset(blocks, 0, 64 * (mbs_per_slice - i) * blocks_per_mb
230 if (x + mb_width <= w && y + 16 <= h) {
232 elinesize = linesize;
237 elinesize = 16 *
sizeof(*emu_buf);
239 bw =
FFMIN(w - x, mb_width);
240 bh =
FFMIN(h - y, 16);
242 for (j = 0; j < bh; j++) {
243 memcpy(emu_buf + j * 16,
244 (
const uint8_t*)src + j * linesize,
246 pix = emu_buf[j * 16 + bw - 1];
247 for (k = bw; k < mb_width; k++)
248 emu_buf[j * 16 + k] = pix;
251 memcpy(emu_buf + j * 16,
252 emu_buf + (bh - 1) * 16,
253 mb_width *
sizeof(*emu_buf));
256 ctx->
dsp.
fdct(esrc, elinesize, blocks);
258 if (blocks_per_mb > 2) {
259 ctx->
dsp.
fdct(esrc + 8, elinesize, blocks);
262 ctx->
dsp.
fdct(esrc + elinesize * 4, elinesize, blocks);
264 if (blocks_per_mb > 2) {
265 ctx->
dsp.
fdct(esrc + elinesize * 4 + 8, elinesize, blocks);
269 ctx->
dsp.
fdct(esrc, elinesize, blocks);
271 ctx->
dsp.
fdct(esrc + elinesize * 4, elinesize, blocks);
273 if (blocks_per_mb > 2) {
274 ctx->
dsp.
fdct(esrc + 8, elinesize, blocks);
276 ctx->
dsp.
fdct(esrc + elinesize * 4 + 8, elinesize, blocks);
290 unsigned int rice_order, exp_order, switch_bits, switch_val;
294 switch_bits = (codebook & 3) + 1;
295 rice_order = codebook >> 5;
296 exp_order = (codebook >> 2) & 7;
298 switch_val = switch_bits << rice_order;
300 if (val >= switch_val) {
301 val -= switch_val - (1 << exp_order);
304 put_bits(pb, exponent - exp_order + switch_bits, 0);
307 exponent = val >> rice_order;
317 #define GET_SIGN(x) ((x) >> 31)
318 #define MAKE_CODE(x) (((x) << 1) ^ GET_SIGN(x))
321 int blocks_per_slice,
int scale)
324 int codebook = 3, code, dc, prev_dc,
delta, sign, new_sign;
326 prev_dc = (blocks[0] - 0x4000) / scale;
332 for (i = 1; i < blocks_per_slice; i++, blocks += 64) {
333 dc = (blocks[0] - 0x4000) / scale;
334 delta = dc - prev_dc;
336 delta = (delta ^ sign) - sign;
339 codebook = (code + (code & 1)) >> 1;
340 codebook =
FFMIN(codebook, 3);
347 int blocks_per_slice,
348 int plane_size_factor,
349 const uint8_t *scan,
const int16_t *qmat)
353 int max_coeffs, abs_level;
355 max_coeffs = blocks_per_slice << 6;
360 for (i = 1; i < 64; i++) {
361 for (idx = scan[i]; idx < max_coeffs; idx += 64) {
362 level = blocks[idx] / qmat[scan[i]];
364 abs_level =
FFABS(level);
381 const uint16_t *src,
int linesize,
382 int mbs_per_slice,
DCTELEM *blocks,
383 int blocks_per_mb,
int plane_size_factor,
386 int blocks_per_slice, saved_pos;
389 blocks_per_slice = mbs_per_slice * blocks_per_mb;
391 encode_dcs(pb, blocks, blocks_per_slice, qmat[0]);
392 encode_acs(pb, blocks, blocks_per_slice, plane_size_factor,
408 int slice_width_factor =
av_log2(mbs_per_slice);
409 int num_cblocks, pwidth, linesize, line_add;
410 int plane_factor, is_chroma;
424 for (i = 0; i < 64; i++)
429 is_chroma = (i == 1 || i == 2);
430 plane_factor = slice_width_factor + 2;
437 pwidth = avctx->
width;
442 pwidth = avctx->
width >> 1;
446 src = (
const uint16_t*)(pic->
data[i] + yp * linesize +
452 mbs_per_slice, num_cblocks, is_chroma);
454 mbs_per_slice, ctx->
blocks[0],
455 num_cblocks, plane_factor,
457 total_size += sizes[i];
464 unsigned int rice_order, exp_order, switch_bits, switch_val;
468 switch_bits = (codebook & 3) + 1;
469 rice_order = codebook >> 5;
470 exp_order = (codebook >> 2) & 7;
472 switch_val = switch_bits << rice_order;
474 if (val >= switch_val) {
475 val -= switch_val - (1 << exp_order);
478 return exponent * 2 - exp_order + switch_bits + 1;
480 return (val >> rice_order) + rice_order + 1;
488 int codebook = 3, code, dc, prev_dc,
delta, sign, new_sign;
491 prev_dc = (blocks[0] - 0x4000) / scale;
498 for (i = 1; i < blocks_per_slice; i++, blocks += 64) {
499 dc = (blocks[0] - 0x4000) / scale;
501 delta = dc - prev_dc;
503 delta = (delta ^ sign) - sign;
506 codebook = (code + (code & 1)) >> 1;
507 codebook =
FFMIN(codebook, 3);
516 int plane_size_factor,
517 const uint8_t *scan,
const int16_t *qmat)
521 int max_coeffs, abs_level;
524 max_coeffs = blocks_per_slice << 6;
529 for (i = 1; i < 64; i++) {
530 for (idx = scan[i]; idx < max_coeffs; idx += 64) {
531 level = blocks[idx] / qmat[scan[i]];
532 *error +=
FFABS(blocks[idx]) % qmat[scan[i]];
534 abs_level =
FFABS(level);
552 const uint16_t *src,
int linesize,
554 int blocks_per_mb,
int plane_size_factor,
557 int blocks_per_slice;
560 blocks_per_slice = mbs_per_slice * blocks_per_mb;
570 int trellis_node,
int x,
int y,
int mbs_per_slice,
574 int i, q, pq, xp, yp;
576 int slice_width_factor =
av_log2(mbs_per_slice);
581 int error,
bits, bits_limit;
582 int mbs, prev, cur, new_score;
586 int linesize[4], line_add;
592 mbs = x + mbs_per_slice;
595 is_chroma[i] = (i == 1 || i == 2);
596 plane_factor[i] = slice_width_factor + 2;
603 pwidth = avctx->
width;
608 pwidth = avctx->
width >> 1;
612 src = (
const uint16_t*)(pic->
data[i] + yp * linesize[i] +
618 mbs_per_slice, num_cblocks[i], is_chroma[i]);
621 for (q = min_quant; q < max_quant + 2; q++) {
627 for (q = min_quant; q <=
max_quant; q++) {
634 num_cblocks[i], plane_factor[i],
637 if (bits > 65000 * 8) {
641 slice_bits[q] =
bits;
642 slice_score[q] = error;
644 if (slice_bits[max_quant] <= ctx->
bits_per_mb * mbs_per_slice) {
645 slice_bits[max_quant + 1] = slice_bits[
max_quant];
646 slice_score[max_quant + 1] = slice_score[
max_quant] + 1;
649 for (q = max_quant + 1; q < 128; q++) {
656 for (i = 0; i < 64; i++)
663 num_cblocks[i], plane_factor[i],
666 if (bits <= ctx->bits_per_mb * mbs_per_slice)
670 slice_bits[max_quant + 1] =
bits;
671 slice_score[max_quant + 1] = error;
674 td->
nodes[trellis_node + max_quant + 1].
quant = overquant;
677 for (pq = min_quant; pq < max_quant + 2; pq++) {
680 for (q = min_quant; q < max_quant + 2; q++) {
681 cur = trellis_node + q;
683 bits = td->
nodes[prev].
bits + slice_bits[q];
684 error = slice_score[q];
685 if (bits > bits_limit)
704 for (q = min_quant + 1; q < max_quant + 2; q++) {
705 if (td->
nodes[trellis_node + q].
score <= error) {
707 pq = trellis_node + q;
715 int jobnr,
int threadnr)
720 int x, y = jobnr,
mb, q = 0;
722 for (x = mb = 0; x < ctx->
mb_width; x += mbs_per_slice, mb++) {
723 while (ctx->
mb_width - x < mbs_per_slice)
739 const AVFrame *pic,
int *got_packet)
742 uint8_t *orig_buf, *buf, *slice_hdr, *slice_sizes, *tmp;
745 int x, y, i,
mb, q = 0;
746 int sizes[4] = { 0 };
747 int slice_hdr_size = 2 + 2 * (ctx->
num_planes - 1);
761 orig_buf = pkt->
data;
765 bytestream_put_be32 (&orig_buf,
FRAME_ID);
771 bytestream_put_be16 (&buf, 0);
773 bytestream_put_be16 (&buf, avctx->
width);
774 bytestream_put_be16 (&buf, avctx->
height);
779 bytestream_put_byte (&buf, frame_flags);
781 bytestream_put_byte (&buf, 0);
783 bytestream_put_byte (&buf, avctx->
color_trc);
784 bytestream_put_byte (&buf, avctx->
colorspace);
785 bytestream_put_byte (&buf, 0x40);
786 bytestream_put_byte (&buf, 0);
788 bytestream_put_byte (&buf, 0x03);
790 for (i = 0; i < 64; i++)
791 bytestream_put_byte(&buf, ctx->
quant_mat[i]);
793 for (i = 0; i < 64; i++)
794 bytestream_put_byte(&buf, ctx->
quant_mat[i]);
796 bytestream_put_byte (&buf, 0x00);
798 bytestream_put_be16 (&tmp, buf - orig_buf);
804 picture_size_pos = buf + 1;
805 bytestream_put_byte (&buf, 0x40);
824 for (x = mb = 0; x < ctx->
mb_width; x += mbs_per_slice, mb++) {
828 while (ctx->
mb_width - x < mbs_per_slice)
831 bytestream_put_byte(&buf, slice_hdr_size << 3);
833 buf += slice_hdr_size - 1;
835 encode_slice(avctx, pic, &pb, sizes, x, y, q, mbs_per_slice);
837 bytestream_put_byte(&slice_hdr, q);
838 slice_size = slice_hdr_size + sizes[ctx->
num_planes - 1];
840 bytestream_put_be16(&slice_hdr, sizes[i]);
841 slice_size += sizes[i];
843 bytestream_put_be16(&slice_sizes, slice_size);
844 buf += slice_size - slice_hdr_size;
848 picture_size = buf - (picture_size_pos - 1);
849 bytestream_put_be32(&picture_size_pos, picture_size);
853 frame_size = buf - orig_buf;
854 bytestream_put_be32(&orig_buf, frame_size);
899 if (mps & (mps - 1)) {
901 "there should be an integer power of two MBs per slice\n");
928 if (strlen(ctx->
vendor) != 4) {
949 for (j = 0; j < 64; j++)
973 for (i = min_quant; i < max_quant + 2; i++) {
987 for (j = 0; j < 64; j++) {
1008 "profile %d, %d slices, interlacing: %s, %d bits per MB\n",
1017 #define OFFSET(x) offsetof(ProresContext, x)
1018 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1021 {
"mbs_per_slice",
"macroblocks per slice",
OFFSET(mbs_per_slice),
1027 0, 0,
VE,
"profile" },
1029 0, 0,
VE,
"profile" },
1031 0, 0,
VE,
"profile" },
1033 0, 0,
VE,
"profile" },
1034 {
"vendor",
"vendor ID",
OFFSET(vendor),
1036 {
"bits_per_mb",
"desired bits per macroblock",
OFFSET(bits_per_mb),
1041 0, 0,
VE,
"quant_mat" },
1043 0, 0,
VE,
"quant_mat" },
1045 0, 0,
VE,
"quant_mat" },
1047 0, 0,
VE,
"quant_mat" },
1049 0, 0,
VE,
"quant_mat" },
1051 0, 0,
VE,
"quant_mat" },
1063 .
name =
"prores_kostya",