Bump version
[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 "video_decoder.h"
25 #include "audio_decoder.h"
26 #include "encoder.h"
27 #include "job.h"
28 #include "options.h"
29 #include "image.h"
30 #include "decoder_factory.h"
31 #include "matcher.h"
32 #include "delay_line.h"
33 #include "gain.h"
34 #include "combiner.h"
35 #include "trimmer.h"
36
37 /** @file src/ab_transcoder.cc
38  *  @brief A transcoder which uses one Film for the left half of the screen, and a different one
39  *  for the right half (to facilitate A/B comparisons of settings)
40  */
41
42 using std::string;
43 using boost::shared_ptr;
44 using boost::dynamic_pointer_cast;
45
46 /** @param a Film to use for the left half of the screen.
47  *  @param b Film to use for the right half of the screen.
48  *  @param o Decoder options.
49  *  @param j Job that we are associated with.
50  *  @param e Encoder to use.
51  */
52
53 ABTranscoder::ABTranscoder (
54         shared_ptr<Film> a, shared_ptr<Film> b, DecodeOptions o, Job* j, shared_ptr<Encoder> e)
55         : _film_a (a)
56         , _film_b (b)
57         , _job (j)
58         , _encoder (e)
59         , _combiner (new Combiner (a->log()))
60 {
61         _da = decoder_factory (_film_a, o);
62         _db = decoder_factory (_film_b, o);
63
64         shared_ptr<AudioStream> st = _film_a->audio_stream();
65         if (st) {
66                 _matcher.reset (new Matcher (_film_a->log(), st->sample_rate(), _film_a->source_frame_rate()));
67         }
68         _delay_line.reset (new DelayLine (_film_a->log(), _film_a->audio_delay() / 1000.0f));
69         _gain.reset (new Gain (_film_a->log(), _film_a->audio_gain()));
70
71         int const sr = st ? st->sample_rate() : 0;
72         int const trim_start = _film_a->trim_type() == Film::ENCODE ? _film_a->trim_start() : 0;
73         int const trim_end = _film_a->trim_type() == Film::ENCODE ? _film_a->trim_end() : 0;
74         _trimmer.reset (new Trimmer (
75                                 _film_a->log(), trim_start, trim_end, _film_a->length().get(),
76                                 sr, _film_a->source_frame_rate(), _film_a->dcp_frame_rate()
77                                 ));
78         
79         /* Set up the decoder to use the film's set streams */
80         _da.video->set_subtitle_stream (_film_a->subtitle_stream ());
81         _db.video->set_subtitle_stream (_film_a->subtitle_stream ());
82         if (_film_a->audio_stream ()) {
83                 _da.audio->set_audio_stream (_film_a->audio_stream ());
84         }
85
86         _da.video->Video.connect (bind (&Combiner::process_video, _combiner, _1, _2, _3, _4));
87         _db.video->Video.connect (bind (&Combiner::process_video_b, _combiner, _1, _2, _3, _4));
88
89         _combiner->connect_video (_delay_line);
90         if (_matcher) {
91                 _delay_line->connect_video (_matcher);
92                 _matcher->connect_video (_trimmer);
93         } else {
94                 _delay_line->connect_video (_trimmer);
95         }
96         _trimmer->connect_video (_encoder);
97         
98         _da.audio->connect_audio (_delay_line);
99         if (_matcher) {
100                 _delay_line->connect_audio (_matcher);
101                 _matcher->connect_audio (_gain);
102         } else {
103                 _delay_line->connect_audio (_gain);
104         }
105         _gain->connect_audio (_trimmer);
106         _trimmer->connect_audio (_encoder);
107 }
108
109 void
110 ABTranscoder::go ()
111 {
112         _encoder->process_begin ();
113
114         bool done[3] = { false, false, false };
115         
116         while (1) {
117                 done[0] = _da.video->pass ();
118                 done[1] = _db.video->pass ();
119                 
120                 if (!done[2] && _da.audio && dynamic_pointer_cast<Decoder> (_da.audio) != dynamic_pointer_cast<Decoder> (_da.video)) {
121                         done[2] = _da.audio->pass ();
122                 } else {
123                         done[2] = true;
124                 }
125                 
126                 if (_job) {
127                         _da.video->set_progress (_job);
128                 }
129                 
130                 if (done[0] && done[1] && done[2]) {
131                         break;
132                 }
133         }
134                 
135         _delay_line->process_end ();
136         if (_matcher) {
137                 _matcher->process_end ();
138         }
139         _gain->process_end ();
140         _encoder->process_end ();
141 }
142