Skip to content

Instantly share code, notes, and snippets.

@corenel
Last active May 20, 2022
Embed
What would you like to do?
Install NVDEC and NVENC as GStreamer plugins

Install NVDEC and NVENC as GStreamer plugins

Environment

  • Ubuntu 18.04
  • NVIDIA driver 460.32.03
  • NVIDIA Video Codec SDK 11.0.10
  • GStreamer 1.14.5

Steps

  1. Clone the gst-plugins-bad and check out to the same version as GStreamer installed on your PC
git clone git://anongit.freedesktop.org/gstreamer/gst-plugins-bad
cd gst-plugins-bad/
git checkout 1.14.5
  1. Download NVIDIA Video Codec SDK and copy header files
cd /path/to/video/codec/sdk
cp /usr/local/cuda/include/cuda.h /path/to/gst-plugins-bad/sys/nvenc
cp Interface/nvEncodeAPI.h /path/to/gst-plugins-bad/sys/nvenc
cp Interface/cuviddec.h /path/to/gst-plugins-bad/sys/nvdec
cp Interface/nvcuvid.h /path/to/gst-plugins-bad/sys/nvdec
  1. Build plugins
cd /path/to/gst-plugins-bad
NVENCODE_CFLAGS="-I/path/to/gst-plugins-bad/sys/nvenc" ./autogen.sh --with-cuda-prefix="/usr/local/cuda" --disable-gtk-doc
cd sys/nvenc
make
sudo cp .libs/libgstnvenc.so /usr/lib/x86_64-linux-gnu/gstreamer-1.0/
cd ../nvdec
make
sudo cp .libs/libgstnvdec.so /usr/lib/x86_64-linux-gnu/gstreamer-1.0/
  1. Check installation
gst-inspect-1.0 | grep nvenc
gst-inspect-1.0 | grep nvdec

References

@sujit25
Copy link

sujit25 commented Mar 31, 2020

Typo error:

  1. cp include/nvcuvid.h /path/to/gst-plugins-bad/sys/nvdec
  2. gst-inspect-1.0 | grep nvenc
  3. gst-inspect-1.0 | grep nvdec

However after installing, unable to see nvenc and nvdec.
image

@corenel
Copy link
Author

corenel commented Mar 31, 2020

Typo error:

  1. cp include/nvcuvid.h /path/to/gst-plugins-bad/sys/nvdec
  2. gst-inspect-1.0 | grep nvenc
  3. gst-inspect-1.0 | grep nvdec

However after installing, unable to see nvenc and nvdec.

image

Thanks for your correction.
These plugins in my PC is fine as follows:

$ gst-inspect-1.0 | grep nvenc
nvenc:  nvh264enc: NVENC H.264 Video Encoder
$ gst-inspect-1.0 | grep nvdec
nvdec:  nvdec: NVDEC video decoder

Could you check the installation location prefix of your GStreamer by where gst-inspect-1.0? Mine is /usr instead of /usr/local, so the plugin directory is /usr/lib/x86_64-linux-gnu/gstreamer-1.0/. Maybe your installation has another location.

Or you can check if these two plugins are blacklisted by gst-inspect-1.0 -b.

@muimota
Copy link

muimota commented Aug 23, 2020

It seems you are using anaconda which has it's own gstreamer included. check which one are you using with which gst-inspect-1.0

@sujit-dn
Copy link

sujit-dn commented Aug 24, 2020

Yeah, I agree. I deactivated conda base environment and it worked. Thanks a lot for assistance

@zshivers
Copy link

zshivers commented Sep 23, 2020

These instructions break with SDK 10, since they renamed include/ to Interface/.
I also found it helpful to add --disable-gtk-doc after ./autogen.sh to disable that dependency.

@cwlinghk
Copy link

cwlinghk commented Jan 12, 2021

how about h265? thanks

@HoangTienDuc
Copy link

HoangTienDuc commented Mar 30, 2021

how did you start streaming with python?

import cv2


