-#include "filter.h"
-#include "delay_line.h"
-#include "ffmpeg_compatibility.h"
-#include "subtitle.h"
-
-using namespace std;
-using namespace boost;
-
-/** @param s FilmState of the Film.
- * @param o Options.
- * @param j Job that we are running within, or 0
- * @param l Log to use.
- * @param minimal true to do the bare minimum of work; just run through the content. Useful for acquiring
- * accurate frame counts as quickly as possible. This generates no video or audio output.
- * @param ignore_length Ignore the content's claimed length when computing progress.
- */
-Decoder::Decoder (boost::shared_ptr<const FilmState> s, boost::shared_ptr<const Options> o, Job* j, Log* l, bool minimal, bool ignore_length)
- : _fs (s)
- , _opt (o)
- , _job (j)
- , _log (l)
- , _minimal (minimal)
- , _ignore_length (ignore_length)
- , _video_frame (0)
- , _buffer_src_context (0)
- , _buffer_sink_context (0)
- , _have_setup_video_filters (false)
- , _delay_line (0)
- , _delay_in_bytes (0)
- , _audio_frames_processed (0)
-{
- if (_opt->decode_video_frequency != 0 && _fs->length() == 0) {
- throw DecodeError ("cannot do a partial decode if length == 0");
- }
-}
-
-Decoder::~Decoder ()
-{
- delete _delay_line;
-}
-
-/** Start off a decode processing run */
-void
-Decoder::process_begin ()
-{
- _delay_in_bytes = _fs->audio_delay() * _fs->audio_sample_rate() * _fs->audio_channels() * bytes_per_audio_sample() / 1000;
- delete _delay_line;
- _delay_line = new DelayLine (_delay_in_bytes);
-
- _audio_frames_processed = 0;
-}
-
-/** Finish off a decode processing run */
-void
-Decoder::process_end ()
-{
- if (_delay_in_bytes < 0) {
- uint8_t remainder[-_delay_in_bytes];
- _delay_line->get_remaining (remainder);
- _audio_frames_processed += _delay_in_bytes / (_fs->audio_channels() * bytes_per_audio_sample());
- emit_audio (remainder, _delay_in_bytes);
- }
-
- /* If we cut the decode off, the audio may be short; push some silence
- in to get it to the right length.
- */
-
- int64_t const video_length_in_audio_frames = ((int64_t) _fs->dcp_length() * _fs->target_sample_rate() / _fs->frames_per_second());
- int64_t const audio_short_by_frames = video_length_in_audio_frames - _audio_frames_processed;
-
- _log->log (
- String::compose ("DCP length is %1 (%2 audio frames); %3 frames of audio processed.",
- _fs->dcp_length(),
- video_length_in_audio_frames,
- _audio_frames_processed)
- );
-
- if (audio_short_by_frames >= 0 && _opt->decode_audio) {
-
- _log->log (String::compose ("DCP length is %1; %2 frames of audio processed.", _fs->dcp_length(), _audio_frames_processed));
- _log->log (String::compose ("Adding %1 frames of silence to the end.", audio_short_by_frames));
-
- int64_t bytes = audio_short_by_frames * _fs->audio_channels() * bytes_per_audio_sample();
-
- int64_t const silence_size = 16 * 1024 * _fs->audio_channels() * bytes_per_audio_sample();
- uint8_t silence[silence_size];
- memset (silence, 0, silence_size);
-
- while (bytes) {
- int64_t const t = min (bytes, silence_size);
- emit_audio (silence, t);
- bytes -= t;
- }
- }
-}
-
-/** Start decoding */
-void
-Decoder::go ()
-{
- process_begin ();
-
- if (_job && _ignore_length) {
- _job->set_progress_unknown ();
- }
-
- while (pass () == false) {
- if (_job && !_ignore_length) {
- _job->set_progress (float (_video_frame) / _fs->dcp_length());
- }
- }
-
- process_end ();
-}
-
-/** Run one pass. This may or may not generate any actual video / audio data;
- * some decoders may require several passes to generate a single frame.
- * @return true if we have finished processing all data; otherwise false.
- */
-bool
-Decoder::pass ()
-{
- if (!_have_setup_video_filters) {
- setup_video_filters ();
- _have_setup_video_filters = true;
- }
-
- if (!_ignore_length && _video_frame >= _fs->dcp_length()) {
- return true;
- }
-
- return do_pass ();
-}
-
-/** Called by subclasses to tell the world that some audio data is ready
- * @param data Audio data, in FilmState::audio_sample_format.
- * @param size Number of bytes of data.
- */
-void
-Decoder::process_audio (uint8_t* data, int size)
-{
- /* Push into the delay line */
- size = _delay_line->feed (data, size);
-
- emit_audio (data, size);
-}
-
-void
-Decoder::emit_audio (uint8_t* data, int size)
-{
- /* Deinterleave and convert to float */
-
- assert ((size % (bytes_per_audio_sample() * _fs->audio_channels())) == 0);
-
- int const total_samples = size / bytes_per_audio_sample();
- int const frames = total_samples / _fs->audio_channels();
- shared_ptr<AudioBuffers> audio (new AudioBuffers (_fs->audio_channels(), frames));
-
- switch (audio_sample_format()) {
- case AV_SAMPLE_FMT_S16:
- {
- int16_t* p = (int16_t *) data;
- int sample = 0;
- int channel = 0;
- for (int i = 0; i < total_samples; ++i) {
- audio->data(channel)[sample] = float(*p++) / (1 << 15);
-
- ++channel;
- if (channel == _fs->audio_channels()) {
- channel = 0;
- ++sample;
- }
- }
- }
- break;
-
- case AV_SAMPLE_FMT_S32:
- {
- int32_t* p = (int32_t *) data;
- int sample = 0;
- int channel = 0;
- for (int i = 0; i < total_samples; ++i) {
- audio->data(channel)[sample] = float(*p++) / (1 << 31);