blob: 24a0f016efcfe4d01069b26e88087278db0c06cb [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/include/video_codec_initializer.h"
Rasmus Brandtd00c8952018-03-14 12:29:57 +010023#include "modules/video_coding/include/video_error_codes.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/video_coding/utility/default_video_bitrate_allocator.h"
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +020025#include "modules/video_coding/utility/simulcast_rate_allocator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#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
Kári Tristan Helgason169005d2018-05-22 13:34:14 +020035using FrameStatistics = VideoCodecTestStats::FrameStatistics;
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +010036
Kári Tristan Helgason169005d2018-05-22 13:34:14 +020037namespace {
Åsa Persson91af24a2018-01-24 17:20:18 +010038const int kMsToRtpTimestamp = kVideoPayloadTypeFrequency / 1000;
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +010039const int kMaxBufferedInputFrames = 10;
brandtr17b958c2017-03-07 01:41:43 -080040
Sergey Silkin3be2a552018-01-17 15:11:44 +010041size_t GetMaxNaluSizeBytes(const EncodedImage& encoded_frame,
Kári Tristan Helgason169005d2018-05-22 13:34:14 +020042 const VideoCodecTestFixture::Config& config) {
ssilkin612f8582017-09-28 09:23:17 -070043 if (config.codec_settings.codecType != kVideoCodecH264)
Sergey Silkin3be2a552018-01-17 15:11:44 +010044 return 0;
ssilkin612f8582017-09-28 09:23:17 -070045
46 std::vector<webrtc::H264::NaluIndex> nalu_indices =
47 webrtc::H264::FindNaluIndices(encoded_frame._buffer,
48 encoded_frame._length);
49
50 RTC_CHECK(!nalu_indices.empty());
51
Sergey Silkin3be2a552018-01-17 15:11:44 +010052 size_t max_size = 0;
ssilkin612f8582017-09-28 09:23:17 -070053 for (const webrtc::H264::NaluIndex& index : nalu_indices)
Sergey Silkin3be2a552018-01-17 15:11:44 +010054 max_size = std::max(max_size, index.payload_size);
ssilkin612f8582017-09-28 09:23:17 -070055
Sergey Silkin3be2a552018-01-17 15:11:44 +010056 return max_size;
ssilkin612f8582017-09-28 09:23:17 -070057}
58
Rasmus Brandtd062a3c2018-03-08 16:45:54 +010059void GetLayerIndices(const CodecSpecificInfo& codec_specific,
Sergey Silkinbc20fe12018-04-15 21:06:16 +020060 size_t* spatial_idx,
Rasmus Brandtd062a3c2018-03-08 16:45:54 +010061 size_t* temporal_idx) {
62 if (codec_specific.codecType == kVideoCodecVP8) {
Sergey Silkinbc20fe12018-04-15 21:06:16 +020063 *spatial_idx = codec_specific.codecSpecific.VP8.simulcastIdx;
Rasmus Brandtd062a3c2018-03-08 16:45:54 +010064 *temporal_idx = codec_specific.codecSpecific.VP8.temporalIdx;
65 } else if (codec_specific.codecType == kVideoCodecVP9) {
Sergey Silkinbc20fe12018-04-15 21:06:16 +020066 *spatial_idx = codec_specific.codecSpecific.VP9.spatial_idx;
Rasmus Brandtd062a3c2018-03-08 16:45:54 +010067 *temporal_idx = codec_specific.codecSpecific.VP9.temporal_idx;
68 }
Sergey Silkinbc20fe12018-04-15 21:06:16 +020069 if (*spatial_idx == kNoSpatialIdx) {
70 *spatial_idx = 0;
Rasmus Brandtd062a3c2018-03-08 16:45:54 +010071 }
72 if (*temporal_idx == kNoTemporalIdx) {
73 *temporal_idx = 0;
74 }
75}
76
asaperssonae9ba042017-03-07 00:25:38 -080077int GetElapsedTimeMicroseconds(int64_t start_ns, int64_t stop_ns) {
78 int64_t diff_us = (stop_ns - start_ns) / rtc::kNumNanosecsPerMicrosec;
79 RTC_DCHECK_GE(diff_us, std::numeric_limits<int>::min());
80 RTC_DCHECK_LE(diff_us, std::numeric_limits<int>::max());
81 return static_cast<int>(diff_us);
82}
83
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +010084void ExtractI420BufferWithSize(const VideoFrame& image,
85 int width,
86 int height,
87 rtc::Buffer* buffer) {
Åsa Perssonf0c44672017-10-24 16:03:39 +020088 if (image.width() != width || image.height() != height) {
89 EXPECT_DOUBLE_EQ(static_cast<double>(width) / height,
90 static_cast<double>(image.width()) / image.height());
91 // Same aspect ratio, no cropping needed.
92 rtc::scoped_refptr<I420Buffer> scaled(I420Buffer::Create(width, height));
93 scaled->ScaleFrom(*image.video_frame_buffer()->ToI420());
94
95 size_t length =
96 CalcBufferSize(VideoType::kI420, scaled->width(), scaled->height());
97 buffer->SetSize(length);
98 RTC_CHECK_NE(ExtractBuffer(scaled, length, buffer->data()), -1);
99 return;
100 }
101
102 // No resize.
103 size_t length =
104 CalcBufferSize(VideoType::kI420, image.width(), image.height());
105 buffer->SetSize(length);
106 RTC_CHECK_NE(ExtractBuffer(image, length, buffer->data()), -1);
107}
108
Sergey Silkin8d3758e2018-03-14 11:28:15 +0100109void CalculateFrameQuality(const I420BufferInterface& ref_buffer,
110 const I420BufferInterface& dec_buffer,
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100111 FrameStatistics* frame_stat) {
Sergey Silkin8d3758e2018-03-14 11:28:15 +0100112 if (ref_buffer.width() != dec_buffer.width() ||
113 ref_buffer.height() != dec_buffer.height()) {
114 RTC_CHECK_GE(ref_buffer.width(), dec_buffer.width());
115 RTC_CHECK_GE(ref_buffer.height(), dec_buffer.height());
116 // Downscale reference frame.
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100117 rtc::scoped_refptr<I420Buffer> scaled_buffer =
Sergey Silkin8d3758e2018-03-14 11:28:15 +0100118 I420Buffer::Create(dec_buffer.width(), dec_buffer.height());
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100119 I420Scale(ref_buffer.DataY(), ref_buffer.StrideY(), ref_buffer.DataU(),
120 ref_buffer.StrideU(), ref_buffer.DataV(), ref_buffer.StrideV(),
121 ref_buffer.width(), ref_buffer.height(),
122 scaled_buffer->MutableDataY(), scaled_buffer->StrideY(),
123 scaled_buffer->MutableDataU(), scaled_buffer->StrideU(),
124 scaled_buffer->MutableDataV(), scaled_buffer->StrideV(),
125 scaled_buffer->width(), scaled_buffer->height(),
126 libyuv::kFilterBox);
Sergey Silkin8d3758e2018-03-14 11:28:15 +0100127
128 CalculateFrameQuality(*scaled_buffer, dec_buffer, frame_stat);
129 } else {
130 const uint64_t sse_y = libyuv::ComputeSumSquareErrorPlane(
131 dec_buffer.DataY(), dec_buffer.StrideY(), ref_buffer.DataY(),
132 ref_buffer.StrideY(), dec_buffer.width(), dec_buffer.height());
133
134 const uint64_t sse_u = libyuv::ComputeSumSquareErrorPlane(
135 dec_buffer.DataU(), dec_buffer.StrideU(), ref_buffer.DataU(),
136 ref_buffer.StrideU(), dec_buffer.width() / 2, dec_buffer.height() / 2);
137
138 const uint64_t sse_v = libyuv::ComputeSumSquareErrorPlane(
139 dec_buffer.DataV(), dec_buffer.StrideV(), ref_buffer.DataV(),
140 ref_buffer.StrideV(), dec_buffer.width() / 2, dec_buffer.height() / 2);
141
142 const size_t num_y_samples = dec_buffer.width() * dec_buffer.height();
143 const size_t num_u_samples =
144 dec_buffer.width() / 2 * dec_buffer.height() / 2;
145
146 frame_stat->psnr_y = libyuv::SumSquareErrorToPsnr(sse_y, num_y_samples);
147 frame_stat->psnr_u = libyuv::SumSquareErrorToPsnr(sse_u, num_u_samples);
148 frame_stat->psnr_v = libyuv::SumSquareErrorToPsnr(sse_v, num_u_samples);
149 frame_stat->psnr = libyuv::SumSquareErrorToPsnr(
150 sse_y + sse_u + sse_v, num_y_samples + 2 * num_u_samples);
151 frame_stat->ssim = I420SSIM(ref_buffer, dec_buffer);
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100152 }
153}
154
Kári Tristan Helgason169005d2018-05-22 13:34:14 +0200155std::vector<FrameType> FrameTypeForFrame(
156 const VideoCodecTestFixture::Config& config,
157 size_t frame_idx) {
158 if (config.keyframe_interval > 0 &&
159 (frame_idx % config.keyframe_interval == 0)) {
160 return {kVideoFrameKey};
161 }
162 return {kVideoFrameDelta};
163}
164
brandtrb78bc752017-02-22 01:26:59 -0800165} // namespace
166
brandtrc4095522017-08-07 08:12:33 -0700167VideoProcessor::VideoProcessor(webrtc::VideoEncoder* encoder,
Sergey Silkin10d9d592018-02-01 13:25:17 +0100168 VideoDecoderList* decoders,
169 FrameReader* input_frame_reader,
Kári Tristan Helgason169005d2018-05-22 13:34:14 +0200170 const VideoCodecTestFixture::Config& config,
171 VideoCodecTestStats* stats,
Sergey Silkin10d9d592018-02-01 13:25:17 +0100172 IvfFileWriterList* encoded_frame_writers,
173 FrameWriterList* decoded_frame_writers)
Åsa Perssonf0c44672017-10-24 16:03:39 +0200174 : config_(config),
Sergey Silkin10d9d592018-02-01 13:25:17 +0100175 num_simulcast_or_spatial_layers_(
176 std::max(config_.NumberOfSimulcastStreams(),
177 config_.NumberOfSpatialLayers())),
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100178 stats_(stats),
brandtr07734a52017-08-08 08:35:53 -0700179 encoder_(encoder),
Sergey Silkin10d9d592018-02-01 13:25:17 +0100180 decoders_(decoders),
Erik Språng82fad3d2018-03-21 09:57:23 +0100181 bitrate_allocator_(VideoCodecInitializer::CreateBitrateAllocator(
182 config_.codec_settings)),
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100183 framerate_fps_(0),
brandtrbdd555c2017-08-21 01:34:04 -0700184 encode_callback_(this),
Sergey Silkin10d9d592018-02-01 13:25:17 +0100185 input_frame_reader_(input_frame_reader),
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100186 merged_encoded_frames_(num_simulcast_or_spatial_layers_),
Sergey Silkin10d9d592018-02-01 13:25:17 +0100187 encoded_frame_writers_(encoded_frame_writers),
188 decoded_frame_writers_(decoded_frame_writers),
Sergey Silkin3be2a552018-01-17 15:11:44 +0100189 last_inputed_frame_num_(0),
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100190 last_inputed_timestamp_(0),
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100191 first_encoded_frame_(num_simulcast_or_spatial_layers_, true),
192 last_encoded_frame_num_(num_simulcast_or_spatial_layers_),
193 first_decoded_frame_(num_simulcast_or_spatial_layers_, true),
Sergey Silkinc89eed92018-04-01 23:57:51 +0200194 last_decoded_frame_num_(num_simulcast_or_spatial_layers_),
Sergey Silkin56138792018-05-02 10:50:55 +0200195 decoded_frame_buffer_(num_simulcast_or_spatial_layers_),
Sergey Silkinc89eed92018-04-01 23:57:51 +0200196 post_encode_time_ns_(0) {
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100197 // Sanity checks.
Rasmus Brandt4b381af2018-02-07 13:56:16 +0100198 RTC_CHECK(rtc::TaskQueue::Current())
199 << "VideoProcessor must be run on a task queue.";
Sergey Silkin10d9d592018-02-01 13:25:17 +0100200 RTC_CHECK(encoder);
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100201 RTC_CHECK(decoders);
202 RTC_CHECK_EQ(decoders->size(), num_simulcast_or_spatial_layers_);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100203 RTC_CHECK(input_frame_reader);
204 RTC_CHECK(stats);
205 RTC_CHECK(!encoded_frame_writers ||
206 encoded_frame_writers->size() == num_simulcast_or_spatial_layers_);
207 RTC_CHECK(!decoded_frame_writers ||
208 decoded_frame_writers->size() == num_simulcast_or_spatial_layers_);
brandtr17b958c2017-03-07 01:41:43 -0800209
Sergey Silkin10d9d592018-02-01 13:25:17 +0100210 // Setup required callbacks for the encoder and decoder and initialize them.
brandtrbdd555c2017-08-21 01:34:04 -0700211 RTC_CHECK_EQ(encoder_->RegisterEncodeCompleteCallback(&encode_callback_),
Åsa Perssonf0c44672017-10-24 16:03:39 +0200212 WEBRTC_VIDEO_CODEC_OK);
asapersson654d54c2017-02-10 00:16:07 -0800213
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100214 // Initialize codecs so that they are ready to receive frames.
Sergey Silkin1723cf92018-01-22 15:49:55 +0100215 RTC_CHECK_EQ(encoder_->InitEncode(&config_.codec_settings,
216 static_cast<int>(config_.NumberOfCores()),
217 config_.max_payload_size_bytes),
218 WEBRTC_VIDEO_CODEC_OK);
Sergey Silkin645e2e02018-04-06 09:42:13 +0200219
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200220 for (size_t i = 0; i < num_simulcast_or_spatial_layers_; ++i) {
Sergey Silkin645e2e02018-04-06 09:42:13 +0200221 decode_callback_.push_back(
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200222 rtc::MakeUnique<VideoProcessorDecodeCompleteCallback>(this, i));
223 RTC_CHECK_EQ(
224 decoders_->at(i)->InitDecode(&config_.codec_settings,
225 static_cast<int>(config_.NumberOfCores())),
226 WEBRTC_VIDEO_CODEC_OK);
227 RTC_CHECK_EQ(decoders_->at(i)->RegisterDecodeCompleteCallback(
228 decode_callback_.at(i).get()),
Sergey Silkin10d9d592018-02-01 13:25:17 +0100229 WEBRTC_VIDEO_CODEC_OK);
230 }
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000231}
232
Åsa Perssonf0c44672017-10-24 16:03:39 +0200233VideoProcessor::~VideoProcessor() {
brandtrc8c59052017-08-21 06:44:16 -0700234 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
235
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100236 // Explicitly reset codecs, in case they don't do that themselves when they
237 // go out of scope.
brandtr77920a42017-08-11 07:48:15 -0700238 RTC_CHECK_EQ(encoder_->Release(), WEBRTC_VIDEO_CODEC_OK);
brandtrbdd555c2017-08-21 01:34:04 -0700239 encoder_->RegisterEncodeCompleteCallback(nullptr);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100240 for (auto& decoder : *decoders_) {
241 RTC_CHECK_EQ(decoder->Release(), WEBRTC_VIDEO_CODEC_OK);
242 decoder->RegisterDecodeCompleteCallback(nullptr);
243 }
244
Rasmus Brandtd00c8952018-03-14 12:29:57 +0100245 // Sanity check.
246 RTC_CHECK_LE(input_frames_.size(), kMaxBufferedInputFrames);
247
248 // Deal with manual memory management of EncodedImage's.
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200249 for (size_t i = 0; i < num_simulcast_or_spatial_layers_; ++i) {
250 uint8_t* buffer = merged_encoded_frames_.at(i)._buffer;
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100251 if (buffer) {
252 delete[] buffer;
253 }
254 }
brandtr77920a42017-08-11 07:48:15 -0700255}
256
brandtr8935d972017-09-06 01:53:22 -0700257void VideoProcessor::ProcessFrame() {
brandtrc8c59052017-08-21 06:44:16 -0700258 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
Sergey Silkin3be2a552018-01-17 15:11:44 +0100259 const size_t frame_number = last_inputed_frame_num_++;
asapersson654d54c2017-02-10 00:16:07 -0800260
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100261 // Get input frame and store for future quality calculation.
262 rtc::scoped_refptr<I420BufferInterface> buffer =
263 input_frame_reader_->ReadFrame();
brandtrbdd555c2017-08-21 01:34:04 -0700264 RTC_CHECK(buffer) << "Tried to read too many frames from the file.";
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100265 const size_t timestamp =
266 last_inputed_timestamp_ + kVideoPayloadTypeFrequency / framerate_fps_;
267 VideoFrame input_frame(buffer, static_cast<uint32_t>(timestamp),
268 static_cast<int64_t>(timestamp / kMsToRtpTimestamp),
269 webrtc::kVideoRotation_0);
Rasmus Brandtd00c8952018-03-14 12:29:57 +0100270 // Store input frame as a reference for quality calculations.
271 if (config_.decode && !config_.measure_cpu) {
Rasmus Brandt7b92ceb2018-06-26 12:34:59 +0200272 if (input_frames_.size() == kMaxBufferedInputFrames) {
273 input_frames_.erase(input_frames_.begin());
274 }
Rasmus Brandtd00c8952018-03-14 12:29:57 +0100275 input_frames_.emplace(frame_number, input_frame);
276 }
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100277 last_inputed_timestamp_ = timestamp;
brandtr17b958c2017-03-07 01:41:43 -0800278
Sergey Silkinc89eed92018-04-01 23:57:51 +0200279 post_encode_time_ns_ = 0;
280
Rasmus Brandt5f7a8912018-02-28 17:17:15 +0100281 // Create frame statistics object for all simulcast/spatial layers.
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200282 for (size_t i = 0; i < num_simulcast_or_spatial_layers_; ++i) {
283 stats_->AddFrame(timestamp, i);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100284 }
brandtr17b958c2017-03-07 01:41:43 -0800285
286 // For the highest measurement accuracy of the encode time, the start/stop
287 // time recordings should wrap the Encode call as tightly as possible.
Sergey Silkin10d9d592018-02-01 13:25:17 +0100288 const int64_t encode_start_ns = rtc::TimeNanos();
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200289 for (size_t i = 0; i < num_simulcast_or_spatial_layers_; ++i) {
290 FrameStatistics* frame_stat = stats_->GetFrame(frame_number, i);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100291 frame_stat->encode_start_ns = encode_start_ns;
292 }
293
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100294 // Encode.
295 const std::vector<FrameType> frame_types =
Kári Tristan Helgason169005d2018-05-22 13:34:14 +0200296 FrameTypeForFrame(config_, frame_number);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100297 const int encode_return_code =
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100298 encoder_->Encode(input_frame, nullptr, &frame_types);
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200299 for (size_t i = 0; i < num_simulcast_or_spatial_layers_; ++i) {
300 FrameStatistics* frame_stat = stats_->GetFrame(frame_number, i);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100301 frame_stat->encode_return_code = encode_return_code;
302 }
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000303}
304
Sergey Silkin3be2a552018-01-17 15:11:44 +0100305void VideoProcessor::SetRates(size_t bitrate_kbps, size_t framerate_fps) {
brandtrc8c59052017-08-21 06:44:16 -0700306 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100307 framerate_fps_ = static_cast<uint32_t>(framerate_fps);
Sergey Silkin3be2a552018-01-17 15:11:44 +0100308 bitrate_allocation_ = bitrate_allocator_->GetAllocation(
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100309 static_cast<uint32_t>(bitrate_kbps * 1000), framerate_fps_);
310 const int set_rates_result =
311 encoder_->SetRateAllocation(bitrate_allocation_, framerate_fps_);
brandtrbea36fd2017-08-07 03:36:54 -0700312 RTC_DCHECK_GE(set_rates_result, 0)
brandtrbdd555c2017-08-21 01:34:04 -0700313 << "Failed to update encoder with new rate " << bitrate_kbps << ".";
brandtrbea36fd2017-08-07 03:36:54 -0700314}
315
Sami Kalliomäki451b29c2018-07-04 14:33:51 +0200316int32_t VideoProcessor::VideoProcessorDecodeCompleteCallback::Decoded(
317 VideoFrame& image) {
318 // Post the callback to the right task queue, if needed.
319 if (!task_queue_->IsCurrent()) {
320 // There might be a limited amount of output buffers, make a copy to make
321 // sure we don't block the decoder.
322 VideoFrame copy(I420Buffer::Copy(*image.video_frame_buffer()->ToI420()),
323 image.rotation(), image.timestamp_us());
324 copy.set_timestamp(image.timestamp());
325
326 task_queue_->PostTask([this, copy]() {
327 video_processor_->FrameDecoded(copy, simulcast_svc_idx_);
328 });
329 return 0;
330 }
331 video_processor_->FrameDecoded(image, simulcast_svc_idx_);
332 return 0;
333}
334
Sergey Silkin10d9d592018-02-01 13:25:17 +0100335void VideoProcessor::FrameEncoded(
336 const webrtc::EncodedImage& encoded_image,
337 const webrtc::CodecSpecificInfo& codec_specific) {
brandtrc8c59052017-08-21 06:44:16 -0700338 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
339
brandtr32e0d262017-02-15 05:29:38 -0800340 // For the highest measurement accuracy of the encode time, the start/stop
341 // time recordings should wrap the Encode call as tightly as possible.
Rasmus Brandt5f7a8912018-02-28 17:17:15 +0100342 const int64_t encode_stop_ns = rtc::TimeNanos();
brandtr32e0d262017-02-15 05:29:38 -0800343
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100344 const VideoCodecType codec_type = codec_specific.codecType;
Rasmus Brandtf7a35582017-10-24 10:16:33 +0200345 if (config_.encoded_frame_checker) {
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100346 config_.encoded_frame_checker->CheckEncodedFrame(codec_type, encoded_image);
Rasmus Brandtf7a35582017-10-24 10:16:33 +0200347 }
brandtrb78bc752017-02-22 01:26:59 -0800348
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100349 // Layer metadata.
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200350 size_t spatial_idx = 0;
Sergey Silkin10d9d592018-02-01 13:25:17 +0100351 size_t temporal_idx = 0;
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200352 GetLayerIndices(codec_specific, &spatial_idx, &temporal_idx);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100353
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200354 FrameStatistics* frame_stat =
355 stats_->GetFrameWithTimestamp(encoded_image._timeStamp, spatial_idx);
Åsa Perssona6e7b882018-01-19 14:57:10 +0100356 const size_t frame_number = frame_stat->frame_number;
Sergey Silkin10d9d592018-02-01 13:25:17 +0100357
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100358 // Ensure that the encode order is monotonically increasing, within this
359 // simulcast/spatial layer.
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200360 RTC_CHECK(first_encoded_frame_[spatial_idx] ||
361 last_encoded_frame_num_[spatial_idx] < frame_number);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100362
363 // Ensure SVC spatial layers are delivered in ascending order.
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200364 if (!first_encoded_frame_[spatial_idx] &&
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100365 config_.NumberOfSpatialLayers() > 1) {
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200366 for (size_t i = 0; i < spatial_idx; ++i) {
Sergey Silkin122ba6c2018-03-27 14:32:21 +0200367 RTC_CHECK_LE(last_encoded_frame_num_[i], frame_number);
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100368 }
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200369 for (size_t i = spatial_idx + 1; i < num_simulcast_or_spatial_layers_;
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100370 ++i) {
371 RTC_CHECK_GT(frame_number, last_encoded_frame_num_[i]);
372 }
Sergey Silkin3be2a552018-01-17 15:11:44 +0100373 }
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200374 first_encoded_frame_[spatial_idx] = false;
375 last_encoded_frame_num_[spatial_idx] = frame_number;
brandtr17b958c2017-03-07 01:41:43 -0800376
brandtr8935d972017-09-06 01:53:22 -0700377 // Update frame statistics.
Sergey Silkin10d9d592018-02-01 13:25:17 +0100378 frame_stat->encoding_successful = true;
Sergey Silkinc89eed92018-04-01 23:57:51 +0200379 frame_stat->encode_time_us = GetElapsedTimeMicroseconds(
380 frame_stat->encode_start_ns, encode_stop_ns - post_encode_time_ns_);
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200381 frame_stat->target_bitrate_kbps =
Sergey Silkinbfd54ef2018-04-13 23:41:11 +0200382 bitrate_allocation_.GetTemporalLayerSum(spatial_idx, temporal_idx) / 1000;
Sergey Silkind4bc01b2018-03-09 14:31:24 +0100383 frame_stat->length_bytes = encoded_image._length;
brandtr17b958c2017-03-07 01:41:43 -0800384 frame_stat->frame_type = encoded_image._frameType;
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200385 frame_stat->temporal_idx = temporal_idx;
386 frame_stat->spatial_idx = spatial_idx;
Sergey Silkin3be2a552018-01-17 15:11:44 +0100387 frame_stat->max_nalu_size_bytes = GetMaxNaluSizeBytes(encoded_image, config_);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100388 frame_stat->qp = encoded_image.qp_;
ssilkin612f8582017-09-28 09:23:17 -0700389
Sergey Silkin645e2e02018-04-06 09:42:13 +0200390 const size_t num_spatial_layers = config_.NumberOfSpatialLayers();
Sergey Silkinbc0f0d32018-04-24 21:29:14 +0200391 bool end_of_picture = false;
Sergey Silkin645e2e02018-04-06 09:42:13 +0200392 if (codec_type == kVideoCodecVP9) {
393 const CodecSpecificInfoVP9& vp9_info = codec_specific.codecSpecific.VP9;
394 frame_stat->inter_layer_predicted = vp9_info.inter_layer_predicted;
Sergey Silkin3c30c9c2018-05-02 09:18:48 +0200395 frame_stat->non_ref_for_inter_layer_pred =
396 vp9_info.non_ref_for_inter_layer_pred;
Sergey Silkinbc0f0d32018-04-24 21:29:14 +0200397 end_of_picture = vp9_info.end_of_picture;
Sergey Silkin3c30c9c2018-05-02 09:18:48 +0200398 } else {
399 frame_stat->inter_layer_predicted = false;
400 frame_stat->non_ref_for_inter_layer_pred = true;
Sergey Silkin645e2e02018-04-06 09:42:13 +0200401 }
402
Sergey Silkin122ba6c2018-03-27 14:32:21 +0200403 const webrtc::EncodedImage* encoded_image_for_decode = &encoded_image;
Sergey Silkin645e2e02018-04-06 09:42:13 +0200404 if (config_.decode || encoded_frame_writers_) {
405 if (num_spatial_layers > 1) {
406 encoded_image_for_decode = BuildAndStoreSuperframe(
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200407 encoded_image, codec_type, frame_number, spatial_idx,
Sergey Silkin645e2e02018-04-06 09:42:13 +0200408 frame_stat->inter_layer_predicted);
Rasmus Brandtd00c8952018-03-14 12:29:57 +0100409 }
Sergey Silkin645e2e02018-04-06 09:42:13 +0200410 }
411
412 if (config_.decode) {
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200413 DecodeFrame(*encoded_image_for_decode, spatial_idx);
Sergey Silkin645e2e02018-04-06 09:42:13 +0200414
Sergey Silkin3c30c9c2018-05-02 09:18:48 +0200415 if (end_of_picture && num_spatial_layers > 1) {
Sergey Silkin645e2e02018-04-06 09:42:13 +0200416 // If inter-layer prediction is enabled and upper layer was dropped then
417 // base layer should be passed to upper layer decoder. Otherwise decoder
418 // won't be able to decode next superframe.
419 const EncodedImage* base_image = nullptr;
Sergey Silkin3c30c9c2018-05-02 09:18:48 +0200420 const FrameStatistics* base_stat = nullptr;
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200421 for (size_t i = 0; i < num_spatial_layers; ++i) {
Sergey Silkin3c30c9c2018-05-02 09:18:48 +0200422 const bool layer_dropped = (first_decoded_frame_[i] ||
423 last_decoded_frame_num_[i] < frame_number);
Sergey Silkin645e2e02018-04-06 09:42:13 +0200424
425 // Ensure current layer was decoded.
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200426 RTC_CHECK(layer_dropped == false || i != spatial_idx);
Sergey Silkin645e2e02018-04-06 09:42:13 +0200427
428 if (!layer_dropped) {
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200429 base_image = &merged_encoded_frames_[i];
Sergey Silkin3c30c9c2018-05-02 09:18:48 +0200430 base_stat =
431 stats_->GetFrameWithTimestamp(encoded_image._timeStamp, i);
432 } else if (base_image && !base_stat->non_ref_for_inter_layer_pred) {
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200433 DecodeFrame(*base_image, i);
Sergey Silkin645e2e02018-04-06 09:42:13 +0200434 }
435 }
436 }
Rasmus Brandtd00c8952018-03-14 12:29:57 +0100437 } else {
438 frame_stat->decode_return_code = WEBRTC_VIDEO_CODEC_NO_OUTPUT;
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100439 }
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100440
441 if (encoded_frame_writers_) {
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200442 RTC_CHECK(encoded_frame_writers_->at(spatial_idx)
Sergey Silkin122ba6c2018-03-27 14:32:21 +0200443 ->WriteFrame(*encoded_image_for_decode,
444 config_.codec_settings.codecType));
brandtr8935d972017-09-06 01:53:22 -0700445 }
Sergey Silkinc89eed92018-04-01 23:57:51 +0200446
447 if (!config_.IsAsyncCodec()) {
448 // To get pure encode time for next layers, measure time spent in encode
449 // callback and subtract it from encode time of next layers.
450 post_encode_time_ns_ += rtc::TimeNanos() - encode_stop_ns;
451 }
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000452}
453
Sergey Silkin645e2e02018-04-06 09:42:13 +0200454void VideoProcessor::FrameDecoded(const VideoFrame& decoded_frame,
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200455 size_t spatial_idx) {
brandtrc8c59052017-08-21 06:44:16 -0700456 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
457
brandtr32e0d262017-02-15 05:29:38 -0800458 // For the highest measurement accuracy of the decode time, the start/stop
459 // time recordings should wrap the Decode call as tightly as possible.
Rasmus Brandt5f7a8912018-02-28 17:17:15 +0100460 const int64_t decode_stop_ns = rtc::TimeNanos();
brandtr8bc93852017-02-15 05:19:51 -0800461
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200462 FrameStatistics* frame_stat =
463 stats_->GetFrameWithTimestamp(decoded_frame.timestamp(), spatial_idx);
Åsa Perssona6e7b882018-01-19 14:57:10 +0100464 const size_t frame_number = frame_stat->frame_number;
Sergey Silkin64eaa992017-11-17 14:47:32 +0100465
Sergey Silkin56138792018-05-02 10:50:55 +0200466 if (decoded_frame_writers_ && !first_decoded_frame_[spatial_idx]) {
467 // Fill drops with last decoded frame to make them look like freeze at
468 // playback and to keep decoded layers in sync.
469 for (size_t i = last_decoded_frame_num_[spatial_idx] + 1; i < frame_number;
470 ++i) {
471 RTC_CHECK(decoded_frame_writers_->at(spatial_idx)
472 ->WriteFrame(decoded_frame_buffer_[spatial_idx].data()));
473 }
474 }
475
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100476 // Ensure that the decode order is monotonically increasing, within this
477 // simulcast/spatial layer.
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200478 RTC_CHECK(first_decoded_frame_[spatial_idx] ||
479 last_decoded_frame_num_[spatial_idx] < frame_number);
480 first_decoded_frame_[spatial_idx] = false;
481 last_decoded_frame_num_[spatial_idx] = frame_number;
brandtr17b958c2017-03-07 01:41:43 -0800482
Sergey Silkin10d9d592018-02-01 13:25:17 +0100483 // Update frame statistics.
484 frame_stat->decoding_successful = true;
485 frame_stat->decode_time_us =
486 GetElapsedTimeMicroseconds(frame_stat->decode_start_ns, decode_stop_ns);
487 frame_stat->decoded_width = decoded_frame.width();
488 frame_stat->decoded_height = decoded_frame.height();
489
Sergey Silkin64eaa992017-11-17 14:47:32 +0100490 // Skip quality metrics calculation to not affect CPU usage.
491 if (!config_.measure_cpu) {
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100492 const auto reference_frame = input_frames_.find(frame_number);
493 RTC_CHECK(reference_frame != input_frames_.cend())
494 << "The codecs are either buffering too much, dropping too much, or "
495 "being too slow relative the input frame rate.";
Sergey Silkin8d3758e2018-03-14 11:28:15 +0100496 CalculateFrameQuality(
497 *reference_frame->second.video_frame_buffer()->ToI420(),
498 *decoded_frame.video_frame_buffer()->ToI420(), frame_stat);
Niels Möller718a7632016-06-13 13:06:01 +0200499
Rasmus Brandtd00c8952018-03-14 12:29:57 +0100500 // Erase all buffered input frames that we have moved past for all
501 // simulcast/spatial layers. Never buffer more than
502 // |kMaxBufferedInputFrames| frames, to protect against long runs of
503 // consecutive frame drops for a particular layer.
504 const auto min_last_decoded_frame_num = std::min_element(
505 last_decoded_frame_num_.cbegin(), last_decoded_frame_num_.cend());
506 const size_t min_buffered_frame_num = std::max(
507 0, static_cast<int>(frame_number) - kMaxBufferedInputFrames + 1);
508 RTC_CHECK(min_last_decoded_frame_num != last_decoded_frame_num_.cend());
509 const auto input_frames_erase_before = input_frames_.lower_bound(
510 std::max(*min_last_decoded_frame_num, min_buffered_frame_num));
511 input_frames_.erase(input_frames_.cbegin(), input_frames_erase_before);
512 }
Sergey Silkin64eaa992017-11-17 14:47:32 +0100513
Sergey Silkin10d9d592018-02-01 13:25:17 +0100514 if (decoded_frame_writers_) {
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100515 ExtractI420BufferWithSize(decoded_frame, config_.codec_settings.width,
Sergey Silkin56138792018-05-02 10:50:55 +0200516 config_.codec_settings.height,
517 &decoded_frame_buffer_[spatial_idx]);
518 RTC_CHECK_EQ(decoded_frame_buffer_[spatial_idx].size(),
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200519 decoded_frame_writers_->at(spatial_idx)->FrameLength());
520 RTC_CHECK(decoded_frame_writers_->at(spatial_idx)
Sergey Silkin56138792018-05-02 10:50:55 +0200521 ->WriteFrame(decoded_frame_buffer_[spatial_idx].data()));
Sergey Silkin64eaa992017-11-17 14:47:32 +0100522 }
Åsa Perssonf0c44672017-10-24 16:03:39 +0200523}
brandtr17b958c2017-03-07 01:41:43 -0800524
Sergey Silkin645e2e02018-04-06 09:42:13 +0200525void VideoProcessor::DecodeFrame(const EncodedImage& encoded_image,
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200526 size_t spatial_idx) {
Sergey Silkin645e2e02018-04-06 09:42:13 +0200527 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200528 FrameStatistics* frame_stat =
529 stats_->GetFrameWithTimestamp(encoded_image._timeStamp, spatial_idx);
Sergey Silkin645e2e02018-04-06 09:42:13 +0200530
531 frame_stat->decode_start_ns = rtc::TimeNanos();
532 frame_stat->decode_return_code =
Niels Möller401d0762018-05-08 11:54:29 +0200533 decoders_->at(spatial_idx)->Decode(encoded_image, false, nullptr, 0);
Sergey Silkin645e2e02018-04-06 09:42:13 +0200534}
535
536const webrtc::EncodedImage* VideoProcessor::BuildAndStoreSuperframe(
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100537 const EncodedImage& encoded_image,
538 const VideoCodecType codec,
539 size_t frame_number,
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200540 size_t spatial_idx,
Sergey Silkin645e2e02018-04-06 09:42:13 +0200541 bool inter_layer_predicted) {
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100542 // Should only be called for SVC.
543 RTC_CHECK_GT(config_.NumberOfSpatialLayers(), 1);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100544
545 EncodedImage base_image;
546 RTC_CHECK_EQ(base_image._length, 0);
547
Sergey Silkin122ba6c2018-03-27 14:32:21 +0200548 // Each SVC layer is decoded with dedicated decoder. Find the nearest
549 // non-dropped base frame and merge it and current frame into superframe.
Sergey Silkin645e2e02018-04-06 09:42:13 +0200550 if (inter_layer_predicted) {
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200551 for (int base_idx = static_cast<int>(spatial_idx) - 1; base_idx >= 0;
Sergey Silkin122ba6c2018-03-27 14:32:21 +0200552 --base_idx) {
553 EncodedImage lower_layer = merged_encoded_frames_.at(base_idx);
554 if (lower_layer._timeStamp == encoded_image._timeStamp) {
555 base_image = lower_layer;
556 break;
557 }
558 }
Sergey Silkin10d9d592018-02-01 13:25:17 +0100559 }
Sergey Silkin10d9d592018-02-01 13:25:17 +0100560 const size_t payload_size_bytes = base_image._length + encoded_image._length;
561 const size_t buffer_size_bytes =
562 payload_size_bytes + EncodedImage::GetBufferPaddingBytes(codec);
563
564 uint8_t* copied_buffer = new uint8_t[buffer_size_bytes];
565 RTC_CHECK(copied_buffer);
566
567 if (base_image._length) {
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100568 RTC_CHECK(base_image._buffer);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100569 memcpy(copied_buffer, base_image._buffer, base_image._length);
570 }
Sergey Silkin10d9d592018-02-01 13:25:17 +0100571 memcpy(copied_buffer + base_image._length, encoded_image._buffer,
572 encoded_image._length);
573
574 EncodedImage copied_image = encoded_image;
575 copied_image = encoded_image;
576 copied_image._buffer = copied_buffer;
577 copied_image._length = payload_size_bytes;
578 copied_image._size = buffer_size_bytes;
579
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100580 // Replace previous EncodedImage for this spatial layer.
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200581 uint8_t* old_buffer = merged_encoded_frames_.at(spatial_idx)._buffer;
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100582 if (old_buffer) {
583 delete[] old_buffer;
584 }
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200585 merged_encoded_frames_.at(spatial_idx) = copied_image;
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100586
Sergey Silkinbc20fe12018-04-15 21:06:16 +0200587 return &merged_encoded_frames_.at(spatial_idx);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100588}
589
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000590} // namespace test
591} // namespace webrtc