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