avcpp 3.2.1
Wrapper for the FFmpeg that simplify usage from C++ projects.
Loading...
Searching...
No Matches
api2-send-receive.cpp

Decode with send/receive.

Decode with send/receive.

api2-send-receive <input file>
// Decodes the first video stream with the avcodec send/receive API and
// demonstrates the end-of-stream contract: call flush() once, drain with
// receive() until it returns an empty result, then check isEof().
//
// Usage: api2-send-receive <input file>
#include <cstddef>
#include <iostream>
#include "avcpp/av.h"
#include "avcpp/packet.h"
#include "avcpp/stream.h"
int main(int argc, char **argv)
{
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <input file>\n";
return 1;
}
std::error_code ec;
ictx.openInput(argv[1], 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 &&st : ictx.streams()) {
if (st.mediaType() == AVMEDIA_TYPE_VIDEO) {
vst = st;
break;
}
}
if (!vst.isValid()) {
std::cerr << "Video stream not found\n";
return 1;
}
dec.open(ec);
if (ec) {
std::cerr << "Can't open codec: " << ec.message() << '\n';
return 1;
}
std::size_t packets = 0;
std::size_t frames = 0;
// receive() returns an empty frame for both EAGAIN and EOF. The loop ends
// only when the decoder has no output left to produce.
auto drain = [&]() -> bool {
for (;;) {
auto frame = dec.receive(ec);
if (ec) {
std::cerr << "Can't receive a frame: " << ec.message() << '\n';
return false;
}
if (!frame)
return true;
++frames;
}
};
while ((pkt = ictx.readPacket(ec))) {
if (pkt.streamIndex() != vst.index())
continue;
++packets;
for (;;) {
bool accepted = dec.send(pkt, ec);
if (ec) {
std::cerr << "Can't send a packet: " << ec.message() << '\n';
return 1;
}
// Drain what is available. A rejected send() means EAGAIN: the
// packet must be retried after the output queue is empty.
if (!drain())
return 1;
if (accepted)
break;
}
}
if (ec) {
std::cerr << "Read error: " << ec.message() << '\n';
return 1;
}
// Switch the decoder to draining mode. flush() is idempotent and reports
// false only when the output queue must be drained before it is retried.
if (!dec.flush(ec)) {
if (ec) {
std::cerr << "Can't flush the decoder: " << ec.message() << '\n';
return 1;
}
if (!drain())
return 1;
if (!dec.flush(ec)) {
std::cerr << "Can't flush the decoder after draining\n";
return 1;
}
}
if (!drain())
return 1;
if (!dec.isEof()) {
std::cerr << "Decoder did not reach EOF after draining\n";
return 1;
}
std::cout << "packets: " << packets << ", frames: " << frames << '\n';
return 0;
}
Library initialization.
void open(OptionalErrorCode ec=throws())
Open the codec with the parameters set so far.
bool isEof() const noexcept
Returns true if the codec has been flushed and all output is drained.
Definition codeccontext.h:272
bool flush(OptionalErrorCode ec=throws())
Puts the codec into draining mode.
Definition codeccontext.h:863
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.
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
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.
Codec contexts: opening, configuring, encoding and decoding.
Container format contexts: opening, demuxing, muxing and seeking.
void init()
Initialize all subsystems the library needs.
Compressed packets and their side data.
Streams inside a container: parameters, time base and disposition.