summaryrefslogtreecommitdiff
path: root/src/lib/util.cc
blob: 1478bab2e52dca65bab8631e59f3cd3cae6a1c9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/*
    Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
    Copyright (C) 2000-2007 Paul Davis

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

/** @file src/lib/util.cc
 *  @brief Some utility functions and classes.
 */

#include <sstream>
#include <iomanip>
#include <iostream>
#include <fstream>
#ifdef DVDOMATIC_POSIX
#include <execinfo.h>
#include <cxxabi.h>
#endif
#include <libssh/libssh.h>
#include <signal.h>
#include <boost/algorithm/string.hpp>
#include <openjpeg.h>
#include <openssl/md5.h>
#include <magick/MagickCore.h>
#include <magick/version.h>
#include <libdcp/version.h>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavfilter/avfiltergraph.h>
#include <libpostproc/postprocess.h>
#include <libavutil/pixfmt.h>
}
#include "util.h"
#include "exceptions.h"
#include "scaler.h"
#include "format.h"
#include "dcp_content_type.h"
#include "filter.h"
#include "screen.h"
#include "film_state.h"
#include "sound_processor.h"
#ifndef DVDOMATIC_DISABLE_PLAYER
#include "player_manager.h"
#endif

#ifdef DEBUG_HASH
#include <mhash.h>
#endif

using namespace std;
using namespace boost;

/** Convert some number of seconds to a string representation
 *  in hours, minutes and seconds.
 *
 *  @param s Seconds.
 *  @return String of the form H:M:S (where H is hours, M
 *  is minutes and S is seconds).
 */
string
seconds_to_hms (int s)
{
	int m = s / 60;
	s -= (m * 60);
	int h = m / 60;
	m -= (h * 60);

	stringstream hms;
	hms << h << ":";
	hms.width (2);
	hms << setfill ('0') << m << ":";
	hms.width (2);
	hms << setfill ('0') << s;

	return hms.str ();
}

/** @param s Number of seconds.
 *  @return String containing an approximate description of s (e.g. "about 2 hours")
 */
string
seconds_to_approximate_hms (int s)
{
	int m = s / 60;
	s -= (m * 60);
	int h = m / 60;
	m -= (h * 60);

	stringstream ap;
	
	if (h > 0) {
		if (m > 30) {
			ap << (h + 1) << " hours";
		} else {
			if (h == 1) {
				ap << "1 hour";
			} else {
				ap << h << " hours";
			}
		}
	} else if (m > 0) {
		if (m == 1) {
			ap << "1 minute";
		} else {
			ap << m << " minutes";
		}
	} else {
		ap << s << " seconds";
	}

	return ap.str ();
}

#ifdef DVDOMATIC_POSIX
/** @param l Mangled C++ identifier.
 *  @return Demangled version.
 */
static string
demangle (string l)
{
	string::size_type const b = l.find_first_of ("(");
	if (b == string::npos) {
		return l;
	}

	string::size_type const p = l.find_last_of ("+");
	if (p == string::npos) {
		return l;
	}

	if ((p - b) <= 1) {
		return l;
	}
	
	string const fn = l.substr (b + 1, p - b - 1);

	int status;
	try {
		
		char* realname = abi::__cxa_demangle (fn.c_str(), 0, 0, &status);
		string d (realname);
		free (realname);
		return d;
		
	} catch (std::exception) {
		
	}
	
	return l;
}

/** Write a stacktrace to an ostream.
 *  @param out Stream to write to.
 *  @param levels Number of levels to go up the call stack.
 */
void
stacktrace (ostream& out, int levels)
{
	void *array[200];
	size_t size;
	char **strings;
	size_t i;
     
	size = backtrace (array, 200);
	strings = backtrace_symbols (array, size);
     
	if (strings) {
		for (i = 0; i < size && (levels == 0 || i < size_t(levels)); i++) {
			out << "  " << demangle (strings[i]) << endl;
		}
		
		free (strings);
	}
}
#endif

/** @param s Sample format.
 *  @return String representation.
 */
string
audio_sample_format_to_string (AVSampleFormat s)
{
	/* Our sample format handling is not exactly complete */
	
	switch (s) {
	case AV_SAMPLE_FMT_S16:
		return "S16";
	default:
		break;
	}

	return "Unknown";
}

