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

Decode video.

Decode video.

api2-decode <args>
// Decodes the first video stream and prints the parameters of every frame.
//
// Shows: demuxing, video decoding with the legacy decode() path, packet side
// data, frame timestamps and pixel formats.
//
// Usage: api2-decode <input file>
#include <iostream>
#include <set>
#include <map>
#include <memory>
#include <functional>
#include "avcpp/avconfig.h"
#include "avcpp/av.h"
#include "avcpp/ffmpeg.h"
#include "avcpp/codec.h"
#include "avcpp/packet.h"
#include "avcpp/avutils.h"
// API2
#include "avcpp/format.h"
#include "avcpp/codec.h"
using namespace std;
using namespace av;
int main(int argc, char **argv)
{
if (argc < 2)
return 1;
av::setFFmpegLoggingLevel(AV_LOG_DEBUG);
string uri {argv[1]};
ssize_t videoStream = -1;
Stream vst;
error_code ec;
int count = 0;
{
ictx.openInput(uri, ec);
if (ec) {
cerr << "Can't open input\n";
return 1;
}
cerr << "Streams: " << ictx.streamsCount() << endl;
ictx.findStreamInfo(ec);
if (ec) {
cerr << "Can't find streams: " << ec << ", " << ec.message() << endl;
return 1;
}
for (auto&& st : ictx.streams()) {
if (st.mediaType() == AVMEDIA_TYPE_VIDEO) {
vst = st;
videoStream = st.index();
break;
}
}
cerr << videoStream << endl;
if (vst.isNull()) {
cerr << "Video stream not found\n";
return 1;
}
if (vst.isValid()) {
vdec = VideoDecoderContext(vst);
//Codec codec = findDecodingCodec(vdec.raw()->codec_id);
//vdec.setCodec(codec);
//vdec.setRefCountedFrames(true);
vdec.open({{"threads", "1"}}, Codec(), ec);
//vdec.open(ec);
if (ec) {
cerr << "Can't open codec\n";
return 1;
}
}
while (Packet pkt = ictx.readPacket(ec)) {
if (ec) {
clog << "Packet reading error: " << ec << ", " << ec.message() << endl;
return 1;
}
if (pkt.streamIndex() != videoStream) {
continue;
}
auto ts = pkt.ts();
clog << "Read packet: " << ts << " / " << ts.seconds() << " / " << pkt.timeBase() << " / st: " << pkt.streamIndex() << endl;
#if AVCPP_HAS_PKT_SIDE_DATA
for (auto side : pkt.sideData()) {
clog << " found packet side data: " << side.name() << endl;
if (side.type() == AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL) {
assert(side.span().size() >= sizeof(uint64_t));
// BE
uint64_t idType{};
memcpy(&idType, side.span().data(), sizeof(idType));
//idType = std::endian::native == std::endian::little ? std::byteswap(idType) : idType;
idType = av_be2ne64(idType);
auto payload = side.span().subspan(sizeof(uint64_t));
clog << " matroska block additions id type = " << idType << endl;
if (!payload.empty() && idType == 100 /* vendor specific */) {
clog << " block additions data: " << std::string_view{(const char*)payload.data(), payload.size()} << endl;
}
}
}
#endif
VideoFrame frame = vdec.decode(pkt, ec);
count++;
//if (count > 100)
// break;
if (ec) {
cerr << "Error: " << ec << ", " << ec.message() << endl;
return 1;
} else if (!frame) {
cerr << "Empty frame\n";
//continue;
}
ts = frame.pts();
clog << " Frame: " << frame.width() << "x" << frame.height() << ", size=" << frame.size() << ", ts=" << ts << ", tm: " << ts.seconds() << ", tb: " << frame.timeBase() << ", ref=" << frame.isReferenced() << ":" << frame.refCount() << endl;
}
clog << "Flush frames;\n";
while (true) {
VideoFrame frame = vdec.decode(Packet(), ec);
if (ec) {
cerr << "Error: " << ec << ", " << ec.message() << endl;
return 1;
}
if (!frame)
break;
auto ts = frame.pts();
clog << " Frame: " << frame.width() << "x" << frame.height() << ", size=" << frame.size() << ", ts=" << ts << ", tm: " << ts.seconds() << ", tb: " << frame.timeBase() << ", ref=" << frame.isReferenced() << ":" << frame.refCount() << endl;
}
// NOTE: stream decodec must be closed/destroyed before
//ictx.close();
//vdec.close();
}
}
Audio resampling with libswresample.
Library initialization.
Common helpers: comparisons, value lookup, non-copyable base, string utilities.
void open(OptionalErrorCode ec=throws())
Open the codec with the parameters set so far.
Codec descriptor.
Definition codec.h:32
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.
size_t streamsCount() const
Number of streams in the container.
Packet readPacket(OptionalErrorCode ec=throws())
Read the next packet from any stream.
Timestamp pts() const
Presentation timestamp.
Compressed data and its metadata.
Definition packet.h:110
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.
double seconds() const noexcept
The timestamp in seconds.
Decodes video packets into frames.
Definition codeccontext.h:1161
VideoFrame decode(const Packet &packet, OptionalErrorCode ec=throws(), bool autoAllocateFrame=true)
decodeVideo - decode video packet
One video frame.
Definition frame.h:578
Codec descriptors and lookup by name or id.
Codec contexts: opening, configuring, encoding and decoding.
FFmpeg types re-exported for the public API and small value wrappers.
Input and output format descriptors.
Container format contexts: opening, demuxing, muxing and seeking.
Root namespace of the library.
Definition audioresampler.h:19
void init()
Initialize all subsystems the library needs.
STL namespace.
Compressed packets and their side data.
bool isNull() const
Whether the wrapper is empty.
Definition ffmpeg.h:77
Video scaling and pixel format conversion with libswscale.