Hardware encoding.
Hardware encoding.
#include <iostream>
#include <string>
namespace {
{
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;
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;
}
}
}
}
int main(int argc, char** argv)
{
if (argc < 3) {
std::cerr << "Usage: " << argv[0] << " <device> <output> [frame count]\n";
return 1;
}
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;
std::cerr << "Can't create device: " << ec.message() << '\n';
return 2;
}
std::cerr << "No hardware H.264 encoder for this device\n";
return 2;
}
encoder.setWidth(width);
encoder.setHeight(height);
encoder.setBitRate(1'000'000);
encoder.addFlags(AV_CODEC_FLAG_GLOBAL_HEADER);
AV_PIX_FMT_NONE, ec);
if (ec) {
std::cerr << "Can't configure hardware encoding: " << ec.message() << '\n';
return 1;
}
encoder.open(codec, ec);
if (ec) {
std::cerr << "Can't open encoder: " << ec.message() << '\n';
return 1;
}
if (ec) {
std::cerr << "Can't open output: " << ec.message() << '\n';
return 1;
}
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) {
if (!frame.isValid()) {
std::cerr << "Can't allocate a frame\n";
return 1;
}
fillFrame(frame, index);
frame.setTimeBase(encoder.timeBase());
frame.setStreamIndex(0);
if (ec) {
std::cerr << "Encode error: " << ec.message() << '\n';
return 1;
}
if (!packet)
continue;
if (ec) {
std::cerr << "Write error: " << ec.message() << '\n';
return 1;
}
++written;
}
for (;;) {
if (ec) {
std::cerr << "Encoder flush error: " << ec.message() << '\n';
return 1;
}
if (!packet)
break;
if (ec) {
std::cerr << "Write error: " << ec.message() << '\n';
return 1;
}
++written;
}
if (ec) {
std::cerr << "Can't write trailer: " << ec.message() << '\n';
return 1;
}
std::cout << "packets: " << written << '\n';
return 0;
}
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().
Compressed data and its metadata.
Definition packet.h:110
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
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.
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