1 /* 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 3 * 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 */ 10 11 #ifndef MODULES_VIDEO_CODING_CODECS_TEST_VIDEOPROCESSOR_H_ 12 #define MODULES_VIDEO_CODING_CODECS_TEST_VIDEOPROCESSOR_H_ 13 14 #include <stddef.h> 15 #include <stdint.h> 16 17 #include <map> 18 #include <memory> 19 #include <utility> 20 #include <vector> 21 22 #include "absl/types/optional.h" 23 #include "api/sequence_checker.h" 24 #include "api/task_queue/task_queue_base.h" 25 #include "api/test/videocodec_test_fixture.h" 26 #include "api/video/encoded_image.h" 27 #include "api/video/i420_buffer.h" 28 #include "api/video/video_bitrate_allocation.h" 29 #include "api/video/video_bitrate_allocator.h" 30 #include "api/video/video_frame.h" 31 #include "api/video_codecs/video_decoder.h" 32 #include "api/video_codecs/video_encoder.h" 33 #include "modules/include/module_common_types.h" 34 #include "modules/video_coding/codecs/test/videocodec_test_stats_impl.h" 35 #include "modules/video_coding/include/video_codec_interface.h" 36 #include "modules/video_coding/utility/ivf_file_writer.h" 37 #include "rtc_base/buffer.h" 38 #include "rtc_base/checks.h" 39 #include "rtc_base/system/no_unique_address.h" 40 #include "rtc_base/thread_annotations.h" 41 #include "test/testsupport/frame_reader.h" 42 #include "test/testsupport/frame_writer.h" 43 44 namespace webrtc { 45 namespace test { 46 47 // 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. 52 class VideoProcessor { 53 public: 54 using VideoDecoderList = std::vector<std::unique_ptr<VideoDecoder>>; 55 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. 59 using FrameWriterList = std::vector<std::unique_ptr<FrameWriter>>; 60 using FrameStatistics = VideoCodecTestStats::FrameStatistics; 61 62 VideoProcessor(webrtc::VideoEncoder* encoder, 63 VideoDecoderList* decoders, 64 FrameReader* input_frame_reader, 65 const VideoCodecTestFixture::Config& config, 66 VideoCodecTestStatsImpl* stats, 67 IvfFileWriterMap* encoded_frame_writers, 68 FrameWriterList* decoded_frame_writers); 69 ~VideoProcessor(); 70 71 VideoProcessor(const VideoProcessor&) = delete; 72 VideoProcessor& operator=(const VideoProcessor&) = delete; 73 74 // 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. 78 void ProcessFrame(); 79 80 // Updates the encoder with target rates. Must be called at least once. 81 void SetRates(size_t bitrate_kbps, double framerate_fps); 82 83 // 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 88 private: 89 class VideoProcessorEncodeCompleteCallback 90 : public webrtc::EncodedImageCallback { 91 public: VideoProcessorEncodeCompleteCallback(VideoProcessor * video_processor)92 explicit VideoProcessorEncodeCompleteCallback( 93 VideoProcessor* video_processor) 94 : video_processor_(video_processor), 95 task_queue_(TaskQueueBase::Current()) { 96 RTC_DCHECK(video_processor_); 97 RTC_DCHECK(task_queue_); 98 } 99 OnEncodedImage(const webrtc::EncodedImage & encoded_image,const webrtc::CodecSpecificInfo * codec_specific_info)100 Result OnEncodedImage( 101 const webrtc::EncodedImage& encoded_image, 102 const webrtc::CodecSpecificInfo* codec_specific_info) override { 103 RTC_CHECK(codec_specific_info); 104 105 // Post the callback to the right task queue, if needed. 106 if (!task_queue_->IsCurrent()) { 107 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 }); 112 return Result(Result::OK, 0); 113 } 114 115 video_processor_->FrameEncoded(encoded_image, *codec_specific_info); 116 return Result(Result::OK, 0); 117 } 118 119 private: 120 VideoProcessor* const video_processor_; 121 TaskQueueBase* const task_queue_; 122 }; 123 124 class VideoProcessorDecodeCompleteCallback 125 : public webrtc::DecodedImageCallback { 126 public: VideoProcessorDecodeCompleteCallback(VideoProcessor * video_processor,size_t simulcast_svc_idx)127 explicit VideoProcessorDecodeCompleteCallback( 128 VideoProcessor* video_processor, 129 size_t simulcast_svc_idx) 130 : video_processor_(video_processor), 131 simulcast_svc_idx_(simulcast_svc_idx), 132 task_queue_(TaskQueueBase::Current()) { 133 RTC_DCHECK(video_processor_); 134 RTC_DCHECK(task_queue_); 135 } 136 137 int32_t Decoded(webrtc::VideoFrame& image) override; 138 Decoded(webrtc::VideoFrame & image,int64_t decode_time_ms)139 int32_t Decoded(webrtc::VideoFrame& image, 140 int64_t decode_time_ms) override { 141 return Decoded(image); 142 } 143 Decoded(webrtc::VideoFrame & image,absl::optional<int32_t> decode_time_ms,absl::optional<uint8_t> qp)144 void Decoded(webrtc::VideoFrame& image, 145 absl::optional<int32_t> decode_time_ms, 146 absl::optional<uint8_t> qp) override { 147 Decoded(image); 148 } 149 150 private: 151 VideoProcessor* const video_processor_; 152 const size_t simulcast_svc_idx_; 153 TaskQueueBase* const task_queue_; 154 }; 155 156 // Invoked by the callback adapter when a frame has completed encoding. 157 void FrameEncoded(const webrtc::EncodedImage& encoded_image, 158 const webrtc::CodecSpecificInfo& codec_specific); 159 160 // Invoked by the callback adapter when a frame has completed decoding. 161 void FrameDecoded(const webrtc::VideoFrame& image, size_t simulcast_svc_idx); 162 163 void DecodeFrame(const EncodedImage& encoded_image, size_t simulcast_svc_idx); 164 165 // 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. 167 const webrtc::EncodedImage* BuildAndStoreSuperframe( 168 const EncodedImage& encoded_image, 169 VideoCodecType codec, 170 size_t frame_number, 171 size_t simulcast_svc_idx, 172 bool inter_layer_predicted) RTC_RUN_ON(sequence_checker_); 173 174 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_; 184 const size_t num_simulcast_or_spatial_layers_; 185 const bool analyze_frame_quality_; 186 187 // Frame statistics. 188 VideoCodecTestStatsImpl* const stats_; 189 190 // Codecs. 191 webrtc::VideoEncoder* const encoder_; 192 VideoDecoderList* const decoders_; 193 const std::unique_ptr<VideoBitrateAllocator> bitrate_allocator_; 194 195 // Target bitrate and framerate per frame. 196 std::map<size_t, RateProfile> target_rates_ RTC_GUARDED_BY(sequence_checker_); 197 198 // Adapters for the codec callbacks. 199 VideoProcessorEncodeCompleteCallback encode_callback_; 200 // Assign separate callback object to each decoder. This allows us to identify 201 // decoded layer in frame decode callback. 202 // simulcast_svc_idx -> decode callback. 203 std::vector<std::unique_ptr<VideoProcessorDecodeCompleteCallback>> 204 decode_callback_; 205 206 // Each call to ProcessFrame() will read one frame from `input_frame_reader_`. 207 FrameReader* const input_frame_reader_; 208 209 // Input frames are used as reference for frame quality evaluations. 210 // Async codecs might queue frames. To handle that we keep input frame 211 // and release it after corresponding coded frame is decoded and quality 212 // measurement is done. 213 // frame_number -> frame. 214 std::map<size_t, VideoFrame> input_frames_ RTC_GUARDED_BY(sequence_checker_); 215 216 // Encoder delivers coded frame layer-by-layer. We store coded frames and 217 // then, after all layers are encoded, decode them. Such separation of 218 // frame processing on superframe level simplifies encoding/decoding time 219 // measurement. 220 // simulcast_svc_idx -> merged SVC encoded frame. 221 std::vector<EncodedImage> merged_encoded_frames_ 222 RTC_GUARDED_BY(sequence_checker_); 223 224 // These (optional) file writers are used to persistently store the encoded 225 // and decoded bitstreams. Each frame writer is enabled by being non-null. 226 IvfFileWriterMap* const encoded_frame_writers_; 227 FrameWriterList* const decoded_frame_writers_; 228 229 // Metadata for inputed/encoded/decoded frames. Used for frame identification, 230 // frame drop detection, etc. We assume that encoded/decoded frames are 231 // ordered within each simulcast/spatial layer, but we do not make any 232 // assumptions of frame ordering between layers. 233 size_t last_inputed_frame_num_ RTC_GUARDED_BY(sequence_checker_); 234 size_t last_inputed_timestamp_ RTC_GUARDED_BY(sequence_checker_); 235 // simulcast_svc_idx -> encode status. 236 std::vector<bool> first_encoded_frame_ RTC_GUARDED_BY(sequence_checker_); 237 // simulcast_svc_idx -> frame_number. 238 std::vector<size_t> last_encoded_frame_num_ RTC_GUARDED_BY(sequence_checker_); 239 // simulcast_svc_idx -> decode status. 240 std::vector<bool> first_decoded_frame_ RTC_GUARDED_BY(sequence_checker_); 241 // simulcast_svc_idx -> frame_number. 242 std::vector<size_t> last_decoded_frame_num_ RTC_GUARDED_BY(sequence_checker_); 243 // simulcast_svc_idx -> buffer. 244 std::vector<rtc::scoped_refptr<I420Buffer>> last_decoded_frame_buffer_ 245 RTC_GUARDED_BY(sequence_checker_); 246 247 // Time spent in frame encode callback. It is accumulated for layers and 248 // reset when frame encode starts. When next layer is encoded post-encode time 249 // is substracted from measured encode time. Thus we get pure encode time. 250 int64_t post_encode_time_ns_ RTC_GUARDED_BY(sequence_checker_); 251 252 // Indicates whether Finalize() was called or not. 253 bool is_finalized_ RTC_GUARDED_BY(sequence_checker_); 254 255 // This class must be operated on a TaskQueue. 256 RTC_NO_UNIQUE_ADDRESS SequenceChecker sequence_checker_; 257 }; 258 259 } // namespace test 260 } // namespace webrtc 261 262 #endif // MODULES_VIDEO_CODING_CODECS_TEST_VIDEOPROCESSOR_H_ 263