blob: db4f6dff3ef4f8b45c2183a72bc799278d6c5796 [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 {
Åsa Persson91af24a2018-01-24 17:20:18 +010034const int kMsToRtpTimestamp = kVideoPayloadTypeFrequency / 1000;
brandtr17b958c2017-03-07 01:41:43 -080035
brandtraebc61e2017-02-28 07:13:47 -080036std::unique_ptr<VideoBitrateAllocator> CreateBitrateAllocator(
brandtr07734a52017-08-08 08:35:53 -070037 TestConfig* config) {
brandtraebc61e2017-02-28 07:13:47 -080038 std::unique_ptr<TemporalLayersFactory> tl_factory;
brandtr07734a52017-08-08 08:35:53 -070039 if (config->codec_settings.codecType == VideoCodecType::kVideoCodecVP8) {
brandtraebc61e2017-02-28 07:13:47 -080040 tl_factory.reset(new TemporalLayersFactory());
brandtr07734a52017-08-08 08:35:53 -070041 config->codec_settings.VP8()->tl_factory = tl_factory.get();
brandtraebc61e2017-02-28 07:13:47 -080042 }
43 return std::unique_ptr<VideoBitrateAllocator>(
brandtr07734a52017-08-08 08:35:53 -070044 VideoCodecInitializer::CreateBitrateAllocator(config->codec_settings,
brandtraebc61e2017-02-28 07:13:47 -080045 std::move(tl_factory)));
46}
47
Sergey Silkin3be2a552018-01-17 15:11:44 +010048size_t GetMaxNaluSizeBytes(const EncodedImage& encoded_frame,
49 const TestConfig& config) {
ssilkin612f8582017-09-28 09:23:17 -070050 if (config.codec_settings.codecType != kVideoCodecH264)
Sergey Silkin3be2a552018-01-17 15:11:44 +010051 return 0;
ssilkin612f8582017-09-28 09:23:17 -070052
53 std::vector<webrtc::H264::NaluIndex> nalu_indices =
54 webrtc::H264::FindNaluIndices(encoded_frame._buffer,
55 encoded_frame._length);
56
57 RTC_CHECK(!nalu_indices.empty());
58
Sergey Silkin3be2a552018-01-17 15:11:44 +010059 size_t max_size = 0;
ssilkin612f8582017-09-28 09:23:17 -070060 for (const webrtc::H264::NaluIndex& index : nalu_indices)
Sergey Silkin3be2a552018-01-17 15:11:44 +010061 max_size = std::max(max_size, index.payload_size);
ssilkin612f8582017-09-28 09:23:17 -070062
Sergey Silkin3be2a552018-01-17 15:11:44 +010063 return max_size;
ssilkin612f8582017-09-28 09:23:17 -070064}
65
Rasmus Brandtd062a3c2018-03-08 16:45:54 +010066void GetLayerIndices(const CodecSpecificInfo& codec_specific,
67 size_t* simulcast_svc_idx,
68 size_t* temporal_idx) {
69 if (codec_specific.codecType == kVideoCodecVP8) {
70 *simulcast_svc_idx = codec_specific.codecSpecific.VP8.simulcastIdx;
71 *temporal_idx = codec_specific.codecSpecific.VP8.temporalIdx;
72 } else if (codec_specific.codecType == kVideoCodecVP9) {
73 *simulcast_svc_idx = codec_specific.codecSpecific.VP9.spatial_idx;
74 *temporal_idx = codec_specific.codecSpecific.VP9.temporal_idx;
75 }
76 if (*simulcast_svc_idx == kNoSpatialIdx) {
77 *simulcast_svc_idx = 0;
78 }
79 if (*temporal_idx == kNoTemporalIdx) {
80 *temporal_idx = 0;
81 }
82}
83
asaperssonae9ba042017-03-07 00:25:38 -080084int GetElapsedTimeMicroseconds(int64_t start_ns, int64_t stop_ns) {
85 int64_t diff_us = (stop_ns - start_ns) / rtc::kNumNanosecsPerMicrosec;
86 RTC_DCHECK_GE(diff_us, std::numeric_limits<int>::min());
87 RTC_DCHECK_LE(diff_us, std::numeric_limits<int>::max());
88 return static_cast<int>(diff_us);
89}
90
Åsa Perssonf0c44672017-10-24 16:03:39 +020091void ExtractBufferWithSize(const VideoFrame& image,
92 int width,
93 int height,
94 rtc::Buffer* buffer) {
95 if (image.width() != width || image.height() != height) {
96 EXPECT_DOUBLE_EQ(static_cast<double>(width) / height,
97 static_cast<double>(image.width()) / image.height());
98 // Same aspect ratio, no cropping needed.
99 rtc::scoped_refptr<I420Buffer> scaled(I420Buffer::Create(width, height));
100 scaled->ScaleFrom(*image.video_frame_buffer()->ToI420());
101
102 size_t length =
103 CalcBufferSize(VideoType::kI420, scaled->width(), scaled->height());
104 buffer->SetSize(length);
105 RTC_CHECK_NE(ExtractBuffer(scaled, length, buffer->data()), -1);
106 return;
107 }
108
109 // No resize.
110 size_t length =
111 CalcBufferSize(VideoType::kI420, image.width(), image.height());
112 buffer->SetSize(length);
113 RTC_CHECK_NE(ExtractBuffer(image, length, buffer->data()), -1);
114}
115
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100116void CalculateFrameQuality(const VideoFrame& ref_frame,
117 const VideoFrame& dec_frame,
118 FrameStatistics* frame_stat) {
119 if (ref_frame.width() == dec_frame.width() ||
120 ref_frame.height() == dec_frame.height()) {
121 frame_stat->psnr = I420PSNR(&ref_frame, &dec_frame);
122 frame_stat->ssim = I420SSIM(&ref_frame, &dec_frame);
123 } else {
124 RTC_CHECK_GE(ref_frame.width(), dec_frame.width());
125 RTC_CHECK_GE(ref_frame.height(), dec_frame.height());
126 // Downscale reference frame. Use bilinear interpolation since it is used
127 // to get lowres inputs for encoder at simulcasting.
128 // TODO(ssilkin): Sync with VP9 SVC which uses 8-taps polyphase.
129 rtc::scoped_refptr<I420Buffer> scaled_buffer =
130 I420Buffer::Create(dec_frame.width(), dec_frame.height());
131 const I420BufferInterface& ref_buffer =
132 *ref_frame.video_frame_buffer()->ToI420();
133 I420Scale(ref_buffer.DataY(), ref_buffer.StrideY(), ref_buffer.DataU(),
134 ref_buffer.StrideU(), ref_buffer.DataV(), ref_buffer.StrideV(),
135 ref_buffer.width(), ref_buffer.height(),
136 scaled_buffer->MutableDataY(), scaled_buffer->StrideY(),
137 scaled_buffer->MutableDataU(), scaled_buffer->StrideU(),
138 scaled_buffer->MutableDataV(), scaled_buffer->StrideV(),
139 scaled_buffer->width(), scaled_buffer->height(),
140 libyuv::kFilterBox);
141 frame_stat->psnr =
142 I420PSNR(*scaled_buffer, *dec_frame.video_frame_buffer()->ToI420());
143 frame_stat->ssim =
144 I420SSIM(*scaled_buffer, *dec_frame.video_frame_buffer()->ToI420());
145 }
146}
147
brandtrb78bc752017-02-22 01:26:59 -0800148} // namespace
149
brandtrc4095522017-08-07 08:12:33 -0700150VideoProcessor::VideoProcessor(webrtc::VideoEncoder* encoder,
Sergey Silkin10d9d592018-02-01 13:25:17 +0100151 VideoDecoderList* decoders,
152 FrameReader* input_frame_reader,
brandtrc4095522017-08-07 08:12:33 -0700153 const TestConfig& config,
Sergey Silkin06a8f302018-02-20 09:48:26 +0100154 Stats* stats,
Sergey Silkin10d9d592018-02-01 13:25:17 +0100155 IvfFileWriterList* encoded_frame_writers,
156 FrameWriterList* decoded_frame_writers)
Åsa Perssonf0c44672017-10-24 16:03:39 +0200157 : config_(config),
Sergey Silkin10d9d592018-02-01 13:25:17 +0100158 num_simulcast_or_spatial_layers_(
159 std::max(config_.NumberOfSimulcastStreams(),
160 config_.NumberOfSpatialLayers())),
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100161 stats_(stats),
brandtr07734a52017-08-08 08:35:53 -0700162 encoder_(encoder),
Sergey Silkin10d9d592018-02-01 13:25:17 +0100163 decoders_(decoders),
brandtr07734a52017-08-08 08:35:53 -0700164 bitrate_allocator_(CreateBitrateAllocator(&config_)),
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100165 framerate_fps_(0),
brandtrbdd555c2017-08-21 01:34:04 -0700166 encode_callback_(this),
167 decode_callback_(this),
Sergey Silkin10d9d592018-02-01 13:25:17 +0100168 input_frame_reader_(input_frame_reader),
169 encoded_frame_writers_(encoded_frame_writers),
170 decoded_frame_writers_(decoded_frame_writers),
Sergey Silkin3be2a552018-01-17 15:11:44 +0100171 last_inputed_frame_num_(0),
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100172 last_inputed_timestamp_(0),
173 first_encoded_frame(true),
Sergey Silkin3be2a552018-01-17 15:11:44 +0100174 last_encoded_frame_num_(0),
Sergey Silkin10d9d592018-02-01 13:25:17 +0100175 last_encoded_simulcast_svc_idx_(0),
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100176 last_decoded_frame_num_(0) {
177 // Sanity checks.
Rasmus Brandt4b381af2018-02-07 13:56:16 +0100178 RTC_CHECK(rtc::TaskQueue::Current())
179 << "VideoProcessor must be run on a task queue.";
Sergey Silkin10d9d592018-02-01 13:25:17 +0100180 RTC_CHECK(encoder);
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100181 RTC_CHECK(decoders);
182 RTC_CHECK_EQ(decoders->size(), num_simulcast_or_spatial_layers_);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100183 RTC_CHECK(input_frame_reader);
184 RTC_CHECK(stats);
185 RTC_CHECK(!encoded_frame_writers ||
186 encoded_frame_writers->size() == num_simulcast_or_spatial_layers_);
187 RTC_CHECK(!decoded_frame_writers ||
188 decoded_frame_writers->size() == num_simulcast_or_spatial_layers_);
brandtr17b958c2017-03-07 01:41:43 -0800189
Sergey Silkin10d9d592018-02-01 13:25:17 +0100190 // Setup required callbacks for the encoder and decoder and initialize them.
brandtrbdd555c2017-08-21 01:34:04 -0700191 RTC_CHECK_EQ(encoder_->RegisterEncodeCompleteCallback(&encode_callback_),
Åsa Perssonf0c44672017-10-24 16:03:39 +0200192 WEBRTC_VIDEO_CODEC_OK);
asapersson654d54c2017-02-10 00:16:07 -0800193
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100194 // Initialize codecs so that they are ready to receive frames.
Sergey Silkin1723cf92018-01-22 15:49:55 +0100195 RTC_CHECK_EQ(encoder_->InitEncode(&config_.codec_settings,
196 static_cast<int>(config_.NumberOfCores()),
197 config_.max_payload_size_bytes),
198 WEBRTC_VIDEO_CODEC_OK);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100199
200 for (auto& decoder : *decoders_) {
201 RTC_CHECK_EQ(decoder->InitDecode(&config_.codec_settings,
202 static_cast<int>(config_.NumberOfCores())),
203 WEBRTC_VIDEO_CODEC_OK);
204 RTC_CHECK_EQ(decoder->RegisterDecodeCompleteCallback(&decode_callback_),
205 WEBRTC_VIDEO_CODEC_OK);
206 }
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000207}
208
Åsa Perssonf0c44672017-10-24 16:03:39 +0200209VideoProcessor::~VideoProcessor() {
brandtrc8c59052017-08-21 06:44:16 -0700210 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
211
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100212 // Explicitly reset codecs, in case they don't do that themselves when they
213 // go out of scope.
brandtr77920a42017-08-11 07:48:15 -0700214 RTC_CHECK_EQ(encoder_->Release(), WEBRTC_VIDEO_CODEC_OK);
brandtrbdd555c2017-08-21 01:34:04 -0700215 encoder_->RegisterEncodeCompleteCallback(nullptr);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100216 for (auto& decoder : *decoders_) {
217 RTC_CHECK_EQ(decoder->Release(), WEBRTC_VIDEO_CODEC_OK);
218 decoder->RegisterDecodeCompleteCallback(nullptr);
219 }
220
221 RTC_CHECK(last_encoded_frames_.empty());
brandtr77920a42017-08-11 07:48:15 -0700222}
223
brandtr8935d972017-09-06 01:53:22 -0700224void VideoProcessor::ProcessFrame() {
brandtrc8c59052017-08-21 06:44:16 -0700225 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
Sergey Silkin3be2a552018-01-17 15:11:44 +0100226 const size_t frame_number = last_inputed_frame_num_++;
asapersson654d54c2017-02-10 00:16:07 -0800227
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100228 // Get input frame and store for future quality calculation.
229 rtc::scoped_refptr<I420BufferInterface> buffer =
230 input_frame_reader_->ReadFrame();
brandtrbdd555c2017-08-21 01:34:04 -0700231 RTC_CHECK(buffer) << "Tried to read too many frames from the file.";
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100232 const size_t timestamp =
233 last_inputed_timestamp_ + kVideoPayloadTypeFrequency / framerate_fps_;
234 VideoFrame input_frame(buffer, static_cast<uint32_t>(timestamp),
235 static_cast<int64_t>(timestamp / kMsToRtpTimestamp),
236 webrtc::kVideoRotation_0);
237 input_frames_.emplace(frame_number, input_frame);
238 last_inputed_timestamp_ = timestamp;
brandtr17b958c2017-03-07 01:41:43 -0800239
Rasmus Brandt5f7a8912018-02-28 17:17:15 +0100240 // Create frame statistics object for all simulcast/spatial layers.
Sergey Silkin10d9d592018-02-01 13:25:17 +0100241 for (size_t simulcast_svc_idx = 0;
242 simulcast_svc_idx < num_simulcast_or_spatial_layers_;
243 ++simulcast_svc_idx) {
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100244 stats_->AddFrame(timestamp, simulcast_svc_idx);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100245 }
brandtr17b958c2017-03-07 01:41:43 -0800246
247 // For the highest measurement accuracy of the encode time, the start/stop
248 // time recordings should wrap the Encode call as tightly as possible.
Sergey Silkin10d9d592018-02-01 13:25:17 +0100249 const int64_t encode_start_ns = rtc::TimeNanos();
250 for (size_t simulcast_svc_idx = 0;
251 simulcast_svc_idx < num_simulcast_or_spatial_layers_;
252 ++simulcast_svc_idx) {
Sergey Silkin06a8f302018-02-20 09:48:26 +0100253 FrameStatistics* frame_stat =
254 stats_->GetFrame(frame_number, simulcast_svc_idx);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100255 frame_stat->encode_start_ns = encode_start_ns;
256 }
257
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100258 // Encode.
259 const std::vector<FrameType> frame_types =
260 config_.FrameTypeForFrame(frame_number);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100261 const int encode_return_code =
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100262 encoder_->Encode(input_frame, nullptr, &frame_types);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100263
264 for (size_t simulcast_svc_idx = 0;
265 simulcast_svc_idx < num_simulcast_or_spatial_layers_;
266 ++simulcast_svc_idx) {
Sergey Silkin06a8f302018-02-20 09:48:26 +0100267 FrameStatistics* frame_stat =
268 stats_->GetFrame(frame_number, simulcast_svc_idx);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100269 frame_stat->encode_return_code = encode_return_code;
270 }
271
272 // For async codecs frame decoding is done in frame encode callback.
273 if (!config_.IsAsyncCodec()) {
274 for (size_t simulcast_svc_idx = 0;
275 simulcast_svc_idx < num_simulcast_or_spatial_layers_;
276 ++simulcast_svc_idx) {
277 if (last_encoded_frames_.find(simulcast_svc_idx) !=
278 last_encoded_frames_.end()) {
279 EncodedImage& encoded_image = last_encoded_frames_[simulcast_svc_idx];
280
Sergey Silkin06a8f302018-02-20 09:48:26 +0100281 FrameStatistics* frame_stat =
282 stats_->GetFrame(frame_number, simulcast_svc_idx);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100283
284 if (encoded_frame_writers_) {
285 RTC_CHECK(encoded_frame_writers_->at(simulcast_svc_idx)
286 ->WriteFrame(encoded_image,
287 config_.codec_settings.codecType));
288 }
289
290 // For the highest measurement accuracy of the decode time, the
291 // start/stop time recordings should wrap the Decode call as tightly as
292 // possible.
293 frame_stat->decode_start_ns = rtc::TimeNanos();
294 frame_stat->decode_return_code =
295 decoders_->at(simulcast_svc_idx)
296 ->Decode(encoded_image, false, nullptr);
297
298 RTC_CHECK(encoded_image._buffer);
299 delete[] encoded_image._buffer;
300 encoded_image._buffer = nullptr;
301
302 last_encoded_frames_.erase(simulcast_svc_idx);
303 }
304 }
305 }
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000306}
307
Sergey Silkin3be2a552018-01-17 15:11:44 +0100308void VideoProcessor::SetRates(size_t bitrate_kbps, size_t framerate_fps) {
brandtrc8c59052017-08-21 06:44:16 -0700309 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100310 framerate_fps_ = static_cast<uint32_t>(framerate_fps);
Sergey Silkin3be2a552018-01-17 15:11:44 +0100311 bitrate_allocation_ = bitrate_allocator_->GetAllocation(
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100312 static_cast<uint32_t>(bitrate_kbps * 1000), framerate_fps_);
313 const int set_rates_result =
314 encoder_->SetRateAllocation(bitrate_allocation_, framerate_fps_);
brandtrbea36fd2017-08-07 03:36:54 -0700315 RTC_DCHECK_GE(set_rates_result, 0)
brandtrbdd555c2017-08-21 01:34:04 -0700316 << "Failed to update encoder with new rate " << bitrate_kbps << ".";
brandtrbea36fd2017-08-07 03:36:54 -0700317}
318
Sergey Silkin10d9d592018-02-01 13:25:17 +0100319void VideoProcessor::FrameEncoded(
320 const webrtc::EncodedImage& encoded_image,
321 const webrtc::CodecSpecificInfo& codec_specific) {
brandtrc8c59052017-08-21 06:44:16 -0700322 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
323
brandtr32e0d262017-02-15 05:29:38 -0800324 // For the highest measurement accuracy of the encode time, the start/stop
325 // time recordings should wrap the Encode call as tightly as possible.
Rasmus Brandt5f7a8912018-02-28 17:17:15 +0100326 const int64_t encode_stop_ns = rtc::TimeNanos();
brandtr32e0d262017-02-15 05:29:38 -0800327
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100328 const VideoCodecType codec_type = codec_specific.codecType;
Rasmus Brandtf7a35582017-10-24 10:16:33 +0200329 if (config_.encoded_frame_checker) {
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100330 config_.encoded_frame_checker->CheckEncodedFrame(codec_type, encoded_image);
Rasmus Brandtf7a35582017-10-24 10:16:33 +0200331 }
brandtrb78bc752017-02-22 01:26:59 -0800332
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100333 // Layer metadata.
Sergey Silkin10d9d592018-02-01 13:25:17 +0100334 size_t simulcast_svc_idx = 0;
335 size_t temporal_idx = 0;
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100336 GetLayerIndices(codec_specific, &simulcast_svc_idx, &temporal_idx);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100337 const size_t frame_wxh =
338 encoded_image._encodedWidth * encoded_image._encodedHeight;
339 frame_wxh_to_simulcast_svc_idx_[frame_wxh] = simulcast_svc_idx;
340
Sergey Silkin06a8f302018-02-20 09:48:26 +0100341 FrameStatistics* frame_stat = stats_->GetFrameWithTimestamp(
342 encoded_image._timeStamp, simulcast_svc_idx);
Åsa Perssona6e7b882018-01-19 14:57:10 +0100343 const size_t frame_number = frame_stat->frame_number;
Sergey Silkin10d9d592018-02-01 13:25:17 +0100344
345 // Reordering is unexpected. Frames of different layers have the same value
346 // of frame_number. VP8 multi-res delivers frames starting from hires layer.
347 RTC_CHECK_GE(frame_number, last_encoded_frame_num_);
348
349 // Ensure SVC spatial layers are delivered in ascending order.
350 if (config_.NumberOfSpatialLayers() > 1) {
Rasmus Brandt5f7a8912018-02-28 17:17:15 +0100351 RTC_CHECK(first_encoded_frame || frame_number >= last_encoded_frame_num_ ||
352 simulcast_svc_idx > last_encoded_simulcast_svc_idx_);
Sergey Silkin3be2a552018-01-17 15:11:44 +0100353 }
Rasmus Brandt5f7a8912018-02-28 17:17:15 +0100354 first_encoded_frame = false;
brandtr17b958c2017-03-07 01:41:43 -0800355 last_encoded_frame_num_ = frame_number;
Sergey Silkin10d9d592018-02-01 13:25:17 +0100356 last_encoded_simulcast_svc_idx_ = simulcast_svc_idx;
brandtr17b958c2017-03-07 01:41:43 -0800357
brandtr8935d972017-09-06 01:53:22 -0700358 // Update frame statistics.
Sergey Silkin10d9d592018-02-01 13:25:17 +0100359 frame_stat->encoding_successful = true;
brandtr8935d972017-09-06 01:53:22 -0700360 frame_stat->encode_time_us =
361 GetElapsedTimeMicroseconds(frame_stat->encode_start_ns, encode_stop_ns);
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100362 if (codec_type == kVideoCodecVP9) {
Sergey Silkin06a8f302018-02-20 09:48:26 +0100363 const CodecSpecificInfoVP9& vp9_info = codec_specific.codecSpecific.VP9;
364 frame_stat->inter_layer_predicted = vp9_info.inter_layer_predicted;
365
366 // TODO(ssilkin): Implement bitrate allocation for VP9 SVC. For now set
367 // target for base layers equal to total target to avoid devision by zero
368 // at analysis.
369 frame_stat->target_bitrate_kbps = bitrate_allocation_.get_sum_kbps();
370 } else {
371 frame_stat->target_bitrate_kbps =
372 (bitrate_allocation_.GetBitrate(simulcast_svc_idx, temporal_idx) +
373 500) /
374 1000;
375 }
376
Sergey Silkind4bc01b2018-03-09 14:31:24 +0100377 frame_stat->length_bytes = encoded_image._length;
brandtr17b958c2017-03-07 01:41:43 -0800378 frame_stat->frame_type = encoded_image._frameType;
Sergey Silkin10d9d592018-02-01 13:25:17 +0100379 frame_stat->temporal_layer_idx = temporal_idx;
380 frame_stat->simulcast_svc_idx = simulcast_svc_idx;
Sergey Silkin3be2a552018-01-17 15:11:44 +0100381 frame_stat->max_nalu_size_bytes = GetMaxNaluSizeBytes(encoded_image, config_);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100382 frame_stat->qp = encoded_image.qp_;
ssilkin612f8582017-09-28 09:23:17 -0700383
Sergey Silkin10d9d592018-02-01 13:25:17 +0100384 if (!config_.IsAsyncCodec()) {
385 // Store encoded frame. It will be decoded after all layers are encoded.
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100386 CopyEncodedImage(encoded_image, codec_type, frame_number,
387 simulcast_svc_idx);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100388 } else {
389 const size_t simulcast_idx =
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100390 codec_type == kVideoCodecVP8
391 ? codec_specific.codecSpecific.VP8.simulcastIdx
392 : 0;
Sergey Silkin10d9d592018-02-01 13:25:17 +0100393 frame_stat->decode_start_ns = rtc::TimeNanos();
394 frame_stat->decode_return_code =
395 decoders_->at(simulcast_idx)->Decode(encoded_image, false, nullptr);
brandtr8935d972017-09-06 01:53:22 -0700396 }
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000397}
398
Sergey Silkin64eaa992017-11-17 14:47:32 +0100399void VideoProcessor::FrameDecoded(const VideoFrame& decoded_frame) {
brandtrc8c59052017-08-21 06:44:16 -0700400 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
401
brandtr32e0d262017-02-15 05:29:38 -0800402 // For the highest measurement accuracy of the decode time, the start/stop
403 // time recordings should wrap the Decode call as tightly as possible.
Rasmus Brandt5f7a8912018-02-28 17:17:15 +0100404 const int64_t decode_stop_ns = rtc::TimeNanos();
brandtr8bc93852017-02-15 05:19:51 -0800405
Sergey Silkin10d9d592018-02-01 13:25:17 +0100406 RTC_CHECK(frame_wxh_to_simulcast_svc_idx_.find(decoded_frame.size()) !=
407 frame_wxh_to_simulcast_svc_idx_.end());
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100408 // Layer metadata.
Sergey Silkin10d9d592018-02-01 13:25:17 +0100409 const size_t simulcast_svc_idx =
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100410 frame_wxh_to_simulcast_svc_idx_.at(decoded_frame.size());
Sergey Silkin06a8f302018-02-20 09:48:26 +0100411 FrameStatistics* frame_stat = stats_->GetFrameWithTimestamp(
412 decoded_frame.timestamp(), simulcast_svc_idx);
Åsa Perssona6e7b882018-01-19 14:57:10 +0100413 const size_t frame_number = frame_stat->frame_number;
Sergey Silkin64eaa992017-11-17 14:47:32 +0100414
Sergey Silkin10d9d592018-02-01 13:25:17 +0100415 // Reordering is unexpected. Frames of different layers have the same value
416 // of frame_number.
417 RTC_CHECK_GE(frame_number, last_decoded_frame_num_);
brandtr17b958c2017-03-07 01:41:43 -0800418 last_decoded_frame_num_ = frame_number;
419
Sergey Silkin10d9d592018-02-01 13:25:17 +0100420 // Update frame statistics.
421 frame_stat->decoding_successful = true;
422 frame_stat->decode_time_us =
423 GetElapsedTimeMicroseconds(frame_stat->decode_start_ns, decode_stop_ns);
424 frame_stat->decoded_width = decoded_frame.width();
425 frame_stat->decoded_height = decoded_frame.height();
426
Sergey Silkin64eaa992017-11-17 14:47:32 +0100427 // Skip quality metrics calculation to not affect CPU usage.
428 if (!config_.measure_cpu) {
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100429 CalculateFrameQuality(input_frames_.at(frame_number), decoded_frame,
Sergey Silkin10d9d592018-02-01 13:25:17 +0100430 frame_stat);
Sergey Silkin64eaa992017-11-17 14:47:32 +0100431 }
Niels Möller718a7632016-06-13 13:06:01 +0200432
Sergey Silkin64eaa992017-11-17 14:47:32 +0100433 // Delay erasing of input frames by one frame. The current frame might
434 // still be needed for other simulcast stream or spatial layer.
Sergey Silkin3be2a552018-01-17 15:11:44 +0100435 if (frame_number > 0) {
436 auto input_frame_erase_to = input_frames_.lower_bound(frame_number - 1);
Sergey Silkin64eaa992017-11-17 14:47:32 +0100437 input_frames_.erase(input_frames_.begin(), input_frame_erase_to);
438 }
439
Sergey Silkin10d9d592018-02-01 13:25:17 +0100440 if (decoded_frame_writers_) {
Sergey Silkin64eaa992017-11-17 14:47:32 +0100441 ExtractBufferWithSize(decoded_frame, config_.codec_settings.width,
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100442 config_.codec_settings.height, &tmp_i420_buffer_);
Rasmus Brandt5f7a8912018-02-28 17:17:15 +0100443 RTC_CHECK(simulcast_svc_idx < decoded_frame_writers_->size());
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100444 RTC_CHECK_EQ(tmp_i420_buffer_.size(),
Rasmus Brandt5f7a8912018-02-28 17:17:15 +0100445 decoded_frame_writers_->at(simulcast_svc_idx)->FrameLength());
446 RTC_CHECK(decoded_frame_writers_->at(simulcast_svc_idx)
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100447 ->WriteFrame(tmp_i420_buffer_.data()));
Sergey Silkin64eaa992017-11-17 14:47:32 +0100448 }
Åsa Perssonf0c44672017-10-24 16:03:39 +0200449}
brandtr17b958c2017-03-07 01:41:43 -0800450
Sergey Silkin10d9d592018-02-01 13:25:17 +0100451void VideoProcessor::CopyEncodedImage(const EncodedImage& encoded_image,
452 const VideoCodecType codec,
453 size_t frame_number,
454 size_t simulcast_svc_idx) {
455 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
456
457 EncodedImage base_image;
458 RTC_CHECK_EQ(base_image._length, 0);
459
460 // Each SVC layer is decoded with dedicated decoder. Add data of base layers
461 // to current coded frame buffer.
462 if (config_.NumberOfSpatialLayers() > 1 && simulcast_svc_idx > 0) {
463 RTC_CHECK(last_encoded_frames_.find(simulcast_svc_idx - 1) !=
464 last_encoded_frames_.end());
465 base_image = last_encoded_frames_[simulcast_svc_idx - 1];
466 }
467
468 const size_t payload_size_bytes = base_image._length + encoded_image._length;
469 const size_t buffer_size_bytes =
470 payload_size_bytes + EncodedImage::GetBufferPaddingBytes(codec);
471
472 uint8_t* copied_buffer = new uint8_t[buffer_size_bytes];
473 RTC_CHECK(copied_buffer);
474
475 if (base_image._length) {
476 memcpy(copied_buffer, base_image._buffer, base_image._length);
477 }
478
479 memcpy(copied_buffer + base_image._length, encoded_image._buffer,
480 encoded_image._length);
481
482 EncodedImage copied_image = encoded_image;
483 copied_image = encoded_image;
484 copied_image._buffer = copied_buffer;
485 copied_image._length = payload_size_bytes;
486 copied_image._size = buffer_size_bytes;
487
488 last_encoded_frames_[simulcast_svc_idx] = copied_image;
489}
490
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000491} // namespace test
492} // namespace webrtc