diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 00000000..c584905e --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1,5 @@ +blank_issues_enabled: false +contact_links: + - name: 時雨堂コミュニティ Discord + url: https://discord.gg/shiguredo + about: Issue を作成するには Discord での相談が必要です diff --git a/.github/actions/download/action.yml b/.github/actions/download/action.yml new file mode 100644 index 00000000..13a8623a --- /dev/null +++ b/.github/actions/download/action.yml @@ -0,0 +1,28 @@ +name: download-artifact + +description: Download Artifact + +inputs: + platform: + description: Platform + required: true + +runs: + using: composite + steps: + - uses: actions/download-artifact@v3 + with: + name: ${{ inputs.platform }}.env + path: ${{ inputs.platform }}.env + - name: Env to output + shell: bash + run: | + sed -i 's/\r//g' ${{ inputs.platform }}.env/${{ inputs.platform }}.env + source ${{ inputs.platform }}.env/${{ inputs.platform }}.env + echo "package_name=$PACKAGE_NAME" >> $GITHUB_OUTPUT + echo "$PACKAGE_NAME/$PACKAGE_NAME" >> package_paths.env + id: env + - uses: actions/download-artifact@v3 + with: + name: ${{ steps.env.outputs.package_name }} + path: ${{ steps.env.outputs.package_name }} diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 9e00d9cc..68b8e863 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -20,8 +20,14 @@ jobs: name: Build momo for Windows_${{ matrix.arch }} runs-on: windows-2019 steps: - - uses: actions/checkout@v3 - - uses: microsoft/setup-msbuild@v1.1 + - uses: actions/checkout@v4 + - uses: microsoft/setup-msbuild@v1.3 + # - uses: GuillaumeFalourd/setup-windows10-sdk-action@v1.11 + # with: + # sdk-version: 20348 + - name: test + run: | + ls "C:\Program Files (x86)\Windows Kits\10\Include\" - name: Get Versions run: | Get-Content "VERSION" | Foreach-Object { @@ -83,7 +89,7 @@ jobs: name: Build momo for macOS_${{ matrix.arch }} runs-on: macos-11.0 steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Get Versions run: | source VERSION @@ -125,13 +131,13 @@ jobs: - raspberry-pi-os_armv6 - raspberry-pi-os_armv7 - raspberry-pi-os_armv8 - - ubuntu-18.04_armv8_jetson_nano - - ubuntu-18.04_armv8_jetson_xavier - ubuntu-20.04_x86_64 + - ubuntu-20.04_armv8_jetson_xavier + - ubuntu-22.04_x86_64 name: Build momo for ${{ matrix.name }} - runs-on: ubuntu-18.04 + runs-on: ubuntu-20.04 steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Get Versions run: | source VERSION @@ -173,74 +179,42 @@ jobs: - build-linux runs-on: ubuntu-latest steps: - - name: Create Release - id: create_release - uses: actions/create-release@v1.1.4 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - uses: actions/checkout@v4 + - uses: ./.github/actions/download with: - tag_name: ${{ github.ref }} - release_name: Release ${{ github.ref }} - draft: false - prerelease: false - - name: Output upload url - shell: bash - run: echo "UPLOAD_URL='${{ steps.create_release.outputs.upload_url }}'" > create-release.env - - name: Upload create-release Environment - uses: actions/upload-artifact@v3 + platform: windows_x86_64 + - uses: ./.github/actions/download with: - name: create-release.env - path: create-release.env - upload-assets: - name: Upload Assets to Release - needs: - - create-release - runs-on: ubuntu-latest - strategy: - fail-fast: true - matrix: - name: - - windows_x86_64 - - macos_arm64 - - raspberry-pi-os_armv6 - - raspberry-pi-os_armv7 - - raspberry-pi-os_armv8 - - ubuntu-18.04_armv8_jetson_nano - - ubuntu-18.04_armv8_jetson_xavier - - ubuntu-20.04_x86_64 - steps: - - uses: actions/download-artifact@v3 + platform: macos_arm64 + - uses: ./.github/actions/download with: - name: ${{ matrix.name }}.env - path: ${{ matrix.name }}.env - - uses: actions/download-artifact@v3 + platform: raspberry-pi-os_armv6 + - uses: ./.github/actions/download + with: + platform: raspberry-pi-os_armv7 + - uses: ./.github/actions/download with: - name: create-release.env - path: create-release.env + platform: raspberry-pi-os_armv8 + - uses: ./.github/actions/download + with: + platform: ubuntu-20.04_x86_64 + - uses: ./.github/actions/download + with: + platform: ubuntu-20.04_armv8_jetson_xavier + - uses: ./.github/actions/download + with: + platform: ubuntu-22.04_x86_64 - name: Env to output - shell: bash run: | - sed -i 's/\r//g' ${{ matrix.name }}.env/${{ matrix.name }}.env - sed -i 's/\r//g' create-release.env/create-release.env - source ${{ matrix.name }}.env/${{ matrix.name }}.env - source create-release.env/create-release.env - echo "package_name=$PACKAGE_NAME" >> $GITHUB_OUTPUT - echo "content_type=$CONTENT_TYPE" >> $GITHUB_OUTPUT - echo "upload_url=$UPLOAD_URL" >> $GITHUB_OUTPUT + echo "package_paths<> $GITHUB_OUTPUT + cat package_paths.env >> $GITHUB_OUTPUT + echo "EOF" >> $GITHUB_OUTPUT id: env - - uses: actions/download-artifact@v3 - with: - name: ${{ steps.env.outputs.package_name }} - path: ${{ steps.env.outputs.package_name }} - - name: Upload ${{ matrix.name }} Release Asset - uses: actions/upload-release-asset@v1.0.2 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Release + uses: softprops/action-gh-release@v1 with: - upload_url: ${{ steps.env.outputs.upload_url }} - asset_path: ${{ steps.env.outputs.package_name }}/${{ steps.env.outputs.package_name }} - asset_name: ${{ steps.env.outputs.package_name }} - asset_content_type: ${{ steps.env.outputs.content_type }} + files: + ${{ steps.env.outputs.package_paths }} notification: name: Slack Notification runs-on: ubuntu-latest @@ -249,10 +223,9 @@ jobs: - build-macos - build-linux - create-release - - upload-assets if: always() steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: rtCamp/action-slack-notify@v2 if: | needs.build-windows.result == 'failure' || diff --git a/.github/workflows/daily_build.yml b/.github/workflows/daily_build.yml index 191fc45e..dfb2e81e 100644 --- a/.github/workflows/daily_build.yml +++ b/.github/workflows/daily_build.yml @@ -16,8 +16,8 @@ jobs: name: Build momo for Windows_${{ matrix.arch }} runs-on: windows-2019 steps: - - uses: actions/checkout@v3 - - uses: microsoft/setup-msbuild@v1.1 + - uses: actions/checkout@v4 + - uses: microsoft/setup-msbuild@v1.3 - name: Get Versions run: | Get-Content "VERSION" | Foreach-Object { @@ -52,7 +52,7 @@ jobs: name: Build momo for macOS_${{ matrix.arch }} runs-on: macos-11.0 steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Get Versions run: | source VERSION @@ -75,13 +75,13 @@ jobs: - raspberry-pi-os_armv6 - raspberry-pi-os_armv7 - raspberry-pi-os_armv8 - - ubuntu-18.04_armv8_jetson_nano - - ubuntu-18.04_armv8_jetson_xavier - ubuntu-20.04_x86_64 + - ubuntu-20.04_armv8_jetson_xavier + - ubuntu-22.04_x86_64 name: Build momo for ${{ matrix.name }} - runs-on: ubuntu-18.04 + runs-on: ubuntu-20.04 steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Get Versions run: | source VERSION @@ -105,7 +105,7 @@ jobs: - build-linux if: always() steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: rtCamp/action-slack-notify@v2 if: | needs.build-windows.result == 'failure' || diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index c90e7936..d1c31daf 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -100,6 +100,40 @@ "cStandard": "c11", "cppStandard": "c++14", "intelliSenseMode": "linux-clang-x64" + }, + { + "name": "raspberry-pi-os_armv8 on Ubuntu 20.04 x86_64", + "includePath": [ + "${workspaceFolder}/src", + "${workspaceFolder}/NvCodec/include", + "${workspaceFolder}/NvCodec/NvCodec", + "${workspaceFolder}/_install/raspberry-pi-os_armv8/Release/webrtc/include", + "${workspaceFolder}/_install/raspberry-pi-os_armv8/Release/webrtc/include/third_party/abseil-cpp", + "${workspaceFolder}/_install/raspberry-pi-os_armv8/Release/webrtc/include/third_party/boringssl/src/include", + "${workspaceFolder}/_install/raspberry-pi-os_armv8/Release/webrtc/include/third_party/libyuv/include", + "${workspaceFolder}/_install/raspberry-pi-os_armv8/Release/webrtc/include/third_party/zlib", + "${workspaceFolder}/_install/raspberry-pi-os_armv8/Release/boost/include", + "${workspaceFolder}/_install/raspberry-pi-os_armv8/Release/CLI11/include", + // "${workspaceFolder}/_install/raspberry-pi-os_armv8/Release/SDL2/include/SDL2", + "${workspaceFolder}/_install/raspberry-pi-os_armv8/Release/rootfs/usr/include", + "${workspaceFolder}/_install/raspberry-pi-os_armv8/Release/rootfs/usr/include/libcamera", + "${workspaceFolder}/_build/raspberry-pi-os_armv8" + ], + "defines": [ + "WEBRTC_LINUX", + "USE_NVCODEC_ENCODER=0", + "USE_MMAL_ENCODER=0", + "USE_JETSON_ENCODER=0", + "USE_V4L2_ENCODER=1", + "USE_H264=1", + "USE_SDL2=0", + "USE_LINUX_PULSE_AUDIO=0", + "USE_SCREEN_CAPTURER=0" + ], + "compilerPath": "${workspaceFolder}/_install/raspberry-pi-os_armv8/Release/llvm/clang/bin/clang", + "cStandard": "c11", + "cppStandard": "c++17", + "intelliSenseMode": "linux-clang-x64" } ], "version": 4 diff --git a/.vscode/settings.json b/.vscode/settings.json index 816c0c3e..39d5b3d9 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,7 +4,75 @@ }, "files.associations": { "*.cs": "csharp", + "CMakeLists.txt": "cmake", "*.txt": "plaintext", - "*.ipp": "cpp" + "*.ipp": "cpp", + "span": "cpp", + "array": "cpp", + "*.tcc": "cpp", + "memory": "cpp", + "future": "cpp", + "istream": "cpp", + "ranges": "cpp", + "functional": "cpp", + "tuple": "cpp", + "utility": "cpp", + "variant": "cpp", + "__functional_base": "cpp", + "locale": "cpp", + "__verbose_abort": "cpp", + "atomic": "cpp", + "string": "cpp", + "vector": "cpp", + "bit": "cpp", + "bitset": "cpp", + "cctype": "cpp", + "chrono": "cpp", + "cmath": "cpp", + "compare": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "map": "cpp", + "set": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "iterator": "cpp", + "memory_resource": "cpp", + "random": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "type_traits": "cpp", + "fstream": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "limits": "cpp", + "new": "cpp", + "ostream": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "cinttypes": "cpp", + "cwctype": "cpp", + "typeinfo": "cpp", + "__nullptr": "cpp", + "regex": "cpp", + "__config": "cpp", + "__debug": "cpp", + "__node_handle": "cpp", + "__tree": "cpp", + "concepts": "cpp", + "list": "cpp", + "ios": "cpp", + "__threading_support": "cpp", + "any": "cpp", + "__atomic": "cpp", + "filesystem": "cpp" } } \ No newline at end of file diff --git a/CHANGES.md b/CHANGES.md index c9f96b09..a788d838 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,811 +1,855 @@ # 変更履歴 - CHANGE - - 下位互換のない変更 + - 下位互換のない変更 - UPDATE - - 下位互換がある変更 + - 下位互換がある変更 - ADD - - 下位互換がある追加 + - 下位互換がある追加 - FIX - - バグ修正 + - バグ修正 ## develop +## 2023.1.0 + +- [CHANGE] --show-me オプションの削除 + - @melpon +- [CHANGE] ubuntu-18.04_armv8_jetson_nano と ubuntu-18.04_armv8_jetson_xavier パッケージの削除 + - @melpon +- [UPDATE] CMake を 3.27.6 に上げる + - @voluntas +- [UPDATE] SDL を 2.28.3 に上げる + - @voluntas, @melpon +- [UPDATE] CLI11 を 2.3.2 に上げる + - @voluntas, @melpon +- [UPDATE] Boost を 1.83.0 に上げる + - @melpon @voluntas +- [UPDATE] WebRTC を m117.5938.2.0 に上げる + - VP9/AV1 のサイマルキャストが動作するよう対応 + - @melpon, @torikizi +- [UPDATE] NVIDIA VIDEO CODEC SDK を 12.0 に上げる + - @melpon +- [UPDATE] deprecated になった actions/create-release と actions/upload-release の利用をやめて softprops/action-gh-release を利用する + - @melpon +- [UPDATE] m116 で `cricket::Codec` は protected になったため `cricket::CreateVideoCodec` を利用するように修正 + - @torikizi +- [UPDATE] VPL の Init を毎回呼ぶように修正 + - Sora C++ SDK で一部の Windows で VP8 の受信時にクラッシュする問題があり、修正内容を momo に展開する + - @melpon +- [UPDATE] Raspberry Pi 4 で利用する ADM を ALSA から PulseAudio に変更 + - @melpon +- [ADD] Ubuntu 22.04 x86_64 を追加 + - @melpon +- [ADD] ubuntu-20.04_armv8_jetson_xavier(JetPack 5.1.1 対応版のパッケージ)を追加 + - @melpon +- [ADD] ラズパイ専用カメラ(libcamera) に対応 + - `--use-libcamera` と `--use-libcamera-native` オプションを追加 + - @melpon +- [ADD] V4L2 m2m を利用した H.264 のエンコードとデコードに対応 + - @melpon +- [ADD] Raspberry Pi armv8 向けビルドで SDL2 を利用可能にする + - @melpon +- [FIX] metadata に JSON にパースできない値を指定した時に異常終了する問題を修正する + - @miosakuma +- [FIX] ayame モードで momo が offer 時に stats が取得できない問題の修正する + - @kabosy620 + ## 2022.4.1 - [FIX] CI で Windows の場合 $GITHUB_OUTPUT に "\r" が混入するのを除去する - - @miosakuma + - @miosakuma ## 2022.4.0 - [CHANGE] `ubuntu-18.04_x86_64` のビルドを削除 - - @miosakuma + - @miosakuma - [CHANGE] `--multistream` オプションを削除して値を true 固定にする - - @miosakuma + - @miosakuma - [UPDATE] Boost を 1.80.0 に上げる - - @melpon + - @melpon - [UPDATE] SDL を 2.24.1 に上げる - - @melpon + - @melpon - [UPDATE] cmake を 3.24.2 に上げる - - @voluntas + - @voluntas - [UPDATE] libwebrtc を `M107.5304@{#4}` に上げる - - @miosakuma - - @melpon + - @miosakuma + - @melpon - [FIX] `--data-channel-signaling`, `--ignore-disconnect-websocket` に 'none' を指定するとエラーになる問題を修正 - - @miosakuma + - @miosakuma - [FIX] ayame モードの `--channel-id` オプションを `--room-id` に修正 - - @miosakuma + - @miosakuma ## 2022.3.0 - [CHANGE] `--multistream` のデフォルトを true にする - - @melpon + - @melpon - [CHANGE] `--role upstream` と `--role downstream` を削除 - - @melpon + - @melpon - [CHANGE] macos_x86_64 のビルドを削除 - - @melpon + - @melpon - [CHANGE] 音声系オプションの --disable-residual-echo-detector を削除する - - @melpon + - @melpon - [UPDATE] `libwebrtc` を `M104.5112@{#8}` に上げる - - @voluntas, @melpon + - @voluntas, @melpon - [ADD] TURN-TLS 向けの HTTP Proxy サーバの設定を追加する - - `--proxy-url` - - `--proxy-username` - - `--proxy-password` - - @melpon + - `--proxy-url` + - `--proxy-username` + - `--proxy-password` + - @melpon - [ADD] Sora シグナリング用の WebSocket の HTTP Proxy 対応を追加する - - @melpon + - @melpon - [ADD] HTTP Proxy サーバの SNI 対応を追加する - - @melpon + - @melpon - [FIX] Sora の設定によっては `--multistream` オプションに関わらず常に multistream: true になっていたのを修正 - - @melpon + - @melpon ## 2022.2.0 - [UPDATE] CLI11 を 2.2.0 に上げる - - @voluntas + - @voluntas - [UPDATE] Boost 1.79.0 に上げる - - @voluntas + - @voluntas - [UPDATE] `libwebrtc` を `M102.5005@{#1}` に上げる - - @tnoho @voluntas + - @tnoho @voluntas - [ADD] `--client-cert` と `--client-key` でクライアント認証をできるようにする - - @melpon + - @melpon - [ADD] Windows と Ubuntu で NVIDIA VIDEO CODEC SDK を使ったハードウェアデコーダに対応 - - @melpon + - @melpon - [ADD] Windows x86_64 と Ubuntu 20.04 x86_64 で Intel Media SDK に対応 - - @melpon + - @melpon - [FIX] Ubuntu 20.04 + H.264 + サイマルキャスト + --hw-mjpeg-decoder true で落ちるのを修正 (#221) - - @melpon + - @melpon - [FIX] Raspberry Pi + H.264 + --hw-mjpeg-decoder true で、カメラの種類によっては動かないことがあるのを修正 (#141) - - @melpon + - @melpon - [FIX] Raspberry Pi + H.264 + サイマルキャスト + --hw-mjpeg-decoder true で動かないのを修正 (#236) - - @melpon + - @melpon - [FIX] libwebrtc m100 で make_ref_counted を使って scoped_refptr を作るようになったので修正 - - @tnoho + - @tnoho - [FIX] SDL のビルドが mac では declaration-after-statement に触れてビルドが通らないのでパッチで回避 - - @tnoho + - @tnoho ## 2022.1.0 - [UPDATE] Raspberry Pi OS bullseye に対応 - - @tnoho + - @tnoho - [UPDATE] JetPack 4.6 に上げる - - @tnoho + - @tnoho - [UPDATE] `libwebrtc` を `99.4844.1.0` に上げる - - @tnoho + - @tnoho - [UPDATE] sdl2 を 2.0.20 に上げる - - @voluntas + - @voluntas - [UPDATE] cmake を 3.22.3 に上げる - - @voluntas + - @voluntas - [ADD] DataChannel を使うことになっていて Offer を行う際には DataChannel を作るように変更 - - @tnoho + - @tnoho - [FIX] Jetson のハードウェアデコーダーが出力時に出力サイズでフレームを切り抜いていなかったため修正 - - @tnoho + - @tnoho - [FIX] スクリーンキャプチャが Linux で落ちるのを修正 - - @tnoho + - @tnoho ## 2021.6.0 - [UPDATE] Boost 1.78.0 に上げる - - @voluntas + - @voluntas - [UPDATE] cmake を 3.22.1 に上げる - - @melpon -- [FIX] CaptureProcessの終了処理修正。selectの戻り値(retVal)と終了フラグ(quit_)の参照順を変更 - - @KaitoYutaka + - @melpon +- [FIX] CaptureProcess の終了処理修正。select の戻り値(retVal)と終了フラグ(quit\_)の参照順を変更 + - @KaitoYutaka ## 2021.5.0 - [UPDATE] sdl2 を 2.0.18 に上げる - - @voluntas + - @voluntas - [UPDATE] cmake を 3.21.4 に上げる - - @voluntas + - @voluntas - [UPDATE] CLI11 を 2.1.2 に上げる - - @voluntas + - @voluntas - [UPDATE] `libwebrtc` を `97.4692.0.4` に上げる - - @melpon @voluntas + - @melpon @voluntas - [CHANGE] シグナリング URL、チャンネル ID の指定に `--signaling-url`, `--channel-id` オプションを必須にする - - @melpon + - @melpon - [UPDATE] signaling mid 対応 - - @melpon + - @melpon - [ADD] 複数のシグナリング URL 指定を可能にし、type: redirect に対応することでクラスタリングに対応 - - @melpon + - @melpon - [FIX] libwebrtc m93 で `__config_site` が必要になったため追加 - - zakuro からの移植 - - @melpon @voluntas + - zakuro からの移植 + - @melpon @voluntas - [FIX] libwebrtc m93 で api/video-track_source_proxy.h が pc/video_track_source_proxy.h に移動したのを修正 - - zakuro からの移植 - - @melpon @voluntas + - zakuro からの移植 + - @melpon @voluntas ## 2021.4.3 - [FIX] Let's Encrypt な証明書の SSL 接続が失敗する問題を修正する - - @melpon + - @melpon ## 2021.4.2 - [FIX] SetParameters() するタイミングを SetLocalDescription() の処理後に変更する事で Priority が動作するようにする - - @tsuyoshiii + - @tsuyoshiii - [FIX] Priority から DegradationPreference への変換を実動作に合わせる - [UPDATE] cmake を 3.21.3 に上げる - - @voluntas + - @voluntas ## 2021.4.1 - [FIX] Windows 版リリース時の Invoke-WebRequest を curl に擬態する - - @melpon + - @melpon ## 2021.4 - [UPDATE] Boost 1.77.0 に上げる - - @voluntas + - @voluntas - [UPDATE] cmake を 3.21.2 に上げる - - @voluntas + - @voluntas - [UPDATE] `libwebrtc` を `M92.4515@{#9}` に上げる - - @melpon + - @melpon - [UPDATE] CLI11 を 2.0.0 に上げる - - @melpon + - @melpon - [UPDATE] AES-GCM を候補に含める - - @melpon + - @melpon - [ADD] Sora モードの DataChannel を使ったシグナリングに対応 - - Sora 2021.1 から利用可能です - - 以下のオプションを追加 - - `--data-channel-signaling` - - `--data-channel-signaling-timeout` - - `--ignore-disconnect-websocket` - - `--disconnect-wait-timeout` - - @melpon + - Sora 2021.1 から利用可能です + - 以下のオプションを追加 + - `--data-channel-signaling` + - `--data-channel-signaling-timeout` + - `--ignore-disconnect-websocket` + - `--disconnect-wait-timeout` + - @melpon - [ADD] Sora モードのシグナリング re-offer に対応 - - Sora 2021.1 から利用可能です - - @melpon + - Sora 2021.1 から利用可能です + - @melpon - [FIX] Sora モードのサイマルキャスト active: false に対応 - - @melpon + - @melpon ## 2021.3 - [UPDATE] `libwebrtc` を `M90.4430@{#3}` に上げる - - @voluntas + - @voluntas - [UPDATE] Boost 1.76.0 に上げる - - @voluntas + - @voluntas - [UPDATE] cmake を 3.20.1 に上げる - - @voluntas + - @voluntas - [FIX] サイマルキャストのエラー・メッセージで示されていたオプションが古かったので修正する - - @enm10k + - @enm10k ## 2021.2.3 - [UPDATE] cmake を 3.20.0 に上げる - - @melpon @voluntas + - @melpon @voluntas - [FIX] Jetson で HW エンコーダー指定時に、初期化タイミングによって、まれにセグフォが発生する問題を修正する - - @enm10k + - @enm10k ## 2021.2.2 - [UPDATE] cmake を 3.19.6 に上げる - - @voluntas + - @voluntas - [UPDATE] `libwebrtc` を `M89.4389@{#7}` に上げる - - @voluntas + - @voluntas - [FIX] `momo --verson` 実行時にエラーメッセージが出るのを修正 - - HW エンコーダが利用できるかをチェックしている際に利用できない場合に標準出力にエラーが出てしまうのを抑制するという方法で修正 - - @melpon @torikizi + - HW エンコーダが利用できるかをチェックしている際に利用できない場合に標準出力にエラーが出てしまうのを抑制するという方法で修正 + - @melpon @torikizi - [FIX] OpenSSLCertificate では無くなったので BoringSSLCertificate を利用するように修正 - - TURN-TLS でセグフォする問題を解決 - - @melpon @tnoho + - TURN-TLS でセグフォする問題を解決 + - @melpon @tnoho ## 2021.2.1 - [FIX] ubuntu-18.04_armv8 向け libwebrtc ビルドで Jetson が動かない問題を解消する - - @tnoho + - @tnoho - [UPDATE] `libwebrtc` を `M89.4389@{#5}` に上げる - - @tnoho + - @tnoho - [UPDATE] cmake を 3.19.5 に上げる - - @voluntas + - @voluntas ## 2021.2 - [CHANGE] M89 で使用不可になったため対応ピクセルフォーマットから `NV12` を削除 - - @tnoho + - @tnoho - [UPDATE] `libwebrtc` を `M89.4389@{#4}` に上げる - - @tnoho + - @tnoho - [UPDATE] JetPack を 4.5 にする - - @tnoho + - @tnoho - [UPDATE] cmake を 3.19.4 に上げる - - @voluntas + - @voluntas ## 2021.1 - [UPDATE] cmake を 3.19.3 に上げる - - @voluntas + - @voluntas - [UPDATE] GitHub Actions の macOS ビルドを macos-11.0 に変更する - - @voluntas + - @voluntas - [UPDATE] Boost 1.75.0 に上げる - - @voluntas + - @voluntas - [UPDATE] nlohmann/json を Boost.JSON に変更 - - @melpon + - @melpon - [ADD] サイマルキャストの active と adaptivePtime に対応 - - @melpon + - @melpon - [ADD] Apple Silicon 対応 - - @hakobera + - @hakobera ## 2020.11 - [UPDATE] cmake を 3.19.2 に上げる - - @voluntas + - @voluntas - [UPDATE] `libwebrtc` を `M88.4324@{#3}` に上げる - - @voluntas + - @voluntas - [ADD] 統計情報を HTTP API で取得できるようにする - - 統計情報サーバーのポート番号を指定する `--metrics-port INT` を追加 - - ループバック (127.0.0.1 で listen) がデフォルト、グローバル (0.0.0.0 で listen) アクセスを許可する場合は `--metrics-allow-external-ip` 引数を指定する - - @hakobera + - 統計情報サーバーのポート番号を指定する `--metrics-port INT` を追加 + - ループバック (127.0.0.1 で listen) がデフォルト、グローバル (0.0.0.0 で listen) アクセスを許可する場合は `--metrics-allow-external-ip` 引数を指定する + - @hakobera ## 2020.10 - [CHANGE] `--use-native` を `--hw-mjpeg-decoder=` に変更して、ソフトウェアエンコーダとの組み合わせを不可にする - - @melpon @tnoho + - @melpon @tnoho - [UPDATE] `libwebrtc` を `M88.4324@{#0}` に上げる - - @tnoho @melpon @voluntas + - @tnoho @melpon @voluntas - [UPDATE] cmake を 3.18.4 に上げる - - @voluntas + - @voluntas - [ADD] Jetson Nano で VP8 HWA が利用できるようにする - - @tnoho + - @tnoho ## 2020.9 - [CHANGE] `ubuntu-16.04_x86_64_ros` を削除 - - @melpon + - @melpon - [CHANGE] Jetson のフレーム変換順序を変更 - - @tnoho + - @tnoho - [CHANGE] `raspberry-pi-os_armv8` から SDL を削除 - - @melpon + - @melpon - [CHANGE] `--multistream` と `--simulcast` に引数 true/false の指定を必要にする - - @melpon + - @melpon - [CHANGE] `--audio-bitrate` を `--audio-bit-rate` に変更 - - @melpon + - @melpon - [CHANGE] `--video-bitrate` を `--video-bit-rate` に変更 - - @melpon + - @melpon - [CHANGE] `--audio-codec` を `--audio-codec-type` に変更 - - @melpon + - @melpon - [CHANGE] `--video-codec` を `--video-codec-type` に変更 - - @melpon + - @melpon - [CHANGE] `--spotlight INT` を `--spotlight BOOL` に変更(true/false で指定) - - @melpon + - @melpon - [UPDATE] Boost 1.74.0 に上げる - - @voluntas + - @voluntas - [UPDATE] cmake を 3.18.3 に上げる - - @voluntas + - @voluntas - [UPDATE] json を 3.9.1 に上げる - - @voluntas + - @voluntas - [UPDATE] `libwebrtc` を `M86.4240@{#10}` に上げる - - @voluntas + - @voluntas - [ADD] `--spotlight-number INT` の引数を追加 - - @melpon + - @melpon - [FIX] `SDL_PollEvent` の扱い方を修正 - - @melpon + - @melpon - [FIX] スクリーンキャプチャの前に `CoInitializeEx` が必要になっていたのを修正 - - @torikizi @melpon + - @torikizi @melpon ## 2020.8.1 - [UPDATE] `libwebrtc` を `M85.4183@{#2}` に上げる - - @voluntas + - @voluntas ## 2020.8 - [CHANGE] パッケージ名 `ubuntu-18.04_armv8_jetson` を `ubuntu-18.04_armv8_jetson_nano` と `ubuntu-18.04_armv8_jetson_xavier` に変更 - - @tnoho + - @tnoho - [ADD] macOS でも全画面スクリーンキャプチャ機能を利用できるようにする - - @hakobera + - @hakobera - [ADD] Jetson Xavier シリーズで VP9 HWA を有効にする - - @tnoho @melpon + - @tnoho @melpon - [ADD] サイマルキャストへの対応を追加 - - Sora モードで利用可能 - - @melpon @shino + - Sora モードで利用可能 + - @melpon @shino - [UPDATE] Jetson の RootFS 構築方法をリポジトリからの取得に変更 - - @tnoho + - @tnoho - [UPDATE] `libwebrtc` を `M85.4183@{#1}` に上げる - - @hakobera @voluntas + - @hakobera @voluntas - [UPDATE] CLI11 を v1.9.1 にアップデートする - - @voluntas + - @voluntas - [UPDATE] json を 3.8.0 に上げる - - @voluntas + - @voluntas - [UPDATE] cmake を 3.18.0 に上げる - - @voluntas + - @voluntas ## 2020.7 - [UPDATE] `libwebrtc` を `M84.4147@{#7}` に上げる - - @voluntas @melpon + - @voluntas @melpon - [UPDATE] cmake を 3.17.3 に上げる - - @voluntas + - @voluntas - [UPDATE] Boost 1.73.0 にアップデートする - - @voluntas + - @voluntas - [UPDATE] Jetson Nano 用のライブラリを NVIDIA L4T 32.4.2 に上げる - - @melpon + - @melpon - [ADD] Ubuntu 20.04 x86_64 に対応する - - @hakobera + - @hakobera - [ADD] ビデオエンコーダデコーダを表示する `--video-codec-engines` を追加 - - @melpon + - @melpon - [ADD] GitHub Actions の Boost をキャッシュ化する - - @melpon + - @melpon - [ADD] 全画面スクリーンキャプチャ機能を Windows / Linux 向けに追加する - - ``--screen-capture`` 指定することで利用可能 - - @melpon + - `--screen-capture` 指定することで利用可能 + - @melpon - [ADD] `raspberry-pi-os_armv8` を追加 - - @melpon + - @melpon - [ADD] ビデオコーデックのエンジン名を指定できる機能を実装 - - @melpon + - @melpon - [CHANGE] パッケージ名 `ubuntu-18.04_armv8_jetson_nano` を `ubuntu-18.04_armv8_jetson` に変更 - - @melpon + - @melpon - [CHANGE] パッケージ名 `raspbian-buster_armv6` と `raspbian-buster_armv7` を `raspberry-pi-os_armv6` と `raspberry-pi-os_armv7` に変更 - - @melpon + - @melpon - [FIX] Windows の ADM に専用の関数を使うようにする - - @torikizi @melpon + - @torikizi @melpon - [FIX] build.sh の --no-tty オプションのヘルプメッセージの修正 - - @hakobera + - @hakobera ## 2020.6 - [UPDATE] `libwebrtc` を `M84.4127@{#0}` に上げる - - @voluntas + - @voluntas - [ADD] test モードの Momo と Ayame モードの Momo の相互接続を可能とする - - @tnoho + - @tnoho - [CHANGE] ubuntu-16.04_armv7_ros ビルドを削除 - - @melpon + - @melpon ## 2020.5.2 - [FIX] AV1 が利用できなかったのを修正する - - @torikizi @voluntas + - @torikizi @voluntas - [UPDATE] `libwebrtc` を `M84.4104@{#0}` に上げる - - @voluntas + - @voluntas ## 2020.5.1 - [FIX] CMakeLists.txt のタイポを修正する - - @azamiya @torikizi @tnoho @melpon + - @azamiya @torikizi @tnoho @melpon ## 2020.5 リリース日: 2020.04.14 - [UPDATE] `libwebrtc` を `M83.4103@{#2}` に上げる - - @voluntas + - @voluntas - [UPDATE] `libwebrtc` を `M81.4044@{#13}` に上げる - - @voluntas + - @voluntas - [UPDATE] `cmake` を `3.17.1` に上げる - - @voluntas + - @voluntas - [ADD] 実験的に AV1 に対応する - - Sora モードでのみ利用可能 - - @voluntas @tnoho + - Sora モードでのみ利用可能 + - @voluntas @tnoho - [FIX] Jetson Nano では ALSA ではなく PulseAudio を利用する - - Jetson Nano でつながらない問題が発生するのを修正 - - @azamiya @torikizi @tnoho @melpon + - Jetson Nano でつながらない問題が発生するのを修正 + - @azamiya @torikizi @tnoho @melpon ## 2020.4 リリース日: 2020.04.01 - [UPDATE] `libwebrtc` を `M81.4044@{#11}` に上げる - - @voluntas + - @voluntas - [UPDATE] `sdl2` を `2.0.12` に上げる - - @voluntas + - @voluntas - [UPDATE] `cmake` を `3.17.0` に上げる - - @voluntas + - @voluntas - [ADD] Windows でも `--video-device` を指定できるようにする - - @msnoigrs + - @msnoigrs - [ADD] sora モードの引数に `--audio` と `--video` を追加 - - @melpon + - @melpon - [CHANGE] ルートでの `--port` 引数を削除し、`sora` モードと `test` モードで `--port` を指定する - - @melpon + - @melpon - [CHANGE] `sora` モードで `--port` を指定していない場合、`--auto` を指定しなくても自動的に接続する - - @melpon + - @melpon - [CHANGE] `--daemon` 引数を削除 - - @melpon + - @melpon - [CHANGE] `--no-video` と `--no-audio` 引数を `--no-video-device` と `--no-audio-device` に変更 - - @melpon + - @melpon - [CHANGE] PCMU オーディオコーデックを削除 - - @melpon + - @melpon - [CHANGE] sora モードの `--video-codec` や `--audio-codec` を指定しなかった場合、Sora 側のデフォルト値を使うようにする - - 今までは VP8, OPUS だった - - @melpon -- [FIX] video_adapter_メンバ変数は使用していないので削除する - - @msnoigrs + - 今までは VP8, OPUS だった + - @melpon +- [FIX] video*adapter*メンバ変数は使用していないので削除する + - @msnoigrs - [FIX] Ubuntu 18.04 で `libcuda.so` / `libnvcuvid.so` がイントールされていなくても起動するようにする - - @melpon + - @melpon ## 2020.3.1 - [FIX] ubuntu-18.04_x86_64 で H.264 を有効にする - - @melpon + - @melpon ## 2020.3 - [UPDATE] Raspberry Pi の H.264 を利用時のリサイズ処理をハードウェアに変更する - - VPU でソフトウェア処理される `vc.ril.resize` からハードウェア処理される `vc.ril.isp` への変更 - - YUV の形式が異なる場合の変換処理もハードウェアに変更 - - @tnoho + - VPU でソフトウェア処理される `vc.ril.resize` からハードウェア処理される `vc.ril.isp` への変更 + - YUV の形式が異なる場合の変換処理もハードウェアに変更 + - @tnoho - [UPDATE] libwebrtc を M81.4044@{#9} に上げる - - @voluntas + - @voluntas - [UPDATE] libwebrtc を M81.4044@{#7} に上げる - - @voluntas + - @voluntas - [UPDATE] libwebrtc を M80.3987@{#6} に上げる - - @voluntas + - @voluntas - [ADD] Windows 10 で NVIDIA VIDEO CODEC SDK を利用した H.264 ハードウェアエンコーダへ対応 - - @melpon + - @melpon - [ADD] Ubuntu 18.04 で NVIDIA VIDEO CODEC SDK を利用した H.264 ハードウェアエンコーダへ対応 - - @melpon + - @melpon - [ADD] TLS チェックを行わない --insecure オプションを追加 - - @melpon + - @melpon - [ADD] WSS と TURN-TLS 時の証明書チェックを libwebrtc ハードコードとデフォルトパス両方を利用するようにする - - @melpon + - @melpon - [ADD] WebRTC カスタム用のスクリプトを追加 - - @melpon + - @melpon - [ADD] Sora モード利用時の `type: pong` で stats 取得して送るようにする - - @melpon + - @melpon - [ADD] Raspberry Pi で SDL 利用時に H264 ハードウェアデコーダを利用するようにする - - @tnoho + - @tnoho - [FIX] Jetson Nano で --use-native を使った際に FHD 設定で下部に緑の帯が出るのを修正 - - https://github.com/shiguredo/momo/issues/124 - - @tetsu-koba @tnoho + - https://github.com/shiguredo/momo/issues/124 + - @tetsu-koba @tnoho - [FIX] Jetson Nano で H264 デコーダを止める際にハングしてしまう問題を修正 - - @soudegesu @tnoho + - @soudegesu @tnoho - [FIX] macOS で WebRTC のバージョンが埋め込まれていなかった問題を修正 - - @melpon + - @melpon - [FIX] Jetson Nano で RTP タイムスタンプが 90kHz になっていなかったのを修正 - - https://github.com/shiguredo/momo/pull/137 - - @tetsu-koba @tnoho + - https://github.com/shiguredo/momo/pull/137 + - @tetsu-koba @tnoho ## 2020.2.1 **hotfix** - [FIX] macOS で --use-sdl オプションを利用すると落ちていたのを修正する - - https://bugzilla.libsdl.org/show_bug.cgi?id=4617 - - @melpon + - https://bugzilla.libsdl.org/show_bug.cgi?id=4617 + - @melpon ## 2020.2 - [UPDATE] CLI11 を v1.9.0 にアップデートする - - @voluntas + - @voluntas - [ADD] Windows 10 対応を追加 - - @melpon + - @melpon - [ADD] Windows の Sora/Ayame モード利用時のシグナリング接続情報に environment / libwebrtc / sora_client を追加 - - `"environment": "[x64] Windows 10.0 Build 18362"` - - `"libwebrtc": "Shiguredo-Build M80.3987@{#2} (80.3987.2.1 fba51dc6)"` - - `"sora_client": "WebRTC Native Client Momo 2020.1 (0ff24ff3)"` - - @melpon + - `"environment": "[x64] Windows 10.0 Build 18362"` + - `"libwebrtc": "Shiguredo-Build M80.3987@{#2} (80.3987.2.1 fba51dc6)"` + - `"sora_client": "WebRTC Native Client Momo 2020.1 (0ff24ff3)"` + - @melpon - [ADD] ビルド環境を CMake 化 - - @melpon + - @melpon - [CHANGE] ubuntu-18.04_armv8 のビルドを削除 - - @melpon + - @melpon ## 2020.1 - [UPDATE] libwebrtc を M80.3987@{#2} に上げる - - libwebrtc のハッシュは fba51dc69b97f6f170d9c325a38e05ddd69c8b28 - - @melpon + - libwebrtc のハッシュは fba51dc69b97f6f170d9c325a38e05ddd69c8b28 + - @melpon - [UPDATE] Momo 2020.1 にバージョンを上げる - - バージョン番号を <リリース年>.<その年のリリース回数> に変更 - - @voluntas + - バージョン番号を <リリース年>.<その年のリリース回数> に変更 + - @voluntas - [UPDATE] Boost 1.72.0 にアップデートする - - @voluntas + - @voluntas - [UPDATE] --video-device を Linux 全般で有効にする - - V4L2 capturer を使うようにした - - @shino + - V4L2 capturer を使うようにした + - @shino - [UPDATE] Jetson Nano 用のライブラリを NVIDIA L4T 32.3.1 に上げる - - [L4T \| NVIDIA Developer](https://developer.nvidia.com/embedded/linux-tegra) - - @melpon + - [L4T \| NVIDIA Developer](https://developer.nvidia.com/embedded/linux-tegra) + - @melpon - [UPDATE] 音声系オプションの --disable-residual-echo-detector を追加する - - @melpon + - @melpon - [ADD] データチャネルを利用したシリアルポートへの読み書き機能を追加する - - --serial を指定することでデータチャネル経由でのシリアル読み書きが可能になる - - test と ayame モードでのみ利用可能 - - @tnoho + - --serial を指定することでデータチャネル経由でのシリアル読み書きが可能になる + - test と ayame モードでのみ利用可能 + - @tnoho - [ADD] 自由に解像度の値を指定できるようにする - - `--resolution 640x480` のように指定できるようになりました - - この機能が有効になるのは、カメラに依存するため動作保証はありません - - @melpon + - `--resolution 640x480` のように指定できるようになりました + - この機能が有効になるのは、カメラに依存するため動作保証はありません + - @melpon - [ADD] Sora モード利用時のシグナリング接続情報に enviroment / libwebrtc / sora_client を追加する - - Jetson Nano の場合 - - `"environment": "[aarch64] Ubuntu 18.04.3 LTS (nvidia-l4t-core 32.2.1-20190812212815)"` - - `"libwebrtc": "Shiguredo-Build M80.3987@{#2} (80.3987.2.1 15b26e4)"` - - `"sora_client": "WebRTC Native Client Momo 2020.1 (f6b69e77)"` - - macOS の場合 - - `"environment": "[x86_64] macOS Version 10.15.2 (Build 19C57)"` - - `"libwebrtc": "Shiguredo-Build M80.3987@{#2} (80.3987.2.1 15b26e4)"` - - `"sora_client": "WebRTC Native Client Momo 2020.1 (f6b69e77)"` - - Ubuntu 18.04 x86_64 の場合 - - `"environment": "[x86_64] Ubuntu 18.04.3 LTS"` - - `"libwebrtc": "Shiguredo-Build M80.3987@{#2} (80.3987.2.1 15b26e4)"` - - `"sora_client": "WebRTC Native Client Momo 2020.1 (f6b69e77)"` - - @melpon + - Jetson Nano の場合 + - `"environment": "[aarch64] Ubuntu 18.04.3 LTS (nvidia-l4t-core 32.2.1-20190812212815)"` + - `"libwebrtc": "Shiguredo-Build M80.3987@{#2} (80.3987.2.1 15b26e4)"` + - `"sora_client": "WebRTC Native Client Momo 2020.1 (f6b69e77)"` + - macOS の場合 + - `"environment": "[x86_64] macOS Version 10.15.2 (Build 19C57)"` + - `"libwebrtc": "Shiguredo-Build M80.3987@{#2} (80.3987.2.1 15b26e4)"` + - `"sora_client": "WebRTC Native Client Momo 2020.1 (f6b69e77)"` + - Ubuntu 18.04 x86_64 の場合 + - `"environment": "[x86_64] Ubuntu 18.04.3 LTS"` + - `"libwebrtc": "Shiguredo-Build M80.3987@{#2} (80.3987.2.1 15b26e4)"` + - `"sora_client": "WebRTC Native Client Momo 2020.1 (f6b69e77)"` + - @melpon - [ADD] Ayame モード利用時のシグナリング接続情報に enviroment / libwebrtc / ayameClient を追加する - - Sora 時の sora_client が ayameClient に変わります - - @melpon + - Sora 時の sora_client が ayameClient に変わります + - @melpon - [ADD] Raspbian ミラーを追加する - - @melpon + - @melpon - [CHANGE] momo --help の英語化 - - @shino @msnoigrs + - @shino @msnoigrs - [CHANGE] .edit の機能とドキュメントを削除 - - @melpon + - @melpon - [CHANGE] armv6 で SDL を使えなくする - - @melpon + - @melpon - [FIX] --no-video を指定しているにもかかわらずカメラを一瞬だけ掴むのを修正する - - @melpon @mganeko + - @melpon @mganeko - [FIX] SDL が有効でない時に SDL 関連のオプションを指定するとエラーにする - - @melpon + - @melpon - [FIX] macOS のビルドで Python 2.7 必須を外す - - @melpon + - @melpon - [FIX] Ayame モードで WebSocket が閉じられた際に再接続処理に進まない箇所を修正 - - @Hexa + - @Hexa - [FIX] Ayame モードで シグナリングで bye を受信した際処理として、各 close 処理を追加する - - @Hexa + - @Hexa - [FIX] Ayame モードで 再接続処理の 1 回目を、5 秒後からすぐに実行されるように変更する - - @Hexa + - @Hexa ## 19.12.1 - [UPDATE] libwebrtc を時前ビルドしないようにする - - https://github.com/shiguredo-webrtc-build/webrtc-build を利用する - - @melpon + - https://github.com/shiguredo-webrtc-build/webrtc-build を利用する + - @melpon - [FIX] momo + ayame モードで再接続時に delay してしまう問題を解決 - - @kdxu + - @kdxu ## 19.12.0 - [UPDATE] libwebrtc M79 コミットポジションを 5 にする - - libwebrtc のハッシュは b484ec0082948ae086c2ba4142b4d2bf8bc4dd4b - - @voluntas + - libwebrtc のハッシュは b484ec0082948ae086c2ba4142b4d2bf8bc4dd4b + - @voluntas - [UPDATE] json を 3.7.3 に上げる - - @voluntas + - @voluntas - [ADD] sora モード利用時の --role に sendrecv | sendonly | recvonly を指定できるようにする - - @melpon + - @melpon - [FIX] QVGA の指定を 320x240 にする - - @melpon @Bugfire + - @melpon @Bugfire - [FIX] ayame モードで再接続時に segmentation fault が起こる場合があるのを修正する - - ただし、互いに接続を確立するまでping-pongを送らない/ping timeoutで再接続するまで数秒かかることがある」ので、再接続によって受信側が数秒待つ必要が出てくる可能性がある - - 上記の問題はこの修正では未解決 - - @kdxu + - ただし、互いに接続を確立するまで ping-pong を送らない/ping timeout で再接続するまで数秒かかることがある」ので、再接続によって受信側が数秒待つ必要が出てくる可能性がある + - 上記の問題はこの修正では未解決 + - @kdxu - [FIX] OpenH264 を明示的にビルドしないようにする - - @melpon + - @melpon ## 19.11.1 - [ADD] Raspberry Pi 4 での動作を確認 - - @voluntas @Hexa + - @voluntas @Hexa - [UPDATE] libwebrtc M79 コミットポジションを 3 にする - - libwebrtc のハッシュは 2958d0d691526c60f755eaa364abcdbcda6adc39 - - @voluntas + - libwebrtc のハッシュは 2958d0d691526c60f755eaa364abcdbcda6adc39 + - @voluntas - [UPDATE] libwebrtc M79 コミットポジションを 2 にする - - libwebrtc のハッシュは 8e36cc906e5e1c16486e60e62acbf79c1c691879 - - @voluntas + - libwebrtc のハッシュは 8e36cc906e5e1c16486e60e62acbf79c1c691879 + - @voluntas - [UPDATE] Ayame で isExistUser フラグが accept 時に返却されなかった場合 2 回 peer connection を生成する - [ADD] SDL を利用した音声と映像の受信可能にする `--use-sdl` を追加する - - [Simple DirectMedia Layer](https://www.libsdl.org/) - - @tnoho + - [Simple DirectMedia Layer](https://www.libsdl.org/) + - @tnoho - [ADD] SDL を Sora のマルチストリームに対応する `--multistream` を追加する - - @tnoho + - @tnoho - [ADD] SDL を Sora のスポットライトに対応する `--spotlight` を追加する - - @tnoho + - @tnoho - [ADD] SDL 利用時に Jetson Nano では H.264 ハードウェアデコーダを利用するようにする - - @tnoho + - @tnoho - [ADD] SDL 利用時に自分のカメラ映像を表示する `--show-me` を追加する - - @tnoho + - @tnoho - [ADD] SDL 利用時に映像を表示するウインドウの幅を `--window-width` と `--window-height` で指定可能にする - - @tnoho + - @tnoho - [ADD] SDL 利用時に映像を表示するウインドウをフルスクリーンにする `--fullscreen` を追加する - - f を押すと全画面、もう一度 f を押すと戻る - - @tnoho + - f を押すと全画面、もう一度 f を押すと戻る + - @tnoho - [ADD] sora 利用時に `--role upstream` または `--role downstream` を指定できるようにする - - @melpon + - @melpon - [CHANGE] ayame の `accept` 時に返却される `isExistUser` フラグによって offer を送るかどうかを決めるよう変更する - - @kdxu + - @kdxu - [FIX] C++14 にする - - @melpon -- [FIX] USE_H264が定義されない場合でも--video-codecが使えるように修正する - - @msnoigrs + - @melpon +- [FIX] USE_H264 が定義されない場合でも--video-codec が使えるように修正する + - @msnoigrs ## 19.11.0 - [UPDATE] json を 3.7.1 に上げる - - @voluntas + - @voluntas - [UPDATE] GitHub Actions の macOS ビルドを macos-latest に変更する - - @voluntas + - @voluntas - [UPDATE] libwebrtc M78 コミットポジションを 8 にする - - libwebrtc のハッシュは 0b2302e5e0418b6716fbc0b3927874fd3a842caf - - @voluntas + - libwebrtc のハッシュは 0b2302e5e0418b6716fbc0b3927874fd3a842caf + - @voluntas - [ADD] GitHub Actions のデイリービルドに ROS を追加する - - @voluntas + - @voluntas - [ADD] GitHub Actions のビルドに Jetson Nano と macOS を追加する - - @voluntas + - @voluntas - [ADD] Jetson Nano で 4K@30 出すためのドキュメントを追加 - - @tnoho @voluntas + - @tnoho @voluntas - [ADD] macOS 用に --video-device オプションを追加 - - @hakobera + - @hakobera - [FIX] GitHub Actions のビルドがディスク容量不足でエラーになっていたのを修正する - - @hakobera + - @hakobera - [FIX] ayame の client id を指定していない場合のランダム生成がうまくいっていなかったので修正する - - @kdxu + - @kdxu - [FIX] ROS バージョンが正常にビルドできていなかったのを修正する - - @melpon + - @melpon ## 19.09.2 - [UPDATE] libwebrtc M78 コミットポジションを 5 にする - - libwebrtc のハッシュは dfa0b46737036e347acbd3b47f0f58ff6c8350c8 - - @voluntas -- [FIX] iceServers が json プロパティかつ array の場合のみ ice_servers_ にセットするよう修正する - - @kdxu + - libwebrtc のハッシュは dfa0b46737036e347acbd3b47f0f58ff6c8350c8 + - @voluntas +- [FIX] iceServers が json プロパティかつ array の場合のみ ice*servers* にセットするよう修正する + - @kdxu ## 19.09.1 - [ADD] Jetson Nano のハードウェアエンコーダを利用する機能を実装 - - @tnoho + - @tnoho - [ADD] Jetson Nano のビルドを追加 - - @melpon + - @melpon - [ADD] CI を CircleCI から GitHub Actions へ切り替える - - macOS の時間制限が OSS の場合はないため Weekly build から Daily build のみにきりかえる - - @hakobera + - macOS の時間制限が OSS の場合はないため Weekly build から Daily build のみにきりかえる + - @hakobera - [ADD] .clang-format の追加 - - @melpon + - @melpon - [UPDATE] libwebrtc M78 コミットポジションを 3 にする - - libwebrtc のハッシュは 68c715dc01cd8cd0ad2726453e7376b5f353fcd1 - - @voluntas + - libwebrtc のハッシュは 68c715dc01cd8cd0ad2726453e7376b5f353fcd1 + - @voluntas - [UPDATE] コマンドオプションをできるだけ共通化する - - @melpon + - @melpon - [UPDATE] Raspberry Pi のビルド OS を Ubuntu 16.04 から 18.04 に上げる - - @melpon + - @melpon ## 19.09.0 - [ADD] --disable-echo-cancellation オプションを追加 - - @melpon + - @melpon - [ADD] --disable-auto-gain-control オプションを追加 - - @melpon + - @melpon - [ADD] --disable-noise-suppression オプションを追加 - - @melpon + - @melpon - [ADD] --disable-highpass-filter オプションを追加 - - @melpon + - @melpon - [ADD] --disable-typing-detection オプションを追加 - - @melpon + - @melpon - [UPDATE] Boost 1.71.0 にアップデートする - - @voluntas + - @voluntas - [UPDATE] libwebrtc M78 コミットポジションを 0 にする - - libwebrtc のハッシュは 5b728cca77c46ed47ae589acba676485a957070b - - @tnoho + - libwebrtc のハッシュは 5b728cca77c46ed47ae589acba676485a957070b + - @tnoho - [UPDATE] libwebrtc M77 コミットポジションを 10 にする - - libwebrtc のハッシュは ad73985e75684cb4ac4dadb9d3d86ad0d66612a0 - - @voluntas + - libwebrtc のハッシュは ad73985e75684cb4ac4dadb9d3d86ad0d66612a0 + - @voluntas - [FIX] Track を複数の PeerConnection で共有するよう修正 - - @tnoho + - @tnoho - [FIX] --no-audio 設定時にも capturer をチェックしていたので修正 - - @tnoho + - @tnoho - [FIX] PeerConnectionObserver の解放がなかったため修正 - - @tnoho + - @tnoho ## 19.08.1 - [ADD] Raspberry Pi 用に `--video-device` オプションを追加 - - @melpon + - @melpon - [UPDATE] sora の metadata オプションを公開する - - @melpon + - @melpon ## 19.08.0 - [UPDATE] nlohmann/json を v3.7.0 にアップデートする - - @melpon + - @melpon - [UPDATE] Raspbian Buster に対応 - - @voluntas + - @voluntas - [UPDATE] libwebrtc M77 コミットポジションを 6 にする - - libwebrtc のハッシュは 71e2db7296a26c6d9b18269668d74b764a320680 - - @voluntas + - libwebrtc のハッシュは 71e2db7296a26c6d9b18269668d74b764a320680 + - @voluntas - [UPDATE] libwebrtc M77 コミットポジションを 3 にする - - libwebrtc のハッシュは 3d8e627cb5893714a66082544d562cbf4a561515 - - @kdxu @voluntas + - libwebrtc のハッシュは 3d8e627cb5893714a66082544d562cbf4a561515 + - @kdxu @voluntas - [UPDATE] libwebrtc M76 コミットポジションを 3 にする - - libwebrtc のハッシュは 9863f3d246e2da7a2e1f42bbc5757f6af5ec5682 - - @voluntas + - libwebrtc のハッシュは 9863f3d246e2da7a2e1f42bbc5757f6af5ec5682 + - @voluntas - [UPDATE] I420 の時にもハードウェアでリサイズする - - @tnoho + - @tnoho - [ADD] Raspberry Pi 向けに --use-native オプションを追加しました - - USB カメラ用で MJPEG をハードウェアデコードします - - @tnoho + - USB カメラ用で MJPEG をハードウェアデコードします + - @tnoho - [ADD] Raspberry Pi 向けに --force-i420 オプションを追加しました - - Raspberry Pi 専用カメラ用で MJPEG を使えないため HD 以上の解像度でも MJPEG にせず強制的に I420 でキャプチャーする - - @tnoho + - Raspberry Pi 専用カメラ用で MJPEG を使えないため HD 以上の解像度でも MJPEG にせず強制的に I420 でキャプチャーする + - @tnoho - [ADD] Ayame のサブコマンドに --signaling-key を追加する - - @kdxu @tnoho + - @kdxu @tnoho - [ADD] Ayame 利用時に iceServers の払い出しに対応する - - 独自の STUN/TURN が利用可能になる - - @kdxu @tnoho + - 独自の STUN/TURN が利用可能になる + - @kdxu @tnoho - [CHANGE] Ayame のサブコマンドで client id を optional に指定できるように修正する - - @kdxu + - @kdxu - [CHANGE] ./momo p2p を ./momo test に変更する - - @melpon + - @melpon - [FIX] Ayame の candidate 交換の際の JSON スキーマが間違っていたのを修正する - - @kdxu + - @kdxu - [FIX] Ayame の sdp 交換の際の type が answer 固定になっていたのを修正する - - @kdxu + - @kdxu - [FIX] Ayame で peer connection 生成後に createOffer して send する実装が漏れていたので追加する - - @kdxu + - @kdxu - [FIX] Ayame で momo を起動したあとに映像を受信できない場合が発生するのバグを修正する - - @kdxu + - @kdxu - [FIX] Raspberry Pi でハードウェアエンコーダを利用した際に再接続できなくなることがある問題の修正 - - @tnoho + - @tnoho - [FIX] libwebrtc M77 で作成した armv6 バイナリがクラッシュしてしまう問題の対策 - - @tnoho + - @tnoho - [FIX] macOS 版 Momo で VideoToolbox 利用時の解像度変更時に落ちる問題の修正 - - @hakobera + - @hakobera - [FIX] macOS 版がビルドは成功するが動作させようとするとセグメンテーションフォルトする問題の修正 - - @hakobera -- [FIX] Raspberry Pi でハードウェアエンコーダを利用した際にGPUのメモリを食いつぶしてしまう問題の修正 - - @tnoho + - @hakobera +- [FIX] Raspberry Pi でハードウェアエンコーダを利用した際に GPU のメモリを食いつぶしてしまう問題の修正 + - @tnoho ## 19.07.0 - [UPDATE] Raspberry Pi の H.264 を MMAL を利用したハードウェアエンコードに変更する - - 720p 30fps や 1080p 20fps を可能にする - - @tnoho + - 720p 30fps や 1080p 20fps を可能にする + - @tnoho - [UPDATE] libwebrtc を M75 に上げる - - libwebrtc のハッシュは 159c16f3ceea1d02d08d51fc83d843019d773ec6 - - @tnoho + - libwebrtc のハッシュは 159c16f3ceea1d02d08d51fc83d843019d773ec6 + - @tnoho - [UPDATE] libwebrtc を M76 に上げる - - libwebrtc のハッシュは d91cdbd2dd2969889a1affce28c89b8c0f8bcdb7 - - @kdxu + - libwebrtc のハッシュは d91cdbd2dd2969889a1affce28c89b8c0f8bcdb7 + - @kdxu - [UPDATE] Unified Plan に対応する - - @tnoho + - @tnoho - [UPDATE] no-audio 時に AudioDevice を無効化するよう変更 - - @tnoho + - @tnoho - [UPDATE] CLI11 を v1.8.0 にアップデートする - - @melpon + - @melpon - [UPDATE] JSON v3.6.1 にアップデートする - - @melpon + - @melpon - [UPDATE] macOS のビルドドキュメントを独立させる - - @voluntas + - @voluntas - [UPDATE] doc/CACHE.md を削除 - - make PACKAGE.clean にてビルドキャッシュの削除が可能になったため - - @melpon + - make PACKAGE.clean にてビルドキャッシュの削除が可能になったため + - @melpon - [UPDATE] audio/video の共通オプションを sora のオプションに移動する - - Momo 側ではコーデックやビットレートは指定できない - - p2p の場合は HTML で sdp を切り替えている - - --audio-codec - - --audio-bitrate - - --video-codec - - --video-bitrate - - @melpon + - Momo 側ではコーデックやビットレートは指定できない + - p2p の場合は HTML で sdp を切り替えている + - --audio-codec + - --audio-bitrate + - --video-codec + - --video-bitrate + - @melpon - [UPDATE] WebRTC Signaling Server Ayame 19.07.0 に追従する - - @kdxu + - @kdxu - [ADD] WebRTC Signaling Server Ayame に対応しました - - https://github.com/OpenAyame/ayame - - @kdxu + - https://github.com/OpenAyame/ayame + - @kdxu - [ADD] Circle CI で Linux 版を毎日 22:00 に自動ビルドする - - @voluntas + - @voluntas - [ADD] Circle CI で macOS 版を毎週日曜日 22:00 に自動ビルドする - - @voluntas + - @voluntas - [FIX] macOS でデバイスがつかめなくなっていたのを修正する - - ただし --fixed-resolution 必須 - - @tnoho + - ただし --fixed-resolution 必須 + - @tnoho - [FIX] ROS 対応がビルドできなくなっていたのを修正する - - @tnoho + - @tnoho ## 19.02.0 @@ -816,7 +860,7 @@ - [FIX] カメラがない環境で起動させるとセグフォが起きるのを修正する - [FIX] ARM ROS 版で H.264 配信の場合はハードウェアエンコーダを使用するように修正する - [CHANGE] ROS Audio に対応する - - 別ノードから送られてきたオーディオを使用するように変更 + - 別ノードから送られてきたオーディオを使用するように変更 - [UPDATE] 利用している libwebrtc のライブラリを M73 にする ## 19.01.0 @@ -857,7 +901,7 @@ ## 18.10.0-rc4 - [ADD] 4K の配信にに対応する - - armv6, armv7 にも対応はしているが、現時点で Raspberry Pi で配信はマシンパワー不足のためできない + - armv6, armv7 にも対応はしているが、現時点で Raspberry Pi で配信はマシンパワー不足のためできない ## 18.10.0-rc3 @@ -867,8 +911,8 @@ ## 18.10.0-rc2 - [CHANGE] libwebrtc が 4K に対応していないため解像度指定から 4K を削除する - - 将来的に対応していく予定 - - https://github.com/shiguredo/momo/issues/21 + - 将来的に対応していく予定 + - https://github.com/shiguredo/momo/issues/21 - [FIX] P2P モードのサンプルで映像を有効にした場合、音声が正常に流れない問題を修正 ## 18.10.0-rc1 @@ -885,29 +929,29 @@ - Momo を Apache License 2.0 でオープンソース化 - libwebrtc M70 対応 - Ubuntu 18.04 x86_64 対応 - - Ubuntu 18.04 x86_64 向けのバイナリの提供 + - Ubuntu 18.04 x86_64 向けのバイナリの提供 - Ubuntu 16.04 ARMv8 対応 - - Ubuntu 16.04 ARMv8 向けのバイナリの提供 - - PINE64 の Rock64 対応 + - Ubuntu 16.04 ARMv8 向けのバイナリの提供 + - PINE64 の Rock64 対応 - Raspberry Pi 3 B/B+ 対応 - - Raspberry Pi 3 B/B+ 向け ARMv7 バイナリの提供 - - Raspberry Pi 3 B/B+ 向け H.264 HWA 対応 + - Raspberry Pi 3 B/B+ 向け ARMv7 バイナリの提供 + - Raspberry Pi 3 B/B+ 向け H.264 HWA 対応 - Raspberry Pi Zero W/WH 対応 - - Raspberry Pi Zero W/WH 向け ARMv6 バイナリの提供 - - Raspberry Pi Zero W/WH 向け H.264 HWA 対応 + - Raspberry Pi Zero W/WH 向け ARMv6 バイナリの提供 + - Raspberry Pi Zero W/WH 向け H.264 HWA 対応 - 解像度指定オプション - - QVGA、VGA、HD、FHD、4K + - QVGA、VGA、HD、FHD、4K - フレームレート指定オプション - - 1-60 + - 1-60 - 優先オプション - - この機能は実験的機能です - - フレームレートか解像度のどちらを優先するか指定可能 + - この機能は実験的機能です + - フレームレートか解像度のどちらを優先するか指定可能 - ビデオを利用しないオプション - オーディオを利用しないオプション - ビデオコーデック指定オプション - オーディオコーデック指定オプション - デーモン化オプション - - Systemd の利用をおすすめします + - Systemd の利用をおすすめします - ログレベル - P2P 機能 - WebRTC SFU Sora 18.04.12 対応 diff --git a/CMakeLists.txt b/CMakeLists.txt index 37789f76..e7b18e64 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -117,7 +117,12 @@ elseif(MOMO_PACKAGE_NAME STREQUAL "raspberry-pi-os_armv8") set(TARGET_OS_LINUX "raspberry-pi-os") set(TARGET_ARCH "arm") set(TARGET_ARCH_ARM "armv8") + set(USE_V4L2_ENCODER ON) + set(USE_H264 ON) + set(USE_SDL2 ON) + set(USE_LINUX_PULSE_AUDIO ON) set(BOOST_ROOT_DIR /root/boost) + set(SDL2_ROOT_DIR /root/SDL2) set(CLI11_ROOT_DIR /root/CLI11) set(WEBRTC_INCLUDE_DIR /root/webrtc/include) set(WEBRTC_LIBRARY_DIR /root/webrtc/lib) @@ -127,31 +132,35 @@ elseif(MOMO_PACKAGE_NAME STREQUAL "raspberry-pi-os_armv8") set(USE_LIBCXX ON) set(LIBCXX_INCLUDE_DIR /root/llvm/libcxx/include) -elseif(MOMO_PACKAGE_NAME STREQUAL "ubuntu-18.04_armv8_jetson_nano") +elseif(MOMO_PACKAGE_NAME STREQUAL "ubuntu-20.04_x86_64") set(TARGET_OS "linux") - set(TARGET_OS_LINUX "ubuntu-18.04") - set(TARGET_ARCH "arm") - set(TARGET_ARCH_ARM "armv8") - set(USE_JETSON_ENCODER ON) + set(TARGET_OS_LINUX "ubuntu-20.04") + set(TARGET_ARCH "x86_64") set(USE_H264 ON) set(USE_SDL2 ON) - set(USE_LINUX_PULSE_AUDIO ON) + set(USE_NVCODEC_ENCODER ON) + set(USE_MSDK_ENCODER ON) + set(USE_SCREEN_CAPTURER ON) set(BOOST_ROOT_DIR /root/boost) set(CLI11_ROOT_DIR /root/CLI11) set(SDL2_ROOT_DIR /root/SDL2) + set(LIBVA_ROOT_DIR /root/libva) + set(MSDK_ROOT_DIR /root/msdk) set(WEBRTC_INCLUDE_DIR /root/webrtc/include) set(WEBRTC_LIBRARY_DIR /root/webrtc/lib) - set(CLANG_ROOT /root/llvm/clang) - set(SYSROOT /root/rootfs) + + # /root/llvm/clang にあるコンパイラは使わず、apt でインストールした clang-10 を利用する + set(CMAKE_C_COMPILER clang-10) + set(CMAKE_CXX_COMPILER clang++-10) set(USE_LIBCXX ON) set(LIBCXX_INCLUDE_DIR /root/llvm/libcxx/include) -elseif(MOMO_PACKAGE_NAME STREQUAL "ubuntu-18.04_armv8_jetson_xavier") +elseif(MOMO_PACKAGE_NAME STREQUAL "ubuntu-20.04_armv8_jetson_xavier") set(TARGET_OS "linux") - set(TARGET_OS_LINUX "ubuntu-18.04") + set(TARGET_OS_LINUX "ubuntu-20.04") set(TARGET_ARCH "arm") set(TARGET_ARCH_ARM "armv8") set(USE_JETSON_ENCODER ON) @@ -169,10 +178,10 @@ elseif(MOMO_PACKAGE_NAME STREQUAL "ubuntu-18.04_armv8_jetson_xavier") set(USE_LIBCXX ON) set(LIBCXX_INCLUDE_DIR /root/llvm/libcxx/include) -elseif(MOMO_PACKAGE_NAME STREQUAL "ubuntu-20.04_x86_64") +elseif(MOMO_PACKAGE_NAME STREQUAL "ubuntu-22.04_x86_64") set(TARGET_OS "linux") - set(TARGET_OS_LINUX "ubuntu-20.04") + set(TARGET_OS_LINUX "ubuntu-22.04") set(TARGET_ARCH "x86_64") set(USE_H264 ON) set(USE_SDL2 ON) @@ -187,9 +196,9 @@ elseif(MOMO_PACKAGE_NAME STREQUAL "ubuntu-20.04_x86_64") set(WEBRTC_INCLUDE_DIR /root/webrtc/include) set(WEBRTC_LIBRARY_DIR /root/webrtc/lib) - # /root/llvm/clang にあるコンパイラは使わず、apt でインストールした clang-10 を利用する - set(CMAKE_C_COMPILER clang-10) - set(CMAKE_CXX_COMPILER clang++-10) + # /root/llvm/clang にあるコンパイラは使わず、apt でインストールした clang-12 を利用する + set(CMAKE_C_COMPILER clang-12) + set(CMAKE_CXX_COMPILER clang++-12) set(USE_LIBCXX ON) set(LIBCXX_INCLUDE_DIR /root/llvm/libcxx/include) @@ -285,6 +294,7 @@ target_sources(momo src/p2p/p2p_server.cpp src/p2p/p2p_session.cpp src/p2p/p2p_websocket_session.cpp + src/rtc/aligned_encoder_adapter.cpp src/rtc/device_video_capturer.cpp src/rtc/momo_video_decoder_factory.cpp src/rtc/momo_video_encoder_factory.cpp @@ -306,7 +316,11 @@ target_sources(momo target_include_directories(momo PRIVATE src) -set_target_properties(momo PROPERTIES CXX_STANDARD 17 C_STANDARD 99) +if (WIN32) + set_target_properties(momo PROPERTIES CXX_STANDARD 20 C_STANDARD 99) +else() + set_target_properties(momo PROPERTIES CXX_STANDARD 17 C_STANDARD 99) +endif() target_link_libraries(momo PRIVATE @@ -324,6 +338,7 @@ target_compile_definitions(momo OPENSSL_IS_BORINGSSL USE_NVCODEC_ENCODER=$ USE_MMAL_ENCODER=$ + USE_V4L2_ENCODER=$ USE_JETSON_ENCODER=$ USE_MSDK_ENCODER=$ USE_H264=$ @@ -441,6 +456,7 @@ if ("${TARGET_OS}" STREQUAL "windows") iphlpapi.lib msdmo.lib Secur32.lib + Shcore.lib wmcodecdspuuid.lib Dwmapi.lib ) @@ -569,6 +585,7 @@ elseif (TARGET_OS STREQUAL "linux") target_compile_definitions(momo PRIVATE WEBRTC_POSIX + WEBRTC_LINUX ) # Linux 系の場合はカスタムされた libc++ を使っているためオプション追加 target_compile_definitions(momo PRIVATE _LIBCPP_ABI_NAMESPACE=Cr _LIBCPP_ABI_VERSION=2 _LIBCPP_DISABLE_AVAILABILITY) @@ -612,6 +629,12 @@ elseif (TARGET_OS STREQUAL "linux") NvCodec/NvCodec /usr/local/cuda/include) + if (WIN32) + set(_CUDA_COMPILE_OPTIONS "-xcuda;--cuda-gpu-arch=sm_35;-std=gnu++20") + else() + set(_CUDA_COMPILE_OPTIONS "-xcuda;--cuda-gpu-arch=sm_35;-std=gnu++17") + endif() + # これらのソースは CUDA としてコンパイルする set_source_files_properties( src/cuda/cuda_context_cuda.cpp @@ -620,7 +643,7 @@ elseif (TARGET_OS STREQUAL "linux") NvCodec/NvCodec/NvDecoder/NvDecoder.cpp NvCodec/NvCodec/NvEncoder/NvEncoderCuda.cpp PROPERTIES - COMPILE_OPTIONS "-xcuda;--cuda-gpu-arch=sm_35;-std=gnu++14" + COMPILE_OPTIONS "${_CUDA_COMPILE_OPTIONS}" ) target_link_directories(momo PRIVATE /usr/local/cuda/lib64) @@ -661,6 +684,7 @@ elseif (TARGET_OS STREQUAL "linux") src/hwenc_jetson/jetson_v4l2_capturer.cpp src/hwenc_jetson/jetson_video_encoder.cpp src/hwenc_jetson/jetson_video_decoder.cpp + ${SYSROOT}/usr/src/jetson_multimedia_api/samples/common/classes/NvBufSurface.cpp ${SYSROOT}/usr/src/jetson_multimedia_api/samples/common/classes/NvBuffer.cpp ${SYSROOT}/usr/src/jetson_multimedia_api/samples/common/classes/NvElement.cpp ${SYSROOT}/usr/src/jetson_multimedia_api/samples/common/classes/NvElementProfiler.cpp @@ -669,7 +693,6 @@ elseif (TARGET_OS STREQUAL "linux") ${SYSROOT}/usr/src/jetson_multimedia_api/samples/common/classes/NvLogging.cpp ${SYSROOT}/usr/src/jetson_multimedia_api/samples/common/classes/NvV4l2Element.cpp ${SYSROOT}/usr/src/jetson_multimedia_api/samples/common/classes/NvV4l2ElementPlane.cpp - ${SYSROOT}/usr/src/jetson_multimedia_api/samples/common/classes/NvVideoConverter.cpp ${SYSROOT}/usr/src/jetson_multimedia_api/samples/common/classes/NvVideoEncoder.cpp ${SYSROOT}/usr/src/jetson_multimedia_api/samples/common/classes/NvVideoDecoder.cpp ) @@ -688,15 +711,46 @@ elseif (TARGET_OS STREQUAL "linux") nvv4l2 nvv4lconvert nvbuf_utils - nvbuf_fdmap - nvddk_vic - nvddk_2d_v2 + #nvbuf_fdmap + #nvddk_vic + #nvddk_2d_v2 nvjpeg - nvrm - nvrm_graphics - nvos + nvbufsurface + nvbufsurftransform + #nvrm + #nvrm_graphics + #nvos ) endif(USE_JETSON_ENCODER) + + if (USE_V4L2_ENCODER) + add_library(camerac SHARED) + target_include_directories(camerac PRIVATE ${SYSROOT}/usr/include/libcamera) + set_target_properties(camerac PROPERTIES CXX_STANDARD 17 C_STANDARD 99) + target_sources(camerac + PRIVATE + src/libcamerac/libcamerac.cpp + ) + target_link_libraries(camerac + PRIVATE + camera + camera-base + ) + + target_sources(momo + PRIVATE + src/hwenc_v4l2/libcamera_capturer.cpp + src/hwenc_v4l2/v4l2_buffers.cpp + src/hwenc_v4l2/v4l2_capturer.cpp + src/hwenc_v4l2/v4l2_converter.cpp + src/hwenc_v4l2/v4l2_h264_decoder.cpp + src/hwenc_v4l2/v4l2_h264_encoder.cpp + src/hwenc_v4l2/v4l2_native_buffer.cpp + src/hwenc_v4l2/v4l2_runner.cpp + ) + target_link_libraries(momo PRIVATE camerac) + set_target_properties(momo PROPERTIES BUILD_RPATH_USE_ORIGIN ON) + endif(USE_V4L2_ENCODER) else() # armv6, armv7 用 if (TARGET_ARCH_ARM STREQUAL "armv6") diff --git a/NvCodec/NvCodec/NvDecoder/NvDecoder.cpp b/NvCodec/NvCodec/NvDecoder/NvDecoder.cpp index c1689b8a..b6d31ddb 100644 --- a/NvCodec/NvCodec/NvDecoder/NvDecoder.cpp +++ b/NvCodec/NvCodec/NvDecoder/NvDecoder.cpp @@ -1,689 +1,889 @@ -/* -* Copyright 2017-2020 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ - -#include -#include -#include - -#include "NvDecoder.h" -#include "dyn/cuda.h" -#include "dyn/nvcuvid.h" - -#define START_TIMER auto start = std::chrono::high_resolution_clock::now(); -#define STOP_TIMER(print_message) std::cout << print_message << \ - std::chrono::duration_cast( \ - std::chrono::high_resolution_clock::now() - start).count() \ - << " ms " << std::endl; - -#define CUDA_DRVAPI_CALL( call ) \ - do \ - { \ - CUresult err__ = call; \ - if (err__ != CUDA_SUCCESS) \ - { \ - const char *szErrName = NULL; \ - dyn::cuGetErrorName(err__, &szErrName); \ - std::ostringstream errorLog; \ - errorLog << "CUDA driver API error " << szErrName ; \ - throw NVDECException::makeNVDECException(errorLog.str(), err__, __FUNCTION__, __FILE__, __LINE__); \ - } \ - } \ - while (0) - -static const char * GetVideoCodecString(cudaVideoCodec eCodec) { - static struct { - cudaVideoCodec eCodec; - const char *name; - } aCodecName [] = { - { cudaVideoCodec_MPEG1, "MPEG-1" }, - { cudaVideoCodec_MPEG2, "MPEG-2" }, - { cudaVideoCodec_MPEG4, "MPEG-4 (ASP)" }, - { cudaVideoCodec_VC1, "VC-1/WMV" }, - { cudaVideoCodec_H264, "AVC/H.264" }, - { cudaVideoCodec_JPEG, "M-JPEG" }, - { cudaVideoCodec_H264_SVC, "H.264/SVC" }, - { cudaVideoCodec_H264_MVC, "H.264/MVC" }, - { cudaVideoCodec_HEVC, "H.265/HEVC" }, - { cudaVideoCodec_VP8, "VP8" }, - { cudaVideoCodec_VP9, "VP9" }, - { cudaVideoCodec_NumCodecs, "Invalid" }, - { cudaVideoCodec_YUV420, "YUV 4:2:0" }, - { cudaVideoCodec_YV12, "YV12 4:2:0" }, - { cudaVideoCodec_NV12, "NV12 4:2:0" }, - { cudaVideoCodec_YUYV, "YUYV 4:2:2" }, - { cudaVideoCodec_UYVY, "UYVY 4:2:2" }, - }; - - if (eCodec >= 0 && eCodec <= cudaVideoCodec_NumCodecs) { - return aCodecName[eCodec].name; - } - for (int i = cudaVideoCodec_NumCodecs + 1; i < sizeof(aCodecName) / sizeof(aCodecName[0]); i++) { - if (eCodec == aCodecName[i].eCodec) { - return aCodecName[eCodec].name; - } - } - return "Unknown"; -} - -static const char * GetVideoChromaFormatString(cudaVideoChromaFormat eChromaFormat) { - static struct { - cudaVideoChromaFormat eChromaFormat; - const char *name; - } aChromaFormatName[] = { - { cudaVideoChromaFormat_Monochrome, "YUV 400 (Monochrome)" }, - { cudaVideoChromaFormat_420, "YUV 420" }, - { cudaVideoChromaFormat_422, "YUV 422" }, - { cudaVideoChromaFormat_444, "YUV 444" }, - }; - - if (eChromaFormat >= 0 && eChromaFormat < sizeof(aChromaFormatName) / sizeof(aChromaFormatName[0])) { - return aChromaFormatName[eChromaFormat].name; - } - return "Unknown"; -} - -static float GetChromaHeightFactor(cudaVideoSurfaceFormat eSurfaceFormat) -{ - float factor = 0.5; - switch (eSurfaceFormat) - { - case cudaVideoSurfaceFormat_NV12: - case cudaVideoSurfaceFormat_P016: - factor = 0.5; - break; - case cudaVideoSurfaceFormat_YUV444: - case cudaVideoSurfaceFormat_YUV444_16Bit: - factor = 1.0; - break; - } - - return factor; -} - -static int GetChromaPlaneCount(cudaVideoSurfaceFormat eSurfaceFormat) -{ - int numPlane = 1; - switch (eSurfaceFormat) - { - case cudaVideoSurfaceFormat_NV12: - case cudaVideoSurfaceFormat_P016: - numPlane = 1; - break; - case cudaVideoSurfaceFormat_YUV444: - case cudaVideoSurfaceFormat_YUV444_16Bit: - numPlane = 2; - break; - } - - return numPlane; -} - -/** -* @brief This function is used to get codec string from codec id -*/ -const char *NvDecoder::GetCodecString(cudaVideoCodec eCodec) -{ - return GetVideoCodecString(eCodec); -} - -/* Return value from HandleVideoSequence() are interpreted as : -* 0: fail, 1: succeeded, > 1: override dpb size of parser (set by CUVIDPARSERPARAMS::ulMaxNumDecodeSurfaces while creating parser) -*/ -int NvDecoder::HandleVideoSequence(CUVIDEOFORMAT *pVideoFormat) -{ - START_TIMER - m_videoInfo.str(""); - m_videoInfo.clear(); - m_videoInfo << "Video Input Information" << std::endl - << "\tCodec : " << GetVideoCodecString(pVideoFormat->codec) << std::endl - << "\tFrame rate : " << pVideoFormat->frame_rate.numerator << "/" << pVideoFormat->frame_rate.denominator - << " = " << 1.0 * pVideoFormat->frame_rate.numerator / pVideoFormat->frame_rate.denominator << " fps" << std::endl - << "\tSequence : " << (pVideoFormat->progressive_sequence ? "Progressive" : "Interlaced") << std::endl - << "\tCoded size : [" << pVideoFormat->coded_width << ", " << pVideoFormat->coded_height << "]" << std::endl - << "\tDisplay area : [" << pVideoFormat->display_area.left << ", " << pVideoFormat->display_area.top << ", " - << pVideoFormat->display_area.right << ", " << pVideoFormat->display_area.bottom << "]" << std::endl - << "\tChroma : " << GetVideoChromaFormatString(pVideoFormat->chroma_format) << std::endl - << "\tBit depth : " << pVideoFormat->bit_depth_luma_minus8 + 8 - ; - m_videoInfo << std::endl; - - int nDecodeSurface = pVideoFormat->min_num_decode_surfaces; - - CUVIDDECODECAPS decodecaps; - memset(&decodecaps, 0, sizeof(decodecaps)); - - decodecaps.eCodecType = pVideoFormat->codec; - decodecaps.eChromaFormat = pVideoFormat->chroma_format; - decodecaps.nBitDepthMinus8 = pVideoFormat->bit_depth_luma_minus8; - - CUDA_DRVAPI_CALL(dyn::cuCtxPushCurrent(m_cuContext)); - NVDEC_API_CALL(dyn::cuvidGetDecoderCaps(&decodecaps)); - CUDA_DRVAPI_CALL(dyn::cuCtxPopCurrent(nullptr)); - - if(!decodecaps.bIsSupported){ - NVDEC_THROW_ERROR("Codec not supported on this GPU", CUDA_ERROR_NOT_SUPPORTED); - return nDecodeSurface; - } - - if ((pVideoFormat->coded_width > decodecaps.nMaxWidth) || - (pVideoFormat->coded_height > decodecaps.nMaxHeight)){ - - std::ostringstream errorString; - errorString << std::endl - << "Resolution : " << pVideoFormat->coded_width << "x" << pVideoFormat->coded_height << std::endl - << "Max Supported (wxh) : " << decodecaps.nMaxWidth << "x" << decodecaps.nMaxHeight << std::endl - << "Resolution not supported on this GPU"; - - const std::string cErr = errorString.str(); - NVDEC_THROW_ERROR(cErr, CUDA_ERROR_NOT_SUPPORTED); - return nDecodeSurface; - } - - if ((pVideoFormat->coded_width>>4)*(pVideoFormat->coded_height>>4) > decodecaps.nMaxMBCount){ - - std::ostringstream errorString; - errorString << std::endl - << "MBCount : " << (pVideoFormat->coded_width >> 4)*(pVideoFormat->coded_height >> 4) << std::endl - << "Max Supported mbcnt : " << decodecaps.nMaxMBCount << std::endl - << "MBCount not supported on this GPU"; - - const std::string cErr = errorString.str(); - NVDEC_THROW_ERROR(cErr, CUDA_ERROR_NOT_SUPPORTED); - return nDecodeSurface; - } - - if (m_nWidth && m_nLumaHeight && m_nChromaHeight) { - - // cuvidCreateDecoder() has been called before, and now there's possible config change - return ReconfigureDecoder(pVideoFormat); - } - - // eCodec has been set in the constructor (for parser). Here it's set again for potential correction - m_eCodec = pVideoFormat->codec; - m_eChromaFormat = pVideoFormat->chroma_format; - m_nBitDepthMinus8 = pVideoFormat->bit_depth_luma_minus8; - m_nBPP = m_nBitDepthMinus8 > 0 ? 2 : 1; - - // Set the output surface format same as chroma format - if (m_eChromaFormat == cudaVideoChromaFormat_420) - m_eOutputFormat = pVideoFormat->bit_depth_luma_minus8 ? cudaVideoSurfaceFormat_P016 : cudaVideoSurfaceFormat_NV12; - else if (m_eChromaFormat == cudaVideoChromaFormat_444) - m_eOutputFormat = pVideoFormat->bit_depth_luma_minus8 ? cudaVideoSurfaceFormat_YUV444_16Bit : cudaVideoSurfaceFormat_YUV444; - else if (m_eChromaFormat == cudaVideoChromaFormat_422) - m_eOutputFormat = cudaVideoSurfaceFormat_NV12; // no 4:2:2 output format supported yet so make 420 default - - // Check if output format supported. If not, check falback options - if (!(decodecaps.nOutputFormatMask & (1 << m_eOutputFormat))) - { - if (decodecaps.nOutputFormatMask & (1 << cudaVideoSurfaceFormat_NV12)) - m_eOutputFormat = cudaVideoSurfaceFormat_NV12; - else if (decodecaps.nOutputFormatMask & (1 << cudaVideoSurfaceFormat_P016)) - m_eOutputFormat = cudaVideoSurfaceFormat_P016; - else if (decodecaps.nOutputFormatMask & (1 << cudaVideoSurfaceFormat_YUV444)) - m_eOutputFormat = cudaVideoSurfaceFormat_YUV444; - else if (decodecaps.nOutputFormatMask & (1 << cudaVideoSurfaceFormat_YUV444_16Bit)) - m_eOutputFormat = cudaVideoSurfaceFormat_YUV444_16Bit; - else - NVDEC_THROW_ERROR("No supported output format found", CUDA_ERROR_NOT_SUPPORTED); - } - m_videoFormat = *pVideoFormat; - - CUVIDDECODECREATEINFO videoDecodeCreateInfo = { 0 }; - videoDecodeCreateInfo.CodecType = pVideoFormat->codec; - videoDecodeCreateInfo.ChromaFormat = pVideoFormat->chroma_format; - videoDecodeCreateInfo.OutputFormat = m_eOutputFormat; - videoDecodeCreateInfo.bitDepthMinus8 = pVideoFormat->bit_depth_luma_minus8; - if (pVideoFormat->progressive_sequence) - videoDecodeCreateInfo.DeinterlaceMode = cudaVideoDeinterlaceMode_Weave; - else - videoDecodeCreateInfo.DeinterlaceMode = cudaVideoDeinterlaceMode_Adaptive; - videoDecodeCreateInfo.ulNumOutputSurfaces = 2; - // With PreferCUVID, JPEG is still decoded by CUDA while video is decoded by NVDEC hardware - videoDecodeCreateInfo.ulCreationFlags = cudaVideoCreate_PreferCUVID; - videoDecodeCreateInfo.ulNumDecodeSurfaces = nDecodeSurface; - videoDecodeCreateInfo.vidLock = m_ctxLock; - videoDecodeCreateInfo.ulWidth = pVideoFormat->coded_width; - videoDecodeCreateInfo.ulHeight = pVideoFormat->coded_height; - if (m_nMaxWidth < (int)pVideoFormat->coded_width) - m_nMaxWidth = pVideoFormat->coded_width; - if (m_nMaxHeight < (int)pVideoFormat->coded_height) - m_nMaxHeight = pVideoFormat->coded_height; - videoDecodeCreateInfo.ulMaxWidth = m_nMaxWidth; - videoDecodeCreateInfo.ulMaxHeight = m_nMaxHeight; - - if (!(m_cropRect.r && m_cropRect.b) && !(m_resizeDim.w && m_resizeDim.h)) { - m_nWidth = pVideoFormat->display_area.right - pVideoFormat->display_area.left; - m_nLumaHeight = pVideoFormat->display_area.bottom - pVideoFormat->display_area.top; - videoDecodeCreateInfo.ulTargetWidth = pVideoFormat->coded_width; - videoDecodeCreateInfo.ulTargetHeight = pVideoFormat->coded_height; - } else { - if (m_resizeDim.w && m_resizeDim.h) { - videoDecodeCreateInfo.display_area.left = pVideoFormat->display_area.left; - videoDecodeCreateInfo.display_area.top = pVideoFormat->display_area.top; - videoDecodeCreateInfo.display_area.right = pVideoFormat->display_area.right; - videoDecodeCreateInfo.display_area.bottom = pVideoFormat->display_area.bottom; - m_nWidth = m_resizeDim.w; - m_nLumaHeight = m_resizeDim.h; - } - - if (m_cropRect.r && m_cropRect.b) { - videoDecodeCreateInfo.display_area.left = m_cropRect.l; - videoDecodeCreateInfo.display_area.top = m_cropRect.t; - videoDecodeCreateInfo.display_area.right = m_cropRect.r; - videoDecodeCreateInfo.display_area.bottom = m_cropRect.b; - m_nWidth = m_cropRect.r - m_cropRect.l; - m_nLumaHeight = m_cropRect.b - m_cropRect.t; - } - videoDecodeCreateInfo.ulTargetWidth = m_nWidth; - videoDecodeCreateInfo.ulTargetHeight = m_nLumaHeight; - } - - m_nChromaHeight = (int)(m_nLumaHeight * GetChromaHeightFactor(m_eOutputFormat)); - m_nNumChromaPlanes = GetChromaPlaneCount(m_eOutputFormat); - m_nSurfaceHeight = videoDecodeCreateInfo.ulTargetHeight; - m_nSurfaceWidth = videoDecodeCreateInfo.ulTargetWidth; - m_displayRect.b = videoDecodeCreateInfo.display_area.bottom; - m_displayRect.t = videoDecodeCreateInfo.display_area.top; - m_displayRect.l = videoDecodeCreateInfo.display_area.left; - m_displayRect.r = videoDecodeCreateInfo.display_area.right; - - m_videoInfo << "Video Decoding Params:" << std::endl - << "\tNum Surfaces : " << videoDecodeCreateInfo.ulNumDecodeSurfaces << std::endl - << "\tCrop : [" << videoDecodeCreateInfo.display_area.left << ", " << videoDecodeCreateInfo.display_area.top << ", " - << videoDecodeCreateInfo.display_area.right << ", " << videoDecodeCreateInfo.display_area.bottom << "]" << std::endl - << "\tResize : " << videoDecodeCreateInfo.ulTargetWidth << "x" << videoDecodeCreateInfo.ulTargetHeight << std::endl - << "\tDeinterlace : " << std::vector{"Weave", "Bob", "Adaptive"}[videoDecodeCreateInfo.DeinterlaceMode] - ; - m_videoInfo << std::endl; - - CUDA_DRVAPI_CALL(dyn::cuCtxPushCurrent(m_cuContext)); - NVDEC_API_CALL(dyn::cuvidCreateDecoder(&m_hDecoder, &videoDecodeCreateInfo)); - CUDA_DRVAPI_CALL(dyn::cuCtxPopCurrent(nullptr)); - STOP_TIMER("Session Initialization Time: "); - return nDecodeSurface; -} - -int NvDecoder::ReconfigureDecoder(CUVIDEOFORMAT *pVideoFormat) -{ - if (pVideoFormat->bit_depth_luma_minus8 != m_videoFormat.bit_depth_luma_minus8 || pVideoFormat->bit_depth_chroma_minus8 != m_videoFormat.bit_depth_chroma_minus8){ - - NVDEC_THROW_ERROR("Reconfigure Not supported for bit depth change", CUDA_ERROR_NOT_SUPPORTED); - } - - if (pVideoFormat->chroma_format != m_videoFormat.chroma_format) { - - NVDEC_THROW_ERROR("Reconfigure Not supported for chroma format change", CUDA_ERROR_NOT_SUPPORTED); - } - - bool bDecodeResChange = !(pVideoFormat->coded_width == m_videoFormat.coded_width && pVideoFormat->coded_height == m_videoFormat.coded_height); - bool bDisplayRectChange = !(pVideoFormat->display_area.bottom == m_videoFormat.display_area.bottom && pVideoFormat->display_area.top == m_videoFormat.display_area.top \ - && pVideoFormat->display_area.left == m_videoFormat.display_area.left && pVideoFormat->display_area.right == m_videoFormat.display_area.right); - - int nDecodeSurface = pVideoFormat->min_num_decode_surfaces; - - if ((pVideoFormat->coded_width > m_nMaxWidth) || (pVideoFormat->coded_height > m_nMaxHeight)) { - // For VP9, let driver handle the change if new width/height > maxwidth/maxheight - if ((m_eCodec != cudaVideoCodec_VP9) || m_bReconfigExternal) - { - NVDEC_THROW_ERROR("Reconfigure Not supported when width/height > maxwidth/maxheight", CUDA_ERROR_NOT_SUPPORTED); - } - return 1; - } - - if (!bDecodeResChange && !m_bReconfigExtPPChange) { - // if the coded_width/coded_height hasn't changed but display resolution has changed, then need to update width/height for - // correct output without cropping. Example : 1920x1080 vs 1920x1088 - if (bDisplayRectChange) - { - m_nWidth = pVideoFormat->display_area.right - pVideoFormat->display_area.left; - m_nLumaHeight = pVideoFormat->display_area.bottom - pVideoFormat->display_area.top; - m_nChromaHeight = int(m_nLumaHeight * GetChromaHeightFactor(m_eOutputFormat)); - m_nNumChromaPlanes = GetChromaPlaneCount(m_eOutputFormat); - } - - // no need for reconfigureDecoder(). Just return - return 1; - } - - CUVIDRECONFIGUREDECODERINFO reconfigParams = { 0 }; - - reconfigParams.ulWidth = m_videoFormat.coded_width = pVideoFormat->coded_width; - reconfigParams.ulHeight = m_videoFormat.coded_height = pVideoFormat->coded_height; - - // Dont change display rect and get scaled output from decoder. This will help display app to present apps smoothly - reconfigParams.display_area.bottom = m_displayRect.b; - reconfigParams.display_area.top = m_displayRect.t; - reconfigParams.display_area.left = m_displayRect.l; - reconfigParams.display_area.right = m_displayRect.r; - reconfigParams.ulTargetWidth = m_nSurfaceWidth; - reconfigParams.ulTargetHeight = m_nSurfaceHeight; - - // If external reconfigure is called along with resolution change even if post processing params is not changed, - // do full reconfigure params update - if ((m_bReconfigExternal && bDecodeResChange) || m_bReconfigExtPPChange) { - // update display rect and target resolution if requested explicitely - m_bReconfigExternal = false; - m_bReconfigExtPPChange = false; - m_videoFormat = *pVideoFormat; - if (!(m_cropRect.r && m_cropRect.b) && !(m_resizeDim.w && m_resizeDim.h)) { - m_nWidth = pVideoFormat->display_area.right - pVideoFormat->display_area.left; - m_nLumaHeight = pVideoFormat->display_area.bottom - pVideoFormat->display_area.top; - reconfigParams.ulTargetWidth = pVideoFormat->coded_width; - reconfigParams.ulTargetHeight = pVideoFormat->coded_height; - } - else { - if (m_resizeDim.w && m_resizeDim.h) { - reconfigParams.display_area.left = pVideoFormat->display_area.left; - reconfigParams.display_area.top = pVideoFormat->display_area.top; - reconfigParams.display_area.right = pVideoFormat->display_area.right; - reconfigParams.display_area.bottom = pVideoFormat->display_area.bottom; - m_nWidth = m_resizeDim.w; - m_nLumaHeight = m_resizeDim.h; - } - - if (m_cropRect.r && m_cropRect.b) { - reconfigParams.display_area.left = m_cropRect.l; - reconfigParams.display_area.top = m_cropRect.t; - reconfigParams.display_area.right = m_cropRect.r; - reconfigParams.display_area.bottom = m_cropRect.b; - m_nWidth = m_cropRect.r - m_cropRect.l; - m_nLumaHeight = m_cropRect.b - m_cropRect.t; - } - reconfigParams.ulTargetWidth = m_nWidth; - reconfigParams.ulTargetHeight = m_nLumaHeight; - } - - m_nChromaHeight = int(m_nLumaHeight * GetChromaHeightFactor(m_eOutputFormat)); - m_nNumChromaPlanes = GetChromaPlaneCount(m_eOutputFormat); - m_nSurfaceHeight = reconfigParams.ulTargetHeight; - m_nSurfaceWidth = reconfigParams.ulTargetWidth; - m_displayRect.b = reconfigParams.display_area.bottom; - m_displayRect.t = reconfigParams.display_area.top; - m_displayRect.l = reconfigParams.display_area.left; - m_displayRect.r = reconfigParams.display_area.right; - } - - reconfigParams.ulNumDecodeSurfaces = nDecodeSurface; - - START_TIMER - CUDA_DRVAPI_CALL(dyn::cuCtxPushCurrent(m_cuContext)); - NVDEC_API_CALL(dyn::cuvidReconfigureDecoder(m_hDecoder, &reconfigParams)); - CUDA_DRVAPI_CALL(dyn::cuCtxPopCurrent(nullptr)); - STOP_TIMER("Session Reconfigure Time: "); - - return nDecodeSurface; -} - -int NvDecoder::setReconfigParams(const Rect *pCropRect, const Dim *pResizeDim) -{ - m_bReconfigExternal = true; - m_bReconfigExtPPChange = false; - if (pCropRect) - { - if (!((pCropRect->t == m_cropRect.t) && (pCropRect->l == m_cropRect.l) && - (pCropRect->b == m_cropRect.b) && (pCropRect->r == m_cropRect.r))) - { - m_bReconfigExtPPChange = true; - m_cropRect = *pCropRect; - } - } - if (pResizeDim) - { - if (!((pResizeDim->w == m_resizeDim.w) && (pResizeDim->h == m_resizeDim.h))) - { - m_bReconfigExtPPChange = true; - m_resizeDim = *pResizeDim; - } - } - - // Clear existing output buffers of different size - uint8_t *pFrame = NULL; - while (!m_vpFrame.empty()) - { - pFrame = m_vpFrame.back(); - m_vpFrame.pop_back(); - if (m_bUseDeviceFrame) - { - CUDA_DRVAPI_CALL(dyn::cuCtxPushCurrent(m_cuContext)); - CUDA_DRVAPI_CALL(dyn::cuMemFree((CUdeviceptr)pFrame)); - CUDA_DRVAPI_CALL(dyn::cuCtxPopCurrent(nullptr)); - } - else - { - delete pFrame; - } - } - - return 1; -} - -/* Return value from HandlePictureDecode() are interpreted as: -* 0: fail, >=1: succeeded -*/ -int NvDecoder::HandlePictureDecode(CUVIDPICPARAMS *pPicParams) { - if (!m_hDecoder) - { - NVDEC_THROW_ERROR("Decoder not initialized.", CUDA_ERROR_NOT_INITIALIZED); - return false; - } - m_nPicNumInDecodeOrder[pPicParams->CurrPicIdx] = m_nDecodePicCnt++; - NVDEC_API_CALL(dyn::cuvidDecodePicture(m_hDecoder, pPicParams)); - return 1; -} - -/* Return value from HandlePictureDisplay() are interpreted as: -* 0: fail, >=1: succeeded -*/ -int NvDecoder::HandlePictureDisplay(CUVIDPARSERDISPINFO *pDispInfo) { - CUVIDPROCPARAMS videoProcessingParameters = {}; - videoProcessingParameters.progressive_frame = pDispInfo->progressive_frame; - videoProcessingParameters.second_field = pDispInfo->repeat_first_field + 1; - videoProcessingParameters.top_field_first = pDispInfo->top_field_first; - videoProcessingParameters.unpaired_field = pDispInfo->repeat_first_field < 0; - videoProcessingParameters.output_stream = m_cuvidStream; - - CUdeviceptr dpSrcFrame = 0; - unsigned int nSrcPitch = 0; - NVDEC_API_CALL(dyn::cuvidMapVideoFrame(m_hDecoder, pDispInfo->picture_index, &dpSrcFrame, - &nSrcPitch, &videoProcessingParameters)); - - CUVIDGETDECODESTATUS DecodeStatus; - memset(&DecodeStatus, 0, sizeof(DecodeStatus)); - CUresult result = dyn::cuvidGetDecodeStatus(m_hDecoder, pDispInfo->picture_index, &DecodeStatus); - if (result == CUDA_SUCCESS && (DecodeStatus.decodeStatus == cuvidDecodeStatus_Error || DecodeStatus.decodeStatus == cuvidDecodeStatus_Error_Concealed)) - { - printf("Decode Error occurred for picture %d\n", m_nPicNumInDecodeOrder[pDispInfo->picture_index]); - } - - uint8_t *pDecodedFrame = nullptr; - { - std::lock_guard lock(m_mtxVPFrame); - if ((unsigned)++m_nDecodedFrame > m_vpFrame.size()) - { - // Not enough frames in stock - m_nFrameAlloc++; - uint8_t *pFrame = NULL; - if (m_bUseDeviceFrame) - { - CUDA_DRVAPI_CALL(dyn::cuCtxPushCurrent(m_cuContext)); - if (m_bDeviceFramePitched) - { - CUDA_DRVAPI_CALL(dyn::cuMemAllocPitch((CUdeviceptr *)&pFrame, &m_nDeviceFramePitch, m_nWidth * m_nBPP, m_nLumaHeight + (m_nChromaHeight * m_nNumChromaPlanes), 16)); - } - else - { - CUDA_DRVAPI_CALL(dyn::cuMemAlloc((CUdeviceptr *)&pFrame, GetFrameSize())); - } - CUDA_DRVAPI_CALL(dyn::cuCtxPopCurrent(nullptr)); - } - else - { - pFrame = new uint8_t[GetFrameSize()]; - } - m_vpFrame.push_back(pFrame); - } - pDecodedFrame = m_vpFrame[m_nDecodedFrame - 1]; - } - - CUDA_DRVAPI_CALL(dyn::cuCtxPushCurrent(m_cuContext)); - CUDA_MEMCPY2D m = { 0 }; - m.srcMemoryType = CU_MEMORYTYPE_DEVICE; - m.srcDevice = dpSrcFrame; - m.srcPitch = nSrcPitch; - m.dstMemoryType = m_bUseDeviceFrame ? CU_MEMORYTYPE_DEVICE : CU_MEMORYTYPE_HOST; - m.dstDevice = (CUdeviceptr)(m.dstHost = pDecodedFrame); - m.dstPitch = m_nDeviceFramePitch ? m_nDeviceFramePitch : m_nWidth * m_nBPP; - m.WidthInBytes = m_nWidth * m_nBPP; - m.Height = m_nLumaHeight; - CUDA_DRVAPI_CALL(dyn::cuMemcpy2DAsync(&m, m_cuvidStream)); - - m.srcDevice = (CUdeviceptr)((uint8_t *)dpSrcFrame + m.srcPitch * m_nSurfaceHeight); - m.dstDevice = (CUdeviceptr)(m.dstHost = pDecodedFrame + m.dstPitch * m_nLumaHeight); - m.Height = m_nChromaHeight; - CUDA_DRVAPI_CALL(dyn::cuMemcpy2DAsync(&m, m_cuvidStream)); - - if (m_nNumChromaPlanes == 2) - { - m.srcDevice = (CUdeviceptr)((uint8_t *)dpSrcFrame + m.srcPitch * m_nSurfaceHeight * 2); - m.dstDevice = (CUdeviceptr)(m.dstHost = pDecodedFrame + m.dstPitch * m_nLumaHeight * 2); - m.Height = m_nChromaHeight; - CUDA_DRVAPI_CALL(dyn::cuMemcpy2DAsync(&m, m_cuvidStream)); - } - CUDA_DRVAPI_CALL(dyn::cuStreamSynchronize(m_cuvidStream)); - CUDA_DRVAPI_CALL(dyn::cuCtxPopCurrent(nullptr)); - - if ((int)m_vTimestamp.size() < m_nDecodedFrame) { - m_vTimestamp.resize(m_vpFrame.size()); - } - m_vTimestamp[m_nDecodedFrame - 1] = pDispInfo->timestamp; - - NVDEC_API_CALL(dyn::cuvidUnmapVideoFrame(m_hDecoder, dpSrcFrame)); - return 1; -} - -NvDecoder::NvDecoder(CUcontext cuContext, bool bUseDeviceFrame, cudaVideoCodec eCodec, bool bLowLatency, - bool bDeviceFramePitched, const Rect *pCropRect, const Dim *pResizeDim, int maxWidth, int maxHeight, unsigned int clkRate) : - m_cuContext(cuContext), m_bUseDeviceFrame(bUseDeviceFrame), m_eCodec(eCodec), m_bDeviceFramePitched(bDeviceFramePitched), - m_nMaxWidth (maxWidth), m_nMaxHeight(maxHeight) -{ - if (pCropRect) m_cropRect = *pCropRect; - if (pResizeDim) m_resizeDim = *pResizeDim; - - NVDEC_API_CALL(dyn::cuvidCtxLockCreate(&m_ctxLock, cuContext)); - - CUVIDPARSERPARAMS videoParserParameters = {}; - videoParserParameters.CodecType = eCodec; - videoParserParameters.ulMaxNumDecodeSurfaces = 1; - videoParserParameters.ulClockRate = clkRate; - videoParserParameters.ulMaxDisplayDelay = bLowLatency ? 0 : 1; - videoParserParameters.pUserData = this; - videoParserParameters.pfnSequenceCallback = HandleVideoSequenceProc; - videoParserParameters.pfnDecodePicture = HandlePictureDecodeProc; - videoParserParameters.pfnDisplayPicture = HandlePictureDisplayProc; - NVDEC_API_CALL(dyn::cuvidCreateVideoParser(&m_hParser, &videoParserParameters)); -} - -NvDecoder::~NvDecoder() { - - START_TIMER - dyn::cuCtxPushCurrent(m_cuContext); - dyn::cuCtxPopCurrent(nullptr); - - if (m_hParser) { - dyn::cuvidDestroyVideoParser(m_hParser); - } - - if (m_hDecoder) { - dyn::cuvidDestroyDecoder(m_hDecoder); - } - - std::lock_guard lock(m_mtxVPFrame); - - for (uint8_t *pFrame : m_vpFrame) - { - if (m_bUseDeviceFrame) - { - dyn::cuCtxPushCurrent(m_cuContext); - dyn::cuMemFree((CUdeviceptr)pFrame); - dyn::cuCtxPopCurrent(nullptr); - } - else - { - delete[] pFrame; - } - } - dyn::cuvidCtxLockDestroy(m_ctxLock); - STOP_TIMER("Session Deinitialization Time: "); -} - -int NvDecoder::Decode(const uint8_t *pData, int nSize, int nFlags, int64_t nTimestamp) -{ - m_nDecodedFrame = 0; - m_nDecodedFrameReturned = 0; - CUVIDSOURCEDATAPACKET packet = { 0 }; - packet.payload = pData; - packet.payload_size = nSize; - packet.flags = nFlags | CUVID_PKT_TIMESTAMP; - packet.timestamp = nTimestamp; - if (!pData || nSize == 0) { - packet.flags |= CUVID_PKT_ENDOFSTREAM; - } - NVDEC_API_CALL(dyn::cuvidParseVideoData(m_hParser, &packet)); - m_cuvidStream = 0; - - return m_nDecodedFrame; -} - -uint8_t* NvDecoder::GetFrame(int64_t* pTimestamp) -{ - if (m_nDecodedFrame > 0) - { - std::lock_guard lock(m_mtxVPFrame); - m_nDecodedFrame--; - if (pTimestamp) - *pTimestamp = m_vTimestamp[m_nDecodedFrameReturned]; - return m_vpFrame[m_nDecodedFrameReturned++]; - } - - return NULL; -} - -uint8_t* NvDecoder::GetLockedFrame(int64_t* pTimestamp) -{ - uint8_t *pFrame; - uint64_t timestamp; - if (m_nDecodedFrame > 0) { - std::lock_guard lock(m_mtxVPFrame); - m_nDecodedFrame--; - pFrame = m_vpFrame[0]; - m_vpFrame.erase(m_vpFrame.begin(), m_vpFrame.begin() + 1); - - timestamp = m_vTimestamp[0]; - m_vTimestamp.erase(m_vTimestamp.begin(), m_vTimestamp.begin() + 1); - - if (pTimestamp) - *pTimestamp = timestamp; - - return pFrame; - } - - return NULL; -} - -void NvDecoder::UnlockFrame(uint8_t **pFrame) -{ - std::lock_guard lock(m_mtxVPFrame); - m_vpFrame.insert(m_vpFrame.end(), &pFrame[0], &pFrame[1]); - - // add a dummy entry for timestamp - uint64_t timestamp[2] = {0}; - m_vTimestamp.insert(m_vTimestamp.end(), ×tamp[0], ×tamp[1]); -} +/* +* Copyright 2017-2022 NVIDIA Corporation. All rights reserved. +* +* Please refer to the NVIDIA end user license agreement (EULA) associated +* with this source code for terms and conditions that govern your use of +* this software. Any use, reproduction, disclosure, or distribution of +* this software and related documentation outside the terms of the EULA +* is strictly prohibited. +* +*/ + +#include +#include +#include +#include + +#include "NvDecoder/NvDecoder.h" +#include "dyn/cuda.h" +#include "dyn/nvcuvid.h" + +#define START_TIMER auto start = std::chrono::high_resolution_clock::now(); + +#define STOP_TIMER(print_message) \ + int64_t elapsedTime = std::chrono::duration_cast( \ + std::chrono::high_resolution_clock::now() - start) \ + .count(); \ + std::cout << print_message << elapsedTime << " ms " << std::endl; + +#define CUDA_DRVAPI_CALL(call) \ + do { \ + CUresult err__ = call; \ + if (err__ != CUDA_SUCCESS) { \ + const char* szErrName = NULL; \ + dyn::cuGetErrorName(err__, &szErrName); \ + std::ostringstream errorLog; \ + errorLog << "CUDA driver API error " << szErrName; \ + throw NVDECException::makeNVDECException( \ + errorLog.str(), err__, __FUNCTION__, __FILE__, __LINE__); \ + } \ + } while (0) + +static const char* GetVideoCodecString(cudaVideoCodec eCodec) { + static struct { + cudaVideoCodec eCodec; + const char* name; + } aCodecName[] = { + {cudaVideoCodec_MPEG1, "MPEG-1"}, + {cudaVideoCodec_MPEG2, "MPEG-2"}, + {cudaVideoCodec_MPEG4, "MPEG-4 (ASP)"}, + {cudaVideoCodec_VC1, "VC-1/WMV"}, + {cudaVideoCodec_H264, "AVC/H.264"}, + {cudaVideoCodec_JPEG, "M-JPEG"}, + {cudaVideoCodec_H264_SVC, "H.264/SVC"}, + {cudaVideoCodec_H264_MVC, "H.264/MVC"}, + {cudaVideoCodec_HEVC, "H.265/HEVC"}, + {cudaVideoCodec_VP8, "VP8"}, + {cudaVideoCodec_VP9, "VP9"}, + {cudaVideoCodec_AV1, "AV1"}, + {cudaVideoCodec_NumCodecs, "Invalid"}, + {cudaVideoCodec_YUV420, "YUV 4:2:0"}, + {cudaVideoCodec_YV12, "YV12 4:2:0"}, + {cudaVideoCodec_NV12, "NV12 4:2:0"}, + {cudaVideoCodec_YUYV, "YUYV 4:2:2"}, + {cudaVideoCodec_UYVY, "UYVY 4:2:2"}, + }; + + if (eCodec >= 0 && eCodec <= cudaVideoCodec_NumCodecs) { + return aCodecName[eCodec].name; + } + for (int i = cudaVideoCodec_NumCodecs + 1; + i < sizeof(aCodecName) / sizeof(aCodecName[0]); i++) { + if (eCodec == aCodecName[i].eCodec) { + return aCodecName[eCodec].name; + } + } + return "Unknown"; +} + +static const char* GetVideoChromaFormatString( + cudaVideoChromaFormat eChromaFormat) { + static struct { + cudaVideoChromaFormat eChromaFormat; + const char* name; + } aChromaFormatName[] = { + {cudaVideoChromaFormat_Monochrome, "YUV 400 (Monochrome)"}, + {cudaVideoChromaFormat_420, "YUV 420"}, + {cudaVideoChromaFormat_422, "YUV 422"}, + {cudaVideoChromaFormat_444, "YUV 444"}, + }; + + if (eChromaFormat >= 0 && eChromaFormat < sizeof(aChromaFormatName) / + sizeof(aChromaFormatName[0])) { + return aChromaFormatName[eChromaFormat].name; + } + return "Unknown"; +} + +static float GetChromaHeightFactor(cudaVideoSurfaceFormat eSurfaceFormat) { + float factor = 0.5; + switch (eSurfaceFormat) { + case cudaVideoSurfaceFormat_NV12: + case cudaVideoSurfaceFormat_P016: + factor = 0.5; + break; + case cudaVideoSurfaceFormat_YUV444: + case cudaVideoSurfaceFormat_YUV444_16Bit: + factor = 1.0; + break; + } + + return factor; +} + +static int GetChromaPlaneCount(cudaVideoSurfaceFormat eSurfaceFormat) { + int numPlane = 1; + switch (eSurfaceFormat) { + case cudaVideoSurfaceFormat_NV12: + case cudaVideoSurfaceFormat_P016: + numPlane = 1; + break; + case cudaVideoSurfaceFormat_YUV444: + case cudaVideoSurfaceFormat_YUV444_16Bit: + numPlane = 2; + break; + } + + return numPlane; +} + +std::map NvDecoder::sessionOverHead = {{0, 0}, {1, 0}}; + +/** +* @brief This function is used to get codec string from codec id +*/ +const char* NvDecoder::GetCodecString(cudaVideoCodec eCodec) { + return GetVideoCodecString(eCodec); +} + +/* Called when the parser encounters sequence header for AV1 SVC content +* return value interpretation: +* < 0 : fail, >=0: succeeded (bit 0-9: currOperatingPoint, bit 10-10: bDispAllLayer, bit 11-30: reserved, must be set 0) +*/ +int NvDecoder::GetOperatingPoint(CUVIDOPERATINGPOINTINFO* pOPInfo) { + if (pOPInfo->codec == cudaVideoCodec_AV1) { + if (pOPInfo->av1.operating_points_cnt > 1) { + // clip has SVC enabled + if (m_nOperatingPoint >= pOPInfo->av1.operating_points_cnt) + m_nOperatingPoint = 0; + + printf("AV1 SVC clip: operating point count %d ", + pOPInfo->av1.operating_points_cnt); + printf("Selected operating point: %d, IDC 0x%x bOutputAllLayers %d\n", + m_nOperatingPoint, + pOPInfo->av1.operating_points_idc[m_nOperatingPoint], + m_bDispAllLayers); + return (m_nOperatingPoint | (m_bDispAllLayers << 10)); + } + } + return -1; +} + +/* Return value from HandleVideoSequence() are interpreted as : +* 0: fail, 1: succeeded, > 1: override dpb size of parser (set by CUVIDPARSERPARAMS::ulMaxNumDecodeSurfaces while creating parser) +*/ +int NvDecoder::HandleVideoSequence(CUVIDEOFORMAT* pVideoFormat) { + START_TIMER + m_videoInfo.str(""); + m_videoInfo.clear(); + m_videoInfo << "Video Input Information" << std::endl + << "\tCodec : " << GetVideoCodecString(pVideoFormat->codec) + << std::endl + << "\tFrame rate : " << pVideoFormat->frame_rate.numerator + << "/" << pVideoFormat->frame_rate.denominator << " = " + << 1.0 * pVideoFormat->frame_rate.numerator / + pVideoFormat->frame_rate.denominator + << " fps" << std::endl + << "\tSequence : " + << (pVideoFormat->progressive_sequence ? "Progressive" + : "Interlaced") + << std::endl + << "\tCoded size : [" << pVideoFormat->coded_width << ", " + << pVideoFormat->coded_height << "]" << std::endl + << "\tDisplay area : [" << pVideoFormat->display_area.left << ", " + << pVideoFormat->display_area.top << ", " + << pVideoFormat->display_area.right << ", " + << pVideoFormat->display_area.bottom << "]" << std::endl + << "\tChroma : " + << GetVideoChromaFormatString(pVideoFormat->chroma_format) + << std::endl + << "\tBit depth : " << pVideoFormat->bit_depth_luma_minus8 + 8; + m_videoInfo << std::endl; + + int nDecodeSurface = pVideoFormat->min_num_decode_surfaces; + + CUVIDDECODECAPS decodecaps; + memset(&decodecaps, 0, sizeof(decodecaps)); + + decodecaps.eCodecType = pVideoFormat->codec; + decodecaps.eChromaFormat = pVideoFormat->chroma_format; + decodecaps.nBitDepthMinus8 = pVideoFormat->bit_depth_luma_minus8; + + CUDA_DRVAPI_CALL(dyn::cuCtxPushCurrent(m_cuContext)); + NVDEC_API_CALL(dyn::cuvidGetDecoderCaps(&decodecaps)); + CUDA_DRVAPI_CALL(dyn::cuCtxPopCurrent(nullptr)); + + if (!decodecaps.bIsSupported) { + NVDEC_THROW_ERROR("Codec not supported on this GPU", + CUDA_ERROR_NOT_SUPPORTED); + return nDecodeSurface; + } + + if ((pVideoFormat->coded_width > decodecaps.nMaxWidth) || + (pVideoFormat->coded_height > decodecaps.nMaxHeight)) { + std::ostringstream errorString; + errorString << std::endl + << "Resolution : " << pVideoFormat->coded_width << "x" + << pVideoFormat->coded_height << std::endl + << "Max Supported (wxh) : " << decodecaps.nMaxWidth << "x" + << decodecaps.nMaxHeight << std::endl + << "Resolution not supported on this GPU"; + + const std::string cErr = errorString.str(); + NVDEC_THROW_ERROR(cErr, CUDA_ERROR_NOT_SUPPORTED); + return nDecodeSurface; + } + + if ((pVideoFormat->coded_width >> 4) * (pVideoFormat->coded_height >> 4) > + decodecaps.nMaxMBCount) { + std::ostringstream errorString; + errorString << std::endl + << "MBCount : " + << (pVideoFormat->coded_width >> 4) * + (pVideoFormat->coded_height >> 4) + << std::endl + << "Max Supported mbcnt : " << decodecaps.nMaxMBCount + << std::endl + << "MBCount not supported on this GPU"; + + const std::string cErr = errorString.str(); + NVDEC_THROW_ERROR(cErr, CUDA_ERROR_NOT_SUPPORTED); + return nDecodeSurface; + } + + if (m_nWidth && m_nLumaHeight && m_nChromaHeight) { + // cuvidCreateDecoder() has been called before, and now there's possible config change + return ReconfigureDecoder(pVideoFormat); + } + + // eCodec has been set in the constructor (for parser). Here it's set again for potential correction + m_eCodec = pVideoFormat->codec; + m_eChromaFormat = pVideoFormat->chroma_format; + m_nBitDepthMinus8 = pVideoFormat->bit_depth_luma_minus8; + m_nBPP = m_nBitDepthMinus8 > 0 ? 2 : 1; + + // Set the output surface format same as chroma format + if (m_eChromaFormat == cudaVideoChromaFormat_420 || + cudaVideoChromaFormat_Monochrome) + m_eOutputFormat = pVideoFormat->bit_depth_luma_minus8 + ? cudaVideoSurfaceFormat_P016 + : cudaVideoSurfaceFormat_NV12; + else if (m_eChromaFormat == cudaVideoChromaFormat_444) + m_eOutputFormat = pVideoFormat->bit_depth_luma_minus8 + ? cudaVideoSurfaceFormat_YUV444_16Bit + : cudaVideoSurfaceFormat_YUV444; + else if (m_eChromaFormat == cudaVideoChromaFormat_422) + m_eOutputFormat = + cudaVideoSurfaceFormat_NV12; // no 4:2:2 output format supported yet so make 420 default + + // Check if output format supported. If not, check falback options + if (!(decodecaps.nOutputFormatMask & (1 << m_eOutputFormat))) { + if (decodecaps.nOutputFormatMask & (1 << cudaVideoSurfaceFormat_NV12)) + m_eOutputFormat = cudaVideoSurfaceFormat_NV12; + else if (decodecaps.nOutputFormatMask & (1 << cudaVideoSurfaceFormat_P016)) + m_eOutputFormat = cudaVideoSurfaceFormat_P016; + else if (decodecaps.nOutputFormatMask & + (1 << cudaVideoSurfaceFormat_YUV444)) + m_eOutputFormat = cudaVideoSurfaceFormat_YUV444; + else if (decodecaps.nOutputFormatMask & + (1 << cudaVideoSurfaceFormat_YUV444_16Bit)) + m_eOutputFormat = cudaVideoSurfaceFormat_YUV444_16Bit; + else + NVDEC_THROW_ERROR("No supported output format found", + CUDA_ERROR_NOT_SUPPORTED); + } + m_videoFormat = *pVideoFormat; + + CUVIDDECODECREATEINFO videoDecodeCreateInfo = {0}; + videoDecodeCreateInfo.CodecType = pVideoFormat->codec; + videoDecodeCreateInfo.ChromaFormat = pVideoFormat->chroma_format; + videoDecodeCreateInfo.OutputFormat = m_eOutputFormat; + videoDecodeCreateInfo.bitDepthMinus8 = pVideoFormat->bit_depth_luma_minus8; + if (pVideoFormat->progressive_sequence) + videoDecodeCreateInfo.DeinterlaceMode = cudaVideoDeinterlaceMode_Weave; + else + videoDecodeCreateInfo.DeinterlaceMode = cudaVideoDeinterlaceMode_Adaptive; + videoDecodeCreateInfo.ulNumOutputSurfaces = 2; + // With PreferCUVID, JPEG is still decoded by CUDA while video is decoded by NVDEC hardware + videoDecodeCreateInfo.ulCreationFlags = cudaVideoCreate_PreferCUVID; + videoDecodeCreateInfo.ulNumDecodeSurfaces = nDecodeSurface; + videoDecodeCreateInfo.vidLock = m_ctxLock; + videoDecodeCreateInfo.ulWidth = pVideoFormat->coded_width; + videoDecodeCreateInfo.ulHeight = pVideoFormat->coded_height; + // AV1 has max width/height of sequence in sequence header + if (pVideoFormat->codec == cudaVideoCodec_AV1 && + pVideoFormat->seqhdr_data_length > 0) { + // dont overwrite if it is already set from cmdline or reconfig.txt + if (!(m_nMaxWidth > pVideoFormat->coded_width || + m_nMaxHeight > pVideoFormat->coded_height)) { + CUVIDEOFORMATEX* vidFormatEx = (CUVIDEOFORMATEX*)pVideoFormat; + m_nMaxWidth = vidFormatEx->av1.max_width; + m_nMaxHeight = vidFormatEx->av1.max_height; + } + } + if (m_nMaxWidth < (int)pVideoFormat->coded_width) + m_nMaxWidth = pVideoFormat->coded_width; + if (m_nMaxHeight < (int)pVideoFormat->coded_height) + m_nMaxHeight = pVideoFormat->coded_height; + videoDecodeCreateInfo.ulMaxWidth = m_nMaxWidth; + videoDecodeCreateInfo.ulMaxHeight = m_nMaxHeight; + + if (!(m_cropRect.r && m_cropRect.b) && !(m_resizeDim.w && m_resizeDim.h)) { + m_nWidth = + pVideoFormat->display_area.right - pVideoFormat->display_area.left; + m_nLumaHeight = + pVideoFormat->display_area.bottom - pVideoFormat->display_area.top; + videoDecodeCreateInfo.ulTargetWidth = pVideoFormat->coded_width; + videoDecodeCreateInfo.ulTargetHeight = pVideoFormat->coded_height; + } else { + if (m_resizeDim.w && m_resizeDim.h) { + videoDecodeCreateInfo.display_area.left = pVideoFormat->display_area.left; + videoDecodeCreateInfo.display_area.top = pVideoFormat->display_area.top; + videoDecodeCreateInfo.display_area.right = + pVideoFormat->display_area.right; + videoDecodeCreateInfo.display_area.bottom = + pVideoFormat->display_area.bottom; + m_nWidth = m_resizeDim.w; + m_nLumaHeight = m_resizeDim.h; + } + + if (m_cropRect.r && m_cropRect.b) { + videoDecodeCreateInfo.display_area.left = m_cropRect.l; + videoDecodeCreateInfo.display_area.top = m_cropRect.t; + videoDecodeCreateInfo.display_area.right = m_cropRect.r; + videoDecodeCreateInfo.display_area.bottom = m_cropRect.b; + m_nWidth = m_cropRect.r - m_cropRect.l; + m_nLumaHeight = m_cropRect.b - m_cropRect.t; + } + videoDecodeCreateInfo.ulTargetWidth = m_nWidth; + videoDecodeCreateInfo.ulTargetHeight = m_nLumaHeight; + } + + m_nChromaHeight = + (int)(ceil(m_nLumaHeight * GetChromaHeightFactor(m_eOutputFormat))); + m_nNumChromaPlanes = GetChromaPlaneCount(m_eOutputFormat); + m_nSurfaceHeight = videoDecodeCreateInfo.ulTargetHeight; + m_nSurfaceWidth = videoDecodeCreateInfo.ulTargetWidth; + m_displayRect.b = videoDecodeCreateInfo.display_area.bottom; + m_displayRect.t = videoDecodeCreateInfo.display_area.top; + m_displayRect.l = videoDecodeCreateInfo.display_area.left; + m_displayRect.r = videoDecodeCreateInfo.display_area.right; + + m_videoInfo << "Video Decoding Params:" << std::endl + << "\tNum Surfaces : " + << videoDecodeCreateInfo.ulNumDecodeSurfaces << std::endl + << "\tCrop : [" << videoDecodeCreateInfo.display_area.left + << ", " << videoDecodeCreateInfo.display_area.top << ", " + << videoDecodeCreateInfo.display_area.right << ", " + << videoDecodeCreateInfo.display_area.bottom << "]" << std::endl + << "\tResize : " << videoDecodeCreateInfo.ulTargetWidth + << "x" << videoDecodeCreateInfo.ulTargetHeight << std::endl + << "\tDeinterlace : " + << std::vector{ + "Weave", "Bob", + "Adaptive"}[videoDecodeCreateInfo.DeinterlaceMode]; + m_videoInfo << std::endl; + + CUDA_DRVAPI_CALL(dyn::cuCtxPushCurrent(m_cuContext)); + NVDEC_API_CALL(dyn::cuvidCreateDecoder(&m_hDecoder, &videoDecodeCreateInfo)); + CUDA_DRVAPI_CALL(dyn::cuCtxPopCurrent(nullptr)); + STOP_TIMER("Session Initialization Time: "); + NvDecoder::addDecoderSessionOverHead(getDecoderSessionID(), elapsedTime); + return nDecodeSurface; +} + +int NvDecoder::ReconfigureDecoder(CUVIDEOFORMAT* pVideoFormat) { + if (pVideoFormat->bit_depth_luma_minus8 != + m_videoFormat.bit_depth_luma_minus8 || + pVideoFormat->bit_depth_chroma_minus8 != + m_videoFormat.bit_depth_chroma_minus8) { + NVDEC_THROW_ERROR("Reconfigure Not supported for bit depth change", + CUDA_ERROR_NOT_SUPPORTED); + } + + if (pVideoFormat->chroma_format != m_videoFormat.chroma_format) { + NVDEC_THROW_ERROR("Reconfigure Not supported for chroma format change", + CUDA_ERROR_NOT_SUPPORTED); + } + + bool bDecodeResChange = + !(pVideoFormat->coded_width == m_videoFormat.coded_width && + pVideoFormat->coded_height == m_videoFormat.coded_height); + bool bDisplayRectChange = !( + pVideoFormat->display_area.bottom == m_videoFormat.display_area.bottom && + pVideoFormat->display_area.top == m_videoFormat.display_area.top && + pVideoFormat->display_area.left == m_videoFormat.display_area.left && + pVideoFormat->display_area.right == m_videoFormat.display_area.right); + + int nDecodeSurface = pVideoFormat->min_num_decode_surfaces; + + if ((pVideoFormat->coded_width > m_nMaxWidth) || + (pVideoFormat->coded_height > m_nMaxHeight)) { + // For VP9, let driver handle the change if new width/height > maxwidth/maxheight + if ((m_eCodec != cudaVideoCodec_VP9) || m_bReconfigExternal) { + NVDEC_THROW_ERROR( + "Reconfigure Not supported when width/height > maxwidth/maxheight", + CUDA_ERROR_NOT_SUPPORTED); + } + return 1; + } + + if (!bDecodeResChange && !m_bReconfigExtPPChange) { + // if the coded_width/coded_height hasn't changed but display resolution has changed, then need to update width/height for + // correct output without cropping. Example : 1920x1080 vs 1920x1088 + if (bDisplayRectChange) { + m_nWidth = + pVideoFormat->display_area.right - pVideoFormat->display_area.left; + m_nLumaHeight = + pVideoFormat->display_area.bottom - pVideoFormat->display_area.top; + m_nChromaHeight = + (int)ceil(m_nLumaHeight * GetChromaHeightFactor(m_eOutputFormat)); + m_nNumChromaPlanes = GetChromaPlaneCount(m_eOutputFormat); + } + + // no need for reconfigureDecoder(). Just return + return 1; + } + + CUVIDRECONFIGUREDECODERINFO reconfigParams = {0}; + + reconfigParams.ulWidth = m_videoFormat.coded_width = + pVideoFormat->coded_width; + reconfigParams.ulHeight = m_videoFormat.coded_height = + pVideoFormat->coded_height; + + // Dont change display rect and get scaled output from decoder. This will help display app to present apps smoothly + reconfigParams.display_area.bottom = m_displayRect.b; + reconfigParams.display_area.top = m_displayRect.t; + reconfigParams.display_area.left = m_displayRect.l; + reconfigParams.display_area.right = m_displayRect.r; + reconfigParams.ulTargetWidth = m_nSurfaceWidth; + reconfigParams.ulTargetHeight = m_nSurfaceHeight; + + // If external reconfigure is called along with resolution change even if post processing params is not changed, + // do full reconfigure params update + if ((m_bReconfigExternal && bDecodeResChange) || m_bReconfigExtPPChange) { + // update display rect and target resolution if requested explicitely + m_bReconfigExternal = false; + m_bReconfigExtPPChange = false; + m_videoFormat = *pVideoFormat; + if (!(m_cropRect.r && m_cropRect.b) && !(m_resizeDim.w && m_resizeDim.h)) { + m_nWidth = + pVideoFormat->display_area.right - pVideoFormat->display_area.left; + m_nLumaHeight = + pVideoFormat->display_area.bottom - pVideoFormat->display_area.top; + reconfigParams.ulTargetWidth = pVideoFormat->coded_width; + reconfigParams.ulTargetHeight = pVideoFormat->coded_height; + } else { + if (m_resizeDim.w && m_resizeDim.h) { + reconfigParams.display_area.left = pVideoFormat->display_area.left; + reconfigParams.display_area.top = pVideoFormat->display_area.top; + reconfigParams.display_area.right = pVideoFormat->display_area.right; + reconfigParams.display_area.bottom = pVideoFormat->display_area.bottom; + m_nWidth = m_resizeDim.w; + m_nLumaHeight = m_resizeDim.h; + } + + if (m_cropRect.r && m_cropRect.b) { + reconfigParams.display_area.left = m_cropRect.l; + reconfigParams.display_area.top = m_cropRect.t; + reconfigParams.display_area.right = m_cropRect.r; + reconfigParams.display_area.bottom = m_cropRect.b; + m_nWidth = m_cropRect.r - m_cropRect.l; + m_nLumaHeight = m_cropRect.b - m_cropRect.t; + } + reconfigParams.ulTargetWidth = m_nWidth; + reconfigParams.ulTargetHeight = m_nLumaHeight; + } + + m_nChromaHeight = + (int)ceil(m_nLumaHeight * GetChromaHeightFactor(m_eOutputFormat)); + m_nNumChromaPlanes = GetChromaPlaneCount(m_eOutputFormat); + m_nSurfaceHeight = reconfigParams.ulTargetHeight; + m_nSurfaceWidth = reconfigParams.ulTargetWidth; + m_displayRect.b = reconfigParams.display_area.bottom; + m_displayRect.t = reconfigParams.display_area.top; + m_displayRect.l = reconfigParams.display_area.left; + m_displayRect.r = reconfigParams.display_area.right; + } + + reconfigParams.ulNumDecodeSurfaces = nDecodeSurface; + + START_TIMER + CUDA_DRVAPI_CALL(dyn::cuCtxPushCurrent(m_cuContext)); + NVDEC_API_CALL(dyn::cuvidReconfigureDecoder(m_hDecoder, &reconfigParams)); + CUDA_DRVAPI_CALL(dyn::cuCtxPopCurrent(nullptr)); + STOP_TIMER("Session Reconfigure Time: "); + + return nDecodeSurface; +} + +int NvDecoder::setReconfigParams(const Rect* pCropRect, const Dim* pResizeDim) { + m_bReconfigExternal = true; + m_bReconfigExtPPChange = false; + if (pCropRect) { + if (!((pCropRect->t == m_cropRect.t) && (pCropRect->l == m_cropRect.l) && + (pCropRect->b == m_cropRect.b) && (pCropRect->r == m_cropRect.r))) { + m_bReconfigExtPPChange = true; + m_cropRect = *pCropRect; + } + } + if (pResizeDim) { + if (!((pResizeDim->w == m_resizeDim.w) && + (pResizeDim->h == m_resizeDim.h))) { + m_bReconfigExtPPChange = true; + m_resizeDim = *pResizeDim; + } + } + + // Clear existing output buffers of different size + uint8_t* pFrame = NULL; + while (!m_vpFrame.empty()) { + pFrame = m_vpFrame.back(); + m_vpFrame.pop_back(); + if (m_bUseDeviceFrame) { + CUDA_DRVAPI_CALL(dyn::cuCtxPushCurrent(m_cuContext)); + CUDA_DRVAPI_CALL(dyn::cuMemFree((CUdeviceptr)pFrame)); + CUDA_DRVAPI_CALL(dyn::cuCtxPopCurrent(nullptr)); + } else { + delete pFrame; + } + } + + return 1; +} + +/* Return value from HandlePictureDecode() are interpreted as: +* 0: fail, >=1: succeeded +*/ +int NvDecoder::HandlePictureDecode(CUVIDPICPARAMS* pPicParams) { + if (!m_hDecoder) { + NVDEC_THROW_ERROR("Decoder not initialized.", CUDA_ERROR_NOT_INITIALIZED); + return false; + } + m_nPicNumInDecodeOrder[pPicParams->CurrPicIdx] = m_nDecodePicCnt++; + CUDA_DRVAPI_CALL(dyn::cuCtxPushCurrent(m_cuContext)); + NVDEC_API_CALL(dyn::cuvidDecodePicture(m_hDecoder, pPicParams)); + if (m_bForce_zero_latency && + ((!pPicParams->field_pic_flag) || (pPicParams->second_field))) { + CUVIDPARSERDISPINFO dispInfo; + memset(&dispInfo, 0, sizeof(dispInfo)); + dispInfo.picture_index = pPicParams->CurrPicIdx; + dispInfo.progressive_frame = !pPicParams->field_pic_flag; + dispInfo.top_field_first = pPicParams->bottom_field_flag ^ 1; + HandlePictureDisplay(&dispInfo); + } + CUDA_DRVAPI_CALL(dyn::cuCtxPopCurrent(nullptr)); + return 1; +} + +/* Return value from HandlePictureDisplay() are interpreted as: +* 0: fail, >=1: succeeded +*/ +int NvDecoder::HandlePictureDisplay(CUVIDPARSERDISPINFO* pDispInfo) { + CUVIDPROCPARAMS videoProcessingParameters = {}; + videoProcessingParameters.progressive_frame = pDispInfo->progressive_frame; + videoProcessingParameters.second_field = pDispInfo->repeat_first_field + 1; + videoProcessingParameters.top_field_first = pDispInfo->top_field_first; + videoProcessingParameters.unpaired_field = pDispInfo->repeat_first_field < 0; + videoProcessingParameters.output_stream = m_cuvidStream; + + if (m_bExtractSEIMessage) { + if (m_SEIMessagesDisplayOrder[pDispInfo->picture_index].pSEIData) { + // Write SEI Message + uint8_t* seiBuffer = + (uint8_t*)(m_SEIMessagesDisplayOrder[pDispInfo->picture_index] + .pSEIData); + uint32_t seiNumMessages = + m_SEIMessagesDisplayOrder[pDispInfo->picture_index].sei_message_count; + CUSEIMESSAGE* seiMessagesInfo = + m_SEIMessagesDisplayOrder[pDispInfo->picture_index].pSEIMessage; + if (m_fpSEI) { + for (uint32_t i = 0; i < seiNumMessages; i++) { + if (m_eCodec == cudaVideoCodec_H264 || cudaVideoCodec_H264_SVC || + cudaVideoCodec_H264_MVC || cudaVideoCodec_HEVC) { + switch (seiMessagesInfo[i].sei_message_type) { + case SEI_TYPE_TIME_CODE: { + HEVCSEITIMECODE* timecode = (HEVCSEITIMECODE*)seiBuffer; + fwrite(timecode, sizeof(HEVCSEITIMECODE), 1, m_fpSEI); + } break; + case SEI_TYPE_USER_DATA_UNREGISTERED: { + fwrite(seiBuffer, seiMessagesInfo[i].sei_message_size, 1, + m_fpSEI); + } break; + } + } + if (m_eCodec == cudaVideoCodec_AV1) { + fwrite(seiBuffer, seiMessagesInfo[i].sei_message_size, 1, m_fpSEI); + } + seiBuffer += seiMessagesInfo[i].sei_message_size; + } + } + free(m_SEIMessagesDisplayOrder[pDispInfo->picture_index].pSEIData); + free(m_SEIMessagesDisplayOrder[pDispInfo->picture_index].pSEIMessage); + } + } + + CUdeviceptr dpSrcFrame = 0; + unsigned int nSrcPitch = 0; + CUDA_DRVAPI_CALL(dyn::cuCtxPushCurrent(m_cuContext)); + NVDEC_API_CALL(dyn::cuvidMapVideoFrame(m_hDecoder, pDispInfo->picture_index, + &dpSrcFrame, &nSrcPitch, + &videoProcessingParameters)); + + CUVIDGETDECODESTATUS DecodeStatus; + memset(&DecodeStatus, 0, sizeof(DecodeStatus)); + CUresult result = dyn::cuvidGetDecodeStatus( + m_hDecoder, pDispInfo->picture_index, &DecodeStatus); + if (result == CUDA_SUCCESS && + (DecodeStatus.decodeStatus == cuvidDecodeStatus_Error || + DecodeStatus.decodeStatus == cuvidDecodeStatus_Error_Concealed)) { + printf("Decode Error occurred for picture %d\n", + m_nPicNumInDecodeOrder[pDispInfo->picture_index]); + } + + uint8_t* pDecodedFrame = nullptr; + { + std::lock_guard lock(m_mtxVPFrame); + if ((unsigned)++m_nDecodedFrame > m_vpFrame.size()) { + // Not enough frames in stock + m_nFrameAlloc++; + uint8_t* pFrame = NULL; + if (m_bUseDeviceFrame) { + if (m_bDeviceFramePitched) { + CUDA_DRVAPI_CALL(dyn::cuMemAllocPitch( + (CUdeviceptr*)&pFrame, &m_nDeviceFramePitch, GetWidth() * m_nBPP, + m_nLumaHeight + (m_nChromaHeight * m_nNumChromaPlanes), 16)); + } else { + CUDA_DRVAPI_CALL( + dyn::cuMemAlloc((CUdeviceptr*)&pFrame, GetFrameSize())); + } + } else { + pFrame = new uint8_t[GetFrameSize()]; + } + m_vpFrame.push_back(pFrame); + } + pDecodedFrame = m_vpFrame[m_nDecodedFrame - 1]; + } + + // Copy luma plane + CUDA_MEMCPY2D m = {0}; + m.srcMemoryType = CU_MEMORYTYPE_DEVICE; + m.srcDevice = dpSrcFrame; + m.srcPitch = nSrcPitch; + m.dstMemoryType = + m_bUseDeviceFrame ? CU_MEMORYTYPE_DEVICE : CU_MEMORYTYPE_HOST; + m.dstDevice = (CUdeviceptr)(m.dstHost = pDecodedFrame); + m.dstPitch = m_nDeviceFramePitch ? m_nDeviceFramePitch : GetWidth() * m_nBPP; + m.WidthInBytes = GetWidth() * m_nBPP; + m.Height = m_nLumaHeight; + CUDA_DRVAPI_CALL(dyn::cuMemcpy2DAsync(&m, m_cuvidStream)); + + // Copy chroma plane + // NVDEC output has luma height aligned by 2. Adjust chroma offset by aligning height + m.srcDevice = (CUdeviceptr)((uint8_t*)dpSrcFrame + + m.srcPitch * ((m_nSurfaceHeight + 1) & ~1)); + m.dstDevice = + (CUdeviceptr)(m.dstHost = pDecodedFrame + m.dstPitch * m_nLumaHeight); + m.Height = m_nChromaHeight; + CUDA_DRVAPI_CALL(dyn::cuMemcpy2DAsync(&m, m_cuvidStream)); + + if (m_nNumChromaPlanes == 2) { + m.srcDevice = (CUdeviceptr)((uint8_t*)dpSrcFrame + + m.srcPitch * ((m_nSurfaceHeight + 1) & ~1) * 2); + m.dstDevice = (CUdeviceptr)(m.dstHost = pDecodedFrame + + m.dstPitch * m_nLumaHeight * 2); + m.Height = m_nChromaHeight; + CUDA_DRVAPI_CALL(dyn::cuMemcpy2DAsync(&m, m_cuvidStream)); + } + CUDA_DRVAPI_CALL(dyn::cuStreamSynchronize(m_cuvidStream)); + CUDA_DRVAPI_CALL(dyn::cuCtxPopCurrent(nullptr)); + + if ((int)m_vTimestamp.size() < m_nDecodedFrame) { + m_vTimestamp.resize(m_vpFrame.size()); + } + m_vTimestamp[m_nDecodedFrame - 1] = pDispInfo->timestamp; + + NVDEC_API_CALL(dyn::cuvidUnmapVideoFrame(m_hDecoder, dpSrcFrame)); + return 1; +} + +int NvDecoder::GetSEIMessage(CUVIDSEIMESSAGEINFO* pSEIMessageInfo) { + uint32_t seiNumMessages = pSEIMessageInfo->sei_message_count; + CUSEIMESSAGE* seiMessagesInfo = pSEIMessageInfo->pSEIMessage; + size_t totalSEIBufferSize = 0; + if ((pSEIMessageInfo->picIdx < 0) || + (pSEIMessageInfo->picIdx >= MAX_FRM_CNT)) { + printf("Invalid picture index (%d)\n", pSEIMessageInfo->picIdx); + return 0; + } + for (uint32_t i = 0; i < seiNumMessages; i++) { + totalSEIBufferSize += seiMessagesInfo[i].sei_message_size; + } + if (!m_pCurrSEIMessage) { + printf("Out of Memory, Allocation failed for m_pCurrSEIMessage\n"); + return 0; + } + m_pCurrSEIMessage->pSEIData = malloc(totalSEIBufferSize); + if (!m_pCurrSEIMessage->pSEIData) { + printf("Out of Memory, Allocation failed for SEI Buffer\n"); + return 0; + } + memcpy(m_pCurrSEIMessage->pSEIData, pSEIMessageInfo->pSEIData, + totalSEIBufferSize); + m_pCurrSEIMessage->pSEIMessage = + (CUSEIMESSAGE*)malloc(sizeof(CUSEIMESSAGE) * seiNumMessages); + if (!m_pCurrSEIMessage->pSEIMessage) { + free(m_pCurrSEIMessage->pSEIData); + m_pCurrSEIMessage->pSEIData = NULL; + return 0; + } + memcpy(m_pCurrSEIMessage->pSEIMessage, pSEIMessageInfo->pSEIMessage, + sizeof(CUSEIMESSAGE) * seiNumMessages); + m_pCurrSEIMessage->sei_message_count = pSEIMessageInfo->sei_message_count; + m_SEIMessagesDisplayOrder[pSEIMessageInfo->picIdx] = *m_pCurrSEIMessage; + return 1; +} + +NvDecoder::NvDecoder(CUcontext cuContext, + bool bUseDeviceFrame, + cudaVideoCodec eCodec, + bool bLowLatency, + bool bDeviceFramePitched, + const Rect* pCropRect, + const Dim* pResizeDim, + bool extract_user_SEI_Message, + int maxWidth, + int maxHeight, + unsigned int clkRate, + bool force_zero_latency) + : m_cuContext(cuContext), + m_bUseDeviceFrame(bUseDeviceFrame), + m_eCodec(eCodec), + m_bDeviceFramePitched(bDeviceFramePitched), + m_bExtractSEIMessage(extract_user_SEI_Message), + m_nMaxWidth(maxWidth), + m_nMaxHeight(maxHeight), + m_bForce_zero_latency(force_zero_latency) { + if (pCropRect) + m_cropRect = *pCropRect; + if (pResizeDim) + m_resizeDim = *pResizeDim; + + NVDEC_API_CALL(dyn::cuvidCtxLockCreate(&m_ctxLock, cuContext)); + + // ck(dyn::cuStreamCreate(&m_cuvidStream, CU_STREAM_DEFAULT)); + + decoderSessionID = 0; + + if (m_bExtractSEIMessage) { + m_fpSEI = fopen("sei_message.txt", "wb"); + m_pCurrSEIMessage = new CUVIDSEIMESSAGEINFO; + memset(&m_SEIMessagesDisplayOrder, 0, sizeof(m_SEIMessagesDisplayOrder)); + } + CUVIDPARSERPARAMS videoParserParameters = {}; + videoParserParameters.CodecType = eCodec; + videoParserParameters.ulMaxNumDecodeSurfaces = 1; + videoParserParameters.ulClockRate = clkRate; + videoParserParameters.ulMaxDisplayDelay = bLowLatency ? 0 : 1; + videoParserParameters.pUserData = this; + videoParserParameters.pfnSequenceCallback = HandleVideoSequenceProc; + videoParserParameters.pfnDecodePicture = HandlePictureDecodeProc; + videoParserParameters.pfnDisplayPicture = + m_bForce_zero_latency ? NULL : HandlePictureDisplayProc; + videoParserParameters.pfnGetOperatingPoint = HandleOperatingPointProc; + videoParserParameters.pfnGetSEIMsg = + m_bExtractSEIMessage ? HandleSEIMessagesProc : NULL; + NVDEC_API_CALL( + dyn::cuvidCreateVideoParser(&m_hParser, &videoParserParameters)); +} + +NvDecoder::~NvDecoder() { + START_TIMER + + if (m_pCurrSEIMessage) { + delete m_pCurrSEIMessage; + m_pCurrSEIMessage = NULL; + } + + if (m_fpSEI) { + fclose(m_fpSEI); + m_fpSEI = NULL; + } + + if (m_hParser) { + dyn::cuvidDestroyVideoParser(m_hParser); + } + dyn::cuCtxPushCurrent(m_cuContext); + if (m_hDecoder) { + dyn::cuvidDestroyDecoder(m_hDecoder); + } + + std::lock_guard lock(m_mtxVPFrame); + + for (uint8_t* pFrame : m_vpFrame) { + if (m_bUseDeviceFrame) { + dyn::cuMemFree((CUdeviceptr)pFrame); + } else { + delete[] pFrame; + } + } + dyn::cuCtxPopCurrent(nullptr); + + dyn::cuvidCtxLockDestroy(m_ctxLock); + + STOP_TIMER("Session Deinitialization Time: "); + + NvDecoder::addDecoderSessionOverHead(getDecoderSessionID(), elapsedTime); +} + +int NvDecoder::Decode(const uint8_t* pData, + int nSize, + int nFlags, + int64_t nTimestamp) { + m_nDecodedFrame = 0; + m_nDecodedFrameReturned = 0; + CUVIDSOURCEDATAPACKET packet = {0}; + packet.payload = pData; + packet.payload_size = nSize; + packet.flags = nFlags | CUVID_PKT_TIMESTAMP; + packet.timestamp = nTimestamp; + if (!pData || nSize == 0) { + packet.flags |= CUVID_PKT_ENDOFSTREAM; + } + NVDEC_API_CALL(dyn::cuvidParseVideoData(m_hParser, &packet)); + + return m_nDecodedFrame; +} + +uint8_t* NvDecoder::GetFrame(int64_t* pTimestamp) { + if (m_nDecodedFrame > 0) { + std::lock_guard lock(m_mtxVPFrame); + m_nDecodedFrame--; + if (pTimestamp) + *pTimestamp = m_vTimestamp[m_nDecodedFrameReturned]; + return m_vpFrame[m_nDecodedFrameReturned++]; + } + + return NULL; +} + +uint8_t* NvDecoder::GetLockedFrame(int64_t* pTimestamp) { + uint8_t* pFrame; + uint64_t timestamp; + if (m_nDecodedFrame > 0) { + std::lock_guard lock(m_mtxVPFrame); + m_nDecodedFrame--; + pFrame = m_vpFrame[0]; + m_vpFrame.erase(m_vpFrame.begin(), m_vpFrame.begin() + 1); + + timestamp = m_vTimestamp[0]; + m_vTimestamp.erase(m_vTimestamp.begin(), m_vTimestamp.begin() + 1); + + if (pTimestamp) + *pTimestamp = timestamp; + + return pFrame; + } + + return NULL; +} + +void NvDecoder::UnlockFrame(uint8_t** pFrame) { + std::lock_guard lock(m_mtxVPFrame); + m_vpFrame.insert(m_vpFrame.end(), &pFrame[0], &pFrame[1]); + + // add a dummy entry for timestamp + uint64_t timestamp[2] = {0}; + m_vTimestamp.insert(m_vTimestamp.end(), ×tamp[0], ×tamp[1]); +} diff --git a/NvCodec/NvCodec/NvDecoder/NvDecoder.h b/NvCodec/NvCodec/NvDecoder/NvDecoder.h index b944cbc1..19f294b9 100644 --- a/NvCodec/NvCodec/NvDecoder/NvDecoder.h +++ b/NvCodec/NvCodec/NvDecoder/NvDecoder.h @@ -1,284 +1,437 @@ -/* -* Copyright 2017-2020 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ - -#pragma once - -#include -#include -#include -#include -#include -#include -#include -#include -#include "../Utils/NvCodecUtils.h" -#include "dyn/nvcuvid.h" - -/** -* @brief Exception class for error reporting from the decode API. -*/ -class NVDECException : public std::exception -{ -public: - NVDECException(const std::string& errorStr, const CUresult errorCode) - : m_errorString(errorStr), m_errorCode(errorCode) {} - - virtual ~NVDECException() throw() {} - virtual const char* what() const throw() { return m_errorString.c_str(); } - CUresult getErrorCode() const { return m_errorCode; } - const std::string& getErrorString() const { return m_errorString; } - static NVDECException makeNVDECException(const std::string& errorStr, const CUresult errorCode, - const std::string& functionName, const std::string& fileName, int lineNo); -private: - std::string m_errorString; - CUresult m_errorCode; -}; - -inline NVDECException NVDECException::makeNVDECException(const std::string& errorStr, const CUresult errorCode, const std::string& functionName, - const std::string& fileName, int lineNo) -{ - std::ostringstream errorLog; - errorLog << functionName << " : " << errorStr << " at " << fileName << ":" << lineNo << std::endl; - NVDECException exception(errorLog.str(), errorCode); - return exception; -} - -#define NVDEC_THROW_ERROR( errorStr, errorCode ) \ - do \ - { \ - throw NVDECException::makeNVDECException(errorStr, errorCode, __FUNCTION__, __FILE__, __LINE__); \ - } while (0) - - -#define NVDEC_API_CALL( cuvidAPI ) \ - do \ - { \ - CUresult errorCode = cuvidAPI; \ - if( errorCode != CUDA_SUCCESS) \ - { \ - std::ostringstream errorLog; \ - errorLog << #cuvidAPI << " returned error " << errorCode; \ - throw NVDECException::makeNVDECException(errorLog.str(), errorCode, __FUNCTION__, __FILE__, __LINE__); \ - } \ - } while (0) - -struct Rect { - int l, t, r, b; -}; - -struct Dim { - int w, h; -}; - -/** -* @brief Base class for decoder interface. -*/ -class NvDecoder { - -public: - /** - * @brief This function is used to initialize the decoder session. - * Application must call this function to initialize the decoder, before - * starting to decode any frames. - */ - NvDecoder(CUcontext cuContext, bool bUseDeviceFrame, cudaVideoCodec eCodec, bool bLowLatency = false, - bool bDeviceFramePitched = false, const Rect *pCropRect = NULL, const Dim *pResizeDim = NULL, - int maxWidth = 0, int maxHeight = 0, unsigned int clkRate = 1000); - ~NvDecoder(); - - /** - * @brief This function is used to get the current CUDA context. - */ - CUcontext GetContext() { return m_cuContext; } - - /** - * @brief This function is used to get the current decode width. - */ - int GetWidth() { assert(m_nWidth); return m_nWidth; } - - /** - * @brief This function is used to get the current decode height (Luma height). - */ - int GetHeight() { assert(m_nLumaHeight); return m_nLumaHeight; } - - /** - * @brief This function is used to get the current chroma height. - */ - int GetChromaHeight() { assert(m_nChromaHeight); return m_nChromaHeight; } - - /** - * @brief This function is used to get the number of chroma planes. - */ - int GetNumChromaPlanes() { assert(m_nNumChromaPlanes); return m_nNumChromaPlanes; } - - /** - * @brief This function is used to get the current frame size based on pixel format. - */ - int GetFrameSize() { assert(m_nWidth); return m_nWidth * (m_nLumaHeight + m_nChromaHeight * m_nNumChromaPlanes) * m_nBPP; } - - /** - * @brief This function is used to get the pitch of the device buffer holding the decoded frame. - */ - int GetDeviceFramePitch() { assert(m_nWidth); return m_nDeviceFramePitch ? (int)m_nDeviceFramePitch : m_nWidth * m_nBPP; } - - /** - * @brief This function is used to get the bit depth associated with the pixel format. - */ - int GetBitDepth() { assert(m_nWidth); return m_nBitDepthMinus8 + 8; } - - /** - * @brief This function is used to get the bytes used per pixel. - */ - int GetBPP() { assert(m_nWidth); return m_nBPP; } - - /** - * @brief This function is used to get the YUV chroma format - */ - cudaVideoSurfaceFormat GetOutputFormat() { return m_eOutputFormat; } - - /** - * @brief This function is used to get information about the video stream (codec, display parameters etc) - */ - CUVIDEOFORMAT GetVideoFormatInfo() { assert(m_nWidth); return m_videoFormat; } - - /** - * @brief This function is used to get codec string from codec id - */ - const char *GetCodecString(cudaVideoCodec eCodec); - - /** - * @brief This function is used to print information about the video stream - */ - std::string GetVideoInfo() const { return m_videoInfo.str(); } - - /** - * @brief This function decodes a frame and returns the number of frames that are available for - * display. All frames that are available for display should be read before making a subsequent decode call. - * @param pData - pointer to the data buffer that is to be decoded - * @param nSize - size of the data buffer in bytes - * @param nFlags - CUvideopacketflags for setting decode options - * @param nTimestamp - presentation timestamp - */ - int Decode(const uint8_t *pData, int nSize, int nFlags = 0, int64_t nTimestamp = 0); - - /** - * @brief This function returns a decoded frame and timestamp. This function should be called in a loop for - * fetching all the frames that are available for display. - */ - uint8_t* GetFrame(int64_t* pTimestamp = nullptr); - - - /** - * @brief This function decodes a frame and returns the locked frame buffers - * This makes the buffers available for use by the application without the buffers - * getting overwritten, even if subsequent decode calls are made. The frame buffers - * remain locked, until UnlockFrame() is called - */ - uint8_t* GetLockedFrame(int64_t* pTimestamp = nullptr); - - /** - * @brief This function unlocks the frame buffer and makes the frame buffers available for write again - * @param ppFrame - pointer to array of frames that are to be unlocked - * @param nFrame - number of frames to be unlocked - */ - void UnlockFrame(uint8_t **pFrame); - - /** - * @brief This function allow app to set decoder reconfig params - * @param pCropRect - cropping rectangle coordinates - * @param pResizeDim - width and height of resized output - */ - int setReconfigParams(const Rect * pCropRect, const Dim * pResizeDim); - - // start a timer - void startTimer() { m_stDecode_time.Start(); } - - // stop the timer - double stopTimer() { return m_stDecode_time.Stop(); } -private: - /** - * @brief Callback function to be registered for getting a callback when decoding of sequence starts - */ - static int CUDAAPI HandleVideoSequenceProc(void *pUserData, CUVIDEOFORMAT *pVideoFormat) { return ((NvDecoder *)pUserData)->HandleVideoSequence(pVideoFormat); } - - /** - * @brief Callback function to be registered for getting a callback when a decoded frame is ready to be decoded - */ - static int CUDAAPI HandlePictureDecodeProc(void *pUserData, CUVIDPICPARAMS *pPicParams) { return ((NvDecoder *)pUserData)->HandlePictureDecode(pPicParams); } - - /** - * @brief Callback function to be registered for getting a callback when a decoded frame is available for display - */ - static int CUDAAPI HandlePictureDisplayProc(void *pUserData, CUVIDPARSERDISPINFO *pDispInfo) { return ((NvDecoder *)pUserData)->HandlePictureDisplay(pDispInfo); } - - /** - * @brief This function gets called when a sequence is ready to be decoded. The function also gets called - when there is format change - */ - int HandleVideoSequence(CUVIDEOFORMAT *pVideoFormat); - - /** - * @brief This function gets called when a picture is ready to be decoded. cuvidDecodePicture is called from this function - * to decode the picture - */ - int HandlePictureDecode(CUVIDPICPARAMS *pPicParams); - - /** - * @brief This function gets called after a picture is decoded and available for display. Frames are fetched and stored in - internal buffer - */ - int HandlePictureDisplay(CUVIDPARSERDISPINFO *pDispInfo); - - /** - * @brief This function reconfigure decoder if there is a change in sequence params. - */ - int ReconfigureDecoder(CUVIDEOFORMAT *pVideoFormat); - -private: - CUcontext m_cuContext = NULL; - CUvideoctxlock m_ctxLock; - CUvideoparser m_hParser = NULL; - CUvideodecoder m_hDecoder = NULL; - bool m_bUseDeviceFrame; - // dimension of the output - unsigned int m_nWidth = 0, m_nLumaHeight = 0, m_nChromaHeight = 0; - unsigned int m_nNumChromaPlanes = 0; - // height of the mapped surface - int m_nSurfaceHeight = 0; - int m_nSurfaceWidth = 0; - cudaVideoCodec m_eCodec = cudaVideoCodec_NumCodecs; - cudaVideoChromaFormat m_eChromaFormat; - cudaVideoSurfaceFormat m_eOutputFormat; - int m_nBitDepthMinus8 = 0; - int m_nBPP = 1; - CUVIDEOFORMAT m_videoFormat = {}; - Rect m_displayRect = {}; - // stock of frames - std::vector m_vpFrame; - // timestamps of decoded frames - std::vector m_vTimestamp; - int m_nDecodedFrame = 0, m_nDecodedFrameReturned = 0; - int m_nDecodePicCnt = 0, m_nPicNumInDecodeOrder[32]; - bool m_bEndDecodeDone = false; - std::mutex m_mtxVPFrame; - int m_nFrameAlloc = 0; - CUstream m_cuvidStream = 0; - bool m_bDeviceFramePitched = false; - size_t m_nDeviceFramePitch = 0; - Rect m_cropRect = {}; - Dim m_resizeDim = {}; - - std::ostringstream m_videoInfo; - unsigned int m_nMaxWidth = 0, m_nMaxHeight = 0; - bool m_bReconfigExternal = false; - bool m_bReconfigExtPPChange = false; - StopWatch m_stDecode_time; -}; +/* +* Copyright 2017-2022 NVIDIA Corporation. All rights reserved. +* +* Please refer to the NVIDIA end user license agreement (EULA) associated +* with this source code for terms and conditions that govern your use of +* this software. Any use, reproduction, disclosure, or distribution of +* this software and related documentation outside the terms of the EULA +* is strictly prohibited. +* +*/ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "../Utils/NvCodecUtils.h" +#include "dyn/nvcuvid.h" + +#define MAX_FRM_CNT 32 + +typedef enum { + SEI_TYPE_TIME_CODE = 136, + SEI_TYPE_USER_DATA_UNREGISTERED = 5 +} SEI_H264_HEVC_PAYLOAD_TYPE; + +/** +* @brief Exception class for error reporting from the decode API. +*/ +class NVDECException : public std::exception { + public: + NVDECException(const std::string& errorStr, const CUresult errorCode) + : m_errorString(errorStr), m_errorCode(errorCode) {} + + virtual ~NVDECException() throw() {} + virtual const char* what() const throw() { return m_errorString.c_str(); } + CUresult getErrorCode() const { return m_errorCode; } + const std::string& getErrorString() const { return m_errorString; } + static NVDECException makeNVDECException(const std::string& errorStr, + const CUresult errorCode, + const std::string& functionName, + const std::string& fileName, + int lineNo); + + private: + std::string m_errorString; + CUresult m_errorCode; +}; + +inline NVDECException NVDECException::makeNVDECException( + const std::string& errorStr, + const CUresult errorCode, + const std::string& functionName, + const std::string& fileName, + int lineNo) { + std::ostringstream errorLog; + errorLog << functionName << " : " << errorStr << " at " << fileName << ":" + << lineNo << std::endl; + NVDECException exception(errorLog.str(), errorCode); + return exception; +} + +#define NVDEC_THROW_ERROR(errorStr, errorCode) \ + do { \ + throw NVDECException::makeNVDECException( \ + errorStr, errorCode, __FUNCTION__, __FILE__, __LINE__); \ + } while (0) + +#define NVDEC_API_CALL(cuvidAPI) \ + do { \ + CUresult errorCode = cuvidAPI; \ + if (errorCode != CUDA_SUCCESS) { \ + std::ostringstream errorLog; \ + errorLog << #cuvidAPI << " returned error " << errorCode; \ + throw NVDECException::makeNVDECException( \ + errorLog.str(), errorCode, __FUNCTION__, __FILE__, __LINE__); \ + } \ + } while (0) + +struct Rect { + int l, t, r, b; +}; + +struct Dim { + int w, h; +}; + +/** +* @brief Base class for decoder interface. +*/ +class NvDecoder { + public: + /** + * @brief This function is used to initialize the decoder session. + * Application must call this function to initialize the decoder, before + * starting to decode any frames. + */ + NvDecoder(CUcontext cuContext, + bool bUseDeviceFrame, + cudaVideoCodec eCodec, + bool bLowLatency = false, + bool bDeviceFramePitched = false, + const Rect* pCropRect = NULL, + const Dim* pResizeDim = NULL, + bool extract_user_SEI_Message = false, + int maxWidth = 0, + int maxHeight = 0, + unsigned int clkRate = 1000, + bool force_zero_latency = false); + ~NvDecoder(); + + /** + * @brief This function is used to get the current CUDA context. + */ + CUcontext GetContext() { return m_cuContext; } + + /** + * @brief This function is used to get the output frame width. + * NV12/P016 output format width is 2 byte aligned because of U and V interleave + */ + int GetWidth() { + assert(m_nWidth); + return (m_eOutputFormat == cudaVideoSurfaceFormat_NV12 || + m_eOutputFormat == cudaVideoSurfaceFormat_P016) + ? (m_nWidth + 1) & ~1 + : m_nWidth; + } + + /** + * @brief This function is used to get the actual decode width + */ + int GetDecodeWidth() { + assert(m_nWidth); + return m_nWidth; + } + + /** + * @brief This function is used to get the output frame height (Luma height). + */ + int GetHeight() { + assert(m_nLumaHeight); + return m_nLumaHeight; + } + + /** + * @brief This function is used to get the current chroma height. + */ + int GetChromaHeight() { + assert(m_nChromaHeight); + return m_nChromaHeight; + } + + /** + * @brief This function is used to get the number of chroma planes. + */ + int GetNumChromaPlanes() { + assert(m_nNumChromaPlanes); + return m_nNumChromaPlanes; + } + + /** + * @brief This function is used to get the current frame size based on pixel format. + */ + int GetFrameSize() { + assert(m_nWidth); + return GetWidth() * + (m_nLumaHeight + (m_nChromaHeight * m_nNumChromaPlanes)) * m_nBPP; + } + + /** + * @brief This function is used to get the current frame Luma plane size. + */ + int GetLumaPlaneSize() { + assert(m_nWidth); + return GetWidth() * m_nLumaHeight * m_nBPP; + } + + /** + * @brief This function is used to get the current frame chroma plane size. + */ + int GetChromaPlaneSize() { + assert(m_nWidth); + return GetWidth() * (m_nChromaHeight * m_nNumChromaPlanes) * m_nBPP; + } + + /** + * @brief This function is used to get the pitch of the device buffer holding the decoded frame. + */ + int GetDeviceFramePitch() { + assert(m_nWidth); + return m_nDeviceFramePitch ? (int)m_nDeviceFramePitch : GetWidth() * m_nBPP; + } + + /** + * @brief This function is used to get the bit depth associated with the pixel format. + */ + int GetBitDepth() { + assert(m_nWidth); + return m_nBitDepthMinus8 + 8; + } + + /** + * @brief This function is used to get the bytes used per pixel. + */ + int GetBPP() { + assert(m_nWidth); + return m_nBPP; + } + + /** + * @brief This function is used to get the YUV chroma format + */ + cudaVideoSurfaceFormat GetOutputFormat() { return m_eOutputFormat; } + + /** + * @brief This function is used to get information about the video stream (codec, display parameters etc) + */ + CUVIDEOFORMAT GetVideoFormatInfo() { + assert(m_nWidth); + return m_videoFormat; + } + + /** + * @brief This function is used to get codec string from codec id + */ + const char* GetCodecString(cudaVideoCodec eCodec); + + /** + * @brief This function is used to print information about the video stream + */ + std::string GetVideoInfo() const { return m_videoInfo.str(); } + + /** + * @brief This function decodes a frame and returns the number of frames that are available for + * display. All frames that are available for display should be read before making a subsequent decode call. + * @param pData - pointer to the data buffer that is to be decoded + * @param nSize - size of the data buffer in bytes + * @param nFlags - CUvideopacketflags for setting decode options + * @param nTimestamp - presentation timestamp + */ + int Decode(const uint8_t* pData, + int nSize, + int nFlags = 0, + int64_t nTimestamp = 0); + + /** + * @brief This function returns a decoded frame and timestamp. This function should be called in a loop for + * fetching all the frames that are available for display. + */ + uint8_t* GetFrame(int64_t* pTimestamp = nullptr); + + /** + * @brief This function decodes a frame and returns the locked frame buffers + * This makes the buffers available for use by the application without the buffers + * getting overwritten, even if subsequent decode calls are made. The frame buffers + * remain locked, until UnlockFrame() is called + */ + uint8_t* GetLockedFrame(int64_t* pTimestamp = nullptr); + + /** + * @brief This function unlocks the frame buffer and makes the frame buffers available for write again + * @param ppFrame - pointer to array of frames that are to be unlocked + * @param nFrame - number of frames to be unlocked + */ + void UnlockFrame(uint8_t** pFrame); + + /** + * @brief This function allows app to set decoder reconfig params + * @param pCropRect - cropping rectangle coordinates + * @param pResizeDim - width and height of resized output + */ + int setReconfigParams(const Rect* pCropRect, const Dim* pResizeDim); + + /** + * @brief This function allows app to set operating point for AV1 SVC clips + * @param opPoint - operating point of an AV1 scalable bitstream + * @param bDispAllLayers - Output all decoded frames of an AV1 scalable bitstream + */ + void SetOperatingPoint(const uint32_t opPoint, const bool bDispAllLayers) { + m_nOperatingPoint = opPoint; + m_bDispAllLayers = bDispAllLayers; + } + + // start a timer + void startTimer() { m_stDecode_time.Start(); } + + // stop the timer + double stopTimer() { return m_stDecode_time.Stop(); } + + void setDecoderSessionID(int sessionID) { decoderSessionID = sessionID; } + int getDecoderSessionID() { return decoderSessionID; } + + // Session overhead refers to decoder initialization and deinitialization time + static void addDecoderSessionOverHead(int sessionID, int64_t duration) { + sessionOverHead[sessionID] += duration; + } + static int64_t getDecoderSessionOverHead(int sessionID) { + return sessionOverHead[sessionID]; + } + + private: + int decoderSessionID; // Decoder session identifier. Used to gather session level stats. + static std::map + sessionOverHead; // Records session overhead of initialization+deinitialization time. Format is (thread id, duration) + + /** + * @brief Callback function to be registered for getting a callback when decoding of sequence starts + */ + static int CUDAAPI HandleVideoSequenceProc(void* pUserData, + CUVIDEOFORMAT* pVideoFormat) { + return ((NvDecoder*)pUserData)->HandleVideoSequence(pVideoFormat); + } + + /** + * @brief Callback function to be registered for getting a callback when a decoded frame is ready to be decoded + */ + static int CUDAAPI HandlePictureDecodeProc(void* pUserData, + CUVIDPICPARAMS* pPicParams) { + return ((NvDecoder*)pUserData)->HandlePictureDecode(pPicParams); + } + + /** + * @brief Callback function to be registered for getting a callback when a decoded frame is available for display + */ + static int CUDAAPI HandlePictureDisplayProc(void* pUserData, + CUVIDPARSERDISPINFO* pDispInfo) { + return ((NvDecoder*)pUserData)->HandlePictureDisplay(pDispInfo); + } + + /** + * @brief Callback function to be registered for getting a callback to get operating point when AV1 SVC sequence header start. + */ + static int CUDAAPI + HandleOperatingPointProc(void* pUserData, CUVIDOPERATINGPOINTINFO* pOPInfo) { + return ((NvDecoder*)pUserData)->GetOperatingPoint(pOPInfo); + } + + /** + * @brief Callback function to be registered for getting a callback when all the unregistered user SEI Messages are parsed for a frame. + */ + static int CUDAAPI + HandleSEIMessagesProc(void* pUserData, CUVIDSEIMESSAGEINFO* pSEIMessageInfo) { + return ((NvDecoder*)pUserData)->GetSEIMessage(pSEIMessageInfo); + } + + /** + * @brief This function gets called when a sequence is ready to be decoded. The function also gets called + when there is format change + */ + int HandleVideoSequence(CUVIDEOFORMAT* pVideoFormat); + + /** + * @brief This function gets called when a picture is ready to be decoded. cuvidDecodePicture is called from this function + * to decode the picture + */ + int HandlePictureDecode(CUVIDPICPARAMS* pPicParams); + + /** + * @brief This function gets called after a picture is decoded and available for display. Frames are fetched and stored in + internal buffer + */ + int HandlePictureDisplay(CUVIDPARSERDISPINFO* pDispInfo); + + /** + * @brief This function gets called when AV1 sequence encounter more than one operating points + */ + int GetOperatingPoint(CUVIDOPERATINGPOINTINFO* pOPInfo); + + /** + * @brief This function gets called when all unregistered user SEI messages are parsed for a frame + */ + int GetSEIMessage(CUVIDSEIMESSAGEINFO* pSEIMessageInfo); + + /** + * @brief This function reconfigure decoder if there is a change in sequence params. + */ + int ReconfigureDecoder(CUVIDEOFORMAT* pVideoFormat); + + private: + CUcontext m_cuContext = NULL; + CUvideoctxlock m_ctxLock; + CUvideoparser m_hParser = NULL; + CUvideodecoder m_hDecoder = NULL; + bool m_bUseDeviceFrame; + // dimension of the output + unsigned int m_nWidth = 0, m_nLumaHeight = 0, m_nChromaHeight = 0; + unsigned int m_nNumChromaPlanes = 0; + // height of the mapped surface + int m_nSurfaceHeight = 0; + int m_nSurfaceWidth = 0; + cudaVideoCodec m_eCodec = cudaVideoCodec_NumCodecs; + cudaVideoChromaFormat m_eChromaFormat = cudaVideoChromaFormat_420; + cudaVideoSurfaceFormat m_eOutputFormat = cudaVideoSurfaceFormat_NV12; + int m_nBitDepthMinus8 = 0; + int m_nBPP = 1; + CUVIDEOFORMAT m_videoFormat = {}; + Rect m_displayRect = {}; + // stock of frames + std::vector m_vpFrame; + // timestamps of decoded frames + std::vector m_vTimestamp; + int m_nDecodedFrame = 0, m_nDecodedFrameReturned = 0; + int m_nDecodePicCnt = 0, m_nPicNumInDecodeOrder[MAX_FRM_CNT]; + CUVIDSEIMESSAGEINFO* m_pCurrSEIMessage = NULL; + CUVIDSEIMESSAGEINFO m_SEIMessagesDisplayOrder[MAX_FRM_CNT]; + FILE* m_fpSEI = NULL; + bool m_bEndDecodeDone = false; + std::mutex m_mtxVPFrame; + int m_nFrameAlloc = 0; + CUstream m_cuvidStream = 0; + bool m_bDeviceFramePitched = false; + size_t m_nDeviceFramePitch = 0; + Rect m_cropRect = {}; + Dim m_resizeDim = {}; + + std::ostringstream m_videoInfo; + unsigned int m_nMaxWidth = 0, m_nMaxHeight = 0; + bool m_bReconfigExternal = false; + bool m_bReconfigExtPPChange = false; + StopWatch m_stDecode_time; + + unsigned int m_nOperatingPoint = 0; + bool m_bDispAllLayers = false; + // In H.264, there is an inherent display latency for video contents + // which do not have num_reorder_frames=0 in the VUI. This applies to + // All-Intra and IPPP sequences as well. If the user wants zero display + // latency for All-Intra and IPPP sequences, the below flag will enable + // the display callback immediately after the decode callback. + bool m_bForce_zero_latency = false; + bool m_bExtractSEIMessage = false; +}; diff --git a/NvCodec/NvCodec/NvEncoder/NvEncoder.cpp b/NvCodec/NvCodec/NvEncoder/NvEncoder.cpp index 15bada6d..26c490d5 100644 --- a/NvCodec/NvCodec/NvEncoder/NvEncoder.cpp +++ b/NvCodec/NvCodec/NvEncoder/NvEncoder.cpp @@ -1,1038 +1,1121 @@ -/* -* Copyright 2017-2020 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ - -#include "NvEncoder/NvEncoder.h" - -#ifndef _WIN32 -#include -static inline bool operator==(const GUID &guid1, const GUID &guid2) { - return !memcmp(&guid1, &guid2, sizeof(GUID)); -} - -static inline bool operator!=(const GUID &guid1, const GUID &guid2) { - return !(guid1 == guid2); -} -#endif - -NvEncoder::NvEncoder(NV_ENC_DEVICE_TYPE eDeviceType, void *pDevice, uint32_t nWidth, uint32_t nHeight, NV_ENC_BUFFER_FORMAT eBufferFormat, - uint32_t nExtraOutputDelay, bool bMotionEstimationOnly, bool bOutputInVideoMemory) : - m_pDevice(pDevice), - m_eDeviceType(eDeviceType), - m_nWidth(nWidth), - m_nHeight(nHeight), - m_nMaxEncodeWidth(nWidth), - m_nMaxEncodeHeight(nHeight), - m_eBufferFormat(eBufferFormat), - m_bMotionEstimationOnly(bMotionEstimationOnly), - m_bOutputInVideoMemory(bOutputInVideoMemory), - m_nExtraOutputDelay(nExtraOutputDelay), - m_hEncoder(nullptr) -{ - LoadNvEncApi(); - - if (!m_nvenc.nvEncOpenEncodeSession) - { - m_nEncoderBuffer = 0; - NVENC_THROW_ERROR("EncodeAPI not found", NV_ENC_ERR_NO_ENCODE_DEVICE); - } - - NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS encodeSessionExParams = { NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER }; - encodeSessionExParams.device = m_pDevice; - encodeSessionExParams.deviceType = m_eDeviceType; - encodeSessionExParams.apiVersion = NVENCAPI_VERSION; - void *hEncoder = NULL; - NVENC_API_CALL(m_nvenc.nvEncOpenEncodeSessionEx(&encodeSessionExParams, &hEncoder)); - m_hEncoder = hEncoder; -} - -void NvEncoder::TryLoadNvEncApi() { -#if defined(_WIN32) -#if defined(_WIN64) - HMODULE hModule = LoadLibrary(TEXT("nvEncodeAPI64.dll")); -#else - HMODULE hModule = LoadLibrary(TEXT("nvEncodeAPI.dll")); -#endif -#else - void* hModule = dlopen("libnvidia-encode.so.1", RTLD_LAZY); -#endif - - if (hModule == NULL) { - NVENC_THROW_ERROR( - "NVENC library file is not found. Please ensure NV driver is installed", - NV_ENC_ERR_NO_ENCODE_DEVICE); - } - - typedef NVENCSTATUS(NVENCAPI * - NvEncodeAPIGetMaxSupportedVersion_Type)(uint32_t*); -#if defined(_WIN32) - NvEncodeAPIGetMaxSupportedVersion_Type NvEncodeAPIGetMaxSupportedVersion = - (NvEncodeAPIGetMaxSupportedVersion_Type)GetProcAddress( - hModule, "NvEncodeAPIGetMaxSupportedVersion"); -#else - NvEncodeAPIGetMaxSupportedVersion_Type NvEncodeAPIGetMaxSupportedVersion = - (NvEncodeAPIGetMaxSupportedVersion_Type)dlsym( - hModule, "NvEncodeAPIGetMaxSupportedVersion"); -#endif - - uint32_t version = 0; - uint32_t currentVersion = - (NVENCAPI_MAJOR_VERSION << 4) | NVENCAPI_MINOR_VERSION; - NVENC_API_CALL(NvEncodeAPIGetMaxSupportedVersion(&version)); - if (currentVersion > version) { -#if defined(_WIN32) - FreeLibrary((HMODULE)hModule); -#else - dlclose(hModule); -#endif - NVENC_THROW_ERROR( - "Current Driver Version does not support this NvEncodeAPI version, " - "please upgrade driver", - NV_ENC_ERR_INVALID_VERSION); - } -#if defined(_WIN32) - FreeLibrary((HMODULE)hModule); -#else - dlclose(hModule); -#endif -} - -void NvEncoder::LoadNvEncApi() -{ -#if defined(_WIN32) -#if defined(_WIN64) - HMODULE hModule = LoadLibrary(TEXT("nvEncodeAPI64.dll")); -#else - HMODULE hModule = LoadLibrary(TEXT("nvEncodeAPI.dll")); -#endif -#else - void *hModule = dlopen("libnvidia-encode.so.1", RTLD_LAZY); -#endif - - if (hModule == NULL) - { - NVENC_THROW_ERROR("NVENC library file is not found. Please ensure NV driver is installed", NV_ENC_ERR_NO_ENCODE_DEVICE); - } - - m_hModule = hModule; - - typedef NVENCSTATUS(NVENCAPI *NvEncodeAPIGetMaxSupportedVersion_Type)(uint32_t*); -#if defined(_WIN32) - NvEncodeAPIGetMaxSupportedVersion_Type NvEncodeAPIGetMaxSupportedVersion = (NvEncodeAPIGetMaxSupportedVersion_Type)GetProcAddress(hModule, "NvEncodeAPIGetMaxSupportedVersion"); -#else - NvEncodeAPIGetMaxSupportedVersion_Type NvEncodeAPIGetMaxSupportedVersion = (NvEncodeAPIGetMaxSupportedVersion_Type)dlsym(hModule, "NvEncodeAPIGetMaxSupportedVersion"); -#endif - - uint32_t version = 0; - uint32_t currentVersion = (NVENCAPI_MAJOR_VERSION << 4) | NVENCAPI_MINOR_VERSION; - NVENC_API_CALL(NvEncodeAPIGetMaxSupportedVersion(&version)); - if (currentVersion > version) - { - NVENC_THROW_ERROR("Current Driver Version does not support this NvEncodeAPI version, please upgrade driver", NV_ENC_ERR_INVALID_VERSION); - } - - typedef NVENCSTATUS(NVENCAPI *NvEncodeAPICreateInstance_Type)(NV_ENCODE_API_FUNCTION_LIST*); -#if defined(_WIN32) - NvEncodeAPICreateInstance_Type NvEncodeAPICreateInstance = (NvEncodeAPICreateInstance_Type)GetProcAddress(hModule, "NvEncodeAPICreateInstance"); -#else - NvEncodeAPICreateInstance_Type NvEncodeAPICreateInstance = (NvEncodeAPICreateInstance_Type)dlsym(hModule, "NvEncodeAPICreateInstance"); -#endif - - if (!NvEncodeAPICreateInstance) - { - NVENC_THROW_ERROR("Cannot find NvEncodeAPICreateInstance() entry in NVENC library", NV_ENC_ERR_NO_ENCODE_DEVICE); - } - - m_nvenc = { NV_ENCODE_API_FUNCTION_LIST_VER }; - NVENC_API_CALL(NvEncodeAPICreateInstance(&m_nvenc)); -} - -NvEncoder::~NvEncoder() -{ - DestroyHWEncoder(); - - if (m_hModule) - { -#if defined(_WIN32) - FreeLibrary((HMODULE)m_hModule); -#else - dlclose(m_hModule); -#endif - m_hModule = nullptr; - } -} - -void NvEncoder::CreateDefaultEncoderParams(NV_ENC_INITIALIZE_PARAMS* pIntializeParams, GUID codecGuid, GUID presetGuid, NV_ENC_TUNING_INFO tuningInfo) -{ - if (!m_hEncoder) - { - NVENC_THROW_ERROR("Encoder Initialization failed", NV_ENC_ERR_NO_ENCODE_DEVICE); - return; - } - - if (pIntializeParams == nullptr || pIntializeParams->encodeConfig == nullptr) - { - NVENC_THROW_ERROR("pInitializeParams and pInitializeParams->encodeConfig can't be NULL", NV_ENC_ERR_INVALID_PTR); - } - - memset(pIntializeParams->encodeConfig, 0, sizeof(NV_ENC_CONFIG)); - auto pEncodeConfig = pIntializeParams->encodeConfig; - memset(pIntializeParams, 0, sizeof(NV_ENC_INITIALIZE_PARAMS)); - pIntializeParams->encodeConfig = pEncodeConfig; - - - pIntializeParams->encodeConfig->version = NV_ENC_CONFIG_VER; - pIntializeParams->version = NV_ENC_INITIALIZE_PARAMS_VER; - - pIntializeParams->encodeGUID = codecGuid; - pIntializeParams->presetGUID = presetGuid; - pIntializeParams->encodeWidth = m_nWidth; - pIntializeParams->encodeHeight = m_nHeight; - pIntializeParams->darWidth = m_nWidth; - pIntializeParams->darHeight = m_nHeight; - pIntializeParams->frameRateNum = 30; - pIntializeParams->frameRateDen = 1; - pIntializeParams->enablePTD = 1; - pIntializeParams->reportSliceOffsets = 0; - pIntializeParams->enableSubFrameWrite = 0; - pIntializeParams->maxEncodeWidth = m_nWidth; - pIntializeParams->maxEncodeHeight = m_nHeight; - pIntializeParams->enableMEOnlyMode = m_bMotionEstimationOnly; - pIntializeParams->enableOutputInVidmem = m_bOutputInVideoMemory; -#if defined(_WIN32) - if (!m_bOutputInVideoMemory) - { - pIntializeParams->enableEncodeAsync = GetCapabilityValue(codecGuid, NV_ENC_CAPS_ASYNC_ENCODE_SUPPORT); - } -#endif - - NV_ENC_PRESET_CONFIG presetConfig = { NV_ENC_PRESET_CONFIG_VER, { NV_ENC_CONFIG_VER } }; - m_nvenc.nvEncGetEncodePresetConfig(m_hEncoder, codecGuid, presetGuid, &presetConfig); - memcpy(pIntializeParams->encodeConfig, &presetConfig.presetCfg, sizeof(NV_ENC_CONFIG)); - pIntializeParams->encodeConfig->frameIntervalP = 1; - pIntializeParams->encodeConfig->gopLength = NVENC_INFINITE_GOPLENGTH; - - pIntializeParams->encodeConfig->rcParams.rateControlMode = NV_ENC_PARAMS_RC_CONSTQP; - - if (!m_bMotionEstimationOnly) - { - pIntializeParams->tuningInfo = tuningInfo; - NV_ENC_PRESET_CONFIG presetConfig = { NV_ENC_PRESET_CONFIG_VER, { NV_ENC_CONFIG_VER } }; - m_nvenc.nvEncGetEncodePresetConfigEx(m_hEncoder, codecGuid, presetGuid, tuningInfo, &presetConfig); - memcpy(pIntializeParams->encodeConfig, &presetConfig.presetCfg, sizeof(NV_ENC_CONFIG)); - } - else - { - m_encodeConfig.version = NV_ENC_CONFIG_VER; - m_encodeConfig.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CONSTQP; - m_encodeConfig.rcParams.constQP = { 28, 31, 25 }; - } - - if (pIntializeParams->encodeGUID == NV_ENC_CODEC_H264_GUID) - { - if (m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444 || m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444_10BIT) - { - pIntializeParams->encodeConfig->encodeCodecConfig.h264Config.chromaFormatIDC = 3; - } - pIntializeParams->encodeConfig->encodeCodecConfig.h264Config.idrPeriod = pIntializeParams->encodeConfig->gopLength; - } - else if (pIntializeParams->encodeGUID == NV_ENC_CODEC_HEVC_GUID) - { - pIntializeParams->encodeConfig->encodeCodecConfig.hevcConfig.pixelBitDepthMinus8 = - (m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV420_10BIT || m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444_10BIT ) ? 2 : 0; - if (m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444 || m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444_10BIT) - { - pIntializeParams->encodeConfig->encodeCodecConfig.hevcConfig.chromaFormatIDC = 3; - } - pIntializeParams->encodeConfig->encodeCodecConfig.hevcConfig.idrPeriod = pIntializeParams->encodeConfig->gopLength; - } - - return; -} - -void NvEncoder::CreateEncoder(const NV_ENC_INITIALIZE_PARAMS* pEncoderParams) -{ - if (!m_hEncoder) - { - NVENC_THROW_ERROR("Encoder Initialization failed", NV_ENC_ERR_NO_ENCODE_DEVICE); - } - - if (!pEncoderParams) - { - NVENC_THROW_ERROR("Invalid NV_ENC_INITIALIZE_PARAMS ptr", NV_ENC_ERR_INVALID_PTR); - } - - if (pEncoderParams->encodeWidth == 0 || pEncoderParams->encodeHeight == 0) - { - NVENC_THROW_ERROR("Invalid encoder width and height", NV_ENC_ERR_INVALID_PARAM); - } - - if (pEncoderParams->encodeGUID != NV_ENC_CODEC_H264_GUID && pEncoderParams->encodeGUID != NV_ENC_CODEC_HEVC_GUID) - { - NVENC_THROW_ERROR("Invalid codec guid", NV_ENC_ERR_INVALID_PARAM); - } - - if (pEncoderParams->encodeGUID == NV_ENC_CODEC_H264_GUID) - { - if (m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV420_10BIT || m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444_10BIT) - { - NVENC_THROW_ERROR("10-bit format isn't supported by H264 encoder", NV_ENC_ERR_INVALID_PARAM); - } - } - - // set other necessary params if not set yet - if (pEncoderParams->encodeGUID == NV_ENC_CODEC_H264_GUID) - { - if ((m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444) && - (pEncoderParams->encodeConfig->encodeCodecConfig.h264Config.chromaFormatIDC != 3)) - { - NVENC_THROW_ERROR("Invalid ChromaFormatIDC", NV_ENC_ERR_INVALID_PARAM); - } - } - - if (pEncoderParams->encodeGUID == NV_ENC_CODEC_HEVC_GUID) - { - bool yuv10BitFormat = (m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV420_10BIT || m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444_10BIT) ? true : false; - if (yuv10BitFormat && pEncoderParams->encodeConfig->encodeCodecConfig.hevcConfig.pixelBitDepthMinus8 != 2) - { - NVENC_THROW_ERROR("Invalid PixelBitdepth", NV_ENC_ERR_INVALID_PARAM); - } - - if ((m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444 || m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444_10BIT) && - (pEncoderParams->encodeConfig->encodeCodecConfig.hevcConfig.chromaFormatIDC != 3)) - { - NVENC_THROW_ERROR("Invalid ChromaFormatIDC", NV_ENC_ERR_INVALID_PARAM); - } - } - - memcpy(&m_initializeParams, pEncoderParams, sizeof(m_initializeParams)); - m_initializeParams.version = NV_ENC_INITIALIZE_PARAMS_VER; - - if (pEncoderParams->encodeConfig) - { - memcpy(&m_encodeConfig, pEncoderParams->encodeConfig, sizeof(m_encodeConfig)); - m_encodeConfig.version = NV_ENC_CONFIG_VER; - } - else - { - NV_ENC_PRESET_CONFIG presetConfig = { NV_ENC_PRESET_CONFIG_VER, { NV_ENC_CONFIG_VER } }; - if (!m_bMotionEstimationOnly) - { - m_nvenc.nvEncGetEncodePresetConfigEx(m_hEncoder, pEncoderParams->encodeGUID, pEncoderParams->presetGUID, pEncoderParams->tuningInfo, &presetConfig); - memcpy(&m_encodeConfig, &presetConfig.presetCfg, sizeof(NV_ENC_CONFIG)); - } - else - { - m_encodeConfig.version = NV_ENC_CONFIG_VER; - m_encodeConfig.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CONSTQP; - m_encodeConfig.rcParams.constQP = { 28, 31, 25 }; - } - } - m_initializeParams.encodeConfig = &m_encodeConfig; - - NVENC_API_CALL(m_nvenc.nvEncInitializeEncoder(m_hEncoder, &m_initializeParams)); - - m_bEncoderInitialized = true; - m_nWidth = m_initializeParams.encodeWidth; - m_nHeight = m_initializeParams.encodeHeight; - m_nMaxEncodeWidth = m_initializeParams.maxEncodeWidth; - m_nMaxEncodeHeight = m_initializeParams.maxEncodeHeight; - - m_nEncoderBuffer = m_encodeConfig.frameIntervalP + m_encodeConfig.rcParams.lookaheadDepth + m_nExtraOutputDelay; - m_nOutputDelay = m_nEncoderBuffer - 1; - m_vMappedInputBuffers.resize(m_nEncoderBuffer, nullptr); - - if (!m_bOutputInVideoMemory) - { - m_vpCompletionEvent.resize(m_nEncoderBuffer, nullptr); - } - -#if defined(_WIN32) - for (uint32_t i = 0; i < m_vpCompletionEvent.size(); i++) - { - m_vpCompletionEvent[i] = CreateEvent(NULL, FALSE, FALSE, NULL); - NV_ENC_EVENT_PARAMS eventParams = { NV_ENC_EVENT_PARAMS_VER }; - eventParams.completionEvent = m_vpCompletionEvent[i]; - m_nvenc.nvEncRegisterAsyncEvent(m_hEncoder, &eventParams); - } -#endif - - if (m_bMotionEstimationOnly) - { - m_vMappedRefBuffers.resize(m_nEncoderBuffer, nullptr); - - if (!m_bOutputInVideoMemory) - { - InitializeMVOutputBuffer(); - } - } - else - { - if (!m_bOutputInVideoMemory) - { - m_vBitstreamOutputBuffer.resize(m_nEncoderBuffer, nullptr); - InitializeBitstreamBuffer(); - } - } - - AllocateInputBuffers(m_nEncoderBuffer); -} - -void NvEncoder::DestroyEncoder() -{ - if (!m_hEncoder) - { - return; - } - - ReleaseInputBuffers(); - - DestroyHWEncoder(); -} - -void NvEncoder::DestroyHWEncoder() -{ - if (!m_hEncoder) - { - return; - } - -#if defined(_WIN32) - for (uint32_t i = 0; i < m_vpCompletionEvent.size(); i++) - { - if (m_vpCompletionEvent[i]) - { - NV_ENC_EVENT_PARAMS eventParams = { NV_ENC_EVENT_PARAMS_VER }; - eventParams.completionEvent = m_vpCompletionEvent[i]; - m_nvenc.nvEncUnregisterAsyncEvent(m_hEncoder, &eventParams); - CloseHandle(m_vpCompletionEvent[i]); - } - } - m_vpCompletionEvent.clear(); -#endif - - if (m_bMotionEstimationOnly) - { - DestroyMVOutputBuffer(); - } - else - { - DestroyBitstreamBuffer(); - } - - m_nvenc.nvEncDestroyEncoder(m_hEncoder); - - m_hEncoder = nullptr; - - m_bEncoderInitialized = false; -} - -const NvEncInputFrame* NvEncoder::GetNextInputFrame() -{ - int i = m_iToSend % m_nEncoderBuffer; - return &m_vInputFrames[i]; -} - -const NvEncInputFrame* NvEncoder::GetNextReferenceFrame() -{ - int i = m_iToSend % m_nEncoderBuffer; - return &m_vReferenceFrames[i]; -} - -void NvEncoder::MapResources(uint32_t bfrIdx) -{ - NV_ENC_MAP_INPUT_RESOURCE mapInputResource = { NV_ENC_MAP_INPUT_RESOURCE_VER }; - - mapInputResource.registeredResource = m_vRegisteredResources[bfrIdx]; - NVENC_API_CALL(m_nvenc.nvEncMapInputResource(m_hEncoder, &mapInputResource)); - m_vMappedInputBuffers[bfrIdx] = mapInputResource.mappedResource; - - if (m_bMotionEstimationOnly) - { - mapInputResource.registeredResource = m_vRegisteredResourcesForReference[bfrIdx]; - NVENC_API_CALL(m_nvenc.nvEncMapInputResource(m_hEncoder, &mapInputResource)); - m_vMappedRefBuffers[bfrIdx] = mapInputResource.mappedResource; - } -} - -void NvEncoder::EncodeFrame(std::vector> &vPacket, NV_ENC_PIC_PARAMS *pPicParams) -{ - vPacket.clear(); - if (!IsHWEncoderInitialized()) - { - NVENC_THROW_ERROR("Encoder device not found", NV_ENC_ERR_NO_ENCODE_DEVICE); - } - - int bfrIdx = m_iToSend % m_nEncoderBuffer; - - MapResources(bfrIdx); - - NVENCSTATUS nvStatus = DoEncode(m_vMappedInputBuffers[bfrIdx], m_vBitstreamOutputBuffer[bfrIdx], pPicParams); - - if (nvStatus == NV_ENC_SUCCESS || nvStatus == NV_ENC_ERR_NEED_MORE_INPUT) - { - m_iToSend++; - GetEncodedPacket(m_vBitstreamOutputBuffer, vPacket, true); - } - else - { - NVENC_THROW_ERROR("nvEncEncodePicture API failed", nvStatus); - } -} - -void NvEncoder::RunMotionEstimation(std::vector &mvData) -{ - if (!m_hEncoder) - { - NVENC_THROW_ERROR("Encoder Initialization failed", NV_ENC_ERR_NO_ENCODE_DEVICE); - return; - } - - const uint32_t bfrIdx = m_iToSend % m_nEncoderBuffer; - - MapResources(bfrIdx); - - NVENCSTATUS nvStatus = DoMotionEstimation(m_vMappedInputBuffers[bfrIdx], m_vMappedRefBuffers[bfrIdx], m_vMVDataOutputBuffer[bfrIdx]); - - if (nvStatus == NV_ENC_SUCCESS) - { - m_iToSend++; - std::vector> vPacket; - GetEncodedPacket(m_vMVDataOutputBuffer, vPacket, true); - if (vPacket.size() != 1) - { - NVENC_THROW_ERROR("GetEncodedPacket() doesn't return one (and only one) MVData", NV_ENC_ERR_GENERIC); - } - mvData = vPacket[0]; - } - else - { - NVENC_THROW_ERROR("nvEncEncodePicture API failed", nvStatus); - } -} - - -void NvEncoder::GetSequenceParams(std::vector &seqParams) -{ - uint8_t spsppsData[1024]; // Assume maximum spspps data is 1KB or less - memset(spsppsData, 0, sizeof(spsppsData)); - NV_ENC_SEQUENCE_PARAM_PAYLOAD payload = { NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER }; - uint32_t spsppsSize = 0; - - payload.spsppsBuffer = spsppsData; - payload.inBufferSize = sizeof(spsppsData); - payload.outSPSPPSPayloadSize = &spsppsSize; - NVENC_API_CALL(m_nvenc.nvEncGetSequenceParams(m_hEncoder, &payload)); - seqParams.clear(); - seqParams.insert(seqParams.end(), &spsppsData[0], &spsppsData[spsppsSize]); -} - -NVENCSTATUS NvEncoder::DoEncode(NV_ENC_INPUT_PTR inputBuffer, NV_ENC_OUTPUT_PTR outputBuffer, NV_ENC_PIC_PARAMS *pPicParams) -{ - NV_ENC_PIC_PARAMS picParams = {}; - if (pPicParams) - { - picParams = *pPicParams; - } - picParams.version = NV_ENC_PIC_PARAMS_VER; - picParams.pictureStruct = NV_ENC_PIC_STRUCT_FRAME; - picParams.inputBuffer = inputBuffer; - picParams.bufferFmt = GetPixelFormat(); - picParams.inputWidth = GetEncodeWidth(); - picParams.inputHeight = GetEncodeHeight(); - picParams.outputBitstream = outputBuffer; - picParams.completionEvent = GetCompletionEvent(m_iToSend % m_nEncoderBuffer); - NVENCSTATUS nvStatus = m_nvenc.nvEncEncodePicture(m_hEncoder, &picParams); - - return nvStatus; -} - -void NvEncoder::SendEOS() -{ - NV_ENC_PIC_PARAMS picParams = { NV_ENC_PIC_PARAMS_VER }; - picParams.encodePicFlags = NV_ENC_PIC_FLAG_EOS; - picParams.completionEvent = GetCompletionEvent(m_iToSend % m_nEncoderBuffer); - NVENC_API_CALL(m_nvenc.nvEncEncodePicture(m_hEncoder, &picParams)); -} - -void NvEncoder::EndEncode(std::vector> &vPacket) -{ - vPacket.clear(); - if (!IsHWEncoderInitialized()) - { - NVENC_THROW_ERROR("Encoder device not initialized", NV_ENC_ERR_ENCODER_NOT_INITIALIZED); - } - - SendEOS(); - - GetEncodedPacket(m_vBitstreamOutputBuffer, vPacket, false); -} - -void NvEncoder::GetEncodedPacket(std::vector &vOutputBuffer, std::vector> &vPacket, bool bOutputDelay) -{ - unsigned i = 0; - int iEnd = bOutputDelay ? m_iToSend - m_nOutputDelay : m_iToSend; - for (; m_iGot < iEnd; m_iGot++) - { - WaitForCompletionEvent(m_iGot % m_nEncoderBuffer); - NV_ENC_LOCK_BITSTREAM lockBitstreamData = { NV_ENC_LOCK_BITSTREAM_VER }; - lockBitstreamData.outputBitstream = vOutputBuffer[m_iGot % m_nEncoderBuffer]; - lockBitstreamData.doNotWait = false; - NVENC_API_CALL(m_nvenc.nvEncLockBitstream(m_hEncoder, &lockBitstreamData)); - - uint8_t *pData = (uint8_t *)lockBitstreamData.bitstreamBufferPtr; - if (vPacket.size() < i + 1) - { - vPacket.push_back(std::vector()); - } - vPacket[i].clear(); - vPacket[i].insert(vPacket[i].end(), &pData[0], &pData[lockBitstreamData.bitstreamSizeInBytes]); - i++; - - NVENC_API_CALL(m_nvenc.nvEncUnlockBitstream(m_hEncoder, lockBitstreamData.outputBitstream)); - - if (m_vMappedInputBuffers[m_iGot % m_nEncoderBuffer]) - { - NVENC_API_CALL(m_nvenc.nvEncUnmapInputResource(m_hEncoder, m_vMappedInputBuffers[m_iGot % m_nEncoderBuffer])); - m_vMappedInputBuffers[m_iGot % m_nEncoderBuffer] = nullptr; - } - - if (m_bMotionEstimationOnly && m_vMappedRefBuffers[m_iGot % m_nEncoderBuffer]) - { - NVENC_API_CALL(m_nvenc.nvEncUnmapInputResource(m_hEncoder, m_vMappedRefBuffers[m_iGot % m_nEncoderBuffer])); - m_vMappedRefBuffers[m_iGot % m_nEncoderBuffer] = nullptr; - } - } -} - -bool NvEncoder::Reconfigure(const NV_ENC_RECONFIGURE_PARAMS *pReconfigureParams) -{ - NVENC_API_CALL(m_nvenc.nvEncReconfigureEncoder(m_hEncoder, const_cast(pReconfigureParams))); - - memcpy(&m_initializeParams, &(pReconfigureParams->reInitEncodeParams), sizeof(m_initializeParams)); - if (pReconfigureParams->reInitEncodeParams.encodeConfig) - { - memcpy(&m_encodeConfig, pReconfigureParams->reInitEncodeParams.encodeConfig, sizeof(m_encodeConfig)); - } - - m_nWidth = m_initializeParams.encodeWidth; - m_nHeight = m_initializeParams.encodeHeight; - m_nMaxEncodeWidth = m_initializeParams.maxEncodeWidth; - m_nMaxEncodeHeight = m_initializeParams.maxEncodeHeight; - - return true; -} - -NV_ENC_REGISTERED_PTR NvEncoder::RegisterResource(void *pBuffer, NV_ENC_INPUT_RESOURCE_TYPE eResourceType, - int width, int height, int pitch, NV_ENC_BUFFER_FORMAT bufferFormat, NV_ENC_BUFFER_USAGE bufferUsage) -{ - NV_ENC_REGISTER_RESOURCE registerResource = { NV_ENC_REGISTER_RESOURCE_VER }; - registerResource.resourceType = eResourceType; - registerResource.resourceToRegister = pBuffer; - registerResource.width = width; - registerResource.height = height; - registerResource.pitch = pitch; - registerResource.bufferFormat = bufferFormat; - registerResource.bufferUsage = bufferUsage; - NVENC_API_CALL(m_nvenc.nvEncRegisterResource(m_hEncoder, ®isterResource)); - - return registerResource.registeredResource; -} - -void NvEncoder::RegisterInputResources(std::vector inputframes, NV_ENC_INPUT_RESOURCE_TYPE eResourceType, - int width, int height, int pitch, NV_ENC_BUFFER_FORMAT bufferFormat, bool bReferenceFrame) -{ - for (uint32_t i = 0; i < inputframes.size(); ++i) - { - NV_ENC_REGISTERED_PTR registeredPtr = RegisterResource(inputframes[i], eResourceType, width, height, pitch, bufferFormat, NV_ENC_INPUT_IMAGE); - - std::vector _chromaOffsets; - NvEncoder::GetChromaSubPlaneOffsets(bufferFormat, pitch, height, _chromaOffsets); - NvEncInputFrame inputframe = {}; - inputframe.inputPtr = (void *)inputframes[i]; - inputframe.chromaOffsets[0] = 0; - inputframe.chromaOffsets[1] = 0; - for (uint32_t ch = 0; ch < _chromaOffsets.size(); ch++) - { - inputframe.chromaOffsets[ch] = _chromaOffsets[ch]; - } - inputframe.numChromaPlanes = NvEncoder::GetNumChromaPlanes(bufferFormat); - inputframe.pitch = pitch; - inputframe.chromaPitch = NvEncoder::GetChromaPitch(bufferFormat, pitch); - inputframe.bufferFormat = bufferFormat; - inputframe.resourceType = eResourceType; - - if (bReferenceFrame) - { - m_vRegisteredResourcesForReference.push_back(registeredPtr); - m_vReferenceFrames.push_back(inputframe); - } - else - { - m_vRegisteredResources.push_back(registeredPtr); - m_vInputFrames.push_back(inputframe); - } - } -} - -void NvEncoder::FlushEncoder() -{ - if (!m_bMotionEstimationOnly && !m_bOutputInVideoMemory) - { - // Incase of error it is possible for buffers still mapped to encoder. - // flush the encoder queue and then unmapped it if any surface is still mapped - try - { - std::vector> vPacket; - EndEncode(vPacket); - } - catch (...) - { - - } - } -} - -void NvEncoder::UnregisterInputResources() -{ - FlushEncoder(); - - if (m_bMotionEstimationOnly) - { - for (uint32_t i = 0; i < m_vMappedRefBuffers.size(); ++i) - { - if (m_vMappedRefBuffers[i]) - { - m_nvenc.nvEncUnmapInputResource(m_hEncoder, m_vMappedRefBuffers[i]); - } - } - } - m_vMappedRefBuffers.clear(); - - for (uint32_t i = 0; i < m_vMappedInputBuffers.size(); ++i) - { - if (m_vMappedInputBuffers[i]) - { - m_nvenc.nvEncUnmapInputResource(m_hEncoder, m_vMappedInputBuffers[i]); - } - } - m_vMappedInputBuffers.clear(); - - for (uint32_t i = 0; i < m_vRegisteredResources.size(); ++i) - { - if (m_vRegisteredResources[i]) - { - m_nvenc.nvEncUnregisterResource(m_hEncoder, m_vRegisteredResources[i]); - } - } - m_vRegisteredResources.clear(); - - - for (uint32_t i = 0; i < m_vRegisteredResourcesForReference.size(); ++i) - { - if (m_vRegisteredResourcesForReference[i]) - { - m_nvenc.nvEncUnregisterResource(m_hEncoder, m_vRegisteredResourcesForReference[i]); - } - } - m_vRegisteredResourcesForReference.clear(); - -} - - -void NvEncoder::WaitForCompletionEvent(int iEvent) -{ -#if defined(_WIN32) - // Check if we are in async mode. If not, don't wait for event; - NV_ENC_CONFIG sEncodeConfig = { 0 }; - NV_ENC_INITIALIZE_PARAMS sInitializeParams = { 0 }; - sInitializeParams.encodeConfig = &sEncodeConfig; - GetInitializeParams(&sInitializeParams); - - if (0U == sInitializeParams.enableEncodeAsync) - { - return; - } -#ifdef DEBUG - WaitForSingleObject(m_vpCompletionEvent[iEvent], INFINITE); -#else - // wait for 20s which is infinite on terms of gpu time - if (WaitForSingleObject(m_vpCompletionEvent[iEvent], 20000) == WAIT_FAILED) - { - NVENC_THROW_ERROR("Failed to encode frame", NV_ENC_ERR_GENERIC); - } -#endif -#endif -} - -uint32_t NvEncoder::GetWidthInBytes(const NV_ENC_BUFFER_FORMAT bufferFormat, const uint32_t width) -{ - switch (bufferFormat) { - case NV_ENC_BUFFER_FORMAT_NV12: - case NV_ENC_BUFFER_FORMAT_YV12: - case NV_ENC_BUFFER_FORMAT_IYUV: - case NV_ENC_BUFFER_FORMAT_YUV444: - return width; - case NV_ENC_BUFFER_FORMAT_YUV420_10BIT: - case NV_ENC_BUFFER_FORMAT_YUV444_10BIT: - return width * 2; - case NV_ENC_BUFFER_FORMAT_ARGB: - case NV_ENC_BUFFER_FORMAT_ARGB10: - case NV_ENC_BUFFER_FORMAT_AYUV: - case NV_ENC_BUFFER_FORMAT_ABGR: - case NV_ENC_BUFFER_FORMAT_ABGR10: - return width * 4; - default: - NVENC_THROW_ERROR("Invalid Buffer format", NV_ENC_ERR_INVALID_PARAM); - return 0; - } -} - -uint32_t NvEncoder::GetNumChromaPlanes(const NV_ENC_BUFFER_FORMAT bufferFormat) -{ - switch (bufferFormat) - { - case NV_ENC_BUFFER_FORMAT_NV12: - case NV_ENC_BUFFER_FORMAT_YUV420_10BIT: - return 1; - case NV_ENC_BUFFER_FORMAT_YV12: - case NV_ENC_BUFFER_FORMAT_IYUV: - case NV_ENC_BUFFER_FORMAT_YUV444: - case NV_ENC_BUFFER_FORMAT_YUV444_10BIT: - return 2; - case NV_ENC_BUFFER_FORMAT_ARGB: - case NV_ENC_BUFFER_FORMAT_ARGB10: - case NV_ENC_BUFFER_FORMAT_AYUV: - case NV_ENC_BUFFER_FORMAT_ABGR: - case NV_ENC_BUFFER_FORMAT_ABGR10: - return 0; - default: - NVENC_THROW_ERROR("Invalid Buffer format", NV_ENC_ERR_INVALID_PARAM); - return -1; - } -} - -uint32_t NvEncoder::GetChromaPitch(const NV_ENC_BUFFER_FORMAT bufferFormat,const uint32_t lumaPitch) -{ - switch (bufferFormat) - { - case NV_ENC_BUFFER_FORMAT_NV12: - case NV_ENC_BUFFER_FORMAT_YUV420_10BIT: - case NV_ENC_BUFFER_FORMAT_YUV444: - case NV_ENC_BUFFER_FORMAT_YUV444_10BIT: - return lumaPitch; - case NV_ENC_BUFFER_FORMAT_YV12: - case NV_ENC_BUFFER_FORMAT_IYUV: - return (lumaPitch + 1)/2; - case NV_ENC_BUFFER_FORMAT_ARGB: - case NV_ENC_BUFFER_FORMAT_ARGB10: - case NV_ENC_BUFFER_FORMAT_AYUV: - case NV_ENC_BUFFER_FORMAT_ABGR: - case NV_ENC_BUFFER_FORMAT_ABGR10: - return 0; - default: - NVENC_THROW_ERROR("Invalid Buffer format", NV_ENC_ERR_INVALID_PARAM); - return -1; - } -} - -void NvEncoder::GetChromaSubPlaneOffsets(const NV_ENC_BUFFER_FORMAT bufferFormat, const uint32_t pitch, const uint32_t height, std::vector& chromaOffsets) -{ - chromaOffsets.clear(); - switch (bufferFormat) - { - case NV_ENC_BUFFER_FORMAT_NV12: - case NV_ENC_BUFFER_FORMAT_YUV420_10BIT: - chromaOffsets.push_back(pitch * height); - return; - case NV_ENC_BUFFER_FORMAT_YV12: - case NV_ENC_BUFFER_FORMAT_IYUV: - chromaOffsets.push_back(pitch * height); - chromaOffsets.push_back(chromaOffsets[0] + (NvEncoder::GetChromaPitch(bufferFormat, pitch) * GetChromaHeight(bufferFormat, height))); - return; - case NV_ENC_BUFFER_FORMAT_YUV444: - case NV_ENC_BUFFER_FORMAT_YUV444_10BIT: - chromaOffsets.push_back(pitch * height); - chromaOffsets.push_back(chromaOffsets[0] + (pitch * height)); - return; - case NV_ENC_BUFFER_FORMAT_ARGB: - case NV_ENC_BUFFER_FORMAT_ARGB10: - case NV_ENC_BUFFER_FORMAT_AYUV: - case NV_ENC_BUFFER_FORMAT_ABGR: - case NV_ENC_BUFFER_FORMAT_ABGR10: - return; - default: - NVENC_THROW_ERROR("Invalid Buffer format", NV_ENC_ERR_INVALID_PARAM); - return; - } -} - -uint32_t NvEncoder::GetChromaHeight(const NV_ENC_BUFFER_FORMAT bufferFormat, const uint32_t lumaHeight) -{ - switch (bufferFormat) - { - case NV_ENC_BUFFER_FORMAT_YV12: - case NV_ENC_BUFFER_FORMAT_IYUV: - case NV_ENC_BUFFER_FORMAT_NV12: - case NV_ENC_BUFFER_FORMAT_YUV420_10BIT: - return (lumaHeight + 1)/2; - case NV_ENC_BUFFER_FORMAT_YUV444: - case NV_ENC_BUFFER_FORMAT_YUV444_10BIT: - return lumaHeight; - case NV_ENC_BUFFER_FORMAT_ARGB: - case NV_ENC_BUFFER_FORMAT_ARGB10: - case NV_ENC_BUFFER_FORMAT_AYUV: - case NV_ENC_BUFFER_FORMAT_ABGR: - case NV_ENC_BUFFER_FORMAT_ABGR10: - return 0; - default: - NVENC_THROW_ERROR("Invalid Buffer format", NV_ENC_ERR_INVALID_PARAM); - return 0; - } -} - -uint32_t NvEncoder::GetChromaWidthInBytes(const NV_ENC_BUFFER_FORMAT bufferFormat, const uint32_t lumaWidth) -{ - switch (bufferFormat) - { - case NV_ENC_BUFFER_FORMAT_YV12: - case NV_ENC_BUFFER_FORMAT_IYUV: - return (lumaWidth + 1) / 2; - case NV_ENC_BUFFER_FORMAT_NV12: - return lumaWidth; - case NV_ENC_BUFFER_FORMAT_YUV420_10BIT: - return 2 * lumaWidth; - case NV_ENC_BUFFER_FORMAT_YUV444: - return lumaWidth; - case NV_ENC_BUFFER_FORMAT_YUV444_10BIT: - return 2 * lumaWidth; - case NV_ENC_BUFFER_FORMAT_ARGB: - case NV_ENC_BUFFER_FORMAT_ARGB10: - case NV_ENC_BUFFER_FORMAT_AYUV: - case NV_ENC_BUFFER_FORMAT_ABGR: - case NV_ENC_BUFFER_FORMAT_ABGR10: - return 0; - default: - NVENC_THROW_ERROR("Invalid Buffer format", NV_ENC_ERR_INVALID_PARAM); - return 0; - } -} - - -int NvEncoder::GetCapabilityValue(GUID guidCodec, NV_ENC_CAPS capsToQuery) -{ - if (!m_hEncoder) - { - return 0; - } - NV_ENC_CAPS_PARAM capsParam = { NV_ENC_CAPS_PARAM_VER }; - capsParam.capsToQuery = capsToQuery; - int v; - m_nvenc.nvEncGetEncodeCaps(m_hEncoder, guidCodec, &capsParam, &v); - return v; -} - -int NvEncoder::GetFrameSize() const -{ - switch (GetPixelFormat()) - { - case NV_ENC_BUFFER_FORMAT_YV12: - case NV_ENC_BUFFER_FORMAT_IYUV: - case NV_ENC_BUFFER_FORMAT_NV12: - return GetEncodeWidth() * (GetEncodeHeight() + (GetEncodeHeight() + 1) / 2); - case NV_ENC_BUFFER_FORMAT_YUV420_10BIT: - return 2 * GetEncodeWidth() * (GetEncodeHeight() + (GetEncodeHeight() + 1) / 2); - case NV_ENC_BUFFER_FORMAT_YUV444: - return GetEncodeWidth() * GetEncodeHeight() * 3; - case NV_ENC_BUFFER_FORMAT_YUV444_10BIT: - return 2 * GetEncodeWidth() * GetEncodeHeight() * 3; - case NV_ENC_BUFFER_FORMAT_ARGB: - case NV_ENC_BUFFER_FORMAT_ARGB10: - case NV_ENC_BUFFER_FORMAT_AYUV: - case NV_ENC_BUFFER_FORMAT_ABGR: - case NV_ENC_BUFFER_FORMAT_ABGR10: - return 4 * GetEncodeWidth() * GetEncodeHeight(); - default: - NVENC_THROW_ERROR("Invalid Buffer format", NV_ENC_ERR_INVALID_PARAM); - return 0; - } -} - -void NvEncoder::GetInitializeParams(NV_ENC_INITIALIZE_PARAMS *pInitializeParams) -{ - if (!pInitializeParams || !pInitializeParams->encodeConfig) - { - NVENC_THROW_ERROR("Both pInitializeParams and pInitializeParams->encodeConfig can't be NULL", NV_ENC_ERR_INVALID_PTR); - } - NV_ENC_CONFIG *pEncodeConfig = pInitializeParams->encodeConfig; - *pEncodeConfig = m_encodeConfig; - *pInitializeParams = m_initializeParams; - pInitializeParams->encodeConfig = pEncodeConfig; -} - -void NvEncoder::InitializeBitstreamBuffer() -{ - for (int i = 0; i < m_nEncoderBuffer; i++) - { - NV_ENC_CREATE_BITSTREAM_BUFFER createBitstreamBuffer = { NV_ENC_CREATE_BITSTREAM_BUFFER_VER }; - NVENC_API_CALL(m_nvenc.nvEncCreateBitstreamBuffer(m_hEncoder, &createBitstreamBuffer)); - m_vBitstreamOutputBuffer[i] = createBitstreamBuffer.bitstreamBuffer; - } -} - -void NvEncoder::DestroyBitstreamBuffer() -{ - for (uint32_t i = 0; i < m_vBitstreamOutputBuffer.size(); i++) - { - if (m_vBitstreamOutputBuffer[i]) - { - m_nvenc.nvEncDestroyBitstreamBuffer(m_hEncoder, m_vBitstreamOutputBuffer[i]); - } - } - - m_vBitstreamOutputBuffer.clear(); -} - -void NvEncoder::InitializeMVOutputBuffer() -{ - for (int i = 0; i < m_nEncoderBuffer; i++) - { - NV_ENC_CREATE_MV_BUFFER createMVBuffer = { NV_ENC_CREATE_MV_BUFFER_VER }; - NVENC_API_CALL(m_nvenc.nvEncCreateMVBuffer(m_hEncoder, &createMVBuffer)); - m_vMVDataOutputBuffer.push_back(createMVBuffer.mvBuffer); - } -} - -void NvEncoder::DestroyMVOutputBuffer() -{ - for (uint32_t i = 0; i < m_vMVDataOutputBuffer.size(); i++) - { - if (m_vMVDataOutputBuffer[i]) - { - m_nvenc.nvEncDestroyMVBuffer(m_hEncoder, m_vMVDataOutputBuffer[i]); - } - } - - m_vMVDataOutputBuffer.clear(); -} - -NVENCSTATUS NvEncoder::DoMotionEstimation(NV_ENC_INPUT_PTR inputBuffer, NV_ENC_INPUT_PTR inputBufferForReference, NV_ENC_OUTPUT_PTR outputBuffer) -{ - NV_ENC_MEONLY_PARAMS meParams = { NV_ENC_MEONLY_PARAMS_VER }; - meParams.inputBuffer = inputBuffer; - meParams.referenceFrame = inputBufferForReference; - meParams.inputWidth = GetEncodeWidth(); - meParams.inputHeight = GetEncodeHeight(); - meParams.mvBuffer = outputBuffer; - meParams.completionEvent = GetCompletionEvent(m_iToSend % m_nEncoderBuffer); - NVENCSTATUS nvStatus = m_nvenc.nvEncRunMotionEstimationOnly(m_hEncoder, &meParams); - - return nvStatus; -} +/* +* Copyright 2017-2022 NVIDIA Corporation. All rights reserved. +* +* Please refer to the NVIDIA end user license agreement (EULA) associated +* with this source code for terms and conditions that govern your use of +* this software. Any use, reproduction, disclosure, or distribution of +* this software and related documentation outside the terms of the EULA +* is strictly prohibited. +* +*/ + +#include "NvEncoder/NvEncoder.h" + +#ifndef _WIN32 +#include +static inline bool operator==(const GUID& guid1, const GUID& guid2) { + return !memcmp(&guid1, &guid2, sizeof(GUID)); +} + +static inline bool operator!=(const GUID& guid1, const GUID& guid2) { + return !(guid1 == guid2); +} +#endif + +NvEncoder::NvEncoder(NV_ENC_DEVICE_TYPE eDeviceType, + void* pDevice, + uint32_t nWidth, + uint32_t nHeight, + NV_ENC_BUFFER_FORMAT eBufferFormat, + uint32_t nExtraOutputDelay, + bool bMotionEstimationOnly, + bool bOutputInVideoMemory, + bool bDX12Encode, + bool bUseIVFContainer) + : m_pDevice(pDevice), + m_eDeviceType(eDeviceType), + m_nWidth(nWidth), + m_nHeight(nHeight), + m_nMaxEncodeWidth(nWidth), + m_nMaxEncodeHeight(nHeight), + m_eBufferFormat(eBufferFormat), + m_bMotionEstimationOnly(bMotionEstimationOnly), + m_bOutputInVideoMemory(bOutputInVideoMemory), + m_bIsDX12Encode(bDX12Encode), + m_bUseIVFContainer(bUseIVFContainer), + m_nExtraOutputDelay(nExtraOutputDelay), + m_hEncoder(nullptr) { + LoadNvEncApi(); + + if (!m_nvenc.nvEncOpenEncodeSession) { + m_nEncoderBuffer = 0; + NVENC_THROW_ERROR("EncodeAPI not found", NV_ENC_ERR_NO_ENCODE_DEVICE); + } + + NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS encodeSessionExParams = { + NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER}; + encodeSessionExParams.device = m_pDevice; + encodeSessionExParams.deviceType = m_eDeviceType; + encodeSessionExParams.apiVersion = NVENCAPI_VERSION; + void* hEncoder = NULL; + NVENC_API_CALL( + m_nvenc.nvEncOpenEncodeSessionEx(&encodeSessionExParams, &hEncoder)); + m_hEncoder = hEncoder; +} + +void NvEncoder::TryLoadNvEncApi() { +#if defined(_WIN32) +#if defined(_WIN64) + HMODULE hModule = LoadLibrary(TEXT("nvEncodeAPI64.dll")); +#else + HMODULE hModule = LoadLibrary(TEXT("nvEncodeAPI.dll")); +#endif +#else + void* hModule = dlopen("libnvidia-encode.so.1", RTLD_LAZY); +#endif + + if (hModule == NULL) { + NVENC_THROW_ERROR( + "NVENC library file is not found. Please ensure NV driver is installed", + NV_ENC_ERR_NO_ENCODE_DEVICE); + } + + typedef NVENCSTATUS(NVENCAPI * + NvEncodeAPIGetMaxSupportedVersion_Type)(uint32_t*); +#if defined(_WIN32) + NvEncodeAPIGetMaxSupportedVersion_Type NvEncodeAPIGetMaxSupportedVersion = + (NvEncodeAPIGetMaxSupportedVersion_Type)GetProcAddress( + hModule, "NvEncodeAPIGetMaxSupportedVersion"); +#else + NvEncodeAPIGetMaxSupportedVersion_Type NvEncodeAPIGetMaxSupportedVersion = + (NvEncodeAPIGetMaxSupportedVersion_Type)dlsym( + hModule, "NvEncodeAPIGetMaxSupportedVersion"); +#endif + if (NvEncodeAPIGetMaxSupportedVersion == NULL) { + NVENC_THROW_ERROR( + "NvEncodeAPIGetMaxSupportedVersion function is not exported", + NV_ENC_ERR_NO_ENCODE_DEVICE); + } + + uint32_t version = 0; + uint32_t currentVersion = + (NVENCAPI_MAJOR_VERSION << 4) | NVENCAPI_MINOR_VERSION; + NVENC_API_CALL(NvEncodeAPIGetMaxSupportedVersion(&version)); + if (currentVersion > version) { +#if defined(_WIN32) + FreeLibrary((HMODULE)hModule); +#else + dlclose(hModule); +#endif + NVENC_THROW_ERROR( + "Current Driver Version does not support this NvEncodeAPI version, " + "please upgrade driver", + NV_ENC_ERR_INVALID_VERSION); + } +#if defined(_WIN32) + FreeLibrary((HMODULE)hModule); +#else + dlclose(hModule); +#endif +} + +void NvEncoder::LoadNvEncApi() { +#if defined(_WIN32) +#if defined(_WIN64) + HMODULE hModule = LoadLibrary(TEXT("nvEncodeAPI64.dll")); +#else + HMODULE hModule = LoadLibrary(TEXT("nvEncodeAPI.dll")); +#endif +#else + void* hModule = dlopen("libnvidia-encode.so.1", RTLD_LAZY); +#endif + + if (hModule == NULL) { + NVENC_THROW_ERROR( + "NVENC library file is not found. Please ensure NV driver is installed", + NV_ENC_ERR_NO_ENCODE_DEVICE); + } + + m_hModule = hModule; + + typedef NVENCSTATUS(NVENCAPI * + NvEncodeAPIGetMaxSupportedVersion_Type)(uint32_t*); +#if defined(_WIN32) + NvEncodeAPIGetMaxSupportedVersion_Type NvEncodeAPIGetMaxSupportedVersion = + (NvEncodeAPIGetMaxSupportedVersion_Type)GetProcAddress( + hModule, "NvEncodeAPIGetMaxSupportedVersion"); +#else + NvEncodeAPIGetMaxSupportedVersion_Type NvEncodeAPIGetMaxSupportedVersion = + (NvEncodeAPIGetMaxSupportedVersion_Type)dlsym( + hModule, "NvEncodeAPIGetMaxSupportedVersion"); +#endif + if (NvEncodeAPIGetMaxSupportedVersion == NULL) { + NVENC_THROW_ERROR( + "NvEncodeAPIGetMaxSupportedVersion function is not exported", + NV_ENC_ERR_NO_ENCODE_DEVICE); + } + + uint32_t version = 0; + uint32_t currentVersion = + (NVENCAPI_MAJOR_VERSION << 4) | NVENCAPI_MINOR_VERSION; + NVENC_API_CALL(NvEncodeAPIGetMaxSupportedVersion(&version)); + if (currentVersion > version) { + NVENC_THROW_ERROR( + "Current Driver Version does not support this NvEncodeAPI version, " + "please upgrade driver", + NV_ENC_ERR_INVALID_VERSION); + } + + typedef NVENCSTATUS(NVENCAPI *NvEncodeAPICreateInstance_Type)(NV_ENCODE_API_FUNCTION_LIST*); +#if defined(_WIN32) + NvEncodeAPICreateInstance_Type NvEncodeAPICreateInstance = + (NvEncodeAPICreateInstance_Type)GetProcAddress(hModule, "NvEncodeAPICreateInstance"); +#else + NvEncodeAPICreateInstance_Type NvEncodeAPICreateInstance = + (NvEncodeAPICreateInstance_Type)dlsym(hModule, "NvEncodeAPICreateInstance"); +#endif + + if (!NvEncodeAPICreateInstance) { + NVENC_THROW_ERROR( + "Cannot find NvEncodeAPICreateInstance() entry in NVENC library", + NV_ENC_ERR_NO_ENCODE_DEVICE); + } + + m_nvenc = {NV_ENCODE_API_FUNCTION_LIST_VER}; + NVENC_API_CALL(NvEncodeAPICreateInstance(&m_nvenc)); +} + +NvEncoder::~NvEncoder() { + DestroyHWEncoder(); + + if (m_hModule) { +#if defined(_WIN32) + FreeLibrary((HMODULE)m_hModule); +#else + dlclose(m_hModule); +#endif + m_hModule = nullptr; + } +} + +void NvEncoder::CreateDefaultEncoderParams( + NV_ENC_INITIALIZE_PARAMS* pIntializeParams, + GUID codecGuid, + GUID presetGuid, + NV_ENC_TUNING_INFO tuningInfo) { + if (!m_hEncoder) { + NVENC_THROW_ERROR("Encoder Initialization failed", + NV_ENC_ERR_NO_ENCODE_DEVICE); + return; + } + + if (pIntializeParams == nullptr || + pIntializeParams->encodeConfig == nullptr) { + NVENC_THROW_ERROR( + "pInitializeParams and pInitializeParams->encodeConfig can't be NULL", + NV_ENC_ERR_INVALID_PTR); + } + + memset(pIntializeParams->encodeConfig, 0, sizeof(NV_ENC_CONFIG)); + auto pEncodeConfig = pIntializeParams->encodeConfig; + memset(pIntializeParams, 0, sizeof(NV_ENC_INITIALIZE_PARAMS)); + pIntializeParams->encodeConfig = pEncodeConfig; + + pIntializeParams->encodeConfig->version = NV_ENC_CONFIG_VER; + pIntializeParams->version = NV_ENC_INITIALIZE_PARAMS_VER; + + pIntializeParams->encodeGUID = codecGuid; + pIntializeParams->presetGUID = presetGuid; + pIntializeParams->encodeWidth = m_nWidth; + pIntializeParams->encodeHeight = m_nHeight; + pIntializeParams->darWidth = m_nWidth; + pIntializeParams->darHeight = m_nHeight; + pIntializeParams->frameRateNum = 30; + pIntializeParams->frameRateDen = 1; + pIntializeParams->enablePTD = 1; + pIntializeParams->reportSliceOffsets = 0; + pIntializeParams->enableSubFrameWrite = 0; + pIntializeParams->maxEncodeWidth = m_nWidth; + pIntializeParams->maxEncodeHeight = m_nHeight; + pIntializeParams->enableMEOnlyMode = m_bMotionEstimationOnly; + pIntializeParams->enableOutputInVidmem = m_bOutputInVideoMemory; +#if defined(_WIN32) + if (!m_bOutputInVideoMemory) { + pIntializeParams->enableEncodeAsync = + GetCapabilityValue(codecGuid, NV_ENC_CAPS_ASYNC_ENCODE_SUPPORT); + } +#endif + + NV_ENC_PRESET_CONFIG presetConfig = {NV_ENC_PRESET_CONFIG_VER, + {NV_ENC_CONFIG_VER}}; + m_nvenc.nvEncGetEncodePresetConfig(m_hEncoder, codecGuid, presetGuid, + &presetConfig); + memcpy(pIntializeParams->encodeConfig, &presetConfig.presetCfg, + sizeof(NV_ENC_CONFIG)); + pIntializeParams->encodeConfig->frameIntervalP = 1; + pIntializeParams->encodeConfig->gopLength = NVENC_INFINITE_GOPLENGTH; + + pIntializeParams->encodeConfig->rcParams.rateControlMode = + NV_ENC_PARAMS_RC_CONSTQP; + + if (!m_bMotionEstimationOnly) { + pIntializeParams->tuningInfo = tuningInfo; + NV_ENC_PRESET_CONFIG presetConfig = {NV_ENC_PRESET_CONFIG_VER, + {NV_ENC_CONFIG_VER}}; + m_nvenc.nvEncGetEncodePresetConfigEx(m_hEncoder, codecGuid, presetGuid, + tuningInfo, &presetConfig); + memcpy(pIntializeParams->encodeConfig, &presetConfig.presetCfg, + sizeof(NV_ENC_CONFIG)); + } else { + m_encodeConfig.version = NV_ENC_CONFIG_VER; + m_encodeConfig.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CONSTQP; + m_encodeConfig.rcParams.constQP = {28, 31, 25}; + } + + if (pIntializeParams->encodeGUID == NV_ENC_CODEC_H264_GUID) { + if (m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444 || + m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444_10BIT) { + pIntializeParams->encodeConfig->encodeCodecConfig.h264Config + .chromaFormatIDC = 3; + } + pIntializeParams->encodeConfig->encodeCodecConfig.h264Config.idrPeriod = + pIntializeParams->encodeConfig->gopLength; + } else if (pIntializeParams->encodeGUID == NV_ENC_CODEC_HEVC_GUID) { + pIntializeParams->encodeConfig->encodeCodecConfig.hevcConfig + .pixelBitDepthMinus8 = + (m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV420_10BIT || + m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444_10BIT) + ? 2 + : 0; + if (m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444 || + m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444_10BIT) { + pIntializeParams->encodeConfig->encodeCodecConfig.hevcConfig + .chromaFormatIDC = 3; + } + pIntializeParams->encodeConfig->encodeCodecConfig.hevcConfig.idrPeriod = + pIntializeParams->encodeConfig->gopLength; + } else if (pIntializeParams->encodeGUID == NV_ENC_CODEC_AV1_GUID) { + pIntializeParams->encodeConfig->encodeCodecConfig.av1Config + .pixelBitDepthMinus8 = + (m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV420_10BIT) ? 2 : 0; + pIntializeParams->encodeConfig->encodeCodecConfig.av1Config + .inputPixelBitDepthMinus8 = + (m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV420_10BIT) ? 2 : 0; + pIntializeParams->encodeConfig->encodeCodecConfig.av1Config + .chromaFormatIDC = 1; + pIntializeParams->encodeConfig->encodeCodecConfig.av1Config.idrPeriod = + pIntializeParams->encodeConfig->gopLength; + if (m_bOutputInVideoMemory) { + pIntializeParams->encodeConfig->frameIntervalP = 1; + } + } + + if (m_bIsDX12Encode) { + pIntializeParams->bufferFormat = m_eBufferFormat; + } + + return; +} + +void NvEncoder::CreateEncoder(const NV_ENC_INITIALIZE_PARAMS* pEncoderParams) { + if (!m_hEncoder) { + NVENC_THROW_ERROR("Encoder Initialization failed", + NV_ENC_ERR_NO_ENCODE_DEVICE); + } + + if (!pEncoderParams) { + NVENC_THROW_ERROR("Invalid NV_ENC_INITIALIZE_PARAMS ptr", + NV_ENC_ERR_INVALID_PTR); + } + + if (pEncoderParams->encodeWidth == 0 || pEncoderParams->encodeHeight == 0) { + NVENC_THROW_ERROR("Invalid encoder width and height", + NV_ENC_ERR_INVALID_PARAM); + } + + if (pEncoderParams->encodeGUID != NV_ENC_CODEC_H264_GUID && + pEncoderParams->encodeGUID != NV_ENC_CODEC_HEVC_GUID && + pEncoderParams->encodeGUID != NV_ENC_CODEC_AV1_GUID) { + NVENC_THROW_ERROR("Invalid codec guid", NV_ENC_ERR_INVALID_PARAM); + } + + if (pEncoderParams->encodeGUID == NV_ENC_CODEC_H264_GUID) { + if (m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV420_10BIT || + m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444_10BIT) { + NVENC_THROW_ERROR("10-bit format isn't supported by H264 encoder", + NV_ENC_ERR_INVALID_PARAM); + } + } + + if (pEncoderParams->encodeGUID == NV_ENC_CODEC_AV1_GUID) { + if (m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444 || + m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444_10BIT) { + NVENC_THROW_ERROR("YUV444 format isn't supported by AV1 encoder", + NV_ENC_ERR_INVALID_PARAM); + } + } + + // set other necessary params if not set yet + if (pEncoderParams->encodeGUID == NV_ENC_CODEC_H264_GUID) { + if ((m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444) && + (pEncoderParams->encodeConfig->encodeCodecConfig.h264Config + .chromaFormatIDC != 3)) { + NVENC_THROW_ERROR("Invalid ChromaFormatIDC", NV_ENC_ERR_INVALID_PARAM); + } + } + + if (pEncoderParams->encodeGUID == NV_ENC_CODEC_HEVC_GUID) { + bool yuv10BitFormat = + (m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV420_10BIT || + m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444_10BIT) + ? true + : false; + if (yuv10BitFormat && pEncoderParams->encodeConfig->encodeCodecConfig + .hevcConfig.pixelBitDepthMinus8 != 2) { + NVENC_THROW_ERROR("Invalid PixelBitdepth", NV_ENC_ERR_INVALID_PARAM); + } + + if ((m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444 || + m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444_10BIT) && + (pEncoderParams->encodeConfig->encodeCodecConfig.hevcConfig + .chromaFormatIDC != 3)) { + NVENC_THROW_ERROR("Invalid ChromaFormatIDC", NV_ENC_ERR_INVALID_PARAM); + } + } + + if (pEncoderParams->encodeGUID == NV_ENC_CODEC_AV1_GUID) { + bool yuv10BitFormat = + (m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV420_10BIT) ? true : false; + if (yuv10BitFormat && pEncoderParams->encodeConfig->encodeCodecConfig + .av1Config.pixelBitDepthMinus8 != 2) { + NVENC_THROW_ERROR("Invalid PixelBitdepth", NV_ENC_ERR_INVALID_PARAM); + } + + if (pEncoderParams->encodeConfig->encodeCodecConfig.av1Config + .chromaFormatIDC != 1) { + NVENC_THROW_ERROR("Invalid ChromaFormatIDC", NV_ENC_ERR_INVALID_PARAM); + } + + if (m_bOutputInVideoMemory && + pEncoderParams->encodeConfig->frameIntervalP > 1) { + NVENC_THROW_ERROR( + "Alt Ref frames not supported for AV1 in case of OutputInVideoMemory", + NV_ENC_ERR_INVALID_PARAM); + } + } + + memcpy(&m_initializeParams, pEncoderParams, sizeof(m_initializeParams)); + m_initializeParams.version = NV_ENC_INITIALIZE_PARAMS_VER; + + if (pEncoderParams->encodeConfig) { + memcpy(&m_encodeConfig, pEncoderParams->encodeConfig, + sizeof(m_encodeConfig)); + m_encodeConfig.version = NV_ENC_CONFIG_VER; + } else { + NV_ENC_PRESET_CONFIG presetConfig = {NV_ENC_PRESET_CONFIG_VER, + {NV_ENC_CONFIG_VER}}; + if (!m_bMotionEstimationOnly) { + m_nvenc.nvEncGetEncodePresetConfigEx( + m_hEncoder, pEncoderParams->encodeGUID, pEncoderParams->presetGUID, + pEncoderParams->tuningInfo, &presetConfig); + memcpy(&m_encodeConfig, &presetConfig.presetCfg, sizeof(NV_ENC_CONFIG)); + if (m_bOutputInVideoMemory && + pEncoderParams->encodeGUID == NV_ENC_CODEC_AV1_GUID) { + m_encodeConfig.frameIntervalP = 1; + } + } else { + m_encodeConfig.version = NV_ENC_CONFIG_VER; + m_encodeConfig.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CONSTQP; + m_encodeConfig.rcParams.constQP = {28, 31, 25}; + } + } + m_initializeParams.encodeConfig = &m_encodeConfig; + + NVENC_API_CALL( + m_nvenc.nvEncInitializeEncoder(m_hEncoder, &m_initializeParams)); + + m_bEncoderInitialized = true; + m_nWidth = m_initializeParams.encodeWidth; + m_nHeight = m_initializeParams.encodeHeight; + m_nMaxEncodeWidth = m_initializeParams.maxEncodeWidth; + m_nMaxEncodeHeight = m_initializeParams.maxEncodeHeight; + + m_nEncoderBuffer = m_encodeConfig.frameIntervalP + + m_encodeConfig.rcParams.lookaheadDepth + + m_nExtraOutputDelay; + m_nOutputDelay = m_nEncoderBuffer - 1; + + if (!m_bOutputInVideoMemory) { + m_vpCompletionEvent.resize(m_nEncoderBuffer, nullptr); + } + +#if defined(_WIN32) + for (uint32_t i = 0; i < m_vpCompletionEvent.size(); i++) { + m_vpCompletionEvent[i] = CreateEvent(NULL, FALSE, FALSE, NULL); + if (!m_bIsDX12Encode) { + NV_ENC_EVENT_PARAMS eventParams = {NV_ENC_EVENT_PARAMS_VER}; + eventParams.completionEvent = m_vpCompletionEvent[i]; + m_nvenc.nvEncRegisterAsyncEvent(m_hEncoder, &eventParams); + } + } +#endif + + m_vMappedInputBuffers.resize(m_nEncoderBuffer, nullptr); + + if (m_bMotionEstimationOnly) { + m_vMappedRefBuffers.resize(m_nEncoderBuffer, nullptr); + + if (!m_bOutputInVideoMemory) { + InitializeMVOutputBuffer(); + } + } else { + if (!m_bOutputInVideoMemory && !m_bIsDX12Encode) { + m_vBitstreamOutputBuffer.resize(m_nEncoderBuffer, nullptr); + InitializeBitstreamBuffer(); + } + } + + AllocateInputBuffers(m_nEncoderBuffer); +} + +void NvEncoder::DestroyEncoder() { + if (!m_hEncoder) { + return; + } + + ReleaseInputBuffers(); + + DestroyHWEncoder(); +} + +void NvEncoder::DestroyHWEncoder() { + if (!m_hEncoder) { + return; + } + +#if defined(_WIN32) + for (uint32_t i = 0; i < m_vpCompletionEvent.size(); i++) { + if (m_vpCompletionEvent[i]) { + if (!m_bIsDX12Encode) { + NV_ENC_EVENT_PARAMS eventParams = {NV_ENC_EVENT_PARAMS_VER}; + eventParams.completionEvent = m_vpCompletionEvent[i]; + m_nvenc.nvEncUnregisterAsyncEvent(m_hEncoder, &eventParams); + } + CloseHandle(m_vpCompletionEvent[i]); + } + } + m_vpCompletionEvent.clear(); +#endif + + if (m_bMotionEstimationOnly) { + DestroyMVOutputBuffer(); + } else { + if (!m_bIsDX12Encode) + DestroyBitstreamBuffer(); + } + + m_nvenc.nvEncDestroyEncoder(m_hEncoder); + + m_hEncoder = nullptr; + + m_bEncoderInitialized = false; +} + +const NvEncInputFrame* NvEncoder::GetNextInputFrame() { + int i = m_iToSend % m_nEncoderBuffer; + return &m_vInputFrames[i]; +} + +const NvEncInputFrame* NvEncoder::GetNextReferenceFrame() { + int i = m_iToSend % m_nEncoderBuffer; + return &m_vReferenceFrames[i]; +} + +void NvEncoder::MapResources(uint32_t bfrIdx) { + NV_ENC_MAP_INPUT_RESOURCE mapInputResource = {NV_ENC_MAP_INPUT_RESOURCE_VER}; + + mapInputResource.registeredResource = m_vRegisteredResources[bfrIdx]; + NVENC_API_CALL(m_nvenc.nvEncMapInputResource(m_hEncoder, &mapInputResource)); + m_vMappedInputBuffers[bfrIdx] = mapInputResource.mappedResource; + + if (m_bMotionEstimationOnly) { + mapInputResource.registeredResource = + m_vRegisteredResourcesForReference[bfrIdx]; + NVENC_API_CALL( + m_nvenc.nvEncMapInputResource(m_hEncoder, &mapInputResource)); + m_vMappedRefBuffers[bfrIdx] = mapInputResource.mappedResource; + } +} + +void NvEncoder::EncodeFrame(std::vector>& vPacket, + NV_ENC_PIC_PARAMS* pPicParams) { + vPacket.clear(); + if (!IsHWEncoderInitialized()) { + NVENC_THROW_ERROR("Encoder device not found", NV_ENC_ERR_NO_ENCODE_DEVICE); + } + + int bfrIdx = m_iToSend % m_nEncoderBuffer; + + MapResources(bfrIdx); + + NVENCSTATUS nvStatus = DoEncode(m_vMappedInputBuffers[bfrIdx], + m_vBitstreamOutputBuffer[bfrIdx], pPicParams); + + if (nvStatus == NV_ENC_SUCCESS || nvStatus == NV_ENC_ERR_NEED_MORE_INPUT) { + m_iToSend++; + GetEncodedPacket(m_vBitstreamOutputBuffer, vPacket, true); + } else { + NVENC_THROW_ERROR("nvEncEncodePicture API failed", nvStatus); + } +} + +void NvEncoder::RunMotionEstimation(std::vector& mvData) { + if (!m_hEncoder) { + NVENC_THROW_ERROR("Encoder Initialization failed", + NV_ENC_ERR_NO_ENCODE_DEVICE); + return; + } + + const uint32_t bfrIdx = m_iToSend % m_nEncoderBuffer; + + MapResources(bfrIdx); + + NVENCSTATUS nvStatus = DoMotionEstimation(m_vMappedInputBuffers[bfrIdx], + m_vMappedRefBuffers[bfrIdx], + m_vMVDataOutputBuffer[bfrIdx]); + + if (nvStatus == NV_ENC_SUCCESS) { + m_iToSend++; + std::vector> vPacket; + GetEncodedPacket(m_vMVDataOutputBuffer, vPacket, true); + if (vPacket.size() != 1) { + NVENC_THROW_ERROR( + "GetEncodedPacket() doesn't return one (and only one) MVData", + NV_ENC_ERR_GENERIC); + } + mvData = vPacket[0]; + } else { + NVENC_THROW_ERROR("nvEncEncodePicture API failed", nvStatus); + } +} + +void NvEncoder::GetSequenceParams(std::vector& seqParams) { + uint8_t spsppsData[1024]; // Assume maximum spspps data is 1KB or less + memset(spsppsData, 0, sizeof(spsppsData)); + NV_ENC_SEQUENCE_PARAM_PAYLOAD payload = {NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER}; + uint32_t spsppsSize = 0; + + payload.spsppsBuffer = spsppsData; + payload.inBufferSize = sizeof(spsppsData); + payload.outSPSPPSPayloadSize = &spsppsSize; + NVENC_API_CALL(m_nvenc.nvEncGetSequenceParams(m_hEncoder, &payload)); + seqParams.clear(); + seqParams.insert(seqParams.end(), &spsppsData[0], &spsppsData[spsppsSize]); +} + +NVENCSTATUS NvEncoder::DoEncode(NV_ENC_INPUT_PTR inputBuffer, + NV_ENC_OUTPUT_PTR outputBuffer, + NV_ENC_PIC_PARAMS* pPicParams) { + NV_ENC_PIC_PARAMS picParams = {}; + if (pPicParams) { + picParams = *pPicParams; + } + picParams.version = NV_ENC_PIC_PARAMS_VER; + picParams.pictureStruct = NV_ENC_PIC_STRUCT_FRAME; + picParams.inputBuffer = inputBuffer; + picParams.bufferFmt = GetPixelFormat(); + picParams.inputWidth = GetEncodeWidth(); + picParams.inputHeight = GetEncodeHeight(); + picParams.outputBitstream = outputBuffer; + picParams.completionEvent = GetCompletionEvent(m_iToSend % m_nEncoderBuffer); + NVENCSTATUS nvStatus = m_nvenc.nvEncEncodePicture(m_hEncoder, &picParams); + + return nvStatus; +} + +void NvEncoder::SendEOS() { + NV_ENC_PIC_PARAMS picParams = {NV_ENC_PIC_PARAMS_VER}; + picParams.encodePicFlags = NV_ENC_PIC_FLAG_EOS; + picParams.completionEvent = GetCompletionEvent(m_iToSend % m_nEncoderBuffer); + NVENC_API_CALL(m_nvenc.nvEncEncodePicture(m_hEncoder, &picParams)); +} + +void NvEncoder::EndEncode(std::vector>& vPacket) { + vPacket.clear(); + if (!IsHWEncoderInitialized()) { + NVENC_THROW_ERROR("Encoder device not initialized", + NV_ENC_ERR_ENCODER_NOT_INITIALIZED); + } + + SendEOS(); + + GetEncodedPacket(m_vBitstreamOutputBuffer, vPacket, false); +} + +void NvEncoder::GetEncodedPacket(std::vector& vOutputBuffer, + std::vector>& vPacket, + bool bOutputDelay) { + unsigned i = 0; + int iEnd = bOutputDelay ? m_iToSend - m_nOutputDelay : m_iToSend; + for (; m_iGot < iEnd; m_iGot++) { + WaitForCompletionEvent(m_iGot % m_nEncoderBuffer); + NV_ENC_LOCK_BITSTREAM lockBitstreamData = {NV_ENC_LOCK_BITSTREAM_VER}; + lockBitstreamData.outputBitstream = + vOutputBuffer[m_iGot % m_nEncoderBuffer]; + lockBitstreamData.doNotWait = false; + NVENC_API_CALL(m_nvenc.nvEncLockBitstream(m_hEncoder, &lockBitstreamData)); + + uint8_t* pData = (uint8_t*)lockBitstreamData.bitstreamBufferPtr; + if (vPacket.size() < i + 1) { + vPacket.push_back(std::vector()); + } + vPacket[i].clear(); + + if ((m_initializeParams.encodeGUID == NV_ENC_CODEC_AV1_GUID) && + (m_bUseIVFContainer)) { + if (m_bWriteIVFFileHeader) { + m_IVFUtils.WriteFileHeader(vPacket[i], MAKE_FOURCC('A', 'V', '0', '1'), + m_initializeParams.encodeWidth, + m_initializeParams.encodeHeight, + m_initializeParams.frameRateNum, + m_initializeParams.frameRateDen, 0xFFFF); + m_bWriteIVFFileHeader = false; + } + + m_IVFUtils.WriteFrameHeader(vPacket[i], + lockBitstreamData.bitstreamSizeInBytes, + lockBitstreamData.outputTimeStamp); + } + vPacket[i].insert(vPacket[i].end(), &pData[0], + &pData[lockBitstreamData.bitstreamSizeInBytes]); + + i++; + + NVENC_API_CALL(m_nvenc.nvEncUnlockBitstream( + m_hEncoder, lockBitstreamData.outputBitstream)); + + if (m_vMappedInputBuffers[m_iGot % m_nEncoderBuffer]) { + NVENC_API_CALL(m_nvenc.nvEncUnmapInputResource( + m_hEncoder, m_vMappedInputBuffers[m_iGot % m_nEncoderBuffer])); + m_vMappedInputBuffers[m_iGot % m_nEncoderBuffer] = nullptr; + } + + if (m_bMotionEstimationOnly && + m_vMappedRefBuffers[m_iGot % m_nEncoderBuffer]) { + NVENC_API_CALL(m_nvenc.nvEncUnmapInputResource( + m_hEncoder, m_vMappedRefBuffers[m_iGot % m_nEncoderBuffer])); + m_vMappedRefBuffers[m_iGot % m_nEncoderBuffer] = nullptr; + } + } +} + +bool NvEncoder::Reconfigure( + const NV_ENC_RECONFIGURE_PARAMS* pReconfigureParams) { + NVENC_API_CALL(m_nvenc.nvEncReconfigureEncoder( + m_hEncoder, const_cast(pReconfigureParams))); + + memcpy(&m_initializeParams, &(pReconfigureParams->reInitEncodeParams), + sizeof(m_initializeParams)); + if (pReconfigureParams->reInitEncodeParams.encodeConfig) { + memcpy(&m_encodeConfig, pReconfigureParams->reInitEncodeParams.encodeConfig, + sizeof(m_encodeConfig)); + } + + m_nWidth = m_initializeParams.encodeWidth; + m_nHeight = m_initializeParams.encodeHeight; + m_nMaxEncodeWidth = m_initializeParams.maxEncodeWidth; + m_nMaxEncodeHeight = m_initializeParams.maxEncodeHeight; + + return true; +} + +NV_ENC_REGISTERED_PTR NvEncoder::RegisterResource( + void* pBuffer, + NV_ENC_INPUT_RESOURCE_TYPE eResourceType, + int width, + int height, + int pitch, + NV_ENC_BUFFER_FORMAT bufferFormat, + NV_ENC_BUFFER_USAGE bufferUsage, + NV_ENC_FENCE_POINT_D3D12* pInputFencePoint) { + NV_ENC_REGISTER_RESOURCE registerResource = {NV_ENC_REGISTER_RESOURCE_VER}; + registerResource.resourceType = eResourceType; + registerResource.resourceToRegister = pBuffer; + registerResource.width = width; + registerResource.height = height; + registerResource.pitch = pitch; + registerResource.bufferFormat = bufferFormat; + registerResource.bufferUsage = bufferUsage; + registerResource.pInputFencePoint = pInputFencePoint; + NVENC_API_CALL(m_nvenc.nvEncRegisterResource(m_hEncoder, ®isterResource)); + + return registerResource.registeredResource; +} + +void NvEncoder::RegisterInputResources(std::vector inputframes, + NV_ENC_INPUT_RESOURCE_TYPE eResourceType, + int width, + int height, + int pitch, + NV_ENC_BUFFER_FORMAT bufferFormat, + bool bReferenceFrame) { + for (uint32_t i = 0; i < inputframes.size(); ++i) { + NV_ENC_REGISTERED_PTR registeredPtr = + RegisterResource(inputframes[i], eResourceType, width, height, pitch, + bufferFormat, NV_ENC_INPUT_IMAGE); + + std::vector _chromaOffsets; + NvEncoder::GetChromaSubPlaneOffsets(bufferFormat, pitch, height, + _chromaOffsets); + NvEncInputFrame inputframe = {}; + inputframe.inputPtr = (void*)inputframes[i]; + inputframe.chromaOffsets[0] = 0; + inputframe.chromaOffsets[1] = 0; + for (uint32_t ch = 0; ch < _chromaOffsets.size(); ch++) { + inputframe.chromaOffsets[ch] = _chromaOffsets[ch]; + } + inputframe.numChromaPlanes = NvEncoder::GetNumChromaPlanes(bufferFormat); + inputframe.pitch = pitch; + inputframe.chromaPitch = NvEncoder::GetChromaPitch(bufferFormat, pitch); + inputframe.bufferFormat = bufferFormat; + inputframe.resourceType = eResourceType; + + if (bReferenceFrame) { + m_vRegisteredResourcesForReference.push_back(registeredPtr); + m_vReferenceFrames.push_back(inputframe); + } else { + m_vRegisteredResources.push_back(registeredPtr); + m_vInputFrames.push_back(inputframe); + } + } +} + +void NvEncoder::FlushEncoder() { + if (!m_bMotionEstimationOnly && !m_bOutputInVideoMemory) { + // Incase of error it is possible for buffers still mapped to encoder. + // flush the encoder queue and then unmapped it if any surface is still mapped + try { + std::vector> vPacket; + EndEncode(vPacket); + } catch (...) { + } + } +} + +void NvEncoder::UnregisterInputResources() { + FlushEncoder(); + + if (m_bMotionEstimationOnly) { + for (uint32_t i = 0; i < m_vMappedRefBuffers.size(); ++i) { + if (m_vMappedRefBuffers[i]) { + m_nvenc.nvEncUnmapInputResource(m_hEncoder, m_vMappedRefBuffers[i]); + } + } + } + m_vMappedRefBuffers.clear(); + + for (uint32_t i = 0; i < m_vMappedInputBuffers.size(); ++i) { + if (m_vMappedInputBuffers[i]) { + m_nvenc.nvEncUnmapInputResource(m_hEncoder, m_vMappedInputBuffers[i]); + } + } + m_vMappedInputBuffers.clear(); + + for (uint32_t i = 0; i < m_vRegisteredResources.size(); ++i) { + if (m_vRegisteredResources[i]) { + m_nvenc.nvEncUnregisterResource(m_hEncoder, m_vRegisteredResources[i]); + } + } + m_vRegisteredResources.clear(); + + for (uint32_t i = 0; i < m_vRegisteredResourcesForReference.size(); ++i) { + if (m_vRegisteredResourcesForReference[i]) { + m_nvenc.nvEncUnregisterResource(m_hEncoder, + m_vRegisteredResourcesForReference[i]); + } + } + m_vRegisteredResourcesForReference.clear(); +} + +void NvEncoder::WaitForCompletionEvent(int iEvent) { +#if defined(_WIN32) + // Check if we are in async mode. If not, don't wait for event; + NV_ENC_CONFIG sEncodeConfig = {0}; + NV_ENC_INITIALIZE_PARAMS sInitializeParams = {0}; + sInitializeParams.encodeConfig = &sEncodeConfig; + GetInitializeParams(&sInitializeParams); + + if (0U == sInitializeParams.enableEncodeAsync) { + return; + } +#ifdef DEBUG + WaitForSingleObject(m_vpCompletionEvent[iEvent], INFINITE); +#else + // wait for 20s which is infinite on terms of gpu time + if (WaitForSingleObject(m_vpCompletionEvent[iEvent], 20000) == WAIT_FAILED) { + NVENC_THROW_ERROR("Failed to encode frame", NV_ENC_ERR_GENERIC); + } +#endif +#endif +} + +uint32_t NvEncoder::GetWidthInBytes(const NV_ENC_BUFFER_FORMAT bufferFormat, + const uint32_t width) { + switch (bufferFormat) { + case NV_ENC_BUFFER_FORMAT_NV12: + case NV_ENC_BUFFER_FORMAT_YV12: + case NV_ENC_BUFFER_FORMAT_IYUV: + case NV_ENC_BUFFER_FORMAT_YUV444: + return width; + case NV_ENC_BUFFER_FORMAT_YUV420_10BIT: + case NV_ENC_BUFFER_FORMAT_YUV444_10BIT: + return width * 2; + case NV_ENC_BUFFER_FORMAT_ARGB: + case NV_ENC_BUFFER_FORMAT_ARGB10: + case NV_ENC_BUFFER_FORMAT_AYUV: + case NV_ENC_BUFFER_FORMAT_ABGR: + case NV_ENC_BUFFER_FORMAT_ABGR10: + return width * 4; + default: + NVENC_THROW_ERROR("Invalid Buffer format", NV_ENC_ERR_INVALID_PARAM); + return 0; + } +} + +uint32_t NvEncoder::GetNumChromaPlanes( + const NV_ENC_BUFFER_FORMAT bufferFormat) { + switch (bufferFormat) { + case NV_ENC_BUFFER_FORMAT_NV12: + case NV_ENC_BUFFER_FORMAT_YUV420_10BIT: + return 1; + case NV_ENC_BUFFER_FORMAT_YV12: + case NV_ENC_BUFFER_FORMAT_IYUV: + case NV_ENC_BUFFER_FORMAT_YUV444: + case NV_ENC_BUFFER_FORMAT_YUV444_10BIT: + return 2; + case NV_ENC_BUFFER_FORMAT_ARGB: + case NV_ENC_BUFFER_FORMAT_ARGB10: + case NV_ENC_BUFFER_FORMAT_AYUV: + case NV_ENC_BUFFER_FORMAT_ABGR: + case NV_ENC_BUFFER_FORMAT_ABGR10: + return 0; + default: + NVENC_THROW_ERROR("Invalid Buffer format", NV_ENC_ERR_INVALID_PARAM); + return -1; + } +} + +uint32_t NvEncoder::GetChromaPitch(const NV_ENC_BUFFER_FORMAT bufferFormat, + const uint32_t lumaPitch) { + switch (bufferFormat) { + case NV_ENC_BUFFER_FORMAT_NV12: + case NV_ENC_BUFFER_FORMAT_YUV420_10BIT: + case NV_ENC_BUFFER_FORMAT_YUV444: + case NV_ENC_BUFFER_FORMAT_YUV444_10BIT: + return lumaPitch; + case NV_ENC_BUFFER_FORMAT_YV12: + case NV_ENC_BUFFER_FORMAT_IYUV: + return (lumaPitch + 1) / 2; + case NV_ENC_BUFFER_FORMAT_ARGB: + case NV_ENC_BUFFER_FORMAT_ARGB10: + case NV_ENC_BUFFER_FORMAT_AYUV: + case NV_ENC_BUFFER_FORMAT_ABGR: + case NV_ENC_BUFFER_FORMAT_ABGR10: + return 0; + default: + NVENC_THROW_ERROR("Invalid Buffer format", NV_ENC_ERR_INVALID_PARAM); + return -1; + } +} + +void NvEncoder::GetChromaSubPlaneOffsets( + const NV_ENC_BUFFER_FORMAT bufferFormat, + const uint32_t pitch, + const uint32_t height, + std::vector& chromaOffsets) { + chromaOffsets.clear(); + switch (bufferFormat) { + case NV_ENC_BUFFER_FORMAT_NV12: + case NV_ENC_BUFFER_FORMAT_YUV420_10BIT: + chromaOffsets.push_back(pitch * height); + return; + case NV_ENC_BUFFER_FORMAT_YV12: + case NV_ENC_BUFFER_FORMAT_IYUV: + chromaOffsets.push_back(pitch * height); + chromaOffsets.push_back(chromaOffsets[0] + + (NvEncoder::GetChromaPitch(bufferFormat, pitch) * + GetChromaHeight(bufferFormat, height))); + return; + case NV_ENC_BUFFER_FORMAT_YUV444: + case NV_ENC_BUFFER_FORMAT_YUV444_10BIT: + chromaOffsets.push_back(pitch * height); + chromaOffsets.push_back(chromaOffsets[0] + (pitch * height)); + return; + case NV_ENC_BUFFER_FORMAT_ARGB: + case NV_ENC_BUFFER_FORMAT_ARGB10: + case NV_ENC_BUFFER_FORMAT_AYUV: + case NV_ENC_BUFFER_FORMAT_ABGR: + case NV_ENC_BUFFER_FORMAT_ABGR10: + return; + default: + NVENC_THROW_ERROR("Invalid Buffer format", NV_ENC_ERR_INVALID_PARAM); + return; + } +} + +uint32_t NvEncoder::GetChromaHeight(const NV_ENC_BUFFER_FORMAT bufferFormat, + const uint32_t lumaHeight) { + switch (bufferFormat) { + case NV_ENC_BUFFER_FORMAT_YV12: + case NV_ENC_BUFFER_FORMAT_IYUV: + case NV_ENC_BUFFER_FORMAT_NV12: + case NV_ENC_BUFFER_FORMAT_YUV420_10BIT: + return (lumaHeight + 1) / 2; + case NV_ENC_BUFFER_FORMAT_YUV444: + case NV_ENC_BUFFER_FORMAT_YUV444_10BIT: + return lumaHeight; + case NV_ENC_BUFFER_FORMAT_ARGB: + case NV_ENC_BUFFER_FORMAT_ARGB10: + case NV_ENC_BUFFER_FORMAT_AYUV: + case NV_ENC_BUFFER_FORMAT_ABGR: + case NV_ENC_BUFFER_FORMAT_ABGR10: + return 0; + default: + NVENC_THROW_ERROR("Invalid Buffer format", NV_ENC_ERR_INVALID_PARAM); + return 0; + } +} + +uint32_t NvEncoder::GetChromaWidthInBytes( + const NV_ENC_BUFFER_FORMAT bufferFormat, + const uint32_t lumaWidth) { + switch (bufferFormat) { + case NV_ENC_BUFFER_FORMAT_YV12: + case NV_ENC_BUFFER_FORMAT_IYUV: + return (lumaWidth + 1) / 2; + case NV_ENC_BUFFER_FORMAT_NV12: + return lumaWidth; + case NV_ENC_BUFFER_FORMAT_YUV420_10BIT: + return 2 * lumaWidth; + case NV_ENC_BUFFER_FORMAT_YUV444: + return lumaWidth; + case NV_ENC_BUFFER_FORMAT_YUV444_10BIT: + return 2 * lumaWidth; + case NV_ENC_BUFFER_FORMAT_ARGB: + case NV_ENC_BUFFER_FORMAT_ARGB10: + case NV_ENC_BUFFER_FORMAT_AYUV: + case NV_ENC_BUFFER_FORMAT_ABGR: + case NV_ENC_BUFFER_FORMAT_ABGR10: + return 0; + default: + NVENC_THROW_ERROR("Invalid Buffer format", NV_ENC_ERR_INVALID_PARAM); + return 0; + } +} + +int NvEncoder::GetCapabilityValue(GUID guidCodec, NV_ENC_CAPS capsToQuery) { + if (!m_hEncoder) { + return 0; + } + NV_ENC_CAPS_PARAM capsParam = {NV_ENC_CAPS_PARAM_VER}; + capsParam.capsToQuery = capsToQuery; + int v; + m_nvenc.nvEncGetEncodeCaps(m_hEncoder, guidCodec, &capsParam, &v); + return v; +} + +int NvEncoder::GetFrameSize() const { + switch (GetPixelFormat()) { + case NV_ENC_BUFFER_FORMAT_YV12: + case NV_ENC_BUFFER_FORMAT_IYUV: + case NV_ENC_BUFFER_FORMAT_NV12: + return GetEncodeWidth() * + (GetEncodeHeight() + (GetEncodeHeight() + 1) / 2); + case NV_ENC_BUFFER_FORMAT_YUV420_10BIT: + return 2 * GetEncodeWidth() * + (GetEncodeHeight() + (GetEncodeHeight() + 1) / 2); + case NV_ENC_BUFFER_FORMAT_YUV444: + return GetEncodeWidth() * GetEncodeHeight() * 3; + case NV_ENC_BUFFER_FORMAT_YUV444_10BIT: + return 2 * GetEncodeWidth() * GetEncodeHeight() * 3; + case NV_ENC_BUFFER_FORMAT_ARGB: + case NV_ENC_BUFFER_FORMAT_ARGB10: + case NV_ENC_BUFFER_FORMAT_AYUV: + case NV_ENC_BUFFER_FORMAT_ABGR: + case NV_ENC_BUFFER_FORMAT_ABGR10: + return 4 * GetEncodeWidth() * GetEncodeHeight(); + default: + NVENC_THROW_ERROR("Invalid Buffer format", NV_ENC_ERR_INVALID_PARAM); + return 0; + } +} + +void NvEncoder::GetInitializeParams( + NV_ENC_INITIALIZE_PARAMS* pInitializeParams) { + if (!pInitializeParams || !pInitializeParams->encodeConfig) { + NVENC_THROW_ERROR( + "Both pInitializeParams and pInitializeParams->encodeConfig can't be " + "NULL", + NV_ENC_ERR_INVALID_PTR); + } + NV_ENC_CONFIG* pEncodeConfig = pInitializeParams->encodeConfig; + *pEncodeConfig = m_encodeConfig; + *pInitializeParams = m_initializeParams; + pInitializeParams->encodeConfig = pEncodeConfig; +} + +void NvEncoder::InitializeBitstreamBuffer() { + for (int i = 0; i < m_nEncoderBuffer; i++) { + NV_ENC_CREATE_BITSTREAM_BUFFER createBitstreamBuffer = { + NV_ENC_CREATE_BITSTREAM_BUFFER_VER}; + NVENC_API_CALL( + m_nvenc.nvEncCreateBitstreamBuffer(m_hEncoder, &createBitstreamBuffer)); + m_vBitstreamOutputBuffer[i] = createBitstreamBuffer.bitstreamBuffer; + } +} + +void NvEncoder::DestroyBitstreamBuffer() { + for (uint32_t i = 0; i < m_vBitstreamOutputBuffer.size(); i++) { + if (m_vBitstreamOutputBuffer[i]) { + m_nvenc.nvEncDestroyBitstreamBuffer(m_hEncoder, + m_vBitstreamOutputBuffer[i]); + } + } + + m_vBitstreamOutputBuffer.clear(); +} + +void NvEncoder::InitializeMVOutputBuffer() { + for (int i = 0; i < m_nEncoderBuffer; i++) { + NV_ENC_CREATE_MV_BUFFER createMVBuffer = {NV_ENC_CREATE_MV_BUFFER_VER}; + NVENC_API_CALL(m_nvenc.nvEncCreateMVBuffer(m_hEncoder, &createMVBuffer)); + m_vMVDataOutputBuffer.push_back(createMVBuffer.mvBuffer); + } +} + +void NvEncoder::DestroyMVOutputBuffer() { + for (uint32_t i = 0; i < m_vMVDataOutputBuffer.size(); i++) { + if (m_vMVDataOutputBuffer[i]) { + m_nvenc.nvEncDestroyMVBuffer(m_hEncoder, m_vMVDataOutputBuffer[i]); + } + } + + m_vMVDataOutputBuffer.clear(); +} + +NVENCSTATUS NvEncoder::DoMotionEstimation( + NV_ENC_INPUT_PTR inputBuffer, + NV_ENC_INPUT_PTR inputBufferForReference, + NV_ENC_OUTPUT_PTR outputBuffer) { + NV_ENC_MEONLY_PARAMS meParams = {NV_ENC_MEONLY_PARAMS_VER}; + meParams.inputBuffer = inputBuffer; + meParams.referenceFrame = inputBufferForReference; + meParams.inputWidth = GetEncodeWidth(); + meParams.inputHeight = GetEncodeHeight(); + meParams.mvBuffer = outputBuffer; + meParams.completionEvent = GetCompletionEvent(m_iToSend % m_nEncoderBuffer); + NVENCSTATUS nvStatus = + m_nvenc.nvEncRunMotionEstimationOnly(m_hEncoder, &meParams); + + return nvStatus; +} diff --git a/NvCodec/NvCodec/NvEncoder/NvEncoder.h b/NvCodec/NvCodec/NvEncoder/NvEncoder.h index 9244219b..66ca1a77 100644 --- a/NvCodec/NvCodec/NvEncoder/NvEncoder.h +++ b/NvCodec/NvCodec/NvEncoder/NvEncoder.h @@ -1,446 +1,496 @@ -/* -* Copyright 2017-2020 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ - -#pragma once - -#include -#include "nvEncodeAPI.h" -#include -#include -#include -#include -#include -#include -#if !defined(_WIN32) -#include -#endif - -/** -* @brief Exception class for error reporting from NvEncodeAPI calls. -*/ -class NVENCException : public std::exception -{ -public: - NVENCException(const std::string& errorStr, const NVENCSTATUS errorCode) - : m_errorString(errorStr), m_errorCode(errorCode) {} - - virtual ~NVENCException() throw() {} - virtual const char* what() const throw() { return m_errorString.c_str(); } - NVENCSTATUS getErrorCode() const { return m_errorCode; } - const std::string& getErrorString() const { return m_errorString; } - static NVENCException makeNVENCException(const std::string& errorStr, const NVENCSTATUS errorCode, - const std::string& functionName, const std::string& fileName, int lineNo); -private: - std::string m_errorString; - NVENCSTATUS m_errorCode; -}; - -inline NVENCException NVENCException::makeNVENCException(const std::string& errorStr, const NVENCSTATUS errorCode, const std::string& functionName, - const std::string& fileName, int lineNo) -{ - std::ostringstream errorLog; - errorLog << functionName << " : " << errorStr << " at " << fileName << ":" << lineNo << std::endl; - NVENCException exception(errorLog.str(), errorCode); - return exception; -} - -#define NVENC_THROW_ERROR( errorStr, errorCode ) \ - do \ - { \ - throw NVENCException::makeNVENCException(errorStr, errorCode, __FUNCTION__, __FILE__, __LINE__); \ - } while (0) - - -#define NVENC_API_CALL( nvencAPI ) \ - do \ - { \ - NVENCSTATUS errorCode = nvencAPI; \ - if( errorCode != NV_ENC_SUCCESS) \ - { \ - std::ostringstream errorLog; \ - errorLog << #nvencAPI << " returned error " << errorCode; \ - throw NVENCException::makeNVENCException(errorLog.str(), errorCode, __FUNCTION__, __FILE__, __LINE__); \ - } \ - } while (0) - -struct NvEncInputFrame -{ - void* inputPtr = nullptr; - uint32_t chromaOffsets[2]; - uint32_t numChromaPlanes; - uint32_t pitch; - uint32_t chromaPitch; - NV_ENC_BUFFER_FORMAT bufferFormat; - NV_ENC_INPUT_RESOURCE_TYPE resourceType; -}; - -/** -* @brief Shared base class for different encoder interfaces. -*/ -class NvEncoder -{ -public: - static void TryLoadNvEncApi(); - - /** - * @brief This function is used to initialize the encoder session. - * Application must call this function to initialize the encoder, before - * starting to encode any frames. - */ - void CreateEncoder(const NV_ENC_INITIALIZE_PARAMS* pEncodeParams); - - /** - * @brief This function is used to destroy the encoder session. - * Application must call this function to destroy the encoder session and - * clean up any allocated resources. The application must call EndEncode() - * function to get any queued encoded frames before calling DestroyEncoder(). - */ - void DestroyEncoder(); - - /** - * @brief This function is used to reconfigure an existing encoder session. - * Application can use this function to dynamically change the bitrate, - * resolution and other QOS parameters. If the application changes the - * resolution, it must set NV_ENC_RECONFIGURE_PARAMS::forceIDR. - */ - bool Reconfigure(const NV_ENC_RECONFIGURE_PARAMS *pReconfigureParams); - - /** - * @brief This function is used to get the next available input buffer. - * Applications must call this function to obtain a pointer to the next - * input buffer. The application must copy the uncompressed data to the - * input buffer and then call EncodeFrame() function to encode it. - */ - const NvEncInputFrame* GetNextInputFrame(); - - - /** - * @brief This function is used to encode a frame. - * Applications must call EncodeFrame() function to encode the uncompressed - * data, which has been copied to an input buffer obtained from the - * GetNextInputFrame() function. - */ - void EncodeFrame(std::vector> &vPacket, NV_ENC_PIC_PARAMS *pPicParams = nullptr); - - /** - * @brief This function to flush the encoder queue. - * The encoder might be queuing frames for B picture encoding or lookahead; - * the application must call EndEncode() to get all the queued encoded frames - * from the encoder. The application must call this function before destroying - * an encoder session. - */ - void EndEncode(std::vector> &vPacket); - - /** - * @brief This function is used to query hardware encoder capabilities. - * Applications can call this function to query capabilities like maximum encode - * dimensions, support for lookahead or the ME-only mode etc. - */ - int GetCapabilityValue(GUID guidCodec, NV_ENC_CAPS capsToQuery); - - /** - * @brief This function is used to get the current device on which encoder is running. - */ - void *GetDevice() const { return m_pDevice; } - - /** - * @brief This function is used to get the current device type which encoder is running. - */ - NV_ENC_DEVICE_TYPE GetDeviceType() const { return m_eDeviceType; } - - /** - * @brief This function is used to get the current encode width. - * The encode width can be modified by Reconfigure() function. - */ - int GetEncodeWidth() const { return m_nWidth; } - - /** - * @brief This function is used to get the current encode height. - * The encode height can be modified by Reconfigure() function. - */ - int GetEncodeHeight() const { return m_nHeight; } - - /** - * @brief This function is used to get the current frame size based on pixel format. - */ - int GetFrameSize() const; - - /** - * @brief This function is used to initialize config parameters based on - * given codec and preset guids. - * The application can call this function to get the default configuration - * for a certain preset. The application can either use these parameters - * directly or override them with application-specific settings before - * using them in CreateEncoder() function. - */ - void CreateDefaultEncoderParams(NV_ENC_INITIALIZE_PARAMS* pIntializeParams, GUID codecGuid, GUID presetGuid, NV_ENC_TUNING_INFO tuningInfo = NV_ENC_TUNING_INFO_UNDEFINED); - - /** - * @brief This function is used to get the current initialization parameters, - * which had been used to configure the encoder session. - * The initialization parameters are modified if the application calls - * Reconfigure() function. - */ - void GetInitializeParams(NV_ENC_INITIALIZE_PARAMS *pInitializeParams); - - /** - * @brief This function is used to run motion estimation - * This is used to run motion estimation on a a pair of frames. The - * application must copy the reference frame data to the buffer obtained - * by calling GetNextReferenceFrame(), and copy the input frame data to - * the buffer obtained by calling GetNextInputFrame() before calling the - * RunMotionEstimation() function. - */ - void RunMotionEstimation(std::vector &mvData); - - /** - * @brief This function is used to get an available reference frame. - * Application must call this function to get a pointer to reference buffer, - * to be used in the subsequent RunMotionEstimation() function. - */ - const NvEncInputFrame* GetNextReferenceFrame(); - - /** - * @brief This function is used to get sequence and picture parameter headers. - * Application can call this function after encoder is initialized to get SPS and PPS - * nalus for the current encoder instance. The sequence header data might change when - * application calls Reconfigure() function. - */ - void GetSequenceParams(std::vector &seqParams); - - /** - * @brief NvEncoder class virtual destructor. - */ - virtual ~NvEncoder(); - -public: - /** - * @brief This a static function to get chroma offsets for YUV planar formats. - */ - static void GetChromaSubPlaneOffsets(const NV_ENC_BUFFER_FORMAT bufferFormat, const uint32_t pitch, - const uint32_t height, std::vector& chromaOffsets); - /** - * @brief This a static function to get the chroma plane pitch for YUV planar formats. - */ - static uint32_t GetChromaPitch(const NV_ENC_BUFFER_FORMAT bufferFormat, const uint32_t lumaPitch); - - /** - * @brief This a static function to get the number of chroma planes for YUV planar formats. - */ - static uint32_t GetNumChromaPlanes(const NV_ENC_BUFFER_FORMAT bufferFormat); - - /** - * @brief This a static function to get the chroma plane width in bytes for YUV planar formats. - */ - static uint32_t GetChromaWidthInBytes(const NV_ENC_BUFFER_FORMAT bufferFormat, const uint32_t lumaWidth); - - /** - * @brief This a static function to get the chroma planes height in bytes for YUV planar formats. - */ - static uint32_t GetChromaHeight(const NV_ENC_BUFFER_FORMAT bufferFormat, const uint32_t lumaHeight); - - - /** - * @brief This a static function to get the width in bytes for the frame. - * For YUV planar format this is the width in bytes of the luma plane. - */ - static uint32_t GetWidthInBytes(const NV_ENC_BUFFER_FORMAT bufferFormat, const uint32_t width); - - /** - * @brief This function returns the number of allocated buffers. - */ - uint32_t GetEncoderBufferCount() const { return m_nEncoderBuffer; } -protected: - - /** - * @brief NvEncoder class constructor. - * NvEncoder class constructor cannot be called directly by the application. - */ - NvEncoder(NV_ENC_DEVICE_TYPE eDeviceType, void *pDevice, uint32_t nWidth, uint32_t nHeight, - NV_ENC_BUFFER_FORMAT eBufferFormat, uint32_t nOutputDelay, bool bMotionEstimationOnly, bool bOutputInVideoMemory = false); - - /** - * @brief This function is used to check if hardware encoder is properly initialized. - */ - bool IsHWEncoderInitialized() const { return m_hEncoder != NULL && m_bEncoderInitialized; } - - /** - * @brief This function is used to register CUDA, D3D or OpenGL input buffers with NvEncodeAPI. - * This is non public function and is called by derived class for allocating - * and registering input buffers. - */ - void RegisterInputResources(std::vector inputframes, NV_ENC_INPUT_RESOURCE_TYPE eResourceType, - int width, int height, int pitch, NV_ENC_BUFFER_FORMAT bufferFormat, bool bReferenceFrame = false); - - /** - * @brief This function is used to unregister resources which had been previously registered for encoding - * using RegisterInputResources() function. - */ - void UnregisterInputResources(); - - /** - * @brief This function is used to register CUDA, D3D or OpenGL input or output buffers with NvEncodeAPI. - */ - NV_ENC_REGISTERED_PTR RegisterResource(void *pBuffer, NV_ENC_INPUT_RESOURCE_TYPE eResourceType, - int width, int height, int pitch, NV_ENC_BUFFER_FORMAT bufferFormat, NV_ENC_BUFFER_USAGE bufferUsage = NV_ENC_INPUT_IMAGE); - - /** - * @brief This function returns maximum width used to open the encoder session. - * All encode input buffers are allocated using maximum dimensions. - */ - uint32_t GetMaxEncodeWidth() const { return m_nMaxEncodeWidth; } - - /** - * @brief This function returns maximum height used to open the encoder session. - * All encode input buffers are allocated using maximum dimensions. - */ - uint32_t GetMaxEncodeHeight() const { return m_nMaxEncodeHeight; } - - /** - * @brief This function returns the completion event. - */ - void* GetCompletionEvent(uint32_t eventIdx) { return (m_vpCompletionEvent.size() == m_nEncoderBuffer) ? m_vpCompletionEvent[eventIdx] : nullptr; } - - /** - * @brief This function returns the current pixel format. - */ - NV_ENC_BUFFER_FORMAT GetPixelFormat() const { return m_eBufferFormat; } - - /** - * @brief This function is used to submit the encode commands to the - * NVENC hardware. - */ - NVENCSTATUS DoEncode(NV_ENC_INPUT_PTR inputBuffer, NV_ENC_OUTPUT_PTR outputBuffer, NV_ENC_PIC_PARAMS *pPicParams); - - /** - * @brief This function is used to submit the encode commands to the - * NVENC hardware for ME only mode. - */ - NVENCSTATUS DoMotionEstimation(NV_ENC_INPUT_PTR inputBuffer, NV_ENC_INPUT_PTR inputBufferForReference, NV_ENC_OUTPUT_PTR outputBuffer); - - /** - * @brief This function is used to map the input buffers to NvEncodeAPI. - */ - void MapResources(uint32_t bfrIdx); - - /** - * @brief This function is used to wait for completion of encode command. - */ - void WaitForCompletionEvent(int iEvent); - - /** - * @brief This function is used to send EOS to HW encoder. - */ - void SendEOS(); - -private: - /** - * @brief This is a private function which is used to check if there is any - buffering done by encoder. - * The encoder generally buffers data to encode B frames or for lookahead - * or pipelining. - */ - bool IsZeroDelay() { return m_nOutputDelay == 0; } - - /** - * @brief This is a private function which is used to load the encode api shared library. - */ - void LoadNvEncApi(); - - /** - * @brief This is a private function which is used to get the output packets - * from the encoder HW. - * This is called by DoEncode() function. If there is buffering enabled, - * this may return without any output data. - */ - void GetEncodedPacket(std::vector &vOutputBuffer, std::vector> &vPacket, bool bOutputDelay); - - /** - * @brief This is a private function which is used to initialize the bitstream buffers. - * This is only used in the encoding mode. - */ - void InitializeBitstreamBuffer(); - - /** - * @brief This is a private function which is used to destroy the bitstream buffers. - * This is only used in the encoding mode. - */ - void DestroyBitstreamBuffer(); - - /** - * @brief This is a private function which is used to initialize MV output buffers. - * This is only used in ME-only Mode. - */ - void InitializeMVOutputBuffer(); - - /** - * @brief This is a private function which is used to destroy MV output buffers. - * This is only used in ME-only Mode. - */ - void DestroyMVOutputBuffer(); - - /** - * @brief This is a private function which is used to destroy HW encoder. - */ - void DestroyHWEncoder(); - - /** - * @brief This function is used to flush the encoder queue. - */ - void FlushEncoder(); - -private: - /** - * @brief This is a pure virtual function which is used to allocate input buffers. - * The derived classes must implement this function. - */ - virtual void AllocateInputBuffers(int32_t numInputBuffers) = 0; - - /** - * @brief This is a pure virtual function which is used to destroy input buffers. - * The derived classes must implement this function. - */ - virtual void ReleaseInputBuffers() = 0; - -protected: - bool m_bMotionEstimationOnly = false; - bool m_bOutputInVideoMemory = false; - void *m_hEncoder = nullptr; - NV_ENCODE_API_FUNCTION_LIST m_nvenc; - std::vector m_vInputFrames; - std::vector m_vRegisteredResources; - std::vector m_vReferenceFrames; - std::vector m_vRegisteredResourcesForReference; - std::vector m_vMappedInputBuffers; - std::vector m_vMappedRefBuffers; - std::vector m_vpCompletionEvent; - - int32_t m_iToSend = 0; - int32_t m_iGot = 0; - int32_t m_nEncoderBuffer = 0; - int32_t m_nOutputDelay = 0; - -private: - uint32_t m_nWidth; - uint32_t m_nHeight; - NV_ENC_BUFFER_FORMAT m_eBufferFormat; - void *m_pDevice; - NV_ENC_DEVICE_TYPE m_eDeviceType; - NV_ENC_INITIALIZE_PARAMS m_initializeParams = {}; - NV_ENC_CONFIG m_encodeConfig = {}; - bool m_bEncoderInitialized = false; - uint32_t m_nExtraOutputDelay = 3; // To ensure encode and graphics can work in parallel, m_nExtraOutputDelay should be set to at least 1 - std::vector m_vBitstreamOutputBuffer; - std::vector m_vMVDataOutputBuffer; - uint32_t m_nMaxEncodeWidth = 0; - uint32_t m_nMaxEncodeHeight = 0; - void* m_hModule = nullptr; -}; +/* +* Copyright 2017-2022 NVIDIA Corporation. All rights reserved. +* +* Please refer to the NVIDIA end user license agreement (EULA) associated +* with this source code for terms and conditions that govern your use of +* this software. Any use, reproduction, disclosure, or distribution of +* this software and related documentation outside the terms of the EULA +* is strictly prohibited. +* +*/ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include "../Utils/NvCodecUtils.h" +#include "nvEncodeAPI.h" +#if !defined(_WIN32) +#include +#endif + +/** +* @brief Exception class for error reporting from NvEncodeAPI calls. +*/ +class NVENCException : public std::exception { + public: + NVENCException(const std::string& errorStr, const NVENCSTATUS errorCode) + : m_errorString(errorStr), m_errorCode(errorCode) {} + + virtual ~NVENCException() throw() {} + virtual const char* what() const throw() { return m_errorString.c_str(); } + NVENCSTATUS getErrorCode() const { return m_errorCode; } + const std::string& getErrorString() const { return m_errorString; } + static NVENCException makeNVENCException(const std::string& errorStr, + const NVENCSTATUS errorCode, + const std::string& functionName, + const std::string& fileName, + int lineNo); + + private: + std::string m_errorString; + NVENCSTATUS m_errorCode; +}; + +inline NVENCException NVENCException::makeNVENCException( + const std::string& errorStr, + const NVENCSTATUS errorCode, + const std::string& functionName, + const std::string& fileName, + int lineNo) { + std::ostringstream errorLog; + errorLog << functionName << " : " << errorStr << " at " << fileName << ":" + << lineNo << std::endl; + NVENCException exception(errorLog.str(), errorCode); + return exception; +} + +#define NVENC_THROW_ERROR(errorStr, errorCode) \ + do { \ + throw NVENCException::makeNVENCException( \ + errorStr, errorCode, __FUNCTION__, __FILE__, __LINE__); \ + } while (0) + +#define NVENC_API_CALL(nvencAPI) \ + do { \ + NVENCSTATUS errorCode = nvencAPI; \ + if (errorCode != NV_ENC_SUCCESS) { \ + std::ostringstream errorLog; \ + errorLog << #nvencAPI << " returned error " << errorCode; \ + throw NVENCException::makeNVENCException( \ + errorLog.str(), errorCode, __FUNCTION__, __FILE__, __LINE__); \ + } \ + } while (0) + +struct NvEncInputFrame { + void* inputPtr = nullptr; + uint32_t chromaOffsets[2]; + uint32_t numChromaPlanes; + uint32_t pitch; + uint32_t chromaPitch; + NV_ENC_BUFFER_FORMAT bufferFormat; + NV_ENC_INPUT_RESOURCE_TYPE resourceType; +}; + +/** +* @brief Shared base class for different encoder interfaces. +*/ +class NvEncoder { + public: + static void TryLoadNvEncApi(); + + /** + * @brief This function is used to initialize the encoder session. + * Application must call this function to initialize the encoder, before + * starting to encode any frames. + */ + virtual void CreateEncoder(const NV_ENC_INITIALIZE_PARAMS* pEncodeParams); + + /** + * @brief This function is used to destroy the encoder session. + * Application must call this function to destroy the encoder session and + * clean up any allocated resources. The application must call EndEncode() + * function to get any queued encoded frames before calling DestroyEncoder(). + */ + virtual void DestroyEncoder(); + + /** + * @brief This function is used to reconfigure an existing encoder session. + * Application can use this function to dynamically change the bitrate, + * resolution and other QOS parameters. If the application changes the + * resolution, it must set NV_ENC_RECONFIGURE_PARAMS::forceIDR. + */ + bool Reconfigure(const NV_ENC_RECONFIGURE_PARAMS* pReconfigureParams); + + /** + * @brief This function is used to get the next available input buffer. + * Applications must call this function to obtain a pointer to the next + * input buffer. The application must copy the uncompressed data to the + * input buffer and then call EncodeFrame() function to encode it. + */ + const NvEncInputFrame* GetNextInputFrame(); + + /** + * @brief This function is used to encode a frame. + * Applications must call EncodeFrame() function to encode the uncompressed + * data, which has been copied to an input buffer obtained from the + * GetNextInputFrame() function. + */ + virtual void EncodeFrame(std::vector>& vPacket, + NV_ENC_PIC_PARAMS* pPicParams = nullptr); + + /** + * @brief This function to flush the encoder queue. + * The encoder might be queuing frames for B picture encoding or lookahead; + * the application must call EndEncode() to get all the queued encoded frames + * from the encoder. The application must call this function before destroying + * an encoder session. + */ + virtual void EndEncode(std::vector>& vPacket); + + /** + * @brief This function is used to query hardware encoder capabilities. + * Applications can call this function to query capabilities like maximum encode + * dimensions, support for lookahead or the ME-only mode etc. + */ + int GetCapabilityValue(GUID guidCodec, NV_ENC_CAPS capsToQuery); + + /** + * @brief This function is used to get the current device on which encoder is running. + */ + void* GetDevice() const { return m_pDevice; } + + /** + * @brief This function is used to get the current device type which encoder is running. + */ + NV_ENC_DEVICE_TYPE GetDeviceType() const { return m_eDeviceType; } + + /** + * @brief This function is used to get the current encode width. + * The encode width can be modified by Reconfigure() function. + */ + int GetEncodeWidth() const { return m_nWidth; } + + /** + * @brief This function is used to get the current encode height. + * The encode height can be modified by Reconfigure() function. + */ + int GetEncodeHeight() const { return m_nHeight; } + + /** + * @brief This function is used to get the current frame size based on pixel format. + */ + int GetFrameSize() const; + + /** + * @brief This function is used to initialize config parameters based on + * given codec and preset guids. + * The application can call this function to get the default configuration + * for a certain preset. The application can either use these parameters + * directly or override them with application-specific settings before + * using them in CreateEncoder() function. + */ + void CreateDefaultEncoderParams( + NV_ENC_INITIALIZE_PARAMS* pIntializeParams, + GUID codecGuid, + GUID presetGuid, + NV_ENC_TUNING_INFO tuningInfo = NV_ENC_TUNING_INFO_UNDEFINED); + + /** + * @brief This function is used to get the current initialization parameters, + * which had been used to configure the encoder session. + * The initialization parameters are modified if the application calls + * Reconfigure() function. + */ + void GetInitializeParams(NV_ENC_INITIALIZE_PARAMS* pInitializeParams); + + /** + * @brief This function is used to run motion estimation + * This is used to run motion estimation on a a pair of frames. The + * application must copy the reference frame data to the buffer obtained + * by calling GetNextReferenceFrame(), and copy the input frame data to + * the buffer obtained by calling GetNextInputFrame() before calling the + * RunMotionEstimation() function. + */ + void RunMotionEstimation(std::vector& mvData); + + /** + * @brief This function is used to get an available reference frame. + * Application must call this function to get a pointer to reference buffer, + * to be used in the subsequent RunMotionEstimation() function. + */ + const NvEncInputFrame* GetNextReferenceFrame(); + + /** + * @brief This function is used to get sequence and picture parameter headers. + * Application can call this function after encoder is initialized to get SPS and PPS + * nalus for the current encoder instance. The sequence header data might change when + * application calls Reconfigure() function. + */ + void GetSequenceParams(std::vector& seqParams); + + /** + * @brief NvEncoder class virtual destructor. + */ + virtual ~NvEncoder(); + + public: + /** + * @brief This a static function to get chroma offsets for YUV planar formats. + */ + static void GetChromaSubPlaneOffsets(const NV_ENC_BUFFER_FORMAT bufferFormat, + const uint32_t pitch, + const uint32_t height, + std::vector& chromaOffsets); + /** + * @brief This a static function to get the chroma plane pitch for YUV planar formats. + */ + static uint32_t GetChromaPitch(const NV_ENC_BUFFER_FORMAT bufferFormat, + const uint32_t lumaPitch); + + /** + * @brief This a static function to get the number of chroma planes for YUV planar formats. + */ + static uint32_t GetNumChromaPlanes(const NV_ENC_BUFFER_FORMAT bufferFormat); + + /** + * @brief This a static function to get the chroma plane width in bytes for YUV planar formats. + */ + static uint32_t GetChromaWidthInBytes(const NV_ENC_BUFFER_FORMAT bufferFormat, + const uint32_t lumaWidth); + + /** + * @brief This a static function to get the chroma planes height in bytes for YUV planar formats. + */ + static uint32_t GetChromaHeight(const NV_ENC_BUFFER_FORMAT bufferFormat, + const uint32_t lumaHeight); + + /** + * @brief This a static function to get the width in bytes for the frame. + * For YUV planar format this is the width in bytes of the luma plane. + */ + static uint32_t GetWidthInBytes(const NV_ENC_BUFFER_FORMAT bufferFormat, + const uint32_t width); + + /** + * @brief This function returns the number of allocated buffers. + */ + uint32_t GetEncoderBufferCount() const { return m_nEncoderBuffer; } + + protected: + /** + * @brief NvEncoder class constructor. + * NvEncoder class constructor cannot be called directly by the application. + */ + NvEncoder(NV_ENC_DEVICE_TYPE eDeviceType, + void* pDevice, + uint32_t nWidth, + uint32_t nHeight, + NV_ENC_BUFFER_FORMAT eBufferFormat, + uint32_t nOutputDelay, + bool bMotionEstimationOnly, + bool bOutputInVideoMemory = false, + bool bDX12Encode = false, + bool bUseIVFContainer = true); + + /** + * @brief This function is used to check if hardware encoder is properly initialized. + */ + bool IsHWEncoderInitialized() const { + return m_hEncoder != NULL && m_bEncoderInitialized; + } + + /** + * @brief This function is used to register CUDA, D3D or OpenGL input buffers with NvEncodeAPI. + * This is non public function and is called by derived class for allocating + * and registering input buffers. + */ + void RegisterInputResources(std::vector inputframes, + NV_ENC_INPUT_RESOURCE_TYPE eResourceType, + int width, + int height, + int pitch, + NV_ENC_BUFFER_FORMAT bufferFormat, + bool bReferenceFrame = false); + + /** + * @brief This function is used to unregister resources which had been previously registered for encoding + * using RegisterInputResources() function. + */ + void UnregisterInputResources(); + + /** + * @brief This function is used to register CUDA, D3D or OpenGL input or output buffers with NvEncodeAPI. + */ + NV_ENC_REGISTERED_PTR RegisterResource( + void* pBuffer, + NV_ENC_INPUT_RESOURCE_TYPE eResourceType, + int width, + int height, + int pitch, + NV_ENC_BUFFER_FORMAT bufferFormat, + NV_ENC_BUFFER_USAGE bufferUsage = NV_ENC_INPUT_IMAGE, + NV_ENC_FENCE_POINT_D3D12* pInputFencePoint = NULL); + + /** + * @brief This function returns maximum width used to open the encoder session. + * All encode input buffers are allocated using maximum dimensions. + */ + uint32_t GetMaxEncodeWidth() const { return m_nMaxEncodeWidth; } + + /** + * @brief This function returns maximum height used to open the encoder session. + * All encode input buffers are allocated using maximum dimensions. + */ + uint32_t GetMaxEncodeHeight() const { return m_nMaxEncodeHeight; } + + /** + * @brief This function returns the completion event. + */ + void* GetCompletionEvent(uint32_t eventIdx) { + return (m_vpCompletionEvent.size() == m_nEncoderBuffer) + ? m_vpCompletionEvent[eventIdx] + : nullptr; + } + + /** + * @brief This function returns the current pixel format. + */ + NV_ENC_BUFFER_FORMAT GetPixelFormat() const { return m_eBufferFormat; } + + /** + * @brief This function is used to submit the encode commands to the + * NVENC hardware. + */ + NVENCSTATUS DoEncode(NV_ENC_INPUT_PTR inputBuffer, + NV_ENC_OUTPUT_PTR outputBuffer, + NV_ENC_PIC_PARAMS* pPicParams); + + /** + * @brief This function is used to submit the encode commands to the + * NVENC hardware for ME only mode. + */ + NVENCSTATUS DoMotionEstimation(NV_ENC_INPUT_PTR inputBuffer, + NV_ENC_INPUT_PTR inputBufferForReference, + NV_ENC_OUTPUT_PTR outputBuffer); + + /** + * @brief This function is used to map the input buffers to NvEncodeAPI. + */ + void MapResources(uint32_t bfrIdx); + + /** + * @brief This function is used to wait for completion of encode command. + */ + void WaitForCompletionEvent(int iEvent); + + /** + * @brief This function is used to send EOS to HW encoder. + */ + void SendEOS(); + + private: + /** + * @brief This is a private function which is used to check if there is any + buffering done by encoder. + * The encoder generally buffers data to encode B frames or for lookahead + * or pipelining. + */ + bool IsZeroDelay() { return m_nOutputDelay == 0; } + + /** + * @brief This is a private function which is used to load the encode api shared library. + */ + void LoadNvEncApi(); + + /** + * @brief This is a private function which is used to get the output packets + * from the encoder HW. + * This is called by DoEncode() function. If there is buffering enabled, + * this may return without any output data. + */ + void GetEncodedPacket(std::vector& vOutputBuffer, + std::vector>& vPacket, + bool bOutputDelay); + + /** + * @brief This is a private function which is used to initialize the bitstream buffers. + * This is only used in the encoding mode. + */ + void InitializeBitstreamBuffer(); + + /** + * @brief This is a private function which is used to destroy the bitstream buffers. + * This is only used in the encoding mode. + */ + void DestroyBitstreamBuffer(); + + /** + * @brief This is a private function which is used to initialize MV output buffers. + * This is only used in ME-only Mode. + */ + void InitializeMVOutputBuffer(); + + /** + * @brief This is a private function which is used to destroy MV output buffers. + * This is only used in ME-only Mode. + */ + void DestroyMVOutputBuffer(); + + /** + * @brief This is a private function which is used to destroy HW encoder. + */ + void DestroyHWEncoder(); + + /** + * @brief This function is used to flush the encoder queue. + */ + void FlushEncoder(); + + private: + /** + * @brief This is a pure virtual function which is used to allocate input buffers. + * The derived classes must implement this function. + */ + virtual void AllocateInputBuffers(int32_t numInputBuffers) = 0; + + /** + * @brief This is a pure virtual function which is used to destroy input buffers. + * The derived classes must implement this function. + */ + virtual void ReleaseInputBuffers() = 0; + + protected: + bool m_bMotionEstimationOnly = false; + bool m_bOutputInVideoMemory = false; + bool m_bIsDX12Encode = false; + void* m_hEncoder = nullptr; + NV_ENCODE_API_FUNCTION_LIST m_nvenc; + NV_ENC_INITIALIZE_PARAMS m_initializeParams = {}; + std::vector m_vInputFrames; + std::vector m_vRegisteredResources; + std::vector m_vReferenceFrames; + std::vector m_vRegisteredResourcesForReference; + std::vector m_vMappedInputBuffers; + std::vector m_vMappedRefBuffers; + std::vector m_vpCompletionEvent; + + int32_t m_iToSend = 0; + int32_t m_iGot = 0; + int32_t m_nEncoderBuffer = 0; + int32_t m_nOutputDelay = 0; + IVFUtils m_IVFUtils; + bool m_bWriteIVFFileHeader = true; + bool m_bUseIVFContainer = true; + + private: + uint32_t m_nWidth; + uint32_t m_nHeight; + NV_ENC_BUFFER_FORMAT m_eBufferFormat; + void* m_pDevice; + NV_ENC_DEVICE_TYPE m_eDeviceType; + NV_ENC_CONFIG m_encodeConfig = {}; + bool m_bEncoderInitialized = false; + uint32_t m_nExtraOutputDelay = + 3; // To ensure encode and graphics can work in parallel, m_nExtraOutputDelay should be set to at least 1 + std::vector m_vBitstreamOutputBuffer; + std::vector m_vMVDataOutputBuffer; + uint32_t m_nMaxEncodeWidth = 0; + uint32_t m_nMaxEncodeHeight = 0; + void* m_hModule = nullptr; +}; diff --git a/NvCodec/NvCodec/NvEncoder/NvEncoderCuda.cpp b/NvCodec/NvCodec/NvEncoder/NvEncoderCuda.cpp index 37285f1a..5620a51a 100644 --- a/NvCodec/NvCodec/NvEncoder/NvEncoderCuda.cpp +++ b/NvCodec/NvCodec/NvEncoder/NvEncoderCuda.cpp @@ -1,290 +1,272 @@ -/* -* Copyright 2017-2020 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ - -#include "NvEncoder/NvEncoderCuda.h" -#include "dyn/cuda.h" - - -NvEncoderCuda::NvEncoderCuda(CUcontext cuContext, uint32_t nWidth, uint32_t nHeight, NV_ENC_BUFFER_FORMAT eBufferFormat, - uint32_t nExtraOutputDelay, bool bMotionEstimationOnly, bool bOutputInVideoMemory): - NvEncoder(NV_ENC_DEVICE_TYPE_CUDA, cuContext, nWidth, nHeight, eBufferFormat, nExtraOutputDelay, bMotionEstimationOnly, bOutputInVideoMemory), - m_cuContext(cuContext) -{ - if (!m_hEncoder) - { - NVENC_THROW_ERROR("Encoder Initialization failed", NV_ENC_ERR_INVALID_DEVICE); - } - - if (!m_cuContext) - { - NVENC_THROW_ERROR("Invalid Cuda Context", NV_ENC_ERR_INVALID_DEVICE); - } -} - -NvEncoderCuda::~NvEncoderCuda() -{ - ReleaseCudaResources(); -} - -void NvEncoderCuda::AllocateInputBuffers(int32_t numInputBuffers) -{ - if (!IsHWEncoderInitialized()) - { - NVENC_THROW_ERROR("Encoder intialization failed", NV_ENC_ERR_ENCODER_NOT_INITIALIZED); - } - - // for MEOnly mode we need to allocate seperate set of buffers for reference frame - int numCount = m_bMotionEstimationOnly ? 2 : 1; - - for (int count = 0; count < numCount; count++) - { - CUDA_DRVAPI_CALL(dyn::cuCtxPushCurrent(m_cuContext)); - std::vector inputFrames; - for (int i = 0; i < numInputBuffers; i++) - { - CUdeviceptr pDeviceFrame; - uint32_t chromaHeight = GetNumChromaPlanes(GetPixelFormat()) * GetChromaHeight(GetPixelFormat(), GetMaxEncodeHeight()); - if (GetPixelFormat() == NV_ENC_BUFFER_FORMAT_YV12 || GetPixelFormat() == NV_ENC_BUFFER_FORMAT_IYUV) - chromaHeight = GetChromaHeight(GetPixelFormat(), GetMaxEncodeHeight()); - CUDA_DRVAPI_CALL(dyn::cuMemAllocPitch((CUdeviceptr *)&pDeviceFrame, - &m_cudaPitch, - GetWidthInBytes(GetPixelFormat(), GetMaxEncodeWidth()), - GetMaxEncodeHeight() + chromaHeight, 16)); - inputFrames.push_back((void*)pDeviceFrame); - } - CUDA_DRVAPI_CALL(dyn::cuCtxPopCurrent(nullptr)); - - RegisterInputResources(inputFrames, - NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR, - GetMaxEncodeWidth(), - GetMaxEncodeHeight(), - (int)m_cudaPitch, - GetPixelFormat(), - (count == 1) ? true : false); - } -} - -void NvEncoderCuda::SetIOCudaStreams(NV_ENC_CUSTREAM_PTR inputStream, NV_ENC_CUSTREAM_PTR outputStream) -{ - NVENC_API_CALL(m_nvenc.nvEncSetIOCudaStreams(m_hEncoder, inputStream, outputStream)); -} - -void NvEncoderCuda::ReleaseInputBuffers() -{ - ReleaseCudaResources(); -} - -void NvEncoderCuda::ReleaseCudaResources() -{ - if (!m_hEncoder) - { - return; - } - - if (!m_cuContext) - { - return; - } - - UnregisterInputResources(); - - dyn::cuCtxPushCurrent(m_cuContext); - - for (uint32_t i = 0; i < m_vInputFrames.size(); ++i) - { - if (m_vInputFrames[i].inputPtr) - { - dyn::cuMemFree(reinterpret_cast(m_vInputFrames[i].inputPtr)); - } - } - m_vInputFrames.clear(); - - for (uint32_t i = 0; i < m_vReferenceFrames.size(); ++i) - { - if (m_vReferenceFrames[i].inputPtr) - { - dyn::cuMemFree(reinterpret_cast(m_vReferenceFrames[i].inputPtr)); - } - } - m_vReferenceFrames.clear(); - - dyn::cuCtxPopCurrent(nullptr); - m_cuContext = nullptr; -} - -void NvEncoderCuda::CopyToDeviceFrame(CUcontext device, - void* pSrcFrame, - uint32_t nSrcPitch, - CUdeviceptr pDstFrame, - uint32_t dstPitch, - int width, - int height, - CUmemorytype srcMemoryType, - NV_ENC_BUFFER_FORMAT pixelFormat, - const uint32_t dstChromaOffsets[], - uint32_t numChromaPlanes, - bool bUnAlignedDeviceCopy, - CUstream stream) -{ - if (srcMemoryType != CU_MEMORYTYPE_HOST && srcMemoryType != CU_MEMORYTYPE_DEVICE) - { - NVENC_THROW_ERROR("Invalid source memory type for copy", NV_ENC_ERR_INVALID_PARAM); - } - - CUDA_DRVAPI_CALL(dyn::cuCtxPushCurrent(device)); - - uint32_t srcPitch = nSrcPitch ? nSrcPitch : NvEncoder::GetWidthInBytes(pixelFormat, width); - CUDA_MEMCPY2D m = { 0 }; - m.srcMemoryType = srcMemoryType; - if (srcMemoryType == CU_MEMORYTYPE_HOST) - { - m.srcHost = pSrcFrame; - } - else - { - m.srcDevice = (CUdeviceptr)pSrcFrame; - } - m.srcPitch = srcPitch; - m.dstMemoryType = CU_MEMORYTYPE_DEVICE; - m.dstDevice = pDstFrame; - m.dstPitch = dstPitch; - m.WidthInBytes = NvEncoder::GetWidthInBytes(pixelFormat, width); - m.Height = height; - if (bUnAlignedDeviceCopy && srcMemoryType == CU_MEMORYTYPE_DEVICE) - { - CUDA_DRVAPI_CALL(dyn::cuMemcpy2DUnaligned(&m)); - } - else - { - CUDA_DRVAPI_CALL(stream == NULL? dyn::cuMemcpy2D(&m) : dyn::cuMemcpy2DAsync(&m, stream)); - } - - std::vector srcChromaOffsets; - NvEncoder::GetChromaSubPlaneOffsets(pixelFormat, srcPitch, height, srcChromaOffsets); - uint32_t chromaHeight = NvEncoder::GetChromaHeight(pixelFormat, height); - uint32_t destChromaPitch = NvEncoder::GetChromaPitch(pixelFormat, dstPitch); - uint32_t srcChromaPitch = NvEncoder::GetChromaPitch(pixelFormat, srcPitch); - uint32_t chromaWidthInBytes = NvEncoder::GetChromaWidthInBytes(pixelFormat, width); - - for (uint32_t i = 0; i < numChromaPlanes; ++i) - { - if (chromaHeight) - { - if (srcMemoryType == CU_MEMORYTYPE_HOST) - { - m.srcHost = ((uint8_t *)pSrcFrame + srcChromaOffsets[i]); - } - else - { - m.srcDevice = (CUdeviceptr)((uint8_t *)pSrcFrame + srcChromaOffsets[i]); - } - m.srcPitch = srcChromaPitch; - - m.dstDevice = (CUdeviceptr)((uint8_t *)pDstFrame + dstChromaOffsets[i]); - m.dstPitch = destChromaPitch; - m.WidthInBytes = chromaWidthInBytes; - m.Height = chromaHeight; - if (bUnAlignedDeviceCopy && srcMemoryType == CU_MEMORYTYPE_DEVICE) - { - CUDA_DRVAPI_CALL(dyn::cuMemcpy2DUnaligned(&m)); - } - else - { - CUDA_DRVAPI_CALL(stream == NULL? dyn::cuMemcpy2D(&m) : dyn::cuMemcpy2DAsync(&m, stream)); - } - } - } - CUDA_DRVAPI_CALL(dyn::cuCtxPopCurrent(nullptr)); -} - -void NvEncoderCuda::CopyToDeviceFrame(CUcontext device, - void* pSrcFrame, - uint32_t nSrcPitch, - CUdeviceptr pDstFrame, - uint32_t dstPitch, - int width, - int height, - CUmemorytype srcMemoryType, - NV_ENC_BUFFER_FORMAT pixelFormat, - CUdeviceptr dstChromaDevicePtrs[], - uint32_t dstChromaPitch, - uint32_t numChromaPlanes, - bool bUnAlignedDeviceCopy) -{ - if (srcMemoryType != CU_MEMORYTYPE_HOST && srcMemoryType != CU_MEMORYTYPE_DEVICE) - { - NVENC_THROW_ERROR("Invalid source memory type for copy", NV_ENC_ERR_INVALID_PARAM); - } - - CUDA_DRVAPI_CALL(dyn::cuCtxPushCurrent(device)); - - uint32_t srcPitch = nSrcPitch ? nSrcPitch : NvEncoder::GetWidthInBytes(pixelFormat, width); - CUDA_MEMCPY2D m = { 0 }; - m.srcMemoryType = srcMemoryType; - if (srcMemoryType == CU_MEMORYTYPE_HOST) - { - m.srcHost = pSrcFrame; - } - else - { - m.srcDevice = (CUdeviceptr)pSrcFrame; - } - m.srcPitch = srcPitch; - m.dstMemoryType = CU_MEMORYTYPE_DEVICE; - m.dstDevice = pDstFrame; - m.dstPitch = dstPitch; - m.WidthInBytes = NvEncoder::GetWidthInBytes(pixelFormat, width); - m.Height = height; - if (bUnAlignedDeviceCopy && srcMemoryType == CU_MEMORYTYPE_DEVICE) - { - CUDA_DRVAPI_CALL(dyn::cuMemcpy2DUnaligned(&m)); - } - else - { - CUDA_DRVAPI_CALL(dyn::cuMemcpy2D(&m)); - } - - std::vector srcChromaOffsets; - NvEncoder::GetChromaSubPlaneOffsets(pixelFormat, srcPitch, height, srcChromaOffsets); - uint32_t chromaHeight = NvEncoder::GetChromaHeight(pixelFormat, height); - uint32_t srcChromaPitch = NvEncoder::GetChromaPitch(pixelFormat, srcPitch); - uint32_t chromaWidthInBytes = NvEncoder::GetChromaWidthInBytes(pixelFormat, width); - - for (uint32_t i = 0; i < numChromaPlanes; ++i) - { - if (chromaHeight) - { - if (srcMemoryType == CU_MEMORYTYPE_HOST) - { - m.srcHost = ((uint8_t *)pSrcFrame + srcChromaOffsets[i]); - } - else - { - m.srcDevice = (CUdeviceptr)((uint8_t *)pSrcFrame + srcChromaOffsets[i]); - } - m.srcPitch = srcChromaPitch; - - m.dstDevice = dstChromaDevicePtrs[i]; - m.dstPitch = dstChromaPitch; - m.WidthInBytes = chromaWidthInBytes; - m.Height = chromaHeight; - if (bUnAlignedDeviceCopy && srcMemoryType == CU_MEMORYTYPE_DEVICE) - { - CUDA_DRVAPI_CALL(dyn::cuMemcpy2DUnaligned(&m)); - } - else - { - CUDA_DRVAPI_CALL(dyn::cuMemcpy2D(&m)); - } - } - } - CUDA_DRVAPI_CALL(dyn::cuCtxPopCurrent(nullptr)); -} +/* +* Copyright 2017-2022 NVIDIA Corporation. All rights reserved. +* +* Please refer to the NVIDIA end user license agreement (EULA) associated +* with this source code for terms and conditions that govern your use of +* this software. Any use, reproduction, disclosure, or distribution of +* this software and related documentation outside the terms of the EULA +* is strictly prohibited. +* +*/ + +#include "NvEncoder/NvEncoderCuda.h" +#include "dyn/cuda.h" + +NvEncoderCuda::NvEncoderCuda(CUcontext cuContext, + uint32_t nWidth, + uint32_t nHeight, + NV_ENC_BUFFER_FORMAT eBufferFormat, + uint32_t nExtraOutputDelay, + bool bMotionEstimationOnly, + bool bOutputInVideoMemory, + bool bUseIVFContainer) + : NvEncoder(NV_ENC_DEVICE_TYPE_CUDA, + cuContext, + nWidth, + nHeight, + eBufferFormat, + nExtraOutputDelay, + bMotionEstimationOnly, + bOutputInVideoMemory, + false, + bUseIVFContainer), + m_cuContext(cuContext) { + if (!m_hEncoder) { + NVENC_THROW_ERROR("Encoder Initialization failed", + NV_ENC_ERR_INVALID_DEVICE); + } + + if (!m_cuContext) { + NVENC_THROW_ERROR("Invalid Cuda Context", NV_ENC_ERR_INVALID_DEVICE); + } +} + +NvEncoderCuda::~NvEncoderCuda() { + ReleaseCudaResources(); +} + +void NvEncoderCuda::AllocateInputBuffers(int32_t numInputBuffers) { + if (!IsHWEncoderInitialized()) { + NVENC_THROW_ERROR("Encoder intialization failed", + NV_ENC_ERR_ENCODER_NOT_INITIALIZED); + } + + // for MEOnly mode we need to allocate seperate set of buffers for reference frame + int numCount = m_bMotionEstimationOnly ? 2 : 1; + + for (int count = 0; count < numCount; count++) { + CUDA_DRVAPI_CALL(dyn::cuCtxPushCurrent(m_cuContext)); + std::vector inputFrames; + for (int i = 0; i < numInputBuffers; i++) { + CUdeviceptr pDeviceFrame; + uint32_t chromaHeight = + GetNumChromaPlanes(GetPixelFormat()) * + GetChromaHeight(GetPixelFormat(), GetMaxEncodeHeight()); + if (GetPixelFormat() == NV_ENC_BUFFER_FORMAT_YV12 || + GetPixelFormat() == NV_ENC_BUFFER_FORMAT_IYUV) + chromaHeight = GetChromaHeight(GetPixelFormat(), GetMaxEncodeHeight()); + CUDA_DRVAPI_CALL(dyn::cuMemAllocPitch( + (CUdeviceptr*)&pDeviceFrame, &m_cudaPitch, + GetWidthInBytes(GetPixelFormat(), GetMaxEncodeWidth()), + GetMaxEncodeHeight() + chromaHeight, 16)); + inputFrames.push_back((void*)pDeviceFrame); + } + CUDA_DRVAPI_CALL(dyn::cuCtxPopCurrent(nullptr)); + + RegisterInputResources( + inputFrames, NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR, + GetMaxEncodeWidth(), GetMaxEncodeHeight(), (int)m_cudaPitch, + GetPixelFormat(), (count == 1) ? true : false); + } +} + +void NvEncoderCuda::SetIOCudaStreams(NV_ENC_CUSTREAM_PTR inputStream, + NV_ENC_CUSTREAM_PTR outputStream) { + NVENC_API_CALL( + m_nvenc.nvEncSetIOCudaStreams(m_hEncoder, inputStream, outputStream)); +} + +void NvEncoderCuda::ReleaseInputBuffers() { + ReleaseCudaResources(); +} + +void NvEncoderCuda::ReleaseCudaResources() { + if (!m_hEncoder) { + return; + } + + if (!m_cuContext) { + return; + } + + UnregisterInputResources(); + + dyn::cuCtxPushCurrent(m_cuContext); + + for (uint32_t i = 0; i < m_vInputFrames.size(); ++i) { + if (m_vInputFrames[i].inputPtr) { + dyn::cuMemFree(reinterpret_cast(m_vInputFrames[i].inputPtr)); + } + } + m_vInputFrames.clear(); + + for (uint32_t i = 0; i < m_vReferenceFrames.size(); ++i) { + if (m_vReferenceFrames[i].inputPtr) { + dyn::cuMemFree( + reinterpret_cast(m_vReferenceFrames[i].inputPtr)); + } + } + m_vReferenceFrames.clear(); + + dyn::cuCtxPopCurrent(nullptr); + m_cuContext = nullptr; +} + +void NvEncoderCuda::CopyToDeviceFrame(CUcontext device, + void* pSrcFrame, + uint32_t nSrcPitch, + CUdeviceptr pDstFrame, + uint32_t dstPitch, + int width, + int height, + CUmemorytype srcMemoryType, + NV_ENC_BUFFER_FORMAT pixelFormat, + const uint32_t dstChromaOffsets[], + uint32_t numChromaPlanes, + bool bUnAlignedDeviceCopy, + CUstream stream) { + if (srcMemoryType != CU_MEMORYTYPE_HOST && + srcMemoryType != CU_MEMORYTYPE_DEVICE) { + NVENC_THROW_ERROR("Invalid source memory type for copy", + NV_ENC_ERR_INVALID_PARAM); + } + + CUDA_DRVAPI_CALL(dyn::cuCtxPushCurrent(device)); + + uint32_t srcPitch = + nSrcPitch ? nSrcPitch : NvEncoder::GetWidthInBytes(pixelFormat, width); + CUDA_MEMCPY2D m = {0}; + m.srcMemoryType = srcMemoryType; + if (srcMemoryType == CU_MEMORYTYPE_HOST) { + m.srcHost = pSrcFrame; + } else { + m.srcDevice = (CUdeviceptr)pSrcFrame; + } + m.srcPitch = srcPitch; + m.dstMemoryType = CU_MEMORYTYPE_DEVICE; + m.dstDevice = pDstFrame; + m.dstPitch = dstPitch; + m.WidthInBytes = NvEncoder::GetWidthInBytes(pixelFormat, width); + m.Height = height; + if (bUnAlignedDeviceCopy && srcMemoryType == CU_MEMORYTYPE_DEVICE) { + CUDA_DRVAPI_CALL(dyn::cuMemcpy2DUnaligned(&m)); + } else { + CUDA_DRVAPI_CALL(stream == NULL ? dyn::cuMemcpy2D(&m) + : dyn::cuMemcpy2DAsync(&m, stream)); + } + + std::vector srcChromaOffsets; + NvEncoder::GetChromaSubPlaneOffsets(pixelFormat, srcPitch, height, + srcChromaOffsets); + uint32_t chromaHeight = NvEncoder::GetChromaHeight(pixelFormat, height); + uint32_t destChromaPitch = NvEncoder::GetChromaPitch(pixelFormat, dstPitch); + uint32_t srcChromaPitch = NvEncoder::GetChromaPitch(pixelFormat, srcPitch); + uint32_t chromaWidthInBytes = + NvEncoder::GetChromaWidthInBytes(pixelFormat, width); + + for (uint32_t i = 0; i < numChromaPlanes; ++i) { + if (chromaHeight) { + if (srcMemoryType == CU_MEMORYTYPE_HOST) { + m.srcHost = ((uint8_t*)pSrcFrame + srcChromaOffsets[i]); + } else { + m.srcDevice = (CUdeviceptr)((uint8_t*)pSrcFrame + srcChromaOffsets[i]); + } + m.srcPitch = srcChromaPitch; + + m.dstDevice = (CUdeviceptr)((uint8_t*)pDstFrame + dstChromaOffsets[i]); + m.dstPitch = destChromaPitch; + m.WidthInBytes = chromaWidthInBytes; + m.Height = chromaHeight; + if (bUnAlignedDeviceCopy && srcMemoryType == CU_MEMORYTYPE_DEVICE) { + CUDA_DRVAPI_CALL(dyn::cuMemcpy2DUnaligned(&m)); + } else { + CUDA_DRVAPI_CALL(stream == NULL ? dyn::cuMemcpy2D(&m) + : dyn::cuMemcpy2DAsync(&m, stream)); + } + } + } + CUDA_DRVAPI_CALL(dyn::cuCtxPopCurrent(nullptr)); +} + +void NvEncoderCuda::CopyToDeviceFrame(CUcontext device, + void* pSrcFrame, + uint32_t nSrcPitch, + CUdeviceptr pDstFrame, + uint32_t dstPitch, + int width, + int height, + CUmemorytype srcMemoryType, + NV_ENC_BUFFER_FORMAT pixelFormat, + CUdeviceptr dstChromaDevicePtrs[], + uint32_t dstChromaPitch, + uint32_t numChromaPlanes, + bool bUnAlignedDeviceCopy) { + if (srcMemoryType != CU_MEMORYTYPE_HOST && + srcMemoryType != CU_MEMORYTYPE_DEVICE) { + NVENC_THROW_ERROR("Invalid source memory type for copy", + NV_ENC_ERR_INVALID_PARAM); + } + + CUDA_DRVAPI_CALL(dyn::cuCtxPushCurrent(device)); + + uint32_t srcPitch = + nSrcPitch ? nSrcPitch : NvEncoder::GetWidthInBytes(pixelFormat, width); + CUDA_MEMCPY2D m = {0}; + m.srcMemoryType = srcMemoryType; + if (srcMemoryType == CU_MEMORYTYPE_HOST) { + m.srcHost = pSrcFrame; + } else { + m.srcDevice = (CUdeviceptr)pSrcFrame; + } + m.srcPitch = srcPitch; + m.dstMemoryType = CU_MEMORYTYPE_DEVICE; + m.dstDevice = pDstFrame; + m.dstPitch = dstPitch; + m.WidthInBytes = NvEncoder::GetWidthInBytes(pixelFormat, width); + m.Height = height; + if (bUnAlignedDeviceCopy && srcMemoryType == CU_MEMORYTYPE_DEVICE) { + CUDA_DRVAPI_CALL(dyn::cuMemcpy2DUnaligned(&m)); + } else { + CUDA_DRVAPI_CALL(dyn::cuMemcpy2D(&m)); + } + + std::vector srcChromaOffsets; + NvEncoder::GetChromaSubPlaneOffsets(pixelFormat, srcPitch, height, + srcChromaOffsets); + uint32_t chromaHeight = NvEncoder::GetChromaHeight(pixelFormat, height); + uint32_t srcChromaPitch = NvEncoder::GetChromaPitch(pixelFormat, srcPitch); + uint32_t chromaWidthInBytes = + NvEncoder::GetChromaWidthInBytes(pixelFormat, width); + + for (uint32_t i = 0; i < numChromaPlanes; ++i) { + if (chromaHeight) { + if (srcMemoryType == CU_MEMORYTYPE_HOST) { + m.srcHost = ((uint8_t*)pSrcFrame + srcChromaOffsets[i]); + } else { + m.srcDevice = (CUdeviceptr)((uint8_t*)pSrcFrame + srcChromaOffsets[i]); + } + m.srcPitch = srcChromaPitch; + + m.dstDevice = dstChromaDevicePtrs[i]; + m.dstPitch = dstChromaPitch; + m.WidthInBytes = chromaWidthInBytes; + m.Height = chromaHeight; + if (bUnAlignedDeviceCopy && srcMemoryType == CU_MEMORYTYPE_DEVICE) { + CUDA_DRVAPI_CALL(dyn::cuMemcpy2DUnaligned(&m)); + } else { + CUDA_DRVAPI_CALL(dyn::cuMemcpy2D(&m)); + } + } + } + CUDA_DRVAPI_CALL(dyn::cuCtxPopCurrent(nullptr)); +} diff --git a/NvCodec/NvCodec/NvEncoder/NvEncoderCuda.h b/NvCodec/NvCodec/NvEncoder/NvEncoderCuda.h index a78dcf92..82d406a5 100644 --- a/NvCodec/NvCodec/NvEncoder/NvEncoderCuda.h +++ b/NvCodec/NvCodec/NvEncoder/NvEncoderCuda.h @@ -1,111 +1,116 @@ -/* -* Copyright 2017-2020 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ - -#pragma once - -#include -#include -#include -#include -#include "NvEncoder.h" - -#define CUDA_DRVAPI_CALL( call ) \ - do \ - { \ - CUresult err__ = call; \ - if (err__ != CUDA_SUCCESS) \ - { \ - const char *szErrName = NULL; \ - dyn::cuGetErrorName(err__, &szErrName); \ - std::ostringstream errorLog; \ - errorLog << "CUDA driver API error " << szErrName ; \ - throw NVENCException::makeNVENCException(errorLog.str(), NV_ENC_ERR_GENERIC, __FUNCTION__, __FILE__, __LINE__); \ - } \ - } \ - while (0) - -/** -* @brief Encoder for CUDA device memory. -*/ -class NvEncoderCuda : public NvEncoder -{ -public: - NvEncoderCuda(CUcontext cuContext, uint32_t nWidth, uint32_t nHeight, NV_ENC_BUFFER_FORMAT eBufferFormat, - uint32_t nExtraOutputDelay = 3, bool bMotionEstimationOnly = false, bool bOPInVideoMemory = false); - virtual ~NvEncoderCuda(); - - /** - * @brief This is a static function to copy input data from host memory to device memory. - * This function assumes YUV plane is a single contiguous memory segment. - */ - static void CopyToDeviceFrame(CUcontext device, - void* pSrcFrame, - uint32_t nSrcPitch, - CUdeviceptr pDstFrame, - uint32_t dstPitch, - int width, - int height, - CUmemorytype srcMemoryType, - NV_ENC_BUFFER_FORMAT pixelFormat, - const uint32_t dstChromaOffsets[], - uint32_t numChromaPlanes, - bool bUnAlignedDeviceCopy = false, - CUstream stream = NULL); - - /** - * @brief This is a static function to copy input data from host memory to device memory. - * Application must pass a seperate device pointer for each YUV plane. - */ - static void CopyToDeviceFrame(CUcontext device, - void* pSrcFrame, - uint32_t nSrcPitch, - CUdeviceptr pDstFrame, - uint32_t dstPitch, - int width, - int height, - CUmemorytype srcMemoryType, - NV_ENC_BUFFER_FORMAT pixelFormat, - CUdeviceptr dstChromaPtr[], - uint32_t dstChromaPitch, - uint32_t numChromaPlanes, - bool bUnAlignedDeviceCopy = false); - - /** - * @brief This function sets input and output CUDA streams - */ - void SetIOCudaStreams(NV_ENC_CUSTREAM_PTR inputStream, NV_ENC_CUSTREAM_PTR outputStream); - -protected: - /** - * @brief This function is used to release the input buffers allocated for encoding. - * This function is an override of virtual function NvEncoder::ReleaseInputBuffers(). - */ - virtual void ReleaseInputBuffers() override; - -private: - /** - * @brief This function is used to allocate input buffers for encoding. - * This function is an override of virtual function NvEncoder::AllocateInputBuffers(). - */ - virtual void AllocateInputBuffers(int32_t numInputBuffers) override; - -private: - /** - * @brief This is a private function to release CUDA device memory used for encoding. - */ - void ReleaseCudaResources(); - -protected: - CUcontext m_cuContext; - -private: - size_t m_cudaPitch = 0; -}; +/* +* Copyright 2017-2022 NVIDIA Corporation. All rights reserved. +* +* Please refer to the NVIDIA end user license agreement (EULA) associated +* with this source code for terms and conditions that govern your use of +* this software. Any use, reproduction, disclosure, or distribution of +* this software and related documentation outside the terms of the EULA +* is strictly prohibited. +* +*/ + +#pragma once + +#include +#include +#include +#include +#include "NvEncoder.h" + +#define CUDA_DRVAPI_CALL(call) \ + do { \ + CUresult err__ = call; \ + if (err__ != CUDA_SUCCESS) { \ + const char* szErrName = NULL; \ + dyn::cuGetErrorName(err__, &szErrName); \ + std::ostringstream errorLog; \ + errorLog << "CUDA driver API error " << szErrName; \ + throw NVENCException::makeNVENCException( \ + errorLog.str(), NV_ENC_ERR_GENERIC, __FUNCTION__, __FILE__, \ + __LINE__); \ + } \ + } while (0) + +/** +* @brief Encoder for CUDA device memory. +*/ +class NvEncoderCuda : public NvEncoder { + public: + NvEncoderCuda(CUcontext cuContext, + uint32_t nWidth, + uint32_t nHeight, + NV_ENC_BUFFER_FORMAT eBufferFormat, + uint32_t nExtraOutputDelay = 3, + bool bMotionEstimationOnly = false, + bool bOPInVideoMemory = false, + bool bUseIVFContainer = true); + virtual ~NvEncoderCuda(); + + /** + * @brief This is a static function to copy input data from host memory to device memory. + * This function assumes YUV plane is a single contiguous memory segment. + */ + static void CopyToDeviceFrame(CUcontext device, + void* pSrcFrame, + uint32_t nSrcPitch, + CUdeviceptr pDstFrame, + uint32_t dstPitch, + int width, + int height, + CUmemorytype srcMemoryType, + NV_ENC_BUFFER_FORMAT pixelFormat, + const uint32_t dstChromaOffsets[], + uint32_t numChromaPlanes, + bool bUnAlignedDeviceCopy = false, + CUstream stream = NULL); + + /** + * @brief This is a static function to copy input data from host memory to device memory. + * Application must pass a seperate device pointer for each YUV plane. + */ + static void CopyToDeviceFrame(CUcontext device, + void* pSrcFrame, + uint32_t nSrcPitch, + CUdeviceptr pDstFrame, + uint32_t dstPitch, + int width, + int height, + CUmemorytype srcMemoryType, + NV_ENC_BUFFER_FORMAT pixelFormat, + CUdeviceptr dstChromaPtr[], + uint32_t dstChromaPitch, + uint32_t numChromaPlanes, + bool bUnAlignedDeviceCopy = false); + + /** + * @brief This function sets input and output CUDA streams + */ + void SetIOCudaStreams(NV_ENC_CUSTREAM_PTR inputStream, + NV_ENC_CUSTREAM_PTR outputStream); + + protected: + /** + * @brief This function is used to release the input buffers allocated for encoding. + * This function is an override of virtual function NvEncoder::ReleaseInputBuffers(). + */ + virtual void ReleaseInputBuffers() override; + + private: + /** + * @brief This function is used to allocate input buffers for encoding. + * This function is an override of virtual function NvEncoder::AllocateInputBuffers(). + */ + virtual void AllocateInputBuffers(int32_t numInputBuffers) override; + + private: + /** + * @brief This is a private function to release CUDA device memory used for encoding. + */ + void ReleaseCudaResources(); + + protected: + CUcontext m_cuContext; + + private: + size_t m_cudaPitch = 0; +}; diff --git a/NvCodec/NvCodec/NvEncoder/NvEncoderD3D11.cpp b/NvCodec/NvCodec/NvEncoder/NvEncoderD3D11.cpp index 841d85ab..d43294e3 100644 --- a/NvCodec/NvCodec/NvEncoder/NvEncoderD3D11.cpp +++ b/NvCodec/NvCodec/NvEncoder/NvEncoderD3D11.cpp @@ -1,147 +1,144 @@ -/* -* Copyright 2017-2020 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ - - -#ifndef WIN32 -#include -#endif -#include "NvEncoder/NvEncoderD3D11.h" -#include - -#ifndef MAKEFOURCC -#define MAKEFOURCC(a,b,c,d) (((unsigned int)a) | (((unsigned int)b)<< 8) | (((unsigned int)c)<<16) | (((unsigned int)d)<<24) ) -#endif - -DXGI_FORMAT GetD3D11Format(NV_ENC_BUFFER_FORMAT eBufferFormat) -{ - switch (eBufferFormat) - { - case NV_ENC_BUFFER_FORMAT_NV12: - return DXGI_FORMAT_NV12; - case NV_ENC_BUFFER_FORMAT_ARGB: - return DXGI_FORMAT_B8G8R8A8_UNORM; - default: - return DXGI_FORMAT_UNKNOWN; - } -} - -NvEncoderD3D11::NvEncoderD3D11(ID3D11Device* pD3D11Device, uint32_t nWidth, uint32_t nHeight, - NV_ENC_BUFFER_FORMAT eBufferFormat, uint32_t nExtraOutputDelay, bool bMotionEstimationOnly, bool bOutputInVideoMemory) : - NvEncoder(NV_ENC_DEVICE_TYPE_DIRECTX, pD3D11Device, nWidth, nHeight, eBufferFormat, nExtraOutputDelay, bMotionEstimationOnly, bOutputInVideoMemory) -{ - if (!pD3D11Device) - { - NVENC_THROW_ERROR("Bad d3d11device ptr", NV_ENC_ERR_INVALID_PTR); - return; - } - - if (GetD3D11Format(GetPixelFormat()) == DXGI_FORMAT_UNKNOWN) - { - NVENC_THROW_ERROR("Unsupported Buffer format", NV_ENC_ERR_INVALID_PARAM); - } - - if (!m_hEncoder) - { - NVENC_THROW_ERROR("Encoder Initialization failed", NV_ENC_ERR_INVALID_DEVICE); - } - - m_pD3D11Device = pD3D11Device; - m_pD3D11Device->AddRef(); - m_pD3D11Device->GetImmediateContext(&m_pD3D11DeviceContext); -} - -NvEncoderD3D11::~NvEncoderD3D11() -{ - ReleaseD3D11Resources(); -} - -void NvEncoderD3D11::AllocateInputBuffers(int32_t numInputBuffers) -{ - if (!IsHWEncoderInitialized()) - { - NVENC_THROW_ERROR("Encoder intialization failed", NV_ENC_ERR_ENCODER_NOT_INITIALIZED); - } - - // for MEOnly mode we need to allocate seperate set of buffers for reference frame - int numCount = m_bMotionEstimationOnly ? 2 : 1; - for (int count = 0; count < numCount; count++) - { - std::vector inputFrames; - for (int i = 0; i < numInputBuffers; i++) - { - ID3D11Texture2D *pInputTextures = NULL; - D3D11_TEXTURE2D_DESC desc; - ZeroMemory(&desc, sizeof(D3D11_TEXTURE2D_DESC)); - desc.Width = GetMaxEncodeWidth(); - desc.Height = GetMaxEncodeHeight(); - desc.MipLevels = 1; - desc.ArraySize = 1; - desc.Format = GetD3D11Format(GetPixelFormat()); - desc.SampleDesc.Count = 1; - desc.Usage = D3D11_USAGE_DEFAULT; - desc.BindFlags = D3D11_BIND_RENDER_TARGET; - desc.CPUAccessFlags = 0; - if (m_pD3D11Device->CreateTexture2D(&desc, NULL, &pInputTextures) != S_OK) - { - NVENC_THROW_ERROR("Failed to create d3d11textures", NV_ENC_ERR_OUT_OF_MEMORY); - } - inputFrames.push_back(pInputTextures); - } - RegisterInputResources(inputFrames, NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX, - GetMaxEncodeWidth(), GetMaxEncodeHeight(), 0, GetPixelFormat(), count == 1 ? true : false); - } -} - -void NvEncoderD3D11::ReleaseInputBuffers() -{ - ReleaseD3D11Resources(); -} - -void NvEncoderD3D11::ReleaseD3D11Resources() -{ - if (!m_hEncoder) - { - return; - } - - UnregisterInputResources(); - - for (uint32_t i = 0; i < m_vInputFrames.size(); ++i) - { - if (m_vInputFrames[i].inputPtr) - { - reinterpret_cast(m_vInputFrames[i].inputPtr)->Release(); - } - } - m_vInputFrames.clear(); - - for (uint32_t i = 0; i < m_vReferenceFrames.size(); ++i) - { - if (m_vReferenceFrames[i].inputPtr) - { - reinterpret_cast(m_vReferenceFrames[i].inputPtr)->Release(); - } - } - m_vReferenceFrames.clear(); - - if (m_pD3D11DeviceContext) - { - m_pD3D11DeviceContext->Release(); - m_pD3D11DeviceContext = nullptr; - } - - if (m_pD3D11Device) - { - m_pD3D11Device->Release(); - m_pD3D11Device = nullptr; - } -} - +/* +* Copyright 2017-2022 NVIDIA Corporation. All rights reserved. +* +* Please refer to the NVIDIA end user license agreement (EULA) associated +* with this source code for terms and conditions that govern your use of +* this software. Any use, reproduction, disclosure, or distribution of +* this software and related documentation outside the terms of the EULA +* is strictly prohibited. +* +*/ + +#ifndef WIN32 +#include +#endif +#include "NvEncoder/NvEncoderD3D11.h" +#include + +#ifndef MAKEFOURCC +#define MAKEFOURCC(a, b, c, d) \ + (((unsigned int)a) | (((unsigned int)b) << 8) | (((unsigned int)c) << 16) | \ + (((unsigned int)d) << 24)) +#endif + +DXGI_FORMAT GetD3D11Format(NV_ENC_BUFFER_FORMAT eBufferFormat) { + switch (eBufferFormat) { + case NV_ENC_BUFFER_FORMAT_NV12: + return DXGI_FORMAT_NV12; + case NV_ENC_BUFFER_FORMAT_ARGB: + return DXGI_FORMAT_B8G8R8A8_UNORM; + default: + return DXGI_FORMAT_UNKNOWN; + } +} + +NvEncoderD3D11::NvEncoderD3D11(ID3D11Device* pD3D11Device, + uint32_t nWidth, + uint32_t nHeight, + NV_ENC_BUFFER_FORMAT eBufferFormat, + uint32_t nExtraOutputDelay, + bool bMotionEstimationOnly, + bool bOutputInVideoMemory) + : NvEncoder(NV_ENC_DEVICE_TYPE_DIRECTX, + pD3D11Device, + nWidth, + nHeight, + eBufferFormat, + nExtraOutputDelay, + bMotionEstimationOnly, + bOutputInVideoMemory) { + if (!pD3D11Device) { + NVENC_THROW_ERROR("Bad d3d11device ptr", NV_ENC_ERR_INVALID_PTR); + return; + } + + if (GetD3D11Format(GetPixelFormat()) == DXGI_FORMAT_UNKNOWN) { + NVENC_THROW_ERROR("Unsupported Buffer format", NV_ENC_ERR_INVALID_PARAM); + } + + if (!m_hEncoder) { + NVENC_THROW_ERROR("Encoder Initialization failed", + NV_ENC_ERR_INVALID_DEVICE); + } + + m_pD3D11Device = pD3D11Device; + m_pD3D11Device->AddRef(); + m_pD3D11Device->GetImmediateContext(&m_pD3D11DeviceContext); +} + +NvEncoderD3D11::~NvEncoderD3D11() { + ReleaseD3D11Resources(); +} + +void NvEncoderD3D11::AllocateInputBuffers(int32_t numInputBuffers) { + if (!IsHWEncoderInitialized()) { + NVENC_THROW_ERROR("Encoder intialization failed", + NV_ENC_ERR_ENCODER_NOT_INITIALIZED); + } + + // for MEOnly mode we need to allocate seperate set of buffers for reference frame + int numCount = m_bMotionEstimationOnly ? 2 : 1; + for (int count = 0; count < numCount; count++) { + std::vector inputFrames; + for (int i = 0; i < numInputBuffers; i++) { + ID3D11Texture2D* pInputTextures = NULL; + D3D11_TEXTURE2D_DESC desc; + ZeroMemory(&desc, sizeof(D3D11_TEXTURE2D_DESC)); + desc.Width = GetMaxEncodeWidth(); + desc.Height = GetMaxEncodeHeight(); + desc.MipLevels = 1; + desc.ArraySize = 1; + desc.Format = GetD3D11Format(GetPixelFormat()); + desc.SampleDesc.Count = 1; + desc.Usage = D3D11_USAGE_DEFAULT; + desc.BindFlags = D3D11_BIND_RENDER_TARGET; + desc.CPUAccessFlags = 0; + if (m_pD3D11Device->CreateTexture2D(&desc, NULL, &pInputTextures) != + S_OK) { + NVENC_THROW_ERROR("Failed to create d3d11textures", + NV_ENC_ERR_OUT_OF_MEMORY); + } + inputFrames.push_back(pInputTextures); + } + RegisterInputResources(inputFrames, NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX, + GetMaxEncodeWidth(), GetMaxEncodeHeight(), 0, + GetPixelFormat(), count == 1 ? true : false); + } +} + +void NvEncoderD3D11::ReleaseInputBuffers() { + ReleaseD3D11Resources(); +} + +void NvEncoderD3D11::ReleaseD3D11Resources() { + if (!m_hEncoder) { + return; + } + + UnregisterInputResources(); + + for (uint32_t i = 0; i < m_vInputFrames.size(); ++i) { + if (m_vInputFrames[i].inputPtr) { + reinterpret_cast(m_vInputFrames[i].inputPtr)->Release(); + } + } + m_vInputFrames.clear(); + + for (uint32_t i = 0; i < m_vReferenceFrames.size(); ++i) { + if (m_vReferenceFrames[i].inputPtr) { + reinterpret_cast(m_vReferenceFrames[i].inputPtr) + ->Release(); + } + } + m_vReferenceFrames.clear(); + + if (m_pD3D11DeviceContext) { + m_pD3D11DeviceContext->Release(); + m_pD3D11DeviceContext = nullptr; + } + + if (m_pD3D11Device) { + m_pD3D11Device->Release(); + m_pD3D11Device = nullptr; + } +} diff --git a/NvCodec/NvCodec/NvEncoder/NvEncoderD3D11.h b/NvCodec/NvCodec/NvEncoder/NvEncoderD3D11.h index 96530be4..18e77ed7 100644 --- a/NvCodec/NvCodec/NvEncoder/NvEncoderD3D11.h +++ b/NvCodec/NvCodec/NvEncoder/NvEncoderD3D11.h @@ -1,55 +1,59 @@ -/* -* Copyright 2017-2020 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ - -#pragma once - -#include -#include -#include -#include -#include -#include "NvEncoder.h" - -class NvEncoderD3D11 : public NvEncoder -{ -public: - NvEncoderD3D11(ID3D11Device* pD3D11Device, uint32_t nWidth, uint32_t nHeight, NV_ENC_BUFFER_FORMAT eBufferFormat, - uint32_t nExtraOutputDelay = 3, bool bMotionEstimationOnly = false, bool bOPInVideoMemory = false); - virtual ~NvEncoderD3D11(); - -protected: - /** - * @brief This function is used to release the input buffers allocated for encoding. - * This function is an override of virtual function NvEncoder::ReleaseInputBuffers(). - */ - virtual void ReleaseInputBuffers() override; - -private: - /** - * @brief This function is used to allocate input buffers for encoding. - * This function is an override of virtual function NvEncoder::AllocateInputBuffers(). - * This function creates ID3D11Texture2D textures which is used to accept input data. - * To obtain handle to input buffers application must call NvEncoder::GetNextInputFrame() - */ - virtual void AllocateInputBuffers(int32_t numInputBuffers) override; - -private: - /** - * @brief This is a private function to release ID3D11Texture2D textures used for encoding. - */ - void ReleaseD3D11Resources(); - -protected: - ID3D11Device *m_pD3D11Device = nullptr; - -private: - ID3D11DeviceContext* m_pD3D11DeviceContext = nullptr; -}; +/* +* Copyright 2017-2022 NVIDIA Corporation. All rights reserved. +* +* Please refer to the NVIDIA end user license agreement (EULA) associated +* with this source code for terms and conditions that govern your use of +* this software. Any use, reproduction, disclosure, or distribution of +* this software and related documentation outside the terms of the EULA +* is strictly prohibited. +* +*/ + +#pragma once + +#include +#include +#include +#include +#include +#include "NvEncoder.h" + +class NvEncoderD3D11 : public NvEncoder { + public: + NvEncoderD3D11(ID3D11Device* pD3D11Device, + uint32_t nWidth, + uint32_t nHeight, + NV_ENC_BUFFER_FORMAT eBufferFormat, + uint32_t nExtraOutputDelay = 3, + bool bMotionEstimationOnly = false, + bool bOPInVideoMemory = false); + virtual ~NvEncoderD3D11(); + + protected: + /** + * @brief This function is used to release the input buffers allocated for encoding. + * This function is an override of virtual function NvEncoder::ReleaseInputBuffers(). + */ + virtual void ReleaseInputBuffers() override; + + private: + /** + * @brief This function is used to allocate input buffers for encoding. + * This function is an override of virtual function NvEncoder::AllocateInputBuffers(). + * This function creates ID3D11Texture2D textures which is used to accept input data. + * To obtain handle to input buffers application must call NvEncoder::GetNextInputFrame() + */ + virtual void AllocateInputBuffers(int32_t numInputBuffers) override; + + private: + /** + * @brief This is a private function to release ID3D11Texture2D textures used for encoding. + */ + void ReleaseD3D11Resources(); + + protected: + ID3D11Device* m_pD3D11Device = nullptr; + + private: + ID3D11DeviceContext* m_pD3D11DeviceContext = nullptr; +}; diff --git a/NvCodec/Utils/Logger.h b/NvCodec/Utils/Logger.h index a022ada5..1901627c 100644 --- a/NvCodec/Utils/Logger.h +++ b/NvCodec/Utils/Logger.h @@ -1,240 +1,242 @@ -/* -* Copyright 2017-2020 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ - -#pragma once - -#include -#include -#include -#include -#include -#include - -#ifdef _WIN32 -#include -#include - -#pragma comment(lib, "ws2_32.lib") -#undef ERROR -#else -#include -#include -#include -#include -#define SOCKET int -#define INVALID_SOCKET -1 -#endif - -enum LogLevel { - TRACE, - INFO, - WARNING, - ERROR, - FATAL -}; - -namespace simplelogger{ -class Logger { -public: - Logger(LogLevel level, bool bPrintTimeStamp) : level(level), bPrintTimeStamp(bPrintTimeStamp) {} - virtual ~Logger() {} - virtual std::ostream& GetStream() = 0; - virtual void FlushStream() {} - bool ShouldLogFor(LogLevel l) { - return l >= level; - } - char* GetLead(LogLevel l, const char *szFile, int nLine, const char *szFunc) { - if (l < TRACE || l > FATAL) { - sprintf(szLead, "[?????] "); - return szLead; - } - const char *szLevels[] = {"TRACE", "INFO", "WARN", "ERROR", "FATAL"}; - if (bPrintTimeStamp) { - time_t t = time(NULL); - struct tm *ptm = localtime(&t); - sprintf(szLead, "[%-5s][%02d:%02d:%02d] ", - szLevels[l], ptm->tm_hour, ptm->tm_min, ptm->tm_sec); - } else { - sprintf(szLead, "[%-5s] ", szLevels[l]); - } - return szLead; - } - void EnterCriticalSection() { - mtx.lock(); - } - void LeaveCriticalSection() { - mtx.unlock(); - } -private: - LogLevel level; - char szLead[80]; - bool bPrintTimeStamp; - std::mutex mtx; -}; - -class LoggerFactory { -public: - static Logger* CreateFileLogger(std::string strFilePath, - LogLevel level = INFO, bool bPrintTimeStamp = true) { - return new FileLogger(strFilePath, level, bPrintTimeStamp); - } - static Logger* CreateConsoleLogger(LogLevel level = INFO, - bool bPrintTimeStamp = true) { - return new ConsoleLogger(level, bPrintTimeStamp); - } - static Logger* CreateUdpLogger(char *szHost, unsigned uPort, LogLevel level = INFO, - bool bPrintTimeStamp = true) { - return new UdpLogger(szHost, uPort, level, bPrintTimeStamp); - } -private: - LoggerFactory() {} - - class FileLogger : public Logger { - public: - FileLogger(std::string strFilePath, LogLevel level, bool bPrintTimeStamp) - : Logger(level, bPrintTimeStamp) { - pFileOut = new std::ofstream(); - pFileOut->open(strFilePath.c_str()); - } - ~FileLogger() { - pFileOut->close(); - } - std::ostream& GetStream() { - return *pFileOut; - } - private: - std::ofstream *pFileOut; - }; - - class ConsoleLogger : public Logger { - public: - ConsoleLogger(LogLevel level, bool bPrintTimeStamp) - : Logger(level, bPrintTimeStamp) {} - std::ostream& GetStream() { - return std::cout; - } - }; - - class UdpLogger : public Logger { - private: - class UdpOstream : public std::ostream { - public: - UdpOstream(char *szHost, unsigned short uPort) : std::ostream(&sb), socket(INVALID_SOCKET){ -#ifdef _WIN32 - WSADATA w; - if (WSAStartup(0x0101, &w) != 0) { - fprintf(stderr, "WSAStartup() failed.\n"); - return; - } -#endif - socket = ::socket(AF_INET, SOCK_DGRAM, 0); - if (socket == INVALID_SOCKET) { -#ifdef _WIN32 - WSACleanup(); -#endif - fprintf(stderr, "socket() failed.\n"); - return; - } -#ifdef _WIN32 - unsigned int b1, b2, b3, b4; - sscanf(szHost, "%u.%u.%u.%u", &b1, &b2, &b3, &b4); - struct in_addr addr = {(unsigned char)b1, (unsigned char)b2, (unsigned char)b3, (unsigned char)b4}; -#else - struct in_addr addr = {inet_addr(szHost)}; -#endif - struct sockaddr_in s = {AF_INET, htons(uPort), addr}; - server = s; - } - ~UdpOstream() throw() { - if (socket == INVALID_SOCKET) { - return; - } -#ifdef _WIN32 - closesocket(socket); - WSACleanup(); -#else - close(socket); -#endif - } - void Flush() { - if (sendto(socket, sb.str().c_str(), (int)sb.str().length() + 1, - 0, (struct sockaddr *)&server, (int)sizeof(sockaddr_in)) == -1) { - fprintf(stderr, "sendto() failed.\n"); - } - sb.str(""); - } - - private: - std::stringbuf sb; - SOCKET socket; - struct sockaddr_in server; - }; - public: - UdpLogger(char *szHost, unsigned uPort, LogLevel level, bool bPrintTimeStamp) - : Logger(level, bPrintTimeStamp), udpOut(szHost, (unsigned short)uPort) {} - UdpOstream& GetStream() { - return udpOut; - } - virtual void FlushStream() { - udpOut.Flush(); - } - private: - UdpOstream udpOut; - }; -}; - -class LogTransaction { -public: - LogTransaction(Logger *pLogger, LogLevel level, const char *szFile, const int nLine, const char *szFunc) : pLogger(pLogger), level(level) { - if (!pLogger) { - std::cout << "[-----] "; - return; - } - if (!pLogger->ShouldLogFor(level)) { - return; - } - pLogger->EnterCriticalSection(); - pLogger->GetStream() << pLogger->GetLead(level, szFile, nLine, szFunc); - } - ~LogTransaction() { - if (!pLogger) { - std::cout << std::endl; - return; - } - if (!pLogger->ShouldLogFor(level)) { - return; - } - pLogger->GetStream() << std::endl; - pLogger->FlushStream(); - pLogger->LeaveCriticalSection(); - if (level == FATAL) { - exit(1); - } - } - std::ostream& GetStream() { - if (!pLogger) { - return std::cout; - } - if (!pLogger->ShouldLogFor(level)) { - return ossNull; - } - return pLogger->GetStream(); - } -private: - Logger *pLogger; - LogLevel level; - std::ostringstream ossNull; -}; - -} - -extern simplelogger::Logger *logger; -#define LOG(level) simplelogger::LogTransaction(logger, level, __FILE__, __LINE__, __FUNCTION__).GetStream() +/* +* Copyright 2017-2022 NVIDIA Corporation. All rights reserved. +* +* Please refer to the NVIDIA end user license agreement (EULA) associated +* with this source code for terms and conditions that govern your use of +* this software. Any use, reproduction, disclosure, or distribution of +* this software and related documentation outside the terms of the EULA +* is strictly prohibited. +* +*/ + +#pragma once + +#include +#include +#include +#include +#include +#include + +#ifdef _WIN32 +#include +#include + +#pragma comment(lib, "ws2_32.lib") +#undef ERROR +#else +#include +#include +#include +#include +#define SOCKET int +#define INVALID_SOCKET -1 +#endif + +enum LogLevel { TRACE, INFO, WARNING, ERROR, FATAL }; + +namespace simplelogger { +class Logger { + public: + Logger(LogLevel level, bool bPrintTimeStamp) + : level(level), bPrintTimeStamp(bPrintTimeStamp) {} + virtual ~Logger() {} + virtual std::ostream& GetStream() = 0; + virtual void FlushStream() {} + bool ShouldLogFor(LogLevel l) { return l >= level; } + char* GetLead(LogLevel l, const char* szFile, int nLine, const char* szFunc) { + if (l < TRACE || l > FATAL) { + sprintf(szLead, "[?????] "); + return szLead; + } + const char* szLevels[] = {"TRACE", "INFO", "WARN", "ERROR", "FATAL"}; + if (bPrintTimeStamp) { + time_t t = time(NULL); + struct tm* ptm = localtime(&t); + sprintf(szLead, "[%-5s][%02d:%02d:%02d] ", szLevels[l], ptm->tm_hour, + ptm->tm_min, ptm->tm_sec); + } else { + sprintf(szLead, "[%-5s] ", szLevels[l]); + } + return szLead; + } + void EnterCriticalSection() { mtx.lock(); } + void LeaveCriticalSection() { mtx.unlock(); } + + private: + LogLevel level; + char szLead[80]; + bool bPrintTimeStamp; + std::mutex mtx; +}; + +class LoggerFactory { + public: + static Logger* CreateFileLogger(std::string strFilePath, + LogLevel level = INFO, + bool bPrintTimeStamp = true) { + return new FileLogger(strFilePath, level, bPrintTimeStamp); + } + static Logger* CreateConsoleLogger(LogLevel level = INFO, + bool bPrintTimeStamp = true) { + return new ConsoleLogger(level, bPrintTimeStamp); + } + static Logger* CreateUdpLogger(char* szHost, + unsigned uPort, + LogLevel level = INFO, + bool bPrintTimeStamp = true) { + return new UdpLogger(szHost, uPort, level, bPrintTimeStamp); + } + + private: + LoggerFactory() {} + + class FileLogger : public Logger { + public: + FileLogger(std::string strFilePath, LogLevel level, bool bPrintTimeStamp) + : Logger(level, bPrintTimeStamp) { + pFileOut = new std::ofstream(); + pFileOut->open(strFilePath.c_str()); + } + ~FileLogger() { pFileOut->close(); } + std::ostream& GetStream() { return *pFileOut; } + + private: + std::ofstream* pFileOut; + }; + + class ConsoleLogger : public Logger { + public: + ConsoleLogger(LogLevel level, bool bPrintTimeStamp) + : Logger(level, bPrintTimeStamp) {} + std::ostream& GetStream() { return std::cout; } + }; + + class UdpLogger : public Logger { + private: + class UdpOstream : public std::ostream { + public: + UdpOstream(char* szHost, unsigned short uPort) + : std::ostream(&sb), socket(INVALID_SOCKET) { +#ifdef _WIN32 + WSADATA w; + if (WSAStartup(0x0101, &w) != 0) { + fprintf(stderr, "WSAStartup() failed.\n"); + return; + } +#endif + socket = ::socket(AF_INET, SOCK_DGRAM, 0); + if (socket == INVALID_SOCKET) { +#ifdef _WIN32 + WSACleanup(); +#endif + fprintf(stderr, "socket() failed.\n"); + return; + } +#ifdef _WIN32 + unsigned int b1, b2, b3, b4; + sscanf(szHost, "%u.%u.%u.%u", &b1, &b2, &b3, &b4); + struct in_addr addr = {(unsigned char)b1, (unsigned char)b2, + (unsigned char)b3, (unsigned char)b4}; +#else + struct in_addr addr = {inet_addr(szHost)}; +#endif + struct sockaddr_in s = {AF_INET, htons(uPort), addr}; + server = s; + } + ~UdpOstream() throw() { + if (socket == INVALID_SOCKET) { + return; + } +#ifdef _WIN32 + closesocket(socket); + WSACleanup(); +#else + close(socket); +#endif + } + void Flush() { + if (sendto(socket, sb.str().c_str(), (int)sb.str().length() + 1, 0, + (struct sockaddr*)&server, (int)sizeof(sockaddr_in)) == -1) { + fprintf(stderr, "sendto() failed.\n"); + } + sb.str(""); + } + + private: + std::stringbuf sb; + SOCKET socket; + struct sockaddr_in server; + }; + + public: + UdpLogger(char* szHost, + unsigned uPort, + LogLevel level, + bool bPrintTimeStamp) + : Logger(level, bPrintTimeStamp), + udpOut(szHost, (unsigned short)uPort) {} + UdpOstream& GetStream() { return udpOut; } + virtual void FlushStream() { udpOut.Flush(); } + + private: + UdpOstream udpOut; + }; +}; + +class LogTransaction { + public: + LogTransaction(Logger* pLogger, + LogLevel level, + const char* szFile, + const int nLine, + const char* szFunc) + : pLogger(pLogger), level(level) { + if (!pLogger) { + std::cout << "[-----] "; + return; + } + if (!pLogger->ShouldLogFor(level)) { + return; + } + pLogger->EnterCriticalSection(); + pLogger->GetStream() << pLogger->GetLead(level, szFile, nLine, szFunc); + } + ~LogTransaction() { + if (!pLogger) { + std::cout << std::endl; + return; + } + if (!pLogger->ShouldLogFor(level)) { + return; + } + pLogger->GetStream() << std::endl; + pLogger->FlushStream(); + pLogger->LeaveCriticalSection(); + if (level == FATAL) { + exit(1); + } + } + std::ostream& GetStream() { + if (!pLogger) { + return std::cout; + } + if (!pLogger->ShouldLogFor(level)) { + return ossNull; + } + return pLogger->GetStream(); + } + + private: + Logger* pLogger; + LogLevel level; + std::ostringstream ossNull; +}; + +} // namespace simplelogger + +extern simplelogger::Logger* logger; +#define LOG(level) \ + simplelogger::LogTransaction(logger, level, __FILE__, __LINE__, \ + __FUNCTION__) \ + .GetStream() diff --git a/NvCodec/Utils/NvCodecUtils.h b/NvCodec/Utils/NvCodecUtils.h index 0b595fb6..fd6d33e0 100644 --- a/NvCodec/Utils/NvCodecUtils.h +++ b/NvCodec/Utils/NvCodecUtils.h @@ -1,464 +1,651 @@ -/* -* Copyright 2017-2020 NVIDIA Corporation. All rights reserved. -* -* Please refer to the NVIDIA end user license agreement (EULA) associated -* with this source code for terms and conditions that govern your use of -* this software. Any use, reproduction, disclosure, or distribution of -* this software and related documentation outside the terms of the EULA -* is strictly prohibited. -* -*/ - -//--------------------------------------------------------------------------- -//! \file NvCodecUtils.h -//! \brief Miscellaneous classes and error checking functions. -//! -//! Used by Transcode/Encode samples apps for reading input files, mutithreading, performance measurement or colorspace conversion while decoding. -//--------------------------------------------------------------------------- - -#pragma once -#include -#include -#include -#include -#include -#include -#include "Logger.h" -#include -#include -#include -#include -#include -#include "dyn/cuda.h" - -extern simplelogger::Logger *logger; - -#ifdef __cuda_cuda_h__ -inline bool check(CUresult e, int iLine, const char *szFile) { - if (e != CUDA_SUCCESS) { - const char *szErrName = NULL; - dyn::cuGetErrorName(e, &szErrName); - LOG(ERROR) << "CUDA driver API error " << szErrName << " at line " << iLine << " in file " << szFile; - return false; - } - return true; -} -#endif - -#ifdef __CUDA_RUNTIME_H__ -inline bool check(cudaError_t e, int iLine, const char *szFile) { - if (e != cudaSuccess) { - LOG(ERROR) << "CUDA runtime API error " << cudaGetErrorName(e) << " at line " << iLine << " in file " << szFile; - return false; - } - return true; -} -#endif - -#ifdef _NV_ENCODEAPI_H_ -inline bool check(NVENCSTATUS e, int iLine, const char *szFile) { - const char *aszErrName[] = { - "NV_ENC_SUCCESS", - "NV_ENC_ERR_NO_ENCODE_DEVICE", - "NV_ENC_ERR_UNSUPPORTED_DEVICE", - "NV_ENC_ERR_INVALID_ENCODERDEVICE", - "NV_ENC_ERR_INVALID_DEVICE", - "NV_ENC_ERR_DEVICE_NOT_EXIST", - "NV_ENC_ERR_INVALID_PTR", - "NV_ENC_ERR_INVALID_EVENT", - "NV_ENC_ERR_INVALID_PARAM", - "NV_ENC_ERR_INVALID_CALL", - "NV_ENC_ERR_OUT_OF_MEMORY", - "NV_ENC_ERR_ENCODER_NOT_INITIALIZED", - "NV_ENC_ERR_UNSUPPORTED_PARAM", - "NV_ENC_ERR_LOCK_BUSY", - "NV_ENC_ERR_NOT_ENOUGH_BUFFER", - "NV_ENC_ERR_INVALID_VERSION", - "NV_ENC_ERR_MAP_FAILED", - "NV_ENC_ERR_NEED_MORE_INPUT", - "NV_ENC_ERR_ENCODER_BUSY", - "NV_ENC_ERR_EVENT_NOT_REGISTERD", - "NV_ENC_ERR_GENERIC", - "NV_ENC_ERR_INCOMPATIBLE_CLIENT_KEY", - "NV_ENC_ERR_UNIMPLEMENTED", - "NV_ENC_ERR_RESOURCE_REGISTER_FAILED", - "NV_ENC_ERR_RESOURCE_NOT_REGISTERED", - "NV_ENC_ERR_RESOURCE_NOT_MAPPED", - }; - if (e != NV_ENC_SUCCESS) { - LOG(ERROR) << "NVENC error " << aszErrName[e] << " at line " << iLine << " in file " << szFile; - return false; - } - return true; -} -#endif - -#ifdef _WINERROR_ -inline bool check(HRESULT e, int iLine, const char *szFile) { - if (e != S_OK) { - std::stringstream stream; - stream << std::hex << std::uppercase << e; - LOG(ERROR) << "HRESULT error 0x" << stream.str() << " at line " << iLine << " in file " << szFile; - return false; - } - return true; -} -#endif - -#if defined(__gl_h_) || defined(__GL_H__) -inline bool check(GLenum e, int iLine, const char *szFile) { - if (e != 0) { - LOG(ERROR) << "GLenum error " << e << " at line " << iLine << " in file " << szFile; - return false; - } - return true; -} -#endif - -inline bool check(int e, int iLine, const char *szFile) { - if (e < 0) { - LOG(ERROR) << "General error " << e << " at line " << iLine << " in file " << szFile; - return false; - } - return true; -} - -#define ck(call) check(call, __LINE__, __FILE__) - -/** -* @brief Wrapper class around std::thread -*/ -class NvThread -{ -public: - NvThread() = default; - NvThread(const NvThread&) = delete; - NvThread& operator=(const NvThread& other) = delete; - - NvThread(std::thread&& thread) : t(std::move(thread)) - { - - } - - NvThread(NvThread&& thread) : t(std::move(thread.t)) - { - - } - - NvThread& operator=(NvThread&& other) - { - t = std::move(other.t); - return *this; - } - - ~NvThread() - { - join(); - } - - void join() - { - if (t.joinable()) - { - t.join(); - } - } -private: - std::thread t; -}; - -#ifndef _WIN32 -#define _stricmp strcasecmp -#define _stat64 stat64 -#endif - -/** -* @brief Utility class to allocate buffer memory. Helps avoid I/O during the encode/decode loop in case of performance tests. -*/ -class BufferedFileReader { -public: - /** - * @brief Constructor function to allocate appropriate memory and copy file contents into it - */ - BufferedFileReader(const char *szFileName, bool bPartial = false) { - struct _stat64 st; - - if (_stat64(szFileName, &st) != 0) { - return; - } - - nSize = st.st_size; - while (nSize) { - try { - pBuf = new uint8_t[(size_t)nSize]; - if (nSize != st.st_size) { - LOG(WARNING) << "File is too large - only " << std::setprecision(4) << 100.0 * nSize / st.st_size << "% is loaded"; - } - break; - } catch(std::bad_alloc) { - if (!bPartial) { - LOG(ERROR) << "Failed to allocate memory in BufferedReader"; - return; - } - nSize = (uint32_t)(nSize * 0.9); - } - } - - std::ifstream fpIn(szFileName, std::ifstream::in | std::ifstream::binary); - if (!fpIn) - { - LOG(ERROR) << "Unable to open input file: " << szFileName; - return; - } - - std::streamsize nRead = fpIn.read(reinterpret_cast(pBuf), nSize).gcount(); - fpIn.close(); - - assert(nRead == nSize); - } - ~BufferedFileReader() { - if (pBuf) { - delete[] pBuf; - } - } - bool GetBuffer(uint8_t **ppBuf, uint64_t *pnSize) { - if (!pBuf) { - return false; - } - - *ppBuf = pBuf; - *pnSize = nSize; - return true; - } - -private: - uint8_t *pBuf = NULL; - uint64_t nSize = 0; -}; - -/** -* @brief Template class to facilitate color space conversion -*/ -template -class YuvConverter { -public: - YuvConverter(int nWidth, int nHeight) : nWidth(nWidth), nHeight(nHeight) { - pQuad = new T[nWidth * nHeight / 4]; - } - ~YuvConverter() { - delete pQuad; - } - void PlanarToUVInterleaved(T *pFrame, int nPitch = 0) { - if (nPitch == 0) { - nPitch = nWidth; - } - T *puv = pFrame + nPitch * nHeight; - if (nPitch == nWidth) { - memcpy(pQuad, puv, nWidth * nHeight / 4 * sizeof(T)); - } else { - for (int i = 0; i < nHeight / 2; i++) { - memcpy(pQuad + nWidth / 2 * i, puv + nPitch / 2 * i, nWidth / 2 * sizeof(T)); - } - } - T *pv = puv + (nPitch / 2) * (nHeight / 2); - for (int y = 0; y < nHeight / 2; y++) { - for (int x = 0; x < nWidth / 2; x++) { - puv[y * nPitch + x * 2] = pQuad[y * nWidth / 2 + x]; - puv[y * nPitch + x * 2 + 1] = pv[y * nPitch / 2 + x]; - } - } - } - void UVInterleavedToPlanar(T *pFrame, int nPitch = 0) { - if (nPitch == 0) { - nPitch = nWidth; - } - T *puv = pFrame + nPitch * nHeight, - *pu = puv, - *pv = puv + nPitch * nHeight / 4; - for (int y = 0; y < nHeight / 2; y++) { - for (int x = 0; x < nWidth / 2; x++) { - pu[y * nPitch / 2 + x] = puv[y * nPitch + x * 2]; - pQuad[y * nWidth / 2 + x] = puv[y * nPitch + x * 2 + 1]; - } - } - if (nPitch == nWidth) { - memcpy(pv, pQuad, nWidth * nHeight / 4 * sizeof(T)); - } else { - for (int i = 0; i < nHeight / 2; i++) { - memcpy(pv + nPitch / 2 * i, pQuad + nWidth / 2 * i, nWidth / 2 * sizeof(T)); - } - } - } - -private: - T *pQuad; - int nWidth, nHeight; -}; - -/** -* @brief Utility class to measure elapsed time in seconds between the block of executed code -*/ -class StopWatch { -public: - void Start() { - t0 = std::chrono::high_resolution_clock::now(); - } - double Stop() { - return std::chrono::duration_cast(std::chrono::high_resolution_clock::now().time_since_epoch() - t0.time_since_epoch()).count() / 1.0e9; - } - -private: - std::chrono::high_resolution_clock::time_point t0; -}; - -template -class ConcurrentQueue -{ - public: - - ConcurrentQueue() {} - ConcurrentQueue(size_t size) : maxSize(size) {} - ConcurrentQueue(const ConcurrentQueue&) = delete; - ConcurrentQueue& operator=(const ConcurrentQueue&) = delete; - - void setSize(size_t s) { - maxSize = s; - } - - void push_back(const T& value) { - // Do not use a std::lock_guard here. We will need to explicitly - // unlock before notify_one as the other waiting thread will - // automatically try to acquire mutex once it wakes up - // (which will happen on notify_one) - std::unique_lock lock(m_mutex); - auto wasEmpty = m_List.empty(); - - while (full()) { - m_cond.wait(lock); - } - - m_List.push_back(value); - if (wasEmpty && !m_List.empty()) { - lock.unlock(); - m_cond.notify_one(); - } - } - - T pop_front() { - std::unique_lock lock(m_mutex); - - while (m_List.empty()) { - m_cond.wait(lock); - } - auto wasFull = full(); - T data = std::move(m_List.front()); - m_List.pop_front(); - - if (wasFull && !full()) { - lock.unlock(); - m_cond.notify_one(); - } - - return data; - } - - T front() { - std::unique_lock lock(m_mutex); - - while (m_List.empty()) { - m_cond.wait(lock); - } - - return m_List.front(); - } - - size_t size() { - std::unique_lock lock(m_mutex); - return m_List.size(); - } - - bool empty() { - std::unique_lock lock(m_mutex); - return m_List.empty(); - } - void clear() { - std::unique_lock lock(m_mutex); - m_List.clear(); - } - -private: - bool full() { - if (m_List.size() == maxSize) - return true; - return false; - } - -private: - std::list m_List; - std::mutex m_mutex; - std::condition_variable m_cond; - size_t maxSize; -}; - -inline void CheckInputFile(const char *szInFilePath) { - std::ifstream fpIn(szInFilePath, std::ios::in | std::ios::binary); - if (fpIn.fail()) { - std::ostringstream err; - err << "Unable to open input file: " << szInFilePath << std::endl; - throw std::invalid_argument(err.str()); - } -} - -inline void ValidateResolution(int nWidth, int nHeight) { - - if (nWidth <= 0 || nHeight <= 0) { - std::ostringstream err; - err << "Please specify positive non zero resolution as -s WxH. Current resolution is " << nWidth << "x" << nHeight << std::endl; - throw std::invalid_argument(err.str()); - } -} - -template -void Nv12ToColor32(uint8_t *dpNv12, int nNv12Pitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix = 0); -template -void Nv12ToColor64(uint8_t *dpNv12, int nNv12Pitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix = 0); - -template -void P016ToColor32(uint8_t *dpP016, int nP016Pitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix = 4); -template -void P016ToColor64(uint8_t *dpP016, int nP016Pitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix = 4); - -template -void YUV444ToColor32(uint8_t *dpYUV444, int nPitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix = 0); -template -void YUV444ToColor64(uint8_t *dpYUV444, int nPitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix = 0); - -template -void YUV444P16ToColor32(uint8_t *dpYUV444, int nPitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix = 4); -template -void YUV444P16ToColor64(uint8_t *dpYUV444, int nPitch, uint8_t *dpBgra, int nBgraPitch, int nWidth, int nHeight, int iMatrix = 4); - -template -void Nv12ToColorPlanar(uint8_t *dpNv12, int nNv12Pitch, uint8_t *dpBgrp, int nBgrpPitch, int nWidth, int nHeight, int iMatrix = 0); -template -void P016ToColorPlanar(uint8_t *dpP016, int nP016Pitch, uint8_t *dpBgrp, int nBgrpPitch, int nWidth, int nHeight, int iMatrix = 4); - -template -void YUV444ToColorPlanar(uint8_t *dpYUV444, int nPitch, uint8_t *dpBgrp, int nBgrpPitch, int nWidth, int nHeight, int iMatrix = 0); -template -void YUV444P16ToColorPlanar(uint8_t *dpYUV444, int nPitch, uint8_t *dpBgrp, int nBgrpPitch, int nWidth, int nHeight, int iMatrix = 4); - -void Bgra64ToP016(uint8_t *dpBgra, int nBgraPitch, uint8_t *dpP016, int nP016Pitch, int nWidth, int nHeight, int iMatrix = 4); - -void ConvertUInt8ToUInt16(uint8_t *dpUInt8, uint16_t *dpUInt16, int nSrcPitch, int nDestPitch, int nWidth, int nHeight); -void ConvertUInt16ToUInt8(uint16_t *dpUInt16, uint8_t *dpUInt8, int nSrcPitch, int nDestPitch, int nWidth, int nHeight); - -void ResizeNv12(unsigned char *dpDstNv12, int nDstPitch, int nDstWidth, int nDstHeight, unsigned char *dpSrcNv12, int nSrcPitch, int nSrcWidth, int nSrcHeight, unsigned char *dpDstNv12UV = nullptr); -void ResizeP016(unsigned char *dpDstP016, int nDstPitch, int nDstWidth, int nDstHeight, unsigned char *dpSrcP016, int nSrcPitch, int nSrcWidth, int nSrcHeight, unsigned char *dpDstP016UV = nullptr); - -void ScaleYUV420(unsigned char *dpDstY, unsigned char* dpDstU, unsigned char* dpDstV, int nDstPitch, int nDstChromaPitch, int nDstWidth, int nDstHeight, - unsigned char *dpSrcY, unsigned char* dpSrcU, unsigned char* dpSrcV, int nSrcPitch, int nSrcChromaPitch, int nSrcWidth, int nSrcHeight, bool bSemiplanar); - -#ifdef __cuda_cuda_h__ -void ComputeCRC(uint8_t *pBuffer, uint32_t *crcValue, CUstream_st *outputCUStream); -#endif +/* +* Copyright 2017-2022 NVIDIA Corporation. All rights reserved. +* +* Please refer to the NVIDIA end user license agreement (EULA) associated +* with this source code for terms and conditions that govern your use of +* this software. Any use, reproduction, disclosure, or distribution of +* this software and related documentation outside the terms of the EULA +* is strictly prohibited. +* +*/ + +//--------------------------------------------------------------------------- +//! \file NvCodecUtils.h +//! \brief Miscellaneous classes and error checking functions. +//! +//! Used by Transcode/Encode samples apps for reading input files, mutithreading, performance measurement or colorspace conversion while decoding. +//--------------------------------------------------------------------------- + +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "Logger.h" +#include "dyn/cuda.h" + +extern simplelogger::Logger* logger; + +#ifdef __cuda_cuda_h__ +inline bool check(CUresult e, int iLine, const char* szFile) { + if (e != CUDA_SUCCESS) { + const char* szErrName = NULL; + dyn::cuGetErrorName(e, &szErrName); + LOG(ERROR) << "CUDA driver API error " << szErrName << " at line " << iLine + << " in file " << szFile; + return false; + } + return true; +} +#endif + +#ifdef __CUDA_RUNTIME_H__ +inline bool check(cudaError_t e, int iLine, const char* szFile) { + if (e != cudaSuccess) { + LOG(ERROR) << "CUDA runtime API error " << cudaGetErrorName(e) + << " at line " << iLine << " in file " << szFile; + return false; + } + return true; +} +#endif + +#ifdef _NV_ENCODEAPI_H_ +inline bool check(NVENCSTATUS e, int iLine, const char* szFile) { + const char* aszErrName[] = { + "NV_ENC_SUCCESS", + "NV_ENC_ERR_NO_ENCODE_DEVICE", + "NV_ENC_ERR_UNSUPPORTED_DEVICE", + "NV_ENC_ERR_INVALID_ENCODERDEVICE", + "NV_ENC_ERR_INVALID_DEVICE", + "NV_ENC_ERR_DEVICE_NOT_EXIST", + "NV_ENC_ERR_INVALID_PTR", + "NV_ENC_ERR_INVALID_EVENT", + "NV_ENC_ERR_INVALID_PARAM", + "NV_ENC_ERR_INVALID_CALL", + "NV_ENC_ERR_OUT_OF_MEMORY", + "NV_ENC_ERR_ENCODER_NOT_INITIALIZED", + "NV_ENC_ERR_UNSUPPORTED_PARAM", + "NV_ENC_ERR_LOCK_BUSY", + "NV_ENC_ERR_NOT_ENOUGH_BUFFER", + "NV_ENC_ERR_INVALID_VERSION", + "NV_ENC_ERR_MAP_FAILED", + "NV_ENC_ERR_NEED_MORE_INPUT", + "NV_ENC_ERR_ENCODER_BUSY", + "NV_ENC_ERR_EVENT_NOT_REGISTERD", + "NV_ENC_ERR_GENERIC", + "NV_ENC_ERR_INCOMPATIBLE_CLIENT_KEY", + "NV_ENC_ERR_UNIMPLEMENTED", + "NV_ENC_ERR_RESOURCE_REGISTER_FAILED", + "NV_ENC_ERR_RESOURCE_NOT_REGISTERED", + "NV_ENC_ERR_RESOURCE_NOT_MAPPED", + }; + if (e != NV_ENC_SUCCESS) { + LOG(ERROR) << "NVENC error " << aszErrName[e] << " at line " << iLine + << " in file " << szFile; + return false; + } + return true; +} +#endif + +#ifdef _WINERROR_ +inline bool check(HRESULT e, int iLine, const char* szFile) { + if (e != S_OK) { + std::stringstream stream; + stream << std::hex << std::uppercase << e; + LOG(ERROR) << "HRESULT error 0x" << stream.str() << " at line " << iLine + << " in file " << szFile; + return false; + } + return true; +} +#endif + +#if defined(__gl_h_) || defined(__GL_H__) +inline bool check(GLenum e, int iLine, const char* szFile) { + if (e != 0) { + LOG(ERROR) << "GLenum error " << e << " at line " << iLine << " in file " + << szFile; + return false; + } + return true; +} +#endif + +inline bool check(int e, int iLine, const char* szFile) { + if (e < 0) { + LOG(ERROR) << "General error " << e << " at line " << iLine << " in file " + << szFile; + return false; + } + return true; +} + +#define ck(call) check(call, __LINE__, __FILE__) +#define MAKE_FOURCC(ch0, ch1, ch2, ch3) \ + ((uint32_t)(uint8_t)(ch0) | ((uint32_t)(uint8_t)(ch1) << 8) | \ + ((uint32_t)(uint8_t)(ch2) << 16) | ((uint32_t)(uint8_t)(ch3) << 24)) + +/** +* @brief Wrapper class around std::thread +*/ +class NvThread { + public: + NvThread() = default; + NvThread(const NvThread&) = delete; + NvThread& operator=(const NvThread& other) = delete; + + NvThread(std::thread&& thread) : t(std::move(thread)) {} + + NvThread(NvThread&& thread) : t(std::move(thread.t)) {} + + NvThread& operator=(NvThread&& other) { + t = std::move(other.t); + return *this; + } + + ~NvThread() { join(); } + + void join() { + if (t.joinable()) { + t.join(); + } + } + + private: + std::thread t; +}; + +#ifndef _WIN32 +#define _stricmp strcasecmp +#define _stat64 stat64 +#endif + +/** +* @brief Utility class to allocate buffer memory. Helps avoid I/O during the encode/decode loop in case of performance tests. +*/ +class BufferedFileReader { + public: + /** + * @brief Constructor function to allocate appropriate memory and copy file contents into it + */ + BufferedFileReader(const char* szFileName, bool bPartial = false) { + struct _stat64 st; + + if (_stat64(szFileName, &st) != 0) { + return; + } + + nSize = st.st_size; + while (nSize) { + try { + pBuf = new uint8_t[(size_t)nSize]; + if (nSize != st.st_size) { + LOG(WARNING) << "File is too large - only " << std::setprecision(4) + << 100.0 * nSize / st.st_size << "% is loaded"; + } + break; + } catch (std::bad_alloc) { + if (!bPartial) { + LOG(ERROR) << "Failed to allocate memory in BufferedReader"; + return; + } + nSize = (uint32_t)(nSize * 0.9); + } + } + + std::ifstream fpIn(szFileName, std::ifstream::in | std::ifstream::binary); + if (!fpIn) { + LOG(ERROR) << "Unable to open input file: " << szFileName; + return; + } + + std::streamsize nRead = + fpIn.read(reinterpret_cast(pBuf), nSize).gcount(); + fpIn.close(); + + assert(nRead == nSize); + } + ~BufferedFileReader() { + if (pBuf) { + delete[] pBuf; + } + } + bool GetBuffer(uint8_t** ppBuf, uint64_t* pnSize) { + if (!pBuf) { + return false; + } + + *ppBuf = pBuf; + *pnSize = nSize; + return true; + } + + private: + uint8_t* pBuf = NULL; + uint64_t nSize = 0; +}; + +/** +* @brief Template class to facilitate color space conversion +*/ +template +class YuvConverter { + public: + YuvConverter(int nWidth, int nHeight) : nWidth(nWidth), nHeight(nHeight) { + pQuad = new T[((nWidth + 1) / 2) * ((nHeight + 1) / 2)]; + } + ~YuvConverter() { delete[] pQuad; } + void PlanarToUVInterleaved(T* pFrame, int nPitch = 0) { + if (nPitch == 0) { + nPitch = nWidth; + } + + // sizes of source surface plane + int nSizePlaneY = nPitch * nHeight; + int nSizePlaneU = ((nPitch + 1) / 2) * ((nHeight + 1) / 2); + int nSizePlaneV = nSizePlaneU; + + T* puv = pFrame + nSizePlaneY; + if (nPitch == nWidth) { + memcpy(pQuad, puv, nSizePlaneU * sizeof(T)); + } else { + for (int i = 0; i < (nHeight + 1) / 2; i++) { + memcpy(pQuad + ((nWidth + 1) / 2) * i, puv + ((nPitch + 1) / 2) * i, + ((nWidth + 1) / 2) * sizeof(T)); + } + } + T* pv = puv + nSizePlaneU; + for (int y = 0; y < (nHeight + 1) / 2; y++) { + for (int x = 0; x < (nWidth + 1) / 2; x++) { + puv[y * nPitch + x * 2] = pQuad[y * ((nWidth + 1) / 2) + x]; + puv[y * nPitch + x * 2 + 1] = pv[y * ((nPitch + 1) / 2) + x]; + } + } + } + void UVInterleavedToPlanar(T* pFrame, int nPitch = 0) { + if (nPitch == 0) { + nPitch = nWidth; + } + + // sizes of source surface plane + int nSizePlaneY = nPitch * nHeight; + int nSizePlaneU = ((nPitch + 1) / 2) * ((nHeight + 1) / 2); + int nSizePlaneV = nSizePlaneU; + + T *puv = pFrame + nSizePlaneY, *pu = puv, *pv = puv + nSizePlaneU; + + // split chroma from interleave to planar + for (int y = 0; y < (nHeight + 1) / 2; y++) { + for (int x = 0; x < (nWidth + 1) / 2; x++) { + pu[y * ((nPitch + 1) / 2) + x] = puv[y * nPitch + x * 2]; + pQuad[y * ((nWidth + 1) / 2) + x] = puv[y * nPitch + x * 2 + 1]; + } + } + if (nPitch == nWidth) { + memcpy(pv, pQuad, nSizePlaneV * sizeof(T)); + } else { + for (int i = 0; i < (nHeight + 1) / 2; i++) { + memcpy(pv + ((nPitch + 1) / 2) * i, pQuad + ((nWidth + 1) / 2) * i, + ((nWidth + 1) / 2) * sizeof(T)); + } + } + } + + private: + T* pQuad; + int nWidth, nHeight; +}; + +/** +* @brief Class for writing IVF format header for AV1 codec +*/ +class IVFUtils { + public: + void WriteFileHeader(std::vector& vPacket, + uint32_t nFourCC, + uint32_t nWidth, + uint32_t nHeight, + uint32_t nFrameRateNum, + uint32_t nFrameRateDen, + uint32_t nFrameCnt) { + char header[32]; + + header[0] = 'D'; + header[1] = 'K'; + header[2] = 'I'; + header[3] = 'F'; + mem_put_le16(header + 4, 0); // version + mem_put_le16(header + 6, 32); // header size + mem_put_le32(header + 8, nFourCC); // fourcc + mem_put_le16(header + 12, nWidth); // width + mem_put_le16(header + 14, nHeight); // height + mem_put_le32(header + 16, nFrameRateNum); // rate + mem_put_le32(header + 20, nFrameRateDen); // scale + mem_put_le32(header + 24, nFrameCnt); // length + mem_put_le32(header + 28, 0); // unused + + vPacket.insert(vPacket.end(), &header[0], &header[32]); + } + + void WriteFrameHeader(std::vector& vPacket, + size_t nFrameSize, + int64_t pts) { + char header[12]; + mem_put_le32(header, (int)nFrameSize); + mem_put_le32(header + 4, (int)(pts & 0xFFFFFFFF)); + mem_put_le32(header + 8, (int)(pts >> 32)); + + vPacket.insert(vPacket.end(), &header[0], &header[12]); + } + + private: + static inline void mem_put_le32(void* vmem, int val) { + unsigned char* mem = (unsigned char*)vmem; + mem[0] = (unsigned char)((val >> 0) & 0xff); + mem[1] = (unsigned char)((val >> 8) & 0xff); + mem[2] = (unsigned char)((val >> 16) & 0xff); + mem[3] = (unsigned char)((val >> 24) & 0xff); + } + + static inline void mem_put_le16(void* vmem, int val) { + unsigned char* mem = (unsigned char*)vmem; + mem[0] = (unsigned char)((val >> 0) & 0xff); + mem[1] = (unsigned char)((val >> 8) & 0xff); + } +}; + +/** +* @brief Utility class to measure elapsed time in seconds between the block of executed code +*/ +class StopWatch { + public: + void Start() { t0 = std::chrono::high_resolution_clock::now(); } + double Stop() { + return std::chrono::duration_cast( + std::chrono::high_resolution_clock::now().time_since_epoch() - + t0.time_since_epoch()) + .count() / + 1.0e9; + } + + private: + std::chrono::high_resolution_clock::time_point t0; +}; + +template +class ConcurrentQueue { + public: + ConcurrentQueue() {} + ConcurrentQueue(size_t size) : maxSize(size) {} + ConcurrentQueue(const ConcurrentQueue&) = delete; + ConcurrentQueue& operator=(const ConcurrentQueue&) = delete; + + void setSize(size_t s) { maxSize = s; } + + void push_back(const T& value) { + // Do not use a std::lock_guard here. We will need to explicitly + // unlock before notify_one as the other waiting thread will + // automatically try to acquire mutex once it wakes up + // (which will happen on notify_one) + std::unique_lock lock(m_mutex); + auto wasEmpty = m_List.empty(); + + while (full()) { + m_cond.wait(lock); + } + + m_List.push_back(value); + if (wasEmpty && !m_List.empty()) { + lock.unlock(); + m_cond.notify_one(); + } + } + + T pop_front() { + std::unique_lock lock(m_mutex); + + while (m_List.empty()) { + m_cond.wait(lock); + } + auto wasFull = full(); + T data = std::move(m_List.front()); + m_List.pop_front(); + + if (wasFull && !full()) { + lock.unlock(); + m_cond.notify_one(); + } + + return data; + } + + T front() { + std::unique_lock lock(m_mutex); + + while (m_List.empty()) { + m_cond.wait(lock); + } + + return m_List.front(); + } + + size_t size() { + std::unique_lock lock(m_mutex); + return m_List.size(); + } + + bool empty() { + std::unique_lock lock(m_mutex); + return m_List.empty(); + } + void clear() { + std::unique_lock lock(m_mutex); + m_List.clear(); + } + + private: + bool full() { + if (m_List.size() == maxSize) + return true; + return false; + } + + private: + std::list m_List; + std::mutex m_mutex; + std::condition_variable m_cond; + size_t maxSize; +}; + +inline void CheckInputFile(const char* szInFilePath) { + std::ifstream fpIn(szInFilePath, std::ios::in | std::ios::binary); + if (fpIn.fail()) { + std::ostringstream err; + err << "Unable to open input file: " << szInFilePath << std::endl; + throw std::invalid_argument(err.str()); + } +} + +inline void ValidateResolution(int nWidth, int nHeight) { + if (nWidth <= 0 || nHeight <= 0) { + std::ostringstream err; + err << "Please specify positive non zero resolution as -s WxH. Current " + "resolution is " + << nWidth << "x" << nHeight << std::endl; + throw std::invalid_argument(err.str()); + } +} + +template +void Nv12ToColor32(uint8_t* dpNv12, + int nNv12Pitch, + uint8_t* dpBgra, + int nBgraPitch, + int nWidth, + int nHeight, + int iMatrix = 0); +template +void Nv12ToColor64(uint8_t* dpNv12, + int nNv12Pitch, + uint8_t* dpBgra, + int nBgraPitch, + int nWidth, + int nHeight, + int iMatrix = 0); + +template +void P016ToColor32(uint8_t* dpP016, + int nP016Pitch, + uint8_t* dpBgra, + int nBgraPitch, + int nWidth, + int nHeight, + int iMatrix = 4); +template +void P016ToColor64(uint8_t* dpP016, + int nP016Pitch, + uint8_t* dpBgra, + int nBgraPitch, + int nWidth, + int nHeight, + int iMatrix = 4); + +template +void YUV444ToColor32(uint8_t* dpYUV444, + int nPitch, + uint8_t* dpBgra, + int nBgraPitch, + int nWidth, + int nHeight, + int iMatrix = 0); +template +void YUV444ToColor64(uint8_t* dpYUV444, + int nPitch, + uint8_t* dpBgra, + int nBgraPitch, + int nWidth, + int nHeight, + int iMatrix = 0); + +template +void YUV444P16ToColor32(uint8_t* dpYUV444, + int nPitch, + uint8_t* dpBgra, + int nBgraPitch, + int nWidth, + int nHeight, + int iMatrix = 4); +template +void YUV444P16ToColor64(uint8_t* dpYUV444, + int nPitch, + uint8_t* dpBgra, + int nBgraPitch, + int nWidth, + int nHeight, + int iMatrix = 4); + +template +void Nv12ToColorPlanar(uint8_t* dpNv12, + int nNv12Pitch, + uint8_t* dpBgrp, + int nBgrpPitch, + int nWidth, + int nHeight, + int iMatrix = 0); +template +void P016ToColorPlanar(uint8_t* dpP016, + int nP016Pitch, + uint8_t* dpBgrp, + int nBgrpPitch, + int nWidth, + int nHeight, + int iMatrix = 4); + +template +void YUV444ToColorPlanar(uint8_t* dpYUV444, + int nPitch, + uint8_t* dpBgrp, + int nBgrpPitch, + int nWidth, + int nHeight, + int iMatrix = 0); +template +void YUV444P16ToColorPlanar(uint8_t* dpYUV444, + int nPitch, + uint8_t* dpBgrp, + int nBgrpPitch, + int nWidth, + int nHeight, + int iMatrix = 4); + +void Bgra64ToP016(uint8_t* dpBgra, + int nBgraPitch, + uint8_t* dpP016, + int nP016Pitch, + int nWidth, + int nHeight, + int iMatrix = 4); + +void ConvertUInt8ToUInt16(uint8_t* dpUInt8, + uint16_t* dpUInt16, + int nSrcPitch, + int nDestPitch, + int nWidth, + int nHeight); +void ConvertUInt16ToUInt8(uint16_t* dpUInt16, + uint8_t* dpUInt8, + int nSrcPitch, + int nDestPitch, + int nWidth, + int nHeight); + +void ResizeNv12(unsigned char* dpDstNv12, + int nDstPitch, + int nDstWidth, + int nDstHeight, + unsigned char* dpSrcNv12, + int nSrcPitch, + int nSrcWidth, + int nSrcHeight, + unsigned char* dpDstNv12UV = nullptr); +void ResizeP016(unsigned char* dpDstP016, + int nDstPitch, + int nDstWidth, + int nDstHeight, + unsigned char* dpSrcP016, + int nSrcPitch, + int nSrcWidth, + int nSrcHeight, + unsigned char* dpDstP016UV = nullptr); + +void ScaleYUV420(unsigned char* dpDstY, + unsigned char* dpDstU, + unsigned char* dpDstV, + int nDstPitch, + int nDstChromaPitch, + int nDstWidth, + int nDstHeight, + unsigned char* dpSrcY, + unsigned char* dpSrcU, + unsigned char* dpSrcV, + int nSrcPitch, + int nSrcChromaPitch, + int nSrcWidth, + int nSrcHeight, + bool bSemiplanar); + +#ifdef __cuda_cuda_h__ +void ComputeCRC(uint8_t* pBuffer, + uint32_t* crcValue, + CUstream_st* outputCUStream); +#endif diff --git a/NvCodec/include/cuviddec.h b/NvCodec/include/cuviddec.h index 7b31dd00..8e2f13af 100644 --- a/NvCodec/include/cuviddec.h +++ b/NvCodec/include/cuviddec.h @@ -1,1005 +1,1318 @@ -/* - * This copyright notice applies to this header file only: - * - * Copyright (c) 2010-2020 NVIDIA Corporation - * - * Permission is hereby granted, free of charge, to any person - * obtaining a copy of this software and associated documentation - * files (the "Software"), to deal in the Software without - * restriction, including without limitation the rights to use, - * copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the software, and to permit persons to whom the - * software is furnished to do so, subject to the following - * conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES - * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT - * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, - * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - * OTHER DEALINGS IN THE SOFTWARE. - */ - -/*****************************************************************************************************/ -//! \file cuviddec.h -//! NVDECODE API provides video decoding interface to NVIDIA GPU devices. -//! \date 2015-2019 -//! This file contains constants, structure definitions and function prototypes used for decoding. -/*****************************************************************************************************/ - -#if !defined(__CUDA_VIDEO_H__) -#define __CUDA_VIDEO_H__ - -#ifndef __cuda_cuda_h__ -#include -#endif // __cuda_cuda_h__ - -#if defined(_WIN64) || defined(__LP64__) || defined(__x86_64) || defined(AMD64) || defined(_M_AMD64) -#if (CUDA_VERSION >= 3020) && (!defined(CUDA_FORCE_API_VERSION) || (CUDA_FORCE_API_VERSION >= 3020)) -#define __CUVID_DEVPTR64 -#endif -#endif - -#if defined(__cplusplus) -extern "C" { -#endif /* __cplusplus */ - -typedef void *CUvideodecoder; -typedef struct _CUcontextlock_st *CUvideoctxlock; - -/*********************************************************************************/ -//! \enum cudaVideoCodec -//! Video codec enums -//! These enums are used in CUVIDDECODECREATEINFO and CUVIDDECODECAPS structures -/*********************************************************************************/ -typedef enum cudaVideoCodec_enum { - cudaVideoCodec_MPEG1=0, /**< MPEG1 */ - cudaVideoCodec_MPEG2, /**< MPEG2 */ - cudaVideoCodec_MPEG4, /**< MPEG4 */ - cudaVideoCodec_VC1, /**< VC1 */ - cudaVideoCodec_H264, /**< H264 */ - cudaVideoCodec_JPEG, /**< JPEG */ - cudaVideoCodec_H264_SVC, /**< H264-SVC */ - cudaVideoCodec_H264_MVC, /**< H264-MVC */ - cudaVideoCodec_HEVC, /**< HEVC */ - cudaVideoCodec_VP8, /**< VP8 */ - cudaVideoCodec_VP9, /**< VP9 */ - cudaVideoCodec_NumCodecs, /**< Max codecs */ - // Uncompressed YUV - cudaVideoCodec_YUV420 = (('I'<<24)|('Y'<<16)|('U'<<8)|('V')), /**< Y,U,V (4:2:0) */ - cudaVideoCodec_YV12 = (('Y'<<24)|('V'<<16)|('1'<<8)|('2')), /**< Y,V,U (4:2:0) */ - cudaVideoCodec_NV12 = (('N'<<24)|('V'<<16)|('1'<<8)|('2')), /**< Y,UV (4:2:0) */ - cudaVideoCodec_YUYV = (('Y'<<24)|('U'<<16)|('Y'<<8)|('V')), /**< YUYV/YUY2 (4:2:2) */ - cudaVideoCodec_UYVY = (('U'<<24)|('Y'<<16)|('V'<<8)|('Y')) /**< UYVY (4:2:2) */ -} cudaVideoCodec; - -/*********************************************************************************/ -//! \enum cudaVideoSurfaceFormat -//! Video surface format enums used for output format of decoded output -//! These enums are used in CUVIDDECODECREATEINFO structure -/*********************************************************************************/ -typedef enum cudaVideoSurfaceFormat_enum { - cudaVideoSurfaceFormat_NV12=0, /**< Semi-Planar YUV [Y plane followed by interleaved UV plane] */ - cudaVideoSurfaceFormat_P016=1, /**< 16 bit Semi-Planar YUV [Y plane followed by interleaved UV plane]. - Can be used for 10 bit(6LSB bits 0), 12 bit (4LSB bits 0) */ - cudaVideoSurfaceFormat_YUV444=2, /**< Planar YUV [Y plane followed by U and V planes] */ - cudaVideoSurfaceFormat_YUV444_16Bit=3, /**< 16 bit Planar YUV [Y plane followed by U and V planes]. - Can be used for 10 bit(6LSB bits 0), 12 bit (4LSB bits 0) */ -} cudaVideoSurfaceFormat; - -/******************************************************************************************************************/ -//! \enum cudaVideoDeinterlaceMode -//! Deinterlacing mode enums -//! These enums are used in CUVIDDECODECREATEINFO structure -//! Use cudaVideoDeinterlaceMode_Weave for progressive content and for content that doesn't need deinterlacing -//! cudaVideoDeinterlaceMode_Adaptive needs more video memory than other DImodes -/******************************************************************************************************************/ -typedef enum cudaVideoDeinterlaceMode_enum { - cudaVideoDeinterlaceMode_Weave=0, /**< Weave both fields (no deinterlacing) */ - cudaVideoDeinterlaceMode_Bob, /**< Drop one field */ - cudaVideoDeinterlaceMode_Adaptive /**< Adaptive deinterlacing */ -} cudaVideoDeinterlaceMode; - -/**************************************************************************************************************/ -//! \enum cudaVideoChromaFormat -//! Chroma format enums -//! These enums are used in CUVIDDECODECREATEINFO and CUVIDDECODECAPS structures -/**************************************************************************************************************/ -typedef enum cudaVideoChromaFormat_enum { - cudaVideoChromaFormat_Monochrome=0, /**< MonoChrome */ - cudaVideoChromaFormat_420, /**< YUV 4:2:0 */ - cudaVideoChromaFormat_422, /**< YUV 4:2:2 */ - cudaVideoChromaFormat_444 /**< YUV 4:4:4 */ -} cudaVideoChromaFormat; - -/*************************************************************************************************************/ -//! \enum cudaVideoCreateFlags -//! Decoder flag enums to select preferred decode path -//! cudaVideoCreate_Default and cudaVideoCreate_PreferCUVID are most optimized, use these whenever possible -/*************************************************************************************************************/ -typedef enum cudaVideoCreateFlags_enum { - cudaVideoCreate_Default = 0x00, /**< Default operation mode: use dedicated video engines */ - cudaVideoCreate_PreferCUDA = 0x01, /**< Use CUDA-based decoder (requires valid vidLock object for multi-threading) */ - cudaVideoCreate_PreferDXVA = 0x02, /**< Go through DXVA internally if possible (requires D3D9 interop) */ - cudaVideoCreate_PreferCUVID = 0x04 /**< Use dedicated video engines directly */ -} cudaVideoCreateFlags; - - -/*************************************************************************/ -//! \enum cuvidDecodeStatus -//! Decode status enums -//! These enums are used in CUVIDGETDECODESTATUS structure -/*************************************************************************/ -typedef enum cuvidDecodeStatus_enum -{ - cuvidDecodeStatus_Invalid = 0, // Decode status is not valid - cuvidDecodeStatus_InProgress = 1, // Decode is in progress - cuvidDecodeStatus_Success = 2, // Decode is completed without any errors - // 3 to 7 enums are reserved for future use - cuvidDecodeStatus_Error = 8, // Decode is completed with an error (error is not concealed) - cuvidDecodeStatus_Error_Concealed = 9, // Decode is completed with an error and error is concealed -} cuvidDecodeStatus; - -/**************************************************************************************************************/ -//! \struct CUVIDDECODECAPS; -//! This structure is used in cuvidGetDecoderCaps API -/**************************************************************************************************************/ -typedef struct _CUVIDDECODECAPS -{ - cudaVideoCodec eCodecType; /**< IN: cudaVideoCodec_XXX */ - cudaVideoChromaFormat eChromaFormat; /**< IN: cudaVideoChromaFormat_XXX */ - unsigned int nBitDepthMinus8; /**< IN: The Value "BitDepth minus 8" */ - unsigned int reserved1[3]; /**< Reserved for future use - set to zero */ - - unsigned char bIsSupported; /**< OUT: 1 if codec supported, 0 if not supported */ - unsigned char reserved2; /**< Reserved for future use - set to zero */ - unsigned short nOutputFormatMask; /**< OUT: each bit represents corresponding cudaVideoSurfaceFormat enum */ - unsigned int nMaxWidth; /**< OUT: Max supported coded width in pixels */ - unsigned int nMaxHeight; /**< OUT: Max supported coded height in pixels */ - unsigned int nMaxMBCount; /**< OUT: Max supported macroblock count - CodedWidth*CodedHeight/256 must be <= nMaxMBCount */ - unsigned short nMinWidth; /**< OUT: Min supported coded width in pixels */ - unsigned short nMinHeight; /**< OUT: Min supported coded height in pixels */ - unsigned int reserved3[11]; /**< Reserved for future use - set to zero */ -} CUVIDDECODECAPS; - -/**************************************************************************************************************/ -//! \struct CUVIDDECODECREATEINFO -//! This structure is used in cuvidCreateDecoder API -/**************************************************************************************************************/ -typedef struct _CUVIDDECODECREATEINFO -{ - unsigned long ulWidth; /**< IN: Coded sequence width in pixels */ - unsigned long ulHeight; /**< IN: Coded sequence height in pixels */ - unsigned long ulNumDecodeSurfaces; /**< IN: Maximum number of internal decode surfaces */ - cudaVideoCodec CodecType; /**< IN: cudaVideoCodec_XXX */ - cudaVideoChromaFormat ChromaFormat; /**< IN: cudaVideoChromaFormat_XXX */ - unsigned long ulCreationFlags; /**< IN: Decoder creation flags (cudaVideoCreateFlags_XXX) */ - unsigned long bitDepthMinus8; /**< IN: The value "BitDepth minus 8" */ - unsigned long ulIntraDecodeOnly; /**< IN: Set 1 only if video has all intra frames (default value is 0). This will - optimize video memory for Intra frames only decoding. The support is limited - to specific codecs - H264, HEVC, VP9, the flag will be ignored for codecs which - are not supported. However decoding might fail if the flag is enabled in case - of supported codecs for regular bit streams having P and/or B frames. */ - unsigned long ulMaxWidth; /**< IN: Coded sequence max width in pixels used with reconfigure Decoder */ - unsigned long ulMaxHeight; /**< IN: Coded sequence max height in pixels used with reconfigure Decoder */ - unsigned long Reserved1; /**< Reserved for future use - set to zero */ - /** - * IN: area of the frame that should be displayed - */ - struct { - short left; - short top; - short right; - short bottom; - } display_area; - - cudaVideoSurfaceFormat OutputFormat; /**< IN: cudaVideoSurfaceFormat_XXX */ - cudaVideoDeinterlaceMode DeinterlaceMode; /**< IN: cudaVideoDeinterlaceMode_XXX */ - unsigned long ulTargetWidth; /**< IN: Post-processed output width (Should be aligned to 2) */ - unsigned long ulTargetHeight; /**< IN: Post-processed output height (Should be aligned to 2) */ - unsigned long ulNumOutputSurfaces; /**< IN: Maximum number of output surfaces simultaneously mapped */ - CUvideoctxlock vidLock; /**< IN: If non-NULL, context lock used for synchronizing ownership of - the cuda context. Needed for cudaVideoCreate_PreferCUDA decode */ - /** - * IN: target rectangle in the output frame (for aspect ratio conversion) - * if a null rectangle is specified, {0,0,ulTargetWidth,ulTargetHeight} will be used - */ - struct { - short left; - short top; - short right; - short bottom; - } target_rect; - unsigned long Reserved2[5]; /**< Reserved for future use - set to zero */ -} CUVIDDECODECREATEINFO; - -/*********************************************************/ -//! \struct CUVIDH264DPBENTRY -//! H.264 DPB entry -//! This structure is used in CUVIDH264PICPARAMS structure -/*********************************************************/ -typedef struct _CUVIDH264DPBENTRY -{ - int PicIdx; /**< picture index of reference frame */ - int FrameIdx; /**< frame_num(short-term) or LongTermFrameIdx(long-term) */ - int is_long_term; /**< 0=short term reference, 1=long term reference */ - int not_existing; /**< non-existing reference frame (corresponding PicIdx should be set to -1) */ - int used_for_reference; /**< 0=unused, 1=top_field, 2=bottom_field, 3=both_fields */ - int FieldOrderCnt[2]; /**< field order count of top and bottom fields */ -} CUVIDH264DPBENTRY; - -/************************************************************/ -//! \struct CUVIDH264MVCEXT -//! H.264 MVC picture parameters ext -//! This structure is used in CUVIDH264PICPARAMS structure -/************************************************************/ -typedef struct _CUVIDH264MVCEXT -{ - int num_views_minus1; /**< Max number of coded views minus 1 in video : Range - 0 to 1023 */ - int view_id; /**< view identifier */ - unsigned char inter_view_flag; /**< 1 if used for inter-view prediction, 0 if not */ - unsigned char num_inter_view_refs_l0; /**< number of inter-view ref pics in RefPicList0 */ - unsigned char num_inter_view_refs_l1; /**< number of inter-view ref pics in RefPicList1 */ - unsigned char MVCReserved8Bits; /**< Reserved bits */ - int InterViewRefsL0[16]; /**< view id of the i-th view component for inter-view prediction in RefPicList0 */ - int InterViewRefsL1[16]; /**< view id of the i-th view component for inter-view prediction in RefPicList1 */ -} CUVIDH264MVCEXT; - -/*********************************************************/ -//! \struct CUVIDH264SVCEXT -//! H.264 SVC picture parameters ext -//! This structure is used in CUVIDH264PICPARAMS structure -/*********************************************************/ -typedef struct _CUVIDH264SVCEXT -{ - unsigned char profile_idc; - unsigned char level_idc; - unsigned char DQId; - unsigned char DQIdMax; - unsigned char disable_inter_layer_deblocking_filter_idc; - unsigned char ref_layer_chroma_phase_y_plus1; - signed char inter_layer_slice_alpha_c0_offset_div2; - signed char inter_layer_slice_beta_offset_div2; - - unsigned short DPBEntryValidFlag; - unsigned char inter_layer_deblocking_filter_control_present_flag; - unsigned char extended_spatial_scalability_idc; - unsigned char adaptive_tcoeff_level_prediction_flag; - unsigned char slice_header_restriction_flag; - unsigned char chroma_phase_x_plus1_flag; - unsigned char chroma_phase_y_plus1; - - unsigned char tcoeff_level_prediction_flag; - unsigned char constrained_intra_resampling_flag; - unsigned char ref_layer_chroma_phase_x_plus1_flag; - unsigned char store_ref_base_pic_flag; - unsigned char Reserved8BitsA; - unsigned char Reserved8BitsB; - - short scaled_ref_layer_left_offset; - short scaled_ref_layer_top_offset; - short scaled_ref_layer_right_offset; - short scaled_ref_layer_bottom_offset; - unsigned short Reserved16Bits; - struct _CUVIDPICPARAMS *pNextLayer; /**< Points to the picparams for the next layer to be decoded. - Linked list ends at the target layer. */ - int bRefBaseLayer; /**< whether to store ref base pic */ -} CUVIDH264SVCEXT; - -/******************************************************/ -//! \struct CUVIDH264PICPARAMS -//! H.264 picture parameters -//! This structure is used in CUVIDPICPARAMS structure -/******************************************************/ -typedef struct _CUVIDH264PICPARAMS -{ - // SPS - int log2_max_frame_num_minus4; - int pic_order_cnt_type; - int log2_max_pic_order_cnt_lsb_minus4; - int delta_pic_order_always_zero_flag; - int frame_mbs_only_flag; - int direct_8x8_inference_flag; - int num_ref_frames; // NOTE: shall meet level 4.1 restrictions - unsigned char residual_colour_transform_flag; - unsigned char bit_depth_luma_minus8; // Must be 0 (only 8-bit supported) - unsigned char bit_depth_chroma_minus8; // Must be 0 (only 8-bit supported) - unsigned char qpprime_y_zero_transform_bypass_flag; - // PPS - int entropy_coding_mode_flag; - int pic_order_present_flag; - int num_ref_idx_l0_active_minus1; - int num_ref_idx_l1_active_minus1; - int weighted_pred_flag; - int weighted_bipred_idc; - int pic_init_qp_minus26; - int deblocking_filter_control_present_flag; - int redundant_pic_cnt_present_flag; - int transform_8x8_mode_flag; - int MbaffFrameFlag; - int constrained_intra_pred_flag; - int chroma_qp_index_offset; - int second_chroma_qp_index_offset; - int ref_pic_flag; - int frame_num; - int CurrFieldOrderCnt[2]; - // DPB - CUVIDH264DPBENTRY dpb[16]; // List of reference frames within the DPB - // Quantization Matrices (raster-order) - unsigned char WeightScale4x4[6][16]; - unsigned char WeightScale8x8[2][64]; - // FMO/ASO - unsigned char fmo_aso_enable; - unsigned char num_slice_groups_minus1; - unsigned char slice_group_map_type; - signed char pic_init_qs_minus26; - unsigned int slice_group_change_rate_minus1; - union - { - unsigned long long slice_group_map_addr; - const unsigned char *pMb2SliceGroupMap; - } fmo; - unsigned int Reserved[12]; - // SVC/MVC - union - { - CUVIDH264MVCEXT mvcext; - CUVIDH264SVCEXT svcext; - }; -} CUVIDH264PICPARAMS; - - -/********************************************************/ -//! \struct CUVIDMPEG2PICPARAMS -//! MPEG-2 picture parameters -//! This structure is used in CUVIDPICPARAMS structure -/********************************************************/ -typedef struct _CUVIDMPEG2PICPARAMS -{ - int ForwardRefIdx; // Picture index of forward reference (P/B-frames) - int BackwardRefIdx; // Picture index of backward reference (B-frames) - int picture_coding_type; - int full_pel_forward_vector; - int full_pel_backward_vector; - int f_code[2][2]; - int intra_dc_precision; - int frame_pred_frame_dct; - int concealment_motion_vectors; - int q_scale_type; - int intra_vlc_format; - int alternate_scan; - int top_field_first; - // Quantization matrices (raster order) - unsigned char QuantMatrixIntra[64]; - unsigned char QuantMatrixInter[64]; -} CUVIDMPEG2PICPARAMS; - -// MPEG-4 has VOP types instead of Picture types -#define I_VOP 0 -#define P_VOP 1 -#define B_VOP 2 -#define S_VOP 3 - -/*******************************************************/ -//! \struct CUVIDMPEG4PICPARAMS -//! MPEG-4 picture parameters -//! This structure is used in CUVIDPICPARAMS structure -/*******************************************************/ -typedef struct _CUVIDMPEG4PICPARAMS -{ - int ForwardRefIdx; // Picture index of forward reference (P/B-frames) - int BackwardRefIdx; // Picture index of backward reference (B-frames) - // VOL - int video_object_layer_width; - int video_object_layer_height; - int vop_time_increment_bitcount; - int top_field_first; - int resync_marker_disable; - int quant_type; - int quarter_sample; - int short_video_header; - int divx_flags; - // VOP - int vop_coding_type; - int vop_coded; - int vop_rounding_type; - int alternate_vertical_scan_flag; - int interlaced; - int vop_fcode_forward; - int vop_fcode_backward; - int trd[2]; - int trb[2]; - // Quantization matrices (raster order) - unsigned char QuantMatrixIntra[64]; - unsigned char QuantMatrixInter[64]; - int gmc_enabled; -} CUVIDMPEG4PICPARAMS; - -/********************************************************/ -//! \struct CUVIDVC1PICPARAMS -//! VC1 picture parameters -//! This structure is used in CUVIDPICPARAMS structure -/********************************************************/ -typedef struct _CUVIDVC1PICPARAMS -{ - int ForwardRefIdx; /**< Picture index of forward reference (P/B-frames) */ - int BackwardRefIdx; /**< Picture index of backward reference (B-frames) */ - int FrameWidth; /**< Actual frame width */ - int FrameHeight; /**< Actual frame height */ - // PICTURE - int intra_pic_flag; /**< Set to 1 for I,BI frames */ - int ref_pic_flag; /**< Set to 1 for I,P frames */ - int progressive_fcm; /**< Progressive frame */ - // SEQUENCE - int profile; - int postprocflag; - int pulldown; - int interlace; - int tfcntrflag; - int finterpflag; - int psf; - int multires; - int syncmarker; - int rangered; - int maxbframes; - // ENTRYPOINT - int panscan_flag; - int refdist_flag; - int extended_mv; - int dquant; - int vstransform; - int loopfilter; - int fastuvmc; - int overlap; - int quantizer; - int extended_dmv; - int range_mapy_flag; - int range_mapy; - int range_mapuv_flag; - int range_mapuv; - int rangeredfrm; // range reduction state -} CUVIDVC1PICPARAMS; - -/***********************************************************/ -//! \struct CUVIDJPEGPICPARAMS -//! JPEG picture parameters -//! This structure is used in CUVIDPICPARAMS structure -/***********************************************************/ -typedef struct _CUVIDJPEGPICPARAMS -{ - int Reserved; -} CUVIDJPEGPICPARAMS; - - -/*******************************************************/ -//! \struct CUVIDHEVCPICPARAMS -//! HEVC picture parameters -//! This structure is used in CUVIDPICPARAMS structure -/*******************************************************/ -typedef struct _CUVIDHEVCPICPARAMS -{ - // sps - int pic_width_in_luma_samples; - int pic_height_in_luma_samples; - unsigned char log2_min_luma_coding_block_size_minus3; - unsigned char log2_diff_max_min_luma_coding_block_size; - unsigned char log2_min_transform_block_size_minus2; - unsigned char log2_diff_max_min_transform_block_size; - unsigned char pcm_enabled_flag; - unsigned char log2_min_pcm_luma_coding_block_size_minus3; - unsigned char log2_diff_max_min_pcm_luma_coding_block_size; - unsigned char pcm_sample_bit_depth_luma_minus1; - - unsigned char pcm_sample_bit_depth_chroma_minus1; - unsigned char pcm_loop_filter_disabled_flag; - unsigned char strong_intra_smoothing_enabled_flag; - unsigned char max_transform_hierarchy_depth_intra; - unsigned char max_transform_hierarchy_depth_inter; - unsigned char amp_enabled_flag; - unsigned char separate_colour_plane_flag; - unsigned char log2_max_pic_order_cnt_lsb_minus4; - - unsigned char num_short_term_ref_pic_sets; - unsigned char long_term_ref_pics_present_flag; - unsigned char num_long_term_ref_pics_sps; - unsigned char sps_temporal_mvp_enabled_flag; - unsigned char sample_adaptive_offset_enabled_flag; - unsigned char scaling_list_enable_flag; - unsigned char IrapPicFlag; - unsigned char IdrPicFlag; - - unsigned char bit_depth_luma_minus8; - unsigned char bit_depth_chroma_minus8; - //sps/pps extension fields - unsigned char log2_max_transform_skip_block_size_minus2; - unsigned char log2_sao_offset_scale_luma; - unsigned char log2_sao_offset_scale_chroma; - unsigned char high_precision_offsets_enabled_flag; - unsigned char reserved1[10]; - - // pps - unsigned char dependent_slice_segments_enabled_flag; - unsigned char slice_segment_header_extension_present_flag; - unsigned char sign_data_hiding_enabled_flag; - unsigned char cu_qp_delta_enabled_flag; - unsigned char diff_cu_qp_delta_depth; - signed char init_qp_minus26; - signed char pps_cb_qp_offset; - signed char pps_cr_qp_offset; - - unsigned char constrained_intra_pred_flag; - unsigned char weighted_pred_flag; - unsigned char weighted_bipred_flag; - unsigned char transform_skip_enabled_flag; - unsigned char transquant_bypass_enabled_flag; - unsigned char entropy_coding_sync_enabled_flag; - unsigned char log2_parallel_merge_level_minus2; - unsigned char num_extra_slice_header_bits; - - unsigned char loop_filter_across_tiles_enabled_flag; - unsigned char loop_filter_across_slices_enabled_flag; - unsigned char output_flag_present_flag; - unsigned char num_ref_idx_l0_default_active_minus1; - unsigned char num_ref_idx_l1_default_active_minus1; - unsigned char lists_modification_present_flag; - unsigned char cabac_init_present_flag; - unsigned char pps_slice_chroma_qp_offsets_present_flag; - - unsigned char deblocking_filter_override_enabled_flag; - unsigned char pps_deblocking_filter_disabled_flag; - signed char pps_beta_offset_div2; - signed char pps_tc_offset_div2; - unsigned char tiles_enabled_flag; - unsigned char uniform_spacing_flag; - unsigned char num_tile_columns_minus1; - unsigned char num_tile_rows_minus1; - - unsigned short column_width_minus1[21]; - unsigned short row_height_minus1[21]; - - // sps and pps extension HEVC-main 444 - unsigned char sps_range_extension_flag; - unsigned char transform_skip_rotation_enabled_flag; - unsigned char transform_skip_context_enabled_flag; - unsigned char implicit_rdpcm_enabled_flag; - - unsigned char explicit_rdpcm_enabled_flag; - unsigned char extended_precision_processing_flag; - unsigned char intra_smoothing_disabled_flag; - unsigned char persistent_rice_adaptation_enabled_flag; - - unsigned char cabac_bypass_alignment_enabled_flag; - unsigned char pps_range_extension_flag; - unsigned char cross_component_prediction_enabled_flag; - unsigned char chroma_qp_offset_list_enabled_flag; - - unsigned char diff_cu_chroma_qp_offset_depth; - unsigned char chroma_qp_offset_list_len_minus1; - signed char cb_qp_offset_list[6]; - - signed char cr_qp_offset_list[6]; - unsigned char reserved2[2]; - - unsigned int reserved3[8]; - - // RefPicSets - int NumBitsForShortTermRPSInSlice; - int NumDeltaPocsOfRefRpsIdx; - int NumPocTotalCurr; - int NumPocStCurrBefore; - int NumPocStCurrAfter; - int NumPocLtCurr; - int CurrPicOrderCntVal; - int RefPicIdx[16]; // [refpic] Indices of valid reference pictures (-1 if unused for reference) - int PicOrderCntVal[16]; // [refpic] - unsigned char IsLongTerm[16]; // [refpic] 0=not a long-term reference, 1=long-term reference - unsigned char RefPicSetStCurrBefore[8]; // [0..NumPocStCurrBefore-1] -> refpic (0..15) - unsigned char RefPicSetStCurrAfter[8]; // [0..NumPocStCurrAfter-1] -> refpic (0..15) - unsigned char RefPicSetLtCurr[8]; // [0..NumPocLtCurr-1] -> refpic (0..15) - unsigned char RefPicSetInterLayer0[8]; - unsigned char RefPicSetInterLayer1[8]; - unsigned int reserved4[12]; - - // scaling lists (diag order) - unsigned char ScalingList4x4[6][16]; // [matrixId][i] - unsigned char ScalingList8x8[6][64]; // [matrixId][i] - unsigned char ScalingList16x16[6][64]; // [matrixId][i] - unsigned char ScalingList32x32[2][64]; // [matrixId][i] - unsigned char ScalingListDCCoeff16x16[6]; // [matrixId] - unsigned char ScalingListDCCoeff32x32[2]; // [matrixId] -} CUVIDHEVCPICPARAMS; - - -/***********************************************************/ -//! \struct CUVIDVP8PICPARAMS -//! VP8 picture parameters -//! This structure is used in CUVIDPICPARAMS structure -/***********************************************************/ -typedef struct _CUVIDVP8PICPARAMS -{ - int width; - int height; - unsigned int first_partition_size; - //Frame Indexes - unsigned char LastRefIdx; - unsigned char GoldenRefIdx; - unsigned char AltRefIdx; - union { - struct { - unsigned char frame_type : 1; /**< 0 = KEYFRAME, 1 = INTERFRAME */ - unsigned char version : 3; - unsigned char show_frame : 1; - unsigned char update_mb_segmentation_data : 1; /**< Must be 0 if segmentation is not enabled */ - unsigned char Reserved2Bits : 2; - }vp8_frame_tag; - unsigned char wFrameTagFlags; - }; - unsigned char Reserved1[4]; - unsigned int Reserved2[3]; -} CUVIDVP8PICPARAMS; - -/***********************************************************/ -//! \struct CUVIDVP9PICPARAMS -//! VP9 picture parameters -//! This structure is used in CUVIDPICPARAMS structure -/***********************************************************/ -typedef struct _CUVIDVP9PICPARAMS -{ - unsigned int width; - unsigned int height; - - //Frame Indices - unsigned char LastRefIdx; - unsigned char GoldenRefIdx; - unsigned char AltRefIdx; - unsigned char colorSpace; - - unsigned short profile : 3; - unsigned short frameContextIdx : 2; - unsigned short frameType : 1; - unsigned short showFrame : 1; - unsigned short errorResilient : 1; - unsigned short frameParallelDecoding : 1; - unsigned short subSamplingX : 1; - unsigned short subSamplingY : 1; - unsigned short intraOnly : 1; - unsigned short allow_high_precision_mv : 1; - unsigned short refreshEntropyProbs : 1; - unsigned short reserved2Bits : 2; - - unsigned short reserved16Bits; - - unsigned char refFrameSignBias[4]; - - unsigned char bitDepthMinus8Luma; - unsigned char bitDepthMinus8Chroma; - unsigned char loopFilterLevel; - unsigned char loopFilterSharpness; - - unsigned char modeRefLfEnabled; - unsigned char log2_tile_columns; - unsigned char log2_tile_rows; - - unsigned char segmentEnabled : 1; - unsigned char segmentMapUpdate : 1; - unsigned char segmentMapTemporalUpdate : 1; - unsigned char segmentFeatureMode : 1; - unsigned char reserved4Bits : 4; - - - unsigned char segmentFeatureEnable[8][4]; - short segmentFeatureData[8][4]; - unsigned char mb_segment_tree_probs[7]; - unsigned char segment_pred_probs[3]; - unsigned char reservedSegment16Bits[2]; - - int qpYAc; - int qpYDc; - int qpChDc; - int qpChAc; - - unsigned int activeRefIdx[3]; - unsigned int resetFrameContext; - unsigned int mcomp_filter_type; - unsigned int mbRefLfDelta[4]; - unsigned int mbModeLfDelta[2]; - unsigned int frameTagSize; - unsigned int offsetToDctParts; - unsigned int reserved128Bits[4]; - -} CUVIDVP9PICPARAMS; - - -/******************************************************************************************/ -//! \struct CUVIDPICPARAMS -//! Picture parameters for decoding -//! This structure is used in cuvidDecodePicture API -//! IN for cuvidDecodePicture -/******************************************************************************************/ -typedef struct _CUVIDPICPARAMS -{ - int PicWidthInMbs; /**< IN: Coded frame size in macroblocks */ - int FrameHeightInMbs; /**< IN: Coded frame height in macroblocks */ - int CurrPicIdx; /**< IN: Output index of the current picture */ - int field_pic_flag; /**< IN: 0=frame picture, 1=field picture */ - int bottom_field_flag; /**< IN: 0=top field, 1=bottom field (ignored if field_pic_flag=0) */ - int second_field; /**< IN: Second field of a complementary field pair */ - // Bitstream data - unsigned int nBitstreamDataLen; /**< IN: Number of bytes in bitstream data buffer */ - const unsigned char *pBitstreamData; /**< IN: Ptr to bitstream data for this picture (slice-layer) */ - unsigned int nNumSlices; /**< IN: Number of slices in this picture */ - const unsigned int *pSliceDataOffsets; /**< IN: nNumSlices entries, contains offset of each slice within - the bitstream data buffer */ - int ref_pic_flag; /**< IN: This picture is a reference picture */ - int intra_pic_flag; /**< IN: This picture is entirely intra coded */ - unsigned int Reserved[30]; /**< Reserved for future use */ - // IN: Codec-specific data - union { - CUVIDMPEG2PICPARAMS mpeg2; /**< Also used for MPEG-1 */ - CUVIDH264PICPARAMS h264; - CUVIDVC1PICPARAMS vc1; - CUVIDMPEG4PICPARAMS mpeg4; - CUVIDJPEGPICPARAMS jpeg; - CUVIDHEVCPICPARAMS hevc; - CUVIDVP8PICPARAMS vp8; - CUVIDVP9PICPARAMS vp9; - unsigned int CodecReserved[1024]; - } CodecSpecific; -} CUVIDPICPARAMS; - - -/******************************************************/ -//! \struct CUVIDPROCPARAMS -//! Picture parameters for postprocessing -//! This structure is used in cuvidMapVideoFrame API -/******************************************************/ -typedef struct _CUVIDPROCPARAMS -{ - int progressive_frame; /**< IN: Input is progressive (deinterlace_mode will be ignored) */ - int second_field; /**< IN: Output the second field (ignored if deinterlace mode is Weave) */ - int top_field_first; /**< IN: Input frame is top field first (1st field is top, 2nd field is bottom) */ - int unpaired_field; /**< IN: Input only contains one field (2nd field is invalid) */ - // The fields below are used for raw YUV input - unsigned int reserved_flags; /**< Reserved for future use (set to zero) */ - unsigned int reserved_zero; /**< Reserved (set to zero) */ - unsigned long long raw_input_dptr; /**< IN: Input CUdeviceptr for raw YUV extensions */ - unsigned int raw_input_pitch; /**< IN: pitch in bytes of raw YUV input (should be aligned appropriately) */ - unsigned int raw_input_format; /**< IN: Input YUV format (cudaVideoCodec_enum) */ - unsigned long long raw_output_dptr; /**< IN: Output CUdeviceptr for raw YUV extensions */ - unsigned int raw_output_pitch; /**< IN: pitch in bytes of raw YUV output (should be aligned appropriately) */ - unsigned int Reserved1; /**< Reserved for future use (set to zero) */ - CUstream output_stream; /**< IN: stream object used by cuvidMapVideoFrame */ - unsigned int Reserved[46]; /**< Reserved for future use (set to zero) */ - void *Reserved2[2]; /**< Reserved for future use (set to zero) */ -} CUVIDPROCPARAMS; - -/*********************************************************************************************************/ -//! \struct CUVIDGETDECODESTATUS -//! Struct for reporting decode status. -//! This structure is used in cuvidGetDecodeStatus API. -/*********************************************************************************************************/ -typedef struct _CUVIDGETDECODESTATUS -{ - cuvidDecodeStatus decodeStatus; - unsigned int reserved[31]; - void *pReserved[8]; -} CUVIDGETDECODESTATUS; - -/****************************************************/ -//! \struct CUVIDRECONFIGUREDECODERINFO -//! Struct for decoder reset -//! This structure is used in cuvidReconfigureDecoder() API -/****************************************************/ -typedef struct _CUVIDRECONFIGUREDECODERINFO -{ - unsigned int ulWidth; /**< IN: Coded sequence width in pixels, MUST be < = ulMaxWidth defined at CUVIDDECODECREATEINFO */ - unsigned int ulHeight; /**< IN: Coded sequence height in pixels, MUST be < = ulMaxHeight defined at CUVIDDECODECREATEINFO */ - unsigned int ulTargetWidth; /**< IN: Post processed output width */ - unsigned int ulTargetHeight; /**< IN: Post Processed output height */ - unsigned int ulNumDecodeSurfaces; /**< IN: Maximum number of internal decode surfaces */ - unsigned int reserved1[12]; /**< Reserved for future use. Set to Zero */ - /** - * IN: Area of frame to be displayed. Use-case : Source Cropping - */ - struct { - short left; - short top; - short right; - short bottom; - } display_area; - /** - * IN: Target Rectangle in the OutputFrame. Use-case : Aspect ratio Conversion - */ - struct { - short left; - short top; - short right; - short bottom; - } target_rect; - unsigned int reserved2[11]; /**< Reserved for future use. Set to Zero */ -} CUVIDRECONFIGUREDECODERINFO; - - -/***********************************************************************************************************/ -//! VIDEO_DECODER -//! -//! In order to minimize decode latencies, there should be always at least 2 pictures in the decode -//! queue at any time, in order to make sure that all decode engines are always busy. -//! -//! Overall data flow: -//! - cuvidGetDecoderCaps(...) -//! - cuvidCreateDecoder(...) -//! - For each picture: -//! + cuvidDecodePicture(N) -//! + cuvidMapVideoFrame(N-4) -//! + do some processing in cuda -//! + cuvidUnmapVideoFrame(N-4) -//! + cuvidDecodePicture(N+1) -//! + cuvidMapVideoFrame(N-3) -//! + ... -//! - cuvidDestroyDecoder(...) -//! -//! NOTE: -//! - When the cuda context is created from a D3D device, the D3D device must also be created -//! with the D3DCREATE_MULTITHREADED flag. -//! - There is a limit to how many pictures can be mapped simultaneously (ulNumOutputSurfaces) -//! - cuvidDecodePicture may block the calling thread if there are too many pictures pending -//! in the decode queue -/***********************************************************************************************************/ - - -/**********************************************************************************************************************/ -//! \fn CUresult CUDAAPI cuvidGetDecoderCaps(CUVIDDECODECAPS *pdc) -//! Queries decode capabilities of NVDEC-HW based on CodecType, ChromaFormat and BitDepthMinus8 parameters. -//! 1. Application fills IN parameters CodecType, ChromaFormat and BitDepthMinus8 of CUVIDDECODECAPS structure -//! 2. On calling cuvidGetDecoderCaps, driver fills OUT parameters if the IN parameters are supported -//! If IN parameters passed to the driver are not supported by NVDEC-HW, then all OUT params are set to 0. -//! E.g. on Geforce GTX 960: -//! App fills - eCodecType = cudaVideoCodec_H264; eChromaFormat = cudaVideoChromaFormat_420; nBitDepthMinus8 = 0; -//! Given IN parameters are supported, hence driver fills: bIsSupported = 1; nMinWidth = 48; nMinHeight = 16; -//! nMaxWidth = 4096; nMaxHeight = 4096; nMaxMBCount = 65536; -//! CodedWidth*CodedHeight/256 must be less than or equal to nMaxMBCount -/**********************************************************************************************************************/ -extern CUresult CUDAAPI cuvidGetDecoderCaps(CUVIDDECODECAPS *pdc); - -/*****************************************************************************************************/ -//! \fn CUresult CUDAAPI cuvidCreateDecoder(CUvideodecoder *phDecoder, CUVIDDECODECREATEINFO *pdci) -//! Create the decoder object based on pdci. A handle to the created decoder is returned -/*****************************************************************************************************/ -extern CUresult CUDAAPI cuvidCreateDecoder(CUvideodecoder *phDecoder, CUVIDDECODECREATEINFO *pdci); - -/*****************************************************************************************************/ -//! \fn CUresult CUDAAPI cuvidDestroyDecoder(CUvideodecoder hDecoder) -//! Destroy the decoder object -/*****************************************************************************************************/ -extern CUresult CUDAAPI cuvidDestroyDecoder(CUvideodecoder hDecoder); - -/*****************************************************************************************************/ -//! \fn CUresult CUDAAPI cuvidDecodePicture(CUvideodecoder hDecoder, CUVIDPICPARAMS *pPicParams) -//! Decode a single picture (field or frame) -//! Kicks off HW decoding -/*****************************************************************************************************/ -extern CUresult CUDAAPI cuvidDecodePicture(CUvideodecoder hDecoder, CUVIDPICPARAMS *pPicParams); - -/************************************************************************************************************/ -//! \fn CUresult CUDAAPI cuvidGetDecodeStatus(CUvideodecoder hDecoder, int nPicIdx); -//! Get the decode status for frame corresponding to nPicIdx -//! API is supported for Maxwell and above generation GPUs. -//! API is currently supported for HEVC, H264 and JPEG codecs. -//! API returns CUDA_ERROR_NOT_SUPPORTED error code for unsupported GPU or codec. -/************************************************************************************************************/ -extern CUresult CUDAAPI cuvidGetDecodeStatus(CUvideodecoder hDecoder, int nPicIdx, CUVIDGETDECODESTATUS* pDecodeStatus); - -/*********************************************************************************************************/ -//! \fn CUresult CUDAAPI cuvidReconfigureDecoder(CUvideodecoder hDecoder, CUVIDRECONFIGUREDECODERINFO *pDecReconfigParams) -//! Used to reuse single decoder for multiple clips. Currently supports resolution change, resize params, display area -//! params, target area params change for same codec. Must be called during CUVIDPARSERPARAMS::pfnSequenceCallback -/*********************************************************************************************************/ -extern CUresult CUDAAPI cuvidReconfigureDecoder(CUvideodecoder hDecoder, CUVIDRECONFIGUREDECODERINFO *pDecReconfigParams); - - -#if !defined(__CUVID_DEVPTR64) || defined(__CUVID_INTERNAL) -/************************************************************************************************************************/ -//! \fn CUresult CUDAAPI cuvidMapVideoFrame(CUvideodecoder hDecoder, int nPicIdx, unsigned int *pDevPtr, -//! unsigned int *pPitch, CUVIDPROCPARAMS *pVPP); -//! Post-process and map video frame corresponding to nPicIdx for use in cuda. Returns cuda device pointer and associated -//! pitch of the video frame -/************************************************************************************************************************/ -extern CUresult CUDAAPI cuvidMapVideoFrame(CUvideodecoder hDecoder, int nPicIdx, - unsigned int *pDevPtr, unsigned int *pPitch, - CUVIDPROCPARAMS *pVPP); - -/*****************************************************************************************************/ -//! \fn CUresult CUDAAPI cuvidUnmapVideoFrame(CUvideodecoder hDecoder, unsigned int DevPtr) -//! Unmap a previously mapped video frame -/*****************************************************************************************************/ -extern CUresult CUDAAPI cuvidUnmapVideoFrame(CUvideodecoder hDecoder, unsigned int DevPtr); -#endif - -#if defined(_WIN64) || defined(__LP64__) || defined(__x86_64) || defined(AMD64) || defined(_M_AMD64) -/****************************************************************************************************************************/ -//! \fn CUresult CUDAAPI cuvidMapVideoFrame64(CUvideodecoder hDecoder, int nPicIdx, unsigned long long *pDevPtr, -//! unsigned int * pPitch, CUVIDPROCPARAMS *pVPP); -//! Post-process and map video frame corresponding to nPicIdx for use in cuda. Returns cuda device pointer and associated -//! pitch of the video frame -/****************************************************************************************************************************/ -extern CUresult CUDAAPI cuvidMapVideoFrame64(CUvideodecoder hDecoder, int nPicIdx, unsigned long long *pDevPtr, - unsigned int *pPitch, CUVIDPROCPARAMS *pVPP); - -/**************************************************************************************************/ -//! \fn CUresult CUDAAPI cuvidUnmapVideoFrame64(CUvideodecoder hDecoder, unsigned long long DevPtr); -//! Unmap a previously mapped video frame -/**************************************************************************************************/ -extern CUresult CUDAAPI cuvidUnmapVideoFrame64(CUvideodecoder hDecoder, unsigned long long DevPtr); - -#if defined(__CUVID_DEVPTR64) && !defined(__CUVID_INTERNAL) -#define cuvidMapVideoFrame cuvidMapVideoFrame64 -#define cuvidUnmapVideoFrame cuvidUnmapVideoFrame64 -#endif -#endif - - -/********************************************************************************************************************/ -//! -//! Context-locking: to facilitate multi-threaded implementations, the following 4 functions -//! provide a simple mutex-style host synchronization. If a non-NULL context is specified -//! in CUVIDDECODECREATEINFO, the codec library will acquire the mutex associated with the given -//! context before making any cuda calls. -//! A multi-threaded application could create a lock associated with a context handle so that -//! multiple threads can safely share the same cuda context: -//! - use cuCtxPopCurrent immediately after context creation in order to create a 'floating' context -//! that can be passed to cuvidCtxLockCreate. -//! - When using a floating context, all cuda calls should only be made within a cuvidCtxLock/cuvidCtxUnlock section. -//! -//! NOTE: This is a safer alternative to cuCtxPushCurrent and cuCtxPopCurrent, and is not related to video -//! decoder in any way (implemented as a critical section associated with cuCtx{Push|Pop}Current calls). -/********************************************************************************************************************/ - -/********************************************************************************************************************/ -//! \fn CUresult CUDAAPI cuvidCtxLockCreate(CUvideoctxlock *pLock, CUcontext ctx) -//! This API is used to create CtxLock object -/********************************************************************************************************************/ -extern CUresult CUDAAPI cuvidCtxLockCreate(CUvideoctxlock *pLock, CUcontext ctx); - -/********************************************************************************************************************/ -//! \fn CUresult CUDAAPI cuvidCtxLockDestroy(CUvideoctxlock lck) -//! This API is used to free CtxLock object -/********************************************************************************************************************/ -extern CUresult CUDAAPI cuvidCtxLockDestroy(CUvideoctxlock lck); - -/********************************************************************************************************************/ -//! \fn CUresult CUDAAPI cuvidCtxLock(CUvideoctxlock lck, unsigned int reserved_flags) -//! This API is used to acquire ctxlock -/********************************************************************************************************************/ -extern CUresult CUDAAPI cuvidCtxLock(CUvideoctxlock lck, unsigned int reserved_flags); - -/********************************************************************************************************************/ -//! \fn CUresult CUDAAPI cuvidCtxUnlock(CUvideoctxlock lck, unsigned int reserved_flags) -//! This API is used to release ctxlock -/********************************************************************************************************************/ -extern CUresult CUDAAPI cuvidCtxUnlock(CUvideoctxlock lck, unsigned int reserved_flags); - -/**********************************************************************************************/ - - -#if defined(__cplusplus) -} -// Auto-lock helper for C++ applications -class CCtxAutoLock -{ -private: - CUvideoctxlock m_ctx; -public: - CCtxAutoLock(CUvideoctxlock ctx):m_ctx(ctx) { cuvidCtxLock(m_ctx,0); } - ~CCtxAutoLock() { cuvidCtxUnlock(m_ctx,0); } -}; -#endif /* __cplusplus */ - -#endif // __CUDA_VIDEO_H__ - +/* + * This copyright notice applies to this header file only: + * + * Copyright (c) 2010-2022 NVIDIA Corporation + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the software, and to permit persons to whom the + * software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +/*****************************************************************************************************/ +//! \file cuviddec.h +//! NVDECODE API provides video decoding interface to NVIDIA GPU devices. +//! This file contains constants, structure definitions and function prototypes used for decoding. +/*****************************************************************************************************/ + +#if !defined(__CUDA_VIDEO_H__) +#define __CUDA_VIDEO_H__ + +#ifndef __cuda_cuda_h__ +#include +#endif // __cuda_cuda_h__ + +#if defined(_WIN64) || defined(__LP64__) || defined(__x86_64) || \ + defined(AMD64) || defined(_M_AMD64) +#if (CUDA_VERSION >= 3020) && \ + (!defined(CUDA_FORCE_API_VERSION) || (CUDA_FORCE_API_VERSION >= 3020)) +#define __CUVID_DEVPTR64 +#endif +#endif + +#if defined(__cplusplus) +extern "C" { +#endif /* __cplusplus */ + +typedef void* CUvideodecoder; +typedef struct _CUcontextlock_st* CUvideoctxlock; + +/*********************************************************************************/ +//! \enum cudaVideoCodec +//! Video codec enums +//! These enums are used in CUVIDDECODECREATEINFO and CUVIDDECODECAPS structures +/*********************************************************************************/ +typedef enum cudaVideoCodec_enum { + cudaVideoCodec_MPEG1 = 0, /**< MPEG1 */ + cudaVideoCodec_MPEG2, /**< MPEG2 */ + cudaVideoCodec_MPEG4, /**< MPEG4 */ + cudaVideoCodec_VC1, /**< VC1 */ + cudaVideoCodec_H264, /**< H264 */ + cudaVideoCodec_JPEG, /**< JPEG */ + cudaVideoCodec_H264_SVC, /**< H264-SVC */ + cudaVideoCodec_H264_MVC, /**< H264-MVC */ + cudaVideoCodec_HEVC, /**< HEVC */ + cudaVideoCodec_VP8, /**< VP8 */ + cudaVideoCodec_VP9, /**< VP9 */ + cudaVideoCodec_AV1, /**< AV1 */ + cudaVideoCodec_NumCodecs, /**< Max codecs */ + // Uncompressed YUV + cudaVideoCodec_YUV420 = (('I' << 24) | ('Y' << 16) | ('U' << 8) | + ('V')), /**< Y,U,V (4:2:0) */ + cudaVideoCodec_YV12 = (('Y' << 24) | ('V' << 16) | ('1' << 8) | + ('2')), /**< Y,V,U (4:2:0) */ + cudaVideoCodec_NV12 = (('N' << 24) | ('V' << 16) | ('1' << 8) | + ('2')), /**< Y,UV (4:2:0) */ + cudaVideoCodec_YUYV = (('Y' << 24) | ('U' << 16) | ('Y' << 8) | + ('V')), /**< YUYV/YUY2 (4:2:2) */ + cudaVideoCodec_UYVY = (('U' << 24) | ('Y' << 16) | ('V' << 8) | + ('Y')) /**< UYVY (4:2:2) */ +} cudaVideoCodec; + +/*********************************************************************************/ +//! \enum cudaVideoSurfaceFormat +//! Video surface format enums used for output format of decoded output +//! These enums are used in CUVIDDECODECREATEINFO structure +/*********************************************************************************/ +typedef enum cudaVideoSurfaceFormat_enum { + cudaVideoSurfaceFormat_NV12 = + 0, /**< Semi-Planar YUV [Y plane followed by interleaved UV plane] */ + cudaVideoSurfaceFormat_P016 = + 1, /**< 16 bit Semi-Planar YUV [Y plane followed by interleaved UV plane]. + Can be used for 10 bit(6LSB bits 0), 12 bit (4LSB bits 0) */ + cudaVideoSurfaceFormat_YUV444 = + 2, /**< Planar YUV [Y plane followed by U and V planes] */ + cudaVideoSurfaceFormat_YUV444_16Bit = + 3, /**< 16 bit Planar YUV [Y plane followed by U and V planes]. + Can be used for 10 bit(6LSB bits 0), 12 bit (4LSB bits 0) */ +} cudaVideoSurfaceFormat; + +/******************************************************************************************************************/ +//! \enum cudaVideoDeinterlaceMode +//! Deinterlacing mode enums +//! These enums are used in CUVIDDECODECREATEINFO structure +//! Use cudaVideoDeinterlaceMode_Weave for progressive content and for content that doesn't need deinterlacing +//! cudaVideoDeinterlaceMode_Adaptive needs more video memory than other DImodes +/******************************************************************************************************************/ +typedef enum cudaVideoDeinterlaceMode_enum { + cudaVideoDeinterlaceMode_Weave = + 0, /**< Weave both fields (no deinterlacing) */ + cudaVideoDeinterlaceMode_Bob, /**< Drop one field */ + cudaVideoDeinterlaceMode_Adaptive /**< Adaptive deinterlacing */ +} cudaVideoDeinterlaceMode; + +/**************************************************************************************************************/ +//! \enum cudaVideoChromaFormat +//! Chroma format enums +//! These enums are used in CUVIDDECODECREATEINFO and CUVIDDECODECAPS structures +/**************************************************************************************************************/ +typedef enum cudaVideoChromaFormat_enum { + cudaVideoChromaFormat_Monochrome = 0, /**< MonoChrome */ + cudaVideoChromaFormat_420, /**< YUV 4:2:0 */ + cudaVideoChromaFormat_422, /**< YUV 4:2:2 */ + cudaVideoChromaFormat_444 /**< YUV 4:4:4 */ +} cudaVideoChromaFormat; + +/*************************************************************************************************************/ +//! \enum cudaVideoCreateFlags +//! Decoder flag enums to select preferred decode path +//! cudaVideoCreate_Default and cudaVideoCreate_PreferCUVID are most optimized, use these whenever possible +/*************************************************************************************************************/ +typedef enum cudaVideoCreateFlags_enum { + cudaVideoCreate_Default = + 0x00, /**< Default operation mode: use dedicated video engines */ + cudaVideoCreate_PreferCUDA = + 0x01, /**< Use CUDA-based decoder (requires valid vidLock object for multi-threading) */ + cudaVideoCreate_PreferDXVA = + 0x02, /**< Go through DXVA internally if possible (requires D3D9 interop) */ + cudaVideoCreate_PreferCUVID = + 0x04 /**< Use dedicated video engines directly */ +} cudaVideoCreateFlags; + +/*************************************************************************/ +//! \enum cuvidDecodeStatus +//! Decode status enums +//! These enums are used in CUVIDGETDECODESTATUS structure +/*************************************************************************/ +typedef enum cuvidDecodeStatus_enum { + cuvidDecodeStatus_Invalid = 0, // Decode status is not valid + cuvidDecodeStatus_InProgress = 1, // Decode is in progress + cuvidDecodeStatus_Success = 2, // Decode is completed without any errors + // 3 to 7 enums are reserved for future use + cuvidDecodeStatus_Error = + 8, // Decode is completed with an error (error is not concealed) + cuvidDecodeStatus_Error_Concealed = + 9, // Decode is completed with an error and error is concealed +} cuvidDecodeStatus; + +/**************************************************************************************************************/ +//! \struct CUVIDDECODECAPS; +//! This structure is used in cuvidGetDecoderCaps API +/**************************************************************************************************************/ +typedef struct _CUVIDDECODECAPS { + cudaVideoCodec + eCodecType; /**< IN: cudaVideoCodec_XXX */ + cudaVideoChromaFormat + eChromaFormat; /**< IN: cudaVideoChromaFormat_XXX */ + unsigned int + nBitDepthMinus8; /**< IN: The Value "BitDepth minus 8" */ + unsigned int reserved1 + [3]; /**< Reserved for future use - set to zero */ + + unsigned char + bIsSupported; /**< OUT: 1 if codec supported, 0 if not supported */ + unsigned char + nNumNVDECs; /**< OUT: Number of NVDECs that can support IN params */ + unsigned short + nOutputFormatMask; /**< OUT: each bit represents corresponding cudaVideoSurfaceFormat enum */ + unsigned int + nMaxWidth; /**< OUT: Max supported coded width in pixels */ + unsigned int + nMaxHeight; /**< OUT: Max supported coded height in pixels */ + unsigned int nMaxMBCount; /**< OUT: Max supported macroblock count + CodedWidth*CodedHeight/256 must be <= nMaxMBCount */ + unsigned short + nMinWidth; /**< OUT: Min supported coded width in pixels */ + unsigned short + nMinHeight; /**< OUT: Min supported coded height in pixels */ + unsigned char + bIsHistogramSupported; /**< OUT: 1 if Y component histogram output is supported, 0 if not + Note: histogram is computed on original picture data before + any post-processing like scaling, cropping, etc. is applied */ + unsigned char + nCounterBitDepth; /**< OUT: histogram counter bit depth */ + unsigned short + nMaxHistogramBins; /**< OUT: Max number of histogram bins */ + unsigned int reserved3 + [10]; /**< Reserved for future use - set to zero */ +} CUVIDDECODECAPS; + +/**************************************************************************************************************/ +//! \struct CUVIDDECODECREATEINFO +//! This structure is used in cuvidCreateDecoder API +/**************************************************************************************************************/ +typedef struct _CUVIDDECODECREATEINFO { + unsigned long + ulWidth; /**< IN: Coded sequence width in pixels */ + unsigned long + ulHeight; /**< IN: Coded sequence height in pixels */ + unsigned long + ulNumDecodeSurfaces; /**< IN: Maximum number of internal decode surfaces */ + cudaVideoCodec + CodecType; /**< IN: cudaVideoCodec_XXX */ + cudaVideoChromaFormat + ChromaFormat; /**< IN: cudaVideoChromaFormat_XXX */ + unsigned long + ulCreationFlags; /**< IN: Decoder creation flags (cudaVideoCreateFlags_XXX) */ + unsigned long + bitDepthMinus8; /**< IN: The value "BitDepth minus 8" */ + unsigned long + ulIntraDecodeOnly; /**< IN: Set 1 only if video has all intra frames (default value is 0). This will + optimize video memory for Intra frames only decoding. The support is limited + to specific codecs - H264, HEVC, VP9, the flag will be ignored for codecs which + are not supported. However decoding might fail if the flag is enabled in case + of supported codecs for regular bit streams having P and/or B frames. */ + unsigned long + ulMaxWidth; /**< IN: Coded sequence max width in pixels used with reconfigure Decoder */ + unsigned long + ulMaxHeight; /**< IN: Coded sequence max height in pixels used with reconfigure Decoder */ + unsigned long + Reserved1; /**< Reserved for future use - set to zero */ + /** + * IN: area of the frame that should be displayed + */ + struct { + short left; + short top; + short right; + short bottom; + } display_area; + + cudaVideoSurfaceFormat + OutputFormat; /**< IN: cudaVideoSurfaceFormat_XXX */ + cudaVideoDeinterlaceMode + DeinterlaceMode; /**< IN: cudaVideoDeinterlaceMode_XXX */ + unsigned long + ulTargetWidth; /**< IN: Post-processed output width (Should be aligned to 2) */ + unsigned long + ulTargetHeight; /**< IN: Post-processed output height (Should be aligned to 2) */ + unsigned long + ulNumOutputSurfaces; /**< IN: Maximum number of output surfaces simultaneously mapped */ + CUvideoctxlock + vidLock; /**< IN: If non-NULL, context lock used for synchronizing ownership of + the cuda context. Needed for cudaVideoCreate_PreferCUDA decode */ + /** + * IN: target rectangle in the output frame (for aspect ratio conversion) + * if a null rectangle is specified, {0,0,ulTargetWidth,ulTargetHeight} will be used + */ + struct { + short left; + short top; + short right; + short bottom; + } target_rect; + + unsigned long + enableHistogram; /**< IN: enable histogram output, if supported */ + unsigned long Reserved2[4]; /**< Reserved for future use - set to zero */ +} CUVIDDECODECREATEINFO; + +/*********************************************************/ +//! \struct CUVIDH264DPBENTRY +//! H.264 DPB entry +//! This structure is used in CUVIDH264PICPARAMS structure +/*********************************************************/ +typedef struct _CUVIDH264DPBENTRY { + int PicIdx; /**< picture index of reference frame */ + int FrameIdx; /**< frame_num(short-term) or LongTermFrameIdx(long-term) */ + int is_long_term; /**< 0=short term reference, 1=long term reference */ + int not_existing; /**< non-existing reference frame (corresponding PicIdx should be set to -1) */ + int used_for_reference; /**< 0=unused, 1=top_field, 2=bottom_field, 3=both_fields */ + int FieldOrderCnt + [2]; /**< field order count of top and bottom fields */ +} CUVIDH264DPBENTRY; + +/************************************************************/ +//! \struct CUVIDH264MVCEXT +//! H.264 MVC picture parameters ext +//! This structure is used in CUVIDH264PICPARAMS structure +/************************************************************/ +typedef struct _CUVIDH264MVCEXT { + int num_views_minus1; /**< Max number of coded views minus 1 in video : Range - 0 to 1023 */ + int view_id; /**< view identifier */ + unsigned char + inter_view_flag; /**< 1 if used for inter-view prediction, 0 if not */ + unsigned char + num_inter_view_refs_l0; /**< number of inter-view ref pics in RefPicList0 */ + unsigned char + num_inter_view_refs_l1; /**< number of inter-view ref pics in RefPicList1 */ + unsigned char + MVCReserved8Bits; /**< Reserved bits */ + int InterViewRefsL0 + [16]; /**< view id of the i-th view component for inter-view prediction in RefPicList0 */ + int InterViewRefsL1 + [16]; /**< view id of the i-th view component for inter-view prediction in RefPicList1 */ +} CUVIDH264MVCEXT; + +/*********************************************************/ +//! \struct CUVIDH264SVCEXT +//! H.264 SVC picture parameters ext +//! This structure is used in CUVIDH264PICPARAMS structure +/*********************************************************/ +typedef struct _CUVIDH264SVCEXT { + unsigned char profile_idc; + unsigned char level_idc; + unsigned char DQId; + unsigned char DQIdMax; + unsigned char disable_inter_layer_deblocking_filter_idc; + unsigned char ref_layer_chroma_phase_y_plus1; + signed char inter_layer_slice_alpha_c0_offset_div2; + signed char inter_layer_slice_beta_offset_div2; + + unsigned short DPBEntryValidFlag; + unsigned char inter_layer_deblocking_filter_control_present_flag; + unsigned char extended_spatial_scalability_idc; + unsigned char adaptive_tcoeff_level_prediction_flag; + unsigned char slice_header_restriction_flag; + unsigned char chroma_phase_x_plus1_flag; + unsigned char chroma_phase_y_plus1; + + unsigned char tcoeff_level_prediction_flag; + unsigned char constrained_intra_resampling_flag; + unsigned char ref_layer_chroma_phase_x_plus1_flag; + unsigned char store_ref_base_pic_flag; + unsigned char Reserved8BitsA; + unsigned char Reserved8BitsB; + + short scaled_ref_layer_left_offset; + short scaled_ref_layer_top_offset; + short scaled_ref_layer_right_offset; + short scaled_ref_layer_bottom_offset; + unsigned short Reserved16Bits; + struct _CUVIDPICPARAMS* + pNextLayer; /**< Points to the picparams for the next layer to be decoded. + Linked list ends at the target layer. */ + int bRefBaseLayer; /**< whether to store ref base pic */ +} CUVIDH264SVCEXT; + +/******************************************************/ +//! \struct CUVIDH264PICPARAMS +//! H.264 picture parameters +//! This structure is used in CUVIDPICPARAMS structure +/******************************************************/ +typedef struct _CUVIDH264PICPARAMS { + // SPS + int log2_max_frame_num_minus4; + int pic_order_cnt_type; + int log2_max_pic_order_cnt_lsb_minus4; + int delta_pic_order_always_zero_flag; + int frame_mbs_only_flag; + int direct_8x8_inference_flag; + int num_ref_frames; // NOTE: shall meet level 4.1 restrictions + unsigned char residual_colour_transform_flag; + unsigned char bit_depth_luma_minus8; // Must be 0 (only 8-bit supported) + unsigned char bit_depth_chroma_minus8; // Must be 0 (only 8-bit supported) + unsigned char qpprime_y_zero_transform_bypass_flag; + // PPS + int entropy_coding_mode_flag; + int pic_order_present_flag; + int num_ref_idx_l0_active_minus1; + int num_ref_idx_l1_active_minus1; + int weighted_pred_flag; + int weighted_bipred_idc; + int pic_init_qp_minus26; + int deblocking_filter_control_present_flag; + int redundant_pic_cnt_present_flag; + int transform_8x8_mode_flag; + int MbaffFrameFlag; + int constrained_intra_pred_flag; + int chroma_qp_index_offset; + int second_chroma_qp_index_offset; + int ref_pic_flag; + int frame_num; + int CurrFieldOrderCnt[2]; + // DPB + CUVIDH264DPBENTRY dpb[16]; // List of reference frames within the DPB + // Quantization Matrices (raster-order) + unsigned char WeightScale4x4[6][16]; + unsigned char WeightScale8x8[2][64]; + // FMO/ASO + unsigned char fmo_aso_enable; + unsigned char num_slice_groups_minus1; + unsigned char slice_group_map_type; + signed char pic_init_qs_minus26; + unsigned int slice_group_change_rate_minus1; + union { + unsigned long long slice_group_map_addr; + const unsigned char* pMb2SliceGroupMap; + } fmo; + unsigned int Reserved[12]; + // SVC/MVC + union { + CUVIDH264MVCEXT mvcext; + CUVIDH264SVCEXT svcext; + }; +} CUVIDH264PICPARAMS; + +/********************************************************/ +//! \struct CUVIDMPEG2PICPARAMS +//! MPEG-2 picture parameters +//! This structure is used in CUVIDPICPARAMS structure +/********************************************************/ +typedef struct _CUVIDMPEG2PICPARAMS { + int ForwardRefIdx; // Picture index of forward reference (P/B-frames) + int BackwardRefIdx; // Picture index of backward reference (B-frames) + int picture_coding_type; + int full_pel_forward_vector; + int full_pel_backward_vector; + int f_code[2][2]; + int intra_dc_precision; + int frame_pred_frame_dct; + int concealment_motion_vectors; + int q_scale_type; + int intra_vlc_format; + int alternate_scan; + int top_field_first; + // Quantization matrices (raster order) + unsigned char QuantMatrixIntra[64]; + unsigned char QuantMatrixInter[64]; +} CUVIDMPEG2PICPARAMS; + +// MPEG-4 has VOP types instead of Picture types +#define I_VOP 0 +#define P_VOP 1 +#define B_VOP 2 +#define S_VOP 3 + +/*******************************************************/ +//! \struct CUVIDMPEG4PICPARAMS +//! MPEG-4 picture parameters +//! This structure is used in CUVIDPICPARAMS structure +/*******************************************************/ +typedef struct _CUVIDMPEG4PICPARAMS { + int ForwardRefIdx; // Picture index of forward reference (P/B-frames) + int BackwardRefIdx; // Picture index of backward reference (B-frames) + // VOL + int video_object_layer_width; + int video_object_layer_height; + int vop_time_increment_bitcount; + int top_field_first; + int resync_marker_disable; + int quant_type; + int quarter_sample; + int short_video_header; + int divx_flags; + // VOP + int vop_coding_type; + int vop_coded; + int vop_rounding_type; + int alternate_vertical_scan_flag; + int interlaced; + int vop_fcode_forward; + int vop_fcode_backward; + int trd[2]; + int trb[2]; + // Quantization matrices (raster order) + unsigned char QuantMatrixIntra[64]; + unsigned char QuantMatrixInter[64]; + int gmc_enabled; +} CUVIDMPEG4PICPARAMS; + +/********************************************************/ +//! \struct CUVIDVC1PICPARAMS +//! VC1 picture parameters +//! This structure is used in CUVIDPICPARAMS structure +/********************************************************/ +typedef struct _CUVIDVC1PICPARAMS { + int ForwardRefIdx; /**< Picture index of forward reference (P/B-frames) */ + int BackwardRefIdx; /**< Picture index of backward reference (B-frames) */ + int FrameWidth; /**< Actual frame width */ + int FrameHeight; /**< Actual frame height */ + // PICTURE + int intra_pic_flag; /**< Set to 1 for I,BI frames */ + int ref_pic_flag; /**< Set to 1 for I,P frames */ + int progressive_fcm; /**< Progressive frame */ + // SEQUENCE + int profile; + int postprocflag; + int pulldown; + int interlace; + int tfcntrflag; + int finterpflag; + int psf; + int multires; + int syncmarker; + int rangered; + int maxbframes; + // ENTRYPOINT + int panscan_flag; + int refdist_flag; + int extended_mv; + int dquant; + int vstransform; + int loopfilter; + int fastuvmc; + int overlap; + int quantizer; + int extended_dmv; + int range_mapy_flag; + int range_mapy; + int range_mapuv_flag; + int range_mapuv; + int rangeredfrm; // range reduction state +} CUVIDVC1PICPARAMS; + +/***********************************************************/ +//! \struct CUVIDJPEGPICPARAMS +//! JPEG picture parameters +//! This structure is used in CUVIDPICPARAMS structure +/***********************************************************/ +typedef struct _CUVIDJPEGPICPARAMS { + int Reserved; +} CUVIDJPEGPICPARAMS; + +/*******************************************************/ +//! \struct CUVIDHEVCPICPARAMS +//! HEVC picture parameters +//! This structure is used in CUVIDPICPARAMS structure +/*******************************************************/ +typedef struct _CUVIDHEVCPICPARAMS { + // sps + int pic_width_in_luma_samples; + int pic_height_in_luma_samples; + unsigned char log2_min_luma_coding_block_size_minus3; + unsigned char log2_diff_max_min_luma_coding_block_size; + unsigned char log2_min_transform_block_size_minus2; + unsigned char log2_diff_max_min_transform_block_size; + unsigned char pcm_enabled_flag; + unsigned char log2_min_pcm_luma_coding_block_size_minus3; + unsigned char log2_diff_max_min_pcm_luma_coding_block_size; + unsigned char pcm_sample_bit_depth_luma_minus1; + + unsigned char pcm_sample_bit_depth_chroma_minus1; + unsigned char pcm_loop_filter_disabled_flag; + unsigned char strong_intra_smoothing_enabled_flag; + unsigned char max_transform_hierarchy_depth_intra; + unsigned char max_transform_hierarchy_depth_inter; + unsigned char amp_enabled_flag; + unsigned char separate_colour_plane_flag; + unsigned char log2_max_pic_order_cnt_lsb_minus4; + + unsigned char num_short_term_ref_pic_sets; + unsigned char long_term_ref_pics_present_flag; + unsigned char num_long_term_ref_pics_sps; + unsigned char sps_temporal_mvp_enabled_flag; + unsigned char sample_adaptive_offset_enabled_flag; + unsigned char scaling_list_enable_flag; + unsigned char IrapPicFlag; + unsigned char IdrPicFlag; + + unsigned char bit_depth_luma_minus8; + unsigned char bit_depth_chroma_minus8; + //sps/pps extension fields + unsigned char log2_max_transform_skip_block_size_minus2; + unsigned char log2_sao_offset_scale_luma; + unsigned char log2_sao_offset_scale_chroma; + unsigned char high_precision_offsets_enabled_flag; + unsigned char reserved1[10]; + + // pps + unsigned char dependent_slice_segments_enabled_flag; + unsigned char slice_segment_header_extension_present_flag; + unsigned char sign_data_hiding_enabled_flag; + unsigned char cu_qp_delta_enabled_flag; + unsigned char diff_cu_qp_delta_depth; + signed char init_qp_minus26; + signed char pps_cb_qp_offset; + signed char pps_cr_qp_offset; + + unsigned char constrained_intra_pred_flag; + unsigned char weighted_pred_flag; + unsigned char weighted_bipred_flag; + unsigned char transform_skip_enabled_flag; + unsigned char transquant_bypass_enabled_flag; + unsigned char entropy_coding_sync_enabled_flag; + unsigned char log2_parallel_merge_level_minus2; + unsigned char num_extra_slice_header_bits; + + unsigned char loop_filter_across_tiles_enabled_flag; + unsigned char loop_filter_across_slices_enabled_flag; + unsigned char output_flag_present_flag; + unsigned char num_ref_idx_l0_default_active_minus1; + unsigned char num_ref_idx_l1_default_active_minus1; + unsigned char lists_modification_present_flag; + unsigned char cabac_init_present_flag; + unsigned char pps_slice_chroma_qp_offsets_present_flag; + + unsigned char deblocking_filter_override_enabled_flag; + unsigned char pps_deblocking_filter_disabled_flag; + signed char pps_beta_offset_div2; + signed char pps_tc_offset_div2; + unsigned char tiles_enabled_flag; + unsigned char uniform_spacing_flag; + unsigned char num_tile_columns_minus1; + unsigned char num_tile_rows_minus1; + + unsigned short column_width_minus1[21]; + unsigned short row_height_minus1[21]; + + // sps and pps extension HEVC-main 444 + unsigned char sps_range_extension_flag; + unsigned char transform_skip_rotation_enabled_flag; + unsigned char transform_skip_context_enabled_flag; + unsigned char implicit_rdpcm_enabled_flag; + + unsigned char explicit_rdpcm_enabled_flag; + unsigned char extended_precision_processing_flag; + unsigned char intra_smoothing_disabled_flag; + unsigned char persistent_rice_adaptation_enabled_flag; + + unsigned char cabac_bypass_alignment_enabled_flag; + unsigned char pps_range_extension_flag; + unsigned char cross_component_prediction_enabled_flag; + unsigned char chroma_qp_offset_list_enabled_flag; + + unsigned char diff_cu_chroma_qp_offset_depth; + unsigned char chroma_qp_offset_list_len_minus1; + signed char cb_qp_offset_list[6]; + + signed char cr_qp_offset_list[6]; + unsigned char reserved2[2]; + + unsigned int reserved3[8]; + + // RefPicSets + int NumBitsForShortTermRPSInSlice; + int NumDeltaPocsOfRefRpsIdx; + int NumPocTotalCurr; + int NumPocStCurrBefore; + int NumPocStCurrAfter; + int NumPocLtCurr; + int CurrPicOrderCntVal; + int RefPicIdx + [16]; // [refpic] Indices of valid reference pictures (-1 if unused for reference) + int PicOrderCntVal[16]; // [refpic] + unsigned char IsLongTerm + [16]; // [refpic] 0=not a long-term reference, 1=long-term reference + unsigned char + RefPicSetStCurrBefore[8]; // [0..NumPocStCurrBefore-1] -> refpic (0..15) + unsigned char + RefPicSetStCurrAfter[8]; // [0..NumPocStCurrAfter-1] -> refpic (0..15) + unsigned char RefPicSetLtCurr[8]; // [0..NumPocLtCurr-1] -> refpic (0..15) + unsigned char RefPicSetInterLayer0[8]; + unsigned char RefPicSetInterLayer1[8]; + unsigned int reserved4[12]; + + // scaling lists (diag order) + unsigned char ScalingList4x4[6][16]; // [matrixId][i] + unsigned char ScalingList8x8[6][64]; // [matrixId][i] + unsigned char ScalingList16x16[6][64]; // [matrixId][i] + unsigned char ScalingList32x32[2][64]; // [matrixId][i] + unsigned char ScalingListDCCoeff16x16[6]; // [matrixId] + unsigned char ScalingListDCCoeff32x32[2]; // [matrixId] +} CUVIDHEVCPICPARAMS; + +/***********************************************************/ +//! \struct CUVIDVP8PICPARAMS +//! VP8 picture parameters +//! This structure is used in CUVIDPICPARAMS structure +/***********************************************************/ +typedef struct _CUVIDVP8PICPARAMS { + int width; + int height; + unsigned int first_partition_size; + //Frame Indexes + unsigned char LastRefIdx; + unsigned char GoldenRefIdx; + unsigned char AltRefIdx; + union { + struct { + unsigned char frame_type : 1; /**< 0 = KEYFRAME, 1 = INTERFRAME */ + unsigned char version : 3; + unsigned char show_frame : 1; + unsigned char + update_mb_segmentation_data : 1; /**< Must be 0 if segmentation is not enabled */ + unsigned char Reserved2Bits : 2; + } vp8_frame_tag; + unsigned char wFrameTagFlags; + }; + unsigned char Reserved1[4]; + unsigned int Reserved2[3]; +} CUVIDVP8PICPARAMS; + +/***********************************************************/ +//! \struct CUVIDVP9PICPARAMS +//! VP9 picture parameters +//! This structure is used in CUVIDPICPARAMS structure +/***********************************************************/ +typedef struct _CUVIDVP9PICPARAMS { + unsigned int width; + unsigned int height; + + //Frame Indices + unsigned char LastRefIdx; + unsigned char GoldenRefIdx; + unsigned char AltRefIdx; + unsigned char colorSpace; + + unsigned short profile : 3; + unsigned short frameContextIdx : 2; + unsigned short frameType : 1; + unsigned short showFrame : 1; + unsigned short errorResilient : 1; + unsigned short frameParallelDecoding : 1; + unsigned short subSamplingX : 1; + unsigned short subSamplingY : 1; + unsigned short intraOnly : 1; + unsigned short allow_high_precision_mv : 1; + unsigned short refreshEntropyProbs : 1; + unsigned short reserved2Bits : 2; + + unsigned short reserved16Bits; + + unsigned char refFrameSignBias[4]; + + unsigned char bitDepthMinus8Luma; + unsigned char bitDepthMinus8Chroma; + unsigned char loopFilterLevel; + unsigned char loopFilterSharpness; + + unsigned char modeRefLfEnabled; + unsigned char log2_tile_columns; + unsigned char log2_tile_rows; + + unsigned char segmentEnabled : 1; + unsigned char segmentMapUpdate : 1; + unsigned char segmentMapTemporalUpdate : 1; + unsigned char segmentFeatureMode : 1; + unsigned char reserved4Bits : 4; + + unsigned char segmentFeatureEnable[8][4]; + short segmentFeatureData[8][4]; + unsigned char mb_segment_tree_probs[7]; + unsigned char segment_pred_probs[3]; + unsigned char reservedSegment16Bits[2]; + + int qpYAc; + int qpYDc; + int qpChDc; + int qpChAc; + + unsigned int activeRefIdx[3]; + unsigned int resetFrameContext; + unsigned int mcomp_filter_type; + unsigned int mbRefLfDelta[4]; + unsigned int mbModeLfDelta[2]; + unsigned int frameTagSize; + unsigned int offsetToDctParts; + unsigned int reserved128Bits[4]; + +} CUVIDVP9PICPARAMS; + +/***********************************************************/ +//! \struct CUVIDAV1PICPARAMS +//! AV1 picture parameters +//! This structure is used in CUVIDPICPARAMS structure +/***********************************************************/ +typedef struct _CUVIDAV1PICPARAMS { + unsigned int + width; // coded width, if superres enabled then it is upscaled width + unsigned int height; // coded height + unsigned int frame_offset; // defined as order_hint in AV1 specification + int decodePicIdx; // decoded output pic index, if film grain enabled, it will keep decoded (without film grain) output + // It can be used as reference frame for future frames + + // sequence header + unsigned int profile : 3; // 0 = profile0, 1 = profile1, 2 = profile2 + unsigned int + use_128x128_superblock : 1; // superblock size 0:64x64, 1: 128x128 + unsigned int + subsampling_x : 1; // (subsampling_x, _y) 1,1 = 420, 1,0 = 422, 0,0 = 444 + unsigned int subsampling_y : 1; + unsigned int + mono_chrome : 1; // for monochrome content, mono_chrome = 1 and (subsampling_x, _y) should be 1,1 + unsigned int bit_depth_minus8 : 4; // bit depth minus 8 + unsigned int + enable_filter_intra : 1; // tool enable in seq level, 0 : disable 1: frame header control + unsigned int + enable_intra_edge_filter : 1; // intra edge filtering process, 0 : disable 1: enabled + unsigned int + enable_interintra_compound : 1; // interintra, 0 : not present 1: present + unsigned int + enable_masked_compound : 1; // 1: mode info for inter blocks may contain the syntax element compound_type. + // 0: syntax element compound_type will not be present + unsigned int + enable_dual_filter : 1; // vertical and horiz filter selection, 1: enable and 0: disable + unsigned int + enable_order_hint : 1; // order hint, and related tools, 1: enable and 0: disable + unsigned int order_hint_bits_minus1 : 3; // is used to compute OrderHintBits + unsigned int + enable_jnt_comp : 1; // joint compound modes, 1: enable and 0: disable + unsigned int + enable_superres : 1; // superres in seq level, 0 : disable 1: frame level control + unsigned int + enable_cdef : 1; // cdef filtering in seq level, 0 : disable 1: frame level control + unsigned int + enable_restoration : 1; // loop restoration filtering in seq level, 0 : disable 1: frame level control + unsigned int + enable_fgs : 1; // defined as film_grain_params_present in AV1 specification + unsigned int reserved0_7bits : 7; // reserved bits; must be set to 0 + + // frame header + unsigned int + frame_type : 2; // 0:Key frame, 1:Inter frame, 2:intra only, 3:s-frame + unsigned int + show_frame : 1; // show_frame = 1 implies that frame should be immediately output once decoded + unsigned int + disable_cdf_update : 1; // CDF update during symbol decoding, 1: disabled, 0: enabled + unsigned int + allow_screen_content_tools : 1; // 1: intra blocks may use palette encoding, 0: palette encoding is never used + unsigned int + force_integer_mv : 1; // 1: motion vectors will always be integers, 0: can contain fractional bits + unsigned int + coded_denom : 3; // coded_denom of the superres scale as specified in AV1 specification + unsigned int + allow_intrabc : 1; // 1: intra block copy may be used, 0: intra block copy is not allowed + unsigned int allow_high_precision_mv : 1; // 1/8 precision mv enable + unsigned int + interp_filter : 3; // interpolation filter. Refer to section 6.8.9 of the AV1 specification Version 1.0.0 with Errata 1 + unsigned int + switchable_motion_mode : 1; // defined as is_motion_mode_switchable in AV1 specification + unsigned int + use_ref_frame_mvs : 1; // 1: current frame can use the previous frame mv information, 0: will not use. + unsigned int + disable_frame_end_update_cdf : 1; // 1: indicates that the end of frame CDF update is disabled + unsigned int + delta_q_present : 1; // quantizer index delta values are present in the block level + unsigned int + delta_q_res : 2; // left shift which should be applied to decoded quantizer index delta values + unsigned int + using_qmatrix : 1; // 1: quantizer matrix will be used to compute quantizers + unsigned int coded_lossless : 1; // 1: all segments use lossless coding + unsigned int use_superres : 1; // 1: superres enabled for frame + unsigned int tx_mode : 2; // 0: ONLY4x4,1:LARGEST,2:SELECT + unsigned int reference_mode : 1; // 0: SINGLE, 1: SELECT + unsigned int + allow_warped_motion : 1; // 1: allow_warped_motion may be present, 0: allow_warped_motion will not be present + unsigned int + reduced_tx_set : 1; // 1: frame is restricted to subset of the full set of transform types, 0: no such restriction + unsigned int + skip_mode : 1; // 1: most of the mode info is skipped, 0: mode info is not skipped + unsigned int reserved1_3bits : 3; // reserved bits; must be set to 0 + + // tiling info + unsigned int + num_tile_cols : 8; // number of tiles across the frame., max is 64 + unsigned int num_tile_rows : 8; // number of tiles down the frame., max is 64 + unsigned int + context_update_tile_id : 16; // specifies which tile to use for the CDF update + unsigned short tile_widths[64]; // Width of each column in superblocks + unsigned short tile_heights[64]; // height of each row in superblocks + + // CDEF - refer to section 6.10.14 of the AV1 specification Version 1.0.0 with Errata 1 + unsigned char + cdef_damping_minus_3 : 2; // controls the amount of damping in the deringing filter + unsigned char + cdef_bits : 2; // the number of bits needed to specify which CDEF filter to apply + unsigned char reserved2_4bits : 4; // reserved bits; must be set to 0 + unsigned char + cdef_y_strength[8]; // 0-3 bits: y_pri_strength, 4-7 bits y_sec_strength + unsigned char cdef_uv_strength + [8]; // 0-3 bits: uv_pri_strength, 4-7 bits uv_sec_strength + + // SkipModeFrames + unsigned char + SkipModeFrame0 : 4; // specifies the frames to use for compound prediction when skip_mode is equal to 1. + unsigned char SkipModeFrame1 : 4; + + // qp information - refer to section 6.8.11 of the AV1 specification Version 1.0.0 with Errata 1 + unsigned char + base_qindex; // indicates the base frame qindex. Defined as base_q_idx in AV1 specification + char + qp_y_dc_delta_q; // indicates the Y DC quantizer relative to base_q_idx. Defined as DeltaQYDc in AV1 specification + char + qp_u_dc_delta_q; // indicates the U DC quantizer relative to base_q_idx. Defined as DeltaQUDc in AV1 specification + char + qp_v_dc_delta_q; // indicates the V DC quantizer relative to base_q_idx. Defined as DeltaQVDc in AV1 specification + char + qp_u_ac_delta_q; // indicates the U AC quantizer relative to base_q_idx. Defined as DeltaQUAc in AV1 specification + char + qp_v_ac_delta_q; // indicates the V AC quantizer relative to base_q_idx. Defined as DeltaQVAc in AV1 specification + unsigned char + qm_y; // specifies the level in the quantizer matrix that should be used for luma plane decoding + unsigned char + qm_u; // specifies the level in the quantizer matrix that should be used for chroma U plane decoding + unsigned char + qm_v; // specifies the level in the quantizer matrix that should be used for chroma V plane decoding + + // segmentation - refer to section 6.8.13 of the AV1 specification Version 1.0.0 with Errata 1 + unsigned char + segmentation_enabled : 1; // 1 indicates that this frame makes use of the segmentation tool + unsigned char + segmentation_update_map : 1; // 1 indicates that the segmentation map are updated during the decoding of this frame + unsigned char + segmentation_update_data : 1; // 1 indicates that new parameters are about to be specified for each segment + unsigned char + segmentation_temporal_update : 1; // 1 indicates that the updates to the segmentation map are coded relative to the existing segmentation map + unsigned char reserved3_4bits : 4; // reserved bits; must be set to 0 + short segmentation_feature_data + [8][8]; // specifies the feature data for a segment feature + unsigned char segmentation_feature_mask + [8]; // indicates that the corresponding feature is unused or feature value is coded + + // loopfilter - refer to section 6.8.10 of the AV1 specification Version 1.0.0 with Errata 1 + unsigned char loop_filter_level[2]; // contains loop filter strength values + unsigned char loop_filter_level_u; // loop filter strength value of U plane + unsigned char loop_filter_level_v; // loop filter strength value of V plane + unsigned char loop_filter_sharpness; // indicates the sharpness level + char loop_filter_ref_deltas + [8]; // contains the adjustment needed for the filter level based on the chosen reference frame + char loop_filter_mode_deltas + [2]; // contains the adjustment needed for the filter level based on the chosen mode + unsigned char + loop_filter_delta_enabled : 1; // indicates that the filter level depends on the mode and reference frame used to predict a block + unsigned char + loop_filter_delta_update : 1; // indicates that additional syntax elements are present that specify which mode and + // reference frame deltas are to be updated + unsigned char + delta_lf_present : 1; // specifies whether loop filter delta values are present in the block level + unsigned char + delta_lf_res : 2; // specifies the left shift to apply to the decoded loop filter values + unsigned char + delta_lf_multi : 1; // separate loop filter deltas for Hy,Vy,U,V edges + unsigned char reserved4_2bits : 2; // reserved bits; must be set to 0 + + // restoration - refer to section 6.10.15 of the AV1 specification Version 1.0.0 with Errata 1 + unsigned char lr_unit_size + [3]; // specifies the size of loop restoration units: 0: 32, 1: 64, 2: 128, 3: 256 + unsigned char lr_type[3]; // used to compute FrameRestorationType + + // reference frames + unsigned char + primary_ref_frame; // specifies which reference frame contains the CDF values and other state that should be + // loaded at the start of the frame + unsigned char ref_frame_map + [8]; // frames in dpb that can be used as reference for current or future frames + + unsigned char temporal_layer_id : 4; // temporal layer id + unsigned char spatial_layer_id : 4; // spatial layer id + + unsigned char reserved5_32bits[4]; // reserved bits; must be set to 0 + + // ref frame list + struct { + unsigned int width; + unsigned int height; + unsigned char index; + unsigned char reserved24Bits[3]; // reserved bits; must be set to 0 + } ref_frame[7]; // frames used as reference frame for current frame. + + // global motion + struct { + unsigned char invalid : 1; + unsigned char wmtype : 2; // defined as GmType in AV1 specification + unsigned char reserved5Bits : 5; // reserved bits; must be set to 0 + char reserved24Bits[3]; // reserved bits; must be set to 0 + int wmmat[6]; // defined as gm_params[] in AV1 specification + } global_motion[7]; // global motion params for reference frames + + // film grain params - refer to section 6.8.20 of the AV1 specification Version 1.0.0 with Errata 1 + unsigned short apply_grain : 1; + unsigned short overlap_flag : 1; + unsigned short scaling_shift_minus8 : 2; + unsigned short chroma_scaling_from_luma : 1; + unsigned short ar_coeff_lag : 2; + unsigned short ar_coeff_shift_minus6 : 2; + unsigned short grain_scale_shift : 2; + unsigned short clip_to_restricted_range : 1; + unsigned short reserved6_4bits : 4; // reserved bits; must be set to 0 + unsigned char num_y_points; + unsigned char scaling_points_y[14][2]; + unsigned char num_cb_points; + unsigned char scaling_points_cb[10][2]; + unsigned char num_cr_points; + unsigned char scaling_points_cr[10][2]; + unsigned char reserved7_8bits; // reserved bits; must be set to 0 + unsigned short random_seed; + short ar_coeffs_y[24]; + short ar_coeffs_cb[25]; + short ar_coeffs_cr[25]; + unsigned char cb_mult; + unsigned char cb_luma_mult; + short cb_offset; + unsigned char cr_mult; + unsigned char cr_luma_mult; + short cr_offset; + + int reserved[7]; // reserved bits; must be set to 0 +} CUVIDAV1PICPARAMS; + +/******************************************************************************************/ +//! \struct CUVIDPICPARAMS +//! Picture parameters for decoding +//! This structure is used in cuvidDecodePicture API +//! IN for cuvidDecodePicture +/******************************************************************************************/ +typedef struct _CUVIDPICPARAMS { + int PicWidthInMbs; /**< IN: Coded frame size in macroblocks */ + int FrameHeightInMbs; /**< IN: Coded frame height in macroblocks */ + int CurrPicIdx; /**< IN: Output index of the current picture */ + int field_pic_flag; /**< IN: 0=frame picture, 1=field picture */ + int bottom_field_flag; /**< IN: 0=top field, 1=bottom field (ignored if field_pic_flag=0) */ + int second_field; /**< IN: Second field of a complementary field pair */ + // Bitstream data + unsigned int + nBitstreamDataLen; /**< IN: Number of bytes in bitstream data buffer */ + const unsigned char* + pBitstreamData; /**< IN: Ptr to bitstream data for this picture (slice-layer) */ + unsigned int + nNumSlices; /**< IN: Number of slices in this picture */ + const unsigned int* + pSliceDataOffsets; /**< IN: nNumSlices entries, contains offset of each slice within + the bitstream data buffer */ + int ref_pic_flag; /**< IN: This picture is a reference picture */ + int intra_pic_flag; /**< IN: This picture is entirely intra coded */ + unsigned int Reserved + [30]; /**< Reserved for future use */ + // IN: Codec-specific data + union { + CUVIDMPEG2PICPARAMS mpeg2; /**< Also used for MPEG-1 */ + CUVIDH264PICPARAMS h264; + CUVIDVC1PICPARAMS vc1; + CUVIDMPEG4PICPARAMS mpeg4; + CUVIDJPEGPICPARAMS jpeg; + CUVIDHEVCPICPARAMS hevc; + CUVIDVP8PICPARAMS vp8; + CUVIDVP9PICPARAMS vp9; + CUVIDAV1PICPARAMS av1; + unsigned int CodecReserved[1024]; + } CodecSpecific; +} CUVIDPICPARAMS; + +/******************************************************/ +//! \struct CUVIDPROCPARAMS +//! Picture parameters for postprocessing +//! This structure is used in cuvidMapVideoFrame API +/******************************************************/ +typedef struct _CUVIDPROCPARAMS { + int progressive_frame; /**< IN: Input is progressive (deinterlace_mode will be ignored) */ + int second_field; /**< IN: Output the second field (ignored if deinterlace mode is Weave) */ + int top_field_first; /**< IN: Input frame is top field first (1st field is top, 2nd field is bottom) */ + int unpaired_field; /**< IN: Input only contains one field (2nd field is invalid) */ + // The fields below are used for raw YUV input + unsigned int + reserved_flags; /**< Reserved for future use (set to zero) */ + unsigned int + reserved_zero; /**< Reserved (set to zero) */ + unsigned long long + raw_input_dptr; /**< IN: Input CUdeviceptr for raw YUV extensions */ + unsigned int + raw_input_pitch; /**< IN: pitch in bytes of raw YUV input (should be aligned appropriately) */ + unsigned int + raw_input_format; /**< IN: Input YUV format (cudaVideoCodec_enum) */ + unsigned long long + raw_output_dptr; /**< IN: Output CUdeviceptr for raw YUV extensions */ + unsigned int + raw_output_pitch; /**< IN: pitch in bytes of raw YUV output (should be aligned appropriately) */ + unsigned int + Reserved1; /**< Reserved for future use (set to zero) */ + CUstream + output_stream; /**< IN: stream object used by cuvidMapVideoFrame */ + unsigned int Reserved + [46]; /**< Reserved for future use (set to zero) */ + unsigned long long* + histogram_dptr; /**< OUT: Output CUdeviceptr for histogram extensions */ + void* Reserved2 + [1]; /**< Reserved for future use (set to zero) */ +} CUVIDPROCPARAMS; + +/*********************************************************************************************************/ +//! \struct CUVIDGETDECODESTATUS +//! Struct for reporting decode status. +//! This structure is used in cuvidGetDecodeStatus API. +/*********************************************************************************************************/ +typedef struct _CUVIDGETDECODESTATUS { + cuvidDecodeStatus decodeStatus; + unsigned int reserved[31]; + void* pReserved[8]; +} CUVIDGETDECODESTATUS; + +/****************************************************/ +//! \struct CUVIDRECONFIGUREDECODERINFO +//! Struct for decoder reset +//! This structure is used in cuvidReconfigureDecoder() API +/****************************************************/ +typedef struct _CUVIDRECONFIGUREDECODERINFO { + unsigned int + ulWidth; /**< IN: Coded sequence width in pixels, MUST be < = ulMaxWidth defined at CUVIDDECODECREATEINFO */ + unsigned int + ulHeight; /**< IN: Coded sequence height in pixels, MUST be < = ulMaxHeight defined at CUVIDDECODECREATEINFO */ + unsigned int ulTargetWidth; /**< IN: Post processed output width */ + unsigned int ulTargetHeight; /**< IN: Post Processed output height */ + unsigned int + ulNumDecodeSurfaces; /**< IN: Maximum number of internal decode surfaces */ + unsigned int reserved1[12]; /**< Reserved for future use. Set to Zero */ + /** + * IN: Area of frame to be displayed. Use-case : Source Cropping + */ + struct { + short left; + short top; + short right; + short bottom; + } display_area; + /** + * IN: Target Rectangle in the OutputFrame. Use-case : Aspect ratio Conversion + */ + struct { + short left; + short top; + short right; + short bottom; + } target_rect; + unsigned int reserved2[11]; /**< Reserved for future use. Set to Zero */ +} CUVIDRECONFIGUREDECODERINFO; + +/***********************************************************************************************************/ +//! VIDEO_DECODER +//! +//! In order to minimize decode latencies, there should be always at least 2 pictures in the decode +//! queue at any time, in order to make sure that all decode engines are always busy. +//! +//! Overall data flow: +//! - cuvidGetDecoderCaps(...) +//! - cuvidCreateDecoder(...) +//! - For each picture: +//! + cuvidDecodePicture(N) +//! + cuvidMapVideoFrame(N-4) +//! + do some processing in cuda +//! + cuvidUnmapVideoFrame(N-4) +//! + cuvidDecodePicture(N+1) +//! + cuvidMapVideoFrame(N-3) +//! + ... +//! - cuvidDestroyDecoder(...) +//! +//! NOTE: +//! - When the cuda context is created from a D3D device, the D3D device must also be created +//! with the D3DCREATE_MULTITHREADED flag. +//! - There is a limit to how many pictures can be mapped simultaneously (ulNumOutputSurfaces) +//! - cuvidDecodePicture may block the calling thread if there are too many pictures pending +//! in the decode queue +/***********************************************************************************************************/ + +/**********************************************************************************************************************/ +//! \fn CUresult CUDAAPI cuvidGetDecoderCaps(CUVIDDECODECAPS *pdc) +//! Queries decode capabilities of NVDEC-HW based on CodecType, ChromaFormat and BitDepthMinus8 parameters. +//! 1. Application fills IN parameters CodecType, ChromaFormat and BitDepthMinus8 of CUVIDDECODECAPS structure +//! 2. On calling cuvidGetDecoderCaps, driver fills OUT parameters if the IN parameters are supported +//! If IN parameters passed to the driver are not supported by NVDEC-HW, then all OUT params are set to 0. +//! E.g. on Geforce GTX 960: +//! App fills - eCodecType = cudaVideoCodec_H264; eChromaFormat = cudaVideoChromaFormat_420; nBitDepthMinus8 = 0; +//! Given IN parameters are supported, hence driver fills: bIsSupported = 1; nMinWidth = 48; nMinHeight = 16; +//! nMaxWidth = 4096; nMaxHeight = 4096; nMaxMBCount = 65536; +//! CodedWidth*CodedHeight/256 must be less than or equal to nMaxMBCount +/**********************************************************************************************************************/ +extern CUresult CUDAAPI cuvidGetDecoderCaps(CUVIDDECODECAPS* pdc); + +/*****************************************************************************************************/ +//! \fn CUresult CUDAAPI cuvidCreateDecoder(CUvideodecoder *phDecoder, CUVIDDECODECREATEINFO *pdci) +//! Create the decoder object based on pdci. A handle to the created decoder is returned +/*****************************************************************************************************/ +extern CUresult CUDAAPI cuvidCreateDecoder(CUvideodecoder* phDecoder, + CUVIDDECODECREATEINFO* pdci); + +/*****************************************************************************************************/ +//! \fn CUresult CUDAAPI cuvidDestroyDecoder(CUvideodecoder hDecoder) +//! Destroy the decoder object +/*****************************************************************************************************/ +extern CUresult CUDAAPI cuvidDestroyDecoder(CUvideodecoder hDecoder); + +/*****************************************************************************************************/ +//! \fn CUresult CUDAAPI cuvidDecodePicture(CUvideodecoder hDecoder, CUVIDPICPARAMS *pPicParams) +//! Decode a single picture (field or frame) +//! Kicks off HW decoding +/*****************************************************************************************************/ +extern CUresult CUDAAPI cuvidDecodePicture(CUvideodecoder hDecoder, + CUVIDPICPARAMS* pPicParams); + +/************************************************************************************************************/ +//! \fn CUresult CUDAAPI cuvidGetDecodeStatus(CUvideodecoder hDecoder, int nPicIdx); +//! Get the decode status for frame corresponding to nPicIdx +//! API is supported for Maxwell and above generation GPUs. +//! API is currently supported for HEVC, H264 and JPEG codecs. +//! API returns CUDA_ERROR_NOT_SUPPORTED error code for unsupported GPU or codec. +/************************************************************************************************************/ +extern CUresult CUDAAPI +cuvidGetDecodeStatus(CUvideodecoder hDecoder, + int nPicIdx, + CUVIDGETDECODESTATUS* pDecodeStatus); + +/*********************************************************************************************************/ +//! \fn CUresult CUDAAPI cuvidReconfigureDecoder(CUvideodecoder hDecoder, CUVIDRECONFIGUREDECODERINFO *pDecReconfigParams) +//! Used to reuse single decoder for multiple clips. Currently supports resolution change, resize params, display area +//! params, target area params change for same codec. Must be called during CUVIDPARSERPARAMS::pfnSequenceCallback +/*********************************************************************************************************/ +extern CUresult CUDAAPI +cuvidReconfigureDecoder(CUvideodecoder hDecoder, + CUVIDRECONFIGUREDECODERINFO* pDecReconfigParams); + +#if !defined(__CUVID_DEVPTR64) || defined(__CUVID_INTERNAL) +/************************************************************************************************************************/ +//! \fn CUresult CUDAAPI cuvidMapVideoFrame(CUvideodecoder hDecoder, int nPicIdx, unsigned int *pDevPtr, +//! unsigned int *pPitch, CUVIDPROCPARAMS *pVPP); +//! Post-process and map video frame corresponding to nPicIdx for use in cuda. Returns cuda device pointer and associated +//! pitch of the video frame +/************************************************************************************************************************/ +extern CUresult CUDAAPI cuvidMapVideoFrame(CUvideodecoder hDecoder, + int nPicIdx, + unsigned int* pDevPtr, + unsigned int* pPitch, + CUVIDPROCPARAMS* pVPP); + +/*****************************************************************************************************/ +//! \fn CUresult CUDAAPI cuvidUnmapVideoFrame(CUvideodecoder hDecoder, unsigned int DevPtr) +//! Unmap a previously mapped video frame +/*****************************************************************************************************/ +extern CUresult CUDAAPI cuvidUnmapVideoFrame(CUvideodecoder hDecoder, + unsigned int DevPtr); +#endif + +/****************************************************************************************************************************/ +//! \fn CUresult CUDAAPI cuvidMapVideoFrame64(CUvideodecoder hDecoder, int nPicIdx, unsigned long long *pDevPtr, +//! unsigned int * pPitch, CUVIDPROCPARAMS *pVPP); +//! Post-process and map video frame corresponding to nPicIdx for use in cuda. Returns cuda device pointer and associated +//! pitch of the video frame +/****************************************************************************************************************************/ +extern CUresult CUDAAPI cuvidMapVideoFrame64(CUvideodecoder hDecoder, + int nPicIdx, + unsigned long long* pDevPtr, + unsigned int* pPitch, + CUVIDPROCPARAMS* pVPP); + +/**************************************************************************************************/ +//! \fn CUresult CUDAAPI cuvidUnmapVideoFrame64(CUvideodecoder hDecoder, unsigned long long DevPtr); +//! Unmap a previously mapped video frame +/**************************************************************************************************/ +extern CUresult CUDAAPI cuvidUnmapVideoFrame64(CUvideodecoder hDecoder, + unsigned long long DevPtr); + +#if defined(__CUVID_DEVPTR64) && !defined(__CUVID_INTERNAL) +#define cuvidMapVideoFrame cuvidMapVideoFrame64 +#define cuvidUnmapVideoFrame cuvidUnmapVideoFrame64 +#endif + +/********************************************************************************************************************/ +//! +//! Context-locking: to facilitate multi-threaded implementations, the following 4 functions +//! provide a simple mutex-style host synchronization. If a non-NULL context is specified +//! in CUVIDDECODECREATEINFO, the codec library will acquire the mutex associated with the given +//! context before making any cuda calls. +//! A multi-threaded application could create a lock associated with a context handle so that +//! multiple threads can safely share the same cuda context: +//! - use cuCtxPopCurrent immediately after context creation in order to create a 'floating' context +//! that can be passed to cuvidCtxLockCreate. +//! - When using a floating context, all cuda calls should only be made within a cuvidCtxLock/cuvidCtxUnlock section. +//! +//! NOTE: This is a safer alternative to cuCtxPushCurrent and cuCtxPopCurrent, and is not related to video +//! decoder in any way (implemented as a critical section associated with cuCtx{Push|Pop}Current calls). +/********************************************************************************************************************/ + +/********************************************************************************************************************/ +//! \fn CUresult CUDAAPI cuvidCtxLockCreate(CUvideoctxlock *pLock, CUcontext ctx) +//! This API is used to create CtxLock object +/********************************************************************************************************************/ +extern CUresult CUDAAPI cuvidCtxLockCreate(CUvideoctxlock* pLock, + CUcontext ctx); + +/********************************************************************************************************************/ +//! \fn CUresult CUDAAPI cuvidCtxLockDestroy(CUvideoctxlock lck) +//! This API is used to free CtxLock object +/********************************************************************************************************************/ +extern CUresult CUDAAPI cuvidCtxLockDestroy(CUvideoctxlock lck); + +/********************************************************************************************************************/ +//! \fn CUresult CUDAAPI cuvidCtxLock(CUvideoctxlock lck, unsigned int reserved_flags) +//! This API is used to acquire ctxlock +/********************************************************************************************************************/ +extern CUresult CUDAAPI cuvidCtxLock(CUvideoctxlock lck, + unsigned int reserved_flags); + +/********************************************************************************************************************/ +//! \fn CUresult CUDAAPI cuvidCtxUnlock(CUvideoctxlock lck, unsigned int reserved_flags) +//! This API is used to release ctxlock +/********************************************************************************************************************/ +extern CUresult CUDAAPI cuvidCtxUnlock(CUvideoctxlock lck, + unsigned int reserved_flags); + +/**********************************************************************************************/ + +#if defined(__cplusplus) +} +// Auto-lock helper for C++ applications +class CCtxAutoLock { + private: + CUvideoctxlock m_ctx; + + public: + CCtxAutoLock(CUvideoctxlock ctx) : m_ctx(ctx) { cuvidCtxLock(m_ctx, 0); } + ~CCtxAutoLock() { cuvidCtxUnlock(m_ctx, 0); } +}; +#endif /* __cplusplus */ + +#endif // __CUDA_VIDEO_H__ diff --git a/NvCodec/include/nvEncodeAPI.h b/NvCodec/include/nvEncodeAPI.h index 52a53160..0a12ba54 100644 --- a/NvCodec/include/nvEncodeAPI.h +++ b/NvCodec/include/nvEncodeAPI.h @@ -1,3820 +1,4987 @@ -/* - * This copyright notice applies to this header file only: - * - * Copyright (c) 2010-2020 NVIDIA Corporation - * - * Permission is hereby granted, free of charge, to any person - * obtaining a copy of this software and associated documentation - * files (the "Software"), to deal in the Software without - * restriction, including without limitation the rights to use, - * copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the software, and to permit persons to whom the - * software is furnished to do so, subject to the following - * conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES - * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT - * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, - * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - * OTHER DEALINGS IN THE SOFTWARE. - */ - -/** - * \file nvEncodeAPI.h - * NVIDIA GPUs - beginning with the Kepler generation - contain a hardware-based encoder - * (referred to as NVENC) which provides fully-accelerated hardware-based video encoding. - * NvEncodeAPI provides the interface for NVIDIA video encoder (NVENC). - * \date 2011-2020 - * This file contains the interface constants, structure definitions and function prototypes. - */ - -#ifndef _NV_ENCODEAPI_H_ -#define _NV_ENCODEAPI_H_ - -#include - -#ifdef _WIN32 -#include -#endif - -#ifdef _MSC_VER -#ifndef _STDINT -typedef __int32 int32_t; -typedef unsigned __int32 uint32_t; -typedef __int64 int64_t; -typedef unsigned __int64 uint64_t; -typedef signed char int8_t; -typedef unsigned char uint8_t; -typedef short int16_t; -typedef unsigned short uint16_t; -#endif -#else -#include -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * \addtogroup ENCODER_STRUCTURE NvEncodeAPI Data structures - * @{ - */ - -#ifdef _WIN32 -#define NVENCAPI __stdcall -typedef RECT NVENC_RECT; -#else -#define NVENCAPI -// ========================================================================================= -#if !defined(GUID) && !defined(GUID_DEFINED) -/*! - * \struct GUID - * Abstracts the GUID structure for non-windows platforms. - */ -// ========================================================================================= -typedef struct -{ - uint32_t Data1; /**< [in]: Specifies the first 8 hexadecimal digits of the GUID. */ - uint16_t Data2; /**< [in]: Specifies the first group of 4 hexadecimal digits. */ - uint16_t Data3; /**< [in]: Specifies the second group of 4 hexadecimal digits. */ - uint8_t Data4[8]; /**< [in]: Array of 8 bytes. The first 2 bytes contain the third group of 4 hexadecimal digits. - The remaining 6 bytes contain the final 12 hexadecimal digits. */ -} GUID; -#endif // GUID - -/** - * \struct _NVENC_RECT - * Defines a Rectangle. Used in ::NV_ENC_PREPROCESS_FRAME. - */ -typedef struct _NVENC_RECT -{ - uint32_t left; /**< [in]: X coordinate of the upper left corner of rectangular area to be specified. */ - uint32_t top; /**< [in]: Y coordinate of the upper left corner of the rectangular area to be specified. */ - uint32_t right; /**< [in]: X coordinate of the bottom right corner of the rectangular area to be specified. */ - uint32_t bottom; /**< [in]: Y coordinate of the bottom right corner of the rectangular area to be specified. */ -} NVENC_RECT; - -#endif // _WIN32 - -/** @} */ /* End of GUID and NVENC_RECT structure grouping*/ - -typedef void* NV_ENC_INPUT_PTR; /**< NVENCODE API input buffer */ -typedef void* NV_ENC_OUTPUT_PTR; /**< NVENCODE API output buffer*/ -typedef void* NV_ENC_REGISTERED_PTR; /**< A Resource that has been registered with NVENCODE API*/ -typedef void* NV_ENC_CUSTREAM_PTR; /**< Pointer to CUstream*/ - -#define NVENCAPI_MAJOR_VERSION 10 -#define NVENCAPI_MINOR_VERSION 0 - -#define NVENCAPI_VERSION (NVENCAPI_MAJOR_VERSION | (NVENCAPI_MINOR_VERSION << 24)) - -/** - * Macro to generate per-structure version for use with API. - */ -#define NVENCAPI_STRUCT_VERSION(ver) ((uint32_t)NVENCAPI_VERSION | ((ver)<<16) | (0x7 << 28)) - - -#define NVENC_INFINITE_GOPLENGTH 0xffffffff - -#define NV_MAX_SEQ_HDR_LEN (512) - -#ifdef __GNUC__ -#define NV_ENC_DEPRECATED __attribute__ ((deprecated("WILL BE REMOVED IN A FUTURE VIDEO CODEC SDK VERSION"))) -#elif defined(_MSC_VER) -#define NV_ENC_DEPRECATED __declspec(deprecated("WILL BE REMOVED IN A FUTURE VIDEO CODEC SDK VERSION")) -#endif - -// ========================================================================================= -// Encode Codec GUIDS supported by the NvEncodeAPI interface. -// ========================================================================================= - -// {6BC82762-4E63-4ca4-AA85-1E50F321F6BF} -static const GUID NV_ENC_CODEC_H264_GUID = -{ 0x6bc82762, 0x4e63, 0x4ca4, { 0xaa, 0x85, 0x1e, 0x50, 0xf3, 0x21, 0xf6, 0xbf } }; - -// {790CDC88-4522-4d7b-9425-BDA9975F7603} -static const GUID NV_ENC_CODEC_HEVC_GUID = -{ 0x790cdc88, 0x4522, 0x4d7b, { 0x94, 0x25, 0xbd, 0xa9, 0x97, 0x5f, 0x76, 0x3 } }; - - - -// ========================================================================================= -// * Encode Profile GUIDS supported by the NvEncodeAPI interface. -// ========================================================================================= - -// {BFD6F8E7-233C-4341-8B3E-4818523803F4} -static const GUID NV_ENC_CODEC_PROFILE_AUTOSELECT_GUID = -{ 0xbfd6f8e7, 0x233c, 0x4341, { 0x8b, 0x3e, 0x48, 0x18, 0x52, 0x38, 0x3, 0xf4 } }; - -// {0727BCAA-78C4-4c83-8C2F-EF3DFF267C6A} -static const GUID NV_ENC_H264_PROFILE_BASELINE_GUID = -{ 0x727bcaa, 0x78c4, 0x4c83, { 0x8c, 0x2f, 0xef, 0x3d, 0xff, 0x26, 0x7c, 0x6a } }; - -// {60B5C1D4-67FE-4790-94D5-C4726D7B6E6D} -static const GUID NV_ENC_H264_PROFILE_MAIN_GUID = -{ 0x60b5c1d4, 0x67fe, 0x4790, { 0x94, 0xd5, 0xc4, 0x72, 0x6d, 0x7b, 0x6e, 0x6d } }; - -// {E7CBC309-4F7A-4b89-AF2A-D537C92BE310} -static const GUID NV_ENC_H264_PROFILE_HIGH_GUID = -{ 0xe7cbc309, 0x4f7a, 0x4b89, { 0xaf, 0x2a, 0xd5, 0x37, 0xc9, 0x2b, 0xe3, 0x10 } }; - -// {7AC663CB-A598-4960-B844-339B261A7D52} -static const GUID NV_ENC_H264_PROFILE_HIGH_444_GUID = -{ 0x7ac663cb, 0xa598, 0x4960, { 0xb8, 0x44, 0x33, 0x9b, 0x26, 0x1a, 0x7d, 0x52 } }; - -// {40847BF5-33F7-4601-9084-E8FE3C1DB8B7} -static const GUID NV_ENC_H264_PROFILE_STEREO_GUID = -{ 0x40847bf5, 0x33f7, 0x4601, { 0x90, 0x84, 0xe8, 0xfe, 0x3c, 0x1d, 0xb8, 0xb7 } }; - -// {CE788D20-AAA9-4318-92BB-AC7E858C8D36} -static const GUID NV_ENC_H264_PROFILE_SVC_TEMPORAL_SCALABILTY = -{ 0xce788d20, 0xaaa9, 0x4318, { 0x92, 0xbb, 0xac, 0x7e, 0x85, 0x8c, 0x8d, 0x36 } }; - -// {B405AFAC-F32B-417B-89C4-9ABEED3E5978} -static const GUID NV_ENC_H264_PROFILE_PROGRESSIVE_HIGH_GUID = -{ 0xb405afac, 0xf32b, 0x417b, { 0x89, 0xc4, 0x9a, 0xbe, 0xed, 0x3e, 0x59, 0x78 } }; - -// {AEC1BD87-E85B-48f2-84C3-98BCA6285072} -static const GUID NV_ENC_H264_PROFILE_CONSTRAINED_HIGH_GUID = -{ 0xaec1bd87, 0xe85b, 0x48f2, { 0x84, 0xc3, 0x98, 0xbc, 0xa6, 0x28, 0x50, 0x72 } }; - -// {B514C39A-B55B-40fa-878F-F1253B4DFDEC} -static const GUID NV_ENC_HEVC_PROFILE_MAIN_GUID = -{ 0xb514c39a, 0xb55b, 0x40fa, { 0x87, 0x8f, 0xf1, 0x25, 0x3b, 0x4d, 0xfd, 0xec } }; - -// {fa4d2b6c-3a5b-411a-8018-0a3f5e3c9be5} -static const GUID NV_ENC_HEVC_PROFILE_MAIN10_GUID = -{ 0xfa4d2b6c, 0x3a5b, 0x411a, { 0x80, 0x18, 0x0a, 0x3f, 0x5e, 0x3c, 0x9b, 0xe5 } }; - -// For HEVC Main 444 8 bit and HEVC Main 444 10 bit profiles only -// {51ec32b5-1b4c-453c-9cbd-b616bd621341} -static const GUID NV_ENC_HEVC_PROFILE_FREXT_GUID = -{ 0x51ec32b5, 0x1b4c, 0x453c, { 0x9c, 0xbd, 0xb6, 0x16, 0xbd, 0x62, 0x13, 0x41 } }; - -// ========================================================================================= -// * Preset GUIDS supported by the NvEncodeAPI interface. -// ========================================================================================= -// {B2DFB705-4EBD-4C49-9B5F-24A777D3E587} -NV_ENC_DEPRECATED static const GUID NV_ENC_PRESET_DEFAULT_GUID = -{ 0xb2dfb705, 0x4ebd, 0x4c49, { 0x9b, 0x5f, 0x24, 0xa7, 0x77, 0xd3, 0xe5, 0x87 } }; - -// {60E4C59F-E846-4484-A56D-CD45BE9FDDF6} -NV_ENC_DEPRECATED static const GUID NV_ENC_PRESET_HP_GUID = -{ 0x60e4c59f, 0xe846, 0x4484, { 0xa5, 0x6d, 0xcd, 0x45, 0xbe, 0x9f, 0xdd, 0xf6 } }; - -// {34DBA71D-A77B-4B8F-9C3E-B6D5DA24C012} -NV_ENC_DEPRECATED static const GUID NV_ENC_PRESET_HQ_GUID = -{ 0x34dba71d, 0xa77b, 0x4b8f, { 0x9c, 0x3e, 0xb6, 0xd5, 0xda, 0x24, 0xc0, 0x12 } }; - -// {82E3E450-BDBB-4e40-989C-82A90DF9EF32} -NV_ENC_DEPRECATED static const GUID NV_ENC_PRESET_BD_GUID = -{ 0x82e3e450, 0xbdbb, 0x4e40, { 0x98, 0x9c, 0x82, 0xa9, 0xd, 0xf9, 0xef, 0x32 } }; - -// {49DF21C5-6DFA-4feb-9787-6ACC9EFFB726} -NV_ENC_DEPRECATED static const GUID NV_ENC_PRESET_LOW_LATENCY_DEFAULT_GUID = -{ 0x49df21c5, 0x6dfa, 0x4feb, { 0x97, 0x87, 0x6a, 0xcc, 0x9e, 0xff, 0xb7, 0x26 } }; - -// {C5F733B9-EA97-4cf9-BEC2-BF78A74FD105} -NV_ENC_DEPRECATED static const GUID NV_ENC_PRESET_LOW_LATENCY_HQ_GUID = -{ 0xc5f733b9, 0xea97, 0x4cf9, { 0xbe, 0xc2, 0xbf, 0x78, 0xa7, 0x4f, 0xd1, 0x5 } }; - -// {67082A44-4BAD-48FA-98EA-93056D150A58} -NV_ENC_DEPRECATED static const GUID NV_ENC_PRESET_LOW_LATENCY_HP_GUID = -{ 0x67082a44, 0x4bad, 0x48fa, { 0x98, 0xea, 0x93, 0x5, 0x6d, 0x15, 0xa, 0x58 } }; - -// {D5BFB716-C604-44e7-9BB8-DEA5510FC3AC} -NV_ENC_DEPRECATED static const GUID NV_ENC_PRESET_LOSSLESS_DEFAULT_GUID = -{ 0xd5bfb716, 0xc604, 0x44e7, { 0x9b, 0xb8, 0xde, 0xa5, 0x51, 0xf, 0xc3, 0xac } }; - -// {149998E7-2364-411d-82EF-179888093409} -NV_ENC_DEPRECATED static const GUID NV_ENC_PRESET_LOSSLESS_HP_GUID = -{ 0x149998e7, 0x2364, 0x411d, { 0x82, 0xef, 0x17, 0x98, 0x88, 0x9, 0x34, 0x9 } }; - -// Performance degrades and quality improves as we move from P1 to P7. Presets P3 to P7 for H264 and Presets P2 to P7 for HEVC have B frames enabled by default -// for HIGH_QUALITY and LOSSLESS tuning info, and will not work with Weighted Prediction enabled. In case Weighted Prediction is required, disable B frames by -// setting frameIntervalP = 1 -// {FC0A8D3E-45F8-4CF8-80C7-298871590EBF} -static const GUID NV_ENC_PRESET_P1_GUID = -{ 0xfc0a8d3e, 0x45f8, 0x4cf8, { 0x80, 0xc7, 0x29, 0x88, 0x71, 0x59, 0xe, 0xbf } }; - -// {F581CFB8-88D6-4381-93F0-DF13F9C27DAB} -static const GUID NV_ENC_PRESET_P2_GUID = -{ 0xf581cfb8, 0x88d6, 0x4381, { 0x93, 0xf0, 0xdf, 0x13, 0xf9, 0xc2, 0x7d, 0xab } }; - -// {36850110-3A07-441F-94D5-3670631F91F6} -static const GUID NV_ENC_PRESET_P3_GUID = -{ 0x36850110, 0x3a07, 0x441f, { 0x94, 0xd5, 0x36, 0x70, 0x63, 0x1f, 0x91, 0xf6 } }; - -// {90A7B826-DF06-4862-B9D2-CD6D73A08681} -static const GUID NV_ENC_PRESET_P4_GUID = -{ 0x90a7b826, 0xdf06, 0x4862, { 0xb9, 0xd2, 0xcd, 0x6d, 0x73, 0xa0, 0x86, 0x81 } }; - -// {21C6E6B4-297A-4CBA-998F-B6CBDE72ADE3} -static const GUID NV_ENC_PRESET_P5_GUID = -{ 0x21c6e6b4, 0x297a, 0x4cba, { 0x99, 0x8f, 0xb6, 0xcb, 0xde, 0x72, 0xad, 0xe3 } }; - -// {8E75C279-6299-4AB6-8302-0B215A335CF5} -static const GUID NV_ENC_PRESET_P6_GUID = -{ 0x8e75c279, 0x6299, 0x4ab6, { 0x83, 0x2, 0xb, 0x21, 0x5a, 0x33, 0x5c, 0xf5 } }; - -// {84848C12-6F71-4C13-931B-53E283F57974} -static const GUID NV_ENC_PRESET_P7_GUID = -{ 0x84848c12, 0x6f71, 0x4c13, { 0x93, 0x1b, 0x53, 0xe2, 0x83, 0xf5, 0x79, 0x74 } }; - -/** - * \addtogroup ENCODER_STRUCTURE NvEncodeAPI Data structures - * @{ - */ - -/** - * Input frame encode modes - */ -typedef enum _NV_ENC_PARAMS_FRAME_FIELD_MODE -{ - NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME = 0x01, /**< Frame mode */ - NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD = 0x02, /**< Field mode */ - NV_ENC_PARAMS_FRAME_FIELD_MODE_MBAFF = 0x03 /**< MB adaptive frame/field */ -} NV_ENC_PARAMS_FRAME_FIELD_MODE; - -/** - * Rate Control Modes - */ -typedef enum _NV_ENC_PARAMS_RC_MODE -{ - NV_ENC_PARAMS_RC_CONSTQP = 0x0, /**< Constant QP mode */ - NV_ENC_PARAMS_RC_VBR = 0x1, /**< Variable bitrate mode */ - NV_ENC_PARAMS_RC_CBR = 0x2, /**< Constant bitrate mode */ - NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ = 0x8, /**< Deprecated, use NV_ENC_PARAMS_RC_CBR + NV_ENC_TWO_PASS_QUARTER_RESOLUTION / NV_ENC_TWO_PASS_FULL_RESOLUTION + - lowDelayKeyFrameScale=1 */ - NV_ENC_PARAMS_RC_CBR_HQ = 0x10, /**< Deprecated, use NV_ENC_PARAMS_RC_CBR + NV_ENC_TWO_PASS_QUARTER_RESOLUTION / NV_ENC_TWO_PASS_FULL_RESOLUTION */ - NV_ENC_PARAMS_RC_VBR_HQ = 0x20 /**< Deprecated, use NV_ENC_PARAMS_RC_VBR + NV_ENC_TWO_PASS_QUARTER_RESOLUTION / NV_ENC_TWO_PASS_FULL_RESOLUTION */ -} NV_ENC_PARAMS_RC_MODE; - -/** - * Multi Pass encoding - */ -typedef enum _NV_ENC_MULTI_PASS -{ - NV_ENC_MULTI_PASS_DISABLED = 0x0, /**< Single Pass */ - NV_ENC_TWO_PASS_QUARTER_RESOLUTION = 0x1, /**< Two Pass encoding is enabled where first Pass is quarter resolution */ - NV_ENC_TWO_PASS_FULL_RESOLUTION = 0x2, /**< Two Pass encoding is enabled where first Pass is full resolution */ -} NV_ENC_MULTI_PASS; - -/** - * Emphasis Levels - */ -typedef enum _NV_ENC_EMPHASIS_MAP_LEVEL -{ - NV_ENC_EMPHASIS_MAP_LEVEL_0 = 0x0, /**< Emphasis Map Level 0, for zero Delta QP value */ - NV_ENC_EMPHASIS_MAP_LEVEL_1 = 0x1, /**< Emphasis Map Level 1, for very low Delta QP value */ - NV_ENC_EMPHASIS_MAP_LEVEL_2 = 0x2, /**< Emphasis Map Level 2, for low Delta QP value */ - NV_ENC_EMPHASIS_MAP_LEVEL_3 = 0x3, /**< Emphasis Map Level 3, for medium Delta QP value */ - NV_ENC_EMPHASIS_MAP_LEVEL_4 = 0x4, /**< Emphasis Map Level 4, for high Delta QP value */ - NV_ENC_EMPHASIS_MAP_LEVEL_5 = 0x5 /**< Emphasis Map Level 5, for very high Delta QP value */ -} NV_ENC_EMPHASIS_MAP_LEVEL; - -/** - * QP MAP MODE - */ -typedef enum _NV_ENC_QP_MAP_MODE -{ - NV_ENC_QP_MAP_DISABLED = 0x0, /**< Value in NV_ENC_PIC_PARAMS::qpDeltaMap have no effect. */ - NV_ENC_QP_MAP_EMPHASIS = 0x1, /**< Value in NV_ENC_PIC_PARAMS::qpDeltaMap will be treated as Emphasis level. Currently this is only supported for H264 */ - NV_ENC_QP_MAP_DELTA = 0x2, /**< Value in NV_ENC_PIC_PARAMS::qpDeltaMap will be treated as QP delta map. */ - NV_ENC_QP_MAP = 0x3, /**< Currently This is not supported. Value in NV_ENC_PIC_PARAMS::qpDeltaMap will be treated as QP value. */ -} NV_ENC_QP_MAP_MODE; - -#define NV_ENC_PARAMS_RC_VBR_MINQP (NV_ENC_PARAMS_RC_MODE)0x4 /**< Deprecated */ -#define NV_ENC_PARAMS_RC_2_PASS_QUALITY NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ /**< Deprecated */ -#define NV_ENC_PARAMS_RC_2_PASS_FRAMESIZE_CAP NV_ENC_PARAMS_RC_CBR_HQ /**< Deprecated */ -#define NV_ENC_PARAMS_RC_2_PASS_VBR NV_ENC_PARAMS_RC_VBR_HQ /**< Deprecated */ -#define NV_ENC_PARAMS_RC_CBR2 NV_ENC_PARAMS_RC_CBR /**< Deprecated */ - -/** - * Input picture structure - */ -typedef enum _NV_ENC_PIC_STRUCT -{ - NV_ENC_PIC_STRUCT_FRAME = 0x01, /**< Progressive frame */ - NV_ENC_PIC_STRUCT_FIELD_TOP_BOTTOM = 0x02, /**< Field encoding top field first */ - NV_ENC_PIC_STRUCT_FIELD_BOTTOM_TOP = 0x03 /**< Field encoding bottom field first */ -} NV_ENC_PIC_STRUCT; - -/** - * Input picture type - */ -typedef enum _NV_ENC_PIC_TYPE -{ - NV_ENC_PIC_TYPE_P = 0x0, /**< Forward predicted */ - NV_ENC_PIC_TYPE_B = 0x01, /**< Bi-directionally predicted picture */ - NV_ENC_PIC_TYPE_I = 0x02, /**< Intra predicted picture */ - NV_ENC_PIC_TYPE_IDR = 0x03, /**< IDR picture */ - NV_ENC_PIC_TYPE_BI = 0x04, /**< Bi-directionally predicted with only Intra MBs */ - NV_ENC_PIC_TYPE_SKIPPED = 0x05, /**< Picture is skipped */ - NV_ENC_PIC_TYPE_INTRA_REFRESH = 0x06, /**< First picture in intra refresh cycle */ - NV_ENC_PIC_TYPE_NONREF_P = 0x07, /**< Non reference P picture */ - NV_ENC_PIC_TYPE_UNKNOWN = 0xFF /**< Picture type unknown */ -} NV_ENC_PIC_TYPE; - -/** - * Motion vector precisions - */ -typedef enum _NV_ENC_MV_PRECISION -{ - NV_ENC_MV_PRECISION_DEFAULT = 0x0, /**< Driver selects Quarter-Pel motion vector precision by default */ - NV_ENC_MV_PRECISION_FULL_PEL = 0x01, /**< Full-Pel motion vector precision */ - NV_ENC_MV_PRECISION_HALF_PEL = 0x02, /**< Half-Pel motion vector precision */ - NV_ENC_MV_PRECISION_QUARTER_PEL = 0x03 /**< Quarter-Pel motion vector precision */ -} NV_ENC_MV_PRECISION; - - -/** - * Input buffer formats - */ -typedef enum _NV_ENC_BUFFER_FORMAT -{ - NV_ENC_BUFFER_FORMAT_UNDEFINED = 0x00000000, /**< Undefined buffer format */ - - NV_ENC_BUFFER_FORMAT_NV12 = 0x00000001, /**< Semi-Planar YUV [Y plane followed by interleaved UV plane] */ - NV_ENC_BUFFER_FORMAT_YV12 = 0x00000010, /**< Planar YUV [Y plane followed by V and U planes] */ - NV_ENC_BUFFER_FORMAT_IYUV = 0x00000100, /**< Planar YUV [Y plane followed by U and V planes] */ - NV_ENC_BUFFER_FORMAT_YUV444 = 0x00001000, /**< Planar YUV [Y plane followed by U and V planes] */ - NV_ENC_BUFFER_FORMAT_YUV420_10BIT = 0x00010000, /**< 10 bit Semi-Planar YUV [Y plane followed by interleaved UV plane]. Each pixel of size 2 bytes. Most Significant 10 bits contain pixel data. */ - NV_ENC_BUFFER_FORMAT_YUV444_10BIT = 0x00100000, /**< 10 bit Planar YUV444 [Y plane followed by U and V planes]. Each pixel of size 2 bytes. Most Significant 10 bits contain pixel data. */ - NV_ENC_BUFFER_FORMAT_ARGB = 0x01000000, /**< 8 bit Packed A8R8G8B8. This is a word-ordered format - where a pixel is represented by a 32-bit word with B - in the lowest 8 bits, G in the next 8 bits, R in the - 8 bits after that and A in the highest 8 bits. */ - NV_ENC_BUFFER_FORMAT_ARGB10 = 0x02000000, /**< 10 bit Packed A2R10G10B10. This is a word-ordered format - where a pixel is represented by a 32-bit word with B - in the lowest 10 bits, G in the next 10 bits, R in the - 10 bits after that and A in the highest 2 bits. */ - NV_ENC_BUFFER_FORMAT_AYUV = 0x04000000, /**< 8 bit Packed A8Y8U8V8. This is a word-ordered format - where a pixel is represented by a 32-bit word with V - in the lowest 8 bits, U in the next 8 bits, Y in the - 8 bits after that and A in the highest 8 bits. */ - NV_ENC_BUFFER_FORMAT_ABGR = 0x10000000, /**< 8 bit Packed A8B8G8R8. This is a word-ordered format - where a pixel is represented by a 32-bit word with R - in the lowest 8 bits, G in the next 8 bits, B in the - 8 bits after that and A in the highest 8 bits. */ - NV_ENC_BUFFER_FORMAT_ABGR10 = 0x20000000, /**< 10 bit Packed A2B10G10R10. This is a word-ordered format - where a pixel is represented by a 32-bit word with R - in the lowest 10 bits, G in the next 10 bits, B in the - 10 bits after that and A in the highest 2 bits. */ - NV_ENC_BUFFER_FORMAT_U8 = 0x40000000, /**< Buffer format representing one-dimensional buffer. - This format should be used only when registering the - resource as output buffer, which will be used to write - the encoded bit stream or H.264 ME only mode output. */ -} NV_ENC_BUFFER_FORMAT; - -#define NV_ENC_BUFFER_FORMAT_NV12_PL NV_ENC_BUFFER_FORMAT_NV12 -#define NV_ENC_BUFFER_FORMAT_YV12_PL NV_ENC_BUFFER_FORMAT_YV12 -#define NV_ENC_BUFFER_FORMAT_IYUV_PL NV_ENC_BUFFER_FORMAT_IYUV -#define NV_ENC_BUFFER_FORMAT_YUV444_PL NV_ENC_BUFFER_FORMAT_YUV444 - -/** - * Encoding levels - */ -typedef enum _NV_ENC_LEVEL -{ - NV_ENC_LEVEL_AUTOSELECT = 0, - - NV_ENC_LEVEL_H264_1 = 10, - NV_ENC_LEVEL_H264_1b = 9, - NV_ENC_LEVEL_H264_11 = 11, - NV_ENC_LEVEL_H264_12 = 12, - NV_ENC_LEVEL_H264_13 = 13, - NV_ENC_LEVEL_H264_2 = 20, - NV_ENC_LEVEL_H264_21 = 21, - NV_ENC_LEVEL_H264_22 = 22, - NV_ENC_LEVEL_H264_3 = 30, - NV_ENC_LEVEL_H264_31 = 31, - NV_ENC_LEVEL_H264_32 = 32, - NV_ENC_LEVEL_H264_4 = 40, - NV_ENC_LEVEL_H264_41 = 41, - NV_ENC_LEVEL_H264_42 = 42, - NV_ENC_LEVEL_H264_5 = 50, - NV_ENC_LEVEL_H264_51 = 51, - NV_ENC_LEVEL_H264_52 = 52, - NV_ENC_LEVEL_H264_60 = 60, - NV_ENC_LEVEL_H264_61 = 61, - NV_ENC_LEVEL_H264_62 = 62, - - NV_ENC_LEVEL_HEVC_1 = 30, - NV_ENC_LEVEL_HEVC_2 = 60, - NV_ENC_LEVEL_HEVC_21 = 63, - NV_ENC_LEVEL_HEVC_3 = 90, - NV_ENC_LEVEL_HEVC_31 = 93, - NV_ENC_LEVEL_HEVC_4 = 120, - NV_ENC_LEVEL_HEVC_41 = 123, - NV_ENC_LEVEL_HEVC_5 = 150, - NV_ENC_LEVEL_HEVC_51 = 153, - NV_ENC_LEVEL_HEVC_52 = 156, - NV_ENC_LEVEL_HEVC_6 = 180, - NV_ENC_LEVEL_HEVC_61 = 183, - NV_ENC_LEVEL_HEVC_62 = 186, - - NV_ENC_TIER_HEVC_MAIN = 0, - NV_ENC_TIER_HEVC_HIGH = 1 -} NV_ENC_LEVEL; - -/** - * Error Codes - */ -typedef enum _NVENCSTATUS -{ - /** - * This indicates that API call returned with no errors. - */ - NV_ENC_SUCCESS, - - /** - * This indicates that no encode capable devices were detected. - */ - NV_ENC_ERR_NO_ENCODE_DEVICE, - - /** - * This indicates that devices pass by the client is not supported. - */ - NV_ENC_ERR_UNSUPPORTED_DEVICE, - - /** - * This indicates that the encoder device supplied by the client is not - * valid. - */ - NV_ENC_ERR_INVALID_ENCODERDEVICE, - - /** - * This indicates that device passed to the API call is invalid. - */ - NV_ENC_ERR_INVALID_DEVICE, - - /** - * This indicates that device passed to the API call is no longer available and - * needs to be reinitialized. The clients need to destroy the current encoder - * session by freeing the allocated input output buffers and destroying the device - * and create a new encoding session. - */ - NV_ENC_ERR_DEVICE_NOT_EXIST, - - /** - * This indicates that one or more of the pointers passed to the API call - * is invalid. - */ - NV_ENC_ERR_INVALID_PTR, - - /** - * This indicates that completion event passed in ::NvEncEncodePicture() call - * is invalid. - */ - NV_ENC_ERR_INVALID_EVENT, - - /** - * This indicates that one or more of the parameter passed to the API call - * is invalid. - */ - NV_ENC_ERR_INVALID_PARAM, - - /** - * This indicates that an API call was made in wrong sequence/order. - */ - NV_ENC_ERR_INVALID_CALL, - - /** - * This indicates that the API call failed because it was unable to allocate - * enough memory to perform the requested operation. - */ - NV_ENC_ERR_OUT_OF_MEMORY, - - /** - * This indicates that the encoder has not been initialized with - * ::NvEncInitializeEncoder() or that initialization has failed. - * The client cannot allocate input or output buffers or do any encoding - * related operation before successfully initializing the encoder. - */ - NV_ENC_ERR_ENCODER_NOT_INITIALIZED, - - /** - * This indicates that an unsupported parameter was passed by the client. - */ - NV_ENC_ERR_UNSUPPORTED_PARAM, - - /** - * This indicates that the ::NvEncLockBitstream() failed to lock the output - * buffer. This happens when the client makes a non blocking lock call to - * access the output bitstream by passing NV_ENC_LOCK_BITSTREAM::doNotWait flag. - * This is not a fatal error and client should retry the same operation after - * few milliseconds. - */ - NV_ENC_ERR_LOCK_BUSY, - - /** - * This indicates that the size of the user buffer passed by the client is - * insufficient for the requested operation. - */ - NV_ENC_ERR_NOT_ENOUGH_BUFFER, - - /** - * This indicates that an invalid struct version was used by the client. - */ - NV_ENC_ERR_INVALID_VERSION, - - /** - * This indicates that ::NvEncMapInputResource() API failed to map the client - * provided input resource. - */ - NV_ENC_ERR_MAP_FAILED, - - /** - * This indicates encode driver requires more input buffers to produce an output - * bitstream. If this error is returned from ::NvEncEncodePicture() API, this - * is not a fatal error. If the client is encoding with B frames then, - * ::NvEncEncodePicture() API might be buffering the input frame for re-ordering. - * - * A client operating in synchronous mode cannot call ::NvEncLockBitstream() - * API on the output bitstream buffer if ::NvEncEncodePicture() returned the - * ::NV_ENC_ERR_NEED_MORE_INPUT error code. - * The client must continue providing input frames until encode driver returns - * ::NV_ENC_SUCCESS. After receiving ::NV_ENC_SUCCESS status the client can call - * ::NvEncLockBitstream() API on the output buffers in the same order in which - * it has called ::NvEncEncodePicture(). - */ - NV_ENC_ERR_NEED_MORE_INPUT, - - /** - * This indicates that the HW encoder is busy encoding and is unable to encode - * the input. The client should call ::NvEncEncodePicture() again after few - * milliseconds. - */ - NV_ENC_ERR_ENCODER_BUSY, - - /** - * This indicates that the completion event passed in ::NvEncEncodePicture() - * API has not been registered with encoder driver using ::NvEncRegisterAsyncEvent(). - */ - NV_ENC_ERR_EVENT_NOT_REGISTERD, - - /** - * This indicates that an unknown internal error has occurred. - */ - NV_ENC_ERR_GENERIC, - - /** - * This indicates that the client is attempting to use a feature - * that is not available for the license type for the current system. - */ - NV_ENC_ERR_INCOMPATIBLE_CLIENT_KEY, - - /** - * This indicates that the client is attempting to use a feature - * that is not implemented for the current version. - */ - NV_ENC_ERR_UNIMPLEMENTED, - - /** - * This indicates that the ::NvEncRegisterResource API failed to register the resource. - */ - NV_ENC_ERR_RESOURCE_REGISTER_FAILED, - - /** - * This indicates that the client is attempting to unregister a resource - * that has not been successfully registered. - */ - NV_ENC_ERR_RESOURCE_NOT_REGISTERED, - - /** - * This indicates that the client is attempting to unmap a resource - * that has not been successfully mapped. - */ - NV_ENC_ERR_RESOURCE_NOT_MAPPED, - -} NVENCSTATUS; - -/** - * Encode Picture encode flags. - */ -typedef enum _NV_ENC_PIC_FLAGS -{ - NV_ENC_PIC_FLAG_FORCEINTRA = 0x1, /**< Encode the current picture as an Intra picture */ - NV_ENC_PIC_FLAG_FORCEIDR = 0x2, /**< Encode the current picture as an IDR picture. - This flag is only valid when Picture type decision is taken by the Encoder - [_NV_ENC_INITIALIZE_PARAMS::enablePTD == 1]. */ - NV_ENC_PIC_FLAG_OUTPUT_SPSPPS = 0x4, /**< Write the sequence and picture header in encoded bitstream of the current picture */ - NV_ENC_PIC_FLAG_EOS = 0x8, /**< Indicates end of the input stream */ -} NV_ENC_PIC_FLAGS; - -/** - * Memory heap to allocate input and output buffers. - */ -typedef enum _NV_ENC_MEMORY_HEAP -{ - NV_ENC_MEMORY_HEAP_AUTOSELECT = 0, /**< Memory heap to be decided by the encoder driver based on the usage */ - NV_ENC_MEMORY_HEAP_VID = 1, /**< Memory heap is in local video memory */ - NV_ENC_MEMORY_HEAP_SYSMEM_CACHED = 2, /**< Memory heap is in cached system memory */ - NV_ENC_MEMORY_HEAP_SYSMEM_UNCACHED = 3 /**< Memory heap is in uncached system memory */ -} NV_ENC_MEMORY_HEAP; - -/** - * B-frame used as reference modes - */ -typedef enum _NV_ENC_BFRAME_REF_MODE -{ - NV_ENC_BFRAME_REF_MODE_DISABLED = 0x0, /**< B frame is not used for reference */ - NV_ENC_BFRAME_REF_MODE_EACH = 0x1, /**< Each B-frame will be used for reference. currently not supported for H.264 */ - NV_ENC_BFRAME_REF_MODE_MIDDLE = 0x2, /**< Only(Number of B-frame)/2 th B-frame will be used for reference */ -} NV_ENC_BFRAME_REF_MODE; - -/** - * H.264 entropy coding modes. - */ -typedef enum _NV_ENC_H264_ENTROPY_CODING_MODE -{ - NV_ENC_H264_ENTROPY_CODING_MODE_AUTOSELECT = 0x0, /**< Entropy coding mode is auto selected by the encoder driver */ - NV_ENC_H264_ENTROPY_CODING_MODE_CABAC = 0x1, /**< Entropy coding mode is CABAC */ - NV_ENC_H264_ENTROPY_CODING_MODE_CAVLC = 0x2 /**< Entropy coding mode is CAVLC */ -} NV_ENC_H264_ENTROPY_CODING_MODE; - -/** - * H.264 specific BDirect modes - */ -typedef enum _NV_ENC_H264_BDIRECT_MODE -{ - NV_ENC_H264_BDIRECT_MODE_AUTOSELECT = 0x0, /**< BDirect mode is auto selected by the encoder driver */ - NV_ENC_H264_BDIRECT_MODE_DISABLE = 0x1, /**< Disable BDirect mode */ - NV_ENC_H264_BDIRECT_MODE_TEMPORAL = 0x2, /**< Temporal BDirect mode */ - NV_ENC_H264_BDIRECT_MODE_SPATIAL = 0x3 /**< Spatial BDirect mode */ -} NV_ENC_H264_BDIRECT_MODE; - -/** - * H.264 specific FMO usage - */ -typedef enum _NV_ENC_H264_FMO_MODE -{ - NV_ENC_H264_FMO_AUTOSELECT = 0x0, /**< FMO usage is auto selected by the encoder driver */ - NV_ENC_H264_FMO_ENABLE = 0x1, /**< Enable FMO */ - NV_ENC_H264_FMO_DISABLE = 0x2, /**< Disable FMO */ -} NV_ENC_H264_FMO_MODE; - -/** - * H.264 specific Adaptive Transform modes - */ -typedef enum _NV_ENC_H264_ADAPTIVE_TRANSFORM_MODE -{ - NV_ENC_H264_ADAPTIVE_TRANSFORM_AUTOSELECT = 0x0, /**< Adaptive Transform 8x8 mode is auto selected by the encoder driver*/ - NV_ENC_H264_ADAPTIVE_TRANSFORM_DISABLE = 0x1, /**< Adaptive Transform 8x8 mode disabled */ - NV_ENC_H264_ADAPTIVE_TRANSFORM_ENABLE = 0x2, /**< Adaptive Transform 8x8 mode should be used */ -} NV_ENC_H264_ADAPTIVE_TRANSFORM_MODE; - -/** - * Stereo frame packing modes. - */ -typedef enum _NV_ENC_STEREO_PACKING_MODE -{ - NV_ENC_STEREO_PACKING_MODE_NONE = 0x0, /**< No Stereo packing required */ - NV_ENC_STEREO_PACKING_MODE_CHECKERBOARD = 0x1, /**< Checkerboard mode for packing stereo frames */ - NV_ENC_STEREO_PACKING_MODE_COLINTERLEAVE = 0x2, /**< Column Interleave mode for packing stereo frames */ - NV_ENC_STEREO_PACKING_MODE_ROWINTERLEAVE = 0x3, /**< Row Interleave mode for packing stereo frames */ - NV_ENC_STEREO_PACKING_MODE_SIDEBYSIDE = 0x4, /**< Side-by-side mode for packing stereo frames */ - NV_ENC_STEREO_PACKING_MODE_TOPBOTTOM = 0x5, /**< Top-Bottom mode for packing stereo frames */ - NV_ENC_STEREO_PACKING_MODE_FRAMESEQ = 0x6 /**< Frame Sequential mode for packing stereo frames */ -} NV_ENC_STEREO_PACKING_MODE; - -/** - * Input Resource type - */ -typedef enum _NV_ENC_INPUT_RESOURCE_TYPE -{ - NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX = 0x0, /**< input resource type is a directx9 surface*/ - NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR = 0x1, /**< input resource type is a cuda device pointer surface*/ - NV_ENC_INPUT_RESOURCE_TYPE_CUDAARRAY = 0x2, /**< input resource type is a cuda array surface. - This array must be a 2D array and the CUDA_ARRAY3D_SURFACE_LDST - flag must have been specified when creating it. */ - NV_ENC_INPUT_RESOURCE_TYPE_OPENGL_TEX = 0x3 /**< input resource type is an OpenGL texture */ -} NV_ENC_INPUT_RESOURCE_TYPE; - -/** - * Buffer usage - */ -typedef enum _NV_ENC_BUFFER_USAGE -{ - NV_ENC_INPUT_IMAGE = 0x0, /**< Registered surface will be used for input image */ - NV_ENC_OUTPUT_MOTION_VECTOR = 0x1, /**< Registered surface will be used for output of H.264 ME only mode. - This buffer usage type is not supported for HEVC ME only mode. */ - NV_ENC_OUTPUT_BITSTREAM = 0x2 /**< Registered surface will be used for output bitstream in encoding */ -} NV_ENC_BUFFER_USAGE; - -/** - * Encoder Device type - */ -typedef enum _NV_ENC_DEVICE_TYPE -{ - NV_ENC_DEVICE_TYPE_DIRECTX = 0x0, /**< encode device type is a directx9 device */ - NV_ENC_DEVICE_TYPE_CUDA = 0x1, /**< encode device type is a cuda device */ - NV_ENC_DEVICE_TYPE_OPENGL = 0x2 /**< encode device type is an OpenGL device. - Use of this device type is supported only on Linux */ -} NV_ENC_DEVICE_TYPE; - -/** - * Number of reference frames - */ -typedef enum _NV_ENC_NUM_REF_FRAMES -{ - NV_ENC_NUM_REF_FRAMES_AUTOSELECT = 0x0, /**< Number of reference frames is auto selected by the encoder driver */ - NV_ENC_NUM_REF_FRAMES_1 = 0x1, /**< Number of reference frames equal to 1 */ - NV_ENC_NUM_REF_FRAMES_2 = 0x2, /**< Number of reference frames equal to 2 */ - NV_ENC_NUM_REF_FRAMES_3 = 0x3, /**< Number of reference frames equal to 3 */ - NV_ENC_NUM_REF_FRAMES_4 = 0x4, /**< Number of reference frames equal to 4 */ - NV_ENC_NUM_REF_FRAMES_5 = 0x5, /**< Number of reference frames equal to 5 */ - NV_ENC_NUM_REF_FRAMES_6 = 0x6, /**< Number of reference frames equal to 6 */ - NV_ENC_NUM_REF_FRAMES_7 = 0x7 /**< Number of reference frames equal to 7 */ -} NV_ENC_NUM_REF_FRAMES; - -/** - * Encoder capabilities enumeration. - */ -typedef enum _NV_ENC_CAPS -{ - /** - * Maximum number of B-Frames supported. - */ - NV_ENC_CAPS_NUM_MAX_BFRAMES, - - /** - * Rate control modes supported. - * \n The API return value is a bitmask of the values in NV_ENC_PARAMS_RC_MODE. - */ - NV_ENC_CAPS_SUPPORTED_RATECONTROL_MODES, - - /** - * Indicates HW support for field mode encoding. - * \n 0 : Interlaced mode encoding is not supported. - * \n 1 : Interlaced field mode encoding is supported. - * \n 2 : Interlaced frame encoding and field mode encoding are both supported. - */ - NV_ENC_CAPS_SUPPORT_FIELD_ENCODING, - - /** - * Indicates HW support for monochrome mode encoding. - * \n 0 : Monochrome mode not supported. - * \n 1 : Monochrome mode supported. - */ - NV_ENC_CAPS_SUPPORT_MONOCHROME, - - /** - * Indicates HW support for FMO. - * \n 0 : FMO not supported. - * \n 1 : FMO supported. - */ - NV_ENC_CAPS_SUPPORT_FMO, - - /** - * Indicates HW capability for Quarter pel motion estimation. - * \n 0 : Quarter-Pel Motion Estimation not supported. - * \n 1 : Quarter-Pel Motion Estimation supported. - */ - NV_ENC_CAPS_SUPPORT_QPELMV, - - /** - * H.264 specific. Indicates HW support for BDirect modes. - * \n 0 : BDirect mode encoding not supported. - * \n 1 : BDirect mode encoding supported. - */ - NV_ENC_CAPS_SUPPORT_BDIRECT_MODE, - - /** - * H264 specific. Indicates HW support for CABAC entropy coding mode. - * \n 0 : CABAC entropy coding not supported. - * \n 1 : CABAC entropy coding supported. - */ - NV_ENC_CAPS_SUPPORT_CABAC, - - /** - * Indicates HW support for Adaptive Transform. - * \n 0 : Adaptive Transform not supported. - * \n 1 : Adaptive Transform supported. - */ - NV_ENC_CAPS_SUPPORT_ADAPTIVE_TRANSFORM, - - /** - * Indicates HW support for Multi View Coding. - * \n 0 : Multi View Coding not supported. - * \n 1 : Multi View Coding supported. - */ - NV_ENC_CAPS_SUPPORT_STEREO_MVC, - - /** - * Indicates HW support for encoding Temporal layers. - * \n 0 : Encoding Temporal layers not supported. - * \n 1 : Encoding Temporal layers supported. - */ - NV_ENC_CAPS_NUM_MAX_TEMPORAL_LAYERS, - - /** - * Indicates HW support for Hierarchical P frames. - * \n 0 : Hierarchical P frames not supported. - * \n 1 : Hierarchical P frames supported. - */ - NV_ENC_CAPS_SUPPORT_HIERARCHICAL_PFRAMES, - - /** - * Indicates HW support for Hierarchical B frames. - * \n 0 : Hierarchical B frames not supported. - * \n 1 : Hierarchical B frames supported. - */ - NV_ENC_CAPS_SUPPORT_HIERARCHICAL_BFRAMES, - - /** - * Maximum Encoding level supported (See ::NV_ENC_LEVEL for details). - */ - NV_ENC_CAPS_LEVEL_MAX, - - /** - * Minimum Encoding level supported (See ::NV_ENC_LEVEL for details). - */ - NV_ENC_CAPS_LEVEL_MIN, - - /** - * Indicates HW support for separate colour plane encoding. - * \n 0 : Separate colour plane encoding not supported. - * \n 1 : Separate colour plane encoding supported. - */ - NV_ENC_CAPS_SEPARATE_COLOUR_PLANE, - - /** - * Maximum output width supported. - */ - NV_ENC_CAPS_WIDTH_MAX, - - /** - * Maximum output height supported. - */ - NV_ENC_CAPS_HEIGHT_MAX, - - /** - * Indicates Temporal Scalability Support. - * \n 0 : Temporal SVC encoding not supported. - * \n 1 : Temporal SVC encoding supported. - */ - NV_ENC_CAPS_SUPPORT_TEMPORAL_SVC, - - /** - * Indicates Dynamic Encode Resolution Change Support. - * Support added from NvEncodeAPI version 2.0. - * \n 0 : Dynamic Encode Resolution Change not supported. - * \n 1 : Dynamic Encode Resolution Change supported. - */ - NV_ENC_CAPS_SUPPORT_DYN_RES_CHANGE, - - /** - * Indicates Dynamic Encode Bitrate Change Support. - * Support added from NvEncodeAPI version 2.0. - * \n 0 : Dynamic Encode bitrate change not supported. - * \n 1 : Dynamic Encode bitrate change supported. - */ - NV_ENC_CAPS_SUPPORT_DYN_BITRATE_CHANGE, - - /** - * Indicates Forcing Constant QP On The Fly Support. - * Support added from NvEncodeAPI version 2.0. - * \n 0 : Forcing constant QP on the fly not supported. - * \n 1 : Forcing constant QP on the fly supported. - */ - NV_ENC_CAPS_SUPPORT_DYN_FORCE_CONSTQP, - - /** - * Indicates Dynamic rate control mode Change Support. - * \n 0 : Dynamic rate control mode change not supported. - * \n 1 : Dynamic rate control mode change supported. - */ - NV_ENC_CAPS_SUPPORT_DYN_RCMODE_CHANGE, - - /** - * Indicates Subframe readback support for slice-based encoding. If this feature is supported, it can be enabled by setting enableSubFrameWrite = 1. - * \n 0 : Subframe readback not supported. - * \n 1 : Subframe readback supported. - */ - NV_ENC_CAPS_SUPPORT_SUBFRAME_READBACK, - - /** - * Indicates Constrained Encoding mode support. - * Support added from NvEncodeAPI version 2.0. - * \n 0 : Constrained encoding mode not supported. - * \n 1 : Constrained encoding mode supported. - * If this mode is supported client can enable this during initialization. - * Client can then force a picture to be coded as constrained picture where - * in-loop filtering is disabled across slice boundaries and prediction vectors for inter - * macroblocks in each slice will be restricted to the slice region. - */ - NV_ENC_CAPS_SUPPORT_CONSTRAINED_ENCODING, - - /** - * Indicates Intra Refresh Mode Support. - * Support added from NvEncodeAPI version 2.0. - * \n 0 : Intra Refresh Mode not supported. - * \n 1 : Intra Refresh Mode supported. - */ - NV_ENC_CAPS_SUPPORT_INTRA_REFRESH, - - /** - * Indicates Custom VBV Buffer Size support. It can be used for capping frame size. - * Support added from NvEncodeAPI version 2.0. - * \n 0 : Custom VBV buffer size specification from client, not supported. - * \n 1 : Custom VBV buffer size specification from client, supported. - */ - NV_ENC_CAPS_SUPPORT_CUSTOM_VBV_BUF_SIZE, - - /** - * Indicates Dynamic Slice Mode Support. - * Support added from NvEncodeAPI version 2.0. - * \n 0 : Dynamic Slice Mode not supported. - * \n 1 : Dynamic Slice Mode supported. - */ - NV_ENC_CAPS_SUPPORT_DYNAMIC_SLICE_MODE, - - /** - * Indicates Reference Picture Invalidation Support. - * Support added from NvEncodeAPI version 2.0. - * \n 0 : Reference Picture Invalidation not supported. - * \n 1 : Reference Picture Invalidation supported. - */ - NV_ENC_CAPS_SUPPORT_REF_PIC_INVALIDATION, - - /** - * Indicates support for Pre-Processing. - * The API return value is a bitmask of the values defined in ::NV_ENC_PREPROC_FLAGS - */ - NV_ENC_CAPS_PREPROC_SUPPORT, - - /** - * Indicates support Async mode. - * \n 0 : Async Encode mode not supported. - * \n 1 : Async Encode mode supported. - */ - NV_ENC_CAPS_ASYNC_ENCODE_SUPPORT, - - /** - * Maximum MBs per frame supported. - */ - NV_ENC_CAPS_MB_NUM_MAX, - - /** - * Maximum aggregate throughput in MBs per sec. - */ - NV_ENC_CAPS_MB_PER_SEC_MAX, - - /** - * Indicates HW support for YUV444 mode encoding. - * \n 0 : YUV444 mode encoding not supported. - * \n 1 : YUV444 mode encoding supported. - */ - NV_ENC_CAPS_SUPPORT_YUV444_ENCODE, - - /** - * Indicates HW support for lossless encoding. - * \n 0 : lossless encoding not supported. - * \n 1 : lossless encoding supported. - */ - NV_ENC_CAPS_SUPPORT_LOSSLESS_ENCODE, - - /** - * Indicates HW support for Sample Adaptive Offset. - * \n 0 : SAO not supported. - * \n 1 : SAO encoding supported. - */ - NV_ENC_CAPS_SUPPORT_SAO, - - /** - * Indicates HW support for Motion Estimation Only Mode. - * \n 0 : MEOnly Mode not supported. - * \n 1 : MEOnly Mode supported for I and P frames. - * \n 2 : MEOnly Mode supported for I, P and B frames. - */ - NV_ENC_CAPS_SUPPORT_MEONLY_MODE, - - /** - * Indicates HW support for lookahead encoding (enableLookahead=1). - * \n 0 : Lookahead not supported. - * \n 1 : Lookahead supported. - */ - NV_ENC_CAPS_SUPPORT_LOOKAHEAD, - - /** - * Indicates HW support for temporal AQ encoding (enableTemporalAQ=1). - * \n 0 : Temporal AQ not supported. - * \n 1 : Temporal AQ supported. - */ - NV_ENC_CAPS_SUPPORT_TEMPORAL_AQ, - /** - * Indicates HW support for 10 bit encoding. - * \n 0 : 10 bit encoding not supported. - * \n 1 : 10 bit encoding supported. - */ - NV_ENC_CAPS_SUPPORT_10BIT_ENCODE, - /** - * Maximum number of Long Term Reference frames supported - */ - NV_ENC_CAPS_NUM_MAX_LTR_FRAMES, - - /** - * Indicates HW support for Weighted Prediction. - * \n 0 : Weighted Prediction not supported. - * \n 1 : Weighted Prediction supported. - */ - NV_ENC_CAPS_SUPPORT_WEIGHTED_PREDICTION, - - - /** - * On managed (vGPU) platforms (Windows only), this API, in conjunction with other GRID Management APIs, can be used - * to estimate the residual capacity of the hardware encoder on the GPU as a percentage of the total available encoder capacity. - * This API can be called at any time; i.e. during the encode session or before opening the encode session. - * If the available encoder capacity is returned as zero, applications may choose to switch to software encoding - * and continue to call this API (e.g. polling once per second) until capacity becomes available. - * - * On bare metal (non-virtualized GPU) and linux platforms, this API always returns 100. - */ - NV_ENC_CAPS_DYNAMIC_QUERY_ENCODER_CAPACITY, - - /** - * Indicates B as reference support. - * \n 0 : B as reference is not supported. - * \n 1 : each B-Frame as reference is supported. - * \n 2 : only Middle B-frame as reference is supported. - */ - NV_ENC_CAPS_SUPPORT_BFRAME_REF_MODE, - - /** - * Indicates HW support for Emphasis Level Map based delta QP computation. - * \n 0 : Emphasis Level Map based delta QP not supported. - * \n 1 : Emphasis Level Map based delta QP is supported. - */ - NV_ENC_CAPS_SUPPORT_EMPHASIS_LEVEL_MAP, - - /** - * Minimum input width supported. - */ - NV_ENC_CAPS_WIDTH_MIN, - - /** - * Minimum input height supported. - */ - NV_ENC_CAPS_HEIGHT_MIN, - - /** - * Indicates HW support for multiple reference frames. - */ - NV_ENC_CAPS_SUPPORT_MULTIPLE_REF_FRAMES, - - /** - * Reserved - Not to be used by clients. - */ - NV_ENC_CAPS_EXPOSED_COUNT -} NV_ENC_CAPS; - -/** - * HEVC CU SIZE - */ -typedef enum _NV_ENC_HEVC_CUSIZE -{ - NV_ENC_HEVC_CUSIZE_AUTOSELECT = 0, - NV_ENC_HEVC_CUSIZE_8x8 = 1, - NV_ENC_HEVC_CUSIZE_16x16 = 2, - NV_ENC_HEVC_CUSIZE_32x32 = 3, - NV_ENC_HEVC_CUSIZE_64x64 = 4, -}NV_ENC_HEVC_CUSIZE; - -/** - * Input struct for querying Encoding capabilities. - */ -typedef struct _NV_ENC_CAPS_PARAM -{ - uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_CAPS_PARAM_VER */ - NV_ENC_CAPS capsToQuery; /**< [in]: Specifies the encode capability to be queried. Client should pass a member for ::NV_ENC_CAPS enum. */ - uint32_t reserved[62]; /**< [in]: Reserved and must be set to 0 */ -} NV_ENC_CAPS_PARAM; - -/** NV_ENC_CAPS_PARAM struct version. */ -#define NV_ENC_CAPS_PARAM_VER NVENCAPI_STRUCT_VERSION(1) - - -/** - * Encoder Output parameters - */ -typedef struct _NV_ENC_ENCODE_OUT_PARAMS -{ - uint32_t version; /**< [out]: Struct version. */ - uint32_t bitstreamSizeInBytes; /**< [out]: Encoded bitstream size in bytes */ - uint32_t reserved[62]; /**< [out]: Reserved and must be set to 0 */ -} NV_ENC_ENCODE_OUT_PARAMS; - -/** NV_ENC_ENCODE_OUT_PARAMS struct version. */ -#define NV_ENC_ENCODE_OUT_PARAMS_VER NVENCAPI_STRUCT_VERSION(1) - -/** - * Creation parameters for input buffer. - */ -typedef struct _NV_ENC_CREATE_INPUT_BUFFER -{ - uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_CREATE_INPUT_BUFFER_VER */ - uint32_t width; /**< [in]: Input frame width */ - uint32_t height; /**< [in]: Input frame height */ - NV_ENC_MEMORY_HEAP memoryHeap; /**< [in]: Deprecated. Do not use */ - NV_ENC_BUFFER_FORMAT bufferFmt; /**< [in]: Input buffer format */ - uint32_t reserved; /**< [in]: Reserved and must be set to 0 */ - NV_ENC_INPUT_PTR inputBuffer; /**< [out]: Pointer to input buffer */ - void* pSysMemBuffer; /**< [in]: Pointer to existing system memory buffer */ - uint32_t reserved1[57]; /**< [in]: Reserved and must be set to 0 */ - void* reserved2[63]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENC_CREATE_INPUT_BUFFER; - -/** NV_ENC_CREATE_INPUT_BUFFER struct version. */ -#define NV_ENC_CREATE_INPUT_BUFFER_VER NVENCAPI_STRUCT_VERSION(1) - -/** - * Creation parameters for output bitstream buffer. - */ -typedef struct _NV_ENC_CREATE_BITSTREAM_BUFFER -{ - uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_CREATE_BITSTREAM_BUFFER_VER */ - uint32_t size; /**< [in]: Deprecated. Do not use */ - NV_ENC_MEMORY_HEAP memoryHeap; /**< [in]: Deprecated. Do not use */ - uint32_t reserved; /**< [in]: Reserved and must be set to 0 */ - NV_ENC_OUTPUT_PTR bitstreamBuffer; /**< [out]: Pointer to the output bitstream buffer */ - void* bitstreamBufferPtr; /**< [out]: Reserved and should not be used */ - uint32_t reserved1[58]; /**< [in]: Reserved and should be set to 0 */ - void* reserved2[64]; /**< [in]: Reserved and should be set to NULL */ -} NV_ENC_CREATE_BITSTREAM_BUFFER; - -/** NV_ENC_CREATE_BITSTREAM_BUFFER struct version. */ -#define NV_ENC_CREATE_BITSTREAM_BUFFER_VER NVENCAPI_STRUCT_VERSION(1) - -/** - * Structs needed for ME only mode. - */ -typedef struct _NV_ENC_MVECTOR -{ - int16_t mvx; /**< the x component of MV in quarter-pel units */ - int16_t mvy; /**< the y component of MV in quarter-pel units */ -} NV_ENC_MVECTOR; - -/** - * Motion vector structure per macroblock for H264 motion estimation. - */ -typedef struct _NV_ENC_H264_MV_DATA -{ - NV_ENC_MVECTOR mv[4]; /**< up to 4 vectors for 8x8 partition */ - uint8_t mbType; /**< 0 (I), 1 (P), 2 (IPCM), 3 (B) */ - uint8_t partitionType; /**< Specifies the block partition type. 0:16x16, 1:8x8, 2:16x8, 3:8x16 */ - uint16_t reserved; /**< reserved padding for alignment */ - uint32_t mbCost; -} NV_ENC_H264_MV_DATA; - -/** - * Motion vector structure per CU for HEVC motion estimation. - */ -typedef struct _NV_ENC_HEVC_MV_DATA -{ - NV_ENC_MVECTOR mv[4]; /**< up to 4 vectors within a CU */ - uint8_t cuType; /**< 0 (I), 1(P) */ - uint8_t cuSize; /**< 0: 8x8, 1: 16x16, 2: 32x32, 3: 64x64 */ - uint8_t partitionMode; /**< The CU partition mode - 0 (2Nx2N), 1 (2NxN), 2(Nx2N), 3 (NxN), - 4 (2NxnU), 5 (2NxnD), 6(nLx2N), 7 (nRx2N) */ - uint8_t lastCUInCTB; /**< Marker to separate CUs in the current CTB from CUs in the next CTB */ -} NV_ENC_HEVC_MV_DATA; - -/** - * Creation parameters for output motion vector buffer for ME only mode. - */ -typedef struct _NV_ENC_CREATE_MV_BUFFER -{ - uint32_t version; /**< [in]: Struct version. Must be set to NV_ENC_CREATE_MV_BUFFER_VER */ - NV_ENC_OUTPUT_PTR mvBuffer; /**< [out]: Pointer to the output motion vector buffer */ - uint32_t reserved1[255]; /**< [in]: Reserved and should be set to 0 */ - void* reserved2[63]; /**< [in]: Reserved and should be set to NULL */ -} NV_ENC_CREATE_MV_BUFFER; - -/** NV_ENC_CREATE_MV_BUFFER struct version*/ -#define NV_ENC_CREATE_MV_BUFFER_VER NVENCAPI_STRUCT_VERSION(1) - -/** - * QP value for frames - */ -typedef struct _NV_ENC_QP -{ - uint32_t qpInterP; /**< [in]: Specifies QP value for P-frame. Even though this field is uint32_t for legacy reasons, the client should treat this as a signed parameter(int32_t) for cases in which negative QP values are to be specified. */ - uint32_t qpInterB; /**< [in]: Specifies QP value for B-frame. Even though this field is uint32_t for legacy reasons, the client should treat this as a signed parameter(int32_t) for cases in which negative QP values are to be specified. */ - uint32_t qpIntra; /**< [in]: Specifies QP value for Intra Frame. Even though this field is uint32_t for legacy reasons, the client should treat this as a signed parameter(int32_t) for cases in which negative QP values are to be specified. */ -} NV_ENC_QP; - -/** - * Rate Control Configuration Parameters - */ - typedef struct _NV_ENC_RC_PARAMS - { - uint32_t version; - NV_ENC_PARAMS_RC_MODE rateControlMode; /**< [in]: Specifies the rate control mode. Check support for various rate control modes using ::NV_ENC_CAPS_SUPPORTED_RATECONTROL_MODES caps. */ - NV_ENC_QP constQP; /**< [in]: Specifies the initial QP to be used for encoding, these values would be used for all frames if in Constant QP mode. */ - uint32_t averageBitRate; /**< [in]: Specifies the average bitrate(in bits/sec) used for encoding. */ - uint32_t maxBitRate; /**< [in]: Specifies the maximum bitrate for the encoded output. This is used for VBR and ignored for CBR mode. */ - uint32_t vbvBufferSize; /**< [in]: Specifies the VBV(HRD) buffer size. in bits. Set 0 to use the default VBV buffer size. */ - uint32_t vbvInitialDelay; /**< [in]: Specifies the VBV(HRD) initial delay in bits. Set 0 to use the default VBV initial delay .*/ - uint32_t enableMinQP :1; /**< [in]: Set this to 1 if minimum QP used for rate control. */ - uint32_t enableMaxQP :1; /**< [in]: Set this to 1 if maximum QP used for rate control. */ - uint32_t enableInitialRCQP :1; /**< [in]: Set this to 1 if user supplied initial QP is used for rate control. */ - uint32_t enableAQ :1; /**< [in]: Set this to 1 to enable adaptive quantization (Spatial). */ - uint32_t reservedBitField1 :1; /**< [in]: Reserved bitfields and must be set to 0. */ - uint32_t enableLookahead :1; /**< [in]: Set this to 1 to enable lookahead with depth (if lookahead is enabled, input frames must remain available to the encoder until encode completion) */ - uint32_t disableIadapt :1; /**< [in]: Set this to 1 to disable adaptive I-frame insertion at scene cuts (only has an effect when lookahead is enabled) */ - uint32_t disableBadapt :1; /**< [in]: Set this to 1 to disable adaptive B-frame decision (only has an effect when lookahead is enabled) */ - uint32_t enableTemporalAQ :1; /**< [in]: Set this to 1 to enable temporal AQ */ - uint32_t zeroReorderDelay :1; /**< [in]: Set this to 1 to indicate zero latency operation (no reordering delay, num_reorder_frames=0) */ - uint32_t enableNonRefP :1; /**< [in]: Set this to 1 to enable automatic insertion of non-reference P-frames (no effect if enablePTD=0) */ - uint32_t strictGOPTarget :1; /**< [in]: Set this to 1 to minimize GOP-to-GOP rate fluctuations */ - uint32_t aqStrength :4; /**< [in]: When AQ (Spatial) is enabled (i.e. NV_ENC_RC_PARAMS::enableAQ is set), this field is used to specify AQ strength. AQ strength scale is from 1 (low) - 15 (aggressive). - If not set, strength is auto selected by driver. */ - uint32_t reservedBitFields :16; /**< [in]: Reserved bitfields and must be set to 0 */ - NV_ENC_QP minQP; /**< [in]: Specifies the minimum QP used for rate control. Client must set NV_ENC_CONFIG::enableMinQP to 1. */ - NV_ENC_QP maxQP; /**< [in]: Specifies the maximum QP used for rate control. Client must set NV_ENC_CONFIG::enableMaxQP to 1. */ - NV_ENC_QP initialRCQP; /**< [in]: Specifies the initial QP used for rate control. Client must set NV_ENC_CONFIG::enableInitialRCQP to 1. */ - uint32_t temporallayerIdxMask; /**< [in]: Specifies the temporal layers (as a bitmask) whose QPs have changed. Valid max bitmask is [2^NV_ENC_CAPS_NUM_MAX_TEMPORAL_LAYERS - 1] */ - uint8_t temporalLayerQP[8]; /**< [in]: Specifies the temporal layer QPs used for rate control. Temporal layer index is used as the array index */ - uint8_t targetQuality; /**< [in]: Target CQ (Constant Quality) level for VBR mode (range 0-51 with 0-automatic) */ - uint8_t targetQualityLSB; /**< [in]: Fractional part of target quality (as 8.8 fixed point format) */ - uint16_t lookaheadDepth; /**< [in]: Maximum depth of lookahead with range 0-(31 - number of B frames). - lookaheadDepth is only used if enableLookahead=1.*/ - uint8_t lowDelayKeyFrameScale; /**< [in]: Specifies the ratio of I frame bits to P frame bits in case of single frame VBV and CBR rate control mode, - is set to 2 by default for low latency tuning info and 1 by default for ultra low latency tuning info */ - uint8_t reserved1[3]; - NV_ENC_QP_MAP_MODE qpMapMode; /**< [in]: This flag is used to interpret values in array specified by NV_ENC_PIC_PARAMS::qpDeltaMap. - Set this to NV_ENC_QP_MAP_EMPHASIS to treat values specified by NV_ENC_PIC_PARAMS::qpDeltaMap as Emphasis Level Map. - Emphasis Level can be assigned any value specified in enum NV_ENC_EMPHASIS_MAP_LEVEL. - Emphasis Level Map is used to specify regions to be encoded at varying levels of quality. - The hardware encoder adjusts the quantization within the image as per the provided emphasis map, - by adjusting the quantization parameter (QP) assigned to each macroblock. This adjustment is commonly called “Delta QP”. - The adjustment depends on the absolute QP decided by the rate control algorithm, and is applied after the rate control has decided each macroblock’s QP. - Since the Delta QP overrides rate control, enabling Emphasis Level Map may violate bitrate and VBV buffer size constraints. - Emphasis Level Map is useful in situations where client has a priori knowledge of the image complexity (e.g. via use of NVFBC's Classification feature) and encoding those high-complexity areas at higher quality (lower QP) is important, even at the possible cost of violating bitrate/VBV buffer size constraints - This feature is not supported when AQ( Spatial/Temporal) is enabled. - This feature is only supported for H264 codec currently. - - Set this to NV_ENC_QP_MAP_DELTA to treat values specified by NV_ENC_PIC_PARAMS::qpDeltaMap as QP Delta. This specifies QP modifier to be applied on top of the QP chosen by rate control - - Set this to NV_ENC_QP_MAP_DISABLED to ignore NV_ENC_PIC_PARAMS::qpDeltaMap values. In this case, qpDeltaMap should be set to NULL. - - Other values are reserved for future use.*/ - NV_ENC_MULTI_PASS multiPass; /**< [in]: This flag is used to enable multi-pass encoding for a given ::NV_ENC_PARAMS_RC_MODE. This flag is not valid for H264 and HEVC MEOnly mode */ - uint32_t reserved[6]; - } NV_ENC_RC_PARAMS; - -/** macro for constructing the version field of ::_NV_ENC_RC_PARAMS */ -#define NV_ENC_RC_PARAMS_VER NVENCAPI_STRUCT_VERSION(1) - - - -/** - * \struct _NV_ENC_CONFIG_H264_VUI_PARAMETERS - * H264 Video Usability Info parameters - */ -typedef struct _NV_ENC_CONFIG_H264_VUI_PARAMETERS -{ - uint32_t overscanInfoPresentFlag; /**< [in]: if set to 1 , it specifies that the overscanInfo is present */ - uint32_t overscanInfo; /**< [in]: Specifies the overscan info(as defined in Annex E of the ITU-T Specification). */ - uint32_t videoSignalTypePresentFlag; /**< [in]: If set to 1, it specifies that the videoFormat, videoFullRangeFlag and colourDescriptionPresentFlag are present. */ - uint32_t videoFormat; /**< [in]: Specifies the source video format(as defined in Annex E of the ITU-T Specification).*/ - uint32_t videoFullRangeFlag; /**< [in]: Specifies the output range of the luma and chroma samples(as defined in Annex E of the ITU-T Specification). */ - uint32_t colourDescriptionPresentFlag; /**< [in]: If set to 1, it specifies that the colourPrimaries, transferCharacteristics and colourMatrix are present. */ - uint32_t colourPrimaries; /**< [in]: Specifies color primaries for converting to RGB(as defined in Annex E of the ITU-T Specification) */ - uint32_t transferCharacteristics; /**< [in]: Specifies the opto-electronic transfer characteristics to use (as defined in Annex E of the ITU-T Specification) */ - uint32_t colourMatrix; /**< [in]: Specifies the matrix coefficients used in deriving the luma and chroma from the RGB primaries (as defined in Annex E of the ITU-T Specification). */ - uint32_t chromaSampleLocationFlag; /**< [in]: if set to 1 , it specifies that the chromaSampleLocationTop and chromaSampleLocationBot are present.*/ - uint32_t chromaSampleLocationTop; /**< [in]: Specifies the chroma sample location for top field(as defined in Annex E of the ITU-T Specification) */ - uint32_t chromaSampleLocationBot; /**< [in]: Specifies the chroma sample location for bottom field(as defined in Annex E of the ITU-T Specification) */ - uint32_t bitstreamRestrictionFlag; /**< [in]: if set to 1, it specifies the bitstream restriction parameters are present in the bitstream.*/ - uint32_t reserved[15]; -}NV_ENC_CONFIG_H264_VUI_PARAMETERS; - -typedef NV_ENC_CONFIG_H264_VUI_PARAMETERS NV_ENC_CONFIG_HEVC_VUI_PARAMETERS; - -/** - * \struct _NVENC_EXTERNAL_ME_HINT_COUNTS_PER_BLOCKTYPE - * External motion vector hint counts per block type. - * H264 supports multiple hint while HEVC supports one hint for each valid candidate. - */ -typedef struct _NVENC_EXTERNAL_ME_HINT_COUNTS_PER_BLOCKTYPE -{ - uint32_t numCandsPerBlk16x16 : 4; /**< [in]: Supported for H264, HEVC. It Specifies the number of candidates per 16x16 block. */ - uint32_t numCandsPerBlk16x8 : 4; /**< [in]: Supported for H264 only. Specifies the number of candidates per 16x8 block. */ - uint32_t numCandsPerBlk8x16 : 4; /**< [in]: Supported for H264 only. Specifies the number of candidates per 8x16 block. */ - uint32_t numCandsPerBlk8x8 : 4; /**< [in]: Supported for H264, HEVC. Specifies the number of candidates per 8x8 block. */ - uint32_t reserved : 16; /**< [in]: Reserved for padding. */ - uint32_t reserved1[3]; /**< [in]: Reserved for future use. */ -} NVENC_EXTERNAL_ME_HINT_COUNTS_PER_BLOCKTYPE; - - -/** - * \struct _NVENC_EXTERNAL_ME_HINT - * External Motion Vector hint structure. - */ -typedef struct _NVENC_EXTERNAL_ME_HINT -{ - int32_t mvx : 12; /**< [in]: Specifies the x component of integer pixel MV (relative to current MB) S12.0. */ - int32_t mvy : 10; /**< [in]: Specifies the y component of integer pixel MV (relative to current MB) S10.0 .*/ - int32_t refidx : 5; /**< [in]: Specifies the reference index (31=invalid). Current we support only 1 reference frame per direction for external hints, so \p refidx must be 0. */ - int32_t dir : 1; /**< [in]: Specifies the direction of motion estimation . 0=L0 1=L1.*/ - int32_t partType : 2; /**< [in]: Specifies the block partition type.0=16x16 1=16x8 2=8x16 3=8x8 (blocks in partition must be consecutive).*/ - int32_t lastofPart : 1; /**< [in]: Set to 1 for the last MV of (sub) partition */ - int32_t lastOfMB : 1; /**< [in]: Set to 1 for the last MV of macroblock. */ -} NVENC_EXTERNAL_ME_HINT; - - -/** - * \struct _NV_ENC_CONFIG_H264 - * H264 encoder configuration parameters - */ -typedef struct _NV_ENC_CONFIG_H264 -{ - uint32_t reserved :1; /**< [in]: Reserved and must be set to 0 */ - uint32_t enableStereoMVC :1; /**< [in]: Set to 1 to enable stereo MVC*/ - uint32_t hierarchicalPFrames :1; /**< [in]: Set to 1 to enable hierarchical P Frames */ - uint32_t hierarchicalBFrames :1; /**< [in]: Set to 1 to enable hierarchical B Frames */ - uint32_t outputBufferingPeriodSEI :1; /**< [in]: Set to 1 to write SEI buffering period syntax in the bitstream */ - uint32_t outputPictureTimingSEI :1; /**< [in]: Set to 1 to write SEI picture timing syntax in the bitstream. When set for following rateControlMode : NV_ENC_PARAMS_RC_CBR, NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ, - NV_ENC_PARAMS_RC_CBR_HQ, filler data is inserted if needed to achieve HRD bitrate */ - uint32_t outputAUD :1; /**< [in]: Set to 1 to write access unit delimiter syntax in bitstream */ - uint32_t disableSPSPPS :1; /**< [in]: Set to 1 to disable writing of Sequence and Picture parameter info in bitstream */ - uint32_t outputFramePackingSEI :1; /**< [in]: Set to 1 to enable writing of frame packing arrangement SEI messages to bitstream */ - uint32_t outputRecoveryPointSEI :1; /**< [in]: Set to 1 to enable writing of recovery point SEI message */ - uint32_t enableIntraRefresh :1; /**< [in]: Set to 1 to enable gradual decoder refresh or intra refresh. If the GOP structure uses B frames this will be ignored */ - uint32_t enableConstrainedEncoding :1; /**< [in]: Set this to 1 to enable constrainedFrame encoding where each slice in the constrained picture is independent of other slices. - Constrained encoding works only with rectangular slices. - Check support for constrained encoding using ::NV_ENC_CAPS_SUPPORT_CONSTRAINED_ENCODING caps. */ - uint32_t repeatSPSPPS :1; /**< [in]: Set to 1 to enable writing of Sequence and Picture parameter for every IDR frame */ - uint32_t enableVFR :1; /**< [in]: Setting enableVFR=1 currently only sets the fixed_frame_rate_flag=0 in the VUI but otherwise - has no impact on the encoder behavior. For more details please refer to E.1 VUI syntax of H.264 standard. Note, however, that NVENC does not support VFR encoding and rate control. */ - uint32_t enableLTR :1; /**< [in]: Set to 1 to enable LTR (Long Term Reference) frame support. LTR can be used in two modes: "LTR Trust" mode and "LTR Per Picture" mode. - LTR Trust mode: In this mode, ltrNumFrames pictures after IDR are automatically marked as LTR. This mode is enabled by setting ltrTrustMode = 1. - Use of LTR Trust mode is strongly discouraged as this mode may be deprecated in future. - LTR Per Picture mode: In this mode, client can control whether the current picture should be marked as LTR. Enable this mode by setting - ltrTrustMode = 0 and ltrMarkFrame = 1 for the picture to be marked as LTR. This is the preferred mode - for using LTR. - Note that LTRs are not supported if encoding session is configured with B-frames */ - uint32_t qpPrimeYZeroTransformBypassFlag :1; /**< [in]: To enable lossless encode set this to 1, set QP to 0 and RC_mode to NV_ENC_PARAMS_RC_CONSTQP and profile to HIGH_444_PREDICTIVE_PROFILE. - Check support for lossless encoding using ::NV_ENC_CAPS_SUPPORT_LOSSLESS_ENCODE caps. */ - uint32_t useConstrainedIntraPred :1; /**< [in]: Set 1 to enable constrained intra prediction. */ - uint32_t enableFillerDataInsertion :1; /**< [in]: Set to 1 to enable insertion of filler data in the bitstream. - This flag will take effect only when one of the CBR rate - control modes (NV_ENC_PARAMS_RC_CBR, NV_ENC_PARAMS_RC_CBR_HQ, - NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ) is in use and both - NV_ENC_INITIALIZE_PARAMS::frameRateNum and - NV_ENC_INITIALIZE_PARAMS::frameRateDen are set to non-zero - values. Setting this field when - NV_ENC_INITIALIZE_PARAMS::enableOutputInVidmem is also set - is currently not supported and will make ::NvEncInitializeEncoder() - return an error. */ - uint32_t reservedBitFields :14; /**< [in]: Reserved bitfields and must be set to 0 */ - uint32_t level; /**< [in]: Specifies the encoding level. Client is recommended to set this to NV_ENC_LEVEL_AUTOSELECT in order to enable the NvEncodeAPI interface to select the correct level. */ - uint32_t idrPeriod; /**< [in]: Specifies the IDR interval. If not set, this is made equal to gopLength in NV_ENC_CONFIG.Low latency application client can set IDR interval to NVENC_INFINITE_GOPLENGTH so that IDR frames are not inserted automatically. */ - uint32_t separateColourPlaneFlag; /**< [in]: Set to 1 to enable 4:4:4 separate colour planes */ - uint32_t disableDeblockingFilterIDC; /**< [in]: Specifies the deblocking filter mode. Permissible value range: [0,2]. This flag corresponds - to the flag disable_deblocking_filter_idc specified in section 7.4.3 of H.264 specification, - which specifies whether the operation of the deblocking filter shall be disabled across some - block edges of the slice and specifies for which edges the filtering is disabled. See section - 7.4.3 of H.264 specification for more details.*/ - uint32_t numTemporalLayers; /**< [in]: Specifies max temporal layers to be used for hierarchical coding. Valid value range is [1,::NV_ENC_CAPS_NUM_MAX_TEMPORAL_LAYERS] */ - uint32_t spsId; /**< [in]: Specifies the SPS id of the sequence header */ - uint32_t ppsId; /**< [in]: Specifies the PPS id of the picture header */ - NV_ENC_H264_ADAPTIVE_TRANSFORM_MODE adaptiveTransformMode; /**< [in]: Specifies the AdaptiveTransform Mode. Check support for AdaptiveTransform mode using ::NV_ENC_CAPS_SUPPORT_ADAPTIVE_TRANSFORM caps. */ - NV_ENC_H264_FMO_MODE fmoMode; /**< [in]: Specified the FMO Mode. Check support for FMO using ::NV_ENC_CAPS_SUPPORT_FMO caps. */ - NV_ENC_H264_BDIRECT_MODE bdirectMode; /**< [in]: Specifies the BDirect mode. Check support for BDirect mode using ::NV_ENC_CAPS_SUPPORT_BDIRECT_MODE caps.*/ - NV_ENC_H264_ENTROPY_CODING_MODE entropyCodingMode; /**< [in]: Specifies the entropy coding mode. Check support for CABAC mode using ::NV_ENC_CAPS_SUPPORT_CABAC caps. */ - NV_ENC_STEREO_PACKING_MODE stereoMode; /**< [in]: Specifies the stereo frame packing mode which is to be signaled in frame packing arrangement SEI */ - uint32_t intraRefreshPeriod; /**< [in]: Specifies the interval between successive intra refresh if enableIntrarefresh is set. Requires enableIntraRefresh to be set. - Will be disabled if NV_ENC_CONFIG::gopLength is not set to NVENC_INFINITE_GOPLENGTH. */ - uint32_t intraRefreshCnt; /**< [in]: Specifies the length of intra refresh in number of frames for periodic intra refresh. This value should be smaller than intraRefreshPeriod */ - uint32_t maxNumRefFrames; /**< [in]: Specifies the DPB size used for encoding. Setting it to 0 will let driver use the default DPB size. - The low latency application which wants to invalidate reference frame as an error resilience tool - is recommended to use a large DPB size so that the encoder can keep old reference frames which can be used if recent - frames are invalidated. */ - uint32_t sliceMode; /**< [in]: This parameter in conjunction with sliceModeData specifies the way in which the picture is divided into slices - sliceMode = 0 MB based slices, sliceMode = 1 Byte based slices, sliceMode = 2 MB row based slices, sliceMode = 3 numSlices in Picture. - When forceIntraRefreshWithFrameCnt is set it will have priority over sliceMode setting - When sliceMode == 0 and sliceModeData == 0 whole picture will be coded with one slice */ - uint32_t sliceModeData; /**< [in]: Specifies the parameter needed for sliceMode. For: - sliceMode = 0, sliceModeData specifies # of MBs in each slice (except last slice) - sliceMode = 1, sliceModeData specifies maximum # of bytes in each slice (except last slice) - sliceMode = 2, sliceModeData specifies # of MB rows in each slice (except last slice) - sliceMode = 3, sliceModeData specifies number of slices in the picture. Driver will divide picture into slices optimally */ - NV_ENC_CONFIG_H264_VUI_PARAMETERS h264VUIParameters; /**< [in]: Specifies the H264 video usability info parameters */ - uint32_t ltrNumFrames; /**< [in]: Specifies the number of LTR frames. This parameter has different meaning in two LTR modes. - In "LTR Trust" mode (ltrTrustMode = 1), encoder will mark the first ltrNumFrames base layer reference frames within each IDR interval as LTR. - In "LTR Per Picture" mode (ltrTrustMode = 0 and ltrMarkFrame = 1), ltrNumFrames specifies maximum number of LTR frames in DPB. */ - uint32_t ltrTrustMode; /**< [in]: Specifies the LTR operating mode. See comments near NV_ENC_CONFIG_H264::enableLTR for description of the two modes. - Set to 1 to use "LTR Trust" mode of LTR operation. Clients are discouraged to use "LTR Trust" mode as this mode may - be deprecated in future releases. - Set to 0 when using "LTR Per Picture" mode of LTR operation. */ - uint32_t chromaFormatIDC; /**< [in]: Specifies the chroma format. Should be set to 1 for yuv420 input, 3 for yuv444 input. - Check support for YUV444 encoding using ::NV_ENC_CAPS_SUPPORT_YUV444_ENCODE caps.*/ - uint32_t maxTemporalLayers; /**< [in]: Specifies the max temporal layer used for hierarchical coding. */ - NV_ENC_BFRAME_REF_MODE useBFramesAsRef; /**< [in]: Specifies the B-Frame as reference mode. Check support for useBFramesAsRef mode using ::NV_ENC_CAPS_SUPPORT_BFRAME_REF_MODE caps.*/ - NV_ENC_NUM_REF_FRAMES numRefL0; /**< [in]: Specifies max number of reference frames in reference picture list L0, that can be used by hardware for prediction of a frame. - Check support for numRefL0 using ::NV_ENC_CAPS_SUPPORT_MULTIPLE_REF_FRAMES caps. */ - NV_ENC_NUM_REF_FRAMES numRefL1; /**< [in]: Specifies max number of reference frames in reference picture list L1, that can be used by hardware for prediction of a frame. - Check support for numRefL1 using ::NV_ENC_CAPS_SUPPORT_MULTIPLE_REF_FRAMES caps. */ - uint32_t reserved1[267]; /**< [in]: Reserved and must be set to 0 */ - void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENC_CONFIG_H264; - -/** - * \struct _NV_ENC_CONFIG_HEVC - * HEVC encoder configuration parameters to be set during initialization. - */ -typedef struct _NV_ENC_CONFIG_HEVC -{ - uint32_t level; /**< [in]: Specifies the level of the encoded bitstream.*/ - uint32_t tier; /**< [in]: Specifies the level tier of the encoded bitstream.*/ - NV_ENC_HEVC_CUSIZE minCUSize; /**< [in]: Specifies the minimum size of luma coding unit.*/ - NV_ENC_HEVC_CUSIZE maxCUSize; /**< [in]: Specifies the maximum size of luma coding unit. Currently NVENC SDK only supports maxCUSize equal to NV_ENC_HEVC_CUSIZE_32x32.*/ - uint32_t useConstrainedIntraPred :1; /**< [in]: Set 1 to enable constrained intra prediction. */ - uint32_t disableDeblockAcrossSliceBoundary :1; /**< [in]: Set 1 to disable in loop filtering across slice boundary.*/ - uint32_t outputBufferingPeriodSEI :1; /**< [in]: Set 1 to write SEI buffering period syntax in the bitstream */ - uint32_t outputPictureTimingSEI :1; /**< [in]: Set 1 to write SEI picture timing syntax in the bitstream */ - uint32_t outputAUD :1; /**< [in]: Set 1 to write Access Unit Delimiter syntax. */ - uint32_t enableLTR :1; /**< [in]: Set to 1 to enable LTR (Long Term Reference) frame support. LTR can be used in two modes: "LTR Trust" mode and "LTR Per Picture" mode. - LTR Trust mode: In this mode, ltrNumFrames pictures after IDR are automatically marked as LTR. This mode is enabled by setting ltrTrustMode = 1. - Use of LTR Trust mode is strongly discouraged as this mode may be deprecated in future releases. - LTR Per Picture mode: In this mode, client can control whether the current picture should be marked as LTR. Enable this mode by setting - ltrTrustMode = 0 and ltrMarkFrame = 1 for the picture to be marked as LTR. This is the preferred mode - for using LTR. - Note that LTRs are not supported if encoding session is configured with B-frames */ - uint32_t disableSPSPPS :1; /**< [in]: Set 1 to disable VPS, SPS and PPS signaling in the bitstream. */ - uint32_t repeatSPSPPS :1; /**< [in]: Set 1 to output VPS,SPS and PPS for every IDR frame.*/ - uint32_t enableIntraRefresh :1; /**< [in]: Set 1 to enable gradual decoder refresh or intra refresh. If the GOP structure uses B frames this will be ignored */ - uint32_t chromaFormatIDC :2; /**< [in]: Specifies the chroma format. Should be set to 1 for yuv420 input, 3 for yuv444 input.*/ - uint32_t pixelBitDepthMinus8 :3; /**< [in]: Specifies pixel bit depth minus 8. Should be set to 0 for 8 bit input, 2 for 10 bit input.*/ - uint32_t enableFillerDataInsertion :1; /**< [in]: Set to 1 to enable insertion of filler data in the bitstream. - This flag will take effect only when one of the CBR rate - control modes (NV_ENC_PARAMS_RC_CBR, NV_ENC_PARAMS_RC_CBR_HQ, - NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ) is in use and both - NV_ENC_INITIALIZE_PARAMS::frameRateNum and - NV_ENC_INITIALIZE_PARAMS::frameRateDen are set to non-zero - values. Setting this field when - NV_ENC_INITIALIZE_PARAMS::enableOutputInVidmem is also set - is currently not supported and will make ::NvEncInitializeEncoder() - return an error. */ - uint32_t enableConstrainedEncoding :1; /**< [in]: Set this to 1 to enable constrainedFrame encoding where each slice in the constrained picture is independent of other slices. - Constrained encoding works only with rectangular slices. - Check support for constrained encoding using ::NV_ENC_CAPS_SUPPORT_CONSTRAINED_ENCODING caps. */ - uint32_t reserved :16; /**< [in]: Reserved bitfields.*/ - uint32_t idrPeriod; /**< [in]: Specifies the IDR interval. If not set, this is made equal to gopLength in NV_ENC_CONFIG. Low latency application client can set IDR interval to NVENC_INFINITE_GOPLENGTH so that IDR frames are not inserted automatically. */ - uint32_t intraRefreshPeriod; /**< [in]: Specifies the interval between successive intra refresh if enableIntrarefresh is set. Requires enableIntraRefresh to be set. - Will be disabled if NV_ENC_CONFIG::gopLength is not set to NVENC_INFINITE_GOPLENGTH. */ - uint32_t intraRefreshCnt; /**< [in]: Specifies the length of intra refresh in number of frames for periodic intra refresh. This value should be smaller than intraRefreshPeriod */ - uint32_t maxNumRefFramesInDPB; /**< [in]: Specifies the maximum number of references frames in the DPB.*/ - uint32_t ltrNumFrames; /**< [in]: This parameter has different meaning in two LTR modes. - In "LTR Trust" mode (ltrTrustMode = 1), encoder will mark the first ltrNumFrames base layer reference frames within each IDR interval as LTR. - In "LTR Per Picture" mode (ltrTrustMode = 0 and ltrMarkFrame = 1), ltrNumFrames specifies maximum number of LTR frames in DPB. */ - uint32_t vpsId; /**< [in]: Specifies the VPS id of the video parameter set */ - uint32_t spsId; /**< [in]: Specifies the SPS id of the sequence header */ - uint32_t ppsId; /**< [in]: Specifies the PPS id of the picture header */ - uint32_t sliceMode; /**< [in]: This parameter in conjunction with sliceModeData specifies the way in which the picture is divided into slices - sliceMode = 0 CTU based slices, sliceMode = 1 Byte based slices, sliceMode = 2 CTU row based slices, sliceMode = 3, numSlices in Picture - When sliceMode == 0 and sliceModeData == 0 whole picture will be coded with one slice */ - uint32_t sliceModeData; /**< [in]: Specifies the parameter needed for sliceMode. For: - sliceMode = 0, sliceModeData specifies # of CTUs in each slice (except last slice) - sliceMode = 1, sliceModeData specifies maximum # of bytes in each slice (except last slice) - sliceMode = 2, sliceModeData specifies # of CTU rows in each slice (except last slice) - sliceMode = 3, sliceModeData specifies number of slices in the picture. Driver will divide picture into slices optimally */ - uint32_t maxTemporalLayersMinus1; /**< [in]: Specifies the max temporal layer used for hierarchical coding. */ - NV_ENC_CONFIG_HEVC_VUI_PARAMETERS hevcVUIParameters; /**< [in]: Specifies the HEVC video usability info parameters */ - uint32_t ltrTrustMode; /**< [in]: Specifies the LTR operating mode. See comments near NV_ENC_CONFIG_HEVC::enableLTR for description of the two modes. - Set to 1 to use "LTR Trust" mode of LTR operation. Clients are discouraged to use "LTR Trust" mode as this mode may - be deprecated in future releases. - Set to 0 when using "LTR Per Picture" mode of LTR operation. */ - NV_ENC_BFRAME_REF_MODE useBFramesAsRef; /**< [in]: Specifies the B-Frame as reference mode. Check support for useBFramesAsRef mode using ::NV_ENC_CAPS_SUPPORT_BFRAME_REF_MODE caps.*/ - NV_ENC_NUM_REF_FRAMES numRefL0; /**< [in]: Specifies max number of reference frames in reference picture list L0, that can be used by hardware for prediction of a frame. - Check support for numRefL0 using ::NV_ENC_CAPS_SUPPORT_MULTIPLE_REF_FRAMES caps. */ - NV_ENC_NUM_REF_FRAMES numRefL1; /**< [in]: Specifies max number of reference frames in reference picture list L1, that can be used by hardware for prediction of a frame. - Check support for numRefL1 using ::NV_ENC_CAPS_SUPPORT_MULTIPLE_REF_FRAMES caps. */ - uint32_t reserved1[214]; /**< [in]: Reserved and must be set to 0.*/ - void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENC_CONFIG_HEVC; - -/** - * \struct _NV_ENC_CONFIG_H264_MEONLY - * H264 encoder configuration parameters for ME only Mode - * - */ -typedef struct _NV_ENC_CONFIG_H264_MEONLY -{ - uint32_t disablePartition16x16 :1; /**< [in]: Disable Motion Estimation on 16x16 blocks*/ - uint32_t disablePartition8x16 :1; /**< [in]: Disable Motion Estimation on 8x16 blocks*/ - uint32_t disablePartition16x8 :1; /**< [in]: Disable Motion Estimation on 16x8 blocks*/ - uint32_t disablePartition8x8 :1; /**< [in]: Disable Motion Estimation on 8x8 blocks*/ - uint32_t disableIntraSearch :1; /**< [in]: Disable Intra search during Motion Estimation*/ - uint32_t bStereoEnable :1; /**< [in]: Enable Stereo Mode for Motion Estimation where each view is independently executed*/ - uint32_t reserved :26; /**< [in]: Reserved and must be set to 0 */ - uint32_t reserved1 [255]; /**< [in]: Reserved and must be set to 0 */ - void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENC_CONFIG_H264_MEONLY; - - -/** - * \struct _NV_ENC_CONFIG_HEVC_MEONLY - * HEVC encoder configuration parameters for ME only Mode - * - */ -typedef struct _NV_ENC_CONFIG_HEVC_MEONLY -{ - uint32_t reserved [256]; /**< [in]: Reserved and must be set to 0 */ - void* reserved1[64]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENC_CONFIG_HEVC_MEONLY; - -/** - * \struct _NV_ENC_CODEC_CONFIG - * Codec-specific encoder configuration parameters to be set during initialization. - */ -typedef union _NV_ENC_CODEC_CONFIG -{ - NV_ENC_CONFIG_H264 h264Config; /**< [in]: Specifies the H.264-specific encoder configuration. */ - NV_ENC_CONFIG_HEVC hevcConfig; /**< [in]: Specifies the HEVC-specific encoder configuration. */ - NV_ENC_CONFIG_H264_MEONLY h264MeOnlyConfig; /**< [in]: Specifies the H.264-specific ME only encoder configuration. */ - NV_ENC_CONFIG_HEVC_MEONLY hevcMeOnlyConfig; /**< [in]: Specifies the HEVC-specific ME only encoder configuration. */ - uint32_t reserved[320]; /**< [in]: Reserved and must be set to 0 */ -} NV_ENC_CODEC_CONFIG; - - -/** - * \struct _NV_ENC_CONFIG - * Encoder configuration parameters to be set during initialization. - */ -typedef struct _NV_ENC_CONFIG -{ - uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_CONFIG_VER. */ - GUID profileGUID; /**< [in]: Specifies the codec profile GUID. If client specifies \p NV_ENC_CODEC_PROFILE_AUTOSELECT_GUID the NvEncodeAPI interface will select the appropriate codec profile. */ - uint32_t gopLength; /**< [in]: Specifies the number of pictures in one GOP. Low latency application client can set goplength to NVENC_INFINITE_GOPLENGTH so that keyframes are not inserted automatically. */ - int32_t frameIntervalP; /**< [in]: Specifies the GOP pattern as follows: \p frameIntervalP = 0: I, 1: IPP, 2: IBP, 3: IBBP If goplength is set to NVENC_INFINITE_GOPLENGTH \p frameIntervalP should be set to 1. */ - uint32_t monoChromeEncoding; /**< [in]: Set this to 1 to enable monochrome encoding for this session. */ - NV_ENC_PARAMS_FRAME_FIELD_MODE frameFieldMode; /**< [in]: Specifies the frame/field mode. - Check support for field encoding using ::NV_ENC_CAPS_SUPPORT_FIELD_ENCODING caps. - Using a frameFieldMode other than NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME for RGB input is not supported. */ - NV_ENC_MV_PRECISION mvPrecision; /**< [in]: Specifies the desired motion vector prediction precision. */ - NV_ENC_RC_PARAMS rcParams; /**< [in]: Specifies the rate control parameters for the current encoding session. */ - NV_ENC_CODEC_CONFIG encodeCodecConfig; /**< [in]: Specifies the codec specific config parameters through this union. */ - uint32_t reserved [278]; /**< [in]: Reserved and must be set to 0 */ - void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENC_CONFIG; - -/** macro for constructing the version field of ::_NV_ENC_CONFIG */ -#define NV_ENC_CONFIG_VER (NVENCAPI_STRUCT_VERSION(7) | ( 1<<31 )) - -/** - * Tuning information of NVENC encoding (TuningInfo is not applicable to H264 and HEVC MEOnly mode). - */ -typedef enum NV_ENC_TUNING_INFO -{ - NV_ENC_TUNING_INFO_UNDEFINED = 0, /**< Undefined tuningInfo. Invalid value for encoding. */ - NV_ENC_TUNING_INFO_HIGH_QUALITY = 1, /**< Tune presets for latency tolerant encoding.*/ - NV_ENC_TUNING_INFO_LOW_LATENCY = 2, /**< Tune presets for low latency streaming.*/ - NV_ENC_TUNING_INFO_ULTRA_LOW_LATENCY = 3, /**< Tune presets for ultra low latency streaming.*/ - NV_ENC_TUNING_INFO_LOSSLESS = 4, /**< Tune presets for lossless encoding.*/ - NV_ENC_TUNING_INFO_COUNT /**< Count number of tuningInfos. Invalid value. */ -}NV_ENC_TUNING_INFO; - -/** - * \struct _NV_ENC_INITIALIZE_PARAMS - * Encode Session Initialization parameters. - */ -typedef struct _NV_ENC_INITIALIZE_PARAMS -{ - uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_INITIALIZE_PARAMS_VER. */ - GUID encodeGUID; /**< [in]: Specifies the Encode GUID for which the encoder is being created. ::NvEncInitializeEncoder() API will fail if this is not set, or set to unsupported value. */ - GUID presetGUID; /**< [in]: Specifies the preset for encoding. If the preset GUID is set then , the preset configuration will be applied before any other parameter. */ - uint32_t encodeWidth; /**< [in]: Specifies the encode width. If not set ::NvEncInitializeEncoder() API will fail. */ - uint32_t encodeHeight; /**< [in]: Specifies the encode height. If not set ::NvEncInitializeEncoder() API will fail. */ - uint32_t darWidth; /**< [in]: Specifies the display aspect ratio Width. */ - uint32_t darHeight; /**< [in]: Specifies the display aspect ratio height. */ - uint32_t frameRateNum; /**< [in]: Specifies the numerator for frame rate used for encoding in frames per second ( Frame rate = frameRateNum / frameRateDen ). */ - uint32_t frameRateDen; /**< [in]: Specifies the denominator for frame rate used for encoding in frames per second ( Frame rate = frameRateNum / frameRateDen ). */ - uint32_t enableEncodeAsync; /**< [in]: Set this to 1 to enable asynchronous mode and is expected to use events to get picture completion notification. */ - uint32_t enablePTD; /**< [in]: Set this to 1 to enable the Picture Type Decision is be taken by the NvEncodeAPI interface. */ - uint32_t reportSliceOffsets :1; /**< [in]: Set this to 1 to enable reporting slice offsets in ::_NV_ENC_LOCK_BITSTREAM. NV_ENC_INITIALIZE_PARAMS::enableEncodeAsync must be set to 0 to use this feature. Client must set this to 0 if NV_ENC_CONFIG_H264::sliceMode is 1 on Kepler GPUs */ - uint32_t enableSubFrameWrite :1; /**< [in]: Set this to 1 to write out available bitstream to memory at subframe intervals. - If enableSubFrameWrite = 1, then the hardware encoder returns data as soon as a slice has completed encoding. - This results in better encoding latency, but the downside is that the application has to keep polling via a call to nvEncLockBitstream API continuously to see if any encoded slice data is available. - Use this mode if you feel that the marginal reduction in latency from sub-frame encoding is worth the increase in complexity due to CPU-based polling. */ - uint32_t enableExternalMEHints :1; /**< [in]: Set to 1 to enable external ME hints for the current frame. For NV_ENC_INITIALIZE_PARAMS::enablePTD=1 with B frames, programming L1 hints is optional for B frames since Client doesn't know internal GOP structure. - NV_ENC_PIC_PARAMS::meHintRefPicDist should preferably be set with enablePTD=1. */ - uint32_t enableMEOnlyMode :1; /**< [in]: Set to 1 to enable ME Only Mode .*/ - uint32_t enableWeightedPrediction :1; /**< [in]: Set this to 1 to enable weighted prediction. Not supported if encode session is configured for B-Frames (i.e. NV_ENC_CONFIG::frameIntervalP > 1 or preset >=P3 when tuningInfo = ::NV_ENC_TUNING_INFO_HIGH_QUALITY or - tuningInfo = ::NV_ENC_TUNING_INFO_LOSSLESS. This is because preset >=p3 internally enables B frames when tuningInfo = ::NV_ENC_TUNING_INFO_HIGH_QUALITY or ::NV_ENC_TUNING_INFO_LOSSLESS). */ - uint32_t enableOutputInVidmem :1; /**< [in]: Set this to 1 to enable output of NVENC in video memory buffer created by application. This feature is not supported for HEVC ME only mode. */ - uint32_t reservedBitFields :26; /**< [in]: Reserved bitfields and must be set to 0 */ - uint32_t privDataSize; /**< [in]: Reserved private data buffer size and must be set to 0 */ - void* privData; /**< [in]: Reserved private data buffer and must be set to NULL */ - NV_ENC_CONFIG* encodeConfig; /**< [in]: Specifies the advanced codec specific structure. If client has sent a valid codec config structure, it will override parameters set by the NV_ENC_INITIALIZE_PARAMS::presetGUID parameter. If set to NULL the NvEncodeAPI interface will use the NV_ENC_INITIALIZE_PARAMS::presetGUID to set the codec specific parameters. - Client can also optionally query the NvEncodeAPI interface to get codec specific parameters for a presetGUID using ::NvEncGetEncodePresetConfig() API. It can then modify (if required) some of the codec config parameters and send down a custom config structure as part of ::_NV_ENC_INITIALIZE_PARAMS. - Even in this case client is recommended to pass the same preset guid it has used in ::NvEncGetEncodePresetConfig() API to query the config structure; as NV_ENC_INITIALIZE_PARAMS::presetGUID. This will not override the custom config structure but will be used to determine other Encoder HW specific parameters not exposed in the API. */ - uint32_t maxEncodeWidth; /**< [in]: Maximum encode width to be used for current Encode session. - Client should allocate output buffers according to this dimension for dynamic resolution change. If set to 0, Encoder will not allow dynamic resolution change. */ - uint32_t maxEncodeHeight; /**< [in]: Maximum encode height to be allowed for current Encode session. - Client should allocate output buffers according to this dimension for dynamic resolution change. If set to 0, Encode will not allow dynamic resolution change. */ - NVENC_EXTERNAL_ME_HINT_COUNTS_PER_BLOCKTYPE maxMEHintCountsPerBlock[2]; /**< [in]: If Client wants to pass external motion vectors in NV_ENC_PIC_PARAMS::meExternalHints buffer it must specify the maximum number of hint candidates per block per direction for the encode session. - The NV_ENC_INITIALIZE_PARAMS::maxMEHintCountsPerBlock[0] is for L0 predictors and NV_ENC_INITIALIZE_PARAMS::maxMEHintCountsPerBlock[1] is for L1 predictors. - This client must also set NV_ENC_INITIALIZE_PARAMS::enableExternalMEHints to 1. */ - NV_ENC_TUNING_INFO tuningInfo; /**< [in]: Tuning Info of NVENC encoding(TuningInfo is not applicable to H264 and HEVC meonly mode). */ - uint32_t reserved [288]; /**< [in]: Reserved and must be set to 0 */ - void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENC_INITIALIZE_PARAMS; - -/** macro for constructing the version field of ::_NV_ENC_INITIALIZE_PARAMS */ -#define NV_ENC_INITIALIZE_PARAMS_VER (NVENCAPI_STRUCT_VERSION(5) | ( 1<<31 )) - - -/** - * \struct _NV_ENC_RECONFIGURE_PARAMS - * Encode Session Reconfigured parameters. - */ -typedef struct _NV_ENC_RECONFIGURE_PARAMS -{ - uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_RECONFIGURE_PARAMS_VER. */ - NV_ENC_INITIALIZE_PARAMS reInitEncodeParams; /**< [in]: Encoder session re-initialization parameters. - If reInitEncodeParams.encodeConfig is NULL and - reInitEncodeParams.presetGUID is the same as the preset - GUID specified on the call to NvEncInitializeEncoder(), - EncodeAPI will continue to use the existing encode - configuration. - If reInitEncodeParams.encodeConfig is NULL and - reInitEncodeParams.presetGUID is different from the preset - GUID specified on the call to NvEncInitializeEncoder(), - EncodeAPI will try to use the default configuration for - the preset specified by reInitEncodeParams.presetGUID. - In this case, reconfiguration may fail if the new - configuration is incompatible with the existing - configuration (e.g. the new configuration results in - a change in the GOP structure). */ - uint32_t resetEncoder :1; /**< [in]: This resets the rate control states and other internal encoder states. This should be used only with an IDR frame. - If NV_ENC_INITIALIZE_PARAMS::enablePTD is set to 1, encoder will force the frame type to IDR */ - uint32_t forceIDR :1; /**< [in]: Encode the current picture as an IDR picture. This flag is only valid when Picture type decision is taken by the Encoder - [_NV_ENC_INITIALIZE_PARAMS::enablePTD == 1]. */ - uint32_t reserved :30; - -}NV_ENC_RECONFIGURE_PARAMS; - -/** macro for constructing the version field of ::_NV_ENC_RECONFIGURE_PARAMS */ -#define NV_ENC_RECONFIGURE_PARAMS_VER (NVENCAPI_STRUCT_VERSION(1) | ( 1<<31 )) - -/** - * \struct _NV_ENC_PRESET_CONFIG - * Encoder preset config - */ -typedef struct _NV_ENC_PRESET_CONFIG -{ - uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_PRESET_CONFIG_VER. */ - NV_ENC_CONFIG presetCfg; /**< [out]: preset config returned by the Nvidia Video Encoder interface. */ - uint32_t reserved1[255]; /**< [in]: Reserved and must be set to 0 */ - void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ -}NV_ENC_PRESET_CONFIG; - -/** macro for constructing the version field of ::_NV_ENC_PRESET_CONFIG */ -#define NV_ENC_PRESET_CONFIG_VER (NVENCAPI_STRUCT_VERSION(4) | ( 1<<31 )) - - -/** - * \struct _NV_ENC_PIC_PARAMS_MVC - * MVC-specific parameters to be sent on a per-frame basis. - */ -typedef struct _NV_ENC_PIC_PARAMS_MVC -{ - uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_PIC_PARAMS_MVC_VER. */ - uint32_t viewID; /**< [in]: Specifies the view ID associated with the current input view. */ - uint32_t temporalID; /**< [in]: Specifies the temporal ID associated with the current input view. */ - uint32_t priorityID; /**< [in]: Specifies the priority ID associated with the current input view. Reserved and ignored by the NvEncodeAPI interface. */ - uint32_t reserved1[12]; /**< [in]: Reserved and must be set to 0. */ - void* reserved2[8]; /**< [in]: Reserved and must be set to NULL. */ -}NV_ENC_PIC_PARAMS_MVC; - -/** macro for constructing the version field of ::_NV_ENC_PIC_PARAMS_MVC */ -#define NV_ENC_PIC_PARAMS_MVC_VER NVENCAPI_STRUCT_VERSION(1) - - -/** - * \union _NV_ENC_PIC_PARAMS_H264_EXT - * H264 extension picture parameters - */ -typedef union _NV_ENC_PIC_PARAMS_H264_EXT -{ - NV_ENC_PIC_PARAMS_MVC mvcPicParams; /**< [in]: Specifies the MVC picture parameters. */ - uint32_t reserved1[32]; /**< [in]: Reserved and must be set to 0. */ -}NV_ENC_PIC_PARAMS_H264_EXT; - -/** - * \struct _NV_ENC_SEI_PAYLOAD - * User SEI message - */ -typedef struct _NV_ENC_SEI_PAYLOAD -{ - uint32_t payloadSize; /**< [in] SEI payload size in bytes. SEI payload must be byte aligned, as described in Annex D */ - uint32_t payloadType; /**< [in] SEI payload types and syntax can be found in Annex D of the H.264 Specification. */ - uint8_t *payload; /**< [in] pointer to user data */ -} NV_ENC_SEI_PAYLOAD; - -#define NV_ENC_H264_SEI_PAYLOAD NV_ENC_SEI_PAYLOAD - -/** - * \struct _NV_ENC_PIC_PARAMS_H264 - * H264 specific enc pic params. sent on a per frame basis. - */ -typedef struct _NV_ENC_PIC_PARAMS_H264 -{ - uint32_t displayPOCSyntax; /**< [in]: Specifies the display POC syntax This is required to be set if client is handling the picture type decision. */ - uint32_t reserved3; /**< [in]: Reserved and must be set to 0 */ - uint32_t refPicFlag; /**< [in]: Set to 1 for a reference picture. This is ignored if NV_ENC_INITIALIZE_PARAMS::enablePTD is set to 1. */ - uint32_t colourPlaneId; /**< [in]: Specifies the colour plane ID associated with the current input. */ - uint32_t forceIntraRefreshWithFrameCnt; /**< [in]: Forces an intra refresh with duration equal to intraRefreshFrameCnt. - When outputRecoveryPointSEI is set this is value is used for recovery_frame_cnt in recovery point SEI message - forceIntraRefreshWithFrameCnt cannot be used if B frames are used in the GOP structure specified */ - uint32_t constrainedFrame :1; /**< [in]: Set to 1 if client wants to encode this frame with each slice completely independent of other slices in the frame. - NV_ENC_INITIALIZE_PARAMS::enableConstrainedEncoding should be set to 1 */ - uint32_t sliceModeDataUpdate :1; /**< [in]: Set to 1 if client wants to change the sliceModeData field to specify new sliceSize Parameter - When forceIntraRefreshWithFrameCnt is set it will have priority over sliceMode setting */ - uint32_t ltrMarkFrame :1; /**< [in]: Set to 1 if client wants to mark this frame as LTR */ - uint32_t ltrUseFrames :1; /**< [in]: Set to 1 if client allows encoding this frame using the LTR frames specified in ltrFrameBitmap */ - uint32_t reservedBitFields :28; /**< [in]: Reserved bit fields and must be set to 0 */ - uint8_t* sliceTypeData; /**< [in]: Deprecated. */ - uint32_t sliceTypeArrayCnt; /**< [in]: Deprecated. */ - uint32_t seiPayloadArrayCnt; /**< [in]: Specifies the number of elements allocated in seiPayloadArray array. */ - NV_ENC_SEI_PAYLOAD* seiPayloadArray; /**< [in]: Array of SEI payloads which will be inserted for this frame. */ - uint32_t sliceMode; /**< [in]: This parameter in conjunction with sliceModeData specifies the way in which the picture is divided into slices - sliceMode = 0 MB based slices, sliceMode = 1 Byte based slices, sliceMode = 2 MB row based slices, sliceMode = 3, numSlices in Picture - When forceIntraRefreshWithFrameCnt is set it will have priority over sliceMode setting - When sliceMode == 0 and sliceModeData == 0 whole picture will be coded with one slice */ - uint32_t sliceModeData; /**< [in]: Specifies the parameter needed for sliceMode. For: - sliceMode = 0, sliceModeData specifies # of MBs in each slice (except last slice) - sliceMode = 1, sliceModeData specifies maximum # of bytes in each slice (except last slice) - sliceMode = 2, sliceModeData specifies # of MB rows in each slice (except last slice) - sliceMode = 3, sliceModeData specifies number of slices in the picture. Driver will divide picture into slices optimally */ - uint32_t ltrMarkFrameIdx; /**< [in]: Specifies the long term referenceframe index to use for marking this frame as LTR.*/ - uint32_t ltrUseFrameBitmap; /**< [in]: Specifies the associated bitmap of LTR frame indices to use when encoding this frame. */ - uint32_t ltrUsageMode; /**< [in]: Not supported. Reserved for future use and must be set to 0. */ - uint32_t forceIntraSliceCount; /**< [in]: Specifies the number of slices to be forced to Intra in the current picture. - This option along with forceIntraSliceIdx[] array needs to be used with sliceMode = 3 only */ - uint32_t *forceIntraSliceIdx; /**< [in]: Slice indices to be forced to intra in the current picture. Each slice index should be <= num_slices_in_picture -1. Index starts from 0 for first slice. - The number of entries in this array should be equal to forceIntraSliceCount */ - NV_ENC_PIC_PARAMS_H264_EXT h264ExtPicParams; /**< [in]: Specifies the H264 extension config parameters using this config. */ - uint32_t reserved [210]; /**< [in]: Reserved and must be set to 0. */ - void* reserved2[61]; /**< [in]: Reserved and must be set to NULL. */ -} NV_ENC_PIC_PARAMS_H264; - -/** - * \struct _NV_ENC_PIC_PARAMS_HEVC - * HEVC specific enc pic params. sent on a per frame basis. - */ -typedef struct _NV_ENC_PIC_PARAMS_HEVC -{ - uint32_t displayPOCSyntax; /**< [in]: Specifies the display POC syntax This is required to be set if client is handling the picture type decision. */ - uint32_t refPicFlag; /**< [in]: Set to 1 for a reference picture. This is ignored if NV_ENC_INITIALIZE_PARAMS::enablePTD is set to 1. */ - uint32_t temporalId; /**< [in]: Specifies the temporal id of the picture */ - uint32_t forceIntraRefreshWithFrameCnt; /**< [in]: Forces an intra refresh with duration equal to intraRefreshFrameCnt. - When outputRecoveryPointSEI is set this is value is used for recovery_frame_cnt in recovery point SEI message - forceIntraRefreshWithFrameCnt cannot be used if B frames are used in the GOP structure specified */ - uint32_t constrainedFrame :1; /**< [in]: Set to 1 if client wants to encode this frame with each slice completely independent of other slices in the frame. - NV_ENC_INITIALIZE_PARAMS::enableConstrainedEncoding should be set to 1 */ - uint32_t sliceModeDataUpdate :1; /**< [in]: Set to 1 if client wants to change the sliceModeData field to specify new sliceSize Parameter - When forceIntraRefreshWithFrameCnt is set it will have priority over sliceMode setting */ - uint32_t ltrMarkFrame :1; /**< [in]: Set to 1 if client wants to mark this frame as LTR */ - uint32_t ltrUseFrames :1; /**< [in]: Set to 1 if client allows encoding this frame using the LTR frames specified in ltrFrameBitmap */ - uint32_t reservedBitFields :28; /**< [in]: Reserved bit fields and must be set to 0 */ - uint8_t* sliceTypeData; /**< [in]: Array which specifies the slice type used to force intra slice for a particular slice. Currently supported only for NV_ENC_CONFIG_H264::sliceMode == 3. - Client should allocate array of size sliceModeData where sliceModeData is specified in field of ::_NV_ENC_CONFIG_H264 - Array element with index n corresponds to nth slice. To force a particular slice to intra client should set corresponding array element to NV_ENC_SLICE_TYPE_I - all other array elements should be set to NV_ENC_SLICE_TYPE_DEFAULT */ - uint32_t sliceTypeArrayCnt; /**< [in]: Client should set this to the number of elements allocated in sliceTypeData array. If sliceTypeData is NULL then this should be set to 0 */ - uint32_t sliceMode; /**< [in]: This parameter in conjunction with sliceModeData specifies the way in which the picture is divided into slices - sliceMode = 0 CTU based slices, sliceMode = 1 Byte based slices, sliceMode = 2 CTU row based slices, sliceMode = 3, numSlices in Picture - When forceIntraRefreshWithFrameCnt is set it will have priority over sliceMode setting - When sliceMode == 0 and sliceModeData == 0 whole picture will be coded with one slice */ - uint32_t sliceModeData; /**< [in]: Specifies the parameter needed for sliceMode. For: - sliceMode = 0, sliceModeData specifies # of CTUs in each slice (except last slice) - sliceMode = 1, sliceModeData specifies maximum # of bytes in each slice (except last slice) - sliceMode = 2, sliceModeData specifies # of CTU rows in each slice (except last slice) - sliceMode = 3, sliceModeData specifies number of slices in the picture. Driver will divide picture into slices optimally */ - uint32_t ltrMarkFrameIdx; /**< [in]: Specifies the long term reference frame index to use for marking this frame as LTR.*/ - uint32_t ltrUseFrameBitmap; /**< [in]: Specifies the associated bitmap of LTR frame indices to use when encoding this frame. */ - uint32_t ltrUsageMode; /**< [in]: Not supported. Reserved for future use and must be set to 0. */ - uint32_t seiPayloadArrayCnt; /**< [in]: Specifies the number of elements allocated in seiPayloadArray array. */ - uint32_t reserved; /**< [in]: Reserved and must be set to 0. */ - NV_ENC_SEI_PAYLOAD* seiPayloadArray; /**< [in]: Array of SEI payloads which will be inserted for this frame. */ - uint32_t reserved2 [244]; /**< [in]: Reserved and must be set to 0. */ - void* reserved3[61]; /**< [in]: Reserved and must be set to NULL. */ -} NV_ENC_PIC_PARAMS_HEVC; - -/** - * Codec specific per-picture encoding parameters. - */ -typedef union _NV_ENC_CODEC_PIC_PARAMS -{ - NV_ENC_PIC_PARAMS_H264 h264PicParams; /**< [in]: H264 encode picture params. */ - NV_ENC_PIC_PARAMS_HEVC hevcPicParams; /**< [in]: HEVC encode picture params. */ - uint32_t reserved[256]; /**< [in]: Reserved and must be set to 0. */ -} NV_ENC_CODEC_PIC_PARAMS; - -/** - * \struct _NV_ENC_PIC_PARAMS - * Encoding parameters that need to be sent on a per frame basis. - */ -typedef struct _NV_ENC_PIC_PARAMS -{ - uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_PIC_PARAMS_VER. */ - uint32_t inputWidth; /**< [in]: Specifies the input frame width */ - uint32_t inputHeight; /**< [in]: Specifies the input frame height */ - uint32_t inputPitch; /**< [in]: Specifies the input buffer pitch. If pitch value is not known, set this to inputWidth. */ - uint32_t encodePicFlags; /**< [in]: Specifies bit-wise OR of encode picture flags. See ::NV_ENC_PIC_FLAGS enum. */ - uint32_t frameIdx; /**< [in]: Specifies the frame index associated with the input frame [optional]. */ - uint64_t inputTimeStamp; /**< [in]: Specifies opaque data which is associated with the encoded frame, but not actually encoded in the output bitstream. - This opaque data can be used later to uniquely refer to the corresponding encoded frame. For example, it can be used - for identifying the frame to be invalidated in the reference picture buffer, if lost at the client. */ - uint64_t inputDuration; /**< [in]: Specifies duration of the input picture */ - NV_ENC_INPUT_PTR inputBuffer; /**< [in]: Specifies the input buffer pointer. Client must use a pointer obtained from ::NvEncCreateInputBuffer() or ::NvEncMapInputResource() APIs.*/ - NV_ENC_OUTPUT_PTR outputBitstream; /**< [in]: Specifies the output buffer pointer. - If NV_ENC_INITIALIZE_PARAMS::enableOutputInVidmem is set to 0, specifies the pointer to output buffer. Client should use a pointer obtained from ::NvEncCreateBitstreamBuffer() API. - If NV_ENC_INITIALIZE_PARAMS::enableOutputInVidmem is set to 1, client should allocate buffer in video memory for NV_ENC_ENCODE_OUT_PARAMS struct and encoded bitstream data. Client - should use a pointer obtained from ::NvEncMapInputResource() API, when mapping this output buffer and assign it to NV_ENC_PIC_PARAMS::outputBitstream. - First 256 bytes of this buffer should be interpreted as NV_ENC_ENCODE_OUT_PARAMS struct followed by encoded bitstream data. Recommended size for output buffer is sum of size of - NV_ENC_ENCODE_OUT_PARAMS struct and twice the input frame size for lower resolution eg. CIF and 1.5 times the input frame size for higher resolutions. If encoded bitstream size is - greater than the allocated buffer size for encoded bitstream, then the output buffer will have encoded bitstream data equal to buffer size. All CUDA operations on this buffer must use - the default stream. */ - void* completionEvent; /**< [in]: Specifies an event to be signaled on completion of encoding of this Frame [only if operating in Asynchronous mode]. Each output buffer should be associated with a distinct event pointer. */ - NV_ENC_BUFFER_FORMAT bufferFmt; /**< [in]: Specifies the input buffer format. */ - NV_ENC_PIC_STRUCT pictureStruct; /**< [in]: Specifies structure of the input picture. */ - NV_ENC_PIC_TYPE pictureType; /**< [in]: Specifies input picture type. Client required to be set explicitly by the client if the client has not set NV_ENC_INITALIZE_PARAMS::enablePTD to 1 while calling NvInitializeEncoder. */ - NV_ENC_CODEC_PIC_PARAMS codecPicParams; /**< [in]: Specifies the codec specific per-picture encoding parameters. */ - NVENC_EXTERNAL_ME_HINT_COUNTS_PER_BLOCKTYPE meHintCountsPerBlock[2]; /**< [in]: Specifies the number of hint candidates per block per direction for the current frame. meHintCountsPerBlock[0] is for L0 predictors and meHintCountsPerBlock[1] is for L1 predictors. - The candidate count in NV_ENC_PIC_PARAMS::meHintCountsPerBlock[lx] must never exceed NV_ENC_INITIALIZE_PARAMS::maxMEHintCountsPerBlock[lx] provided during encoder initialization. */ - NVENC_EXTERNAL_ME_HINT *meExternalHints; /**< [in]: Specifies the pointer to ME external hints for the current frame. The size of ME hint buffer should be equal to number of macroblocks * the total number of candidates per macroblock. - The total number of candidates per MB per direction = 1*meHintCountsPerBlock[Lx].numCandsPerBlk16x16 + 2*meHintCountsPerBlock[Lx].numCandsPerBlk16x8 + 2*meHintCountsPerBlock[Lx].numCandsPerBlk8x8 - + 4*meHintCountsPerBlock[Lx].numCandsPerBlk8x8. For frames using bidirectional ME , the total number of candidates for single macroblock is sum of total number of candidates per MB for each direction (L0 and L1) */ - uint32_t reserved1[6]; /**< [in]: Reserved and must be set to 0 */ - void* reserved2[2]; /**< [in]: Reserved and must be set to NULL */ - int8_t *qpDeltaMap; /**< [in]: Specifies the pointer to signed byte array containing value per MB for H264 and per CTB for HEVC in raster scan order for the current picture, which will be interpreted depending on NV_ENC_RC_PARAMS::qpMapMode. - If NV_ENC_RC_PARAMS::qpMapMode is NV_ENC_QP_MAP_DELTA, qpDeltaMap specifies QP modifier per MB for H264 and per CTB for HEVC. This QP modifier will be applied on top of the QP chosen by rate control. - If NV_ENC_RC_PARAMS::qpMapMode is NV_ENC_QP_MAP_EMPHASIS, qpDeltaMap specifies Emphasis Level Map per MB for H264. This level value along with QP chosen by rate control is used to - compute the QP modifier, which in turn is applied on top of QP chosen by rate control. - If NV_ENC_RC_PARAMS::qpMapMode is NV_ENC_QP_MAP_DISABLED, value in qpDeltaMap will be ignored.*/ - uint32_t qpDeltaMapSize; /**< [in]: Specifies the size in bytes of qpDeltaMap surface allocated by client and pointed to by NV_ENC_PIC_PARAMS::qpDeltaMap. Surface (array) should be picWidthInMbs * picHeightInMbs for H264 and picWidthInCtbs * picHeightInCtbs for HEVC */ - uint32_t reservedBitFields; /**< [in]: Reserved bitfields and must be set to 0 */ - uint16_t meHintRefPicDist[2]; /**< [in]: Specifies temporal distance for reference picture (NVENC_EXTERNAL_ME_HINT::refidx = 0) used during external ME with NV_ENC_INITALIZE_PARAMS::enablePTD = 1 . meHintRefPicDist[0] is for L0 hints and meHintRefPicDist[1] is for L1 hints. - If not set, will internally infer distance of 1. Ignored for NV_ENC_INITALIZE_PARAMS::enablePTD = 0 */ - uint32_t reserved3[286]; /**< [in]: Reserved and must be set to 0 */ - void* reserved4[60]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENC_PIC_PARAMS; - -/** Macro for constructing the version field of ::_NV_ENC_PIC_PARAMS */ -#define NV_ENC_PIC_PARAMS_VER (NVENCAPI_STRUCT_VERSION(4) | ( 1<<31 )) - - -/** - * \struct _NV_ENC_MEONLY_PARAMS - * MEOnly parameters that need to be sent on a per motion estimation basis. - * NV_ENC_MEONLY_PARAMS::meExternalHints is supported for H264 only. - */ -typedef struct _NV_ENC_MEONLY_PARAMS -{ - uint32_t version; /**< [in]: Struct version. Must be set to NV_ENC_MEONLY_PARAMS_VER.*/ - uint32_t inputWidth; /**< [in]: Specifies the input frame width */ - uint32_t inputHeight; /**< [in]: Specifies the input frame height */ - NV_ENC_INPUT_PTR inputBuffer; /**< [in]: Specifies the input buffer pointer. Client must use a pointer obtained from NvEncCreateInputBuffer() or NvEncMapInputResource() APIs. */ - NV_ENC_INPUT_PTR referenceFrame; /**< [in]: Specifies the reference frame pointer */ - NV_ENC_OUTPUT_PTR mvBuffer; /**< [in]: Specifies the output buffer pointer. - If NV_ENC_INITIALIZE_PARAMS::enableOutputInVidmem is set to 0, specifies the pointer to motion vector data buffer allocated by NvEncCreateMVBuffer. - Client must lock mvBuffer using ::NvEncLockBitstream() API to get the motion vector data. - If NV_ENC_INITIALIZE_PARAMS::enableOutputInVidmem is set to 1, client should allocate buffer in video memory for storing the motion vector data. The size of this buffer must - be equal to total number of macroblocks multiplied by size of NV_ENC_H264_MV_DATA struct. Client should use a pointer obtained from ::NvEncMapInputResource() API, when mapping this - output buffer and assign it to NV_ENC_MEONLY_PARAMS::mvBuffer. All CUDA operations on this buffer must use the default stream. */ - NV_ENC_BUFFER_FORMAT bufferFmt; /**< [in]: Specifies the input buffer format. */ - void* completionEvent; /**< [in]: Specifies an event to be signaled on completion of motion estimation - of this Frame [only if operating in Asynchronous mode]. - Each output buffer should be associated with a distinct event pointer. */ - uint32_t viewID; /**< [in]: Specifies left or right viewID if NV_ENC_CONFIG_H264_MEONLY::bStereoEnable is set. - viewID can be 0,1 if bStereoEnable is set, 0 otherwise. */ - NVENC_EXTERNAL_ME_HINT_COUNTS_PER_BLOCKTYPE - meHintCountsPerBlock[2]; /**< [in]: Specifies the number of hint candidates per block for the current frame. meHintCountsPerBlock[0] is for L0 predictors. - The candidate count in NV_ENC_PIC_PARAMS::meHintCountsPerBlock[lx] must never exceed NV_ENC_INITIALIZE_PARAMS::maxMEHintCountsPerBlock[lx] provided during encoder initialization. */ - NVENC_EXTERNAL_ME_HINT *meExternalHints; /**< [in]: Specifies the pointer to ME external hints for the current frame. The size of ME hint buffer should be equal to number of macroblocks * the total number of candidates per macroblock. - The total number of candidates per MB per direction = 1*meHintCountsPerBlock[Lx].numCandsPerBlk16x16 + 2*meHintCountsPerBlock[Lx].numCandsPerBlk16x8 + 2*meHintCountsPerBlock[Lx].numCandsPerBlk8x8 - + 4*meHintCountsPerBlock[Lx].numCandsPerBlk8x8. For frames using bidirectional ME , the total number of candidates for single macroblock is sum of total number of candidates per MB for each direction (L0 and L1) */ - uint32_t reserved1[243]; /**< [in]: Reserved and must be set to 0 */ - void* reserved2[59]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENC_MEONLY_PARAMS; - -/** NV_ENC_MEONLY_PARAMS struct version*/ -#define NV_ENC_MEONLY_PARAMS_VER NVENCAPI_STRUCT_VERSION(3) - - -/** - * \struct _NV_ENC_LOCK_BITSTREAM - * Bitstream buffer lock parameters. - */ -typedef struct _NV_ENC_LOCK_BITSTREAM -{ - uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_LOCK_BITSTREAM_VER. */ - uint32_t doNotWait :1; /**< [in]: If this flag is set, the NvEncodeAPI interface will return buffer pointer even if operation is not completed. If not set, the call will block until operation completes. */ - uint32_t ltrFrame :1; /**< [out]: Flag indicating this frame is marked as LTR frame */ - uint32_t getRCStats :1; /**< [in]: If this flag is set then lockBitstream call will add additional intra-inter MB count and average MVX, MVY */ - uint32_t reservedBitFields :29; /**< [in]: Reserved bit fields and must be set to 0 */ - void* outputBitstream; /**< [in]: Pointer to the bitstream buffer being locked. */ - uint32_t* sliceOffsets; /**< [in, out]: Array which receives the slice offsets. This is not supported if NV_ENC_CONFIG_H264::sliceMode is 1 on Kepler GPUs. Array size must be equal to size of frame in MBs. */ - uint32_t frameIdx; /**< [out]: Frame no. for which the bitstream is being retrieved. */ - uint32_t hwEncodeStatus; /**< [out]: The NvEncodeAPI interface status for the locked picture. */ - uint32_t numSlices; /**< [out]: Number of slices in the encoded picture. Will be reported only if NV_ENC_INITIALIZE_PARAMS::reportSliceOffsets set to 1. */ - uint32_t bitstreamSizeInBytes; /**< [out]: Actual number of bytes generated and copied to the memory pointed by bitstreamBufferPtr. */ - uint64_t outputTimeStamp; /**< [out]: Presentation timestamp associated with the encoded output. */ - uint64_t outputDuration; /**< [out]: Presentation duration associates with the encoded output. */ - void* bitstreamBufferPtr; /**< [out]: Pointer to the generated output bitstream. - For MEOnly mode _NV_ENC_LOCK_BITSTREAM::bitstreamBufferPtr should be typecast to - NV_ENC_H264_MV_DATA/NV_ENC_HEVC_MV_DATA pointer respectively for H264/HEVC */ - NV_ENC_PIC_TYPE pictureType; /**< [out]: Picture type of the encoded picture. */ - NV_ENC_PIC_STRUCT pictureStruct; /**< [out]: Structure of the generated output picture. */ - uint32_t frameAvgQP; /**< [out]: Average QP of the frame. */ - uint32_t frameSatd; /**< [out]: Total SATD cost for whole frame. */ - uint32_t ltrFrameIdx; /**< [out]: Frame index associated with this LTR frame. */ - uint32_t ltrFrameBitmap; /**< [out]: Bitmap of LTR frames indices which were used for encoding this frame. Value of 0 if no LTR frames were used. */ - uint32_t reserved[13]; /**< [in]: Reserved and must be set to 0 */ - uint32_t intraMBCount; /**< [out]: For H264, Number of Intra MBs in the encoded frame. For HEVC, Number of Intra CTBs in the encoded frame. Supported only if _NV_ENC_LOCK_BITSTREAM::getRCStats set to 1. */ - uint32_t interMBCount; /**< [out]: For H264, Number of Inter MBs in the encoded frame, includes skip MBs. For HEVC, Number of Inter CTBs in the encoded frame. Supported only if _NV_ENC_LOCK_BITSTREAM::getRCStats set to 1. */ - int32_t averageMVX; /**< [out]: Average Motion Vector in X direction for the encoded frame. Supported only if _NV_ENC_LOCK_BITSTREAM::getRCStats set to 1. */ - int32_t averageMVY; /**< [out]: Average Motion Vector in y direction for the encoded frame. Supported only if _NV_ENC_LOCK_BITSTREAM::getRCStats set to 1. */ - uint32_t reserved1[219]; /**< [in]: Reserved and must be set to 0 */ - void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENC_LOCK_BITSTREAM; - -/** Macro for constructing the version field of ::_NV_ENC_LOCK_BITSTREAM */ -#define NV_ENC_LOCK_BITSTREAM_VER NVENCAPI_STRUCT_VERSION(1) - - -/** - * \struct _NV_ENC_LOCK_INPUT_BUFFER - * Uncompressed Input Buffer lock parameters. - */ -typedef struct _NV_ENC_LOCK_INPUT_BUFFER -{ - uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_LOCK_INPUT_BUFFER_VER. */ - uint32_t doNotWait :1; /**< [in]: Set to 1 to make ::NvEncLockInputBuffer() a unblocking call. If the encoding is not completed, driver will return ::NV_ENC_ERR_ENCODER_BUSY error code. */ - uint32_t reservedBitFields :31; /**< [in]: Reserved bitfields and must be set to 0 */ - NV_ENC_INPUT_PTR inputBuffer; /**< [in]: Pointer to the input buffer to be locked, client should pass the pointer obtained from ::NvEncCreateInputBuffer() or ::NvEncMapInputResource API. */ - void* bufferDataPtr; /**< [out]: Pointed to the locked input buffer data. Client can only access input buffer using the \p bufferDataPtr. */ - uint32_t pitch; /**< [out]: Pitch of the locked input buffer. */ - uint32_t reserved1[251]; /**< [in]: Reserved and must be set to 0 */ - void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENC_LOCK_INPUT_BUFFER; - -/** Macro for constructing the version field of ::_NV_ENC_LOCK_INPUT_BUFFER */ -#define NV_ENC_LOCK_INPUT_BUFFER_VER NVENCAPI_STRUCT_VERSION(1) - - -/** - * \struct _NV_ENC_MAP_INPUT_RESOURCE - * Map an input resource to a Nvidia Encoder Input Buffer - */ -typedef struct _NV_ENC_MAP_INPUT_RESOURCE -{ - uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_MAP_INPUT_RESOURCE_VER. */ - uint32_t subResourceIndex; /**< [in]: Deprecated. Do not use. */ - void* inputResource; /**< [in]: Deprecated. Do not use. */ - NV_ENC_REGISTERED_PTR registeredResource; /**< [in]: The Registered resource handle obtained by calling NvEncRegisterInputResource. */ - NV_ENC_INPUT_PTR mappedResource; /**< [out]: Mapped pointer corresponding to the registeredResource. This pointer must be used in NV_ENC_PIC_PARAMS::inputBuffer parameter in ::NvEncEncodePicture() API. */ - NV_ENC_BUFFER_FORMAT mappedBufferFmt; /**< [out]: Buffer format of the outputResource. This buffer format must be used in NV_ENC_PIC_PARAMS::bufferFmt if client using the above mapped resource pointer. */ - uint32_t reserved1[251]; /**< [in]: Reserved and must be set to 0. */ - void* reserved2[63]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENC_MAP_INPUT_RESOURCE; - -/** Macro for constructing the version field of ::_NV_ENC_MAP_INPUT_RESOURCE */ -#define NV_ENC_MAP_INPUT_RESOURCE_VER NVENCAPI_STRUCT_VERSION(4) - -/** - * \struct _NV_ENC_INPUT_RESOURCE_OPENGL_TEX - * NV_ENC_REGISTER_RESOURCE::resourceToRegister must be a pointer to a variable of this type, - * when NV_ENC_REGISTER_RESOURCE::resourceType is NV_ENC_INPUT_RESOURCE_TYPE_OPENGL_TEX - */ -typedef struct _NV_ENC_INPUT_RESOURCE_OPENGL_TEX -{ - uint32_t texture; /**< [in]: The name of the texture to be used. */ - uint32_t target; /**< [in]: Accepted values are GL_TEXTURE_RECTANGLE and GL_TEXTURE_2D. */ -} NV_ENC_INPUT_RESOURCE_OPENGL_TEX; - -/** - * \struct _NV_ENC_REGISTER_RESOURCE - * Register a resource for future use with the Nvidia Video Encoder Interface. - */ -typedef struct _NV_ENC_REGISTER_RESOURCE -{ - uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_REGISTER_RESOURCE_VER. */ - NV_ENC_INPUT_RESOURCE_TYPE resourceType; /**< [in]: Specifies the type of resource to be registered. - Supported values are - ::NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX, - ::NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR, - ::NV_ENC_INPUT_RESOURCE_TYPE_OPENGL_TEX */ - uint32_t width; /**< [in]: Input frame width. */ - uint32_t height; /**< [in]: Input frame height. */ - uint32_t pitch; /**< [in]: Input buffer pitch. - For ::NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX resources, set this to 0. - For ::NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR resources, set this to - the pitch as obtained from cuMemAllocPitch(), or to the width in - bytes (if this resource was created by using cuMemAlloc()). This - value must be a multiple of 4. - For ::NV_ENC_INPUT_RESOURCE_TYPE_CUDAARRAY resources, set this to the - width of the allocation in bytes (i.e. - CUDA_ARRAY3D_DESCRIPTOR::Width * CUDA_ARRAY3D_DESCRIPTOR::NumChannels). - For ::NV_ENC_INPUT_RESOURCE_TYPE_OPENGL_TEX resources, set this to the - texture width multiplied by the number of components in the texture - format. */ - uint32_t subResourceIndex; /**< [in]: Subresource Index of the DirectX resource to be registered. Should be set to 0 for other interfaces. */ - void* resourceToRegister; /**< [in]: Handle to the resource that is being registered. */ - NV_ENC_REGISTERED_PTR registeredResource; /**< [out]: Registered resource handle. This should be used in future interactions with the Nvidia Video Encoder Interface. */ - NV_ENC_BUFFER_FORMAT bufferFormat; /**< [in]: Buffer format of resource to be registered. */ - NV_ENC_BUFFER_USAGE bufferUsage; /**< [in]: Usage of resource to be registered. */ - uint32_t reserved1[247]; /**< [in]: Reserved and must be set to 0. */ - void* reserved2[62]; /**< [in]: Reserved and must be set to NULL. */ -} NV_ENC_REGISTER_RESOURCE; - -/** Macro for constructing the version field of ::_NV_ENC_REGISTER_RESOURCE */ -#define NV_ENC_REGISTER_RESOURCE_VER NVENCAPI_STRUCT_VERSION(3) - -/** - * \struct _NV_ENC_STAT - * Encode Stats structure. - */ -typedef struct _NV_ENC_STAT -{ - uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_STAT_VER. */ - uint32_t reserved; /**< [in]: Reserved and must be set to 0 */ - NV_ENC_OUTPUT_PTR outputBitStream; /**< [out]: Specifies the pointer to output bitstream. */ - uint32_t bitStreamSize; /**< [out]: Size of generated bitstream in bytes. */ - uint32_t picType; /**< [out]: Picture type of encoded picture. See ::NV_ENC_PIC_TYPE. */ - uint32_t lastValidByteOffset; /**< [out]: Offset of last valid bytes of completed bitstream */ - uint32_t sliceOffsets[16]; /**< [out]: Offsets of each slice */ - uint32_t picIdx; /**< [out]: Picture number */ - uint32_t frameAvgQP; /**< [out]: Average QP of the frame. */ - uint32_t ltrFrame :1; /**< [out]: Flag indicating this frame is marked as LTR frame */ - uint32_t reservedBitFields :31; /**< [in]: Reserved bit fields and must be set to 0 */ - uint32_t ltrFrameIdx; /**< [out]: Frame index associated with this LTR frame. */ - uint32_t intraMBCount; /**< [out]: For H264, Number of Intra MBs in the encoded frame. For HEVC, Number of Intra CTBs in the encoded frame. */ - uint32_t interMBCount; /**< [out]: For H264, Number of Inter MBs in the encoded frame, includes skip MBs. For HEVC, Number of Inter CTBs in the encoded frame. */ - int32_t averageMVX; /**< [out]: Average Motion Vector in X direction for the encoded frame. */ - int32_t averageMVY; /**< [out]: Average Motion Vector in y direction for the encoded frame. */ - uint32_t reserved1[226]; /**< [in]: Reserved and must be set to 0 */ - void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENC_STAT; - -/** Macro for constructing the version field of ::_NV_ENC_STAT */ -#define NV_ENC_STAT_VER NVENCAPI_STRUCT_VERSION(1) - - -/** - * \struct _NV_ENC_SEQUENCE_PARAM_PAYLOAD - * Sequence and picture paramaters payload. - */ -typedef struct _NV_ENC_SEQUENCE_PARAM_PAYLOAD -{ - uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_INITIALIZE_PARAMS_VER. */ - uint32_t inBufferSize; /**< [in]: Specifies the size of the spsppsBuffer provided by the client */ - uint32_t spsId; /**< [in]: Specifies the SPS id to be used in sequence header. Default value is 0. */ - uint32_t ppsId; /**< [in]: Specifies the PPS id to be used in picture header. Default value is 0. */ - void* spsppsBuffer; /**< [in]: Specifies bitstream header pointer of size NV_ENC_SEQUENCE_PARAM_PAYLOAD::inBufferSize. - It is the client's responsibility to manage this memory. */ - uint32_t* outSPSPPSPayloadSize; /**< [out]: Size of the sequence and picture header in bytes. */ - uint32_t reserved [250]; /**< [in]: Reserved and must be set to 0 */ - void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENC_SEQUENCE_PARAM_PAYLOAD; - -/** Macro for constructing the version field of ::_NV_ENC_SEQUENCE_PARAM_PAYLOAD */ -#define NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER NVENCAPI_STRUCT_VERSION(1) - - -/** - * Event registration/unregistration parameters. - */ -typedef struct _NV_ENC_EVENT_PARAMS -{ - uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_EVENT_PARAMS_VER. */ - uint32_t reserved; /**< [in]: Reserved and must be set to 0 */ - void* completionEvent; /**< [in]: Handle to event to be registered/unregistered with the NvEncodeAPI interface. */ - uint32_t reserved1[253]; /**< [in]: Reserved and must be set to 0 */ - void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENC_EVENT_PARAMS; - -/** Macro for constructing the version field of ::_NV_ENC_EVENT_PARAMS */ -#define NV_ENC_EVENT_PARAMS_VER NVENCAPI_STRUCT_VERSION(1) - -/** - * Encoder Session Creation parameters - */ -typedef struct _NV_ENC_OPEN_ENCODE_SESSIONEX_PARAMS -{ - uint32_t version; /**< [in]: Struct version. Must be set to ::NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER. */ - NV_ENC_DEVICE_TYPE deviceType; /**< [in]: Specified the device Type */ - void* device; /**< [in]: Pointer to client device. */ - void* reserved; /**< [in]: Reserved and must be set to 0. */ - uint32_t apiVersion; /**< [in]: API version. Should be set to NVENCAPI_VERSION. */ - uint32_t reserved1[253]; /**< [in]: Reserved and must be set to 0 */ - void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS; -/** Macro for constructing the version field of ::_NV_ENC_OPEN_ENCODE_SESSIONEX_PARAMS */ -#define NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER NVENCAPI_STRUCT_VERSION(1) - -/** @} */ /* END ENCODER_STRUCTURE */ - - -/** - * \addtogroup ENCODE_FUNC NvEncodeAPI Functions - * @{ - */ - -// NvEncOpenEncodeSession -/** - * \brief Opens an encoding session. - * - * Deprecated. - * - * \return - * ::NV_ENC_ERR_INVALID_CALL\n - * - */ -NVENCSTATUS NVENCAPI NvEncOpenEncodeSession (void* device, uint32_t deviceType, void** encoder); - -// NvEncGetEncodeGuidCount -/** - * \brief Retrieves the number of supported encode GUIDs. - * - * The function returns the number of codec GUIDs supported by the NvEncodeAPI - * interface. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [out] encodeGUIDCount - * Number of supported encode GUIDs. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncGetEncodeGUIDCount (void* encoder, uint32_t* encodeGUIDCount); - - -// NvEncGetEncodeGUIDs -/** - * \brief Retrieves an array of supported encoder codec GUIDs. - * - * The function returns an array of codec GUIDs supported by the NvEncodeAPI interface. - * The client must allocate an array where the NvEncodeAPI interface can - * fill the supported GUIDs and pass the pointer in \p *GUIDs parameter. - * The size of the array can be determined by using ::NvEncGetEncodeGUIDCount() API. - * The Nvidia Encoding interface returns the number of codec GUIDs it has actually - * filled in the GUID array in the \p GUIDCount parameter. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] guidArraySize - * Number of GUIDs to retrieved. Should be set to the number retrieved using - * ::NvEncGetEncodeGUIDCount. - * \param [out] GUIDs - * Array of supported Encode GUIDs. - * \param [out] GUIDCount - * Number of supported Encode GUIDs. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncGetEncodeGUIDs (void* encoder, GUID* GUIDs, uint32_t guidArraySize, uint32_t* GUIDCount); - - -// NvEncGetEncodeProfileGuidCount -/** - * \brief Retrieves the number of supported profile GUIDs. - * - * The function returns the number of profile GUIDs supported for a given codec. - * The client must first enumerate the codec GUIDs supported by the NvEncodeAPI - * interface. After determining the codec GUID, it can query the NvEncodeAPI - * interface to determine the number of profile GUIDs supported for a particular - * codec GUID. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] encodeGUID - * The codec GUID for which the profile GUIDs are being enumerated. - * \param [out] encodeProfileGUIDCount - * Number of encode profiles supported for the given encodeGUID. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncGetEncodeProfileGUIDCount (void* encoder, GUID encodeGUID, uint32_t* encodeProfileGUIDCount); - - -// NvEncGetEncodeProfileGUIDs -/** - * \brief Retrieves an array of supported encode profile GUIDs. - * - * The function returns an array of supported profile GUIDs for a particular - * codec GUID. The client must allocate an array where the NvEncodeAPI interface - * can populate the profile GUIDs. The client can determine the array size using - * ::NvEncGetEncodeProfileGUIDCount() API. The client must also validiate that the - * NvEncodeAPI interface supports the GUID the client wants to pass as \p encodeGUID - * parameter. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] encodeGUID - * The encode GUID whose profile GUIDs are being enumerated. - * \param [in] guidArraySize - * Number of GUIDs to be retrieved. Should be set to the number retrieved using - * ::NvEncGetEncodeProfileGUIDCount. - * \param [out] profileGUIDs - * Array of supported Encode Profile GUIDs - * \param [out] GUIDCount - * Number of valid encode profile GUIDs in \p profileGUIDs array. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncGetEncodeProfileGUIDs (void* encoder, GUID encodeGUID, GUID* profileGUIDs, uint32_t guidArraySize, uint32_t* GUIDCount); - -// NvEncGetInputFormatCount -/** - * \brief Retrieve the number of supported Input formats. - * - * The function returns the number of supported input formats. The client must - * query the NvEncodeAPI interface to determine the supported input formats - * before creating the input surfaces. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] encodeGUID - * Encode GUID, corresponding to which the number of supported input formats - * is to be retrieved. - * \param [out] inputFmtCount - * Number of input formats supported for specified Encode GUID. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_GENERIC \n - */ -NVENCSTATUS NVENCAPI NvEncGetInputFormatCount (void* encoder, GUID encodeGUID, uint32_t* inputFmtCount); - - -// NvEncGetInputFormats -/** - * \brief Retrieves an array of supported Input formats - * - * Returns an array of supported input formats The client must use the input - * format to create input surface using ::NvEncCreateInputBuffer() API. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] encodeGUID - * Encode GUID, corresponding to which the number of supported input formats - * is to be retrieved. - *\param [in] inputFmtArraySize - * Size input format count array passed in \p inputFmts. - *\param [out] inputFmts - * Array of input formats supported for this Encode GUID. - *\param [out] inputFmtCount - * The number of valid input format types returned by the NvEncodeAPI - * interface in \p inputFmts array. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncGetInputFormats (void* encoder, GUID encodeGUID, NV_ENC_BUFFER_FORMAT* inputFmts, uint32_t inputFmtArraySize, uint32_t* inputFmtCount); - - -// NvEncGetEncodeCaps -/** - * \brief Retrieves the capability value for a specified encoder attribute. - * - * The function returns the capability value for a given encoder attribute. The - * client must validate the encodeGUID using ::NvEncGetEncodeGUIDs() API before - * calling this function. The encoder attribute being queried are enumerated in - * ::NV_ENC_CAPS_PARAM enum. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] encodeGUID - * Encode GUID, corresponding to which the capability attribute is to be retrieved. - * \param [in] capsParam - * Used to specify attribute being queried. Refer ::NV_ENC_CAPS_PARAM for more - * details. - * \param [out] capsVal - * The value corresponding to the capability attribute being queried. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_GENERIC \n - */ -NVENCSTATUS NVENCAPI NvEncGetEncodeCaps (void* encoder, GUID encodeGUID, NV_ENC_CAPS_PARAM* capsParam, int* capsVal); - - -// NvEncGetEncodePresetCount -/** - * \brief Retrieves the number of supported preset GUIDs. - * - * The function returns the number of preset GUIDs available for a given codec. - * The client must validate the codec GUID using ::NvEncGetEncodeGUIDs() API - * before calling this function. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] encodeGUID - * Encode GUID, corresponding to which the number of supported presets is to - * be retrieved. - * \param [out] encodePresetGUIDCount - * Receives the number of supported preset GUIDs. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncGetEncodePresetCount (void* encoder, GUID encodeGUID, uint32_t* encodePresetGUIDCount); - - -// NvEncGetEncodePresetGUIDs -/** - * \brief Receives an array of supported encoder preset GUIDs. - * - * The function returns an array of encode preset GUIDs available for a given codec. - * The client can directly use one of the preset GUIDs based upon the use case - * or target device. The preset GUID chosen can be directly used in - * NV_ENC_INITIALIZE_PARAMS::presetGUID parameter to ::NvEncEncodePicture() API. - * Alternately client can also use the preset GUID to retrieve the encoding config - * parameters being used by NvEncodeAPI interface for that given preset, using - * ::NvEncGetEncodePresetConfig() API. It can then modify preset config parameters - * as per its use case and send it to NvEncodeAPI interface as part of - * NV_ENC_INITIALIZE_PARAMS::encodeConfig parameter for NvEncInitializeEncoder() - * API. - * - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] encodeGUID - * Encode GUID, corresponding to which the list of supported presets is to be - * retrieved. - * \param [in] guidArraySize - * Size of array of preset GUIDs passed in \p preset GUIDs - * \param [out] presetGUIDs - * Array of supported Encode preset GUIDs from the NvEncodeAPI interface - * to client. - * \param [out] encodePresetGUIDCount - * Receives the number of preset GUIDs returned by the NvEncodeAPI - * interface. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncGetEncodePresetGUIDs (void* encoder, GUID encodeGUID, GUID* presetGUIDs, uint32_t guidArraySize, uint32_t* encodePresetGUIDCount); - - -// NvEncGetEncodePresetConfig -/** - * \brief Returns a preset config structure supported for given preset GUID. - * - * The function returns a preset config structure for a given preset GUID. Before - * using this function the client must enumerate the preset GUIDs available for - * a given codec. The preset config structure can be modified by the client depending - * upon its use case and can be then used to initialize the encoder using - * ::NvEncInitializeEncoder() API. The client can use this function only if it - * wants to modify the NvEncodeAPI preset configuration, otherwise it can - * directly use the preset GUID. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] encodeGUID - * Encode GUID, corresponding to which the list of supported presets is to be - * retrieved. - * \param [in] presetGUID - * Preset GUID, corresponding to which the Encoding configurations is to be - * retrieved. - * \param [out] presetConfig - * The requested Preset Encoder Attribute set. Refer ::_NV_ENC_CONFIG for -* more details. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncGetEncodePresetConfig (void* encoder, GUID encodeGUID, GUID presetGUID, NV_ENC_PRESET_CONFIG* presetConfig); - -// NvEncGetEncodePresetConfigEx -/** - * \brief Returns a preset config structure supported for given preset GUID. - * - * The function returns a preset config structure for a given preset GUID and tuning info. - * NvEncGetEncodePresetConfigEx() API is not applicable to H264 and HEVC meonly mode. - * Before using this function the client must enumerate the preset GUIDs available for - * a given codec. The preset config structure can be modified by the client depending - * upon its use case and can be then used to initialize the encoder using - * ::NvEncInitializeEncoder() API. The client can use this function only if it - * wants to modify the NvEncodeAPI preset configuration, otherwise it can - * directly use the preset GUID. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] encodeGUID - * Encode GUID, corresponding to which the list of supported presets is to be - * retrieved. - * \param [in] presetGUID - * Preset GUID, corresponding to which the Encoding configurations is to be - * retrieved. - * \param [in] tuningInfo - * tuning info, corresponding to which the Encoding configurations is to be - * retrieved. - * \param [out] presetConfig - * The requested Preset Encoder Attribute set. Refer ::_NV_ENC_CONFIG for - * more details. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncGetEncodePresetConfigEx (void* encoder, GUID encodeGUID, GUID presetGUID, NV_ENC_TUNING_INFO tuningInfo, NV_ENC_PRESET_CONFIG* presetConfig); - -// NvEncInitializeEncoder -/** - * \brief Initialize the encoder. - * - * This API must be used to initialize the encoder. The initialization parameter - * is passed using \p *createEncodeParams The client must send the following - * fields of the _NV_ENC_INITIALIZE_PARAMS structure with a valid value. - * - NV_ENC_INITIALIZE_PARAMS::encodeGUID - * - NV_ENC_INITIALIZE_PARAMS::encodeWidth - * - NV_ENC_INITIALIZE_PARAMS::encodeHeight - * - * The client can pass a preset GUID directly to the NvEncodeAPI interface using - * NV_ENC_INITIALIZE_PARAMS::presetGUID field. If the client doesn't pass - * NV_ENC_INITIALIZE_PARAMS::encodeConfig structure, the codec specific parameters - * will be selected based on the preset GUID. The preset GUID must have been - * validated by the client using ::NvEncGetEncodePresetGUIDs() API. - * If the client passes a custom ::_NV_ENC_CONFIG structure through - * NV_ENC_INITIALIZE_PARAMS::encodeConfig , it will override the codec specific parameters - * based on the preset GUID. It is recommended that even if the client passes a custom config, - * it should also send a preset GUID. In this case, the preset GUID passed by the client - * will not override any of the custom config parameters programmed by the client, - * it is only used as a hint by the NvEncodeAPI interface to determine certain encoder parameters - * which are not exposed to the client. - * - * There are two modes of operation for the encoder namely: - * - Asynchronous mode - * - Synchronous mode - * - * The client can select asynchronous or synchronous mode by setting the \p - * enableEncodeAsync field in ::_NV_ENC_INITIALIZE_PARAMS to 1 or 0 respectively. - *\par Asynchronous mode of operation: - * The Asynchronous mode can be enabled by setting NV_ENC_INITIALIZE_PARAMS::enableEncodeAsync to 1. - * The client operating in asynchronous mode must allocate completion event object - * for each output buffer and pass the completion event object in the - * ::NvEncEncodePicture() API. The client can create another thread and wait on - * the event object to be signaled by NvEncodeAPI interface on completion of the - * encoding process for the output frame. This should unblock the main thread from - * submitting work to the encoder. When the event is signaled the client can call - * NvEncodeAPI interfaces to copy the bitstream data using ::NvEncLockBitstream() - * API. This is the preferred mode of operation. - * - * NOTE: Asynchronous mode is not supported on Linux. - * - *\par Synchronous mode of operation: - * The client can select synchronous mode by setting NV_ENC_INITIALIZE_PARAMS::enableEncodeAsync to 0. - * The client working in synchronous mode can work in a single threaded or multi - * threaded mode. The client need not allocate any event objects. The client can - * only lock the bitstream data after NvEncodeAPI interface has returned - * ::NV_ENC_SUCCESS from encode picture. The NvEncodeAPI interface can return - * ::NV_ENC_ERR_NEED_MORE_INPUT error code from ::NvEncEncodePicture() API. The - * client must not lock the output buffer in such case but should send the next - * frame for encoding. The client must keep on calling ::NvEncEncodePicture() API - * until it returns ::NV_ENC_SUCCESS. \n - * The client must always lock the bitstream data in order in which it has submitted. - * This is true for both asynchronous and synchronous mode. - * - *\par Picture type decision: - * If the client is taking the picture type decision and it must disable the picture - * type decision module in NvEncodeAPI by setting NV_ENC_INITIALIZE_PARAMS::enablePTD - * to 0. In this case the client is required to send the picture in encoding - * order to NvEncodeAPI by doing the re-ordering for B frames. \n - * If the client doesn't want to take the picture type decision it can enable - * picture type decision module in the NvEncodeAPI interface by setting - * NV_ENC_INITIALIZE_PARAMS::enablePTD to 1 and send the input pictures in display - * order. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] createEncodeParams - * Refer ::_NV_ENC_INITIALIZE_PARAMS for details. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncInitializeEncoder (void* encoder, NV_ENC_INITIALIZE_PARAMS* createEncodeParams); - - -// NvEncCreateInputBuffer -/** - * \brief Allocates Input buffer. - * - * This function is used to allocate an input buffer. The client must enumerate - * the input buffer format before allocating the input buffer resources. The - * NV_ENC_INPUT_PTR returned by the NvEncodeAPI interface in the - * NV_ENC_CREATE_INPUT_BUFFER::inputBuffer field can be directly used in - * ::NvEncEncodePicture() API. The number of input buffers to be allocated by the - * client must be at least 4 more than the number of B frames being used for encoding. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in,out] createInputBufferParams - * Pointer to the ::NV_ENC_CREATE_INPUT_BUFFER structure. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncCreateInputBuffer (void* encoder, NV_ENC_CREATE_INPUT_BUFFER* createInputBufferParams); - - -// NvEncDestroyInputBuffer -/** - * \brief Release an input buffers. - * - * This function is used to free an input buffer. If the client has allocated - * any input buffer using ::NvEncCreateInputBuffer() API, it must free those - * input buffers by calling this function. The client must release the input - * buffers before destroying the encoder using ::NvEncDestroyEncoder() API. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] inputBuffer - * Pointer to the input buffer to be released. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncDestroyInputBuffer (void* encoder, NV_ENC_INPUT_PTR inputBuffer); - -// NvEncSetIOCudaStreams -/** - * \brief Set input and output CUDA stream for specified encoder attribute. - * - * Encoding may involve CUDA pre-processing on the input and post-processing on encoded output. - * This function is used to set input and output CUDA streams to pipeline the CUDA pre-processing - * and post-processing tasks. Clients should call this function before the call to - * NvEncUnlockInputBuffer(). If this function is not called, the default CUDA stream is used for - * input and output processing. After a successful call to this function, the streams specified - * in that call will replace the previously-used streams. - * This API is supported for NVCUVID interface only. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] inputStream - * Pointer to CUstream which is used to process ::NV_ENC_PIC_PARAMS::inputFrame for encode. - * In case of ME-only mode, inputStream is used to process ::NV_ENC_MEONLY_PARAMS::inputBuffer and - * ::NV_ENC_MEONLY_PARAMS::referenceFrame - * \param [in] outputStream - * Pointer to CUstream which is used to process ::NV_ENC_PIC_PARAMS::outputBuffer for encode. - * In case of ME-only mode, outputStream is used to process ::NV_ENC_MEONLY_PARAMS::mvBuffer - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_GENERIC \n - */ -NVENCSTATUS NVENCAPI NvEncSetIOCudaStreams (void* encoder, NV_ENC_CUSTREAM_PTR inputStream, NV_ENC_CUSTREAM_PTR outputStream); - - -// NvEncCreateBitstreamBuffer -/** - * \brief Allocates an output bitstream buffer - * - * This function is used to allocate an output bitstream buffer and returns a - * NV_ENC_OUTPUT_PTR to bitstream buffer to the client in the - * NV_ENC_CREATE_BITSTREAM_BUFFER::bitstreamBuffer field. - * The client can only call this function after the encoder session has been - * initialized using ::NvEncInitializeEncoder() API. The minimum number of output - * buffers allocated by the client must be at least 4 more than the number of B - * B frames being used for encoding. The client can only access the output - * bitstream data by locking the \p bitstreamBuffer using the ::NvEncLockBitstream() - * function. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in,out] createBitstreamBufferParams - * Pointer ::NV_ENC_CREATE_BITSTREAM_BUFFER for details. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncCreateBitstreamBuffer (void* encoder, NV_ENC_CREATE_BITSTREAM_BUFFER* createBitstreamBufferParams); - - -// NvEncDestroyBitstreamBuffer -/** - * \brief Release a bitstream buffer. - * - * This function is used to release the output bitstream buffer allocated using - * the ::NvEncCreateBitstreamBuffer() function. The client must release the output - * bitstreamBuffer using this function before destroying the encoder session. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] bitstreamBuffer - * Pointer to the bitstream buffer being released. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncDestroyBitstreamBuffer (void* encoder, NV_ENC_OUTPUT_PTR bitstreamBuffer); - -// NvEncEncodePicture -/** - * \brief Submit an input picture for encoding. - * - * This function is used to submit an input picture buffer for encoding. The - * encoding parameters are passed using \p *encodePicParams which is a pointer - * to the ::_NV_ENC_PIC_PARAMS structure. - * - * If the client has set NV_ENC_INITIALIZE_PARAMS::enablePTD to 0, then it must - * send a valid value for the following fields. - * - NV_ENC_PIC_PARAMS::pictureType - * - NV_ENC_PIC_PARAMS_H264::displayPOCSyntax (H264 only) - * - NV_ENC_PIC_PARAMS_H264::frameNumSyntax(H264 only) - * - NV_ENC_PIC_PARAMS_H264::refPicFlag(H264 only) - * - *\par MVC Encoding: - * For MVC encoding the client must call encode picture API for each view separately - * and must pass valid view id in NV_ENC_PIC_PARAMS_MVC::viewID field. Currently - * NvEncodeAPI only support stereo MVC so client must send viewID as 0 for base - * view and view ID as 1 for dependent view. - * - *\par Asynchronous Encoding - * If the client has enabled asynchronous mode of encoding by setting - * NV_ENC_INITIALIZE_PARAMS::enableEncodeAsync to 1 in the ::NvEncInitializeEncoder() - * API ,then the client must send a valid NV_ENC_PIC_PARAMS::completionEvent. - * Incase of asynchronous mode of operation, client can queue the ::NvEncEncodePicture() - * API commands from the main thread and then queue output buffers to be processed - * to a secondary worker thread. Before the locking the output buffers in the - * secondary thread , the client must wait on NV_ENC_PIC_PARAMS::completionEvent - * it has queued in ::NvEncEncodePicture() API call. The client must always process - * completion event and the output buffer in the same order in which they have been - * submitted for encoding. The NvEncodeAPI interface is responsible for any - * re-ordering required for B frames and will always ensure that encoded bitstream - * data is written in the same order in which output buffer is submitted. - * The NvEncodeAPI interface may return ::NV_ENC_ERR_NEED_MORE_INPUT error code for - * some ::NvEncEncodePicture() API calls but the client must not treat it as a fatal error. - * The NvEncodeAPI interface might not be able to submit an input picture buffer for encoding - * immediately due to re-ordering for B frames. - *\code - The below example shows how asynchronous encoding in case of 1 B frames - ------------------------------------------------------------------------ - Suppose the client allocated 4 input buffers(I1,I2..), 4 output buffers(O1,O2..) - and 4 completion events(E1, E2, ...). The NvEncodeAPI interface will need to - keep a copy of the input buffers for re-ordering and it allocates following - internal buffers (NvI1, NvI2...). These internal buffers are managed by NvEncodeAPI - and the client is not responsible for the allocating or freeing the memory of - the internal buffers. - - a) The client main thread will queue the following encode frame calls. - Note the picture type is unknown to the client, the decision is being taken by - NvEncodeAPI interface. The client should pass ::_NV_ENC_PIC_PARAMS parameter - consisting of allocated input buffer, output buffer and output events in successive - ::NvEncEncodePicture() API calls along with other required encode picture params. - For example: - 1st EncodePicture parameters - (I1, O1, E1) - 2nd EncodePicture parameters - (I2, O2, E2) - 3rd EncodePicture parameters - (I3, O3, E3) - - b) NvEncodeAPI SW will receive the following encode Commands from the client. - The left side shows input from client in the form (Input buffer, Output Buffer, - Output Event). The right hand side shows a possible picture type decision take by - the NvEncodeAPI interface. - (I1, O1, E1) ---P1 Frame - (I2, O2, E2) ---B2 Frame - (I3, O3, E3) ---P3 Frame - - c) NvEncodeAPI interface will make a copy of the input buffers to its internal - buffers for re-ordering. These copies are done as part of nvEncEncodePicture - function call from the client and NvEncodeAPI interface is responsible for - synchronization of copy operation with the actual encoding operation. - I1 --> NvI1 - I2 --> NvI2 - I3 --> NvI3 - - d) The NvEncodeAPI encodes I1 as P frame and submits I1 to encoder HW and returns ::NV_ENC_SUCCESS. - The NvEncodeAPI tries to encode I2 as B frame and fails with ::NV_ENC_ERR_NEED_MORE_INPUT error code. - The error is not fatal and it notifies client that I2 is not submitted to encoder immediately. - The NvEncodeAPI encodes I3 as P frame and submits I3 for encoding which will be used as backward - reference frame for I2. The NvEncodeAPI then submits I2 for encoding and returns ::NV_ENC_SUCESS. - Both the submission are part of the same ::NvEncEncodePicture() function call. - - e) After returning from ::NvEncEncodePicture() call , the client must queue the output - bitstream processing work to the secondary thread. The output bitstream processing - for asynchronous mode consist of first waiting on completion event(E1, E2..) - and then locking the output bitstream buffer(O1, O2..) for reading the encoded - data. The work queued to the secondary thread by the client is in the following order - (I1, O1, E1) - (I2, O2, E2) - (I3, O3, E3) - Note they are in the same order in which client calls ::NvEncEncodePicture() API - in \p step a). - - f) NvEncodeAPI interface will do the re-ordering such that Encoder HW will receive - the following encode commands: - (NvI1, O1, E1) ---P1 Frame - (NvI3, O2, E2) ---P3 Frame - (NvI2, O3, E3) ---B2 frame - - g) After the encoding operations are completed, the events will be signaled - by NvEncodeAPI interface in the following order : - (O1, E1) ---P1 Frame ,output bitstream copied to O1 and event E1 signaled. - (O2, E2) ---P3 Frame ,output bitstream copied to O2 and event E2 signaled. - (O3, E3) ---B2 Frame ,output bitstream copied to O3 and event E3 signaled. - - h) The client must lock the bitstream data using ::NvEncLockBitstream() API in - the order O1,O2,O3 to read the encoded data, after waiting for the events - to be signaled in the same order i.e E1, E2 and E3.The output processing is - done in the secondary thread in the following order: - Waits on E1, copies encoded bitstream from O1 - Waits on E2, copies encoded bitstream from O2 - Waits on E3, copies encoded bitstream from O3 - - -Note the client will receive the events signaling and output buffer in the - same order in which they have submitted for encoding. - -Note the LockBitstream will have picture type field which will notify the - output picture type to the clients. - -Note the input, output buffer and the output completion event are free to be - reused once NvEncodeAPI interfaced has signaled the event and the client has - copied the data from the output buffer. - - * \endcode - * - *\par Synchronous Encoding - * The client can enable synchronous mode of encoding by setting - * NV_ENC_INITIALIZE_PARAMS::enableEncodeAsync to 0 in ::NvEncInitializeEncoder() API. - * The NvEncodeAPI interface may return ::NV_ENC_ERR_NEED_MORE_INPUT error code for - * some ::NvEncEncodePicture() API calls when NV_ENC_INITIALIZE_PARAMS::enablePTD - * is set to 1, but the client must not treat it as a fatal error. The NvEncodeAPI - * interface might not be able to submit an input picture buffer for encoding - * immediately due to re-ordering for B frames. The NvEncodeAPI interface cannot - * submit the input picture which is decided to be encoded as B frame as it waits - * for backward reference from temporally subsequent frames. This input picture - * is buffered internally and waits for more input picture to arrive. The client - * must not call ::NvEncLockBitstream() API on the output buffers whose - * ::NvEncEncodePicture() API returns ::NV_ENC_ERR_NEED_MORE_INPUT. The client must - * wait for the NvEncodeAPI interface to return ::NV_ENC_SUCCESS before locking the - * output bitstreams to read the encoded bitstream data. The following example - * explains the scenario with synchronous encoding with 2 B frames. - *\code - The below example shows how synchronous encoding works in case of 1 B frames - ----------------------------------------------------------------------------- - Suppose the client allocated 4 input buffers(I1,I2..), 4 output buffers(O1,O2..) - and 4 completion events(E1, E2, ...). The NvEncodeAPI interface will need to - keep a copy of the input buffers for re-ordering and it allocates following - internal buffers (NvI1, NvI2...). These internal buffers are managed by NvEncodeAPI - and the client is not responsible for the allocating or freeing the memory of - the internal buffers. - - The client calls ::NvEncEncodePicture() API with input buffer I1 and output buffer O1. - The NvEncodeAPI decides to encode I1 as P frame and submits it to encoder - HW and returns ::NV_ENC_SUCCESS. - The client can now read the encoded data by locking the output O1 by calling - NvEncLockBitstream API. - - The client calls ::NvEncEncodePicture() API with input buffer I2 and output buffer O2. - The NvEncodeAPI decides to encode I2 as B frame and buffers I2 by copying it - to internal buffer and returns ::NV_ENC_ERR_NEED_MORE_INPUT. - The error is not fatal and it notifies client that it cannot read the encoded - data by locking the output O2 by calling ::NvEncLockBitstream() API without submitting - more work to the NvEncodeAPI interface. - - The client calls ::NvEncEncodePicture() with input buffer I3 and output buffer O3. - The NvEncodeAPI decides to encode I3 as P frame and it first submits I3 for - encoding which will be used as backward reference frame for I2. - The NvEncodeAPI then submits I2 for encoding and returns ::NV_ENC_SUCESS. Both - the submission are part of the same ::NvEncEncodePicture() function call. - The client can now read the encoded data for both the frames by locking the output - O2 followed by O3 ,by calling ::NvEncLockBitstream() API. - - The client must always lock the output in the same order in which it has submitted - to receive the encoded bitstream in correct encoding order. - - * \endcode - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in,out] encodePicParams - * Pointer to the ::_NV_ENC_PIC_PARAMS structure. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_ENCODER_BUSY \n - * ::NV_ENC_ERR_NEED_MORE_INPUT \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncEncodePicture (void* encoder, NV_ENC_PIC_PARAMS* encodePicParams); - - -// NvEncLockBitstream -/** - * \brief Lock output bitstream buffer - * - * This function is used to lock the bitstream buffer to read the encoded data. - * The client can only access the encoded data by calling this function. - * The pointer to client accessible encoded data is returned in the - * NV_ENC_LOCK_BITSTREAM::bitstreamBufferPtr field. The size of the encoded data - * in the output buffer is returned in the NV_ENC_LOCK_BITSTREAM::bitstreamSizeInBytes - * The NvEncodeAPI interface also returns the output picture type and picture structure - * of the encoded frame in NV_ENC_LOCK_BITSTREAM::pictureType and - * NV_ENC_LOCK_BITSTREAM::pictureStruct fields respectively. If the client has - * set NV_ENC_LOCK_BITSTREAM::doNotWait to 1, the function might return - * ::NV_ENC_ERR_LOCK_BUSY if client is operating in synchronous mode. This is not - * a fatal failure if NV_ENC_LOCK_BITSTREAM::doNotWait is set to 1. In the above case the client can - * retry the function after few milliseconds. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in,out] lockBitstreamBufferParams - * Pointer to the ::_NV_ENC_LOCK_BITSTREAM structure. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_LOCK_BUSY \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncLockBitstream (void* encoder, NV_ENC_LOCK_BITSTREAM* lockBitstreamBufferParams); - - -// NvEncUnlockBitstream -/** - * \brief Unlock the output bitstream buffer - * - * This function is used to unlock the output bitstream buffer after the client - * has read the encoded data from output buffer. The client must call this function - * to unlock the output buffer which it has previously locked using ::NvEncLockBitstream() - * function. Using a locked bitstream buffer in ::NvEncEncodePicture() API will cause - * the function to fail. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in,out] bitstreamBuffer - * bitstream buffer pointer being unlocked - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncUnlockBitstream (void* encoder, NV_ENC_OUTPUT_PTR bitstreamBuffer); - - -// NvLockInputBuffer -/** - * \brief Locks an input buffer - * - * This function is used to lock the input buffer to load the uncompressed YUV - * pixel data into input buffer memory. The client must pass the NV_ENC_INPUT_PTR - * it had previously allocated using ::NvEncCreateInputBuffer()in the - * NV_ENC_LOCK_INPUT_BUFFER::inputBuffer field. - * The NvEncodeAPI interface returns pointer to client accessible input buffer - * memory in NV_ENC_LOCK_INPUT_BUFFER::bufferDataPtr field. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in,out] lockInputBufferParams - * Pointer to the ::_NV_ENC_LOCK_INPUT_BUFFER structure - * - * \return - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_LOCK_BUSY \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncLockInputBuffer (void* encoder, NV_ENC_LOCK_INPUT_BUFFER* lockInputBufferParams); - - -// NvUnlockInputBuffer -/** - * \brief Unlocks the input buffer - * - * This function is used to unlock the input buffer memory previously locked for - * uploading YUV pixel data. The input buffer must be unlocked before being used - * again for encoding, otherwise NvEncodeAPI will fail the ::NvEncEncodePicture() - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] inputBuffer - * Pointer to the input buffer that is being unlocked. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_GENERIC \n - * - * - */ -NVENCSTATUS NVENCAPI NvEncUnlockInputBuffer (void* encoder, NV_ENC_INPUT_PTR inputBuffer); - - -// NvEncGetEncodeStats -/** - * \brief Get encoding statistics. - * - * This function is used to retrieve the encoding statistics. - * This API is not supported when encode device type is CUDA. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in,out] encodeStats - * Pointer to the ::_NV_ENC_STAT structure. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncGetEncodeStats (void* encoder, NV_ENC_STAT* encodeStats); - - -// NvEncGetSequenceParams -/** - * \brief Get encoded sequence and picture header. - * - * This function can be used to retrieve the sequence and picture header out of - * band. The client must call this function only after the encoder has been - * initialized using ::NvEncInitializeEncoder() function. The client must - * allocate the memory where the NvEncodeAPI interface can copy the bitstream - * header and pass the pointer to the memory in NV_ENC_SEQUENCE_PARAM_PAYLOAD::spsppsBuffer. - * The size of buffer is passed in the field NV_ENC_SEQUENCE_PARAM_PAYLOAD::inBufferSize. - * The NvEncodeAPI interface will copy the bitstream header payload and returns - * the actual size of the bitstream header in the field - * NV_ENC_SEQUENCE_PARAM_PAYLOAD::outSPSPPSPayloadSize. - * The client must call ::NvEncGetSequenceParams() function from the same thread which is - * being used to call ::NvEncEncodePicture() function. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in,out] sequenceParamPayload - * Pointer to the ::_NV_ENC_SEQUENCE_PARAM_PAYLOAD structure. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncGetSequenceParams (void* encoder, NV_ENC_SEQUENCE_PARAM_PAYLOAD* sequenceParamPayload); - -// NvEncGetSequenceParamEx -/** - * \brief Get sequence and picture header. - * - * This function can be used to retrieve the sequence and picture header out of band, even when - * encoder has not been initialized using ::NvEncInitializeEncoder() function. - * The client must allocate the memory where the NvEncodeAPI interface can copy the bitstream - * header and pass the pointer to the memory in NV_ENC_SEQUENCE_PARAM_PAYLOAD::spsppsBuffer. - * The size of buffer is passed in the field NV_ENC_SEQUENCE_PARAM_PAYLOAD::inBufferSize. - * If encoder has not been initialized using ::NvEncInitializeEncoder() function, client must - * send NV_ENC_INITIALIZE_PARAMS as input. The NV_ENC_INITIALIZE_PARAMS passed must be same as the - * one which will be used for initializing encoder using ::NvEncInitializeEncoder() function later. - * If encoder is already initialized using ::NvEncInitializeEncoder() function, the provided - * NV_ENC_INITIALIZE_PARAMS structure is ignored. The NvEncodeAPI interface will copy the bitstream - * header payload and returns the actual size of the bitstream header in the field - * NV_ENC_SEQUENCE_PARAM_PAYLOAD::outSPSPPSPayloadSize. The client must call ::NvEncGetSequenceParamsEx() - * function from the same thread which is being used to call ::NvEncEncodePicture() function. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] encInitParams - * Pointer to the _NV_ENC_INITIALIZE_PARAMS structure. - * \param [in,out] sequenceParamPayload - * Pointer to the ::_NV_ENC_SEQUENCE_PARAM_PAYLOAD structure. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncGetSequenceParamEx (void* encoder, NV_ENC_INITIALIZE_PARAMS* encInitParams, NV_ENC_SEQUENCE_PARAM_PAYLOAD* sequenceParamPayload); - -// NvEncRegisterAsyncEvent -/** - * \brief Register event for notification to encoding completion. - * - * This function is used to register the completion event with NvEncodeAPI - * interface. The event is required when the client has configured the encoder to - * work in asynchronous mode. In this mode the client needs to send a completion - * event with every output buffer. The NvEncodeAPI interface will signal the - * completion of the encoding process using this event. Only after the event is - * signaled the client can get the encoded data using ::NvEncLockBitstream() function. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] eventParams - * Pointer to the ::_NV_ENC_EVENT_PARAMS structure. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncRegisterAsyncEvent (void* encoder, NV_ENC_EVENT_PARAMS* eventParams); - - -// NvEncUnregisterAsyncEvent -/** - * \brief Unregister completion event. - * - * This function is used to unregister completion event which has been previously - * registered using ::NvEncRegisterAsyncEvent() function. The client must unregister - * all events before destroying the encoder using ::NvEncDestroyEncoder() function. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] eventParams - * Pointer to the ::_NV_ENC_EVENT_PARAMS structure. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncUnregisterAsyncEvent (void* encoder, NV_ENC_EVENT_PARAMS* eventParams); - - -// NvEncMapInputResource -/** - * \brief Map an externally created input resource pointer for encoding. - * - * Maps an externally allocated input resource [using and returns a NV_ENC_INPUT_PTR - * which can be used for encoding in the ::NvEncEncodePicture() function. The - * mapped resource is returned in the field NV_ENC_MAP_INPUT_RESOURCE::outputResourcePtr. - * The NvEncodeAPI interface also returns the buffer format of the mapped resource - * in the field NV_ENC_MAP_INPUT_RESOURCE::outbufferFmt. - * This function provides synchronization guarantee that any graphics work submitted - * on the input buffer is completed before the buffer is used for encoding. This is - * also true for compute (i.e. CUDA) work, provided that the previous workload using - * the input resource was submitted to the default stream. - * The client should not access any input buffer while they are mapped by the encoder. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in,out] mapInputResParams - * Pointer to the ::_NV_ENC_MAP_INPUT_RESOURCE structure. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_RESOURCE_NOT_REGISTERED \n - * ::NV_ENC_ERR_MAP_FAILED \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncMapInputResource (void* encoder, NV_ENC_MAP_INPUT_RESOURCE* mapInputResParams); - - -// NvEncUnmapInputResource -/** - * \brief UnMaps a NV_ENC_INPUT_PTR which was mapped for encoding - * - * - * UnMaps an input buffer which was previously mapped using ::NvEncMapInputResource() - * API. The mapping created using ::NvEncMapInputResource() should be invalidated - * using this API before the external resource is destroyed by the client. The client - * must unmap the buffer after ::NvEncLockBitstream() API returns successfully for encode - * work submitted using the mapped input buffer. - * - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] mappedInputBuffer - * Pointer to the NV_ENC_INPUT_PTR - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_RESOURCE_NOT_REGISTERED \n - * ::NV_ENC_ERR_RESOURCE_NOT_MAPPED \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncUnmapInputResource (void* encoder, NV_ENC_INPUT_PTR mappedInputBuffer); - -// NvEncDestroyEncoder -/** - * \brief Destroy Encoding Session - * - * Destroys the encoder session previously created using ::NvEncOpenEncodeSession() - * function. The client must flush the encoder before freeing any resources. In order - * to flush the encoder the client must pass a NULL encode picture packet and either - * wait for the ::NvEncEncodePicture() function to return in synchronous mode or wait - * for the flush event to be signaled by the encoder in asynchronous mode. - * The client must free all the input and output resources created using the - * NvEncodeAPI interface before destroying the encoder. If the client is operating - * in asynchronous mode, it must also unregister the completion events previously - * registered. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncDestroyEncoder (void* encoder); - -// NvEncInvalidateRefFrames -/** - * \brief Invalidate reference frames - * - * Invalidates reference frame based on the time stamp provided by the client. - * The encoder marks any reference frames or any frames which have been reconstructed - * using the corrupt frame as invalid for motion estimation and uses older reference - * frames for motion estimation. The encoded forces the current frame to be encoded - * as an intra frame if no reference frames are left after invalidation process. - * This is useful for low latency application for error resiliency. The client - * is recommended to set NV_ENC_CONFIG_H264::maxNumRefFrames to a large value so - * that encoder can keep a backup of older reference frames in the DPB and can use them - * for motion estimation when the newer reference frames have been invalidated. - * This API can be called multiple times. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] invalidRefFrameTimeStamp - * Timestamp of the invalid reference frames which needs to be invalidated. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncInvalidateRefFrames(void* encoder, uint64_t invalidRefFrameTimeStamp); - -// NvEncOpenEncodeSessionEx -/** - * \brief Opens an encoding session. - * - * Opens an encoding session and returns a pointer to the encoder interface in - * the \p **encoder parameter. The client should start encoding process by calling - * this API first. - * The client must pass a pointer to IDirect3DDevice9 device or CUDA context in the \p *device parameter. - * For the OpenGL interface, \p device must be NULL. An OpenGL context must be current when - * calling all NvEncodeAPI functions. - * If the creation of encoder session fails, the client must call ::NvEncDestroyEncoder API - * before exiting. - * - * \param [in] openSessionExParams - * Pointer to a ::NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS structure. - * \param [out] encoder - * Encode Session pointer to the NvEncodeAPI interface. - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_NO_ENCODE_DEVICE \n - * ::NV_ENC_ERR_UNSUPPORTED_DEVICE \n - * ::NV_ENC_ERR_INVALID_DEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncOpenEncodeSessionEx (NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS *openSessionExParams, void** encoder); - -// NvEncRegisterResource -/** - * \brief Registers a resource with the Nvidia Video Encoder Interface. - * - * Registers a resource with the Nvidia Video Encoder Interface for book keeping. - * The client is expected to pass the registered resource handle as well, while calling ::NvEncMapInputResource API. - * - * \param [in] encoder - * Pointer to the NVEncodeAPI interface. - * - * \param [in] registerResParams - * Pointer to a ::_NV_ENC_REGISTER_RESOURCE structure - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_RESOURCE_REGISTER_FAILED \n - * ::NV_ENC_ERR_GENERIC \n - * ::NV_ENC_ERR_UNIMPLEMENTED \n - * - */ -NVENCSTATUS NVENCAPI NvEncRegisterResource (void* encoder, NV_ENC_REGISTER_RESOURCE* registerResParams); - -// NvEncUnregisterResource -/** - * \brief Unregisters a resource previously registered with the Nvidia Video Encoder Interface. - * - * Unregisters a resource previously registered with the Nvidia Video Encoder Interface. - * The client is expected to unregister any resource that it has registered with the - * Nvidia Video Encoder Interface before destroying the resource. - * - * \param [in] encoder - * Pointer to the NVEncodeAPI interface. - * - * \param [in] registeredResource - * The registered resource pointer that was returned in ::NvEncRegisterResource. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_RESOURCE_NOT_REGISTERED \n - * ::NV_ENC_ERR_GENERIC \n - * ::NV_ENC_ERR_UNIMPLEMENTED \n - * - */ -NVENCSTATUS NVENCAPI NvEncUnregisterResource (void* encoder, NV_ENC_REGISTERED_PTR registeredResource); - -// NvEncReconfigureEncoder -/** - * \brief Reconfigure an existing encoding session. - * - * Reconfigure an existing encoding session. - * The client should call this API to change/reconfigure the parameter passed during - * NvEncInitializeEncoder API call. - * Currently Reconfiguration of following are not supported. - * Change in GOP structure. - * Change in sync-Async mode. - * Change in MaxWidth & MaxHeight. - * Change in PTD mode. - * - * Resolution change is possible only if maxEncodeWidth & maxEncodeHeight of NV_ENC_INITIALIZE_PARAMS - * is set while creating encoder session. - * - * \param [in] encoder - * Pointer to the NVEncodeAPI interface. - * - * \param [in] reInitEncodeParams - * Pointer to a ::NV_ENC_RECONFIGURE_PARAMS structure. - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_NO_ENCODE_DEVICE \n - * ::NV_ENC_ERR_UNSUPPORTED_DEVICE \n - * ::NV_ENC_ERR_INVALID_DEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_GENERIC \n - * - */ -NVENCSTATUS NVENCAPI NvEncReconfigureEncoder (void *encoder, NV_ENC_RECONFIGURE_PARAMS* reInitEncodeParams); - - - -// NvEncCreateMVBuffer -/** - * \brief Allocates output MV buffer for ME only mode. - * - * This function is used to allocate an output MV buffer. The size of the mvBuffer is - * dependent on the frame height and width of the last ::NvEncCreateInputBuffer() call. - * The NV_ENC_OUTPUT_PTR returned by the NvEncodeAPI interface in the - * ::NV_ENC_CREATE_MV_BUFFER::mvBuffer field should be used in - * ::NvEncRunMotionEstimationOnly() API. - * Client must lock ::NV_ENC_CREATE_MV_BUFFER::mvBuffer using ::NvEncLockBitstream() API to get the motion vector data. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in,out] createMVBufferParams - * Pointer to the ::NV_ENC_CREATE_MV_BUFFER structure. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_GENERIC \n - */ -NVENCSTATUS NVENCAPI NvEncCreateMVBuffer (void* encoder, NV_ENC_CREATE_MV_BUFFER* createMVBufferParams); - - -// NvEncDestroyMVBuffer -/** - * \brief Release an output MV buffer for ME only mode. - * - * This function is used to release the output MV buffer allocated using - * the ::NvEncCreateMVBuffer() function. The client must release the output - * mvBuffer using this function before destroying the encoder session. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] mvBuffer - * Pointer to the mvBuffer being released. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_GENERIC \n - */ -NVENCSTATUS NVENCAPI NvEncDestroyMVBuffer (void* encoder, NV_ENC_OUTPUT_PTR mvBuffer); - - -// NvEncRunMotionEstimationOnly -/** - * \brief Submit an input picture and reference frame for motion estimation in ME only mode. - * - * This function is used to submit the input frame and reference frame for motion - * estimation. The ME parameters are passed using *meOnlyParams which is a pointer - * to ::_NV_ENC_MEONLY_PARAMS structure. - * Client must lock ::NV_ENC_CREATE_MV_BUFFER::mvBuffer using ::NvEncLockBitstream() API to get the motion vector data. - * to get motion vector data. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * \param [in] meOnlyParams - * Pointer to the ::_NV_ENC_MEONLY_PARAMS structure. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n - * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n - * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n - * ::NV_ENC_ERR_OUT_OF_MEMORY \n - * ::NV_ENC_ERR_INVALID_PARAM \n - * ::NV_ENC_ERR_INVALID_VERSION \n - * ::NV_ENC_ERR_NEED_MORE_INPUT \n - * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n - * ::NV_ENC_ERR_GENERIC \n - */ -NVENCSTATUS NVENCAPI NvEncRunMotionEstimationOnly (void* encoder, NV_ENC_MEONLY_PARAMS* meOnlyParams); - -// NvEncodeAPIGetMaxSupportedVersion -/** - * \brief Get the largest NvEncodeAPI version supported by the driver. - * - * This function can be used by clients to determine if the driver supports - * the NvEncodeAPI header the application was compiled with. - * - * \param [out] version - * Pointer to the requested value. The 4 least significant bits in the returned - * indicate the minor version and the rest of the bits indicate the major - * version of the largest supported version. - * - * \return - * ::NV_ENC_SUCCESS \n - * ::NV_ENC_ERR_INVALID_PTR \n - */ -NVENCSTATUS NVENCAPI NvEncodeAPIGetMaxSupportedVersion (uint32_t* version); - - -// NvEncGetLastErrorString -/** - * \brief Get the description of the last error reported by the API. - * - * This function returns a null-terminated string that can be used by clients to better understand the reason - * for failure of a previous API call. - * - * \param [in] encoder - * Pointer to the NvEncodeAPI interface. - * - * \return - * Pointer to buffer containing the details of the last error encountered by the API. - */ -const char * NVENCAPI NvEncGetLastErrorString (void* encoder); - - -/// \cond API PFN -/* - * Defines API function pointers - */ -typedef NVENCSTATUS (NVENCAPI* PNVENCOPENENCODESESSION) (void* device, uint32_t deviceType, void** encoder); -typedef NVENCSTATUS (NVENCAPI* PNVENCGETENCODEGUIDCOUNT) (void* encoder, uint32_t* encodeGUIDCount); -typedef NVENCSTATUS (NVENCAPI* PNVENCGETENCODEGUIDS) (void* encoder, GUID* GUIDs, uint32_t guidArraySize, uint32_t* GUIDCount); -typedef NVENCSTATUS (NVENCAPI* PNVENCGETENCODEPROFILEGUIDCOUNT) (void* encoder, GUID encodeGUID, uint32_t* encodeProfileGUIDCount); -typedef NVENCSTATUS (NVENCAPI* PNVENCGETENCODEPROFILEGUIDS) (void* encoder, GUID encodeGUID, GUID* profileGUIDs, uint32_t guidArraySize, uint32_t* GUIDCount); -typedef NVENCSTATUS (NVENCAPI* PNVENCGETINPUTFORMATCOUNT) (void* encoder, GUID encodeGUID, uint32_t* inputFmtCount); -typedef NVENCSTATUS (NVENCAPI* PNVENCGETINPUTFORMATS) (void* encoder, GUID encodeGUID, NV_ENC_BUFFER_FORMAT* inputFmts, uint32_t inputFmtArraySize, uint32_t* inputFmtCount); -typedef NVENCSTATUS (NVENCAPI* PNVENCGETENCODECAPS) (void* encoder, GUID encodeGUID, NV_ENC_CAPS_PARAM* capsParam, int* capsVal); -typedef NVENCSTATUS (NVENCAPI* PNVENCGETENCODEPRESETCOUNT) (void* encoder, GUID encodeGUID, uint32_t* encodePresetGUIDCount); -typedef NVENCSTATUS (NVENCAPI* PNVENCGETENCODEPRESETGUIDS) (void* encoder, GUID encodeGUID, GUID* presetGUIDs, uint32_t guidArraySize, uint32_t* encodePresetGUIDCount); -typedef NVENCSTATUS (NVENCAPI* PNVENCGETENCODEPRESETCONFIG) (void* encoder, GUID encodeGUID, GUID presetGUID, NV_ENC_PRESET_CONFIG* presetConfig); -typedef NVENCSTATUS (NVENCAPI* PNVENCGETENCODEPRESETCONFIGEX) (void* encoder, GUID encodeGUID, GUID presetGUID, NV_ENC_TUNING_INFO tuningInfo, NV_ENC_PRESET_CONFIG* presetConfig); -typedef NVENCSTATUS (NVENCAPI* PNVENCINITIALIZEENCODER) (void* encoder, NV_ENC_INITIALIZE_PARAMS* createEncodeParams); -typedef NVENCSTATUS (NVENCAPI* PNVENCCREATEINPUTBUFFER) (void* encoder, NV_ENC_CREATE_INPUT_BUFFER* createInputBufferParams); -typedef NVENCSTATUS (NVENCAPI* PNVENCDESTROYINPUTBUFFER) (void* encoder, NV_ENC_INPUT_PTR inputBuffer); -typedef NVENCSTATUS (NVENCAPI* PNVENCCREATEBITSTREAMBUFFER) (void* encoder, NV_ENC_CREATE_BITSTREAM_BUFFER* createBitstreamBufferParams); -typedef NVENCSTATUS (NVENCAPI* PNVENCDESTROYBITSTREAMBUFFER) (void* encoder, NV_ENC_OUTPUT_PTR bitstreamBuffer); -typedef NVENCSTATUS (NVENCAPI* PNVENCENCODEPICTURE) (void* encoder, NV_ENC_PIC_PARAMS* encodePicParams); -typedef NVENCSTATUS (NVENCAPI* PNVENCLOCKBITSTREAM) (void* encoder, NV_ENC_LOCK_BITSTREAM* lockBitstreamBufferParams); -typedef NVENCSTATUS (NVENCAPI* PNVENCUNLOCKBITSTREAM) (void* encoder, NV_ENC_OUTPUT_PTR bitstreamBuffer); -typedef NVENCSTATUS (NVENCAPI* PNVENCLOCKINPUTBUFFER) (void* encoder, NV_ENC_LOCK_INPUT_BUFFER* lockInputBufferParams); -typedef NVENCSTATUS (NVENCAPI* PNVENCUNLOCKINPUTBUFFER) (void* encoder, NV_ENC_INPUT_PTR inputBuffer); -typedef NVENCSTATUS (NVENCAPI* PNVENCGETENCODESTATS) (void* encoder, NV_ENC_STAT* encodeStats); -typedef NVENCSTATUS (NVENCAPI* PNVENCGETSEQUENCEPARAMS) (void* encoder, NV_ENC_SEQUENCE_PARAM_PAYLOAD* sequenceParamPayload); -typedef NVENCSTATUS (NVENCAPI* PNVENCREGISTERASYNCEVENT) (void* encoder, NV_ENC_EVENT_PARAMS* eventParams); -typedef NVENCSTATUS (NVENCAPI* PNVENCUNREGISTERASYNCEVENT) (void* encoder, NV_ENC_EVENT_PARAMS* eventParams); -typedef NVENCSTATUS (NVENCAPI* PNVENCMAPINPUTRESOURCE) (void* encoder, NV_ENC_MAP_INPUT_RESOURCE* mapInputResParams); -typedef NVENCSTATUS (NVENCAPI* PNVENCUNMAPINPUTRESOURCE) (void* encoder, NV_ENC_INPUT_PTR mappedInputBuffer); -typedef NVENCSTATUS (NVENCAPI* PNVENCDESTROYENCODER) (void* encoder); -typedef NVENCSTATUS (NVENCAPI* PNVENCINVALIDATEREFFRAMES) (void* encoder, uint64_t invalidRefFrameTimeStamp); -typedef NVENCSTATUS (NVENCAPI* PNVENCOPENENCODESESSIONEX) (NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS *openSessionExParams, void** encoder); -typedef NVENCSTATUS (NVENCAPI* PNVENCREGISTERRESOURCE) (void* encoder, NV_ENC_REGISTER_RESOURCE* registerResParams); -typedef NVENCSTATUS (NVENCAPI* PNVENCUNREGISTERRESOURCE) (void* encoder, NV_ENC_REGISTERED_PTR registeredRes); -typedef NVENCSTATUS (NVENCAPI* PNVENCRECONFIGUREENCODER) (void* encoder, NV_ENC_RECONFIGURE_PARAMS* reInitEncodeParams); - -typedef NVENCSTATUS (NVENCAPI* PNVENCCREATEMVBUFFER) (void* encoder, NV_ENC_CREATE_MV_BUFFER* createMVBufferParams); -typedef NVENCSTATUS (NVENCAPI* PNVENCDESTROYMVBUFFER) (void* encoder, NV_ENC_OUTPUT_PTR mvBuffer); -typedef NVENCSTATUS (NVENCAPI* PNVENCRUNMOTIONESTIMATIONONLY) (void* encoder, NV_ENC_MEONLY_PARAMS* meOnlyParams); -typedef const char * (NVENCAPI* PNVENCGETLASTERROR) (void* encoder); -typedef NVENCSTATUS (NVENCAPI* PNVENCSETIOCUDASTREAMS) (void* encoder, NV_ENC_CUSTREAM_PTR inputStream, NV_ENC_CUSTREAM_PTR outputStream); -typedef NVENCSTATUS (NVENCAPI* PNVENCGETSEQUENCEPARAMEX) (void* encoder, NV_ENC_INITIALIZE_PARAMS* encInitParams, NV_ENC_SEQUENCE_PARAM_PAYLOAD* sequenceParamPayload); - - -/// \endcond - - -/** @} */ /* END ENCODE_FUNC */ - -/** - * \ingroup ENCODER_STRUCTURE - * NV_ENCODE_API_FUNCTION_LIST - */ -typedef struct _NV_ENCODE_API_FUNCTION_LIST -{ - uint32_t version; /**< [in]: Client should pass NV_ENCODE_API_FUNCTION_LIST_VER. */ - uint32_t reserved; /**< [in]: Reserved and should be set to 0. */ - PNVENCOPENENCODESESSION nvEncOpenEncodeSession; /**< [out]: Client should access ::NvEncOpenEncodeSession() API through this pointer. */ - PNVENCGETENCODEGUIDCOUNT nvEncGetEncodeGUIDCount; /**< [out]: Client should access ::NvEncGetEncodeGUIDCount() API through this pointer. */ - PNVENCGETENCODEPRESETCOUNT nvEncGetEncodeProfileGUIDCount; /**< [out]: Client should access ::NvEncGetEncodeProfileGUIDCount() API through this pointer.*/ - PNVENCGETENCODEPRESETGUIDS nvEncGetEncodeProfileGUIDs; /**< [out]: Client should access ::NvEncGetEncodeProfileGUIDs() API through this pointer. */ - PNVENCGETENCODEGUIDS nvEncGetEncodeGUIDs; /**< [out]: Client should access ::NvEncGetEncodeGUIDs() API through this pointer. */ - PNVENCGETINPUTFORMATCOUNT nvEncGetInputFormatCount; /**< [out]: Client should access ::NvEncGetInputFormatCount() API through this pointer. */ - PNVENCGETINPUTFORMATS nvEncGetInputFormats; /**< [out]: Client should access ::NvEncGetInputFormats() API through this pointer. */ - PNVENCGETENCODECAPS nvEncGetEncodeCaps; /**< [out]: Client should access ::NvEncGetEncodeCaps() API through this pointer. */ - PNVENCGETENCODEPRESETCOUNT nvEncGetEncodePresetCount; /**< [out]: Client should access ::NvEncGetEncodePresetCount() API through this pointer. */ - PNVENCGETENCODEPRESETGUIDS nvEncGetEncodePresetGUIDs; /**< [out]: Client should access ::NvEncGetEncodePresetGUIDs() API through this pointer. */ - PNVENCGETENCODEPRESETCONFIG nvEncGetEncodePresetConfig; /**< [out]: Client should access ::NvEncGetEncodePresetConfig() API through this pointer. */ - PNVENCINITIALIZEENCODER nvEncInitializeEncoder; /**< [out]: Client should access ::NvEncInitializeEncoder() API through this pointer. */ - PNVENCCREATEINPUTBUFFER nvEncCreateInputBuffer; /**< [out]: Client should access ::NvEncCreateInputBuffer() API through this pointer. */ - PNVENCDESTROYINPUTBUFFER nvEncDestroyInputBuffer; /**< [out]: Client should access ::NvEncDestroyInputBuffer() API through this pointer. */ - PNVENCCREATEBITSTREAMBUFFER nvEncCreateBitstreamBuffer; /**< [out]: Client should access ::NvEncCreateBitstreamBuffer() API through this pointer. */ - PNVENCDESTROYBITSTREAMBUFFER nvEncDestroyBitstreamBuffer; /**< [out]: Client should access ::NvEncDestroyBitstreamBuffer() API through this pointer. */ - PNVENCENCODEPICTURE nvEncEncodePicture; /**< [out]: Client should access ::NvEncEncodePicture() API through this pointer. */ - PNVENCLOCKBITSTREAM nvEncLockBitstream; /**< [out]: Client should access ::NvEncLockBitstream() API through this pointer. */ - PNVENCUNLOCKBITSTREAM nvEncUnlockBitstream; /**< [out]: Client should access ::NvEncUnlockBitstream() API through this pointer. */ - PNVENCLOCKINPUTBUFFER nvEncLockInputBuffer; /**< [out]: Client should access ::NvEncLockInputBuffer() API through this pointer. */ - PNVENCUNLOCKINPUTBUFFER nvEncUnlockInputBuffer; /**< [out]: Client should access ::NvEncUnlockInputBuffer() API through this pointer. */ - PNVENCGETENCODESTATS nvEncGetEncodeStats; /**< [out]: Client should access ::NvEncGetEncodeStats() API through this pointer. */ - PNVENCGETSEQUENCEPARAMS nvEncGetSequenceParams; /**< [out]: Client should access ::NvEncGetSequenceParams() API through this pointer. */ - PNVENCREGISTERASYNCEVENT nvEncRegisterAsyncEvent; /**< [out]: Client should access ::NvEncRegisterAsyncEvent() API through this pointer. */ - PNVENCUNREGISTERASYNCEVENT nvEncUnregisterAsyncEvent; /**< [out]: Client should access ::NvEncUnregisterAsyncEvent() API through this pointer. */ - PNVENCMAPINPUTRESOURCE nvEncMapInputResource; /**< [out]: Client should access ::NvEncMapInputResource() API through this pointer. */ - PNVENCUNMAPINPUTRESOURCE nvEncUnmapInputResource; /**< [out]: Client should access ::NvEncUnmapInputResource() API through this pointer. */ - PNVENCDESTROYENCODER nvEncDestroyEncoder; /**< [out]: Client should access ::NvEncDestroyEncoder() API through this pointer. */ - PNVENCINVALIDATEREFFRAMES nvEncInvalidateRefFrames; /**< [out]: Client should access ::NvEncInvalidateRefFrames() API through this pointer. */ - PNVENCOPENENCODESESSIONEX nvEncOpenEncodeSessionEx; /**< [out]: Client should access ::NvEncOpenEncodeSession() API through this pointer. */ - PNVENCREGISTERRESOURCE nvEncRegisterResource; /**< [out]: Client should access ::NvEncRegisterResource() API through this pointer. */ - PNVENCUNREGISTERRESOURCE nvEncUnregisterResource; /**< [out]: Client should access ::NvEncUnregisterResource() API through this pointer. */ - PNVENCRECONFIGUREENCODER nvEncReconfigureEncoder; /**< [out]: Client should access ::NvEncReconfigureEncoder() API through this pointer. */ - void* reserved1; - PNVENCCREATEMVBUFFER nvEncCreateMVBuffer; /**< [out]: Client should access ::NvEncCreateMVBuffer API through this pointer. */ - PNVENCDESTROYMVBUFFER nvEncDestroyMVBuffer; /**< [out]: Client should access ::NvEncDestroyMVBuffer API through this pointer. */ - PNVENCRUNMOTIONESTIMATIONONLY nvEncRunMotionEstimationOnly; /**< [out]: Client should access ::NvEncRunMotionEstimationOnly API through this pointer. */ - PNVENCGETLASTERROR nvEncGetLastErrorString; /**< [out]: Client should access ::nvEncGetLastErrorString API through this pointer. */ - PNVENCSETIOCUDASTREAMS nvEncSetIOCudaStreams; /**< [out]: Client should access ::nvEncSetIOCudaStreams API through this pointer. */ - PNVENCGETENCODEPRESETCONFIGEX nvEncGetEncodePresetConfigEx; /**< [out]: Client should access ::NvEncGetEncodePresetConfigEx() API through this pointer. */ - PNVENCGETSEQUENCEPARAMEX nvEncGetSequenceParamEx; /**< [out]: Client should access ::NvEncGetSequenceParamEx() API through this pointer. */ - void* reserved2[277]; /**< [in]: Reserved and must be set to NULL */ -} NV_ENCODE_API_FUNCTION_LIST; - -/** Macro for constructing the version field of ::_NV_ENCODEAPI_FUNCTION_LIST. */ -#define NV_ENCODE_API_FUNCTION_LIST_VER NVENCAPI_STRUCT_VERSION(2) - -// NvEncodeAPICreateInstance -/** - * \ingroup ENCODE_FUNC - * Entry Point to the NvEncodeAPI interface. - * - * Creates an instance of the NvEncodeAPI interface, and populates the - * pFunctionList with function pointers to the API routines implemented by the - * NvEncodeAPI interface. - * - * \param [out] functionList - * - * \return - * ::NV_ENC_SUCCESS - * ::NV_ENC_ERR_INVALID_PTR - */ -NVENCSTATUS NVENCAPI NvEncodeAPICreateInstance(NV_ENCODE_API_FUNCTION_LIST *functionList); - -#ifdef __cplusplus -} -#endif - - -#endif - +/* + * This copyright notice applies to this header file only: + * + * Copyright (c) 2010-2022 NVIDIA Corporation + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the software, and to permit persons to whom the + * software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +/** + * \file nvEncodeAPI.h + * NVIDIA GPUs - beginning with the Kepler generation - contain a hardware-based encoder + * (referred to as NVENC) which provides fully-accelerated hardware-based video encoding. + * NvEncodeAPI provides the interface for NVIDIA video encoder (NVENC). + * \date 2011-2022 + * This file contains the interface constants, structure definitions and function prototypes. + */ + +#ifndef _NV_ENCODEAPI_H_ +#define _NV_ENCODEAPI_H_ + +#include + +#ifdef _WIN32 +#include +#endif + +#ifdef _MSC_VER +#ifndef _STDINT +typedef __int32 int32_t; +typedef unsigned __int32 uint32_t; +typedef __int64 int64_t; +typedef unsigned __int64 uint64_t; +typedef signed char int8_t; +typedef unsigned char uint8_t; +typedef short int16_t; +typedef unsigned short uint16_t; +#endif +#else +#include +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * \addtogroup ENCODER_STRUCTURE NvEncodeAPI Data structures + * @{ + */ + +#ifdef _WIN32 +#define NVENCAPI __stdcall +typedef RECT NVENC_RECT; +#else +#define NVENCAPI +// ========================================================================================= +#ifndef GUID_DEFINED +#define GUID_DEFINED +/*! + * \struct GUID + * Abstracts the GUID structure for non-windows platforms. + */ +// ========================================================================================= +typedef struct _GUID { + uint32_t + Data1; /**< [in]: Specifies the first 8 hexadecimal digits of the GUID. */ + uint16_t + Data2; /**< [in]: Specifies the first group of 4 hexadecimal digits. */ + uint16_t + Data3; /**< [in]: Specifies the second group of 4 hexadecimal digits. */ + uint8_t Data4 + [8]; /**< [in]: Array of 8 bytes. The first 2 bytes contain the third group of 4 hexadecimal digits. + The remaining 6 bytes contain the final 12 hexadecimal digits. */ +} GUID, *LPGUID; +#endif // GUID + +/** + * \struct _NVENC_RECT + * Defines a Rectangle. Used in ::NV_ENC_PREPROCESS_FRAME. + */ +typedef struct _NVENC_RECT { + uint32_t + left; /**< [in]: X coordinate of the upper left corner of rectangular area to be specified. */ + uint32_t + top; /**< [in]: Y coordinate of the upper left corner of the rectangular area to be specified. */ + uint32_t + right; /**< [in]: X coordinate of the bottom right corner of the rectangular area to be specified. */ + uint32_t + bottom; /**< [in]: Y coordinate of the bottom right corner of the rectangular area to be specified. */ +} NVENC_RECT; + +#endif // _WIN32 + +/** @} */ /* End of GUID and NVENC_RECT structure grouping*/ + +typedef void* + NV_ENC_INPUT_PTR; /**< NVENCODE API input buffer */ +typedef void* NV_ENC_OUTPUT_PTR; /**< NVENCODE API output buffer*/ +typedef void* + NV_ENC_REGISTERED_PTR; /**< A Resource that has been registered with NVENCODE API*/ +typedef void* NV_ENC_CUSTREAM_PTR; /**< Pointer to CUstream*/ + +#define NVENCAPI_MAJOR_VERSION 12 +#define NVENCAPI_MINOR_VERSION 0 + +#define NVENCAPI_VERSION \ + (NVENCAPI_MAJOR_VERSION | (NVENCAPI_MINOR_VERSION << 24)) + +/** + * Macro to generate per-structure version for use with API. + */ +#define NVENCAPI_STRUCT_VERSION(ver) \ + ((uint32_t)NVENCAPI_VERSION | ((ver) << 16) | (0x7 << 28)) + +#define NVENC_INFINITE_GOPLENGTH 0xffffffff + +#define NV_MAX_SEQ_HDR_LEN (512) + +#ifdef __GNUC__ +#define NV_ENC_DEPRECATED \ + __attribute__(( \ + deprecated("WILL BE REMOVED IN A FUTURE VIDEO CODEC SDK VERSION"))) +#elif defined(_MSC_VER) +#define NV_ENC_DEPRECATED \ + __declspec(deprecated("WILL BE REMOVED IN A FUTURE VIDEO CODEC SDK " \ + "VERSION")) +#endif + +// ========================================================================================= +// Encode Codec GUIDS supported by the NvEncodeAPI interface. +// ========================================================================================= + +// {6BC82762-4E63-4ca4-AA85-1E50F321F6BF} +static const GUID NV_ENC_CODEC_H264_GUID = { + 0x6bc82762, + 0x4e63, + 0x4ca4, + {0xaa, 0x85, 0x1e, 0x50, 0xf3, 0x21, 0xf6, 0xbf}}; + +// {790CDC88-4522-4d7b-9425-BDA9975F7603} +static const GUID NV_ENC_CODEC_HEVC_GUID = { + 0x790cdc88, + 0x4522, + 0x4d7b, + {0x94, 0x25, 0xbd, 0xa9, 0x97, 0x5f, 0x76, 0x3}}; + +// {0A352289-0AA7-4759-862D-5D15CD16D254} +static const GUID NV_ENC_CODEC_AV1_GUID = { + 0x0a352289, + 0x0aa7, + 0x4759, + {0x86, 0x2d, 0x5d, 0x15, 0xcd, 0x16, 0xd2, 0x54}}; + +// ========================================================================================= +// * Encode Profile GUIDS supported by the NvEncodeAPI interface. +// ========================================================================================= + +// {BFD6F8E7-233C-4341-8B3E-4818523803F4} +static const GUID NV_ENC_CODEC_PROFILE_AUTOSELECT_GUID = { + 0xbfd6f8e7, + 0x233c, + 0x4341, + {0x8b, 0x3e, 0x48, 0x18, 0x52, 0x38, 0x3, 0xf4}}; + +// {0727BCAA-78C4-4c83-8C2F-EF3DFF267C6A} +static const GUID NV_ENC_H264_PROFILE_BASELINE_GUID = { + 0x727bcaa, + 0x78c4, + 0x4c83, + {0x8c, 0x2f, 0xef, 0x3d, 0xff, 0x26, 0x7c, 0x6a}}; + +// {60B5C1D4-67FE-4790-94D5-C4726D7B6E6D} +static const GUID NV_ENC_H264_PROFILE_MAIN_GUID = { + 0x60b5c1d4, + 0x67fe, + 0x4790, + {0x94, 0xd5, 0xc4, 0x72, 0x6d, 0x7b, 0x6e, 0x6d}}; + +// {E7CBC309-4F7A-4b89-AF2A-D537C92BE310} +static const GUID NV_ENC_H264_PROFILE_HIGH_GUID = { + 0xe7cbc309, + 0x4f7a, + 0x4b89, + {0xaf, 0x2a, 0xd5, 0x37, 0xc9, 0x2b, 0xe3, 0x10}}; + +// {7AC663CB-A598-4960-B844-339B261A7D52} +static const GUID NV_ENC_H264_PROFILE_HIGH_444_GUID = { + 0x7ac663cb, + 0xa598, + 0x4960, + {0xb8, 0x44, 0x33, 0x9b, 0x26, 0x1a, 0x7d, 0x52}}; + +// {40847BF5-33F7-4601-9084-E8FE3C1DB8B7} +static const GUID NV_ENC_H264_PROFILE_STEREO_GUID = { + 0x40847bf5, + 0x33f7, + 0x4601, + {0x90, 0x84, 0xe8, 0xfe, 0x3c, 0x1d, 0xb8, 0xb7}}; + +// {B405AFAC-F32B-417B-89C4-9ABEED3E5978} +static const GUID NV_ENC_H264_PROFILE_PROGRESSIVE_HIGH_GUID = { + 0xb405afac, + 0xf32b, + 0x417b, + {0x89, 0xc4, 0x9a, 0xbe, 0xed, 0x3e, 0x59, 0x78}}; + +// {AEC1BD87-E85B-48f2-84C3-98BCA6285072} +static const GUID NV_ENC_H264_PROFILE_CONSTRAINED_HIGH_GUID = { + 0xaec1bd87, + 0xe85b, + 0x48f2, + {0x84, 0xc3, 0x98, 0xbc, 0xa6, 0x28, 0x50, 0x72}}; + +// {B514C39A-B55B-40fa-878F-F1253B4DFDEC} +static const GUID NV_ENC_HEVC_PROFILE_MAIN_GUID = { + 0xb514c39a, + 0xb55b, + 0x40fa, + {0x87, 0x8f, 0xf1, 0x25, 0x3b, 0x4d, 0xfd, 0xec}}; + +// {fa4d2b6c-3a5b-411a-8018-0a3f5e3c9be5} +static const GUID NV_ENC_HEVC_PROFILE_MAIN10_GUID = { + 0xfa4d2b6c, + 0x3a5b, + 0x411a, + {0x80, 0x18, 0x0a, 0x3f, 0x5e, 0x3c, 0x9b, 0xe5}}; + +// For HEVC Main 444 8 bit and HEVC Main 444 10 bit profiles only +// {51ec32b5-1b4c-453c-9cbd-b616bd621341} +static const GUID NV_ENC_HEVC_PROFILE_FREXT_GUID = { + 0x51ec32b5, + 0x1b4c, + 0x453c, + {0x9c, 0xbd, 0xb6, 0x16, 0xbd, 0x62, 0x13, 0x41}}; + +// {5f2a39f5-f14e-4f95-9a9e-b76d568fcf97} +static const GUID NV_ENC_AV1_PROFILE_MAIN_GUID = { + 0x5f2a39f5, + 0xf14e, + 0x4f95, + {0x9a, 0x9e, 0xb7, 0x6d, 0x56, 0x8f, 0xcf, 0x97}}; + +// ========================================================================================= +// * Preset GUIDS supported by the NvEncodeAPI interface. +// ========================================================================================= +// {B2DFB705-4EBD-4C49-9B5F-24A777D3E587} +NV_ENC_DEPRECATED static const GUID NV_ENC_PRESET_DEFAULT_GUID = { + 0xb2dfb705, + 0x4ebd, + 0x4c49, + {0x9b, 0x5f, 0x24, 0xa7, 0x77, 0xd3, 0xe5, 0x87}}; + +// {60E4C59F-E846-4484-A56D-CD45BE9FDDF6} +NV_ENC_DEPRECATED static const GUID NV_ENC_PRESET_HP_GUID = { + 0x60e4c59f, + 0xe846, + 0x4484, + {0xa5, 0x6d, 0xcd, 0x45, 0xbe, 0x9f, 0xdd, 0xf6}}; + +// {34DBA71D-A77B-4B8F-9C3E-B6D5DA24C012} +NV_ENC_DEPRECATED static const GUID NV_ENC_PRESET_HQ_GUID = { + 0x34dba71d, + 0xa77b, + 0x4b8f, + {0x9c, 0x3e, 0xb6, 0xd5, 0xda, 0x24, 0xc0, 0x12}}; + +// {82E3E450-BDBB-4e40-989C-82A90DF9EF32} +NV_ENC_DEPRECATED static const GUID NV_ENC_PRESET_BD_GUID = { + 0x82e3e450, + 0xbdbb, + 0x4e40, + {0x98, 0x9c, 0x82, 0xa9, 0xd, 0xf9, 0xef, 0x32}}; + +// {49DF21C5-6DFA-4feb-9787-6ACC9EFFB726} +NV_ENC_DEPRECATED static const GUID NV_ENC_PRESET_LOW_LATENCY_DEFAULT_GUID = { + 0x49df21c5, + 0x6dfa, + 0x4feb, + {0x97, 0x87, 0x6a, 0xcc, 0x9e, 0xff, 0xb7, 0x26}}; + +// {C5F733B9-EA97-4cf9-BEC2-BF78A74FD105} +NV_ENC_DEPRECATED static const GUID NV_ENC_PRESET_LOW_LATENCY_HQ_GUID = { + 0xc5f733b9, + 0xea97, + 0x4cf9, + {0xbe, 0xc2, 0xbf, 0x78, 0xa7, 0x4f, 0xd1, 0x5}}; + +// {67082A44-4BAD-48FA-98EA-93056D150A58} +NV_ENC_DEPRECATED static const GUID NV_ENC_PRESET_LOW_LATENCY_HP_GUID = { + 0x67082a44, + 0x4bad, + 0x48fa, + {0x98, 0xea, 0x93, 0x5, 0x6d, 0x15, 0xa, 0x58}}; + +// {D5BFB716-C604-44e7-9BB8-DEA5510FC3AC} +NV_ENC_DEPRECATED static const GUID NV_ENC_PRESET_LOSSLESS_DEFAULT_GUID = { + 0xd5bfb716, + 0xc604, + 0x44e7, + {0x9b, 0xb8, 0xde, 0xa5, 0x51, 0xf, 0xc3, 0xac}}; + +// {149998E7-2364-411d-82EF-179888093409} +NV_ENC_DEPRECATED static const GUID NV_ENC_PRESET_LOSSLESS_HP_GUID = { + 0x149998e7, + 0x2364, + 0x411d, + {0x82, 0xef, 0x17, 0x98, 0x88, 0x9, 0x34, 0x9}}; + +// Performance degrades and quality improves as we move from P1 to P7. Presets P3 to P7 for H264 and Presets P2 to P7 for HEVC have B frames enabled by default +// for HIGH_QUALITY and LOSSLESS tuning info, and will not work with Weighted Prediction enabled. In case Weighted Prediction is required, disable B frames by +// setting frameIntervalP = 1 +// {FC0A8D3E-45F8-4CF8-80C7-298871590EBF} +static const GUID NV_ENC_PRESET_P1_GUID = { + 0xfc0a8d3e, + 0x45f8, + 0x4cf8, + {0x80, 0xc7, 0x29, 0x88, 0x71, 0x59, 0xe, 0xbf}}; + +// {F581CFB8-88D6-4381-93F0-DF13F9C27DAB} +static const GUID NV_ENC_PRESET_P2_GUID = { + 0xf581cfb8, + 0x88d6, + 0x4381, + {0x93, 0xf0, 0xdf, 0x13, 0xf9, 0xc2, 0x7d, 0xab}}; + +// {36850110-3A07-441F-94D5-3670631F91F6} +static const GUID NV_ENC_PRESET_P3_GUID = { + 0x36850110, + 0x3a07, + 0x441f, + {0x94, 0xd5, 0x36, 0x70, 0x63, 0x1f, 0x91, 0xf6}}; + +// {90A7B826-DF06-4862-B9D2-CD6D73A08681} +static const GUID NV_ENC_PRESET_P4_GUID = { + 0x90a7b826, + 0xdf06, + 0x4862, + {0xb9, 0xd2, 0xcd, 0x6d, 0x73, 0xa0, 0x86, 0x81}}; + +// {21C6E6B4-297A-4CBA-998F-B6CBDE72ADE3} +static const GUID NV_ENC_PRESET_P5_GUID = { + 0x21c6e6b4, + 0x297a, + 0x4cba, + {0x99, 0x8f, 0xb6, 0xcb, 0xde, 0x72, 0xad, 0xe3}}; + +// {8E75C279-6299-4AB6-8302-0B215A335CF5} +static const GUID NV_ENC_PRESET_P6_GUID = { + 0x8e75c279, + 0x6299, + 0x4ab6, + {0x83, 0x2, 0xb, 0x21, 0x5a, 0x33, 0x5c, 0xf5}}; + +// {84848C12-6F71-4C13-931B-53E283F57974} +static const GUID NV_ENC_PRESET_P7_GUID = { + 0x84848c12, + 0x6f71, + 0x4c13, + {0x93, 0x1b, 0x53, 0xe2, 0x83, 0xf5, 0x79, 0x74}}; + +/** + * \addtogroup ENCODER_STRUCTURE NvEncodeAPI Data structures + * @{ + */ + +/** + * Input frame encode modes + */ +typedef enum _NV_ENC_PARAMS_FRAME_FIELD_MODE { + NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME = 0x01, /**< Frame mode */ + NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD = 0x02, /**< Field mode */ + NV_ENC_PARAMS_FRAME_FIELD_MODE_MBAFF = 0x03 /**< MB adaptive frame/field */ +} NV_ENC_PARAMS_FRAME_FIELD_MODE; + +/** + * Rate Control Modes + */ +typedef enum _NV_ENC_PARAMS_RC_MODE { + NV_ENC_PARAMS_RC_CONSTQP = 0x0, /**< Constant QP mode */ + NV_ENC_PARAMS_RC_VBR = 0x1, /**< Variable bitrate mode */ + NV_ENC_PARAMS_RC_CBR = 0x2, /**< Constant bitrate mode */ + NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ = + 0x8, /**< Deprecated, use NV_ENC_PARAMS_RC_CBR + NV_ENC_TWO_PASS_QUARTER_RESOLUTION / NV_ENC_TWO_PASS_FULL_RESOLUTION + + lowDelayKeyFrameScale=1 */ + NV_ENC_PARAMS_RC_CBR_HQ = + 0x10, /**< Deprecated, use NV_ENC_PARAMS_RC_CBR + NV_ENC_TWO_PASS_QUARTER_RESOLUTION / NV_ENC_TWO_PASS_FULL_RESOLUTION */ + NV_ENC_PARAMS_RC_VBR_HQ = + 0x20 /**< Deprecated, use NV_ENC_PARAMS_RC_VBR + NV_ENC_TWO_PASS_QUARTER_RESOLUTION / NV_ENC_TWO_PASS_FULL_RESOLUTION */ +} NV_ENC_PARAMS_RC_MODE; + +/** + * Multi Pass encoding + */ +typedef enum _NV_ENC_MULTI_PASS { + NV_ENC_MULTI_PASS_DISABLED = 0x0, /**< Single Pass */ + NV_ENC_TWO_PASS_QUARTER_RESOLUTION = + 0x1, /**< Two Pass encoding is enabled where first Pass is quarter resolution */ + NV_ENC_TWO_PASS_FULL_RESOLUTION = + 0x2, /**< Two Pass encoding is enabled where first Pass is full resolution */ +} NV_ENC_MULTI_PASS; + +/** + * Emphasis Levels + */ +typedef enum _NV_ENC_EMPHASIS_MAP_LEVEL { + NV_ENC_EMPHASIS_MAP_LEVEL_0 = + 0x0, /**< Emphasis Map Level 0, for zero Delta QP value */ + NV_ENC_EMPHASIS_MAP_LEVEL_1 = + 0x1, /**< Emphasis Map Level 1, for very low Delta QP value */ + NV_ENC_EMPHASIS_MAP_LEVEL_2 = + 0x2, /**< Emphasis Map Level 2, for low Delta QP value */ + NV_ENC_EMPHASIS_MAP_LEVEL_3 = + 0x3, /**< Emphasis Map Level 3, for medium Delta QP value */ + NV_ENC_EMPHASIS_MAP_LEVEL_4 = + 0x4, /**< Emphasis Map Level 4, for high Delta QP value */ + NV_ENC_EMPHASIS_MAP_LEVEL_5 = + 0x5 /**< Emphasis Map Level 5, for very high Delta QP value */ +} NV_ENC_EMPHASIS_MAP_LEVEL; + +/** + * QP MAP MODE + */ +typedef enum _NV_ENC_QP_MAP_MODE { + NV_ENC_QP_MAP_DISABLED = + 0x0, /**< Value in NV_ENC_PIC_PARAMS::qpDeltaMap have no effect. */ + NV_ENC_QP_MAP_EMPHASIS = + 0x1, /**< Value in NV_ENC_PIC_PARAMS::qpDeltaMap will be treated as Emphasis level. Currently this is only supported for H264 */ + NV_ENC_QP_MAP_DELTA = + 0x2, /**< Value in NV_ENC_PIC_PARAMS::qpDeltaMap will be treated as QP delta map. */ + NV_ENC_QP_MAP = + 0x3, /**< Currently This is not supported. Value in NV_ENC_PIC_PARAMS::qpDeltaMap will be treated as QP value. */ +} NV_ENC_QP_MAP_MODE; + +#define NV_ENC_PARAMS_RC_VBR_MINQP (NV_ENC_PARAMS_RC_MODE)0x4 /**< Deprecated */ +#define NV_ENC_PARAMS_RC_2_PASS_QUALITY \ + NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ /**< Deprecated */ +#define NV_ENC_PARAMS_RC_2_PASS_FRAMESIZE_CAP \ + NV_ENC_PARAMS_RC_CBR_HQ /**< Deprecated */ +#define NV_ENC_PARAMS_RC_2_PASS_VBR NV_ENC_PARAMS_RC_VBR_HQ /**< Deprecated */ +#define NV_ENC_PARAMS_RC_CBR2 NV_ENC_PARAMS_RC_CBR /**< Deprecated */ + +/** + * Input picture structure + */ +typedef enum _NV_ENC_PIC_STRUCT { + NV_ENC_PIC_STRUCT_FRAME = 0x01, /**< Progressive frame */ + NV_ENC_PIC_STRUCT_FIELD_TOP_BOTTOM = + 0x02, /**< Field encoding top field first */ + NV_ENC_PIC_STRUCT_FIELD_BOTTOM_TOP = + 0x03 /**< Field encoding bottom field first */ +} NV_ENC_PIC_STRUCT; + +/** + * Display picture structure + * Currently, this enum is only used for deciding the number of clock timestamp sets in Picture Timing SEI / Time Code SEI + * Otherwise, this has no impact on encoder behavior + */ +typedef enum _NV_ENC_DISPLAY_PIC_STRUCT { + NV_ENC_PIC_STRUCT_DISPLAY_FRAME = 0x00, /**< Field encoding top field first */ + NV_ENC_PIC_STRUCT_DISPLAY_FIELD_TOP_BOTTOM = + 0x01, /**< Field encoding top field first */ + NV_ENC_PIC_STRUCT_DISPLAY_FIELD_BOTTOM_TOP = + 0x02, /**< Field encoding bottom field first */ + NV_ENC_PIC_STRUCT_DISPLAY_FRAME_DOUBLING = 0x03, /**< Frame doubling */ + NV_ENC_PIC_STRUCT_DISPLAY_FRAME_TRIPLING = 0x04 /**< Field tripling */ +} NV_ENC_DISPLAY_PIC_STRUCT; + +/** + * Input picture type + */ +typedef enum _NV_ENC_PIC_TYPE { + NV_ENC_PIC_TYPE_P = 0x0, /**< Forward predicted */ + NV_ENC_PIC_TYPE_B = 0x01, /**< Bi-directionally predicted picture */ + NV_ENC_PIC_TYPE_I = 0x02, /**< Intra predicted picture */ + NV_ENC_PIC_TYPE_IDR = 0x03, /**< IDR picture */ + NV_ENC_PIC_TYPE_BI = + 0x04, /**< Bi-directionally predicted with only Intra MBs */ + NV_ENC_PIC_TYPE_SKIPPED = 0x05, /**< Picture is skipped */ + NV_ENC_PIC_TYPE_INTRA_REFRESH = + 0x06, /**< First picture in intra refresh cycle */ + NV_ENC_PIC_TYPE_NONREF_P = 0x07, /**< Non reference P picture */ + NV_ENC_PIC_TYPE_UNKNOWN = 0xFF /**< Picture type unknown */ +} NV_ENC_PIC_TYPE; + +/** + * Motion vector precisions + */ +typedef enum _NV_ENC_MV_PRECISION { + NV_ENC_MV_PRECISION_DEFAULT = + 0x0, /**< Driver selects Quarter-Pel motion vector precision by default */ + NV_ENC_MV_PRECISION_FULL_PEL = 0x01, /**< Full-Pel motion vector precision */ + NV_ENC_MV_PRECISION_HALF_PEL = 0x02, /**< Half-Pel motion vector precision */ + NV_ENC_MV_PRECISION_QUARTER_PEL = + 0x03 /**< Quarter-Pel motion vector precision */ +} NV_ENC_MV_PRECISION; + +/** + * Input buffer formats + */ +typedef enum _NV_ENC_BUFFER_FORMAT { + NV_ENC_BUFFER_FORMAT_UNDEFINED = 0x00000000, /**< Undefined buffer format */ + + NV_ENC_BUFFER_FORMAT_NV12 = + 0x00000001, /**< Semi-Planar YUV [Y plane followed by interleaved UV plane] */ + NV_ENC_BUFFER_FORMAT_YV12 = + 0x00000010, /**< Planar YUV [Y plane followed by V and U planes] */ + NV_ENC_BUFFER_FORMAT_IYUV = + 0x00000100, /**< Planar YUV [Y plane followed by U and V planes] */ + NV_ENC_BUFFER_FORMAT_YUV444 = + 0x00001000, /**< Planar YUV [Y plane followed by U and V planes] */ + NV_ENC_BUFFER_FORMAT_YUV420_10BIT = + 0x00010000, /**< 10 bit Semi-Planar YUV [Y plane followed by interleaved UV plane]. Each pixel of size 2 bytes. Most Significant 10 bits contain pixel data. */ + NV_ENC_BUFFER_FORMAT_YUV444_10BIT = + 0x00100000, /**< 10 bit Planar YUV444 [Y plane followed by U and V planes]. Each pixel of size 2 bytes. Most Significant 10 bits contain pixel data. */ + NV_ENC_BUFFER_FORMAT_ARGB = + 0x01000000, /**< 8 bit Packed A8R8G8B8. This is a word-ordered format + where a pixel is represented by a 32-bit word with B + in the lowest 8 bits, G in the next 8 bits, R in the + 8 bits after that and A in the highest 8 bits. */ + NV_ENC_BUFFER_FORMAT_ARGB10 = + 0x02000000, /**< 10 bit Packed A2R10G10B10. This is a word-ordered format + where a pixel is represented by a 32-bit word with B + in the lowest 10 bits, G in the next 10 bits, R in the + 10 bits after that and A in the highest 2 bits. */ + NV_ENC_BUFFER_FORMAT_AYUV = + 0x04000000, /**< 8 bit Packed A8Y8U8V8. This is a word-ordered format + where a pixel is represented by a 32-bit word with V + in the lowest 8 bits, U in the next 8 bits, Y in the + 8 bits after that and A in the highest 8 bits. */ + NV_ENC_BUFFER_FORMAT_ABGR = + 0x10000000, /**< 8 bit Packed A8B8G8R8. This is a word-ordered format + where a pixel is represented by a 32-bit word with R + in the lowest 8 bits, G in the next 8 bits, B in the + 8 bits after that and A in the highest 8 bits. */ + NV_ENC_BUFFER_FORMAT_ABGR10 = + 0x20000000, /**< 10 bit Packed A2B10G10R10. This is a word-ordered format + where a pixel is represented by a 32-bit word with R + in the lowest 10 bits, G in the next 10 bits, B in the + 10 bits after that and A in the highest 2 bits. */ + NV_ENC_BUFFER_FORMAT_U8 = + 0x40000000, /**< Buffer format representing one-dimensional buffer. + This format should be used only when registering the + resource as output buffer, which will be used to write + the encoded bit stream or H.264 ME only mode output. */ +} NV_ENC_BUFFER_FORMAT; + +#define NV_ENC_BUFFER_FORMAT_NV12_PL NV_ENC_BUFFER_FORMAT_NV12 +#define NV_ENC_BUFFER_FORMAT_YV12_PL NV_ENC_BUFFER_FORMAT_YV12 +#define NV_ENC_BUFFER_FORMAT_IYUV_PL NV_ENC_BUFFER_FORMAT_IYUV +#define NV_ENC_BUFFER_FORMAT_YUV444_PL NV_ENC_BUFFER_FORMAT_YUV444 + +/** + * Encoding levels + */ +typedef enum _NV_ENC_LEVEL { + NV_ENC_LEVEL_AUTOSELECT = 0, + + NV_ENC_LEVEL_H264_1 = 10, + NV_ENC_LEVEL_H264_1b = 9, + NV_ENC_LEVEL_H264_11 = 11, + NV_ENC_LEVEL_H264_12 = 12, + NV_ENC_LEVEL_H264_13 = 13, + NV_ENC_LEVEL_H264_2 = 20, + NV_ENC_LEVEL_H264_21 = 21, + NV_ENC_LEVEL_H264_22 = 22, + NV_ENC_LEVEL_H264_3 = 30, + NV_ENC_LEVEL_H264_31 = 31, + NV_ENC_LEVEL_H264_32 = 32, + NV_ENC_LEVEL_H264_4 = 40, + NV_ENC_LEVEL_H264_41 = 41, + NV_ENC_LEVEL_H264_42 = 42, + NV_ENC_LEVEL_H264_5 = 50, + NV_ENC_LEVEL_H264_51 = 51, + NV_ENC_LEVEL_H264_52 = 52, + NV_ENC_LEVEL_H264_60 = 60, + NV_ENC_LEVEL_H264_61 = 61, + NV_ENC_LEVEL_H264_62 = 62, + + NV_ENC_LEVEL_HEVC_1 = 30, + NV_ENC_LEVEL_HEVC_2 = 60, + NV_ENC_LEVEL_HEVC_21 = 63, + NV_ENC_LEVEL_HEVC_3 = 90, + NV_ENC_LEVEL_HEVC_31 = 93, + NV_ENC_LEVEL_HEVC_4 = 120, + NV_ENC_LEVEL_HEVC_41 = 123, + NV_ENC_LEVEL_HEVC_5 = 150, + NV_ENC_LEVEL_HEVC_51 = 153, + NV_ENC_LEVEL_HEVC_52 = 156, + NV_ENC_LEVEL_HEVC_6 = 180, + NV_ENC_LEVEL_HEVC_61 = 183, + NV_ENC_LEVEL_HEVC_62 = 186, + + NV_ENC_TIER_HEVC_MAIN = 0, + NV_ENC_TIER_HEVC_HIGH = 1, + + NV_ENC_LEVEL_AV1_2 = 0, + NV_ENC_LEVEL_AV1_21 = 1, + NV_ENC_LEVEL_AV1_22 = 2, + NV_ENC_LEVEL_AV1_23 = 3, + NV_ENC_LEVEL_AV1_3 = 4, + NV_ENC_LEVEL_AV1_31 = 5, + NV_ENC_LEVEL_AV1_32 = 6, + NV_ENC_LEVEL_AV1_33 = 7, + NV_ENC_LEVEL_AV1_4 = 8, + NV_ENC_LEVEL_AV1_41 = 9, + NV_ENC_LEVEL_AV1_42 = 10, + NV_ENC_LEVEL_AV1_43 = 11, + NV_ENC_LEVEL_AV1_5 = 12, + NV_ENC_LEVEL_AV1_51 = 13, + NV_ENC_LEVEL_AV1_52 = 14, + NV_ENC_LEVEL_AV1_53 = 15, + NV_ENC_LEVEL_AV1_6 = 16, + NV_ENC_LEVEL_AV1_61 = 17, + NV_ENC_LEVEL_AV1_62 = 18, + NV_ENC_LEVEL_AV1_63 = 19, + NV_ENC_LEVEL_AV1_7 = 20, + NV_ENC_LEVEL_AV1_71 = 21, + NV_ENC_LEVEL_AV1_72 = 22, + NV_ENC_LEVEL_AV1_73 = 23, + NV_ENC_LEVEL_AV1_AUTOSELECT, + + NV_ENC_TIER_AV1_0 = 0, + NV_ENC_TIER_AV1_1 = 1 +} NV_ENC_LEVEL; + +/** + * Error Codes + */ +typedef enum _NVENCSTATUS { + /** + * This indicates that API call returned with no errors. + */ + NV_ENC_SUCCESS, + + /** + * This indicates that no encode capable devices were detected. + */ + NV_ENC_ERR_NO_ENCODE_DEVICE, + + /** + * This indicates that devices pass by the client is not supported. + */ + NV_ENC_ERR_UNSUPPORTED_DEVICE, + + /** + * This indicates that the encoder device supplied by the client is not + * valid. + */ + NV_ENC_ERR_INVALID_ENCODERDEVICE, + + /** + * This indicates that device passed to the API call is invalid. + */ + NV_ENC_ERR_INVALID_DEVICE, + + /** + * This indicates that device passed to the API call is no longer available and + * needs to be reinitialized. The clients need to destroy the current encoder + * session by freeing the allocated input output buffers and destroying the device + * and create a new encoding session. + */ + NV_ENC_ERR_DEVICE_NOT_EXIST, + + /** + * This indicates that one or more of the pointers passed to the API call + * is invalid. + */ + NV_ENC_ERR_INVALID_PTR, + + /** + * This indicates that completion event passed in ::NvEncEncodePicture() call + * is invalid. + */ + NV_ENC_ERR_INVALID_EVENT, + + /** + * This indicates that one or more of the parameter passed to the API call + * is invalid. + */ + NV_ENC_ERR_INVALID_PARAM, + + /** + * This indicates that an API call was made in wrong sequence/order. + */ + NV_ENC_ERR_INVALID_CALL, + + /** + * This indicates that the API call failed because it was unable to allocate + * enough memory to perform the requested operation. + */ + NV_ENC_ERR_OUT_OF_MEMORY, + + /** + * This indicates that the encoder has not been initialized with + * ::NvEncInitializeEncoder() or that initialization has failed. + * The client cannot allocate input or output buffers or do any encoding + * related operation before successfully initializing the encoder. + */ + NV_ENC_ERR_ENCODER_NOT_INITIALIZED, + + /** + * This indicates that an unsupported parameter was passed by the client. + */ + NV_ENC_ERR_UNSUPPORTED_PARAM, + + /** + * This indicates that the ::NvEncLockBitstream() failed to lock the output + * buffer. This happens when the client makes a non blocking lock call to + * access the output bitstream by passing NV_ENC_LOCK_BITSTREAM::doNotWait flag. + * This is not a fatal error and client should retry the same operation after + * few milliseconds. + */ + NV_ENC_ERR_LOCK_BUSY, + + /** + * This indicates that the size of the user buffer passed by the client is + * insufficient for the requested operation. + */ + NV_ENC_ERR_NOT_ENOUGH_BUFFER, + + /** + * This indicates that an invalid struct version was used by the client. + */ + NV_ENC_ERR_INVALID_VERSION, + + /** + * This indicates that ::NvEncMapInputResource() API failed to map the client + * provided input resource. + */ + NV_ENC_ERR_MAP_FAILED, + + /** + * This indicates encode driver requires more input buffers to produce an output + * bitstream. If this error is returned from ::NvEncEncodePicture() API, this + * is not a fatal error. If the client is encoding with B frames then, + * ::NvEncEncodePicture() API might be buffering the input frame for re-ordering. + * + * A client operating in synchronous mode cannot call ::NvEncLockBitstream() + * API on the output bitstream buffer if ::NvEncEncodePicture() returned the + * ::NV_ENC_ERR_NEED_MORE_INPUT error code. + * The client must continue providing input frames until encode driver returns + * ::NV_ENC_SUCCESS. After receiving ::NV_ENC_SUCCESS status the client can call + * ::NvEncLockBitstream() API on the output buffers in the same order in which + * it has called ::NvEncEncodePicture(). + */ + NV_ENC_ERR_NEED_MORE_INPUT, + + /** + * This indicates that the HW encoder is busy encoding and is unable to encode + * the input. The client should call ::NvEncEncodePicture() again after few + * milliseconds. + */ + NV_ENC_ERR_ENCODER_BUSY, + + /** + * This indicates that the completion event passed in ::NvEncEncodePicture() + * API has not been registered with encoder driver using ::NvEncRegisterAsyncEvent(). + */ + NV_ENC_ERR_EVENT_NOT_REGISTERD, + + /** + * This indicates that an unknown internal error has occurred. + */ + NV_ENC_ERR_GENERIC, + + /** + * This indicates that the client is attempting to use a feature + * that is not available for the license type for the current system. + */ + NV_ENC_ERR_INCOMPATIBLE_CLIENT_KEY, + + /** + * This indicates that the client is attempting to use a feature + * that is not implemented for the current version. + */ + NV_ENC_ERR_UNIMPLEMENTED, + + /** + * This indicates that the ::NvEncRegisterResource API failed to register the resource. + */ + NV_ENC_ERR_RESOURCE_REGISTER_FAILED, + + /** + * This indicates that the client is attempting to unregister a resource + * that has not been successfully registered. + */ + NV_ENC_ERR_RESOURCE_NOT_REGISTERED, + + /** + * This indicates that the client is attempting to unmap a resource + * that has not been successfully mapped. + */ + NV_ENC_ERR_RESOURCE_NOT_MAPPED, + +} NVENCSTATUS; + +/** + * Encode Picture encode flags. + */ +typedef enum _NV_ENC_PIC_FLAGS { + NV_ENC_PIC_FLAG_FORCEINTRA = + 0x1, /**< Encode the current picture as an Intra picture */ + NV_ENC_PIC_FLAG_FORCEIDR = + 0x2, /**< Encode the current picture as an IDR picture. + This flag is only valid when Picture type decision is taken by the Encoder + [_NV_ENC_INITIALIZE_PARAMS::enablePTD == 1]. */ + NV_ENC_PIC_FLAG_OUTPUT_SPSPPS = + 0x4, /**< Write the sequence and picture header in encoded bitstream of the current picture */ + NV_ENC_PIC_FLAG_EOS = 0x8, /**< Indicates end of the input stream */ +} NV_ENC_PIC_FLAGS; + +/** + * Memory heap to allocate input and output buffers. + */ +typedef enum _NV_ENC_MEMORY_HEAP { + NV_ENC_MEMORY_HEAP_AUTOSELECT = + 0, /**< Memory heap to be decided by the encoder driver based on the usage */ + NV_ENC_MEMORY_HEAP_VID = 1, /**< Memory heap is in local video memory */ + NV_ENC_MEMORY_HEAP_SYSMEM_CACHED = + 2, /**< Memory heap is in cached system memory */ + NV_ENC_MEMORY_HEAP_SYSMEM_UNCACHED = + 3 /**< Memory heap is in uncached system memory */ +} NV_ENC_MEMORY_HEAP; + +/** + * B-frame used as reference modes + */ +typedef enum _NV_ENC_BFRAME_REF_MODE { + NV_ENC_BFRAME_REF_MODE_DISABLED = + 0x0, /**< B frame is not used for reference */ + NV_ENC_BFRAME_REF_MODE_EACH = + 0x1, /**< Each B-frame will be used for reference */ + NV_ENC_BFRAME_REF_MODE_MIDDLE = + 0x2, /**< Only(Number of B-frame)/2 th B-frame will be used for reference */ +} NV_ENC_BFRAME_REF_MODE; + +/** + * H.264 entropy coding modes. + */ +typedef enum _NV_ENC_H264_ENTROPY_CODING_MODE { + NV_ENC_H264_ENTROPY_CODING_MODE_AUTOSELECT = + 0x0, /**< Entropy coding mode is auto selected by the encoder driver */ + NV_ENC_H264_ENTROPY_CODING_MODE_CABAC = + 0x1, /**< Entropy coding mode is CABAC */ + NV_ENC_H264_ENTROPY_CODING_MODE_CAVLC = + 0x2 /**< Entropy coding mode is CAVLC */ +} NV_ENC_H264_ENTROPY_CODING_MODE; + +/** + * H.264 specific BDirect modes + */ +typedef enum _NV_ENC_H264_BDIRECT_MODE { + NV_ENC_H264_BDIRECT_MODE_AUTOSELECT = + 0x0, /**< BDirect mode is auto selected by the encoder driver */ + NV_ENC_H264_BDIRECT_MODE_DISABLE = 0x1, /**< Disable BDirect mode */ + NV_ENC_H264_BDIRECT_MODE_TEMPORAL = 0x2, /**< Temporal BDirect mode */ + NV_ENC_H264_BDIRECT_MODE_SPATIAL = 0x3 /**< Spatial BDirect mode */ +} NV_ENC_H264_BDIRECT_MODE; + +/** + * H.264 specific FMO usage + */ +typedef enum _NV_ENC_H264_FMO_MODE { + NV_ENC_H264_FMO_AUTOSELECT = + 0x0, /**< FMO usage is auto selected by the encoder driver */ + NV_ENC_H264_FMO_ENABLE = 0x1, /**< Enable FMO */ + NV_ENC_H264_FMO_DISABLE = 0x2, /**< Disable FMO */ +} NV_ENC_H264_FMO_MODE; + +/** + * H.264 specific Adaptive Transform modes + */ +typedef enum _NV_ENC_H264_ADAPTIVE_TRANSFORM_MODE { + NV_ENC_H264_ADAPTIVE_TRANSFORM_AUTOSELECT = + 0x0, /**< Adaptive Transform 8x8 mode is auto selected by the encoder driver*/ + NV_ENC_H264_ADAPTIVE_TRANSFORM_DISABLE = + 0x1, /**< Adaptive Transform 8x8 mode disabled */ + NV_ENC_H264_ADAPTIVE_TRANSFORM_ENABLE = + 0x2, /**< Adaptive Transform 8x8 mode should be used */ +} NV_ENC_H264_ADAPTIVE_TRANSFORM_MODE; + +/** + * Stereo frame packing modes. + */ +typedef enum _NV_ENC_STEREO_PACKING_MODE { + NV_ENC_STEREO_PACKING_MODE_NONE = 0x0, /**< No Stereo packing required */ + NV_ENC_STEREO_PACKING_MODE_CHECKERBOARD = + 0x1, /**< Checkerboard mode for packing stereo frames */ + NV_ENC_STEREO_PACKING_MODE_COLINTERLEAVE = + 0x2, /**< Column Interleave mode for packing stereo frames */ + NV_ENC_STEREO_PACKING_MODE_ROWINTERLEAVE = + 0x3, /**< Row Interleave mode for packing stereo frames */ + NV_ENC_STEREO_PACKING_MODE_SIDEBYSIDE = + 0x4, /**< Side-by-side mode for packing stereo frames */ + NV_ENC_STEREO_PACKING_MODE_TOPBOTTOM = + 0x5, /**< Top-Bottom mode for packing stereo frames */ + NV_ENC_STEREO_PACKING_MODE_FRAMESEQ = + 0x6 /**< Frame Sequential mode for packing stereo frames */ +} NV_ENC_STEREO_PACKING_MODE; + +/** + * Input Resource type + */ +typedef enum _NV_ENC_INPUT_RESOURCE_TYPE { + NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX = + 0x0, /**< input resource type is a directx9 surface*/ + NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR = + 0x1, /**< input resource type is a cuda device pointer surface*/ + NV_ENC_INPUT_RESOURCE_TYPE_CUDAARRAY = + 0x2, /**< input resource type is a cuda array surface. + This array must be a 2D array and the CUDA_ARRAY3D_SURFACE_LDST + flag must have been specified when creating it. */ + NV_ENC_INPUT_RESOURCE_TYPE_OPENGL_TEX = + 0x3 /**< input resource type is an OpenGL texture */ +} NV_ENC_INPUT_RESOURCE_TYPE; + +/** + * Buffer usage + */ +typedef enum _NV_ENC_BUFFER_USAGE { + NV_ENC_INPUT_IMAGE = + 0x0, /**< Registered surface will be used for input image */ + NV_ENC_OUTPUT_MOTION_VECTOR = + 0x1, /**< Registered surface will be used for output of H.264 ME only mode. + This buffer usage type is not supported for HEVC ME only mode. */ + NV_ENC_OUTPUT_BITSTREAM = + 0x2, /**< Registered surface will be used for output bitstream in encoding */ +} NV_ENC_BUFFER_USAGE; + +/** + * Encoder Device type + */ +typedef enum _NV_ENC_DEVICE_TYPE { + NV_ENC_DEVICE_TYPE_DIRECTX = + 0x0, /**< encode device type is a directx9 device */ + NV_ENC_DEVICE_TYPE_CUDA = 0x1, /**< encode device type is a cuda device */ + NV_ENC_DEVICE_TYPE_OPENGL = 0x2 /**< encode device type is an OpenGL device. + Use of this device type is supported only on Linux */ +} NV_ENC_DEVICE_TYPE; + +/** + * Number of reference frames + */ +typedef enum _NV_ENC_NUM_REF_FRAMES { + NV_ENC_NUM_REF_FRAMES_AUTOSELECT = + 0x0, /**< Number of reference frames is auto selected by the encoder driver */ + NV_ENC_NUM_REF_FRAMES_1 = 0x1, /**< Number of reference frames equal to 1 */ + NV_ENC_NUM_REF_FRAMES_2 = 0x2, /**< Number of reference frames equal to 2 */ + NV_ENC_NUM_REF_FRAMES_3 = 0x3, /**< Number of reference frames equal to 3 */ + NV_ENC_NUM_REF_FRAMES_4 = 0x4, /**< Number of reference frames equal to 4 */ + NV_ENC_NUM_REF_FRAMES_5 = 0x5, /**< Number of reference frames equal to 5 */ + NV_ENC_NUM_REF_FRAMES_6 = 0x6, /**< Number of reference frames equal to 6 */ + NV_ENC_NUM_REF_FRAMES_7 = 0x7 /**< Number of reference frames equal to 7 */ +} NV_ENC_NUM_REF_FRAMES; + +/** + * Encoder capabilities enumeration. + */ +typedef enum _NV_ENC_CAPS { + /** + * Maximum number of B-Frames supported. + */ + NV_ENC_CAPS_NUM_MAX_BFRAMES, + + /** + * Rate control modes supported. + * \n The API return value is a bitmask of the values in NV_ENC_PARAMS_RC_MODE. + */ + NV_ENC_CAPS_SUPPORTED_RATECONTROL_MODES, + + /** + * Indicates HW support for field mode encoding. + * \n 0 : Interlaced mode encoding is not supported. + * \n 1 : Interlaced field mode encoding is supported. + * \n 2 : Interlaced frame encoding and field mode encoding are both supported. + */ + NV_ENC_CAPS_SUPPORT_FIELD_ENCODING, + + /** + * Indicates HW support for monochrome mode encoding. + * \n 0 : Monochrome mode not supported. + * \n 1 : Monochrome mode supported. + */ + NV_ENC_CAPS_SUPPORT_MONOCHROME, + + /** + * Indicates HW support for FMO. + * \n 0 : FMO not supported. + * \n 1 : FMO supported. + */ + NV_ENC_CAPS_SUPPORT_FMO, + + /** + * Indicates HW capability for Quarter pel motion estimation. + * \n 0 : Quarter-Pel Motion Estimation not supported. + * \n 1 : Quarter-Pel Motion Estimation supported. + */ + NV_ENC_CAPS_SUPPORT_QPELMV, + + /** + * H.264 specific. Indicates HW support for BDirect modes. + * \n 0 : BDirect mode encoding not supported. + * \n 1 : BDirect mode encoding supported. + */ + NV_ENC_CAPS_SUPPORT_BDIRECT_MODE, + + /** + * H264 specific. Indicates HW support for CABAC entropy coding mode. + * \n 0 : CABAC entropy coding not supported. + * \n 1 : CABAC entropy coding supported. + */ + NV_ENC_CAPS_SUPPORT_CABAC, + + /** + * Indicates HW support for Adaptive Transform. + * \n 0 : Adaptive Transform not supported. + * \n 1 : Adaptive Transform supported. + */ + NV_ENC_CAPS_SUPPORT_ADAPTIVE_TRANSFORM, + + /** + * Indicates HW support for Multi View Coding. + * \n 0 : Multi View Coding not supported. + * \n 1 : Multi View Coding supported. + */ + NV_ENC_CAPS_SUPPORT_STEREO_MVC, + + /** + * Indicates HW support for encoding Temporal layers. + * \n 0 : Encoding Temporal layers not supported. + * \n 1 : Encoding Temporal layers supported. + */ + NV_ENC_CAPS_NUM_MAX_TEMPORAL_LAYERS, + + /** + * Indicates HW support for Hierarchical P frames. + * \n 0 : Hierarchical P frames not supported. + * \n 1 : Hierarchical P frames supported. + */ + NV_ENC_CAPS_SUPPORT_HIERARCHICAL_PFRAMES, + + /** + * Indicates HW support for Hierarchical B frames. + * \n 0 : Hierarchical B frames not supported. + * \n 1 : Hierarchical B frames supported. + */ + NV_ENC_CAPS_SUPPORT_HIERARCHICAL_BFRAMES, + + /** + * Maximum Encoding level supported (See ::NV_ENC_LEVEL for details). + */ + NV_ENC_CAPS_LEVEL_MAX, + + /** + * Minimum Encoding level supported (See ::NV_ENC_LEVEL for details). + */ + NV_ENC_CAPS_LEVEL_MIN, + + /** + * Indicates HW support for separate colour plane encoding. + * \n 0 : Separate colour plane encoding not supported. + * \n 1 : Separate colour plane encoding supported. + */ + NV_ENC_CAPS_SEPARATE_COLOUR_PLANE, + + /** + * Maximum output width supported. + */ + NV_ENC_CAPS_WIDTH_MAX, + + /** + * Maximum output height supported. + */ + NV_ENC_CAPS_HEIGHT_MAX, + + /** + * Indicates Temporal Scalability Support. + * \n 0 : Temporal SVC encoding not supported. + * \n 1 : Temporal SVC encoding supported. + */ + NV_ENC_CAPS_SUPPORT_TEMPORAL_SVC, + + /** + * Indicates Dynamic Encode Resolution Change Support. + * Support added from NvEncodeAPI version 2.0. + * \n 0 : Dynamic Encode Resolution Change not supported. + * \n 1 : Dynamic Encode Resolution Change supported. + */ + NV_ENC_CAPS_SUPPORT_DYN_RES_CHANGE, + + /** + * Indicates Dynamic Encode Bitrate Change Support. + * Support added from NvEncodeAPI version 2.0. + * \n 0 : Dynamic Encode bitrate change not supported. + * \n 1 : Dynamic Encode bitrate change supported. + */ + NV_ENC_CAPS_SUPPORT_DYN_BITRATE_CHANGE, + + /** + * Indicates Forcing Constant QP On The Fly Support. + * Support added from NvEncodeAPI version 2.0. + * \n 0 : Forcing constant QP on the fly not supported. + * \n 1 : Forcing constant QP on the fly supported. + */ + NV_ENC_CAPS_SUPPORT_DYN_FORCE_CONSTQP, + + /** + * Indicates Dynamic rate control mode Change Support. + * \n 0 : Dynamic rate control mode change not supported. + * \n 1 : Dynamic rate control mode change supported. + */ + NV_ENC_CAPS_SUPPORT_DYN_RCMODE_CHANGE, + + /** + * Indicates Subframe readback support for slice-based encoding. If this feature is supported, it can be enabled by setting enableSubFrameWrite = 1. + * \n 0 : Subframe readback not supported. + * \n 1 : Subframe readback supported. + */ + NV_ENC_CAPS_SUPPORT_SUBFRAME_READBACK, + + /** + * Indicates Constrained Encoding mode support. + * Support added from NvEncodeAPI version 2.0. + * \n 0 : Constrained encoding mode not supported. + * \n 1 : Constrained encoding mode supported. + * If this mode is supported client can enable this during initialization. + * Client can then force a picture to be coded as constrained picture where + * in-loop filtering is disabled across slice boundaries and prediction vectors for inter + * macroblocks in each slice will be restricted to the slice region. + */ + NV_ENC_CAPS_SUPPORT_CONSTRAINED_ENCODING, + + /** + * Indicates Intra Refresh Mode Support. + * Support added from NvEncodeAPI version 2.0. + * \n 0 : Intra Refresh Mode not supported. + * \n 1 : Intra Refresh Mode supported. + */ + NV_ENC_CAPS_SUPPORT_INTRA_REFRESH, + + /** + * Indicates Custom VBV Buffer Size support. It can be used for capping frame size. + * Support added from NvEncodeAPI version 2.0. + * \n 0 : Custom VBV buffer size specification from client, not supported. + * \n 1 : Custom VBV buffer size specification from client, supported. + */ + NV_ENC_CAPS_SUPPORT_CUSTOM_VBV_BUF_SIZE, + + /** + * Indicates Dynamic Slice Mode Support. + * Support added from NvEncodeAPI version 2.0. + * \n 0 : Dynamic Slice Mode not supported. + * \n 1 : Dynamic Slice Mode supported. + */ + NV_ENC_CAPS_SUPPORT_DYNAMIC_SLICE_MODE, + + /** + * Indicates Reference Picture Invalidation Support. + * Support added from NvEncodeAPI version 2.0. + * \n 0 : Reference Picture Invalidation not supported. + * \n 1 : Reference Picture Invalidation supported. + */ + NV_ENC_CAPS_SUPPORT_REF_PIC_INVALIDATION, + + /** + * Indicates support for Pre-Processing. + * The API return value is a bitmask of the values defined in ::NV_ENC_PREPROC_FLAGS + */ + NV_ENC_CAPS_PREPROC_SUPPORT, + + /** + * Indicates support Async mode. + * \n 0 : Async Encode mode not supported. + * \n 1 : Async Encode mode supported. + */ + NV_ENC_CAPS_ASYNC_ENCODE_SUPPORT, + + /** + * Maximum MBs per frame supported. + */ + NV_ENC_CAPS_MB_NUM_MAX, + + /** + * Maximum aggregate throughput in MBs per sec. + */ + NV_ENC_CAPS_MB_PER_SEC_MAX, + + /** + * Indicates HW support for YUV444 mode encoding. + * \n 0 : YUV444 mode encoding not supported. + * \n 1 : YUV444 mode encoding supported. + */ + NV_ENC_CAPS_SUPPORT_YUV444_ENCODE, + + /** + * Indicates HW support for lossless encoding. + * \n 0 : lossless encoding not supported. + * \n 1 : lossless encoding supported. + */ + NV_ENC_CAPS_SUPPORT_LOSSLESS_ENCODE, + + /** + * Indicates HW support for Sample Adaptive Offset. + * \n 0 : SAO not supported. + * \n 1 : SAO encoding supported. + */ + NV_ENC_CAPS_SUPPORT_SAO, + + /** + * Indicates HW support for Motion Estimation Only Mode. + * \n 0 : MEOnly Mode not supported. + * \n 1 : MEOnly Mode supported for I and P frames. + * \n 2 : MEOnly Mode supported for I, P and B frames. + */ + NV_ENC_CAPS_SUPPORT_MEONLY_MODE, + + /** + * Indicates HW support for lookahead encoding (enableLookahead=1). + * \n 0 : Lookahead not supported. + * \n 1 : Lookahead supported. + */ + NV_ENC_CAPS_SUPPORT_LOOKAHEAD, + + /** + * Indicates HW support for temporal AQ encoding (enableTemporalAQ=1). + * \n 0 : Temporal AQ not supported. + * \n 1 : Temporal AQ supported. + */ + NV_ENC_CAPS_SUPPORT_TEMPORAL_AQ, + /** + * Indicates HW support for 10 bit encoding. + * \n 0 : 10 bit encoding not supported. + * \n 1 : 10 bit encoding supported. + */ + NV_ENC_CAPS_SUPPORT_10BIT_ENCODE, + /** + * Maximum number of Long Term Reference frames supported + */ + NV_ENC_CAPS_NUM_MAX_LTR_FRAMES, + + /** + * Indicates HW support for Weighted Prediction. + * \n 0 : Weighted Prediction not supported. + * \n 1 : Weighted Prediction supported. + */ + NV_ENC_CAPS_SUPPORT_WEIGHTED_PREDICTION, + + /** + * On managed (vGPU) platforms (Windows only), this API, in conjunction with other GRID Management APIs, can be used + * to estimate the residual capacity of the hardware encoder on the GPU as a percentage of the total available encoder capacity. + * This API can be called at any time; i.e. during the encode session or before opening the encode session. + * If the available encoder capacity is returned as zero, applications may choose to switch to software encoding + * and continue to call this API (e.g. polling once per second) until capacity becomes available. + * + * On bare metal (non-virtualized GPU) and linux platforms, this API always returns 100. + */ + NV_ENC_CAPS_DYNAMIC_QUERY_ENCODER_CAPACITY, + + /** + * Indicates B as reference support. + * \n 0 : B as reference is not supported. + * \n 1 : each B-Frame as reference is supported. + * \n 2 : only Middle B-frame as reference is supported. + */ + NV_ENC_CAPS_SUPPORT_BFRAME_REF_MODE, + + /** + * Indicates HW support for Emphasis Level Map based delta QP computation. + * \n 0 : Emphasis Level Map based delta QP not supported. + * \n 1 : Emphasis Level Map based delta QP is supported. + */ + NV_ENC_CAPS_SUPPORT_EMPHASIS_LEVEL_MAP, + + /** + * Minimum input width supported. + */ + NV_ENC_CAPS_WIDTH_MIN, + + /** + * Minimum input height supported. + */ + NV_ENC_CAPS_HEIGHT_MIN, + + /** + * Indicates HW support for multiple reference frames. + */ + NV_ENC_CAPS_SUPPORT_MULTIPLE_REF_FRAMES, + + /** + * Indicates HW support for HEVC with alpha encoding. + * \n 0 : HEVC with alpha encoding not supported. + * \n 1 : HEVC with alpha encoding is supported. + */ + NV_ENC_CAPS_SUPPORT_ALPHA_LAYER_ENCODING, + + /** + * Indicates number of Encoding engines present on GPU. + */ + NV_ENC_CAPS_NUM_ENCODER_ENGINES, + + /** + * Indicates single slice intra refresh support. + */ + NV_ENC_CAPS_SINGLE_SLICE_INTRA_REFRESH, + + /** + * Reserved - Not to be used by clients. + */ + NV_ENC_CAPS_EXPOSED_COUNT + +} NV_ENC_CAPS; + +/** + * HEVC CU SIZE + */ +typedef enum _NV_ENC_HEVC_CUSIZE { + NV_ENC_HEVC_CUSIZE_AUTOSELECT = 0, + NV_ENC_HEVC_CUSIZE_8x8 = 1, + NV_ENC_HEVC_CUSIZE_16x16 = 2, + NV_ENC_HEVC_CUSIZE_32x32 = 3, + NV_ENC_HEVC_CUSIZE_64x64 = 4, +} NV_ENC_HEVC_CUSIZE; + +/** +* AV1 PART SIZE +*/ +typedef enum _NV_ENC_AV1_PART_SIZE { + NV_ENC_AV1_PART_SIZE_AUTOSELECT = 0, + NV_ENC_AV1_PART_SIZE_4x4 = 1, + NV_ENC_AV1_PART_SIZE_8x8 = 2, + NV_ENC_AV1_PART_SIZE_16x16 = 3, + NV_ENC_AV1_PART_SIZE_32x32 = 4, + NV_ENC_AV1_PART_SIZE_64x64 = 5, +} NV_ENC_AV1_PART_SIZE; + +/** +* Enums related to fields in VUI parameters. +*/ +typedef enum _NV_ENC_VUI_VIDEO_FORMAT { + NV_ENC_VUI_VIDEO_FORMAT_COMPONENT = 0, + NV_ENC_VUI_VIDEO_FORMAT_PAL = 1, + NV_ENC_VUI_VIDEO_FORMAT_NTSC = 2, + NV_ENC_VUI_VIDEO_FORMAT_SECAM = 3, + NV_ENC_VUI_VIDEO_FORMAT_MAC = 4, + NV_ENC_VUI_VIDEO_FORMAT_UNSPECIFIED = 5, +} NV_ENC_VUI_VIDEO_FORMAT; + +typedef enum _NV_ENC_VUI_COLOR_PRIMARIES { + NV_ENC_VUI_COLOR_PRIMARIES_UNDEFINED = 0, + NV_ENC_VUI_COLOR_PRIMARIES_BT709 = 1, + NV_ENC_VUI_COLOR_PRIMARIES_UNSPECIFIED = 2, + NV_ENC_VUI_COLOR_PRIMARIES_RESERVED = 3, + NV_ENC_VUI_COLOR_PRIMARIES_BT470M = 4, + NV_ENC_VUI_COLOR_PRIMARIES_BT470BG = 5, + NV_ENC_VUI_COLOR_PRIMARIES_SMPTE170M = 6, + NV_ENC_VUI_COLOR_PRIMARIES_SMPTE240M = 7, + NV_ENC_VUI_COLOR_PRIMARIES_FILM = 8, + NV_ENC_VUI_COLOR_PRIMARIES_BT2020 = 9, + NV_ENC_VUI_COLOR_PRIMARIES_SMPTE428 = 10, + NV_ENC_VUI_COLOR_PRIMARIES_SMPTE431 = 11, + NV_ENC_VUI_COLOR_PRIMARIES_SMPTE432 = 12, + NV_ENC_VUI_COLOR_PRIMARIES_JEDEC_P22 = 22, +} NV_ENC_VUI_COLOR_PRIMARIES; + +typedef enum _NV_ENC_VUI_TRANSFER_CHARACTERISTIC { + NV_ENC_VUI_TRANSFER_CHARACTERISTIC_UNDEFINED = 0, + NV_ENC_VUI_TRANSFER_CHARACTERISTIC_BT709 = 1, + NV_ENC_VUI_TRANSFER_CHARACTERISTIC_UNSPECIFIED = 2, + NV_ENC_VUI_TRANSFER_CHARACTERISTIC_RESERVED = 3, + NV_ENC_VUI_TRANSFER_CHARACTERISTIC_BT470M = 4, + NV_ENC_VUI_TRANSFER_CHARACTERISTIC_BT470BG = 5, + NV_ENC_VUI_TRANSFER_CHARACTERISTIC_SMPTE170M = 6, + NV_ENC_VUI_TRANSFER_CHARACTERISTIC_SMPTE240M = 7, + NV_ENC_VUI_TRANSFER_CHARACTERISTIC_LINEAR = 8, + NV_ENC_VUI_TRANSFER_CHARACTERISTIC_LOG = 9, + NV_ENC_VUI_TRANSFER_CHARACTERISTIC_LOG_SQRT = 10, + NV_ENC_VUI_TRANSFER_CHARACTERISTIC_IEC61966_2_4 = 11, + NV_ENC_VUI_TRANSFER_CHARACTERISTIC_BT1361_ECG = 12, + NV_ENC_VUI_TRANSFER_CHARACTERISTIC_SRGB = 13, + NV_ENC_VUI_TRANSFER_CHARACTERISTIC_BT2020_10 = 14, + NV_ENC_VUI_TRANSFER_CHARACTERISTIC_BT2020_12 = 15, + NV_ENC_VUI_TRANSFER_CHARACTERISTIC_SMPTE2084 = 16, + NV_ENC_VUI_TRANSFER_CHARACTERISTIC_SMPTE428 = 17, + NV_ENC_VUI_TRANSFER_CHARACTERISTIC_ARIB_STD_B67 = 18, +} NV_ENC_VUI_TRANSFER_CHARACTERISTIC; + +typedef enum _NV_ENC_VUI_MATRIX_COEFFS { + NV_ENC_VUI_MATRIX_COEFFS_RGB = 0, + NV_ENC_VUI_MATRIX_COEFFS_BT709 = 1, + NV_ENC_VUI_MATRIX_COEFFS_UNSPECIFIED = 2, + NV_ENC_VUI_MATRIX_COEFFS_RESERVED = 3, + NV_ENC_VUI_MATRIX_COEFFS_FCC = 4, + NV_ENC_VUI_MATRIX_COEFFS_BT470BG = 5, + NV_ENC_VUI_MATRIX_COEFFS_SMPTE170M = 6, + NV_ENC_VUI_MATRIX_COEFFS_SMPTE240M = 7, + NV_ENC_VUI_MATRIX_COEFFS_YCGCO = 8, + NV_ENC_VUI_MATRIX_COEFFS_BT2020_NCL = 9, + NV_ENC_VUI_MATRIX_COEFFS_BT2020_CL = 10, + NV_ENC_VUI_MATRIX_COEFFS_SMPTE2085 = 11, +} NV_ENC_VUI_MATRIX_COEFFS; + +/** + * Input struct for querying Encoding capabilities. + */ +typedef struct _NV_ENC_CAPS_PARAM { + uint32_t + version; /**< [in]: Struct version. Must be set to ::NV_ENC_CAPS_PARAM_VER */ + NV_ENC_CAPS + capsToQuery; /**< [in]: Specifies the encode capability to be queried. Client should pass a member for ::NV_ENC_CAPS enum. */ + uint32_t reserved[62]; /**< [in]: Reserved and must be set to 0 */ +} NV_ENC_CAPS_PARAM; + +/** NV_ENC_CAPS_PARAM struct version. */ +#define NV_ENC_CAPS_PARAM_VER NVENCAPI_STRUCT_VERSION(1) + +/** + * Encoder Output parameters + */ +typedef struct _NV_ENC_ENCODE_OUT_PARAMS { + uint32_t version; /**< [out]: Struct version. */ + uint32_t bitstreamSizeInBytes; /**< [out]: Encoded bitstream size in bytes */ + uint32_t reserved[62]; /**< [out]: Reserved and must be set to 0 */ +} NV_ENC_ENCODE_OUT_PARAMS; + +/** NV_ENC_ENCODE_OUT_PARAMS struct version. */ +#define NV_ENC_ENCODE_OUT_PARAMS_VER NVENCAPI_STRUCT_VERSION(1) + +/** + * Creation parameters for input buffer. + */ +typedef struct _NV_ENC_CREATE_INPUT_BUFFER { + uint32_t + version; /**< [in]: Struct version. Must be set to ::NV_ENC_CREATE_INPUT_BUFFER_VER */ + uint32_t width; /**< [in]: Input frame width */ + uint32_t height; /**< [in]: Input frame height */ + NV_ENC_MEMORY_HEAP memoryHeap; /**< [in]: Deprecated. Do not use */ + NV_ENC_BUFFER_FORMAT bufferFmt; /**< [in]: Input buffer format */ + uint32_t reserved; /**< [in]: Reserved and must be set to 0 */ + NV_ENC_INPUT_PTR inputBuffer; /**< [out]: Pointer to input buffer */ + void* pSysMemBuffer; /**< [in]: Pointer to existing system memory buffer */ + uint32_t reserved1[57]; /**< [in]: Reserved and must be set to 0 */ + void* reserved2[63]; /**< [in]: Reserved and must be set to NULL */ +} NV_ENC_CREATE_INPUT_BUFFER; + +/** NV_ENC_CREATE_INPUT_BUFFER struct version. */ +#define NV_ENC_CREATE_INPUT_BUFFER_VER NVENCAPI_STRUCT_VERSION(1) + +/** + * Creation parameters for output bitstream buffer. + */ +typedef struct _NV_ENC_CREATE_BITSTREAM_BUFFER { + uint32_t + version; /**< [in]: Struct version. Must be set to ::NV_ENC_CREATE_BITSTREAM_BUFFER_VER */ + uint32_t size; /**< [in]: Deprecated. Do not use */ + NV_ENC_MEMORY_HEAP memoryHeap; /**< [in]: Deprecated. Do not use */ + uint32_t reserved; /**< [in]: Reserved and must be set to 0 */ + NV_ENC_OUTPUT_PTR + bitstreamBuffer; /**< [out]: Pointer to the output bitstream buffer */ + void* bitstreamBufferPtr; /**< [out]: Reserved and should not be used */ + uint32_t reserved1[58]; /**< [in]: Reserved and should be set to 0 */ + void* reserved2[64]; /**< [in]: Reserved and should be set to NULL */ +} NV_ENC_CREATE_BITSTREAM_BUFFER; + +/** NV_ENC_CREATE_BITSTREAM_BUFFER struct version. */ +#define NV_ENC_CREATE_BITSTREAM_BUFFER_VER NVENCAPI_STRUCT_VERSION(1) + +/** + * Structs needed for ME only mode. + */ +typedef struct _NV_ENC_MVECTOR { + int16_t mvx; /**< the x component of MV in quarter-pel units */ + int16_t mvy; /**< the y component of MV in quarter-pel units */ +} NV_ENC_MVECTOR; + +/** + * Motion vector structure per macroblock for H264 motion estimation. + */ +typedef struct _NV_ENC_H264_MV_DATA { + NV_ENC_MVECTOR mv[4]; /**< up to 4 vectors for 8x8 partition */ + uint8_t mbType; /**< 0 (I), 1 (P), 2 (IPCM), 3 (B) */ + uint8_t + partitionType; /**< Specifies the block partition type. 0:16x16, 1:8x8, 2:16x8, 3:8x16 */ + uint16_t reserved; /**< reserved padding for alignment */ + uint32_t mbCost; +} NV_ENC_H264_MV_DATA; + +/** + * Motion vector structure per CU for HEVC motion estimation. + */ +typedef struct _NV_ENC_HEVC_MV_DATA { + NV_ENC_MVECTOR mv[4]; /**< up to 4 vectors within a CU */ + uint8_t cuType; /**< 0 (I), 1(P) */ + uint8_t cuSize; /**< 0: 8x8, 1: 16x16, 2: 32x32, 3: 64x64 */ + uint8_t partitionMode; /**< The CU partition mode + 0 (2Nx2N), 1 (2NxN), 2(Nx2N), 3 (NxN), + 4 (2NxnU), 5 (2NxnD), 6(nLx2N), 7 (nRx2N) */ + uint8_t + lastCUInCTB; /**< Marker to separate CUs in the current CTB from CUs in the next CTB */ +} NV_ENC_HEVC_MV_DATA; + +/** + * Creation parameters for output motion vector buffer for ME only mode. + */ +typedef struct _NV_ENC_CREATE_MV_BUFFER { + uint32_t + version; /**< [in]: Struct version. Must be set to NV_ENC_CREATE_MV_BUFFER_VER */ + NV_ENC_OUTPUT_PTR + mvBuffer; /**< [out]: Pointer to the output motion vector buffer */ + uint32_t reserved1[255]; /**< [in]: Reserved and should be set to 0 */ + void* reserved2[63]; /**< [in]: Reserved and should be set to NULL */ +} NV_ENC_CREATE_MV_BUFFER; + +/** NV_ENC_CREATE_MV_BUFFER struct version*/ +#define NV_ENC_CREATE_MV_BUFFER_VER NVENCAPI_STRUCT_VERSION(1) + +/** + * QP value for frames + */ +typedef struct _NV_ENC_QP { + uint32_t + qpInterP; /**< [in]: Specifies QP value for P-frame. Even though this field is uint32_t for legacy reasons, the client should treat this as a signed parameter(int32_t) for cases in which negative QP values are to be specified. */ + uint32_t + qpInterB; /**< [in]: Specifies QP value for B-frame. Even though this field is uint32_t for legacy reasons, the client should treat this as a signed parameter(int32_t) for cases in which negative QP values are to be specified. */ + uint32_t + qpIntra; /**< [in]: Specifies QP value for Intra Frame. Even though this field is uint32_t for legacy reasons, the client should treat this as a signed parameter(int32_t) for cases in which negative QP values are to be specified. */ +} NV_ENC_QP; + +/** + * Rate Control Configuration Parameters + */ +typedef struct _NV_ENC_RC_PARAMS { + uint32_t version; + NV_ENC_PARAMS_RC_MODE + rateControlMode; /**< [in]: Specifies the rate control mode. Check support for various rate control modes using ::NV_ENC_CAPS_SUPPORTED_RATECONTROL_MODES caps. */ + NV_ENC_QP + constQP; /**< [in]: Specifies the initial QP to be used for encoding, these values would be used for all frames if in Constant QP mode. */ + uint32_t + averageBitRate; /**< [in]: Specifies the average bitrate(in bits/sec) used for encoding. */ + uint32_t + maxBitRate; /**< [in]: Specifies the maximum bitrate for the encoded output. This is used for VBR and ignored for CBR mode. */ + uint32_t + vbvBufferSize; /**< [in]: Specifies the VBV(HRD) buffer size. in bits. Set 0 to use the default VBV buffer size. */ + uint32_t + vbvInitialDelay; /**< [in]: Specifies the VBV(HRD) initial delay in bits. Set 0 to use the default VBV initial delay .*/ + uint32_t + enableMinQP : 1; /**< [in]: Set this to 1 if minimum QP used for rate control. */ + uint32_t + enableMaxQP : 1; /**< [in]: Set this to 1 if maximum QP used for rate control. */ + uint32_t + enableInitialRCQP : 1; /**< [in]: Set this to 1 if user supplied initial QP is used for rate control. */ + uint32_t + enableAQ : 1; /**< [in]: Set this to 1 to enable adaptive quantization (Spatial). */ + uint32_t + reservedBitField1 : 1; /**< [in]: Reserved bitfields and must be set to 0. */ + uint32_t + enableLookahead : 1; /**< [in]: Set this to 1 to enable lookahead with depth (if lookahead is enabled, input frames must remain available to the encoder until encode completion) */ + uint32_t + disableIadapt : 1; /**< [in]: Set this to 1 to disable adaptive I-frame insertion at scene cuts (only has an effect when lookahead is enabled) */ + uint32_t + disableBadapt : 1; /**< [in]: Set this to 1 to disable adaptive B-frame decision (only has an effect when lookahead is enabled) */ + uint32_t + enableTemporalAQ : 1; /**< [in]: Set this to 1 to enable temporal AQ */ + uint32_t + zeroReorderDelay : 1; /**< [in]: Set this to 1 to indicate zero latency operation (no reordering delay, num_reorder_frames=0) */ + uint32_t + enableNonRefP : 1; /**< [in]: Set this to 1 to enable automatic insertion of non-reference P-frames (no effect if enablePTD=0) */ + uint32_t + strictGOPTarget : 1; /**< [in]: Set this to 1 to minimize GOP-to-GOP rate fluctuations */ + uint32_t + aqStrength : 4; /**< [in]: When AQ (Spatial) is enabled (i.e. NV_ENC_RC_PARAMS::enableAQ is set), this field is used to specify AQ strength. AQ strength scale is from 1 (low) - 15 (aggressive). + If not set, strength is auto selected by driver. */ + uint32_t + reservedBitFields : 16; /**< [in]: Reserved bitfields and must be set to 0 */ + NV_ENC_QP + minQP; /**< [in]: Specifies the minimum QP used for rate control. Client must set NV_ENC_CONFIG::enableMinQP to 1. */ + NV_ENC_QP + maxQP; /**< [in]: Specifies the maximum QP used for rate control. Client must set NV_ENC_CONFIG::enableMaxQP to 1. */ + NV_ENC_QP + initialRCQP; /**< [in]: Specifies the initial QP used for rate control. Client must set NV_ENC_CONFIG::enableInitialRCQP to 1. */ + uint32_t + temporallayerIdxMask; /**< [in]: Specifies the temporal layers (as a bitmask) whose QPs have changed. Valid max bitmask is [2^NV_ENC_CAPS_NUM_MAX_TEMPORAL_LAYERS - 1]. + Applicable only for constant QP mode (NV_ENC_RC_PARAMS::rateControlMode = NV_ENC_PARAMS_RC_CONSTQP). */ + uint8_t temporalLayerQP + [8]; /**< [in]: Specifies the temporal layer QPs used for rate control. Temporal layer index is used as the array index. + Applicable only for constant QP mode (NV_ENC_RC_PARAMS::rateControlMode = NV_ENC_PARAMS_RC_CONSTQP). */ + uint8_t + targetQuality; /**< [in]: Target CQ (Constant Quality) level for VBR mode (range 0-51 with 0-automatic) */ + uint8_t + targetQualityLSB; /**< [in]: Fractional part of target quality (as 8.8 fixed point format) */ + uint16_t + lookaheadDepth; /**< [in]: Maximum depth of lookahead with range 0-(31 - number of B frames). + lookaheadDepth is only used if enableLookahead=1.*/ + uint8_t + lowDelayKeyFrameScale; /**< [in]: Specifies the ratio of I frame bits to P frame bits in case of single frame VBV and CBR rate control mode, + is set to 2 by default for low latency tuning info and 1 by default for ultra low latency tuning info */ + int8_t + yDcQPIndexOffset; /**< [in]: Specifies the value of 'deltaQ_y_dc' in AV1.*/ + int8_t + uDcQPIndexOffset; /**< [in]: Specifies the value of 'deltaQ_u_dc' in AV1.*/ + int8_t + vDcQPIndexOffset; /**< [in]: Specifies the value of 'deltaQ_v_dc' in AV1 (for future use only - deltaQ_v_dc is currently always internally set to same value as deltaQ_u_dc). */ + NV_ENC_QP_MAP_MODE + qpMapMode; /**< [in]: This flag is used to interpret values in array specified by NV_ENC_PIC_PARAMS::qpDeltaMap. + Set this to NV_ENC_QP_MAP_EMPHASIS to treat values specified by NV_ENC_PIC_PARAMS::qpDeltaMap as Emphasis Level Map. + Emphasis Level can be assigned any value specified in enum NV_ENC_EMPHASIS_MAP_LEVEL. + Emphasis Level Map is used to specify regions to be encoded at varying levels of quality. + The hardware encoder adjusts the quantization within the image as per the provided emphasis map, + by adjusting the quantization parameter (QP) assigned to each macroblock. This adjustment is commonly called "Delta QP". + The adjustment depends on the absolute QP decided by the rate control algorithm, and is applied after the rate control has decided each macroblock's QP. + Since the Delta QP overrides rate control, enabling Emphasis Level Map may violate bitrate and VBV buffer size constraints. + Emphasis Level Map is useful in situations where client has a priori knowledge of the image complexity (e.g. via use of NVFBC's Classification feature) and encoding those high-complexity areas at higher quality (lower QP) is important, even at the possible cost of violating bitrate/VBV buffer size constraints + This feature is not supported when AQ( Spatial/Temporal) is enabled. + This feature is only supported for H264 codec currently. + + Set this to NV_ENC_QP_MAP_DELTA to treat values specified by NV_ENC_PIC_PARAMS::qpDeltaMap as QP Delta. This specifies QP modifier to be applied on top of the QP chosen by rate control + + Set this to NV_ENC_QP_MAP_DISABLED to ignore NV_ENC_PIC_PARAMS::qpDeltaMap values. In this case, qpDeltaMap should be set to NULL. + + Other values are reserved for future use.*/ + NV_ENC_MULTI_PASS + multiPass; /**< [in]: This flag is used to enable multi-pass encoding for a given ::NV_ENC_PARAMS_RC_MODE. This flag is not valid for H264 and HEVC MEOnly mode */ + uint32_t + alphaLayerBitrateRatio; /**< [in]: Specifies the ratio in which bitrate should be split between base and alpha layer. A value 'x' for this field will split the target bitrate in a ratio of x : 1 between base and alpha layer. + The default split ratio is 15.*/ + int8_t + cbQPIndexOffset; /**< [in]: Specifies the value of 'chroma_qp_index_offset' in H264 / 'pps_cb_qp_offset' in HEVC / 'deltaQ_u_ac' in AV1.*/ + int8_t + crQPIndexOffset; /**< [in]: Specifies the value of 'second_chroma_qp_index_offset' in H264 / 'pps_cr_qp_offset' in HEVC / 'deltaQ_v_ac' in AV1 (for future use only - deltaQ_v_ac is currently always internally set to same value as deltaQ_u_ac). */ + uint16_t reserved2; + uint32_t reserved[4]; +} NV_ENC_RC_PARAMS; + +/** macro for constructing the version field of ::_NV_ENC_RC_PARAMS */ +#define NV_ENC_RC_PARAMS_VER NVENCAPI_STRUCT_VERSION(1) + +#define MAX_NUM_CLOCK_TS 3 + +/** +* Clock Timestamp set parameters +* For H264, this structure is used to populate Picture Timing SEI when NV_ENC_CONFIG_H264::enableTimeCode is set to 1. +* For HEVC, this structure is used to populate Time Code SEI when NV_ENC_CONFIG_HEVC::enableTimeCodeSEI is set to 1. +* For more details, refer to Annex D of ITU-T Specification. +*/ + +typedef struct _NV_ENC_CLOCK_TIMESTAMP_SET { + uint32_t countingType : 1; /**< [in] Specifies the 'counting_type' */ + uint32_t + discontinuityFlag : 1; /**< [in] Specifies the 'discontinuity_flag' */ + uint32_t cntDroppedFrames : 1; /**< [in] Specifies the 'cnt_dropped_flag' */ + uint32_t nFrames : 8; /**< [in] Specifies the value of 'n_frames' */ + uint32_t secondsValue : 6; /**< [in] Specifies the 'seconds_value' */ + uint32_t minutesValue : 6; /**< [in] Specifies the 'minutes_value' */ + uint32_t hoursValue : 5; /**< [in] Specifies the 'hours_value' */ + uint32_t reserved2 : 4; /**< [in] Reserved and must be set to 0 */ + uint32_t timeOffset; /**< [in] Specifies the 'time_offset_value' */ +} NV_ENC_CLOCK_TIMESTAMP_SET; + +typedef struct _NV_ENC_TIME_CODE { + NV_ENC_DISPLAY_PIC_STRUCT displayPicStruct; /**< [in] Display picStruct */ + NV_ENC_CLOCK_TIMESTAMP_SET + clockTimestamp[MAX_NUM_CLOCK_TS]; /**< [in] Clock Timestamp set */ +} NV_ENC_TIME_CODE; + +/** + * \struct _NV_ENC_CONFIG_H264_VUI_PARAMETERS + * H264 Video Usability Info parameters + */ +typedef struct _NV_ENC_CONFIG_H264_VUI_PARAMETERS { + uint32_t + overscanInfoPresentFlag; /**< [in]: If set to 1 , it specifies that the overscanInfo is present */ + uint32_t + overscanInfo; /**< [in]: Specifies the overscan info(as defined in Annex E of the ITU-T Specification). */ + uint32_t + videoSignalTypePresentFlag; /**< [in]: If set to 1, it specifies that the videoFormat, videoFullRangeFlag and colourDescriptionPresentFlag are present. */ + NV_ENC_VUI_VIDEO_FORMAT + videoFormat; /**< [in]: Specifies the source video format(as defined in Annex E of the ITU-T Specification).*/ + uint32_t + videoFullRangeFlag; /**< [in]: Specifies the output range of the luma and chroma samples(as defined in Annex E of the ITU-T Specification). */ + uint32_t + colourDescriptionPresentFlag; /**< [in]: If set to 1, it specifies that the colourPrimaries, transferCharacteristics and colourMatrix are present. */ + NV_ENC_VUI_COLOR_PRIMARIES + colourPrimaries; /**< [in]: Specifies color primaries for converting to RGB(as defined in Annex E of the ITU-T Specification) */ + NV_ENC_VUI_TRANSFER_CHARACTERISTIC + transferCharacteristics; /**< [in]: Specifies the opto-electronic transfer characteristics to use (as defined in Annex E of the ITU-T Specification) */ + NV_ENC_VUI_MATRIX_COEFFS + colourMatrix; /**< [in]: Specifies the matrix coefficients used in deriving the luma and chroma from the RGB primaries (as defined in Annex E of the ITU-T Specification). */ + uint32_t + chromaSampleLocationFlag; /**< [in]: If set to 1 , it specifies that the chromaSampleLocationTop and chromaSampleLocationBot are present.*/ + uint32_t + chromaSampleLocationTop; /**< [in]: Specifies the chroma sample location for top field(as defined in Annex E of the ITU-T Specification) */ + uint32_t + chromaSampleLocationBot; /**< [in]: Specifies the chroma sample location for bottom field(as defined in Annex E of the ITU-T Specification) */ + uint32_t + bitstreamRestrictionFlag; /**< [in]: If set to 1, it specifies the bitstream restriction parameters are present in the bitstream.*/ + uint32_t + timingInfoPresentFlag; /**< [in]: If set to 1, it specifies that the timingInfo is present and the 'numUnitInTicks' and 'timeScale' fields are specified by the application. */ + /**< [in]: If not set, the timingInfo may still be present with timing related fields calculated internally basedon the frame rate specified by the application. */ + uint32_t + numUnitInTicks; /**< [in]: Specifies the number of time units of the clock(as defined in Annex E of the ITU-T Specification). */ + uint32_t + timeScale; /**< [in]: Specifies the frquency of the clock(as defined in Annex E of the ITU-T Specification). */ + uint32_t reserved[12]; /**< [in]: Reserved and must be set to 0 */ +} NV_ENC_CONFIG_H264_VUI_PARAMETERS; + +typedef NV_ENC_CONFIG_H264_VUI_PARAMETERS NV_ENC_CONFIG_HEVC_VUI_PARAMETERS; + +/** + * \struct _NVENC_EXTERNAL_ME_HINT_COUNTS_PER_BLOCKTYPE + * External motion vector hint counts per block type. + * H264 and AV1 support multiple hint while HEVC supports one hint for each valid candidate. + */ +typedef struct _NVENC_EXTERNAL_ME_HINT_COUNTS_PER_BLOCKTYPE { + uint32_t + numCandsPerBlk16x16 : 4; /**< [in]: Supported for H264, HEVC. It Specifies the number of candidates per 16x16 block. */ + uint32_t + numCandsPerBlk16x8 : 4; /**< [in]: Supported for H264 only. Specifies the number of candidates per 16x8 block. */ + uint32_t + numCandsPerBlk8x16 : 4; /**< [in]: Supported for H264 only. Specifies the number of candidates per 8x16 block. */ + uint32_t + numCandsPerBlk8x8 : 4; /**< [in]: Supported for H264, HEVC. Specifies the number of candidates per 8x8 block. */ + uint32_t + numCandsPerSb : 8; /**< [in]: Supported for AV1 only. Specifies the number of candidates per SB. */ + uint32_t reserved : 8; /**< [in]: Reserved for padding. */ + uint32_t reserved1[3]; /**< [in]: Reserved for future use. */ +} NVENC_EXTERNAL_ME_HINT_COUNTS_PER_BLOCKTYPE; + +/** + * \struct _NVENC_EXTERNAL_ME_HINT + * External Motion Vector hint structure for H264 and HEVC. + */ +typedef struct _NVENC_EXTERNAL_ME_HINT { + int32_t + mvx : 12; /**< [in]: Specifies the x component of integer pixel MV (relative to current MB) S12.0. */ + int32_t + mvy : 10; /**< [in]: Specifies the y component of integer pixel MV (relative to current MB) S10.0 .*/ + int32_t + refidx : 5; /**< [in]: Specifies the reference index (31=invalid). Current we support only 1 reference frame per direction for external hints, so \p refidx must be 0. */ + int32_t + dir : 1; /**< [in]: Specifies the direction of motion estimation . 0=L0 1=L1.*/ + int32_t + partType : 2; /**< [in]: Specifies the block partition type.0=16x16 1=16x8 2=8x16 3=8x8 (blocks in partition must be consecutive).*/ + int32_t + lastofPart : 1; /**< [in]: Set to 1 for the last MV of (sub) partition */ + int32_t lastOfMB : 1; /**< [in]: Set to 1 for the last MV of macroblock. */ +} NVENC_EXTERNAL_ME_HINT; + +/** + * \struct _NVENC_EXTERNAL_ME_SB_HINT + * External Motion Vector SB hint structure for AV1 + */ +typedef struct _NVENC_EXTERNAL_ME_SB_HINT { + int16_t refidx : 5; /**< [in]: Specifies the reference index (31=invalid) */ + int16_t + direction : 1; /**< [in]: Specifies the direction of motion estimation . 0=L0 1=L1.*/ + int16_t + bi : 1; /**< [in]: Specifies reference mode 0=single mv, 1=compound mv */ + int16_t + partition_type : 3; /**< [in]: Specifies the partition type: 0: 2NX2N, 1:2NxN, 2:Nx2N. reserved 3bits for future modes */ + int16_t + x8 : 3; /**< [in]: Specifies the current partition's top left x position in 8 pixel unit */ + int16_t last_of_cu : 1; /**< [in]: Set to 1 for the last MV current CU */ + int16_t last_of_sb : 1; /**< [in]: Set to 1 for the last MV of current SB */ + int16_t reserved0 : 1; /**< [in]: Reserved and must be set to 0 */ + int16_t + mvx : 14; /**< [in]: Specifies the x component of integer pixel MV (relative to current MB) S12.2. */ + int16_t + cu_size : 2; /**< [in]: Specifies the CU size: 0: 8x8, 1: 16x16, 2:32x32, 3:64x64 */ + int16_t + mvy : 12; /**< [in]: Specifies the y component of integer pixel MV (relative to current MB) S10.2 .*/ + int16_t + y8 : 3; /**< [in]: Specifies the current partition's top left y position in 8 pixel unit */ + int16_t reserved1 : 1; /**< [in]: Reserved and must be set to 0 */ +} NVENC_EXTERNAL_ME_SB_HINT; + +/** + * \struct _NV_ENC_CONFIG_H264 + * H264 encoder configuration parameters + */ +typedef struct _NV_ENC_CONFIG_H264 { + uint32_t enableTemporalSVC : 1; /**< [in]: Set to 1 to enable SVC temporal*/ + uint32_t enableStereoMVC : 1; /**< [in]: Set to 1 to enable stereo MVC*/ + uint32_t + hierarchicalPFrames : 1; /**< [in]: Set to 1 to enable hierarchical P Frames */ + uint32_t + hierarchicalBFrames : 1; /**< [in]: Set to 1 to enable hierarchical B Frames */ + uint32_t + outputBufferingPeriodSEI : 1; /**< [in]: Set to 1 to write SEI buffering period syntax in the bitstream */ + uint32_t + outputPictureTimingSEI : 1; /**< [in]: Set to 1 to write SEI picture timing syntax in the bitstream. */ + uint32_t + outputAUD : 1; /**< [in]: Set to 1 to write access unit delimiter syntax in bitstream */ + uint32_t + disableSPSPPS : 1; /**< [in]: Set to 1 to disable writing of Sequence and Picture parameter info in bitstream */ + uint32_t + outputFramePackingSEI : 1; /**< [in]: Set to 1 to enable writing of frame packing arrangement SEI messages to bitstream */ + uint32_t + outputRecoveryPointSEI : 1; /**< [in]: Set to 1 to enable writing of recovery point SEI message */ + uint32_t + enableIntraRefresh : 1; /**< [in]: Set to 1 to enable gradual decoder refresh or intra refresh. If the GOP structure uses B frames this will be ignored */ + uint32_t + enableConstrainedEncoding : 1; /**< [in]: Set this to 1 to enable constrainedFrame encoding where each slice in the constrained picture is independent of other slices. + Constrained encoding works only with rectangular slices. + Check support for constrained encoding using ::NV_ENC_CAPS_SUPPORT_CONSTRAINED_ENCODING caps. */ + uint32_t + repeatSPSPPS : 1; /**< [in]: Set to 1 to enable writing of Sequence and Picture parameter for every IDR frame */ + uint32_t + enableVFR : 1; /**< [in]: Setting enableVFR=1 currently only sets the fixed_frame_rate_flag=0 in the VUI but otherwise + has no impact on the encoder behavior. For more details please refer to E.1 VUI syntax of H.264 standard. Note, however, that NVENC does not support VFR encoding and rate control. */ + uint32_t + enableLTR : 1; /**< [in]: Set to 1 to enable LTR (Long Term Reference) frame support. LTR can be used in two modes: "LTR Trust" mode and "LTR Per Picture" mode. + LTR Trust mode: In this mode, ltrNumFrames pictures after IDR are automatically marked as LTR. This mode is enabled by setting ltrTrustMode = 1. + Use of LTR Trust mode is strongly discouraged as this mode may be deprecated in future. + LTR Per Picture mode: In this mode, client can control whether the current picture should be marked as LTR. Enable this mode by setting + ltrTrustMode = 0 and ltrMarkFrame = 1 for the picture to be marked as LTR. This is the preferred mode + for using LTR. + Note that LTRs are not supported if encoding session is configured with B-frames */ + uint32_t + qpPrimeYZeroTransformBypassFlag : 1; /**< [in]: To enable lossless encode set this to 1, set QP to 0 and RC_mode to NV_ENC_PARAMS_RC_CONSTQP and profile to HIGH_444_PREDICTIVE_PROFILE. + Check support for lossless encoding using ::NV_ENC_CAPS_SUPPORT_LOSSLESS_ENCODE caps. */ + uint32_t + useConstrainedIntraPred : 1; /**< [in]: Set 1 to enable constrained intra prediction. */ + uint32_t + enableFillerDataInsertion : 1; /**< [in]: Set to 1 to enable insertion of filler data in the bitstream. + This flag will take effect only when one of the CBR rate + control modes (NV_ENC_PARAMS_RC_CBR, NV_ENC_PARAMS_RC_CBR_HQ, + NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ) is in use and both + NV_ENC_INITIALIZE_PARAMS::frameRateNum and + NV_ENC_INITIALIZE_PARAMS::frameRateDen are set to non-zero + values. Setting this field when + NV_ENC_INITIALIZE_PARAMS::enableOutputInVidmem is also set + is currently not supported and will make ::NvEncInitializeEncoder() + return an error. */ + uint32_t + disableSVCPrefixNalu : 1; /**< [in]: Set to 1 to disable writing of SVC Prefix NALU preceding each slice in bitstream. + Applicable only when temporal SVC is enabled (NV_ENC_CONFIG_H264::enableTemporalSVC = 1). */ + uint32_t + enableScalabilityInfoSEI : 1; /**< [in]: Set to 1 to enable writing of Scalability Information SEI message preceding each IDR picture in bitstream + Applicable only when temporal SVC is enabled (NV_ENC_CONFIG_H264::enableTemporalSVC = 1). */ + uint32_t + singleSliceIntraRefresh : 1; /**< [in]: Set to 1 to maintain single slice in frames during intra refresh. + Check support for single slice intra refresh using ::NV_ENC_CAPS_SINGLE_SLICE_INTRA_REFRESH caps. + This flag will be ignored if the value returned for ::NV_ENC_CAPS_SINGLE_SLICE_INTRA_REFRESH caps is false. */ + uint32_t + enableTimeCode : 1; /**< [in]: Set to 1 to enable writing of clock timestamp sets in picture timing SEI. Note that this flag will be ignored for D3D12 interface. */ + uint32_t + reservedBitFields : 10; /**< [in]: Reserved bitfields and must be set to 0 */ + uint32_t + level; /**< [in]: Specifies the encoding level. Client is recommended to set this to NV_ENC_LEVEL_AUTOSELECT in order to enable the NvEncodeAPI interface to select the correct level. */ + uint32_t + idrPeriod; /**< [in]: Specifies the IDR interval. If not set, this is made equal to gopLength in NV_ENC_CONFIG.Low latency application client can set IDR interval to NVENC_INFINITE_GOPLENGTH so that IDR frames are not inserted automatically. */ + uint32_t + separateColourPlaneFlag; /**< [in]: Set to 1 to enable 4:4:4 separate colour planes */ + uint32_t + disableDeblockingFilterIDC; /**< [in]: Specifies the deblocking filter mode. Permissible value range: [0,2]. This flag corresponds + to the flag disable_deblocking_filter_idc specified in section 7.4.3 of H.264 specification, + which specifies whether the operation of the deblocking filter shall be disabled across some + block edges of the slice and specifies for which edges the filtering is disabled. See section + 7.4.3 of H.264 specification for more details.*/ + uint32_t + numTemporalLayers; /**< [in]: Specifies number of temporal layers to be used for hierarchical coding / temporal SVC. Valid value range is [1,::NV_ENC_CAPS_NUM_MAX_TEMPORAL_LAYERS] */ + uint32_t spsId; /**< [in]: Specifies the SPS id of the sequence header */ + uint32_t ppsId; /**< [in]: Specifies the PPS id of the picture header */ + NV_ENC_H264_ADAPTIVE_TRANSFORM_MODE + adaptiveTransformMode; /**< [in]: Specifies the AdaptiveTransform Mode. Check support for AdaptiveTransform mode using ::NV_ENC_CAPS_SUPPORT_ADAPTIVE_TRANSFORM caps. */ + NV_ENC_H264_FMO_MODE + fmoMode; /**< [in]: Specified the FMO Mode. Check support for FMO using ::NV_ENC_CAPS_SUPPORT_FMO caps. */ + NV_ENC_H264_BDIRECT_MODE + bdirectMode; /**< [in]: Specifies the BDirect mode. Check support for BDirect mode using ::NV_ENC_CAPS_SUPPORT_BDIRECT_MODE caps.*/ + NV_ENC_H264_ENTROPY_CODING_MODE + entropyCodingMode; /**< [in]: Specifies the entropy coding mode. Check support for CABAC mode using ::NV_ENC_CAPS_SUPPORT_CABAC caps. */ + NV_ENC_STEREO_PACKING_MODE + stereoMode; /**< [in]: Specifies the stereo frame packing mode which is to be signaled in frame packing arrangement SEI */ + uint32_t + intraRefreshPeriod; /**< [in]: Specifies the interval between successive intra refresh if enableIntrarefresh is set. Requires enableIntraRefresh to be set. + Will be disabled if NV_ENC_CONFIG::gopLength is not set to NVENC_INFINITE_GOPLENGTH. */ + uint32_t + intraRefreshCnt; /**< [in]: Specifies the length of intra refresh in number of frames for periodic intra refresh. This value should be smaller than intraRefreshPeriod */ + uint32_t + maxNumRefFrames; /**< [in]: Specifies the DPB size used for encoding. Setting it to 0 will let driver use the default DPB size. + The low latency application which wants to invalidate reference frame as an error resilience tool + is recommended to use a large DPB size so that the encoder can keep old reference frames which can be used if recent + frames are invalidated. */ + uint32_t + sliceMode; /**< [in]: This parameter in conjunction with sliceModeData specifies the way in which the picture is divided into slices + sliceMode = 0 MB based slices, sliceMode = 1 Byte based slices, sliceMode = 2 MB row based slices, sliceMode = 3 numSlices in Picture. + When forceIntraRefreshWithFrameCnt is set it will have priority over sliceMode setting + When sliceMode == 0 and sliceModeData == 0 whole picture will be coded with one slice */ + uint32_t + sliceModeData; /**< [in]: Specifies the parameter needed for sliceMode. For: + sliceMode = 0, sliceModeData specifies # of MBs in each slice (except last slice) + sliceMode = 1, sliceModeData specifies maximum # of bytes in each slice (except last slice) + sliceMode = 2, sliceModeData specifies # of MB rows in each slice (except last slice) + sliceMode = 3, sliceModeData specifies number of slices in the picture. Driver will divide picture into slices optimally */ + NV_ENC_CONFIG_H264_VUI_PARAMETERS + h264VUIParameters; /**< [in]: Specifies the H264 video usability info parameters */ + uint32_t + ltrNumFrames; /**< [in]: Specifies the number of LTR frames. This parameter has different meaning in two LTR modes. + In "LTR Trust" mode (ltrTrustMode = 1), encoder will mark the first ltrNumFrames base layer reference frames within each IDR interval as LTR. + In "LTR Per Picture" mode (ltrTrustMode = 0 and ltrMarkFrame = 1), ltrNumFrames specifies maximum number of LTR frames in DPB. */ + uint32_t + ltrTrustMode; /**< [in]: Specifies the LTR operating mode. See comments near NV_ENC_CONFIG_H264::enableLTR for description of the two modes. + Set to 1 to use "LTR Trust" mode of LTR operation. Clients are discouraged to use "LTR Trust" mode as this mode may + be deprecated in future releases. + Set to 0 when using "LTR Per Picture" mode of LTR operation. */ + uint32_t + chromaFormatIDC; /**< [in]: Specifies the chroma format. Should be set to 1 for yuv420 input, 3 for yuv444 input. + Check support for YUV444 encoding using ::NV_ENC_CAPS_SUPPORT_YUV444_ENCODE caps.*/ + uint32_t + maxTemporalLayers; /**< [in]: Specifies the max temporal layer used for temporal SVC / hierarchical coding. + Defaut value of this field is NV_ENC_CAPS::NV_ENC_CAPS_NUM_MAX_TEMPORAL_LAYERS. Note that the value NV_ENC_CONFIG_H264::maxNumRefFrames should + be greater than or equal to (NV_ENC_CONFIG_H264::maxTemporalLayers - 2) * 2, for NV_ENC_CONFIG_H264::maxTemporalLayers >= 2.*/ + NV_ENC_BFRAME_REF_MODE + useBFramesAsRef; /**< [in]: Specifies the B-Frame as reference mode. Check support for useBFramesAsRef mode using ::NV_ENC_CAPS_SUPPORT_BFRAME_REF_MODE caps.*/ + NV_ENC_NUM_REF_FRAMES + numRefL0; /**< [in]: Specifies max number of reference frames in reference picture list L0, that can be used by hardware for prediction of a frame. + Check support for numRefL0 using ::NV_ENC_CAPS_SUPPORT_MULTIPLE_REF_FRAMES caps. */ + NV_ENC_NUM_REF_FRAMES + numRefL1; /**< [in]: Specifies max number of reference frames in reference picture list L1, that can be used by hardware for prediction of a frame. + Check support for numRefL1 using ::NV_ENC_CAPS_SUPPORT_MULTIPLE_REF_FRAMES caps. */ + + uint32_t reserved1[267]; /**< [in]: Reserved and must be set to 0 */ + void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ +} NV_ENC_CONFIG_H264; + +/** + * \struct _NV_ENC_CONFIG_HEVC + * HEVC encoder configuration parameters to be set during initialization. + */ +typedef struct _NV_ENC_CONFIG_HEVC { + uint32_t level; /**< [in]: Specifies the level of the encoded bitstream.*/ + uint32_t tier; /**< [in]: Specifies the level tier of the encoded bitstream.*/ + NV_ENC_HEVC_CUSIZE + minCUSize; /**< [in]: Specifies the minimum size of luma coding unit.*/ + NV_ENC_HEVC_CUSIZE + maxCUSize; /**< [in]: Specifies the maximum size of luma coding unit. Currently NVENC SDK only supports maxCUSize equal to NV_ENC_HEVC_CUSIZE_32x32.*/ + uint32_t + useConstrainedIntraPred : 1; /**< [in]: Set 1 to enable constrained intra prediction. */ + uint32_t + disableDeblockAcrossSliceBoundary : 1; /**< [in]: Set 1 to disable in loop filtering across slice boundary.*/ + uint32_t + outputBufferingPeriodSEI : 1; /**< [in]: Set 1 to write SEI buffering period syntax in the bitstream */ + uint32_t + outputPictureTimingSEI : 1; /**< [in]: Set 1 to write SEI picture timing syntax in the bitstream */ + uint32_t + outputAUD : 1; /**< [in]: Set 1 to write Access Unit Delimiter syntax. */ + uint32_t + enableLTR : 1; /**< [in]: Set to 1 to enable LTR (Long Term Reference) frame support. LTR can be used in two modes: "LTR Trust" mode and "LTR Per Picture" mode. + LTR Trust mode: In this mode, ltrNumFrames pictures after IDR are automatically marked as LTR. This mode is enabled by setting ltrTrustMode = 1. + Use of LTR Trust mode is strongly discouraged as this mode may be deprecated in future releases. + LTR Per Picture mode: In this mode, client can control whether the current picture should be marked as LTR. Enable this mode by setting + ltrTrustMode = 0 and ltrMarkFrame = 1 for the picture to be marked as LTR. This is the preferred mode + for using LTR. + Note that LTRs are not supported if encoding session is configured with B-frames */ + uint32_t + disableSPSPPS : 1; /**< [in]: Set 1 to disable VPS, SPS and PPS signaling in the bitstream. */ + uint32_t + repeatSPSPPS : 1; /**< [in]: Set 1 to output VPS,SPS and PPS for every IDR frame.*/ + uint32_t + enableIntraRefresh : 1; /**< [in]: Set 1 to enable gradual decoder refresh or intra refresh. If the GOP structure uses B frames this will be ignored */ + uint32_t + chromaFormatIDC : 2; /**< [in]: Specifies the chroma format. Should be set to 1 for yuv420 input, 3 for yuv444 input.*/ + uint32_t + pixelBitDepthMinus8 : 3; /**< [in]: Specifies pixel bit depth minus 8. Should be set to 0 for 8 bit input, 2 for 10 bit input.*/ + uint32_t + enableFillerDataInsertion : 1; /**< [in]: Set to 1 to enable insertion of filler data in the bitstream. + This flag will take effect only when one of the CBR rate + control modes (NV_ENC_PARAMS_RC_CBR, NV_ENC_PARAMS_RC_CBR_HQ, + NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ) is in use and both + NV_ENC_INITIALIZE_PARAMS::frameRateNum and + NV_ENC_INITIALIZE_PARAMS::frameRateDen are set to non-zero + values. Setting this field when + NV_ENC_INITIALIZE_PARAMS::enableOutputInVidmem is also set + is currently not supported and will make ::NvEncInitializeEncoder() + return an error. */ + uint32_t + enableConstrainedEncoding : 1; /**< [in]: Set this to 1 to enable constrainedFrame encoding where each slice in the constrained picture is independent of other slices. + Constrained encoding works only with rectangular slices. + Check support for constrained encoding using ::NV_ENC_CAPS_SUPPORT_CONSTRAINED_ENCODING caps. */ + uint32_t + enableAlphaLayerEncoding : 1; /**< [in]: Set this to 1 to enable HEVC encode with alpha layer. */ + uint32_t + singleSliceIntraRefresh : 1; /**< [in]: Set this to 1 to maintain single slice frames during intra refresh. + Check support for single slice intra refresh using ::NV_ENC_CAPS_SINGLE_SLICE_INTRA_REFRESH caps. + This flag will be ignored if the value returned for ::NV_ENC_CAPS_SINGLE_SLICE_INTRA_REFRESH caps is false. */ + uint32_t + outputRecoveryPointSEI : 1; /**< [in]: Set to 1 to enable writing of recovery point SEI message */ + uint32_t + outputTimeCodeSEI : 1; /**< [in]: Set 1 to write SEI time code syntax in the bitstream. Note that this flag will be ignored for D3D12 interface.*/ + uint32_t reserved : 12; /**< [in]: Reserved bitfields.*/ + uint32_t + idrPeriod; /**< [in]: Specifies the IDR interval. If not set, this is made equal to gopLength in NV_ENC_CONFIG. Low latency application client can set IDR interval to NVENC_INFINITE_GOPLENGTH so that IDR frames are not inserted automatically. */ + uint32_t + intraRefreshPeriod; /**< [in]: Specifies the interval between successive intra refresh if enableIntrarefresh is set. Requires enableIntraRefresh to be set. + Will be disabled if NV_ENC_CONFIG::gopLength is not set to NVENC_INFINITE_GOPLENGTH. */ + uint32_t + intraRefreshCnt; /**< [in]: Specifies the length of intra refresh in number of frames for periodic intra refresh. This value should be smaller than intraRefreshPeriod */ + uint32_t + maxNumRefFramesInDPB; /**< [in]: Specifies the maximum number of references frames in the DPB.*/ + uint32_t + ltrNumFrames; /**< [in]: This parameter has different meaning in two LTR modes. + In "LTR Trust" mode (ltrTrustMode = 1), encoder will mark the first ltrNumFrames base layer reference frames within each IDR interval as LTR. + In "LTR Per Picture" mode (ltrTrustMode = 0 and ltrMarkFrame = 1), ltrNumFrames specifies maximum number of LTR frames in DPB. + These ltrNumFrames acts as a guidance to the encoder and are not necessarily honored. To achieve a right balance between the encoding + quality and keeping LTR frames in the DPB queue, the encoder can internally limit the number of LTR frames. + The number of LTR frames actually used depends upon the encoding preset being used; Faster encoding presets will use fewer LTR frames.*/ + uint32_t vpsId; /**< [in]: Specifies the VPS id of the video parameter set */ + uint32_t spsId; /**< [in]: Specifies the SPS id of the sequence header */ + uint32_t ppsId; /**< [in]: Specifies the PPS id of the picture header */ + uint32_t + sliceMode; /**< [in]: This parameter in conjunction with sliceModeData specifies the way in which the picture is divided into slices + sliceMode = 0 CTU based slices, sliceMode = 1 Byte based slices, sliceMode = 2 CTU row based slices, sliceMode = 3, numSlices in Picture + When sliceMode == 0 and sliceModeData == 0 whole picture will be coded with one slice */ + uint32_t + sliceModeData; /**< [in]: Specifies the parameter needed for sliceMode. For: + sliceMode = 0, sliceModeData specifies # of CTUs in each slice (except last slice) + sliceMode = 1, sliceModeData specifies maximum # of bytes in each slice (except last slice) + sliceMode = 2, sliceModeData specifies # of CTU rows in each slice (except last slice) + sliceMode = 3, sliceModeData specifies number of slices in the picture. Driver will divide picture into slices optimally */ + uint32_t + maxTemporalLayersMinus1; /**< [in]: Specifies the max temporal layer used for hierarchical coding. */ + NV_ENC_CONFIG_HEVC_VUI_PARAMETERS + hevcVUIParameters; /**< [in]: Specifies the HEVC video usability info parameters */ + uint32_t + ltrTrustMode; /**< [in]: Specifies the LTR operating mode. See comments near NV_ENC_CONFIG_HEVC::enableLTR for description of the two modes. + Set to 1 to use "LTR Trust" mode of LTR operation. Clients are discouraged to use "LTR Trust" mode as this mode may + be deprecated in future releases. + Set to 0 when using "LTR Per Picture" mode of LTR operation. */ + NV_ENC_BFRAME_REF_MODE + useBFramesAsRef; /**< [in]: Specifies the B-Frame as reference mode. Check support for useBFramesAsRef mode using ::NV_ENC_CAPS_SUPPORT_BFRAME_REF_MODE caps.*/ + NV_ENC_NUM_REF_FRAMES + numRefL0; /**< [in]: Specifies max number of reference frames in reference picture list L0, that can be used by hardware for prediction of a frame. + Check support for numRefL0 using ::NV_ENC_CAPS_SUPPORT_MULTIPLE_REF_FRAMES caps. */ + NV_ENC_NUM_REF_FRAMES + numRefL1; /**< [in]: Specifies max number of reference frames in reference picture list L1, that can be used by hardware for prediction of a frame. + Check support for numRefL1 using ::NV_ENC_CAPS_SUPPORT_MULTIPLE_REF_FRAMES caps. */ + uint32_t reserved1[214]; /**< [in]: Reserved and must be set to 0.*/ + void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ +} NV_ENC_CONFIG_HEVC; + +#define NV_MAX_TILE_COLS_AV1 64 +#define NV_MAX_TILE_ROWS_AV1 64 + +/** + * \struct _NV_ENC_FILM_GRAIN_PARAMS_AV1 + * AV1 Film Grain Parameters structure + */ + +typedef struct _NV_ENC_FILM_GRAIN_PARAMS_AV1 { + uint32_t + applyGrain : 1; /**< [in]: Set to 1 to specify film grain should be added to frame */ + uint32_t + chromaScalingFromLuma : 1; /**< [in]: Set to 1 to specify the chroma scaling is inferred from luma scaling */ + uint32_t + overlapFlag : 1; /**< [in]: Set to 1 to indicate that overlap between film grain blocks should be applied*/ + uint32_t + clipToRestrictedRange : 1; /**< [in]: Set to 1 to clip values to restricted (studio) range after adding film grain */ + uint32_t + grainScalingMinus8 : 2; /**< [in]: Represents the shift - 8 applied to the values of the chroma component */ + uint32_t + arCoeffLag : 2; /**< [in]: Specifies the number of auto-regressive coefficients for luma and chroma */ + uint32_t + numYPoints : 4; /**< [in]: Specifies the number of points for the piecewise linear scaling function of the luma component */ + uint32_t + numCbPoints : 4; /**< [in]: Specifies the number of points for the piecewise linear scaling function of the cb component */ + uint32_t + numCrPoints : 4; /**< [in]: Specifies the number of points for the piecewise linear scaling function of the cr component */ + uint32_t + arCoeffShiftMinus6 : 2; /**< [in]: specifies the range of the auto-regressive coefficients */ + uint32_t + grainScaleShift : 2; /**< [in]: Specifies how much the Gaussian random numbers should be scaled down during the grain synthesi process */ + uint32_t reserved1 : 8; /**< [in]: Reserved bits field - should be set to 0 */ + uint8_t pointYValue + [14]; /**< [in]: pointYValue[i]: x coordinate for i-th point of luma piecewise linear scaling function. Values on a scale of 0...255 */ + uint8_t pointYScaling + [14]; /**< [in]: pointYScaling[i]: i-th point output value of luma piecewise linear scaling function */ + uint8_t pointCbValue + [10]; /**< [in]: pointCbValue[i]: x coordinate for i-th point of cb piecewise linear scaling function. Values on a scale of 0...255 */ + uint8_t pointCbScaling + [10]; /**< [in]: pointCbScaling[i]: i-th point output value of cb piecewise linear scaling function */ + uint8_t pointCrValue + [10]; /**< [in]: pointCrValue[i]: x coordinate for i-th point of cr piecewise linear scaling function. Values on a scale of 0...255 */ + uint8_t pointCrScaling + [10]; /**< [in]: pointCrScaling[i]: i-th point output value of cr piecewise linear scaling function */ + uint8_t arCoeffsYPlus128 + [24]; /**< [in]: Specifies auto-regressive coefficients used for the Y plane */ + uint8_t arCoeffsCbPlus128 + [25]; /**< [in]: Specifies auto-regressive coefficients used for the U plane */ + uint8_t arCoeffsCrPlus128 + [25]; /**< [in]: Specifies auto-regressive coefficients used for the V plane */ + uint8_t reserved2[2]; /**< [in]: Reserved bytes - should be set to 0 */ + uint8_t + cbMult; /**< [in]: Represents a multiplier for the cb component used in derivation of the input index to the cb component scaling function */ + uint8_t + cbLumaMult; /**< [in]: represents a multiplier for the average luma component used in derivation of the input index to the cb component scaling function. */ + uint16_t + cbOffset; /**< [in]: Represents an offset used in derivation of the input index to the cb component scaling function */ + uint8_t + crMult; /**< [in]: Represents a multiplier for the cr component used in derivation of the input index to the cr component scaling function */ + uint8_t + crLumaMult; /**< [in]: represents a multiplier for the average luma component used in derivation of the input index to the cr component scaling function. */ + uint16_t + crOffset; /**< [in]: Represents an offset used in derivation of the input index to the cr component scaling function */ +} NV_ENC_FILM_GRAIN_PARAMS_AV1; + +/** +* \struct _NV_ENC_CONFIG_AV1 +* AV1 encoder configuration parameters to be set during initialization. +*/ +typedef struct _NV_ENC_CONFIG_AV1 { + uint32_t level; /**< [in]: Specifies the level of the encoded bitstream.*/ + uint32_t tier; /**< [in]: Specifies the level tier of the encoded bitstream.*/ + NV_ENC_AV1_PART_SIZE + minPartSize; /**< [in]: Specifies the minimum size of luma coding block partition.*/ + NV_ENC_AV1_PART_SIZE + maxPartSize; /**< [in]: Specifies the maximum size of luma coding block partition.*/ + uint32_t + outputAnnexBFormat : 1; /**< [in]: Set 1 to use Annex B format for bitstream output.*/ + uint32_t + enableTimingInfo : 1; /**< [in]: Set 1 to write Timing Info into sequence/frame headers */ + uint32_t + enableDecoderModelInfo : 1; /**< [in]: Set 1 to write Decoder Model Info into sequence/frame headers */ + uint32_t + enableFrameIdNumbers : 1; /**< [in]: Set 1 to write Frame id numbers in bitstream */ + uint32_t + disableSeqHdr : 1; /**< [in]: Set 1 to disable Sequence Header signaling in the bitstream. */ + uint32_t + repeatSeqHdr : 1; /**< [in]: Set 1 to output Sequence Header for every Key frame.*/ + uint32_t + enableIntraRefresh : 1; /**< [in]: Set 1 to enable gradual decoder refresh or intra refresh. If the GOP structure uses B frames this will be ignored */ + uint32_t + chromaFormatIDC : 2; /**< [in]: Specifies the chroma format. Should be set to 1 for yuv420 input (yuv444 input currently not supported).*/ + uint32_t + enableBitstreamPadding : 1; /**< [in]: Set 1 to enable bitstream padding. */ + uint32_t + enableCustomTileConfig : 1; /**< [in]: Set 1 to enable custom tile configuration: numTileColumns and numTileRows must have non zero values and tileWidths and tileHeights must point to a valid address */ + uint32_t + enableFilmGrainParams : 1; /**< [in]: Set 1 to enable custom film grain parameters: filmGrainParams must point to a valid address */ + uint32_t + inputPixelBitDepthMinus8 : 3; /**< [in]: Specifies pixel bit depth minus 8 of video input. Should be set to 0 for 8 bit input, 2 for 10 bit input.*/ + uint32_t + pixelBitDepthMinus8 : 3; /**< [in]: Specifies pixel bit depth minus 8 of encoded video. Should be set to 0 for 8 bit, 2 for 10 bit. + HW will do the bitdepth conversion internally from inputPixelBitDepthMinus8 -> pixelBitDepthMinus8 if bit dpeths differ + Support for 8 bit input to 10 bit encode conversion only */ + uint32_t reserved : 14; /**< [in]: Reserved bitfields.*/ + uint32_t + idrPeriod; /**< [in]: Specifies the IDR/Key frame interval. If not set, this is made equal to gopLength in NV_ENC_CONFIG.Low latency application client can set IDR interval to NVENC_INFINITE_GOPLENGTH so that IDR frames are not inserted automatically. */ + uint32_t + intraRefreshPeriod; /**< [in]: Specifies the interval between successive intra refresh if enableIntrarefresh is set. Requires enableIntraRefresh to be set. + Will be disabled if NV_ENC_CONFIG::gopLength is not set to NVENC_INFINITE_GOPLENGTH. */ + uint32_t + intraRefreshCnt; /**< [in]: Specifies the length of intra refresh in number of frames for periodic intra refresh. This value should be smaller than intraRefreshPeriod */ + uint32_t + maxNumRefFramesInDPB; /**< [in]: Specifies the maximum number of references frames in the DPB.*/ + uint32_t + numTileColumns; /**< [in]: This parameter in conjunction with the flag enableCustomTileConfig and the array tileWidths[] specifies the way in which the picture is divided into tile columns. + When enableCustomTileConfig == 0, the picture will be uniformly divided into numTileColumns tile columns. If numTileColumns is not a power of 2, + it will be rounded down to the next power of 2 value. If numTileColumns == 0, the picture will be coded with the smallest number of vertical tiles as allowed by standard. + When enableCustomTileConfig == 1, numTileColumns must be > 0 and <= NV_MAX_TILE_COLS_AV1 and tileWidths must point to a valid array of numTileColumns entries. + Entry i specifies the width in 64x64 CTU unit of tile colum i. The sum of all the entries should be equal to the picture width in 64x64 CTU units. */ + uint32_t + numTileRows; /**< [in]: This parameter in conjunction with the flag enableCustomTileConfig and the array tileHeights[] specifies the way in which the picture is divided into tiles rows + When enableCustomTileConfig == 0, the picture will be uniformly divided into numTileRows tile rows. If numTileRows is not a power of 2, + it will be rounded down to the next power of 2 value. If numTileRows == 0, the picture will be coded with the smallest number of horizontal tiles as allowed by standard. + When enableCustomTileConfig == 1, numTileRows must be > 0 and <= NV_MAX_TILE_ROWS_AV1 and tileHeights must point to a valid array of numTileRows entries. + Entry i specifies the height in 64x64 CTU unit of tile row i. The sum of all the entries should be equal to the picture hieght in 64x64 CTU units. */ + uint32_t* + tileWidths; /**< [in]: If enableCustomTileConfig == 1, tileWidths[i] specifies the width of tile column i in 64x64 CTU unit, with 0 <= i <= numTileColumns -1. */ + uint32_t* + tileHeights; /**< [in]: If enableCustomTileConfig == 1, tileHeights[i] specifies the height of tile row i in 64x64 CTU unit, with 0 <= i <= numTileRows -1. */ + uint32_t + maxTemporalLayersMinus1; /**< [in]: Specifies the max temporal layer used for hierarchical coding. */ + NV_ENC_VUI_COLOR_PRIMARIES + colorPrimaries; /**< [in]: as defined in section of ISO/IEC 23091-4/ITU-T H.273 */ + NV_ENC_VUI_TRANSFER_CHARACTERISTIC + transferCharacteristics; /**< [in]: as defined in section of ISO/IEC 23091-4/ITU-T H.273 */ + NV_ENC_VUI_MATRIX_COEFFS + matrixCoefficients; /**< [in]: as defined in section of ISO/IEC 23091-4/ITU-T H.273 */ + uint32_t + colorRange; /**< [in]: 0: studio swing representation - 1: full swing representation */ + uint32_t chromaSamplePosition; /**< [in]: 0: unknown + 1: Horizontally collocated with luma (0,0) sample, between two vertical samples + 2: Co-located with luma (0,0) sample */ + NV_ENC_BFRAME_REF_MODE + useBFramesAsRef; /**< [in]: Specifies the B-Frame as reference mode. Check support for useBFramesAsRef mode using ::NV_ENC_CAPS_SUPPORT_BFRAME_REF_MODE caps.*/ + NV_ENC_FILM_GRAIN_PARAMS_AV1* + filmGrainParams; /**< [in]: If enableFilmGrainParams == 1, filmGrainParams must point to a valid NV_ENC_FILM_GRAIN_PARAMS_AV1 structure */ + NV_ENC_NUM_REF_FRAMES + numFwdRefs; /**< [in]: Specifies max number of forward reference frame used for prediction of a frame. It must be in range 1-4 (Last, Last2, last3 and Golden). It's a suggestive value not necessarily be honored always. */ + NV_ENC_NUM_REF_FRAMES + numBwdRefs; /**< [in]: Specifies max number of L1 list reference frame used for prediction of a frame. It must be in range 1-3 (Backward, Altref2, Altref). It's a suggestive value not necessarily be honored always. */ + uint32_t reserved1[235]; /**< [in]: Reserved and must be set to 0.*/ + void* reserved2[62]; /**< [in]: Reserved and must be set to NULL */ +} NV_ENC_CONFIG_AV1; + +/** + * \struct _NV_ENC_CONFIG_H264_MEONLY + * H264 encoder configuration parameters for ME only Mode + * + */ +typedef struct _NV_ENC_CONFIG_H264_MEONLY { + uint32_t + disablePartition16x16 : 1; /**< [in]: Disable Motion Estimation on 16x16 blocks*/ + uint32_t + disablePartition8x16 : 1; /**< [in]: Disable Motion Estimation on 8x16 blocks*/ + uint32_t + disablePartition16x8 : 1; /**< [in]: Disable Motion Estimation on 16x8 blocks*/ + uint32_t + disablePartition8x8 : 1; /**< [in]: Disable Motion Estimation on 8x8 blocks*/ + uint32_t + disableIntraSearch : 1; /**< [in]: Disable Intra search during Motion Estimation*/ + uint32_t + bStereoEnable : 1; /**< [in]: Enable Stereo Mode for Motion Estimation where each view is independently executed*/ + uint32_t reserved : 26; /**< [in]: Reserved and must be set to 0 */ + uint32_t reserved1[255]; /**< [in]: Reserved and must be set to 0 */ + void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ +} NV_ENC_CONFIG_H264_MEONLY; + +/** + * \struct _NV_ENC_CONFIG_HEVC_MEONLY + * HEVC encoder configuration parameters for ME only Mode + * + */ +typedef struct _NV_ENC_CONFIG_HEVC_MEONLY { + uint32_t reserved[256]; /**< [in]: Reserved and must be set to 0 */ + void* reserved1[64]; /**< [in]: Reserved and must be set to NULL */ +} NV_ENC_CONFIG_HEVC_MEONLY; + +/** + * \struct _NV_ENC_CODEC_CONFIG + * Codec-specific encoder configuration parameters to be set during initialization. + */ +typedef union _NV_ENC_CODEC_CONFIG { + NV_ENC_CONFIG_H264 + h264Config; /**< [in]: Specifies the H.264-specific encoder configuration. */ + NV_ENC_CONFIG_HEVC + hevcConfig; /**< [in]: Specifies the HEVC-specific encoder configuration. */ + NV_ENC_CONFIG_AV1 + av1Config; /**< [in]: Specifies the AV1-specific encoder configuration. */ + NV_ENC_CONFIG_H264_MEONLY + h264MeOnlyConfig; /**< [in]: Specifies the H.264-specific ME only encoder configuration. */ + NV_ENC_CONFIG_HEVC_MEONLY + hevcMeOnlyConfig; /**< [in]: Specifies the HEVC-specific ME only encoder configuration. */ + uint32_t reserved[320]; /**< [in]: Reserved and must be set to 0 */ +} NV_ENC_CODEC_CONFIG; + +/** + * \struct _NV_ENC_CONFIG + * Encoder configuration parameters to be set during initialization. + */ +typedef struct _NV_ENC_CONFIG { + uint32_t + version; /**< [in]: Struct version. Must be set to ::NV_ENC_CONFIG_VER. */ + GUID + profileGUID; /**< [in]: Specifies the codec profile GUID. If client specifies \p NV_ENC_CODEC_PROFILE_AUTOSELECT_GUID the NvEncodeAPI interface will select the appropriate codec profile. */ + uint32_t + gopLength; /**< [in]: Specifies the number of pictures in one GOP. Low latency application client can set goplength to NVENC_INFINITE_GOPLENGTH so that keyframes are not inserted automatically. */ + int32_t + frameIntervalP; /**< [in]: Specifies the GOP pattern as follows: \p frameIntervalP = 0: I, 1: IPP, 2: IBP, 3: IBBP If goplength is set to NVENC_INFINITE_GOPLENGTH \p frameIntervalP should be set to 1. */ + uint32_t + monoChromeEncoding; /**< [in]: Set this to 1 to enable monochrome encoding for this session. */ + NV_ENC_PARAMS_FRAME_FIELD_MODE + frameFieldMode; /**< [in]: Specifies the frame/field mode. + Check support for field encoding using ::NV_ENC_CAPS_SUPPORT_FIELD_ENCODING caps. + Using a frameFieldMode other than NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME for RGB input is not supported. */ + NV_ENC_MV_PRECISION + mvPrecision; /**< [in]: Specifies the desired motion vector prediction precision. */ + NV_ENC_RC_PARAMS + rcParams; /**< [in]: Specifies the rate control parameters for the current encoding session. */ + NV_ENC_CODEC_CONFIG + encodeCodecConfig; /**< [in]: Specifies the codec specific config parameters through this union. */ + uint32_t reserved[278]; /**< [in]: Reserved and must be set to 0 */ + void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ +} NV_ENC_CONFIG; + +/** macro for constructing the version field of ::_NV_ENC_CONFIG */ +#define NV_ENC_CONFIG_VER (NVENCAPI_STRUCT_VERSION(8) | (1 << 31)) + +/** + * Tuning information of NVENC encoding (TuningInfo is not applicable to H264 and HEVC MEOnly mode). + */ +typedef enum NV_ENC_TUNING_INFO { + NV_ENC_TUNING_INFO_UNDEFINED = + 0, /**< Undefined tuningInfo. Invalid value for encoding. */ + NV_ENC_TUNING_INFO_HIGH_QUALITY = + 1, /**< Tune presets for latency tolerant encoding.*/ + NV_ENC_TUNING_INFO_LOW_LATENCY = + 2, /**< Tune presets for low latency streaming.*/ + NV_ENC_TUNING_INFO_ULTRA_LOW_LATENCY = + 3, /**< Tune presets for ultra low latency streaming.*/ + NV_ENC_TUNING_INFO_LOSSLESS = 4, /**< Tune presets for lossless encoding.*/ + NV_ENC_TUNING_INFO_COUNT /**< Count number of tuningInfos. Invalid value. */ +} NV_ENC_TUNING_INFO; + +/** + * \struct _NV_ENC_INITIALIZE_PARAMS + * Encode Session Initialization parameters. + */ +typedef struct _NV_ENC_INITIALIZE_PARAMS { + uint32_t + version; /**< [in]: Struct version. Must be set to ::NV_ENC_INITIALIZE_PARAMS_VER. */ + GUID + encodeGUID; /**< [in]: Specifies the Encode GUID for which the encoder is being created. ::NvEncInitializeEncoder() API will fail if this is not set, or set to unsupported value. */ + GUID + presetGUID; /**< [in]: Specifies the preset for encoding. If the preset GUID is set then , the preset configuration will be applied before any other parameter. */ + uint32_t + encodeWidth; /**< [in]: Specifies the encode width. If not set ::NvEncInitializeEncoder() API will fail. */ + uint32_t + encodeHeight; /**< [in]: Specifies the encode height. If not set ::NvEncInitializeEncoder() API will fail. */ + uint32_t + darWidth; /**< [in]: Specifies the display aspect ratio width (H264/HEVC) or the render width (AV1). */ + uint32_t + darHeight; /**< [in]: Specifies the display aspect ratio height (H264/HEVC) or the render height (AV1). */ + uint32_t + frameRateNum; /**< [in]: Specifies the numerator for frame rate used for encoding in frames per second ( Frame rate = frameRateNum / frameRateDen ). */ + uint32_t + frameRateDen; /**< [in]: Specifies the denominator for frame rate used for encoding in frames per second ( Frame rate = frameRateNum / frameRateDen ). */ + uint32_t + enableEncodeAsync; /**< [in]: Set this to 1 to enable asynchronous mode and is expected to use events to get picture completion notification. */ + uint32_t + enablePTD; /**< [in]: Set this to 1 to enable the Picture Type Decision is be taken by the NvEncodeAPI interface. */ + uint32_t + reportSliceOffsets : 1; /**< [in]: Set this to 1 to enable reporting slice offsets in ::_NV_ENC_LOCK_BITSTREAM. NV_ENC_INITIALIZE_PARAMS::enableEncodeAsync must be set to 0 to use this feature. Client must set this to 0 if NV_ENC_CONFIG_H264::sliceMode is 1 on Kepler GPUs */ + uint32_t + enableSubFrameWrite : 1; /**< [in]: Set this to 1 to write out available bitstream to memory at subframe intervals. + If enableSubFrameWrite = 1, then the hardware encoder returns data as soon as a slice (H264/HEVC) or tile (AV1) has completed encoding. + This results in better encoding latency, but the downside is that the application has to keep polling via a call to nvEncLockBitstream API continuously to see if any encoded slice/tile data is available. + Use this mode if you feel that the marginal reduction in latency from sub-frame encoding is worth the increase in complexity due to CPU-based polling. */ + uint32_t + enableExternalMEHints : 1; /**< [in]: Set to 1 to enable external ME hints for the current frame. For NV_ENC_INITIALIZE_PARAMS::enablePTD=1 with B frames, programming L1 hints is optional for B frames since Client doesn't know internal GOP structure. + NV_ENC_PIC_PARAMS::meHintRefPicDist should preferably be set with enablePTD=1. */ + uint32_t enableMEOnlyMode : 1; /**< [in]: Set to 1 to enable ME Only Mode .*/ + uint32_t + enableWeightedPrediction : 1; /**< [in]: Set this to 1 to enable weighted prediction. Not supported if encode session is configured for B-Frames (i.e. NV_ENC_CONFIG::frameIntervalP > 1 or preset >=P3 when tuningInfo = ::NV_ENC_TUNING_INFO_HIGH_QUALITY or + tuningInfo = ::NV_ENC_TUNING_INFO_LOSSLESS. This is because preset >=p3 internally enables B frames when tuningInfo = ::NV_ENC_TUNING_INFO_HIGH_QUALITY or ::NV_ENC_TUNING_INFO_LOSSLESS). */ + uint32_t + enableOutputInVidmem : 1; /**< [in]: Set this to 1 to enable output of NVENC in video memory buffer created by application. This feature is not supported for HEVC ME only mode. */ + uint32_t + reservedBitFields : 26; /**< [in]: Reserved bitfields and must be set to 0 */ + uint32_t + privDataSize; /**< [in]: Reserved private data buffer size and must be set to 0 */ + void* + privData; /**< [in]: Reserved private data buffer and must be set to NULL */ + NV_ENC_CONFIG* + encodeConfig; /**< [in]: Specifies the advanced codec specific structure. If client has sent a valid codec config structure, it will override parameters set by the NV_ENC_INITIALIZE_PARAMS::presetGUID parameter. If set to NULL the NvEncodeAPI interface will use the NV_ENC_INITIALIZE_PARAMS::presetGUID to set the codec specific parameters. + Client can also optionally query the NvEncodeAPI interface to get codec specific parameters for a presetGUID using ::NvEncGetEncodePresetConfig() API. It can then modify (if required) some of the codec config parameters and send down a custom config structure as part of ::_NV_ENC_INITIALIZE_PARAMS. + Even in this case client is recommended to pass the same preset guid it has used in ::NvEncGetEncodePresetConfig() API to query the config structure; as NV_ENC_INITIALIZE_PARAMS::presetGUID. This will not override the custom config structure but will be used to determine other Encoder HW specific parameters not exposed in the API. */ + uint32_t + maxEncodeWidth; /**< [in]: Maximum encode width to be used for current Encode session. + Client should allocate output buffers according to this dimension for dynamic resolution change. If set to 0, Encoder will not allow dynamic resolution change. */ + uint32_t + maxEncodeHeight; /**< [in]: Maximum encode height to be allowed for current Encode session. + Client should allocate output buffers according to this dimension for dynamic resolution change. If set to 0, Encode will not allow dynamic resolution change. */ + NVENC_EXTERNAL_ME_HINT_COUNTS_PER_BLOCKTYPE maxMEHintCountsPerBlock + [2]; /**< [in]: If Client wants to pass external motion vectors in NV_ENC_PIC_PARAMS::meExternalHints buffer it must specify the maximum number of hint candidates per block per direction for the encode session. + The NV_ENC_INITIALIZE_PARAMS::maxMEHintCountsPerBlock[0] is for L0 predictors and NV_ENC_INITIALIZE_PARAMS::maxMEHintCountsPerBlock[1] is for L1 predictors. + This client must also set NV_ENC_INITIALIZE_PARAMS::enableExternalMEHints to 1. */ + NV_ENC_TUNING_INFO + tuningInfo; /**< [in]: Tuning Info of NVENC encoding(TuningInfo is not applicable to H264 and HEVC meonly mode). */ + NV_ENC_BUFFER_FORMAT + bufferFormat; /**< [in]: Input buffer format. Used only when DX12 interface type is used */ + uint32_t reserved[287]; /**< [in]: Reserved and must be set to 0 */ + void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ +} NV_ENC_INITIALIZE_PARAMS; + +/** macro for constructing the version field of ::_NV_ENC_INITIALIZE_PARAMS */ +#define NV_ENC_INITIALIZE_PARAMS_VER (NVENCAPI_STRUCT_VERSION(5) | (1 << 31)) + +/** + * \struct _NV_ENC_RECONFIGURE_PARAMS + * Encode Session Reconfigured parameters. + */ +typedef struct _NV_ENC_RECONFIGURE_PARAMS { + uint32_t + version; /**< [in]: Struct version. Must be set to ::NV_ENC_RECONFIGURE_PARAMS_VER. */ + NV_ENC_INITIALIZE_PARAMS + reInitEncodeParams; /**< [in]: Encoder session re-initialization parameters. + If reInitEncodeParams.encodeConfig is NULL and + reInitEncodeParams.presetGUID is the same as the preset + GUID specified on the call to NvEncInitializeEncoder(), + EncodeAPI will continue to use the existing encode + configuration. + If reInitEncodeParams.encodeConfig is NULL and + reInitEncodeParams.presetGUID is different from the preset + GUID specified on the call to NvEncInitializeEncoder(), + EncodeAPI will try to use the default configuration for + the preset specified by reInitEncodeParams.presetGUID. + In this case, reconfiguration may fail if the new + configuration is incompatible with the existing + configuration (e.g. the new configuration results in + a change in the GOP structure). */ + uint32_t + resetEncoder : 1; /**< [in]: This resets the rate control states and other internal encoder states. This should be used only with an IDR frame. + If NV_ENC_INITIALIZE_PARAMS::enablePTD is set to 1, encoder will force the frame type to IDR */ + uint32_t + forceIDR : 1; /**< [in]: Encode the current picture as an IDR picture. This flag is only valid when Picture type decision is taken by the Encoder + [_NV_ENC_INITIALIZE_PARAMS::enablePTD == 1]. */ + uint32_t reserved : 30; + +} NV_ENC_RECONFIGURE_PARAMS; + +/** macro for constructing the version field of ::_NV_ENC_RECONFIGURE_PARAMS */ +#define NV_ENC_RECONFIGURE_PARAMS_VER (NVENCAPI_STRUCT_VERSION(1) | (1 << 31)) + +/** + * \struct _NV_ENC_PRESET_CONFIG + * Encoder preset config + */ +typedef struct _NV_ENC_PRESET_CONFIG { + uint32_t + version; /**< [in]: Struct version. Must be set to ::NV_ENC_PRESET_CONFIG_VER. */ + NV_ENC_CONFIG + presetCfg; /**< [out]: preset config returned by the Nvidia Video Encoder interface. */ + uint32_t reserved1[255]; /**< [in]: Reserved and must be set to 0 */ + void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ +} NV_ENC_PRESET_CONFIG; + +/** macro for constructing the version field of ::_NV_ENC_PRESET_CONFIG */ +#define NV_ENC_PRESET_CONFIG_VER (NVENCAPI_STRUCT_VERSION(4) | (1 << 31)) + +/** + * \struct _NV_ENC_PIC_PARAMS_MVC + * MVC-specific parameters to be sent on a per-frame basis. + */ +typedef struct _NV_ENC_PIC_PARAMS_MVC { + uint32_t + version; /**< [in]: Struct version. Must be set to ::NV_ENC_PIC_PARAMS_MVC_VER. */ + uint32_t + viewID; /**< [in]: Specifies the view ID associated with the current input view. */ + uint32_t + temporalID; /**< [in]: Specifies the temporal ID associated with the current input view. */ + uint32_t + priorityID; /**< [in]: Specifies the priority ID associated with the current input view. Reserved and ignored by the NvEncodeAPI interface. */ + uint32_t reserved1[12]; /**< [in]: Reserved and must be set to 0. */ + void* reserved2[8]; /**< [in]: Reserved and must be set to NULL. */ +} NV_ENC_PIC_PARAMS_MVC; + +/** macro for constructing the version field of ::_NV_ENC_PIC_PARAMS_MVC */ +#define NV_ENC_PIC_PARAMS_MVC_VER NVENCAPI_STRUCT_VERSION(1) + +/** + * \union _NV_ENC_PIC_PARAMS_H264_EXT + * H264 extension picture parameters + */ +typedef union _NV_ENC_PIC_PARAMS_H264_EXT { + NV_ENC_PIC_PARAMS_MVC + mvcPicParams; /**< [in]: Specifies the MVC picture parameters. */ + uint32_t reserved1[32]; /**< [in]: Reserved and must be set to 0. */ +} NV_ENC_PIC_PARAMS_H264_EXT; + +/** + * \struct _NV_ENC_SEI_PAYLOAD + * User SEI message + */ +typedef struct _NV_ENC_SEI_PAYLOAD { + uint32_t + payloadSize; /**< [in] SEI payload size in bytes. SEI payload must be byte aligned, as described in Annex D */ + uint32_t + payloadType; /**< [in] SEI payload types and syntax can be found in Annex D of the H.264 Specification. */ + uint8_t* payload; /**< [in] pointer to user data */ +} NV_ENC_SEI_PAYLOAD; + +#define NV_ENC_H264_SEI_PAYLOAD NV_ENC_SEI_PAYLOAD + +/** + * \struct _NV_ENC_PIC_PARAMS_H264 + * H264 specific enc pic params. sent on a per frame basis. + */ +typedef struct _NV_ENC_PIC_PARAMS_H264 { + uint32_t + displayPOCSyntax; /**< [in]: Specifies the display POC syntax This is required to be set if client is handling the picture type decision. */ + uint32_t reserved3; /**< [in]: Reserved and must be set to 0 */ + uint32_t + refPicFlag; /**< [in]: Set to 1 for a reference picture. This is ignored if NV_ENC_INITIALIZE_PARAMS::enablePTD is set to 1. */ + uint32_t + colourPlaneId; /**< [in]: Specifies the colour plane ID associated with the current input. */ + uint32_t + forceIntraRefreshWithFrameCnt; /**< [in]: Forces an intra refresh with duration equal to intraRefreshFrameCnt. + When outputRecoveryPointSEI is set this is value is used for recovery_frame_cnt in recovery point SEI message + forceIntraRefreshWithFrameCnt cannot be used if B frames are used in the GOP structure specified */ + uint32_t + constrainedFrame : 1; /**< [in]: Set to 1 if client wants to encode this frame with each slice completely independent of other slices in the frame. + NV_ENC_INITIALIZE_PARAMS::enableConstrainedEncoding should be set to 1 */ + uint32_t + sliceModeDataUpdate : 1; /**< [in]: Set to 1 if client wants to change the sliceModeData field to specify new sliceSize Parameter + When forceIntraRefreshWithFrameCnt is set it will have priority over sliceMode setting */ + uint32_t + ltrMarkFrame : 1; /**< [in]: Set to 1 if client wants to mark this frame as LTR */ + uint32_t + ltrUseFrames : 1; /**< [in]: Set to 1 if client allows encoding this frame using the LTR frames specified in ltrFrameBitmap */ + uint32_t + reservedBitFields : 28; /**< [in]: Reserved bit fields and must be set to 0 */ + uint8_t* sliceTypeData; /**< [in]: Deprecated. */ + uint32_t sliceTypeArrayCnt; /**< [in]: Deprecated. */ + uint32_t + seiPayloadArrayCnt; /**< [in]: Specifies the number of elements allocated in seiPayloadArray array. */ + NV_ENC_SEI_PAYLOAD* + seiPayloadArray; /**< [in]: Array of SEI payloads which will be inserted for this frame. */ + uint32_t + sliceMode; /**< [in]: This parameter in conjunction with sliceModeData specifies the way in which the picture is divided into slices + sliceMode = 0 MB based slices, sliceMode = 1 Byte based slices, sliceMode = 2 MB row based slices, sliceMode = 3, numSlices in Picture + When forceIntraRefreshWithFrameCnt is set it will have priority over sliceMode setting + When sliceMode == 0 and sliceModeData == 0 whole picture will be coded with one slice */ + uint32_t + sliceModeData; /**< [in]: Specifies the parameter needed for sliceMode. For: + sliceMode = 0, sliceModeData specifies # of MBs in each slice (except last slice) + sliceMode = 1, sliceModeData specifies maximum # of bytes in each slice (except last slice) + sliceMode = 2, sliceModeData specifies # of MB rows in each slice (except last slice) + sliceMode = 3, sliceModeData specifies number of slices in the picture. Driver will divide picture into slices optimally */ + uint32_t + ltrMarkFrameIdx; /**< [in]: Specifies the long term referenceframe index to use for marking this frame as LTR.*/ + uint32_t + ltrUseFrameBitmap; /**< [in]: Specifies the associated bitmap of LTR frame indices to use when encoding this frame. */ + uint32_t + ltrUsageMode; /**< [in]: Not supported. Reserved for future use and must be set to 0. */ + uint32_t + forceIntraSliceCount; /**< [in]: Specifies the number of slices to be forced to Intra in the current picture. + This option along with forceIntraSliceIdx[] array needs to be used with sliceMode = 3 only */ + uint32_t* + forceIntraSliceIdx; /**< [in]: Slice indices to be forced to intra in the current picture. Each slice index should be <= num_slices_in_picture -1. Index starts from 0 for first slice. + The number of entries in this array should be equal to forceIntraSliceCount */ + NV_ENC_PIC_PARAMS_H264_EXT + h264ExtPicParams; /**< [in]: Specifies the H264 extension config parameters using this config. */ + NV_ENC_TIME_CODE + timeCode; /**< [in]: Specifies the clock timestamp sets used in picture timing SEI. Applicable only when NV_ENC_CONFIG_H264::enableTimeCode is set to 1. */ + uint32_t reserved[203]; /**< [in]: Reserved and must be set to 0. */ + void* reserved2[61]; /**< [in]: Reserved and must be set to NULL. */ +} NV_ENC_PIC_PARAMS_H264; + +/** + * \struct _NV_ENC_PIC_PARAMS_HEVC + * HEVC specific enc pic params. sent on a per frame basis. + */ +typedef struct _NV_ENC_PIC_PARAMS_HEVC { + uint32_t + displayPOCSyntax; /**< [in]: Specifies the display POC syntax This is required to be set if client is handling the picture type decision. */ + uint32_t + refPicFlag; /**< [in]: Set to 1 for a reference picture. This is ignored if NV_ENC_INITIALIZE_PARAMS::enablePTD is set to 1. */ + uint32_t temporalId; /**< [in]: Specifies the temporal id of the picture */ + uint32_t + forceIntraRefreshWithFrameCnt; /**< [in]: Forces an intra refresh with duration equal to intraRefreshFrameCnt. + When outputRecoveryPointSEI is set this is value is used for recovery_frame_cnt in recovery point SEI message + forceIntraRefreshWithFrameCnt cannot be used if B frames are used in the GOP structure specified */ + uint32_t + constrainedFrame : 1; /**< [in]: Set to 1 if client wants to encode this frame with each slice completely independent of other slices in the frame. + NV_ENC_INITIALIZE_PARAMS::enableConstrainedEncoding should be set to 1 */ + uint32_t + sliceModeDataUpdate : 1; /**< [in]: Set to 1 if client wants to change the sliceModeData field to specify new sliceSize Parameter + When forceIntraRefreshWithFrameCnt is set it will have priority over sliceMode setting */ + uint32_t + ltrMarkFrame : 1; /**< [in]: Set to 1 if client wants to mark this frame as LTR */ + uint32_t + ltrUseFrames : 1; /**< [in]: Set to 1 if client allows encoding this frame using the LTR frames specified in ltrFrameBitmap */ + uint32_t + reservedBitFields : 28; /**< [in]: Reserved bit fields and must be set to 0 */ + uint8_t* + sliceTypeData; /**< [in]: Array which specifies the slice type used to force intra slice for a particular slice. Currently supported only for NV_ENC_CONFIG_H264::sliceMode == 3. + Client should allocate array of size sliceModeData where sliceModeData is specified in field of ::_NV_ENC_CONFIG_H264 + Array element with index n corresponds to nth slice. To force a particular slice to intra client should set corresponding array element to NV_ENC_SLICE_TYPE_I + all other array elements should be set to NV_ENC_SLICE_TYPE_DEFAULT */ + uint32_t + sliceTypeArrayCnt; /**< [in]: Client should set this to the number of elements allocated in sliceTypeData array. If sliceTypeData is NULL then this should be set to 0 */ + uint32_t + sliceMode; /**< [in]: This parameter in conjunction with sliceModeData specifies the way in which the picture is divided into slices + sliceMode = 0 CTU based slices, sliceMode = 1 Byte based slices, sliceMode = 2 CTU row based slices, sliceMode = 3, numSlices in Picture + When forceIntraRefreshWithFrameCnt is set it will have priority over sliceMode setting + When sliceMode == 0 and sliceModeData == 0 whole picture will be coded with one slice */ + uint32_t + sliceModeData; /**< [in]: Specifies the parameter needed for sliceMode. For: + sliceMode = 0, sliceModeData specifies # of CTUs in each slice (except last slice) + sliceMode = 1, sliceModeData specifies maximum # of bytes in each slice (except last slice) + sliceMode = 2, sliceModeData specifies # of CTU rows in each slice (except last slice) + sliceMode = 3, sliceModeData specifies number of slices in the picture. Driver will divide picture into slices optimally */ + uint32_t + ltrMarkFrameIdx; /**< [in]: Specifies the long term reference frame index to use for marking this frame as LTR.*/ + uint32_t + ltrUseFrameBitmap; /**< [in]: Specifies the associated bitmap of LTR frame indices to use when encoding this frame. */ + uint32_t + ltrUsageMode; /**< [in]: Not supported. Reserved for future use and must be set to 0. */ + uint32_t + seiPayloadArrayCnt; /**< [in]: Specifies the number of elements allocated in seiPayloadArray array. */ + uint32_t reserved; /**< [in]: Reserved and must be set to 0. */ + NV_ENC_SEI_PAYLOAD* + seiPayloadArray; /**< [in]: Array of SEI payloads which will be inserted for this frame. */ + NV_ENC_TIME_CODE + timeCode; /**< [in]: Specifies the clock timestamp sets used in time code SEI. Applicable only when NV_ENC_CONFIG_HEVC::enableTimeCodeSEI is set to 1. */ + uint32_t reserved2[237]; /**< [in]: Reserved and must be set to 0. */ + void* reserved3[61]; /**< [in]: Reserved and must be set to NULL. */ +} NV_ENC_PIC_PARAMS_HEVC; + +#define NV_ENC_AV1_OBU_PAYLOAD NV_ENC_SEI_PAYLOAD + +/** +* \struct _NV_ENC_PIC_PARAMS_AV1 +* AV1 specific enc pic params. sent on a per frame basis. +*/ +typedef struct _NV_ENC_PIC_PARAMS_AV1 { + uint32_t + displayPOCSyntax; /**< [in]: Specifies the display POC syntax This is required to be set if client is handling the picture type decision. */ + uint32_t + refPicFlag; /**< [in]: Set to 1 for a reference picture. This is ignored if NV_ENC_INITIALIZE_PARAMS::enablePTD is set to 1. */ + uint32_t temporalId; /**< [in]: Specifies the temporal id of the picture */ + uint32_t + forceIntraRefreshWithFrameCnt; /**< [in]: Forces an intra refresh with duration equal to intraRefreshFrameCnt. + forceIntraRefreshWithFrameCnt cannot be used if B frames are used in the GOP structure specified */ + uint32_t + goldenFrameFlag : 1; /**< [in]: Encode frame as Golden Frame. This is ignored if NV_ENC_INITIALIZE_PARAMS::enablePTD is set to 1. */ + uint32_t + arfFrameFlag : 1; /**< [in]: Encode frame as Alternate Reference Frame. This is ignored if NV_ENC_INITIALIZE_PARAMS::enablePTD is set to 1. */ + uint32_t + arf2FrameFlag : 1; /**< [in]: Encode frame as Alternate Reference 2 Frame. This is ignored if NV_ENC_INITIALIZE_PARAMS::enablePTD is set to 1. */ + uint32_t + bwdFrameFlag : 1; /**< [in]: Encode frame as Backward Reference Frame. This is ignored if NV_ENC_INITIALIZE_PARAMS::enablePTD is set to 1. */ + uint32_t + overlayFrameFlag : 1; /**< [in]: Encode frame as overlay frame. A previously encoded frame with the same displayPOCSyntax value should be present in reference frame buffer. + This is ignored if NV_ENC_INITIALIZE_PARAMS::enablePTD is set to 1. */ + uint32_t + showExistingFrameFlag : 1; /**< [in]: When ovelayFrameFlag is set to 1, this flag controls the value of the show_existing_frame syntax element associated with the overlay frame. + This flag is added to the interface as a placeholder. Its value is ignored for now and always assumed to be set to 1. + This is ignored if NV_ENC_INITIALIZE_PARAMS::enablePTD is set to 1. */ + uint32_t + errorResilientModeFlag : 1; /**< [in]: encode frame independently from previously encoded frames */ + + uint32_t + tileConfigUpdate : 1; /**< [in]: Set to 1 if client wants to overwrite the default tile configuration with the tile parameters specified below + When forceIntraRefreshWithFrameCnt is set it will have priority over tileConfigUpdate setting */ + uint32_t + enableCustomTileConfig : 1; /**< [in]: Set 1 to enable custom tile configuration: numTileColumns and numTileRows must have non zero values and tileWidths and tileHeights must point to a valid address */ + uint32_t + filmGrainParamsUpdate : 1; /**< [in]: Set to 1 if client wants to update previous film grain parameters: filmGrainParams must point to a valid address and encoder must have been configured with film grain enabled */ + uint32_t + reservedBitFields : 22; /**< [in]: Reserved bitfields and must be set to 0 */ + uint32_t + numTileColumns; /**< [in]: This parameter in conjunction with the flag enableCustomTileConfig and the array tileWidths[] specifies the way in which the picture is divided into tile columns. + When enableCustomTileConfig == 0, the picture will be uniformly divided into numTileColumns tile columns. If numTileColumns is not a power of 2, + it will be rounded down to the next power of 2 value. If numTileColumns == 0, the picture will be coded with the smallest number of vertical tiles as allowed by standard. + When enableCustomTileConfig == 1, numTileColumns must be > 0 and <= NV_MAX_TILE_COLS_AV1 and tileWidths must point to a valid array of numTileColumns entries. + Entry i specifies the width in 64x64 CTU unit of tile colum i. The sum of all the entries should be equal to the picture width in 64x64 CTU units. */ + uint32_t + numTileRows; /**< [in]: This parameter in conjunction with the flag enableCustomTileConfig and the array tileHeights[] specifies the way in which the picture is divided into tiles rows + When enableCustomTileConfig == 0, the picture will be uniformly divided into numTileRows tile rows. If numTileRows is not a power of 2, + it will be rounded down to the next power of 2 value. If numTileRows == 0, the picture will be coded with the smallest number of horizontal tiles as allowed by standard. + When enableCustomTileConfig == 1, numTileRows must be > 0 and <= NV_MAX_TILE_ROWS_AV1 and tileHeights must point to a valid array of numTileRows entries. + Entry i specifies the height in 64x64 CTU unit of tile row i. The sum of all the entries should be equal to the picture hieght in 64x64 CTU units. */ + uint32_t* + tileWidths; /**< [in]: If enableCustomTileConfig == 1, tileWidths[i] specifies the width of tile column i in 64x64 CTU unit, with 0 <= i <= numTileColumns -1. */ + uint32_t* + tileHeights; /**< [in]: If enableCustomTileConfig == 1, tileHeights[i] specifies the height of tile row i in 64x64 CTU unit, with 0 <= i <= numTileRows -1. */ + uint32_t + obuPayloadArrayCnt; /**< [in]: Specifies the number of elements allocated in obuPayloadArray array. */ + uint32_t reserved; /**< [in]: Reserved and must be set to 0. */ + NV_ENC_AV1_OBU_PAYLOAD* + obuPayloadArray; /**< [in]: Array of OBU payloads which will be inserted for this frame. */ + NV_ENC_FILM_GRAIN_PARAMS_AV1* + filmGrainParams; /**< [in]: If filmGrainParamsUpdate == 1, filmGrainParams must point to a valid NV_ENC_FILM_GRAIN_PARAMS_AV1 structure */ + uint32_t reserved2[247]; /**< [in]: Reserved and must be set to 0. */ + void* reserved3[61]; /**< [in]: Reserved and must be set to NULL. */ +} NV_ENC_PIC_PARAMS_AV1; + +/** + * Codec specific per-picture encoding parameters. + */ +typedef union _NV_ENC_CODEC_PIC_PARAMS { + NV_ENC_PIC_PARAMS_H264 + h264PicParams; /**< [in]: H264 encode picture params. */ + NV_ENC_PIC_PARAMS_HEVC + hevcPicParams; /**< [in]: HEVC encode picture params. */ + NV_ENC_PIC_PARAMS_AV1 av1PicParams; /**< [in]: AV1 encode picture params. */ + uint32_t reserved[256]; /**< [in]: Reserved and must be set to 0. */ +} NV_ENC_CODEC_PIC_PARAMS; + +/** + * \struct _NV_ENC_PIC_PARAMS + * Encoding parameters that need to be sent on a per frame basis. + */ +typedef struct _NV_ENC_PIC_PARAMS { + uint32_t + version; /**< [in]: Struct version. Must be set to ::NV_ENC_PIC_PARAMS_VER. */ + uint32_t inputWidth; /**< [in]: Specifies the input frame width */ + uint32_t inputHeight; /**< [in]: Specifies the input frame height */ + uint32_t + inputPitch; /**< [in]: Specifies the input buffer pitch. If pitch value is not known, set this to inputWidth. */ + uint32_t + encodePicFlags; /**< [in]: Specifies bit-wise OR of encode picture flags. See ::NV_ENC_PIC_FLAGS enum. */ + uint32_t + frameIdx; /**< [in]: Specifies the frame index associated with the input frame [optional]. */ + uint64_t + inputTimeStamp; /**< [in]: Specifies opaque data which is associated with the encoded frame, but not actually encoded in the output bitstream. + This opaque data can be used later to uniquely refer to the corresponding encoded frame. For example, it can be used + for identifying the frame to be invalidated in the reference picture buffer, if lost at the client. */ + uint64_t inputDuration; /**< [in]: Specifies duration of the input picture */ + NV_ENC_INPUT_PTR + inputBuffer; /**< [in]: Specifies the input buffer pointer. Client must use a pointer obtained from ::NvEncCreateInputBuffer() or ::NvEncMapInputResource() APIs.*/ + NV_ENC_OUTPUT_PTR + outputBitstream; /**< [in]: Specifies the output buffer pointer. + If NV_ENC_INITIALIZE_PARAMS::enableOutputInVidmem is set to 0, specifies the pointer to output buffer. Client should use a pointer obtained from ::NvEncCreateBitstreamBuffer() API. + If NV_ENC_INITIALIZE_PARAMS::enableOutputInVidmem is set to 1, client should allocate buffer in video memory for NV_ENC_ENCODE_OUT_PARAMS struct and encoded bitstream data. Client + should use a pointer obtained from ::NvEncMapInputResource() API, when mapping this output buffer and assign it to NV_ENC_PIC_PARAMS::outputBitstream. + First 256 bytes of this buffer should be interpreted as NV_ENC_ENCODE_OUT_PARAMS struct followed by encoded bitstream data. Recommended size for output buffer is sum of size of + NV_ENC_ENCODE_OUT_PARAMS struct and twice the input frame size for lower resolution eg. CIF and 1.5 times the input frame size for higher resolutions. If encoded bitstream size is + greater than the allocated buffer size for encoded bitstream, then the output buffer will have encoded bitstream data equal to buffer size. All CUDA operations on this buffer must use + the default stream. */ + void* + completionEvent; /**< [in]: Specifies an event to be signaled on completion of encoding of this Frame [only if operating in Asynchronous mode]. Each output buffer should be associated with a distinct event pointer. */ + NV_ENC_BUFFER_FORMAT + bufferFmt; /**< [in]: Specifies the input buffer format. */ + NV_ENC_PIC_STRUCT + pictureStruct; /**< [in]: Specifies structure of the input picture. */ + NV_ENC_PIC_TYPE + pictureType; /**< [in]: Specifies input picture type. Client required to be set explicitly by the client if the client has not set NV_ENC_INITALIZE_PARAMS::enablePTD to 1 while calling NvInitializeEncoder. */ + NV_ENC_CODEC_PIC_PARAMS + codecPicParams; /**< [in]: Specifies the codec specific per-picture encoding parameters. */ + NVENC_EXTERNAL_ME_HINT_COUNTS_PER_BLOCKTYPE meHintCountsPerBlock + [2]; /**< [in]: For H264 and Hevc, specifies the number of hint candidates per block per direction for the current frame. meHintCountsPerBlock[0] is for L0 predictors and meHintCountsPerBlock[1] is for L1 predictors. + The candidate count in NV_ENC_PIC_PARAMS::meHintCountsPerBlock[lx] must never exceed NV_ENC_INITIALIZE_PARAMS::maxMEHintCountsPerBlock[lx] provided during encoder initialization. */ + NVENC_EXTERNAL_ME_HINT* + meExternalHints; /**< [in]: For H264 and Hevc, Specifies the pointer to ME external hints for the current frame. The size of ME hint buffer should be equal to number of macroblocks * the total number of candidates per macroblock. + The total number of candidates per MB per direction = 1*meHintCountsPerBlock[Lx].numCandsPerBlk16x16 + 2*meHintCountsPerBlock[Lx].numCandsPerBlk16x8 + 2*meHintCountsPerBlock[Lx].numCandsPerBlk8x8 + + 4*meHintCountsPerBlock[Lx].numCandsPerBlk8x8. For frames using bidirectional ME , the total number of candidates for single macroblock is sum of total number of candidates per MB for each direction (L0 and L1) */ + uint32_t reserved1[6]; /**< [in]: Reserved and must be set to 0 */ + void* reserved2[2]; /**< [in]: Reserved and must be set to NULL */ + int8_t* + qpDeltaMap; /**< [in]: Specifies the pointer to signed byte array containing value per MB for H264, per CTB for HEVC and per SB for AV1 in raster scan order for the current picture, which will be interpreted depending on NV_ENC_RC_PARAMS::qpMapMode. + If NV_ENC_RC_PARAMS::qpMapMode is NV_ENC_QP_MAP_DELTA, qpDeltaMap specifies QP modifier per MB for H264, per CTB for HEVC and per SB for AV1. This QP modifier will be applied on top of the QP chosen by rate control. + If NV_ENC_RC_PARAMS::qpMapMode is NV_ENC_QP_MAP_EMPHASIS, qpDeltaMap specifies Emphasis Level Map per MB for H264. This level value along with QP chosen by rate control is used to + compute the QP modifier, which in turn is applied on top of QP chosen by rate control. + If NV_ENC_RC_PARAMS::qpMapMode is NV_ENC_QP_MAP_DISABLED, value in qpDeltaMap will be ignored.*/ + uint32_t + qpDeltaMapSize; /**< [in]: Specifies the size in bytes of qpDeltaMap surface allocated by client and pointed to by NV_ENC_PIC_PARAMS::qpDeltaMap. Surface (array) should be picWidthInMbs * picHeightInMbs for H264, picWidthInCtbs * picHeightInCtbs for HEVC and + picWidthInSbs * picHeightInSbs for AV1 */ + uint32_t + reservedBitFields; /**< [in]: Reserved bitfields and must be set to 0 */ + uint16_t meHintRefPicDist + [2]; /**< [in]: Specifies temporal distance for reference picture (NVENC_EXTERNAL_ME_HINT::refidx = 0) used during external ME with NV_ENC_INITALIZE_PARAMS::enablePTD = 1 . meHintRefPicDist[0] is for L0 hints and meHintRefPicDist[1] is for L1 hints. + If not set, will internally infer distance of 1. Ignored for NV_ENC_INITALIZE_PARAMS::enablePTD = 0 */ + NV_ENC_INPUT_PTR + alphaBuffer; /**< [in]: Specifies the input alpha buffer pointer. Client must use a pointer obtained from ::NvEncCreateInputBuffer() or ::NvEncMapInputResource() APIs. + Applicable only when encoding hevc with alpha layer is enabled. */ + NVENC_EXTERNAL_ME_SB_HINT* + meExternalSbHints; /**< [in]: For AV1,Specifies the pointer to ME external SB hints for the current frame. The size of ME hint buffer should be equal to meSbHintsCount. */ + uint32_t + meSbHintsCount; /**< [in]: For AV1, specifies the total number of external ME SB hint candidates for the frame + NV_ENC_PIC_PARAMS::meSbHintsCount must never exceed the total number of SBs in frame * the max number of candidates per SB provided during encoder initialization. + The max number of candidates per SB is maxMeHintCountsPerBlock[0].numCandsPerSb + maxMeHintCountsPerBlock[1].numCandsPerSb */ + uint32_t reserved3[285]; /**< [in]: Reserved and must be set to 0 */ + void* reserved4[58]; /**< [in]: Reserved and must be set to NULL */ +} NV_ENC_PIC_PARAMS; + +/** Macro for constructing the version field of ::_NV_ENC_PIC_PARAMS */ +#define NV_ENC_PIC_PARAMS_VER (NVENCAPI_STRUCT_VERSION(6) | (1 << 31)) + +/** + * \struct _NV_ENC_MEONLY_PARAMS + * MEOnly parameters that need to be sent on a per motion estimation basis. + * NV_ENC_MEONLY_PARAMS::meExternalHints is supported for H264 only. + */ +typedef struct _NV_ENC_MEONLY_PARAMS { + uint32_t + version; /**< [in]: Struct version. Must be set to NV_ENC_MEONLY_PARAMS_VER.*/ + uint32_t inputWidth; /**< [in]: Specifies the input frame width */ + uint32_t inputHeight; /**< [in]: Specifies the input frame height */ + NV_ENC_INPUT_PTR + inputBuffer; /**< [in]: Specifies the input buffer pointer. Client must use a pointer obtained from NvEncCreateInputBuffer() or NvEncMapInputResource() APIs. */ + NV_ENC_INPUT_PTR + referenceFrame; /**< [in]: Specifies the reference frame pointer */ + NV_ENC_OUTPUT_PTR mvBuffer; /**< [in]: Specifies the output buffer pointer. + If NV_ENC_INITIALIZE_PARAMS::enableOutputInVidmem is set to 0, specifies the pointer to motion vector data buffer allocated by NvEncCreateMVBuffer. + Client must lock mvBuffer using ::NvEncLockBitstream() API to get the motion vector data. + If NV_ENC_INITIALIZE_PARAMS::enableOutputInVidmem is set to 1, client should allocate buffer in video memory for storing the motion vector data. The size of this buffer must + be equal to total number of macroblocks multiplied by size of NV_ENC_H264_MV_DATA struct. Client should use a pointer obtained from ::NvEncMapInputResource() API, when mapping this + output buffer and assign it to NV_ENC_MEONLY_PARAMS::mvBuffer. All CUDA operations on this buffer must use the default stream. */ + NV_ENC_BUFFER_FORMAT + bufferFmt; /**< [in]: Specifies the input buffer format. */ + void* + completionEvent; /**< [in]: Specifies an event to be signaled on completion of motion estimation + of this Frame [only if operating in Asynchronous mode]. + Each output buffer should be associated with a distinct event pointer. */ + uint32_t + viewID; /**< [in]: Specifies left or right viewID if NV_ENC_CONFIG_H264_MEONLY::bStereoEnable is set. + viewID can be 0,1 if bStereoEnable is set, 0 otherwise. */ + NVENC_EXTERNAL_ME_HINT_COUNTS_PER_BLOCKTYPE + meHintCountsPerBlock + [2]; /**< [in]: Specifies the number of hint candidates per block for the current frame. meHintCountsPerBlock[0] is for L0 predictors. + The candidate count in NV_ENC_PIC_PARAMS::meHintCountsPerBlock[lx] must never exceed NV_ENC_INITIALIZE_PARAMS::maxMEHintCountsPerBlock[lx] provided during encoder initialization. */ + NVENC_EXTERNAL_ME_HINT* + meExternalHints; /**< [in]: Specifies the pointer to ME external hints for the current frame. The size of ME hint buffer should be equal to number of macroblocks * the total number of candidates per macroblock. + The total number of candidates per MB per direction = 1*meHintCountsPerBlock[Lx].numCandsPerBlk16x16 + 2*meHintCountsPerBlock[Lx].numCandsPerBlk16x8 + 2*meHintCountsPerBlock[Lx].numCandsPerBlk8x8 + + 4*meHintCountsPerBlock[Lx].numCandsPerBlk8x8. For frames using bidirectional ME , the total number of candidates for single macroblock is sum of total number of candidates per MB for each direction (L0 and L1) */ + uint32_t reserved1[243]; /**< [in]: Reserved and must be set to 0 */ + void* reserved2[59]; /**< [in]: Reserved and must be set to NULL */ +} NV_ENC_MEONLY_PARAMS; + +/** NV_ENC_MEONLY_PARAMS struct version*/ +#define NV_ENC_MEONLY_PARAMS_VER NVENCAPI_STRUCT_VERSION(3) + +/** + * \struct _NV_ENC_LOCK_BITSTREAM + * Bitstream buffer lock parameters. + */ +typedef struct _NV_ENC_LOCK_BITSTREAM { + uint32_t + version; /**< [in]: Struct version. Must be set to ::NV_ENC_LOCK_BITSTREAM_VER. */ + uint32_t + doNotWait : 1; /**< [in]: If this flag is set, the NvEncodeAPI interface will return buffer pointer even if operation is not completed. If not set, the call will block until operation completes. */ + uint32_t + ltrFrame : 1; /**< [out]: Flag indicating this frame is marked as LTR frame */ + uint32_t + getRCStats : 1; /**< [in]: If this flag is set then lockBitstream call will add additional intra-inter MB count and average MVX, MVY */ + uint32_t + reservedBitFields : 29; /**< [in]: Reserved bit fields and must be set to 0 */ + void* + outputBitstream; /**< [in]: Pointer to the bitstream buffer being locked. */ + uint32_t* + sliceOffsets; /**< [in, out]: Array which receives the slice (H264/HEVC) or tile (AV1) offsets. This is not supported if NV_ENC_CONFIG_H264::sliceMode is 1 on Kepler GPUs. Array size must be equal to size of frame in MBs. */ + uint32_t + frameIdx; /**< [out]: Frame no. for which the bitstream is being retrieved. */ + uint32_t + hwEncodeStatus; /**< [out]: The NvEncodeAPI interface status for the locked picture. */ + uint32_t + numSlices; /**< [out]: Number of slices (H264/HEVC) or tiles (AV1) in the encoded picture. Will be reported only if NV_ENC_INITIALIZE_PARAMS::reportSliceOffsets set to 1. */ + uint32_t + bitstreamSizeInBytes; /**< [out]: Actual number of bytes generated and copied to the memory pointed by bitstreamBufferPtr. + When HEVC alpha layer encoding is enabled, this field reports the total encoded size in bytes i.e it is the encoded size of the base plus the alpha layer. + For AV1 when enablePTD is set, this field reports the total encoded size in bytes of all the encoded frames packed into the current output surface i.e. show frame plus all preceding no-show frames */ + uint64_t + outputTimeStamp; /**< [out]: Presentation timestamp associated with the encoded output. */ + uint64_t + outputDuration; /**< [out]: Presentation duration associates with the encoded output. */ + void* + bitstreamBufferPtr; /**< [out]: Pointer to the generated output bitstream. + For MEOnly mode _NV_ENC_LOCK_BITSTREAM::bitstreamBufferPtr should be typecast to + NV_ENC_H264_MV_DATA/NV_ENC_HEVC_MV_DATA pointer respectively for H264/HEVC */ + NV_ENC_PIC_TYPE + pictureType; /**< [out]: Picture type of the encoded picture. */ + NV_ENC_PIC_STRUCT + pictureStruct; /**< [out]: Structure of the generated output picture. */ + uint32_t frameAvgQP; /**< [out]: Average QP of the frame. */ + uint32_t frameSatd; /**< [out]: Total SATD cost for whole frame. */ + uint32_t + ltrFrameIdx; /**< [out]: Frame index associated with this LTR frame. */ + uint32_t + ltrFrameBitmap; /**< [out]: Bitmap of LTR frames indices which were used for encoding this frame. Value of 0 if no LTR frames were used. */ + uint32_t + temporalId; /**< [out]: TemporalId value of the frame when using temporalSVC encoding */ + uint32_t reserved[12]; /**< [in]: Reserved and must be set to 0 */ + uint32_t + intraMBCount; /**< [out]: For H264, Number of Intra MBs in the encoded frame. For HEVC, Number of Intra CTBs in the encoded frame. For AV1, Number of Intra SBs in the encoded show frame. Supported only if _NV_ENC_LOCK_BITSTREAM::getRCStats set to 1. */ + uint32_t + interMBCount; /**< [out]: For H264, Number of Inter MBs in the encoded frame, includes skip MBs. For HEVC, Number of Inter CTBs in the encoded frame. For AV1, Number of Inter SBs in the encoded show frame. Supported only if _NV_ENC_LOCK_BITSTREAM::getRCStats set to 1. */ + int32_t + averageMVX; /**< [out]: Average Motion Vector in X direction for the encoded frame. Supported only if _NV_ENC_LOCK_BITSTREAM::getRCStats set to 1. */ + int32_t + averageMVY; /**< [out]: Average Motion Vector in y direction for the encoded frame. Supported only if _NV_ENC_LOCK_BITSTREAM::getRCStats set to 1. */ + uint32_t + alphaLayerSizeInBytes; /**< [out]: Number of bytes generated for the alpha layer in the encoded output. Applicable only when HEVC with alpha encoding is enabled. */ + + uint32_t reserved1[218]; /**< [in]: Reserved and must be set to 0 */ + void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ +} NV_ENC_LOCK_BITSTREAM; + +/** Macro for constructing the version field of ::_NV_ENC_LOCK_BITSTREAM */ +#define NV_ENC_LOCK_BITSTREAM_VER NVENCAPI_STRUCT_VERSION(2) + +/** + * \struct _NV_ENC_LOCK_INPUT_BUFFER + * Uncompressed Input Buffer lock parameters. + */ +typedef struct _NV_ENC_LOCK_INPUT_BUFFER { + uint32_t + version; /**< [in]: Struct version. Must be set to ::NV_ENC_LOCK_INPUT_BUFFER_VER. */ + uint32_t + doNotWait : 1; /**< [in]: Set to 1 to make ::NvEncLockInputBuffer() a unblocking call. If the encoding is not completed, driver will return ::NV_ENC_ERR_ENCODER_BUSY error code. */ + uint32_t + reservedBitFields : 31; /**< [in]: Reserved bitfields and must be set to 0 */ + NV_ENC_INPUT_PTR + inputBuffer; /**< [in]: Pointer to the input buffer to be locked, client should pass the pointer obtained from ::NvEncCreateInputBuffer() or ::NvEncMapInputResource API. */ + void* + bufferDataPtr; /**< [out]: Pointed to the locked input buffer data. Client can only access input buffer using the \p bufferDataPtr. */ + uint32_t pitch; /**< [out]: Pitch of the locked input buffer. */ + uint32_t reserved1[251]; /**< [in]: Reserved and must be set to 0 */ + void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ +} NV_ENC_LOCK_INPUT_BUFFER; + +/** Macro for constructing the version field of ::_NV_ENC_LOCK_INPUT_BUFFER */ +#define NV_ENC_LOCK_INPUT_BUFFER_VER NVENCAPI_STRUCT_VERSION(1) + +/** + * \struct _NV_ENC_MAP_INPUT_RESOURCE + * Map an input resource to a Nvidia Encoder Input Buffer + */ +typedef struct _NV_ENC_MAP_INPUT_RESOURCE { + uint32_t + version; /**< [in]: Struct version. Must be set to ::NV_ENC_MAP_INPUT_RESOURCE_VER. */ + uint32_t subResourceIndex; /**< [in]: Deprecated. Do not use. */ + void* inputResource; /**< [in]: Deprecated. Do not use. */ + NV_ENC_REGISTERED_PTR + registeredResource; /**< [in]: The Registered resource handle obtained by calling NvEncRegisterInputResource. */ + NV_ENC_INPUT_PTR + mappedResource; /**< [out]: Mapped pointer corresponding to the registeredResource. This pointer must be used in NV_ENC_PIC_PARAMS::inputBuffer parameter in ::NvEncEncodePicture() API. */ + NV_ENC_BUFFER_FORMAT + mappedBufferFmt; /**< [out]: Buffer format of the outputResource. This buffer format must be used in NV_ENC_PIC_PARAMS::bufferFmt if client using the above mapped resource pointer. */ + uint32_t reserved1[251]; /**< [in]: Reserved and must be set to 0. */ + void* reserved2[63]; /**< [in]: Reserved and must be set to NULL */ +} NV_ENC_MAP_INPUT_RESOURCE; + +/** Macro for constructing the version field of ::_NV_ENC_MAP_INPUT_RESOURCE */ +#define NV_ENC_MAP_INPUT_RESOURCE_VER NVENCAPI_STRUCT_VERSION(4) + +/** + * \struct _NV_ENC_INPUT_RESOURCE_OPENGL_TEX + * NV_ENC_REGISTER_RESOURCE::resourceToRegister must be a pointer to a variable of this type, + * when NV_ENC_REGISTER_RESOURCE::resourceType is NV_ENC_INPUT_RESOURCE_TYPE_OPENGL_TEX + */ +typedef struct _NV_ENC_INPUT_RESOURCE_OPENGL_TEX { + uint32_t texture; /**< [in]: The name of the texture to be used. */ + uint32_t + target; /**< [in]: Accepted values are GL_TEXTURE_RECTANGLE and GL_TEXTURE_2D. */ +} NV_ENC_INPUT_RESOURCE_OPENGL_TEX; + +/** \struct NV_ENC_FENCE_POINT_D3D12 +* Fence and fence value for synchronization. +*/ +typedef struct _NV_ENC_FENCE_POINT_D3D12 { + uint32_t + version; /**< [in]: Struct version. Must be set to ::NV_ENC_FENCE_POINT_D3D12_VER. */ + uint32_t reserved; /**< [in]: Reserved and must be set to 0. */ + void* + pFence; /**< [in]: Pointer to ID3D12Fence. This fence object is used for synchronization. */ + uint64_t + waitValue; /**< [in]: Fence value to reach or exceed before the GPU operation. */ + uint64_t + signalValue; /**< [in]: Fence value to set the fence to, after the GPU operation. */ + uint32_t + bWait : 1; /**< [in]: Wait on 'waitValue' if bWait is set to 1, before starting GPU operation. */ + uint32_t + bSignal : 1; /**< [in]: Signal on 'signalValue' if bSignal is set to 1, after GPU operation is complete. */ + uint32_t reservedBitField : 30; /**< [in]: Reserved and must be set to 0. */ + uint32_t reserved1[7]; /**< [in]: Reserved and must be set to 0. */ +} NV_ENC_FENCE_POINT_D3D12; + +#define NV_ENC_FENCE_POINT_D3D12_VER NVENCAPI_STRUCT_VERSION(1) + +/** + * \struct _NV_ENC_INPUT_RESOURCE_D3D12 + * NV_ENC_PIC_PARAMS::inputBuffer and NV_ENC_PIC_PARAMS::alphaBuffer must be a pointer to a struct of this type, + * when D3D12 interface is used + */ +typedef struct _NV_ENC_INPUT_RESOURCE_D3D12 { + uint32_t + version; /**< [in]: Struct version. Must be set to ::NV_ENC_INPUT_RESOURCE_D3D12_VER. */ + uint32_t reserved; /**< [in]: Reserved and must be set to 0. */ + NV_ENC_INPUT_PTR + pInputBuffer; /**< [in]: Specifies the input surface pointer. Client must use a pointer obtained from NvEncMapInputResource() in NV_ENC_MAP_INPUT_RESOURCE::mappedResource + when mapping the input surface. */ + NV_ENC_FENCE_POINT_D3D12 + inputFencePoint; /**< [in]: Specifies the fence and corresponding fence values to do GPU wait and signal. */ + uint32_t reserved1[16]; /**< [in]: Reserved and must be set to 0. */ + void* reserved2[16]; /**< [in]: Reserved and must be set to NULL. */ +} NV_ENC_INPUT_RESOURCE_D3D12; + +#define NV_ENC_INPUT_RESOURCE_D3D12_VER NVENCAPI_STRUCT_VERSION(1) + +/** + * \struct _NV_ENC_OUTPUT_RESOURCE_D3D12 + * NV_ENC_PIC_PARAMS::outputBitstream and NV_ENC_LOCK_BITSTREAM::outputBitstream must be a pointer to a struct of this type, + * when D3D12 interface is used + */ +typedef struct _NV_ENC_OUTPUT_RESOURCE_D3D12 { + uint32_t + version; /**< [in]: Struct version. Must be set to ::NV_ENC_OUTPUT_RESOURCE_D3D12_VER. */ + uint32_t reserved; /**< [in]: Reserved and must be set to 0. */ + NV_ENC_INPUT_PTR + pOutputBuffer; /**< [in]: Specifies the output buffer pointer. Client must use a pointer obtained from NvEncMapInputResource() in NV_ENC_MAP_INPUT_RESOURCE::mappedResource + when mapping output bitstream buffer */ + NV_ENC_FENCE_POINT_D3D12 + outputFencePoint; /**< [in]: Specifies the fence and corresponding fence values to do GPU wait and signal.*/ + uint32_t reserved1[16]; /**< [in]: Reserved and must be set to 0. */ + void* reserved2[16]; /**< [in]: Reserved and must be set to NULL. */ +} NV_ENC_OUTPUT_RESOURCE_D3D12; + +#define NV_ENC_OUTPUT_RESOURCE_D3D12_VER NVENCAPI_STRUCT_VERSION(1) + +/** + * \struct _NV_ENC_REGISTER_RESOURCE + * Register a resource for future use with the Nvidia Video Encoder Interface. + */ +typedef struct _NV_ENC_REGISTER_RESOURCE { + uint32_t + version; /**< [in]: Struct version. Must be set to ::NV_ENC_REGISTER_RESOURCE_VER. */ + NV_ENC_INPUT_RESOURCE_TYPE + resourceType; /**< [in]: Specifies the type of resource to be registered. + Supported values are + ::NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX, + ::NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR, + ::NV_ENC_INPUT_RESOURCE_TYPE_OPENGL_TEX */ + uint32_t width; /**< [in]: Input frame width. */ + uint32_t height; /**< [in]: Input frame height. */ + uint32_t pitch; /**< [in]: Input buffer pitch. + For ::NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX resources, set this to 0. + For ::NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR resources, set this to + the pitch as obtained from cuMemAllocPitch(), or to the width in + bytes (if this resource was created by using cuMemAlloc()). This + value must be a multiple of 4. + For ::NV_ENC_INPUT_RESOURCE_TYPE_CUDAARRAY resources, set this to the + width of the allocation in bytes (i.e. + CUDA_ARRAY3D_DESCRIPTOR::Width * CUDA_ARRAY3D_DESCRIPTOR::NumChannels). + For ::NV_ENC_INPUT_RESOURCE_TYPE_OPENGL_TEX resources, set this to the + texture width multiplied by the number of components in the texture + format. */ + uint32_t + subResourceIndex; /**< [in]: Subresource Index of the DirectX resource to be registered. Should be set to 0 for other interfaces. */ + void* + resourceToRegister; /**< [in]: Handle to the resource that is being registered. */ + NV_ENC_REGISTERED_PTR + registeredResource; /**< [out]: Registered resource handle. This should be used in future interactions with the Nvidia Video Encoder Interface. */ + NV_ENC_BUFFER_FORMAT + bufferFormat; /**< [in]: Buffer format of resource to be registered. */ + NV_ENC_BUFFER_USAGE + bufferUsage; /**< [in]: Usage of resource to be registered. */ + NV_ENC_FENCE_POINT_D3D12* + pInputFencePoint; /**< [in]: Specifies the input fence and corresponding fence values to do GPU wait and signal. + To be used only when NV_ENC_REGISTER_RESOURCE::resourceToRegister represents D3D12 surface and + NV_ENC_BUFFER_USAGE::bufferUsage is NV_ENC_INPUT_IMAGE. + The fence NV_ENC_FENCE_POINT_D3D12::pFence and NV_ENC_FENCE_POINT_D3D12::waitValue will be used to do GPU wait + before starting GPU operation, if NV_ENC_FENCE_POINT_D3D12::bWait is set. + The fence NV_ENC_FENCE_POINT_D3D12::pFence and NV_ENC_FENCE_POINT_D3D12::signalValue will be used to do GPU signal + when GPU operation finishes, if NV_ENC_FENCE_POINT_D3D12::bSignal is set. */ + uint32_t reserved1[247]; /**< [in]: Reserved and must be set to 0. */ + void* reserved2[61]; /**< [in]: Reserved and must be set to NULL. */ +} NV_ENC_REGISTER_RESOURCE; + +/** Macro for constructing the version field of ::_NV_ENC_REGISTER_RESOURCE */ +#define NV_ENC_REGISTER_RESOURCE_VER NVENCAPI_STRUCT_VERSION(4) + +/** + * \struct _NV_ENC_STAT + * Encode Stats structure. + */ +typedef struct _NV_ENC_STAT { + uint32_t + version; /**< [in]: Struct version. Must be set to ::NV_ENC_STAT_VER. */ + uint32_t reserved; /**< [in]: Reserved and must be set to 0 */ + NV_ENC_OUTPUT_PTR + outputBitStream; /**< [out]: Specifies the pointer to output bitstream. */ + uint32_t bitStreamSize; /**< [out]: Size of generated bitstream in bytes. */ + uint32_t + picType; /**< [out]: Picture type of encoded picture. See ::NV_ENC_PIC_TYPE. */ + uint32_t + lastValidByteOffset; /**< [out]: Offset of last valid bytes of completed bitstream */ + uint32_t sliceOffsets[16]; /**< [out]: Offsets of each slice */ + uint32_t picIdx; /**< [out]: Picture number */ + uint32_t frameAvgQP; /**< [out]: Average QP of the frame. */ + uint32_t + ltrFrame : 1; /**< [out]: Flag indicating this frame is marked as LTR frame */ + uint32_t + reservedBitFields : 31; /**< [in]: Reserved bit fields and must be set to 0 */ + uint32_t + ltrFrameIdx; /**< [out]: Frame index associated with this LTR frame. */ + uint32_t + intraMBCount; /**< [out]: For H264, Number of Intra MBs in the encoded frame. For HEVC, Number of Intra CTBs in the encoded frame. */ + uint32_t + interMBCount; /**< [out]: For H264, Number of Inter MBs in the encoded frame, includes skip MBs. For HEVC, Number of Inter CTBs in the encoded frame. */ + int32_t + averageMVX; /**< [out]: Average Motion Vector in X direction for the encoded frame. */ + int32_t + averageMVY; /**< [out]: Average Motion Vector in y direction for the encoded frame. */ + uint32_t reserved1[226]; /**< [in]: Reserved and must be set to 0 */ + void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ +} NV_ENC_STAT; + +/** Macro for constructing the version field of ::_NV_ENC_STAT */ +#define NV_ENC_STAT_VER NVENCAPI_STRUCT_VERSION(1) + +/** + * \struct _NV_ENC_SEQUENCE_PARAM_PAYLOAD + * Sequence and picture paramaters payload. + */ +typedef struct _NV_ENC_SEQUENCE_PARAM_PAYLOAD { + uint32_t + version; /**< [in]: Struct version. Must be set to ::NV_ENC_INITIALIZE_PARAMS_VER. */ + uint32_t + inBufferSize; /**< [in]: Specifies the size of the spsppsBuffer provided by the client */ + uint32_t + spsId; /**< [in]: Specifies the SPS id to be used in sequence header. Default value is 0. */ + uint32_t + ppsId; /**< [in]: Specifies the PPS id to be used in picture header. Default value is 0. */ + void* + spsppsBuffer; /**< [in]: Specifies bitstream header pointer of size NV_ENC_SEQUENCE_PARAM_PAYLOAD::inBufferSize. + It is the client's responsibility to manage this memory. */ + uint32_t* + outSPSPPSPayloadSize; /**< [out]: Size of the sequence and picture header in bytes. */ + uint32_t reserved[250]; /**< [in]: Reserved and must be set to 0 */ + void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ +} NV_ENC_SEQUENCE_PARAM_PAYLOAD; + +/** Macro for constructing the version field of ::_NV_ENC_SEQUENCE_PARAM_PAYLOAD */ +#define NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER NVENCAPI_STRUCT_VERSION(1) + +/** + * Event registration/unregistration parameters. + */ +typedef struct _NV_ENC_EVENT_PARAMS { + uint32_t + version; /**< [in]: Struct version. Must be set to ::NV_ENC_EVENT_PARAMS_VER. */ + uint32_t reserved; /**< [in]: Reserved and must be set to 0 */ + void* + completionEvent; /**< [in]: Handle to event to be registered/unregistered with the NvEncodeAPI interface. */ + uint32_t reserved1[253]; /**< [in]: Reserved and must be set to 0 */ + void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ +} NV_ENC_EVENT_PARAMS; + +/** Macro for constructing the version field of ::_NV_ENC_EVENT_PARAMS */ +#define NV_ENC_EVENT_PARAMS_VER NVENCAPI_STRUCT_VERSION(1) + +/** + * Encoder Session Creation parameters + */ +typedef struct _NV_ENC_OPEN_ENCODE_SESSIONEX_PARAMS { + uint32_t + version; /**< [in]: Struct version. Must be set to ::NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER. */ + NV_ENC_DEVICE_TYPE deviceType; /**< [in]: Specified the device Type */ + void* device; /**< [in]: Pointer to client device. */ + void* reserved; /**< [in]: Reserved and must be set to 0. */ + uint32_t + apiVersion; /**< [in]: API version. Should be set to NVENCAPI_VERSION. */ + uint32_t reserved1[253]; /**< [in]: Reserved and must be set to 0 */ + void* reserved2[64]; /**< [in]: Reserved and must be set to NULL */ +} NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS; +/** Macro for constructing the version field of ::_NV_ENC_OPEN_ENCODE_SESSIONEX_PARAMS */ +#define NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER NVENCAPI_STRUCT_VERSION(1) + +/** @} */ /* END ENCODER_STRUCTURE */ + +/** + * \addtogroup ENCODE_FUNC NvEncodeAPI Functions + * @{ + */ + +// NvEncOpenEncodeSession +/** + * \brief Opens an encoding session. + * + * Deprecated. + * + * \return + * ::NV_ENC_ERR_INVALID_CALL\n + * + */ +NVENCSTATUS NVENCAPI NvEncOpenEncodeSession(void* device, + uint32_t deviceType, + void** encoder); + +// NvEncGetEncodeGuidCount +/** + * \brief Retrieves the number of supported encode GUIDs. + * + * The function returns the number of codec GUIDs supported by the NvEncodeAPI + * interface. + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * \param [out] encodeGUIDCount + * Number of supported encode GUIDs. + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_GENERIC \n + * + */ +NVENCSTATUS NVENCAPI NvEncGetEncodeGUIDCount(void* encoder, + uint32_t* encodeGUIDCount); + +// NvEncGetEncodeGUIDs +/** + * \brief Retrieves an array of supported encoder codec GUIDs. + * + * The function returns an array of codec GUIDs supported by the NvEncodeAPI interface. + * The client must allocate an array where the NvEncodeAPI interface can + * fill the supported GUIDs and pass the pointer in \p *GUIDs parameter. + * The size of the array can be determined by using ::NvEncGetEncodeGUIDCount() API. + * The Nvidia Encoding interface returns the number of codec GUIDs it has actually + * filled in the GUID array in the \p GUIDCount parameter. + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * \param [in] guidArraySize + * Number of GUIDs to retrieved. Should be set to the number retrieved using + * ::NvEncGetEncodeGUIDCount. + * \param [out] GUIDs + * Array of supported Encode GUIDs. + * \param [out] GUIDCount + * Number of supported Encode GUIDs. + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_GENERIC \n + * + */ +NVENCSTATUS NVENCAPI NvEncGetEncodeGUIDs(void* encoder, + GUID* GUIDs, + uint32_t guidArraySize, + uint32_t* GUIDCount); + +// NvEncGetEncodeProfileGuidCount +/** + * \brief Retrieves the number of supported profile GUIDs. + * + * The function returns the number of profile GUIDs supported for a given codec. + * The client must first enumerate the codec GUIDs supported by the NvEncodeAPI + * interface. After determining the codec GUID, it can query the NvEncodeAPI + * interface to determine the number of profile GUIDs supported for a particular + * codec GUID. + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * \param [in] encodeGUID + * The codec GUID for which the profile GUIDs are being enumerated. + * \param [out] encodeProfileGUIDCount + * Number of encode profiles supported for the given encodeGUID. + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_GENERIC \n + * + */ +NVENCSTATUS NVENCAPI +NvEncGetEncodeProfileGUIDCount(void* encoder, + GUID encodeGUID, + uint32_t* encodeProfileGUIDCount); + +// NvEncGetEncodeProfileGUIDs +/** + * \brief Retrieves an array of supported encode profile GUIDs. + * + * The function returns an array of supported profile GUIDs for a particular + * codec GUID. The client must allocate an array where the NvEncodeAPI interface + * can populate the profile GUIDs. The client can determine the array size using + * ::NvEncGetEncodeProfileGUIDCount() API. The client must also validiate that the + * NvEncodeAPI interface supports the GUID the client wants to pass as \p encodeGUID + * parameter. + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * \param [in] encodeGUID + * The encode GUID whose profile GUIDs are being enumerated. + * \param [in] guidArraySize + * Number of GUIDs to be retrieved. Should be set to the number retrieved using + * ::NvEncGetEncodeProfileGUIDCount. + * \param [out] profileGUIDs + * Array of supported Encode Profile GUIDs + * \param [out] GUIDCount + * Number of valid encode profile GUIDs in \p profileGUIDs array. + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_GENERIC \n + * + */ +NVENCSTATUS NVENCAPI NvEncGetEncodeProfileGUIDs(void* encoder, + GUID encodeGUID, + GUID* profileGUIDs, + uint32_t guidArraySize, + uint32_t* GUIDCount); + +// NvEncGetInputFormatCount +/** + * \brief Retrieve the number of supported Input formats. + * + * The function returns the number of supported input formats. The client must + * query the NvEncodeAPI interface to determine the supported input formats + * before creating the input surfaces. + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * \param [in] encodeGUID + * Encode GUID, corresponding to which the number of supported input formats + * is to be retrieved. + * \param [out] inputFmtCount + * Number of input formats supported for specified Encode GUID. + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_GENERIC \n + */ +NVENCSTATUS NVENCAPI NvEncGetInputFormatCount(void* encoder, + GUID encodeGUID, + uint32_t* inputFmtCount); + +// NvEncGetInputFormats +/** + * \brief Retrieves an array of supported Input formats + * + * Returns an array of supported input formats The client must use the input + * format to create input surface using ::NvEncCreateInputBuffer() API. + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * \param [in] encodeGUID + * Encode GUID, corresponding to which the number of supported input formats + * is to be retrieved. + *\param [in] inputFmtArraySize + * Size input format count array passed in \p inputFmts. + *\param [out] inputFmts + * Array of input formats supported for this Encode GUID. + *\param [out] inputFmtCount + * The number of valid input format types returned by the NvEncodeAPI + * interface in \p inputFmts array. + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_GENERIC \n + * + */ +NVENCSTATUS NVENCAPI NvEncGetInputFormats(void* encoder, + GUID encodeGUID, + NV_ENC_BUFFER_FORMAT* inputFmts, + uint32_t inputFmtArraySize, + uint32_t* inputFmtCount); + +// NvEncGetEncodeCaps +/** + * \brief Retrieves the capability value for a specified encoder attribute. + * + * The function returns the capability value for a given encoder attribute. The + * client must validate the encodeGUID using ::NvEncGetEncodeGUIDs() API before + * calling this function. The encoder attribute being queried are enumerated in + * ::NV_ENC_CAPS_PARAM enum. + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * \param [in] encodeGUID + * Encode GUID, corresponding to which the capability attribute is to be retrieved. + * \param [in] capsParam + * Used to specify attribute being queried. Refer ::NV_ENC_CAPS_PARAM for more + * details. + * \param [out] capsVal + * The value corresponding to the capability attribute being queried. + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_GENERIC \n + */ +NVENCSTATUS NVENCAPI NvEncGetEncodeCaps(void* encoder, + GUID encodeGUID, + NV_ENC_CAPS_PARAM* capsParam, + int* capsVal); + +// NvEncGetEncodePresetCount +/** + * \brief Retrieves the number of supported preset GUIDs. + * + * The function returns the number of preset GUIDs available for a given codec. + * The client must validate the codec GUID using ::NvEncGetEncodeGUIDs() API + * before calling this function. + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * \param [in] encodeGUID + * Encode GUID, corresponding to which the number of supported presets is to + * be retrieved. + * \param [out] encodePresetGUIDCount + * Receives the number of supported preset GUIDs. + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_GENERIC \n + * + */ +NVENCSTATUS NVENCAPI NvEncGetEncodePresetCount(void* encoder, + GUID encodeGUID, + uint32_t* encodePresetGUIDCount); + +// NvEncGetEncodePresetGUIDs +/** + * \brief Receives an array of supported encoder preset GUIDs. + * + * The function returns an array of encode preset GUIDs available for a given codec. + * The client can directly use one of the preset GUIDs based upon the use case + * or target device. The preset GUID chosen can be directly used in + * NV_ENC_INITIALIZE_PARAMS::presetGUID parameter to ::NvEncEncodePicture() API. + * Alternately client can also use the preset GUID to retrieve the encoding config + * parameters being used by NvEncodeAPI interface for that given preset, using + * ::NvEncGetEncodePresetConfig() API. It can then modify preset config parameters + * as per its use case and send it to NvEncodeAPI interface as part of + * NV_ENC_INITIALIZE_PARAMS::encodeConfig parameter for NvEncInitializeEncoder() + * API. + * + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * \param [in] encodeGUID + * Encode GUID, corresponding to which the list of supported presets is to be + * retrieved. + * \param [in] guidArraySize + * Size of array of preset GUIDs passed in \p preset GUIDs + * \param [out] presetGUIDs + * Array of supported Encode preset GUIDs from the NvEncodeAPI interface + * to client. + * \param [out] encodePresetGUIDCount + * Receives the number of preset GUIDs returned by the NvEncodeAPI + * interface. + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_GENERIC \n + * + */ +NVENCSTATUS NVENCAPI NvEncGetEncodePresetGUIDs(void* encoder, + GUID encodeGUID, + GUID* presetGUIDs, + uint32_t guidArraySize, + uint32_t* encodePresetGUIDCount); + +// NvEncGetEncodePresetConfig +/** + * \brief Returns a preset config structure supported for given preset GUID. + * + * The function returns a preset config structure for a given preset GUID. + * NvEncGetEncodePresetConfig() API is not applicable to AV1. + * Before using this function the client must enumerate the preset GUIDs available for + * a given codec. The preset config structure can be modified by the client depending + * upon its use case and can be then used to initialize the encoder using + * ::NvEncInitializeEncoder() API. The client can use this function only if it + * wants to modify the NvEncodeAPI preset configuration, otherwise it can + * directly use the preset GUID. + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * \param [in] encodeGUID + * Encode GUID, corresponding to which the list of supported presets is to be + * retrieved. + * \param [in] presetGUID + * Preset GUID, corresponding to which the Encoding configurations is to be + * retrieved. + * \param [out] presetConfig + * The requested Preset Encoder Attribute set. Refer ::_NV_ENC_CONFIG for +* more details. + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_INVALID_VERSION \n + * ::NV_ENC_ERR_GENERIC \n + * + */ +NVENCSTATUS NVENCAPI +NvEncGetEncodePresetConfig(void* encoder, + GUID encodeGUID, + GUID presetGUID, + NV_ENC_PRESET_CONFIG* presetConfig); + +// NvEncGetEncodePresetConfigEx +/** + * \brief Returns a preset config structure supported for given preset GUID. + * + * The function returns a preset config structure for a given preset GUID and tuning info. + * NvEncGetEncodePresetConfigEx() API is not applicable to H264 and HEVC meonly mode. + * Before using this function the client must enumerate the preset GUIDs available for + * a given codec. The preset config structure can be modified by the client depending + * upon its use case and can be then used to initialize the encoder using + * ::NvEncInitializeEncoder() API. The client can use this function only if it + * wants to modify the NvEncodeAPI preset configuration, otherwise it can + * directly use the preset GUID. + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * \param [in] encodeGUID + * Encode GUID, corresponding to which the list of supported presets is to be + * retrieved. + * \param [in] presetGUID + * Preset GUID, corresponding to which the Encoding configurations is to be + * retrieved. + * \param [in] tuningInfo + * tuning info, corresponding to which the Encoding configurations is to be + * retrieved. + * \param [out] presetConfig + * The requested Preset Encoder Attribute set. Refer ::_NV_ENC_CONFIG for + * more details. + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_INVALID_VERSION \n + * ::NV_ENC_ERR_GENERIC \n + * + */ +NVENCSTATUS NVENCAPI +NvEncGetEncodePresetConfigEx(void* encoder, + GUID encodeGUID, + GUID presetGUID, + NV_ENC_TUNING_INFO tuningInfo, + NV_ENC_PRESET_CONFIG* presetConfig); + +// NvEncInitializeEncoder +/** + * \brief Initialize the encoder. + * + * This API must be used to initialize the encoder. The initialization parameter + * is passed using \p *createEncodeParams The client must send the following + * fields of the _NV_ENC_INITIALIZE_PARAMS structure with a valid value. + * - NV_ENC_INITIALIZE_PARAMS::encodeGUID + * - NV_ENC_INITIALIZE_PARAMS::encodeWidth + * - NV_ENC_INITIALIZE_PARAMS::encodeHeight + * + * The client can pass a preset GUID directly to the NvEncodeAPI interface using + * NV_ENC_INITIALIZE_PARAMS::presetGUID field. If the client doesn't pass + * NV_ENC_INITIALIZE_PARAMS::encodeConfig structure, the codec specific parameters + * will be selected based on the preset GUID. The preset GUID must have been + * validated by the client using ::NvEncGetEncodePresetGUIDs() API. + * If the client passes a custom ::_NV_ENC_CONFIG structure through + * NV_ENC_INITIALIZE_PARAMS::encodeConfig , it will override the codec specific parameters + * based on the preset GUID. It is recommended that even if the client passes a custom config, + * it should also send a preset GUID. In this case, the preset GUID passed by the client + * will not override any of the custom config parameters programmed by the client, + * it is only used as a hint by the NvEncodeAPI interface to determine certain encoder parameters + * which are not exposed to the client. + * + * There are two modes of operation for the encoder namely: + * - Asynchronous mode + * - Synchronous mode + * + * The client can select asynchronous or synchronous mode by setting the \p + * enableEncodeAsync field in ::_NV_ENC_INITIALIZE_PARAMS to 1 or 0 respectively. + *\par Asynchronous mode of operation: + * The Asynchronous mode can be enabled by setting NV_ENC_INITIALIZE_PARAMS::enableEncodeAsync to 1. + * The client operating in asynchronous mode must allocate completion event object + * for each output buffer and pass the completion event object in the + * ::NvEncEncodePicture() API. The client can create another thread and wait on + * the event object to be signaled by NvEncodeAPI interface on completion of the + * encoding process for the output frame. This should unblock the main thread from + * submitting work to the encoder. When the event is signaled the client can call + * NvEncodeAPI interfaces to copy the bitstream data using ::NvEncLockBitstream() + * API. This is the preferred mode of operation. + * + * NOTE: Asynchronous mode is not supported on Linux. + * + *\par Synchronous mode of operation: + * The client can select synchronous mode by setting NV_ENC_INITIALIZE_PARAMS::enableEncodeAsync to 0. + * The client working in synchronous mode can work in a single threaded or multi + * threaded mode. The client need not allocate any event objects. The client can + * only lock the bitstream data after NvEncodeAPI interface has returned + * ::NV_ENC_SUCCESS from encode picture. The NvEncodeAPI interface can return + * ::NV_ENC_ERR_NEED_MORE_INPUT error code from ::NvEncEncodePicture() API. The + * client must not lock the output buffer in such case but should send the next + * frame for encoding. The client must keep on calling ::NvEncEncodePicture() API + * until it returns ::NV_ENC_SUCCESS. \n + * The client must always lock the bitstream data in order in which it has submitted. + * This is true for both asynchronous and synchronous mode. + * + *\par Picture type decision: + * If the client is taking the picture type decision and it must disable the picture + * type decision module in NvEncodeAPI by setting NV_ENC_INITIALIZE_PARAMS::enablePTD + * to 0. In this case the client is required to send the picture in encoding + * order to NvEncodeAPI by doing the re-ordering for B frames. \n + * If the client doesn't want to take the picture type decision it can enable + * picture type decision module in the NvEncodeAPI interface by setting + * NV_ENC_INITIALIZE_PARAMS::enablePTD to 1 and send the input pictures in display + * order. + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * \param [in] createEncodeParams + * Refer ::_NV_ENC_INITIALIZE_PARAMS for details. + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_INVALID_VERSION \n + * ::NV_ENC_ERR_GENERIC \n + * + */ +NVENCSTATUS NVENCAPI +NvEncInitializeEncoder(void* encoder, + NV_ENC_INITIALIZE_PARAMS* createEncodeParams); + +// NvEncCreateInputBuffer +/** + * \brief Allocates Input buffer. + * + * This function is used to allocate an input buffer. The client must enumerate + * the input buffer format before allocating the input buffer resources. The + * NV_ENC_INPUT_PTR returned by the NvEncodeAPI interface in the + * NV_ENC_CREATE_INPUT_BUFFER::inputBuffer field can be directly used in + * ::NvEncEncodePicture() API. The number of input buffers to be allocated by the + * client must be at least 4 more than the number of B frames being used for encoding. + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * \param [in,out] createInputBufferParams + * Pointer to the ::NV_ENC_CREATE_INPUT_BUFFER structure. + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_INVALID_VERSION \n + * ::NV_ENC_ERR_GENERIC \n + * + */ +NVENCSTATUS NVENCAPI +NvEncCreateInputBuffer(void* encoder, + NV_ENC_CREATE_INPUT_BUFFER* createInputBufferParams); + +// NvEncDestroyInputBuffer +/** + * \brief Release an input buffers. + * + * This function is used to free an input buffer. If the client has allocated + * any input buffer using ::NvEncCreateInputBuffer() API, it must free those + * input buffers by calling this function. The client must release the input + * buffers before destroying the encoder using ::NvEncDestroyEncoder() API. + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * \param [in] inputBuffer + * Pointer to the input buffer to be released. + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_INVALID_VERSION \n + * ::NV_ENC_ERR_GENERIC \n + * + */ +NVENCSTATUS NVENCAPI NvEncDestroyInputBuffer(void* encoder, + NV_ENC_INPUT_PTR inputBuffer); + +// NvEncSetIOCudaStreams +/** + * \brief Set input and output CUDA stream for specified encoder attribute. + * + * Encoding may involve CUDA pre-processing on the input and post-processing on encoded output. + * This function is used to set input and output CUDA streams to pipeline the CUDA pre-processing + * and post-processing tasks. Clients should call this function before the call to + * NvEncUnlockInputBuffer(). If this function is not called, the default CUDA stream is used for + * input and output processing. After a successful call to this function, the streams specified + * in that call will replace the previously-used streams. + * This API is supported for NVCUVID interface only. + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * \param [in] inputStream + * Pointer to CUstream which is used to process ::NV_ENC_PIC_PARAMS::inputFrame for encode. + * In case of ME-only mode, inputStream is used to process ::NV_ENC_MEONLY_PARAMS::inputBuffer and + * ::NV_ENC_MEONLY_PARAMS::referenceFrame + * \param [in] outputStream + * Pointer to CUstream which is used to process ::NV_ENC_PIC_PARAMS::outputBuffer for encode. + * In case of ME-only mode, outputStream is used to process ::NV_ENC_MEONLY_PARAMS::mvBuffer + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_INVALID_VERSION \n + * ::NV_ENC_ERR_GENERIC \n + */ +NVENCSTATUS NVENCAPI NvEncSetIOCudaStreams(void* encoder, + NV_ENC_CUSTREAM_PTR inputStream, + NV_ENC_CUSTREAM_PTR outputStream); + +// NvEncCreateBitstreamBuffer +/** + * \brief Allocates an output bitstream buffer + * + * This function is used to allocate an output bitstream buffer and returns a + * NV_ENC_OUTPUT_PTR to bitstream buffer to the client in the + * NV_ENC_CREATE_BITSTREAM_BUFFER::bitstreamBuffer field. + * The client can only call this function after the encoder session has been + * initialized using ::NvEncInitializeEncoder() API. The minimum number of output + * buffers allocated by the client must be at least 4 more than the number of B + * B frames being used for encoding. The client can only access the output + * bitstream data by locking the \p bitstreamBuffer using the ::NvEncLockBitstream() + * function. + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * \param [in,out] createBitstreamBufferParams + * Pointer ::NV_ENC_CREATE_BITSTREAM_BUFFER for details. + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_INVALID_VERSION \n + * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n + * ::NV_ENC_ERR_GENERIC \n + * + */ +NVENCSTATUS NVENCAPI NvEncCreateBitstreamBuffer( + void* encoder, + NV_ENC_CREATE_BITSTREAM_BUFFER* createBitstreamBufferParams); + +// NvEncDestroyBitstreamBuffer +/** + * \brief Release a bitstream buffer. + * + * This function is used to release the output bitstream buffer allocated using + * the ::NvEncCreateBitstreamBuffer() function. The client must release the output + * bitstreamBuffer using this function before destroying the encoder session. + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * \param [in] bitstreamBuffer + * Pointer to the bitstream buffer being released. + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_INVALID_VERSION \n + * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n + * ::NV_ENC_ERR_GENERIC \n + * + */ +NVENCSTATUS NVENCAPI +NvEncDestroyBitstreamBuffer(void* encoder, NV_ENC_OUTPUT_PTR bitstreamBuffer); + +// NvEncEncodePicture +/** + * \brief Submit an input picture for encoding. + * + * This function is used to submit an input picture buffer for encoding. The + * encoding parameters are passed using \p *encodePicParams which is a pointer + * to the ::_NV_ENC_PIC_PARAMS structure. + * + * If the client has set NV_ENC_INITIALIZE_PARAMS::enablePTD to 0, then it must + * send a valid value for the following fields. + * - NV_ENC_PIC_PARAMS::pictureType + * - NV_ENC_PIC_PARAMS_H264::displayPOCSyntax (H264 only) + * - NV_ENC_PIC_PARAMS_H264::frameNumSyntax(H264 only) + * - NV_ENC_PIC_PARAMS_H264::refPicFlag(H264 only) + * + *\par MVC Encoding: + * For MVC encoding the client must call encode picture API for each view separately + * and must pass valid view id in NV_ENC_PIC_PARAMS_MVC::viewID field. Currently + * NvEncodeAPI only support stereo MVC so client must send viewID as 0 for base + * view and view ID as 1 for dependent view. + * + *\par Asynchronous Encoding + * If the client has enabled asynchronous mode of encoding by setting + * NV_ENC_INITIALIZE_PARAMS::enableEncodeAsync to 1 in the ::NvEncInitializeEncoder() + * API ,then the client must send a valid NV_ENC_PIC_PARAMS::completionEvent. + * Incase of asynchronous mode of operation, client can queue the ::NvEncEncodePicture() + * API commands from the main thread and then queue output buffers to be processed + * to a secondary worker thread. Before the locking the output buffers in the + * secondary thread , the client must wait on NV_ENC_PIC_PARAMS::completionEvent + * it has queued in ::NvEncEncodePicture() API call. The client must always process + * completion event and the output buffer in the same order in which they have been + * submitted for encoding. The NvEncodeAPI interface is responsible for any + * re-ordering required for B frames and will always ensure that encoded bitstream + * data is written in the same order in which output buffer is submitted. + * The NvEncodeAPI interface may return ::NV_ENC_ERR_NEED_MORE_INPUT error code for + * some ::NvEncEncodePicture() API calls but the client must not treat it as a fatal error. + * The NvEncodeAPI interface might not be able to submit an input picture buffer for encoding + * immediately due to re-ordering for B frames. + *\code + The below example shows how asynchronous encoding in case of 1 B frames + ------------------------------------------------------------------------ + Suppose the client allocated 4 input buffers(I1,I2..), 4 output buffers(O1,O2..) + and 4 completion events(E1, E2, ...). The NvEncodeAPI interface will need to + keep a copy of the input buffers for re-ordering and it allocates following + internal buffers (NvI1, NvI2...). These internal buffers are managed by NvEncodeAPI + and the client is not responsible for the allocating or freeing the memory of + the internal buffers. + + a) The client main thread will queue the following encode frame calls. + Note the picture type is unknown to the client, the decision is being taken by + NvEncodeAPI interface. The client should pass ::_NV_ENC_PIC_PARAMS parameter + consisting of allocated input buffer, output buffer and output events in successive + ::NvEncEncodePicture() API calls along with other required encode picture params. + For example: + 1st EncodePicture parameters - (I1, O1, E1) + 2nd EncodePicture parameters - (I2, O2, E2) + 3rd EncodePicture parameters - (I3, O3, E3) + + b) NvEncodeAPI SW will receive the following encode Commands from the client. + The left side shows input from client in the form (Input buffer, Output Buffer, + Output Event). The right hand side shows a possible picture type decision take by + the NvEncodeAPI interface. + (I1, O1, E1) ---P1 Frame + (I2, O2, E2) ---B2 Frame + (I3, O3, E3) ---P3 Frame + + c) NvEncodeAPI interface will make a copy of the input buffers to its internal + buffers for re-ordering. These copies are done as part of nvEncEncodePicture + function call from the client and NvEncodeAPI interface is responsible for + synchronization of copy operation with the actual encoding operation. + I1 --> NvI1 + I2 --> NvI2 + I3 --> NvI3 + + d) The NvEncodeAPI encodes I1 as P frame and submits I1 to encoder HW and returns ::NV_ENC_SUCCESS. + The NvEncodeAPI tries to encode I2 as B frame and fails with ::NV_ENC_ERR_NEED_MORE_INPUT error code. + The error is not fatal and it notifies client that I2 is not submitted to encoder immediately. + The NvEncodeAPI encodes I3 as P frame and submits I3 for encoding which will be used as backward + reference frame for I2. The NvEncodeAPI then submits I2 for encoding and returns ::NV_ENC_SUCESS. + Both the submission are part of the same ::NvEncEncodePicture() function call. + + e) After returning from ::NvEncEncodePicture() call , the client must queue the output + bitstream processing work to the secondary thread. The output bitstream processing + for asynchronous mode consist of first waiting on completion event(E1, E2..) + and then locking the output bitstream buffer(O1, O2..) for reading the encoded + data. The work queued to the secondary thread by the client is in the following order + (I1, O1, E1) + (I2, O2, E2) + (I3, O3, E3) + Note they are in the same order in which client calls ::NvEncEncodePicture() API + in \p step a). + + f) NvEncodeAPI interface will do the re-ordering such that Encoder HW will receive + the following encode commands: + (NvI1, O1, E1) ---P1 Frame + (NvI3, O2, E2) ---P3 Frame + (NvI2, O3, E3) ---B2 frame + + g) After the encoding operations are completed, the events will be signaled + by NvEncodeAPI interface in the following order : + (O1, E1) ---P1 Frame ,output bitstream copied to O1 and event E1 signaled. + (O2, E2) ---P3 Frame ,output bitstream copied to O2 and event E2 signaled. + (O3, E3) ---B2 Frame ,output bitstream copied to O3 and event E3 signaled. + + h) The client must lock the bitstream data using ::NvEncLockBitstream() API in + the order O1,O2,O3 to read the encoded data, after waiting for the events + to be signaled in the same order i.e E1, E2 and E3.The output processing is + done in the secondary thread in the following order: + Waits on E1, copies encoded bitstream from O1 + Waits on E2, copies encoded bitstream from O2 + Waits on E3, copies encoded bitstream from O3 + + -Note the client will receive the events signaling and output buffer in the + same order in which they have submitted for encoding. + -Note the LockBitstream will have picture type field which will notify the + output picture type to the clients. + -Note the input, output buffer and the output completion event are free to be + reused once NvEncodeAPI interfaced has signaled the event and the client has + copied the data from the output buffer. + + * \endcode + * + *\par Synchronous Encoding + * The client can enable synchronous mode of encoding by setting + * NV_ENC_INITIALIZE_PARAMS::enableEncodeAsync to 0 in ::NvEncInitializeEncoder() API. + * The NvEncodeAPI interface may return ::NV_ENC_ERR_NEED_MORE_INPUT error code for + * some ::NvEncEncodePicture() API calls when NV_ENC_INITIALIZE_PARAMS::enablePTD + * is set to 1, but the client must not treat it as a fatal error. The NvEncodeAPI + * interface might not be able to submit an input picture buffer for encoding + * immediately due to re-ordering for B frames. The NvEncodeAPI interface cannot + * submit the input picture which is decided to be encoded as B frame as it waits + * for backward reference from temporally subsequent frames. This input picture + * is buffered internally and waits for more input picture to arrive. The client + * must not call ::NvEncLockBitstream() API on the output buffers whose + * ::NvEncEncodePicture() API returns ::NV_ENC_ERR_NEED_MORE_INPUT. The client must + * wait for the NvEncodeAPI interface to return ::NV_ENC_SUCCESS before locking the + * output bitstreams to read the encoded bitstream data. The following example + * explains the scenario with synchronous encoding with 2 B frames. + *\code + The below example shows how synchronous encoding works in case of 1 B frames + ----------------------------------------------------------------------------- + Suppose the client allocated 4 input buffers(I1,I2..), 4 output buffers(O1,O2..) + and 4 completion events(E1, E2, ...). The NvEncodeAPI interface will need to + keep a copy of the input buffers for re-ordering and it allocates following + internal buffers (NvI1, NvI2...). These internal buffers are managed by NvEncodeAPI + and the client is not responsible for the allocating or freeing the memory of + the internal buffers. + + The client calls ::NvEncEncodePicture() API with input buffer I1 and output buffer O1. + The NvEncodeAPI decides to encode I1 as P frame and submits it to encoder + HW and returns ::NV_ENC_SUCCESS. + The client can now read the encoded data by locking the output O1 by calling + NvEncLockBitstream API. + + The client calls ::NvEncEncodePicture() API with input buffer I2 and output buffer O2. + The NvEncodeAPI decides to encode I2 as B frame and buffers I2 by copying it + to internal buffer and returns ::NV_ENC_ERR_NEED_MORE_INPUT. + The error is not fatal and it notifies client that it cannot read the encoded + data by locking the output O2 by calling ::NvEncLockBitstream() API without submitting + more work to the NvEncodeAPI interface. + + The client calls ::NvEncEncodePicture() with input buffer I3 and output buffer O3. + The NvEncodeAPI decides to encode I3 as P frame and it first submits I3 for + encoding which will be used as backward reference frame for I2. + The NvEncodeAPI then submits I2 for encoding and returns ::NV_ENC_SUCESS. Both + the submission are part of the same ::NvEncEncodePicture() function call. + The client can now read the encoded data for both the frames by locking the output + O2 followed by O3 ,by calling ::NvEncLockBitstream() API. + + The client must always lock the output in the same order in which it has submitted + to receive the encoded bitstream in correct encoding order. + + * \endcode + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * \param [in,out] encodePicParams + * Pointer to the ::_NV_ENC_PIC_PARAMS structure. + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_INVALID_VERSION \n + * ::NV_ENC_ERR_ENCODER_BUSY \n + * ::NV_ENC_ERR_NEED_MORE_INPUT \n + * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n + * ::NV_ENC_ERR_GENERIC \n + * + */ +NVENCSTATUS NVENCAPI NvEncEncodePicture(void* encoder, + NV_ENC_PIC_PARAMS* encodePicParams); + +// NvEncLockBitstream +/** + * \brief Lock output bitstream buffer + * + * This function is used to lock the bitstream buffer to read the encoded data. + * The client can only access the encoded data by calling this function. + * The pointer to client accessible encoded data is returned in the + * NV_ENC_LOCK_BITSTREAM::bitstreamBufferPtr field. The size of the encoded data + * in the output buffer is returned in the NV_ENC_LOCK_BITSTREAM::bitstreamSizeInBytes + * The NvEncodeAPI interface also returns the output picture type and picture structure + * of the encoded frame in NV_ENC_LOCK_BITSTREAM::pictureType and + * NV_ENC_LOCK_BITSTREAM::pictureStruct fields respectively. If the client has + * set NV_ENC_LOCK_BITSTREAM::doNotWait to 1, the function might return + * ::NV_ENC_ERR_LOCK_BUSY if client is operating in synchronous mode. This is not + * a fatal failure if NV_ENC_LOCK_BITSTREAM::doNotWait is set to 1. In the above case the client can + * retry the function after few milliseconds. + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * \param [in,out] lockBitstreamBufferParams + * Pointer to the ::_NV_ENC_LOCK_BITSTREAM structure. + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_INVALID_VERSION \n + * ::NV_ENC_ERR_LOCK_BUSY \n + * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n + * ::NV_ENC_ERR_GENERIC \n + * + */ +NVENCSTATUS NVENCAPI +NvEncLockBitstream(void* encoder, + NV_ENC_LOCK_BITSTREAM* lockBitstreamBufferParams); + +// NvEncUnlockBitstream +/** + * \brief Unlock the output bitstream buffer + * + * This function is used to unlock the output bitstream buffer after the client + * has read the encoded data from output buffer. The client must call this function + * to unlock the output buffer which it has previously locked using ::NvEncLockBitstream() + * function. Using a locked bitstream buffer in ::NvEncEncodePicture() API will cause + * the function to fail. + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * \param [in,out] bitstreamBuffer + * bitstream buffer pointer being unlocked + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n + * ::NV_ENC_ERR_GENERIC \n + * + */ +NVENCSTATUS NVENCAPI NvEncUnlockBitstream(void* encoder, + NV_ENC_OUTPUT_PTR bitstreamBuffer); + +// NvLockInputBuffer +/** + * \brief Locks an input buffer + * + * This function is used to lock the input buffer to load the uncompressed YUV + * pixel data into input buffer memory. The client must pass the NV_ENC_INPUT_PTR + * it had previously allocated using ::NvEncCreateInputBuffer()in the + * NV_ENC_LOCK_INPUT_BUFFER::inputBuffer field. + * The NvEncodeAPI interface returns pointer to client accessible input buffer + * memory in NV_ENC_LOCK_INPUT_BUFFER::bufferDataPtr field. + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * \param [in,out] lockInputBufferParams + * Pointer to the ::_NV_ENC_LOCK_INPUT_BUFFER structure + * + * \return + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_INVALID_VERSION \n + * ::NV_ENC_ERR_LOCK_BUSY \n + * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n + * ::NV_ENC_ERR_GENERIC \n + * + */ +NVENCSTATUS NVENCAPI +NvEncLockInputBuffer(void* encoder, + NV_ENC_LOCK_INPUT_BUFFER* lockInputBufferParams); + +// NvUnlockInputBuffer +/** + * \brief Unlocks the input buffer + * + * This function is used to unlock the input buffer memory previously locked for + * uploading YUV pixel data. The input buffer must be unlocked before being used + * again for encoding, otherwise NvEncodeAPI will fail the ::NvEncEncodePicture() + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * \param [in] inputBuffer + * Pointer to the input buffer that is being unlocked. + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_VERSION \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n + * ::NV_ENC_ERR_GENERIC \n + * + * + */ +NVENCSTATUS NVENCAPI NvEncUnlockInputBuffer(void* encoder, + NV_ENC_INPUT_PTR inputBuffer); + +// NvEncGetEncodeStats +/** + * \brief Get encoding statistics. + * + * This function is used to retrieve the encoding statistics. + * This API is not supported when encode device type is CUDA. + * Note that this API will be removed in future Video Codec SDK release. + * Clients should use NvEncLockBitstream() API to retrieve the encoding statistics. + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * \param [in,out] encodeStats + * Pointer to the ::_NV_ENC_STAT structure. + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n + * ::NV_ENC_ERR_GENERIC \n + * + */ +NVENCSTATUS NVENCAPI NvEncGetEncodeStats(void* encoder, + NV_ENC_STAT* encodeStats); + +// NvEncGetSequenceParams +/** + * \brief Get encoded sequence and picture header. + * + * This function can be used to retrieve the sequence and picture header out of + * band. The client must call this function only after the encoder has been + * initialized using ::NvEncInitializeEncoder() function. The client must + * allocate the memory where the NvEncodeAPI interface can copy the bitstream + * header and pass the pointer to the memory in NV_ENC_SEQUENCE_PARAM_PAYLOAD::spsppsBuffer. + * The size of buffer is passed in the field NV_ENC_SEQUENCE_PARAM_PAYLOAD::inBufferSize. + * The NvEncodeAPI interface will copy the bitstream header payload and returns + * the actual size of the bitstream header in the field + * NV_ENC_SEQUENCE_PARAM_PAYLOAD::outSPSPPSPayloadSize. + * The client must call ::NvEncGetSequenceParams() function from the same thread which is + * being used to call ::NvEncEncodePicture() function. + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * \param [in,out] sequenceParamPayload + * Pointer to the ::_NV_ENC_SEQUENCE_PARAM_PAYLOAD structure. + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_VERSION \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n + * ::NV_ENC_ERR_GENERIC \n + * + */ +NVENCSTATUS NVENCAPI +NvEncGetSequenceParams(void* encoder, + NV_ENC_SEQUENCE_PARAM_PAYLOAD* sequenceParamPayload); + +// NvEncGetSequenceParamEx +/** + * \brief Get sequence and picture header. + * + * This function can be used to retrieve the sequence and picture header out of band, even when + * encoder has not been initialized using ::NvEncInitializeEncoder() function. + * The client must allocate the memory where the NvEncodeAPI interface can copy the bitstream + * header and pass the pointer to the memory in NV_ENC_SEQUENCE_PARAM_PAYLOAD::spsppsBuffer. + * The size of buffer is passed in the field NV_ENC_SEQUENCE_PARAM_PAYLOAD::inBufferSize. + * If encoder has not been initialized using ::NvEncInitializeEncoder() function, client must + * send NV_ENC_INITIALIZE_PARAMS as input. The NV_ENC_INITIALIZE_PARAMS passed must be same as the + * one which will be used for initializing encoder using ::NvEncInitializeEncoder() function later. + * If encoder is already initialized using ::NvEncInitializeEncoder() function, the provided + * NV_ENC_INITIALIZE_PARAMS structure is ignored. The NvEncodeAPI interface will copy the bitstream + * header payload and returns the actual size of the bitstream header in the field + * NV_ENC_SEQUENCE_PARAM_PAYLOAD::outSPSPPSPayloadSize. The client must call ::NvEncGetSequenceParamsEx() + * function from the same thread which is being used to call ::NvEncEncodePicture() function. + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * \param [in] encInitParams + * Pointer to the _NV_ENC_INITIALIZE_PARAMS structure. + * \param [in,out] sequenceParamPayload + * Pointer to the ::_NV_ENC_SEQUENCE_PARAM_PAYLOAD structure. + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_VERSION \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_GENERIC \n + * + */ +NVENCSTATUS NVENCAPI +NvEncGetSequenceParamEx(void* encoder, + NV_ENC_INITIALIZE_PARAMS* encInitParams, + NV_ENC_SEQUENCE_PARAM_PAYLOAD* sequenceParamPayload); + +// NvEncRegisterAsyncEvent +/** + * \brief Register event for notification to encoding completion. + * + * This function is used to register the completion event with NvEncodeAPI + * interface. The event is required when the client has configured the encoder to + * work in asynchronous mode. In this mode the client needs to send a completion + * event with every output buffer. The NvEncodeAPI interface will signal the + * completion of the encoding process using this event. Only after the event is + * signaled the client can get the encoded data using ::NvEncLockBitstream() function. + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * \param [in] eventParams + * Pointer to the ::_NV_ENC_EVENT_PARAMS structure. + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_VERSION \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n + * ::NV_ENC_ERR_GENERIC \n + * + */ +NVENCSTATUS NVENCAPI NvEncRegisterAsyncEvent(void* encoder, + NV_ENC_EVENT_PARAMS* eventParams); + +// NvEncUnregisterAsyncEvent +/** + * \brief Unregister completion event. + * + * This function is used to unregister completion event which has been previously + * registered using ::NvEncRegisterAsyncEvent() function. The client must unregister + * all events before destroying the encoder using ::NvEncDestroyEncoder() function. + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * \param [in] eventParams + * Pointer to the ::_NV_ENC_EVENT_PARAMS structure. + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_VERSION \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n + * ::NV_ENC_ERR_GENERIC \n + * + */ +NVENCSTATUS NVENCAPI +NvEncUnregisterAsyncEvent(void* encoder, NV_ENC_EVENT_PARAMS* eventParams); + +// NvEncMapInputResource +/** + * \brief Map an externally created input resource pointer for encoding. + * + * Maps an externally allocated input resource [using and returns a NV_ENC_INPUT_PTR + * which can be used for encoding in the ::NvEncEncodePicture() function. The + * mapped resource is returned in the field NV_ENC_MAP_INPUT_RESOURCE::outputResourcePtr. + * The NvEncodeAPI interface also returns the buffer format of the mapped resource + * in the field NV_ENC_MAP_INPUT_RESOURCE::outbufferFmt. + * This function provides synchronization guarantee that any graphics work submitted + * on the input buffer is completed before the buffer is used for encoding. This is + * also true for compute (i.e. CUDA) work, provided that the previous workload using + * the input resource was submitted to the default stream. + * The client should not access any input buffer while they are mapped by the encoder. + * For D3D12 interface type, this function does not provide synchronization guarantee. + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * \param [in,out] mapInputResParams + * Pointer to the ::_NV_ENC_MAP_INPUT_RESOURCE structure. + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_VERSION \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n + * ::NV_ENC_ERR_RESOURCE_NOT_REGISTERED \n + * ::NV_ENC_ERR_MAP_FAILED \n + * ::NV_ENC_ERR_GENERIC \n + * + */ +NVENCSTATUS NVENCAPI +NvEncMapInputResource(void* encoder, + NV_ENC_MAP_INPUT_RESOURCE* mapInputResParams); + +// NvEncUnmapInputResource +/** + * \brief UnMaps a NV_ENC_INPUT_PTR which was mapped for encoding + * + * + * UnMaps an input buffer which was previously mapped using ::NvEncMapInputResource() + * API. The mapping created using ::NvEncMapInputResource() should be invalidated + * using this API before the external resource is destroyed by the client. The client + * must unmap the buffer after ::NvEncLockBitstream() API returns successfully for encode + * work submitted using the mapped input buffer. + * + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * \param [in] mappedInputBuffer + * Pointer to the NV_ENC_INPUT_PTR + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_VERSION \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n + * ::NV_ENC_ERR_RESOURCE_NOT_REGISTERED \n + * ::NV_ENC_ERR_RESOURCE_NOT_MAPPED \n + * ::NV_ENC_ERR_GENERIC \n + * + */ +NVENCSTATUS NVENCAPI +NvEncUnmapInputResource(void* encoder, NV_ENC_INPUT_PTR mappedInputBuffer); + +// NvEncDestroyEncoder +/** + * \brief Destroy Encoding Session + * + * Destroys the encoder session previously created using ::NvEncOpenEncodeSession() + * function. The client must flush the encoder before freeing any resources. In order + * to flush the encoder the client must pass a NULL encode picture packet and either + * wait for the ::NvEncEncodePicture() function to return in synchronous mode or wait + * for the flush event to be signaled by the encoder in asynchronous mode. + * The client must free all the input and output resources created using the + * NvEncodeAPI interface before destroying the encoder. If the client is operating + * in asynchronous mode, it must also unregister the completion events previously + * registered. + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_GENERIC \n + * + */ +NVENCSTATUS NVENCAPI NvEncDestroyEncoder(void* encoder); + +// NvEncInvalidateRefFrames +/** + * \brief Invalidate reference frames + * + * Invalidates reference frame based on the time stamp provided by the client. + * The encoder marks any reference frames or any frames which have been reconstructed + * using the corrupt frame as invalid for motion estimation and uses older reference + * frames for motion estimation. The encoder forces the current frame to be encoded + * as an intra frame if no reference frames are left after invalidation process. + * This is useful for low latency application for error resiliency. The client + * is recommended to set NV_ENC_CONFIG_H264::maxNumRefFrames to a large value so + * that encoder can keep a backup of older reference frames in the DPB and can use them + * for motion estimation when the newer reference frames have been invalidated. + * This API can be called multiple times. + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * \param [in] invalidRefFrameTimeStamp + * Timestamp of the invalid reference frames which needs to be invalidated. + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_GENERIC \n + * + */ +NVENCSTATUS NVENCAPI +NvEncInvalidateRefFrames(void* encoder, uint64_t invalidRefFrameTimeStamp); + +// NvEncOpenEncodeSessionEx +/** + * \brief Opens an encoding session. + * + * Opens an encoding session and returns a pointer to the encoder interface in + * the \p **encoder parameter. The client should start encoding process by calling + * this API first. + * The client must pass a pointer to IDirect3DDevice9 device or CUDA context in the \p *device parameter. + * For the OpenGL interface, \p device must be NULL. An OpenGL context must be current when + * calling all NvEncodeAPI functions. + * If the creation of encoder session fails, the client must call ::NvEncDestroyEncoder API + * before exiting. + * + * \param [in] openSessionExParams + * Pointer to a ::NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS structure. + * \param [out] encoder + * Encode Session pointer to the NvEncodeAPI interface. + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_NO_ENCODE_DEVICE \n + * ::NV_ENC_ERR_UNSUPPORTED_DEVICE \n + * ::NV_ENC_ERR_INVALID_DEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_GENERIC \n + * + */ +NVENCSTATUS NVENCAPI NvEncOpenEncodeSessionEx( + NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS* openSessionExParams, + void** encoder); + +// NvEncRegisterResource +/** + * \brief Registers a resource with the Nvidia Video Encoder Interface. + * + * Registers a resource with the Nvidia Video Encoder Interface for book keeping. + * The client is expected to pass the registered resource handle as well, while calling ::NvEncMapInputResource API. + * + * \param [in] encoder + * Pointer to the NVEncodeAPI interface. + * + * \param [in] registerResParams + * Pointer to a ::_NV_ENC_REGISTER_RESOURCE structure + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_VERSION \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n + * ::NV_ENC_ERR_RESOURCE_REGISTER_FAILED \n + * ::NV_ENC_ERR_GENERIC \n + * ::NV_ENC_ERR_UNIMPLEMENTED \n + * + */ +NVENCSTATUS NVENCAPI +NvEncRegisterResource(void* encoder, + NV_ENC_REGISTER_RESOURCE* registerResParams); + +// NvEncUnregisterResource +/** + * \brief Unregisters a resource previously registered with the Nvidia Video Encoder Interface. + * + * Unregisters a resource previously registered with the Nvidia Video Encoder Interface. + * The client is expected to unregister any resource that it has registered with the + * Nvidia Video Encoder Interface before destroying the resource. + * + * \param [in] encoder + * Pointer to the NVEncodeAPI interface. + * + * \param [in] registeredResource + * The registered resource pointer that was returned in ::NvEncRegisterResource. + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_VERSION \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n + * ::NV_ENC_ERR_RESOURCE_NOT_REGISTERED \n + * ::NV_ENC_ERR_GENERIC \n + * ::NV_ENC_ERR_UNIMPLEMENTED \n + * + */ +NVENCSTATUS NVENCAPI +NvEncUnregisterResource(void* encoder, + NV_ENC_REGISTERED_PTR registeredResource); + +// NvEncReconfigureEncoder +/** + * \brief Reconfigure an existing encoding session. + * + * Reconfigure an existing encoding session. + * The client should call this API to change/reconfigure the parameter passed during + * NvEncInitializeEncoder API call. + * Currently Reconfiguration of following are not supported. + * Change in GOP structure. + * Change in sync-Async mode. + * Change in MaxWidth & MaxHeight. + * Change in PTD mode. + * + * Resolution change is possible only if maxEncodeWidth & maxEncodeHeight of NV_ENC_INITIALIZE_PARAMS + * is set while creating encoder session. + * + * \param [in] encoder + * Pointer to the NVEncodeAPI interface. + * + * \param [in] reInitEncodeParams + * Pointer to a ::NV_ENC_RECONFIGURE_PARAMS structure. + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_NO_ENCODE_DEVICE \n + * ::NV_ENC_ERR_UNSUPPORTED_DEVICE \n + * ::NV_ENC_ERR_INVALID_DEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_GENERIC \n + * + */ +NVENCSTATUS NVENCAPI +NvEncReconfigureEncoder(void* encoder, + NV_ENC_RECONFIGURE_PARAMS* reInitEncodeParams); + +// NvEncCreateMVBuffer +/** + * \brief Allocates output MV buffer for ME only mode. + * + * This function is used to allocate an output MV buffer. The size of the mvBuffer is + * dependent on the frame height and width of the last ::NvEncCreateInputBuffer() call. + * The NV_ENC_OUTPUT_PTR returned by the NvEncodeAPI interface in the + * ::NV_ENC_CREATE_MV_BUFFER::mvBuffer field should be used in + * ::NvEncRunMotionEstimationOnly() API. + * Client must lock ::NV_ENC_CREATE_MV_BUFFER::mvBuffer using ::NvEncLockBitstream() API to get the motion vector data. + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * \param [in,out] createMVBufferParams + * Pointer to the ::NV_ENC_CREATE_MV_BUFFER structure. + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_INVALID_VERSION \n + * ::NV_ENC_ERR_GENERIC \n + */ +NVENCSTATUS NVENCAPI +NvEncCreateMVBuffer(void* encoder, + NV_ENC_CREATE_MV_BUFFER* createMVBufferParams); + +// NvEncDestroyMVBuffer +/** + * \brief Release an output MV buffer for ME only mode. + * + * This function is used to release the output MV buffer allocated using + * the ::NvEncCreateMVBuffer() function. The client must release the output + * mvBuffer using this function before destroying the encoder session. + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * \param [in] mvBuffer + * Pointer to the mvBuffer being released. + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_INVALID_VERSION \n + * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n + * ::NV_ENC_ERR_GENERIC \n + */ +NVENCSTATUS NVENCAPI NvEncDestroyMVBuffer(void* encoder, + NV_ENC_OUTPUT_PTR mvBuffer); + +// NvEncRunMotionEstimationOnly +/** + * \brief Submit an input picture and reference frame for motion estimation in ME only mode. + * + * This function is used to submit the input frame and reference frame for motion + * estimation. The ME parameters are passed using *meOnlyParams which is a pointer + * to ::_NV_ENC_MEONLY_PARAMS structure. + * Client must lock ::NV_ENC_CREATE_MV_BUFFER::mvBuffer using ::NvEncLockBitstream() API to get the motion vector data. + * to get motion vector data. + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * \param [in] meOnlyParams + * Pointer to the ::_NV_ENC_MEONLY_PARAMS structure. + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + * ::NV_ENC_ERR_INVALID_ENCODERDEVICE \n + * ::NV_ENC_ERR_DEVICE_NOT_EXIST \n + * ::NV_ENC_ERR_UNSUPPORTED_PARAM \n + * ::NV_ENC_ERR_OUT_OF_MEMORY \n + * ::NV_ENC_ERR_INVALID_PARAM \n + * ::NV_ENC_ERR_INVALID_VERSION \n + * ::NV_ENC_ERR_NEED_MORE_INPUT \n + * ::NV_ENC_ERR_ENCODER_NOT_INITIALIZED \n + * ::NV_ENC_ERR_GENERIC \n + */ +NVENCSTATUS NVENCAPI +NvEncRunMotionEstimationOnly(void* encoder, NV_ENC_MEONLY_PARAMS* meOnlyParams); + +// NvEncodeAPIGetMaxSupportedVersion +/** + * \brief Get the largest NvEncodeAPI version supported by the driver. + * + * This function can be used by clients to determine if the driver supports + * the NvEncodeAPI header the application was compiled with. + * + * \param [out] version + * Pointer to the requested value. The 4 least significant bits in the returned + * indicate the minor version and the rest of the bits indicate the major + * version of the largest supported version. + * + * \return + * ::NV_ENC_SUCCESS \n + * ::NV_ENC_ERR_INVALID_PTR \n + */ +NVENCSTATUS NVENCAPI NvEncodeAPIGetMaxSupportedVersion(uint32_t* version); + +// NvEncGetLastErrorString +/** + * \brief Get the description of the last error reported by the API. + * + * This function returns a null-terminated string that can be used by clients to better understand the reason + * for failure of a previous API call. + * + * \param [in] encoder + * Pointer to the NvEncodeAPI interface. + * + * \return + * Pointer to buffer containing the details of the last error encountered by the API. + */ +const char* NVENCAPI NvEncGetLastErrorString(void* encoder); + +/// \cond API PFN +/* + * Defines API function pointers + */ +typedef NVENCSTATUS(NVENCAPI* PNVENCOPENENCODESESSION)(void* device, + uint32_t deviceType, + void** encoder); +typedef NVENCSTATUS(NVENCAPI* PNVENCGETENCODEGUIDCOUNT)( + void* encoder, + uint32_t* encodeGUIDCount); +typedef NVENCSTATUS(NVENCAPI* PNVENCGETENCODEGUIDS)(void* encoder, + GUID* GUIDs, + uint32_t guidArraySize, + uint32_t* GUIDCount); +typedef NVENCSTATUS(NVENCAPI* PNVENCGETENCODEPROFILEGUIDCOUNT)( + void* encoder, + GUID encodeGUID, + uint32_t* encodeProfileGUIDCount); +typedef NVENCSTATUS(NVENCAPI* PNVENCGETENCODEPROFILEGUIDS)( + void* encoder, + GUID encodeGUID, + GUID* profileGUIDs, + uint32_t guidArraySize, + uint32_t* GUIDCount); +typedef NVENCSTATUS(NVENCAPI* PNVENCGETINPUTFORMATCOUNT)( + void* encoder, + GUID encodeGUID, + uint32_t* inputFmtCount); +typedef NVENCSTATUS(NVENCAPI* PNVENCGETINPUTFORMATS)( + void* encoder, + GUID encodeGUID, + NV_ENC_BUFFER_FORMAT* inputFmts, + uint32_t inputFmtArraySize, + uint32_t* inputFmtCount); +typedef NVENCSTATUS(NVENCAPI* PNVENCGETENCODECAPS)(void* encoder, + GUID encodeGUID, + NV_ENC_CAPS_PARAM* capsParam, + int* capsVal); +typedef NVENCSTATUS(NVENCAPI* PNVENCGETENCODEPRESETCOUNT)( + void* encoder, + GUID encodeGUID, + uint32_t* encodePresetGUIDCount); +typedef NVENCSTATUS(NVENCAPI* PNVENCGETENCODEPRESETGUIDS)( + void* encoder, + GUID encodeGUID, + GUID* presetGUIDs, + uint32_t guidArraySize, + uint32_t* encodePresetGUIDCount); +typedef NVENCSTATUS(NVENCAPI* PNVENCGETENCODEPRESETCONFIG)( + void* encoder, + GUID encodeGUID, + GUID presetGUID, + NV_ENC_PRESET_CONFIG* presetConfig); +typedef NVENCSTATUS(NVENCAPI* PNVENCGETENCODEPRESETCONFIGEX)( + void* encoder, + GUID encodeGUID, + GUID presetGUID, + NV_ENC_TUNING_INFO tuningInfo, + NV_ENC_PRESET_CONFIG* presetConfig); +typedef NVENCSTATUS(NVENCAPI* PNVENCINITIALIZEENCODER)( + void* encoder, + NV_ENC_INITIALIZE_PARAMS* createEncodeParams); +typedef NVENCSTATUS(NVENCAPI* PNVENCCREATEINPUTBUFFER)( + void* encoder, + NV_ENC_CREATE_INPUT_BUFFER* createInputBufferParams); +typedef NVENCSTATUS(NVENCAPI* PNVENCDESTROYINPUTBUFFER)( + void* encoder, + NV_ENC_INPUT_PTR inputBuffer); +typedef NVENCSTATUS(NVENCAPI* PNVENCCREATEBITSTREAMBUFFER)( + void* encoder, + NV_ENC_CREATE_BITSTREAM_BUFFER* createBitstreamBufferParams); +typedef NVENCSTATUS(NVENCAPI* PNVENCDESTROYBITSTREAMBUFFER)( + void* encoder, + NV_ENC_OUTPUT_PTR bitstreamBuffer); +typedef NVENCSTATUS(NVENCAPI* PNVENCENCODEPICTURE)( + void* encoder, + NV_ENC_PIC_PARAMS* encodePicParams); +typedef NVENCSTATUS(NVENCAPI* PNVENCLOCKBITSTREAM)( + void* encoder, + NV_ENC_LOCK_BITSTREAM* lockBitstreamBufferParams); +typedef NVENCSTATUS(NVENCAPI* PNVENCUNLOCKBITSTREAM)( + void* encoder, + NV_ENC_OUTPUT_PTR bitstreamBuffer); +typedef NVENCSTATUS(NVENCAPI* PNVENCLOCKINPUTBUFFER)( + void* encoder, + NV_ENC_LOCK_INPUT_BUFFER* lockInputBufferParams); +typedef NVENCSTATUS(NVENCAPI* PNVENCUNLOCKINPUTBUFFER)( + void* encoder, + NV_ENC_INPUT_PTR inputBuffer); +typedef NVENCSTATUS(NVENCAPI* PNVENCGETENCODESTATS)(void* encoder, + NV_ENC_STAT* encodeStats); +typedef NVENCSTATUS(NVENCAPI* PNVENCGETSEQUENCEPARAMS)( + void* encoder, + NV_ENC_SEQUENCE_PARAM_PAYLOAD* sequenceParamPayload); +typedef NVENCSTATUS(NVENCAPI* PNVENCREGISTERASYNCEVENT)( + void* encoder, + NV_ENC_EVENT_PARAMS* eventParams); +typedef NVENCSTATUS(NVENCAPI* PNVENCUNREGISTERASYNCEVENT)( + void* encoder, + NV_ENC_EVENT_PARAMS* eventParams); +typedef NVENCSTATUS(NVENCAPI* PNVENCMAPINPUTRESOURCE)( + void* encoder, + NV_ENC_MAP_INPUT_RESOURCE* mapInputResParams); +typedef NVENCSTATUS(NVENCAPI* PNVENCUNMAPINPUTRESOURCE)( + void* encoder, + NV_ENC_INPUT_PTR mappedInputBuffer); +typedef NVENCSTATUS(NVENCAPI* PNVENCDESTROYENCODER)(void* encoder); +typedef NVENCSTATUS(NVENCAPI* PNVENCINVALIDATEREFFRAMES)( + void* encoder, + uint64_t invalidRefFrameTimeStamp); +typedef NVENCSTATUS(NVENCAPI* PNVENCOPENENCODESESSIONEX)( + NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS* openSessionExParams, + void** encoder); +typedef NVENCSTATUS(NVENCAPI* PNVENCREGISTERRESOURCE)( + void* encoder, + NV_ENC_REGISTER_RESOURCE* registerResParams); +typedef NVENCSTATUS(NVENCAPI* PNVENCUNREGISTERRESOURCE)( + void* encoder, + NV_ENC_REGISTERED_PTR registeredRes); +typedef NVENCSTATUS(NVENCAPI* PNVENCRECONFIGUREENCODER)( + void* encoder, + NV_ENC_RECONFIGURE_PARAMS* reInitEncodeParams); + +typedef NVENCSTATUS(NVENCAPI* PNVENCCREATEMVBUFFER)( + void* encoder, + NV_ENC_CREATE_MV_BUFFER* createMVBufferParams); +typedef NVENCSTATUS( + NVENCAPI* PNVENCDESTROYMVBUFFER)(void* encoder, NV_ENC_OUTPUT_PTR mvBuffer); +typedef NVENCSTATUS(NVENCAPI* PNVENCRUNMOTIONESTIMATIONONLY)( + void* encoder, + NV_ENC_MEONLY_PARAMS* meOnlyParams); +typedef const char*(NVENCAPI* PNVENCGETLASTERROR)(void* encoder); +typedef NVENCSTATUS(NVENCAPI* PNVENCSETIOCUDASTREAMS)( + void* encoder, + NV_ENC_CUSTREAM_PTR inputStream, + NV_ENC_CUSTREAM_PTR outputStream); +typedef NVENCSTATUS(NVENCAPI* PNVENCGETSEQUENCEPARAMEX)( + void* encoder, + NV_ENC_INITIALIZE_PARAMS* encInitParams, + NV_ENC_SEQUENCE_PARAM_PAYLOAD* sequenceParamPayload); + +/// \endcond + +/** @} */ /* END ENCODE_FUNC */ + +/** + * \ingroup ENCODER_STRUCTURE + * NV_ENCODE_API_FUNCTION_LIST + */ +typedef struct _NV_ENCODE_API_FUNCTION_LIST { + uint32_t + version; /**< [in]: Client should pass NV_ENCODE_API_FUNCTION_LIST_VER. */ + uint32_t + reserved; /**< [in]: Reserved and should be set to 0. */ + PNVENCOPENENCODESESSION + nvEncOpenEncodeSession; /**< [out]: Client should access ::NvEncOpenEncodeSession() API through this pointer. */ + PNVENCGETENCODEGUIDCOUNT + nvEncGetEncodeGUIDCount; /**< [out]: Client should access ::NvEncGetEncodeGUIDCount() API through this pointer. */ + PNVENCGETENCODEPRESETCOUNT + nvEncGetEncodeProfileGUIDCount; /**< [out]: Client should access ::NvEncGetEncodeProfileGUIDCount() API through this pointer.*/ + PNVENCGETENCODEPRESETGUIDS + nvEncGetEncodeProfileGUIDs; /**< [out]: Client should access ::NvEncGetEncodeProfileGUIDs() API through this pointer. */ + PNVENCGETENCODEGUIDS + nvEncGetEncodeGUIDs; /**< [out]: Client should access ::NvEncGetEncodeGUIDs() API through this pointer. */ + PNVENCGETINPUTFORMATCOUNT + nvEncGetInputFormatCount; /**< [out]: Client should access ::NvEncGetInputFormatCount() API through this pointer. */ + PNVENCGETINPUTFORMATS + nvEncGetInputFormats; /**< [out]: Client should access ::NvEncGetInputFormats() API through this pointer. */ + PNVENCGETENCODECAPS + nvEncGetEncodeCaps; /**< [out]: Client should access ::NvEncGetEncodeCaps() API through this pointer. */ + PNVENCGETENCODEPRESETCOUNT + nvEncGetEncodePresetCount; /**< [out]: Client should access ::NvEncGetEncodePresetCount() API through this pointer. */ + PNVENCGETENCODEPRESETGUIDS + nvEncGetEncodePresetGUIDs; /**< [out]: Client should access ::NvEncGetEncodePresetGUIDs() API through this pointer. */ + PNVENCGETENCODEPRESETCONFIG + nvEncGetEncodePresetConfig; /**< [out]: Client should access ::NvEncGetEncodePresetConfig() API through this pointer. */ + PNVENCINITIALIZEENCODER + nvEncInitializeEncoder; /**< [out]: Client should access ::NvEncInitializeEncoder() API through this pointer. */ + PNVENCCREATEINPUTBUFFER + nvEncCreateInputBuffer; /**< [out]: Client should access ::NvEncCreateInputBuffer() API through this pointer. */ + PNVENCDESTROYINPUTBUFFER + nvEncDestroyInputBuffer; /**< [out]: Client should access ::NvEncDestroyInputBuffer() API through this pointer. */ + PNVENCCREATEBITSTREAMBUFFER + nvEncCreateBitstreamBuffer; /**< [out]: Client should access ::NvEncCreateBitstreamBuffer() API through this pointer. */ + PNVENCDESTROYBITSTREAMBUFFER + nvEncDestroyBitstreamBuffer; /**< [out]: Client should access ::NvEncDestroyBitstreamBuffer() API through this pointer. */ + PNVENCENCODEPICTURE + nvEncEncodePicture; /**< [out]: Client should access ::NvEncEncodePicture() API through this pointer. */ + PNVENCLOCKBITSTREAM + nvEncLockBitstream; /**< [out]: Client should access ::NvEncLockBitstream() API through this pointer. */ + PNVENCUNLOCKBITSTREAM + nvEncUnlockBitstream; /**< [out]: Client should access ::NvEncUnlockBitstream() API through this pointer. */ + PNVENCLOCKINPUTBUFFER + nvEncLockInputBuffer; /**< [out]: Client should access ::NvEncLockInputBuffer() API through this pointer. */ + PNVENCUNLOCKINPUTBUFFER + nvEncUnlockInputBuffer; /**< [out]: Client should access ::NvEncUnlockInputBuffer() API through this pointer. */ + PNVENCGETENCODESTATS + nvEncGetEncodeStats; /**< [out]: Client should access ::NvEncGetEncodeStats() API through this pointer. */ + PNVENCGETSEQUENCEPARAMS + nvEncGetSequenceParams; /**< [out]: Client should access ::NvEncGetSequenceParams() API through this pointer. */ + PNVENCREGISTERASYNCEVENT + nvEncRegisterAsyncEvent; /**< [out]: Client should access ::NvEncRegisterAsyncEvent() API through this pointer. */ + PNVENCUNREGISTERASYNCEVENT + nvEncUnregisterAsyncEvent; /**< [out]: Client should access ::NvEncUnregisterAsyncEvent() API through this pointer. */ + PNVENCMAPINPUTRESOURCE + nvEncMapInputResource; /**< [out]: Client should access ::NvEncMapInputResource() API through this pointer. */ + PNVENCUNMAPINPUTRESOURCE + nvEncUnmapInputResource; /**< [out]: Client should access ::NvEncUnmapInputResource() API through this pointer. */ + PNVENCDESTROYENCODER + nvEncDestroyEncoder; /**< [out]: Client should access ::NvEncDestroyEncoder() API through this pointer. */ + PNVENCINVALIDATEREFFRAMES + nvEncInvalidateRefFrames; /**< [out]: Client should access ::NvEncInvalidateRefFrames() API through this pointer. */ + PNVENCOPENENCODESESSIONEX + nvEncOpenEncodeSessionEx; /**< [out]: Client should access ::NvEncOpenEncodeSession() API through this pointer. */ + PNVENCREGISTERRESOURCE + nvEncRegisterResource; /**< [out]: Client should access ::NvEncRegisterResource() API through this pointer. */ + PNVENCUNREGISTERRESOURCE + nvEncUnregisterResource; /**< [out]: Client should access ::NvEncUnregisterResource() API through this pointer. */ + PNVENCRECONFIGUREENCODER + nvEncReconfigureEncoder; /**< [out]: Client should access ::NvEncReconfigureEncoder() API through this pointer. */ + void* reserved1; + PNVENCCREATEMVBUFFER + nvEncCreateMVBuffer; /**< [out]: Client should access ::NvEncCreateMVBuffer API through this pointer. */ + PNVENCDESTROYMVBUFFER + nvEncDestroyMVBuffer; /**< [out]: Client should access ::NvEncDestroyMVBuffer API through this pointer. */ + PNVENCRUNMOTIONESTIMATIONONLY + nvEncRunMotionEstimationOnly; /**< [out]: Client should access ::NvEncRunMotionEstimationOnly API through this pointer. */ + PNVENCGETLASTERROR + nvEncGetLastErrorString; /**< [out]: Client should access ::nvEncGetLastErrorString API through this pointer. */ + PNVENCSETIOCUDASTREAMS + nvEncSetIOCudaStreams; /**< [out]: Client should access ::nvEncSetIOCudaStreams API through this pointer. */ + PNVENCGETENCODEPRESETCONFIGEX + nvEncGetEncodePresetConfigEx; /**< [out]: Client should access ::NvEncGetEncodePresetConfigEx() API through this pointer. */ + PNVENCGETSEQUENCEPARAMEX + nvEncGetSequenceParamEx; /**< [out]: Client should access ::NvEncGetSequenceParamEx() API through this pointer. */ + void* reserved2 + [277]; /**< [in]: Reserved and must be set to NULL */ +} NV_ENCODE_API_FUNCTION_LIST; + +/** Macro for constructing the version field of ::_NV_ENCODEAPI_FUNCTION_LIST. */ +#define NV_ENCODE_API_FUNCTION_LIST_VER NVENCAPI_STRUCT_VERSION(2) + +// NvEncodeAPICreateInstance +/** + * \ingroup ENCODE_FUNC + * Entry Point to the NvEncodeAPI interface. + * + * Creates an instance of the NvEncodeAPI interface, and populates the + * pFunctionList with function pointers to the API routines implemented by the + * NvEncodeAPI interface. + * + * \param [out] functionList + * + * \return + * ::NV_ENC_SUCCESS + * ::NV_ENC_ERR_INVALID_PTR + */ +NVENCSTATUS NVENCAPI +NvEncodeAPICreateInstance(NV_ENCODE_API_FUNCTION_LIST* functionList); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/NvCodec/include/nvcuvid.h b/NvCodec/include/nvcuvid.h index 9678541c..3f306a90 100644 --- a/NvCodec/include/nvcuvid.h +++ b/NvCodec/include/nvcuvid.h @@ -1,392 +1,551 @@ -/* - * This copyright notice applies to this header file only: - * - * Copyright (c) 2010-2020 NVIDIA Corporation - * - * Permission is hereby granted, free of charge, to any person - * obtaining a copy of this software and associated documentation - * files (the "Software"), to deal in the Software without - * restriction, including without limitation the rights to use, - * copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the software, and to permit persons to whom the - * software is furnished to do so, subject to the following - * conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES - * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT - * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, - * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - * OTHER DEALINGS IN THE SOFTWARE. - */ - -/********************************************************************************************************************/ -//! \file nvcuvid.h -//! NVDECODE API provides video decoding interface to NVIDIA GPU devices. -//! \date 2015-2019 -//! This file contains the interface constants, structure definitions and function prototypes. -/********************************************************************************************************************/ - -#if !defined(__NVCUVID_H__) -#define __NVCUVID_H__ - -#include "cuviddec.h" - -#if defined(__cplusplus) -extern "C" { -#endif /* __cplusplus */ - - -/***********************************************/ -//! -//! High-level helper APIs for video sources -//! -/***********************************************/ - -typedef void *CUvideosource; -typedef void *CUvideoparser; -typedef long long CUvideotimestamp; - - -/************************************************************************/ -//! \enum cudaVideoState -//! Video source state enums -//! Used in cuvidSetVideoSourceState and cuvidGetVideoSourceState APIs -/************************************************************************/ -typedef enum { - cudaVideoState_Error = -1, /**< Error state (invalid source) */ - cudaVideoState_Stopped = 0, /**< Source is stopped (or reached end-of-stream) */ - cudaVideoState_Started = 1 /**< Source is running and delivering data */ -} cudaVideoState; - -/************************************************************************/ -//! \enum cudaAudioCodec -//! Audio compression enums -//! Used in CUAUDIOFORMAT structure -/************************************************************************/ -typedef enum { - cudaAudioCodec_MPEG1=0, /**< MPEG-1 Audio */ - cudaAudioCodec_MPEG2, /**< MPEG-2 Audio */ - cudaAudioCodec_MP3, /**< MPEG-1 Layer III Audio */ - cudaAudioCodec_AC3, /**< Dolby Digital (AC3) Audio */ - cudaAudioCodec_LPCM, /**< PCM Audio */ - cudaAudioCodec_AAC, /**< AAC Audio */ -} cudaAudioCodec; - -/************************************************************************************************/ -//! \ingroup STRUCTS -//! \struct CUVIDEOFORMAT -//! Video format -//! Used in cuvidGetSourceVideoFormat API -/************************************************************************************************/ -typedef struct -{ - cudaVideoCodec codec; /**< OUT: Compression format */ - /** - * OUT: frame rate = numerator / denominator (for example: 30000/1001) - */ - struct { - /**< OUT: frame rate numerator (0 = unspecified or variable frame rate) */ - unsigned int numerator; - /**< OUT: frame rate denominator (0 = unspecified or variable frame rate) */ - unsigned int denominator; - } frame_rate; - unsigned char progressive_sequence; /**< OUT: 0=interlaced, 1=progressive */ - unsigned char bit_depth_luma_minus8; /**< OUT: high bit depth luma. E.g, 2 for 10-bitdepth, 4 for 12-bitdepth */ - unsigned char bit_depth_chroma_minus8; /**< OUT: high bit depth chroma. E.g, 2 for 10-bitdepth, 4 for 12-bitdepth */ - unsigned char min_num_decode_surfaces; /**< OUT: Minimum number of decode surfaces to be allocated for correct - decoding. The client can send this value in ulNumDecodeSurfaces - (in CUVIDDECODECREATEINFO structure). - This guarantees correct functionality and optimal video memory - usage but not necessarily the best performance, which depends on - the design of the overall application. The optimal number of - decode surfaces (in terms of performance and memory utilization) - should be decided by experimentation for each application, but it - cannot go below min_num_decode_surfaces. - If this value is used for ulNumDecodeSurfaces then it must be - returned to parser during sequence callback. */ - unsigned int coded_width; /**< OUT: coded frame width in pixels */ - unsigned int coded_height; /**< OUT: coded frame height in pixels */ - /** - * area of the frame that should be displayed - * typical example: - * coded_width = 1920, coded_height = 1088 - * display_area = { 0,0,1920,1080 } - */ - struct { - int left; /**< OUT: left position of display rect */ - int top; /**< OUT: top position of display rect */ - int right; /**< OUT: right position of display rect */ - int bottom; /**< OUT: bottom position of display rect */ - } display_area; - cudaVideoChromaFormat chroma_format; /**< OUT: Chroma format */ - unsigned int bitrate; /**< OUT: video bitrate (bps, 0=unknown) */ - /** - * OUT: Display Aspect Ratio = x:y (4:3, 16:9, etc) - */ - struct { - int x; - int y; - } display_aspect_ratio; - /** - * Video Signal Description - * Refer section E.2.1 (VUI parameters semantics) of H264 spec file - */ - struct { - unsigned char video_format : 3; /**< OUT: 0-Component, 1-PAL, 2-NTSC, 3-SECAM, 4-MAC, 5-Unspecified */ - unsigned char video_full_range_flag : 1; /**< OUT: indicates the black level and luma and chroma range */ - unsigned char reserved_zero_bits : 4; /**< Reserved bits */ - unsigned char color_primaries; /**< OUT: chromaticity coordinates of source primaries */ - unsigned char transfer_characteristics; /**< OUT: opto-electronic transfer characteristic of the source picture */ - unsigned char matrix_coefficients; /**< OUT: used in deriving luma and chroma signals from RGB primaries */ - } video_signal_description; - unsigned int seqhdr_data_length; /**< OUT: Additional bytes following (CUVIDEOFORMATEX) */ -} CUVIDEOFORMAT; - -/****************************************************************/ -//! \ingroup STRUCTS -//! \struct CUVIDEOFORMATEX -//! Video format including raw sequence header information -//! Used in cuvidGetSourceVideoFormat API -/****************************************************************/ -typedef struct -{ - CUVIDEOFORMAT format; /**< OUT: CUVIDEOFORMAT structure */ - unsigned char raw_seqhdr_data[1024]; /**< OUT: Sequence header data */ -} CUVIDEOFORMATEX; - -/****************************************************************/ -//! \ingroup STRUCTS -//! \struct CUAUDIOFORMAT -//! Audio formats -//! Used in cuvidGetSourceAudioFormat API -/****************************************************************/ -typedef struct -{ - cudaAudioCodec codec; /**< OUT: Compression format */ - unsigned int channels; /**< OUT: number of audio channels */ - unsigned int samplespersec; /**< OUT: sampling frequency */ - unsigned int bitrate; /**< OUT: For uncompressed, can also be used to determine bits per sample */ - unsigned int reserved1; /**< Reserved for future use */ - unsigned int reserved2; /**< Reserved for future use */ -} CUAUDIOFORMAT; - - -/***************************************************************/ -//! \enum CUvideopacketflags -//! Data packet flags -//! Used in CUVIDSOURCEDATAPACKET structure -/***************************************************************/ -typedef enum { - CUVID_PKT_ENDOFSTREAM = 0x01, /**< Set when this is the last packet for this stream */ - CUVID_PKT_TIMESTAMP = 0x02, /**< Timestamp is valid */ - CUVID_PKT_DISCONTINUITY = 0x04, /**< Set when a discontinuity has to be signalled */ - CUVID_PKT_ENDOFPICTURE = 0x08, /**< Set when the packet contains exactly one frame or one field */ - CUVID_PKT_NOTIFY_EOS = 0x10, /**< If this flag is set along with CUVID_PKT_ENDOFSTREAM, an additional (dummy) - display callback will be invoked with null value of CUVIDPARSERDISPINFO which - should be interpreted as end of the stream. */ -} CUvideopacketflags; - -/*****************************************************************************/ -//! \ingroup STRUCTS -//! \struct CUVIDSOURCEDATAPACKET -//! Data Packet -//! Used in cuvidParseVideoData API -//! IN for cuvidParseVideoData -/*****************************************************************************/ -typedef struct _CUVIDSOURCEDATAPACKET -{ - unsigned long flags; /**< IN: Combination of CUVID_PKT_XXX flags */ - unsigned long payload_size; /**< IN: number of bytes in the payload (may be zero if EOS flag is set) */ - const unsigned char *payload; /**< IN: Pointer to packet payload data (may be NULL if EOS flag is set) */ - CUvideotimestamp timestamp; /**< IN: Presentation time stamp (10MHz clock), only valid if - CUVID_PKT_TIMESTAMP flag is set */ -} CUVIDSOURCEDATAPACKET; - -// Callback for packet delivery -typedef int (CUDAAPI *PFNVIDSOURCECALLBACK)(void *, CUVIDSOURCEDATAPACKET *); - -/**************************************************************************************************************************/ -//! \ingroup STRUCTS -//! \struct CUVIDSOURCEPARAMS -//! Describes parameters needed in cuvidCreateVideoSource API -//! NVDECODE API is intended for HW accelerated video decoding so CUvideosource doesn't have audio demuxer for all supported -//! containers. It's recommended to clients to use their own or third party demuxer if audio support is needed. -/**************************************************************************************************************************/ -typedef struct _CUVIDSOURCEPARAMS -{ - unsigned int ulClockRate; /**< IN: Time stamp units in Hz (0=default=10000000Hz) */ - unsigned int uReserved1[7]; /**< Reserved for future use - set to zero */ - void *pUserData; /**< IN: User private data passed in to the data handlers */ - PFNVIDSOURCECALLBACK pfnVideoDataHandler; /**< IN: Called to deliver video packets */ - PFNVIDSOURCECALLBACK pfnAudioDataHandler; /**< IN: Called to deliver audio packets. */ - void *pvReserved2[8]; /**< Reserved for future use - set to NULL */ -} CUVIDSOURCEPARAMS; - - -/**********************************************/ -//! \ingroup ENUMS -//! \enum CUvideosourceformat_flags -//! CUvideosourceformat_flags -//! Used in cuvidGetSourceVideoFormat API -/**********************************************/ -typedef enum { - CUVID_FMT_EXTFORMATINFO = 0x100 /**< Return extended format structure (CUVIDEOFORMATEX) */ -} CUvideosourceformat_flags; - -#if !defined(__APPLE__) -/***************************************************************************************************************************/ -//! \ingroup FUNCTS -//! \fn CUresult CUDAAPI cuvidCreateVideoSource(CUvideosource *pObj, const char *pszFileName, CUVIDSOURCEPARAMS *pParams) -//! Create CUvideosource object. CUvideosource spawns demultiplexer thread that provides two callbacks: -//! pfnVideoDataHandler() and pfnAudioDataHandler() -//! NVDECODE API is intended for HW accelerated video decoding so CUvideosource doesn't have audio demuxer for all supported -//! containers. It's recommended to clients to use their own or third party demuxer if audio support is needed. -/***************************************************************************************************************************/ -CUresult CUDAAPI cuvidCreateVideoSource(CUvideosource *pObj, const char *pszFileName, CUVIDSOURCEPARAMS *pParams); - -/***************************************************************************************************************************/ -//! \ingroup FUNCTS -//! \fn CUresult CUDAAPI cuvidCreateVideoSourceW(CUvideosource *pObj, const wchar_t *pwszFileName, CUVIDSOURCEPARAMS *pParams) -//! Create video source -/***************************************************************************************************************************/ -CUresult CUDAAPI cuvidCreateVideoSourceW(CUvideosource *pObj, const wchar_t *pwszFileName, CUVIDSOURCEPARAMS *pParams); - -/********************************************************************/ -//! \ingroup FUNCTS -//! \fn CUresult CUDAAPI cuvidDestroyVideoSource(CUvideosource obj) -//! Destroy video source -/********************************************************************/ -CUresult CUDAAPI cuvidDestroyVideoSource(CUvideosource obj); - -/******************************************************************************************/ -//! \ingroup FUNCTS -//! \fn CUresult CUDAAPI cuvidSetVideoSourceState(CUvideosource obj, cudaVideoState state) -//! Set video source state to: -//! cudaVideoState_Started - to signal the source to run and deliver data -//! cudaVideoState_Stopped - to stop the source from delivering the data -//! cudaVideoState_Error - invalid source -/******************************************************************************************/ -CUresult CUDAAPI cuvidSetVideoSourceState(CUvideosource obj, cudaVideoState state); - -/******************************************************************************************/ -//! \ingroup FUNCTS -//! \fn cudaVideoState CUDAAPI cuvidGetVideoSourceState(CUvideosource obj) -//! Get video source state -//! Returns: -//! cudaVideoState_Started - if Source is running and delivering data -//! cudaVideoState_Stopped - if Source is stopped or reached end-of-stream -//! cudaVideoState_Error - if Source is in error state -/******************************************************************************************/ -cudaVideoState CUDAAPI cuvidGetVideoSourceState(CUvideosource obj); - -/******************************************************************************************************************/ -//! \ingroup FUNCTS -//! \fn CUresult CUDAAPI cuvidGetSourceVideoFormat(CUvideosource obj, CUVIDEOFORMAT *pvidfmt, unsigned int flags) -//! Gets video source format in pvidfmt, flags is set to combination of CUvideosourceformat_flags as per requirement -/******************************************************************************************************************/ -CUresult CUDAAPI cuvidGetSourceVideoFormat(CUvideosource obj, CUVIDEOFORMAT *pvidfmt, unsigned int flags); - -/**************************************************************************************************************************/ -//! \ingroup FUNCTS -//! \fn CUresult CUDAAPI cuvidGetSourceAudioFormat(CUvideosource obj, CUAUDIOFORMAT *paudfmt, unsigned int flags) -//! Get audio source format -//! NVDECODE API is intended for HW accelerated video decoding so CUvideosource doesn't have audio demuxer for all supported -//! containers. It's recommended to clients to use their own or third party demuxer if audio support is needed. -/**************************************************************************************************************************/ -CUresult CUDAAPI cuvidGetSourceAudioFormat(CUvideosource obj, CUAUDIOFORMAT *paudfmt, unsigned int flags); - -#endif -/**********************************************************************************/ -//! \ingroup STRUCTS -//! \struct CUVIDPARSERDISPINFO -//! Used in cuvidParseVideoData API with PFNVIDDISPLAYCALLBACK pfnDisplayPicture -/**********************************************************************************/ -typedef struct _CUVIDPARSERDISPINFO -{ - int picture_index; /**< OUT: Index of the current picture */ - int progressive_frame; /**< OUT: 1 if progressive frame; 0 otherwise */ - int top_field_first; /**< OUT: 1 if top field is displayed first; 0 otherwise */ - int repeat_first_field; /**< OUT: Number of additional fields (1=ivtc, 2=frame doubling, 4=frame tripling, - -1=unpaired field) */ - CUvideotimestamp timestamp; /**< OUT: Presentation time stamp */ -} CUVIDPARSERDISPINFO; - -/***********************************************************************************************************************/ -//! Parser callbacks -//! The parser will call these synchronously from within cuvidParseVideoData(), whenever there is sequence change or a picture -//! is ready to be decoded and/or displayed. First argument in functions is "void *pUserData" member of structure CUVIDSOURCEPARAMS -//! Return values from these callbacks are interpreted as below. If the callbacks return failure, it will be propagated by -//! cuvidParseVideoData() to the application. -//! PFNVIDSEQUENCECALLBACK : 0: fail, 1: succeeded, > 1: override dpb size of parser (set by CUVIDPARSERPARAMS::ulMaxNumDecodeSurfaces -//! while creating parser) -//! PFNVIDDECODECALLBACK : 0: fail, >=1: succeeded -//! PFNVIDDISPLAYCALLBACK : 0: fail, >=1: succeeded -/***********************************************************************************************************************/ -typedef int (CUDAAPI *PFNVIDSEQUENCECALLBACK)(void *, CUVIDEOFORMAT *); -typedef int (CUDAAPI *PFNVIDDECODECALLBACK)(void *, CUVIDPICPARAMS *); -typedef int (CUDAAPI *PFNVIDDISPLAYCALLBACK)(void *, CUVIDPARSERDISPINFO *); - -/**************************************/ -//! \ingroup STRUCTS -//! \struct CUVIDPARSERPARAMS -//! Used in cuvidCreateVideoParser API -/**************************************/ -typedef struct _CUVIDPARSERPARAMS -{ - cudaVideoCodec CodecType; /**< IN: cudaVideoCodec_XXX */ - unsigned int ulMaxNumDecodeSurfaces; /**< IN: Max # of decode surfaces (parser will cycle through these) */ - unsigned int ulClockRate; /**< IN: Timestamp units in Hz (0=default=10000000Hz) */ - unsigned int ulErrorThreshold; /**< IN: % Error threshold (0-100) for calling pfnDecodePicture (100=always - IN: call pfnDecodePicture even if picture bitstream is fully corrupted) */ - unsigned int ulMaxDisplayDelay; /**< IN: Max display queue delay (improves pipelining of decode with display) - 0=no delay (recommended values: 2..4) */ - unsigned int uReserved1[5]; /**< IN: Reserved for future use - set to 0 */ - void *pUserData; /**< IN: User data for callbacks */ - PFNVIDSEQUENCECALLBACK pfnSequenceCallback; /**< IN: Called before decoding frames and/or whenever there is a fmt change */ - PFNVIDDECODECALLBACK pfnDecodePicture; /**< IN: Called when a picture is ready to be decoded (decode order) */ - PFNVIDDISPLAYCALLBACK pfnDisplayPicture; /**< IN: Called whenever a picture is ready to be displayed (display order) */ - void *pvReserved2[7]; /**< Reserved for future use - set to NULL */ - CUVIDEOFORMATEX *pExtVideoInfo; /**< IN: [Optional] sequence header data from system layer */ -} CUVIDPARSERPARAMS; - -/************************************************************************************************/ -//! \ingroup FUNCTS -//! \fn CUresult CUDAAPI cuvidCreateVideoParser(CUvideoparser *pObj, CUVIDPARSERPARAMS *pParams) -//! Create video parser object and initialize -/************************************************************************************************/ -CUresult CUDAAPI cuvidCreateVideoParser(CUvideoparser *pObj, CUVIDPARSERPARAMS *pParams); - -/************************************************************************************************/ -//! \ingroup FUNCTS -//! \fn CUresult CUDAAPI cuvidParseVideoData(CUvideoparser obj, CUVIDSOURCEDATAPACKET *pPacket) -//! Parse the video data from source data packet in pPacket -//! Extracts parameter sets like SPS, PPS, bitstream etc. from pPacket and -//! calls back pfnDecodePicture with CUVIDPICPARAMS data for kicking of HW decoding -//! calls back pfnSequenceCallback with CUVIDEOFORMAT data for initial sequence header or when -//! the decoder encounters a video format change -//! calls back pfnDisplayPicture with CUVIDPARSERDISPINFO data to display a video frame -/************************************************************************************************/ -CUresult CUDAAPI cuvidParseVideoData(CUvideoparser obj, CUVIDSOURCEDATAPACKET *pPacket); - -/************************************************************************************************/ -//! \ingroup FUNCTS -//! \fn CUresult CUDAAPI cuvidDestroyVideoParser(CUvideoparser obj) -//! Destroy the video parser -/************************************************************************************************/ -CUresult CUDAAPI cuvidDestroyVideoParser(CUvideoparser obj); - -/**********************************************************************************************/ - -#if defined(__cplusplus) -} -#endif /* __cplusplus */ - -#endif // __NVCUVID_H__ - - +/* + * This copyright notice applies to this header file only: + * + * Copyright (c) 2010-2022 NVIDIA Corporation + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the software, and to permit persons to whom the + * software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +/********************************************************************************************************************/ +//! \file nvcuvid.h +//! NVDECODE API provides video decoding interface to NVIDIA GPU devices. +//! \date 2015-2022 +//! This file contains the interface constants, structure definitions and function prototypes. +/********************************************************************************************************************/ + +#if !defined(__NVCUVID_H__) +#define __NVCUVID_H__ + +#include "cuviddec.h" + +#if defined(__cplusplus) +extern "C" { +#endif /* __cplusplus */ + +#define MAX_CLOCK_TS 3 + +/***********************************************/ +//! +//! High-level helper APIs for video sources +//! +/***********************************************/ + +typedef void* CUvideosource; +typedef void* CUvideoparser; +typedef long long CUvideotimestamp; + +/************************************************************************/ +//! \enum cudaVideoState +//! Video source state enums +//! Used in cuvidSetVideoSourceState and cuvidGetVideoSourceState APIs +/************************************************************************/ +typedef enum { + cudaVideoState_Error = + -1, /**< Error state (invalid source) */ + cudaVideoState_Stopped = + 0, /**< Source is stopped (or reached end-of-stream) */ + cudaVideoState_Started = + 1 /**< Source is running and delivering data */ +} cudaVideoState; + +/************************************************************************/ +//! \enum cudaAudioCodec +//! Audio compression enums +//! Used in CUAUDIOFORMAT structure +/************************************************************************/ +typedef enum { + cudaAudioCodec_MPEG1 = 0, /**< MPEG-1 Audio */ + cudaAudioCodec_MPEG2, /**< MPEG-2 Audio */ + cudaAudioCodec_MP3, /**< MPEG-1 Layer III Audio */ + cudaAudioCodec_AC3, /**< Dolby Digital (AC3) Audio */ + cudaAudioCodec_LPCM, /**< PCM Audio */ + cudaAudioCodec_AAC, /**< AAC Audio */ +} cudaAudioCodec; + +/************************************************************************/ +//! \ingroup STRUCTS +//! \struct HEVCTIMECODESET +//! Used to store Time code extracted from Time code SEI in HEVC codec +/************************************************************************/ +typedef struct _HEVCTIMECODESET { + unsigned int time_offset_value; + unsigned short n_frames; + unsigned char clock_timestamp_flag; + unsigned char units_field_based_flag; + unsigned char counting_type; + unsigned char full_timestamp_flag; + unsigned char discontinuity_flag; + unsigned char cnt_dropped_flag; + unsigned char seconds_value; + unsigned char minutes_value; + unsigned char hours_value; + unsigned char seconds_flag; + unsigned char minutes_flag; + unsigned char hours_flag; + unsigned char time_offset_length; + unsigned char reserved; +} HEVCTIMECODESET; + +/************************************************************************/ +//! \ingroup STRUCTS +//! \struct HEVCSEITIMECODE +//! Used to extract Time code SEI in HEVC codec +/************************************************************************/ +typedef struct _HEVCSEITIMECODE { + HEVCTIMECODESET time_code_set[MAX_CLOCK_TS]; + unsigned char num_clock_ts; +} HEVCSEITIMECODE; + +/**********************************************************************************/ +//! \ingroup STRUCTS +//! \struct CUSEIMESSAGE; +//! Used in CUVIDSEIMESSAGEINFO structure +/**********************************************************************************/ +typedef struct _CUSEIMESSAGE { + unsigned char sei_message_type; /**< OUT: SEI Message Type */ + unsigned char reserved[3]; + unsigned int sei_message_size; /**< OUT: SEI Message Size */ +} CUSEIMESSAGE; + +/************************************************************************************************/ +//! \ingroup STRUCTS +//! \struct CUVIDEOFORMAT +//! Video format +//! Used in cuvidGetSourceVideoFormat API +/************************************************************************************************/ +typedef struct { + cudaVideoCodec codec; /**< OUT: Compression format */ + /** + * OUT: frame rate = numerator / denominator (for example: 30000/1001) + */ + struct { + /**< OUT: frame rate numerator (0 = unspecified or variable frame rate) */ + unsigned int numerator; + /**< OUT: frame rate denominator (0 = unspecified or variable frame rate) */ + unsigned int denominator; + } frame_rate; + unsigned char + progressive_sequence; /**< OUT: 0=interlaced, 1=progressive */ + unsigned char + bit_depth_luma_minus8; /**< OUT: high bit depth luma. E.g, 2 for 10-bitdepth, 4 for 12-bitdepth */ + unsigned char + bit_depth_chroma_minus8; /**< OUT: high bit depth chroma. E.g, 2 for 10-bitdepth, 4 for 12-bitdepth */ + unsigned char + min_num_decode_surfaces; /**< OUT: Minimum number of decode surfaces to be allocated for correct + decoding. The client can send this value in ulNumDecodeSurfaces + (in CUVIDDECODECREATEINFO structure). + This guarantees correct functionality and optimal video memory + usage but not necessarily the best performance, which depends on + the design of the overall application. The optimal number of + decode surfaces (in terms of performance and memory utilization) + should be decided by experimentation for each application, but it + cannot go below min_num_decode_surfaces. + If this value is used for ulNumDecodeSurfaces then it must be + returned to parser during sequence callback. */ + unsigned int + coded_width; /**< OUT: coded frame width in pixels */ + unsigned int + coded_height; /**< OUT: coded frame height in pixels */ + /** + * area of the frame that should be displayed + * typical example: + * coded_width = 1920, coded_height = 1088 + * display_area = { 0,0,1920,1080 } + */ + struct { + int left; /**< OUT: left position of display rect */ + int top; /**< OUT: top position of display rect */ + int right; /**< OUT: right position of display rect */ + int bottom; /**< OUT: bottom position of display rect */ + } display_area; + cudaVideoChromaFormat + chroma_format; /**< OUT: Chroma format */ + unsigned int bitrate; /**< OUT: video bitrate (bps, 0=unknown) */ + /** + * OUT: Display Aspect Ratio = x:y (4:3, 16:9, etc) + */ + struct { + int x; + int y; + } display_aspect_ratio; + /** + * Video Signal Description + * Refer section E.2.1 (VUI parameters semantics) of H264 spec file + */ + struct { + unsigned char + video_format : 3; /**< OUT: 0-Component, 1-PAL, 2-NTSC, 3-SECAM, 4-MAC, 5-Unspecified */ + unsigned char + video_full_range_flag : 1; /**< OUT: indicates the black level and luma and chroma range */ + unsigned char + reserved_zero_bits : 4; /**< Reserved bits */ + unsigned char + color_primaries; /**< OUT: chromaticity coordinates of source primaries */ + unsigned char + transfer_characteristics; /**< OUT: opto-electronic transfer characteristic of the source picture */ + unsigned char + matrix_coefficients; /**< OUT: used in deriving luma and chroma signals from RGB primaries */ + } video_signal_description; + unsigned int + seqhdr_data_length; /**< OUT: Additional bytes following (CUVIDEOFORMATEX) */ +} CUVIDEOFORMAT; + +/****************************************************************/ +//! \ingroup STRUCTS +//! \struct CUVIDOPERATINGPOINTINFO +//! Operating point information of scalable bitstream +/****************************************************************/ +typedef struct { + cudaVideoCodec codec; + union { + struct { + unsigned char operating_points_cnt; + unsigned char reserved24_bits[3]; + unsigned short operating_points_idc[32]; + } av1; + unsigned char CodecReserved[1024]; + }; +} CUVIDOPERATINGPOINTINFO; + +/**********************************************************************************/ +//! \ingroup STRUCTS +//! \struct CUVIDSEIMESSAGEINFO +//! Used in cuvidParseVideoData API with PFNVIDSEIMSGCALLBACK pfnGetSEIMsg +/**********************************************************************************/ +typedef struct _CUVIDSEIMESSAGEINFO { + void* pSEIData; /**< OUT: SEI Message Data */ + CUSEIMESSAGE* pSEIMessage; /**< OUT: SEI Message Info */ + unsigned int sei_message_count; /**< OUT: SEI Message Count */ + unsigned int picIdx; /**< OUT: SEI Message Pic Index */ +} CUVIDSEIMESSAGEINFO; + +/****************************************************************/ +//! \ingroup STRUCTS +//! \struct CUVIDAV1SEQHDR +//! AV1 specific sequence header information +/****************************************************************/ +typedef struct { + unsigned int max_width; + unsigned int max_height; + unsigned char reserved[1016]; +} CUVIDAV1SEQHDR; + +/****************************************************************/ +//! \ingroup STRUCTS +//! \struct CUVIDEOFORMATEX +//! Video format including raw sequence header information +//! Used in cuvidGetSourceVideoFormat API +/****************************************************************/ +typedef struct { + CUVIDEOFORMAT format; /**< OUT: CUVIDEOFORMAT structure */ + union { + CUVIDAV1SEQHDR av1; + unsigned char raw_seqhdr_data[1024]; /**< OUT: Sequence header data */ + }; +} CUVIDEOFORMATEX; + +/****************************************************************/ +//! \ingroup STRUCTS +//! \struct CUAUDIOFORMAT +//! Audio formats +//! Used in cuvidGetSourceAudioFormat API +/****************************************************************/ +typedef struct { + cudaAudioCodec + codec; /**< OUT: Compression format */ + unsigned int + channels; /**< OUT: number of audio channels */ + unsigned int + samplespersec; /**< OUT: sampling frequency */ + unsigned int + bitrate; /**< OUT: For uncompressed, can also be used to determine bits per sample */ + unsigned int + reserved1; /**< Reserved for future use */ + unsigned int + reserved2; /**< Reserved for future use */ +} CUAUDIOFORMAT; + +/***************************************************************/ +//! \enum CUvideopacketflags +//! Data packet flags +//! Used in CUVIDSOURCEDATAPACKET structure +/***************************************************************/ +typedef enum { + CUVID_PKT_ENDOFSTREAM = + 0x01, /**< Set when this is the last packet for this stream */ + CUVID_PKT_TIMESTAMP = + 0x02, /**< Timestamp is valid */ + CUVID_PKT_DISCONTINUITY = + 0x04, /**< Set when a discontinuity has to be signalled */ + CUVID_PKT_ENDOFPICTURE = + 0x08, /**< Set when the packet contains exactly one frame or one field */ + CUVID_PKT_NOTIFY_EOS = + 0x10, /**< If this flag is set along with CUVID_PKT_ENDOFSTREAM, an additional (dummy) + display callback will be invoked with null value of CUVIDPARSERDISPINFO which + should be interpreted as end of the stream. */ +} CUvideopacketflags; + +/*****************************************************************************/ +//! \ingroup STRUCTS +//! \struct CUVIDSOURCEDATAPACKET +//! Data Packet +//! Used in cuvidParseVideoData API +//! IN for cuvidParseVideoData +/*****************************************************************************/ +typedef struct _CUVIDSOURCEDATAPACKET { + unsigned long + flags; /**< IN: Combination of CUVID_PKT_XXX flags */ + unsigned long + payload_size; /**< IN: number of bytes in the payload (may be zero if EOS flag is set) */ + const unsigned char* + payload; /**< IN: Pointer to packet payload data (may be NULL if EOS flag is set) */ + CUvideotimestamp + timestamp; /**< IN: Presentation time stamp (10MHz clock), only valid if + CUVID_PKT_TIMESTAMP flag is set */ +} CUVIDSOURCEDATAPACKET; + +// Callback for packet delivery +typedef int(CUDAAPI* PFNVIDSOURCECALLBACK)(void*, CUVIDSOURCEDATAPACKET*); + +/**************************************************************************************************************************/ +//! \ingroup STRUCTS +//! \struct CUVIDSOURCEPARAMS +//! Describes parameters needed in cuvidCreateVideoSource API +//! NVDECODE API is intended for HW accelerated video decoding so CUvideosource doesn't have audio demuxer for all supported +//! containers. It's recommended to clients to use their own or third party demuxer if audio support is needed. +/**************************************************************************************************************************/ +typedef struct _CUVIDSOURCEPARAMS { + unsigned int + ulClockRate; /**< IN: Time stamp units in Hz (0=default=10000000Hz) */ + unsigned int + bAnnexb : 1; /**< IN: AV1 annexB stream */ + unsigned int + uReserved : 31; /**< Reserved for future use - set to zero */ + unsigned int uReserved1 + [6]; /**< Reserved for future use - set to zero */ + void* + pUserData; /**< IN: User private data passed in to the data handlers */ + PFNVIDSOURCECALLBACK + pfnVideoDataHandler; /**< IN: Called to deliver video packets */ + PFNVIDSOURCECALLBACK + pfnAudioDataHandler; /**< IN: Called to deliver audio packets. */ + void* pvReserved2 + [8]; /**< Reserved for future use - set to NULL */ +} CUVIDSOURCEPARAMS; + +/**********************************************/ +//! \ingroup ENUMS +//! \enum CUvideosourceformat_flags +//! CUvideosourceformat_flags +//! Used in cuvidGetSourceVideoFormat API +/**********************************************/ +typedef enum { + CUVID_FMT_EXTFORMATINFO = + 0x100 /**< Return extended format structure (CUVIDEOFORMATEX) */ +} CUvideosourceformat_flags; + +#if !defined(__APPLE__) +/***************************************************************************************************************************/ +//! \ingroup FUNCTS +//! \fn CUresult CUDAAPI cuvidCreateVideoSource(CUvideosource *pObj, const char *pszFileName, CUVIDSOURCEPARAMS *pParams) +//! Create CUvideosource object. CUvideosource spawns demultiplexer thread that provides two callbacks: +//! pfnVideoDataHandler() and pfnAudioDataHandler() +//! NVDECODE API is intended for HW accelerated video decoding so CUvideosource doesn't have audio demuxer for all supported +//! containers. It's recommended to clients to use their own or third party demuxer if audio support is needed. +/***************************************************************************************************************************/ +CUresult CUDAAPI cuvidCreateVideoSource(CUvideosource* pObj, + const char* pszFileName, + CUVIDSOURCEPARAMS* pParams); + +/***************************************************************************************************************************/ +//! \ingroup FUNCTS +//! \fn CUresult CUDAAPI cuvidCreateVideoSourceW(CUvideosource *pObj, const wchar_t *pwszFileName, CUVIDSOURCEPARAMS *pParams) +//! Create video source +/***************************************************************************************************************************/ +CUresult CUDAAPI cuvidCreateVideoSourceW(CUvideosource* pObj, + const wchar_t* pwszFileName, + CUVIDSOURCEPARAMS* pParams); + +/********************************************************************/ +//! \ingroup FUNCTS +//! \fn CUresult CUDAAPI cuvidDestroyVideoSource(CUvideosource obj) +//! Destroy video source +/********************************************************************/ +CUresult CUDAAPI cuvidDestroyVideoSource(CUvideosource obj); + +/******************************************************************************************/ +//! \ingroup FUNCTS +//! \fn CUresult CUDAAPI cuvidSetVideoSourceState(CUvideosource obj, cudaVideoState state) +//! Set video source state to: +//! cudaVideoState_Started - to signal the source to run and deliver data +//! cudaVideoState_Stopped - to stop the source from delivering the data +//! cudaVideoState_Error - invalid source +/******************************************************************************************/ +CUresult CUDAAPI cuvidSetVideoSourceState(CUvideosource obj, + cudaVideoState state); + +/******************************************************************************************/ +//! \ingroup FUNCTS +//! \fn cudaVideoState CUDAAPI cuvidGetVideoSourceState(CUvideosource obj) +//! Get video source state +//! Returns: +//! cudaVideoState_Started - if Source is running and delivering data +//! cudaVideoState_Stopped - if Source is stopped or reached end-of-stream +//! cudaVideoState_Error - if Source is in error state +/******************************************************************************************/ +cudaVideoState CUDAAPI cuvidGetVideoSourceState(CUvideosource obj); + +/******************************************************************************************************************/ +//! \ingroup FUNCTS +//! \fn CUresult CUDAAPI cuvidGetSourceVideoFormat(CUvideosource obj, CUVIDEOFORMAT *pvidfmt, unsigned int flags) +//! Gets video source format in pvidfmt, flags is set to combination of CUvideosourceformat_flags as per requirement +/******************************************************************************************************************/ +CUresult CUDAAPI cuvidGetSourceVideoFormat(CUvideosource obj, + CUVIDEOFORMAT* pvidfmt, + unsigned int flags); + +/**************************************************************************************************************************/ +//! \ingroup FUNCTS +//! \fn CUresult CUDAAPI cuvidGetSourceAudioFormat(CUvideosource obj, CUAUDIOFORMAT *paudfmt, unsigned int flags) +//! Get audio source format +//! NVDECODE API is intended for HW accelerated video decoding so CUvideosource doesn't have audio demuxer for all supported +//! containers. It's recommended to clients to use their own or third party demuxer if audio support is needed. +/**************************************************************************************************************************/ +CUresult CUDAAPI cuvidGetSourceAudioFormat(CUvideosource obj, + CUAUDIOFORMAT* paudfmt, + unsigned int flags); + +#endif +/**********************************************************************************/ +//! \ingroup STRUCTS +//! \struct CUVIDPARSERDISPINFO +//! Used in cuvidParseVideoData API with PFNVIDDISPLAYCALLBACK pfnDisplayPicture +/**********************************************************************************/ +typedef struct _CUVIDPARSERDISPINFO { + int picture_index; /**< OUT: Index of the current picture */ + int progressive_frame; /**< OUT: 1 if progressive frame; 0 otherwise */ + int top_field_first; /**< OUT: 1 if top field is displayed first; 0 otherwise */ + int repeat_first_field; /**< OUT: Number of additional fields (1=ivtc, 2=frame doubling, 4=frame tripling, + -1=unpaired field) */ + CUvideotimestamp + timestamp; /**< OUT: Presentation time stamp */ +} CUVIDPARSERDISPINFO; + +/***********************************************************************************************************************/ +//! Parser callbacks +//! The parser will call these synchronously from within cuvidParseVideoData(), whenever there is sequence change or a picture +//! is ready to be decoded and/or displayed. First argument in functions is "void *pUserData" member of structure CUVIDSOURCEPARAMS +//! Return values from these callbacks are interpreted as below. If the callbacks return failure, it will be propagated by +//! cuvidParseVideoData() to the application. +//! Parser picks default operating point as 0 and outputAllLayers flag as 0 if PFNVIDOPPOINTCALLBACK is not set or return value is +//! -1 or invalid operating point. +//! PFNVIDSEQUENCECALLBACK : 0: fail, 1: succeeded, > 1: override dpb size of parser (set by CUVIDPARSERPARAMS::ulMaxNumDecodeSurfaces +//! while creating parser) +//! PFNVIDDECODECALLBACK : 0: fail, >=1: succeeded +//! PFNVIDDISPLAYCALLBACK : 0: fail, >=1: succeeded +//! PFNVIDOPPOINTCALLBACK : <0: fail, >=0: succeeded (bit 0-9: OperatingPoint, bit 10-10: outputAllLayers, bit 11-30: reserved) +//! PFNVIDSEIMSGCALLBACK : 0: fail, >=1: succeeded +/***********************************************************************************************************************/ +typedef int(CUDAAPI* PFNVIDSEQUENCECALLBACK)(void*, CUVIDEOFORMAT*); +typedef int(CUDAAPI* PFNVIDDECODECALLBACK)(void*, CUVIDPICPARAMS*); +typedef int(CUDAAPI* PFNVIDDISPLAYCALLBACK)(void*, CUVIDPARSERDISPINFO*); +typedef int(CUDAAPI* PFNVIDOPPOINTCALLBACK)(void*, CUVIDOPERATINGPOINTINFO*); +typedef int(CUDAAPI* PFNVIDSEIMSGCALLBACK)(void*, CUVIDSEIMESSAGEINFO*); + +/**************************************/ +//! \ingroup STRUCTS +//! \struct CUVIDPARSERPARAMS +//! Used in cuvidCreateVideoParser API +/**************************************/ +typedef struct _CUVIDPARSERPARAMS { + cudaVideoCodec + CodecType; /**< IN: cudaVideoCodec_XXX */ + unsigned int + ulMaxNumDecodeSurfaces; /**< IN: Max # of decode surfaces (parser will cycle through these) */ + unsigned int + ulClockRate; /**< IN: Timestamp units in Hz (0=default=10000000Hz) */ + unsigned int + ulErrorThreshold; /**< IN: % Error threshold (0-100) for calling pfnDecodePicture (100=always + IN: call pfnDecodePicture even if picture bitstream is fully corrupted) */ + unsigned int + ulMaxDisplayDelay; /**< IN: Max display queue delay (improves pipelining of decode with display) + 0=no delay (recommended values: 2..4) */ + unsigned int + bAnnexb : 1; /**< IN: AV1 annexB stream */ + unsigned int + uReserved : 31; /**< Reserved for future use - set to zero */ + unsigned int uReserved1 + [4]; /**< IN: Reserved for future use - set to 0 */ + void* + pUserData; /**< IN: User data for callbacks */ + PFNVIDSEQUENCECALLBACK + pfnSequenceCallback; /**< IN: Called before decoding frames and/or whenever there is a fmt change */ + PFNVIDDECODECALLBACK + pfnDecodePicture; /**< IN: Called when a picture is ready to be decoded (decode order) */ + PFNVIDDISPLAYCALLBACK + pfnDisplayPicture; /**< IN: Called whenever a picture is ready to be displayed (display order) */ + PFNVIDOPPOINTCALLBACK + pfnGetOperatingPoint; /**< IN: Called from AV1 sequence header to get operating point of a AV1 + scalable bitstream */ + PFNVIDSEIMSGCALLBACK + pfnGetSEIMsg; /**< IN: Called when all SEI messages are parsed for particular frame */ + void* pvReserved2 + [5]; /**< Reserved for future use - set to NULL */ + CUVIDEOFORMATEX* + pExtVideoInfo; /**< IN: [Optional] sequence header data from system layer */ +} CUVIDPARSERPARAMS; + +/************************************************************************************************/ +//! \ingroup FUNCTS +//! \fn CUresult CUDAAPI cuvidCreateVideoParser(CUvideoparser *pObj, CUVIDPARSERPARAMS *pParams) +//! Create video parser object and initialize +/************************************************************************************************/ +CUresult CUDAAPI cuvidCreateVideoParser(CUvideoparser* pObj, + CUVIDPARSERPARAMS* pParams); + +/************************************************************************************************/ +//! \ingroup FUNCTS +//! \fn CUresult CUDAAPI cuvidParseVideoData(CUvideoparser obj, CUVIDSOURCEDATAPACKET *pPacket) +//! Parse the video data from source data packet in pPacket +//! Extracts parameter sets like SPS, PPS, bitstream etc. from pPacket and +//! calls back pfnDecodePicture with CUVIDPICPARAMS data for kicking of HW decoding +//! calls back pfnSequenceCallback with CUVIDEOFORMAT data for initial sequence header or when +//! the decoder encounters a video format change +//! calls back pfnDisplayPicture with CUVIDPARSERDISPINFO data to display a video frame +/************************************************************************************************/ +CUresult CUDAAPI cuvidParseVideoData(CUvideoparser obj, + CUVIDSOURCEDATAPACKET* pPacket); + +/************************************************************************************************/ +//! \ingroup FUNCTS +//! \fn CUresult CUDAAPI cuvidDestroyVideoParser(CUvideoparser obj) +//! Destroy the video parser +/************************************************************************************************/ +CUresult CUDAAPI cuvidDestroyVideoParser(CUvideoparser obj); + +/**********************************************************************************************/ + +#if defined(__cplusplus) +} +#endif /* __cplusplus */ + +#endif // __NVCUVID_H__ diff --git a/README.en.md b/README.en.md index b3c46623..4d90c856 100644 --- a/README.en.md +++ b/README.en.md @@ -13,16 +13,7 @@ Please read https://github.com/shiguredo/oss before use. ## About WebRTC Native Client Momo Momo is a WebRTC Native Client that uses "libwebrtc" and runs on various browser-less devices. -When Momo runs on Jetson Nano, it is possible to deliver 4K video compressed with H.264 to the browser within 1 second. - -## Binary for Jetson Nano - -Download the latest version of the binary for Jetson Nano from below. -https://github.com/shiguredo/momo/releases - -``` -momo-_ubuntu-18.04_armv8_jetson_nano.tar.gz -``` +When Momo runs on Jetson Xavier NX / AGX, it is possible to deliver 4K video compressed with H.264 to the browser within 1 second. ## Binary for Jetson Xavier NX / AGX @@ -30,7 +21,7 @@ Download the latest version of the binary for Jetson Xavier NX / AGX from below. https://github.com/shiguredo/momo/releases ``` -momo-_ubuntu-18.04_armv8_jetson_xavier.tar.gz +momo-_ubuntu-20.04_armv8_jetson_xavier.tar.gz ``` ## Use "test mode" diff --git a/README.md b/README.md index 1f7bf7be..6032cbc5 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # WebRTC Native Client Momo -[![libwebrtc](https://img.shields.io/badge/libwebrtc-m107.5304-blue.svg)](https://chromium.googlesource.com/external/webrtc/+/branch-heads/5304) +[![libwebrtc](https://img.shields.io/badge/libwebrtc-m117.5938-blue.svg)](https://chromium.googlesource.com/external/webrtc/+/branch-heads/5938) [![GitHub tag (latest SemVer)](https://img.shields.io/github/tag/shiguredo/momo.svg)](https://github.com/shiguredo/momo) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![Actions Status](https://github.com/shiguredo/momo/workflows/daily-build-workflow/badge.svg)](https://github.com/shiguredo/momo/actions) @@ -52,6 +52,7 @@ AV1 の送受信に対応済みです。 ### クライアント証明書への対応 Momo は Sora モード利用時にクライアント証明書に対応しています。 + ## 動画 [WebRTC Native Client Momo と Jetson Nano で 4K@30 配信](https://www.youtube.com/watch?v=z05bWtsgDPY) @@ -94,11 +95,11 @@ https://github.com/shiguredo/momo/releases - Raspberry Pi Zero - Raspberry Pi 1 - Ubuntu 20.04 x86_64 -- Ubuntu 18.04 ARMv8 Jetson - - 2023 年 4 月末にて終了 - - [NVIDIA Jetson Nano](https://www.nvidia.com/ja-jp/autonomous-machines/embedded-systems/jetson-nano/) - - [NVIDIA Jetson Xavier NX](https://www.nvidia.com/ja-jp/autonomous-machines/embedded-systems/jetson-xavier-nx/) +- Ubuntu 22.04 x86_64 +- Ubuntu 20.04 ARMv8 Jetson + - [NVIDIA Jetson AGX Orin](https://www.nvidia.com/ja-jp/autonomous-machines/embedded-systems/jetson-orin/) - [NVIDIA Jetson AGX Xavier](https://www.nvidia.com/ja-jp/autonomous-machines/embedded-systems/jetson-agx-xavier/) + - [NVIDIA Jetson Xavier NX](https://www.nvidia.com/ja-jp/autonomous-machines/embedded-systems/jetson-xavier-nx/) - macOS 12 arm64 以降 - Windows 10.1809 x86_64 以降 @@ -106,11 +107,12 @@ https://github.com/shiguredo/momo/releases - macOS x86_64 - Ubuntu 20.04 ARMv8 Jetson - - [NVIDIA Jetson AGX Orin](https://www.nvidia.com/ja-jp/autonomous-machines/embedded-systems/jetson-orin/) - - [NVIDIA Jetson Orin NX](https://www.nvidia.com/ja-jp/autonomous-machines/embedded-systems/jetson-orin/) + - [NVIDIA Jetson Orin Nano](https://www.nvidia.com/ja-jp/autonomous-machines/embedded-systems/jetson-orin/) + - Jetson Orin Nano は HWA を詰んでいないので対応はしません +- Ubuntu 18.04 ARMv8 Jetson + - [NVIDIA Jetson Nano](https://www.nvidia.com/ja-jp/autonomous-machines/embedded-systems/jetson-nano/) - [NVIDIA Jetson Xavier NX](https://www.nvidia.com/ja-jp/autonomous-machines/embedded-systems/jetson-xavier-nx/) - [NVIDIA Jetson AGX Xavier](https://www.nvidia.com/ja-jp/autonomous-machines/embedded-systems/jetson-agx-xavier/) - - [NVIDIA Jetson Orin Nano](https://www.nvidia.com/ja-jp/autonomous-machines/embedded-systems/jetson-orin/) ## 使ってみる @@ -170,30 +172,11 @@ limitations under the License. **詳細は Discord やメールなどでお気軽にお問い合わせください** -- Jetson AGX Orin 対応 - - H.265 ハードウェアエンコーダー対応 - - H.265 ハードウェアデコーダー対応 -- Jetson AGX Xavier 対応 - - H.265 ハードウェアエンコーダー対応 - - H.265 ハードウェアデコーダー対応 -- Jetson Xavier NX 対応 - - H.265 ハードウェアエンコーダー対応 - - H.265 ハードウェアデコーダー対応 -- Raspberry Pi 4 - - H.265 ハードウェアデコーダー対応 +- oneVPL 対応 - AV1 対応 - Windows - 統計機能 - Ayame のシグナリング 経由での出力 -- NVIDIA VIDEO CODEC SDK - - H.265 ハードウェアエンコーダー対応 - - Ubuntu 20.04 - - Windows 10 - - H.265 ハードウェアデコーダー対応 - - Ubuntu 20.04 - - Windows 10 -- oneVPL 対応 - - H.265 ハードウェアエンコーダー対応 - 録画対応 - MP4 形式での出力 - WebM 形式での出力 @@ -252,3 +235,4 @@ H.264 ハードウェアエンコーダー **のみ** を利用している Momo - [NVIDIA Jetson Nano 搭載の H\.264/H\.265 ハードウェアエンコーダーのライセンスについて](https://medium.com/@voluntas/nvidia-jetson-nano-%E6%90%AD%E8%BC%89%E3%81%AE-h-264-h-265-%E3%83%8F%E3%83%BC%E3%83%89%E3%82%A6%E3%82%A7%E3%82%A2%E3%82%A8%E3%83%B3%E3%82%B3%E3%83%BC%E3%83%80%E3%81%AE%E3%83%A9%E3%82%A4%E3%82%BB%E3%83%B3%E3%82%B9%E3%81%AB%E3%81%A4%E3%81%84%E3%81%A6-ca207af302ee) - Intel Quick Sync Video のハードウェアエンコーダーライセンス費用は別途、団体との契約が必要 - [QuickSync \- H\.264 patent licensing fees \- Intel Community](https://community.intel.com/t5/Media-Intel-oneAPI-Video/QuickSync-H-264-patent-licensing-fees/td-p/921396) + diff --git a/VERSION b/VERSION index 5c195846..50f88400 100644 --- a/VERSION +++ b/VERSION @@ -1,9 +1,9 @@ -MOMO_VERSION=2022.4.1 -WEBRTC_BUILD_VERSION=107.5304.4.0 -BOOST_VERSION=1.80.0 -CLI11_VERSION=2.2.0 -SDL2_VERSION=2.24.1 -CMAKE_VERSION=3.24.2 +MOMO_VERSION=2023.1.0 +WEBRTC_BUILD_VERSION=117.5938.2.0 +BOOST_VERSION=1.83.0 +CLI11_VERSION=2.3.2 +SDL2_VERSION=2.28.3 +CMAKE_VERSION=3.27.6 CUDA_VERSION=11.0.2-1 WINCUDA_VERSION=10.2 LIBVA_VERSION=2.7.0 diff --git a/build/build.ps1 b/build/build.ps1 index 08f88277..55eb2da8 100644 --- a/build/build.ps1 +++ b/build/build.ps1 @@ -46,6 +46,7 @@ $MOMO_COMMIT = "$(git rev-parse HEAD)" mkdir "..\_build\windows_${WINDOWS_ARCH}" -Force -ErrorAction Ignore Push-Location "..\_build\windows_${WINDOWS_ARCH}" cmake ..\.. -G "Visual Studio 16 2019" ` + -DCMAKE_SYSTEM_VERSION=10.0.20348.0 ` -DMOMO_PACKAGE_NAME="windows_${WINDOWS_ARCH}" ` -DMOMO_VERSION="$MOMO_VERSION" ` -DMOMO_COMMIT="$MOMO_COMMIT" ` diff --git a/build/build.sh b/build/build.sh index 9aa237fd..f942087d 100755 --- a/build/build.sh +++ b/build/build.sh @@ -12,9 +12,9 @@ _PACKAGES=" \ raspberry-pi-os_armv6 \ raspberry-pi-os_armv7 \ raspberry-pi-os_armv8 \ - ubuntu-18.04_armv8_jetson_nano \ - ubuntu-18.04_armv8_jetson_xavier \ + ubuntu-20.04_armv8_jetson_xavier \ ubuntu-20.04_x86_64 \ + ubuntu-22.04_x86_64 \ " function show_help() { @@ -169,6 +169,8 @@ case "$PACKAGE" in rm -rf $PACKAGE/script cp -r ../script $PACKAGE/script + rm -rf $PACKAGE/patch + cp -r ../patch $PACKAGE/patch # 可能な限りキャッシュを利用する mkdir -p $PACKAGE/_cache/boost/ @@ -203,6 +205,7 @@ case "$PACKAGE" in set -e rm -r $PACKAGE/script + rm -r $PACKAGE/patch ../script/docker_run.sh `pwd` `pwd`/.. $DOCKER_MOUNT_TYPE $PACKAGE momo/$PACKAGE:m$WEBRTC_BUILD_VERSION $MOMO_COMMIT @@ -215,6 +218,9 @@ case "$PACKAGE" in cp LICENSE _package/momo-${MOMO_VERSION}_${PACKAGE}/ cp NOTICE _package/momo-${MOMO_VERSION}_${PACKAGE}/ cp -r html _package/momo-${MOMO_VERSION}_${PACKAGE}/html + if [ -e _build/${PACKAGE}/libcamerac.so ]; then + cp _build/${PACKAGE}/libcamerac.so _package/momo-${MOMO_VERSION}_${PACKAGE}/ + fi pushd _package tar czf momo-${MOMO_VERSION}_${PACKAGE}.tar.gz momo-${MOMO_VERSION}_${PACKAGE} popd diff --git a/build/raspberry-pi-os_armv8/Dockerfile b/build/raspberry-pi-os_armv8/Dockerfile index bc67030b..f29debf8 100644 --- a/build/raspberry-pi-os_armv8/Dockerfile +++ b/build/raspberry-pi-os_armv8/Dockerfile @@ -84,3 +84,68 @@ ARG CMAKE_VERSION COPY script/get_cmake.sh /root/ RUN /root/get_cmake.sh "$CMAKE_VERSION" linux /root ENV PATH "/root/cmake/bin:$PATH" + +# SDL2 のビルド + +ARG SDL2_VERSION + +COPY script/setup_sdl2.sh /root/ +RUN \ + set -ex \ + && /root/setup_sdl2.sh "$SDL2_VERSION" /root/sdl2-source \ + && mkdir -p /root/sdl2-source/build \ + && cd /root/sdl2-source/build \ + && cmake ../source \ + -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_INSTALL_PREFIX=/root/SDL2 \ + -DCMAKE_SYSTEM_NAME=Linux \ + -DCMAKE_SYSTEM_PROCESSOR=aarch64 \ + -DCMAKE_C_COMPILER=/root/llvm/clang/bin/clang \ + -DCMAKE_C_COMPILER_TARGET=aarch64-linux-gnu \ + -DCMAKE_CXX_COMPILER=/root/llvm/clang/bin/clang++ \ + -DCMAKE_CXX_COMPILER_TARGET=aarch64-linux-gnu \ + -DCMAKE_FIND_ROOT_PATH=/root/rootfs \ + -DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER \ + -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=BOTH \ + -DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=BOTH \ + -DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=BOTH \ + -DCMAKE_SYSROOT=/root/rootfs \ + -DSDL_STATIC=ON \ + -DSDL_SHARED=OFF \ + -DSDL_ATOMIC=OFF \ + -DSDL_AUDIO=OFF \ + -DSDL_VIDEO=ON \ + -DSDL_RENDER=ON \ + -DSDL_EVENTS=ON \ + -DSDL_JOYSTICK=ON \ + -DSDL_HAPTIC=ON \ + -DSDL_POWER=ON \ + -DSDL_THREADS=ON \ + -DSDL_TIMERS=OFF \ + -DSDL_FILE=OFF \ + -DSDL_LOADSO=ON \ + -DSDL_CPUINFO=OFF \ + -DSDL_FILESYSTEM=OFF \ + -DSDL_DLOPEN=ON \ + -DSDL_SENSOR=ON \ + -DSDL_COCOA=OFF \ + -DSDL_KMSDRM=OFF \ + -DSDL_METAL=OFF \ + -DSDL_OPENGL=ON \ + -DSDL_OPENGLES=ON \ + -DSDL_RPI=OFF \ + -DSDL_VIVANTE=OFF \ + -DSDL_VULKAN=OFF \ + -DSDL_WAYLAND=OFF \ + -DSDL_X11=ON \ + -DSDL_X11_SHARED=OFF \ + -DSDL_X11_XCURSOR=OFF \ + -DSDL_X11_XFIXES=OFF \ + -DSDL_X11_XINERAMA=OFF \ + -DSDL_X11_XINPUT=OFF \ + -DSDL_X11_XRANDR=OFF \ + -DSDL_X11_XSCRNSAVER=OFF \ + -DSDL_X11_XSHAPE=OFF \ + -DSDL_X11_XVM=OFF \ + && make -j`nproc` \ + && make install diff --git a/build/raspberry-pi-os_armv8/rpi-raspbian.conf b/build/raspberry-pi-os_armv8/rpi-raspbian.conf index ed7457aa..fe08b4d6 100644 --- a/build/raspberry-pi-os_armv8/rpi-raspbian.conf +++ b/build/raspberry-pi-os_armv8/rpi-raspbian.conf @@ -4,11 +4,11 @@ bootstrap=Deb Rasp aptsources=Deb Rasp [Deb] -packages=libc6-dev libstdc++-10-dev libasound2-dev libpulse-dev libudev-dev libexpat1-dev libnss3-dev libxext-dev libxtst-dev libx11-dev +packages=libc6-dev libstdc++-10-dev libasound2-dev libpulse-dev libudev-dev libexpat1-dev libnss3-dev libxext-dev libxtst-dev libsdl2-dev source=http://deb.debian.org/debian suite=bullseye [Rasp] -packages=libraspberrypi-bin libraspberrypi-dev +packages=libcamera-dev source=http://archive.raspberrypi.org/debian suite=bullseye diff --git a/build/ubuntu-18.04_armv8_jetson_nano/arm64.conf b/build/ubuntu-18.04_armv8_jetson_nano/arm64.conf deleted file mode 100644 index d15b09ac..00000000 --- a/build/ubuntu-18.04_armv8_jetson_nano/arm64.conf +++ /dev/null @@ -1,23 +0,0 @@ -[General] -noauth=true -unpack=true -bootstrap=Ports Jetson T210 -aptsources=Ports Jetson T210 - -[Ports] -packages=libc6-dev libstdc++-6-dev libasound2-dev libpulse-dev libudev-dev libexpat1-dev libnss3-dev python-dev libgtk-3-dev libgstreamer-plugins-bad1.0-0 -source=http://ports.ubuntu.com -suite=bionic -components=main universe - -[Jetson] -packages=nvidia-l4t-camera nvidia-l4t-multimedia -source=https://repo.download.nvidia.com/jetson/common -suite=r32.6 -components=main - -[T210] -packages=nvidia-l4t-jetson-multimedia-api -source=https://repo.download.nvidia.com/jetson/t210 -suite=r32.6 -components=main \ No newline at end of file diff --git a/build/ubuntu-18.04_armv8_jetson_nano/jetson.sh b/build/ubuntu-18.04_armv8_jetson_nano/jetson.sh deleted file mode 100755 index 82d98a8f..00000000 --- a/build/ubuntu-18.04_armv8_jetson_nano/jetson.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash - -set -ex - -SYSDIR=/root/rootfs - -pushd $SYSDIR/usr/lib/aarch64-linux-gnu - # 既存の libdl.so は libdl.so -> ../../../lib/aarch64-linux-gnu/libdl.so.2 なのに対して、 - # Jetson Nano の libdl.so は libdl.so -> /lib/aarch64-linux-gnu/libdl.so.2 になっているため、パスが見つからない。 - # なので symlink を相対パスで貼り直してやる。 - ln -sf ../../../lib/aarch64-linux-gnu/libdl.so.2 libdl.so - pushd tegra - # libnvbuf_utils.so.1.0.0 も同じ - ln -s libnvbuf_fdmap.so.1.0.0 libnvbuf_fdmap.so - popd -popd diff --git a/build/ubuntu-18.04_armv8_jetson_nano/Dockerfile b/build/ubuntu-20.04_armv8_jetson_xavier/Dockerfile similarity index 84% rename from build/ubuntu-18.04_armv8_jetson_nano/Dockerfile rename to build/ubuntu-20.04_armv8_jetson_xavier/Dockerfile index 329a1adf..f1c218bd 100644 --- a/build/ubuntu-18.04_armv8_jetson_nano/Dockerfile +++ b/build/ubuntu-20.04_armv8_jetson_xavier/Dockerfile @@ -1,5 +1,5 @@ # syntax = docker/dockerfile:1.1.1-experimental -FROM ubuntu:18.04 +FROM ubuntu:20.04 ARG PACKAGE_NAME @@ -20,8 +20,6 @@ COPY arm64.conf /root/ RUN --mount=type=cache,id=$PACKAGE_NAME,target=/var/cache/apt --mount=type=cache,id=$PACKAGE_NAME,target=/var/lib/apt \ /root/init_rootfs_arm64.sh /root/rootfs /root/arm64.conf -# 構築した RootFS を Jetson Nano のファイルで上書きする - COPY jetson.sh /root/ RUN /root/jetson.sh @@ -30,7 +28,7 @@ RUN /root/jetson.sh ARG WEBRTC_BUILD_VERSION COPY script/get_webrtc.sh /root/ -RUN /root/get_webrtc.sh "$WEBRTC_BUILD_VERSION" ubuntu-18.04_armv8 /root /root +RUN /root/get_webrtc.sh "$WEBRTC_BUILD_VERSION" ubuntu-20.04_armv8 /root /root # コンパイラの取得 @@ -116,6 +114,7 @@ RUN \ -DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=BOTH \ -DCMAKE_SYSROOT=/root/rootfs \ -DBUILD_SHARED_LIBS=OFF \ + -DSDL_STATIC_PIC=ON \ -DSDL_ATOMIC=OFF \ -DSDL_AUDIO=OFF \ -DSDL_VIDEO=ON \ @@ -130,26 +129,25 @@ RUN \ -DSDL_LOADSO=ON \ -DSDL_CPUINFO=OFF \ -DSDL_FILESYSTEM=OFF \ - -DSDL_DLOPEN=ON \ -DSDL_SENSOR=ON \ - -DVIDEO_OPENGL=ON \ - -DVIDEO_OPENGLES=ON \ - -DVIDEO_RPI=OFF \ - -DVIDEO_WAYLAND=OFF \ - -DVIDEO_X11=ON \ - -DX11_SHARED=OFF \ - -DVIDEO_X11_XCURSOR=OFF \ - -DVIDEO_X11_XINERAMA=OFF \ - -DVIDEO_X11_XINPUT=OFF \ - -DVIDEO_X11_XRANDR=OFF \ - -DVIDEO_X11_XSCRNSAVER=OFF \ - -DVIDEO_X11_XSHAPE=OFF \ - -DVIDEO_X11_XVM=OFF \ - -DVIDEO_VULKAN=OFF \ - -DVIDEO_VIVANTE=OFF \ - -DVIDEO_COCOA=OFF \ - -DVIDEO_METAL=OFF \ - -DVIDEO_KMSDRM=OFF \ + -DSDL_OPENGL=ON \ + -DSDL_OPENGLES=ON \ + -DSDL_RPI=OFF \ + -DSDL_WAYLAND=OFF \ + -DSDL_X11=ON \ + -DSDL_X11_SHARED=OFF \ + -DSDL_X11_XCURSOR=OFF \ + -DSDL_X11_XDBE=OFF \ + -DSDL_X11_XINPUT=OFF \ + -DSDL_X11_XFIXES=OFF \ + -DSDL_X11_XRANDR=OFF \ + -DSDL_X11_XSCRNSAVER=OFF \ + -DSDL_X11_XSHAPE=OFF \ + -DSDL_VULKAN=OFF \ + -DSDL_VIVANTE=OFF \ + -DSDL_COCOA=OFF \ + -DSDL_METAL=OFF \ + -DSDL_KMSDRM=OFF \ && make -j`nproc` \ && make install diff --git a/build/ubuntu-18.04_armv8_jetson_xavier/arm64.conf b/build/ubuntu-20.04_armv8_jetson_xavier/arm64.conf similarity index 52% rename from build/ubuntu-18.04_armv8_jetson_xavier/arm64.conf rename to build/ubuntu-20.04_armv8_jetson_xavier/arm64.conf index e2a70dee..4f2dd3ad 100644 --- a/build/ubuntu-18.04_armv8_jetson_xavier/arm64.conf +++ b/build/ubuntu-20.04_armv8_jetson_xavier/arm64.conf @@ -5,19 +5,19 @@ bootstrap=Ports Jetson T194 aptsources=Ports Jetson T194 [Ports] -packages=libc6-dev libstdc++-6-dev libasound2-dev libpulse-dev libudev-dev libexpat1-dev libnss3-dev python-dev libgtk-3-dev libgstreamer-plugins-bad1.0-0 +packages=libc6-dev libstdc++-10-dev libxext-dev libxtst-dev source=http://ports.ubuntu.com -suite=bionic +suite=focal components=main universe [Jetson] -packages=nvidia-l4t-camera nvidia-l4t-multimedia +packages= source=https://repo.download.nvidia.com/jetson/common -suite=r32.6 +suite=r35.3 components=main [T194] -packages=nvidia-l4t-jetson-multimedia-api +packages=nvidia-l4t-camera nvidia-l4t-jetson-multimedia-api source=https://repo.download.nvidia.com/jetson/t194 -suite=r32.6 +suite=r35.3 components=main \ No newline at end of file diff --git a/build/ubuntu-18.04_armv8_jetson_xavier/jetson.sh b/build/ubuntu-20.04_armv8_jetson_xavier/jetson.sh similarity index 100% rename from build/ubuntu-18.04_armv8_jetson_xavier/jetson.sh rename to build/ubuntu-20.04_armv8_jetson_xavier/jetson.sh diff --git a/build/ubuntu-18.04_armv8_jetson_xavier/Dockerfile b/build/ubuntu-22.04_x86_64/Dockerfile similarity index 56% rename from build/ubuntu-18.04_armv8_jetson_xavier/Dockerfile rename to build/ubuntu-22.04_x86_64/Dockerfile index 329a1adf..ff541c47 100644 --- a/build/ubuntu-18.04_armv8_jetson_xavier/Dockerfile +++ b/build/ubuntu-22.04_x86_64/Dockerfile @@ -1,5 +1,5 @@ # syntax = docker/dockerfile:1.1.1-experimental -FROM ubuntu:18.04 +FROM ubuntu:22.04 ARG PACKAGE_NAME @@ -9,28 +9,18 @@ RUN rm -f /etc/apt/apt.conf.d/docker-clean; echo 'Binary::apt::APT::Keep-Downloa # パッケージのインストール -COPY script/apt_install_arm.sh /root/ +COPY script/apt_install_x86_64.sh /root/ RUN --mount=type=cache,id=$PACKAGE_NAME,target=/var/cache/apt --mount=type=cache,id=$PACKAGE_NAME,target=/var/lib/apt \ - /root/apt_install_arm.sh - -# RootFS の構築 - -COPY script/init_rootfs_arm64.sh /root/ -COPY arm64.conf /root/ -RUN --mount=type=cache,id=$PACKAGE_NAME,target=/var/cache/apt --mount=type=cache,id=$PACKAGE_NAME,target=/var/lib/apt \ - /root/init_rootfs_arm64.sh /root/rootfs /root/arm64.conf - -# 構築した RootFS を Jetson Nano のファイルで上書きする - -COPY jetson.sh /root/ -RUN /root/jetson.sh + /root/apt_install_x86_64.sh \ + && apt-get -y install libdrm-dev libstdc++-12-dev # WebRTC の取得 ARG WEBRTC_BUILD_VERSION COPY script/get_webrtc.sh /root/ -RUN /root/get_webrtc.sh "$WEBRTC_BUILD_VERSION" ubuntu-18.04_armv8 /root /root +RUN /root/get_webrtc.sh "$WEBRTC_BUILD_VERSION" ubuntu-22.04_x86_64 /root /root +# COPY webrtc/ /root/webrtc/ # コンパイラの取得 @@ -56,18 +46,12 @@ RUN \ -D_LIBCPP_DISABLE_AVAILABILITY \ -nostdinc++ \ -isystem/root/llvm/libcxx/include \ - --target=aarch64-linux-gnu \ - --sysroot=/root/rootfs \ - -I/root/rootfs/usr/include/aarch64-linux-gnu \ ' \ linkflags=' \ - -L/root/rootfs/usr/lib/aarch64-linux-gnu \ - -B/root/rootfs/usr/lib/aarch64-linux-gnu \ ' \ toolset=clang \ visibility=global \ target-os=linux \ - architecture=arm \ address-model=64 \ link=static \ variant=release \ @@ -103,18 +87,8 @@ RUN \ && cmake ../source \ -DCMAKE_BUILD_TYPE=Release \ -DCMAKE_INSTALL_PREFIX=/root/SDL2 \ - -DCMAKE_SYSTEM_NAME=Linux \ - -DCMAKE_SYSTEM_PROCESSOR=aarch64 \ -DCMAKE_C_COMPILER=/root/llvm/clang/bin/clang \ - -DCMAKE_C_COMPILER_TARGET=aarch64-linux-gnu \ -DCMAKE_CXX_COMPILER=/root/llvm/clang/bin/clang++ \ - -DCMAKE_CXX_COMPILER_TARGET=aarch64-linux-gnu \ - -DCMAKE_FIND_ROOT_PATH=/root/rootfs \ - -DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER \ - -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=BOTH \ - -DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=BOTH \ - -DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=BOTH \ - -DCMAKE_SYSROOT=/root/rootfs \ -DBUILD_SHARED_LIBS=OFF \ -DSDL_ATOMIC=OFF \ -DSDL_AUDIO=OFF \ @@ -153,3 +127,57 @@ RUN \ && make -j`nproc` \ && make install +# CUDA 周りのインストール +ARG CUDA_VERSION +RUN set -ex \ + && apt-get update \ + && apt-get install -y software-properties-common \ + && wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin \ + && mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600 \ + && apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/3bf863cc.pub \ + && add-apt-repository "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /" \ + && apt-get update \ + && DEBIAN_FRONTEND=noninteractive apt-get -y install cuda=$CUDA_VERSION clang-12 + +# libva +ARG LIBVA_VERSION +RUN set -ex \ + && git clone --depth 1 --branch $LIBVA_VERSION https://github.com/intel/libva.git /root/libva-source \ + && mkdir -p /root/libva-build \ + && cd /root/libva-build \ + && CC=/root/llvm/clang/bin/clang \ + CXX=/root/llvm/clang/bin/clang++ \ + CFLAGS="-fPIC" \ + /root/libva-source/autogen.sh \ + --enable-static \ + --disable-shared \ + --with-drivers-path=/usr/lib/x86_64-linux-gnu/dri \ + --prefix /root/libva \ + && make -j`nproc` \ + && make install \ + && rm -rf /root/libva-build \ + && rm -rf /root/libva-source + +# Intel Media SDK +COPY patch/msdk_limits.patch /root/msdk_limits.patch +ARG MSDK_VERSION +RUN set -ex \ + && git clone --depth 1 --branch intel-mediasdk-$MSDK_VERSION https://github.com/Intel-Media-SDK/MediaSDK.git /root/msdk-source \ + && cd /root/msdk-source \ + && find . -name "CMakeLists.txt" | while read line; do sed -i 's/SHARED/STATIC/g' $line; done \ + && patch -p1 < /root/msdk_limits.patch \ + && mkdir -p /root/msdk-build \ + && cd /root/msdk-build \ + && cmake \ + -DCMAKE_INSTALL_PREFIX=/root/msdk \ + -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_PREFIX_PATH=/root/libva \ + -DCMAKE_C_COMPILER=/root/llvm/clang/bin/clang \ + -DCMAKE_CXX_COMPILER=/root/llvm/clang/bin/clang++ \ + -DBUILD_SAMPLES=OFF \ + -DBUILD_TUTORIALS=OFF \ + /root/msdk-source \ + && cmake --build . -j`nproc` \ + && cmake --install . \ + && rm -rf /root/msdk-build \ + && rm -rf /root/msdk-source diff --git a/doc/BUILD_LINUX.md b/doc/BUILD_LINUX.md index da2fd542..d3a35d13 100644 --- a/doc/BUILD_LINUX.md +++ b/doc/BUILD_LINUX.md @@ -47,32 +47,32 @@ $ ./build.sh raspberry-pi-os_armv8 うまくいかない場合は `./build.sh --clean raspberry-pi-os_armv8 && ./build.sh raspberry-pi-os_armv8` を試してみてください。 -## Ubuntu 18.04 (armv8) Jetson Nano 向けバイナリを作成する +## Ubuntu 20.04 (armv8) Jetson Xavier NX / AGX 向けバイナリを作成する -build ディレクトリ以下で ./build.sh ubuntu-18.04_armv8_jetson_nano と打つことで Momo の Ubuntu 18.04 armv8 Jetson Nano 向けバイナリが生成されます。 +build ディレクトリ以下で ./build.sh ubuntu-20.04_armv8_jetson_xavier と打つことで Momo の Ubuntu 20.04 armv8 Jetson Xavier NX / AGX 向けバイナリが生成されます。 ```shell -$ ./build.sh ubuntu-18.04_armv8_jetson_nano +$ ./build.sh ubuntu-20.04_armv8_jetson_xavier ``` -うまくいかない場合は `./build.sh --clean ubuntu-18.04_armv8_jetson_nano && ./build.sh ubuntu-18.04_armv8_jetson_nano` を試してみてください。 +うまくいかない場合は `./build.sh --clean ubuntu-20.04_armv8_jetson_xavier && ./build.sh ubuntu-20.04_armv8_jetson_xavier` を試してみてください。 -## Ubuntu 18.04 (armv8) Jetson Xavier NX / AGX 向けバイナリを作成する +## Ubuntu 20.04 (x86_64) 向けバイナリを作成する -build ディレクトリ以下で ./build.sh ubuntu-18.04_armv8_jetson_xavier と打つことで Momo の Ubuntu 18.04 armv8 Jetson Xavier NX / AGX 向けバイナリが生成されます。 +build ディレクトリ以下で ./build.sh ubuntu-20.04_x86_64 と打つことで Momo の Ubuntu 20.04 (x86_64) 向けバイナリが生成されます。 ```shell -$ ./build.sh ubuntu-18.04_armv8_jetson_xavier +$ ./build.sh ubuntu-20.04_x86_64 ``` -うまくいかない場合は `./build.sh --clean ubuntu-18.04_armv8_jetson_xavier && ./build.sh ubuntu-18.04_armv8_jetson_xavier` を試してみてください。 +うまくいかない場合は `./build.sh --clean ubuntu-20.04_x86_64 && ./build.sh ubuntu-20.04_x86_64` を試してみてください。 -## Ubuntu 20.04 (x86_64) 向けバイナリを作成する +## Ubuntu 22.04 (x86_64) 向けバイナリを作成する -build ディレクトリ以下で ./build.sh ubuntu-20.04_x86_64 と打つことで Momo の Ubuntu 20.04 (x86_64) 向けバイナリが生成されます。 +build ディレクトリ以下で ./build.sh ubuntu-22.04_x86_64 と打つことで Momo の Ubuntu 22.04 (x86_64) 向けバイナリが生成されます。 ```shell -$ ./build.sh ubuntu-20.04_x86_64 +$ ./build.sh ubuntu-22.04_x86_64 ``` -うまくいかない場合は `./build.sh --clean ubuntu-20.04_x86_64 && ./build.sh ubuntu-20.04_x86_64` を試してみてください。 +うまくいかない場合は `./build.sh --clean ubuntu-22.04_x86_64 && ./build.sh ubuntu-22.04_x86_64` を試してみてください。 \ No newline at end of file diff --git a/doc/BUY_JETSON.md b/doc/BUY_JETSON.md index 06b051e2..12e15cc5 100644 --- a/doc/BUY_JETSON.md +++ b/doc/BUY_JETSON.md @@ -4,24 +4,12 @@ NVIDIA Jetson を購入する場合のオススメリンクです。 ## Momo の Jetson シリーズへの対応について -2022 年 10 月時点での状況。 - -- Jetson シリーズは Jetson AGX Orin を除いて入手困難または異常に高価な販売となっている -- Jetpack 5 系は Jetson Nano には対応しない -- Jetson Orin nano はハードウェアエンコードに対応しない -- Jetson Xavier NX は Jetson Nano と比較すると JPEG のハードウェア処理が劣る -- Jetson Nano で利用されている Ubuntu 18.04 は 2023 年 4 月でサポートが終了する -- Jetson Nano ハードウェアサポートは 2025 年 1 月までサポートされる - -以上を踏まえて Momo では今後以下のような方針をとることにしました。 - -- 2023 年 4 月をもって Jetpack 4 系への対応を終了する - - Jetpack 5 系のみの対応とする - - それに伴い Jetpack 4 系のみに対応している Jetson Nano の対応を終了する -- Jetson Orin Nano への対応は行わない -- Jetson Orin NX への対応は優先実装とする +- Jetpack 5 系のみの対応とします。 +- Jetpack 4 系への対応を終了しました。 + - 上記に伴い Jetpack 4 系のみに対応している Jetson Nano の対応を終了しました。 +- ハードウェアエンコードに対応しない Jetson Orin Nano への対応は行いません +- Jetson Orin NX への対応は優先実装とします。 - Xavier と同じパターンだと Jetson Orin Nano より JPEG のハードウェア処理が劣る可能性があるため -- Jetson Nano は support/jetpack-4.6 として 2023 年 4 月まで維持する ## Jetson AGX Orin @@ -31,7 +19,7 @@ NVIDIA Jetson を購入する場合のオススメリンクです。 ## 4K@30 出るカメラを購入する -実際に Jetson Nano で検証して 4K で 30fps の出力動作確認が取れているカメラです。 +実際に Jetson で検証して 4K で 30fps の出力動作確認が取れているカメラです。 - [高解像度 4 18K カメラモジュール 3840 × 2160 ソニー IMX317 Mjpeg 30fps ミニ Usb ウェブカメラ Web カメラ](https://ja.aliexpress.com/item/32999909513.html) @@ -54,4 +42,3 @@ NVIDIA Jetson を購入する場合のオススメリンクです。 - [Jetson Roadmap \| NVIDIA Developer](https://developer.nvidia.com/embedded/develop/roadmap) - Jetson ライフサイクル - [Jetson Product Lifecycle \| NVIDIA Developer](https://developer.nvidia.com/embedded/lifecycle) - diff --git a/doc/FAQ.md b/doc/FAQ.md index b85a1a39..b3b13f24 100644 --- a/doc/FAQ.md +++ b/doc/FAQ.md @@ -54,23 +54,23 @@ NVENC が利用可能なビデオカードは以下で確認してください **是非 Discord の #momo-nvidia-video-codec-sdk チャネルまでご連絡ください** - GeForce GTX 1080 Ti - - @melpon + - @melpon - GeForce GTX 1650 - - @melpon + - @melpon - GeForce GTX 1060 6GB - - @massie_g + - @massie_g - GeForce GTX 1050 Ti 4GB - - @cli_nil + - @cli_nil - GeForce GTX 1070 with Max-Q Design 8GB - - @torikizi + - @torikizi - Quadro P1000 4GB - - 株式会社オプティム + - 株式会社オプティム - Quadro P4000 - - 株式会社オプティム + - 株式会社オプティム - GeForce RTX 2070 - - @msnoigrs + - @msnoigrs - GeForce RTX 2080 - - @shirokunet + - @shirokunet ## 4K カメラのオススメはありますか? @@ -82,17 +82,17 @@ NVENC が利用可能なビデオカードは以下で確認してください 以下の記事を参考にしてみてください。 -[カメラ無しのラズパイとmomoでテスト映像をWebRTCで配信する \- Qiita](https://qiita.com/tetsu_koba/items/789a19cb575953f41a1a) +[カメラ無しのラズパイと momo でテスト映像を WebRTC で配信する \- Qiita](https://qiita.com/tetsu_koba/items/789a19cb575953f41a1a) ## Momo はマイクからの音声以外を入力できますか? 以下の記事を参考にしてみてください。 -[ラズパイ\+momoでWebRTCで送信するときにマイクの代わりに音声ファイルを使用する \- Qiita](https://qiita.com/tetsu_koba/items/b887c1a0be9f26b795f2) +[ラズパイ\+momo で WebRTC で送信するときにマイクの代わりに音声ファイルを使用する \- Qiita](https://qiita.com/tetsu_koba/items/b887c1a0be9f26b795f2) ## Mac で 60fps を利用できますか? -利用できません。60fps を使ってみたい場合は Jetson Nano などをご利用ください。 +利用できません。60fps を使ってみたい場合は Momo が対応する Jetson シリーズ などをご利用ください。 ## Sora モードで DataChannel メッセージングは利用できますか? @@ -110,12 +110,16 @@ MJPEG に対応した CSI カメラや USB カメラをご用意いただくか ## Mac (arm64) から H.264 の FHD でスクリーンキャプチャを配信したい -Mac (arm64) から FHD でスクリーンキャプチャを配信したい場合は Sora の H.264 のプロファイルレベル ID を 3.2 以上に設定してください。 +Mac (arm64) から FHD でスクリーンキャプチャを配信したい場合は Sora の H.264 のプロファイルレベル ID を 3.2 以上に設定してください。 設定方法はこちらの [Sora のドキュメント](https://sora-doc.shiguredo.jp/sora_conf#default-h264-profile-level-id)をお読みください。 プロファイルレベル ID を変更しない場合は H.264 の HD 以下で配信するか、他のコーデックを使用して FHD 配信をしてください。 +また、プロファイルレベル ID を変更することができない Test モードや Ayame モードでは FHD 配信ができません。 + +こちらも H.264 の HD 以下で配信するか、他のコーデックを使用して FHD 配信をしてください。 + ## Windows で H.264 を利用できますか? NVIDIA のビデオカードの NVENC を利用することで H.264 が利用可能になります。 @@ -134,6 +138,44 @@ Momo の Sora モードではマルチストリーム機能を無効にして利 以下に示すオプションを指定することで利用可能です。 - `--proxy-url` : プロキシの URL - - 例) http://proxy.example.com:3128 + - 例) http://proxy.example.com:3128 - `--proxy-username` : プロキシ認証に使用するユーザー名 - `--proxy-password` : プロキシ認証に使用するパスワード + +## Raspberry Pi (Raspberry-Pi-OS) 64 bit で H.264 を利用できますか? + +Release 2023.1.0 以降から利用可能です。 + +## Momo のオプションで指定した解像度で映像の送信はできますか? + +`--resolution` オプションを使用することで解像度の設定が可能です。 + +しかしこれはあくまでも Momo からカメラデバイスに対して送るリクエストであり、最終的な解像度はカメラデバイスの仕様に依存します。 + +もし、カメラデバイスのサポート外の解像度を試してみたい方は以下の記事を参考にしてみてください。 + +[WebRTC momo でカメラのサポートしていない解像度、フレームレートで送信する](https://zenn.dev/tetsu_koba/articles/c3b12bb5e52a57) + +## Jetson で Momo を実行するとエラーが出ていますがこれはなんですか? + +Jetson で Momo を実行すると `Capture Plane:Error in VIDIOC_S_FMT: Invalid argument` というエラーが出ます。 + +これは HWA 対応確認のために非対応の可能性のあるエンコーダーとデコーダーを作成する処理を行っているためです。 + +動作には影響ありません。 + +## Jetson で SDL を使用して AV1 の映像が受信できません + +Momo はハードウェアデコードを優先して使用するようになっており、AV1 のハードウェアデコードに対応しているのは Jetson Orin のみです。 + +Jetson Orin 以外の Jetson をご利用の場合は `--av1-decoder` オプションで `software` を指定してください。 + +## Raspberry Pi カメラモジュール V3 は利用できますか? + +Release 2023.1.0 以降から利用可能です。 + +## 利用できる JetPack のバージョンはいくつですか? + +JetPack 5.1.1 のみで利用可能です。 + +JetPack 5.1.1 以外のバージョンでは利用できません。 diff --git a/doc/INTEL_MEDIA_SDK.md b/doc/INTEL_MEDIA_SDK.md index e768be45..cfacc5bb 100644 --- a/doc/INTEL_MEDIA_SDK.md +++ b/doc/INTEL_MEDIA_SDK.md @@ -19,6 +19,7 @@ Intel Media SDK の詳細については以下のリンクをご確認くださ - Windows 10 x86_64 - Ubuntu 20.04 x86_64 +- Ubuntu 22.04 x86_64 ## Windows 10 での利用方法 @@ -64,9 +65,9 @@ H264: - Intel Media SDK [intel] (default) ``` -## Ubuntu 20.04 での利用方法 +## Ubuntu 20.04、 Ubuntu 22.04 での利用方法 -### ドライバーのインストール +### ドライバーのインストール∂ - Ubuntu の最新化を実行します - `sudo apt-get update` diff --git a/doc/PACKAGE.md b/doc/PACKAGE.md index a4af4399..6d3cebb0 100644 --- a/doc/PACKAGE.md +++ b/doc/PACKAGE.md @@ -23,9 +23,9 @@ Windows の場合は以下のコマンドになります。 - raspberry-pi-os_armv6 - raspberry-pi-os_armv7 - raspberry-pi-os_armv8 -- ubuntu-18.04_armv8_jetson_nano -- ubuntu-18.04_armv8_jetson_xavier +- ubuntu-20.04_armv8_jetson_xavier - ubuntu-20.04_x86_64 +- ubuntu-22.04_x86_64 ## パッケージ解凍後の構成 diff --git a/doc/SETUP_JETSON.md b/doc/SETUP_JETSON.md index a3eef1b0..8fd16008 100644 --- a/doc/SETUP_JETSON.md +++ b/doc/SETUP_JETSON.md @@ -2,18 +2,16 @@ もし Jetson シリーズを購入する場合は [BUY_JETSON.md](BUY_JETSON.md) を参考にしてください。 -## Jetson シリーズでは JetPack 4.4 以上の利用を前提としています +## Jetson シリーズでは JetPack 5.1.1 のみの利用を前提としています -[JetPack 4\.4 \- L4T R32\.4\.3 production release \- Jetson & Embedded Systems / Announcements \- NVIDIA Developer Forums](https://forums.developer.nvidia.com/t/jetpack-4-4-l4t-r32-4-3-production-release/140870) +[JetPack 5.1.1 is now live \- Jetson & Embedded Systems / Announcements \- NVIDIA Developer Forums](https://forums.developer.nvidia.com/t/jetpack-5-1-1-is-now-live/247862/1) ## Jetson シリーズ向けのバイナリは以下にて提供しています https://github.com/shiguredo/momo/releases にて最新版のバイナリをダウンロードしてください。 -- `momo-_ubuntu-18.04_armv8_jetson_nano.tar.gz` - - Jetson Nano -- `momo-_ubuntu-18.04_armv8_jetson_xavier.tar.gz` - - Jetson Xavier NX または Jetson AGX Xavier +- `momo-_ubuntu-20.04_armv8_jetson_xavier.tar.gz` + - Jetson AGX Orin , Jetson AGX Xavier または Jetson Xavier NX ## ダウンロードしたパッケージ、解凍後の構成 @@ -36,7 +34,7 @@ $ tree ビデオデバイスの指定については [LINUX_VIDEO_DEVICE.md](LINUX_VIDEO_DEVICE.md) をご確認ください。 -## Jetson Nano 向けの追加のオプション +## Jetson 向けの追加のオプション ### --hw-mjpeg-decoder @@ -51,13 +49,13 @@ $ ./momo --hw-mjpeg-decoder=true --no-audio-device test ### 実行時のコマンドについて -`--fixed-resolution` を外してみてください。4Kの時には `--fixed-resolution` オプションを使うとレートが安定しない傾向があります。 +`--fixed-resolution` を外してみてください。4K の時には `--fixed-resolution` オプションを使うとレートが安定しない傾向があります。 ### フレームレートが出ない場合 一番多いのは暗い場所で利用しているパターンです。カメラが自動的に露光時間を伸ばすためフレームレートが下がります。部屋を明るくする。もしくはカメラの設定変更が可能な場合はフレームレート優先設定に変更してください。 -### [IMX317を搭載した推奨カメラ](https://ja.aliexpress.com/item/32999909513.html) をご利用の場合 +### [IMX317 を搭載した推奨カメラ](https://ja.aliexpress.com/item/32999909513.html) をご利用の場合 > v4l2-ctl --set-ctrl=exposure_auto=1 @@ -87,13 +85,13 @@ error 5 getting ext_ctrl Zoom, Absolute ## 4K@30fps の実行例 -ここでは Jetson Nano を使って 4K@30fps を実行する方法を記載します。 +ここでは Jetson AGX Orin を使って 4K@30fps を実行する方法を記載します。 ### 事前確認 4K@30fps のコマンドを実行する前に準備が完了しているか事前に確認をします。 -- Jetson Nano で momo を使うためのセットアップが全て完了している +- Jetson AGX Orin で momo を使うためのセットアップが全て完了している - 4K@30fps が可能なカメラがセットされている - Sora/Sora Labo のアカウントの用意がある @@ -108,10 +106,8 @@ Sora Labo の利用申請や使用方法については [Sora Labo のドキュ ```shell $ ./momo --hw-mjpeg-decoder true --framerate 30 --resolution 4K --log-level 2 sora \ --signaling-url \ - wss://0001.canary.sora-labo.shiguredo.app/signaling \ - wss://0002.canary.sora-labo.shiguredo.app/signaling \ - wss://0003.canary.sora-labo.shiguredo.app/signaling \ - --channel-id sora@shiguredo#0 \ + wss://canary.sora-labo.shiguredo.app/signaling \ + --channel-id shiguredo_0_sora \ --video true --audio true \ --video-codec-type VP8 --video-bit-rate 15000 \ --auto --role sendonly \ @@ -121,25 +117,25 @@ $ ./momo --hw-mjpeg-decoder true --framerate 30 --resolution 4K --log-level 2 so コマンド例の構成は以下のようになっています。 - ./momo ~ sora までは momo に対して行うパラメータになっています。 - - `--hw-mjpeg-decoder true` は Hardware Acceleration を有効に設定しています - - `--framerate 30` は フレームレートを 30 に設定しています - - `--resolution 4K` は解像度を 4K に設定しています - - `--log-level 2` は error と warning のログを出力するように設定しています - - `sora` は Sora モードを利用するように設定しています + - `--hw-mjpeg-decoder true` は Hardware Acceleration を有効に設定しています + - `--framerate 30` は フレームレートを 30 に設定しています + - `--resolution 4K` は解像度を 4K に設定しています + - `--log-level 2` は error と warning のログを出力するように設定しています + - `sora` は Sora モードを利用するように設定しています - `sora` 以降 2 行目からは Sora との接続のためのパラメータになっています - - `wss://0001.canary.sora-labo.shiguredo.app/signaling` はシグナリング URL の設定をしています - - `sora@shiguredo#0` はチャネル ID を設定しています - - `--video true` は Sora への映像送信を有効に設定しています - - `--audio true` は Sora への音声送信を有効に設定しています - - `--video-codec-type VP8` はコーデックを VP8 に設定しています - - `--video-bit-rate 15000` はビデオビットレートを 1.5Mbps で設定しています - - `--auto` は Sora との自動接続を有効に設定しています - - `--role sendonly` は送信時の役割を送信のみで設定しています - - `--metadata '{"access_token": "xyz"}'` は Sora Labo のアクセストークンをメタデータに設定しています + - `wss://canary.sora-labo.shiguredo.app/signaling` はシグナリング URL の設定をしています + - `shiguredo_0_sora` はチャネル ID を設定しています + - `--video true` は Sora への映像送信を有効に設定しています + - `--audio true` は Sora への音声送信を有効に設定しています + - `--video-codec-type VP8` はコーデックを VP8 に設定しています + - `--video-bit-rate 15000` はビデオビットレートを 1.5Mbps で設定しています + - `--auto` は Sora との自動接続を有効に設定しています + - `--role sendonly` は送信時の役割を送信のみで設定しています + - `--metadata '{"access_token": "xyz"}'` は Sora Labo のアクセストークンをメタデータに設定しています ### 実行結果 -実行結果の確認はChrome の `chrome://webrtc-internals` を利用します。 +実行結果の確認は Chrome の `chrome://webrtc-internals` を利用します。 `chrome://webrtc-internals` を確認すると以下のように 4K(3840x2160) で 30 fps が出ていることが確認できます。 diff --git a/doc/SETUP_RASPBERRY_PI.md b/doc/SETUP_RASPBERRY_PI.md index f8b53ea0..20d333a9 100644 --- a/doc/SETUP_RASPBERRY_PI.md +++ b/doc/SETUP_RASPBERRY_PI.md @@ -1,9 +1,14 @@ # Raspberry Pi (Raspberry-Pi-OS) で Momo を使ってみる +## 注意 + +Raspberry Pi OS のレガシー版には対応しておりません。最新版の Raspberry Pi OS を利用してください + ## Raspberry Pi 向けのバイナリは以下にて提供しています https://github.com/shiguredo/momo/releases にて最新版のバイナリをダウンロードしてください。 +- Raspberry Pi OS 64 bit を利用する場合は、 `momo-_raspberry-pi-os_armv8.tar.gz` を利用してください - Raspberry Pi 2 や 3 や 4 を利用する場合は、 `momo-_raspberry-pi-os_armv7.tar.gz` を利用してください - Raspberry Pi Zero や 1 を利用する場合は、 `momo-_raspberry-pi-os_armv6.tar.gz` を利用してください @@ -32,6 +37,19 @@ $ sudo apt-get upgrade $ sudo apt-get install libnspr4 libnss3 ``` +#### Raspberry Pi OS Lite を利用する場合 + +Raspberry Pi Lite では映像に関するパッケージが入っていないため、`ldd ./momo | grep not` を実行し、不足しているパッケージを確認してください。 + +下記に実行する一例を示します。 + +``` +$ sudo apt-get install libSDL2-2.0 +$ sudo apt-get install libxtst6 +$ sudo apt-get install libegl1-mesa-dev +$ sudo apt-get install libgles2-mesa +``` + ### Raspberry-Pi-OS で Raspberry Pi 用カメラなどの CSI カメラを利用する場合 これは USB カメラを利用する場合は不要なオプションです。 @@ -57,8 +75,7 @@ $ sudo modprobe bcm2835-v4l2 max_video_width=2592 max_video_height=1944 ### --force-i420 `--force-i420` は Raspberry Pi 専用カメラ用では MJPEG を使うとパフォーマンスが落ちるため HD 以上の解像度でも MJPEG にせず強制的に I420 でキャプチャーします。 -USBカメラでは逆にフレームレートが落ちるため使わないでください。 - +USB カメラでは逆にフレームレートが落ちるため使わないでください。 ```shell $ ./momo --force-i420 --no-audio-device test @@ -80,7 +97,7 @@ $ ./momo --hw-mjpeg-decoder true --no-audio-device test ### オプションを見直す -Raspberry Pi 用カメラ利用時には `--hw-mjpeg-decoder=true --force-i420` オプションを併用するとCPU使用率が下がりフレームレートが上がります。例えば、 Raspberry Pi Zero の場合には +Raspberry Pi 用カメラ利用時には `--hw-mjpeg-decoder=true --force-i420` オプションを併用すると CPU 使用率が下がりフレームレートが上がります。例えば、 Raspberry Pi Zero の場合には ```shell $ ./momo --resolution=HD --force-i420 --hw-mjpeg-decoder=true test @@ -88,12 +105,11 @@ $ ./momo --resolution=HD --force-i420 --hw-mjpeg-decoder=true test がリアルタイムでの最高解像度設定となります。 - ## USB カメラでパフォーマンスが出ない ### --hw-mjpeg-decoder -一部の MJPEG に対応した USBカメラを使用している場合、 `--hw-mjpeg-decoder` は ハードウェアによるビデオのリサイズ と MJPEG をハードウェアデコードします。 +一部の MJPEG に対応した USB カメラを使用している場合、 `--hw-mjpeg-decoder` は ハードウェアによるビデオのリサイズ と MJPEG をハードウェアデコードします。 ```shell $ ./momo --hw-mjpeg-decoder true --no-audio-device test @@ -101,7 +117,7 @@ $ ./momo --hw-mjpeg-decoder true --no-audio-device test ### Raspberry Pi で USB カメラ利用時に --hw-mjpeg-decoder を使ってもフレームレートが出ない -USB カメラ利用時には `--hw-mjpeg-decoder` を使わない方がフレームレートは出ます。しかし `--hw-mjpeg-decoder` を使ってCPU使用率を下げた状態で利用したい場合は /boot/config.txt の末尾に下記を追記してください +USB カメラ利用時には `--hw-mjpeg-decoder` を使わない方がフレームレートは出ます。しかし `--hw-mjpeg-decoder` を使って CPU 使用率を下げた状態で利用したい場合は /boot/config.txt の末尾に下記を追記してください ``` gpu_mem=256 diff --git a/doc/SETUP_UBUNTU.md b/doc/SETUP_UBUNTU.md index ed05bb7b..ccd758cf 100644 --- a/doc/SETUP_UBUNTU.md +++ b/doc/SETUP_UBUNTU.md @@ -1,10 +1,10 @@ -# Ubuntu 20.04 x86_64 で Momo を使ってみる +# Ubuntu 20.04 x86_64、 Ubuntu 22.04 x86_64 で Momo を使ってみる -## Ubuntu 20.04 x86_64 向けのバイナリは以下にて提供しています +## Ubuntu 20.04 x86_64、 Ubuntu 22.04 x86_64 向けのバイナリは以下にて提供しています https://github.com/shiguredo/momo/releases にて最新版のバイナリをダウンロードしてください。 -- バイナリは、 `momo-_ubuntu-20.04_x86_64.tar.gz` を利用してください +- バイナリは、 `momo-_ubuntu-20.04_x86_64.tar.gz` あるいは `momo-_ubuntu-22.04_x86_64.tar.gz` を利用してください ## ダウンロードしたパッケージ、解凍後の構成 diff --git a/doc/SETUP_WINDOWS.md b/doc/SETUP_WINDOWS.md index 0a844e92..bad797bf 100644 --- a/doc/SETUP_WINDOWS.md +++ b/doc/SETUP_WINDOWS.md @@ -18,10 +18,8 @@ PowerShell での実行例: .\momo.exe --no-audio-device ` sora ` --signaling-url ` - wss://0001.canary.sora-labo.shiguredo.app/signaling ` - wss://0002.canary.sora-labo.shiguredo.app/signaling ` - wss://0003.canary.sora-labo.shiguredo.app/signaling ` - --channel-id sora@shiguredo#0 ` + wss://canary.sora-labo.shiguredo.app/signaling ` + --channel-id shiguredo_0_sora ` --video-codec-type VP8 --video-bit-rate 500 ` --audio false ` --role sendonly --metadata '{\"access_token\": \"xyz\"}' diff --git a/doc/USE.md b/doc/USE.md index 888a7967..0546f344 100644 --- a/doc/USE.md +++ b/doc/USE.md @@ -86,7 +86,7 @@ USE_SDL2=1 ### ヘルプ ``` -$ ./momo --help +$ ./momo --help Momo - WebRTC Native Client Usage: ./momo [OPTIONS] [SUBCOMMAND] @@ -97,16 +97,18 @@ Options: --no-video-device Do not use video device --no-audio-device Do not use audio device --force-i420 Prefer I420 format for video capture (only on supported devices) - --hw-mjpeg-decoder Perform MJPEG deoode and video resize by hardware acceleration (only on supported devices) + --hw-mjpeg-decoder BOOLEAN:value in {false->0,true->1} OR {0,1} + Perform MJPEG deoode and video resize by hardware acceleration (only on supported devices) + --use-libcamera Use libcamera for video capture (only on supported devices) + --use-libcamera-native Use native buffer for H.264 encoding --video-device TEXT Use the video device specified by an index or a name (use the first one if not specified) --resolution TEXT Video resolution (one of QVGA, VGA, HD, FHD, 4K, or [WIDTH]x[HEIGHT]) --framerate INT:INT in [1 - 60] Video framerate --fixed-resolution Maintain video resolution in degradation --priority TEXT:{BALANCE,FRAMERATE,RESOLUTION} - Preference in video degradation (experimental) + Specifies the quality that is maintained against video degradation --use-sdl Show video using SDL (if SDL is available) - --show-me Show self video (if SDL is available) --window-width INT:INT in [180 - 16384] Window width for videos (if SDL is available) --window-height INT:INT in [180 - 16384] @@ -121,23 +123,22 @@ Options: --disable-auto-gain-control Disable auto gain control for audio --disable-noise-suppression Disable noise suppression for audio --disable-highpass-filter Disable highpass filter for audio - --disable-typing-detection Disable typing detection for audio --video-codec-engines List available video encoders/decoders - --vp8-encoder :value in {default->0,software->6} OR {0,6} + --vp8-encoder ENUM:value in {default->0,software->7} OR {0,7} VP8 Encoder - --vp8-decoder :value in {default->0,software->6} OR {0,6} + --vp8-decoder ENUM:value in {default->0,software->7} OR {0,7} VP8 Decoder - --vp9-encoder :value in {default->0,software->6} OR {0,6} + --vp9-encoder ENUM:value in {default->0,software->7} OR {0,7} VP9 Encoder - --vp9-decoder :value in {default->0,software->6} OR {0,6} + --vp9-decoder ENUM:value in {default->0,software->7} OR {0,7} VP9 Decoder - --av1-encoder :value in {default->0,software->6} OR {0,6} + --av1-encoder ENUM:value in {default->0,software->7} OR {0,7} AV1 Encoder - --av1-decoder :value in {default->0,software->6} OR {0,6} + --av1-decoder ENUM:value in {default->0,software->7} OR {0,7} AV1 Decoder - --h264-encoder :value in {default->0,videotoolbox->5} OR {0,5} + --h264-encoder ENUM:value in {default->0,videotoolbox->5} OR {0,5} H.264 Encoder - --h264-decoder :value in {default->0,videotoolbox->5} OR {0,5} + --h264-decoder ENUM:value in {default->0,videotoolbox->5} OR {0,5} H.264 Decoder --serial TEXT:serial setting format Serial port settings for datachannel passthrough [DEVICE],[BAUDRATE] @@ -187,7 +188,6 @@ H264: ### test モードヘルプ - ``` $ ./momo test --help Mode for momo development with simple HTTP server @@ -203,7 +203,6 @@ Options: ### ayame モードヘルプ - ``` $ ./momo ayame --help Mode for working with WebRTC Signaling Server Ayame @@ -224,23 +223,23 @@ Options: ``` $ ./momo sora --help Mode for working with WebRTC SFU Sora -Usage: ./momo sora [OPTIONS] SIGNALING-URL CHANNEL-ID - -Positionals: - SIGNALING-URL TEXT REQUIRED Signaling URL - CHANNEL-ID TEXT REQUIRED Channel ID +Usage: ./momo sora [OPTIONS] Options: -h,--help Print this help message and exit --help-all Print help message for all modes and exit + --signaling-url TEXT ... REQUIRED + Signaling URLs + --channel-id TEXT REQUIRED Channel ID --auto Connect to Sora automatically --video BOOLEAN:value in {false->0,true->1} OR {0,1} Send video to sora (default: true) --audio BOOLEAN:value in {false->0,true->1} OR {0,1} Send audio to sora (default: true) - --video-codec-type TEXT:{,AV1,H264,VP8,VP9} + --video-codec-type TEXT:{VP8,VP9,AV1,H264} Video codec for send - --audio-codec-type TEXT:{,OPUS} + --audio-codec-type TEXT:{OPUS} + Audio codec for send --video-bit-rate INT:INT in [0 - 30000] Video bit rate --audio-bit-rate INT:INT in [0 - 510] @@ -255,11 +254,11 @@ Options: Port number (default: -1) --simulcast BOOLEAN:value in {false->0,true->1} OR {0,1} Use simulcast (default: false) - --data-channel-signaling TEXT:value in {false-> 0,true-> 1,none->--} OR { 0, 1,--} + --data-channel-signaling TEXT:{true,false,none} Use DataChannel for Sora signaling (default: none) --data-channel-signaling-timeout INT:POSITIVE Timeout for Data Channel in seconds (default: 180) - --ignore-disconnect-websocket TEXT:value in {false-> 0,true-> 1,none->--} OR { 0, 1,--} + --ignore-disconnect-websocket TEXT:{true,false,none} Ignore WebSocket disconnection if using Data Channel (default: none) --disconnect-wait-timeout INT:POSITIVE Disconnecting timeout for Data Channel in seconds (default: 5) @@ -269,3 +268,5 @@ Options: ## うまく動作しない時 - カメラを uv4l など他のプロセスが利用していないか確認してください +- 既知の問題ではないか確認してください + - [既知の問題](https://github.com/shiguredo/momo/issues/89) diff --git a/doc/USE_SDL.md b/doc/USE_SDL.md index 94953342..ba05341c 100644 --- a/doc/USE_SDL.md +++ b/doc/USE_SDL.md @@ -11,15 +11,11 @@ SDL (Simple DirectMedia Layer) を利用することで、 Momo 自体が受信 - この機能は ayame と sora モードでのみ利用できます - test モードでは test.html が HTTPS ではないため getUserMedia を使用できません - この機能は Windows または macOS または Linux で利用できます -- この機能を以下の組み合わせで利用するとパフォーマンスに影響が出るため避けてください - - --hw-mjpeg-decoder=true と --show-me を併用する組み合わせ ## SDL コマンド引数 - --use-sdl - SDL 機能を使う場合は指定します -- --show-me - - Momo が取得した映像を出力します - --window-width - 映像を表示するウインドウの横幅を指定します - --window-height @@ -39,7 +35,7 @@ SDL (Simple DirectMedia Layer) を利用することで、 Momo 自体が受信 - ルーム ID を推測されにくい値に変更して下さい ``` -./momo --resolution VGA --no-audio-device --use-sdl --show-me ayame --signaling-url wss://ayame-labo.shiguredo.jp/signaling --room-id momo-sdl-ayame +./momo --resolution VGA --no-audio-device --use-sdl ayame --signaling-url wss://ayame-labo.shiguredo.jp/signaling --room-id momo-sdl-ayame ``` [![Image from Gyazo](https://i.gyazo.com/8ca80e9b60c9e848e04afcefd86a2c07.png)](https://gyazo.com/8ca80e9b60c9e848e04afcefd86a2c07) @@ -50,7 +46,7 @@ SDL (Simple DirectMedia Layer) を利用することで、 Momo 自体が受信 - Signaling サーバの URL はダミーです ``` -./momo --resolution VGA --no-audio-device --use-sdl --show-me sora --video-codec-type VP8 --video-bit-rate 1000 --audio false --signaling-url wss://example.com/signaling --channel-id momo-sdl-sora +./momo --resolution VGA --no-audio-device --use-sdl sora --video-codec-type VP8 --video-bit-rate 1000 --audio false --signaling-url wss://example.com/signaling --channel-id momo-sdl-sora ``` [![Image from Gyazo](https://i.gyazo.com/abdb1802bd66440ef32e75da6842f0cf.png)](https://gyazo.com/abdb1802bd66440ef32e75da6842f0cf) diff --git a/doc/USE_SORA.md b/doc/USE_SORA.md index 259d971a..5f31229b 100644 --- a/doc/USE_SORA.md +++ b/doc/USE_SORA.md @@ -14,11 +14,11 @@ GitHub アカウントを用意して https://sora-labo.shiguredo.app/ にサイ ### 片方向配信をしてみる -- チャネル名に `<好きな文字列>@#` を指定してください - - 好きな文字列が sora 、GitHub ID が 0 、 GitHub ユーザ名が shiguredo とした場合は `sora@shiguredo#0` のように指定してください - - ここではチャネル ID を `sora@shiguredo#0` とします +- チャネル名に `__<好きな文字列>` を指定してください + - 好きな文字列が sora 、GitHub ID が 0 、 GitHub ユーザ名が shiguredo とした場合は `shiguredo_0_sora` のように指定してください + - ここではチャネル ID を `shiguredo_0_sora` とします - sora モードのオプションである --metadata を利用し生成したアクセストークンを `access_token` で指定します - - SoraLabo Home のアクセストークン生成にて先程チャネル名で指定した `<好きな文字列>` を入力してアクセストークンを生成してください + - SoraLabo Home のアクセストークン生成にて先程の `<チャネル名>` を入力してアクセストークンを生成してください - この指定は商用の Sora を利用する場合は不要です。Sora Labo 専用の機能になります - ここではアクセストークンを `xyz` とします。 @@ -26,10 +26,8 @@ GitHub アカウントを用意して https://sora-labo.shiguredo.app/ にサイ ./momo --no-audio-device \ sora \ --signaling-url \ - wss://0001.canary.sora-labo.shiguredo.app/signaling \ - wss://0002.canary.sora-labo.shiguredo.app/signaling \ - wss://0003.canary.sora-labo.shiguredo.app/signaling \ - --channel-id sora@shiguredo#0 \ + wss://canary.sora-labo.shiguredo.app/signaling \ + --channel-id shiguredo_0_sora \ --video-codec-type VP8 --video-bit-rate 500 \ --audio false \ --role sendonly --metadata '{"access_token": "xyz"}' @@ -42,13 +40,11 @@ GitHub アカウントを用意して https://sora-labo.shiguredo.app/ にサイ GUI 環境で Momo を利用すると、 SDL を利用し音声や映像の受信が可能になります。 ```shell -./momo --resolution VGA --no-audio-device --use-sdl --show-me \ +./momo --resolution VGA --no-audio-device --use-sdl \ sora \ --signaling-url \ - wss://0001.canary.sora-labo.shiguredo.app/signaling \ - wss://0002.canary.sora-labo.shiguredo.app/signaling \ - wss://0003.canary.sora-labo.shiguredo.app/signaling \ - --channel-id sora@shiguredo#0 \ + wss://canary.sora-labo.shiguredo.app/signaling \ + --channel-id shiguredo_0_sora \ --video-codec-type VP8 --video-bit-rate 1000 \ --audio false \ --role sendrecv --metadata '{"access_token": "xyz"}' @@ -62,10 +58,8 @@ GUI 環境で Momo を利用すると、 SDL を利用し音声や映像の受 ./momo --no-audio-device \ sora \ --signaling-url \ - wss://0001.canary.sora-labo.shiguredo.app/signaling \ - wss://0002.canary.sora-labo.shiguredo.app/signaling \ - wss://0003.canary.sora-labo.shiguredo.app/signaling \ - --channel-id sora@shiguredo#0 \ + wss://canary.sora-labo.shiguredo.app/signaling \ + --channel-id shiguredo_0_sora \ --video-codec-type VP8 --video-bit-rate 500 \ --audio false \ --simulcast true \ diff --git a/doc/USE_TEST.md b/doc/USE_TEST.md index 546442ac..7492e5a8 100644 --- a/doc/USE_TEST.md +++ b/doc/USE_TEST.md @@ -27,13 +27,13 @@ SDL についての詳細は [USE_SDL.md](USE_SDL.md) をお読みください Momo 1: ```shell -./momo --use-sdl --show-me test +./momo --use-sdl test ``` Momo 2: ```shell -./momo --use-sdl --show-me ayame --signaling-url ws://[Momo 1のIPアドレス]:8080/ws --room-id test +./momo --use-sdl ayame --signaling-url ws://[Momo 1 の IP アドレス]:8080/ws --room-id test ``` Google STUN を利用したくない場合は`--no-google-stun`をオプションを追加することで可能になります。 @@ -41,13 +41,13 @@ Google STUN を利用したくない場合は`--no-google-stun`をオプショ Momo 1: ```shell -./momo --no-google-stun --use-sdl --show-me test +./momo --no-google-stun --use-sdl test ``` Momo 2: ```shell -./momo --no-google-stun --use-sdl --show-me ayame --signaling-url ws://[Momo 1のIPアドレス]:8080/ws --room-id test +./momo --no-google-stun --use-sdl ayame --signaling-url ws://[Momo 1 の IP アドレス]:8080/ws --room-id test ``` 配信がうまくいくとそれぞれのマシンにお互いの映像と音声が出力されます。 diff --git a/patch/msdk_limits.patch b/patch/msdk_limits.patch new file mode 100644 index 00000000..13fc9b33 --- /dev/null +++ b/patch/msdk_limits.patch @@ -0,0 +1,15 @@ +# https://github.com/Intel-Media-SDK/MediaSDK/commit/aa4dfc524e1dd4cb33a88fa453de1eef50a39ed7 +# Ubuntu 22.04 上でのビルドでエラーになったのでこのコミットを適用する + +diff --git a/_studio/mfx_lib/mctf_package/mctf/src/mctf_common.cpp b/_studio/mfx_lib/mctf_package/mctf/src/mctf_common.cpp +index 49f114c792..b2fb722872 100644 +--- a/_studio/mfx_lib/mctf_package/mctf/src/mctf_common.cpp ++++ b/_studio/mfx_lib/mctf_package/mctf/src/mctf_common.cpp +@@ -44,6 +44,7 @@ + #include + #include + #include ++#include + #include "cmrt_cross_platform.h" + + using std::min; \ No newline at end of file diff --git a/script/apt_install_arm.sh b/script/apt_install_arm.sh index 4f62853d..c4ef912b 100755 --- a/script/apt_install_arm.sh +++ b/script/apt_install_arm.sh @@ -8,7 +8,7 @@ apt-get -y install tzdata echo 'Asia/Tokyo' > /etc/timezone dpkg-reconfigure -f noninteractive tzdata -apt-get -y install \ +DEBIAN_FRONTEND=noninteractive apt-get -y install \ binutils-aarch64-linux-gnu \ binutils-arm-linux-gnueabi \ binutils-arm-linux-gnueabihf \ diff --git a/script/apt_install_x86_64.sh b/script/apt_install_x86_64.sh index 03974adf..471c33ab 100755 --- a/script/apt_install_x86_64.sh +++ b/script/apt_install_x86_64.sh @@ -9,7 +9,7 @@ echo 'Asia/Tokyo' > /etc/timezone dpkg-reconfigure -f noninteractive tzdata # libtinfo5 は Ubuntu 20.04 のために入れたが将来的に不要になる可能性がある -apt-get -y install \ +DEBIAN_FRONTEND=noninteractive apt-get -y install \ autoconf \ automake \ build-essential \ @@ -27,8 +27,8 @@ apt-get -y install \ libudev-dev \ libxrandr-dev \ lsb-release \ - python \ - python-dev \ + python3 \ + python3-dev \ rsync \ sudo \ vim \ diff --git a/src/ayame/ayame_client.cpp b/src/ayame/ayame_client.cpp index 6c8146dc..91821f7f 100644 --- a/src/ayame/ayame_client.cpp +++ b/src/ayame/ayame_client.cpp @@ -29,9 +29,13 @@ bool AyameClient::ParseURL(URLParts& parts) const { void AyameClient::GetStats( std::function&)> callback) { - if (connection_ && rtc_state_ == + if (connection_ && (rtc_state_ == webrtc::PeerConnectionInterface::IceConnectionState:: - kIceConnectionConnected) { + kIceConnectionConnected || + rtc_state_ == + webrtc::PeerConnectionInterface::IceConnectionState:: + kIceConnectionCompleted + )) { connection_->GetStats(std::move(callback)); } else { callback(nullptr); diff --git a/src/dyn/dyn.h b/src/dyn/dyn.h index 79ea7841..3b65df63 100644 --- a/src/dyn/dyn.h +++ b/src/dyn/dyn.h @@ -1,6 +1,7 @@ #ifndef DYN_DYN_H_ #define DYN_DYN_H_ +#include #include #include #include @@ -117,6 +118,8 @@ class DynModule { auto f = \ (func_type)DynModule::Instance().GetFunc(soname, DYN_STRINGIZE(func)); \ if (f == nullptr) { \ + std::cerr << "Failed to load function " DYN_STRINGIZE(func) " in " \ + << soname << std::endl; \ exit(1); \ } \ return f(args...); \ diff --git a/src/hwenc_jetson/jetson_jpeg_decoder_pool.cpp b/src/hwenc_jetson/jetson_jpeg_decoder_pool.cpp index 451fe5bc..2050e3c8 100644 --- a/src/hwenc_jetson/jetson_jpeg_decoder_pool.cpp +++ b/src/hwenc_jetson/jetson_jpeg_decoder_pool.cpp @@ -6,14 +6,16 @@ std::shared_ptr JetsonJpegDecoderPool::Pop() { std::unique_ptr nv_decoder; + // プールを使うとなぜか実行時にクラッシュすることがあるのでコメントアウト + // 多分 nvjpeg のバグ { - std::lock_guard lock(mtx_); - if (decoder_queue_.size() == 0) { - nv_decoder.reset(NvJPEGDecoder::createJPEGDecoder("jpegdec")); - } else { - nv_decoder = std::move(decoder_queue_.front()); - decoder_queue_.pop(); - } + //std::lock_guard lock(mtx_); + //if (decoder_queue_.size() == 0) { + nv_decoder.reset(NvJPEGDecoder::createJPEGDecoder("jpegdec")); + //} else { + // nv_decoder = std::move(decoder_queue_.front()); + // decoder_queue_.pop(); + //} } std::shared_ptr decoder( @@ -23,5 +25,5 @@ std::shared_ptr JetsonJpegDecoderPool::Pop() { void JetsonJpegDecoderPool::Push(std::unique_ptr decoder) { std::lock_guard lock(mtx_); - decoder_queue_.push(std::move(decoder)); + //decoder_queue_.push(std::move(decoder)); } \ No newline at end of file diff --git a/src/hwenc_jetson/jetson_v4l2_capturer.cpp b/src/hwenc_jetson/jetson_v4l2_capturer.cpp index 3195a363..a344ffd3 100644 --- a/src/hwenc_jetson/jetson_v4l2_capturer.cpp +++ b/src/hwenc_jetson/jetson_v4l2_capturer.cpp @@ -1,17 +1,43 @@ #include "jetson_v4l2_capturer.h" +// C +#include +#include +#include + +// C++ +#include +#include + // Linux +#include +#include +#include #include +#include +#include +#include // WebRTC +#include +#include +#include +#include #include #include +#include +#include + +// L4T Multimedia API +#include #include "jetson_buffer.h" -rtc::scoped_refptr JetsonV4L2Capturer::Create( - V4L2VideoCapturerConfig config) { - rtc::scoped_refptr capturer; +#define MJPEG_EOS_SEARCH_SIZE 4096 + +rtc::scoped_refptr JetsonV4L2Capturer::Create( + const V4L2VideoCapturerConfig& config) { + rtc::scoped_refptr capturer; std::unique_ptr device_info( webrtc::VideoCaptureFactory::CreateDeviceInfo()); if (!device_info) { @@ -32,59 +58,492 @@ rtc::scoped_refptr JetsonV4L2Capturer::Create( return nullptr; } -bool JetsonV4L2Capturer::UseNativeBuffer() { - return true; +void JetsonV4L2Capturer::LogDeviceList( + webrtc::VideoCaptureModule::DeviceInfo* device_info) { + for (int i = 0; i < device_info->NumberOfDevices(); ++i) { + char device_name[256]; + char unique_name[256]; + if (device_info->GetDeviceName(static_cast(i), device_name, + sizeof(device_name), unique_name, + sizeof(unique_name)) != 0) { + RTC_LOG(LS_WARNING) << "Failed to GetDeviceName(" << i << ")"; + continue; + } + RTC_LOG(LS_INFO) << "GetDeviceName(" << i + << "): device_name=" << device_name + << ", unique_name=" << unique_name; + } } -rtc::scoped_refptr JetsonV4L2Capturer::Create( +rtc::scoped_refptr JetsonV4L2Capturer::Create( webrtc::VideoCaptureModule::DeviceInfo* device_info, - V4L2VideoCapturerConfig config, + const V4L2VideoCapturerConfig& config, size_t capture_device_index) { char device_name[256]; char unique_name[256]; - if (device_info->GetDeviceName(static_cast(capture_device_index), device_name, sizeof(device_name), unique_name, sizeof(unique_name)) != 0) { RTC_LOG(LS_WARNING) << "Failed to GetDeviceName"; return nullptr; } - - rtc::scoped_refptr v4l2_capturer( - new rtc::RefCountedObject()); - + rtc::scoped_refptr v4l2_capturer = + rtc::make_ref_counted(config); if (v4l2_capturer->Init((const char*)&unique_name, config.video_device) < 0) { RTC_LOG(LS_WARNING) << "Failed to create JetsonV4L2Capturer(" << unique_name << ")"; return nullptr; } - if (v4l2_capturer->StartCapture(config) < 0) { RTC_LOG(LS_WARNING) << "Failed to start JetsonV4L2Capturer(w = " << config.width << ", h = " << config.height << ", fps = " << config.framerate << ")"; return nullptr; } - return v4l2_capturer; } +JetsonV4L2Capturer::JetsonV4L2Capturer(const V4L2VideoCapturerConfig& config) + : ScalableVideoTrackSource(config), + _deviceFd(-1), + _buffersAllocatedByDevice(-1), + _currentWidth(-1), + _currentHeight(-1), + _currentFrameRate(-1), + _captureStarted(false), + _captureVideoType(webrtc::VideoType::kI420), + _pool(NULL) {} + +bool JetsonV4L2Capturer::FindDevice(const char* deviceUniqueIdUTF8, + const std::string& device) { + int fd; + if ((fd = open(device.c_str(), O_RDONLY)) != -1) { + // query device capabilities + struct v4l2_capability cap; + if (ioctl(fd, VIDIOC_QUERYCAP, &cap) == 0) { + if (cap.bus_info[0] != 0) { + if (strncmp((const char*)cap.bus_info, (const char*)deviceUniqueIdUTF8, + strlen((const char*)deviceUniqueIdUTF8)) == + 0) // match with device id + { + close(fd); + return true; + } + } + } + close(fd); // close since this is not the matching device + } + return false; +} + +int32_t JetsonV4L2Capturer::Init(const char* deviceUniqueIdUTF8, + const std::string& specifiedVideoDevice) { + int fd; + bool found = false; + + if (!specifiedVideoDevice.empty()) { + // specifiedVideoDevice が指定されてる場合はそれだけ調べる + if (FindDevice(deviceUniqueIdUTF8, specifiedVideoDevice)) { + found = true; + _videoDevice = specifiedVideoDevice; + } + } else { + // specifiedVideoDevice が指定されてない場合は頑張って探す + /* detect /dev/video [0-63] entries */ + char device[32]; + int n; + for (n = 0; n < 64; n++) { + sprintf(device, "/dev/video%d", n); + if (FindDevice(deviceUniqueIdUTF8, device)) { + found = true; + _videoDevice = device; // store the video device + break; + } + } + } + + if (!found) { + RTC_LOG(LS_INFO) << "no matching device found"; + return -1; + } + return 0; +} + +JetsonV4L2Capturer::~JetsonV4L2Capturer() { + StopCapture(); + if (_deviceFd != -1) + close(_deviceFd); +} + +int32_t JetsonV4L2Capturer::StartCapture( + const V4L2VideoCapturerConfig& config) { + if (_captureStarted) { + if (config.width == _currentWidth && config.height == _currentHeight) { + return 0; + } else { + StopCapture(); + } + } + + webrtc::MutexLock lock(&capture_lock_); + // first open /dev/video device + if ((_deviceFd = open(_videoDevice.c_str(), O_RDWR | O_NONBLOCK, 0)) < 0) { + RTC_LOG(LS_INFO) << "error in opening " << _videoDevice + << " errono = " << errno; + return -1; + } + + // Supported video formats in preferred order. + // If the requested resolution is larger than VGA, we prefer MJPEG. Go for + // I420 otherwise. + const int nFormats = 6; + unsigned int fmts[nFormats] = {}; + if (config.use_native) { + fmts[0] = V4L2_PIX_FMT_MJPEG; + fmts[1] = V4L2_PIX_FMT_JPEG; + } else if (!config.force_i420 && + (config.width > 640 || config.height > 480)) { + fmts[0] = V4L2_PIX_FMT_MJPEG; + fmts[1] = V4L2_PIX_FMT_YUV420; + fmts[2] = V4L2_PIX_FMT_YVU420; + fmts[3] = V4L2_PIX_FMT_YUYV; + fmts[4] = V4L2_PIX_FMT_UYVY; + fmts[5] = V4L2_PIX_FMT_JPEG; + } else { + fmts[0] = V4L2_PIX_FMT_YUV420; + fmts[1] = V4L2_PIX_FMT_YVU420; + fmts[2] = V4L2_PIX_FMT_YUYV; + fmts[3] = V4L2_PIX_FMT_UYVY; + fmts[4] = V4L2_PIX_FMT_MJPEG; + fmts[5] = V4L2_PIX_FMT_JPEG; + } + + // Enumerate image formats. + struct v4l2_fmtdesc fmt; + int fmtsIdx = nFormats; + memset(&fmt, 0, sizeof(fmt)); + fmt.index = 0; + fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + RTC_LOG(LS_INFO) << "Video Capture enumerats supported image formats:"; + while (ioctl(_deviceFd, VIDIOC_ENUM_FMT, &fmt) == 0) { + RTC_LOG(LS_INFO) << " { pixelformat = " + << cricket::GetFourccName(fmt.pixelformat) + << ", description = '" << fmt.description << "' }"; + // Match the preferred order. + for (int i = 0; i < nFormats; i++) { + if (fmt.pixelformat == fmts[i] && i < fmtsIdx) + fmtsIdx = i; + } + // Keep enumerating. + fmt.index++; + } + + if (fmtsIdx == nFormats) { + RTC_LOG(LS_INFO) << "no supporting video formats found"; + return -1; + } else { + RTC_LOG(LS_INFO) << "We prefer format " + << cricket::GetFourccName(fmts[fmtsIdx]); + } + + struct v4l2_format video_fmt; + memset(&video_fmt, 0, sizeof(struct v4l2_format)); + video_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + video_fmt.fmt.pix.sizeimage = 0; + video_fmt.fmt.pix.width = config.width; + video_fmt.fmt.pix.height = config.height; + video_fmt.fmt.pix.pixelformat = fmts[fmtsIdx]; + + // set format and frame size now + if (ioctl(_deviceFd, VIDIOC_S_FMT, &video_fmt) < 0) { + RTC_LOG(LS_INFO) << "error in VIDIOC_S_FMT, errno = " << errno; + return -1; + } + + // initialize current width and height + _currentWidth = video_fmt.fmt.pix.width; + _currentHeight = video_fmt.fmt.pix.height; + _currentPixelFormat = video_fmt.fmt.pix.pixelformat; + + if (video_fmt.fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV) + _captureVideoType = webrtc::VideoType::kYUY2; + else if (video_fmt.fmt.pix.pixelformat == V4L2_PIX_FMT_YUV420) + _captureVideoType = webrtc::VideoType::kI420; + else if (video_fmt.fmt.pix.pixelformat == V4L2_PIX_FMT_YVU420) + _captureVideoType = webrtc::VideoType::kYV12; + else if (video_fmt.fmt.pix.pixelformat == V4L2_PIX_FMT_UYVY) + _captureVideoType = webrtc::VideoType::kUYVY; + else if (video_fmt.fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG || + video_fmt.fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG) + _captureVideoType = webrtc::VideoType::kMJPEG; + + // Trying to set frame rate, before check driver capability. + bool driver_framerate_support = true; + struct v4l2_streamparm streamparms; + memset(&streamparms, 0, sizeof(streamparms)); + streamparms.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + if (ioctl(_deviceFd, VIDIOC_G_PARM, &streamparms) < 0) { + RTC_LOG(LS_INFO) << "error in VIDIOC_G_PARM errno = " << errno; + driver_framerate_support = false; + // continue + } else { + // check the capability flag is set to V4L2_CAP_TIMEPERFRAME. + if (streamparms.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) { + // driver supports the feature. Set required framerate. + memset(&streamparms, 0, sizeof(streamparms)); + streamparms.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + streamparms.parm.capture.timeperframe.numerator = 1; + streamparms.parm.capture.timeperframe.denominator = config.framerate; + if (ioctl(_deviceFd, VIDIOC_S_PARM, &streamparms) < 0) { + RTC_LOG(LS_INFO) << "Failed to set the framerate. errno=" << errno; + driver_framerate_support = false; + } else { + _currentFrameRate = config.framerate; + } + } + } + // If driver doesn't support framerate control, need to hardcode. + // Hardcoding the value based on the frame size. + if (!driver_framerate_support) { + if (!config.use_native && _currentWidth >= 800 && + _captureVideoType != webrtc::VideoType::kMJPEG) { + _currentFrameRate = 15; + } else { + _currentFrameRate = 30; + } + } + + if (!AllocateVideoBuffers()) { + RTC_LOG(LS_INFO) << "failed to allocate video capture buffers"; + return -1; + } + + // start capture thread; + if (_captureThread.empty()) { + quit_ = false; + _captureThread = rtc::PlatformThread::SpawnJoinable( + std::bind(JetsonV4L2Capturer::CaptureThread, this), "CaptureThread", + rtc::ThreadAttributes().SetPriority(rtc::ThreadPriority::kHigh)); + } + + // Needed to start UVC camera - from the uvcview application + enum v4l2_buf_type type; + type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + if (ioctl(_deviceFd, VIDIOC_STREAMON, &type) == -1) { + RTC_LOG(LS_INFO) << "Failed to turn on stream"; + return -1; + } + + _captureStarted = true; + return 0; +} + +int32_t JetsonV4L2Capturer::StopCapture() { + if (!_captureThread.empty()) { + { + webrtc::MutexLock lock(&capture_lock_); + quit_ = true; + } + _captureThread.Finalize(); + } + + webrtc::MutexLock lock(&capture_lock_); + if (_captureStarted) { + _captureStarted = false; + + DeAllocateVideoBuffers(); + close(_deviceFd); + _deviceFd = -1; + } + + return 0; +} + +// critical section protected by the caller + bool JetsonV4L2Capturer::AllocateVideoBuffers() { - bool result = V4L2VideoCapturer::AllocateVideoBuffers(); - if (result && _captureVideoType == webrtc::VideoType::kMJPEG) { - std::shared_ptr jpeg_decoder_pool( - new JetsonJpegDecoderPool()); - jpeg_decoder_pool_ = jpeg_decoder_pool; + struct v4l2_requestbuffers rbuffer; + memset(&rbuffer, 0, sizeof(v4l2_requestbuffers)); + + rbuffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + rbuffer.memory = V4L2_MEMORY_MMAP; + rbuffer.count = kNoOfV4L2Bufffers; + + if (ioctl(_deviceFd, VIDIOC_REQBUFS, &rbuffer) < 0) { + RTC_LOG(LS_INFO) << "Could not get buffers from device. errno = " << errno; + return false; } - return result; + + if (rbuffer.count > kNoOfV4L2Bufffers) + rbuffer.count = kNoOfV4L2Bufffers; + + _buffersAllocatedByDevice = rbuffer.count; + + std::unique_ptr fds; + if (_captureVideoType != webrtc::VideoType::kMJPEG) { + fds.reset(new int[rbuffer.count]); + NvBufSurf::NvCommonAllocateParams params = {0}; + + params.memType = NVBUF_MEM_SURFACE_ARRAY; + params.width = _currentWidth; + params.height = _currentHeight; + params.layout = NVBUF_LAYOUT_PITCH; + if (_captureVideoType == webrtc::VideoType::kYUY2) + params.colorFormat = NVBUF_COLOR_FORMAT_YUYV; + else if (_captureVideoType == webrtc::VideoType::kI420) + params.colorFormat = NVBUF_COLOR_FORMAT_YUV420; + else if (_captureVideoType == webrtc::VideoType::kYV12) + params.colorFormat = NVBUF_COLOR_FORMAT_YVU420; + else if (_captureVideoType == webrtc::VideoType::kUYVY) + params.colorFormat = NVBUF_COLOR_FORMAT_UYVY; + params.memtag = NvBufSurfaceTag_CAMERA; + if (NvBufSurf::NvAllocate(¶ms, rbuffer.count, fds.get())) { + return false; + } + } + + // Map the buffers + _pool = new Buffer[rbuffer.count]; + + for (unsigned int i = 0; i < rbuffer.count; i++) { + struct v4l2_buffer buffer; + memset(&buffer, 0, sizeof(v4l2_buffer)); + if (fds == nullptr) { + buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + buffer.memory = V4L2_MEMORY_MMAP; + buffer.index = i; + + if (ioctl(_deviceFd, VIDIOC_QUERYBUF, &buffer) < 0) { + return false; + } + + _pool[i].start = mmap(NULL, buffer.length, PROT_READ | PROT_WRITE, + MAP_SHARED, _deviceFd, buffer.m.offset); + + if (MAP_FAILED == _pool[i].start) { + for (unsigned int j = 0; j < i; j++) + munmap(_pool[j].start, _pool[j].length); + return false; + } + + _pool[i].length = buffer.length; + } else { + buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + buffer.memory = V4L2_MEMORY_DMABUF; + buffer.index = i; + + if (ioctl(_deviceFd, VIDIOC_QUERYBUF, &buffer) < 0) { + return false; + } + + _pool[i].fd = fds[i]; + _pool[i].length = buffer.length; + } + + if (ioctl(_deviceFd, VIDIOC_QBUF, &buffer) < 0) { + return false; + } + } + + if (_captureVideoType == webrtc::VideoType::kMJPEG) { + jpeg_decoder_pool_.reset(new JetsonJpegDecoderPool()); + } + + return true; } bool JetsonV4L2Capturer::DeAllocateVideoBuffers() { - jpeg_decoder_pool_ = nullptr; - return V4L2VideoCapturer::DeAllocateVideoBuffers(); + if (_captureVideoType == webrtc::VideoType::kMJPEG) { + jpeg_decoder_pool_ = nullptr; + + // unmap buffers + for (int i = 0; i < _buffersAllocatedByDevice; i++) { + munmap(_pool[i].start, _pool[i].length); + } + delete[] _pool; + + } else { + for (int i = 0; i < _buffersAllocatedByDevice; i++) { + NvBufSurf::NvDestroy(_pool[i].fd); + } + delete[] _pool; + } + + // turn off stream + enum v4l2_buf_type type; + type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + if (ioctl(_deviceFd, VIDIOC_STREAMOFF, &type) < 0) { + RTC_LOG(LS_INFO) << "VIDIOC_STREAMOFF error. errno: " << errno; + } + + return true; } -void JetsonV4L2Capturer::OnCaptured(uint8_t* data, uint32_t bytesused) { +void JetsonV4L2Capturer::CaptureThread(void* obj) { + JetsonV4L2Capturer* capturer = static_cast(obj); + while (capturer->CaptureProcess()) { + } +} + +bool JetsonV4L2Capturer::CaptureProcess() { + int retVal = 0; + fd_set rSet; + struct timeval timeout; + + FD_ZERO(&rSet); + FD_SET(_deviceFd, &rSet); + timeout.tv_sec = 1; + timeout.tv_usec = 0; + + // _deviceFd written only in StartCapture, when this thread isn't running. + retVal = select(_deviceFd + 1, &rSet, NULL, NULL, &timeout); + { + webrtc::MutexLock lock(&capture_lock_); + + if (quit_) { + return false; + } else if (retVal < 0 && errno != EINTR /* continue if interrupted */) { + // select failed + return false; + } else if (retVal == 0) { + // select timed out + return true; + } else if (!FD_ISSET(_deviceFd, &rSet)) { + // not event on camera handle + return true; + } + + if (_captureStarted) { + struct v4l2_buffer buf; + memset(&buf, 0, sizeof(struct v4l2_buffer)); + buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + if (_captureVideoType == webrtc::VideoType::kMJPEG) { + buf.memory = V4L2_MEMORY_MMAP; + } else { + buf.memory = V4L2_MEMORY_DMABUF; + } + // dequeue a buffer - repeat until dequeued properly! + while (ioctl(_deviceFd, VIDIOC_DQBUF, &buf) < 0) { + if (errno != EINTR) { + RTC_LOG(LS_INFO) << "could not sync on a buffer on device " + << strerror(errno); + return true; + } + } + + OnCaptured(&buf); + + // enqueue the buffer again + if (ioctl(_deviceFd, VIDIOC_QBUF, &buf) == -1) { + RTC_LOG(LS_INFO) << __FUNCTION__ << " Failed to enqueue capture buffer"; + } + } + } + usleep(0); + return true; +} + +void JetsonV4L2Capturer::OnCaptured(v4l2_buffer* buf) { const int64_t timestamp_us = rtc::TimeMicros(); int adapted_width, adapted_height, crop_width, crop_height, crop_x, crop_y; if (!AdaptFrame(_currentWidth, _currentHeight, timestamp_us, &adapted_width, @@ -94,14 +553,47 @@ void JetsonV4L2Capturer::OnCaptured(uint8_t* data, uint32_t bytesused) { } if (_captureVideoType == webrtc::VideoType::kMJPEG) { - auto decoder = jpeg_decoder_pool_->Pop(); + uint8_t* data = (uint8_t*)_pool[buf->index].start; + uint32_t bytesused = buf->bytesused; + // 一部のカメラ (DELL WB7022) は不正なデータを送ってくることがある。 + // これをハードウェアJPEGデコーダーに送ると Momo ごとクラッシュしてしまう。 + // JPEG の先頭は SOI マーカー 0xffd8 で始まるのでチェックして落ちないようにする。 + if (bytesused < 2 || data[0] != 0xff || data[1] != 0xd8) { + RTC_LOG(LS_WARNING) << __FUNCTION__ + << " Invalid JPEG buffer frame skipped"; + return; + } + + unsigned int eosSearchSize = MJPEG_EOS_SEARCH_SIZE; + uint8_t* p; + /* v4l2_buf.bytesused may have padding bytes for alignment + Search for EOF to get exact size */ + if (eosSearchSize > bytesused) + eosSearchSize = bytesused; + bool found = false; + for (unsigned int i = 0; i < eosSearchSize; i++) { + p = data + bytesused; + if ((*(p - 2) == 0xff) && (*(p - 1) == 0xd9)) { + found = true; + break; + } + bytesused--; + } + if (!found) { + RTC_LOG(LS_WARNING) << __FUNCTION__ + << " Invalid JPEG buffer frame skipped"; + return; + } + + std::shared_ptr decoder = jpeg_decoder_pool_->Pop(); int fd = 0; uint32_t width, height, pixfmt; + RTC_LOG(LS_INFO) << "data: " << (void*)data << " (" << (int)data[0] << "," + << (int)data[1] << ") bytesused: " << bytesused; if (decoder->DecodeToFd(fd, data, bytesused, pixfmt, width, height) < 0) { RTC_LOG(LS_ERROR) << "decodeToFd Failed"; return; } - rtc::scoped_refptr jetson_buffer( JetsonBuffer::Create(_captureVideoType, width, height, adapted_width, adapted_height, fd, pixfmt, std::move(decoder))); @@ -112,13 +604,10 @@ void JetsonV4L2Capturer::OnCaptured(uint8_t* data, uint32_t bytesused) { .set_timestamp_us(rtc::TimeMicros()) .set_rotation(webrtc::kVideoRotation_0) .build()); - } else { - rtc::scoped_refptr jetson_buffer( - JetsonBuffer::Create(_captureVideoType, _currentWidth, _currentHeight, - adapted_width, adapted_height)); - memcpy(jetson_buffer->Data(), data, bytesused); - jetson_buffer->SetLength(bytesused); + rtc::scoped_refptr jetson_buffer(JetsonBuffer::Create( + _captureVideoType, _currentWidth, _currentHeight, adapted_width, + adapted_height, _pool[buf->index].fd, _currentPixelFormat, nullptr)); OnFrame(webrtc::VideoFrame::Builder() .set_video_frame_buffer(jetson_buffer) .set_timestamp_rtp(0) @@ -127,4 +616,4 @@ void JetsonV4L2Capturer::OnCaptured(uint8_t* data, uint32_t bytesused) { .set_rotation(webrtc::kVideoRotation_0) .build()); } -} \ No newline at end of file +} diff --git a/src/hwenc_jetson/jetson_v4l2_capturer.h b/src/hwenc_jetson/jetson_v4l2_capturer.h index 4b89adda..6d91515d 100644 --- a/src/hwenc_jetson/jetson_v4l2_capturer.h +++ b/src/hwenc_jetson/jetson_v4l2_capturer.h @@ -1,28 +1,79 @@ -#ifndef JETSON_V4L2_CAPTURER_H_ -#define JETSON_V4L2_CAPTURER_H_ +#ifndef HWENC_JETSON_JETSON_V4L2_CAPTURER_H_ +#define HWENC_JETSON_JETSON_V4L2_CAPTURER_H_ -#include +#include +#include + +#include + +// Linux +#include + +// WebRTC +#include +#include +#include +#include #include "jetson_jpeg_decoder_pool.h" +#include "rtc/scalable_track_source.h" +#include "v4l2_video_capturer/v4l2_video_capturer.h" -class JetsonV4L2Capturer : public V4L2VideoCapturer { +class JetsonV4L2Capturer : public ScalableVideoTrackSource { public: - static rtc::scoped_refptr Create( - V4L2VideoCapturerConfig config); + static rtc::scoped_refptr Create( + const V4L2VideoCapturerConfig& config); + JetsonV4L2Capturer(const V4L2VideoCapturerConfig& config); + ~JetsonV4L2Capturer(); + + private: + static void LogDeviceList( + webrtc::VideoCaptureModule::DeviceInfo* device_info); - bool UseNativeBuffer() override; + int32_t Init(const char* deviceUniqueId, + const std::string& specifiedVideoDevice); + int32_t StartCapture(const V4L2VideoCapturerConfig& config); + + int32_t StopCapture(); + bool AllocateVideoBuffers(); + bool DeAllocateVideoBuffers(); + void OnCaptured(v4l2_buffer* buf); + + int32_t _deviceFd; + int32_t _currentWidth; + int32_t _currentHeight; + int32_t _currentPixelFormat; + int32_t _currentFrameRate; + webrtc::VideoType _captureVideoType; + struct Buffer { + void* start; + size_t length; + int fd; + }; + Buffer* _pool; private: - static rtc::scoped_refptr Create( + static rtc::scoped_refptr Create( webrtc::VideoCaptureModule::DeviceInfo* device_info, - V4L2VideoCapturerConfig config, + const V4L2VideoCapturerConfig& config, size_t capture_device_index); + bool FindDevice(const char* deviceUniqueIdUTF8, const std::string& device); + + enum { kNoOfV4L2Bufffers = 4 }; + + static void CaptureThread(void*); + bool CaptureProcess(); + + rtc::PlatformThread _captureThread; + webrtc::Mutex capture_lock_; + bool quit_ RTC_GUARDED_BY(capture_lock_); + std::string _videoDevice; - bool AllocateVideoBuffers() override; - bool DeAllocateVideoBuffers() override; - void OnCaptured(uint8_t* data, uint32_t bytesused) override; + int32_t _buffersAllocatedByDevice; + bool _useNative; + bool _captureStarted; std::shared_ptr jpeg_decoder_pool_; }; -#endif // JETSON_V4L2_CAPTURER_H_ +#endif diff --git a/src/hwenc_jetson/jetson_video_decoder.cpp b/src/hwenc_jetson/jetson_video_decoder.cpp index fbac1e76..f1a3cf45 100644 --- a/src/hwenc_jetson/jetson_video_decoder.cpp +++ b/src/hwenc_jetson/jetson_video_decoder.cpp @@ -24,6 +24,9 @@ // L4T Multimedia API #include +// Jetson Linux Multimedia API +#include + #define INIT_ERROR(cond, desc) \ if (cond) { \ RTC_LOG(LS_ERROR) << __FUNCTION__ << desc; \ @@ -32,8 +35,12 @@ } #define CHUNK_SIZE 4000000 -JetsonVideoDecoder::JetsonVideoDecoder(uint32_t input_format) - : input_format_(input_format), +JetsonVideoDecoder::JetsonVideoDecoder(webrtc::VideoCodecType codec) + : input_format_(codec == webrtc::kVideoCodecVP8 ? V4L2_PIX_FMT_VP8 + : codec == webrtc::kVideoCodecVP9 ? V4L2_PIX_FMT_VP9 + : codec == webrtc::kVideoCodecH264 ? V4L2_PIX_FMT_H264 + : codec == webrtc::kVideoCodecAV1 ? V4L2_PIX_FMT_AV1 + : 0), decoder_(nullptr), decode_complete_callback_(nullptr), buffer_pool_(false, 300 /* max_number_of_buffers*/), @@ -45,6 +52,24 @@ JetsonVideoDecoder::~JetsonVideoDecoder() { Release(); } +bool JetsonVideoDecoder::IsSupportedVP8() { + //SuppressErrors sup; + + auto decoder = NvVideoDecoder::createVideoDecoder("dec0"); + auto ret = decoder->setOutputPlaneFormat(V4L2_PIX_FMT_VP8, CHUNK_SIZE); + delete decoder; + return ret >= 0; +} + +bool JetsonVideoDecoder::IsSupportedAV1() { + //SuppressErrors sup; + + auto decoder = NvVideoDecoder::createVideoDecoder("dec0"); + auto ret = decoder->setOutputPlaneFormat(V4L2_PIX_FMT_AV1, CHUNK_SIZE); + delete decoder; + return ret >= 0; +} + bool JetsonVideoDecoder::Configure(const Settings& settings) { if (JetsonConfigure() != WEBRTC_VIDEO_CODEC_OK) { RTC_LOG(LS_ERROR) << __FUNCTION__ << "Failed to JetsonConfigure"; @@ -74,10 +99,10 @@ int32_t JetsonVideoDecoder::Decode(const webrtc::EncodedImage& input_image, memset(planes, 0, sizeof(planes)); v4l2_buf.m.planes = planes; - RTC_LOG(LS_INFO) << __FUNCTION__ << " output_plane.getNumBuffers: " - << decoder_->output_plane.getNumBuffers() - << " output_plane.getNumQueuedBuffers: " - << decoder_->output_plane.getNumQueuedBuffers(); + // RTC_LOG(LS_INFO) << __FUNCTION__ << " output_plane.getNumBuffers: " + // << decoder_->output_plane.getNumBuffers() + // << " output_plane.getNumQueuedBuffers: " + // << decoder_->output_plane.getNumQueuedBuffers(); if (decoder_->output_plane.getNumQueuedBuffers() == decoder_->output_plane.getNumBuffers()) { @@ -107,8 +132,8 @@ int32_t JetsonVideoDecoder::Decode(const webrtc::EncodedImage& input_image, return WEBRTC_VIDEO_CODEC_ERROR; } - RTC_LOG(LS_INFO) << __FUNCTION__ << " timestamp:" << input_image.Timestamp() - << " bytesused:" << buffer->planes[0].bytesused; + // RTC_LOG(LS_INFO) << __FUNCTION__ << " timestamp:" << input_image.Timestamp() + // << " bytesused:" << buffer->planes[0].bytesused; return WEBRTC_VIDEO_CODEC_OK; } diff --git a/src/hwenc_jetson/jetson_video_decoder.h b/src/hwenc_jetson/jetson_video_decoder.h index 1227e056..c175bda1 100644 --- a/src/hwenc_jetson/jetson_video_decoder.h +++ b/src/hwenc_jetson/jetson_video_decoder.h @@ -9,22 +9,26 @@ * */ -#ifndef JETSON_VIDEO_DECODER_H_ -#define JETSON_VIDEO_DECODER_H_ +#ifndef HWENC_JETSON_JETSON_VIDEO_DECODER_H_ +#define HWENC_JETSON_JETSON_VIDEO_DECODER_H_ // WebRTC #include #include #include -// Jetson Linux Multimedia API -#include +struct v4l2_crop; +class NvV4l2Element; +class NvVideoDecoder; class JetsonVideoDecoder : public webrtc::VideoDecoder { public: - JetsonVideoDecoder(uint32_t input_format); + JetsonVideoDecoder(webrtc::VideoCodecType codec); ~JetsonVideoDecoder() override; + static bool IsSupportedVP8(); + static bool IsSupportedAV1(); + bool Configure(const Settings& settings) override; int32_t Decode(const webrtc::EncodedImage& input_image, @@ -54,7 +58,7 @@ class JetsonVideoDecoder : public webrtc::VideoDecoder { std::atomic eos_; std::atomic got_error_; int dst_dma_fd_; - std::unique_ptr capture_crop_; + std::shared_ptr capture_crop_; }; -#endif // JETSON_VIDEO_DECODER_H_ +#endif diff --git a/src/hwenc_jetson/jetson_video_encoder.cpp b/src/hwenc_jetson/jetson_video_encoder.cpp index 8bfc4f82..22c6ae74 100644 --- a/src/hwenc_jetson/jetson_video_encoder.cpp +++ b/src/hwenc_jetson/jetson_video_encoder.cpp @@ -16,6 +16,7 @@ // WebRTC #include +#include #include #include #include @@ -25,6 +26,8 @@ #include // L4T Multimedia API +#include +#include #include #include "jetson_buffer.h" @@ -37,17 +40,8 @@ return WEBRTC_VIDEO_CODEC_ERROR; \ } -namespace { -struct nal_entry { - size_t offset; - size_t size; -}; - -} // namespace - JetsonVideoEncoder::JetsonVideoEncoder(const cricket::VideoCodec& codec) : callback_(nullptr), - converter_(nullptr), encoder_(nullptr), configured_framerate_(30), use_native_(false), @@ -58,42 +52,57 @@ JetsonVideoEncoder::~JetsonVideoEncoder() { } // 標準出力や標準エラーに出力されないようにいろいろする -struct SuppressErrors { - SuppressErrors() { - old_stdout = stdout; - old_stderr = stderr; - old_log_level = log_level; - stdout = fopen("/dev/null", "w"); - stderr = fopen("/dev/null", "w"); - log_level = -1; - } - ~SuppressErrors() { - fclose(stdout); - fclose(stderr); - stdout = old_stdout; - stderr = old_stderr; - log_level = old_log_level; - } - FILE* old_stdout; - FILE* old_stderr; - int old_log_level; -}; +//struct SuppressErrors { +// SuppressErrors() { +// old_stdout = stdout; +// old_stderr = stderr; +// old_log_level = log_level; +// stdout = fopen("/dev/null", "w"); +// stderr = fopen("/dev/null", "w"); +// log_level = -1; +// } +// ~SuppressErrors() { +// fclose(stdout); +// fclose(stderr); +// stdout = old_stdout; +// stderr = old_stderr; +// log_level = old_log_level; +// } +// FILE* old_stdout; +// FILE* old_stderr; +// int old_log_level; +//}; bool JetsonVideoEncoder::IsSupportedVP8() { - SuppressErrors sup; + //SuppressErrors sup; auto encoder = NvVideoEncoder::createVideoEncoder("enc0"); auto ret = encoder->setCapturePlaneFormat(V4L2_PIX_FMT_VP8, 1024, 768, 2 * 1024 * 1024); + delete encoder; + return ret >= 0; } bool JetsonVideoEncoder::IsSupportedVP9() { - SuppressErrors sup; + //SuppressErrors sup; auto encoder = NvVideoEncoder::createVideoEncoder("enc0"); auto ret = encoder->setCapturePlaneFormat(V4L2_PIX_FMT_VP9, 1024, 768, 2 * 1024 * 1024); + delete encoder; + + return ret >= 0; +} + +bool JetsonVideoEncoder::IsSupportedAV1() { + //SuppressErrors sup; + + auto encoder = NvVideoEncoder::createVideoEncoder("enc0"); + auto ret = encoder->setCapturePlaneFormat(V4L2_PIX_FMT_AV1, 1024, 768, + 2 * 1024 * 1024); + delete encoder; + return ret >= 0; } @@ -132,6 +141,15 @@ int32_t JetsonVideoEncoder::InitEncode(const webrtc::VideoCodec* codec_settings, << codec_settings->VP9().numberOfSpatialLayers; RTC_LOG(LS_INFO) << "interLayerPred: " << codec_settings->VP9().interLayerPred; + } else if (codec_settings->codecType == webrtc::kVideoCodecAV1) { + auto scalability_mode = codec_settings->GetScalabilityMode(); + if (!scalability_mode) { + RTC_LOG(LS_WARNING) << "Scalability mode is not set, using 'L1T1'."; + scalability_mode = webrtc::ScalabilityMode::kL1T1; + } + RTC_LOG(LS_INFO) << "InitEncode scalability_mode:" + << (int)*scalability_mode; + svc_controller_ = webrtc::CreateScalabilityStructure(*scalability_mode); } framerate_ = codec_settings->maxFramerate; @@ -164,60 +182,6 @@ int32_t JetsonVideoEncoder::JetsonConfigure() { use_native_ && (width_ != raw_width_ || height_ != raw_height_ || decode_pixfmt_ != V4L2_PIX_FMT_YUV420M); - if (use_converter) { - enc0_buffer_queue_ = new std::queue; - - converter_ = NvVideoConverter::createVideoConverter("conv"); - INIT_ERROR(!converter_, "Failed to createVideoConverter"); - - ret = converter_->setOutputPlaneFormat( - decode_pixfmt_, raw_width_, raw_height_, V4L2_NV_BUFFER_LAYOUT_PITCH); - INIT_ERROR(ret < 0, "Failed to converter setOutputPlaneFormat"); - - ret = - converter_->setCapturePlaneFormat(V4L2_PIX_FMT_YUV420M, width_, height_, - V4L2_NV_BUFFER_LAYOUT_BLOCKLINEAR); - INIT_ERROR(ret < 0, "Failed to converter setCapturePlaneFormat"); - - ret = converter_->setCropRect(0, 0, raw_width_, raw_height_); - INIT_ERROR(ret < 0, "Failed to converter setCropRect"); - - if (use_dmabuff_) { - ret = converter_->output_plane.setupPlane(V4L2_MEMORY_DMABUF, 1, false, - false); - INIT_ERROR(ret < 0, "Failed to setupPlane at converter output_plane"); - } else { - ret = converter_->output_plane.setupPlane(V4L2_MEMORY_USERPTR, 1, false, - false); - INIT_ERROR(ret < 0, "Failed to setupPlane at converter output_plane"); - } - - NvBufferCreateParams create_params = {0}; - create_params.width = width_; - create_params.height = height_; - create_params.layout = NvBufferLayout_BlockLinear; - create_params.payloadType = NvBufferPayload_SurfArray; - create_params.colorFormat = NvBufferColorFormat_YUV420; - create_params.nvbuf_tag = NvBufferTag_VIDEO_ENC; - for (int i = 0; i < CONVERTER_CAPTURE_NUM; i++) { - ret = NvBufferCreateEx(&dmabuff_fd_[i], &create_params); - INIT_ERROR(ret < 0, "Failed to NvBufferCreateEx at converter"); - } - - ret = converter_->capture_plane.reqbufs(V4L2_MEMORY_DMABUF, - CONVERTER_CAPTURE_NUM); - INIT_ERROR(ret < 0, "Failed to reqbufs at converter capture_plane"); - - ret = converter_->output_plane.setStreamStatus(true); - INIT_ERROR(ret < 0, "Failed to setStreamStatus at converter output_plane"); - - ret = converter_->capture_plane.setStreamStatus(true); - INIT_ERROR(ret < 0, "Failed to setStreamStatus at converter capture_plane"); - - converter_->capture_plane.setDQThreadCallback( - ConvertFinishedCallbackFunction); - } - encoder_ = NvVideoEncoder::createVideoEncoder("enc0"); INIT_ERROR(!encoder_, "Failed to createVideoEncoder"); @@ -230,6 +194,9 @@ int32_t JetsonVideoEncoder::JetsonConfigure() { } else if (codec_.codecType == webrtc::kVideoCodecVP9) { ret = encoder_->setCapturePlaneFormat(V4L2_PIX_FMT_VP9, width_, height_, 2 * 1024 * 1024); + } else if (codec_.codecType == webrtc::kVideoCodecAV1) { + ret = encoder_->setCapturePlaneFormat(V4L2_PIX_FMT_AV1, width_, height_, + 2 * 1024 * 1024); } INIT_ERROR(ret < 0, "Failed to encoder setCapturePlaneFormat"); @@ -301,9 +268,23 @@ int32_t JetsonVideoEncoder::JetsonConfigure() { if (use_native_) { if (use_dmabuff_ || use_converter) { - ret = encoder_->output_plane.setupPlane(V4L2_MEMORY_DMABUF, 1, false, - false); - INIT_ERROR(ret < 0, "Failed to setupPlane at encoder output_plane"); + ret = encoder_->output_plane.reqbufs(V4L2_MEMORY_DMABUF, 10); + INIT_ERROR(ret < 0, "Failed to reqbufs at encoder output_plane"); + + int fd; + NvBufSurf::NvCommonAllocateParams cParams; + cParams.width = width_; + cParams.height = height_; + cParams.layout = NVBUF_LAYOUT_PITCH; + cParams.colorFormat = NVBUF_COLOR_FORMAT_YUV420; + cParams.memtag = NvBufSurfaceTag_VIDEO_ENC; + cParams.memType = NVBUF_MEM_SURFACE_ARRAY; + for (uint32_t i = 0; i < encoder_->output_plane.getNumBuffers(); i++) { + ret = NvBufSurf::NvAllocate(&cParams, 1, &fd); + INIT_ERROR(ret, "Failed to create NvBuffer"); + RTC_LOG(LS_ERROR) << "NvBufferCreateEx i:" << i << " fd:" << fd; + output_plane_fd_[i] = fd; + } } else { ret = encoder_->output_plane.setupPlane(V4L2_MEMORY_USERPTR, 1, false, false); @@ -326,35 +307,6 @@ int32_t JetsonVideoEncoder::JetsonConfigure() { ret = encoder_->capture_plane.setStreamStatus(true); INIT_ERROR(ret < 0, "Failed to setStreamStatus at encoder capture_plane"); - if (use_converter) { - converter_->capture_plane.startDQThread(this); - - for (uint32_t i = 0; i < CONVERTER_CAPTURE_NUM; i++) { - struct v4l2_buffer v4l2_buf; - struct v4l2_plane planes[MAX_PLANES]; - - memset(&v4l2_buf, 0, sizeof(v4l2_buf)); - memset(planes, 0, MAX_PLANES * sizeof(struct v4l2_plane)); - - v4l2_buf.index = i; - v4l2_buf.m.planes = planes; - v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; - v4l2_buf.memory = V4L2_MEMORY_DMABUF; - v4l2_buf.m.planes[0].m.fd = dmabuff_fd_[i]; - ret = converter_->capture_plane.qBuffer(v4l2_buf, nullptr); - INIT_ERROR(ret < 0, "Failed to qBuffer at converter capture_plane"); - } - - for (uint32_t i = 0; i < encoder_->output_plane.getNumBuffers(); i++) { - enc0_buffer_queue_->push(encoder_->output_plane.getNthBuffer(i)); - } - encoder_->output_plane.setDQThreadCallback(EncodeOutputCallbackFunction); - encoder_->output_plane.startDQThread(this); - - native_input_elem_ = converter_; - } else { - native_input_elem_ = encoder_; - } encoder_->capture_plane.setDQThreadCallback(EncodeFinishedCallbackFunction); encoder_->capture_plane.startDQThread(this); @@ -377,33 +329,30 @@ int32_t JetsonVideoEncoder::JetsonConfigure() { void JetsonVideoEncoder::JetsonRelease() { if (!encoder_) return; - if (converter_) { - SendEOS(converter_); - } else { - SendEOS(encoder_); - } + SendEOS(); encoder_->capture_plane.waitForDQThread(2000); encoder_->capture_plane.deinitPlane(); - encoder_->output_plane.deinitPlane(); - if (converter_) { - delete enc0_buffer_queue_; - } - delete encoder_; - encoder_ = nullptr; - if (converter_) { - converter_->capture_plane.waitForDQThread(2000); - for (int i = 0; i < CONVERTER_CAPTURE_NUM; i++) { - if (dmabuff_fd_[i] != 0) { - NvBufferDestroy(dmabuff_fd_[i]); + if (use_dmabuff_) { + for (uint32_t i = 0; i < encoder_->output_plane.getNumBuffers(); i++) { + if (encoder_->output_plane.unmapOutputBuffers(i, output_plane_fd_[i]) < + 0) { + RTC_LOG(LS_ERROR) + << "Failed to unmapOutputBuffers at encoder output_plane"; + } + if (NvBufSurf::NvDestroy(output_plane_fd_[i]) < 0) { + RTC_LOG(LS_ERROR) + << "Failed to NvBufferDestroy at encoder output_plane"; } } - delete converter_; - converter_ = nullptr; + } else { + encoder_->output_plane.deinitPlane(); } + delete encoder_; + encoder_ = nullptr; } -void JetsonVideoEncoder::SendEOS(NvV4l2Element* element) { - if (element->output_plane.getStreamStatus()) { +void JetsonVideoEncoder::SendEOS() { + if (encoder_->output_plane.getStreamStatus()) { struct v4l2_buffer v4l2_buf; struct v4l2_plane planes[MAX_PLANES]; NvBuffer* buffer; @@ -412,9 +361,9 @@ void JetsonVideoEncoder::SendEOS(NvV4l2Element* element) { memset(planes, 0, MAX_PLANES * sizeof(struct v4l2_plane)); v4l2_buf.m.planes = planes; - if (element->output_plane.getNumQueuedBuffers() == - element->output_plane.getNumBuffers()) { - if (element->output_plane.dqBuffer(v4l2_buf, &buffer, NULL, 10) < 0) { + if (encoder_->output_plane.getNumQueuedBuffers() == + encoder_->output_plane.getNumBuffers()) { + if (encoder_->output_plane.dqBuffer(v4l2_buf, &buffer, NULL, 10) < 0) { RTC_LOG(LS_ERROR) << "Failed to dqBuffer at encoder output_plane"; } } @@ -422,108 +371,12 @@ void JetsonVideoEncoder::SendEOS(NvV4l2Element* element) { for (int i = 0; i < buffer->n_planes; i++) { buffer->planes[i].bytesused = 0; } - if (element->output_plane.qBuffer(v4l2_buf, NULL) < 0) { + if (encoder_->output_plane.qBuffer(v4l2_buf, NULL) < 0) { RTC_LOG(LS_ERROR) << "Failed to qBuffer at encoder output_plane"; } } } -bool JetsonVideoEncoder::ConvertFinishedCallbackFunction( - struct v4l2_buffer* v4l2_buf, - NvBuffer* buffer, - NvBuffer* shared_buffer, - void* data) { - return ((JetsonVideoEncoder*)data) - ->ConvertFinishedCallback(v4l2_buf, buffer, shared_buffer); -} - -bool JetsonVideoEncoder::ConvertFinishedCallback(struct v4l2_buffer* v4l2_buf, - NvBuffer* buffer, - NvBuffer* shared_buffer) { - NvBuffer* enc0_buffer; - struct v4l2_buffer enc0_qbuf; - struct v4l2_plane planes[MAX_PLANES]; - - if (!v4l2_buf) { - RTC_LOG(LS_ERROR) << __FUNCTION__ << " v4l2_buf is null"; - return false; - } - - { - std::unique_lock lock(enc0_buffer_mtx_); - enc0_buffer_cond_.wait(lock, - [this] { return !enc0_buffer_queue_->empty(); }); - enc0_buffer = enc0_buffer_queue_->front(); - enc0_buffer_queue_->pop(); - } - - memset(&enc0_qbuf, 0, sizeof(enc0_qbuf)); - memset(&planes, 0, sizeof(planes)); - - enc0_qbuf.index = enc0_buffer->index; - enc0_qbuf.m.planes = planes; - buffer->planes[0].fd = dmabuff_fd_[v4l2_buf->index]; - - enc0_qbuf.flags |= V4L2_BUF_FLAG_TIMESTAMP_COPY; - enc0_qbuf.timestamp.tv_sec = v4l2_buf->timestamp.tv_sec; - enc0_qbuf.timestamp.tv_usec = v4l2_buf->timestamp.tv_usec; - - if (encoder_->output_plane.qBuffer(enc0_qbuf, buffer) < 0) { - RTC_LOG(LS_ERROR) << __FUNCTION__ - << " Failed to qBuffer at encoder output_plane"; - return false; - } - - if (v4l2_buf->m.planes[0].bytesused == 0) { - RTC_LOG(LS_ERROR) << __FUNCTION__ << " buffer size is zero"; - return false; - } - - return true; -} - -bool JetsonVideoEncoder::EncodeOutputCallbackFunction( - struct v4l2_buffer* v4l2_buf, - NvBuffer* buffer, - NvBuffer* shared_buffer, - void* data) { - return ((JetsonVideoEncoder*)data) - ->EncodeOutputCallback(v4l2_buf, buffer, shared_buffer); -} - -bool JetsonVideoEncoder::EncodeOutputCallback(struct v4l2_buffer* v4l2_buf, - NvBuffer* buffer, - NvBuffer* shared_buffer) { - struct v4l2_buffer conv_qbuf; - struct v4l2_plane planes[MAX_PLANES]; - - if (!v4l2_buf) { - RTC_LOG(LS_INFO) << __FUNCTION__ << " v4l2_buf is null"; - return false; - } - - memset(&conv_qbuf, 0, sizeof(conv_qbuf)); - memset(&planes, 0, sizeof(planes)); - - conv_qbuf.index = shared_buffer->index; - conv_qbuf.m.planes = planes; - conv_qbuf.m.planes[0].m.fd = dmabuff_fd_[shared_buffer->index]; - { - std::unique_lock lock(enc0_buffer_mtx_); - enc0_buffer_queue_->push(buffer); - - if (converter_->capture_plane.qBuffer(conv_qbuf, nullptr) < 0) { - RTC_LOG(LS_ERROR) << __FUNCTION__ - << "Failed to qBuffer at converter capture_plane"; - return false; - } - - enc0_buffer_cond_.notify_all(); - } - - return true; -} - bool JetsonVideoEncoder::EncodeFinishedCallbackFunction( struct v4l2_buffer* v4l2_buf, NvBuffer* buffer, @@ -580,7 +433,7 @@ bool JetsonVideoEncoder::EncodeFinishedCallback(struct v4l2_buffer* v4l2_buf, } SendFrame(buffer->planes[0].data, buffer->planes[0].bytesused, - std::move(params), enc_metadata); + std::move(params), &enc_metadata); if (encoder_->capture_plane.qBuffer(*v4l2_buf, NULL) < 0) { RTC_LOG(LS_ERROR) << __FUNCTION__ << "Failed to qBuffer at capture_plane"; @@ -604,6 +457,10 @@ void JetsonVideoEncoder::SetRates(const RateControlParameters& parameters) { RTC_LOG(LS_INFO) << __FUNCTION__ << " framerate:" << parameters.framerate_fps << " bitrate:" << parameters.bitrate.ToString(); + if (svc_controller_) { + svc_controller_->OnRatesUpdated(parameters.bitrate); + } + framerate_ = parameters.framerate_fps; target_bitrate_bps_ = parameters.bitrate.get_sum_bps(); @@ -666,6 +523,11 @@ webrtc::VideoEncoder::EncoderInfo JetsonVideoEncoder::GetEncoderInfo() const { static const int kHighVp9QpThreshold = 151; info.scaling_settings = VideoEncoder::ScalingSettings(kLowVp9QpThreshold, kHighVp9QpThreshold); + } else if (codec_.codecType == webrtc::kVideoCodecAV1) { + static const int kLowAv1QpThreshold = 145; + static const int kHighAv1QpThreshold = 205; + info.scaling_settings = + VideoEncoder::ScalingSettings(kLowAv1QpThreshold, kHighAv1QpThreshold); } return info; } @@ -693,27 +555,10 @@ int32_t JetsonVideoEncoder::Encode( video_type = jetson_buffer->VideoType(); raw_width_ = jetson_buffer->RawWidth(); raw_height_ = jetson_buffer->RawHeight(); - if (video_type == webrtc::VideoType::kMJPEG) { - use_dmabuff_ = true; - fd = jetson_buffer->DecodedFd(); - decode_pixfmt_ = jetson_buffer->V4L2PixelFormat(); - decoder = jetson_buffer->JpegDecoder(); - } else { - use_dmabuff_ = false; - if (video_type == webrtc::VideoType::kYUY2) { - decode_pixfmt_ = V4L2_PIX_FMT_YUYV; - } else if (video_type == webrtc::VideoType::kI420) { - decode_pixfmt_ = V4L2_PIX_FMT_YUV420M; - } else if (video_type == webrtc::VideoType::kYV12) { - decode_pixfmt_ = V4L2_PIX_FMT_YUV420M; - } else if (video_type == webrtc::VideoType::kUYVY) { - decode_pixfmt_ = V4L2_PIX_FMT_UYVY; - } else { - RTC_LOG(LS_ERROR) << "Unsupported VideoType"; - return WEBRTC_VIDEO_CODEC_ERROR; - } - native_data = jetson_buffer->Data(); - } + use_dmabuff_ = true; + fd = jetson_buffer->DecodedFd(); + decode_pixfmt_ = jetson_buffer->V4L2PixelFormat(); + decoder = jetson_buffer->JpegDecoder(); } else { use_native_ = false; } @@ -758,22 +603,21 @@ int32_t JetsonVideoEncoder::Encode( if (use_native_) { NvBuffer* buffer; - if (native_input_elem_->output_plane.getNumQueuedBuffers() == - native_input_elem_->output_plane.getNumBuffers()) { - if (native_input_elem_->output_plane.dqBuffer(v4l2_buf, &buffer, NULL, - 10) < 0) { - RTC_LOG(LS_ERROR) << "Failed to dqBuffer at converter output_plane"; + if (encoder_->output_plane.getNumQueuedBuffers() == + encoder_->output_plane.getNumBuffers()) { + if (encoder_->output_plane.dqBuffer(v4l2_buf, &buffer, NULL, 10) < 0) { + RTC_LOG(LS_ERROR) << "Failed to dqBuffer at encoder output_plane"; return WEBRTC_VIDEO_CODEC_ERROR; } - } else if (!use_dmabuff_ || native_input_elem_ == encoder_) { - buffer = native_input_elem_->output_plane.getNthBuffer( - native_input_elem_->output_plane.getNumQueuedBuffers()); - v4l2_buf.index = native_input_elem_->output_plane.getNumQueuedBuffers(); + } else { + buffer = encoder_->output_plane.getNthBuffer( + encoder_->output_plane.getNumQueuedBuffers()); + v4l2_buf.index = encoder_->output_plane.getNumQueuedBuffers(); } + int src_dma_fd = -1; if (use_dmabuff_) { - planes[0].m.fd = fd; - planes[0].bytesused = 1234; + src_dma_fd = fd; } else if (video_type == webrtc::VideoType::kYUY2 || video_type == webrtc::VideoType::kUYVY) { buffer->planes[0].bytesused = buffer->planes[0].fmt.width * @@ -810,13 +654,40 @@ int32_t JetsonVideoEncoder::Encode( return WEBRTC_VIDEO_CODEC_ERROR; } + NvBufSurf::NvCommonTransformParams transform_params; + /* Indicates which of the transform parameters are valid */ + memset(&transform_params, 0, sizeof(transform_params)); + transform_params.src_top = 0; + transform_params.src_left = 0; + transform_params.src_width = raw_width_; + transform_params.src_height = raw_height_; + transform_params.dst_top = 0; + transform_params.dst_left = 0; + transform_params.dst_width = width_; + transform_params.dst_height = height_; + transform_params.flag = + (NvBufSurfTransform_Transform_Flag)(NVBUFSURF_TRANSFORM_FILTER | + NVBUFSURF_TRANSFORM_CROP_SRC); + transform_params.flip = NvBufSurfTransform_None; + transform_params.filter = NvBufSurfTransformInter_Bilinear; + if (NvBufSurf::NvTransform(&transform_params, src_dma_fd, + output_plane_fd_[v4l2_buf.index])) { + RTC_LOG(LS_ERROR) << "Failed to NvBufferTransform"; + return WEBRTC_VIDEO_CODEC_ERROR; + } + + planes[0].m.fd = output_plane_fd_[v4l2_buf.index]; + planes[0].bytesused = 1234; + + v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; + v4l2_buf.memory = V4L2_MEMORY_DMABUF; v4l2_buf.flags |= V4L2_BUF_FLAG_TIMESTAMP_COPY; v4l2_buf.timestamp.tv_sec = input_frame.timestamp_us() / rtc::kNumMicrosecsPerSec; v4l2_buf.timestamp.tv_usec = input_frame.timestamp_us() % rtc::kNumMicrosecsPerSec; - if (native_input_elem_->output_plane.qBuffer(v4l2_buf, nullptr) < 0) { + if (encoder_->output_plane.qBuffer(v4l2_buf, nullptr) < 0) { RTC_LOG(LS_ERROR) << "Failed to qBuffer at converter output_plane"; return WEBRTC_VIDEO_CODEC_ERROR; } @@ -895,7 +766,7 @@ int32_t JetsonVideoEncoder::SendFrame( unsigned char* buffer, size_t size, std::unique_ptr params, - v4l2_ctrl_videoenc_outputbuf_metadata enc_metadata) { + v4l2_ctrl_videoenc_outputbuf_metadata* enc_metadata) { if (!callback_) { RTC_LOG(LS_WARNING) << "InitEncode() has been called, but a callback function " @@ -910,8 +781,8 @@ int32_t JetsonVideoEncoder::SendFrame( encoded_image_.capture_time_ms_ = params->render_time_ms; encoded_image_.ntp_time_ms_ = params->ntp_time_ms; encoded_image_.rotation_ = params->rotation; - encoded_image_.qp_ = enc_metadata.AvgQP; - if (enc_metadata.KeyFrame) { + encoded_image_.qp_ = enc_metadata->AvgQP; + if (enc_metadata->KeyFrame) { encoded_image_._frameType = webrtc::VideoFrameType::kVideoFrameKey; } else { encoded_image_._frameType = webrtc::VideoFrameType::kVideoFrameDelta; @@ -926,9 +797,10 @@ int32_t JetsonVideoEncoder::SendFrame( codec_specific.codecSpecific.H264.packetization_mode = webrtc::H264PacketizationMode::NonInterleaved; - } else if (codec_.codecType == webrtc::kVideoCodecVP9 || + } else if (codec_.codecType == webrtc::kVideoCodecAV1 || + codec_.codecType == webrtc::kVideoCodecVP9 || codec_.codecType == webrtc::kVideoCodecVP8) { - // VP8, VP9 はIVFヘッダーがエンコードフレームについているので取り除く + // VP8, VP9, AV1 はIVFヘッダーがエンコードフレームについているので取り除く if ((buffer[0] == 'D') && (buffer[1] == 'K') && (buffer[2] == 'I') && (buffer[3] == 'F')) { buffer += 32; @@ -946,14 +818,14 @@ int32_t JetsonVideoEncoder::SendFrame( // nonReference かを知ることはできなかった codec_specific.codecSpecific.VP8.nonReference = false; } else if (codec_.codecType == webrtc::kVideoCodecVP9) { - if (enc_metadata.KeyFrame) { + if (enc_metadata->KeyFrame) { gof_idx_ = 0; } codec_specific.codecSpecific.VP9.inter_pic_predicted = - enc_metadata.KeyFrame ? false : true; + enc_metadata->KeyFrame ? false : true; codec_specific.codecSpecific.VP9.flexible_mode = false; codec_specific.codecSpecific.VP9.ss_data_available = - enc_metadata.KeyFrame ? true : false; + enc_metadata->KeyFrame ? true : false; codec_specific.codecSpecific.VP9.temporal_idx = webrtc::kNoTemporalIdx; codec_specific.codecSpecific.VP9.temporal_up_switch = true; codec_specific.codecSpecific.VP9.inter_layer_predicted = false; @@ -961,7 +833,6 @@ int32_t JetsonVideoEncoder::SendFrame( static_cast(gof_idx_++ % gof_.num_frames_in_gof); codec_specific.codecSpecific.VP9.num_spatial_layers = 1; codec_specific.codecSpecific.VP9.first_frame_in_picture = true; - codec_specific.codecSpecific.VP9.end_of_picture = true; codec_specific.codecSpecific.VP9.spatial_layer_resolution_present = false; if (codec_specific.codecSpecific.VP9.ss_data_available) { codec_specific.codecSpecific.VP9.spatial_layer_resolution_present = @@ -972,10 +843,31 @@ int32_t JetsonVideoEncoder::SendFrame( encoded_image_._encodedHeight; codec_specific.codecSpecific.VP9.gof.CopyGofInfoVP9(gof_); } + } else if (codec_.codecType == webrtc::kVideoCodecAV1) { + bool is_key = buffer[2] == 0x0a; + // v4l2_ctrl_videoenc_outputbuf_metadata.KeyFrame が効いていない + // キーフレームの時には OBU_SEQUENCE_HEADER が入っているために 0x0a になるためこれを使う + // キーフレームではない時には OBU_FRAME が入っていて 0x32 になっている + if (is_key) { + encoded_image_._frameType = webrtc::VideoFrameType::kVideoFrameKey; + } + + std::vector + layer_frames = svc_controller_->NextFrameConfig(is_key); + codec_specific.end_of_picture = true; + codec_specific.generic_frame_info = + svc_controller_->OnEncodeDone(layer_frames[0]); + if (is_key && codec_specific.generic_frame_info) { + codec_specific.template_structure = + svc_controller_->DependencyStructure(); + auto& resolutions = codec_specific.template_structure->resolutions; + resolutions = {webrtc::RenderResolution(encoded_image_._encodedWidth, + encoded_image_._encodedHeight)}; + } } } - RTC_LOG(LS_VERBOSE) << "key_frame=" << enc_metadata.KeyFrame + RTC_LOG(LS_VERBOSE) << "key_frame=" << enc_metadata->KeyFrame << " size=" << size << " qp=" << encoded_image_.qp_; webrtc::EncodedImageCallback::Result result = diff --git a/src/hwenc_jetson/jetson_video_encoder.h b/src/hwenc_jetson/jetson_video_encoder.h index 6fa6bacf..4cf75b90 100644 --- a/src/hwenc_jetson/jetson_video_encoder.h +++ b/src/hwenc_jetson/jetson_video_encoder.h @@ -9,8 +9,8 @@ * */ -#ifndef JETSON_VIDEO_ENCODER_H_ -#define JETSON_VIDEO_ENCODER_H_ +#ifndef HWENC_JETSON_JETSON_VIDEO_ENCODER_H_ +#define HWENC_JETSON_JETSON_VIDEO_ENCODER_H_ #include #include @@ -24,17 +24,17 @@ #include #include #include -#include "rtc_base/synchronization/mutex.h" - -// Jetson Linux Multimedia API -#include "NvVideoConverter.h" -#include "NvVideoEncoder.h" +#include +#include #include "jetson_jpeg_decoder.h" #define CONVERTER_CAPTURE_NUM 2 -class ProcessThread; +class NvBuffer; +class NvV4l2Element; +class NvVideoEncoder; +struct v4l2_ctrl_videoenc_outputbuf_metadata_; class JetsonVideoEncoder : public webrtc::VideoEncoder { public: @@ -43,6 +43,7 @@ class JetsonVideoEncoder : public webrtc::VideoEncoder { static bool IsSupportedVP8(); static bool IsSupportedVP9(); + static bool IsSupportedAV1(); int32_t InitEncode(const webrtc::VideoCodec* codec_settings, int32_t number_of_cores, @@ -90,21 +91,7 @@ class JetsonVideoEncoder : public webrtc::VideoEncoder { int32_t JetsonConfigure(); void JetsonRelease(); - void SendEOS(NvV4l2Element* element); - static bool ConvertFinishedCallbackFunction(struct v4l2_buffer* v4l2_buf, - NvBuffer* buffer, - NvBuffer* shared_buffer, - void* data); - bool ConvertFinishedCallback(struct v4l2_buffer* v4l2_buf, - NvBuffer* buffer, - NvBuffer* shared_buffer); - static bool EncodeOutputCallbackFunction(struct v4l2_buffer* v4l2_buf, - NvBuffer* buffer, - NvBuffer* shared_buffer, - void* data); - bool EncodeOutputCallback(struct v4l2_buffer* v4l2_buf, - NvBuffer* buffer, - NvBuffer* shared_buffer); + void SendEOS(); static bool EncodeFinishedCallbackFunction(struct v4l2_buffer* v4l2_buf, NvBuffer* buffer, NvBuffer* shared_buffer, @@ -117,11 +104,10 @@ class JetsonVideoEncoder : public webrtc::VideoEncoder { int32_t SendFrame(unsigned char* buffer, size_t size, std::unique_ptr params, - v4l2_ctrl_videoenc_outputbuf_metadata enc_metadata); + v4l2_ctrl_videoenc_outputbuf_metadata_* enc_metadata); webrtc::VideoCodec codec_; webrtc::EncodedImageCallback* callback_; - NvVideoConverter* converter_; NvVideoEncoder* encoder_; std::unique_ptr bitrate_adjuster_; uint32_t framerate_; @@ -135,19 +121,17 @@ class JetsonVideoEncoder : public webrtc::VideoEncoder { int32_t width_; int32_t height_; bool use_native_; - NvV4l2Element* native_input_elem_; bool use_dmabuff_; - int dmabuff_fd_[CONVERTER_CAPTURE_NUM]; webrtc::GofInfoVP9 gof_; size_t gof_idx_; + std::unique_ptr svc_controller_; + webrtc::Mutex frame_params_lock_; std::queue> frame_params_; - std::mutex enc0_buffer_mtx_; - std::condition_variable enc0_buffer_cond_; - std::queue* enc0_buffer_queue_; + int output_plane_fd_[32]; webrtc::EncodedImage encoded_image_; }; -#endif // JETSON_VIDEO_ENCODER_H_ +#endif diff --git a/src/hwenc_mmal/mmal_v4l2_capturer.cpp b/src/hwenc_mmal/mmal_v4l2_capturer.cpp index 015ed152..f45cad00 100644 --- a/src/hwenc_mmal/mmal_v4l2_capturer.cpp +++ b/src/hwenc_mmal/mmal_v4l2_capturer.cpp @@ -64,7 +64,8 @@ rtc::scoped_refptr MMALV4L2Capturer::Create( } MMALV4L2Capturer::MMALV4L2Capturer(const MMALV4L2CapturerConfig& config) - : component_in_(nullptr), + : V4L2VideoCapturer(config), + component_in_(nullptr), decoder_(nullptr), resizer_(nullptr), connection_(nullptr), diff --git a/src/hwenc_msdk/msdk_video_decoder.cpp b/src/hwenc_msdk/msdk_video_decoder.cpp index 2991ca0a..cf37c0d6 100644 --- a/src/hwenc_msdk/msdk_video_decoder.cpp +++ b/src/hwenc_msdk/msdk_video_decoder.cpp @@ -79,7 +79,9 @@ std::unique_ptr MsdkVideoDecoder::CreateDecoder( // << std::endl; //} - if (init) { + // Query した上で Init しても MFX_ERR_UNSUPPORTED になることがあるので + // 本来 Init が不要な時も常に呼ぶようにして確認する + /*if (init)*/ { // Initialize the Media SDK encoder sts = decoder->Init(¶m); if (sts != MFX_ERR_NONE) { @@ -277,4 +279,4 @@ void MsdkVideoDecoder::ReleaseMediaSDK() { decoder_->Close(); } decoder_.reset(); -} \ No newline at end of file +} diff --git a/src/hwenc_nvcodec/nvcodec_v4l2_capturer.cpp b/src/hwenc_nvcodec/nvcodec_v4l2_capturer.cpp index 006ec0a0..faedf5dc 100644 --- a/src/hwenc_nvcodec/nvcodec_v4l2_capturer.cpp +++ b/src/hwenc_nvcodec/nvcodec_v4l2_capturer.cpp @@ -31,6 +31,10 @@ rtc::scoped_refptr NvCodecV4L2Capturer::Create( return nullptr; } +NvCodecV4L2Capturer::NvCodecV4L2Capturer( + const NvCodecV4L2CapturerConfig& config) + : V4L2VideoCapturer(config) {} + bool NvCodecV4L2Capturer::UseNativeBuffer() { return true; } @@ -49,8 +53,8 @@ rtc::scoped_refptr NvCodecV4L2Capturer::Create( return nullptr; } - rtc::scoped_refptr v4l2_capturer( - new rtc::RefCountedObject()); + rtc::scoped_refptr v4l2_capturer = + rtc::make_ref_counted(config); v4l2_capturer->decoder_.reset( new NvCodecDecoderCuda(config.cuda_context, CudaVideoCodec::JPEG)); diff --git a/src/hwenc_nvcodec/nvcodec_v4l2_capturer.h b/src/hwenc_nvcodec/nvcodec_v4l2_capturer.h index 0dbc6afc..17ddb2de 100644 --- a/src/hwenc_nvcodec/nvcodec_v4l2_capturer.h +++ b/src/hwenc_nvcodec/nvcodec_v4l2_capturer.h @@ -16,6 +16,7 @@ class NvCodecV4L2Capturer : public V4L2VideoCapturer { public: static rtc::scoped_refptr Create( NvCodecV4L2CapturerConfig config); + NvCodecV4L2Capturer(const NvCodecV4L2CapturerConfig& config); bool UseNativeBuffer() override; diff --git a/src/hwenc_v4l2/libcamera_capturer.cpp b/src/hwenc_v4l2/libcamera_capturer.cpp new file mode 100644 index 00000000..ffa71abf --- /dev/null +++ b/src/hwenc_v4l2/libcamera_capturer.cpp @@ -0,0 +1,384 @@ +#include "libcamera_capturer.h" + +#include +#include +#include + +// WebRTC +#include +#include +#include + +#include "v4l2_native_buffer.h" + +rtc::scoped_refptr LibcameraCapturer::Create( + LibcameraCapturerConfig config) { + rtc::scoped_refptr capturer; + + LogDeviceList(); + + for (int i = 0; i < 1; ++i) { + capturer = Create(config, i); + if (capturer) { + RTC_LOG(LS_INFO) << "Get Capture"; + return capturer; + } + } + RTC_LOG(LS_ERROR) << "Failed to create LibcameraCapturer"; + return nullptr; +} + +void LibcameraCapturer::LogDeviceList() { + auto camera_manager = libcameracpp_CameraManager_new(); + int ret = libcamerac_CameraManager_start(camera_manager.get()); + if (ret) { + RTC_LOG(LS_ERROR) << __FUNCTION__ + << "libcamera CameraManager failed to start. code: " + << ret; + } else { + auto cameras = libcameracpp_CameraManager_cameras(camera_manager.get()); + int i = 0; + if (libcamerac_vector_Camera_size(cameras.get()) != 0) { + //for (auto const& camera : cameras) { + // RTC_LOG(LS_INFO) << "GetDeviceName(" << i++ << "): device_name=" + // << camera->properties().get( + // libcamera::properties::Model) + // << ", unique_name=" << camera->id(); + //} + } else { + RTC_LOG(LS_ERROR) << "No cameras available"; + } + } + libcamerac_CameraManager_stop(camera_manager.get()); +} + +rtc::scoped_refptr LibcameraCapturer::Create( + LibcameraCapturerConfig config, + size_t capture_device_index) { + rtc::scoped_refptr capturer( + new rtc::RefCountedObject()); + if (capturer->Init(capture_device_index) < 0) { + RTC_LOG(LS_WARNING) << "Failed to create LibcameraCapturer(" + << capture_device_index << ")"; + return nullptr; + } + if (capturer->StartCapture(config) < 0) { + RTC_LOG(LS_WARNING) << "Failed to start LibcameraCapturer(w = " + << config.width << ", h = " << config.height + << ", fps = " << config.framerate << ")"; + return nullptr; + } + return capturer; +} + +LibcameraCapturer::LibcameraCapturer() + : ScalableVideoTrackSource(ScalableVideoTrackSourceConfig()), + acquired_(false), + controls_(libcameracpp_ControlList_controls()), + camera_started_(false) {} + +LibcameraCapturer::~LibcameraCapturer() { + Release(); +} + +int32_t LibcameraCapturer::Init(int camera_id) { + auto camera_manager = libcameracpp_CameraManager_new(); + int ret = libcamerac_CameraManager_start(camera_manager.get()); + if (ret) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " CameraManager failed to start." + << " code: " << ret; + return -1; + } + + auto cameras = libcameracpp_CameraManager_cameras(camera_manager.get()); + auto camera_size = libcamerac_vector_Camera_size(cameras.get()); + if (camera_size == 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " No cameras available."; + return -1; + } + if (camera_id >= camera_size) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Selected camera id is not available"; + return -1; + } + + camera_manager_ = camera_manager; + auto camera = libcamerac_vector_Camera_at(cameras.get(), camera_id); + std::string cam_id = libcamerac_Camera_id(camera); + camera_ = + libcameracpp_CameraManager_get(camera_manager_.get(), cam_id.c_str()); + if (!camera_) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to find camera " << cam_id; + return -1; + } + + if (libcamerac_Camera_acquire(camera_.get())) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to acquire camera " << cam_id; + return -1; + } + acquired_ = true; + return 0; +} + +void LibcameraCapturer::Release() { + if (acquired_) + libcamerac_Camera_release(camera_.get()); + acquired_ = false; + + camera_.reset(); + + camera_manager_.reset(); +} + +int32_t LibcameraCapturer::StartCapture(LibcameraCapturerConfig config) { + auto stream_roles = libcameracpp_vector_StreamRole_new(); + libcamerac_vector_StreamRole_push_back(stream_roles.get(), + libcamerac_StreamRole_VideoRecording); + configuration_ = libcameracpp_Camera_generateConfiguration( + camera_.get(), stream_roles.get()); + if (!configuration_) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to generateConfiguration"; + return -1; + } + + auto cfg = libcamerac_CameraConfiguration_at(configuration_.get(), 0); + libcamerac_StreamConfiguration_set_pixelFormat( + cfg, libcamerac_PixelFormat_YUV420()); + libcamerac_StreamConfiguration_set_bufferCount(cfg, 6); + libcamerac_StreamConfiguration_set_size_width(cfg, config.width); + libcamerac_StreamConfiguration_set_size_height(cfg, config.height); + //libcamerac_StreamConfiguration_set_colorSpace(cfg, + // libcamerac_ColorSpace_Jpeg()); + + auto validation = + libcamerac_CameraConfiguration_validate(configuration_.get()); + if (validation == libcamerac_CameraConfiguration_Status_Invalid) { + RTC_LOG(LS_ERROR) << __FUNCTION__ + << " Failed to validate stream configurations"; + return -1; + } else if (validation == libcamerac_CameraConfiguration_Status_Adjusted) { + RTC_LOG(LS_WARNING) << __FUNCTION__ << " Camera configuration adjusted"; + return -1; + } + + if (libcamerac_Camera_configure(camera_.get(), configuration_.get()) < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to configure camera"; + return -1; + } + + allocator_ = libcameracpp_FrameBufferAllocator_new(camera_.get()); + stream_ = libcamerac_StreamConfiguration_stream(cfg); + if (libcamerac_FrameBufferAllocator_allocate(allocator_.get(), stream_) < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to allocate buffers"; + return -1; + } + + auto buffers_size = + libcamerac_FrameBufferAllocator_buffers_size(allocator_.get(), stream_); + for (int i = 0; i < buffers_size; i++) { + auto buffer = libcamerac_FrameBufferAllocator_buffers_at(allocator_.get(), + stream_, i); + int size = 0; + int planes_size = libcamerac_FrameBuffer_planes_size(buffer); + for (int i = 0; i < planes_size; i++) { + auto plane = libcamerac_FrameBuffer_planes_at(buffer, i); + auto fd = libcamerac_FrameBuffer_Plane_fd(plane); + size += libcamerac_FrameBuffer_Plane_length(plane); + if (i == planes_size - 1 || + fd != libcamerac_FrameBuffer_Plane_fd( + libcamerac_FrameBuffer_planes_at(buffer, i + 1))) { + if (config.native_frame_output) { + mapped_buffers_[buffer].push_back(Span{nullptr, (int)size, fd}); + } else { + void* memory = + mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + mapped_buffers_[buffer].push_back( + Span{static_cast(memory), (int)size, 0}); + } + } + } + frame_buffer_.push(buffer); + } + + while (true) { + if (frame_buffer_.empty()) { + break; + } + auto request = libcameracpp_Camera_createRequest(camera_.get()); + if (!request) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to create request"; + return -1; + } + requests_.push_back(request); + libcamerac_FrameBuffer* buffer = frame_buffer_.front(); + frame_buffer_.pop(); + if (libcamerac_Request_addBuffer(requests_.back().get(), stream_, buffer) < + 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to add buffer to request"; + return -1; + } + } + + if (libcamerac_Camera_start(camera_.get(), controls_.get())) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to start camera"; + return -1; + } + libcamerac_ControlList_clear(controls_.get()); + camera_started_ = true; + + auto signal = libcamerac_Camera_requestCompleted(camera_.get()); + libcamerac_Signal_Request_connect( + signal, &LibcameraCapturer::requestCompleteStatic, this); + + for (auto& request : requests_) { + if (libcamerac_Camera_queueRequest(camera_.get(), request.get()) < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to queue request"; + return -1; + } + } + return 0; +} + +int32_t LibcameraCapturer::StopCapture() { + { + std::lock_guard lock(camera_stop_mutex_); + if (camera_started_) { + if (libcamerac_Camera_stop(camera_.get())) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to stop camera"; + return -1; + } + camera_started_ = false; + } + } + + if (camera_) { + auto signal = libcamerac_Camera_requestCompleted(camera_.get()); + libcamerac_Signal_Request_disconnect( + signal, &LibcameraCapturer::requestCompleteStatic, this); + } + + requests_.clear(); + + return 0; +} + +void LibcameraCapturer::requestCompleteStatic(libcamerac_Request* request, + void* user_data) { + auto self = static_cast(user_data); + self->requestComplete(request); +} + +void LibcameraCapturer::requestComplete(libcamerac_Request* request) { + if (libcamerac_Request_status(request) == + libcamerac_Request_Status_RequestCancelled) { + return; + } + + auto cfg = libcamerac_CameraConfiguration_at(configuration_.get(), 0); + + int width = libcamerac_StreamConfiguration_get_size_width(cfg); + int height = libcamerac_StreamConfiguration_get_size_height(cfg); + int stride = libcamerac_StreamConfiguration_get_stride(cfg); + + const int64_t timestamp_us = rtc::TimeMicros(); + int adapted_width, adapted_height, crop_width, crop_height, crop_x, crop_y; + if (!AdaptFrame(width, height, timestamp_us, &adapted_width, &adapted_height, + &crop_width, &crop_height, &crop_x, &crop_y)) { + RTC_LOG(LS_INFO) << "Drop frame"; + queueRequest(request); + return; + } + + libcamerac_FrameBuffer* buffer = + libcamerac_Request_findBuffer(request, stream_); + auto item = mapped_buffers_.find(buffer); + if (item == mapped_buffers_.end()) { + return; + } + const std::vector& buffers = item->second; + + rtc::scoped_refptr frame_buffer; + if (buffers[0].buffer != nullptr) { + // メモリ出力なので I420Buffer に格納する + rtc::scoped_refptr i420_buffer( + webrtc::I420Buffer::Create(adapted_width, adapted_height)); + auto chroma_stride = stride / 2; + auto chroma_height = (height + 1) / 2; + auto src_y = buffers[0].buffer; + auto src_u = src_y + stride * height; + auto src_v = src_y + stride * height + chroma_stride * chroma_height; + if (libyuv::I420Scale(src_y, stride, src_u, stride / 2, src_v, stride / 2, + width, height, i420_buffer->MutableDataY(), + i420_buffer->StrideY(), i420_buffer->MutableDataU(), + i420_buffer->StrideU(), i420_buffer->MutableDataV(), + i420_buffer->StrideV(), adapted_width, adapted_height, + libyuv::kFilterBox) < 0) { + RTC_LOG(LS_ERROR) << "I420Scale Failed"; + } + + frame_buffer = i420_buffer; + + webrtc::VideoFrame video_frame = webrtc::VideoFrame::Builder() + .set_video_frame_buffer(frame_buffer) + .set_timestamp_rtp(0) + .set_timestamp_us(rtc::TimeMicros()) + .set_rotation(webrtc::kVideoRotation_0) + .build(); + OnFrame(video_frame); + queueRequest(request); + } else { + // DMA なので V4L2NativeBuffer に格納する + frame_buffer = rtc::make_ref_counted( + webrtc::VideoType::kI420, width, height, adapted_width, adapted_height, + buffers[0].fd, nullptr, buffers[0].length, stride, + [this, request]() { queueRequest(request); }); + RTC_LOG(LS_VERBOSE) << "V4L2NativeBuffer created: with=" << width + << " height=" << height << " stride=" << stride + << " adapted_width=" << adapted_width + << " adapted_height=" << adapted_height + << " fd=" << buffers[0].fd + << " buffers_size=" << buffers.size(); + + webrtc::VideoFrame video_frame = webrtc::VideoFrame::Builder() + .set_video_frame_buffer(frame_buffer) + .set_timestamp_rtp(0) + .set_timestamp_us(rtc::TimeMicros()) + .set_rotation(webrtc::kVideoRotation_0) + .build(); + OnFrame(video_frame); + } +} + +void LibcameraCapturer::queueRequest(libcamerac_Request* request) { + std::map buffers; + auto map = libcamerac_Request_buffers(request); + libcamerac_Request_BufferMap_foreach( + map, + [](const libcamerac_Stream* stream, libcamerac_FrameBuffer* buffer, + void* data) { + (*((std::map*) + data))[stream] = buffer; + }, + &buffers); + libcamerac_Request_reuse(request); + + std::lock_guard stop_lock(camera_stop_mutex_); + if (!camera_started_) { + return; + } + + for (auto const& p : buffers) { + if (libcamerac_Request_addBuffer(request, p.first, p.second) < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to add buffer to request"; + return; + } + } + + libcamerac_ControlList_copy(controls_.get(), + libcamerac_Request_controls(request)); + libcamerac_ControlList_clear(controls_.get()); + + if (libcamerac_Camera_queueRequest(camera_.get(), request) < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to queue request"; + return; + } +} diff --git a/src/hwenc_v4l2/libcamera_capturer.h b/src/hwenc_v4l2/libcamera_capturer.h new file mode 100644 index 00000000..d6a5ded8 --- /dev/null +++ b/src/hwenc_v4l2/libcamera_capturer.h @@ -0,0 +1,73 @@ +#ifndef LIBCAMERA_CAPTURER_H_ +#define LIBCAMERA_CAPTURER_H_ + +#include +#include +#include + +#include + +#include "libcamerac/libcameracpp.h" +#include "rtc/scalable_track_source.h" + +struct LibcameraCapturerConfig : V4L2VideoCapturerConfig { + LibcameraCapturerConfig() {} + LibcameraCapturerConfig(const V4L2VideoCapturerConfig& config) { + *static_cast(this) = config; + } + LibcameraCapturerConfig(const LibcameraCapturerConfig& config) { + *this = config; + } + // native_frame_output == true の場合、キャプチャしたデータを kNative なフレームとして渡す。 + // native_frame_output == false の場合、データをコピーして I420Buffer なフレームを作って渡す。 + // 前者の方が効率が良いけれども、kNative なフレームはサイマルキャスト時に自動で + // リサイズしてくれないので、状況に応じて使い分けるのが良い。 + bool native_frame_output = false; +}; + +// Raspberry Pi 専用のカメラからの映像を取得するクラス +// 出力の形式として、fd そのままで取得する形式と、メモリ上にコピーして取得する形式がある +// 渡されるフレームバッファは、fd そのままで取得する場合は V4L2NativeBuffer クラスになり、 +// メモリ上にコピーする場合は webrtc::I420Buffer クラスになる。 +class LibcameraCapturer : public ScalableVideoTrackSource { + public: + static rtc::scoped_refptr Create( + LibcameraCapturerConfig config); + static void LogDeviceList(); + LibcameraCapturer(); + ~LibcameraCapturer(); + + int32_t Init(int camera_id); + void Release(); + int32_t StartCapture(LibcameraCapturerConfig config); + + private: + static rtc::scoped_refptr Create( + LibcameraCapturerConfig config, + size_t capture_device_index); + int32_t StopCapture(); + static void requestCompleteStatic(libcamerac_Request* request, + void* user_data); + void requestComplete(libcamerac_Request* request); + void queueRequest(libcamerac_Request* request); + + std::shared_ptr camera_manager_; + std::shared_ptr camera_; + bool acquired_; + std::shared_ptr configuration_; + libcamerac_Stream* stream_; + std::shared_ptr allocator_ = nullptr; + struct Span { + uint8_t* buffer; + int length; + int fd; + }; + std::map> mapped_buffers_; + std::queue frame_buffer_; + std::vector> requests_; + std::shared_ptr controls_; + bool camera_started_; + std::mutex camera_stop_mutex_; +}; + +#endif // LIBCAMERA_CAPTURER_H_ \ No newline at end of file diff --git a/src/hwenc_v4l2/v4l2_buffers.cpp b/src/hwenc_v4l2/v4l2_buffers.cpp new file mode 100644 index 00000000..1bb49fc5 --- /dev/null +++ b/src/hwenc_v4l2/v4l2_buffers.cpp @@ -0,0 +1,142 @@ +#include "v4l2_buffers.h" + +#include + +// Linux +#include +#include + +// WebRTC +#include +#include + +int V4L2Buffers::Allocate(int fd, + int type, + int memory, + int req_count, + v4l2_format* format, + bool export_dmafds) { + Deallocate(); + + v4l2_requestbuffers reqbufs = {}; + reqbufs.count = req_count; + reqbufs.type = type; + reqbufs.memory = memory; + if (ioctl(fd, VIDIOC_REQBUFS, &reqbufs) < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to request buffers"; + return WEBRTC_VIDEO_CODEC_ERROR; + } + buffers_.resize(reqbufs.count); + RTC_LOG(LS_INFO) << "Request buffers: type=" << reqbufs.type + << " memory=" << reqbufs.memory << " count=" << reqbufs.count + << " capabilities=" << reqbufs.capabilities; + + if (memory == V4L2_MEMORY_MMAP) { + for (unsigned int i = 0; i < reqbufs.count; i++) { + v4l2_plane planes[VIDEO_MAX_PLANES]; + v4l2_buffer v4l2_buf = {}; + v4l2_buf.type = type; + v4l2_buf.memory = V4L2_MEMORY_MMAP; + v4l2_buf.index = i; + v4l2_buf.length = 1; + v4l2_buf.m.planes = planes; + if (ioctl(fd, VIDIOC_QUERYBUF, &v4l2_buf) < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to query buffer" + << " index: " << i; + return WEBRTC_VIDEO_CODEC_ERROR; + } + + Buffer buffer; + buffer.n_planes = v4l2_buf.length; + for (size_t j = 0; j < buffer.n_planes; j++) { + PlaneBuffer& plane = buffer.planes[j]; + if (!export_dmafds) { + plane.start = + mmap(0, v4l2_buf.m.planes[j].length, PROT_READ | PROT_WRITE, + MAP_SHARED, fd, v4l2_buf.m.planes[j].m.mem_offset); + if (plane.start == MAP_FAILED) { + RTC_LOG(LS_ERROR) + << __FUNCTION__ << " Failed to map plane buffer" + << " buffer index: " << i << " plane index: " << j; + return WEBRTC_VIDEO_CODEC_ERROR; + } + plane.length = v4l2_buf.m.planes[j].length; + } else { + v4l2_exportbuffer expbuf = {}; + expbuf.type = type; + expbuf.index = i; + expbuf.plane = j; + if (ioctl(fd, VIDIOC_EXPBUF, &expbuf) < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to export buffer" + << " index=" << i << " plane=" << j + << " error=" << strerror(errno); + return WEBRTC_VIDEO_CODEC_ERROR; + } + plane.fd = expbuf.fd; + } + plane.sizeimage = format->fmt.pix_mp.plane_fmt[j].sizeimage; + plane.bytesperline = format->fmt.pix_mp.plane_fmt[j].bytesperline; + } + buffers_[i] = buffer; + } + } + fd_ = fd; + type_ = type; + memory_ = memory; + export_dmafds_ = export_dmafds; + + return WEBRTC_VIDEO_CODEC_OK; +} + +void V4L2Buffers::Deallocate() { + for (int i = 0; i < buffers_.size(); i++) { + Buffer& buffer = buffers_[i]; + for (size_t j = 0; j < buffer.n_planes; j++) { + PlaneBuffer* plane = &buffer.planes[j]; + if (plane->start != nullptr) { + if (munmap(plane->start, plane->length) < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to unmap buffer" + << " index: " << i; + } + } + if (plane->fd != 0) { + close(plane->fd); + } + } + } + buffers_.clear(); + + if (fd_ != 0) { + v4l2_requestbuffers reqbufs = {}; + reqbufs.count = 0; + reqbufs.type = type_; + reqbufs.memory = memory_; + if (ioctl(fd_, VIDIOC_REQBUFS, &reqbufs) < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to free buffers: error=" + << strerror(errno); + } + fd_ = 0; + type_ = 0; + memory_ = 0; + } +} + +V4L2Buffers::~V4L2Buffers() { + Deallocate(); +} + +int V4L2Buffers::type() const { + return type_; +} +int V4L2Buffers::memory() const { + return memory_; +} +int V4L2Buffers::count() const { + return buffers_.size(); +} +bool V4L2Buffers::dmafds_exported() const { + return export_dmafds_; +} +V4L2Buffers::Buffer& V4L2Buffers::at(int index) { + return buffers_.at(index); +} diff --git a/src/hwenc_v4l2/v4l2_buffers.h b/src/hwenc_v4l2/v4l2_buffers.h new file mode 100644 index 00000000..4c778a43 --- /dev/null +++ b/src/hwenc_v4l2/v4l2_buffers.h @@ -0,0 +1,49 @@ +#ifndef V4L2_BUFFERS_H_ +#define V4L2_BUFFERS_H_ + +#include + +// Linux +#include + +// MMAP なバッファと DMABUF なバッファを扱うためのクラス +class V4L2Buffers { + public: + struct PlaneBuffer { + void* start = nullptr; + size_t length = 0; + int sizeimage = 0; + int bytesperline = 0; + int fd = 0; + }; + struct Buffer { + PlaneBuffer planes[VIDEO_MAX_PLANES]; + size_t n_planes = 0; + }; + + int Allocate(int fd, + int type, + int memory, + int req_count, + v4l2_format* format, + bool export_dmafds); + + void Deallocate(); + + ~V4L2Buffers(); + + int type() const; + int memory() const; + int count() const; + bool dmafds_exported() const; + Buffer& at(int index); + + private: + int fd_ = 0; + int type_ = 0; + int memory_ = 0; + bool export_dmafds_ = false; + std::vector buffers_; +}; + +#endif diff --git a/src/hwenc_v4l2/v4l2_capturer.cpp b/src/hwenc_v4l2/v4l2_capturer.cpp new file mode 100644 index 00000000..cedc9432 --- /dev/null +++ b/src/hwenc_v4l2/v4l2_capturer.cpp @@ -0,0 +1,97 @@ +#include "v4l2_capturer.h" + +// Linux +#include + +// WebRTC +#include +#include +#include + +#include "v4l2_native_buffer.h" + +rtc::scoped_refptr V4L2Capturer::Create( + V4L2VideoCapturerConfig config) { + rtc::scoped_refptr capturer; + std::unique_ptr device_info( + webrtc::VideoCaptureFactory::CreateDeviceInfo()); + if (!device_info) { + RTC_LOG(LS_ERROR) << "Failed to CreateDeviceInfo"; + return nullptr; + } + + LogDeviceList(device_info.get()); + + for (int i = 0; i < device_info->NumberOfDevices(); ++i) { + capturer = Create(device_info.get(), config, i); + if (capturer) { + RTC_LOG(LS_INFO) << "Get Capture"; + return capturer; + } + } + RTC_LOG(LS_ERROR) << "Failed to create V4L2Capturer"; + return nullptr; +} + +V4L2Capturer::V4L2Capturer(const V4L2VideoCapturerConfig& config) + : V4L2VideoCapturer(config) {} + +bool V4L2Capturer::UseNativeBuffer() { + return true; +} + +rtc::scoped_refptr V4L2Capturer::Create( + webrtc::VideoCaptureModule::DeviceInfo* device_info, + V4L2VideoCapturerConfig config, + size_t capture_device_index) { + char device_name[256]; + char unique_name[256]; + + if (device_info->GetDeviceName(static_cast(capture_device_index), + device_name, sizeof(device_name), unique_name, + sizeof(unique_name)) != 0) { + RTC_LOG(LS_WARNING) << "Failed to GetDeviceName"; + return nullptr; + } + + rtc::scoped_refptr v4l2_capturer = + rtc::make_ref_counted(config); + + if (v4l2_capturer->Init((const char*)&unique_name, config.video_device) < 0) { + RTC_LOG(LS_WARNING) << "Failed to create V4L2Capturer(" << unique_name + << ")"; + return nullptr; + } + + if (v4l2_capturer->StartCapture(config) < 0) { + RTC_LOG(LS_WARNING) << "Failed to start V4L2Capturer(w = " << config.width + << ", h = " << config.height + << ", fps = " << config.framerate << ")"; + return nullptr; + } + + return v4l2_capturer; +} + +void V4L2Capturer::OnCaptured(uint8_t* data, uint32_t bytesused) { + const int64_t timestamp_us = rtc::TimeMicros(); + int adapted_width, adapted_height, crop_width, crop_height, crop_x, crop_y; + if (!AdaptFrame(_currentWidth, _currentHeight, timestamp_us, &adapted_width, + &adapted_height, &crop_width, &crop_height, &crop_x, + &crop_y)) { + return; + } + + auto frame_buffer = rtc::make_ref_counted( + webrtc::VideoType::kMJPEG, _currentWidth, _currentHeight, adapted_width, + adapted_height, 0, data, bytesused, _currentWidth, nullptr); + + webrtc::VideoFrame video_frame = webrtc::VideoFrame::Builder() + .set_video_frame_buffer(frame_buffer) + .set_timestamp_rtp(0) + .set_timestamp_ms(rtc::TimeMillis()) + .set_timestamp_us(rtc::TimeMicros()) + .set_rotation(webrtc::kVideoRotation_0) + .build(); + OnFrame(video_frame); +} \ No newline at end of file diff --git a/src/hwenc_v4l2/v4l2_capturer.h b/src/hwenc_v4l2/v4l2_capturer.h new file mode 100644 index 00000000..d6e102a3 --- /dev/null +++ b/src/hwenc_v4l2/v4l2_capturer.h @@ -0,0 +1,23 @@ +#ifndef V4L2_CAPTURER_H_ +#define V4L2_CAPTURER_H_ + +#include "../v4l2_video_capturer/v4l2_video_capturer.h" + +class V4L2Capturer : public V4L2VideoCapturer { + public: + static rtc::scoped_refptr Create( + V4L2VideoCapturerConfig config); + V4L2Capturer(const V4L2VideoCapturerConfig& config); + + bool UseNativeBuffer() override; + + private: + static rtc::scoped_refptr Create( + webrtc::VideoCaptureModule::DeviceInfo* device_info, + V4L2VideoCapturerConfig config, + size_t capture_device_index); + + void OnCaptured(uint8_t* data, uint32_t bytesused) override; +}; + +#endif // NVCODEC_V4L2_CAPTURER_H_ diff --git a/src/hwenc_v4l2/v4l2_converter.cpp b/src/hwenc_v4l2/v4l2_converter.cpp new file mode 100644 index 00000000..3c0f0a9d --- /dev/null +++ b/src/hwenc_v4l2/v4l2_converter.cpp @@ -0,0 +1,745 @@ +#include "v4l2_converter.h" + +#include + +// Linux +#include +#include + +// WebRTC +#include +#include +#include +#include +#include +#include + +#include "v4l2_native_buffer.h" + +// V4L2Helper + +void V4L2Helper::InitFormat(int type, + int width, + int height, + int pixelformat, + int bytesperline, + int sizeimage, + v4l2_format* fmt) { + fmt->type = type; + fmt->fmt.pix_mp.width = width; + fmt->fmt.pix_mp.height = height; + fmt->fmt.pix_mp.pixelformat = pixelformat; + fmt->fmt.pix_mp.field = V4L2_FIELD_ANY; + fmt->fmt.pix_mp.colorspace = V4L2_COLORSPACE_DEFAULT; + fmt->fmt.pix_mp.num_planes = 1; + fmt->fmt.pix_mp.plane_fmt[0].bytesperline = bytesperline; + fmt->fmt.pix_mp.plane_fmt[0].sizeimage = sizeimage; +} + +int V4L2Helper::QueueBuffers(int fd, const V4L2Buffers& buffers) { + for (int i = 0; i < buffers.count(); i++) { + v4l2_plane planes[VIDEO_MAX_PLANES]; + v4l2_buffer v4l2_buf = {}; + v4l2_buf.type = buffers.type(); + v4l2_buf.memory = buffers.memory(); + v4l2_buf.index = i; + v4l2_buf.length = 1; + v4l2_buf.m.planes = planes; + if (ioctl(fd, VIDIOC_QBUF, &v4l2_buf) < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to queue buffer" + << " type=" << buffers.type() + << " memory=" << buffers.memory() << " index=" << i + << " error=" << strerror(errno); + return WEBRTC_VIDEO_CODEC_ERROR; + } + } + return WEBRTC_VIDEO_CODEC_OK; +} + +// V4L2H264EncodeConverter +std::shared_ptr V4L2H264EncodeConverter::Create( + int src_memory, + int src_width, + int src_height, + int src_stride) { + auto p = std::make_shared(); + if (p->Init(src_memory, src_width, src_height, src_stride) != + WEBRTC_VIDEO_CODEC_OK) { + return nullptr; + } + return p; +} + +int V4L2H264EncodeConverter::Init(int src_memory, + int src_width, + int src_height, + int src_stride) { + const char device_name[] = "/dev/video11"; + fd_ = open(device_name, O_RDWR, 0); + if (fd_ < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to create v4l2 encoder"; + return WEBRTC_VIDEO_CODEC_ERROR; + } + + v4l2_control ctrl = {}; + ctrl.id = V4L2_CID_MPEG_VIDEO_H264_PROFILE; + ctrl.value = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH; + if (ioctl(fd_, VIDIOC_S_CTRL, &ctrl) < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to set profile"; + return WEBRTC_VIDEO_CODEC_ERROR; + } + + ctrl.id = V4L2_CID_MPEG_VIDEO_H264_LEVEL; + ctrl.value = V4L2_MPEG_VIDEO_H264_LEVEL_4_2; + if (ioctl(fd_, VIDIOC_S_CTRL, &ctrl) < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to set level"; + return WEBRTC_VIDEO_CODEC_ERROR; + } + + ctrl.id = V4L2_CID_MPEG_VIDEO_H264_I_PERIOD; + ctrl.value = 500; + if (ioctl(fd_, VIDIOC_S_CTRL, &ctrl) < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to set intra period"; + return WEBRTC_VIDEO_CODEC_ERROR; + } + + ctrl.id = V4L2_CID_MPEG_VIDEO_REPEAT_SEQ_HEADER; + ctrl.value = 1; + if (ioctl(fd_, VIDIOC_S_CTRL, &ctrl) < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to enable inline header"; + return WEBRTC_VIDEO_CODEC_ERROR; + } + + v4l2_format src_fmt = {}; + V4L2Helper::InitFormat(V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, src_width, + src_height, V4L2_PIX_FMT_YUV420, src_stride, 0, + &src_fmt); + if (ioctl(fd_, VIDIOC_S_FMT, &src_fmt) < 0) { + RTC_LOG(LS_ERROR) << "Failed to set output format"; + return WEBRTC_VIDEO_CODEC_ERROR; + } + RTC_LOG(LS_INFO) << __FUNCTION__ << " Output buffer format" + << " width:" << src_fmt.fmt.pix_mp.width + << " height:" << src_fmt.fmt.pix_mp.height + << " bytesperline:" + << src_fmt.fmt.pix_mp.plane_fmt[0].bytesperline; + + v4l2_format dst_fmt = {}; + V4L2Helper::InitFormat(V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, src_width, + src_height, V4L2_PIX_FMT_H264, 0, 512 << 10, &dst_fmt); + if (ioctl(fd_, VIDIOC_S_FMT, &dst_fmt) < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to set capture format"; + return WEBRTC_VIDEO_CODEC_ERROR; + } + + int r = + src_buffers_.Allocate(fd_, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, src_memory, + NUM_OUTPUT_BUFFERS, &src_fmt, false); + if (r != WEBRTC_VIDEO_CODEC_OK) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to allocate output buffers"; + return r; + } + + r = dst_buffers_.Allocate(fd_, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, + V4L2_MEMORY_MMAP, NUM_CAPTURE_BUFFERS, &dst_fmt, + false); + if (r != WEBRTC_VIDEO_CODEC_OK) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to request output buffers"; + return r; + } + + r = V4L2Helper::QueueBuffers(fd_, dst_buffers_); + if (r != WEBRTC_VIDEO_CODEC_OK) { + return r; + } + + return WEBRTC_VIDEO_CODEC_OK; +} + +int V4L2H264EncodeConverter::fd() const { + return fd_; +} + +int V4L2H264EncodeConverter::Encode( + const rtc::scoped_refptr& frame_buffer, + int64_t timestamp_us, + bool force_key_frame, + OnCompleteCallback on_complete) { + if (force_key_frame) { + v4l2_control ctrl = {}; + ctrl.id = V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME; + ctrl.value = 1; + if (ioctl(fd_, VIDIOC_S_CTRL, &ctrl) < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to request I frame"; + } + } + + if (!runner_) { + v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; + if (ioctl(fd_, VIDIOC_STREAMON, &type) < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to start output stream"; + return WEBRTC_VIDEO_CODEC_ERROR; + } + type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; + if (ioctl(fd_, VIDIOC_STREAMON, &type) < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to start capture stream"; + return WEBRTC_VIDEO_CODEC_ERROR; + } + + runner_ = V4L2Runner::Create("H264Encoder", fd_, src_buffers_.count(), + src_buffers_.memory(), V4L2_MEMORY_MMAP); + } + + std::optional index = runner_->PopAvailableBufferIndex(); + if (!index) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " No available output buffers"; + return WEBRTC_VIDEO_CODEC_ERROR; + } + + rtc::scoped_refptr bind_buffer; + v4l2_buffer v4l2_buf = {}; + v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; + v4l2_buf.index = *index; + v4l2_buf.field = V4L2_FIELD_NONE; + v4l2_buf.length = 1; + v4l2_plane planes[VIDEO_MAX_PLANES] = {}; + v4l2_buf.m.planes = planes; + v4l2_buf.flags |= V4L2_BUF_FLAG_TIMESTAMP_COPY; + v4l2_buf.timestamp.tv_sec = timestamp_us / rtc::kNumMicrosecsPerSec; + v4l2_buf.timestamp.tv_usec = timestamp_us % rtc::kNumMicrosecsPerSec; + if (frame_buffer->type() == webrtc::VideoFrameBuffer::Type::kNative) { + v4l2_buf.memory = V4L2_MEMORY_DMABUF; + auto native_buffer = static_cast(frame_buffer.get()); + planes[0].m.fd = native_buffer->fd(); + planes[0].bytesused = native_buffer->size(); + planes[0].length = native_buffer->size(); + bind_buffer = frame_buffer; + } else { + v4l2_buf.memory = V4L2_MEMORY_MMAP; + + auto& src_buffer = src_buffers_.at(v4l2_buf.index); + + rtc::scoped_refptr i420_buffer = + frame_buffer->ToI420(); + int width = i420_buffer->width(); + int height = i420_buffer->height(); + int dst_stride = src_buffer.planes[0].bytesperline; + int dst_chroma_stride = (dst_stride + 1) / 2; + int dst_chroma_height = (height + 1) / 2; + uint8_t* dst_y = (uint8_t*)src_buffer.planes[0].start; + uint8_t* dst_u = dst_y + dst_stride * height; + uint8_t* dst_v = dst_u + dst_chroma_stride * dst_chroma_height; + libyuv::I420Copy(i420_buffer->DataY(), i420_buffer->StrideY(), + i420_buffer->DataU(), i420_buffer->StrideU(), + i420_buffer->DataV(), i420_buffer->StrideV(), dst_y, + dst_stride, dst_u, dst_chroma_stride, dst_v, + dst_chroma_stride, width, height); + bind_buffer = i420_buffer; + } + + runner_->Enqueue( + &v4l2_buf, [this, bind_buffer, on_complete]( + v4l2_buffer* v4l2_buf, std::function on_next) { + int64_t timestamp_us = + v4l2_buf->timestamp.tv_sec * rtc::kNumMicrosecsPerSec + + v4l2_buf->timestamp.tv_usec; + bool is_key_frame = !!(v4l2_buf->flags & V4L2_BUF_FLAG_KEYFRAME); + V4L2Buffers::PlaneBuffer& plane = + dst_buffers_.at(v4l2_buf->index).planes[0]; + on_complete((uint8_t*)plane.start, v4l2_buf->m.planes[0].bytesused, + timestamp_us, is_key_frame); + on_next(); + }); + + return WEBRTC_VIDEO_CODEC_OK; +} + +V4L2H264EncodeConverter::~V4L2H264EncodeConverter() { + runner_.reset(); + + v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; + if (ioctl(fd_, VIDIOC_STREAMOFF, &type) < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to stop output stream"; + } + type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; + if (ioctl(fd_, VIDIOC_STREAMOFF, &type) < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to stop capture stream"; + } + + src_buffers_.Deallocate(); + dst_buffers_.Deallocate(); + + close(fd_); +} + +// V4L2ScaleConverter + +std::shared_ptr V4L2ScaleConverter::Create( + int src_memory, + int src_width, + int src_height, + int src_stride, + bool dst_export_dmafds, + int dst_width, + int dst_height, + int dst_stride) { + auto p = std::make_shared(); + if (p->Init(src_memory, src_width, src_height, src_stride, dst_export_dmafds, + dst_width, dst_height, dst_stride) != WEBRTC_VIDEO_CODEC_OK) { + return nullptr; + } + return p; +} + +int V4L2ScaleConverter::Init(int src_memory, + int src_width, + int src_height, + int src_stride, + bool dst_export_dmafds, + int dst_width, + int dst_height, + int dst_stride) { + RTC_LOG(LS_INFO) << "V4L2ScaleConverter::Init src_memory=" << src_memory + << " src_width=" << src_width << " src_height=" << src_height + << " src_stride=" << src_stride + << " dst_export_dmafds=" << dst_export_dmafds + << " dst_width=" << dst_width << " dst_height=" << dst_height + << " dst_stride=" << dst_stride; + + const char device_name[] = "/dev/video12"; + fd_ = open(device_name, O_RDWR, 0); + if (fd_ < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to create v4l2 scaler"; + return WEBRTC_VIDEO_CODEC_ERROR; + } + + v4l2_format src_fmt = {}; + V4L2Helper::InitFormat(V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, src_width, + src_height, V4L2_PIX_FMT_YUV420, src_stride, 0, + &src_fmt); + if (ioctl(fd_, VIDIOC_S_FMT, &src_fmt) < 0) { + RTC_LOG(LS_ERROR) << "Failed to set output format"; + return WEBRTC_VIDEO_CODEC_ERROR; + } + RTC_LOG(LS_INFO) << __FUNCTION__ << " Output buffer format" + << " width:" << src_fmt.fmt.pix_mp.width + << " height:" << src_fmt.fmt.pix_mp.height + << " bytesperline:" + << src_fmt.fmt.pix_mp.plane_fmt[0].bytesperline; + + v4l2_format dst_fmt = {}; + V4L2Helper::InitFormat(V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, dst_width, + dst_height, V4L2_PIX_FMT_YUV420, dst_stride, 0, + &dst_fmt); + if (ioctl(fd_, VIDIOC_S_FMT, &dst_fmt) < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to set capture format"; + return WEBRTC_VIDEO_CODEC_ERROR; + } + + int r = + src_buffers_.Allocate(fd_, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, src_memory, + NUM_OUTPUT_BUFFERS, &src_fmt, false); + if (r != WEBRTC_VIDEO_CODEC_OK) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to request output buffers"; + return WEBRTC_VIDEO_CODEC_ERROR; + } + + r = dst_buffers_.Allocate(fd_, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, + V4L2_MEMORY_MMAP, NUM_CAPTURE_BUFFERS, &dst_fmt, + dst_export_dmafds); + if (r != WEBRTC_VIDEO_CODEC_OK) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to request output buffers"; + return WEBRTC_VIDEO_CODEC_ERROR; + } + + r = V4L2Helper::QueueBuffers(fd_, dst_buffers_); + if (r != WEBRTC_VIDEO_CODEC_OK) { + return r; + } + + v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; + if (ioctl(fd_, VIDIOC_STREAMON, &type) < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to start output stream"; + return WEBRTC_VIDEO_CODEC_ERROR; + } + type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; + if (ioctl(fd_, VIDIOC_STREAMON, &type) < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to start capture stream"; + return WEBRTC_VIDEO_CODEC_ERROR; + } + + dst_width_ = dst_width; + dst_height_ = dst_height; + dst_stride_ = dst_stride; + + runner_ = V4L2Runner::Create("Scaler", fd_, src_buffers_.count(), src_memory, + V4L2_MEMORY_MMAP); + + return WEBRTC_VIDEO_CODEC_OK; +} + +int V4L2ScaleConverter::Scale( + const rtc::scoped_refptr& frame_buffer, + int64_t timestamp_us, + OnCompleteCallback on_complete) { + RTC_LOG(LS_VERBOSE) << "V4L2ScaleConverter::Scale"; + std::optional index = runner_->PopAvailableBufferIndex(); + if (!index) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " No available output buffers"; + return WEBRTC_VIDEO_CODEC_ERROR; + } + + rtc::scoped_refptr bind_buffer; + v4l2_buffer v4l2_buf = {}; + v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; + v4l2_buf.index = *index; + v4l2_buf.field = V4L2_FIELD_NONE; + v4l2_buf.length = 1; + v4l2_plane planes[VIDEO_MAX_PLANES] = {}; + v4l2_buf.m.planes = planes; + v4l2_buf.flags |= V4L2_BUF_FLAG_TIMESTAMP_COPY; + v4l2_buf.timestamp.tv_sec = timestamp_us / rtc::kNumMicrosecsPerSec; + v4l2_buf.timestamp.tv_usec = timestamp_us % rtc::kNumMicrosecsPerSec; + if (frame_buffer->type() == webrtc::VideoFrameBuffer::Type::kNative) { + v4l2_buf.memory = V4L2_MEMORY_DMABUF; + auto native_buffer = static_cast(frame_buffer.get()); + planes[0].m.fd = native_buffer->fd(); + planes[0].bytesused = native_buffer->size(); + planes[0].length = native_buffer->size(); + bind_buffer = frame_buffer; + } else { + v4l2_buf.memory = V4L2_MEMORY_MMAP; + + auto& d_buffer = src_buffers_.at(v4l2_buf.index); + + rtc::scoped_refptr s_buffer = + frame_buffer->ToI420(); + int width = s_buffer->width(); + int height = s_buffer->height(); + int d_stride = d_buffer.planes[0].bytesperline; + int d_chroma_stride = (d_stride + 1) / 2; + int d_chroma_height = (height + 1) / 2; + uint8_t* d_y = (uint8_t*)d_buffer.planes[0].start; + uint8_t* d_u = d_y + d_stride * height; + uint8_t* d_v = d_u + d_chroma_stride * d_chroma_height; + libyuv::I420Copy(s_buffer->DataY(), s_buffer->StrideY(), s_buffer->DataU(), + s_buffer->StrideU(), s_buffer->DataV(), + s_buffer->StrideV(), d_y, d_stride, d_u, d_chroma_stride, + d_v, d_chroma_stride, width, height); + bind_buffer = s_buffer; + } + + runner_->Enqueue( + &v4l2_buf, [this, bind_buffer, on_complete]( + v4l2_buffer* v4l2_buf, std::function on_next) { + int64_t timestamp_us = + v4l2_buf->timestamp.tv_sec * rtc::kNumMicrosecsPerSec + + v4l2_buf->timestamp.tv_usec; + if (dst_buffers_.dmafds_exported()) { + auto& plane = dst_buffers_.at(v4l2_buf->index).planes[0]; + RTC_LOG(LS_VERBOSE) + << "Scale completed: length=" << v4l2_buf->length + << " fd=" << plane.fd << " bytesused=" << plane.bytesperline; + auto frame_buffer = rtc::make_ref_counted( + webrtc::VideoType::kI420, dst_width_, dst_height_, dst_width_, + dst_height_, plane.fd, nullptr, plane.sizeimage, + plane.bytesperline, [on_next]() { on_next(); }); + on_complete(frame_buffer, timestamp_us); + } else { + auto d_buffer = webrtc::I420Buffer::Create(dst_width_, dst_height_); + int s_chroma_stride = (dst_stride_ + 1) / 2; + int s_chroma_height = (dst_height_ + 1) / 2; + auto& plane = dst_buffers_.at(v4l2_buf->index).planes[0]; + uint8_t* s_y = (uint8_t*)plane.start; + uint8_t* s_u = s_y + dst_stride_ * dst_height_; + uint8_t* s_v = s_u + s_chroma_stride * s_chroma_height; + libyuv::I420Copy(s_y, dst_stride_, s_u, s_chroma_stride, s_v, + s_chroma_stride, d_buffer->MutableDataY(), + d_buffer->StrideY(), d_buffer->MutableDataU(), + d_buffer->StrideU(), d_buffer->MutableDataV(), + d_buffer->StrideV(), dst_width_, dst_height_); + on_complete(d_buffer, timestamp_us); + on_next(); + } + }); + + return WEBRTC_VIDEO_CODEC_OK; +} + +V4L2ScaleConverter::~V4L2ScaleConverter() { + runner_.reset(); + + v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; + if (ioctl(fd_, VIDIOC_STREAMOFF, &type) < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to stop output stream"; + } + type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; + if (ioctl(fd_, VIDIOC_STREAMOFF, &type) < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to stop capture stream"; + } + + src_buffers_.Deallocate(); + dst_buffers_.Deallocate(); + + close(fd_); +} + +// V4L2DecodeConverter + +std::shared_ptr V4L2DecodeConverter::Create( + int src_pixelformat, + bool dst_export_dmafds) { + auto p = std::make_shared(); + if (p->Init(src_pixelformat, dst_export_dmafds) != WEBRTC_VIDEO_CODEC_OK) { + return nullptr; + } + return p; +} + +std::shared_ptr V4L2DecodeConverter::Create( + int src_pixelformat, + bool dst_export_dmafds, + int dst_width, + int dst_height, + int dst_stride) { + auto p = std::make_shared(); + if (p->Init(src_pixelformat, dst_export_dmafds, dst_width, dst_height, + dst_stride) != WEBRTC_VIDEO_CODEC_OK) { + return nullptr; + } + return p; +} + +int V4L2DecodeConverter::Init(int src_pixelformat, bool dst_export_dmafds) { + const char device_name[] = "/dev/video10"; + fd_ = open(device_name, O_RDWR, 0); + if (fd_ < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to create v4l2 decoder"; + return WEBRTC_VIDEO_CODEC_ERROR; + } + + v4l2_format src_fmt = {}; + V4L2Helper::InitFormat(V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 0, 0, + src_pixelformat, 0, 512 << 10, &src_fmt); + if (ioctl(fd_, VIDIOC_S_FMT, &src_fmt) < 0) { + RTC_LOG(LS_ERROR) << "Failed to set output format"; + return WEBRTC_VIDEO_CODEC_ERROR; + } + + int r = src_buffers_.Allocate(fd_, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, + V4L2_MEMORY_MMAP, NUM_OUTPUT_BUFFERS, &src_fmt, + false); + if (r != WEBRTC_VIDEO_CODEC_OK) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to allocate output buffers"; + return r; + } + + v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; + if (ioctl(fd_, VIDIOC_STREAMON, &type) < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to start output stream"; + return WEBRTC_VIDEO_CODEC_ERROR; + } + + auto runner_name = src_pixelformat == V4L2_PIX_FMT_H264 ? "H264Decoder" + : src_pixelformat == V4L2_PIX_FMT_MJPEG ? "MJPEGDecoder" + : "Decoder"; + runner_ = V4L2Runner::Create( + runner_name, fd_, src_buffers_.count(), V4L2_MEMORY_MMAP, + V4L2_MEMORY_MMAP, + // 画像サイズが変わった場合に呼び出される + [this, dst_export_dmafds]() { + // 全てのストリームを止めて、バッファをクリアする + int type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; + if (ioctl(fd_, VIDIOC_STREAMOFF, &type) < 0) { + RTC_LOG(LS_ERROR) + << __FUNCTION__ << " Failed to start capture stream"; + return; + } + dst_buffers_.Deallocate(); + + // デコードされたイメージの新しいサイズを取得する + v4l2_format dst_fmt = {}; + dst_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; + if (ioctl(fd_, VIDIOC_G_FMT, &dst_fmt) < 0) { + RTC_LOG(LS_ERROR) << "Failed to get format"; + return; + } + + RTC_LOG(LS_INFO) << " On change capture buffer resolution" + << " width:" << dst_fmt.fmt.pix_mp.width + << " height:" << dst_fmt.fmt.pix_mp.height + << " bytesperline:" + << dst_fmt.fmt.pix_mp.plane_fmt[0].bytesperline; + + // capture バッファを作り直してキューに詰める + int r = dst_buffers_.Allocate(fd_, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, + V4L2_MEMORY_MMAP, NUM_CAPTURE_BUFFERS, + &dst_fmt, dst_export_dmafds); + if (r != WEBRTC_VIDEO_CODEC_OK) { + RTC_LOG(LS_ERROR) << "Failed to allocate capture buffers"; + return; + } + + r = V4L2Helper::QueueBuffers(fd_, dst_buffers_); + if (r != WEBRTC_VIDEO_CODEC_OK) { + return; + } + + dst_width_ = dst_fmt.fmt.pix_mp.width; + dst_height_ = dst_fmt.fmt.pix_mp.height; + dst_stride_ = dst_fmt.fmt.pix_mp.plane_fmt[0].bytesperline; + + type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; + if (ioctl(fd_, VIDIOC_STREAMON, &type) < 0) { + RTC_LOG(LS_ERROR) + << __FUNCTION__ << " Failed to start capture stream"; + return; + } + + RTC_LOG(LS_INFO) << "Ready to decode capture stream"; + }); + + return WEBRTC_VIDEO_CODEC_OK; +} + +int V4L2DecodeConverter::Init(int src_pixelformat, + bool dst_export_dmafds, + int dst_width, + int dst_height, + int dst_stride) { + int r = Init(src_pixelformat, dst_export_dmafds); + if (r != WEBRTC_VIDEO_CODEC_OK) { + return r; + } + + v4l2_format dst_fmt = {}; + V4L2Helper::InitFormat(V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, dst_width, + dst_height, V4L2_PIX_FMT_YUV420, dst_stride, 0, + &dst_fmt); + if (ioctl(fd_, VIDIOC_S_FMT, &dst_fmt) < 0) { + RTC_LOG(LS_ERROR) << "Failed to set format"; + return WEBRTC_VIDEO_CODEC_ERROR; + } + + r = dst_buffers_.Allocate(fd_, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, + V4L2_MEMORY_MMAP, NUM_CAPTURE_BUFFERS, &dst_fmt, + dst_export_dmafds); + if (r != WEBRTC_VIDEO_CODEC_OK) { + RTC_LOG(LS_ERROR) << "Failed to allocate capture buffers"; + return r; + } + + r = V4L2Helper::QueueBuffers(fd_, dst_buffers_); + if (r != WEBRTC_VIDEO_CODEC_OK) { + return r; + } + + dst_width_ = dst_fmt.fmt.pix_mp.width; + dst_height_ = dst_fmt.fmt.pix_mp.height; + dst_stride_ = dst_fmt.fmt.pix_mp.plane_fmt[0].bytesperline; + + int type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; + if (ioctl(fd_, VIDIOC_STREAMON, &type) < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to start capture stream"; + return WEBRTC_VIDEO_CODEC_ERROR; + } + + RTC_LOG(LS_INFO) << "Init capture buffer resolution" + << " width:" << dst_fmt.fmt.pix_mp.width + << " height:" << dst_fmt.fmt.pix_mp.height + << " bytesperline:" + << dst_fmt.fmt.pix_mp.plane_fmt[0].bytesperline; + + return WEBRTC_VIDEO_CODEC_OK; +} + +int V4L2DecodeConverter::fd() const { + return fd_; +} + +int V4L2DecodeConverter::Decode(const uint8_t* data, + int size, + int64_t timestamp_rtp, + OnCompleteCallback on_complete) { + std::optional index = runner_->PopAvailableBufferIndex(); + RTC_LOG(LS_VERBOSE) << "Decode: index=" << index.value_or(-1); + if (!index) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " No available output buffers"; + return WEBRTC_VIDEO_CODEC_ERROR; + } + + v4l2_buffer v4l2_buf = {}; + v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; + v4l2_buf.memory = V4L2_MEMORY_MMAP; + v4l2_buf.index = *index; + v4l2_buf.field = V4L2_FIELD_NONE; + v4l2_buf.length = 1; + v4l2_plane planes[VIDEO_MAX_PLANES] = {}; + v4l2_buf.m.planes = planes; + v4l2_buf.flags |= V4L2_BUF_FLAG_TIMESTAMP_COPY; + // RTP なんだけど無理やり us として扱う + v4l2_buf.timestamp.tv_sec = timestamp_rtp / rtc::kNumMicrosecsPerSec; + v4l2_buf.timestamp.tv_usec = timestamp_rtp % rtc::kNumMicrosecsPerSec; + + auto& buffer = src_buffers_.at(*index); + memcpy(buffer.planes[0].start, data, size); + buffer.planes[0].sizeimage = size; + + v4l2_buf.m.planes[0].bytesused = buffer.planes[0].sizeimage; + + runner_->Enqueue( + &v4l2_buf, [this, on_complete](v4l2_buffer* v4l2_buf, + std::function on_next) { + int64_t timestamp_rtp = + v4l2_buf->timestamp.tv_sec * rtc::kNumMicrosecsPerSec + + v4l2_buf->timestamp.tv_usec; + if (dst_buffers_.dmafds_exported()) { + auto& plane = dst_buffers_.at(v4l2_buf->index).planes[0]; + RTC_LOG(LS_VERBOSE) + << "Decode completed: fd=" << plane.fd << " width=" << dst_width_ + << " height=" << dst_height_; + auto frame_buffer = rtc::make_ref_counted( + webrtc::VideoType::kI420, dst_width_, dst_height_, dst_width_, + dst_height_, plane.fd, nullptr, plane.sizeimage, + plane.bytesperline, [on_next]() { on_next(); }); + on_complete(frame_buffer, timestamp_rtp); + } else { + auto d_buffer = webrtc::I420Buffer::Create(dst_width_, dst_height_); + int s_chroma_stride = (dst_stride_ + 1) / 2; + int s_chroma_height = (dst_height_ + 1) / 2; + auto& plane = dst_buffers_.at(v4l2_buf->index).planes[0]; + uint8_t* s_y = (uint8_t*)plane.start; + uint8_t* s_u = s_y + dst_stride_ * dst_height_; + uint8_t* s_v = s_u + s_chroma_stride * s_chroma_height; + RTC_LOG(LS_VERBOSE) << "Decoded image: width=" << dst_width_ + << " height=" << dst_height_; + libyuv::I420Copy(s_y, dst_stride_, s_u, s_chroma_stride, s_v, + s_chroma_stride, d_buffer->MutableDataY(), + d_buffer->StrideY(), d_buffer->MutableDataU(), + d_buffer->StrideU(), d_buffer->MutableDataV(), + d_buffer->StrideV(), dst_width_, dst_height_); + on_complete(d_buffer, timestamp_rtp); + on_next(); + } + }); + + return WEBRTC_VIDEO_CODEC_OK; +} + +V4L2DecodeConverter::~V4L2DecodeConverter() { + runner_.reset(); + + v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; + if (ioctl(fd_, VIDIOC_STREAMOFF, &type) < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to stop output stream"; + } + type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; + if (ioctl(fd_, VIDIOC_STREAMOFF, &type) < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to stop capture stream"; + } + + src_buffers_.Deallocate(); + dst_buffers_.Deallocate(); + + close(fd_); +} diff --git a/src/hwenc_v4l2/v4l2_converter.h b/src/hwenc_v4l2/v4l2_converter.h new file mode 100644 index 00000000..5f182650 --- /dev/null +++ b/src/hwenc_v4l2/v4l2_converter.h @@ -0,0 +1,162 @@ +#ifndef V4L2_CONVERTER_H_ +#define V4L2_CONVERTER_H_ + +#include +#include + +// Linux +#include + +// WebRTC +#include +#include + +#include "v4l2_buffers.h" +#include "v4l2_runner.h" + +class V4L2Helper { + public: + static void InitFormat(int type, + int width, + int height, + int pixelformat, + int bytesperline, + int sizeimage, + v4l2_format* fmt); + static int QueueBuffers(int fd, const V4L2Buffers& buffers); +}; + +class V4L2H264EncodeConverter { + public: + typedef std::function OnCompleteCallback; + + static std::shared_ptr Create(int src_memory, + int src_width, + int src_height, + int src_stride); + + private: + static constexpr int NUM_OUTPUT_BUFFERS = 4; + static constexpr int NUM_CAPTURE_BUFFERS = 4; + + int Init(int src_memory, int src_width, int src_height, int src_stride); + + public: + int fd() const; + + int Encode(const rtc::scoped_refptr& frame_buffer, + int64_t timestamp_us, + bool force_key_frame, + OnCompleteCallback on_complete); + + ~V4L2H264EncodeConverter(); + + private: + int fd_ = 0; + + V4L2Buffers src_buffers_; + V4L2Buffers dst_buffers_; + + std::shared_ptr runner_; +}; + +class V4L2ScaleConverter { + public: + typedef std::function, + int64_t)> + OnCompleteCallback; + + static std::shared_ptr Create(int src_memory, + int src_width, + int src_height, + int src_stride, + bool dst_export_dmafds, + int dst_width, + int dst_height, + int dst_stride); + + private: + static constexpr int NUM_OUTPUT_BUFFERS = 4; + static constexpr int NUM_CAPTURE_BUFFERS = 4; + + int Init(int src_memory, + int src_width, + int src_height, + int src_stride, + bool dst_export_dmafds, + int dst_width, + int dst_height, + int dst_stride); + + public: + int Scale(const rtc::scoped_refptr& frame_buffer, + int64_t timestamp_us, + OnCompleteCallback on_complete); + + ~V4L2ScaleConverter(); + + private: + int fd_ = 0; + + int dst_width_ = 0; + int dst_height_ = 0; + int dst_stride_ = 0; + + V4L2Buffers src_buffers_; + V4L2Buffers dst_buffers_; + + std::shared_ptr runner_; +}; + +class V4L2DecodeConverter { + public: + typedef std::function, + int64_t)> + OnCompleteCallback; + + static std::shared_ptr Create(int src_pixelformat, + bool dst_export_dmafds); + + // デコード後のサイズが分かる(かつ V4L2_EVENT_SRC_CH_RESOLUTION イベントが飛んでこないフォーマットの)場合はこっちを使う + static std::shared_ptr Create(int src_pixelformat, + bool dst_export_dmafds, + int dst_width, + int dst_height, + int dst_stride); + + private: + static constexpr int NUM_OUTPUT_BUFFERS = 4; + static constexpr int NUM_CAPTURE_BUFFERS = 4; + + int Init(int src_pixelformat, bool dst_export_dmafds); + + int Init(int src_pixelformat, + bool dst_export_dmafds, + int dst_width, + int dst_height, + int dst_stride); + + public: + int fd() const; + + int Decode(const uint8_t* data, + int size, + int64_t timestamp_rtp, + OnCompleteCallback on_complete); + + ~V4L2DecodeConverter(); + + private: + int fd_ = 0; + + int dst_width_ = 0; + int dst_height_ = 0; + int dst_stride_ = 0; + + V4L2Buffers src_buffers_; + V4L2Buffers dst_buffers_; + + std::shared_ptr runner_; +}; + +#endif diff --git a/src/hwenc_v4l2/v4l2_h264_decoder.cpp b/src/hwenc_v4l2/v4l2_h264_decoder.cpp new file mode 100644 index 00000000..9e94e8ce --- /dev/null +++ b/src/hwenc_v4l2/v4l2_h264_decoder.cpp @@ -0,0 +1,69 @@ +#include "v4l2_h264_decoder.h" + +#include + +// WebRTC +#include +#include +#include +#include +#include +#include + +V4L2H264Decoder::V4L2H264Decoder(webrtc::VideoCodecType codec) + : decoder_(nullptr), decode_complete_callback_(nullptr) {} + +V4L2H264Decoder::~V4L2H264Decoder() { + Release(); +} + +bool V4L2H264Decoder::Configure(const Settings& settings) { + decoder_ = V4L2DecodeConverter::Create(V4L2_PIX_FMT_H264, false); + if (decoder_ == nullptr) { + RTC_LOG(LS_ERROR) << "Failed to create decoder"; + return false; + } + return true; +} + +int32_t V4L2H264Decoder::Decode(const webrtc::EncodedImage& input_image, + bool missing_frames, + int64_t render_time_ms) { + if (decoder_ == nullptr) { + return WEBRTC_VIDEO_CODEC_UNINITIALIZED; + } + if (decode_complete_callback_ == NULL) { + return WEBRTC_VIDEO_CODEC_UNINITIALIZED; + } + if (input_image.data() == NULL && input_image.size() > 0) { + return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; + } + + decoder_->Decode( + input_image.data(), input_image.size(), input_image.Timestamp(), + [this](rtc::scoped_refptr buffer, + int64_t timestamp_rtp) { + webrtc::VideoFrame decoded_image = webrtc::VideoFrame::Builder() + .set_video_frame_buffer(buffer) + .set_timestamp_rtp(timestamp_rtp) + .build(); + decode_complete_callback_->Decoded(decoded_image, absl::nullopt, + absl::nullopt); + }); + + return WEBRTC_VIDEO_CODEC_OK; +} + +int32_t V4L2H264Decoder::RegisterDecodeCompleteCallback( + webrtc::DecodedImageCallback* callback) { + decode_complete_callback_ = callback; + return WEBRTC_VIDEO_CODEC_OK; +} + +int32_t V4L2H264Decoder::Release() { + return WEBRTC_VIDEO_CODEC_OK; +} + +const char* V4L2H264Decoder::ImplementationName() const { + return "V4L2 Video"; +} diff --git a/src/hwenc_v4l2/v4l2_h264_decoder.h b/src/hwenc_v4l2/v4l2_h264_decoder.h new file mode 100644 index 00000000..55e54256 --- /dev/null +++ b/src/hwenc_v4l2/v4l2_h264_decoder.h @@ -0,0 +1,30 @@ +#ifndef HWENC_V4L2_V4L2_H264_DECODER_H_ +#define HWENC_V4L2_V4L2_H264_DECODER_H_ + +// WebRTC +#include +#include +#include + +#include "v4l2_converter.h" + +class V4L2H264Decoder : public webrtc::VideoDecoder { + public: + V4L2H264Decoder(webrtc::VideoCodecType codec); + ~V4L2H264Decoder() override; + + bool Configure(const Settings& settings) override; + int32_t Decode(const webrtc::EncodedImage& input_image, + bool missing_frames, + int64_t render_time_ms) override; + int32_t RegisterDecodeCompleteCallback( + webrtc::DecodedImageCallback* callback) override; + int32_t Release() override; + const char* ImplementationName() const override; + + private: + webrtc::DecodedImageCallback* decode_complete_callback_; + std::shared_ptr decoder_; +}; + +#endif diff --git a/src/hwenc_v4l2/v4l2_h264_encoder.cpp b/src/hwenc_v4l2/v4l2_h264_encoder.cpp new file mode 100644 index 00000000..f9379c14 --- /dev/null +++ b/src/hwenc_v4l2/v4l2_h264_encoder.cpp @@ -0,0 +1,334 @@ +#include "v4l2_h264_encoder.h" + +#include +#include +#include + +#include +#include +#include +#include + +// WebRTC +#include +#include +#include +#include +#include +#include +#include +#include + +#include "v4l2_native_buffer.h" + +namespace { + +const int kLowH264QpThreshold = 34; +const int kHighH264QpThreshold = 40; + +} // namespace + +V4L2H264Encoder::V4L2H264Encoder(const cricket::VideoCodec& codec) + : configured_width_(0), + configured_height_(0), + callback_(nullptr), + bitrate_adjuster_(.5, .95), + target_framerate_fps_(30), + configured_framerate_fps_(30) {} + +V4L2H264Encoder::~V4L2H264Encoder() {} + +int32_t V4L2H264Encoder::InitEncode( + const webrtc::VideoCodec* codec_settings, + const webrtc::VideoEncoder::Settings& settings) { + RTC_DCHECK(codec_settings); + RTC_DCHECK_EQ(codec_settings->codecType, webrtc::kVideoCodecH264); + + target_bitrate_bps_ = codec_settings->startBitrate * 1000; + bitrate_adjuster_.SetTargetBitrateBps(target_bitrate_bps_); + + RTC_LOG(LS_INFO) << __FUNCTION__ << " width: " << codec_settings->width + << " height: " << codec_settings->height + << " bitrate: " << target_bitrate_bps_ << "bit/sec"; + + encoded_image_.timing_.flags = + webrtc::VideoSendTiming::TimingFrameFlags::kInvalid; + encoded_image_.content_type_ = + (codec_settings->mode == webrtc::VideoCodecMode::kScreensharing) + ? webrtc::VideoContentType::SCREENSHARE + : webrtc::VideoContentType::UNSPECIFIED; + + return Configure(webrtc::VideoFrameBuffer::Type::kI420, + webrtc::VideoType::kI420, codec_settings->width, + codec_settings->height, codec_settings->width, + codec_settings->width, codec_settings->height); +} + +int32_t V4L2H264Encoder::Configure(webrtc::VideoFrameBuffer::Type type, + webrtc::VideoType video_type, + int32_t raw_width, + int32_t raw_height, + int32_t raw_stride, + int32_t width, + int32_t height) { + int memory = type == webrtc::VideoFrameBuffer::Type::kNative + ? V4L2_MEMORY_DMABUF + : V4L2_MEMORY_MMAP; + if (video_type == webrtc::VideoType::kMJPEG) { + jpeg_decoder_ = V4L2DecodeConverter::Create( + V4L2_PIX_FMT_MJPEG, true, raw_width, raw_height, raw_stride); + if (jpeg_decoder_ == nullptr) { + RTC_LOG(LS_ERROR) << "Failed to MJPEG decoder"; + return WEBRTC_VIDEO_CODEC_ERROR; + } + } + if (memory == V4L2_MEMORY_DMABUF) { + scaler_ = + V4L2ScaleConverter::Create(V4L2_MEMORY_DMABUF, raw_width, raw_height, + raw_stride, true, width, height, width); + if (scaler_ == nullptr) { + RTC_LOG(LS_ERROR) << "Failed to create scaler"; + return WEBRTC_VIDEO_CODEC_ERROR; + } + } + h264_encoder_ = V4L2H264EncodeConverter::Create(memory, width, height, width); + if (h264_encoder_ == nullptr) { + return WEBRTC_VIDEO_CODEC_ERROR; + } + configured_type_ = type; + configured_width_ = width; + configured_height_ = height; + + return WEBRTC_VIDEO_CODEC_OK; +} + +int32_t V4L2H264Encoder::Release() { + jpeg_decoder_.reset(); + scaler_.reset(); + h264_encoder_.reset(); + return WEBRTC_VIDEO_CODEC_OK; +} + +int32_t V4L2H264Encoder::RegisterEncodeCompleteCallback( + webrtc::EncodedImageCallback* callback) { + std::lock_guard lock(callback_mutex_); + callback_ = callback; + return WEBRTC_VIDEO_CODEC_OK; +} + +void V4L2H264Encoder::SetRates(const RateControlParameters& parameters) { + if (h264_encoder_ == nullptr) + return; + if (parameters.bitrate.get_sum_bps() <= 0 || parameters.framerate_fps <= 0) + return; + + RTC_LOG(LS_INFO) << __FUNCTION__ + << " bitrate:" << parameters.bitrate.get_sum_bps() + << " fps:" << parameters.framerate_fps; + target_bitrate_bps_ = parameters.bitrate.get_sum_bps(); + bitrate_adjuster_.SetTargetBitrateBps(target_bitrate_bps_); + target_framerate_fps_ = parameters.framerate_fps; + return; +} + +void V4L2H264Encoder::SetBitrateBps(uint32_t bitrate_bps) { + if (h264_encoder_ == nullptr) + return; + if (bitrate_bps < 300000 || configured_bitrate_bps_ == bitrate_bps) { + return; + } + RTC_LOG(LS_INFO) << __FUNCTION__ << " bitrate: " << bitrate_bps + << " bit/sec"; + v4l2_control ctrl = {}; + ctrl.id = V4L2_CID_MPEG_VIDEO_BITRATE; + ctrl.value = bitrate_bps; + if (ioctl(h264_encoder_->fd(), VIDIOC_S_CTRL, &ctrl) < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to set bitrate"; + return; + } + configured_bitrate_bps_ = bitrate_bps; +} + +void V4L2H264Encoder::SetFramerateFps(double framerate_fps) { + if (h264_encoder_ == nullptr) + return; + if (configured_framerate_fps_ == framerate_fps) { + return; + } + RTC_LOG(LS_INFO) << __FUNCTION__ << " fps: " << framerate_fps; + v4l2_streamparm stream = {}; + stream.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; + stream.parm.output.timeperframe.numerator = 1; + stream.parm.output.timeperframe.denominator = framerate_fps; + if (ioctl(h264_encoder_->fd(), VIDIOC_S_PARM, &stream) < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to set framerate"; + return; + } + configured_framerate_fps_ = framerate_fps; +} + +webrtc::VideoEncoder::EncoderInfo V4L2H264Encoder::GetEncoderInfo() const { + EncoderInfo info; + info.supports_native_handle = true; + info.implementation_name = "V4L2 H264"; + info.scaling_settings = + VideoEncoder::ScalingSettings(kLowH264QpThreshold, kHighH264QpThreshold); + return info; +} + +int32_t V4L2H264Encoder::Encode( + const webrtc::VideoFrame& input_frame, + const std::vector* frame_types) { + { + std::lock_guard lock(callback_mutex_); + if (!callback_) { + RTC_LOG(LS_WARNING) + << "InitEncode() has been called, but a callback function " + << "has not been set with RegisterEncodeCompleteCallback()"; + return WEBRTC_VIDEO_CODEC_UNINITIALIZED; + } + } + + bool force_key_frame = false; + if (frame_types != nullptr) { + RTC_DCHECK_EQ(frame_types->size(), static_cast(1)); + if ((*frame_types)[0] == webrtc::VideoFrameType::kEmptyFrame) { + return WEBRTC_VIDEO_CODEC_OK; + } + force_key_frame = + (*frame_types)[0] == webrtc::VideoFrameType::kVideoFrameKey; + } + + rtc::scoped_refptr frame_buffer = + input_frame.video_frame_buffer(); + + RTC_LOG(LS_VERBOSE) << "V4L2H264Encoder::Encode: type=" + << (int)frame_buffer->type(); + if (frame_buffer->type() != configured_type_ || + frame_buffer->width() != configured_width_ || + frame_buffer->height() != configured_height_) { + RTC_LOG(LS_INFO) << "Encoder reinitialized from " << configured_width_ + << "x" << configured_height_ << " to " + << frame_buffer->width() << "x" << frame_buffer->height(); + Release(); + webrtc::VideoType video_type = webrtc::VideoType::kI420; + int stride = frame_buffer->width(); + int raw_width = frame_buffer->width(); + int raw_height = frame_buffer->height(); + if (frame_buffer->type() == webrtc::VideoFrameBuffer::Type::kNative) { + auto native_buffer = static_cast(frame_buffer.get()); + video_type = native_buffer->video_type(); + stride = native_buffer->stride(); + raw_width = native_buffer->raw_width(); + raw_height = native_buffer->raw_height(); + } + if (Configure(frame_buffer->type(), video_type, raw_width, raw_height, + stride, frame_buffer->width(), + frame_buffer->height()) != WEBRTC_VIDEO_CODEC_OK) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " Failed to Configure"; + return WEBRTC_VIDEO_CODEC_ERROR; + } + } + + SetBitrateBps(bitrate_adjuster_.GetAdjustedBitrateBps()); + SetFramerateFps(target_framerate_fps_); + + if (scaler_ == nullptr) { + h264_encoder_->Encode( + frame_buffer, input_frame.timestamp_us(), force_key_frame, + [this, input_frame](uint8_t* buffer, int size, int64_t timestamp_us, + bool is_key_frame) { + SendFrame(input_frame, buffer, size, timestamp_us, is_key_frame); + }); + } else { + if (jpeg_decoder_ == nullptr) { + scaler_->Scale( + frame_buffer, input_frame.timestamp_us(), + [this, force_key_frame, input_frame]( + rtc::scoped_refptr buffer, + int64_t timestamp_us) { + h264_encoder_->Encode( + buffer, timestamp_us, force_key_frame, + [this, input_frame](uint8_t* buffer, int size, + int64_t timestamp_us, bool is_key_frame) { + SendFrame(input_frame, buffer, size, timestamp_us, + is_key_frame); + }); + }); + } else { + auto native_buffer = static_cast(frame_buffer.get()); + jpeg_decoder_->Decode( + native_buffer->data().get(), native_buffer->size(), + input_frame.timestamp(), + [this, force_key_frame, input_frame]( + rtc::scoped_refptr buffer, + int64_t timestamp_rtp) { + RTC_LOG(LS_VERBOSE) << "Decoded JPEG frame: type=" << buffer->type() + << " width=" << buffer->width() + << " height=" << buffer->height(); + scaler_->Scale( + buffer, input_frame.timestamp_us(), + [this, force_key_frame, input_frame]( + rtc::scoped_refptr buffer, + int64_t timestamp_us) { + h264_encoder_->Encode( + buffer, timestamp_us, force_key_frame, + [this, input_frame](uint8_t* buffer, int size, + int64_t timestamp_us, + bool is_key_frame) { + SendFrame(input_frame, buffer, size, timestamp_us, + is_key_frame); + }); + }); + }); + } + } + + return WEBRTC_VIDEO_CODEC_OK; +} + +int32_t V4L2H264Encoder::SendFrame(const webrtc::VideoFrame& frame, + unsigned char* buffer, + size_t size, + int64_t timestamp_us, + bool is_key_frame) { + if (frame.timestamp_us() != timestamp_us) { + RTC_LOG(LS_ERROR) << __FUNCTION__ + << " Frame parameter is not found. SkipFrame" + << " timestamp_us: " << timestamp_us; + return WEBRTC_VIDEO_CODEC_ERROR; + } + + auto encoded_image_buffer = webrtc::EncodedImageBuffer::Create(buffer, size); + encoded_image_.SetEncodedData(encoded_image_buffer); + + encoded_image_._encodedWidth = frame.width(); + encoded_image_._encodedHeight = frame.height(); + encoded_image_.capture_time_ms_ = frame.render_time_ms(); + encoded_image_.ntp_time_ms_ = frame.ntp_time_ms(); + encoded_image_.SetTimestamp(frame.timestamp()); + encoded_image_.rotation_ = frame.rotation(); + encoded_image_.SetColorSpace(frame.color_space()); + encoded_image_._frameType = is_key_frame + ? webrtc::VideoFrameType::kVideoFrameKey + : webrtc::VideoFrameType::kVideoFrameDelta; + + h264_bitstream_parser_.ParseBitstream(encoded_image_); + encoded_image_.qp_ = h264_bitstream_parser_.GetLastSliceQp().value_or(-1); + RTC_LOG(LS_VERBOSE) << __FUNCTION__ << " qp:" << encoded_image_.qp_; + + webrtc::CodecSpecificInfo codec_specific; + codec_specific.codecType = webrtc::kVideoCodecH264; + codec_specific.codecSpecific.H264.packetization_mode = + webrtc::H264PacketizationMode::NonInterleaved; + + webrtc::EncodedImageCallback::Result result = + callback_->OnEncodedImage(encoded_image_, &codec_specific); + if (result.error != webrtc::EncodedImageCallback::Result::OK) { + RTC_LOG(LS_ERROR) << __FUNCTION__ << " OnEncodedImage failed" + << " error:" << result.error; + return WEBRTC_VIDEO_CODEC_ERROR; + } + bitrate_adjuster_.Update(size); + return WEBRTC_VIDEO_CODEC_OK; +} diff --git a/src/hwenc_v4l2/v4l2_h264_encoder.h b/src/hwenc_v4l2/v4l2_h264_encoder.h new file mode 100644 index 00000000..24b7501b --- /dev/null +++ b/src/hwenc_v4l2/v4l2_h264_encoder.h @@ -0,0 +1,76 @@ +#ifndef V4L2_H264_ENCODER_H_ +#define V4L2_H264_ENCODER_H_ + +#include +#include +#include +#include + +#include + +// WebRTC +#include +#include +#include +#include +#include +#include +#include + +#include "v4l2_converter.h" + +class V4L2H264Encoder : public webrtc::VideoEncoder { + public: + explicit V4L2H264Encoder(const cricket::VideoCodec& codec); + ~V4L2H264Encoder() override; + + int32_t InitEncode(const webrtc::VideoCodec* codec_settings, + const webrtc::VideoEncoder::Settings& settings) override; + int32_t RegisterEncodeCompleteCallback( + webrtc::EncodedImageCallback* callback) override; + int32_t Release() override; + void SetRates(const RateControlParameters& parameters) override; + webrtc::VideoEncoder::EncoderInfo GetEncoderInfo() const override; + int32_t Encode( + const webrtc::VideoFrame& frame, + const std::vector* frame_types) override; + + private: + int32_t Configure(webrtc::VideoFrameBuffer::Type type, + webrtc::VideoType video_type, + int32_t width, + int32_t height, + int32_t stride, + int32_t scaled_width, + int32_t scaled_height); + void SetBitrateBps(uint32_t bitrate_bps); + void SetFramerateFps(double framerate_fps); + int32_t SendFrame(const webrtc::VideoFrame& frame, + unsigned char* buffer, + size_t size, + int64_t timestamp_us, + bool is_key_frame); + + private: + std::shared_ptr jpeg_decoder_; + std::shared_ptr scaler_; + std::shared_ptr h264_encoder_; + + webrtc::VideoFrameBuffer::Type configured_type_; + int32_t configured_width_; + int32_t configured_height_; + + webrtc::EncodedImageCallback* callback_; + std::mutex callback_mutex_; + webrtc::BitrateAdjuster bitrate_adjuster_; + uint32_t target_bitrate_bps_; + uint32_t configured_bitrate_bps_; + double target_framerate_fps_; + int32_t configured_framerate_fps_; + + webrtc::H264BitstreamParser h264_bitstream_parser_; + + webrtc::EncodedImage encoded_image_; +}; + +#endif // V4L2_H264_ENCODER_H_ diff --git a/src/hwenc_v4l2/v4l2_native_buffer.cpp b/src/hwenc_v4l2/v4l2_native_buffer.cpp new file mode 100644 index 00000000..cec87856 --- /dev/null +++ b/src/hwenc_v4l2/v4l2_native_buffer.cpp @@ -0,0 +1,102 @@ +#include "v4l2_native_buffer.h" + +// WebRTC +#include + +V4L2NativeBuffer::V4L2NativeBuffer(webrtc::VideoType video_type, + int raw_width, + int raw_height, + int scaled_width, + int scaled_height, + int fd, + const uint8_t* data, + int size, + int stride, + std::function on_destruction) + : video_type_(video_type), + raw_width_(raw_width), + raw_height_(raw_height), + scaled_width_(scaled_width), + scaled_height_(scaled_height), + fd_(fd), + size_(size), + stride_(stride) { + if (data != nullptr) { + data_.reset(new uint8_t[size_]); + memcpy(data_.get(), data, size_); + } + if (on_destruction != nullptr) { + shared_on_destruction_.reset(new int(), + [on_destruction](int*) { on_destruction(); }); + } +} + +V4L2NativeBuffer::V4L2NativeBuffer(webrtc::VideoType video_type, + int raw_width, + int raw_height, + int scaled_width, + int scaled_height, + int fd, + const std::shared_ptr data, + int size, + int stride, + std::shared_ptr shared_on_destruction) + : video_type_(video_type), + raw_width_(raw_width), + raw_height_(raw_height), + scaled_width_(scaled_width), + scaled_height_(scaled_height), + fd_(fd), + data_(data), + size_(size), + stride_(stride), + shared_on_destruction_(shared_on_destruction) {} + +webrtc::VideoFrameBuffer::Type V4L2NativeBuffer::type() const { + return webrtc::VideoFrameBuffer::Type::kNative; +}; +int V4L2NativeBuffer::width() const { + return scaled_width_; +} +int V4L2NativeBuffer::height() const { + return scaled_height_; +} +rtc::scoped_refptr V4L2NativeBuffer::ToI420() { + RTC_LOG(LS_ERROR) << "V4L2NativeBuffer::ToI420() not implemented"; + return nullptr; +} + +// crop は無視してサイズだけ変更する +rtc::scoped_refptr V4L2NativeBuffer::CropAndScale( + int offset_x, + int offset_y, + int crop_width, + int crop_height, + int scaled_width, + int scaled_height) { + return rtc::make_ref_counted( + video_type_, raw_width_, raw_height_, scaled_width, scaled_height, fd_, + data_, size_, stride_, shared_on_destruction_); +} + +webrtc::VideoType V4L2NativeBuffer::video_type() const { + return video_type_; +} +int V4L2NativeBuffer::fd() const { + return fd_; +} +std::shared_ptr V4L2NativeBuffer::data() const { + return data_; +} +int V4L2NativeBuffer::size() const { + return size_; +} +int V4L2NativeBuffer::stride() const { + return stride_; +} +int V4L2NativeBuffer::raw_width() const { + return raw_width_; +} +int V4L2NativeBuffer::raw_height() const { + return raw_height_; +} diff --git a/src/hwenc_v4l2/v4l2_native_buffer.h b/src/hwenc_v4l2/v4l2_native_buffer.h new file mode 100644 index 00000000..35c3c628 --- /dev/null +++ b/src/hwenc_v4l2/v4l2_native_buffer.h @@ -0,0 +1,71 @@ +#ifndef V4L2_NATIVE_BUFFER_H_ +#define V4L2_NATIVE_BUFFER_H_ + +#include + +// WebRTC +#include +#include +#include +#include + +class V4L2NativeBuffer : public webrtc::VideoFrameBuffer { + public: + V4L2NativeBuffer(webrtc::VideoType video_type, + int raw_width, + int raw_height, + int scaled_width, + int scaled_height, + int fd, + const uint8_t* data, + int size, + int stride, + std::function on_destruction); + + V4L2NativeBuffer(webrtc::VideoType video_type, + int raw_width, + int raw_height, + int scaled_width, + int scaled_height, + int fd, + const std::shared_ptr data, + int size, + int stride, + std::shared_ptr shared_on_destruction); + + webrtc::VideoFrameBuffer::Type type() const override; + int width() const override; + int height() const override; + rtc::scoped_refptr ToI420() override; + + // crop は無視してサイズだけ変更する + rtc::scoped_refptr CropAndScale( + int offset_x, + int offset_y, + int crop_width, + int crop_height, + int scaled_width, + int scaled_height) override; + + webrtc::VideoType video_type() const; + int fd() const; + std::shared_ptr data() const; + int size() const; + int stride() const; + int raw_width() const; + int raw_height() const; + + private: + webrtc::VideoType video_type_; + int raw_width_; + int raw_height_; + int scaled_width_; + int scaled_height_; + int fd_; + std::shared_ptr data_; + int size_; + int stride_; + std::shared_ptr shared_on_destruction_; +}; + +#endif diff --git a/src/hwenc_v4l2/v4l2_runner.cpp b/src/hwenc_v4l2/v4l2_runner.cpp new file mode 100644 index 00000000..d003a66d --- /dev/null +++ b/src/hwenc_v4l2/v4l2_runner.cpp @@ -0,0 +1,147 @@ +#include "v4l2_runner.h" + +// Linux +#include +#include + +// WebRTC +#include +#include + +V4L2Runner::~V4L2Runner() { + abort_poll_ = true; + thread_.Finalize(); +} + +std::shared_ptr V4L2Runner::Create( + std::string name, + int fd, + int src_count, + int src_memory, + int dst_memory, + std::function on_change_resolution) { + auto p = std::make_shared(); + p->name_ = name; + p->fd_ = fd; + p->src_count_ = src_count; + p->src_memory_ = src_memory; + p->dst_memory_ = dst_memory; + + if (on_change_resolution) { + v4l2_event_subscription sub = {}; + sub.type = V4L2_EVENT_SOURCE_CHANGE; + if (ioctl(fd, VIDIOC_SUBSCRIBE_EVENT, &sub) < 0) { + RTC_LOG(LS_ERROR) << "Failed to subscribe to V4L2_EVENT_SOURCE_CHANGE"; + return nullptr; + } + p->on_change_resolution_ = on_change_resolution; + } + + p->abort_poll_ = false; + p->thread_ = rtc::PlatformThread::SpawnJoinable( + [p = p.get()]() { p->PollProcess(); }, "PollThread", + rtc::ThreadAttributes().SetPriority(rtc::ThreadPriority::kHigh)); + for (int i = 0; i < src_count; i++) { + p->output_buffers_available_.push(i); + } + return p; +} + +int V4L2Runner::Enqueue(v4l2_buffer* v4l2_buf, OnCompleteCallback on_complete) { + if (ioctl(fd_, VIDIOC_QBUF, v4l2_buf) < 0) { + RTC_LOG(LS_ERROR) << __FUNCTION__ + << " Failed to queue output buffer: error=" + << strerror(errno); + return WEBRTC_VIDEO_CODEC_ERROR; + } + on_completes_.push(on_complete); + + return WEBRTC_VIDEO_CODEC_OK; +} + +std::optional V4L2Runner::PopAvailableBufferIndex() { + return output_buffers_available_.pop(); +} + +void V4L2Runner::PollProcess() { + while (true) { + RTC_LOG(LS_VERBOSE) << "[POLL][" << name_ << "] Start poll"; + pollfd p = {fd_, POLLIN | POLLPRI, 0}; + int ret = poll(&p, 1, 500); + if (abort_poll_ && output_buffers_available_.size() == src_count_) { + break; + } + if (ret == -1) { + RTC_LOG(LS_ERROR) << "[POLL][" << name_ + << "] unexpected error ret=" << ret; + break; + } + if (p.revents & POLLPRI) { + RTC_LOG(LS_VERBOSE) << "[POLL][" << name_ << "] Polled POLLPRI"; + if (on_change_resolution_) { + v4l2_event event = {}; + if (ioctl(fd_, VIDIOC_DQEVENT, &event) < 0) { + RTC_LOG(LS_ERROR) + << "[POLL][" << name_ << "] Failed dequeing an event"; + break; + } + if (event.type == V4L2_EVENT_SOURCE_CHANGE && + (event.u.src_change.changes & V4L2_EVENT_SRC_CH_RESOLUTION) != 0) { + RTC_LOG(LS_VERBOSE) << "On change resolution"; + on_change_resolution_(); + } + } + } + if (p.revents & POLLIN) { + RTC_LOG(LS_VERBOSE) << "[POLL][" << name_ << "] Polled POLLIN"; + v4l2_buffer v4l2_buf = {}; + v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; + v4l2_buf.memory = src_memory_; + v4l2_buf.length = 1; + v4l2_plane planes[VIDEO_MAX_PLANES] = {}; + v4l2_buf.m.planes = planes; + RTC_LOG(LS_VERBOSE) << "[POLL][" << name_ << "] DQBUF output"; + int ret = ioctl(fd_, VIDIOC_DQBUF, &v4l2_buf); + if (ret != 0) { + RTC_LOG(LS_ERROR) << "[POLL][" << name_ + << "] Failed to dequeue output buffer: error=" + << strerror(errno); + } else { + output_buffers_available_.push(v4l2_buf.index); + } + + v4l2_buf = {}; + memset(planes, 0, sizeof(planes)); + v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; + v4l2_buf.memory = V4L2_MEMORY_MMAP; + v4l2_buf.length = 1; + v4l2_buf.m.planes = planes; + RTC_LOG(LS_VERBOSE) << "[POLL][" << name_ << "] DQBUF capture"; + ret = ioctl(fd_, VIDIOC_DQBUF, &v4l2_buf); + if (ret != 0) { + RTC_LOG(LS_ERROR) << "[POLL][" << name_ + << "] Failed to dequeue capture buffer: error=" + << strerror(errno); + } else { + if (abort_poll_) { + break; + } + std::optional on_complete = on_completes_.pop(); + if (!on_complete) { + RTC_LOG(LS_ERROR) + << "[POLL][" << name_ << "] on_completes_ is empty."; + } else { + (*on_complete)(&v4l2_buf, [fd = fd_, v4l2_buf]() mutable { + v4l2_plane planes[VIDEO_MAX_PLANES] = {}; + v4l2_buf.m.planes = planes; + if (ioctl(fd, VIDIOC_QBUF, &v4l2_buf) < 0) { + RTC_LOG(LS_ERROR) << "Failed to enqueue capture buffer: error=" + << strerror(errno); + } + }); + } + } + RTC_LOG(LS_VERBOSE) << "[POLL][" << name_ << "] Completed POLLIN"; + } + } +} \ No newline at end of file diff --git a/src/hwenc_v4l2/v4l2_runner.h b/src/hwenc_v4l2/v4l2_runner.h new file mode 100644 index 00000000..1a077e1e --- /dev/null +++ b/src/hwenc_v4l2/v4l2_runner.h @@ -0,0 +1,87 @@ +#ifndef V4L2_RUNNER_H_ +#define V4L2_RUNNER_H_ + +#include +#include +#include +#include +#include +#include + +// Linux +#include + +// WebRTC +#include + +template +class ConcurrentQueue { + public: + void push(T t) { + std::lock_guard lock(mutex_); + queue_.push(t); + } + std::optional pop() { + std::lock_guard lock(mutex_); + if (queue_.empty()) { + return std::nullopt; + } + T t = queue_.front(); + queue_.pop(); + return t; + } + bool empty() { + std::lock_guard lock(mutex_); + return queue_.empty(); + } + size_t size() { + std::lock_guard lock(mutex_); + return queue_.size(); + } + void clear() { + std::lock_guard lock(mutex_); + queue_ = std::queue(); + } + + private: + std::queue queue_; + std::mutex mutex_; +}; + +class V4L2Runner { + public: + ~V4L2Runner(); + + static std::shared_ptr Create( + std::string name, + int fd, + int src_count, + int src_memory, + int dst_memory, + std::function on_change_resolution = nullptr); + + typedef std::function)> + OnCompleteCallback; + + int Enqueue(v4l2_buffer* v4l2_buf, OnCompleteCallback on_complete); + + std::optional PopAvailableBufferIndex(); + + private: + void PollProcess(); + + private: + std::string name_; + int fd_; + int src_count_; + int src_memory_; + int dst_memory_; + std::function on_change_resolution_; + + ConcurrentQueue output_buffers_available_; + ConcurrentQueue on_completes_; + std::atomic abort_poll_; + rtc::PlatformThread thread_; +}; + +#endif diff --git a/src/libcamerac/libcamerac.cpp b/src/libcamerac/libcamerac.cpp new file mode 100644 index 00000000..7f40beb1 --- /dev/null +++ b/src/libcamerac/libcamerac.cpp @@ -0,0 +1,380 @@ +#include "libcamerac.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +extern "C" { + +// libcamerac_CameraManager + +libcamerac_CameraManager* libcamerac_CameraManager_new() { + return (libcamerac_CameraManager*)new libcamera::CameraManager(); +} +void libcamerac_CameraManager_destroy(libcamerac_CameraManager* p) { + delete (libcamera::CameraManager*)p; +} +int libcamerac_CameraManager_start(libcamerac_CameraManager* p) { + return ((libcamera::CameraManager*)p)->start(); +} +void libcamerac_CameraManager_stop(libcamerac_CameraManager* p) { + ((libcamera::CameraManager*)p)->stop(); +} +libcamerac_vector_Camera* libcamerac_CameraManager_cameras( + libcamerac_CameraManager* p) { + return (libcamerac_vector_Camera*)new std::vector< + std::shared_ptr>( + ((libcamera::CameraManager*)p)->cameras()); +} +libcamerac_Camera* libcamerac_CameraManager_get(libcamerac_CameraManager* p, + const char* id) { + return (libcamerac_Camera*)new std::shared_ptr( + ((libcamera::CameraManager*)p)->get(id)); +} + +// libcamerac_vector_Camera + +void libcamerac_vector_Camera_destroy(libcamerac_vector_Camera* p) { + delete (std::vector>*)p; +} +int libcamerac_vector_Camera_size(libcamerac_vector_Camera* p) { + return ((std::vector>*)p)->size(); +} +libcamerac_Camera* libcamerac_vector_Camera_at(libcamerac_vector_Camera* p, + int i) { + return (libcamerac_Camera*)&( + (std::vector>*)p) + ->at(i); +} + +// libcamerac_vector_StreamRole + +libcamerac_vector_StreamRole* libcamerac_vector_StreamRole_new() { + return ( + libcamerac_vector_StreamRole*)new std::vector(); +} +void libcamerac_vector_StreamRole_destroy(libcamerac_vector_StreamRole* p) { + delete (std::vector*)p; +} +void libcamerac_vector_StreamRole_push_back(libcamerac_vector_StreamRole* p, + libcamerac_StreamRole v) { + ((std::vector*)p)->push_back((libcamera::StreamRole)v); +} +int libcamerac_vector_StreamRole_size(libcamerac_vector_StreamRole* p) { + return ((std::vector*)p)->size(); +} +void libcamerac_vector_StreamRole_clear(libcamerac_vector_StreamRole* p) { + ((std::vector*)p)->clear(); +} + +// libcamerac_Camera + +libcamerac_Camera* libcamerac_Camera_own(libcamerac_Camera* p) { + return (libcamerac_Camera*)new std::shared_ptr( + *(std::shared_ptr*)p); +} +void libcamerac_Camera_disown(libcamerac_Camera* p) { + delete (std::shared_ptr*)p; +} +const char* libcamerac_Camera_id(libcamerac_Camera* p) { + return (*(std::shared_ptr*)p)->id().c_str(); +} +libcamerac_CameraConfiguration* libcamerac_Camera_generateConfiguration( + libcamerac_Camera* p, + libcamerac_vector_StreamRole* roles) { + return (libcamerac_CameraConfiguration*)(*(std::shared_ptr< + libcamera::Camera>*)p) + ->generateConfiguration(*(std::vector*)roles) + .release(); +} +int libcamerac_Camera_configure(libcamerac_Camera* p, + libcamerac_CameraConfiguration* config) { + return (*(std::shared_ptr*)p) + ->configure((libcamera::CameraConfiguration*)config); +} +libcamerac_Request* libcamerac_Camera_createRequest(libcamerac_Camera* p) { + return (libcamerac_Request*)(*(std::shared_ptr*)p) + ->createRequest() + .release(); +} +int libcamerac_Camera_queueRequest(libcamerac_Camera* p, + libcamerac_Request* request) { + return (*(std::shared_ptr*)p) + ->queueRequest((libcamera::Request*)request); +} +int libcamerac_Camera_start(libcamerac_Camera* p, + const libcamerac_ControlList* control_list) { + return (*(std::shared_ptr*)p) + ->start((const libcamera::ControlList*)control_list); +} +int libcamerac_Camera_stop(libcamerac_Camera* p) { + return (*(std::shared_ptr*)p)->stop(); +} +int libcamerac_Camera_acquire(libcamerac_Camera* p) { + return (*(std::shared_ptr*)p)->acquire(); +} +int libcamerac_Camera_release(libcamerac_Camera* p) { + return (*(std::shared_ptr*)p)->release(); +} +libcamerac_Signal_Request* libcamerac_Camera_requestCompleted( + libcamerac_Camera* p) { + return (libcamerac_Signal_Request*)&(*(std::shared_ptr*)p) + ->requestCompleted; +} + +// libcamerac_CameraConfiguration + +void libcamerac_CameraConfiguration_destroy(libcamerac_CameraConfiguration* p) { + delete (libcamera::CameraConfiguration*)p; +} +libcamerac_StreamConfiguration* libcamerac_CameraConfiguration_at( + libcamerac_CameraConfiguration* p, + int i) { + return (libcamerac_StreamConfiguration*)&((libcamera::CameraConfiguration*)p) + ->at(i); +} +libcamerac_CameraConfiguration_Status libcamerac_CameraConfiguration_validate( + libcamerac_CameraConfiguration* p) { + return (libcamerac_CameraConfiguration_Status)((libcamera:: + CameraConfiguration*)p) + ->validate(); +} + +// libcamerac_StreamConfiguration + +libcamerac_Stream* libcamerac_StreamConfiguration_stream( + libcamerac_StreamConfiguration* p) { + return (libcamerac_Stream*)((libcamera::StreamConfiguration*)p)->stream(); +} +int libcamerac_StreamConfiguration_get_size_width( + libcamerac_StreamConfiguration* p) { + return ((libcamera::StreamConfiguration*)p)->size.width; +} +int libcamerac_StreamConfiguration_get_size_height( + libcamerac_StreamConfiguration* p) { + return ((libcamera::StreamConfiguration*)p)->size.height; +} +int libcamerac_StreamConfiguration_get_stride( + libcamerac_StreamConfiguration* p) { + return ((libcamera::StreamConfiguration*)p)->stride; +} +void libcamerac_StreamConfiguration_set_pixelFormat( + libcamerac_StreamConfiguration* p, + libcamerac_PixelFormat* pixelFormat) { + ((libcamera::StreamConfiguration*)p)->pixelFormat = + *(libcamera::PixelFormat*)pixelFormat; +} +void libcamerac_StreamConfiguration_set_size_width( + libcamerac_StreamConfiguration* p, + int width) { + ((libcamera::StreamConfiguration*)p)->size.width = width; +} +void libcamerac_StreamConfiguration_set_size_height( + libcamerac_StreamConfiguration* p, + int height) { + ((libcamera::StreamConfiguration*)p)->size.height = height; +} +void libcamerac_StreamConfiguration_set_stride( + libcamerac_StreamConfiguration* p, + int stride) { + ((libcamera::StreamConfiguration*)p)->stride = stride; +} +void libcamerac_StreamConfiguration_set_frameSize( + libcamerac_StreamConfiguration* p, + int frameSize) { + ((libcamera::StreamConfiguration*)p)->frameSize = frameSize; +} +void libcamerac_StreamConfiguration_set_bufferCount( + libcamerac_StreamConfiguration* p, + int bufferCount) { + ((libcamera::StreamConfiguration*)p)->bufferCount = bufferCount; +} +void libcamerac_StreamConfiguration_set_colorSpace( + libcamerac_StreamConfiguration* p, + libcamerac_ColorSpace* colorSpace) { + ((libcamera::StreamConfiguration*)p)->colorSpace = + *(libcamera::ColorSpace*)colorSpace; +} + +// libcamerac_FrameBufferAllocator + +libcamerac_FrameBufferAllocator* libcamerac_FrameBufferAllocator_new( + libcamerac_Camera* camera) { + return (libcamerac_FrameBufferAllocator*)new libcamera::FrameBufferAllocator( + *(std::shared_ptr*)camera); +} +void libcamerac_FrameBufferAllocator_destroy( + libcamerac_FrameBufferAllocator* p) { + delete (libcamera::FrameBufferAllocator*)p; +} +int libcamerac_FrameBufferAllocator_allocate(libcamerac_FrameBufferAllocator* p, + libcamerac_Stream* stream) { + return ((libcamera::FrameBufferAllocator*)p) + ->allocate((libcamera::Stream*)stream); +} +int libcamerac_FrameBufferAllocator_free(libcamerac_FrameBufferAllocator* p, + libcamerac_Stream* stream) { + return ((libcamera::FrameBufferAllocator*)p) + ->free((libcamera::Stream*)stream); +} +libcamerac_FrameBuffer* libcamerac_FrameBufferAllocator_buffers_at( + const libcamerac_FrameBufferAllocator* p, + libcamerac_Stream* stream, + int i) { + return (libcamerac_FrameBuffer*)((const libcamera::FrameBufferAllocator*)p) + ->buffers((libcamera::Stream*)stream)[i] + .get(); +} +int libcamerac_FrameBufferAllocator_buffers_size( + const libcamerac_FrameBufferAllocator* p, + libcamerac_Stream* stream) { + return ((libcamera::FrameBufferAllocator*)p) + ->buffers((libcamera::Stream*)stream) + .size(); +} + +// libcamerac_FrameBuffer + +int libcamerac_FrameBuffer_planes_size(const libcamerac_FrameBuffer* p) { + return ((const libcamera::FrameBuffer*)p)->planes().size(); +} +const libcamerac_FrameBuffer_Plane* libcamerac_FrameBuffer_planes_at( + const libcamerac_FrameBuffer* p, + int i) { + return (const libcamerac_FrameBuffer_Plane*)&( + (const libcamera::FrameBuffer*)p) + ->planes() + .at(i); +} + +// libcamerac_FrameBuffer_Plane + +int libcamerac_FrameBuffer_Plane_fd(const libcamerac_FrameBuffer_Plane* p) { + return ((const libcamera::FrameBuffer::Plane*)p)->fd.get(); +} +int libcamerac_FrameBuffer_Plane_length(const libcamerac_FrameBuffer_Plane* p) { + return ((const libcamera::FrameBuffer::Plane*)p)->length; +} + +// libcamerac_ControlList + +libcamerac_ControlList* libcamerac_ControlList_controls() { + return (libcamerac_ControlList*)new libcamera::ControlList( + libcamera::controls::controls); +} +void libcamerac_ControlList_destroy(libcamerac_ControlList* p) { + delete (libcamera::ControlList*)p; +} +void libcamerac_ControlList_clear(libcamerac_ControlList* p) { + ((libcamera::ControlList*)p)->clear(); +} +void libcamerac_ControlList_copy(const libcamerac_ControlList* p, + libcamerac_ControlList* control_list) { + *((libcamera::ControlList*)control_list) = *(const libcamera::ControlList*)p; +} + +// libcamerac_Request + +void libcamerac_Request_destroy(libcamerac_Request* p) { + delete (libcamera::Request*)p; +} +libcamerac_Request_Status libcamerac_Request_status( + const libcamerac_Request* p) { + return (libcamerac_Request_Status)((const libcamera::Request*)p)->status(); +} +libcamerac_FrameBuffer* libcamerac_Request_findBuffer( + const libcamerac_Request* p, + const libcamerac_Stream* stream) { + return (libcamerac_FrameBuffer*)((const libcamera::Request*)p) + ->findBuffer((const libcamera::Stream*)stream); +} +int libcamerac_Request_addBuffer(libcamerac_Request* p, + const libcamerac_Stream* stream, + libcamerac_FrameBuffer* buffer) { + return ((libcamera::Request*)p) + ->addBuffer((const libcamera::Stream*)stream, + (libcamera::FrameBuffer*)buffer); +} +void libcamerac_Request_reuse(libcamerac_Request* p) { + ((libcamera::Request*)p)->reuse(); +} +const libcamerac_Request_BufferMap* libcamerac_Request_buffers( + const libcamerac_Request* p) { + return (const libcamerac_Request_BufferMap*)&((const libcamera::Request*)p) + ->buffers(); +} +libcamerac_ControlList* libcamerac_Request_controls(libcamerac_Request* p) { + return (libcamerac_ControlList*)&((libcamera::Request*)p)->controls(); +} + +// libcamerac_Request_BufferMap + +void libcamerac_Request_BufferMap_foreach( + const libcamerac_Request_BufferMap* p, + void (*callback)(const libcamerac_Stream* stream, + libcamerac_FrameBuffer* buffer, + void* user_data), + void* user_data) { + for (const auto& v : *(const libcamera::Request::BufferMap*)p) { + callback((const libcamerac_Stream*)v.first, + (libcamerac_FrameBuffer*)v.second, user_data); + } +} + +// libcamerac_PixelFormat + +libcamerac_PixelFormat* libcamerac_PixelFormat_YUV420() { + return (libcamerac_PixelFormat*)&libcamera::formats::YUV420; +} + +// libcamerac_ColorSpace + +//extern libcamerac_ColorSpace* libcamerac_ColorSpace_Jpeg() { +// return (libcamerac_ColorSpace*)&libcamera::ColorSpace::Jpeg; +//} + +// libcamerac_Signal_Request + +struct Signal_Request { + void (*callback)(libcamerac_Request* request, void* user_data); + void* user_data; + void Run(libcamera::Request* request) { + callback((libcamerac_Request*)request, user_data); + } +}; +std::vector> g_signal_requests; + +void libcamerac_Signal_Request_connect( + libcamerac_Signal_Request* p, + void (*callback)(libcamerac_Request* request, void* user_data), + void* user_data) { + auto signal = std::make_shared(); + signal->callback = callback; + signal->user_data = user_data; + g_signal_requests.push_back(signal); + ((libcamera::Signal*)p) + ->connect(signal.get(), &Signal_Request::Run); +} +void libcamerac_Signal_Request_disconnect( + libcamerac_Signal_Request* p, + void (*callback)(libcamerac_Request* request, void* user_data), + void* user_data) { + auto it = std::find_if(g_signal_requests.begin(), g_signal_requests.end(), + [=](const std::shared_ptr& signal) { + return signal->callback == callback && + signal->user_data == user_data; + }); + if (it != g_signal_requests.end()) { + ((libcamera::Signal*)p) + ->disconnect(it->get(), &Signal_Request::Run); + g_signal_requests.erase(it); + }; +} +} diff --git a/src/libcamerac/libcamerac.h b/src/libcamerac/libcamerac.h new file mode 100644 index 00000000..f5c66b8d --- /dev/null +++ b/src/libcamerac/libcamerac.h @@ -0,0 +1,264 @@ +#ifndef LIBCAMERAC_H_ +#define LIBCAMERAC_H_ + +// libcamera を C API から使うためのヘッダ + +#ifdef __cplusplus +extern "C" { +#endif + +struct libcamerac_CameraManager_t; +typedef struct libcamerac_CameraManager_t libcamerac_CameraManager; + +struct libcamerac_Camera_t; +typedef struct libcamerac_Camera_t libcamerac_Camera; + +struct libcamerac_vector_Camera_t; +typedef struct libcamerac_vector_Camera_t libcamerac_vector_Camera; + +struct libcamerac_CameraConfiguration_t; +typedef struct libcamerac_CameraConfiguration_t libcamerac_CameraConfiguration; + +struct libcamerac_StreamConfiguration_t; +typedef struct libcamerac_StreamConfiguration_t libcamerac_StreamConfiguration; + +struct libcamerac_Stream_t; +typedef struct libcamerac_Stream_t libcamerac_Stream; + +struct libcamerac_FrameBufferAllocator_t; +typedef struct libcamerac_FrameBufferAllocator_t + libcamerac_FrameBufferAllocator; + +struct libcamerac_FrameBuffer_t; +typedef struct libcamerac_FrameBuffer_t libcamerac_FrameBuffer; + +struct libcamerac_FrameBuffer_Plane_t; +typedef struct libcamerac_FrameBuffer_Plane_t libcamerac_FrameBuffer_Plane; + +struct libcamerac_Request_t; +typedef struct libcamerac_Request_t libcamerac_Request; + +typedef enum { + libcamerac_Request_Status_RequestPending, + libcamerac_Request_Status_RequestComplete, + libcamerac_Request_Status_RequestCancelled, +} libcamerac_Request_Status; + +struct libcamerac_Request_BufferMap_t; +typedef struct libcamerac_Request_BufferMap_t libcamerac_Request_BufferMap; + +typedef enum { + libcamerac_StreamRole_Raw, + libcamerac_StreamRole_StillCapture, + libcamerac_StreamRole_VideoRecording, + libcamerac_StreamRole_Viewfinder, +} libcamerac_StreamRole; + +struct libcamerac_vector_StreamRole_t; +typedef struct libcamerac_vector_StreamRole_t libcamerac_vector_StreamRole; + +typedef enum { + libcamerac_CameraConfiguration_Status_Valid, + libcamerac_CameraConfiguration_Status_Adjusted, + libcamerac_CameraConfiguration_Status_Invalid, +} libcamerac_CameraConfiguration_Status; + +struct libcamerac_ControlList_t; +typedef struct libcamerac_ControlList_t libcamerac_ControlList; + +struct libcamerac_PixelFormat_t; +typedef struct libcamerac_PixelFormat_t libcamerac_PixelFormat; + +struct libcamerac_ColorSpace_t; +typedef struct libcamerac_ColorSpace_t libcamerac_ColorSpace; + +struct libcamerac_Signal_Request_t; +typedef struct libcamerac_Signal_Request_t libcamerac_Signal_Request; + +// libcamerac_CameraManager + +extern libcamerac_CameraManager* libcamerac_CameraManager_new(); +extern void libcamerac_CameraManager_destroy(libcamerac_CameraManager* p); +extern int libcamerac_CameraManager_start(libcamerac_CameraManager* p); +extern void libcamerac_CameraManager_stop(libcamerac_CameraManager* p); +extern libcamerac_vector_Camera* libcamerac_CameraManager_cameras( + libcamerac_CameraManager* p); +extern libcamerac_Camera* libcamerac_CameraManager_get( + libcamerac_CameraManager* p, + const char* id); + +// libcamerac_vector_Camera + +extern void libcamerac_vector_Camera_destroy(libcamerac_vector_Camera* p); +extern int libcamerac_vector_Camera_size(libcamerac_vector_Camera* p); +extern libcamerac_Camera* libcamerac_vector_Camera_at( + libcamerac_vector_Camera* p, + int i); + +// libcamerac_vector_StreamRole + +extern libcamerac_vector_StreamRole* libcamerac_vector_StreamRole_new(); +extern void libcamerac_vector_StreamRole_destroy( + libcamerac_vector_StreamRole* p); +extern void libcamerac_vector_StreamRole_push_back( + libcamerac_vector_StreamRole* p, + libcamerac_StreamRole v); +extern int libcamerac_vector_StreamRole_size(libcamerac_vector_StreamRole* p); +extern void libcamerac_vector_StreamRole_clear(libcamerac_vector_StreamRole* p); + +// libcamerac_Camera + +extern libcamerac_Camera* libcamerac_Camera_own(libcamerac_Camera* p); +extern void libcamerac_Camera_disown(libcamerac_Camera* p); +extern const char* libcamerac_Camera_id(libcamerac_Camera* p); +extern libcamerac_CameraConfiguration* libcamerac_Camera_generateConfiguration( + libcamerac_Camera* p, + libcamerac_vector_StreamRole* roles); +extern int libcamerac_Camera_configure(libcamerac_Camera* p, + libcamerac_CameraConfiguration* config); +extern libcamerac_Request* libcamerac_Camera_createRequest( + libcamerac_Camera* p); +extern int libcamerac_Camera_queueRequest(libcamerac_Camera* p, + libcamerac_Request* request); +extern int libcamerac_Camera_start(libcamerac_Camera* p, + const libcamerac_ControlList* control_list); +extern int libcamerac_Camera_stop(libcamerac_Camera* p); +extern int libcamerac_Camera_acquire(libcamerac_Camera* p); +extern int libcamerac_Camera_release(libcamerac_Camera* p); +extern libcamerac_Signal_Request* libcamerac_Camera_requestCompleted( + libcamerac_Camera* p); + +// libcamerac_CameraConfiguration + +extern void libcamerac_CameraConfiguration_destroy( + libcamerac_CameraConfiguration* p); +extern libcamerac_StreamConfiguration* libcamerac_CameraConfiguration_at( + libcamerac_CameraConfiguration* p, + int i); +extern libcamerac_CameraConfiguration_Status +libcamerac_CameraConfiguration_validate(libcamerac_CameraConfiguration* p); + +// libcamerac_StreamConfiguration + +extern libcamerac_Stream* libcamerac_StreamConfiguration_stream( + libcamerac_StreamConfiguration* p); +extern int libcamerac_StreamConfiguration_get_size_width( + libcamerac_StreamConfiguration* p); +extern int libcamerac_StreamConfiguration_get_size_height( + libcamerac_StreamConfiguration* p); +int libcamerac_StreamConfiguration_get_stride( + libcamerac_StreamConfiguration* p); +extern void libcamerac_StreamConfiguration_set_pixelFormat( + libcamerac_StreamConfiguration* p, + libcamerac_PixelFormat* pixelFormat); +extern void libcamerac_StreamConfiguration_set_size_width( + libcamerac_StreamConfiguration* p, + int width); +extern void libcamerac_StreamConfiguration_set_size_height( + libcamerac_StreamConfiguration* p, + int height); +extern void libcamerac_StreamConfiguration_set_stride( + libcamerac_StreamConfiguration* p, + int stride); +extern void libcamerac_StreamConfiguration_set_frameSize( + libcamerac_StreamConfiguration* p, + int frameSize); +extern void libcamerac_StreamConfiguration_set_bufferCount( + libcamerac_StreamConfiguration* p, + int bufferCount); +extern void libcamerac_StreamConfiguration_set_colorSpace( + libcamerac_StreamConfiguration* p, + libcamerac_ColorSpace* colorSpace); + +// libcamerac_FrameBufferAllocator + +extern libcamerac_FrameBufferAllocator* libcamerac_FrameBufferAllocator_new( + libcamerac_Camera* camera); +extern void libcamerac_FrameBufferAllocator_destroy( + libcamerac_FrameBufferAllocator* p); +extern int libcamerac_FrameBufferAllocator_allocate( + libcamerac_FrameBufferAllocator* p, + libcamerac_Stream* stream); +extern int libcamerac_FrameBufferAllocator_free( + libcamerac_FrameBufferAllocator* p, + libcamerac_Stream* stream); +extern libcamerac_FrameBuffer* libcamerac_FrameBufferAllocator_buffers_at( + const libcamerac_FrameBufferAllocator* p, + libcamerac_Stream* stream, + int i); +extern int libcamerac_FrameBufferAllocator_buffers_size( + const libcamerac_FrameBufferAllocator* p, + libcamerac_Stream* stream); + +// libcamerac_FrameBuffer + +extern int libcamerac_FrameBuffer_planes_size(const libcamerac_FrameBuffer* p); +extern const libcamerac_FrameBuffer_Plane* libcamerac_FrameBuffer_planes_at( + const libcamerac_FrameBuffer* p, + int i); + +// libcamerac_FrameBuffer_Plane + +extern int libcamerac_FrameBuffer_Plane_fd( + const libcamerac_FrameBuffer_Plane* p); +extern int libcamerac_FrameBuffer_Plane_length( + const libcamerac_FrameBuffer_Plane* p); + +// libcamerac_ControlList + +extern libcamerac_ControlList* libcamerac_ControlList_controls(); +extern void libcamerac_ControlList_destroy(libcamerac_ControlList* p); +extern void libcamerac_ControlList_clear(libcamerac_ControlList* p); +extern void libcamerac_ControlList_copy(const libcamerac_ControlList* p, + libcamerac_ControlList* control_list); + +// libcamerac_Request + +extern void libcamerac_Request_destroy(libcamerac_Request* p); +extern libcamerac_Request_Status libcamerac_Request_status( + const libcamerac_Request* p); +extern libcamerac_FrameBuffer* libcamerac_Request_findBuffer( + const libcamerac_Request* p, + const libcamerac_Stream* stream); +extern int libcamerac_Request_addBuffer(libcamerac_Request* p, + const libcamerac_Stream* stream, + libcamerac_FrameBuffer* buffer); +extern void libcamerac_Request_reuse(libcamerac_Request* p); +extern const libcamerac_Request_BufferMap* libcamerac_Request_buffers( + const libcamerac_Request* p); +extern libcamerac_ControlList* libcamerac_Request_controls( + libcamerac_Request* p); + +// libcamerac_Request_BufferMap + +extern void libcamerac_Request_BufferMap_foreach( + const libcamerac_Request_BufferMap* p, + void (*callback)(const libcamerac_Stream* stream, + libcamerac_FrameBuffer* buffer, + void* user_data), + void* user_data); + +// libcamerac_PixelFormat + +extern libcamerac_PixelFormat* libcamerac_PixelFormat_YUV420(); + +// libcamerac_ColorSpace + +//extern libcamerac_ColorSpace* libcamerac_ColorSpace_Jpeg(); + +// libcamerac_Signal_Request + +extern void libcamerac_Signal_Request_connect( + libcamerac_Signal_Request* p, + void (*callback)(libcamerac_Request* request, void* user_data), + void* user_data); +extern void libcamerac_Signal_Request_disconnect( + libcamerac_Signal_Request* p, + void (*callback)(libcamerac_Request* request, void* user_data), + void* user_data); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/libcamerac/libcameracpp.h b/src/libcamerac/libcameracpp.h new file mode 100644 index 00000000..2ad44c02 --- /dev/null +++ b/src/libcamerac/libcameracpp.h @@ -0,0 +1,65 @@ +#ifndef LIBCAMERACPP_H_ +#define LIBCAMERACPP_H_ + +#include + +#include "libcamerac.h" + +static std::shared_ptr +libcameracpp_CameraManager_new() { + return std::shared_ptr( + libcamerac_CameraManager_new(), libcamerac_CameraManager_destroy); +} + +static std::shared_ptr +libcameracpp_CameraManager_cameras(libcamerac_CameraManager* p) { + return std::shared_ptr( + libcamerac_CameraManager_cameras(p), libcamerac_vector_Camera_destroy); +} + +static std::shared_ptr libcameracpp_CameraManager_get( + libcamerac_CameraManager* p, + const char* id) { + return std::shared_ptr(libcamerac_CameraManager_get(p, id), + libcamerac_Camera_disown); +} + +static std::shared_ptr +libcameracpp_vector_StreamRole_new() { + return std::shared_ptr( + libcamerac_vector_StreamRole_new(), libcamerac_vector_StreamRole_destroy); +} + +static std::shared_ptr libcameracpp_Camera_own( + libcamerac_Camera* p) { + return std::shared_ptr(libcamerac_Camera_own(p), + libcamerac_Camera_disown); +} + +static std::shared_ptr +libcameracpp_Camera_generateConfiguration(libcamerac_Camera* p, + libcamerac_vector_StreamRole* roles) { + return std::shared_ptr( + libcamerac_Camera_generateConfiguration(p, roles), + libcamerac_CameraConfiguration_destroy); +} + +static std::shared_ptr libcameracpp_Camera_createRequest( + libcamerac_Camera* p) { + return std::shared_ptr(libcamerac_Camera_createRequest(p), + libcamerac_Request_destroy); +} + +static std::shared_ptr +libcameracpp_FrameBufferAllocator_new(libcamerac_Camera* camera) { + return std::shared_ptr( + libcamerac_FrameBufferAllocator_new(camera), + libcamerac_FrameBufferAllocator_destroy); +} + +static std::shared_ptr +libcameracpp_ControlList_controls() { + return std::shared_ptr( + libcamerac_ControlList_controls(), libcamerac_ControlList_destroy); +} +#endif \ No newline at end of file diff --git a/src/mac_helper/mac_capturer.mm b/src/mac_helper/mac_capturer.mm index 24496f0e..5564416d 100644 --- a/src/mac_helper/mac_capturer.mm +++ b/src/mac_helper/mac_capturer.mm @@ -67,7 +67,8 @@ - (void)capturer:(RTCVideoCapturer*)capturer MacCapturer::MacCapturer(size_t width, size_t height, size_t target_fps, - AVCaptureDevice* device) { + AVCaptureDevice* device) + : ScalableVideoTrackSource(ScalableVideoTrackSourceConfig()) { RTC_LOG(LS_INFO) << "MacCapturer width=" << width << ", height=" << height << ", target_fps=" << target_fps; diff --git a/src/main.cpp b/src/main.cpp index ae07e3c6..1424710e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -23,6 +23,9 @@ #include "hwenc_jetson/jetson_v4l2_capturer.h" #elif USE_NVCODEC_ENCODER #include "hwenc_nvcodec/nvcodec_v4l2_capturer.h" +#elif USE_V4L2_ENCODER +#include "hwenc_v4l2/libcamera_capturer.h" +#include "hwenc_v4l2/v4l2_capturer.h" #endif #include "v4l2_video_capturer/v4l2_video_capturer.h" #else @@ -159,6 +162,18 @@ int main(int argc, char* argv[]) { } else { return V4L2VideoCapturer::Create(std::move(v4l2_config)); } +#elif USE_V4L2_ENCODER + if (args.use_libcamera) { + LibcameraCapturerConfig libcamera_config = v4l2_config; + // use_libcamera_native == true でも、サイマルキャストの場合はネイティブフレームを出力しない + libcamera_config.native_frame_output = + args.use_libcamera_native && !(use_sora && args.sora_simulcast); + return LibcameraCapturer::Create(libcamera_config); + } else if (v4l2_config.use_native && !(use_sora && args.sora_simulcast)) { + return V4L2Capturer::Create(std::move(v4l2_config)); + } else { + return V4L2VideoCapturer::Create(std::move(v4l2_config)); + } #else return V4L2VideoCapturer::Create(std::move(v4l2_config)); #endif @@ -180,7 +195,6 @@ int main(int argc, char* argv[]) { rtcm_config.no_audio_device = args.no_audio_device; rtcm_config.fixed_resolution = args.fixed_resolution; - rtcm_config.show_me = args.show_me; rtcm_config.simulcast = args.sora_simulcast; rtcm_config.hardware_encoder_only = args.hw_mjpeg_decoder; diff --git a/src/momo_args.h b/src/momo_args.h index edf4ba5c..f2915c2d 100644 --- a/src/momo_args.h +++ b/src/momo_args.h @@ -24,13 +24,17 @@ struct MomoArgs { #else bool hw_mjpeg_decoder = false; #endif + // Raspberry Pi 4 の場合だけ使える + bool use_libcamera = false; + // use_libcamera == true の場合だけ使える。 + // sora_video_codec_type == "H264" かつ sora_simulcast == false の場合だけしか機能しない。 + bool use_libcamera_native = false; std::string video_device = ""; std::string resolution = "VGA"; int framerate = 30; bool fixed_resolution = false; std::string priority = "FRAMERATE"; bool use_sdl = false; - bool show_me = false; int window_width = 640; int window_height = 480; bool fullscreen = false; diff --git a/src/rtc/aligned_encoder_adapter.cpp b/src/rtc/aligned_encoder_adapter.cpp new file mode 100644 index 00000000..91ed6a8a --- /dev/null +++ b/src/rtc/aligned_encoder_adapter.cpp @@ -0,0 +1,96 @@ +#include "aligned_encoder_adapter.h" + +#include + +static int Align(int size, int alignment) { + return size - (size % alignment); +} + +AlignedEncoderAdapter::AlignedEncoderAdapter( + std::shared_ptr encoder, + int horizontal_alignment, + int vertical_alignment) + : encoder_(encoder), + horizontal_alignment_(horizontal_alignment), + vertical_alignment_(vertical_alignment) {} + +void AlignedEncoderAdapter::SetFecControllerOverride( + webrtc::FecControllerOverride* fec_controller_override) { + encoder_->SetFecControllerOverride(fec_controller_override); +} +int AlignedEncoderAdapter::Release() { + return encoder_->Release(); +} +int AlignedEncoderAdapter::InitEncode( + const webrtc::VideoCodec* codec_settings, + const webrtc::VideoEncoder::Settings& settings) { + auto cs = *codec_settings; + cs.width = Align(cs.width, horizontal_alignment_); + cs.height = Align(cs.height, vertical_alignment_); + for (int i = 0; i < cs.numberOfSimulcastStreams; i++) { + cs.simulcastStream[i].width = + Align(cs.simulcastStream[i].width, horizontal_alignment_); + cs.simulcastStream[i].height = + Align(cs.simulcastStream[i].height, vertical_alignment_); + } + width_ = cs.width; + height_ = cs.height; + return encoder_->InitEncode(&cs, settings); +} +int AlignedEncoderAdapter::Encode( + const webrtc::VideoFrame& input_image, + const std::vector* frame_types) { + auto frame = input_image; + auto frame_ratio = (double)frame.width() / frame.height(); + auto target_ratio = (double)width_ / height_; + int crop_width; + int crop_height; + if (frame_ratio > target_ratio) { + // frame の横の方が広い場合は height に合わせる + crop_height = frame.height(); + crop_width = (int)(crop_height * target_ratio); + } else { + // frame の縦の方が広い場合は width に合わせる + crop_width = frame.width(); + crop_height = (int)(crop_width / target_ratio); + } + auto crop_x = (frame.width() - crop_width) / 2; + auto crop_y = (frame.height() - crop_height) / 2; + // RTC_LOG(LS_INFO) << "type=" << frame.video_frame_buffer()->type() + // << " crop_x=" << crop_x << " crop_y=" << crop_y + // << " crop_width=" << crop_width + // << " crop_height=" << crop_height << " width_=" << width_ + // << " height_=" << height_ << " frame_width=" << frame.width() + // << " frame_height=" << frame.height(); + if (crop_x != 0 || crop_y != 0 || frame.width() != width_ || + frame.height() != height_) { + auto buffer = frame.video_frame_buffer()->CropAndScale( + crop_x / 2, crop_y / 2, crop_width, crop_height, width_, height_); + frame.set_video_frame_buffer(buffer); + } + + return encoder_->Encode(frame, frame_types); +} + +int AlignedEncoderAdapter::RegisterEncodeCompleteCallback( + webrtc::EncodedImageCallback* callback) { + return encoder_->RegisterEncodeCompleteCallback(callback); +} +void AlignedEncoderAdapter::SetRates(const RateControlParameters& parameters) { + encoder_->SetRates(parameters); +} +void AlignedEncoderAdapter::OnPacketLossRateUpdate(float packet_loss_rate) { + encoder_->OnPacketLossRateUpdate(packet_loss_rate); +} +void AlignedEncoderAdapter::OnRttUpdate(int64_t rtt_ms) { + encoder_->OnRttUpdate(rtt_ms); +} +void AlignedEncoderAdapter::OnLossNotification( + const LossNotification& loss_notification) { + encoder_->OnLossNotification(loss_notification); +} + +webrtc::VideoEncoder::EncoderInfo AlignedEncoderAdapter::GetEncoderInfo() + const { + return encoder_->GetEncoderInfo(); +} diff --git a/src/rtc/aligned_encoder_adapter.h b/src/rtc/aligned_encoder_adapter.h new file mode 100644 index 00000000..5de1acb6 --- /dev/null +++ b/src/rtc/aligned_encoder_adapter.h @@ -0,0 +1,46 @@ +#ifndef ALIGNED_ENCODER_ADAPTER_H_ +#define ALIGNED_ENCODER_ADAPTER_H_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +class AlignedEncoderAdapter : public webrtc::VideoEncoder { + public: + AlignedEncoderAdapter(std::shared_ptr encoder, + int horizontal_alignment, + int vertical_alignment); + + void SetFecControllerOverride( + webrtc::FecControllerOverride* fec_controller_override) override; + int Release() override; + int InitEncode(const webrtc::VideoCodec* codec_settings, + const webrtc::VideoEncoder::Settings& settings) override; + int Encode(const webrtc::VideoFrame& input_image, + const std::vector* frame_types) override; + int RegisterEncodeCompleteCallback( + webrtc::EncodedImageCallback* callback) override; + void SetRates(const RateControlParameters& parameters) override; + void OnPacketLossRateUpdate(float packet_loss_rate) override; + void OnRttUpdate(int64_t rtt_ms) override; + void OnLossNotification(const LossNotification& loss_notification) override; + + EncoderInfo GetEncoderInfo() const override; + + private: + std::shared_ptr encoder_; + int horizontal_alignment_; + int vertical_alignment_; + int width_; + int height_; +}; + +#endif \ No newline at end of file diff --git a/src/rtc/device_video_capturer.cpp b/src/rtc/device_video_capturer.cpp index d3896232..05bd1290 100644 --- a/src/rtc/device_video_capturer.cpp +++ b/src/rtc/device_video_capturer.cpp @@ -19,7 +19,9 @@ #include #include -DeviceVideoCapturer::DeviceVideoCapturer() : vcm_(nullptr) {} +DeviceVideoCapturer::DeviceVideoCapturer() + : ScalableVideoTrackSource(ScalableVideoTrackSourceConfig()), + vcm_(nullptr) {} DeviceVideoCapturer::~DeviceVideoCapturer() { Destroy(); diff --git a/src/rtc/momo_video_decoder_factory.cpp b/src/rtc/momo_video_decoder_factory.cpp index e68d72ad..806757d9 100644 --- a/src/rtc/momo_video_decoder_factory.cpp +++ b/src/rtc/momo_video_decoder_factory.cpp @@ -12,7 +12,8 @@ #include #if !defined(__arm__) || defined(__aarch64__) || defined(__ARM_NEON__) -#include +#include +#include #endif #if defined(__APPLE__) @@ -31,6 +32,10 @@ #include "hwenc_msdk/msdk_video_decoder.h" #endif +#if USE_V4L2_ENCODER +#include "hwenc_v4l2/v4l2_h264_decoder.h" +#endif + namespace { bool IsFormatSupported( @@ -71,16 +76,22 @@ MomoVideoDecoderFactory::GetSupportedFormats() const { config_.vp9_decoder == VideoCodecInfo::Type::Jetson || config_.vp9_decoder == VideoCodecInfo::Type::NVIDIA || config_.vp9_decoder == VideoCodecInfo::Type::Intel) { - for (const webrtc::SdpVideoFormat& format : webrtc::SupportedVP9Codecs()) { + for (const webrtc::SdpVideoFormat& format : + webrtc::SupportedVP9Codecs(true)) { supported_codecs.push_back(format); } } +#if !defined(__arm__) || defined(__aarch64__) || defined(__ARM_NEON__) // AV1 if (config_.av1_decoder == VideoCodecInfo::Type::Software || + config_.av1_decoder == VideoCodecInfo::Type::Jetson || config_.av1_decoder == VideoCodecInfo::Type::Intel) { - supported_codecs.push_back(webrtc::SdpVideoFormat(cricket::kAv1CodecName)); + supported_codecs.push_back(webrtc::SdpVideoFormat( + cricket::kAv1CodecName, webrtc::SdpVideoFormat::Parameters(), + webrtc::LibaomAv1EncoderSupportedScalabilityModes())); } +#endif // H264 std::vector h264_codecs = { @@ -134,9 +145,10 @@ MomoVideoDecoderFactory::CreateVideoDecoder( } #endif #if USE_JETSON_ENCODER - if (config_.vp8_decoder == VideoCodecInfo::Type::Jetson) { + if (config_.vp8_decoder == VideoCodecInfo::Type::Jetson && + JetsonVideoDecoder::IsSupportedVP8()) { return std::unique_ptr( - absl::make_unique(V4L2_PIX_FMT_VP8)); + absl::make_unique(webrtc::kVideoCodecVP8)); } #endif @@ -163,7 +175,7 @@ MomoVideoDecoderFactory::CreateVideoDecoder( #if USE_JETSON_ENCODER if (config_.vp9_decoder == VideoCodecInfo::Type::Jetson) { return std::unique_ptr( - absl::make_unique(V4L2_PIX_FMT_VP9)); + absl::make_unique(webrtc::kVideoCodecVP9)); } #endif @@ -180,9 +192,16 @@ MomoVideoDecoderFactory::CreateVideoDecoder( MFX_CODEC_AV1)); } #endif +#if USE_JETSON_ENCODER + if (config_.av1_decoder == VideoCodecInfo::Type::Jetson && + JetsonVideoDecoder::IsSupportedAV1()) { + return std::unique_ptr( + absl::make_unique(webrtc::kVideoCodecAV1)); + } +#endif #if !defined(__arm__) || defined(__aarch64__) || defined(__ARM_NEON__) if (config_.av1_decoder == VideoCodecInfo::Type::Software) { - return webrtc::CreateLibaomAv1Decoder(); + return webrtc::CreateDav1dDecoder(); } #endif } @@ -211,7 +230,7 @@ MomoVideoDecoderFactory::CreateVideoDecoder( #if USE_JETSON_ENCODER if (config_.h264_decoder == VideoCodecInfo::Type::Jetson) { return std::unique_ptr( - absl::make_unique(V4L2_PIX_FMT_H264)); + absl::make_unique(webrtc::kVideoCodecH264)); } #endif @@ -221,6 +240,13 @@ MomoVideoDecoderFactory::CreateVideoDecoder( absl::make_unique()); } #endif + +#if USE_V4L2_ENCODER + if (config_.h264_decoder == VideoCodecInfo::Type::V4L2) { + return std::unique_ptr( + absl::make_unique(webrtc::kVideoCodecH264)); + } +#endif } RTC_DCHECK_NOTREACHED(); diff --git a/src/rtc/momo_video_encoder_factory.cpp b/src/rtc/momo_video_encoder_factory.cpp index 93187b34..f3db1e57 100644 --- a/src/rtc/momo_video_encoder_factory.cpp +++ b/src/rtc/momo_video_encoder_factory.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -16,6 +17,7 @@ #include #if !defined(__arm__) || defined(__aarch64__) || defined(__ARM_NEON__) +#include #include #endif @@ -35,6 +37,11 @@ #if USE_MSDK_ENCODER #include "hwenc_msdk/msdk_video_encoder.h" #endif +#if USE_V4L2_ENCODER +#include "hwenc_v4l2/v4l2_h264_encoder.h" +#endif + +#include "rtc/aligned_encoder_adapter.h" MomoVideoEncoderFactory::MomoVideoEncoderFactory( const MomoVideoEncoderFactoryConfig& config) @@ -61,7 +68,8 @@ MomoVideoEncoderFactory::GetSupportedFormats() const { // VP9 if (config_.vp9_encoder == VideoCodecInfo::Type::Software) { - for (const webrtc::SdpVideoFormat& format : webrtc::SupportedVP9Codecs()) { + for (const webrtc::SdpVideoFormat& format : + webrtc::SupportedVP9Codecs(true)) { supported_codecs.push_back(format); } } else if (config_.vp9_encoder == VideoCodecInfo::Type::Jetson || @@ -72,11 +80,16 @@ MomoVideoEncoderFactory::GetSupportedFormats() const { webrtc::VP9ProfileToString(webrtc::VP9Profile::kProfile0)}})); } +#if !defined(__arm__) || defined(__aarch64__) || defined(__ARM_NEON__) // AV1 if (config_.av1_encoder == VideoCodecInfo::Type::Software || + config_.av1_encoder == VideoCodecInfo::Type::Jetson || config_.av1_encoder == VideoCodecInfo::Type::Intel) { - supported_codecs.push_back(webrtc::SdpVideoFormat(cricket::kAv1CodecName)); + supported_codecs.push_back(webrtc::SdpVideoFormat( + cricket::kAv1CodecName, webrtc::SdpVideoFormat::Parameters(), + webrtc::LibaomAv1EncoderSupportedScalabilityModes())); } +#endif // H264 std::vector h264_codecs = { @@ -113,6 +126,12 @@ MomoVideoEncoderFactory::GetSupportedFormats() const { supported_codecs.push_back(format); } } +#endif + } else if (config_.h264_encoder == VideoCodecInfo::Type::V4L2) { +#if USE_V4L2_ENCODER + for (const webrtc::SdpVideoFormat& format : h264_codecs) { + supported_codecs.push_back(format); + } #endif } else if ((config_.h264_encoder == VideoCodecInfo::Type::Software) || config_.h264_encoder != VideoCodecInfo::Type::NotSupported) { @@ -169,10 +188,11 @@ MomoVideoEncoderFactory::CreateVideoEncoder( }); } #if USE_JETSON_ENCODER - if (config_.vp8_encoder == VideoCodecInfo::Type::Jetson) { + if (config_.vp8_encoder == VideoCodecInfo::Type::Jetson && + JetsonVideoEncoder::IsSupportedVP8()) { return WithSimulcast(format, [](const webrtc::SdpVideoFormat& format) { return std::unique_ptr( - absl::make_unique(cricket::VideoCodec(format))); + absl::make_unique(cricket::CreateVideoCodec(format))); }); } #endif @@ -190,14 +210,15 @@ MomoVideoEncoderFactory::CreateVideoEncoder( if (absl::EqualsIgnoreCase(format.name, cricket::kVp9CodecName)) { if (config_.vp9_encoder == VideoCodecInfo::Type::Software) { return WithSimulcast(format, [](const webrtc::SdpVideoFormat& format) { - return webrtc::VP9Encoder::Create(cricket::VideoCodec(format)); + return webrtc::VP9Encoder::Create(cricket::CreateVideoCodec(format)); }); } #if USE_JETSON_ENCODER - if (config_.vp9_encoder == VideoCodecInfo::Type::Jetson) { + if (config_.vp9_encoder == VideoCodecInfo::Type::Jetson && + JetsonVideoEncoder::IsSupportedVP9()) { return WithSimulcast(format, [](const webrtc::SdpVideoFormat& format) { return std::unique_ptr( - absl::make_unique(cricket::VideoCodec(format))); + absl::make_unique(cricket::CreateVideoCodec(format))); }); } #endif @@ -228,6 +249,15 @@ MomoVideoEncoderFactory::CreateVideoEncoder( MFX_CODEC_AV1)); }); } +#endif +#if USE_JETSON_ENCODER + if (config_.av1_encoder == VideoCodecInfo::Type::Jetson && + JetsonVideoEncoder::IsSupportedAV1()) { + return WithSimulcast(format, [](const webrtc::SdpVideoFormat& format) { + return std::unique_ptr( + absl::make_unique(cricket::CreateVideoCodec(format))); + }); + } #endif } @@ -245,7 +275,7 @@ MomoVideoEncoderFactory::CreateVideoEncoder( if (config_.h264_encoder == VideoCodecInfo::Type::MMAL) { return WithSimulcast(format, [](const webrtc::SdpVideoFormat& format) { return std::unique_ptr( - absl::make_unique(cricket::VideoCodec(format))); + absl::make_unique(cricket::CreateVideoCodec(format))); }); } #endif @@ -254,7 +284,7 @@ MomoVideoEncoderFactory::CreateVideoEncoder( if (config_.h264_encoder == VideoCodecInfo::Type::Jetson) { return WithSimulcast(format, [](const webrtc::SdpVideoFormat& format) { return std::unique_ptr( - absl::make_unique(cricket::VideoCodec(format))); + absl::make_unique(cricket::CreateVideoCodec(format))); }); } #endif @@ -269,14 +299,14 @@ MomoVideoEncoderFactory::CreateVideoEncoder( config_.cuda_context](const webrtc::SdpVideoFormat& format) { return std::unique_ptr( absl::make_unique( - cricket::VideoCodec(format), cuda_context)); + cricket::CreateVideoCodec(format), cuda_context)); } #else format, [](const webrtc::SdpVideoFormat& format) { return std::unique_ptr( absl::make_unique( - cricket::VideoCodec(format))); + cricket::CreateVideoCodec(format))); } #endif ); @@ -290,6 +320,14 @@ MomoVideoEncoderFactory::CreateVideoEncoder( MFX_CODEC_AVC)); }); } +#endif +#if USE_V4L2_ENCODER + if (config_.h264_encoder == VideoCodecInfo::Type::V4L2) { + return WithSimulcast(format, [](const webrtc::SdpVideoFormat& format) { + return std::unique_ptr( + absl::make_unique(cricket::CreateVideoCodec(format))); + }); + } #endif } @@ -302,11 +340,12 @@ std::unique_ptr MomoVideoEncoderFactory::WithSimulcast( const webrtc::SdpVideoFormat& format, std::function( const webrtc::SdpVideoFormat&)> create) { + std::shared_ptr encoder; if (internal_encoder_factory_) { - return std::unique_ptr( - new webrtc::SimulcastEncoderAdapter(internal_encoder_factory_.get(), - format)); + encoder = std::make_shared( + internal_encoder_factory_.get(), format); } else { - return create(format); + encoder.reset(create(format).release()); } + return std::make_unique(encoder, 16, 16); } diff --git a/src/rtc/rtc_manager.cpp b/src/rtc/rtc_manager.cpp index c9cb7bff..a64da7b7 100644 --- a/src/rtc/rtc_manager.cpp +++ b/src/rtc/rtc_manager.cpp @@ -75,19 +75,17 @@ RTCManager::RTCManager( cricket::MediaEngineDependencies media_dependencies; media_dependencies.task_queue_factory = dependencies.task_queue_factory.get(); #if defined(_WIN32) - media_dependencies.adm = - worker_thread_->Invoke>( - RTC_FROM_HERE, [&] { - return webrtc::CreateWindowsCoreAudioAudioDeviceModule( - dependencies.task_queue_factory.get()); - }); + media_dependencies.adm = worker_thread_->BlockingCall( + [&]() -> rtc::scoped_refptr { + return webrtc::CreateWindowsCoreAudioAudioDeviceModule( + dependencies.task_queue_factory.get()); + }); #else - media_dependencies.adm = - worker_thread_->Invoke>( - RTC_FROM_HERE, [&] { - return webrtc::AudioDeviceModule::Create( - audio_layer, dependencies.task_queue_factory.get()); - }); + media_dependencies.adm = worker_thread_->BlockingCall( + [&]() -> rtc::scoped_refptr { + return webrtc::AudioDeviceModule::Create( + audio_layer, dependencies.task_queue_factory.get()); + }); #endif media_dependencies.audio_encoder_factory = webrtc::CreateBuiltinAudioEncoderFactory(); @@ -137,8 +135,8 @@ RTCManager::RTCManager( using result_type = std::pair, rtc::scoped_refptr>; - auto p = dependencies.signaling_thread->Invoke( - RTC_FROM_HERE, [&dependencies]() { + auto p = dependencies.signaling_thread->BlockingCall( + [&dependencies]() -> result_type { auto factory = CustomPeerConnectionFactory::Create(std::move(dependencies)); if (factory == nullptr) { @@ -185,16 +183,13 @@ RTCManager::RTCManager( rtc::scoped_refptr video_source = webrtc::VideoTrackSourceProxy::Create( signaling_thread_.get(), worker_thread_.get(), video_track_source); - video_track_ = factory_->CreateVideoTrack(Util::GenerateRandomChars(), - video_source.get()); + video_track_ = + factory_->CreateVideoTrack(video_source, Util::GenerateRandomChars()); if (video_track_) { if (config_.fixed_resolution) { video_track_->set_content_hint( webrtc::VideoTrackInterface::ContentHint::kText); } - if (receiver_ != nullptr && config_.show_me) { - receiver_->AddTrack(video_track_.get()); - } } else { RTC_LOG(LS_WARNING) << __FUNCTION__ << ": Cannot create video_track"; } diff --git a/src/rtc/rtc_manager.h b/src/rtc/rtc_manager.h index f267192c..2a75378c 100644 --- a/src/rtc/rtc_manager.h +++ b/src/rtc/rtc_manager.h @@ -53,7 +53,6 @@ struct RTCManagerConfig { bool no_audio_device = false; bool fixed_resolution = false; - bool show_me = false; bool simulcast = false; bool hardware_encoder_only = false; diff --git a/src/rtc/scalable_track_source.cpp b/src/rtc/scalable_track_source.cpp index ac5098bd..043ebe98 100644 --- a/src/rtc/scalable_track_source.cpp +++ b/src/rtc/scalable_track_source.cpp @@ -19,10 +19,12 @@ #include #include -#include "native_buffer.h" +// libyuv +#include -ScalableVideoTrackSource::ScalableVideoTrackSource() - : AdaptedVideoTrackSource(4) {} +ScalableVideoTrackSource::ScalableVideoTrackSource( + ScalableVideoTrackSourceConfig config) + : AdaptedVideoTrackSource(4), config_(config) {} ScalableVideoTrackSource::~ScalableVideoTrackSource() {} bool ScalableVideoTrackSource::is_screencast() const { @@ -42,12 +44,52 @@ bool ScalableVideoTrackSource::remote() const { return false; } -void ScalableVideoTrackSource::OnCapturedFrame( - const webrtc::VideoFrame& frame) { +bool ScalableVideoTrackSource::OnCapturedFrame( + const webrtc::VideoFrame& video_frame) { + webrtc::VideoFrame frame = video_frame; + const int64_t timestamp_us = frame.timestamp_us(); const int64_t translated_timestamp_us = timestamp_aligner_.TranslateTimestamp(timestamp_us, rtc::TimeMicros()); + // 回転が必要 + if (frame.rotation() != webrtc::kVideoRotation_0) { + int width; + int height; + libyuv::RotationMode mode; + switch (frame.rotation()) { + case webrtc::kVideoRotation_180: + width = frame.width(); + height = frame.height(); + mode = libyuv::kRotate180; + break; + case webrtc::kVideoRotation_90: + width = frame.height(); + height = frame.width(); + mode = libyuv::kRotate90; + break; + case webrtc::kVideoRotation_270: + default: + width = frame.height(); + height = frame.width(); + mode = libyuv::kRotate270; + break; + } + + rtc::scoped_refptr rotated = + webrtc::I420Buffer::Create(width, height); + rtc::scoped_refptr src = + frame.video_frame_buffer()->ToI420(); + libyuv::I420Rotate(src->DataY(), src->StrideY(), src->DataU(), + src->StrideU(), src->DataV(), src->StrideV(), + rotated->MutableDataY(), rotated->StrideY(), + rotated->MutableDataU(), rotated->StrideU(), + rotated->MutableDataV(), rotated->StrideV(), + frame.width(), frame.height(), mode); + frame.set_video_frame_buffer(rotated); + frame.set_rotation(webrtc::kVideoRotation_0); + } + int adapted_width; int adapted_height; int crop_width; @@ -57,16 +99,17 @@ void ScalableVideoTrackSource::OnCapturedFrame( if (!AdaptFrame(frame.width(), frame.height(), timestamp_us, &adapted_width, &adapted_height, &crop_width, &crop_height, &crop_x, &crop_y)) { - return; + return false; } - if (UseNativeBuffer() && frame.video_frame_buffer()->type() == - webrtc::VideoFrameBuffer::Type::kNative) { - NativeBuffer* frame_buffer = - dynamic_cast(frame.video_frame_buffer().get()); - frame_buffer->SetScaledSize(adapted_width, adapted_height); + if (config_.on_frame) { + config_.on_frame(frame); + } + + if (frame.video_frame_buffer()->type() == + webrtc::VideoFrameBuffer::Type::kNative) { OnFrame(frame); - return; + return true; } rtc::scoped_refptr buffer = @@ -86,4 +129,6 @@ void ScalableVideoTrackSource::OnCapturedFrame( .set_rotation(frame.rotation()) .set_timestamp_us(translated_timestamp_us) .build()); + + return true; } diff --git a/src/rtc/scalable_track_source.h b/src/rtc/scalable_track_source.h index d3e4c49f..d5442445 100644 --- a/src/rtc/scalable_track_source.h +++ b/src/rtc/scalable_track_source.h @@ -19,22 +19,24 @@ #include #include +struct ScalableVideoTrackSourceConfig { + std::function on_frame; +}; + class ScalableVideoTrackSource : public rtc::AdaptedVideoTrackSource { public: - ScalableVideoTrackSource(); + ScalableVideoTrackSource(ScalableVideoTrackSourceConfig config); virtual ~ScalableVideoTrackSource(); bool is_screencast() const override; absl::optional needs_denoising() const override; webrtc::MediaSourceInterface::SourceState state() const override; bool remote() const override; - void OnCapturedFrame(const webrtc::VideoFrame& frame); - - protected: - virtual bool UseNativeBuffer() { return false; } + bool OnCapturedFrame(const webrtc::VideoFrame& frame); private: + ScalableVideoTrackSourceConfig config_; rtc::TimestampAligner timestamp_aligner_; }; -#endif // SCALABLE_VIDEO_TRACK_SOURCE_H_ +#endif diff --git a/src/rtc/screen_video_capturer.cpp b/src/rtc/screen_video_capturer.cpp index 3fb86791..925f4f99 100644 --- a/src/rtc/screen_video_capturer.cpp +++ b/src/rtc/screen_video_capturer.cpp @@ -55,7 +55,8 @@ ScreenVideoCapturer::ScreenVideoCapturer( size_t max_width, size_t max_height, size_t target_fps) - : max_width_(max_width), + : ScalableVideoTrackSource(ScalableVideoTrackSourceConfig()), + max_width_(max_width), max_height_(max_height), requested_frame_duration_((int)(1000.0f / target_fps)), max_cpu_consumption_percentage_(50), @@ -95,7 +96,6 @@ ScreenVideoCapturer::CreateDesktopCaptureOptions() { #if defined(_WIN32) options.set_allow_directx_capturer(true); - options.set_allow_use_magnification_api(false); #elif defined(__APPLE__) options.set_allow_iosurface(true); #endif diff --git a/src/sora/sora_client.cpp b/src/sora/sora_client.cpp index d56d4409..f3a015dc 100644 --- a/src/sora/sora_client.cpp +++ b/src/sora/sora_client.cpp @@ -500,6 +500,10 @@ void SoraClient::OnRead(boost::system::error_code ec, if (p.count("adaptivePtime") != 0) { params.adaptive_ptime = p["adaptivePtime"].as_bool(); } + if (p.count("scalabilityMode") != 0) { + params.scalability_mode = + p["scalabilityMode"].as_string().c_str(); + } encoding_parameters.push_back(params); } diff --git a/src/util.cpp b/src/util.cpp index f05dcf1c..046101cf 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -64,7 +64,7 @@ void Util::ParseArgs(int argc, auto is_valid_hw_mjpeg_decoder = CLI::Validator( [](std::string input) -> std::string { if (input == "1") { -#if USE_MMAL_ENCODER || USE_JETSON_ENCODER || USE_NVCODEC_ENCODER +#if USE_MMAL_ENCODER || USE_JETSON_ENCODER || USE_NVCODEC_ENCODER || USE_V4L2_ENCODER return std::string(); #else return "Not available because your device does not have this " @@ -147,6 +147,11 @@ void Util::ParseArgs(int argc, "(only on supported devices)") ->check(is_valid_hw_mjpeg_decoder) ->transform(CLI::CheckedTransformer(bool_map, CLI::ignore_case)); + app.add_flag("--use-libcamera", args.use_libcamera, + "Use libcamera for video capture (only on supported devices)"); + app.add_flag("--use-libcamera-native", args.use_libcamera_native, + "Use native buffer for H.264 encoding"); + #if defined(__APPLE__) || defined(_WIN32) app.add_option("--video-device", args.video_device, "Use the video device specified by an index or a name " @@ -172,9 +177,6 @@ void Util::ParseArgs(int argc, app.add_flag("--use-sdl", args.use_sdl, "Show video using SDL (if SDL is available)") ->check(is_sdl_available); - app.add_flag("--show-me", args.show_me, - "Show self video (if SDL is available)") - ->check(is_sdl_available); app.add_option("--window-width", args.window_width, "Window width for videos (if SDL is available)") ->check(is_sdl_available) @@ -367,7 +369,7 @@ void Util::ParseArgs(int argc, auto is_json = CLI::Validator( [](std::string input) -> std::string { boost::json::error_code ec; - boost::json::parse(input); + boost::json::parse(input, ec); if (ec) { return "Value " + input + " is not JSON Value"; } @@ -386,15 +388,6 @@ void Util::ParseArgs(int argc, exit(app.exit(e)); } - // サイマルキャストは VP8, H264 のみで動作する - if (args.sora_simulcast && args.sora_video_codec_type != "VP8" && - args.sora_video_codec_type != "H264") { - std::cerr << "Simulcast works only --video-codec-type=VP8 or " - "--video-codec-type=H264." - << std::endl; - exit(1); - } - if (!serial_setting.empty()) { auto separater_pos = serial_setting.find(','); std::string baudrate_str = serial_setting.substr(separater_pos + 1); @@ -615,7 +608,7 @@ http::response Util::BadRequest( res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, "text/html"); res.keep_alive(req.keep_alive()); - res.body() = why.to_string(); + res.body() = why; res.prepare_payload(); return res; } @@ -627,7 +620,7 @@ http::response Util::NotFound( res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, "text/html"); res.keep_alive(req.keep_alive()); - res.body() = "The resource '" + target.to_string() + "' was not found."; + res.body() = "The resource '" + std::string(target) + "' was not found."; res.prepare_payload(); return res; } @@ -640,7 +633,7 @@ http::response Util::ServerError( res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, "text/html"); res.keep_alive(req.keep_alive()); - res.body() = "An error occurred: '" + what.to_string() + "'"; + res.body() = "An error occurred: '" + std::string(what) + "'"; res.prepare_payload(); return res; } diff --git a/src/v4l2_video_capturer/v4l2_video_capturer.cpp b/src/v4l2_video_capturer/v4l2_video_capturer.cpp index d9bd3078..190c40f7 100644 --- a/src/v4l2_video_capturer/v4l2_video_capturer.cpp +++ b/src/v4l2_video_capturer/v4l2_video_capturer.cpp @@ -93,8 +93,8 @@ rtc::scoped_refptr V4L2VideoCapturer::Create( RTC_LOG(LS_WARNING) << "Failed to GetDeviceName"; return nullptr; } - rtc::scoped_refptr v4l2_capturer( - new rtc::RefCountedObject()); + rtc::scoped_refptr v4l2_capturer = + rtc::make_ref_counted(config); if (v4l2_capturer->Init((const char*)&unique_name, config.video_device) < 0) { RTC_LOG(LS_WARNING) << "Failed to create V4L2VideoCapturer(" << unique_name << ")"; @@ -109,8 +109,9 @@ rtc::scoped_refptr V4L2VideoCapturer::Create( return v4l2_capturer; } -V4L2VideoCapturer::V4L2VideoCapturer() - : _deviceFd(-1), +V4L2VideoCapturer::V4L2VideoCapturer(V4L2VideoCapturerConfig config) + : ScalableVideoTrackSource(config), + _deviceFd(-1), _buffersAllocatedByDevice(-1), _currentWidth(-1), _currentHeight(-1), diff --git a/src/v4l2_video_capturer/v4l2_video_capturer.h b/src/v4l2_video_capturer/v4l2_video_capturer.h index 8157a000..8a36067a 100644 --- a/src/v4l2_video_capturer/v4l2_video_capturer.h +++ b/src/v4l2_video_capturer/v4l2_video_capturer.h @@ -23,7 +23,7 @@ #include #include -struct V4L2VideoCapturerConfig { +struct V4L2VideoCapturerConfig : ScalableVideoTrackSourceConfig { std::string video_device; int width = 640; int height = 480; @@ -38,13 +38,13 @@ class V4L2VideoCapturer : public ScalableVideoTrackSource { V4L2VideoCapturerConfig config); static void LogDeviceList( webrtc::VideoCaptureModule::DeviceInfo* device_info); - V4L2VideoCapturer(); + V4L2VideoCapturer(V4L2VideoCapturerConfig config); ~V4L2VideoCapturer(); int32_t Init(const char* deviceUniqueId, const std::string& specifiedVideoDevice); virtual int32_t StartCapture(V4L2VideoCapturerConfig config); - virtual bool UseNativeBuffer() override; + virtual bool UseNativeBuffer(); protected: virtual int32_t StopCapture(); diff --git a/src/video_codec_info.h b/src/video_codec_info.h index 08e8f747..66f8c8af 100644 --- a/src/video_codec_info.h +++ b/src/video_codec_info.h @@ -17,6 +17,7 @@ #endif #if USE_JETSON_ENCODER +#include "hwenc_jetson/jetson_video_decoder.h" #include "hwenc_jetson/jetson_video_encoder.h" #endif @@ -28,6 +29,7 @@ struct VideoCodecInfo { NVIDIA, Intel, VideoToolbox, + V4L2, Software, NotSupported, }; @@ -83,6 +85,8 @@ struct VideoCodecInfo { return {"Intel Media SDK", "intel"}; case Type::VideoToolbox: return {"VideoToolbox", "videotoolbox"}; + case Type::V4L2: + return {"V4L2", "v4l2"}; case Type::Software: return {"Software", "software"}; default: @@ -239,11 +243,24 @@ struct VideoCodecInfo { if (JetsonVideoEncoder::IsSupportedVP8()) { info.vp8_encoders.push_back(Type::Jetson); } - info.vp8_decoders.push_back(Type::Jetson); - info.vp9_decoders.push_back(Type::Jetson); + if (JetsonVideoDecoder::IsSupportedVP8()) { + info.vp8_decoders.push_back(Type::Jetson); + } if (JetsonVideoEncoder::IsSupportedVP9()) { info.vp9_encoders.push_back(Type::Jetson); } + info.vp9_decoders.push_back(Type::Jetson); + if (JetsonVideoEncoder::IsSupportedAV1()) { + info.av1_encoders.push_back(Type::Jetson); + } + if (JetsonVideoDecoder::IsSupportedAV1()) { + info.av1_decoders.push_back(Type::Jetson); + } +#endif + +#if USE_V4L2_ENCODER + info.h264_encoders.push_back(Type::V4L2); + info.h264_decoders.push_back(Type::V4L2); #endif info.vp8_encoders.push_back(Type::Software);