Missing files.
[dcpomatic.git] / src / lib / matcher.cc
1 #include "matcher.h"
2 #include "image.h"
3 #include "log.h"
4
5 using boost::shared_ptr;
6
7 Matcher::Matcher (Log* log, int sample_rate, float frames_per_second)
8         : AudioVideoProcessor (log)
9         , _sample_rate (sample_rate)
10         , _frames_per_second (frames_per_second)
11         , _video_frames (0)
12         , _audio_frames (0)
13 {
14
15 }
16
17 void
18 Matcher::process_video (boost::shared_ptr<Image> i, boost::shared_ptr<Subtitle> s)
19 {
20         Video (i, s);
21         _video_frames++;
22
23         _pixel_format = i->pixel_format ();
24         _size = i->size ();
25 }
26
27 void
28 Matcher::process_audio (boost::shared_ptr<AudioBuffers> b)
29 {
30         Audio (b);
31         _audio_frames += b->frames ();
32
33         _channels = b->channels ();
34 }
35
36 void
37 Matcher::process_end ()
38 {
39         if (_audio_frames == 0 || !_pixel_format || !_size || !_channels) {
40                 /* We won't do anything */
41                 return;
42         }
43         
44         int64_t audio_short_by_frames = video_frames_to_audio_frames (_video_frames, _sample_rate, _frames_per_second) - _audio_frames;
45
46         _log->log (
47                 String::compose (
48                         "Matching processor has seen %1 video frames (which equals %2 audio frames) and %3 audio frames",
49                         _video_frames,
50                         video_frames_to_audio_frames (_video_frames, _sample_rate, _frames_per_second),
51                         _audio_frames
52                         )
53                 );
54         
55         if (audio_short_by_frames < 0) {
56                 
57                 _log->log (String::compose ("%1 too many audio frames", -audio_short_by_frames));
58                 
59                 /* We have seen more audio than video.  Emit enough black video frames so that we reverse this */
60                 int const black_video_frames = ceil (-audio_short_by_frames * _frames_per_second / _sample_rate);
61                 
62                 _log->log (String::compose ("Emitting %1 frames of black video", black_video_frames));
63
64                 shared_ptr<Image> black (new CompactImage (_pixel_format.get(), _size.get()));
65                 black->make_black ();
66                 for (int i = 0; i < black_video_frames; ++i) {
67                         Video (black, shared_ptr<Subtitle>());
68                 }
69                 
70                 /* Now recompute our check value */
71                 audio_short_by_frames = video_frames_to_audio_frames (_video_frames, _sample_rate, _frames_per_second) - _audio_frames;
72         }
73         
74         if (audio_short_by_frames > 0) {
75                 _log->log (String::compose ("Emitted %1 too few audio frames", audio_short_by_frames));
76                 shared_ptr<AudioBuffers> b (new AudioBuffers (_channels.get(), audio_short_by_frames));
77                 b->make_silent ();
78                 Audio (b);
79                 _audio_frames += b->frames ();
80         }
81 }