2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3 Copyright (C) 2000-2007 Paul Davis
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 /** @file src/lib/util.cc
22 * @brief Some utility functions and classes.
29 #ifdef DVDOMATIC_POSIX
33 #include <libssh/libssh.h>
35 #include <boost/algorithm/string.hpp>
37 #include <openssl/md5.h>
38 #include <magick/MagickCore.h>
39 #include <magick/version.h>
40 #include <libdcp/version.h>
42 #include <libavcodec/avcodec.h>
43 #include <libavformat/avformat.h>
44 #include <libswscale/swscale.h>
45 #include <libavfilter/avfiltergraph.h>
46 #include <libpostproc/postprocess.h>
47 #include <libavutil/pixfmt.h>
50 #include "exceptions.h"
53 #include "dcp_content_type.h"
56 #include "film_state.h"
57 #include "sound_processor.h"
58 #ifndef DVDOMATIC_DISABLE_PLAYER
59 #include "player_manager.h"
67 using namespace boost;
69 /** Convert some number of seconds to a string representation
70 * in hours, minutes and seconds.
73 * @return String of the form H:M:S (where H is hours, M
74 * is minutes and S is seconds).
77 seconds_to_hms (int s)
87 hms << setfill ('0') << m << ":";
89 hms << setfill ('0') << s;
94 /** @param s Number of seconds.
95 * @return String containing an approximate description of s (e.g. "about 2 hours")
98 seconds_to_approximate_hms (int s)
109 ap << (h + 1) << " hours";
121 ap << m << " minutes";
124 ap << s << " seconds";
130 #ifdef DVDOMATIC_POSIX
131 /** @param l Mangled C++ identifier.
132 * @return Demangled version.
137 string::size_type const b = l.find_first_of ("(");
138 if (b == string::npos) {
142 string::size_type const p = l.find_last_of ("+");
143 if (p == string::npos) {
151 string const fn = l.substr (b + 1, p - b - 1);
156 char* realname = abi::__cxa_demangle (fn.c_str(), 0, 0, &status);
161 } catch (std::exception) {
168 /** Write a stacktrace to an ostream.
169 * @param out Stream to write to.
170 * @param levels Number of levels to go up the call stack.
173 stacktrace (ostream& out, int levels)
180 size = backtrace (array, 200);
181 strings = backtrace_symbols (array, size);
184 for (i = 0; i < size && (levels == 0 || i < size_t(levels)); i++) {
185 out << " " << demangle (strings[i]) << endl;
193 /** @param s Sample format.
194 * @return String representation.
197 audio_sample_format_to_string (AVSampleFormat s)
199 /* Our sample format handling is not exactly complete */
202 case AV_SAMPLE_FMT_S16:
211 /** @param s String representation of a sample format, as returned from audio_sample_format_to_string().
212 * @return Sample format.
215 audio_sample_format_from_string (string s)
218 return AV_SAMPLE_FMT_S16;
221 return AV_SAMPLE_FMT_NONE;
224 /** @return Version of vobcopy that is on the path (and hence that we will use) */
228 FILE* f = popen ("vobcopy -V 2>&1", "r");
230 throw EncodeError ("could not run vobcopy to check version");
233 string version = "unknown";
237 if (fgets (buf, sizeof (buf), f)) {
240 split (b, s, is_any_of (" "));
241 if (b.size() >= 2 && b[0] == "Vobcopy") {
252 /** @param v Version as used by FFmpeg.
253 * @return A string representation of v.
256 ffmpeg_version_to_string (int v)
259 s << ((v & 0xff0000) >> 16) << "." << ((v & 0xff00) >> 8) << "." << (v & 0xff);
263 /** Return a user-readable string summarising the versions of our dependencies */
265 dependency_version_summary ()
268 s << "libopenjpeg " << opj_version () << ", "
269 << "vobcopy " << vobcopy_version() << ", "
270 << "libavcodec " << ffmpeg_version_to_string (avcodec_version()) << ", "
271 << "libavfilter " << ffmpeg_version_to_string (avfilter_version()) << ", "
272 << "libavformat " << ffmpeg_version_to_string (avformat_version()) << ", "
273 << "libavutil " << ffmpeg_version_to_string (avutil_version()) << ", "
274 << "libpostproc " << ffmpeg_version_to_string (postproc_version()) << ", "
275 << "libswscale " << ffmpeg_version_to_string (swscale_version()) << ", "
276 << MagickVersion << ", "
277 << "libssh " << ssh_version (0) << ", "
278 << "libdcp " << libdcp::version << " git " << libdcp::git_commit;
284 seconds (struct timeval t)
286 return t.tv_sec + (double (t.tv_usec) / 1e6);
289 /** @param socket Socket to read from */
290 SocketReader::SocketReader (shared_ptr<asio::ip::tcp::socket> socket)
297 /** Mark some data as being `consumed', so that it will not be returned
299 * @param size Amount of data to consume, in bytes.
302 SocketReader::consume (int size)
304 assert (_buffer_data >= size);
306 _buffer_data -= size;
307 if (_buffer_data > 0) {
308 /* Shift still-valid data to the start of the buffer */
309 memmove (_buffer, _buffer + size, _buffer_data);
313 /** Read a definite amount of data from our socket, and mark
315 * @param data Where to put the data.
316 * @param size Number of bytes to read.
319 SocketReader::read_definite_and_consume (uint8_t* data, int size)
321 int const from_buffer = min (_buffer_data, size);
322 if (from_buffer > 0) {
323 /* Get data from our buffer */
324 memcpy (data, _buffer, from_buffer);
325 consume (from_buffer);
326 /* Update our output state */
331 /* read() the rest */
333 int const n = asio::read (*_socket, asio::buffer (data, size));
335 throw NetworkError ("could not read");
343 /** Read as much data as is available, up to some limit.
344 * @param data Where to put the data.
345 * @param size Maximum amount of data to read.
348 SocketReader::read_indefinite (uint8_t* data, int size)
350 assert (size < int (sizeof (_buffer)));
352 /* Amount of extra data we need to read () */
353 int to_read = size - _buffer_data;
354 while (to_read > 0) {
355 /* read as much of it as we can (into our buffer) */
356 int const n = asio::read (*_socket, asio::buffer (_buffer + _buffer_data, to_read));
358 throw NetworkError ("could not read");
365 assert (_buffer_data >= size);
367 /* copy data into the output buffer */
368 assert (size >= _buffer_data);
369 memcpy (data, _buffer, size);
372 #ifdef DVDOMATIC_POSIX
374 sigchld_handler (int, siginfo_t* info, void *)
376 #ifndef DVDOMATIC_DISABLE_PLAYER
377 PlayerManager::instance()->child_exited (info->si_pid);
382 /** Call the required functions to set up DVD-o-matic's static arrays, etc. */
386 Format::setup_formats ();
387 DCPContentType::setup_dcp_content_types ();
388 Scaler::setup_scalers ();
389 Filter::setup_filters ();
390 SoundProcessor::setup_sound_processors ();
392 #ifdef DVDOMATIC_POSIX
394 sa.sa_flags = SA_SIGINFO;
395 sigemptyset (&sa.sa_mask);
396 sa.sa_sigaction = sigchld_handler;
397 sigaction (SIGCHLD, &sa, 0);
402 crop_string (Position start, Size size)
405 s << "crop=" << size.width << ":" << size.height << ":" << start.x << ":" << start.y;
410 split_at_spaces_considering_quotes (string s)
413 bool in_quotes = false;
415 for (string::size_type i = 0; i < s.length(); ++i) {
416 if (s[i] == ' ' && !in_quotes) {
419 } else if (s[i] == '"') {
420 in_quotes = !in_quotes;
432 md5_data (string title, void const * data, int size)
434 MHASH ht = mhash_init (MHASH_MD5);
435 if (ht == MHASH_FAILED) {
436 throw EncodeError ("could not create hash thread");
439 mhash (ht, data, size);
442 mhash_deinit (ht, hash);
444 printf ("%s [%d]: ", title.c_str (), size);
445 for (int i = 0; i < int (mhash_get_block_size (MHASH_MD5)); ++i) {
446 printf ("%.2x", hash[i]);
453 md5_digest (string file)
455 ifstream f (file.c_str(), ios::binary);
457 throw OpenFileError (file);
460 f.seekg (0, ios::end);
461 int bytes = f.tellg ();
462 f.seekg (0, ios::beg);
464 int const buffer_size = 64 * 1024;
465 char buffer[buffer_size];
468 MD5_Init (&md5_context);
470 int const t = min (bytes, buffer_size);
472 MD5_Update (&md5_context, buffer, t);
476 unsigned char digest[MD5_DIGEST_LENGTH];
477 MD5_Final (digest, &md5_context);
480 for (int i = 0; i < MD5_DIGEST_LENGTH; ++i) {
481 s << hex << setfill('0') << setw(2) << ((int) digest[i]);
488 dcp_audio_sample_rate (int fs)
497 bool operator== (Crop const & a, Crop const & b)
499 return (a.left == b.left && a.right == b.right && a.top == b.top && a.bottom == b.bottom);
502 bool operator!= (Crop const & a, Crop const & b)
508 colour_lut_index_to_name (int index)