Do alpha blending maybe right.
[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         , _subtitle_stream (-1)
60         , _frame (0)
61         , _video_codec_context (0)
62         , _video_codec (0)
63         , _audio_codec_context (0)
64         , _audio_codec (0)
65         , _subtitle_codec_context (0)
66         , _subtitle_codec (0)
67         , _have_subtitle (false)
68 {
69         setup_general ();
70         setup_video ();
71         setup_audio ();
72         setup_subtitle ();
73 }
74
75 FFmpegDecoder::~FFmpegDecoder ()
76 {
77         if (_audio_codec_context) {
78                 avcodec_close (_audio_codec_context);
79         }
80         
81         if (_video_codec_context) {
82                 avcodec_close (_video_codec_context);
83         }
84
85         if (_have_subtitle) {
86                 avsubtitle_free (&_subtitle);
87         }
88
89         if (_subtitle_codec_context) {
90                 avcodec_close (_subtitle_codec_context);
91         }
92         
93         av_free (_frame);
94         avformat_close_input (&_format_context);
95 }       
96
97 void
98 FFmpegDecoder::setup_general ()
99 {
100         int r;
101         
102         av_register_all ();
103
104         if ((r = avformat_open_input (&_format_context, _fs->content_path().c_str(), 0, 0)) != 0) {
105                 throw OpenFileError (_fs->content_path ());
106         }
107
108         if (avformat_find_stream_info (_format_context, 0) < 0) {
109                 throw DecodeError ("could not find stream information");
110         }
111
112         for (uint32_t i = 0; i < _format_context->nb_streams; ++i) {
113                 if (_format_context->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
114                         _video_stream = i;
115                 } else if (_format_context->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
116                         _audio_stream = i;
117                 } else if (_format_context->streams[i]->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
118                         _subtitle_stream = i;
119                 }
120         }
121
122         if (_video_stream < 0) {
123                 throw DecodeError ("could not find video stream");
124         }
125
126         _frame = avcodec_alloc_frame ();
127         if (_frame == 0) {
128                 throw DecodeError ("could not allocate frame");
129         }
130 }
131
132 void
133 FFmpegDecoder::setup_video ()
134 {
135         _video_codec_context = _format_context->streams[_video_stream]->codec;
136         _video_codec = avcodec_find_decoder (_video_codec_context->codec_id);
137
138         if (_video_codec == 0) {
139                 throw DecodeError ("could not find video decoder");
140         }
141         
142         if (avcodec_open2 (_video_codec_context, _video_codec, 0) < 0) {
143                 throw DecodeError ("could not open video decoder");
144         }
145 }
146
147 void
148 FFmpegDecoder::setup_audio ()
149 {
150         if (_audio_stream < 0) {
151                 return;
152         }
153         
154         _audio_codec_context = _format_context->streams[_audio_stream]->codec;
155         _audio_codec = avcodec_find_decoder (_audio_codec_context->codec_id);
156
157         if (_audio_codec == 0) {
158                 throw DecodeError ("could not find audio decoder");
159         }
160
161         if (avcodec_open2 (_audio_codec_context, _audio_codec, 0) < 0) {
162                 throw DecodeError ("could not open audio decoder");
163         }
164
165         /* This is a hack; sometimes it seems that _audio_codec_context->channel_layout isn't set up,
166            so bodge it here.  No idea why we should have to do this.
167         */
168
169         if (_audio_codec_context->channel_layout == 0) {
170                 _audio_codec_context->channel_layout = av_get_default_channel_layout (audio_channels ());
171         }
172 }
173
174 void
175 FFmpegDecoder::setup_subtitle ()
176 {
177         if (_subtitle_stream < 0) {
178                 return;
179         }
180
181         _subtitle_codec_context = _format_context->streams[_subtitle_stream]->codec;
182         _subtitle_codec = avcodec_find_decoder (_subtitle_codec_context->codec_id);
183
184         if (_subtitle_codec == 0) {
185                 throw DecodeError ("could not find subtitle decoder");
186         }
187         
188         if (avcodec_open2 (_subtitle_codec_context, _subtitle_codec, 0) < 0) {
189                 throw DecodeError ("could not open subtitle decoder");
190         }
191 }
192
193
194 bool
195 FFmpegDecoder::do_pass ()
196 {
197         int r = av_read_frame (_format_context, &_packet);
198         if (r < 0) {
199                 if (r != AVERROR_EOF) {
200                         throw DecodeError ("error on av_read_frame");
201                 }
202                 
203                 /* Get any remaining frames */
204                 
205                 _packet.data = 0;
206                 _packet.size = 0;
207
208                 int frame_finished;
209
210                 if (_opt->decode_video) {
211                         while (avcodec_decode_video2 (_video_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
212
213                                 process_video (_frame);
214                         }
215                 }
216
217                 if (_audio_stream >= 0 && _opt->decode_audio) {
218                         while (avcodec_decode_audio4 (_audio_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
219                                 int const data_size = av_samples_get_buffer_size (
220                                         0, _audio_codec_context->channels, _frame->nb_samples, audio_sample_format (), 1
221                                         );
222                                 
223                                 assert (_audio_codec_context->channels == _fs->audio_channels);
224                                 process_audio (_frame->data[0], data_size);
225                         }
226                 }
227
228                 return true;
229         }
230
231         if (_packet.stream_index == _video_stream && _opt->decode_video) {
232
233                 int frame_finished;
234                 if (avcodec_decode_video2 (_video_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
235                         
236                         cout << "decoded some video.\n";
237                         if (_have_subtitle) {
238                                 cout << "have a subtitle; " << _subtitle.num_rects << "\n";
239                                 for (unsigned int i = 0; i < _subtitle.num_rects; ++i) {
240                                         AVSubtitleRect* rect = _subtitle.rects[i];
241                                         if (rect->type != SUBTITLE_BITMAP) {
242                                                 cout << "not a bitmap\n";
243                                                 throw DecodeError ("non-bitmap subtitles not yet supported");
244                                         }
245
246                                         /* XXX: all this assumes YUV420 in _frame */
247                                         
248                                         assert (rect->nb_colors == 4);
249                                         assert (rect->pict.data[0]);
250
251                                         /* Start of the first line in the target frame */
252                                         uint8_t* frame_y_p = _frame->data[0] + rect->y * _frame->linesize[0];
253                                         uint8_t* frame_u_p = _frame->data[1] + (rect->y / 2) * _frame->linesize[1];
254                                         uint8_t* frame_v_p = _frame->data[2] + (rect->y / 2) * _frame->linesize[2];
255                                         
256                                         /* Start of the first line in the subtitle */
257                                         uint8_t* sub_p = rect->pict.data[0];
258
259                                         cout << "frame ls 0 is " << _frame->linesize[0] << "\n";
260                                         cout << "frame ls 1 is " << _frame->linesize[1] << "\n";
261                                         cout << "frame ls 2 is " << _frame->linesize[2] << "\n";
262
263                                         uint32_t* palette = (uint32_t *) rect->pict.data[1];
264                                         
265                                         for (int sub_y = 0; sub_y < rect->h; ++sub_y) {
266                                                 uint8_t* sub_line_p = sub_p;
267                                                 uint8_t* frame_line_y_p = frame_y_p + rect->x;
268                                                 uint8_t* frame_line_u_p = frame_u_p + (rect->x / 2);
269                                                 uint8_t* frame_line_v_p = frame_v_p + (rect->x / 2);
270
271                                                 uint8_t current_u = 0;
272                                                 uint8_t current_v = 0;
273                                                 int subsample_step = 0;
274                                                 
275                                                 for (int sub_x = 0; sub_x < rect->w; ++sub_x) {
276
277                                                         uint32_t const val = palette[*sub_line_p++];
278                                                         
279                                                         int const     red =  (val &       0xff);
280                                                         int const   green =  (val &     0xff00) >> 8;
281                                                         int const    blue =  (val &   0xff0000) >> 16;
282                                                         float const alpha = ((val & 0xff000000) >> 24) / 255.0;
283
284                                                         int const cy = *frame_line_y_p;
285
286                                                         *frame_line_y_p++ = int (cy * (1 - alpha)) + int (RGB_TO_Y_CCIR (red, green, blue) * alpha);
287                                                         
288                                                         current_u |= ((RGB_TO_U_CCIR (red, green, blue, 0) & 0xf0) >> 4) << (4 * subsample_step);
289                                                         current_v |= ((RGB_TO_V_CCIR (red, green, blue, 0) & 0xf0) >> 4) << (4 * subsample_step);
290
291                                                         if (subsample_step == 1 && (sub_y % 2) == 0) {
292                                                                 int const cu = *frame_line_u_p;
293                                                                 int const cv = *frame_line_v_p;
294                                                                 *frame_line_u_p++ = int (cu * (1 - alpha)) + int (current_u * alpha);
295                                                                 *frame_line_v_p++ = int (cv * (1 - alpha)) + int (current_v * alpha);
296                                                                 current_u = current_v = 0;
297                                                         }
298
299                                                         subsample_step = (subsample_step + 1) % 2;
300                                                 }
301
302                                                 sub_p += rect->pict.linesize[0];
303                                                 frame_y_p += _frame->linesize[0];
304                                                 if ((sub_y % 2) == 0) {
305                                                         frame_u_p += _frame->linesize[1];
306                                                         frame_v_p += _frame->linesize[2];
307                                                 }
308                                         }
309                                 }
310                         }
311                         
312                         process_video (_frame);
313                 }
314
315         } else if (_audio_stream >= 0 && _packet.stream_index == _audio_stream && _opt->decode_audio) {
316                 
317                 avcodec_get_frame_defaults (_frame);
318                 
319                 int frame_finished;
320                 if (avcodec_decode_audio4 (_audio_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
321                         int const data_size = av_samples_get_buffer_size (
322                                 0, _audio_codec_context->channels, _frame->nb_samples, audio_sample_format (), 1
323                                 );
324
325                         assert (_audio_codec_context->channels == _fs->audio_channels);
326                         process_audio (_frame->data[0], data_size);
327                 }
328
329         } else if (_subtitle_stream >= 0 && _packet.stream_index == _subtitle_stream) {
330
331                 if (_have_subtitle) {
332                         avsubtitle_free (&_subtitle);
333                         _have_subtitle = false;
334                 }
335
336                 int got_subtitle;
337                 if (avcodec_decode_subtitle2 (_subtitle_codec_context, &_subtitle, &got_subtitle, &_packet) && got_subtitle) {
338                         cout << "got a subtitle.\n";
339                         _have_subtitle = true;
340                 }
341         }
342         
343         av_free_packet (&_packet);
344         return false;
345 }
346
347 int
348 FFmpegDecoder::length_in_frames () const
349 {
350         return (_format_context->duration / AV_TIME_BASE) * frames_per_second ();
351 }
352
353 float
354 FFmpegDecoder::frames_per_second () const
355 {
356         return av_q2d (_format_context->streams[_video_stream]->avg_frame_rate);
357 }
358
359 int
360 FFmpegDecoder::audio_channels () const
361 {
362         if (_audio_codec_context == 0) {
363                 return 0;
364         }
365
366         return _audio_codec_context->channels;
367 }
368
369 int
370 FFmpegDecoder::audio_sample_rate () const
371 {
372         if (_audio_codec_context == 0) {
373                 return 0;
374         }
375         
376         return _audio_codec_context->sample_rate;
377 }
378
379 AVSampleFormat
380 FFmpegDecoder::audio_sample_format () const
381 {
382         if (_audio_codec_context == 0) {
383                 return (AVSampleFormat) 0;
384         }
385         
386         return _audio_codec_context->sample_fmt;
387 }
388
389 int64_t
390 FFmpegDecoder::audio_channel_layout () const
391 {
392         if (_audio_codec_context == 0) {
393                 return 0;
394         }
395         
396         return _audio_codec_context->channel_layout;
397 }
398
399 Size
400 FFmpegDecoder::native_size () const
401 {
402         return Size (_video_codec_context->width, _video_codec_context->height);
403 }
404
405 PixelFormat
406 FFmpegDecoder::pixel_format () const
407 {
408         return _video_codec_context->pix_fmt;
409 }
410
411 int
412 FFmpegDecoder::time_base_numerator () const
413 {
414         return _video_codec_context->time_base.num;
415 }
416
417 int
418 FFmpegDecoder::time_base_denominator () const
419 {
420         return _video_codec_context->time_base.den;
421 }
422
423 int
424 FFmpegDecoder::sample_aspect_ratio_numerator () const
425 {
426         return _video_codec_context->sample_aspect_ratio.num;
427 }
428
429 int
430 FFmpegDecoder::sample_aspect_ratio_denominator () const
431 {
432         return _video_codec_context->sample_aspect_ratio.den;
433 }
434