Try a different approach to frame duplication in the decoder.
authorCarl Hetherington <cth@carlh.net>
Wed, 31 Oct 2012 11:46:05 +0000 (11:46 +0000)
committerCarl Hetherington <cth@carlh.net>
Wed, 31 Oct 2012 11:46:05 +0000 (11:46 +0000)
13 files changed:
src/lib/dcp_video_frame.cc
src/lib/dcp_video_frame.h
src/lib/decoder.cc
src/lib/decoder.h
src/lib/encoder.h
src/lib/ffmpeg_decoder.cc
src/lib/ffmpeg_decoder.h
src/lib/imagemagick_encoder.cc
src/lib/imagemagick_encoder.h
src/lib/j2k_still_encoder.cc
src/lib/j2k_still_encoder.h
src/lib/j2k_wav_encoder.cc
src/lib/j2k_wav_encoder.h

index 201c74ec173ca3e3a241e07429000c0120d9c29c..9d6022efa52ca8cb6866c1538f9b387198d33550 100644 (file)
@@ -74,7 +74,7 @@ using boost::shared_ptr;
  *  @param l Log to write to.
  */
 DCPVideoFrame::DCPVideoFrame (
-       shared_ptr<Image> yuv, shared_ptr<Subtitle> sub,
+       shared_ptr<const Image> yuv, shared_ptr<Subtitle> sub,
        Size out, int p, int subtitle_offset, float subtitle_scale,
        Scaler const * s, int f, float fps, string pp, int clut, int bw, Log* l
        )
index 4e9a777bd78ec909c4c0bd303b289880c5c0cfe4..0ff29f7bfd1d803e885fb7f28daa5b6e470f813d 100644 (file)
@@ -106,7 +106,7 @@ public:
 class DCPVideoFrame
 {
 public:
-       DCPVideoFrame (boost::shared_ptr<Image>, boost::shared_ptr<Subtitle>, Size, int, int, float, Scaler const *, int, float, std::string, int, int, Log *);
+       DCPVideoFrame (boost::shared_ptr<const Image>, boost::shared_ptr<Subtitle>, Size, int, int, float, Scaler const *, int, float, std::string, int, int, Log *);
        virtual ~DCPVideoFrame ();
 
        boost::shared_ptr<EncodedData> encode_locally ();
@@ -120,7 +120,7 @@ private:
        void create_openjpeg_container ();
        void write_encoded (boost::shared_ptr<const Options>, uint8_t *, int);
 
-       boost::shared_ptr<Image> _input; ///< the input image
+       boost::shared_ptr<const Image> _input; ///< the input image
        boost::shared_ptr<Subtitle> _subtitle; ///< any subtitle that should be on the image
        Size _out_size;                  ///< the required size of the output, in pixels
        int _padding;
index abb4d13a1ec404daccdb0f7ef21f6bf94a70234e..e154aac5d10b3f3fd2d3b878798eb776a7337afc 100644 (file)
@@ -310,11 +310,21 @@ Decoder::process_video (AVFrame* frame)
                }
                
                TIMING ("Decoder emits %1", _video_frame_index);
-               Video ((*i), _video_frame_index, sub);
+               Video (*i, _video_frame_index, sub);
                ++_video_frame_index;
+               _last_image = *i;
+               _last_subtitle = sub;
        }
 }
 
+void
+Decoder::repeat_last_video ()
+{
+       assert (_last_image);
+       Video (_last_image, _video_frame_index, _last_subtitle);
+       ++_video_frame_index;
+}
+
 void
 Decoder::process_subtitle (shared_ptr<TimedSubtitle> s)
 {
index 2af9157b4da6175ae78c5f666ee9763f05afcf0e..6cd7757b6562c05a36ee099f8b57b4a4769f5f39 100644 (file)
@@ -112,6 +112,7 @@ protected:
        void process_video (AVFrame *);
        void process_audio (uint8_t *, int);
        void process_subtitle (boost::shared_ptr<TimedSubtitle>);
+       void repeat_last_video ();
 
        int bytes_per_audio_sample () const;
        
@@ -147,6 +148,9 @@ private:
        int64_t _audio_frames_processed;
 
        boost::shared_ptr<TimedSubtitle> _timed_subtitle;
+
+       boost::shared_ptr<Image> _last_image;
+       boost::shared_ptr<Subtitle> _last_subtitle;
 };
 
 #endif
