42 #define DEFAULT_SLICE_MB_WIDTH 8 
   56 static const int bitrate_table[]  = { 1000, 2100, 3500, 5400, 7000, 10000};
 
   67          4,  7,  9, 11, 13, 14, 15, 63,
 
   68          7,  7, 11, 12, 14, 15, 63, 63,
 
   69          9, 11, 13, 14, 15, 63, 63, 63,
 
   70         11, 11, 13, 14, 63, 63, 63, 63,
 
   71         11, 13, 14, 63, 63, 63, 63, 63,
 
   72         13, 14, 63, 63, 63, 63, 63, 63,
 
   73         13, 63, 63, 63, 63, 63, 63, 63,
 
   74         63, 63, 63, 63, 63, 63, 63, 63
 
   76          4,  5,  6,  7,  9, 11, 13, 15,
 
   77          5,  5,  7,  8, 11, 13, 15, 17,
 
   78          6,  7,  9, 11, 13, 15, 15, 17,
 
   79          7,  7,  9, 11, 13, 15, 17, 19,
 
   80          7,  9, 11, 13, 14, 16, 19, 23,
 
   81          9, 11, 13, 14, 16, 19, 23, 29,
 
   82          9, 11, 13, 15, 17, 21, 28, 35,
 
   83         11, 13, 16, 17, 21, 28, 35, 41
 
   85          4,  4,  5,  5,  6,  7,  7,  9,
 
   86          4,  4,  5,  6,  7,  7,  9,  9,
 
   87          5,  5,  6,  7,  7,  9,  9, 10,
 
   88          5,  5,  6,  7,  7,  9,  9, 10,
 
   89          5,  6,  7,  7,  8,  9, 10, 12,
 
   90          6,  7,  7,  8,  9, 10, 12, 15,
 
   91          6,  7,  7,  9, 10, 11, 14, 17,
 
   92          7,  7,  9, 10, 11, 14, 17, 21
 
   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,  4,
 
   97          4,  4,  4,  4,  4,  4,  4,  5,
 
   98          4,  4,  4,  4,  4,  4,  5,  5,
 
   99          4,  4,  4,  4,  4,  5,  5,  6,
 
  100          4,  4,  4,  4,  5,  5,  6,  7,
 
  101          4,  4,  4,  4,  5,  6,  7,  7
 
  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,
 
  106         4,  4,  4,  4,  4,  4,  4,  5,
 
  107         4,  4,  4,  4,  4,  4,  5,  5,
 
  108         4,  4,  4,  4,  4,  5,  5,  6,
 
  109         4,  4,  4,  4,  5,  5,  6,  7,
 
  110         4,  4,  4,  4,  5,  6,  7,  7
 
  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,  2,
 
  115         2,  2,  2,  2,  2,  2,  2,  3,
 
  116         2,  2,  2,  2,  2,  2,  3,  3,
 
  117         2,  2,  2,  2,  2,  3,  3,  3,
 
  118         2,  2,  2,  2,  3,  3,  3,  4,
 
  119         2,  2,  2,  2,  3,  3,  4,  4,
 
  125          4,  7,  9, 11, 13, 14, 63, 63,
 
  126          7,  7, 11, 12, 14, 63, 63, 63,
 
  127          9, 11, 13, 14, 63, 63, 63, 63,
 
  128         11, 11, 13, 14, 63, 63, 63, 63,
 
  129         11, 13, 14, 63, 63, 63, 63, 63,
 
  130         13, 14, 63, 63, 63, 63, 63, 63,
 
  131         13, 63, 63, 63, 63, 63, 63, 63,
 
  132         63, 63, 63, 63, 63, 63, 63, 63
 
  134          4,  5,  6,  7,  9, 11, 13, 15,
 
  135          5,  5,  7,  8, 11, 13, 15, 17,
 
  136          6,  7,  9, 11, 13, 15, 15, 17,
 
  137          7,  7,  9, 11, 13, 15, 17, 19,
 
  138          7,  9, 11, 13, 14, 16, 19, 23,
 
  139          9, 11, 13, 14, 16, 19, 23, 29,
 
  140          9, 11, 13, 15, 17, 21, 28, 35,
 
  141         11, 13, 16, 17, 21, 28, 35, 41
 
  143          4,  4,  5,  5,  6,  7,  7,  9,
 
  144          4,  4,  5,  6,  7,  7,  9,  9,
 
  145          5,  5,  6,  7,  7,  9,  9, 10,
 
  146          5,  5,  6,  7,  7,  9,  9, 10,
 
  147          5,  6,  7,  7,  8,  9, 10, 12,
 
  148          6,  7,  7,  8,  9, 10, 12, 15,
 
  149          6,  7,  7,  9, 10, 11, 14, 17,
 
  150          7,  7,  9, 10, 11, 14, 17, 21
 
  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,  4,
 
  155          4,  4,  4,  4,  4,  4,  4,  5,
 
  156          4,  4,  4,  4,  4,  4,  5,  5,
 
  157          4,  4,  4,  4,  4,  5,  5,  6,
 
  158          4,  4,  4,  4,  5,  5,  6,  7,
 
  159          4,  4,  4,  4,  5,  6,  7,  7
 
  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,  4,
 
  164         4,  4,  4,  4,  4,  4,  4,  5,
 
  165         4,  4,  4,  4,  4,  4,  5,  5,
 
  166         4,  4,  4,  4,  4,  5,  5,  6,
 
  167         4,  4,  4,  4,  5,  5,  6,  7,
 
  168         4,  4,  4,  4,  5,  6,  7,  7
 
  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,  4,
 
  173         4,  4,  4,  4,  4,  4,  4,  5,
 
  174         4,  4,  4,  4,  4,  4,  5,  5,
 
  175         4,  4,  4,  4,  4,  5,  5,  6,
 
  176         4,  4,  4,  4,  5,  5,  6,  7,
 
  177         4,  4,  4,  4,  5,  6,  7,  7
 
  190     int qmat_luma[16][64];
 
  191     int qmat_chroma[16][64];
 
  203     unsigned int rice_order, exp_order, switch_bits, first_exp, 
