diff --git a/README.md b/README.md index ab62b07..7074592 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,8 @@ PC hardware decoding supports NVIDIA, with experimental AMD support on Mesa 26.1+. - **Hardware decoding** on the Steam Frame: 8K 60 fps HEVC (8-bit and 10-bit) - and H.264 without dropped frames, tested up to 150 Mbit/s over SMB. + and H.264 without dropped frames, tested up to 150 Mbit/s over SMB. AV1 + (8-bit) needs SteamOS 0.5.10+. - **Tuned for the Steam Frame**: video goes straight from the hardware decoder to the GPU without being copied through the CPU, leaving the GPU headroom to spare and keeping power, heat and fan noise down. diff --git a/packaging/frame/ffmpeg-v4l2.patch b/packaging/frame/ffmpeg-v4l2.patch index b4305d1..d2ae59a 100644 --- a/packaging/frame/ffmpeg-v4l2.patch +++ b/packaging/frame/ffmpeg-v4l2.patch @@ -1,8 +1,18 @@ # Build the Frame's V4L2 decoder (packaging/frame/ffmpeg/, copied over FFmpeg's own) # and refuse to map Qualcomm compressed frames to the CPU. Apply to FFmpeg 9.0.2. +--- a/configure ++++ b/configure +@@ -3727,6 +3727,7 @@ vp9_vaapi_encoder_select="vaapi_encode" + vp9_qsv_encoder_deps="libmfx MFX_CODEC_VP9" + vp9_qsv_encoder_select="qsvenc" + vp9_v4l2m2m_decoder_deps="v4l2_m2m vp9_v4l2_m2m" ++av1_v4l2m2m_decoder_deps="v4l2_m2m" + amf_capture_filter_deps="amf" + vvc_qsv_decoder_select="vvc_mp4toannexb_bsf qsvdec" + --- a/libavcodec/Makefile +++ b/libavcodec/Makefile -@@ -184,7 +184,8 @@ +@@ -184,7 +184,8 @@ OBJS-$(CONFIG_VC1DSP) + OBJS-$(CONFIG_VIDEODSP) += videodsp.o OBJS-$(CONFIG_VP3DSP) += vp3dsp.o OBJS-$(CONFIG_VP8DSP) += vp8dsp.o @@ -12,6 +22,24 @@ OBJS-$(CONFIG_WMA_FREQS) += wma_freqs.o OBJS-$(CONFIG_WMV2DSP) += wmv2dsp.o +@@ -829,6 +830,7 @@ OBJS-$(CONFIG_VP9_VAAPI_ENCODER) + + OBJS-$(CONFIG_VP9_QSV_ENCODER) += qsvenc_vp9.o + OBJS-$(CONFIG_VPLAYER_DECODER) += textdec.o ass.o + OBJS-$(CONFIG_VP9_V4L2M2M_DECODER) += v4l2_m2m_dec.o ++OBJS-$(CONFIG_AV1_V4L2M2M_DECODER) += v4l2_m2m_dec.o + OBJS-$(CONFIG_VQA_DECODER) += vqavideo.o + OBJS-$(CONFIG_VQC_DECODER) += vqcdec.o + OBJS-$(CONFIG_VVC_DECODER) += executor.o h2645data.o +--- a/libavcodec/allcodecs.c ++++ b/libavcodec/allcodecs.c +@@ -847,6 +847,7 @@ extern const FFCodec ff_h263_v4l2m2m_enc + extern const FFCodec ff_libaom_av1_decoder; + /* hwaccel hooks only, so prefer external decoders */ + extern const FFCodec ff_av1_decoder; ++extern const FFCodec ff_av1_v4l2m2m_decoder; + extern const FFCodec ff_av1_cuvid_decoder; + extern const FFCodec ff_av1_d3d12va_encoder; + extern const FFCodec ff_av1_mediacodec_decoder; --- a/libavutil/hwcontext_drm.c +++ b/libavutil/hwcontext_drm.c @@ -31,6 +31,7 @@ @@ -22,7 +50,7 @@ #include #include "avassert.h" -@@ -130,6 +131,13 @@ +@@ -130,6 +131,13 @@ static int drm_map_frame(AVHWFramesConte int mmap_prot; void *addr; diff --git a/packaging/frame/ffmpeg/libavcodec/v4l2_fmt.c b/packaging/frame/ffmpeg/libavcodec/v4l2_fmt.c index 52d104b..9503037 100644 --- a/packaging/frame/ffmpeg/libavcodec/v4l2_fmt.c +++ b/packaging/frame/ffmpeg/libavcodec/v4l2_fmt.c @@ -118,6 +118,8 @@ static const struct fmt_conversion { #ifdef V4L2_PIX_FMT_VP9 { AV_FMT(NONE), AV_CODEC(VP9), V4L2_FMT(VP9) }, #endif + // V4L2_PIX_FMT_AV1, an AV1 temporal unit per buffer (Iris since SteamOS 0.5.10). + { AV_FMT(NONE), AV_CODEC(AV1), v4l2_fourcc('A','V','0','1') }, #ifdef V4L2_PIX_FMT_HEVC { AV_FMT(NONE), AV_CODEC(HEVC), V4L2_FMT(HEVC) }, #endif diff --git a/packaging/frame/ffmpeg/libavcodec/v4l2_m2m_dec.c b/packaging/frame/ffmpeg/libavcodec/v4l2_m2m_dec.c index 2475e4f..234db7b 100644 --- a/packaging/frame/ffmpeg/libavcodec/v4l2_m2m_dec.c +++ b/packaging/frame/ffmpeg/libavcodec/v4l2_m2m_dec.c @@ -250,6 +250,28 @@ h264_xd_copy(const uint8_t * const extradata, const int extrasize, uint8_t * abd return d - abdata; } +// The decoder takes AV1 a temporal unit per buffer, which opens with a +// temporal delimiter. MP4 and Matroska samples leave it out. +static const uint8_t av1_temporal_delimiter[2] = {0x12, 0x00}; + +// The size of the temporal delimiter OBU a packet opens with, or 0. +static int av1_leading_delimiter(const uint8_t *p, const int size) +{ + int n; + if (size < 2 || ((p[0] >> 3) & 15) != 2 || !(p[0] & 2)) + return 0; + n = 1 + !!(p[0] & 4); + // Its size field: zero, in up to eight LEB128 bytes. + for (int i = 0; i < 8 && n < size; i++) { + const uint8_t b = p[n++]; + if (b & 0x7f) + return 0; + if (!(b & 0x80)) + return n; + } + return 0; +} + static int copy_extradata(AVCodecContext * const avctx, const void * const src_data, const int src_len, @@ -260,6 +282,30 @@ copy_extradata(AVCodecContext * const avctx, *pdst_len = 0; av_freep(pdst_data); + // AV1's comes as an av1C record, four bytes of its own before the + // sequence header OBUs, or as the OBUs alone. Send them after a + // delimiter, so they open the first temporal unit. + if (avctx->codec_id == AV_CODEC_ID_AV1) { + const uint8_t *obus = src_data; + len = src_len; + if (len > 0 && (obus[0] & 0x80)) { + if (len < 4 || obus[0] != 0x81) + return AVERROR_INVALIDDATA; + obus += 4; + len -= 4; + } + if (len < 0) + return AVERROR(EINVAL); + if ((*pdst_data = av_malloc(len + sizeof(av1_temporal_delimiter) + + AV_INPUT_BUFFER_PADDING_SIZE)) == NULL) + return AVERROR(ENOMEM); + memcpy(*pdst_data, av1_temporal_delimiter, sizeof(av1_temporal_delimiter)); + if (len) + memcpy((uint8_t *)*pdst_data + sizeof(av1_temporal_delimiter), obus, len); + *pdst_len = len + sizeof(av1_temporal_delimiter); + return 0; + } + if (avctx->codec_id == AV_CODEC_ID_H264) len = h264_xd_copy(src_data, src_len, NULL); else @@ -627,6 +673,7 @@ static int try_enqueue_src(AVCodecContext * const avctx, V4L2m2mContext * const } // Retry a side-data only pkt + av_packet_unref(&s->buf_pkt); } // If i >= 256 something has gone wrong if (i >= 256) { @@ -708,10 +755,24 @@ static int try_enqueue_src(AVCodecContext * const avctx, V4L2m2mContext * const return ret; } } - if (s->extdata_sent) - ret = ff_v4l2_context_enqueue_packet(&s->output, &s->buf_pkt, NULL, 0); - else - ret = ff_v4l2_context_enqueue_packet(&s->output, &s->buf_pkt, s->extdata_data, s->extdata_size); + { + const void *prefix = s->extdata_sent ? NULL : s->extdata_data; + size_t prefix_size = s->extdata_sent ? 0 : s->extdata_size; + if (avctx->codec_id == AV_CODEC_ID_AV1) { + // An empty packet would ask the decoder to drain: a lone + // delimiter goes as it is, after the sequence header if that's due. + const int delimiter = av1_leading_delimiter(s->buf_pkt.data, s->buf_pkt.size); + if (delimiter && delimiter < s->buf_pkt.size) { + s->buf_pkt.data += delimiter; + s->buf_pkt.size -= delimiter; + } + if (!prefix_size && delimiter != s->buf_pkt.size) { + prefix = av1_temporal_delimiter; + prefix_size = sizeof(av1_temporal_delimiter); + } + } + ret = ff_v4l2_context_enqueue_packet(&s->output, &s->buf_pkt, prefix, prefix_size); + } if (ret == AVERROR(EAGAIN)) { // Out of input buffers - keep packet @@ -1609,11 +1670,16 @@ static av_cold int v4l2_decode_init(AVCodecContext *avctx) const AVPixFmtDescriptor *source = av_pix_fmt_desc_get(priv->source_pix_fmt); priv->capture_10bit = source && source->comp[0].depth == 10 && avctx->codec_id == AV_CODEC_ID_HEVC; + // AV1 of unknown format needs its av1C to say Main, 8-bit 4:2:0. + const uint8_t *av1c = avctx->extradata; + const int av1_8bit = avctx->extradata_size >= 4 && av1c[0] == 0x81 && + (av1c[1] >> 5) == 0 && (av1c[2] & 0x7c) == 0x0c; if ((source && (source->nb_components != 3 || source->log2_chroma_w != 1 || source->log2_chroma_h != 1 || (source->comp[0].depth != 8 && !priv->capture_10bit))) || (!source && avctx->codec_id == AV_CODEC_ID_HEVC && - avctx->profile == AV_PROFILE_HEVC_MAIN_10)) { + avctx->profile == AV_PROFILE_HEVC_MAIN_10) || + (!source && avctx->codec_id == AV_CODEC_ID_AV1 && !av1_8bit)) { av_log(avctx, AV_LOG_ERROR, "Unsupported Iris source bit depth or chroma\n"); return AVERROR(ENOSYS); } @@ -1936,3 +2002,4 @@ M2MDEC(h263, "H.263", AV_CODEC_ID_H263, NULL); M2MDEC(vc1 , "VC1", AV_CODEC_ID_VC1, NULL); M2MDEC(vp8, "VP8", AV_CODEC_ID_VP8, NULL); M2MDEC(vp9, "VP9", AV_CODEC_ID_VP9, NULL); +M2MDEC(av1, "AV1", AV_CODEC_ID_AV1, NULL); diff --git a/packaging/frame/mpv-v4l2-av1.patch b/packaging/frame/mpv-v4l2-av1.patch new file mode 100644 index 0000000..dac6f2b --- /dev/null +++ b/packaging/frame/mpv-v4l2-av1.patch @@ -0,0 +1,60 @@ +# Frame: AV1 on the Iris decoder that offers it (SteamOS 0.5.10): its capture pool, +# and software decoding when the hardware refuses a file. +--- a/video/decode/vd_lavc.c ++++ b/video/decode/vd_lavc.c +@@ -63,12 +63,13 @@ void frame_ubwc_decoder_closed(void); + // Frame: the smallest picture area, by codec and bit depth, whose capture + // pool the upstream Iris driver refused. Later sessions at least that large + // take the smallest pool at once, so a seek or the next file doesn't fail first. +-static _Atomic int64_t v4l2_refused_area[2][2] = {{INT64_MAX, INT64_MAX}, {INT64_MAX, INT64_MAX}}; ++static _Atomic int64_t v4l2_refused_area[3][2] = {{INT64_MAX, INT64_MAX}, {INT64_MAX, INT64_MAX}, ++ {INT64_MAX, INT64_MAX}}; + + static _Atomic int64_t *v4l2_refused(const AVCodecParameters *par) + { + const AVPixFmtDescriptor *source = av_pix_fmt_desc_get(par->format); +- return &v4l2_refused_area[par->codec_id == AV_CODEC_ID_HEVC] ++ return &v4l2_refused_area[par->codec_id == AV_CODEC_ID_AV1 ? 2 : par->codec_id == AV_CODEC_ID_HEVC] + [source && source->comp[0].depth > 8]; + } + +@@ -703,7 +704,9 @@ static void select_and_set_hwdec(struct + } else { + const AVCodecParameters *par = ctx->codec->lav_codecpar; + bool large_v4l2 = false; +- if (par && (int64_t)par->width * par->height >= 7680LL * 3840) { ++ // AV1 still falls back: software plays it, if not smoothly. ++ if (par && par->codec_id != AV_CODEC_ID_AV1 && ++ (int64_t)par->width * par->height >= 7680LL * 3840) { + for (int n = 0; n < ctx->num_attempted_hwdecs; n++) { + if (bstr_find0(ctx->attempted_hwdecs[n], "v4l2m2m") >= 0) + large_v4l2 = true; +@@ -890,12 +893,17 @@ static void init_avctx(struct mp_filter + spare += 3; + av_opt_set_int(avctx->priv_data, "capture_spare", spare, 0); + } ++ // AV1 shows its frames in decoding order: three spare, as for 8-bit HEVC ++ // that doesn't reorder. ++ if (wrapper && strcmp(wrapper, "v4l2m2m") == 0 && avctx->codec_id == AV_CODEC_ID_AV1) ++ av_opt_set_int(avctx->priv_data, "capture_spare", 3, 0); + // The upstream Iris driver (SteamOS 0.5.10) keeps its reference frames + // apart, and fits them, its working memory and the pool in one address + // range: 8K HEVC Main 10 starts with at most one frame beyond its minimum. + // Once it refused a pool, a stream that size takes that one. + if (wrapper && strcmp(wrapper, "v4l2m2m") == 0 && c->lav_codecpar && +- (avctx->codec_id == AV_CODEC_ID_HEVC || avctx->codec_id == AV_CODEC_ID_H264)) { ++ (avctx->codec_id == AV_CODEC_ID_HEVC || avctx->codec_id == AV_CODEC_ID_H264 || ++ avctx->codec_id == AV_CODEC_ID_AV1)) { + const AVCodecParameters *par = c->lav_codecpar; + ctx->v4l2_smallest_pool = (int64_t)par->width * par->height >= atomic_load(v4l2_refused(par)); + if (ctx->v4l2_smallest_pool) +@@ -1377,7 +1385,8 @@ static void handle_err(struct mp_filter + const AVCodecParameters *par = ctx->codec->lav_codecpar; + int64_t refused = 0; + bool smaller = false; +- if (par && (par->codec_id == AV_CODEC_ID_HEVC || par->codec_id == AV_CODEC_ID_H264) && ++ if (par && (par->codec_id == AV_CODEC_ID_HEVC || par->codec_id == AV_CODEC_ID_H264 || ++ par->codec_id == AV_CODEC_ID_AV1) && + av_opt_get_int(ctx->avctx->priv_data, "capture_start_refused", 0, &refused) >= 0 && + refused) { + v4l2_note_refused(par); diff --git a/packaging/release/assemble.py b/packaging/release/assemble.py index 395d4ae..74b9f82 100644 --- a/packaging/release/assemble.py +++ b/packaging/release/assemble.py @@ -169,7 +169,7 @@ def assemble(bundle, app, work, sdk, cache, supplement, metadata, source_name): "json": "MIT; compiled-in header library, unmodified", "mpv": ("upstream GPL-2.0-or-later, combined under GPLv3; both builds modified by mpv-mvc.patch and the " "packaging/mpv/mvc sources (3D Blu-ray, GPL-3.0); Frame build also by mpv-v4l2-reset.patch, " - "mpv-v4l2-hardware-only.patch, mpv-v4l2-interrupt.patch, mpv-export-release.patch and dmabuf_ubwc sources"), + "mpv-v4l2-hardware-only.patch, mpv-v4l2-interrupt.patch, mpv-export-release.patch, mpv-v4l2-av1.patch and dmabuf_ubwc sources"), "ffmpeg": ("upstream LGPL-2.1-or-later. Desktop build: unmodified, LGPL-2.1-or-later. " "Frame build: its V4L2 decoder replaced by the packaging/frame/ffmpeg sources, which are FFmpeg's " "decoder as rewritten by John Cox in rpi-ffmpeg (https://github.com/jc-kynesim/rpi-ffmpeg, commit " diff --git a/packaging/release/build-sdk.sh b/packaging/release/build-sdk.sh index f538457..11e1e38 100644 --- a/packaging/release/build-sdk.sh +++ b/packaging/release/build-sdk.sh @@ -128,6 +128,7 @@ if [[ $arch == aarch64 ]]; then patch -d "$mpv_source" -p1 < /src/packaging/frame/mpv-v4l2-hardware-only.patch patch -d "$mpv_source" -p1 < /src/packaging/frame/mpv-v4l2-interrupt.patch patch -d "$mpv_source" -p1 < /src/packaging/frame/mpv-export-release.patch + patch -d "$mpv_source" -p1 < /src/packaging/frame/mpv-v4l2-av1.patch cp /src/packaging/frame/mpv/dmabuf_ubwc.{c,h} /src/src/video_export.h "$mpv_source/video/out/hwdec/" glslangValidator -V --target-env vulkan1.2 --vn dmabuf_ubwc_spv /src/packaging/frame/mpv/dmabuf_ubwc.comp \ -o "$mpv_source/video/out/hwdec/dmabuf_ubwc.spv.h" diff --git a/src/main.cpp b/src/main.cpp index 00cfecd..eb23b57 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3423,6 +3423,7 @@ int run(Options opts) { view.crashedLastRun = previousRunCrashed() && diagnostics.state() != DiagnosticsExport::State::Saved; view.playback = player.playback(); + view.hardwareAv1 = player.hardwareAv1(); if (heldSeek.holding() || heldSeekLanding) { view.playback.position = heldSeekTarget; if (heldSeekPaused) view.playback.paused = false; // only waiting for the stick diff --git a/src/menu.h b/src/menu.h index 76b7b82..5c295c6 100644 --- a/src/menu.h +++ b/src/menu.h @@ -182,6 +182,7 @@ struct MenuView { Theme theme = Theme::Glass; Accent accent; bool floatingSubtitles = false; + bool hardwareAv1 = false; // the headset's decoder takes AV1 (SteamOS 0.5.10) bool streaming = false; // the current video is a link, not a file int videoWidth = 0, videoHeight = 0; bool background = true; // night sky around the flat screen diff --git a/src/menu_player.cpp b/src/menu_player.cpp index 11c8223..e81cc35 100644 --- a/src/menu_player.cpp +++ b/src/menu_player.cpp @@ -20,6 +20,11 @@ std::string upper(std::string s) { return s; } +// The codecs the Frame decodes in hardware: AV1 too where its decoder takes it. +bool hardwareCodec(const std::string& codec, bool av1) { + return codec == "h264" || codec == "hevc" || (av1 && codec == "av1"); +} + } // namespace void Menu::layoutMain() { @@ -109,7 +114,7 @@ void Menu::layoutMain() { if (Button* b = label(t.softwareDecoding.label, [] { return MenuCommand{}; })) { b->danger = true; // The Frame decodes only H.264 and HEVC in hardware. - if (kStandaloneHeadset && p.videoCodec != "h264" && p.videoCodec != "hevc") { + if (kStandaloneHeadset && !hardwareCodec(p.videoCodec, view_.hardwareAv1)) { char reason[256]; std::snprintf(reason, sizeof(reason), t.softwareDecoding.noHardware, upper(p.videoCodec).c_str()); diff --git a/src/video_player.cpp b/src/video_player.cpp index e537c33..0bf61ec 100644 --- a/src/video_player.cpp +++ b/src/video_player.cpp @@ -21,10 +21,16 @@ #include #include #include +#include #include #include #include +#include +#include +#include +#include + namespace { constexpr const char* kBt1886 = "bt.1886"; // mpv's name for SDR video's transfer @@ -40,6 +46,33 @@ constexpr uint64_t kCommandReply = 3; // How long the last file may take to close before a switch counts as failed. constexpr double kCloseTimeout = 10.0; +// Whether the headset's decoder takes AV1: the upstream Iris driver in +// SteamOS 0.5.10 does, the vendor driver before it didn't. Only the +// decoder's node is opened; most others are the cameras'. +bool decoderTakesAv1() { + std::error_code error; + for (const auto& node : std::filesystem::directory_iterator("/sys/class/video4linux", error)) { + std::ifstream nameFile(node.path() / "name"); + std::string name; + if (!std::getline(nameFile, name) || name.find("iris-decoder") == std::string::npos) continue; + const std::string path = "/dev/" + node.path().filename().string(); + const int fd = ::open(path.c_str(), O_RDWR | O_NONBLOCK | O_CLOEXEC); + if (fd < 0) continue; + bool av1 = false; + v4l2_capability cap{}; + if (::ioctl(fd, VIDIOC_QUERYCAP, &cap) == 0 && + std::strcmp(reinterpret_cast(cap.driver), "iris_driver") == 0) { + v4l2_fmtdesc format{}; + format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; + for (; !av1 && ::ioctl(fd, VIDIOC_ENUM_FMT, &format) == 0; ++format.index) + av1 = format.pixelformat == v4l2_fourcc('A', 'V', '0', '1'); + } + ::close(fd); + if (av1) return true; + } + return false; +} + void setProperty(mpv_handle* mpv, const char* name, const std::string& value) { const char* text = value.c_str(); mpv_set_property_async(mpv, 0, name, MPV_FORMAT_STRING, &text); @@ -185,8 +218,10 @@ bool VideoPlayer::init(const std::string& hwdec, GlInterop& interop, const MpvGl mpv_set_option_string(mpv_, "hwdec-software-fallback", "8"); // Iris stalled on a 4K VP9 file without reporting an error, so mpv never // fell back and the picture froze. Software decodes 4K60 VP9 at about - // 170 fps on the Frame's cores; H.264 and HEVC stay on the hardware. - mpv_set_option_string(mpv_, "hwdec-codecs", "h264,hevc"); + // 170 fps on the Frame's cores; H.264 and HEVC stay on the hardware, and + // AV1 where the decoder takes it (8K AV1 plays at about 28 fps in software). + hardwareAv1_ = kStandaloneHeadset && decoderTakesAv1(); + mpv_set_option_string(mpv_, "hwdec-codecs", hardwareAv1_ ? "h264,hevc,av1" : "h264,hevc"); } if (hwdec.rfind("nvdec", 0) == 0) { // Spare decoder frames beyond the video's reference frames, each a full diff --git a/src/video_player.h b/src/video_player.h index be0e04c..4ced1b7 100644 --- a/src/video_player.h +++ b/src/video_player.h @@ -163,6 +163,8 @@ class VideoPlayer { // texture still holds the previous video meanwhile. bool loading() const { return loading_; } bool failed() const { return failed_; } + // The headset's decoder takes AV1 (SteamOS 0.5.10 and later). + bool hardwareAv1() const { return hardwareAv1_; } // Whether width() and height() (and playback().views) are this file's, // not still the last one's while it opens. bool sized() const { @@ -253,6 +255,7 @@ class VideoPlayer { std::string url, title, lang; }; std::future> sidecarSubtitles_; + bool hardwareAv1_ = false; // hwdec-codecs includes AV1 std::atomic remoteFailed_{false}; // a remote file's read, or Stash's stream bool hardwareRefused_ = false; // the headset's decoder can't start the video bool logReachedLoad_ = false; // mpv's log has listed the last loaded file's tracks