ChangeLog.
[dcpomatic.git] / src / lib / ffmpeg_decoder.cc
index ede724b5f26e9d03fdb85223a8a05985c89ceeb8..5a1b78762d92e6e30d55108dfae637f8b348e676 100644 (file)
@@ -61,7 +61,7 @@ using libdcp::Size;
 FFmpegDecoder::FFmpegDecoder (shared_ptr<const Film> f, shared_ptr<const FFmpegContent> c, bool video, bool audio)
        : Decoder (f)
        , VideoDecoder (f, c)
-       , AudioDecoder (f)
+       , AudioDecoder (f, c)
        , SubtitleDecoder (f)
        , FFmpeg (c)
        , _subtitle_codec_context (0)
@@ -97,6 +97,8 @@ FFmpegDecoder::FFmpegDecoder (shared_ptr<const Film> f, shared_ptr<const FFmpegC
                _video_pts_offset = _audio_pts_offset = - min (c->first_video().get(), c->audio_stream()->first_audio.get());
        } else if (have_video) {
                _video_pts_offset = - c->first_video().get();
+       } else if (have_audio) {
+               _audio_pts_offset = - c->audio_stream()->first_audio.get();
        }
 
        /* Now adjust both so that the video pts starts on a frame */
@@ -169,12 +171,14 @@ FFmpegDecoder::pass ()
 
        shared_ptr<const Film> film = _film.lock ();
        assert (film);
+
+       int const si = _packet.stream_index;
        
-       if (_packet.stream_index == _video_stream && _decode_video) {
+       if (si == _video_stream && _decode_video) {
                decode_video_packet ();
-       } else if (_ffmpeg_content->audio_stream() && _packet.stream_index == _ffmpeg_content->audio_stream()->id && _decode_audio) {
+       } else if (_ffmpeg_content->audio_stream() && si == _ffmpeg_content->audio_stream()->index (_format_context) && _decode_audio) {
                decode_audio_packet ();
-       } else if (_ffmpeg_content->subtitle_stream() && _packet.stream_index == _ffmpeg_content->subtitle_stream()->id && film->with_subtitles ()) {
+       } else if (_ffmpeg_content->subtitle_stream() && si == _ffmpeg_content->subtitle_stream()->index (_format_context) && film->with_subtitles ()) {
                decode_subtitle_packet ();
        }
 
@@ -337,6 +341,7 @@ FFmpegDecoder::seek (VideoContent::Frame frame, bool accurate)
                }
 
                if (_packet.stream_index != _video_stream) {
+                       av_free_packet (&_packet);
                        continue;
                }
                
@@ -385,7 +390,7 @@ FFmpegDecoder::decode_audio_packet ()
                                /* Where we are in the source, in seconds */
                                double const pts = av_q2d (_format_context->streams[copy_packet.stream_index]->time_base)
                                        * av_frame_get_best_effort_timestamp(_frame) + _audio_pts_offset;
-                               
+
                                if (pts > 0) {
                                        /* Emit some silence */
                                        shared_ptr<AudioBuffers> silence (
@@ -508,11 +513,11 @@ FFmpegDecoder::setup_subtitle ()
 {
        boost::mutex::scoped_lock lm (_mutex);
        
-       if (!_ffmpeg_content->subtitle_stream() || _ffmpeg_content->subtitle_stream()->id >= int (_format_context->nb_streams)) {
+       if (!_ffmpeg_content->subtitle_stream() || _ffmpeg_content->subtitle_stream()->index (_format_context) >= int (_format_context->nb_streams)) {
                return;
        }
 
-       _subtitle_codec_context = _format_context->streams[_ffmpeg_content->subtitle_stream()->id]->codec;
+       _subtitle_codec_context = _ffmpeg_content->subtitle_stream()->stream(_format_context)->codec;
        _subtitle_codec = avcodec_find_decoder (_subtitle_codec_context->codec_id);
 
        if (_subtitle_codec == 0) {