Move trimming into the encoder; seems to be cleaner.
[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 "decoder.h"
25 #include "encoder.h"
26 #include "job.h"
27 #include "options.h"
28 #include "image.h"
29 #include "decoder_factory.h"
30
31 /** @file src/ab_transcoder.cc
32  *  @brief A transcoder which uses one Film for the left half of the screen, and a different one
33  *  for the right half (to facilitate A/B comparisons of settings)
34  */
35
36 using std::string;
37 using boost::shared_ptr;
38
39 /** @param a Film to use for the left half of the screen.
40  *  @param b Film to use for the right half of the screen.
41  *  @param o Options.
42  *  @param j Job that we are associated with.
43  *  @param e Encoder to use.
44  */
45
46 ABTranscoder::ABTranscoder (
47         shared_ptr<Film> a, shared_ptr<Film> b, shared_ptr<const Options> o, Job* j, shared_ptr<Encoder> e)
48         : _film_a (a)
49         , _film_b (b)
50         , _opt (o)
51         , _job (j)
52         , _encoder (e)
53         , _last_frame (0)
54 {
55         _da = decoder_factory (_film_a, o, j);
56         _db = decoder_factory (_film_b, o, j);
57
58         _da->Video.connect (bind (&ABTranscoder::process_video, this, _1, _2, _3, 0));
59         _db->Video.connect (bind (&ABTranscoder::process_video, this, _1, _2, _3, 1));
60         _da->Audio.connect (bind (&Encoder::process_audio, e, _1, _2));
61 }
62
63 ABTranscoder::~ABTranscoder ()
64 {
65
66 }
67
68 void
69 ABTranscoder::process_video (shared_ptr<Image> yuv, SourceFrame frame, shared_ptr<Subtitle> sub, int index)
70 {
71         if (index == 0) {
72                 /* Keep this image around until we get the other half */
73                 _image = yuv;
74         } else {
75                 /* Copy the right half of yuv into _image */
76                 for (int i = 0; i < yuv->components(); ++i) {
77                         int const line_size = yuv->line_size()[i];
78                         int const half_line_size = line_size / 2;
79                         int const stride = yuv->stride()[i];
80
81                         uint8_t* p = _image->data()[i];
82                         uint8_t* q = yuv->data()[i];
83                         
84                         for (int j = 0; j < yuv->lines (i); ++j) {
85                                 memcpy (p + half_line_size, q + half_line_size, half_line_size);
86                                 p += stride;
87                                 q += stride;
88                         }
89                 }
90                         
91                 /* And pass it to the encoder */
92                 _encoder->process_video (_image, frame, sub);
93                 _image.reset ();
94         }
95         
96         _last_frame = frame;
97 }
98
99
100 void
101 ABTranscoder::go ()
102 {
103         _encoder->process_begin (_da->audio_channel_layout());
104         _da->process_begin ();
105         _db->process_begin ();
106         
107         while (1) {
108                 bool const a = _da->pass ();
109                 bool const b = _db->pass ();
110
111                 if (a && b) {
112                         break;
113                 }
114         }
115
116         _encoder->process_end ();
117         _da->process_end ();
118         _db->process_end ();
119 }
120