Fix a couple of audio crashes in the film viewer. Fix serialisation of SubRipContent.
[dcpomatic.git] / src / lib / ffmpeg_decoder.cc
index fff9824895565bcc854129424ce8c40133344966..4534de589693a21b7839009e9425325a821760c7 100644 (file)
@@ -136,9 +136,8 @@ FFmpegDecoder::flush ()
        
        if (_ffmpeg_content->audio_stream() && _decode_audio) {
                decode_audio_packet ();
+               AudioDecoder::flush ();
        }
-
-       AudioDecoder::flush ();
 }
 
 bool
@@ -167,9 +166,9 @@ FFmpegDecoder::pass ()
        
        if (si == _video_stream && _decode_video) {
                decode_video_packet ();
-       } else if (_ffmpeg_content->audio_stream() && si == _ffmpeg_content->audio_stream()->index (_format_context) && _decode_audio) {
+       } else if (_ffmpeg_content->audio_stream() && _ffmpeg_content->audio_stream()->uses_index (_format_context, si) && _decode_audio) {
                decode_audio_packet ();
-       } else if (_ffmpeg_content->subtitle_stream() && si == _ffmpeg_content->subtitle_stream()->index (_format_context) && film->with_subtitles ()) {
+       } else if (_ffmpeg_content->subtitle_stream() && _ffmpeg_content->subtitle_stream()->uses_index (_format_context, si) && film->with_subtitles ()) {
                decode_subtitle_packet ();
        }
 
@@ -323,7 +322,7 @@ FFmpegDecoder::minimal_run (boost::function<bool (optional<ContentTime>, optiona
                                        );
                        }
 
-               } else if (_ffmpeg_content->audio_stream() && _packet.stream_index == _ffmpeg_content->audio_stream()->index (_format_context)) {
+               } else if (_ffmpeg_content->audio_stream() && _ffmpeg_content->audio_stream()->uses_index (_format_context, _packet.stream_index)) {
                        AVPacket copy_packet = _packet;
                        while (copy_packet.size > 0) {
 
@@ -391,7 +390,9 @@ void
 FFmpegDecoder::seek (ContentTime time, bool accurate)
 {
        Decoder::seek (time, accurate);
-       AudioDecoder::seek (time, accurate);
+       if (_decode_audio) {
+               AudioDecoder::seek (time, accurate);
+       }
        
        /* If we are doing an accurate seek, our initial shot will be 200ms (200 being
           a number plucked from the air) earlier than we want to end up.  The loop below
@@ -521,15 +522,19 @@ FFmpegDecoder::setup_subtitle ()
 {
        boost::mutex::scoped_lock lm (_mutex);
        
-       if (!_ffmpeg_content->subtitle_stream() || _ffmpeg_content->subtitle_stream()->index (_format_context) >= int (_format_context->nb_streams)) {
+       if (!_ffmpeg_content->subtitle_stream()) {
                return;
        }
 
        _subtitle_codec_context = _ffmpeg_content->subtitle_stream()->stream(_format_context)->codec;
+       if (_subtitle_codec_context == 0) {
+               throw DecodeError (N_("could not find subtitle stream"));
+       }
+
        _subtitle_codec = avcodec_find_decoder (_subtitle_codec_context->codec_id);
 
        if (_subtitle_codec == 0) {
-               throw DecodeError (_("could not find subtitle decoder"));
+               throw DecodeError (N_("could not find subtitle decoder"));
        }
        
        if (avcodec_open2 (_subtitle_codec_context, _subtitle_codec, 0) < 0) {