Setup fast state of decoder after creation.
[dcpomatic.git] / src / lib / resampler.cc
index d08e7bc38e22f60eab6486d4c9626dc5562837f7..b27d7c9668dda9f4bbb0419fcf1a2ed381f42c75 100644 (file)
@@ -43,6 +43,7 @@ Resampler::Resampler (int in, int out, int channels)
        : _in_rate (in)
        , _out_rate (out)
        , _channels (channels)
+       , _fast (false)
 {
        int error;
        _src = src_new (SRC_SINC_BEST_QUALITY, _channels, &error);
@@ -53,33 +54,33 @@ Resampler::Resampler (int in, int out, int channels)
 
 Resampler::~Resampler ()
 {
-       src_delete (_src);
+       if (_src) {
+               src_delete (_src);
+       }
 }
 
 void
-Resampler::set_fast ()
+Resampler::set_fast (bool fast)
 {
+       if (fast == _fast) {
+               return;
+       }
+
+       _fast = fast;
+
        src_delete (_src);
+       _src = 0;
+
        int error;
-       _src = src_new (SRC_LINEAR, _channels, &error);
+       _src = src_new (_fast ? SRC_LINEAR : SRC_SINC_BEST_QUALITY, _channels, &error);
        if (!_src) {
                throw runtime_error (String::compose (N_("could not create sample-rate converter (%1)"), error));
        }
 }
 
-pair<shared_ptr<const AudioBuffers>, Frame>
-Resampler::run (shared_ptr<const AudioBuffers> in, Frame frame)
+shared_ptr<const AudioBuffers>
+Resampler::run (shared_ptr<const AudioBuffers> in)
 {
-       if (!_next_in || !_next_out || _next_in.get() != frame) {
-               /* Either there was a discontinuity in the input or this is the first input;
-                  reset _next_out.
-               */
-               _next_out = lrintf (frame * _out_rate / _in_rate);
-       }
-
-       /* Expected next input frame */
-       _next_in = frame + in->frames ();
-
        int in_frames = in->frames ();
        int in_offset = 0;
        int out_offset = 0;
@@ -128,6 +129,8 @@ Resampler::run (shared_ptr<const AudioBuffers> in, Frame frame)
                }
 
                if (data.output_frames_gen == 0) {
+                       delete[] data.data_in;
+                       delete[] data.data_out;
                        break;
                }
 
@@ -152,12 +155,7 @@ Resampler::run (shared_ptr<const AudioBuffers> in, Frame frame)
                delete[] data.data_out;
        }
 
-       Frame out_frame = _next_out.get ();
-
-       /* Expected next output frame */
-       _next_out = _next_out.get() + resampled->frames();
-
-       return make_pair (resampled, out_frame);
+       return resampled;
 }
 
 shared_ptr<const AudioBuffers>
@@ -200,3 +198,9 @@ Resampler::flush ()
        delete[] buffer;
        return out;
 }
+
+void
+Resampler::reset ()
+{
+       src_reset (_src);
+}