Untested fixes for files without audio.
[dcpomatic.git] / src / lib / ffmpeg_decoder.cc
1 /*
2     Copyright (C) 2012 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 /** @file  src/ffmpeg_decoder.cc
21  *  @brief A decoder using FFmpeg to decode content.
22  */
23
24 #include <stdexcept>
25 #include <vector>
26 #include <sstream>
27 #include <iomanip>
28 #include <iostream>
29 #include <stdint.h>
30 extern "C" {
31 #include <tiffio.h>
32 #include <libavcodec/avcodec.h>
33 #include <libavformat/avformat.h>
34 #include <libswscale/swscale.h>
35 #include <libpostproc/postprocess.h>
36 }
37 #include <sndfile.h>
38 #include "film.h"
39 #include "format.h"
40 #include "transcoder.h"
41 #include "job.h"
42 #include "filter.h"
43 #include "film_state.h"
44 #include "options.h"
45 #include "exceptions.h"
46 #include "image.h"
47 #include "util.h"
48 #include "log.h"
49 #include "ffmpeg_decoder.h"
50
51 using namespace std;
52 using namespace boost;
53
54 FFmpegDecoder::FFmpegDecoder (boost::shared_ptr<const FilmState> s, boost::shared_ptr<const Options> o, Job* j, Log* l, bool minimal, bool ignore_length)
55         : Decoder (s, o, j, l, minimal, ignore_length)
56         , _format_context (0)
57         , _video_stream (-1)
58         , _audio_stream (-1)
59         , _frame (0)
60         , _video_codec_context (0)
61         , _video_codec (0)
62         , _audio_codec_context (0)
63         , _audio_codec (0)
64 {
65         setup_general ();
66         setup_video ();
67         setup_audio ();
68 }
69
70 FFmpegDecoder::~FFmpegDecoder ()
71 {
72         if (_audio_codec_context) {
73                 avcodec_close (_audio_codec_context);
74         }
75         
76         if (_video_codec_context) {
77                 avcodec_close (_video_codec_context);
78         }
79         
80         av_free (_frame);
81         avformat_close_input (&_format_context);
82 }       
83
84 void
85 FFmpegDecoder::setup_general ()
86 {
87         int r;
88         
89         av_register_all ();
90
91         if ((r = avformat_open_input (&_format_context, _fs->content_path().c_str(), 0, 0)) != 0) {
92                 throw OpenFileError (_fs->content_path ());
93         }
94
95         if (avformat_find_stream_info (_format_context, 0) < 0) {
96                 throw DecodeError ("could not find stream information");
97         }
98
99         for (uint32_t i = 0; i < _format_context->nb_streams; ++i) {
100                 if (_format_context->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
101                         _video_stream = i;
102                 } else if (_format_context->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
103                         _audio_stream = i;
104                 }
105         }
106
107         if (_video_stream < 0) {
108                 throw DecodeError ("could not find video stream");
109         }
110
111         _frame = avcodec_alloc_frame ();
112         if (_frame == 0) {
113                 throw DecodeError ("could not allocate frame");
114         }
115 }
116
117 void
118 FFmpegDecoder::setup_video ()
119 {
120         _video_codec_context = _format_context->streams[_video_stream]->codec;
121         _video_codec = avcodec_find_decoder (_video_codec_context->codec_id);
122
123         if (_video_codec == 0) {
124                 throw DecodeError ("could not find video decoder");
125         }
126         
127         if (avcodec_open2 (_video_codec_context, _video_codec, 0) < 0) {
128                 throw DecodeError ("could not open video decoder");
129         }
130 }
131
132 void
133 FFmpegDecoder::setup_audio ()
134 {
135         if (_audio_stream < 0) {
136                 return;
137         }
138         
139         _audio_codec_context = _format_context->streams[_audio_stream]->codec;
140         _audio_codec = avcodec_find_decoder (_audio_codec_context->codec_id);
141
142         if (_audio_codec == 0) {
143                 throw DecodeError ("could not find audio decoder");
144         }
145         
146         if (avcodec_open2 (_audio_codec_context, _audio_codec, 0) < 0) {
147                 throw DecodeError ("could not open audio decoder");
148         }
149 }
150
151 bool
152 FFmpegDecoder::do_pass ()
153 {
154         int r = av_read_frame (_format_context, &_packet);
155         if (r < 0) {
156                 return true;
157         }
158
159         if (_packet.stream_index == _video_stream && _opt->decode_video) {
160                 
161                 int frame_finished;
162                 if (avcodec_decode_video2 (_video_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
163                         process_video (_frame);
164                 }
165
166         } else if (_audio_stream >= 0 && _packet.stream_index == _audio_stream && _opt->decode_audio) {
167                 
168                 avcodec_get_frame_defaults (_frame);
169                 
170                 int frame_finished;
171                 if (avcodec_decode_audio4 (_audio_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
172                         int const data_size = av_samples_get_buffer_size (
173                                 0, _audio_codec_context->channels, _frame->nb_samples, audio_sample_format (), 1
174                                 );
175
176                         assert (_audio_codec_context->channels == _fs->audio_channels);
177                         process_audio (_frame->data[0], data_size);
178                 }
179         }
180         
181         av_free_packet (&_packet);
182         return false;
183 }
184
185 int
186 FFmpegDecoder::length_in_frames () const
187 {
188         return (_format_context->duration / AV_TIME_BASE) * frames_per_second ();
189 }
190
191 float
192 FFmpegDecoder::frames_per_second () const
193 {
194         return av_q2d (_format_context->streams[_video_stream]->avg_frame_rate);
195 }
196
197 int
198 FFmpegDecoder::audio_channels () const
199 {
200         if (_audio_codec_context == 0) {
201                 return 0;
202         }
203         
204         return _audio_codec_context->channels;
205 }
206
207 int
208 FFmpegDecoder::audio_sample_rate () const
209 {
210         if (_audio_codec_context == 0) {
211                 return 0;
212         }
213         
214         return _audio_codec_context->sample_rate;
215 }
216
217 AVSampleFormat
218 FFmpegDecoder::audio_sample_format () const
219 {
220         if (_audio_codec_context == 0) {
221                 return (AVSampleFormat) 0;
222         }
223         
224         return _audio_codec_context->sample_fmt;
225 }
226
227 int64_t
228 FFmpegDecoder::audio_channel_layout () const
229 {
230         if (_audio_codec_context == 0) {
231                 return 0;
232         }
233         
234         return _audio_codec_context->channel_layout;
235 }
236
237 Size
238 FFmpegDecoder::native_size () const
239 {
240         return Size (_video_codec_context->width, _video_codec_context->height);
241 }
242
243 PixelFormat
244 FFmpegDecoder::pixel_format () const
245 {
246         return _video_codec_context->pix_fmt;
247 }
248
249 int
250 FFmpegDecoder::time_base_numerator () const
251 {
252         return _video_codec_context->time_base.num;
253 }
254
255 int
256 FFmpegDecoder::time_base_denominator () const
257 {
258         return _video_codec_context->time_base.den;
259 }
260
261 int
262 FFmpegDecoder::sample_aspect_ratio_numerator () const
263 {
264         return _video_codec_context->sample_aspect_ratio.num;
265 }
266
267 int
268 FFmpegDecoder::sample_aspect_ratio_denominator () const
269 {
270         return _video_codec_context->sample_aspect_ratio.den;
271 }
272