blob: 8117444ddb27978a07632304817576bb7e5a616b [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"
24#include "modules/video_coding/utility/default_video_bitrate_allocator.h"
25#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "rtc_base/timeutils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "test/gtest.h"
Sergey Silkin10d9d592018-02-01 13:25:17 +010028#include "third_party/libyuv/include/libyuv/scale.h"
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000029
30namespace webrtc {
31namespace test {
32
brandtrb78bc752017-02-22 01:26:59 -080033namespace {
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +010034
Åsa Persson91af24a2018-01-24 17:20:18 +010035const int kMsToRtpTimestamp = kVideoPayloadTypeFrequency / 1000;
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +010036const int kMaxBufferedInputFrames = 10;
brandtr17b958c2017-03-07 01:41:43 -080037
brandtraebc61e2017-02-28 07:13:47 -080038std::unique_ptr<VideoBitrateAllocator> CreateBitrateAllocator(
brandtr07734a52017-08-08 08:35:53 -070039 TestConfig* config) {
brandtraebc61e2017-02-28 07:13:47 -080040 std::unique_ptr<TemporalLayersFactory> tl_factory;
brandtr07734a52017-08-08 08:35:53 -070041 if (config->codec_settings.codecType == VideoCodecType::kVideoCodecVP8) {
brandtraebc61e2017-02-28 07:13:47 -080042 tl_factory.reset(new TemporalLayersFactory());
brandtr07734a52017-08-08 08:35:53 -070043 config->codec_settings.VP8()->tl_factory = tl_factory.get();
brandtraebc61e2017-02-28 07:13:47 -080044 }
45 return std::unique_ptr<VideoBitrateAllocator>(
brandtr07734a52017-08-08 08:35:53 -070046 VideoCodecInitializer::CreateBitrateAllocator(config->codec_settings,
brandtraebc61e2017-02-28 07:13:47 -080047 std::move(tl_factory)));
48}
49
Sergey Silkin3be2a552018-01-17 15:11:44 +010050size_t GetMaxNaluSizeBytes(const EncodedImage& encoded_frame,
51 const TestConfig& config) {
ssilkin612f8582017-09-28 09:23:17 -070052 if (config.codec_settings.codecType != kVideoCodecH264)
Sergey Silkin3be2a552018-01-17 15:11:44 +010053 return 0;
ssilkin612f8582017-09-28 09:23:17 -070054
55 std::vector<webrtc::H264::NaluIndex> nalu_indices =
56 webrtc::H264::FindNaluIndices(encoded_frame._buffer,
57 encoded_frame._length);
58
59 RTC_CHECK(!nalu_indices.empty());
60
Sergey Silkin3be2a552018-01-17 15:11:44 +010061 size_t max_size = 0;
ssilkin612f8582017-09-28 09:23:17 -070062 for (const webrtc::H264::NaluIndex& index : nalu_indices)
Sergey Silkin3be2a552018-01-17 15:11:44 +010063 max_size = std::max(max_size, index.payload_size);
ssilkin612f8582017-09-28 09:23:17 -070064
Sergey Silkin3be2a552018-01-17 15:11:44 +010065 return max_size;
ssilkin612f8582017-09-28 09:23:17 -070066}
67
Rasmus Brandtd062a3c2018-03-08 16:45:54 +010068void GetLayerIndices(const CodecSpecificInfo& codec_specific,
69 size_t* simulcast_svc_idx,
70 size_t* temporal_idx) {
71 if (codec_specific.codecType == kVideoCodecVP8) {
72 *simulcast_svc_idx = codec_specific.codecSpecific.VP8.simulcastIdx;
73 *temporal_idx = codec_specific.codecSpecific.VP8.temporalIdx;
74 } else if (codec_specific.codecType == kVideoCodecVP9) {
75 *simulcast_svc_idx = codec_specific.codecSpecific.VP9.spatial_idx;
76 *temporal_idx = codec_specific.codecSpecific.VP9.temporal_idx;
77 }
78 if (*simulcast_svc_idx == kNoSpatialIdx) {
79 *simulcast_svc_idx = 0;
80 }
81 if (*temporal_idx == kNoTemporalIdx) {
82 *temporal_idx = 0;
83 }
84}
85
asaperssonae9ba042017-03-07 00:25:38 -080086int GetElapsedTimeMicroseconds(int64_t start_ns, int64_t stop_ns) {
87 int64_t diff_us = (stop_ns - start_ns) / rtc::kNumNanosecsPerMicrosec;
88 RTC_DCHECK_GE(diff_us, std::numeric_limits<int>::min());
89 RTC_DCHECK_LE(diff_us, std::numeric_limits<int>::max());
90 return static_cast<int>(diff_us);
91}
92
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +010093void ExtractI420BufferWithSize(const VideoFrame& image,
94 int width,
95 int height,
96 rtc::Buffer* buffer) {
Åsa Perssonf0c44672017-10-24 16:03:39 +020097 if (image.width() != width || image.height() != height) {
98 EXPECT_DOUBLE_EQ(static_cast<double>(width) / height,
99 static_cast<double>(image.width()) / image.height());
100 // Same aspect ratio, no cropping needed.
101 rtc::scoped_refptr<I420Buffer> scaled(I420Buffer::Create(width, height));
102 scaled->ScaleFrom(*image.video_frame_buffer()->ToI420());
103
104 size_t length =
105 CalcBufferSize(VideoType::kI420, scaled->width(), scaled->height());
106 buffer->SetSize(length);
107 RTC_CHECK_NE(ExtractBuffer(scaled, length, buffer->data()), -1);
108 return;
109 }
110
111 // No resize.
112 size_t length =
113 CalcBufferSize(VideoType::kI420, image.width(), image.height());
114 buffer->SetSize(length);
115 RTC_CHECK_NE(ExtractBuffer(image, length, buffer->data()), -1);
116}
117
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100118void CalculateFrameQuality(const VideoFrame& ref_frame,
119 const VideoFrame& dec_frame,
120 FrameStatistics* frame_stat) {
121 if (ref_frame.width() == dec_frame.width() ||
122 ref_frame.height() == dec_frame.height()) {
123 frame_stat->psnr = I420PSNR(&ref_frame, &dec_frame);
124 frame_stat->ssim = I420SSIM(&ref_frame, &dec_frame);
125 } else {
126 RTC_CHECK_GE(ref_frame.width(), dec_frame.width());
127 RTC_CHECK_GE(ref_frame.height(), dec_frame.height());
128 // Downscale reference frame. Use bilinear interpolation since it is used
129 // to get lowres inputs for encoder at simulcasting.
130 // TODO(ssilkin): Sync with VP9 SVC which uses 8-taps polyphase.
131 rtc::scoped_refptr<I420Buffer> scaled_buffer =
132 I420Buffer::Create(dec_frame.width(), dec_frame.height());
133 const I420BufferInterface& ref_buffer =
134 *ref_frame.video_frame_buffer()->ToI420();
135 I420Scale(ref_buffer.DataY(), ref_buffer.StrideY(), ref_buffer.DataU(),
136 ref_buffer.StrideU(), ref_buffer.DataV(), ref_buffer.StrideV(),
137 ref_buffer.width(), ref_buffer.height(),
138 scaled_buffer->MutableDataY(), scaled_buffer->StrideY(),
139 scaled_buffer->MutableDataU(), scaled_buffer->StrideU(),
140 scaled_buffer->MutableDataV(), scaled_buffer->StrideV(),
141 scaled_buffer->width(), scaled_buffer->height(),
142 libyuv::kFilterBox);
143 frame_stat->psnr =
144 I420PSNR(*scaled_buffer, *dec_frame.video_frame_buffer()->ToI420());
145 frame_stat->ssim =
146 I420SSIM(*scaled_buffer, *dec_frame.video_frame_buffer()->ToI420());
147 }
148}
149
brandtrb78bc752017-02-22 01:26:59 -0800150} // namespace
151
brandtrc4095522017-08-07 08:12:33 -0700152VideoProcessor::VideoProcessor(webrtc::VideoEncoder* encoder,
Sergey Silkin10d9d592018-02-01 13:25:17 +0100153 VideoDecoderList* decoders,
154 FrameReader* input_frame_reader,
brandtrc4095522017-08-07 08:12:33 -0700155 const TestConfig& config,
Sergey Silkin06a8f302018-02-20 09:48:26 +0100156 Stats* stats,
Sergey Silkin10d9d592018-02-01 13:25:17 +0100157 IvfFileWriterList* encoded_frame_writers,
158 FrameWriterList* decoded_frame_writers)
Åsa Perssonf0c44672017-10-24 16:03:39 +0200159 : config_(config),
Sergey Silkin10d9d592018-02-01 13:25:17 +0100160 num_simulcast_or_spatial_layers_(
161 std::max(config_.NumberOfSimulcastStreams(),
162 config_.NumberOfSpatialLayers())),
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100163 stats_(stats),
brandtr07734a52017-08-08 08:35:53 -0700164 encoder_(encoder),
Sergey Silkin10d9d592018-02-01 13:25:17 +0100165 decoders_(decoders),
brandtr07734a52017-08-08 08:35:53 -0700166 bitrate_allocator_(CreateBitrateAllocator(&config_)),
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100167 framerate_fps_(0),
brandtrbdd555c2017-08-21 01:34:04 -0700168 encode_callback_(this),
169 decode_callback_(this),
Sergey Silkin10d9d592018-02-01 13:25:17 +0100170 input_frame_reader_(input_frame_reader),
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100171 merged_encoded_frames_(num_simulcast_or_spatial_layers_),
Sergey Silkin10d9d592018-02-01 13:25:17 +0100172 encoded_frame_writers_(encoded_frame_writers),
173 decoded_frame_writers_(decoded_frame_writers),
Sergey Silkin3be2a552018-01-17 15:11:44 +0100174 last_inputed_frame_num_(0),
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100175 last_inputed_timestamp_(0),
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100176 first_encoded_frame_(num_simulcast_or_spatial_layers_, true),
177 last_encoded_frame_num_(num_simulcast_or_spatial_layers_),
178 first_decoded_frame_(num_simulcast_or_spatial_layers_, true),
179 last_decoded_frame_num_(num_simulcast_or_spatial_layers_) {
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100180 // Sanity checks.
Rasmus Brandt4b381af2018-02-07 13:56:16 +0100181 RTC_CHECK(rtc::TaskQueue::Current())
182 << "VideoProcessor must be run on a task queue.";
Sergey Silkin10d9d592018-02-01 13:25:17 +0100183 RTC_CHECK(encoder);
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100184 RTC_CHECK(decoders);
185 RTC_CHECK_EQ(decoders->size(), num_simulcast_or_spatial_layers_);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100186 RTC_CHECK(input_frame_reader);
187 RTC_CHECK(stats);
188 RTC_CHECK(!encoded_frame_writers ||
189 encoded_frame_writers->size() == num_simulcast_or_spatial_layers_);
190 RTC_CHECK(!decoded_frame_writers ||
191 decoded_frame_writers->size() == num_simulcast_or_spatial_layers_);
brandtr17b958c2017-03-07 01:41:43 -0800192
Sergey Silkin10d9d592018-02-01 13:25:17 +0100193 // Setup required callbacks for the encoder and decoder and initialize them.
brandtrbdd555c2017-08-21 01:34:04 -0700194 RTC_CHECK_EQ(encoder_->RegisterEncodeCompleteCallback(&encode_callback_),
Åsa Perssonf0c44672017-10-24 16:03:39 +0200195 WEBRTC_VIDEO_CODEC_OK);
asapersson654d54c2017-02-10 00:16:07 -0800196
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100197 // Initialize codecs so that they are ready to receive frames.
Sergey Silkin1723cf92018-01-22 15:49:55 +0100198 RTC_CHECK_EQ(encoder_->InitEncode(&config_.codec_settings,
199 static_cast<int>(config_.NumberOfCores()),
200 config_.max_payload_size_bytes),
201 WEBRTC_VIDEO_CODEC_OK);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100202 for (auto& decoder : *decoders_) {
203 RTC_CHECK_EQ(decoder->InitDecode(&config_.codec_settings,
204 static_cast<int>(config_.NumberOfCores())),
205 WEBRTC_VIDEO_CODEC_OK);
206 RTC_CHECK_EQ(decoder->RegisterDecodeCompleteCallback(&decode_callback_),
207 WEBRTC_VIDEO_CODEC_OK);
208 }
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000209}
210
Åsa Perssonf0c44672017-10-24 16:03:39 +0200211VideoProcessor::~VideoProcessor() {
brandtrc8c59052017-08-21 06:44:16 -0700212 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
213
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100214 // Explicitly reset codecs, in case they don't do that themselves when they
215 // go out of scope.
brandtr77920a42017-08-11 07:48:15 -0700216 RTC_CHECK_EQ(encoder_->Release(), WEBRTC_VIDEO_CODEC_OK);
brandtrbdd555c2017-08-21 01:34:04 -0700217 encoder_->RegisterEncodeCompleteCallback(nullptr);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100218 for (auto& decoder : *decoders_) {
219 RTC_CHECK_EQ(decoder->Release(), WEBRTC_VIDEO_CODEC_OK);
220 decoder->RegisterDecodeCompleteCallback(nullptr);
221 }
222
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100223 for (size_t simulcast_svc_idx = 0;
224 simulcast_svc_idx < num_simulcast_or_spatial_layers_;
225 ++simulcast_svc_idx) {
226 uint8_t* buffer = merged_encoded_frames_.at(simulcast_svc_idx)._buffer;
227 if (buffer) {
228 delete[] buffer;
229 }
230 }
brandtr77920a42017-08-11 07:48:15 -0700231}
232
brandtr8935d972017-09-06 01:53:22 -0700233void VideoProcessor::ProcessFrame() {
brandtrc8c59052017-08-21 06:44:16 -0700234 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
Sergey Silkin3be2a552018-01-17 15:11:44 +0100235 const size_t frame_number = last_inputed_frame_num_++;
asapersson654d54c2017-02-10 00:16:07 -0800236
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100237 // Get input frame and store for future quality calculation.
238 rtc::scoped_refptr<I420BufferInterface> buffer =
239 input_frame_reader_->ReadFrame();
brandtrbdd555c2017-08-21 01:34:04 -0700240 RTC_CHECK(buffer) << "Tried to read too many frames from the file.";
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100241 const size_t timestamp =
242 last_inputed_timestamp_ + kVideoPayloadTypeFrequency / framerate_fps_;
243 VideoFrame input_frame(buffer, static_cast<uint32_t>(timestamp),
244 static_cast<int64_t>(timestamp / kMsToRtpTimestamp),
245 webrtc::kVideoRotation_0);
246 input_frames_.emplace(frame_number, input_frame);
247 last_inputed_timestamp_ = timestamp;
brandtr17b958c2017-03-07 01:41:43 -0800248
Rasmus Brandt5f7a8912018-02-28 17:17:15 +0100249 // Create frame statistics object for all simulcast/spatial layers.
Sergey Silkin10d9d592018-02-01 13:25:17 +0100250 for (size_t simulcast_svc_idx = 0;
251 simulcast_svc_idx < num_simulcast_or_spatial_layers_;
252 ++simulcast_svc_idx) {
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100253 stats_->AddFrame(timestamp, simulcast_svc_idx);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100254 }
brandtr17b958c2017-03-07 01:41:43 -0800255
256 // For the highest measurement accuracy of the encode time, the start/stop
257 // time recordings should wrap the Encode call as tightly as possible.
Sergey Silkin10d9d592018-02-01 13:25:17 +0100258 const int64_t encode_start_ns = rtc::TimeNanos();
259 for (size_t simulcast_svc_idx = 0;
260 simulcast_svc_idx < num_simulcast_or_spatial_layers_;
261 ++simulcast_svc_idx) {
Sergey Silkin06a8f302018-02-20 09:48:26 +0100262 FrameStatistics* frame_stat =
263 stats_->GetFrame(frame_number, simulcast_svc_idx);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100264 frame_stat->encode_start_ns = encode_start_ns;
265 }
266
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100267 // Encode.
268 const std::vector<FrameType> frame_types =
269 config_.FrameTypeForFrame(frame_number);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100270 const int encode_return_code =
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100271 encoder_->Encode(input_frame, nullptr, &frame_types);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100272 for (size_t simulcast_svc_idx = 0;
273 simulcast_svc_idx < num_simulcast_or_spatial_layers_;
274 ++simulcast_svc_idx) {
Sergey Silkin06a8f302018-02-20 09:48:26 +0100275 FrameStatistics* frame_stat =
276 stats_->GetFrame(frame_number, simulcast_svc_idx);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100277 frame_stat->encode_return_code = encode_return_code;
278 }
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000279}
280
Sergey Silkin3be2a552018-01-17 15:11:44 +0100281void VideoProcessor::SetRates(size_t bitrate_kbps, size_t framerate_fps) {
brandtrc8c59052017-08-21 06:44:16 -0700282 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100283 framerate_fps_ = static_cast<uint32_t>(framerate_fps);
Sergey Silkin3be2a552018-01-17 15:11:44 +0100284 bitrate_allocation_ = bitrate_allocator_->GetAllocation(
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100285 static_cast<uint32_t>(bitrate_kbps * 1000), framerate_fps_);
286 const int set_rates_result =
287 encoder_->SetRateAllocation(bitrate_allocation_, framerate_fps_);
brandtrbea36fd2017-08-07 03:36:54 -0700288 RTC_DCHECK_GE(set_rates_result, 0)
brandtrbdd555c2017-08-21 01:34:04 -0700289 << "Failed to update encoder with new rate " << bitrate_kbps << ".";
brandtrbea36fd2017-08-07 03:36:54 -0700290}
291
Sergey Silkin10d9d592018-02-01 13:25:17 +0100292void VideoProcessor::FrameEncoded(
293 const webrtc::EncodedImage& encoded_image,
294 const webrtc::CodecSpecificInfo& codec_specific) {
brandtrc8c59052017-08-21 06:44:16 -0700295 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
296
brandtr32e0d262017-02-15 05:29:38 -0800297 // For the highest measurement accuracy of the encode time, the start/stop
298 // time recordings should wrap the Encode call as tightly as possible.
Rasmus Brandt5f7a8912018-02-28 17:17:15 +0100299 const int64_t encode_stop_ns = rtc::TimeNanos();
brandtr32e0d262017-02-15 05:29:38 -0800300
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100301 const VideoCodecType codec_type = codec_specific.codecType;
Rasmus Brandtf7a35582017-10-24 10:16:33 +0200302 if (config_.encoded_frame_checker) {
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100303 config_.encoded_frame_checker->CheckEncodedFrame(codec_type, encoded_image);
Rasmus Brandtf7a35582017-10-24 10:16:33 +0200304 }
brandtrb78bc752017-02-22 01:26:59 -0800305
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100306 // Layer metadata.
Sergey Silkin10d9d592018-02-01 13:25:17 +0100307 size_t simulcast_svc_idx = 0;
308 size_t temporal_idx = 0;
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100309 GetLayerIndices(codec_specific, &simulcast_svc_idx, &temporal_idx);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100310 const size_t frame_wxh =
311 encoded_image._encodedWidth * encoded_image._encodedHeight;
312 frame_wxh_to_simulcast_svc_idx_[frame_wxh] = simulcast_svc_idx;
313
Sergey Silkin06a8f302018-02-20 09:48:26 +0100314 FrameStatistics* frame_stat = stats_->GetFrameWithTimestamp(
315 encoded_image._timeStamp, simulcast_svc_idx);
Åsa Perssona6e7b882018-01-19 14:57:10 +0100316 const size_t frame_number = frame_stat->frame_number;
Sergey Silkin10d9d592018-02-01 13:25:17 +0100317
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100318 // Ensure that the encode order is monotonically increasing, within this
319 // simulcast/spatial layer.
320 RTC_CHECK(first_encoded_frame_[simulcast_svc_idx] ||
321 last_encoded_frame_num_[simulcast_svc_idx] < frame_number);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100322
323 // Ensure SVC spatial layers are delivered in ascending order.
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100324 if (!first_encoded_frame_[simulcast_svc_idx] &&
325 config_.NumberOfSpatialLayers() > 1) {
326 for (size_t i = 0; i < simulcast_svc_idx; ++i) {
327 RTC_CHECK_EQ(last_encoded_frame_num_[i], frame_number);
328 }
329 for (size_t i = simulcast_svc_idx + 1; i < num_simulcast_or_spatial_layers_;
330 ++i) {
331 RTC_CHECK_GT(frame_number, last_encoded_frame_num_[i]);
332 }
Sergey Silkin3be2a552018-01-17 15:11:44 +0100333 }
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100334 first_encoded_frame_[simulcast_svc_idx] = false;
335 last_encoded_frame_num_[simulcast_svc_idx] = frame_number;
brandtr17b958c2017-03-07 01:41:43 -0800336
brandtr8935d972017-09-06 01:53:22 -0700337 // Update frame statistics.
Sergey Silkin10d9d592018-02-01 13:25:17 +0100338 frame_stat->encoding_successful = true;
brandtr8935d972017-09-06 01:53:22 -0700339 frame_stat->encode_time_us =
340 GetElapsedTimeMicroseconds(frame_stat->encode_start_ns, encode_stop_ns);
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100341 if (codec_type == kVideoCodecVP9) {
Sergey Silkin06a8f302018-02-20 09:48:26 +0100342 const CodecSpecificInfoVP9& vp9_info = codec_specific.codecSpecific.VP9;
343 frame_stat->inter_layer_predicted = vp9_info.inter_layer_predicted;
344
345 // TODO(ssilkin): Implement bitrate allocation for VP9 SVC. For now set
346 // target for base layers equal to total target to avoid devision by zero
347 // at analysis.
348 frame_stat->target_bitrate_kbps = bitrate_allocation_.get_sum_kbps();
349 } else {
350 frame_stat->target_bitrate_kbps =
351 (bitrate_allocation_.GetBitrate(simulcast_svc_idx, temporal_idx) +
352 500) /
353 1000;
354 }
Sergey Silkind4bc01b2018-03-09 14:31:24 +0100355 frame_stat->length_bytes = encoded_image._length;
brandtr17b958c2017-03-07 01:41:43 -0800356 frame_stat->frame_type = encoded_image._frameType;
Sergey Silkin10d9d592018-02-01 13:25:17 +0100357 frame_stat->temporal_layer_idx = temporal_idx;
358 frame_stat->simulcast_svc_idx = simulcast_svc_idx;
Sergey Silkin3be2a552018-01-17 15:11:44 +0100359 frame_stat->max_nalu_size_bytes = GetMaxNaluSizeBytes(encoded_image, config_);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100360 frame_stat->qp = encoded_image.qp_;
ssilkin612f8582017-09-28 09:23:17 -0700361
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100362 // Decode.
363 const webrtc::EncodedImage* encoded_image_for_decode = &encoded_image;
364 if (config_.NumberOfSpatialLayers() > 1) {
365 encoded_image_for_decode = MergeAndStoreEncodedImageForSvcDecoding(
366 encoded_image, codec_type, frame_number, simulcast_svc_idx);
367 }
368 frame_stat->decode_start_ns = rtc::TimeNanos();
369 frame_stat->decode_return_code =
370 decoders_->at(simulcast_svc_idx)
371 ->Decode(*encoded_image_for_decode, false, nullptr);
372
373 if (encoded_frame_writers_) {
374 RTC_CHECK(
375 encoded_frame_writers_->at(simulcast_svc_idx)
376 ->WriteFrame(encoded_image, config_.codec_settings.codecType));
brandtr8935d972017-09-06 01:53:22 -0700377 }
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000378}
379
Sergey Silkin64eaa992017-11-17 14:47:32 +0100380void VideoProcessor::FrameDecoded(const VideoFrame& decoded_frame) {
brandtrc8c59052017-08-21 06:44:16 -0700381 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
382
brandtr32e0d262017-02-15 05:29:38 -0800383 // For the highest measurement accuracy of the decode time, the start/stop
384 // time recordings should wrap the Decode call as tightly as possible.
Rasmus Brandt5f7a8912018-02-28 17:17:15 +0100385 const int64_t decode_stop_ns = rtc::TimeNanos();
brandtr8bc93852017-02-15 05:19:51 -0800386
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100387 // Layer metadata.
Sergey Silkin10d9d592018-02-01 13:25:17 +0100388 const size_t simulcast_svc_idx =
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100389 frame_wxh_to_simulcast_svc_idx_.at(decoded_frame.size());
Sergey Silkin06a8f302018-02-20 09:48:26 +0100390 FrameStatistics* frame_stat = stats_->GetFrameWithTimestamp(
391 decoded_frame.timestamp(), simulcast_svc_idx);
Åsa Perssona6e7b882018-01-19 14:57:10 +0100392 const size_t frame_number = frame_stat->frame_number;
Sergey Silkin64eaa992017-11-17 14:47:32 +0100393
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100394 // Ensure that the decode order is monotonically increasing, within this
395 // simulcast/spatial layer.
396 RTC_CHECK(first_decoded_frame_[simulcast_svc_idx] ||
397 last_decoded_frame_num_[simulcast_svc_idx] < frame_number);
398 first_decoded_frame_[simulcast_svc_idx] = false;
399 last_decoded_frame_num_[simulcast_svc_idx] = frame_number;
brandtr17b958c2017-03-07 01:41:43 -0800400
Sergey Silkin10d9d592018-02-01 13:25:17 +0100401 // Update frame statistics.
402 frame_stat->decoding_successful = true;
403 frame_stat->decode_time_us =
404 GetElapsedTimeMicroseconds(frame_stat->decode_start_ns, decode_stop_ns);
405 frame_stat->decoded_width = decoded_frame.width();
406 frame_stat->decoded_height = decoded_frame.height();
407
Sergey Silkin64eaa992017-11-17 14:47:32 +0100408 // Skip quality metrics calculation to not affect CPU usage.
409 if (!config_.measure_cpu) {
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100410 const auto reference_frame = input_frames_.find(frame_number);
411 RTC_CHECK(reference_frame != input_frames_.cend())
412 << "The codecs are either buffering too much, dropping too much, or "
413 "being too slow relative the input frame rate.";
414 CalculateFrameQuality(reference_frame->second, decoded_frame, frame_stat);
Sergey Silkin64eaa992017-11-17 14:47:32 +0100415 }
Niels Möller718a7632016-06-13 13:06:01 +0200416
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100417 // Erase all buffered input frames that we have moved past for all
418 // simulcast/spatial layers. Never buffer more than |kMaxBufferedInputFrames|
419 // frames, to protect against long runs of consecutive frame drops for a
420 // particular layer.
421 const auto min_last_decoded_frame_num = std::min_element(
422 last_decoded_frame_num_.cbegin(), last_decoded_frame_num_.cend());
423 const size_t min_buffered_frame_num =
424 std::max(0, static_cast<int>(frame_number) - kMaxBufferedInputFrames + 1);
425 RTC_CHECK(min_last_decoded_frame_num != last_decoded_frame_num_.cend());
426 const auto input_frames_erase_before = input_frames_.lower_bound(
427 std::max(*min_last_decoded_frame_num, min_buffered_frame_num));
428 input_frames_.erase(input_frames_.cbegin(), input_frames_erase_before);
Sergey Silkin64eaa992017-11-17 14:47:32 +0100429
Sergey Silkin10d9d592018-02-01 13:25:17 +0100430 if (decoded_frame_writers_) {
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100431 ExtractI420BufferWithSize(decoded_frame, config_.codec_settings.width,
432 config_.codec_settings.height, &tmp_i420_buffer_);
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100433 RTC_CHECK_EQ(tmp_i420_buffer_.size(),
Rasmus Brandt5f7a8912018-02-28 17:17:15 +0100434 decoded_frame_writers_->at(simulcast_svc_idx)->FrameLength());
435 RTC_CHECK(decoded_frame_writers_->at(simulcast_svc_idx)
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100436 ->WriteFrame(tmp_i420_buffer_.data()));
Sergey Silkin64eaa992017-11-17 14:47:32 +0100437 }
Åsa Perssonf0c44672017-10-24 16:03:39 +0200438}
brandtr17b958c2017-03-07 01:41:43 -0800439
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100440const webrtc::EncodedImage*
441VideoProcessor::MergeAndStoreEncodedImageForSvcDecoding(
442 const EncodedImage& encoded_image,
443 const VideoCodecType codec,
444 size_t frame_number,
445 size_t simulcast_svc_idx) {
446 // Should only be called for SVC.
447 RTC_CHECK_GT(config_.NumberOfSpatialLayers(), 1);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100448
449 EncodedImage base_image;
450 RTC_CHECK_EQ(base_image._length, 0);
451
452 // Each SVC layer is decoded with dedicated decoder. Add data of base layers
453 // to current coded frame buffer.
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100454 if (simulcast_svc_idx > 0) {
455 base_image = merged_encoded_frames_.at(simulcast_svc_idx - 1);
456 RTC_CHECK_EQ(base_image._timeStamp, encoded_image._timeStamp);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100457 }
Sergey Silkin10d9d592018-02-01 13:25:17 +0100458 const size_t payload_size_bytes = base_image._length + encoded_image._length;
459 const size_t buffer_size_bytes =
460 payload_size_bytes + EncodedImage::GetBufferPaddingBytes(codec);
461
462 uint8_t* copied_buffer = new uint8_t[buffer_size_bytes];
463 RTC_CHECK(copied_buffer);
464
465 if (base_image._length) {
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100466 RTC_CHECK(base_image._buffer);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100467 memcpy(copied_buffer, base_image._buffer, base_image._length);
468 }
Sergey Silkin10d9d592018-02-01 13:25:17 +0100469 memcpy(copied_buffer + base_image._length, encoded_image._buffer,
470 encoded_image._length);
471
472 EncodedImage copied_image = encoded_image;
473 copied_image = encoded_image;
474 copied_image._buffer = copied_buffer;
475 copied_image._length = payload_size_bytes;
476 copied_image._size = buffer_size_bytes;
477
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100478 // Replace previous EncodedImage for this spatial layer.
479 uint8_t* old_buffer = merged_encoded_frames_.at(simulcast_svc_idx)._buffer;
480 if (old_buffer) {
481 delete[] old_buffer;
482 }
483 merged_encoded_frames_.at(simulcast_svc_idx) = copied_image;
484
485 return &merged_encoded_frames_.at(simulcast_svc_idx);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100486}
487
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000488} // namespace test
489} // namespace webrtc