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/task_queue/queued_task.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/video_bitrate_allocation.h" 28 #include "api/video/video_bitrate_allocator.h" 29 #include "api/video/video_frame.h" 30 #include "api/video_codecs/video_decoder.h" 31 #include "api/video_codecs/video_encoder.h" 32 #include "modules/include/module_common_types.h" 33 #include "modules/video_coding/codecs/test/videocodec_test_stats_impl.h" 34 #include "modules/video_coding/include/video_codec_interface.h" 35 #include "modules/video_coding/utility/ivf_file_writer.h" 36 #include "rtc_base/buffer.h" 37 #include "rtc_base/checks.h" 38 #include "rtc_base/constructor_magic.h" 39 #include "rtc_base/synchronization/sequence_checker.h" 40 #include "rtc_base/thread_annotations.h" 41 #include "rtc_base/thread_checker.h" 42 #include "test/testsupport/frame_reader.h" 43 #include "test/testsupport/frame_writer.h" 44 45 namespace webrtc { 46 namespace test { 47 48 // Handles encoding/decoding of video using the VideoEncoder/VideoDecoder 49 // interfaces. This is done in a sequential manner in order to be able to 50 // measure times properly. 51 // The class processes a frame at the time for the configured input file. 52 // It maintains state of where in the source input file the processing is at. 53 class VideoProcessor { 54 public: 55 using VideoDecoderList = std::vector<std::unique_ptr<VideoDecoder>>; 56 using LayerKey = std::pair<int /* spatial_idx */, int /* temporal_idx */>; 57 using IvfFileWriterMap = std::map<LayerKey, std::unique_ptr<IvfFileWriter>>; 58 // TODO(brandtr): Consider changing FrameWriterList to be a FrameWriterMap, 59 // to be able to save different TLs separately. 60 using FrameWriterList = std::vector<std::unique_ptr<FrameWriter>>; 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 // Reads a frame and sends it to the encoder. When the encode callback 72 // is received, the encoded frame is buffered. After encoding is finished 73 // buffered frame is sent to decoder. Quality evaluation is done in 74 // the decode callback. 75 void ProcessFrame(); 76 77 // Updates the encoder with target rates. Must be called at least once. 78 void SetRates(size_t bitrate_kbps, double framerate_fps); 79 80 private: 81 class VideoProcessorEncodeCompleteCallback 82 : public webrtc::EncodedImageCallback { 83 public: VideoProcessorEncodeCompleteCallback(VideoProcessor * video_processor)84 explicit VideoProcessorEncodeCompleteCallback( 85 VideoProcessor* video_processor) 86 : video_processor_(video_processor), 87 task_queue_(TaskQueueBase::Current()) { 88 RTC_DCHECK(video_processor_); 89 RTC_DCHECK(task_queue_); 90 } 91 OnEncodedImage(const webrtc::EncodedImage & encoded_image,const webrtc::CodecSpecificInfo * codec_specific_info,const webrtc::RTPFragmentationHeader * fragmentation)92 Result OnEncodedImage( 93 const webrtc::EncodedImage& encoded_image, 94 const webrtc::CodecSpecificInfo* codec_specific_info, 95 const webrtc::RTPFragmentationHeader* fragmentation) override { 96 RTC_CHECK(codec_specific_info); 97 98 // Post the callback to the right task queue, if needed. 99 if (!task_queue_->IsCurrent()) { 100 task_queue_->PostTask(std::make_unique<EncodeCallbackTask>( 101 video_processor_, encoded_image, codec_specific_info)); 102 return Result(Result::OK, 0); 103 } 104 105 video_processor_->FrameEncoded(encoded_image, *codec_specific_info); 106 return Result(Result::OK, 0); 107 } 108 109 private: 110 class EncodeCallbackTask : public QueuedTask { 111 public: EncodeCallbackTask(VideoProcessor * video_processor,const webrtc::EncodedImage & encoded_image,const webrtc::CodecSpecificInfo * codec_specific_info)112 EncodeCallbackTask(VideoProcessor* video_processor, 113 const webrtc::EncodedImage& encoded_image, 114 const webrtc::CodecSpecificInfo* codec_specific_info) 115 : video_processor_(video_processor), 116 encoded_image_(encoded_image), 117 codec_specific_info_(*codec_specific_info) { 118 encoded_image_.Retain(); 119 } 120 Run()121 bool Run() override { 122 video_processor_->FrameEncoded(encoded_image_, codec_specific_info_); 123 return true; 124 } 125 126 private: 127 VideoProcessor* const video_processor_; 128 webrtc::EncodedImage encoded_image_; 129 const webrtc::CodecSpecificInfo codec_specific_info_; 130 }; 131 132 VideoProcessor* const video_processor_; 133 TaskQueueBase* const task_queue_; 134 }; 135 136 class VideoProcessorDecodeCompleteCallback 137 : public webrtc::DecodedImageCallback { 138 public: VideoProcessorDecodeCompleteCallback(VideoProcessor * video_processor,size_t simulcast_svc_idx)139 explicit VideoProcessorDecodeCompleteCallback( 140 VideoProcessor* video_processor, 141 size_t simulcast_svc_idx) 142 : video_processor_(video_processor), 143 simulcast_svc_idx_(simulcast_svc_idx), 144 task_queue_(TaskQueueBase::Current()) { 145 RTC_DCHECK(video_processor_); 146 RTC_DCHECK(task_queue_); 147 } 148 149 int32_t Decoded(webrtc::VideoFrame& image) override; 150 Decoded(webrtc::VideoFrame & image,int64_t decode_time_ms)151 int32_t Decoded(webrtc::VideoFrame& image, 152 int64_t decode_time_ms) override { 153 return Decoded(image); 154 } 155 Decoded(webrtc::VideoFrame & image,absl::optional<int32_t> decode_time_ms,absl::optional<uint8_t> qp)156 void Decoded(webrtc::VideoFrame& image, 157 absl::optional<int32_t> decode_time_ms, 158 absl::optional<uint8_t> qp) override { 159 Decoded(image); 160 } 161 162 private: 163 VideoProcessor* const video_processor_; 164 const size_t simulcast_svc_idx_; 165 TaskQueueBase* const task_queue_; 166 }; 167 168 // Invoked by the callback adapter when a frame has completed encoding. 169 void FrameEncoded(const webrtc::EncodedImage& encoded_image, 170 const webrtc::CodecSpecificInfo& codec_specific); 171 172 // Invoked by the callback adapter when a frame has completed decoding. 173 void FrameDecoded(const webrtc::VideoFrame& image, size_t simulcast_svc_idx); 174 175 void DecodeFrame(const EncodedImage& encoded_image, size_t simulcast_svc_idx); 176 177 // In order to supply the SVC decoders with super frames containing all 178 // lower layer frames, we merge and store the layer frames in this method. 179 const webrtc::EncodedImage* BuildAndStoreSuperframe( 180 const EncodedImage& encoded_image, 181 const VideoCodecType codec, 182 size_t frame_number, 183 size_t simulcast_svc_idx, 184 bool inter_layer_predicted) RTC_RUN_ON(sequence_checker_); 185 186 // Test input/output. 187 VideoCodecTestFixture::Config config_ RTC_GUARDED_BY(sequence_checker_); 188 const size_t num_simulcast_or_spatial_layers_; 189 VideoCodecTestStatsImpl* const stats_; 190 191 // Codecs. 192 webrtc::VideoEncoder* const encoder_; 193 VideoDecoderList* const decoders_; 194 const std::unique_ptr<VideoBitrateAllocator> bitrate_allocator_; 195 VideoBitrateAllocation bitrate_allocation_ RTC_GUARDED_BY(sequence_checker_); 196 double framerate_fps_ 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::Buffer> 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 // This class must be operated on a TaskQueue. 253 SequenceChecker sequence_checker_; 254 255 RTC_DISALLOW_COPY_AND_ASSIGN(VideoProcessor); 256 }; 257 258 } // namespace test 259 } // namespace webrtc 260 261 #endif // MODULES_VIDEO_CODING_CODECS_TEST_VIDEOPROCESSOR_H_ 262