5 using boost::shared_ptr;
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)
18 Matcher::process_video (boost::shared_ptr<Image> i, boost::shared_ptr<Subtitle> s)
23 _pixel_format = i->pixel_format ();
28 Matcher::process_audio (boost::shared_ptr<AudioBuffers> b)
31 _audio_frames += b->frames ();
33 _channels = b->channels ();
37 Matcher::process_end ()
39 if (_audio_frames == 0 || !_pixel_format || !_size || !_channels) {
40 /* We won't do anything */
44 int64_t audio_short_by_frames = video_frames_to_audio_frames (_video_frames, _sample_rate, _frames_per_second) - _audio_frames;
48 "Matching processor has seen %1 video frames (which equals %2 audio frames) and %3 audio frames",
50 video_frames_to_audio_frames (_video_frames, _sample_rate, _frames_per_second),
55 if (audio_short_by_frames < 0) {
57 _log->log (String::compose ("%1 too many audio frames", -audio_short_by_frames));
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);
62 _log->log (String::compose ("Emitting %1 frames of black video", black_video_frames));
64 shared_ptr<Image> black (new CompactImage (_pixel_format.get(), _size.get()));
66 for (int i = 0; i < black_video_frames; ++i) {
67 Video (black, shared_ptr<Subtitle>());
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;
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));
79 _audio_frames += b->frames ();