2013-10-02 17:46:26 +03: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 22:51:52 +03:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#define X265_API_IMPORTS 1
|
|
|
|
#endif
|
|
|
|
|
2013-10-02 17:46:26 +03:00
|
|
|
#include <x265.h>
|
2015-02-18 00:04:53 +02:00
|
|
|
#include <float.h>
|
2013-10-02 17:46:26 +03: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 10:58:55 +02:00
|
|
|
const x265_api *api;
|
2013-10-02 17:46:26 +03:00
|
|
|
|
2015-02-18 00:04:53 +02:00
|
|
|
float crf;
|
2016-11-02 16:37:25 +02:00
|
|
|
int forced_idr;
|
2013-10-02 17:46:26 +03:00
|
|
|
char *preset;
|
|
|
|
char *tune;
|
|
|
|
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 10:58:55 +02:00
|
|
|
ctx->api->param_free(ctx->params);
|
2013-10-02 17:46:26 +03:00
|
|
|
|
|
|
|
if (ctx->encoder)
|
2015-05-07 10:58:55 +02:00
|
|
|
ctx->api->encoder_close(ctx->encoder);
|
2013-10-02 17:46:26 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int libx265_encode_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
libx265Context *ctx = avctx->priv_data;
|
|
|
|
|
2015-09-03 13:44:14 +02:00
|
|
|
ctx->api = x265_api_get(av_pix_fmt_desc_get(avctx->pix_fmt)->comp[0].depth);
|
2015-05-07 10:58:55 +02:00
|
|
|
if (!ctx->api)
|
|
|
|
ctx->api = x265_api_get(0);
|
|
|
|
|
|
|
|
ctx->params = ctx->api->param_alloc();
|
2013-10-02 17:46:26 +03:00
|
|
|
if (!ctx->params) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Could not allocate x265 param structure.\n");
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
2015-05-07 10:58:55 +02:00
|
|
|
if (ctx->api->param_default_preset(ctx->params, ctx->preset, ctx->tune) < 0) {
|
2015-04-10 21:47:03 +02: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 17:46:26 +03:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->params->frameNumThreads = avctx->thread_count;
|
2014-02-21 19:26:40 +03:00
|
|
|
ctx->params->fpsNum = avctx->time_base.den;
|
|
|
|
ctx->params->fpsDenom = avctx->time_base.num * avctx->ticks_per_frame;
|
2013-10-02 17:46:26 +03:00
|
|
|
ctx->params->sourceWidth = avctx->width;
|
|
|
|
ctx->params->sourceHeight = avctx->height;
|
2015-06-29 21:59:37 +02:00
|
|
|
ctx->params->bEnablePsnr = !!(avctx->flags & AV_CODEC_FLAG_PSNR);
|
2014-02-21 19:26:40 +03:00
|
|
|
|
2015-04-13 10:45:51 +02:00
|
|
|
if ((avctx->color_primaries <= AVCOL_PRI_BT2020 &&
|
|
|
|
avctx->color_primaries != AVCOL_PRI_UNSPECIFIED) ||
|
|
|
|
(avctx->color_trc <= AVCOL_TRC_BT2020_12 &&
|
|
|
|
avctx->color_trc != AVCOL_TRC_UNSPECIFIED) ||
|
|
|
|
(avctx->colorspace <= AVCOL_SPC_BT2020_CL &&
|
|
|
|
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 15:27:41 +03:00
|
|
|
if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
|
2015-02-18 00:04:51 +02:00
|
|
|
char sar[12];
|
|
|
|
int sar_num, sar_den;
|
|
|
|
|
2014-04-10 15:27:41 +03: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 10:58:55 +02:00
|
|
|
if (ctx->api->param_parse(ctx->params, "sar", sar) == X265_PARAM_BAD_VALUE) {
|
2014-04-10 15:27:41 +03:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid SAR: %d:%d.\n", sar_num, sar_den);
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2014-04-08 20:44:53 +03:00
|
|
|
}
|
2014-02-21 20:05:50 +03:00
|
|
|
|
2014-02-21 19:59:13 +03:00
|
|
|
switch (avctx->pix_fmt) {
|
|
|
|
case AV_PIX_FMT_YUV420P:
|
|
|
|
case AV_PIX_FMT_YUV420P10:
|
2016-01-27 02:10:02 +02:00
|
|
|
case AV_PIX_FMT_YUV420P12:
|
2014-02-21 19:59:13 +03:00
|
|
|
ctx->params->internalCsp = X265_CSP_I420;
|
|
|
|
break;
|
2014-06-11 23:19:42 +03:00
|
|
|
case AV_PIX_FMT_YUV422P:
|
|
|
|
case AV_PIX_FMT_YUV422P10:
|
2016-01-27 02:10:02 +02:00
|
|
|
case AV_PIX_FMT_YUV422P12:
|
2014-06-11 23:19:42 +03:00
|
|
|
ctx->params->internalCsp = X265_CSP_I422;
|
|
|
|
break;
|
2016-03-13 10:40:09 +02: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 19:59:13 +03:00
|
|
|
case AV_PIX_FMT_YUV444P:
|
|
|
|
case AV_PIX_FMT_YUV444P10:
|
2016-01-27 02:10:02 +02:00
|
|
|
case AV_PIX_FMT_YUV444P12:
|
2014-02-21 19:59:13 +03:00
|
|
|
ctx->params->internalCsp = X265_CSP_I444;
|
|
|
|
break;
|
2016-07-06 18:56:19 +02:00
|
|
|
case AV_PIX_FMT_GRAY8:
|
|
|
|
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 19:59:13 +03:00
|
|
|
}
|
|
|
|
|
2015-02-18 00:04:53 +02:00
|
|
|
if (ctx->crf >= 0) {
|
|
|
|
char crf[6];
|
|
|
|
|
|
|
|
snprintf(crf, sizeof(crf), "%2.2f", ctx->crf);
|
2015-05-07 10:58:55 +02:00
|
|
|
if (ctx->api->param_parse(ctx->params, "crf", crf) == X265_PARAM_BAD_VALUE) {
|
2015-02-18 00:04:53 +02: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 17:46:26 +03:00
|
|
|
ctx->params->rc.bitrate = avctx->bit_rate / 1000;
|
|
|
|
ctx->params->rc.rateControlMode = X265_RC_ABR;
|
|
|
|
}
|
|
|
|
|
2015-06-29 21:59:37 +02:00
|
|
|
if (!(avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER))
|
2014-03-25 23:51:21 +03:00
|
|
|
ctx->params->bRepeatHeaders = 1;
|
|
|
|
|
2013-10-02 17:46:26 +03: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 10:58:55 +02:00
|
|
|
int parse_ret = ctx->api->param_parse(ctx->params, en->key, en->value);
|
2013-10-02 17:46:26 +03: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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-07 10:58:55 +02:00
|
|
|
ctx->encoder = ctx->api->encoder_open(ctx->params);
|
2013-10-02 17:46:26 +03:00
|
|
|
if (!ctx->encoder) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Cannot open libx265 encoder.\n");
|
|
|
|
libx265_encode_close(avctx);
|
2013-10-02 17:46:26 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2013-10-02 17:46:26 +03:00
|
|
|
}
|
|
|
|
|
2015-06-29 21:59:37 +02:00
|
|
|
if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
|
2015-02-18 00:04:51 +02:00
|
|
|
x265_nal *nal;
|
|
|
|
int nnal;
|
|
|
|
|
2015-05-07 10:58:55 +02:00
|
|
|
avctx->extradata_size = ctx->api->encoder_headers(ctx->encoder, &nal, &nnal);
|
2014-03-25 23:51:21 +03: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 17:39:46 +03:00
|
|
|
|
2015-06-29 23:48:34 +02:00
|
|
|
avctx->extradata = av_malloc(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
|
2014-03-25 23:51:21 +03: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 06:26:24 +03:00
|
|
|
}
|
|
|
|
|
2013-10-02 17:46:26 +03: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 16:39:26 +02:00
|
|
|
x265_picture x265pic_out = { 0 };
|
2013-10-02 17:46:26 +03:00
|
|
|
x265_nal *nal;
|
|
|
|
uint8_t *dst;
|
|
|
|
int payload = 0;
|
|
|
|
int nnal;
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
2015-05-07 10:58:55 +02:00
|
|
|
ctx->api->picture_init(ctx->params, &x265pic);
|
2014-02-13 23:49:05 +03:00
|
|
|
|
2013-10-02 17:46:26 +03:00
|
|
|
if (pic) {
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
x265pic.planes[i] = pic->data[i];
|
|
|
|
x265pic.stride[i] = pic->linesize[i];
|
|
|
|
}
|
|
|
|
|
2014-02-21 19:26:40 +03:00
|
|
|
x265pic.pts = pic->pts;
|
2015-09-03 13:44:14 +02:00
|
|
|
x265pic.bitDepth = av_pix_fmt_desc_get(avctx->pix_fmt)->comp[0].depth;
|
2015-01-21 17:16:18 +02:00
|
|
|
|
2016-11-02 16:37:25 +02:00
|
|
|
x265pic.sliceType = pic->pict_type == AV_PICTURE_TYPE_I ?
|
|
|
|
(ctx->forced_idr ? X265_TYPE_IDR : X265_TYPE_I) :
|
2015-01-21 17:16:18 +02: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 17:46:26 +03:00
|
|
|
}
|
|
|
|
|
2015-05-07 10:58:55 +02:00
|
|
|
ret = ctx->api->encoder_encode(ctx->encoder, &nal, &nnal,
|
|
|
|
pic ? &x265pic : NULL, &x265pic_out);
|
2013-10-02 17:46:26 +03:00
|
|
|
if (ret < 0)
|
2014-02-21 20:13:56 +03:00
|
|
|
return AVERROR_EXTERNAL;
|
2013-10-02 17:46:26 +03:00
|
|
|
|
|
|
|
if (!nnal)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (i = 0; i < nnal; i++)
|
|
|
|
payload += nal[i].sizeBytes;
|
|
|
|
|
|
|
|
ret = ff_alloc_packet(pkt, payload);
|
|
|
|
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 19:41:22 +02:00
|
|
|
#if FF_API_CODED_FRAME
|
|
|
|
FF_DISABLE_DEPRECATION_WARNINGS
|
2015-03-31 00:37:46 +02: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 19:41:22 +02:00
|
|
|
FF_ENABLE_DEPRECATION_WARNINGS
|
|
|
|
#endif
|
2015-03-31 00:37:46 +02:00
|
|
|
|
2013-10-02 17:46:26 +03:00
|
|
|
*got_packet = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const enum AVPixelFormat x265_csp_eight[] = {
|
|
|
|
AV_PIX_FMT_YUV420P,
|
2014-06-11 23:19:42 +03:00
|
|
|
AV_PIX_FMT_YUV422P,
|
2014-02-21 19:59:13 +03:00
|
|
|
AV_PIX_FMT_YUV444P,
|
2016-03-13 10:40:09 +02:00
|
|
|
AV_PIX_FMT_GBRP,
|
2016-07-06 18:56:19 +02:00
|
|
|
AV_PIX_FMT_GRAY8,
|
2013-10-02 17:46:26 +03:00
|
|
|
AV_PIX_FMT_NONE
|
|
|
|
};
|
|
|
|
|
2016-01-27 02:10:02 +02:00
|
|
|
static const enum AVPixelFormat x265_csp_ten[] = {
|
|
|
|
AV_PIX_FMT_YUV420P,
|
|
|
|
AV_PIX_FMT_YUV422P,
|
|
|
|
AV_PIX_FMT_YUV444P,
|
2016-03-13 10:40:09 +02:00
|
|
|
AV_PIX_FMT_GBRP,
|
2016-01-27 02:10:02 +02:00
|
|
|
AV_PIX_FMT_YUV420P10,
|
|
|
|
AV_PIX_FMT_YUV422P10,
|
|
|
|
AV_PIX_FMT_YUV444P10,
|
2016-03-13 10:40:09 +02:00
|
|
|
AV_PIX_FMT_GBRP10,
|
2016-07-06 18:56:19 +02:00
|
|
|
AV_PIX_FMT_GRAY8,
|
2016-01-27 02:10:02 +02:00
|
|
|
AV_PIX_FMT_NONE
|
|
|
|
};
|
|
|
|
|
2013-10-02 17:46:26 +03:00
|
|
|
static const enum AVPixelFormat x265_csp_twelve[] = {
|
|
|
|
AV_PIX_FMT_YUV420P,
|
2014-06-11 23:19:42 +03:00
|
|
|
AV_PIX_FMT_YUV422P,
|
2014-02-21 19:59:13 +03:00
|
|
|
AV_PIX_FMT_YUV444P,
|
2016-03-13 10:40:09 +02:00
|
|
|
AV_PIX_FMT_GBRP,
|
2013-10-02 17:46:26 +03:00
|
|
|
AV_PIX_FMT_YUV420P10,
|
2014-06-11 23:19:42 +03:00
|
|
|
AV_PIX_FMT_YUV422P10,
|
2014-02-21 19:59:13 +03:00
|
|
|
AV_PIX_FMT_YUV444P10,
|
2016-03-13 10:40:09 +02:00
|
|
|
AV_PIX_FMT_GBRP10,
|
2016-01-27 02:10:02 +02:00
|
|
|
AV_PIX_FMT_YUV420P12,
|
|
|
|
AV_PIX_FMT_YUV422P12,
|
|
|
|
AV_PIX_FMT_YUV444P12,
|
2016-03-13 10:40:09 +02:00
|
|
|
AV_PIX_FMT_GBRP12,
|
2016-07-06 18:56:19 +02:00
|
|
|
AV_PIX_FMT_GRAY8,
|
2013-10-02 17:46:26 +03:00
|
|
|
AV_PIX_FMT_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
static av_cold void libx265_encode_init_csp(AVCodec *codec)
|
|
|
|
{
|
2016-01-27 02:10:02 +02:00
|
|
|
if (x265_api_get(12))
|
2013-10-02 17:46:26 +03:00
|
|
|
codec->pix_fmts = x265_csp_twelve;
|
2016-01-27 02:10:02 +02:00
|
|
|
else if (x265_api_get(10))
|
|
|
|
codec->pix_fmts = x265_csp_ten;
|
2015-05-16 05:05:12 +02:00
|
|
|
else if (x265_api_get(8))
|
|
|
|
codec->pix_fmts = x265_csp_eight;
|
2013-10-02 17:46:26 +03: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-18 00:04:53 +02:00
|
|
|
{ "crf", "set the x265 crf", OFFSET(crf), AV_OPT_TYPE_FLOAT, { .dbl = -1 }, -1, FLT_MAX, VE },
|
2016-11-02 16:37:25 +02: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 17:46:26 +03: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 },
|
|
|
|
{ "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 04:49:05 +03:00
|
|
|
static const AVCodecDefault x265_defaults[] = {
|
|
|
|
{ "b", "0" },
|
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
2013-10-02 17:46:26 +03: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 04:49:05 +03:00
|
|
|
.defaults = x265_defaults,
|
2015-07-07 02:41:27 +02:00
|
|
|
.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS,
|
2013-10-02 17:46:26 +03:00
|
|
|
};
|