More player debugging for butler video-full states.
[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 #include <libavutil/eval.h>
27 #include <libavutil/display.h>
28 }
29 #include "ffmpeg_examiner.h"
30 #include "ffmpeg_content.h"
31 #include "job.h"
32 #include "ffmpeg_audio_stream.h"
33 #include "ffmpeg_subtitle_stream.h"
34 #include "util.h"
35 #include <boost/foreach.hpp>
36 #include <iostream>
37
38 #include "i18n.h"
39
40 using std::string;
41 using std::cout;
42 using std::max;
43 using boost::shared_ptr;
44 using boost::optional;
45
46 /** @param job job that the examiner is operating in, or 0 */
47 FFmpegExaminer::FFmpegExaminer (shared_ptr<const FFmpegContent> c, shared_ptr<Job> job)
48         : FFmpeg (c)
49         , _video_length (0)
50         , _need_video_length (false)
51 {
52         /* Find audio and subtitle streams */
53
54         for (uint32_t i = 0; i < _format_context->nb_streams; ++i) {
55                 AVStream* s = _format_context->streams[i];
56                 if (s->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
57
58                         /* This is a hack; sometimes it seems that _audio_codec_context->channel_layout isn't set up,
59                            so bodge it here.  No idea why we should have to do this.
60                         */
61
62                         if (s->codec->channel_layout == 0) {
63                                 s->codec->channel_layout = av_get_default_channel_layout (s->codec->channels);
64                         }
65
66                         DCPOMATIC_ASSERT (_format_context->duration != AV_NOPTS_VALUE);
67                         DCPOMATIC_ASSERT (s->codec->codec);
68                         DCPOMATIC_ASSERT (s->codec->codec->name);
69
70                         _audio_streams.push_back (
71                                 shared_ptr<FFmpegAudioStream> (
72                                         new FFmpegAudioStream (
73                                                 stream_name (s),
74                                                 s->codec->codec->name,
75                                                 s->id,
76                                                 s->codec->sample_rate,
77                                                 llrint ((double (_format_context->duration) / AV_TIME_BASE) * s->codec->sample_rate),
78                                                 s->codec->channels
79                                                 )
80                                         )
81                                 );
82
83                 } else if (s->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
84                         _subtitle_streams.push_back (shared_ptr<FFmpegSubtitleStream> (new FFmpegSubtitleStream (subtitle_stream_name (s), s->id)));
85                 }
86         }
87
88         if (has_video ()) {
89                 /* See if the header has duration information in it */
90                 _need_video_length = _format_context->duration == AV_NOPTS_VALUE;
91                 if (!_need_video_length) {
92                         _video_length = llrint ((double (_format_context->duration) / AV_TIME_BASE) * video_frame_rate().get());
93                 }
94         }
95
96         if (job && _need_video_length) {
97                 job->sub (_("Finding length"));
98         }
99
100         /* Run through until we find:
101          *   - the first video.
102          *   - the first audio for each stream.
103          */
104
105         int64_t const len = _file_group.length ();
106         while (true) {
107                 int r = av_read_frame (_format_context, &_packet);
108                 if (r < 0) {
109                         break;
110                 }
111
112                 if (job) {
113                         if (len > 0) {
114                                 job->set_progress (float (_format_context->pb->pos) / len);
115                         } else {
116                                 job->set_progress_unknown ();
117                         }
118                 }
119
120                 AVCodecContext* context = _format_context->streams[_packet.stream_index]->codec;
121
122                 if (_video_stream && _packet.stream_index == _video_stream.get()) {
123                         video_packet (context);
124                 }
125
126                 bool got_all_audio = true;
127
128                 for (size_t i = 0; i < _audio_streams.size(); ++i) {
129                         if (_audio_streams[i]->uses_index (_format_context, _packet.stream_index)) {
130                                 audio_packet (context, _audio_streams[i]);
131                         }
132                         if (!_audio_streams[i]->first_audio) {
133                                 got_all_audio = false;
134                         }
135                 }
136
137                 av_packet_unref (&_packet);
138
139                 if (_first_video && got_all_audio) {
140                         /* All done */
141                         break;
142                 }
143         }
144
145         if (_video_stream) {
146                 /* This code taken from get_rotation() in ffmpeg:cmdutils.c */
147                 AVStream* stream = _format_context->streams[*_video_stream];
148                 AVDictionaryEntry* rotate_tag = av_dict_get (stream->metadata, "rotate", 0, 0);
149                 uint8_t* displaymatrix = av_stream_get_side_data (stream, AV_PKT_DATA_DISPLAYMATRIX, 0);
150                 _rotation = 0;
151
152                 if (rotate_tag && *rotate_tag->value && strcmp(rotate_tag->value, "0")) {
153                         char *tail;
154                         _rotation = av_strtod (rotate_tag->value, &tail);
155                         if (*tail) {
156                                 _rotation = 0;
157                         }
158                 }
159
160                 if (displaymatrix && !_rotation) {
161                         _rotation = - av_display_rotation_get ((int32_t*) displaymatrix);
162                 }
163
164                 _rotation = *_rotation - 360 * floor (*_rotation / 360 + 0.9 / 360);
165
166                 DCPOMATIC_ASSERT (fabs (*_rotation - 90 * round (*_rotation / 90)) < 2);
167         }
168 }
169
170 void
171 FFmpegExaminer::video_packet (AVCodecContext* context)
172 {
173         DCPOMATIC_ASSERT (_video_stream);
174
175         if (_first_video && !_need_video_length) {
176                 return;
177         }
178
179         int frame_finished;
180         if (avcodec_decode_video2 (context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
181                 if (!_first_video) {
182                         _first_video = frame_time (_format_context->streams[_video_stream.get()]);
183                 }
184                 if (_need_video_length) {
185                         _video_length = frame_time (
186                                 _format_context->streams[_video_stream.get()]
187                                 ).get_value_or (ContentTime ()).frames_round (video_frame_rate().get ());
188                 }
189         }
190 }
191
192 void
193 FFmpegExaminer::audio_packet (AVCodecContext* context, shared_ptr<FFmpegAudioStream> stream)
194 {
195         if (stream->first_audio) {
196                 return;
197         }
198
199         int frame_finished;
200         if (avcodec_decode_audio4 (context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
201                 stream->first_audio = frame_time (stream->stream (_format_context));
202         }
203 }
204
205 optional<ContentTime>
206 FFmpegExaminer::frame_time (AVStream* s) const
207 {
208         optional<ContentTime> t;
209
210         int64_t const bet = av_frame_get_best_effort_timestamp (_frame);
211         if (bet != AV_NOPTS_VALUE) {
212                 t = ContentTime::from_seconds (bet * av_q2d (s->time_base));
213         }
214
215         return t;
216 }
217
218 optional<double>
219 FFmpegExaminer::video_frame_rate () const
220 {
221         DCPOMATIC_ASSERT (_video_stream);
222         return av_q2d(av_guess_frame_rate(_format_context, _format_context->streams[_video_stream.get()], 0));
223 }
224
225 dcp::Size
226 FFmpegExaminer::video_size () const
227 {
228         return dcp::Size (video_codec_context()->width, video_codec_context()->height);
229 }
230
231 /** @return Length according to our content's header */
232 Frame
233 FFmpegExaminer::video_length () const
234 {
235         return max (Frame (1), _video_length);
236 }
237
238 optional<double>
239 FFmpegExaminer::sample_aspect_ratio () const
240 {
241         DCPOMATIC_ASSERT (_video_stream);
242         AVRational sar = av_guess_sample_aspect_ratio (_format_context, _format_context->streams[_video_stream.get()], 0);
243         if (sar.num == 0) {
244                 /* I assume this means that we don't know */
245                 return optional<double> ();
246         }
247         return double (sar.num) / sar.den;
248 }
249
250 string
251 FFmpegExaminer::subtitle_stream_name (AVStream* s) const
252 {
253         string n = stream_name (s);
254
255         if (n.empty()) {
256                 n = _("unknown");
257         }
258
259         return n;
260 }
261
262 string
263 FFmpegExaminer::stream_name (AVStream* s) const
264 {
265         string n;
266
267         if (s->metadata) {
268                 AVDictionaryEntry const * lang = av_dict_get (s->metadata, "language", 0, 0);
269                 if (lang) {
270                         n = lang->value;
271                 }
272
273                 AVDictionaryEntry const * title = av_dict_get (s->metadata, "title", 0, 0);
274                 if (title) {
275                         if (!n.empty()) {
276                                 n += " ";
277                         }
278                         n += title->value;
279                 }
280         }
281
282         return n;
283 }
284
285 optional<int>
286 FFmpegExaminer::bits_per_pixel () const
287 {
288         if (video_codec_context()->pix_fmt == -1) {
289                 return optional<int>();
290         }
291
292         AVPixFmtDescriptor const * d = av_pix_fmt_desc_get (video_codec_context()->pix_fmt);
293         DCPOMATIC_ASSERT (d);
294         return av_get_bits_per_pixel (d);
295 }
296
297 bool
298 FFmpegExaminer::yuv () const
299 {
300         switch (video_codec_context()->pix_fmt) {
301         case AV_PIX_FMT_YUV420P:
302         case AV_PIX_FMT_YUYV422:
303         case AV_PIX_FMT_YUV422P:
304         case AV_PIX_FMT_YUV444P:
305         case AV_PIX_FMT_YUV410P:
306         case AV_PIX_FMT_YUV411P:
307         case AV_PIX_FMT_YUVJ420P:
308         case AV_PIX_FMT_YUVJ422P:
309         case AV_PIX_FMT_YUVJ444P:
310         case AV_PIX_FMT_UYVY422:
311         case AV_PIX_FMT_UYYVYY411:
312         case AV_PIX_FMT_NV12:
313         case AV_PIX_FMT_NV21:
314         case AV_PIX_FMT_YUV440P:
315         case AV_PIX_FMT_YUVJ440P:
316         case AV_PIX_FMT_YUVA420P:
317         case AV_PIX_FMT_YUV420P16LE:
318         case AV_PIX_FMT_YUV420P16BE:
319         case AV_PIX_FMT_YUV422P16LE:
320         case AV_PIX_FMT_YUV422P16BE:
321         case AV_PIX_FMT_YUV444P16LE:
322         case AV_PIX_FMT_YUV444P16BE:
323         case AV_PIX_FMT_YUV420P9BE:
324         case AV_PIX_FMT_YUV420P9LE:
325         case AV_PIX_FMT_YUV420P10BE:
326         case AV_PIX_FMT_YUV420P10LE:
327         case AV_PIX_FMT_YUV422P10BE:
328         case AV_PIX_FMT_YUV422P10LE:
329         case AV_PIX_FMT_YUV444P9BE:
330         case AV_PIX_FMT_YUV444P9LE:
331         case AV_PIX_FMT_YUV444P10BE:
332         case AV_PIX_FMT_YUV444P10LE:
333         case AV_PIX_FMT_YUV422P9BE:
334         case AV_PIX_FMT_YUV422P9LE:
335         case AV_PIX_FMT_YUVA420P9BE:
336         case AV_PIX_FMT_YUVA420P9LE:
337         case AV_PIX_FMT_YUVA422P9BE:
338         case AV_PIX_FMT_YUVA422P9LE:
339         case AV_PIX_FMT_YUVA444P9BE:
340         case AV_PIX_FMT_YUVA444P9LE:
341         case AV_PIX_FMT_YUVA420P10BE:
342         case AV_PIX_FMT_YUVA420P10LE:
343         case AV_PIX_FMT_YUVA422P10BE:
344         case AV_PIX_FMT_YUVA422P10LE:
345         case AV_PIX_FMT_YUVA444P10BE:
346         case AV_PIX_FMT_YUVA444P10LE:
347         case AV_PIX_FMT_YUVA420P16BE:
348         case AV_PIX_FMT_YUVA420P16LE:
349         case AV_PIX_FMT_YUVA422P16BE:
350         case AV_PIX_FMT_YUVA422P16LE:
351         case AV_PIX_FMT_YUVA444P16BE:
352         case AV_PIX_FMT_YUVA444P16LE:
353         case AV_PIX_FMT_NV16:
354         case AV_PIX_FMT_NV20LE:
355         case AV_PIX_FMT_NV20BE:
356         case AV_PIX_FMT_YVYU422:
357         case AV_PIX_FMT_YUVA444P:
358         case AV_PIX_FMT_YUVA422P:
359         case AV_PIX_FMT_YUV420P12BE:
360         case AV_PIX_FMT_YUV420P12LE:
361         case AV_PIX_FMT_YUV420P14BE:
362         case AV_PIX_FMT_YUV420P14LE:
363         case AV_PIX_FMT_YUV422P12BE:
364         case AV_PIX_FMT_YUV422P12LE:
365         case AV_PIX_FMT_YUV422P14BE:
366         case AV_PIX_FMT_YUV422P14LE:
367         case AV_PIX_FMT_YUV444P12BE:
368         case AV_PIX_FMT_YUV444P12LE:
369         case AV_PIX_FMT_YUV444P14BE:
370         case AV_PIX_FMT_YUV444P14LE:
371         case AV_PIX_FMT_YUVJ411P:
372                 return true;
373         default:
374                 return false;
375         }
376 }
377
378 bool
379 FFmpegExaminer::has_video () const
380 {
381         return static_cast<bool> (_video_stream);
382 }