static int qsv_decode_init(AVCodecContext *avctx, QSVContext *q, AVPacket *avpkt)

{

    mfxVideoParam param = { { 0 } };

    mfxBitstream bs   = { { { 0 } } };

    int ret;

    enum AVPixelFormat pix_fmts[3] = { AV_PIX_FMT_QSV,

                                       AV_PIX_FMT_NV12,

                                       AV_PIX_FMT_NONE };



    ret = ff_get_format(avctx, pix_fmts);

    if (ret < 0)

        return ret;



    avctx->pix_fmt      = ret;



    q->iopattern  = MFX_IOPATTERN_OUT_SYSTEM_MEMORY;

    if (avctx->hwaccel_context) {

        AVQSVContext *qsv = avctx->hwaccel_context;



        q->session        = qsv->session;

        q->iopattern      = qsv->iopattern;

        q->ext_buffers    = qsv->ext_buffers;

        q->nb_ext_buffers = qsv->nb_ext_buffers;

    }

    if (!q->session) {

        if (!q->internal_qs.session) {

            ret = ff_qsv_init_internal_session(avctx, &q->internal_qs,

                                               q->load_plugins);

            if (ret < 0)

                return ret;

        }



        q->session = q->internal_qs.session;

    }



    if (avpkt->size) {

        bs.Data       = avpkt->data;

        bs.DataLength = avpkt->size;

        bs.MaxLength  = bs.DataLength;

        bs.TimeStamp  = avpkt->pts;

    } else

        return AVERROR_INVALIDDATA;



    ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);

    if (ret < 0) {

        av_log(avctx, AV_LOG_ERROR, "Unsupported codec_id %08x\n", avctx->codec_id);

        return ret;

    }



    param.mfx.CodecId = ret;



    ret = MFXVideoDECODE_DecodeHeader(q->session, &bs, &param);

    if (MFX_ERR_MORE_DATA==ret) {

        /* this code means that header not found so we return packet size to skip

           a current packet

         */

        return avpkt->size;

    } else if (ret < 0) {

        av_log(avctx, AV_LOG_ERROR, "Decode header error %d\n", ret);

        return ff_qsv_error(ret);

    }

    param.IOPattern   = q->iopattern;

    param.AsyncDepth  = q->async_depth;

    param.ExtParam    = q->ext_buffers;

    param.NumExtParam = q->nb_ext_buffers;

    param.mfx.FrameInfo.BitDepthLuma   = 8;

    param.mfx.FrameInfo.BitDepthChroma = 8;



    ret = MFXVideoDECODE_Init(q->session, &param);

    if (ret < 0) {

        if (MFX_ERR_INVALID_VIDEO_PARAM==ret) {

            av_log(avctx, AV_LOG_ERROR,

                   "Error initializing the MFX video decoder, unsupported video\n");

        } else {

            av_log(avctx, AV_LOG_ERROR,

                   "Error initializing the MFX video decoder %d\n", ret);

        }

        return ff_qsv_error(ret);

    }



    avctx->profile      = param.mfx.CodecProfile;

    avctx->level        = param.mfx.CodecLevel;

    avctx->coded_width  = param.mfx.FrameInfo.Width;

    avctx->coded_height = param.mfx.FrameInfo.Height;

    avctx->width        = param.mfx.FrameInfo.CropW - param.mfx.FrameInfo.CropX;

    avctx->height       = param.mfx.FrameInfo.CropH - param.mfx.FrameInfo.CropY;



    /* maximum decoder latency should be not exceed max DPB size for h.264 and

       HEVC which is 16 for both cases.

       So weare  pre-allocating fifo big enough for 17 elements:

     */

    if (!q->async_fifo) {

        q->async_fifo = av_fifo_alloc((1 + 16) *

                                      (sizeof(mfxSyncPoint*) + sizeof(QSVFrame*)));

        if (!q->async_fifo)

            return AVERROR(ENOMEM);

    }



    if (!q->input_fifo) {

        q->input_fifo = av_fifo_alloc(1024*16);

        if (!q->input_fifo)

            return AVERROR(ENOMEM);

    }



    if (!q->pkt_fifo) {

        q->pkt_fifo = av_fifo_alloc( sizeof(AVPacket) * (1 + 16) );

        if (!q->pkt_fifo)

            return AVERROR(ENOMEM);

    }

    q->engine_ready = 1;



    return 0;

}

