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

Decode audio.

Decode audio.

api2-decode-audio <args>
// Decodes the first audio stream and prints the parameters of every buffer.
//
// Shows: demuxing, audio decoding with the legacy decode() path, sample layout
// accessors.
//
// Usage: api2-decode-audio <input file> [format]
// format optional demuxer name, for example "wav"
#include <iostream>
#include <set>
#include <map>
#include <memory>
#include <functional>
#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 audioStream = -1;
Stream ast;
error_code ec;
string format;
if (argc > 2) {
format = argv[2];
}
int count = 0;
{
if (!format.empty()) {
ifmt.setFormat(format);
}
ictx.openInput(uri, ifmt, ec);
if (ec) {
cerr << "Can't open input\n";
return 1;
}
for (size_t i = 0; i < ictx.streamsCount(); ++i) {
auto st = ictx.stream(i);
if (st.isAudio()) {
audioStream = i;
ast = st;
break;
}
}
cerr << audioStream << endl;
if (ast.isNull()) {
cerr << "Audio stream not found\n";
return 1;
}
if (ast.isValid()) {
adec = AudioDecoderContext(ast);
Codec codec = findDecodingCodec(adec.raw()->codec_id);
adec.setCodec(codec);
adec.setRefCountedFrames(true);
adec.open(Codec(), ec);
if (ec) {
cerr << "Can't open codec\n";
return 1;
}
}
clog << "NO_PTS: " << av::NoPts << endl;
clog << "Duration: " << ictx.duration() << " / " << ictx.duration().seconds()
<< ", Start Time: " << ictx.startTime() << " / " << ictx.startTime().seconds()
<< endl;
// Substract start time from the Packet PTS and DTS values: PTS starts from the zero
ictx.substractStartTime(true);
while (true) {
Packet pkt = ictx.readPacket(ec);
if (ec)
{
clog << "Packet reading error: " << ec << ", " << ec.message() << endl;
break;
}
// EOF
if (!pkt)
{
break;
}
if (pkt.streamIndex() != audioStream) {
continue;
}
clog << "Read packet: " << pkt.pts() << " / " << pkt.pts().seconds() << " / " << pkt.timeBase() << " / st: " << pkt.streamIndex() << endl;
AudioSamples samples = adec.decode(pkt, ec);
if (ec) {
cerr << "Error: " << ec << endl;
return 1;
} else if (!samples) {
//cerr << "Empty frame\n";
//continue;
}
clog << " Samples: " << samples.samplesCount()
<< ", ch: " << samples.channelsCount()
<< ", freq: " << samples.sampleRate()
<< ", name: " << samples.channelsLayoutString()
<< ", ref=" << samples.isReferenced() << ":" << samples.refCount()
<< ", ts: " << samples.pts()
<< ", tm: " << samples.pts().seconds()
<< endl;
//return 0;
count++;
if (count > 100)
break;
}
// 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.
Decodes audio packets into sample buffers.
Definition codeccontext.h:1692
AudioSamples decode(const Packet &inPacket, OptionalErrorCode ec=throws())
Decode one audio packet.
One buffer of decoded audio samples.
Definition frame.h:892
std::string channelsLayoutString() const
Human-readable name of the channel layout.
int channelsCount() const
Number of audio channels.
int sampleRate() const
Sample rate of the buffer.
int samplesCount() const
Number of samples per channel.
void open(OptionalErrorCode ec=throws())
Open the codec with the parameters set so far.
void setRefCountedFrames(bool refcounted) const noexcept
Ask the codec for reference-counted frames.
void setCodec(const Codec &codec, OptionalErrorCode ec=throws())
Choose the codec without resetting its defaults.
Definition codeccontext.h:822
Codec descriptor.
Definition codec.h:32
Container context: the entry point for demuxing, muxing and seeking.
Definition formatcontext.h:153
void openInput(const std::string &uri, OptionalErrorCode ec=throws())
Open an input and probe its container.
Timestamp duration() const noexcept
Duration of the container.
Timestamp startTime() const noexcept
Start time of the container.
void substractStartTime(bool enable)
Shift timestamps so the file starts at zero.
void findStreamInfo(OptionalErrorCode ec=throws())
Read the stream parameters of an opened input.
size_t streamsCount() const
Number of streams in the container.
Stream stream(size_t idx)
Stream by index.
Packet readPacket(OptionalErrorCode ec=throws())
Read the next packet from any stream.
int refCount() const
Number of references to the frame buffer.
bool isReferenced() const
Whether the frame data is reference counted.
Timestamp pts() const
Presentation timestamp.
Demuxer descriptor.
Definition format.h:168
bool setFormat(const std::string &name) noexcept
Look a demuxer up by name and point the descriptor at it.
Compressed data and its metadata.
Definition packet.h:110
const Rational & timeBase() const
Time base of pts() and dts().
Definition packet.h:466
Timestamp pts() const
Presentation timestamp.
int streamIndex() const
Index of the stream the packet belongs to.
One stream inside a container.
Definition stream.h:34
bool isValid() const
Whether the view refers to a stream.
double seconds() const noexcept
The timestamp in seconds.
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
Codec findDecodingCodec(AVCodecID id)
Find a decoder by codec identifier.
void init()
Initialize all subsystems the library needs.
constexpr auto NoPts
Timestamp value meaning "no timestamp".
Definition avutils.h:55
STL namespace.
Compressed packets and their side data.
const T * raw() const
Read access to the wrapped object.
Definition ffmpeg.h:56
bool isNull() const
Whether the wrapper is empty.
Definition ffmpeg.h:77
Video scaling and pixel format conversion with libswscale.