Skip to content

Commit 17e4746

Browse files
committed
avcodec/libx265: add alpha layer encoding support
1 parent fce0622 commit 17e4746

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

Changelog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ version <next>:
66
- VVC VAAPI decoder
77
- RealVideo 6.0 decoder
88
- OpenMAX encoders deprecated
9+
- libx265 alpha layer encoding
910

1011
version 7.1:
1112
- Raw Captions with Time (RCWT) closed caption demuxer

libavcodec/libx265.c

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@
4242
#include "atsc_a53.h"
4343
#include "sei.h"
4444

45+
#if defined(X265_ENABLE_ALPHA) && MAX_LAYERS > 2
46+
#define FF_X265_MAX_LAYERS MAX_LAYERS
47+
#elif X265_BUILD >= 210
48+
#define FF_X265_MAX_LAYERS 2
49+
#else
50+
#define FF_X265_MAX_LAYERS 1
51+
#endif
52+
4553
typedef struct ReorderedData {
4654
int64_t duration;
4755

@@ -539,6 +547,15 @@ FF_ENABLE_DEPRECATION_WARNINGS
539547
ctx->dovi.cfg.dv_bl_signal_compatibility_id;
540548
#endif
541549

550+
#if X265_BUILD >= 210 && FF_X265_MAX_LAYERS > 1
551+
if (desc->flags & AV_PIX_FMT_FLAG_ALPHA) {
552+
if (ctx->api->param_parse(ctx->params, "alpha", "1") < 0) {
553+
av_log(avctx, AV_LOG_ERROR, "Loaded libx265 does not support alpha layer encoding.\n");
554+
return AVERROR(ENOTSUP);
555+
}
556+
}
557+
#endif
558+
542559
ctx->encoder = ctx->api->encoder_open(ctx->params);
543560
if (!ctx->encoder) {
544561
av_log(avctx, AV_LOG_ERROR, "Cannot open libx265 encoder.\n");
@@ -659,15 +676,13 @@ static void free_picture(libx265Context *ctx, x265_picture *pic)
659676
static int libx265_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
660677
const AVFrame *pic, int *got_packet)
661678
{
679+
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
662680
libx265Context *ctx = avctx->priv_data;
663681
x265_picture x265pic;
682+
x265_picture x265pic_out[FF_X265_MAX_LAYERS] = { 0 };
664683
#if (X265_BUILD >= 210) && (X265_BUILD < 213)
665-
x265_picture x265pic_layers_out[MAX_SCALABLE_LAYERS];
666-
x265_picture* x265pic_lyrptr_out[MAX_SCALABLE_LAYERS];
667-
#else
668-
x265_picture x265pic_solo_out = { 0 };
684+
x265_picture *x265pic_lyrptr_out[FF_X265_MAX_LAYERS];
669685
#endif
670-
x265_picture* x265pic_out;
671686
x265_nal *nal;
672687
x265_sei *sei;
673688
uint8_t *dst;
@@ -687,7 +702,7 @@ static int libx265_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
687702
ReorderedData *rd;
688703
int rd_idx;
689704

690-
for (i = 0; i < 3; i++) {
705+
for (i = 0; i < desc->nb_components; i++) {
691706
x265pic.planes[i] = pic->data[i];
692707
x265pic.stride[i] = pic->linesize[i];
693708
}
@@ -806,14 +821,14 @@ static int libx265_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
806821
}
807822

808823
#if (X265_BUILD >= 210) && (X265_BUILD < 213)
809-
for (i = 0; i < MAX_SCALABLE_LAYERS; i++)
810-
x265pic_lyrptr_out[i] = &x265pic_layers_out[i];
824+
for (i = 0; i < FF_ARRAY_ELEMS(x265pic_out); i++)
825+
x265pic_lyrptr_out[i] = &x265pic_out[i];
811826

812827
ret = ctx->api->encoder_encode(ctx->encoder, &nal, &nnal,
813828
pic ? &x265pic : NULL, x265pic_lyrptr_out);
814829
#else
815830
ret = ctx->api->encoder_encode(ctx->encoder, &nal, &nnal,
816-
pic ? &x265pic : NULL, &x265pic_solo_out);
831+
pic ? &x265pic : NULL, x265pic_out);
817832
#endif
818833

819834
for (i = 0; i < sei->numPayloads; i++)
@@ -844,12 +859,6 @@ static int libx265_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
844859
pkt->flags |= AV_PKT_FLAG_KEY;
845860
}
846861

847-
#if (X265_BUILD >= 210) && (X265_BUILD < 213)
848-
x265pic_out = x265pic_lyrptr_out[0];
849-
#else
850-
x265pic_out = &x265pic_solo_out;
851-
#endif
852-
853862
pkt->pts = x265pic_out->pts;
854863
pkt->dts = x265pic_out->dts;
855864

@@ -907,6 +916,9 @@ static const enum AVPixelFormat x265_csp_eight[] = {
907916
AV_PIX_FMT_YUVJ444P,
908917
AV_PIX_FMT_GBRP,
909918
AV_PIX_FMT_GRAY8,
919+
#if X265_BUILD >= 210 && FF_X265_MAX_LAYERS > 1
920+
AV_PIX_FMT_YUVA420P,
921+
#endif
910922
AV_PIX_FMT_NONE
911923
};
912924

@@ -924,6 +936,10 @@ static const enum AVPixelFormat x265_csp_ten[] = {
924936
AV_PIX_FMT_GBRP10,
925937
AV_PIX_FMT_GRAY8,
926938
AV_PIX_FMT_GRAY10,
939+
#if X265_BUILD >= 210 && FF_X265_MAX_LAYERS > 1
940+
AV_PIX_FMT_YUVA420P,
941+
AV_PIX_FMT_YUVA420P10,
942+
#endif
927943
AV_PIX_FMT_NONE
928944
};
929945

@@ -946,6 +962,10 @@ static const enum AVPixelFormat x265_csp_twelve[] = {
946962
AV_PIX_FMT_GRAY8,
947963
AV_PIX_FMT_GRAY10,
948964
AV_PIX_FMT_GRAY12,
965+
#if X265_BUILD >= 210 && FF_X265_MAX_LAYERS > 1
966+
AV_PIX_FMT_YUVA420P,
967+
AV_PIX_FMT_YUVA420P10,
968+
#endif
949969
AV_PIX_FMT_NONE
950970
};
951971

libavcodec/version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include "version_major.h"
3131

3232
#define LIBAVCODEC_VERSION_MINOR 27
33-
#define LIBAVCODEC_VERSION_MICRO 100
33+
#define LIBAVCODEC_VERSION_MICRO 101
3434

3535
#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
3636
LIBAVCODEC_VERSION_MINOR, \

0 commit comments

Comments
 (0)