kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 1 | /* |
pwestin@webrtc.org | ce33035 | 2012-04-12 06:59:14 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 3 | * |
| 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.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/video_coding/codecs/test/videoprocessor.h" |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 12 | |
ssilkin | 612f858 | 2017-09-28 09:23:17 -0700 | [diff] [blame] | 13 | #include <algorithm> |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 14 | #include <limits> |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 15 | #include <utility> |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "api/video/i420_buffer.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 18 | #include "common_types.h" // NOLINT(build/include) |
ssilkin | 612f858 | 2017-09-28 09:23:17 -0700 | [diff] [blame] | 19 | #include "common_video/h264/h264_common.h" |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 20 | #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "modules/video_coding/codecs/vp8/simulcast_rate_allocator.h" |
| 22 | #include "modules/video_coding/include/video_codec_initializer.h" |
| 23 | #include "modules/video_coding/utility/default_video_bitrate_allocator.h" |
| 24 | #include "rtc_base/checks.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "rtc_base/timeutils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "test/gtest.h" |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 27 | #include "third_party/libyuv/include/libyuv/scale.h" |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 28 | |
| 29 | namespace webrtc { |
| 30 | namespace test { |
| 31 | |
brandtr | b78bc75 | 2017-02-22 01:26:59 -0800 | [diff] [blame] | 32 | namespace { |
Åsa Persson | 91af24a | 2018-01-24 17:20:18 +0100 | [diff] [blame] | 33 | const int kMsToRtpTimestamp = kVideoPayloadTypeFrequency / 1000; |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 34 | |
brandtr | aebc61e | 2017-02-28 07:13:47 -0800 | [diff] [blame] | 35 | std::unique_ptr<VideoBitrateAllocator> CreateBitrateAllocator( |
brandtr | 07734a5 | 2017-08-08 08:35:53 -0700 | [diff] [blame] | 36 | TestConfig* config) { |
brandtr | aebc61e | 2017-02-28 07:13:47 -0800 | [diff] [blame] | 37 | std::unique_ptr<TemporalLayersFactory> tl_factory; |
brandtr | 07734a5 | 2017-08-08 08:35:53 -0700 | [diff] [blame] | 38 | if (config->codec_settings.codecType == VideoCodecType::kVideoCodecVP8) { |
brandtr | aebc61e | 2017-02-28 07:13:47 -0800 | [diff] [blame] | 39 | tl_factory.reset(new TemporalLayersFactory()); |
brandtr | 07734a5 | 2017-08-08 08:35:53 -0700 | [diff] [blame] | 40 | config->codec_settings.VP8()->tl_factory = tl_factory.get(); |
brandtr | aebc61e | 2017-02-28 07:13:47 -0800 | [diff] [blame] | 41 | } |
| 42 | return std::unique_ptr<VideoBitrateAllocator>( |
brandtr | 07734a5 | 2017-08-08 08:35:53 -0700 | [diff] [blame] | 43 | VideoCodecInitializer::CreateBitrateAllocator(config->codec_settings, |
brandtr | aebc61e | 2017-02-28 07:13:47 -0800 | [diff] [blame] | 44 | std::move(tl_factory))); |
| 45 | } |
| 46 | |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 47 | size_t GetMaxNaluSizeBytes(const EncodedImage& encoded_frame, |
| 48 | const TestConfig& config) { |
ssilkin | 612f858 | 2017-09-28 09:23:17 -0700 | [diff] [blame] | 49 | if (config.codec_settings.codecType != kVideoCodecH264) |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 50 | return 0; |
ssilkin | 612f858 | 2017-09-28 09:23:17 -0700 | [diff] [blame] | 51 | |
| 52 | std::vector<webrtc::H264::NaluIndex> nalu_indices = |
| 53 | webrtc::H264::FindNaluIndices(encoded_frame._buffer, |
| 54 | encoded_frame._length); |
| 55 | |
| 56 | RTC_CHECK(!nalu_indices.empty()); |
| 57 | |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 58 | size_t max_size = 0; |
ssilkin | 612f858 | 2017-09-28 09:23:17 -0700 | [diff] [blame] | 59 | for (const webrtc::H264::NaluIndex& index : nalu_indices) |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 60 | max_size = std::max(max_size, index.payload_size); |
ssilkin | 612f858 | 2017-09-28 09:23:17 -0700 | [diff] [blame] | 61 | |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 62 | return max_size; |
ssilkin | 612f858 | 2017-09-28 09:23:17 -0700 | [diff] [blame] | 63 | } |
| 64 | |
asapersson | ae9ba04 | 2017-03-07 00:25:38 -0800 | [diff] [blame] | 65 | int GetElapsedTimeMicroseconds(int64_t start_ns, int64_t stop_ns) { |
| 66 | int64_t diff_us = (stop_ns - start_ns) / rtc::kNumNanosecsPerMicrosec; |
| 67 | RTC_DCHECK_GE(diff_us, std::numeric_limits<int>::min()); |
| 68 | RTC_DCHECK_LE(diff_us, std::numeric_limits<int>::max()); |
| 69 | return static_cast<int>(diff_us); |
| 70 | } |
| 71 | |
Åsa Persson | f0c4467 | 2017-10-24 16:03:39 +0200 | [diff] [blame] | 72 | void ExtractBufferWithSize(const VideoFrame& image, |
| 73 | int width, |
| 74 | int height, |
| 75 | rtc::Buffer* buffer) { |
| 76 | if (image.width() != width || image.height() != height) { |
| 77 | EXPECT_DOUBLE_EQ(static_cast<double>(width) / height, |
| 78 | static_cast<double>(image.width()) / image.height()); |
| 79 | // Same aspect ratio, no cropping needed. |
| 80 | rtc::scoped_refptr<I420Buffer> scaled(I420Buffer::Create(width, height)); |
| 81 | scaled->ScaleFrom(*image.video_frame_buffer()->ToI420()); |
| 82 | |
| 83 | size_t length = |
| 84 | CalcBufferSize(VideoType::kI420, scaled->width(), scaled->height()); |
| 85 | buffer->SetSize(length); |
| 86 | RTC_CHECK_NE(ExtractBuffer(scaled, length, buffer->data()), -1); |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | // No resize. |
| 91 | size_t length = |
| 92 | CalcBufferSize(VideoType::kI420, image.width(), image.height()); |
| 93 | buffer->SetSize(length); |
| 94 | RTC_CHECK_NE(ExtractBuffer(image, length, buffer->data()), -1); |
| 95 | } |
| 96 | |
brandtr | b78bc75 | 2017-02-22 01:26:59 -0800 | [diff] [blame] | 97 | } // namespace |
| 98 | |
brandtr | c409552 | 2017-08-07 08:12:33 -0700 | [diff] [blame] | 99 | VideoProcessor::VideoProcessor(webrtc::VideoEncoder* encoder, |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 100 | VideoDecoderList* decoders, |
| 101 | FrameReader* input_frame_reader, |
brandtr | c409552 | 2017-08-07 08:12:33 -0700 | [diff] [blame] | 102 | const TestConfig& config, |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 103 | std::vector<Stats>* stats, |
| 104 | IvfFileWriterList* encoded_frame_writers, |
| 105 | FrameWriterList* decoded_frame_writers) |
Åsa Persson | f0c4467 | 2017-10-24 16:03:39 +0200 | [diff] [blame] | 106 | : config_(config), |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 107 | num_simulcast_or_spatial_layers_( |
| 108 | std::max(config_.NumberOfSimulcastStreams(), |
| 109 | config_.NumberOfSpatialLayers())), |
brandtr | 07734a5 | 2017-08-08 08:35:53 -0700 | [diff] [blame] | 110 | encoder_(encoder), |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 111 | decoders_(decoders), |
brandtr | 07734a5 | 2017-08-08 08:35:53 -0700 | [diff] [blame] | 112 | bitrate_allocator_(CreateBitrateAllocator(&config_)), |
brandtr | bdd555c | 2017-08-21 01:34:04 -0700 | [diff] [blame] | 113 | encode_callback_(this), |
| 114 | decode_callback_(this), |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 115 | input_frame_reader_(input_frame_reader), |
| 116 | encoded_frame_writers_(encoded_frame_writers), |
| 117 | decoded_frame_writers_(decoded_frame_writers), |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 118 | last_inputed_frame_num_(0), |
| 119 | last_encoded_frame_num_(0), |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 120 | last_encoded_simulcast_svc_idx_(0), |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 121 | last_decoded_frame_num_(0), |
| 122 | num_encoded_frames_(0), |
| 123 | num_decoded_frames_(0), |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 124 | stats_(stats) { |
Rasmus Brandt | 4b381af | 2018-02-07 13:56:16 +0100 | [diff] [blame] | 125 | RTC_CHECK(rtc::TaskQueue::Current()) |
| 126 | << "VideoProcessor must be run on a task queue."; |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 127 | RTC_CHECK(encoder); |
| 128 | RTC_CHECK(decoders && decoders->size() == num_simulcast_or_spatial_layers_); |
| 129 | RTC_CHECK(input_frame_reader); |
| 130 | RTC_CHECK(stats); |
| 131 | RTC_CHECK(!encoded_frame_writers || |
| 132 | encoded_frame_writers->size() == num_simulcast_or_spatial_layers_); |
| 133 | RTC_CHECK(!decoded_frame_writers || |
| 134 | decoded_frame_writers->size() == num_simulcast_or_spatial_layers_); |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 135 | |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 136 | // Setup required callbacks for the encoder and decoder and initialize them. |
brandtr | bdd555c | 2017-08-21 01:34:04 -0700 | [diff] [blame] | 137 | RTC_CHECK_EQ(encoder_->RegisterEncodeCompleteCallback(&encode_callback_), |
Åsa Persson | f0c4467 | 2017-10-24 16:03:39 +0200 | [diff] [blame] | 138 | WEBRTC_VIDEO_CODEC_OK); |
asapersson | 654d54c | 2017-02-10 00:16:07 -0800 | [diff] [blame] | 139 | |
Sergey Silkin | 1723cf9 | 2018-01-22 15:49:55 +0100 | [diff] [blame] | 140 | RTC_CHECK_EQ(encoder_->InitEncode(&config_.codec_settings, |
| 141 | static_cast<int>(config_.NumberOfCores()), |
| 142 | config_.max_payload_size_bytes), |
| 143 | WEBRTC_VIDEO_CODEC_OK); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 144 | |
| 145 | for (auto& decoder : *decoders_) { |
| 146 | RTC_CHECK_EQ(decoder->InitDecode(&config_.codec_settings, |
| 147 | static_cast<int>(config_.NumberOfCores())), |
| 148 | WEBRTC_VIDEO_CODEC_OK); |
| 149 | RTC_CHECK_EQ(decoder->RegisterDecodeCompleteCallback(&decode_callback_), |
| 150 | WEBRTC_VIDEO_CODEC_OK); |
| 151 | } |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Åsa Persson | f0c4467 | 2017-10-24 16:03:39 +0200 | [diff] [blame] | 154 | VideoProcessor::~VideoProcessor() { |
brandtr | c8c5905 | 2017-08-21 06:44:16 -0700 | [diff] [blame] | 155 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
| 156 | |
brandtr | 77920a4 | 2017-08-11 07:48:15 -0700 | [diff] [blame] | 157 | RTC_CHECK_EQ(encoder_->Release(), WEBRTC_VIDEO_CODEC_OK); |
brandtr | bdd555c | 2017-08-21 01:34:04 -0700 | [diff] [blame] | 158 | encoder_->RegisterEncodeCompleteCallback(nullptr); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 159 | |
| 160 | for (auto& decoder : *decoders_) { |
| 161 | RTC_CHECK_EQ(decoder->Release(), WEBRTC_VIDEO_CODEC_OK); |
| 162 | decoder->RegisterDecodeCompleteCallback(nullptr); |
| 163 | } |
| 164 | |
| 165 | RTC_CHECK(last_encoded_frames_.empty()); |
brandtr | 77920a4 | 2017-08-11 07:48:15 -0700 | [diff] [blame] | 166 | } |
| 167 | |
brandtr | 8935d97 | 2017-09-06 01:53:22 -0700 | [diff] [blame] | 168 | void VideoProcessor::ProcessFrame() { |
brandtr | c8c5905 | 2017-08-21 06:44:16 -0700 | [diff] [blame] | 169 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 170 | const size_t frame_number = last_inputed_frame_num_++; |
asapersson | 654d54c | 2017-02-10 00:16:07 -0800 | [diff] [blame] | 171 | |
brandtr | bdd555c | 2017-08-21 01:34:04 -0700 | [diff] [blame] | 172 | // Get frame from file. |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 173 | rtc::scoped_refptr<I420BufferInterface> buffer( |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 174 | input_frame_reader_->ReadFrame()); |
brandtr | bdd555c | 2017-08-21 01:34:04 -0700 | [diff] [blame] | 175 | RTC_CHECK(buffer) << "Tried to read too many frames from the file."; |
Åsa Persson | 91af24a | 2018-01-24 17:20:18 +0100 | [diff] [blame] | 176 | |
| 177 | size_t rtp_timestamp = |
| 178 | (frame_number > 0) ? input_frames_[frame_number - 1]->timestamp() : 0; |
| 179 | rtp_timestamp += |
| 180 | kVideoPayloadTypeFrequency / config_.codec_settings.maxFramerate; |
| 181 | |
| 182 | input_frames_[frame_number] = rtc::MakeUnique<VideoFrame>( |
| 183 | buffer, static_cast<uint32_t>(rtp_timestamp), |
| 184 | static_cast<int64_t>(rtp_timestamp / kMsToRtpTimestamp), |
| 185 | webrtc::kVideoRotation_0); |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 186 | |
Sergey Silkin | 64eaa99 | 2017-11-17 14:47:32 +0100 | [diff] [blame] | 187 | std::vector<FrameType> frame_types = config_.FrameTypeForFrame(frame_number); |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 188 | |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 189 | // Create frame statistics object for all simulcast /spatial layers. |
| 190 | for (size_t simulcast_svc_idx = 0; |
| 191 | simulcast_svc_idx < num_simulcast_or_spatial_layers_; |
| 192 | ++simulcast_svc_idx) { |
| 193 | stats_->at(simulcast_svc_idx).AddFrame(rtp_timestamp); |
| 194 | } |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 195 | |
| 196 | // For the highest measurement accuracy of the encode time, the start/stop |
| 197 | // time recordings should wrap the Encode call as tightly as possible. |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 198 | const int64_t encode_start_ns = rtc::TimeNanos(); |
| 199 | for (size_t simulcast_svc_idx = 0; |
| 200 | simulcast_svc_idx < num_simulcast_or_spatial_layers_; |
| 201 | ++simulcast_svc_idx) { |
| 202 | FrameStatistic* frame_stat = |
| 203 | stats_->at(simulcast_svc_idx).GetFrame(frame_number); |
| 204 | frame_stat->encode_start_ns = encode_start_ns; |
| 205 | } |
| 206 | |
| 207 | const int encode_return_code = |
Sergey Silkin | 64eaa99 | 2017-11-17 14:47:32 +0100 | [diff] [blame] | 208 | encoder_->Encode(*input_frames_[frame_number], nullptr, &frame_types); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 209 | |
| 210 | for (size_t simulcast_svc_idx = 0; |
| 211 | simulcast_svc_idx < num_simulcast_or_spatial_layers_; |
| 212 | ++simulcast_svc_idx) { |
| 213 | FrameStatistic* frame_stat = |
| 214 | stats_->at(simulcast_svc_idx).GetFrame(frame_number); |
| 215 | frame_stat->encode_return_code = encode_return_code; |
| 216 | } |
| 217 | |
| 218 | // For async codecs frame decoding is done in frame encode callback. |
| 219 | if (!config_.IsAsyncCodec()) { |
| 220 | for (size_t simulcast_svc_idx = 0; |
| 221 | simulcast_svc_idx < num_simulcast_or_spatial_layers_; |
| 222 | ++simulcast_svc_idx) { |
| 223 | if (last_encoded_frames_.find(simulcast_svc_idx) != |
| 224 | last_encoded_frames_.end()) { |
| 225 | EncodedImage& encoded_image = last_encoded_frames_[simulcast_svc_idx]; |
| 226 | |
| 227 | FrameStatistic* frame_stat = |
| 228 | stats_->at(simulcast_svc_idx).GetFrame(frame_number); |
| 229 | |
| 230 | if (encoded_frame_writers_) { |
| 231 | RTC_CHECK(encoded_frame_writers_->at(simulcast_svc_idx) |
| 232 | ->WriteFrame(encoded_image, |
| 233 | config_.codec_settings.codecType)); |
| 234 | } |
| 235 | |
| 236 | // For the highest measurement accuracy of the decode time, the |
| 237 | // start/stop time recordings should wrap the Decode call as tightly as |
| 238 | // possible. |
| 239 | frame_stat->decode_start_ns = rtc::TimeNanos(); |
| 240 | frame_stat->decode_return_code = |
| 241 | decoders_->at(simulcast_svc_idx) |
| 242 | ->Decode(encoded_image, false, nullptr); |
| 243 | |
| 244 | RTC_CHECK(encoded_image._buffer); |
| 245 | delete[] encoded_image._buffer; |
| 246 | encoded_image._buffer = nullptr; |
| 247 | |
| 248 | last_encoded_frames_.erase(simulcast_svc_idx); |
| 249 | } |
| 250 | } |
| 251 | } |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 254 | void VideoProcessor::SetRates(size_t bitrate_kbps, size_t framerate_fps) { |
brandtr | c8c5905 | 2017-08-21 06:44:16 -0700 | [diff] [blame] | 255 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 256 | config_.codec_settings.maxFramerate = static_cast<uint32_t>(framerate_fps); |
| 257 | bitrate_allocation_ = bitrate_allocator_->GetAllocation( |
| 258 | static_cast<uint32_t>(bitrate_kbps * 1000), |
| 259 | static_cast<uint32_t>(framerate_fps)); |
| 260 | const int set_rates_result = encoder_->SetRateAllocation( |
| 261 | bitrate_allocation_, static_cast<uint32_t>(framerate_fps)); |
brandtr | bea36fd | 2017-08-07 03:36:54 -0700 | [diff] [blame] | 262 | RTC_DCHECK_GE(set_rates_result, 0) |
brandtr | bdd555c | 2017-08-21 01:34:04 -0700 | [diff] [blame] | 263 | << "Failed to update encoder with new rate " << bitrate_kbps << "."; |
brandtr | bea36fd | 2017-08-07 03:36:54 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 266 | void VideoProcessor::FrameEncoded( |
| 267 | const webrtc::EncodedImage& encoded_image, |
| 268 | const webrtc::CodecSpecificInfo& codec_specific) { |
brandtr | c8c5905 | 2017-08-21 06:44:16 -0700 | [diff] [blame] | 269 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
| 270 | |
brandtr | 32e0d26 | 2017-02-15 05:29:38 -0800 | [diff] [blame] | 271 | // For the highest measurement accuracy of the encode time, the start/stop |
| 272 | // time recordings should wrap the Encode call as tightly as possible. |
| 273 | int64_t encode_stop_ns = rtc::TimeNanos(); |
| 274 | |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 275 | const VideoCodecType codec = codec_specific.codecType; |
Rasmus Brandt | f7a3558 | 2017-10-24 10:16:33 +0200 | [diff] [blame] | 276 | if (config_.encoded_frame_checker) { |
| 277 | config_.encoded_frame_checker->CheckEncodedFrame(codec, encoded_image); |
| 278 | } |
brandtr | b78bc75 | 2017-02-22 01:26:59 -0800 | [diff] [blame] | 279 | |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 280 | size_t simulcast_svc_idx = 0; |
| 281 | size_t temporal_idx = 0; |
Sergey Silkin | 64eaa99 | 2017-11-17 14:47:32 +0100 | [diff] [blame] | 282 | |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 283 | if (codec == kVideoCodecVP8) { |
| 284 | simulcast_svc_idx = codec_specific.codecSpecific.VP8.simulcastIdx; |
| 285 | temporal_idx = codec_specific.codecSpecific.VP8.temporalIdx; |
| 286 | } else if (codec == kVideoCodecVP9) { |
| 287 | simulcast_svc_idx = codec_specific.codecSpecific.VP9.spatial_idx; |
| 288 | temporal_idx = codec_specific.codecSpecific.VP9.temporal_idx; |
| 289 | } |
| 290 | |
| 291 | if (simulcast_svc_idx == kNoSpatialIdx) { |
| 292 | simulcast_svc_idx = 0; |
| 293 | } |
| 294 | |
| 295 | if (temporal_idx == kNoTemporalIdx) { |
| 296 | temporal_idx = 0; |
| 297 | } |
| 298 | |
| 299 | const size_t frame_wxh = |
| 300 | encoded_image._encodedWidth * encoded_image._encodedHeight; |
| 301 | frame_wxh_to_simulcast_svc_idx_[frame_wxh] = simulcast_svc_idx; |
| 302 | |
| 303 | FrameStatistic* frame_stat = |
| 304 | stats_->at(simulcast_svc_idx) |
| 305 | .GetFrameWithTimestamp(encoded_image._timeStamp); |
Åsa Persson | a6e7b88 | 2018-01-19 14:57:10 +0100 | [diff] [blame] | 306 | const size_t frame_number = frame_stat->frame_number; |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 307 | |
| 308 | // Reordering is unexpected. Frames of different layers have the same value |
| 309 | // of frame_number. VP8 multi-res delivers frames starting from hires layer. |
| 310 | RTC_CHECK_GE(frame_number, last_encoded_frame_num_); |
| 311 | |
| 312 | // Ensure SVC spatial layers are delivered in ascending order. |
| 313 | if (config_.NumberOfSpatialLayers() > 1) { |
| 314 | RTC_CHECK(simulcast_svc_idx > last_encoded_simulcast_svc_idx_ || |
| 315 | frame_number != last_encoded_frame_num_ || |
| 316 | num_encoded_frames_ == 0); |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 317 | } |
Sergey Silkin | 64eaa99 | 2017-11-17 14:47:32 +0100 | [diff] [blame] | 318 | |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 319 | last_encoded_frame_num_ = frame_number; |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 320 | last_encoded_simulcast_svc_idx_ = simulcast_svc_idx; |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 321 | |
brandtr | 8935d97 | 2017-09-06 01:53:22 -0700 | [diff] [blame] | 322 | // Update frame statistics. |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 323 | frame_stat->encoding_successful = true; |
brandtr | 8935d97 | 2017-09-06 01:53:22 -0700 | [diff] [blame] | 324 | frame_stat->encode_time_us = |
| 325 | GetElapsedTimeMicroseconds(frame_stat->encode_start_ns, encode_stop_ns); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 326 | |
| 327 | // TODO(ssilkin): Implement bitrate allocation for VP9 SVC. For now set |
| 328 | // target for base layers equal to total target to avoid devision by zero |
| 329 | // at analysis. |
| 330 | frame_stat->target_bitrate_kbps = |
| 331 | bitrate_allocation_.GetSpatialLayerSum( |
| 332 | codec == kVideoCodecVP9 ? 0 : simulcast_svc_idx) / |
| 333 | 1000; |
brandtr | 8935d97 | 2017-09-06 01:53:22 -0700 | [diff] [blame] | 334 | frame_stat->encoded_frame_size_bytes = encoded_image._length; |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 335 | frame_stat->frame_type = encoded_image._frameType; |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 336 | frame_stat->temporal_layer_idx = temporal_idx; |
| 337 | frame_stat->simulcast_svc_idx = simulcast_svc_idx; |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 338 | frame_stat->max_nalu_size_bytes = GetMaxNaluSizeBytes(encoded_image, config_); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 339 | frame_stat->qp = encoded_image.qp_; |
ssilkin | 612f858 | 2017-09-28 09:23:17 -0700 | [diff] [blame] | 340 | |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 341 | if (!config_.IsAsyncCodec()) { |
| 342 | // Store encoded frame. It will be decoded after all layers are encoded. |
| 343 | CopyEncodedImage(encoded_image, codec, frame_number, simulcast_svc_idx); |
| 344 | } else { |
| 345 | const size_t simulcast_idx = |
| 346 | codec == kVideoCodecVP8 ? codec_specific.codecSpecific.VP8.simulcastIdx |
| 347 | : 0; |
| 348 | frame_stat->decode_start_ns = rtc::TimeNanos(); |
| 349 | frame_stat->decode_return_code = |
| 350 | decoders_->at(simulcast_idx)->Decode(encoded_image, false, nullptr); |
brandtr | 8935d97 | 2017-09-06 01:53:22 -0700 | [diff] [blame] | 351 | } |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 352 | |
| 353 | ++num_encoded_frames_; |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Sergey Silkin | 64eaa99 | 2017-11-17 14:47:32 +0100 | [diff] [blame] | 356 | void VideoProcessor::FrameDecoded(const VideoFrame& decoded_frame) { |
brandtr | c8c5905 | 2017-08-21 06:44:16 -0700 | [diff] [blame] | 357 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
| 358 | |
brandtr | 32e0d26 | 2017-02-15 05:29:38 -0800 | [diff] [blame] | 359 | // For the highest measurement accuracy of the decode time, the start/stop |
| 360 | // time recordings should wrap the Decode call as tightly as possible. |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 361 | int64_t decode_stop_ns = rtc::TimeNanos(); |
brandtr | 8bc9385 | 2017-02-15 05:19:51 -0800 | [diff] [blame] | 362 | |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 363 | RTC_CHECK(frame_wxh_to_simulcast_svc_idx_.find(decoded_frame.size()) != |
| 364 | frame_wxh_to_simulcast_svc_idx_.end()); |
| 365 | const size_t simulcast_svc_idx = |
| 366 | frame_wxh_to_simulcast_svc_idx_[decoded_frame.size()]; |
| 367 | |
Åsa Persson | a6e7b88 | 2018-01-19 14:57:10 +0100 | [diff] [blame] | 368 | FrameStatistic* frame_stat = |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 369 | stats_->at(simulcast_svc_idx) |
| 370 | .GetFrameWithTimestamp(decoded_frame.timestamp()); |
Åsa Persson | a6e7b88 | 2018-01-19 14:57:10 +0100 | [diff] [blame] | 371 | const size_t frame_number = frame_stat->frame_number; |
Sergey Silkin | 64eaa99 | 2017-11-17 14:47:32 +0100 | [diff] [blame] | 372 | |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 373 | // Reordering is unexpected. Frames of different layers have the same value |
| 374 | // of frame_number. |
| 375 | RTC_CHECK_GE(frame_number, last_decoded_frame_num_); |
| 376 | |
| 377 | if (decoded_frame_writers_ && num_decoded_frames_ > 0) { |
| 378 | // For dropped frames, write out the last decoded frame to make it look like |
| 379 | // a freeze at playback. |
| 380 | for (size_t num_dropped_frames = 0; num_dropped_frames < frame_number; |
| 381 | ++num_dropped_frames) { |
| 382 | const FrameStatistic* prev_frame_stat = |
| 383 | stats_->at(simulcast_svc_idx) |
| 384 | .GetFrame(frame_number - num_dropped_frames - 1); |
| 385 | if (prev_frame_stat->decoding_successful) { |
| 386 | break; |
Sergey Silkin | 64eaa99 | 2017-11-17 14:47:32 +0100 | [diff] [blame] | 387 | } |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 388 | WriteDecodedFrameToFile(&last_decoded_frame_buffers_[simulcast_svc_idx], |
| 389 | simulcast_svc_idx); |
Sergey Silkin | 64eaa99 | 2017-11-17 14:47:32 +0100 | [diff] [blame] | 390 | } |
marpan@webrtc.org | f4c2de9 | 2012-06-05 21:07:28 +0000 | [diff] [blame] | 391 | } |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 392 | |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 393 | last_decoded_frame_num_ = frame_number; |
| 394 | |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 395 | // Update frame statistics. |
| 396 | frame_stat->decoding_successful = true; |
| 397 | frame_stat->decode_time_us = |
| 398 | GetElapsedTimeMicroseconds(frame_stat->decode_start_ns, decode_stop_ns); |
| 399 | frame_stat->decoded_width = decoded_frame.width(); |
| 400 | frame_stat->decoded_height = decoded_frame.height(); |
| 401 | |
Sergey Silkin | 64eaa99 | 2017-11-17 14:47:32 +0100 | [diff] [blame] | 402 | // Skip quality metrics calculation to not affect CPU usage. |
| 403 | if (!config_.measure_cpu) { |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 404 | CalculateFrameQuality(*input_frames_[frame_number], decoded_frame, |
| 405 | frame_stat); |
Sergey Silkin | 64eaa99 | 2017-11-17 14:47:32 +0100 | [diff] [blame] | 406 | } |
Niels Möller | 718a763 | 2016-06-13 13:06:01 +0200 | [diff] [blame] | 407 | |
Sergey Silkin | 64eaa99 | 2017-11-17 14:47:32 +0100 | [diff] [blame] | 408 | // Delay erasing of input frames by one frame. The current frame might |
| 409 | // still be needed for other simulcast stream or spatial layer. |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 410 | if (frame_number > 0) { |
| 411 | auto input_frame_erase_to = input_frames_.lower_bound(frame_number - 1); |
Sergey Silkin | 64eaa99 | 2017-11-17 14:47:32 +0100 | [diff] [blame] | 412 | input_frames_.erase(input_frames_.begin(), input_frame_erase_to); |
| 413 | } |
| 414 | |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 415 | if (decoded_frame_writers_) { |
Sergey Silkin | 64eaa99 | 2017-11-17 14:47:32 +0100 | [diff] [blame] | 416 | ExtractBufferWithSize(decoded_frame, config_.codec_settings.width, |
| 417 | config_.codec_settings.height, |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 418 | &last_decoded_frame_buffers_[simulcast_svc_idx]); |
| 419 | WriteDecodedFrameToFile(&last_decoded_frame_buffers_[simulcast_svc_idx], |
| 420 | simulcast_svc_idx); |
Sergey Silkin | 64eaa99 | 2017-11-17 14:47:32 +0100 | [diff] [blame] | 421 | } |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 422 | |
| 423 | ++num_decoded_frames_; |
Åsa Persson | f0c4467 | 2017-10-24 16:03:39 +0200 | [diff] [blame] | 424 | } |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 425 | |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 426 | void VideoProcessor::CopyEncodedImage(const EncodedImage& encoded_image, |
| 427 | const VideoCodecType codec, |
| 428 | size_t frame_number, |
| 429 | size_t simulcast_svc_idx) { |
| 430 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
| 431 | |
| 432 | EncodedImage base_image; |
| 433 | RTC_CHECK_EQ(base_image._length, 0); |
| 434 | |
| 435 | // Each SVC layer is decoded with dedicated decoder. Add data of base layers |
| 436 | // to current coded frame buffer. |
| 437 | if (config_.NumberOfSpatialLayers() > 1 && simulcast_svc_idx > 0) { |
| 438 | RTC_CHECK(last_encoded_frames_.find(simulcast_svc_idx - 1) != |
| 439 | last_encoded_frames_.end()); |
| 440 | base_image = last_encoded_frames_[simulcast_svc_idx - 1]; |
| 441 | } |
| 442 | |
| 443 | const size_t payload_size_bytes = base_image._length + encoded_image._length; |
| 444 | const size_t buffer_size_bytes = |
| 445 | payload_size_bytes + EncodedImage::GetBufferPaddingBytes(codec); |
| 446 | |
| 447 | uint8_t* copied_buffer = new uint8_t[buffer_size_bytes]; |
| 448 | RTC_CHECK(copied_buffer); |
| 449 | |
| 450 | if (base_image._length) { |
| 451 | memcpy(copied_buffer, base_image._buffer, base_image._length); |
| 452 | } |
| 453 | |
| 454 | memcpy(copied_buffer + base_image._length, encoded_image._buffer, |
| 455 | encoded_image._length); |
| 456 | |
| 457 | EncodedImage copied_image = encoded_image; |
| 458 | copied_image = encoded_image; |
| 459 | copied_image._buffer = copied_buffer; |
| 460 | copied_image._length = payload_size_bytes; |
| 461 | copied_image._size = buffer_size_bytes; |
| 462 | |
| 463 | last_encoded_frames_[simulcast_svc_idx] = copied_image; |
| 464 | } |
| 465 | |
| 466 | void VideoProcessor::CalculateFrameQuality(const VideoFrame& ref_frame, |
| 467 | const VideoFrame& dec_frame, |
| 468 | FrameStatistic* frame_stat) { |
| 469 | if (ref_frame.width() == dec_frame.width() || |
| 470 | ref_frame.height() == dec_frame.height()) { |
| 471 | frame_stat->psnr = I420PSNR(&ref_frame, &dec_frame); |
| 472 | frame_stat->ssim = I420SSIM(&ref_frame, &dec_frame); |
| 473 | } else { |
| 474 | RTC_CHECK_GE(ref_frame.width(), dec_frame.width()); |
| 475 | RTC_CHECK_GE(ref_frame.height(), dec_frame.height()); |
| 476 | // Downscale reference frame. Use bilinear interpolation since it is used |
| 477 | // to get lowres inputs for encoder at simulcasting. |
| 478 | // TODO(ssilkin): Sync with VP9 SVC which uses 8-taps polyphase. |
| 479 | rtc::scoped_refptr<I420Buffer> scaled_buffer = |
| 480 | I420Buffer::Create(dec_frame.width(), dec_frame.height()); |
| 481 | const I420BufferInterface& ref_buffer = |
| 482 | *ref_frame.video_frame_buffer()->ToI420(); |
| 483 | I420Scale(ref_buffer.DataY(), ref_buffer.StrideY(), ref_buffer.DataU(), |
| 484 | ref_buffer.StrideU(), ref_buffer.DataV(), ref_buffer.StrideV(), |
| 485 | ref_buffer.width(), ref_buffer.height(), |
| 486 | scaled_buffer->MutableDataY(), scaled_buffer->StrideY(), |
| 487 | scaled_buffer->MutableDataU(), scaled_buffer->StrideU(), |
| 488 | scaled_buffer->MutableDataV(), scaled_buffer->StrideV(), |
| 489 | scaled_buffer->width(), scaled_buffer->height(), |
| 490 | libyuv::kFilterBilinear); |
| 491 | frame_stat->psnr = |
| 492 | I420PSNR(*scaled_buffer, *dec_frame.video_frame_buffer()->ToI420()); |
| 493 | frame_stat->ssim = |
| 494 | I420SSIM(*scaled_buffer, *dec_frame.video_frame_buffer()->ToI420()); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | void VideoProcessor::WriteDecodedFrameToFile(rtc::Buffer* buffer, |
| 499 | size_t simulcast_svc_idx) { |
| 500 | RTC_CHECK(simulcast_svc_idx < decoded_frame_writers_->size()); |
| 501 | RTC_DCHECK_EQ(buffer->size(), |
| 502 | decoded_frame_writers_->at(simulcast_svc_idx)->FrameLength()); |
| 503 | RTC_CHECK(decoded_frame_writers_->at(simulcast_svc_idx) |
| 504 | ->WriteFrame(buffer->data())); |
Åsa Persson | f0c4467 | 2017-10-24 16:03:39 +0200 | [diff] [blame] | 505 | } |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 506 | |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 507 | } // namespace test |
| 508 | } // namespace webrtc |