e5d356a27b7b213ffeaaa2a04f6e09f9c168ca94
[dcpomatic.git] / src / lib / ffmpeg_examiner.cc
1 /*
2     Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 extern "C" {
21 #include <libavcodec/avcodec.h>
22 #include <libavformat/avformat.h>
23 }
24 #include "ffmpeg_examiner.h"
25 #include "ffmpeg_content.h"
26
27 using std::string;
28 using std::stringstream;
29 using boost::shared_ptr;
30
31 FFmpegExaminer::FFmpegExaminer (shared_ptr<const FFmpegContent> c)
32         : FFmpeg (c)
33 {
34         /* Find audio and subtitle streams */
35
36         for (uint32_t i = 0; i < _format_context->nb_streams; ++i) {
37                 AVStream* s = _format_context->streams[i];
38                 if (s->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
39
40                         /* This is a hack; sometimes it seems that _audio_codec_context->channel_layout isn't set up,
41                            so bodge it here.  No idea why we should have to do this.
42                         */
43
44                         if (s->codec->channel_layout == 0) {
45                                 s->codec->channel_layout = av_get_default_channel_layout (s->codec->channels);
46                         }
47                         
48                         _audio_streams.push_back (
49                                 shared_ptr<FFmpegAudioStream> (
50                                         new FFmpegAudioStream (stream_name (s), i, s->codec->sample_rate, s->codec->channels)
51                                         )
52                                 );
53
54                 } else if (s->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
55                         _subtitle_streams.push_back (shared_ptr<FFmpegSubtitleStream> (new FFmpegSubtitleStream (stream_name (s), i)));
56                 }
57         }
58
59 }
60
61 float
62 FFmpegExaminer::video_frame_rate () const
63 {
64         AVStream* s = _format_context->streams[_video_stream];
65
66         if (s->avg_frame_rate.num && s->avg_frame_rate.den) {
67                 return av_q2d (s->avg_frame_rate);
68         }
69
70         return av_q2d (s->r_frame_rate);
71 }
72
73 libdcp::Size
74 FFmpegExaminer::video_size () const
75 {
76         return libdcp::Size (_video_codec_context->width, _video_codec_context->height);
77 }
78
79 /** @return Length (in video frames) according to our content's header */
80 ContentVideoFrame
81 FFmpegExaminer::video_length () const
82 {
83         return (double (_format_context->duration) / AV_TIME_BASE) * video_frame_rate();
84 }
85
86 string
87 FFmpegExaminer::stream_name (AVStream* s) const
88 {
89         stringstream n;
90
91         if (s->metadata) {
92                 AVDictionaryEntry const * lang = av_dict_get (s->metadata, "language", 0, 0);
93                 if (lang) {
94                         n << lang->value;
95                 }
96                 
97                 AVDictionaryEntry const * title = av_dict_get (s->metadata, "title", 0, 0);
98                 if (title) {
99                         if (!n.str().empty()) {
100                                 n << " ";
101                         }
102                         n << title->value;
103                 }
104         }
105
106         if (n.str().empty()) {
107                 n << "unknown";
108         }
109
110         return n.str ();
111 }