]> git.sesse.net Git - nageru/blobdiff - nageru/av1_encoder.cpp
Fix a Clang 19 warning.
[nageru] / nageru / av1_encoder.cpp
index 568cd5f9acc3673c425f393fd145416b6e8782e8..bb6c17be145f71e04913fd23811c217f31c90738 100644 (file)
@@ -63,7 +63,7 @@ AV1Encoder::AV1Encoder(const AVOutputFormat *oformat)
                        av1_latency_histogram.init("av1");
                });
 
-       const size_t bytes_per_pixel = global_flags.av1_bit_depth > 8 ? 2 : 1;
+       const 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 * AV1_QUEUE_LENGTH]);
        for (unsigned i = 0; i < AV1_QUEUE_LENGTH; ++i) {
                free_frames.push(frame_pool.get() + i * (global_flags.width * global_flags.height * 2 * bytes_per_pixel));
@@ -104,7 +104,7 @@ void AV1Encoder::add_frame(int64_t pts, int64_t duration, YCbCrLumaCoefficients
        // SVT-AV1 makes its own copy, though, and it would have been nice to avoid the
        // double-copy (and also perhaps let the GPU do the 10-bit compression SVT-AV1
        // wants, instead of doing it on the CPU).
-       const size_t bytes_per_pixel = global_flags.av1_bit_depth > 8 ? 2 : 1;
+       const size_t bytes_per_pixel = global_flags.bit_depth > 8 ? 2 : 1;
        size_t frame_size = global_flags.width * global_flags.height * bytes_per_pixel;
        assert(global_flags.width % 2 == 0);
        assert(global_flags.height % 2 == 0);
@@ -112,7 +112,7 @@ void AV1Encoder::add_frame(int64_t pts, int64_t duration, YCbCrLumaCoefficients
        uint8_t *cb = y + frame_size;
        uint8_t *cr = cb + frame_size / 4;
        memcpy(y, data, frame_size);
-       if (global_flags.av1_bit_depth == 8) {
+       if (global_flags.bit_depth == 8) {
                memcpy_interleaved(cb, cr, data + frame_size, frame_size / 2);
        } else {
                const uint16_t *src = reinterpret_cast<const uint16_t *>(data + frame_size);
@@ -132,21 +132,30 @@ void AV1Encoder::add_frame(int64_t pts, int64_t duration, YCbCrLumaCoefficients
 void AV1Encoder::init_av1()
 {
        EbSvtAv1EncConfiguration config;
+
+       // svt_av1_enc_init_handle() is defined to fill config with the defaults;
+       // yet, seemingly, not everything is written, and some of it can cause
+       // Valgrind warnings and/or crashes. It should never hurt to put it
+       // into a known state beforehand, and it seems to fix the crashes,
+       // so we do that.
+       memset(&config, 0, sizeof(config));
+
        EbErrorType ret = svt_av1_enc_init_handle(&encoder, nullptr, &config);
        if (ret != EB_ErrorNone) {
                fprintf(stderr, "Error initializing SVT-AV1 handle (error %08x)\n", ret);
                exit(EXIT_FAILURE);
        }
 
+       // NOTE: We don't set CBR, as it requires low-delay mode, which is
+       // generally problematic wrt. quality and performance.
        config.enc_mode = global_flags.av1_preset;
        config.intra_period_length = 63;  // Approx. one second, conforms to the (n % 8) - 1 == 0 rule.
        config.source_width = global_flags.width;
        config.source_height = global_flags.height;
        config.frame_rate_numerator = global_flags.av1_fps_num;
        config.frame_rate_denominator = global_flags.av1_fps_den;
-       config.encoder_bit_depth = global_flags.av1_bit_depth;
-       config.rate_control_mode = 2;  // CBR.
-       config.pred_structure = 1;  // PRED_LOW_DELAY_B (needed for CBR).
+       config.encoder_bit_depth = global_flags.bit_depth;
+       config.rate_control_mode = 1;  // VBR.
        config.target_bit_rate = global_flags.av1_bitrate * 1000;
 
        // NOTE: These should be in sync with the ones in quicksync_encoder.cpp (sps_rbsp()).
@@ -158,9 +167,7 @@ void AV1Encoder::init_av1()
                config.matrix_coefficients = EB_CICP_MC_BT_601;
        }
        config.color_range = EB_CR_STUDIO_RANGE;
-#if SVT_AV1_CHECK_VERSION(1, 0, 0)
        config.chroma_sample_position = EB_CSP_VERTICAL;
-#endif
 
        const vector<string> &extra_param = global_flags.av1_extra_param;
        for (const string &str : extra_param) {
@@ -205,7 +212,7 @@ void AV1Encoder::init_av1()
                global_headers = string(reinterpret_cast<const char *>(header->p_buffer), header->n_filled_len);
 
                svt_av1_enc_stream_header_release(header);  // Don't care about errors.
-          }
+       }
 }
 
 void AV1Encoder::encoder_thread_func()
@@ -281,7 +288,7 @@ void AV1Encoder::encoder_thread_func()
 void AV1Encoder::encode_frame(AV1Encoder::QueuedFrame qf)
 {
        if (qf.data) {
-               const size_t bytes_per_pixel = global_flags.av1_bit_depth > 8 ? 2 : 1;
+               const size_t bytes_per_pixel = global_flags.bit_depth > 8 ? 2 : 1;
 
                EbSvtIOFormat pic;
                pic.luma = qf.data;     
@@ -292,17 +299,17 @@ void AV1Encoder::encode_frame(AV1Encoder::QueuedFrame qf)
                pic.cr_stride = global_flags.width / 2;  // Likewise.
                pic.width = global_flags.width;
                pic.height = global_flags.height;
-               pic.origin_x = 0;
-               pic.origin_y = 0;
+               pic.org_x = 0;
+               pic.org_y = 0;
                pic.color_fmt = EB_YUV420;
-               pic.bit_depth = global_flags.av1_bit_depth > 8 ? EB_TEN_BIT : EB_EIGHT_BIT;
+               pic.bit_depth = global_flags.bit_depth > 8 ? EB_TEN_BIT : EB_EIGHT_BIT;
 
                EbBufferHeaderType hdr;
                hdr.p_buffer      = reinterpret_cast<uint8_t *>(&pic);
                hdr.n_alloc_len   = (global_flags.width * global_flags.height * 3 / 2) * bytes_per_pixel;
                hdr.n_filled_len  = hdr.n_alloc_len;
                hdr.n_tick_count  = 0;
-               hdr.p_app_private = reinterpret_cast<void *>(intptr_t(qf.duration));
+               hdr.p_app_private = nullptr;
                hdr.pic_type      = EB_AV1_INVALID_PICTURE;  // Actually means auto, according to FFmpeg.
                hdr.metadata      = nullptr;
                hdr.flags         = 0;