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

Hardware decoding.

Hardware decoding.

api2-hw-decode <device type> <input file> [--to-system]
// Decodes the first video stream with a hardware decoder.
//
// Usage: api2-hw-decode <device> <input> [--to-system]
//
// <device> is a device type name such as "vaapi", "qsv", "cuda" or
// "videotoolbox". The optional --to-system flag transfers every decoded frame
// to system memory.
#include <cstring>
#include <iostream>
#include <string>
#include "avcpp/av.h"
#include "avcpp/packet.h"
#include "avcpp/stream.h"
int main(int argc, char** argv)
{
if (argc < 3) {
std::cerr << "Usage: " << argv[0] << " <device> <input> [--to-system]\n";
return 1;
}
if (type.isNone()) {
std::cerr << "Unknown device type: " << argv[1] << '\n';
return 1;
}
const bool toSystem = (argc > 3 && std::strcmp(argv[3], "--to-system") == 0);
std::error_code ec;
av::hw::DeviceContext device = av::hw::DeviceContext::create(type, nullptr, {}, ec);
if (!device.isValid()) {
std::cerr << "Can't create device: " << ec.message() << '\n';
return 2;
}
ictx.openInput(argv[2], ec);
if (ec) {
std::cerr << "Can't open input: " << ec.message() << '\n';
return 1;
}
ictx.findStreamInfo(ec);
if (ec) {
std::cerr << "Can't find streams: " << ec.message() << '\n';
return 1;
}
for (auto&& stream : ictx.streams()) {
if (stream.mediaType() == AVMEDIA_TYPE_VIDEO) {
vst = stream;
break;
}
}
if (!vst.isValid()) {
std::cerr << "Video stream not found\n";
return 1;
}
if (!codec.raw()) {
std::cerr << "No hardware decoder for this codec and device\n";
return 2;
}
av::VideoDecoderContext decoder(vst, codec);
decoder.setHwDeviceContext(device, ec);
decoder.setHwTransferToSystem(toSystem);
decoder.open(av::Codec(), ec);
if (ec) {
std::cerr << "Can't open decoder: " << ec.message() << '\n';
return 1;
}
std::cout << "decoder: " << codec.name() << ", device: " << type.name() << '\n';
std::size_t packets = 0;
std::size_t frames = 0;
std::size_t hardware = 0;
auto consume = [&](const av::VideoFrame& frame) {
if (!frame)
return;
++frames;
if (frame.isHwFrame())
++hardware;
if (frames == 1) {
std::cout << "first frame: " << frame.width() << 'x' << frame.height()
<< ", format: " << av::PixelFormat(static_cast<AVPixelFormat>(frame.raw()->format)).name()
<< ", hardware: " << (frame.isHwFrame() ? "yes" : "no")
<< '\n';
}
};
av::Packet packet;
while ((packet = ictx.readPacket(ec))) {
if (packet.streamIndex() != vst.index())
continue;
++packets;
for (;;) {
const bool accepted = decoder.send(packet, ec);
if (ec) {
std::cerr << "Send error: " << ec.message() << '\n';
return 1;
}
for (;;) {
av::VideoFrame frame = decoder.receive(ec);
if (ec) {
std::cerr << "Receive error: " << ec.message() << '\n';
return 1;
}
if (!frame.isValid())
break;
consume(frame);
}
if (accepted)
break;
}
}
if (ec) {
std::cerr << "Read error: " << ec.message() << '\n';
return 1;
}
// Hardware decoders can delay frames, so drain them after the last packet.
if (!decoder.flush(ec)) {
std::cerr << "Flush error: " << ec.message() << '\n';
return 1;
}
for (;;) {
av::VideoFrame frame = decoder.receive(ec);
if (ec) {
std::cerr << "Receive error: " << ec.message() << '\n';
return 1;
}
if (!frame.isValid())
break;
consume(frame);
}
std::cout << "packets: " << packets
<< ", frames: " << frames
<< ", hardware frames: " << hardware << '\n';
return 0;
}
Library initialization.
void open(OptionalErrorCode ec=throws())
Open the codec with the parameters set so far.
bool flush(OptionalErrorCode ec=throws())
Puts the codec into draining mode.
Definition codeccontext.h:863
AVCodecID codecId() const
Identifier of the codec.
Codec descriptor.
Definition codec.h:32
const char * name() const
Short name of the codec, as FFmpeg knows it.
Container context: the entry point for demuxing, muxing and seeking.
Definition formatcontext.h:153
StreamsView streams(OptionalErrorCode ec=throws()) const
All streams as a range, for range-based for and std algorithms.
void openInput(const std::string &uri, OptionalErrorCode ec=throws())
Open an input and probe its container.
void findStreamInfo(OptionalErrorCode ec=throws())
Read the stream parameters of an opened input.
Packet readPacket(OptionalErrorCode ec=throws())
Read the next packet from any stream.
bool isValid() const
Whether the wrapper holds a frame.
Compressed data and its metadata.
Definition packet.h:110
int streamIndex() const
Index of the stream the packet belongs to.
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.
One stream inside a container.
Definition stream.h:34
CodecParametersView codecParameters() const
Codec parameters of the stream.
int index() const
Position of the stream in its container.
bool isValid() const
Whether the view refers to a stream.
Decodes video packets into frames.
Definition codeccontext.h:1161
bool send(const Packet &packet, OptionalErrorCode ec=throws())
Sends a packet to the decoder.
VideoFrame receive(OptionalErrorCode ec=throws(), bool autoAllocateFrame=true)
Receives one decoded frame.
void setHwDeviceContext(const hw::DeviceContext &device, OptionalErrorCode ec=throws())
Configures a hardware device for decoding.
void setHwTransferToSystem(bool enabled) noexcept
Transfer decoded hardware frames to system memory automatically.
One video frame.
Definition frame.h:578
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
static DeviceType fromName(const char *name) noexcept
Look a device type up by its FFmpeg name.
const char * name() const noexcept
FFmpeg name of the device type, or "none".
bool isNone() const noexcept
Whether the wrapper holds no device type.
Definition hwcontext.h:84
Codec contexts: opening, configuring, encoding and decoding.
Container format contexts: opening, demuxing, muxing and seeking.
Hardware devices, hardware frames and hardware codec configuration.
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.
Compressed packets and their side data.
Streams inside a container: parameters, time base and disposition.
const T * raw() const
Read access to the wrapped object.
Definition ffmpeg.h:56