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.
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <boost/algorithm/string.hpp>
35 #include <magick/MagickCore.h>
36 #include <magick/version.h>
37 #include <libssh/libssh.h>
39 #include <libavcodec/avcodec.h>
40 #include <libavformat/avformat.h>
41 #include <libswscale/swscale.h>
42 #include <libswresample/swresample.h>
43 #include <libavfilter/avfiltergraph.h>
44 #include <libavfilter/avcodec.h>
45 #include <libavfilter/buffersink.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 "player_manager.h"
64 using namespace boost;
66 /** Convert some number of seconds to a string representation
67 * in hours, minutes and seconds.
70 * @return String of the form H:M:S (where H is hours, M
71 * is minutes and S is seconds).
74 seconds_to_hms (int s)
84 hms << setfill ('0') << m << ":";
86 hms << setfill ('0') << s;
91 /** @param s Number of seconds.
92 * @return String containing an approximate description of s (e.g. "about 2 hours")
95 seconds_to_approximate_hms (int s)
106 ap << (h + 1) << " hours";
118 ap << m << " minutes";
121 ap << s << " seconds";
127 /** @param l Mangled C++ identifier.
128 * @return Demangled version.
133 string::size_type const b = l.find_first_of ("(");
134 if (b == string::npos) {
138 string::size_type const p = l.find_last_of ("+");
139 if (p == string::npos) {
147 string const fn = l.substr (b + 1, p - b - 1);
152 char* realname = abi::__cxa_demangle (fn.c_str(), 0, 0, &status);
157 } catch (std::exception) {
164 /** Write a stacktrace to an ostream.
165 * @param out Stream to write to.
166 * @param levels Number of levels to go up the call stack.
169 stacktrace (ostream& out, int levels)
176 size = backtrace (array, 200);
177 strings = backtrace_symbols (array, size);
180 for (i = 0; i < size && (levels == 0 || i < size_t(levels)); i++) {
181 out << " " << demangle (strings[i]) << endl;
188 /** @param s Sample format.
189 * @return String representation.
192 audio_sample_format_to_string (AVSampleFormat s)
194 /* Our sample format handling is not exactly complete */
197 case AV_SAMPLE_FMT_S16:
206 /** @param s String representation of a sample format, as returned from audio_sample_format_to_string().
207 * @return Sample format.
210 audio_sample_format_from_string (string s)
213 return AV_SAMPLE_FMT_S16;
216 return AV_SAMPLE_FMT_NONE;
219 /** @return Version of vobcopy that is on the path (and hence that we will use) */
223 FILE* f = popen ("vobcopy -V 2>&1", "r");
225 throw EncodeError ("could not run vobcopy to check version");
228 string version = "unknown";
233 ssize_t const r = getline (&buf, &n, f);
237 split (b, s, is_any_of (" "));
238 if (b.size() >= 2 && b[0] == "Vobcopy") {
250 /** @param v Version as used by FFmpeg.
251 * @return A string representation of v.
254 ffmpeg_version_to_string (int v)
257 s << ((v & 0xff0000) >> 16) << "." << ((v & 0xff00) >> 8) << "." << (v & 0xff);
261 /** Return a user-readable string summarising the versions of our dependencies */
263 dependency_version_summary ()
266 s << "libopenjpeg " << opj_version () << ", "
267 << "vobcopy " << vobcopy_version() << ", "
268 << "libswresample " << ffmpeg_version_to_string (swresample_version()) << ", "
269 << "libavcodec " << ffmpeg_version_to_string (avcodec_version()) << ", "
270 << "libavfilter " << ffmpeg_version_to_string (avfilter_version()) << ", "
271 << "libavformat " << ffmpeg_version_to_string (avformat_version()) << ", "
272 << "libavutil " << ffmpeg_version_to_string (avutil_version()) << ", "
273 << "libpostproc " << ffmpeg_version_to_string (postproc_version()) << ", "
274 << "libswscale " << ffmpeg_version_to_string (swscale_version()) << ", "
275 << MagickVersion << ", "
276 << "libssh " << ssh_version (0);
281 /** Write some data to a socket.
282 * @param fd Socket file descriptor.
284 * @param size Amount to write, in bytes.
287 socket_write (int fd, uint8_t const * data, int size)
289 uint8_t const * p = data;
291 int const n = send (fd, p, size, MSG_NOSIGNAL);
294 s << "could not write (" << strerror (errno) << ")";
295 throw NetworkError (s.str ());
304 seconds (struct timeval t)
306 return t.tv_sec + (double (t.tv_usec) / 1e6);
309 /** @param fd File descriptor to read from */
310 SocketReader::SocketReader (int fd)
317 /** Mark some data as being `consumed', so that it will not be returned
319 * @param size Amount of data to consume, in bytes.
322 SocketReader::consume (int size)
324 assert (_buffer_data >= size);
326 _buffer_data -= size;
327 if (_buffer_data > 0) {
328 /* Shift still-valid data to the start of the buffer */
329 memmove (_buffer, _buffer + size, _buffer_data);
333 /** Read a definite amount of data from our socket, and mark
335 * @param data Where to put the data.
336 * @param size Number of bytes to read.
339 SocketReader::read_definite_and_consume (uint8_t* data, int size)
341 int const from_buffer = min (_buffer_data, size);
342 if (from_buffer > 0) {
343 /* Get data from our buffer */
344 memcpy (data, _buffer, from_buffer);
345 consume (from_buffer);
346 /* Update our output state */
351 /* read() the rest */
353 int const n = ::read (_fd, data, size);
355 throw NetworkError ("could not read");
363 /** Read as much data as is available, up to some limit.
364 * @param data Where to put the data.
365 * @param size Maximum amount of data to read.
368 SocketReader::read_indefinite (uint8_t* data, int size)
370 assert (size < int (sizeof (_buffer)));
372 /* Amount of extra data we need to read () */
373 int to_read = size - _buffer_data;
374 while (to_read > 0) {
375 /* read as much of it as we can (into our buffer) */
376 int const n = ::read (_fd, _buffer + _buffer_data, to_read);
378 throw NetworkError ("could not read");
385 assert (_buffer_data >= size);
387 /* copy data into the output buffer */
388 assert (size >= _buffer_data);
389 memcpy (data, _buffer, size);
393 sigchld_handler (int, siginfo_t* info, void *)
395 PlayerManager::instance()->child_exited (info->si_pid);
398 /** Call the required functions to set up DVD-o-matic's static arrays, etc. */
402 Format::setup_formats ();
403 DCPContentType::setup_dcp_content_types ();
404 Scaler::setup_scalers ();
405 Filter::setup_filters ();
408 sa.sa_flags = SA_SIGINFO;
409 sigemptyset (&sa.sa_mask);
410 sa.sa_sigaction = sigchld_handler;
411 sigaction (SIGCHLD, &sa, 0);
415 crop_string (Position start, Size size)
418 s << "crop=" << size.width << ":" << size.height << ":" << start.x << ":" << start.y;
423 split_at_spaces_considering_quotes (string s)
426 bool in_quotes = false;
428 for (string::size_type i = 0; i < s.length(); ++i) {
429 if (s[i] == ' ' && !in_quotes) {
432 } else if (s[i] == '"') {
433 in_quotes = !in_quotes;
445 md5_data (string title, void const * data, int size)
447 MHASH ht = mhash_init (MHASH_MD5);
448 if (ht == MHASH_FAILED) {
449 throw EncodeError ("could not create hash thread");
452 mhash (ht, data, size);
455 mhash_deinit (ht, hash);
457 printf ("%s [%d]: ", title.c_str (), size);
458 for (int i = 0; i < int (mhash_get_block_size (MHASH_MD5)); ++i) {
459 printf ("%.2x", hash[i]);