31 #define CFACTOR_Y422 2
32 #define CFACTOR_Y444 3
34 #define MAX_MBS_PER_SLICE 8
36 #define MAX_PLANES 3 // should be increased to 4 when there's AV_PIX_FMT_YUV444AP10
55 4, 7, 9, 11, 13, 14, 15, 63,
56 7, 7, 11, 12, 14, 15, 63, 63,
57 9, 11, 13, 14, 15, 63, 63, 63,
58 11, 11, 13, 14, 63, 63, 63, 63,
59 11, 13, 14, 63, 63, 63, 63, 63,
60 13, 14, 63, 63, 63, 63, 63, 63,
61 13, 63, 63, 63, 63, 63, 63, 63,
62 63, 63, 63, 63, 63, 63, 63, 63,
65 4, 5, 6, 7, 9, 11, 13, 15,
66 5, 5, 7, 8, 11, 13, 15, 17,
67 6, 7, 9, 11, 13, 15, 15, 17,
68 7, 7, 9, 11, 13, 15, 17, 19,
69 7, 9, 11, 13, 14, 16, 19, 23,
70 9, 11, 13, 14, 16, 19, 23, 29,
71 9, 11, 13, 15, 17, 21, 28, 35,
72 11, 13, 16, 17, 21, 28, 35, 41,
75 4, 4, 5, 5, 6, 7, 7, 9,
76 4, 4, 5, 6, 7, 7, 9, 9,
77 5, 5, 6, 7, 7, 9, 9, 10,
78 5, 5, 6, 7, 7, 9, 9, 10,
79 5, 6, 7, 7, 8, 9, 10, 12,
80 6, 7, 7, 8, 9, 10, 12, 15,
81 6, 7, 7, 9, 10, 11, 14, 17,
82 7, 7, 9, 10, 11, 14, 17, 21,
85 4, 4, 4, 4, 4, 4, 4, 4,
86 4, 4, 4, 4, 4, 4, 4, 4,
87 4, 4, 4, 4, 4, 4, 4, 4,
88 4, 4, 4, 4, 4, 4, 4, 5,
89 4, 4, 4, 4, 4, 4, 5, 5,
90 4, 4, 4, 4, 4, 5, 5, 6,
91 4, 4, 4, 4, 5, 5, 6, 7,
92 4, 4, 4, 4, 5, 6, 7, 7,
95 4, 4, 4, 4, 4, 4, 4, 4,
96 4, 4, 4, 4, 4, 4, 4, 4,
97 4, 4, 4, 4, 4, 4, 4, 4,
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,
106 #define NUM_MB_LIMITS 4
124 .tag =
MKTAG(
'a',
'p',
'c',
'o'),
127 .br_tab = { 300, 242, 220, 194 },
132 .tag =
MKTAG(
'a',
'p',
'c',
's'),
135 .br_tab = { 720, 560, 490, 440 },
139 .full_name =
"standard",
140 .tag =
MKTAG(
'a',
'p',
'c',
'n'),
143 .br_tab = { 1050, 808, 710, 632 },
147 .full_name =
"high quality",
148 .tag =
MKTAG(
'a',
'p',
'c',
'h'),
151 .br_tab = { 1566, 1216, 1070, 950 },
157 #define TRELLIS_WIDTH 16
158 #define SCORE_LIMIT INT_MAX / 2
167 #define MAX_STORED_Q 16
212 int linesize,
int x,
int y,
int w,
int h,
213 DCTELEM *blocks, uint16_t *emu_buf,
214 int mbs_per_slice,
int blocks_per_mb,
int is_chroma)
216 const uint16_t *esrc;
217 const int mb_width = 4 * blocks_per_mb;
221 for (i = 0; i < mbs_per_slice; i++, src += mb_width) {
223 memset(blocks, 0, 64 * (mbs_per_slice - i) * blocks_per_mb
227 if (x + mb_width <= w && y + 16 <= h) {
229 elinesize = linesize;
234 elinesize = 16 *
sizeof(*emu_buf);
236 bw =
FFMIN(w - x, mb_width);
237 bh =
FFMIN(h - y, 16);
239 for (j = 0; j < bh; j++) {
240 memcpy(emu_buf + j * 16,
241 (
const uint8_t*)src + j * linesize,
243 pix = emu_buf[j * 16 + bw - 1];
244 for (k = bw; k < mb_width; k++)
245 emu_buf[j * 16 + k] = pix;
248 memcpy(emu_buf + j * 16,
249 emu_buf + (bh - 1) * 16,
250 mb_width *
sizeof(*emu_buf));
253 ctx->
dsp.
fdct(esrc, elinesize, blocks);
255 if (blocks_per_mb > 2) {
256 ctx->
dsp.
fdct(esrc + 8, elinesize, blocks);
259 ctx->
dsp.
fdct(esrc + elinesize * 4, elinesize, blocks);
261 if (blocks_per_mb > 2) {
262 ctx->
dsp.
fdct(esrc + elinesize * 4 + 8, elinesize, blocks);
266 ctx->
dsp.
fdct(esrc, elinesize, blocks);
268 ctx->
dsp.
fdct(esrc + elinesize * 4, elinesize, blocks);
270 if (blocks_per_mb > 2) {
271 ctx->
dsp.
fdct(esrc + 8, elinesize, blocks);
273 ctx->
dsp.
fdct(esrc + elinesize * 4 + 8, elinesize, blocks);
287 unsigned int rice_order, exp_order, switch_bits, switch_val;
291 switch_bits = (codebook & 3) + 1;
292 rice_order = codebook >> 5;
293 exp_order = (codebook >> 2) & 7;
295 switch_val = switch_bits << rice_order;
297 if (val >= switch_val) {
298 val -= switch_val - (1 << exp_order);
301 put_bits(pb, exponent - exp_order + switch_bits, 0);
304 exponent = val >> rice_order;
314 #define GET_SIGN(x) ((x) >> 31)
315 #define MAKE_CODE(x) (((x) << 1) ^ GET_SIGN(x))
318 int blocks_per_slice,
int scale)
321 int codebook = 3, code, dc, prev_dc,
delta, sign, new_sign;
323 prev_dc = (blocks[0] - 0x4000) / scale;
329 for (i = 1; i < blocks_per_slice; i++, blocks += 64) {
330 dc = (blocks[0] - 0x4000) / scale;
331 delta = dc - prev_dc;
333 delta = (delta ^ sign) - sign;
336 codebook = (code + (code & 1)) >> 1;
337 codebook =
FFMIN(codebook, 3);
344 int blocks_per_slice,
345 int plane_size_factor,
346 const uint8_t *scan,
const int16_t *qmat)
350 int max_coeffs, abs_level;
352 max_coeffs = blocks_per_slice << 6;
357 for (i = 1; i < 64; i++) {
358 for (idx = scan[i]; idx < max_coeffs; idx += 64) {
359 level = blocks[idx] / qmat[scan[i]];
361 abs_level =
FFABS(level);
378 const uint16_t *src,
int linesize,
379 int mbs_per_slice,
DCTELEM *blocks,
380 int blocks_per_mb,
int plane_size_factor,
383 int blocks_per_slice, saved_pos;
386 blocks_per_slice = mbs_per_slice * blocks_per_mb;
388 encode_dcs(pb, blocks, blocks_per_slice, qmat[0]);
389 encode_acs(pb, blocks, blocks_per_slice, plane_size_factor,
405 int slice_width_factor =
av_log2(mbs_per_slice);
406 int num_cblocks, pwidth, linesize, line_add;
407 int plane_factor, is_chroma;
421 for (i = 0; i < 64; i++)
426 is_chroma = (i == 1 || i == 2);
427 plane_factor = slice_width_factor + 2;
434 pwidth = avctx->
width;
439 pwidth = avctx->
width >> 1;
443 src = (
const uint16_t*)(pic->
data[i] + yp * linesize +
449 mbs_per_slice, num_cblocks, is_chroma);
451 mbs_per_slice, ctx->
blocks[0],
452 num_cblocks, plane_factor,
454 total_size += sizes[i];
461 unsigned int rice_order, exp_order, switch_bits, switch_val;
465 switch_bits = (codebook & 3) + 1;
466 rice_order = codebook >> 5;
467 exp_order = (codebook >> 2) & 7;
469 switch_val = switch_bits << rice_order;
471 if (val >= switch_val) {
472 val -= switch_val - (1 << exp_order);
475 return exponent * 2 - exp_order + switch_bits + 1;
477 return (val >> rice_order) + rice_order + 1;
485 int codebook = 3, code, dc, prev_dc,
delta, sign, new_sign;
488 prev_dc = (blocks[0] - 0x4000) / scale;
495 for (i = 1; i < blocks_per_slice; i++, blocks += 64) {
496 dc = (blocks[0] - 0x4000) / scale;
498 delta = dc - prev_dc;
500 delta = (delta ^ sign) - sign;
503 codebook = (code + (code & 1)) >> 1;
504 codebook =
FFMIN(codebook, 3);
513 int plane_size_factor,
514 const uint8_t *scan,
const int16_t *qmat)
518 int max_coeffs, abs_level;
521 max_coeffs = blocks_per_slice << 6;
526 for (i = 1; i < 64; i++) {
527 for (idx = scan[i]; idx < max_coeffs; idx += 64) {
528 level = blocks[idx] / qmat[scan[i]];
529 *error +=
FFABS(blocks[idx]) % qmat[scan[i]];
531 abs_level =
FFABS(level);
549 const uint16_t *src,
int linesize,
551 int blocks_per_mb,
int plane_size_factor,
554 int blocks_per_slice;
557 blocks_per_slice = mbs_per_slice * blocks_per_mb;
567 int trellis_node,
int x,
int y,
int mbs_per_slice,
571 int i, q, pq, xp, yp;
573 int slice_width_factor =
av_log2(mbs_per_slice);
578 int error,
bits, bits_limit;
579 int mbs, prev, cur, new_score;
583 int linesize[4], line_add;
589 mbs = x + mbs_per_slice;
592 is_chroma[i] = (i == 1 || i == 2);
593 plane_factor[i] = slice_width_factor + 2;
600 pwidth = avctx->
width;
605 pwidth = avctx->
width >> 1;
609 src = (
const uint16_t*)(pic->
data[i] + yp * linesize[i] +
615 mbs_per_slice, num_cblocks[i], is_chroma[i]);
618 for (q = min_quant; q < max_quant + 2; q++) {
624 for (q = min_quant; q <=
max_quant; q++) {
631 num_cblocks[i], plane_factor[i],
634 if (bits > 65000 * 8) {
638 slice_bits[q] =
bits;
639 slice_score[q] = error;
641 if (slice_bits[max_quant] <= ctx->
bits_per_mb * mbs_per_slice) {
642 slice_bits[max_quant + 1] = slice_bits[
max_quant];
643 slice_score[max_quant + 1] = slice_score[
max_quant] + 1;
646 for (q = max_quant + 1; q < 128; q++) {
653 for (i = 0; i < 64; i++)
660 num_cblocks[i], plane_factor[i],
663 if (bits <= ctx->bits_per_mb * mbs_per_slice)
667 slice_bits[max_quant + 1] =
bits;
668 slice_score[max_quant + 1] = error;
671 td->
nodes[trellis_node + max_quant + 1].
quant = overquant;
674 for (pq = min_quant; pq < max_quant + 2; pq++) {
677 for (q = min_quant; q < max_quant + 2; q++) {
678 cur = trellis_node + q;
680 bits = td->
nodes[prev].
bits + slice_bits[q];
681 error = slice_score[q];
682 if (bits > bits_limit)
701 for (q = min_quant + 1; q < max_quant + 2; q++) {
702 if (td->
nodes[trellis_node + q].
score <= error) {
704 pq = trellis_node + q;
712 int jobnr,
int threadnr)
717 int x, y = jobnr, mb, q = 0;
719 for (x = mb = 0; x < ctx->
mb_width; x += mbs_per_slice, mb++) {
720 while (ctx->
mb_width - x < mbs_per_slice)
736 const AVFrame *pic,
int *got_packet)
739 uint8_t *orig_buf, *buf, *slice_hdr, *slice_sizes, *tmp;
742 int x, y, i, mb, q = 0;
743 int sizes[4] = { 0 };
744 int slice_hdr_size = 2 + 2 * (ctx->
num_planes - 1);
760 orig_buf = pkt->
data;
764 bytestream_put_be32 (&orig_buf,
FRAME_ID);
770 bytestream_put_be16 (&buf, 0);
772 bytestream_put_be16 (&buf, avctx->
width);
773 bytestream_put_be16 (&buf, avctx->
height);
778 bytestream_put_byte (&buf, frame_flags);
780 bytestream_put_byte (&buf, 0);
782 bytestream_put_byte (&buf, avctx->
color_trc);
783 bytestream_put_byte (&buf, avctx->
colorspace);
784 bytestream_put_byte (&buf, 0x40);
785 bytestream_put_byte (&buf, 0);
787 bytestream_put_byte (&buf, 0x03);
789 for (i = 0; i < 64; i++)
790 bytestream_put_byte(&buf, ctx->
quant_mat[i]);
792 for (i = 0; i < 64; i++)
793 bytestream_put_byte(&buf, ctx->
quant_mat[i]);
795 bytestream_put_byte (&buf, 0x00);
797 bytestream_put_be16 (&tmp, buf - orig_buf);
803 picture_size_pos = buf + 1;
804 bytestream_put_byte (&buf, 0x40);
823 for (x = mb = 0; x < ctx->
mb_width; x += mbs_per_slice, mb++) {
827 while (ctx->
mb_width - x < mbs_per_slice)
830 bytestream_put_byte(&buf, slice_hdr_size << 3);
832 buf += slice_hdr_size - 1;
834 encode_slice(avctx, pic, &pb, sizes, x, y, q, mbs_per_slice);
836 bytestream_put_byte(&slice_hdr, q);
837 slice_size = slice_hdr_size + sizes[ctx->
num_planes - 1];
839 bytestream_put_be16(&slice_hdr, sizes[i]);
840 slice_size += sizes[i];
842 bytestream_put_be16(&slice_sizes, slice_size);
843 buf += slice_size - slice_hdr_size;
848 picture_size = buf - picture_size_pos - 6;
850 picture_size = buf - picture_size_pos + 1;
851 bytestream_put_be32(&picture_size_pos, picture_size);
855 frame_size = buf - orig_buf;
856 bytestream_put_be32(&orig_buf, frame_size);
901 if (mps & (mps - 1)) {
903 "there should be an integer power of two MBs per slice\n");
930 if (strlen(ctx->
vendor) != 4) {
951 for (j = 0; j < 64; j++)
975 for (i = min_quant; i < max_quant + 2; i++) {
989 for (j = 0; j < 64; j++) {
1010 "profile %d, %d slices, interlacing: %s, %d bits per MB\n",
1019 #define OFFSET(x) offsetof(ProresContext, x)
1020 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1023 {
"mbs_per_slice",
"macroblocks per slice",
OFFSET(mbs_per_slice),
1029 0, 0,
VE,
"profile" },
1031 0, 0,
VE,
"profile" },
1033 0, 0,
VE,
"profile" },
1035 0, 0,
VE,
"profile" },
1036 {
"vendor",
"vendor ID",
OFFSET(vendor),
1038 {
"bits_per_mb",
"desired bits per macroblock",
OFFSET(bits_per_mb),
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" },
1053 0, 0,
VE,
"quant_mat" },