2013-10-02 14:46:26 +00:00
|
|
|
/*
|
|
|
|
* libx265 encoder
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013-2014 Derek Buitenhuis
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2014-06-11 19:51:52 +00:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#define X265_API_IMPORTS 1
|
|
|
|
#endif
|
|
|
|
|
2013-10-02 14:46:26 +00:00
|
|
|
#include <x265.h>
|
2015-02-17 22:04:53 +00:00
|
|
|
#include <float.h>
|
2013-10-02 14:46:26 +00:00
|
|
|
|
|
|
|
#include "libavutil/internal.h"
|
|
|
|
#include "libavutil/common.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavutil/pixdesc.h"
|
|
|
|
#include "avcodec.h"
|
2024-04-25 09:18:18 +00:00
|
|
|
#include "codec_internal.h"
|
|
|
|
#include "encode.h"
|
2013-10-02 14:46:26 +00:00
|
|
|
#include "internal.h"
|
2020-06-02 21:38:33 +00:00
|
|
|
#include "packet_internal.h"
|
2024-04-25 09:18:18 +00:00
|
|
|
#include "sei.h"
|
2013-10-02 14:46:26 +00:00
|
|
|
|
|
|
|
typedef struct libx265Context {
|
|
|
|
const AVClass *class;
|
|
|
|
|
|
|
|
x265_encoder *encoder;
|
|
|
|
x265_param *params;
|
2015-05-07 08:58:55 +00:00
|
|
|
const x265_api *api;
|
2013-10-02 14:46:26 +00:00
|
|
|
|
2015-02-17 22:04:53 +00:00
|
|
|
float crf;
|
2019-08-14 01:09:34 +00:00
|
|
|
int cqp;
|
2016-11-02 14:37:25 +00:00
|
|
|
int forced_idr;
|
2013-10-02 14:46:26 +00:00
|
|
|
char *preset;
|
|
|
|
char *tune;
|
2017-12-05 07:47:53 +00:00
|
|
|
char *profile;
|
2019-12-25 00:50:36 +00:00
|
|
|
AVDictionary *x265_opts;
|
2019-09-23 02:02:35 +00:00
|
|
|
|
2024-04-25 09:18:18 +00:00
|
|
|
void *sei_data;
|
|
|
|
int sei_data_size;
|
|
|
|
int udu_sei;
|
|
|
|
|
2019-09-23 02:02:35 +00:00
|
|
|
/**
|
|
|
|
* If the encoder does not support ROI then warn the first time we
|
|
|
|
* encounter a frame with ROI side data.
|
|
|
|
*/
|
|
|
|
int roi_warned;
|
2013-10-02 14:46:26 +00:00
|
|
|
} libx265Context;
|
|
|
|
|
|
|
|
static int is_keyframe(NalUnitType naltype)
|
|
|
|
{
|
|
|
|
switch (naltype) {
|
|
|
|
case NAL_UNIT_CODED_SLICE_BLA_W_LP:
|
|
|
|
case NAL_UNIT_CODED_SLICE_BLA_W_RADL:
|
|
|
|
case NAL_UNIT_CODED_SLICE_BLA_N_LP:
|
|
|
|
case NAL_UNIT_CODED_SLICE_IDR_W_RADL:
|
|
|
|
case NAL_UNIT_CODED_SLICE_IDR_N_LP:
|
|
|
|
case NAL_UNIT_CODED_SLICE_CRA:
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int libx265_encode_close(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
libx265Context *ctx = avctx->priv_data;
|
|
|
|
|
2015-05-07 08:58:55 +00:00
|
|
|
ctx->api->param_free(ctx->params);
|
2024-04-25 09:18:18 +00:00
|
|
|
av_freep(&ctx->sei_data);
|
2013-10-02 14:46:26 +00:00
|
|
|
|
|
|
|
if (ctx->encoder)
|
2015-05-07 08:58:55 +00:00
|
|
|
ctx->api->encoder_close(ctx->encoder);
|
2013-10-02 14:46:26 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-08-14 01:09:34 +00:00
|
|
|
static av_cold int libx265_param_parse_float(AVCodecContext *avctx,
|
|
|
|
const char *key, float value)
|
|
|
|
{
|
|
|
|
libx265Context *ctx = avctx->priv_data;
|
|
|
|
char buf[256];
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "%2.2f", value);
|
|
|
|
if (ctx->api->param_parse(ctx->params, key, buf) == X265_PARAM_BAD_VALUE) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid value %2.2f for param \"%s\".\n", value, key);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-08-14 01:07:08 +00:00
|
|
|
static av_cold int libx265_param_parse_int(AVCodecContext *avctx,
|
|
|
|
const char *key, int value)
|
|
|
|
{
|
|
|
|
libx265Context *ctx = avctx->priv_data;
|
|
|
|
char buf[256];
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "%d", value);
|
|
|
|
if (ctx->api->param_parse(ctx->params, key, buf) == X265_PARAM_BAD_VALUE) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid value %d for param \"%s\".\n", value, key);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-02 14:46:26 +00:00
|
|
|
static av_cold int libx265_encode_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
libx265Context *ctx = avctx->priv_data;
|
2019-02-23 00:35:28 +00:00
|
|
|
AVCPBProperties *cpb_props = NULL;
|
2024-04-25 09:18:18 +00:00
|
|
|
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
|
2019-08-14 01:07:08 +00:00
|
|
|
int ret;
|
2013-10-02 14:46:26 +00:00
|
|
|
|
2024-04-25 09:18:18 +00:00
|
|
|
ctx->api = x265_api_get(desc->comp[0].depth);
|
2015-05-07 08:58:55 +00:00
|
|
|
if (!ctx->api)
|
|
|
|
ctx->api = x265_api_get(0);
|
|
|
|
|
|
|
|
ctx->params = ctx->api->param_alloc();
|
2013-10-02 14:46:26 +00:00
|
|
|
if (!ctx->params) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Could not allocate x265 param structure.\n");
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
2015-05-07 08:58:55 +00:00
|
|
|
if (ctx->api->param_default_preset(ctx->params, ctx->preset, ctx->tune) < 0) {
|
2015-04-10 19:47:03 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Error setting preset/tune %s/%s.\n", ctx->preset, ctx->tune);
|
|
|
|
av_log(avctx, AV_LOG_INFO, "Possible presets:");
|
|
|
|
for (i = 0; x265_preset_names[i]; i++)
|
|
|
|
av_log(avctx, AV_LOG_INFO, " %s", x265_preset_names[i]);
|
|
|
|
|
|
|
|
av_log(avctx, AV_LOG_INFO, "\n");
|
|
|
|
av_log(avctx, AV_LOG_INFO, "Possible tunes:");
|
|
|
|
for (i = 0; x265_tune_names[i]; i++)
|
|
|
|
av_log(avctx, AV_LOG_INFO, " %s", x265_tune_names[i]);
|
|
|
|
|
|
|
|
av_log(avctx, AV_LOG_INFO, "\n");
|
|
|
|
|
2013-10-02 14:46:26 +00:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->params->frameNumThreads = avctx->thread_count;
|
2019-04-27 10:52:57 +00:00
|
|
|
if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
|
|
|
|
ctx->params->fpsNum = avctx->framerate.num;
|
|
|
|
ctx->params->fpsDenom = avctx->framerate.den;
|
|
|
|
} else {
|
|
|
|
ctx->params->fpsNum = avctx->time_base.den;
|
|
|
|
ctx->params->fpsDenom = avctx->time_base.num * avctx->ticks_per_frame;
|
|
|
|
}
|
2013-10-02 14:46:26 +00:00
|
|
|
ctx->params->sourceWidth = avctx->width;
|
|
|
|
ctx->params->sourceHeight = avctx->height;
|
2015-06-29 19:59:37 +00:00
|
|
|
ctx->params->bEnablePsnr = !!(avctx->flags & AV_CODEC_FLAG_PSNR);
|
2018-06-01 10:44:02 +00:00
|
|
|
ctx->params->bOpenGOP = !(avctx->flags & AV_CODEC_FLAG_CLOSED_GOP);
|
2014-02-21 16:26:40 +00:00
|
|
|
|
2018-03-16 15:34:25 +00:00
|
|
|
/* Tune the CTU size based on input resolution. */
|
|
|
|
if (ctx->params->sourceWidth < 64 || ctx->params->sourceHeight < 64)
|
|
|
|
ctx->params->maxCUSize = 32;
|
|
|
|
if (ctx->params->sourceWidth < 32 || ctx->params->sourceHeight < 32)
|
|
|
|
ctx->params->maxCUSize = 16;
|
|
|
|
if (ctx->params->sourceWidth < 16 || ctx->params->sourceHeight < 16) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Image size is too small (%dx%d).\n",
|
|
|
|
ctx->params->sourceWidth, ctx->params->sourceHeight);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2019-05-24 15:15:11 +00:00
|
|
|
|
|
|
|
ctx->params->vui.bEnableVideoSignalTypePresentFlag = 1;
|
|
|
|
|
2024-04-25 09:18:18 +00:00
|
|
|
if (avctx->color_range != AVCOL_RANGE_UNSPECIFIED)
|
|
|
|
ctx->params->vui.bEnableVideoFullRangeFlag =
|
|
|
|
avctx->color_range == AVCOL_RANGE_JPEG;
|
|
|
|
else
|
|
|
|
ctx->params->vui.bEnableVideoFullRangeFlag =
|
|
|
|
(desc->flags & AV_PIX_FMT_FLAG_RGB) ||
|
|
|
|
avctx->pix_fmt == AV_PIX_FMT_YUVJ420P ||
|
|
|
|
avctx->pix_fmt == AV_PIX_FMT_YUVJ422P ||
|
|
|
|
avctx->pix_fmt == AV_PIX_FMT_YUVJ444P;
|
2019-05-24 15:15:11 +00:00
|
|
|
|
2017-12-18 12:18:57 +00:00
|
|
|
if ((avctx->color_primaries <= AVCOL_PRI_SMPTE432 &&
|
2015-04-13 08:45:51 +00:00
|
|
|
avctx->color_primaries != AVCOL_PRI_UNSPECIFIED) ||
|
2017-12-18 12:18:57 +00:00
|
|
|
(avctx->color_trc <= AVCOL_TRC_ARIB_STD_B67 &&
|
2015-04-13 08:45:51 +00:00
|
|
|
avctx->color_trc != AVCOL_TRC_UNSPECIFIED) ||
|
2017-12-18 12:18:57 +00:00
|
|
|
(avctx->colorspace <= AVCOL_SPC_ICTCP &&
|
2015-04-13 08:45:51 +00:00
|
|
|
avctx->colorspace != AVCOL_SPC_UNSPECIFIED)) {
|
|
|
|
|
|
|
|
ctx->params->vui.bEnableColorDescriptionPresentFlag = 1;
|
|
|
|
|
|
|
|
// x265 validates the parameters internally
|
|
|
|
ctx->params->vui.colorPrimaries = avctx->color_primaries;
|
|
|
|
ctx->params->vui.transferCharacteristics = avctx->color_trc;
|
2019-12-04 14:24:09 +00:00
|
|
|
#if X265_BUILD >= 159
|
|
|
|
if (avctx->color_trc == AVCOL_TRC_ARIB_STD_B67)
|
|
|
|
ctx->params->preferredTransferCharacteristics = ctx->params->vui.transferCharacteristics;
|
|
|
|
#endif
|
2015-04-13 08:45:51 +00:00
|
|
|
ctx->params->vui.matrixCoeffs = avctx->colorspace;
|
|
|
|
}
|
|
|
|
|
2024-04-25 09:18:18 +00:00
|
|
|
// chroma sample location values are to be ignored in case of non-4:2:0
|
|
|
|
// according to the specification, so we only write them out in case of
|
|
|
|
// 4:2:0 (log2_chroma_{w,h} == 1).
|
|
|
|
ctx->params->vui.bEnableChromaLocInfoPresentFlag =
|
|
|
|
avctx->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED &&
|
|
|
|
desc->log2_chroma_w == 1 && desc->log2_chroma_h == 1;
|
|
|
|
|
|
|
|
if (ctx->params->vui.bEnableChromaLocInfoPresentFlag) {
|
|
|
|
ctx->params->vui.chromaSampleLocTypeTopField =
|
|
|
|
ctx->params->vui.chromaSampleLocTypeBottomField =
|
|
|
|
avctx->chroma_sample_location - 1;
|
|
|
|
}
|
|
|
|
|
2014-04-10 12:27:41 +00:00
|
|
|
if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
|
2015-02-17 22:04:51 +00:00
|
|
|
char sar[12];
|
|
|
|
int sar_num, sar_den;
|
|
|
|
|
2014-04-10 12:27:41 +00:00
|
|
|
av_reduce(&sar_num, &sar_den,
|
|
|
|
avctx->sample_aspect_ratio.num,
|
|
|
|
avctx->sample_aspect_ratio.den, 65535);
|
|
|
|
snprintf(sar, sizeof(sar), "%d:%d", sar_num, sar_den);
|
2015-05-07 08:58:55 +00:00
|
|
|
if (ctx->api->param_parse(ctx->params, "sar", sar) == X265_PARAM_BAD_VALUE) {
|
2014-04-10 12:27:41 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid SAR: %d:%d.\n", sar_num, sar_den);
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2014-04-08 17:44:53 +00:00
|
|
|
}
|
2014-02-21 17:05:50 +00:00
|
|
|
|
2024-04-25 09:18:18 +00:00
|
|
|
switch (desc->log2_chroma_w) {
|
|
|
|
// 4:4:4, RGB. gray
|
|
|
|
case 0:
|
|
|
|
// gray
|
|
|
|
if (desc->nb_components == 1) {
|
|
|
|
if (ctx->api->api_build_number < 85) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"libx265 version is %d, must be at least 85 for gray encoding.\n",
|
|
|
|
ctx->api->api_build_number);
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
ctx->params->internalCsp = X265_CSP_I400;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set identity matrix for RGB
|
|
|
|
if (desc->flags & AV_PIX_FMT_FLAG_RGB) {
|
|
|
|
ctx->params->vui.matrixCoeffs = AVCOL_SPC_RGB;
|
|
|
|
ctx->params->vui.bEnableVideoSignalTypePresentFlag = 1;
|
|
|
|
ctx->params->vui.bEnableColorDescriptionPresentFlag = 1;
|
|
|
|
}
|
|
|
|
|
2014-02-21 16:59:13 +00:00
|
|
|
ctx->params->internalCsp = X265_CSP_I444;
|
|
|
|
break;
|
2024-04-25 09:18:18 +00:00
|
|
|
// 4:2:0, 4:2:2
|
|
|
|
case 1:
|
|
|
|
ctx->params->internalCsp = desc->log2_chroma_h == 1 ?
|
|
|
|
X265_CSP_I420 : X265_CSP_I422;
|
2016-07-06 16:56:19 +00:00
|
|
|
break;
|
2024-04-25 09:18:18 +00:00
|
|
|
default:
|
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"Pixel format '%s' cannot be mapped to a libx265 CSP!\n",
|
|
|
|
desc->name);
|
|
|
|
return AVERROR_BUG;
|
2014-02-21 16:59:13 +00:00
|
|
|
}
|
|
|
|
|
2015-02-17 22:04:53 +00:00
|
|
|
if (ctx->crf >= 0) {
|
|
|
|
char crf[6];
|
|
|
|
|
|
|
|
snprintf(crf, sizeof(crf), "%2.2f", ctx->crf);
|
2015-05-07 08:58:55 +00:00
|
|
|
if (ctx->api->param_parse(ctx->params, "crf", crf) == X265_PARAM_BAD_VALUE) {
|
2015-02-17 22:04:53 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid crf: %2.2f.\n", ctx->crf);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
} else if (avctx->bit_rate > 0) {
|
2013-10-02 14:46:26 +00:00
|
|
|
ctx->params->rc.bitrate = avctx->bit_rate / 1000;
|
|
|
|
ctx->params->rc.rateControlMode = X265_RC_ABR;
|
2019-08-14 01:09:34 +00:00
|
|
|
} else if (ctx->cqp >= 0) {
|
|
|
|
ret = libx265_param_parse_int(avctx, "qp", ctx->cqp);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if X265_BUILD >= 89
|
|
|
|
if (avctx->qmin >= 0) {
|
|
|
|
ret = libx265_param_parse_int(avctx, "qpmin", avctx->qmin);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if (avctx->qmax >= 0) {
|
|
|
|
ret = libx265_param_parse_int(avctx, "qpmax", avctx->qmax);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (avctx->max_qdiff >= 0) {
|
|
|
|
ret = libx265_param_parse_int(avctx, "qpstep", avctx->max_qdiff);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if (avctx->qblur >= 0) {
|
|
|
|
ret = libx265_param_parse_float(avctx, "qblur", avctx->qblur);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if (avctx->qcompress >= 0) {
|
|
|
|
ret = libx265_param_parse_float(avctx, "qcomp", avctx->qcompress);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if (avctx->i_quant_factor >= 0) {
|
|
|
|
ret = libx265_param_parse_float(avctx, "ipratio", avctx->i_quant_factor);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if (avctx->b_quant_factor >= 0) {
|
|
|
|
ret = libx265_param_parse_float(avctx, "pbratio", avctx->b_quant_factor);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2013-10-02 14:46:26 +00:00
|
|
|
}
|
|
|
|
|
2018-06-18 06:39:54 +00:00
|
|
|
ctx->params->rc.vbvBufferSize = avctx->rc_buffer_size / 1000;
|
|
|
|
ctx->params->rc.vbvMaxBitrate = avctx->rc_max_rate / 1000;
|
|
|
|
|
2019-02-23 00:35:28 +00:00
|
|
|
cpb_props = ff_add_cpb_side_data(avctx);
|
|
|
|
if (!cpb_props)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
cpb_props->buffer_size = ctx->params->rc.vbvBufferSize * 1000;
|
2022-05-23 03:23:35 +00:00
|
|
|
cpb_props->max_bitrate = ctx->params->rc.vbvMaxBitrate * 1000LL;
|
|
|
|
cpb_props->avg_bitrate = ctx->params->rc.bitrate * 1000LL;
|
2019-02-23 00:35:28 +00:00
|
|
|
|
2015-06-29 19:59:37 +00:00
|
|
|
if (!(avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER))
|
2014-03-25 20:51:21 +00:00
|
|
|
ctx->params->bRepeatHeaders = 1;
|
|
|
|
|
2019-08-14 01:07:08 +00:00
|
|
|
if (avctx->gop_size >= 0) {
|
|
|
|
ret = libx265_param_parse_int(avctx, "keyint", avctx->gop_size);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if (avctx->keyint_min > 0) {
|
|
|
|
ret = libx265_param_parse_int(avctx, "min-keyint", avctx->keyint_min);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if (avctx->max_b_frames >= 0) {
|
|
|
|
ret = libx265_param_parse_int(avctx, "bframes", avctx->max_b_frames);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if (avctx->refs >= 0) {
|
|
|
|
ret = libx265_param_parse_int(avctx, "ref", avctx->refs);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-12-25 00:50:36 +00:00
|
|
|
{
|
2013-10-02 14:46:26 +00:00
|
|
|
AVDictionaryEntry *en = NULL;
|
2019-12-25 00:50:36 +00:00
|
|
|
while ((en = av_dict_get(ctx->x265_opts, "", en, AV_DICT_IGNORE_SUFFIX))) {
|
|
|
|
int parse_ret = ctx->api->param_parse(ctx->params, en->key, en->value);
|
|
|
|
|
|
|
|
switch (parse_ret) {
|
|
|
|
case X265_PARAM_BAD_NAME:
|
|
|
|
av_log(avctx, AV_LOG_WARNING,
|
|
|
|
"Unknown option: %s.\n", en->key);
|
|
|
|
break;
|
|
|
|
case X265_PARAM_BAD_VALUE:
|
|
|
|
av_log(avctx, AV_LOG_WARNING,
|
|
|
|
"Invalid value for %s: %s.\n", en->key, en->value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2013-10-02 14:46:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-18 06:39:54 +00:00
|
|
|
if (ctx->params->rc.vbvBufferSize && avctx->rc_initial_buffer_occupancy > 1000 &&
|
|
|
|
ctx->params->rc.vbvBufferInit == 0.9) {
|
|
|
|
ctx->params->rc.vbvBufferInit = (float)avctx->rc_initial_buffer_occupancy / 1000;
|
|
|
|
}
|
|
|
|
|
2017-12-05 07:47:53 +00:00
|
|
|
if (ctx->profile) {
|
|
|
|
if (ctx->api->param_apply_profile(ctx->params, ctx->profile) < 0) {
|
|
|
|
int i;
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid or incompatible profile set: %s.\n", ctx->profile);
|
|
|
|
av_log(avctx, AV_LOG_INFO, "Possible profiles:");
|
|
|
|
for (i = 0; x265_profile_names[i]; i++)
|
|
|
|
av_log(avctx, AV_LOG_INFO, " %s", x265_profile_names[i]);
|
|
|
|
av_log(avctx, AV_LOG_INFO, "\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-07 08:58:55 +00:00
|
|
|
ctx->encoder = ctx->api->encoder_open(ctx->params);
|
2013-10-02 14:46:26 +00:00
|
|
|
if (!ctx->encoder) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Cannot open libx265 encoder.\n");
|
|
|
|
libx265_encode_close(avctx);
|
2013-10-02 14:46:26 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
2013-10-02 14:46:26 +00:00
|
|
|
}
|
|
|
|
|
2015-06-29 19:59:37 +00:00
|
|
|
if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
|
2015-02-17 22:04:51 +00:00
|
|
|
x265_nal *nal;
|
|
|
|
int nnal;
|
|
|
|
|
2015-05-07 08:58:55 +00:00
|
|
|
avctx->extradata_size = ctx->api->encoder_headers(ctx->encoder, &nal, &nnal);
|
2014-03-25 20:51:21 +00:00
|
|
|
if (avctx->extradata_size <= 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Cannot encode headers.\n");
|
|
|
|
libx265_encode_close(avctx);
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2014-03-23 14:39:46 +00:00
|
|
|
|
2015-06-29 21:48:34 +00:00
|
|
|
avctx->extradata = av_malloc(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
|
2014-03-25 20:51:21 +00:00
|
|
|
if (!avctx->extradata) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"Cannot allocate HEVC header of size %d.\n", avctx->extradata_size);
|
|
|
|
libx265_encode_close(avctx);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(avctx->extradata, nal[0].payload, avctx->extradata_size);
|
2020-01-18 00:29:22 +00:00
|
|
|
memset(avctx->extradata + avctx->extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
|
2014-03-14 03:26:24 +00:00
|
|
|
}
|
|
|
|
|
2013-10-02 14:46:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-23 16:11:05 +00:00
|
|
|
static av_cold int libx265_encode_set_roi(libx265Context *ctx, const AVFrame *frame, x265_picture* pic)
|
|
|
|
{
|
|
|
|
AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_REGIONS_OF_INTEREST);
|
|
|
|
if (sd) {
|
|
|
|
if (ctx->params->rc.aqMode == X265_AQ_NONE) {
|
2019-09-23 02:02:35 +00:00
|
|
|
if (!ctx->roi_warned) {
|
|
|
|
ctx->roi_warned = 1;
|
|
|
|
av_log(ctx, AV_LOG_WARNING, "Adaptive quantization must be enabled to use ROI encoding, skipping ROI.\n");
|
|
|
|
}
|
2019-01-23 16:11:05 +00:00
|
|
|
} else {
|
|
|
|
/* 8x8 block when qg-size is 8, 16*16 block otherwise. */
|
|
|
|
int mb_size = (ctx->params->rc.qgSize == 8) ? 8 : 16;
|
|
|
|
int mbx = (frame->width + mb_size - 1) / mb_size;
|
|
|
|
int mby = (frame->height + mb_size - 1) / mb_size;
|
2019-06-03 23:19:03 +00:00
|
|
|
int qp_range = 51 + 6 * (pic->bitDepth - 8);
|
2019-01-23 16:11:05 +00:00
|
|
|
int nb_rois;
|
2019-06-03 23:19:03 +00:00
|
|
|
const AVRegionOfInterest *roi;
|
|
|
|
uint32_t roi_size;
|
2019-01-23 16:11:05 +00:00
|
|
|
float *qoffsets; /* will be freed after encode is called. */
|
2019-06-03 23:19:03 +00:00
|
|
|
|
|
|
|
roi = (const AVRegionOfInterest*)sd->data;
|
|
|
|
roi_size = roi->self_size;
|
|
|
|
if (!roi_size || sd->size % roi_size != 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Invalid AVRegionOfInterest.self_size.\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
nb_rois = sd->size / roi_size;
|
|
|
|
|
2024-04-25 09:18:18 +00:00
|
|
|
qoffsets = av_calloc(mbx * mby, sizeof(*qoffsets));
|
2019-01-23 16:11:05 +00:00
|
|
|
if (!qoffsets)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2019-06-03 23:19:03 +00:00
|
|
|
// This list must be iterated in reverse because the first
|
|
|
|
// region in the list applies when regions overlap.
|
|
|
|
for (int i = nb_rois - 1; i >= 0; i--) {
|
|
|
|
int startx, endx, starty, endy;
|
2019-01-23 16:11:05 +00:00
|
|
|
float qoffset;
|
|
|
|
|
2019-06-03 23:19:03 +00:00
|
|
|
roi = (const AVRegionOfInterest*)(sd->data + roi_size * i);
|
|
|
|
|
|
|
|
starty = FFMIN(mby, roi->top / mb_size);
|
|
|
|
endy = FFMIN(mby, (roi->bottom + mb_size - 1)/ mb_size);
|
|
|
|
startx = FFMIN(mbx, roi->left / mb_size);
|
|
|
|
endx = FFMIN(mbx, (roi->right + mb_size - 1)/ mb_size);
|
2019-01-23 16:11:05 +00:00
|
|
|
|
|
|
|
if (roi->qoffset.den == 0) {
|
|
|
|
av_free(qoffsets);
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "AVRegionOfInterest.qoffset.den must not be zero.\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
qoffset = roi->qoffset.num * 1.0f / roi->qoffset.den;
|
2019-06-03 23:19:03 +00:00
|
|
|
qoffset = av_clipf(qoffset * qp_range, -qp_range, +qp_range);
|
2019-01-23 16:11:05 +00:00
|
|
|
|
|
|
|
for (int y = starty; y < endy; y++)
|
|
|
|
for (int x = startx; x < endx; x++)
|
|
|
|
qoffsets[x + y*mbx] = qoffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
pic->quantOffsets = qoffsets;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-02 14:46:26 +00:00
|
|
|
static int libx265_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
|
|
|
|
const AVFrame *pic, int *got_packet)
|
|
|
|
{
|
|
|
|
libx265Context *ctx = avctx->priv_data;
|
|
|
|
x265_picture x265pic;
|
2015-05-01 14:39:26 +00:00
|
|
|
x265_picture x265pic_out = { 0 };
|
2013-10-02 14:46:26 +00:00
|
|
|
x265_nal *nal;
|
|
|
|
uint8_t *dst;
|
2019-12-28 16:34:19 +00:00
|
|
|
int pict_type;
|
2013-10-02 14:46:26 +00:00
|
|
|
int payload = 0;
|
|
|
|
int nnal;
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
2015-05-07 08:58:55 +00:00
|
|
|
ctx->api->picture_init(ctx->params, &x265pic);
|
2014-02-13 20:49:05 +00:00
|
|
|
|
2013-10-02 14:46:26 +00:00
|
|
|
if (pic) {
|
2024-04-25 09:18:18 +00:00
|
|
|
x265_sei *sei = &x265pic.userSEI;
|
|
|
|
sei->numPayloads = 0;
|
2013-10-02 14:46:26 +00:00
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
x265pic.planes[i] = pic->data[i];
|
|
|
|
x265pic.stride[i] = pic->linesize[i];
|
|
|
|
}
|
|
|
|
|
2014-02-21 16:26:40 +00:00
|
|
|
x265pic.pts = pic->pts;
|
2015-09-03 11:44:14 +00:00
|
|
|
x265pic.bitDepth = av_pix_fmt_desc_get(avctx->pix_fmt)->comp[0].depth;
|
2015-01-21 15:16:18 +00:00
|
|
|
|
2016-11-02 14:37:25 +00:00
|
|
|
x265pic.sliceType = pic->pict_type == AV_PICTURE_TYPE_I ?
|
|
|
|
(ctx->forced_idr ? X265_TYPE_IDR : X265_TYPE_I) :
|
2015-01-21 15:16:18 +00:00
|
|
|
pic->pict_type == AV_PICTURE_TYPE_P ? X265_TYPE_P :
|
|
|
|
pic->pict_type == AV_PICTURE_TYPE_B ? X265_TYPE_B :
|
|
|
|
X265_TYPE_AUTO;
|
2019-01-23 16:11:05 +00:00
|
|
|
|
|
|
|
ret = libx265_encode_set_roi(ctx, pic, &x265pic);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2020-05-19 18:55:13 +00:00
|
|
|
|
|
|
|
if (pic->reordered_opaque) {
|
|
|
|
x265pic.userData = av_malloc(sizeof(pic->reordered_opaque));
|
|
|
|
if (!x265pic.userData) {
|
|
|
|
av_freep(&x265pic.quantOffsets);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(x265pic.userData, &pic->reordered_opaque, sizeof(pic->reordered_opaque));
|
|
|
|
}
|
2024-04-25 09:18:18 +00:00
|
|
|
|
|
|
|
if (ctx->udu_sei) {
|
|
|
|
for (i = 0; i < pic->nb_side_data; i++) {
|
|
|
|
AVFrameSideData *side_data = pic->side_data[i];
|
|
|
|
void *tmp;
|
|
|
|
x265_sei_payload *sei_payload;
|
|
|
|
|
|
|
|
if (side_data->type != AV_FRAME_DATA_SEI_UNREGISTERED)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
tmp = av_fast_realloc(ctx->sei_data,
|
|
|
|
&ctx->sei_data_size,
|
|
|
|
(sei->numPayloads + 1) * sizeof(*sei_payload));
|
|
|
|
if (!tmp) {
|
|
|
|
av_freep(&x265pic.userData);
|
|
|
|
av_freep(&x265pic.quantOffsets);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
ctx->sei_data = tmp;
|
|
|
|
sei->payloads = ctx->sei_data;
|
|
|
|
sei_payload = &sei->payloads[sei->numPayloads];
|
|
|
|
sei_payload->payload = side_data->data;
|
|
|
|
sei_payload->payloadSize = side_data->size;
|
|
|
|
/* Equal to libx265 USER_DATA_UNREGISTERED */
|
|
|
|
sei_payload->payloadType = SEI_TYPE_USER_DATA_UNREGISTERED;
|
|
|
|
sei->numPayloads++;
|
|
|
|
}
|
|
|
|
}
|
2013-10-02 14:46:26 +00:00
|
|
|
}
|
|
|
|
|
2015-05-07 08:58:55 +00:00
|
|
|
ret = ctx->api->encoder_encode(ctx->encoder, &nal, &nnal,
|
|
|
|
pic ? &x265pic : NULL, &x265pic_out);
|
2019-01-23 16:11:05 +00:00
|
|
|
|
|
|
|
av_freep(&x265pic.quantOffsets);
|
|
|
|
|
2013-10-02 14:46:26 +00:00
|
|
|
if (ret < 0)
|
2014-02-21 17:13:56 +00:00
|
|
|
return AVERROR_EXTERNAL;
|
2013-10-02 14:46:26 +00:00
|
|
|
|
|
|
|
if (!nnal)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (i = 0; i < nnal; i++)
|
|
|
|
payload += nal[i].sizeBytes;
|
|
|
|
|
2024-04-25 09:18:18 +00:00
|
|
|
ret = ff_get_encode_buffer(avctx, pkt, payload, 0);
|
2013-10-02 14:46:26 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
dst = pkt->data;
|
|
|
|
|
|
|
|
for (i = 0; i < nnal; i++) {
|
|
|
|
memcpy(dst, nal[i].payload, nal[i].sizeBytes);
|
|
|
|
dst += nal[i].sizeBytes;
|
|
|
|
|
|
|
|
if (is_keyframe(nal[i].type))
|
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
|
|
|
}
|
|
|
|
|
|
|
|
pkt->pts = x265pic_out.pts;
|
|
|
|
pkt->dts = x265pic_out.dts;
|
|
|
|
|
2015-03-30 22:37:46 +00:00
|
|
|
switch (x265pic_out.sliceType) {
|
|
|
|
case X265_TYPE_IDR:
|
|
|
|
case X265_TYPE_I:
|
2019-12-28 16:34:19 +00:00
|
|
|
pict_type = AV_PICTURE_TYPE_I;
|
2015-03-30 22:37:46 +00:00
|
|
|
break;
|
|
|
|
case X265_TYPE_P:
|
2019-12-28 16:34:19 +00:00
|
|
|
pict_type = AV_PICTURE_TYPE_P;
|
2015-03-30 22:37:46 +00:00
|
|
|
break;
|
|
|
|
case X265_TYPE_B:
|
2019-12-28 16:34:19 +00:00
|
|
|
case X265_TYPE_BREF:
|
|
|
|
pict_type = AV_PICTURE_TYPE_B;
|
2015-03-30 22:37:46 +00:00
|
|
|
break;
|
2020-04-18 04:52:47 +00:00
|
|
|
default:
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Unknown picture type encountered.\n");
|
|
|
|
return AVERROR_EXTERNAL;
|
2015-03-30 22:37:46 +00:00
|
|
|
}
|
2019-12-28 16:34:19 +00:00
|
|
|
|
2017-12-06 00:36:58 +00:00
|
|
|
#if X265_BUILD >= 130
|
|
|
|
if (x265pic_out.sliceType == X265_TYPE_B)
|
|
|
|
#else
|
|
|
|
if (x265pic_out.frameData.sliceType == 'b')
|
|
|
|
#endif
|
|
|
|
pkt->flags |= AV_PKT_FLAG_DISPOSABLE;
|
|
|
|
|
2019-12-28 16:34:19 +00:00
|
|
|
ff_side_data_set_encoder_stats(pkt, x265pic_out.frameData.qp * FF_QP2LAMBDA, NULL, 0, pict_type);
|
|
|
|
|
2020-05-19 18:55:13 +00:00
|
|
|
if (x265pic_out.userData) {
|
|
|
|
memcpy(&avctx->reordered_opaque, x265pic_out.userData, sizeof(avctx->reordered_opaque));
|
|
|
|
av_freep(&x265pic_out.userData);
|
|
|
|
} else
|
|
|
|
avctx->reordered_opaque = 0;
|
|
|
|
|
2013-10-02 14:46:26 +00:00
|
|
|
*got_packet = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const enum AVPixelFormat x265_csp_eight[] = {
|
|
|
|
AV_PIX_FMT_YUV420P,
|
2019-05-24 15:15:11 +00:00
|
|
|
AV_PIX_FMT_YUVJ420P,
|
2014-06-11 20:19:42 +00:00
|
|
|
AV_PIX_FMT_YUV422P,
|
2019-05-24 15:15:11 +00:00
|
|
|
AV_PIX_FMT_YUVJ422P,
|
2014-02-21 16:59:13 +00:00
|
|
|
AV_PIX_FMT_YUV444P,
|
2019-05-24 15:15:11 +00:00
|
|
|
AV_PIX_FMT_YUVJ444P,
|
2016-03-13 08:40:09 +00:00
|
|
|
AV_PIX_FMT_GBRP,
|
2016-07-06 16:56:19 +00:00
|
|
|
AV_PIX_FMT_GRAY8,
|
2013-10-02 14:46:26 +00:00
|
|
|
AV_PIX_FMT_NONE
|
|
|
|
};
|
|
|
|
|
2016-01-27 00:10:02 +00:00
|
|
|
static const enum AVPixelFormat x265_csp_ten[] = {
|
|
|
|
AV_PIX_FMT_YUV420P,
|
2019-05-24 15:15:11 +00:00
|
|
|
AV_PIX_FMT_YUVJ420P,
|
2016-01-27 00:10:02 +00:00
|
|
|
AV_PIX_FMT_YUV422P,
|
2019-05-24 15:15:11 +00:00
|
|
|
AV_PIX_FMT_YUVJ422P,
|
2016-01-27 00:10:02 +00:00
|
|
|
AV_PIX_FMT_YUV444P,
|
2019-05-24 15:15:11 +00:00
|
|
|
AV_PIX_FMT_YUVJ444P,
|
2016-03-13 08:40:09 +00:00
|
|
|
AV_PIX_FMT_GBRP,
|
2016-01-27 00:10:02 +00:00
|
|
|
AV_PIX_FMT_YUV420P10,
|
|
|
|
AV_PIX_FMT_YUV422P10,
|
|
|
|
AV_PIX_FMT_YUV444P10,
|
2016-03-13 08:40:09 +00:00
|
|
|
AV_PIX_FMT_GBRP10,
|
2016-07-06 16:56:19 +00:00
|
|
|
AV_PIX_FMT_GRAY8,
|
2017-03-07 23:25:33 +00:00
|
|
|
AV_PIX_FMT_GRAY10,
|
2016-01-27 00:10:02 +00:00
|
|
|
AV_PIX_FMT_NONE
|
|
|
|
};
|
|
|
|
|
2013-10-02 14:46:26 +00:00
|
|
|
static const enum AVPixelFormat x265_csp_twelve[] = {
|
|
|
|
AV_PIX_FMT_YUV420P,
|
2019-05-24 15:15:11 +00:00
|
|
|
AV_PIX_FMT_YUVJ420P,
|
2014-06-11 20:19:42 +00:00
|
|
|
AV_PIX_FMT_YUV422P,
|
2019-05-24 15:15:11 +00:00
|
|
|
AV_PIX_FMT_YUVJ422P,
|
2014-02-21 16:59:13 +00:00
|
|
|
AV_PIX_FMT_YUV444P,
|
2019-05-24 15:15:11 +00:00
|
|
|
AV_PIX_FMT_YUVJ444P,
|
2016-03-13 08:40:09 +00:00
|
|
|
AV_PIX_FMT_GBRP,
|
2013-10-02 14:46:26 +00:00
|
|
|
AV_PIX_FMT_YUV420P10,
|
2014-06-11 20:19:42 +00:00
|
|
|
AV_PIX_FMT_YUV422P10,
|
2014-02-21 16:59:13 +00:00
|
|
|
AV_PIX_FMT_YUV444P10,
|
2016-03-13 08:40:09 +00:00
|
|
|
AV_PIX_FMT_GBRP10,
|
2016-01-27 00:10:02 +00:00
|
|
|
AV_PIX_FMT_YUV420P12,
|
|
|
|
AV_PIX_FMT_YUV422P12,
|
|
|
|
AV_PIX_FMT_YUV444P12,
|
2016-03-13 08:40:09 +00:00
|
|
|
AV_PIX_FMT_GBRP12,
|
2016-07-06 16:56:19 +00:00
|
|
|
AV_PIX_FMT_GRAY8,
|
2017-03-07 23:25:33 +00:00
|
|
|
AV_PIX_FMT_GRAY10,
|
|
|
|
AV_PIX_FMT_GRAY12,
|
2013-10-02 14:46:26 +00:00
|
|
|
AV_PIX_FMT_NONE
|
|
|
|
};
|
|
|
|
|
2024-04-25 09:18:18 +00:00
|
|
|
static av_cold void libx265_encode_init_csp(FFCodec *codec)
|
2013-10-02 14:46:26 +00:00
|
|
|
{
|
2016-01-27 00:10:02 +00:00
|
|
|
if (x265_api_get(12))
|
2024-04-25 09:18:18 +00:00
|
|
|
codec->p.pix_fmts = x265_csp_twelve;
|
2016-01-27 00:10:02 +00:00
|
|
|
else if (x265_api_get(10))
|
2024-04-25 09:18:18 +00:00
|
|
|
codec->p.pix_fmts = x265_csp_ten;
|
2015-05-16 03:05:12 +00:00
|
|
|
else if (x265_api_get(8))
|
2024-04-25 09:18:18 +00:00
|
|
|
codec->p.pix_fmts = x265_csp_eight;
|
2013-10-02 14:46:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(libx265Context, x)
|
|
|
|
#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
|
|
|
|
static const AVOption options[] = {
|
2015-02-17 22:04:53 +00:00
|
|
|
{ "crf", "set the x265 crf", OFFSET(crf), AV_OPT_TYPE_FLOAT, { .dbl = -1 }, -1, FLT_MAX, VE },
|
2019-08-14 01:09:34 +00:00
|
|
|
{ "qp", "set the x265 qp", OFFSET(cqp), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE },
|
2016-11-02 14:37:25 +00:00
|
|
|
{ "forced-idr", "if forcing keyframes, force them as IDR frames", OFFSET(forced_idr),AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
|
2013-10-02 14:46:26 +00:00
|
|
|
{ "preset", "set the x265 preset", OFFSET(preset), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
|
|
|
|
{ "tune", "set the x265 tune parameter", OFFSET(tune), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
|
2017-12-05 07:47:53 +00:00
|
|
|
{ "profile", "set the x265 profile", OFFSET(profile), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
|
2024-04-25 09:18:18 +00:00
|
|
|
{ "udu_sei", "Use user data unregistered SEI if available", OFFSET(udu_sei), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
|
2019-12-25 00:50:36 +00:00
|
|
|
{ "x265-params", "set the x265 configuration using a :-separated list of key=value parameters", OFFSET(x265_opts), AV_OPT_TYPE_DICT, { 0 }, 0, 0, VE },
|
2013-10-02 14:46:26 +00:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass class = {
|
|
|
|
.class_name = "libx265",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
2024-04-25 09:18:18 +00:00
|
|
|
static const FFCodecDefault x265_defaults[] = {
|
2014-08-28 01:49:05 +00:00
|
|
|
{ "b", "0" },
|
2019-08-14 01:07:08 +00:00
|
|
|
{ "bf", "-1" },
|
|
|
|
{ "g", "-1" },
|
|
|
|
{ "keyint_min", "-1" },
|
|
|
|
{ "refs", "-1" },
|
2019-08-14 01:09:34 +00:00
|
|
|
{ "qmin", "-1" },
|
|
|
|
{ "qmax", "-1" },
|
|
|
|
{ "qdiff", "-1" },
|
|
|
|
{ "qblur", "-1" },
|
|
|
|
{ "qcomp", "-1" },
|
|
|
|
{ "i_qfactor", "-1" },
|
|
|
|
{ "b_qfactor", "-1" },
|
2014-08-28 01:49:05 +00:00
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
2024-04-25 09:18:18 +00:00
|
|
|
FFCodec ff_libx265_encoder = {
|
|
|
|
.p.name = "libx265",
|
|
|
|
.p.long_name = NULL_IF_CONFIG_SMALL("libx265 H.265 / HEVC"),
|
|
|
|
.p.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.p.id = AV_CODEC_ID_HEVC,
|
|
|
|
.p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
|
|
|
|
AV_CODEC_CAP_OTHER_THREADS |
|
|
|
|
AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
|
|
|
|
.p.priv_class = &class,
|
|
|
|
.p.wrapper_name = "libx265",
|
2013-10-02 14:46:26 +00:00
|
|
|
.init = libx265_encode_init,
|
|
|
|
.init_static_data = libx265_encode_init_csp,
|
2024-04-25 09:18:18 +00:00
|
|
|
FF_CODEC_ENCODE_CB(libx265_encode_frame),
|
2013-10-02 14:46:26 +00:00
|
|
|
.close = libx265_encode_close,
|
|
|
|
.priv_data_size = sizeof(libx265Context),
|
2014-08-28 01:49:05 +00:00
|
|
|
.defaults = x265_defaults,
|
2022-05-23 03:23:35 +00:00
|
|
|
.caps_internal = FF_CODEC_CAP_AUTO_THREADS,
|
2013-10-02 14:46:26 +00:00
|
|
|
};
|