Merge master.
[dcpomatic.git] / src / lib / ffmpeg_decoder.cc
index c8e46776f2b90db7782e56b237c357c96df5f3f6..a0949f635ffeb420a3096dfb2e740d6ef98ddaff 100644 (file)
@@ -41,7 +41,6 @@ extern "C" {
 #include "transcoder.h"
 #include "job.h"
 #include "filter.h"
-#include "options.h"
 #include "exceptions.h"
 #include "image.h"
 #include "util.h"
@@ -62,10 +61,12 @@ using boost::optional;
 using boost::dynamic_pointer_cast;
 using libdcp::Size;
 
-FFmpegDecoder::FFmpegDecoder (shared_ptr<const Film> f, shared_ptr<FFmpegContent> c, DecodeOptions o)
-       : Decoder (f, o)
-       , VideoDecoder (f, c, o)
-       , AudioDecoder (f, c, o)
+boost::mutex FFmpegDecoder::_mutex;
+
+FFmpegDecoder::FFmpegDecoder (shared_ptr<const Film> f, shared_ptr<const FFmpegContent> c, bool video, bool audio, bool subtitles, bool video_sync)
+       : Decoder (f)
+       , VideoDecoder (f)
+       , AudioDecoder (f)
        , _ffmpeg_content (c)
        , _format_context (0)
        , _video_stream (-1)
@@ -76,23 +77,29 @@ FFmpegDecoder::FFmpegDecoder (shared_ptr<const Film> f, shared_ptr<FFmpegContent
        , _audio_codec (0)
        , _subtitle_codec_context (0)
        , _subtitle_codec (0)
+       , _decode_video (video)
+       , _decode_audio (audio)
+       , _decode_subtitles (subtitles)
+       , _video_sync (video_sync)
 {
        setup_general ();
        setup_video ();
        setup_audio ();
        setup_subtitle ();
 
-       if (!o.video_sync) {
+       if (!video_sync) {
                _first_video = 0;
        }
 }
 
 FFmpegDecoder::~FFmpegDecoder ()
 {
+       boost::mutex::scoped_lock lm (_mutex);
+       
        if (_audio_codec_context) {
                avcodec_close (_audio_codec_context);
        }
-       
+
        if (_video_codec_context) {
                avcodec_close (_video_codec_context);
        }
@@ -119,7 +126,7 @@ FFmpegDecoder::setup_general ()
                throw DecodeError (_("could not find stream information"));
        }
 
-       /* Find video, audio and subtitle streams and choose the first of each */
+       /* Find video, audio and subtitle streams */
 
        for (uint32_t i = 0; i < _format_context->nb_streams; ++i) {
                AVStream* s = _format_context->streams[i];
@@ -157,6 +164,8 @@ FFmpegDecoder::setup_general ()
 void
 FFmpegDecoder::setup_video ()
 {
+       boost::mutex::scoped_lock lm (_mutex);
+       
        _video_codec_context = _format_context->streams[_video_stream]->codec;
        _video_codec = avcodec_find_decoder (_video_codec_context->codec_id);
 
@@ -172,6 +181,8 @@ FFmpegDecoder::setup_video ()
 void
 FFmpegDecoder::setup_audio ()
 {
+       boost::mutex::scoped_lock lm (_mutex);
+       
        if (!_ffmpeg_content->audio_stream ()) {
                return;
        }
@@ -191,6 +202,8 @@ FFmpegDecoder::setup_audio ()
 void
 FFmpegDecoder::setup_subtitle ()
 {
+       boost::mutex::scoped_lock lm (_mutex);
+       
        if (!_ffmpeg_content->subtitle_stream() || _ffmpeg_content->subtitle_stream()->id >= int (_format_context->nb_streams)) {
                return;
        }
@@ -230,13 +243,13 @@ FFmpegDecoder::pass ()
 
                int frame_finished;
 
-               if (_opt.decode_video) {
+               if (_decode_video) {
                        while (avcodec_decode_video2 (_video_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
                                filter_and_emit_video (_frame);
                        }
                }
 
-               if (_ffmpeg_content->audio_stream() && _opt.decode_audio) {
+               if (_ffmpeg_content->audio_stream() && _decode_audio) {
                        decode_audio_packet ();
                }
 
@@ -245,7 +258,7 @@ FFmpegDecoder::pass ()
 
        avcodec_get_frame_defaults (_frame);
 
-       if (_packet.stream_index == _video_stream && _opt.decode_video) {
+       if (_packet.stream_index == _video_stream && _decode_video) {
 
                int frame_finished;
                int const r = avcodec_decode_video2 (_video_codec_context, _frame, &frame_finished, &_packet);
@@ -255,16 +268,16 @@ FFmpegDecoder::pass ()
                                _film->log()->log (String::compose (N_("Used only %1 bytes of %2 in packet"), r, _packet.size));
                        }
 
-                       if (_opt.video_sync) {
+                       if (_video_sync) {
                                out_with_sync ();
                        } else {
                                filter_and_emit_video (_frame);
                        }
                }
 
-       } else if (_ffmpeg_content->audio_stream() && _packet.stream_index == _ffmpeg_content->audio_stream()->id && _opt.decode_audio) {
+       } else if (_ffmpeg_content->audio_stream() && _packet.stream_index == _ffmpeg_content->audio_stream()->id && _decode_audio) {
                decode_audio_packet ();
-       } else if (_ffmpeg_content->subtitle_stream() && _packet.stream_index == _ffmpeg_content->subtitle_stream()->id && _opt.decode_subtitles && _first_video) {
+       } else if (_ffmpeg_content->subtitle_stream() && _packet.stream_index == _ffmpeg_content->subtitle_stream()->id && _decode_subtitles && _first_video) {
 
                int got_subtitle;
                AVSubtitle sub;
@@ -387,7 +400,7 @@ FFmpegDecoder::deinterleave_audio (uint8_t** data, int size)
 }
 
 float
-FFmpegDecoder::frames_per_second () const
+FFmpegDecoder::video_frame_rate () const
 {
        AVStream* s = _format_context->streams[_video_stream];
 
@@ -537,7 +550,7 @@ void
 FFmpegDecoder::out_with_sync ()
 {
        /* Where we are in the output, in seconds */
-       double const out_pts_seconds = video_frame() / frames_per_second();
+       double const out_pts_seconds = video_frame() / video_frame_rate();
        
        /* Where we are in the source, in seconds */
        double const source_pts_seconds = av_q2d (_format_context->streams[_packet.stream_index]->time_base)
@@ -554,17 +567,17 @@ FFmpegDecoder::out_with_sync ()
        
        /* Difference between where we are and where we should be */
        double const delta = source_pts_seconds - _first_video.get() - out_pts_seconds;
-       double const one_frame = 1 / frames_per_second();
+       double const one_frame = 1 / video_frame_rate();
        
        /* 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) {
-                       repeat_last_video ();
+                       repeat_last_video (frame_time ());
                        _film->log()->log (
                                String::compose (
                                        N_("Extra video frame inserted at %1s; source frame %2, source PTS %3 (at %4 fps)"),
-                                       out_pts_seconds, video_frame(), source_pts_seconds, frames_per_second()
+                                       out_pts_seconds, video_frame(), source_pts_seconds, video_frame_rate()
                                        )
                                );
                }
@@ -589,7 +602,6 @@ FFmpegDecoder::film_changed (Film::Property p)
                boost::mutex::scoped_lock lm (_filter_graphs_mutex);
                _filter_graphs.clear ();
        }
-       OutputChanged ();
        break;
 
        default:
@@ -601,7 +613,7 @@ FFmpegDecoder::film_changed (Film::Property p)
 ContentVideoFrame
 FFmpegDecoder::video_length () const
 {
-       return (double(_format_context->duration) / AV_TIME_BASE) * frames_per_second();
+       return (double(_format_context->duration) / AV_TIME_BASE) * video_frame_rate();
 }
 
 double
@@ -633,9 +645,9 @@ FFmpegDecoder::decode_audio_packet ()
                           was before this packet.  Until then audio is thrown away.
                        */
                        
-                       if ((_first_video && _first_video.get() <= source_pts_seconds) || !_opt.decode_video) {
+                       if ((_first_video && _first_video.get() <= source_pts_seconds) || !_decode_video) {
                                
-                               if (!_first_audio && _opt.decode_video) {
+                               if (!_first_audio && _decode_video) {
                                        _first_audio = source_pts_seconds;
                                        
                                        /* This is our first audio frame, and if we've arrived here we must have had our