[FFmpeg-devel] [PATCH V1 1/2] lavc/svt_hevc: add libsvt hevc encoder wrapper.

James Almer jamrial at gmail.com
Sun Dec 2 16:19:07 EET 2018


On 12/2/2018 2:38 AM, Jun Zhao wrote:
> base on patch by Huang, Zhengxu from https://github.com/intel/SVT-HEVC
> 
> Signed-off-by: Huang, Zhengxu <zhengxu.huang at intel.com>
> Signed-off-by: hassene <hassene.tmar at intel.com>
> Signed-off-by: Jun Zhao <jun.zhao at intel.com>
> ---
>  configure                |    4 +
>  libavcodec/Makefile      |    1 +
>  libavcodec/allcodecs.c   |    1 +
>  libavcodec/libsvt_hevc.c |  370 ++++++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 376 insertions(+), 0 deletions(-)
>  create mode 100644 libavcodec/libsvt_hevc.c
> 
> diff --git a/configure b/configure
> index 54b7e11..c3dca73 100755
> --- a/configure
> +++ b/configure
> @@ -263,6 +263,7 @@ External library support:
>    --enable-libspeex        enable Speex de/encoding via libspeex [no]
>    --enable-libsrt          enable Haivision SRT protocol via libsrt [no]
>    --enable-libssh          enable SFTP protocol via libssh [no]
> +  --enable-libsvt          enable HEVC encoding via svt [no]
>    --enable-libtensorflow   enable TensorFlow as a DNN module backend
>                             for DNN based filters like sr [no]
>    --enable-libtesseract    enable Tesseract, needed for ocr filter [no]
> @@ -1665,6 +1666,7 @@ EXTERNAL_LIBRARY_GPL_LIST="
>      libcdio
>      libdavs2
>      librubberband
> +    libsvt
>      libvidstab
>      libx264
>      libx265
> @@ -3087,6 +3089,7 @@ pcm_mulaw_at_encoder_select="audio_frame_queue"
>  chromaprint_muxer_deps="chromaprint"
>  h264_videotoolbox_encoder_deps="pthreads"
>  h264_videotoolbox_encoder_select="videotoolbox_encoder"
> +hevc_svt_encoder_deps="libsvt"

Since this is an external library based encoder, the correct name would
be libsvt_encoder.
You can also make it libsvt_hevc_encoder instead, which is what you used
as AVCodec.name.

>  hevc_videotoolbox_encoder_deps="pthreads"
>  hevc_videotoolbox_encoder_select="videotoolbox_encoder"
>  libaom_av1_decoder_deps="libaom"


[...]

