Tidy up test film naming.
[dcpomatic.git] / src / lib / decoder.cc
index ee725c39d56eee0d03ce3956b021349edd5ec739..045c1b818270052d58a93249377a7e2a6f5c6e27 100644 (file)
@@ -105,26 +105,35 @@ Decoder::process_end ()
                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);
+               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 audio_short_by_frames =
-               ((int64_t) _fs->dcp_length() * _fs->target_sample_rate() / _fs->frames_per_second())
-               - _audio_frames_processed;
+       int64_t const video_length_in_audio_frames = ((int64_t) _fs->dcp_length() * _fs->audio_sample_rate() / _fs->frames_per_second());
+       int64_t const audio_short_by_frames = video_length_in_audio_frames - _audio_frames_processed;
 
-       if (audio_short_by_frames >= 0) {
+       _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));
 
-               stringstream s;
-               s << "Adding " << audio_short_by_frames << " frames of silence to the end.";
-               _log->log (s.str ());
+               /* XXX: this is slightly questionable; does memset () give silence with all
+                  sample formats?
+               */
 
                int64_t bytes = audio_short_by_frames * _fs->audio_channels() * bytes_per_audio_sample();
                
-               int64_t const silence_size = 64 * 1024;
+               int64_t const silence_size = 16 * 1024 * _fs->audio_channels() * bytes_per_audio_sample();
                uint8_t silence[silence_size];
                memset (silence, 0, silence_size);
                
@@ -167,7 +176,7 @@ Decoder::pass ()
                _have_setup_video_filters = true;
        }
        
-       if (_video_frame >= _fs->dcp_length()) {
+       if (!_ignore_length && _video_frame >= _fs->dcp_length()) {
                return true;
        }
 
@@ -192,6 +201,8 @@ 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));
@@ -199,28 +210,37 @@ Decoder::emit_audio (uint8_t* data, int size)
        switch (audio_sample_format()) {
        case AV_SAMPLE_FMT_S16:
        {
-               uint8_t* p = data;
+               int16_t* p = (int16_t *) data;
                int sample = 0;
                int channel = 0;
                for (int i = 0; i < total_samples; ++i) {
-                       /* unsigned sample */
-                       int const ou = p[0] | (p[1] << 8);
-                       /* signed sample */
-                       int const os = ou >= 0x8000 ? (- 0x10000 + ou) : ou;
-                       /* float sample */
-                       audio->data(channel)[sample] = float(os) / 0x8000;
+                       audio->data(channel)[sample] = float(*p++) / (1 << 15);
 
                        ++channel;
                        if (channel == _fs->audio_channels()) {
                                channel = 0;
                                ++sample;
                        }
-
-                       p += 2;
                }
        }
        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);
+
+                       ++channel;
+                       if (channel == _fs->audio_channels()) {
+                               channel = 0;
+                               ++sample;
+                       }
+               }
+       }
+
        case AV_SAMPLE_FMT_FLTP:
        {
                float* p = reinterpret_cast<float*> (data);
@@ -246,7 +266,7 @@ Decoder::emit_audio (uint8_t* data, int size)
        }
 
        /* Update the number of audio frames we've pushed to the encoder */
-       _audio_frames_processed += frames;
+       _audio_frames_processed += audio->frames ();
 
        Audio (audio);
 }
@@ -332,7 +352,7 @@ Decoder::process_video (AVFrame* frame)
                        }
 
                        shared_ptr<Subtitle> sub;
-                       if (_timed_subtitle && _timed_subtitle->displayed_at (double (last_video_frame()) / rint (_fs->frames_per_second()))) {
+                       if (_timed_subtitle && _timed_subtitle->displayed_at (double (last_video_frame()) / _fs->frames_per_second())) {
                                sub = _timed_subtitle->subtitle ();
                        }
 
@@ -443,7 +463,7 @@ Decoder::process_subtitle (shared_ptr<TimedSubtitle> s)
 {
        _timed_subtitle = s;
        
-       if (_opt->apply_crop) {
+       if (_timed_subtitle && _opt->apply_crop) {
                Position const p = _timed_subtitle->subtitle()->position ();
                _timed_subtitle->subtitle()->set_position (Position (p.x - _fs->crop().left, p.y - _fs->crop().top));
        }