MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ open()

bool MayaFlux::IO::AudioStreamContext::open ( const FFmpegDemuxContext demux,
bool  planar_output = false,
uint32_t  target_rate = 0 
)

Open the audio stream from an already-probed demux context.

Finds the best audio stream, allocates and opens the codec context, caches audio parameters, and initialises the SwrContext for conversion to AV_SAMPLE_FMT_DBL (or AV_SAMPLE_FMT_DBLP if planar output requested).

Parameters
demuxOpen demux context (must outlive this object).
planar_outputIf true, configure swr for planar double output.
target_rateResample target in Hz; 0 = keep source rate.
Returns
True on success.

Definition at line 44 of file AudioStreamContext.cpp.

47{
48 close();
50
51 if (!demux.is_open()) {
52 m_last_error = "Demux context is not open";
53 return false;
54 }
55
56 const AVCodec* codec = nullptr;
57 stream_index = demux.find_best_stream(AVMEDIA_TYPE_AUDIO,
58 reinterpret_cast<const void**>(&codec));
59 if (stream_index < 0 || !codec) {
60 m_last_error = "No audio stream found";
61 return false;
62 }
63
64 codec_context = avcodec_alloc_context3(codec);
65 if (!codec_context) {
66 m_last_error = "avcodec_alloc_context3 failed";
67 return false;
68 }
69
70 AVStream* stream = demux.get_stream(stream_index);
71 if (avcodec_parameters_to_context(codec_context, stream->codecpar) < 0) {
72 m_last_error = "avcodec_parameters_to_context failed";
73 close();
74 return false;
75 }
76
77 if (avcodec_open2(codec_context, codec, nullptr) < 0) {
78 m_last_error = "avcodec_open2 failed";
79 close();
80 return false;
81 }
82
83 sample_rate = static_cast<uint32_t>(codec_context->sample_rate);
84 channels = static_cast<uint32_t>(codec_context->ch_layout.nb_channels);
85
86 if (stream->duration > 0) {
87 total_frames = av_rescale_q(stream->duration, stream->time_base,
88 AVRational { 1, (int)sample_rate });
89 } else if (demux.format_context->duration != AV_NOPTS_VALUE) {
90 double dur = (double)demux.format_context->duration / AV_TIME_BASE;
91 total_frames = static_cast<uint64_t>(dur * sample_rate);
92 }
93
94 if (!setup_resampler(planar_output, target_rate)) {
95 close();
96 return false;
97 }
98
100
101 return true;
102}
void drain_resampler_init()
Drain any samples buffered inside the resampler.
AVCodecContext * codec_context
Owned; freed in destructor.
bool setup_resampler(bool planar_output, uint32_t target_rate)
void close()
Release codec and resampler resources.
static void init_ffmpeg()
Initialise FFmpeg logging level once per process.

References channels, close(), codec_context, drain_resampler_init(), MayaFlux::IO::FFmpegDemuxContext::find_best_stream(), MayaFlux::IO::FFmpegDemuxContext::format_context, MayaFlux::IO::FFmpegDemuxContext::get_stream(), MayaFlux::IO::FFmpegDemuxContext::init_ffmpeg(), MayaFlux::IO::FFmpegDemuxContext::is_open(), m_last_error, sample_rate, setup_resampler(), stream_index, and total_frames.

+ Here is the call graph for this function: