libx265 supports these natively, we just need to attach the generated NALs to the x265picture, as well as setting the appropriate DV profile.
Showing the changes to libavcodec (1 of 2 files). Show all
Unified · Split
| @@ -36,6 +36,7 @@ · 35 unmodified lines | |||
| 36 | #include "libavutil/pixdesc.h" | 36 | #include "libavutil/pixdesc.h" |
| 37 | #include "avcodec.h" | 37 | #include "avcodec.h" |
| 38 | #include "codec_internal.h" | 38 | #include "codec_internal.h" |
| 39 | #include "dovi_rpu.h" | ||
| 39 | #include "encode.h" | 40 | #include "encode.h" |
| 40 | #include "packet_internal.h" | 41 | #include "packet_internal.h" |
| 41 | #include "atsc_a53.h" | 42 | #include "atsc_a53.h" |
| @@ -78,6 +79,8 @@ typedef struct libx265Context { · 36 unmodified lines | |||
| 78 | * encounter a frame with ROI side data. | 79 | * encounter a frame with ROI side data. |
| 79 | */ | 80 | */ |
| 80 | int roi_warned; | 81 | int roi_warned; |
| 82 | |||
| 83 | DOVIContext dovi; | ||
| 81 | } libx265Context; | 84 | } libx265Context; |
| 82 | 85 | ||
| 83 | static int is_keyframe(NalUnitType naltype) | 86 | static int is_keyframe(NalUnitType naltype) |
| @@ -143,6 +146,8 @@ static av_cold int libx265_encode_close(AVCodecContext *avctx) · 59 unmodified lines | |||
| 143 | if (ctx->encoder) | 146 | if (ctx->encoder) |
| 144 | ctx->api->encoder_close(ctx->encoder); | 147 | ctx->api->encoder_close(ctx->encoder); |
| 145 | 148 | ||
| 149 | ff_dovi_ctx_unref(&ctx->dovi); | ||
| 150 | |||
| 146 | return 0; | 151 | return 0; |
| 147 | } | 152 | } |
| 148 | 153 | ||
| @@ -526,6 +531,14 @@ FF_ENABLE_DEPRECATION_WARNINGS · 377 unmodified lines | |||
| 526 | } | 531 | } |
| 527 | } | 532 | } |
| 528 | 533 | ||
| 534 | #if X265_BUILD >= 167 | ||
| 535 | ctx->dovi.logctx = avctx; | ||
| 536 | if ((ret = ff_dovi_configure(&ctx->dovi, avctx)) < 0) | ||
| 537 | return ret; | ||
| 538 | ctx->params->dolbyProfile = ctx->dovi.cfg.dv_profile * 10 + | ||
| 539 | ctx->dovi.cfg.dv_bl_signal_compatibility_id; | ||
| 540 | #endif | ||
| 541 | |||
| 529 | ctx->encoder = ctx->api->encoder_open(ctx->params); | 542 | ctx->encoder = ctx->api->encoder_open(ctx->params); |
| 530 | if (!ctx->encoder) { | 543 | if (!ctx->encoder) { |
| 531 | av_log(avctx, AV_LOG_ERROR, "Cannot open libx265 encoder.\n"); | 544 | av_log(avctx, AV_LOG_ERROR, "Cannot open libx265 encoder.\n"); |
| @@ -629,6 +642,10 @@ static void free_picture(libx265Context *ctx, x265_picture *pic) · 97 unmodified lines | |||
| 629 | for (int i = 0; i < sei->numPayloads; i++) | 642 | for (int i = 0; i < sei->numPayloads; i++) |
| 630 | av_free(sei->payloads[i].payload); | 643 | av_free(sei->payloads[i].payload); |
| 631 | 644 | ||
| 645 | #if X265_BUILD >= 167 | ||
| 646 | av_free(pic->rpu.payload); | ||
| 647 | #endif | ||
| 648 | |||
| 632 | if (pic->userData) { | 649 | if (pic->userData) { |
| 633 | int idx = (int)(intptr_t)pic->userData - 1; | 650 | int idx = (int)(intptr_t)pic->userData - 1; |
| 634 | rd_release(ctx, idx); | 651 | rd_release(ctx, idx); |
| @@ -660,6 +677,7 @@ static int libx265_encode_frame(AVCodecContext *avctx, AVPacket *pkt, · 25 unmodified lines | |||
| 660 | sei->numPayloads = 0; | 677 | sei->numPayloads = 0; |
| 661 | 678 | ||
| 662 | if (pic) { | 679 | if (pic) { |
| 680 | AVFrameSideData *sd; | ||
| 663 | ReorderedData *rd; | 681 | ReorderedData *rd; |
| 664 | int rd_idx; | 682 | int rd_idx; |
| 665 | 683 | ||
| @@ -760,6 +778,24 @@ static int libx265_encode_frame(AVCodecContext *avctx, AVPacket *pkt, · 94 unmodified lines | |||
| 760 | sei->numPayloads++; | 778 | sei->numPayloads++; |
| 761 | } | 779 | } |
| 762 | } | 780 | } |
| 781 | |||
| 782 | #if X265_BUILD >= 167 | ||
| 783 | sd = av_frame_get_side_data(pic, AV_FRAME_DATA_DOVI_METADATA); | ||
| 784 | if (ctx->dovi.cfg.dv_profile && sd) { | ||
| 785 | const AVDOVIMetadata *metadata = (const AVDOVIMetadata *)sd->data; | ||
| 786 | ret = ff_dovi_rpu_generate(&ctx->dovi, metadata, &x265pic.rpu.payload, | ||
| 787 | &x265pic.rpu.payloadSize); | ||
| 788 | if (ret < 0) { | ||
| 789 | free_picture(ctx, &x265pic); | ||
| 790 | return ret; | ||
| 791 | } | ||
| 792 | } else if (ctx->dovi.cfg.dv_profile) { | ||
| 793 | av_log(avctx, AV_LOG_ERROR, "Dolby Vision enabled, but received frame " | ||
| 794 | "without AV_FRAME_DATA_DOVI_METADATA"); | ||
| 795 | free_picture(ctx, &x265pic); | ||
| 796 | return AVERROR_INVALIDDATA; | ||
| 797 | } | ||
| 798 | #endif | ||
| 763 | } | 799 | } |
| 764 | 800 | ||
| 765 | ret = ctx->api->encoder_encode(ctx->encoder, &nal, &nnal, | 801 | ret = ctx->api->encoder_encode(ctx->encoder, &nal, &nnal, |
| @@ -914,6 +950,10 @@ static const AVOption options[] = { · 148 unmodified lines | |||
| 914 | { "udu_sei", "Use user data unregistered SEI if available", OFFSET(udu_sei), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, | 950 | { "udu_sei", "Use user data unregistered SEI if available", OFFSET(udu_sei), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, |
| 915 | { "a53cc", "Use A53 Closed Captions (if available)", OFFSET(a53_cc), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, VE }, | 951 | { "a53cc", "Use A53 Closed Captions (if available)", OFFSET(a53_cc), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, VE }, |
| 916 | { "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 }, | 952 | { "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 }, |
| 953 | #if X265_BUILD >= 167 | ||
| 954 | { "dolbyvision", "Enable Dolby Vision RPU coding", OFFSET(dovi.enable), AV_OPT_TYPE_BOOL, {.i64 = FF_DOVI_AUTOMATIC }, -1, 1, VE, .unit = "dovi" }, | ||
| 955 | { "auto", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_DOVI_AUTOMATIC}, .flags = VE, .unit = "dovi" }, | ||
| 956 | #endif | ||
| 917 | { NULL } | 957 | { NULL } |
| 918 | }; | 958 | }; |
| 919 | 959 | ||