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