Decode a raw H.264 stream.
Decode a raw H.264 stream.
#include <cstdio>
#include <iostream>
#include <filesystem>
class File
{
std::FILE *_fp = nullptr;
public:
File() = default;
File(std::FILE *fp)
: _fp(fp)
{
}
File(std::nullptr_t)
{
}
~File()
{
std::fclose(_fp);
}
File(const File &) = delete;
void operator=(const File &) = delete;
File(File &&rhs)
: _fp(std::exchange(rhs._fp, nullptr))
{
}
File &operator=(File &&rhs)
{
if (this != std::addressof(rhs)) {
File(std::move(rhs)).swap(*this);
}
return *this;
}
void swap(File &rhs)
{
using std::swap;
swap(_fp, rhs._fp);
}
void reset(std::FILE *fp = nullptr)
{
if (_fp == fp)
return;
File(fp).swap(*this);
}
std::FILE *get() const
{
return _fp;
}
operator bool() const noexcept
{
return !!_fp;
}
operator std::FILE *() const noexcept
{
return _fp;
}
};
int main(int argc, char **argv)
{
if (argc < 2)
return 1;
av::setFFmpegLoggingLevel(AV_LOG_DEBUG);
string uri {argv[1]};
error_code ec;
{
size_t counter {};
File in = std::fopen(uri.c_str(), "rb+");
if (!in) {
cerr << "Can't open input\n";
return 1;
}
{
if (ec) {
cerr << "Can't open codec\n";
return 1;
}
}
auto const fsize = std::filesystem::file_size(uri);
std::vector<uint8_t> data(fsize);
std::fread(data.data(), 1, data.size(), in);
const uint8_t startCode[3] = {0x00, 0x00, 0x01};
auto start = data.begin();
while (true) {
auto it = std::search(start, data.end(), std::begin(startCode), std::end(startCode));
auto const finish = (it == data.begin() || *(it - 1) != 0x00) ? it : it - 1;
if (start != data.begin())
start -= 1;
auto const ptr = std::to_address(start);
auto const size = size_t(finish - start);
start = it + 1;
clog << " Data: " << (void *)ptr << ", size: " << size << endl;
auto frame = vdec.
decode(pkt, ec);
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 << " : side_data: " << (void *)frame.raw()->side_data << endl;
if (frame)
++counter;
if (it == data.end())
break;
}
clog << "Flush frames:\n";
while (true) {
if (ec) {
cerr << "Error: " << ec << ", " << ec.message() << endl;
return 1;
}
if (!frame)
break;
clog <<
" Frame: " << frame.width() <<
"x" << frame.height() <<
", size=" << frame.size() <<
", ts=" << ts <<
", tm: " << ts.
seconds() <<
", tb: " << frame.timeBase() <<
", ref=" << frame.isReferenced() <<
":" << frame.refCount() << endl;
++counter;
}
clog << "total frames: " << counter << endl;
}
}
Audio resampling with libswresample.
Common helpers: comparisons, value lookup, non-copyable base, string utilities.
void open(OptionalErrorCode ec=throws())
Open the codec with the parameters set so far.
Timestamp pts() const
Presentation timestamp.
Compressed data and its metadata.
Definition packet.h:110
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.
Root namespace of the library.
Definition audioresampler.h:19
Codec findDecodingCodec(AVCodecID id)
Find a decoder by codec identifier.
void init()
Initialize all subsystems the library needs.
Compressed packets and their side data.
Wrap static data, do not owning and free.
Definition packet.h:124
Video scaling and pixel format conversion with libswscale.