blob: b1b038f455ea3340bbcf6666d97cd9c3d06ab4bd [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 const TestConfig& config,
101 Stats* stats,
brandtrc4095522017-08-07 08:12:33 -0700102 IvfFileWriter* encoded_frame_writer,
103 FrameWriter* decoded_frame_writer)
Åsa Perssonf0c44672017-10-24 16:03:39 +0200104 : config_(config),
brandtr07734a52017-08-08 08:35:53 -0700105 encoder_(encoder),
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000106 decoder_(decoder),
brandtr07734a52017-08-08 08:35:53 -0700107 bitrate_allocator_(CreateBitrateAllocator(&config_)),
brandtrbdd555c2017-08-21 01:34:04 -0700108 encode_callback_(this),
109 decode_callback_(this),
brandtraebc61e2017-02-28 07:13:47 -0800110 analysis_frame_reader_(analysis_frame_reader),
brandtrb78bc752017-02-22 01:26:59 -0800111 encoded_frame_writer_(encoded_frame_writer),
112 decoded_frame_writer_(decoded_frame_writer),
Sergey Silkin3be2a552018-01-17 15:11:44 +0100113 last_inputed_frame_num_(0),
114 last_encoded_frame_num_(0),
115 last_decoded_frame_num_(0),
116 num_encoded_frames_(0),
117 num_decoded_frames_(0),
brandtrbdd555c2017-08-21 01:34:04 -0700118 last_decoded_frame_buffer_(analysis_frame_reader->FrameLength()),
Sergey Silkin3be2a552018-01-17 15:11:44 +0100119 stats_(stats) {
Erik Språng08127a92016-11-16 16:41:30 +0100120 RTC_DCHECK(encoder);
121 RTC_DCHECK(decoder);
brandtrb78bc752017-02-22 01:26:59 -0800122 RTC_DCHECK(analysis_frame_reader);
Erik Språng08127a92016-11-16 16:41:30 +0100123 RTC_DCHECK(stats);
brandtr17b958c2017-03-07 01:41:43 -0800124
brandtrbea36fd2017-08-07 03:36:54 -0700125 // Setup required callbacks for the encoder and decoder.
brandtrbdd555c2017-08-21 01:34:04 -0700126 RTC_CHECK_EQ(encoder_->RegisterEncodeCompleteCallback(&encode_callback_),
Åsa Perssonf0c44672017-10-24 16:03:39 +0200127 WEBRTC_VIDEO_CODEC_OK);
brandtrbdd555c2017-08-21 01:34:04 -0700128 RTC_CHECK_EQ(decoder_->RegisterDecodeCompleteCallback(&decode_callback_),
Åsa Perssonf0c44672017-10-24 16:03:39 +0200129 WEBRTC_VIDEO_CODEC_OK);
asapersson654d54c2017-02-10 00:16:07 -0800130
brandtraebc61e2017-02-28 07:13:47 -0800131 // Initialize the encoder and decoder.
Sergey Silkin1723cf92018-01-22 15:49:55 +0100132 RTC_CHECK_EQ(encoder_->InitEncode(&config_.codec_settings,
133 static_cast<int>(config_.NumberOfCores()),
134 config_.max_payload_size_bytes),
135 WEBRTC_VIDEO_CODEC_OK);
Sergey Silkin3be2a552018-01-17 15:11:44 +0100136 RTC_CHECK_EQ(decoder_->InitDecode(&config_.codec_settings,
137 static_cast<int>(config_.NumberOfCores())),
138 WEBRTC_VIDEO_CODEC_OK);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000139}
140
Åsa Perssonf0c44672017-10-24 16:03:39 +0200141VideoProcessor::~VideoProcessor() {
brandtrc8c59052017-08-21 06:44:16 -0700142 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
143
brandtr77920a42017-08-11 07:48:15 -0700144 RTC_CHECK_EQ(encoder_->Release(), WEBRTC_VIDEO_CODEC_OK);
145 RTC_CHECK_EQ(decoder_->Release(), WEBRTC_VIDEO_CODEC_OK);
146
brandtrbdd555c2017-08-21 01:34:04 -0700147 encoder_->RegisterEncodeCompleteCallback(nullptr);
148 decoder_->RegisterDecodeCompleteCallback(nullptr);
brandtr77920a42017-08-11 07:48:15 -0700149}
150
brandtr8935d972017-09-06 01:53:22 -0700151void VideoProcessor::ProcessFrame() {
brandtrc8c59052017-08-21 06:44:16 -0700152 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
Sergey Silkin3be2a552018-01-17 15:11:44 +0100153 const size_t frame_number = last_inputed_frame_num_++;
asapersson654d54c2017-02-10 00:16:07 -0800154
brandtrbdd555c2017-08-21 01:34:04 -0700155 // Get frame from file.
magjed3f075492017-06-01 10:02:26 -0700156 rtc::scoped_refptr<I420BufferInterface> buffer(
brandtrb78bc752017-02-22 01:26:59 -0800157 analysis_frame_reader_->ReadFrame());
brandtrbdd555c2017-08-21 01:34:04 -0700158 RTC_CHECK(buffer) << "Tried to read too many frames from the file.";
brandtrb57f4262017-08-30 06:29:51 -0700159 // Use the frame number as the basis for timestamp to identify frames. Let the
160 // first timestamp be non-zero, to not make the IvfFileWriter believe that we
161 // want to use capture timestamps in the IVF files.
Åsa Perssona6e7b882018-01-19 14:57:10 +0100162 // TODO(asapersson): Time stamps jump back if framerate increases.
Sergey Silkin3be2a552018-01-17 15:11:44 +0100163 const size_t rtp_timestamp = (frame_number + 1) * kVideoPayloadTypeFrequency /
164 config_.codec_settings.maxFramerate;
Sami Kalliomäki20b294c2017-12-12 16:37:16 +0100165 const int64_t render_time_ms = (frame_number + 1) * rtc::kNumMillisecsPerSec /
166 config_.codec_settings.maxFramerate;
Sergey Silkin3be2a552018-01-17 15:11:44 +0100167 input_frames_[frame_number] =
168 rtc::MakeUnique<VideoFrame>(buffer, static_cast<uint32_t>(rtp_timestamp),
169 render_time_ms, webrtc::kVideoRotation_0);
brandtr17b958c2017-03-07 01:41:43 -0800170
Sergey Silkin64eaa992017-11-17 14:47:32 +0100171 std::vector<FrameType> frame_types = config_.FrameTypeForFrame(frame_number);
brandtr17b958c2017-03-07 01:41:43 -0800172
173 // Create frame statistics object used for aggregation at end of test run.
Åsa Perssona6e7b882018-01-19 14:57:10 +0100174 FrameStatistic* frame_stat = stats_->AddFrame(rtp_timestamp);
brandtr17b958c2017-03-07 01:41:43 -0800175
176 // For the highest measurement accuracy of the encode time, the start/stop
177 // time recordings should wrap the Encode call as tightly as possible.
brandtr8935d972017-09-06 01:53:22 -0700178 frame_stat->encode_start_ns = rtc::TimeNanos();
brandtr17b958c2017-03-07 01:41:43 -0800179 frame_stat->encode_return_code =
Sergey Silkin64eaa992017-11-17 14:47:32 +0100180 encoder_->Encode(*input_frames_[frame_number], nullptr, &frame_types);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000181}
182
Sergey Silkin3be2a552018-01-17 15:11:44 +0100183void VideoProcessor::SetRates(size_t bitrate_kbps, size_t framerate_fps) {
brandtrc8c59052017-08-21 06:44:16 -0700184 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
Sergey Silkin3be2a552018-01-17 15:11:44 +0100185 config_.codec_settings.maxFramerate = static_cast<uint32_t>(framerate_fps);
186 bitrate_allocation_ = bitrate_allocator_->GetAllocation(
187 static_cast<uint32_t>(bitrate_kbps * 1000),
188 static_cast<uint32_t>(framerate_fps));
189 const int set_rates_result = encoder_->SetRateAllocation(
190 bitrate_allocation_, static_cast<uint32_t>(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 << ".";
brandtrbea36fd2017-08-07 03:36:54 -0700193}
194
brandtr45535622017-08-22 03:33:11 -0700195void VideoProcessor::FrameEncoded(webrtc::VideoCodecType codec,
196 const EncodedImage& encoded_image) {
brandtrc8c59052017-08-21 06:44:16 -0700197 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
198
brandtr32e0d262017-02-15 05:29:38 -0800199 // For the highest measurement accuracy of the encode time, the start/stop
200 // time recordings should wrap the Encode call as tightly as possible.
201 int64_t encode_stop_ns = rtc::TimeNanos();
202
Rasmus Brandtf7a35582017-10-24 10:16:33 +0200203 if (config_.encoded_frame_checker) {
204 config_.encoded_frame_checker->CheckEncodedFrame(codec, encoded_image);
205 }
brandtrb78bc752017-02-22 01:26:59 -0800206
Åsa Perssona6e7b882018-01-19 14:57:10 +0100207 FrameStatistic* frame_stat =
208 stats_->GetFrameWithTimestamp(encoded_image._timeStamp);
Sergey Silkin64eaa992017-11-17 14:47:32 +0100209
210 // Ensure strict monotonicity.
Åsa Perssona6e7b882018-01-19 14:57:10 +0100211 const size_t frame_number = frame_stat->frame_number;
Sergey Silkin3be2a552018-01-17 15:11:44 +0100212 if (num_encoded_frames_ > 0) {
213 RTC_CHECK_GT(frame_number, last_encoded_frame_num_);
214 }
Sergey Silkin64eaa992017-11-17 14:47:32 +0100215
brandtr17b958c2017-03-07 01:41:43 -0800216 last_encoded_frame_num_ = frame_number;
217
brandtr8935d972017-09-06 01:53:22 -0700218 // Update frame statistics.
brandtr8935d972017-09-06 01:53:22 -0700219 frame_stat->encode_time_us =
220 GetElapsedTimeMicroseconds(frame_stat->encode_start_ns, encode_stop_ns);
brandtr17b958c2017-03-07 01:41:43 -0800221 frame_stat->encoding_successful = true;
brandtr8935d972017-09-06 01:53:22 -0700222 frame_stat->encoded_frame_size_bytes = encoded_image._length;
brandtr17b958c2017-03-07 01:41:43 -0800223 frame_stat->frame_type = encoded_image._frameType;
Sergey Silkin3be2a552018-01-17 15:11:44 +0100224 frame_stat->temporal_layer_idx = config_.TemporalLayerForFrame(frame_number);
brandtr17b958c2017-03-07 01:41:43 -0800225 frame_stat->qp = encoded_image.qp_;
Sergey Silkin3be2a552018-01-17 15:11:44 +0100226 frame_stat->target_bitrate_kbps =
227 bitrate_allocation_.GetSpatialLayerSum(0) / 1000;
Sergey Silkin3be2a552018-01-17 15:11:44 +0100228 frame_stat->max_nalu_size_bytes = GetMaxNaluSizeBytes(encoded_image, config_);
ssilkin612f8582017-09-28 09:23:17 -0700229
brandtr32e0d262017-02-15 05:29:38 -0800230 // For the highest measurement accuracy of the decode time, the start/stop
231 // time recordings should wrap the Decode call as tightly as possible.
brandtr8935d972017-09-06 01:53:22 -0700232 frame_stat->decode_start_ns = rtc::TimeNanos();
brandtr17b958c2017-03-07 01:41:43 -0800233 frame_stat->decode_return_code =
Sergey Silkin1723cf92018-01-22 15:49:55 +0100234 decoder_->Decode(encoded_image, false, nullptr);
brandtr8bc93852017-02-15 05:19:51 -0800235
brandtr8935d972017-09-06 01:53:22 -0700236 if (encoded_frame_writer_) {
237 RTC_CHECK(encoded_frame_writer_->WriteFrame(encoded_image, codec));
238 }
Sergey Silkin3be2a552018-01-17 15:11:44 +0100239
240 ++num_encoded_frames_;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000241}
242
Sergey Silkin64eaa992017-11-17 14:47:32 +0100243void VideoProcessor::FrameDecoded(const VideoFrame& decoded_frame) {
brandtrc8c59052017-08-21 06:44:16 -0700244 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
245
brandtr32e0d262017-02-15 05:29:38 -0800246 // For the highest measurement accuracy of the decode time, the start/stop
247 // time recordings should wrap the Decode call as tightly as possible.
Niels Möllerd28db7f2016-05-10 16:31:47 +0200248 int64_t decode_stop_ns = rtc::TimeNanos();
brandtr8bc93852017-02-15 05:19:51 -0800249
brandtr8935d972017-09-06 01:53:22 -0700250 // Update frame statistics.
Åsa Perssona6e7b882018-01-19 14:57:10 +0100251 FrameStatistic* frame_stat =
252 stats_->GetFrameWithTimestamp(decoded_frame.timestamp());
Sergey Silkin64eaa992017-11-17 14:47:32 +0100253 frame_stat->decoded_width = decoded_frame.width();
254 frame_stat->decoded_height = decoded_frame.height();
brandtr8935d972017-09-06 01:53:22 -0700255 frame_stat->decode_time_us =
256 GetElapsedTimeMicroseconds(frame_stat->decode_start_ns, decode_stop_ns);
brandtr17b958c2017-03-07 01:41:43 -0800257 frame_stat->decoding_successful = true;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000258
Sergey Silkin64eaa992017-11-17 14:47:32 +0100259 // Ensure strict monotonicity.
Åsa Perssona6e7b882018-01-19 14:57:10 +0100260 const size_t frame_number = frame_stat->frame_number;
Sergey Silkin3be2a552018-01-17 15:11:44 +0100261 if (num_decoded_frames_ > 0) {
262 RTC_CHECK_GT(frame_number, last_decoded_frame_num_);
263 }
Sergey Silkin64eaa992017-11-17 14:47:32 +0100264
brandtr17b958c2017-03-07 01:41:43 -0800265 // Check if the codecs have resized the frame since previously decoded frame.
266 if (frame_number > 0) {
Sergey Silkin3be2a552018-01-17 15:11:44 +0100267 if (decoded_frame_writer_ && num_decoded_frames_ > 0) {
Sergey Silkin64eaa992017-11-17 14:47:32 +0100268 // For dropped/lost frames, write out the last decoded frame to make it
269 // look like a freeze at playback.
Sergey Silkin3be2a552018-01-17 15:11:44 +0100270 const size_t num_dropped_frames =
271 frame_number - last_decoded_frame_num_ - 1;
272 for (size_t i = 0; i < num_dropped_frames; i++) {
Sergey Silkin64eaa992017-11-17 14:47:32 +0100273 WriteDecodedFrameToFile(&last_decoded_frame_buffer_);
274 }
275 }
marpan@webrtc.orgf4c2de92012-06-05 21:07:28 +0000276 }
brandtr17b958c2017-03-07 01:41:43 -0800277 last_decoded_frame_num_ = frame_number;
278
Sergey Silkin64eaa992017-11-17 14:47:32 +0100279 // Skip quality metrics calculation to not affect CPU usage.
280 if (!config_.measure_cpu) {
281 frame_stat->psnr =
282 I420PSNR(input_frames_[frame_number].get(), &decoded_frame);
283 frame_stat->ssim =
284 I420SSIM(input_frames_[frame_number].get(), &decoded_frame);
285 }
Niels Möller718a7632016-06-13 13:06:01 +0200286
Sergey Silkin64eaa992017-11-17 14:47:32 +0100287 // Delay erasing of input frames by one frame. The current frame might
288 // still be needed for other simulcast stream or spatial layer.
Sergey Silkin3be2a552018-01-17 15:11:44 +0100289 if (frame_number > 0) {
290 auto input_frame_erase_to = input_frames_.lower_bound(frame_number - 1);
Sergey Silkin64eaa992017-11-17 14:47:32 +0100291 input_frames_.erase(input_frames_.begin(), input_frame_erase_to);
292 }
293
294 if (decoded_frame_writer_) {
295 ExtractBufferWithSize(decoded_frame, config_.codec_settings.width,
296 config_.codec_settings.height,
297 &last_decoded_frame_buffer_);
298 WriteDecodedFrameToFile(&last_decoded_frame_buffer_);
299 }
Sergey Silkin3be2a552018-01-17 15:11:44 +0100300
301 ++num_decoded_frames_;
Åsa Perssonf0c44672017-10-24 16:03:39 +0200302}
brandtr17b958c2017-03-07 01:41:43 -0800303
Åsa Perssonf0c44672017-10-24 16:03:39 +0200304void VideoProcessor::WriteDecodedFrameToFile(rtc::Buffer* buffer) {
Sergey Silkin64eaa992017-11-17 14:47:32 +0100305 RTC_DCHECK_EQ(buffer->size(), decoded_frame_writer_->FrameLength());
306 RTC_CHECK(decoded_frame_writer_->WriteFrame(buffer->data()));
Åsa Perssonf0c44672017-10-24 16:03:39 +0200307}
brandtr17b958c2017-03-07 01:41:43 -0800308
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000309} // namespace test
310} // namespace webrtc