summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2012-10-22 11:21:31 +0100
committerCarl Hetherington <cth@carlh.net>2012-10-22 11:21:31 +0100
commit4cb33e432c7070f59c3ee3fbeb0b5c8755bba3bd (patch)
treeb574347f4921b9b8edc9b1a64e6ae80c62b599cf /src
parent1cc6986bdc5c35fd512257336f36bdb1b4142183 (diff)
Partial fix to sync according to pts.
Diffstat (limited to 'src')
-rw-r--r--src/lib/decoder.h5
-rw-r--r--src/lib/examine_content_job.cc8
-rw-r--r--src/lib/ffmpeg_decoder.cc18
-rw-r--r--src/lib/ffmpeg_decoder.h4
4 files changed, 34 insertions, 1 deletions
diff --git a/src/lib/decoder.h b/src/lib/decoder.h
index 2285cf4f9..7559217eb 100644
--- a/src/lib/decoder.h
+++ b/src/lib/decoder.h
@@ -71,6 +71,11 @@ public:
virtual int64_t audio_channel_layout () const = 0;
virtual bool has_subtitles () const = 0;
+ /** @return amount of extra unwanted audio at the start (or -ve for unwanted video) in milliseconds */
+ virtual int audio_to_discard () const {
+ return 0;
+ }
+
void process_begin ();
bool pass ();
void process_end ();
diff --git a/src/lib/examine_content_job.cc b/src/lib/examine_content_job.cc
index cad560908..d91fc2136 100644
--- a/src/lib/examine_content_job.cc
+++ b/src/lib/examine_content_job.cc
@@ -28,6 +28,7 @@
#include "decoder.h"
#include "imagemagick_encoder.h"
#include "transcoder.h"
+#include "log.h"
using namespace std;
using namespace boost;
@@ -67,7 +68,12 @@ ExamineContentJob::run ()
_decoder = decoder_factory (fs, o, this, _log, true, true);
_decoder->go ();
- fs->set_length (last_video_frame ());
+
+ fs->set_length (_decoder->last_video_frame ());
+ fs->set_audio_delay (-_decoder->audio_to_discard ());
+
+ _log->log (String::compose ("Video length is %1 frames", _decoder->last_video_frame()));
+ _log->log (String::compose ("%1ms of audio to discard", _decoder->audio_to_discard()));
ascend ();
diff --git a/src/lib/ffmpeg_decoder.cc b/src/lib/ffmpeg_decoder.cc
index e52ea735f..d8a541be3 100644
--- a/src/lib/ffmpeg_decoder.cc
+++ b/src/lib/ffmpeg_decoder.cc
@@ -66,6 +66,7 @@ FFmpegDecoder::FFmpegDecoder (boost::shared_ptr<const FilmState> s, boost::share
, _audio_codec (0)
, _subtitle_codec_context (0)
, _subtitle_codec (0)
+ , _first_video_pts (-1)
{
setup_general ();
setup_video ();
@@ -221,6 +222,8 @@ FFmpegDecoder::do_pass ()
_packet.data = 0;
_packet.size = 0;
+ /* XXX: should we reset _packet.data and size after each *_decode_* call? */
+
int frame_finished;
while (avcodec_decode_video2 (_video_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
@@ -245,6 +248,9 @@ FFmpegDecoder::do_pass ()
int frame_finished;
if (avcodec_decode_video2 (_video_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
+ if (_first_video_pts == -1) {
+ _first_video_pts = _packet.pts;
+ }
process_video (_frame);
}
@@ -417,3 +423,15 @@ FFmpegDecoder::stream_name (AVStream* s) const
return n.str ();
}
+
+int
+FFmpegDecoder::audio_to_discard () const
+{
+ AVStream* v = _format_context->streams[_video_stream];
+ AVStream* a = _format_context->streams[_audio_stream];
+
+ assert (v->time_base.num == a->time_base.num);
+ assert (v->time_base.den == a->time_base.den);
+
+ return rint (av_q2d (v->time_base) * 1000 * (_first_video_pts - _first_audio_pts));
+}
diff --git a/src/lib/ffmpeg_decoder.h b/src/lib/ffmpeg_decoder.h
index 339afbaef..dc10635a5 100644
--- a/src/lib/ffmpeg_decoder.h
+++ b/src/lib/ffmpeg_decoder.h
@@ -66,6 +66,7 @@ public:
int64_t audio_channel_layout () const;
bool has_subtitles () const;
int bytes_per_audio_sample () const;
+ int audio_to_discard () const;
std::vector<AudioStream> audio_streams () const;
std::vector<SubtitleStream> subtitle_streams () const;
@@ -105,4 +106,7 @@ private:
AVCodec* _subtitle_codec; ///< may be 0 if there is no subtitle
AVPacket _packet;
+
+ int64_t _first_video_pts;
+ int64_t _first_audio_pts;
};