static int rkmpp_retrieve_frame(AVCodecContext *avctx, AVFrame *frame)

{

    RKMPPDecodeContext *rk_context = avctx->priv_data;

    RKMPPDecoder *decoder = (RKMPPDecoder *)rk_context->decoder_ref->data;

    RKMPPFrameContext *framecontext = NULL;

    AVBufferRef *framecontextref = NULL;

    int ret;

    MppFrame mppframe = NULL;

    MppBuffer buffer = NULL;

    AVDRMFrameDescriptor *desc = NULL;

    AVDRMLayerDescriptor *layer = NULL;

    int retrycount = 0;

    int mode;

    MppFrameFormat mppformat;

    uint32_t drmformat;



    // on start of decoding, MPP can return -1, which is supposed to be expected

    // this is due to some internal MPP init which is not completed, that will

    // only happen in the first few frames queries, but should not be interpreted

    // as an error, Therefore we need to retry a couple times when we get -1

    // in order to let it time to complete it's init, then we sleep a bit between retries.

retry_get_frame:

    ret = decoder->mpi->decode_get_frame(decoder->ctx, &mppframe);

    if (ret != MPP_OK && ret != MPP_ERR_TIMEOUT && !decoder->first_frame) {

        if (retrycount < 5) {

            av_log(avctx, AV_LOG_DEBUG, "Failed to get a frame, retrying (code = %d, retrycount = %d)\n", ret, retrycount);

            usleep(10000);

            retrycount++;

            goto retry_get_frame;

        } else {

            av_log(avctx, AV_LOG_ERROR, "Failed to get a frame from MPP (code = %d)\n", ret);

            goto fail;

        }

    }



    if (mppframe) {

        // Check wether we have a special frame or not

        if (mpp_frame_get_info_change(mppframe)) {

            AVHWFramesContext *hwframes;



            av_log(avctx, AV_LOG_INFO, "Decoder noticed an info change (%dx%d), format=%d\n",

                                        (int)mpp_frame_get_width(mppframe), (int)mpp_frame_get_height(mppframe),

                                        (int)mpp_frame_get_fmt(mppframe));



            avctx->width = mpp_frame_get_width(mppframe);

            avctx->height = mpp_frame_get_height(mppframe);



            decoder->mpi->control(decoder->ctx, MPP_DEC_SET_INFO_CHANGE_READY, NULL);

            decoder->first_frame = 1;



            av_buffer_unref(&decoder->frames_ref);



            decoder->frames_ref = av_hwframe_ctx_alloc(decoder->device_ref);

            if (!decoder->frames_ref) {

                ret = AVERROR(ENOMEM);

                goto fail;

            }



            mppformat = mpp_frame_get_fmt(mppframe);

            drmformat = rkmpp_get_frameformat(mppformat);



            hwframes = (AVHWFramesContext*)decoder->frames_ref->data;

            hwframes->format    = AV_PIX_FMT_DRM_PRIME;

            hwframes->sw_format = drmformat == DRM_FORMAT_NV12 ? AV_PIX_FMT_NV12 : AV_PIX_FMT_NONE;

            hwframes->width     = avctx->width;

            hwframes->height    = avctx->height;

            ret = av_hwframe_ctx_init(decoder->frames_ref);

            if (ret < 0)

                goto fail;



            // here decoder is fully initialized, we need to feed it again with data

            ret = AVERROR(EAGAIN);

            goto fail;

        } else if (mpp_frame_get_eos(mppframe)) {

            av_log(avctx, AV_LOG_DEBUG, "Received a EOS frame.\n");

            decoder->eos_reached = 1;

            ret = AVERROR_EOF;

            goto fail;

        } else if (mpp_frame_get_discard(mppframe)) {

            av_log(avctx, AV_LOG_DEBUG, "Received a discard frame.\n");

            ret = AVERROR(EAGAIN);

            goto fail;

        } else if (mpp_frame_get_errinfo(mppframe)) {

            av_log(avctx, AV_LOG_ERROR, "Received a errinfo frame.\n");

            ret = AVERROR_UNKNOWN;

            goto fail;

        }



        // here we should have a valid frame

        av_log(avctx, AV_LOG_DEBUG, "Received a frame.\n");



        // setup general frame fields

        frame->format           = AV_PIX_FMT_DRM_PRIME;

        frame->width            = mpp_frame_get_width(mppframe);

        frame->height           = mpp_frame_get_height(mppframe);

        frame->pts              = mpp_frame_get_pts(mppframe);

        frame->color_range      = mpp_frame_get_color_range(mppframe);

        frame->color_primaries  = mpp_frame_get_color_primaries(mppframe);

        frame->color_trc        = mpp_frame_get_color_trc(mppframe);

        frame->colorspace       = mpp_frame_get_colorspace(mppframe);



        mode = mpp_frame_get_mode(mppframe);

        frame->interlaced_frame = ((mode & MPP_FRAME_FLAG_FIELD_ORDER_MASK) == MPP_FRAME_FLAG_DEINTERLACED);

        frame->top_field_first  = ((mode & MPP_FRAME_FLAG_FIELD_ORDER_MASK) == MPP_FRAME_FLAG_TOP_FIRST);



        mppformat = mpp_frame_get_fmt(mppframe);

        drmformat = rkmpp_get_frameformat(mppformat);



        // now setup the frame buffer info

        buffer = mpp_frame_get_buffer(mppframe);

        if (buffer) {

            desc = av_mallocz(sizeof(AVDRMFrameDescriptor));

            if (!desc) {

                ret = AVERROR(ENOMEM);

                goto fail;

            }



            desc->nb_objects = 1;

            desc->objects[0].fd = mpp_buffer_get_fd(buffer);

            desc->objects[0].size = mpp_buffer_get_size(buffer);



            desc->nb_layers = 1;

            layer = &desc->layers[0];

            layer->format = drmformat;

            layer->nb_planes = 2;



            layer->planes[0].object_index = 0;

            layer->planes[0].offset = 0;

            layer->planes[0].pitch = mpp_frame_get_hor_stride(mppframe);



            layer->planes[1].object_index = 0;

            layer->planes[1].offset = layer->planes[0].pitch * mpp_frame_get_ver_stride(mppframe);

            layer->planes[1].pitch = layer->planes[0].pitch;



            // we also allocate a struct in buf[0] that will allow to hold additionnal information

            // for releasing properly MPP frames and decoder

            framecontextref = av_buffer_allocz(sizeof(*framecontext));

            if (!framecontextref) {

                ret = AVERROR(ENOMEM);

                goto fail;

            }



            // MPP decoder needs to be closed only when all frames have been released.

            framecontext = (RKMPPFrameContext *)framecontextref->data;

            framecontext->decoder_ref = av_buffer_ref(rk_context->decoder_ref);

            framecontext->frame = mppframe;



            frame->data[0]  = (uint8_t *)desc;

            frame->buf[0]   = av_buffer_create((uint8_t *)desc, sizeof(*desc), rkmpp_release_frame,

                                               framecontextref, AV_BUFFER_FLAG_READONLY);



            if (!frame->buf[0]) {

                ret = AVERROR(ENOMEM);

                goto fail;

            }



            frame->hw_frames_ctx = av_buffer_ref(decoder->frames_ref);

            if (!frame->hw_frames_ctx) {

                ret = AVERROR(ENOMEM);

                goto fail;

            }



            decoder->first_frame = 0;

            return 0;

        } else {

            av_log(avctx, AV_LOG_ERROR, "Failed to retrieve the frame buffer, frame is dropped (code = %d)\n", ret);

            mpp_frame_deinit(&mppframe);

        }

    } else if (decoder->eos_reached) {

        return AVERROR_EOF;

    } else if (ret == MPP_ERR_TIMEOUT) {

        av_log(avctx, AV_LOG_DEBUG, "Timeout when trying to get a frame from MPP\n");

    }



    return AVERROR(EAGAIN);



fail:

    if (mppframe)

        mpp_frame_deinit(&mppframe);



    if (framecontext)

        av_buffer_unref(&framecontext->decoder_ref);



    if (framecontextref)

        av_buffer_unref(&framecontextref);



    if (desc)

        av_free(desc);



    return ret;

}

