Merge branch 'video-player' of /home/carl/git/dvdomatic into video-player
[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
36 /** @file src/ab_transcoder.cc
37  *  @brief A transcoder which uses one Film for the left half of the screen, and a different one
38  *  for the right half (to facilitate A/B comparisons of settings)
39  */
40
41 using std::string;
42 using boost::shared_ptr;
43
44 /** @param a Film to use for the left half of the screen.
45  *  @param b Film to use for the right half of the screen.
46  *  @param o Decoder options.
47  *  @param j Job that we are associated with.
48  *  @param e Encoder to use.
49  */
50
51 ABTranscoder::ABTranscoder (
52         shared_ptr<Film> a, shared_ptr<Film> b, shared_ptr<const DecodeOptions> o, Job* j, shared_ptr<Encoder> e)
53         : _film_a (a)
54         , _film_b (b)
55         , _job (j)
56         , _encoder (e)
57 {
58         _da = decoder_factory (_film_a, o, j);
59         _db = decoder_factory (_film_b, o, j);
60
61         if (_film_a->audio_stream()) {
62                 shared_ptr<AudioStream> st = _film_a->audio_stream();
63                 _matcher.reset (new Matcher (_film_a->log(), st->sample_rate(), _film_a->frames_per_second()));
64                 _delay_line.reset (new DelayLine (_film_a->log(), st->channels(), _film_a->audio_delay() * st->sample_rate() / 1000));
65                 _gain.reset (new Gain (_film_a->log(), _film_a->audio_gain()));
66         }
67
68         /* Set up the decoder to use the film's set streams */
69         _da.video->set_subtitle_stream (_film_a->subtitle_stream ());
70         _db.video->set_subtitle_stream (_film_a->subtitle_stream ());
71         _da.audio->set_audio_stream (_film_a->audio_stream ());
72
73         _da.video->Video.connect (bind (&Combiner::process_video, _combiner, _1, _2, _3));
74         _db.video->Video.connect (bind (&Combiner::process_video_b, _combiner, _1, _2, _3));
75
76         if (_matcher) {
77                 _combiner->connect_video (_matcher);
78                 _matcher->connect_video (_encoder);
79         } else {
80                 _combiner->connect_video (_encoder);
81         }
82         
83         if (_matcher && _delay_line) {
84                 _da.audio->connect_audio (_delay_line);
85                 _delay_line->connect_audio (_matcher);
86                 _matcher->connect_audio (_gain);
87                 _gain->connect_audio (_encoder);
88         }
89 }
90
91 void
92 ABTranscoder::go ()
93 {
94         _encoder->process_begin ();
95         
96         while (1) {
97                 bool const va = _da.video->pass ();
98                 bool const vb = _db.video->pass ();
99                 bool const a = _da.audio->pass ();
100
101                 _da.video->set_progress ();
102
103                 if (va && vb && a) {
104                         break;
105                 }
106         }
107
108         if (_delay_line) {
109                 _delay_line->process_end ();
110         }
111         if (_matcher) {
112                 _matcher->process_end ();
113         }
114         if (_gain) {
115                 _gain->process_end ();
116         }
117         _encoder->process_end ();
118 }
119