]> git.sesse.net Git - casparcg/blobdiff - modules/ffmpeg/producer/audio/audio_decoder.cpp
[ffmpeg] Copied flush logic when seeking from 2.0, as well as current frame in clip...
[casparcg] / modules / ffmpeg / producer / audio / audio_decoder.cpp
index f94a39c35efed8f068b7760afd81b19921b5123f..e92a7c18134859e05370fc65b13e6fb18a5d720e 100644 (file)
@@ -62,8 +62,9 @@ struct audio_decoder::impl : boost::noncopyable
        core::monitor::subject                                                                          monitor_subject_;
        input&                                                                                                          input_;
        int                                                                                                                     index_;
+       int                                                                                                                     actual_index_;
        const core::video_format_desc                                                           format_desc_;
-       const spl::shared_ptr<AVCodecContext>                                           codec_context_          = open_codec(input_.context(), AVMEDIA_TYPE_AUDIO, index_, false);
+       const spl::shared_ptr<AVCodecContext>                                           codec_context_          = open_codec(input_.context(), AVMEDIA_TYPE_AUDIO, actual_index_, false);
 
        std::shared_ptr<SwrContext>                                                                     swr_                            {
                                                                                                                                                                                swr_alloc_set_opts(
@@ -80,16 +81,19 @@ struct audio_decoder::impl : boost::noncopyable
                                                                                                                                                                        };
 
        cache_aligned_vector<uint8_t>                                                           buffer_;
-       core::audio_channel_layout                                                                      channel_layout_;
 
        std::shared_ptr<AVPacket>                                                                       current_packet_;
        
 public:
-       explicit impl(input& in, const core::video_format_desc& format_desc, const std::wstring& channel_layout_spec) 
+       explicit impl(
+                       input& in,
+                       const core::video_format_desc& format_desc,
+                       int audio_stream_index)
                : input_(in)
+               , index_(audio_stream_index)
+               , actual_index_(input_.get_actual_audio_stream_index(index_))
                , format_desc_(format_desc)
                , buffer_(480000 * 4)
-               , channel_layout_(get_audio_channel_layout(*codec_context_, channel_layout_spec))
        {
                if(!swr_)
                        CASPAR_THROW_EXCEPTION(bad_alloc());
@@ -99,22 +103,22 @@ public:
                
        std::shared_ptr<AVFrame> poll()
        {               
-               if(!current_packet_ && !input_.try_pop_audio(current_packet_))
+               if(!current_packet_ && !input_.try_pop_audio(current_packet_, index_))
                        return nullptr;
                
                std::shared_ptr<AVFrame> audio;
 
-               if(!current_packet_)    
-               {
-                       avcodec_flush_buffers(codec_context_.get());    
-               }
-               else if(!current_packet_->data)
+               if (!current_packet_->data)
                {
                        if(codec_context_->codec->capabilities & CODEC_CAP_DELAY)                       
                                audio = decode(*current_packet_);
                        
-                       if(!audio)
+                       if (!audio)
+                       {
+                               avcodec_flush_buffers(codec_context_.get());
                                current_packet_.reset();
+                               audio = flush();
+                       }
                }
                else
                {
@@ -177,12 +181,11 @@ public:
        }
 };
 
-audio_decoder::audio_decoder(input& input, const core::video_format_desc& format_desc, const std::wstring& channel_layout_spec) : impl_(new impl(input, format_desc, channel_layout_spec)){}
+audio_decoder::audio_decoder(input& input, const core::video_format_desc& format_desc, int audio_stream_index) : impl_(new impl(input, format_desc, audio_stream_index)){}
 audio_decoder::audio_decoder(audio_decoder&& other) : impl_(std::move(other.impl_)){}
 audio_decoder& audio_decoder::operator=(audio_decoder&& other){impl_ = std::move(other.impl_); return *this;}
 std::shared_ptr<AVFrame> audio_decoder::operator()(){return impl_->poll();}
 uint32_t audio_decoder::nb_frames() const{return impl_->nb_frames();}
-const core::audio_channel_layout& audio_decoder::channel_layout() const { return impl_->channel_layout_; }
 std::wstring audio_decoder::print() const{return impl_->print();}
 core::monitor::subject& audio_decoder::monitor_output() { return impl_->monitor_subject_;}