X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fffmpeg.cc;h=9d6921dcfbf33e2fab4f3ade9887ca66ad4bf6e7;hb=0914bfdbfc498d99821c2cd0ca3a4f9a184d07b5;hp=63fe9c9d8a3cc1aa5a86364593f84835db08ffee;hpb=937240f9e360e434b36c418656ae6fe8cc6afea9;p=dcpomatic.git diff --git a/src/lib/ffmpeg.cc b/src/lib/ffmpeg.cc index 63fe9c9d8..9d6921dcf 100644 --- a/src/lib/ffmpeg.cc +++ b/src/lib/ffmpeg.cc @@ -20,25 +20,31 @@ #include "ffmpeg.h" #include "ffmpeg_content.h" #include "film.h" -#include "ffmpeg_audio_stream.h" -#include "ffmpeg_subtitle_stream.h" #include "exceptions.h" #include "util.h" #include "raw_convert.h" #include "log.h" +#include "ffmpeg_subtitle_stream.h" +#include "ffmpeg_audio_stream.h" +#include "md5_digester.h" +#include "compose.hpp" extern "C" { #include #include #include } #include +#include +#include #include "i18n.h" using std::string; using std::cout; using std::cerr; +using std::vector; using boost::shared_ptr; +using boost::optional; boost::mutex FFmpeg::_mutex; boost::weak_ptr FFmpeg::_ffmpeg_log; @@ -94,7 +100,7 @@ FFmpeg::ffmpeg_log_callback (void* ptr, int level, const char* fmt, va_list vl) if (log) { string str (line); boost::algorithm::trim (str); - log->log (String::compose ("FFmpeg: %1", str), Log::TYPE_GENERAL); + log->log (String::compose ("FFmpeg: %1", str), LogEntry::TYPE_GENERAL); } else { cerr << line; } @@ -138,9 +144,6 @@ FFmpeg::setup_general () for (uint32_t i = 0; i < _format_context->nb_streams; ++i) { AVStream* s = _format_context->streams[i]; - /* Files from iTunes sometimes have two video streams, one with the avg_frame_rate.num and .den set - to zero. Ignore these streams. - */ if (s->codec->codec_type == AVMEDIA_TYPE_VIDEO) { if (s->avg_frame_rate.num > 0 && s->avg_frame_rate.den > 0) { /* This is definitely our video stream */ @@ -249,3 +252,118 @@ FFmpeg::avio_seek (int64_t const pos, int whence) return _file_group.seek (pos, whence); } + +FFmpegSubtitlePeriod +FFmpeg::subtitle_period (AVSubtitle const & sub) +{ + ContentTime const packet_time = ContentTime::from_seconds (static_cast (sub.pts) / AV_TIME_BASE); + + if (sub.end_display_time == static_cast (-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) + ); +} + +string +FFmpeg::subtitle_id (AVSubtitle const & sub) +{ + MD5Digester digester; + digester.add (sub.pts); + for (unsigned int i = 0; i < sub.num_rects; ++i) { + AVSubtitleRect* rect = sub.rects[i]; + digester.add (rect->x); + digester.add (rect->y); + digester.add (rect->w); + digester.add (rect->h); + int const line = rect->pict.linesize[0]; + for (int j = 0; j < rect->h; ++j) { + digester.add (rect->pict.data[0] + j * line, line); + } + } + return digester.get (); +} + +/** @return true if sub starts a new image subtitle */ +bool +FFmpeg::subtitle_starts_image (AVSubtitle const & sub) +{ + bool image = false; + bool text = false; + + for (unsigned int i = 0; i < sub.num_rects; ++i) { + switch (sub.rects[i]->type) { + case SUBTITLE_BITMAP: + image = true; + break; + case SUBTITLE_TEXT: + case SUBTITLE_ASS: + text = true; + break; + default: + break; + } + } + + /* We can't cope with mixed image/text in one AVSubtitle */ + DCPOMATIC_ASSERT (!image || !text); + + return image; +} + +/** Compute the pts offset to use given a set of audio streams and some video details. + * Sometimes these parameters will have just been determined by an Examiner, sometimes + * they will have been retrieved from a piece of Content, hence the need for this method + * in FFmpeg. + */ +ContentTime +FFmpeg::pts_offset (vector > audio_streams, optional first_video, double video_frame_rate) const +{ + /* Audio and video frame PTS values may not start with 0. We want + to fiddle them so that: + + 1. One of them starts at time 0. + 2. The first video PTS value ends up on a frame boundary. + + Then we remove big initial gaps in PTS and we allow our + insertion of black frames to work. + + We will do: + audio_pts_to_use = audio_pts_from_ffmpeg + pts_offset; + video_pts_to_use = video_pts_from_ffmpeg + pts_offset; + */ + + /* First, make one of them start at 0 */ + + ContentTime po = ContentTime::min (); + + if (first_video) { + po = - first_video.get (); + } + + BOOST_FOREACH (shared_ptr i, audio_streams) { + if (i->first_audio) { + po = max (po, - i->first_audio.get ()); + } + } + + /* If the offset is positive we would be pushing things from a -ve PTS to be played. + I don't think we ever want to do that, as it seems things at -ve PTS are not meant + to be seen (use for alignment bars etc.); see mantis #418. + */ + if (po > ContentTime ()) { + po = ContentTime (); + } + + /* Now adjust so that the video pts starts on a frame */ + if (first_video) { + ContentTime const fvc = first_video.get() + po; + po += fvc.round_up (video_frame_rate) - fvc; + } + + return po; +}