43 #define DEFAULT_SLICE_MB_WIDTH 8
57 static const int bitrate_table[] = { 1000, 2100, 3500, 5400, 7000, 10000};
68 4, 7, 9, 11, 13, 14, 15, 63,
69 7, 7, 11, 12, 14, 15, 63, 63,
70 9, 11, 13, 14, 15, 63, 63, 63,
71 11, 11, 13, 14, 63, 63, 63, 63,
72 11, 13, 14, 63, 63, 63, 63, 63,
73 13, 14, 63, 63, 63, 63, 63, 63,
74 13, 63, 63, 63, 63, 63, 63, 63,
75 63, 63, 63, 63, 63, 63, 63, 63
77 4, 5, 6, 7, 9, 11, 13, 15,
78 5, 5, 7, 8, 11, 13, 15, 17,
79 6, 7, 9, 11, 13, 15, 15, 17,
80 7, 7, 9, 11, 13, 15, 17, 19,
81 7, 9, 11, 13, 14, 16, 19, 23,
82 9, 11, 13, 14, 16, 19, 23, 29,
83 9, 11, 13, 15, 17, 21, 28, 35,
84 11, 13, 16, 17, 21, 28, 35, 41
86 4, 4, 5, 5, 6, 7, 7, 9,
87 4, 4, 5, 6, 7, 7, 9, 9,
88 5, 5, 6, 7, 7, 9, 9, 10,
89 5, 5, 6, 7, 7, 9, 9, 10,
90 5, 6, 7, 7, 8, 9, 10, 12,
91 6, 7, 7, 8, 9, 10, 12, 15,
92 6, 7, 7, 9, 10, 11, 14, 17,
93 7, 7, 9, 10, 11, 14, 17, 21
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, 5,
99 4, 4, 4, 4, 4, 4, 5, 5,
100 4, 4, 4, 4, 4, 5, 5, 6,
101 4, 4, 4, 4, 5, 5, 6, 7,
102 4, 4, 4, 4, 5, 6, 7, 7
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,
107 4, 4, 4, 4, 4, 4, 4, 5,
108 4, 4, 4, 4, 4, 4, 5, 5,
109 4, 4, 4, 4, 4, 5, 5, 6,
110 4, 4, 4, 4, 5, 5, 6, 7,
111 4, 4, 4, 4, 5, 6, 7, 7
113 2, 2, 2, 2, 2, 2, 2, 2,
114 2, 2, 2, 2, 2, 2, 2, 2,
115 2, 2, 2, 2, 2, 2, 2, 2,
116 2, 2, 2, 2, 2, 2, 2, 3,
117 2, 2, 2, 2, 2, 2, 3, 3,
118 2, 2, 2, 2, 2, 3, 3, 3,
119 2, 2, 2, 2, 3, 3, 3, 4,
120 2, 2, 2, 2, 3, 3, 4, 4,
126 4, 7, 9, 11, 13, 14, 63, 63,
127 7, 7, 11, 12, 14, 63, 63, 63,
128 9, 11, 13, 14, 63, 63, 63, 63,
129 11, 11, 13, 14, 63, 63, 63, 63,
130 11, 13, 14, 63, 63, 63, 63, 63,
131 13, 14, 63, 63, 63, 63, 63, 63,
132 13, 63, 63, 63, 63, 63, 63, 63,
133 63, 63, 63, 63, 63, 63, 63, 63
135 4, 5, 6, 7, 9, 11, 13, 15,
136 5, 5, 7, 8, 11, 13, 15, 17,
137 6, 7, 9, 11, 13, 15, 15, 17,
138 7, 7, 9, 11, 13, 15, 17, 19,
139 7, 9, 11, 13, 14, 16, 19, 23,
140 9, 11, 13, 14, 16, 19, 23, 29,
141 9, 11, 13, 15, 17, 21, 28, 35,
142 11, 13, 16, 17, 21, 28, 35, 41
144 4, 4, 5, 5, 6, 7, 7, 9,
145 4, 4, 5, 6, 7, 7, 9, 9,
146 5, 5, 6, 7, 7, 9, 9, 10,
147 5, 5, 6, 7, 7, 9, 9, 10,
148 5, 6, 7, 7, 8, 9, 10, 12,
149 6, 7, 7, 8, 9, 10, 12, 15,
150 6, 7, 7, 9, 10, 11, 14, 17,
151 7, 7, 9, 10, 11, 14, 17, 21
153 4, 4, 4, 4, 4, 4, 4, 4,
154 4, 4, 4, 4, 4, 4, 4, 4,
155 4, 4, 4, 4, 4, 4, 4, 4,
156 4, 4, 4, 4, 4, 4, 4, 5,
157 4, 4, 4, 4, 4, 4, 5, 5,
158 4, 4, 4, 4, 4, 5, 5, 6,
159 4, 4, 4, 4, 5, 5, 6, 7,
160 4, 4, 4, 4, 5, 6, 7, 7
162 4, 4, 4, 4, 4, 4, 4, 4,
163 4, 4, 4, 4, 4, 4, 4, 4,
164 4, 4, 4, 4, 4, 4, 4, 4,
165 4, 4, 4, 4, 4, 4, 4, 5,
166 4, 4, 4, 4, 4, 4, 5, 5,
167 4, 4, 4, 4, 4, 5, 5, 6,
168 4, 4, 4, 4, 5, 5, 6, 7,
169 4, 4, 4, 4, 5, 6, 7, 7
171 4, 4, 4, 4, 4, 4, 4, 4,
172 4, 4, 4, 4, 4, 4, 4, 4,
173 4, 4, 4, 4, 4, 4, 4, 4,
174 4, 4, 4, 4, 4, 4, 4, 5,
175 4, 4, 4, 4, 4, 4, 5, 5,
176 4, 4, 4, 4, 4, 5, 5, 6,
177 4, 4, 4, 4, 5, 5, 6, 7,
178 4, 4, 4, 4, 5, 6, 7, 7
191 int qmat_luma[16][64];
192 int qmat_chroma[16][64];
204 unsigned int rice_order, exp_order, switch_bits, first_exp,
exp, zeros;
211 first_exp = ((switch_bits + 1) << rice_order);
213 if (
val >= first_exp) {
215 val += (1 << exp_order);
217 zeros =
exp - exp_order + switch_bits + 1;
220 }
else if (rice_order) {
230 #define QSCALE(qmat,ind,val) ((val) / ((qmat)[ind]))
231 #define TO_GOLOMB(val) (((val) * 2) ^ ((val) >> 31))
232 #define DIFF_SIGN(val, sign) (((val) >> 31) ^ (sign))
233 #define IS_NEGATIVE(val) ((((val) >> 31) ^ -1) + 1)
234 #define TO_GOLOMB2(val,sign) ((val)==0 ? 0 : ((val) << 1) + (sign))
238 int sign = (
val >> 31);
239 return (
val ^ sign) - sign;
242 #define FIRST_DC_CB 0xB8
244 static const uint8_t
dc_codebook[7] = { 0x04, 0x28, 0x28, 0x4D, 0x4D, 0x70, 0x70};
247 int blocks_per_slice,
int *qmat)
251 int new_dc,
delta, diff_sign, new_code;
253 prev_dc =
QSCALE(qmat, 0, in[0] - 16384);
257 code = 5; sign = 0; idx = 64;
258 for (
i = 1;
i < blocks_per_slice;
i++, idx += 64) {
259 new_dc =
QSCALE(qmat, 0, in[idx] - 16384);
260 delta = new_dc - prev_dc;
272 static const uint8_t
run_to_cb[16] = { 0x06, 0x06, 0x05, 0x05, 0x04, 0x29,
273 0x29, 0x29, 0x29, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x4C };
274 static const uint8_t
lev_to_cb[10] = { 0x04, 0x0A, 0x05, 0x06, 0x04, 0x28,
275 0x28, 0x28, 0x28, 0x4C };
278 int16_t *in,
int blocks_per_slice,
int *qmat,
const uint8_t ff_prores_scan[64])
284 for (
i = 1;
i < 64;
i++) {
285 int indp = ff_prores_scan[
i];
286 for (j = 0; j < blocks_per_slice; j++) {
287 int val =
QSCALE(qmat, indp, in[(j << 6) + indp]);
312 for (
i = 0;
i < 8;
i++) {
334 for (
i = 0;
i < mb_count;
i++) {
343 }
else if (
chroma && is_422){
344 for (
i = 0;
i < mb_count;
i++) {
351 for (
i = 0;
i < mb_count;
i++) {
363 static int encode_slice_plane(int16_t *blocks,
int mb_count, uint8_t *buf,
unsigned buf_size,
int *qmat,
int sub_sample_chroma,
364 const uint8_t ff_prores_scan[64])
366 int blocks_per_slice;
369 blocks_per_slice = mb_count << (2 - sub_sample_chroma);
380 int16_t * blocks_y, int16_t * blocks_u, int16_t * blocks_v,
381 unsigned mb_count, uint8_t *buf,
unsigned data_size,
382 unsigned* y_data_size,
unsigned* u_data_size,
unsigned* v_data_size,
388 buf, data_size,
ctx->qmat_luma[qp - 1], 0,
ctx->scantable);
391 *u_data_size =
encode_slice_plane(blocks_u, mb_count, buf + *y_data_size, data_size - *y_data_size,
392 ctx->qmat_chroma[qp - 1],
ctx->is_422,
ctx->scantable);
394 *v_data_size =
encode_slice_plane(blocks_v, mb_count, buf + *y_data_size + *u_data_size,
395 data_size - *y_data_size - *u_data_size,
396 ctx->qmat_chroma[qp - 1],
ctx->is_422,
ctx->scantable);
399 return *y_data_size + *u_data_size + *v_data_size;
404 const int abits = 16;
406 const int dsize = 1 << dbits - 1;
407 int diff = cur - prev;
410 if (
diff >= (1 << abits) - dsize)
412 if (diff < -dsize || diff > dsize || !
diff) {
436 unsigned mb_count, uint8_t *buf,
unsigned data_size,
unsigned* a_data_size)
438 const int abits = 16;
439 const int mask = (1 << abits) - 1;
440 const int num_coeffs = mb_count * 256;
441 int prev =
mask, cur;
444 int16_t * blocks = (int16_t *)src_a;
461 }
while (idx < num_coeffs);
469 "Underestimated required buffer size.\n");
478 unsigned dst_width,
unsigned dst_height,
int is_alpha_plane,
479 int is_interlaced,
int is_top_field)
482 int i, j, src_stride, box_height;
483 uint16_t last_pix, *last_line;
485 if (!is_interlaced) {
487 src += y * src_stride + x;
491 src += y * src_stride + x;
497 for (
i = 0;
i < box_height; ++
i) {
498 for (j = 0; j < box_width; ++j) {
499 if (!is_alpha_plane) {
502 dst[j] =
src[j] << 6;
505 if (!is_alpha_plane) {
506 last_pix = dst[j - 1];
508 last_pix = dst[j - 1] << 6;
510 for (; j < dst_width; j++)
515 last_line = dst - dst_width;
516 for (;
i < dst_height;
i++) {
517 for (j = 0; j < dst_width; ++j) {
518 dst[j] = last_line[j];
526 unsigned dst_width,
unsigned dst_height,
int is_interlaced,
int is_top_field)
528 subimage_with_fill_template(
src, x, y,
stride,
width,
height, dst, dst_width, dst_height, 0, is_interlaced, is_top_field);
534 unsigned dst_width,
unsigned dst_height,
int is_interlaced,
int is_top_field)
536 subimage_with_fill_template(
src, x, y,
stride,
width,
height, dst, dst_width, dst_height, 1, is_interlaced, is_top_field);
540 int mb_y,
unsigned mb_count, uint8_t *buf,
unsigned data_size,
541 int unsafe,
int *qp,
int is_interlaced,
int is_top_field)
543 int luma_stride, chroma_stride, alpha_stride = 0;
545 int hdr_size = 6 + (
ctx->need_alpha * 2);
546 int ret = 0, slice_size;
547 uint8_t *dest_y, *dest_u, *dest_v;
548 unsigned y_data_size = 0, u_data_size = 0, v_data_size = 0, a_data_size = 0;
551 int low_bytes = (tgt_bits - (tgt_bits >> 3)) >> 3;
552 int high_bytes = (tgt_bits + (tgt_bits >> 3)) >> 3;
564 if (!is_interlaced) {
565 dest_y = pic->
data[0] + (mb_y << 4) * luma_stride + (mb_x << 5);
566 dest_u = pic->
data[1] + (mb_y << 4) * chroma_stride + (mb_x << (5 -
ctx->is_422));
567 dest_v = pic->
data[2] + (mb_y << 4) * chroma_stride + (mb_x << (5 -
ctx->is_422));
569 dest_y = pic->
data[0] + (mb_y << 4) * luma_stride * 2 + (mb_x << 5);
570 dest_u = pic->
data[1] + (mb_y << 4) * chroma_stride * 2 + (mb_x << (5 -
ctx->is_422));
571 dest_v = pic->
data[2] + (mb_y << 4) * chroma_stride * 2 + (mb_x << (5 -
ctx->is_422));
573 dest_y += luma_stride;
574 dest_u += chroma_stride;
575 dest_v += chroma_stride;
582 (uint16_t *)
ctx->fill_y, mb_count << 4, 16, is_interlaced, is_top_field);
585 (uint16_t *)
ctx->fill_u, mb_count << (4 -
ctx->is_422), 16, is_interlaced, is_top_field);
588 (uint16_t *)
ctx->fill_v, mb_count << (4 -
ctx->is_422), 16, is_interlaced, is_top_field);
596 mb_count, buf + hdr_size, data_size - hdr_size,
597 &y_data_size, &u_data_size, &v_data_size,
600 if (!is_interlaced) {
601 calc_plane_dct(fdsp, dest_y, blocks_y, luma_stride, mb_count, 0, 0);
605 calc_plane_dct(fdsp, dest_y, blocks_y, luma_stride * 2, mb_count, 0, 0);
606 calc_plane_dct(fdsp, dest_u, blocks_u, chroma_stride * 2, mb_count, 1,
ctx->is_422);
607 calc_plane_dct(fdsp, dest_v, blocks_v, chroma_stride * 2, mb_count, 1,
ctx->is_422);
611 mb_count, buf + hdr_size, data_size - hdr_size,
612 &y_data_size, &u_data_size, &v_data_size,
619 mb_count, buf + hdr_size, data_size - hdr_size,
620 &y_data_size, &u_data_size, &v_data_size,
623 }
else if (slice_size < low_bytes && *qp
628 mb_count, buf + hdr_size, data_size - hdr_size,
629 &y_data_size, &u_data_size, &v_data_size,
635 buf[0] = hdr_size << 3;
640 if (
ctx->need_alpha) {
645 (uint16_t *)
ctx->fill_a, mb_count << 4, 16, is_interlaced, is_top_field);
647 buf + hdr_size + slice_size,
648 data_size - hdr_size - slice_size, &a_data_size);
654 return hdr_size + y_data_size + u_data_size + v_data_size + a_data_size;
658 uint8_t *buf,
const int buf_size,
const int picture_index,
const int is_top_field)
661 int mb_width = (avctx->
width + 15) >> 4;
662 int hdr_size, sl_size,
i;
663 int mb_y, sl_data_size, qp, mb_height, picture_height, unsafe_mb_height_limit;
664 int unsafe_bot, unsafe_right;
665 uint8_t *sl_data, *sl_data_sizes;
666 int slice_per_line = 0, rem = mb_width;
668 if (!
ctx->is_interlaced) {
669 mb_height = (avctx->
height + 15) >> 4;
670 unsafe_mb_height_limit = mb_height;
673 picture_height = (avctx->
height + 1) / 2;
675 picture_height = avctx->
height / 2;
677 mb_height = (picture_height + 15) >> 4;
678 unsafe_mb_height_limit = mb_height;
682 slice_per_line += rem >>
i;
687 hdr_size = 8; sl_data_size = buf_size - hdr_size;
688 sl_data_sizes = buf + hdr_size;
689 sl_data = sl_data_sizes + (slice_per_line * mb_height * 2);
690 for (mb_y = 0; mb_y < mb_height; mb_y++) {
693 while (mb_x < mb_width) {
694 while (mb_width - mb_x < slice_mb_count)
695 slice_mb_count >>= 1;
697 unsafe_bot = (avctx->
height & 0xf) && (mb_y == unsafe_mb_height_limit - 1);
698 unsafe_right = (avctx->
width & 0xf) && (mb_x + slice_mb_count == mb_width);
700 sl_size =
encode_slice(avctx, pic, mb_x, mb_y, slice_mb_count,
701 sl_data, sl_data_size, unsafe_bot || unsafe_right, &qp,
ctx->is_interlaced, is_top_field);
706 bytestream_put_be16(&sl_data_sizes, sl_size);
708 sl_data_size -= sl_size;
709 mb_x += slice_mb_count;
713 buf[0] = hdr_size << 3;
714 AV_WB32(buf + 1, sl_data - buf);
715 AV_WB16(buf + 5, slice_per_line * mb_height);
718 return sl_data - buf;
722 const AVFrame *pict,
int *got_packet)
725 int header_size = 148;
727 int compress_frame_size, pic_size,
ret, is_top_field_first = 0;
736 compress_frame_size = 8 + header_size;
738 bytestream_put_be32(&buf, compress_frame_size);
741 bytestream_put_be16(&buf, header_size);
742 bytestream_put_be16(&buf, 0);
744 bytestream_put_be16(&buf, avctx->
width);
745 bytestream_put_be16(&buf, avctx->
height);
749 if (
ctx->is_interlaced) {
753 is_top_field_first = 1;
761 *buf++ = frame_flags;
783 pkt->
size - compress_frame_size, 0, is_top_field_first);
787 compress_frame_size += pic_size;
789 if (
ctx->is_interlaced) {
791 pkt->
size - compress_frame_size, 1, !is_top_field_first);
795 compress_frame_size += pic_size;
799 pkt->
size = compress_frame_size;
808 for (
i = 0;
i < 64;
i++)
820 if (
ctx->is_interlaced) {
826 if (avctx->
width & 0x1) {
828 "frame width needs to be multiple of 2\n");
832 if (avctx->
width > 65534 || avctx->
height > 65535) {
834 "The maximum dimensions are 65534x65535\n");
838 if (strlen(
ctx->vendor) != 4) {
847 "encoding with ProRes standard (apcn) profile\n");
851 "encoding with ProRes 4444 (ap4h) profile\n");
855 "encoding with ProRes 4444+ (ap4h) profile\n");
862 "unknown profile %d, use [0 - apco, 1 - apcs, 2 - apcn (default), 3 - apch, 4 - ap4h, 5 - ap4x]\n",
867 "encoding with ProRes 444/Xq (ap4h/ap4x) profile, need YUV444P10 input\n");
872 "encoding with ProRes Proxy/LT/422/422 HQ (apco, apcs, apcn, ap4h) profile, need YUV422P10 input\n");
906 for (
i = 1;
i <= 16;
i++) {
923 #define OFFSET(x) offsetof(ProresContext, x)
924 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
944 .
p.
name =
"prores_aw",