+
+
+FFmpegSubtitlePeriod
+FFmpeg::subtitle_period (AVPacket const* packet, AVStream const* stream, AVSubtitle const & sub)
+{
+ auto const packet_time = ContentTime::from_seconds (packet->pts * av_q2d(stream->time_base));
+ auto const start = packet_time + ContentTime::from_seconds(sub.start_display_time / 1e3);
+
+ if (sub.end_display_time == 0 || sub.end_display_time == static_cast<uint32_t>(-1)) {
+ /* End time is not in the AVSubtitle; perhaps we can use the AVPacket's duration */
+ if (packet->duration) {
+ return FFmpegSubtitlePeriod(start, start + ContentTime::from_seconds(packet->duration * av_q2d(stream->time_base)));
+ } else {
+ return FFmpegSubtitlePeriod(start);
+ }
+ }
+
+ return FFmpegSubtitlePeriod (start, packet_time + ContentTime::from_seconds(sub.end_display_time / 1e3));
+}
+
+
+/** 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<shared_ptr<FFmpegAudioStream>> audio_streams, optional<ContentTime> 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 */
+
+ auto po = ContentTime::min ();
+
+ if (first_video) {
+ po = - first_video.get ();
+ }
+
+ for (auto 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) {
+ auto const fvc = first_video.get() + po;
+ po += fvc.ceil (video_frame_rate) - fvc;
+ }
+
+ return po;
+}
+
+
+AVFrame *
+FFmpeg::audio_frame (shared_ptr<const FFmpegAudioStream> stream)
+{
+ auto iter = _audio_frame.find(stream);
+ if (iter != _audio_frame.end()) {
+ return iter->second;
+ }
+
+ auto frame = av_frame_alloc ();
+ if (frame == nullptr) {
+ throw std::bad_alloc();
+ }
+
+ _audio_frame[stream] = frame;
+ return frame;
+}
+