Fix crash on using delay; fix x-thread GUI access caused by FilmState default copy...
[dcpomatic.git] / src / lib / decoder.cc
index 14401994fbad7e847881b66cb283a91c68ff0d70..a90c14b2bdc995af6664e51965a68bd7995978f8 100644 (file)
@@ -105,17 +105,23 @@ 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->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));
@@ -166,7 +172,7 @@ Decoder::pass ()
                _have_setup_video_filters = true;
        }
        
-       if (_video_frame >= _fs->dcp_length()) {
+       if (!_ignore_length && _video_frame >= _fs->dcp_length()) {
                return true;
        }
 
@@ -200,28 +206,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);