| 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 |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 | 114 |
| 115 VideoProcessor::VideoProcessor(webrtc::VideoEncoder* encoder, | 115 VideoProcessor::VideoProcessor(webrtc::VideoEncoder* encoder, |
| 116 webrtc::VideoDecoder* decoder, | 116 webrtc::VideoDecoder* decoder, |
| 117 FrameReader* analysis_frame_reader, | 117 FrameReader* analysis_frame_reader, |
| 118 FrameWriter* analysis_frame_writer, | 118 FrameWriter* analysis_frame_writer, |
| 119 PacketManipulator* packet_manipulator, | 119 PacketManipulator* packet_manipulator, |
| 120 const TestConfig& config, | 120 const TestConfig& config, |
| 121 Stats* stats, | 121 Stats* stats, |
| 122 IvfFileWriter* encoded_frame_writer, | 122 IvfFileWriter* encoded_frame_writer, |
| 123 FrameWriter* decoded_frame_writer) | 123 FrameWriter* decoded_frame_writer) |
| 124 : config_(config), | 124 : initialized_(false), |
| 125 config_(config), |
| 125 encoder_(encoder), | 126 encoder_(encoder), |
| 126 decoder_(decoder), | 127 decoder_(decoder), |
| 127 bitrate_allocator_(CreateBitrateAllocator(&config_)), | 128 bitrate_allocator_(CreateBitrateAllocator(&config_)), |
| 128 encode_callback_(this), | 129 encode_callback_(this), |
| 129 decode_callback_(this), | 130 decode_callback_(this), |
| 130 packet_manipulator_(packet_manipulator), | 131 packet_manipulator_(packet_manipulator), |
| 131 analysis_frame_reader_(analysis_frame_reader), | 132 analysis_frame_reader_(analysis_frame_reader), |
| 132 analysis_frame_writer_(analysis_frame_writer), | 133 analysis_frame_writer_(analysis_frame_writer), |
| 133 encoded_frame_writer_(encoded_frame_writer), | 134 encoded_frame_writer_(encoded_frame_writer), |
| 134 decoded_frame_writer_(decoded_frame_writer), | 135 decoded_frame_writer_(decoded_frame_writer), |
| 135 initialized_(false), | |
| 136 last_encoded_frame_num_(-1), | 136 last_encoded_frame_num_(-1), |
| 137 last_decoded_frame_num_(-1), | 137 last_decoded_frame_num_(-1), |
| 138 first_key_frame_has_been_excluded_(false), | 138 first_key_frame_has_been_excluded_(false), |
| 139 last_decoded_frame_buffer_(analysis_frame_reader->FrameLength()), | 139 last_decoded_frame_buffer_(analysis_frame_reader->FrameLength()), |
| 140 stats_(stats), | 140 stats_(stats), |
| 141 num_dropped_frames_(0), | 141 num_dropped_frames_(0), |
| 142 num_spatial_resizes_(0) { | 142 num_spatial_resizes_(0) { |
| 143 RTC_DCHECK(encoder); | 143 RTC_DCHECK(encoder); |
| 144 RTC_DCHECK(decoder); | 144 RTC_DCHECK(decoder); |
| 145 RTC_DCHECK(packet_manipulator); | 145 RTC_DCHECK(packet_manipulator); |
| 146 RTC_DCHECK(analysis_frame_reader); | 146 RTC_DCHECK(analysis_frame_reader); |
| 147 RTC_DCHECK(analysis_frame_writer); | 147 RTC_DCHECK(analysis_frame_writer); |
| 148 RTC_DCHECK(stats); | 148 RTC_DCHECK(stats); |
| 149 frame_infos_.reserve(analysis_frame_reader->NumberOfFrames()); | 149 frame_infos_.reserve(analysis_frame_reader->NumberOfFrames()); |
| 150 } | 150 } |
| 151 | 151 |
| 152 VideoProcessor::~VideoProcessor() = default; | 152 VideoProcessor::~VideoProcessor() = default; |
| 153 | 153 |
| 154 void VideoProcessor::Init() { | 154 void VideoProcessor::Init() { |
| 155 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
| 155 RTC_DCHECK(!initialized_) << "VideoProcessor already initialized."; | 156 RTC_DCHECK(!initialized_) << "VideoProcessor already initialized."; |
| 156 initialized_ = true; | 157 initialized_ = true; |
| 157 | 158 |
| 158 // Setup required callbacks for the encoder and decoder. | 159 // Setup required callbacks for the encoder and decoder. |
| 159 RTC_CHECK_EQ(encoder_->RegisterEncodeCompleteCallback(&encode_callback_), | 160 RTC_CHECK_EQ(encoder_->RegisterEncodeCompleteCallback(&encode_callback_), |
| 160 WEBRTC_VIDEO_CODEC_OK) | 161 WEBRTC_VIDEO_CODEC_OK) |
| 161 << "Failed to register encode complete callback"; | 162 << "Failed to register encode complete callback"; |
| 162 RTC_CHECK_EQ(decoder_->RegisterDecodeCompleteCallback(&decode_callback_), | 163 RTC_CHECK_EQ(decoder_->RegisterDecodeCompleteCallback(&decode_callback_), |
| 163 WEBRTC_VIDEO_CODEC_OK) | 164 WEBRTC_VIDEO_CODEC_OK) |
| 164 << "Failed to register decode complete callback"; | 165 << "Failed to register decode complete callback"; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 191 CodecTypeToPayloadName(config_.codec_settings.codecType) | 192 CodecTypeToPayloadName(config_.codec_settings.codecType) |
| 192 .value_or("Unknown"), | 193 .value_or("Unknown"), |
| 193 encoder_->ImplementationName()); | 194 encoder_->ImplementationName()); |
| 194 } | 195 } |
| 195 PrintCodecSettings(config_.codec_settings); | 196 PrintCodecSettings(config_.codec_settings); |
| 196 printf("\n"); | 197 printf("\n"); |
| 197 } | 198 } |
| 198 } | 199 } |
| 199 | 200 |
| 200 void VideoProcessor::Release() { | 201 void VideoProcessor::Release() { |
| 202 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
| 203 |
| 201 RTC_CHECK_EQ(encoder_->Release(), WEBRTC_VIDEO_CODEC_OK); | 204 RTC_CHECK_EQ(encoder_->Release(), WEBRTC_VIDEO_CODEC_OK); |
| 202 RTC_CHECK_EQ(decoder_->Release(), WEBRTC_VIDEO_CODEC_OK); | 205 RTC_CHECK_EQ(decoder_->Release(), WEBRTC_VIDEO_CODEC_OK); |
| 203 | 206 |
| 204 encoder_->RegisterEncodeCompleteCallback(nullptr); | 207 encoder_->RegisterEncodeCompleteCallback(nullptr); |
| 205 decoder_->RegisterDecodeCompleteCallback(nullptr); | 208 decoder_->RegisterDecodeCompleteCallback(nullptr); |
| 206 | 209 |
| 207 initialized_ = false; | 210 initialized_ = false; |
| 208 } | 211 } |
| 209 | 212 |
| 210 void VideoProcessor::ProcessFrame(int frame_number) { | 213 void VideoProcessor::ProcessFrame(int frame_number) { |
| 214 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
| 211 RTC_DCHECK_EQ(frame_number, frame_infos_.size()) | 215 RTC_DCHECK_EQ(frame_number, frame_infos_.size()) |
| 212 << "Must process frames in sequence."; | 216 << "Must process frames in sequence."; |
| 213 RTC_DCHECK(initialized_) << "VideoProcessor not initialized."; | 217 RTC_DCHECK(initialized_) << "VideoProcessor not initialized."; |
| 214 | 218 |
| 215 // Get frame from file. | 219 // Get frame from file. |
| 216 rtc::scoped_refptr<I420BufferInterface> buffer( | 220 rtc::scoped_refptr<I420BufferInterface> buffer( |
| 217 analysis_frame_reader_->ReadFrame()); | 221 analysis_frame_reader_->ReadFrame()); |
| 218 RTC_CHECK(buffer) << "Tried to read too many frames from the file."; | 222 RTC_CHECK(buffer) << "Tried to read too many frames from the file."; |
| 219 const int64_t kNoRenderTime = 0; | 223 const int64_t kNoRenderTime = 0; |
| 220 VideoFrame source_frame(buffer, FrameNumberToTimestamp(frame_number), | 224 VideoFrame source_frame(buffer, FrameNumberToTimestamp(frame_number), |
| (...skipping 20 matching lines...) Expand all Loading... |
| 241 encoder_->Encode(source_frame, nullptr, &frame_types); | 245 encoder_->Encode(source_frame, nullptr, &frame_types); |
| 242 | 246 |
| 243 if (frame_stat->encode_return_code != WEBRTC_VIDEO_CODEC_OK) { | 247 if (frame_stat->encode_return_code != WEBRTC_VIDEO_CODEC_OK) { |
| 244 LOG(LS_WARNING) << "Failed to encode frame " << frame_number | 248 LOG(LS_WARNING) << "Failed to encode frame " << frame_number |
| 245 << ", return code: " << frame_stat->encode_return_code | 249 << ", return code: " << frame_stat->encode_return_code |
| 246 << "."; | 250 << "."; |
| 247 } | 251 } |
| 248 } | 252 } |
| 249 | 253 |
| 250 void VideoProcessor::SetRates(int bitrate_kbps, int framerate_fps) { | 254 void VideoProcessor::SetRates(int bitrate_kbps, int framerate_fps) { |
| 255 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
| 256 |
| 251 config_.codec_settings.maxFramerate = framerate_fps; | 257 config_.codec_settings.maxFramerate = framerate_fps; |
| 252 int set_rates_result = encoder_->SetRateAllocation( | 258 int set_rates_result = encoder_->SetRateAllocation( |
| 253 bitrate_allocator_->GetAllocation(bitrate_kbps * 1000, framerate_fps), | 259 bitrate_allocator_->GetAllocation(bitrate_kbps * 1000, framerate_fps), |
| 254 framerate_fps); | 260 framerate_fps); |
| 255 RTC_DCHECK_GE(set_rates_result, 0) | 261 RTC_DCHECK_GE(set_rates_result, 0) |
| 256 << "Failed to update encoder with new rate " << bitrate_kbps << "."; | 262 << "Failed to update encoder with new rate " << bitrate_kbps << "."; |
| 257 num_dropped_frames_ = 0; | 263 num_dropped_frames_ = 0; |
| 258 num_spatial_resizes_ = 0; | 264 num_spatial_resizes_ = 0; |
| 259 } | 265 } |
| 260 | 266 |
| 261 int VideoProcessor::GetQpFromEncoder(int frame_number) const { | 267 int VideoProcessor::GetQpFromEncoder(int frame_number) const { |
| 268 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
| 262 RTC_CHECK_LT(frame_number, frame_infos_.size()); | 269 RTC_CHECK_LT(frame_number, frame_infos_.size()); |
| 263 return frame_infos_[frame_number].qp_encoder; | 270 return frame_infos_[frame_number].qp_encoder; |
| 264 } | 271 } |
| 265 | 272 |
| 266 int VideoProcessor::GetQpFromBitstream(int frame_number) const { | 273 int VideoProcessor::GetQpFromBitstream(int frame_number) const { |
| 274 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
| 267 RTC_CHECK_LT(frame_number, frame_infos_.size()); | 275 RTC_CHECK_LT(frame_number, frame_infos_.size()); |
| 268 return frame_infos_[frame_number].qp_bitstream; | 276 return frame_infos_[frame_number].qp_bitstream; |
| 269 } | 277 } |
| 270 | 278 |
| 271 int VideoProcessor::NumberDroppedFrames() { | 279 int VideoProcessor::NumberDroppedFrames() { |
| 280 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
| 272 return num_dropped_frames_; | 281 return num_dropped_frames_; |
| 273 } | 282 } |
| 274 | 283 |
| 275 int VideoProcessor::NumberSpatialResizes() { | 284 int VideoProcessor::NumberSpatialResizes() { |
| 285 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
| 276 return num_spatial_resizes_; | 286 return num_spatial_resizes_; |
| 277 } | 287 } |
| 278 | 288 |
| 279 void VideoProcessor::FrameEncoded( | 289 void VideoProcessor::FrameEncoded( |
| 280 webrtc::VideoCodecType codec, | 290 webrtc::VideoCodecType codec, |
| 281 const EncodedImage& encoded_image, | 291 const EncodedImage& encoded_image, |
| 282 const webrtc::RTPFragmentationHeader* fragmentation) { | 292 const webrtc::RTPFragmentationHeader* fragmentation) { |
| 293 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
| 294 |
| 283 // For the highest measurement accuracy of the encode time, the start/stop | 295 // For the highest measurement accuracy of the encode time, the start/stop |
| 284 // time recordings should wrap the Encode call as tightly as possible. | 296 // time recordings should wrap the Encode call as tightly as possible. |
| 285 int64_t encode_stop_ns = rtc::TimeNanos(); | 297 int64_t encode_stop_ns = rtc::TimeNanos(); |
| 286 | 298 |
| 287 if (encoded_frame_writer_) { | 299 if (encoded_frame_writer_) { |
| 288 RTC_CHECK(encoded_frame_writer_->WriteFrame(encoded_image, codec)); | 300 RTC_CHECK(encoded_frame_writer_->WriteFrame(encoded_image, codec)); |
| 289 } | 301 } |
| 290 | 302 |
| 291 // Timestamp is proportional to frame number, so this gives us number of | 303 // Timestamp is proportional to frame number, so this gives us number of |
| 292 // dropped frames. | 304 // dropped frames. |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 404 if (decoded_frame_writer_) { | 416 if (decoded_frame_writer_) { |
| 405 RTC_DCHECK_EQ(last_decoded_frame_buffer_.size(), | 417 RTC_DCHECK_EQ(last_decoded_frame_buffer_.size(), |
| 406 decoded_frame_writer_->FrameLength()); | 418 decoded_frame_writer_->FrameLength()); |
| 407 RTC_CHECK( | 419 RTC_CHECK( |
| 408 decoded_frame_writer_->WriteFrame(last_decoded_frame_buffer_.data())); | 420 decoded_frame_writer_->WriteFrame(last_decoded_frame_buffer_.data())); |
| 409 } | 421 } |
| 410 } | 422 } |
| 411 } | 423 } |
| 412 | 424 |
| 413 void VideoProcessor::FrameDecoded(const VideoFrame& image) { | 425 void VideoProcessor::FrameDecoded(const VideoFrame& image) { |
| 426 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
| 427 |
| 414 // For the highest measurement accuracy of the decode time, the start/stop | 428 // For the highest measurement accuracy of the decode time, the start/stop |
| 415 // time recordings should wrap the Decode call as tightly as possible. | 429 // time recordings should wrap the Decode call as tightly as possible. |
| 416 int64_t decode_stop_ns = rtc::TimeNanos(); | 430 int64_t decode_stop_ns = rtc::TimeNanos(); |
| 417 | 431 |
| 418 // Update frame information and statistics. | 432 // Update frame information and statistics. |
| 419 int frame_number = TimestampToFrameNumber(image.timestamp()); | 433 int frame_number = TimestampToFrameNumber(image.timestamp()); |
| 420 RTC_DCHECK_LT(frame_number, frame_infos_.size()); | 434 RTC_DCHECK_LT(frame_number, frame_infos_.size()); |
| 421 FrameInfo* frame_info = &frame_infos_[frame_number]; | 435 FrameInfo* frame_info = &frame_infos_[frame_number]; |
| 422 frame_info->decoded_width = image.width(); | 436 frame_info->decoded_width = image.width(); |
| 423 frame_info->decoded_height = image.height(); | 437 frame_info->decoded_height = image.height(); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 472 RTC_CHECK(analysis_frame_writer_->WriteFrame(extracted_buffer.data())); | 486 RTC_CHECK(analysis_frame_writer_->WriteFrame(extracted_buffer.data())); |
| 473 if (decoded_frame_writer_) { | 487 if (decoded_frame_writer_) { |
| 474 RTC_DCHECK_EQ(extracted_length, decoded_frame_writer_->FrameLength()); | 488 RTC_DCHECK_EQ(extracted_length, decoded_frame_writer_->FrameLength()); |
| 475 RTC_CHECK(decoded_frame_writer_->WriteFrame(extracted_buffer.data())); | 489 RTC_CHECK(decoded_frame_writer_->WriteFrame(extracted_buffer.data())); |
| 476 } | 490 } |
| 477 | 491 |
| 478 last_decoded_frame_buffer_ = std::move(extracted_buffer); | 492 last_decoded_frame_buffer_ = std::move(extracted_buffer); |
| 479 } | 493 } |
| 480 | 494 |
| 481 uint32_t VideoProcessor::FrameNumberToTimestamp(int frame_number) const { | 495 uint32_t VideoProcessor::FrameNumberToTimestamp(int frame_number) const { |
| 496 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
| 497 |
| 482 RTC_DCHECK_GE(frame_number, 0); | 498 RTC_DCHECK_GE(frame_number, 0); |
| 483 const int ticks_per_frame = | 499 const int ticks_per_frame = |
| 484 kRtpClockRateHz / config_.codec_settings.maxFramerate; | 500 kRtpClockRateHz / config_.codec_settings.maxFramerate; |
| 485 return (frame_number + 1) * ticks_per_frame; | 501 return (frame_number + 1) * ticks_per_frame; |
| 486 } | 502 } |
| 487 | 503 |
| 488 int VideoProcessor::TimestampToFrameNumber(uint32_t timestamp) const { | 504 int VideoProcessor::TimestampToFrameNumber(uint32_t timestamp) const { |
| 505 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
| 506 |
| 489 RTC_DCHECK_GT(timestamp, 0); | 507 RTC_DCHECK_GT(timestamp, 0); |
| 490 const int ticks_per_frame = | 508 const int ticks_per_frame = |
| 491 kRtpClockRateHz / config_.codec_settings.maxFramerate; | 509 kRtpClockRateHz / config_.codec_settings.maxFramerate; |
| 492 RTC_DCHECK_EQ(timestamp % ticks_per_frame, 0); | 510 RTC_DCHECK_EQ(timestamp % ticks_per_frame, 0); |
| 493 return (timestamp / ticks_per_frame) - 1; | 511 return (timestamp / ticks_per_frame) - 1; |
| 494 } | 512 } |
| 495 | 513 |
| 496 } // namespace test | 514 } // namespace test |
| 497 } // namespace webrtc | 515 } // namespace webrtc |
| OLD | NEW |