index 4d741a92bc4ff1768a6ee59adb451c4b58047f3c..590b8aaa9a25e8189102d5105fbefc6a599507bc 100644 (file)
@@ -61,7 +61,7 @@ public:
         *  @param f Frame number within the film.
         *  @param s A subtitle that should be on this frame, or 0.
         */
-       virtual void process_video (boost::shared_ptr<Image> i, int f, boost::shared_ptr<Subtitle> s) = 0;
+       virtual void process_video (boost::shared_ptr<const Image> i, int f, boost::shared_ptr<Subtitle> s) = 0;
 
        /** Called with some audio data.
         *  @param d Array of pointers to floating point sample data for each channel.
index a9db6f4893bae07c19daa54b8267f1df3836420c..47cc02e6165a96b3dff5b49659d9c897428171c8 100644 (file)
@@ -61,9 +61,7 @@ FFmpegDecoder::FFmpegDecoder (shared_ptr<Film> f, shared_ptr<const Options> o, J
        , _video_stream (-1)
        , _audio_stream (-1)
        , _subtitle_stream (-1)
-       , _last_video_frame (-1)
-       , _this_video_frame (0)
-       , _audio_frame (0)
+       , _frame (0)
        , _video_codec_context (0)
        , _video_codec (0)
        , _audio_codec_context (0)
@@ -71,10 +69,6 @@ FFmpegDecoder::FFmpegDecoder (shared_ptr<Film> f, shared_ptr<const Options> o, J
        , _subtitle_codec_context (0)
        , _subtitle_codec (0)
 {
-       for (int i = 0; i < 2; ++i) {
-               _video_frame[i] = 0;
-       }
-       
        setup_general ();
        setup_video ();
        setup_audio ();
@@ -95,11 +89,7 @@ FFmpegDecoder::~FFmpegDecoder ()
                avcodec_close (_subtitle_codec_context);
        }
 
-       for (int i = 0; i < 2; ++i) {
-               av_free (_video_frame[i]);
-       }
-
-       av_free (_audio_frame);
+       av_free (_frame);
        
        avformat_close_input (&_format_context);
 }      
@@ -152,15 +142,8 @@ FFmpegDecoder::setup_general ()
                throw DecodeError ("could not find video stream");
        }
 
-       for (int i = 0; i < 2; ++i) {
-               _video_frame[i] = avcodec_alloc_frame ();
-               if (_video_frame[i] == 0) {
-                       throw DecodeError ("could not allocate frame");
-               }
-       }
-
-       _audio_frame = avcodec_alloc_frame ();
-       if (_audio_frame == 0) {
+       _frame = avcodec_alloc_frame ();
+       if (_frame == 0) {
                throw DecodeError ("could not allocate frame");
        }
 }
@@ -253,18 +236,18 @@ FFmpegDecoder::do_pass ()
 
                int frame_finished;
 
-               while (avcodec_decode_video2 (_video_codec_context, _video_frame[_this_video_frame], &frame_finished, &_packet) >= 0 && frame_finished) {
-                       process_video (_video_frame[_this_video_frame]);
+               while (avcodec_decode_video2 (_video_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
+                       process_video (_frame);
                }
 
                if (_audio_stream >= 0 && _opt->decode_audio) {
-                       while (avcodec_decode_audio4 (_audio_codec_context, _audio_frame, &frame_finished, &_packet) >= 0 && frame_finished) {
+                       while (avcodec_decode_audio4 (_audio_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
                                int const data_size = av_samples_get_buffer_size (
-                                       0, _audio_codec_context->channels, _audio_frame->nb_samples, audio_sample_format (), 1
+                                       0, _audio_codec_context->channels, _frame->nb_samples, audio_sample_format (), 1
                                        );
 
                                assert (_audio_codec_context->channels == _film->audio_channels());
-                               process_audio (_audio_frame->data[0], data_size);
+                               process_audio (_frame->data[0], data_size);
                        }
                }
 
@@ -273,16 +256,16 @@ FFmpegDecoder::do_pass ()
 
        double const pts_seconds = av_q2d (_format_context->streams[_packet.stream_index]->time_base) * _packet.pts;
 
+       avcodec_get_frame_defaults (_frame);
+       
        if (_packet.stream_index == _video_stream) {
 
-               avcodec_get_frame_defaults (_video_frame[_this_video_frame]);
-               
                if (!_first_video) {
                        _first_video = pts_seconds;
                }
 
                int frame_finished;
-               if (avcodec_decode_video2 (_video_codec_context, _video_frame[_this_video_frame], &frame_finished, &_packet) >= 0 && frame_finished) {
+               if (avcodec_decode_video2 (_video_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
 
                        /* Where we are in the output, in seconds */
                        double const out_pts_seconds = video_frame_index() / frames_per_second();
