Hopefully fix up still-image generation.
[dcpomatic.git] / src / lib / matcher.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 #include "matcher.h"
21 #include "image.h"
22 #include "log.h"
23
24 #include "i18n.h"
25
26 using std::min;
27 using std::cout;
28 using std::list;
29 using boost::shared_ptr;
30
31 Matcher::Matcher (Log* log, int sample_rate, float frames_per_second)
32         : Processor (log)
33         , _sample_rate (sample_rate)
34         , _frames_per_second (frames_per_second)
35         , _video_frames (0)
36         , _audio_frames (0)
37 {
38
39 }
40
41 void
42 Matcher::process_video (boost::shared_ptr<Image> image, bool same, boost::shared_ptr<Subtitle> sub, double t)
43 {
44         _pixel_format = image->pixel_format ();
45         _size = image->size ();
46
47         _log->log(String::compose("Matcher video @ %1 (same=%2)", t, same));
48
49         if (!_first_input) {
50                 _first_input = t;
51         }
52
53         /* Video before audio is fine, since we can make up an arbitrary difference
54            with audio samples (contrasting with video which is quantised to frames)
55         */
56
57         /* Difference between where this video is and where it should be */
58         double const delta = t - _first_input.get() - _video_frames / _frames_per_second;
59         double const one_frame = 1 / _frames_per_second;
60         
61         if (delta > one_frame) {
62                 /* Insert frames to make up the difference */
63                 int const extra = rint (delta / one_frame);
64                 for (int i = 0; i < extra; ++i) {
65                         repeat_last_video ();
66                         _log->log (String::compose ("Extra video frame inserted at %1s", _video_frames / _frames_per_second));
67                 }
68         }
69                 
70         if (delta > -one_frame) {
71                 Video (image, same, sub);
72                 ++_video_frames;
73         } else {
74                 /* We are omitting a frame to keep things right */
75                 _log->log (String::compose ("Frame removed at %1s", t));
76         }
77         
78         if (!_pending_audio.empty() && _video_frames == 1) {
79                 /* First video since we got audio */
80                 fix_start ();
81         }
82                 
83         _last_image = image;
84         _last_subtitle = sub;
85 }
86
87 void
88 Matcher::process_audio (boost::shared_ptr<AudioBuffers> b, double t)
89 {
90         _channels = b->channels ();
91         
92         if (!_first_input) {
93                 _first_input = t;
94         }
95         
96         if (_video_frames == 0) {
97                 /* No video yet; we must postpone these data until we have some */
98                 _pending_audio.push_back (AudioRecord (b, t));
99         } else if (_video_frames > 0 && _pending_audio.empty ()) {
100                 /* First audio since we got video */
101                 _pending_audio.push_back (AudioRecord (b, t));
102                 fix_start ();
103         } else {
104                 /* Normal running.  We assume audio time stamps are consecutive */
105                 Audio (b);
106                 _audio_frames += b->frames ();
107         }
108 }
109
110 void
111 Matcher::process_end ()
112 {
113         if (_audio_frames == 0 || !_pixel_format || !_size || !_channels) {
114                 /* We won't do anything */
115                 return;
116         }
117
118         _log->log (String::compose ("Matcher has seen %1 video frames (which equals %2 audio frames) and %3 audio frames",
119                                     _video_frames, video_frames_to_audio_frames (_video_frames, _sample_rate, _frames_per_second), _audio_frames));
120         
121         match ((double (_audio_frames) / _sample_rate) - (double (_video_frames) / _frames_per_second));
122 }
123
124 void
125 Matcher::fix_start ()
126 {
127         assert (!_pending_audio.empty ());
128         assert (_first_input);
129
130         _log->log (String::compose ("Fixing start; start at %1, audio at %2", _first_input.get(), _pending_audio.front().time));
131
132         /* This will not add any video frames, since the input parameter will always be -ve.
133            Indeed, it cannot add any, since we've already started adding "real" video.
134         */
135         match (_first_input.get() - _pending_audio.front().time);
136
137         for (list<AudioRecord>::iterator i = _pending_audio.begin(); i != _pending_audio.end(); ++i) {
138                 process_audio (i->audio, i->time);
139         }
140         
141         _pending_audio.clear ();
142 }
143
144 void
145 Matcher::match (double extra_video_needed)
146 {
147         if (extra_video_needed > 0) {
148
149                 /* Emit black video frames */
150                 
151                 int const black_video_frames = ceil (extra_video_needed * _frames_per_second);
152
153                 _log->log (String::compose (N_("Emitting %1 frames of black video"), black_video_frames));
154
155                 shared_ptr<Image> black (new SimpleImage (_pixel_format.get(), _size.get(), true));
156                 black->make_black ();
157                 for (int i = 0; i < black_video_frames; ++i) {
158                         Video (black, i != 0, shared_ptr<Subtitle>());
159                         ++_video_frames;
160                 }
161
162                 extra_video_needed -= black_video_frames / _frames_per_second;
163         }
164
165         if (extra_video_needed < 0) {
166                 
167                 /* Emit silence */
168                 
169                 int64_t to_do = -extra_video_needed * _sample_rate;
170                 _log->log (String::compose (N_("Emitted %1 frames of silence"), to_do));
171
172                 /* Do things in half second blocks as I think there may be limits
173                    to what FFmpeg (and in particular the resampler) can cope with.
174                 */
175                 int64_t const block = _sample_rate / 2;
176                 shared_ptr<AudioBuffers> b (new AudioBuffers (_channels.get(), block));
177                 b->make_silent ();
178                 
179                 while (to_do > 0) {
180                         int64_t const this_time = min (to_do, block);
181                         b->set_frames (this_time);
182                         Audio (b);
183                         _audio_frames += b->frames ();
184                         to_do -= this_time;
185                 }
186         }
187 }
188
189 void
190 Matcher::repeat_last_video ()
191 {
192         if (!_last_image) {
193                 _last_image.reset (new SimpleImage (_pixel_format.get(), _size.get(), true));
194                 _last_image->make_black ();
195         }
196
197         Video (_last_image, true, _last_subtitle);
198         ++_video_frames;
199 }
200