41 #define DEFAULT_SLICE_MB_WIDTH 8
55 static const int bitrate_table[] = { 1000, 2100, 3500, 5400, 7000, 10000};
66 4, 7, 9, 11, 13, 14, 15, 63,
67 7, 7, 11, 12, 14, 15, 63, 63,
68 9, 11, 13, 14, 15, 63, 63, 63,
69 11, 11, 13, 14, 63, 63, 63, 63,
70 11, 13, 14, 63, 63, 63, 63, 63,
71 13, 14, 63, 63, 63, 63, 63, 63,
72 13, 63, 63, 63, 63, 63, 63, 63,
73 63, 63, 63, 63, 63, 63, 63, 63
75 4, 5, 6, 7, 9, 11, 13, 15,
76 5, 5, 7, 8, 11, 13, 15, 17,
77 6, 7, 9, 11, 13, 15, 15, 17,
78 7, 7, 9, 11, 13, 15, 17, 19,
79 7, 9, 11, 13, 14, 16, 19, 23,
80 9, 11, 13, 14, 16, 19, 23, 29,
81 9, 11, 13, 15, 17, 21, 28, 35,
82 11, 13, 16, 17, 21, 28, 35, 41
84 4, 4, 5, 5, 6, 7, 7, 9,
85 4, 4, 5, 6, 7, 7, 9, 9,
86 5, 5, 6, 7, 7, 9, 9, 10,
87 5, 5, 6, 7, 7, 9, 9, 10,
88 5, 6, 7, 7, 8, 9, 10, 12,
89 6, 7, 7, 8, 9, 10, 12, 15,
90 6, 7, 7, 9, 10, 11, 14, 17,
91 7, 7, 9, 10, 11, 14, 17, 21
93 4, 4, 4, 4, 4, 4, 4, 4,
94 4, 4, 4, 4, 4, 4, 4, 4,
95 4, 4, 4, 4, 4, 4, 4, 4,
96 4, 4, 4, 4, 4, 4, 4, 5,
97 4, 4, 4, 4, 4, 4, 5, 5,
98 4, 4, 4, 4, 4, 5, 5, 6,
99 4, 4, 4, 4, 5, 5, 6, 7,
100 4, 4, 4, 4, 5, 6, 7, 7
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, 5,
106 4, 4, 4, 4, 4, 4, 5, 5,
107 4, 4, 4, 4, 4, 5, 5, 6,
108 4, 4, 4, 4, 5, 5, 6, 7,
109 4, 4, 4, 4, 5, 6, 7, 7
111 2, 2, 2, 2, 2, 2, 2, 2,
112 2, 2, 2, 2, 2, 2, 2, 2,
113 2, 2, 2, 2, 2, 2, 2, 2,
114 2, 2, 2, 2, 2, 2, 2, 3,
115 2, 2, 2, 2, 2, 2, 3, 3,
116 2, 2, 2, 2, 2, 3, 3, 3,
117 2, 2, 2, 2, 3, 3, 3, 4,
118 2, 2, 2, 2, 3, 3, 4, 4,
124 4, 7, 9, 11, 13, 14, 63, 63,
125 7, 7, 11, 12, 14, 63, 63, 63,
126 9, 11, 13, 14, 63, 63, 63, 63,
127 11, 11, 13, 14, 63, 63, 63, 63,
128 11, 13, 14, 63, 63, 63, 63, 63,
129 13, 14, 63, 63, 63, 63, 63, 63,
130 13, 63, 63, 63, 63, 63, 63, 63,
131 63, 63, 63, 63, 63, 63, 63, 63
133 4, 5, 6, 7, 9, 11, 13, 15,
134 5, 5, 7, 8, 11, 13, 15, 17,
135 6, 7, 9, 11, 13, 15, 15, 17,
136 7, 7, 9, 11, 13, 15, 17, 19,
137 7, 9, 11, 13, 14, 16, 19, 23,
138 9, 11, 13, 14, 16, 19, 23, 29,
139 9, 11, 13, 15, 17, 21, 28, 35,
140 11, 13, 16, 17, 21, 28, 35, 41
142 4, 4, 5, 5, 6, 7, 7, 9,
143 4, 4, 5, 6, 7, 7, 9, 9,
144 5, 5, 6, 7, 7, 9, 9, 10,
145 5, 5, 6, 7, 7, 9, 9, 10,
146 5, 6, 7, 7, 8, 9, 10, 12,
147 6, 7, 7, 8, 9, 10, 12, 15,
148 6, 7, 7, 9, 10, 11, 14, 17,
149 7, 7, 9, 10, 11, 14, 17, 21
151 4, 4, 4, 4, 4, 4, 4, 4,
152 4, 4, 4, 4, 4, 4, 4, 4,
153 4, 4, 4, 4, 4, 4, 4, 4,
154 4, 4, 4, 4, 4, 4, 4, 5,
155 4, 4, 4, 4, 4, 4, 5, 5,
156 4, 4, 4, 4, 4, 5, 5, 6,
157 4, 4, 4, 4, 5, 5, 6, 7,
158 4, 4, 4, 4, 5, 6, 7, 7
160 4, 4, 4, 4, 4, 4, 4, 4,
161 4, 4, 4, 4, 4, 4, 4, 4,
162 4, 4, 4, 4, 4, 4, 4, 4,
163 4, 4, 4, 4, 4, 4, 4, 5,
164 4, 4, 4, 4, 4, 4, 5, 5,
165 4, 4, 4, 4, 4, 5, 5, 6,
166 4, 4, 4, 4, 5, 5, 6, 7,
167 4, 4, 4, 4, 5, 6, 7, 7
169 4, 4, 4, 4, 4, 4, 4, 4,
170 4, 4, 4, 4, 4, 4, 4, 4,
171 4, 4, 4, 4, 4, 4, 4, 4,
172 4, 4, 4, 4, 4, 4, 4, 5,
173 4, 4, 4, 4, 4, 4, 5, 5,
174 4, 4, 4, 4, 4, 5, 5, 6,
175 4, 4, 4, 4, 5, 5, 6, 7,
176 4, 4, 4, 4, 5, 6, 7, 7
189 int qmat_luma[16][64];
190 int qmat_chroma[16][64];
210 const int *array_valid_values,
int default_value)
215 int ref_val = array_valid_values[
i];
216 if (ref_val == INT_MAX)
224 "%s %d are not supported. Set to default value : %d\n",
225 val_name,
val, default_value);
226 return default_value;
231 unsigned int rice_order, exp_order, switch_bits, first_exp,
exp, zeros;
238 first_exp = ((switch_bits + 1) << rice_order);
240 if (
val >= first_exp) {
242 val += (1 << exp_order);
244 zeros =
exp - exp_order + switch_bits + 1;
247 }
else if (rice_order) {
257 #define QSCALE(qmat,ind,val) ((val) / ((qmat)[ind]))
258 #define TO_GOLOMB(val) (((val) * 2) ^ ((val) >> 31))
259 #define DIFF_SIGN(val, sign) (((val) >> 31) ^ (sign))
260 #define IS_NEGATIVE(val) ((((val) >> 31) ^ -1) + 1)
261 #define TO_GOLOMB2(val,sign) ((val)==0 ? 0 : ((val) << 1) + (sign))
265 int sign = (
val >> 31);
266 return (
val ^ sign) - sign;
269 #define FIRST_DC_CB 0xB8
271 static const uint8_t
dc_codebook[7] = { 0x04, 0x28, 0x28, 0x4D, 0x4D, 0x70, 0x70};
274 int blocks_per_slice,
int *qmat)
278 int new_dc,
delta, diff_sign, new_code;
280 prev_dc =
QSCALE(qmat, 0, in[0] - 16384);
284 code = 5; sign = 0; idx = 64;
285 for (
i = 1;
i < blocks_per_slice;
i++, idx += 64) {
286 new_dc =
QSCALE(qmat, 0, in[idx] - 16384);
287 delta = new_dc - prev_dc;
299 static const uint8_t
run_to_cb[16] = { 0x06, 0x06, 0x05, 0x05, 0x04, 0x29,
300 0x29, 0x29, 0x29, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x4C };
301 static const uint8_t
lev_to_cb[10] = { 0x04, 0x0A, 0x05, 0x06, 0x04, 0x28,
302 0x28, 0x28, 0x28, 0x4C };
305 int16_t *in,
int blocks_per_slice,
int *qmat,
const uint8_t ff_prores_scan[64])
311 for (
i = 1;
i < 64;
i++) {
312 int indp = ff_prores_scan[
i];
313 for (j = 0; j < blocks_per_slice; j++) {
314 int val =
QSCALE(qmat, indp, in[(j << 6) + indp]);
339 for (
i = 0;
i < 8;
i++) {
361 for (
i = 0;
i < mb_count;
i++) {
370 }
else if (
chroma && is_422){
371 for (
i = 0;
i < mb_count;
i++) {
378 for (
i = 0;
i < mb_count;
i++) {
390 static int encode_slice_plane(int16_t *blocks,
int mb_count, uint8_t *buf,
unsigned buf_size,
int *qmat,
int sub_sample_chroma,
391 const uint8_t ff_prores_scan[64])
393 int blocks_per_slice;
396 blocks_per_slice = mb_count << (2 - sub_sample_chroma);
407 int16_t * blocks_y, int16_t * blocks_u, int16_t * blocks_v,
408 unsigned mb_count, uint8_t *buf,
unsigned data_size,
409 unsigned* y_data_size,
unsigned* u_data_size,
unsigned* v_data_size,
415 buf, data_size,
ctx->qmat_luma[qp - 1], 0,
ctx->scantable);
418 *u_data_size =
encode_slice_plane(blocks_u, mb_count, buf + *y_data_size, data_size - *y_data_size,
419 ctx->qmat_chroma[qp - 1],
ctx->is_422,
ctx->scantable);
421 *v_data_size =
encode_slice_plane(blocks_v, mb_count, buf + *y_data_size + *u_data_size,
422 data_size - *y_data_size - *u_data_size,
423 ctx->qmat_chroma[qp - 1],
ctx->is_422,
ctx->scantable);
426 return *y_data_size + *u_data_size + *v_data_size;
431 const int abits = 16;
433 const int dsize = 1 << dbits - 1;
434 int diff = cur - prev;
437 if (
diff >= (1 << abits) - dsize)
439 if (diff < -dsize || diff > dsize || !
diff) {
463 unsigned mb_count, uint8_t *buf,
unsigned data_size,
unsigned* a_data_size)
465 const int abits = 16;
466 const int mask = (1 << abits) - 1;
467 const int num_coeffs = mb_count * 256;
468 int prev =
mask, cur;
471 int16_t * blocks = (int16_t *)src_a;
488 }
while (idx < num_coeffs);
496 "Underestimated required buffer size.\n");
505 unsigned dst_width,
unsigned dst_height,
int is_alpha_plane,
506 int is_interlaced,
int is_top_field)
509 int i, j, src_stride, box_height;
510 uint16_t last_pix, *last_line;
512 if (!is_interlaced) {
514 src += y * src_stride + x;
518 src += y * src_stride + x;
524 for (
i = 0;
i < box_height; ++
i) {
525 for (j = 0; j < box_width; ++j) {
526 if (!is_alpha_plane) {
529 dst[j] =
src[j] << 6;
532 if (!is_alpha_plane) {
533 last_pix = dst[j - 1];
535 last_pix = dst[j - 1] << 6;
537 for (; j < dst_width; j++)
542 last_line = dst - dst_width;
543 for (;
i < dst_height;
i++) {
544 for (j = 0; j < dst_width; ++j) {
545 dst[j] = last_line[j];
553 unsigned dst_width,
unsigned dst_height,
int is_interlaced,
int is_top_field)
555 subimage_with_fill_template(
src, x, y,
stride,
width,
height, dst, dst_width, dst_height, 0, is_interlaced, is_top_field);
561 unsigned dst_width,
unsigned dst_height,
int is_interlaced,
int is_top_field)
563 subimage_with_fill_template(
src, x, y,
stride,
width,
height, dst, dst_width, dst_height, 1, is_interlaced, is_top_field);
567 int mb_y,
unsigned mb_count, uint8_t *buf,
unsigned data_size,
568 int unsafe,
int *qp,
int is_interlaced,
int is_top_field)
570 int luma_stride, chroma_stride, alpha_stride = 0;
572 int hdr_size = 6 + (
ctx->need_alpha * 2);
573 int ret = 0, slice_size;
574 const uint8_t *dest_y, *dest_u, *dest_v;
575 unsigned y_data_size = 0, u_data_size = 0, v_data_size = 0, a_data_size = 0;
578 int low_bytes = (tgt_bits - (tgt_bits >> 3)) >> 3;
579 int high_bytes = (tgt_bits + (tgt_bits >> 3)) >> 3;
591 if (!is_interlaced) {
592 dest_y = pic->
data[0] + (mb_y << 4) * luma_stride + (mb_x << 5);
593 dest_u = pic->
data[1] + (mb_y << 4) * chroma_stride + (mb_x << (5 -
ctx->is_422));
594 dest_v = pic->
data[2] + (mb_y << 4) * chroma_stride + (mb_x << (5 -
ctx->is_422));
596 dest_y = pic->
data[0] + (mb_y << 4) * luma_stride * 2 + (mb_x << 5);
597 dest_u = pic->
data[1] + (mb_y << 4) * chroma_stride * 2 + (mb_x << (5 -
ctx->is_422));
598 dest_v = pic->
data[2] + (mb_y << 4) * chroma_stride * 2 + (mb_x << (5 -
ctx->is_422));
600 dest_y += luma_stride;
601 dest_u += chroma_stride;
602 dest_v += chroma_stride;
609 (uint16_t *)
ctx->fill_y, mb_count << 4, 16, is_interlaced, is_top_field);
612 (uint16_t *)
ctx->fill_u, mb_count << (4 -
ctx->is_422), 16, is_interlaced, is_top_field);
615 (uint16_t *)
ctx->fill_v, mb_count << (4 -
ctx->is_422), 16, is_interlaced, is_top_field);
623 mb_count, buf + hdr_size, data_size - hdr_size,
624 &y_data_size, &u_data_size, &v_data_size,
627 if (!is_interlaced) {
628 calc_plane_dct(fdsp, dest_y, blocks_y, luma_stride, mb_count, 0, 0);
632 calc_plane_dct(fdsp, dest_y, blocks_y, luma_stride * 2, mb_count, 0, 0);
633 calc_plane_dct(fdsp, dest_u, blocks_u, chroma_stride * 2, mb_count, 1,
ctx->is_422);
634 calc_plane_dct(fdsp, dest_v, blocks_v, chroma_stride * 2, mb_count, 1,
ctx->is_422);
638 mb_count, buf + hdr_size, data_size - hdr_size,
639 &y_data_size, &u_data_size, &v_data_size,
646 mb_count, buf + hdr_size, data_size - hdr_size,
647 &y_data_size, &u_data_size, &v_data_size,
650 }
else if (slice_size < low_bytes && *qp
655 mb_count, buf + hdr_size, data_size - hdr_size,
656 &y_data_size, &u_data_size, &v_data_size,
662 buf[0] = hdr_size << 3;
667 if (
ctx->need_alpha) {
672 (uint16_t *)
ctx->fill_a, mb_count << 4, 16, is_interlaced, is_top_field);
674 buf + hdr_size + slice_size,
675 data_size - hdr_size - slice_size, &a_data_size);
681 return hdr_size + y_data_size + u_data_size + v_data_size + a_data_size;
685 uint8_t *buf,
const int buf_size,
const int picture_index,
const int is_top_field)
688 int mb_width = (avctx->
width + 15) >> 4;
689 int hdr_size, sl_size,
i;
690 int mb_y, sl_data_size, qp, mb_height, picture_height, unsafe_mb_height_limit;
691 int unsafe_bot, unsafe_right;
692 uint8_t *sl_data, *sl_data_sizes;
693 int slice_per_line = 0, rem = mb_width;
695 if (!
ctx->is_interlaced) {
696 mb_height = (avctx->
height + 15) >> 4;
697 unsafe_mb_height_limit = mb_height;
700 picture_height = (avctx->
height + 1) / 2;
702 picture_height = avctx->
height / 2;
704 mb_height = (picture_height + 15) >> 4;
705 unsafe_mb_height_limit = mb_height;
709 slice_per_line += rem >>
i;
714 hdr_size = 8; sl_data_size = buf_size - hdr_size;
715 sl_data_sizes = buf + hdr_size;
716 sl_data = sl_data_sizes + (slice_per_line * mb_height * 2);
717 for (mb_y = 0; mb_y < mb_height; mb_y++) {
720 while (mb_x < mb_width) {
721 while (mb_width - mb_x < slice_mb_count)
722 slice_mb_count >>= 1;
724 unsafe_bot = (avctx->
height & 0xf) && (mb_y == unsafe_mb_height_limit - 1);
725 unsafe_right = (avctx->
width & 0xf) && (mb_x + slice_mb_count == mb_width);
727 sl_size =
encode_slice(avctx, pic, mb_x, mb_y, slice_mb_count,
728 sl_data, sl_data_size, unsafe_bot || unsafe_right, &qp,
ctx->is_interlaced, is_top_field);
733 bytestream_put_be16(&sl_data_sizes, sl_size);
735 sl_data_size -= sl_size;
736 mb_x += slice_mb_count;
740 buf[0] = hdr_size << 3;
741 AV_WB32(buf + 1, sl_data - buf);
742 AV_WB16(buf + 5, slice_per_line * mb_height);
745 return sl_data - buf;
749 const AVFrame *pict,
int *got_packet)
752 int header_size = 148;
754 int compress_frame_size, pic_size,
ret, is_top_field_first = 0;
763 compress_frame_size = 8 + header_size;
765 bytestream_put_be32(&buf, compress_frame_size);
768 bytestream_put_be16(&buf, header_size);
769 bytestream_put_be16(&buf, 0);
771 bytestream_put_be16(&buf, avctx->
width);
772 bytestream_put_be16(&buf, avctx->
height);
776 if (
ctx->is_interlaced) {
781 is_top_field_first = 1;
789 *buf++ = frame_flags;
814 pkt->
size - compress_frame_size, 0, is_top_field_first);
818 compress_frame_size += pic_size;
820 if (
ctx->is_interlaced) {
822 pkt->
size - compress_frame_size, 1, !is_top_field_first);
826 compress_frame_size += pic_size;
830 pkt->
size = compress_frame_size;
839 for (
i = 0;
i < 64;
i++)
851 if (
ctx->is_interlaced) {
857 if (avctx->
width & 0x1) {
859 "frame width needs to be multiple of 2\n");
863 if (avctx->
width > 65534 || avctx->
height > 65535) {
865 "The maximum dimensions are 65534x65535\n");
869 if (strlen(
ctx->vendor) != 4) {
878 "encoding with ProRes standard (apcn) profile\n");
882 "encoding with ProRes 4444 (ap4h) profile\n");
886 "encoding with ProRes 4444+ (ap4h) profile\n");
893 "unknown profile %d, use [0 - apco, 1 - apcs, 2 - apcn (default), 3 - apch, 4 - ap4h, 5 - ap4x]\n",
898 "encoding with ProRes 444/Xq (ap4h/ap4x) profile, need YUV444P10 input\n");
903 "encoding with ProRes Proxy/LT/422/422 HQ (apco, apcs, apcn, ap4h) profile, need YUV422P10 input\n");
937 for (
i = 1;
i <= 16;
i++) {
954 #define OFFSET(x) offsetof(ProresContext, x)
955 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
975 .
p.
name =
"prores_aw",