blob: 27653db55b7877b800a53eb026a2e620619845ff [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"
Sergey Silkin10d9d592018-02-01 13:25:17 +010027#include "third_party/libyuv/include/libyuv/scale.h"
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000028
29namespace webrtc {
30namespace test {
31
brandtrb78bc752017-02-22 01:26:59 -080032namespace {
Åsa Persson91af24a2018-01-24 17:20:18 +010033const int kMsToRtpTimestamp = kVideoPayloadTypeFrequency / 1000;
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
Sergey Silkin3be2a552018-01-17 15:11:44 +010047size_t GetMaxNaluSizeBytes(const EncodedImage& encoded_frame,
48 const TestConfig& config) {
ssilkin612f8582017-09-28 09:23:17 -070049 if (config.codec_settings.codecType != kVideoCodecH264)
Sergey Silkin3be2a552018-01-17 15:11:44 +010050 return 0;
ssilkin612f8582017-09-28 09:23:17 -070051
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
Sergey Silkin3be2a552018-01-17 15:11:44 +010058 size_t max_size = 0;
ssilkin612f8582017-09-28 09:23:17 -070059 for (const webrtc::H264::NaluIndex& index : nalu_indices)
Sergey Silkin3be2a552018-01-17 15:11:44 +010060 max_size = std::max(max_size, index.payload_size);
ssilkin612f8582017-09-28 09:23:17 -070061
Sergey Silkin3be2a552018-01-17 15:11:44 +010062 return max_size;
ssilkin612f8582017-09-28 09:23:17 -070063}
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,
Sergey Silkin10d9d592018-02-01 13:25:17 +0100100 VideoDecoderList* decoders,
101 FrameReader* input_frame_reader,
brandtrc4095522017-08-07 08:12:33 -0700102 const TestConfig& config,
Sergey Silkin10d9d592018-02-01 13:25:17 +0100103 std::vector<Stats>* stats,
104 IvfFileWriterList* encoded_frame_writers,
105 FrameWriterList* decoded_frame_writers)
Åsa Perssonf0c44672017-10-24 16:03:39 +0200106 : config_(config),
Sergey Silkin10d9d592018-02-01 13:25:17 +0100107 num_simulcast_or_spatial_layers_(
108 std::max(config_.NumberOfSimulcastStreams(),
109 config_.NumberOfSpatialLayers())),
brandtr07734a52017-08-08 08:35:53 -0700110 encoder_(encoder),
Sergey Silkin10d9d592018-02-01 13:25:17 +0100111 decoders_(decoders),
brandtr07734a52017-08-08 08:35:53 -0700112 bitrate_allocator_(CreateBitrateAllocator(&config_)),
brandtrbdd555c2017-08-21 01:34:04 -0700113 encode_callback_(this),
114 decode_callback_(this),
Sergey Silkin10d9d592018-02-01 13:25:17 +0100115 input_frame_reader_(input_frame_reader),
116 encoded_frame_writers_(encoded_frame_writers),
117 decoded_frame_writers_(decoded_frame_writers),
Sergey Silkin3be2a552018-01-17 15:11:44 +0100118 last_inputed_frame_num_(0),
119 last_encoded_frame_num_(0),
Sergey Silkin10d9d592018-02-01 13:25:17 +0100120 last_encoded_simulcast_svc_idx_(0),
Sergey Silkin3be2a552018-01-17 15:11:44 +0100121 last_decoded_frame_num_(0),
122 num_encoded_frames_(0),
123 num_decoded_frames_(0),
Sergey Silkin3be2a552018-01-17 15:11:44 +0100124 stats_(stats) {
Rasmus Brandt4b381af2018-02-07 13:56:16 +0100125 RTC_CHECK(rtc::TaskQueue::Current())
126 << "VideoProcessor must be run on a task queue.";
Sergey Silkin10d9d592018-02-01 13:25:17 +0100127 RTC_CHECK(encoder);
128 RTC_CHECK(decoders && decoders->size() == num_simulcast_or_spatial_layers_);
129 RTC_CHECK(input_frame_reader);
130 RTC_CHECK(stats);
131 RTC_CHECK(!encoded_frame_writers ||
132 encoded_frame_writers->size() == num_simulcast_or_spatial_layers_);
133 RTC_CHECK(!decoded_frame_writers ||
134 decoded_frame_writers->size() == num_simulcast_or_spatial_layers_);
brandtr17b958c2017-03-07 01:41:43 -0800135
Sergey Silkin10d9d592018-02-01 13:25:17 +0100136 // Setup required callbacks for the encoder and decoder and initialize them.
brandtrbdd555c2017-08-21 01:34:04 -0700137 RTC_CHECK_EQ(encoder_->RegisterEncodeCompleteCallback(&encode_callback_),
Åsa Perssonf0c44672017-10-24 16:03:39 +0200138 WEBRTC_VIDEO_CODEC_OK);
asapersson654d54c2017-02-10 00:16:07 -0800139
Sergey Silkin1723cf92018-01-22 15:49:55 +0100140 RTC_CHECK_EQ(encoder_->InitEncode(&config_.codec_settings,
141 static_cast<int>(config_.NumberOfCores()),
142 config_.max_payload_size_bytes),
143 WEBRTC_VIDEO_CODEC_OK);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100144
145 for (auto& decoder : *decoders_) {
146 RTC_CHECK_EQ(decoder->InitDecode(&config_.codec_settings,
147 static_cast<int>(config_.NumberOfCores())),
148 WEBRTC_VIDEO_CODEC_OK);
149 RTC_CHECK_EQ(decoder->RegisterDecodeCompleteCallback(&decode_callback_),
150 WEBRTC_VIDEO_CODEC_OK);
151 }
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000152}
153
Åsa Perssonf0c44672017-10-24 16:03:39 +0200154VideoProcessor::~VideoProcessor() {
brandtrc8c59052017-08-21 06:44:16 -0700155 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
156
brandtr77920a42017-08-11 07:48:15 -0700157 RTC_CHECK_EQ(encoder_->Release(), WEBRTC_VIDEO_CODEC_OK);
brandtrbdd555c2017-08-21 01:34:04 -0700158 encoder_->RegisterEncodeCompleteCallback(nullptr);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100159
160 for (auto& decoder : *decoders_) {
161 RTC_CHECK_EQ(decoder->Release(), WEBRTC_VIDEO_CODEC_OK);
162 decoder->RegisterDecodeCompleteCallback(nullptr);
163 }
164
165 RTC_CHECK(last_encoded_frames_.empty());
brandtr77920a42017-08-11 07:48:15 -0700166}
167
brandtr8935d972017-09-06 01:53:22 -0700168void VideoProcessor::ProcessFrame() {
brandtrc8c59052017-08-21 06:44:16 -0700169 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
Sergey Silkin3be2a552018-01-17 15:11:44 +0100170 const size_t frame_number = last_inputed_frame_num_++;
asapersson654d54c2017-02-10 00:16:07 -0800171
brandtrbdd555c2017-08-21 01:34:04 -0700172 // Get frame from file.
magjed3f075492017-06-01 10:02:26 -0700173 rtc::scoped_refptr<I420BufferInterface> buffer(
Sergey Silkin10d9d592018-02-01 13:25:17 +0100174 input_frame_reader_->ReadFrame());
brandtrbdd555c2017-08-21 01:34:04 -0700175 RTC_CHECK(buffer) << "Tried to read too many frames from the file.";
Åsa Persson91af24a2018-01-24 17:20:18 +0100176
177 size_t rtp_timestamp =
178 (frame_number > 0) ? input_frames_[frame_number - 1]->timestamp() : 0;
179 rtp_timestamp +=
180 kVideoPayloadTypeFrequency / config_.codec_settings.maxFramerate;
181
182 input_frames_[frame_number] = rtc::MakeUnique<VideoFrame>(
183 buffer, static_cast<uint32_t>(rtp_timestamp),
184 static_cast<int64_t>(rtp_timestamp / kMsToRtpTimestamp),
185 webrtc::kVideoRotation_0);
brandtr17b958c2017-03-07 01:41:43 -0800186
Sergey Silkin64eaa992017-11-17 14:47:32 +0100187 std::vector<FrameType> frame_types = config_.FrameTypeForFrame(frame_number);
brandtr17b958c2017-03-07 01:41:43 -0800188
Sergey Silkin10d9d592018-02-01 13:25:17 +0100189 // Create frame statistics object for all simulcast /spatial layers.
190 for (size_t simulcast_svc_idx = 0;
191 simulcast_svc_idx < num_simulcast_or_spatial_layers_;
192 ++simulcast_svc_idx) {
193 stats_->at(simulcast_svc_idx).AddFrame(rtp_timestamp);
194 }
brandtr17b958c2017-03-07 01:41:43 -0800195
196 // For the highest measurement accuracy of the encode time, the start/stop
197 // time recordings should wrap the Encode call as tightly as possible.
Sergey Silkin10d9d592018-02-01 13:25:17 +0100198 const int64_t encode_start_ns = rtc::TimeNanos();
199 for (size_t simulcast_svc_idx = 0;
200 simulcast_svc_idx < num_simulcast_or_spatial_layers_;
201 ++simulcast_svc_idx) {
202 FrameStatistic* frame_stat =
203 stats_->at(simulcast_svc_idx).GetFrame(frame_number);
204 frame_stat->encode_start_ns = encode_start_ns;
205 }
206
207 const int encode_return_code =
Sergey Silkin64eaa992017-11-17 14:47:32 +0100208 encoder_->Encode(*input_frames_[frame_number], nullptr, &frame_types);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100209
210 for (size_t simulcast_svc_idx = 0;
211 simulcast_svc_idx < num_simulcast_or_spatial_layers_;
212 ++simulcast_svc_idx) {
213 FrameStatistic* frame_stat =
214 stats_->at(simulcast_svc_idx).GetFrame(frame_number);
215 frame_stat->encode_return_code = encode_return_code;
216 }
217
218 // For async codecs frame decoding is done in frame encode callback.
219 if (!config_.IsAsyncCodec()) {
220 for (size_t simulcast_svc_idx = 0;
221 simulcast_svc_idx < num_simulcast_or_spatial_layers_;
222 ++simulcast_svc_idx) {
223 if (last_encoded_frames_.find(simulcast_svc_idx) !=
224 last_encoded_frames_.end()) {
225 EncodedImage& encoded_image = last_encoded_frames_[simulcast_svc_idx];
226
227 FrameStatistic* frame_stat =
228 stats_->at(simulcast_svc_idx).GetFrame(frame_number);
229
230 if (encoded_frame_writers_) {
231 RTC_CHECK(encoded_frame_writers_->at(simulcast_svc_idx)
232 ->WriteFrame(encoded_image,
233 config_.codec_settings.codecType));
234 }
235
236 // For the highest measurement accuracy of the decode time, the
237 // start/stop time recordings should wrap the Decode call as tightly as
238 // possible.
239 frame_stat->decode_start_ns = rtc::TimeNanos();
240 frame_stat->decode_return_code =
241 decoders_->at(simulcast_svc_idx)
242 ->Decode(encoded_image, false, nullptr);
243
244 RTC_CHECK(encoded_image._buffer);
245 delete[] encoded_image._buffer;
246 encoded_image._buffer = nullptr;
247
248 last_encoded_frames_.erase(simulcast_svc_idx);
249 }
250 }
251 }
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000252}
253
Sergey Silkin3be2a552018-01-17 15:11:44 +0100254void VideoProcessor::SetRates(size_t bitrate_kbps, size_t framerate_fps) {
brandtrc8c59052017-08-21 06:44:16 -0700255 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
Sergey Silkin3be2a552018-01-17 15:11:44 +0100256 config_.codec_settings.maxFramerate = static_cast<uint32_t>(framerate_fps);
257 bitrate_allocation_ = bitrate_allocator_->GetAllocation(
258 static_cast<uint32_t>(bitrate_kbps * 1000),
259 static_cast<uint32_t>(framerate_fps));
260 const int set_rates_result = encoder_->SetRateAllocation(
261 bitrate_allocation_, static_cast<uint32_t>(framerate_fps));
brandtrbea36fd2017-08-07 03:36:54 -0700262 RTC_DCHECK_GE(set_rates_result, 0)
brandtrbdd555c2017-08-21 01:34:04 -0700263 << "Failed to update encoder with new rate " << bitrate_kbps << ".";
brandtrbea36fd2017-08-07 03:36:54 -0700264}
265
Sergey Silkin10d9d592018-02-01 13:25:17 +0100266void VideoProcessor::FrameEncoded(
267 const webrtc::EncodedImage& encoded_image,
268 const webrtc::CodecSpecificInfo& codec_specific) {
brandtrc8c59052017-08-21 06:44:16 -0700269 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
270
brandtr32e0d262017-02-15 05:29:38 -0800271 // For the highest measurement accuracy of the encode time, the start/stop
272 // time recordings should wrap the Encode call as tightly as possible.
273 int64_t encode_stop_ns = rtc::TimeNanos();
274
Sergey Silkin10d9d592018-02-01 13:25:17 +0100275 const VideoCodecType codec = codec_specific.codecType;
Rasmus Brandtf7a35582017-10-24 10:16:33 +0200276 if (config_.encoded_frame_checker) {
277 config_.encoded_frame_checker->CheckEncodedFrame(codec, encoded_image);
278 }
brandtrb78bc752017-02-22 01:26:59 -0800279
Sergey Silkin10d9d592018-02-01 13:25:17 +0100280 size_t simulcast_svc_idx = 0;
281 size_t temporal_idx = 0;
Sergey Silkin64eaa992017-11-17 14:47:32 +0100282
Sergey Silkin10d9d592018-02-01 13:25:17 +0100283 if (codec == kVideoCodecVP8) {
284 simulcast_svc_idx = codec_specific.codecSpecific.VP8.simulcastIdx;
285 temporal_idx = codec_specific.codecSpecific.VP8.temporalIdx;
286 } else if (codec == kVideoCodecVP9) {
287 simulcast_svc_idx = codec_specific.codecSpecific.VP9.spatial_idx;
288 temporal_idx = codec_specific.codecSpecific.VP9.temporal_idx;
289 }
290
291 if (simulcast_svc_idx == kNoSpatialIdx) {
292 simulcast_svc_idx = 0;
293 }
294
295 if (temporal_idx == kNoTemporalIdx) {
296 temporal_idx = 0;
297 }
298
299 const size_t frame_wxh =
300 encoded_image._encodedWidth * encoded_image._encodedHeight;
301 frame_wxh_to_simulcast_svc_idx_[frame_wxh] = simulcast_svc_idx;
302
303 FrameStatistic* frame_stat =
304 stats_->at(simulcast_svc_idx)
305 .GetFrameWithTimestamp(encoded_image._timeStamp);
Åsa Perssona6e7b882018-01-19 14:57:10 +0100306 const size_t frame_number = frame_stat->frame_number;
Sergey Silkin10d9d592018-02-01 13:25:17 +0100307
308 // Reordering is unexpected. Frames of different layers have the same value
309 // of frame_number. VP8 multi-res delivers frames starting from hires layer.
310 RTC_CHECK_GE(frame_number, last_encoded_frame_num_);
311
312 // Ensure SVC spatial layers are delivered in ascending order.
313 if (config_.NumberOfSpatialLayers() > 1) {
314 RTC_CHECK(simulcast_svc_idx > last_encoded_simulcast_svc_idx_ ||
315 frame_number != last_encoded_frame_num_ ||
316 num_encoded_frames_ == 0);
Sergey Silkin3be2a552018-01-17 15:11:44 +0100317 }
Sergey Silkin64eaa992017-11-17 14:47:32 +0100318
brandtr17b958c2017-03-07 01:41:43 -0800319 last_encoded_frame_num_ = frame_number;
Sergey Silkin10d9d592018-02-01 13:25:17 +0100320 last_encoded_simulcast_svc_idx_ = simulcast_svc_idx;
brandtr17b958c2017-03-07 01:41:43 -0800321
brandtr8935d972017-09-06 01:53:22 -0700322 // Update frame statistics.
Sergey Silkin10d9d592018-02-01 13:25:17 +0100323 frame_stat->encoding_successful = true;
brandtr8935d972017-09-06 01:53:22 -0700324 frame_stat->encode_time_us =
325 GetElapsedTimeMicroseconds(frame_stat->encode_start_ns, encode_stop_ns);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100326
327 // TODO(ssilkin): Implement bitrate allocation for VP9 SVC. For now set
328 // target for base layers equal to total target to avoid devision by zero
329 // at analysis.
330 frame_stat->target_bitrate_kbps =
331 bitrate_allocation_.GetSpatialLayerSum(
332 codec == kVideoCodecVP9 ? 0 : simulcast_svc_idx) /
333 1000;
brandtr8935d972017-09-06 01:53:22 -0700334 frame_stat->encoded_frame_size_bytes = encoded_image._length;
brandtr17b958c2017-03-07 01:41:43 -0800335 frame_stat->frame_type = encoded_image._frameType;
Sergey Silkin10d9d592018-02-01 13:25:17 +0100336 frame_stat->temporal_layer_idx = temporal_idx;
337 frame_stat->simulcast_svc_idx = simulcast_svc_idx;
Sergey Silkin3be2a552018-01-17 15:11:44 +0100338 frame_stat->max_nalu_size_bytes = GetMaxNaluSizeBytes(encoded_image, config_);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100339 frame_stat->qp = encoded_image.qp_;
ssilkin612f8582017-09-28 09:23:17 -0700340
Sergey Silkin10d9d592018-02-01 13:25:17 +0100341 if (!config_.IsAsyncCodec()) {
342 // Store encoded frame. It will be decoded after all layers are encoded.
343 CopyEncodedImage(encoded_image, codec, frame_number, simulcast_svc_idx);
344 } else {
345 const size_t simulcast_idx =
346 codec == kVideoCodecVP8 ? codec_specific.codecSpecific.VP8.simulcastIdx
347 : 0;
348 frame_stat->decode_start_ns = rtc::TimeNanos();
349 frame_stat->decode_return_code =
350 decoders_->at(simulcast_idx)->Decode(encoded_image, false, nullptr);
brandtr8935d972017-09-06 01:53:22 -0700351 }
Sergey Silkin3be2a552018-01-17 15:11:44 +0100352
353 ++num_encoded_frames_;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000354}
355
Sergey Silkin64eaa992017-11-17 14:47:32 +0100356void VideoProcessor::FrameDecoded(const VideoFrame& decoded_frame) {
brandtrc8c59052017-08-21 06:44:16 -0700357 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
358
brandtr32e0d262017-02-15 05:29:38 -0800359 // For the highest measurement accuracy of the decode time, the start/stop
360 // time recordings should wrap the Decode call as tightly as possible.
Niels Möllerd28db7f2016-05-10 16:31:47 +0200361 int64_t decode_stop_ns = rtc::TimeNanos();
brandtr8bc93852017-02-15 05:19:51 -0800362
Sergey Silkin10d9d592018-02-01 13:25:17 +0100363 RTC_CHECK(frame_wxh_to_simulcast_svc_idx_.find(decoded_frame.size()) !=
364 frame_wxh_to_simulcast_svc_idx_.end());
365 const size_t simulcast_svc_idx =
366 frame_wxh_to_simulcast_svc_idx_[decoded_frame.size()];
367
Åsa Perssona6e7b882018-01-19 14:57:10 +0100368 FrameStatistic* frame_stat =
Sergey Silkin10d9d592018-02-01 13:25:17 +0100369 stats_->at(simulcast_svc_idx)
370 .GetFrameWithTimestamp(decoded_frame.timestamp());
Åsa Perssona6e7b882018-01-19 14:57:10 +0100371 const size_t frame_number = frame_stat->frame_number;
Sergey Silkin64eaa992017-11-17 14:47:32 +0100372
Sergey Silkin10d9d592018-02-01 13:25:17 +0100373 // Reordering is unexpected. Frames of different layers have the same value
374 // of frame_number.
375 RTC_CHECK_GE(frame_number, last_decoded_frame_num_);
376
377 if (decoded_frame_writers_ && num_decoded_frames_ > 0) {
378 // For dropped frames, write out the last decoded frame to make it look like
379 // a freeze at playback.
380 for (size_t num_dropped_frames = 0; num_dropped_frames < frame_number;
381 ++num_dropped_frames) {
382 const FrameStatistic* prev_frame_stat =
383 stats_->at(simulcast_svc_idx)
384 .GetFrame(frame_number - num_dropped_frames - 1);
385 if (prev_frame_stat->decoding_successful) {
386 break;
Sergey Silkin64eaa992017-11-17 14:47:32 +0100387 }
Sergey Silkin10d9d592018-02-01 13:25:17 +0100388 WriteDecodedFrameToFile(&last_decoded_frame_buffers_[simulcast_svc_idx],
389 simulcast_svc_idx);
Sergey Silkin64eaa992017-11-17 14:47:32 +0100390 }
marpan@webrtc.orgf4c2de92012-06-05 21:07:28 +0000391 }
Sergey Silkin10d9d592018-02-01 13:25:17 +0100392
brandtr17b958c2017-03-07 01:41:43 -0800393 last_decoded_frame_num_ = frame_number;
394
Sergey Silkin10d9d592018-02-01 13:25:17 +0100395 // Update frame statistics.
396 frame_stat->decoding_successful = true;
397 frame_stat->decode_time_us =
398 GetElapsedTimeMicroseconds(frame_stat->decode_start_ns, decode_stop_ns);
399 frame_stat->decoded_width = decoded_frame.width();
400 frame_stat->decoded_height = decoded_frame.height();
401
Sergey Silkin64eaa992017-11-17 14:47:32 +0100402 // Skip quality metrics calculation to not affect CPU usage.
403 if (!config_.measure_cpu) {
Sergey Silkin10d9d592018-02-01 13:25:17 +0100404 CalculateFrameQuality(*input_frames_[frame_number], decoded_frame,
405 frame_stat);
Sergey Silkin64eaa992017-11-17 14:47:32 +0100406 }
Niels Möller718a7632016-06-13 13:06:01 +0200407
Sergey Silkin64eaa992017-11-17 14:47:32 +0100408 // Delay erasing of input frames by one frame. The current frame might
409 // still be needed for other simulcast stream or spatial layer.
Sergey Silkin3be2a552018-01-17 15:11:44 +0100410 if (frame_number > 0) {
411 auto input_frame_erase_to = input_frames_.lower_bound(frame_number - 1);
Sergey Silkin64eaa992017-11-17 14:47:32 +0100412 input_frames_.erase(input_frames_.begin(), input_frame_erase_to);
413 }
414
Sergey Silkin10d9d592018-02-01 13:25:17 +0100415 if (decoded_frame_writers_) {
Sergey Silkin64eaa992017-11-17 14:47:32 +0100416 ExtractBufferWithSize(decoded_frame, config_.codec_settings.width,
417 config_.codec_settings.height,
Sergey Silkin10d9d592018-02-01 13:25:17 +0100418 &last_decoded_frame_buffers_[simulcast_svc_idx]);
419 WriteDecodedFrameToFile(&last_decoded_frame_buffers_[simulcast_svc_idx],
420 simulcast_svc_idx);
Sergey Silkin64eaa992017-11-17 14:47:32 +0100421 }
Sergey Silkin3be2a552018-01-17 15:11:44 +0100422
423 ++num_decoded_frames_;
Åsa Perssonf0c44672017-10-24 16:03:39 +0200424}
brandtr17b958c2017-03-07 01:41:43 -0800425
Sergey Silkin10d9d592018-02-01 13:25:17 +0100426void VideoProcessor::CopyEncodedImage(const EncodedImage& encoded_image,
427 const VideoCodecType codec,
428 size_t frame_number,
429 size_t simulcast_svc_idx) {
430 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
431
432 EncodedImage base_image;
433 RTC_CHECK_EQ(base_image._length, 0);
434
435 // Each SVC layer is decoded with dedicated decoder. Add data of base layers
436 // to current coded frame buffer.
437 if (config_.NumberOfSpatialLayers() > 1 && simulcast_svc_idx > 0) {
438 RTC_CHECK(last_encoded_frames_.find(simulcast_svc_idx - 1) !=
439 last_encoded_frames_.end());
440 base_image = last_encoded_frames_[simulcast_svc_idx - 1];
441 }
442
443 const size_t payload_size_bytes = base_image._length + encoded_image._length;
444 const size_t buffer_size_bytes =
445 payload_size_bytes + EncodedImage::GetBufferPaddingBytes(codec);
446
447 uint8_t* copied_buffer = new uint8_t[buffer_size_bytes];
448 RTC_CHECK(copied_buffer);
449
450 if (base_image._length) {
451 memcpy(copied_buffer, base_image._buffer, base_image._length);
452 }
453
454 memcpy(copied_buffer + base_image._length, encoded_image._buffer,
455 encoded_image._length);
456
457 EncodedImage copied_image = encoded_image;
458 copied_image = encoded_image;
459 copied_image._buffer = copied_buffer;
460 copied_image._length = payload_size_bytes;
461 copied_image._size = buffer_size_bytes;
462
463 last_encoded_frames_[simulcast_svc_idx] = copied_image;
464}
465
466void VideoProcessor::CalculateFrameQuality(const VideoFrame& ref_frame,
467 const VideoFrame& dec_frame,
468 FrameStatistic* frame_stat) {
469 if (ref_frame.width() == dec_frame.width() ||
470 ref_frame.height() == dec_frame.height()) {
471 frame_stat->psnr = I420PSNR(&ref_frame, &dec_frame);
472 frame_stat->ssim = I420SSIM(&ref_frame, &dec_frame);
473 } else {
474 RTC_CHECK_GE(ref_frame.width(), dec_frame.width());
475 RTC_CHECK_GE(ref_frame.height(), dec_frame.height());
476 // Downscale reference frame. Use bilinear interpolation since it is used
477 // to get lowres inputs for encoder at simulcasting.
478 // TODO(ssilkin): Sync with VP9 SVC which uses 8-taps polyphase.
479 rtc::scoped_refptr<I420Buffer> scaled_buffer =
480 I420Buffer::Create(dec_frame.width(), dec_frame.height());
481 const I420BufferInterface& ref_buffer =
482 *ref_frame.video_frame_buffer()->ToI420();
483 I420Scale(ref_buffer.DataY(), ref_buffer.StrideY(), ref_buffer.DataU(),
484 ref_buffer.StrideU(), ref_buffer.DataV(), ref_buffer.StrideV(),
485 ref_buffer.width(), ref_buffer.height(),
486 scaled_buffer->MutableDataY(), scaled_buffer->StrideY(),
487 scaled_buffer->MutableDataU(), scaled_buffer->StrideU(),
488 scaled_buffer->MutableDataV(), scaled_buffer->StrideV(),
489 scaled_buffer->width(), scaled_buffer->height(),
490 libyuv::kFilterBilinear);
491 frame_stat->psnr =
492 I420PSNR(*scaled_buffer, *dec_frame.video_frame_buffer()->ToI420());
493 frame_stat->ssim =
494 I420SSIM(*scaled_buffer, *dec_frame.video_frame_buffer()->ToI420());
495 }
496}
497
498void VideoProcessor::WriteDecodedFrameToFile(rtc::Buffer* buffer,
499 size_t simulcast_svc_idx) {
500 RTC_CHECK(simulcast_svc_idx < decoded_frame_writers_->size());
501 RTC_DCHECK_EQ(buffer->size(),
502 decoded_frame_writers_->at(simulcast_svc_idx)->FrameLength());
503 RTC_CHECK(decoded_frame_writers_->at(simulcast_svc_idx)
504 ->WriteFrame(buffer->data()));
Åsa Perssonf0c44672017-10-24 16:03:39 +0200505}
brandtr17b958c2017-03-07 01:41:43 -0800506
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000507} // namespace test
508} // namespace webrtc