Decode with send/receive.
Decode with send/receive.
#include <cstddef>
#include <iostream>
int main(int argc, char **argv)
{
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <input file>\n";
return 1;
}
std::error_code ec;
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;
}
if (st.mediaType() == AVMEDIA_TYPE_VIDEO) {
vst = st;
break;
}
}
std::cerr << "Video stream not found\n";
return 1;
}
if (ec) {
std::cerr << "Can't open codec: " << ec.message() << '\n';
return 1;
}
std::size_t packets = 0;
std::size_t frames = 0;
auto drain = [&]() -> bool {
for (;;) {
if (ec) {
std::cerr << "Can't receive a frame: " << ec.message() << '\n';
return false;
}
if (!frame)
return true;
++frames;
}
};
continue;
++packets;
for (;;) {
bool accepted = dec.
send(pkt, ec);
if (ec) {
std::cerr << "Can't send a packet: " << ec.message() << '\n';
return 1;
}
if (!drain())
return 1;
if (accepted)
break;
}
}
if (ec) {
std::cerr << "Read error: " << ec.message() << '\n';
return 1;
}
if (ec) {
std::cerr << "Can't flush the decoder: " << ec.message() << '\n';
return 1;
}
if (!drain())
return 1;
std::cerr << "Can't flush the decoder after draining\n";
return 1;
}
}
if (!drain())
return 1;
std::cerr << "Decoder did not reach EOF after draining\n";
return 1;
}
std::cout << "packets: " << packets << ", frames: " << frames << '\n';
return 0;
}
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.