summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2012-10-31 11:46:05 +0000
committerCarl Hetherington <cth@carlh.net>2012-10-31 11:46:05 +0000
commit09a95eca971ddd2ea75cd615e7ef2b997d52b690 (patch)
tree3c429226dc070cbf0800a2ebbfdd72fa19b2156c /src
parent3ed5df31baed21633d0db9167cd75562f384dec7 (diff)
Try a different approach to frame duplication in the decoder.
Diffstat (limited to 'src')
-rw-r--r--src/lib/dcp_video_frame.cc2
-rw-r--r--src/lib/dcp_video_frame.h4
-rw-r--r--src/lib/decoder.cc12
-rw-r--r--src/lib/decoder.h4
-rw-r--r--src/lib/encoder.h2
-rw-r--r--src/lib/ffmpeg_decoder.cc65
-rw-r--r--src/lib/ffmpeg_decoder.h5
-rw-r--r--src/lib/imagemagick_encoder.cc2
-rw-r--r--src/lib/imagemagick_encoder.h2
-rw-r--r--src/lib/j2k_still_encoder.cc2
-rw-r--r--src/lib/j2k_still_encoder.h2
-rw-r--r--src/lib/j2k_wav_encoder.cc2
-rw-r--r--src/lib/j2k_wav_encoder.h2
13 files changed, 49 insertions, 57 deletions
diff --git a/src/lib/dcp_video_frame.cc b/src/lib/dcp_video_frame.cc
index 201c74ec1..9d6022efa 100644
--- a/src/lib/dcp_video_frame.cc
+++ b/src/lib/dcp_video_frame.cc
@@ -74,7 +74,7 @@ using boost::shared_ptr;
* @param l Log to write to.
*/
DCPVideoFrame::DCPVideoFrame (
- shared_ptr<Image> yuv, shared_ptr<Subtitle> sub,
+ shared_ptr<const Image> yuv, shared_ptr<Subtitle> sub,
Size out, int p, int subtitle_offset, float subtitle_scale,
Scaler const * s, int f, float fps, string pp, int clut, int bw, Log* l
)
diff --git a/src/lib/dcp_video_frame.h b/src/lib/dcp_video_frame.h
index 4e9a777bd..0ff29f7bf 100644
--- a/src/lib/dcp_video_frame.h
+++ b/src/lib/dcp_video_frame.h
@@ -106,7 +106,7 @@ public:
class DCPVideoFrame
{
public:
- DCPVideoFrame (boost::shared_ptr<Image>, boost::shared_ptr<Subtitle>, Size, int, int, float, Scaler const *, int, float, std::string, int, int, Log *);
+ DCPVideoFrame (boost::shared_ptr<const Image>, boost::shared_ptr<Subtitle>, Size, int, int, float, Scaler const *, int, float, std::string, int, int, Log *);
virtual ~DCPVideoFrame ();
boost::shared_ptr<EncodedData> encode_locally ();
@@ -120,7 +120,7 @@ private:
void create_openjpeg_container ();
void write_encoded (boost::shared_ptr<const Options>, uint8_t *, int);
- boost::shared_ptr<Image> _input; ///< the input image
+ boost::shared_ptr<const Image> _input; ///< the input image
boost::shared_ptr<Subtitle> _subtitle; ///< any subtitle that should be on the image
Size _out_size; ///< the required size of the output, in pixels
int _padding;
diff --git a/src/lib/decoder.cc b/src/lib/decoder.cc
index abb4d13a1..e154aac5d 100644
--- a/src/lib/decoder.cc
+++ b/src/lib/decoder.cc
@@ -310,12 +310,22 @@ Decoder::process_video (AVFrame* frame)
}
TIMING ("Decoder emits %1", _video_frame_index);
- Video ((*i), _video_frame_index, sub);
+ Video (*i, _video_frame_index, sub);
++_video_frame_index;
+ _last_image = *i;
+ _last_subtitle = sub;
}
}
void
+Decoder::repeat_last_video ()
+{
+ assert (_last_image);
+ Video (_last_image, _video_frame_index, _last_subtitle);
+ ++_video_frame_index;
+}
+
+void
Decoder::process_subtitle (shared_ptr<TimedSubtitle> s)
{
_timed_subtitle = s;
diff --git a/src/lib/decoder.h b/src/lib/decoder.h
index 2af9157b4..6cd7757b6 100644
--- a/src/lib/decoder.h
+++ b/src/lib/decoder.h
@@ -112,6 +112,7 @@ protected:
void process_video (AVFrame *);
void process_audio (uint8_t *, int);
void process_subtitle (boost::shared_ptr<TimedSubtitle>);
+ void repeat_last_video ();
int bytes_per_audio_sample () const;
@@ -147,6 +148,9 @@ private:
int64_t _audio_frames_processed;
boost::shared_ptr<TimedSubtitle> _timed_subtitle;
+
+ boost::shared_ptr<Image> _last_image;
+ boost::shared_ptr<Subtitle> _last_subtitle;
};
#endif
diff --git a/src/lib/encoder.h b/src/lib/encoder.h
index 4d741a92b..590b8aaa9 100644
--- a/src/lib/encoder.h
+++ b/src/lib/encoder.h
@@ -61,7 +61,7 @@ public:
* @param f Frame number within the film.
* @param s A subtitle that should be on this frame, or 0.
*/
- virtual void process_video (boost::shared_ptr<Image> i, int f, boost::shared_ptr<Subtitle> s) = 0;
+ virtual void process_video (boost::shared_ptr<const Image> i, int f, boost::shared_ptr<Subtitle> s) = 0;
/** Called with some audio data.
* @param d Array of pointers to floating point sample data for each channel.
diff --git a/src/lib/ffmpeg_decoder.cc b/src/lib/ffmpeg_decoder.cc
index a9db6f489..47cc02e61 100644
--- a/src/lib/ffmpeg_decoder.cc
+++ b/src/lib/ffmpeg_decoder.cc
@@ -61,9 +61,7 @@ FFmpegDecoder::FFmpegDecoder (shared_ptr<Film> f, shared_ptr<const Options> o, J
, _video_stream (-1)
, _audio_stream (-1)
, _subtitle_stream (-1)
- , _last_video_frame (-1)
- , _this_video_frame (0)
- , _audio_frame (0)
+ , _frame (0)
, _video_codec_context (0)
, _video_codec (0)
, _audio_codec_context (0)
@@ -71,10 +69,6 @@ FFmpegDecoder::FFmpegDecoder (shared_ptr<Film> f, shared_ptr<const Options> o, J
, _subtitle_codec_context (0)
, _subtitle_codec (0)
{
- for (int i = 0; i < 2; ++i) {
- _video_frame[i] = 0;
- }
-
setup_general ();
setup_video ();
setup_audio ();
@@ -95,11 +89,7 @@ FFmpegDecoder::~FFmpegDecoder ()
avcodec_close (_subtitle_codec_context);
}
- for (int i = 0; i < 2; ++i) {
- av_free (_video_frame[i]);
- }
-
- av_free (_audio_frame);
+ av_free (_frame);
avformat_close_input (&_format_context);
}
@@ -152,15 +142,8 @@ FFmpegDecoder::setup_general ()
throw DecodeError ("could not find video stream");
}
- for (int i = 0; i < 2; ++i) {
- _video_frame[i] = avcodec_alloc_frame ();
- if (_video_frame[i] == 0) {
- throw DecodeError ("could not allocate frame");
- }
- }
-
- _audio_frame = avcodec_alloc_frame ();
- if (_audio_frame == 0) {
+ _frame = avcodec_alloc_frame ();
+ if (_frame == 0) {
throw DecodeError ("could not allocate frame");
}
}
@@ -253,18 +236,18 @@ FFmpegDecoder::do_pass ()
int frame_finished;
- while (avcodec_decode_video2 (_video_codec_context, _video_frame[_this_video_frame], &frame_finished, &_packet) >= 0 && frame_finished) {
- process_video (_video_frame[_this_video_frame]);
+ while (avcodec_decode_video2 (_video_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
+ process_video (_frame);
}
if (_audio_stream >= 0 && _opt->decode_audio) {
- while (avcodec_decode_audio4 (_audio_codec_context, _audio_frame, &frame_finished, &_packet) >= 0 && frame_finished) {
+ while (avcodec_decode_audio4 (_audio_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
int const data_size = av_samples_get_buffer_size (
- 0, _audio_codec_context->channels, _audio_frame->nb_samples, audio_sample_format (), 1
+ 0, _audio_codec_context->channels, _frame->nb_samples, audio_sample_format (), 1
);
assert (_audio_codec_context->channels == _film->audio_channels());
- process_audio (_audio_frame->data[0], data_size);
+ process_audio (_frame->data[0], data_size);
}
}
@@ -273,16 +256,16 @@ FFmpegDecoder::do_pass ()
double const pts_seconds = av_q2d (_format_context->streams[_packet.stream_index]->time_base) * _packet.pts;
+ avcodec_get_frame_defaults (_frame);
+
if (_packet.stream_index == _video_stream) {
- avcodec_get_frame_defaults (_video_frame[_this_video_frame]);
-
if (!_first_video) {
_first_video = pts_seconds;
}
int frame_finished;
- if (avcodec_decode_video2 (_video_codec_context, _video_frame[_this_video_frame], &frame_finished, &_packet) >= 0 && frame_finished) {
+ if (avcodec_decode_video2 (_video_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
/* Where we are in the output, in seconds */
double const out_pts_seconds = video_frame_index() / frames_per_second();
@@ -291,29 +274,27 @@ FFmpegDecoder::do_pass ()
double const delta = pts_seconds - out_pts_seconds;
double const one_frame = 1 / frames_per_second();
- /* Insert the last frame if we have one, otherwise just use this one */
- int const insert_frame = _last_video_frame == -1 ? _this_video_frame : _last_video_frame;
-
/* Insert frames if required to get out_pts_seconds up to pts_seconds */
if (delta > one_frame) {
int const extra = rint (delta / one_frame);
for (int i = 0; i < extra; ++i) {
- _film->log()->log (String::compose ("Extra frame inserted at %1s", out_pts_seconds));
- process_video (_video_frame[insert_frame]);
+ repeat_last_video ();
+ _film->log()->log (
+ String::compose (
+ "Extra frame inserted at %1s; DCP frame %2, packet PTS %3",
+ out_pts_seconds, video_frame_index(), pts_seconds
+ )
+ );
}
}
if (delta > -one_frame) {
/* Process this frame */
- process_video (_video_frame[_this_video_frame]);
+ process_video (_frame);
} else {
/* Otherwise we are omitting a frame to keep things right */
_film->log()->log (String::compose ("Frame removed at %1s", out_pts_seconds));
}
-
- /* Swap over so that we use the alternate video frames next time */
- _this_video_frame = 1 - _this_video_frame;
- _last_video_frame = 1 - _this_video_frame;
}
} else if (_audio_stream >= 0 && _packet.stream_index == _audio_stream && _opt->decode_audio && _first_video && _first_video.get() <= pts_seconds) {
@@ -354,13 +335,13 @@ FFmpegDecoder::do_pass ()
}
int frame_finished;
- if (avcodec_decode_audio4 (_audio_codec_context, _audio_frame, &frame_finished, &_packet) >= 0 && frame_finished) {
+ if (avcodec_decode_audio4 (_audio_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
int const data_size = av_samples_get_buffer_size (
- 0, _audio_codec_context->channels, _audio_frame->nb_samples, audio_sample_format (), 1
+ 0, _audio_codec_context->channels, _frame->nb_samples, audio_sample_format (), 1
);
assert (_audio_codec_context->channels == _film->audio_channels());
- process_audio (_audio_frame->data[0], data_size);
+ process_audio (_frame->data[0], data_size);
}
} else if (_subtitle_stream >= 0 && _packet.stream_index == _subtitle_stream && _opt->decode_subtitles && _first_video) {
diff --git a/src/lib/ffmpeg_decoder.h b/src/lib/ffmpeg_decoder.h
index 58e08a779..c04dba5b3 100644
--- a/src/lib/ffmpeg_decoder.h
+++ b/src/lib/ffmpeg_decoder.h
@@ -90,10 +90,7 @@ private:
int _audio_stream; ///< may be < 0 if there is no audio
int _subtitle_stream; ///< may be < 0 if there is no subtitle
- AVFrame* _video_frame[2];
- int _last_video_frame;
- int _this_video_frame;
- AVFrame* _audio_frame;
+ AVFrame* _frame;
std::vector<AudioStream> _audio_streams;
std::vector<SubtitleStream> _subtitle_streams;
diff --git a/src/lib/imagemagick_encoder.cc b/src/lib/imagemagick_encoder.cc
index 8d42b3dbe..e8b060997 100644
--- a/src/lib/imagemagick_encoder.cc
+++ b/src/lib/imagemagick_encoder.cc
@@ -50,7 +50,7 @@ ImageMagickEncoder::ImageMagickEncoder (shared_ptr<const Film> f, shared_ptr<con
}
void
-ImageMagickEncoder::process_video (shared_ptr<Image> image, int frame, shared_ptr<Subtitle> sub)
+ImageMagickEncoder::process_video (shared_ptr<const Image> image, int frame, shared_ptr<Subtitle> sub)
{
shared_ptr<Image> scaled = image->scale_and_convert_to_rgb (_opt->out_size, _opt->padding, _film->scaler());
shared_ptr<Image> compact (new CompactImage (scaled));
diff --git a/src/lib/imagemagick_encoder.h b/src/lib/imagemagick_encoder.h
index f5e97ccd8..c112300e3 100644
--- a/src/lib/imagemagick_encoder.h
+++ b/src/lib/imagemagick_encoder.h
@@ -37,7 +37,7 @@ public:
ImageMagickEncoder (boost::shared_ptr<const Film> f, boost::shared_ptr<const Options> o);
void process_begin (int64_t audio_channel_layout) {}
- void process_video (boost::shared_ptr<Image>, int, boost::shared_ptr<Subtitle>);
+ void process_video (boost::shared_ptr<const Image>, int, boost::shared_ptr<Subtitle>);
void process_audio (boost::shared_ptr<const AudioBuffers>) {}
void process_end () {}
};
diff --git a/src/lib/j2k_still_encoder.cc b/src/lib/j2k_still_encoder.cc
index 56fbb3152..edcf0de89 100644
--- a/src/lib/j2k_still_encoder.cc
+++ b/src/lib/j2k_still_encoder.cc
@@ -49,7 +49,7 @@ J2KStillEncoder::J2KStillEncoder (shared_ptr<const Film> f, shared_ptr<const Opt
}
void
-J2KStillEncoder::process_video (shared_ptr<Image> yuv, int frame, shared_ptr<Subtitle> sub)
+J2KStillEncoder::process_video (shared_ptr<const Image> yuv, int frame, shared_ptr<Subtitle> sub)
{
pair<string, string> const s = Filter::ffmpeg_strings (_film->filters());
DCPVideoFrame* f = new DCPVideoFrame (
diff --git a/src/lib/j2k_still_encoder.h b/src/lib/j2k_still_encoder.h
index 433c497a6..86bebc0d2 100644
--- a/src/lib/j2k_still_encoder.h
+++ b/src/lib/j2k_still_encoder.h
@@ -37,7 +37,7 @@ public:
J2KStillEncoder (boost::shared_ptr<const Film>, boost::shared_ptr<const Options>);
void process_begin (int64_t audio_channel_layout) {}
- void process_video (boost::shared_ptr<Image>, int, boost::shared_ptr<Subtitle>);
+ void process_video (boost::shared_ptr<const Image>, int, boost::shared_ptr<Subtitle>);
void process_audio (boost::shared_ptr<const AudioBuffers>) {}
void process_end () {}
};
diff --git a/src/lib/j2k_wav_encoder.cc b/src/lib/j2k_wav_encoder.cc
index 73a70910e..bac7d5f35 100644
--- a/src/lib/j2k_wav_encoder.cc
+++ b/src/lib/j2k_wav_encoder.cc
@@ -106,7 +106,7 @@ J2KWAVEncoder::close_sound_files ()
}
void
-J2KWAVEncoder::process_video (shared_ptr<Image> yuv, int frame, shared_ptr<Subtitle> sub)
+J2KWAVEncoder::process_video (shared_ptr<const Image> yuv, int frame, shared_ptr<Subtitle> sub)
{
boost::mutex::scoped_lock lock (_worker_mutex);
diff --git a/src/lib/j2k_wav_encoder.h b/src/lib/j2k_wav_encoder.h
index 95a802b6e..99924829d 100644
--- a/src/lib/j2k_wav_encoder.h
+++ b/src/lib/j2k_wav_encoder.h
@@ -51,7 +51,7 @@ public:
~J2KWAVEncoder ();
void process_begin (int64_t audio_channel_layout);
- void process_video (boost::shared_ptr<Image>, int, boost::shared_ptr<Subtitle>);
+ void process_video (boost::shared_ptr<const Image>, int, boost::shared_ptr<Subtitle>);
void process_audio (boost::shared_ptr<const AudioBuffers>);
void process_end ();