Hardware decoding.
Hardware decoding.
#include <cstring>
#include <iostream>
#include <string>
int main(int argc, char** argv)
{
if (argc < 3) {
std::cerr << "Usage: " << argv[0] << " <device> <input> [--to-system]\n";
return 1;
}
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;
std::cerr << "Can't create device: " << ec.message() << '\n';
return 2;
}
if (ec) {
std::cerr << "Can't open input: " << ec.message() << '\n';
return 1;
}
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;
}
}
std::cerr << "Video stream not found\n";
return 1;
}
std::cerr << "No hardware decoder for this codec and device\n";
return 2;
}
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;
if (!frame)
return;
++frames;
if (frame.isHwFrame())
++hardware;
if (frames == 1) {
std::cout << "first frame: " << frame.width() << 'x' << frame.height()
<< ", hardware: " << (frame.isHwFrame() ? "yes" : "no")
<< '\n';
}
};
continue;
++packets;
for (;;) {
const bool accepted = decoder.
send(packet, ec);
if (ec) {
std::cerr << "Send error: " << ec.message() << '\n';
return 1;
}
for (;;) {
if (ec) {
std::cerr << "Receive error: " << ec.message() << '\n';
return 1;
}
break;
consume(frame);
}
if (accepted)
break;
}
}
if (ec) {
std::cerr << "Read error: " << ec.message() << '\n';
return 1;
}
if (!decoder.
flush(ec)) {
std::cerr << "Flush error: " << ec.message() << '\n';
return 1;
}
for (;;) {
if (ec) {
std::cerr << "Receive error: " << ec.message() << '\n';
return 1;
}
break;
consume(frame);
}
std::cout << "packets: " << packets
<< ", frames: " << frames
<< ", hardware frames: " << hardware << '\n';
return 0;
}
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.
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