libx265: Support tiny video sizes
[libav.git] / libavcodec / libx265.c
CommitLineData
50ea9315
DB
1/*
2 * libx265 encoder
3 *
4 * Copyright (c) 2013-2014 Derek Buitenhuis
5 *
6 * This file is part of Libav.
7 *
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.
12 *
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.
17 *
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
21 */
22
180e6b48
DB
23#if defined(_MSC_VER)
24#define X265_API_IMPORTS 1
25#endif
26
50ea9315 27#include <x265.h>
d617e77c 28#include <float.h>
50ea9315
DB
29
30#include "libavutil/internal.h"
31#include "libavutil/common.h"
32#include "libavutil/opt.h"
33#include "libavutil/pixdesc.h"
34#include "avcodec.h"
35#include "internal.h"
36
37typedef struct libx265Context {
38 const AVClass *class;
39
40 x265_encoder *encoder;
41 x265_param *params;
3b5e5e6a 42 const x265_api *api;
50ea9315 43
d617e77c 44 float crf;
db0b3dcc 45 int forced_idr;
50ea9315
DB
46 char *preset;
47 char *tune;
48 char *x265_opts;
49} libx265Context;
50
51static int is_keyframe(NalUnitType naltype)
52{
53 switch (naltype) {
54 case NAL_UNIT_CODED_SLICE_BLA_W_LP:
55 case NAL_UNIT_CODED_SLICE_BLA_W_RADL:
56 case NAL_UNIT_CODED_SLICE_BLA_N_LP:
57 case NAL_UNIT_CODED_SLICE_IDR_W_RADL:
58 case NAL_UNIT_CODED_SLICE_IDR_N_LP:
59 case NAL_UNIT_CODED_SLICE_CRA:
60 return 1;
61 default:
62 return 0;
63 }
64}
65
66static av_cold int libx265_encode_close(AVCodecContext *avctx)
67{
68 libx265Context *ctx = avctx->priv_data;
69
3b5e5e6a 70 ctx->api->param_free(ctx->params);
50ea9315
DB
71
72 if (ctx->encoder)
3b5e5e6a 73 ctx->api->encoder_close(ctx->encoder);
50ea9315
DB
74
75 return 0;
76}
77
78static av_cold int libx265_encode_init(AVCodecContext *avctx)
79{
80 libx265Context *ctx = avctx->priv_data;
50ea9315 81
2268db2c 82 ctx->api = x265_api_get(av_pix_fmt_desc_get(avctx->pix_fmt)->comp[0].depth);
3b5e5e6a
GG
83 if (!ctx->api)
84 ctx->api = x265_api_get(0);
85
2142b2ef 86 if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL &&
61a34491 87 !av_pix_fmt_desc_get(avctx->pix_fmt)->log2_chroma_w) {
2142b2ef 88 av_log(avctx, AV_LOG_ERROR,
61a34491 89 "4:2:2 and 4:4:4 support is not fully defined for HEVC yet. "
2142b2ef
DB
90 "Set -strict experimental to encode anyway.\n");
91 return AVERROR(ENOSYS);
92 }
93
3b5e5e6a 94 ctx->params = ctx->api->param_alloc();
50ea9315
DB
95 if (!ctx->params) {
96 av_log(avctx, AV_LOG_ERROR, "Could not allocate x265 param structure.\n");
97 return AVERROR(ENOMEM);
98 }
99
3b5e5e6a 100 if (ctx->api->param_default_preset(ctx->params, ctx->preset, ctx->tune) < 0) {
bbdb50d7
JA
101 int i;
102
103 av_log(avctx, AV_LOG_ERROR, "Error setting preset/tune %s/%s.\n", ctx->preset, ctx->tune);
104 av_log(avctx, AV_LOG_INFO, "Possible presets:");
105 for (i = 0; x265_preset_names[i]; i++)
106 av_log(avctx, AV_LOG_INFO, " %s", x265_preset_names[i]);
107
108 av_log(avctx, AV_LOG_INFO, "\n");
109 av_log(avctx, AV_LOG_INFO, "Possible tunes:");
110 for (i = 0; x265_tune_names[i]; i++)
111 av_log(avctx, AV_LOG_INFO, " %s", x265_tune_names[i]);
112
113 av_log(avctx, AV_LOG_INFO, "\n");
114
50ea9315
DB
115 return AVERROR(EINVAL);
116 }
117
118 ctx->params->frameNumThreads = avctx->thread_count;
d00a504b
DB
119 ctx->params->fpsNum = avctx->time_base.den;
120 ctx->params->fpsDenom = avctx->time_base.num * avctx->ticks_per_frame;
50ea9315
DB
121 ctx->params->sourceWidth = avctx->width;
122 ctx->params->sourceHeight = avctx->height;
7c6eb0a1 123 ctx->params->bEnablePsnr = !!(avctx->flags & AV_CODEC_FLAG_PSNR);
d00a504b 124
cc06f7bd
VG
125 /* Tune the CTU size based on input resolution. */
126 if (ctx->params->sourceWidth < 64 || ctx->params->sourceHeight < 64)
127 ctx->params->maxCUSize = 32;
128 if (ctx->params->sourceWidth < 32 || ctx->params->sourceHeight < 32)
129 ctx->params->maxCUSize = 16;
130 if (ctx->params->sourceWidth < 16 || ctx->params->sourceHeight < 16) {
131 av_log(avctx, AV_LOG_ERROR, "Image size is too small (%dx%d).\n",
132 ctx->params->sourceWidth, ctx->params->sourceHeight);
133 return AVERROR(EINVAL);
134 }
135
ec89f35e
LB
136 if ((avctx->color_primaries <= AVCOL_PRI_BT2020 &&
137 avctx->color_primaries != AVCOL_PRI_UNSPECIFIED) ||
138 (avctx->color_trc <= AVCOL_TRC_BT2020_12 &&
139 avctx->color_trc != AVCOL_TRC_UNSPECIFIED) ||
140 (avctx->colorspace <= AVCOL_SPC_BT2020_CL &&
141 avctx->colorspace != AVCOL_SPC_UNSPECIFIED)) {
142
143 ctx->params->vui.bEnableVideoSignalTypePresentFlag = 1;
144 ctx->params->vui.bEnableColorDescriptionPresentFlag = 1;
145
146 // x265 validates the parameters internally
147 ctx->params->vui.colorPrimaries = avctx->color_primaries;
148 ctx->params->vui.transferCharacteristics = avctx->color_trc;
149 ctx->params->vui.matrixCoeffs = avctx->colorspace;
150 }
151
25fbfeed 152 if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
2de887e4
DB
153 char sar[12];
154 int sar_num, sar_den;
155
25fbfeed
DB
156 av_reduce(&sar_num, &sar_den,
157 avctx->sample_aspect_ratio.num,
158 avctx->sample_aspect_ratio.den, 65535);
159 snprintf(sar, sizeof(sar), "%d:%d", sar_num, sar_den);
3b5e5e6a 160 if (ctx->api->param_parse(ctx->params, "sar", sar) == X265_PARAM_BAD_VALUE) {
25fbfeed
DB
161 av_log(avctx, AV_LOG_ERROR, "Invalid SAR: %d:%d.\n", sar_num, sar_den);
162 return AVERROR_INVALIDDATA;
163 }
4a222645 164 }
0f7fa48c 165
2142b2ef
DB
166 switch (avctx->pix_fmt) {
167 case AV_PIX_FMT_YUV420P:
168 case AV_PIX_FMT_YUV420P10:
169 ctx->params->internalCsp = X265_CSP_I420;
170 break;
61a34491
DB
171 case AV_PIX_FMT_YUV422P:
172 case AV_PIX_FMT_YUV422P10:
173 ctx->params->internalCsp = X265_CSP_I422;
174 break;
2142b2ef
DB
175 case AV_PIX_FMT_YUV444P:
176 case AV_PIX_FMT_YUV444P10:
177 ctx->params->internalCsp = X265_CSP_I444;
178 break;
179 }
180
d617e77c
DB
181 if (ctx->crf >= 0) {
182 char crf[6];
183
184 snprintf(crf, sizeof(crf), "%2.2f", ctx->crf);
3b5e5e6a 185 if (ctx->api->param_parse(ctx->params, "crf", crf) == X265_PARAM_BAD_VALUE) {
d617e77c
DB
186 av_log(avctx, AV_LOG_ERROR, "Invalid crf: %2.2f.\n", ctx->crf);
187 return AVERROR(EINVAL);
188 }
189 } else if (avctx->bit_rate > 0) {
50ea9315
DB
190 ctx->params->rc.bitrate = avctx->bit_rate / 1000;
191 ctx->params->rc.rateControlMode = X265_RC_ABR;
192 }
193
7c6eb0a1 194 if (!(avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER))
c042cbc3
MG
195 ctx->params->bRepeatHeaders = 1;
196
50ea9315
DB
197 if (ctx->x265_opts) {
198 AVDictionary *dict = NULL;
199 AVDictionaryEntry *en = NULL;
200
201 if (!av_dict_parse_string(&dict, ctx->x265_opts, "=", ":", 0)) {
202 while ((en = av_dict_get(dict, "", en, AV_DICT_IGNORE_SUFFIX))) {
3b5e5e6a 203 int parse_ret = ctx->api->param_parse(ctx->params, en->key, en->value);
50ea9315
DB
204
205 switch (parse_ret) {
206 case X265_PARAM_BAD_NAME:
207 av_log(avctx, AV_LOG_WARNING,
208 "Unknown option: %s.\n", en->key);
209 break;
210 case X265_PARAM_BAD_VALUE:
211 av_log(avctx, AV_LOG_WARNING,
212 "Invalid value for %s: %s.\n", en->key, en->value);
213 break;
214 default:
215 break;
216 }
217 }
218 av_dict_free(&dict);
219 }
220 }
221
3b5e5e6a 222 ctx->encoder = ctx->api->encoder_open(ctx->params);
50ea9315
DB
223 if (!ctx->encoder) {
224 av_log(avctx, AV_LOG_ERROR, "Cannot open libx265 encoder.\n");
225 libx265_encode_close(avctx);
226 return AVERROR_INVALIDDATA;
227 }
228
7c6eb0a1 229 if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
2de887e4
DB
230 x265_nal *nal;
231 int nnal;
232
3b5e5e6a 233 avctx->extradata_size = ctx->api->encoder_headers(ctx->encoder, &nal, &nnal);
c042cbc3
MG
234 if (avctx->extradata_size <= 0) {
235 av_log(avctx, AV_LOG_ERROR, "Cannot encode headers.\n");
236 libx265_encode_close(avctx);
237 return AVERROR_INVALIDDATA;
238 }
60728e8b 239
059a9348 240 avctx->extradata = av_malloc(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
c042cbc3
MG
241 if (!avctx->extradata) {
242 av_log(avctx, AV_LOG_ERROR,
243 "Cannot allocate HEVC header of size %d.\n", avctx->extradata_size);
244 libx265_encode_close(avctx);
245 return AVERROR(ENOMEM);
246 }
247
248 memcpy(avctx->extradata, nal[0].payload, avctx->extradata_size);
60728e8b
MN
249 }
250
50ea9315
DB
251 return 0;
252}
253
254static int libx265_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
255 const AVFrame *pic, int *got_packet)
256{
257 libx265Context *ctx = avctx->priv_data;
258 x265_picture x265pic;
04070dbc 259 x265_picture x265pic_out = { 0 };
50ea9315
DB
260 x265_nal *nal;
261 uint8_t *dst;
262 int payload = 0;
263 int nnal;
264 int ret;
265 int i;
266
3b5e5e6a 267 ctx->api->picture_init(ctx->params, &x265pic);
41836c4e 268
50ea9315
DB
269 if (pic) {
270 for (i = 0; i < 3; i++) {
271 x265pic.planes[i] = pic->data[i];
272 x265pic.stride[i] = pic->linesize[i];
273 }
274
d00a504b 275 x265pic.pts = pic->pts;
2268db2c 276 x265pic.bitDepth = av_pix_fmt_desc_get(avctx->pix_fmt)->comp[0].depth;
6341ab0a 277
db0b3dcc
DB
278 x265pic.sliceType = pic->pict_type == AV_PICTURE_TYPE_I ?
279 (ctx->forced_idr ? X265_TYPE_IDR : X265_TYPE_I) :
6341ab0a
DB
280 pic->pict_type == AV_PICTURE_TYPE_P ? X265_TYPE_P :
281 pic->pict_type == AV_PICTURE_TYPE_B ? X265_TYPE_B :
282 X265_TYPE_AUTO;
50ea9315
DB
283 }
284
3b5e5e6a
GG
285 ret = ctx->api->encoder_encode(ctx->encoder, &nal, &nnal,
286 pic ? &x265pic : NULL, &x265pic_out);
50ea9315
DB
287 if (ret < 0)
288 return AVERROR_UNKNOWN;
289
290 if (!nnal)
291 return 0;
292
293 for (i = 0; i < nnal; i++)
294 payload += nal[i].sizeBytes;
295
50ea9315
DB
296 ret = ff_alloc_packet(pkt, payload);
297 if (ret < 0) {
298 av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
299 return ret;
300 }
301 dst = pkt->data;
302
50ea9315
DB
303 for (i = 0; i < nnal; i++) {
304 memcpy(dst, nal[i].payload, nal[i].sizeBytes);
305 dst += nal[i].sizeBytes;
306
307 if (is_keyframe(nal[i].type))
308 pkt->flags |= AV_PKT_FLAG_KEY;
309 }
310
311 pkt->pts = x265pic_out.pts;
312 pkt->dts = x265pic_out.dts;
313
40cf1bba
VG
314#if FF_API_CODED_FRAME
315FF_DISABLE_DEPRECATION_WARNINGS
31048d48
MN
316 switch (x265pic_out.sliceType) {
317 case X265_TYPE_IDR:
318 case X265_TYPE_I:
319 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
320 break;
321 case X265_TYPE_P:
322 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_P;
323 break;
324 case X265_TYPE_B:
325 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_B;
326 break;
327 }
40cf1bba
VG
328FF_ENABLE_DEPRECATION_WARNINGS
329#endif
31048d48 330
50ea9315
DB
331 *got_packet = 1;
332 return 0;
333}
334
335static const enum AVPixelFormat x265_csp_eight[] = {
336 AV_PIX_FMT_YUV420P,
61a34491 337 AV_PIX_FMT_YUV422P,
2142b2ef 338 AV_PIX_FMT_YUV444P,
50ea9315
DB
339 AV_PIX_FMT_NONE
340};
341
342static const enum AVPixelFormat x265_csp_twelve[] = {
343 AV_PIX_FMT_YUV420P,
61a34491 344 AV_PIX_FMT_YUV422P,
2142b2ef 345 AV_PIX_FMT_YUV444P,
50ea9315 346 AV_PIX_FMT_YUV420P10,
61a34491 347 AV_PIX_FMT_YUV422P10,
2142b2ef 348 AV_PIX_FMT_YUV444P10,
50ea9315
DB
349 AV_PIX_FMT_NONE
350};
351
352static av_cold void libx265_encode_init_csp(AVCodec *codec)
353{
354 if (x265_max_bit_depth == 8)
355 codec->pix_fmts = x265_csp_eight;
356 else if (x265_max_bit_depth == 12)
357 codec->pix_fmts = x265_csp_twelve;
358}
359
360#define OFFSET(x) offsetof(libx265Context, x)
361#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
362static const AVOption options[] = {
d617e77c 363 { "crf", "set the x265 crf", OFFSET(crf), AV_OPT_TYPE_FLOAT, { .dbl = -1 }, -1, FLT_MAX, VE },
db0b3dcc 364 { "forced-idr", "if forcing keyframes, force them as IDR frames", OFFSET(forced_idr),AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
50ea9315
DB
365 { "preset", "set the x265 preset", OFFSET(preset), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
366 { "tune", "set the x265 tune parameter", OFFSET(tune), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
367 { "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 },
368 { NULL }
369};
370
371static const AVClass class = {
372 .class_name = "libx265",
373 .item_name = av_default_item_name,
374 .option = options,
375 .version = LIBAVUTIL_VERSION_INT,
376};
377
4912b634
LB
378static const AVCodecDefault x265_defaults[] = {
379 { "b", "0" },
380 { NULL },
381};
382
50ea9315
DB
383AVCodec ff_libx265_encoder = {
384 .name = "libx265",
385 .long_name = NULL_IF_CONFIG_SMALL("libx265 H.265 / HEVC"),
386 .type = AVMEDIA_TYPE_VIDEO,
387 .id = AV_CODEC_ID_HEVC,
388 .init = libx265_encode_init,
389 .init_static_data = libx265_encode_init_csp,
390 .encode2 = libx265_encode_frame,
391 .close = libx265_encode_close,
392 .priv_data_size = sizeof(libx265Context),
393 .priv_class = &class,
4912b634 394 .defaults = x265_defaults,
def97856 395 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS,
47687a2f 396 .wrapper_name = "libx265",
50ea9315 397};