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"
|
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
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;
|
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;
|
2013-10-02 14:46:26 +00:00
|
|
|
char *x265_opts;
|
|
|
|
} 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);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int libx265_encode_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
libx265Context *ctx = avctx->priv_data;
|
|
|
|
|
2015-09-03 11:44:14 +00:00
|
|
|
ctx->api = x265_api_get(av_pix_fmt_desc_get(avctx->pix_fmt)->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;
|
2014-02-21 16:26:40 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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.bEnableVideoSignalTypePresentFlag = 1;
|
|
|
|
ctx->params->vui.bEnableColorDescriptionPresentFlag = 1;
|
|
|
|
|
|
|
|
// x265 validates the parameters internally
|
|
|
|
ctx->params->vui.colorPrimaries = avctx->color_primaries;
|
|
|
|
ctx->params->vui.transferCharacteristics = avctx->color_trc;
|
|
|
|
ctx->params->vui.matrixCoeffs = avctx->colorspace;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2014-02-21 16:59:13 +00:00
|
|
|
switch (avctx->pix_fmt) {
|
|
|
|
case AV_PIX_FMT_YUV420P:
|
|
|
|
case AV_PIX_FMT_YUV420P10:
|
2016-01-27 00:10:02 +00:00
|
|
|
case AV_PIX_FMT_YUV420P12:
|
2014-02-21 16:59:13 +00:00
|
|
|
ctx->params->internalCsp = X265_CSP_I420;
|
|
|
|
break;
|
2014-06-11 20:19:42 +00:00
|
|
|
case AV_PIX_FMT_YUV422P:
|
|
|
|
case AV_PIX_FMT_YUV422P10:
|
2016-01-27 00:10:02 +00:00
|
|
|
case AV_PIX_FMT_YUV422P12:
|
2014-06-11 20:19:42 +00:00
|
|
|
ctx->params->internalCsp = X265_CSP_I422;
|
|
|
|
break;
|
2016-03-13 08:40:09 +00:00
|
|
|
case AV_PIX_FMT_GBRP:
|
|
|
|
case AV_PIX_FMT_GBRP10:
|
|
|
|
case AV_PIX_FMT_GBRP12:
|
|
|
|
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
|
|
|
case AV_PIX_FMT_YUV444P:
|
|
|
|
case AV_PIX_FMT_YUV444P10:
|
2016-01-27 00:10:02 +00:00
|
|
|
case AV_PIX_FMT_YUV444P12:
|
2014-02-21 16:59:13 +00:00
|
|
|
ctx->params->internalCsp = X265_CSP_I444;
|
|
|
|
break;
|
2016-07-06 16:56:19 +00:00
|
|
|
case AV_PIX_FMT_GRAY8:
|
2017-03-07 23:25:33 +00:00
|
|
|
case AV_PIX_FMT_GRAY10:
|
|
|
|
case AV_PIX_FMT_GRAY12:
|
2016-07-06 16:56:19 +00:00
|
|
|
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;
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
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;
|
|
|
|
|
2013-10-02 14:46:26 +00:00
|
|
|
if (ctx->x265_opts) {
|
|
|
|
AVDictionary *dict = NULL;
|
|
|
|
AVDictionaryEntry *en = NULL;
|
|
|
|
|
|
|
|
if (!av_dict_parse_string(&dict, ctx->x265_opts, "=", ":", 0)) {
|
|
|
|
while ((en = av_dict_get(dict, "", en, AV_DICT_IGNORE_SUFFIX))) {
|
2015-05-07 08:58:55 +00:00
|
|
|
int parse_ret = ctx->api->param_parse(ctx->params, en->key, en->value);
|
2013-10-02 14:46:26 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
av_dict_free(&dict);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2014-03-14 03:26:24 +00:00
|
|
|
}
|
|
|
|
|
2013-10-02 14:46:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
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) {
|
|
|
|
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;
|
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);
|
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;
|
|
|
|
|
2017-11-08 13:04:51 +00:00
|
|
|
ret = ff_alloc_packet2(avctx, pkt, payload, payload);
|
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-07-15 17:41:22 +00:00
|
|
|
#if FF_API_CODED_FRAME
|
|
|
|
FF_DISABLE_DEPRECATION_WARNINGS
|
2015-03-30 22:37:46 +00:00
|
|
|
switch (x265pic_out.sliceType) {
|
|
|
|
case X265_TYPE_IDR:
|
|
|
|
case X265_TYPE_I:
|
|
|
|
avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
|
|
|
|
break;
|
|
|
|
case X265_TYPE_P:
|
|
|
|
avctx->coded_frame->pict_type = AV_PICTURE_TYPE_P;
|
|
|
|
break;
|
|
|
|
case X265_TYPE_B:
|
|
|
|
avctx->coded_frame->pict_type = AV_PICTURE_TYPE_B;
|
|
|
|
break;
|
|
|
|
}
|
2015-07-15 17:41:22 +00:00
|
|
|
FF_ENABLE_DEPRECATION_WARNINGS
|
|
|
|
#endif
|
2015-03-30 22:37:46 +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;
|
|
|
|
|
2013-10-02 14:46:26 +00:00
|
|
|
*got_packet = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const enum AVPixelFormat x265_csp_eight[] = {
|
|
|
|
AV_PIX_FMT_YUV420P,
|
2014-06-11 20:19:42 +00:00
|
|
|
AV_PIX_FMT_YUV422P,
|
2014-02-21 16:59:13 +00:00
|
|
|
AV_PIX_FMT_YUV444P,
|
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,
|
|
|
|
AV_PIX_FMT_YUV422P,
|
|
|
|
AV_PIX_FMT_YUV444P,
|
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,
|
2014-06-11 20:19:42 +00:00
|
|
|
AV_PIX_FMT_YUV422P,
|
2014-02-21 16:59:13 +00:00
|
|
|
AV_PIX_FMT_YUV444P,
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
static av_cold void libx265_encode_init_csp(AVCodec *codec)
|
|
|
|
{
|
2016-01-27 00:10:02 +00:00
|
|
|
if (x265_api_get(12))
|
2013-10-02 14:46:26 +00:00
|
|
|
codec->pix_fmts = x265_csp_twelve;
|
2016-01-27 00:10:02 +00:00
|
|
|
else if (x265_api_get(10))
|
|
|
|
codec->pix_fmts = x265_csp_ten;
|
2015-05-16 03:05:12 +00:00
|
|
|
else if (x265_api_get(8))
|
|
|
|
codec->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 },
|
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 },
|
2013-10-02 14:46:26 +00:00
|
|
|
{ "x265-params", "set the x265 configuration using a :-separated list of key=value parameters", OFFSET(x265_opts), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass class = {
|
|
|
|
.class_name = "libx265",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
2014-08-28 01:49:05 +00:00
|
|
|
static const AVCodecDefault x265_defaults[] = {
|
|
|
|
{ "b", "0" },
|
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
2013-10-02 14:46:26 +00:00
|
|
|
AVCodec ff_libx265_encoder = {
|
|
|
|
.name = "libx265",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("libx265 H.265 / HEVC"),
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.id = AV_CODEC_ID_HEVC,
|
|
|
|
.init = libx265_encode_init,
|
|
|
|
.init_static_data = libx265_encode_init_csp,
|
|
|
|
.encode2 = libx265_encode_frame,
|
|
|
|
.close = libx265_encode_close,
|
|
|
|
.priv_data_size = sizeof(libx265Context),
|
|
|
|
.priv_class = &class,
|
2014-08-28 01:49:05 +00:00
|
|
|
.defaults = x265_defaults,
|
2015-07-07 00:41:27 +00:00
|
|
|
.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS,
|
2017-12-11 15:18:44 +00:00
|
|
|
.wrapper_name = "libx265",
|
2013-10-02 14:46:26 +00:00
|
|
|
};
|