00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "libavutil/opt.h"
00027 #include "avcodec.h"
00028 #include "put_bits.h"
00029 #include "bytestream.h"
00030 #include "internal.h"
00031 #include "proresdsp.h"
00032 #include "proresdata.h"
00033
00034 #define CFACTOR_Y422 2
00035 #define CFACTOR_Y444 3
00036
00037 #define MAX_MBS_PER_SLICE 8
00038
00039 #define MAX_PLANES 3 // should be increased to 4 when there's PIX_FMT_YUV444AP10
00040
00041 enum {
00042 PRORES_PROFILE_PROXY = 0,
00043 PRORES_PROFILE_LT,
00044 PRORES_PROFILE_STANDARD,
00045 PRORES_PROFILE_HQ,
00046 };
00047
00048 enum {
00049 QUANT_MAT_PROXY = 0,
00050 QUANT_MAT_LT,
00051 QUANT_MAT_STANDARD,
00052 QUANT_MAT_HQ,
00053 QUANT_MAT_DEFAULT,
00054 };
00055
00056 static const uint8_t prores_quant_matrices[][64] = {
00057 {
00058 4, 7, 9, 11, 13, 14, 15, 63,
00059 7, 7, 11, 12, 14, 15, 63, 63,
00060 9, 11, 13, 14, 15, 63, 63, 63,
00061 11, 11, 13, 14, 63, 63, 63, 63,
00062 11, 13, 14, 63, 63, 63, 63, 63,
00063 13, 14, 63, 63, 63, 63, 63, 63,
00064 13, 63, 63, 63, 63, 63, 63, 63,
00065 63, 63, 63, 63, 63, 63, 63, 63,
00066 },
00067 {
00068 4, 5, 6, 7, 9, 11, 13, 15,
00069 5, 5, 7, 8, 11, 13, 15, 17,
00070 6, 7, 9, 11, 13, 15, 15, 17,
00071 7, 7, 9, 11, 13, 15, 17, 19,
00072 7, 9, 11, 13, 14, 16, 19, 23,
00073 9, 11, 13, 14, 16, 19, 23, 29,
00074 9, 11, 13, 15, 17, 21, 28, 35,
00075 11, 13, 16, 17, 21, 28, 35, 41,
00076 },
00077 {
00078 4, 4, 5, 5, 6, 7, 7, 9,
00079 4, 4, 5, 6, 7, 7, 9, 9,
00080 5, 5, 6, 7, 7, 9, 9, 10,
00081 5, 5, 6, 7, 7, 9, 9, 10,
00082 5, 6, 7, 7, 8, 9, 10, 12,
00083 6, 7, 7, 8, 9, 10, 12, 15,
00084 6, 7, 7, 9, 10, 11, 14, 17,
00085 7, 7, 9, 10, 11, 14, 17, 21,
00086 },
00087 {
00088 4, 4, 4, 4, 4, 4, 4, 4,
00089 4, 4, 4, 4, 4, 4, 4, 4,
00090 4, 4, 4, 4, 4, 4, 4, 4,
00091 4, 4, 4, 4, 4, 4, 4, 5,
00092 4, 4, 4, 4, 4, 4, 5, 5,
00093 4, 4, 4, 4, 4, 5, 5, 6,
00094 4, 4, 4, 4, 5, 5, 6, 7,
00095 4, 4, 4, 4, 5, 6, 7, 7,
00096 },
00097 {
00098 4, 4, 4, 4, 4, 4, 4, 4,
00099 4, 4, 4, 4, 4, 4, 4, 4,
00100 4, 4, 4, 4, 4, 4, 4, 4,
00101 4, 4, 4, 4, 4, 4, 4, 4,
00102 4, 4, 4, 4, 4, 4, 4, 4,
00103 4, 4, 4, 4, 4, 4, 4, 4,
00104 4, 4, 4, 4, 4, 4, 4, 4,
00105 4, 4, 4, 4, 4, 4, 4, 4,
00106 },
00107 };
00108
00109 #define NUM_MB_LIMITS 4
00110 static const int prores_mb_limits[NUM_MB_LIMITS] = {
00111 1620,
00112 2700,
00113 6075,
00114 9216,
00115 };
00116
00117 static const struct prores_profile {
00118 const char *full_name;
00119 uint32_t tag;
00120 int min_quant;
00121 int max_quant;
00122 int br_tab[NUM_MB_LIMITS];
00123 int quant;
00124 } prores_profile_info[4] = {
00125 {
00126 .full_name = "proxy",
00127 .tag = MKTAG('a', 'p', 'c', 'o'),
00128 .min_quant = 4,
00129 .max_quant = 8,
00130 .br_tab = { 300, 242, 220, 194 },
00131 .quant = QUANT_MAT_PROXY,
00132 },
00133 {
00134 .full_name = "LT",
00135 .tag = MKTAG('a', 'p', 'c', 's'),
00136 .min_quant = 1,
00137 .max_quant = 9,
00138 .br_tab = { 720, 560, 490, 440 },
00139 .quant = QUANT_MAT_LT,
00140 },
00141 {
00142 .full_name = "standard",
00143 .tag = MKTAG('a', 'p', 'c', 'n'),
00144 .min_quant = 1,
00145 .max_quant = 6,
00146 .br_tab = { 1050, 808, 710, 632 },
00147 .quant = QUANT_MAT_STANDARD,
00148 },
00149 {
00150 .full_name = "high quality",
00151 .tag = MKTAG('a', 'p', 'c', 'h'),
00152 .min_quant = 1,
00153 .max_quant = 6,
00154 .br_tab = { 1566, 1216, 1070, 950 },
00155 .quant = QUANT_MAT_HQ,
00156 }
00157
00158 };
00159
00160 #define TRELLIS_WIDTH 16
00161 #define SCORE_LIMIT INT_MAX / 2
00162
00163 struct TrellisNode {
00164 int prev_node;
00165 int quant;
00166 int bits;
00167 int score;
00168 };
00169
00170 #define MAX_STORED_Q 16
00171
00172 typedef struct ProresThreadData {
00173 DECLARE_ALIGNED(16, DCTELEM, blocks)[MAX_PLANES][64 * 4 * MAX_MBS_PER_SLICE];
00174 DECLARE_ALIGNED(16, uint16_t, emu_buf)[16 * 16];
00175 int16_t custom_q[64];
00176 struct TrellisNode *nodes;
00177 } ProresThreadData;
00178
00179 typedef struct ProresContext {
00180 AVClass *class;
00181 DECLARE_ALIGNED(16, DCTELEM, blocks)[MAX_PLANES][64 * 4 * MAX_MBS_PER_SLICE];
00182 DECLARE_ALIGNED(16, uint16_t, emu_buf)[16*16];
00183 int16_t quants[MAX_STORED_Q][64];
00184 int16_t custom_q[64];
00185 const uint8_t *quant_mat;
00186
00187 ProresDSPContext dsp;
00188 ScanTable scantable;
00189
00190 int mb_width, mb_height;
00191 int mbs_per_slice;
00192 int num_chroma_blocks, chroma_factor;
00193 int slices_width;
00194 int slices_per_picture;
00195 int pictures_per_frame;
00196 int cur_picture_idx;
00197 int num_planes;
00198 int bits_per_mb;
00199 int force_quant;
00200
00201 char *vendor;
00202 int quant_sel;
00203
00204 int frame_size_upper_bound;
00205
00206 int profile;
00207 const struct prores_profile *profile_info;
00208
00209 int *slice_q;
00210
00211 ProresThreadData *tdata;
00212 } ProresContext;
00213
00214 static void get_slice_data(ProresContext *ctx, const uint16_t *src,
00215 int linesize, int x, int y, int w, int h,
00216 DCTELEM *blocks, uint16_t *emu_buf,
00217 int mbs_per_slice, int blocks_per_mb, int is_chroma)
00218 {
00219 const uint16_t *esrc;
00220 const int mb_width = 4 * blocks_per_mb;
00221 int elinesize;
00222 int i, j, k;
00223
00224 for (i = 0; i < mbs_per_slice; i++, src += mb_width) {
00225 if (x >= w) {
00226 memset(blocks, 0, 64 * (mbs_per_slice - i) * blocks_per_mb
00227 * sizeof(*blocks));
00228 return;
00229 }
00230 if (x + mb_width <= w && y + 16 <= h) {
00231 esrc = src;
00232 elinesize = linesize;
00233 } else {
00234 int bw, bh, pix;
00235
00236 esrc = emu_buf;
00237 elinesize = 16 * sizeof(*emu_buf);
00238
00239 bw = FFMIN(w - x, mb_width);
00240 bh = FFMIN(h - y, 16);
00241
00242 for (j = 0; j < bh; j++) {
00243 memcpy(emu_buf + j * 16,
00244 (const uint8_t*)src + j * linesize,
00245 bw * sizeof(*src));
00246 pix = emu_buf[j * 16 + bw - 1];
00247 for (k = bw; k < mb_width; k++)
00248 emu_buf[j * 16 + k] = pix;
00249 }
00250 for (; j < 16; j++)
00251 memcpy(emu_buf + j * 16,
00252 emu_buf + (bh - 1) * 16,
00253 mb_width * sizeof(*emu_buf));
00254 }
00255 if (!is_chroma) {
00256 ctx->dsp.fdct(esrc, elinesize, blocks);
00257 blocks += 64;
00258 if (blocks_per_mb > 2) {
00259 ctx->dsp.fdct(esrc + 8, elinesize, blocks);
00260 blocks += 64;
00261 }
00262 ctx->dsp.fdct(esrc + elinesize * 4, elinesize, blocks);
00263 blocks += 64;
00264 if (blocks_per_mb > 2) {
00265 ctx->dsp.fdct(esrc + elinesize * 4 + 8, elinesize, blocks);
00266 blocks += 64;
00267 }
00268 } else {
00269 ctx->dsp.fdct(esrc, elinesize, blocks);
00270 blocks += 64;
00271 ctx->dsp.fdct(esrc + elinesize * 4, elinesize, blocks);
00272 blocks += 64;
00273 if (blocks_per_mb > 2) {
00274 ctx->dsp.fdct(esrc + 8, elinesize, blocks);
00275 blocks += 64;
00276 ctx->dsp.fdct(esrc + elinesize * 4 + 8, elinesize, blocks);
00277 blocks += 64;
00278 }
00279 }
00280
00281 x += mb_width;
00282 }
00283 }
00284
00288 static inline void encode_vlc_codeword(PutBitContext *pb, unsigned codebook, int val)
00289 {
00290 unsigned int rice_order, exp_order, switch_bits, switch_val;
00291 int exponent;
00292
00293
00294 switch_bits = (codebook & 3) + 1;
00295 rice_order = codebook >> 5;
00296 exp_order = (codebook >> 2) & 7;
00297
00298 switch_val = switch_bits << rice_order;
00299
00300 if (val >= switch_val) {
00301 val -= switch_val - (1 << exp_order);
00302 exponent = av_log2(val);
00303
00304 put_bits(pb, exponent - exp_order + switch_bits, 0);
00305 put_bits(pb, exponent + 1, val);
00306 } else {
00307 exponent = val >> rice_order;
00308
00309 if (exponent)
00310 put_bits(pb, exponent, 0);
00311 put_bits(pb, 1, 1);
00312 if (rice_order)
00313 put_sbits(pb, rice_order, val);
00314 }
00315 }
00316
00317 #define GET_SIGN(x) ((x) >> 31)
00318 #define MAKE_CODE(x) (((x) << 1) ^ GET_SIGN(x))
00319
00320 static void encode_dcs(PutBitContext *pb, DCTELEM *blocks,
00321 int blocks_per_slice, int scale)
00322 {
00323 int i;
00324 int codebook = 3, code, dc, prev_dc, delta, sign, new_sign;
00325
00326 prev_dc = (blocks[0] - 0x4000) / scale;
00327 encode_vlc_codeword(pb, FIRST_DC_CB, MAKE_CODE(prev_dc));
00328 sign = 0;
00329 codebook = 3;
00330 blocks += 64;
00331
00332 for (i = 1; i < blocks_per_slice; i++, blocks += 64) {
00333 dc = (blocks[0] - 0x4000) / scale;
00334 delta = dc - prev_dc;
00335 new_sign = GET_SIGN(delta);
00336 delta = (delta ^ sign) - sign;
00337 code = MAKE_CODE(delta);
00338 encode_vlc_codeword(pb, ff_prores_dc_codebook[codebook], code);
00339 codebook = (code + (code & 1)) >> 1;
00340 codebook = FFMIN(codebook, 3);
00341 sign = new_sign;
00342 prev_dc = dc;
00343 }
00344 }
00345
00346 static void encode_acs(PutBitContext *pb, DCTELEM *blocks,
00347 int blocks_per_slice,
00348 int plane_size_factor,
00349 const uint8_t *scan, const int16_t *qmat)
00350 {
00351 int idx, i;
00352 int run, level, run_cb, lev_cb;
00353 int max_coeffs, abs_level;
00354
00355 max_coeffs = blocks_per_slice << 6;
00356 run_cb = ff_prores_run_to_cb_index[4];
00357 lev_cb = ff_prores_lev_to_cb_index[2];
00358 run = 0;
00359
00360 for (i = 1; i < 64; i++) {
00361 for (idx = scan[i]; idx < max_coeffs; idx += 64) {
00362 level = blocks[idx] / qmat[scan[i]];
00363 if (level) {
00364 abs_level = FFABS(level);
00365 encode_vlc_codeword(pb, ff_prores_ac_codebook[run_cb], run);
00366 encode_vlc_codeword(pb, ff_prores_ac_codebook[lev_cb],
00367 abs_level - 1);
00368 put_sbits(pb, 1, GET_SIGN(level));
00369
00370 run_cb = ff_prores_run_to_cb_index[FFMIN(run, 15)];
00371 lev_cb = ff_prores_lev_to_cb_index[FFMIN(abs_level, 9)];
00372 run = 0;
00373 } else {
00374 run++;
00375 }
00376 }
00377 }
00378 }
00379
00380 static int encode_slice_plane(ProresContext *ctx, PutBitContext *pb,
00381 const uint16_t *src, int linesize,
00382 int mbs_per_slice, DCTELEM *blocks,
00383 int blocks_per_mb, int plane_size_factor,
00384 const int16_t *qmat)
00385 {
00386 int blocks_per_slice, saved_pos;
00387
00388 saved_pos = put_bits_count(pb);
00389 blocks_per_slice = mbs_per_slice * blocks_per_mb;
00390
00391 encode_dcs(pb, blocks, blocks_per_slice, qmat[0]);
00392 encode_acs(pb, blocks, blocks_per_slice, plane_size_factor,
00393 ctx->scantable.permutated, qmat);
00394 flush_put_bits(pb);
00395
00396 return (put_bits_count(pb) - saved_pos) >> 3;
00397 }
00398
00399 static int encode_slice(AVCodecContext *avctx, const AVFrame *pic,
00400 PutBitContext *pb,
00401 int sizes[4], int x, int y, int quant,
00402 int mbs_per_slice)
00403 {
00404 ProresContext *ctx = avctx->priv_data;
00405 int i, xp, yp;
00406 int total_size = 0;
00407 const uint16_t *src;
00408 int slice_width_factor = av_log2(mbs_per_slice);
00409 int num_cblocks, pwidth, linesize, line_add;
00410 int plane_factor, is_chroma;
00411 uint16_t *qmat;
00412
00413 if (ctx->pictures_per_frame == 1)
00414 line_add = 0;
00415 else
00416 line_add = ctx->cur_picture_idx ^ !pic->top_field_first;
00417
00418 if (ctx->force_quant) {
00419 qmat = ctx->quants[0];
00420 } else if (quant < MAX_STORED_Q) {
00421 qmat = ctx->quants[quant];
00422 } else {
00423 qmat = ctx->custom_q;
00424 for (i = 0; i < 64; i++)
00425 qmat[i] = ctx->quant_mat[i] * quant;
00426 }
00427
00428 for (i = 0; i < ctx->num_planes; i++) {
00429 is_chroma = (i == 1 || i == 2);
00430 plane_factor = slice_width_factor + 2;
00431 if (is_chroma)
00432 plane_factor += ctx->chroma_factor - 3;
00433 if (!is_chroma || ctx->chroma_factor == CFACTOR_Y444) {
00434 xp = x << 4;
00435 yp = y << 4;
00436 num_cblocks = 4;
00437 pwidth = avctx->width;
00438 } else {
00439 xp = x << 3;
00440 yp = y << 4;
00441 num_cblocks = 2;
00442 pwidth = avctx->width >> 1;
00443 }
00444
00445 linesize = pic->linesize[i] * ctx->pictures_per_frame;
00446 src = (const uint16_t*)(pic->data[i] + yp * linesize +
00447 line_add * pic->linesize[i]) + xp;
00448
00449 get_slice_data(ctx, src, linesize, xp, yp,
00450 pwidth, avctx->height / ctx->pictures_per_frame,
00451 ctx->blocks[0], ctx->emu_buf,
00452 mbs_per_slice, num_cblocks, is_chroma);
00453 sizes[i] = encode_slice_plane(ctx, pb, src, linesize,
00454 mbs_per_slice, ctx->blocks[0],
00455 num_cblocks, plane_factor,
00456 qmat);
00457 total_size += sizes[i];
00458 }
00459 return total_size;
00460 }
00461
00462 static inline int estimate_vlc(unsigned codebook, int val)
00463 {
00464 unsigned int rice_order, exp_order, switch_bits, switch_val;
00465 int exponent;
00466
00467
00468 switch_bits = (codebook & 3) + 1;
00469 rice_order = codebook >> 5;
00470 exp_order = (codebook >> 2) & 7;
00471
00472 switch_val = switch_bits << rice_order;
00473
00474 if (val >= switch_val) {
00475 val -= switch_val - (1 << exp_order);
00476 exponent = av_log2(val);
00477
00478 return exponent * 2 - exp_order + switch_bits + 1;
00479 } else {
00480 return (val >> rice_order) + rice_order + 1;
00481 }
00482 }
00483
00484 static int estimate_dcs(int *error, DCTELEM *blocks, int blocks_per_slice,
00485 int scale)
00486 {
00487 int i;
00488 int codebook = 3, code, dc, prev_dc, delta, sign, new_sign;
00489 int bits;
00490
00491 prev_dc = (blocks[0] - 0x4000) / scale;
00492 bits = estimate_vlc(FIRST_DC_CB, MAKE_CODE(prev_dc));
00493 sign = 0;
00494 codebook = 3;
00495 blocks += 64;
00496 *error += FFABS(blocks[0] - 0x4000) % scale;
00497
00498 for (i = 1; i < blocks_per_slice; i++, blocks += 64) {
00499 dc = (blocks[0] - 0x4000) / scale;
00500 *error += FFABS(blocks[0] - 0x4000) % scale;
00501 delta = dc - prev_dc;
00502 new_sign = GET_SIGN(delta);
00503 delta = (delta ^ sign) - sign;
00504 code = MAKE_CODE(delta);
00505 bits += estimate_vlc(ff_prores_dc_codebook[codebook], code);
00506 codebook = (code + (code & 1)) >> 1;
00507 codebook = FFMIN(codebook, 3);
00508 sign = new_sign;
00509 prev_dc = dc;
00510 }
00511
00512 return bits;
00513 }
00514
00515 static int estimate_acs(int *error, DCTELEM *blocks, int blocks_per_slice,
00516 int plane_size_factor,
00517 const uint8_t *scan, const int16_t *qmat)
00518 {
00519 int idx, i;
00520 int run, level, run_cb, lev_cb;
00521 int max_coeffs, abs_level;
00522 int bits = 0;
00523
00524 max_coeffs = blocks_per_slice << 6;
00525 run_cb = ff_prores_run_to_cb_index[4];
00526 lev_cb = ff_prores_lev_to_cb_index[2];
00527 run = 0;
00528
00529 for (i = 1; i < 64; i++) {
00530 for (idx = scan[i]; idx < max_coeffs; idx += 64) {
00531 level = blocks[idx] / qmat[scan[i]];
00532 *error += FFABS(blocks[idx]) % qmat[scan[i]];
00533 if (level) {
00534 abs_level = FFABS(level);
00535 bits += estimate_vlc(ff_prores_ac_codebook[run_cb], run);
00536 bits += estimate_vlc(ff_prores_ac_codebook[lev_cb],
00537 abs_level - 1) + 1;
00538
00539 run_cb = ff_prores_run_to_cb_index[FFMIN(run, 15)];
00540 lev_cb = ff_prores_lev_to_cb_index[FFMIN(abs_level, 9)];
00541 run = 0;
00542 } else {
00543 run++;
00544 }
00545 }
00546 }
00547
00548 return bits;
00549 }
00550
00551 static int estimate_slice_plane(ProresContext *ctx, int *error, int plane,
00552 const uint16_t *src, int linesize,
00553 int mbs_per_slice,
00554 int blocks_per_mb, int plane_size_factor,
00555 const int16_t *qmat, ProresThreadData *td)
00556 {
00557 int blocks_per_slice;
00558 int bits;
00559
00560 blocks_per_slice = mbs_per_slice * blocks_per_mb;
00561
00562 bits = estimate_dcs(error, td->blocks[plane], blocks_per_slice, qmat[0]);
00563 bits += estimate_acs(error, td->blocks[plane], blocks_per_slice,
00564 plane_size_factor, ctx->scantable.permutated, qmat);
00565
00566 return FFALIGN(bits, 8);
00567 }
00568
00569 static int find_slice_quant(AVCodecContext *avctx, const AVFrame *pic,
00570 int trellis_node, int x, int y, int mbs_per_slice,
00571 ProresThreadData *td)
00572 {
00573 ProresContext *ctx = avctx->priv_data;
00574 int i, q, pq, xp, yp;
00575 const uint16_t *src;
00576 int slice_width_factor = av_log2(mbs_per_slice);
00577 int num_cblocks[MAX_PLANES], pwidth;
00578 int plane_factor[MAX_PLANES], is_chroma[MAX_PLANES];
00579 const int min_quant = ctx->profile_info->min_quant;
00580 const int max_quant = ctx->profile_info->max_quant;
00581 int error, bits, bits_limit;
00582 int mbs, prev, cur, new_score;
00583 int slice_bits[TRELLIS_WIDTH], slice_score[TRELLIS_WIDTH];
00584 int overquant;
00585 uint16_t *qmat;
00586 int linesize[4], line_add;
00587
00588 if (ctx->pictures_per_frame == 1)
00589 line_add = 0;
00590 else
00591 line_add = ctx->cur_picture_idx ^ !pic->top_field_first;
00592 mbs = x + mbs_per_slice;
00593
00594 for (i = 0; i < ctx->num_planes; i++) {
00595 is_chroma[i] = (i == 1 || i == 2);
00596 plane_factor[i] = slice_width_factor + 2;
00597 if (is_chroma[i])
00598 plane_factor[i] += ctx->chroma_factor - 3;
00599 if (!is_chroma[i] || ctx->chroma_factor == CFACTOR_Y444) {
00600 xp = x << 4;
00601 yp = y << 4;
00602 num_cblocks[i] = 4;
00603 pwidth = avctx->width;
00604 } else {
00605 xp = x << 3;
00606 yp = y << 4;
00607 num_cblocks[i] = 2;
00608 pwidth = avctx->width >> 1;
00609 }
00610
00611 linesize[i] = pic->linesize[i] * ctx->pictures_per_frame;
00612 src = (const uint16_t*)(pic->data[i] + yp * linesize[i] +
00613 line_add * pic->linesize[i]) + xp;
00614
00615 get_slice_data(ctx, src, linesize[i], xp, yp,
00616 pwidth, avctx->height / ctx->pictures_per_frame,
00617 td->blocks[i], td->emu_buf,
00618 mbs_per_slice, num_cblocks[i], is_chroma[i]);
00619 }
00620
00621 for (q = min_quant; q < max_quant + 2; q++) {
00622 td->nodes[trellis_node + q].prev_node = -1;
00623 td->nodes[trellis_node + q].quant = q;
00624 }
00625
00626
00627 for (q = min_quant; q <= max_quant; q++) {
00628 bits = 0;
00629 error = 0;
00630 for (i = 0; i < ctx->num_planes; i++) {
00631 bits += estimate_slice_plane(ctx, &error, i,
00632 src, linesize[i],
00633 mbs_per_slice,
00634 num_cblocks[i], plane_factor[i],
00635 ctx->quants[q], td);
00636 }
00637 if (bits > 65000 * 8) {
00638 error = SCORE_LIMIT;
00639 break;
00640 }
00641 slice_bits[q] = bits;
00642 slice_score[q] = error;
00643 }
00644 if (slice_bits[max_quant] <= ctx->bits_per_mb * mbs_per_slice) {
00645 slice_bits[max_quant + 1] = slice_bits[max_quant];
00646 slice_score[max_quant + 1] = slice_score[max_quant] + 1;
00647 overquant = max_quant;
00648 } else {
00649 for (q = max_quant + 1; q < 128; q++) {
00650 bits = 0;
00651 error = 0;
00652 if (q < MAX_STORED_Q) {
00653 qmat = ctx->quants[q];
00654 } else {
00655 qmat = td->custom_q;
00656 for (i = 0; i < 64; i++)
00657 qmat[i] = ctx->quant_mat[i] * q;
00658 }
00659 for (i = 0; i < ctx->num_planes; i++) {
00660 bits += estimate_slice_plane(ctx, &error, i,
00661 src, linesize[i],
00662 mbs_per_slice,
00663 num_cblocks[i], plane_factor[i],
00664 qmat, td);
00665 }
00666 if (bits <= ctx->bits_per_mb * mbs_per_slice)
00667 break;
00668 }
00669
00670 slice_bits[max_quant + 1] = bits;
00671 slice_score[max_quant + 1] = error;
00672 overquant = q;
00673 }
00674 td->nodes[trellis_node + max_quant + 1].quant = overquant;
00675
00676 bits_limit = mbs * ctx->bits_per_mb;
00677 for (pq = min_quant; pq < max_quant + 2; pq++) {
00678 prev = trellis_node - TRELLIS_WIDTH + pq;
00679
00680 for (q = min_quant; q < max_quant + 2; q++) {
00681 cur = trellis_node + q;
00682
00683 bits = td->nodes[prev].bits + slice_bits[q];
00684 error = slice_score[q];
00685 if (bits > bits_limit)
00686 error = SCORE_LIMIT;
00687
00688 if (td->nodes[prev].score < SCORE_LIMIT && error < SCORE_LIMIT)
00689 new_score = td->nodes[prev].score + error;
00690 else
00691 new_score = SCORE_LIMIT;
00692 if (td->nodes[cur].prev_node == -1 ||
00693 td->nodes[cur].score >= new_score) {
00694
00695 td->nodes[cur].bits = bits;
00696 td->nodes[cur].score = new_score;
00697 td->nodes[cur].prev_node = prev;
00698 }
00699 }
00700 }
00701
00702 error = td->nodes[trellis_node + min_quant].score;
00703 pq = trellis_node + min_quant;
00704 for (q = min_quant + 1; q < max_quant + 2; q++) {
00705 if (td->nodes[trellis_node + q].score <= error) {
00706 error = td->nodes[trellis_node + q].score;
00707 pq = trellis_node + q;
00708 }
00709 }
00710
00711 return pq;
00712 }
00713
00714 static int find_quant_thread(AVCodecContext *avctx, void *arg,
00715 int jobnr, int threadnr)
00716 {
00717 ProresContext *ctx = avctx->priv_data;
00718 ProresThreadData *td = ctx->tdata + threadnr;
00719 int mbs_per_slice = ctx->mbs_per_slice;
00720 int x, y = jobnr, mb, q = 0;
00721
00722 for (x = mb = 0; x < ctx->mb_width; x += mbs_per_slice, mb++) {
00723 while (ctx->mb_width - x < mbs_per_slice)
00724 mbs_per_slice >>= 1;
00725 q = find_slice_quant(avctx, avctx->coded_frame,
00726 (mb + 1) * TRELLIS_WIDTH, x, y,
00727 mbs_per_slice, td);
00728 }
00729
00730 for (x = ctx->slices_width - 1; x >= 0; x--) {
00731 ctx->slice_q[x + y * ctx->slices_width] = td->nodes[q].quant;
00732 q = td->nodes[q].prev_node;
00733 }
00734
00735 return 0;
00736 }
00737
00738 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
00739 const AVFrame *pic, int *got_packet)
00740 {
00741 ProresContext *ctx = avctx->priv_data;
00742 uint8_t *orig_buf, *buf, *slice_hdr, *slice_sizes, *tmp;
00743 uint8_t *picture_size_pos;
00744 PutBitContext pb;
00745 int x, y, i, mb, q = 0;
00746 int sizes[4] = { 0 };
00747 int slice_hdr_size = 2 + 2 * (ctx->num_planes - 1);
00748 int frame_size, picture_size, slice_size;
00749 int pkt_size, ret;
00750 uint8_t frame_flags;
00751
00752 *avctx->coded_frame = *pic;
00753 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
00754 avctx->coded_frame->key_frame = 1;
00755
00756 pkt_size = ctx->frame_size_upper_bound + FF_MIN_BUFFER_SIZE;
00757
00758 if ((ret = ff_alloc_packet2(avctx, pkt, pkt_size)) < 0)
00759 return ret;
00760
00761 orig_buf = pkt->data;
00762
00763
00764 orig_buf += 4;
00765 bytestream_put_be32 (&orig_buf, FRAME_ID);
00766 buf = orig_buf;
00767
00768
00769 tmp = buf;
00770 buf += 2;
00771 bytestream_put_be16 (&buf, 0);
00772 bytestream_put_buffer(&buf, ctx->vendor, 4);
00773 bytestream_put_be16 (&buf, avctx->width);
00774 bytestream_put_be16 (&buf, avctx->height);
00775
00776 frame_flags = ctx->chroma_factor << 6;
00777 if (avctx->flags & CODEC_FLAG_INTERLACED_DCT)
00778 frame_flags |= pic->top_field_first ? 0x04 : 0x08;
00779 bytestream_put_byte (&buf, frame_flags);
00780
00781 bytestream_put_byte (&buf, 0);
00782 bytestream_put_byte (&buf, avctx->color_primaries);
00783 bytestream_put_byte (&buf, avctx->color_trc);
00784 bytestream_put_byte (&buf, avctx->colorspace);
00785 bytestream_put_byte (&buf, 0x40);
00786 bytestream_put_byte (&buf, 0);
00787 if (ctx->quant_sel != QUANT_MAT_DEFAULT) {
00788 bytestream_put_byte (&buf, 0x03);
00789
00790 for (i = 0; i < 64; i++)
00791 bytestream_put_byte(&buf, ctx->quant_mat[i]);
00792
00793 for (i = 0; i < 64; i++)
00794 bytestream_put_byte(&buf, ctx->quant_mat[i]);
00795 } else {
00796 bytestream_put_byte (&buf, 0x00);
00797 }
00798 bytestream_put_be16 (&tmp, buf - orig_buf);
00799
00800 for (ctx->cur_picture_idx = 0;
00801 ctx->cur_picture_idx < ctx->pictures_per_frame;
00802 ctx->cur_picture_idx++) {
00803
00804 picture_size_pos = buf + 1;
00805 bytestream_put_byte (&buf, 0x40);
00806 buf += 4;
00807 bytestream_put_be16 (&buf, ctx->slices_per_picture);
00808 bytestream_put_byte (&buf, av_log2(ctx->mbs_per_slice) << 4);
00809
00810
00811 slice_sizes = buf;
00812 buf += ctx->slices_per_picture * 2;
00813
00814
00815 if (!ctx->force_quant) {
00816 ret = avctx->execute2(avctx, find_quant_thread, NULL, NULL,
00817 ctx->mb_height);
00818 if (ret)
00819 return ret;
00820 }
00821
00822 for (y = 0; y < ctx->mb_height; y++) {
00823 int mbs_per_slice = ctx->mbs_per_slice;
00824 for (x = mb = 0; x < ctx->mb_width; x += mbs_per_slice, mb++) {
00825 q = ctx->force_quant ? ctx->force_quant
00826 : ctx->slice_q[mb + y * ctx->slices_width];
00827
00828 while (ctx->mb_width - x < mbs_per_slice)
00829 mbs_per_slice >>= 1;
00830
00831 bytestream_put_byte(&buf, slice_hdr_size << 3);
00832 slice_hdr = buf;
00833 buf += slice_hdr_size - 1;
00834 init_put_bits(&pb, buf, (pkt_size - (buf - orig_buf)) * 8);
00835 encode_slice(avctx, pic, &pb, sizes, x, y, q, mbs_per_slice);
00836
00837 bytestream_put_byte(&slice_hdr, q);
00838 slice_size = slice_hdr_size + sizes[ctx->num_planes - 1];
00839 for (i = 0; i < ctx->num_planes - 1; i++) {
00840 bytestream_put_be16(&slice_hdr, sizes[i]);
00841 slice_size += sizes[i];
00842 }
00843 bytestream_put_be16(&slice_sizes, slice_size);
00844 buf += slice_size - slice_hdr_size;
00845 }
00846 }
00847
00848 picture_size = buf - (picture_size_pos - 1);
00849 bytestream_put_be32(&picture_size_pos, picture_size);
00850 }
00851
00852 orig_buf -= 8;
00853 frame_size = buf - orig_buf;
00854 bytestream_put_be32(&orig_buf, frame_size);
00855
00856 pkt->size = frame_size;
00857 pkt->flags |= AV_PKT_FLAG_KEY;
00858 *got_packet = 1;
00859
00860 return 0;
00861 }
00862
00863 static av_cold int encode_close(AVCodecContext *avctx)
00864 {
00865 ProresContext *ctx = avctx->priv_data;
00866 int i;
00867
00868 av_freep(&avctx->coded_frame);
00869
00870 if (ctx->tdata) {
00871 for (i = 0; i < avctx->thread_count; i++)
00872 av_free(ctx->tdata[i].nodes);
00873 }
00874 av_freep(&ctx->tdata);
00875 av_freep(&ctx->slice_q);
00876
00877 return 0;
00878 }
00879
00880 static av_cold int encode_init(AVCodecContext *avctx)
00881 {
00882 ProresContext *ctx = avctx->priv_data;
00883 int mps;
00884 int i, j;
00885 int min_quant, max_quant;
00886 int interlaced = !!(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
00887
00888 avctx->bits_per_raw_sample = 10;
00889 avctx->coded_frame = avcodec_alloc_frame();
00890 if (!avctx->coded_frame)
00891 return AVERROR(ENOMEM);
00892
00893 ff_proresdsp_init(&ctx->dsp, avctx);
00894 ff_init_scantable(ctx->dsp.dct_permutation, &ctx->scantable,
00895 interlaced ? ff_prores_interlaced_scan
00896 : ff_prores_progressive_scan);
00897
00898 mps = ctx->mbs_per_slice;
00899 if (mps & (mps - 1)) {
00900 av_log(avctx, AV_LOG_ERROR,
00901 "there should be an integer power of two MBs per slice\n");
00902 return AVERROR(EINVAL);
00903 }
00904
00905 ctx->chroma_factor = avctx->pix_fmt == PIX_FMT_YUV422P10
00906 ? CFACTOR_Y422
00907 : CFACTOR_Y444;
00908 ctx->profile_info = prores_profile_info + ctx->profile;
00909 ctx->num_planes = 3;
00910
00911 ctx->mb_width = FFALIGN(avctx->width, 16) >> 4;
00912
00913 if (interlaced)
00914 ctx->mb_height = FFALIGN(avctx->height, 32) >> 5;
00915 else
00916 ctx->mb_height = FFALIGN(avctx->height, 16) >> 4;
00917
00918 ctx->slices_width = ctx->mb_width / mps;
00919 ctx->slices_width += av_popcount(ctx->mb_width - ctx->slices_width * mps);
00920 ctx->slices_per_picture = ctx->mb_height * ctx->slices_width;
00921 ctx->pictures_per_frame = 1 + interlaced;
00922
00923 if (ctx->quant_sel == -1)
00924 ctx->quant_mat = prores_quant_matrices[ctx->profile_info->quant];
00925 else
00926 ctx->quant_mat = prores_quant_matrices[ctx->quant_sel];
00927
00928 if (strlen(ctx->vendor) != 4) {
00929 av_log(avctx, AV_LOG_ERROR, "vendor ID should be 4 bytes\n");
00930 return AVERROR_INVALIDDATA;
00931 }
00932
00933 ctx->force_quant = avctx->global_quality / FF_QP2LAMBDA;
00934 if (!ctx->force_quant) {
00935 if (!ctx->bits_per_mb) {
00936 for (i = 0; i < NUM_MB_LIMITS - 1; i++)
00937 if (prores_mb_limits[i] >= ctx->mb_width * ctx->mb_height *
00938 ctx->pictures_per_frame)
00939 break;
00940 ctx->bits_per_mb = ctx->profile_info->br_tab[i];
00941 } else if (ctx->bits_per_mb < 128) {
00942 av_log(avctx, AV_LOG_ERROR, "too few bits per MB, please set at least 128\n");
00943 return AVERROR_INVALIDDATA;
00944 }
00945
00946 min_quant = ctx->profile_info->min_quant;
00947 max_quant = ctx->profile_info->max_quant;
00948 for (i = min_quant; i < MAX_STORED_Q; i++) {
00949 for (j = 0; j < 64; j++)
00950 ctx->quants[i][j] = ctx->quant_mat[j] * i;
00951 }
00952
00953 ctx->slice_q = av_malloc(ctx->slices_per_picture * sizeof(*ctx->slice_q));
00954 if (!ctx->slice_q) {
00955 encode_close(avctx);
00956 return AVERROR(ENOMEM);
00957 }
00958
00959 ctx->tdata = av_mallocz(avctx->thread_count * sizeof(*ctx->tdata));
00960 if (!ctx->tdata) {
00961 encode_close(avctx);
00962 return AVERROR(ENOMEM);
00963 }
00964
00965 for (j = 0; j < avctx->thread_count; j++) {
00966 ctx->tdata[j].nodes = av_malloc((ctx->slices_width + 1)
00967 * TRELLIS_WIDTH
00968 * sizeof(*ctx->tdata->nodes));
00969 if (!ctx->tdata[j].nodes) {
00970 encode_close(avctx);
00971 return AVERROR(ENOMEM);
00972 }
00973 for (i = min_quant; i < max_quant + 2; i++) {
00974 ctx->tdata[j].nodes[i].prev_node = -1;
00975 ctx->tdata[j].nodes[i].bits = 0;
00976 ctx->tdata[j].nodes[i].score = 0;
00977 }
00978 }
00979 } else {
00980 int ls = 0;
00981
00982 if (ctx->force_quant > 64) {
00983 av_log(avctx, AV_LOG_ERROR, "too large quantiser, maximum is 64\n");
00984 return AVERROR_INVALIDDATA;
00985 }
00986
00987 for (j = 0; j < 64; j++) {
00988 ctx->quants[0][j] = ctx->quant_mat[j] * ctx->force_quant;
00989 ls += av_log2((1 << 11) / ctx->quants[0][j]) * 2 + 1;
00990 }
00991
00992 ctx->bits_per_mb = ls * 8;
00993 if (ctx->chroma_factor == CFACTOR_Y444)
00994 ctx->bits_per_mb += ls * 4;
00995 if (ctx->num_planes == 4)
00996 ctx->bits_per_mb += ls * 4;
00997 }
00998
00999 ctx->frame_size_upper_bound = ctx->pictures_per_frame *
01000 ctx->slices_per_picture *
01001 (2 + 2 * ctx->num_planes +
01002 (mps * ctx->bits_per_mb) / 8)
01003 + 200;
01004
01005 avctx->codec_tag = ctx->profile_info->tag;
01006
01007 av_log(avctx, AV_LOG_DEBUG,
01008 "profile %d, %d slices, interlacing: %s, %d bits per MB\n",
01009 ctx->profile, ctx->slices_per_picture * ctx->pictures_per_frame,
01010 interlaced ? "yes" : "no", ctx->bits_per_mb);
01011 av_log(avctx, AV_LOG_DEBUG, "frame size upper bound: %d\n",
01012 ctx->frame_size_upper_bound);
01013
01014 return 0;
01015 }
01016
01017 #define OFFSET(x) offsetof(ProresContext, x)
01018 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
01019
01020 static const AVOption options[] = {
01021 { "mbs_per_slice", "macroblocks per slice", OFFSET(mbs_per_slice),
01022 AV_OPT_TYPE_INT, { .i64 = 8 }, 1, MAX_MBS_PER_SLICE, VE },
01023 { "profile", NULL, OFFSET(profile), AV_OPT_TYPE_INT,
01024 { .i64 = PRORES_PROFILE_STANDARD },
01025 PRORES_PROFILE_PROXY, PRORES_PROFILE_HQ, VE, "profile" },
01026 { "proxy", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PRORES_PROFILE_PROXY },
01027 0, 0, VE, "profile" },
01028 { "lt", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PRORES_PROFILE_LT },
01029 0, 0, VE, "profile" },
01030 { "standard", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PRORES_PROFILE_STANDARD },
01031 0, 0, VE, "profile" },
01032 { "hq", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PRORES_PROFILE_HQ },
01033 0, 0, VE, "profile" },
01034 { "vendor", "vendor ID", OFFSET(vendor),
01035 AV_OPT_TYPE_STRING, { .str = "Lavc" }, CHAR_MIN, CHAR_MAX, VE },
01036 { "bits_per_mb", "desired bits per macroblock", OFFSET(bits_per_mb),
01037 AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 8192, VE },
01038 { "quant_mat", "quantiser matrix", OFFSET(quant_sel), AV_OPT_TYPE_INT,
01039 { .i64 = -1 }, -1, QUANT_MAT_DEFAULT, VE, "quant_mat" },
01040 { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = -1 },
01041 0, 0, VE, "quant_mat" },
01042 { "proxy", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = QUANT_MAT_PROXY },
01043 0, 0, VE, "quant_mat" },
01044 { "lt", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = QUANT_MAT_LT },
01045 0, 0, VE, "quant_mat" },
01046 { "standard", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = QUANT_MAT_STANDARD },
01047 0, 0, VE, "quant_mat" },
01048 { "hq", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = QUANT_MAT_HQ },
01049 0, 0, VE, "quant_mat" },
01050 { "default", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = QUANT_MAT_DEFAULT },
01051 0, 0, VE, "quant_mat" },
01052 { NULL }
01053 };
01054
01055 static const AVClass proresenc_class = {
01056 .class_name = "ProRes encoder",
01057 .item_name = av_default_item_name,
01058 .option = options,
01059 .version = LIBAVUTIL_VERSION_INT,
01060 };
01061
01062 AVCodec ff_prores_kostya_encoder = {
01063 .name = "prores_kostya",
01064 .type = AVMEDIA_TYPE_VIDEO,
01065 .id = AV_CODEC_ID_PRORES,
01066 .priv_data_size = sizeof(ProresContext),
01067 .init = encode_init,
01068 .close = encode_close,
01069 .encode2 = encode_frame,
01070 .capabilities = CODEC_CAP_SLICE_THREADS,
01071 .long_name = NULL_IF_CONFIG_SMALL("Apple ProRes (iCodec Pro)"),
01072 .pix_fmts = (const enum PixelFormat[]) {
01073 PIX_FMT_YUV422P10, PIX_FMT_YUV444P10, PIX_FMT_NONE
01074 },
01075 .priv_class = &proresenc_class,
01076 };