4 * Copyright (c) 2013-2014 Derek Buitenhuis
6 * This file is part of Libav.
8 * Libav is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * Libav is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "libavutil/internal.h"
26 #include "libavutil/common.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/pixdesc.h"
32 typedef struct libx265Context {
35 x265_encoder *encoder;
45 static int is_keyframe(NalUnitType naltype)
48 case NAL_UNIT_CODED_SLICE_BLA_W_LP:
49 case NAL_UNIT_CODED_SLICE_BLA_W_RADL:
50 case NAL_UNIT_CODED_SLICE_BLA_N_LP:
51 case NAL_UNIT_CODED_SLICE_IDR_W_RADL:
52 case NAL_UNIT_CODED_SLICE_IDR_N_LP:
53 case NAL_UNIT_CODED_SLICE_CRA:
60 static av_cold int libx265_encode_close(AVCodecContext *avctx)
62 libx265Context *ctx = avctx->priv_data;
64 av_frame_free(&avctx->coded_frame);
65 av_freep(&ctx->header);
67 x265_param_free(ctx->params);
70 x265_encoder_close(ctx->encoder);
75 static av_cold int libx265_encode_init(AVCodecContext *avctx)
77 libx265Context *ctx = avctx->priv_data;
84 if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL &&
85 !av_pix_fmt_desc_get(avctx->pix_fmt)->log2_chroma_w &&
86 !av_pix_fmt_desc_get(avctx->pix_fmt)->log2_chroma_h) {
87 av_log(avctx, AV_LOG_ERROR,
88 "4:4:4 support is not fully defined for HEVC yet. "
89 "Set -strict experimental to encode anyway.\n");
90 return AVERROR(ENOSYS);
93 avctx->coded_frame = av_frame_alloc();
94 if (!avctx->coded_frame) {
95 av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
96 return AVERROR(ENOMEM);
99 ctx->params = x265_param_alloc();
101 av_log(avctx, AV_LOG_ERROR, "Could not allocate x265 param structure.\n");
102 return AVERROR(ENOMEM);
105 if (x265_param_default_preset(ctx->params, ctx->preset, ctx->tune) < 0) {
106 av_log(avctx, AV_LOG_ERROR, "Invalid preset or tune.\n");
107 return AVERROR(EINVAL);
110 ctx->params->frameNumThreads = avctx->thread_count;
111 ctx->params->fpsNum = avctx->time_base.den;
112 ctx->params->fpsDenom = avctx->time_base.num * avctx->ticks_per_frame;
113 ctx->params->sourceWidth = avctx->width;
114 ctx->params->sourceHeight = avctx->height;
116 if (x265_max_bit_depth == 8)
117 ctx->params->internalBitDepth = 8;
118 else if (x265_max_bit_depth == 12)
119 ctx->params->internalBitDepth = 10;
121 switch (avctx->pix_fmt) {
122 case AV_PIX_FMT_YUV420P:
123 case AV_PIX_FMT_YUV420P10:
124 ctx->params->internalCsp = X265_CSP_I420;
126 case AV_PIX_FMT_YUV444P:
127 case AV_PIX_FMT_YUV444P10:
128 ctx->params->internalCsp = X265_CSP_I444;
132 if (avctx->bit_rate > 0) {
133 ctx->params->rc.bitrate = avctx->bit_rate / 1000;
134 ctx->params->rc.rateControlMode = X265_RC_ABR;
137 if (ctx->x265_opts) {
138 AVDictionary *dict = NULL;
139 AVDictionaryEntry *en = NULL;
141 if (!av_dict_parse_string(&dict, ctx->x265_opts, "=", ":", 0)) {
142 while ((en = av_dict_get(dict, "", en, AV_DICT_IGNORE_SUFFIX))) {
143 int parse_ret = x265_param_parse(ctx->params, en->key, en->value);
146 case X265_PARAM_BAD_NAME:
147 av_log(avctx, AV_LOG_WARNING,
148 "Unknown option: %s.\n", en->key);
150 case X265_PARAM_BAD_VALUE:
151 av_log(avctx, AV_LOG_WARNING,
152 "Invalid value for %s: %s.\n", en->key, en->value);
162 ctx->encoder = x265_encoder_open(ctx->params);
164 av_log(avctx, AV_LOG_ERROR, "Cannot open libx265 encoder.\n");
165 libx265_encode_close(avctx);
166 return AVERROR_INVALIDDATA;
169 ret = x265_encoder_headers(ctx->encoder, &nal, &nnal);
171 av_log(avctx, AV_LOG_ERROR, "Cannot encode headers.\n");
172 libx265_encode_close(avctx);
173 return AVERROR_INVALIDDATA;
176 for (i = 0; i < nnal; i++)
177 ctx->header_size += nal[i].sizeBytes;
179 ctx->header = av_malloc(ctx->header_size);
181 av_log(avctx, AV_LOG_ERROR,
182 "Cannot allocate HEVC header of size %d.\n", ctx->header_size);
183 libx265_encode_close(avctx);
184 return AVERROR(ENOMEM);
188 for (i = 0; i < nnal; i++) {
189 memcpy(buf, nal[i].payload, nal[i].sizeBytes);
190 buf += nal[i].sizeBytes;
196 static int libx265_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
197 const AVFrame *pic, int *got_packet)
199 libx265Context *ctx = avctx->priv_data;
200 x265_picture x265pic;
201 x265_picture x265pic_out = { { 0 } };
209 x265_picture_init(ctx->params, &x265pic);
212 for (i = 0; i < 3; i++) {
213 x265pic.planes[i] = pic->data[i];
214 x265pic.stride[i] = pic->linesize[i];
217 x265pic.pts = pic->pts;
218 x265pic.bitDepth = av_pix_fmt_desc_get(avctx->pix_fmt)->comp[0].depth_minus1 + 1;
221 ret = x265_encoder_encode(ctx->encoder, &nal, &nnal,
222 pic ? &x265pic : NULL, &x265pic_out);
224 return AVERROR_UNKNOWN;
229 for (i = 0; i < nnal; i++)
230 payload += nal[i].sizeBytes;
232 payload += ctx->header_size;
234 ret = ff_alloc_packet(pkt, payload);
236 av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
242 memcpy(dst, ctx->header, ctx->header_size);
243 dst += ctx->header_size;
245 av_freep(&ctx->header);
246 ctx->header_size = 0;
249 for (i = 0; i < nnal; i++) {
250 memcpy(dst, nal[i].payload, nal[i].sizeBytes);
251 dst += nal[i].sizeBytes;
253 if (is_keyframe(nal[i].type))
254 pkt->flags |= AV_PKT_FLAG_KEY;
257 pkt->pts = x265pic_out.pts;
258 pkt->dts = x265pic_out.dts;
264 static const enum AVPixelFormat x265_csp_eight[] = {
270 static const enum AVPixelFormat x265_csp_twelve[] = {
273 AV_PIX_FMT_YUV420P10,
274 AV_PIX_FMT_YUV444P10,
278 static av_cold void libx265_encode_init_csp(AVCodec *codec)
280 if (x265_max_bit_depth == 8)
281 codec->pix_fmts = x265_csp_eight;
282 else if (x265_max_bit_depth == 12)
283 codec->pix_fmts = x265_csp_twelve;
286 #define OFFSET(x) offsetof(libx265Context, x)
287 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
288 static const AVOption options[] = {
289 { "preset", "set the x265 preset", OFFSET(preset), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
290 { "tune", "set the x265 tune parameter", OFFSET(tune), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
291 { "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 },
295 static const AVClass class = {
296 .class_name = "libx265",
297 .item_name = av_default_item_name,
299 .version = LIBAVUTIL_VERSION_INT,
302 AVCodec ff_libx265_encoder = {
304 .long_name = NULL_IF_CONFIG_SMALL("libx265 H.265 / HEVC"),
305 .type = AVMEDIA_TYPE_VIDEO,
306 .id = AV_CODEC_ID_HEVC,
307 .init = libx265_encode_init,
308 .init_static_data = libx265_encode_init_csp,
309 .encode2 = libx265_encode_frame,
310 .close = libx265_encode_close,
311 .priv_data_size = sizeof(libx265Context),
312 .priv_class = &class,
313 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_AUTO_THREADS,