Re-work FilmState / Film relationship a bit; Film now inherits from FilmState and...
[dcpomatic.git] / src / lib / ffmpeg_decoder.cc
index 767299ea61a3411e795dc99bd7f299c0200a1949..828934604b0db42426567aae5a17863db4d425b0 100644 (file)
@@ -27,6 +27,7 @@
 #include <iomanip>
 #include <iostream>
 #include <stdint.h>
+#include <boost/lexical_cast.hpp>
 extern "C" {
 #include <tiffio.h>
 #include <libavcodec/avcodec.h>
@@ -47,6 +48,7 @@ extern "C" {
 #include "util.h"
 #include "log.h"
 #include "ffmpeg_decoder.h"
+#include "subtitle.h"
 
 using namespace std;
 using namespace boost;
@@ -56,15 +58,19 @@ FFmpegDecoder::FFmpegDecoder (boost::shared_ptr<const FilmState> s, boost::share
        , _format_context (0)
        , _video_stream (-1)
        , _audio_stream (-1)
+       , _subtitle_stream (-1)
        , _frame (0)
        , _video_codec_context (0)
        , _video_codec (0)
        , _audio_codec_context (0)
        , _audio_codec (0)
+       , _subtitle_codec_context (0)
+       , _subtitle_codec (0)
 {
        setup_general ();
        setup_video ();
        setup_audio ();
+       setup_subtitle ();
 }
 
 FFmpegDecoder::~FFmpegDecoder ()
@@ -76,6 +82,10 @@ FFmpegDecoder::~FFmpegDecoder ()
        if (_video_codec_context) {
                avcodec_close (_video_codec_context);
        }
+
+       if (_subtitle_codec_context) {
+               avcodec_close (_subtitle_codec_context);
+       }
        
        av_free (_frame);
        avformat_close_input (&_format_context);
@@ -100,7 +110,15 @@ FFmpegDecoder::setup_general ()
                if (_format_context->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
                        _video_stream = i;
                } else if (_format_context->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
-                       _audio_stream = i;
+                       if (_audio_stream == -1) {
+                               _audio_stream = i;
+                       }
+                       _audio_streams.push_back (Stream (stream_name (_format_context->streams[i]), i));
+               } else if (_format_context->streams[i]->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
+                       if (_subtitle_stream == -1) {
+                               _subtitle_stream = i;
+                       }
+                       _subtitle_streams.push_back (Stream (stream_name (_format_context->streams[i]), i));
                }
        }
 
@@ -156,6 +174,26 @@ FFmpegDecoder::setup_audio ()
        }
 }
 
+void
+FFmpegDecoder::setup_subtitle ()
+{
+       if (_subtitle_stream < 0) {
+               return;
+       }
+
+       _subtitle_codec_context = _format_context->streams[_subtitle_stream]->codec;
+       _subtitle_codec = avcodec_find_decoder (_subtitle_codec_context->codec_id);
+
+       if (_subtitle_codec == 0) {
+               throw DecodeError ("could not find subtitle decoder");
+       }
+       
+       if (avcodec_open2 (_subtitle_codec_context, _subtitle_codec, 0) < 0) {
+               throw DecodeError ("could not open subtitle decoder");
+       }
+}
+
+
 bool
 FFmpegDecoder::do_pass ()
 {
@@ -182,7 +220,7 @@ FFmpegDecoder::do_pass ()
                                        0, _audio_codec_context->channels, _frame->nb_samples, audio_sample_format (), 1
                                        );
                                
-                               assert (_audio_codec_context->channels == _fs->audio_channels);
+                               assert (_audio_codec_context->channels == _fs->audio_channels());
                                process_audio (_frame->data[0], data_size);
                        }
                }
@@ -207,9 +245,18 @@ FFmpegDecoder::do_pass ()
                                0, _audio_codec_context->channels, _frame->nb_samples, audio_sample_format (), 1
                                );
 
-                       assert (_audio_codec_context->channels == _fs->audio_channels);
+                       assert (_audio_codec_context->channels == _fs->audio_channels());
                        process_audio (_frame->data[0], data_size);
                }
+
+       } else if (_subtitle_stream >= 0 && _packet.stream_index == _subtitle_stream && _opt->decode_subtitles) {
+
+               int got_subtitle;
+               AVSubtitle sub;
+               if (avcodec_decode_subtitle2 (_subtitle_codec_context, &sub, &got_subtitle, &_packet) && got_subtitle) {
+                       process_subtitle (shared_ptr<TimedSubtitle> (new TimedSubtitle (sub)));
+                       avsubtitle_free (&sub);
+               }
        }
        
        av_free_packet (&_packet);
@@ -310,3 +357,59 @@ FFmpegDecoder::sample_aspect_ratio_denominator () const
        return _video_codec_context->sample_aspect_ratio.den;
 }
 
+bool
+FFmpegDecoder::has_subtitles () const
+{
+       return (_subtitle_stream != -1);
+}
+
+vector<Stream>
+FFmpegDecoder::audio_streams () const
+{
+       return _audio_streams;
+}
+
+vector<Stream>
+FFmpegDecoder::subtitle_streams () const
+{
+       return _subtitle_streams;
+}
+
+void
+FFmpegDecoder::set_audio_stream (int s)
+{
+       _audio_stream = s;
+       setup_audio ();
+}
+
+void
+FFmpegDecoder::set_subtitle_stream (int s)
+{
+       _subtitle_stream = s;
+       setup_subtitle ();
+}
+
+string
+FFmpegDecoder::stream_name (AVStream* s) const
+{
+       stringstream n;
+       
+       AVDictionaryEntry const * lang = av_dict_get (s->metadata, "language", 0, 0);
+       if (lang) {
+               n << lang->value;
+       }
+       
+       AVDictionaryEntry const * title = av_dict_get (s->metadata, "title", 0, 0);
+       if (title) {
+               if (!n.str().empty()) {
+                       n << " ";
+               }
+               n << title->value;
+       }
+
+       if (n.str().empty()) {
+               n << "unknown";
+       }
+
+       return n.str ();
+}