]> git.sesse.net Git - nageru/blobdiff - nageru/x264_encoder.cpp
Fix a Clang 19 warning.
[nageru] / nageru / x264_encoder.cpp
index 8351bdd346f3f5526f3faf49ecc9d6cd322d9ed7..014d7d17a3126d9c4bafc8d9920ca5258765b099 100644 (file)
@@ -1,16 +1,24 @@
 #include "x264_encoder.h"
 
 #include <assert.h>
+#include <atomic>
+#include <cstdint>
 #include <dlfcn.h>
+#include <functional>
+#include <inttypes.h>
+#include <math.h>
+#include <memory>
+#include <movit/image_format.h>
+#include <mutex>
+#include <pthread.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <string>
+#include <thread>
 #include <unistd.h>
+#include <vector>
 #include <x264.h>
-#include <atomic>
-#include <cstdint>
-#include <functional>
-#include <mutex>
 
 #include "defs.h"
 #include "flags.h"
@@ -22,7 +30,7 @@
 #include "x264_speed_control.h"
 
 extern "C" {
-#include <libavcodec/avcodec.h>
+#include <libavcodec/packet.h>
 #include <libavformat/avformat.h>
 }
 
@@ -60,16 +68,8 @@ void update_vbv_settings(x264_param_t *param)
        if (global_flags.x264_bitrate == -1) {
                return;
        }
-       if (global_flags.x264_vbv_buffer_size < 0) {
-               param->rc.i_vbv_buffer_size = param->rc.i_bitrate;  // One-second VBV.
-       } else {
-               param->rc.i_vbv_buffer_size = global_flags.x264_vbv_buffer_size;
-       }
-       if (global_flags.x264_vbv_max_bitrate < 0) {
-               param->rc.i_vbv_max_bitrate = param->rc.i_bitrate;  // CBR.
-       } else {
-               param->rc.i_vbv_max_bitrate = global_flags.x264_vbv_max_bitrate;
-       }
+       param->rc.i_vbv_buffer_size = param->rc.i_bitrate;  // One-second VBV.
+       param->rc.i_vbv_max_bitrate = param->rc.i_bitrate;  // CBR.
 }
 
 }  // namespace
@@ -77,7 +77,7 @@ void update_vbv_settings(x264_param_t *param)
 X264Encoder::X264Encoder(const AVOutputFormat *oformat, bool use_separate_disk_params)
        : wants_global_headers(oformat->flags & AVFMT_GLOBALHEADER),
          use_separate_disk_params(use_separate_disk_params),
-         dyn(load_x264_for_bit_depth(global_flags.x264_bit_depth))
+         dyn(load_x264_for_bit_depth(global_flags.bit_depth))
 {
        if (use_separate_disk_params) {
                call_once(x264_disk_metrics_inited, []{
@@ -107,7 +107,7 @@ X264Encoder::X264Encoder(const AVOutputFormat *oformat, bool use_separate_disk_p
                });
        }
 
-       size_t bytes_per_pixel = global_flags.x264_bit_depth > 8 ? 2 : 1;
+       size_t bytes_per_pixel = global_flags.bit_depth > 8 ? 2 : 1;
        frame_pool.reset(new uint8_t[global_flags.width * global_flags.height * 2 * bytes_per_pixel * X264_QUEUE_LENGTH]);
        for (unsigned i = 0; i < X264_QUEUE_LENGTH; ++i) {
                free_frames.push(frame_pool.get() + i * (global_flags.width * global_flags.height * 2 * bytes_per_pixel));
@@ -152,7 +152,7 @@ void X264Encoder::add_frame(int64_t pts, int64_t duration, YCbCrLumaCoefficients
                free_frames.pop();
        }
 
-       size_t bytes_per_pixel = global_flags.x264_bit_depth > 8 ? 2 : 1;
+       size_t bytes_per_pixel = global_flags.bit_depth > 8 ? 2 : 1;
        memcpy(qf.data, data, global_flags.width * global_flags.height * 2 * bytes_per_pixel);
 
        {
@@ -179,7 +179,7 @@ void X264Encoder::init_x264()
        param.i_width = global_flags.width;
        param.i_height = global_flags.height;
        param.i_csp = X264_CSP_NV12;
-       if (global_flags.x264_bit_depth > 8) {
+       if (global_flags.bit_depth > 8) {
                param.i_csp |= X264_CSP_HIGH_DEPTH;
        }
        param.b_vfr_input = 1;
@@ -190,7 +190,7 @@ void X264Encoder::init_x264()
                param.i_frame_reference = 16;  // Because speedcontrol is never allowed to change this above what we set at start.
        }
 #if X264_BUILD >= 153
-       param.i_bitdepth = global_flags.x264_bit_depth;
+       param.i_bitdepth = global_flags.bit_depth;
 #endif
 
        // NOTE: These should be in sync with the ones in quicksync_encoder.cpp (sps_rbsp()).
@@ -212,16 +212,11 @@ void X264Encoder::init_x264()
        } else {
                param.rc.i_rc_method = X264_RC_ABR;
                param.rc.i_bitrate = bitrate;
-       }
-       if (!use_separate_disk_params) {
-               update_vbv_settings(&param);
-       }
-       if (param.rc.i_vbv_max_bitrate > 0) {
-               // If the user wants VBV control to cap the max rate, it is
-               // also reasonable to assume that they are fine with the stream
-               // constantly being around that rate even for very low-complexity
-               // content; the obvious and extreme example being a static
-               // black picture.
+
+               // If the user wants to cap the max rate, it is also reasonable
+               // to assume that they are fine with the stream constantly
+               // being around that rate even for very low-complexity content;
+               // the obvious and extreme example being a static black picture.
                //
                // One would think it's fine to have low-complexity content use
                // less bitrate, but it seems to cause problems in practice;
@@ -243,6 +238,9 @@ void X264Encoder::init_x264()
                // thus ignores the parameter.)
                param.rc.b_filler = 1;
        }
+       if (!use_separate_disk_params) {
+               update_vbv_settings(&param);
+       }
 
        // Occasionally players have problem with extremely low quantizers;
        // be on the safe side. Shouldn't affect quality in any meaningful way.
@@ -265,7 +263,7 @@ void X264Encoder::init_x264()
                }
        }
 
-       if (global_flags.x264_bit_depth > 8) {
+       if (global_flags.bit_depth > 8) {
                dyn.x264_param_apply_profile(&param, "high10");
        } else {
                dyn.x264_param_apply_profile(&param, "high");
@@ -361,7 +359,7 @@ void X264Encoder::encode_frame(X264Encoder::QueuedFrame qf)
                dyn.x264_picture_init(&pic);
 
                pic.i_pts = qf.pts;
-               if (global_flags.x264_bit_depth > 8) {
+               if (global_flags.bit_depth > 8) {
                        pic.img.i_csp = X264_CSP_NV12 | X264_CSP_HIGH_DEPTH;
                        pic.img.i_plane = 2;
                        pic.img.plane[0] = qf.data;