static int qsv_decode_init(AVCodecContext *avctx, QSVContext *q, AVPacket *avpkt)

{

    mfxVideoParam param = { { 0 } };

    mfxBitstream bs   = { { { 0 } } };

    int ret;

    enum AVPixelFormat pix_fmts[3] = { AV_PIX_FMT_QSV,

                                       AV_PIX_FMT_NV12,

                                       AV_PIX_FMT_NONE };



    ret = ff_get_format(avctx, pix_fmts);

    if (ret < 0)

        return ret;



    avctx->pix_fmt      = ret;



    q->iopattern  = MFX_IOPATTERN_OUT_SYSTEM_MEMORY;

    if (avctx->hwaccel_context) {

        AVQSVContext *qsv = avctx->hwaccel_context;



        q->session        = qsv->session;

        q->iopattern      = qsv->iopattern;

        q->ext_buffers    = qsv->ext_buffers;

        q->nb_ext_buffers = qsv->nb_ext_buffers;

    }

    if (!q->session) {

        if (!q->internal_qs.session) {

            ret = ff_qsv_init_internal_session(avctx, &q->internal_qs,

                                               q->load_plugins);

            if (ret < 0)

                return ret;

        }



        q->session = q->internal_qs.session;

    }



    if (avpkt->size) {

        bs.Data       = avpkt->data;

        bs.DataLength = avpkt->size;

        bs.MaxLength  = bs.DataLength;

        bs.TimeStamp  = avpkt->pts;

    } else

        return AVERROR_INVALIDDATA;



    ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);

    if (ret < 0) {

        av_log(avctx, AV_LOG_ERROR, "Unsupported codec_id %08x\n", avctx->codec_id);

        return ret;

    }



    param.mfx.CodecId = ret;



    ret = MFXVideoDECODE_DecodeHeader(q->session, &bs, &param);

    if (MFX_ERR_MORE_DATA==ret) {

        





        return avpkt->size;

    } else if (ret < 0) {

        av_log(avctx, AV_LOG_ERROR, "Decode header error %d\n", ret);

        return ff_qsv_error(ret);

    }

    param.IOPattern   = q->iopattern;

    param.AsyncDepth  = q->async_depth;

    param.ExtParam    = q->ext_buffers;

    param.NumExtParam = q->nb_ext_buffers;

    param.mfx.FrameInfo.BitDepthLuma   = 8;

    param.mfx.FrameInfo.BitDepthChroma = 8;



    ret = MFXVideoDECODE_Init(q->session, &param);

    if (ret < 0) {

        if (MFX_ERR_INVALID_VIDEO_PARAM==ret) {

            av_log(avctx, AV_LOG_ERROR,

                   "Error initializing the MFX video decoder, unsupported video\n");

        } else {

            av_log(avctx, AV_LOG_ERROR,

                   "Error initializing the MFX video decoder %d\n", ret);

        }

        return ff_qsv_error(ret);

    }



    avctx->profile      = param.mfx.CodecProfile;

    avctx->level        = param.mfx.CodecLevel;

    avctx->coded_width  = param.mfx.FrameInfo.Width;

    avctx->coded_height = param.mfx.FrameInfo.Height;

    avctx->width        = param.mfx.FrameInfo.CropW - param.mfx.FrameInfo.CropX;

    avctx->height       = param.mfx.FrameInfo.CropH - param.mfx.FrameInfo.CropY;



    







    if (!q->async_fifo) {

        q->async_fifo = av_fifo_alloc((1 + 16) *

                                      (sizeof(mfxSyncPoint*) + sizeof(QSVFrame*)));

        if (!q->async_fifo)

            return AVERROR(ENOMEM);

    }



    if (!q->input_fifo) {

        q->input_fifo = av_fifo_alloc(1024*16);

        if (!q->input_fifo)

            return AVERROR(ENOMEM);

    }



    if (!q->pkt_fifo) {

        q->pkt_fifo = av_fifo_alloc( sizeof(AVPacket) * (1 + 16) );

        if (!q->pkt_fifo)

            return AVERROR(ENOMEM);

    }

    q->engine_ready = 1;



    return 0;

}

                                      (sizeof(mfxSyncPoint*) + sizeof(QSVFrame*)));         } else {
static int FUNC_0(AVCodecContext *VAR_0, QSVContext *VAR_1, AVPacket *VAR_2)

{

    mfxVideoParam param = { { 0 } };

    mfxBitstream bs   = { { { 0 } } };

    int VAR_3;

    enum AVPixelFormat VAR_4[3] = { AV_PIX_FMT_QSV,

                                       AV_PIX_FMT_NV12,

                                       AV_PIX_FMT_NONE };



    VAR_3 = ff_get_format(VAR_0, VAR_4);

    if (VAR_3 < 0)

        return VAR_3;



    VAR_0->pix_fmt      = VAR_3;



    VAR_1->iopattern  = MFX_IOPATTERN_OUT_SYSTEM_MEMORY;

    if (VAR_0->hwaccel_context) {

        AVQSVContext *qsv = VAR_0->hwaccel_context;



        VAR_1->session        = qsv->session;

        VAR_1->iopattern      = qsv->iopattern;

        VAR_1->ext_buffers    = qsv->ext_buffers;

        VAR_1->nb_ext_buffers = qsv->nb_ext_buffers;

    }

    if (!VAR_1->session) {

        if (!VAR_1->internal_qs.session) {

            VAR_3 = ff_qsv_init_internal_session(VAR_0, &VAR_1->internal_qs,

                                               VAR_1->load_plugins);

            if (VAR_3 < 0)

                return VAR_3;

        }



        VAR_1->session = VAR_1->internal_qs.session;

    }



    if (VAR_2->size) {

        bs.Data       = VAR_2->data;

        bs.DataLength = VAR_2->size;

        bs.MaxLength  = bs.DataLength;

        bs.TimeStamp  = VAR_2->pts;

    } else

        return AVERROR_INVALIDDATA;



    VAR_3 = ff_qsv_codec_id_to_mfx(VAR_0->codec_id);

    if (VAR_3 < 0) {

        av_log(VAR_0, AV_LOG_ERROR, "Unsupported codec_id %08x\n", VAR_0->codec_id);

        return VAR_3;

    }



    param.mfx.CodecId = VAR_3;



    VAR_3 = MFXVideoDECODE_DecodeHeader(VAR_1->session, &bs, &param);

    if (MFX_ERR_MORE_DATA==VAR_3) {

        





        return VAR_2->size;

    } else if (VAR_3 < 0) {

        av_log(VAR_0, AV_LOG_ERROR, "Decode header error %d\n", VAR_3);

        return ff_qsv_error(VAR_3);

    }

    param.IOPattern   = VAR_1->iopattern;

    param.AsyncDepth  = VAR_1->async_depth;

    param.ExtParam    = VAR_1->ext_buffers;

    param.NumExtParam = VAR_1->nb_ext_buffers;

    param.mfx.FrameInfo.BitDepthLuma   = 8;

    param.mfx.FrameInfo.BitDepthChroma = 8;



    VAR_3 = MFXVideoDECODE_Init(VAR_1->session, &param);

    if (VAR_3 < 0) {

        if (MFX_ERR_INVALID_VIDEO_PARAM==VAR_3) {

            av_log(VAR_0, AV_LOG_ERROR,

                   "Error initializing the MFX video decoder, unsupported video\n");

        } else {

            av_log(VAR_0, AV_LOG_ERROR,

                   "Error initializing the MFX video decoder %d\n", VAR_3);

        }

        return ff_qsv_error(VAR_3);

    }



    VAR_0->profile      = param.mfx.CodecProfile;

    VAR_0->level        = param.mfx.CodecLevel;

    VAR_0->coded_width  = param.mfx.FrameInfo.Width;

    VAR_0->coded_height = param.mfx.FrameInfo.Height;

    VAR_0->width        = param.mfx.FrameInfo.CropW - param.mfx.FrameInfo.CropX;

    VAR_0->height       = param.mfx.FrameInfo.CropH - param.mfx.FrameInfo.CropY;



    







    if (!VAR_1->async_fifo) {

        VAR_1->async_fifo = av_fifo_alloc((1 + 16) *

                                      (sizeof(mfxSyncPoint*) + sizeof(QSVFrame*)));

        if (!VAR_1->async_fifo)

            return AVERROR(ENOMEM);

    }



    if (!VAR_1->input_fifo) {

        VAR_1->input_fifo = av_fifo_alloc(1024*16);

        if (!VAR_1->input_fifo)

            return AVERROR(ENOMEM);

    }



    if (!VAR_1->pkt_fifo) {

        VAR_1->pkt_fifo = av_fifo_alloc( sizeof(AVPacket) * (1 + 16) );

        if (!VAR_1->pkt_fifo)

            return AVERROR(ENOMEM);

    }

    VAR_1->engine_ready = 1;



    return 0;

}

static int FUNC_0(AVCodecContext *VAR_0, QSVContext *VAR_1, AVPacket *VAR_2)
{ mfxVideoParam param = { { 0 } }; mfxBitstream bs   = { { { 0 } } }; int VAR_3; enum AVPixelFormat VAR_4[3] = { AV_PIX_FMT_QSV, AV_PIX_FMT_NV12,
AV_PIX_FMT_NONE }; VAR_3 = ff_get_format(VAR_0, VAR_4); if (VAR_3 < 0)
return VAR_3; VAR_0->pix_fmt      = VAR_3; VAR_1->iopattern  = MFX_IOPATTERN_OUT_SYSTEM_MEMORY; if (VAR_0->hwaccel_context) { AVQSVContext *qsv = VAR_0->hwaccel_context; VAR_1->session        = qsv->session; VAR_1->iopattern      = qsv->iopattern; VAR_1->ext_buffers    = qsv->ext_buffers; VAR_1->nb_ext_buffers = qsv->nb_ext_buffers; } if (!VAR_1->session) { if (!VAR_1->internal_qs.session) { VAR_3 = ff_qsv_init_internal_session(VAR_0, &VAR_1->internal_qs,
VAR_1->load_plugins); if (VAR_3 < 0)
return VAR_3; } VAR_1->session = VAR_1->internal_qs.session; } if (VAR_2->size) { bs.Data       = VAR_2->data; bs.DataLength = VAR_2->size; bs.MaxLength  = bs.DataLength; bs.TimeStamp  = VAR_2->pts; } else return AVERROR_INVALIDDATA; VAR_3 = ff_qsv_codec_id_to_mfx(VAR_0->codec_id); if (VAR_3 < 0) { av_log(VAR_0, AV_LOG_ERROR, "Unsupported codec_id %08x\n", VAR_0->codec_id); return VAR_3; } param.mfx.CodecId = VAR_3; VAR_3 = MFXVideoDECODE_DecodeHeader(VAR_1->session, &bs, &param); if (MFX_ERR_MORE_DATA==VAR_3) { return VAR_2->size; } else if (VAR_3 < 0) { av_log(VAR_0, AV_LOG_ERROR, "Decode header error %d\n", VAR_3); return ff_qsv_error(VAR_3); } param.IOPattern   = VAR_1->iopattern; param.AsyncDepth  = VAR_1->async_depth; param.ExtParam    = VAR_1->ext_buffers; param.NumExtParam = VAR_1->nb_ext_buffers; param.mfx.FrameInfo.BitDepthLuma   = 8; param.mfx.FrameInfo.BitDepthChroma = 8; VAR_3 = MFXVideoDECODE_Init(VAR_1->session, &param); if (VAR_3 < 0) { if (MFX_ERR_INVALID_VIDEO_PARAM==VAR_3) { av_log(VAR_0, AV_LOG_ERROR,
"Error initializing the MFX video decoder, unsupported video\n"); } else { av_log(VAR_0, AV_LOG_ERROR,
"Error initializing the MFX video decoder %d\n", VAR_3); } return ff_qsv_error(VAR_3); } VAR_0->profile      = param.mfx.CodecProfile; VAR_0->level        = param.mfx.CodecLevel; VAR_0->coded_width  = param.mfx.FrameInfo.Width; VAR_0->coded_height = param.mfx.FrameInfo.Height; VAR_0->width        = param.mfx.FrameInfo.CropW - param.mfx.FrameInfo.CropX; VAR_0->height       = param.mfx.FrameInfo.CropH - param.mfx.FrameInfo.CropY; if (!VAR_1->async_fifo) { VAR_1->async_fifo = av_fifo_alloc((1 + 16) *
(sizeof(mfxSyncPoint*) + sizeof(QSVFrame*))); if (!VAR_1->async_fifo)
return AVERROR(ENOMEM); } if (!VAR_1->input_fifo) { VAR_1->input_fifo = av_fifo_alloc(1024*16); if (!VAR_1->input_fifo)
return AVERROR(ENOMEM); } if (!VAR_1->pkt_fifo) { VAR_1->pkt_fifo = av_fifo_alloc( sizeof(AVPacket) * (1 + 16) ); if (!VAR_1->pkt_fifo)
return AVERROR(ENOMEM); } VAR_1->engine_ready = 1; return 0; }