blob: 62a12ef871debeadb612b3ae47e574f3fb5803d8 [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"
Åsa Persson2d27fb52017-10-19 14:05:50 +020024#include "modules/video_coding/codecs/test/test_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/video_coding/include/video_codec_interface.h"
26#include "modules/video_coding/utility/ivf_file_writer.h"
27#include "modules/video_coding/utility/vp8_header_parser.h"
28#include "modules/video_coding/utility/vp9_uncompressed_header_parser.h"
29#include "rtc_base/buffer.h"
30#include "rtc_base/checks.h"
31#include "rtc_base/constructormagic.h"
32#include "rtc_base/sequenced_task_checker.h"
33#include "rtc_base/task_queue.h"
34#include "test/testsupport/frame_reader.h"
35#include "test/testsupport/frame_writer.h"
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000036
37namespace webrtc {
Erik Språng08127a92016-11-16 16:41:30 +010038
39class VideoBitrateAllocator;
40
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000041namespace test {
42
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000043// Handles encoding/decoding of video using the VideoEncoder/VideoDecoder
44// interfaces. This is done in a sequential manner in order to be able to
45// measure times properly.
46// The class processes a frame at the time for the configured input file.
47// It maintains state of where in the source input file the processing is at.
48//
49// Regarding packet loss: Note that keyframes are excluded (first or all
50// depending on the ExcludeFrameTypes setting). This is because if key frames
51// would be altered, all the following delta frames would be pretty much
52// worthless. VP8 has an error-resilience feature that makes it able to handle
53// packet loss in key non-first keyframes, which is why only the first is
54// excluded by default.
55// Packet loss in such important frames is handled on a higher level in the
56// Video Engine, where signaling would request a retransmit of the lost packets,
57// since they're so important.
58//
Åsa Persson7173cf22017-10-19 12:14:09 +020059// Note this class is not thread safe and is meant for simple testing purposes.
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000060class VideoProcessor {
61 public:
brandtrc4095522017-08-07 08:12:33 -070062 VideoProcessor(webrtc::VideoEncoder* encoder,
63 webrtc::VideoDecoder* decoder,
64 FrameReader* analysis_frame_reader,
brandtrc4095522017-08-07 08:12:33 -070065 PacketManipulator* packet_manipulator,
66 const TestConfig& config,
67 Stats* stats,
brandtrc4095522017-08-07 08:12:33 -070068 IvfFileWriter* encoded_frame_writer,
69 FrameWriter* decoded_frame_writer);
70 ~VideoProcessor();
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000071
brandtr8935d972017-09-06 01:53:22 -070072 // Reads a frame from the analysis frame reader and sends it to the encoder.
73 // When the encode callback is received, the encoded frame is sent to the
74 // decoder. The decoded frame is written to disk by the analysis frame writer.
75 // Objective video quality metrics can thus be calculated after the fact.
76 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.
79 void SetRates(int bitrate_kbps, int framerate_fps);
marpan@webrtc.orgf4c2de92012-06-05 21:07:28 +000080
brandtrb57f4262017-08-30 06:29:51 -070081 // Returns the number of dropped frames.
82 std::vector<int> NumberDroppedFramesPerRateUpdate() const;
marpan@webrtc.orgf4c2de92012-06-05 21:07:28 +000083
brandtrb57f4262017-08-30 06:29:51 -070084 // Returns the number of spatial resizes.
85 std::vector<int> NumberSpatialResizesPerRateUpdate() const;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000086
87 private:
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000088 class VideoProcessorEncodeCompleteCallback
pbos@webrtc.org7f7162a2013-07-30 15:18:31 +000089 : public webrtc::EncodedImageCallback {
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000090 public:
brandtrc4095522017-08-07 08:12:33 -070091 explicit VideoProcessorEncodeCompleteCallback(
92 VideoProcessor* video_processor)
brandtrc8c59052017-08-21 06:44:16 -070093 : video_processor_(video_processor),
94 task_queue_(rtc::TaskQueue::Current()) {}
95
Sergey Ulanov525df3f2016-08-02 17:46:41 -070096 Result OnEncodedImage(
pbos@webrtc.org273a4142014-12-01 15:23:21 +000097 const webrtc::EncodedImage& encoded_image,
98 const webrtc::CodecSpecificInfo* codec_specific_info,
brandtr8bc93852017-02-15 05:19:51 -080099 const webrtc::RTPFragmentationHeader* fragmentation) override {
brandtr8bc93852017-02-15 05:19:51 -0800100 RTC_CHECK(codec_specific_info);
brandtrc8c59052017-08-21 06:44:16 -0700101
102 if (task_queue_ && !task_queue_->IsCurrent()) {
brandtr45535622017-08-22 03:33:11 -0700103 task_queue_->PostTask(
104 std::unique_ptr<rtc::QueuedTask>(new EncodeCallbackTask(
105 video_processor_, encoded_image, codec_specific_info)));
brandtrc8c59052017-08-21 06:44:16 -0700106 return Result(Result::OK, 0);
107 }
108
brandtr8bc93852017-02-15 05:19:51 -0800109 video_processor_->FrameEncoded(codec_specific_info->codecType,
brandtr45535622017-08-22 03:33:11 -0700110 encoded_image);
brandtr8bc93852017-02-15 05:19:51 -0800111 return Result(Result::OK, 0);
112 }
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000113
114 private:
brandtrc8c59052017-08-21 06:44:16 -0700115 class EncodeCallbackTask : public rtc::QueuedTask {
116 public:
117 EncodeCallbackTask(VideoProcessor* video_processor,
118 const webrtc::EncodedImage& encoded_image,
brandtr45535622017-08-22 03:33:11 -0700119 const webrtc::CodecSpecificInfo* codec_specific_info)
brandtrc8c59052017-08-21 06:44:16 -0700120 : video_processor_(video_processor),
121 buffer_(encoded_image._buffer, encoded_image._length),
122 encoded_image_(encoded_image),
123 codec_specific_info_(*codec_specific_info) {
124 encoded_image_._buffer = buffer_.data();
brandtrc8c59052017-08-21 06:44:16 -0700125 }
126
127 bool Run() override {
128 video_processor_->FrameEncoded(codec_specific_info_.codecType,
brandtr45535622017-08-22 03:33:11 -0700129 encoded_image_);
brandtrc8c59052017-08-21 06:44:16 -0700130 return true;
131 }
132
133 private:
134 VideoProcessor* const video_processor_;
135 rtc::Buffer buffer_;
136 webrtc::EncodedImage encoded_image_;
137 const webrtc::CodecSpecificInfo codec_specific_info_;
brandtrc8c59052017-08-21 06:44:16 -0700138 };
139
brandtrc4095522017-08-07 08:12:33 -0700140 VideoProcessor* const video_processor_;
brandtrc8c59052017-08-21 06:44:16 -0700141 rtc::TaskQueue* const task_queue_;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000142 };
143
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000144 class VideoProcessorDecodeCompleteCallback
philipelcce46fc2015-12-21 03:04:49 -0800145 : public webrtc::DecodedImageCallback {
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000146 public:
brandtrc4095522017-08-07 08:12:33 -0700147 explicit VideoProcessorDecodeCompleteCallback(
148 VideoProcessor* video_processor)
brandtrc8c59052017-08-21 06:44:16 -0700149 : video_processor_(video_processor),
150 task_queue_(rtc::TaskQueue::Current()) {}
151
brandtr8bc93852017-02-15 05:19:51 -0800152 int32_t Decoded(webrtc::VideoFrame& image) override {
brandtrc8c59052017-08-21 06:44:16 -0700153 if (task_queue_ && !task_queue_->IsCurrent()) {
154 task_queue_->PostTask(
155 [this, image]() { video_processor_->FrameDecoded(image); });
156 return 0;
157 }
brandtr8bc93852017-02-15 05:19:51 -0800158 video_processor_->FrameDecoded(image);
159 return 0;
160 }
brandtrc8c59052017-08-21 06:44:16 -0700161
philipelcce46fc2015-12-21 03:04:49 -0800162 int32_t Decoded(webrtc::VideoFrame& image,
163 int64_t decode_time_ms) override {
brandtr6bb8e0e2017-02-20 04:35:52 -0800164 return Decoded(image);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000165 }
brandtrc8c59052017-08-21 06:44:16 -0700166
brandtr6bb8e0e2017-02-20 04:35:52 -0800167 void Decoded(webrtc::VideoFrame& image,
sakalcc452e12017-02-09 04:53:45 -0800168 rtc::Optional<int32_t> decode_time_ms,
169 rtc::Optional<uint8_t> qp) override {
brandtrbea36fd2017-08-07 03:36:54 -0700170 Decoded(image);
sakalcc452e12017-02-09 04:53:45 -0800171 }
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000172
173 private:
brandtrc4095522017-08-07 08:12:33 -0700174 VideoProcessor* const video_processor_;
brandtrc8c59052017-08-21 06:44:16 -0700175 rtc::TaskQueue* const task_queue_;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000176 };
brandtr8bc93852017-02-15 05:19:51 -0800177
brandtrbdd555c2017-08-21 01:34:04 -0700178 // Invoked by the callback adapter when a frame has completed encoding.
brandtr8bc93852017-02-15 05:19:51 -0800179 void FrameEncoded(webrtc::VideoCodecType codec,
brandtr45535622017-08-22 03:33:11 -0700180 const webrtc::EncodedImage& encodedImage);
brandtr8bc93852017-02-15 05:19:51 -0800181
brandtrbdd555c2017-08-21 01:34:04 -0700182 // Invoked by the callback adapter when a frame has completed decoding.
brandtr8bc93852017-02-15 05:19:51 -0800183 void FrameDecoded(const webrtc::VideoFrame& image);
184
Åsa Perssonf0c44672017-10-24 16:03:39 +0200185 void WriteDecodedFrameToFile(rtc::Buffer* buffer);
186 bool ExcludeFrame(const EncodedImage& encoded_image);
187
danilchap56359be2017-09-07 07:53:45 -0700188 TestConfig config_ RTC_GUARDED_BY(sequence_checker_);
brandtr07734a52017-08-08 08:35:53 -0700189
brandtr8bc93852017-02-15 05:19:51 -0800190 webrtc::VideoEncoder* const encoder_;
191 webrtc::VideoDecoder* const decoder_;
brandtraebc61e2017-02-28 07:13:47 -0800192 const std::unique_ptr<VideoBitrateAllocator> bitrate_allocator_;
193
194 // Adapters for the codec callbacks.
brandtrbdd555c2017-08-21 01:34:04 -0700195 VideoProcessorEncodeCompleteCallback encode_callback_;
196 VideoProcessorDecodeCompleteCallback decode_callback_;
brandtraebc61e2017-02-28 07:13:47 -0800197
brandtr07734a52017-08-08 08:35:53 -0700198 // Fake network.
brandtraebc61e2017-02-28 07:13:47 -0800199 PacketManipulator* const packet_manipulator_;
brandtraebc61e2017-02-28 07:13:47 -0800200
Sergey Silkin64eaa992017-11-17 14:47:32 +0100201 // Input frames. Used as reference at frame quality evaluation.
202 // Async codecs might queue frames. To handle that we keep input frame
203 // and release it after corresponding coded frame is decoded and quality
204 // measurement is done.
205 std::map<int, std::unique_ptr<VideoFrame>> input_frames_
206 RTC_GUARDED_BY(sequence_checker_);
207
brandtrb78bc752017-02-22 01:26:59 -0800208 // These (mandatory) file manipulators are used for, e.g., objective PSNR and
209 // SSIM calculations at the end of a test run.
210 FrameReader* const analysis_frame_reader_;
brandtraebc61e2017-02-28 07:13:47 -0800211
brandtrc287c802017-08-07 08:30:43 -0700212 // These (optional) file writers are used to persistently store the encoded
213 // and decoded bitstreams. The purpose is to give the experimenter an option
214 // to subjectively evaluate the quality of the processing. Each frame writer
215 // is enabled by being non-null.
brandtrb78bc752017-02-22 01:26:59 -0800216 IvfFileWriter* const encoded_frame_writer_;
217 FrameWriter* const decoded_frame_writer_;
brandtr8bc93852017-02-15 05:19:51 -0800218
brandtr8935d972017-09-06 01:53:22 -0700219 // Keep track of inputed/encoded/decoded frames, so we can detect frame drops.
danilchap56359be2017-09-07 07:53:45 -0700220 int last_inputed_frame_num_ RTC_GUARDED_BY(sequence_checker_);
221 int last_encoded_frame_num_ RTC_GUARDED_BY(sequence_checker_);
222 int last_decoded_frame_num_ RTC_GUARDED_BY(sequence_checker_);
brandtr17b958c2017-03-07 01:41:43 -0800223
brandtrb57f4262017-08-30 06:29:51 -0700224 // Store an RTP timestamp -> frame number map, since the timestamps are
225 // based off of the frame rate, which can change mid-test.
226 std::map<uint32_t, int> rtp_timestamp_to_frame_num_
danilchap56359be2017-09-07 07:53:45 -0700227 RTC_GUARDED_BY(sequence_checker_);
brandtrb57f4262017-08-30 06:29:51 -0700228
brandtr17b958c2017-03-07 01:41:43 -0800229 // Keep track of if we have excluded the first key frame from packet loss.
danilchap56359be2017-09-07 07:53:45 -0700230 bool first_key_frame_has_been_excluded_ RTC_GUARDED_BY(sequence_checker_);
brandtr17b958c2017-03-07 01:41:43 -0800231
232 // Keep track of the last successfully decoded frame, since we write that
233 // frame to disk when decoding fails.
danilchap56359be2017-09-07 07:53:45 -0700234 rtc::Buffer last_decoded_frame_buffer_ RTC_GUARDED_BY(sequence_checker_);
brandtr8bc93852017-02-15 05:19:51 -0800235
236 // Statistics.
brandtraebc61e2017-02-28 07:13:47 -0800237 Stats* stats_;
danilchap56359be2017-09-07 07:53:45 -0700238 std::vector<int> num_dropped_frames_ RTC_GUARDED_BY(sequence_checker_);
239 std::vector<int> num_spatial_resizes_ RTC_GUARDED_BY(sequence_checker_);
240 int rate_update_index_ RTC_GUARDED_BY(sequence_checker_);
brandtrc8c59052017-08-21 06:44:16 -0700241
242 rtc::SequencedTaskChecker sequence_checker_;
brandtrbdd555c2017-08-21 01:34:04 -0700243
244 RTC_DISALLOW_COPY_AND_ASSIGN(VideoProcessor);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000245};
246
247} // namespace test
248} // namespace webrtc
249
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200250#endif // MODULES_VIDEO_CODING_CODECS_TEST_VIDEOPROCESSOR_H_