FFmpeg ile AMD Radeon'da Yazılımsal Decode ve Donanımsal H.264 Encode Kullanımı

Eski veya egzotik bir giriş codec’ini AMD Radeon’da doğrudan h264_vaapi ile transcode etmeye çalıştığında, FFmpeg onu decode etmek için de GPU’yu kullanmayı deneyebilir. VAAPI sürücüsü giriş codec’ini desteklemiyorsa, filter graph başarısız olur ve hiçbir şey yazılmaz. Bu yazı, decode’u CPU’da tutarken H.264 encode için hâlâ GPU’yu nasıl kullanacağını gösterir.

Sorun

Böyle bir komut MPEG-4 girdisi için başarısız olur:

h264_vaapi_fail.sh
LIBVA_DRIVER_NAME=radeonsi ffmpeg \
  -hwaccel vaapi -hwaccel_device /dev/dri/renderD129 -hwaccel_output_format vaapi \
  -i input.avi \
  -c:v h264_vaapi -rc_mode CQP -global_quality:v 24 \
  -c:a copy \
  output-h264.mp4

Hata çıktısı şöyle görünür:

h264_vaapi_error.txt
[mpeg4 @ 0x5a0c496f8a00] No support for codec mpeg4 profile -99.
[mpeg4 @ 0x5a0c496f8a00] Failed setup for format vaapi: hwaccel initialisation returned error.
[mpeg4 @ 0x5a0c496fb800] Video uses a non-standard and wasteful way to store B-frames ('packed B-frames'). Consider using the mpeg4_unpack_bframes bitstream filter without encoding but stream copy to fix it.
Impossible to convert between the formats supported by the filter 'Parsed_null_0' and the filter 'auto_scale_0'
Link 'Parsed_null_0.default' -> 'auto_scale_0.default':
  src: yuv420p
  dst: yuv420p
[vf#0:0 @ 0x5a0c494a8780] Error reinitializing filters!
[vf#0:0 @ 0x5a0c494a8780] Task finished with error code: -38 (Function not implemented)
[vf#0:0 @ 0x5a0c494a8780] Terminating thread with return code -38 (Function not implemented)
[vost#0:0/h264_vaapi @ 0x5a0c494a7f80] [enc:h264_vaapi @ 0x5a0c494a8340] Could not open encoder before EOF
[vost#0:0/h264_vaapi @ 0x5a0c494a7f80] Task finished with error code: -22 (Invalid argument)
[vost#0:0/h264_vaapi @ 0x5a0c494a7f80] Terminating thread with return code -22 (Invalid argument)
[out#0/mp4 @ 0x5a0c49504640] Nothing was written into output file, because at least one of its streams received no packets.

Kanıt No support for codec mpeg4 profile ... ve Failed setup for format vaapi: hwaccel initialisation returned error satırlarıdır. Decoder donanımla hızlandırılamıyor, bu yüzden tüm pipeline çöküyor.

Çözüm: yazılımla decode et, sonra VAAPI’ye yükle

-hwaccel‘i kaldır, VAAPI cihazını açıkça oluştur, yazılımla decode et, frame’leri nv12‘ye dönüştür ve h264_vaapi devralmadan önce GPU’ya yükle:

h264_software_decode_hw_encode.sh
export RENDER_DEVICE=/dev/dri/renderD128

LIBVA_DRIVER_NAME=radeonsi ffmpeg \
  -init_hw_device vaapi=va:"$RENDER_DEVICE" \
  -filter_hw_device va \
  -i input.avi \
  -vf 'format=nv12,hwupload' \
  -c:v h264_vaapi -rc_mode CQP -global_quality:v 15 \
  -c:a copy \
  output-h264.mp4

Flag’ler ne yapar

  • -init_hw_device vaapi=va:/dev/dri/renderD128 Radeon render node’unda va adlı bir VAAPI cihazı oluşturur.
  • -filter_hw_device va filter graph’a bu cihazı donanım filtreleri ve encoder için kullanmasını söyler.
  • -i input.avi artık CPU tarafından decode edilir çünkü -hwaccel ayarlanmamıştır.
  • -vf 'format=nv12,hwupload' yazılımsal yuv420p frame’lerini VAAPI-uyumlu nv12 formatına dönüştürür ve GPU belleğine yükler.
  • -c:v h264_vaapi yüklenen frame’leri donanımda encode eder.
  • -rc_mode CQP -global_quality:v 15 CQP ve düşük bir kalite değeri kullanır çünkü h264_vaapi zaten upload overhead’ini taşımak zorundadır; bu kullanım durumu için sonuç genellikle yüksek kalitedir.
  • -c:a copy audio’yu yeniden encode etmeden kopyalar.

Wrapper script

Bunu transcode-h264-software-decode-radeon.sh olarak kaydet ve herhangi bir girdiyle çalıştır. Mevcut dizinde output-h264.mp4 yazar:

transcode-h264-software-decode-radeon.sh
#!/bin/bash
set -euo pipefail

RENDER_DEVICE="${RENDER_DEVICE:-/dev/dri/renderD128}"

if [ $# -ne 2 ]; then
    echo "Usage: $0 <input video> <output-h264.mp4>" >&2
    exit 1
fi

input="$1"
output="$2"

LIBVA_DRIVER_NAME=radeonsi ffmpeg \
  -init_hw_device vaapi=va:"$RENDER_DEVICE" \
  -filter_hw_device va \
  -i "$input" \
  -vf 'format=nv12,hwupload' \
  -c:v h264_vaapi -rc_mode CQP -global_quality:v 15 \
  -c:a copy \
  "$output"

Örnek:

example_usage_software_decode.sh
chmod +x transcode-h264-software-decode-radeon.sh
RENDER_DEVICE=/dev/dri/renderD129 ./transcode-h264-software-decode-radeon.sh input.avi output-h264.mp4

Bu bilgisayarda doğrulama

MPEG-4 AVI için başarısız olan komut şunu üretti:

h264_vaapi_fail_output.txt
[mpeg4 @ ...] No support for codec mpeg4 profile 0.
[mpeg4 @ ...] Failed setup for format vaapi: hwaccel initialisation returned error.
...
[out#0/mp4 @ ...] Nothing was written into output file, because at least one of its streams received no packets.
Conversion failed!

Yazılımsal-decode / donanımsal-encode komutu başarıyla tamamlandı:

h264_software_decode_output.txt
Stream mapping:
  Stream #0:0 -> #0:0 (mpeg4 (native) -> h264 (h264_vaapi))
  Stream #0:1 -> #0:1 (copy)
Output #0, mp4, to 'testsrc_mpeg4_h264.mp4':
  Stream #0:0: Video: h264 (High) (avc1 / 0x31637661), vaapi(tv, progressive), 1920x1080, q=2-31, 30 fps
  Stream #0:1: Audio: aac (LC), 44100 Hz, mono, fltp, 69 kb/s
[out#0/mp4] video:590KiB audio:43KiB ... bitrate= 1046.8kbits/s speed=7.75x

Çıktı üzerinde ffprobe, h264_vaapi tarafından üretilen codec_name=h264 ve profile=High‘ı doğruladı.

Bu bilgisayarda /dev/dri/renderD128 entegre Intel GPU’suna ait olduğundan Radeon kart aslında /dev/dri/renderD129. Her zaman vainfo‘nun Radeon kartın olarak rapor ettiği cihazı kullan.

İlgili yazılar


Check out similar posts by category: Video Linux