1 /*
2 * libx265 encoder
3 *
4 * Copyright (c) 2013-2014 Derek Buitenhuis
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #if defined(_MSC_VER)
24 #define X265_API_IMPORTS 1
25 #endif
26
27 #include <x265.h>
29
43
45 #if FF_API_REORDERED_OPAQUE
46 int64_t reordered_opaque;
47 #endif
49
52
55
58
62
70
75
78
79 /**
80 * If the encoder does not support ROI then warn the first time we
81 * encounter a frame with ROI side data.
82 */
85
87 {
88 switch (naltype) {
89 case NAL_UNIT_CODED_SLICE_BLA_W_LP:
90 case NAL_UNIT_CODED_SLICE_BLA_W_RADL:
91 case NAL_UNIT_CODED_SLICE_BLA_N_LP:
92 case NAL_UNIT_CODED_SLICE_IDR_W_RADL:
93 case NAL_UNIT_CODED_SLICE_IDR_N_LP:
94 case NAL_UNIT_CODED_SLICE_CRA:
95 return 1;
96 default:
97 return 0;
98 }
99 }
100
102 {
104
106 int idx;
107
108 for (
int i = 0;
i <
ctx->nb_rd;
i++)
109 if (!
ctx->rd[
i].in_use) {
110 ctx->rd[
i].in_use = 1;
112 }
113
118
121
123 ctx->rd[idx].in_use = 1;
124
125 return idx;
126 }
127
129 {
132 memset(&
ctx->rd[idx], 0,
sizeof(
ctx->rd[idx]));
133 }
134
136 {
138
139 ctx->api->param_free(
ctx->params);
141
142 for (
int i = 0;
i <
ctx->nb_rd;
i++)
145
147 ctx->api->encoder_close(
ctx->encoder);
148
149 return 0;
150 }
151
154 {
156 char buf[256];
157
159 if (
ctx->api->param_parse(
ctx->params,
key, buf) == X265_PARAM_BAD_VALUE) {
162 }
163
164 return 0;
165 }
166
169 {
171 char buf[256];
172
174 if (
ctx->api->param_parse(
ctx->params,
key, buf) == X265_PARAM_BAD_VALUE) {
177 }
178
179 return 0;
180 }
181
183 {
188
189 ctx->api = x265_api_get(
desc->comp[0].depth);
191 ctx->api = x265_api_get(0);
192
193 ctx->params =
ctx->api->param_alloc();
197 }
198
199 if (
ctx->api->param_default_preset(
ctx->params,
ctx->preset,
ctx->tune) < 0) {
201
204 for (
i = 0; x265_preset_names[
i];
i++)
206
209 for (
i = 0; x265_tune_names[
i];
i++)
211
213
215 }
216
221 } else {
224 }
225 ctx->params->sourceWidth = avctx->
width;
226 ctx->params->sourceHeight = avctx->
height;
229
230 /* Tune the CTU size based on input resolution. */
231 if (
ctx->params->sourceWidth < 64 ||
ctx->params->sourceHeight < 64)
232 ctx->params->maxCUSize = 32;
233 if (
ctx->params->sourceWidth < 32 ||
ctx->params->sourceHeight < 32)
234 ctx->params->maxCUSize = 16;
235 if (
ctx->params->sourceWidth < 16 ||
ctx->params->sourceHeight < 16) {
237 ctx->params->sourceWidth,
ctx->params->sourceHeight);
239 }
240
241
242 ctx->params->vui.bEnableVideoSignalTypePresentFlag = 1;
243
245 ctx->params->vui.bEnableVideoFullRangeFlag =
247 else
248 ctx->params->vui.bEnableVideoFullRangeFlag =
253
260
261 ctx->params->vui.bEnableColorDescriptionPresentFlag = 1;
262
263 // x265 validates the parameters internally
265 ctx->params->vui.transferCharacteristics = avctx->
color_trc;
266 #if X265_BUILD >= 159
268 ctx->params->preferredTransferCharacteristics =
ctx->params->vui.transferCharacteristics;
269 #endif
271 }
272
273 // chroma sample location values are to be ignored in case of non-4:2:0
274 // according to the specification, so we only write them out in case of
275 // 4:2:0 (log2_chroma_{w,h} == 1).
276 ctx->params->vui.bEnableChromaLocInfoPresentFlag =
278 desc->log2_chroma_w == 1 &&
desc->log2_chroma_h == 1;
279
280 if (
ctx->params->vui.bEnableChromaLocInfoPresentFlag) {
281 ctx->params->vui.chromaSampleLocTypeTopField =
282 ctx->params->vui.chromaSampleLocTypeBottomField =
284 }
285
287 char sar[12];
288 int sar_num, sar_den;
289
293 snprintf(sar,
sizeof(sar),
"%d:%d", sar_num, sar_den);
294 if (
ctx->api->param_parse(
ctx->params,
"sar", sar) == X265_PARAM_BAD_VALUE) {
297 }
298 }
299
300 switch (
desc->log2_chroma_w) {
301 // 4:4:4, RGB. gray
302 case 0:
303 // gray
304 if (
desc->nb_components == 1) {
305 if (
ctx->api->api_build_number < 85) {
307 "libx265 version is %d, must be at least 85 for gray encoding.\n",
308 ctx->api->api_build_number);
310 }
311 ctx->params->internalCsp = X265_CSP_I400;
312 break;
313 }
314
315 // set identity matrix for RGB
318 ctx->params->vui.bEnableVideoSignalTypePresentFlag = 1;
319 ctx->params->vui.bEnableColorDescriptionPresentFlag = 1;
320 }
321
322 ctx->params->internalCsp = X265_CSP_I444;
323 break;
324 // 4:2:0, 4:2:2
325 case 1:
326 ctx->params->internalCsp =
desc->log2_chroma_h == 1 ?
327 X265_CSP_I420 : X265_CSP_I422;
328 break;
329 default:
331 "Pixel format '%s' cannot be mapped to a libx265 CSP!\n",
334 }
335
337 char crf[6];
338
340 if (
ctx->api->param_parse(
ctx->params,
"crf", crf) == X265_PARAM_BAD_VALUE) {
343 }
346 ctx->params->rc.rateControlMode = X265_RC_ABR;
347 }
else if (
ctx->cqp >= 0) {
351 }
352
353 if (avctx->
qmin >= 0) {
357 }
358 if (avctx->
qmax >= 0) {
362 }
367 }
368 if (avctx->
qblur >= 0) {
372 }
377 }
382 }
387 }
388
391
393 if (!cpb_props)
398
400 ctx->params->bRepeatHeaders = 1;
401
406 }
411 }
416 }
417 if (avctx->
refs >= 0) {
421 }
422
423 {
426 int parse_ret =
ctx->api->param_parse(
ctx->params, en->
key, en->
value);
427
428 switch (parse_ret) {
429 case X265_PARAM_BAD_NAME:
431 "Unknown option: %s.\n", en->
key);
432 break;
433 case X265_PARAM_BAD_VALUE:
435 "Invalid value for %s: %s.\n", en->
key, en->
value);
436 break;
437 default:
438 break;
439 }
440 }
441 }
442
444 ctx->params->rc.vbvBufferInit == 0.9) {
446 }
447
449 if (
ctx->api->param_apply_profile(
ctx->params,
ctx->profile) < 0) {
453 for (
i = 0; x265_profile_names[
i];
i++)
457 }
458 }
459
460 ctx->encoder =
ctx->api->encoder_open(
ctx->params);
465 }
466
468 x265_nal *nal;
469 int nnal;
470
476 }
477
481 "Cannot allocate HEVC header of size %d.\n", avctx->
extradata_size);
484 }
485
488 }
489
490 return 0;
491 }
492
494 {
496 if (sd) {
497 if (
ctx->params->rc.aqMode == X265_AQ_NONE) {
498 if (!
ctx->roi_warned) {
501 }
502 } else {
503 /* 8x8 block when qg-size is 8, 16*16 block otherwise. */
504 int mb_size = (
ctx->params->rc.qgSize == 8) ? 8 : 16;
505 int mbx = (
frame->width + mb_size - 1) / mb_size;
506 int mby = (
frame->height + mb_size - 1) / mb_size;
507 int qp_range = 51 + 6 * (pic->bitDepth - 8);
508 int nb_rois;
510 uint32_t roi_size;
511 float *qoffsets; /* will be freed after encode is called. */
512
515 if (!roi_size || sd->
size % roi_size != 0) {
518 }
519 nb_rois = sd->
size / roi_size;
520
521 qoffsets =
av_calloc(mbx * mby,
sizeof(*qoffsets));
522 if (!qoffsets)
524
525 // This list must be iterated in reverse because the first
526 // region in the list applies when regions overlap.
527 for (
int i = nb_rois - 1;
i >= 0;
i--) {
528 int startx, endx, starty, endy;
529 float qoffset;
530
532
533 starty =
FFMIN(mby, roi->
top / mb_size);
534 endy =
FFMIN(mby, (roi->
bottom + mb_size - 1)/ mb_size);
535 startx =
FFMIN(mbx, roi->
left / mb_size);
536 endx =
FFMIN(mbx, (roi->
right + mb_size - 1)/ mb_size);
537
542 }
544 qoffset =
av_clipf(qoffset * qp_range, -qp_range, +qp_range);
545
546 for (int y = starty; y < endy; y++)
547 for (int x = startx; x < endx; x++)
548 qoffsets[x + y*mbx] = qoffset;
549 }
550
551 pic->quantOffsets = qoffsets;
552 }
553 }
554 return 0;
555 }
556
558 {
559 x265_sei *
sei = &pic->userSEI;
560 for (
int i = 0;
i <
sei->numPayloads;
i++)
562
563 if (pic->userData) {
564 int idx = (
int)(intptr_t)pic->userData - 1;
566 pic->userData =
NULL;
567 }
568
570 sei->numPayloads = 0;
571 }
572
574 const AVFrame *pic,
int *got_packet)
575 {
577 x265_picture x265pic;
578 x265_picture x265pic_out = { 0 };
579 x265_nal *nal;
581 uint8_t *dst;
582 int pict_type;
583 int payload = 0;
584 int nnal;
587
588 ctx->api->picture_init(
ctx->params, &x265pic);
589
590 sei = &x265pic.userSEI;
591 sei->numPayloads = 0;
592
593 if (pic) {
595 int rd_idx;
596
597 for (
i = 0;
i < 3;
i++) {
598 x265pic.planes[
i] = pic->
data[
i];
600 }
601
602 x265pic.pts = pic->
pts;
604
606 (
ctx->forced_idr ? X265_TYPE_IDR : X265_TYPE_I) :
609 X265_TYPE_AUTO;
610
614
616 if (rd_idx < 0) {
618 return rd_idx;
619 }
620 rd = &
ctx->rd[rd_idx];
621
623 #if FF_API_REORDERED_OPAQUE
627 #endif
635 }
636 }
637
638 x265pic.userData = (void*)(intptr_t)(rd_idx + 1);
639
641 void *sei_data;
642 size_t sei_size;
643
647 } else if (sei_data) {
649 x265_sei_payload *sei_payload;
650
653 (
sei->numPayloads + 1) *
sizeof(*sei_payload));
658 }
660 sei->payloads =
ctx->sei_data;
661 sei_payload = &
sei->payloads[
sei->numPayloads];
662 sei_payload->payload = sei_data;
663 sei_payload->payloadSize = sei_size;
666 }
667 }
668
673 x265_sei_payload *sei_payload;
674
676 continue;
677
680 (
sei->numPayloads + 1) *
sizeof(*sei_payload));
684 }
686 sei->payloads =
ctx->sei_data;
687 sei_payload = &
sei->payloads[
sei->numPayloads];
689 if (!sei_payload->payload) {
692 }
693 sei_payload->payloadSize = side_data->
size;
694 /* Equal to libx265 USER_DATA_UNREGISTERED */
697 }
698 }
699 }
700
701 ret =
ctx->api->encoder_encode(
ctx->encoder, &nal, &nnal,
702 pic ? &x265pic :
NULL, &x265pic_out);
703
704 for (
i = 0;
i <
sei->numPayloads;
i++)
707
710
711 if (!nnal)
712 return 0;
713
714 for (
i = 0;
i < nnal;
i++)
715 payload += nal[
i].sizeBytes;
716
721 }
723
724 for (
i = 0;
i < nnal;
i++) {
725 memcpy(dst, nal[
i].payload, nal[
i].sizeBytes);
726 dst += nal[
i].sizeBytes;
727
730 }
731
732 pkt->
pts = x265pic_out.pts;
733 pkt->
dts = x265pic_out.dts;
734
735 switch (x265pic_out.sliceType) {
736 case X265_TYPE_IDR:
737 case X265_TYPE_I:
739 break;
740 case X265_TYPE_P:
742 break;
743 case X265_TYPE_B:
744 case X265_TYPE_BREF:
746 break;
747 default:
750 }
751
752 #if X265_BUILD >= 130
753 if (x265pic_out.sliceType == X265_TYPE_B)
754 #else
755 if (x265pic_out.frameData.sliceType == 'b')
756 #endif
758
760
761 if (x265pic_out.userData) {
762 int idx = (
int)(intptr_t)x265pic_out.userData - 1;
764
765 #if FF_API_REORDERED_OPAQUE
767 avctx->reordered_opaque = rd->reordered_opaque;
769 #endif
771
776 }
777
779 }
780 #if FF_API_REORDERED_OPAQUE
781 else {
783 avctx->reordered_opaque = 0;
785 }
786 #endif
787
788 *got_packet = 1;
789 return 0;
790 }
791
802 };
803
819 };
820
841 };
842
844 {
845 if (x265_api_get(12))
847 else if (x265_api_get(10))
849 else if (x265_api_get(8))
851 }
852
853 #define OFFSET(x) offsetof(libx265Context, x)
854 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
858 {
"forced-idr",
"if forcing keyframes, force them as IDR frames",
OFFSET(forced_idr),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
VE },
862 {
"udu_sei",
"Use user data unregistered SEI if available",
OFFSET(udu_sei),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
VE },
864 {
"x265-params",
"set the x265 configuration using a :-separated list of key=value parameters",
OFFSET(x265_opts),
AV_OPT_TYPE_DICT, { 0 }, 0, 0,
VE },
866 };
867
873 };
874
876 { "b", "0" },
877 { "bf", "-1" },
878 { "g", "-1" },
879 { "keyint_min", "-1" },
880 { "refs", "-1" },
881 { "qmin", "-1" },
882 { "qmax", "-1" },
883 { "qdiff", "-1" },
884 { "qblur", "-1" },
885 { "qcomp", "-1" },
886 { "i_qfactor", "-1" },
887 { "b_qfactor", "-1" },
889 };
890
899 .p.priv_class = &class,
900 .p.wrapper_name = "libx265",
909 };