exp, zeros;
 
  210     first_exp = ((switch_bits + 1) << rice_order);
 
  212     if (
val >= first_exp) { 
 
  214         val += (1 << exp_order);
 
  216         zeros = 
exp - exp_order + switch_bits + 1;
 
  219     } 
else if (rice_order) {
 
  229 #define QSCALE(qmat,ind,val) ((val) / ((qmat)[ind])) 
  230 #define TO_GOLOMB(val) (((val) * 2) ^ ((val) >> 31)) 
  231 #define DIFF_SIGN(val, sign) (((val) >> 31) ^ (sign)) 
  232 #define IS_NEGATIVE(val) ((((val) >> 31) ^ -1) + 1) 
  233 #define TO_GOLOMB2(val,sign) ((val)==0 ? 0 : ((val) << 1) + (sign)) 
  237     int sign = (
val >> 31);
 
  238     return (
val ^ sign) - sign;
 
  241 #define FIRST_DC_CB 0xB8 
  243 static const uint8_t 
dc_codebook[7] = { 0x04, 0x28, 0x28, 0x4D, 0x4D, 0x70, 0x70};
 
  246         int blocks_per_slice, 
int *qmat)
 
  250     int new_dc, 
delta, diff_sign, new_code;
 
  252     prev_dc = 
