blob: 168f6bbd77ad754b9751348c5466b2664fe4b492 [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.
Åsa Perssona6e7b882018-01-19 14:57:10 +0100167 // TODO(asapersson): Time stamps jump back if framerate increases.
Sergey Silkin3be2a552018-01-17 15:11:44 +0100168 const size_t rtp_timestamp = (frame_number + 1) * kVideoPayloadTypeFrequency /
169 config_.codec_settings.maxFramerate;
Sami Kalliomäki20b294c2017-12-12 16:37:16 +0100170 const int64_t render_time_ms = (frame_number + 1) * rtc::kNumMillisecsPerSec /
171 config_.codec_settings.maxFramerate;
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.
Åsa Perssona6e7b882018-01-19 14:57:10 +0100179 FrameStatistic* frame_stat = stats_->AddFrame(rtp_timestamp);
brandtr17b958c2017-03-07 01:41:43 -0800180
181 // For the highest measurement accuracy of the encode time, the start/stop
182 // time recordings should wrap the Encode call as tightly as possible.
brandtr8935d972017-09-06 01:53:22 -0700183 frame_stat->encode_start_ns = rtc::TimeNanos();
brandtr17b958c2017-03-07 01:41:43 -0800184 frame_stat->encode_return_code =
Sergey Silkin64eaa992017-11-17 14:47:32 +0100185 encoder_->Encode(*input_frames_[frame_number], nullptr, &frame_types);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000186}
187
Sergey Silkin3be2a552018-01-17 15:11:44 +0100188void VideoProcessor::SetRates(size_t bitrate_kbps, size_t framerate_fps) {
brandtrc8c59052017-08-21 06:44:16 -0700189 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
Sergey Silkin3be2a552018-01-17 15:11:44 +0100190 config_.codec_settings.maxFramerate = static_cast<uint32_t>(framerate_fps);
191 bitrate_allocation_ = bitrate_allocator_->GetAllocation(
192 static_cast<uint32_t>(bitrate_kbps * 1000),
193 static_cast<uint32_t>(framerate_fps));
194 const int set_rates_result = encoder_->SetRateAllocation(
195 bitrate_allocation_, static_cast<uint32_t>(framerate_fps));
brandtrbea36fd2017-08-07 03:36:54 -0700196 RTC_DCHECK_GE(set_rates_result, 0)
brandtrbdd555c2017-08-21 01:34:04 -0700197 << "Failed to update encoder with new rate " << bitrate_kbps << ".";
brandtrbea36fd2017-08-07 03:36:54 -0700198}
199
brandtr45535622017-08-22 03:33:11 -0700200void VideoProcessor::FrameEncoded(webrtc::VideoCodecType codec,
201 const EncodedImage& encoded_image) {
brandtrc8c59052017-08-21 06:44:16 -0700202 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
203
brandtr32e0d262017-02-15 05:29:38 -0800204 // For the highest measurement accuracy of the encode time, the start/stop
205 // time recordings should wrap the Encode call as tightly as possible.
206 int64_t encode_stop_ns = rtc::TimeNanos();
207
Rasmus Brandtf7a35582017-10-24 10:16:33 +0200208 if (config_.encoded_frame_checker) {
209 config_.encoded_frame_checker->CheckEncodedFrame(codec, encoded_image);
210 }
brandtrb78bc752017-02-22 01:26:59 -0800211
Åsa Perssona6e7b882018-01-19 14:57:10 +0100212 FrameStatistic* frame_stat =
213 stats_->GetFrameWithTimestamp(encoded_image._timeStamp);
Sergey Silkin64eaa992017-11-17 14:47:32 +0100214
215 // Ensure strict monotonicity.
Åsa Perssona6e7b882018-01-19 14:57:10 +0100216 const size_t frame_number = frame_stat->frame_number;
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.
brandtr8935d972017-09-06 01:53:22 -0700231 frame_stat->encode_time_us =
232 GetElapsedTimeMicroseconds(frame_stat->encode_start_ns, encode_stop_ns);
brandtr17b958c2017-03-07 01:41:43 -0800233 frame_stat->encoding_successful = true;
brandtr8935d972017-09-06 01:53:22 -0700234 frame_stat->encoded_frame_size_bytes = encoded_image._length;
brandtr17b958c2017-03-07 01:41:43 -0800235 frame_stat->frame_type = encoded_image._frameType;
Sergey Silkin3be2a552018-01-17 15:11:44 +0100236 frame_stat->temporal_layer_idx = config_.TemporalLayerForFrame(frame_number);
brandtr17b958c2017-03-07 01:41:43 -0800237 frame_stat->qp = encoded_image.qp_;
Sergey Silkin3be2a552018-01-17 15:11:44 +0100238 frame_stat->target_bitrate_kbps =
239 bitrate_allocation_.GetSpatialLayerSum(0) / 1000;
brandtr17b958c2017-03-07 01:41:43 -0800240 frame_stat->total_packets =
philipelcce46fc2015-12-21 03:04:49 -0800241 encoded_image._length / config_.networking_config.packet_size_in_bytes +
242 1;
Sergey Silkin3be2a552018-01-17 15:11:44 +0100243 frame_stat->max_nalu_size_bytes = GetMaxNaluSizeBytes(encoded_image, config_);
ssilkin612f8582017-09-28 09:23:17 -0700244
Åsa Perssonf0c44672017-10-24 16:03:39 +0200245 // Make a raw copy of |encoded_image| to feed to the decoder.
hbos3fe2c6a2016-01-22 00:07:12 -0800246 size_t copied_buffer_size = encoded_image._length +
247 EncodedImage::GetBufferPaddingBytes(codec);
kwiberg3f55dea2016-02-29 05:51:59 -0800248 std::unique_ptr<uint8_t[]> copied_buffer(new uint8_t[copied_buffer_size]);
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000249 memcpy(copied_buffer.get(), encoded_image._buffer, encoded_image._length);
Åsa Perssonf0c44672017-10-24 16:03:39 +0200250 EncodedImage copied_image = encoded_image;
hbos3fe2c6a2016-01-22 00:07:12 -0800251 copied_image._size = copied_buffer_size;
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000252 copied_image._buffer = copied_buffer.get();
hbosbab934b2016-01-27 01:36:03 -0800253
Åsa Perssonf0c44672017-10-24 16:03:39 +0200254 // Simulate packet loss.
255 if (!ExcludeFrame(copied_image)) {
brandtr17b958c2017-03-07 01:41:43 -0800256 frame_stat->packets_dropped =
philipelcce46fc2015-12-21 03:04:49 -0800257 packet_manipulator_->ManipulatePackets(&copied_image);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000258 }
brandtr8935d972017-09-06 01:53:22 -0700259 frame_stat->manipulated_length = copied_image._length;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000260
brandtr32e0d262017-02-15 05:29:38 -0800261 // For the highest measurement accuracy of the decode time, the start/stop
262 // time recordings should wrap the Decode call as tightly as possible.
brandtr8935d972017-09-06 01:53:22 -0700263 frame_stat->decode_start_ns = rtc::TimeNanos();
brandtr17b958c2017-03-07 01:41:43 -0800264 frame_stat->decode_return_code =
265 decoder_->Decode(copied_image, last_frame_missing, nullptr);
brandtr8bc93852017-02-15 05:19:51 -0800266
brandtr8935d972017-09-06 01:53:22 -0700267 if (encoded_frame_writer_) {
268 RTC_CHECK(encoded_frame_writer_->WriteFrame(encoded_image, codec));
269 }
Sergey Silkin3be2a552018-01-17 15:11:44 +0100270
271 ++num_encoded_frames_;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000272}
273
Sergey Silkin64eaa992017-11-17 14:47:32 +0100274void VideoProcessor::FrameDecoded(const VideoFrame& decoded_frame) {
brandtrc8c59052017-08-21 06:44:16 -0700275 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
276
brandtr32e0d262017-02-15 05:29:38 -0800277 // For the highest measurement accuracy of the decode time, the start/stop
278 // time recordings should wrap the Decode call as tightly as possible.
Niels Möllerd28db7f2016-05-10 16:31:47 +0200279 int64_t decode_stop_ns = rtc::TimeNanos();
brandtr8bc93852017-02-15 05:19:51 -0800280
brandtr8935d972017-09-06 01:53:22 -0700281 // Update frame statistics.
Åsa Perssona6e7b882018-01-19 14:57:10 +0100282 FrameStatistic* frame_stat =
283 stats_->GetFrameWithTimestamp(decoded_frame.timestamp());
Sergey Silkin64eaa992017-11-17 14:47:32 +0100284 frame_stat->decoded_width = decoded_frame.width();
285 frame_stat->decoded_height = decoded_frame.height();
brandtr8935d972017-09-06 01:53:22 -0700286 frame_stat->decode_time_us =
287 GetElapsedTimeMicroseconds(frame_stat->decode_start_ns, decode_stop_ns);
brandtr17b958c2017-03-07 01:41:43 -0800288 frame_stat->decoding_successful = true;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000289
Sergey Silkin64eaa992017-11-17 14:47:32 +0100290 // Ensure strict monotonicity.
Åsa Perssona6e7b882018-01-19 14:57:10 +0100291 const size_t frame_number = frame_stat->frame_number;
Sergey Silkin3be2a552018-01-17 15:11:44 +0100292 if (num_decoded_frames_ > 0) {
293 RTC_CHECK_GT(frame_number, last_decoded_frame_num_);
294 }
Sergey Silkin64eaa992017-11-17 14:47:32 +0100295
brandtr17b958c2017-03-07 01:41:43 -0800296 // Check if the codecs have resized the frame since previously decoded frame.
297 if (frame_number > 0) {
Sergey Silkin3be2a552018-01-17 15:11:44 +0100298 if (decoded_frame_writer_ && num_decoded_frames_ > 0) {
Sergey Silkin64eaa992017-11-17 14:47:32 +0100299 // For dropped/lost frames, write out the last decoded frame to make it
300 // look like a freeze at playback.
Sergey Silkin3be2a552018-01-17 15:11:44 +0100301 const size_t num_dropped_frames =
302 frame_number - last_decoded_frame_num_ - 1;
303 for (size_t i = 0; i < num_dropped_frames; i++) {
Sergey Silkin64eaa992017-11-17 14:47:32 +0100304 WriteDecodedFrameToFile(&last_decoded_frame_buffer_);
305 }
306 }
marpan@webrtc.orgf4c2de92012-06-05 21:07:28 +0000307 }
brandtr17b958c2017-03-07 01:41:43 -0800308 last_decoded_frame_num_ = frame_number;
309
Sergey Silkin64eaa992017-11-17 14:47:32 +0100310 // Skip quality metrics calculation to not affect CPU usage.
311 if (!config_.measure_cpu) {
312 frame_stat->psnr =
313 I420PSNR(input_frames_[frame_number].get(), &decoded_frame);
314 frame_stat->ssim =
315 I420SSIM(input_frames_[frame_number].get(), &decoded_frame);
316 }
Niels Möller718a7632016-06-13 13:06:01 +0200317
Sergey Silkin64eaa992017-11-17 14:47:32 +0100318 // Delay erasing of input frames by one frame. The current frame might
319 // still be needed for other simulcast stream or spatial layer.
Sergey Silkin3be2a552018-01-17 15:11:44 +0100320 if (frame_number > 0) {
321 auto input_frame_erase_to = input_frames_.lower_bound(frame_number - 1);
Sergey Silkin64eaa992017-11-17 14:47:32 +0100322 input_frames_.erase(input_frames_.begin(), input_frame_erase_to);
323 }
324
325 if (decoded_frame_writer_) {
326 ExtractBufferWithSize(decoded_frame, config_.codec_settings.width,
327 config_.codec_settings.height,
328 &last_decoded_frame_buffer_);
329 WriteDecodedFrameToFile(&last_decoded_frame_buffer_);
330 }
Sergey Silkin3be2a552018-01-17 15:11:44 +0100331
332 ++num_decoded_frames_;
Åsa Perssonf0c44672017-10-24 16:03:39 +0200333}
brandtr17b958c2017-03-07 01:41:43 -0800334
Åsa Perssonf0c44672017-10-24 16:03:39 +0200335void VideoProcessor::WriteDecodedFrameToFile(rtc::Buffer* buffer) {
Sergey Silkin64eaa992017-11-17 14:47:32 +0100336 RTC_DCHECK_EQ(buffer->size(), decoded_frame_writer_->FrameLength());
337 RTC_CHECK(decoded_frame_writer_->WriteFrame(buffer->data()));
Åsa Perssonf0c44672017-10-24 16:03:39 +0200338}
brandtr17b958c2017-03-07 01:41:43 -0800339
Åsa Perssonf0c44672017-10-24 16:03:39 +0200340bool VideoProcessor::ExcludeFrame(const EncodedImage& encoded_image) {
341 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
342 if (encoded_image._frameType != kVideoFrameKey) {
343 return false;
344 }
345 bool exclude_frame = false;
346 switch (config_.exclude_frame_types) {
347 case kExcludeOnlyFirstKeyFrame:
348 if (!first_key_frame_has_been_excluded_) {
349 first_key_frame_has_been_excluded_ = true;
350 exclude_frame = true;
351 }
352 break;
353 case kExcludeAllKeyFrames:
354 exclude_frame = true;
355 break;
356 default:
357 RTC_NOTREACHED();
358 }
359 return exclude_frame;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000360}
361
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000362} // namespace test
363} // namespace webrtc