/** @param s String representation of a sample format, as returned from audio_sample_format_to_string().
 *  @return Sample format.
 */
AVSampleFormat
audio_sample_format_from_string (string s)
{
	if (s == "S16") {
		return AV_SAMPLE_FMT_S16;
	}

	return AV_SAMPLE_FMT_NONE;
}

/** @return Version of vobcopy that is on the path (and hence that we will use) */
static string
vobcopy_version ()
{
	FILE* f = popen ("vobcopy -V 2>&1", "r");
	if (f == 0) {
		throw EncodeError ("could not run vobcopy to check version");
	}

	string version = "unknown";
	
	while (!feof (f)) {
		char buf[256];
		if (fgets (buf, sizeof (buf), f)) {
			string s (buf);
			vector<string> b;
			split (b, s, is_any_of (" "));
			if (b.size() >= 2 && b[0] == "Vobcopy") {
				version = b[1];
			}
		}
	}

	pclose (f);

	return version;
}

/** @param v Version as used by FFmpeg.
 *  @return A string representation of v.
 */
static string
ffmpeg_version_to_string (int v)
{
	stringstream s;
	s << ((v & 0xff0000) >> 16) << "." << ((v & 0xff00) >> 8) << "." << (v & 0xff);
	return s.str ();
}

/** Return a user-readable string summarising the versions of our dependencies */
string
dependency_version_summary ()
{
	stringstream s;
	s << "libopenjpeg " << opj_version () << ", "
	  << "vobcopy " << vobcopy_version() << ", "
	  << "libavcodec " << ffmpeg_version_to_string (avcodec_version()) << ", "
	  << "libavfilter " << ffmpeg_version_to_string (avfilter_version()) << ", "
	  << "libavformat " << ffmpeg_version_to_string (avformat_version()) << ", "
	  << "libavutil " << ffmpeg_version_to_string (avutil_version()) << ", "
	  << "libpostproc " << ffmpeg_version_to_string (postproc_version()) << ", "
	  << "libswscale " << ffmpeg_version_to_string (swscale_version()) << ", "
	  << MagickVersion << ", "
	  << "libssh " << ssh_version (0) << ", "
	  << "libdcp " << libdcp::version << " git " << libdcp::git_commit;

	return s.str ();
}

double
seconds (struct timeval t)
{
	return t.tv_sec + (double (t.tv_usec) / 1e6);
}

/** @param socket Socket to read from */
SocketReader::SocketReader (shared_ptr<asio::ip::tcp::socket> socket)
	: _socket (socket)
	, _buffer_data (0)
{

}

/** Mark some data as being `consumed', so that it will not be returned
 *  as data again.
 *  @param size Amount of data to consume, in bytes.
 */
void
SocketReader::consume (int size)
{
	assert (_buffer_data >= size);
	
	_buffer_data -= size;
	if (_buffer_data > 0) {
		/* Shift still-valid data to the start of the buffer */
		memmove (_buffer, _buffer + size, _buffer_data);
	}
}

/** Read a definite amount of data from our socket, and mark
 *  it as consumed.
 *  @param data Where to put the data.
 *  @param size Number of bytes to read.
 */
void
SocketReader::read_definite_and_consume (uint8_t* data, int size)
{
	int const from_buffer = min (_buffer_data, size);
	if (from_buffer > 0) {
		/* Get data from our buffer */
		memcpy (data, _buffer, from_buffer);
		consume (from_buffer);
		/* Update our output state */
		data += from_buffer;
		size -= from_buffer;
	}

	/* read() the rest */
	while (size > 0) {
		int const n = asio::read (*_socket, asio::buffer (data, size));
		if (n <= 0) {
			throw NetworkError ("could not read");
		}

		data += n;
		size -= n;
	}
}

/** Read as much data as is available, up to some limit.
 *  @param data Where to put the data.
 *  @param size Maximum amount of data to read.
 */
