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" |
Rasmus Brandt | 5f7a891 | 2018-02-28 17:17:15 +0100 | [diff] [blame] | 20 | #include "common_video/libyuv/include/webrtc_libyuv.h" |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 21 | #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "modules/video_coding/codecs/vp8/simulcast_rate_allocator.h" |
| 23 | #include "modules/video_coding/include/video_codec_initializer.h" |
Rasmus Brandt | d00c895 | 2018-03-14 12:29:57 +0100 | [diff] [blame] | 24 | #include "modules/video_coding/include/video_error_codes.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "modules/video_coding/utility/default_video_bitrate_allocator.h" |
| 26 | #include "rtc_base/checks.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 27 | #include "rtc_base/timeutils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 28 | #include "test/gtest.h" |
Sergey Silkin | 8d3758e | 2018-03-14 11:28:15 +0100 | [diff] [blame] | 29 | #include "third_party/libyuv/include/libyuv/compare.h" |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 30 | #include "third_party/libyuv/include/libyuv/scale.h" |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 31 | |
| 32 | namespace webrtc { |
| 33 | namespace test { |
| 34 | |
brandtr | b78bc75 | 2017-02-22 01:26:59 -0800 | [diff] [blame] | 35 | namespace { |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 36 | |
Åsa Persson | 91af24a | 2018-01-24 17:20:18 +0100 | [diff] [blame] | 37 | const int kMsToRtpTimestamp = kVideoPayloadTypeFrequency / 1000; |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 38 | const int kMaxBufferedInputFrames = 10; |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 39 | |
brandtr | aebc61e | 2017-02-28 07:13:47 -0800 | [diff] [blame] | 40 | std::unique_ptr<VideoBitrateAllocator> CreateBitrateAllocator( |
brandtr | 07734a5 | 2017-08-08 08:35:53 -0700 | [diff] [blame] | 41 | TestConfig* config) { |
brandtr | aebc61e | 2017-02-28 07:13:47 -0800 | [diff] [blame] | 42 | std::unique_ptr<TemporalLayersFactory> tl_factory; |
brandtr | 07734a5 | 2017-08-08 08:35:53 -0700 | [diff] [blame] | 43 | if (config->codec_settings.codecType == VideoCodecType::kVideoCodecVP8) { |
brandtr | aebc61e | 2017-02-28 07:13:47 -0800 | [diff] [blame] | 44 | tl_factory.reset(new TemporalLayersFactory()); |
brandtr | 07734a5 | 2017-08-08 08:35:53 -0700 | [diff] [blame] | 45 | config->codec_settings.VP8()->tl_factory = tl_factory.get(); |
brandtr | aebc61e | 2017-02-28 07:13:47 -0800 | [diff] [blame] | 46 | } |
| 47 | return std::unique_ptr<VideoBitrateAllocator>( |
brandtr | 07734a5 | 2017-08-08 08:35:53 -0700 | [diff] [blame] | 48 | VideoCodecInitializer::CreateBitrateAllocator(config->codec_settings, |
brandtr | aebc61e | 2017-02-28 07:13:47 -0800 | [diff] [blame] | 49 | std::move(tl_factory))); |
| 50 | } |
| 51 | |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 52 | size_t GetMaxNaluSizeBytes(const EncodedImage& encoded_frame, |
| 53 | const TestConfig& 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 = |
| 58 | webrtc::H264::FindNaluIndices(encoded_frame._buffer, |
| 59 | encoded_frame._length); |
| 60 | |
| 61 | RTC_CHECK(!nalu_indices.empty()); |
| 62 | |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 63 | size_t max_size = 0; |
ssilkin | 612f858 | 2017-09-28 09:23:17 -0700 | [diff] [blame] | 64 | for (const webrtc::H264::NaluIndex& index : nalu_indices) |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 65 | max_size = std::max(max_size, index.payload_size); |
ssilkin | 612f858 | 2017-09-28 09:23:17 -0700 | [diff] [blame] | 66 | |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 67 | return max_size; |
ssilkin | 612f858 | 2017-09-28 09:23:17 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 70 | void GetLayerIndices(const CodecSpecificInfo& codec_specific, |
| 71 | size_t* simulcast_svc_idx, |
| 72 | size_t* temporal_idx) { |
| 73 | if (codec_specific.codecType == kVideoCodecVP8) { |
| 74 | *simulcast_svc_idx = codec_specific.codecSpecific.VP8.simulcastIdx; |
| 75 | *temporal_idx = codec_specific.codecSpecific.VP8.temporalIdx; |
| 76 | } else if (codec_specific.codecType == kVideoCodecVP9) { |
| 77 | *simulcast_svc_idx = codec_specific.codecSpecific.VP9.spatial_idx; |
| 78 | *temporal_idx = codec_specific.codecSpecific.VP9.temporal_idx; |
| 79 | } |
| 80 | if (*simulcast_svc_idx == kNoSpatialIdx) { |
| 81 | *simulcast_svc_idx = 0; |
| 82 | } |
| 83 | if (*temporal_idx == kNoTemporalIdx) { |
| 84 | *temporal_idx = 0; |
| 85 | } |
| 86 | } |
| 87 | |
asapersson | ae9ba04 | 2017-03-07 00:25:38 -0800 | [diff] [blame] | 88 | int GetElapsedTimeMicroseconds(int64_t start_ns, int64_t stop_ns) { |
| 89 | int64_t diff_us = (stop_ns - start_ns) / rtc::kNumNanosecsPerMicrosec; |
| 90 | RTC_DCHECK_GE(diff_us, std::numeric_limits<int>::min()); |
| 91 | RTC_DCHECK_LE(diff_us, std::numeric_limits<int>::max()); |
| 92 | return static_cast<int>(diff_us); |
| 93 | } |
| 94 | |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 95 | void ExtractI420BufferWithSize(const VideoFrame& image, |
| 96 | int width, |
| 97 | int height, |
| 98 | rtc::Buffer* buffer) { |
Åsa Persson | f0c4467 | 2017-10-24 16:03:39 +0200 | [diff] [blame] | 99 | if (image.width() != width || image.height() != height) { |
| 100 | EXPECT_DOUBLE_EQ(static_cast<double>(width) / height, |
| 101 | static_cast<double>(image.width()) / image.height()); |
| 102 | // Same aspect ratio, no cropping needed. |
| 103 | rtc::scoped_refptr<I420Buffer> scaled(I420Buffer::Create(width, height)); |
| 104 | scaled->ScaleFrom(*image.video_frame_buffer()->ToI420()); |
| 105 | |
| 106 | size_t length = |
| 107 | CalcBufferSize(VideoType::kI420, scaled->width(), scaled->height()); |
| 108 | buffer->SetSize(length); |
| 109 | RTC_CHECK_NE(ExtractBuffer(scaled, length, buffer->data()), -1); |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | // No resize. |
| 114 | size_t length = |
| 115 | CalcBufferSize(VideoType::kI420, image.width(), image.height()); |
| 116 | buffer->SetSize(length); |
| 117 | RTC_CHECK_NE(ExtractBuffer(image, length, buffer->data()), -1); |
| 118 | } |
| 119 | |
Sergey Silkin | 8d3758e | 2018-03-14 11:28:15 +0100 | [diff] [blame] | 120 | void CalculateFrameQuality(const I420BufferInterface& ref_buffer, |
| 121 | const I420BufferInterface& dec_buffer, |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 122 | FrameStatistics* frame_stat) { |
Sergey Silkin | 8d3758e | 2018-03-14 11:28:15 +0100 | [diff] [blame] | 123 | if (ref_buffer.width() != dec_buffer.width() || |
| 124 | ref_buffer.height() != dec_buffer.height()) { |
| 125 | RTC_CHECK_GE(ref_buffer.width(), dec_buffer.width()); |
| 126 | RTC_CHECK_GE(ref_buffer.height(), dec_buffer.height()); |
| 127 | // Downscale reference frame. |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 128 | rtc::scoped_refptr<I420Buffer> scaled_buffer = |
Sergey Silkin | 8d3758e | 2018-03-14 11:28:15 +0100 | [diff] [blame] | 129 | I420Buffer::Create(dec_buffer.width(), dec_buffer.height()); |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 130 | I420Scale(ref_buffer.DataY(), ref_buffer.StrideY(), ref_buffer.DataU(), |
| 131 | ref_buffer.StrideU(), ref_buffer.DataV(), ref_buffer.StrideV(), |
| 132 | ref_buffer.width(), ref_buffer.height(), |
| 133 | scaled_buffer->MutableDataY(), scaled_buffer->StrideY(), |
| 134 | scaled_buffer->MutableDataU(), scaled_buffer->StrideU(), |
| 135 | scaled_buffer->MutableDataV(), scaled_buffer->StrideV(), |
| 136 | scaled_buffer->width(), scaled_buffer->height(), |
| 137 | libyuv::kFilterBox); |
Sergey Silkin | 8d3758e | 2018-03-14 11:28:15 +0100 | [diff] [blame] | 138 | |
| 139 | CalculateFrameQuality(*scaled_buffer, dec_buffer, frame_stat); |
| 140 | } else { |
| 141 | const uint64_t sse_y = libyuv::ComputeSumSquareErrorPlane( |
| 142 | dec_buffer.DataY(), dec_buffer.StrideY(), ref_buffer.DataY(), |
| 143 | ref_buffer.StrideY(), dec_buffer.width(), dec_buffer.height()); |
| 144 | |
| 145 | const uint64_t sse_u = libyuv::ComputeSumSquareErrorPlane( |
| 146 | dec_buffer.DataU(), dec_buffer.StrideU(), ref_buffer.DataU(), |
| 147 | ref_buffer.StrideU(), dec_buffer.width() / 2, dec_buffer.height() / 2); |
| 148 | |
| 149 | const uint64_t sse_v = libyuv::ComputeSumSquareErrorPlane( |
| 150 | dec_buffer.DataV(), dec_buffer.StrideV(), ref_buffer.DataV(), |
| 151 | ref_buffer.StrideV(), dec_buffer.width() / 2, dec_buffer.height() / 2); |
| 152 | |
| 153 | const size_t num_y_samples = dec_buffer.width() * dec_buffer.height(); |
| 154 | const size_t num_u_samples = |
| 155 | dec_buffer.width() / 2 * dec_buffer.height() / 2; |
| 156 | |
| 157 | frame_stat->psnr_y = libyuv::SumSquareErrorToPsnr(sse_y, num_y_samples); |
| 158 | frame_stat->psnr_u = libyuv::SumSquareErrorToPsnr(sse_u, num_u_samples); |
| 159 | frame_stat->psnr_v = libyuv::SumSquareErrorToPsnr(sse_v, num_u_samples); |
| 160 | frame_stat->psnr = libyuv::SumSquareErrorToPsnr( |
| 161 | sse_y + sse_u + sse_v, num_y_samples + 2 * num_u_samples); |
| 162 | frame_stat->ssim = I420SSIM(ref_buffer, dec_buffer); |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
brandtr | b78bc75 | 2017-02-22 01:26:59 -0800 | [diff] [blame] | 166 | } // namespace |
| 167 | |
brandtr | c409552 | 2017-08-07 08:12:33 -0700 | [diff] [blame] | 168 | VideoProcessor::VideoProcessor(webrtc::VideoEncoder* encoder, |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 169 | VideoDecoderList* decoders, |
| 170 | FrameReader* input_frame_reader, |
brandtr | c409552 | 2017-08-07 08:12:33 -0700 | [diff] [blame] | 171 | const TestConfig& config, |
Sergey Silkin | 06a8f30 | 2018-02-20 09:48:26 +0100 | [diff] [blame] | 172 | Stats* stats, |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 173 | IvfFileWriterList* encoded_frame_writers, |
| 174 | FrameWriterList* decoded_frame_writers) |
Åsa Persson | f0c4467 | 2017-10-24 16:03:39 +0200 | [diff] [blame] | 175 | : config_(config), |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 176 | num_simulcast_or_spatial_layers_( |
| 177 | std::max(config_.NumberOfSimulcastStreams(), |
| 178 | config_.NumberOfSpatialLayers())), |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 179 | stats_(stats), |
brandtr | 07734a5 | 2017-08-08 08:35:53 -0700 | [diff] [blame] | 180 | encoder_(encoder), |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 181 | decoders_(decoders), |
brandtr | 07734a5 | 2017-08-08 08:35:53 -0700 | [diff] [blame] | 182 | bitrate_allocator_(CreateBitrateAllocator(&config_)), |
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), |
| 185 | decode_callback_(this), |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 186 | input_frame_reader_(input_frame_reader), |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 187 | merged_encoded_frames_(num_simulcast_or_spatial_layers_), |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 188 | encoded_frame_writers_(encoded_frame_writers), |
| 189 | decoded_frame_writers_(decoded_frame_writers), |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 190 | last_inputed_frame_num_(0), |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 191 | last_inputed_timestamp_(0), |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 192 | first_encoded_frame_(num_simulcast_or_spatial_layers_, true), |
| 193 | last_encoded_frame_num_(num_simulcast_or_spatial_layers_), |
| 194 | first_decoded_frame_(num_simulcast_or_spatial_layers_, true), |
| 195 | last_decoded_frame_num_(num_simulcast_or_spatial_layers_) { |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 196 | // Sanity checks. |
Rasmus Brandt | 4b381af | 2018-02-07 13:56:16 +0100 | [diff] [blame] | 197 | RTC_CHECK(rtc::TaskQueue::Current()) |
| 198 | << "VideoProcessor must be run on a task queue."; |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 199 | RTC_CHECK(encoder); |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 200 | RTC_CHECK(decoders); |
| 201 | RTC_CHECK_EQ(decoders->size(), num_simulcast_or_spatial_layers_); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 202 | RTC_CHECK(input_frame_reader); |
| 203 | RTC_CHECK(stats); |
| 204 | RTC_CHECK(!encoded_frame_writers || |
| 205 | encoded_frame_writers->size() == num_simulcast_or_spatial_layers_); |
| 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. |
Sergey Silkin | 1723cf9 | 2018-01-22 15:49:55 +0100 | [diff] [blame] | 214 | RTC_CHECK_EQ(encoder_->InitEncode(&config_.codec_settings, |
| 215 | static_cast<int>(config_.NumberOfCores()), |
| 216 | config_.max_payload_size_bytes), |
| 217 | WEBRTC_VIDEO_CODEC_OK); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 218 | for (auto& decoder : *decoders_) { |
| 219 | RTC_CHECK_EQ(decoder->InitDecode(&config_.codec_settings, |
| 220 | static_cast<int>(config_.NumberOfCores())), |
| 221 | WEBRTC_VIDEO_CODEC_OK); |
| 222 | RTC_CHECK_EQ(decoder->RegisterDecodeCompleteCallback(&decode_callback_), |
| 223 | WEBRTC_VIDEO_CODEC_OK); |
| 224 | } |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Åsa Persson | f0c4467 | 2017-10-24 16:03:39 +0200 | [diff] [blame] | 227 | VideoProcessor::~VideoProcessor() { |
brandtr | c8c5905 | 2017-08-21 06:44:16 -0700 | [diff] [blame] | 228 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
| 229 | |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 230 | // Explicitly reset codecs, in case they don't do that themselves when they |
| 231 | // go out of scope. |
brandtr | 77920a4 | 2017-08-11 07:48:15 -0700 | [diff] [blame] | 232 | RTC_CHECK_EQ(encoder_->Release(), WEBRTC_VIDEO_CODEC_OK); |
brandtr | bdd555c | 2017-08-21 01:34:04 -0700 | [diff] [blame] | 233 | encoder_->RegisterEncodeCompleteCallback(nullptr); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 234 | for (auto& decoder : *decoders_) { |
| 235 | RTC_CHECK_EQ(decoder->Release(), WEBRTC_VIDEO_CODEC_OK); |
| 236 | decoder->RegisterDecodeCompleteCallback(nullptr); |
| 237 | } |
| 238 | |
Rasmus Brandt | d00c895 | 2018-03-14 12:29:57 +0100 | [diff] [blame] | 239 | // Sanity check. |
| 240 | RTC_CHECK_LE(input_frames_.size(), kMaxBufferedInputFrames); |
| 241 | |
| 242 | // Deal with manual memory management of EncodedImage's. |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 243 | for (size_t simulcast_svc_idx = 0; |
| 244 | simulcast_svc_idx < num_simulcast_or_spatial_layers_; |
| 245 | ++simulcast_svc_idx) { |
| 246 | uint8_t* buffer = merged_encoded_frames_.at(simulcast_svc_idx)._buffer; |
| 247 | if (buffer) { |
| 248 | delete[] buffer; |
| 249 | } |
| 250 | } |
brandtr | 77920a4 | 2017-08-11 07:48:15 -0700 | [diff] [blame] | 251 | } |
| 252 | |
brandtr | 8935d97 | 2017-09-06 01:53:22 -0700 | [diff] [blame] | 253 | void VideoProcessor::ProcessFrame() { |
brandtr | c8c5905 | 2017-08-21 06:44:16 -0700 | [diff] [blame] | 254 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 255 | const size_t frame_number = last_inputed_frame_num_++; |
asapersson | 654d54c | 2017-02-10 00:16:07 -0800 | [diff] [blame] | 256 | |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 257 | // Get input frame and store for future quality calculation. |
| 258 | rtc::scoped_refptr<I420BufferInterface> buffer = |
| 259 | input_frame_reader_->ReadFrame(); |
brandtr | bdd555c | 2017-08-21 01:34:04 -0700 | [diff] [blame] | 260 | RTC_CHECK(buffer) << "Tried to read too many frames from the file."; |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 261 | const size_t timestamp = |
| 262 | last_inputed_timestamp_ + kVideoPayloadTypeFrequency / framerate_fps_; |
| 263 | VideoFrame input_frame(buffer, static_cast<uint32_t>(timestamp), |
| 264 | static_cast<int64_t>(timestamp / kMsToRtpTimestamp), |
| 265 | webrtc::kVideoRotation_0); |
Rasmus Brandt | d00c895 | 2018-03-14 12:29:57 +0100 | [diff] [blame] | 266 | // Store input frame as a reference for quality calculations. |
| 267 | if (config_.decode && !config_.measure_cpu) { |
| 268 | input_frames_.emplace(frame_number, input_frame); |
| 269 | } |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 270 | last_inputed_timestamp_ = timestamp; |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 271 | |
Rasmus Brandt | 5f7a891 | 2018-02-28 17:17:15 +0100 | [diff] [blame] | 272 | // Create frame statistics object for all simulcast/spatial layers. |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 273 | for (size_t simulcast_svc_idx = 0; |
| 274 | simulcast_svc_idx < num_simulcast_or_spatial_layers_; |
| 275 | ++simulcast_svc_idx) { |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 276 | stats_->AddFrame(timestamp, simulcast_svc_idx); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 277 | } |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 278 | |
| 279 | // For the highest measurement accuracy of the encode time, the start/stop |
| 280 | // time recordings should wrap the Encode call as tightly as possible. |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 281 | const int64_t encode_start_ns = rtc::TimeNanos(); |
| 282 | for (size_t simulcast_svc_idx = 0; |
| 283 | simulcast_svc_idx < num_simulcast_or_spatial_layers_; |
| 284 | ++simulcast_svc_idx) { |
Sergey Silkin | 06a8f30 | 2018-02-20 09:48:26 +0100 | [diff] [blame] | 285 | FrameStatistics* frame_stat = |
| 286 | stats_->GetFrame(frame_number, simulcast_svc_idx); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 287 | frame_stat->encode_start_ns = encode_start_ns; |
| 288 | } |
| 289 | |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 290 | // Encode. |
| 291 | const std::vector<FrameType> frame_types = |
| 292 | config_.FrameTypeForFrame(frame_number); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 293 | const int encode_return_code = |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 294 | encoder_->Encode(input_frame, nullptr, &frame_types); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 295 | for (size_t simulcast_svc_idx = 0; |
| 296 | simulcast_svc_idx < num_simulcast_or_spatial_layers_; |
| 297 | ++simulcast_svc_idx) { |
Sergey Silkin | 06a8f30 | 2018-02-20 09:48:26 +0100 | [diff] [blame] | 298 | FrameStatistics* frame_stat = |
| 299 | stats_->GetFrame(frame_number, simulcast_svc_idx); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 300 | frame_stat->encode_return_code = encode_return_code; |
| 301 | } |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 304 | void VideoProcessor::SetRates(size_t bitrate_kbps, size_t framerate_fps) { |
brandtr | c8c5905 | 2017-08-21 06:44:16 -0700 | [diff] [blame] | 305 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 306 | framerate_fps_ = static_cast<uint32_t>(framerate_fps); |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 307 | bitrate_allocation_ = bitrate_allocator_->GetAllocation( |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 308 | static_cast<uint32_t>(bitrate_kbps * 1000), framerate_fps_); |
| 309 | const int set_rates_result = |
| 310 | encoder_->SetRateAllocation(bitrate_allocation_, framerate_fps_); |
brandtr | bea36fd | 2017-08-07 03:36:54 -0700 | [diff] [blame] | 311 | RTC_DCHECK_GE(set_rates_result, 0) |
brandtr | bdd555c | 2017-08-21 01:34:04 -0700 | [diff] [blame] | 312 | << "Failed to update encoder with new rate " << bitrate_kbps << "."; |
brandtr | bea36fd | 2017-08-07 03:36:54 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 315 | void VideoProcessor::FrameEncoded( |
| 316 | const webrtc::EncodedImage& encoded_image, |
| 317 | const webrtc::CodecSpecificInfo& codec_specific) { |
brandtr | c8c5905 | 2017-08-21 06:44:16 -0700 | [diff] [blame] | 318 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
| 319 | |
brandtr | 32e0d26 | 2017-02-15 05:29:38 -0800 | [diff] [blame] | 320 | // For the highest measurement accuracy of the encode time, the start/stop |
| 321 | // time recordings should wrap the Encode call as tightly as possible. |
Rasmus Brandt | 5f7a891 | 2018-02-28 17:17:15 +0100 | [diff] [blame] | 322 | const int64_t encode_stop_ns = rtc::TimeNanos(); |
brandtr | 32e0d26 | 2017-02-15 05:29:38 -0800 | [diff] [blame] | 323 | |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 324 | const VideoCodecType codec_type = codec_specific.codecType; |
Rasmus Brandt | f7a3558 | 2017-10-24 10:16:33 +0200 | [diff] [blame] | 325 | if (config_.encoded_frame_checker) { |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 326 | config_.encoded_frame_checker->CheckEncodedFrame(codec_type, encoded_image); |
Rasmus Brandt | f7a3558 | 2017-10-24 10:16:33 +0200 | [diff] [blame] | 327 | } |
brandtr | b78bc75 | 2017-02-22 01:26:59 -0800 | [diff] [blame] | 328 | |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 329 | // Layer metadata. |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 330 | size_t simulcast_svc_idx = 0; |
| 331 | size_t temporal_idx = 0; |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 332 | GetLayerIndices(codec_specific, &simulcast_svc_idx, &temporal_idx); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 333 | const size_t frame_wxh = |
| 334 | encoded_image._encodedWidth * encoded_image._encodedHeight; |
| 335 | frame_wxh_to_simulcast_svc_idx_[frame_wxh] = simulcast_svc_idx; |
| 336 | |
Sergey Silkin | 06a8f30 | 2018-02-20 09:48:26 +0100 | [diff] [blame] | 337 | FrameStatistics* frame_stat = stats_->GetFrameWithTimestamp( |
| 338 | encoded_image._timeStamp, simulcast_svc_idx); |
Åsa Persson | a6e7b88 | 2018-01-19 14:57:10 +0100 | [diff] [blame] | 339 | const size_t frame_number = frame_stat->frame_number; |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 340 | |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 341 | // Ensure that the encode order is monotonically increasing, within this |
| 342 | // simulcast/spatial layer. |
| 343 | RTC_CHECK(first_encoded_frame_[simulcast_svc_idx] || |
| 344 | last_encoded_frame_num_[simulcast_svc_idx] < frame_number); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 345 | |
| 346 | // Ensure SVC spatial layers are delivered in ascending order. |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 347 | if (!first_encoded_frame_[simulcast_svc_idx] && |
| 348 | config_.NumberOfSpatialLayers() > 1) { |
| 349 | for (size_t i = 0; i < simulcast_svc_idx; ++i) { |
| 350 | RTC_CHECK_EQ(last_encoded_frame_num_[i], frame_number); |
| 351 | } |
| 352 | for (size_t i = simulcast_svc_idx + 1; i < num_simulcast_or_spatial_layers_; |
| 353 | ++i) { |
| 354 | RTC_CHECK_GT(frame_number, last_encoded_frame_num_[i]); |
| 355 | } |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 356 | } |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 357 | first_encoded_frame_[simulcast_svc_idx] = false; |
| 358 | last_encoded_frame_num_[simulcast_svc_idx] = frame_number; |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 359 | |
brandtr | 8935d97 | 2017-09-06 01:53:22 -0700 | [diff] [blame] | 360 | // Update frame statistics. |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 361 | frame_stat->encoding_successful = true; |
brandtr | 8935d97 | 2017-09-06 01:53:22 -0700 | [diff] [blame] | 362 | frame_stat->encode_time_us = |
| 363 | GetElapsedTimeMicroseconds(frame_stat->encode_start_ns, encode_stop_ns); |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 364 | if (codec_type == kVideoCodecVP9) { |
Sergey Silkin | 06a8f30 | 2018-02-20 09:48:26 +0100 | [diff] [blame] | 365 | const CodecSpecificInfoVP9& vp9_info = codec_specific.codecSpecific.VP9; |
| 366 | frame_stat->inter_layer_predicted = vp9_info.inter_layer_predicted; |
| 367 | |
| 368 | // TODO(ssilkin): Implement bitrate allocation for VP9 SVC. For now set |
| 369 | // target for base layers equal to total target to avoid devision by zero |
| 370 | // at analysis. |
| 371 | frame_stat->target_bitrate_kbps = bitrate_allocation_.get_sum_kbps(); |
| 372 | } else { |
| 373 | frame_stat->target_bitrate_kbps = |
| 374 | (bitrate_allocation_.GetBitrate(simulcast_svc_idx, temporal_idx) + |
| 375 | 500) / |
| 376 | 1000; |
| 377 | } |
Sergey Silkin | d4bc01b | 2018-03-09 14:31:24 +0100 | [diff] [blame] | 378 | frame_stat->length_bytes = encoded_image._length; |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 379 | frame_stat->frame_type = encoded_image._frameType; |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 380 | frame_stat->temporal_layer_idx = temporal_idx; |
| 381 | frame_stat->simulcast_svc_idx = simulcast_svc_idx; |
Sergey Silkin | 3be2a55 | 2018-01-17 15:11:44 +0100 | [diff] [blame] | 382 | frame_stat->max_nalu_size_bytes = GetMaxNaluSizeBytes(encoded_image, config_); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 383 | frame_stat->qp = encoded_image.qp_; |
ssilkin | 612f858 | 2017-09-28 09:23:17 -0700 | [diff] [blame] | 384 | |
Rasmus Brandt | d00c895 | 2018-03-14 12:29:57 +0100 | [diff] [blame] | 385 | if (config_.decode) { |
| 386 | const webrtc::EncodedImage* encoded_image_for_decode = &encoded_image; |
| 387 | if (config_.NumberOfSpatialLayers() > 1) { |
| 388 | encoded_image_for_decode = MergeAndStoreEncodedImageForSvcDecoding( |
| 389 | encoded_image, codec_type, frame_number, simulcast_svc_idx); |
| 390 | } |
| 391 | frame_stat->decode_start_ns = rtc::TimeNanos(); |
| 392 | frame_stat->decode_return_code = |
| 393 | decoders_->at(simulcast_svc_idx) |
| 394 | ->Decode(*encoded_image_for_decode, false, nullptr); |
| 395 | } else { |
| 396 | frame_stat->decode_return_code = WEBRTC_VIDEO_CODEC_NO_OUTPUT; |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 397 | } |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 398 | |
| 399 | if (encoded_frame_writers_) { |
| 400 | RTC_CHECK( |
| 401 | encoded_frame_writers_->at(simulcast_svc_idx) |
| 402 | ->WriteFrame(encoded_image, config_.codec_settings.codecType)); |
brandtr | 8935d97 | 2017-09-06 01:53:22 -0700 | [diff] [blame] | 403 | } |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Sergey Silkin | 64eaa99 | 2017-11-17 14:47:32 +0100 | [diff] [blame] | 406 | void VideoProcessor::FrameDecoded(const VideoFrame& decoded_frame) { |
brandtr | c8c5905 | 2017-08-21 06:44:16 -0700 | [diff] [blame] | 407 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
| 408 | |
brandtr | 32e0d26 | 2017-02-15 05:29:38 -0800 | [diff] [blame] | 409 | // For the highest measurement accuracy of the decode time, the start/stop |
| 410 | // time recordings should wrap the Decode call as tightly as possible. |
Rasmus Brandt | 5f7a891 | 2018-02-28 17:17:15 +0100 | [diff] [blame] | 411 | const int64_t decode_stop_ns = rtc::TimeNanos(); |
brandtr | 8bc9385 | 2017-02-15 05:19:51 -0800 | [diff] [blame] | 412 | |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 413 | // Layer metadata. |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 414 | const size_t simulcast_svc_idx = |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 415 | frame_wxh_to_simulcast_svc_idx_.at(decoded_frame.size()); |
Sergey Silkin | 06a8f30 | 2018-02-20 09:48:26 +0100 | [diff] [blame] | 416 | FrameStatistics* frame_stat = stats_->GetFrameWithTimestamp( |
| 417 | decoded_frame.timestamp(), simulcast_svc_idx); |
Åsa Persson | a6e7b88 | 2018-01-19 14:57:10 +0100 | [diff] [blame] | 418 | const size_t frame_number = frame_stat->frame_number; |
Sergey Silkin | 64eaa99 | 2017-11-17 14:47:32 +0100 | [diff] [blame] | 419 | |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 420 | // Ensure that the decode order is monotonically increasing, within this |
| 421 | // simulcast/spatial layer. |
| 422 | RTC_CHECK(first_decoded_frame_[simulcast_svc_idx] || |
| 423 | last_decoded_frame_num_[simulcast_svc_idx] < frame_number); |
| 424 | first_decoded_frame_[simulcast_svc_idx] = false; |
| 425 | last_decoded_frame_num_[simulcast_svc_idx] = frame_number; |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 426 | |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 427 | // Update frame statistics. |
| 428 | frame_stat->decoding_successful = true; |
| 429 | frame_stat->decode_time_us = |
| 430 | GetElapsedTimeMicroseconds(frame_stat->decode_start_ns, decode_stop_ns); |
| 431 | frame_stat->decoded_width = decoded_frame.width(); |
| 432 | frame_stat->decoded_height = decoded_frame.height(); |
| 433 | |
Sergey Silkin | 64eaa99 | 2017-11-17 14:47:32 +0100 | [diff] [blame] | 434 | // Skip quality metrics calculation to not affect CPU usage. |
| 435 | if (!config_.measure_cpu) { |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 436 | const auto reference_frame = input_frames_.find(frame_number); |
| 437 | RTC_CHECK(reference_frame != input_frames_.cend()) |
| 438 | << "The codecs are either buffering too much, dropping too much, or " |
| 439 | "being too slow relative the input frame rate."; |
Sergey Silkin | 8d3758e | 2018-03-14 11:28:15 +0100 | [diff] [blame] | 440 | CalculateFrameQuality( |
| 441 | *reference_frame->second.video_frame_buffer()->ToI420(), |
| 442 | *decoded_frame.video_frame_buffer()->ToI420(), frame_stat); |
Niels Möller | 718a763 | 2016-06-13 13:06:01 +0200 | [diff] [blame] | 443 | |
Rasmus Brandt | d00c895 | 2018-03-14 12:29:57 +0100 | [diff] [blame] | 444 | // Erase all buffered input frames that we have moved past for all |
| 445 | // simulcast/spatial layers. Never buffer more than |
| 446 | // |kMaxBufferedInputFrames| frames, to protect against long runs of |
| 447 | // consecutive frame drops for a particular layer. |
| 448 | const auto min_last_decoded_frame_num = std::min_element( |
| 449 | last_decoded_frame_num_.cbegin(), last_decoded_frame_num_.cend()); |
| 450 | const size_t min_buffered_frame_num = std::max( |
| 451 | 0, static_cast<int>(frame_number) - kMaxBufferedInputFrames + 1); |
| 452 | RTC_CHECK(min_last_decoded_frame_num != last_decoded_frame_num_.cend()); |
| 453 | const auto input_frames_erase_before = input_frames_.lower_bound( |
| 454 | std::max(*min_last_decoded_frame_num, min_buffered_frame_num)); |
| 455 | input_frames_.erase(input_frames_.cbegin(), input_frames_erase_before); |
| 456 | } |
Sergey Silkin | 64eaa99 | 2017-11-17 14:47:32 +0100 | [diff] [blame] | 457 | |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 458 | if (decoded_frame_writers_) { |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 459 | ExtractI420BufferWithSize(decoded_frame, config_.codec_settings.width, |
| 460 | config_.codec_settings.height, &tmp_i420_buffer_); |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 461 | RTC_CHECK_EQ(tmp_i420_buffer_.size(), |
Rasmus Brandt | 5f7a891 | 2018-02-28 17:17:15 +0100 | [diff] [blame] | 462 | decoded_frame_writers_->at(simulcast_svc_idx)->FrameLength()); |
| 463 | RTC_CHECK(decoded_frame_writers_->at(simulcast_svc_idx) |
Rasmus Brandt | d062a3c | 2018-03-08 16:45:54 +0100 | [diff] [blame] | 464 | ->WriteFrame(tmp_i420_buffer_.data())); |
Sergey Silkin | 64eaa99 | 2017-11-17 14:47:32 +0100 | [diff] [blame] | 465 | } |
Åsa Persson | f0c4467 | 2017-10-24 16:03:39 +0200 | [diff] [blame] | 466 | } |
brandtr | 17b958c | 2017-03-07 01:41:43 -0800 | [diff] [blame] | 467 | |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 468 | const webrtc::EncodedImage* |
| 469 | VideoProcessor::MergeAndStoreEncodedImageForSvcDecoding( |
| 470 | const EncodedImage& encoded_image, |
| 471 | const VideoCodecType codec, |
| 472 | size_t frame_number, |
| 473 | size_t simulcast_svc_idx) { |
| 474 | // Should only be called for SVC. |
| 475 | RTC_CHECK_GT(config_.NumberOfSpatialLayers(), 1); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 476 | |
| 477 | EncodedImage base_image; |
| 478 | RTC_CHECK_EQ(base_image._length, 0); |
| 479 | |
| 480 | // Each SVC layer is decoded with dedicated decoder. Add data of base layers |
| 481 | // to current coded frame buffer. |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 482 | if (simulcast_svc_idx > 0) { |
| 483 | base_image = merged_encoded_frames_.at(simulcast_svc_idx - 1); |
| 484 | RTC_CHECK_EQ(base_image._timeStamp, encoded_image._timeStamp); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 485 | } |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 486 | const size_t payload_size_bytes = base_image._length + encoded_image._length; |
| 487 | const size_t buffer_size_bytes = |
| 488 | payload_size_bytes + EncodedImage::GetBufferPaddingBytes(codec); |
| 489 | |
| 490 | uint8_t* copied_buffer = new uint8_t[buffer_size_bytes]; |
| 491 | RTC_CHECK(copied_buffer); |
| 492 | |
| 493 | if (base_image._length) { |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 494 | RTC_CHECK(base_image._buffer); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 495 | memcpy(copied_buffer, base_image._buffer, base_image._length); |
| 496 | } |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 497 | memcpy(copied_buffer + base_image._length, encoded_image._buffer, |
| 498 | encoded_image._length); |
| 499 | |
| 500 | EncodedImage copied_image = encoded_image; |
| 501 | copied_image = encoded_image; |
| 502 | copied_image._buffer = copied_buffer; |
| 503 | copied_image._length = payload_size_bytes; |
| 504 | copied_image._size = buffer_size_bytes; |
| 505 | |
Rasmus Brandt | 0f1c0bd | 2018-03-12 10:01:16 +0100 | [diff] [blame] | 506 | // Replace previous EncodedImage for this spatial layer. |
| 507 | uint8_t* old_buffer = merged_encoded_frames_.at(simulcast_svc_idx)._buffer; |
| 508 | if (old_buffer) { |
| 509 | delete[] old_buffer; |
| 510 | } |
| 511 | merged_encoded_frames_.at(simulcast_svc_idx) = copied_image; |
| 512 | |
| 513 | return &merged_encoded_frames_.at(simulcast_svc_idx); |
Sergey Silkin | 10d9d59 | 2018-02-01 13:25:17 +0100 | [diff] [blame] | 514 | } |
| 515 | |
kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame] | 516 | } // namespace test |
| 517 | } // namespace webrtc |