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 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 13 | #include <string.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 14 | |
ssilkin | 612f858 | 2017-09-28 09:23:17 -0700 | [diff] [blame] | 15 | #include <algorithm> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 16 | #include <cstddef> |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 17 | #include <limits> |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 18 | #include <utility> |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 19 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 20 | #include "absl/memory/memory.h" |
Mirko Bonadei | d970807 | 2019-01-25 20:26:48 +0100 | [diff] [blame] | 21 | #include "api/scoped_refptr.h" |
Jiawei Ou | c2ebe21 | 2018-11-08 10:02:56 -0800 | [diff] [blame] | 22 | #include "api/video/builtin_video_bitrate_allocator_factory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "api/video/i420_buffer.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 24 | #include "api/video/video_bitrate_allocator_factory.h" |
| 25 | #include "api/video/video_frame_buffer.h" |
| 26 | #include "api/video/video_rotation.h" |
| 27 | #include "api/video_codecs/video_codec.h" |
Elad Alon | 370f93a | 2019-06-11 14:57:57 +0200 | [diff] [blame] | 28 | #include "api/video_codecs/video_encoder.h" |
ssilkin | 612f858 | 2017-09-28 09:23:17 -0700 | [diff] [blame] | 29 | #include "common_video/h264/h264_common.h" |
Rasmus Brandt | 5f7a891 | 2018-02-28 17:17:15 +0100 | [diff] [blame] | 30 | #include "common_video/libyuv/include/webrtc_libyuv.h" |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 31 | #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 32 | #include "modules/video_coding/codecs/interface/common_constants.h" |
Rasmus Brandt | d00c895 | 2018-03-14 12:29:57 +0100 | [diff] [blame] | 33 | #include "modules/video_coding/include/video_error_codes.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 34 | #include "rtc_base/checks.h" |
Danil Chapovalov | ad89528 | 2019-03-11 10:28:05 +0000 | [diff] [blame] | 35 | #include "rtc_base/task_utils/to_queued_task.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 36 | #include "rtc_base/time_utils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 37 | #include "test/gtest.h" |
Sergey Silkin | 8d3758e | 2018-03-14 11:28:15 +0100 | [diff] [blame] | 38 | #include "third_party/libyuv/include/libyuv/compare.h" |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 39 | #include "third_party/libyuv/include/libyuv/scale.h" |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 40 | |
| 41 | namespace webrtc { |
| 42 | namespace test { |
| 43 | |
Kári Tristan Helgason | 169005d | 2018-05-22 13:34:14 +0200 | [diff] [blame] | 44 | using FrameStatistics = VideoCodecTestStats::FrameStatistics; |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 45 | |
Kári Tristan Helgason | 169005d | 2018-05-22 13:34:14 +0200 | [diff] [blame] | 46 | namespace { |
Åsa Persson | 91af24a | 2018-01-24 17:20:18 +0100 | [diff] [blame] | 47 | const int kMsToRtpTimestamp = kVideoPayloadTypeFrequency / 1000; |
Sami Kalliomäki | c75a5e8 | 2018-07-09 13:27:42 +0200 | [diff] [blame] | 48 | const int kMaxBufferedInputFrames = 20; |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 49 | |
Elad Alon | 370f93a | 2019-06-11 14:57:57 +0200 | [diff] [blame] | 50 | const VideoEncoder::Capabilities kCapabilities(false); |
| 51 | |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 52 | size_t GetMaxNaluSizeBytes(const EncodedImage& encoded_frame, |
Kári Tristan Helgason | 169005d | 2018-05-22 13:34:14 +0200 | [diff] [blame] | 53 | const VideoCodecTestFixture::Config& config) { |
ssilkin | 612f858 | 2017-09-28 09:23:17 -0700 | [diff] [blame] | 54 | if (config.codec_settings.codecType != kVideoCodecH264) |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 55 | return 0; |
ssilkin | 612f858 | 2017-09-28 09:23:17 -0700 | [diff] [blame] | 56 | |
| 57 | std::vector<webrtc::H264::NaluIndex> nalu_indices = |
Niels Möller | 77536a2 | 2019-01-15 08:50:01 +0100 | [diff] [blame] | 58 | webrtc::H264::FindNaluIndices(encoded_frame.data(), encoded_frame.size()); |
ssilkin | 612f858 | 2017-09-28 09:23:17 -0700 | [diff] [blame] | 59 | |
| 60 | RTC_CHECK(!nalu_indices.empty()); |
| 61 | |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 62 | size_t max_size = 0; |
ssilkin | 612f858 | 2017-09-28 09:23:17 -0700 | [diff] [blame] | 63 | for (const webrtc::H264::NaluIndex& index : nalu_indices) |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 64 | max_size = std::max(max_size, index.payload_size); |
ssilkin | 612f858 | 2017-09-28 09:23:17 -0700 | [diff] [blame] | 65 | |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 66 | return max_size; |
ssilkin | 612f858 | 2017-09-28 09:23:17 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Niels Möller | d3b8c63 | 2018-08-27 15:33:42 +0200 | [diff] [blame] | 69 | size_t GetTemporalLayerIndex(const CodecSpecificInfo& codec_specific) { |
| 70 | size_t temporal_idx = 0; |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 71 | if (codec_specific.codecType == kVideoCodecVP8) { |
Niels Möller | d3b8c63 | 2018-08-27 15:33:42 +0200 | [diff] [blame] | 72 | temporal_idx = codec_specific.codecSpecific.VP8.temporalIdx; |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 73 | } else if (codec_specific.codecType == kVideoCodecVP9) { |
Niels Möller | d3b8c63 | 2018-08-27 15:33:42 +0200 | [diff] [blame] | 74 | temporal_idx = codec_specific.codecSpecific.VP9.temporal_idx; |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 75 | } |
Niels Möller | d3b8c63 | 2018-08-27 15:33:42 +0200 | [diff] [blame] | 76 | if (temporal_idx == kNoTemporalIdx) { |
| 77 | temporal_idx = 0; |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 78 | } |
Niels Möller | d3b8c63 | 2018-08-27 15:33:42 +0200 | [diff] [blame] | 79 | return temporal_idx; |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 80 | } |
| 81 | |
asapersson | ae9ba04 | 2017-03-07 00:25:38 -0800 | [diff] [blame] | 82 | int GetElapsedTimeMicroseconds(int64_t start_ns, int64_t stop_ns) { |
| 83 | int64_t diff_us = (stop_ns - start_ns) / rtc::kNumNanosecsPerMicrosec; |
| 84 | RTC_DCHECK_GE(diff_us, std::numeric_limits<int>::min()); |
| 85 | RTC_DCHECK_LE(diff_us, std::numeric_limits<int>::max()); |
| 86 | return static_cast<int>(diff_us); |
| 87 | } |
| 88 | |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 89 | void ExtractI420BufferWithSize(const VideoFrame& image, |
| 90 | int width, |
| 91 | int height, |
| 92 | rtc::Buffer* buffer) { |
Åsa Persson | f0c4467 | 2017-10-24 16:03:39 +0200 | [diff] [blame] | 93 | if (image.width() != width || image.height() != height) { |
| 94 | EXPECT_DOUBLE_EQ(static_cast<double>(width) / height, |
| 95 | static_cast<double>(image.width()) / image.height()); |
| 96 | // Same aspect ratio, no cropping needed. |
| 97 | rtc::scoped_refptr<I420Buffer> scaled(I420Buffer::Create(width, height)); |
| 98 | scaled->ScaleFrom(*image.video_frame_buffer()->ToI420()); |
| 99 | |
| 100 | size_t length = |
| 101 | CalcBufferSize(VideoType::kI420, scaled->width(), scaled->height()); |
| 102 | buffer->SetSize(length); |
| 103 | RTC_CHECK_NE(ExtractBuffer(scaled, length, buffer->data()), -1); |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | // No resize. |
| 108 | size_t length = |
| 109 | CalcBufferSize(VideoType::kI420, image.width(), image.height()); |
| 110 | buffer->SetSize(length); |
| 111 | RTC_CHECK_NE(ExtractBuffer(image, length, buffer->data()), -1); |
| 112 | } |
| 113 | |
Sergey Silkin | 8d3758e | 2018-03-14 11:28:15 +0100 | [diff] [blame] | 114 | void CalculateFrameQuality(const I420BufferInterface& ref_buffer, |
| 115 | const I420BufferInterface& dec_buffer, |
Sergey Silkin | 6e1402b | 2019-02-13 09:33:00 +0100 | [diff] [blame] | 116 | FrameStatistics* frame_stat, |
| 117 | bool calc_ssim) { |
Sergey Silkin | 8d3758e | 2018-03-14 11:28:15 +0100 | [diff] [blame] | 118 | if (ref_buffer.width() != dec_buffer.width() || |
| 119 | ref_buffer.height() != dec_buffer.height()) { |
| 120 | RTC_CHECK_GE(ref_buffer.width(), dec_buffer.width()); |
| 121 | RTC_CHECK_GE(ref_buffer.height(), dec_buffer.height()); |
| 122 | // Downscale reference frame. |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 123 | rtc::scoped_refptr<I420Buffer> scaled_buffer = |
Sergey Silkin | 8d3758e | 2018-03-14 11:28:15 +0100 | [diff] [blame] | 124 | I420Buffer::Create(dec_buffer.width(), dec_buffer.height()); |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 125 | I420Scale(ref_buffer.DataY(), ref_buffer.StrideY(), ref_buffer.DataU(), |
| 126 | ref_buffer.StrideU(), ref_buffer.DataV(), ref_buffer.StrideV(), |
| 127 | ref_buffer.width(), ref_buffer.height(), |
| 128 | scaled_buffer->MutableDataY(), scaled_buffer->StrideY(), |
| 129 | scaled_buffer->MutableDataU(), scaled_buffer->StrideU(), |
| 130 | scaled_buffer->MutableDataV(), scaled_buffer->StrideV(), |
| 131 | scaled_buffer->width(), scaled_buffer->height(), |
| 132 | libyuv::kFilterBox); |
Sergey Silkin | 8d3758e | 2018-03-14 11:28:15 +0100 | [diff] [blame] | 133 | |
Sergey Silkin | 6e1402b | 2019-02-13 09:33:00 +0100 | [diff] [blame] | 134 | CalculateFrameQuality(*scaled_buffer, dec_buffer, frame_stat, calc_ssim); |
Sergey Silkin | 8d3758e | 2018-03-14 11:28:15 +0100 | [diff] [blame] | 135 | } else { |
| 136 | const uint64_t sse_y = libyuv::ComputeSumSquareErrorPlane( |
| 137 | dec_buffer.DataY(), dec_buffer.StrideY(), ref_buffer.DataY(), |
| 138 | ref_buffer.StrideY(), dec_buffer.width(), dec_buffer.height()); |
| 139 | |
| 140 | const uint64_t sse_u = libyuv::ComputeSumSquareErrorPlane( |
| 141 | dec_buffer.DataU(), dec_buffer.StrideU(), ref_buffer.DataU(), |
| 142 | ref_buffer.StrideU(), dec_buffer.width() / 2, dec_buffer.height() / 2); |
| 143 | |
| 144 | const uint64_t sse_v = libyuv::ComputeSumSquareErrorPlane( |
| 145 | dec_buffer.DataV(), dec_buffer.StrideV(), ref_buffer.DataV(), |
| 146 | ref_buffer.StrideV(), dec_buffer.width() / 2, dec_buffer.height() / 2); |
| 147 | |
| 148 | const size_t num_y_samples = dec_buffer.width() * dec_buffer.height(); |
| 149 | const size_t num_u_samples = |
| 150 | dec_buffer.width() / 2 * dec_buffer.height() / 2; |
| 151 | |
| 152 | frame_stat->psnr_y = libyuv::SumSquareErrorToPsnr(sse_y, num_y_samples); |
| 153 | frame_stat->psnr_u = libyuv::SumSquareErrorToPsnr(sse_u, num_u_samples); |
| 154 | frame_stat->psnr_v = libyuv::SumSquareErrorToPsnr(sse_v, num_u_samples); |
| 155 | frame_stat->psnr = libyuv::SumSquareErrorToPsnr( |
| 156 | sse_y + sse_u + sse_v, num_y_samples + 2 * num_u_samples); |
Sergey Silkin | 6e1402b | 2019-02-13 09:33:00 +0100 | [diff] [blame] | 157 | |
| 158 | if (calc_ssim) { |
| 159 | frame_stat->ssim = I420SSIM(ref_buffer, dec_buffer); |
| 160 | } |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
brandtr | b78bc75 | 2017-02-22 01:26:59 -0800 | [diff] [blame] | 164 | } // namespace |
| 165 | |
brandtr | c409552 | 2017-08-07 08:12:33 -0700 | [diff] [blame] | 166 | VideoProcessor::VideoProcessor(webrtc::VideoEncoder* encoder, |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 167 | VideoDecoderList* decoders, |
| 168 | FrameReader* input_frame_reader, |
Kári Tristan Helgason | 169005d | 2018-05-22 13:34:14 +0200 | [diff] [blame] | 169 | const VideoCodecTestFixture::Config& config, |
Rasmus Brandt | 7d72d0f | 2019-03-26 16:56:14 +0100 | [diff] [blame] | 170 | VideoCodecTestStatsImpl* stats, |
Rasmus Brandt | 001c782 | 2019-03-22 13:41:48 +0100 | [diff] [blame] | 171 | IvfFileWriterMap* encoded_frame_writers, |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 172 | FrameWriterList* decoded_frame_writers) |
Åsa Persson | f0c4467 | 2017-10-24 16:03:39 +0200 | [diff] [blame] | 173 | : config_(config), |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 174 | num_simulcast_or_spatial_layers_( |
| 175 | std::max(config_.NumberOfSimulcastStreams(), |
| 176 | config_.NumberOfSpatialLayers())), |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 177 | stats_(stats), |
brandtr | 07734a5 | 2017-08-08 08:35:53 -0700 | [diff] [blame] | 178 | encoder_(encoder), |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 179 | decoders_(decoders), |
Jiawei Ou | c2ebe21 | 2018-11-08 10:02:56 -0800 | [diff] [blame] | 180 | bitrate_allocator_( |
| 181 | CreateBuiltinVideoBitrateAllocatorFactory() |
| 182 | ->CreateVideoBitrateAllocator(config_.codec_settings)), |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 183 | framerate_fps_(0), |
brandtr | bdd555c | 2017-08-21 01:34:04 -0700 | [diff] [blame] | 184 | encode_callback_(this), |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 185 | input_frame_reader_(input_frame_reader), |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 186 | merged_encoded_frames_(num_simulcast_or_spatial_layers_), |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 187 | encoded_frame_writers_(encoded_frame_writers), |
| 188 | decoded_frame_writers_(decoded_frame_writers), |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 189 | last_inputed_frame_num_(0), |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 190 | last_inputed_timestamp_(0), |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 191 | first_encoded_frame_(num_simulcast_or_spatial_layers_, true), |
| 192 | last_encoded_frame_num_(num_simulcast_or_spatial_layers_), |
| 193 | first_decoded_frame_(num_simulcast_or_spatial_layers_, true), |
Sergey Silkin | c89eed9 | 2018-04-01 23:57:51 +0200 | [diff] [blame] | 194 | last_decoded_frame_num_(num_simulcast_or_spatial_layers_), |
Sergey Silkin | 5613879 | 2018-05-02 10:50:55 +0200 | [diff] [blame] | 195 | decoded_frame_buffer_(num_simulcast_or_spatial_layers_), |
Sergey Silkin | c89eed9 | 2018-04-01 23:57:51 +0200 | [diff] [blame] | 196 | post_encode_time_ns_(0) { |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 197 | // Sanity checks. |
Danil Chapovalov | ad89528 | 2019-03-11 10:28:05 +0000 | [diff] [blame] | 198 | RTC_CHECK(TaskQueueBase::Current()) |
Rasmus Brandt | 4b381af | 2018-02-07 13:56:16 +0100 | [diff] [blame] | 199 | << "VideoProcessor must be run on a task queue."; |
Rasmus Brandt | 001c782 | 2019-03-22 13:41:48 +0100 | [diff] [blame] | 200 | RTC_CHECK(stats_); |
| 201 | RTC_CHECK(encoder_); |
| 202 | RTC_CHECK(decoders_); |
| 203 | RTC_CHECK_EQ(decoders_->size(), num_simulcast_or_spatial_layers_); |
| 204 | RTC_CHECK(input_frame_reader_); |
| 205 | RTC_CHECK(encoded_frame_writers_); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 206 | RTC_CHECK(!decoded_frame_writers || |
| 207 | decoded_frame_writers->size() == num_simulcast_or_spatial_layers_); |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 208 | |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 209 | // Setup required callbacks for the encoder and decoder and initialize them. |
brandtr | bdd555c | 2017-08-21 01:34:04 -0700 | [diff] [blame] | 210 | RTC_CHECK_EQ(encoder_->RegisterEncodeCompleteCallback(&encode_callback_), |
Åsa Persson | f0c4467 | 2017-10-24 16:03:39 +0200 | [diff] [blame] | 211 | WEBRTC_VIDEO_CODEC_OK); |
asapersson | 654d54c | 2017-02-10 00:16:07 -0800 | [diff] [blame] | 212 | |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 213 | // Initialize codecs so that they are ready to receive frames. |
Elad Alon | 370f93a | 2019-06-11 14:57:57 +0200 | [diff] [blame] | 214 | RTC_CHECK_EQ(encoder_->InitEncode( |
| 215 | &config_.codec_settings, |
| 216 | VideoEncoder::Settings( |
| 217 | kCapabilities, static_cast<int>(config_.NumberOfCores()), |
| 218 | config_.max_payload_size_bytes)), |
Sergey Silkin | 1723cf9 | 2018-01-22 15:49:55 +0100 | [diff] [blame] | 219 | WEBRTC_VIDEO_CODEC_OK); |
Sergey Silkin | 645e2e0 | 2018-04-06 09:42:13 +0200 | [diff] [blame] | 220 | |
Sergey Silkin | bc20fe1 | 2018-04-15 21:06:16 +0200 | [diff] [blame] | 221 | for (size_t i = 0; i < num_simulcast_or_spatial_layers_; ++i) { |
Sergey Silkin | 645e2e0 | 2018-04-06 09:42:13 +0200 | [diff] [blame] | 222 | decode_callback_.push_back( |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 223 | absl::make_unique<VideoProcessorDecodeCompleteCallback>(this, i)); |
Sergey Silkin | bc20fe1 | 2018-04-15 21:06:16 +0200 | [diff] [blame] | 224 | RTC_CHECK_EQ( |
| 225 | decoders_->at(i)->InitDecode(&config_.codec_settings, |
| 226 | static_cast<int>(config_.NumberOfCores())), |
| 227 | WEBRTC_VIDEO_CODEC_OK); |
| 228 | RTC_CHECK_EQ(decoders_->at(i)->RegisterDecodeCompleteCallback( |
| 229 | decode_callback_.at(i).get()), |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 230 | WEBRTC_VIDEO_CODEC_OK); |
| 231 | } |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Åsa Persson | f0c4467 | 2017-10-24 16:03:39 +0200 | [diff] [blame] | 234 | VideoProcessor::~VideoProcessor() { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 235 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
brandtr | c8c5905 | 2017-08-21 06:44:16 -0700 | [diff] [blame] | 236 | |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 237 | // Explicitly reset codecs, in case they don't do that themselves when they |
| 238 | // go out of scope. |
brandtr | 77920a4 | 2017-08-11 07:48:15 -0700 | [diff] [blame] | 239 | RTC_CHECK_EQ(encoder_->Release(), WEBRTC_VIDEO_CODEC_OK); |
brandtr | bdd555c | 2017-08-21 01:34:04 -0700 | [diff] [blame] | 240 | encoder_->RegisterEncodeCompleteCallback(nullptr); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 241 | for (auto& decoder : *decoders_) { |
| 242 | RTC_CHECK_EQ(decoder->Release(), WEBRTC_VIDEO_CODEC_OK); |
| 243 | decoder->RegisterDecodeCompleteCallback(nullptr); |
| 244 | } |
| 245 | |
Rasmus Brandt | d00c895 | 2018-03-14 12:29:57 +0100 | [diff] [blame] | 246 | // Sanity check. |
| 247 | RTC_CHECK_LE(input_frames_.size(), kMaxBufferedInputFrames); |
brandtr | 77920a4 | 2017-08-11 07:48:15 -0700 | [diff] [blame] | 248 | } |
| 249 | |
brandtr | 8935d97 | 2017-09-06 01:53:22 -0700 | [diff] [blame] | 250 | void VideoProcessor::ProcessFrame() { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 251 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 252 | const size_t frame_number = last_inputed_frame_num_++; |
asapersson | 654d54c | 2017-02-10 00:16:07 -0800 | [diff] [blame] | 253 | |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 254 | // Get input frame and store for future quality calculation. |
| 255 | rtc::scoped_refptr<I420BufferInterface> buffer = |
| 256 | input_frame_reader_->ReadFrame(); |
brandtr | bdd555c | 2017-08-21 01:34:04 -0700 | [diff] [blame] | 257 | RTC_CHECK(buffer) << "Tried to read too many frames from the file."; |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 258 | const size_t timestamp = |
Sergey Silkin | 44cec0b | 2019-07-11 14:20:38 +0200 | [diff] [blame] | 259 | last_inputed_timestamp_ + |
| 260 | static_cast<size_t>(kVideoPayloadTypeFrequency / framerate_fps_); |
Artem Titov | 1ebfb6a | 2019-01-03 23:49:37 +0100 | [diff] [blame] | 261 | VideoFrame input_frame = |
| 262 | VideoFrame::Builder() |
| 263 | .set_video_frame_buffer(buffer) |
| 264 | .set_timestamp_rtp(static_cast<uint32_t>(timestamp)) |
| 265 | .set_timestamp_ms(static_cast<int64_t>(timestamp / kMsToRtpTimestamp)) |
| 266 | .set_rotation(webrtc::kVideoRotation_0) |
| 267 | .build(); |
Rasmus Brandt | d00c895 | 2018-03-14 12:29:57 +0100 | [diff] [blame] | 268 | // Store input frame as a reference for quality calculations. |
| 269 | if (config_.decode && !config_.measure_cpu) { |
Rasmus Brandt | 7b92ceb | 2018-06-26 12:34:59 +0200 | [diff] [blame] | 270 | if (input_frames_.size() == kMaxBufferedInputFrames) { |
| 271 | input_frames_.erase(input_frames_.begin()); |
| 272 | } |
Rasmus Brandt | d00c895 | 2018-03-14 12:29:57 +0100 | [diff] [blame] | 273 | input_frames_.emplace(frame_number, input_frame); |
| 274 | } |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 275 | last_inputed_timestamp_ = timestamp; |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 276 | |
Sergey Silkin | c89eed9 | 2018-04-01 23:57:51 +0200 | [diff] [blame] | 277 | post_encode_time_ns_ = 0; |
| 278 | |
Rasmus Brandt | 5f7a891 | 2018-02-28 17:17:15 +0100 | [diff] [blame] | 279 | // Create frame statistics object for all simulcast/spatial layers. |
Sergey Silkin | bc20fe1 | 2018-04-15 21:06:16 +0200 | [diff] [blame] | 280 | for (size_t i = 0; i < num_simulcast_or_spatial_layers_; ++i) { |
Sergey Silkin | 02fed02 | 2018-09-25 13:48:19 +0200 | [diff] [blame] | 281 | FrameStatistics frame_stat(frame_number, timestamp, i); |
| 282 | stats_->AddFrame(frame_stat); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 283 | } |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 284 | |
| 285 | // For the highest measurement accuracy of the encode time, the start/stop |
| 286 | // time recordings should wrap the Encode call as tightly as possible. |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 287 | const int64_t encode_start_ns = rtc::TimeNanos(); |
Sergey Silkin | bc20fe1 | 2018-04-15 21:06:16 +0200 | [diff] [blame] | 288 | for (size_t i = 0; i < num_simulcast_or_spatial_layers_; ++i) { |
| 289 | FrameStatistics* frame_stat = stats_->GetFrame(frame_number, i); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 290 | frame_stat->encode_start_ns = encode_start_ns; |
| 291 | } |
| 292 | |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 293 | // Encode. |
Niels Möller | 87e2d78 | 2019-03-07 10:18:23 +0100 | [diff] [blame] | 294 | const std::vector<VideoFrameType> frame_types = |
Niels Möller | 8f7ce22 | 2019-03-21 15:43:58 +0100 | [diff] [blame] | 295 | (frame_number == 0) |
| 296 | ? std::vector<VideoFrameType>{VideoFrameType::kVideoFrameKey} |
| 297 | : std::vector<VideoFrameType>{VideoFrameType::kVideoFrameDelta}; |
Niels Möller | c8d2e73 | 2019-03-06 12:00:33 +0100 | [diff] [blame] | 298 | const int encode_return_code = encoder_->Encode(input_frame, &frame_types); |
Sergey Silkin | bc20fe1 | 2018-04-15 21:06:16 +0200 | [diff] [blame] | 299 | for (size_t i = 0; i < num_simulcast_or_spatial_layers_; ++i) { |
| 300 | FrameStatistics* frame_stat = stats_->GetFrame(frame_number, i); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 301 | frame_stat->encode_return_code = encode_return_code; |
| 302 | } |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Sergey Silkin | 44cec0b | 2019-07-11 14:20:38 +0200 | [diff] [blame] | 305 | void VideoProcessor::SetRates(size_t bitrate_kbps, double framerate_fps) { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 306 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
Sergey Silkin | 44cec0b | 2019-07-11 14:20:38 +0200 | [diff] [blame] | 307 | framerate_fps_ = framerate_fps; |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 308 | bitrate_allocation_ = bitrate_allocator_->GetAllocation( |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 309 | static_cast<uint32_t>(bitrate_kbps * 1000), framerate_fps_); |
Sergey Silkin | 44cec0b | 2019-07-11 14:20:38 +0200 | [diff] [blame] | 310 | encoder_->SetRates( |
| 311 | VideoEncoder::RateControlParameters(bitrate_allocation_, framerate_fps_)); |
brandtr | bea36fd | 2017-08-07 03:36:54 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Sami Kalliomäki | 451b29c | 2018-07-04 14:33:51 +0200 | [diff] [blame] | 314 | int32_t VideoProcessor::VideoProcessorDecodeCompleteCallback::Decoded( |
| 315 | VideoFrame& image) { |
| 316 | // Post the callback to the right task queue, if needed. |
| 317 | if (!task_queue_->IsCurrent()) { |
| 318 | // There might be a limited amount of output buffers, make a copy to make |
| 319 | // sure we don't block the decoder. |
Artem Titov | 1ebfb6a | 2019-01-03 23:49:37 +0100 | [diff] [blame] | 320 | VideoFrame copy = VideoFrame::Builder() |
| 321 | .set_video_frame_buffer(I420Buffer::Copy( |
| 322 | *image.video_frame_buffer()->ToI420())) |
| 323 | .set_rotation(image.rotation()) |
| 324 | .set_timestamp_us(image.timestamp_us()) |
| 325 | .set_id(image.id()) |
| 326 | .build(); |
Sami Kalliomäki | 451b29c | 2018-07-04 14:33:51 +0200 | [diff] [blame] | 327 | copy.set_timestamp(image.timestamp()); |
| 328 | |
Danil Chapovalov | ad89528 | 2019-03-11 10:28:05 +0000 | [diff] [blame] | 329 | task_queue_->PostTask(ToQueuedTask([this, copy]() { |
Sami Kalliomäki | 451b29c | 2018-07-04 14:33:51 +0200 | [diff] [blame] | 330 | video_processor_->FrameDecoded(copy, simulcast_svc_idx_); |
Danil Chapovalov | ad89528 | 2019-03-11 10:28:05 +0000 | [diff] [blame] | 331 | })); |
Sami Kalliomäki | 451b29c | 2018-07-04 14:33:51 +0200 | [diff] [blame] | 332 | return 0; |
| 333 | } |
| 334 | video_processor_->FrameDecoded(image, simulcast_svc_idx_); |
| 335 | return 0; |
| 336 | } |
| 337 | |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 338 | void VideoProcessor::FrameEncoded( |
| 339 | const webrtc::EncodedImage& encoded_image, |
| 340 | const webrtc::CodecSpecificInfo& codec_specific) { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 341 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
brandtr | c8c5905 | 2017-08-21 06:44:16 -0700 | [diff] [blame] | 342 | |
brandtr | 32e0d26 | 2017-02-15 05:29:38 -0800 | [diff] [blame] | 343 | // For the highest measurement accuracy of the encode time, the start/stop |
| 344 | // time recordings should wrap the Encode call as tightly as possible. |
Rasmus Brandt | 5f7a891 | 2018-02-28 17:17:15 +0100 | [diff] [blame] | 345 | const int64_t encode_stop_ns = rtc::TimeNanos(); |
brandtr | 32e0d26 | 2017-02-15 05:29:38 -0800 | [diff] [blame] | 346 | |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 347 | const VideoCodecType codec_type = codec_specific.codecType; |
Rasmus Brandt | f7a3558 | 2017-10-24 10:16:33 +0200 | [diff] [blame] | 348 | if (config_.encoded_frame_checker) { |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 349 | config_.encoded_frame_checker->CheckEncodedFrame(codec_type, encoded_image); |
Rasmus Brandt | f7a3558 | 2017-10-24 10:16:33 +0200 | [diff] [blame] | 350 | } |
brandtr | b78bc75 | 2017-02-22 01:26:59 -0800 | [diff] [blame] | 351 | |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 352 | // Layer metadata. |
Niels Möller | d3b8c63 | 2018-08-27 15:33:42 +0200 | [diff] [blame] | 353 | size_t spatial_idx = encoded_image.SpatialIndex().value_or(0); |
| 354 | size_t temporal_idx = GetTemporalLayerIndex(codec_specific); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 355 | |
Sergey Silkin | bc20fe1 | 2018-04-15 21:06:16 +0200 | [diff] [blame] | 356 | FrameStatistics* frame_stat = |
Niels Möller | 2377588 | 2018-08-16 10:24:12 +0200 | [diff] [blame] | 357 | stats_->GetFrameWithTimestamp(encoded_image.Timestamp(), spatial_idx); |
Åsa Persson | a6e7b88 | 2018-01-19 14:57:10 +0100 | [diff] [blame] | 358 | const size_t frame_number = frame_stat->frame_number; |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 359 | |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 360 | // Ensure that the encode order is monotonically increasing, within this |
| 361 | // simulcast/spatial layer. |
Sergey Silkin | bc20fe1 | 2018-04-15 21:06:16 +0200 | [diff] [blame] | 362 | RTC_CHECK(first_encoded_frame_[spatial_idx] || |
| 363 | last_encoded_frame_num_[spatial_idx] < frame_number); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 364 | |
| 365 | // Ensure SVC spatial layers are delivered in ascending order. |
Rasmus Brandt | 001c782 | 2019-03-22 13:41:48 +0100 | [diff] [blame] | 366 | const size_t num_spatial_layers = config_.NumberOfSpatialLayers(); |
| 367 | if (!first_encoded_frame_[spatial_idx] && num_spatial_layers > 1) { |
Sergey Silkin | bc20fe1 | 2018-04-15 21:06:16 +0200 | [diff] [blame] | 368 | for (size_t i = 0; i < spatial_idx; ++i) { |
Sergey Silkin | 122ba6c | 2018-03-27 14:32:21 +0200 | [diff] [blame] | 369 | RTC_CHECK_LE(last_encoded_frame_num_[i], frame_number); |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 370 | } |
Sergey Silkin | bc20fe1 | 2018-04-15 21:06:16 +0200 | [diff] [blame] | 371 | for (size_t i = spatial_idx + 1; i < num_simulcast_or_spatial_layers_; |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 372 | ++i) { |
| 373 | RTC_CHECK_GT(frame_number, last_encoded_frame_num_[i]); |
| 374 | } |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 375 | } |
Sergey Silkin | bc20fe1 | 2018-04-15 21:06:16 +0200 | [diff] [blame] | 376 | first_encoded_frame_[spatial_idx] = false; |
| 377 | last_encoded_frame_num_[spatial_idx] = frame_number; |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 378 | |
brandtr | 8935d97 | 2017-09-06 01:53:22 -0700 | [diff] [blame] | 379 | // Update frame statistics. |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 380 | frame_stat->encoding_successful = true; |
Sergey Silkin | c89eed9 | 2018-04-01 23:57:51 +0200 | [diff] [blame] | 381 | frame_stat->encode_time_us = GetElapsedTimeMicroseconds( |
| 382 | frame_stat->encode_start_ns, encode_stop_ns - post_encode_time_ns_); |
Sergey Silkin | bc20fe1 | 2018-04-15 21:06:16 +0200 | [diff] [blame] | 383 | frame_stat->target_bitrate_kbps = |
Sergey Silkin | bfd54ef | 2018-04-13 23:41:11 +0200 | [diff] [blame] | 384 | bitrate_allocation_.GetTemporalLayerSum(spatial_idx, temporal_idx) / 1000; |
Sergey Silkin | 44cec0b | 2019-07-11 14:20:38 +0200 | [diff] [blame] | 385 | frame_stat->target_framerate_fps = framerate_fps_; |
Niels Möller | 77536a2 | 2019-01-15 08:50:01 +0100 | [diff] [blame] | 386 | frame_stat->length_bytes = encoded_image.size(); |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 387 | frame_stat->frame_type = encoded_image._frameType; |
Sergey Silkin | bc20fe1 | 2018-04-15 21:06:16 +0200 | [diff] [blame] | 388 | frame_stat->temporal_idx = temporal_idx; |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 389 | frame_stat->max_nalu_size_bytes = GetMaxNaluSizeBytes(encoded_image, config_); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 390 | frame_stat->qp = encoded_image.qp_; |
ssilkin | 612f858 | 2017-09-28 09:23:17 -0700 | [diff] [blame] | 391 | |
Sergey Silkin | bc0f0d3 | 2018-04-24 21:29:14 +0200 | [diff] [blame] | 392 | bool end_of_picture = false; |
Sergey Silkin | 645e2e0 | 2018-04-06 09:42:13 +0200 | [diff] [blame] | 393 | if (codec_type == kVideoCodecVP9) { |
| 394 | const CodecSpecificInfoVP9& vp9_info = codec_specific.codecSpecific.VP9; |
| 395 | frame_stat->inter_layer_predicted = vp9_info.inter_layer_predicted; |
Sergey Silkin | 3c30c9c | 2018-05-02 09:18:48 +0200 | [diff] [blame] | 396 | frame_stat->non_ref_for_inter_layer_pred = |
| 397 | vp9_info.non_ref_for_inter_layer_pred; |
Sergey Silkin | bc0f0d3 | 2018-04-24 21:29:14 +0200 | [diff] [blame] | 398 | end_of_picture = vp9_info.end_of_picture; |
Sergey Silkin | 3c30c9c | 2018-05-02 09:18:48 +0200 | [diff] [blame] | 399 | } else { |
| 400 | frame_stat->inter_layer_predicted = false; |
| 401 | frame_stat->non_ref_for_inter_layer_pred = true; |
Sergey Silkin | 645e2e0 | 2018-04-06 09:42:13 +0200 | [diff] [blame] | 402 | } |
| 403 | |
Sergey Silkin | 122ba6c | 2018-03-27 14:32:21 +0200 | [diff] [blame] | 404 | const webrtc::EncodedImage* encoded_image_for_decode = &encoded_image; |
Rasmus Brandt | 001c782 | 2019-03-22 13:41:48 +0100 | [diff] [blame] | 405 | if (config_.decode || !encoded_frame_writers_->empty()) { |
Sergey Silkin | 645e2e0 | 2018-04-06 09:42:13 +0200 | [diff] [blame] | 406 | if (num_spatial_layers > 1) { |
| 407 | encoded_image_for_decode = BuildAndStoreSuperframe( |
Sergey Silkin | bc20fe1 | 2018-04-15 21:06:16 +0200 | [diff] [blame] | 408 | encoded_image, codec_type, frame_number, spatial_idx, |
Sergey Silkin | 645e2e0 | 2018-04-06 09:42:13 +0200 | [diff] [blame] | 409 | frame_stat->inter_layer_predicted); |
Rasmus Brandt | d00c895 | 2018-03-14 12:29:57 +0100 | [diff] [blame] | 410 | } |
Sergey Silkin | 645e2e0 | 2018-04-06 09:42:13 +0200 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | if (config_.decode) { |
Sergey Silkin | bc20fe1 | 2018-04-15 21:06:16 +0200 | [diff] [blame] | 414 | DecodeFrame(*encoded_image_for_decode, spatial_idx); |
Sergey Silkin | 645e2e0 | 2018-04-06 09:42:13 +0200 | [diff] [blame] | 415 | |
Sergey Silkin | 3c30c9c | 2018-05-02 09:18:48 +0200 | [diff] [blame] | 416 | if (end_of_picture && num_spatial_layers > 1) { |
Sergey Silkin | 645e2e0 | 2018-04-06 09:42:13 +0200 | [diff] [blame] | 417 | // If inter-layer prediction is enabled and upper layer was dropped then |
| 418 | // base layer should be passed to upper layer decoder. Otherwise decoder |
| 419 | // won't be able to decode next superframe. |
| 420 | const EncodedImage* base_image = nullptr; |
Sergey Silkin | 3c30c9c | 2018-05-02 09:18:48 +0200 | [diff] [blame] | 421 | const FrameStatistics* base_stat = nullptr; |
Sergey Silkin | bc20fe1 | 2018-04-15 21:06:16 +0200 | [diff] [blame] | 422 | for (size_t i = 0; i < num_spatial_layers; ++i) { |
Sergey Silkin | 3c30c9c | 2018-05-02 09:18:48 +0200 | [diff] [blame] | 423 | const bool layer_dropped = (first_decoded_frame_[i] || |
| 424 | last_decoded_frame_num_[i] < frame_number); |
Sergey Silkin | 645e2e0 | 2018-04-06 09:42:13 +0200 | [diff] [blame] | 425 | |
| 426 | // Ensure current layer was decoded. |
Sergey Silkin | bc20fe1 | 2018-04-15 21:06:16 +0200 | [diff] [blame] | 427 | RTC_CHECK(layer_dropped == false || i != spatial_idx); |
Sergey Silkin | 645e2e0 | 2018-04-06 09:42:13 +0200 | [diff] [blame] | 428 | |
| 429 | if (!layer_dropped) { |
Sergey Silkin | bc20fe1 | 2018-04-15 21:06:16 +0200 | [diff] [blame] | 430 | base_image = &merged_encoded_frames_[i]; |
Sergey Silkin | 3c30c9c | 2018-05-02 09:18:48 +0200 | [diff] [blame] | 431 | base_stat = |
Niels Möller | 2377588 | 2018-08-16 10:24:12 +0200 | [diff] [blame] | 432 | stats_->GetFrameWithTimestamp(encoded_image.Timestamp(), i); |
Sergey Silkin | 3c30c9c | 2018-05-02 09:18:48 +0200 | [diff] [blame] | 433 | } else if (base_image && !base_stat->non_ref_for_inter_layer_pred) { |
Sergey Silkin | bc20fe1 | 2018-04-15 21:06:16 +0200 | [diff] [blame] | 434 | DecodeFrame(*base_image, i); |
Sergey Silkin | 645e2e0 | 2018-04-06 09:42:13 +0200 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | } |
Rasmus Brandt | d00c895 | 2018-03-14 12:29:57 +0100 | [diff] [blame] | 438 | } else { |
| 439 | frame_stat->decode_return_code = WEBRTC_VIDEO_CODEC_NO_OUTPUT; |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 440 | } |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 441 | |
Rasmus Brandt | 001c782 | 2019-03-22 13:41:48 +0100 | [diff] [blame] | 442 | // Since frames in higher TLs typically depend on frames in lower TLs, |
| 443 | // write out frames in lower TLs to bitstream dumps of higher TLs. |
| 444 | for (size_t write_temporal_idx = temporal_idx; |
| 445 | write_temporal_idx < config_.NumberOfTemporalLayers(); |
| 446 | ++write_temporal_idx) { |
| 447 | const VideoProcessor::LayerKey layer_key(spatial_idx, write_temporal_idx); |
| 448 | auto it = encoded_frame_writers_->find(layer_key); |
| 449 | if (it != encoded_frame_writers_->cend()) { |
| 450 | RTC_CHECK(it->second->WriteFrame(*encoded_image_for_decode, |
| 451 | config_.codec_settings.codecType)); |
| 452 | } |
brandtr | 8935d97 | 2017-09-06 01:53:22 -0700 | [diff] [blame] | 453 | } |
Sergey Silkin | c89eed9 | 2018-04-01 23:57:51 +0200 | [diff] [blame] | 454 | |
Kári Tristan Helgason | f167762 | 2018-08-24 13:21:26 +0200 | [diff] [blame] | 455 | if (!config_.encode_in_real_time) { |
Sergey Silkin | c89eed9 | 2018-04-01 23:57:51 +0200 | [diff] [blame] | 456 | // To get pure encode time for next layers, measure time spent in encode |
| 457 | // callback and subtract it from encode time of next layers. |
| 458 | post_encode_time_ns_ += rtc::TimeNanos() - encode_stop_ns; |
| 459 | } |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Sergey Silkin | 645e2e0 | 2018-04-06 09:42:13 +0200 | [diff] [blame] | 462 | void VideoProcessor::FrameDecoded(const VideoFrame& decoded_frame, |
Sergey Silkin | bc20fe1 | 2018-04-15 21:06:16 +0200 | [diff] [blame] | 463 | size_t spatial_idx) { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 464 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
brandtr | c8c5905 | 2017-08-21 06:44:16 -0700 | [diff] [blame] | 465 | |
brandtr | 32e0d26 | 2017-02-15 05:29:38 -0800 | [diff] [blame] | 466 | // For the highest measurement accuracy of the decode time, the start/stop |
| 467 | // time recordings should wrap the Decode call as tightly as possible. |
Rasmus Brandt | 5f7a891 | 2018-02-28 17:17:15 +0100 | [diff] [blame] | 468 | const int64_t decode_stop_ns = rtc::TimeNanos(); |
brandtr | 8bc9385 | 2017-02-15 05:19:51 -0800 | [diff] [blame] | 469 | |
Sergey Silkin | bc20fe1 | 2018-04-15 21:06:16 +0200 | [diff] [blame] | 470 | FrameStatistics* frame_stat = |
| 471 | stats_->GetFrameWithTimestamp(decoded_frame.timestamp(), spatial_idx); |
Åsa Persson | a6e7b88 | 2018-01-19 14:57:10 +0100 | [diff] [blame] | 472 | const size_t frame_number = frame_stat->frame_number; |
Sergey Silkin | 64eaa99 | 2017-11-17 14:47:32 +0100 | [diff] [blame] | 473 | |
Sergey Silkin | 5613879 | 2018-05-02 10:50:55 +0200 | [diff] [blame] | 474 | if (decoded_frame_writers_ && !first_decoded_frame_[spatial_idx]) { |
| 475 | // Fill drops with last decoded frame to make them look like freeze at |
| 476 | // playback and to keep decoded layers in sync. |
| 477 | for (size_t i = last_decoded_frame_num_[spatial_idx] + 1; i < frame_number; |
| 478 | ++i) { |
| 479 | RTC_CHECK(decoded_frame_writers_->at(spatial_idx) |
| 480 | ->WriteFrame(decoded_frame_buffer_[spatial_idx].data())); |
| 481 | } |
| 482 | } |
| 483 | |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 484 | // Ensure that the decode order is monotonically increasing, within this |
| 485 | // simulcast/spatial layer. |
Sergey Silkin | bc20fe1 | 2018-04-15 21:06:16 +0200 | [diff] [blame] | 486 | RTC_CHECK(first_decoded_frame_[spatial_idx] || |
| 487 | last_decoded_frame_num_[spatial_idx] < frame_number); |
| 488 | first_decoded_frame_[spatial_idx] = false; |
| 489 | last_decoded_frame_num_[spatial_idx] = frame_number; |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 490 | |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 491 | // Update frame statistics. |
| 492 | frame_stat->decoding_successful = true; |
| 493 | frame_stat->decode_time_us = |
| 494 | GetElapsedTimeMicroseconds(frame_stat->decode_start_ns, decode_stop_ns); |
| 495 | frame_stat->decoded_width = decoded_frame.width(); |
| 496 | frame_stat->decoded_height = decoded_frame.height(); |
| 497 | |
Sergey Silkin | 64eaa99 | 2017-11-17 14:47:32 +0100 | [diff] [blame] | 498 | // Skip quality metrics calculation to not affect CPU usage. |
| 499 | if (!config_.measure_cpu) { |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 500 | const auto reference_frame = input_frames_.find(frame_number); |
| 501 | RTC_CHECK(reference_frame != input_frames_.cend()) |
| 502 | << "The codecs are either buffering too much, dropping too much, or " |
| 503 | "being too slow relative the input frame rate."; |
Sergey Silkin | 6e1402b | 2019-02-13 09:33:00 +0100 | [diff] [blame] | 504 | |
| 505 | // SSIM calculation is not optimized. Skip it in real-time mode. |
| 506 | const bool calc_ssim = !config_.encode_in_real_time; |
Sergey Silkin | 8d3758e | 2018-03-14 11:28:15 +0100 | [diff] [blame] | 507 | CalculateFrameQuality( |
| 508 | *reference_frame->second.video_frame_buffer()->ToI420(), |
Sergey Silkin | 6e1402b | 2019-02-13 09:33:00 +0100 | [diff] [blame] | 509 | *decoded_frame.video_frame_buffer()->ToI420(), frame_stat, calc_ssim); |
Niels Möller | 718a763 | 2016-06-13 13:06:01 +0200 | [diff] [blame] | 510 | |
Rasmus Brandt | d00c895 | 2018-03-14 12:29:57 +0100 | [diff] [blame] | 511 | // Erase all buffered input frames that we have moved past for all |
| 512 | // simulcast/spatial layers. Never buffer more than |
| 513 | // |kMaxBufferedInputFrames| frames, to protect against long runs of |
| 514 | // consecutive frame drops for a particular layer. |
| 515 | const auto min_last_decoded_frame_num = std::min_element( |
| 516 | last_decoded_frame_num_.cbegin(), last_decoded_frame_num_.cend()); |
| 517 | const size_t min_buffered_frame_num = std::max( |
| 518 | 0, static_cast<int>(frame_number) - kMaxBufferedInputFrames + 1); |
| 519 | RTC_CHECK(min_last_decoded_frame_num != last_decoded_frame_num_.cend()); |
| 520 | const auto input_frames_erase_before = input_frames_.lower_bound( |
| 521 | std::max(*min_last_decoded_frame_num, min_buffered_frame_num)); |
| 522 | input_frames_.erase(input_frames_.cbegin(), input_frames_erase_before); |
| 523 | } |
Sergey Silkin | 64eaa99 | 2017-11-17 14:47:32 +0100 | [diff] [blame] | 524 | |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 525 | if (decoded_frame_writers_) { |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 526 | ExtractI420BufferWithSize(decoded_frame, config_.codec_settings.width, |
Sergey Silkin | 5613879 | 2018-05-02 10:50:55 +0200 | [diff] [blame] | 527 | config_.codec_settings.height, |
| 528 | &decoded_frame_buffer_[spatial_idx]); |
| 529 | RTC_CHECK_EQ(decoded_frame_buffer_[spatial_idx].size(), |
Sergey Silkin | bc20fe1 | 2018-04-15 21:06:16 +0200 | [diff] [blame] | 530 | decoded_frame_writers_->at(spatial_idx)->FrameLength()); |
| 531 | RTC_CHECK(decoded_frame_writers_->at(spatial_idx) |
Sergey Silkin | 5613879 | 2018-05-02 10:50:55 +0200 | [diff] [blame] | 532 | ->WriteFrame(decoded_frame_buffer_[spatial_idx].data())); |
Sergey Silkin | 64eaa99 | 2017-11-17 14:47:32 +0100 | [diff] [blame] | 533 | } |
Åsa Persson | f0c4467 | 2017-10-24 16:03:39 +0200 | [diff] [blame] | 534 | } |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 535 | |
Sergey Silkin | 645e2e0 | 2018-04-06 09:42:13 +0200 | [diff] [blame] | 536 | void VideoProcessor::DecodeFrame(const EncodedImage& encoded_image, |
Sergey Silkin | bc20fe1 | 2018-04-15 21:06:16 +0200 | [diff] [blame] | 537 | size_t spatial_idx) { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 538 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
Sergey Silkin | bc20fe1 | 2018-04-15 21:06:16 +0200 | [diff] [blame] | 539 | FrameStatistics* frame_stat = |
Niels Möller | 2377588 | 2018-08-16 10:24:12 +0200 | [diff] [blame] | 540 | stats_->GetFrameWithTimestamp(encoded_image.Timestamp(), spatial_idx); |
Sergey Silkin | 645e2e0 | 2018-04-06 09:42:13 +0200 | [diff] [blame] | 541 | |
| 542 | frame_stat->decode_start_ns = rtc::TimeNanos(); |
| 543 | frame_stat->decode_return_code = |
Niels Möller | 7aacdd9 | 2019-03-25 09:11:40 +0100 | [diff] [blame] | 544 | decoders_->at(spatial_idx)->Decode(encoded_image, false, 0); |
Sergey Silkin | 645e2e0 | 2018-04-06 09:42:13 +0200 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | const webrtc::EncodedImage* VideoProcessor::BuildAndStoreSuperframe( |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 548 | const EncodedImage& encoded_image, |
| 549 | const VideoCodecType codec, |
| 550 | size_t frame_number, |
Sergey Silkin | bc20fe1 | 2018-04-15 21:06:16 +0200 | [diff] [blame] | 551 | size_t spatial_idx, |
Sergey Silkin | 645e2e0 | 2018-04-06 09:42:13 +0200 | [diff] [blame] | 552 | bool inter_layer_predicted) { |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 553 | // Should only be called for SVC. |
| 554 | RTC_CHECK_GT(config_.NumberOfSpatialLayers(), 1); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 555 | |
| 556 | EncodedImage base_image; |
Niels Möller | 77536a2 | 2019-01-15 08:50:01 +0100 | [diff] [blame] | 557 | RTC_CHECK_EQ(base_image.size(), 0); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 558 | |
Sergey Silkin | 122ba6c | 2018-03-27 14:32:21 +0200 | [diff] [blame] | 559 | // Each SVC layer is decoded with dedicated decoder. Find the nearest |
| 560 | // non-dropped base frame and merge it and current frame into superframe. |
Sergey Silkin | 645e2e0 | 2018-04-06 09:42:13 +0200 | [diff] [blame] | 561 | if (inter_layer_predicted) { |
Sergey Silkin | bc20fe1 | 2018-04-15 21:06:16 +0200 | [diff] [blame] | 562 | for (int base_idx = static_cast<int>(spatial_idx) - 1; base_idx >= 0; |
Sergey Silkin | 122ba6c | 2018-03-27 14:32:21 +0200 | [diff] [blame] | 563 | --base_idx) { |
| 564 | EncodedImage lower_layer = merged_encoded_frames_.at(base_idx); |
Niels Möller | 2377588 | 2018-08-16 10:24:12 +0200 | [diff] [blame] | 565 | if (lower_layer.Timestamp() == encoded_image.Timestamp()) { |
Sergey Silkin | 122ba6c | 2018-03-27 14:32:21 +0200 | [diff] [blame] | 566 | base_image = lower_layer; |
| 567 | break; |
| 568 | } |
| 569 | } |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 570 | } |
Niels Möller | 77536a2 | 2019-01-15 08:50:01 +0100 | [diff] [blame] | 571 | const size_t payload_size_bytes = base_image.size() + encoded_image.size(); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 572 | |
Niels Möller | 663844d | 2019-02-14 16:15:54 +0100 | [diff] [blame] | 573 | EncodedImage copied_image = encoded_image; |
Niels Möller | 4d504c7 | 2019-06-18 15:56:56 +0200 | [diff] [blame] | 574 | copied_image.SetEncodedData(EncodedImageBuffer::Create(payload_size_bytes)); |
Niels Möller | 77536a2 | 2019-01-15 08:50:01 +0100 | [diff] [blame] | 575 | if (base_image.size()) { |
Niels Möller | 24871e4 | 2019-01-17 11:31:13 +0100 | [diff] [blame] | 576 | RTC_CHECK(base_image.data()); |
Niels Möller | 663844d | 2019-02-14 16:15:54 +0100 | [diff] [blame] | 577 | memcpy(copied_image.data(), base_image.data(), base_image.size()); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 578 | } |
Niels Möller | 663844d | 2019-02-14 16:15:54 +0100 | [diff] [blame] | 579 | memcpy(copied_image.data() + base_image.size(), encoded_image.data(), |
Niels Möller | 77536a2 | 2019-01-15 08:50:01 +0100 | [diff] [blame] | 580 | encoded_image.size()); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 581 | |
Niels Möller | 77536a2 | 2019-01-15 08:50:01 +0100 | [diff] [blame] | 582 | copied_image.set_size(payload_size_bytes); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 583 | |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 584 | // Replace previous EncodedImage for this spatial layer. |
Niels Möller | 663844d | 2019-02-14 16:15:54 +0100 | [diff] [blame] | 585 | merged_encoded_frames_.at(spatial_idx) = std::move(copied_image); |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 586 | |
Sergey Silkin | bc20fe1 | 2018-04-15 21:06:16 +0200 | [diff] [blame] | 587 | return &merged_encoded_frames_.at(spatial_idx); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 588 | } |
| 589 | |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 590 | } // namespace test |
| 591 | } // namespace webrtc |