Fix crash on using delay; fix x-thread GUI access caused by FilmState default copy...
[dcpomatic.git] / src / lib / decoder.cc
index ec046fcafe5d537cf329df098178da585f4856c0..a90c14b2bdc995af6664e51965a68bd7995978f8 100644 (file)
@@ -90,7 +90,7 @@ Decoder::~Decoder ()
 void
 Decoder::process_begin ()
 {
-       _delay_in_bytes = _fs->audio_delay() * _fs->audio_sample_rate() * _fs->audio_channels() * _fs->bytes_per_sample() / 1000;
+       _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);
 
@@ -104,27 +104,32 @@ 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());
-               emit_audio (remainder, _delay_in_bytes);
+               _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 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;
 
-       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) {
 
-               stringstream s;
-               s << "Adding " << audio_short_by_frames << " frames of silence to the end.";
-               _log->log (s.str ());
+               _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() * _fs->bytes_per_sample();
+               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 +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;
        }
 
@@ -192,49 +197,52 @@ Decoder::emit_audio (uint8_t* data, int size)
 {
        /* Deinterleave and convert to float */
 
-       float* samples[_fs->audio_channels()];
-       int const total_samples = size / _fs->bytes_per_sample();
+       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();
-       for (int i = 0; i < _fs->audio_channels(); ++i) {
-               samples[i] = new float[frames];
-       }
+       shared_ptr<AudioBuffers> audio (new AudioBuffers (_fs->audio_channels(), frames));
 
-       switch (_fs->audio_sample_format()) {
+       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 */
-                       samples[channel][sample] = float(os) / 0x8000;
-
-                       cout << samples[channel][sample] << " from s16\n";
-                       
+                       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);
                for (int i = 0; i < _fs->audio_channels(); ++i) {
-                       for (int j = 0; j < frames; ++j) {
-                               samples[i][j] = *p++;
-                               cout << samples[i][j] << " from float.\n";
-                               ++p;
-                       }
+                       memcpy (audio->data(i), p, frames * sizeof(float));
+                       p += frames;
                }
        }
        break;
@@ -248,7 +256,7 @@ Decoder::emit_audio (uint8_t* data, int size)
                float const linear_gain = pow (10, _fs->audio_gain() / 20);
                for (int i = 0; i < _fs->audio_channels(); ++i) {
                        for (int j = 0; j < frames; ++j) {
-                               samples[i][j] *= linear_gain;
+                               audio->data(i)[j] *= linear_gain;
                        }
                }
        }
@@ -256,11 +264,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 (samples, frames);
-
-       for (int i = 0; i < _fs->audio_channels(); ++i) {
-               delete[] samples[i];
-       }
+       Audio (audio);
 }
 
 /** Called by subclasses to tell the world that some video data is ready.
@@ -460,3 +464,10 @@ Decoder::process_subtitle (shared_ptr<TimedSubtitle> s)
                _timed_subtitle->subtitle()->set_position (Position (p.x - _fs->crop().left, p.y - _fs->crop().top));
        }
 }
+
+
+int
+Decoder::bytes_per_audio_sample () const
+{
+       return av_get_bytes_per_sample (audio_sample_format ());
+}