Key _next_time with the audio stream pointer.
authorCarl Hetherington <cth@carlh.net>
Mon, 1 Mar 2021 20:12:47 +0000 (21:12 +0100)
committerCarl Hetherington <cth@carlh.net>
Tue, 2 Mar 2021 14:40:18 +0000 (15:40 +0100)
src/lib/ffmpeg_decoder.cc
src/lib/ffmpeg_decoder.h

index c91a2f2e75752c37327a1c1aabad420be0096cab..9b65e56394f15372bd6123c746f6d744c99b45ec 100644 (file)
@@ -101,7 +101,9 @@ FFmpegDecoder::FFmpegDecoder (shared_ptr<const Film> film, shared_ptr<const FFmp
                text.push_back (make_shared<TextDecoder>(this, c->only_text(), ContentTime()));
        }
 
-       _next_time.resize (_format_context->nb_streams);
+       for (auto i: c->ffmpeg_audio_streams()) {
+               _next_time[i] = {};
+       }
 }
 
 
@@ -423,7 +425,7 @@ DCPOMATIC_ENABLE_WARNINGS
        _have_current_subtitle = false;
 
        for (auto& i: _next_time) {
-               i = optional<ContentTime>();
+               i.second = {};
        }
 }
 
@@ -447,7 +449,7 @@ FFmpegDecoder::audio_stream_from_index (int index) const
 
 
 void
-FFmpegDecoder::process_audio_frame (shared_ptr<FFmpegAudioStream> stream, int stream_index, int64_t packet_pts)
+FFmpegDecoder::process_audio_frame (shared_ptr<FFmpegAudioStream> stream, int64_t packet_pts)
 {
        auto data = deinterleave_audio (stream);
 
@@ -457,8 +459,8 @@ FFmpegDecoder::process_audio_frame (shared_ptr<FFmpegAudioStream> stream, int st
                   that have AV_NOPTS_VALUE we need to work out the timestamp ourselves.  This is
                   particularly noticeable with TrueHD streams (see #1111).
                   */
-               if (_next_time[stream_index]) {
-                       ct = *_next_time[stream_index];
+               if (_next_time[stream]) {
+                       ct = *_next_time[stream];
                }
        } else {
                ct = ContentTime::from_seconds (
@@ -467,7 +469,7 @@ FFmpegDecoder::process_audio_frame (shared_ptr<FFmpegAudioStream> stream, int st
                        + _pts_offset;
        }
 
-       _next_time[stream_index] = ct + ContentTime::from_frames(data->frames(), stream->frame_rate());
+       _next_time[stream] = ct + ContentTime::from_frames(data->frames(), stream->frame_rate());
 
        if (ct < ContentTime()) {
                /* Discard audio data that comes before time 0 */
@@ -482,7 +484,7 @@ FFmpegDecoder::process_audio_frame (shared_ptr<FFmpegAudioStream> stream, int st
                        "Crazy timestamp %1 for %2 samples in stream %3 packet pts %4 (ts=%5 tb=%6, off=%7)",
                        to_string(ct),
                        data->frames(),
-                       stream_index,
+                       stream->id(),
                        packet_pts,
                        _frame->best_effort_timestamp,
                        av_q2d(stream->stream(_format_context)->time_base),
@@ -500,8 +502,7 @@ FFmpegDecoder::process_audio_frame (shared_ptr<FFmpegAudioStream> stream, int st
 void
 FFmpegDecoder::decode_audio_packet (AVPacket* packet)
 {
-       int const stream_index = packet->stream_index;
-       auto stream = audio_stream_from_index (stream_index);
+       auto stream = audio_stream_from_index (packet->stream_index);
        if (!stream) {
                return;
        }
@@ -533,7 +534,7 @@ FFmpegDecoder::decode_audio_packet (AVPacket* packet)
                }
 
                if (frame_finished) {
-                       process_audio_frame (stream, stream_index, copy_packet.pts);
+                       process_audio_frame (stream, copy_packet.pts);
                }
 
                copy_packet.data += decode_result;
index 58120da33cf3433f36cc79e04658f7589c30c6da..a0a93c9a04282377ba215f24145ee75e88b6b89e 100644 (file)
@@ -58,7 +58,7 @@ private:
        int bytes_per_audio_sample (std::shared_ptr<FFmpegAudioStream> stream) const;
 
        std::shared_ptr<FFmpegAudioStream> audio_stream_from_index (int index) const;
-       void process_audio_frame (std::shared_ptr<FFmpegAudioStream> stream, int stream_index, int64_t packet_pts);
+       void process_audio_frame (std::shared_ptr<FFmpegAudioStream> stream, int64_t packet_pts);
 
        bool decode_video_packet (AVPacket* packet);
        void decode_audio_packet (AVPacket* packet);
@@ -80,5 +80,5 @@ private:
 
        std::shared_ptr<Image> _black_image;
 
-       std::vector<boost::optional<dcpomatic::ContentTime> > _next_time;
+       std::map<std::shared_ptr<FFmpegAudioStream>, boost::optional<dcpomatic::ContentTime>> _next_time;
 };