blob: 0ed380cc8628bb064f4e3a9ee43cdb1d30ab556e [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;
brandtr17b958c2017-03-07 01:41:43 -080033
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
ssilkin612f8582017-09-28 09:23:17 -070046rtc::Optional<size_t> GetMaxNaluLength(const EncodedImage& encoded_frame,
47 const TestConfig& config) {
48 if (config.codec_settings.codecType != kVideoCodecH264)
49 return rtc::Optional<size_t>();
50
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
57 size_t max_length = 0;
58 for (const webrtc::H264::NaluIndex& index : nalu_indices)
59 max_length = std::max(max_length, index.payload_size);
60
61 return rtc::Optional<size_t>(max_length);
62}
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,
101 FrameWriter* analysis_frame_writer,
102 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),
115 analysis_frame_writer_(analysis_frame_writer),
brandtrb78bc752017-02-22 01:26:59 -0800116 encoded_frame_writer_(encoded_frame_writer),
117 decoded_frame_writer_(decoded_frame_writer),
brandtr8935d972017-09-06 01:53:22 -0700118 last_inputed_frame_num_(-1),
brandtr17b958c2017-03-07 01:41:43 -0800119 last_encoded_frame_num_(-1),
120 last_decoded_frame_num_(-1),
121 first_key_frame_has_been_excluded_(false),
brandtrbdd555c2017-08-21 01:34:04 -0700122 last_decoded_frame_buffer_(analysis_frame_reader->FrameLength()),
brandtraebc61e2017-02-28 07:13:47 -0800123 stats_(stats),
brandtrb57f4262017-08-30 06:29:51 -0700124 rate_update_index_(-1) {
Erik Språng08127a92016-11-16 16:41:30 +0100125 RTC_DCHECK(encoder);
126 RTC_DCHECK(decoder);
brandtraebc61e2017-02-28 07:13:47 -0800127 RTC_DCHECK(packet_manipulator);
brandtrb78bc752017-02-22 01:26:59 -0800128 RTC_DCHECK(analysis_frame_reader);
129 RTC_DCHECK(analysis_frame_writer);
Erik Språng08127a92016-11-16 16:41:30 +0100130 RTC_DCHECK(stats);
brandtr17b958c2017-03-07 01:41:43 -0800131
brandtrbea36fd2017-08-07 03:36:54 -0700132 // Setup required callbacks for the encoder and decoder.
brandtrbdd555c2017-08-21 01:34:04 -0700133 RTC_CHECK_EQ(encoder_->RegisterEncodeCompleteCallback(&encode_callback_),
Åsa Perssonf0c44672017-10-24 16:03:39 +0200134 WEBRTC_VIDEO_CODEC_OK);
brandtrbdd555c2017-08-21 01:34:04 -0700135 RTC_CHECK_EQ(decoder_->RegisterDecodeCompleteCallback(&decode_callback_),
Åsa Perssonf0c44672017-10-24 16:03:39 +0200136 WEBRTC_VIDEO_CODEC_OK);
asapersson654d54c2017-02-10 00:16:07 -0800137
brandtraebc61e2017-02-28 07:13:47 -0800138 // Initialize the encoder and decoder.
asapersson654d54c2017-02-10 00:16:07 -0800139 RTC_CHECK_EQ(
Åsa Persson2d27fb52017-10-19 14:05:50 +0200140 encoder_->InitEncode(&config_.codec_settings, config_.NumberOfCores(),
asapersson654d54c2017-02-10 00:16:07 -0800141 config_.networking_config.max_payload_size_in_bytes),
Åsa Perssonf0c44672017-10-24 16:03:39 +0200142 WEBRTC_VIDEO_CODEC_OK);
Åsa Persson2d27fb52017-10-19 14:05:50 +0200143 RTC_CHECK_EQ(
144 decoder_->InitDecode(&config_.codec_settings, config_.NumberOfCores()),
Åsa Perssonf0c44672017-10-24 16:03:39 +0200145 WEBRTC_VIDEO_CODEC_OK);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000146}
147
Åsa Perssonf0c44672017-10-24 16:03:39 +0200148VideoProcessor::~VideoProcessor() {
brandtrc8c59052017-08-21 06:44:16 -0700149 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
150
brandtr77920a42017-08-11 07:48:15 -0700151 RTC_CHECK_EQ(encoder_->Release(), WEBRTC_VIDEO_CODEC_OK);
152 RTC_CHECK_EQ(decoder_->Release(), WEBRTC_VIDEO_CODEC_OK);
153
brandtrbdd555c2017-08-21 01:34:04 -0700154 encoder_->RegisterEncodeCompleteCallback(nullptr);
155 decoder_->RegisterDecodeCompleteCallback(nullptr);
brandtr77920a42017-08-11 07:48:15 -0700156}
157
brandtr8935d972017-09-06 01:53:22 -0700158void VideoProcessor::ProcessFrame() {
brandtrc8c59052017-08-21 06:44:16 -0700159 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
brandtr8935d972017-09-06 01:53:22 -0700160 ++last_inputed_frame_num_;
asapersson654d54c2017-02-10 00:16:07 -0800161
brandtrbdd555c2017-08-21 01:34:04 -0700162 // Get frame from file.
magjed3f075492017-06-01 10:02:26 -0700163 rtc::scoped_refptr<I420BufferInterface> buffer(
brandtrb78bc752017-02-22 01:26:59 -0800164 analysis_frame_reader_->ReadFrame());
brandtrbdd555c2017-08-21 01:34:04 -0700165 RTC_CHECK(buffer) << "Tried to read too many frames from the file.";
brandtrb57f4262017-08-30 06:29:51 -0700166 // Use the frame number as the basis for timestamp to identify frames. Let the
167 // first timestamp be non-zero, to not make the IvfFileWriter believe that we
168 // want to use capture timestamps in the IVF files.
brandtr8935d972017-09-06 01:53:22 -0700169 const uint32_t rtp_timestamp = (last_inputed_frame_num_ + 1) *
170 kRtpClockRateHz /
brandtrb57f4262017-08-30 06:29:51 -0700171 config_.codec_settings.maxFramerate;
brandtr8935d972017-09-06 01:53:22 -0700172 rtp_timestamp_to_frame_num_[rtp_timestamp] = last_inputed_frame_num_;
brandtrbdd555c2017-08-21 01:34:04 -0700173 const int64_t kNoRenderTime = 0;
brandtrb57f4262017-08-30 06:29:51 -0700174 VideoFrame source_frame(buffer, rtp_timestamp, kNoRenderTime,
175 webrtc::kVideoRotation_0);
brandtr17b958c2017-03-07 01:41:43 -0800176
177 // Decide if we are going to force a keyframe.
178 std::vector<FrameType> frame_types(1, kVideoFrameDelta);
179 if (config_.keyframe_interval > 0 &&
brandtr8935d972017-09-06 01:53:22 -0700180 last_inputed_frame_num_ % config_.keyframe_interval == 0) {
brandtr17b958c2017-03-07 01:41:43 -0800181 frame_types[0] = kVideoFrameKey;
182 }
183
184 // Create frame statistics object used for aggregation at end of test run.
brandtr8935d972017-09-06 01:53:22 -0700185 FrameStatistic* frame_stat = stats_->AddFrame();
brandtr17b958c2017-03-07 01:41:43 -0800186
187 // For the highest measurement accuracy of the encode time, the start/stop
188 // time recordings should wrap the Encode call as tightly as possible.
brandtr8935d972017-09-06 01:53:22 -0700189 frame_stat->encode_start_ns = rtc::TimeNanos();
brandtr17b958c2017-03-07 01:41:43 -0800190 frame_stat->encode_return_code =
191 encoder_->Encode(source_frame, nullptr, &frame_types);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000192}
193
brandtrbdd555c2017-08-21 01:34:04 -0700194void VideoProcessor::SetRates(int bitrate_kbps, int framerate_fps) {
brandtrc8c59052017-08-21 06:44:16 -0700195 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
brandtrbdd555c2017-08-21 01:34:04 -0700196 config_.codec_settings.maxFramerate = framerate_fps;
brandtrbea36fd2017-08-07 03:36:54 -0700197 int set_rates_result = encoder_->SetRateAllocation(
brandtrbdd555c2017-08-21 01:34:04 -0700198 bitrate_allocator_->GetAllocation(bitrate_kbps * 1000, framerate_fps),
199 framerate_fps);
brandtrbea36fd2017-08-07 03:36:54 -0700200 RTC_DCHECK_GE(set_rates_result, 0)
brandtrbdd555c2017-08-21 01:34:04 -0700201 << "Failed to update encoder with new rate " << bitrate_kbps << ".";
brandtrb57f4262017-08-30 06:29:51 -0700202 ++rate_update_index_;
203 num_dropped_frames_.push_back(0);
204 num_spatial_resizes_.push_back(0);
brandtrbea36fd2017-08-07 03:36:54 -0700205}
206
brandtrb57f4262017-08-30 06:29:51 -0700207std::vector<int> VideoProcessor::NumberDroppedFramesPerRateUpdate() const {
brandtrc8c59052017-08-21 06:44:16 -0700208 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
brandtrbea36fd2017-08-07 03:36:54 -0700209 return num_dropped_frames_;
210}
211
brandtrb57f4262017-08-30 06:29:51 -0700212std::vector<int> VideoProcessor::NumberSpatialResizesPerRateUpdate() const {
brandtrc8c59052017-08-21 06:44:16 -0700213 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
brandtrbea36fd2017-08-07 03:36:54 -0700214 return num_spatial_resizes_;
215}
216
brandtr45535622017-08-22 03:33:11 -0700217void VideoProcessor::FrameEncoded(webrtc::VideoCodecType codec,
218 const EncodedImage& encoded_image) {
brandtrc8c59052017-08-21 06:44:16 -0700219 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
220
brandtr32e0d262017-02-15 05:29:38 -0800221 // For the highest measurement accuracy of the encode time, the start/stop
222 // time recordings should wrap the Encode call as tightly as possible.
223 int64_t encode_stop_ns = rtc::TimeNanos();
224
Rasmus Brandtf7a35582017-10-24 10:16:33 +0200225 if (config_.encoded_frame_checker) {
226 config_.encoded_frame_checker->CheckEncodedFrame(codec, encoded_image);
227 }
brandtrb78bc752017-02-22 01:26:59 -0800228
brandtrb57f4262017-08-30 06:29:51 -0700229 // Check for dropped frames.
230 const int frame_number =
231 rtp_timestamp_to_frame_num_[encoded_image._timeStamp];
brandtr17b958c2017-03-07 01:41:43 -0800232 bool last_frame_missing = false;
233 if (frame_number > 0) {
234 RTC_DCHECK_GE(last_encoded_frame_num_, 0);
235 int num_dropped_from_last_encode =
236 frame_number - last_encoded_frame_num_ - 1;
237 RTC_DCHECK_GE(num_dropped_from_last_encode, 0);
brandtrb57f4262017-08-30 06:29:51 -0700238 RTC_CHECK_GE(rate_update_index_, 0);
239 num_dropped_frames_[rate_update_index_] += num_dropped_from_last_encode;
brandtr17b958c2017-03-07 01:41:43 -0800240 if (num_dropped_from_last_encode > 0) {
241 // For dropped frames, we write out the last decoded frame to avoid
242 // getting out of sync for the computation of PSNR and SSIM.
243 for (int i = 0; i < num_dropped_from_last_encode; i++) {
Åsa Perssonf0c44672017-10-24 16:03:39 +0200244 WriteDecodedFrameToFile(&last_decoded_frame_buffer_);
brandtrb78bc752017-02-22 01:26:59 -0800245 }
marpan@webrtc.orgf4c2de92012-06-05 21:07:28 +0000246 }
brandtr8935d972017-09-06 01:53:22 -0700247 const FrameStatistic* last_encoded_frame_stat =
248 stats_->GetFrame(last_encoded_frame_num_);
249 last_frame_missing = (last_encoded_frame_stat->manipulated_length == 0);
brandtr17b958c2017-03-07 01:41:43 -0800250 }
251 // Ensure strict monotonicity.
252 RTC_CHECK_GT(frame_number, last_encoded_frame_num_);
253 last_encoded_frame_num_ = frame_number;
254
brandtr8935d972017-09-06 01:53:22 -0700255 // Update frame statistics.
256 FrameStatistic* frame_stat = stats_->GetFrame(frame_number);
257 frame_stat->encode_time_us =
258 GetElapsedTimeMicroseconds(frame_stat->encode_start_ns, encode_stop_ns);
brandtr17b958c2017-03-07 01:41:43 -0800259 frame_stat->encoding_successful = true;
brandtr8935d972017-09-06 01:53:22 -0700260 frame_stat->encoded_frame_size_bytes = encoded_image._length;
brandtr17b958c2017-03-07 01:41:43 -0800261 frame_stat->frame_type = encoded_image._frameType;
262 frame_stat->qp = encoded_image.qp_;
brandtr8935d972017-09-06 01:53:22 -0700263 frame_stat->bitrate_kbps = static_cast<int>(
brandtr07734a52017-08-08 08:35:53 -0700264 encoded_image._length * config_.codec_settings.maxFramerate * 8 / 1000);
brandtr17b958c2017-03-07 01:41:43 -0800265 frame_stat->total_packets =
philipelcce46fc2015-12-21 03:04:49 -0800266 encoded_image._length / config_.networking_config.packet_size_in_bytes +
267 1;
ssilkin612f8582017-09-28 09:23:17 -0700268 frame_stat->max_nalu_length = GetMaxNaluLength(encoded_image, config_);
269
Åsa Perssonf0c44672017-10-24 16:03:39 +0200270 // Make a raw copy of |encoded_image| to feed to the decoder.
hbos3fe2c6a2016-01-22 00:07:12 -0800271 size_t copied_buffer_size = encoded_image._length +
272 EncodedImage::GetBufferPaddingBytes(codec);
kwiberg3f55dea2016-02-29 05:51:59 -0800273 std::unique_ptr<uint8_t[]> copied_buffer(new uint8_t[copied_buffer_size]);
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000274 memcpy(copied_buffer.get(), encoded_image._buffer, encoded_image._length);
Åsa Perssonf0c44672017-10-24 16:03:39 +0200275 EncodedImage copied_image = encoded_image;
hbos3fe2c6a2016-01-22 00:07:12 -0800276 copied_image._size = copied_buffer_size;
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000277 copied_image._buffer = copied_buffer.get();
hbosbab934b2016-01-27 01:36:03 -0800278
Åsa Perssonf0c44672017-10-24 16:03:39 +0200279 // Simulate packet loss.
280 if (!ExcludeFrame(copied_image)) {
brandtr17b958c2017-03-07 01:41:43 -0800281 frame_stat->packets_dropped =
philipelcce46fc2015-12-21 03:04:49 -0800282 packet_manipulator_->ManipulatePackets(&copied_image);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000283 }
brandtr8935d972017-09-06 01:53:22 -0700284 frame_stat->manipulated_length = copied_image._length;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000285
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.
brandtr8935d972017-09-06 01:53:22 -0700288 frame_stat->decode_start_ns = rtc::TimeNanos();
brandtr17b958c2017-03-07 01:41:43 -0800289 frame_stat->decode_return_code =
290 decoder_->Decode(copied_image, last_frame_missing, nullptr);
brandtr8bc93852017-02-15 05:19:51 -0800291
brandtr17b958c2017-03-07 01:41:43 -0800292 if (frame_stat->decode_return_code != WEBRTC_VIDEO_CODEC_OK) {
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000293 // Write the last successful frame the output file to avoid getting it out
brandtr8bc93852017-02-15 05:19:51 -0800294 // of sync with the source file for SSIM and PSNR comparisons.
Åsa Perssonf0c44672017-10-24 16:03:39 +0200295 WriteDecodedFrameToFile(&last_decoded_frame_buffer_);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000296 }
brandtr8935d972017-09-06 01:53:22 -0700297
298 if (encoded_frame_writer_) {
299 RTC_CHECK(encoded_frame_writer_->WriteFrame(encoded_image, codec));
300 }
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000301}
302
brandtrc4095522017-08-07 08:12:33 -0700303void VideoProcessor::FrameDecoded(const VideoFrame& image) {
brandtrc8c59052017-08-21 06:44:16 -0700304 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
305
brandtr32e0d262017-02-15 05:29:38 -0800306 // For the highest measurement accuracy of the decode time, the start/stop
307 // time recordings should wrap the Decode call as tightly as possible.
Niels Möllerd28db7f2016-05-10 16:31:47 +0200308 int64_t decode_stop_ns = rtc::TimeNanos();
brandtr8bc93852017-02-15 05:19:51 -0800309
brandtr8935d972017-09-06 01:53:22 -0700310 // Update frame statistics.
brandtrb57f4262017-08-30 06:29:51 -0700311 const int frame_number = rtp_timestamp_to_frame_num_[image.timestamp()];
brandtr8935d972017-09-06 01:53:22 -0700312 FrameStatistic* frame_stat = stats_->GetFrame(frame_number);
313 frame_stat->decoded_width = image.width();
314 frame_stat->decoded_height = image.height();
315 frame_stat->decode_time_us =
316 GetElapsedTimeMicroseconds(frame_stat->decode_start_ns, decode_stop_ns);
brandtr17b958c2017-03-07 01:41:43 -0800317 frame_stat->decoding_successful = true;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000318
brandtr17b958c2017-03-07 01:41:43 -0800319 // Check if the codecs have resized the frame since previously decoded frame.
320 if (frame_number > 0) {
brandtrb57f4262017-08-30 06:29:51 -0700321 RTC_CHECK_GE(last_decoded_frame_num_, 0);
brandtr8935d972017-09-06 01:53:22 -0700322 const FrameStatistic* last_decoded_frame_stat =
323 stats_->GetFrame(last_decoded_frame_num_);
brandtr17b958c2017-03-07 01:41:43 -0800324 if (static_cast<int>(image.width()) !=
brandtr8935d972017-09-06 01:53:22 -0700325 last_decoded_frame_stat->decoded_width ||
brandtr17b958c2017-03-07 01:41:43 -0800326 static_cast<int>(image.height()) !=
brandtr8935d972017-09-06 01:53:22 -0700327 last_decoded_frame_stat->decoded_height) {
brandtrb57f4262017-08-30 06:29:51 -0700328 RTC_CHECK_GE(rate_update_index_, 0);
329 ++num_spatial_resizes_[rate_update_index_];
brandtr17b958c2017-03-07 01:41:43 -0800330 }
marpan@webrtc.orgf4c2de92012-06-05 21:07:28 +0000331 }
brandtr17b958c2017-03-07 01:41:43 -0800332 // Ensure strict monotonicity.
333 RTC_CHECK_GT(frame_number, last_decoded_frame_num_);
334 last_decoded_frame_num_ = frame_number;
335
Åsa Perssonf0c44672017-10-24 16:03:39 +0200336 // If the frame size is different from the original size, scale back to the
337 // original size. This is needed for the PSNR and SSIM calculations.
338 rtc::Buffer buffer;
339 ExtractBufferWithSize(image, config_.codec_settings.width,
340 config_.codec_settings.height, &buffer);
341 WriteDecodedFrameToFile(&buffer);
Niels Möller718a7632016-06-13 13:06:01 +0200342
Åsa Perssonf0c44672017-10-24 16:03:39 +0200343 last_decoded_frame_buffer_ = std::move(buffer);
344}
brandtr17b958c2017-03-07 01:41:43 -0800345
Åsa Perssonf0c44672017-10-24 16:03:39 +0200346void VideoProcessor::WriteDecodedFrameToFile(rtc::Buffer* buffer) {
347 RTC_DCHECK_EQ(buffer->size(), analysis_frame_writer_->FrameLength());
348 RTC_CHECK(analysis_frame_writer_->WriteFrame(buffer->data()));
brandtr17b958c2017-03-07 01:41:43 -0800349 if (decoded_frame_writer_) {
Åsa Perssonf0c44672017-10-24 16:03:39 +0200350 RTC_DCHECK_EQ(buffer->size(), decoded_frame_writer_->FrameLength());
351 RTC_CHECK(decoded_frame_writer_->WriteFrame(buffer->data()));
brandtr17b958c2017-03-07 01:41:43 -0800352 }
Åsa Perssonf0c44672017-10-24 16:03:39 +0200353}
brandtr17b958c2017-03-07 01:41:43 -0800354
Åsa Perssonf0c44672017-10-24 16:03:39 +0200355bool VideoProcessor::ExcludeFrame(const EncodedImage& encoded_image) {
356 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
357 if (encoded_image._frameType != kVideoFrameKey) {
358 return false;
359 }
360 bool exclude_frame = false;
361 switch (config_.exclude_frame_types) {
362 case kExcludeOnlyFirstKeyFrame:
363 if (!first_key_frame_has_been_excluded_) {
364 first_key_frame_has_been_excluded_ = true;
365 exclude_frame = true;
366 }
367 break;
368 case kExcludeAllKeyFrames:
369 exclude_frame = true;
370 break;
371 default:
372 RTC_NOTREACHED();
373 }
374 return exclude_frame;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000375}
376
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000377} // namespace test
378} // namespace webrtc