2007-10-08 13:27:18 +02:00
|
|
|
/*
|
|
|
|
* VC3/DNxHD encoder
|
|
|
|
* Copyright (c) 2007 Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>
|
2011-07-20 19:58:27 +02:00
|
|
|
* Copyright (c) 2011 MirriAd Ltd
|
2007-10-08 13:27:18 +02:00
|
|
|
*
|
|
|
|
* VC-3 encoder funded by the British Broadcasting Corporation
|
2011-07-20 19:58:27 +02:00
|
|
|
* 10 bit support added by MirriAd Ltd, Joseph Artsimovich <joseph@mirriad.com>
|
2007-10-08 13:27:18 +02:00
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2013-02-01 10:31:59 +01:00
|
|
|
#include "libavutil/attributes.h"
|
2013-02-08 00:31:13 +01:00
|
|
|
#include "libavutil/internal.h"
|
2020-06-10 15:38:08 +02:00
|
|
|
#include "libavutil/mem_internal.h"
|
2011-03-02 00:50:25 +01:00
|
|
|
#include "libavutil/opt.h"
|
2014-03-28 02:02:48 +01:00
|
|
|
|
2007-10-08 13:27:18 +02:00
|
|
|
#include "avcodec.h"
|
2014-01-14 10:33:47 +01:00
|
|
|
#include "blockdsp.h"
|
2021-04-25 01:43:26 +02:00
|
|
|
#include "encode.h"
|
2014-02-03 19:09:45 +01:00
|
|
|
#include "fdctdsp.h"
|
2012-02-11 20:03:42 +01:00
|
|
|
#include "internal.h"
|
2007-10-08 13:27:18 +02:00
|
|
|
#include "mpegvideo.h"
|
2014-02-03 23:29:09 +01:00
|
|
|
#include "pixblockdsp.h"
|
2020-06-02 23:38:33 +02:00
|
|
|
#include "packet_internal.h"
|
2016-07-17 04:37:37 +02:00
|
|
|
#include "profiles.h"
|
2008-12-11 02:32:24 +01:00
|
|
|
#include "dnxhdenc.h"
|
2007-10-08 13:27:18 +02:00
|
|
|
|
2016-04-27 19:45:23 +02:00
|
|
|
// The largest value that will not lead to overflow for 10-bit samples.
|
2014-03-28 02:02:48 +01:00
|
|
|
#define DNX10BIT_QMAT_SHIFT 18
|
|
|
|
#define RC_VARIANCE 1 // use variance or ssd for fast rc
|
|
|
|
#define LAMBDA_FRAC_BITS 10
|
2011-03-02 00:50:25 +01:00
|
|
|
|
2014-03-28 02:02:48 +01:00
|
|
|
#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
|
|
|
|
static const AVOption options[] = {
|
|
|
|
{ "nitris_compat", "encode with Avid Nitris compatibility",
|
2015-11-21 22:04:39 +01:00
|
|
|
offsetof(DNXHDEncContext, nitris_compat), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
|
2015-07-02 13:14:22 +02:00
|
|
|
{ "ibias", "intra quant bias",
|
|
|
|
offsetof(DNXHDEncContext, intra_quant_bias), AV_OPT_TYPE_INT,
|
2015-08-27 02:59:43 +02:00
|
|
|
{ .i64 = 0 }, INT_MIN, INT_MAX, VE },
|
2016-07-17 04:37:39 +02:00
|
|
|
{ "profile", NULL, offsetof(DNXHDEncContext, profile), AV_OPT_TYPE_INT,
|
|
|
|
{ .i64 = FF_PROFILE_DNXHD },
|
|
|
|
FF_PROFILE_DNXHD, FF_PROFILE_DNXHR_444, VE, "profile" },
|
|
|
|
{ "dnxhd", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_PROFILE_DNXHD },
|
|
|
|
0, 0, VE, "profile" },
|
|
|
|
{ "dnxhr_444", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_PROFILE_DNXHR_444 },
|
|
|
|
0, 0, VE, "profile" },
|
|
|
|
{ "dnxhr_hqx", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_PROFILE_DNXHR_HQX },
|
|
|
|
0, 0, VE, "profile" },
|
|
|
|
{ "dnxhr_hq", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_PROFILE_DNXHR_HQ },
|
|
|
|
0, 0, VE, "profile" },
|
|
|
|
{ "dnxhr_sq", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_PROFILE_DNXHR_SQ },
|
|
|
|
0, 0, VE, "profile" },
|
|
|
|
{ "dnxhr_lb", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FF_PROFILE_DNXHR_LB },
|
|
|
|
0, 0, VE, "profile" },
|
2014-03-28 02:02:48 +01:00
|
|
|
{ NULL }
|
2011-03-02 00:50:25 +01:00
|
|
|
};
|
2012-06-23 01:32:42 +02:00
|
|
|
|
2013-06-30 13:46:27 +02:00
|
|
|
static const AVClass dnxhd_class = {
|
2012-06-23 01:32:42 +02:00
|
|
|
.class_name = "dnxhd",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
2011-03-02 00:50:25 +01:00
|
|
|
|
2014-04-11 14:04:10 +02:00
|
|
|
static void dnxhd_8bit_get_pixels_8x4_sym(int16_t *av_restrict block,
|
2014-03-28 02:02:48 +01:00
|
|
|
const uint8_t *pixels,
|
2014-04-11 14:04:10 +02:00
|
|
|
ptrdiff_t line_size)
|
2008-12-12 01:12:59 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 4; i++) {
|
2014-03-28 02:02:48 +01:00
|
|
|
block[0] = pixels[0];
|
|
|
|
block[1] = pixels[1];
|
|
|
|
block[2] = pixels[2];
|
|
|
|
block[3] = pixels[3];
|
|
|
|
block[4] = pixels[4];
|
|
|
|
block[5] = pixels[5];
|
|
|
|
block[6] = pixels[6];
|
|
|
|
block[7] = pixels[7];
|
|
|
|
pixels += line_size;
|
|
|
|
block += 8;
|
2008-12-12 01:12:59 +01:00
|
|
|
}
|
2011-07-20 19:38:37 +02:00
|
|
|
memcpy(block, block - 8, sizeof(*block) * 8);
|
|
|
|
memcpy(block + 8, block - 16, sizeof(*block) * 8);
|
|
|
|
memcpy(block + 16, block - 24, sizeof(*block) * 8);
|
|
|
|
memcpy(block + 24, block - 32, sizeof(*block) * 8);
|
2008-12-12 01:12:59 +01:00
|
|
|
}
|
|
|
|
|
2014-03-28 02:02:48 +01:00
|
|
|
static av_always_inline
|
2014-04-11 14:04:10 +02:00
|
|
|
void dnxhd_10bit_get_pixels_8x4_sym(int16_t *av_restrict block,
|
2014-03-28 02:02:48 +01:00
|
|
|
const uint8_t *pixels,
|
2014-04-11 14:04:10 +02:00
|
|
|
ptrdiff_t line_size)
|
2011-07-20 19:58:27 +02:00
|
|
|
{
|
2015-09-30 01:50:02 +02:00
|
|
|
memcpy(block + 0 * 8, pixels + 0 * line_size, 8 * sizeof(*block));
|
|
|
|
memcpy(block + 7 * 8, pixels + 0 * line_size, 8 * sizeof(*block));
|
|
|
|
memcpy(block + 1 * 8, pixels + 1 * line_size, 8 * sizeof(*block));
|
|
|
|
memcpy(block + 6 * 8, pixels + 1 * line_size, 8 * sizeof(*block));
|
|
|
|
memcpy(block + 2 * 8, pixels + 2 * line_size, 8 * sizeof(*block));
|
|
|
|
memcpy(block + 5 * 8, pixels + 2 * line_size, 8 * sizeof(*block));
|
|
|
|
memcpy(block + 3 * 8, pixels + 3 * line_size, 8 * sizeof(*block));
|
|
|
|
memcpy(block + 4 * 8, pixels + 3 * line_size, 8 * sizeof(*block));
|
2011-07-20 19:58:27 +02:00
|
|
|
}
|
|
|
|
|
2017-03-25 20:19:26 +01:00
|
|
|
static int dnxhd_10bit_dct_quantize_444(MpegEncContext *ctx, int16_t *block,
|
|
|
|
int n, int qscale, int *overflow)
|
|
|
|
{
|
|
|
|
int i, j, level, last_non_zero, start_i;
|
|
|
|
const int *qmat;
|
|
|
|
const uint8_t *scantable= ctx->intra_scantable.scantable;
|
|
|
|
int bias;
|
|
|
|
int max = 0;
|
|
|
|
unsigned int threshold1, threshold2;
|
|
|
|
|
|
|
|
ctx->fdsp.fdct(block);
|
|
|
|
|
|
|
|
block[0] = (block[0] + 2) >> 2;
|
|
|
|
start_i = 1;
|
|
|
|
last_non_zero = 0;
|
|
|
|
qmat = n < 4 ? ctx->q_intra_matrix[qscale] : ctx->q_chroma_intra_matrix[qscale];
|
|
|
|
bias= ctx->intra_quant_bias * (1 << (16 - 8));
|
|
|
|
threshold1 = (1 << 16) - bias - 1;
|
|
|
|
threshold2 = (threshold1 << 1);
|
|
|
|
|
|
|
|
for (i = 63; i >= start_i; i--) {
|
|
|
|
j = scantable[i];
|
|
|
|
level = block[j] * qmat[j];
|
|
|
|
|
|
|
|
if (((unsigned)(level + threshold1)) > threshold2) {
|
|
|
|
last_non_zero = i;
|
|
|
|
break;
|
|
|
|
} else{
|
|
|
|
block[j]=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = start_i; i <= last_non_zero; i++) {
|
|
|
|
j = scantable[i];
|
|
|
|
level = block[j] * qmat[j];
|
|
|
|
|
|
|
|
if (((unsigned)(level + threshold1)) > threshold2) {
|
|
|
|
if (level > 0) {
|
|
|
|
level = (bias + level) >> 16;
|
|
|
|
block[j] = level;
|
|
|
|
} else{
|
|
|
|
level = (bias - level) >> 16;
|
|
|
|
block[j] = -level;
|
|
|
|
}
|
|
|
|
max |= level;
|
|
|
|
} else {
|
|
|
|
block[j] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*overflow = ctx->max_qcoeff < max; //overflow might have happened
|
|
|
|
|
|
|
|
/* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
|
|
|
|
if (ctx->idsp.perm_type != FF_IDCT_PERM_NONE)
|
|
|
|
ff_block_permute(block, ctx->idsp.idct_permutation,
|
|
|
|
scantable, last_non_zero);
|
|
|
|
|
|
|
|
return last_non_zero;
|
|
|
|
}
|
|
|
|
|
2013-01-20 01:02:29 +01:00
|
|
|
static int dnxhd_10bit_dct_quantize(MpegEncContext *ctx, int16_t *block,
|
2011-07-20 19:58:27 +02:00
|
|
|
int n, int qscale, int *overflow)
|
|
|
|
{
|
|
|
|
const uint8_t *scantable= ctx->intra_scantable.scantable;
|
2011-09-25 06:46:39 +02:00
|
|
|
const int *qmat = n<4 ? ctx->q_intra_matrix[qscale] : ctx->q_chroma_intra_matrix[qscale];
|
2011-07-20 19:58:27 +02:00
|
|
|
int last_non_zero = 0;
|
2011-07-27 17:14:33 +02:00
|
|
|
int i;
|
2011-07-20 19:58:27 +02:00
|
|
|
|
2014-02-03 19:09:45 +01:00
|
|
|
ctx->fdsp.fdct(block);
|
2011-07-20 19:58:27 +02:00
|
|
|
|
|
|
|
// Divide by 4 with rounding, to compensate scaling of DCT coefficients
|
|
|
|
block[0] = (block[0] + 2) >> 2;
|
|
|
|
|
2011-07-27 17:14:33 +02:00
|
|
|
for (i = 1; i < 64; ++i) {
|
2011-07-20 19:58:27 +02:00
|
|
|
int j = scantable[i];
|
2014-11-17 00:22:27 +01:00
|
|
|
int sign = FF_SIGNBIT(block[j]);
|
2011-07-20 19:58:27 +02:00
|
|
|
int level = (block[j] ^ sign) - sign;
|
|
|
|
level = level * qmat[j] >> DNX10BIT_QMAT_SHIFT;
|
|
|
|
block[j] = (level ^ sign) - sign;
|
|
|
|
if (level)
|
|
|
|
last_non_zero = i;
|
|
|
|
}
|
|
|
|
|
2015-10-12 19:37:42 +02:00
|
|
|
/* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
|
|
|
|
if (ctx->idsp.perm_type != FF_IDCT_PERM_NONE)
|
|
|
|
ff_block_permute(block, ctx->idsp.idct_permutation,
|
|
|
|
scantable, last_non_zero);
|
|
|
|
|
2011-07-20 19:58:27 +02:00
|
|
|
return last_non_zero;
|
|
|
|
}
|
|
|
|
|
2013-02-01 10:31:59 +01:00
|
|
|
static av_cold int dnxhd_init_vlc(DNXHDEncContext *ctx)
|
2007-10-08 13:27:18 +02:00
|
|
|
{
|
2008-06-02 05:07:32 +02:00
|
|
|
int i, j, level, run;
|
2017-03-25 20:19:26 +01:00
|
|
|
int max_level = 1 << (ctx->bit_depth + 2);
|
2014-03-28 02:02:48 +01:00
|
|
|
|
2020-06-01 15:49:12 +02:00
|
|
|
if (!FF_ALLOCZ_TYPED_ARRAY(ctx->orig_vlc_codes, max_level * 4) ||
|
|
|
|
!FF_ALLOCZ_TYPED_ARRAY(ctx->orig_vlc_bits, max_level * 4) ||
|
|
|
|
!(ctx->run_codes = av_mallocz(63 * 2)) ||
|
|
|
|
!(ctx->run_bits = av_mallocz(63)))
|
|
|
|
return AVERROR(ENOMEM);
|
2018-08-30 11:35:07 +02:00
|
|
|
ctx->vlc_codes = ctx->orig_vlc_codes + max_level * 2;
|
|
|
|
ctx->vlc_bits = ctx->orig_vlc_bits + max_level * 2;
|
2008-06-02 05:07:32 +02:00
|
|
|
for (level = -max_level; level < max_level; level++) {
|
|
|
|
for (run = 0; run < 2; run++) {
|
2019-09-28 04:26:06 +02:00
|
|
|
int index = level * (1 << 1) | run;
|
2008-06-02 05:07:32 +02:00
|
|
|
int sign, offset = 0, alevel = level;
|
|
|
|
|
|
|
|
MASK_ABS(sign, alevel);
|
|
|
|
if (alevel > 64) {
|
2014-03-28 02:02:48 +01:00
|
|
|
offset = (alevel - 1) >> 6;
|
|
|
|
alevel -= offset << 6;
|
2008-06-02 05:07:32 +02:00
|
|
|
}
|
|
|
|
for (j = 0; j < 257; j++) {
|
2015-10-14 19:20:49 +02:00
|
|
|
if (ctx->cid_table->ac_info[2*j+0] >> 1 == alevel &&
|
|
|
|
(!offset || (ctx->cid_table->ac_info[2*j+1] & 1) && offset) &&
|
|
|
|
(!run || (ctx->cid_table->ac_info[2*j+1] & 2) && run)) {
|
2012-06-01 12:14:03 +02:00
|
|
|
av_assert1(!ctx->vlc_codes[index]);
|
2008-06-02 05:07:32 +02:00
|
|
|
if (alevel) {
|
2014-03-28 02:02:48 +01:00
|
|
|
ctx->vlc_codes[index] =
|
|
|
|
(ctx->cid_table->ac_codes[j] << 1) | (sign & 1);
|
|
|
|
ctx->vlc_bits[index] = ctx->cid_table->ac_bits[j] + 1;
|
2008-06-02 05:07:32 +02:00
|
|
|
} else {
|
2008-06-02 05:12:43 +02:00
|
|
|
ctx->vlc_codes[index] = ctx->cid_table->ac_codes[j];
|
2014-03-28 02:02:48 +01:00
|
|
|
ctx->vlc_bits[index] = ctx->cid_table->ac_bits[j];
|
2008-06-02 05:07:32 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-06-01 12:14:03 +02:00
|
|
|
av_assert0(!alevel || j < 257);
|
2008-06-02 05:07:32 +02:00
|
|
|
if (offset) {
|
2014-03-28 02:02:48 +01:00
|
|
|
ctx->vlc_codes[index] =
|
|
|
|
(ctx->vlc_codes[index] << ctx->cid_table->index_bits) | offset;
|
|
|
|
ctx->vlc_bits[index] += ctx->cid_table->index_bits;
|
2008-06-02 05:07:32 +02:00
|
|
|
}
|
|
|
|
}
|
2007-10-08 13:27:18 +02:00
|
|
|
}
|
|
|
|
for (i = 0; i < 62; i++) {
|
|
|
|
int run = ctx->cid_table->run[i];
|
2012-06-01 12:14:03 +02:00
|
|
|
av_assert0(run < 63);
|
2008-06-02 05:12:43 +02:00
|
|
|
ctx->run_codes[run] = ctx->cid_table->run_codes[i];
|
2014-03-28 02:02:48 +01:00
|
|
|
ctx->run_bits[run] = ctx->cid_table->run_bits[i];
|
2007-10-08 13:27:18 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-02-01 10:31:59 +01:00
|
|
|
static av_cold int dnxhd_init_qmat(DNXHDEncContext *ctx, int lbias, int cbias)
|
2007-10-08 13:27:18 +02:00
|
|
|
{
|
|
|
|
// init first elem to 1 to avoid div by 0 in convert_matrix
|
2014-03-28 02:02:48 +01:00
|
|
|
uint16_t weight_matrix[64] = { 1, }; // convert_matrix needs uint16_t*
|
2007-10-08 13:27:18 +02:00
|
|
|
int qscale, i;
|
2011-07-20 19:58:27 +02:00
|
|
|
const uint8_t *luma_weight_table = ctx->cid_table->luma_weight;
|
|
|
|
const uint8_t *chroma_weight_table = ctx->cid_table->chroma_weight;
|
2007-10-08 13:27:18 +02:00
|
|
|
|
2020-06-01 15:49:12 +02:00
|
|
|
if (!FF_ALLOCZ_TYPED_ARRAY(ctx->qmatrix_l, ctx->m.avctx->qmax + 1) ||
|
|
|
|
!FF_ALLOCZ_TYPED_ARRAY(ctx->qmatrix_c, ctx->m.avctx->qmax + 1) ||
|
|
|
|
!FF_ALLOCZ_TYPED_ARRAY(ctx->qmatrix_l16, ctx->m.avctx->qmax + 1) ||
|
|
|
|
!FF_ALLOCZ_TYPED_ARRAY(ctx->qmatrix_c16, ctx->m.avctx->qmax + 1))
|
|
|
|
return AVERROR(ENOMEM);
|
2007-10-08 13:27:18 +02:00
|
|
|
|
2017-03-25 20:19:26 +01:00
|
|
|
if (ctx->bit_depth == 8) {
|
2011-07-20 19:58:27 +02:00
|
|
|
for (i = 1; i < 64; i++) {
|
2014-01-24 11:55:16 +01:00
|
|
|
int j = ctx->m.idsp.idct_permutation[ff_zigzag_direct[i]];
|
2011-07-20 19:58:27 +02:00
|
|
|
weight_matrix[j] = ctx->cid_table->luma_weight[i];
|
|
|
|
}
|
2014-02-07 10:05:46 +01:00
|
|
|
ff_convert_matrix(&ctx->m, ctx->qmatrix_l, ctx->qmatrix_l16,
|
2015-07-02 13:14:22 +02:00
|
|
|
weight_matrix, ctx->intra_quant_bias, 1,
|
2014-03-28 02:02:48 +01:00
|
|
|
ctx->m.avctx->qmax, 1);
|
2011-07-20 19:58:27 +02:00
|
|
|
for (i = 1; i < 64; i++) {
|
2014-01-24 11:55:16 +01:00
|
|
|
int j = ctx->m.idsp.idct_permutation[ff_zigzag_direct[i]];
|
2011-07-20 19:58:27 +02:00
|
|
|
weight_matrix[j] = ctx->cid_table->chroma_weight[i];
|
|
|
|
}
|
2014-02-07 10:05:46 +01:00
|
|
|
ff_convert_matrix(&ctx->m, ctx->qmatrix_c, ctx->qmatrix_c16,
|
2015-07-02 13:14:22 +02:00
|
|
|
weight_matrix, ctx->intra_quant_bias, 1,
|
2014-03-28 02:02:48 +01:00
|
|
|
ctx->m.avctx->qmax, 1);
|
2011-07-20 19:58:27 +02:00
|
|
|
|
|
|
|
for (qscale = 1; qscale <= ctx->m.avctx->qmax; qscale++) {
|
|
|
|
for (i = 0; i < 64; i++) {
|
2014-03-28 02:02:48 +01:00
|
|
|
ctx->qmatrix_l[qscale][i] <<= 2;
|
|
|
|
ctx->qmatrix_c[qscale][i] <<= 2;
|
|
|
|
ctx->qmatrix_l16[qscale][0][i] <<= 2;
|
|
|
|
ctx->qmatrix_l16[qscale][1][i] <<= 2;
|
|
|
|
ctx->qmatrix_c16[qscale][0][i] <<= 2;
|
|
|
|
ctx->qmatrix_c16[qscale][1][i] <<= 2;
|
2011-07-20 19:58:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// 10-bit
|
|
|
|
for (qscale = 1; qscale <= ctx->m.avctx->qmax; qscale++) {
|
|
|
|
for (i = 1; i < 64; i++) {
|
2015-10-12 19:37:42 +02:00
|
|
|
int j = ff_zigzag_direct[i];
|
2011-07-20 19:58:27 +02:00
|
|
|
|
2014-03-28 02:02:48 +01:00
|
|
|
/* The quantization formula from the VC-3 standard is:
|
|
|
|
* quantized = sign(block[i]) * floor(abs(block[i]/s) * p /
|
|
|
|
* (qscale * weight_table[i]))
|
|
|
|
* Where p is 32 for 8-bit samples and 8 for 10-bit ones.
|
|
|
|
* The s factor compensates scaling of DCT coefficients done by
|
|
|
|
* the DCT routines, and therefore is not present in standard.
|
|
|
|
* It's 8 for 8-bit samples and 4 for 10-bit ones.
|
|
|
|
* We want values of ctx->qtmatrix_l and ctx->qtmatrix_r to be:
|
|
|
|
* ((1 << DNX10BIT_QMAT_SHIFT) * (p / s)) /
|
|
|
|
* (qscale * weight_table[i])
|
|
|
|
* For 10-bit samples, p / s == 2 */
|
|
|
|
ctx->qmatrix_l[qscale][j] = (1 << (DNX10BIT_QMAT_SHIFT + 1)) /
|
|
|
|
(qscale * luma_weight_table[i]);
|
|
|
|
ctx->qmatrix_c[qscale][j] = (1 << (DNX10BIT_QMAT_SHIFT + 1)) /
|
|
|
|
(qscale * chroma_weight_table[i]);
|
2011-07-20 19:58:27 +02:00
|
|
|
}
|
2007-10-08 13:27:18 +02:00
|
|
|
}
|
|
|
|
}
|
2011-07-20 19:58:27 +02:00
|
|
|
|
2011-09-25 06:46:39 +02:00
|
|
|
ctx->m.q_chroma_intra_matrix16 = ctx->qmatrix_c16;
|
|
|
|
ctx->m.q_chroma_intra_matrix = ctx->qmatrix_c;
|
|
|
|
ctx->m.q_intra_matrix16 = ctx->qmatrix_l16;
|
|
|
|
ctx->m.q_intra_matrix = ctx->qmatrix_l;
|
|
|
|
|
2007-10-08 13:27:18 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-02-01 10:31:59 +01:00
|
|
|
static av_cold int dnxhd_init_rc(DNXHDEncContext *ctx)
|
2007-10-08 13:27:18 +02:00
|
|
|
{
|
2020-06-01 15:49:12 +02:00
|
|
|
if (!FF_ALLOCZ_TYPED_ARRAY(ctx->mb_rc, (ctx->m.avctx->qmax + 1) * ctx->m.mb_num))
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2016-10-21 13:29:46 +02:00
|
|
|
if (ctx->m.avctx->mb_decision != FF_MB_DECISION_RD) {
|
2020-06-01 15:49:12 +02:00
|
|
|
if (!FF_ALLOCZ_TYPED_ARRAY(ctx->mb_cmp, ctx->m.mb_num) ||
|
|
|
|
!FF_ALLOCZ_TYPED_ARRAY(ctx->mb_cmp_tmp, ctx->m.mb_num))
|
|
|
|
return AVERROR(ENOMEM);
|
2016-10-21 13:29:46 +02:00
|
|
|
}
|
2016-07-17 04:37:39 +02:00
|
|
|
ctx->frame_bits = (ctx->coding_unit_size -
|
|
|
|
ctx->data_offset - 4 - ctx->min_padding) * 8;
|
2007-10-08 13:27:18 +02:00
|
|
|
ctx->qscale = 1;
|
2014-03-28 02:02:48 +01:00
|
|
|
ctx->lambda = 2 << LAMBDA_FRAC_BITS; // qscale 2
|
2007-10-08 13:27:18 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-02-01 10:31:59 +01:00
|
|
|
static av_cold int dnxhd_encode_init(AVCodecContext *avctx)
|
2007-10-08 13:27:18 +02:00
|
|
|
{
|
|
|
|
DNXHDEncContext *ctx = avctx->priv_data;
|
2021-01-22 22:47:27 +01:00
|
|
|
int i, ret;
|
2011-07-20 19:58:27 +02:00
|
|
|
|
|
|
|
switch (avctx->pix_fmt) {
|
2012-10-06 12:10:34 +02:00
|
|
|
case AV_PIX_FMT_YUV422P:
|
2017-03-25 20:19:26 +01:00
|
|
|
ctx->bit_depth = 8;
|
2011-07-20 19:58:27 +02:00
|
|
|
break;
|
2012-10-06 12:10:34 +02:00
|
|
|
case AV_PIX_FMT_YUV422P10:
|
2017-03-25 20:19:26 +01:00
|
|
|
case AV_PIX_FMT_YUV444P10:
|
|
|
|
case AV_PIX_FMT_GBRP10:
|
|
|
|
ctx->bit_depth = 10;
|
2011-07-20 19:58:27 +02:00
|
|
|
break;
|
|
|
|
}
|
2007-10-08 13:27:18 +02:00
|
|
|
|
2017-03-25 20:19:26 +01:00
|
|
|
if ((ctx->profile == FF_PROFILE_DNXHR_444 && (avctx->pix_fmt != AV_PIX_FMT_YUV444P10 &&
|
|
|
|
avctx->pix_fmt != AV_PIX_FMT_GBRP10)) ||
|
|
|
|
(ctx->profile != FF_PROFILE_DNXHR_444 && (avctx->pix_fmt == AV_PIX_FMT_YUV444P10 ||
|
|
|
|
avctx->pix_fmt == AV_PIX_FMT_GBRP10))) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"pixel format is incompatible with DNxHD profile\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->profile == FF_PROFILE_DNXHR_HQX && avctx->pix_fmt != AV_PIX_FMT_YUV422P10) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"pixel format is incompatible with DNxHR HQX profile\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ctx->profile == FF_PROFILE_DNXHR_LB ||
|
|
|
|
ctx->profile == FF_PROFILE_DNXHR_SQ ||
|
|
|
|
ctx->profile == FF_PROFILE_DNXHR_HQ) && avctx->pix_fmt != AV_PIX_FMT_YUV422P) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"pixel format is incompatible with DNxHR LB/SQ/HQ profile\n");
|
|
|
|
return AVERROR(EINVAL);
|
2016-07-17 04:37:39 +02:00
|
|
|
}
|
|
|
|
|
2017-03-25 20:19:26 +01:00
|
|
|
ctx->is_444 = ctx->profile == FF_PROFILE_DNXHR_444;
|
2016-07-17 04:37:39 +02:00
|
|
|
avctx->profile = ctx->profile;
|
2017-03-25 20:19:26 +01:00
|
|
|
ctx->cid = ff_dnxhd_find_cid(avctx, ctx->bit_depth);
|
2011-07-20 19:58:27 +02:00
|
|
|
if (!ctx->cid) {
|
2014-03-28 02:02:48 +01:00
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
2014-04-11 14:04:10 +02:00
|
|
|
"video parameters incompatible with DNxHD. Valid DNxHD profiles:\n");
|
2013-01-26 12:46:01 +01:00
|
|
|
ff_dnxhd_print_profiles(avctx, AV_LOG_ERROR);
|
2014-01-18 23:05:31 +01:00
|
|
|
return AVERROR(EINVAL);
|
2007-10-08 13:27:18 +02:00
|
|
|
}
|
2007-12-18 14:52:07 +01:00
|
|
|
av_log(avctx, AV_LOG_DEBUG, "cid %d\n", ctx->cid);
|
2007-10-08 13:27:18 +02:00
|
|
|
|
2016-07-17 04:37:39 +02:00
|
|
|
if (ctx->cid >= 1270 && ctx->cid <= 1274)
|
|
|
|
avctx->codec_tag = MKTAG('A','V','d','h');
|
|
|
|
|
|
|
|
if (avctx->width < 256 || avctx->height < 120) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"Input dimensions too small, input must be at least 256x120\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2021-01-22 22:47:27 +01:00
|
|
|
ctx->cid_table = ff_dnxhd_get_cid_table(ctx->cid);
|
|
|
|
av_assert0(ctx->cid_table);
|
2007-10-08 13:27:18 +02:00
|
|
|
|
2014-03-28 02:02:48 +01:00
|
|
|
ctx->m.avctx = avctx;
|
2007-10-08 13:27:18 +02:00
|
|
|
ctx->m.mb_intra = 1;
|
|
|
|
ctx->m.h263_aic = 1;
|
|
|
|
|
2017-03-25 20:19:26 +01:00
|
|
|
avctx->bits_per_raw_sample = ctx->bit_depth;
|
2008-12-12 01:19:14 +01:00
|
|
|
|
2014-01-14 10:33:47 +01:00
|
|
|
ff_blockdsp_init(&ctx->bdsp, avctx);
|
2014-02-03 19:09:45 +01:00
|
|
|
ff_fdctdsp_init(&ctx->m.fdsp, avctx);
|
2014-08-08 17:11:20 +02:00
|
|
|
ff_mpv_idct_init(&ctx->m);
|
2013-12-30 19:56:07 +01:00
|
|
|
ff_mpegvideoencdsp_init(&ctx->m.mpvencdsp, avctx);
|
2014-02-03 23:29:09 +01:00
|
|
|
ff_pixblockdsp_init(&ctx->m.pdsp, avctx);
|
2012-09-25 18:05:02 +02:00
|
|
|
ff_dct_encode_init(&ctx->m);
|
|
|
|
|
2016-08-08 01:15:27 +02:00
|
|
|
if (ctx->profile != FF_PROFILE_DNXHD)
|
2017-03-25 20:19:26 +01:00
|
|
|
ff_videodsp_init(&ctx->m.vdsp, ctx->bit_depth);
|
2016-08-08 01:15:27 +02:00
|
|
|
|
2011-07-20 19:58:27 +02:00
|
|
|
if (!ctx->m.dct_quantize)
|
2012-02-15 10:40:48 +01:00
|
|
|
ctx->m.dct_quantize = ff_dct_quantize_c;
|
2011-07-20 19:58:27 +02:00
|
|
|
|
2017-03-25 20:19:26 +01:00
|
|
|
if (ctx->is_444 || ctx->profile == FF_PROFILE_DNXHR_HQX) {
|
|
|
|
ctx->m.dct_quantize = dnxhd_10bit_dct_quantize_444;
|
|
|
|
ctx->get_pixels_8x4_sym = dnxhd_10bit_get_pixels_8x4_sym;
|
|
|
|
ctx->block_width_l2 = 4;
|
|
|
|
} else if (ctx->bit_depth == 10) {
|
2014-03-28 02:02:48 +01:00
|
|
|
ctx->m.dct_quantize = dnxhd_10bit_dct_quantize;
|
|
|
|
ctx->get_pixels_8x4_sym = dnxhd_10bit_get_pixels_8x4_sym;
|
|
|
|
ctx->block_width_l2 = 4;
|
2011-07-20 19:58:27 +02:00
|
|
|
} else {
|
2014-03-28 02:02:48 +01:00
|
|
|
ctx->get_pixels_8x4_sym = dnxhd_8bit_get_pixels_8x4_sym;
|
|
|
|
ctx->block_width_l2 = 3;
|
2011-07-20 19:58:27 +02:00
|
|
|
}
|
|
|
|
|
2012-08-13 12:20:07 +02:00
|
|
|
if (ARCH_X86)
|
|
|
|
ff_dnxhdenc_init_x86(ctx);
|
2007-10-08 13:27:18 +02:00
|
|
|
|
|
|
|
ctx->m.mb_height = (avctx->height + 15) / 16;
|
|
|
|
ctx->m.mb_width = (avctx->width + 15) / 16;
|
|
|
|
|
2015-06-29 21:59:37 +02:00
|
|
|
if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
|
2014-03-28 02:02:48 +01:00
|
|
|
ctx->interlaced = 1;
|
2007-10-08 13:27:18 +02:00
|
|
|
ctx->m.mb_height /= 2;
|
|
|
|
}
|
|
|
|
|
2018-09-06 13:43:20 +02:00
|
|
|
if (ctx->interlaced && ctx->profile != FF_PROFILE_DNXHD) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"Interlaced encoding is not supported for DNxHR profiles.\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2007-10-08 13:27:18 +02:00
|
|
|
ctx->m.mb_num = ctx->m.mb_height * ctx->m.mb_width;
|
|
|
|
|
2016-07-17 04:37:39 +02:00
|
|
|
if (ctx->cid_table->frame_size == DNXHD_VARIABLE) {
|
2018-09-08 07:42:13 +02:00
|
|
|
ctx->frame_size = avpriv_dnxhd_get_hr_frame_size(ctx->cid,
|
2017-04-15 09:22:52 +02:00
|
|
|
avctx->width, avctx->height);
|
2017-06-14 16:58:35 +02:00
|
|
|
av_assert0(ctx->frame_size >= 0);
|
2016-07-17 04:37:39 +02:00
|
|
|
ctx->coding_unit_size = ctx->frame_size;
|
|
|
|
} else {
|
|
|
|
ctx->frame_size = ctx->cid_table->frame_size;
|
|
|
|
ctx->coding_unit_size = ctx->cid_table->coding_unit_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->m.mb_height > 68)
|
|
|
|
ctx->data_offset = 0x170 + (ctx->m.mb_height << 2);
|
|
|
|
else
|
|
|
|
ctx->data_offset = 0x280;
|
|
|
|
|
2014-03-28 02:02:48 +01:00
|
|
|
// XXX tune lbias/cbias
|
2015-07-02 13:14:22 +02:00
|
|
|
if ((ret = dnxhd_init_qmat(ctx, ctx->intra_quant_bias, 0)) < 0)
|
2014-01-18 23:05:31 +01:00
|
|
|
return ret;
|
2007-10-08 13:27:18 +02:00
|
|
|
|
2014-03-28 02:02:48 +01:00
|
|
|
/* Avid Nitris hardware decoder requires a minimum amount of padding
|
|
|
|
* in the coding unit payload */
|
2011-03-02 00:50:25 +01:00
|
|
|
if (ctx->nitris_compat)
|
|
|
|
ctx->min_padding = 1600;
|
|
|
|
|
2014-01-18 23:05:31 +01:00
|
|
|
if ((ret = dnxhd_init_vlc(ctx)) < 0)
|
|
|
|
return ret;
|
|
|
|
if ((ret = dnxhd_init_rc(ctx)) < 0)
|
|
|
|
return ret;
|
2007-10-08 13:27:18 +02:00
|
|
|
|
2020-06-01 15:49:12 +02:00
|
|
|
if (!FF_ALLOCZ_TYPED_ARRAY(ctx->slice_size, ctx->m.mb_height) ||
|
|
|
|
!FF_ALLOCZ_TYPED_ARRAY(ctx->slice_offs, ctx->m.mb_height) ||
|
|
|
|
!FF_ALLOCZ_TYPED_ARRAY(ctx->mb_bits, ctx->m.mb_num) ||
|
|
|
|
!FF_ALLOCZ_TYPED_ARRAY(ctx->mb_qscale, ctx->m.mb_num))
|
|
|
|
return AVERROR(ENOMEM);
|
2007-10-08 13:27:18 +02:00
|
|
|
|
2017-08-17 11:42:16 +02:00
|
|
|
if (avctx->active_thread_type == FF_THREAD_SLICE) {
|
|
|
|
if (avctx->thread_count > MAX_THREADS) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "too many threads\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2007-10-08 13:27:18 +02:00
|
|
|
}
|
|
|
|
|
2014-01-17 20:58:43 +01:00
|
|
|
if (avctx->qmax <= 1) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "qmax must be at least 2\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2007-10-08 13:27:18 +02:00
|
|
|
ctx->thread[0] = ctx;
|
2017-08-17 11:42:16 +02:00
|
|
|
if (avctx->active_thread_type == FF_THREAD_SLICE) {
|
|
|
|
for (i = 1; i < avctx->thread_count; i++) {
|
2021-05-06 02:01:38 +02:00
|
|
|
ctx->thread[i] = av_memdup(ctx, sizeof(DNXHDEncContext));
|
2019-09-23 05:36:18 +02:00
|
|
|
if (!ctx->thread[i])
|
2020-06-01 15:49:12 +02:00
|
|
|
return AVERROR(ENOMEM);
|
2017-08-17 11:42:16 +02:00
|
|
|
}
|
2007-10-08 13:27:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dnxhd_write_header(AVCodecContext *avctx, uint8_t *buf)
|
|
|
|
{
|
|
|
|
DNXHDEncContext *ctx = avctx->priv_data;
|
|
|
|
|
2016-07-17 04:37:39 +02:00
|
|
|
memset(buf, 0, ctx->data_offset);
|
|
|
|
|
|
|
|
// * write prefix */
|
|
|
|
AV_WB16(buf + 0x02, ctx->data_offset);
|
|
|
|
if (ctx->cid >= 1270 && ctx->cid <= 1274)
|
|
|
|
buf[4] = 0x03;
|
|
|
|
else
|
|
|
|
buf[4] = 0x01;
|
2009-09-18 21:03:05 +02:00
|
|
|
|
2014-03-28 02:02:48 +01:00
|
|
|
buf[5] = ctx->interlaced ? ctx->cur_field + 2 : 0x01;
|
2007-10-08 13:27:18 +02:00
|
|
|
buf[6] = 0x80; // crc flag off
|
|
|
|
buf[7] = 0xa0; // reserved
|
2014-03-28 02:02:48 +01:00
|
|
|
AV_WB16(buf + 0x18, avctx->height >> ctx->interlaced); // ALPF
|
2007-10-08 13:27:18 +02:00
|
|
|
AV_WB16(buf + 0x1a, avctx->width); // SPL
|
2014-03-28 02:02:48 +01:00
|
|
|
AV_WB16(buf + 0x1d, avctx->height >> ctx->interlaced); // NAL
|
2007-10-08 13:27:18 +02:00
|
|
|
|
2017-03-25 20:19:26 +01:00
|
|
|
buf[0x21] = ctx->bit_depth == 10 ? 0x58 : 0x38;
|
2014-03-28 02:02:48 +01:00
|
|
|
buf[0x22] = 0x88 + (ctx->interlaced << 2);
|
2007-10-08 13:27:18 +02:00
|
|
|
AV_WB32(buf + 0x28, ctx->cid); // CID
|
2017-03-25 20:19:26 +01:00
|
|
|
buf[0x2c] = (!ctx->interlaced << 7) | (ctx->is_444 << 6) | (avctx->pix_fmt == AV_PIX_FMT_YUV444P10);
|
2007-10-08 13:27:18 +02:00
|
|
|
|
|
|
|
buf[0x5f] = 0x01; // UDL
|
|
|
|
|
|
|
|
buf[0x167] = 0x02; // reserved
|
|
|
|
AV_WB16(buf + 0x16a, ctx->m.mb_height * 4 + 4); // MSIPS
|
2017-04-14 16:00:35 +02:00
|
|
|
AV_WB16(buf + 0x16c, ctx->m.mb_height); // Ns
|
2007-10-08 13:27:18 +02:00
|
|
|
buf[0x16f] = 0x10; // reserved
|
|
|
|
|
|
|
|
ctx->msip = buf + 0x170;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_always_inline void dnxhd_encode_dc(DNXHDEncContext *ctx, int diff)
|
|
|
|
{
|
|
|
|
int nbits;
|
|
|
|
if (diff < 0) {
|
2014-03-28 02:02:48 +01:00
|
|
|
nbits = av_log2_16bit(-2 * diff);
|
2007-10-08 13:27:18 +02:00
|
|
|
diff--;
|
|
|
|
} else {
|
2014-03-28 02:02:48 +01:00
|
|
|
nbits = av_log2_16bit(2 * diff);
|
2007-10-08 13:27:18 +02:00
|
|
|
}
|
|
|
|
put_bits(&ctx->m.pb, ctx->cid_table->dc_bits[nbits] + nbits,
|
2014-03-28 02:02:48 +01:00
|
|
|
(ctx->cid_table->dc_codes[nbits] << nbits) +
|
2015-04-22 03:28:21 +02:00
|
|
|
av_mod_uintp2(diff, nbits));
|
2007-10-08 13:27:18 +02:00
|
|
|
}
|
|
|
|
|
2014-03-28 02:02:48 +01:00
|
|
|
static av_always_inline
|
|
|
|
void dnxhd_encode_block(DNXHDEncContext *ctx, int16_t *block,
|
|
|
|
int last_index, int n)
|
2007-10-08 13:27:18 +02:00
|
|
|
{
|
|
|
|
int last_non_zero = 0;
|
|
|
|
int slevel, i, j;
|
|
|
|
|
|
|
|
dnxhd_encode_dc(ctx, block[0] - ctx->m.last_dc[n]);
|
|
|
|
ctx->m.last_dc[n] = block[0];
|
|
|
|
|
|
|
|
for (i = 1; i <= last_index; i++) {
|
|
|
|
j = ctx->m.intra_scantable.permutated[i];
|
|
|
|
slevel = block[j];
|
|
|
|
if (slevel) {
|
|
|
|
int run_level = i - last_non_zero - 1;
|
2019-09-28 04:26:06 +02:00
|
|
|
int rlevel = slevel * (1 << 1) | !!run_level;
|
2008-06-02 05:12:43 +02:00
|
|
|
put_bits(&ctx->m.pb, ctx->vlc_bits[rlevel], ctx->vlc_codes[rlevel]);
|
2007-10-08 13:27:18 +02:00
|
|
|
if (run_level)
|
2014-03-28 02:02:48 +01:00
|
|
|
put_bits(&ctx->m.pb, ctx->run_bits[run_level],
|
|
|
|
ctx->run_codes[run_level]);
|
2007-10-08 13:27:18 +02:00
|
|
|
last_non_zero = i;
|
|
|
|
}
|
|
|
|
}
|
2008-06-02 05:12:43 +02:00
|
|
|
put_bits(&ctx->m.pb, ctx->vlc_bits[0], ctx->vlc_codes[0]); // EOB
|
2007-10-08 13:27:18 +02:00
|
|
|
}
|
|
|
|
|
2014-03-28 02:02:48 +01:00
|
|
|
static av_always_inline
|
|
|
|
void dnxhd_unquantize_c(DNXHDEncContext *ctx, int16_t *block, int n,
|
|
|
|
int qscale, int last_index)
|
2007-10-08 13:27:18 +02:00
|
|
|
{
|
2007-10-08 14:28:36 +02:00
|
|
|
const uint8_t *weight_matrix;
|
2007-10-08 13:27:18 +02:00
|
|
|
int level;
|
|
|
|
int i;
|
|
|
|
|
2017-03-25 20:19:26 +01:00
|
|
|
if (ctx->is_444) {
|
|
|
|
weight_matrix = ((n % 6) < 2) ? ctx->cid_table->luma_weight
|
|
|
|
: ctx->cid_table->chroma_weight;
|
|
|
|
} else {
|
|
|
|
weight_matrix = (n & 2) ? ctx->cid_table->chroma_weight
|
|
|
|
: ctx->cid_table->luma_weight;
|
|
|
|
}
|
2007-10-08 13:27:18 +02:00
|
|
|
|
|
|
|
for (i = 1; i <= last_index; i++) {
|
|
|
|
int j = ctx->m.intra_scantable.permutated[i];
|
|
|
|
level = block[j];
|
|
|
|
if (level) {
|
|
|
|
if (level < 0) {
|
2014-03-28 02:02:48 +01:00
|
|
|
level = (1 - 2 * level) * qscale * weight_matrix[i];
|
2017-03-25 20:19:26 +01:00
|
|
|
if (ctx->bit_depth == 10) {
|
2011-07-20 19:58:27 +02:00
|
|
|
if (weight_matrix[i] != 8)
|
|
|
|
level += 8;
|
|
|
|
level >>= 4;
|
|
|
|
} else {
|
|
|
|
if (weight_matrix[i] != 32)
|
|
|
|
level += 32;
|
|
|
|
level >>= 6;
|
|
|
|
}
|
2007-10-08 13:27:18 +02:00
|
|
|
level = -level;
|
|
|
|
} else {
|
2014-03-28 02:02:48 +01:00
|
|
|
level = (2 * level + 1) * qscale * weight_matrix[i];
|
2017-03-25 20:19:26 +01:00
|
|
|
if (ctx->bit_depth == 10) {
|
2011-07-20 19:58:27 +02:00
|
|
|
if (weight_matrix[i] != 8)
|
|
|
|
level += 8;
|
|
|
|
level >>= 4;
|
|
|
|
} else {
|
|
|
|
if (weight_matrix[i] != 32)
|
|
|
|
level += 32;
|
|
|
|
level >>= 6;
|
|
|
|
}
|
2007-10-08 13:27:18 +02:00
|
|
|
}
|
|
|
|
block[j] = level;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-20 01:02:29 +01:00
|
|
|
static av_always_inline int dnxhd_ssd_block(int16_t *qblock, int16_t *block)
|
2007-10-08 13:27:18 +02:00
|
|
|
{
|
|
|
|
int score = 0;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 64; i++)
|
2011-07-20 19:38:37 +02:00
|
|
|
score += (block[i] - qblock[i]) * (block[i] - qblock[i]);
|
2007-10-08 13:27:18 +02:00
|
|
|
return score;
|
|
|
|
}
|
|
|
|
|
2014-03-28 02:02:48 +01:00
|
|
|
static av_always_inline
|
|
|
|
int dnxhd_calc_ac_bits(DNXHDEncContext *ctx, int16_t *block, int last_index)
|
2007-10-08 13:27:18 +02:00
|
|
|
{
|
|
|
|
int last_non_zero = 0;
|
|
|
|
int bits = 0;
|
|
|
|
int i, j, level;
|
|
|
|
for (i = 1; i <= last_index; i++) {
|
|
|
|
j = ctx->m.intra_scantable.permutated[i];
|
|
|
|
level = block[j];
|
|
|
|
if (level) {
|
|
|
|
int run_level = i - last_non_zero - 1;
|
2019-09-28 04:26:06 +02:00
|
|
|
bits += ctx->vlc_bits[level * (1 << 1) |
|
2014-03-28 02:02:48 +01:00
|
|
|
!!run_level] + ctx->run_bits[run_level];
|
2007-10-08 13:27:18 +02:00
|
|
|
last_non_zero = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bits;
|
|
|
|
}
|
|
|
|
|
2014-03-28 02:02:48 +01:00
|
|
|
static av_always_inline
|
|
|
|
void dnxhd_get_blocks(DNXHDEncContext *ctx, int mb_x, int mb_y)
|
2007-10-08 13:27:18 +02:00
|
|
|
{
|
2011-07-20 19:58:27 +02:00
|
|
|
const int bs = ctx->block_width_l2;
|
|
|
|
const int bw = 1 << bs;
|
2016-08-08 01:15:27 +02:00
|
|
|
int dct_y_offset = ctx->dct_y_offset;
|
|
|
|
int dct_uv_offset = ctx->dct_uv_offset;
|
|
|
|
int linesize = ctx->m.linesize;
|
|
|
|
int uvlinesize = ctx->m.uvlinesize;
|
2014-03-28 02:02:48 +01:00
|
|
|
const uint8_t *ptr_y = ctx->thread[0]->src[0] +
|
|
|
|
((mb_y << 4) * ctx->m.linesize) + (mb_x << bs + 1);
|
|
|
|
const uint8_t *ptr_u = ctx->thread[0]->src[1] +
|
2017-03-25 20:19:26 +01:00
|
|
|
((mb_y << 4) * ctx->m.uvlinesize) + (mb_x << bs + ctx->is_444);
|
2014-03-28 02:02:48 +01:00
|
|
|
const uint8_t *ptr_v = ctx->thread[0]->src[2] +
|
2017-03-25 20:19:26 +01:00
|
|
|
((mb_y << 4) * ctx->m.uvlinesize) + (mb_x << bs + ctx->is_444);
|
2014-02-03 23:29:09 +01:00
|
|
|
PixblockDSPContext *pdsp = &ctx->m.pdsp;
|
2016-08-08 01:15:27 +02:00
|
|
|
VideoDSPContext *vdsp = &ctx->m.vdsp;
|
|
|
|
|
2017-03-25 20:19:26 +01:00
|
|
|
if (ctx->bit_depth != 10 && vdsp->emulated_edge_mc && ((mb_x << 4) + 16 > ctx->m.avctx->width ||
|
|
|
|
(mb_y << 4) + 16 > ctx->m.avctx->height)) {
|
2016-08-08 01:15:27 +02:00
|
|
|
int y_w = ctx->m.avctx->width - (mb_x << 4);
|
|
|
|
int y_h = ctx->m.avctx->height - (mb_y << 4);
|
|
|
|
int uv_w = (y_w + 1) / 2;
|
|
|
|
int uv_h = y_h;
|
|
|
|
linesize = 16;
|
|
|
|
uvlinesize = 8;
|
|
|
|
|
|
|
|
vdsp->emulated_edge_mc(&ctx->edge_buf_y[0], ptr_y,
|
|
|
|
linesize, ctx->m.linesize,
|
|
|
|
linesize, 16,
|
|
|
|
0, 0, y_w, y_h);
|
|
|
|
vdsp->emulated_edge_mc(&ctx->edge_buf_uv[0][0], ptr_u,
|
|
|
|
uvlinesize, ctx->m.uvlinesize,
|
|
|
|
uvlinesize, 16,
|
|
|
|
0, 0, uv_w, uv_h);
|
|
|
|
vdsp->emulated_edge_mc(&ctx->edge_buf_uv[1][0], ptr_v,
|
|
|
|
uvlinesize, ctx->m.uvlinesize,
|
|
|
|
uvlinesize, 16,
|
|
|
|
0, 0, uv_w, uv_h);
|
|
|
|
|
2017-03-25 20:19:26 +01:00
|
|
|
dct_y_offset = bw * linesize;
|
|
|
|
dct_uv_offset = bw * uvlinesize;
|
|
|
|
ptr_y = &ctx->edge_buf_y[0];
|
|
|
|
ptr_u = &ctx->edge_buf_uv[0][0];
|
|
|
|
ptr_v = &ctx->edge_buf_uv[1][0];
|
2017-09-12 15:26:15 +02:00
|
|
|
} else if (ctx->bit_depth == 10 && vdsp->emulated_edge_mc && ((mb_x << 4) + 16 > ctx->m.avctx->width ||
|
|
|
|
(mb_y << 4) + 16 > ctx->m.avctx->height)) {
|
|
|
|
int y_w = ctx->m.avctx->width - (mb_x << 4);
|
|
|
|
int y_h = ctx->m.avctx->height - (mb_y << 4);
|
2017-03-25 20:19:26 +01:00
|
|
|
int uv_w = ctx->is_444 ? y_w : (y_w + 1) / 2;
|
|
|
|
int uv_h = y_h;
|
2017-09-12 15:26:15 +02:00
|
|
|
linesize = 32;
|
|
|
|
uvlinesize = 16 + 16 * ctx->is_444;
|
2017-03-25 20:19:26 +01:00
|
|
|
|
|
|
|
vdsp->emulated_edge_mc(&ctx->edge_buf_y[0], ptr_y,
|
|
|
|
linesize, ctx->m.linesize,
|
|
|
|
linesize / 2, 16,
|
|
|
|
0, 0, y_w, y_h);
|
|
|
|
vdsp->emulated_edge_mc(&ctx->edge_buf_uv[0][0], ptr_u,
|
|
|
|
uvlinesize, ctx->m.uvlinesize,
|
|
|
|
uvlinesize / 2, 16,
|
|
|
|
0, 0, uv_w, uv_h);
|
|
|
|
vdsp->emulated_edge_mc(&ctx->edge_buf_uv[1][0], ptr_v,
|
|
|
|
uvlinesize, ctx->m.uvlinesize,
|
|
|
|
uvlinesize / 2, 16,
|
|
|
|
0, 0, uv_w, uv_h);
|
|
|
|
|
2017-09-12 15:26:15 +02:00
|
|
|
dct_y_offset = bw * linesize / 2;
|
|
|
|
dct_uv_offset = bw * uvlinesize / 2;
|
2016-08-08 01:15:27 +02:00
|
|
|
ptr_y = &ctx->edge_buf_y[0];
|
|
|
|
ptr_u = &ctx->edge_buf_uv[0][0];
|
|
|
|
ptr_v = &ctx->edge_buf_uv[1][0];
|
|
|
|
}
|
2007-10-08 13:27:18 +02:00
|
|
|
|
2017-03-25 20:19:26 +01:00
|
|
|
if (!ctx->is_444) {
|
|
|
|
pdsp->get_pixels(ctx->blocks[0], ptr_y, linesize);
|
|
|
|
pdsp->get_pixels(ctx->blocks[1], ptr_y + bw, linesize);
|
|
|
|
pdsp->get_pixels(ctx->blocks[2], ptr_u, uvlinesize);
|
|
|
|
pdsp->get_pixels(ctx->blocks[3], ptr_v, uvlinesize);
|
|
|
|
|
|
|
|
if (mb_y + 1 == ctx->m.mb_height && ctx->m.avctx->height == 1080) {
|
|
|
|
if (ctx->interlaced) {
|
|
|
|
ctx->get_pixels_8x4_sym(ctx->blocks[4],
|
|
|
|
ptr_y + dct_y_offset,
|
|
|
|
linesize);
|
|
|
|
ctx->get_pixels_8x4_sym(ctx->blocks[5],
|
|
|
|
ptr_y + dct_y_offset + bw,
|
|
|
|
linesize);
|
|
|
|
ctx->get_pixels_8x4_sym(ctx->blocks[6],
|
|
|
|
ptr_u + dct_uv_offset,
|
|
|
|
uvlinesize);
|
|
|
|
ctx->get_pixels_8x4_sym(ctx->blocks[7],
|
|
|
|
ptr_v + dct_uv_offset,
|
|
|
|
uvlinesize);
|
|
|
|
} else {
|
|
|
|
ctx->bdsp.clear_block(ctx->blocks[4]);
|
|
|
|
ctx->bdsp.clear_block(ctx->blocks[5]);
|
|
|
|
ctx->bdsp.clear_block(ctx->blocks[6]);
|
|
|
|
ctx->bdsp.clear_block(ctx->blocks[7]);
|
|
|
|
}
|
2008-12-11 02:15:00 +01:00
|
|
|
} else {
|
2017-03-25 20:19:26 +01:00
|
|
|
pdsp->get_pixels(ctx->blocks[4],
|
|
|
|
ptr_y + dct_y_offset, linesize);
|
|
|
|
pdsp->get_pixels(ctx->blocks[5],
|
|
|
|
ptr_y + dct_y_offset + bw, linesize);
|
|
|
|
pdsp->get_pixels(ctx->blocks[6],
|
|
|
|
ptr_u + dct_uv_offset, uvlinesize);
|
|
|
|
pdsp->get_pixels(ctx->blocks[7],
|
|
|
|
ptr_v + dct_uv_offset, uvlinesize);
|
2008-12-11 02:15:00 +01:00
|
|
|
}
|
2007-10-08 13:27:18 +02:00
|
|
|
} else {
|
2017-03-25 20:19:26 +01:00
|
|
|
pdsp->get_pixels(ctx->blocks[0], ptr_y, linesize);
|
|
|
|
pdsp->get_pixels(ctx->blocks[1], ptr_y + bw, linesize);
|
|
|
|
pdsp->get_pixels(ctx->blocks[6], ptr_y + dct_y_offset, linesize);
|
|
|
|
pdsp->get_pixels(ctx->blocks[7], ptr_y + dct_y_offset + bw, linesize);
|
|
|
|
|
|
|
|
pdsp->get_pixels(ctx->blocks[2], ptr_u, uvlinesize);
|
|
|
|
pdsp->get_pixels(ctx->blocks[3], ptr_u + bw, uvlinesize);
|
|
|
|
pdsp->get_pixels(ctx->blocks[8], ptr_u + dct_uv_offset, uvlinesize);
|
|
|
|
pdsp->get_pixels(ctx->blocks[9], ptr_u + dct_uv_offset + bw, uvlinesize);
|
|
|
|
|
|
|
|
pdsp->get_pixels(ctx->blocks[4], ptr_v, uvlinesize);
|
|
|
|
pdsp->get_pixels(ctx->blocks[5], ptr_v + bw, uvlinesize);
|
|
|
|
pdsp->get_pixels(ctx->blocks[10], ptr_v + dct_uv_offset, uvlinesize);
|
|
|
|
pdsp->get_pixels(ctx->blocks[11], ptr_v + dct_uv_offset + bw, uvlinesize);
|
2007-10-08 13:27:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-28 02:02:48 +01:00
|
|
|
static av_always_inline
|
|
|
|
int dnxhd_switch_matrix(DNXHDEncContext *ctx, int i)
|
2007-10-08 13:27:18 +02:00
|
|
|
{
|
2017-03-25 20:19:26 +01:00
|
|
|
int x;
|
|
|
|
|
|
|
|
if (ctx->is_444) {
|
|
|
|
x = (i >> 1) % 3;
|
|
|
|
} else {
|
|
|
|
const static uint8_t component[8]={0,0,1,2,0,0,1,2};
|
|
|
|
x = component[i];
|
|
|
|
}
|
|
|
|
return x;
|
2007-10-08 13:27:18 +02:00
|
|
|
}
|
|
|
|
|
2014-03-28 02:02:48 +01:00
|
|
|
static int dnxhd_calc_bits_thread(AVCodecContext *avctx, void *arg,
|
|
|
|
int jobnr, int threadnr)
|
2007-10-08 13:27:18 +02:00
|
|
|
{
|
2009-10-12 16:43:57 +02:00
|
|
|
DNXHDEncContext *ctx = avctx->priv_data;
|
|
|
|
int mb_y = jobnr, mb_x;
|
|
|
|
int qscale = ctx->qscale;
|
2013-01-20 01:02:29 +01:00
|
|
|
LOCAL_ALIGNED_16(int16_t, block, [64]);
|
2009-10-12 16:43:57 +02:00
|
|
|
ctx = ctx->thread[threadnr];
|
2007-10-08 13:27:18 +02:00
|
|
|
|
2009-10-12 16:46:53 +02:00
|
|
|
ctx->m.last_dc[0] =
|
|
|
|
ctx->m.last_dc[1] =
|
2017-03-25 20:19:26 +01:00
|
|
|
ctx->m.last_dc[2] = 1 << (ctx->bit_depth + 2);
|
2009-10-12 16:46:53 +02:00
|
|
|
|
|
|
|
for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
|
|
|
|
unsigned mb = mb_y * ctx->m.mb_width + mb_x;
|
|
|
|
int ssd = 0;
|
|
|
|
int ac_bits = 0;
|
|
|
|
int dc_bits = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
dnxhd_get_blocks(ctx, mb_x, mb_y);
|
|
|
|
|
2017-03-25 20:19:26 +01:00
|
|
|
for (i = 0; i < 8 + 4 * ctx->is_444; i++) {
|
2013-01-20 01:02:29 +01:00
|
|
|
int16_t *src_block = ctx->blocks[i];
|
2009-10-12 16:46:53 +02:00
|
|
|
int overflow, nbits, diff, last_index;
|
|
|
|
int n = dnxhd_switch_matrix(ctx, i);
|
|
|
|
|
2014-03-28 02:02:48 +01:00
|
|
|
memcpy(block, src_block, 64 * sizeof(*block));
|
2017-03-25 20:19:26 +01:00
|
|
|
last_index = ctx->m.dct_quantize(&ctx->m, block,
|
|
|
|
ctx->is_444 ? 4 * (n > 0): 4 & (2*i),
|
2014-03-28 02:02:48 +01:00
|
|
|
qscale, &overflow);
|
|
|
|
ac_bits += dnxhd_calc_ac_bits(ctx, block, last_index);
|
2009-10-12 16:46:53 +02:00
|
|
|
|
|
|
|
diff = block[0] - ctx->m.last_dc[n];
|
2014-03-28 02:02:48 +01:00
|
|
|
if (diff < 0)
|
|
|
|
nbits = av_log2_16bit(-2 * diff);
|
|
|
|
else
|
|
|
|
nbits = av_log2_16bit(2 * diff);
|
2011-07-20 19:58:27 +02:00
|
|
|
|
2017-03-25 20:19:26 +01:00
|
|
|
av_assert1(nbits < ctx->bit_depth + 4);
|
2009-10-12 16:46:53 +02:00
|
|
|
dc_bits += ctx->cid_table->dc_bits[nbits] + nbits;
|
|
|
|
|
|
|
|
ctx->m.last_dc[n] = block[0];
|
|
|
|
|
|
|
|
if (avctx->mb_decision == FF_MB_DECISION_RD || !RC_VARIANCE) {
|
|
|
|
dnxhd_unquantize_c(ctx, block, i, qscale, last_index);
|
2014-01-24 11:55:16 +01:00
|
|
|
ctx->m.idsp.idct(block);
|
2009-10-12 16:46:53 +02:00
|
|
|
ssd += dnxhd_ssd_block(block, src_block);
|
2007-10-08 13:27:18 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-17 04:37:38 +02:00
|
|
|
ctx->mb_rc[(qscale * ctx->m.mb_num) + mb].ssd = ssd;
|
|
|
|
ctx->mb_rc[(qscale * ctx->m.mb_num) + mb].bits = ac_bits + dc_bits + 12 +
|
2017-03-25 20:19:26 +01:00
|
|
|
(1 + ctx->is_444) * 8 * ctx->vlc_bits[0];
|
2009-10-12 16:46:53 +02:00
|
|
|
}
|
2007-10-08 13:27:18 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-28 02:02:48 +01:00
|
|
|
static int dnxhd_encode_thread(AVCodecContext *avctx, void *arg,
|
|
|
|
int jobnr, int threadnr)
|
2007-10-08 13:27:18 +02:00
|
|
|
{
|
2009-10-12 16:43:57 +02:00
|
|
|
DNXHDEncContext *ctx = avctx->priv_data;
|
|
|
|
int mb_y = jobnr, mb_x;
|
|
|
|
ctx = ctx->thread[threadnr];
|
2016-07-17 04:37:39 +02:00
|
|
|
init_put_bits(&ctx->m.pb, (uint8_t *)arg + ctx->data_offset + ctx->slice_offs[jobnr],
|
2014-03-28 02:02:48 +01:00
|
|
|
ctx->slice_size[jobnr]);
|
2007-10-08 13:27:18 +02:00
|
|
|
|
2009-10-12 16:46:53 +02:00
|
|
|
ctx->m.last_dc[0] =
|
|
|
|
ctx->m.last_dc[1] =
|
2017-03-25 20:19:26 +01:00
|
|
|
ctx->m.last_dc[2] = 1 << (ctx->bit_depth + 2);
|
2009-10-12 16:46:53 +02:00
|
|
|
for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
|
|
|
|
unsigned mb = mb_y * ctx->m.mb_width + mb_x;
|
|
|
|
int qscale = ctx->mb_qscale[mb];
|
|
|
|
int i;
|
|
|
|
|
2017-03-25 20:19:26 +01:00
|
|
|
put_bits(&ctx->m.pb, 11, qscale);
|
|
|
|
put_bits(&ctx->m.pb, 1, avctx->pix_fmt == AV_PIX_FMT_YUV444P10);
|
2009-10-12 16:46:53 +02:00
|
|
|
|
|
|
|
dnxhd_get_blocks(ctx, mb_x, mb_y);
|
|
|
|
|
2017-03-25 20:19:26 +01:00
|
|
|
for (i = 0; i < 8 + 4 * ctx->is_444; i++) {
|
2013-01-20 01:02:29 +01:00
|
|
|
int16_t *block = ctx->blocks[i];
|
2012-01-03 13:18:49 +01:00
|
|
|
int overflow, n = dnxhd_switch_matrix(ctx, i);
|
2017-03-25 20:19:26 +01:00
|
|
|
int last_index = ctx->m.dct_quantize(&ctx->m, block,
|
|
|
|
ctx->is_444 ? (((i >> 1) % 3) < 1 ? 0 : 4): 4 & (2*i),
|
2012-01-03 13:18:49 +01:00
|
|
|
qscale, &overflow);
|
2020-03-13 14:06:01 +01:00
|
|
|
|
2009-10-12 16:46:53 +02:00
|
|
|
dnxhd_encode_block(ctx, block, last_index, n);
|
2007-10-08 13:27:18 +02:00
|
|
|
}
|
2009-10-12 16:46:53 +02:00
|
|
|
}
|
2014-03-28 02:02:48 +01:00
|
|
|
if (put_bits_count(&ctx->m.pb) & 31)
|
|
|
|
put_bits(&ctx->m.pb, 32 - (put_bits_count(&ctx->m.pb) & 31), 0);
|
2007-10-08 13:27:18 +02:00
|
|
|
flush_put_bits(&ctx->m.pb);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-10-12 16:43:57 +02:00
|
|
|
static void dnxhd_setup_threads_slices(DNXHDEncContext *ctx)
|
2007-10-08 13:27:18 +02:00
|
|
|
{
|
|
|
|
int mb_y, mb_x;
|
2009-10-12 16:43:57 +02:00
|
|
|
int offset = 0;
|
|
|
|
for (mb_y = 0; mb_y < ctx->m.mb_height; mb_y++) {
|
|
|
|
int thread_size;
|
|
|
|
ctx->slice_offs[mb_y] = offset;
|
2011-07-20 19:38:37 +02:00
|
|
|
ctx->slice_size[mb_y] = 0;
|
|
|
|
for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
|
|
|
|
unsigned mb = mb_y * ctx->m.mb_width + mb_x;
|
|
|
|
ctx->slice_size[mb_y] += ctx->mb_bits[mb];
|
|
|
|
}
|
2014-03-28 02:02:48 +01:00
|
|
|
ctx->slice_size[mb_y] = (ctx->slice_size[mb_y] + 31) & ~31;
|
2011-07-20 19:38:37 +02:00
|
|
|
ctx->slice_size[mb_y] >>= 3;
|
|
|
|
thread_size = ctx->slice_size[mb_y];
|
2007-10-08 13:27:18 +02:00
|
|
|
offset += thread_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-28 02:02:48 +01:00
|
|
|
static int dnxhd_mb_var_thread(AVCodecContext *avctx, void *arg,
|
|
|
|
int jobnr, int threadnr)
|
2007-10-08 13:27:18 +02:00
|
|
|
{
|
2009-10-12 16:43:57 +02:00
|
|
|
DNXHDEncContext *ctx = avctx->priv_data;
|
2013-01-22 11:55:54 +01:00
|
|
|
int mb_y = jobnr, mb_x, x, y;
|
|
|
|
int partial_last_row = (mb_y == ctx->m.mb_height - 1) &&
|
|
|
|
((avctx->height >> ctx->interlaced) & 0xF);
|
|
|
|
|
2009-10-12 16:43:57 +02:00
|
|
|
ctx = ctx->thread[threadnr];
|
2017-03-25 20:19:26 +01:00
|
|
|
if (ctx->bit_depth == 8) {
|
2014-03-28 02:02:48 +01:00
|
|
|
uint8_t *pix = ctx->thread[0]->src[0] + ((mb_y << 4) * ctx->m.linesize);
|
2011-07-20 19:58:27 +02:00
|
|
|
for (mb_x = 0; mb_x < ctx->m.mb_width; ++mb_x, pix += 16) {
|
2014-03-28 02:02:48 +01:00
|
|
|
unsigned mb = mb_y * ctx->m.mb_width + mb_x;
|
2013-01-22 11:55:54 +01:00
|
|
|
int sum;
|
|
|
|
int varc;
|
|
|
|
|
2016-07-17 04:37:39 +02:00
|
|
|
if (!partial_last_row && mb_x * 16 <= avctx->width - 16 && (avctx->width % 16) == 0) {
|
2013-12-30 19:56:07 +01:00
|
|
|
sum = ctx->m.mpvencdsp.pix_sum(pix, ctx->m.linesize);
|
|
|
|
varc = ctx->m.mpvencdsp.pix_norm1(pix, ctx->m.linesize);
|
2013-01-22 11:55:54 +01:00
|
|
|
} else {
|
|
|
|
int bw = FFMIN(avctx->width - 16 * mb_x, 16);
|
|
|
|
int bh = FFMIN((avctx->height >> ctx->interlaced) - 16 * mb_y, 16);
|
|
|
|
sum = varc = 0;
|
|
|
|
for (y = 0; y < bh; y++) {
|
|
|
|
for (x = 0; x < bw; x++) {
|
|
|
|
uint8_t val = pix[x + y * ctx->m.linesize];
|
|
|
|
sum += val;
|
|
|
|
varc += val * val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-28 02:02:48 +01:00
|
|
|
varc = (varc - (((unsigned) sum * sum) >> 8) + 128) >> 8;
|
2013-01-22 11:55:54 +01:00
|
|
|
|
2011-07-20 19:58:27 +02:00
|
|
|
ctx->mb_cmp[mb].value = varc;
|
2014-03-28 02:02:48 +01:00
|
|
|
ctx->mb_cmp[mb].mb = mb;
|
2011-07-20 19:58:27 +02:00
|
|
|
}
|
|
|
|
} else { // 10-bit
|
2016-03-22 15:33:28 +01:00
|
|
|
const int linesize = ctx->m.linesize >> 1;
|
2011-07-20 19:58:27 +02:00
|
|
|
for (mb_x = 0; mb_x < ctx->m.mb_width; ++mb_x) {
|
2014-03-28 02:02:48 +01:00
|
|
|
uint16_t *pix = (uint16_t *)ctx->thread[0]->src[0] +
|
|
|
|
((mb_y << 4) * linesize) + (mb_x << 4);
|
2011-07-20 19:58:27 +02:00
|
|
|
unsigned mb = mb_y * ctx->m.mb_width + mb_x;
|
|
|
|
int sum = 0;
|
|
|
|
int sqsum = 0;
|
2015-10-13 22:02:41 +02:00
|
|
|
int bw = FFMIN(avctx->width - 16 * mb_x, 16);
|
|
|
|
int bh = FFMIN((avctx->height >> ctx->interlaced) - 16 * mb_y, 16);
|
2011-07-20 19:58:27 +02:00
|
|
|
int mean, sqmean;
|
2011-07-27 17:14:33 +02:00
|
|
|
int i, j;
|
2011-07-20 19:58:27 +02:00
|
|
|
// Macroblocks are 16x16 pixels, unlike DCT blocks which are 8x8.
|
2015-10-13 22:02:41 +02:00
|
|
|
for (i = 0; i < bh; ++i) {
|
|
|
|
for (j = 0; j < bw; ++j) {
|
2011-07-20 19:58:27 +02:00
|
|
|
// Turn 16-bit pixels into 10-bit ones.
|
2016-03-22 15:33:28 +01:00
|
|
|
const int sample = (unsigned) pix[j] >> 6;
|
2014-03-28 02:02:48 +01:00
|
|
|
sum += sample;
|
2011-07-20 19:58:27 +02:00
|
|
|
sqsum += sample * sample;
|
|
|
|
// 2^10 * 2^10 * 16 * 16 = 2^28, which is less than INT_MAX
|
|
|
|
}
|
|
|
|
pix += linesize;
|
|
|
|
}
|
|
|
|
mean = sum >> 8; // 16*16 == 2^8
|
|
|
|
sqmean = sqsum >> 8;
|
|
|
|
ctx->mb_cmp[mb].value = sqmean - mean * mean;
|
2014-03-28 02:02:48 +01:00
|
|
|
ctx->mb_cmp[mb].mb = mb;
|
2011-07-20 19:58:27 +02:00
|
|
|
}
|
2009-10-12 16:46:53 +02:00
|
|
|
}
|
2007-10-08 13:27:18 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dnxhd_encode_rdo(AVCodecContext *avctx, DNXHDEncContext *ctx)
|
|
|
|
{
|
2007-10-09 16:15:03 +02:00
|
|
|
int lambda, up_step, down_step;
|
|
|
|
int last_lower = INT_MAX, last_higher = 0;
|
2007-10-08 13:27:18 +02:00
|
|
|
int x, y, q;
|
|
|
|
|
|
|
|
for (q = 1; q < avctx->qmax; q++) {
|
|
|
|
ctx->qscale = q;
|
2014-03-28 02:02:48 +01:00
|
|
|
avctx->execute2(avctx, dnxhd_calc_bits_thread,
|
|
|
|
NULL, NULL, ctx->m.mb_height);
|
2007-10-08 13:27:18 +02:00
|
|
|
}
|
2014-03-28 02:02:48 +01:00
|
|
|
up_step = down_step = 2 << LAMBDA_FRAC_BITS;
|
|
|
|
lambda = ctx->lambda;
|
2007-10-08 13:27:18 +02:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
int bits = 0;
|
2014-03-28 02:02:48 +01:00
|
|
|
int end = 0;
|
2007-10-09 16:15:03 +02:00
|
|
|
if (lambda == last_higher) {
|
2007-10-08 13:27:18 +02:00
|
|
|
lambda++;
|
2007-10-09 16:15:03 +02:00
|
|
|
end = 1; // need to set final qscales/bits
|
2007-10-08 13:27:18 +02:00
|
|
|
}
|
|
|
|
for (y = 0; y < ctx->m.mb_height; y++) {
|
|
|
|
for (x = 0; x < ctx->m.mb_width; x++) {
|
|
|
|
unsigned min = UINT_MAX;
|
|
|
|
int qscale = 1;
|
2014-03-28 02:02:48 +01:00
|
|
|
int mb = y * ctx->m.mb_width + x;
|
2016-07-17 04:37:38 +02:00
|
|
|
int rc = 0;
|
2007-10-08 13:27:18 +02:00
|
|
|
for (q = 1; q < avctx->qmax; q++) {
|
2016-07-17 04:37:38 +02:00
|
|
|
int i = (q*ctx->m.mb_num) + mb;
|
|
|
|
unsigned score = ctx->mb_rc[i].bits * lambda +
|
|
|
|
((unsigned) ctx->mb_rc[i].ssd << LAMBDA_FRAC_BITS);
|
2007-10-08 13:27:18 +02:00
|
|
|
if (score < min) {
|
2014-03-28 02:02:48 +01:00
|
|
|
min = score;
|
2007-10-08 13:27:18 +02:00
|
|
|
qscale = q;
|
2016-07-17 04:37:38 +02:00
|
|
|
rc = i;
|
2007-10-08 13:27:18 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-17 04:37:38 +02:00
|
|
|
bits += ctx->mb_rc[rc].bits;
|
2007-10-08 13:27:18 +02:00
|
|
|
ctx->mb_qscale[mb] = qscale;
|
2016-07-17 04:37:38 +02:00
|
|
|
ctx->mb_bits[mb] = ctx->mb_rc[rc].bits;
|
2007-10-08 13:27:18 +02:00
|
|
|
}
|
2014-03-28 02:02:48 +01:00
|
|
|
bits = (bits + 31) & ~31; // padding
|
2007-10-08 13:27:18 +02:00
|
|
|
if (bits > ctx->frame_bits)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (end) {
|
|
|
|
if (bits > ctx->frame_bits)
|
2014-01-18 23:05:31 +01:00
|
|
|
return AVERROR(EINVAL);
|
2007-10-08 13:27:18 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (bits < ctx->frame_bits) {
|
2007-10-09 16:15:03 +02:00
|
|
|
last_lower = FFMIN(lambda, last_lower);
|
|
|
|
if (last_higher != 0)
|
|
|
|
lambda = (lambda+last_higher)>>1;
|
|
|
|
else
|
|
|
|
lambda -= down_step;
|
2011-11-26 21:11:12 +01:00
|
|
|
down_step = FFMIN((int64_t)down_step*5, INT_MAX);
|
2007-10-09 16:15:03 +02:00
|
|
|
up_step = 1<<LAMBDA_FRAC_BITS;
|
|
|
|
lambda = FFMAX(1, lambda);
|
|
|
|
if (lambda == last_lower)
|
|
|
|
break;
|
2007-10-08 13:27:18 +02:00
|
|
|
} else {
|
2007-10-09 16:15:03 +02:00
|
|
|
last_higher = FFMAX(lambda, last_higher);
|
|
|
|
if (last_lower != INT_MAX)
|
|
|
|
lambda = (lambda+last_lower)>>1;
|
2009-11-20 01:13:34 +01:00
|
|
|
else if ((int64_t)lambda + up_step > INT_MAX)
|
2014-01-18 23:05:31 +01:00
|
|
|
return AVERROR(EINVAL);
|
2007-10-09 16:15:03 +02:00
|
|
|
else
|
|
|
|
lambda += up_step;
|
2009-11-20 01:13:34 +01:00
|
|
|
up_step = FFMIN((int64_t)up_step*5, INT_MAX);
|
2007-10-09 16:15:03 +02:00
|
|
|
down_step = 1<<LAMBDA_FRAC_BITS;
|
2007-10-08 13:27:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx->lambda = lambda;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dnxhd_find_qscale(DNXHDEncContext *ctx)
|
|
|
|
{
|
|
|
|
int bits = 0;
|
|
|
|
int up_step = 1;
|
|
|
|
int down_step = 1;
|
|
|
|
int last_higher = 0;
|
|
|
|
int last_lower = INT_MAX;
|
|
|
|
int qscale;
|
|
|
|
int x, y;
|
|
|
|
|
|
|
|
qscale = ctx->qscale;
|
|
|
|
for (;;) {
|
|
|
|
bits = 0;
|
|
|
|
ctx->qscale = qscale;
|
|
|
|
// XXX avoid recalculating bits
|
2014-03-28 02:02:48 +01:00
|
|
|
ctx->m.avctx->execute2(ctx->m.avctx, dnxhd_calc_bits_thread,
|
|
|
|
NULL, NULL, ctx->m.mb_height);
|
2007-10-08 13:27:18 +02:00
|
|
|
for (y = 0; y < ctx->m.mb_height; y++) {
|
|
|
|
for (x = 0; x < ctx->m.mb_width; x++)
|
2016-07-17 04:37:38 +02:00
|
|
|
bits += ctx->mb_rc[(qscale*ctx->m.mb_num) + (y*ctx->m.mb_width+x)].bits;
|
2007-10-08 13:27:18 +02:00
|
|
|
bits = (bits+31)&~31; // padding
|
|
|
|
if (bits > ctx->frame_bits)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (bits < ctx->frame_bits) {
|
|
|
|
if (qscale == 1)
|
2007-12-02 20:20:46 +01:00
|
|
|
return 1;
|
2007-10-08 13:27:18 +02:00
|
|
|
if (last_higher == qscale - 1) {
|
|
|
|
qscale = last_higher;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
last_lower = FFMIN(qscale, last_lower);
|
|
|
|
if (last_higher != 0)
|
2014-03-28 02:02:48 +01:00
|
|
|
qscale = (qscale + last_higher) >> 1;
|
2007-10-08 13:27:18 +02:00
|
|
|
else
|
|
|
|
qscale -= down_step++;
|
|
|
|
if (qscale < 1)
|
|
|
|
qscale = 1;
|
|
|
|
up_step = 1;
|
|
|
|
} else {
|
|
|
|
if (last_lower == qscale + 1)
|
|
|
|
break;
|
|
|
|
last_higher = FFMAX(qscale, last_higher);
|
|
|
|
if (last_lower != INT_MAX)
|
2014-03-28 02:02:48 +01:00
|
|
|
qscale = (qscale + last_lower) >> 1;
|
2007-10-08 13:27:18 +02:00
|
|
|
else
|
|
|
|
qscale += up_step++;
|
|
|
|
down_step = 1;
|
|
|
|
if (qscale >= ctx->m.avctx->qmax)
|
2014-01-18 23:05:31 +01:00
|
|
|
return AVERROR(EINVAL);
|
2007-10-08 13:27:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx->qscale = qscale;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-09-21 12:28:31 +02:00
|
|
|
#define BUCKET_BITS 8
|
|
|
|
#define RADIX_PASSES 4
|
|
|
|
#define NBUCKETS (1 << BUCKET_BITS)
|
|
|
|
|
|
|
|
static inline int get_bucket(int value, int shift)
|
|
|
|
{
|
|
|
|
value >>= shift;
|
2014-03-28 02:02:48 +01:00
|
|
|
value &= NBUCKETS - 1;
|
2009-09-21 12:28:31 +02:00
|
|
|
return NBUCKETS - 1 - value;
|
|
|
|
}
|
|
|
|
|
2014-03-28 02:02:48 +01:00
|
|
|
static void radix_count(const RCCMPEntry *data, int size,
|
|
|
|
int buckets[RADIX_PASSES][NBUCKETS])
|
2009-09-21 12:28:31 +02:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
memset(buckets, 0, sizeof(buckets[0][0]) * RADIX_PASSES * NBUCKETS);
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
int v = data[i].value;
|
|
|
|
for (j = 0; j < RADIX_PASSES; j++) {
|
|
|
|
buckets[j][get_bucket(v, 0)]++;
|
|
|
|
v >>= BUCKET_BITS;
|
|
|
|
}
|
2012-06-01 12:14:03 +02:00
|
|
|
av_assert1(!v);
|
2009-09-21 12:28:31 +02:00
|
|
|
}
|
|
|
|
for (j = 0; j < RADIX_PASSES; j++) {
|
|
|
|
int offset = size;
|
|
|
|
for (i = NBUCKETS - 1; i >= 0; i--)
|
|
|
|
buckets[j][i] = offset -= buckets[j][i];
|
2012-06-01 12:14:03 +02:00
|
|
|
av_assert1(!buckets[j][0]);
|
2009-09-21 12:28:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-28 02:02:48 +01:00
|
|
|
static void radix_sort_pass(RCCMPEntry *dst, const RCCMPEntry *data,
|
|
|
|
int size, int buckets[NBUCKETS], int pass)
|
2009-09-21 12:28:31 +02:00
|
|
|
{
|
|
|
|
int shift = pass * BUCKET_BITS;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < size; i++) {
|
2014-03-28 02:02:48 +01:00
|
|
|
int v = get_bucket(data[i].value, shift);
|
2009-09-21 12:28:31 +02:00
|
|
|
int pos = buckets[v]++;
|
|
|
|
dst[pos] = data[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-21 13:29:46 +02:00
|
|
|
static void radix_sort(RCCMPEntry *data, RCCMPEntry *tmp, int size)
|
2007-10-08 13:27:18 +02:00
|
|
|
{
|
2009-09-21 12:28:31 +02:00
|
|
|
int buckets[RADIX_PASSES][NBUCKETS];
|
|
|
|
radix_count(data, size, buckets);
|
|
|
|
radix_sort_pass(tmp, data, size, buckets[0], 0);
|
|
|
|
radix_sort_pass(data, tmp, size, buckets[1], 1);
|
|
|
|
if (buckets[2][NBUCKETS - 1] || buckets[3][NBUCKETS - 1]) {
|
|
|
|
radix_sort_pass(tmp, data, size, buckets[2], 2);
|
|
|
|
radix_sort_pass(data, tmp, size, buckets[3], 3);
|
|
|
|
}
|
2007-10-08 13:27:18 +02:00
|
|
|
}
|
|
|
|
|
2007-12-02 20:22:38 +01:00
|
|
|
static int dnxhd_encode_fast(AVCodecContext *avctx, DNXHDEncContext *ctx)
|
2007-10-08 13:27:18 +02:00
|
|
|
{
|
|
|
|
int max_bits = 0;
|
2007-12-02 20:20:46 +01:00
|
|
|
int ret, x, y;
|
|
|
|
if ((ret = dnxhd_find_qscale(ctx)) < 0)
|
2014-01-18 23:05:31 +01:00
|
|
|
return ret;
|
2007-10-08 13:27:18 +02:00
|
|
|
for (y = 0; y < ctx->m.mb_height; y++) {
|
|
|
|
for (x = 0; x < ctx->m.mb_width; x++) {
|
2014-03-28 02:02:48 +01:00
|
|
|
int mb = y * ctx->m.mb_width + x;
|
2016-07-17 04:37:38 +02:00
|
|
|
int rc = (ctx->qscale * ctx->m.mb_num ) + mb;
|
2007-10-08 13:27:18 +02:00
|
|
|
int delta_bits;
|
|
|
|
ctx->mb_qscale[mb] = ctx->qscale;
|
2016-07-17 04:37:38 +02:00
|
|
|
ctx->mb_bits[mb] = ctx->mb_rc[rc].bits;
|
|
|
|
max_bits += ctx->mb_rc[rc].bits;
|
2007-10-08 13:27:18 +02:00
|
|
|
if (!RC_VARIANCE) {
|
2016-07-17 04:37:38 +02:00
|
|
|
delta_bits = ctx->mb_rc[rc].bits -
|
|
|
|
ctx->mb_rc[rc + ctx->m.mb_num].bits;
|
2007-10-08 13:27:18 +02:00
|
|
|
ctx->mb_cmp[mb].mb = mb;
|
2014-03-28 02:02:48 +01:00
|
|
|
ctx->mb_cmp[mb].value =
|
2016-07-17 04:37:38 +02:00
|
|
|
delta_bits ? ((ctx->mb_rc[rc].ssd -
|
|
|
|
ctx->mb_rc[rc + ctx->m.mb_num].ssd) * 100) /
|
2014-03-28 02:02:48 +01:00
|
|
|
delta_bits
|
|
|
|
: INT_MIN; // avoid increasing qscale
|
2007-10-08 13:27:18 +02:00
|
|
|
}
|
|
|
|
}
|
2014-03-28 02:02:48 +01:00
|
|
|
max_bits += 31; // worst padding
|
2007-10-08 13:27:18 +02:00
|
|
|
}
|
2007-12-02 20:20:46 +01:00
|
|
|
if (!ret) {
|
2007-10-08 13:27:18 +02:00
|
|
|
if (RC_VARIANCE)
|
2014-03-28 02:02:48 +01:00
|
|
|
avctx->execute2(avctx, dnxhd_mb_var_thread,
|
|
|
|
NULL, NULL, ctx->m.mb_height);
|
2016-10-21 13:29:46 +02:00
|
|
|
radix_sort(ctx->mb_cmp, ctx->mb_cmp_tmp, ctx->m.mb_num);
|
2007-10-08 13:27:18 +02:00
|
|
|
for (x = 0; x < ctx->m.mb_num && max_bits > ctx->frame_bits; x++) {
|
|
|
|
int mb = ctx->mb_cmp[x].mb;
|
2016-07-17 04:37:38 +02:00
|
|
|
int rc = (ctx->qscale * ctx->m.mb_num ) + mb;
|
|
|
|
max_bits -= ctx->mb_rc[rc].bits -
|
|
|
|
ctx->mb_rc[rc + ctx->m.mb_num].bits;
|
2014-03-28 02:02:48 +01:00
|
|
|
ctx->mb_qscale[mb] = ctx->qscale + 1;
|
2016-07-17 04:37:38 +02:00
|
|
|
ctx->mb_bits[mb] = ctx->mb_rc[rc + ctx->m.mb_num].bits;
|
2007-10-08 13:27:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-01 04:26:31 +01:00
|
|
|
static void dnxhd_load_picture(DNXHDEncContext *ctx, const AVFrame *frame)
|
2007-10-08 13:27:18 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ctx->m.avctx->thread_count; i++) {
|
2013-11-09 10:14:46 +01:00
|
|
|
ctx->thread[i]->m.linesize = frame->linesize[0] << ctx->interlaced;
|
|
|
|
ctx->thread[i]->m.uvlinesize = frame->linesize[1] << ctx->interlaced;
|
2007-10-08 13:27:18 +02:00
|
|
|
ctx->thread[i]->dct_y_offset = ctx->m.linesize *8;
|
|
|
|
ctx->thread[i]->dct_uv_offset = ctx->m.uvlinesize*8;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->cur_field = frame->interlaced_frame && !frame->top_field_first;
|
|
|
|
}
|
|
|
|
|
2012-02-11 20:03:42 +01:00
|
|
|
static int dnxhd_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
|
|
|
|
const AVFrame *frame, int *got_packet)
|
2007-10-08 13:27:18 +02:00
|
|
|
{
|
|
|
|
DNXHDEncContext *ctx = avctx->priv_data;
|
|
|
|
int first_field = 1;
|
|
|
|
int offset, i, ret;
|
2015-07-22 21:01:56 +02:00
|
|
|
uint8_t *buf;
|
2007-10-08 13:27:18 +02:00
|
|
|
|
2021-04-25 01:43:26 +02:00
|
|
|
if ((ret = ff_get_encode_buffer(avctx, pkt, ctx->frame_size, 0)) < 0)
|
2012-02-11 20:03:42 +01:00
|
|
|
return ret;
|
|
|
|
buf = pkt->data;
|
2007-10-08 13:27:18 +02:00
|
|
|
|
2012-02-11 20:03:42 +01:00
|
|
|
dnxhd_load_picture(ctx, frame);
|
2007-10-08 13:27:18 +02:00
|
|
|
|
2014-03-28 02:02:48 +01:00
|
|
|
encode_coding_unit:
|
2007-10-08 13:27:18 +02:00
|
|
|
for (i = 0; i < 3; i++) {
|
2013-11-09 10:14:46 +01:00
|
|
|
ctx->src[i] = frame->data[i];
|
2007-10-08 13:27:18 +02:00
|
|
|
if (ctx->interlaced && ctx->cur_field)
|
2013-11-09 10:14:46 +01:00
|
|
|
ctx->src[i] += frame->linesize[i];
|
2007-10-08 13:27:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
dnxhd_write_header(avctx, buf);
|
|
|
|
|
|
|
|
if (avctx->mb_decision == FF_MB_DECISION_RD)
|
|
|
|
ret = dnxhd_encode_rdo(avctx, ctx);
|
|
|
|
else
|
2007-12-02 20:22:38 +01:00
|
|
|
ret = dnxhd_encode_fast(avctx, ctx);
|
2007-10-08 13:27:18 +02:00
|
|
|
if (ret < 0) {
|
2009-11-20 00:28:21 +01:00
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"picture could not fit ratecontrol constraints, increase qmax\n");
|
2014-01-18 23:05:31 +01:00
|
|
|
return ret;
|
2007-10-08 13:27:18 +02:00
|
|
|
}
|
|
|
|
|
2009-10-12 16:43:57 +02:00
|
|
|
dnxhd_setup_threads_slices(ctx);
|
2007-10-08 13:27:18 +02:00
|
|
|
|
|
|
|
offset = 0;
|
|
|
|
for (i = 0; i < ctx->m.mb_height; i++) {
|
|
|
|
AV_WB32(ctx->msip + i * 4, offset);
|
|
|
|
offset += ctx->slice_size[i];
|
2012-06-01 12:14:03 +02:00
|
|
|
av_assert1(!(ctx->slice_size[i] & 3));
|
2007-10-08 13:27:18 +02:00
|
|
|
}
|
|
|
|
|
2009-10-12 16:43:57 +02:00
|
|
|
avctx->execute2(avctx, dnxhd_encode_thread, buf, NULL, ctx->m.mb_height);
|
2007-10-08 13:27:18 +02:00
|
|
|
|
2016-07-17 04:37:39 +02:00
|
|
|
av_assert1(ctx->data_offset + offset + 4 <= ctx->coding_unit_size);
|
|
|
|
memset(buf + ctx->data_offset + offset, 0,
|
|
|
|
ctx->coding_unit_size - 4 - offset - ctx->data_offset);
|
2009-09-18 21:03:05 +02:00
|
|
|
|
2016-07-17 04:37:39 +02:00
|
|
|
AV_WB32(buf + ctx->coding_unit_size - 4, 0x600DC0DE); // EOF
|
2007-10-08 13:27:18 +02:00
|
|
|
|
|
|
|
if (ctx->interlaced && first_field) {
|
|
|
|
first_field = 0;
|
|
|
|
ctx->cur_field ^= 1;
|
2016-07-17 04:37:39 +02:00
|
|
|
buf += ctx->coding_unit_size;
|
2007-10-08 13:27:18 +02:00
|
|
|
goto encode_coding_unit;
|
|
|
|
}
|
|
|
|
|
2015-07-22 20:46:52 +02:00
|
|
|
ff_side_data_set_encoder_stats(pkt, ctx->qscale * FF_QP2LAMBDA, NULL, 0, AV_PICTURE_TYPE_I);
|
2015-07-15 19:41:21 +02:00
|
|
|
|
2012-02-11 20:03:42 +01:00
|
|
|
*got_packet = 1;
|
|
|
|
return 0;
|
2007-10-08 13:27:18 +02:00
|
|
|
}
|
|
|
|
|
2013-05-02 10:20:12 +02:00
|
|
|
static av_cold int dnxhd_encode_end(AVCodecContext *avctx)
|
2007-10-08 13:27:18 +02:00
|
|
|
{
|
|
|
|
DNXHDEncContext *ctx = avctx->priv_data;
|
|
|
|
int i;
|
|
|
|
|
2018-08-30 11:35:07 +02:00
|
|
|
av_freep(&ctx->orig_vlc_codes);
|
|
|
|
av_freep(&ctx->orig_vlc_bits);
|
2008-06-02 05:12:43 +02:00
|
|
|
av_freep(&ctx->run_codes);
|
|
|
|
av_freep(&ctx->run_bits);
|
2007-10-08 13:27:18 +02:00
|
|
|
|
|
|
|
av_freep(&ctx->mb_bits);
|
|
|
|
av_freep(&ctx->mb_qscale);
|
|
|
|
av_freep(&ctx->mb_rc);
|
|
|
|
av_freep(&ctx->mb_cmp);
|
2016-10-21 13:29:46 +02:00
|
|
|
av_freep(&ctx->mb_cmp_tmp);
|
2007-10-08 13:27:18 +02:00
|
|
|
av_freep(&ctx->slice_size);
|
2009-10-12 16:43:57 +02:00
|
|
|
av_freep(&ctx->slice_offs);
|
2007-10-08 13:27:18 +02:00
|
|
|
|
|
|
|
av_freep(&ctx->qmatrix_c);
|
|
|
|
av_freep(&ctx->qmatrix_l);
|
|
|
|
av_freep(&ctx->qmatrix_c16);
|
|
|
|
av_freep(&ctx->qmatrix_l16);
|
|
|
|
|
2021-05-06 01:47:57 +02:00
|
|
|
if (ctx->thread[1]) {
|
2018-06-01 10:54:39 +02:00
|
|
|
for (i = 1; i < avctx->thread_count; i++)
|
|
|
|
av_freep(&ctx->thread[i]);
|
|
|
|
}
|
2007-10-08 13:27:18 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-07 16:47:23 +01:00
|
|
|
static const AVCodecDefault dnxhd_defaults[] = {
|
|
|
|
{ "qmax", "1024" }, /* Maximum quantization scale factor allowed for VC-3 */
|
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
2021-02-25 10:50:26 +01:00
|
|
|
const AVCodec ff_dnxhd_encoder = {
|
2011-07-17 12:54:31 +02:00
|
|
|
.name = "dnxhd",
|
2013-10-03 22:57:53 +02:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("VC3/DNxHD"),
|
2011-07-17 12:54:31 +02:00
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2012-08-05 11:11:04 +02:00
|
|
|
.id = AV_CODEC_ID_DNXHD,
|
2021-04-25 01:43:26 +02:00
|
|
|
.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
|
|
|
|
AV_CODEC_CAP_SLICE_THREADS,
|
2011-07-17 12:54:31 +02:00
|
|
|
.priv_data_size = sizeof(DNXHDEncContext),
|
|
|
|
.init = dnxhd_encode_init,
|
2012-02-11 20:03:42 +01:00
|
|
|
.encode2 = dnxhd_encode_picture,
|
2011-07-17 12:54:31 +02:00
|
|
|
.close = dnxhd_encode_end,
|
2014-03-28 02:02:48 +01:00
|
|
|
.pix_fmts = (const enum AVPixelFormat[]) {
|
|
|
|
AV_PIX_FMT_YUV422P,
|
|
|
|
AV_PIX_FMT_YUV422P10,
|
2017-03-25 20:19:26 +01:00
|
|
|
AV_PIX_FMT_YUV444P10,
|
|
|
|
AV_PIX_FMT_GBRP10,
|
2014-03-28 02:02:48 +01:00
|
|
|
AV_PIX_FMT_NONE
|
|
|
|
},
|
2013-06-30 13:46:27 +02:00
|
|
|
.priv_class = &dnxhd_class,
|
2012-02-07 16:47:23 +01:00
|
|
|
.defaults = dnxhd_defaults,
|
2016-07-17 04:37:37 +02:00
|
|
|
.profiles = NULL_IF_CONFIG_SMALL(ff_dnxhd_profiles),
|
2021-05-06 03:37:37 +02:00
|
|
|
.caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
|
2007-10-08 13:27:18 +02:00
|
|
|
};
|