@@ -291,29 +274,27 @@ FFmpegDecoder::do_pass ()
                        double const delta = pts_seconds - out_pts_seconds;
                        double const one_frame = 1 / frames_per_second();
 
-                       /* Insert the last frame if we have one, otherwise just use this one */
-                       int const insert_frame = _last_video_frame == -1 ? _this_video_frame : _last_video_frame;
-
                        /* Insert frames if required to get out_pts_seconds up to pts_seconds */
                        if (delta > one_frame) {
                                int const extra = rint (delta / one_frame);
                                for (int i = 0; i < extra; ++i) {
-                                       _film->log()->log (String::compose ("Extra frame inserted at %1s", out_pts_seconds));
-                                       process_video (_video_frame[insert_frame]);
+                                       repeat_last_video ();
+                                       _film->log()->log (
+                                               String::compose (
+                                                       "Extra frame inserted at %1s; DCP frame %2, packet PTS %3",
+                                                       out_pts_seconds, video_frame_index(), pts_seconds
+                                                       )
+                                               );
                                }
                        }
 
                        if (delta > -one_frame) {
                                /* Process this frame */
-                               process_video (_video_frame[_this_video_frame]);
+                               process_video (_frame);
                        } else {
                                /* Otherwise we are omitting a frame to keep things right */
                                _film->log()->log (String::compose ("Frame removed at %1s", out_pts_seconds));
                        }
-
-                       /* Swap over so that we use the alternate video frames next time */
-                       _this_video_frame = 1 - _this_video_frame;
-                       _last_video_frame = 1 - _this_video_frame;
                }
 
        } else if (_audio_stream >= 0 && _packet.stream_index == _audio_stream && _opt->decode_audio && _first_video && _first_video.get() <= pts_seconds) {
@@ -354,13 +335,13 @@ FFmpegDecoder::do_pass ()
                }
                
                int frame_finished;
