blob: 0d09c2ddb07d8fbc55af4cdd460ff55c0b9b8ec3 [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
brandtrb57f4262017-08-30 06:29:51 -070014#include <map>
Erik Språng08127a92016-11-16 16:41:30 +010015#include <memory>
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000016#include <string>
brandtr17b958c2017-03-07 01:41:43 -080017#include <vector>
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000018
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "api/video/video_frame.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/video_coding/codecs/test/stats.h"
Åsa Persson2d27fb52017-10-19 14:05:50 +020021#include "modules/video_coding/codecs/test/test_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/video_coding/utility/ivf_file_writer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/constructormagic.h"
25#include "rtc_base/sequenced_task_checker.h"
26#include "rtc_base/task_queue.h"
27#include "test/testsupport/frame_reader.h"
28#include "test/testsupport/frame_writer.h"
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000029
30namespace webrtc {
Erik Språng08127a92016-11-16 16:41:30 +010031
32class VideoBitrateAllocator;
33
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000034namespace test {
35
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000036// Handles encoding/decoding of video using the VideoEncoder/VideoDecoder
37// interfaces. This is done in a sequential manner in order to be able to
38// measure times properly.
39// The class processes a frame at the time for the configured input file.
40// It maintains state of where in the source input file the processing is at.
41//
Åsa Persson7173cf22017-10-19 12:14:09 +020042// Note this class is not thread safe and is meant for simple testing purposes.
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000043class VideoProcessor {
44 public:
Sergey Silkin10d9d592018-02-01 13:25:17 +010045 using VideoDecoderList = std::vector<std::unique_ptr<VideoDecoder>>;
46 using IvfFileWriterList = std::vector<std::unique_ptr<IvfFileWriter>>;
47 using FrameWriterList = std::vector<std::unique_ptr<FrameWriter>>;
48
brandtrc4095522017-08-07 08:12:33 -070049 VideoProcessor(webrtc::VideoEncoder* encoder,
Sergey Silkin10d9d592018-02-01 13:25:17 +010050 VideoDecoderList* decoders,
51 FrameReader* input_frame_reader,
brandtrc4095522017-08-07 08:12:33 -070052 const TestConfig& config,
Sergey Silkin06a8f302018-02-20 09:48:26 +010053 Stats* stats,
Sergey Silkin10d9d592018-02-01 13:25:17 +010054 IvfFileWriterList* encoded_frame_writers,
55 FrameWriterList* decoded_frame_writers);
brandtrc4095522017-08-07 08:12:33 -070056 ~VideoProcessor();
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000057
Sergey Silkin10d9d592018-02-01 13:25:17 +010058 // Reads a frame and sends it to the encoder. When the encode callback
59 // is received, the encoded frame is buffered. After encoding is finished
60 // buffered frame is sent to decoder. Quality evaluation is done in
61 // the decode callback.
brandtr8935d972017-09-06 01:53:22 -070062 void ProcessFrame();
marpan@webrtc.orgf4c2de92012-06-05 21:07:28 +000063
brandtrbdd555c2017-08-21 01:34:04 -070064 // Updates the encoder with target rates. Must be called at least once.
Sergey Silkin3be2a552018-01-17 15:11:44 +010065 void SetRates(size_t bitrate_kbps, size_t framerate_fps);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000066
67 private:
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000068 class VideoProcessorEncodeCompleteCallback
pbos@webrtc.org7f7162a2013-07-30 15:18:31 +000069 : public webrtc::EncodedImageCallback {
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000070 public:
brandtrc4095522017-08-07 08:12:33 -070071 explicit VideoProcessorEncodeCompleteCallback(
72 VideoProcessor* video_processor)
brandtrc8c59052017-08-21 06:44:16 -070073 : video_processor_(video_processor),
Rasmus Brandt4b381af2018-02-07 13:56:16 +010074 task_queue_(rtc::TaskQueue::Current()) {
Rasmus Brandt5f7a8912018-02-28 17:17:15 +010075 RTC_DCHECK(video_processor_);
Rasmus Brandt4b381af2018-02-07 13:56:16 +010076 RTC_DCHECK(task_queue_);
77 }
brandtrc8c59052017-08-21 06:44:16 -070078
Sergey Ulanov525df3f2016-08-02 17:46:41 -070079 Result OnEncodedImage(
pbos@webrtc.org273a4142014-12-01 15:23:21 +000080 const webrtc::EncodedImage& encoded_image,
81 const webrtc::CodecSpecificInfo* codec_specific_info,
brandtr8bc93852017-02-15 05:19:51 -080082 const webrtc::RTPFragmentationHeader* fragmentation) override {
brandtr8bc93852017-02-15 05:19:51 -080083 RTC_CHECK(codec_specific_info);
brandtrc8c59052017-08-21 06:44:16 -070084
Rasmus Brandt4b381af2018-02-07 13:56:16 +010085 // Post the callback to the right task queue, if needed.
86 if (!task_queue_->IsCurrent()) {
brandtr45535622017-08-22 03:33:11 -070087 task_queue_->PostTask(
88 std::unique_ptr<rtc::QueuedTask>(new EncodeCallbackTask(
89 video_processor_, encoded_image, codec_specific_info)));
brandtrc8c59052017-08-21 06:44:16 -070090 return Result(Result::OK, 0);
91 }
92
Sergey Silkin10d9d592018-02-01 13:25:17 +010093 video_processor_->FrameEncoded(encoded_image, *codec_specific_info);
brandtr8bc93852017-02-15 05:19:51 -080094 return Result(Result::OK, 0);
95 }
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000096
97 private:
brandtrc8c59052017-08-21 06:44:16 -070098 class EncodeCallbackTask : public rtc::QueuedTask {
99 public:
100 EncodeCallbackTask(VideoProcessor* video_processor,
101 const webrtc::EncodedImage& encoded_image,
brandtr45535622017-08-22 03:33:11 -0700102 const webrtc::CodecSpecificInfo* codec_specific_info)
brandtrc8c59052017-08-21 06:44:16 -0700103 : video_processor_(video_processor),
104 buffer_(encoded_image._buffer, encoded_image._length),
105 encoded_image_(encoded_image),
106 codec_specific_info_(*codec_specific_info) {
107 encoded_image_._buffer = buffer_.data();
brandtrc8c59052017-08-21 06:44:16 -0700108 }
109
110 bool Run() override {
Sergey Silkin10d9d592018-02-01 13:25:17 +0100111 video_processor_->FrameEncoded(encoded_image_, codec_specific_info_);
brandtrc8c59052017-08-21 06:44:16 -0700112 return true;
113 }
114
115 private:
116 VideoProcessor* const video_processor_;
117 rtc::Buffer buffer_;
118 webrtc::EncodedImage encoded_image_;
119 const webrtc::CodecSpecificInfo codec_specific_info_;
brandtrc8c59052017-08-21 06:44:16 -0700120 };
121
brandtrc4095522017-08-07 08:12:33 -0700122 VideoProcessor* const video_processor_;
brandtrc8c59052017-08-21 06:44:16 -0700123 rtc::TaskQueue* const task_queue_;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000124 };
125
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000126 class VideoProcessorDecodeCompleteCallback
philipelcce46fc2015-12-21 03:04:49 -0800127 : public webrtc::DecodedImageCallback {
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000128 public:
brandtrc4095522017-08-07 08:12:33 -0700129 explicit VideoProcessorDecodeCompleteCallback(
130 VideoProcessor* video_processor)
brandtrc8c59052017-08-21 06:44:16 -0700131 : video_processor_(video_processor),
Rasmus Brandt4b381af2018-02-07 13:56:16 +0100132 task_queue_(rtc::TaskQueue::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
brandtr8bc93852017-02-15 05:19:51 -0800137 int32_t Decoded(webrtc::VideoFrame& image) override {
Rasmus Brandt4b381af2018-02-07 13:56:16 +0100138 // Post the callback to the right task queue, if needed.
139 if (!task_queue_->IsCurrent()) {
brandtrc8c59052017-08-21 06:44:16 -0700140 task_queue_->PostTask(
141 [this, image]() { video_processor_->FrameDecoded(image); });
142 return 0;
143 }
brandtr8bc93852017-02-15 05:19:51 -0800144 video_processor_->FrameDecoded(image);
145 return 0;
146 }
brandtrc8c59052017-08-21 06:44:16 -0700147
philipelcce46fc2015-12-21 03:04:49 -0800148 int32_t Decoded(webrtc::VideoFrame& image,
149 int64_t decode_time_ms) override {
brandtr6bb8e0e2017-02-20 04:35:52 -0800150 return Decoded(image);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000151 }
brandtrc8c59052017-08-21 06:44:16 -0700152
brandtr6bb8e0e2017-02-20 04:35:52 -0800153 void Decoded(webrtc::VideoFrame& image,
sakalcc452e12017-02-09 04:53:45 -0800154 rtc::Optional<int32_t> decode_time_ms,
155 rtc::Optional<uint8_t> qp) override {
brandtrbea36fd2017-08-07 03:36:54 -0700156 Decoded(image);
sakalcc452e12017-02-09 04:53:45 -0800157 }
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000158
159 private:
brandtrc4095522017-08-07 08:12:33 -0700160 VideoProcessor* const video_processor_;
brandtrc8c59052017-08-21 06:44:16 -0700161 rtc::TaskQueue* const task_queue_;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000162 };
brandtr8bc93852017-02-15 05:19:51 -0800163
brandtrbdd555c2017-08-21 01:34:04 -0700164 // Invoked by the callback adapter when a frame has completed encoding.
Sergey Silkin10d9d592018-02-01 13:25:17 +0100165 void FrameEncoded(const webrtc::EncodedImage& encoded_image,
166 const webrtc::CodecSpecificInfo& codec_specific);
brandtr8bc93852017-02-15 05:19:51 -0800167
brandtrbdd555c2017-08-21 01:34:04 -0700168 // Invoked by the callback adapter when a frame has completed decoding.
brandtr8bc93852017-02-15 05:19:51 -0800169 void FrameDecoded(const webrtc::VideoFrame& image);
170
Sergey Silkin10d9d592018-02-01 13:25:17 +0100171 void CopyEncodedImage(const EncodedImage& encoded_image,
172 const VideoCodecType codec,
173 size_t frame_number,
174 size_t simulcast_svc_idx);
175
176 void CalculateFrameQuality(const VideoFrame& ref_frame,
177 const VideoFrame& dec_frame,
Sergey Silkin06a8f302018-02-20 09:48:26 +0100178 FrameStatistics* frame_stat);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100179
danilchap56359be2017-09-07 07:53:45 -0700180 TestConfig config_ RTC_GUARDED_BY(sequence_checker_);
brandtr07734a52017-08-08 08:35:53 -0700181
Sergey Silkin10d9d592018-02-01 13:25:17 +0100182 const size_t num_simulcast_or_spatial_layers_;
183
brandtr8bc93852017-02-15 05:19:51 -0800184 webrtc::VideoEncoder* const encoder_;
Sergey Silkin10d9d592018-02-01 13:25:17 +0100185 VideoDecoderList* const decoders_;
brandtraebc61e2017-02-28 07:13:47 -0800186 const std::unique_ptr<VideoBitrateAllocator> bitrate_allocator_;
Sergey Silkin3be2a552018-01-17 15:11:44 +0100187 BitrateAllocation bitrate_allocation_ RTC_GUARDED_BY(sequence_checker_);
brandtraebc61e2017-02-28 07:13:47 -0800188
189 // Adapters for the codec callbacks.
brandtrbdd555c2017-08-21 01:34:04 -0700190 VideoProcessorEncodeCompleteCallback encode_callback_;
191 VideoProcessorDecodeCompleteCallback decode_callback_;
brandtraebc61e2017-02-28 07:13:47 -0800192
Sergey Silkin64eaa992017-11-17 14:47:32 +0100193 // Input frames. Used as reference at frame quality evaluation.
194 // Async codecs might queue frames. To handle that we keep input frame
195 // and release it after corresponding coded frame is decoded and quality
196 // measurement is done.
Sergey Silkin3be2a552018-01-17 15:11:44 +0100197 std::map<size_t, std::unique_ptr<VideoFrame>> input_frames_
Sergey Silkin64eaa992017-11-17 14:47:32 +0100198 RTC_GUARDED_BY(sequence_checker_);
199
Sergey Silkin10d9d592018-02-01 13:25:17 +0100200 FrameReader* const input_frame_reader_;
brandtraebc61e2017-02-28 07:13:47 -0800201
brandtrc287c802017-08-07 08:30:43 -0700202 // These (optional) file writers are used to persistently store the encoded
203 // and decoded bitstreams. The purpose is to give the experimenter an option
204 // to subjectively evaluate the quality of the processing. Each frame writer
205 // is enabled by being non-null.
Sergey Silkin10d9d592018-02-01 13:25:17 +0100206 IvfFileWriterList* const encoded_frame_writers_;
207 FrameWriterList* const decoded_frame_writers_;
brandtr8bc93852017-02-15 05:19:51 -0800208
brandtr8935d972017-09-06 01:53:22 -0700209 // Keep track of inputed/encoded/decoded frames, so we can detect frame drops.
Rasmus Brandt5f7a8912018-02-28 17:17:15 +0100210 bool first_encoded_frame;
Sergey Silkin3be2a552018-01-17 15:11:44 +0100211 size_t last_inputed_frame_num_ RTC_GUARDED_BY(sequence_checker_);
212 size_t last_encoded_frame_num_ RTC_GUARDED_BY(sequence_checker_);
Sergey Silkin10d9d592018-02-01 13:25:17 +0100213 size_t last_encoded_simulcast_svc_idx_ RTC_GUARDED_BY(sequence_checker_);
Sergey Silkin3be2a552018-01-17 15:11:44 +0100214 size_t last_decoded_frame_num_ RTC_GUARDED_BY(sequence_checker_);
brandtr17b958c2017-03-07 01:41:43 -0800215
Sergey Silkin10d9d592018-02-01 13:25:17 +0100216 // Map of frame size (in pixels) to simulcast/spatial layer index.
217 std::map<size_t, size_t> frame_wxh_to_simulcast_svc_idx_
218 RTC_GUARDED_BY(sequence_checker_);
219
220 // Encoder delivers coded frame layer-by-layer. We store coded frames and
221 // then, after all layers are encoded, decode them. Such separation of
222 // frame processing on superframe level simplifies encoding/decoding time
223 // measurement.
224 std::map<size_t, EncodedImage> last_encoded_frames_
225 RTC_GUARDED_BY(sequence_checker_);
226
Rasmus Brandt5f7a8912018-02-28 17:17:15 +0100227 rtc::Buffer tmp_planar_i420_buffer_;
brandtr8bc93852017-02-15 05:19:51 -0800228
229 // Statistics.
Sergey Silkin06a8f302018-02-20 09:48:26 +0100230 Stats* const stats_;
brandtrc8c59052017-08-21 06:44:16 -0700231
232 rtc::SequencedTaskChecker sequence_checker_;
brandtrbdd555c2017-08-21 01:34:04 -0700233
234 RTC_DISALLOW_COPY_AND_ASSIGN(VideoProcessor);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000235};
236
237} // namespace test
238} // namespace webrtc
239
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200240#endif // MODULES_VIDEO_CODING_CODECS_TEST_VIDEOPROCESSOR_H_