Hardware transcode.
Hardware transcode.
#include <iostream>
#include <string>
int main(int argc, char** argv)
{
if (argc < 4) {
std::cerr << "Usage: " << argv[0] << " <device> <input> <output>\n";
return 1;
}
std::cerr << "Unknown device type: " << argv[1] << '\n';
return 1;
}
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;
}
if (!decoderCodec.
raw() || !encoderCodec.
raw()) {
std::cerr << "No hardware decoder or encoder for this codec and device\n";
return 2;
}
if (ec) {
std::cerr << "Can't open decoder: " << ec.message() << '\n';
return 1;
}
bool outputReady = false;
bool zeroCopy = false;
int64_t index = 0;
encoder.
addFlags(AV_CODEC_FLAG_GLOBAL_HEADER);
zeroCopy = !ec;
if (!zeroCopy) {
std::cerr << "Zero-copy path is not available: " << ec.message()
<< "; using the copy path\n";
ec.clear();
AV_PIX_FMT_NONE, ec);
}
if (ec) {
std::cerr << "Can't configure hardware encoding: " << ec.message() << '\n';
return false;
}
encoder.
open(encoderCodec, ec);
if (ec) {
std::cerr << "Can't open encoder: " << ec.message() << '\n';
return false;
}
if (ec) {
std::cerr << "Can't open output: " << ec.message() << '\n';
return false;
}
if (ec) {
std::cerr << "Can't write header: " << ec.message() << '\n';
return false;
}
outputReady = true;
std::cout <<
"encoder: " << encoderCodec.
name()
<< ", path: " << (zeroCopy ? "zero-copy" : "copy") << '\n';
return true;
};
if (!outputReady && !setupOutput(frame))
return false;
if (!zeroCopy && frame.isHwFrame()) {
frame = frame.toSystemMemory(AV_PIX_FMT_NONE, ec);
if (ec) {
std::cerr << "Can't transfer frame to system memory: "
<< ec.message() << '\n';
return false;
}
}
frame.setStreamIndex(0);
frame.setPts(sourceTimestamp);
else
++index;
if (ec) {
std::cerr << "Encode error: " << ec.message() << '\n';
return false;
}
if (!packet)
return true;
if (ec) {
std::cerr << "Write error: " << ec.message() << '\n';
return false;
}
return true;
};
auto drainDecoder = [&]() -> bool {
for (;;) {
if (ec) {
std::cerr << "Receive error: " << ec.message() << '\n';
return false;
}
if (!frame)
return true;
if (!encodeFrame(std::move(frame)))
return false;
}
};
std::size_t packets = 0;
continue;
++packets;
for (;;) {
const bool accepted = decoder.
send(packet, ec);
if (ec) {
std::cerr << "Send error: " << ec.message() << '\n';
return 1;
}
if (!drainDecoder())
return 1;
if (accepted)
break;
}
}
if (ec) {
std::cerr << "Read error: " << ec.message() << '\n';
return 1;
}
if (!decoder.
flush(ec)) {
if (ec) {
std::cerr << "Flush error: " << ec.message() << '\n';
return 1;
}
if (!drainDecoder())
return 1;
if (!decoder.
flush(ec)) {
std::cerr << "Can't flush decoder\n";
return 1;
}
}
if (!drainDecoder())
return 1;
if (outputReady) {
for (;;) {
if (ec) {
std::cerr << "Encoder flush error: " << ec.message() << '\n';
return 1;
}
if (!flushed)
break;
if (ec) {
std::cerr << "Write error: " << ec.message() << '\n';
return 1;
}
}
if (ec) {
std::cerr << "Can't write trailer: " << ec.message() << '\n';
return 1;
}
}
std::cout << "packets: " << packets << '\n';
return 0;
}
int64_t bitRate() const noexcept
Target bit rate.
void setFrameRate(const Rational &value) noexcept
Set the frame rate.
Rational timeBase() const noexcept
Time base of the codec.
void setBitRate(int64_t bitRate) noexcept
Set the target bit rate.
void open(OptionalErrorCode ec=throws())
Open the codec with the parameters set so far.
void addFlags(int flags) noexcept
Add AV_CODEC_FLAG_* flags.
void setTimeBase(const Rational &value) noexcept
Set the time base.
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.
OutputFormat outputFormat() const
The muxer in use.
void openInput(const std::string &uri, OptionalErrorCode ec=throws())
Open an input and probe its container.
Stream addStream(const Codec &codec, OptionalErrorCode ec=throws())
Add a stream for the given codec.
void writeTrailer(OptionalErrorCode ec=throws())
Write the container trailer and close the output.
void writeHeader(OptionalErrorCode ec=throws())
Write the container header.
void openOutput(const std::string &uri, OptionalErrorCode ec=throws())
Open an output.
void writePacket(OptionalErrorCode ec=throws())
Flush the muxer queue.
void flush()
Discard buffered packets and internal state.
void setFormat(const InputFormat &format)
Force the demuxer used by openInput().
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.
Compressed data and its metadata.
Definition packet.h:110
int streamIndex() const
Index of the stream the packet belongs to.
void setStreamIndex(int idx)
Set the stream index.
Rational number: a numerator and a denominator.
Definition rational.h:49
One stream inside a container.
Definition stream.h:34
CodecParametersView codecParameters() const
Codec parameters of the stream.
void setFrameRate(const Rational &frameRate)
Set the declared frame rate.
Rational frameRate() const
Frame rate declared by the stream.
int index() const
Position of the stream in its container.
bool isValid() const
Whether the view refers to a stream.
void setTimeBase(const Rational &timeBase)
Set the time base of the stream.
void setAverageFrameRate(const Rational &frameRate)
Set the average frame rate.
The Timestamp class represents timestamp value and it timebase.
Definition timestamp.h:25
bool isNoPts() const noexcept
Whether the timestamp means "no timestamp".
bool isValid() const noexcept
Whether the timestamp is usable.
void setWidth(int w)
Set the frame width.
Definition codeccontext.h:1033
void setHeight(int h)
Set the frame height.
Definition codeccontext.h:1049
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.
Encodes video frames into packets.
Definition codeccontext.h:1338
Packet encode(OptionalErrorCode ec=throws())
encodeVideo - Flush encoder
void configureHwEncoding(const hw::DeviceContext &device, hw::EncodingMode mode=hw::EncodingMode::Auto, PixelFormat swFormat=AV_PIX_FMT_NONE, OptionalErrorCode ec=throws())
Configures hardware encoding for the given device.
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.
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 findEncoder(AVCodecID id, DeviceType type) noexcept
The first encoder for a codec identifier that supports a device type.
@ Auto
Prefer a hardware frames context, fall back to a device context.
Definition hwcontext.h:37
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