Set interop flags on assets, not just the DCP/CPL's XML.
[dcpomatic.git] / src / lib / ffmpeg_decoder.cc
index be32475377da2c851647bc4ea8f247218c9d24f5..a6f9a17c3bc7978e91821e64758f1be52a2361b6 100644 (file)
@@ -63,7 +63,7 @@ FFmpegDecoder::FFmpegDecoder (shared_ptr<const Film> f, shared_ptr<const FFmpegC
        , VideoDecoder (f, c)
        , AudioDecoder (f, c)
        , SubtitleDecoder (f)
-       , FFmpeg (c, false)
+       , FFmpeg (c)
        , _subtitle_codec_context (0)
        , _subtitle_codec (0)
        , _decode_video (video)
@@ -167,16 +167,16 @@ FFmpegDecoder::pass ()
                return;
        }
 
-       avcodec_get_frame_defaults (_frame);
-
        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 ();
        }
 
@@ -343,8 +343,6 @@ FFmpegDecoder::seek (VideoContent::Frame frame, bool accurate)
                        continue;
                }
                
-               avcodec_get_frame_defaults (_frame);
-               
                int finished = 0;
                r = avcodec_decode_video2 (video_codec_context(), _frame, &finished, &_packet);
                if (r >= 0 && finished) {
@@ -511,11 +509,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) {
@@ -557,8 +555,8 @@ FFmpegDecoder::decode_subtitle_packet ()
        /* Subtitle PTS in seconds (within the source, not taking into account any of the
           source that we may have chopped off for the DCP)
        */
-       double const packet_time = static_cast<double> (sub.pts) / AV_TIME_BASE;
-       
+       double const packet_time = (static_cast<double> (sub.pts ) / AV_TIME_BASE) + _video_pts_offset;
+
        /* hence start time for this sub */
        Time const from = (packet_time + (double (sub.start_display_time) / 1e3)) * TIME_HZ;
        Time const to = (packet_time + (double (sub.end_display_time) / 1e3)) * TIME_HZ;
@@ -568,21 +566,27 @@ FFmpegDecoder::decode_subtitle_packet ()
        if (rect->type != SUBTITLE_BITMAP) {
                throw DecodeError (_("non-bitmap subtitles not yet supported"));
        }
-       
+
+       /* Note RGBA is expressed little-endian, so the first byte in the word is R, second
+          G, third B, fourth A.
+       */
        shared_ptr<Image> image (new Image (PIX_FMT_RGBA, libdcp::Size (rect->w, rect->h), true));
 
        /* Start of the first line in the subtitle */
        uint8_t* sub_p = rect->pict.data[0];
-       /* sub_p looks up into a RGB palette which is here */
+       /* sub_p looks up into a BGRA palette which is here
+          (i.e. first byte B, second G, third R, fourth A)
+       */
        uint32_t const * palette = (uint32_t *) rect->pict.data[1];
        /* Start of the output data */
        uint32_t* out_p = (uint32_t *) image->data()[0];
-       
+
        for (int y = 0; y < rect->h; ++y) {
                uint8_t* sub_line_p = sub_p;
                uint32_t* out_line_p = out_p;
                for (int x = 0; x < rect->w; ++x) {
-                       *out_line_p++ = palette[*sub_line_p++];
+                       uint32_t const p = palette[*sub_line_p++];
+                       *out_line_p++ = ((p & 0xff) << 16) | (p & 0xff00) | ((p & 0xff0000) >> 16) | (p & 0xff000000);
                }
                sub_p += rect->pict.linesize[0];
                out_p += image->stride()[0] / sizeof (uint32_t);