blob: af7a33a1db1201bf4c7039100c2ca8ca7838a2f6 [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"
20#include "common_video/libyuv/include/webrtc_libyuv.h"
ssilkin612f8582017-09-28 09:23:17 -070021#include "modules/video_coding/codecs/h264/include/h264_globals.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/video_coding/codecs/test/packet_manipulator.h"
23#include "modules/video_coding/codecs/test/stats.h"
24#include "modules/video_coding/include/video_codec_interface.h"
25#include "modules/video_coding/utility/ivf_file_writer.h"
26#include "modules/video_coding/utility/vp8_header_parser.h"
27#include "modules/video_coding/utility/vp9_uncompressed_header_parser.h"
28#include "rtc_base/buffer.h"
29#include "rtc_base/checks.h"
30#include "rtc_base/constructormagic.h"
31#include "rtc_base/sequenced_task_checker.h"
32#include "rtc_base/task_queue.h"
33#include "test/testsupport/frame_reader.h"
34#include "test/testsupport/frame_writer.h"
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000035
36namespace webrtc {
Erik Språng08127a92016-11-16 16:41:30 +010037
38class VideoBitrateAllocator;
39
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000040namespace test {
41
42// Defines which frame types shall be excluded from packet loss and when.
43enum ExcludeFrameTypes {
44 // Will exclude the first keyframe in the video sequence from packet loss.
45 // Following keyframes will be targeted for packet loss.
46 kExcludeOnlyFirstKeyFrame,
47 // Exclude all keyframes from packet loss, no matter where in the video
48 // sequence they occur.
49 kExcludeAllKeyFrames
50};
brandtr8bc93852017-02-15 05:19:51 -080051
brandtr8bc93852017-02-15 05:19:51 -080052// Test configuration for a test run.
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000053struct TestConfig {
Åsa Persson7173cf22017-10-19 12:14:09 +020054 // Returns the number of cores to use.
55 int NumberOfCores() const;
56
asapersson8339e1a2017-08-02 00:17:18 -070057 // Plain name of YUV file to process without file extension.
58 std::string filename;
59
asapersson8a90f872017-06-29 05:13:27 -070060 // File to process. This must be a video file in the YUV format.
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000061 std::string input_filename;
62
63 // File to write to during processing for the test. Will be a video file
64 // in the YUV format.
65 std::string output_filename;
66
Åsa Perssondc182a42017-10-12 13:53:58 +020067 // Number of frames to process.
68 int num_frames = 0;
69
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000070 // Configurations related to networking.
71 NetworkingConfig networking_config;
72
73 // Decides how the packet loss simulations shall exclude certain frames
asapersson8a90f872017-06-29 05:13:27 -070074 // from packet loss.
75 ExcludeFrameTypes exclude_frame_types = kExcludeOnlyFirstKeyFrame;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000076
77 // Force the encoder and decoder to use a single core for processing.
78 // Using a single core is necessary to get a deterministic behavior for the
79 // encoded frames - using multiple cores will produce different encoded frames
80 // since multiple cores are competing to consume the byte budget for each
81 // frame in parallel.
82 // If set to false, the maximum number of available cores will be used.
asapersson8a90f872017-06-29 05:13:27 -070083 bool use_single_core = false;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000084
Åsa Persson7173cf22017-10-19 12:14:09 +020085 // Should cpu usage be measured?
86 // If set to true, the encoding will run in real-time.
87 bool measure_cpu = false;
88
asapersson8339e1a2017-08-02 00:17:18 -070089 // If > 0: forces the encoder to create a keyframe every Nth frame.
90 // Note that the encoder may create a keyframe in other locations in addition
91 // to this setting. Forcing key frames may also affect encoder planning
92 // optimizations in a negative way, since it will suddenly be forced to
93 // produce an expensive key frame.
asapersson8a90f872017-06-29 05:13:27 -070094 int keyframe_interval = 0;
kjellander@webrtc.orga31b2542011-10-07 06:50:22 +000095
Åsa Persson7173cf22017-10-19 12:14:09 +020096 // Codec settings to use.
brandtr07734a52017-08-08 08:35:53 -070097 webrtc::VideoCodec codec_settings;
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000098
99 // If printing of information to stdout shall be performed during processing.
asapersson8a90f872017-06-29 05:13:27 -0700100 bool verbose = true;
asapersson8339e1a2017-08-02 00:17:18 -0700101
brandtrd635e5b2017-09-06 04:48:22 -0700102 // Should hardware accelerated codecs be used?
103 bool hw_encoder = false;
104 bool hw_decoder = false;
105
106 // Should the hardware codecs be wrapped in software fallbacks?
107 bool sw_fallback_encoder = false;
Rasmus Brandt638200e2017-09-27 13:14:14 +0200108 bool sw_fallback_decoder = false;
ssilkin612f8582017-09-28 09:23:17 -0700109
110 // RTP H264 packetization mode.
111 H264PacketizationMode packetization_mode =
112 H264PacketizationMode::NonInterleaved;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000113};
114
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000115// Handles encoding/decoding of video using the VideoEncoder/VideoDecoder
116// interfaces. This is done in a sequential manner in order to be able to
117// measure times properly.
118// The class processes a frame at the time for the configured input file.
119// It maintains state of where in the source input file the processing is at.
120//
121// Regarding packet loss: Note that keyframes are excluded (first or all
122// depending on the ExcludeFrameTypes setting). This is because if key frames
123// would be altered, all the following delta frames would be pretty much
124// worthless. VP8 has an error-resilience feature that makes it able to handle
125// packet loss in key non-first keyframes, which is why only the first is
126// excluded by default.
127// Packet loss in such important frames is handled on a higher level in the
128// Video Engine, where signaling would request a retransmit of the lost packets,
129// since they're so important.
130//
Åsa Persson7173cf22017-10-19 12:14:09 +0200131// Note this class is not thread safe and is meant for simple testing purposes.
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000132class VideoProcessor {
133 public:
brandtrc4095522017-08-07 08:12:33 -0700134 VideoProcessor(webrtc::VideoEncoder* encoder,
135 webrtc::VideoDecoder* decoder,
136 FrameReader* analysis_frame_reader,
137 FrameWriter* analysis_frame_writer,
138 PacketManipulator* packet_manipulator,
139 const TestConfig& config,
140 Stats* stats,
brandtrc4095522017-08-07 08:12:33 -0700141 IvfFileWriter* encoded_frame_writer,
142 FrameWriter* decoded_frame_writer);
143 ~VideoProcessor();
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000144
asapersson1d29c862017-06-22 02:18:50 -0700145 // Sets up callbacks and initializes the encoder and decoder.
brandtrc4095522017-08-07 08:12:33 -0700146 void Init();
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000147
brandtr77920a42017-08-11 07:48:15 -0700148 // Tears down callbacks and releases the encoder and decoder.
149 void Release();
150
brandtr8935d972017-09-06 01:53:22 -0700151 // Reads a frame from the analysis frame reader and sends it to the encoder.
152 // When the encode callback is received, the encoded frame is sent to the
153 // decoder. The decoded frame is written to disk by the analysis frame writer.
154 // Objective video quality metrics can thus be calculated after the fact.
155 void ProcessFrame();
marpan@webrtc.orgf4c2de92012-06-05 21:07:28 +0000156
brandtrbdd555c2017-08-21 01:34:04 -0700157 // Updates the encoder with target rates. Must be called at least once.
158 void SetRates(int bitrate_kbps, int framerate_fps);
marpan@webrtc.orgf4c2de92012-06-05 21:07:28 +0000159
brandtrb57f4262017-08-30 06:29:51 -0700160 // Returns the number of dropped frames.
161 std::vector<int> NumberDroppedFramesPerRateUpdate() const;
marpan@webrtc.orgf4c2de92012-06-05 21:07:28 +0000162
brandtrb57f4262017-08-30 06:29:51 -0700163 // Returns the number of spatial resizes.
164 std::vector<int> NumberSpatialResizesPerRateUpdate() const;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000165
166 private:
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000167 class VideoProcessorEncodeCompleteCallback
pbos@webrtc.org7f7162a2013-07-30 15:18:31 +0000168 : public webrtc::EncodedImageCallback {
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000169 public:
brandtrc4095522017-08-07 08:12:33 -0700170 explicit VideoProcessorEncodeCompleteCallback(
171 VideoProcessor* video_processor)
brandtrc8c59052017-08-21 06:44:16 -0700172 : video_processor_(video_processor),
173 task_queue_(rtc::TaskQueue::Current()) {}
174
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700175 Result OnEncodedImage(
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000176 const webrtc::EncodedImage& encoded_image,
177 const webrtc::CodecSpecificInfo* codec_specific_info,
brandtr8bc93852017-02-15 05:19:51 -0800178 const webrtc::RTPFragmentationHeader* fragmentation) override {
brandtr8bc93852017-02-15 05:19:51 -0800179 RTC_CHECK(codec_specific_info);
brandtrc8c59052017-08-21 06:44:16 -0700180
181 if (task_queue_ && !task_queue_->IsCurrent()) {
brandtr45535622017-08-22 03:33:11 -0700182 task_queue_->PostTask(
183 std::unique_ptr<rtc::QueuedTask>(new EncodeCallbackTask(
184 video_processor_, encoded_image, codec_specific_info)));
brandtrc8c59052017-08-21 06:44:16 -0700185 return Result(Result::OK, 0);
186 }
187
brandtr8bc93852017-02-15 05:19:51 -0800188 video_processor_->FrameEncoded(codec_specific_info->codecType,
brandtr45535622017-08-22 03:33:11 -0700189 encoded_image);
brandtr8bc93852017-02-15 05:19:51 -0800190 return Result(Result::OK, 0);
191 }
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000192
193 private:
brandtrc8c59052017-08-21 06:44:16 -0700194 class EncodeCallbackTask : public rtc::QueuedTask {
195 public:
196 EncodeCallbackTask(VideoProcessor* video_processor,
197 const webrtc::EncodedImage& encoded_image,
brandtr45535622017-08-22 03:33:11 -0700198 const webrtc::CodecSpecificInfo* codec_specific_info)
brandtrc8c59052017-08-21 06:44:16 -0700199 : video_processor_(video_processor),
200 buffer_(encoded_image._buffer, encoded_image._length),
201 encoded_image_(encoded_image),
202 codec_specific_info_(*codec_specific_info) {
203 encoded_image_._buffer = buffer_.data();
brandtrc8c59052017-08-21 06:44:16 -0700204 }
205
206 bool Run() override {
207 video_processor_->FrameEncoded(codec_specific_info_.codecType,
brandtr45535622017-08-22 03:33:11 -0700208 encoded_image_);
brandtrc8c59052017-08-21 06:44:16 -0700209 return true;
210 }
211
212 private:
213 VideoProcessor* const video_processor_;
214 rtc::Buffer buffer_;
215 webrtc::EncodedImage encoded_image_;
216 const webrtc::CodecSpecificInfo codec_specific_info_;
brandtrc8c59052017-08-21 06:44:16 -0700217 };
218
brandtrc4095522017-08-07 08:12:33 -0700219 VideoProcessor* const video_processor_;
brandtrc8c59052017-08-21 06:44:16 -0700220 rtc::TaskQueue* const task_queue_;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000221 };
222
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000223 class VideoProcessorDecodeCompleteCallback
philipelcce46fc2015-12-21 03:04:49 -0800224 : public webrtc::DecodedImageCallback {
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000225 public:
brandtrc4095522017-08-07 08:12:33 -0700226 explicit VideoProcessorDecodeCompleteCallback(
227 VideoProcessor* video_processor)
brandtrc8c59052017-08-21 06:44:16 -0700228 : video_processor_(video_processor),
229 task_queue_(rtc::TaskQueue::Current()) {}
230
brandtr8bc93852017-02-15 05:19:51 -0800231 int32_t Decoded(webrtc::VideoFrame& image) override {
brandtrc8c59052017-08-21 06:44:16 -0700232 if (task_queue_ && !task_queue_->IsCurrent()) {
233 task_queue_->PostTask(
234 [this, image]() { video_processor_->FrameDecoded(image); });
235 return 0;
236 }
brandtr8bc93852017-02-15 05:19:51 -0800237 video_processor_->FrameDecoded(image);
238 return 0;
239 }
brandtrc8c59052017-08-21 06:44:16 -0700240
philipelcce46fc2015-12-21 03:04:49 -0800241 int32_t Decoded(webrtc::VideoFrame& image,
242 int64_t decode_time_ms) override {
brandtr6bb8e0e2017-02-20 04:35:52 -0800243 return Decoded(image);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000244 }
brandtrc8c59052017-08-21 06:44:16 -0700245
brandtr6bb8e0e2017-02-20 04:35:52 -0800246 void Decoded(webrtc::VideoFrame& image,
sakalcc452e12017-02-09 04:53:45 -0800247 rtc::Optional<int32_t> decode_time_ms,
248 rtc::Optional<uint8_t> qp) override {
brandtrbea36fd2017-08-07 03:36:54 -0700249 Decoded(image);
sakalcc452e12017-02-09 04:53:45 -0800250 }
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000251
252 private:
brandtrc4095522017-08-07 08:12:33 -0700253 VideoProcessor* const video_processor_;
brandtrc8c59052017-08-21 06:44:16 -0700254 rtc::TaskQueue* const task_queue_;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000255 };
brandtr8bc93852017-02-15 05:19:51 -0800256
brandtrbdd555c2017-08-21 01:34:04 -0700257 // Invoked by the callback adapter when a frame has completed encoding.
brandtr8bc93852017-02-15 05:19:51 -0800258 void FrameEncoded(webrtc::VideoCodecType codec,
brandtr45535622017-08-22 03:33:11 -0700259 const webrtc::EncodedImage& encodedImage);
brandtr8bc93852017-02-15 05:19:51 -0800260
brandtrbdd555c2017-08-21 01:34:04 -0700261 // Invoked by the callback adapter when a frame has completed decoding.
brandtr8bc93852017-02-15 05:19:51 -0800262 void FrameDecoded(const webrtc::VideoFrame& image);
263
danilchap56359be2017-09-07 07:53:45 -0700264 bool initialized_ RTC_GUARDED_BY(sequence_checker_);
265 TestConfig config_ RTC_GUARDED_BY(sequence_checker_);
brandtr07734a52017-08-08 08:35:53 -0700266
brandtr8bc93852017-02-15 05:19:51 -0800267 webrtc::VideoEncoder* const encoder_;
268 webrtc::VideoDecoder* const decoder_;
brandtraebc61e2017-02-28 07:13:47 -0800269 const std::unique_ptr<VideoBitrateAllocator> bitrate_allocator_;
270
271 // Adapters for the codec callbacks.
brandtrbdd555c2017-08-21 01:34:04 -0700272 VideoProcessorEncodeCompleteCallback encode_callback_;
273 VideoProcessorDecodeCompleteCallback decode_callback_;
brandtraebc61e2017-02-28 07:13:47 -0800274
brandtr07734a52017-08-08 08:35:53 -0700275 // Fake network.
brandtraebc61e2017-02-28 07:13:47 -0800276 PacketManipulator* const packet_manipulator_;
brandtraebc61e2017-02-28 07:13:47 -0800277
brandtrb78bc752017-02-22 01:26:59 -0800278 // These (mandatory) file manipulators are used for, e.g., objective PSNR and
279 // SSIM calculations at the end of a test run.
280 FrameReader* const analysis_frame_reader_;
281 FrameWriter* const analysis_frame_writer_;
brandtraebc61e2017-02-28 07:13:47 -0800282
brandtrc287c802017-08-07 08:30:43 -0700283 // These (optional) file writers are used to persistently store the encoded
284 // and decoded bitstreams. The purpose is to give the experimenter an option
285 // to subjectively evaluate the quality of the processing. Each frame writer
286 // is enabled by being non-null.
brandtrb78bc752017-02-22 01:26:59 -0800287 IvfFileWriter* const encoded_frame_writer_;
288 FrameWriter* const decoded_frame_writer_;
brandtr8bc93852017-02-15 05:19:51 -0800289
brandtr8935d972017-09-06 01:53:22 -0700290 // Keep track of inputed/encoded/decoded frames, so we can detect frame drops.
danilchap56359be2017-09-07 07:53:45 -0700291 int last_inputed_frame_num_ RTC_GUARDED_BY(sequence_checker_);
292 int last_encoded_frame_num_ RTC_GUARDED_BY(sequence_checker_);
293 int last_decoded_frame_num_ RTC_GUARDED_BY(sequence_checker_);
brandtr17b958c2017-03-07 01:41:43 -0800294
brandtrb57f4262017-08-30 06:29:51 -0700295 // Store an RTP timestamp -> frame number map, since the timestamps are
296 // based off of the frame rate, which can change mid-test.
297 std::map<uint32_t, int> rtp_timestamp_to_frame_num_
danilchap56359be2017-09-07 07:53:45 -0700298 RTC_GUARDED_BY(sequence_checker_);
brandtrb57f4262017-08-30 06:29:51 -0700299
brandtr17b958c2017-03-07 01:41:43 -0800300 // Keep track of if we have excluded the first key frame from packet loss.
danilchap56359be2017-09-07 07:53:45 -0700301 bool first_key_frame_has_been_excluded_ RTC_GUARDED_BY(sequence_checker_);
brandtr17b958c2017-03-07 01:41:43 -0800302
303 // Keep track of the last successfully decoded frame, since we write that
304 // frame to disk when decoding fails.
danilchap56359be2017-09-07 07:53:45 -0700305 rtc::Buffer last_decoded_frame_buffer_ RTC_GUARDED_BY(sequence_checker_);
brandtr8bc93852017-02-15 05:19:51 -0800306
307 // Statistics.
brandtraebc61e2017-02-28 07:13:47 -0800308 Stats* stats_;
danilchap56359be2017-09-07 07:53:45 -0700309 std::vector<int> num_dropped_frames_ RTC_GUARDED_BY(sequence_checker_);
310 std::vector<int> num_spatial_resizes_ RTC_GUARDED_BY(sequence_checker_);
311 int rate_update_index_ RTC_GUARDED_BY(sequence_checker_);
brandtrc8c59052017-08-21 06:44:16 -0700312
313 rtc::SequencedTaskChecker sequence_checker_;
brandtrbdd555c2017-08-21 01:34:04 -0700314
315 RTC_DISALLOW_COPY_AND_ASSIGN(VideoProcessor);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000316};
317
318} // namespace test
319} // namespace webrtc
320
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200321#endif // MODULES_VIDEO_CODING_CODECS_TEST_VIDEOPROCESSOR_H_