blob: 6c06087820cada7e5d77b298d1033e43a766ac86 [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) {
Sergey Silkin10d9d592018-02-01 13:25:17 +0100125 RTC_CHECK(encoder);
126 RTC_CHECK(decoders && decoders->size() == num_simulcast_or_spatial_layers_);
127 RTC_CHECK(input_frame_reader);
128 RTC_CHECK(stats);
129 RTC_CHECK(!encoded_frame_writers ||
130 encoded_frame_writers->size() == num_simulcast_or_spatial_layers_);
131 RTC_CHECK(!decoded_frame_writers ||
132 decoded_frame_writers->size() == num_simulcast_or_spatial_layers_);
brandtr17b958c2017-03-07 01:41:43 -0800133
Sergey Silkin10d9d592018-02-01 13:25:17 +0100134 // Setup required callbacks for the encoder and decoder and initialize them.
brandtrbdd555c2017-08-21 01:34:04 -0700135 RTC_CHECK_EQ(encoder_->RegisterEncodeCompleteCallback(&encode_callback_),
Åsa Perssonf0c44672017-10-24 16:03:39 +0200136 WEBRTC_VIDEO_CODEC_OK);
asapersson654d54c2017-02-10 00:16:07 -0800137
Sergey Silkin1723cf92018-01-22 15:49:55 +0100138 RTC_CHECK_EQ(encoder_->InitEncode(&config_.codec_settings,
139 static_cast<int>(config_.NumberOfCores()),
140 config_.max_payload_size_bytes),
141 WEBRTC_VIDEO_CODEC_OK);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100142
143 for (auto& decoder : *decoders_) {
144 RTC_CHECK_EQ(decoder->InitDecode(&config_.codec_settings,
145 static_cast<int>(config_.NumberOfCores())),
146 WEBRTC_VIDEO_CODEC_OK);
147 RTC_CHECK_EQ(decoder->RegisterDecodeCompleteCallback(&decode_callback_),
148 WEBRTC_VIDEO_CODEC_OK);
149 }
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000150}
151
Åsa Perssonf0c44672017-10-24 16:03:39 +0200152VideoProcessor::~VideoProcessor() {
brandtrc8c59052017-08-21 06:44:16 -0700153 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
154
brandtr77920a42017-08-11 07:48:15 -0700155 RTC_CHECK_EQ(encoder_->Release(), WEBRTC_VIDEO_CODEC_OK);
brandtrbdd555c2017-08-21 01:34:04 -0700156 encoder_->RegisterEncodeCompleteCallback(nullptr);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100157
158 for (auto& decoder : *decoders_) {
159 RTC_CHECK_EQ(decoder->Release(), WEBRTC_VIDEO_CODEC_OK);
160 decoder->RegisterDecodeCompleteCallback(nullptr);
161 }
162
163 RTC_CHECK(last_encoded_frames_.empty());
brandtr77920a42017-08-11 07:48:15 -0700164}
165
brandtr8935d972017-09-06 01:53:22 -0700166void VideoProcessor::ProcessFrame() {
brandtrc8c59052017-08-21 06:44:16 -0700167 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
Sergey Silkin3be2a552018-01-17 15:11:44 +0100168 const size_t frame_number = last_inputed_frame_num_++;
asapersson654d54c2017-02-10 00:16:07 -0800169
brandtrbdd555c2017-08-21 01:34:04 -0700170 // Get frame from file.
magjed3f075492017-06-01 10:02:26 -0700171 rtc::scoped_refptr<I420BufferInterface> buffer(
Sergey Silkin10d9d592018-02-01 13:25:17 +0100172 input_frame_reader_->ReadFrame());
brandtrbdd555c2017-08-21 01:34:04 -0700173 RTC_CHECK(buffer) << "Tried to read too many frames from the file.";
Åsa Persson91af24a2018-01-24 17:20:18 +0100174
175 size_t rtp_timestamp =
176 (frame_number > 0) ? input_frames_[frame_number - 1]->timestamp() : 0;
177 rtp_timestamp +=
178 kVideoPayloadTypeFrequency / config_.codec_settings.maxFramerate;
179
180 input_frames_[frame_number] = rtc::MakeUnique<VideoFrame>(
181 buffer, static_cast<uint32_t>(rtp_timestamp),
182 static_cast<int64_t>(rtp_timestamp / kMsToRtpTimestamp),
183 webrtc::kVideoRotation_0);
brandtr17b958c2017-03-07 01:41:43 -0800184
Sergey Silkin64eaa992017-11-17 14:47:32 +0100185 std::vector<FrameType> frame_types = config_.FrameTypeForFrame(frame_number);
brandtr17b958c2017-03-07 01:41:43 -0800186
Sergey Silkin10d9d592018-02-01 13:25:17 +0100187 // Create frame statistics object for all simulcast /spatial layers.
188 for (size_t simulcast_svc_idx = 0;
189 simulcast_svc_idx < num_simulcast_or_spatial_layers_;
190 ++simulcast_svc_idx) {
191 stats_->at(simulcast_svc_idx).AddFrame(rtp_timestamp);
192 }
brandtr17b958c2017-03-07 01:41:43 -0800193
194 // For the highest measurement accuracy of the encode time, the start/stop
195 // time recordings should wrap the Encode call as tightly as possible.
Sergey Silkin10d9d592018-02-01 13:25:17 +0100196 const int64_t encode_start_ns = rtc::TimeNanos();
197 for (size_t simulcast_svc_idx = 0;
198 simulcast_svc_idx < num_simulcast_or_spatial_layers_;
199 ++simulcast_svc_idx) {
200 FrameStatistic* frame_stat =
201 stats_->at(simulcast_svc_idx).GetFrame(frame_number);
202 frame_stat->encode_start_ns = encode_start_ns;
203 }
204
205 const int encode_return_code =
Sergey Silkin64eaa992017-11-17 14:47:32 +0100206 encoder_->Encode(*input_frames_[frame_number], nullptr, &frame_types);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100207
208 for (size_t simulcast_svc_idx = 0;
209 simulcast_svc_idx < num_simulcast_or_spatial_layers_;
210 ++simulcast_svc_idx) {
211 FrameStatistic* frame_stat =
212 stats_->at(simulcast_svc_idx).GetFrame(frame_number);
213 frame_stat->encode_return_code = encode_return_code;
214 }
215
216 // For async codecs frame decoding is done in frame encode callback.
217 if (!config_.IsAsyncCodec()) {
218 for (size_t simulcast_svc_idx = 0;
219 simulcast_svc_idx < num_simulcast_or_spatial_layers_;
220 ++simulcast_svc_idx) {
221 if (last_encoded_frames_.find(simulcast_svc_idx) !=
222 last_encoded_frames_.end()) {
223 EncodedImage& encoded_image = last_encoded_frames_[simulcast_svc_idx];
224
225 FrameStatistic* frame_stat =
226 stats_->at(simulcast_svc_idx).GetFrame(frame_number);
227
228 if (encoded_frame_writers_) {
229 RTC_CHECK(encoded_frame_writers_->at(simulcast_svc_idx)
230 ->WriteFrame(encoded_image,
231 config_.codec_settings.codecType));
232 }
233
234 // For the highest measurement accuracy of the decode time, the
235 // start/stop time recordings should wrap the Decode call as tightly as
236 // possible.
237 frame_stat->decode_start_ns = rtc::TimeNanos();
238 frame_stat->decode_return_code =
239 decoders_->at(simulcast_svc_idx)
240 ->Decode(encoded_image, false, nullptr);
241
242 RTC_CHECK(encoded_image._buffer);
243 delete[] encoded_image._buffer;
244 encoded_image._buffer = nullptr;
245
246 last_encoded_frames_.erase(simulcast_svc_idx);
247 }
248 }
249 }
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000250}
251
Sergey Silkin3be2a552018-01-17 15:11:44 +0100252void VideoProcessor::SetRates(size_t bitrate_kbps, size_t framerate_fps) {
brandtrc8c59052017-08-21 06:44:16 -0700253 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
Sergey Silkin3be2a552018-01-17 15:11:44 +0100254 config_.codec_settings.maxFramerate = static_cast<uint32_t>(framerate_fps);
255 bitrate_allocation_ = bitrate_allocator_->GetAllocation(
256 static_cast<uint32_t>(bitrate_kbps * 1000),
257 static_cast<uint32_t>(framerate_fps));
258 const int set_rates_result = encoder_->SetRateAllocation(
259 bitrate_allocation_, static_cast<uint32_t>(framerate_fps));
brandtrbea36fd2017-08-07 03:36:54 -0700260 RTC_DCHECK_GE(set_rates_result, 0)
brandtrbdd555c2017-08-21 01:34:04 -0700261 << "Failed to update encoder with new rate " << bitrate_kbps << ".";
brandtrbea36fd2017-08-07 03:36:54 -0700262}
263
Sergey Silkin10d9d592018-02-01 13:25:17 +0100264void VideoProcessor::FrameEncoded(
265 const webrtc::EncodedImage& encoded_image,
266 const webrtc::CodecSpecificInfo& codec_specific) {
brandtrc8c59052017-08-21 06:44:16 -0700267 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
268
brandtr32e0d262017-02-15 05:29:38 -0800269 // For the highest measurement accuracy of the encode time, the start/stop
270 // time recordings should wrap the Encode call as tightly as possible.
271 int64_t encode_stop_ns = rtc::TimeNanos();
272
Sergey Silkin10d9d592018-02-01 13:25:17 +0100273 const VideoCodecType codec = codec_specific.codecType;
Rasmus Brandtf7a35582017-10-24 10:16:33 +0200274 if (config_.encoded_frame_checker) {
275 config_.encoded_frame_checker->CheckEncodedFrame(codec, encoded_image);
276 }
brandtrb78bc752017-02-22 01:26:59 -0800277
Sergey Silkin10d9d592018-02-01 13:25:17 +0100278 size_t simulcast_svc_idx = 0;
279 size_t temporal_idx = 0;
Sergey Silkin64eaa992017-11-17 14:47:32 +0100280
Sergey Silkin10d9d592018-02-01 13:25:17 +0100281 if (codec == kVideoCodecVP8) {
282 simulcast_svc_idx = codec_specific.codecSpecific.VP8.simulcastIdx;
283 temporal_idx = codec_specific.codecSpecific.VP8.temporalIdx;
284 } else if (codec == kVideoCodecVP9) {
285 simulcast_svc_idx = codec_specific.codecSpecific.VP9.spatial_idx;
286 temporal_idx = codec_specific.codecSpecific.VP9.temporal_idx;
287 }
288
289 if (simulcast_svc_idx == kNoSpatialIdx) {
290 simulcast_svc_idx = 0;
291 }
292
293 if (temporal_idx == kNoTemporalIdx) {
294 temporal_idx = 0;
295 }
296
297 const size_t frame_wxh =
298 encoded_image._encodedWidth * encoded_image._encodedHeight;
299 frame_wxh_to_simulcast_svc_idx_[frame_wxh] = simulcast_svc_idx;
300
301 FrameStatistic* frame_stat =
302 stats_->at(simulcast_svc_idx)
303 .GetFrameWithTimestamp(encoded_image._timeStamp);
Åsa Perssona6e7b882018-01-19 14:57:10 +0100304 const size_t frame_number = frame_stat->frame_number;
Sergey Silkin10d9d592018-02-01 13:25:17 +0100305
306 // Reordering is unexpected. Frames of different layers have the same value
307 // of frame_number. VP8 multi-res delivers frames starting from hires layer.
308 RTC_CHECK_GE(frame_number, last_encoded_frame_num_);
309
310 // Ensure SVC spatial layers are delivered in ascending order.
311 if (config_.NumberOfSpatialLayers() > 1) {
312 RTC_CHECK(simulcast_svc_idx > last_encoded_simulcast_svc_idx_ ||
313 frame_number != last_encoded_frame_num_ ||
314 num_encoded_frames_ == 0);
Sergey Silkin3be2a552018-01-17 15:11:44 +0100315 }
Sergey Silkin64eaa992017-11-17 14:47:32 +0100316
brandtr17b958c2017-03-07 01:41:43 -0800317 last_encoded_frame_num_ = frame_number;
Sergey Silkin10d9d592018-02-01 13:25:17 +0100318 last_encoded_simulcast_svc_idx_ = simulcast_svc_idx;
brandtr17b958c2017-03-07 01:41:43 -0800319
brandtr8935d972017-09-06 01:53:22 -0700320 // Update frame statistics.
Sergey Silkin10d9d592018-02-01 13:25:17 +0100321 frame_stat->encoding_successful = true;
brandtr8935d972017-09-06 01:53:22 -0700322 frame_stat->encode_time_us =
323 GetElapsedTimeMicroseconds(frame_stat->encode_start_ns, encode_stop_ns);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100324
325 // TODO(ssilkin): Implement bitrate allocation for VP9 SVC. For now set
326 // target for base layers equal to total target to avoid devision by zero
327 // at analysis.
328 frame_stat->target_bitrate_kbps =
329 bitrate_allocation_.GetSpatialLayerSum(
330 codec == kVideoCodecVP9 ? 0 : simulcast_svc_idx) /
331 1000;
brandtr8935d972017-09-06 01:53:22 -0700332 frame_stat->encoded_frame_size_bytes = encoded_image._length;
brandtr17b958c2017-03-07 01:41:43 -0800333 frame_stat->frame_type = encoded_image._frameType;
Sergey Silkin10d9d592018-02-01 13:25:17 +0100334 frame_stat->temporal_layer_idx = temporal_idx;
335 frame_stat->simulcast_svc_idx = simulcast_svc_idx;
Sergey Silkin3be2a552018-01-17 15:11:44 +0100336 frame_stat->max_nalu_size_bytes = GetMaxNaluSizeBytes(encoded_image, config_);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100337 frame_stat->qp = encoded_image.qp_;
ssilkin612f8582017-09-28 09:23:17 -0700338
Sergey Silkin10d9d592018-02-01 13:25:17 +0100339 if (!config_.IsAsyncCodec()) {
340 // Store encoded frame. It will be decoded after all layers are encoded.
341 CopyEncodedImage(encoded_image, codec, frame_number, simulcast_svc_idx);
342 } else {
343 const size_t simulcast_idx =
344 codec == kVideoCodecVP8 ? codec_specific.codecSpecific.VP8.simulcastIdx
345 : 0;
346 frame_stat->decode_start_ns = rtc::TimeNanos();
347 frame_stat->decode_return_code =
348 decoders_->at(simulcast_idx)->Decode(encoded_image, false, nullptr);
brandtr8935d972017-09-06 01:53:22 -0700349 }
Sergey Silkin3be2a552018-01-17 15:11:44 +0100350
351 ++num_encoded_frames_;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000352}
353
Sergey Silkin64eaa992017-11-17 14:47:32 +0100354void VideoProcessor::FrameDecoded(const VideoFrame& decoded_frame) {
brandtrc8c59052017-08-21 06:44:16 -0700355 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
356
brandtr32e0d262017-02-15 05:29:38 -0800357 // For the highest measurement accuracy of the decode time, the start/stop
358 // time recordings should wrap the Decode call as tightly as possible.
Niels Möllerd28db7f2016-05-10 16:31:47 +0200359 int64_t decode_stop_ns = rtc::TimeNanos();
brandtr8bc93852017-02-15 05:19:51 -0800360
Sergey Silkin10d9d592018-02-01 13:25:17 +0100361 RTC_CHECK(frame_wxh_to_simulcast_svc_idx_.find(decoded_frame.size()) !=
362 frame_wxh_to_simulcast_svc_idx_.end());
363 const size_t simulcast_svc_idx =
364 frame_wxh_to_simulcast_svc_idx_[decoded_frame.size()];
365
Åsa Perssona6e7b882018-01-19 14:57:10 +0100366 FrameStatistic* frame_stat =
Sergey Silkin10d9d592018-02-01 13:25:17 +0100367 stats_->at(simulcast_svc_idx)
368 .GetFrameWithTimestamp(decoded_frame.timestamp());
Åsa Perssona6e7b882018-01-19 14:57:10 +0100369 const size_t frame_number = frame_stat->frame_number;
Sergey Silkin64eaa992017-11-17 14:47:32 +0100370
Sergey Silkin10d9d592018-02-01 13:25:17 +0100371 // Reordering is unexpected. Frames of different layers have the same value
372 // of frame_number.
373 RTC_CHECK_GE(frame_number, last_decoded_frame_num_);
374
375 if (decoded_frame_writers_ && num_decoded_frames_ > 0) {
376 // For dropped frames, write out the last decoded frame to make it look like
377 // a freeze at playback.
378 for (size_t num_dropped_frames = 0; num_dropped_frames < frame_number;
379 ++num_dropped_frames) {
380 const FrameStatistic* prev_frame_stat =
381 stats_->at(simulcast_svc_idx)
382 .GetFrame(frame_number - num_dropped_frames - 1);
383 if (prev_frame_stat->decoding_successful) {
384 break;
Sergey Silkin64eaa992017-11-17 14:47:32 +0100385 }
Sergey Silkin10d9d592018-02-01 13:25:17 +0100386 WriteDecodedFrameToFile(&last_decoded_frame_buffers_[simulcast_svc_idx],
387 simulcast_svc_idx);
Sergey Silkin64eaa992017-11-17 14:47:32 +0100388 }
marpan@webrtc.orgf4c2de92012-06-05 21:07:28 +0000389 }
Sergey Silkin10d9d592018-02-01 13:25:17 +0100390
brandtr17b958c2017-03-07 01:41:43 -0800391 last_decoded_frame_num_ = frame_number;
392
Sergey Silkin10d9d592018-02-01 13:25:17 +0100393 // Update frame statistics.
394 frame_stat->decoding_successful = true;
395 frame_stat->decode_time_us =
396 GetElapsedTimeMicroseconds(frame_stat->decode_start_ns, decode_stop_ns);
397 frame_stat->decoded_width = decoded_frame.width();
398 frame_stat->decoded_height = decoded_frame.height();
399
Sergey Silkin64eaa992017-11-17 14:47:32 +0100400 // Skip quality metrics calculation to not affect CPU usage.
401 if (!config_.measure_cpu) {
Sergey Silkin10d9d592018-02-01 13:25:17 +0100402 CalculateFrameQuality(*input_frames_[frame_number], decoded_frame,
403 frame_stat);
Sergey Silkin64eaa992017-11-17 14:47:32 +0100404 }
Niels Möller718a7632016-06-13 13:06:01 +0200405
Sergey Silkin64eaa992017-11-17 14:47:32 +0100406 // Delay erasing of input frames by one frame. The current frame might
407 // still be needed for other simulcast stream or spatial layer.
Sergey Silkin3be2a552018-01-17 15:11:44 +0100408 if (frame_number > 0) {
409 auto input_frame_erase_to = input_frames_.lower_bound(frame_number - 1);
Sergey Silkin64eaa992017-11-17 14:47:32 +0100410 input_frames_.erase(input_frames_.begin(), input_frame_erase_to);
411 }
412
Sergey Silkin10d9d592018-02-01 13:25:17 +0100413 if (decoded_frame_writers_) {
Sergey Silkin64eaa992017-11-17 14:47:32 +0100414 ExtractBufferWithSize(decoded_frame, config_.codec_settings.width,
415 config_.codec_settings.height,
Sergey Silkin10d9d592018-02-01 13:25:17 +0100416 &last_decoded_frame_buffers_[simulcast_svc_idx]);
417 WriteDecodedFrameToFile(&last_decoded_frame_buffers_[simulcast_svc_idx],
418 simulcast_svc_idx);
Sergey Silkin64eaa992017-11-17 14:47:32 +0100419 }
Sergey Silkin3be2a552018-01-17 15:11:44 +0100420
421 ++num_decoded_frames_;
Åsa Perssonf0c44672017-10-24 16:03:39 +0200422}
brandtr17b958c2017-03-07 01:41:43 -0800423
Sergey Silkin10d9d592018-02-01 13:25:17 +0100424void VideoProcessor::CopyEncodedImage(const EncodedImage& encoded_image,
425 const VideoCodecType codec,
426 size_t frame_number,
427 size_t simulcast_svc_idx) {
428 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
429
430 EncodedImage base_image;
431 RTC_CHECK_EQ(base_image._length, 0);
432
433 // Each SVC layer is decoded with dedicated decoder. Add data of base layers
434 // to current coded frame buffer.
435 if (config_.NumberOfSpatialLayers() > 1 && simulcast_svc_idx > 0) {
436 RTC_CHECK(last_encoded_frames_.find(simulcast_svc_idx - 1) !=
437 last_encoded_frames_.end());
438 base_image = last_encoded_frames_[simulcast_svc_idx - 1];
439 }
440
441 const size_t payload_size_bytes = base_image._length + encoded_image._length;
442 const size_t buffer_size_bytes =
443 payload_size_bytes + EncodedImage::GetBufferPaddingBytes(codec);
444
445 uint8_t* copied_buffer = new uint8_t[buffer_size_bytes];
446 RTC_CHECK(copied_buffer);
447
448 if (base_image._length) {
449 memcpy(copied_buffer, base_image._buffer, base_image._length);
450 }
451
452 memcpy(copied_buffer + base_image._length, encoded_image._buffer,
453 encoded_image._length);
454
455 EncodedImage copied_image = encoded_image;
456 copied_image = encoded_image;
457 copied_image._buffer = copied_buffer;
458 copied_image._length = payload_size_bytes;
459 copied_image._size = buffer_size_bytes;
460
461 last_encoded_frames_[simulcast_svc_idx] = copied_image;
462}
463
464void VideoProcessor::CalculateFrameQuality(const VideoFrame& ref_frame,
465 const VideoFrame& dec_frame,
466 FrameStatistic* frame_stat) {
467 if (ref_frame.width() == dec_frame.width() ||
468 ref_frame.height() == dec_frame.height()) {
469 frame_stat->psnr = I420PSNR(&ref_frame, &dec_frame);
470 frame_stat->ssim = I420SSIM(&ref_frame, &dec_frame);
471 } else {
472 RTC_CHECK_GE(ref_frame.width(), dec_frame.width());
473 RTC_CHECK_GE(ref_frame.height(), dec_frame.height());
474 // Downscale reference frame. Use bilinear interpolation since it is used
475 // to get lowres inputs for encoder at simulcasting.
476 // TODO(ssilkin): Sync with VP9 SVC which uses 8-taps polyphase.
477 rtc::scoped_refptr<I420Buffer> scaled_buffer =
478 I420Buffer::Create(dec_frame.width(), dec_frame.height());
479 const I420BufferInterface& ref_buffer =
480 *ref_frame.video_frame_buffer()->ToI420();
481 I420Scale(ref_buffer.DataY(), ref_buffer.StrideY(), ref_buffer.DataU(),
482 ref_buffer.StrideU(), ref_buffer.DataV(), ref_buffer.StrideV(),
483 ref_buffer.width(), ref_buffer.height(),
484 scaled_buffer->MutableDataY(), scaled_buffer->StrideY(),
485 scaled_buffer->MutableDataU(), scaled_buffer->StrideU(),
486 scaled_buffer->MutableDataV(), scaled_buffer->StrideV(),
487 scaled_buffer->width(), scaled_buffer->height(),
488 libyuv::kFilterBilinear);
489 frame_stat->psnr =
490 I420PSNR(*scaled_buffer, *dec_frame.video_frame_buffer()->ToI420());
491 frame_stat->ssim =
492 I420SSIM(*scaled_buffer, *dec_frame.video_frame_buffer()->ToI420());
493 }
494}
495
496void VideoProcessor::WriteDecodedFrameToFile(rtc::Buffer* buffer,
497 size_t simulcast_svc_idx) {
498 RTC_CHECK(simulcast_svc_idx < decoded_frame_writers_->size());
499 RTC_DCHECK_EQ(buffer->size(),
500 decoded_frame_writers_->at(simulcast_svc_idx)->FrameLength());
501 RTC_CHECK(decoded_frame_writers_->at(simulcast_svc_idx)
502 ->WriteFrame(buffer->data()));
Åsa Perssonf0c44672017-10-24 16:03:39 +0200503}
brandtr17b958c2017-03-07 01:41:43 -0800504
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000505} // namespace test
506} // namespace webrtc