QSCALE(qmat, 0, in[0] - 16384);
 
  256     code = 5; sign = 0; idx = 64;
 
  257     for (
i = 1; 
i < blocks_per_slice; 
i++, idx += 64) {
 
  258         new_dc    = 
QSCALE(qmat, 0, in[idx] - 16384);
 
  259         delta     = new_dc - prev_dc;
 
  271 static const uint8_t 
run_to_cb[16] = { 0x06, 0x06, 0x05, 0x05, 0x04, 0x29,
 
  272         0x29, 0x29, 0x29, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x4C };
 
  273 static const uint8_t 
lev_to_cb[10] = { 0x04, 0x0A, 0x05, 0x06, 0x04, 0x28,
 
  274         0x28, 0x28, 0x28, 0x4C };
 
  277         int16_t *in, 
int blocks_per_slice, 
int *qmat, 
const uint8_t ff_prores_scan[64])
 
  283     for (
i = 1; 
i < 64; 
i++) {
 
  284         int indp = ff_prores_scan[
i];
 
  285         for (j = 0; j < blocks_per_slice; j++) {
 
  286             int val = 
QSCALE(qmat, indp, in[(j << 6) + indp]);
 
  311     for (
i = 0; 
i < 8; 
i++) {
 
  333         for (
i = 0; 
i < mb_count; 
i++) {
 
  342     } 
else if (
chroma && is_422){ 
 
  343         for (
i = 0; 
i < mb_count; 
i++) {
 
  350         for (
i = 0; 
i < mb_count; 
i++) {
 
  362 static int encode_slice_plane(int16_t *blocks, 
int mb_count, uint8_t *buf, 
unsigned buf_size, 
int *qmat, 
int sub_sample_chroma,
 
  363                               const uint8_t ff_prores_scan[64])
 
  365     int blocks_per_slice;
 
  368     blocks_per_slice = mb_count << (2 - sub_sample_chroma);
 
  379                                                    int16_t * blocks_y, int16_t * blocks_u, int16_t * blocks_v,
 
  380                                                    unsigned mb_count, uint8_t *buf, 
unsigned data_size,
 
  381                                                    unsigned* y_data_size, 
unsigned* u_data_size, 
unsigned* v_data_size,
 
  387                                       buf, data_size, 
ctx->qmat_luma[qp - 1], 0, 
ctx->scantable);
 
  390         *u_data_size = 
encode_slice_plane(blocks_u, mb_count, buf + *y_data_size, data_size - *y_data_size,
 
  391                                           ctx->qmat_chroma[qp - 1], 
ctx->is_422, 
ctx->scantable);
 
  393         *v_data_size = 
encode_slice_plane(blocks_v, mb_count, buf + *y_data_size + *u_data_size,
 
  394                                           data_size - *y_data_size - *u_data_size,
 
  395                                           ctx->qmat_chroma[qp - 1], 
ctx->is_422, 
ctx->scantable);
 
  398     return *y_data_size + *u_data_size + *v_data_size;
 
  403     const int abits = 16;
 
  405     const int dsize = 1 << dbits - 1;
 
  406     int diff = cur - prev;
 
  409     if (
diff >= (1 << abits) - dsize)
 
  411     if (diff < -dsize || diff > dsize || !
diff) {
 
  435                                                    unsigned mb_count, uint8_t *buf, 
unsigned data_size, 
unsigned* a_data_size)
 
  437     const int abits = 16;
 
  438     const int mask  = (1 << abits) - 1;
 
  439     const int num_coeffs = mb_count * 256;
 
  440     int prev = 
mask, cur;
 
  443     int16_t * blocks = (int16_t *)src_a;
 
  460     } 
while (idx < num_coeffs);
 
  468                "Underestimated required buffer size.\n");
 
  477                                                unsigned dst_width, 
unsigned dst_height, 
int is_alpha_plane,
 
  478                                                int is_interlaced, 
int is_top_field)
 
  481     int i, j, src_stride, box_height;
 
  482     uint16_t last_pix, *last_line;
 
  484     if (!is_interlaced) {
 
  486         src += y * src_stride + x;
 
  490         src += y * src_stride + x;
 
  496     for (
i = 0; 
i < box_height; ++
i) {
 
  497         for (j = 0; j < box_width; ++j) {
 
  498             if (!is_alpha_plane) {
 
  501                 dst[j] = 
src[j] << 6; 
 
  504         if (!is_alpha_plane) {
 
  505             last_pix = dst[j - 1];
 
  507             last_pix = dst[j - 1] << 6; 
 
  509         for (; j < dst_width; j++)
 
  514     last_line = dst - dst_width;
 
  515     for (; 
i < dst_height; 
i++) {
 
  516         for (j = 0; j < dst_width; ++j) {
 
  517             dst[j] = last_line[j];
 
  525         unsigned dst_width, 
unsigned dst_height, 
int is_interlaced, 
int is_top_field)
 
  527     subimage_with_fill_template(
src, x, y, 
stride, 
width, 
height, dst, dst_width, dst_height, 0, is_interlaced, is_top_field);
 
  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, 1, is_interlaced, is_top_field);
 
  539         int mb_y, 
unsigned mb_count, uint8_t *buf, 
unsigned data_size,
 
  540         int unsafe, 
int *qp, 
int is_interlaced, 
int is_top_field)
 
  542     int luma_stride, chroma_stride, alpha_stride = 0;
 
  544     int hdr_size = 6 + (
ctx->need_alpha * 2); 
 
  545     int ret = 0, slice_size;
 
  546     const uint8_t *dest_y, *dest_u, *dest_v;
 
  547     unsigned y_data_size = 0, u_data_size = 0, v_data_size = 0, a_data_size = 0;
 
  550     int low_bytes  = (tgt_bits - (tgt_bits >> 3)) >> 3; 
 
  551     int high_bytes = (tgt_bits + (tgt_bits >> 3)) >> 3;
 
  563     if (!is_interlaced) {
 
  564         dest_y = pic->
data[0] + (mb_y << 4) * luma_stride   + (mb_x << 5);
 
  565         dest_u = pic->
data[1] + (mb_y << 4) * chroma_stride + (mb_x << (5 - 
ctx->is_422));
 
  566         dest_v = pic->
data[2] + (mb_y << 4) * chroma_stride + (mb_x << (5 - 
ctx->is_422));
 
  568         dest_y = pic->
data[0] + (mb_y << 4) * luma_stride * 2   + (mb_x << 5);
 
  569         dest_u = pic->
data[1] + (mb_y << 4) * chroma_stride * 2 + (mb_x << (5 - 
ctx->is_422));
 
  570         dest_v = pic->
data[2] + (mb_y << 4) * chroma_stride * 2 + (mb_x << (5 - 
ctx->is_422));
 
  572             dest_y += luma_stride;
 
  573             dest_u += chroma_stride;
 
  574             dest_v += chroma_stride;
 
  581                 (uint16_t *) 
ctx->fill_y, mb_count << 4, 16, is_interlaced, is_top_field);
 
  584                            (uint16_t *) 
ctx->fill_u, mb_count << (4 - 
ctx->is_422), 16, is_interlaced, is_top_field);
 
  587                            (uint16_t *) 
ctx->fill_v, mb_count << (4 - 
ctx->is_422), 16, is_interlaced, is_top_field);
 
  595                           mb_count, buf + hdr_size, data_size - hdr_size,
 
  596                           &y_data_size, &u_data_size, &v_data_size,
 
  599         if (!is_interlaced) {
 
  600             calc_plane_dct(fdsp, dest_y, blocks_y, luma_stride, mb_count, 0, 0);
 
  604             calc_plane_dct(fdsp, dest_y, blocks_y, luma_stride   * 2, mb_count, 0, 0);
 
  605             calc_plane_dct(fdsp, dest_u, blocks_u, chroma_stride * 2, mb_count, 1, 
ctx->is_422);
 
  606             calc_plane_dct(fdsp, dest_v, blocks_v, chroma_stride * 2, mb_count, 1, 
ctx->is_422);
 
  610                           mb_count, buf + hdr_size, data_size - hdr_size,
 
  611                           &y_data_size, &u_data_size, &v_data_size,
 
  618                                                mb_count, buf + hdr_size, data_size - hdr_size,
 
  619                                                &y_data_size, &u_data_size, &v_data_size,
 
  622         } 
else if (slice_size < low_bytes && *qp
 
  627                                                mb_count, buf + hdr_size, data_size - hdr_size,
 
  628                                                &y_data_size, &u_data_size, &v_data_size,
 
  634     buf[0] = hdr_size << 3;
 
  639     if (
ctx->need_alpha) {
 
  644                            (uint16_t *) 
ctx->fill_a, mb_count << 4, 16, is_interlaced, is_top_field);
 
  646                                       buf + hdr_size + slice_size,
 
  647                                       data_size - hdr_size - slice_size, &a_data_size);
 
  653     return hdr_size + y_data_size + u_data_size + v_data_size + a_data_size;
 
  657         uint8_t *buf, 
const int buf_size, 
const int picture_index, 
const int is_top_field)
 
  660     int mb_width = (avctx->
width + 15) >> 4;
 
  661     int hdr_size, sl_size, 
i;
 
  662     int mb_y, sl_data_size, qp, mb_height, picture_height, unsafe_mb_height_limit;
 
  663     int unsafe_bot, unsafe_right;
 
  664     uint8_t *sl_data, *sl_data_sizes;
 
  665     int slice_per_line = 0, rem = mb_width;
 
  667     if (!
ctx->is_interlaced) { 
 
  668         mb_height = (avctx->
height + 15) >> 4;
 
  669         unsafe_mb_height_limit = mb_height;
 
  672             picture_height = (avctx->
height + 1) / 2;
 
  674             picture_height = avctx->
height / 2;
 
  676         mb_height = (picture_height + 15) >> 4;
 
  677         unsafe_mb_height_limit = mb_height;
 
  681         slice_per_line += rem >> 
i;
 
  686     hdr_size = 8; sl_data_size = buf_size - hdr_size;
 
  687     sl_data_sizes = buf + hdr_size;
 
  688     sl_data = sl_data_sizes + (slice_per_line * mb_height * 2);
 
  689     for (mb_y = 0; mb_y < mb_height; mb_y++) {
 
  692         while (mb_x < mb_width) {
 
  693             while (mb_width - mb_x < slice_mb_count)
 
  694                 slice_mb_count >>= 1;
 
  696             unsafe_bot = (avctx->
height & 0xf) && (mb_y == unsafe_mb_height_limit - 1);
 
  697             unsafe_right = (avctx->
width & 0xf) && (mb_x + slice_mb_count == mb_width);
 
  699             sl_size = 
encode_slice(avctx, pic, mb_x, mb_y, slice_mb_count,
 
  700                     sl_data, sl_data_size, unsafe_bot || unsafe_right, &qp, 
ctx->is_interlaced, is_top_field);
 
  705             bytestream_put_be16(&sl_data_sizes, sl_size);
 
  707             sl_data_size      -= sl_size;
 
  708             mb_x              += slice_mb_count;
 
  712     buf[0] = hdr_size << 3;
 
  713     AV_WB32(buf + 1, sl_data - buf);
 
  714     AV_WB16(buf + 5, slice_per_line * mb_height); 
 
  717     return sl_data - buf;
 
  721                                const AVFrame *pict, 
int *got_packet)
 
  724     int header_size = 148;
 
  726     int compress_frame_size, pic_size, 
ret, is_top_field_first = 0;
 
  735     compress_frame_size = 8 + header_size;
 
  737     bytestream_put_be32(&buf, compress_frame_size);
 
  740     bytestream_put_be16(&buf, header_size);
 
  741     bytestream_put_be16(&buf, 0); 
 
  743     bytestream_put_be16(&buf, avctx->
width);
 
  744     bytestream_put_be16(&buf, avctx->
height);
 
  748     if (
ctx->is_interlaced) {
 
  752             is_top_field_first = 1;
 
  760     *buf++ = frame_flags;
 
  782                                      pkt->
size - compress_frame_size, 0, is_top_field_first);
 
  786     compress_frame_size += pic_size;
 
  788     if (
ctx->is_interlaced) { 
 
  790                                          pkt->
size - compress_frame_size, 1, !is_top_field_first);
 
  794         compress_frame_size += pic_size;
 
  798     pkt->
size = compress_frame_size;
 
  807     for (
i = 0; 
i < 64; 
i++)
 
  819     if (
ctx->is_interlaced) {
 
  825     if (avctx->
width & 0x1) {
 
  827                 "frame width needs to be multiple of 2\n");
 
  831     if (avctx->
width > 65534 || avctx->
height > 65535) {
 
  833                 "The maximum dimensions are 65534x65535\n");
 
  837     if (strlen(
ctx->vendor) != 4) {
 
  846                 "encoding with ProRes standard (apcn) profile\n");
 
  850                    "encoding with ProRes 4444 (ap4h) profile\n");
 
  854                    "encoding with ProRes 4444+ (ap4h) profile\n");
 
  861                 "unknown profile %d, use [0 - apco, 1 - apcs, 2 - apcn (default), 3 - apch, 4 - ap4h, 5 - ap4x]\n",
 
  866                "encoding with ProRes 444/Xq (ap4h/ap4x) profile, need YUV444P10 input\n");
 
  871                "encoding with ProRes Proxy/LT/422/422 HQ (apco, apcs, apcn, ap4h) profile, need YUV422P10 input\n");
 
  905     for (
i = 1; 
i <= 16; 
i++) {
 
  922 #define OFFSET(x) offsetof(ProresContext, x) 
  923 #define VE     AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM 
  943     .
p.
name         = 
"prores_aw",