6109b72126af904a4a8909e52bb4061ac221c545
[dcpomatic.git] / src / lib / ffmpeg_content.cc
1 #include "ffmpeg_content.h"
2 #include "ffmpeg_decoder.h"
3 #include "compose.hpp"
4 #include "job.h"
5 #include "util.h"
6 #include "log.h"
7
8 #include "i18n.h"
9
10 using std::string;
11 using boost::shared_ptr;
12
13 int const FFmpegContentProperty::SUBTITLE_STREAMS = 100;
14 int const FFmpegContentProperty::SUBTITLE_STREAM = 101;
15 int const FFmpegContentProperty::AUDIO_STREAMS = 102;
16 int const FFmpegContentProperty::AUDIO_STREAM = 103;
17
18 FFmpegContent::FFmpegContent (boost::filesystem::path f)
19         : Content (f)
20         , VideoContent (f)
21         , AudioContent (f)
22 {
23
24 }
25
26 void
27 FFmpegContent::examine (shared_ptr<Film> film, shared_ptr<Job> job, bool quick)
28 {
29         job->descend (0.5);
30         Content::examine (film, job, quick);
31         job->ascend ();
32
33         job->set_progress_unknown ();
34
35         shared_ptr<FFmpegDecoder> decoder (new FFmpegDecoder (film, shared_from_this (), true, false, false, true));
36
37         ContentVideoFrame video_length = 0;
38         if (quick) {
39                 video_length = decoder->video_length ();
40                 film->log()->log (String::compose ("Video length obtained from header as %1 frames", decoder->video_length ()));
41         } else {
42                 while (!decoder->pass ()) {
43                         /* keep going */
44                 }
45
46                 video_length = decoder->video_frame ();
47                 film->log()->log (String::compose ("Video length examined as %1 frames", decoder->video_frame ()));
48         }
49
50         {
51                 boost::mutex::scoped_lock lm (_mutex);
52
53                 _video_length = video_length;
54
55                 _subtitle_streams = decoder->subtitle_streams ();
56                 if (!_subtitle_streams.empty ()) {
57                         _subtitle_stream = _subtitle_streams.front ();
58                 }
59                 
60                 _audio_streams = decoder->audio_streams ();
61                 if (!_audio_streams.empty ()) {
62                         _audio_stream = _audio_streams.front ();
63                 }
64         }
65
66         take_from_video_decoder (decoder);
67
68         Changed (VideoContentProperty::VIDEO_LENGTH);
69         Changed (FFmpegContentProperty::SUBTITLE_STREAMS);
70         Changed (FFmpegContentProperty::SUBTITLE_STREAM);
71         Changed (FFmpegContentProperty::AUDIO_STREAMS);
72         Changed (FFmpegContentProperty::AUDIO_STREAM);
73 }
74
75 string
76 FFmpegContent::summary () const
77 {
78         return String::compose (_("Movie: %1"), file().filename ());
79 }
80
81 void
82 FFmpegContent::set_subtitle_stream (FFmpegSubtitleStream s)
83 {
84         {
85                 boost::mutex::scoped_lock lm (_mutex);
86                 _subtitle_stream = s;
87         }
88
89         Changed (FFmpegContentProperty::SUBTITLE_STREAM);
90 }
91
92 void
93 FFmpegContent::set_audio_stream (FFmpegAudioStream s)
94 {
95         {
96                 boost::mutex::scoped_lock lm (_mutex);
97                 _audio_stream = s;
98         }
99
100         Changed (FFmpegContentProperty::AUDIO_STREAM);
101 }
102
103 ContentAudioFrame
104 FFmpegContent::audio_length () const
105 {
106         if (!_audio_stream) {
107                 return 0;
108         }
109         
110         return video_frames_to_audio_frames (_video_length, audio_frame_rate(), video_frame_rate());
111 }
112
113 int
114 FFmpegContent::audio_channels () const
115 {
116         if (!_audio_stream) {
117                 return 0;
118         }
119
120         return _audio_stream->channels ();
121 }
122
123 int
124 FFmpegContent::audio_frame_rate () const
125 {
126         if (!_audio_stream) {
127                 return 0;
128         }
129
130         return _audio_stream->frame_rate;
131 }
132
133 int64_t
134 FFmpegContent::audio_channel_layout () const
135 {
136         if (!_audio_stream) {
137                 return 0;
138         }
139
140         return _audio_stream->channel_layout;
141 }
142         
143 bool
144 operator== (FFmpegSubtitleStream const & a, FFmpegSubtitleStream const & b)
145 {
146         return a.id == b.id;
147 }
148
149 bool
150 operator== (FFmpegAudioStream const & a, FFmpegAudioStream const & b)
151 {
152         return a.id == b.id;
153 }