blob: f7d1fc9832b849170907c8486138e62d8c44c07a [file] [log] [blame]
kjellander@webrtc.org35a17562011-10-06 06:44:54 +00001/*
pwestin@webrtc.orgce330352012-04-12 06:59:14 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
kjellander@webrtc.org35a17562011-10-06 06:44:54 +00003 *
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 */
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000010
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/video_coding/codecs/test/videoprocessor.h"
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000012
ssilkin612f8582017-09-28 09:23:17 -070013#include <algorithm>
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000014#include <limits>
Erik Språng08127a92016-11-16 16:41:30 +010015#include <utility>
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/video/i420_buffer.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020018#include "common_types.h" // NOLINT(build/include)
ssilkin612f8582017-09-28 09:23:17 -070019#include "common_video/h264/h264_common.h"
Sergey Silkin3be2a552018-01-17 15:11:44 +010020#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/video_coding/codecs/vp8/simulcast_rate_allocator.h"
22#include "modules/video_coding/include/video_codec_initializer.h"
23#include "modules/video_coding/utility/default_video_bitrate_allocator.h"
24#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "rtc_base/timeutils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "test/gtest.h"
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000027
28namespace webrtc {
29namespace test {
30
brandtrb78bc752017-02-22 01:26:59 -080031namespace {
brandtr17b958c2017-03-07 01:41:43 -080032
brandtraebc61e2017-02-28 07:13:47 -080033std::unique_ptr<VideoBitrateAllocator> CreateBitrateAllocator(
brandtr07734a52017-08-08 08:35:53 -070034 TestConfig* config) {
brandtraebc61e2017-02-28 07:13:47 -080035 std::unique_ptr<TemporalLayersFactory> tl_factory;
brandtr07734a52017-08-08 08:35:53 -070036 if (config->codec_settings.codecType == VideoCodecType::kVideoCodecVP8) {
brandtraebc61e2017-02-28 07:13:47 -080037 tl_factory.reset(new TemporalLayersFactory());
brandtr07734a52017-08-08 08:35:53 -070038 config->codec_settings.VP8()->tl_factory = tl_factory.get();
brandtraebc61e2017-02-28 07:13:47 -080039 }
40 return std::unique_ptr<VideoBitrateAllocator>(
brandtr07734a52017-08-08 08:35:53 -070041 VideoCodecInitializer::CreateBitrateAllocator(config->codec_settings,
brandtraebc61e2017-02-28 07:13:47 -080042 std::move(tl_factory)));
43}
44
Sergey Silkin3be2a552018-01-17 15:11:44 +010045size_t GetMaxNaluSizeBytes(const EncodedImage& encoded_frame,
46 const TestConfig& config) {
ssilkin612f8582017-09-28 09:23:17 -070047 if (config.codec_settings.codecType != kVideoCodecH264)
Sergey Silkin3be2a552018-01-17 15:11:44 +010048 return 0;
ssilkin612f8582017-09-28 09:23:17 -070049
50 std::vector<webrtc::H264::NaluIndex> nalu_indices =
51 webrtc::H264::FindNaluIndices(encoded_frame._buffer,
52 encoded_frame._length);
53
54 RTC_CHECK(!nalu_indices.empty());
55
Sergey Silkin3be2a552018-01-17 15:11:44 +010056 size_t max_size = 0;
ssilkin612f8582017-09-28 09:23:17 -070057 for (const webrtc::H264::NaluIndex& index : nalu_indices)
Sergey Silkin3be2a552018-01-17 15:11:44 +010058 max_size = std::max(max_size, index.payload_size);
ssilkin612f8582017-09-28 09:23:17 -070059
Sergey Silkin3be2a552018-01-17 15:11:44 +010060 return max_size;
ssilkin612f8582017-09-28 09:23:17 -070061}
62
asaperssonae9ba042017-03-07 00:25:38 -080063int GetElapsedTimeMicroseconds(int64_t start_ns, int64_t stop_ns) {
64 int64_t diff_us = (stop_ns - start_ns) / rtc::kNumNanosecsPerMicrosec;
65 RTC_DCHECK_GE(diff_us, std::numeric_limits<int>::min());
66 RTC_DCHECK_LE(diff_us, std::numeric_limits<int>::max());
67 return static_cast<int>(diff_us);
68}
69
Åsa Perssonf0c44672017-10-24 16:03:39 +020070void ExtractBufferWithSize(const VideoFrame& image,
71 int width,
72 int height,
73 rtc::Buffer* buffer) {
74 if (image.width() != width || image.height() != height) {
75 EXPECT_DOUBLE_EQ(static_cast<double>(width) / height,
76 static_cast<double>(image.width()) / image.height());
77 // Same aspect ratio, no cropping needed.
78 rtc::scoped_refptr<I420Buffer> scaled(I420Buffer::Create(width, height));
79 scaled->ScaleFrom(*image.video_frame_buffer()->ToI420());
80
81 size_t length =
82 CalcBufferSize(VideoType::kI420, scaled->width(), scaled->height());
83 buffer->SetSize(length);
84 RTC_CHECK_NE(ExtractBuffer(scaled, length, buffer->data()), -1);
85 return;
86 }
87
88 // No resize.
89 size_t length =
90 CalcBufferSize(VideoType::kI420, image.width(), image.height());
91 buffer->SetSize(length);
92 RTC_CHECK_NE(ExtractBuffer(image, length, buffer->data()), -1);
93}
94
brandtrb78bc752017-02-22 01:26:59 -080095} // namespace
96
brandtrc4095522017-08-07 08:12:33 -070097VideoProcessor::VideoProcessor(webrtc::VideoEncoder* encoder,
98 webrtc::VideoDecoder* decoder,
99 FrameReader* analysis_frame_reader,
brandtrc4095522017-08-07 08:12:33 -0700100 PacketManipulator* packet_manipulator,
101 const TestConfig& config,
102 Stats* stats,
brandtrc4095522017-08-07 08:12:33 -0700103 IvfFileWriter* encoded_frame_writer,
104 FrameWriter* decoded_frame_writer)
Åsa Perssonf0c44672017-10-24 16:03:39 +0200105 : config_(config),
brandtr07734a52017-08-08 08:35:53 -0700106 encoder_(encoder),
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000107 decoder_(decoder),
brandtr07734a52017-08-08 08:35:53 -0700108 bitrate_allocator_(CreateBitrateAllocator(&config_)),
brandtrbdd555c2017-08-21 01:34:04 -0700109 encode_callback_(this),
110 decode_callback_(this),
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000111 packet_manipulator_(packet_manipulator),
brandtraebc61e2017-02-28 07:13:47 -0800112 analysis_frame_reader_(analysis_frame_reader),
brandtrb78bc752017-02-22 01:26:59 -0800113 encoded_frame_writer_(encoded_frame_writer),
114 decoded_frame_writer_(decoded_frame_writer),
Sergey Silkin3be2a552018-01-17 15:11:44 +0100115 last_inputed_frame_num_(0),
116 last_encoded_frame_num_(0),
117 last_decoded_frame_num_(0),
118 num_encoded_frames_(0),
119 num_decoded_frames_(0),
brandtr17b958c2017-03-07 01:41:43 -0800120 first_key_frame_has_been_excluded_(false),
brandtrbdd555c2017-08-21 01:34:04 -0700121 last_decoded_frame_buffer_(analysis_frame_reader->FrameLength()),
Sergey Silkin3be2a552018-01-17 15:11:44 +0100122 stats_(stats) {
Erik Språng08127a92016-11-16 16:41:30 +0100123 RTC_DCHECK(encoder);
124 RTC_DCHECK(decoder);
brandtraebc61e2017-02-28 07:13:47 -0800125 RTC_DCHECK(packet_manipulator);
brandtrb78bc752017-02-22 01:26:59 -0800126 RTC_DCHECK(analysis_frame_reader);
Erik Språng08127a92016-11-16 16:41:30 +0100127 RTC_DCHECK(stats);
brandtr17b958c2017-03-07 01:41:43 -0800128
brandtrbea36fd2017-08-07 03:36:54 -0700129 // Setup required callbacks for the encoder and decoder.
brandtrbdd555c2017-08-21 01:34:04 -0700130 RTC_CHECK_EQ(encoder_->RegisterEncodeCompleteCallback(&encode_callback_),
Åsa Perssonf0c44672017-10-24 16:03:39 +0200131 WEBRTC_VIDEO_CODEC_OK);
brandtrbdd555c2017-08-21 01:34:04 -0700132 RTC_CHECK_EQ(decoder_->RegisterDecodeCompleteCallback(&decode_callback_),
Åsa Perssonf0c44672017-10-24 16:03:39 +0200133 WEBRTC_VIDEO_CODEC_OK);
asapersson654d54c2017-02-10 00:16:07 -0800134
brandtraebc61e2017-02-28 07:13:47 -0800135 // Initialize the encoder and decoder.
asapersson654d54c2017-02-10 00:16:07 -0800136 RTC_CHECK_EQ(
Sergey Silkin3be2a552018-01-17 15:11:44 +0100137 encoder_->InitEncode(&config_.codec_settings,
138 static_cast<int>(config_.NumberOfCores()),
asapersson654d54c2017-02-10 00:16:07 -0800139 config_.networking_config.max_payload_size_in_bytes),
Åsa Perssonf0c44672017-10-24 16:03:39 +0200140 WEBRTC_VIDEO_CODEC_OK);
Sergey Silkin3be2a552018-01-17 15:11:44 +0100141 RTC_CHECK_EQ(decoder_->InitDecode(&config_.codec_settings,
142 static_cast<int>(config_.NumberOfCores())),
143 WEBRTC_VIDEO_CODEC_OK);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000144}
145
Åsa Perssonf0c44672017-10-24 16:03:39 +0200146VideoProcessor::~VideoProcessor() {
brandtrc8c59052017-08-21 06:44:16 -0700147 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
148
brandtr77920a42017-08-11 07:48:15 -0700149 RTC_CHECK_EQ(encoder_->Release(), WEBRTC_VIDEO_CODEC_OK);
150 RTC_CHECK_EQ(decoder_->Release(), WEBRTC_VIDEO_CODEC_OK);
151
brandtrbdd555c2017-08-21 01:34:04 -0700152 encoder_->RegisterEncodeCompleteCallback(nullptr);
153 decoder_->RegisterDecodeCompleteCallback(nullptr);
brandtr77920a42017-08-11 07:48:15 -0700154}
155
brandtr8935d972017-09-06 01:53:22 -0700156void VideoProcessor::ProcessFrame() {
brandtrc8c59052017-08-21 06:44:16 -0700157 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
Sergey Silkin3be2a552018-01-17 15:11:44 +0100158 const size_t frame_number = last_inputed_frame_num_++;
asapersson654d54c2017-02-10 00:16:07 -0800159
brandtrbdd555c2017-08-21 01:34:04 -0700160 // Get frame from file.
magjed3f075492017-06-01 10:02:26 -0700161 rtc::scoped_refptr<I420BufferInterface> buffer(
brandtrb78bc752017-02-22 01:26:59 -0800162 analysis_frame_reader_->ReadFrame());
brandtrbdd555c2017-08-21 01:34:04 -0700163 RTC_CHECK(buffer) << "Tried to read too many frames from the file.";
brandtrb57f4262017-08-30 06:29:51 -0700164 // Use the frame number as the basis for timestamp to identify frames. Let the
165 // first timestamp be non-zero, to not make the IvfFileWriter believe that we
166 // want to use capture timestamps in the IVF files.
Sergey Silkin3be2a552018-01-17 15:11:44 +0100167 const size_t rtp_timestamp = (frame_number + 1) * kVideoPayloadTypeFrequency /
168 config_.codec_settings.maxFramerate;
Sami Kalliomäki20b294c2017-12-12 16:37:16 +0100169 const int64_t render_time_ms = (frame_number + 1) * rtc::kNumMillisecsPerSec /
170 config_.codec_settings.maxFramerate;
Sergey Silkin64eaa992017-11-17 14:47:32 +0100171 rtp_timestamp_to_frame_num_[rtp_timestamp] = frame_number;
Sergey Silkin3be2a552018-01-17 15:11:44 +0100172 input_frames_[frame_number] =
173 rtc::MakeUnique<VideoFrame>(buffer, static_cast<uint32_t>(rtp_timestamp),
174 render_time_ms, webrtc::kVideoRotation_0);
brandtr17b958c2017-03-07 01:41:43 -0800175
Sergey Silkin64eaa992017-11-17 14:47:32 +0100176 std::vector<FrameType> frame_types = config_.FrameTypeForFrame(frame_number);
brandtr17b958c2017-03-07 01:41:43 -0800177
178 // Create frame statistics object used for aggregation at end of test run.
brandtr8935d972017-09-06 01:53:22 -0700179 FrameStatistic* frame_stat = stats_->AddFrame();
Sergey Silkin3be2a552018-01-17 15:11:44 +0100180 frame_stat->rtp_timestamp = rtp_timestamp;
brandtr17b958c2017-03-07 01:41:43 -0800181
182 // For the highest measurement accuracy of the encode time, the start/stop
183 // time recordings should wrap the Encode call as tightly as possible.
brandtr8935d972017-09-06 01:53:22 -0700184 frame_stat->encode_start_ns = rtc::TimeNanos();
brandtr17b958c2017-03-07 01:41:43 -0800185 frame_stat->encode_return_code =
Sergey Silkin64eaa992017-11-17 14:47:32 +0100186 encoder_->Encode(*input_frames_[frame_number], nullptr, &frame_types);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000187}
188
Sergey Silkin3be2a552018-01-17 15:11:44 +0100189void VideoProcessor::SetRates(size_t bitrate_kbps, size_t framerate_fps) {
brandtrc8c59052017-08-21 06:44:16 -0700190 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
Sergey Silkin3be2a552018-01-17 15:11:44 +0100191 config_.codec_settings.maxFramerate = static_cast<uint32_t>(framerate_fps);
192 bitrate_allocation_ = bitrate_allocator_->GetAllocation(
193 static_cast<uint32_t>(bitrate_kbps * 1000),
194 static_cast<uint32_t>(framerate_fps));
195 const int set_rates_result = encoder_->SetRateAllocation(
196 bitrate_allocation_, static_cast<uint32_t>(framerate_fps));
brandtrbea36fd2017-08-07 03:36:54 -0700197 RTC_DCHECK_GE(set_rates_result, 0)
brandtrbdd555c2017-08-21 01:34:04 -0700198 << "Failed to update encoder with new rate " << bitrate_kbps << ".";
brandtrbea36fd2017-08-07 03:36:54 -0700199}
200
brandtr45535622017-08-22 03:33:11 -0700201void VideoProcessor::FrameEncoded(webrtc::VideoCodecType codec,
202 const EncodedImage& encoded_image) {
brandtrc8c59052017-08-21 06:44:16 -0700203 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
204
brandtr32e0d262017-02-15 05:29:38 -0800205 // For the highest measurement accuracy of the encode time, the start/stop
206 // time recordings should wrap the Encode call as tightly as possible.
207 int64_t encode_stop_ns = rtc::TimeNanos();
208
Rasmus Brandtf7a35582017-10-24 10:16:33 +0200209 if (config_.encoded_frame_checker) {
210 config_.encoded_frame_checker->CheckEncodedFrame(codec, encoded_image);
211 }
brandtrb78bc752017-02-22 01:26:59 -0800212
Sergey Silkin3be2a552018-01-17 15:11:44 +0100213 const size_t frame_number =
brandtrb57f4262017-08-30 06:29:51 -0700214 rtp_timestamp_to_frame_num_[encoded_image._timeStamp];
Sergey Silkin64eaa992017-11-17 14:47:32 +0100215
216 // Ensure strict monotonicity.
Sergey Silkin3be2a552018-01-17 15:11:44 +0100217 if (num_encoded_frames_ > 0) {
218 RTC_CHECK_GT(frame_number, last_encoded_frame_num_);
219 }
Sergey Silkin64eaa992017-11-17 14:47:32 +0100220
221 // Check for dropped frames.
brandtr17b958c2017-03-07 01:41:43 -0800222 bool last_frame_missing = false;
223 if (frame_number > 0) {
brandtr8935d972017-09-06 01:53:22 -0700224 const FrameStatistic* last_encoded_frame_stat =
225 stats_->GetFrame(last_encoded_frame_num_);
226 last_frame_missing = (last_encoded_frame_stat->manipulated_length == 0);
brandtr17b958c2017-03-07 01:41:43 -0800227 }
brandtr17b958c2017-03-07 01:41:43 -0800228 last_encoded_frame_num_ = frame_number;
229
brandtr8935d972017-09-06 01:53:22 -0700230 // Update frame statistics.
231 FrameStatistic* frame_stat = stats_->GetFrame(frame_number);
232 frame_stat->encode_time_us =
233 GetElapsedTimeMicroseconds(frame_stat->encode_start_ns, encode_stop_ns);
brandtr17b958c2017-03-07 01:41:43 -0800234 frame_stat->encoding_successful = true;
brandtr8935d972017-09-06 01:53:22 -0700235 frame_stat->encoded_frame_size_bytes = encoded_image._length;
brandtr17b958c2017-03-07 01:41:43 -0800236 frame_stat->frame_type = encoded_image._frameType;
Sergey Silkin3be2a552018-01-17 15:11:44 +0100237 frame_stat->temporal_layer_idx = config_.TemporalLayerForFrame(frame_number);
brandtr17b958c2017-03-07 01:41:43 -0800238 frame_stat->qp = encoded_image.qp_;
Sergey Silkin3be2a552018-01-17 15:11:44 +0100239 frame_stat->target_bitrate_kbps =
240 bitrate_allocation_.GetSpatialLayerSum(0) / 1000;
brandtr17b958c2017-03-07 01:41:43 -0800241 frame_stat->total_packets =
philipelcce46fc2015-12-21 03:04:49 -0800242 encoded_image._length / config_.networking_config.packet_size_in_bytes +
243 1;
Sergey Silkin3be2a552018-01-17 15:11:44 +0100244 frame_stat->max_nalu_size_bytes = GetMaxNaluSizeBytes(encoded_image, config_);
ssilkin612f8582017-09-28 09:23:17 -0700245
Åsa Perssonf0c44672017-10-24 16:03:39 +0200246 // Make a raw copy of |encoded_image| to feed to the decoder.
hbos3fe2c6a2016-01-22 00:07:12 -0800247 size_t copied_buffer_size = encoded_image._length +
248 EncodedImage::GetBufferPaddingBytes(codec);
kwiberg3f55dea2016-02-29 05:51:59 -0800249 std::unique_ptr<uint8_t[]> copied_buffer(new uint8_t[copied_buffer_size]);
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000250 memcpy(copied_buffer.get(), encoded_image._buffer, encoded_image._length);
Åsa Perssonf0c44672017-10-24 16:03:39 +0200251 EncodedImage copied_image = encoded_image;
hbos3fe2c6a2016-01-22 00:07:12 -0800252 copied_image._size = copied_buffer_size;
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000253 copied_image._buffer = copied_buffer.get();
hbosbab934b2016-01-27 01:36:03 -0800254
Åsa Perssonf0c44672017-10-24 16:03:39 +0200255 // Simulate packet loss.
256 if (!ExcludeFrame(copied_image)) {
brandtr17b958c2017-03-07 01:41:43 -0800257 frame_stat->packets_dropped =
philipelcce46fc2015-12-21 03:04:49 -0800258 packet_manipulator_->ManipulatePackets(&copied_image);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000259 }
brandtr8935d972017-09-06 01:53:22 -0700260 frame_stat->manipulated_length = copied_image._length;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000261
brandtr32e0d262017-02-15 05:29:38 -0800262 // For the highest measurement accuracy of the decode time, the start/stop
263 // time recordings should wrap the Decode call as tightly as possible.
brandtr8935d972017-09-06 01:53:22 -0700264 frame_stat->decode_start_ns = rtc::TimeNanos();
brandtr17b958c2017-03-07 01:41:43 -0800265 frame_stat->decode_return_code =
266 decoder_->Decode(copied_image, last_frame_missing, nullptr);
brandtr8bc93852017-02-15 05:19:51 -0800267
brandtr8935d972017-09-06 01:53:22 -0700268 if (encoded_frame_writer_) {
269 RTC_CHECK(encoded_frame_writer_->WriteFrame(encoded_image, codec));
270 }
Sergey Silkin3be2a552018-01-17 15:11:44 +0100271
272 ++num_encoded_frames_;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000273}
274
Sergey Silkin64eaa992017-11-17 14:47:32 +0100275void VideoProcessor::FrameDecoded(const VideoFrame& decoded_frame) {
brandtrc8c59052017-08-21 06:44:16 -0700276 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
277
brandtr32e0d262017-02-15 05:29:38 -0800278 // For the highest measurement accuracy of the decode time, the start/stop
279 // time recordings should wrap the Decode call as tightly as possible.
Niels Möllerd28db7f2016-05-10 16:31:47 +0200280 int64_t decode_stop_ns = rtc::TimeNanos();
brandtr8bc93852017-02-15 05:19:51 -0800281
brandtr8935d972017-09-06 01:53:22 -0700282 // Update frame statistics.
Sergey Silkin3be2a552018-01-17 15:11:44 +0100283 const size_t frame_number =
Sergey Silkin64eaa992017-11-17 14:47:32 +0100284 rtp_timestamp_to_frame_num_[decoded_frame.timestamp()];
brandtr8935d972017-09-06 01:53:22 -0700285 FrameStatistic* frame_stat = stats_->GetFrame(frame_number);
Sergey Silkin64eaa992017-11-17 14:47:32 +0100286 frame_stat->decoded_width = decoded_frame.width();
287 frame_stat->decoded_height = decoded_frame.height();
brandtr8935d972017-09-06 01:53:22 -0700288 frame_stat->decode_time_us =
289 GetElapsedTimeMicroseconds(frame_stat->decode_start_ns, decode_stop_ns);
brandtr17b958c2017-03-07 01:41:43 -0800290 frame_stat->decoding_successful = true;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000291
Sergey Silkin64eaa992017-11-17 14:47:32 +0100292 // Ensure strict monotonicity.
Sergey Silkin3be2a552018-01-17 15:11:44 +0100293 if (num_decoded_frames_ > 0) {
294 RTC_CHECK_GT(frame_number, last_decoded_frame_num_);
295 }
Sergey Silkin64eaa992017-11-17 14:47:32 +0100296
brandtr17b958c2017-03-07 01:41:43 -0800297 // Check if the codecs have resized the frame since previously decoded frame.
298 if (frame_number > 0) {
Sergey Silkin3be2a552018-01-17 15:11:44 +0100299 if (decoded_frame_writer_ && num_decoded_frames_ > 0) {
Sergey Silkin64eaa992017-11-17 14:47:32 +0100300 // For dropped/lost frames, write out the last decoded frame to make it
301 // look like a freeze at playback.
Sergey Silkin3be2a552018-01-17 15:11:44 +0100302 const size_t num_dropped_frames =
303 frame_number - last_decoded_frame_num_ - 1;
304 for (size_t i = 0; i < num_dropped_frames; i++) {
Sergey Silkin64eaa992017-11-17 14:47:32 +0100305 WriteDecodedFrameToFile(&last_decoded_frame_buffer_);
306 }
307 }
marpan@webrtc.orgf4c2de92012-06-05 21:07:28 +0000308 }
brandtr17b958c2017-03-07 01:41:43 -0800309 last_decoded_frame_num_ = frame_number;
310
Sergey Silkin64eaa992017-11-17 14:47:32 +0100311 // Skip quality metrics calculation to not affect CPU usage.
312 if (!config_.measure_cpu) {
313 frame_stat->psnr =
314 I420PSNR(input_frames_[frame_number].get(), &decoded_frame);
315 frame_stat->ssim =
316 I420SSIM(input_frames_[frame_number].get(), &decoded_frame);
317 }
Niels Möller718a7632016-06-13 13:06:01 +0200318
Sergey Silkin64eaa992017-11-17 14:47:32 +0100319 // Delay erasing of input frames by one frame. The current frame might
320 // still be needed for other simulcast stream or spatial layer.
Sergey Silkin3be2a552018-01-17 15:11:44 +0100321 if (frame_number > 0) {
322 auto input_frame_erase_to = input_frames_.lower_bound(frame_number - 1);
Sergey Silkin64eaa992017-11-17 14:47:32 +0100323 input_frames_.erase(input_frames_.begin(), input_frame_erase_to);
324 }
325
326 if (decoded_frame_writer_) {
327 ExtractBufferWithSize(decoded_frame, config_.codec_settings.width,
328 config_.codec_settings.height,
329 &last_decoded_frame_buffer_);
330 WriteDecodedFrameToFile(&last_decoded_frame_buffer_);
331 }
Sergey Silkin3be2a552018-01-17 15:11:44 +0100332
333 ++num_decoded_frames_;
Åsa Perssonf0c44672017-10-24 16:03:39 +0200334}
brandtr17b958c2017-03-07 01:41:43 -0800335
Åsa Perssonf0c44672017-10-24 16:03:39 +0200336void VideoProcessor::WriteDecodedFrameToFile(rtc::Buffer* buffer) {
Sergey Silkin64eaa992017-11-17 14:47:32 +0100337 RTC_DCHECK_EQ(buffer->size(), decoded_frame_writer_->FrameLength());
338 RTC_CHECK(decoded_frame_writer_->WriteFrame(buffer->data()));
Åsa Perssonf0c44672017-10-24 16:03:39 +0200339}
brandtr17b958c2017-03-07 01:41:43 -0800340
Åsa Perssonf0c44672017-10-24 16:03:39 +0200341bool VideoProcessor::ExcludeFrame(const EncodedImage& encoded_image) {
342 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
343 if (encoded_image._frameType != kVideoFrameKey) {
344 return false;
345 }
346 bool exclude_frame = false;
347 switch (config_.exclude_frame_types) {
348 case kExcludeOnlyFirstKeyFrame:
349 if (!first_key_frame_has_been_excluded_) {
350 first_key_frame_has_been_excluded_ = true;
351 exclude_frame = true;
352 }
353 break;
354 case kExcludeAllKeyFrames:
355 exclude_frame = true;
356 break;
357 default:
358 RTC_NOTREACHED();
359 }
360 return exclude_frame;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000361}
362
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000363} // namespace test
364} // namespace webrtc