2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
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.
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.
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.
25 using boost::shared_ptr;
27 Matcher::Matcher (Log* log, int sample_rate, float frames_per_second)
28 : AudioVideoProcessor (log)
29 , _sample_rate (sample_rate)
30 , _frames_per_second (frames_per_second)
38 Matcher::process_video (boost::shared_ptr<Image> i, boost::shared_ptr<Subtitle> s)
43 _pixel_format = i->pixel_format ();
48 Matcher::process_audio (boost::shared_ptr<AudioBuffers> b)
51 _audio_frames += b->frames ();
53 _channels = b->channels ();
57 Matcher::process_end ()
59 if (_audio_frames == 0 || !_pixel_format || !_size || !_channels) {
60 /* We won't do anything */
64 int64_t audio_short_by_frames = video_frames_to_audio_frames (_video_frames, _sample_rate, _frames_per_second) - _audio_frames;
68 "Matching processor has seen %1 video frames (which equals %2 audio frames) and %3 audio frames",
70 video_frames_to_audio_frames (_video_frames, _sample_rate, _frames_per_second),
75 if (audio_short_by_frames < 0) {
77 _log->log (String::compose ("%1 too many audio frames", -audio_short_by_frames));
79 /* We have seen more audio than video. Emit enough black video frames so that we reverse this */
80 int const black_video_frames = ceil (-audio_short_by_frames * _frames_per_second / _sample_rate);
82 _log->log (String::compose ("Emitting %1 frames of black video", black_video_frames));
84 shared_ptr<Image> black (new SimpleImage (_pixel_format.get(), _size.get(), false));
86 for (int i = 0; i < black_video_frames; ++i) {
87 Video (black, shared_ptr<Subtitle>());
90 /* Now recompute our check value */
91 audio_short_by_frames = video_frames_to_audio_frames (_video_frames, _sample_rate, _frames_per_second) - _audio_frames;
94 if (audio_short_by_frames > 0) {
95 _log->log (String::compose ("Emitted %1 too few audio frames", audio_short_by_frames));
97 /* Do things in half second blocks as I think there may be limits
98 to what FFmpeg (and in particular the resampler) can cope with.
100 int64_t const block = _sample_rate / 2;
101 shared_ptr<AudioBuffers> b (new AudioBuffers (_channels.get(), block));
104 int64_t to_do = audio_short_by_frames;
106 int64_t const this_time = min (to_do, block);
107 b->set_frames (this_time);
109 _audio_frames += b->frames ();