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

Hardware encoding.

Hardware encoding.

api2-hw-encode <device type> <output file>
// Hardware encoder sample with synthetic frames.
//
// Usage: api2-hw-encode <device> <output> [frame count]
//
// The sample configures hardware encoding in Auto mode. Software frames are
// uploaded automatically before they reach the encoder.
#include <iostream>
#include <string>
#include "avcpp/av.h"
#include "avcpp/format.h"
#include "avcpp/frame.h"
#include "avcpp/packet.h"
namespace {
void fillFrame(av::VideoFrame& frame, int index)
{
AVFrame* raw = frame.raw();
const AVPixFmtDescriptor* desc =
av_pix_fmt_desc_get(static_cast<AVPixelFormat>(raw->format));
if (!desc)
return;
const int planes = av_pix_fmt_count_planes(static_cast<AVPixelFormat>(raw->format));
for (int plane = 0; plane < planes; ++plane) {
if (!raw->data[plane] || raw->linesize[plane] <= 0)
continue;
int height = raw->height;
// The second and third planes are chroma planes in most YUV formats.
if (plane == 1 || plane == 2)
height >>= desc->log2_chroma_h;
const uint8_t value = static_cast<uint8_t>((index * 3 + plane * 40) & 0xFF);
for (int row = 0; row < height; ++row) {
uint8_t* line = raw->data[plane] + row * raw->linesize[plane];
for (int x = 0; x < raw->linesize[plane]; ++x)
line[x] = value;
}
}
}
} // namespace
int main(int argc, char** argv)
{
if (argc < 3) {
std::cerr << "Usage: " << argv[0] << " <device> <output> [frame count]\n";
return 1;
}
if (type.isNone()) {
std::cerr << "Unknown device type: " << argv[1] << '\n';
return 1;
}
const int width = 320;
const int height = 240;
const int frameCount = argc > 3 ? std::stoi(argv[3]) : 50;
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;
}
av::Codec codec = av::hw::findEncoder(AV_CODEC_ID_H264, type);
if (!codec.raw()) {
std::cerr << "No hardware H.264 encoder for this device\n";
return 2;
}
ofrmt.setFormat(std::string(), argv[2]);
octx.setFormat(ofrmt);
av::VideoEncoderContext encoder(codec);
encoder.setWidth(width);
encoder.setHeight(height);
encoder.setTimeBase(av::Rational{1, 25});
encoder.setFrameRate(av::Rational{25, 1});
encoder.setBitRate(1'000'000);
if (octx.outputFormat().isFlags(AVFMT_GLOBALHEADER))
encoder.addFlags(AV_CODEC_FLAG_GLOBAL_HEADER);
encoder.configureHwEncoding(device, av::hw::EncodingMode::Auto,
AV_PIX_FMT_NONE, ec);
if (ec) {
std::cerr << "Can't configure hardware encoding: " << ec.message() << '\n';
return 1;
}
av::PixelFormat softwareFormat(AV_PIX_FMT_NV12);
const av::hw::FramesContext frames = encoder.hwFramesContext();
if (frames.isValid())
softwareFormat = frames.swFormat();
encoder.open(codec, ec);
if (ec) {
std::cerr << "Can't open encoder: " << ec.message() << '\n';
return 1;
}
av::Stream ost = octx.addStream(encoder);
ost.setFrameRate(encoder.frameRate());
ost.setAverageFrameRate(encoder.frameRate());
ost.setTimeBase(encoder.timeBase());
octx.openOutput(argv[2], ec);
if (ec) {
std::cerr << "Can't open output: " << ec.message() << '\n';
return 1;
}
octx.writeHeader(ec);
octx.flush();
if (ec) {
std::cerr << "Can't write header: " << ec.message() << '\n';
return 1;
}
std::cout << "encoder: " << codec.name()
<< ", software format: " << softwareFormat.name()
<< ", frames: " << frameCount << '\n';
int written = 0;
for (int index = 0; index < frameCount; ++index) {
av::VideoFrame frame(softwareFormat, width, height, 32);
if (!frame.isValid()) {
std::cerr << "Can't allocate a frame\n";
return 1;
}
fillFrame(frame, index);
frame.setPts(av::Timestamp(index, encoder.timeBase()));
frame.setTimeBase(encoder.timeBase());
frame.setStreamIndex(0);
av::Packet packet = encoder.encode(frame, ec);
if (ec) {
std::cerr << "Encode error: " << ec.message() << '\n';
return 1;
}
if (!packet)
continue;
packet.setStreamIndex(0);
octx.writePacket(packet, ec);
if (ec) {
std::cerr << "Write error: " << ec.message() << '\n';
return 1;
}
++written;
}
for (;;) {
av::Packet packet = encoder.encode(ec);
if (ec) {
std::cerr << "Encoder flush error: " << ec.message() << '\n';
return 1;
}
if (!packet)
break;
packet.setStreamIndex(0);
octx.writePacket(packet, ec);
if (ec) {
std::cerr << "Write error: " << ec.message() << '\n';
return 1;
}
++written;
}
octx.writeTrailer(ec);
if (ec) {
std::cerr << "Can't write trailer: " << ec.message() << '\n';
return 1;
}
std::cout << "packets: " << written << '\n';
return 0;
}
Library initialization.
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
OutputFormat outputFormat() const
The muxer in use.
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().
Muxer descriptor.
Definition format.h:202
bool setFormat(const std::string &name, const std::string &url=std::string(), const std::string &mime=std::string()) noexcept
Look a muxer up and point the descriptor at it.
Compressed data and its metadata.
Definition packet.h:110
void setStreamIndex(int idx)
Set the stream index.
The PixelFormat class is a simple wrapper for AVPixelFormat that allow to acces it it properties.
Definition pixelformat.h:41
Rational number: a numerator and a denominator.
Definition rational.h:49
One stream inside a container.
Definition stream.h:34
void setFrameRate(const Rational &frameRate)
Set the declared frame rate.
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
Encodes video frames into packets.
Definition codeccontext.h:1338
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
Owner of an AVHWFramesContext.
Definition hwcontext.h:242
bool isValid() const noexcept
Whether the wrapper holds a pool.
PixelFormat swFormat() const noexcept
The system-memory format used to transfer frames into and out of the pool.
Codec contexts: opening, configuring, encoding and decoding.
Input and output format descriptors.
Container format contexts: opening, demuxing, muxing and seeking.
Video frames, audio samples and their common frame interface.
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
void init()
Initialize all subsystems the library needs.
Compressed packets and their side data.
const T * raw() const
Read access to the wrapped object.
Definition ffmpeg.h:56
bool isFlags(int32_t flags) const noexcept
Whether all given flags are set.
Definition format.h:112