url = "rtsp://admin:abcd1234@192.168.1.104:554/Streaming/Channels/101?transportmode=mcast&profile=Profile_1"
pipeline = 'rtspsrc location=%s latency=0 ! decodebin ! ' % url
# cvt_pipeline = (
#                 'nvvidconv !'
#                 'videoscale ! '
#                 'video/x-raw, width=(int)%d, height=(int)%d !'
#                 'videoconvert ! appsink sync=false'
#                 % (1920, 1080)
#                 )

cvt_pipeline = (
                # 'nvh264enc !'
                # 'nvh264dec !'
                'videoscale ! '
                'video/x-raw, width=(int)%d, height=(int)%d !'
                'videoconvert ! appsink sync=false'
                % (1920, 1080)
                )
cap = cv2.VideoCapture(pipeline + cvt_pipeline, cv2.CAP_GSTREAMER)

while True:
      ret,frame = cap.read()
      if not ret:
        print('empty frame')
        continue

i tried another way to add nvcodec but it failed

@HoangTienDuc
Copy link

HoangTienDuc commented Mar 30, 2021

/usr/bin/ld: cannot find -lnvidia-encode

i got error when make in sys/nvenc

cannot find -lnvcuvid

and onther error in sys/nvdec

@rlienlaf
Copy link

rlienlaf commented Apr 9, 2021

/usr/bin/ld: cannot find -lnvidia-encode

i got error when make in sys/nvenc

cannot find -lnvcuvid

and onther error in sys/nvdec

If you downloaded SDK 10 from nvidia-SDK you can find both .so inside Lib/linux/stubs/x86_64/ (for ubuntu18).
The correct way is probably to move both files to its corresponding locations. But what I did was to add "-L/path/to/nvidia-sdk/Lib/linux/stubs/x86_64/" to the variable "NVENCODE_LIBS" for sys/nvenc/Makefile and to the variable "libgstnvdec_la_LIBADD" for sys/nvdec/Makefile.

@tadam98
Copy link

tadam98 commented Aug 8, 2021

The below procesure fails on wsl2 over windows 11.

  1. There is probably a missing "make" after the ./autogen.sh. But running make fails with an error.
  2. gst-xxx are in /usr/lib/ but anyway we dont get there is make fails.

https://gist.github.com/corenel/a615b6f7eb5b5425aa49343a7b409200

$ cd Downloads
$ mkdir gstreamer
$ cd gstreamer

step 1

git clone git://anongit.freedesktop.org/gstreamer/gst-plugins-bad
cd gst-plugins-bad/
git checkout 1.14.5

step 2

https://developer.nvidia.com/nvidia-video-codec-sdk/download

/home/mickey/Downloads/gstreamer/Video_Codec_SDK_11.1.5

cd /home/mickey/Downloads/gstreamer/Video_Codec_SDK_11.1.5
cp /usr/local/cuda/include/cuda.h /home/mickey/Downloads/gstreamer/gst-plugins-bad/sys/nvenc/
cp Interface/nvEncodeAPI.h /home/mickey/Downloads/gstreamer/gst-plugins-bad/sys/nvenc/
cp Interface/cuviddec.h /home/mickey/Downloads/gstreamer/gst-plugins-bad/sys/nvenc/
cp Interface/nvcuvid.h /home/mickey/Downloads/gstreamer/gst-plugins-bad/sys/nvenc/

step 3

cd /home/mickey/Downloads/gstreamer/gst-plugins-bad/
NVENCODE_CFLAGS="-I/home/mickey/Downloads/gstreamer/gst-plugins-bad/sys/nvenc/" ./autogen.sh --with-cuda-prefix="/usr/local/cuda" --disable-gtk-doc
make # fails
error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.

cd /home/mickey/Downloads/gstreamer/gst-plugins-bad/sys/nvenc
make
#sudo cp .libs/libgstnvenc.so /usr/lib/x86_64-linux-gnu/gstreamer-1.0/
sudo cp .libs/libgstnvenc.so /usr/lib
cd ../nvdec
make
#sudo cp .libs/libgstnvdec.so /usr/lib/x86_64-linux-gnu/gstreamer-1.0/
sudo cp .libs/libgstnvdec.so /usr/lib/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment