diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 739c4e17..ef97b47a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -156,6 +156,7 @@ jobs: uses: docker/build-push-action@v5 with: context: . + file: docker/Dockerfile platforms: | linux/amd64 linux/386 @@ -215,7 +216,7 @@ jobs: uses: docker/build-push-action@v5 with: context: . - file: hardware.Dockerfile + file: docker/hardware.Dockerfile platforms: linux/amd64 push: ${{ github.event_name != 'pull_request' }} tags: ${{ steps.meta-hw.outputs.tags }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f2089dec..5d9e7e25 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -79,6 +79,7 @@ jobs: uses: docker/build-push-action@v5 with: context: . + file: docker/Dockerfile platforms: linux/${{ matrix.platform }} push: false load: true @@ -92,7 +93,7 @@ jobs: uses: docker/build-push-action@v5 with: context: . - file: hardware.Dockerfile + file: docker/hardware.Dockerfile platforms: linux/amd64 push: false load: true diff --git a/Dockerfile b/docker/Dockerfile similarity index 79% rename from Dockerfile rename to docker/Dockerfile index ba436825..34a96757 100644 --- a/Dockerfile +++ b/docker/Dockerfile @@ -5,16 +5,7 @@ ARG PYTHON_VERSION="3.11" ARG GO_VERSION="1.24" -# 1. Download ngrok binary (for support arm/v6) -FROM alpine AS ngrok -ARG TARGETARCH -ARG TARGETOS - -ADD https://bin.equinox.io/c/bNyj1mQVY4c/ngrok-v3-stable-${TARGETOS}-${TARGETARCH}.tgz / -RUN tar -xzf /ngrok-v3-stable-${TARGETOS}-${TARGETARCH}.tgz -C /bin - - -# 2. Build go2rtc binary +# 1. Build go2rtc binary FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine AS build ARG TARGETPLATFORM ARG TARGETOS @@ -35,7 +26,7 @@ COPY . . RUN --mount=type=cache,target=/root/.cache/go-build CGO_ENABLED=0 go build -ldflags "-s -w" -trimpath -# 3. Final image +# 2. Final image FROM python:${PYTHON_VERSION}-alpine AS base # Install ffmpeg, tini (for signal handling), @@ -55,7 +46,6 @@ RUN if [ "${TARGETARCH}" = "amd64" ]; then apk add --no-cache libva-intel-driver # RUN libva-vdpau-driver mesa-vdpau-gallium (+150MB total) COPY --from=build /build/go2rtc /usr/local/bin/ -COPY --from=ngrok /bin/ngrok /usr/local/bin/ ENTRYPOINT ["/sbin/tini", "--"] VOLUME /config diff --git a/hardware.Dockerfile b/docker/hardware.Dockerfile similarity index 75% rename from hardware.Dockerfile rename to docker/hardware.Dockerfile index e75a97cd..03b7d496 100644 --- a/hardware.Dockerfile +++ b/docker/hardware.Dockerfile @@ -5,15 +5,10 @@ # https://packages.debian.org/trixie/ffmpeg ARG DEBIAN_VERSION="trixie-slim" ARG GO_VERSION="1.24-bookworm" -ARG NGROK_VERSION="3" - -FROM debian:${DEBIAN_VERSION} AS base -FROM golang:${GO_VERSION} AS go -FROM ngrok/ngrok:${NGROK_VERSION} AS ngrok # 1. Build go2rtc binary -FROM --platform=$BUILDPLATFORM go AS build +FROM --platform=$BUILDPLATFORM golang:${GO_VERSION} AS build ARG TARGETPLATFORM ARG TARGETOS ARG TARGETARCH @@ -31,34 +26,28 @@ COPY . . RUN --mount=type=cache,target=/root/.cache/go-build CGO_ENABLED=0 go build -ldflags "-s -w" -trimpath -# 2. Collect all files -FROM scratch AS rootfs +# 2. Final image +FROM debian:${DEBIAN_VERSION} -COPY --link --from=build /build/go2rtc /usr/local/bin/ -COPY --link --from=ngrok /bin/ngrok /usr/local/bin/ - -# 3. Final image -FROM base # Prepare apt for buildkit cache RUN rm -f /etc/apt/apt.conf.d/docker-clean \ && echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' >/etc/apt/apt.conf.d/keep-cache -# Install ffmpeg, bash (for run.sh), tini (for signal handling), + +# Install ffmpeg, tini (for signal handling), # and other common tools for the echo source. # non-free for Intel QSV support (not used by go2rtc, just for tests) # mesa-va-drivers for AMD APU # libasound2-plugins for ALSA support RUN --mount=type=cache,target=/var/cache/apt,sharing=locked --mount=type=cache,target=/var/lib/apt,sharing=locked \ echo 'deb http://deb.debian.org/debian trixie non-free' > /etc/apt/sources.list.d/debian-non-free.list && \ - apt-get -y update && apt-get -y install tini ffmpeg \ + apt-get -y update && apt-get -y install ffmpeg tini \ python3 curl jq \ intel-media-va-driver-non-free \ mesa-va-drivers \ libasound2-plugins && \ apt-get clean && rm -rf /var/lib/apt/lists/* -COPY --link --from=rootfs / / - - +COPY --from=build /build/go2rtc /usr/local/bin/ ENTRYPOINT ["/usr/bin/tini", "--"] VOLUME /config diff --git a/internal/ffmpeg/ffmpeg.go b/internal/ffmpeg/ffmpeg.go index 25d61e4b..8eba0a0b 100644 --- a/internal/ffmpeg/ffmpeg.go +++ b/internal/ffmpeg/ffmpeg.go @@ -129,8 +129,9 @@ var defaults = map[string]string{ // hardware Rockchip // important to use custom ffmpeg https://github.com/AlexxIT/go2rtc/issues/768 // hevc - doesn't have a profile setting - "h264/rkmpp": "-c:v h264_rkmpp_encoder -g 50 -bf 0 -profile:v high -level:v 4.1", - "h265/rkmpp": "-c:v hevc_rkmpp_encoder -g 50 -bf 0 -level:v 5.1", + "h264/rkmpp": "-c:v h264_rkmpp -g 50 -bf 0 -profile:v high -level:v 4.1", + "h265/rkmpp": "-c:v hevc_rkmpp -g 50 -bf 0 -profile:v main -level:v 5.1", + "mjpeg/rkmpp": "-c:v mjpeg_rkmpp", // hardware NVidia on Linux and Windows // preset=p2 - faster, tune=ll - low latency diff --git a/internal/ffmpeg/ffmpeg_test.go b/internal/ffmpeg/ffmpeg_test.go index 2ab1170d..30052d78 100644 --- a/internal/ffmpeg/ffmpeg_test.go +++ b/internal/ffmpeg/ffmpeg_test.go @@ -251,15 +251,33 @@ func _TestParseArgsHwV4l2m2m(t *testing.T) { } func TestParseArgsHwRKMPP(t *testing.T) { - // [HTTP-MJPEG] video will be transcoded to H264 - args := parseArgs("http://example.com#video=h264#hardware=rkmpp") - require.Equal(t, `ffmpeg -hide_banner -fflags nobuffer -flags low_delay -i http://example.com -c:v h264_rkmpp_encoder -g 50 -bf 0 -profile:v high -level:v 4.1 -an -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String()) - - args = parseArgs("http://example.com#video=h264#rotate=180#hardware=rkmpp") - require.Equal(t, `ffmpeg -hide_banner -fflags nobuffer -flags low_delay -i http://example.com -c:v h264_rkmpp_encoder -g 50 -bf 0 -profile:v high -level:v 4.1 -an -vf "transpose=1,transpose=1" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String()) - - args = parseArgs("http://example.com#video=h264#height=320#hardware=rkmpp") - require.Equal(t, `ffmpeg -hide_banner -fflags nobuffer -flags low_delay -i http://example.com -c:v h264_rkmpp_encoder -g 50 -bf 0 -profile:v high -level:v 4.1 -height 320 -an -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String()) + tests := []struct { + name string + source string + expect string + }{ + { + name: "[FILE] transcoding to H264", + source: "bbb.mp4#video=h264#hardware=rkmpp", + expect: `ffmpeg -hide_banner -hwaccel rkmpp -hwaccel_output_format drm_prime -afbc rga -re -i bbb.mp4 -c:v h264_rkmpp -g 50 -bf 0 -profile:v high -level:v 4.1 -an -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, + }, + { + name: "[FILE] transcoding with rotation", + source: "bbb.mp4#video=h264#rotate=180#hardware=rkmpp", + expect: `ffmpeg -hide_banner -hwaccel rkmpp -hwaccel_output_format drm_prime -afbc rga -re -i bbb.mp4 -c:v h264_rkmpp -g 50 -bf 0 -profile:v high -level:v 4.1 -an -vf "format=drm_prime|nv12,hwupload,vpp_rkrga=transpose=4" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, + }, + { + name: "[FILE] transcoding with scaling", + source: "bbb.mp4#video=h264#height=320#hardware=rkmpp", + expect: `ffmpeg -hide_banner -hwaccel rkmpp -hwaccel_output_format drm_prime -afbc rga -re -i bbb.mp4 -c:v h264_rkmpp -g 50 -bf 0 -profile:v high -level:v 4.1 -an -vf "format=drm_prime|nv12,hwupload,scale_rkrga=-1:320:force_original_aspect_ratio=0" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + args := parseArgs(test.source) + require.Equal(t, test.expect, args.String()) + }) + } } func _TestParseArgsHwCuda(t *testing.T) { diff --git a/internal/ffmpeg/hardware/README.md b/internal/ffmpeg/hardware/README.md new file mode 100644 index 00000000..2d7f21cf --- /dev/null +++ b/internal/ffmpeg/hardware/README.md @@ -0,0 +1,106 @@ +# Hardware + +You **DON'T** need hardware acceleration if: + +- you not using [FFmpeg source](https://github.com/AlexxIT/go2rtc#source-ffmpeg) +- you using only `#video=copy` for FFmpeg source +- you using only `#audio=...` (any audio) transcoding for FFmpeg source + +You **NEED** hardware acceleration if you using `#video=h264`, `#video=h265`, `#video=mjpeg` (video) transcoding. + +## Important + +- Acceleration is disabled by default because it can be unstable (it can be changed in future) +- go2rtc can automatically detect supported hardware acceleration if enabled +- go2rtc will enable hardware decoding only if hardware encoding supported +- go2rtc will use the same GPU for decoder and encoder +- Intel and AMD will switch to software decoder if input codec is not supported with hardware decoder +- NVidia will fail if input codec is not supported with hardware decoder +- Raspberry always uses software decoder + +```yaml +streams: + # auto select hardware encoder + camera1_hw: ffmpeg:rtsp://rtsp:12345678@192.168.1.123/av_stream/ch0#video=h264#hardware + + # manual select hardware encoder (vaapi, cuda, v4l2m2m, dxva2, videotoolbox) + camera1_vaapi: ffmpeg:rtsp://rtsp:12345678@192.168.1.123/av_stream/ch0#video=h264#hardware=vaapi +``` + +## Docker and Hass Addon + +There are two versions of the Docker container and Hass Add-on: + +- Latest (alpine) support hardware acceleration for Intel iGPU (CPU with Graphics) and Raspberry. +- Hardware (debian 12) support Intel iGPU, AMD GPU, NVidia GPU. + +## Intel iGPU + +**Supported on:** Windows binary, Linux binary, Docker, Hass Addon. + +If you have Intel CPU Sandy Bridge (2011) with Graphics, you already have support hardware decoding/encoding for `AVC/H.264`. + +If you have Intel CPU Skylake (2015) with Graphics, you already have support hardware decoding/encoding for `AVC/H.264`, `HEVC/H.265` and `MJPEG`. + +Read more [here](https://en.wikipedia.org/wiki/Intel_Quick_Sync_Video#Hardware_decoding_and_encoding) and [here](https://en.wikipedia.org/wiki/Intel_Graphics_Technology#Capabilities_(GPU_video_acceleration)). + +Linux and Docker: + +- It may be important to have the latest version of the OS with the latest version of the Linux kernel. For example, on my **Debian 10 (kernel 4.19)** it did not work, but after update to **Debian 11 (kernel 5.10)** all was fine. +- In case of troube check you have `/dev/dri/` folder on your host. + +Docker users should add `--privileged` option to container for access to Hardware. + +**PS.** Supported via [VAAPI](https://trac.ffmpeg.org/wiki/Hardware/VAAPI) engine on Linux and [DXVA2+QSV](https://trac.ffmpeg.org/wiki/Hardware/QuickSync) engine on Windows. + +## AMD GPU + +*I don't have the hardware for test support!!!* + +**Supported on:** Linux binary, Docker, Hass Addon. + +Docker users should install: `alexxit/go2rtc:master-hardware`. Docker users should add `--privileged` option to container for access to Hardware. + +Hass Addon users should install **go2rtc master hardware** version. + +**PS.** Supported via [VAAPI](https://trac.ffmpeg.org/wiki/Hardware/VAAPI) engine. + +## NVidia GPU + +**Supported on:** Windows binary, Linux binary, Docker. + +Docker users should install: `alexxit/go2rtc:master-hardware`. + +Read more [here](https://docs.frigate.video/configuration/hardware_acceleration) and [here](https://jellyfin.org/docs/general/administration/hardware-acceleration/#nvidia-hardware-acceleration-on-docker-linux). + +**PS.** Supported via [CUDA](https://trac.ffmpeg.org/wiki/HWAccelIntro#CUDANVENCNVDEC) engine. + +## Raspberry Pi 3 + +**Supported on:** Linux binary, Docker, Hass Addon. + +I don't recommend using transcoding on the Raspberry Pi 3. It's extreamly slow, even with hardware acceleration. Also it may fail when transcoding 2K+ stream. + +## Raspberry Pi 4 + +*I don't have the hardware for test support!!!* + +**Supported on:** Linux binary, Docker, Hass Addon. + +**PS.** Supported via [v4l2m2m](https://lalitm.com/hw-encoding-raspi/) engine. + +## macOS + +In my tests, transcoding is faster on the M1 CPU than on the M1 GPU. Transcoding time on M1 CPU better than any Intel iGPU and comparable to NVidia RTX 2070. + +**PS.** Supported via [videotoolbox](https://trac.ffmpeg.org/wiki/HWAccelIntro#VideoToolbox) engine. + +## Rockchip + +- Important to use custom FFmpeg with Rockchip support from [@nyanmisaka](https://github.com/nyanmisaka/ffmpeg-rockchip) + - Static binaries from [@MarcA711](https://github.com/MarcA711/Rockchip-FFmpeg-Builds/releases/) +- Important to have Linux kernel 5.10 or 6.1 + +**Tested** + +- [Orange Pi 3B](https://www.armbian.com/orangepi3b/) with Armbian 6.1, support transcoding H264, H265, MJPEG diff --git a/internal/ffmpeg/hardware/hardware.go b/internal/ffmpeg/hardware/hardware.go index 39ce3323..80166890 100644 --- a/internal/ffmpeg/hardware/hardware.go +++ b/internal/ffmpeg/hardware/hardware.go @@ -128,19 +128,32 @@ func MakeHardware(args *ffmpeg.Args, engine string, defaults map[string]string) case EngineRKMPP: args.Codecs[i] = defaults[name+"/"+engine] - for j, filter := range args.Filters { - if strings.HasPrefix(filter, "scale=") { - args.Filters = append(args.Filters[:j], args.Filters[j+1:]...) + if !args.HasFilters("drawtext=") { + args.Input = "-hwaccel rkmpp -hwaccel_output_format drm_prime -afbc rga " + args.Input - width, height, _ := strings.Cut(filter[6:], ":") - if width != "-1" { - args.Codecs[i] += " -width " + width + for i, filter := range args.Filters { + if strings.HasPrefix(filter, "scale=") { + args.Filters[i] = "scale_rkrga=" + filter[6:] + ":force_original_aspect_ratio=0" } - if height != "-1" { - args.Codecs[i] += " -height " + height + if strings.HasPrefix(filter, "transpose=") { + if filter == "transpose=1,transpose=1" { // 180 degrees half-turn + args.Filters[i] = "vpp_rkrga=transpose=4" // reversal + } else { + args.Filters[i] = "vpp_rkrga=transpose=" + filter[10:] + } } - break } + + if len(args.Filters) > 0 { + // fix if input doesn't support hwaccel, do nothing when support + // insert as first filter before hardware scale and transpose + args.InsertFilter("format=drm_prime|nv12,hwupload") + } + } else { + // enable software pixel for drawtext, scale and transpose + args.Input = "-hwaccel rkmpp -hwaccel_output_format nv12 -afbc rga " + args.Input + + args.AddFilter("hwupload") } } } diff --git a/internal/ffmpeg/hardware/hardware_unix.go b/internal/ffmpeg/hardware/hardware_unix.go index 4f688ce4..e8000e17 100644 --- a/internal/ffmpeg/hardware/hardware_unix.go +++ b/internal/ffmpeg/hardware/hardware_unix.go @@ -11,8 +11,9 @@ import ( const ( ProbeV4L2M2MH264 = "-f lavfi -i testsrc2 -t 1 -c h264_v4l2m2m -f null -" ProbeV4L2M2MH265 = "-f lavfi -i testsrc2 -t 1 -c hevc_v4l2m2m -f null -" - ProbeRKMPPH264 = "-f lavfi -i testsrc2 -t 1 -c h264_rkmpp_encoder -f null -" - ProbeRKMPPH265 = "-f lavfi -i testsrc2 -t 1 -c hevc_rkmpp_encoder -f null -" + ProbeRKMPPH264 = "-f lavfi -i testsrc2 -t 1 -c h264_rkmpp -f null -" + ProbeRKMPPH265 = "-f lavfi -i testsrc2 -t 1 -c hevc_rkmpp -f null -" + ProbeRKMPPJPEG = "-f lavfi -i testsrc2 -t 1 -c mjpeg_rkmpp -f null -" ProbeVAAPIH264 = "-init_hw_device vaapi -f lavfi -i testsrc2 -t 1 -vf format=nv12,hwupload -c h264_vaapi -f null -" ProbeVAAPIH265 = "-init_hw_device vaapi -f lavfi -i testsrc2 -t 1 -vf format=nv12,hwupload -c hevc_vaapi -f null -" ProbeVAAPIJPEG = "-init_hw_device vaapi -f lavfi -i testsrc2 -t 1 -vf format=nv12,hwupload -c mjpeg_vaapi -f null -" @@ -39,6 +40,10 @@ func ProbeAll(bin string) []*api.Source { Name: runToString(bin, ProbeRKMPPH265), URL: "ffmpeg:...#video=h265#hardware=" + EngineRKMPP, }, + { + Name: runToString(bin, ProbeRKMPPJPEG), + URL: "ffmpeg:...#video=mjpeg#hardware=" + EngineRKMPP, + }, } } @@ -83,6 +88,10 @@ func ProbeHardware(bin, name string) string { if run(bin, ProbeRKMPPH265) { return EngineRKMPP } + case "mjpeg": + if run(bin, ProbeRKMPPJPEG) { + return EngineRKMPP + } } return EngineSoftware