a145ae25c8f2c6fe6ab1d3fde72c8217c37577e4
[dcpomatic.git] / src / lib / ffmpeg_examiner.cc
1 /*
2     Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 extern "C" {
22 #include <libavcodec/avcodec.h>
23 #include <libavformat/avformat.h>
24 #include <libavutil/pixfmt.h>
25 #include <libavutil/pixdesc.h>
26 }
27 #include "ffmpeg_examiner.h"
28 #include "ffmpeg_content.h"
29 #include "job.h"
30 #include "ffmpeg_audio_stream.h"
31 #include "ffmpeg_subtitle_stream.h"
32 #include "util.h"
33 #include <boost/foreach.hpp>
34 #include <iostream>
35
36 #include "i18n.h"
37
38 using std::string;
39 using std::cout;
40 using std::max;
41 using boost::shared_ptr;
42 using boost::optional;
43
44 /** @param job job that the examiner is operating in, or 0 */
45 FFmpegExaminer::FFmpegExaminer (shared_ptr<const FFmpegContent> c, shared_ptr<Job> job)
46         : FFmpeg (c)
47         , _video_length (0)
48         , _need_video_length (false)
49 {
50         /* Find audio and subtitle streams */
51
52         for (uint32_t i = 0; i < _format_context->nb_streams; ++i) {
53                 AVStream* s = _format_context->streams[i];
54                 if (s->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
55
56                         /* This is a hack; sometimes it seems that _audio_codec_context->channel_layout isn't set up,
57                            so bodge it here.  No idea why we should have to do this.
58                         */
59
60                         if (s->codec->channel_layout == 0) {
61                                 s->codec->channel_layout = av_get_default_channel_layout (s->codec->channels);
62                         }
63
64                         DCPOMATIC_ASSERT (_format_context->duration != AV_NOPTS_VALUE);
65                         DCPOMATIC_ASSERT (s->codec->codec);
66                         DCPOMATIC_ASSERT (s->codec->codec->name);
67
68                         _audio_streams.push_back (
69                                 shared_ptr<FFmpegAudioStream> (
70                                         new FFmpegAudioStream (
71                                                 stream_name (s),
72                                                 s->codec->codec->name,
73                                                 s->id,
74                                                 s->codec->sample_rate,
75                                                 (double (_format_context->duration) / AV_TIME_BASE) * s->codec->sample_rate,
76                                                 s->codec->channels
77                                                 )
78                                         )
79                                 );
80
81                 } else if (s->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
82                         _subtitle_streams.push_back (shared_ptr<FFmpegSubtitleStream> (new FFmpegSubtitleStream (subtitle_stream_name (s), s->id)));
83                 }
84         }
85
86         if (has_video ()) {
87                 /* See if the header has duration information in it */
88                 _need_video_length = _format_context->duration == AV_NOPTS_VALUE;
89                 if (!_need_video_length) {
90                         _video_length = (double (_format_context->duration) / AV_TIME_BASE) * video_frame_rate().get ();
91                 }
92         }
93
94         if (job && _need_video_length) {
95                 job->sub (_("Finding length"));
96         }
97
98         /* Run through until we find:
99          *   - the first video.
100          *   - the first audio for each stream.
101          */
102
103         int64_t const len = _file_group.length ();
104         while (true) {
105                 int r = av_read_frame (_format_context, &_packet);
106                 if (r < 0) {
107                         break;
108                 }
109
110                 if (job) {
111                         if (len > 0) {
112                                 job->set_progress (float (_format_context->pb->pos) / len);
113                         } else {
114                                 job->set_progress_unknown ();
115                         }
116                 }
117
118                 AVCodecContext* context = _format_context->streams[_packet.stream_index]->codec;
119
120                 if (_video_stream && _packet.stream_index == _video_stream.get()) {
121                         video_packet (context);
122                 }
123
124                 bool got_all_audio = true;
125
126                 for (size_t i = 0; i < _audio_streams.size(); ++i) {
127                         if (_audio_streams[i]->uses_index (_format_context, _packet.stream_index)) {
128                                 audio_packet (context, _audio_streams[i]);
129                         }
130                         if (!_audio_streams[i]->first_audio) {
131                                 got_all_audio = false;
132                         }
133                 }
134
135                 av_packet_unref (&_packet);
136
137                 if (_first_video && got_all_audio) {
138                         /* All done */
139                         break;
140                 }
141         }
142 }
143
144 void
145 FFmpegExaminer::video_packet (AVCodecContext* context)
146 {
147         DCPOMATIC_ASSERT (_video_stream);
148
149         if (_first_video && !_need_video_length) {
150                 return;
151         }
152
153         int frame_finished;
154         if (avcodec_decode_video2 (context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
155                 if (!_first_video) {
156                         _first_video = frame_time (_format_context->streams[_video_stream.get()]);
157                 }
158                 if (_need_video_length) {
159                         _video_length = frame_time (
160                                 _format_context->streams[_video_stream.get()]
161                                 ).get_value_or (ContentTime ()).frames_round (video_frame_rate().get ());
162                 }
163         }
164 }
165
166 void
167 FFmpegExaminer::audio_packet (AVCodecContext* context, shared_ptr<FFmpegAudioStream> stream)
168 {
169         if (stream->first_audio) {
170                 return;
171         }
172
173         int frame_finished;
174         if (avcodec_decode_audio4 (context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
175                 stream->first_audio = frame_time (stream->stream (_format_context));
176         }
177 }
178
179 optional<ContentTime>
180 FFmpegExaminer::frame_time (AVStream* s) const
181 {
182         optional<ContentTime> t;
183
184         int64_t const bet = av_frame_get_best_effort_timestamp (_frame);
185         if (bet != AV_NOPTS_VALUE) {
186                 t = ContentTime::from_seconds (bet * av_q2d (s->time_base));
187         }
188
189         return t;
190 }
191
192 optional<double>
193 FFmpegExaminer::video_frame_rate () const
194 {
195         DCPOMATIC_ASSERT (_video_stream);
196         /* This use of r_frame_rate is debateable; there's a few different
197          * frame rates in the format context, but this one seems to be the most
198          * reliable.
199          */
200         return av_q2d (av_stream_get_r_frame_rate (_format_context->streams[_video_stream.get()]));
201 }
202
203 dcp::Size
204 FFmpegExaminer::video_size () const
205 {
206         return dcp::Size (video_codec_context()->width, video_codec_context()->height);
207 }
208
209 /** @return Length according to our content's header */
210 Frame
211 FFmpegExaminer::video_length () const
212 {
213         return max (Frame (1), _video_length);
214 }
215
216 optional<double>
217 FFmpegExaminer::sample_aspect_ratio () const
218 {
219         DCPOMATIC_ASSERT (_video_stream);
220         AVRational sar = av_guess_sample_aspect_ratio (_format_context, _format_context->streams[_video_stream.get()], 0);
221         if (sar.num == 0) {
222                 /* I assume this means that we don't know */
223                 return optional<double> ();
224         }
225         return double (sar.num) / sar.den;
226 }
227
228 string
229 FFmpegExaminer::subtitle_stream_name (AVStream* s) const
230 {
231         string n = stream_name (s);
232
233         if (n.empty()) {
234                 n = _("unknown");
235         }
236
237         return n;
238 }
239
240 string
241 FFmpegExaminer::stream_name (AVStream* s) const
242 {
243         string n;
244
245         if (s->metadata) {
246                 AVDictionaryEntry const * lang = av_dict_get (s->metadata, "language", 0, 0);
247                 if (lang) {
248                         n = lang->value;
249                 }
250
251                 AVDictionaryEntry const * title = av_dict_get (s->metadata, "title", 0, 0);
252                 if (title) {
253                         if (!n.empty()) {
254                                 n += " ";
255                         }
256                         n += title->value;
257                 }
258         }
259
260         return n;
261 }
262
263 int
264 FFmpegExaminer::bits_per_pixel () const
265 {
266         if (video_codec_context()->pix_fmt == -1) {
267                 throw DecodeError (_("Could not find pixel format for video."));
268         }
269
270         AVPixFmtDescriptor const * d = av_pix_fmt_desc_get (video_codec_context()->pix_fmt);
271         DCPOMATIC_ASSERT (d);
272         return av_get_bits_per_pixel (d);
273 }
274
275 bool
276 FFmpegExaminer::yuv () const
277 {
278         switch (video_codec_context()->pix_fmt) {
279         case AV_PIX_FMT_YUV420P:
280         case AV_PIX_FMT_YUYV422:
281         case AV_PIX_FMT_YUV422P:
282         case AV_PIX_FMT_YUV444P:
283         case AV_PIX_FMT_YUV410P:
284         case AV_PIX_FMT_YUV411P:
285         case AV_PIX_FMT_YUVJ420P:
286         case AV_PIX_FMT_YUVJ422P:
287         case AV_PIX_FMT_YUVJ444P:
288         case AV_PIX_FMT_UYVY422:
289         case AV_PIX_FMT_UYYVYY411:
290         case AV_PIX_FMT_NV12:
291         case AV_PIX_FMT_NV21:
292         case AV_PIX_FMT_YUV440P:
293         case AV_PIX_FMT_YUVJ440P:
294         case AV_PIX_FMT_YUVA420P:
295         case AV_PIX_FMT_YUV420P16LE:
296         case AV_PIX_FMT_YUV420P16BE:
297         case AV_PIX_FMT_YUV422P16LE:
298         case AV_PIX_FMT_YUV422P16BE:
299         case AV_PIX_FMT_YUV444P16LE:
300         case AV_PIX_FMT_YUV444P16BE:
301         case AV_PIX_FMT_YUV420P9BE:
302         case AV_PIX_FMT_YUV420P9LE:
303         case AV_PIX_FMT_YUV420P10BE:
304         case AV_PIX_FMT_YUV420P10LE:
305         case AV_PIX_FMT_YUV422P10BE:
306         case AV_PIX_FMT_YUV422P10LE:
307         case AV_PIX_FMT_YUV444P9BE:
308         case AV_PIX_FMT_YUV444P9LE:
309         case AV_PIX_FMT_YUV444P10BE:
310         case AV_PIX_FMT_YUV444P10LE:
311         case AV_PIX_FMT_YUV422P9BE:
312         case AV_PIX_FMT_YUV422P9LE:
313         case AV_PIX_FMT_YUVA420P9BE:
314         case AV_PIX_FMT_YUVA420P9LE:
315         case AV_PIX_FMT_YUVA422P9BE:
316         case AV_PIX_FMT_YUVA422P9LE:
317         case AV_PIX_FMT_YUVA444P9BE:
318         case AV_PIX_FMT_YUVA444P9LE:
319         case AV_PIX_FMT_YUVA420P10BE:
320         case AV_PIX_FMT_YUVA420P10LE:
321         case AV_PIX_FMT_YUVA422P10BE:
322         case AV_PIX_FMT_YUVA422P10LE:
323         case AV_PIX_FMT_YUVA444P10BE:
324         case AV_PIX_FMT_YUVA444P10LE:
325         case AV_PIX_FMT_YUVA420P16BE:
326         case AV_PIX_FMT_YUVA420P16LE:
327         case AV_PIX_FMT_YUVA422P16BE:
328         case AV_PIX_FMT_YUVA422P16LE:
329         case AV_PIX_FMT_YUVA444P16BE:
330         case AV_PIX_FMT_YUVA444P16LE:
331         case AV_PIX_FMT_NV16:
332         case AV_PIX_FMT_NV20LE:
333         case AV_PIX_FMT_NV20BE:
334         case AV_PIX_FMT_YVYU422:
335         case AV_PIX_FMT_YUVA444P:
336         case AV_PIX_FMT_YUVA422P:
337         case AV_PIX_FMT_YUV420P12BE:
338         case AV_PIX_FMT_YUV420P12LE:
339         case AV_PIX_FMT_YUV420P14BE:
340         case AV_PIX_FMT_YUV420P14LE:
341         case AV_PIX_FMT_YUV422P12BE:
342         case AV_PIX_FMT_YUV422P12LE:
343         case AV_PIX_FMT_YUV422P14BE:
344         case AV_PIX_FMT_YUV422P14LE:
345         case AV_PIX_FMT_YUV444P12BE:
346         case AV_PIX_FMT_YUV444P12LE:
347         case AV_PIX_FMT_YUV444P14BE:
348         case AV_PIX_FMT_YUV444P14LE:
349         case AV_PIX_FMT_YUVJ411P:
350                 return true;
351         default:
352                 return false;
353         }
354 }
355
356 bool
357 FFmpegExaminer::has_video () const
358 {
359         return static_cast<bool> (_video_stream);
360 }