avcpp 3.2.1
Wrapper for the FFmpeg that simplify usage from C++ projects.
Loading...
Searching...
No Matches
api2-hw-probe.cpp

Probe hardware devices.

Probe hardware devices.

api2-hw-probe
// Lists hardware device types, probes the devices and prints the H.264 and
// H.265 hardware codec configurations.
//
// Usage: api2-hw-probe [device]
//
// The optional device string is backend-specific. For example, a VAAPI device
// is usually "/dev/dri/renderD128" and a CUDA device is a numeric index.
#include <iostream>
#include <string>
#include "avcpp/av.h"
#include "avcpp/codec.h"
namespace {
const char* codecName(const av::Codec& codec)
{
return codec.raw() ? codec.name() : "none";
}
std::string pixelFormatName(AVPixelFormat format)
{
if (format == AV_PIX_FMT_NONE)
return "none";
std::error_code ec;
const char* name = av::PixelFormat(format).name(ec);
return ec ? "invalid" : name;
}
void printConfigs(const av::Codec& codec, const char* title)
{
std::cout << " " << title << " (" << codecName(codec) << "):";
if (!codec.raw()) {
std::cout << " none\n";
return;
}
bool first = true;
for (const AVCodecHWConfig& config : av::hw::codecConfigs(codec)) {
std::cout << (first ? " " : ", ")
<< av::hw::DeviceType(config.device_type).name()
<< '/'
<< pixelFormatName(config.pix_fmt);
if (config.methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX)
std::cout << "+device";
if (config.methods & AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX)
std::cout << "+frames";
first = false;
}
if (first)
std::cout << " none";
std::cout << '\n';
}
void printCodecs(av::hw::DeviceType type)
{
printConfigs(av::hw::findDecoder(AV_CODEC_ID_H264, type), "h264 decoder");
printConfigs(av::hw::findEncoder(AV_CODEC_ID_H264, type), "h264 encoder");
printConfigs(av::hw::findDecoder(AV_CODEC_ID_HEVC, type), "hevc decoder");
printConfigs(av::hw::findEncoder(AV_CODEC_ID_HEVC, type), "hevc encoder");
}
} // namespace
int main(int argc, char** argv)
{
const char* device = argc > 1 ? argv[1] : nullptr;
std::cout << "Device types compiled into FFmpeg:";
std::cout << ' ' << type.name();
std::cout << "\n\n";
for (const av::hw::DeviceProbe& probe : av::hw::probeDevices(device)) {
std::cout << probe.type.name() << ": ";
if (!probe.available) {
std::cout << "unavailable (" << probe.error.message() << ")\n";
continue;
}
std::cout << "available\n";
std::error_code ec;
av::hw::DeviceContext::create(probe.type, device, {}, ec);
if (!context.isValid()) {
std::cout << " device creation failed: " << ec.message() << '\n';
continue;
}
if (constraints.isValid()) {
std::cout << " hardware formats:";
for (av::PixelFormat format : constraints.hwFormats())
std::cout << ' ' << format.name();
std::cout << "\n software formats:";
for (av::PixelFormat format : constraints.swFormats())
std::cout << ' ' << format.name();
std::cout << "\n size: "
<< constraints.minWidth() << 'x' << constraints.minHeight()
<< " .. "
<< constraints.maxWidth() << 'x' << constraints.maxHeight()
<< '\n';
} else {
std::cout << " frame constraints: " << ec.message() << '\n';
}
printCodecs(probe.type);
}
return 0;
}
Library initialization.
Codec descriptor.
Definition codec.h:32
const char * name() const
Short name of the codec, as FFmpeg knows it.
The PixelFormat class is a simple wrapper for AVPixelFormat that allow to acces it it properties.
Definition pixelformat.h:41
const char * name(OptionalErrorCode ec=throws()) const
Name of the format.
Owner of an AVHWDeviceContext.
Definition hwcontext.h:126
bool isValid() const noexcept
Whether the wrapper holds a device.
static DeviceContext create(DeviceType type, const char *device=nullptr, const Dictionary &options={}, OptionalErrorCode ec=throws())
Create and open a hardware device.
Wrapper for AVHWDeviceType.
Definition hwcontext.h:53
const char * name() const noexcept
FFmpeg name of the device type, or "none".
Owner of AVHWFramesConstraints.
Definition hwcontext.h:1087
int minWidth() const noexcept
Smallest supported width.
int maxWidth() const noexcept
Largest supported width.
PixelFormatRange hwFormats() const noexcept
Hardware surface formats the device supports.
static FrameConstraints query(const DeviceContext &device, OptionalErrorCode ec=throws())
Ask the device for its frame constraints.
PixelFormatRange swFormats() const noexcept
System-memory formats that can be transferred to the device.
bool isValid() const noexcept
Whether constraints were obtained.
Definition hwcontext.h:1113
int maxHeight() const noexcept
Largest supported height.
int minHeight() const noexcept
Smallest supported height.
Codec descriptors and lookup by name or id.
Hardware devices, hardware frames and hardware codec configuration.
DeviceTypeRange allDeviceTypes() noexcept
All hardware device types compiled into FFmpeg.
CodecConfigRange codecConfigs(const Codec &codec) noexcept
All hardware configurations of a codec.
Codec findEncoder(AVCodecID id, DeviceType type) noexcept
The first encoder for a codec identifier that supports a device type.
DeviceProbeRange probeDevices(const char *device=nullptr)
Probe every compiled device type with a borrowed selector.
Codec findDecoder(AVCodecID id, DeviceType type) noexcept
The first decoder for a codec identifier that supports a device type.
void init()
Initialize all subsystems the library needs.
const T * raw() const
Read access to the wrapped object.
Definition ffmpeg.h:56
Result of probing one device type: whether it could be created, and why not.
Definition hwcontext.h:552