From 9e5abe336ad789225b162bd57456f2b92bdf91ea Mon Sep 17 00:00:00 2001 From: Lionel CHAZALLON Date: Sat, 23 Sep 2017 22:32:49 -0700 Subject: [PATCH 1/9] [PATCH v7] Add support for RockChip Media Process Platform This adds hardware decoding for h264 / HEVC / VP8 using MPP Rockchip API. Will return frames holding an AVDRMFrameDescriptor struct in buf[0] that allows drm / dmabuf usage. Was tested on RK3288 (TinkerBoard) and RK3328. Changes from Previous patch : - Frame colorspace info is now filled. - Frame interlacing is now filled (Note : currently had a bug in mpp which will be fixed soon by rockchip, will set the to progressive). - hw_frame_context returns none as format for the rockchip specific 10 bits. - Added support for VP9 codec. - removed MPG4 codec : it seems that MPP doesn't handle properly all the MPEG4 formats - removed MPEG2 : there is still an issue with MPEG2 decoder, this is being investigated by RockChip. - the ION format has been kept for MPP init (rather than DRM) as this is the only one working right, using DRM format will cause assertions upon close. - Other minor comments have been taken into account --- Changelog | 2 +- configure | 15 ++ libavcodec/Makefile | 4 + libavcodec/allcodecs.c | 4 + libavcodec/rkmppdec.c | 591 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 615 insertions(+), 1 deletion(-) create mode 100644 libavcodec/rkmppdec.c diff --git a/Changelog b/Changelog index 678dcdadc734f..0006430e67257 100644 --- a/Changelog +++ b/Changelog @@ -50,7 +50,7 @@ version : - KMS screen grabber - CUDA thumbnail filter - V4L2 mem2mem HW assisted codecs - +- Rockchip MPP hardware decoding version 3.3: - CrystalHD decoder moved to new decode API diff --git a/configure b/configure index 16fc2bd074046..a08653c5d0f20 100755 --- a/configure +++ b/configure @@ -316,6 +316,7 @@ External library support: --disable-nvenc disable Nvidia video encoding code [autodetect] --enable-omx enable OpenMAX IL code [no] --enable-omx-rpi enable OpenMAX IL code for Raspberry Pi [no] + --enable-rkmpp enable Rockchip Media Process Platform code [no] --disable-vaapi disable Video Acceleration API (mainly Unix/Intel) code [autodetect] --disable-vda disable Apple Video Decode Acceleration code [autodetect] --disable-vdpau disable Nvidia Video Decode and Presentation API for Unix code [autodetect] @@ -1548,6 +1549,7 @@ EXTERNAL_LIBRARY_VERSION3_LIST=" libopencore_amrnb libopencore_amrwb libvo_amrwbenc + rkmpp " EXTERNAL_LIBRARY_GPLV3_LIST=" @@ -2782,6 +2784,8 @@ h264_qsv_decoder_deps="libmfx" h264_qsv_decoder_select="h264_mp4toannexb_bsf h264_parser qsvdec h264_qsv_hwaccel" h264_qsv_encoder_deps="libmfx" h264_qsv_encoder_select="qsvenc" +h264_rkmpp_decoder_deps="rkmpp" +h264_rkmpp_decoder_select="h264_mp4toannexb_bsf" h264_vaapi_encoder_deps="VAEncPictureParameterBufferH264" h264_vaapi_encoder_select="vaapi_encode golomb" h264_vda_decoder_deps="vda" @@ -2799,6 +2803,8 @@ hevc_qsv_decoder_deps="libmfx" hevc_qsv_decoder_select="hevc_mp4toannexb_bsf hevc_parser qsvdec hevc_qsv_hwaccel" hevc_qsv_encoder_deps="libmfx" hevc_qsv_encoder_select="hevcparse qsvenc" +hevc_rkmpp_decoder_deps="rkmpp" +hevc_rkmpp_decoder_select="hevc_mp4toannexb_bsf" hevc_vaapi_encoder_deps="VAEncPictureParameterBufferHEVC" hevc_vaapi_encoder_select="vaapi_encode golomb" hevc_v4l2m2m_decoder_deps="v4l2_m2m hevc_v4l2_m2m" @@ -2845,12 +2851,14 @@ vp8_cuvid_decoder_deps="cuda cuvid" vp8_mediacodec_decoder_deps="mediacodec" vp8_qsv_decoder_deps="libmfx" vp8_qsv_decoder_select="qsvdec vp8_qsv_hwaccel vp8_parser" +vp8_rkmpp_decoder_deps="rkmpp" vp8_vaapi_encoder_deps="VAEncPictureParameterBufferVP8" vp8_vaapi_encoder_select="vaapi_encode" vp8_v4l2m2m_decoder_deps="v4l2_m2m vp8_v4l2_m2m" vp8_v4l2m2m_encoder_deps="v4l2_m2m vp8_v4l2_m2m" vp9_cuvid_decoder_deps="cuda cuvid" vp9_mediacodec_decoder_deps="mediacodec" +vp9_rkmpp_decoder_deps="rkmpp" vp9_vaapi_encoder_deps="VAEncPictureParameterBufferVP9" vp9_vaapi_encoder_select="vaapi_encode" vp9_v4l2m2m_decoder_deps="v4l2_m2m vp9_v4l2_m2m" @@ -6060,6 +6068,13 @@ enabled openssl && { use_pkg_config openssl openssl/ssl.h OPENSSL_init check_lib openssl openssl/ssl.h SSL_library_init -lssl32 -leay32 || check_lib openssl openssl/ssl.h SSL_library_init -lssl -lcrypto -lws2_32 -lgdi32 || die "ERROR: openssl not found"; } +enabled rkmpp && { { require_pkg_config rockchip_mpp rockchip/rk_mpi.h mpp_create || + die "ERROR : Rockchip MPP was not found."; } && + { check_func_headers rockchip/rk_mpi_cmd.h "MPP_DEC_GET_FREE_PACKET_SLOT_COUNT" || + die "ERROR: Rockchip MPP is outdated, please get a more recent one."; } && + { enabled libdrm || + die "ERROR: rkmpp requires --enable-libdrm"; } + } if enabled gcrypt; then GCRYPT_CONFIG="${cross_prefix}libgcrypt-config" diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 1b17c27408569..c4ec09b1c4535 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -341,6 +341,7 @@ OBJS-$(CONFIG_H264_VDA_DECODER) += vda_h264_dec.o OBJS-$(CONFIG_H264_OMX_ENCODER) += omx.o OBJS-$(CONFIG_H264_QSV_DECODER) += qsvdec_h2645.o OBJS-$(CONFIG_H264_QSV_ENCODER) += qsvenc_h264.o +OBJS-$(CONFIG_H264_RKMPP_DECODER) += rkmppdec.o OBJS-$(CONFIG_H264_VAAPI_ENCODER) += vaapi_encode_h264.o vaapi_encode_h26x.o OBJS-$(CONFIG_H264_VIDEOTOOLBOX_ENCODER) += videotoolboxenc.o OBJS-$(CONFIG_H264_V4L2M2M_DECODER) += v4l2_m2m_dec.o @@ -357,6 +358,7 @@ OBJS-$(CONFIG_NVENC_HEVC_ENCODER) += nvenc_hevc.o OBJS-$(CONFIG_HEVC_QSV_DECODER) += qsvdec_h2645.o OBJS-$(CONFIG_HEVC_QSV_ENCODER) += qsvenc_hevc.o hevc_ps_enc.o \ hevc_data.o +OBJS-$(CONFIG_HEVC_RKMPP_DECODER) += rkmppdec.o OBJS-$(CONFIG_HEVC_VAAPI_ENCODER) += vaapi_encode_h265.o vaapi_encode_h26x.o OBJS-$(CONFIG_HEVC_V4L2M2M_DECODER) += v4l2_m2m_dec.o OBJS-$(CONFIG_HEVC_V4L2M2M_ENCODER) += v4l2_m2m_enc.o @@ -636,6 +638,7 @@ OBJS-$(CONFIG_VP8_DECODER) += vp8.o vp56rac.o OBJS-$(CONFIG_VP8_CUVID_DECODER) += cuvid.o OBJS-$(CONFIG_VP8_MEDIACODEC_DECODER) += mediacodecdec.o OBJS-$(CONFIG_VP8_QSV_DECODER) += qsvdec_other.o +OBJS-$(CONFIG_VP8_RKMPP_DECODER) += rkmppdec.o OBJS-$(CONFIG_VP8_VAAPI_ENCODER) += vaapi_encode_vp8.o OBJS-$(CONFIG_VP8_V4L2M2M_DECODER) += v4l2_m2m_dec.o OBJS-$(CONFIG_VP8_V4L2M2M_ENCODER) += v4l2_m2m_enc.o @@ -644,6 +647,7 @@ OBJS-$(CONFIG_VP9_DECODER) += vp9.o vp9data.o vp9dsp.o vp9lpf.o vp9r vp9dsp_8bpp.o vp9dsp_10bpp.o vp9dsp_12bpp.o OBJS-$(CONFIG_VP9_CUVID_DECODER) += cuvid.o OBJS-$(CONFIG_VP9_MEDIACODEC_DECODER) += mediacodecdec.o +OBJS-$(CONFIG_VP9_RKMPP_DECODER) += rkmppdec.o OBJS-$(CONFIG_VP9_VAAPI_ENCODER) += vaapi_encode_vp9.o OBJS-$(CONFIG_VPLAYER_DECODER) += textdec.o ass.o OBJS-$(CONFIG_VP9_V4L2M2M_DECODER) += v4l2_m2m_dec.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index db2db158f3443..f5ef280313626 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -215,6 +215,7 @@ static void register_all(void) REGISTER_DECODER(H264_MEDIACODEC, h264_mediacodec); REGISTER_DECODER(H264_MMAL, h264_mmal); REGISTER_DECODER(H264_QSV, h264_qsv); + REGISTER_DECODER(H264_RKMPP, h264_rkmpp); REGISTER_DECODER(H264_VDA, h264_vda); #if FF_API_VDPAU REGISTER_DECODER(H264_VDPAU, h264_vdpau); @@ -222,6 +223,7 @@ static void register_all(void) REGISTER_ENCDEC (HAP, hap); REGISTER_DECODER(HEVC, hevc); REGISTER_DECODER(HEVC_QSV, hevc_qsv); + REGISTER_DECODER(HEVC_RKMPP, hevc_rkmpp); REGISTER_ENCDEC (HEVC_V4L2M2M, hevc_v4l2m2m); REGISTER_DECODER(HNM4_VIDEO, hnm4_video); REGISTER_DECODER(HQ_HQA, hq_hqa); @@ -380,8 +382,10 @@ static void register_all(void) REGISTER_DECODER(VP6F, vp6f); REGISTER_DECODER(VP7, vp7); REGISTER_DECODER(VP8, vp8); + REGISTER_DECODER(VP8_RKMPP, vp8_rkmpp); REGISTER_ENCDEC (VP8_V4L2M2M, vp8_v4l2m2m); REGISTER_DECODER(VP9, vp9); + REGISTER_DECODER(VP9_RKMPP, vp9_rkmpp); REGISTER_DECODER(VP9_V4L2M2M, vp9_v4l2m2m); REGISTER_DECODER(VQA, vqa); REGISTER_DECODER(BITPACKED, bitpacked); diff --git a/libavcodec/rkmppdec.c b/libavcodec/rkmppdec.c new file mode 100644 index 0000000000000..5b04a55027a09 --- /dev/null +++ b/libavcodec/rkmppdec.c @@ -0,0 +1,591 @@ +/* + * RockChip MPP Video Decoder + * Copyright (c) 2017 Lionel CHAZALLON + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include +#include +#include +#include + +#include "avcodec.h" +#include "decode.h" +#include "internal.h" +#include "libavutil/buffer.h" +#include "libavutil/common.h" +#include "libavutil/frame.h" +#include "libavutil/hwcontext.h" +#include "libavutil/hwcontext_drm.h" +#include "libavutil/imgutils.h" +#include "libavutil/log.h" + +#define RECEIVE_FRAME_TIMEOUT 100 +#define FRAMEGROUP_MAX_FRAMES 16 + +typedef struct { + MppCtx ctx; + MppApi *mpi; + MppBufferGroup frame_group; + + char first_frame; + char first_packet; + char eos_reached; + + AVBufferRef *frames_ref; + AVBufferRef *device_ref; +} RKMPPDecoder; + +typedef struct { + AVClass *av_class; + AVBufferRef *decoder_ref; +} RKMPPDecodeContext; + +typedef struct { + MppFrame frame; + AVBufferRef *decoder_ref; +} RKMPPFrameContext; + +static MppCodingType rkmpp_get_codingtype(AVCodecContext *avctx) +{ + switch (avctx->codec_id) { + case AV_CODEC_ID_H264: return MPP_VIDEO_CodingAVC; + case AV_CODEC_ID_HEVC: return MPP_VIDEO_CodingHEVC; + case AV_CODEC_ID_VP8: return MPP_VIDEO_CodingVP8; + case AV_CODEC_ID_VP9: return MPP_VIDEO_CodingVP9; + default: return MPP_VIDEO_CodingUnused; + } +} + +static int rkmpp_get_frameformat(MppFrameFormat mppformat) +{ + switch (mppformat) { + case MPP_FMT_YUV420SP: return DRM_FORMAT_NV12; +#ifdef DRM_FORMAT_NV12_10 + case MPP_FMT_YUV420SP_10BIT: return DRM_FORMAT_NV12_10; +#endif + default: return 0; + } +} + +static int rkmpp_write_data(AVCodecContext *avctx, uint8_t *buffer, int size, int64_t pts) +{ + RKMPPDecodeContext *rk_context = avctx->priv_data; + RKMPPDecoder *decoder = (RKMPPDecoder *)rk_context->decoder_ref->data; + int ret = MPP_NOK; + MppPacket packet; + + // create the MPP packet + ret = mpp_packet_init(&packet, buffer, size); + if (ret != MPP_OK) { + av_log(avctx, AV_LOG_ERROR, "Failed to init MPP packet (code = %d)\n", ret); + return AVERROR_UNKNOWN; + } + + mpp_packet_set_pts(packet, pts); + + if (!buffer) + mpp_packet_set_eos(packet); + + ret = decoder->mpi->decode_put_packet(decoder->ctx, packet); + if (ret != MPP_OK) { + if (ret == MPP_ERR_BUFFER_FULL) { + av_log(avctx, AV_LOG_DEBUG, "Buffer full writing %d bytes to decoder\n", size); + ret = AVERROR(EAGAIN); + } else + ret = AVERROR_UNKNOWN; + } + else + av_log(avctx, AV_LOG_DEBUG, "Wrote %d bytes to decoder\n", size); + + mpp_packet_deinit(&packet); + + return ret; +} + +static int rkmpp_close_decoder(AVCodecContext *avctx) +{ + RKMPPDecodeContext *rk_context = avctx->priv_data; + av_buffer_unref(&rk_context->decoder_ref); + return 0; +} + +static void rkmpp_release_decoder(void *opaque, uint8_t *data) +{ + RKMPPDecoder *decoder = (RKMPPDecoder *)data; + + if (decoder->mpi) { + decoder->mpi->reset(decoder->ctx); + mpp_destroy(decoder->ctx); + decoder->ctx = NULL; + } + + if (decoder->frame_group) { + mpp_buffer_group_put(decoder->frame_group); + decoder->frame_group = NULL; + } + + av_buffer_unref(&decoder->frames_ref); + av_buffer_unref(&decoder->device_ref); + + av_free(decoder); +} + +static int rkmpp_init_decoder(AVCodecContext *avctx) +{ + RKMPPDecodeContext *rk_context = avctx->priv_data; + RKMPPDecoder *decoder = NULL; + MppCodingType codectype = MPP_VIDEO_CodingUnused; + int ret = MPP_NOK; + RK_S64 paramS64; + RK_S32 paramS32; + + avctx->pix_fmt = AV_PIX_FMT_DRM_PRIME; + + // create a decoder and a ref to it + decoder = av_mallocz(sizeof(RKMPPDecoder)); + if (!decoder) { + ret = AVERROR(ENOMEM); + goto fail; + } + + rk_context->decoder_ref = av_buffer_create((uint8_t *)decoder, sizeof(*decoder), rkmpp_release_decoder, + NULL, AV_BUFFER_FLAG_READONLY); + if (!rk_context->decoder_ref) { + av_free(decoder); + ret = AVERROR(ENOMEM); + goto fail; + } + + av_log(avctx, AV_LOG_DEBUG, "Initializing RKMPP decoder.\n"); + + codectype = rkmpp_get_codingtype(avctx); + if (codectype == MPP_VIDEO_CodingUnused) { + av_log(avctx, AV_LOG_ERROR, "Unknown codec type (%d).\n", avctx->codec_id); + ret = AVERROR_UNKNOWN; + goto fail; + } + + ret = mpp_check_support_format(MPP_CTX_DEC, codectype); + if (ret != MPP_OK) { + av_log(avctx, AV_LOG_ERROR, "Codec type (%d) unsupported by MPP\n", avctx->codec_id); + ret = AVERROR_UNKNOWN; + goto fail; + } + + // Create the MPP context + ret = mpp_create(&decoder->ctx, &decoder->mpi); + if (ret != MPP_OK) { + av_log(avctx, AV_LOG_ERROR, "Failed to create MPP context (code = %d).\n", ret); + ret = AVERROR_UNKNOWN; + goto fail; + } + + // initialize mpp + ret = mpp_init(decoder->ctx, MPP_CTX_DEC, codectype); + if (ret != MPP_OK) { + av_log(avctx, AV_LOG_ERROR, "Failed to initialize MPP context (code = %d).\n", ret); + ret = AVERROR_UNKNOWN; + goto fail; + } + + // make decode calls blocking with a timeout + paramS32 = MPP_POLL_BLOCK; + ret = decoder->mpi->control(decoder->ctx, MPP_SET_OUTPUT_BLOCK, ¶mS32); + if (ret != MPP_OK) { + av_log(avctx, AV_LOG_ERROR, "Failed to set blocking mode on MPI (code = %d).\n", ret); + ret = AVERROR_UNKNOWN; + goto fail; + } + + paramS64 = RECEIVE_FRAME_TIMEOUT; + ret = decoder->mpi->control(decoder->ctx, MPP_SET_OUTPUT_BLOCK_TIMEOUT, ¶mS64); + if (ret != MPP_OK) { + av_log(avctx, AV_LOG_ERROR, "Failed to set block timeout on MPI (code = %d).\n", ret); + ret = AVERROR_UNKNOWN; + goto fail; + } + + ret = mpp_buffer_group_get_internal(&decoder->frame_group, MPP_BUFFER_TYPE_ION); + if (ret) { + av_log(avctx, AV_LOG_ERROR, "Failed to retrieve buffer group (code = %d)\n", ret); + ret = AVERROR_UNKNOWN; + goto fail; + } + + ret = decoder->mpi->control(decoder->ctx, MPP_DEC_SET_EXT_BUF_GROUP, decoder->frame_group); + if (ret) { + av_log(avctx, AV_LOG_ERROR, "Failed to assign buffer group (code = %d)\n", ret); + ret = AVERROR_UNKNOWN; + goto fail; + } + + ret = mpp_buffer_group_limit_config(decoder->frame_group, 0, FRAMEGROUP_MAX_FRAMES); + if (ret) { + av_log(avctx, AV_LOG_ERROR, "Failed to set buffer group limit (code = %d)\n", ret); + ret = AVERROR_UNKNOWN; + goto fail; + } + + decoder->first_packet = 1; + + av_log(avctx, AV_LOG_DEBUG, "RKMPP decoder initialized successfully.\n"); + + decoder->device_ref = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_DRM); + if (!decoder->device_ref) { + ret = AVERROR(ENOMEM); + goto fail; + } + ret = av_hwdevice_ctx_init(decoder->device_ref); + if (ret < 0) + goto fail; + + return 0; + +fail: + av_log(avctx, AV_LOG_ERROR, "Failed to initialize RKMPP decoder.\n"); + rkmpp_close_decoder(avctx); + return ret; +} + +static int rkmpp_send_packet(AVCodecContext *avctx, const AVPacket *avpkt) +{ + RKMPPDecodeContext *rk_context = avctx->priv_data; + RKMPPDecoder *decoder = (RKMPPDecoder *)rk_context->decoder_ref->data; + int ret = MPP_NOK; + + // handle EOF + if (!avpkt->size) { + av_log(avctx, AV_LOG_DEBUG, "End of stream.\n"); + decoder->eos_reached = 1; + ret = rkmpp_write_data(avctx, NULL, 0, 0); + if (ret) + av_log(avctx, AV_LOG_ERROR, "Failed to send EOS to decoder (code = %d)\n", ret); + return ret; + } + + // on first packet, send extradata + if (decoder->first_packet) { + if (avctx->extradata_size) { + ret = rkmpp_write_data(avctx, avctx->extradata, + avctx->extradata_size, + avpkt->pts); + if (ret) { + av_log(avctx, AV_LOG_ERROR, "Failed to write extradata to decoder (code = %d)\n", ret); + return ret; + } + } + decoder->first_packet = 0; + } + + // now send packet + ret = rkmpp_write_data(avctx, avpkt->data, avpkt->size, avpkt->pts); + if (ret && ret!=AVERROR(EAGAIN)) + av_log(avctx, AV_LOG_ERROR, "Failed to write data to decoder (code = %d)\n", ret); + + return ret; +} + +static void rkmpp_release_frame(void *opaque, uint8_t *data) +{ + AVDRMFrameDescriptor *desc = (AVDRMFrameDescriptor *)data; + AVBufferRef *framecontextref = (AVBufferRef *)opaque; + RKMPPFrameContext *framecontext = (RKMPPFrameContext *)framecontextref->data; + + mpp_frame_deinit(&framecontext->frame); + av_buffer_unref(&framecontext->decoder_ref); + av_buffer_unref(&framecontextref); + + av_free(desc); +} + +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 = MPP_NOK; + MppFrame mppframe = NULL; + MppBuffer buffer = NULL; + AVDRMFrameDescriptor *desc = NULL; + AVDRMLayerDescriptor *layer = NULL; + int retrycount = 0; + int format, mode; + + // 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; + } + + format = (int)mpp_frame_get_fmt(mppframe); + + hwframes = (AVHWFramesContext*)decoder->frames_ref->data; + hwframes->format = AV_PIX_FMT_DRM_PRIME; + hwframes->sw_format = rkmpp_get_frameformat(format) == 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); + + + // 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 = rkmpp_get_frameformat(mpp_frame_get_fmt(mppframe)); + 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_receive_frame(AVCodecContext *avctx, AVFrame *frame) +{ + RKMPPDecodeContext *rk_context = avctx->priv_data; + RKMPPDecoder *decoder = (RKMPPDecoder *)rk_context->decoder_ref->data; + int ret = MPP_NOK; + AVPacket pkt = {0}; + RK_S32 freeslots; + + if (!decoder->eos_reached) { + // we get the available slots in decoder + ret = decoder->mpi->control(decoder->ctx, MPP_DEC_GET_FREE_PACKET_SLOT_COUNT, &freeslots); + if (ret != MPP_OK) { + av_log(avctx, AV_LOG_ERROR, "Failed to get decoder free slots (code = %d).\n", ret); + return ret; + } + + if (freeslots > 0) { + ret = ff_decode_get_packet(avctx, &pkt); + if (ret < 0 && ret != AVERROR_EOF) { + return ret; + } + + ret = rkmpp_send_packet(avctx, &pkt); + av_packet_unref(&pkt); + + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "Failed to send packet to decoder (code = %d)\n", ret); + return ret; + } + } + + // make sure we keep decoder full + if (freeslots > 1 && decoder->first_frame) + return AVERROR(EAGAIN); + } + + return rkmpp_retrieve_frame(avctx, frame); +} + +static void rkmpp_flush(AVCodecContext *avctx) +{ + RKMPPDecodeContext *rk_context = avctx->priv_data; + RKMPPDecoder *decoder = (RKMPPDecoder *)rk_context->decoder_ref->data; + int ret = MPP_NOK; + + av_log(avctx, AV_LOG_DEBUG, "Flush.\n"); + + ret = decoder->mpi->reset(decoder->ctx); + if (ret == MPP_OK) { + decoder->first_frame = 1; + decoder->first_packet = 1; + } else + av_log(avctx, AV_LOG_ERROR, "Failed to reset MPI (code = %d)\n", ret); +} + + +#define RKMPP_DEC_CLASS(NAME) \ + static const AVClass rkmpp_##NAME##_dec_class = { \ + .class_name = "rkmpp_" #NAME "_dec", \ + .version = LIBAVUTIL_VERSION_INT, \ + }; + +#define RKMPP_DEC(NAME, ID, BSFS) \ + RKMPP_DEC_CLASS(NAME) \ + AVCodec ff_##NAME##_rkmpp_decoder = { \ + .name = #NAME "_rkmpp", \ + .long_name = NULL_IF_CONFIG_SMALL(#NAME " (rkmpp)"), \ + .type = AVMEDIA_TYPE_VIDEO, \ + .id = ID, \ + .priv_data_size = sizeof(RKMPPDecodeContext), \ + .init = rkmpp_init_decoder, \ + .close = rkmpp_close_decoder, \ + .receive_frame = rkmpp_receive_frame, \ + .flush = rkmpp_flush, \ + .priv_class = &rkmpp_##NAME##_dec_class, \ + .capabilities = AV_CODEC_CAP_DELAY, \ + .caps_internal = AV_CODEC_CAP_AVOID_PROBING, \ + .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_DRM_PRIME, \ + AV_PIX_FMT_NONE}, \ + .bsfs = BSFS, \ + }; + +RKMPP_DEC(h264, AV_CODEC_ID_H264, "h264_mp4toannexb") +RKMPP_DEC(hevc, AV_CODEC_ID_HEVC, "hevc_mp4toannexb") +RKMPP_DEC(vp8, AV_CODEC_ID_VP8, NULL) +RKMPP_DEC(vp9, AV_CODEC_ID_VP9, NULL) From c6e5e781d187793d66a5041cac6fb14ee6d495fa Mon Sep 17 00:00:00 2001 From: LongChair Date: Sat, 23 Sep 2017 01:49:23 -0700 Subject: [PATCH 2/9] Add MPEG2 Decoder --- configure | 1 + libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 1 + libavcodec/rkmppdec.c | 10 ++++++++++ 4 files changed, 13 insertions(+) diff --git a/configure b/configure index a08653c5d0f20..fe7a5b23af495 100755 --- a/configure +++ b/configure @@ -2824,6 +2824,7 @@ mpeg2_qsv_decoder_deps="libmfx" mpeg2_qsv_decoder_select="qsvdec mpeg2_qsv_hwaccel" mpeg2_qsv_encoder_deps="libmfx" mpeg2_qsv_encoder_select="qsvenc" +mpeg2_rkmpp_decoder_deps="rkmpp" mpeg2_vaapi_encoder_deps="VAEncPictureParameterBufferMPEG2" mpeg2_vaapi_encoder_select="vaapi_encode" mpeg2_v4l2m2m_decoder_deps="v4l2_m2m mpeg2_v4l2_m2m" diff --git a/libavcodec/Makefile b/libavcodec/Makefile index c4ec09b1c4535..4209d129ed2ab 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -435,6 +435,7 @@ OBJS-$(CONFIG_MPEG1_V4L2M2M_DECODER) += v4l2_m2m_dec.o OBJS-$(CONFIG_MPEG2_MMAL_DECODER) += mmaldec.o OBJS-$(CONFIG_MPEG2_QSV_DECODER) += qsvdec_other.o OBJS-$(CONFIG_MPEG2_QSV_ENCODER) += qsvenc_mpeg2.o +OBJS-$(CONFIG_MPEG2_RKMPP_DECODER) += rkmppdec.o OBJS-$(CONFIG_MPEG2VIDEO_DECODER) += mpeg12dec.o mpeg12.o mpeg12data.o OBJS-$(CONFIG_MPEG2VIDEO_ENCODER) += mpeg12enc.o mpeg12.o OBJS-$(CONFIG_MPEG2_MEDIACODEC_DECODER) += mediacodecdec.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index f5ef280313626..5bb41bd1d713a 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -257,6 +257,7 @@ static void register_all(void) #endif /* FF_API_XVMC */ REGISTER_ENCDEC (MPEG1VIDEO, mpeg1video); REGISTER_ENCDEC (MPEG2VIDEO, mpeg2video); + REGISTER_DECODER(MPEG2_RKMPP, mpeg2_rkmpp); REGISTER_ENCDEC (MPEG4, mpeg4); REGISTER_DECODER(MPEG4_CRYSTALHD, mpeg4_crystalhd); REGISTER_ENCDEC (MPEG4_V4L2M2M, mpeg4_v4l2m2m); diff --git a/libavcodec/rkmppdec.c b/libavcodec/rkmppdec.c index 5b04a55027a09..e816e569bc914 100644 --- a/libavcodec/rkmppdec.c +++ b/libavcodec/rkmppdec.c @@ -68,6 +68,7 @@ static MppCodingType rkmpp_get_codingtype(AVCodecContext *avctx) switch (avctx->codec_id) { case AV_CODEC_ID_H264: return MPP_VIDEO_CodingAVC; case AV_CODEC_ID_HEVC: return MPP_VIDEO_CodingHEVC; + case AV_CODEC_ID_MPEG2VIDEO: return MPP_VIDEO_CodingMPEG2; case AV_CODEC_ID_VP8: return MPP_VIDEO_CodingVP8; case AV_CODEC_ID_VP9: return MPP_VIDEO_CodingVP9; default: return MPP_VIDEO_CodingUnused; @@ -198,6 +199,14 @@ static int rkmpp_init_decoder(AVCodecContext *avctx) goto fail; } + paramS32 = 1; + ret = decoder->mpi->control(decoder->ctx, MPP_DEC_SET_PARSER_SPLIT_MODE, ¶mS32); + if (ret != MPP_OK) { + av_log(avctx, AV_LOG_ERROR, "Failed to set parser split mode on MPI (code = %d).\n", ret); + ret = AVERROR_UNKNOWN; + goto fail; + } + // initialize mpp ret = mpp_init(decoder->ctx, MPP_CTX_DEC, codectype); if (ret != MPP_OK) { @@ -587,5 +596,6 @@ static void rkmpp_flush(AVCodecContext *avctx) RKMPP_DEC(h264, AV_CODEC_ID_H264, "h264_mp4toannexb") RKMPP_DEC(hevc, AV_CODEC_ID_HEVC, "hevc_mp4toannexb") +RKMPP_DEC(mpeg2, AV_CODEC_ID_MPEG2VIDEO, NULL) RKMPP_DEC(vp8, AV_CODEC_ID_VP8, NULL) RKMPP_DEC(vp9, AV_CODEC_ID_VP9, NULL) From ee8067adc6fae9003381e12e88304fcce5cb443f Mon Sep 17 00:00:00 2001 From: LongChair Date: Sat, 23 Sep 2017 01:51:13 -0700 Subject: [PATCH 3/9] Add more debug info --- libavcodec/rkmppdec.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/libavcodec/rkmppdec.c b/libavcodec/rkmppdec.c index e816e569bc914..1e799ec56c3ea 100644 --- a/libavcodec/rkmppdec.c +++ b/libavcodec/rkmppdec.c @@ -48,6 +48,7 @@ typedef struct { char first_frame; char first_packet; char eos_reached; + int frame_count; AVBufferRef *frames_ref; AVBufferRef *device_ref; @@ -114,7 +115,7 @@ static int rkmpp_write_data(AVCodecContext *avctx, uint8_t *buffer, int size, in ret = AVERROR_UNKNOWN; } else - av_log(avctx, AV_LOG_DEBUG, "Wrote %d bytes to decoder\n", size); + av_log(avctx, AV_LOG_DEBUG, "Wrote %d bytes to decoder (pts = %" PRId64 ")\n", size, pts); mpp_packet_deinit(&packet); @@ -411,7 +412,6 @@ static int rkmpp_retrieve_frame(AVCodecContext *avctx, AVFrame *frame) } // 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; @@ -430,6 +430,7 @@ static int rkmpp_retrieve_frame(AVCodecContext *avctx, AVFrame *frame) // now setup the frame buffer info buffer = mpp_frame_get_buffer(mppframe); + // av_log(avctx, AV_LOG_DEBUG, "Received Buffer %p.\n", buffer); if (buffer) { desc = av_mallocz(sizeof(AVDRMFrameDescriptor)); if (!desc) { @@ -482,6 +483,9 @@ static int rkmpp_retrieve_frame(AVCodecContext *avctx, AVFrame *frame) goto fail; } + decoder->frame_count++; + av_log(avctx, AV_LOG_DEBUG, "Received a frame. (#%d), (%d x %d), pts=%" PRId64 "\n", decoder->frame_count, frame->width, frame->height, frame->pts); + decoder->first_frame = 0; return 0; } else { From 64ebcffa9cce0fee0bc66f1d1171aefde07058eb Mon Sep 17 00:00:00 2001 From: zillevdr Date: Tue, 3 Oct 2017 11:40:14 +0200 Subject: [PATCH 4/9] add rkmpp to AVHWAccel --- configure | 6 ++++-- libavcodec/allcodecs.c | 2 ++ libavcodec/rkmppdec.c | 8 ++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/configure b/configure index fe7a5b23af495..ca51d8fecbb20 100755 --- a/configure +++ b/configure @@ -2650,6 +2650,7 @@ h264_dxva2_hwaccel_select="h264_decoder" h264_mediacodec_hwaccel_deps="mediacodec" h264_mmal_hwaccel_deps="mmal" h264_qsv_hwaccel_deps="libmfx" +h264_rkmpp_hwaccel_deps="rkmpp" h264_vaapi_hwaccel_deps="vaapi" h264_vaapi_hwaccel_select="h264_decoder" h264_vda_hwaccel_deps="vda" @@ -2670,6 +2671,7 @@ hevc_d3d11va2_hwaccel_select="hevc_decoder" hevc_dxva2_hwaccel_deps="dxva2 DXVA_PicParams_HEVC" hevc_dxva2_hwaccel_select="hevc_decoder" hevc_qsv_hwaccel_deps="libmfx" +hevc_rkmpp_hwaccel_deps="rkmpp" hevc_vaapi_hwaccel_deps="vaapi VAPictureParameterBufferHEVC" hevc_vaapi_hwaccel_select="hevc_decoder" hevc_vdpau_hwaccel_deps="vdpau VdpPictureInfoHEVC" @@ -2785,7 +2787,7 @@ h264_qsv_decoder_select="h264_mp4toannexb_bsf h264_parser qsvdec h264_qsv_hwacce h264_qsv_encoder_deps="libmfx" h264_qsv_encoder_select="qsvenc" h264_rkmpp_decoder_deps="rkmpp" -h264_rkmpp_decoder_select="h264_mp4toannexb_bsf" +h264_rkmpp_decoder_select="h264_mp4toannexb_bsf h264_rkmpp_hwaccel" h264_vaapi_encoder_deps="VAEncPictureParameterBufferH264" h264_vaapi_encoder_select="vaapi_encode golomb" h264_vda_decoder_deps="vda" @@ -2804,7 +2806,7 @@ hevc_qsv_decoder_select="hevc_mp4toannexb_bsf hevc_parser qsvdec hevc_qsv_hwacce hevc_qsv_encoder_deps="libmfx" hevc_qsv_encoder_select="hevcparse qsvenc" hevc_rkmpp_decoder_deps="rkmpp" -hevc_rkmpp_decoder_select="hevc_mp4toannexb_bsf" +hevc_rkmpp_decoder_select="hevc_mp4toannexb_bsf h264_rkmpp_hwaccel" hevc_vaapi_encoder_deps="VAEncPictureParameterBufferHEVC" hevc_vaapi_encoder_select="vaapi_encode golomb" hevc_v4l2m2m_decoder_deps="v4l2_m2m hevc_v4l2_m2m" diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index 5bb41bd1d713a..28ab0c54b2a11 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -71,6 +71,7 @@ static void register_all(void) REGISTER_HWACCEL(H264_MEDIACODEC, h264_mediacodec); REGISTER_HWACCEL(H264_MMAL, h264_mmal); REGISTER_HWACCEL(H264_QSV, h264_qsv); + REGISTER_HWACCEL(H264_RKMPP, h264_rkmpp); REGISTER_HWACCEL(H264_VAAPI, h264_vaapi); REGISTER_HWACCEL(H264_VDA, h264_vda); REGISTER_HWACCEL(H264_VDA_OLD, h264_vda_old); @@ -82,6 +83,7 @@ static void register_all(void) REGISTER_HWACCEL(HEVC_DXVA2, hevc_dxva2); REGISTER_HWACCEL(HEVC_MEDIACODEC, hevc_mediacodec); REGISTER_HWACCEL(HEVC_QSV, hevc_qsv); + REGISTER_HWACCEL(HEVC_RKMPP, hevc_rkmpp); REGISTER_HWACCEL(HEVC_VAAPI, hevc_vaapi); REGISTER_HWACCEL(HEVC_VDPAU, hevc_vdpau); REGISTER_HWACCEL(MJPEG_CUVID, mjpeg_cuvid); diff --git a/libavcodec/rkmppdec.c b/libavcodec/rkmppdec.c index 1e799ec56c3ea..818cae7f91667 100644 --- a/libavcodec/rkmppdec.c +++ b/libavcodec/rkmppdec.c @@ -571,6 +571,13 @@ static void rkmpp_flush(AVCodecContext *avctx) av_log(avctx, AV_LOG_ERROR, "Failed to reset MPI (code = %d)\n", ret); } +#define FFRKMPP_DEC_HWACCEL(NAME, ID) \ + AVHWAccel ff_##NAME##_rkmpp_hwaccel = { \ + .name = #NAME "_rkmpp", \ + .type = AVMEDIA_TYPE_VIDEO,\ + .id = ID, \ + .pix_fmt = AV_PIX_FMT_DRM_PRIME,\ + }; #define RKMPP_DEC_CLASS(NAME) \ static const AVClass rkmpp_##NAME##_dec_class = { \ @@ -580,6 +587,7 @@ static void rkmpp_flush(AVCodecContext *avctx) #define RKMPP_DEC(NAME, ID, BSFS) \ RKMPP_DEC_CLASS(NAME) \ + FFRKMPP_DEC_HWACCEL(NAME, ID) \ AVCodec ff_##NAME##_rkmpp_decoder = { \ .name = #NAME "_rkmpp", \ .long_name = NULL_IF_CONFIG_SMALL(#NAME " (rkmpp)"), \ From 47235e7e67dfacb5736adfe427d378dad65f431f Mon Sep 17 00:00:00 2001 From: zillevdr Date: Tue, 3 Oct 2017 11:44:41 +0200 Subject: [PATCH 5/9] with this the frames are in a wrong pts order --- libavcodec/rkmppdec.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libavcodec/rkmppdec.c b/libavcodec/rkmppdec.c index 818cae7f91667..7dbd1c540a240 100644 --- a/libavcodec/rkmppdec.c +++ b/libavcodec/rkmppdec.c @@ -200,13 +200,13 @@ static int rkmpp_init_decoder(AVCodecContext *avctx) goto fail; } - paramS32 = 1; +/* paramS32 = 1; ret = decoder->mpi->control(decoder->ctx, MPP_DEC_SET_PARSER_SPLIT_MODE, ¶mS32); if (ret != MPP_OK) { av_log(avctx, AV_LOG_ERROR, "Failed to set parser split mode on MPI (code = %d).\n", ret); ret = AVERROR_UNKNOWN; goto fail; - } + }*/ // initialize mpp ret = mpp_init(decoder->ctx, MPP_CTX_DEC, codectype); From 8d6ad12e9ec68ccc0805b25816915c09042012ac Mon Sep 17 00:00:00 2001 From: zillevdr Date: Tue, 3 Oct 2017 14:04:42 +0200 Subject: [PATCH 6/9] remove unnecessary code --- libavcodec/rkmppdec.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/libavcodec/rkmppdec.c b/libavcodec/rkmppdec.c index 7dbd1c540a240..091b1b996e1f9 100644 --- a/libavcodec/rkmppdec.c +++ b/libavcodec/rkmppdec.c @@ -546,10 +546,6 @@ static int rkmpp_receive_frame(AVCodecContext *avctx, AVFrame *frame) return ret; } } - - // make sure we keep decoder full - if (freeslots > 1 && decoder->first_frame) - return AVERROR(EAGAIN); } return rkmpp_retrieve_frame(avctx, frame); From 12354fb43da1bc91cebb854faf3935c756340724 Mon Sep 17 00:00:00 2001 From: zillevdr Date: Tue, 3 Oct 2017 15:01:37 +0200 Subject: [PATCH 7/9] if there no frame send the next packet and look at the next cycle --- libavcodec/rkmppdec.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/libavcodec/rkmppdec.c b/libavcodec/rkmppdec.c index 091b1b996e1f9..4cdc9f90ba6b8 100644 --- a/libavcodec/rkmppdec.c +++ b/libavcodec/rkmppdec.c @@ -347,16 +347,9 @@ static int rkmpp_retrieve_frame(AVCodecContext *avctx, AVFrame *frame) // 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 (ret != MPP_OK) { + av_log(avctx, AV_LOG_ERROR, "can't get a frame frome decoder (code = %d)\n", ret); + goto fail; } if (mppframe) { From a1ba2e49585ea2487da4213538255e7133bfe1ee Mon Sep 17 00:00:00 2001 From: zillevdr Date: Tue, 3 Oct 2017 15:24:21 +0200 Subject: [PATCH 8/9] wait for frame EOS --- libavcodec/rkmppdec.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/libavcodec/rkmppdec.c b/libavcodec/rkmppdec.c index 4cdc9f90ba6b8..58c4cf532dfa7 100644 --- a/libavcodec/rkmppdec.c +++ b/libavcodec/rkmppdec.c @@ -337,7 +337,6 @@ static int rkmpp_retrieve_frame(AVCodecContext *avctx, AVFrame *frame) MppBuffer buffer = NULL; AVDRMFrameDescriptor *desc = NULL; AVDRMLayerDescriptor *layer = NULL; - int retrycount = 0; int format, mode; // on start of decoding, MPP can return -1, which is supposed to be expected @@ -352,6 +351,18 @@ static int rkmpp_retrieve_frame(AVCodecContext *avctx, AVFrame *frame) goto fail; } + if (decoder->eos_reached && !mpp_frame_get_eos(mppframe)) { + if (mppframe) + mpp_frame_deinit(&mppframe); + usleep(10000); + goto retry_get_frame; + } + if (mppframe && mpp_frame_get_eos(mppframe)) { + av_log(avctx, AV_LOG_DEBUG, "EOS frame found\n"); + ret = AVERROR_EXIT; + goto fail; + } + if (mppframe) { // Check wether we have a special frame or not if (mpp_frame_get_info_change(mppframe)) { From 30c267e0d9a7ad1abf359481977fef1c1b5aea44 Mon Sep 17 00:00:00 2001 From: zillevdr Date: Wed, 4 Oct 2017 19:38:49 +0200 Subject: [PATCH 9/9] change a loglevel --- libavcodec/rkmppdec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libavcodec/rkmppdec.c b/libavcodec/rkmppdec.c index 58c4cf532dfa7..4df0339ca8710 100644 --- a/libavcodec/rkmppdec.c +++ b/libavcodec/rkmppdec.c @@ -347,7 +347,7 @@ static int rkmpp_retrieve_frame(AVCodecContext *avctx, AVFrame *frame) retry_get_frame: ret = decoder->mpi->decode_get_frame(decoder->ctx, &mppframe); if (ret != MPP_OK) { - av_log(avctx, AV_LOG_ERROR, "can't get a frame frome decoder (code = %d)\n", ret); + av_log(avctx, AV_LOG_DEBUG, "can't get a frame frome decoder (code = %d)\n", ret); goto fail; }