blob: d8df9375ff4784137700764a31c59c42107aa82c [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"
Rasmus Brandt5f7a8912018-02-28 17:17:15 +010020#include "common_video/libyuv/include/webrtc_libyuv.h"
Sergey Silkin3be2a552018-01-17 15:11:44 +010021#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/video_coding/codecs/vp8/simulcast_rate_allocator.h"
23#include "modules/video_coding/include/video_codec_initializer.h"
Rasmus Brandtd00c8952018-03-14 12:29:57 +010024#include "modules/video_coding/include/video_error_codes.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/video_coding/utility/default_video_bitrate_allocator.h"
26#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "rtc_base/timeutils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "test/gtest.h"
Sergey Silkin8d3758e2018-03-14 11:28:15 +010029#include "third_party/libyuv/include/libyuv/compare.h"
Sergey Silkin10d9d592018-02-01 13:25:17 +010030#include "third_party/libyuv/include/libyuv/scale.h"
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000031
32namespace webrtc {
33namespace test {
34
brandtrb78bc752017-02-22 01:26:59 -080035namespace {
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +010036
Åsa Persson91af24a2018-01-24 17:20:18 +010037const int kMsToRtpTimestamp = kVideoPayloadTypeFrequency / 1000;
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +010038const int kMaxBufferedInputFrames = 10;
brandtr17b958c2017-03-07 01:41:43 -080039
Sergey Silkin3be2a552018-01-17 15:11:44 +010040size_t GetMaxNaluSizeBytes(const EncodedImage& encoded_frame,
41 const TestConfig& config) {
ssilkin612f8582017-09-28 09:23:17 -070042 if (config.codec_settings.codecType != kVideoCodecH264)
Sergey Silkin3be2a552018-01-17 15:11:44 +010043 return 0;
ssilkin612f8582017-09-28 09:23:17 -070044
45 std::vector<webrtc::H264::NaluIndex> nalu_indices =
46 webrtc::H264::FindNaluIndices(encoded_frame._buffer,
47 encoded_frame._length);
48
49 RTC_CHECK(!nalu_indices.empty());
50
Sergey Silkin3be2a552018-01-17 15:11:44 +010051 size_t max_size = 0;
ssilkin612f8582017-09-28 09:23:17 -070052 for (const webrtc::H264::NaluIndex& index : nalu_indices)
Sergey Silkin3be2a552018-01-17 15:11:44 +010053 max_size = std::max(max_size, index.payload_size);
ssilkin612f8582017-09-28 09:23:17 -070054
Sergey Silkin3be2a552018-01-17 15:11:44 +010055 return max_size;
ssilkin612f8582017-09-28 09:23:17 -070056}
57
Rasmus Brandtd062a3c2018-03-08 16:45:54 +010058void GetLayerIndices(const CodecSpecificInfo& codec_specific,
59 size_t* simulcast_svc_idx,
60 size_t* temporal_idx) {
61 if (codec_specific.codecType == kVideoCodecVP8) {
62 *simulcast_svc_idx = codec_specific.codecSpecific.VP8.simulcastIdx;
63 *temporal_idx = codec_specific.codecSpecific.VP8.temporalIdx;
64 } else if (codec_specific.codecType == kVideoCodecVP9) {
65 *simulcast_svc_idx = codec_specific.codecSpecific.VP9.spatial_idx;
66 *temporal_idx = codec_specific.codecSpecific.VP9.temporal_idx;
67 }
68 if (*simulcast_svc_idx == kNoSpatialIdx) {
69 *simulcast_svc_idx = 0;
70 }
71 if (*temporal_idx == kNoTemporalIdx) {
72 *temporal_idx = 0;
73 }
74}
75
asaperssonae9ba042017-03-07 00:25:38 -080076int GetElapsedTimeMicroseconds(int64_t start_ns, int64_t stop_ns) {
77 int64_t diff_us = (stop_ns - start_ns) / rtc::kNumNanosecsPerMicrosec;
78 RTC_DCHECK_GE(diff_us, std::numeric_limits<int>::min());
79 RTC_DCHECK_LE(diff_us, std::numeric_limits<int>::max());
80 return static_cast<int>(diff_us);
81}
82
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +010083void ExtractI420BufferWithSize(const VideoFrame& image,
84 int width,
85 int height,
86 rtc::Buffer* buffer) {
Åsa Perssonf0c44672017-10-24 16:03:39 +020087 if (image.width() != width || image.height() != height) {
88 EXPECT_DOUBLE_EQ(static_cast<double>(width) / height,
89 static_cast<double>(image.width()) / image.height());
90 // Same aspect ratio, no cropping needed.
91 rtc::scoped_refptr<I420Buffer> scaled(I420Buffer::Create(width, height));
92 scaled->ScaleFrom(*image.video_frame_buffer()->ToI420());
93
94 size_t length =
95 CalcBufferSize(VideoType::kI420, scaled->width(), scaled->height());
96 buffer->SetSize(length);
97 RTC_CHECK_NE(ExtractBuffer(scaled, length, buffer->data()), -1);
98 return;
99 }
100
101 // No resize.
102 size_t length =
103 CalcBufferSize(VideoType::kI420, image.width(), image.height());
104 buffer->SetSize(length);
105 RTC_CHECK_NE(ExtractBuffer(image, length, buffer->data()), -1);
106}
107
Sergey Silkin8d3758e2018-03-14 11:28:15 +0100108void CalculateFrameQuality(const I420BufferInterface& ref_buffer,
109 const I420BufferInterface& dec_buffer,
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100110 FrameStatistics* frame_stat) {
Sergey Silkin8d3758e2018-03-14 11:28:15 +0100111 if (ref_buffer.width() != dec_buffer.width() ||
112 ref_buffer.height() != dec_buffer.height()) {
113 RTC_CHECK_GE(ref_buffer.width(), dec_buffer.width());
114 RTC_CHECK_GE(ref_buffer.height(), dec_buffer.height());
115 // Downscale reference frame.
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100116 rtc::scoped_refptr<I420Buffer> scaled_buffer =
Sergey Silkin8d3758e2018-03-14 11:28:15 +0100117 I420Buffer::Create(dec_buffer.width(), dec_buffer.height());
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100118 I420Scale(ref_buffer.DataY(), ref_buffer.StrideY(), ref_buffer.DataU(),
119 ref_buffer.StrideU(), ref_buffer.DataV(), ref_buffer.StrideV(),
120 ref_buffer.width(), ref_buffer.height(),
121 scaled_buffer->MutableDataY(), scaled_buffer->StrideY(),
122 scaled_buffer->MutableDataU(), scaled_buffer->StrideU(),
123 scaled_buffer->MutableDataV(), scaled_buffer->StrideV(),
124 scaled_buffer->width(), scaled_buffer->height(),
125 libyuv::kFilterBox);
Sergey Silkin8d3758e2018-03-14 11:28:15 +0100126
127 CalculateFrameQuality(*scaled_buffer, dec_buffer, frame_stat);
128 } else {
129 const uint64_t sse_y = libyuv::ComputeSumSquareErrorPlane(
130 dec_buffer.DataY(), dec_buffer.StrideY(), ref_buffer.DataY(),
131 ref_buffer.StrideY(), dec_buffer.width(), dec_buffer.height());
132
133 const uint64_t sse_u = libyuv::ComputeSumSquareErrorPlane(
134 dec_buffer.DataU(), dec_buffer.StrideU(), ref_buffer.DataU(),
135 ref_buffer.StrideU(), dec_buffer.width() / 2, dec_buffer.height() / 2);
136
137 const uint64_t sse_v = libyuv::ComputeSumSquareErrorPlane(
138 dec_buffer.DataV(), dec_buffer.StrideV(), ref_buffer.DataV(),
139 ref_buffer.StrideV(), dec_buffer.width() / 2, dec_buffer.height() / 2);
140
141 const size_t num_y_samples = dec_buffer.width() * dec_buffer.height();
142 const size_t num_u_samples =
143 dec_buffer.width() / 2 * dec_buffer.height() / 2;
144
145 frame_stat->psnr_y = libyuv::SumSquareErrorToPsnr(sse_y, num_y_samples);
146 frame_stat->psnr_u = libyuv::SumSquareErrorToPsnr(sse_u, num_u_samples);
147 frame_stat->psnr_v = libyuv::SumSquareErrorToPsnr(sse_v, num_u_samples);
148 frame_stat->psnr = libyuv::SumSquareErrorToPsnr(
149 sse_y + sse_u + sse_v, num_y_samples + 2 * num_u_samples);
150 frame_stat->ssim = I420SSIM(ref_buffer, dec_buffer);
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100151 }
152}
153
brandtrb78bc752017-02-22 01:26:59 -0800154} // namespace
155
brandtrc4095522017-08-07 08:12:33 -0700156VideoProcessor::VideoProcessor(webrtc::VideoEncoder* encoder,
Sergey Silkin10d9d592018-02-01 13:25:17 +0100157 VideoDecoderList* decoders,
158 FrameReader* input_frame_reader,
brandtrc4095522017-08-07 08:12:33 -0700159 const TestConfig& config,
Sergey Silkin06a8f302018-02-20 09:48:26 +0100160 Stats* stats,
Sergey Silkin10d9d592018-02-01 13:25:17 +0100161 IvfFileWriterList* encoded_frame_writers,
162 FrameWriterList* decoded_frame_writers)
Åsa Perssonf0c44672017-10-24 16:03:39 +0200163 : config_(config),
Sergey Silkin10d9d592018-02-01 13:25:17 +0100164 num_simulcast_or_spatial_layers_(
165 std::max(config_.NumberOfSimulcastStreams(),
166 config_.NumberOfSpatialLayers())),
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100167 stats_(stats),
brandtr07734a52017-08-08 08:35:53 -0700168 encoder_(encoder),
Sergey Silkin10d9d592018-02-01 13:25:17 +0100169 decoders_(decoders),
Erik Språng82fad3d2018-03-21 09:57:23 +0100170 bitrate_allocator_(VideoCodecInitializer::CreateBitrateAllocator(
171 config_.codec_settings)),
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100172 framerate_fps_(0),
brandtrbdd555c2017-08-21 01:34:04 -0700173 encode_callback_(this),
174 decode_callback_(this),
Sergey Silkin10d9d592018-02-01 13:25:17 +0100175 input_frame_reader_(input_frame_reader),
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100176 merged_encoded_frames_(num_simulcast_or_spatial_layers_),
Sergey Silkin10d9d592018-02-01 13:25:17 +0100177 encoded_frame_writers_(encoded_frame_writers),
178 decoded_frame_writers_(decoded_frame_writers),
Sergey Silkin3be2a552018-01-17 15:11:44 +0100179 last_inputed_frame_num_(0),
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100180 last_inputed_timestamp_(0),
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100181 first_encoded_frame_(num_simulcast_or_spatial_layers_, true),
182 last_encoded_frame_num_(num_simulcast_or_spatial_layers_),
183 first_decoded_frame_(num_simulcast_or_spatial_layers_, true),
Sergey Silkinc89eed92018-04-01 23:57:51 +0200184 last_decoded_frame_num_(num_simulcast_or_spatial_layers_),
185 post_encode_time_ns_(0) {
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100186 // Sanity checks.
Rasmus Brandt4b381af2018-02-07 13:56:16 +0100187 RTC_CHECK(rtc::TaskQueue::Current())
188 << "VideoProcessor must be run on a task queue.";
Sergey Silkin10d9d592018-02-01 13:25:17 +0100189 RTC_CHECK(encoder);
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100190 RTC_CHECK(decoders);
191 RTC_CHECK_EQ(decoders->size(), num_simulcast_or_spatial_layers_);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100192 RTC_CHECK(input_frame_reader);
193 RTC_CHECK(stats);
194 RTC_CHECK(!encoded_frame_writers ||
195 encoded_frame_writers->size() == num_simulcast_or_spatial_layers_);
196 RTC_CHECK(!decoded_frame_writers ||
197 decoded_frame_writers->size() == num_simulcast_or_spatial_layers_);
brandtr17b958c2017-03-07 01:41:43 -0800198
Sergey Silkin10d9d592018-02-01 13:25:17 +0100199 // Setup required callbacks for the encoder and decoder and initialize them.
brandtrbdd555c2017-08-21 01:34:04 -0700200 RTC_CHECK_EQ(encoder_->RegisterEncodeCompleteCallback(&encode_callback_),
Åsa Perssonf0c44672017-10-24 16:03:39 +0200201 WEBRTC_VIDEO_CODEC_OK);
asapersson654d54c2017-02-10 00:16:07 -0800202
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100203 // Initialize codecs so that they are ready to receive frames.
Sergey Silkin1723cf92018-01-22 15:49:55 +0100204 RTC_CHECK_EQ(encoder_->InitEncode(&config_.codec_settings,
205 static_cast<int>(config_.NumberOfCores()),
206 config_.max_payload_size_bytes),
207 WEBRTC_VIDEO_CODEC_OK);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100208 for (auto& decoder : *decoders_) {
209 RTC_CHECK_EQ(decoder->InitDecode(&config_.codec_settings,
210 static_cast<int>(config_.NumberOfCores())),
211 WEBRTC_VIDEO_CODEC_OK);
212 RTC_CHECK_EQ(decoder->RegisterDecodeCompleteCallback(&decode_callback_),
213 WEBRTC_VIDEO_CODEC_OK);
214 }
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000215}
216
Åsa Perssonf0c44672017-10-24 16:03:39 +0200217VideoProcessor::~VideoProcessor() {
brandtrc8c59052017-08-21 06:44:16 -0700218 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
219
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100220 // Explicitly reset codecs, in case they don't do that themselves when they
221 // go out of scope.
brandtr77920a42017-08-11 07:48:15 -0700222 RTC_CHECK_EQ(encoder_->Release(), WEBRTC_VIDEO_CODEC_OK);
brandtrbdd555c2017-08-21 01:34:04 -0700223 encoder_->RegisterEncodeCompleteCallback(nullptr);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100224 for (auto& decoder : *decoders_) {
225 RTC_CHECK_EQ(decoder->Release(), WEBRTC_VIDEO_CODEC_OK);
226 decoder->RegisterDecodeCompleteCallback(nullptr);
227 }
228
Rasmus Brandtd00c8952018-03-14 12:29:57 +0100229 // Sanity check.
230 RTC_CHECK_LE(input_frames_.size(), kMaxBufferedInputFrames);
231
232 // Deal with manual memory management of EncodedImage's.
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100233 for (size_t simulcast_svc_idx = 0;
234 simulcast_svc_idx < num_simulcast_or_spatial_layers_;
235 ++simulcast_svc_idx) {
236 uint8_t* buffer = merged_encoded_frames_.at(simulcast_svc_idx)._buffer;
237 if (buffer) {
238 delete[] buffer;
239 }
240 }
brandtr77920a42017-08-11 07:48:15 -0700241}
242
brandtr8935d972017-09-06 01:53:22 -0700243void VideoProcessor::ProcessFrame() {
brandtrc8c59052017-08-21 06:44:16 -0700244 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
Sergey Silkin3be2a552018-01-17 15:11:44 +0100245 const size_t frame_number = last_inputed_frame_num_++;
asapersson654d54c2017-02-10 00:16:07 -0800246
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100247 // Get input frame and store for future quality calculation.
248 rtc::scoped_refptr<I420BufferInterface> buffer =
249 input_frame_reader_->ReadFrame();
brandtrbdd555c2017-08-21 01:34:04 -0700250 RTC_CHECK(buffer) << "Tried to read too many frames from the file.";
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100251 const size_t timestamp =
252 last_inputed_timestamp_ + kVideoPayloadTypeFrequency / framerate_fps_;
253 VideoFrame input_frame(buffer, static_cast<uint32_t>(timestamp),
254 static_cast<int64_t>(timestamp / kMsToRtpTimestamp),
255 webrtc::kVideoRotation_0);
Rasmus Brandtd00c8952018-03-14 12:29:57 +0100256 // Store input frame as a reference for quality calculations.
257 if (config_.decode && !config_.measure_cpu) {
258 input_frames_.emplace(frame_number, input_frame);
259 }
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100260 last_inputed_timestamp_ = timestamp;
brandtr17b958c2017-03-07 01:41:43 -0800261
Sergey Silkinc89eed92018-04-01 23:57:51 +0200262 post_encode_time_ns_ = 0;
263
Rasmus Brandt5f7a8912018-02-28 17:17:15 +0100264 // Create frame statistics object for all simulcast/spatial layers.
Sergey Silkin10d9d592018-02-01 13:25:17 +0100265 for (size_t simulcast_svc_idx = 0;
266 simulcast_svc_idx < num_simulcast_or_spatial_layers_;
267 ++simulcast_svc_idx) {
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100268 stats_->AddFrame(timestamp, simulcast_svc_idx);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100269 }
brandtr17b958c2017-03-07 01:41:43 -0800270
271 // For the highest measurement accuracy of the encode time, the start/stop
272 // time recordings should wrap the Encode call as tightly as possible.
Sergey Silkin10d9d592018-02-01 13:25:17 +0100273 const int64_t encode_start_ns = rtc::TimeNanos();
274 for (size_t simulcast_svc_idx = 0;
275 simulcast_svc_idx < num_simulcast_or_spatial_layers_;
276 ++simulcast_svc_idx) {
Sergey Silkin06a8f302018-02-20 09:48:26 +0100277 FrameStatistics* frame_stat =
278 stats_->GetFrame(frame_number, simulcast_svc_idx);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100279 frame_stat->encode_start_ns = encode_start_ns;
280 }
281
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100282 // Encode.
283 const std::vector<FrameType> frame_types =
284 config_.FrameTypeForFrame(frame_number);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100285 const int encode_return_code =
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100286 encoder_->Encode(input_frame, nullptr, &frame_types);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100287 for (size_t simulcast_svc_idx = 0;
288 simulcast_svc_idx < num_simulcast_or_spatial_layers_;
289 ++simulcast_svc_idx) {
Sergey Silkin06a8f302018-02-20 09:48:26 +0100290 FrameStatistics* frame_stat =
291 stats_->GetFrame(frame_number, simulcast_svc_idx);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100292 frame_stat->encode_return_code = encode_return_code;
293 }
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000294}
295
Sergey Silkin3be2a552018-01-17 15:11:44 +0100296void VideoProcessor::SetRates(size_t bitrate_kbps, size_t framerate_fps) {
brandtrc8c59052017-08-21 06:44:16 -0700297 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100298 framerate_fps_ = static_cast<uint32_t>(framerate_fps);
Sergey Silkin3be2a552018-01-17 15:11:44 +0100299 bitrate_allocation_ = bitrate_allocator_->GetAllocation(
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100300 static_cast<uint32_t>(bitrate_kbps * 1000), framerate_fps_);
301 const int set_rates_result =
302 encoder_->SetRateAllocation(bitrate_allocation_, framerate_fps_);
brandtrbea36fd2017-08-07 03:36:54 -0700303 RTC_DCHECK_GE(set_rates_result, 0)
brandtrbdd555c2017-08-21 01:34:04 -0700304 << "Failed to update encoder with new rate " << bitrate_kbps << ".";
brandtrbea36fd2017-08-07 03:36:54 -0700305}
306
Sergey Silkin10d9d592018-02-01 13:25:17 +0100307void VideoProcessor::FrameEncoded(
308 const webrtc::EncodedImage& encoded_image,
309 const webrtc::CodecSpecificInfo& codec_specific) {
brandtrc8c59052017-08-21 06:44:16 -0700310 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
311
brandtr32e0d262017-02-15 05:29:38 -0800312 // For the highest measurement accuracy of the encode time, the start/stop
313 // time recordings should wrap the Encode call as tightly as possible.
Rasmus Brandt5f7a8912018-02-28 17:17:15 +0100314 const int64_t encode_stop_ns = rtc::TimeNanos();
brandtr32e0d262017-02-15 05:29:38 -0800315
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100316 const VideoCodecType codec_type = codec_specific.codecType;
Rasmus Brandtf7a35582017-10-24 10:16:33 +0200317 if (config_.encoded_frame_checker) {
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100318 config_.encoded_frame_checker->CheckEncodedFrame(codec_type, encoded_image);
Rasmus Brandtf7a35582017-10-24 10:16:33 +0200319 }
brandtrb78bc752017-02-22 01:26:59 -0800320
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100321 // Layer metadata.
Sergey Silkin10d9d592018-02-01 13:25:17 +0100322 size_t simulcast_svc_idx = 0;
323 size_t temporal_idx = 0;
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100324 GetLayerIndices(codec_specific, &simulcast_svc_idx, &temporal_idx);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100325 const size_t frame_wxh =
326 encoded_image._encodedWidth * encoded_image._encodedHeight;
327 frame_wxh_to_simulcast_svc_idx_[frame_wxh] = simulcast_svc_idx;
328
Sergey Silkin06a8f302018-02-20 09:48:26 +0100329 FrameStatistics* frame_stat = stats_->GetFrameWithTimestamp(
330 encoded_image._timeStamp, simulcast_svc_idx);
Åsa Perssona6e7b882018-01-19 14:57:10 +0100331 const size_t frame_number = frame_stat->frame_number;
Sergey Silkin10d9d592018-02-01 13:25:17 +0100332
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100333 // Ensure that the encode order is monotonically increasing, within this
334 // simulcast/spatial layer.
335 RTC_CHECK(first_encoded_frame_[simulcast_svc_idx] ||
336 last_encoded_frame_num_[simulcast_svc_idx] < frame_number);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100337
338 // Ensure SVC spatial layers are delivered in ascending order.
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100339 if (!first_encoded_frame_[simulcast_svc_idx] &&
340 config_.NumberOfSpatialLayers() > 1) {
341 for (size_t i = 0; i < simulcast_svc_idx; ++i) {
Sergey Silkin122ba6c2018-03-27 14:32:21 +0200342 RTC_CHECK_LE(last_encoded_frame_num_[i], frame_number);
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100343 }
344 for (size_t i = simulcast_svc_idx + 1; i < num_simulcast_or_spatial_layers_;
345 ++i) {
346 RTC_CHECK_GT(frame_number, last_encoded_frame_num_[i]);
347 }
Sergey Silkin3be2a552018-01-17 15:11:44 +0100348 }
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100349 first_encoded_frame_[simulcast_svc_idx] = false;
350 last_encoded_frame_num_[simulcast_svc_idx] = frame_number;
brandtr17b958c2017-03-07 01:41:43 -0800351
brandtr8935d972017-09-06 01:53:22 -0700352 // Update frame statistics.
Sergey Silkin10d9d592018-02-01 13:25:17 +0100353 frame_stat->encoding_successful = true;
Sergey Silkinc89eed92018-04-01 23:57:51 +0200354 frame_stat->encode_time_us = GetElapsedTimeMicroseconds(
355 frame_stat->encode_start_ns, encode_stop_ns - post_encode_time_ns_);
Sergey Silkin86684962018-03-28 19:32:37 +0200356 frame_stat->target_bitrate_kbps = (bitrate_allocation_.GetTemporalLayerSum(
357 simulcast_svc_idx, temporal_idx) +
358 500) /
359 1000;
Sergey Silkind4bc01b2018-03-09 14:31:24 +0100360 frame_stat->length_bytes = encoded_image._length;
brandtr17b958c2017-03-07 01:41:43 -0800361 frame_stat->frame_type = encoded_image._frameType;
Sergey Silkin10d9d592018-02-01 13:25:17 +0100362 frame_stat->temporal_layer_idx = temporal_idx;
363 frame_stat->simulcast_svc_idx = simulcast_svc_idx;
Sergey Silkin86684962018-03-28 19:32:37 +0200364 if (codec_type == kVideoCodecVP9) {
365 const CodecSpecificInfoVP9& vp9_info = codec_specific.codecSpecific.VP9;
366 frame_stat->inter_layer_predicted = vp9_info.inter_layer_predicted;
367 }
Sergey Silkin3be2a552018-01-17 15:11:44 +0100368 frame_stat->max_nalu_size_bytes = GetMaxNaluSizeBytes(encoded_image, config_);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100369 frame_stat->qp = encoded_image.qp_;
ssilkin612f8582017-09-28 09:23:17 -0700370
Sergey Silkin122ba6c2018-03-27 14:32:21 +0200371 const webrtc::EncodedImage* encoded_image_for_decode = &encoded_image;
Rasmus Brandtd00c8952018-03-14 12:29:57 +0100372 if (config_.decode) {
Rasmus Brandtd00c8952018-03-14 12:29:57 +0100373 if (config_.NumberOfSpatialLayers() > 1) {
374 encoded_image_for_decode = MergeAndStoreEncodedImageForSvcDecoding(
375 encoded_image, codec_type, frame_number, simulcast_svc_idx);
376 }
377 frame_stat->decode_start_ns = rtc::TimeNanos();
378 frame_stat->decode_return_code =
379 decoders_->at(simulcast_svc_idx)
380 ->Decode(*encoded_image_for_decode, false, nullptr);
381 } else {
382 frame_stat->decode_return_code = WEBRTC_VIDEO_CODEC_NO_OUTPUT;
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100383 }
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100384
385 if (encoded_frame_writers_) {
Sergey Silkin122ba6c2018-03-27 14:32:21 +0200386 RTC_CHECK(encoded_frame_writers_->at(simulcast_svc_idx)
387 ->WriteFrame(*encoded_image_for_decode,
388 config_.codec_settings.codecType));
brandtr8935d972017-09-06 01:53:22 -0700389 }
Sergey Silkinc89eed92018-04-01 23:57:51 +0200390
391 if (!config_.IsAsyncCodec()) {
392 // To get pure encode time for next layers, measure time spent in encode
393 // callback and subtract it from encode time of next layers.
394 post_encode_time_ns_ += rtc::TimeNanos() - encode_stop_ns;
395 }
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000396}
397
Sergey Silkin64eaa992017-11-17 14:47:32 +0100398void VideoProcessor::FrameDecoded(const VideoFrame& decoded_frame) {
brandtrc8c59052017-08-21 06:44:16 -0700399 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
400
brandtr32e0d262017-02-15 05:29:38 -0800401 // For the highest measurement accuracy of the decode time, the start/stop
402 // time recordings should wrap the Decode call as tightly as possible.
Rasmus Brandt5f7a8912018-02-28 17:17:15 +0100403 const int64_t decode_stop_ns = rtc::TimeNanos();
brandtr8bc93852017-02-15 05:19:51 -0800404
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100405 // Layer metadata.
Sergey Silkin10d9d592018-02-01 13:25:17 +0100406 const size_t simulcast_svc_idx =
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100407 frame_wxh_to_simulcast_svc_idx_.at(decoded_frame.size());
Sergey Silkin06a8f302018-02-20 09:48:26 +0100408 FrameStatistics* frame_stat = stats_->GetFrameWithTimestamp(
409 decoded_frame.timestamp(), simulcast_svc_idx);
Åsa Perssona6e7b882018-01-19 14:57:10 +0100410 const size_t frame_number = frame_stat->frame_number;
Sergey Silkin64eaa992017-11-17 14:47:32 +0100411
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100412 // Ensure that the decode order is monotonically increasing, within this
413 // simulcast/spatial layer.
414 RTC_CHECK(first_decoded_frame_[simulcast_svc_idx] ||
415 last_decoded_frame_num_[simulcast_svc_idx] < frame_number);
416 first_decoded_frame_[simulcast_svc_idx] = false;
417 last_decoded_frame_num_[simulcast_svc_idx] = frame_number;
brandtr17b958c2017-03-07 01:41:43 -0800418
Sergey Silkin10d9d592018-02-01 13:25:17 +0100419 // Update frame statistics.
420 frame_stat->decoding_successful = true;
421 frame_stat->decode_time_us =
422 GetElapsedTimeMicroseconds(frame_stat->decode_start_ns, decode_stop_ns);
423 frame_stat->decoded_width = decoded_frame.width();
424 frame_stat->decoded_height = decoded_frame.height();
425
Sergey Silkin64eaa992017-11-17 14:47:32 +0100426 // Skip quality metrics calculation to not affect CPU usage.
427 if (!config_.measure_cpu) {
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100428 const auto reference_frame = input_frames_.find(frame_number);
429 RTC_CHECK(reference_frame != input_frames_.cend())
430 << "The codecs are either buffering too much, dropping too much, or "
431 "being too slow relative the input frame rate.";
Sergey Silkin8d3758e2018-03-14 11:28:15 +0100432 CalculateFrameQuality(
433 *reference_frame->second.video_frame_buffer()->ToI420(),
434 *decoded_frame.video_frame_buffer()->ToI420(), frame_stat);
Niels Möller718a7632016-06-13 13:06:01 +0200435
Rasmus Brandtd00c8952018-03-14 12:29:57 +0100436 // Erase all buffered input frames that we have moved past for all
437 // simulcast/spatial layers. Never buffer more than
438 // |kMaxBufferedInputFrames| frames, to protect against long runs of
439 // consecutive frame drops for a particular layer.
440 const auto min_last_decoded_frame_num = std::min_element(
441 last_decoded_frame_num_.cbegin(), last_decoded_frame_num_.cend());
442 const size_t min_buffered_frame_num = std::max(
443 0, static_cast<int>(frame_number) - kMaxBufferedInputFrames + 1);
444 RTC_CHECK(min_last_decoded_frame_num != last_decoded_frame_num_.cend());
445 const auto input_frames_erase_before = input_frames_.lower_bound(
446 std::max(*min_last_decoded_frame_num, min_buffered_frame_num));
447 input_frames_.erase(input_frames_.cbegin(), input_frames_erase_before);
448 }
Sergey Silkin64eaa992017-11-17 14:47:32 +0100449
Sergey Silkin10d9d592018-02-01 13:25:17 +0100450 if (decoded_frame_writers_) {
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100451 ExtractI420BufferWithSize(decoded_frame, config_.codec_settings.width,
452 config_.codec_settings.height, &tmp_i420_buffer_);
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100453 RTC_CHECK_EQ(tmp_i420_buffer_.size(),
Rasmus Brandt5f7a8912018-02-28 17:17:15 +0100454 decoded_frame_writers_->at(simulcast_svc_idx)->FrameLength());
455 RTC_CHECK(decoded_frame_writers_->at(simulcast_svc_idx)
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100456 ->WriteFrame(tmp_i420_buffer_.data()));
Sergey Silkin64eaa992017-11-17 14:47:32 +0100457 }
Åsa Perssonf0c44672017-10-24 16:03:39 +0200458}
brandtr17b958c2017-03-07 01:41:43 -0800459
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100460const webrtc::EncodedImage*
461VideoProcessor::MergeAndStoreEncodedImageForSvcDecoding(
462 const EncodedImage& encoded_image,
463 const VideoCodecType codec,
464 size_t frame_number,
465 size_t simulcast_svc_idx) {
466 // Should only be called for SVC.
467 RTC_CHECK_GT(config_.NumberOfSpatialLayers(), 1);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100468
469 EncodedImage base_image;
470 RTC_CHECK_EQ(base_image._length, 0);
471
Sergey Silkin122ba6c2018-03-27 14:32:21 +0200472 // Each SVC layer is decoded with dedicated decoder. Find the nearest
473 // non-dropped base frame and merge it and current frame into superframe.
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100474 if (simulcast_svc_idx > 0) {
Sergey Silkin122ba6c2018-03-27 14:32:21 +0200475 for (int base_idx = static_cast<int>(simulcast_svc_idx) - 1; base_idx >= 0;
476 --base_idx) {
477 EncodedImage lower_layer = merged_encoded_frames_.at(base_idx);
478 if (lower_layer._timeStamp == encoded_image._timeStamp) {
479 base_image = lower_layer;
480 break;
481 }
482 }
Sergey Silkin10d9d592018-02-01 13:25:17 +0100483 }
Sergey Silkin10d9d592018-02-01 13:25:17 +0100484 const size_t payload_size_bytes = base_image._length + encoded_image._length;
485 const size_t buffer_size_bytes =
486 payload_size_bytes + EncodedImage::GetBufferPaddingBytes(codec);
487
488 uint8_t* copied_buffer = new uint8_t[buffer_size_bytes];
489 RTC_CHECK(copied_buffer);
490
491 if (base_image._length) {
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100492 RTC_CHECK(base_image._buffer);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100493 memcpy(copied_buffer, base_image._buffer, base_image._length);
494 }
Sergey Silkin10d9d592018-02-01 13:25:17 +0100495 memcpy(copied_buffer + base_image._length, encoded_image._buffer,
496 encoded_image._length);
497
498 EncodedImage copied_image = encoded_image;
499 copied_image = encoded_image;
500 copied_image._buffer = copied_buffer;
501 copied_image._length = payload_size_bytes;
502 copied_image._size = buffer_size_bytes;
503
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100504 // Replace previous EncodedImage for this spatial layer.
505 uint8_t* old_buffer = merged_encoded_frames_.at(simulcast_svc_idx)._buffer;
506 if (old_buffer) {
507 delete[] old_buffer;
508 }
509 merged_encoded_frames_.at(simulcast_svc_idx) = copied_image;
510
511 return &merged_encoded_frames_.at(simulcast_svc_idx);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100512}
513
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000514} // namespace test
515} // namespace webrtc