blob: fe0211de7d5009a7b010ef35653361aae55b37d0 [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
Danil Chapovalov471783f2019-03-11 14:26:02 +010022#include "absl/memory/memory.h"
Yves Gerey3e707812018-11-28 16:47:49 +010023#include "absl/types/optional.h"
Danil Chapovalov471783f2019-03-11 14:26:02 +010024#include "api/task_queue/queued_task.h"
Danil Chapovalovad895282019-03-11 10:28:05 +000025#include "api/task_queue/task_queue_base.h"
Kári Tristan Helgason169005d2018-05-22 13:34:14 +020026#include "api/test/videocodec_test_fixture.h"
Yves Gerey3e707812018-11-28 16:47:49 +010027#include "api/video/encoded_image.h"
28#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"
Steve Anton10542f22019-01-11 09:11:00 -080039#include "rtc_base/constructor_magic.h"
Sebastian Janssonb55015e2019-04-09 13:44:04 +020040#include "rtc_base/synchronization/sequence_checker.h"
Yves Gerey3e707812018-11-28 16:47:49 +010041#include "rtc_base/thread_annotations.h"
42#include "rtc_base/thread_checker.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020043#include "test/testsupport/frame_reader.h"
44#include "test/testsupport/frame_writer.h"
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000045
46namespace webrtc {
47namespace test {
48
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000049// Handles encoding/decoding of video using the VideoEncoder/VideoDecoder
50// interfaces. This is done in a sequential manner in order to be able to
51// measure times properly.
52// The class processes a frame at the time for the configured input file.
53// It maintains state of where in the source input file the processing is at.
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000054class VideoProcessor {
55 public:
Sergey Silkin10d9d592018-02-01 13:25:17 +010056 using VideoDecoderList = std::vector<std::unique_ptr<VideoDecoder>>;
Rasmus Brandt001c7822019-03-22 13:41:48 +010057 using LayerKey = std::pair<int /* spatial_idx */, int /* temporal_idx */>;
58 using IvfFileWriterMap = std::map<LayerKey, std::unique_ptr<IvfFileWriter>>;
59 // TODO(brandtr): Consider changing FrameWriterList to be a FrameWriterMap,
60 // to be able to save different TLs separately.
Sergey Silkin10d9d592018-02-01 13:25:17 +010061 using FrameWriterList = std::vector<std::unique_ptr<FrameWriter>>;
62
brandtrc4095522017-08-07 08:12:33 -070063 VideoProcessor(webrtc::VideoEncoder* encoder,
Sergey Silkin10d9d592018-02-01 13:25:17 +010064 VideoDecoderList* decoders,
65 FrameReader* input_frame_reader,
Kári Tristan Helgason169005d2018-05-22 13:34:14 +020066 const VideoCodecTestFixture::Config& config,
Rasmus Brandt7d72d0f2019-03-26 16:56:14 +010067 VideoCodecTestStatsImpl* stats,
Rasmus Brandt001c7822019-03-22 13:41:48 +010068 IvfFileWriterMap* encoded_frame_writers,
Sergey Silkin10d9d592018-02-01 13:25:17 +010069 FrameWriterList* decoded_frame_writers);
brandtrc4095522017-08-07 08:12:33 -070070 ~VideoProcessor();
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000071
Sergey Silkin10d9d592018-02-01 13:25:17 +010072 // Reads a frame and sends it to the encoder. When the encode callback
73 // is received, the encoded frame is buffered. After encoding is finished
74 // buffered frame is sent to decoder. Quality evaluation is done in
75 // the decode callback.
brandtr8935d972017-09-06 01:53:22 -070076 void ProcessFrame();
marpan@webrtc.orgf4c2de92012-06-05 21:07:28 +000077
brandtrbdd555c2017-08-21 01:34:04 -070078 // Updates the encoder with target rates. Must be called at least once.
Sergey Silkin44cec0b2019-07-11 14:20:38 +020079 void SetRates(size_t bitrate_kbps, double framerate_fps);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000080
81 private:
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000082 class VideoProcessorEncodeCompleteCallback
pbos@webrtc.org7f7162a2013-07-30 15:18:31 +000083 : public webrtc::EncodedImageCallback {
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000084 public:
brandtrc4095522017-08-07 08:12:33 -070085 explicit VideoProcessorEncodeCompleteCallback(
86 VideoProcessor* video_processor)
brandtrc8c59052017-08-21 06:44:16 -070087 : video_processor_(video_processor),
Danil Chapovalovad895282019-03-11 10:28:05 +000088 task_queue_(TaskQueueBase::Current()) {
Rasmus Brandt5f7a8912018-02-28 17:17:15 +010089 RTC_DCHECK(video_processor_);
Rasmus Brandt4b381af2018-02-07 13:56:16 +010090 RTC_DCHECK(task_queue_);
91 }
brandtrc8c59052017-08-21 06:44:16 -070092
Sergey Ulanov525df3f2016-08-02 17:46:41 -070093 Result OnEncodedImage(
pbos@webrtc.org273a4142014-12-01 15:23:21 +000094 const webrtc::EncodedImage& encoded_image,
95 const webrtc::CodecSpecificInfo* codec_specific_info,
brandtr8bc93852017-02-15 05:19:51 -080096 const webrtc::RTPFragmentationHeader* fragmentation) override {
brandtr8bc93852017-02-15 05:19:51 -080097 RTC_CHECK(codec_specific_info);
brandtrc8c59052017-08-21 06:44:16 -070098
Rasmus Brandt4b381af2018-02-07 13:56:16 +010099 // Post the callback to the right task queue, if needed.
100 if (!task_queue_->IsCurrent()) {
Danil Chapovalov471783f2019-03-11 14:26:02 +0100101 task_queue_->PostTask(absl::make_unique<EncodeCallbackTask>(
102 video_processor_, encoded_image, codec_specific_info));
brandtrc8c59052017-08-21 06:44:16 -0700103 return Result(Result::OK, 0);
104 }
105
Sergey Silkin10d9d592018-02-01 13:25:17 +0100106 video_processor_->FrameEncoded(encoded_image, *codec_specific_info);
brandtr8bc93852017-02-15 05:19:51 -0800107 return Result(Result::OK, 0);
108 }
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000109
110 private:
Danil Chapovalov471783f2019-03-11 14:26:02 +0100111 class EncodeCallbackTask : public QueuedTask {
brandtrc8c59052017-08-21 06:44:16 -0700112 public:
113 EncodeCallbackTask(VideoProcessor* video_processor,
114 const webrtc::EncodedImage& encoded_image,
brandtr45535622017-08-22 03:33:11 -0700115 const webrtc::CodecSpecificInfo* codec_specific_info)
brandtrc8c59052017-08-21 06:44:16 -0700116 : video_processor_(video_processor),
brandtrc8c59052017-08-21 06:44:16 -0700117 encoded_image_(encoded_image),
118 codec_specific_info_(*codec_specific_info) {
Niels Möller663844d2019-02-14 16:15:54 +0100119 encoded_image_.Retain();
brandtrc8c59052017-08-21 06:44:16 -0700120 }
121
122 bool Run() override {
Sergey Silkin10d9d592018-02-01 13:25:17 +0100123 video_processor_->FrameEncoded(encoded_image_, codec_specific_info_);
brandtrc8c59052017-08-21 06:44:16 -0700124 return true;
125 }
126
127 private:
128 VideoProcessor* const video_processor_;
brandtrc8c59052017-08-21 06:44:16 -0700129 webrtc::EncodedImage encoded_image_;
130 const webrtc::CodecSpecificInfo codec_specific_info_;
brandtrc8c59052017-08-21 06:44:16 -0700131 };
132
brandtrc4095522017-08-07 08:12:33 -0700133 VideoProcessor* const video_processor_;
Danil Chapovalovad895282019-03-11 10:28:05 +0000134 TaskQueueBase* const task_queue_;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000135 };
136
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000137 class VideoProcessorDecodeCompleteCallback
philipelcce46fc2015-12-21 03:04:49 -0800138 : public webrtc::DecodedImageCallback {
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000139 public:
brandtrc4095522017-08-07 08:12:33 -0700140 explicit VideoProcessorDecodeCompleteCallback(
Sergey Silkin645e2e02018-04-06 09:42:13 +0200141 VideoProcessor* video_processor,
142 size_t simulcast_svc_idx)
brandtrc8c59052017-08-21 06:44:16 -0700143 : video_processor_(video_processor),
Sergey Silkin645e2e02018-04-06 09:42:13 +0200144 simulcast_svc_idx_(simulcast_svc_idx),
Danil Chapovalovad895282019-03-11 10:28:05 +0000145 task_queue_(TaskQueueBase::Current()) {
Rasmus Brandt5f7a8912018-02-28 17:17:15 +0100146 RTC_DCHECK(video_processor_);
Rasmus Brandt4b381af2018-02-07 13:56:16 +0100147 RTC_DCHECK(task_queue_);
148 }
brandtrc8c59052017-08-21 06:44:16 -0700149
Sami Kalliomäki451b29c2018-07-04 14:33:51 +0200150 int32_t Decoded(webrtc::VideoFrame& image) override;
brandtrc8c59052017-08-21 06:44:16 -0700151
philipelcce46fc2015-12-21 03:04:49 -0800152 int32_t Decoded(webrtc::VideoFrame& image,
153 int64_t decode_time_ms) override {
brandtr6bb8e0e2017-02-20 04:35:52 -0800154 return Decoded(image);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000155 }
brandtrc8c59052017-08-21 06:44:16 -0700156
brandtr6bb8e0e2017-02-20 04:35:52 -0800157 void Decoded(webrtc::VideoFrame& image,
Danil Chapovalov0040b662018-06-18 10:48:16 +0200158 absl::optional<int32_t> decode_time_ms,
159 absl::optional<uint8_t> qp) override {
brandtrbea36fd2017-08-07 03:36:54 -0700160 Decoded(image);
sakalcc452e12017-02-09 04:53:45 -0800161 }
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000162
163 private:
brandtrc4095522017-08-07 08:12:33 -0700164 VideoProcessor* const video_processor_;
Sergey Silkin645e2e02018-04-06 09:42:13 +0200165 const size_t simulcast_svc_idx_;
Danil Chapovalovad895282019-03-11 10:28:05 +0000166 TaskQueueBase* const task_queue_;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000167 };
brandtr8bc93852017-02-15 05:19:51 -0800168
brandtrbdd555c2017-08-21 01:34:04 -0700169 // Invoked by the callback adapter when a frame has completed encoding.
Sergey Silkin10d9d592018-02-01 13:25:17 +0100170 void FrameEncoded(const webrtc::EncodedImage& encoded_image,
171 const webrtc::CodecSpecificInfo& codec_specific);
brandtr8bc93852017-02-15 05:19:51 -0800172
brandtrbdd555c2017-08-21 01:34:04 -0700173 // Invoked by the callback adapter when a frame has completed decoding.
Sergey Silkin645e2e02018-04-06 09:42:13 +0200174 void FrameDecoded(const webrtc::VideoFrame& image, size_t simulcast_svc_idx);
175
176 void DecodeFrame(const EncodedImage& encoded_image, size_t simulcast_svc_idx);
brandtr8bc93852017-02-15 05:19:51 -0800177
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100178 // In order to supply the SVC decoders with super frames containing all
179 // lower layer frames, we merge and store the layer frames in this method.
Sergey Silkin645e2e02018-04-06 09:42:13 +0200180 const webrtc::EncodedImage* BuildAndStoreSuperframe(
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100181 const EncodedImage& encoded_image,
182 const VideoCodecType codec,
183 size_t frame_number,
Sergey Silkin645e2e02018-04-06 09:42:13 +0200184 size_t simulcast_svc_idx,
185 bool inter_layer_predicted) RTC_RUN_ON(sequence_checker_);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100186
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100187 // Test input/output.
Kári Tristan Helgason169005d2018-05-22 13:34:14 +0200188 VideoCodecTestFixture::Config config_ RTC_GUARDED_BY(sequence_checker_);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100189 const size_t num_simulcast_or_spatial_layers_;
Rasmus Brandt7d72d0f2019-03-26 16:56:14 +0100190 VideoCodecTestStatsImpl* const stats_;
Sergey Silkin10d9d592018-02-01 13:25:17 +0100191
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100192 // Codecs.
brandtr8bc93852017-02-15 05:19:51 -0800193 webrtc::VideoEncoder* const encoder_;
Sergey Silkin10d9d592018-02-01 13:25:17 +0100194 VideoDecoderList* const decoders_;
brandtraebc61e2017-02-28 07:13:47 -0800195 const std::unique_ptr<VideoBitrateAllocator> bitrate_allocator_;
Erik Språng566124a2018-04-23 12:32:22 +0200196 VideoBitrateAllocation bitrate_allocation_ RTC_GUARDED_BY(sequence_checker_);
Sergey Silkin44cec0b2019-07-11 14:20:38 +0200197 double framerate_fps_ RTC_GUARDED_BY(sequence_checker_);
brandtraebc61e2017-02-28 07:13:47 -0800198
199 // Adapters for the codec callbacks.
brandtrbdd555c2017-08-21 01:34:04 -0700200 VideoProcessorEncodeCompleteCallback encode_callback_;
Sergey Silkin645e2e02018-04-06 09:42:13 +0200201 // Assign separate callback object to each decoder. This allows us to identify
202 // decoded layer in frame decode callback.
203 // simulcast_svc_idx -> decode callback.
204 std::vector<std::unique_ptr<VideoProcessorDecodeCompleteCallback>>
205 decode_callback_;
brandtraebc61e2017-02-28 07:13:47 -0800206
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100207 // Each call to ProcessFrame() will read one frame from |input_frame_reader_|.
208 FrameReader* const input_frame_reader_;
209
210 // Input frames are used as reference for frame quality evaluations.
Sergey Silkin64eaa992017-11-17 14:47:32 +0100211 // Async codecs might queue frames. To handle that we keep input frame
212 // and release it after corresponding coded frame is decoded and quality
213 // measurement is done.
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100214 // frame_number -> frame.
215 std::map<size_t, VideoFrame> input_frames_ RTC_GUARDED_BY(sequence_checker_);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100216
217 // Encoder delivers coded frame layer-by-layer. We store coded frames and
218 // then, after all layers are encoded, decode them. Such separation of
219 // frame processing on superframe level simplifies encoding/decoding time
220 // measurement.
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100221 // simulcast_svc_idx -> merged SVC encoded frame.
222 std::vector<EncodedImage> merged_encoded_frames_
Sergey Silkin10d9d592018-02-01 13:25:17 +0100223 RTC_GUARDED_BY(sequence_checker_);
224
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100225 // These (optional) file writers are used to persistently store the encoded
226 // and decoded bitstreams. Each frame writer is enabled by being non-null.
Rasmus Brandt001c7822019-03-22 13:41:48 +0100227 IvfFileWriterMap* const encoded_frame_writers_;
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100228 FrameWriterList* const decoded_frame_writers_;
brandtr8bc93852017-02-15 05:19:51 -0800229
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100230 // Metadata for inputed/encoded/decoded frames. Used for frame identification,
231 // frame drop detection, etc. We assume that encoded/decoded frames are
232 // ordered within each simulcast/spatial layer, but we do not make any
233 // assumptions of frame ordering between layers.
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100234 size_t last_inputed_frame_num_ RTC_GUARDED_BY(sequence_checker_);
235 size_t last_inputed_timestamp_ RTC_GUARDED_BY(sequence_checker_);
Rasmus Brandt0f1c0bd2018-03-12 10:01:16 +0100236 // simulcast_svc_idx -> encode status.
237 std::vector<bool> first_encoded_frame_ RTC_GUARDED_BY(sequence_checker_);
238 // simulcast_svc_idx -> frame_number.
239 std::vector<size_t> last_encoded_frame_num_ RTC_GUARDED_BY(sequence_checker_);
240 // simulcast_svc_idx -> decode status.
241 std::vector<bool> first_decoded_frame_ RTC_GUARDED_BY(sequence_checker_);
242 // simulcast_svc_idx -> frame_number.
243 std::vector<size_t> last_decoded_frame_num_ RTC_GUARDED_BY(sequence_checker_);
Sergey Silkin56138792018-05-02 10:50:55 +0200244 // simulcast_svc_idx -> buffer.
245 std::vector<rtc::Buffer> decoded_frame_buffer_
246 RTC_GUARDED_BY(sequence_checker_);
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100247
Sergey Silkinc89eed92018-04-01 23:57:51 +0200248 // Time spent in frame encode callback. It is accumulated for layers and
249 // reset when frame encode starts. When next layer is encoded post-encode time
250 // is substracted from measured encode time. Thus we get pure encode time.
251 int64_t post_encode_time_ns_ RTC_GUARDED_BY(sequence_checker_);
252
Rasmus Brandtd062a3c2018-03-08 16:45:54 +0100253 // This class must be operated on a TaskQueue.
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200254 SequenceChecker sequence_checker_;
brandtrbdd555c2017-08-21 01:34:04 -0700255
256 RTC_DISALLOW_COPY_AND_ASSIGN(VideoProcessor);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000257};
258
259} // namespace test
260} // namespace webrtc
261
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200262#endif // MODULES_VIDEO_CODING_CODECS_TEST_VIDEOPROCESSOR_H_