static int rkmpp_retrieve_frame(AVCodecContext *avctx, AVFrame *frame)

{

    RKMPPDecodeContext *rk_context = avctx->priv_data;

    RKMPPDecoder *decoder = (RKMPPDecoder *)rk_context->decoder_ref->data;

    RKMPPFrameContext *framecontext = NULL;

    AVBufferRef *framecontextref = NULL;

    int ret;

    MppFrame mppframe = NULL;

    MppBuffer buffer = NULL;

    AVDRMFrameDescriptor *desc = NULL;

    AVDRMLayerDescriptor *layer = NULL;

    int retrycount = 0;

    int mode;

    MppFrameFormat mppformat;

    uint32_t drmformat;



    

    

    

    

    

retry_get_frame:

    ret = decoder->mpi->decode_get_frame(decoder->ctx, &mppframe);

    if (ret != MPP_OK && ret != MPP_ERR_TIMEOUT && !decoder->first_frame) {

        if (retrycount < 5) {

            av_log(avctx, AV_LOG_DEBUG, "Failed to get a frame, retrying (code = %d, retrycount = %d)\n", ret, retrycount);

            usleep(10000);

            retrycount++;

            goto retry_get_frame;

        } else {

            av_log(avctx, AV_LOG_ERROR, "Failed to get a frame from MPP (code = %d)\n", ret);

            goto fail;

        }

    }



    if (mppframe) {

        

        if (mpp_frame_get_info_change(mppframe)) {

            AVHWFramesContext *hwframes;



            av_log(avctx, AV_LOG_INFO, "Decoder noticed an info change (%dx%d), format=%d\n",

                                        (int)mpp_frame_get_width(mppframe), (int)mpp_frame_get_height(mppframe),

                                        (int)mpp_frame_get_fmt(mppframe));



            avctx->width = mpp_frame_get_width(mppframe);

            avctx->height = mpp_frame_get_height(mppframe);



            decoder->mpi->control(decoder->ctx, MPP_DEC_SET_INFO_CHANGE_READY, NULL);

            decoder->first_frame = 1;



            av_buffer_unref(&decoder->frames_ref);



            decoder->frames_ref = av_hwframe_ctx_alloc(decoder->device_ref);

            if (!decoder->frames_ref) {

                ret = AVERROR(ENOMEM);

                goto fail;

            }



            mppformat = mpp_frame_get_fmt(mppframe);

            drmformat = rkmpp_get_frameformat(mppformat);



            hwframes = (AVHWFramesContext*)decoder->frames_ref->data;

            hwframes->format    = AV_PIX_FMT_DRM_PRIME;

            hwframes->sw_format = drmformat == DRM_FORMAT_NV12 ? AV_PIX_FMT_NV12 : AV_PIX_FMT_NONE;

            hwframes->width     = avctx->width;

            hwframes->height    = avctx->height;

            ret = av_hwframe_ctx_init(decoder->frames_ref);

            if (ret < 0)

                goto fail;



            

            ret = AVERROR(EAGAIN);

            goto fail;

        } else if (mpp_frame_get_eos(mppframe)) {

            av_log(avctx, AV_LOG_DEBUG, "Received a EOS frame.\n");

            decoder->eos_reached = 1;

            ret = AVERROR_EOF;

            goto fail;

        } else if (mpp_frame_get_discard(mppframe)) {

            av_log(avctx, AV_LOG_DEBUG, "Received a discard frame.\n");

            ret = AVERROR(EAGAIN);

            goto fail;

        } else if (mpp_frame_get_errinfo(mppframe)) {

            av_log(avctx, AV_LOG_ERROR, "Received a errinfo frame.\n");

            ret = AVERROR_UNKNOWN;

            goto fail;

        }



        

        av_log(avctx, AV_LOG_DEBUG, "Received a frame.\n");



        

        frame->format           = AV_PIX_FMT_DRM_PRIME;

        frame->width            = mpp_frame_get_width(mppframe);

        frame->height           = mpp_frame_get_height(mppframe);

        frame->pts              = mpp_frame_get_pts(mppframe);

        frame->color_range      = mpp_frame_get_color_range(mppframe);

        frame->color_primaries  = mpp_frame_get_color_primaries(mppframe);

        frame->color_trc        = mpp_frame_get_color_trc(mppframe);

        frame->colorspace       = mpp_frame_get_colorspace(mppframe);



        mode = mpp_frame_get_mode(mppframe);

        frame->interlaced_frame = ((mode & MPP_FRAME_FLAG_FIELD_ORDER_MASK) == MPP_FRAME_FLAG_DEINTERLACED);

        frame->top_field_first  = ((mode & MPP_FRAME_FLAG_FIELD_ORDER_MASK) == MPP_FRAME_FLAG_TOP_FIRST);



        mppformat = mpp_frame_get_fmt(mppframe);

        drmformat = rkmpp_get_frameformat(mppformat);



        

        buffer = mpp_frame_get_buffer(mppframe);

        if (buffer) {

            desc = av_mallocz(sizeof(AVDRMFrameDescriptor));

            if (!desc) {

                ret = AVERROR(ENOMEM);

                goto fail;

            }



            desc->nb_objects = 1;

            desc->objects[0].fd = mpp_buffer_get_fd(buffer);

            desc->objects[0].size = mpp_buffer_get_size(buffer);



            desc->nb_layers = 1;

            layer = &desc->layers[0];

            layer->format = drmformat;

            layer->nb_planes = 2;



            layer->planes[0].object_index = 0;

            layer->planes[0].offset = 0;

            layer->planes[0].pitch = mpp_frame_get_hor_stride(mppframe);



            layer->planes[1].object_index = 0;

            layer->planes[1].offset = layer->planes[0].pitch * mpp_frame_get_ver_stride(mppframe);

            layer->planes[1].pitch = layer->planes[0].pitch;



            

            

            framecontextref = av_buffer_allocz(sizeof(*framecontext));

            if (!framecontextref) {

                ret = AVERROR(ENOMEM);

                goto fail;

            }



            

            framecontext = (RKMPPFrameContext *)framecontextref->data;

            framecontext->decoder_ref = av_buffer_ref(rk_context->decoder_ref);

            framecontext->frame = mppframe;



            frame->data[0]  = (uint8_t *)desc;

            frame->buf[0]   = av_buffer_create((uint8_t *)desc, sizeof(*desc), rkmpp_release_frame,

                                               framecontextref, AV_BUFFER_FLAG_READONLY);



            if (!frame->buf[0]) {

                ret = AVERROR(ENOMEM);

                goto fail;

            }



            frame->hw_frames_ctx = av_buffer_ref(decoder->frames_ref);

            if (!frame->hw_frames_ctx) {

                ret = AVERROR(ENOMEM);

                goto fail;

            }



            decoder->first_frame = 0;

            return 0;

        } else {

            av_log(avctx, AV_LOG_ERROR, "Failed to retrieve the frame buffer, frame is dropped (code = %d)\n", ret);

            mpp_frame_deinit(&mppframe);

        }

    } else if (decoder->eos_reached) {

        return AVERROR_EOF;

    } else if (ret == MPP_ERR_TIMEOUT) {

        av_log(avctx, AV_LOG_DEBUG, "Timeout when trying to get a frame from MPP\n");

    }



    return AVERROR(EAGAIN);



fail:

    if (mppframe)

        mpp_frame_deinit(&mppframe);



    if (framecontext)

        av_buffer_unref(&framecontext->decoder_ref);



    if (framecontextref)

        av_buffer_unref(&framecontextref);



    if (desc)

        av_free(desc);



    return ret;

}

