summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2012-07-23 00:42:37 +0100
committerCarl Hetherington <cth@carlh.net>2012-07-23 00:42:37 +0100
commita7bc9d57f6a5c7c13400e700db50f21c283766f4 (patch)
tree726ef6f292e6fd1dca6bb1b2e3036040c02d2b00 /src
parent2fb6cdae8446699be824aaeb902f4bca1888657d (diff)
Use MD5 digest of content file as part of the J2C directory, rather than the filename itself.
Diffstat (limited to 'src')
-rw-r--r--src/lib/film.cc3
-rw-r--r--src/lib/film_state.cc8
-rw-r--r--src/lib/film_state.h2
-rw-r--r--src/lib/util.cc36
-rw-r--r--src/lib/util.h1
5 files changed, 49 insertions, 1 deletions
diff --git a/src/lib/film.cc b/src/lib/film.cc
index 502119db3..f11d30aba 100644
--- a/src/lib/film.cc
+++ b/src/lib/film.cc
@@ -193,6 +193,7 @@ Film::set_content (string c)
_state.audio_sample_rate = d->audio_sample_rate ();
_state.audio_sample_format = d->audio_sample_format ();
+ _state.content_digest = md5_digest (c);
_state.content = c;
signal_changed (SIZE);
@@ -424,7 +425,7 @@ Film::j2k_dir () const
settings.
*/
s << _state.format->nickname()
- << "_" << _state.content
+ << "_" << _state.content_digest
<< "_" << left_crop() << "_" << right_crop() << "_" << top_crop() << "_" << bottom_crop()
<< "_" << f.first << "_" << f.second
<< "_" << _state.scaler->id();
diff --git a/src/lib/film_state.cc b/src/lib/film_state.cc
index 16378086c..369e4c986 100644
--- a/src/lib/film_state.cc
+++ b/src/lib/film_state.cc
@@ -92,6 +92,7 @@ FilmState::write_metadata (ofstream& f) const
f << "audio_channels " << audio_channels << "\n";
f << "audio_sample_rate " << audio_sample_rate << "\n";
f << "audio_sample_format " << audio_sample_format_to_string (audio_sample_format) << "\n";
+ f << "content_digest " << content_digest << "\n";
}
/** Read state from a key / value pair.
@@ -161,6 +162,13 @@ FilmState::read_metadata (string k, string v)
audio_sample_rate = atoi (v.c_str ());
} else if (k == "audio_sample_format") {
audio_sample_format = audio_sample_format_from_string (v);
+ } else if (k == "content_digest") {
+ content_digest = v;
+ }
+
+ /* Itsy bitsy hack: compute digest here if don't have one (for backwards compatibility) */
+ if (content_digest.empty() && !content.empty()) {
+ content_digest = md5_digest (file (content));
}
}
diff --git a/src/lib/film_state.h b/src/lib/film_state.h
index 52525ecd4..62ca76c41 100644
--- a/src/lib/film_state.h
+++ b/src/lib/film_state.h
@@ -147,6 +147,8 @@ public:
int audio_sample_rate;
/** Format of the audio samples */
AVSampleFormat audio_sample_format;
+ /** MD5 digest of our content file */
+ std::string content_digest;
private:
std::string thumb_file_for_frame (int) const;
diff --git a/src/lib/util.cc b/src/lib/util.cc
index 7388539cd..799d0e54e 100644
--- a/src/lib/util.cc
+++ b/src/lib/util.cc
@@ -25,6 +25,7 @@
#include <sstream>
#include <iomanip>
#include <iostream>
+#include <fstream>
#ifdef DVDOMATIC_POSIX
#include <execinfo.h>
#include <cxxabi.h>
@@ -35,6 +36,7 @@
#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>
@@ -469,3 +471,37 @@ md5_data (string title, void const * data, int size)
}
#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 ();
+}
diff --git a/src/lib/util.h b/src/lib/util.h
index 3901e7ec6..1f635677a 100644
--- a/src/lib/util.h
+++ b/src/lib/util.h
@@ -45,6 +45,7 @@ extern void socket_write (int, uint8_t const *, int);
extern double seconds (struct timeval);
extern void dvdomatic_setup ();
extern std::vector<std::string> split_at_spaces_considering_quotes (std::string);
+extern std::string md5_digest (std::string);
enum ContentType {
STILL,