09788cd0cb887599c939cf31439df9e23def8c9d
[dcpomatic.git] / src / lib / 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/decoder.cc
21  *  @brief Parent class for decoders of content.
22  */
23
24 #include <iostream>
25 #include <stdint.h>
26 #include <boost/lexical_cast.hpp>
27 #include "film.h"
28 #include "format.h"
29 #include "job.h"
30 #include "options.h"
31 #include "exceptions.h"
32 #include "image.h"
33 #include "util.h"
34 #include "log.h"
35 #include "decoder.h"
36 #include "delay_line.h"
37 #include "subtitle.h"
38 #include "filter_graph.h"
39
40 using std::string;
41 using std::stringstream;
42 using std::min;
43 using std::pair;
44 using std::list;
45 using boost::shared_ptr;
46
47 /** @param f Film.
48  *  @param o Options.
49  *  @param j Job that we are running within, or 0
50  *  @param minimal true to do the bare minimum of work; just run through the content.  Useful for acquiring
51  *  accurate frame counts as quickly as possible.  This generates no video or audio output.
52  */
53 Decoder::Decoder (boost::shared_ptr<Film> f, boost::shared_ptr<const Options> o, Job* j)
54         : _film (f)
55         , _opt (o)
56         , _job (j)
57         , _video_frame (0)
58         , _audio_frame (0)
59         , _delay_line (0)
60         , _delay_in_frames (0)
61 {
62         
63 }
64
65 Decoder::~Decoder ()
66 {
67         delete _delay_line;
68 }
69
70 /** Start off a decode processing run.  This should only be called once on
71  *  a given Decoder object.
72  */
73 void
74 Decoder::process_begin ()
75 {
76         _delay_in_frames = _film->audio_delay() * audio_sample_rate() / 1000;
77         _delay_line = new DelayLine (audio_channels(), _delay_in_frames);
78 }
79
80 /** Finish off a decode processing run */
81 void
82 Decoder::process_end ()
83 {
84         if (_delay_in_frames < 0 && _opt->decode_audio && audio_channels()) {
85                 shared_ptr<AudioBuffers> b (new AudioBuffers (audio_channels(), -_delay_in_frames));
86                 b->make_silent ();
87                 emit_audio (b);
88         }
89
90         if (_opt->decode_audio && audio_channels()) {
91
92                 /* Ensure that our video and audio emissions are the same length */
93
94                 int64_t audio_short_by_frames = video_frames_to_audio_frames (_video_frame, audio_sample_rate(), frames_per_second()) - _audio_frame;
95
96                 _film->log()->log (
97                         String::compose (
98                                 "Decoder has emitted %1 video frames (which equals %2 audio frames) and %3 audio frames",
99                                 _video_frame,
100                                 video_frames_to_audio_frames (_video_frame, audio_sample_rate(), frames_per_second()),
101                                 _audio_frame
102                                 )
103                         );
104
105                 if (audio_short_by_frames < 0) {
106
107                         _film->log()->log (String::compose ("Emitted %1 too many audio frames", -audio_short_by_frames));
108                         
109                         /* We have emitted more audio than video.  Emit enough black video frames so that we reverse this */
110                         int const black_video_frames = ceil (-audio_short_by_frames * frames_per_second() / audio_sample_rate());
111
112                         _film->log()->log (String::compose ("Emitting %1 frames of black video", black_video_frames));
113
114                         shared_ptr<Image> black (new CompactImage (pixel_format(), native_size()));
115                         black->make_black ();
116                         for (int i = 0; i < black_video_frames; ++i) {
117                                 emit_video (black, shared_ptr<Subtitle> ());
118                         }
119
120                         /* Now recompute our check value */
121                         audio_short_by_frames = video_frames_to_audio_frames (_video_frame, audio_sample_rate(), frames_per_second()) - _audio_frame;
122                 }
123         
124                 if (audio_short_by_frames > 0) {
125                         _film->log()->log (String::compose ("Emitted %1 too few audio frames", audio_short_by_frames));
126                         shared_ptr<AudioBuffers> b (new AudioBuffers (audio_channels(), audio_short_by_frames));
127                         b->make_silent ();
128                         emit_audio (b);
129                 }
130         }
131 }
132
133 /** Start decoding */
134 void
135 Decoder::go ()
136 {
137         process_begin ();
138
139         if (_job && !_film->dcp_length()) {
140                 _job->set_progress_unknown ();
141         }
142
143         while (pass () == false) {
144                 if (_job && _film->dcp_length()) {
145                         _job->set_progress (float (_video_frame) / _film->length().get());
146                 }
147         }
148
149         process_end ();
150 }
151
152 /** Called by subclasses to tell the world that some audio data is ready
153  *  @param data Audio data, in Film::audio_sample_format.
154  *  @param size Number of bytes of data.
155  */
156 void
157 Decoder::process_audio (shared_ptr<AudioBuffers> audio)
158 {
159         /* Maybe apply gain */
160         if (_film->audio_gain() != 0) {
161                 float const linear_gain = pow (10, _film->audio_gain() / 20);
162                 for (int i = 0; i < audio->channels(); ++i) {
163                         for (int j = 0; j < audio->frames(); ++j) {
164                                 audio->data(i)[j] *= linear_gain;
165                         }
166                 }
167         }
168
169         _delay_line->feed (audio);
170         emit_audio (audio);
171 }
172
173 /** Called by subclasses to tell the world that some video data is ready.
174  *  We do some post-processing / filtering then emit it for listeners.
175  *  @param frame to decode; caller manages memory.
176  */
177 void
178 Decoder::process_video (AVFrame* frame)
179 {
180         shared_ptr<FilterGraph> graph;
181
182         list<shared_ptr<FilterGraph> >::iterator i = _filter_graphs.begin();
183         while (i != _filter_graphs.end() && !(*i)->can_process (Size (frame->width, frame->height), (AVPixelFormat) frame->format)) {
184                 ++i;
185         }
186
187         if (i == _filter_graphs.end ()) {
188                 graph.reset (new FilterGraph (_film, this, _opt->apply_crop, Size (frame->width, frame->height), (AVPixelFormat) frame->format));
189                 _filter_graphs.push_back (graph);
190                 _film->log()->log (String::compose ("New graph for %1x%2, pixel format %3", frame->width, frame->height, frame->format));
191         } else {
192                 graph = *i;
193         }
194
195         list<shared_ptr<Image> > images = graph->process (frame);
196
197         for (list<shared_ptr<Image> >::iterator i = images.begin(); i != images.end(); ++i) {
198                 shared_ptr<Subtitle> sub;
199                 if (_timed_subtitle && _timed_subtitle->displayed_at (double (video_frame()) / _film->frames_per_second())) {
200                         sub = _timed_subtitle->subtitle ();
201                 }
202
203                 emit_video (*i, sub);
204         }
205 }
206
207 void
208 Decoder::repeat_last_video ()
209 {
210         if (!_last_image) {
211                 _last_image.reset (new CompactImage (pixel_format(), native_size()));
212                 _last_image->make_black ();
213         }
214
215         emit_video (_last_image, _last_subtitle);
216 }
217
218 void
219 Decoder::emit_video (shared_ptr<Image> image, shared_ptr<Subtitle> sub)
220 {
221         TIMING ("Decoder emits %1", _video_frame);
222         Video (image, _video_frame, sub);
223         ++_video_frame;
224
225         _last_image = image;
226         _last_subtitle = sub;
227 }
228
229 void
230 Decoder::emit_audio (shared_ptr<AudioBuffers> audio)
231 {
232         Audio (audio, _audio_frame);
233         _audio_frame += audio->frames ();
234 }
235
236 void
237 Decoder::process_subtitle (shared_ptr<TimedSubtitle> s)
238 {
239         _timed_subtitle = s;
240         
241         if (_timed_subtitle && _opt->apply_crop) {
242                 Position const p = _timed_subtitle->subtitle()->position ();
243                 _timed_subtitle->subtitle()->set_position (Position (p.x - _film->crop().left, p.y - _film->crop().top));
244         }
245 }
246
247 int
248 Decoder::bytes_per_audio_sample () const
249 {
250         return av_get_bytes_per_sample (audio_sample_format ());
251 }