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, switch_val;
 
  239     switch_val  = switch_bits << rice_order;
 
  241     if (
val >= switch_val) {
 
  242         val -= switch_val - (1 << exp_order);
 
  245         put_bits(pb, exponent - exp_order + switch_bits, 0);
 
  248         exponent = 
val >> rice_order;
 
  258 #define GET_SIGN(x)  ((x) >> 31) 
  259 #define MAKE_CODE(x) (((x) * 2) ^ GET_SIGN(x)) 
  262                        int blocks_per_slice, 
int scale)
 
  267     prev_dc = (blocks[0] - 0x4000) / 
scale;
 
  272     for (
i = 1; 
i < blocks_per_slice; 
i++, blocks += 64) {
 
  273         dc       = (blocks[0] - 0x4000) / 
scale;
 
  286                        int blocks_per_slice,
 
  287                        int *qmat, 
const uint8_t *scan)
 
  293     int max_coeffs, abs_level;
 
  295     max_coeffs = blocks_per_slice << 6;
 
  297     for (
i = 1; 
i < 64; 
i++) {
 
  298         for (idx = scan[
i]; idx < max_coeffs; idx += 64) {
 
  299             level = blocks[idx] / qmat[scan[
i]];
 
  307                 prev_level = 
FFMIN(abs_level, 9);
 
  320     for (
i = 0; 
i < 8; 
i++) {
 
  342         for (
i = 0; 
i < mb_count; 
i++) {
 
  351     } 
else if (
chroma && is_422){ 
 
  352         for (
i = 0; 
i < mb_count; 
i++) {
 
  359         for (
i = 0; 
i < mb_count; 
i++) {
 
  371 static int encode_slice_plane(int16_t *blocks, 
int mb_count, uint8_t *buf, 
unsigned buf_size, 
int *qmat, 
int sub_sample_chroma,
 
  374     int blocks_per_slice;
 
  377     blocks_per_slice = mb_count << (2 - sub_sample_chroma);
 
  380     encode_dcs(&pb, blocks, blocks_per_slice, qmat[0]);
 
  381     encode_acs(&pb, blocks, blocks_per_slice, qmat, scan);
 
  388                                                    int16_t * blocks_y, int16_t * blocks_u, int16_t * blocks_v,
 
  389                                                    unsigned mb_count, uint8_t *buf, 
unsigned data_size,
 
  390                                                    unsigned* y_data_size, 
unsigned* u_data_size, 
unsigned* v_data_size,
 
  396                                       buf, data_size, 
ctx->qmat_luma[qp - 1], 0, 
ctx->scantable);
 
  399         *u_data_size = 
encode_slice_plane(blocks_u, mb_count, buf + *y_data_size, data_size - *y_data_size,
 
  400                                           ctx->qmat_chroma[qp - 1], 
ctx->is_422, 
ctx->scantable);
 
  402         *v_data_size = 
encode_slice_plane(blocks_v, mb_count, buf + *y_data_size + *u_data_size,
 
  403                                           data_size - *y_data_size - *u_data_size,
 
  404                                           ctx->qmat_chroma[qp - 1], 
ctx->is_422, 
ctx->scantable);
 
  407     return *y_data_size + *u_data_size + *v_data_size;
 
  412     const int abits = 16;
 
  414     const int dsize = 1 << dbits - 1;
 
  415     int diff = cur - prev;
 
  418     if (
diff >= (1 << abits) - dsize)
 
  420     if (diff < -dsize || diff > dsize || !
diff) {
 
  444                                                    unsigned mb_count, uint8_t *buf, 
unsigned data_size, 
unsigned* a_data_size)
 
  446     const int abits = 16;
 
  447     const int mask  = (1 << abits) - 1;
 
  448     const int num_coeffs = mb_count * 256;
 
  449     int prev = 
mask, cur;
 
  452     int16_t * blocks = (int16_t *)src_a;
 
  469     } 
while (idx < num_coeffs);
 
  476                "Underestimated required buffer size.\n");
 
  485                                                unsigned dst_width, 
unsigned dst_height, 
int is_alpha_plane,
 
  486                                                int is_interlaced, 
int is_top_field)
 
  489     int i, j, src_stride, box_height;
 
  490     uint16_t last_pix, *last_line;
 
  492     if (!is_interlaced) {
 
  494         src += y * src_stride + x;
 
  498         src += y * src_stride + x;
 
  504     for (
i = 0; 
i < box_height; ++
i) {
 
  505         for (j = 0; j < box_width; ++j) {
 
  506             if (!is_alpha_plane) {
 
  509                 dst[j] = 
src[j] << 6; 
 
  512         if (!is_alpha_plane) {
 
  513             last_pix = dst[j - 1];
 
  515             last_pix = dst[j - 1] << 6; 
 
  517         for (; j < dst_width; j++)
 
  522     last_line = dst - dst_width;
 
  523     for (; 
i < dst_height; 
i++) {
 
  524         for (j = 0; j < dst_width; ++j) {
 
  525             dst[j] = last_line[j];
 
  533         unsigned dst_width, 
unsigned dst_height, 
int is_interlaced, 
int is_top_field)
 
  535     subimage_with_fill_template(
src, x, y, 
stride, 
width, 
height, dst, dst_width, dst_height, 0, is_interlaced, is_top_field);
 
  541                                unsigned dst_width, 
unsigned dst_height, 
int is_interlaced, 
int is_top_field)
 
  543     subimage_with_fill_template(
src, x, y, 
stride, 
width, 
height, dst, dst_width, dst_height, 1, is_interlaced, is_top_field);
 
  547         int mb_y, 
unsigned mb_count, uint8_t *buf, 
unsigned data_size,
 
  548         int unsafe, 
int *qp, 
int is_interlaced, 
int is_top_field)
 
  550     int luma_stride, chroma_stride, alpha_stride = 0;
 
  552     int hdr_size = 6 + (
ctx->need_alpha * 2); 
 
  553     int ret = 0, slice_size;
 
  554     const uint8_t *dest_y, *dest_u, *dest_v;
 
  555     unsigned y_data_size = 0, u_data_size = 0, v_data_size = 0, a_data_size = 0;
 
  558     int low_bytes  = (tgt_bits - (tgt_bits >> 3)) >> 3; 
 
  559     int high_bytes = (tgt_bits + (tgt_bits >> 3)) >> 3;
 
  571     if (!is_interlaced) {
 
  572         dest_y = pic->
data[0] + (mb_y << 4) * luma_stride   + (mb_x << 5);
 
  573         dest_u = pic->
data[1] + (mb_y << 4) * chroma_stride + (mb_x << (5 - 
ctx->is_422));
 
  574         dest_v = pic->
data[2] + (mb_y << 4) * chroma_stride + (mb_x << (5 - 
ctx->is_422));
 
  576         dest_y = pic->
data[0] + (mb_y << 4) * luma_stride * 2   + (mb_x << 5);
 
  577         dest_u = pic->
data[1] + (mb_y << 4) * chroma_stride * 2 + (mb_x << (5 - 
ctx->is_422));
 
  578         dest_v = pic->
data[2] + (mb_y << 4) * chroma_stride * 2 + (mb_x << (5 - 
ctx->is_422));
 
  580             dest_y += luma_stride;
 
  581             dest_u += chroma_stride;
 
  582             dest_v += chroma_stride;
 
  589                 (uint16_t *) 
ctx->fill_y, mb_count << 4, 16, is_interlaced, is_top_field);
 
  592                            (uint16_t *) 
ctx->fill_u, mb_count << (4 - 
ctx->is_422), 16, is_interlaced, is_top_field);
 
  595                            (uint16_t *) 
ctx->fill_v, mb_count << (4 - 
ctx->is_422), 16, is_interlaced, is_top_field);
 
  603                           mb_count, buf + hdr_size, data_size - hdr_size,
 
  604                           &y_data_size, &u_data_size, &v_data_size,
 
  607         if (!is_interlaced) {
 
  608             calc_plane_dct(fdsp, dest_y, blocks_y, luma_stride, mb_count, 0, 0);
 
  612             calc_plane_dct(fdsp, dest_y, blocks_y, luma_stride   * 2, mb_count, 0, 0);
 
  613             calc_plane_dct(fdsp, dest_u, blocks_u, chroma_stride * 2, mb_count, 1, 
ctx->is_422);
 
  614             calc_plane_dct(fdsp, dest_v, blocks_v, chroma_stride * 2, mb_count, 1, 
ctx->is_422);
 
  618                           mb_count, buf + hdr_size, data_size - hdr_size,
 
  619                           &y_data_size, &u_data_size, &v_data_size,
 
  626                                                mb_count, buf + hdr_size, data_size - hdr_size,
 
  627                                                &y_data_size, &u_data_size, &v_data_size,
 
  630         } 
else if (slice_size < low_bytes && *qp
 
  635                                                mb_count, buf + hdr_size, data_size - hdr_size,
 
  636                                                &y_data_size, &u_data_size, &v_data_size,
 
  642     buf[0] = hdr_size << 3;
 
  647     if (
ctx->need_alpha) {
 
  652                            (uint16_t *) 
ctx->fill_a, mb_count << 4, 16, is_interlaced, is_top_field);
 
  654                                       buf + hdr_size + slice_size,
 
  655                                       data_size - hdr_size - slice_size, &a_data_size);
 
  661     return hdr_size + y_data_size + u_data_size + v_data_size + a_data_size;
 
  665         uint8_t *buf, 
const int buf_size, 
const int picture_index, 
const int is_top_field)
 
  668     int mb_width = (avctx->
width + 15) >> 4;
 
  669     int hdr_size, sl_size, 
i;
 
  670     int mb_y, sl_data_size, qp, mb_height, picture_height, unsafe_mb_height_limit;
 
  671     int unsafe_bot, unsafe_right;
 
  672     uint8_t *sl_data, *sl_data_sizes;
 
  673     int slice_per_line = 0, rem = mb_width;
 
  675     if (!
ctx->is_interlaced) { 
 
  676         mb_height = (avctx->
height + 15) >> 4;
 
  677         unsafe_mb_height_limit = mb_height;
 
  680             picture_height = (avctx->
height + 1) / 2;
 
  682             picture_height = avctx->
height / 2;
 
  684         mb_height = (picture_height + 15) >> 4;
 
  685         unsafe_mb_height_limit = mb_height;
 
  689         slice_per_line += rem >> 
i;
 
  694     hdr_size = 8; sl_data_size = buf_size - hdr_size;
 
  695     sl_data_sizes = buf + hdr_size;
 
  696     sl_data = sl_data_sizes + (slice_per_line * mb_height * 2);
 
  697     for (mb_y = 0; mb_y < mb_height; mb_y++) {
 
  700         while (mb_x < mb_width) {
 
  701             while (mb_width - mb_x < slice_mb_count)
 
  702                 slice_mb_count >>= 1;
 
  704             unsafe_bot = (avctx->
height & 0xf) && (mb_y == unsafe_mb_height_limit - 1);
 
  705             unsafe_right = (avctx->
width & 0xf) && (mb_x + slice_mb_count == mb_width);
 
  707             sl_size = 
encode_slice(avctx, pic, mb_x, mb_y, slice_mb_count,
 
  708                     sl_data, sl_data_size, unsafe_bot || unsafe_right, &qp, 
ctx->is_interlaced, is_top_field);
 
  713             bytestream_put_be16(&sl_data_sizes, sl_size);
 
  715             sl_data_size      -= sl_size;
 
  716             mb_x              += slice_mb_count;
 
  720     buf[0] = hdr_size << 3;
 
  721     AV_WB32(buf + 1, sl_data - buf);
 
  722     AV_WB16(buf + 5, slice_per_line * mb_height); 
 
  725     return sl_data - buf;
 
  729                                const AVFrame *pict, 
int *got_packet)
 
  732     int header_size = 148;
 
  734     int compress_frame_size, pic_size, 
ret, is_top_field_first = 0;
 
  743     compress_frame_size = 8 + header_size;
 
  745     bytestream_put_be32(&buf, compress_frame_size);
 
  746     bytestream_put_be32(&buf, 
FRAME_ID);
 
  748     bytestream_put_be16(&buf, header_size);
 
  751     bytestream_put_be16(&buf, avctx->
width);
 
  752     bytestream_put_be16(&buf, avctx->
height);
 
  756     if (
ctx->is_interlaced) {
 
  761             is_top_field_first = 1;
 
  769     *buf++ = frame_flags;
 
  778     *buf++ = 
ctx->need_alpha ? 0x2  : 0;
 
  786                                      pkt->
size - compress_frame_size, 0, is_top_field_first);
 
  790     compress_frame_size += pic_size;
 
  792     if (
ctx->is_interlaced) { 
 
  794                                          pkt->
size - compress_frame_size, 1, !is_top_field_first);
 
  798         compress_frame_size += pic_size;
 
  802     pkt->
size = compress_frame_size;
 
  811     for (
i = 0; 
i < 64; 
i++)
 
  823     if (
ctx->is_interlaced) {
 
  829     if (avctx->
width & 0x1) {
 
  831                 "frame width needs to be multiple of 2\n");
 
  835     if (avctx->
width > 65534 || avctx->
height > 65535) {
 
  837                 "The maximum dimensions are 65534x65535\n");
 
  841     if (strlen(
ctx->vendor) != 4) {
 
  850                 "encoding with ProRes standard (apcn) profile\n");
 
  854                    "encoding with ProRes 4444 (ap4h) profile\n");
 
  858                    "encoding with ProRes 4444+ (ap4h) profile\n");
 
  866                 "unknown profile %d, use [0 - apco, 1 - apcs, 2 - apcn (default), 3 - apch, 4 - ap4h, 5 - ap4x]\n",
 
  871                "encoding with ProRes 444/Xq (ap4h/ap4x) profile, need YUV444P10 input\n");
 
  876                "encoding with ProRes Proxy/LT/422/422 HQ (apco, apcs, apcn, ap4h) profile, need YUV422P10 input\n");
 
  913     for (
i = 1; 
i <= 16; 
i++) {
 
  930 #define OFFSET(x) offsetof(ProresContext, x) 
  931 #define VE     AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM 
  951     .
p.
name         = 
"prores_aw",