> +static av_cold int eb_enc_init(AVCodecContext *avctx)
> +{
> +    SvtContext   *q = avctx->priv_data;
> +    SvtEncoder   *svt_enc = NULL;
> +    EB_ERRORTYPE ret = EB_ErrorNone;
> +
> +    q->svt_enc  = av_mallocz(sizeof(*q->svt_enc));
> +    if (!q->svt_enc)
> +        return AVERROR(ENOMEM);
> +    svt_enc = q->svt_enc;
> +
> +    q->eos_flag = 0;
> +
> +    ret = EbInitHandle(&svt_enc->svt_handle, q, &svt_enc->enc_params);
> +    if (ret != EB_ErrorNone)
> +        goto failed_init;
> +
> +    ret = config_enc_params(&svt_enc->enc_params, avctx);
> +    if (ret != EB_ErrorNone)
> +        goto failed_init;
> +
> +    ret = EbH265EncSetParameter(svt_enc->svt_handle, &svt_enc->enc_params);
> +    if (ret != EB_ErrorNone)
> +        goto failed_init;
> +
> +    ret = EbInitEncoder(svt_enc->svt_handle);
> +    if (ret != EB_ErrorNone)
> +        goto failed_init;
> +
> +    if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
> +
> +        EB_BUFFERHEADERTYPE headerPtr;
> +        headerPtr.nSize       = sizeof(EB_BUFFERHEADERTYPE);
> +        headerPtr.nFilledLen  = 0;
> +        headerPtr.pBuffer     = av_malloc(10 * 1024 * 1024);
> +        headerPtr.nAllocLen   = (10 * 1024 * 1024);
> +
> +        if (!headerPtr.pBuffer)
> +            return AVERROR(ENOMEM);
> +
> +        ret = EbH265EncStreamHeader(svt_enc->svt_handle, &headerPtr);
> +        if (ret != EB_ErrorNone) {
> +            av_freep(&headerPtr.pBuffer);
> +            goto failed_init;
> +        }
> +        avctx->extradata_size = headerPtr.nFilledLen;
> +        avctx->extradata = av_malloc(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
> +        if (!avctx->extradata) {
> +            av_log(avctx, AV_LOG_ERROR,
> +                   "Cannot allocate HEVC header of size %d.\n", avctx->extradata_size);
> +            return AVERROR(ENOMEM);
> +        }
> +        memcpy(avctx->extradata, headerPtr.pBuffer, avctx->extradata_size);
> +        av_freep(&headerPtr.pBuffer);
> +    }
> +    return 0;
> +
> +failed_init:
> +    return error_mapping(ret);

If it's only this line, just use it directly where needed instead of a goto.

[...]

> +#define OFFSET(x) offsetof(SvtContext, x)
> +#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
> +static const AVOption options[] = {
> +    {"vui", "Enable vui info", OFFSET(svt_param.vui_info), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
> +    {"hielevel", "Hierarchical Prediction Levels [0,3]", OFFSET(svt_param.hierarchical_level), AV_OPT_TYPE_INT, { .i64 = 3 }, 0, 3, VE },
> +    {"la_depth", "Look Ahead Distance [0,256]", OFFSET(svt_param.la_depth), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 256, VE },
> +    {"intra_ref_type", "Intra Refresh Type 0: No intra refresh 1: CRA (Open GOP) 2: IDR", OFFSET(svt_param.intra_ref_type), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 2, VE },
> +    {"enc_p", "Encoding preset [0,12] (for tune 0 and >=4k resolution), [0,10] (for >= 1080p resolution), [0,9] (for all resolution and modes)", OFFSET(svt_param.enc_mode), AV_OPT_TYPE_INT, { .i64 = 9 }, 0, 12, VE },

Use preset instead, which is more in line with similar encoders.

> +    {"profile", "Profile now support[1,2], Main Still Picture Profile not supported", OFFSET(svt_param.profile), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, 2, VE },
> +    {"rc", "RC mode 0: CQP 1: VBR", OFFSET(svt_param.rc_mode), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
> +    {"q", "QP value for intra frames", OFFSET(svt_param.qp), AV_OPT_TYPE_INT, { .i64 = 32 }, 0, 51, VE },

qp.

> +    {"scd", "Scene change detection", OFFSET(svt_param.scd), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
> +    {"tune", "Tune mode: SQ/OQ[0,1]", OFFSET(svt_param.tune), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
> +    {"bl_mode", "Random Access Prediction Structure Type", OFFSET(svt_param.base_layer_switch_mode), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
> +    {NULL},
> +};
> +
> +static const AVClass class = {
> +    .class_name = "libsvt_hevc",
> +    .item_name  = av_default_item_name,
> +    .option     = options,
> +    .version    = LIBAVUTIL_VERSION_INT,
> +};
> +
> +static const AVCodecDefault eb_enc_defaults[] = {
> +    { "b",         "7M"    },
> +    { "refs",      "0"     },
> +    { "g",         "64"   },
> +    { "flags",     "+cgop" },
> +    { NULL },
> +};
> +
> +AVCodec ff_hevc_svt_encoder = {
> +    .name           = "libsvt_hevc",
> +    .long_name      = NULL_IF_CONFIG_SMALL("SVT-HEVC(Scalable Video Technology for HEVC) encoder"),
> +    .priv_data_size = sizeof(SvtContext),
> +    .type           = AVMEDIA_TYPE_VIDEO,
> +    .id             = AV_CODEC_ID_HEVC,
> +    .init           = eb_enc_init,
> +    .send_frame     = eb_send_frame,
> +    .receive_packet = eb_receive_packet,

Keep in mind that, at least for now and unlike with the decoupled
input-output decode API, if you only provide a send/receive callback
then the encoder will not work with avcodec_encode_video2(), only with
avcodec_send_frame() and avcodec_receive_packet().

> +    .close          = eb_enc_close,
> +    .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS,
> +    .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P,
> +                                                    AV_PIX_FMT_YUV420P10,
> +                                                    AV_PIX_FMT_NONE },
> +    .priv_class     = &class,
> +    .defaults       = eb_enc_defaults,
> +    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
> +    .wrapper_name   = "libsvt_hevc",
> +};
> 



More information about the ffmpeg-devel mailing list