void
SocketReader::read_indefinite (uint8_t* data, int size)
{
	assert (size < int (sizeof (_buffer)));

	/* Amount of extra data we need to read () */
	int to_read = size - _buffer_data;
	while (to_read > 0) {
		/* read as much of it as we can (into our buffer) */
		int const n = asio::read (*_socket, asio::buffer (_buffer + _buffer_data, to_read));
		if (n <= 0) {
			throw NetworkError ("could not read");
		}

		to_read -= n;
		_buffer_data += n;
	}

	assert (_buffer_data >= size);

	/* copy data into the output buffer */
	assert (size >= _buffer_data);
	memcpy (data, _buffer, size);
}

#ifdef DVDOMATIC_POSIX
void
sigchld_handler (int, siginfo_t* info, void *)
{
#ifndef DVDOMATIC_DISABLE_PLAYER	
	PlayerManager::instance()->child_exited (info->si_pid);
#endif	
}
#endif

/** Call the required functions to set up DVD-o-matic's static arrays, etc. */
void
dvdomatic_setup ()
{
	Format::setup_formats ();
	DCPContentType::setup_dcp_content_types ();
	Scaler::setup_scalers ();
	Filter::setup_filters ();
	SoundProcessor::setup_sound_processors ();

#ifdef DVDOMATIC_POSIX	
	struct sigaction sa;
	sa.sa_flags = SA_SIGINFO;
	sigemptyset (&sa.sa_mask);
	sa.sa_sigaction = sigchld_handler;
	sigaction (SIGCHLD, &sa, 0);
#endif	
}

string
crop_string (Position start, Size size)
{
	stringstream s;
	s << "crop=" << size.width << ":" << size.height << ":" << start.x << ":" << start.y;
	return s.str ();
}

vector<string>
split_at_spaces_considering_quotes (string s)
{
	vector<string> out;
	bool in_quotes = false;
	string c;
	for (string::size_type i = 0; i < s.length(); ++i) {
		if (s[i] == ' ' && !in_quotes) {
			out.push_back (c);
			c = "";
		} else if (s[i] == '"') {
			in_quotes = !in_quotes;
		} else {
			c += s[i];
		}
	}

	out.push_back (c);
	return out;
}

#ifdef DEBUG_HASH
void
md5_data (string title, void const * data, int size)
{
	MHASH ht = mhash_init (MHASH_MD5);
	if (ht == MHASH_FAILED) {
		throw EncodeError ("could not create hash thread");
	}

	mhash (ht, data, size);
	
	uint8_t hash[16];
	mhash_deinit (ht, hash);
	
	printf ("%s [%d]: ", title.c_str (), size);
	for (int i = 0; i < int (mhash_get_block_size (MHASH_MD5)); ++i) {
		printf ("%.2x", hash[i]);
	}
	printf ("\n");
}
#endif

string
md5_digest (string file)
{
	ifstream f (file.c_str(), ios::binary);
	if (!f.good ()) {
		throw OpenFileError (file);
	}
	
	f.seekg (0, ios::end);
	int bytes = f.tellg ();
	f.seekg (0, ios::beg);

	int const buffer_size = 64 * 1024;
	char buffer[buffer_size];

	MD5_CTX md5_context;
	MD5_Init (&md5_context);
	while (bytes > 0) {
		int const t = min (bytes, buffer_size);
		f.read (buffer, t);
		MD5_Update (&md5_context, buffer, t);
		bytes -= t;
	}

	unsigned char digest[MD5_DIGEST_LENGTH];
	MD5_Final (digest, &md5_context);

	stringstream s;
	for (int i = 0; i < MD5_DIGEST_LENGTH; ++i) {
		s << hex << setfill('0') << setw(2) << ((int) digest[i]);
	}

	return s.str ();
}

int
dcp_audio_sample_rate (int fs)
{
	if (fs <= 48000) {
		return 48000;
	}

	return 96000;
}

bool operator== (Crop const & a, Crop const & b)
{
	return (a.left == b.left && a.right == b.right && a.top == b.top && a.bottom == b.bottom);
}

bool operator!= (Crop const & a, Crop const & b)
{
	return !(a == b);
}

string
colour_lut_index_to_name (int index)
{
	switch (index) {
	case 0:
		return "sRGB";
	case 1:
		return "Rec 709";
	}

	assert (false);
	return "";
}