blob: 06475e196dbe47d57930576c026ea3058ea5be99 [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
Sergey Silkin18bc3e12018-01-17 13:15:57 +000032const int kRtpClockRateHz = 90000;
33
brandtraebc61e2017-02-28 07:13:47 -080034std::unique_ptr<VideoBitrateAllocator> CreateBitrateAllocator(
brandtr07734a52017-08-08 08:35:53 -070035 TestConfig* config) {
brandtraebc61e2017-02-28 07:13:47 -080036 std::unique_ptr<TemporalLayersFactory> tl_factory;
brandtr07734a52017-08-08 08:35:53 -070037 if (config->codec_settings.codecType == VideoCodecType::kVideoCodecVP8) {
brandtraebc61e2017-02-28 07:13:47 -080038 tl_factory.reset(new TemporalLayersFactory());
brandtr07734a52017-08-08 08:35:53 -070039 config->codec_settings.VP8()->tl_factory = tl_factory.get();
brandtraebc61e2017-02-28 07:13:47 -080040 }
41 return std::unique_ptr<VideoBitrateAllocator>(
brandtr07734a52017-08-08 08:35:53 -070042 VideoCodecInitializer::CreateBitrateAllocator(config->codec_settings,
brandtraebc61e2017-02-28 07:13:47 -080043 std::move(tl_factory)));
44}
45
Sergey Silkin18bc3e12018-01-17 13:15:57 +000046rtc::Optional<size_t> GetMaxNaluLength(const EncodedImage& encoded_frame,
47 const TestConfig& config) {
ssilkin612f8582017-09-28 09:23:17 -070048 if (config.codec_settings.codecType != kVideoCodecH264)
Sergey Silkin18bc3e12018-01-17 13:15:57 +000049 return rtc::nullopt;
ssilkin612f8582017-09-28 09:23:17 -070050
51 std::vector<webrtc::H264::NaluIndex> nalu_indices =
52 webrtc::H264::FindNaluIndices(encoded_frame._buffer,
53 encoded_frame._length);
54
55 RTC_CHECK(!nalu_indices.empty());
56
Sergey Silkin18bc3e12018-01-17 13:15:57 +000057 size_t max_length = 0;
ssilkin612f8582017-09-28 09:23:17 -070058 for (const webrtc::H264::NaluIndex& index : nalu_indices)
Sergey Silkin18bc3e12018-01-17 13:15:57 +000059 max_length = std::max(max_length, index.payload_size);
ssilkin612f8582017-09-28 09:23:17 -070060
Sergey Silkin18bc3e12018-01-17 13:15:57 +000061 return max_length;
ssilkin612f8582017-09-28 09:23:17 -070062}
63
asaperssonae9ba042017-03-07 00:25:38 -080064int GetElapsedTimeMicroseconds(int64_t start_ns, int64_t stop_ns) {
65 int64_t diff_us = (stop_ns - start_ns) / rtc::kNumNanosecsPerMicrosec;
66 RTC_DCHECK_GE(diff_us, std::numeric_limits<int>::min());
67 RTC_DCHECK_LE(diff_us, std::numeric_limits<int>::max());
68 return static_cast<int>(diff_us);
69}
70
Åsa Perssonf0c44672017-10-24 16:03:39 +020071void ExtractBufferWithSize(const VideoFrame& image,
72 int width,
73 int height,
74 rtc::Buffer* buffer) {
75 if (image.width() != width || image.height() != height) {
76 EXPECT_DOUBLE_EQ(static_cast<double>(width) / height,
77 static_cast<double>(image.width()) / image.height());
78 // Same aspect ratio, no cropping needed.
79 rtc::scoped_refptr<I420Buffer> scaled(I420Buffer::Create(width, height));
80 scaled->ScaleFrom(*image.video_frame_buffer()->ToI420());
81
82 size_t length =
83 CalcBufferSize(VideoType::kI420, scaled->width(), scaled->height());
84 buffer->SetSize(length);
85 RTC_CHECK_NE(ExtractBuffer(scaled, length, buffer->data()), -1);
86 return;
87 }
88
89 // No resize.
90 size_t length =
91 CalcBufferSize(VideoType::kI420, image.width(), image.height());
92 buffer->SetSize(length);
93 RTC_CHECK_NE(ExtractBuffer(image, length, buffer->data()), -1);
94}
95
brandtrb78bc752017-02-22 01:26:59 -080096} // namespace
97
brandtrc4095522017-08-07 08:12:33 -070098VideoProcessor::VideoProcessor(webrtc::VideoEncoder* encoder,
99 webrtc::VideoDecoder* decoder,
100 FrameReader* analysis_frame_reader,
brandtrc4095522017-08-07 08:12:33 -0700101 PacketManipulator* packet_manipulator,
102 const TestConfig& config,
103 Stats* stats,
brandtrc4095522017-08-07 08:12:33 -0700104 IvfFileWriter* encoded_frame_writer,
105 FrameWriter* decoded_frame_writer)
Åsa Perssonf0c44672017-10-24 16:03:39 +0200106 : config_(config),
brandtr07734a52017-08-08 08:35:53 -0700107 encoder_(encoder),
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000108 decoder_(decoder),
brandtr07734a52017-08-08 08:35:53 -0700109 bitrate_allocator_(CreateBitrateAllocator(&config_)),
brandtrbdd555c2017-08-21 01:34:04 -0700110 encode_callback_(this),
111 decode_callback_(this),
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000112 packet_manipulator_(packet_manipulator),
brandtraebc61e2017-02-28 07:13:47 -0800113 analysis_frame_reader_(analysis_frame_reader),
brandtrb78bc752017-02-22 01:26:59 -0800114 encoded_frame_writer_(encoded_frame_writer),
115 decoded_frame_writer_(decoded_frame_writer),
Sergey Silkin18bc3e12018-01-17 13:15:57 +0000116 last_inputed_frame_num_(-1),
117 last_encoded_frame_num_(-1),
118 last_decoded_frame_num_(-1),
brandtr17b958c2017-03-07 01:41:43 -0800119 first_key_frame_has_been_excluded_(false),
brandtrbdd555c2017-08-21 01:34:04 -0700120 last_decoded_frame_buffer_(analysis_frame_reader->FrameLength()),
Sergey Silkin18bc3e12018-01-17 13:15:57 +0000121 stats_(stats),
122 rate_update_index_(-1) {
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 Silkin18bc3e12018-01-17 13:15:57 +0000137 encoder_->InitEncode(&config_.codec_settings, config_.NumberOfCores(),
asapersson654d54c2017-02-10 00:16:07 -0800138 config_.networking_config.max_payload_size_in_bytes),
Åsa Perssonf0c44672017-10-24 16:03:39 +0200139 WEBRTC_VIDEO_CODEC_OK);
Sergey Silkin18bc3e12018-01-17 13:15:57 +0000140 RTC_CHECK_EQ(
141 decoder_->InitDecode(&config_.codec_settings, config_.NumberOfCores()),
142 WEBRTC_VIDEO_CODEC_OK);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000143}
144
Åsa Perssonf0c44672017-10-24 16:03:39 +0200145VideoProcessor::~VideoProcessor() {
brandtrc8c59052017-08-21 06:44:16 -0700146 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
147
brandtr77920a42017-08-11 07:48:15 -0700148 RTC_CHECK_EQ(encoder_->Release(), WEBRTC_VIDEO_CODEC_OK);
149 RTC_CHECK_EQ(decoder_->Release(), WEBRTC_VIDEO_CODEC_OK);
150
brandtrbdd555c2017-08-21 01:34:04 -0700151 encoder_->RegisterEncodeCompleteCallback(nullptr);
152 decoder_->RegisterDecodeCompleteCallback(nullptr);
brandtr77920a42017-08-11 07:48:15 -0700153}
154
brandtr8935d972017-09-06 01:53:22 -0700155void VideoProcessor::ProcessFrame() {
brandtrc8c59052017-08-21 06:44:16 -0700156 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
Sergey Silkin18bc3e12018-01-17 13:15:57 +0000157 const int frame_number = ++last_inputed_frame_num_;
asapersson654d54c2017-02-10 00:16:07 -0800158
brandtrbdd555c2017-08-21 01:34:04 -0700159 // Get frame from file.
magjed3f075492017-06-01 10:02:26 -0700160 rtc::scoped_refptr<I420BufferInterface> buffer(
brandtrb78bc752017-02-22 01:26:59 -0800161 analysis_frame_reader_->ReadFrame());
brandtrbdd555c2017-08-21 01:34:04 -0700162 RTC_CHECK(buffer) << "Tried to read too many frames from the file.";
brandtrb57f4262017-08-30 06:29:51 -0700163 // Use the frame number as the basis for timestamp to identify frames. Let the
164 // first timestamp be non-zero, to not make the IvfFileWriter believe that we
165 // want to use capture timestamps in the IVF files.
Sergey Silkin18bc3e12018-01-17 13:15:57 +0000166 const uint32_t rtp_timestamp = (frame_number + 1) * kRtpClockRateHz /
167 config_.codec_settings.maxFramerate;
Sami Kalliomäki20b294c2017-12-12 16:37:16 +0100168 const int64_t render_time_ms = (frame_number + 1) * rtc::kNumMillisecsPerSec /
169 config_.codec_settings.maxFramerate;
Sergey Silkin64eaa992017-11-17 14:47:32 +0100170 rtp_timestamp_to_frame_num_[rtp_timestamp] = frame_number;
Sergey Silkin18bc3e12018-01-17 13:15:57 +0000171 input_frames_[frame_number] = rtc::MakeUnique<VideoFrame>(
172 buffer, rtp_timestamp, render_time_ms, webrtc::kVideoRotation_0);
brandtr17b958c2017-03-07 01:41:43 -0800173
Sergey Silkin64eaa992017-11-17 14:47:32 +0100174 std::vector<FrameType> frame_types = config_.FrameTypeForFrame(frame_number);
brandtr17b958c2017-03-07 01:41:43 -0800175
176 // Create frame statistics object used for aggregation at end of test run.
brandtr8935d972017-09-06 01:53:22 -0700177 FrameStatistic* frame_stat = stats_->AddFrame();
brandtr17b958c2017-03-07 01:41:43 -0800178
179 // For the highest measurement accuracy of the encode time, the start/stop
180 // time recordings should wrap the Encode call as tightly as possible.
brandtr8935d972017-09-06 01:53:22 -0700181 frame_stat->encode_start_ns = rtc::TimeNanos();
brandtr17b958c2017-03-07 01:41:43 -0800182 frame_stat->encode_return_code =
Sergey Silkin64eaa992017-11-17 14:47:32 +0100183 encoder_->Encode(*input_frames_[frame_number], nullptr, &frame_types);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000184}
185
Sergey Silkin18bc3e12018-01-17 13:15:57 +0000186void VideoProcessor::SetRates(int bitrate_kbps, int framerate_fps) {
brandtrc8c59052017-08-21 06:44:16 -0700187 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
Sergey Silkin18bc3e12018-01-17 13:15:57 +0000188 config_.codec_settings.maxFramerate = framerate_fps;
189 int set_rates_result = encoder_->SetRateAllocation(
190 bitrate_allocator_->GetAllocation(bitrate_kbps * 1000, framerate_fps),
191 framerate_fps);
brandtrbea36fd2017-08-07 03:36:54 -0700192 RTC_DCHECK_GE(set_rates_result, 0)
brandtrbdd555c2017-08-21 01:34:04 -0700193 << "Failed to update encoder with new rate " << bitrate_kbps << ".";
Sergey Silkin18bc3e12018-01-17 13:15:57 +0000194 ++rate_update_index_;
195 num_dropped_frames_.push_back(0);
196 num_spatial_resizes_.push_back(0);
197}
198
199std::vector<int> VideoProcessor::NumberDroppedFramesPerRateUpdate() const {
200 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
201 return num_dropped_frames_;
202}
203
204std::vector<int> VideoProcessor::NumberSpatialResizesPerRateUpdate() const {
205 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
206 return num_spatial_resizes_;
brandtrbea36fd2017-08-07 03:36:54 -0700207}
208
brandtr45535622017-08-22 03:33:11 -0700209void VideoProcessor::FrameEncoded(webrtc::VideoCodecType codec,
210 const EncodedImage& encoded_image) {
brandtrc8c59052017-08-21 06:44:16 -0700211 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
212
brandtr32e0d262017-02-15 05:29:38 -0800213 // For the highest measurement accuracy of the encode time, the start/stop
214 // time recordings should wrap the Encode call as tightly as possible.
215 int64_t encode_stop_ns = rtc::TimeNanos();
216
Rasmus Brandtf7a35582017-10-24 10:16:33 +0200217 if (config_.encoded_frame_checker) {
218 config_.encoded_frame_checker->CheckEncodedFrame(codec, encoded_image);
219 }
brandtrb78bc752017-02-22 01:26:59 -0800220
Sergey Silkin18bc3e12018-01-17 13:15:57 +0000221 const int frame_number =
brandtrb57f4262017-08-30 06:29:51 -0700222 rtp_timestamp_to_frame_num_[encoded_image._timeStamp];
Sergey Silkin64eaa992017-11-17 14:47:32 +0100223
224 // Ensure strict monotonicity.
Sergey Silkin18bc3e12018-01-17 13:15:57 +0000225 RTC_CHECK_GT(frame_number, last_encoded_frame_num_);
Sergey Silkin64eaa992017-11-17 14:47:32 +0100226
227 // Check for dropped frames.
brandtr17b958c2017-03-07 01:41:43 -0800228 bool last_frame_missing = false;
229 if (frame_number > 0) {
Sergey Silkin18bc3e12018-01-17 13:15:57 +0000230 int num_dropped_from_last_encode =
231 frame_number - last_encoded_frame_num_ - 1;
232 RTC_DCHECK_GE(num_dropped_from_last_encode, 0);
233 RTC_CHECK_GE(rate_update_index_, 0);
234 num_dropped_frames_[rate_update_index_] += num_dropped_from_last_encode;
brandtr8935d972017-09-06 01:53:22 -0700235 const FrameStatistic* last_encoded_frame_stat =
236 stats_->GetFrame(last_encoded_frame_num_);
237 last_frame_missing = (last_encoded_frame_stat->manipulated_length == 0);
brandtr17b958c2017-03-07 01:41:43 -0800238 }
brandtr17b958c2017-03-07 01:41:43 -0800239 last_encoded_frame_num_ = frame_number;
240
brandtr8935d972017-09-06 01:53:22 -0700241 // Update frame statistics.
242 FrameStatistic* frame_stat = stats_->GetFrame(frame_number);
243 frame_stat->encode_time_us =
244 GetElapsedTimeMicroseconds(frame_stat->encode_start_ns, encode_stop_ns);
brandtr17b958c2017-03-07 01:41:43 -0800245 frame_stat->encoding_successful = true;
brandtr8935d972017-09-06 01:53:22 -0700246 frame_stat->encoded_frame_size_bytes = encoded_image._length;
brandtr17b958c2017-03-07 01:41:43 -0800247 frame_stat->frame_type = encoded_image._frameType;
248 frame_stat->qp = encoded_image.qp_;
Sergey Silkin18bc3e12018-01-17 13:15:57 +0000249 frame_stat->bitrate_kbps = static_cast<int>(
250 encoded_image._length * config_.codec_settings.maxFramerate * 8 / 1000);
brandtr17b958c2017-03-07 01:41:43 -0800251 frame_stat->total_packets =
philipelcce46fc2015-12-21 03:04:49 -0800252 encoded_image._length / config_.networking_config.packet_size_in_bytes +
253 1;
Sergey Silkin18bc3e12018-01-17 13:15:57 +0000254 frame_stat->max_nalu_length = GetMaxNaluLength(encoded_image, config_);
ssilkin612f8582017-09-28 09:23:17 -0700255
Åsa Perssonf0c44672017-10-24 16:03:39 +0200256 // Make a raw copy of |encoded_image| to feed to the decoder.
hbos3fe2c6a2016-01-22 00:07:12 -0800257 size_t copied_buffer_size = encoded_image._length +
258 EncodedImage::GetBufferPaddingBytes(codec);
kwiberg3f55dea2016-02-29 05:51:59 -0800259 std::unique_ptr<uint8_t[]> copied_buffer(new uint8_t[copied_buffer_size]);
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000260 memcpy(copied_buffer.get(), encoded_image._buffer, encoded_image._length);
Åsa Perssonf0c44672017-10-24 16:03:39 +0200261 EncodedImage copied_image = encoded_image;
hbos3fe2c6a2016-01-22 00:07:12 -0800262 copied_image._size = copied_buffer_size;
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000263 copied_image._buffer = copied_buffer.get();
hbosbab934b2016-01-27 01:36:03 -0800264
Åsa Perssonf0c44672017-10-24 16:03:39 +0200265 // Simulate packet loss.
266 if (!ExcludeFrame(copied_image)) {
brandtr17b958c2017-03-07 01:41:43 -0800267 frame_stat->packets_dropped =
philipelcce46fc2015-12-21 03:04:49 -0800268 packet_manipulator_->ManipulatePackets(&copied_image);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000269 }
brandtr8935d972017-09-06 01:53:22 -0700270 frame_stat->manipulated_length = copied_image._length;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000271
brandtr32e0d262017-02-15 05:29:38 -0800272 // For the highest measurement accuracy of the decode time, the start/stop
273 // time recordings should wrap the Decode call as tightly as possible.
brandtr8935d972017-09-06 01:53:22 -0700274 frame_stat->decode_start_ns = rtc::TimeNanos();
brandtr17b958c2017-03-07 01:41:43 -0800275 frame_stat->decode_return_code =
276 decoder_->Decode(copied_image, last_frame_missing, nullptr);
brandtr8bc93852017-02-15 05:19:51 -0800277
brandtr8935d972017-09-06 01:53:22 -0700278 if (encoded_frame_writer_) {
279 RTC_CHECK(encoded_frame_writer_->WriteFrame(encoded_image, codec));
280 }
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000281}
282
Sergey Silkin64eaa992017-11-17 14:47:32 +0100283void VideoProcessor::FrameDecoded(const VideoFrame& decoded_frame) {
brandtrc8c59052017-08-21 06:44:16 -0700284 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
285
brandtr32e0d262017-02-15 05:29:38 -0800286 // For the highest measurement accuracy of the decode time, the start/stop
287 // time recordings should wrap the Decode call as tightly as possible.
Niels Möllerd28db7f2016-05-10 16:31:47 +0200288 int64_t decode_stop_ns = rtc::TimeNanos();
brandtr8bc93852017-02-15 05:19:51 -0800289
brandtr8935d972017-09-06 01:53:22 -0700290 // Update frame statistics.
Sergey Silkin18bc3e12018-01-17 13:15:57 +0000291 const int frame_number =
Sergey Silkin64eaa992017-11-17 14:47:32 +0100292 rtp_timestamp_to_frame_num_[decoded_frame.timestamp()];
brandtr8935d972017-09-06 01:53:22 -0700293 FrameStatistic* frame_stat = stats_->GetFrame(frame_number);
Sergey Silkin64eaa992017-11-17 14:47:32 +0100294 frame_stat->decoded_width = decoded_frame.width();
295 frame_stat->decoded_height = decoded_frame.height();
brandtr8935d972017-09-06 01:53:22 -0700296 frame_stat->decode_time_us =
297 GetElapsedTimeMicroseconds(frame_stat->decode_start_ns, decode_stop_ns);
brandtr17b958c2017-03-07 01:41:43 -0800298 frame_stat->decoding_successful = true;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000299
Sergey Silkin64eaa992017-11-17 14:47:32 +0100300 // Ensure strict monotonicity.
Sergey Silkin18bc3e12018-01-17 13:15:57 +0000301 RTC_CHECK_GT(frame_number, last_decoded_frame_num_);
Sergey Silkin64eaa992017-11-17 14:47:32 +0100302
brandtr17b958c2017-03-07 01:41:43 -0800303 // Check if the codecs have resized the frame since previously decoded frame.
304 if (frame_number > 0) {
Sergey Silkin64eaa992017-11-17 14:47:32 +0100305 if (decoded_frame_writer_ && last_decoded_frame_num_ >= 0) {
306 // For dropped/lost frames, write out the last decoded frame to make it
307 // look like a freeze at playback.
Sergey Silkin18bc3e12018-01-17 13:15:57 +0000308 const int num_dropped_frames = frame_number - last_decoded_frame_num_;
309 for (int i = 0; i < num_dropped_frames; i++) {
Sergey Silkin64eaa992017-11-17 14:47:32 +0100310 WriteDecodedFrameToFile(&last_decoded_frame_buffer_);
311 }
312 }
Sergey Silkin18bc3e12018-01-17 13:15:57 +0000313 // TODO(ssilkin): move to FrameEncoded when webm:1474 is implemented.
314 const FrameStatistic* last_decoded_frame_stat =
315 stats_->GetFrame(last_decoded_frame_num_);
316 if (decoded_frame.width() != last_decoded_frame_stat->decoded_width ||
317 decoded_frame.height() != last_decoded_frame_stat->decoded_height) {
318 RTC_CHECK_GE(rate_update_index_, 0);
319 ++num_spatial_resizes_[rate_update_index_];
320 }
marpan@webrtc.orgf4c2de92012-06-05 21:07:28 +0000321 }
brandtr17b958c2017-03-07 01:41:43 -0800322 last_decoded_frame_num_ = frame_number;
323
Sergey Silkin64eaa992017-11-17 14:47:32 +0100324 // Skip quality metrics calculation to not affect CPU usage.
325 if (!config_.measure_cpu) {
326 frame_stat->psnr =
327 I420PSNR(input_frames_[frame_number].get(), &decoded_frame);
328 frame_stat->ssim =
329 I420SSIM(input_frames_[frame_number].get(), &decoded_frame);
330 }
Niels Möller718a7632016-06-13 13:06:01 +0200331
Sergey Silkin64eaa992017-11-17 14:47:32 +0100332 // Delay erasing of input frames by one frame. The current frame might
333 // still be needed for other simulcast stream or spatial layer.
Sergey Silkin18bc3e12018-01-17 13:15:57 +0000334 const int frame_number_to_erase = frame_number - 1;
335 if (frame_number_to_erase >= 0) {
336 auto input_frame_erase_to =
337 input_frames_.lower_bound(frame_number_to_erase);
Sergey Silkin64eaa992017-11-17 14:47:32 +0100338 input_frames_.erase(input_frames_.begin(), input_frame_erase_to);
339 }
340
341 if (decoded_frame_writer_) {
342 ExtractBufferWithSize(decoded_frame, config_.codec_settings.width,
343 config_.codec_settings.height,
344 &last_decoded_frame_buffer_);
345 WriteDecodedFrameToFile(&last_decoded_frame_buffer_);
346 }
Åsa Perssonf0c44672017-10-24 16:03:39 +0200347}
brandtr17b958c2017-03-07 01:41:43 -0800348
Åsa Perssonf0c44672017-10-24 16:03:39 +0200349void VideoProcessor::WriteDecodedFrameToFile(rtc::Buffer* buffer) {
Sergey Silkin64eaa992017-11-17 14:47:32 +0100350 RTC_DCHECK_EQ(buffer->size(), decoded_frame_writer_->FrameLength());
351 RTC_CHECK(decoded_frame_writer_->WriteFrame(buffer->data()));
Åsa Perssonf0c44672017-10-24 16:03:39 +0200352}
brandtr17b958c2017-03-07 01:41:43 -0800353
Åsa Perssonf0c44672017-10-24 16:03:39 +0200354bool VideoProcessor::ExcludeFrame(const EncodedImage& encoded_image) {
355 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
356 if (encoded_image._frameType != kVideoFrameKey) {
357 return false;
358 }
359 bool exclude_frame = false;
360 switch (config_.exclude_frame_types) {
361 case kExcludeOnlyFirstKeyFrame:
362 if (!first_key_frame_has_been_excluded_) {
363 first_key_frame_has_been_excluded_ = true;
364 exclude_frame = true;
365 }
366 break;
367 case kExcludeAllKeyFrames:
368 exclude_frame = true;
369 break;
370 default:
371 RTC_NOTREACHED();
372 }
373 return exclude_frame;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000374}
375
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000376} // namespace test
377} // namespace webrtc