2#include <ossia/detail/config.hpp>
4#include <ossia/audio/audio_parameter.hpp>
5#include <ossia/dataflow/audio_stretch_mode.hpp>
6#include <ossia/dataflow/graph_node.hpp>
7#include <ossia/dataflow/nodes/media.hpp>
8#include <ossia/dataflow/nodes/sound.hpp>
9#include <ossia/dataflow/port.hpp>
10#include <ossia/detail/libav.hpp>
11#include <ossia/detail/pod_vector.hpp>
18#include <libavcodec/avcodec.h>
19#include <libavformat/avformat.h>
20#include <libavutil/frame.h>
21#include <libavutil/mem.h>
22#include <libswresample/swresample.h>
27class sound_libav final :
public ossia::sound_node
34 : packet{av_packet_alloc()}
35 , frame{av_frame_alloc()}
37 m_outlets.push_back(&audio_out);
44 av_frame_free(&frame);
45 av_packet_free(&packet);
48 std::string label() const noexcept
override {
return "sound_libav"; }
50 void set_start(std::size_t v) { start = v; }
52 void set_upmix(std::size_t v) { upmix = v; }
54 void set_sound(libav_handle hdl)
58 m_handle = std::move(hdl);
61 m_channel_q = boost::circular_buffer<float>(
62 m_handle ? 8192 * m_handle.channels() : 0);
67 void transport(time_value flicks)
override { transport_scaled(flicks, 0.); }
69 void transport(time_value flicks,
const ossia::tick_transport_info& tinfo)
override
71 transport_scaled(flicks, tinfo.current_tempo);
75 void transport_scaled(time_value flicks,
double timeline_tempo)
79 m_resampler.transport(
80 file_sample_for_model_time(flicks, timeline_tempo, m_handle.out_rate()));
85 bool decode_one(std::optional<int64_t>& pts)
noexcept
87 auto fmt_ctx = m_handle.format;
88 auto codec_ctx = m_handle.codec;
89 auto stream = m_handle.stream;
90 const std::size_t channels = this->channels();
91 if(!fmt_ctx || !codec_ctx || !stream || channels == 0)
97 int ret = avcodec_receive_frame(codec_ctx, frame);
100 const int in_samples = frame->nb_samples;
101 if(frame->best_effort_timestamp != AV_NOPTS_VALUE)
102 pts = frame->best_effort_timestamp;
104 const int capacity = m_handle.out_capacity_for(in_samples);
105 m_tmp.resize(std::size_t(capacity) * channels, boost::container::default_init);
106 float* out_ptr = m_tmp.data();
107 const int out_samples = swr_convert(
108 m_handle.resample, (uint8_t**)&out_ptr, capacity,
109 (
const uint8_t**)frame->extended_data, in_samples);
112 push_window(out_ptr, out_samples);
115 if(ret != AVERROR(EAGAIN))
121 av_packet_unref(packet);
122 ret = av_read_frame(fmt_ctx, packet);
123 while(ret >= 0 && packet->stream_index != stream->index)
125 av_packet_unref(packet);
126 ret = av_read_frame(fmt_ctx, packet);
133 avcodec_send_packet(codec_ctx,
nullptr);
135 else if(avcodec_send_packet(codec_ctx, packet) < 0)
143 void push_window(
const float* data, int64_t frames)
noexcept
145 const int64_t channels = int64_t(this->channels());
146 const int64_t capacity = int64_t(m_channel_q.capacity()) / channels;
147 const int64_t held = int64_t(m_channel_q.size()) / channels;
149 if(frames >= capacity)
152 const int64_t keep = capacity;
154 m_window_start += held + (frames - keep);
156 m_channel_q.end(), data + (frames - keep) * channels,
157 data + frames * channels);
161 const int64_t overflow = std::max<int64_t>(0, held + frames - capacity);
164 m_channel_q.erase_begin(std::size_t(overflow * channels));
165 m_window_start += overflow;
167 m_channel_q.insert(m_channel_q.end(), data, data + frames * channels);
171 bool seek_window(int64_t frame)
noexcept
175 m_positioned =
false;
178 const int64_t orate = m_handle.out_rate();
179 if(orate <= 0 || !m_handle.stream)
183 const int64_t flicks = int64_t(
184 std::llround(ossia::flicks_per_second<double> *
double(frame) /
double(orate)));
185 if(!ossia::seek_to_flick(
186 m_handle.format, m_handle.codec, m_handle.stream, flicks,
187 AVSEEK_FLAG_BACKWARD))
190 m_handle.flush_resampler();
194 std::optional<int64_t> pts;
195 if(!decode_one(pts) || !pts)
198 const int64_t start_time = (m_handle.stream->start_time != AV_NOPTS_VALUE)
199 ? m_handle.stream->start_time
201 const int64_t pos = av_rescale_q(
202 *pts - start_time, m_handle.stream->time_base, AVRational{1, int(orate)});
208 m_window_start += pos;
214 void ensure_window(int64_t frame, int64_t count)
noexcept
216 const int64_t channels = int64_t(this->channels());
217 if(channels == 0 || count <= 0)
221 const std::size_t want = std::size_t((2 * count + 32768) * channels);
222 if(m_channel_q.capacity() < want)
223 m_channel_q.set_capacity(want);
225 const int64_t held = int64_t(m_channel_q.size()) / channels;
226 const bool in_reach = m_positioned && frame >= m_window_start
227 && frame <= m_window_start + held + count;
228 if(!in_reach && !seek_window(frame))
232 const int64_t limit = int64_t(m_channel_q.capacity()) / channels;
234 while(m_window_start + int64_t(m_channel_q.size()) / channels < frame + count)
236 std::optional<int64_t> pts;
239 if(++decoded > limit)
245 template <
typename T>
246 void read_window(int64_t frame, int64_t count, T** out)
const noexcept
248 const int64_t channels = int64_t(this->channels());
251 const int64_t held = int64_t(m_channel_q.size()) / channels;
252 const int64_t offset = frame - m_window_start;
254 for(int64_t k = 0; k < count; k++)
256 const int64_t i = offset + k;
257 if(i >= 0 && i < held)
259 auto it = m_channel_q.begin() + std::size_t(i * channels);
260 for(int64_t chan = 0; chan < channels; chan++)
261 out[chan][k] = T(*it++);
265 for(int64_t chan = 0; chan < channels; chan++)
271 template <
typename T>
273 fetch_audio(int64_t start, int64_t samples_to_write, T** audio_array_base)
noexcept
275 if(this->channels() == 0 || samples_to_write <= 0)
280 ensure_window(start, samples_to_write);
281 read_window(start, samples_to_write, audio_array_base);
284 template <
typename T>
285 void fetch_audio_backward(
286 int64_t start, int64_t samples_to_write, T** audio_array_base)
noexcept
288 const int64_t channels = int64_t(this->channels());
289 if(channels == 0 || samples_to_write <= 0)
294 const int64_t first = start - samples_to_write + 1;
296 ensure_window(std::max<int64_t>(0, first), samples_to_write);
297 read_window(first, samples_to_write, audio_array_base);
299 for(int64_t chan = 0; chan < channels; chan++)
300 std::reverse(audio_array_base[chan], audio_array_base[chan] + samples_to_write);
303 void run(
const ossia::token_request& t, ossia::exec_state_facade e)
noexcept override
308 const auto channels = m_handle.channels();
309 const auto len = int64_t(this->duration());
311 ossia::audio_port& ap = *audio_out;
312 ap.set_channels(std::max((std::size_t)upmix, (std::size_t)channels));
314 const auto [samples_to_read, samples_to_write]
315 = snd::sample_info(e.bufferSize(), e.modelToSamples(), t);
316 if(samples_to_write <= 0)
319 assert(samples_to_write > 0);
321 const auto samples_offset = t.physical_start(e.modelToSamples());
325 if(t.prev_date < m_prev_date)
330 m_prev_date = t.prev_date;
332 transport(t.prev_date);
337 if(t.prev_date > m_prev_date)
340 m_prev_date = t.prev_date;
342 transport(t.prev_date);
346 for(
int chan = 0; chan < channels; chan++)
348 ap.channel(chan).resize(e.bufferSize());
351 const double stretch_ratio = update_stretch(t, e);
352 const double abs_stretch_ratio = std::abs(stretch_ratio);
356 *
this, t, e, stretch_ratio, 1., channels, len, samples_to_read,
357 samples_to_write, samples_offset, ap);
359 const bool start_discontinuous = t.start_discontinuous || (m_last_stretch > 70.);
360 const bool end_discontinuous = t.end_discontinuous || (abs_stretch_ratio > 70.);
361 if(abs_stretch_ratio > 70. && m_last_stretch > 70.)
364 for(std::size_t i = 0; i < channels; i++)
366 ossia::snd::do_zero(ap.channel(i), samples_offset, samples_to_write);
372 for(
int chan = 0; chan < channels; chan++)
376 start_discontinuous, end_discontinuous, ap.channel(chan), samples_offset,
381 ossia::snd::perform_upmix(this->upmix, channels, ap);
382 ossia::snd::perform_start_offset(this->start, ap);
384 m_prev_date = t.date;
385 m_last_stretch = abs_stretch_ratio;
388 [[nodiscard]] std::size_t channels()
const
390 return m_handle ? m_handle.channels() : 0;
393 [[nodiscard]] std::size_t duration()
const
397 const int64_t frames = m_handle.totalPCMFrameCount();
398 const int64_t in_rate = m_handle.rate();
399 const int64_t out_rate = m_handle.out_rate();
400 if(in_rate <= 0 || out_rate <= 0 || in_rate == out_rate)
401 return std::size_t(frames);
402 return std::size_t(av_rescale_rnd(frames, out_rate, in_rate, AV_ROUND_DOWN));
406 libav_handle m_handle{};
408 ossia::audio_outlet audio_out;
413 ossia::pod_vector<float> m_tmp{};
417 boost::circular_buffer<float> m_channel_q;
418 int64_t m_window_start{};
The time_value class.
Definition ossia/editor/scenario/time_value.hpp:30