-#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 * _fs->bytes_per_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 * _fs->bytes_per_sample());
- 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 audio_short_by_frames =
- ((int64_t) _fs->dcp_length() * _fs->target_sample_rate() / _fs->frames_per_second)
- - _audio_frames_processed;
-
- if (audio_short_by_frames >= 0) {
-
- stringstream s;
- s << "Adding " << audio_short_by_frames << " frames of silence to the end.";
- _log->log (s.str ());
-
- int64_t bytes = audio_short_by_frames * _fs->audio_channels * _fs->bytes_per_sample();
-
- int64_t const silence_size = 64 * 1024;
- uint8_t silence[silence_size];
- memset (silence, 0, silence_size);
-
- while (bytes) {
- int64_t const t = min (bytes, silence_size);
- 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 (_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 Interleaved audio data, in FilmState::audio_sample_format.
- * @param size Number of bytes of data.
- */
-void
-Decoder::process_audio (uint8_t* data, int size)
-{
- /* Samples per channel */
- int const samples = size / _fs->bytes_per_sample();
-
- /* Maybe apply gain */
- if (_fs->audio_gain != 0) {
- float const linear_gain = pow (10, _fs->audio_gain / 20);
- uint8_t* p = data;
- switch (_fs->audio_sample_format) {
- case AV_SAMPLE_FMT_S16:
- for (int i = 0; i < samples; ++i) {
- /* XXX: assumes little-endian; also we should probably be dithering here */
-
- /* unsigned sample */
- int const ou = p[0] | (p[1] << 8);
-
- /* signed sample */
- int const os = ou >= 0x8000 ? (- 0x10000 + ou) : ou;
-
- /* signed sample with altered gain */
- int const gs = int (os * linear_gain);