Guard PSNR stats with rtc_video_psnr build flag
which is enabled by default when built with Chromium
Bug: webrtc:388070060
Change-Id: Ic9c05029f944a43929ea3fa3aa91bcb549f44eb6
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/403841
Reviewed-by: Erik Språng <sprang@webrtc.org>
Commit-Queue: Philipp Hancke <phancke@meta.com>
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#45342}
diff --git a/BUILD.gn b/BUILD.gn
index 3357c2d..ef07265 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -372,6 +372,10 @@
     defines += [ "WEBRTC_EXCLUDE_AUDIO_PROCESSING_MODULE" ]
   }
 
+  if (rtc_video_psnr) {
+    defines += [ "WEBRTC_ENCODER_PSNR_STATS" ]
+  }
+
   if (is_clang) {
     cflags += [
       "-Wshadow",
diff --git a/modules/video_coding/codecs/av1/libaom_av1_encoder.cc b/modules/video_coding/codecs/av1/libaom_av1_encoder.cc
index 8a49d3f..bd6f52a 100644
--- a/modules/video_coding/codecs/av1/libaom_av1_encoder.cc
+++ b/modules/video_coding/codecs/av1/libaom_av1_encoder.cc
@@ -287,7 +287,7 @@
     frame_for_encode_ = nullptr;
   }
 
-  // Flag options: AOM_CODEC_USE_PSNR and AOM_CODEC_USE_HIGHBITDEPTH
+  // Flag options: AOM_EFLAG_CALCULATE_PSNR and AOM_CODEC_USE_HIGHBITDEPTH
   aom_codec_flags_t flags = 0;
 
   // Initialize an encoder instance.
@@ -755,7 +755,7 @@
 
     aom_enc_frame_flags_t flags =
         layer_frame->IsKeyframe() ? AOM_EFLAG_FORCE_KF : 0;
-#ifdef AOM_EFLAG_CALCULATE_PSNR
+#if defined(WEBRTC_ENCODER_PSNR_STATS) && defined(AOM_EFLAG_CALCULATE_PSNR)
     if (calculate_psnr_ && psnr_frame_sampler_.ShouldBeSampled(frame)) {
       flags |= AOM_EFLAG_CALCULATE_PSNR;
     }
diff --git a/modules/video_coding/codecs/h264/h264_encoder_impl.cc b/modules/video_coding/codecs/h264/h264_encoder_impl.cc
index 42d581a..537a901 100644
--- a/modules/video_coding/codecs/h264/h264_encoder_impl.cc
+++ b/modules/video_coding/codecs/h264/h264_encoder_impl.cc
@@ -466,8 +466,11 @@
   RTC_DCHECK_EQ(configurations_[0].width, frame_buffer->width());
   RTC_DCHECK_EQ(configurations_[0].height, frame_buffer->height());
 
+#ifdef WEBRTC_ENCODER_PSNR_STATS
   bool calculate_psnr =
       calculate_psnr_ && psnr_frame_sampler_.ShouldBeSampled(input_frame);
+#endif
+
   // Encode image for each layer.
   for (size_t i = 0; i < encoders_.size(); ++i) {
     // EncodeFrame input.
@@ -476,9 +479,11 @@
     pictures_[i].iPicHeight = configurations_[i].height;
     pictures_[i].iColorFormat = EVideoFormatType::videoFormatI420;
     pictures_[i].uiTimeStamp = input_frame.ntp_time_ms();
+#ifdef WEBRTC_ENCODER_PSNR_STATS
     pictures_[i].bPsnrY = calculate_psnr;
     pictures_[i].bPsnrU = calculate_psnr;
     pictures_[i].bPsnrV = calculate_psnr;
+#endif
     // Downscale images on second and ongoing layers.
     if (i == 0) {
       pictures_[i].iStride[0] = frame_buffer->StrideY();
@@ -571,6 +576,7 @@
       h264_bitstream_parser_.ParseBitstream(encoded_images_[i]);
       encoded_images_[i].qp_ =
           h264_bitstream_parser_.GetLastSliceQp().value_or(-1);
+#ifdef WEBRTC_ENCODER_PSNR_STATS
       if (calculate_psnr) {
         encoded_images_[i].set_psnr(EncodedImage::Psnr({
             .y = info.sLayerInfo[info.iLayerNum - 1].rPsnr[0],
@@ -580,6 +586,7 @@
       } else {
         encoded_images_[i].set_psnr(std::nullopt);
       }
+#endif
 
       // Deliver encoded image.
       CodecSpecificInfo codec_specific;
diff --git a/modules/video_coding/codecs/vp8/libvpx_vp8_encoder.cc b/modules/video_coding/codecs/vp8/libvpx_vp8_encoder.cc
index 7ff38e2..c9925b1 100644
--- a/modules/video_coding/codecs/vp8/libvpx_vp8_encoder.cc
+++ b/modules/video_coding/codecs/vp8/libvpx_vp8_encoder.cc
@@ -1093,7 +1093,7 @@
     flags[i] = send_key_frame ? VPX_EFLAG_FORCE_KF : EncodeFlags(tl_configs[i]);
   }
 
-#ifdef VPX_EFLAG_CALCULATE_PSNR
+#if defined(WEBRTC_ENCODER_PSNR_STATS) && defined(VPX_EFLAG_CALCULATE_PSNR)
   if (calculate_psnr_ && psnr_frame_sampler_.ShouldBeSampled(frame)) {
     for (size_t i = 0; i < encoders_.size(); ++i) {
       flags[i] |= VPX_EFLAG_CALCULATE_PSNR;
diff --git a/modules/video_coding/codecs/vp9/libvpx_vp9_encoder.cc b/modules/video_coding/codecs/vp9/libvpx_vp9_encoder.cc
index b07e523..cecb34a 100644
--- a/modules/video_coding/codecs/vp9/libvpx_vp9_encoder.cc
+++ b/modules/video_coding/codecs/vp9/libvpx_vp9_encoder.cc
@@ -1237,7 +1237,7 @@
   if (force_key_frame_) {
     flags = VPX_EFLAG_FORCE_KF;
   }
-#ifdef VPX_EFLAG_CALCULATE_PSNR
+#if defined(WEBRTC_ENCODER_PSNR_STATS) && defined(VPX_EFLAG_CALCULATE_PSNR)
   if (calculate_psnr_ && psnr_frame_sampler_.ShouldBeSampled(input_image)) {
     flags |= VPX_EFLAG_CALCULATE_PSNR;
   }
diff --git a/webrtc.gni b/webrtc.gni
index 57aa114..3cf3cc5 100644
--- a/webrtc.gni
+++ b/webrtc.gni
@@ -323,6 +323,9 @@
 
   # Enables an experimental rust version of base64 for building and testing.
   rtc_rusty_base64 = false
+
+  # Enables PSNR calculation for video getStats.
+  rtc_video_psnr = build_with_chromium
 }
 
 declare_args() {