-               if (avcodec_decode_audio4 (_audio_codec_context, _audio_frame, &frame_finished, &_packet) >= 0 && frame_finished) {
+               if (avcodec_decode_audio4 (_audio_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
                        int const data_size = av_samples_get_buffer_size (
-                               0, _audio_codec_context->channels, _audio_frame->nb_samples, audio_sample_format (), 1
+                               0, _audio_codec_context->channels, _frame->nb_samples, audio_sample_format (), 1
                                );
                        
                        assert (_audio_codec_context->channels == _film->audio_channels());
-                       process_audio (_audio_frame->data[0], data_size);
+                       process_audio (_frame->data[0], data_size);
                }
                        
        } else if (_subtitle_stream >= 0 && _packet.stream_index == _subtitle_stream && _opt->decode_subtitles && _first_video) {
index 58e08a7797a005eeaefab25e8fa5553ee6c15bf2..c04dba5b3cbd0be52b68e46282403b74bbaee01d 100644 (file)
@@ -90,10 +90,7 @@ private:
        int _audio_stream; ///< may be < 0 if there is no audio
        int _subtitle_stream; ///< may be < 0 if there is no subtitle
        
-       AVFrame* _video_frame[2];
-       int _last_video_frame;
-       int _this_video_frame;
-       AVFrame* _audio_frame;
+       AVFrame* _frame;
 
        std::vector<AudioStream> _audio_streams;
        std::vector<SubtitleStream> _subtitle_streams;
index 8d42b3dbea1bc9728bab9838ff4ad0d08f48eba4..e8b060997fb3dd71ee5ab84419be186b70185d9b 100644 (file)
@@ -50,7 +50,7 @@ ImageMagickEncoder::ImageMagickEncoder (shared_ptr<const Film> f, shared_ptr<con
 }
 
 void
-ImageMagickEncoder::process_video (shared_ptr<Image> image, int frame, shared_ptr<Subtitle> sub)
+ImageMagickEncoder::process_video (shared_ptr<const Image> image, int frame, shared_ptr<Subtitle> sub)
 {
        shared_ptr<Image> scaled = image->scale_and_convert_to_rgb (_opt->out_size, _opt->padding, _film->scaler());
        shared_ptr<Image> compact (new CompactImage (scaled));
index f5e97ccd82a6b1049d76c49e80d49afe25041ed4..c112300e3e5488d73ca2f5e901451ad818fd3e4f 100644 (file)
@@ -37,7 +37,7 @@ public:
        ImageMagickEncoder (boost::shared_ptr<const Film> f, boost::shared_ptr<const Options> o);
 
        void process_begin (int64_t audio_channel_layout) {}
-       void process_video (boost::shared_ptr<Image>, int, boost::shared_ptr<Subtitle>);
+       void process_video (boost::shared_ptr<const Image>, int, boost::shared_ptr<Subtitle>);
        void process_audio (boost::shared_ptr<const AudioBuffers>) {}
        void process_end () {}
 };
index 56fbb3152aeb8344aba40a919e2fa2b786d5ec39..edcf0de898e0765c8683ab85594ea80999bfb799 100644 (file)
@@ -49,7 +49,7 @@ J2KStillEncoder::J2KStillEncoder (shared_ptr<const Film> f, shared_ptr<const Opt
 }
 
 void
-J2KStillEncoder::process_video (shared_ptr<Image> yuv, int frame, shared_ptr<Subtitle> sub)
+J2KStillEncoder::process_video (shared_ptr<const Image> yuv, int frame, shared_ptr<Subtitle> sub)
 {
        pair<string, string> const s = Filter::ffmpeg_strings (_film->filters());
        DCPVideoFrame* f = new DCPVideoFrame (
index 433c497a69b381d8e1196e9c9e8b54bfa3374280..86bebc0d27a2f0b18e5eb1a7795bc6aebf6af2b6 100644 (file)
@@ -37,7 +37,7 @@ public:
        J2KStillEncoder (boost::shared_ptr<const Film>, boost::shared_ptr<const Options>);
 
        void process_begin (int64_t audio_channel_layout) {}
-       void process_video (boost::shared_ptr<Image>, int, boost::shared_ptr<Subtitle>);
+       void process_video (boost::shared_ptr<const Image>, int, boost::shared_ptr<Subtitle>);
        void process_audio (boost::shared_ptr<const AudioBuffers>) {}
        void process_end () {}
 };
index 73a70910e9f9f631f3f3109e3c317bbd27694610..bac7d5f35c63e17d2f66d285c3453bde8e56c652 100644 (file)
@@ -106,7 +106,7 @@ J2KWAVEncoder::close_sound_files ()
 }      
 
 void
-J2KWAVEncoder::process_video (shared_ptr<Image> yuv, int frame, shared_ptr<Subtitle> sub)
+J2KWAVEncoder::process_video (shared_ptr<const Image> yuv, int frame, shared_ptr<Subtitle> sub)
 {
        boost::mutex::scoped_lock lock (_worker_mutex);
 
index 95a802b6e531cb9b6db9031b8ed4e1afb160ff83..99924829d2c8207f4454728e8dc4fe2158f85f8a 100644 (file)
@@ -51,7 +51,7 @@ public:
        ~J2KWAVEncoder ();
 
        void process_begin (int64_t audio_channel_layout);
-       void process_video (boost::shared_ptr<Image>, int, boost::shared_ptr<Subtitle>);
+       void process_video (boost::shared_ptr<const Image>, int, boost::shared_ptr<Subtitle>);
        void process_audio (boost::shared_ptr<const AudioBuffers>);
        void process_end ();