ca35c6e81279b7c90f6e029a5c0ae03e9746f1f7
[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 #include <boost/lexical_cast.hpp>
31 extern "C" {
32 #include <tiffio.h>
33 #include <libavcodec/avcodec.h>
34 #include <libavformat/avformat.h>
35 #include <libswscale/swscale.h>
36 #include <libpostproc/postprocess.h>
37 }
38 #include <sndfile.h>
39 #include "film.h"
40 #include "format.h"
41 #include "transcoder.h"
42 #include "job.h"
43 #include "filter.h"
44 #include "film_state.h"
45 #include "options.h"
46 #include "exceptions.h"
47 #include "image.h"
48 #include "util.h"
49 #include "log.h"
50 #include "ffmpeg_decoder.h"
51
52 using namespace std;
53 using namespace boost;
54
55 FFmpegDecoder::FFmpegDecoder (boost::shared_ptr<const FilmState> s, boost::shared_ptr<const Options> o, Job* j, Log* l, bool minimal, bool ignore_length)
56         : Decoder (s, o, j, l, minimal, ignore_length)
57         , _format_context (0)
58         , _video_stream (-1)
59         , _audio_stream (-1)
60         , _subtitle_stream (-1)
61         , _frame (0)
62         , _video_codec_context (0)
63         , _video_codec (0)
64         , _audio_codec_context (0)
65         , _audio_codec (0)
66         , _subtitle_codec_context (0)
67         , _subtitle_codec (0)
68         , _have_subtitle (false)
69 {
70         setup_general ();
71         setup_video ();
72         setup_audio ();
73         setup_subtitle ();
74 }
75
76 FFmpegDecoder::~FFmpegDecoder ()
77 {
78         if (_audio_codec_context) {
79                 avcodec_close (_audio_codec_context);
80         }
81         
82         if (_video_codec_context) {
83                 avcodec_close (_video_codec_context);
84         }
85
86         if (_have_subtitle) {
87                 avsubtitle_free (&_subtitle);
88         }
89
90         if (_subtitle_codec_context) {
91                 avcodec_close (_subtitle_codec_context);
92         }
93         
94         av_free (_frame);
95         avformat_close_input (&_format_context);
96 }       
97
98 void
99 FFmpegDecoder::setup_general ()
100 {
101         int r;
102         
103         av_register_all ();
104
105         if ((r = avformat_open_input (&_format_context, _fs->content_path().c_str(), 0, 0)) != 0) {
106                 throw OpenFileError (_fs->content_path ());
107         }
108
109         if (avformat_find_stream_info (_format_context, 0) < 0) {
110                 throw DecodeError ("could not find stream information");
111         }
112
113         for (uint32_t i = 0; i < _format_context->nb_streams; ++i) {
114                 if (_format_context->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
115                         _video_stream = i;
116                 } else if (_format_context->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
117                         _audio_stream = i;
118                 } else if (_format_context->streams[i]->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
119                         _subtitle_stream = i;
120                 }
121         }
122
123         if (_video_stream < 0) {
124                 throw DecodeError ("could not find video stream");
125         }
126
127         _frame = avcodec_alloc_frame ();
128         if (_frame == 0) {
129                 throw DecodeError ("could not allocate frame");
130         }
131 }
132
133 void
134 FFmpegDecoder::setup_video ()
135 {
136         _video_codec_context = _format_context->streams[_video_stream]->codec;
137         _video_codec = avcodec_find_decoder (_video_codec_context->codec_id);
138
139         if (_video_codec == 0) {
140                 throw DecodeError ("could not find video decoder");
141         }
142         
143         if (avcodec_open2 (_video_codec_context, _video_codec, 0) < 0) {
144                 throw DecodeError ("could not open video decoder");
145         }
146 }
147
148 void
149 FFmpegDecoder::setup_audio ()
150 {
151         if (_audio_stream < 0) {
152                 return;
153         }
154         
155         _audio_codec_context = _format_context->streams[_audio_stream]->codec;
156         _audio_codec = avcodec_find_decoder (_audio_codec_context->codec_id);
157
158         if (_audio_codec == 0) {
159                 throw DecodeError ("could not find audio decoder");
160         }
161
162         if (avcodec_open2 (_audio_codec_context, _audio_codec, 0) < 0) {
163                 throw DecodeError ("could not open audio decoder");
164         }
165
166         /* This is a hack; sometimes it seems that _audio_codec_context->channel_layout isn't set up,
167            so bodge it here.  No idea why we should have to do this.
168         */
169
170         if (_audio_codec_context->channel_layout == 0) {
171                 _audio_codec_context->channel_layout = av_get_default_channel_layout (audio_channels ());
172         }
173 }
174
175 void
176 FFmpegDecoder::setup_subtitle ()
177 {
178         if (_subtitle_stream < 0) {
179                 return;
180         }
181
182         _subtitle_codec_context = _format_context->streams[_subtitle_stream]->codec;
183         _subtitle_codec = avcodec_find_decoder (_subtitle_codec_context->codec_id);
184
185         if (_subtitle_codec == 0) {
186                 throw DecodeError ("could not find subtitle decoder");
187         }
188         
189         if (avcodec_open2 (_subtitle_codec_context, _subtitle_codec, 0) < 0) {
190                 throw DecodeError ("could not open subtitle decoder");
191         }
192 }
193
194
195 bool
196 FFmpegDecoder::do_pass ()
197 {
198         int r = av_read_frame (_format_context, &_packet);
199         if (r < 0) {
200                 if (r != AVERROR_EOF) {
201                         throw DecodeError ("error on av_read_frame");
202                 }
203                 
204                 /* Get any remaining frames */
205                 
206                 _packet.data = 0;
207                 _packet.size = 0;
208
209                 int frame_finished;
210
211                 while (avcodec_decode_video2 (_video_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
212                         process_video (_frame);
213                 }
214
215                 if (_audio_stream >= 0 && _opt->decode_audio) {
216                         while (avcodec_decode_audio4 (_audio_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
217                                 int const data_size = av_samples_get_buffer_size (
218                                         0, _audio_codec_context->channels, _frame->nb_samples, audio_sample_format (), 1
219                                         );
220                                 
221                                 assert (_audio_codec_context->channels == _fs->audio_channels);
222                                 process_audio (_frame->data[0], data_size);
223                         }
224                 }
225
226                 return true;
227         }
228
229         if (_packet.stream_index == _video_stream) {
230
231                 int frame_finished;
232                 if (avcodec_decode_video2 (_video_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
233                         process_video (_frame);
234                 }
235
236         } else if (_audio_stream >= 0 && _packet.stream_index == _audio_stream && _opt->decode_audio) {
237                 
238                 avcodec_get_frame_defaults (_frame);
239                 
240                 int frame_finished;
241                 if (avcodec_decode_audio4 (_audio_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
242                         int const data_size = av_samples_get_buffer_size (
243                                 0, _audio_codec_context->channels, _frame->nb_samples, audio_sample_format (), 1
244                                 );
245
246                         assert (_audio_codec_context->channels == _fs->audio_channels);
247                         process_audio (_frame->data[0], data_size);
248                 }
249
250         } else if (_subtitle_stream >= 0 && _packet.stream_index == _subtitle_stream && _fs->with_subtitles) {
251
252                 if (_have_subtitle) {
253                         avsubtitle_free (&_subtitle);
254                         _have_subtitle = false;
255                 }
256
257                 int got_subtitle;
258                 if (avcodec_decode_subtitle2 (_subtitle_codec_context, &_subtitle, &got_subtitle, &_packet) && got_subtitle) {
259                         _have_subtitle = true;
260                 }
261         }
262         
263         av_free_packet (&_packet);
264         return false;
265 }
266
267 int
268 FFmpegDecoder::length_in_frames () const
269 {
270         return (_format_context->duration / AV_TIME_BASE) * frames_per_second ();
271 }
272
273 float
274 FFmpegDecoder::frames_per_second () const
275 {
276         AVStream* s = _format_context->streams[_video_stream];
277
278         if (s->avg_frame_rate.num && s->avg_frame_rate.den) {
279                 return av_q2d (s->avg_frame_rate);
280         }
281
282         return av_q2d (s->r_frame_rate);
283 }
284
285 int
286 FFmpegDecoder::audio_channels () const
287 {
288         if (_audio_codec_context == 0) {
289                 return 0;
290         }
291
292         return _audio_codec_context->channels;
293 }
294
295 int
296 FFmpegDecoder::audio_sample_rate () const
297 {
298         if (_audio_codec_context == 0) {
299                 return 0;
300         }
301         
302         return _audio_codec_context->sample_rate;
303 }
304
305 AVSampleFormat
306 FFmpegDecoder::audio_sample_format () const
307 {
308         if (_audio_codec_context == 0) {
309                 return (AVSampleFormat) 0;
310         }
311         
312         return _audio_codec_context->sample_fmt;
313 }
314
315 int64_t
316 FFmpegDecoder::audio_channel_layout () const
317 {
318         if (_audio_codec_context == 0) {
319                 return 0;
320         }
321         
322         return _audio_codec_context->channel_layout;
323 }
324
325 Size
326 FFmpegDecoder::native_size () const
327 {
328         return Size (_video_codec_context->width, _video_codec_context->height);
329 }
330
331 PixelFormat
332 FFmpegDecoder::pixel_format () const
333 {
334         return _video_codec_context->pix_fmt;
335 }
336
337 int
338 FFmpegDecoder::time_base_numerator () const
339 {
340         return _video_codec_context->time_base.num;
341 }
342
343 int
344 FFmpegDecoder::time_base_denominator () const
345 {
346         return _video_codec_context->time_base.den;
347 }
348
349 int
350 FFmpegDecoder::sample_aspect_ratio_numerator () const
351 {
352         return _video_codec_context->sample_aspect_ratio.num;
353 }
354
355 int
356 FFmpegDecoder::sample_aspect_ratio_denominator () const
357 {
358         return _video_codec_context->sample_aspect_ratio.den;
359 }
360
361 void
362 FFmpegDecoder::overlay (shared_ptr<Image> image) const
363 {
364         if (!_have_subtitle) {
365                 return;
366         }
367         
368         /* subtitle PTS in seconds */
369         float const packet_time = (_subtitle.pts / AV_TIME_BASE) + float (_subtitle.pts % AV_TIME_BASE) / 1e6;
370         /* hence start time for this sub */
371         float const from = packet_time + (float (_subtitle.start_display_time) / 1e3);
372         float const to = packet_time + (float (_subtitle.end_display_time) / 1e3);
373         
374         float const video_frame_time = float (last_video_frame ()) / rint (_fs->frames_per_second);
375
376         if (from > video_frame_time || video_frame_time < to) {
377                 return;
378         }
379
380         for (unsigned int i = 0; i < _subtitle.num_rects; ++i) {
381                 AVSubtitleRect* rect = _subtitle.rects[i];
382                 if (rect->type != SUBTITLE_BITMAP) {
383                         throw DecodeError ("non-bitmap subtitles not yet supported");
384                 }
385
386                 /* XXX: all this assumes YUV420 in image */
387                 
388                 assert (rect->pict.data[0]);
389
390                 /* Start of the first line in the target image */
391                 uint8_t* frame_y_p = image->data()[0] + rect->y * image->line_size()[0];
392                 uint8_t* frame_u_p = image->data()[1] + (rect->y / 2) * image->line_size()[1];
393                 uint8_t* frame_v_p = image->data()[2] + (rect->y / 2) * image->line_size()[2];
394
395                 int const hlim = min (rect->y + rect->h, image->size().height) - rect->y;
396                 
397                 /* Start of the first line in the subtitle */
398                 uint8_t* sub_p = rect->pict.data[0];
399                 /* sub_p looks up into a RGB palette which is here */
400                 uint32_t const * palette = (uint32_t *) rect->pict.data[1];
401                 
402                 for (int sub_y = 0; sub_y < hlim; ++sub_y) {
403                         /* Pointers to the start of this line */
404                         uint8_t* sub_line_p = sub_p;
405                         uint8_t* frame_line_y_p = frame_y_p + rect->x;
406                         uint8_t* frame_line_u_p = frame_u_p + (rect->x / 2);
407                         uint8_t* frame_line_v_p = frame_v_p + (rect->x / 2);
408                         
409                         /* U and V are subsampled */
410                         uint8_t next_u = 0;
411                         uint8_t next_v = 0;
412                         int subsample_step = 0;
413                         
414                         for (int sub_x = 0; sub_x < rect->w; ++sub_x) {
415                                 
416                                 /* RGB value for this subtitle pixel */
417                                 uint32_t const val = palette[*sub_line_p++];
418                                 
419                                 int const     red =  (val &       0xff);
420                                 int const   green =  (val &     0xff00) >> 8;
421                                 int const    blue =  (val &   0xff0000) >> 16;
422                                 float const alpha = ((val & 0xff000000) >> 24) / 255.0;
423                                 
424                                 /* Alpha-blend Y */
425                                 int const cy = *frame_line_y_p;
426                                 *frame_line_y_p++ = int (cy * (1 - alpha)) + int (RGB_TO_Y_CCIR (red, green, blue) * alpha);
427                                 
428                                 /* Store up U and V */
429                                 next_u |= ((RGB_TO_U_CCIR (red, green, blue, 0) & 0xf0) >> 4) << (4 * subsample_step);
430                                 next_v |= ((RGB_TO_V_CCIR (red, green, blue, 0) & 0xf0) >> 4) << (4 * subsample_step);
431                                 
432                                 if (subsample_step == 1 && (sub_y % 2) == 0) {
433                                         int const cu = *frame_line_u_p;
434                                         int const cv = *frame_line_v_p;
435
436                                         *frame_line_u_p++ =
437                                                 int (((cu & 0x0f) * (1 - alpha) + (next_u & 0x0f) * alpha)) |
438                                                 int (((cu & 0xf0) * (1 - alpha) + (next_u & 0xf0) * alpha));
439
440                                         *frame_line_v_p++ = 
441                                                 int (((cv & 0x0f) * (1 - alpha) + (next_v & 0x0f) * alpha)) |
442                                                 int (((cv & 0xf0) * (1 - alpha) + (next_v & 0xf0) * alpha));
443                                         
444                                         next_u = next_v = 0;
445                                 }
446                                 
447                                 subsample_step = (subsample_step + 1) % 2;
448                         }
449                         
450                         sub_p += rect->pict.linesize[0];
451                         frame_y_p += image->line_size()[0];
452                         if ((sub_y % 2) == 0) {
453                                 frame_u_p += image->line_size()[1];
454                                 frame_v_p += image->line_size()[2];
455                         }
456                 }
457         }
458 }
459     
460 bool
461 FFmpegDecoder::has_subtitles () const
462 {
463         return (_subtitle_stream != -1);
464 }