0c687008d91f18546fd2871a788b7b33905f9663
[dcpomatic.git] / src / lib / ab_transcoder.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 <iostream>
21 #include <boost/shared_ptr.hpp>
22 #include "ab_transcoder.h"
23 #include "film.h"
24 #include "encoder.h"
25 #include "job.h"
26 #include "options.h"
27 #include "image.h"
28 #include "playlist.h"
29 #include "matcher.h"
30 #include "delay_line.h"
31 #include "gain.h"
32 #include "combiner.h"
33
34 /** @file src/ab_transcoder.cc
35  *  @brief A transcoder which uses one Film for the left half of the screen, and a different one
36  *  for the right half (to facilitate A/B comparisons of settings)
37  */
38
39 using std::string;
40 using boost::shared_ptr;
41 using boost::dynamic_pointer_cast;
42
43 /** @param a Film to use for the left half of the screen.
44  *  @param b Film to use for the right half of the screen.
45  *  @param o Decoder options.
46  *  @param j Job that we are associated with.
47  *  @param e Encoder to use.
48  */
49
50 ABTranscoder::ABTranscoder (shared_ptr<Film> a, shared_ptr<Film> b, DecodeOptions o, shared_ptr<Job> j)
51         : _film_a (a)
52         , _film_b (b)
53         , _playlist_a (_film_a->playlist ())
54         , _playlist_b (_film_b->playlist ())
55         , _job (j)
56         , _encoder (new Encoder (_film_a, _playlist_a))
57         , _combiner (new Combiner (a->log()))
58 {
59         if (_playlist_a->has_audio ()) {
60                 _matcher.reset (new Matcher (_film_a->log(), _playlist_a->audio_frame_rate(), _playlist_a->video_frame_rate()));
61                 _delay_line.reset (new DelayLine (_film_a->log(), _playlist_a->audio_channels(), _film_a->audio_delay() * _playlist_a->audio_frame_rate() / 1000));
62                 _gain.reset (new Gain (_film_a->log(), _film_a->audio_gain()));
63         }
64
65         _playlist_a->Video.connect (bind (&Combiner::process_video, _combiner, _1, _2, _3));
66         _playlist_b->Video.connect (bind (&Combiner::process_video_b, _combiner, _1, _2, _3));
67
68         if (_matcher) {
69                 _combiner->connect_video (_matcher);
70                 _matcher->connect_video (_encoder);
71         } else {
72                 _combiner->connect_video (_encoder);
73         }
74         
75         if (_matcher && _delay_line) {
76                 _playlist_a->connect_audio (_delay_line);
77                 _delay_line->connect_audio (_matcher);
78                 _matcher->connect_audio (_gain);
79                 _gain->connect_audio (_encoder);
80         }
81 }
82
83 void
84 ABTranscoder::go ()
85 {
86         _encoder->process_begin ();
87
88         bool done[2] = { false, false };
89         
90         while (1) {
91                 done[0] = _playlist_a->pass ();
92                 done[1] = _playlist_b->pass ();
93
94                 if (_job) {
95                         _playlist_a->set_progress (_job);
96                 }
97
98                 if (done[0] && done[1]) {
99                         break;
100                 }
101         }
102
103         if (_delay_line) {
104                 _delay_line->process_end ();
105         }
106         if (_matcher) {
107                 _matcher->process_end ();
108         }
109         if (_gain) {
110                 _gain->process_end ();
111         }
112         _encoder->process_end ();
113 }
114