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