static int FUNC_0(AVCodecContext *VAR_0, AVFrame *VAR_1)

{

    RKMPPDecodeContext *rk_context = VAR_0->priv_data;

    RKMPPDecoder *decoder = (RKMPPDecoder *)rk_context->decoder_ref->data;

    RKMPPFrameContext *framecontext = NULL;

    AVBufferRef *framecontextref = NULL;

    int VAR_2;

    MppFrame mppframe = NULL;

    MppBuffer buffer = NULL;

    AVDRMFrameDescriptor *desc = NULL;

    AVDRMLayerDescriptor *layer = NULL;

    int VAR_3 = 0;

    int VAR_4;

    MppFrameFormat mppformat;

    uint32_t drmformat;



    

    

    

    

    

retry_get_frame:

    VAR_2 = decoder->mpi->decode_get_frame(decoder->ctx, &mppframe);

    if (VAR_2 != MPP_OK && VAR_2 != MPP_ERR_TIMEOUT && !decoder->first_frame) {

        if (VAR_3 < 5) {

            av_log(VAR_0, AV_LOG_DEBUG, "Failed to get a VAR_1, retrying (code = %d, VAR_3 = %d)\n", VAR_2, VAR_3);

            usleep(10000);

            VAR_3++;

            goto retry_get_frame;

        } else {

            av_log(VAR_0, AV_LOG_ERROR, "Failed to get a VAR_1 from MPP (code = %d)\n", VAR_2);

            goto fail;

        }

    }



    if (mppframe) {

        

        if (mpp_frame_get_info_change(mppframe)) {

            AVHWFramesContext *hwframes;



            av_log(VAR_0, AV_LOG_INFO, "Decoder noticed an info change (%dx%d), format=%d\n",

                                        (int)mpp_frame_get_width(mppframe), (int)mpp_frame_get_height(mppframe),

                                        (int)mpp_frame_get_fmt(mppframe));



            VAR_0->width = mpp_frame_get_width(mppframe);

            VAR_0->height = mpp_frame_get_height(mppframe);



            decoder->mpi->control(decoder->ctx, MPP_DEC_SET_INFO_CHANGE_READY, NULL);

            decoder->first_frame = 1;



            av_buffer_unref(&decoder->frames_ref);



            decoder->frames_ref = av_hwframe_ctx_alloc(decoder->device_ref);

            if (!decoder->frames_ref) {

                VAR_2 = AVERROR(ENOMEM);

                goto fail;

            }



            mppformat = mpp_frame_get_fmt(mppframe);

            drmformat = rkmpp_get_frameformat(mppformat);



            hwframes = (AVHWFramesContext*)decoder->frames_ref->data;

            hwframes->format    = AV_PIX_FMT_DRM_PRIME;

            hwframes->sw_format = drmformat == DRM_FORMAT_NV12 ? AV_PIX_FMT_NV12 : AV_PIX_FMT_NONE;

            hwframes->width     = VAR_0->width;

            hwframes->height    = VAR_0->height;

            VAR_2 = av_hwframe_ctx_init(decoder->frames_ref);

            if (VAR_2 < 0)

                goto fail;



            

            VAR_2 = AVERROR(EAGAIN);

            goto fail;

        } else if (mpp_frame_get_eos(mppframe)) {

            av_log(VAR_0, AV_LOG_DEBUG, "Received a EOS VAR_1.\n");

            decoder->eos_reached = 1;

            VAR_2 = AVERROR_EOF;

            goto fail;

        } else if (mpp_frame_get_discard(mppframe)) {

            av_log(VAR_0, AV_LOG_DEBUG, "Received a discard VAR_1.\n");

            VAR_2 = AVERROR(EAGAIN);

            goto fail;

        } else if (mpp_frame_get_errinfo(mppframe)) {

            av_log(VAR_0, AV_LOG_ERROR, "Received a errinfo VAR_1.\n");

            VAR_2 = AVERROR_UNKNOWN;

            goto fail;

        }



        

        av_log(VAR_0, AV_LOG_DEBUG, "Received a VAR_1.\n");



        

        VAR_1->format           = AV_PIX_FMT_DRM_PRIME;

        VAR_1->width            = mpp_frame_get_width(mppframe);

        VAR_1->height           = mpp_frame_get_height(mppframe);

        VAR_1->pts              = mpp_frame_get_pts(mppframe);

        VAR_1->color_range      = mpp_frame_get_color_range(mppframe);

        VAR_1->color_primaries  = mpp_frame_get_color_primaries(mppframe);

        VAR_1->color_trc        = mpp_frame_get_color_trc(mppframe);

        VAR_1->colorspace       = mpp_frame_get_colorspace(mppframe);



        VAR_4 = mpp_frame_get_mode(mppframe);

        VAR_1->interlaced_frame = ((VAR_4 & MPP_FRAME_FLAG_FIELD_ORDER_MASK) == MPP_FRAME_FLAG_DEINTERLACED);

        VAR_1->top_field_first  = ((VAR_4 & MPP_FRAME_FLAG_FIELD_ORDER_MASK) == MPP_FRAME_FLAG_TOP_FIRST);



        mppformat = mpp_frame_get_fmt(mppframe);

        drmformat = rkmpp_get_frameformat(mppformat);



        

        buffer = mpp_frame_get_buffer(mppframe);

        if (buffer) {

            desc = av_mallocz(sizeof(AVDRMFrameDescriptor));

            if (!desc) {

                VAR_2 = AVERROR(ENOMEM);

                goto fail;

            }



            desc->nb_objects = 1;

            desc->objects[0].fd = mpp_buffer_get_fd(buffer);

            desc->objects[0].size = mpp_buffer_get_size(buffer);



            desc->nb_layers = 1;

            layer = &desc->layers[0];

            layer->format = drmformat;

            layer->nb_planes = 2;



            layer->planes[0].object_index = 0;

            layer->planes[0].offset = 0;

            layer->planes[0].pitch = mpp_frame_get_hor_stride(mppframe);



            layer->planes[1].object_index = 0;

            layer->planes[1].offset = layer->planes[0].pitch * mpp_frame_get_ver_stride(mppframe);

            layer->planes[1].pitch = layer->planes[0].pitch;



            

            

            framecontextref = av_buffer_allocz(sizeof(*framecontext));

            if (!framecontextref) {

                VAR_2 = AVERROR(ENOMEM);

                goto fail;

            }



            

            framecontext = (RKMPPFrameContext *)framecontextref->data;

            framecontext->decoder_ref = av_buffer_ref(rk_context->decoder_ref);

            framecontext->VAR_1 = mppframe;



            VAR_1->data[0]  = (uint8_t *)desc;

            VAR_1->buf[0]   = av_buffer_create((uint8_t *)desc, sizeof(*desc), rkmpp_release_frame,

                                               framecontextref, AV_BUFFER_FLAG_READONLY);



            if (!VAR_1->buf[0]) {

                VAR_2 = AVERROR(ENOMEM);

                goto fail;

            }



            VAR_1->hw_frames_ctx = av_buffer_ref(decoder->frames_ref);

            if (!VAR_1->hw_frames_ctx) {

                VAR_2 = AVERROR(ENOMEM);

                goto fail;

            }



            decoder->first_frame = 0;

            return 0;

        } else {

            av_log(VAR_0, AV_LOG_ERROR, "Failed to retrieve the VAR_1 buffer, VAR_1 is dropped (code = %d)\n", VAR_2);

            mpp_frame_deinit(&mppframe);

        }

    } else if (decoder->eos_reached) {

        return AVERROR_EOF;

    } else if (VAR_2 == MPP_ERR_TIMEOUT) {

        av_log(VAR_0, AV_LOG_DEBUG, "Timeout when trying to get a VAR_1 from MPP\n");

    }



    return AVERROR(EAGAIN);



fail:

    if (mppframe)

        mpp_frame_deinit(&mppframe);



    if (framecontext)

        av_buffer_unref(&framecontext->decoder_ref);



    if (framecontextref)

        av_buffer_unref(&framecontextref);



    if (desc)

        av_free(desc);



    return VAR_2;

}

