• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "modules/video_coding/codecs/test/videoprocessor.h"
12 
13 #include <string.h>
14 
15 #include <algorithm>
16 #include <cstddef>
17 #include <limits>
18 #include <memory>
19 #include <utility>
20 
21 #include "api/scoped_refptr.h"
22 #include "api/video/builtin_video_bitrate_allocator_factory.h"
23 #include "api/video/i420_buffer.h"
24 #include "api/video/video_bitrate_allocator_factory.h"
25 #include "api/video/video_frame_buffer.h"
26 #include "api/video/video_rotation.h"
27 #include "api/video_codecs/video_codec.h"
28 #include "api/video_codecs/video_encoder.h"
29 #include "common_video/h264/h264_common.h"
30 #include "common_video/libyuv/include/webrtc_libyuv.h"
31 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
32 #include "modules/video_coding/codecs/interface/common_constants.h"
33 #include "modules/video_coding/include/video_error_codes.h"
34 #include "rtc_base/checks.h"
35 #include "rtc_base/time_utils.h"
36 #include "test/gtest.h"
37 #include "third_party/libyuv/include/libyuv/compare.h"
38 #include "third_party/libyuv/include/libyuv/scale.h"
39 
40 namespace webrtc {
41 namespace test {
42 
43 namespace {
44 const int kMsToRtpTimestamp = kVideoPayloadTypeFrequency / 1000;
45 const int kMaxBufferedInputFrames = 20;
46 
47 const VideoEncoder::Capabilities kCapabilities(false);
48 
GetMaxNaluSizeBytes(const EncodedImage & encoded_frame,const VideoCodecTestFixture::Config & config)49 size_t GetMaxNaluSizeBytes(const EncodedImage& encoded_frame,
50                            const VideoCodecTestFixture::Config& config) {
51   if (config.codec_settings.codecType != kVideoCodecH264)
52     return 0;
53 
54   std::vector<webrtc::H264::NaluIndex> nalu_indices =
55       webrtc::H264::FindNaluIndices(encoded_frame.data(), encoded_frame.size());
56 
57   RTC_CHECK(!nalu_indices.empty());
58 
59   size_t max_size = 0;
60   for (const webrtc::H264::NaluIndex& index : nalu_indices)
61     max_size = std::max(max_size, index.payload_size);
62 
63   return max_size;
64 }
65 
GetTemporalLayerIndex(const CodecSpecificInfo & codec_specific)66 size_t GetTemporalLayerIndex(const CodecSpecificInfo& codec_specific) {
67   size_t temporal_idx = 0;
68   if (codec_specific.codecType == kVideoCodecVP8) {
69     temporal_idx = codec_specific.codecSpecific.VP8.temporalIdx;
70   } else if (codec_specific.codecType == kVideoCodecVP9) {
71     temporal_idx = codec_specific.codecSpecific.VP9.temporal_idx;
72   }
73   if (temporal_idx == kNoTemporalIdx) {
74     temporal_idx = 0;
75   }
76   return temporal_idx;
77 }
78 
GetElapsedTimeMicroseconds(int64_t start_ns,int64_t stop_ns)79 int GetElapsedTimeMicroseconds(int64_t start_ns, int64_t stop_ns) {
80   int64_t diff_us = (stop_ns - start_ns) / rtc::kNumNanosecsPerMicrosec;
81   RTC_DCHECK_GE(diff_us, std::numeric_limits<int>::min());
82   RTC_DCHECK_LE(diff_us, std::numeric_limits<int>::max());
83   return static_cast<int>(diff_us);
84 }
85 
CalculateFrameQuality(const I420BufferInterface & ref_buffer,const I420BufferInterface & dec_buffer,VideoCodecTestStats::FrameStatistics * frame_stat,bool calc_ssim)86 void CalculateFrameQuality(const I420BufferInterface& ref_buffer,
87                            const I420BufferInterface& dec_buffer,
88                            VideoCodecTestStats::FrameStatistics* frame_stat,
89                            bool calc_ssim) {
90   if (ref_buffer.width() != dec_buffer.width() ||
91       ref_buffer.height() != dec_buffer.height()) {
92     RTC_CHECK_GE(ref_buffer.width(), dec_buffer.width());
93     RTC_CHECK_GE(ref_buffer.height(), dec_buffer.height());
94     // Downscale reference frame.
95     rtc::scoped_refptr<I420Buffer> scaled_buffer =
96         I420Buffer::Create(dec_buffer.width(), dec_buffer.height());
97     I420Scale(ref_buffer.DataY(), ref_buffer.StrideY(), ref_buffer.DataU(),
98               ref_buffer.StrideU(), ref_buffer.DataV(), ref_buffer.StrideV(),
99               ref_buffer.width(), ref_buffer.height(),
100               scaled_buffer->MutableDataY(), scaled_buffer->StrideY(),
101               scaled_buffer->MutableDataU(), scaled_buffer->StrideU(),
102               scaled_buffer->MutableDataV(), scaled_buffer->StrideV(),
103               scaled_buffer->width(), scaled_buffer->height(),
104               libyuv::kFilterBox);
105 
106     CalculateFrameQuality(*scaled_buffer, dec_buffer, frame_stat, calc_ssim);
107   } else {
108     const uint64_t sse_y = libyuv::ComputeSumSquareErrorPlane(
109         dec_buffer.DataY(), dec_buffer.StrideY(), ref_buffer.DataY(),
110         ref_buffer.StrideY(), dec_buffer.width(), dec_buffer.height());
111 
112     const uint64_t sse_u = libyuv::ComputeSumSquareErrorPlane(
113         dec_buffer.DataU(), dec_buffer.StrideU(), ref_buffer.DataU(),
114         ref_buffer.StrideU(), dec_buffer.width() / 2, dec_buffer.height() / 2);
115 
116     const uint64_t sse_v = libyuv::ComputeSumSquareErrorPlane(
117         dec_buffer.DataV(), dec_buffer.StrideV(), ref_buffer.DataV(),
118         ref_buffer.StrideV(), dec_buffer.width() / 2, dec_buffer.height() / 2);
119 
120     const size_t num_y_samples = dec_buffer.width() * dec_buffer.height();
121     const size_t num_u_samples =
122         dec_buffer.width() / 2 * dec_buffer.height() / 2;
123 
124     frame_stat->psnr_y = libyuv::SumSquareErrorToPsnr(sse_y, num_y_samples);
125     frame_stat->psnr_u = libyuv::SumSquareErrorToPsnr(sse_u, num_u_samples);
126     frame_stat->psnr_v = libyuv::SumSquareErrorToPsnr(sse_v, num_u_samples);
127     frame_stat->psnr = libyuv::SumSquareErrorToPsnr(
128         sse_y + sse_u + sse_v, num_y_samples + 2 * num_u_samples);
129 
130     if (calc_ssim) {
131       frame_stat->ssim = I420SSIM(ref_buffer, dec_buffer);
132     }
133   }
134 }
135 
136 }  // namespace
137 
VideoProcessor(webrtc::VideoEncoder * encoder,VideoDecoderList * decoders,FrameReader * input_frame_reader,const VideoCodecTestFixture::Config & config,VideoCodecTestStatsImpl * stats,IvfFileWriterMap * encoded_frame_writers,FrameWriterList * decoded_frame_writers)138 VideoProcessor::VideoProcessor(webrtc::VideoEncoder* encoder,
139                                VideoDecoderList* decoders,
140                                FrameReader* input_frame_reader,
141                                const VideoCodecTestFixture::Config& config,
142                                VideoCodecTestStatsImpl* stats,
143                                IvfFileWriterMap* encoded_frame_writers,
144                                FrameWriterList* decoded_frame_writers)
145     : config_(config),
146       num_simulcast_or_spatial_layers_(
147           std::max(config_.NumberOfSimulcastStreams(),
148                    config_.NumberOfSpatialLayers())),
149       analyze_frame_quality_(!config_.measure_cpu),
150       stats_(stats),
151       encoder_(encoder),
152       decoders_(decoders),
153       bitrate_allocator_(
154           CreateBuiltinVideoBitrateAllocatorFactory()
155               ->CreateVideoBitrateAllocator(config_.codec_settings)),
156       encode_callback_(this),
157       input_frame_reader_(input_frame_reader),
158       merged_encoded_frames_(num_simulcast_or_spatial_layers_),
159       encoded_frame_writers_(encoded_frame_writers),
160       decoded_frame_writers_(decoded_frame_writers),
161       last_inputed_frame_num_(0),
162       last_inputed_timestamp_(0),
163       first_encoded_frame_(num_simulcast_or_spatial_layers_, true),
164       last_encoded_frame_num_(num_simulcast_or_spatial_layers_),
165       first_decoded_frame_(num_simulcast_or_spatial_layers_, true),
166       last_decoded_frame_num_(num_simulcast_or_spatial_layers_),
167       last_decoded_frame_buffer_(num_simulcast_or_spatial_layers_),
168       post_encode_time_ns_(0),
169       is_finalized_(false) {
170   // Sanity checks.
171   RTC_CHECK(TaskQueueBase::Current())
172       << "VideoProcessor must be run on a task queue.";
173   RTC_CHECK(stats_);
174   RTC_CHECK(encoder_);
175   RTC_CHECK(decoders_);
176   RTC_CHECK_EQ(decoders_->size(), num_simulcast_or_spatial_layers_);
177   RTC_CHECK(input_frame_reader_);
178   RTC_CHECK(encoded_frame_writers_);
179   RTC_CHECK(!decoded_frame_writers ||
180             decoded_frame_writers->size() == num_simulcast_or_spatial_layers_);
181 
182   // Setup required callbacks for the encoder and decoder and initialize them.
183   RTC_CHECK_EQ(encoder_->RegisterEncodeCompleteCallback(&encode_callback_),
184                WEBRTC_VIDEO_CODEC_OK);
185 
186   // Initialize codecs so that they are ready to receive frames.
187   RTC_CHECK_EQ(encoder_->InitEncode(
188                    &config_.codec_settings,
189                    VideoEncoder::Settings(
190                        kCapabilities, static_cast<int>(config_.NumberOfCores()),
191                        config_.max_payload_size_bytes)),
192                WEBRTC_VIDEO_CODEC_OK);
193 
194   for (size_t i = 0; i < num_simulcast_or_spatial_layers_; ++i) {
195     decode_callback_.push_back(
196         std::make_unique<VideoProcessorDecodeCompleteCallback>(this, i));
197     VideoDecoder::Settings decoder_settings;
198     decoder_settings.set_max_render_resolution(
199         {config_.codec_settings.width, config_.codec_settings.height});
200     decoder_settings.set_codec_type(config_.codec_settings.codecType);
201     decoder_settings.set_number_of_cores(config_.NumberOfCores());
202     RTC_CHECK(decoders_->at(i)->Configure(decoder_settings));
203     RTC_CHECK_EQ(decoders_->at(i)->RegisterDecodeCompleteCallback(
204                      decode_callback_.at(i).get()),
205                  WEBRTC_VIDEO_CODEC_OK);
206   }
207 }
208 
~VideoProcessor()209 VideoProcessor::~VideoProcessor() {
210   RTC_DCHECK_RUN_ON(&sequence_checker_);
211 
212   if (!is_finalized_) {
213     Finalize();
214   }
215 
216   // Explicitly reset codecs, in case they don't do that themselves when they
217   // go out of scope.
218   RTC_CHECK_EQ(encoder_->Release(), WEBRTC_VIDEO_CODEC_OK);
219   encoder_->RegisterEncodeCompleteCallback(nullptr);
220   for (auto& decoder : *decoders_) {
221     RTC_CHECK_EQ(decoder->Release(), WEBRTC_VIDEO_CODEC_OK);
222     decoder->RegisterDecodeCompleteCallback(nullptr);
223   }
224 
225   // Sanity check.
226   RTC_CHECK_LE(input_frames_.size(), kMaxBufferedInputFrames);
227 }
228 
ProcessFrame()229 void VideoProcessor::ProcessFrame() {
230   RTC_DCHECK_RUN_ON(&sequence_checker_);
231   RTC_DCHECK(!is_finalized_);
232 
233   RTC_DCHECK_GT(target_rates_.size(), 0u);
234   RTC_DCHECK_EQ(target_rates_.begin()->first, 0u);
235   RateProfile target_rate =
236       std::prev(target_rates_.upper_bound(last_inputed_frame_num_))->second;
237 
238   const size_t frame_number = last_inputed_frame_num_++;
239 
240   // Get input frame and store for future quality calculation.
241   rtc::scoped_refptr<I420BufferInterface> buffer =
242       input_frame_reader_->ReadFrame();
243   RTC_CHECK(buffer) << "Tried to read too many frames from the file.";
244   const size_t timestamp =
245       last_inputed_timestamp_ +
246       static_cast<size_t>(kVideoPayloadTypeFrequency / target_rate.input_fps);
247   VideoFrame input_frame =
248       VideoFrame::Builder()
249           .set_video_frame_buffer(buffer)
250           .set_timestamp_rtp(static_cast<uint32_t>(timestamp))
251           .set_timestamp_ms(static_cast<int64_t>(timestamp / kMsToRtpTimestamp))
252           .set_rotation(webrtc::kVideoRotation_0)
253           .build();
254   // Store input frame as a reference for quality calculations.
255   if (config_.decode && !config_.measure_cpu) {
256     if (input_frames_.size() == kMaxBufferedInputFrames) {
257       input_frames_.erase(input_frames_.begin());
258     }
259 
260     if (config_.reference_width != -1 && config_.reference_height != -1 &&
261         (input_frame.width() != config_.reference_width ||
262          input_frame.height() != config_.reference_height)) {
263       rtc::scoped_refptr<I420Buffer> scaled_buffer = I420Buffer::Create(
264           config_.codec_settings.width, config_.codec_settings.height);
265       scaled_buffer->ScaleFrom(*input_frame.video_frame_buffer()->ToI420());
266 
267       VideoFrame scaled_reference_frame = input_frame;
268       scaled_reference_frame.set_video_frame_buffer(scaled_buffer);
269       input_frames_.emplace(frame_number, scaled_reference_frame);
270 
271       if (config_.reference_width == config_.codec_settings.width &&
272           config_.reference_height == config_.codec_settings.height) {
273         // Both encoding and comparison uses the same down-scale factor, reuse
274         // it for encoder below.
275         input_frame = scaled_reference_frame;
276       }
277     } else {
278       input_frames_.emplace(frame_number, input_frame);
279     }
280   }
281   last_inputed_timestamp_ = timestamp;
282 
283   post_encode_time_ns_ = 0;
284 
285   // Create frame statistics object for all simulcast/spatial layers.
286   for (size_t i = 0; i < num_simulcast_or_spatial_layers_; ++i) {
287     FrameStatistics frame_stat(frame_number, timestamp, i);
288     stats_->AddFrame(frame_stat);
289   }
290 
291   // For the highest measurement accuracy of the encode time, the start/stop
292   // time recordings should wrap the Encode call as tightly as possible.
293   const int64_t encode_start_ns = rtc::TimeNanos();
294   for (size_t i = 0; i < num_simulcast_or_spatial_layers_; ++i) {
295     FrameStatistics* frame_stat = stats_->GetFrame(frame_number, i);
296     frame_stat->encode_start_ns = encode_start_ns;
297   }
298 
299   if (input_frame.width() != config_.codec_settings.width ||
300       input_frame.height() != config_.codec_settings.height) {
301     rtc::scoped_refptr<I420Buffer> scaled_buffer = I420Buffer::Create(
302         config_.codec_settings.width, config_.codec_settings.height);
303     scaled_buffer->ScaleFrom(*input_frame.video_frame_buffer()->ToI420());
304     input_frame.set_video_frame_buffer(scaled_buffer);
305   }
306 
307   // Encode.
308   const std::vector<VideoFrameType> frame_types =
309       (frame_number == 0)
310           ? std::vector<VideoFrameType>{VideoFrameType::kVideoFrameKey}
311           : std::vector<VideoFrameType>{VideoFrameType::kVideoFrameDelta};
312   const int encode_return_code = encoder_->Encode(input_frame, &frame_types);
313   for (size_t i = 0; i < num_simulcast_or_spatial_layers_; ++i) {
314     FrameStatistics* frame_stat = stats_->GetFrame(frame_number, i);
315     frame_stat->encode_return_code = encode_return_code;
316   }
317 }
318 
SetRates(size_t bitrate_kbps,double framerate_fps)319 void VideoProcessor::SetRates(size_t bitrate_kbps, double framerate_fps) {
320   RTC_DCHECK_RUN_ON(&sequence_checker_);
321   RTC_DCHECK(!is_finalized_);
322 
323   target_rates_[last_inputed_frame_num_] =
324       RateProfile({.target_kbps = bitrate_kbps, .input_fps = framerate_fps});
325 
326   auto bitrate_allocation =
327       bitrate_allocator_->Allocate(VideoBitrateAllocationParameters(
328           static_cast<uint32_t>(bitrate_kbps * 1000), framerate_fps));
329   encoder_->SetRates(
330       VideoEncoder::RateControlParameters(bitrate_allocation, framerate_fps));
331 }
332 
Decoded(VideoFrame & image)333 int32_t VideoProcessor::VideoProcessorDecodeCompleteCallback::Decoded(
334     VideoFrame& image) {
335   // Post the callback to the right task queue, if needed.
336   if (!task_queue_->IsCurrent()) {
337     // There might be a limited amount of output buffers, make a copy to make
338     // sure we don't block the decoder.
339     VideoFrame copy = VideoFrame::Builder()
340                           .set_video_frame_buffer(I420Buffer::Copy(
341                               *image.video_frame_buffer()->ToI420()))
342                           .set_rotation(image.rotation())
343                           .set_timestamp_us(image.timestamp_us())
344                           .set_id(image.id())
345                           .build();
346     copy.set_timestamp(image.timestamp());
347 
348     task_queue_->PostTask([this, copy]() {
349       video_processor_->FrameDecoded(copy, simulcast_svc_idx_);
350     });
351     return 0;
352   }
353   video_processor_->FrameDecoded(image, simulcast_svc_idx_);
354   return 0;
355 }
356 
FrameEncoded(const webrtc::EncodedImage & encoded_image,const webrtc::CodecSpecificInfo & codec_specific)357 void VideoProcessor::FrameEncoded(
358     const webrtc::EncodedImage& encoded_image,
359     const webrtc::CodecSpecificInfo& codec_specific) {
360   RTC_DCHECK_RUN_ON(&sequence_checker_);
361 
362   // For the highest measurement accuracy of the encode time, the start/stop
363   // time recordings should wrap the Encode call as tightly as possible.
364   const int64_t encode_stop_ns = rtc::TimeNanos();
365 
366   const VideoCodecType codec_type = codec_specific.codecType;
367   if (config_.encoded_frame_checker) {
368     config_.encoded_frame_checker->CheckEncodedFrame(codec_type, encoded_image);
369   }
370 
371   // Layer metadata.
372   size_t spatial_idx = encoded_image.SpatialIndex().value_or(0);
373   size_t temporal_idx = GetTemporalLayerIndex(codec_specific);
374 
375   FrameStatistics* frame_stat =
376       stats_->GetFrameWithTimestamp(encoded_image.Timestamp(), spatial_idx);
377   const size_t frame_number = frame_stat->frame_number;
378 
379   // Ensure that the encode order is monotonically increasing, within this
380   // simulcast/spatial layer.
381   RTC_CHECK(first_encoded_frame_[spatial_idx] ||
382             last_encoded_frame_num_[spatial_idx] < frame_number);
383 
384   // Ensure SVC spatial layers are delivered in ascending order.
385   const size_t num_spatial_layers = config_.NumberOfSpatialLayers();
386   if (!first_encoded_frame_[spatial_idx] && num_spatial_layers > 1) {
387     for (size_t i = 0; i < spatial_idx; ++i) {
388       RTC_CHECK_LE(last_encoded_frame_num_[i], frame_number);
389     }
390     for (size_t i = spatial_idx + 1; i < num_simulcast_or_spatial_layers_;
391          ++i) {
392       RTC_CHECK_GT(frame_number, last_encoded_frame_num_[i]);
393     }
394   }
395   first_encoded_frame_[spatial_idx] = false;
396   last_encoded_frame_num_[spatial_idx] = frame_number;
397 
398   RateProfile target_rate =
399       std::prev(target_rates_.upper_bound(frame_number))->second;
400   auto bitrate_allocation =
401       bitrate_allocator_->Allocate(VideoBitrateAllocationParameters(
402           static_cast<uint32_t>(target_rate.target_kbps * 1000),
403           target_rate.input_fps));
404 
405   // Update frame statistics.
406   frame_stat->encoding_successful = true;
407   frame_stat->encode_time_us = GetElapsedTimeMicroseconds(
408       frame_stat->encode_start_ns, encode_stop_ns - post_encode_time_ns_);
409   frame_stat->target_bitrate_kbps =
410       bitrate_allocation.GetTemporalLayerSum(spatial_idx, temporal_idx) / 1000;
411   frame_stat->target_framerate_fps = target_rate.input_fps;
412   frame_stat->length_bytes = encoded_image.size();
413   frame_stat->frame_type = encoded_image._frameType;
414   frame_stat->temporal_idx = temporal_idx;
415   frame_stat->max_nalu_size_bytes = GetMaxNaluSizeBytes(encoded_image, config_);
416   frame_stat->qp = encoded_image.qp_;
417 
418   if (codec_type == kVideoCodecVP9) {
419     const CodecSpecificInfoVP9& vp9_info = codec_specific.codecSpecific.VP9;
420     frame_stat->inter_layer_predicted = vp9_info.inter_layer_predicted;
421     frame_stat->non_ref_for_inter_layer_pred =
422         vp9_info.non_ref_for_inter_layer_pred;
423   } else {
424     frame_stat->inter_layer_predicted = false;
425     frame_stat->non_ref_for_inter_layer_pred = true;
426   }
427 
428   const webrtc::EncodedImage* encoded_image_for_decode = &encoded_image;
429   if (config_.decode || !encoded_frame_writers_->empty()) {
430     if (num_spatial_layers > 1) {
431       encoded_image_for_decode = BuildAndStoreSuperframe(
432           encoded_image, codec_type, frame_number, spatial_idx,
433           frame_stat->inter_layer_predicted);
434     }
435   }
436 
437   if (config_.decode) {
438     DecodeFrame(*encoded_image_for_decode, spatial_idx);
439 
440     if (codec_specific.end_of_picture && num_spatial_layers > 1) {
441       // If inter-layer prediction is enabled and upper layer was dropped then
442       // base layer should be passed to upper layer decoder. Otherwise decoder
443       // won't be able to decode next superframe.
444       const EncodedImage* base_image = nullptr;
445       const FrameStatistics* base_stat = nullptr;
446       for (size_t i = 0; i < num_spatial_layers; ++i) {
447         const bool layer_dropped = (first_decoded_frame_[i] ||
448                                     last_decoded_frame_num_[i] < frame_number);
449 
450         // Ensure current layer was decoded.
451         RTC_CHECK(layer_dropped == false || i != spatial_idx);
452 
453         if (!layer_dropped) {
454           base_image = &merged_encoded_frames_[i];
455           base_stat =
456               stats_->GetFrameWithTimestamp(encoded_image.Timestamp(), i);
457         } else if (base_image && !base_stat->non_ref_for_inter_layer_pred) {
458           DecodeFrame(*base_image, i);
459         }
460       }
461     }
462   } else {
463     frame_stat->decode_return_code = WEBRTC_VIDEO_CODEC_NO_OUTPUT;
464   }
465 
466   // Since frames in higher TLs typically depend on frames in lower TLs,
467   // write out frames in lower TLs to bitstream dumps of higher TLs.
468   for (size_t write_temporal_idx = temporal_idx;
469        write_temporal_idx < config_.NumberOfTemporalLayers();
470        ++write_temporal_idx) {
471     const VideoProcessor::LayerKey layer_key(spatial_idx, write_temporal_idx);
472     auto it = encoded_frame_writers_->find(layer_key);
473     if (it != encoded_frame_writers_->cend()) {
474       RTC_CHECK(it->second->WriteFrame(*encoded_image_for_decode,
475                                        config_.codec_settings.codecType));
476     }
477   }
478 
479   if (!config_.encode_in_real_time) {
480     // To get pure encode time for next layers, measure time spent in encode
481     // callback and subtract it from encode time of next layers.
482     post_encode_time_ns_ += rtc::TimeNanos() - encode_stop_ns;
483   }
484 }
485 
CalcFrameQuality(const I420BufferInterface & decoded_frame,FrameStatistics * frame_stat)486 void VideoProcessor::CalcFrameQuality(const I420BufferInterface& decoded_frame,
487                                       FrameStatistics* frame_stat) {
488   RTC_DCHECK_RUN_ON(&sequence_checker_);
489 
490   const auto reference_frame = input_frames_.find(frame_stat->frame_number);
491   RTC_CHECK(reference_frame != input_frames_.cend())
492       << "The codecs are either buffering too much, dropping too much, or "
493          "being too slow relative to the input frame rate.";
494 
495   // SSIM calculation is not optimized. Skip it in real-time mode.
496   const bool calc_ssim = !config_.encode_in_real_time;
497   CalculateFrameQuality(*reference_frame->second.video_frame_buffer()->ToI420(),
498                         decoded_frame, frame_stat, calc_ssim);
499 
500   frame_stat->quality_analysis_successful = true;
501 }
502 
WriteDecodedFrame(const I420BufferInterface & decoded_frame,FrameWriter & frame_writer)503 void VideoProcessor::WriteDecodedFrame(const I420BufferInterface& decoded_frame,
504                                        FrameWriter& frame_writer) {
505   int input_video_width = config_.codec_settings.width;
506   int input_video_height = config_.codec_settings.height;
507 
508   rtc::scoped_refptr<I420Buffer> scaled_buffer;
509   const I420BufferInterface* scaled_frame;
510 
511   if (decoded_frame.width() == input_video_width &&
512       decoded_frame.height() == input_video_height) {
513     scaled_frame = &decoded_frame;
514   } else {
515     EXPECT_DOUBLE_EQ(
516         static_cast<double>(input_video_width) / input_video_height,
517         static_cast<double>(decoded_frame.width()) / decoded_frame.height());
518 
519     scaled_buffer = I420Buffer::Create(input_video_width, input_video_height);
520     scaled_buffer->ScaleFrom(decoded_frame);
521 
522     scaled_frame = scaled_buffer.get();
523   }
524 
525   // Ensure there is no padding.
526   RTC_CHECK_EQ(scaled_frame->StrideY(), input_video_width);
527   RTC_CHECK_EQ(scaled_frame->StrideU(), input_video_width / 2);
528   RTC_CHECK_EQ(scaled_frame->StrideV(), input_video_width / 2);
529 
530   RTC_CHECK_EQ(3 * input_video_width * input_video_height / 2,
531                frame_writer.FrameLength());
532 
533   RTC_CHECK(frame_writer.WriteFrame(scaled_frame->DataY()));
534 }
535 
FrameDecoded(const VideoFrame & decoded_frame,size_t spatial_idx)536 void VideoProcessor::FrameDecoded(const VideoFrame& decoded_frame,
537                                   size_t spatial_idx) {
538   RTC_DCHECK_RUN_ON(&sequence_checker_);
539 
540   // For the highest measurement accuracy of the decode time, the start/stop
541   // time recordings should wrap the Decode call as tightly as possible.
542   const int64_t decode_stop_ns = rtc::TimeNanos();
543 
544   FrameStatistics* frame_stat =
545       stats_->GetFrameWithTimestamp(decoded_frame.timestamp(), spatial_idx);
546   const size_t frame_number = frame_stat->frame_number;
547 
548   if (!first_decoded_frame_[spatial_idx]) {
549     for (size_t dropped_frame_number = last_decoded_frame_num_[spatial_idx] + 1;
550          dropped_frame_number < frame_number; ++dropped_frame_number) {
551       FrameStatistics* dropped_frame_stat =
552           stats_->GetFrame(dropped_frame_number, spatial_idx);
553 
554       if (analyze_frame_quality_ && config_.analyze_quality_of_dropped_frames) {
555         // Calculate frame quality comparing input frame with last decoded one.
556         CalcFrameQuality(*last_decoded_frame_buffer_[spatial_idx],
557                          dropped_frame_stat);
558       }
559 
560       if (decoded_frame_writers_ != nullptr) {
561         // Fill drops with last decoded frame to make them look like freeze at
562         // playback and to keep decoded layers in sync.
563         WriteDecodedFrame(*last_decoded_frame_buffer_[spatial_idx],
564                           *decoded_frame_writers_->at(spatial_idx));
565       }
566     }
567   }
568 
569   // Ensure that the decode order is monotonically increasing, within this
570   // simulcast/spatial layer.
571   RTC_CHECK(first_decoded_frame_[spatial_idx] ||
572             last_decoded_frame_num_[spatial_idx] < frame_number);
573   first_decoded_frame_[spatial_idx] = false;
574   last_decoded_frame_num_[spatial_idx] = frame_number;
575 
576   // Update frame statistics.
577   frame_stat->decoding_successful = true;
578   frame_stat->decode_time_us =
579       GetElapsedTimeMicroseconds(frame_stat->decode_start_ns, decode_stop_ns);
580   frame_stat->decoded_width = decoded_frame.width();
581   frame_stat->decoded_height = decoded_frame.height();
582 
583   // Skip quality metrics calculation to not affect CPU usage.
584   if (analyze_frame_quality_ || decoded_frame_writers_) {
585     // Save last decoded frame to handle possible future drops.
586     rtc::scoped_refptr<I420BufferInterface> i420buffer =
587         decoded_frame.video_frame_buffer()->ToI420();
588 
589     // Copy decoded frame to a buffer without padding/stride such that we can
590     // dump Y, U and V planes into a file in one shot.
591     last_decoded_frame_buffer_[spatial_idx] = I420Buffer::Copy(
592         i420buffer->width(), i420buffer->height(), i420buffer->DataY(),
593         i420buffer->StrideY(), i420buffer->DataU(), i420buffer->StrideU(),
594         i420buffer->DataV(), i420buffer->StrideV());
595   }
596 
597   if (analyze_frame_quality_) {
598     CalcFrameQuality(*decoded_frame.video_frame_buffer()->ToI420(), frame_stat);
599   }
600 
601   if (decoded_frame_writers_ != nullptr) {
602     WriteDecodedFrame(*last_decoded_frame_buffer_[spatial_idx],
603                       *decoded_frame_writers_->at(spatial_idx));
604   }
605 
606   // Erase all buffered input frames that we have moved past for all
607   // simulcast/spatial layers. Never buffer more than
608   // `kMaxBufferedInputFrames` frames, to protect against long runs of
609   // consecutive frame drops for a particular layer.
610   const auto min_last_decoded_frame_num = std::min_element(
611       last_decoded_frame_num_.cbegin(), last_decoded_frame_num_.cend());
612   const size_t min_buffered_frame_num =
613       std::max(0, static_cast<int>(frame_number) - kMaxBufferedInputFrames + 1);
614   RTC_CHECK(min_last_decoded_frame_num != last_decoded_frame_num_.cend());
615   const auto input_frames_erase_before = input_frames_.lower_bound(
616       std::max(*min_last_decoded_frame_num, min_buffered_frame_num));
617   input_frames_.erase(input_frames_.cbegin(), input_frames_erase_before);
618 }
619 
DecodeFrame(const EncodedImage & encoded_image,size_t spatial_idx)620 void VideoProcessor::DecodeFrame(const EncodedImage& encoded_image,
621                                  size_t spatial_idx) {
622   RTC_DCHECK_RUN_ON(&sequence_checker_);
623   FrameStatistics* frame_stat =
624       stats_->GetFrameWithTimestamp(encoded_image.Timestamp(), spatial_idx);
625 
626   frame_stat->decode_start_ns = rtc::TimeNanos();
627   frame_stat->decode_return_code =
628       decoders_->at(spatial_idx)->Decode(encoded_image, false, 0);
629 }
630 
BuildAndStoreSuperframe(const EncodedImage & encoded_image,const VideoCodecType codec,size_t frame_number,size_t spatial_idx,bool inter_layer_predicted)631 const webrtc::EncodedImage* VideoProcessor::BuildAndStoreSuperframe(
632     const EncodedImage& encoded_image,
633     const VideoCodecType codec,
634     size_t frame_number,
635     size_t spatial_idx,
636     bool inter_layer_predicted) {
637   // Should only be called for SVC.
638   RTC_CHECK_GT(config_.NumberOfSpatialLayers(), 1);
639 
640   EncodedImage base_image;
641   RTC_CHECK_EQ(base_image.size(), 0);
642 
643   // Each SVC layer is decoded with dedicated decoder. Find the nearest
644   // non-dropped base frame and merge it and current frame into superframe.
645   if (inter_layer_predicted) {
646     for (int base_idx = static_cast<int>(spatial_idx) - 1; base_idx >= 0;
647          --base_idx) {
648       EncodedImage lower_layer = merged_encoded_frames_.at(base_idx);
649       if (lower_layer.Timestamp() == encoded_image.Timestamp()) {
650         base_image = lower_layer;
651         break;
652       }
653     }
654   }
655   const size_t payload_size_bytes = base_image.size() + encoded_image.size();
656 
657   auto buffer = EncodedImageBuffer::Create(payload_size_bytes);
658   if (base_image.size()) {
659     RTC_CHECK(base_image.data());
660     memcpy(buffer->data(), base_image.data(), base_image.size());
661   }
662   memcpy(buffer->data() + base_image.size(), encoded_image.data(),
663          encoded_image.size());
664 
665   EncodedImage copied_image = encoded_image;
666   copied_image.SetEncodedData(buffer);
667   if (base_image.size())
668     copied_image._frameType = base_image._frameType;
669 
670   // Replace previous EncodedImage for this spatial layer.
671   merged_encoded_frames_.at(spatial_idx) = std::move(copied_image);
672 
673   return &merged_encoded_frames_.at(spatial_idx);
674 }
675 
Finalize()676 void VideoProcessor::Finalize() {
677   RTC_DCHECK_RUN_ON(&sequence_checker_);
678   RTC_DCHECK(!is_finalized_);
679   is_finalized_ = true;
680 
681   if (!(analyze_frame_quality_ && config_.analyze_quality_of_dropped_frames) &&
682       decoded_frame_writers_ == nullptr) {
683     return;
684   }
685 
686   for (size_t spatial_idx = 0; spatial_idx < num_simulcast_or_spatial_layers_;
687        ++spatial_idx) {
688     if (first_decoded_frame_[spatial_idx]) {
689       continue;  // No decoded frames on this spatial layer.
690     }
691 
692     for (size_t dropped_frame_number = last_decoded_frame_num_[spatial_idx] + 1;
693          dropped_frame_number < last_inputed_frame_num_;
694          ++dropped_frame_number) {
695       FrameStatistics* frame_stat =
696           stats_->GetFrame(dropped_frame_number, spatial_idx);
697 
698       RTC_DCHECK(!frame_stat->decoding_successful);
699 
700       if (analyze_frame_quality_ && config_.analyze_quality_of_dropped_frames) {
701         CalcFrameQuality(*last_decoded_frame_buffer_[spatial_idx], frame_stat);
702       }
703 
704       if (decoded_frame_writers_ != nullptr) {
705         WriteDecodedFrame(*last_decoded_frame_buffer_[spatial_idx],
706                           *decoded_frame_writers_->at(spatial_idx));
707       }
708     }
709   }
710 }
711 
712 }  // namespace test
713 }  // namespace webrtc
714