blob: e10aa5f7d2d42328548e7b97d51634c36c360f4f [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 Silkin06a8f302018-02-20 09:48:26 +0100103 Stats* stats,
Sergey Silkin10d9d592018-02-01 13:25:17 +0100104 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) {
Sergey Silkin06a8f302018-02-20 09:48:26 +0100193 stats_->AddFrame(rtp_timestamp, simulcast_svc_idx);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100194 }
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) {
Sergey Silkin06a8f302018-02-20 09:48:26 +0100202 FrameStatistics* frame_stat =
203 stats_->GetFrame(frame_number, simulcast_svc_idx);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100204 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) {
Sergey Silkin06a8f302018-02-20 09:48:26 +0100213 FrameStatistics* frame_stat =
214 stats_->GetFrame(frame_number, simulcast_svc_idx);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100215 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
Sergey Silkin06a8f302018-02-20 09:48:26 +0100227 FrameStatistics* frame_stat =
228 stats_->GetFrame(frame_number, simulcast_svc_idx);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100229
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
Sergey Silkin06a8f302018-02-20 09:48:26 +0100303 FrameStatistics* frame_stat = stats_->GetFrameWithTimestamp(
304 encoded_image._timeStamp, simulcast_svc_idx);
Åsa Perssona6e7b882018-01-19 14:57:10 +0100305 const size_t frame_number = frame_stat->frame_number;
Sergey Silkin10d9d592018-02-01 13:25:17 +0100306
307 // Reordering is unexpected. Frames of different layers have the same value
308 // of frame_number. VP8 multi-res delivers frames starting from hires layer.
309 RTC_CHECK_GE(frame_number, last_encoded_frame_num_);
310
311 // Ensure SVC spatial layers are delivered in ascending order.
312 if (config_.NumberOfSpatialLayers() > 1) {
313 RTC_CHECK(simulcast_svc_idx > last_encoded_simulcast_svc_idx_ ||
314 frame_number != last_encoded_frame_num_ ||
315 num_encoded_frames_ == 0);
Sergey Silkin3be2a552018-01-17 15:11:44 +0100316 }
Sergey Silkin64eaa992017-11-17 14:47:32 +0100317
brandtr17b958c2017-03-07 01:41:43 -0800318 last_encoded_frame_num_ = frame_number;
Sergey Silkin10d9d592018-02-01 13:25:17 +0100319 last_encoded_simulcast_svc_idx_ = simulcast_svc_idx;
brandtr17b958c2017-03-07 01:41:43 -0800320
brandtr8935d972017-09-06 01:53:22 -0700321 // Update frame statistics.
Sergey Silkin10d9d592018-02-01 13:25:17 +0100322 frame_stat->encoding_successful = true;
brandtr8935d972017-09-06 01:53:22 -0700323 frame_stat->encode_time_us =
324 GetElapsedTimeMicroseconds(frame_stat->encode_start_ns, encode_stop_ns);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100325
Sergey Silkin06a8f302018-02-20 09:48:26 +0100326 if (codec == kVideoCodecVP9) {
327 const CodecSpecificInfoVP9& vp9_info = codec_specific.codecSpecific.VP9;
328 frame_stat->inter_layer_predicted = vp9_info.inter_layer_predicted;
329
330 // TODO(ssilkin): Implement bitrate allocation for VP9 SVC. For now set
331 // target for base layers equal to total target to avoid devision by zero
332 // at analysis.
333 frame_stat->target_bitrate_kbps = bitrate_allocation_.get_sum_kbps();
334 } else {
335 frame_stat->target_bitrate_kbps =
336 (bitrate_allocation_.GetBitrate(simulcast_svc_idx, temporal_idx) +
337 500) /
338 1000;
339 }
340
brandtr8935d972017-09-06 01:53:22 -0700341 frame_stat->encoded_frame_size_bytes = encoded_image._length;
brandtr17b958c2017-03-07 01:41:43 -0800342 frame_stat->frame_type = encoded_image._frameType;
Sergey Silkin10d9d592018-02-01 13:25:17 +0100343 frame_stat->temporal_layer_idx = temporal_idx;
344 frame_stat->simulcast_svc_idx = simulcast_svc_idx;
Sergey Silkin3be2a552018-01-17 15:11:44 +0100345 frame_stat->max_nalu_size_bytes = GetMaxNaluSizeBytes(encoded_image, config_);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100346 frame_stat->qp = encoded_image.qp_;
ssilkin612f8582017-09-28 09:23:17 -0700347
Sergey Silkin10d9d592018-02-01 13:25:17 +0100348 if (!config_.IsAsyncCodec()) {
349 // Store encoded frame. It will be decoded after all layers are encoded.
350 CopyEncodedImage(encoded_image, codec, frame_number, simulcast_svc_idx);
351 } else {
352 const size_t simulcast_idx =
353 codec == kVideoCodecVP8 ? codec_specific.codecSpecific.VP8.simulcastIdx
354 : 0;
355 frame_stat->decode_start_ns = rtc::TimeNanos();
356 frame_stat->decode_return_code =
357 decoders_->at(simulcast_idx)->Decode(encoded_image, false, nullptr);
brandtr8935d972017-09-06 01:53:22 -0700358 }
Sergey Silkin3be2a552018-01-17 15:11:44 +0100359
360 ++num_encoded_frames_;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000361}
362
Sergey Silkin64eaa992017-11-17 14:47:32 +0100363void VideoProcessor::FrameDecoded(const VideoFrame& decoded_frame) {
brandtrc8c59052017-08-21 06:44:16 -0700364 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
365
brandtr32e0d262017-02-15 05:29:38 -0800366 // For the highest measurement accuracy of the decode time, the start/stop
367 // time recordings should wrap the Decode call as tightly as possible.
Niels Möllerd28db7f2016-05-10 16:31:47 +0200368 int64_t decode_stop_ns = rtc::TimeNanos();
brandtr8bc93852017-02-15 05:19:51 -0800369
Sergey Silkin10d9d592018-02-01 13:25:17 +0100370 RTC_CHECK(frame_wxh_to_simulcast_svc_idx_.find(decoded_frame.size()) !=
371 frame_wxh_to_simulcast_svc_idx_.end());
372 const size_t simulcast_svc_idx =
373 frame_wxh_to_simulcast_svc_idx_[decoded_frame.size()];
374
Sergey Silkin06a8f302018-02-20 09:48:26 +0100375 FrameStatistics* frame_stat = stats_->GetFrameWithTimestamp(
376 decoded_frame.timestamp(), simulcast_svc_idx);
Åsa Perssona6e7b882018-01-19 14:57:10 +0100377 const size_t frame_number = frame_stat->frame_number;
Sergey Silkin64eaa992017-11-17 14:47:32 +0100378
Sergey Silkin10d9d592018-02-01 13:25:17 +0100379 // Reordering is unexpected. Frames of different layers have the same value
380 // of frame_number.
381 RTC_CHECK_GE(frame_number, last_decoded_frame_num_);
382
383 if (decoded_frame_writers_ && num_decoded_frames_ > 0) {
384 // For dropped frames, write out the last decoded frame to make it look like
385 // a freeze at playback.
386 for (size_t num_dropped_frames = 0; num_dropped_frames < frame_number;
387 ++num_dropped_frames) {
Sergey Silkin06a8f302018-02-20 09:48:26 +0100388 const FrameStatistics* prev_frame_stat = stats_->GetFrame(
389 frame_number - num_dropped_frames - 1, simulcast_svc_idx);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100390 if (prev_frame_stat->decoding_successful) {
391 break;
Sergey Silkin64eaa992017-11-17 14:47:32 +0100392 }
Sergey Silkin10d9d592018-02-01 13:25:17 +0100393 WriteDecodedFrameToFile(&last_decoded_frame_buffers_[simulcast_svc_idx],
394 simulcast_svc_idx);
Sergey Silkin64eaa992017-11-17 14:47:32 +0100395 }
marpan@webrtc.orgf4c2de92012-06-05 21:07:28 +0000396 }
Sergey Silkin10d9d592018-02-01 13:25:17 +0100397
brandtr17b958c2017-03-07 01:41:43 -0800398 last_decoded_frame_num_ = frame_number;
399
Sergey Silkin10d9d592018-02-01 13:25:17 +0100400 // Update frame statistics.
401 frame_stat->decoding_successful = true;
402 frame_stat->decode_time_us =
403 GetElapsedTimeMicroseconds(frame_stat->decode_start_ns, decode_stop_ns);
404 frame_stat->decoded_width = decoded_frame.width();
405 frame_stat->decoded_height = decoded_frame.height();
406
Sergey Silkin64eaa992017-11-17 14:47:32 +0100407 // Skip quality metrics calculation to not affect CPU usage.
408 if (!config_.measure_cpu) {
Sergey Silkin10d9d592018-02-01 13:25:17 +0100409 CalculateFrameQuality(*input_frames_[frame_number], decoded_frame,
410 frame_stat);
Sergey Silkin64eaa992017-11-17 14:47:32 +0100411 }
Niels Möller718a7632016-06-13 13:06:01 +0200412
Sergey Silkin64eaa992017-11-17 14:47:32 +0100413 // Delay erasing of input frames by one frame. The current frame might
414 // still be needed for other simulcast stream or spatial layer.
Sergey Silkin3be2a552018-01-17 15:11:44 +0100415 if (frame_number > 0) {
416 auto input_frame_erase_to = input_frames_.lower_bound(frame_number - 1);
Sergey Silkin64eaa992017-11-17 14:47:32 +0100417 input_frames_.erase(input_frames_.begin(), input_frame_erase_to);
418 }
419
Sergey Silkin10d9d592018-02-01 13:25:17 +0100420 if (decoded_frame_writers_) {
Sergey Silkin64eaa992017-11-17 14:47:32 +0100421 ExtractBufferWithSize(decoded_frame, config_.codec_settings.width,
422 config_.codec_settings.height,
Sergey Silkin10d9d592018-02-01 13:25:17 +0100423 &last_decoded_frame_buffers_[simulcast_svc_idx]);
424 WriteDecodedFrameToFile(&last_decoded_frame_buffers_[simulcast_svc_idx],
425 simulcast_svc_idx);
Sergey Silkin64eaa992017-11-17 14:47:32 +0100426 }
Sergey Silkin3be2a552018-01-17 15:11:44 +0100427
428 ++num_decoded_frames_;
Åsa Perssonf0c44672017-10-24 16:03:39 +0200429}
brandtr17b958c2017-03-07 01:41:43 -0800430
Sergey Silkin10d9d592018-02-01 13:25:17 +0100431void VideoProcessor::CopyEncodedImage(const EncodedImage& encoded_image,
432 const VideoCodecType codec,
433 size_t frame_number,
434 size_t simulcast_svc_idx) {
435 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
436
437 EncodedImage base_image;
438 RTC_CHECK_EQ(base_image._length, 0);
439
440 // Each SVC layer is decoded with dedicated decoder. Add data of base layers
441 // to current coded frame buffer.
442 if (config_.NumberOfSpatialLayers() > 1 && simulcast_svc_idx > 0) {
443 RTC_CHECK(last_encoded_frames_.find(simulcast_svc_idx - 1) !=
444 last_encoded_frames_.end());
445 base_image = last_encoded_frames_[simulcast_svc_idx - 1];
446 }
447
448 const size_t payload_size_bytes = base_image._length + encoded_image._length;
449 const size_t buffer_size_bytes =
450 payload_size_bytes + EncodedImage::GetBufferPaddingBytes(codec);
451
452 uint8_t* copied_buffer = new uint8_t[buffer_size_bytes];
453 RTC_CHECK(copied_buffer);
454
455 if (base_image._length) {
456 memcpy(copied_buffer, base_image._buffer, base_image._length);
457 }
458
459 memcpy(copied_buffer + base_image._length, encoded_image._buffer,
460 encoded_image._length);
461
462 EncodedImage copied_image = encoded_image;
463 copied_image = encoded_image;
464 copied_image._buffer = copied_buffer;
465 copied_image._length = payload_size_bytes;
466 copied_image._size = buffer_size_bytes;
467
468 last_encoded_frames_[simulcast_svc_idx] = copied_image;
469}
470
471void VideoProcessor::CalculateFrameQuality(const VideoFrame& ref_frame,
472 const VideoFrame& dec_frame,
Sergey Silkin06a8f302018-02-20 09:48:26 +0100473 FrameStatistics* frame_stat) {
Sergey Silkin10d9d592018-02-01 13:25:17 +0100474 if (ref_frame.width() == dec_frame.width() ||
475 ref_frame.height() == dec_frame.height()) {
476 frame_stat->psnr = I420PSNR(&ref_frame, &dec_frame);
477 frame_stat->ssim = I420SSIM(&ref_frame, &dec_frame);
478 } else {
479 RTC_CHECK_GE(ref_frame.width(), dec_frame.width());
480 RTC_CHECK_GE(ref_frame.height(), dec_frame.height());
481 // Downscale reference frame. Use bilinear interpolation since it is used
482 // to get lowres inputs for encoder at simulcasting.
483 // TODO(ssilkin): Sync with VP9 SVC which uses 8-taps polyphase.
484 rtc::scoped_refptr<I420Buffer> scaled_buffer =
485 I420Buffer::Create(dec_frame.width(), dec_frame.height());
486 const I420BufferInterface& ref_buffer =
487 *ref_frame.video_frame_buffer()->ToI420();
488 I420Scale(ref_buffer.DataY(), ref_buffer.StrideY(), ref_buffer.DataU(),
489 ref_buffer.StrideU(), ref_buffer.DataV(), ref_buffer.StrideV(),
490 ref_buffer.width(), ref_buffer.height(),
491 scaled_buffer->MutableDataY(), scaled_buffer->StrideY(),
492 scaled_buffer->MutableDataU(), scaled_buffer->StrideU(),
493 scaled_buffer->MutableDataV(), scaled_buffer->StrideV(),
494 scaled_buffer->width(), scaled_buffer->height(),
Sergey Silkin06a8f302018-02-20 09:48:26 +0100495 libyuv::kFilterBox);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100496 frame_stat->psnr =
497 I420PSNR(*scaled_buffer, *dec_frame.video_frame_buffer()->ToI420());
498 frame_stat->ssim =
499 I420SSIM(*scaled_buffer, *dec_frame.video_frame_buffer()->ToI420());
500 }
501}
502
503void VideoProcessor::WriteDecodedFrameToFile(rtc::Buffer* buffer,
504 size_t simulcast_svc_idx) {
505 RTC_CHECK(simulcast_svc_idx < decoded_frame_writers_->size());
506 RTC_DCHECK_EQ(buffer->size(),
507 decoded_frame_writers_->at(simulcast_svc_idx)->FrameLength());
508 RTC_CHECK(decoded_frame_writers_->at(simulcast_svc_idx)
509 ->WriteFrame(buffer->data()));
Åsa Perssonf0c44672017-10-24 16:03:39 +0200510}
brandtr17b958c2017-03-07 01:41:43 -0800511
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000512} // namespace test
513} // namespace webrtc