Move things round a bit.
[dcpomatic.git] / src / tools / servomatictest.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 <iomanip>
22 #include <exception>
23 #include <getopt.h>
24 #include "format.h"
25 #include "film.h"
26 #include "filter.h"
27 #include "util.h"
28 #include "scaler.h"
29 #include "server.h"
30 #include "dcp_video_frame.h"
31 #include "options.h"
32 #include "decoder.h"
33 #include "exceptions.h"
34 #include "scaler.h"
35 #include "log.h"
36 #include "decoder_factory.h"
37
38 using namespace std;
39 using namespace boost;
40
41 static Server* server;
42 static Log log_ ("servomatictest.log");
43
44 void
45 process_video (shared_ptr<Image> image, int frame)
46 {
47         shared_ptr<DCPVideoFrame> local (new DCPVideoFrame (image, Size (1024, 1024), 0, Scaler::from_id ("bicubic"), frame, 24, "", 0, 250000000, &log_));
48         shared_ptr<DCPVideoFrame> remote (new DCPVideoFrame (image, Size (1024, 1024), 0, Scaler::from_id ("bicubic"), frame, 24, "", 0, 250000000, &log_));
49
50 #if defined(DEBUG_HASH)
51         cout << "Frame " << frame << ":\n";
52 #else
53         cout << "Frame " << frame << ": ";
54         cout.flush ();
55 #endif  
56
57         shared_ptr<EncodedData> local_encoded = local->encode_locally ();
58         shared_ptr<EncodedData> remote_encoded;
59
60         string remote_error;
61         try {
62                 remote_encoded = remote->encode_remotely (server);
63         } catch (NetworkError& e) {
64                 remote_error = e.what ();
65         }
66
67 #if defined(DEBUG_HASH)
68         cout << "Frame " << frame << ": ";
69         cout.flush ();
70 #endif  
71
72         if (!remote_error.empty ()) {
73                 cout << "\033[0;31mnetwork problem: " << remote_error << "\033[0m\n";
74                 return;
75         }
76
77         if (local_encoded->size() != remote_encoded->size()) {
78                 cout << "\033[0;31msizes differ\033[0m\n";
79                 return;
80         }
81                 
82         uint8_t* p = local_encoded->data();
83         uint8_t* q = remote_encoded->data();
84         for (int i = 0; i < local_encoded->size(); ++i) {
85                 if (*p++ != *q++) {
86                         cout << "\033[0;31mdata differ\033[0m at byte " << i << "\n";
87                         return;
88                 }
89         }
90
91         cout << "\033[0;32mgood\033[0m\n";
92 }
93
94 static void
95 help (string n)
96 {
97         cerr << "Syntax: " << n << " [--help] --film <film> --server <host>\n";
98         exit (EXIT_FAILURE);
99 }
100
101 int
102 main (int argc, char* argv[])
103 {
104         string film_dir;
105         string server_host;
106
107         while (1) {
108                 static struct option long_options[] = {
109                         { "help", no_argument, 0, 'h'},
110                         { "server", required_argument, 0, 's'},
111                         { "film", required_argument, 0, 'f'},
112                         { 0, 0, 0, 0 }
113                 };
114
115                 int option_index = 0;
116                 int c = getopt_long (argc, argv, "hs:f:", long_options, &option_index);
117
118                 if (c == -1) {
119                         break;
120                 }
121
122                 switch (c) {
123                 case 'h':
124                         help (argv[0]);
125                         exit (EXIT_SUCCESS);
126                 case 's':
127                         server_host = optarg;
128                         break;
129                 case 'f':
130                         film_dir = optarg;
131                         break;
132                 }
133         }
134         
135         if (server_host.empty() || film_dir.empty()) {
136                 help (argv[0]);
137                 exit (EXIT_FAILURE);
138         }
139
140         dvdomatic_setup ();
141
142         server = new Server (server_host, 1);
143         Film film (film_dir, true);
144
145         shared_ptr<Options> opt (new Options ("fred", "jim", "sheila"));
146         opt->out_size = Size (1024, 1024);
147         opt->apply_crop = false;
148         opt->decode_audio = false;
149
150         shared_ptr<Decoder> decoder = decoder_factory (film.state_copy(), opt, 0, &log_);
151         try {
152                 decoder->Video.connect (sigc::ptr_fun (process_video));
153                 decoder->go ();
154         } catch (std::exception& e) {
155                 cerr << "Error: " << e.what() << "\n";
156         }
157
158         return 0;
159 }