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