blob: fb79308054da9dc795bafd01e198394a34be87e5 [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)
50 return rtc::Optional<size_t>();
51
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
62 return rtc::Optional<size_t>(max_length);
63}
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,
102 FrameWriter* analysis_frame_writer,
103 PacketManipulator* packet_manipulator,
104 const TestConfig& config,
105 Stats* stats,
brandtrc4095522017-08-07 08:12:33 -0700106 IvfFileWriter* encoded_frame_writer,
107 FrameWriter* decoded_frame_writer)
Åsa Perssonf0c44672017-10-24 16:03:39 +0200108 : config_(config),
brandtr07734a52017-08-08 08:35:53 -0700109 encoder_(encoder),
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000110 decoder_(decoder),
brandtr07734a52017-08-08 08:35:53 -0700111 bitrate_allocator_(CreateBitrateAllocator(&config_)),
brandtrbdd555c2017-08-21 01:34:04 -0700112 encode_callback_(this),
113 decode_callback_(this),
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000114 packet_manipulator_(packet_manipulator),
brandtraebc61e2017-02-28 07:13:47 -0800115 analysis_frame_reader_(analysis_frame_reader),
116 analysis_frame_writer_(analysis_frame_writer),
brandtrb78bc752017-02-22 01:26:59 -0800117 encoded_frame_writer_(encoded_frame_writer),
118 decoded_frame_writer_(decoded_frame_writer),
brandtr8935d972017-09-06 01:53:22 -0700119 last_inputed_frame_num_(-1),
brandtr17b958c2017-03-07 01:41:43 -0800120 last_encoded_frame_num_(-1),
121 last_decoded_frame_num_(-1),
122 first_key_frame_has_been_excluded_(false),
brandtrbdd555c2017-08-21 01:34:04 -0700123 last_decoded_frame_buffer_(analysis_frame_reader->FrameLength()),
brandtraebc61e2017-02-28 07:13:47 -0800124 stats_(stats),
brandtrb57f4262017-08-30 06:29:51 -0700125 rate_update_index_(-1) {
Erik Språng08127a92016-11-16 16:41:30 +0100126 RTC_DCHECK(encoder);
127 RTC_DCHECK(decoder);
brandtraebc61e2017-02-28 07:13:47 -0800128 RTC_DCHECK(packet_manipulator);
brandtrb78bc752017-02-22 01:26:59 -0800129 RTC_DCHECK(analysis_frame_reader);
130 RTC_DCHECK(analysis_frame_writer);
Erik Språng08127a92016-11-16 16:41:30 +0100131 RTC_DCHECK(stats);
brandtr17b958c2017-03-07 01:41:43 -0800132
brandtrbea36fd2017-08-07 03:36:54 -0700133 // Setup required callbacks for the encoder and decoder.
brandtrbdd555c2017-08-21 01:34:04 -0700134 RTC_CHECK_EQ(encoder_->RegisterEncodeCompleteCallback(&encode_callback_),
Åsa Perssonf0c44672017-10-24 16:03:39 +0200135 WEBRTC_VIDEO_CODEC_OK);
brandtrbdd555c2017-08-21 01:34:04 -0700136 RTC_CHECK_EQ(decoder_->RegisterDecodeCompleteCallback(&decode_callback_),
Åsa Perssonf0c44672017-10-24 16:03:39 +0200137 WEBRTC_VIDEO_CODEC_OK);
asapersson654d54c2017-02-10 00:16:07 -0800138
brandtraebc61e2017-02-28 07:13:47 -0800139 // Initialize the encoder and decoder.
asapersson654d54c2017-02-10 00:16:07 -0800140 RTC_CHECK_EQ(
Åsa Persson2d27fb52017-10-19 14:05:50 +0200141 encoder_->InitEncode(&config_.codec_settings, config_.NumberOfCores(),
asapersson654d54c2017-02-10 00:16:07 -0800142 config_.networking_config.max_payload_size_in_bytes),
Åsa Perssonf0c44672017-10-24 16:03:39 +0200143 WEBRTC_VIDEO_CODEC_OK);
Åsa Persson2d27fb52017-10-19 14:05:50 +0200144 RTC_CHECK_EQ(
145 decoder_->InitDecode(&config_.codec_settings, config_.NumberOfCores()),
Åsa Perssonf0c44672017-10-24 16:03:39 +0200146 WEBRTC_VIDEO_CODEC_OK);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000147}
148
Åsa Perssonf0c44672017-10-24 16:03:39 +0200149VideoProcessor::~VideoProcessor() {
brandtrc8c59052017-08-21 06:44:16 -0700150 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
151
brandtr77920a42017-08-11 07:48:15 -0700152 RTC_CHECK_EQ(encoder_->Release(), WEBRTC_VIDEO_CODEC_OK);
153 RTC_CHECK_EQ(decoder_->Release(), WEBRTC_VIDEO_CODEC_OK);
154
brandtrbdd555c2017-08-21 01:34:04 -0700155 encoder_->RegisterEncodeCompleteCallback(nullptr);
156 decoder_->RegisterDecodeCompleteCallback(nullptr);
brandtr77920a42017-08-11 07:48:15 -0700157}
158
brandtr8935d972017-09-06 01:53:22 -0700159void VideoProcessor::ProcessFrame() {
brandtrc8c59052017-08-21 06:44:16 -0700160 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
brandtr8935d972017-09-06 01:53:22 -0700161 ++last_inputed_frame_num_;
asapersson654d54c2017-02-10 00:16:07 -0800162
brandtrbdd555c2017-08-21 01:34:04 -0700163 // Get frame from file.
magjed3f075492017-06-01 10:02:26 -0700164 rtc::scoped_refptr<I420BufferInterface> buffer(
brandtrb78bc752017-02-22 01:26:59 -0800165 analysis_frame_reader_->ReadFrame());
brandtrbdd555c2017-08-21 01:34:04 -0700166 RTC_CHECK(buffer) << "Tried to read too many frames from the file.";
brandtrb57f4262017-08-30 06:29:51 -0700167 // Use the frame number as the basis for timestamp to identify frames. Let the
168 // first timestamp be non-zero, to not make the IvfFileWriter believe that we
169 // want to use capture timestamps in the IVF files.
brandtr8935d972017-09-06 01:53:22 -0700170 const uint32_t rtp_timestamp = (last_inputed_frame_num_ + 1) *
171 kRtpClockRateHz /
brandtrb57f4262017-08-30 06:29:51 -0700172 config_.codec_settings.maxFramerate;
brandtr8935d972017-09-06 01:53:22 -0700173 rtp_timestamp_to_frame_num_[rtp_timestamp] = last_inputed_frame_num_;
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
Åsa Perssone87cfe22017-10-26 15:33:18 +0200177 std::vector<FrameType> frame_types =
178 config_.FrameTypeForFrame(last_inputed_frame_num_);
brandtr17b958c2017-03-07 01:41:43 -0800179
180 // Create frame statistics object used for aggregation at end of test run.
brandtr8935d972017-09-06 01:53:22 -0700181 FrameStatistic* frame_stat = stats_->AddFrame();
brandtr17b958c2017-03-07 01:41:43 -0800182
183 // For the highest measurement accuracy of the encode time, the start/stop
184 // time recordings should wrap the Encode call as tightly as possible.
brandtr8935d972017-09-06 01:53:22 -0700185 frame_stat->encode_start_ns = rtc::TimeNanos();
brandtr17b958c2017-03-07 01:41:43 -0800186 frame_stat->encode_return_code =
187 encoder_->Encode(source_frame, nullptr, &frame_types);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000188}
189
brandtrbdd555c2017-08-21 01:34:04 -0700190void VideoProcessor::SetRates(int bitrate_kbps, int framerate_fps) {
brandtrc8c59052017-08-21 06:44:16 -0700191 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
brandtrbdd555c2017-08-21 01:34:04 -0700192 config_.codec_settings.maxFramerate = framerate_fps;
brandtrbea36fd2017-08-07 03:36:54 -0700193 int set_rates_result = encoder_->SetRateAllocation(
brandtrbdd555c2017-08-21 01:34:04 -0700194 bitrate_allocator_->GetAllocation(bitrate_kbps * 1000, framerate_fps),
195 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 << ".";
brandtrb57f4262017-08-30 06:29:51 -0700198 ++rate_update_index_;
199 num_dropped_frames_.push_back(0);
200 num_spatial_resizes_.push_back(0);
brandtrbea36fd2017-08-07 03:36:54 -0700201}
202
brandtrb57f4262017-08-30 06:29:51 -0700203std::vector<int> VideoProcessor::NumberDroppedFramesPerRateUpdate() const {
brandtrc8c59052017-08-21 06:44:16 -0700204 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
brandtrbea36fd2017-08-07 03:36:54 -0700205 return num_dropped_frames_;
206}
207
brandtrb57f4262017-08-30 06:29:51 -0700208std::vector<int> VideoProcessor::NumberSpatialResizesPerRateUpdate() const {
brandtrc8c59052017-08-21 06:44:16 -0700209 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
brandtrbea36fd2017-08-07 03:36:54 -0700210 return num_spatial_resizes_;
211}
212
brandtr45535622017-08-22 03:33:11 -0700213void VideoProcessor::FrameEncoded(webrtc::VideoCodecType codec,
214 const EncodedImage& encoded_image) {
brandtrc8c59052017-08-21 06:44:16 -0700215 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
216
brandtr32e0d262017-02-15 05:29:38 -0800217 // For the highest measurement accuracy of the encode time, the start/stop
218 // time recordings should wrap the Encode call as tightly as possible.
219 int64_t encode_stop_ns = rtc::TimeNanos();
220
Rasmus Brandtf7a35582017-10-24 10:16:33 +0200221 if (config_.encoded_frame_checker) {
222 config_.encoded_frame_checker->CheckEncodedFrame(codec, encoded_image);
223 }
brandtrb78bc752017-02-22 01:26:59 -0800224
brandtrb57f4262017-08-30 06:29:51 -0700225 // Check for dropped frames.
226 const int frame_number =
227 rtp_timestamp_to_frame_num_[encoded_image._timeStamp];
brandtr17b958c2017-03-07 01:41:43 -0800228 bool last_frame_missing = false;
229 if (frame_number > 0) {
230 RTC_DCHECK_GE(last_encoded_frame_num_, 0);
231 int num_dropped_from_last_encode =
232 frame_number - last_encoded_frame_num_ - 1;
233 RTC_DCHECK_GE(num_dropped_from_last_encode, 0);
brandtrb57f4262017-08-30 06:29:51 -0700234 RTC_CHECK_GE(rate_update_index_, 0);
235 num_dropped_frames_[rate_update_index_] += num_dropped_from_last_encode;
brandtr17b958c2017-03-07 01:41:43 -0800236 if (num_dropped_from_last_encode > 0) {
237 // For dropped frames, we write out the last decoded frame to avoid
238 // getting out of sync for the computation of PSNR and SSIM.
239 for (int i = 0; i < num_dropped_from_last_encode; i++) {
Åsa Perssonf0c44672017-10-24 16:03:39 +0200240 WriteDecodedFrameToFile(&last_decoded_frame_buffer_);
brandtrb78bc752017-02-22 01:26:59 -0800241 }
marpan@webrtc.orgf4c2de92012-06-05 21:07:28 +0000242 }
brandtr8935d972017-09-06 01:53:22 -0700243 const FrameStatistic* last_encoded_frame_stat =
244 stats_->GetFrame(last_encoded_frame_num_);
245 last_frame_missing = (last_encoded_frame_stat->manipulated_length == 0);
brandtr17b958c2017-03-07 01:41:43 -0800246 }
247 // Ensure strict monotonicity.
248 RTC_CHECK_GT(frame_number, last_encoded_frame_num_);
249 last_encoded_frame_num_ = frame_number;
250
brandtr8935d972017-09-06 01:53:22 -0700251 // Update frame statistics.
252 FrameStatistic* frame_stat = stats_->GetFrame(frame_number);
253 frame_stat->encode_time_us =
254 GetElapsedTimeMicroseconds(frame_stat->encode_start_ns, encode_stop_ns);
brandtr17b958c2017-03-07 01:41:43 -0800255 frame_stat->encoding_successful = true;
brandtr8935d972017-09-06 01:53:22 -0700256 frame_stat->encoded_frame_size_bytes = encoded_image._length;
brandtr17b958c2017-03-07 01:41:43 -0800257 frame_stat->frame_type = encoded_image._frameType;
258 frame_stat->qp = encoded_image.qp_;
brandtr8935d972017-09-06 01:53:22 -0700259 frame_stat->bitrate_kbps = static_cast<int>(
brandtr07734a52017-08-08 08:35:53 -0700260 encoded_image._length * config_.codec_settings.maxFramerate * 8 / 1000);
brandtr17b958c2017-03-07 01:41:43 -0800261 frame_stat->total_packets =
philipelcce46fc2015-12-21 03:04:49 -0800262 encoded_image._length / config_.networking_config.packet_size_in_bytes +
263 1;
ssilkin612f8582017-09-28 09:23:17 -0700264 frame_stat->max_nalu_length = GetMaxNaluLength(encoded_image, config_);
265
Åsa Perssonf0c44672017-10-24 16:03:39 +0200266 // Make a raw copy of |encoded_image| to feed to the decoder.
hbos3fe2c6a2016-01-22 00:07:12 -0800267 size_t copied_buffer_size = encoded_image._length +
268 EncodedImage::GetBufferPaddingBytes(codec);
kwiberg3f55dea2016-02-29 05:51:59 -0800269 std::unique_ptr<uint8_t[]> copied_buffer(new uint8_t[copied_buffer_size]);
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000270 memcpy(copied_buffer.get(), encoded_image._buffer, encoded_image._length);
Åsa Perssonf0c44672017-10-24 16:03:39 +0200271 EncodedImage copied_image = encoded_image;
hbos3fe2c6a2016-01-22 00:07:12 -0800272 copied_image._size = copied_buffer_size;
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000273 copied_image._buffer = copied_buffer.get();
hbosbab934b2016-01-27 01:36:03 -0800274
Åsa Perssonf0c44672017-10-24 16:03:39 +0200275 // Simulate packet loss.
276 if (!ExcludeFrame(copied_image)) {
brandtr17b958c2017-03-07 01:41:43 -0800277 frame_stat->packets_dropped =
philipelcce46fc2015-12-21 03:04:49 -0800278 packet_manipulator_->ManipulatePackets(&copied_image);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000279 }
brandtr8935d972017-09-06 01:53:22 -0700280 frame_stat->manipulated_length = copied_image._length;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000281
brandtr32e0d262017-02-15 05:29:38 -0800282 // For the highest measurement accuracy of the decode time, the start/stop
283 // time recordings should wrap the Decode call as tightly as possible.
brandtr8935d972017-09-06 01:53:22 -0700284 frame_stat->decode_start_ns = rtc::TimeNanos();
brandtr17b958c2017-03-07 01:41:43 -0800285 frame_stat->decode_return_code =
286 decoder_->Decode(copied_image, last_frame_missing, nullptr);
brandtr8bc93852017-02-15 05:19:51 -0800287
brandtr17b958c2017-03-07 01:41:43 -0800288 if (frame_stat->decode_return_code != WEBRTC_VIDEO_CODEC_OK) {
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000289 // Write the last successful frame the output file to avoid getting it out
brandtr8bc93852017-02-15 05:19:51 -0800290 // of sync with the source file for SSIM and PSNR comparisons.
Åsa Perssonf0c44672017-10-24 16:03:39 +0200291 WriteDecodedFrameToFile(&last_decoded_frame_buffer_);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000292 }
brandtr8935d972017-09-06 01:53:22 -0700293
294 if (encoded_frame_writer_) {
295 RTC_CHECK(encoded_frame_writer_->WriteFrame(encoded_image, codec));
296 }
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000297}
298
brandtrc4095522017-08-07 08:12:33 -0700299void VideoProcessor::FrameDecoded(const VideoFrame& image) {
brandtrc8c59052017-08-21 06:44:16 -0700300 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
301
brandtr32e0d262017-02-15 05:29:38 -0800302 // For the highest measurement accuracy of the decode time, the start/stop
303 // time recordings should wrap the Decode call as tightly as possible.
Niels Möllerd28db7f2016-05-10 16:31:47 +0200304 int64_t decode_stop_ns = rtc::TimeNanos();
brandtr8bc93852017-02-15 05:19:51 -0800305
brandtr8935d972017-09-06 01:53:22 -0700306 // Update frame statistics.
brandtrb57f4262017-08-30 06:29:51 -0700307 const int frame_number = rtp_timestamp_to_frame_num_[image.timestamp()];
brandtr8935d972017-09-06 01:53:22 -0700308 FrameStatistic* frame_stat = stats_->GetFrame(frame_number);
309 frame_stat->decoded_width = image.width();
310 frame_stat->decoded_height = image.height();
311 frame_stat->decode_time_us =
312 GetElapsedTimeMicroseconds(frame_stat->decode_start_ns, decode_stop_ns);
brandtr17b958c2017-03-07 01:41:43 -0800313 frame_stat->decoding_successful = true;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000314
brandtr17b958c2017-03-07 01:41:43 -0800315 // Check if the codecs have resized the frame since previously decoded frame.
316 if (frame_number > 0) {
brandtrb57f4262017-08-30 06:29:51 -0700317 RTC_CHECK_GE(last_decoded_frame_num_, 0);
brandtr8935d972017-09-06 01:53:22 -0700318 const FrameStatistic* last_decoded_frame_stat =
319 stats_->GetFrame(last_decoded_frame_num_);
Åsa Perssone87cfe22017-10-26 15:33:18 +0200320 if (image.width() != last_decoded_frame_stat->decoded_width ||
321 image.height() != last_decoded_frame_stat->decoded_height) {
brandtrb57f4262017-08-30 06:29:51 -0700322 RTC_CHECK_GE(rate_update_index_, 0);
323 ++num_spatial_resizes_[rate_update_index_];
brandtr17b958c2017-03-07 01:41:43 -0800324 }
marpan@webrtc.orgf4c2de92012-06-05 21:07:28 +0000325 }
brandtr17b958c2017-03-07 01:41:43 -0800326 // Ensure strict monotonicity.
327 RTC_CHECK_GT(frame_number, last_decoded_frame_num_);
328 last_decoded_frame_num_ = frame_number;
329
Åsa Perssonf0c44672017-10-24 16:03:39 +0200330 // If the frame size is different from the original size, scale back to the
331 // original size. This is needed for the PSNR and SSIM calculations.
332 rtc::Buffer buffer;
333 ExtractBufferWithSize(image, config_.codec_settings.width,
334 config_.codec_settings.height, &buffer);
335 WriteDecodedFrameToFile(&buffer);
Niels Möller718a7632016-06-13 13:06:01 +0200336
Åsa Perssonf0c44672017-10-24 16:03:39 +0200337 last_decoded_frame_buffer_ = std::move(buffer);
338}
brandtr17b958c2017-03-07 01:41:43 -0800339
Åsa Perssonf0c44672017-10-24 16:03:39 +0200340void VideoProcessor::WriteDecodedFrameToFile(rtc::Buffer* buffer) {
341 RTC_DCHECK_EQ(buffer->size(), analysis_frame_writer_->FrameLength());
342 RTC_CHECK(analysis_frame_writer_->WriteFrame(buffer->data()));
brandtr17b958c2017-03-07 01:41:43 -0800343 if (decoded_frame_writer_) {
Åsa Perssonf0c44672017-10-24 16:03:39 +0200344 RTC_DCHECK_EQ(buffer->size(), decoded_frame_writer_->FrameLength());
345 RTC_CHECK(decoded_frame_writer_->WriteFrame(buffer->data()));
brandtr17b958c2017-03-07 01:41:43 -0800346 }
Åsa Perssonf0c44672017-10-24 16:03:39 +0200347}
brandtr17b958c2017-03-07 01:41:43 -0800348
Åsa Perssonf0c44672017-10-24 16:03:39 +0200349bool VideoProcessor::ExcludeFrame(const EncodedImage& encoded_image) {
350 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
351 if (encoded_image._frameType != kVideoFrameKey) {
352 return false;
353 }
354 bool exclude_frame = false;
355 switch (config_.exclude_frame_types) {
356 case kExcludeOnlyFirstKeyFrame:
357 if (!first_key_frame_has_been_excluded_) {
358 first_key_frame_has_been_excluded_ = true;
359 exclude_frame = true;
360 }
361 break;
362 case kExcludeAllKeyFrames:
363 exclude_frame = true;
364 break;
365 default:
366 RTC_NOTREACHED();
367 }
368 return exclude_frame;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000369}
370
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000371} // namespace test
372} // namespace webrtc