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" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "modules/video_coding/codecs/vp8/simulcast_rate_allocator.h" |
| 21 | #include "modules/video_coding/include/video_codec_initializer.h" |
| 22 | #include "modules/video_coding/utility/default_video_bitrate_allocator.h" |
| 23 | #include "rtc_base/checks.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "rtc_base/timeutils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "test/gtest.h" |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
| 28 | namespace test { |
| 29 | |
brandtr | b78bc75 | 2017-02-22 01:26:59 -0800 | [diff] [blame] | 30 | namespace { |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 31 | |
brandtr | bea36fd | 2017-08-07 03:36:54 -0700 | [diff] [blame] | 32 | const int kRtpClockRateHz = 90000; |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 33 | |
brandtr | aebc61e | 2017-02-28 07:13:47 -0800 | [diff] [blame] | 34 | std::unique_ptr<VideoBitrateAllocator> CreateBitrateAllocator( |
brandtr | 07734a5 | 2017-08-08 08:35:53 -0700 | [diff] [blame] | 35 | TestConfig* config) { |
brandtr | aebc61e | 2017-02-28 07:13:47 -0800 | [diff] [blame] | 36 | std::unique_ptr<TemporalLayersFactory> tl_factory; |
brandtr | 07734a5 | 2017-08-08 08:35:53 -0700 | [diff] [blame] | 37 | if (config->codec_settings.codecType == VideoCodecType::kVideoCodecVP8) { |
brandtr | aebc61e | 2017-02-28 07:13:47 -0800 | [diff] [blame] | 38 | tl_factory.reset(new TemporalLayersFactory()); |
brandtr | 07734a5 | 2017-08-08 08:35:53 -0700 | [diff] [blame] | 39 | config->codec_settings.VP8()->tl_factory = tl_factory.get(); |
brandtr | aebc61e | 2017-02-28 07:13:47 -0800 | [diff] [blame] | 40 | } |
| 41 | return std::unique_ptr<VideoBitrateAllocator>( |
brandtr | 07734a5 | 2017-08-08 08:35:53 -0700 | [diff] [blame] | 42 | VideoCodecInitializer::CreateBitrateAllocator(config->codec_settings, |
brandtr | aebc61e | 2017-02-28 07:13:47 -0800 | [diff] [blame] | 43 | std::move(tl_factory))); |
| 44 | } |
| 45 | |
ssilkin | 612f858 | 2017-09-28 09:23:17 -0700 | [diff] [blame] | 46 | rtc::Optional<size_t> GetMaxNaluLength(const EncodedImage& encoded_frame, |
| 47 | const TestConfig& config) { |
| 48 | if (config.codec_settings.codecType != kVideoCodecH264) |
| 49 | return rtc::Optional<size_t>(); |
| 50 | |
| 51 | std::vector<webrtc::H264::NaluIndex> nalu_indices = |
| 52 | webrtc::H264::FindNaluIndices(encoded_frame._buffer, |
| 53 | encoded_frame._length); |
| 54 | |
| 55 | RTC_CHECK(!nalu_indices.empty()); |
| 56 | |
| 57 | size_t max_length = 0; |
| 58 | for (const webrtc::H264::NaluIndex& index : nalu_indices) |
| 59 | max_length = std::max(max_length, index.payload_size); |
| 60 | |
| 61 | return rtc::Optional<size_t>(max_length); |
| 62 | } |
| 63 | |
asapersson | ae9ba04 | 2017-03-07 00:25:38 -0800 | [diff] [blame] | 64 | int GetElapsedTimeMicroseconds(int64_t start_ns, int64_t stop_ns) { |
| 65 | int64_t diff_us = (stop_ns - start_ns) / rtc::kNumNanosecsPerMicrosec; |
| 66 | RTC_DCHECK_GE(diff_us, std::numeric_limits<int>::min()); |
| 67 | RTC_DCHECK_LE(diff_us, std::numeric_limits<int>::max()); |
| 68 | return static_cast<int>(diff_us); |
| 69 | } |
| 70 | |
brandtr | b78bc75 | 2017-02-22 01:26:59 -0800 | [diff] [blame] | 71 | } // namespace |
| 72 | |
brandtr | c409552 | 2017-08-07 08:12:33 -0700 | [diff] [blame] | 73 | VideoProcessor::VideoProcessor(webrtc::VideoEncoder* encoder, |
| 74 | webrtc::VideoDecoder* decoder, |
| 75 | FrameReader* analysis_frame_reader, |
| 76 | FrameWriter* analysis_frame_writer, |
| 77 | PacketManipulator* packet_manipulator, |
| 78 | const TestConfig& config, |
| 79 | Stats* stats, |
brandtr | c409552 | 2017-08-07 08:12:33 -0700 | [diff] [blame] | 80 | IvfFileWriter* encoded_frame_writer, |
| 81 | FrameWriter* decoded_frame_writer) |
brandtr | c8c5905 | 2017-08-21 06:44:16 -0700 | [diff] [blame] | 82 | : initialized_(false), |
| 83 | config_(config), |
brandtr | 07734a5 | 2017-08-08 08:35:53 -0700 | [diff] [blame] | 84 | encoder_(encoder), |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 85 | decoder_(decoder), |
brandtr | 07734a5 | 2017-08-08 08:35:53 -0700 | [diff] [blame] | 86 | bitrate_allocator_(CreateBitrateAllocator(&config_)), |
brandtr | bdd555c | 2017-08-21 01:34:04 -0700 | [diff] [blame] | 87 | encode_callback_(this), |
| 88 | decode_callback_(this), |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 89 | packet_manipulator_(packet_manipulator), |
brandtr | aebc61e | 2017-02-28 07:13:47 -0800 | [diff] [blame] | 90 | analysis_frame_reader_(analysis_frame_reader), |
| 91 | analysis_frame_writer_(analysis_frame_writer), |
brandtr | b78bc75 | 2017-02-22 01:26:59 -0800 | [diff] [blame] | 92 | encoded_frame_writer_(encoded_frame_writer), |
| 93 | decoded_frame_writer_(decoded_frame_writer), |
brandtr | 8935d97 | 2017-09-06 01:53:22 -0700 | [diff] [blame] | 94 | last_inputed_frame_num_(-1), |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 95 | last_encoded_frame_num_(-1), |
| 96 | last_decoded_frame_num_(-1), |
| 97 | first_key_frame_has_been_excluded_(false), |
brandtr | bdd555c | 2017-08-21 01:34:04 -0700 | [diff] [blame] | 98 | last_decoded_frame_buffer_(analysis_frame_reader->FrameLength()), |
brandtr | aebc61e | 2017-02-28 07:13:47 -0800 | [diff] [blame] | 99 | stats_(stats), |
brandtr | b57f426 | 2017-08-30 06:29:51 -0700 | [diff] [blame] | 100 | rate_update_index_(-1) { |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 101 | RTC_DCHECK(encoder); |
| 102 | RTC_DCHECK(decoder); |
brandtr | aebc61e | 2017-02-28 07:13:47 -0800 | [diff] [blame] | 103 | RTC_DCHECK(packet_manipulator); |
brandtr | b78bc75 | 2017-02-22 01:26:59 -0800 | [diff] [blame] | 104 | RTC_DCHECK(analysis_frame_reader); |
| 105 | RTC_DCHECK(analysis_frame_writer); |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 106 | RTC_DCHECK(stats); |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 107 | } |
| 108 | |
brandtr | 77920a4 | 2017-08-11 07:48:15 -0700 | [diff] [blame] | 109 | VideoProcessor::~VideoProcessor() = default; |
brandtr | bea36fd | 2017-08-07 03:36:54 -0700 | [diff] [blame] | 110 | |
brandtr | c409552 | 2017-08-07 08:12:33 -0700 | [diff] [blame] | 111 | void VideoProcessor::Init() { |
brandtr | c8c5905 | 2017-08-21 06:44:16 -0700 | [diff] [blame] | 112 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
asapersson | 1d29c86 | 2017-06-22 02:18:50 -0700 | [diff] [blame] | 113 | RTC_DCHECK(!initialized_) << "VideoProcessor already initialized."; |
| 114 | initialized_ = true; |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 115 | |
brandtr | bea36fd | 2017-08-07 03:36:54 -0700 | [diff] [blame] | 116 | // Setup required callbacks for the encoder and decoder. |
brandtr | bdd555c | 2017-08-21 01:34:04 -0700 | [diff] [blame] | 117 | RTC_CHECK_EQ(encoder_->RegisterEncodeCompleteCallback(&encode_callback_), |
asapersson | 654d54c | 2017-02-10 00:16:07 -0800 | [diff] [blame] | 118 | WEBRTC_VIDEO_CODEC_OK) |
| 119 | << "Failed to register encode complete callback"; |
brandtr | bdd555c | 2017-08-21 01:34:04 -0700 | [diff] [blame] | 120 | RTC_CHECK_EQ(decoder_->RegisterDecodeCompleteCallback(&decode_callback_), |
asapersson | 654d54c | 2017-02-10 00:16:07 -0800 | [diff] [blame] | 121 | WEBRTC_VIDEO_CODEC_OK) |
| 122 | << "Failed to register decode complete callback"; |
| 123 | |
brandtr | aebc61e | 2017-02-28 07:13:47 -0800 | [diff] [blame] | 124 | // Initialize the encoder and decoder. |
asapersson | 654d54c | 2017-02-10 00:16:07 -0800 | [diff] [blame] | 125 | RTC_CHECK_EQ( |
Åsa Persson | 2d27fb5 | 2017-10-19 14:05:50 +0200 | [diff] [blame] | 126 | encoder_->InitEncode(&config_.codec_settings, config_.NumberOfCores(), |
asapersson | 654d54c | 2017-02-10 00:16:07 -0800 | [diff] [blame] | 127 | config_.networking_config.max_payload_size_in_bytes), |
| 128 | WEBRTC_VIDEO_CODEC_OK) |
| 129 | << "Failed to initialize VideoEncoder"; |
| 130 | |
Åsa Persson | 2d27fb5 | 2017-10-19 14:05:50 +0200 | [diff] [blame] | 131 | RTC_CHECK_EQ( |
| 132 | decoder_->InitDecode(&config_.codec_settings, config_.NumberOfCores()), |
| 133 | WEBRTC_VIDEO_CODEC_OK) |
asapersson | 654d54c | 2017-02-10 00:16:07 -0800 | [diff] [blame] | 134 | << "Failed to initialize VideoDecoder"; |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 135 | } |
| 136 | |
brandtr | 77920a4 | 2017-08-11 07:48:15 -0700 | [diff] [blame] | 137 | void VideoProcessor::Release() { |
brandtr | c8c5905 | 2017-08-21 06:44:16 -0700 | [diff] [blame] | 138 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
| 139 | |
brandtr | 77920a4 | 2017-08-11 07:48:15 -0700 | [diff] [blame] | 140 | RTC_CHECK_EQ(encoder_->Release(), WEBRTC_VIDEO_CODEC_OK); |
| 141 | RTC_CHECK_EQ(decoder_->Release(), WEBRTC_VIDEO_CODEC_OK); |
| 142 | |
brandtr | bdd555c | 2017-08-21 01:34:04 -0700 | [diff] [blame] | 143 | encoder_->RegisterEncodeCompleteCallback(nullptr); |
| 144 | decoder_->RegisterDecodeCompleteCallback(nullptr); |
| 145 | |
brandtr | 77920a4 | 2017-08-11 07:48:15 -0700 | [diff] [blame] | 146 | initialized_ = false; |
| 147 | } |
| 148 | |
brandtr | 8935d97 | 2017-09-06 01:53:22 -0700 | [diff] [blame] | 149 | void VideoProcessor::ProcessFrame() { |
brandtr | c8c5905 | 2017-08-21 06:44:16 -0700 | [diff] [blame] | 150 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
asapersson | 1d29c86 | 2017-06-22 02:18:50 -0700 | [diff] [blame] | 151 | RTC_DCHECK(initialized_) << "VideoProcessor not initialized."; |
brandtr | 8935d97 | 2017-09-06 01:53:22 -0700 | [diff] [blame] | 152 | ++last_inputed_frame_num_; |
asapersson | 654d54c | 2017-02-10 00:16:07 -0800 | [diff] [blame] | 153 | |
brandtr | bdd555c | 2017-08-21 01:34:04 -0700 | [diff] [blame] | 154 | // Get frame from file. |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 155 | rtc::scoped_refptr<I420BufferInterface> buffer( |
brandtr | b78bc75 | 2017-02-22 01:26:59 -0800 | [diff] [blame] | 156 | analysis_frame_reader_->ReadFrame()); |
brandtr | bdd555c | 2017-08-21 01:34:04 -0700 | [diff] [blame] | 157 | RTC_CHECK(buffer) << "Tried to read too many frames from the file."; |
brandtr | b57f426 | 2017-08-30 06:29:51 -0700 | [diff] [blame] | 158 | // Use the frame number as the basis for timestamp to identify frames. Let the |
| 159 | // first timestamp be non-zero, to not make the IvfFileWriter believe that we |
| 160 | // want to use capture timestamps in the IVF files. |
brandtr | 8935d97 | 2017-09-06 01:53:22 -0700 | [diff] [blame] | 161 | const uint32_t rtp_timestamp = (last_inputed_frame_num_ + 1) * |
| 162 | kRtpClockRateHz / |
brandtr | b57f426 | 2017-08-30 06:29:51 -0700 | [diff] [blame] | 163 | config_.codec_settings.maxFramerate; |
brandtr | 8935d97 | 2017-09-06 01:53:22 -0700 | [diff] [blame] | 164 | rtp_timestamp_to_frame_num_[rtp_timestamp] = last_inputed_frame_num_; |
brandtr | bdd555c | 2017-08-21 01:34:04 -0700 | [diff] [blame] | 165 | const int64_t kNoRenderTime = 0; |
brandtr | b57f426 | 2017-08-30 06:29:51 -0700 | [diff] [blame] | 166 | VideoFrame source_frame(buffer, rtp_timestamp, kNoRenderTime, |
| 167 | webrtc::kVideoRotation_0); |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 168 | |
| 169 | // Decide if we are going to force a keyframe. |
| 170 | std::vector<FrameType> frame_types(1, kVideoFrameDelta); |
| 171 | if (config_.keyframe_interval > 0 && |
brandtr | 8935d97 | 2017-09-06 01:53:22 -0700 | [diff] [blame] | 172 | last_inputed_frame_num_ % config_.keyframe_interval == 0) { |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 173 | frame_types[0] = kVideoFrameKey; |
| 174 | } |
| 175 | |
| 176 | // Create frame statistics object used for aggregation at end of test run. |
brandtr | 8935d97 | 2017-09-06 01:53:22 -0700 | [diff] [blame] | 177 | FrameStatistic* frame_stat = stats_->AddFrame(); |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 178 | |
| 179 | // For the highest measurement accuracy of the encode time, the start/stop |
| 180 | // time recordings should wrap the Encode call as tightly as possible. |
brandtr | 8935d97 | 2017-09-06 01:53:22 -0700 | [diff] [blame] | 181 | frame_stat->encode_start_ns = rtc::TimeNanos(); |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 182 | frame_stat->encode_return_code = |
| 183 | encoder_->Encode(source_frame, nullptr, &frame_types); |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 184 | } |
| 185 | |
brandtr | bdd555c | 2017-08-21 01:34:04 -0700 | [diff] [blame] | 186 | void VideoProcessor::SetRates(int bitrate_kbps, int framerate_fps) { |
brandtr | c8c5905 | 2017-08-21 06:44:16 -0700 | [diff] [blame] | 187 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
brandtr | bdd555c | 2017-08-21 01:34:04 -0700 | [diff] [blame] | 188 | config_.codec_settings.maxFramerate = framerate_fps; |
brandtr | bea36fd | 2017-08-07 03:36:54 -0700 | [diff] [blame] | 189 | int set_rates_result = encoder_->SetRateAllocation( |
brandtr | bdd555c | 2017-08-21 01:34:04 -0700 | [diff] [blame] | 190 | bitrate_allocator_->GetAllocation(bitrate_kbps * 1000, framerate_fps), |
| 191 | framerate_fps); |
brandtr | bea36fd | 2017-08-07 03:36:54 -0700 | [diff] [blame] | 192 | RTC_DCHECK_GE(set_rates_result, 0) |
brandtr | bdd555c | 2017-08-21 01:34:04 -0700 | [diff] [blame] | 193 | << "Failed to update encoder with new rate " << bitrate_kbps << "."; |
brandtr | b57f426 | 2017-08-30 06:29:51 -0700 | [diff] [blame] | 194 | ++rate_update_index_; |
| 195 | num_dropped_frames_.push_back(0); |
| 196 | num_spatial_resizes_.push_back(0); |
brandtr | bea36fd | 2017-08-07 03:36:54 -0700 | [diff] [blame] | 197 | } |
| 198 | |
brandtr | b57f426 | 2017-08-30 06:29:51 -0700 | [diff] [blame] | 199 | std::vector<int> VideoProcessor::NumberDroppedFramesPerRateUpdate() const { |
brandtr | c8c5905 | 2017-08-21 06:44:16 -0700 | [diff] [blame] | 200 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
brandtr | bea36fd | 2017-08-07 03:36:54 -0700 | [diff] [blame] | 201 | return num_dropped_frames_; |
| 202 | } |
| 203 | |
brandtr | b57f426 | 2017-08-30 06:29:51 -0700 | [diff] [blame] | 204 | std::vector<int> VideoProcessor::NumberSpatialResizesPerRateUpdate() const { |
brandtr | c8c5905 | 2017-08-21 06:44:16 -0700 | [diff] [blame] | 205 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
brandtr | bea36fd | 2017-08-07 03:36:54 -0700 | [diff] [blame] | 206 | return num_spatial_resizes_; |
| 207 | } |
| 208 | |
brandtr | 4553562 | 2017-08-22 03:33:11 -0700 | [diff] [blame] | 209 | void VideoProcessor::FrameEncoded(webrtc::VideoCodecType codec, |
| 210 | const EncodedImage& encoded_image) { |
brandtr | c8c5905 | 2017-08-21 06:44:16 -0700 | [diff] [blame] | 211 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
| 212 | |
brandtr | 32e0d26 | 2017-02-15 05:29:38 -0800 | [diff] [blame] | 213 | // For the highest measurement accuracy of the encode time, the start/stop |
| 214 | // time recordings should wrap the Encode call as tightly as possible. |
| 215 | int64_t encode_stop_ns = rtc::TimeNanos(); |
| 216 | |
Rasmus Brandt | f7a3558 | 2017-10-24 10:16:33 +0200 | [diff] [blame^] | 217 | if (config_.encoded_frame_checker) { |
| 218 | config_.encoded_frame_checker->CheckEncodedFrame(codec, encoded_image); |
| 219 | } |
brandtr | b78bc75 | 2017-02-22 01:26:59 -0800 | [diff] [blame] | 220 | |
brandtr | b57f426 | 2017-08-30 06:29:51 -0700 | [diff] [blame] | 221 | // Check for dropped frames. |
| 222 | const int frame_number = |
| 223 | rtp_timestamp_to_frame_num_[encoded_image._timeStamp]; |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 224 | bool last_frame_missing = false; |
| 225 | if (frame_number > 0) { |
| 226 | RTC_DCHECK_GE(last_encoded_frame_num_, 0); |
| 227 | int num_dropped_from_last_encode = |
| 228 | frame_number - last_encoded_frame_num_ - 1; |
| 229 | RTC_DCHECK_GE(num_dropped_from_last_encode, 0); |
brandtr | b57f426 | 2017-08-30 06:29:51 -0700 | [diff] [blame] | 230 | RTC_CHECK_GE(rate_update_index_, 0); |
| 231 | num_dropped_frames_[rate_update_index_] += num_dropped_from_last_encode; |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 232 | if (num_dropped_from_last_encode > 0) { |
| 233 | // For dropped frames, we write out the last decoded frame to avoid |
| 234 | // getting out of sync for the computation of PSNR and SSIM. |
| 235 | for (int i = 0; i < num_dropped_from_last_encode; i++) { |
| 236 | RTC_DCHECK_EQ(last_decoded_frame_buffer_.size(), |
| 237 | analysis_frame_writer_->FrameLength()); |
| 238 | RTC_CHECK(analysis_frame_writer_->WriteFrame( |
| 239 | last_decoded_frame_buffer_.data())); |
| 240 | if (decoded_frame_writer_) { |
| 241 | RTC_DCHECK_EQ(last_decoded_frame_buffer_.size(), |
| 242 | decoded_frame_writer_->FrameLength()); |
| 243 | RTC_CHECK(decoded_frame_writer_->WriteFrame( |
| 244 | last_decoded_frame_buffer_.data())); |
| 245 | } |
brandtr | b78bc75 | 2017-02-22 01:26:59 -0800 | [diff] [blame] | 246 | } |
marpan@webrtc.org | f4c2de9 | 2012-06-05 21:07:28 +0000 | [diff] [blame] | 247 | } |
brandtr | 8935d97 | 2017-09-06 01:53:22 -0700 | [diff] [blame] | 248 | const FrameStatistic* last_encoded_frame_stat = |
| 249 | stats_->GetFrame(last_encoded_frame_num_); |
| 250 | last_frame_missing = (last_encoded_frame_stat->manipulated_length == 0); |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 251 | } |
| 252 | // Ensure strict monotonicity. |
| 253 | RTC_CHECK_GT(frame_number, last_encoded_frame_num_); |
| 254 | last_encoded_frame_num_ = frame_number; |
| 255 | |
brandtr | 8935d97 | 2017-09-06 01:53:22 -0700 | [diff] [blame] | 256 | // Update frame statistics. |
| 257 | FrameStatistic* frame_stat = stats_->GetFrame(frame_number); |
| 258 | frame_stat->encode_time_us = |
| 259 | GetElapsedTimeMicroseconds(frame_stat->encode_start_ns, encode_stop_ns); |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 260 | frame_stat->encoding_successful = true; |
brandtr | 8935d97 | 2017-09-06 01:53:22 -0700 | [diff] [blame] | 261 | frame_stat->encoded_frame_size_bytes = encoded_image._length; |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 262 | frame_stat->frame_type = encoded_image._frameType; |
| 263 | frame_stat->qp = encoded_image.qp_; |
brandtr | 8935d97 | 2017-09-06 01:53:22 -0700 | [diff] [blame] | 264 | frame_stat->bitrate_kbps = static_cast<int>( |
brandtr | 07734a5 | 2017-08-08 08:35:53 -0700 | [diff] [blame] | 265 | encoded_image._length * config_.codec_settings.maxFramerate * 8 / 1000); |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 266 | frame_stat->total_packets = |
philipel | cce46fc | 2015-12-21 03:04:49 -0800 | [diff] [blame] | 267 | encoded_image._length / config_.networking_config.packet_size_in_bytes + |
| 268 | 1; |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 269 | |
ssilkin | 612f858 | 2017-09-28 09:23:17 -0700 | [diff] [blame] | 270 | frame_stat->max_nalu_length = GetMaxNaluLength(encoded_image, config_); |
| 271 | |
brandtr | 8bc9385 | 2017-02-15 05:19:51 -0800 | [diff] [blame] | 272 | // Simulate packet loss. |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 273 | bool exclude_this_frame = false; |
Peter Boström | 49e196a | 2015-10-23 15:58:18 +0200 | [diff] [blame] | 274 | if (encoded_image._frameType == kVideoFrameKey) { |
brandtr | aebc61e | 2017-02-28 07:13:47 -0800 | [diff] [blame] | 275 | // Only keyframes can be excluded. |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 276 | switch (config_.exclude_frame_types) { |
| 277 | case kExcludeOnlyFirstKeyFrame: |
| 278 | if (!first_key_frame_has_been_excluded_) { |
| 279 | first_key_frame_has_been_excluded_ = true; |
| 280 | exclude_this_frame = true; |
| 281 | } |
| 282 | break; |
| 283 | case kExcludeAllKeyFrames: |
| 284 | exclude_this_frame = true; |
| 285 | break; |
| 286 | default: |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 287 | RTC_NOTREACHED(); |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 288 | } |
| 289 | } |
hbos | bab934b | 2016-01-27 01:36:03 -0800 | [diff] [blame] | 290 | |
| 291 | // Make a raw copy of the |encoded_image| buffer. |
hbos | 3fe2c6a | 2016-01-22 00:07:12 -0800 | [diff] [blame] | 292 | size_t copied_buffer_size = encoded_image._length + |
| 293 | EncodedImage::GetBufferPaddingBytes(codec); |
kwiberg | 3f55dea | 2016-02-29 05:51:59 -0800 | [diff] [blame] | 294 | std::unique_ptr<uint8_t[]> copied_buffer(new uint8_t[copied_buffer_size]); |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 295 | memcpy(copied_buffer.get(), encoded_image._buffer, encoded_image._length); |
hbos | bab934b | 2016-01-27 01:36:03 -0800 | [diff] [blame] | 296 | // The image to feed to the decoder. |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 297 | EncodedImage copied_image; |
| 298 | memcpy(&copied_image, &encoded_image, sizeof(copied_image)); |
hbos | 3fe2c6a | 2016-01-22 00:07:12 -0800 | [diff] [blame] | 299 | copied_image._size = copied_buffer_size; |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 300 | copied_image._buffer = copied_buffer.get(); |
hbos | bab934b | 2016-01-27 01:36:03 -0800 | [diff] [blame] | 301 | |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 302 | if (!exclude_this_frame) { |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 303 | frame_stat->packets_dropped = |
philipel | cce46fc | 2015-12-21 03:04:49 -0800 | [diff] [blame] | 304 | packet_manipulator_->ManipulatePackets(&copied_image); |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 305 | } |
brandtr | 8935d97 | 2017-09-06 01:53:22 -0700 | [diff] [blame] | 306 | frame_stat->manipulated_length = copied_image._length; |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 307 | |
brandtr | 32e0d26 | 2017-02-15 05:29:38 -0800 | [diff] [blame] | 308 | // For the highest measurement accuracy of the decode time, the start/stop |
| 309 | // time recordings should wrap the Decode call as tightly as possible. |
brandtr | 8935d97 | 2017-09-06 01:53:22 -0700 | [diff] [blame] | 310 | frame_stat->decode_start_ns = rtc::TimeNanos(); |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 311 | frame_stat->decode_return_code = |
| 312 | decoder_->Decode(copied_image, last_frame_missing, nullptr); |
brandtr | 8bc9385 | 2017-02-15 05:19:51 -0800 | [diff] [blame] | 313 | |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 314 | if (frame_stat->decode_return_code != WEBRTC_VIDEO_CODEC_OK) { |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 315 | // Write the last successful frame the output file to avoid getting it out |
brandtr | 8bc9385 | 2017-02-15 05:19:51 -0800 | [diff] [blame] | 316 | // of sync with the source file for SSIM and PSNR comparisons. |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 317 | RTC_DCHECK_EQ(last_decoded_frame_buffer_.size(), |
| 318 | analysis_frame_writer_->FrameLength()); |
| 319 | RTC_CHECK( |
| 320 | analysis_frame_writer_->WriteFrame(last_decoded_frame_buffer_.data())); |
brandtr | b78bc75 | 2017-02-22 01:26:59 -0800 | [diff] [blame] | 321 | if (decoded_frame_writer_) { |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 322 | RTC_DCHECK_EQ(last_decoded_frame_buffer_.size(), |
| 323 | decoded_frame_writer_->FrameLength()); |
| 324 | RTC_CHECK( |
| 325 | decoded_frame_writer_->WriteFrame(last_decoded_frame_buffer_.data())); |
brandtr | b78bc75 | 2017-02-22 01:26:59 -0800 | [diff] [blame] | 326 | } |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 327 | } |
brandtr | 8935d97 | 2017-09-06 01:53:22 -0700 | [diff] [blame] | 328 | |
| 329 | if (encoded_frame_writer_) { |
| 330 | RTC_CHECK(encoded_frame_writer_->WriteFrame(encoded_image, codec)); |
| 331 | } |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 332 | } |
| 333 | |
brandtr | c409552 | 2017-08-07 08:12:33 -0700 | [diff] [blame] | 334 | void VideoProcessor::FrameDecoded(const VideoFrame& image) { |
brandtr | c8c5905 | 2017-08-21 06:44:16 -0700 | [diff] [blame] | 335 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
| 336 | |
brandtr | 32e0d26 | 2017-02-15 05:29:38 -0800 | [diff] [blame] | 337 | // For the highest measurement accuracy of the decode time, the start/stop |
| 338 | // time recordings should wrap the Decode call as tightly as possible. |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 339 | int64_t decode_stop_ns = rtc::TimeNanos(); |
brandtr | 8bc9385 | 2017-02-15 05:19:51 -0800 | [diff] [blame] | 340 | |
brandtr | 8935d97 | 2017-09-06 01:53:22 -0700 | [diff] [blame] | 341 | // Update frame statistics. |
brandtr | b57f426 | 2017-08-30 06:29:51 -0700 | [diff] [blame] | 342 | const int frame_number = rtp_timestamp_to_frame_num_[image.timestamp()]; |
brandtr | 8935d97 | 2017-09-06 01:53:22 -0700 | [diff] [blame] | 343 | FrameStatistic* frame_stat = stats_->GetFrame(frame_number); |
| 344 | frame_stat->decoded_width = image.width(); |
| 345 | frame_stat->decoded_height = image.height(); |
| 346 | frame_stat->decode_time_us = |
| 347 | GetElapsedTimeMicroseconds(frame_stat->decode_start_ns, decode_stop_ns); |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 348 | frame_stat->decoding_successful = true; |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 349 | |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 350 | // Check if the codecs have resized the frame since previously decoded frame. |
| 351 | if (frame_number > 0) { |
brandtr | b57f426 | 2017-08-30 06:29:51 -0700 | [diff] [blame] | 352 | RTC_CHECK_GE(last_decoded_frame_num_, 0); |
brandtr | 8935d97 | 2017-09-06 01:53:22 -0700 | [diff] [blame] | 353 | const FrameStatistic* last_decoded_frame_stat = |
| 354 | stats_->GetFrame(last_decoded_frame_num_); |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 355 | if (static_cast<int>(image.width()) != |
brandtr | 8935d97 | 2017-09-06 01:53:22 -0700 | [diff] [blame] | 356 | last_decoded_frame_stat->decoded_width || |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 357 | static_cast<int>(image.height()) != |
brandtr | 8935d97 | 2017-09-06 01:53:22 -0700 | [diff] [blame] | 358 | last_decoded_frame_stat->decoded_height) { |
brandtr | b57f426 | 2017-08-30 06:29:51 -0700 | [diff] [blame] | 359 | RTC_CHECK_GE(rate_update_index_, 0); |
| 360 | ++num_spatial_resizes_[rate_update_index_]; |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 361 | } |
marpan@webrtc.org | f4c2de9 | 2012-06-05 21:07:28 +0000 | [diff] [blame] | 362 | } |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 363 | // Ensure strict monotonicity. |
| 364 | RTC_CHECK_GT(frame_number, last_decoded_frame_num_); |
| 365 | last_decoded_frame_num_ = frame_number; |
| 366 | |
Åsa Persson | 2d27fb5 | 2017-10-19 14:05:50 +0200 | [diff] [blame] | 367 | // Check if frame size is different from the original size, and if so, scale |
| 368 | // back to original size. This is needed for the PSNR and SSIM calculations. |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 369 | size_t extracted_length; |
| 370 | rtc::Buffer extracted_buffer; |
brandtr | 07734a5 | 2017-08-08 08:35:53 -0700 | [diff] [blame] | 371 | if (image.width() != config_.codec_settings.width || |
| 372 | image.height() != config_.codec_settings.height) { |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 373 | rtc::scoped_refptr<I420Buffer> scaled_buffer(I420Buffer::Create( |
brandtr | 07734a5 | 2017-08-08 08:35:53 -0700 | [diff] [blame] | 374 | config_.codec_settings.width, config_.codec_settings.height)); |
Niels Möller | 718a763 | 2016-06-13 13:06:01 +0200 | [diff] [blame] | 375 | // Should be the same aspect ratio, no cropping needed. |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 376 | scaled_buffer->ScaleFrom(*image.video_frame_buffer()->ToI420()); |
Niels Möller | 718a763 | 2016-06-13 13:06:01 +0200 | [diff] [blame] | 377 | |
nisse | eb44b39 | 2017-04-28 07:18:05 -0700 | [diff] [blame] | 378 | size_t length = CalcBufferSize(VideoType::kI420, scaled_buffer->width(), |
| 379 | scaled_buffer->height()); |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 380 | extracted_buffer.SetSize(length); |
| 381 | extracted_length = |
| 382 | ExtractBuffer(scaled_buffer, length, extracted_buffer.data()); |
| 383 | } else { |
| 384 | // No resize. |
nisse | eb44b39 | 2017-04-28 07:18:05 -0700 | [diff] [blame] | 385 | size_t length = |
| 386 | CalcBufferSize(VideoType::kI420, image.width(), image.height()); |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 387 | extracted_buffer.SetSize(length); |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 388 | extracted_length = ExtractBuffer(image.video_frame_buffer()->ToI420(), |
| 389 | length, extracted_buffer.data()); |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 390 | } |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 391 | |
| 392 | RTC_DCHECK_EQ(extracted_length, analysis_frame_writer_->FrameLength()); |
| 393 | RTC_CHECK(analysis_frame_writer_->WriteFrame(extracted_buffer.data())); |
| 394 | if (decoded_frame_writer_) { |
| 395 | RTC_DCHECK_EQ(extracted_length, decoded_frame_writer_->FrameLength()); |
| 396 | RTC_CHECK(decoded_frame_writer_->WriteFrame(extracted_buffer.data())); |
| 397 | } |
| 398 | |
| 399 | last_decoded_frame_buffer_ = std::move(extracted_buffer); |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 400 | } |
| 401 | |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 402 | } // namespace test |
| 403 | } // namespace webrtc |