blob: 4c89c790a9f9509ef55e0fd3c118a6a9ae0574fe [file] [log] [blame]
kjellander@webrtc.org35a17562011-10-06 06:44:54 +00001/*
marpan@webrtc.orgf4c2de92012-06-05 21:07:28 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
kjellander@webrtc.org35a17562011-10-06 06:44:54 +00003 *
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.org5b97b122011-12-08 07:42:18 +000010
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_VIDEO_CODING_CODECS_TEST_VIDEOPROCESSOR_H_
12#define MODULES_VIDEO_CODING_CODECS_TEST_VIDEOPROCESSOR_H_
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000013
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <stddef.h>
15#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020016
brandtrb57f4262017-08-30 06:29:51 -070017#include <map>
Erik Språng08127a92016-11-16 16:41:30 +010018#include <memory>
Rasmus Brandt001c7822019-03-22 13:41:48 +010019#include <utility>
brandtr17b958c2017-03-07 01:41:43 -080020#include <vector>
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000021
Yves Gerey3e707812018-11-28 16:47:49 +010022#include "absl/types/optional.h"
Artem Titovd15a5752021-02-10 14:31:24 +010023#include "api/sequence_checker.h"
Danil Chapovalovad895282019-03-11 10:28:05 +000024#include "api/task_queue/task_queue_base.h"
Kári Tristan Helgason169005d2018-05-22 13:34:14 +020025#include "api/test/videocodec_test_fixture.h"
Yves Gerey3e707812018-11-28 16:47:49 +010026#include "api/video/encoded_image.h"
Sergey Silkinb72cc6d2020-10-29 08:29:26 +010027#include "api/video/i420_buffer.h"
Yves Gerey3e707812018-11-28 16:47:49 +010028#include "api/video/video_bitrate_allocation.h"
Jiawei Ou4206a0a2018-07-20 15:49:43 -070029#include "api/video/video_bitrate_allocator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "api/video/video_frame.h"
Yves Gerey3e707812018-11-28 16:47:49 +010031#include "api/video_codecs/video_decoder.h"
32#include "api/video_codecs/video_encoder.h"
Yves Gerey3e707812018-11-28 16:47:49 +010033#include "modules/include/module_common_types.h"
Rasmus Brandt7d72d0f2019-03-26 16:56:14 +010034#include "modules/video_coding/codecs/test/videocodec_test_stats_impl.h"
Kári Tristan Helgason169005d2018-05-22 13:34:14 +020035#include "modules/video_coding/include/video_codec_interface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "modules/video_coding/utility/ivf_file_writer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020037#include "rtc_base/buffer.h"
Yves Gerey3e707812018-11-28 16:47:49 +010038#include "rtc_base/checks.h"
Mirko Bonadei20e4c802020-11-23 11:07:42 +010039#include "rtc_base/system/no_unique_address.h"
Yves Gerey3e707812018-11-28 16:47:49 +010040#include "rtc_base/thread_annotations.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020041#include "test/testsupport/frame_reader.h"
42#include "test/testsupport/frame_writer.h"
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000043
44namespace webrtc {
45namespace test {
46
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000047// Handles encoding/decoding of video using the VideoEncoder/VideoDecoder
48// interfaces. This is done in a sequential manner in order to be able to
49// measure times properly.
50// The class processes a frame at the time for the configured input file.
51// It maintains state of where in the source input file the processing is at.
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000052class VideoProcessor {
53 public:
Sergey Silkin10d9d592018-02-01 13:25:17 +010054 using VideoDecoderList = std::vector<std::unique_ptr<VideoDecoder>>;
Rasmus Brandt001c7822019-03-22 13:41:48 +010055 using LayerKey = std::pair<int /* spatial_idx */, int /* temporal_idx */>;
56 using IvfFileWriterMap = std::map<LayerKey, std::unique_ptr<IvfFileWriter>>;
57 // TODO(brandtr): Consider changing FrameWriterList to be a FrameWriterMap,
58 // to be able to save different TLs separately.
Sergey Silkin10d9d592018-02-01 13:25:17 +010059 using FrameWriterList = std::vector<std::unique_ptr<FrameWriter>>;
Sergey Silkinb72cc6d2020-10-29 08:29:26 +010060 using FrameStatistics = VideoCodecTestStats::FrameStatistics;
Sergey Silkin10d9d592018-02-01 13:25:17 +010061
brandtrc4095522017-08-07 08:12:33 -070062 VideoProcessor(webrtc::VideoEncoder* encoder,
Sergey Silkin10d9d592018-02-01 13:25:17 +010063 VideoDecoderList* decoders,
64 FrameReader* input_frame_reader,
Kári Tristan Helgason169005d2018-05-22 13:34:14 +020065 const VideoCodecTestFixture::Config& config,
Rasmus Brandt7d72d0f2019-03-26 16:56:14 +010066 VideoCodecTestStatsImpl* stats,
Rasmus Brandt001c7822019-03-22 13:41:48 +010067 IvfFileWriterMap* encoded_frame_writers,
Sergey Silkin10d9d592018-02-01 13:25:17 +010068 FrameWriterList* decoded_frame_writers);
brandtrc4095522017-08-07 08:12:33 -070069 ~VideoProcessor();
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000070
Byoungchan Lee604fd2f2022-01-21 09:49:39 +090071 VideoProcessor(const VideoProcessor&) = delete;
72 VideoProcessor& operator=(const VideoProcessor&) = delete;
73
Sergey Silkin10d9d592018-02-01 13:25:17 +010074 // Reads a frame and sends it to the encoder. When the encode callback
75 // is received, the encoded frame is buffered. After encoding is finished
76 // buffered frame is sent to decoder. Quality evaluation is done in
77 // the decode callback.
brandtr8935d972017-09-06 01:53:22 -070078 void ProcessFrame();
marpan@webrtc.orgf4c2de92012-06-05 21:07:28 +000079
brandtrbdd555c2017-08-21 01:34:04 -070080 // Updates the encoder with target rates. Must be called at least once.
Sergey Silkin44cec0b2019-07-11 14:20:38 +020081 void SetRates(size_t bitrate_kbps, double framerate_fps);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000082
Sergey Silkinb72cc6d2020-10-29 08:29:26 +010083 // Signals processor to finalize frame processing and handle possible tail
84 // drops. If not called expelicitly, this will be called in dtor. It is
85 // unexpected to get ProcessFrame() or SetRates() calls after Finalize().
86 void Finalize();
87
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000088 private:
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000089 class VideoProcessorEncodeCompleteCallback
pbos@webrtc.org7f7162a2013-07-30 15:18:31 +000090 : public webrtc::EncodedImageCallback {
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000091 public:
brandtrc4095522017-08-07 08:12:33 -070092 explicit VideoProcessorEncodeCompleteCallback(
93 VideoProcessor* video_processor)
brandtrc8c59052017-08-21 06:44:16 -070094 : video_processor_(video_processor),
Danil Chapovalovad895282019-03-11 10:28:05 +000095 task_queue_(TaskQueueBase::Current()) {
Rasmus Brandt5f7a8912018-02-28 17:17:15 +010096 RTC_DCHECK(video_processor_);
Rasmus Brandt4b381af2018-02-07 13:56:16 +010097 RTC_DCHECK(task_queue_);
98 }
brandtrc8c59052017-08-21 06:44:16 -070099
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700100 Result OnEncodedImage(
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000101 const webrtc::EncodedImage& encoded_image,
Danil Chapovalov2549f172020-08-12 17:30:36 +0200102 const webrtc::CodecSpecificInfo* codec_specific_info) override {
brandtr8bc93852017-02-15 05:19:51 -0800103 RTC_CHECK(codec_specific_info);
brandtrc8c59052017-08-21 06:44:16 -0700104
Rasmus Brandt4b381af2018-02-07 13:56:16 +0100105 // Post the callback to the right task queue, if needed.
106 if (!task_queue_->IsCurrent()) {
Danil Chapovalov0be8eba2022-07-06 13:17:54 +0200107 VideoProcessor* video_processor = video_processor_;
108 task_queue_->PostTask([video_processor, encoded_image,
109 codec_specific_info = *codec_specific_info] {
110 video_processor->FrameEncoded(encoded_image, codec_specific_info);
111 });
brandtrc8c59052017-08-21 06:44:16 -0700112 return Result(Result::OK, 0);
113 }
114
Sergey Silkin10d9d592018-02-01 13:25:17 +0100115 video_processor_->FrameEncoded(encoded_image, *codec_specific_info);
brandtr8bc93852017-02-15 05:19:51 -0800116 return Result(Result::OK, 0);
117 }
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000118
119 private:
brandtrc4095522017-08-07 08:12:33 -0700120 VideoProcessor* const video_processor_;
Danil Chapovalovad895282019-03-11 10:28:05 +0000121 TaskQueueBase* const task_queue_;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000122 };
123
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000124 class VideoProcessorDecodeCompleteCallback
philipelcce46fc2015-12-21 03:04:49 -0800125 : public webrtc::DecodedImageCallback {
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000126 public:
brandtrc4095522017-08-07 08:12:33 -0700127 explicit VideoProcessorDecodeCompleteCallback(
Sergey Silkin645e2e02018-04-06 09:42:13 +0200128 VideoProcessor* video_processor,
129 size_t simulcast_svc_idx)
brandtrc8c59052017-08-21 06:44:16 -0700130 : video_processor_(video_processor),
Sergey Silkin645e2e02018-04-06 09:42:13 +0200131 simulcast_svc_idx_(simulcast_svc_idx),
Danil Chapovalovad895282019-03-11 10:28:05 +0000132 task_queue_(TaskQueueBase::Current()) {
Rasmus Brandt5f7a8912018-02-28 17:17:15 +0100133 RTC_DCHECK(video_processor_);
Rasmus Brandt4b381af2018-02-07 13:56:16 +0100134 RTC_DCHECK(task_queue_);
135 }
brandtrc8c59052017-08-21 06:44:16 -0700136
Sami Kalliomäki451b29c2018-07-04 14:33:51 +0200137 int32_t Decoded(webrtc::VideoFrame& image) override;
brandtrc8c59052017-08-21 06:44:16 -0700138
philipelcce46fc2015-12-21 03:04:49 -0800139 int32_t Decoded(webrtc::VideoFrame& image,
140 int64_t decode_time_ms) override {
brandtr6bb8e0e2017-02-20 04:35:52 -0800141 return Decoded(image);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000142 }
brandtrc8c59052017-08-21 06:44:16 -0700143
brandtr6bb8e0e2017-02-20 04:35:52 -0800144 void Decoded(webrtc::VideoFrame& image,
Danil Chapovalov0040b662018-06-18 10:48:16 +0200145 absl::optional<int32_t> decode_time_ms,
146 absl::optional<uint8_t> qp) override {
brandtrbea36fd2017-08-07 03:36:54 -0700147 Decoded(image);
sakalcc452e12017-02-09 04:53:45 -0800148 }
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000149
150 private:
brandtrc4095522017-08-07 08:12:33 -0700151 VideoProcessor* const video_processor_;
Sergey Silkin645e2e02018-04-06 09:42:13 +0200152 const size_t simulcast_svc_idx_;
Danil Chapovalovad895282019-03-11 10:28:05 +0000153 TaskQueueBase* const task_queue_;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000154 };
brandtr8bc93852017-02-15 05:19:51 -0800155
brandtrbdd555c2017-08-21 01:34:04 -0700156 // Invoked by the callback adapter when a frame has completed encoding.
Sergey Silkin10d9d592018-02-01 13:25:17 +0100157 void FrameEncoded(const webrtc::EncodedImage& encoded_image,
158 const webrtc::CodecSpecificInfo& codec_specific);
brandtr8bc93852017-02-15 05:19:51 -0800159
brandtrbdd555c2017-08-21 01:34:04 -0700160 // Invoked by the callback adapter when a frame has completed decoding.
Sergey Silkin645e2e02018-04-06 09:42:13 +0200161 void FrameDecoded(const webrtc::VideoFrame& image, size_t simulcast_svc_idx);
162
163 void DecodeFrame(const EncodedImage& encoded_image, size_t simulcast_svc_idx);
brandtr8bc93852017-02-15 05:19:51 -0800164
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100165 // In order to supply the SVC decoders with super frames containing all
166 // lower layer frames, we merge and store the layer frames in this method.
Sergey Silkin645e2e02018-04-06 09:42:13 +0200167 const webrtc::EncodedImage* BuildAndStoreSuperframe(
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100168 const EncodedImage& encoded_image,
Ali Tofigh1e157a92022-01-31 11:08:24 +0100169 VideoCodecType codec,
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100170 size_t frame_number,
Sergey Silkin645e2e02018-04-06 09:42:13 +0200171 size_t simulcast_svc_idx,
172 bool inter_layer_predicted) RTC_RUN_ON(sequence_checker_);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100173
Sergey Silkinb72cc6d2020-10-29 08:29:26 +0100174 void CalcFrameQuality(const I420BufferInterface& decoded_frame,
175 FrameStatistics* frame_stat);
176
177 void WriteDecodedFrame(const I420BufferInterface& decoded_frame,
178 FrameWriter& frame_writer);
179
180 void HandleTailDrops();
181
182 // Test config.
183 const VideoCodecTestFixture::Config config_;
Sergey Silkin10d9d592018-02-01 13:25:17 +0100184 const size_t num_simulcast_or_spatial_layers_;
Sergey Silkinb72cc6d2020-10-29 08:29:26 +0100185 const bool analyze_frame_quality_;
186
187 // Frame statistics.
Rasmus Brandt7d72d0f2019-03-26 16:56:14 +0100188 VideoCodecTestStatsImpl* const stats_;
Sergey Silkin10d9d592018-02-01 13:25:17 +0100189
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100190 // Codecs.
brandtr8bc93852017-02-15 05:19:51 -0800191 webrtc::VideoEncoder* const encoder_;
Sergey Silkin10d9d592018-02-01 13:25:17 +0100192 VideoDecoderList* const decoders_;
brandtraebc61e2017-02-28 07:13:47 -0800193 const std::unique_ptr<VideoBitrateAllocator> bitrate_allocator_;
Erik Språng566124a2018-04-23 12:32:22 +0200194 VideoBitrateAllocation bitrate_allocation_ RTC_GUARDED_BY(sequence_checker_);
Sergey Silkin44cec0b2019-07-11 14:20:38 +0200195 double framerate_fps_ RTC_GUARDED_BY(sequence_checker_);
brandtraebc61e2017-02-28 07:13:47 -0800196
197 // Adapters for the codec callbacks.
brandtrbdd555c2017-08-21 01:34:04 -0700198 VideoProcessorEncodeCompleteCallback encode_callback_;
Sergey Silkin645e2e02018-04-06 09:42:13 +0200199 // Assign separate callback object to each decoder. This allows us to identify
200 // decoded layer in frame decode callback.
201 // simulcast_svc_idx -> decode callback.
202 std::vector<std::unique_ptr<VideoProcessorDecodeCompleteCallback>>
203 decode_callback_;
brandtraebc61e2017-02-28 07:13:47 -0800204
Artem Titovdcd7fc72021-08-09 13:02:57 +0200205 // Each call to ProcessFrame() will read one frame from `input_frame_reader_`.
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100206 FrameReader* const input_frame_reader_;
207
208 // Input frames are used as reference for frame quality evaluations.
Sergey Silkin64eaa992017-11-17 14:47:32 +0100209 // Async codecs might queue frames. To handle that we keep input frame
210 // and release it after corresponding coded frame is decoded and quality
211 // measurement is done.
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100212 // frame_number -> frame.
213 std::map<size_t, VideoFrame> input_frames_ RTC_GUARDED_BY(sequence_checker_);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100214
215 // Encoder delivers coded frame layer-by-layer. We store coded frames and
216 // then, after all layers are encoded, decode them. Such separation of
217 // frame processing on superframe level simplifies encoding/decoding time
218 // measurement.
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100219 // simulcast_svc_idx -> merged SVC encoded frame.
220 std::vector<EncodedImage> merged_encoded_frames_
Sergey Silkin10d9d592018-02-01 13:25:17 +0100221 RTC_GUARDED_BY(sequence_checker_);
222
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100223 // These (optional) file writers are used to persistently store the encoded
224 // and decoded bitstreams. Each frame writer is enabled by being non-null.
Rasmus Brandt001c7822019-03-22 13:41:48 +0100225 IvfFileWriterMap* const encoded_frame_writers_;
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100226 FrameWriterList* const decoded_frame_writers_;
brandtr8bc93852017-02-15 05:19:51 -0800227
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100228 // Metadata for inputed/encoded/decoded frames. Used for frame identification,
229 // frame drop detection, etc. We assume that encoded/decoded frames are
230 // ordered within each simulcast/spatial layer, but we do not make any
231 // assumptions of frame ordering between layers.
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100232 size_t last_inputed_frame_num_ RTC_GUARDED_BY(sequence_checker_);
233 size_t last_inputed_timestamp_ RTC_GUARDED_BY(sequence_checker_);
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100234 // simulcast_svc_idx -> encode status.
235 std::vector<bool> first_encoded_frame_ RTC_GUARDED_BY(sequence_checker_);
236 // simulcast_svc_idx -> frame_number.
237 std::vector<size_t> last_encoded_frame_num_ RTC_GUARDED_BY(sequence_checker_);
238 // simulcast_svc_idx -> decode status.
239 std::vector<bool> first_decoded_frame_ RTC_GUARDED_BY(sequence_checker_);
240 // simulcast_svc_idx -> frame_number.
241 std::vector<size_t> last_decoded_frame_num_ RTC_GUARDED_BY(sequence_checker_);
Sergey Silkin56138792018-05-02 10:50:55 +0200242 // simulcast_svc_idx -> buffer.
Sergey Silkinb72cc6d2020-10-29 08:29:26 +0100243 std::vector<rtc::scoped_refptr<I420Buffer>> last_decoded_frame_buffer_
Sergey Silkin56138792018-05-02 10:50:55 +0200244 RTC_GUARDED_BY(sequence_checker_);
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100245
Sergey Silkinc89eed92018-04-01 23:57:51 +0200246 // Time spent in frame encode callback. It is accumulated for layers and
247 // reset when frame encode starts. When next layer is encoded post-encode time
248 // is substracted from measured encode time. Thus we get pure encode time.
249 int64_t post_encode_time_ns_ RTC_GUARDED_BY(sequence_checker_);
250
Sergey Silkinb72cc6d2020-10-29 08:29:26 +0100251 // Indicates whether Finalize() was called or not.
252 bool is_finalized_ RTC_GUARDED_BY(sequence_checker_);
253
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100254 // This class must be operated on a TaskQueue.
Mirko Bonadei20e4c802020-11-23 11:07:42 +0100255 RTC_NO_UNIQUE_ADDRESS SequenceChecker sequence_checker_;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000256};
257
258} // namespace test
259} // namespace webrtc
260
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200261#endif // MODULES_VIDEO_CODING_CODECS_TEST_VIDEOPROCESSOR_H_