[FFmpeg-devel] [PATCH 02/26] vaapi_encode_h265: Move options and common structures into context

Xiang, Haihao haihao.xiang at intel.com
Wed Apr 25 10:35:25 EEST 2018


> Matching previous commit for H.264.
> ---
>  libavcodec/vaapi_encode_h265.c | 114 +++++++++++++++++++---------------------
> -
>  1 file changed, 54 insertions(+), 60 deletions(-)
> 
> diff --git a/libavcodec/vaapi_encode_h265.c b/libavcodec/vaapi_encode_h265.c
> index 5203c6871d..ba6b426eed 100644
> --- a/libavcodec/vaapi_encode_h265.c
> +++ b/libavcodec/vaapi_encode_h265.c
> @@ -35,6 +35,15 @@
>  
>  
>  typedef struct VAAPIEncodeH265Context {
> +    VAAPIEncodeContext common;
> +
> +    // User options.
> +    int qp;
> +    int aud;
> +    int profile;
> +    int level;
> +
> +    // Derived settings.
>      unsigned int ctu_width;
>      unsigned int ctu_height;
>  
> @@ -42,12 +51,7 @@ typedef struct VAAPIEncodeH265Context {
>      int fixed_qp_p;
>      int fixed_qp_b;
>  
> -    H265RawAUD aud;
> -    H265RawVPS vps;
> -    H265RawSPS sps;
> -    H265RawPPS pps;
> -    H265RawSlice slice;
> -
> +    // Stream state.
>      int64_t last_idr_frame;
>      int pic_order_cnt;
>  
> @@ -55,25 +59,24 @@ typedef struct VAAPIEncodeH265Context {
>      int slice_type;
>      int pic_type;
>  
> +    // Writer structures.
> +    H265RawAUD   raw_aud;
> +    H265RawVPS   raw_vps;
> +    H265RawSPS   raw_sps;
> +    H265RawPPS   raw_pps;
> +    H265RawSlice raw_slice;
> +
>      CodedBitstreamContext *cbc;
>      CodedBitstreamFragment current_access_unit;
>      int aud_needed;
>  } VAAPIEncodeH265Context;
>  
> -typedef struct VAAPIEncodeH265Options {
> -    int qp;
> -    int aud;
> -    int profile;
> -    int level;
> -} VAAPIEncodeH265Options;
> -
>  
>  static int vaapi_encode_h265_write_access_unit(AVCodecContext *avctx,
>                                                 char *data, size_t *data_len,
>                                                 CodedBitstreamFragment *au)
>  {
> -    VAAPIEncodeContext      *ctx = avctx->priv_data;
> -    VAAPIEncodeH265Context *priv = ctx->priv_data;
> +    VAAPIEncodeH265Context *priv = avctx->priv_data;
>      int err;
>  
>      err = ff_cbs_write_fragment_data(priv->cbc, au);
> @@ -99,8 +102,7 @@ static int vaapi_encode_h265_add_nal(AVCodecContext *avctx,
>                                       CodedBitstreamFragment *au,
>                                       void *nal_unit)
>  {
> -    VAAPIEncodeContext      *ctx = avctx->priv_data;
> -    VAAPIEncodeH265Context *priv = ctx->priv_data;
> +    VAAPIEncodeH265Context *priv = avctx->priv_data;
>      H265RawNALUnitHeader *header = nal_unit;
>      int err;
>  
> @@ -118,27 +120,26 @@ static int vaapi_encode_h265_add_nal(AVCodecContext
> *avctx,
>  static int vaapi_encode_h265_write_sequence_header(AVCodecContext *avctx,
>                                                     char *data, size_t
> *data_len)
>  {
> -    VAAPIEncodeContext      *ctx = avctx->priv_data;
> -    VAAPIEncodeH265Context *priv = ctx->priv_data;
> +    VAAPIEncodeH265Context *priv = avctx->priv_data;
>      CodedBitstreamFragment   *au = &priv->current_access_unit;
>      int err;
>  
>      if (priv->aud_needed) {
> -        err = vaapi_encode_h265_add_nal(avctx, au, &priv->aud);
> +        err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_aud);
>          if (err < 0)
>              goto fail;
>          priv->aud_needed = 0;
>      }
>  
> -    err = vaapi_encode_h265_add_nal(avctx, au, &priv->vps);
> +    err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_vps);
>      if (err < 0)
>          goto fail;
>  
> -    err = vaapi_encode_h265_add_nal(avctx, au, &priv->sps);
> +    err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_sps);
>      if (err < 0)
>          goto fail;
>  
> -    err = vaapi_encode_h265_add_nal(avctx, au, &priv->pps);
> +    err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_pps);
>      if (err < 0)
>          goto fail;
>  
> @@ -153,19 +154,18 @@ static int
> vaapi_encode_h265_write_slice_header(AVCodecContext *avctx,
>                                                  VAAPIEncodeSlice *slice,
>                                                  char *data, size_t *data_len)
>  {
> -    VAAPIEncodeContext      *ctx = avctx->priv_data;
> -    VAAPIEncodeH265Context *priv = ctx->priv_data;
> +    VAAPIEncodeH265Context *priv = avctx->priv_data;
>      CodedBitstreamFragment   *au = &priv->current_access_unit;
>      int err;
>  
>      if (priv->aud_needed) {
> -        err = vaapi_encode_h265_add_nal(avctx, au, &priv->aud);
> +        err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_aud);
>          if (err < 0)
>              goto fail;
>          priv->aud_needed = 0;
>      }
>  
> -    err = vaapi_encode_h265_add_nal(avctx, au, &priv->slice);
> +    err = vaapi_encode_h265_add_nal(avctx, au, &priv->raw_slice);
>      if (err < 0)
>          goto fail;
>  
> @@ -178,10 +178,10 @@ fail:
>  static int vaapi_encode_h265_init_sequence_params(AVCodecContext *avctx)
>  {
>      VAAPIEncodeContext                *ctx = avctx->priv_data;
> -    VAAPIEncodeH265Context           *priv = ctx->priv_data;
> -    H265RawVPS                        *vps = &priv->vps;
> -    H265RawSPS                        *sps = &priv->sps;
> -    H265RawPPS                        *pps = &priv->pps;
> +    VAAPIEncodeH265Context           *priv = avctx->priv_data;
> +    H265RawVPS                        *vps = &priv->raw_vps;
> +    H265RawSPS                        *sps = &priv->raw_sps;
> +    H265RawPPS                        *pps = &priv->raw_pps;
>      H265RawVUI                        *vui = &sps->vui;
>      VAEncSequenceParameterBufferHEVC *vseq = ctx->codec_sequence_params;
>      VAEncPictureParameterBufferHEVC  *vpic = ctx->codec_picture_params;
> @@ -537,9 +537,7 @@ static int
> vaapi_encode_h265_init_sequence_params(AVCodecContext *avctx)
>  static int vaapi_encode_h265_init_picture_params(AVCodecContext *avctx,
>                                                   VAAPIEncodePicture *pic)
>  {
> -    VAAPIEncodeContext               *ctx = avctx->priv_data;
> -    VAAPIEncodeH265Context          *priv = ctx->priv_data;
> -    VAAPIEncodeH265Options           *opt = ctx->codec_options;
> +    VAAPIEncodeH265Context          *priv = avctx->priv_data;
>      VAEncPictureParameterBufferHEVC *vpic = pic->codec_picture_params;
>      int i;
>  
> @@ -575,14 +573,16 @@ static int
> vaapi_encode_h265_init_picture_params(AVCodecContext *avctx,
>      }
>      priv->pic_order_cnt = pic->display_order - priv->last_idr_frame;
>  
> -    if (opt->aud) {
> +    if (priv->aud) {
>          priv->aud_needed = 1;
> -        priv->aud.nal_unit_header = (H265RawNALUnitHeader) {
> -            .nal_unit_type         = HEVC_NAL_AUD,
> -            .nuh_layer_id          = 0,
> -            .nuh_temporal_id_plus1 = 1,
> +        priv->raw_aud = (H265RawAUD) {
> +            .nal_unit_header = {
> +                .nal_unit_type         = HEVC_NAL_AUD,
> +                .nuh_layer_id          = 0,
> +                .nuh_temporal_id_plus1 = 1,
> +            },
> +            .pic_type = priv->pic_type,
>          };
> -        priv->aud.pic_type = priv->pic_type;
>      } else {
>          priv->aud_needed = 0;
>      }
> @@ -652,10 +652,10 @@ static int
> vaapi_encode_h265_init_slice_params(AVCodecContext *avctx,
>                                                 VAAPIEncodeSlice *slice)
>  {
>      VAAPIEncodeContext                *ctx = avctx->priv_data;
> -    VAAPIEncodeH265Context           *priv = ctx->priv_data;
> -    const H265RawSPS                  *sps = &priv->sps;
> -    const H265RawPPS                  *pps = &priv->pps;
> -    H265RawSliceHeader                 *sh = &priv->slice.header;
> +    VAAPIEncodeH265Context           *priv = avctx->priv_data;
> +    const H265RawSPS                  *sps = &priv->raw_sps;
> +    const H265RawPPS                  *pps = &priv->raw_pps;
> +    H265RawSliceHeader                 *sh = &priv->raw_slice.header;
>      VAEncPictureParameterBufferHEVC  *vpic = pic->codec_picture_params;
>      VAEncSliceParameterBufferHEVC  *vslice = slice->codec_slice_params;
>      int i;
> @@ -826,8 +826,7 @@ static int
> vaapi_encode_h265_init_slice_params(AVCodecContext *avctx,
>  static av_cold int vaapi_encode_h265_configure(AVCodecContext *avctx)
>  {
>      VAAPIEncodeContext      *ctx = avctx->priv_data;
> -    VAAPIEncodeH265Context *priv = ctx->priv_data;
> -    VAAPIEncodeH265Options  *opt = ctx->codec_options;
> +    VAAPIEncodeH265Context *priv = avctx->priv_data;
>      int err;
>  
>      err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_HEVC, avctx);
> @@ -842,7 +841,7 @@ static av_cold int
> vaapi_encode_h265_configure(AVCodecContext *avctx)
>             ctx->surface_height, priv->ctu_width, priv->ctu_height);
>  
>      if (ctx->va_rc_mode == VA_RC_CQP) {
> -        priv->fixed_qp_p = opt->qp;
> +        priv->fixed_qp_p = priv->qp;
>          if (avctx->i_quant_factor > 0.0)
>              priv->fixed_qp_idr = (int)((priv->fixed_qp_p * avctx-
> >i_quant_factor +
>                                          avctx->i_quant_offset) + 0.5);
> @@ -899,16 +898,15 @@ static const VAAPIEncodeType vaapi_encode_type_h265 = {
>  
>  static av_cold int vaapi_encode_h265_init(AVCodecContext *avctx)
>  {
> -    VAAPIEncodeContext     *ctx = avctx->priv_data;
> -    VAAPIEncodeH265Options *opt =
> -        (VAAPIEncodeH265Options*)ctx->codec_options_data;
> +    VAAPIEncodeContext      *ctx = avctx->priv_data;
> +    VAAPIEncodeH265Context *priv = avctx->priv_data;
> 

Like as what I said for previous commit, here I prefer using 'ctx = &avctx-
>common'. But I am also fine if you don't want to change it.

>  
>      ctx->codec = &vaapi_encode_type_h265;
>  
>      if (avctx->profile == FF_PROFILE_UNKNOWN)
> -        avctx->profile = opt->profile;
> +        avctx->profile = priv->profile;
>      if (avctx->level == FF_LEVEL_UNKNOWN)
> -        avctx->level = opt->level;
> +        avctx->level = priv->level;
>  
>      switch (avctx->profile) {
>      case FF_PROFILE_HEVC_MAIN:
> @@ -953,17 +951,14 @@ static av_cold int vaapi_encode_h265_init(AVCodecContext
> *avctx)
>  
>  static av_cold int vaapi_encode_h265_close(AVCodecContext *avctx)
>  {
> -    VAAPIEncodeContext *ctx = avctx->priv_data;
> -    VAAPIEncodeH265Context *priv = ctx->priv_data;
> +    VAAPIEncodeH265Context *priv = avctx->priv_data;
>  
> -    if (priv)
> -        ff_cbs_close(&priv->cbc);
> +    ff_cbs_close(&priv->cbc);
>  

I see the NULL check is still used in vaapi_encode_h264_close(), it would be
better to remove the NULL check in vaapi_encode_h264_close() too if this check
is unnecessary.

>      return ff_vaapi_encode_close(avctx);
>  }
>  
> -#define OFFSET(x) (offsetof(VAAPIEncodeContext, codec_options_data) + \
> -                   offsetof(VAAPIEncodeH265Options, x))
> +#define OFFSET(x) offsetof(VAAPIEncodeH265Context, x)
>  #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
>  static const AVOption vaapi_encode_h265_options[] = {
>      { "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
> @@ -1029,8 +1024,7 @@ AVCodec ff_hevc_vaapi_encoder = {
>      .long_name      = NULL_IF_CONFIG_SMALL("H.265/HEVC (VAAPI)"),
>      .type           = AVMEDIA_TYPE_VIDEO,
>      .id             = AV_CODEC_ID_HEVC,
> -    .priv_data_size = (sizeof(VAAPIEncodeContext) +
> -                       sizeof(VAAPIEncodeH265Options)),
> +    .priv_data_size = sizeof(VAAPIEncodeH265Context),
>      .init           = &vaapi_encode_h265_init,
>      .encode2        = &ff_vaapi_encode2,
>      .close          = &vaapi_encode_h265_close,


More information about the ffmpeg-devel mailing list