Assorted image subtitle fixes.
[dcpomatic.git] / src / lib / util.cc
index ab991e76b19e58b28bd06373a2ae61673f73e8fa..d8c754607f47a5940b51e95921ead82c11a2daca 100644 (file)
@@ -381,7 +381,7 @@ dcpomatic_setup_gettext_i18n (string lang)
 #endif 
 
 #ifdef DCPOMATIC_LINUX
-       bindtextdomain ("libdcpomatic", POSIX_LOCALE_PREFIX);
+       bindtextdomain ("libdcpomatic", LINUX_LOCALE_PREFIX);
 #endif
 }
 
@@ -423,7 +423,7 @@ md5_digest_head_tail (vector<boost::filesystem::path> files, boost::uintmax_t si
                }
 
                boost::uintmax_t this_time = min (to_do, boost::filesystem::file_size (files[i]));
-               fseek (f, -this_time, SEEK_END);
+               dcpomatic_fseek (f, -this_time, SEEK_END);
                fread (p, 1, this_time, f);
                p += this_time;
                to_do -= this_time;
@@ -509,6 +509,10 @@ audio_channel_name (int c)
 bool
 valid_image_file (boost::filesystem::path f)
 {
+       if (boost::starts_with (f.leaf().string(), "._")) {
+               return false;
+       }
+               
        string ext = f.extension().string();
        transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
        return (
@@ -542,13 +546,13 @@ tidy_for_filename (string f)
 }
 
 dcp::Size
-fit_ratio_within (float ratio, dcp::Size full_frame, int round)
+fit_ratio_within (float ratio, dcp::Size full_frame)
 {
        if (ratio < full_frame.ratio ()) {
-               return dcp::Size (round_to (full_frame.height * ratio, round), full_frame.height);
+               return dcp::Size (rint (full_frame.height * ratio), full_frame.height);
        }
        
-       return dcp::Size (full_frame.width, round_to (full_frame.width / ratio, round));
+       return dcp::Size (full_frame.width, rint (full_frame.width / ratio));
 }
 
 void *
@@ -560,18 +564,21 @@ wrapped_av_malloc (size_t s)
        }
        return p;
 }
-               
-ContentTimePeriod
+
+FFmpegSubtitlePeriod
 subtitle_period (AVSubtitle const & sub)
 {
        ContentTime const packet_time = ContentTime::from_seconds (static_cast<double> (sub.pts) / AV_TIME_BASE);
 
-       ContentTimePeriod period (
+       if (sub.end_display_time == static_cast<uint32_t> (-1)) {
+               /* End time is not known */
+               return FFmpegSubtitlePeriod (packet_time + ContentTime::from_seconds (sub.start_display_time / 1e3));
+       }
+       
+       return FFmpegSubtitlePeriod (
                packet_time + ContentTime::from_seconds (sub.start_display_time / 1e3),
                packet_time + ContentTime::from_seconds (sub.end_display_time / 1e3)
                );
-
-       return period;
 }
 
 map<string, string>
@@ -619,3 +626,47 @@ split_get_request (string url)
 
        return r;
 }
+
+long
+frame_info_position (int frame, Eyes eyes)
+{
+       static int const info_size = 48;
+       
+       switch (eyes) {
+       case EYES_BOTH:
+               return frame * info_size;
+       case EYES_LEFT:
+               return frame * info_size * 2;
+       case EYES_RIGHT:
+               return frame * info_size * 2 + info_size;
+       default:
+               DCPOMATIC_ASSERT (false);
+       }
+
+       DCPOMATIC_ASSERT (false);
+}
+
+dcp::FrameInfo
+read_frame_info (FILE* file, int frame, Eyes eyes)
+{
+       dcp::FrameInfo info;
+       dcpomatic_fseek (file, frame_info_position (frame, eyes), SEEK_SET);
+       fread (&info.offset, sizeof (info.offset), 1, file);
+       fread (&info.size, sizeof (info.size), 1, file);
+       
+       char hash_buffer[33];
+       fread (hash_buffer, 1, 32, file);
+       hash_buffer[32] = '\0';
+       info.hash = hash_buffer;
+
+       return info;
+}
+
+void
+write_frame_info (FILE* file, int frame, Eyes eyes, dcp::FrameInfo info)
+{
+       dcpomatic_fseek (file, frame_info_position (frame, eyes), SEEK_SET);
+       fwrite (&info.offset, sizeof (info.offset), 1, file);
+       fwrite (&info.size, sizeof (info.size), 1, file);
+       fwrite (info.hash.c_str(), 1, info.hash.size(), file);
+}