amf-rs

May 15, 2026 · View on GitHub

crates.io docs.rs License GitHub Actions Discord

About Shiguredo's open source software

We will not respond to PRs or issues that have not been discussed on Discord. Also, Discord is only available in Japanese.

Please read https://github.com/shiguredo/oss before use.

時雨堂のオープンソースソフトウェアについて

利用前に https://github.com/shiguredo/oss をお読みください。

概要

AMD AMF (Advanced Media Framework) を利用したハードウェアビデオエンコーダーおよびデコーダーの Rust バインディングです。

AMF ランタイムライブラリ (libamfrt64.so.1) は AMD GPU ドライバーに同梱されており、dlopen で動的にロードするため、ビルド時のリンクは不要です。

特徴

  • AMF によるハードウェアエンコード (H.264 / H.265 / AV1)
  • AMF によるハードウェアデコード (H.264 / H.265 / AV1)
  • AMF ランタイムライブラリの実行時動的ロード (ビルド時のリンク不要)
  • Vulkan バックエンドによる GPU 処理
  • エンコード入力フォーマット選択 (NV12 / YV12 / I420 / BGRA / ARGB / RGBA / YUY2 / UYVY / P010 / P012 / P016 / Y210 / AYUV / Y410 / Y416)
  • デコード出力は NV12 フォーマット
  • フレーム単位のエンコードオプション (IDR フレーム強制)
  • エンコード中の動的プロパティ再設定 (reconfigure)
  • CQP / CBR / VBR / LCVBR / QVBR / HQVBR / HQCBR レート制御モード
  • ビルド時に GitHub から AMF ヘッダーを自動取得

動作要件

  • Linux (x86_64)
  • AMD GPU (RDNA 以降推奨)
  • AMD GPU ドライバー (AMF ランタイムライブラリを含む)
  • Vulkan ドライバー
  • ビルド時: git

ビルド

cargo build

ビルド時に GitHub から AMF ヘッダーを自動取得します。

docs.rs 向けビルド

AMF ランタイムがない環境では、docs.rs 向けのドキュメント生成のみ可能です。

DOCS_RS=1 cargo doc --no-deps

使い方

エンコード

use std::sync::{Arc, Mutex};

use shiguredo_amf::{
    CodecConfig, EncodeOptions, EncodedFrame, Encoder, EncoderConfig, FrameFormat,
    H264EncoderConfig, H264Profile, RateControlMode, ReconfigureParams, frame_type,
};

let mut config = EncoderConfig::new(
    CodecConfig::H264(H264EncoderConfig {
        profile: Some(H264Profile::High),
    }),
    1920,                    // width
    1080,                    // height
    FrameFormat::Nv12,
    30,                      // framerate_num
    1,                       // framerate_den
    RateControlMode::Cbr,
);
config.target_kbps = Some(5_000);

let encoded = Arc::new(Mutex::new(Vec::new()));
let e = encoded.clone();
let mut encoder = Encoder::new(config, move |frame: EncodedFrame<()>| {
    e.lock().unwrap().push(frame);
})?;

// エンコード中に動的プロパティを再設定
encoder.reconfigure(ReconfigureParams {
    framerate_num: Some(15),
    framerate_den: Some(1),
    target_kbps: Some(3_000),
    ..ReconfigureParams::default()
})?;

// Surface を確保してフレームデータをコピーし、エンコードする
let surface = encoder.alloc_surface()?;
// フレームデータを surface の Y/UV プレーンにコピーする
// ...
let options = EncodeOptions { frame_type: frame_type::UNKNOWN };
encoder.encode(surface, &options, ())?;

// IDR フレームを強制してエンコード
let surface = encoder.alloc_surface()?;
// ...
encoder.encode(surface, &EncodeOptions {
    frame_type: frame_type::IDR | frame_type::I | frame_type::REF,
}, ())?;

// 残りのフレームをすべて取得する
encoder.finish()?;

// エンコード済みフレームを確認
for encoded in encoded.lock().unwrap().iter() {
    println!("encoded bytes: {}", encoded.buffer().get_size());
    println!("pts: {}", encoded.buffer().get_pts());
    println!("picture type: {:?}", encoded.picture_type());
}

reconfigure で変更できる項目は codec ごとに異なります。

  • H.264: framerate_num / framerate_den / target_kbps / max_kbps / qpi / qpp / qpb / gop_pic_size
  • H.265: framerate_num / framerate_den / target_kbps / max_kbps / qpi / qpp
  • AV1: framerate_num / framerate_den / target_kbps / max_kbps / qpi / qpp / qpb / gop_pic_size

framerate_numframerate_den は必ず同時に指定してください。

デコード

use std::sync::{Arc, Mutex};

