2011-10-06 06:44:54 +00:00
|
|
|
/*
|
2012-04-12 06:59:14 +00:00
|
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
2011-10-06 06:44:54 +00:00
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
2011-12-08 07:42:18 +00:00
|
|
|
|
2013-07-16 12:32:05 +00:00
|
|
|
#include "webrtc/modules/video_coding/codecs/test/videoprocessor.h"
|
2011-10-06 06:44:54 +00:00
|
|
|
|
2013-08-05 16:22:53 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
2011-10-06 06:44:54 +00:00
|
|
|
#include <limits>
|
2016-02-29 05:51:59 -08:00
|
|
|
#include <memory>
|
2016-11-16 16:41:30 +01:00
|
|
|
#include <utility>
|
2013-02-13 09:35:12 +00:00
|
|
|
#include <vector>
|
2011-10-06 06:44:54 +00:00
|
|
|
|
2017-01-10 07:44:26 -08:00
|
|
|
#include "webrtc/api/video/i420_buffer.h"
|
2017-02-07 03:54:04 -08:00
|
|
|
#include "webrtc/common_types.h"
|
2017-07-01 16:42:22 +02:00
|
|
|
#include "webrtc/modules/video_coding/codecs/vp8/simulcast_rate_allocator.h"
|
2017-07-06 19:44:34 +02:00
|
|
|
#include "webrtc/modules/video_coding/include/video_codec_initializer.h"
|
2016-11-16 16:41:30 +01:00
|
|
|
#include "webrtc/modules/video_coding/utility/default_video_bitrate_allocator.h"
|
2017-07-06 19:44:34 +02:00
|
|
|
#include "webrtc/rtc_base/checks.h"
|
2017-08-07 03:36:54 -07:00
|
|
|
#include "webrtc/rtc_base/logging.h"
|
2017-07-06 19:44:34 +02:00
|
|
|
#include "webrtc/rtc_base/timeutils.h"
|
2015-10-28 18:17:40 +01:00
|
|
|
#include "webrtc/system_wrappers/include/cpu_info.h"
|
2011-10-06 06:44:54 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
namespace test {
|
|
|
|
|
|
2017-02-22 01:26:59 -08:00
|
|
|
namespace {
|
2017-03-07 01:41:43 -08:00
|
|
|
|
2017-08-07 03:36:54 -07:00
|
|
|
const int kRtpClockRateHz = 90000;
|
2017-03-07 01:41:43 -08:00
|
|
|
|
2017-02-28 07:13:47 -08:00
|
|
|
std::unique_ptr<VideoBitrateAllocator> CreateBitrateAllocator(
|
2017-08-08 08:35:53 -07:00
|
|
|
TestConfig* config) {
|
2017-02-28 07:13:47 -08:00
|
|
|
std::unique_ptr<TemporalLayersFactory> tl_factory;
|
2017-08-08 08:35:53 -07:00
|
|
|
if (config->codec_settings.codecType == VideoCodecType::kVideoCodecVP8) {
|
2017-02-28 07:13:47 -08:00
|
|
|
tl_factory.reset(new TemporalLayersFactory());
|
2017-08-08 08:35:53 -07:00
|
|
|
config->codec_settings.VP8()->tl_factory = tl_factory.get();
|
2017-02-28 07:13:47 -08:00
|
|
|
}
|
|
|
|
|
return std::unique_ptr<VideoBitrateAllocator>(
|
2017-08-08 08:35:53 -07:00
|
|
|
VideoCodecInitializer::CreateBitrateAllocator(config->codec_settings,
|
2017-02-28 07:13:47 -08:00
|
|
|
std::move(tl_factory)));
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-08 08:35:53 -07:00
|
|
|
void PrintCodecSettings(const VideoCodec& codec_settings) {
|
2017-08-07 03:36:54 -07:00
|
|
|
printf(" Codec settings:\n");
|
|
|
|
|
printf(" Codec type : %s\n",
|
2017-08-08 08:35:53 -07:00
|
|
|
CodecTypeToPayloadName(codec_settings.codecType).value_or("Unknown"));
|
|
|
|
|
printf(" Start bitrate : %d kbps\n", codec_settings.startBitrate);
|
|
|
|
|
printf(" Max bitrate : %d kbps\n", codec_settings.maxBitrate);
|
|
|
|
|
printf(" Min bitrate : %d kbps\n", codec_settings.minBitrate);
|
|
|
|
|
printf(" Width : %d\n", codec_settings.width);
|
|
|
|
|
printf(" Height : %d\n", codec_settings.height);
|
|
|
|
|
printf(" Max frame rate : %d\n", codec_settings.maxFramerate);
|
|
|
|
|
printf(" QPmax : %d\n", codec_settings.qpMax);
|
|
|
|
|
if (codec_settings.codecType == kVideoCodecVP8) {
|
|
|
|
|
printf(" Complexity : %d\n", codec_settings.VP8().complexity);
|
|
|
|
|
printf(" Denoising : %d\n", codec_settings.VP8().denoisingOn);
|
2017-08-07 03:36:54 -07:00
|
|
|
printf(" Error concealment : %d\n",
|
2017-08-08 08:35:53 -07:00
|
|
|
codec_settings.VP8().errorConcealmentOn);
|
|
|
|
|
printf(" Frame dropping : %d\n", codec_settings.VP8().frameDroppingOn);
|
|
|
|
|
printf(" Resilience : %d\n", codec_settings.VP8().resilience);
|
|
|
|
|
printf(" Key frame interval: %d\n", codec_settings.VP8().keyFrameInterval);
|
|
|
|
|
} else if (codec_settings.codecType == kVideoCodecVP9) {
|
|
|
|
|
printf(" Complexity : %d\n", codec_settings.VP9().complexity);
|
|
|
|
|
printf(" Denoising : %d\n", codec_settings.VP9().denoisingOn);
|
|
|
|
|
printf(" Frame dropping : %d\n", codec_settings.VP9().frameDroppingOn);
|
|
|
|
|
printf(" Resilience : %d\n", codec_settings.VP9().resilienceOn);
|
|
|
|
|
printf(" Key frame interval: %d\n", codec_settings.VP9().keyFrameInterval);
|
|
|
|
|
printf(" Adaptive QP mode : %d\n", codec_settings.VP9().adaptiveQpMode);
|
|
|
|
|
} else if (codec_settings.codecType == kVideoCodecH264) {
|
|
|
|
|
printf(" Frame dropping : %d\n", codec_settings.H264().frameDroppingOn);
|
2017-08-07 03:36:54 -07:00
|
|
|
printf(" Key frame interval: %d\n",
|
2017-08-08 08:35:53 -07:00
|
|
|
codec_settings.H264().keyFrameInterval);
|
|
|
|
|
printf(" Profile : %d\n", codec_settings.H264().profile);
|
2017-03-07 00:25:38 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int GetElapsedTimeMicroseconds(int64_t start_ns, int64_t stop_ns) {
|
|
|
|
|
int64_t diff_us = (stop_ns - start_ns) / rtc::kNumNanosecsPerMicrosec;
|
|
|
|
|
RTC_DCHECK_GE(diff_us, std::numeric_limits<int>::min());
|
|
|
|
|
RTC_DCHECK_LE(diff_us, std::numeric_limits<int>::max());
|
|
|
|
|
return static_cast<int>(diff_us);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-22 01:26:59 -08:00
|
|
|
} // namespace
|
|
|
|
|
|
2017-02-15 05:19:51 -08:00
|
|
|
const char* ExcludeFrameTypesToStr(ExcludeFrameTypes e) {
|
|
|
|
|
switch (e) {
|
|
|
|
|
case kExcludeOnlyFirstKeyFrame:
|
|
|
|
|
return "ExcludeOnlyFirstKeyFrame";
|
|
|
|
|
case kExcludeAllKeyFrames:
|
|
|
|
|
return "ExcludeAllKeyFrames";
|
|
|
|
|
default:
|
|
|
|
|
RTC_NOTREACHED();
|
|
|
|
|
return "Unknown";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 08:12:33 -07:00
|
|
|
VideoProcessor::VideoProcessor(webrtc::VideoEncoder* encoder,
|
|
|
|
|
webrtc::VideoDecoder* decoder,
|
|
|
|
|
FrameReader* analysis_frame_reader,
|
|
|
|
|
FrameWriter* analysis_frame_writer,
|
|
|
|
|
PacketManipulator* packet_manipulator,
|
|
|
|
|
const TestConfig& config,
|
|
|
|
|
Stats* stats,
|
|
|
|
|
IvfFileWriter* encoded_frame_writer,
|
|
|
|
|
FrameWriter* decoded_frame_writer)
|
2017-08-08 08:35:53 -07:00
|
|
|
: config_(config),
|
|
|
|
|
encoder_(encoder),
|
2011-10-06 06:44:54 +00:00
|
|
|
decoder_(decoder),
|
2017-08-08 08:35:53 -07:00
|
|
|
bitrate_allocator_(CreateBitrateAllocator(&config_)),
|
2017-02-28 07:13:47 -08:00
|
|
|
encode_callback_(new VideoProcessorEncodeCompleteCallback(this)),
|
|
|
|
|
decode_callback_(new VideoProcessorDecodeCompleteCallback(this)),
|
2011-10-06 06:44:54 +00:00
|
|
|
packet_manipulator_(packet_manipulator),
|
2017-02-28 07:13:47 -08:00
|
|
|
analysis_frame_reader_(analysis_frame_reader),
|
|
|
|
|
analysis_frame_writer_(analysis_frame_writer),
|
2017-02-22 01:26:59 -08:00
|
|
|
encoded_frame_writer_(encoded_frame_writer),
|
|
|
|
|
decoded_frame_writer_(decoded_frame_writer),
|
2012-06-05 21:07:28 +00:00
|
|
|
initialized_(false),
|
2017-03-07 01:41:43 -08:00
|
|
|
last_encoded_frame_num_(-1),
|
|
|
|
|
last_decoded_frame_num_(-1),
|
|
|
|
|
first_key_frame_has_been_excluded_(false),
|
|
|
|
|
last_decoded_frame_buffer_(0, analysis_frame_reader->FrameLength()),
|
2017-02-28 07:13:47 -08:00
|
|
|
stats_(stats),
|
|
|
|
|
num_dropped_frames_(0),
|
2017-05-19 04:07:38 -07:00
|
|
|
num_spatial_resizes_(0) {
|
2016-11-16 16:41:30 +01:00
|
|
|
RTC_DCHECK(encoder);
|
|
|
|
|
RTC_DCHECK(decoder);
|
2017-02-28 07:13:47 -08:00
|
|
|
RTC_DCHECK(packet_manipulator);
|
2017-02-22 01:26:59 -08:00
|
|
|
RTC_DCHECK(analysis_frame_reader);
|
|
|
|
|
RTC_DCHECK(analysis_frame_writer);
|
2016-11-16 16:41:30 +01:00
|
|
|
RTC_DCHECK(stats);
|
2017-06-22 02:18:50 -07:00
|
|
|
frame_infos_.reserve(analysis_frame_reader->NumberOfFrames());
|
2011-10-06 06:44:54 +00:00
|
|
|
}
|
|
|
|
|
|
2017-08-07 08:12:33 -07:00
|
|
|
VideoProcessor::~VideoProcessor() {
|
2017-08-07 03:36:54 -07:00
|
|
|
encoder_->RegisterEncodeCompleteCallback(nullptr);
|
|
|
|
|
decoder_->RegisterDecodeCompleteCallback(nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 08:12:33 -07:00
|
|
|
void VideoProcessor::Init() {
|
2017-06-22 02:18:50 -07:00
|
|
|
RTC_DCHECK(!initialized_) << "VideoProcessor already initialized.";
|
|
|
|
|
initialized_ = true;
|
2017-03-07 01:41:43 -08:00
|
|
|
|
2017-08-07 03:36:54 -07:00
|
|
|
// Setup required callbacks for the encoder and decoder.
|
2017-02-10 00:16:07 -08:00
|
|
|
RTC_CHECK_EQ(encoder_->RegisterEncodeCompleteCallback(encode_callback_.get()),
|
|
|
|
|
WEBRTC_VIDEO_CODEC_OK)
|
|
|
|
|
<< "Failed to register encode complete callback";
|
|
|
|
|
RTC_CHECK_EQ(decoder_->RegisterDecodeCompleteCallback(decode_callback_.get()),
|
|
|
|
|
WEBRTC_VIDEO_CODEC_OK)
|
|
|
|
|
<< "Failed to register decode complete callback";
|
|
|
|
|
|
2017-02-28 07:13:47 -08:00
|
|
|
// Initialize the encoder and decoder.
|
2017-02-21 08:30:04 -08:00
|
|
|
uint32_t num_cores =
|
|
|
|
|
config_.use_single_core ? 1 : CpuInfo::DetectNumberOfCores();
|
2017-02-10 00:16:07 -08:00
|
|
|
RTC_CHECK_EQ(
|
2017-08-08 08:35:53 -07:00
|
|
|
encoder_->InitEncode(&config_.codec_settings, num_cores,
|
2017-02-10 00:16:07 -08:00
|
|
|
config_.networking_config.max_payload_size_in_bytes),
|
|
|
|
|
WEBRTC_VIDEO_CODEC_OK)
|
|
|
|
|
<< "Failed to initialize VideoEncoder";
|
|
|
|
|
|
2017-08-08 08:35:53 -07:00
|
|
|
RTC_CHECK_EQ(decoder_->InitDecode(&config_.codec_settings, num_cores),
|
2017-02-10 00:16:07 -08:00
|
|
|
WEBRTC_VIDEO_CODEC_OK)
|
|
|
|
|
<< "Failed to initialize VideoDecoder";
|
2011-10-06 06:44:54 +00:00
|
|
|
|
2011-12-08 07:42:18 +00:00
|
|
|
if (config_.verbose) {
|
|
|
|
|
printf("Video Processor:\n");
|
2017-08-07 03:36:54 -07:00
|
|
|
printf(" Filename : %s\n", config_.filename.c_str());
|
|
|
|
|
printf(" Total # of frames: %d\n",
|
2017-06-22 02:18:50 -07:00
|
|
|
analysis_frame_reader_->NumberOfFrames());
|
2017-08-07 03:36:54 -07:00
|
|
|
printf(" # CPU cores used : %d\n", num_cores);
|
|
|
|
|
const char* encoder_name = encoder_->ImplementationName();
|
|
|
|
|
printf(" Encoder implementation name: %s\n", encoder_name);
|
|
|
|
|
const char* decoder_name = decoder_->ImplementationName();
|
|
|
|
|
printf(" Decoder implementation name: %s\n", decoder_name);
|
|
|
|
|
if (strcmp(encoder_name, decoder_name) == 0) {
|
|
|
|
|
printf(" Codec implementation name : %s_%s\n",
|
2017-08-08 08:35:53 -07:00
|
|
|
CodecTypeToPayloadName(config_.codec_settings.codecType)
|
2017-05-19 04:07:38 -07:00
|
|
|
.value_or("Unknown"),
|
|
|
|
|
encoder_->ImplementationName());
|
|
|
|
|
}
|
2017-03-07 00:25:38 -08:00
|
|
|
PrintCodecSettings(config_.codec_settings);
|
2017-08-07 03:36:54 -07:00
|
|
|
printf("\n");
|
2011-12-08 07:42:18 +00:00
|
|
|
}
|
2011-10-06 06:44:54 +00:00
|
|
|
}
|
|
|
|
|
|
2017-08-07 08:12:33 -07:00
|
|
|
bool VideoProcessor::ProcessFrame(int frame_number) {
|
2017-03-07 01:41:43 -08:00
|
|
|
RTC_DCHECK_GE(frame_number, 0);
|
|
|
|
|
RTC_DCHECK_LE(frame_number, frame_infos_.size())
|
|
|
|
|
<< "Must process frames without gaps.";
|
2017-06-22 02:18:50 -07:00
|
|
|
RTC_DCHECK(initialized_) << "VideoProcessor not initialized.";
|
2017-02-10 00:16:07 -08:00
|
|
|
|
2017-06-01 10:02:26 -07:00
|
|
|
rtc::scoped_refptr<I420BufferInterface> buffer(
|
2017-02-22 01:26:59 -08:00
|
|
|
analysis_frame_reader_->ReadFrame());
|
|
|
|
|
|
2017-03-07 01:41:43 -08:00
|
|
|
if (!buffer) {
|
|
|
|
|
// Last frame has been reached.
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2012-06-05 21:07:28 +00:00
|
|
|
|
2017-03-07 01:41:43 -08:00
|
|
|
uint32_t timestamp = FrameNumberToTimestamp(frame_number);
|
|
|
|
|
VideoFrame source_frame(buffer, timestamp, 0, webrtc::kVideoRotation_0);
|
2012-06-05 21:07:28 +00:00
|
|
|
|
2017-03-07 01:41:43 -08:00
|
|
|
// Store frame information during the different stages of encode and decode.
|
|
|
|
|
frame_infos_.emplace_back();
|
|
|
|
|
FrameInfo* frame_info = &frame_infos_.back();
|
|
|
|
|
frame_info->timestamp = timestamp;
|
2017-02-15 05:19:51 -08:00
|
|
|
|
2017-03-07 01:41:43 -08:00
|
|
|
// Decide if we are going to force a keyframe.
|
|
|
|
|
std::vector<FrameType> frame_types(1, kVideoFrameDelta);
|
|
|
|
|
if (config_.keyframe_interval > 0 &&
|
|
|
|
|
frame_number % config_.keyframe_interval == 0) {
|
|
|
|
|
frame_types[0] = kVideoFrameKey;
|
2011-10-06 06:44:54 +00:00
|
|
|
}
|
2017-03-07 01:41:43 -08:00
|
|
|
|
|
|
|
|
// Create frame statistics object used for aggregation at end of test run.
|
|
|
|
|
FrameStatistic* frame_stat = &stats_->NewFrame(frame_number);
|
|
|
|
|
|
|
|
|
|
// For the highest measurement accuracy of the encode time, the start/stop
|
|
|
|
|
// time recordings should wrap the Encode call as tightly as possible.
|
|
|
|
|
frame_info->encode_start_ns = rtc::TimeNanos();
|
|
|
|
|
frame_stat->encode_return_code =
|
|
|
|
|
encoder_->Encode(source_frame, nullptr, &frame_types);
|
|
|
|
|
|
|
|
|
|
if (frame_stat->encode_return_code != WEBRTC_VIDEO_CODEC_OK) {
|
2017-08-07 03:36:54 -07:00
|
|
|
LOG(LS_WARNING) << "Failed to encode frame " << frame_number
|
|
|
|
|
<< ", return code: " << frame_stat->encode_return_code
|
|
|
|
|
<< ".";
|
2017-03-07 01:41:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
2011-10-06 06:44:54 +00:00
|
|
|
}
|
|
|
|
|
|
2017-08-07 08:12:33 -07:00
|
|
|
void VideoProcessor::SetRates(int bit_rate, int frame_rate) {
|
2017-08-08 08:35:53 -07:00
|
|
|
config_.codec_settings.maxFramerate = frame_rate;
|
2017-08-07 03:36:54 -07:00
|
|
|
int set_rates_result = encoder_->SetRateAllocation(
|
|
|
|
|
bitrate_allocator_->GetAllocation(bit_rate * 1000, frame_rate),
|
|
|
|
|
frame_rate);
|
|
|
|
|
RTC_DCHECK_GE(set_rates_result, 0)
|
|
|
|
|
<< "Failed to update encoder with new rate " << bit_rate;
|
|
|
|
|
num_dropped_frames_ = 0;
|
|
|
|
|
num_spatial_resizes_ = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 08:12:33 -07:00
|
|
|
size_t VideoProcessor::EncodedFrameSize(int frame_number) {
|
2017-08-07 03:36:54 -07:00
|
|
|
RTC_DCHECK_LT(frame_number, frame_infos_.size());
|
|
|
|
|
return frame_infos_[frame_number].encoded_frame_size;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 08:12:33 -07:00
|
|
|
FrameType VideoProcessor::EncodedFrameType(int frame_number) {
|
2017-08-07 03:36:54 -07:00
|
|
|
RTC_DCHECK_LT(frame_number, frame_infos_.size());
|
|
|
|
|
return frame_infos_[frame_number].encoded_frame_type;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 08:12:33 -07:00
|
|
|
int VideoProcessor::GetQpFromEncoder(int frame_number) {
|
2017-08-07 03:36:54 -07:00
|
|
|
RTC_DCHECK_LT(frame_number, frame_infos_.size());
|
|
|
|
|
return frame_infos_[frame_number].qp_encoder;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 08:12:33 -07:00
|
|
|
int VideoProcessor::GetQpFromBitstream(int frame_number) {
|
2017-08-07 03:36:54 -07:00
|
|
|
RTC_DCHECK_LT(frame_number, frame_infos_.size());
|
|
|
|
|
return frame_infos_[frame_number].qp_bitstream;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 08:12:33 -07:00
|
|
|
int VideoProcessor::NumberDroppedFrames() {
|
2017-08-07 03:36:54 -07:00
|
|
|
return num_dropped_frames_;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 08:12:33 -07:00
|
|
|
int VideoProcessor::NumberSpatialResizes() {
|
2017-08-07 03:36:54 -07:00
|
|
|
return num_spatial_resizes_;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 08:12:33 -07:00
|
|
|
void VideoProcessor::FrameEncoded(
|
2016-01-27 01:36:03 -08:00
|
|
|
webrtc::VideoCodecType codec,
|
|
|
|
|
const EncodedImage& encoded_image,
|
|
|
|
|
const webrtc::RTPFragmentationHeader* fragmentation) {
|
2017-02-15 05:29:38 -08:00
|
|
|
// For the highest measurement accuracy of the encode time, the start/stop
|
|
|
|
|
// time recordings should wrap the Encode call as tightly as possible.
|
|
|
|
|
int64_t encode_stop_ns = rtc::TimeNanos();
|
|
|
|
|
|
2017-02-22 01:26:59 -08:00
|
|
|
if (encoded_frame_writer_) {
|
|
|
|
|
RTC_CHECK(encoded_frame_writer_->WriteFrame(encoded_image, codec));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Timestamp is proportional to frame number, so this gives us number of
|
|
|
|
|
// dropped frames.
|
2017-03-07 01:41:43 -08:00
|
|
|
int frame_number = TimestampToFrameNumber(encoded_image._timeStamp);
|
|
|
|
|
bool last_frame_missing = false;
|
|
|
|
|
if (frame_number > 0) {
|
|
|
|
|
RTC_DCHECK_GE(last_encoded_frame_num_, 0);
|
|
|
|
|
int num_dropped_from_last_encode =
|
|
|
|
|
frame_number - last_encoded_frame_num_ - 1;
|
|
|
|
|
RTC_DCHECK_GE(num_dropped_from_last_encode, 0);
|
|
|
|
|
num_dropped_frames_ += num_dropped_from_last_encode;
|
|
|
|
|
if (num_dropped_from_last_encode > 0) {
|
|
|
|
|
// For dropped frames, we write out the last decoded frame to avoid
|
|
|
|
|
// getting out of sync for the computation of PSNR and SSIM.
|
|
|
|
|
for (int i = 0; i < num_dropped_from_last_encode; i++) {
|
|
|
|
|
RTC_DCHECK_EQ(last_decoded_frame_buffer_.size(),
|
|
|
|
|
analysis_frame_writer_->FrameLength());
|
|
|
|
|
RTC_CHECK(analysis_frame_writer_->WriteFrame(
|
|
|
|
|
last_decoded_frame_buffer_.data()));
|
|
|
|
|
if (decoded_frame_writer_) {
|
|
|
|
|
RTC_DCHECK_EQ(last_decoded_frame_buffer_.size(),
|
|
|
|
|
decoded_frame_writer_->FrameLength());
|
|
|
|
|
RTC_CHECK(decoded_frame_writer_->WriteFrame(
|
|
|
|
|
last_decoded_frame_buffer_.data()));
|
|
|
|
|
}
|
2017-02-22 01:26:59 -08:00
|
|
|
}
|
2012-06-05 21:07:28 +00:00
|
|
|
}
|
2017-02-22 01:26:59 -08:00
|
|
|
|
2017-03-07 01:41:43 -08:00
|
|
|
last_frame_missing =
|
|
|
|
|
(frame_infos_[last_encoded_frame_num_].manipulated_length == 0);
|
|
|
|
|
}
|
|
|
|
|
// Ensure strict monotonicity.
|
|
|
|
|
RTC_CHECK_GT(frame_number, last_encoded_frame_num_);
|
|
|
|
|
last_encoded_frame_num_ = frame_number;
|
|
|
|
|
|
|
|
|
|
// Frame is not dropped, so update frame information and statistics.
|
|
|
|
|
RTC_DCHECK_LT(frame_number, frame_infos_.size());
|
|
|
|
|
FrameInfo* frame_info = &frame_infos_[frame_number];
|
|
|
|
|
frame_info->encoded_frame_size = encoded_image._length;
|
|
|
|
|
frame_info->encoded_frame_type = encoded_image._frameType;
|
2017-05-30 10:57:25 -07:00
|
|
|
frame_info->qp_encoder = encoded_image.qp_;
|
|
|
|
|
if (codec == kVideoCodecVP8) {
|
|
|
|
|
vp8::GetQp(encoded_image._buffer, encoded_image._length,
|
|
|
|
|
&frame_info->qp_bitstream);
|
|
|
|
|
} else if (codec == kVideoCodecVP9) {
|
|
|
|
|
vp9::GetQp(encoded_image._buffer, encoded_image._length,
|
|
|
|
|
&frame_info->qp_bitstream);
|
|
|
|
|
}
|
2017-03-07 01:41:43 -08:00
|
|
|
FrameStatistic* frame_stat = &stats_->stats_[frame_number];
|
|
|
|
|
frame_stat->encode_time_in_us =
|
|
|
|
|
GetElapsedTimeMicroseconds(frame_info->encode_start_ns, encode_stop_ns);
|
|
|
|
|
frame_stat->encoding_successful = true;
|
|
|
|
|
frame_stat->encoded_frame_length_in_bytes = encoded_image._length;
|
|
|
|
|
frame_stat->frame_number = frame_number;
|
|
|
|
|
frame_stat->frame_type = encoded_image._frameType;
|
|
|
|
|
frame_stat->qp = encoded_image.qp_;
|
2017-06-22 02:18:50 -07:00
|
|
|
frame_stat->bit_rate_in_kbps = static_cast<int>(
|
2017-08-08 08:35:53 -07:00
|
|
|
encoded_image._length * config_.codec_settings.maxFramerate * 8 / 1000);
|
2017-03-07 01:41:43 -08:00
|
|
|
frame_stat->total_packets =
|
2015-12-21 03:04:49 -08:00
|
|
|
encoded_image._length / config_.networking_config.packet_size_in_bytes +
|
|
|
|
|
1;
|
2011-10-06 06:44:54 +00:00
|
|
|
|
2017-02-15 05:19:51 -08:00
|
|
|
// Simulate packet loss.
|
2011-10-06 06:44:54 +00:00
|
|
|
bool exclude_this_frame = false;
|
2015-10-23 15:58:18 +02:00
|
|
|
if (encoded_image._frameType == kVideoFrameKey) {
|
2017-02-28 07:13:47 -08:00
|
|
|
// Only keyframes can be excluded.
|
2011-10-06 06:44:54 +00:00
|
|
|
switch (config_.exclude_frame_types) {
|
|
|
|
|
case kExcludeOnlyFirstKeyFrame:
|
|
|
|
|
if (!first_key_frame_has_been_excluded_) {
|
|
|
|
|
first_key_frame_has_been_excluded_ = true;
|
|
|
|
|
exclude_this_frame = true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case kExcludeAllKeyFrames:
|
|
|
|
|
exclude_this_frame = true;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2016-11-16 16:41:30 +01:00
|
|
|
RTC_NOTREACHED();
|
2011-10-06 06:44:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
2016-01-27 01:36:03 -08:00
|
|
|
|
|
|
|
|
// Make a raw copy of the |encoded_image| buffer.
|
2016-01-22 00:07:12 -08:00
|
|
|
size_t copied_buffer_size = encoded_image._length +
|
|
|
|
|
EncodedImage::GetBufferPaddingBytes(codec);
|
2016-02-29 05:51:59 -08:00
|
|
|
std::unique_ptr<uint8_t[]> copied_buffer(new uint8_t[copied_buffer_size]);
|
2014-12-01 15:23:21 +00:00
|
|
|
memcpy(copied_buffer.get(), encoded_image._buffer, encoded_image._length);
|
2016-01-27 01:36:03 -08:00
|
|
|
// The image to feed to the decoder.
|
2014-12-01 15:23:21 +00:00
|
|
|
EncodedImage copied_image;
|
|
|
|
|
memcpy(&copied_image, &encoded_image, sizeof(copied_image));
|
2016-01-22 00:07:12 -08:00
|
|
|
copied_image._size = copied_buffer_size;
|
2014-12-01 15:23:21 +00:00
|
|
|
copied_image._buffer = copied_buffer.get();
|
2016-01-27 01:36:03 -08:00
|
|
|
|
2011-10-06 06:44:54 +00:00
|
|
|
if (!exclude_this_frame) {
|
2017-03-07 01:41:43 -08:00
|
|
|
frame_stat->packets_dropped =
|
2015-12-21 03:04:49 -08:00
|
|
|
packet_manipulator_->ManipulatePackets(&copied_image);
|
2011-10-06 06:44:54 +00:00
|
|
|
}
|
2017-03-07 01:41:43 -08:00
|
|
|
frame_info->manipulated_length = copied_image._length;
|
2011-10-06 06:44:54 +00:00
|
|
|
|
|
|
|
|
// Keep track of if frames are lost due to packet loss so we can tell
|
2017-02-15 05:19:51 -08:00
|
|
|
// this to the encoder (this is handled by the RTP logic in the full stack).
|
2011-10-06 06:44:54 +00:00
|
|
|
// TODO(kjellander): Pass fragmentation header to the decoder when
|
|
|
|
|
// CL 172001 has been submitted and PacketManipulator supports this.
|
2017-02-15 05:29:38 -08:00
|
|
|
|
|
|
|
|
// For the highest measurement accuracy of the decode time, the start/stop
|
|
|
|
|
// time recordings should wrap the Decode call as tightly as possible.
|
2017-03-07 01:41:43 -08:00
|
|
|
frame_info->decode_start_ns = rtc::TimeNanos();
|
|
|
|
|
frame_stat->decode_return_code =
|
|
|
|
|
decoder_->Decode(copied_image, last_frame_missing, nullptr);
|
2017-02-15 05:19:51 -08:00
|
|
|
|
2017-03-07 01:41:43 -08:00
|
|
|
if (frame_stat->decode_return_code != WEBRTC_VIDEO_CODEC_OK) {
|
2011-10-06 06:44:54 +00:00
|
|
|
// Write the last successful frame the output file to avoid getting it out
|
2017-02-15 05:19:51 -08:00
|
|
|
// of sync with the source file for SSIM and PSNR comparisons.
|
2017-03-07 01:41:43 -08:00
|
|
|
RTC_DCHECK_EQ(last_decoded_frame_buffer_.size(),
|
|
|
|
|
analysis_frame_writer_->FrameLength());
|
|
|
|
|
RTC_CHECK(
|
|
|
|
|
analysis_frame_writer_->WriteFrame(last_decoded_frame_buffer_.data()));
|
2017-02-22 01:26:59 -08:00
|
|
|
if (decoded_frame_writer_) {
|
2017-03-07 01:41:43 -08:00
|
|
|
RTC_DCHECK_EQ(last_decoded_frame_buffer_.size(),
|
|
|
|
|
decoded_frame_writer_->FrameLength());
|
|
|
|
|
RTC_CHECK(
|
|
|
|
|
decoded_frame_writer_->WriteFrame(last_decoded_frame_buffer_.data()));
|
2017-02-22 01:26:59 -08:00
|
|
|
}
|
2011-10-06 06:44:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 08:12:33 -07:00
|
|
|
void VideoProcessor::FrameDecoded(const VideoFrame& image) {
|
2017-02-15 05:29:38 -08:00
|
|
|
// For the highest measurement accuracy of the decode time, the start/stop
|
|
|
|
|
// time recordings should wrap the Decode call as tightly as possible.
|
2016-05-10 16:31:47 +02:00
|
|
|
int64_t decode_stop_ns = rtc::TimeNanos();
|
2017-02-15 05:19:51 -08:00
|
|
|
|
2017-03-07 01:41:43 -08:00
|
|
|
// Update frame information and statistics.
|
|
|
|
|
int frame_number = TimestampToFrameNumber(image.timestamp());
|
|
|
|
|
RTC_DCHECK_LT(frame_number, frame_infos_.size());
|
|
|
|
|
FrameInfo* frame_info = &frame_infos_[frame_number];
|
|
|
|
|
frame_info->decoded_width = image.width();
|
|
|
|
|
frame_info->decoded_height = image.height();
|
|
|
|
|
FrameStatistic* frame_stat = &stats_->stats_[frame_number];
|
|
|
|
|
frame_stat->decode_time_in_us =
|
|
|
|
|
GetElapsedTimeMicroseconds(frame_info->decode_start_ns, decode_stop_ns);
|
|
|
|
|
frame_stat->decoding_successful = true;
|
|
|
|
|
|
|
|
|
|
// Check if the codecs have resized the frame since previously decoded frame.
|
|
|
|
|
if (frame_number > 0) {
|
|
|
|
|
RTC_DCHECK_GE(last_decoded_frame_num_, 0);
|
|
|
|
|
const FrameInfo& last_decoded_frame_info =
|
|
|
|
|
frame_infos_[last_decoded_frame_num_];
|
|
|
|
|
if (static_cast<int>(image.width()) !=
|
|
|
|
|
last_decoded_frame_info.decoded_width ||
|
|
|
|
|
static_cast<int>(image.height()) !=
|
|
|
|
|
last_decoded_frame_info.decoded_height) {
|
|
|
|
|
++num_spatial_resizes_;
|
|
|
|
|
}
|
2012-06-05 21:07:28 +00:00
|
|
|
}
|
2017-03-07 01:41:43 -08:00
|
|
|
// Ensure strict monotonicity.
|
|
|
|
|
RTC_CHECK_GT(frame_number, last_decoded_frame_num_);
|
|
|
|
|
last_decoded_frame_num_ = frame_number;
|
|
|
|
|
|
|
|
|
|
// Check if codec size is different from the original size, and if so,
|
|
|
|
|
// scale back to original size. This is needed for the PSNR and SSIM
|
2017-02-15 05:19:51 -08:00
|
|
|
// calculations.
|
2017-03-07 01:41:43 -08:00
|
|
|
size_t extracted_length;
|
|
|
|
|
rtc::Buffer extracted_buffer;
|
2017-08-08 08:35:53 -07:00
|
|
|
if (image.width() != config_.codec_settings.width ||
|
|
|
|
|
image.height() != config_.codec_settings.height) {
|
2017-03-07 01:41:43 -08:00
|
|
|
rtc::scoped_refptr<I420Buffer> scaled_buffer(I420Buffer::Create(
|
2017-08-08 08:35:53 -07:00
|
|
|
config_.codec_settings.width, config_.codec_settings.height));
|
2016-06-13 13:06:01 +02:00
|
|
|
// Should be the same aspect ratio, no cropping needed.
|
2017-06-01 10:02:26 -07:00
|
|
|
scaled_buffer->ScaleFrom(*image.video_frame_buffer()->ToI420());
|
2016-06-13 13:06:01 +02:00
|
|
|
|
2017-04-28 07:18:05 -07:00
|
|
|
size_t length = CalcBufferSize(VideoType::kI420, scaled_buffer->width(),
|
|
|
|
|
scaled_buffer->height());
|
2017-03-07 01:41:43 -08:00
|
|
|
extracted_buffer.SetSize(length);
|
|
|
|
|
extracted_length =
|
|
|
|
|
ExtractBuffer(scaled_buffer, length, extracted_buffer.data());
|
|
|
|
|
} else {
|
|
|
|
|
// No resize.
|
2017-04-28 07:18:05 -07:00
|
|
|
size_t length =
|
|
|
|
|
CalcBufferSize(VideoType::kI420, image.width(), image.height());
|
2017-03-07 01:41:43 -08:00
|
|
|
extracted_buffer.SetSize(length);
|
2017-06-01 10:02:26 -07:00
|
|
|
extracted_length = ExtractBuffer(image.video_frame_buffer()->ToI420(),
|
|
|
|
|
length, extracted_buffer.data());
|
2017-03-07 01:41:43 -08:00
|
|
|
}
|
2012-10-24 18:33:04 +00:00
|
|
|
|
2017-03-07 01:41:43 -08:00
|
|
|
RTC_DCHECK_EQ(extracted_length, analysis_frame_writer_->FrameLength());
|
|
|
|
|
RTC_CHECK(analysis_frame_writer_->WriteFrame(extracted_buffer.data()));
|
|
|
|
|
if (decoded_frame_writer_) {
|
|
|
|
|
RTC_DCHECK_EQ(extracted_length, decoded_frame_writer_->FrameLength());
|
|
|
|
|
RTC_CHECK(decoded_frame_writer_->WriteFrame(extracted_buffer.data()));
|
2011-10-06 06:44:54 +00:00
|
|
|
}
|
2017-03-07 01:41:43 -08:00
|
|
|
|
|
|
|
|
last_decoded_frame_buffer_ = std::move(extracted_buffer);
|
2011-10-06 06:44:54 +00:00
|
|
|
}
|
|
|
|
|
|
2017-08-07 08:12:33 -07:00
|
|
|
uint32_t VideoProcessor::FrameNumberToTimestamp(int frame_number) {
|
2017-08-07 03:36:54 -07:00
|
|
|
RTC_DCHECK_GE(frame_number, 0);
|
|
|
|
|
const int ticks_per_frame =
|
2017-08-08 08:35:53 -07:00
|
|
|
kRtpClockRateHz / config_.codec_settings.maxFramerate;
|
2017-08-07 03:36:54 -07:00
|
|
|
return (frame_number + 1) * ticks_per_frame;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 08:12:33 -07:00
|
|
|
int VideoProcessor::TimestampToFrameNumber(uint32_t timestamp) {
|
2017-08-07 03:36:54 -07:00
|
|
|
RTC_DCHECK_GT(timestamp, 0);
|
|
|
|
|
const int ticks_per_frame =
|
2017-08-08 08:35:53 -07:00
|
|
|
kRtpClockRateHz / config_.codec_settings.maxFramerate;
|
2017-08-07 03:36:54 -07:00
|
|
|
RTC_DCHECK_EQ(timestamp % ticks_per_frame, 0);
|
|
|
|
|
return (timestamp / ticks_per_frame) - 1;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-06 06:44:54 +00:00
|
|
|
} // namespace test
|
|
|
|
|
} // namespace webrtc
|