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
00027 #define RC_VARIANCE 1 // use variance or ssd for fast rc
00028
00029 #include "libavutil/opt.h"
00030 #include "avcodec.h"
00031 #include "dsputil.h"
00032 #include "internal.h"
00033 #include "mpegvideo.h"
00034 #include "dnxhdenc.h"
00035 #include "internal.h"
00036
00037 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
00038 #define DNX10BIT_QMAT_SHIFT 18 // The largest value that will not lead to overflow for 10bit samples.
00039
00040 static const AVOption options[]={
00041 {"nitris_compat", "encode with Avid Nitris compatibility", offsetof(DNXHDEncContext, nitris_compat), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, VE},
00042 {NULL}
00043 };
00044
00045 static const AVClass class = {
00046 .class_name = "dnxhd",
00047 .item_name = av_default_item_name,
00048 .option = options,
00049 .version = LIBAVUTIL_VERSION_INT,
00050 };
00051
00052 #define LAMBDA_FRAC_BITS 10
00053
00054 static void dnxhd_8bit_get_pixels_8x4_sym(DCTELEM *av_restrict block, const uint8_t *pixels, int line_size)
00055 {
00056 int i;
00057 for (i = 0; i < 4; i++) {
00058 block[0] = pixels[0]; block[1] = pixels[1];
00059 block[2] = pixels[2]; block[3] = pixels[3];
00060 block[4] = pixels[4]; block[5] = pixels[5];
00061 block[6] = pixels[6]; block[7] = pixels[7];
00062 pixels += line_size;
00063 block += 8;
00064 }
00065 memcpy(block, block - 8, sizeof(*block) * 8);
00066 memcpy(block + 8, block - 16, sizeof(*block) * 8);
00067 memcpy(block + 16, block - 24, sizeof(*block) * 8);
00068 memcpy(block + 24, block - 32, sizeof(*block) * 8);
00069 }
00070
00071 static av_always_inline void dnxhd_10bit_get_pixels_8x4_sym(DCTELEM *av_restrict block, const uint8_t *pixels, int line_size)
00072 {
00073 int i;
00074 const uint16_t* pixels16 = (const uint16_t*)pixels;
00075 line_size >>= 1;
00076
00077 for (i = 0; i < 4; i++) {
00078 block[0] = pixels16[0]; block[1] = pixels16[1];
00079 block[2] = pixels16[2]; block[3] = pixels16[3];
00080 block[4] = pixels16[4]; block[5] = pixels16[5];
00081 block[6] = pixels16[6]; block[7] = pixels16[7];
00082 pixels16 += line_size;
00083 block += 8;
00084 }
00085 memcpy(block, block - 8, sizeof(*block) * 8);
00086 memcpy(block + 8, block - 16, sizeof(*block) * 8);
00087 memcpy(block + 16, block - 24, sizeof(*block) * 8);
00088 memcpy(block + 24, block - 32, sizeof(*block) * 8);
00089 }
00090
00091 static int dnxhd_10bit_dct_quantize(MpegEncContext *ctx, DCTELEM *block,
00092 int n, int qscale, int *overflow)
00093 {
00094 const uint8_t *scantable= ctx->intra_scantable.scantable;
00095 const int *qmat = n<4 ? ctx->q_intra_matrix[qscale] : ctx->q_chroma_intra_matrix[qscale];
00096 int last_non_zero = 0;
00097 int i;
00098
00099 ctx->dsp.fdct(block);
00100
00101
00102 block[0] = (block[0] + 2) >> 2;
00103
00104 for (i = 1; i < 64; ++i) {
00105 int j = scantable[i];
00106 int sign = block[j] >> 31;
00107 int level = (block[j] ^ sign) - sign;
00108 level = level * qmat[j] >> DNX10BIT_QMAT_SHIFT;
00109 block[j] = (level ^ sign) - sign;
00110 if (level)
00111 last_non_zero = i;
00112 }
00113
00114 return last_non_zero;
00115 }
00116
00117 static int dnxhd_init_vlc(DNXHDEncContext *ctx)
00118 {
00119 int i, j, level, run;
00120 int max_level = 1<<(ctx->cid_table->bit_depth+2);
00121
00122 FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->vlc_codes, max_level*4*sizeof(*ctx->vlc_codes), fail);
00123 FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->vlc_bits, max_level*4*sizeof(*ctx->vlc_bits) , fail);
00124 FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->run_codes, 63*2, fail);
00125 FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->run_bits, 63, fail);
00126
00127 ctx->vlc_codes += max_level*2;
00128 ctx->vlc_bits += max_level*2;
00129 for (level = -max_level; level < max_level; level++) {
00130 for (run = 0; run < 2; run++) {
00131 int index = (level<<1)|run;
00132 int sign, offset = 0, alevel = level;
00133
00134 MASK_ABS(sign, alevel);
00135 if (alevel > 64) {
00136 offset = (alevel-1)>>6;
00137 alevel -= offset<<6;
00138 }
00139 for (j = 0; j < 257; j++) {
00140 if (ctx->cid_table->ac_level[j] >> 1 == alevel &&
00141 (!offset || (ctx->cid_table->ac_flags[j] & 1) && offset) &&
00142 (!run || (ctx->cid_table->ac_flags[j] & 2) && run)) {
00143 av_assert1(!ctx->vlc_codes[index]);
00144 if (alevel) {
00145 ctx->vlc_codes[index] = (ctx->cid_table->ac_codes[j]<<1)|(sign&1);
00146 ctx->vlc_bits [index] = ctx->cid_table->ac_bits[j]+1;
00147 } else {
00148 ctx->vlc_codes[index] = ctx->cid_table->ac_codes[j];
00149 ctx->vlc_bits [index] = ctx->cid_table->ac_bits [j];
00150 }
00151 break;
00152 }
00153 }
00154 av_assert0(!alevel || j < 257);
00155 if (offset) {
00156 ctx->vlc_codes[index] = (ctx->vlc_codes[index]<<ctx->cid_table->index_bits)|offset;
00157 ctx->vlc_bits [index]+= ctx->cid_table->index_bits;
00158 }
00159 }
00160 }
00161 for (i = 0; i < 62; i++) {
00162 int run = ctx->cid_table->run[i];
00163 av_assert0(run < 63);
00164 ctx->run_codes[run] = ctx->cid_table->run_codes[i];
00165 ctx->run_bits [run] = ctx->cid_table->run_bits[i];
00166 }
00167 return 0;
00168 fail:
00169 return -1;
00170 }
00171
00172 static int dnxhd_init_qmat(DNXHDEncContext *ctx, int lbias, int cbias)
00173 {
00174
00175 uint16_t weight_matrix[64] = {1,};
00176 int qscale, i;
00177 const uint8_t *luma_weight_table = ctx->cid_table->luma_weight;
00178 const uint8_t *chroma_weight_table = ctx->cid_table->chroma_weight;
00179
00180 FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->qmatrix_l, (ctx->m.avctx->qmax+1) * 64 * sizeof(int), fail);
00181 FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->qmatrix_c, (ctx->m.avctx->qmax+1) * 64 * sizeof(int), fail);
00182 FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->qmatrix_l16, (ctx->m.avctx->qmax+1) * 64 * 2 * sizeof(uint16_t), fail);
00183 FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->qmatrix_c16, (ctx->m.avctx->qmax+1) * 64 * 2 * sizeof(uint16_t), fail);
00184
00185 if (ctx->cid_table->bit_depth == 8) {
00186 for (i = 1; i < 64; i++) {
00187 int j = ctx->m.dsp.idct_permutation[ff_zigzag_direct[i]];
00188 weight_matrix[j] = ctx->cid_table->luma_weight[i];
00189 }
00190 ff_convert_matrix(&ctx->m.dsp, ctx->qmatrix_l, ctx->qmatrix_l16, weight_matrix,
00191 ctx->m.intra_quant_bias, 1, ctx->m.avctx->qmax, 1);
00192 for (i = 1; i < 64; i++) {
00193 int j = ctx->m.dsp.idct_permutation[ff_zigzag_direct[i]];
00194 weight_matrix[j] = ctx->cid_table->chroma_weight[i];
00195 }
00196 ff_convert_matrix(&ctx->m.dsp, ctx->qmatrix_c, ctx->qmatrix_c16, weight_matrix,
00197 ctx->m.intra_quant_bias, 1, ctx->m.avctx->qmax, 1);
00198
00199 for (qscale = 1; qscale <= ctx->m.avctx->qmax; qscale++) {
00200 for (i = 0; i < 64; i++) {
00201 ctx->qmatrix_l [qscale] [i] <<= 2; ctx->qmatrix_c [qscale] [i] <<= 2;
00202 ctx->qmatrix_l16[qscale][0][i] <<= 2; ctx->qmatrix_l16[qscale][1][i] <<= 2;
00203 ctx->qmatrix_c16[qscale][0][i] <<= 2; ctx->qmatrix_c16[qscale][1][i] <<= 2;
00204 }
00205 }
00206 } else {
00207
00208 for (qscale = 1; qscale <= ctx->m.avctx->qmax; qscale++) {
00209 for (i = 1; i < 64; i++) {
00210 int j = ctx->m.dsp.idct_permutation[ff_zigzag_direct[i]];
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220 ctx->qmatrix_l[qscale][j] = (1 << (DNX10BIT_QMAT_SHIFT + 1)) / (qscale * luma_weight_table[i]);
00221 ctx->qmatrix_c[qscale][j] = (1 << (DNX10BIT_QMAT_SHIFT + 1)) / (qscale * chroma_weight_table[i]);
00222 }
00223 }
00224 }
00225
00226 ctx->m.q_chroma_intra_matrix16 = ctx->qmatrix_c16;
00227 ctx->m.q_chroma_intra_matrix = ctx->qmatrix_c;
00228 ctx->m.q_intra_matrix16 = ctx->qmatrix_l16;
00229 ctx->m.q_intra_matrix = ctx->qmatrix_l;
00230
00231 return 0;
00232 fail:
00233 return -1;
00234 }
00235
00236 static int dnxhd_init_rc(DNXHDEncContext *ctx)
00237 {
00238 FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->mb_rc, 8160*ctx->m.avctx->qmax*sizeof(RCEntry), fail);
00239 if (ctx->m.avctx->mb_decision != FF_MB_DECISION_RD)
00240 FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->mb_cmp, ctx->m.mb_num*sizeof(RCCMPEntry), fail);
00241
00242 ctx->frame_bits = (ctx->cid_table->coding_unit_size - 640 - 4 - ctx->min_padding) * 8;
00243 ctx->qscale = 1;
00244 ctx->lambda = 2<<LAMBDA_FRAC_BITS;
00245 return 0;
00246 fail:
00247 return -1;
00248 }
00249
00250 static int dnxhd_encode_init(AVCodecContext *avctx)
00251 {
00252 DNXHDEncContext *ctx = avctx->priv_data;
00253 int i, index, bit_depth;
00254
00255 switch (avctx->pix_fmt) {
00256 case PIX_FMT_YUV422P:
00257 bit_depth = 8;
00258 break;
00259 case PIX_FMT_YUV422P10:
00260 bit_depth = 10;
00261 break;
00262 default:
00263 av_log(avctx, AV_LOG_ERROR, "pixel format is incompatible with DNxHD\n");
00264 return -1;
00265 }
00266
00267 ctx->cid = ff_dnxhd_find_cid(avctx, bit_depth);
00268 if (!ctx->cid) {
00269 av_log(avctx, AV_LOG_ERROR, "video parameters incompatible with DNxHD\n");
00270 return -1;
00271 }
00272 av_log(avctx, AV_LOG_DEBUG, "cid %d\n", ctx->cid);
00273
00274 index = ff_dnxhd_get_cid_table(ctx->cid);
00275 ctx->cid_table = &ff_dnxhd_cid_table[index];
00276
00277 ctx->m.avctx = avctx;
00278 ctx->m.mb_intra = 1;
00279 ctx->m.h263_aic = 1;
00280
00281 avctx->bits_per_raw_sample = ctx->cid_table->bit_depth;
00282
00283 ff_dct_common_init(&ctx->m);
00284 ff_dct_encode_init(&ctx->m);
00285
00286 if (!ctx->m.dct_quantize)
00287 ctx->m.dct_quantize = ff_dct_quantize_c;
00288
00289 if (ctx->cid_table->bit_depth == 10) {
00290 ctx->m.dct_quantize = dnxhd_10bit_dct_quantize;
00291 ctx->get_pixels_8x4_sym = dnxhd_10bit_get_pixels_8x4_sym;
00292 ctx->block_width_l2 = 4;
00293 } else {
00294 ctx->get_pixels_8x4_sym = dnxhd_8bit_get_pixels_8x4_sym;
00295 ctx->block_width_l2 = 3;
00296 }
00297
00298 if (ARCH_X86)
00299 ff_dnxhdenc_init_x86(ctx);
00300
00301 ctx->m.mb_height = (avctx->height + 15) / 16;
00302 ctx->m.mb_width = (avctx->width + 15) / 16;
00303
00304 if (avctx->flags & CODEC_FLAG_INTERLACED_DCT) {
00305 ctx->interlaced = 1;
00306 ctx->m.mb_height /= 2;
00307 }
00308
00309 ctx->m.mb_num = ctx->m.mb_height * ctx->m.mb_width;
00310
00311 if (avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
00312 ctx->m.intra_quant_bias = avctx->intra_quant_bias;
00313 if (dnxhd_init_qmat(ctx, ctx->m.intra_quant_bias, 0) < 0)
00314 return -1;
00315
00316
00317 if (ctx->nitris_compat)
00318 ctx->min_padding = 1600;
00319
00320 if (dnxhd_init_vlc(ctx) < 0)
00321 return -1;
00322 if (dnxhd_init_rc(ctx) < 0)
00323 return -1;
00324
00325 FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->slice_size, ctx->m.mb_height*sizeof(uint32_t), fail);
00326 FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->slice_offs, ctx->m.mb_height*sizeof(uint32_t), fail);
00327 FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->mb_bits, ctx->m.mb_num *sizeof(uint16_t), fail);
00328 FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->mb_qscale, ctx->m.mb_num *sizeof(uint8_t), fail);
00329
00330 ctx->frame.key_frame = 1;
00331 ctx->frame.pict_type = AV_PICTURE_TYPE_I;
00332 ctx->m.avctx->coded_frame = &ctx->frame;
00333
00334 if (avctx->thread_count > MAX_THREADS) {
00335 av_log(avctx, AV_LOG_ERROR, "too many threads\n");
00336 return -1;
00337 }
00338
00339 ctx->thread[0] = ctx;
00340 for (i = 1; i < avctx->thread_count; i++) {
00341 ctx->thread[i] = av_malloc(sizeof(DNXHDEncContext));
00342 memcpy(ctx->thread[i], ctx, sizeof(DNXHDEncContext));
00343 }
00344
00345 return 0;
00346 fail:
00347 return -1;
00348 }
00349
00350 static int dnxhd_write_header(AVCodecContext *avctx, uint8_t *buf)
00351 {
00352 DNXHDEncContext *ctx = avctx->priv_data;
00353 const uint8_t header_prefix[5] = { 0x00,0x00,0x02,0x80,0x01 };
00354
00355 memset(buf, 0, 640);
00356
00357 memcpy(buf, header_prefix, 5);
00358 buf[5] = ctx->interlaced ? ctx->cur_field+2 : 0x01;
00359 buf[6] = 0x80;
00360 buf[7] = 0xa0;
00361 AV_WB16(buf + 0x18, avctx->height>>ctx->interlaced);
00362 AV_WB16(buf + 0x1a, avctx->width);
00363 AV_WB16(buf + 0x1d, avctx->height>>ctx->interlaced);
00364
00365 buf[0x21] = ctx->cid_table->bit_depth == 10 ? 0x58 : 0x38;
00366 buf[0x22] = 0x88 + (ctx->interlaced<<2);
00367 AV_WB32(buf + 0x28, ctx->cid);
00368 buf[0x2c] = ctx->interlaced ? 0 : 0x80;
00369
00370 buf[0x5f] = 0x01;
00371
00372 buf[0x167] = 0x02;
00373 AV_WB16(buf + 0x16a, ctx->m.mb_height * 4 + 4);
00374 buf[0x16d] = ctx->m.mb_height;
00375 buf[0x16f] = 0x10;
00376
00377 ctx->msip = buf + 0x170;
00378 return 0;
00379 }
00380
00381 static av_always_inline void dnxhd_encode_dc(DNXHDEncContext *ctx, int diff)
00382 {
00383 int nbits;
00384 if (diff < 0) {
00385 nbits = av_log2_16bit(-2*diff);
00386 diff--;
00387 } else {
00388 nbits = av_log2_16bit(2*diff);
00389 }
00390 put_bits(&ctx->m.pb, ctx->cid_table->dc_bits[nbits] + nbits,
00391 (ctx->cid_table->dc_codes[nbits]<<nbits) + (diff & ((1 << nbits) - 1)));
00392 }
00393
00394 static av_always_inline void dnxhd_encode_block(DNXHDEncContext *ctx, DCTELEM *block, int last_index, int n)
00395 {
00396 int last_non_zero = 0;
00397 int slevel, i, j;
00398
00399 dnxhd_encode_dc(ctx, block[0] - ctx->m.last_dc[n]);
00400 ctx->m.last_dc[n] = block[0];
00401
00402 for (i = 1; i <= last_index; i++) {
00403 j = ctx->m.intra_scantable.permutated[i];
00404 slevel = block[j];
00405 if (slevel) {
00406 int run_level = i - last_non_zero - 1;
00407 int rlevel = (slevel<<1)|!!run_level;
00408 put_bits(&ctx->m.pb, ctx->vlc_bits[rlevel], ctx->vlc_codes[rlevel]);
00409 if (run_level)
00410 put_bits(&ctx->m.pb, ctx->run_bits[run_level], ctx->run_codes[run_level]);
00411 last_non_zero = i;
00412 }
00413 }
00414 put_bits(&ctx->m.pb, ctx->vlc_bits[0], ctx->vlc_codes[0]);
00415 }
00416
00417 static av_always_inline void dnxhd_unquantize_c(DNXHDEncContext *ctx, DCTELEM *block, int n, int qscale, int last_index)
00418 {
00419 const uint8_t *weight_matrix;
00420 int level;
00421 int i;
00422
00423 weight_matrix = (n&2) ? ctx->cid_table->chroma_weight : ctx->cid_table->luma_weight;
00424
00425 for (i = 1; i <= last_index; i++) {
00426 int j = ctx->m.intra_scantable.permutated[i];
00427 level = block[j];
00428 if (level) {
00429 if (level < 0) {
00430 level = (1-2*level) * qscale * weight_matrix[i];
00431 if (ctx->cid_table->bit_depth == 10) {
00432 if (weight_matrix[i] != 8)
00433 level += 8;
00434 level >>= 4;
00435 } else {
00436 if (weight_matrix[i] != 32)
00437 level += 32;
00438 level >>= 6;
00439 }
00440 level = -level;
00441 } else {
00442 level = (2*level+1) * qscale * weight_matrix[i];
00443 if (ctx->cid_table->bit_depth == 10) {
00444 if (weight_matrix[i] != 8)
00445 level += 8;
00446 level >>= 4;
00447 } else {
00448 if (weight_matrix[i] != 32)
00449 level += 32;
00450 level >>= 6;
00451 }
00452 }
00453 block[j] = level;
00454 }
00455 }
00456 }
00457
00458 static av_always_inline int dnxhd_ssd_block(DCTELEM *qblock, DCTELEM *block)
00459 {
00460 int score = 0;
00461 int i;
00462 for (i = 0; i < 64; i++)
00463 score += (block[i] - qblock[i]) * (block[i] - qblock[i]);
00464 return score;
00465 }
00466
00467 static av_always_inline int dnxhd_calc_ac_bits(DNXHDEncContext *ctx, DCTELEM *block, int last_index)
00468 {
00469 int last_non_zero = 0;
00470 int bits = 0;
00471 int i, j, level;
00472 for (i = 1; i <= last_index; i++) {
00473 j = ctx->m.intra_scantable.permutated[i];
00474 level = block[j];
00475 if (level) {
00476 int run_level = i - last_non_zero - 1;
00477 bits += ctx->vlc_bits[(level<<1)|!!run_level]+ctx->run_bits[run_level];
00478 last_non_zero = i;
00479 }
00480 }
00481 return bits;
00482 }
00483
00484 static av_always_inline void dnxhd_get_blocks(DNXHDEncContext *ctx, int mb_x, int mb_y)
00485 {
00486 const int bs = ctx->block_width_l2;
00487 const int bw = 1 << bs;
00488 const uint8_t *ptr_y = ctx->thread[0]->src[0] + ((mb_y << 4) * ctx->m.linesize) + (mb_x << bs+1);
00489 const uint8_t *ptr_u = ctx->thread[0]->src[1] + ((mb_y << 4) * ctx->m.uvlinesize) + (mb_x << bs);
00490 const uint8_t *ptr_v = ctx->thread[0]->src[2] + ((mb_y << 4) * ctx->m.uvlinesize) + (mb_x << bs);
00491 DSPContext *dsp = &ctx->m.dsp;
00492
00493 dsp->get_pixels(ctx->blocks[0], ptr_y, ctx->m.linesize);
00494 dsp->get_pixels(ctx->blocks[1], ptr_y + bw, ctx->m.linesize);
00495 dsp->get_pixels(ctx->blocks[2], ptr_u, ctx->m.uvlinesize);
00496 dsp->get_pixels(ctx->blocks[3], ptr_v, ctx->m.uvlinesize);
00497
00498 if (mb_y+1 == ctx->m.mb_height && ctx->m.avctx->height == 1080) {
00499 if (ctx->interlaced) {
00500 ctx->get_pixels_8x4_sym(ctx->blocks[4], ptr_y + ctx->dct_y_offset, ctx->m.linesize);
00501 ctx->get_pixels_8x4_sym(ctx->blocks[5], ptr_y + ctx->dct_y_offset + bw, ctx->m.linesize);
00502 ctx->get_pixels_8x4_sym(ctx->blocks[6], ptr_u + ctx->dct_uv_offset, ctx->m.uvlinesize);
00503 ctx->get_pixels_8x4_sym(ctx->blocks[7], ptr_v + ctx->dct_uv_offset, ctx->m.uvlinesize);
00504 } else {
00505 dsp->clear_block(ctx->blocks[4]);
00506 dsp->clear_block(ctx->blocks[5]);
00507 dsp->clear_block(ctx->blocks[6]);
00508 dsp->clear_block(ctx->blocks[7]);
00509 }
00510 } else {
00511 dsp->get_pixels(ctx->blocks[4], ptr_y + ctx->dct_y_offset, ctx->m.linesize);
00512 dsp->get_pixels(ctx->blocks[5], ptr_y + ctx->dct_y_offset + bw, ctx->m.linesize);
00513 dsp->get_pixels(ctx->blocks[6], ptr_u + ctx->dct_uv_offset, ctx->m.uvlinesize);
00514 dsp->get_pixels(ctx->blocks[7], ptr_v + ctx->dct_uv_offset, ctx->m.uvlinesize);
00515 }
00516 }
00517
00518 static av_always_inline int dnxhd_switch_matrix(DNXHDEncContext *ctx, int i)
00519 {
00520 const static uint8_t component[8]={0,0,1,2,0,0,1,2};
00521 return component[i];
00522 }
00523
00524 static int dnxhd_calc_bits_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
00525 {
00526 DNXHDEncContext *ctx = avctx->priv_data;
00527 int mb_y = jobnr, mb_x;
00528 int qscale = ctx->qscale;
00529 LOCAL_ALIGNED_16(DCTELEM, block, [64]);
00530 ctx = ctx->thread[threadnr];
00531
00532 ctx->m.last_dc[0] =
00533 ctx->m.last_dc[1] =
00534 ctx->m.last_dc[2] = 1 << (ctx->cid_table->bit_depth + 2);
00535
00536 for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
00537 unsigned mb = mb_y * ctx->m.mb_width + mb_x;
00538 int ssd = 0;
00539 int ac_bits = 0;
00540 int dc_bits = 0;
00541 int i;
00542
00543 dnxhd_get_blocks(ctx, mb_x, mb_y);
00544
00545 for (i = 0; i < 8; i++) {
00546 DCTELEM *src_block = ctx->blocks[i];
00547 int overflow, nbits, diff, last_index;
00548 int n = dnxhd_switch_matrix(ctx, i);
00549
00550 memcpy(block, src_block, 64*sizeof(*block));
00551 last_index = ctx->m.dct_quantize(&ctx->m, block, 4&(2*i), qscale, &overflow);
00552 ac_bits += dnxhd_calc_ac_bits(ctx, block, last_index);
00553
00554 diff = block[0] - ctx->m.last_dc[n];
00555 if (diff < 0) nbits = av_log2_16bit(-2*diff);
00556 else nbits = av_log2_16bit( 2*diff);
00557
00558 av_assert1(nbits < ctx->cid_table->bit_depth + 4);
00559 dc_bits += ctx->cid_table->dc_bits[nbits] + nbits;
00560
00561 ctx->m.last_dc[n] = block[0];
00562
00563 if (avctx->mb_decision == FF_MB_DECISION_RD || !RC_VARIANCE) {
00564 dnxhd_unquantize_c(ctx, block, i, qscale, last_index);
00565 ctx->m.dsp.idct(block);
00566 ssd += dnxhd_ssd_block(block, src_block);
00567 }
00568 }
00569 ctx->mb_rc[qscale][mb].ssd = ssd;
00570 ctx->mb_rc[qscale][mb].bits = ac_bits+dc_bits+12+8*ctx->vlc_bits[0];
00571 }
00572 return 0;
00573 }
00574
00575 static int dnxhd_encode_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
00576 {
00577 DNXHDEncContext *ctx = avctx->priv_data;
00578 int mb_y = jobnr, mb_x;
00579 ctx = ctx->thread[threadnr];
00580 init_put_bits(&ctx->m.pb, (uint8_t *)arg + 640 + ctx->slice_offs[jobnr], ctx->slice_size[jobnr]);
00581
00582 ctx->m.last_dc[0] =
00583 ctx->m.last_dc[1] =
00584 ctx->m.last_dc[2] = 1 << (ctx->cid_table->bit_depth + 2);
00585 for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
00586 unsigned mb = mb_y * ctx->m.mb_width + mb_x;
00587 int qscale = ctx->mb_qscale[mb];
00588 int i;
00589
00590 put_bits(&ctx->m.pb, 12, qscale<<1);
00591
00592 dnxhd_get_blocks(ctx, mb_x, mb_y);
00593
00594 for (i = 0; i < 8; i++) {
00595 DCTELEM *block = ctx->blocks[i];
00596 int overflow, n = dnxhd_switch_matrix(ctx, i);
00597 int last_index = ctx->m.dct_quantize(&ctx->m, block, 4&(2*i), qscale, &overflow);
00598
00599 dnxhd_encode_block(ctx, block, last_index, n);
00600
00601 }
00602 }
00603 if (put_bits_count(&ctx->m.pb)&31)
00604 put_bits(&ctx->m.pb, 32-(put_bits_count(&ctx->m.pb)&31), 0);
00605 flush_put_bits(&ctx->m.pb);
00606 return 0;
00607 }
00608
00609 static void dnxhd_setup_threads_slices(DNXHDEncContext *ctx)
00610 {
00611 int mb_y, mb_x;
00612 int offset = 0;
00613 for (mb_y = 0; mb_y < ctx->m.mb_height; mb_y++) {
00614 int thread_size;
00615 ctx->slice_offs[mb_y] = offset;
00616 ctx->slice_size[mb_y] = 0;
00617 for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
00618 unsigned mb = mb_y * ctx->m.mb_width + mb_x;
00619 ctx->slice_size[mb_y] += ctx->mb_bits[mb];
00620 }
00621 ctx->slice_size[mb_y] = (ctx->slice_size[mb_y]+31)&~31;
00622 ctx->slice_size[mb_y] >>= 3;
00623 thread_size = ctx->slice_size[mb_y];
00624 offset += thread_size;
00625 }
00626 }
00627
00628 static int dnxhd_mb_var_thread(AVCodecContext *avctx, void *arg, int jobnr, int threadnr)
00629 {
00630 DNXHDEncContext *ctx = avctx->priv_data;
00631 int mb_y = jobnr, mb_x;
00632 ctx = ctx->thread[threadnr];
00633 if (ctx->cid_table->bit_depth == 8) {
00634 uint8_t *pix = ctx->thread[0]->src[0] + ((mb_y<<4) * ctx->m.linesize);
00635 for (mb_x = 0; mb_x < ctx->m.mb_width; ++mb_x, pix += 16) {
00636 unsigned mb = mb_y * ctx->m.mb_width + mb_x;
00637 int sum = ctx->m.dsp.pix_sum(pix, ctx->m.linesize);
00638 int varc = (ctx->m.dsp.pix_norm1(pix, ctx->m.linesize) - (((unsigned)sum*sum)>>8)+128)>>8;
00639 ctx->mb_cmp[mb].value = varc;
00640 ctx->mb_cmp[mb].mb = mb;
00641 }
00642 } else {
00643 int const linesize = ctx->m.linesize >> 1;
00644 for (mb_x = 0; mb_x < ctx->m.mb_width; ++mb_x) {
00645 uint16_t *pix = (uint16_t*)ctx->thread[0]->src[0] + ((mb_y << 4) * linesize) + (mb_x << 4);
00646 unsigned mb = mb_y * ctx->m.mb_width + mb_x;
00647 int sum = 0;
00648 int sqsum = 0;
00649 int mean, sqmean;
00650 int i, j;
00651
00652 for (i = 0; i < 16; ++i) {
00653 for (j = 0; j < 16; ++j) {
00654
00655 int const sample = (unsigned)pix[j] >> 6;
00656 sum += sample;
00657 sqsum += sample * sample;
00658
00659 }
00660 pix += linesize;
00661 }
00662 mean = sum >> 8;
00663 sqmean = sqsum >> 8;
00664 ctx->mb_cmp[mb].value = sqmean - mean * mean;
00665 ctx->mb_cmp[mb].mb = mb;
00666 }
00667 }
00668 return 0;
00669 }
00670
00671 static int dnxhd_encode_rdo(AVCodecContext *avctx, DNXHDEncContext *ctx)
00672 {
00673 int lambda, up_step, down_step;
00674 int last_lower = INT_MAX, last_higher = 0;
00675 int x, y, q;
00676
00677 for (q = 1; q < avctx->qmax; q++) {
00678 ctx->qscale = q;
00679 avctx->execute2(avctx, dnxhd_calc_bits_thread, NULL, NULL, ctx->m.mb_height);
00680 }
00681 up_step = down_step = 2<<LAMBDA_FRAC_BITS;
00682 lambda = ctx->lambda;
00683
00684 for (;;) {
00685 int bits = 0;
00686 int end = 0;
00687 if (lambda == last_higher) {
00688 lambda++;
00689 end = 1;
00690 }
00691 for (y = 0; y < ctx->m.mb_height; y++) {
00692 for (x = 0; x < ctx->m.mb_width; x++) {
00693 unsigned min = UINT_MAX;
00694 int qscale = 1;
00695 int mb = y*ctx->m.mb_width+x;
00696 for (q = 1; q < avctx->qmax; q++) {
00697 unsigned score = ctx->mb_rc[q][mb].bits*lambda+
00698 ((unsigned)ctx->mb_rc[q][mb].ssd<<LAMBDA_FRAC_BITS);
00699 if (score < min) {
00700 min = score;
00701 qscale = q;
00702 }
00703 }
00704 bits += ctx->mb_rc[qscale][mb].bits;
00705 ctx->mb_qscale[mb] = qscale;
00706 ctx->mb_bits[mb] = ctx->mb_rc[qscale][mb].bits;
00707 }
00708 bits = (bits+31)&~31;
00709 if (bits > ctx->frame_bits)
00710 break;
00711 }
00712
00713
00714 if (end) {
00715 if (bits > ctx->frame_bits)
00716 return -1;
00717 break;
00718 }
00719 if (bits < ctx->frame_bits) {
00720 last_lower = FFMIN(lambda, last_lower);
00721 if (last_higher != 0)
00722 lambda = (lambda+last_higher)>>1;
00723 else
00724 lambda -= down_step;
00725 down_step = FFMIN((int64_t)down_step*5, INT_MAX);
00726 up_step = 1<<LAMBDA_FRAC_BITS;
00727 lambda = FFMAX(1, lambda);
00728 if (lambda == last_lower)
00729 break;
00730 } else {
00731 last_higher = FFMAX(lambda, last_higher);
00732 if (last_lower != INT_MAX)
00733 lambda = (lambda+last_lower)>>1;
00734 else if ((int64_t)lambda + up_step > INT_MAX)
00735 return -1;
00736 else
00737 lambda += up_step;
00738 up_step = FFMIN((int64_t)up_step*5, INT_MAX);
00739 down_step = 1<<LAMBDA_FRAC_BITS;
00740 }
00741 }
00742
00743 ctx->lambda = lambda;
00744 return 0;
00745 }
00746
00747 static int dnxhd_find_qscale(DNXHDEncContext *ctx)
00748 {
00749 int bits = 0;
00750 int up_step = 1;
00751 int down_step = 1;
00752 int last_higher = 0;
00753 int last_lower = INT_MAX;
00754 int qscale;
00755 int x, y;
00756
00757 qscale = ctx->qscale;
00758 for (;;) {
00759 bits = 0;
00760 ctx->qscale = qscale;
00761
00762 ctx->m.avctx->execute2(ctx->m.avctx, dnxhd_calc_bits_thread, NULL, NULL, ctx->m.mb_height);
00763 for (y = 0; y < ctx->m.mb_height; y++) {
00764 for (x = 0; x < ctx->m.mb_width; x++)
00765 bits += ctx->mb_rc[qscale][y*ctx->m.mb_width+x].bits;
00766 bits = (bits+31)&~31;
00767 if (bits > ctx->frame_bits)
00768 break;
00769 }
00770
00771
00772 if (bits < ctx->frame_bits) {
00773 if (qscale == 1)
00774 return 1;
00775 if (last_higher == qscale - 1) {
00776 qscale = last_higher;
00777 break;
00778 }
00779 last_lower = FFMIN(qscale, last_lower);
00780 if (last_higher != 0)
00781 qscale = (qscale+last_higher)>>1;
00782 else
00783 qscale -= down_step++;
00784 if (qscale < 1)
00785 qscale = 1;
00786 up_step = 1;
00787 } else {
00788 if (last_lower == qscale + 1)
00789 break;
00790 last_higher = FFMAX(qscale, last_higher);
00791 if (last_lower != INT_MAX)
00792 qscale = (qscale+last_lower)>>1;
00793 else
00794 qscale += up_step++;
00795 down_step = 1;
00796 if (qscale >= ctx->m.avctx->qmax)
00797 return -1;
00798 }
00799 }
00800
00801 ctx->qscale = qscale;
00802 return 0;
00803 }
00804
00805 #define BUCKET_BITS 8
00806 #define RADIX_PASSES 4
00807 #define NBUCKETS (1 << BUCKET_BITS)
00808
00809 static inline int get_bucket(int value, int shift)
00810 {
00811 value >>= shift;
00812 value &= NBUCKETS - 1;
00813 return NBUCKETS - 1 - value;
00814 }
00815
00816 static void radix_count(const RCCMPEntry *data, int size, int buckets[RADIX_PASSES][NBUCKETS])
00817 {
00818 int i, j;
00819 memset(buckets, 0, sizeof(buckets[0][0]) * RADIX_PASSES * NBUCKETS);
00820 for (i = 0; i < size; i++) {
00821 int v = data[i].value;
00822 for (j = 0; j < RADIX_PASSES; j++) {
00823 buckets[j][get_bucket(v, 0)]++;
00824 v >>= BUCKET_BITS;
00825 }
00826 av_assert1(!v);
00827 }
00828 for (j = 0; j < RADIX_PASSES; j++) {
00829 int offset = size;
00830 for (i = NBUCKETS - 1; i >= 0; i--)
00831 buckets[j][i] = offset -= buckets[j][i];
00832 av_assert1(!buckets[j][0]);
00833 }
00834 }
00835
00836 static void radix_sort_pass(RCCMPEntry *dst, const RCCMPEntry *data, int size, int buckets[NBUCKETS], int pass)
00837 {
00838 int shift = pass * BUCKET_BITS;
00839 int i;
00840 for (i = 0; i < size; i++) {
00841 int v = get_bucket(data[i].value, shift);
00842 int pos = buckets[v]++;
00843 dst[pos] = data[i];
00844 }
00845 }
00846
00847 static void radix_sort(RCCMPEntry *data, int size)
00848 {
00849 int buckets[RADIX_PASSES][NBUCKETS];
00850 RCCMPEntry *tmp = av_malloc(sizeof(*tmp) * size);
00851 radix_count(data, size, buckets);
00852 radix_sort_pass(tmp, data, size, buckets[0], 0);
00853 radix_sort_pass(data, tmp, size, buckets[1], 1);
00854 if (buckets[2][NBUCKETS - 1] || buckets[3][NBUCKETS - 1]) {
00855 radix_sort_pass(tmp, data, size, buckets[2], 2);
00856 radix_sort_pass(data, tmp, size, buckets[3], 3);
00857 }
00858 av_free(tmp);
00859 }
00860
00861 static int dnxhd_encode_fast(AVCodecContext *avctx, DNXHDEncContext *ctx)
00862 {
00863 int max_bits = 0;
00864 int ret, x, y;
00865 if ((ret = dnxhd_find_qscale(ctx)) < 0)
00866 return -1;
00867 for (y = 0; y < ctx->m.mb_height; y++) {
00868 for (x = 0; x < ctx->m.mb_width; x++) {
00869 int mb = y*ctx->m.mb_width+x;
00870 int delta_bits;
00871 ctx->mb_qscale[mb] = ctx->qscale;
00872 ctx->mb_bits[mb] = ctx->mb_rc[ctx->qscale][mb].bits;
00873 max_bits += ctx->mb_rc[ctx->qscale][mb].bits;
00874 if (!RC_VARIANCE) {
00875 delta_bits = ctx->mb_rc[ctx->qscale][mb].bits-ctx->mb_rc[ctx->qscale+1][mb].bits;
00876 ctx->mb_cmp[mb].mb = mb;
00877 ctx->mb_cmp[mb].value = delta_bits ?
00878 ((ctx->mb_rc[ctx->qscale][mb].ssd-ctx->mb_rc[ctx->qscale+1][mb].ssd)*100)/delta_bits
00879 : INT_MIN;
00880 }
00881 }
00882 max_bits += 31;
00883 }
00884 if (!ret) {
00885 if (RC_VARIANCE)
00886 avctx->execute2(avctx, dnxhd_mb_var_thread, NULL, NULL, ctx->m.mb_height);
00887 radix_sort(ctx->mb_cmp, ctx->m.mb_num);
00888 for (x = 0; x < ctx->m.mb_num && max_bits > ctx->frame_bits; x++) {
00889 int mb = ctx->mb_cmp[x].mb;
00890 max_bits -= ctx->mb_rc[ctx->qscale][mb].bits - ctx->mb_rc[ctx->qscale+1][mb].bits;
00891 ctx->mb_qscale[mb] = ctx->qscale+1;
00892 ctx->mb_bits[mb] = ctx->mb_rc[ctx->qscale+1][mb].bits;
00893 }
00894 }
00895 return 0;
00896 }
00897
00898 static void dnxhd_load_picture(DNXHDEncContext *ctx, const AVFrame *frame)
00899 {
00900 int i;
00901
00902 for (i = 0; i < 3; i++) {
00903 ctx->frame.data[i] = frame->data[i];
00904 ctx->frame.linesize[i] = frame->linesize[i];
00905 }
00906
00907 for (i = 0; i < ctx->m.avctx->thread_count; i++) {
00908 ctx->thread[i]->m.linesize = ctx->frame.linesize[0]<<ctx->interlaced;
00909 ctx->thread[i]->m.uvlinesize = ctx->frame.linesize[1]<<ctx->interlaced;
00910 ctx->thread[i]->dct_y_offset = ctx->m.linesize *8;
00911 ctx->thread[i]->dct_uv_offset = ctx->m.uvlinesize*8;
00912 }
00913
00914 ctx->frame.interlaced_frame = frame->interlaced_frame;
00915 ctx->cur_field = frame->interlaced_frame && !frame->top_field_first;
00916 }
00917
00918 static int dnxhd_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
00919 const AVFrame *frame, int *got_packet)
00920 {
00921 DNXHDEncContext *ctx = avctx->priv_data;
00922 int first_field = 1;
00923 int offset, i, ret;
00924 uint8_t *buf;
00925
00926 if ((ret = ff_alloc_packet2(avctx, pkt, ctx->cid_table->frame_size)) < 0)
00927 return ret;
00928 buf = pkt->data;
00929
00930 dnxhd_load_picture(ctx, frame);
00931
00932 encode_coding_unit:
00933 for (i = 0; i < 3; i++) {
00934 ctx->src[i] = ctx->frame.data[i];
00935 if (ctx->interlaced && ctx->cur_field)
00936 ctx->src[i] += ctx->frame.linesize[i];
00937 }
00938
00939 dnxhd_write_header(avctx, buf);
00940
00941 if (avctx->mb_decision == FF_MB_DECISION_RD)
00942 ret = dnxhd_encode_rdo(avctx, ctx);
00943 else
00944 ret = dnxhd_encode_fast(avctx, ctx);
00945 if (ret < 0) {
00946 av_log(avctx, AV_LOG_ERROR,
00947 "picture could not fit ratecontrol constraints, increase qmax\n");
00948 return -1;
00949 }
00950
00951 dnxhd_setup_threads_slices(ctx);
00952
00953 offset = 0;
00954 for (i = 0; i < ctx->m.mb_height; i++) {
00955 AV_WB32(ctx->msip + i * 4, offset);
00956 offset += ctx->slice_size[i];
00957 av_assert1(!(ctx->slice_size[i] & 3));
00958 }
00959
00960 avctx->execute2(avctx, dnxhd_encode_thread, buf, NULL, ctx->m.mb_height);
00961
00962 av_assert1(640 + offset + 4 <= ctx->cid_table->coding_unit_size);
00963 memset(buf + 640 + offset, 0, ctx->cid_table->coding_unit_size - 4 - offset - 640);
00964
00965 AV_WB32(buf + ctx->cid_table->coding_unit_size - 4, 0x600DC0DE);
00966
00967 if (ctx->interlaced && first_field) {
00968 first_field = 0;
00969 ctx->cur_field ^= 1;
00970 buf += ctx->cid_table->coding_unit_size;
00971 goto encode_coding_unit;
00972 }
00973
00974 ctx->frame.quality = ctx->qscale*FF_QP2LAMBDA;
00975
00976 pkt->flags |= AV_PKT_FLAG_KEY;
00977 *got_packet = 1;
00978 return 0;
00979 }
00980
00981 static int dnxhd_encode_end(AVCodecContext *avctx)
00982 {
00983 DNXHDEncContext *ctx = avctx->priv_data;
00984 int max_level = 1<<(ctx->cid_table->bit_depth+2);
00985 int i;
00986
00987 av_free(ctx->vlc_codes-max_level*2);
00988 av_free(ctx->vlc_bits -max_level*2);
00989 av_freep(&ctx->run_codes);
00990 av_freep(&ctx->run_bits);
00991
00992 av_freep(&ctx->mb_bits);
00993 av_freep(&ctx->mb_qscale);
00994 av_freep(&ctx->mb_rc);
00995 av_freep(&ctx->mb_cmp);
00996 av_freep(&ctx->slice_size);
00997 av_freep(&ctx->slice_offs);
00998
00999 av_freep(&ctx->qmatrix_c);
01000 av_freep(&ctx->qmatrix_l);
01001 av_freep(&ctx->qmatrix_c16);
01002 av_freep(&ctx->qmatrix_l16);
01003
01004 for (i = 1; i < avctx->thread_count; i++)
01005 av_freep(&ctx->thread[i]);
01006
01007 return 0;
01008 }
01009
01010 static const AVCodecDefault dnxhd_defaults[] = {
01011 { "qmax", "1024" },
01012 { NULL },
01013 };
01014
01015 AVCodec ff_dnxhd_encoder = {
01016 .name = "dnxhd",
01017 .type = AVMEDIA_TYPE_VIDEO,
01018 .id = AV_CODEC_ID_DNXHD,
01019 .priv_data_size = sizeof(DNXHDEncContext),
01020 .init = dnxhd_encode_init,
01021 .encode2 = dnxhd_encode_picture,
01022 .close = dnxhd_encode_end,
01023 .capabilities = CODEC_CAP_SLICE_THREADS,
01024 .pix_fmts = (const enum PixelFormat[]){ PIX_FMT_YUV422P,
01025 PIX_FMT_YUV422P10,
01026 PIX_FMT_NONE },
01027 .long_name = NULL_IF_CONFIG_SMALL("VC3/DNxHD"),
01028 .priv_class = &class,
01029 .defaults = dnxhd_defaults,
01030 };