static int FUNC_0(AVCodecContext *VAR_0, AVFrame *VAR_1)
{ RKMPPDecodeContext *rk_context = VAR_0->priv_data; RKMPPDecoder *decoder = (RKMPPDecoder *)rk_context->decoder_ref->data; RKMPPFrameContext *framecontext = NULL; AVBufferRef *framecontextref = NULL; int VAR_2; MppFrame mppframe = NULL; MppBuffer buffer = NULL; AVDRMFrameDescriptor *desc = NULL; AVDRMLayerDescriptor *layer = NULL; int VAR_3 = 0; int VAR_4; MppFrameFormat mppformat; uint32_t drmformat; retry_get_frame:
VAR_2 = decoder->mpi->decode_get_frame(decoder->ctx, &mppframe); if (VAR_2 != MPP_OK && VAR_2 != MPP_ERR_TIMEOUT && !decoder->first_frame) { if (VAR_3 < 5) { av_log(VAR_0, AV_LOG_DEBUG, "Failed to get a VAR_1, retrying (code = %d, VAR_3 = %d)\n", VAR_2, VAR_3); usleep(10000); VAR_3++; goto retry_get_frame; } else { av_log(VAR_0, AV_LOG_ERROR, "Failed to get a VAR_1 from MPP (code = %d)\n", VAR_2); goto fail; } } if (mppframe) { if (mpp_frame_get_info_change(mppframe)) { AVHWFramesContext *hwframes; av_log(VAR_0, AV_LOG_INFO, "Decoder noticed an info change (%dx%d), format=%d\n",
(int)mpp_frame_get_width(mppframe), (int)mpp_frame_get_height(mppframe),
(int)mpp_frame_get_fmt(mppframe)); VAR_0->width = mpp_frame_get_width(mppframe); VAR_0->height = mpp_frame_get_height(mppframe); decoder->mpi->control(decoder->ctx, MPP_DEC_SET_INFO_CHANGE_READY, NULL); decoder->first_frame = 1; av_buffer_unref(&decoder->frames_ref); decoder->frames_ref = av_hwframe_ctx_alloc(decoder->device_ref); if (!decoder->frames_ref) { VAR_2 = AVERROR(ENOMEM); goto fail; } mppformat = mpp_frame_get_fmt(mppframe); drmformat = rkmpp_get_frameformat(mppformat); hwframes = (AVHWFramesContext*)decoder->frames_ref->data; hwframes->format    = AV_PIX_FMT_DRM_PRIME; hwframes->sw_format = drmformat == DRM_FORMAT_NV12 ? AV_PIX_FMT_NV12 : AV_PIX_FMT_NONE; hwframes->width     = VAR_0->width; hwframes->height    = VAR_0->height; VAR_2 = av_hwframe_ctx_init(decoder->frames_ref); if (VAR_2 < 0)
goto fail; VAR_2 = AVERROR(EAGAIN); goto fail; } else if (mpp_frame_get_eos(mppframe)) { av_log(VAR_0, AV_LOG_DEBUG, "Received a EOS VAR_1.\n"); decoder->eos_reached = 1; VAR_2 = AVERROR_EOF; goto fail; } else if (mpp_frame_get_discard(mppframe)) { av_log(VAR_0, AV_LOG_DEBUG, "Received a discard VAR_1.\n"); VAR_2 = AVERROR(EAGAIN); goto fail; } else if (mpp_frame_get_errinfo(mppframe)) { av_log(VAR_0, AV_LOG_ERROR, "Received a errinfo VAR_1.\n"); VAR_2 = AVERROR_UNKNOWN; goto fail; } av_log(VAR_0, AV_LOG_DEBUG, "Received a VAR_1.\n"); VAR_1->format           = AV_PIX_FMT_DRM_PRIME; VAR_1->width            = mpp_frame_get_width(mppframe); VAR_1->height           = mpp_frame_get_height(mppframe); VAR_1->pts              = mpp_frame_get_pts(mppframe); VAR_1->color_range      = mpp_frame_get_color_range(mppframe); VAR_1->color_primaries  = mpp_frame_get_color_primaries(mppframe); VAR_1->color_trc        = mpp_frame_get_color_trc(mppframe); VAR_1->colorspace       = mpp_frame_get_colorspace(mppframe); VAR_4 = mpp_frame_get_mode(mppframe); VAR_1->interlaced_frame = ((VAR_4 & MPP_FRAME_FLAG_FIELD_ORDER_MASK) == MPP_FRAME_FLAG_DEINTERLACED); VAR_1->top_field_first  = ((VAR_4 & MPP_FRAME_FLAG_FIELD_ORDER_MASK) == MPP_FRAME_FLAG_TOP_FIRST); mppformat = mpp_frame_get_fmt(mppframe); drmformat = rkmpp_get_frameformat(mppformat); buffer = mpp_frame_get_buffer(mppframe); if (buffer) { desc = av_mallocz(sizeof(AVDRMFrameDescriptor)); if (!desc) { VAR_2 = AVERROR(ENOMEM); goto fail; } desc->nb_objects = 1; desc->objects[0].fd = mpp_buffer_get_fd(buffer); desc->objects[0].size = mpp_buffer_get_size(buffer); desc->nb_layers = 1; layer = &desc->layers[0]; layer->format = drmformat; layer->nb_planes = 2; layer->planes[0].object_index = 0; layer->planes[0].offset = 0; layer->planes[0].pitch = mpp_frame_get_hor_stride(mppframe); layer->planes[1].object_index = 0; layer->planes[1].offset = layer->planes[0].pitch * mpp_frame_get_ver_stride(mppframe); layer->planes[1].pitch = layer->planes[0].pitch; framecontextref = av_buffer_allocz(sizeof(*framecontext)); if (!framecontextref) { VAR_2 = AVERROR(ENOMEM); goto fail; } framecontext = (RKMPPFrameContext *)framecontextref->data; framecontext->decoder_ref = av_buffer_ref(rk_context->decoder_ref); framecontext->VAR_1 = mppframe; VAR_1->data[0]  = (uint8_t *)desc; VAR_1->buf[0]   = av_buffer_create((uint8_t *)desc, sizeof(*desc), rkmpp_release_frame,
framecontextref, AV_BUFFER_FLAG_READONLY); if (!VAR_1->buf[0]) { VAR_2 = AVERROR(ENOMEM); goto fail; } VAR_1->hw_frames_ctx = av_buffer_ref(decoder->frames_ref); if (!VAR_1->hw_frames_ctx) { VAR_2 = AVERROR(ENOMEM); goto fail; } decoder->first_frame = 0; return 0; } else { av_log(VAR_0, AV_LOG_ERROR, "Failed to retrieve the VAR_1 buffer, VAR_1 is dropped (code = %d)\n", VAR_2); mpp_frame_deinit(&mppframe); } } else if (decoder->eos_reached) { return AVERROR_EOF; } else if (VAR_2 == MPP_ERR_TIMEOUT) { av_log(VAR_0, AV_LOG_DEBUG, "Timeout when trying to get a VAR_1 from MPP\n"); } return AVERROR(EAGAIN); fail:
if (mppframe)
mpp_frame_deinit(&mppframe); if (framecontext)
av_buffer_unref(&framecontext->decoder_ref); if (framecontextref)
av_buffer_unref(&framecontextref); if (desc)
av_free(desc); return VAR_2; }