2 Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
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.
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.
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.
21 #include <libavcodec/avcodec.h>
22 #include <libavformat/avformat.h>
24 #include "ffmpeg_examiner.h"
25 #include "ffmpeg_content.h"
28 using std::stringstream;
29 using boost::shared_ptr;
31 FFmpegExaminer::FFmpegExaminer (shared_ptr<const FFmpegContent> c)
34 /* Find audio and subtitle streams */
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) {
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.
44 if (s->codec->channel_layout == 0) {
45 s->codec->channel_layout = av_get_default_channel_layout (s->codec->channels);
48 _audio_streams.push_back (
49 shared_ptr<FFmpegAudioStream> (
50 new FFmpegAudioStream (stream_name (s), i, s->codec->sample_rate, s->codec->channels)
54 } else if (s->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
55 _subtitle_streams.push_back (shared_ptr<FFmpegSubtitleStream> (new FFmpegSubtitleStream (stream_name (s), i)));
62 FFmpegExaminer::video_frame_rate () const
64 AVStream* s = _format_context->streams[_video_stream];
66 if (s->avg_frame_rate.num && s->avg_frame_rate.den) {
67 return av_q2d (s->avg_frame_rate);
70 return av_q2d (s->r_frame_rate);
74 FFmpegExaminer::video_size () const
76 return libdcp::Size (_video_codec_context->width, _video_codec_context->height);
79 /** @return Length (in video frames) according to our content's header */
81 FFmpegExaminer::video_length () const
83 return (double (_format_context->duration) / AV_TIME_BASE) * video_frame_rate();
87 FFmpegExaminer::stream_name (AVStream* s) const
92 AVDictionaryEntry const * lang = av_dict_get (s->metadata, "language", 0, 0);
97 AVDictionaryEntry const * title = av_dict_get (s->metadata, "title", 0, 0);
99 if (!n.str().empty()) {
106 if (n.str().empty()) {