AudioDecoder::AudioDecoder (shared_ptr<const Film> f)
: Decoder (f)
- , _next_audio_frame (0)
+ , _audio_position (0)
{
}
AudioDecoder::audio (shared_ptr<const AudioBuffers> data, AudioContent::Frame frame)
{
Audio (data, frame);
- _next_audio_frame = frame + data->frames ();
+ _audio_position = frame + data->frames ();
}
protected:
void audio (boost::shared_ptr<const AudioBuffers>, AudioContent::Frame);
- AudioContent::Frame _next_audio_frame;
+ AudioContent::Frame _audio_position;
};
#endif
frame_rate = node->number_child<int> ("FrameRate");
channels = node->number_child<int64_t> ("Channels");
mapping = AudioMapping (node->node_child ("Mapping"));
- start = node->optional_number_child<Time> ("Start");
+ first_audio = node->optional_number_child<Time> ("FirstAudio");
}
void
root->add_child("Id")->add_child_text (lexical_cast<string> (id));
root->add_child("FrameRate")->add_child_text (lexical_cast<string> (frame_rate));
root->add_child("Channels")->add_child_text (lexical_cast<string> (channels));
- if (start) {
- root->add_child("Start")->add_child_text (lexical_cast<string> (start));
+ if (first_audio) {
+ root->add_child("FirstAudio")->add_child_text (lexical_cast<string> (first_audio));
}
mapping.as_xml (root->add_child("Mapping"));
}
int frame_rate;
int channels;
AudioMapping mapping;
- boost::optional<Time> start;
+ boost::optional<double> first_audio;
};
extern bool operator== (FFmpegAudioStream const & a, FFmpegAudioStream const & b);
void set_subtitle_stream (boost::shared_ptr<FFmpegSubtitleStream>);
void set_audio_stream (boost::shared_ptr<FFmpegAudioStream>);
+
+ boost::optional<Time> first_video () const {
+ boost::mutex::scoped_lock lm (_mutex);
+ return _first_video;
+ }
private:
std::vector<boost::shared_ptr<FFmpegSubtitleStream> > _subtitle_streams;
, _subtitle_codec (0)
, _decode_video (video)
, _decode_audio (audio)
+ , _pts_offset (0)
{
setup_subtitle ();
+
+ if (video && audio && c->audio_stream() && c->first_video() && c->audio_stream()->first_audio) {
+ _pts_offset = compute_pts_offset (c->first_video().get(), c->audio_stream()->first_audio.get(), c->video_frame_rate());
+ }
+}
+
+double
+FFmpegDecoder::compute_pts_offset (double first_video, double first_audio, float video_frame_rate)
+{
+ assert (first_video >= 0);
+ assert (first_audio >= 0);
+
+ double const old_first_video = first_video;
+
+ /* Round the first video to a frame boundary */
+ if (fabs (rint (first_video * video_frame_rate) - first_video * video_frame_rate) > 1e-6) {
+ first_video = ceil (first_video * video_frame_rate) / video_frame_rate;
+ }
+
+ /* Compute the required offset (also removing any common start delay) */
+ return first_video - old_first_video - min (first_video, first_audio);
}
FFmpegDecoder::~FFmpegDecoder ()
}
/* Stop us being asked for any more data */
- _next_video_frame = _ffmpeg_content->video_length ();
- _next_audio_frame = _ffmpeg_content->audio_length ();
+ _video_position = _ffmpeg_content->video_length ();
+ _audio_position = _ffmpeg_content->audio_length ();
return;
}
void
FFmpegDecoder::seek_back ()
{
- if (_next_video_frame == 0) {
+ if (_video_position == 0) {
return;
}
- do_seek (_next_video_frame - 1, true, true);
+ do_seek (_video_position - 1, true, true);
}
void
int const decode_result = avcodec_decode_audio4 (audio_codec_context(), _frame, &frame_finished, ©_packet);
if (decode_result >= 0) {
if (frame_finished) {
-
- /* Where we are in the source, in seconds */
- double const source_pts_seconds = av_q2d (_format_context->streams[copy_packet.stream_index]->time_base)
- * av_frame_get_best_effort_timestamp(_frame);
+
+ if (_audio_position == 0) {
+ /* Where we are in the source, in seconds */
+ double const pts = av_q2d (_format_context->streams[copy_packet.stream_index]->time_base)
+ * av_frame_get_best_effort_timestamp(_frame) - _pts_offset;
+
+ if (pts > 0) {
+ /* Emit some silence */
+ shared_ptr<AudioBuffers> silence (
+ new AudioBuffers (
+ _ffmpeg_content->audio_channels(),
+ pts * _ffmpeg_content->content_audio_frame_rate()
+ )
+ );
+
+ silence->make_silent ();
+ audio (silence, _audio_position);
+ }
+ }
+
int const data_size = av_samples_get_buffer_size (
0, audio_codec_context()->channels, _frame->nb_samples, audio_sample_format (), 1
);
assert (audio_codec_context()->channels == _ffmpeg_content->audio_channels());
- Audio (deinterleave_audio (_frame->data, data_size), source_pts_seconds * _ffmpeg_content->content_audio_frame_rate());
+ audio (deinterleave_audio (_frame->data, data_size), _audio_position);
}
copy_packet.data += decode_result;
int64_t const bet = av_frame_get_best_effort_timestamp (_frame);
if (bet != AV_NOPTS_VALUE) {
- double const pts = bet * av_q2d (_format_context->streams[_video_stream]->time_base);
- double const next = _next_video_frame / _ffmpeg_content->video_frame_rate();
+ double const pts = bet * av_q2d (_format_context->streams[_video_stream]->time_base) - _pts_offset;
+ double const next = _video_position / _ffmpeg_content->video_frame_rate();
double const one_frame = 1 / _ffmpeg_content->video_frame_rate ();
double delta = pts - next;
);
black->make_black ();
- video (image, false, _next_video_frame);
+ video (image, false, _video_position);
delta -= one_frame;
}
if (delta > -one_frame) {
/* This PTS is within a frame of being right; emit this (otherwise it will be dropped) */
- video (image, false, _next_video_frame);
+ video (image, false, _video_position);
}
} else {
shared_ptr<const Film> film = _film.lock ();
bool
FFmpegDecoder::done () const
{
- bool const vd = !_decode_video || (_next_video_frame >= _ffmpeg_content->video_length());
- bool const ad = !_decode_audio || !_ffmpeg_content->audio_stream() || (_next_audio_frame >= _ffmpeg_content->audio_length());
+ bool const vd = !_decode_video || (_video_position >= _ffmpeg_content->video_length());
+ bool const ad = !_decode_audio || !_ffmpeg_content->audio_stream() || (_audio_position >= _ffmpeg_content->audio_length());
return vd && ad;
}
#include "ffmpeg.h"
class Film;
+class ffmpeg_pts_offset_test;
/** @class FFmpegDecoder
* @brief A decoder using FFmpeg to decode content.
bool done () const;
private:
+ friend class ::ffmpeg_pts_offset_test;
/* No copy construction */
FFmpegDecoder (FFmpegDecoder const &);
FFmpegDecoder& operator= (FFmpegDecoder const &);
+ static double compute_pts_offset (double, double, float);
+
void setup_subtitle ();
AVSampleFormat audio_sample_format () const;
bool _decode_video;
bool _decode_audio;
+
+ double _pts_offset;
};
}
} else {
for (size_t i = 0; i < _audio_streams.size(); ++i) {
- if (_packet.stream_index == _audio_streams[i]->id && !_audio_streams[i]->start) {
+ if (_packet.stream_index == _audio_streams[i]->id && !_audio_streams[i]->first_audio) {
if (avcodec_decode_audio4 (context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
- _audio_streams[i]->start = frame_time (_audio_streams[i]->id);
+ _audio_streams[i]->first_audio = frame_time (_audio_streams[i]->id);
}
}
}
bool have_all_audio = true;
size_t i = 0;
while (i < _audio_streams.size() && have_all_audio) {
- have_all_audio = _audio_streams[i]->start;
+ have_all_audio = _audio_streams[i]->first_audio;
++i;
}
}
}
-optional<Time>
+optional<double>
FFmpegExaminer::frame_time (int stream) const
{
- optional<Time> t;
+ optional<double> t;
int64_t const bet = av_frame_get_best_effort_timestamp (_frame);
if (bet != AV_NOPTS_VALUE) {
- t = bet * av_q2d (_format_context->streams[stream]->time_base) * TIME_HZ;
+ t = bet * av_q2d (_format_context->streams[stream]->time_base);
}
return t;
return _audio_streams;
}
- boost::optional<Time> first_video () const {
+ boost::optional<double> first_video () const {
return _first_video;
}
private:
std::string stream_name (AVStream* s) const;
- boost::optional<Time> frame_time (int) const;
+ boost::optional<double> frame_time (int) const;
std::vector<boost::shared_ptr<FFmpegSubtitleStream> > _subtitle_streams;
std::vector<boost::shared_ptr<FFmpegAudioStream> > _audio_streams;
- boost::optional<Time> _first_video;
+ boost::optional<double> _first_video;
};
void
ImageMagickDecoder::pass ()
{
- if (_next_video_frame >= _imagemagick_content->video_length ()) {
+ if (_video_position >= _imagemagick_content->video_length ()) {
return;
}
if (_image) {
- video (_image, true, _next_video_frame);
+ video (_image, true, _video_position);
return;
}
delete magick_image;
- video (_image, false, _next_video_frame);
+ video (_image, false, _video_position);
}
void
ImageMagickDecoder::seek (VideoContent::Frame frame)
{
- _next_video_frame = frame;
+ _video_position = frame;
}
void
ImageMagickDecoder::seek_back ()
{
- if (_next_video_frame > 0) {
- _next_video_frame--;
+ if (_video_position > 0) {
+ _video_position--;
}
}
bool
ImageMagickDecoder::done () const
{
- return _next_video_frame >= _imagemagick_content->video_length ();
+ return _video_position >= _imagemagick_content->video_length ();
}
bool
SndfileDecoder::done () const
{
- return _next_audio_frame > _sndfile_content->audio_length ();
+ return _audio_position >= _sndfile_content->audio_length ();
}
VideoDecoder::VideoDecoder (shared_ptr<const Film> f)
: Decoder (f)
- , _next_video_frame (0)
+ , _video_position (0)
{
}
VideoDecoder::video (shared_ptr<const Image> image, bool same, VideoContent::Frame frame)
{
Video (image, same, frame);
- _next_video_frame = frame + 1;
+ _video_position = frame + 1;
}
#if 0
protected:
void video (boost::shared_ptr<const Image>, bool, VideoContent::Frame);
- VideoContent::Frame _next_video_frame;
+ VideoContent::Frame _video_position;
};
#endif
shared_ptr<FFmpegContent> content (new FFmpegContent (film, "test/data/count300bd24.m2ts"));
shared_ptr<FFmpegExaminer> examiner (new FFmpegExaminer (content));
- BOOST_CHECK_EQUAL (examiner->first_video().get(), 57604000);
+ BOOST_CHECK_EQUAL (examiner->first_video().get(), 600.04166666666674);
BOOST_CHECK_EQUAL (examiner->audio_streams().size(), 1);
- BOOST_CHECK_EQUAL (examiner->audio_streams()[0]->start.get(), 57600000);
+ BOOST_CHECK_EQUAL (examiner->audio_streams()[0]->first_audio.get(), 600);
}
--- /dev/null
+/*
+ Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+*/
+
+BOOST_AUTO_TEST_CASE (ffmpeg_pts_offset_test)
+{
+ /* Sound == video so no offset required */
+ BOOST_CHECK_EQUAL (FFmpegDecoder::compute_pts_offset (0, 0, 24), 0);
+
+ /* Common offset should be removed */
+ BOOST_CHECK_CLOSE (FFmpegDecoder::compute_pts_offset (42, 42, 24), -42, 1e-9);
+
+ /* Video is on a frame boundary */
+ BOOST_CHECK_EQUAL (FFmpegDecoder::compute_pts_offset (1.0 / 24.0, 0, 24), 0);
+
+ /* Again, video is on a frame boundary */
+ BOOST_CHECK_EQUAL (FFmpegDecoder::compute_pts_offset (1.0 / 23.97, 0, 23.97), 0);
+
+ /* And again, video is on a frame boundary */
+ BOOST_CHECK_EQUAL (FFmpegDecoder::compute_pts_offset (3.0 / 23.97, 0, 23.97), 0);
+
+ /* Off a frame boundary */
+ BOOST_CHECK_CLOSE (FFmpegDecoder::compute_pts_offset (1.0 / 24.0 - 0.0215, 0, 24), 0.0215, 1e-9);
+}
BOOST_CHECK (ref_dcp.equals (check_dcp, options, boost::bind (note, _1, _2)));
}
+#include "ffmpeg_pts_offset.cc"
#include "ffmpeg_examiner_test.cc"
#include "black_fill_test.cc"
#include "scaling_test.cc"