NVIDIA / VideoProcessingFramework

Set of Python bindings to C++ libraries which provides full HW acceleration for video decoding, encoding and GPU-accelerated color space and pixel format conversions
Apache License 2.0
1.32k stars 233 forks source link

Compiling with C++ Program #504

Closed avickars closed 1 year ago

avickars commented 1 year ago

Describe the bug A clear and concise description of what the bug is.

I am trying to use VPF in c++ and having trouble linking everything together properly. To be succinct, my sample program is super basic I am just trying to get it to compile:

#include <iostream>
#include <NvDecoder.h>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

Since I don't need the python bindings, I am only compiling the source code in the /home/VideoProcessingFramework/src/TC directory via:

cd VideoProcessingFramework/src/TC
mkdir build
cmake ..
make

And to compile my program Im using the following makefile:

SHELL := /bin/bash
CC=g++ -std=c++17 -Wall -g
NV=nvcc

PKGS:= libavcodec libavfilter libavformat libavutil libswresample

CFLAGS:= -I/home/VideoProcessingFramework/src/TC/inc/
CFLAGS+= -I/home/VideoProcessingFramework/src/TC/TC_CORE/inc/
CFLAGS+= -I/home/VideoProcessingFramework/src/TC/build/TC_CORE/
CFLAGS+= -I/home/VideoProcessingFramework/src/TC/third_party/
CFLAGS+= -I/usr/include/linux/
CFLAGS+= -I/home/VideoProcessingFramework/src/TC/build/
CFLAGS+= $(shell pkg-config --cflags $(PKGS))

LIBS:= $(shell pkg-config --libs $(PKGS))
LIBS+= -lTC_CORE -lTC

LINK:= -L/home/VideoProcessingFramework/src/TC/build/TC_CORE/
LINK+= -L/home/VideoProcessingFramework/src/TC/build/

all:
    $(CC) test.cpp -o test $(CFLAGS) $(LIBS) $(LINK)

This gives a whole bunch of errors so I am clear missing something. Any help on what actually needs to be included/linked together would be greatly appreciated!

As an FYI, I am using the nvcr.io/nvidia/tritonserver:22.12-py3 as a base docker image. My docker file is in a comment below. Also, when I execute pip install . everything works no problem, it really just appears to be a linking issue where Im not getting everything that needs to be linked.

To Reproduce Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior It should compile.

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

Additional context Add any other context about the problem here.

avickars commented 1 year ago

Dockerfile

FROM nvcr.io/nvidia/tritonserver:22.12-py3

RUN apt-get update

# ****************************************** pkg-config ******************************************
RUN apt-get install -y pkg-config \
    ffmpeg
RUN pip install cmake --upgrade

# ****************************************** VPF ******************************************
RUN apt install -y \
              libavfilter-dev \
              libavformat-dev \
              libavcodec-dev \
              libswresample-dev \
              libavutil-dev\
              wget \
              build-essential \
              git

WORKDIR /home
RUN git clone https://github.com/NVIDIA/VideoProcessingFramework.git
WORKDIR ./VideoProcessingFramework/src/TC
RomanArzumanyan commented 1 year ago

Hi @avickars

You can simply link your application against TC_CORE and / or TC library. You can follow PyNvCodec sources as sample illustrating how to use C++ interface. Both TC_CORE and TC have an APIs suitable for dynamic libraries.

Basically PyNvCodec is just another library linked against TC but instead of .dll or .so binary Python module is generated thanks to pybind11 CMake magic.

You can see the corresp. target declared here: https://github.com/NVIDIA/VideoProcessingFramework/blob/3347e555ed795ba7de98b4e6b9bf7fe441784663/src/PyNvCodec/CMakeLists.txt#L46-L60

VPF doesn't have "pure" C++ interface intentionally because of pybind11 shim layer which translates Python lists, dictionaries etc. into STL-like containers available from C++ land within PyNvCodec module.

Otherwise there would be another abstraction layer which sole purpose is to copy data between std:: containers and pybind11:: containers.

avickars commented 1 year ago

Hi @RomanArzumanyan ,

Thank you for your response!

I totally understand that PyNvCodec is linked against TC etc. My confusion is on actually linking with the TC and TC_Core libraries. I can see that after compiling the TC and TC_Core libraries, the static libraries /home/VideoProcessingFramework/src/TC/build/libTC.a and /home/VideoProcessingFramework/src/TC/build/TC_CORE/libTC_CORE.a are generated and I have adjusted my makefile to include them. However, when I compile my test program that links to these libraries I am still getting a multitude of errors and I have been unable to determine what I am missing.

In short, is there any sample c++ program that just shows the compilation when linked to the TC and TC_CORE libraries (the only thing I've been able to find is about 3 years old)? To be clear Im not looking for any program that shows how the libraries work in c++ as that can be easily seen by just looking at the python bindings. Im simply just stuck getting my program to compile as from what understand there isn't any reference.

Thanks so much! Any help is appreciated!

RomanArzumanyan commented 1 year ago

@avickars

Ah, I see. There were changes to TC/CMakeLists.txt: https://github.com/NVIDIA/VideoProcessingFramework/blob/3347e555ed795ba7de98b4e6b9bf7fe441784663/src/TC/CMakeLists.txt#L40-L53

It's no longer shared library target. Probably that was done to simplify the pip install process.

Unless it's properly fixed I assume you can do a quick dirty fix (if you don't need anything Python) by adding SHARED keyword to TC and TC_CORE CMakeLists.txt files.

P. S. You can follow older TC snd TC_CORE CMakeLists.txt files.

avickars commented 1 year ago

Hey @RomanArzumanyan ,

I tried that and unfortunately it didn't work. To be clear I added SHARED to: https://github.com/NVIDIA/VideoProcessingFramework/blob/3347e555ed795ba7de98b4e6b9bf7fe441784663/src/TC/TC_CORE/CMakeLists.txt#L27 and https://github.com/NVIDIA/VideoProcessingFramework/blob/3347e555ed795ba7de98b4e6b9bf7fe441784663/src/TC/CMakeLists.txt#L40

and Im still not able to compile and am getting a multitude of errors (I copied this commit as you recommended: https://github.com/NVIDIA/VideoProcessingFramework/blob/50ac0f241bc7299bf6a82ad67aeba286152eb6b7/PyNvCodec/TC/TC_CORE/CMakeLists.txt). For debugging purposes I uploaded the output from the compile attempt here: https://drive.google.com/file/d/1SZu-X4oTVDTx3R0EsdGKgpGM4eiaELb7/view?usp=sharing

avickars commented 1 year ago

update: Got it to work (stupid mistake in my makefile). Thanks for your help @RomanArzumanyan !