use shiguredo_amf::{DecodedFrame, Decoder, DecoderCodec, DecoderConfig};

let config = DecoderConfig {
    codec: DecoderCodec::H264,
};
let decoded = Arc::new(Mutex::new(Vec::new()));
let d = decoded.clone();
let mut decoder = Decoder::new(config, move |frame: DecodedFrame<()>| {
    d.lock().unwrap().push(frame);
})?;

// Buffer を確保してビットストリームデータをコピーし、デコードする
let buffer = decoder.alloc_buffer(bitstream_data.len())?;
// bitstream データを buffer にコピーする
// ...
decoder.decode(buffer, ())?;

// 残りのフレームをすべて取得する
decoder.finish()?;
drop(decoder);

// デコード済みフレームを確認
for frame in decoded.lock().unwrap().iter() {
    let y_plane = frame.surface().get_plane(shiguredo_amf::ffi::AMF_PLANE_TYPE::AMF_PLANE_Y).unwrap();
    println!("decoded: {}x{}", y_plane.get_width(), y_plane.get_height());
}

コーデック対応状況の確認

use shiguredo_amf::supported_codecs;

let codecs = supported_codecs();
for info in &codecs {
    println!("{:?}:", info.codec);
    println!("  decoding: supported={}, hw_accel={}",
        info.decoding.supported, info.decoding.hardware_accelerated);
    println!("  encoding: supported={}, hw_accel={}",
        info.encoding.supported, info.encoding.hardware_accelerated);
}

AMF ランタイムがロードできない環境では、全コーデックが非対応として返されます。

サポートコーデック

エンコード

コーデックCodecConfig
H.264CodecConfig::H264(H264EncoderConfig)
H.265CodecConfig::Hevc(HevcEncoderConfig)
AV1CodecConfig::Av1(Av1EncoderConfig)

デコード

コーデックDecoderCodec
H.264DecoderCodec::H264
H.265DecoderCodec::Hevc
AV1DecoderCodec::Av1

サポートフォーマット

エンコード入力フォーマット (FrameFormat)

フォーマットFrameFormat説明
NV12FrameFormat::Nv12Semi-Planar YUV 4:2:0 8bit
YV12FrameFormat::Yv12Planar YUV 4:2:0 8bit (Y+V+U)
I420FrameFormat::I420Planar YUV 4:2:0 8bit (Y+U+V)
BGRAFrameFormat::BgraPacked BGRA 8bit
ARGBFrameFormat::ArgbPacked ARGB 8bit
RGBAFrameFormat::RgbaPacked RGBA 8bit
YUY2FrameFormat::Yuy2Packed YUV 4:2:2 8bit
UYVYFrameFormat::UyvyPacked YUV 4:2:2 8bit
P010FrameFormat::P010Semi-Planar YUV 4:2:0 10bit
P012FrameFormat::P012Semi-Planar YUV 4:2:0 12bit (16bit 格納)
P016FrameFormat::P016Semi-Planar YUV 4:2:0 16bit
Y210FrameFormat::Y210Packed YUV 4:2:2 10bit (16bit 格納)
AYUVFrameFormat::AyuvPacked YUV 4:4:4 8bit
Y410FrameFormat::Y410Packed YUV 4:4:4 10bit
Y416FrameFormat::Y416Packed YUV 4:4:4 16bit

デコード出力フォーマット

フォーマット説明
NV12Semi-Planar YUV 4:2:0 8bit

サポートプロファイル

H.264 (H264Profile)

プロファイルH264Profile
BaselineH264Profile::Baseline
MainH264Profile::Main
HighH264Profile::High
Constrained BaselineH264Profile::ConstrainedBaseline
Constrained HighH264Profile::ConstrainedHigh

H.265 (HevcProfile)

プロファイルHevcProfile
MainHevcProfile::Main
Main 10HevcProfile::Main10

AV1 (Av1Profile)

プロファイルAv1Profile
MainAv1Profile::Main

レート制御モード (RateControlMode)

モードRateControlMode説明
CQPRateControlMode::Cqp固定 QP
CBRRateControlMode::Cbr固定ビットレート
VBRRateControlMode::Vbrピーク制約付き可変ビットレート
LCVBRRateControlMode::LatencyConstrainedVbrレイテンシ制約付き可変ビットレート
QVBRRateControlMode::QualityVbr品質 VBR
HQVBRRateControlMode::HighQualityVbr高品質 VBR
HQCBRRateControlMode::HighQualityCbr高品質 CBR

ライセンス

Apache License 2.0

Copyright 2026-2026, Shiguredo Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

AMD AMF

https://github.com/GPUOpen-LibrariesAndSDKs/AMF

https://gpuopen.com/advanced-media-framework/