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 avctx->coded_frame = av_frame_alloc();
85 if (!avctx->coded_frame) {
86 av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
87 return AVERROR(ENOMEM);
90 ctx->params = x265_param_alloc();
92 av_log(avctx, AV_LOG_ERROR, "Could not allocate x265 param structure.\n");
93 return AVERROR(ENOMEM);
96 if (x265_param_default_preset(ctx->params, ctx->preset, ctx->tune) < 0) {
97 av_log(avctx, AV_LOG_ERROR, "Invalid preset or tune.\n");
98 return AVERROR(EINVAL);
101 ctx->params->frameNumThreads = avctx->thread_count;
102 ctx->params->frameRate = (int) (avctx->time_base.den / avctx->time_base.num);
103 ctx->params->sourceWidth = avctx->width;
104 ctx->params->sourceHeight = avctx->height;
105 ctx->params->inputBitDepth = av_pix_fmt_desc_get(avctx->pix_fmt)->comp[0].depth_minus1 + 1;
107 if (avctx->bit_rate > 0) {
108 ctx->params->rc.bitrate = avctx->bit_rate / 1000;
109 ctx->params->rc.rateControlMode = X265_RC_ABR;
112 if (ctx->x265_opts) {
113 AVDictionary *dict = NULL;
114 AVDictionaryEntry *en = NULL;
116 if (!av_dict_parse_string(&dict, ctx->x265_opts, "=", ":", 0)) {
117 while ((en = av_dict_get(dict, "", en, AV_DICT_IGNORE_SUFFIX))) {
118 int parse_ret = x265_param_parse(ctx->params, en->key, en->value);
121 case X265_PARAM_BAD_NAME:
122 av_log(avctx, AV_LOG_WARNING,
123 "Unknown option: %s.\n", en->key);
125 case X265_PARAM_BAD_VALUE:
126 av_log(avctx, AV_LOG_WARNING,
127 "Invalid value for %s: %s.\n", en->key, en->value);
137 ctx->encoder = x265_encoder_open(ctx->params);
139 av_log(avctx, AV_LOG_ERROR, "Cannot open libx265 encoder.\n");
140 libx265_encode_close(avctx);
141 return AVERROR_INVALIDDATA;
144 ret = x265_encoder_headers(ctx->encoder, &nal, &nnal);
146 av_log(avctx, AV_LOG_ERROR, "Cannot encode headers.\n");
147 libx265_encode_close(avctx);
148 return AVERROR_INVALIDDATA;
151 for (i = 0; i < nnal; i++)
152 ctx->header_size += nal[i].sizeBytes;
154 ctx->header = av_malloc(ctx->header_size);
156 av_log(avctx, AV_LOG_ERROR,
157 "Cannot allocate HEVC header of size %d.\n", ctx->header_size);
158 libx265_encode_close(avctx);
159 return AVERROR(ENOMEM);
163 for (i = 0; i < nnal; i++) {
164 memcpy(buf, nal[i].payload, nal[i].sizeBytes);
165 buf += nal[i].sizeBytes;
171 static int libx265_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
172 const AVFrame *pic, int *got_packet)
174 libx265Context *ctx = avctx->priv_data;
175 x265_picture x265pic;
176 x265_picture x265pic_out = { { 0 } };
184 x265_picture_init(ctx->params, &x265pic);
187 for (i = 0; i < 3; i++) {
188 x265pic.planes[i] = pic->data[i];
189 x265pic.stride[i] = pic->linesize[i];
192 x265pic.pts = pic->pts;
195 ret = x265_encoder_encode(ctx->encoder, &nal, &nnal,
196 pic ? &x265pic : NULL, &x265pic_out);
198 return AVERROR_UNKNOWN;
203 for (i = 0; i < nnal; i++)
204 payload += nal[i].sizeBytes;
206 payload += ctx->header_size;
208 ret = ff_alloc_packet(pkt, payload);
210 av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
216 memcpy(dst, ctx->header, ctx->header_size);
217 dst += ctx->header_size;
219 av_freep(&ctx->header);
220 ctx->header_size = 0;
223 for (i = 0; i < nnal; i++) {
224 memcpy(dst, nal[i].payload, nal[i].sizeBytes);
225 dst += nal[i].sizeBytes;
227 if (is_keyframe(nal[i].type))
228 pkt->flags |= AV_PKT_FLAG_KEY;
231 pkt->pts = x265pic_out.pts;
232 pkt->dts = x265pic_out.dts;
238 static const enum AVPixelFormat x265_csp_eight[] = {
243 static const enum AVPixelFormat x265_csp_twelve[] = {
245 AV_PIX_FMT_YUV420P10,
249 static av_cold void libx265_encode_init_csp(AVCodec *codec)
251 if (x265_max_bit_depth == 8)
252 codec->pix_fmts = x265_csp_eight;
253 else if (x265_max_bit_depth == 12)
254 codec->pix_fmts = x265_csp_twelve;
257 #define OFFSET(x) offsetof(libx265Context, x)
258 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
259 static const AVOption options[] = {
260 { "preset", "set the x265 preset", OFFSET(preset), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
261 { "tune", "set the x265 tune parameter", OFFSET(tune), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
262 { "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 },
266 static const AVClass class = {
267 .class_name = "libx265",
268 .item_name = av_default_item_name,
270 .version = LIBAVUTIL_VERSION_INT,
273 AVCodec ff_libx265_encoder = {
275 .long_name = NULL_IF_CONFIG_SMALL("libx265 H.265 / HEVC"),
276 .type = AVMEDIA_TYPE_VIDEO,
277 .id = AV_CODEC_ID_HEVC,
278 .init = libx265_encode_init,
279 .init_static_data = libx265_encode_init_csp,
280 .encode2 = libx265_encode_frame,
281 .close = libx265_encode_close,
282 .priv_data_size = sizeof(libx265Context),
283 .priv_class = &class,
284 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_AUTO_THREADS,