diff --git a/ffmpeg_nvmpi.patch b/ffmpeg_nvmpi.patch index 0681436..8ea9242 100644 --- a/ffmpeg_nvmpi.patch +++ b/ffmpeg_nvmpi.patch @@ -1,7 +1,8 @@ -diff -Naur ffmpeg/configure ffmpeg2/configure ---- ffmpeg/configure 2020-07-09 16:17:46.000000000 +0700 -+++ ffmpeg2/configure 2021-02-28 09:38:00.158190548 +0700 -@@ -340,6 +340,7 @@ +diff --git a/configure b/configure +index 6a7a85c..5810ab5 100755 +--- a/configure ++++ b/configure +@@ -340,6 +340,7 @@ External library support: --disable-vaapi disable Video Acceleration API (mainly Unix/Intel) code [autodetect] --disable-vdpau disable Nvidia Video Decode and Presentation API for Unix code [autodetect] --disable-videotoolbox disable VideoToolbox code [autodetect] @@ -9,7 +10,7 @@ diff -Naur ffmpeg/configure ffmpeg2/configure Toolchain options: --arch=ARCH select architecture [$arch] -@@ -1851,6 +1852,7 @@ +@@ -1851,6 +1852,7 @@ HWACCEL_LIBRARY_LIST=" mmal omx opencl @@ -17,7 +18,7 @@ diff -Naur ffmpeg/configure ffmpeg2/configure " DOCUMENT_LIST=" -@@ -3014,11 +3016,14 @@ +@@ -3014,11 +3016,14 @@ h264_mediacodec_decoder_deps="mediacodec" h264_mediacodec_decoder_select="h264_mp4toannexb_bsf h264_parser" h264_mmal_decoder_deps="mmal" h264_nvenc_encoder_deps="nvenc" @@ -32,7 +33,7 @@ diff -Naur ffmpeg/configure ffmpeg2/configure h264_vaapi_encoder_select="cbs_h264 vaapi_encode" h264_v4l2m2m_decoder_deps="v4l2_m2m h264_v4l2_m2m" h264_v4l2m2m_decoder_select="h264_mp4toannexb_bsf" -@@ -3029,10 +3034,13 @@ +@@ -3029,10 +3034,13 @@ hevc_cuvid_decoder_select="hevc_mp4toannexb_bsf" hevc_mediacodec_decoder_deps="mediacodec" hevc_mediacodec_decoder_select="hevc_mp4toannexb_bsf hevc_parser" hevc_nvenc_encoder_deps="nvenc" @@ -46,7 +47,7 @@ diff -Naur ffmpeg/configure ffmpeg2/configure hevc_vaapi_encoder_deps="VAEncPictureParameterBufferHEVC" hevc_vaapi_encoder_select="cbs_h265 vaapi_encode" hevc_v4l2m2m_decoder_deps="v4l2_m2m hevc_v4l2_m2m" -@@ -3047,6 +3055,7 @@ +@@ -3047,6 +3055,7 @@ mpeg1_cuvid_decoder_deps="cuvid" mpeg1_v4l2m2m_decoder_deps="v4l2_m2m mpeg1_v4l2_m2m" mpeg2_crystalhd_decoder_select="crystalhd" mpeg2_cuvid_decoder_deps="cuvid" @@ -54,7 +55,7 @@ diff -Naur ffmpeg/configure ffmpeg2/configure mpeg2_mmal_decoder_deps="mmal" mpeg2_mediacodec_decoder_deps="mediacodec" mpeg2_qsv_decoder_select="qsvdec mpegvideo_parser" -@@ -3055,6 +3064,7 @@ +@@ -3055,6 +3064,7 @@ mpeg2_vaapi_encoder_select="cbs_mpeg2 vaapi_encode" mpeg2_v4l2m2m_decoder_deps="v4l2_m2m mpeg2_v4l2_m2m" mpeg4_crystalhd_decoder_select="crystalhd" mpeg4_cuvid_decoder_deps="cuvid" @@ -62,7 +63,7 @@ diff -Naur ffmpeg/configure ffmpeg2/configure mpeg4_mediacodec_decoder_deps="mediacodec" mpeg4_mmal_decoder_deps="mmal" mpeg4_omx_encoder_deps="omx" -@@ -3069,6 +3079,7 @@ +@@ -3069,6 +3079,7 @@ vc1_mmal_decoder_deps="mmal" vc1_qsv_decoder_select="qsvdec vc1_parser" vc1_v4l2m2m_decoder_deps="v4l2_m2m vc1_v4l2_m2m" vp8_cuvid_decoder_deps="cuvid" @@ -70,7 +71,7 @@ diff -Naur ffmpeg/configure ffmpeg2/configure vp8_mediacodec_decoder_deps="mediacodec" vp8_qsv_decoder_select="qsvdec vp8_parser" vp8_rkmpp_decoder_deps="rkmpp" -@@ -3077,6 +3088,7 @@ +@@ -3077,6 +3088,7 @@ 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="cuvid" @@ -78,7 +79,7 @@ diff -Naur ffmpeg/configure ffmpeg2/configure vp9_mediacodec_decoder_deps="mediacodec" vp9_rkmpp_decoder_deps="rkmpp" vp9_vaapi_encoder_deps="VAEncPictureParameterBufferVP9" -@@ -6366,6 +6378,7 @@ +@@ -6366,6 +6378,7 @@ enabled rkmpp && { require_pkg_config rkmpp rockchip_mpp rockchip/r die "ERROR: rkmpp requires --enable-libdrm"; } } enabled vapoursynth && require_pkg_config vapoursynth "vapoursynth-script >= 42" VSScript.h vsscript_init @@ -86,52 +87,11 @@ diff -Naur ffmpeg/configure ffmpeg2/configure if enabled gcrypt; then -diff -Naur ffmpeg/libavcodec/allcodecs.c ffmpeg2/libavcodec/allcodecs.c ---- ffmpeg/libavcodec/allcodecs.c 2020-07-09 16:17:46.000000000 +0700 -+++ ffmpeg2/libavcodec/allcodecs.c 2021-02-28 09:38:00.158190548 +0700 -@@ -143,11 +143,15 @@ - extern AVCodec ff_h264_mmal_decoder; - extern AVCodec ff_h264_qsv_decoder; - extern AVCodec ff_h264_rkmpp_decoder; -+extern AVCodec ff_h264_nvmpi_decoder; -+extern AVCodec ff_h264_nvmpi_encoder; - extern AVCodec ff_hap_encoder; - extern AVCodec ff_hap_decoder; - extern AVCodec ff_hevc_decoder; - extern AVCodec ff_hevc_qsv_decoder; - extern AVCodec ff_hevc_rkmpp_decoder; -+extern AVCodec ff_hevc_nvmpi_decoder; -+extern AVCodec ff_hevc_nvmpi_encoder; - extern AVCodec ff_hevc_v4l2m2m_decoder; - extern AVCodec ff_hnm4_video_decoder; - extern AVCodec ff_hq_hqa_decoder; -@@ -766,18 +770,22 @@ - extern AVCodec ff_mjpeg_vaapi_encoder; - extern AVCodec ff_mpeg1_cuvid_decoder; - extern AVCodec ff_mpeg2_cuvid_decoder; -+extern AVCodec ff_mpeg2_nvmpi_decoder; - extern AVCodec ff_mpeg2_qsv_encoder; - extern AVCodec ff_mpeg2_vaapi_encoder; - extern AVCodec ff_mpeg4_cuvid_decoder; -+extern AVCodec ff_mpeg4_nvmpi_decoder; - extern AVCodec ff_mpeg4_mediacodec_decoder; - extern AVCodec ff_mpeg4_v4l2m2m_encoder; - extern AVCodec ff_vc1_cuvid_decoder; - extern AVCodec ff_vp8_cuvid_decoder; -+extern AVCodec ff_vp8_nvmpi_decoder; - extern AVCodec ff_vp8_mediacodec_decoder; - extern AVCodec ff_vp8_qsv_decoder; - extern AVCodec ff_vp8_v4l2m2m_encoder; - extern AVCodec ff_vp8_vaapi_encoder; - extern AVCodec ff_vp9_cuvid_decoder; -+extern AVCodec ff_vp9_nvmpi_decoder; - extern AVCodec ff_vp9_mediacodec_decoder; - extern AVCodec ff_vp9_vaapi_encoder; - -diff -Naur ffmpeg/libavcodec/Makefile ffmpeg2/libavcodec/Makefile ---- ffmpeg/libavcodec/Makefile 2020-07-09 16:17:46.000000000 +0700 -+++ ffmpeg2/libavcodec/Makefile 2021-02-28 09:38:00.158190548 +0700 -@@ -354,6 +354,8 @@ +diff --git a/libavcodec/Makefile b/libavcodec/Makefile +index 3cd73fb..c3ed5cc 100644 +--- a/libavcodec/Makefile ++++ b/libavcodec/Makefile +@@ -354,6 +354,8 @@ OBJS-$(CONFIG_H264_MMAL_DECODER) += mmaldec.o OBJS-$(CONFIG_H264_NVENC_ENCODER) += nvenc_h264.o OBJS-$(CONFIG_NVENC_ENCODER) += nvenc_h264.o OBJS-$(CONFIG_NVENC_H264_ENCODER) += nvenc_h264.o @@ -140,7 +100,7 @@ diff -Naur ffmpeg/libavcodec/Makefile ffmpeg2/libavcodec/Makefile OBJS-$(CONFIG_H264_OMX_ENCODER) += omx.o OBJS-$(CONFIG_H264_QSV_DECODER) += qsvdec_h2645.o OBJS-$(CONFIG_H264_QSV_ENCODER) += qsvenc_h264.o -@@ -379,6 +381,8 @@ +@@ -379,6 +381,8 @@ OBJS-$(CONFIG_HEVC_QSV_ENCODER) += qsvenc_hevc.o hevc_ps_enc.o \ OBJS-$(CONFIG_HEVC_RKMPP_DECODER) += rkmppdec.o OBJS-$(CONFIG_HEVC_VAAPI_ENCODER) += vaapi_encode_h265.o h265_profile_level.o OBJS-$(CONFIG_HEVC_V4L2M2M_DECODER) += v4l2_m2m_dec.o @@ -149,7 +109,7 @@ diff -Naur ffmpeg/libavcodec/Makefile ffmpeg2/libavcodec/Makefile OBJS-$(CONFIG_HEVC_V4L2M2M_ENCODER) += v4l2_m2m_enc.o OBJS-$(CONFIG_HNM4_VIDEO_DECODER) += hnm4video.o OBJS-$(CONFIG_HQ_HQA_DECODER) += hq_hqa.o hq_hqadata.o hq_hqadsp.o \ -@@ -464,11 +468,13 @@ +@@ -464,11 +468,13 @@ OBJS-$(CONFIG_MPEG2_QSV_ENCODER) += qsvenc_mpeg2.o OBJS-$(CONFIG_MPEG2VIDEO_DECODER) += mpeg12dec.o mpeg12.o mpeg12data.o OBJS-$(CONFIG_MPEG2VIDEO_ENCODER) += mpeg12enc.o mpeg12.o OBJS-$(CONFIG_MPEG2_CUVID_DECODER) += cuviddec.o @@ -163,7 +123,7 @@ diff -Naur ffmpeg/libavcodec/Makefile ffmpeg2/libavcodec/Makefile OBJS-$(CONFIG_MPEG4_MEDIACODEC_DECODER) += mediacodecdec.o OBJS-$(CONFIG_MPEG4_OMX_ENCODER) += omx.o OBJS-$(CONFIG_MPEG4_V4L2M2M_DECODER) += v4l2_m2m_dec.o -@@ -669,6 +675,7 @@ +@@ -669,6 +675,7 @@ OBJS-$(CONFIG_VP6_DECODER) += vp6.o vp56.o vp56data.o \ OBJS-$(CONFIG_VP7_DECODER) += vp8.o vp56rac.o OBJS-$(CONFIG_VP8_DECODER) += vp8.o vp56rac.o OBJS-$(CONFIG_VP8_CUVID_DECODER) += cuviddec.o @@ -171,7 +131,7 @@ diff -Naur ffmpeg/libavcodec/Makefile ffmpeg2/libavcodec/Makefile OBJS-$(CONFIG_VP8_MEDIACODEC_DECODER) += mediacodecdec.o OBJS-$(CONFIG_VP8_QSV_DECODER) += qsvdec_other.o OBJS-$(CONFIG_VP8_RKMPP_DECODER) += rkmppdec.o -@@ -679,6 +686,7 @@ +@@ -679,6 +686,7 @@ OBJS-$(CONFIG_VP9_DECODER) += vp9.o vp9data.o vp9dsp.o vp9lpf.o vp9r vp9block.o vp9prob.o vp9mvs.o vp56rac.o \ vp9dsp_8bpp.o vp9dsp_10bpp.o vp9dsp_12bpp.o OBJS-$(CONFIG_VP9_CUVID_DECODER) += cuviddec.o @@ -179,9 +139,54 @@ diff -Naur ffmpeg/libavcodec/Makefile ffmpeg2/libavcodec/Makefile OBJS-$(CONFIG_VP9_MEDIACODEC_DECODER) += mediacodecdec.o OBJS-$(CONFIG_VP9_RKMPP_DECODER) += rkmppdec.o OBJS-$(CONFIG_VP9_VAAPI_ENCODER) += vaapi_encode_vp9.o -diff -Naur ffmpeg/libavcodec/nvmpi_dec.c ffmpeg2/libavcodec/nvmpi_dec.c ---- ffmpeg/libavcodec/nvmpi_dec.c 1970-01-01 08:00:00.000000000 +0800 -+++ ffmpeg2/libavcodec/nvmpi_dec.c 2021-02-28 10:48:56.062551585 +0700 +diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c +index d2f9a39..04dc62b 100644 +--- a/libavcodec/allcodecs.c ++++ b/libavcodec/allcodecs.c +@@ -143,11 +143,15 @@ extern AVCodec ff_h264_mediacodec_decoder; + extern AVCodec ff_h264_mmal_decoder; + extern AVCodec ff_h264_qsv_decoder; + extern AVCodec ff_h264_rkmpp_decoder; ++extern AVCodec ff_h264_nvmpi_decoder; ++extern AVCodec ff_h264_nvmpi_encoder; + extern AVCodec ff_hap_encoder; + extern AVCodec ff_hap_decoder; + extern AVCodec ff_hevc_decoder; + extern AVCodec ff_hevc_qsv_decoder; + extern AVCodec ff_hevc_rkmpp_decoder; ++extern AVCodec ff_hevc_nvmpi_decoder; ++extern AVCodec ff_hevc_nvmpi_encoder; + extern AVCodec ff_hevc_v4l2m2m_decoder; + extern AVCodec ff_hnm4_video_decoder; + extern AVCodec ff_hq_hqa_decoder; +@@ -766,18 +770,22 @@ extern AVCodec ff_mjpeg_qsv_encoder; + extern AVCodec ff_mjpeg_vaapi_encoder; + extern AVCodec ff_mpeg1_cuvid_decoder; + extern AVCodec ff_mpeg2_cuvid_decoder; ++extern AVCodec ff_mpeg2_nvmpi_decoder; + extern AVCodec ff_mpeg2_qsv_encoder; + extern AVCodec ff_mpeg2_vaapi_encoder; + extern AVCodec ff_mpeg4_cuvid_decoder; ++extern AVCodec ff_mpeg4_nvmpi_decoder; + extern AVCodec ff_mpeg4_mediacodec_decoder; + extern AVCodec ff_mpeg4_v4l2m2m_encoder; + extern AVCodec ff_vc1_cuvid_decoder; + extern AVCodec ff_vp8_cuvid_decoder; ++extern AVCodec ff_vp8_nvmpi_decoder; + extern AVCodec ff_vp8_mediacodec_decoder; + extern AVCodec ff_vp8_qsv_decoder; + extern AVCodec ff_vp8_v4l2m2m_encoder; + extern AVCodec ff_vp8_vaapi_encoder; + extern AVCodec ff_vp9_cuvid_decoder; ++extern AVCodec ff_vp9_nvmpi_decoder; + extern AVCodec ff_vp9_mediacodec_decoder; + extern AVCodec ff_vp9_vaapi_encoder; + +diff --git a/libavcodec/nvmpi_dec.c b/libavcodec/nvmpi_dec.c +new file mode 100644 +index 0000000..f82aa61 +--- /dev/null ++++ b/libavcodec/nvmpi_dec.c @@ -0,0 +1,171 @@ +#include +#include @@ -238,8 +243,8 @@ diff -Naur ffmpeg/libavcodec/nvmpi_dec.c ffmpeg2/libavcodec/nvmpi_dec.c + + if(avctx->pix_fmt ==AV_PIX_FMT_NONE){ + avctx->pix_fmt=AV_PIX_FMT_YUV420P; -+ }else if(avctx-> pix_fmt != AV_PIX_FMT_YUV420P && avctx-> pix_fmt != AV_PIX_FMT_YUVJ420P){ -+ av_log(avctx, AV_LOG_ERROR, "Invalid Pix_FMT for NVMPI Only yuv420p or yuvj420p is supported\n"); ++ }else if(avctx-> pix_fmt != AV_PIX_FMT_YUV420P){ ++ av_log(avctx, AV_LOG_ERROR, "Invalid Pix_FMT for NVMPI Only yuv420p is supported\n"); + return AVERROR_INVALIDDATA; + } + @@ -354,10 +359,12 @@ diff -Naur ffmpeg/libavcodec/nvmpi_dec.c ffmpeg2/libavcodec/nvmpi_dec.c +NVMPI_DEC(vp9, AV_CODEC_ID_VP9,NULL); +NVMPI_DEC(vp8, AV_CODEC_ID_VP8,NULL); + -diff -Naur ffmpeg/libavcodec/nvmpi_enc.c ffmpeg2/libavcodec/nvmpi_enc.c ---- ffmpeg/libavcodec/nvmpi_enc.c 1970-01-01 08:00:00.000000000 +0800 -+++ ffmpeg2/libavcodec/nvmpi_enc.c 2021-03-01 11:38:35.700002819 +0700 -@@ -0,0 +1,271 @@ +diff --git a/libavcodec/nvmpi_enc.c b/libavcodec/nvmpi_enc.c +new file mode 100644 +index 0000000..0e1390e +--- /dev/null ++++ b/libavcodec/nvmpi_enc.c +@@ -0,0 +1,272 @@ +#include +#include "avcodec.h" +#include "internal.h" @@ -499,13 +506,14 @@ diff -Naur ffmpeg/libavcodec/nvmpi_enc.c ffmpeg2/libavcodec/nvmpi_enc.c + _nvframe.payload[1]=frame->data[1]; + _nvframe.payload[2]=frame->data[2]; + ++ _nvframe.payload_size[0]=frame->linesize[0]*frame->height; ++ _nvframe.payload_size[1]=frame->linesize[1]*frame->height/2; ++ _nvframe.payload_size[2]=frame->linesize[2]*frame->height/2; ++ + _nvframe.linesize[0]=frame->linesize[0]; + _nvframe.linesize[1]=frame->linesize[1]; + _nvframe.linesize[2]=frame->linesize[2]; + -+ _nvframe.width = frame->width; -+ _nvframe.height = frame->height; -+ + _nvframe.timestamp=frame->pts; + + res=nvmpi_encoder_put_frame(nvmpi_context->ctx,&_nvframe); diff --git a/nvmpi_enc.cpp b/nvmpi_enc.cpp index 6d3590f..43f267d 100644 --- a/nvmpi_enc.cpp +++ b/nvmpi_enc.cpp @@ -113,7 +113,6 @@ static bool encoder_capture_plane_dq_callback(struct v4l2_buffer *v4l2_buf, NvBu nvmpictx* nvmpi_create_encoder(nvCodingType codingType,nvEncParam * param){ int ret; - log_level = LOG_LEVEL_INFO; nvmpictx *ctx=new nvmpictx; ctx->index=0; @@ -374,26 +373,6 @@ nvmpictx* nvmpi_create_encoder(nvCodingType codingType,nvEncParam * param){ return ctx; } -static void copy_image_plane(unsigned char* dst, const unsigned char* src, int dstPitch, int srcPitch, int height, uint32_t* byteCopied) -{ - if(!dst || !src){ - return; - } - if(dstPitch == srcPitch){ - *byteCopied = srcPitch * height; - memcpy(dst, src, *byteCopied); - } - else{ - int pitch = min(dstPitch, srcPitch); - *byteCopied = 0; - for(int i = 0; i < height; i++){ - memcpy(dst, src, pitch); - dst += dstPitch; - src += srcPitch; - *byteCopied += pitch; - } - } -} int nvmpi_encoder_put_frame(nvmpictx* ctx,nvFrame* frame){ int ret; @@ -425,9 +404,12 @@ int nvmpi_encoder_put_frame(nvmpictx* ctx,nvFrame* frame){ } - copy_image_plane(nvBuffer->planes[0].data, frame->payload[0], nvBuffer->planes[0].fmt.stride, frame->linesize[0], frame->height, &nvBuffer->planes[0].bytesused); - copy_image_plane(nvBuffer->planes[1].data, frame->payload[1], nvBuffer->planes[1].fmt.stride, frame->linesize[1], frame->height >> 1, &nvBuffer->planes[1].bytesused); - copy_image_plane(nvBuffer->planes[2].data, frame->payload[2], nvBuffer->planes[2].fmt.stride, frame->linesize[2], frame->height >> 1, &nvBuffer->planes[2].bytesused); + memcpy(nvBuffer->planes[0].data,frame->payload[0],frame->payload_size[0]); + memcpy(nvBuffer->planes[1].data,frame->payload[1],frame->payload_size[1]); + memcpy(nvBuffer->planes[2].data,frame->payload[2],frame->payload_size[2]); + nvBuffer->planes[0].bytesused=frame->payload_size[0]; + nvBuffer->planes[1].bytesused=frame->payload_size[1]; + nvBuffer->planes[2].bytesused=frame->payload_size[2]; v4l2_buf.flags |= V4L2_BUF_FLAG_TIMESTAMP_COPY; v4l2_buf.timestamp.tv_usec = frame->timestamp % 1000000;