blob: 7e1047f458d7825d6bb78319d58cf8130fa7b2e8 [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 {
Åsa Persson91af24a2018-01-24 17:20:18 +010032const int kMsToRtpTimestamp = kVideoPayloadTypeFrequency / 1000;
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
Sergey Silkin3be2a552018-01-17 15:11:44 +010046size_t GetMaxNaluSizeBytes(const EncodedImage& encoded_frame,
47 const TestConfig& config) {
ssilkin612f8582017-09-28 09:23:17 -070048 if (config.codec_settings.codecType != kVideoCodecH264)
Sergey Silkin3be2a552018-01-17 15:11:44 +010049 return 0;
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 Silkin3be2a552018-01-17 15:11:44 +010057 size_t max_size = 0;
ssilkin612f8582017-09-28 09:23:17 -070058 for (const webrtc::H264::NaluIndex& index : nalu_indices)
Sergey Silkin3be2a552018-01-17 15:11:44 +010059 max_size = std::max(max_size, index.payload_size);
ssilkin612f8582017-09-28 09:23:17 -070060
Sergey Silkin3be2a552018-01-17 15:11:44 +010061 return max_size;
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 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),
brandtraebc61e2017-02-28 07:13:47 -0800111 analysis_frame_reader_(analysis_frame_reader),
brandtrb78bc752017-02-22 01:26:59 -0800112 encoded_frame_writer_(encoded_frame_writer),
113 decoded_frame_writer_(decoded_frame_writer),
Sergey Silkin3be2a552018-01-17 15:11:44 +0100114 last_inputed_frame_num_(0),
115 last_encoded_frame_num_(0),
116 last_decoded_frame_num_(0),
117 num_encoded_frames_(0),
118 num_decoded_frames_(0),
brandtrbdd555c2017-08-21 01:34:04 -0700119 last_decoded_frame_buffer_(analysis_frame_reader->FrameLength()),
Sergey Silkin3be2a552018-01-17 15:11:44 +0100120 stats_(stats) {
Erik Språng08127a92016-11-16 16:41:30 +0100121 RTC_DCHECK(encoder);
122 RTC_DCHECK(decoder);
brandtrb78bc752017-02-22 01:26:59 -0800123 RTC_DCHECK(analysis_frame_reader);
Erik Språng08127a92016-11-16 16:41:30 +0100124 RTC_DCHECK(stats);
brandtr17b958c2017-03-07 01:41:43 -0800125
brandtrbea36fd2017-08-07 03:36:54 -0700126 // Setup required callbacks for the encoder and decoder.
brandtrbdd555c2017-08-21 01:34:04 -0700127 RTC_CHECK_EQ(encoder_->RegisterEncodeCompleteCallback(&encode_callback_),
Åsa Perssonf0c44672017-10-24 16:03:39 +0200128 WEBRTC_VIDEO_CODEC_OK);
brandtrbdd555c2017-08-21 01:34:04 -0700129 RTC_CHECK_EQ(decoder_->RegisterDecodeCompleteCallback(&decode_callback_),
Åsa Perssonf0c44672017-10-24 16:03:39 +0200130 WEBRTC_VIDEO_CODEC_OK);
asapersson654d54c2017-02-10 00:16:07 -0800131
brandtraebc61e2017-02-28 07:13:47 -0800132 // Initialize the encoder and decoder.
Sergey Silkin1723cf92018-01-22 15:49:55 +0100133 RTC_CHECK_EQ(encoder_->InitEncode(&config_.codec_settings,
134 static_cast<int>(config_.NumberOfCores()),
135 config_.max_payload_size_bytes),
136 WEBRTC_VIDEO_CODEC_OK);
Sergey Silkin3be2a552018-01-17 15:11:44 +0100137 RTC_CHECK_EQ(decoder_->InitDecode(&config_.codec_settings,
138 static_cast<int>(config_.NumberOfCores())),
139 WEBRTC_VIDEO_CODEC_OK);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000140}
141
Åsa Perssonf0c44672017-10-24 16:03:39 +0200142VideoProcessor::~VideoProcessor() {
brandtrc8c59052017-08-21 06:44:16 -0700143 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
144
brandtr77920a42017-08-11 07:48:15 -0700145 RTC_CHECK_EQ(encoder_->Release(), WEBRTC_VIDEO_CODEC_OK);
146 RTC_CHECK_EQ(decoder_->Release(), WEBRTC_VIDEO_CODEC_OK);
147
brandtrbdd555c2017-08-21 01:34:04 -0700148 encoder_->RegisterEncodeCompleteCallback(nullptr);
149 decoder_->RegisterDecodeCompleteCallback(nullptr);
brandtr77920a42017-08-11 07:48:15 -0700150}
151
brandtr8935d972017-09-06 01:53:22 -0700152void VideoProcessor::ProcessFrame() {
brandtrc8c59052017-08-21 06:44:16 -0700153 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
Sergey Silkin3be2a552018-01-17 15:11:44 +0100154 const size_t frame_number = last_inputed_frame_num_++;
asapersson654d54c2017-02-10 00:16:07 -0800155
brandtrbdd555c2017-08-21 01:34:04 -0700156 // Get frame from file.
magjed3f075492017-06-01 10:02:26 -0700157 rtc::scoped_refptr<I420BufferInterface> buffer(
brandtrb78bc752017-02-22 01:26:59 -0800158 analysis_frame_reader_->ReadFrame());
brandtrbdd555c2017-08-21 01:34:04 -0700159 RTC_CHECK(buffer) << "Tried to read too many frames from the file.";
Åsa Persson91af24a2018-01-24 17:20:18 +0100160
161 size_t rtp_timestamp =
162 (frame_number > 0) ? input_frames_[frame_number - 1]->timestamp() : 0;
163 rtp_timestamp +=
164 kVideoPayloadTypeFrequency / config_.codec_settings.maxFramerate;
165
166 input_frames_[frame_number] = rtc::MakeUnique<VideoFrame>(
167 buffer, static_cast<uint32_t>(rtp_timestamp),
168 static_cast<int64_t>(rtp_timestamp / kMsToRtpTimestamp),
169 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