]> git.sesse.net Git - nageru/commitdiff
Remove some random unneeded memory allocations.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 9 Nov 2017 20:00:03 +0000 (21:00 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 9 Nov 2017 20:00:03 +0000 (21:00 +0100)
mixer.cpp
theme.cpp
x264_encoder.cpp
x264_encoder.h

index f45880d321be6d2a1c17c5a61b7ba0babc28e846..f502eb0e4590c86092588841bd04298e1f4f4600 100644 (file)
--- a/mixer.cpp
+++ b/mixer.cpp
@@ -1396,10 +1396,10 @@ void Mixer::render_one_frame(int64_t duration)
        for (int i = 1; i < theme->get_num_channels() + 2; ++i) {
                DisplayFrame display_frame;
                Theme::Chain chain = theme->get_chain(i, pts(), global_flags.width, global_flags.height, input_state);  // FIXME: dimensions
-               display_frame.chain = chain.chain;
-               display_frame.setup_chain = chain.setup_chain;
+               display_frame.chain = move(chain.chain);
+               display_frame.setup_chain = move(chain.setup_chain);
                display_frame.ready_fence = fence;
-               display_frame.input_frames = chain.input_frames;
+               display_frame.input_frames = move(chain.input_frames);
                display_frame.temp_textures = {};
                output_channel[i].output_frame(display_frame);
        }
@@ -1530,7 +1530,7 @@ void Mixer::OutputChannel::output_frame(DisplayFrame frame)
                if (has_ready_frame) {
                        parent->release_display_frame(&ready_frame);
                }
-               ready_frame = frame;
+               ready_frame = move(frame);
                has_ready_frame = true;
 
                // Call the callbacks under the mutex (they should be short),
@@ -1593,7 +1593,7 @@ bool Mixer::OutputChannel::get_display_frame(DisplayFrame *frame)
        }
        if (has_ready_frame) {
                assert(!has_current_frame);
-               current_frame = ready_frame;
+               current_frame = move(ready_frame);
                ready_frame.ready_fence.reset();  // Drop the refcount.
                ready_frame.input_frames.clear();  // Drop the refcounts.
                has_current_frame = true;
index ed34faed2c9a1411ce7417c06da9ab68f5e1fd40..c03776b4b8e0a8b53bbb9ec04f625bff74a707c5 100644 (file)
--- a/theme.cpp
+++ b/theme.cpp
@@ -1043,6 +1043,7 @@ Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned he
 
        // TODO: Can we do better, e.g. by running setup_chain() and seeing what it references?
        // Actually, setup_chain does maybe hold all the references we need now anyway?
+       chain.input_frames.reserve(num_cards * FRAME_HISTORY_LENGTH);
        for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
                for (unsigned frame_num = 0; frame_num < FRAME_HISTORY_LENGTH; ++frame_num) {
                        chain.input_frames.push_back(input_state.buffered_frames[card_index][frame_num].frame);
index 70b50e4b94f7369f9d1e8447c17a784a4e194fa7..ccf6942e1813ef7aa4748b45b531d108dd06d64f 100644 (file)
@@ -9,6 +9,7 @@
 #include <x264.h>
 #include <atomic>
 #include <cstdint>
+#include <functional>
 #include <mutex>
 
 #include "defs.h"
@@ -28,6 +29,7 @@ extern "C" {
 using namespace movit;
 using namespace std;
 using namespace std::chrono;
+using namespace std::placeholders;
 
 namespace {
 
@@ -330,38 +332,13 @@ void X264Encoder::encode_frame(X264Encoder::QueuedFrame qf)
                frames_being_encoded[qf.pts] = qf.received_ts;
        }
 
-       // See if we have a new bitrate to change to.
-       unsigned new_rate = new_bitrate_kbit.exchange(0);  // Read and clear.
-       if (new_rate != 0) {
-               bitrate_override_func = [new_rate](x264_param_t *param) {
-                       param->rc.i_bitrate = new_rate;
-                       update_vbv_settings(param);
-               };
-       }
-
-       auto ycbcr_coefficients_override_func = [qf](x264_param_t *param) {
-               if (qf.ycbcr_coefficients == YCBCR_REC_709) {
-                       param->vui.i_colmatrix = 1;  // BT.709.
-               } else {
-                       assert(qf.ycbcr_coefficients == YCBCR_REC_601);
-                       param->vui.i_colmatrix = 6;  // BT.601/SMPTE 170M.
-               }
-       };
-
+       unsigned new_rate = new_bitrate_kbit.load();  // Can be 0 for no change.
        if (speed_control) {
-               speed_control->set_config_override_function([this, ycbcr_coefficients_override_func](x264_param_t *param) {
-                       if (bitrate_override_func) {
-                               bitrate_override_func(param);
-                       }
-                       ycbcr_coefficients_override_func(param);
-               });
+               speed_control->set_config_override_function(bind(&speed_control_override_func, new_rate, qf.ycbcr_coefficients, _1));
        } else {
                x264_param_t param;
                dyn.x264_encoder_parameters(x264, &param);
-               if (bitrate_override_func) {
-                       bitrate_override_func(&param);
-               }
-               ycbcr_coefficients_override_func(&param);
+               speed_control_override_func(new_rate, qf.ycbcr_coefficients, &param);
                dyn.x264_encoder_reconfig(x264, &param);
        }
 
@@ -434,3 +411,18 @@ void X264Encoder::encode_frame(X264Encoder::QueuedFrame qf)
                mux->add_packet(pkt, pic.i_pts, pic.i_dts);
        }
 }
+
+void X264Encoder::speed_control_override_func(unsigned bitrate_kbit, movit::YCbCrLumaCoefficients ycbcr_coefficients, x264_param_t *param)
+{
+       if (bitrate_kbit != 0) {
+               param->rc.i_bitrate = bitrate_kbit;
+               update_vbv_settings(param);
+       }
+
+       if (ycbcr_coefficients == YCBCR_REC_709) {
+               param->vui.i_colmatrix = 1;  // BT.709.
+       } else {
+               assert(ycbcr_coefficients == YCBCR_REC_601);
+               param->vui.i_colmatrix = 6;  // BT.601/SMPTE 170M.
+       }
+}
index 7a155e341bd0e64b0f0153b8a2977058cd001bf3..687bf718679c316a3b2e6a24a41c8e449d79c776 100644 (file)
@@ -77,6 +77,9 @@ private:
        void init_x264();
        void encode_frame(QueuedFrame qf);
 
+       // bitrate_kbit can be 0 for no change.
+       static void speed_control_override_func(unsigned bitrate_kbit, movit::YCbCrLumaCoefficients coefficients, x264_param_t *param);
+
        // One big memory chunk of all 50 (or whatever) frames, allocated in
        // the constructor. All data functions just use pointers into this
        // pool.
@@ -95,8 +98,6 @@ private:
        x264_t *x264;
        std::unique_ptr<X264SpeedControl> speed_control;
 
-       std::function<void(x264_param_t *)> bitrate_override_func;
-
        std::atomic<unsigned> new_bitrate_kbit{0};  // 0 for no change.
 
        // Protects everything below it.