Remove some FFmpeg-related warnings by directly accessing AVFrame.
[dcpomatic.git] / src / lib / ffmpeg_examiner.cc
1 /*
2     Copyright (C) 2013-2021 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
22 #include "dcpomatic_log.h"
23 #include "ffmpeg_examiner.h"
24 #include "ffmpeg_content.h"
25 #include "job.h"
26 #include "ffmpeg_audio_stream.h"
27 #include "ffmpeg_subtitle_stream.h"
28 #include "util.h"
29 #include "warnings.h"
30 DCPOMATIC_DISABLE_WARNINGS
31 extern "C" {
32 #include <libavcodec/avcodec.h>
33 #include <libavformat/avformat.h>
34 #include <libavutil/pixfmt.h>
35 #include <libavutil/pixdesc.h>
36 #include <libavutil/eval.h>
37 #include <libavutil/display.h>
38 }
39 DCPOMATIC_ENABLE_WARNINGS
40 #include <iostream>
41
42 #include "i18n.h"
43
44
45 using std::cout;
46 using std::make_shared;
47 using std::max;
48 using std::shared_ptr;
49 using std::string;
50 using std::vector;
51 using boost::optional;
52 using namespace dcpomatic;
53
54
55 /* This is how many frames from the start of any video that we will examine to see if we
56  * can spot soft 2:3 pull-down ("telecine").
57  */
58 static const int PULLDOWN_CHECK_FRAMES = 16;
59
60
61 /** @param job job that the examiner is operating in, or 0 */
62 FFmpegExaminer::FFmpegExaminer (shared_ptr<const FFmpegContent> c, shared_ptr<Job> job)
63         : FFmpeg (c)
64         , _video_length (0)
65         , _need_video_length (false)
66         , _pulldown (false)
67 {
68         /* Find audio and subtitle streams */
69
70         for (uint32_t i = 0; i < _format_context->nb_streams; ++i) {
71                 auto s = _format_context->streams[i];
72 DCPOMATIC_DISABLE_WARNINGS
73                 if (s->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
74
75                         /* This is a hack; sometimes it seems that _audio_codec_context->channel_layout isn't set up,
76                            so bodge it here.  No idea why we should have to do this.
77                         */
78
79                         if (s->codec->channel_layout == 0) {
80                                 s->codec->channel_layout = av_get_default_channel_layout (s->codec->channels);
81                         }
82
83                         DCPOMATIC_ASSERT (_format_context->duration != AV_NOPTS_VALUE);
84                         DCPOMATIC_ASSERT (s->codec->codec);
85                         DCPOMATIC_ASSERT (s->codec->codec->name);
86
87                         _audio_streams.push_back (
88                                 make_shared<FFmpegAudioStream>(
89                                         stream_name (s),
90                                         s->codec->codec->name,
91                                         s->id,
92                                         s->codec->sample_rate,
93                                         llrint ((double(_format_context->duration) / AV_TIME_BASE) * s->codec->sample_rate),
94                                         s->codec->channels
95                                         )
96                                 );
97
98                 } else if (s->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
99                         _subtitle_streams.push_back (shared_ptr<FFmpegSubtitleStream> (new FFmpegSubtitleStream (subtitle_stream_name (s), s->id)));
100                 }
101         }
102
103         if (has_video ()) {
104                 /* See if the header has duration information in it */
105                 _need_video_length = _format_context->duration == AV_NOPTS_VALUE;
106                 if (!_need_video_length) {
107                         _video_length = llrint ((double (_format_context->duration) / AV_TIME_BASE) * video_frame_rate().get());
108                 }
109         }
110
111         if (job && _need_video_length) {
112                 job->sub (_("Finding length"));
113         }
114
115         /* Run through until we find:
116          *   - the first video.
117          *   - the first audio for each stream.
118          *   - the top-field-first and repeat-first-frame values ("temporal_reference") for the first PULLDOWN_CHECK_FRAMES video frames.
119          */
120
121         int64_t const len = _file_group.length ();
122         /* A string which we build up to describe the top-field-first and repeat-first-frame values for the first few frames.
123          * It would be nicer to use something like vector<bool> here but we want to search the array for a pattern later,
124          * and a string seems a reasonably neat way to do that.
125          */
126         string temporal_reference;
127         while (true) {
128                 auto packet = av_packet_alloc ();
129                 DCPOMATIC_ASSERT (packet);
130                 int r = av_read_frame (_format_context, packet);
131                 if (r < 0) {
132                         av_packet_free (&packet);
133                         break;
134                 }
135
136                 if (job) {
137                         if (len > 0) {
138                                 job->set_progress (float (_format_context->pb->pos) / len);
139                         } else {
140                                 job->set_progress_unknown ();
141                         }
142                 }
143
144                 auto context = _format_context->streams[packet->stream_index]->codec;
145 DCPOMATIC_ENABLE_WARNINGS
146
147                 if (_video_stream && packet->stream_index == _video_stream.get()) {
148                         video_packet (context, temporal_reference, packet);
149                 }
150
151                 bool got_all_audio = true;
152
153                 for (size_t i = 0; i < _audio_streams.size(); ++i) {
154                         if (_audio_streams[i]->uses_index(_format_context, packet->stream_index)) {
155                                 audio_packet (context, _audio_streams[i], packet);
156                         }
157                         if (!_audio_streams[i]->first_audio) {
158                                 got_all_audio = false;
159                         }
160                 }
161
162                 av_packet_free (&packet);
163
164                 if (_first_video && got_all_audio && temporal_reference.size() >= (PULLDOWN_CHECK_FRAMES * 2)) {
165                         /* All done */
166                         break;
167                 }
168         }
169
170         if (_video_stream) {
171                 AVPacket packet;
172                 av_init_packet (&packet);
173                 packet.data = nullptr;
174                 packet.size = 0;
175 DCPOMATIC_DISABLE_WARNINGS
176                 auto context = _format_context->streams[*_video_stream]->codec;
177 DCPOMATIC_ENABLE_WARNINGS
178                 while (video_packet(context, temporal_reference, &packet)) {}
179         }
180
181         for (auto i: _audio_streams) {
182                 AVPacket packet;
183                 av_init_packet (&packet);
184                 packet.data = nullptr;
185                 packet.size = 0;
186 DCPOMATIC_DISABLE_WARNINGS
187                 audio_packet (i->stream(_format_context)->codec, i, &packet);
188 DCPOMATIC_ENABLE_WARNINGS
189         }
190
191         if (_video_stream) {
192                 /* This code taken from get_rotation() in ffmpeg:cmdutils.c */
193                 auto stream = _format_context->streams[*_video_stream];
194                 auto rotate_tag = av_dict_get (stream->metadata, "rotate", 0, 0);
195                 uint8_t* displaymatrix = av_stream_get_side_data (stream, AV_PKT_DATA_DISPLAYMATRIX, 0);
196                 _rotation = 0;
197
198                 if (rotate_tag && *rotate_tag->value && strcmp(rotate_tag->value, "0")) {
199                         char *tail;
200                         _rotation = av_strtod (rotate_tag->value, &tail);
201                         if (*tail) {
202                                 _rotation = 0;
203                         }
204                 }
205
206                 if (displaymatrix && !_rotation) {
207                         _rotation = - av_display_rotation_get ((int32_t*) displaymatrix);
208                 }
209
210                 _rotation = *_rotation - 360 * floor (*_rotation / 360 + 0.9 / 360);
211         }
212
213         LOG_GENERAL("Temporal reference was %1", temporal_reference);
214         if (temporal_reference.find("T2T3B2B3T2T3B2B3") != string::npos || temporal_reference.find("B2B3T2T3B2B3T2T3") != string::npos) {
215                 /* The magical sequence (taken from mediainfo) suggests that 2:3 pull-down is in use */
216                 _pulldown = true;
217                 LOG_GENERAL_NC("Suggest that this may be 2:3 pull-down (soft telecine)");
218         }
219 }
220
221
222 /** @param temporal_reference A string to which we should add two characters per frame;
223  *  the first   is T or B depending on whether it's top- or bottom-field first,
224  *  the second  is 3 or 2 depending on whether "repeat_pict" is true or not.
225  *  @return true if some video was decoded, otherwise false.
226  */
227 bool
228 FFmpegExaminer::video_packet (AVCodecContext* context, string& temporal_reference, AVPacket* packet)
229 {
230         DCPOMATIC_ASSERT (_video_stream);
231
232         if (_first_video && !_need_video_length && temporal_reference.size() >= (PULLDOWN_CHECK_FRAMES * 2)) {
233                 return false;
234         }
235
236         int frame_finished;
237 DCPOMATIC_DISABLE_WARNINGS
238         if (avcodec_decode_video2 (context, _frame, &frame_finished, packet) < 0 || !frame_finished) {
239                 return false;
240         }
241 DCPOMATIC_ENABLE_WARNINGS
242
243         if (!_first_video) {
244                 _first_video = frame_time (_format_context->streams[_video_stream.get()]);
245         }
246         if (_need_video_length) {
247                 _video_length = frame_time (
248                         _format_context->streams[_video_stream.get()]
249                         ).get_value_or (ContentTime ()).frames_round (video_frame_rate().get ());
250         }
251         if (temporal_reference.size() < (PULLDOWN_CHECK_FRAMES * 2)) {
252                 temporal_reference += (_frame->top_field_first ? "T" : "B");
253                 temporal_reference += (_frame->repeat_pict ? "3" : "2");
254         }
255
256         return true;
257 }
258
259
260 void
261 FFmpegExaminer::audio_packet (AVCodecContext* context, shared_ptr<FFmpegAudioStream> stream, AVPacket* packet)
262 {
263         if (stream->first_audio) {
264                 return;
265         }
266
267         int frame_finished;
268 DCPOMATIC_DISABLE_WARNINGS
269         if (avcodec_decode_audio4 (context, _frame, &frame_finished, packet) >= 0 && frame_finished) {
270 DCPOMATIC_ENABLE_WARNINGS
271                 stream->first_audio = frame_time (stream->stream (_format_context));
272         }
273 }
274
275
276 optional<ContentTime>
277 FFmpegExaminer::frame_time (AVStream* s) const
278 {
279         optional<ContentTime> t;
280
281         int64_t const bet = _frame->best_effort_timestamp;
282         if (bet != AV_NOPTS_VALUE) {
283                 t = ContentTime::from_seconds (bet * av_q2d (s->time_base));
284         }
285
286         return t;
287 }
288
289
290 optional<double>
291 FFmpegExaminer::video_frame_rate () const
292 {
293         DCPOMATIC_ASSERT (_video_stream);
294         return av_q2d(av_guess_frame_rate(_format_context, _format_context->streams[_video_stream.get()], 0));
295 }
296
297
298 dcp::Size
299 FFmpegExaminer::video_size () const
300 {
301         return dcp::Size (video_codec_context()->width, video_codec_context()->height);
302 }
303
304
305 /** @return Length according to our content's header */
306 Frame
307 FFmpegExaminer::video_length () const
308 {
309         return max (Frame (1), _video_length);
310 }
311
312
313 optional<double>
314 FFmpegExaminer::sample_aspect_ratio () const
315 {
316         DCPOMATIC_ASSERT (_video_stream);
317         auto sar = av_guess_sample_aspect_ratio (_format_context, _format_context->streams[_video_stream.get()], 0);
318         if (sar.num == 0) {
319                 /* I assume this means that we don't know */
320                 return {};
321         }
322         return double (sar.num) / sar.den;
323 }
324
325
326 string
327 FFmpegExaminer::subtitle_stream_name (AVStream* s) const
328 {
329         auto n = stream_name (s);
330
331         if (n.empty()) {
332                 n = _("unknown");
333         }
334
335         return n;
336 }
337
338
339 string
340 FFmpegExaminer::stream_name (AVStream* s) const
341 {
342         string n;
343
344         if (s->metadata) {
345                 auto const lang = av_dict_get (s->metadata, "language", 0, 0);
346                 if (lang) {
347                         n = lang->value;
348                 }
349
350                 auto const title = av_dict_get (s->metadata, "title", 0, 0);
351                 if (title) {
352                         if (!n.empty()) {
353                                 n += " ";
354                         }
355                         n += title->value;
356                 }
357         }
358
359         return n;
360 }
361
362
363 optional<int>
364 FFmpegExaminer::bits_per_pixel () const
365 {
366         if (video_codec_context()->pix_fmt == -1) {
367                 return {};
368         }
369
370         auto const d = av_pix_fmt_desc_get (video_codec_context()->pix_fmt);
371         DCPOMATIC_ASSERT (d);
372         return av_get_bits_per_pixel (d);
373 }
374
375
376 bool
377 FFmpegExaminer::yuv () const
378 {
379         switch (video_codec_context()->pix_fmt) {
380         case AV_PIX_FMT_YUV420P:
381         case AV_PIX_FMT_YUYV422:
382         case AV_PIX_FMT_YUV422P:
383         case AV_PIX_FMT_YUV444P:
384         case AV_PIX_FMT_YUV410P:
385         case AV_PIX_FMT_YUV411P:
386         case AV_PIX_FMT_YUVJ420P:
387         case AV_PIX_FMT_YUVJ422P:
388         case AV_PIX_FMT_YUVJ444P:
389         case AV_PIX_FMT_UYVY422:
390         case AV_PIX_FMT_UYYVYY411:
391         case AV_PIX_FMT_NV12:
392         case AV_PIX_FMT_NV21:
393         case AV_PIX_FMT_YUV440P:
394         case AV_PIX_FMT_YUVJ440P:
395         case AV_PIX_FMT_YUVA420P:
396         case AV_PIX_FMT_YUV420P16LE:
397         case AV_PIX_FMT_YUV420P16BE:
398         case AV_PIX_FMT_YUV422P16LE:
399         case AV_PIX_FMT_YUV422P16BE:
400         case AV_PIX_FMT_YUV444P16LE:
401         case AV_PIX_FMT_YUV444P16BE:
402         case AV_PIX_FMT_YUV420P9BE:
403         case AV_PIX_FMT_YUV420P9LE:
404         case AV_PIX_FMT_YUV420P10BE:
405         case AV_PIX_FMT_YUV420P10LE:
406         case AV_PIX_FMT_YUV422P10BE:
407         case AV_PIX_FMT_YUV422P10LE:
408         case AV_PIX_FMT_YUV444P9BE:
409         case AV_PIX_FMT_YUV444P9LE:
410         case AV_PIX_FMT_YUV444P10BE:
411         case AV_PIX_FMT_YUV444P10LE:
412         case AV_PIX_FMT_YUV422P9BE:
413         case AV_PIX_FMT_YUV422P9LE:
414         case AV_PIX_FMT_YUVA420P9BE:
415         case AV_PIX_FMT_YUVA420P9LE:
416         case AV_PIX_FMT_YUVA422P9BE:
417         case AV_PIX_FMT_YUVA422P9LE:
418         case AV_PIX_FMT_YUVA444P9BE:
419         case AV_PIX_FMT_YUVA444P9LE:
420         case AV_PIX_FMT_YUVA420P10BE:
421         case AV_PIX_FMT_YUVA420P10LE:
422         case AV_PIX_FMT_YUVA422P10BE:
423         case AV_PIX_FMT_YUVA422P10LE:
424         case AV_PIX_FMT_YUVA444P10BE:
425         case AV_PIX_FMT_YUVA444P10LE:
426         case AV_PIX_FMT_YUVA420P16BE:
427         case AV_PIX_FMT_YUVA420P16LE:
428         case AV_PIX_FMT_YUVA422P16BE:
429         case AV_PIX_FMT_YUVA422P16LE:
430         case AV_PIX_FMT_YUVA444P16BE:
431         case AV_PIX_FMT_YUVA444P16LE:
432         case AV_PIX_FMT_NV16:
433         case AV_PIX_FMT_NV20LE:
434         case AV_PIX_FMT_NV20BE:
435         case AV_PIX_FMT_YVYU422:
436         case AV_PIX_FMT_YUVA444P:
437         case AV_PIX_FMT_YUVA422P:
438         case AV_PIX_FMT_YUV420P12BE:
439         case AV_PIX_FMT_YUV420P12LE:
440         case AV_PIX_FMT_YUV420P14BE:
441         case AV_PIX_FMT_YUV420P14LE:
442         case AV_PIX_FMT_YUV422P12BE:
443         case AV_PIX_FMT_YUV422P12LE:
444         case AV_PIX_FMT_YUV422P14BE:
445         case AV_PIX_FMT_YUV422P14LE:
446         case AV_PIX_FMT_YUV444P12BE:
447         case AV_PIX_FMT_YUV444P12LE:
448         case AV_PIX_FMT_YUV444P14BE:
449         case AV_PIX_FMT_YUV444P14LE:
450         case AV_PIX_FMT_YUVJ411P:
451                 return true;
452         default:
453                 return false;
454         }
455 }
456
457
458 bool
459 FFmpegExaminer::has_video () const
460 {
461         return static_cast<bool>(_video_stream);
462 }
463
464
465 VideoRange
466 FFmpegExaminer::range () const
467 {
468         switch (color_range()) {
469         case AVCOL_RANGE_MPEG:
470         case AVCOL_RANGE_UNSPECIFIED:
471                 return VideoRange::VIDEO;
472         case AVCOL_RANGE_JPEG:
473         default:
474                 return VideoRange::FULL;
475         }
476 }