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.
27 using boost::shared_ptr;
29 Matcher::Matcher (shared_ptr<Log> log, int sample_rate, float frames_per_second)
30 : AudioVideoProcessor (log)
31 , _sample_rate (sample_rate)
32 , _frames_per_second (frames_per_second)
40 Matcher::process_video (shared_ptr<Image> i, bool same, shared_ptr<Subtitle> s)
45 _pixel_format = i->pixel_format ();
50 Matcher::process_audio (shared_ptr<AudioBuffers> b)
53 _audio_frames += b->frames ();
55 _channels = b->channels ();
59 Matcher::process_end ()
61 if (_audio_frames == 0 || !_pixel_format || !_size || !_channels) {
62 /* We won't do anything */
66 int64_t audio_short_by_frames = video_frames_to_audio_frames (_video_frames, _sample_rate, _frames_per_second) - _audio_frames;
70 N_("Matching processor has seen %1 video frames (which equals %2 audio frames) and %3 audio frames"),
72 video_frames_to_audio_frames (_video_frames, _sample_rate, _frames_per_second),
77 if (audio_short_by_frames < 0) {
79 _log->log (String::compose (N_("%1 too many audio frames"), -audio_short_by_frames));
81 /* We have seen more audio than video. Emit enough black video frames so that we reverse this */
82 int const black_video_frames = ceil (-audio_short_by_frames * _frames_per_second / _sample_rate);
84 _log->log (String::compose (N_("Emitting %1 frames of black video"), black_video_frames));
86 shared_ptr<Image> black (new SimpleImage (_pixel_format.get(), _size.get(), true));
88 for (int i = 0; i < black_video_frames; ++i) {
89 Video (black, i != 0, shared_ptr<Subtitle>());
92 /* Now recompute our check value */
93 audio_short_by_frames = video_frames_to_audio_frames (_video_frames, _sample_rate, _frames_per_second) - _audio_frames;
96 if (audio_short_by_frames > 0) {
97 _log->log (String::compose (N_("Emitted %1 too few audio frames"), audio_short_by_frames));
99 /* Do things in half second blocks as I think there may be limits
100 to what FFmpeg (and in particular the resampler) can cope with.
102 int64_t const block = _sample_rate / 2;
103 shared_ptr<AudioBuffers> b (new AudioBuffers (_channels.get(), block));
106 int64_t to_do = audio_short_by_frames;
108 int64_t const this_time = min (to_do, block);
109 b->set_frames (this_time);
111 _audio_frames += b->frames ();