From d6fa7c8f5c89f195490a2c3bb91ca486179c3eef Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Tue, 4 May 2021 23:20:02 +0200 Subject: Refer to Piece audio streams by index instead of pointer. --- src/lib/piece.cc | 92 ++++++++++++++++++++++++++------------------------- src/lib/piece.h | 18 +++++++--- src/lib/piece_audio.h | 10 +++--- src/lib/player.cc | 4 +-- 4 files changed, 68 insertions(+), 56 deletions(-) diff --git a/src/lib/piece.cc b/src/lib/piece.cc index b1b4decba..7590698ab 100644 --- a/src/lib/piece.cc +++ b/src/lib/piece.cc @@ -56,9 +56,8 @@ Piece::Piece (weak_ptr film, shared_ptr content, shared_ptr , _fast (fast) { if (_content->audio) { - for (auto j: _content->audio->streams()) { - _stream_last_push_end[j] = _content->position(); - _positions[j] = 0; + for (auto const& i: content->audio->streams()) { + _audio_streams.push_back(Stream(_content->position(), i->mapping())); } } @@ -92,32 +91,34 @@ Piece::video (shared_ptr image, Frame frame, Eyes eyes, Part p void -Piece::audio (AudioStreamPtr stream, shared_ptr audio, Frame frame) +Piece::audio (AudioStreamPtr stream_ptr, shared_ptr audio, Frame frame) { auto film = _film.lock (); DCPOMATIC_ASSERT (film); + auto content_streams = _content->audio->streams(); + auto content_stream_iter = std::find(content_streams.begin(), content_streams.end(), stream_ptr); + DCPOMATIC_ASSERT (content_stream_iter != content_streams.end()); + int index = std::distance(content_streams.begin(), content_stream_iter); + DCPOMATIC_ASSERT (index >= 0 && index < static_cast(_audio_streams.size())); + auto& stream = _audio_streams[index]; + int const resampled_rate = _content->audio->resampled_frame_rate(film); - shared_ptr resampler; - auto i = _resamplers.find(stream); - if (i != _resamplers.end()) { - resampler = i->second; - } else { - if (stream->frame_rate() != resampled_rate) { - LOG_GENERAL ( - "Creating new resampler from %1 to %2 with %3 channels", - stream->frame_rate(), - resampled_rate, - stream->channels() - ); - - resampler = make_shared(stream->frame_rate(), resampled_rate, stream->channels()); - if (_fast) { - resampler->set_fast (); - } - _resamplers[stream] = resampler; + auto resampler = stream.resampler; + if (!resampler && stream_ptr->frame_rate() != resampled_rate) { + LOG_GENERAL ( + "Creating new resampler from %1 to %2 with %3 channels", + stream_ptr->frame_rate(), + resampled_rate, + stream_ptr->channels() + ); + + resampler = make_shared(stream_ptr->frame_rate(), resampled_rate, stream_ptr->channels()); + if (_fast) { + resampler->set_fast (); } + stream.resampler = resampler; } if (resampler) { @@ -128,13 +129,13 @@ Piece::audio (AudioStreamPtr stream, shared_ptr audio, Frame audio = ro; } - if (_positions[stream] == 0) { - _positions[stream] = frame; + if (stream.position == 0) { + stream.position = frame; } - auto const pos = _positions[stream]; - Audio (PieceAudio(stream, audio, pos, resampled_audio_to_dcp(pos))); - _positions[stream] += audio->frames(); + auto const pos = stream.position; + Audio (PieceAudio(index, audio, pos, resampled_audio_to_dcp(pos), stream_ptr->mapping())); + stream.position += audio->frames(); } @@ -169,21 +170,21 @@ Piece::atmos (shared_ptr data, Frame frame, AtmosMetadata void Piece::update_pull_to (DCPTime& pull_to) const { - if (done()) { + if (done() || _audio_streams.empty()) { return; } - for (auto const& i: _stream_last_push_end) { - pull_to = std::min(pull_to, i.second); + for (auto const& i: _audio_streams) { + pull_to = std::min(pull_to, i.last_push_end); } } void -Piece::set_last_push_end (AudioStreamPtr stream, DCPTime end) +Piece::set_last_push_end (int stream, DCPTime end) { - DCPOMATIC_ASSERT (_stream_last_push_end.find(stream) != _stream_last_push_end.end()); - _stream_last_push_end[stream] = end; + DCPOMATIC_ASSERT (stream >= 0 && stream < static_cast(_audio_streams.size())); + _audio_streams[stream].last_push_end = end; } @@ -341,13 +342,12 @@ Piece::reference_dcp_audio () const void Piece::seek (DCPTime time, bool accurate) { - for (auto i: _resamplers) { - i.second->flush (); - i.second->reset (); - } - - for (auto& i: _positions) { - i.second = 0; + for (auto& i: _audio_streams) { + if (i.resampler) { + i.resampler->flush (); + i.resampler->reset (); + } + i.position = 0; } if (time < position()) { @@ -407,13 +407,15 @@ Piece::period () const void Piece::flush () { - for (auto const& i: _resamplers) { - auto ro = i.second->flush (); + int index = 0; + for (auto& i: _audio_streams) { + auto ro = i.resampler->flush (); if (ro->frames() > 0) { - auto const frame = _positions[i.first]; - Audio (PieceAudio(i.first, ro, frame, resampled_audio_to_dcp(frame))); - _positions[i.first] += ro->frames(); + auto const frame = i.position; + Audio (PieceAudio(index, ro, frame, resampled_audio_to_dcp(frame), i.mapping)); + i.position += ro->frames(); } + ++index; } } diff --git a/src/lib/piece.h b/src/lib/piece.h index 1d50cb204..7abacbef7 100644 --- a/src/lib/piece.h +++ b/src/lib/piece.h @@ -51,7 +51,7 @@ public: Piece (std::weak_ptr film, std::shared_ptr content, std::shared_ptr decoder, FrameRateChange frc, bool fast); void update_pull_to (dcpomatic::DCPTime& pull_to) const; - void set_last_push_end (AudioStreamPtr stream, dcpomatic::DCPTime last_push_end); + void set_last_push_end (int stream, dcpomatic::DCPTime last_push_end); boost::optional content_time_to_dcp (std::shared_ptr content, dcpomatic::ContentTime t) const; @@ -118,11 +118,21 @@ private: FrameRateChange _frc; bool _fast = false; boost::optional _ignore_video; - std::map _stream_last_push_end; - std::map> _resamplers; - std::map _positions; + struct Stream + { + Stream (dcpomatic::DCPTime lpe, AudioMapping m) + : last_push_end(lpe) + , mapping(m) + {} + dcpomatic::DCPTime last_push_end; + std::shared_ptr resampler; + Frame position = 0; + AudioMapping mapping; + }; + + std::vector _audio_streams; }; diff --git a/src/lib/piece_audio.h b/src/lib/piece_audio.h index 876591c5b..e29cafd03 100644 --- a/src/lib/piece_audio.h +++ b/src/lib/piece_audio.h @@ -29,12 +29,10 @@ #include "audio_buffers.h" +#include "audio_mapping.h" #include "types.h" -class AudioStream; - - /** @class PieceAudio * @brief A block of audio from a Piece, with a timestamp as a frame within that piece. */ @@ -45,17 +43,19 @@ public: : audio (new AudioBuffers(0, 0)) {} - PieceAudio (std::shared_ptr s, std::shared_ptr a, Frame f, dcpomatic::DCPTime t) + PieceAudio (int s, std::shared_ptr a, Frame f, dcpomatic::DCPTime t, AudioMapping m) : stream (s) , audio (a) , frame (f) , time (t) + , mapping (m) {} - std::shared_ptr stream; + int stream; ///< index of stream within the Piece std::shared_ptr audio; Frame frame = 0; dcpomatic::DCPTime time; + AudioMapping mapping; }; diff --git a/src/lib/player.cc b/src/lib/player.cc index 7a4a9c2c7..2fa6805ba 100644 --- a/src/lib/player.cc +++ b/src/lib/player.cc @@ -947,12 +947,12 @@ Player::audio (weak_ptr wp, PieceAudio audio) /* Remap */ - audio.audio = remap (audio.audio, _film->audio_channels(), audio.stream->mapping()); + audio.audio = remap (audio.audio, _film->audio_channels(), audio.mapping); /* Process */ if (_audio_processor) { - audio.audio = _audio_processor->run (audio.audio, _film->audio_channels ()); + audio.audio = _audio_processor->run (audio.audio, _film->audio_channels()); } /* Push */ -- cgit v1.2.3