blob: 20cb0cffbdcac2c876d4f32849a0b34f1d60d4c8 [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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/video_coding/codecs/vp8/simulcast_rate_allocator.h"
21#include "modules/video_coding/include/video_codec_initializer.h"
22#include "modules/video_coding/utility/default_video_bitrate_allocator.h"
23#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/timeutils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "test/gtest.h"
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000026
27namespace webrtc {
28namespace test {
29
brandtrb78bc752017-02-22 01:26:59 -080030namespace {
brandtr17b958c2017-03-07 01:41:43 -080031
brandtrbea36fd2017-08-07 03:36:54 -070032const int kRtpClockRateHz = 90000;
Åsa Perssone87cfe22017-10-26 15:33:18 +020033const int64_t kNoRenderTime = 0;
brandtr17b958c2017-03-07 01:41:43 -080034
brandtraebc61e2017-02-28 07:13:47 -080035std::unique_ptr<VideoBitrateAllocator> CreateBitrateAllocator(
brandtr07734a52017-08-08 08:35:53 -070036 TestConfig* config) {
brandtraebc61e2017-02-28 07:13:47 -080037 std::unique_ptr<TemporalLayersFactory> tl_factory;
brandtr07734a52017-08-08 08:35:53 -070038 if (config->codec_settings.codecType == VideoCodecType::kVideoCodecVP8) {
brandtraebc61e2017-02-28 07:13:47 -080039 tl_factory.reset(new TemporalLayersFactory());
brandtr07734a52017-08-08 08:35:53 -070040 config->codec_settings.VP8()->tl_factory = tl_factory.get();
brandtraebc61e2017-02-28 07:13:47 -080041 }
42 return std::unique_ptr<VideoBitrateAllocator>(
brandtr07734a52017-08-08 08:35:53 -070043 VideoCodecInitializer::CreateBitrateAllocator(config->codec_settings,
brandtraebc61e2017-02-28 07:13:47 -080044 std::move(tl_factory)));
45}
46
ssilkin612f8582017-09-28 09:23:17 -070047rtc::Optional<size_t> GetMaxNaluLength(const EncodedImage& encoded_frame,
48 const TestConfig& config) {
49 if (config.codec_settings.codecType != kVideoCodecH264)
Oskar Sundbom6bd39022017-11-16 10:54:49 +010050 return rtc::nullopt;
ssilkin612f8582017-09-28 09:23:17 -070051
52 std::vector<webrtc::H264::NaluIndex> nalu_indices =
53 webrtc::H264::FindNaluIndices(encoded_frame._buffer,
54 encoded_frame._length);
55
56 RTC_CHECK(!nalu_indices.empty());
57
58 size_t max_length = 0;
59 for (const webrtc::H264::NaluIndex& index : nalu_indices)
60 max_length = std::max(max_length, index.payload_size);
61
Oskar Sundbom6bd39022017-11-16 10:54:49 +010062 return max_length;
ssilkin612f8582017-09-28 09:23:17 -070063}
64
asaperssonae9ba042017-03-07 00:25:38 -080065int GetElapsedTimeMicroseconds(int64_t start_ns, int64_t stop_ns) {
66 int64_t diff_us = (stop_ns - start_ns) / rtc::kNumNanosecsPerMicrosec;
67 RTC_DCHECK_GE(diff_us, std::numeric_limits<int>::min());
68 RTC_DCHECK_LE(diff_us, std::numeric_limits<int>::max());
69 return static_cast<int>(diff_us);
70}
71
Åsa Perssonf0c44672017-10-24 16:03:39 +020072void ExtractBufferWithSize(const VideoFrame& image,
73 int width,
74 int height,
75 rtc::Buffer* buffer) {
76 if (image.width() != width || image.height() != height) {
77 EXPECT_DOUBLE_EQ(static_cast<double>(width) / height,
78 static_cast<double>(image.width()) / image.height());
79 // Same aspect ratio, no cropping needed.
80 rtc::scoped_refptr<I420Buffer> scaled(I420Buffer::Create(width, height));
81 scaled->ScaleFrom(*image.video_frame_buffer()->ToI420());
82
83 size_t length =
84 CalcBufferSize(VideoType::kI420, scaled->width(), scaled->height());
85 buffer->SetSize(length);
86 RTC_CHECK_NE(ExtractBuffer(scaled, length, buffer->data()), -1);
87 return;
88 }
89
90 // No resize.
91 size_t length =
92 CalcBufferSize(VideoType::kI420, image.width(), image.height());
93 buffer->SetSize(length);
94 RTC_CHECK_NE(ExtractBuffer(image, length, buffer->data()), -1);
95}
96
brandtrb78bc752017-02-22 01:26:59 -080097} // namespace
98
brandtrc4095522017-08-07 08:12:33 -070099VideoProcessor::VideoProcessor(webrtc::VideoEncoder* encoder,
100 webrtc::VideoDecoder* decoder,
101 FrameReader* analysis_frame_reader,
brandtrc4095522017-08-07 08:12:33 -0700102 PacketManipulator* packet_manipulator,
103 const TestConfig& config,
104 Stats* stats,
brandtrc4095522017-08-07 08:12:33 -0700105 IvfFileWriter* encoded_frame_writer,
106 FrameWriter* decoded_frame_writer)
Åsa Perssonf0c44672017-10-24 16:03:39 +0200107 : config_(config),
brandtr07734a52017-08-08 08:35:53 -0700108 encoder_(encoder),
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000109 decoder_(decoder),
brandtr07734a52017-08-08 08:35:53 -0700110 bitrate_allocator_(CreateBitrateAllocator(&config_)),
brandtrbdd555c2017-08-21 01:34:04 -0700111 encode_callback_(this),
112 decode_callback_(this),
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000113 packet_manipulator_(packet_manipulator),
brandtraebc61e2017-02-28 07:13:47 -0800114 analysis_frame_reader_(analysis_frame_reader),
brandtrb78bc752017-02-22 01:26:59 -0800115 encoded_frame_writer_(encoded_frame_writer),
116 decoded_frame_writer_(decoded_frame_writer),
brandtr8935d972017-09-06 01:53:22 -0700117 last_inputed_frame_num_(-1),
brandtr17b958c2017-03-07 01:41:43 -0800118 last_encoded_frame_num_(-1),
119 last_decoded_frame_num_(-1),
120 first_key_frame_has_been_excluded_(false),
brandtrbdd555c2017-08-21 01:34:04 -0700121 last_decoded_frame_buffer_(analysis_frame_reader->FrameLength()),
brandtraebc61e2017-02-28 07:13:47 -0800122 stats_(stats),
brandtrb57f4262017-08-30 06:29:51 -0700123 rate_update_index_(-1) {
Erik Språng08127a92016-11-16 16:41:30 +0100124 RTC_DCHECK(encoder);
125 RTC_DCHECK(decoder);
brandtraebc61e2017-02-28 07:13:47 -0800126 RTC_DCHECK(packet_manipulator);
brandtrb78bc752017-02-22 01:26:59 -0800127 RTC_DCHECK(analysis_frame_reader);
Erik Språng08127a92016-11-16 16:41:30 +0100128 RTC_DCHECK(stats);
brandtr17b958c2017-03-07 01:41:43 -0800129
brandtrbea36fd2017-08-07 03:36:54 -0700130 // Setup required callbacks for the encoder and decoder.
brandtrbdd555c2017-08-21 01:34:04 -0700131 RTC_CHECK_EQ(encoder_->RegisterEncodeCompleteCallback(&encode_callback_),
Åsa Perssonf0c44672017-10-24 16:03:39 +0200132 WEBRTC_VIDEO_CODEC_OK);
brandtrbdd555c2017-08-21 01:34:04 -0700133 RTC_CHECK_EQ(decoder_->RegisterDecodeCompleteCallback(&decode_callback_),
Åsa Perssonf0c44672017-10-24 16:03:39 +0200134 WEBRTC_VIDEO_CODEC_OK);
asapersson654d54c2017-02-10 00:16:07 -0800135
brandtraebc61e2017-02-28 07:13:47 -0800136 // Initialize the encoder and decoder.
asapersson654d54c2017-02-10 00:16:07 -0800137 RTC_CHECK_EQ(
Åsa Persson2d27fb52017-10-19 14:05:50 +0200138 encoder_->InitEncode(&config_.codec_settings, 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);
Åsa Persson2d27fb52017-10-19 14:05:50 +0200141 RTC_CHECK_EQ(
142 decoder_->InitDecode(&config_.codec_settings, config_.NumberOfCores()),
Åsa Perssonf0c44672017-10-24 16:03:39 +0200143 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 Silkin64eaa992017-11-17 14:47:32 +0100158 const int 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 Silkin64eaa992017-11-17 14:47:32 +0100167 const uint32_t rtp_timestamp = (frame_number + 1) * kRtpClockRateHz /
brandtrb57f4262017-08-30 06:29:51 -0700168 config_.codec_settings.maxFramerate;
Sergey Silkin64eaa992017-11-17 14:47:32 +0100169 rtp_timestamp_to_frame_num_[rtp_timestamp] = frame_number;
170 input_frames_[frame_number] = rtc::MakeUnique<VideoFrame>(
171 buffer, rtp_timestamp, kNoRenderTime, webrtc::kVideoRotation_0);
brandtr17b958c2017-03-07 01:41:43 -0800172
Sergey Silkin64eaa992017-11-17 14:47:32 +0100173 std::vector<FrameType> frame_types = config_.FrameTypeForFrame(frame_number);
brandtr17b958c2017-03-07 01:41:43 -0800174
175 // Create frame statistics object used for aggregation at end of test run.
brandtr8935d972017-09-06 01:53:22 -0700176 FrameStatistic* frame_stat = stats_->AddFrame();
brandtr17b958c2017-03-07 01:41:43 -0800177
178 // For the highest measurement accuracy of the encode time, the start/stop
179 // time recordings should wrap the Encode call as tightly as possible.
brandtr8935d972017-09-06 01:53:22 -0700180 frame_stat->encode_start_ns = rtc::TimeNanos();
brandtr17b958c2017-03-07 01:41:43 -0800181 frame_stat->encode_return_code =
Sergey Silkin64eaa992017-11-17 14:47:32 +0100182 encoder_->Encode(*input_frames_[frame_number], nullptr, &frame_types);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000183}
184
brandtrbdd555c2017-08-21 01:34:04 -0700185void VideoProcessor::SetRates(int bitrate_kbps, int framerate_fps) {
brandtrc8c59052017-08-21 06:44:16 -0700186 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
brandtrbdd555c2017-08-21 01:34:04 -0700187 config_.codec_settings.maxFramerate = framerate_fps;
brandtrbea36fd2017-08-07 03:36:54 -0700188 int set_rates_result = encoder_->SetRateAllocation(
brandtrbdd555c2017-08-21 01:34:04 -0700189 bitrate_allocator_->GetAllocation(bitrate_kbps * 1000, framerate_fps),
190 framerate_fps);
brandtrbea36fd2017-08-07 03:36:54 -0700191 RTC_DCHECK_GE(set_rates_result, 0)
brandtrbdd555c2017-08-21 01:34:04 -0700192 << "Failed to update encoder with new rate " << bitrate_kbps << ".";
brandtrb57f4262017-08-30 06:29:51 -0700193 ++rate_update_index_;
194 num_dropped_frames_.push_back(0);
195 num_spatial_resizes_.push_back(0);
brandtrbea36fd2017-08-07 03:36:54 -0700196}
197
brandtrb57f4262017-08-30 06:29:51 -0700198std::vector<int> VideoProcessor::NumberDroppedFramesPerRateUpdate() const {
brandtrc8c59052017-08-21 06:44:16 -0700199 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
brandtrbea36fd2017-08-07 03:36:54 -0700200 return num_dropped_frames_;
201}
202
brandtrb57f4262017-08-30 06:29:51 -0700203std::vector<int> VideoProcessor::NumberSpatialResizesPerRateUpdate() const {
brandtrc8c59052017-08-21 06:44:16 -0700204 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
brandtrbea36fd2017-08-07 03:36:54 -0700205 return num_spatial_resizes_;
206}
207
brandtr45535622017-08-22 03:33:11 -0700208void VideoProcessor::FrameEncoded(webrtc::VideoCodecType codec,
209 const EncodedImage& encoded_image) {
brandtrc8c59052017-08-21 06:44:16 -0700210 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
211
brandtr32e0d262017-02-15 05:29:38 -0800212 // For the highest measurement accuracy of the encode time, the start/stop
213 // time recordings should wrap the Encode call as tightly as possible.
214 int64_t encode_stop_ns = rtc::TimeNanos();
215
Rasmus Brandtf7a35582017-10-24 10:16:33 +0200216 if (config_.encoded_frame_checker) {
217 config_.encoded_frame_checker->CheckEncodedFrame(codec, encoded_image);
218 }
brandtrb78bc752017-02-22 01:26:59 -0800219
brandtrb57f4262017-08-30 06:29:51 -0700220 const int frame_number =
221 rtp_timestamp_to_frame_num_[encoded_image._timeStamp];
Sergey Silkin64eaa992017-11-17 14:47:32 +0100222
223 // Ensure strict monotonicity.
224 RTC_CHECK_GT(frame_number, last_encoded_frame_num_);
225
226 // Check for dropped frames.
brandtr17b958c2017-03-07 01:41:43 -0800227 bool last_frame_missing = false;
228 if (frame_number > 0) {
brandtr17b958c2017-03-07 01:41:43 -0800229 int num_dropped_from_last_encode =
230 frame_number - last_encoded_frame_num_ - 1;
231 RTC_DCHECK_GE(num_dropped_from_last_encode, 0);
brandtrb57f4262017-08-30 06:29:51 -0700232 RTC_CHECK_GE(rate_update_index_, 0);
233 num_dropped_frames_[rate_update_index_] += num_dropped_from_last_encode;
brandtr8935d972017-09-06 01:53:22 -0700234 const FrameStatistic* last_encoded_frame_stat =
235 stats_->GetFrame(last_encoded_frame_num_);
236 last_frame_missing = (last_encoded_frame_stat->manipulated_length == 0);
brandtr17b958c2017-03-07 01:41:43 -0800237 }
brandtr17b958c2017-03-07 01:41:43 -0800238 last_encoded_frame_num_ = frame_number;
239
brandtr8935d972017-09-06 01:53:22 -0700240 // Update frame statistics.
241 FrameStatistic* frame_stat = stats_->GetFrame(frame_number);
242 frame_stat->encode_time_us =
243 GetElapsedTimeMicroseconds(frame_stat->encode_start_ns, encode_stop_ns);
brandtr17b958c2017-03-07 01:41:43 -0800244 frame_stat->encoding_successful = true;
brandtr8935d972017-09-06 01:53:22 -0700245 frame_stat->encoded_frame_size_bytes = encoded_image._length;
brandtr17b958c2017-03-07 01:41:43 -0800246 frame_stat->frame_type = encoded_image._frameType;
247 frame_stat->qp = encoded_image.qp_;
brandtr8935d972017-09-06 01:53:22 -0700248 frame_stat->bitrate_kbps = static_cast<int>(
brandtr07734a52017-08-08 08:35:53 -0700249 encoded_image._length * config_.codec_settings.maxFramerate * 8 / 1000);
brandtr17b958c2017-03-07 01:41:43 -0800250 frame_stat->total_packets =
philipelcce46fc2015-12-21 03:04:49 -0800251 encoded_image._length / config_.networking_config.packet_size_in_bytes +
252 1;
ssilkin612f8582017-09-28 09:23:17 -0700253 frame_stat->max_nalu_length = GetMaxNaluLength(encoded_image, config_);
254
Åsa Perssonf0c44672017-10-24 16:03:39 +0200255 // Make a raw copy of |encoded_image| to feed to the decoder.
hbos3fe2c6a2016-01-22 00:07:12 -0800256 size_t copied_buffer_size = encoded_image._length +
257 EncodedImage::GetBufferPaddingBytes(codec);
kwiberg3f55dea2016-02-29 05:51:59 -0800258 std::unique_ptr<uint8_t[]> copied_buffer(new uint8_t[copied_buffer_size]);
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000259 memcpy(copied_buffer.get(), encoded_image._buffer, encoded_image._length);
Åsa Perssonf0c44672017-10-24 16:03:39 +0200260 EncodedImage copied_image = encoded_image;
hbos3fe2c6a2016-01-22 00:07:12 -0800261 copied_image._size = copied_buffer_size;
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000262 copied_image._buffer = copied_buffer.get();
hbosbab934b2016-01-27 01:36:03 -0800263
Åsa Perssonf0c44672017-10-24 16:03:39 +0200264 // Simulate packet loss.
265 if (!ExcludeFrame(copied_image)) {
brandtr17b958c2017-03-07 01:41:43 -0800266 frame_stat->packets_dropped =
philipelcce46fc2015-12-21 03:04:49 -0800267 packet_manipulator_->ManipulatePackets(&copied_image);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000268 }
brandtr8935d972017-09-06 01:53:22 -0700269 frame_stat->manipulated_length = copied_image._length;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000270
brandtr32e0d262017-02-15 05:29:38 -0800271 // For the highest measurement accuracy of the decode time, the start/stop
272 // time recordings should wrap the Decode call as tightly as possible.
brandtr8935d972017-09-06 01:53:22 -0700273 frame_stat->decode_start_ns = rtc::TimeNanos();
brandtr17b958c2017-03-07 01:41:43 -0800274 frame_stat->decode_return_code =
275 decoder_->Decode(copied_image, last_frame_missing, nullptr);
brandtr8bc93852017-02-15 05:19:51 -0800276
brandtr8935d972017-09-06 01:53:22 -0700277 if (encoded_frame_writer_) {
278 RTC_CHECK(encoded_frame_writer_->WriteFrame(encoded_image, codec));
279 }
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000280}
281
Sergey Silkin64eaa992017-11-17 14:47:32 +0100282void VideoProcessor::FrameDecoded(const VideoFrame& decoded_frame) {
brandtrc8c59052017-08-21 06:44:16 -0700283 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
284
brandtr32e0d262017-02-15 05:29:38 -0800285 // For the highest measurement accuracy of the decode time, the start/stop
286 // time recordings should wrap the Decode call as tightly as possible.
Niels Möllerd28db7f2016-05-10 16:31:47 +0200287 int64_t decode_stop_ns = rtc::TimeNanos();
brandtr8bc93852017-02-15 05:19:51 -0800288
brandtr8935d972017-09-06 01:53:22 -0700289 // Update frame statistics.
Sergey Silkin64eaa992017-11-17 14:47:32 +0100290 const int frame_number =
291 rtp_timestamp_to_frame_num_[decoded_frame.timestamp()];
brandtr8935d972017-09-06 01:53:22 -0700292 FrameStatistic* frame_stat = stats_->GetFrame(frame_number);
Sergey Silkin64eaa992017-11-17 14:47:32 +0100293 frame_stat->decoded_width = decoded_frame.width();
294 frame_stat->decoded_height = decoded_frame.height();
brandtr8935d972017-09-06 01:53:22 -0700295 frame_stat->decode_time_us =
296 GetElapsedTimeMicroseconds(frame_stat->decode_start_ns, decode_stop_ns);
brandtr17b958c2017-03-07 01:41:43 -0800297 frame_stat->decoding_successful = true;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000298
Sergey Silkin64eaa992017-11-17 14:47:32 +0100299 // Ensure strict monotonicity.
300 RTC_CHECK_GT(frame_number, last_decoded_frame_num_);
301
brandtr17b958c2017-03-07 01:41:43 -0800302 // Check if the codecs have resized the frame since previously decoded frame.
303 if (frame_number > 0) {
Sergey Silkin64eaa992017-11-17 14:47:32 +0100304 if (decoded_frame_writer_ && last_decoded_frame_num_ >= 0) {
305 // For dropped/lost frames, write out the last decoded frame to make it
306 // look like a freeze at playback.
307 const int num_dropped_frames = frame_number - last_decoded_frame_num_;
308 for (int i = 0; i < num_dropped_frames; i++) {
309 WriteDecodedFrameToFile(&last_decoded_frame_buffer_);
310 }
311 }
312 // TODO(ssilkin): move to FrameEncoded when webm:1474 is implemented.
brandtr8935d972017-09-06 01:53:22 -0700313 const FrameStatistic* last_decoded_frame_stat =
314 stats_->GetFrame(last_decoded_frame_num_);
Sergey Silkin64eaa992017-11-17 14:47:32 +0100315 if (decoded_frame.width() != last_decoded_frame_stat->decoded_width ||
316 decoded_frame.height() != last_decoded_frame_stat->decoded_height) {
brandtrb57f4262017-08-30 06:29:51 -0700317 RTC_CHECK_GE(rate_update_index_, 0);
318 ++num_spatial_resizes_[rate_update_index_];
brandtr17b958c2017-03-07 01:41:43 -0800319 }
marpan@webrtc.orgf4c2de92012-06-05 21:07:28 +0000320 }
brandtr17b958c2017-03-07 01:41:43 -0800321 last_decoded_frame_num_ = frame_number;
322
Sergey Silkin64eaa992017-11-17 14:47:32 +0100323 // Skip quality metrics calculation to not affect CPU usage.
324 if (!config_.measure_cpu) {
325 frame_stat->psnr =
326 I420PSNR(input_frames_[frame_number].get(), &decoded_frame);
327 frame_stat->ssim =
328 I420SSIM(input_frames_[frame_number].get(), &decoded_frame);
329 }
Niels Möller718a7632016-06-13 13:06:01 +0200330
Sergey Silkin64eaa992017-11-17 14:47:32 +0100331 // Delay erasing of input frames by one frame. The current frame might
332 // still be needed for other simulcast stream or spatial layer.
333 const int frame_number_to_erase = frame_number - 1;
334 if (frame_number_to_erase >= 0) {
335 auto input_frame_erase_to =
336 input_frames_.lower_bound(frame_number_to_erase);
337 input_frames_.erase(input_frames_.begin(), input_frame_erase_to);
338 }
339
340 if (decoded_frame_writer_) {
341 ExtractBufferWithSize(decoded_frame, config_.codec_settings.width,
342 config_.codec_settings.height,
343 &last_decoded_frame_buffer_);
344 WriteDecodedFrameToFile(&last_decoded_frame_buffer_);
345 }
Åsa Perssonf0c44672017-10-24 16:03:39 +0200346}
brandtr17b958c2017-03-07 01:41:43 -0800347
Åsa Perssonf0c44672017-10-24 16:03:39 +0200348void VideoProcessor::WriteDecodedFrameToFile(rtc::Buffer* buffer) {
Sergey Silkin64eaa992017-11-17 14:47:32 +0100349 RTC_DCHECK_EQ(buffer->size(), decoded_frame_writer_->FrameLength());
350 RTC_CHECK(decoded_frame_writer_->WriteFrame(buffer->data()));
Åsa Perssonf0c44672017-10-24 16:03:39 +0200351}
brandtr17b958c2017-03-07 01:41:43 -0800352
Åsa Perssonf0c44672017-10-24 16:03:39 +0200353bool VideoProcessor::ExcludeFrame(const EncodedImage& encoded_image) {
354 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
355 if (encoded_image._frameType != kVideoFrameKey) {
356 return false;
357 }
358 bool exclude_frame = false;
359 switch (config_.exclude_frame_types) {
360 case kExcludeOnlyFirstKeyFrame:
361 if (!first_key_frame_has_been_excluded_) {
362 first_key_frame_has_been_excluded_ = true;
363 exclude_frame = true;
364 }
365 break;
366 case kExcludeAllKeyFrames:
367 exclude_frame = true;
368 break;
369 default:
370 RTC_NOTREACHED();
371 }
372 return exclude_frame;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000373}
374
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000375} // namespace test
376} // namespace webrtc