| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #ifndef WEBRTC_MODULES_VIDEO_CODING_CODECS_TEST_VIDEOPROCESSOR_H_ | 11 #ifndef WEBRTC_MODULES_VIDEO_CODING_CODECS_TEST_VIDEOPROCESSOR_H_ |
| 12 #define WEBRTC_MODULES_VIDEO_CODING_CODECS_TEST_VIDEOPROCESSOR_H_ | 12 #define WEBRTC_MODULES_VIDEO_CODING_CODECS_TEST_VIDEOPROCESSOR_H_ |
| 13 | 13 |
| 14 #include <memory> | 14 #include <memory> |
| 15 #include <string> | 15 #include <string> |
| 16 #include <vector> | 16 #include <vector> |
| 17 | 17 |
| 18 #include "webrtc/api/video/video_frame.h" | 18 #include "webrtc/api/video/video_frame.h" |
| 19 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" | 19 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" |
| 20 #include "webrtc/modules/video_coding/codecs/test/packet_manipulator.h" | 20 #include "webrtc/modules/video_coding/codecs/test/packet_manipulator.h" |
| 21 #include "webrtc/modules/video_coding/codecs/test/stats.h" | 21 #include "webrtc/modules/video_coding/codecs/test/stats.h" |
| 22 #include "webrtc/modules/video_coding/include/video_codec_interface.h" | 22 #include "webrtc/modules/video_coding/include/video_codec_interface.h" |
| 23 #include "webrtc/modules/video_coding/utility/ivf_file_writer.h" | 23 #include "webrtc/modules/video_coding/utility/ivf_file_writer.h" |
| 24 #include "webrtc/modules/video_coding/utility/vp8_header_parser.h" | 24 #include "webrtc/modules/video_coding/utility/vp8_header_parser.h" |
| 25 #include "webrtc/modules/video_coding/utility/vp9_uncompressed_header_parser.h" | 25 #include "webrtc/modules/video_coding/utility/vp9_uncompressed_header_parser.h" |
| 26 #include "webrtc/rtc_base/buffer.h" | 26 #include "webrtc/rtc_base/buffer.h" |
| 27 #include "webrtc/rtc_base/checks.h" | 27 #include "webrtc/rtc_base/checks.h" |
| 28 #include "webrtc/rtc_base/constructormagic.h" |
| 29 #include "webrtc/rtc_base/sequenced_task_checker.h" |
| 30 #include "webrtc/rtc_base/task_queue.h" |
| 28 #include "webrtc/test/testsupport/frame_reader.h" | 31 #include "webrtc/test/testsupport/frame_reader.h" |
| 29 #include "webrtc/test/testsupport/frame_writer.h" | 32 #include "webrtc/test/testsupport/frame_writer.h" |
| 30 | 33 |
| 31 namespace webrtc { | 34 namespace webrtc { |
| 32 | 35 |
| 33 class VideoBitrateAllocator; | 36 class VideoBitrateAllocator; |
| 34 | 37 |
| 35 namespace test { | 38 namespace test { |
| 36 | 39 |
| 37 // Defines which frame types shall be excluded from packet loss and when. | 40 // Defines which frame types shall be excluded from packet loss and when. |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 // The codec settings to use for the test (target bitrate, video size, | 106 // The codec settings to use for the test (target bitrate, video size, |
| 104 // framerate and so on). This struct should be filled in using the | 107 // framerate and so on). This struct should be filled in using the |
| 105 // VideoCodingModule::Codec() method. | 108 // VideoCodingModule::Codec() method. |
| 106 webrtc::VideoCodec codec_settings; | 109 webrtc::VideoCodec codec_settings; |
| 107 | 110 |
| 108 // If printing of information to stdout shall be performed during processing. | 111 // If printing of information to stdout shall be performed during processing. |
| 109 bool verbose = true; | 112 bool verbose = true; |
| 110 | 113 |
| 111 // If HW or SW codec should be used. | 114 // If HW or SW codec should be used. |
| 112 bool hw_codec = false; | 115 bool hw_codec = false; |
| 113 | |
| 114 // In batch mode, the VideoProcessor is fed all the frames for processing | |
| 115 // before any metrics are calculated. This is useful for pipelining HW codecs, | |
| 116 // for which some calculated metrics otherwise would be incorrect. The | |
| 117 // downside with batch mode is that mid-test rate allocation is not supported. | |
| 118 bool batch_mode = false; | |
| 119 }; | 116 }; |
| 120 | 117 |
| 121 // Handles encoding/decoding of video using the VideoEncoder/VideoDecoder | 118 // Handles encoding/decoding of video using the VideoEncoder/VideoDecoder |
| 122 // interfaces. This is done in a sequential manner in order to be able to | 119 // interfaces. This is done in a sequential manner in order to be able to |
| 123 // measure times properly. | 120 // measure times properly. |
| 124 // The class processes a frame at the time for the configured input file. | 121 // The class processes a frame at the time for the configured input file. |
| 125 // It maintains state of where in the source input file the processing is at. | 122 // It maintains state of where in the source input file the processing is at. |
| 126 // | 123 // |
| 127 // Regarding packet loss: Note that keyframes are excluded (first or all | 124 // Regarding packet loss: Note that keyframes are excluded (first or all |
| 128 // depending on the ExcludeFrameTypes setting). This is because if key frames | 125 // depending on the ExcludeFrameTypes setting). This is because if key frames |
| (...skipping 19 matching lines...) Expand all Loading... |
| 148 IvfFileWriter* encoded_frame_writer, | 145 IvfFileWriter* encoded_frame_writer, |
| 149 FrameWriter* decoded_frame_writer); | 146 FrameWriter* decoded_frame_writer); |
| 150 ~VideoProcessor(); | 147 ~VideoProcessor(); |
| 151 | 148 |
| 152 // Sets up callbacks and initializes the encoder and decoder. | 149 // Sets up callbacks and initializes the encoder and decoder. |
| 153 void Init(); | 150 void Init(); |
| 154 | 151 |
| 155 // Tears down callbacks and releases the encoder and decoder. | 152 // Tears down callbacks and releases the encoder and decoder. |
| 156 void Release(); | 153 void Release(); |
| 157 | 154 |
| 158 // Processes a single frame. Returns true as long as there's more frames | 155 // Processes a single frame. The frames must be processed in order. |
| 159 // available in the source clip. | 156 void ProcessFrame(int frame_number); |
| 160 // |frame_number| must be an integer >= 0. | |
| 161 bool ProcessFrame(int frame_number); | |
| 162 | 157 |
| 163 // Updates the encoder with the target |bit_rate| and the |frame_rate|. | 158 // Updates the encoder with target rates. Must be called at least once. |
| 164 void SetRates(int bit_rate, int frame_rate); | 159 void SetRates(int bitrate_bps, int framerate_fps); |
| 165 | 160 |
| 166 // Return the size of the encoded frame in bytes. Dropped frames by the | 161 // Returns the number of frames that have been decoded. |
| 167 // encoder are regarded as zero size. | 162 int NumFramesDecoded() const; |
| 168 size_t EncodedFrameSize(int frame_number); | |
| 169 | 163 |
| 170 // Return the encoded frame type (key or delta). | 164 // TODO(brandtr): Get rid of these functions by moving the corresponding QP |
| 171 FrameType EncodedFrameType(int frame_number); | 165 // fields to the Stats object. |
| 166 int GetQpFromEncoder(int frame_number) const; |
| 167 int GetQpFromBitstream(int frame_number) const; |
| 172 | 168 |
| 173 // Return the qp used by encoder. | 169 // Returns the number of dropped frames. |
| 174 int GetQpFromEncoder(int frame_number); | 170 std::vector<int> NumberDroppedFramesPerRateUpdate() const; |
| 175 | 171 |
| 176 // Return the qp from the qp parser. | 172 // Returns the number of spatial resizes. |
| 177 int GetQpFromBitstream(int frame_number); | 173 std::vector<int> NumberSpatialResizesPerRateUpdate() const; |
| 178 | |
| 179 // Return the number of dropped frames. | |
| 180 int NumberDroppedFrames(); | |
| 181 | |
| 182 // Return the number of spatial resizes. | |
| 183 int NumberSpatialResizes(); | |
| 184 | 174 |
| 185 private: | 175 private: |
| 186 // Container that holds per-frame information that needs to be stored between | 176 // Container that holds per-frame information that needs to be stored between |
| 187 // calls to Encode and Decode, as well as the corresponding callbacks. It is | 177 // calls to Encode and Decode, as well as the corresponding callbacks. It is |
| 188 // not directly used for statistics -- for that, test::FrameStatistic is used. | 178 // not directly used for statistics -- for that, test::FrameStatistic is used. |
| 179 // TODO(brandtr): Get rid of this struct and use the Stats class instead. |
| 189 struct FrameInfo { | 180 struct FrameInfo { |
| 190 FrameInfo() | 181 int64_t encode_start_ns = 0; |
| 191 : timestamp(0), | 182 int64_t decode_start_ns = 0; |
| 192 encode_start_ns(0), | 183 int qp_encoder = 0; |
| 193 decode_start_ns(0), | 184 int qp_bitstream = 0; |
| 194 encoded_frame_size(0), | 185 int decoded_width = 0; |
| 195 encoded_frame_type(kVideoFrameDelta), | 186 int decoded_height = 0; |
| 196 decoded_width(0), | 187 size_t manipulated_length = 0; |
| 197 decoded_height(0), | |
| 198 manipulated_length(0), | |
| 199 qp_encoder(0), | |
| 200 qp_bitstream(0) {} | |
| 201 | |
| 202 uint32_t timestamp; | |
| 203 int64_t encode_start_ns; | |
| 204 int64_t decode_start_ns; | |
| 205 size_t encoded_frame_size; | |
| 206 FrameType encoded_frame_type; | |
| 207 int decoded_width; | |
| 208 int decoded_height; | |
| 209 size_t manipulated_length; | |
| 210 int qp_encoder; | |
| 211 int qp_bitstream; | |
| 212 }; | 188 }; |
| 213 | 189 |
| 214 // Callback class required to implement according to the VideoEncoder API. | |
| 215 class VideoProcessorEncodeCompleteCallback | 190 class VideoProcessorEncodeCompleteCallback |
| 216 : public webrtc::EncodedImageCallback { | 191 : public webrtc::EncodedImageCallback { |
| 217 public: | 192 public: |
| 218 explicit VideoProcessorEncodeCompleteCallback( | 193 explicit VideoProcessorEncodeCompleteCallback( |
| 219 VideoProcessor* video_processor) | 194 VideoProcessor* video_processor) |
| 220 : video_processor_(video_processor) {} | 195 : video_processor_(video_processor), |
| 196 task_queue_(rtc::TaskQueue::Current()) {} |
| 197 |
| 221 Result OnEncodedImage( | 198 Result OnEncodedImage( |
| 222 const webrtc::EncodedImage& encoded_image, | 199 const webrtc::EncodedImage& encoded_image, |
| 223 const webrtc::CodecSpecificInfo* codec_specific_info, | 200 const webrtc::CodecSpecificInfo* codec_specific_info, |
| 224 const webrtc::RTPFragmentationHeader* fragmentation) override { | 201 const webrtc::RTPFragmentationHeader* fragmentation) override { |
| 225 // Forward to parent class. | |
| 226 RTC_CHECK(codec_specific_info); | 202 RTC_CHECK(codec_specific_info); |
| 203 |
| 204 if (task_queue_ && !task_queue_->IsCurrent()) { |
| 205 task_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>( |
| 206 new EncodeCallbackTask(video_processor_, encoded_image, |
| 207 codec_specific_info, fragmentation))); |
| 208 return Result(Result::OK, 0); |
| 209 } |
| 210 |
| 227 video_processor_->FrameEncoded(codec_specific_info->codecType, | 211 video_processor_->FrameEncoded(codec_specific_info->codecType, |
| 228 encoded_image, fragmentation); | 212 encoded_image, fragmentation); |
| 229 return Result(Result::OK, 0); | 213 return Result(Result::OK, 0); |
| 230 } | 214 } |
| 231 | 215 |
| 232 private: | 216 private: |
| 217 class EncodeCallbackTask : public rtc::QueuedTask { |
| 218 public: |
| 219 EncodeCallbackTask(VideoProcessor* video_processor, |
| 220 const webrtc::EncodedImage& encoded_image, |
| 221 const webrtc::CodecSpecificInfo* codec_specific_info, |
| 222 const webrtc::RTPFragmentationHeader* fragmentation) |
| 223 : video_processor_(video_processor), |
| 224 buffer_(encoded_image._buffer, encoded_image._length), |
| 225 encoded_image_(encoded_image), |
| 226 codec_specific_info_(*codec_specific_info) { |
| 227 encoded_image_._buffer = buffer_.data(); |
| 228 RTC_CHECK(fragmentation); |
| 229 fragmentation_.CopyFrom(*fragmentation); |
| 230 } |
| 231 |
| 232 bool Run() override { |
| 233 video_processor_->FrameEncoded(codec_specific_info_.codecType, |
| 234 encoded_image_, &fragmentation_); |
| 235 return true; |
| 236 } |
| 237 |
| 238 private: |
| 239 VideoProcessor* const video_processor_; |
| 240 rtc::Buffer buffer_; |
| 241 webrtc::EncodedImage encoded_image_; |
| 242 const webrtc::CodecSpecificInfo codec_specific_info_; |
| 243 webrtc::RTPFragmentationHeader fragmentation_; |
| 244 }; |
| 245 |
| 233 VideoProcessor* const video_processor_; | 246 VideoProcessor* const video_processor_; |
| 247 rtc::TaskQueue* const task_queue_; |
| 234 }; | 248 }; |
| 235 | 249 |
| 236 // Callback class required to implement according to the VideoDecoder API. | |
| 237 class VideoProcessorDecodeCompleteCallback | 250 class VideoProcessorDecodeCompleteCallback |
| 238 : public webrtc::DecodedImageCallback { | 251 : public webrtc::DecodedImageCallback { |
| 239 public: | 252 public: |
| 240 explicit VideoProcessorDecodeCompleteCallback( | 253 explicit VideoProcessorDecodeCompleteCallback( |
| 241 VideoProcessor* video_processor) | 254 VideoProcessor* video_processor) |
| 242 : video_processor_(video_processor) {} | 255 : video_processor_(video_processor), |
| 256 task_queue_(rtc::TaskQueue::Current()) {} |
| 257 |
| 243 int32_t Decoded(webrtc::VideoFrame& image) override { | 258 int32_t Decoded(webrtc::VideoFrame& image) override { |
| 244 // Forward to parent class. | 259 if (task_queue_ && !task_queue_->IsCurrent()) { |
| 260 task_queue_->PostTask( |
| 261 [this, image]() { video_processor_->FrameDecoded(image); }); |
| 262 return 0; |
| 263 } |
| 264 |
| 245 video_processor_->FrameDecoded(image); | 265 video_processor_->FrameDecoded(image); |
| 246 return 0; | 266 return 0; |
| 247 } | 267 } |
| 268 |
| 248 int32_t Decoded(webrtc::VideoFrame& image, | 269 int32_t Decoded(webrtc::VideoFrame& image, |
| 249 int64_t decode_time_ms) override { | 270 int64_t decode_time_ms) override { |
| 250 return Decoded(image); | 271 return Decoded(image); |
| 251 } | 272 } |
| 273 |
| 252 void Decoded(webrtc::VideoFrame& image, | 274 void Decoded(webrtc::VideoFrame& image, |
| 253 rtc::Optional<int32_t> decode_time_ms, | 275 rtc::Optional<int32_t> decode_time_ms, |
| 254 rtc::Optional<uint8_t> qp) override { | 276 rtc::Optional<uint8_t> qp) override { |
| 255 Decoded(image); | 277 Decoded(image); |
| 256 } | 278 } |
| 257 | 279 |
| 258 private: | 280 private: |
| 259 VideoProcessor* const video_processor_; | 281 VideoProcessor* const video_processor_; |
| 282 rtc::TaskQueue* const task_queue_; |
| 260 }; | 283 }; |
| 261 | 284 |
| 262 // Invoked by the callback when a frame has completed encoding. | 285 // Invoked by the callback adapter when a frame has completed encoding. |
| 263 void FrameEncoded(webrtc::VideoCodecType codec, | 286 void FrameEncoded(webrtc::VideoCodecType codec, |
| 264 const webrtc::EncodedImage& encodedImage, | 287 const webrtc::EncodedImage& encodedImage, |
| 265 const webrtc::RTPFragmentationHeader* fragmentation); | 288 const webrtc::RTPFragmentationHeader* fragmentation); |
| 266 | 289 |
| 267 // Invoked by the callback when a frame has completed decoding. | 290 // Invoked by the callback adapter when a frame has completed decoding. |
| 268 void FrameDecoded(const webrtc::VideoFrame& image); | 291 void FrameDecoded(const webrtc::VideoFrame& image); |
| 269 | 292 |
| 270 // Use the frame number as the basis for timestamp to identify frames. Let the | 293 // Use the frame number as the basis for timestamp to identify frames. Let the |
| 271 // first timestamp be non-zero, to not make the IvfFileWriter believe that we | 294 // first timestamp be non-zero, to not make the IvfFileWriter believe that we |
| 272 // want to use capture timestamps in the IVF files. | 295 // want to use capture timestamps in the IVF files. |
| 273 uint32_t FrameNumberToTimestamp(int frame_number); | 296 uint32_t FrameNumberToTimestamp(int frame_number) const; |
| 274 int TimestampToFrameNumber(uint32_t timestamp); | 297 int TimestampToFrameNumber(uint32_t timestamp) const; |
| 275 | 298 |
| 276 TestConfig config_; | 299 bool initialized_ GUARDED_BY(sequence_checker_); |
| 300 |
| 301 TestConfig config_ GUARDED_BY(sequence_checker_); |
| 277 | 302 |
| 278 webrtc::VideoEncoder* const encoder_; | 303 webrtc::VideoEncoder* const encoder_; |
| 279 webrtc::VideoDecoder* const decoder_; | 304 webrtc::VideoDecoder* const decoder_; |
| 280 const std::unique_ptr<VideoBitrateAllocator> bitrate_allocator_; | 305 const std::unique_ptr<VideoBitrateAllocator> bitrate_allocator_; |
| 281 | 306 |
| 282 // Adapters for the codec callbacks. | 307 // Adapters for the codec callbacks. |
| 283 const std::unique_ptr<EncodedImageCallback> encode_callback_; | 308 VideoProcessorEncodeCompleteCallback encode_callback_; |
| 284 const std::unique_ptr<DecodedImageCallback> decode_callback_; | 309 VideoProcessorDecodeCompleteCallback decode_callback_; |
| 310 |
| 311 int num_frames_decoded_; |
| 285 | 312 |
| 286 // Fake network. | 313 // Fake network. |
| 287 PacketManipulator* const packet_manipulator_; | 314 PacketManipulator* const packet_manipulator_; |
| 288 | 315 |
| 289 // These (mandatory) file manipulators are used for, e.g., objective PSNR and | 316 // These (mandatory) file manipulators are used for, e.g., objective PSNR and |
| 290 // SSIM calculations at the end of a test run. | 317 // SSIM calculations at the end of a test run. |
| 291 FrameReader* const analysis_frame_reader_; | 318 FrameReader* const analysis_frame_reader_; |
| 292 FrameWriter* const analysis_frame_writer_; | 319 FrameWriter* const analysis_frame_writer_; |
| 293 | 320 |
| 294 // These (optional) file writers are used to persistently store the encoded | 321 // These (optional) file writers are used to persistently store the encoded |
| 295 // and decoded bitstreams. The purpose is to give the experimenter an option | 322 // and decoded bitstreams. The purpose is to give the experimenter an option |
| 296 // to subjectively evaluate the quality of the processing. Each frame writer | 323 // to subjectively evaluate the quality of the processing. Each frame writer |
| 297 // is enabled by being non-null. | 324 // is enabled by being non-null. |
| 298 IvfFileWriter* const encoded_frame_writer_; | 325 IvfFileWriter* const encoded_frame_writer_; |
| 299 FrameWriter* const decoded_frame_writer_; | 326 FrameWriter* const decoded_frame_writer_; |
| 300 | 327 |
| 301 bool initialized_; | |
| 302 | |
| 303 // Frame metadata for all frames that have been added through a call to | 328 // Frame metadata for all frames that have been added through a call to |
| 304 // ProcessFrames(). We need to store this metadata over the course of the | 329 // ProcessFrames(). We need to store this metadata over the course of the |
| 305 // test run, to support pipelining HW codecs. | 330 // test run, to support pipelining HW codecs. |
| 306 std::vector<FrameInfo> frame_infos_; | 331 std::vector<FrameInfo> frame_infos_ GUARDED_BY(sequence_checker_); |
| 307 int last_encoded_frame_num_; | 332 int last_encoded_frame_num_ GUARDED_BY(sequence_checker_); |
| 308 int last_decoded_frame_num_; | 333 int last_decoded_frame_num_ GUARDED_BY(sequence_checker_); |
| 309 | 334 |
| 310 // Keep track of if we have excluded the first key frame from packet loss. | 335 // Keep track of if we have excluded the first key frame from packet loss. |
| 311 bool first_key_frame_has_been_excluded_; | 336 bool first_key_frame_has_been_excluded_ GUARDED_BY(sequence_checker_); |
| 312 | 337 |
| 313 // Keep track of the last successfully decoded frame, since we write that | 338 // Keep track of the last successfully decoded frame, since we write that |
| 314 // frame to disk when decoding fails. | 339 // frame to disk when decoding fails. |
| 315 rtc::Buffer last_decoded_frame_buffer_; | 340 rtc::Buffer last_decoded_frame_buffer_ GUARDED_BY(sequence_checker_); |
| 316 | 341 |
| 317 // Statistics. | 342 // Statistics. |
| 318 Stats* stats_; | 343 Stats* stats_; |
| 319 int num_dropped_frames_; | 344 std::vector<int> num_dropped_frames_ GUARDED_BY(sequence_checker_); |
| 320 int num_spatial_resizes_; | 345 std::vector<int> num_spatial_resizes_ GUARDED_BY(sequence_checker_); |
| 346 int rate_update_index_ GUARDED_BY(sequence_checker_); |
| 347 |
| 348 rtc::SequencedTaskChecker sequence_checker_; |
| 349 |
| 350 RTC_DISALLOW_COPY_AND_ASSIGN(VideoProcessor); |
| 321 }; | 351 }; |
| 322 | 352 |
| 323 } // namespace test | 353 } // namespace test |
| 324 } // namespace webrtc | 354 } // namespace webrtc |
| 325 | 355 |
| 326 #endif // WEBRTC_MODULES_VIDEO_CODING_CODECS_TEST_VIDEOPROCESSOR_H_ | 356 #endif // WEBRTC_MODULES_VIDEO_CODING_CODECS_TEST_VIDEOPROCESSOR_H_ |
| OLD | NEW |