Attempt to move resampling into AudioDecoder.
authorCarl Hetherington <cth@carlh.net>
Wed, 18 Dec 2013 23:43:55 +0000 (23:43 +0000)
committerCarl Hetherington <cth@carlh.net>
Wed, 18 Dec 2013 23:43:55 +0000 (23:43 +0000)
src/lib/audio_decoder.cc
src/lib/audio_decoder.h
src/lib/ffmpeg_decoder.cc
src/lib/player.cc
src/lib/player.h

index 7ff8529c649a18c65ef6b858849d71bec42d5881..aecf396440d63153a3c1c8a9cef4130af05502a4 100644 (file)
@@ -35,12 +35,33 @@ using boost::shared_ptr;
 AudioDecoder::AudioDecoder (shared_ptr<const Film> film, shared_ptr<const AudioContent> content)
        : Decoder (film)
        , _audio_content (content)
+       , _last_audio (0)
 {
-
+       if (content->output_audio_frame_rate() != content->content_audio_frame_rate() && content->audio_channels ()) {
+               _resampler.reset (new Resampler (content->content_audio_frame_rate(), content->output_audio_frame_rate(), content->audio_channels ()));
+       }
 }
 
 void
 AudioDecoder::audio (shared_ptr<const AudioBuffers> data, ContentTime time)
 {
+       if (_resampler) {
+               data = _resampler->run (data);
+       }
+       
        _pending.push_back (shared_ptr<DecodedAudio> (new DecodedAudio (data, time)));
+       _last_audio = time + (data->frames() * TIME_HZ / _audio_content->output_audio_frame_rate());
+}
+
+void
+AudioDecoder::flush ()
+{
+       if (!_resampler) {
+               return;
+       }
+
+       shared_ptr<const AudioBuffers> b = _resampler->flush ();
+       if (b) {
+               audio (b, _last_audio);
+       }
 }
index 0cd0e9754c6c889fb77210b7109029ff50cfd1a7..a295df0ccbbf9d770b7f60afb0926ec99c56f659 100644 (file)
@@ -30,6 +30,7 @@
 #include "decoded.h"
 
 class AudioBuffers;
+class Resampler;
 
 /** @class AudioDecoder.
  *  @brief Parent class for audio decoders.
@@ -38,7 +39,7 @@ class AudioDecoder : public virtual Decoder
 {
 public:
        AudioDecoder (boost::shared_ptr<const Film>, boost::shared_ptr<const AudioContent>);
-
+       
        boost::shared_ptr<const AudioContent> audio_content () const {
                return _audio_content;
        }
@@ -46,8 +47,12 @@ public:
 protected:
 
        void audio (boost::shared_ptr<const AudioBuffers>, ContentTime);
-       
-       boost::shared_ptr<const AudioContent> _audio_content;   
+       void flush ();
+
+       boost::shared_ptr<const AudioContent> _audio_content;
+       boost::shared_ptr<Resampler> _resampler;
+       /* End time of last audio that we wrote to _pending; only used for flushing the resampler */
+       ContentTime _last_audio;
 };
 
 #endif
index 866d846db14ebbf801b165b88724b5f77370f22a..e8489f5f6cbd1ea9e2fcbbabaf58faa279e51c13 100644 (file)
@@ -141,6 +141,8 @@ FFmpegDecoder::flush ()
        if (_ffmpeg_content->audio_stream() && _decode_audio) {
                decode_audio_packet ();
        }
+
+       AudioDecoder::flush ();
 }
 
 bool
index f5bc68565935c953f076d57752574935f85e91e1..bb085af8fa6240cf24e030bef027e06890aeccfd 100644 (file)
@@ -31,7 +31,6 @@
 #include "job.h"
 #include "image.h"
 #include "ratio.h"
-#include "resampler.h"
 #include "log.h"
 #include "scaler.h"
 
@@ -113,21 +112,6 @@ Player::pass ()
                        dec->set_dcp_times ((*i)->frc.speed_up, (*i)->content->position());
                }
 
-               /* XXX: don't know what to do with this */
-#if 0          
-               if (ad->done()) {
-                       shared_ptr<AudioContent> ac = dynamic_pointer_cast<AudioContent> ((*i)->content);
-                       assert (ac);
-                       shared_ptr<Resampler> re = resampler (ac, false);
-                       if (re) {
-                               shared_ptr<const AudioBuffers> b = re->flush ();
-                               if (b->frames ()) {
-                                       process_audio (earliest, b, ac->audio_length ());
-                               }
-                       }
-               }
-#endif         
-
                if (dec && dec->dcp_time < earliest_time) {
                        earliest_piece = *i;
                        earliest_decoded = dec;
@@ -277,11 +261,6 @@ Player::emit_audio (weak_ptr<Piece> weak_piece, shared_ptr<DecodedAudio> audio)
                audio->data = gain;
        }
 
-       /* Resample */
-       if (content->content_audio_frame_rate() != content->output_audio_frame_rate()) {
-               audio->data = resampler(content, true)->run (audio->data);
-       }
-       
        if (content->trimmed (audio->dcp_time - content->position ())) {
                return;
        }
@@ -513,29 +492,6 @@ Player::set_video_container_size (libdcp::Size s)
                );
 }
 
-shared_ptr<Resampler>
-Player::resampler (shared_ptr<AudioContent> c, bool create)
-{
-       map<shared_ptr<AudioContent>, shared_ptr<Resampler> >::iterator i = _resamplers.find (c);
-       if (i != _resamplers.end ()) {
-               return i->second;
-       }
-
-       if (!create) {
-               return shared_ptr<Resampler> ();
-       }
-
-       _film->log()->log (
-               String::compose (
-                       "Creating new resampler for %1 to %2 with %3 channels", c->content_audio_frame_rate(), c->output_audio_frame_rate(), c->audio_channels()
-                       )
-               );
-       
-       shared_ptr<Resampler> r (new Resampler (c->content_audio_frame_rate(), c->output_audio_frame_rate(), c->audio_channels()));
-       _resamplers[c] = r;
-       return r;
-}
-
 void
 Player::emit_black ()
 {
index 6e3f8187f3ea4b1afd4ad95853ae8a3aebd3b6b3..35ffdcca9b7813c0bc6e03d79e61a577eb4f67c5 100644 (file)
@@ -37,7 +37,6 @@ class Playlist;
 class AudioContent;
 class Piece;
 class Image;
-class Resampler;
 
 /** @class PlayerImage
  *  @brief A wrapper for an Image which contains some pending operations; these may
@@ -116,7 +115,6 @@ private:
        void flush ();
        void emit_black ();
        void emit_silence (OutputAudioFrame);
-       boost::shared_ptr<Resampler> resampler (boost::shared_ptr<AudioContent>, bool);
        void film_changed (Film::Property);
        void update_subtitle ();
        void emit_video (boost::weak_ptr<Piece>, boost::shared_ptr<DecodedVideo>);
@@ -141,7 +139,6 @@ private:
 
        libdcp::Size _video_container_size;
        boost::shared_ptr<PlayerImage> _black_frame;
-       std::map<boost::shared_ptr<AudioContent>, boost::shared_ptr<Resampler> > _resamplers;
 
        struct {
                boost::weak_ptr<Piece> piece;