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"
|
2016-11-16 16:41:30 +01:00
|
|
|
#include "webrtc/base/checks.h"
|
2016-05-10 16:31:47 +02:00
|
|
|
#include "webrtc/base/timeutils.h"
|
2017-02-07 03:54:04 -08:00
|
|
|
#include "webrtc/common_types.h"
|
2016-11-16 16:41:30 +01:00
|
|
|
#include "webrtc/modules/video_coding/include/video_codec_initializer.h"
|
|
|
|
|
#include "webrtc/modules/video_coding/utility/default_video_bitrate_allocator.h"
|
|
|
|
|
#include "webrtc/modules/video_coding/utility/simulcast_rate_allocator.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 {
|
|
|
|
|
const int k90khzTimestampFrameDiff = 3000; // Assuming 30 fps.
|
2017-02-28 07:13:47 -08:00
|
|
|
|
|
|
|
|
std::unique_ptr<VideoBitrateAllocator> CreateBitrateAllocator(
|
|
|
|
|
const TestConfig& config) {
|
|
|
|
|
std::unique_ptr<TemporalLayersFactory> tl_factory;
|
|
|
|
|
if (config.codec_settings->codecType == VideoCodecType::kVideoCodecVP8) {
|
|
|
|
|
tl_factory.reset(new TemporalLayersFactory());
|
|
|
|
|
config.codec_settings->VP8()->tl_factory = tl_factory.get();
|
|
|
|
|
}
|
|
|
|
|
return std::unique_ptr<VideoBitrateAllocator>(
|
|
|
|
|
VideoCodecInitializer::CreateBitrateAllocator(*config.codec_settings,
|
|
|
|
|
std::move(tl_factory)));
|
|
|
|
|
}
|
|
|
|
|
|
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";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-30 15:18:31 +00:00
|
|
|
TestConfig::TestConfig()
|
|
|
|
|
: name(""),
|
|
|
|
|
description(""),
|
|
|
|
|
test_number(0),
|
|
|
|
|
input_filename(""),
|
|
|
|
|
output_filename(""),
|
|
|
|
|
output_dir("out"),
|
|
|
|
|
networking_config(),
|
|
|
|
|
exclude_frame_types(kExcludeOnlyFirstKeyFrame),
|
Use size_t more consistently for packet/payload lengths.
See design doc at https://docs.google.com/a/chromium.org/document/d/1I6nmE9D_BmCY-IoV6MDPY2V6WYpEI-dg2apWXTfZyUI/edit?usp=sharing for more information.
This CL was reviewed and approved in pieces in the following CLs:
https://webrtc-codereview.appspot.com/24209004/
https://webrtc-codereview.appspot.com/24229004/
https://webrtc-codereview.appspot.com/24259004/
https://webrtc-codereview.appspot.com/25109004/
https://webrtc-codereview.appspot.com/26099004/
https://webrtc-codereview.appspot.com/27069004/
https://webrtc-codereview.appspot.com/27969004/
https://webrtc-codereview.appspot.com/27989004/
https://webrtc-codereview.appspot.com/29009004/
https://webrtc-codereview.appspot.com/30929004/
https://webrtc-codereview.appspot.com/30939004/
https://webrtc-codereview.appspot.com/31999004/
Committing as TBR to the original reviewers.
BUG=chromium:81439
TEST=none
TBR=pthatcher,henrik.lundin,tina.legrand,stefan,tkchin,glaznev,kjellander,perkj,mflodman,henrika,asapersson,niklas.enbom
Review URL: https://webrtc-codereview.appspot.com/23129004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@7726 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-11-20 22:28:14 +00:00
|
|
|
frame_length_in_bytes(0),
|
2013-07-30 15:18:31 +00:00
|
|
|
use_single_core(false),
|
|
|
|
|
keyframe_interval(0),
|
2016-11-16 16:41:30 +01:00
|
|
|
codec_settings(nullptr),
|
2013-07-30 15:18:31 +00:00
|
|
|
verbose(true) {}
|
|
|
|
|
|
|
|
|
|
TestConfig::~TestConfig() {}
|
|
|
|
|
|
2011-10-06 06:44:54 +00:00
|
|
|
VideoProcessorImpl::VideoProcessorImpl(webrtc::VideoEncoder* encoder,
|
|
|
|
|
webrtc::VideoDecoder* decoder,
|
2017-02-22 01:26:59 -08:00
|
|
|
FrameReader* analysis_frame_reader,
|
|
|
|
|
FrameWriter* analysis_frame_writer,
|
2011-10-06 06:44:54 +00:00
|
|
|
PacketManipulator* packet_manipulator,
|
|
|
|
|
const TestConfig& config,
|
2017-02-22 01:26:59 -08:00
|
|
|
Stats* stats,
|
|
|
|
|
FrameWriter* source_frame_writer,
|
|
|
|
|
IvfFileWriter* encoded_frame_writer,
|
|
|
|
|
FrameWriter* decoded_frame_writer)
|
2011-10-06 06:44:54 +00:00
|
|
|
: encoder_(encoder),
|
|
|
|
|
decoder_(decoder),
|
2017-02-28 07:13:47 -08:00
|
|
|
bitrate_allocator_(CreateBitrateAllocator(config)),
|
|
|
|
|
encode_callback_(new VideoProcessorEncodeCompleteCallback(this)),
|
|
|
|
|
decode_callback_(new VideoProcessorDecodeCompleteCallback(this)),
|
2011-10-06 06:44:54 +00:00
|
|
|
packet_manipulator_(packet_manipulator),
|
|
|
|
|
config_(config),
|
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
|
|
|
source_frame_writer_(source_frame_writer),
|
|
|
|
|
encoded_frame_writer_(encoded_frame_writer),
|
|
|
|
|
decoded_frame_writer_(decoded_frame_writer),
|
2011-10-06 06:44:54 +00:00
|
|
|
first_key_frame_has_been_excluded_(false),
|
|
|
|
|
last_frame_missing_(false),
|
2012-06-05 21:07:28 +00:00
|
|
|
initialized_(false),
|
|
|
|
|
encoded_frame_size_(0),
|
2015-10-23 15:58:18 +02:00
|
|
|
encoded_frame_type_(kVideoFrameKey),
|
2012-06-05 21:07:28 +00:00
|
|
|
prev_time_stamp_(0),
|
|
|
|
|
last_encoder_frame_width_(0),
|
2016-11-16 16:41:30 +01:00
|
|
|
last_encoder_frame_height_(0),
|
2017-02-28 07:13:47 -08:00
|
|
|
stats_(stats),
|
|
|
|
|
num_dropped_frames_(0),
|
|
|
|
|
num_spatial_resizes_(0),
|
2016-11-16 16:41:30 +01:00
|
|
|
bit_rate_factor_(0.0),
|
|
|
|
|
encode_start_ns_(0),
|
|
|
|
|
decode_start_ns_(0) {
|
|
|
|
|
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);
|
2011-10-06 06:44:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool VideoProcessorImpl::Init() {
|
2017-02-15 05:19:51 -08:00
|
|
|
// Calculate a factor used for bit rate calculations.
|
2011-12-30 14:44:07 +00:00
|
|
|
bit_rate_factor_ = config_.codec_settings->maxFramerate * 0.001 * 8; // bits
|
2011-10-06 06:44:54 +00:00
|
|
|
|
2017-02-15 05:19:51 -08:00
|
|
|
// Initialize data structures used by the encoder/decoder APIs.
|
2017-02-22 01:26:59 -08:00
|
|
|
size_t frame_length_in_bytes = analysis_frame_reader_->FrameLength();
|
2017-02-10 00:16:07 -08:00
|
|
|
last_successful_frame_buffer_.reset(new uint8_t[frame_length_in_bytes]);
|
2017-02-15 05:19:51 -08:00
|
|
|
|
2012-10-24 18:33:04 +00:00
|
|
|
// Set fixed properties common for all frames.
|
2012-06-05 21:07:28 +00:00
|
|
|
// To keep track of spatial resize actions by encoder.
|
|
|
|
|
last_encoder_frame_width_ = config_.codec_settings->width;
|
|
|
|
|
last_encoder_frame_height_ = config_.codec_settings->height;
|
|
|
|
|
|
2017-02-15 05:19:51 -08:00
|
|
|
// Setup required callbacks for the encoder/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-02-15 05:19:51 -08: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-02-15 05:19:51 -08: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-02-15 05:19:51 -08:00
|
|
|
printf(" #CPU cores used : %d\n", num_cores);
|
2017-02-22 01:26:59 -08:00
|
|
|
printf(" Total # of frames: %d\n",
|
|
|
|
|
analysis_frame_reader_->NumberOfFrames());
|
2011-12-08 07:42:18 +00:00
|
|
|
printf(" Codec settings:\n");
|
2017-02-07 03:54:04 -08:00
|
|
|
printf(" Start bitrate : %d kbps\n",
|
2011-12-30 14:44:07 +00:00
|
|
|
config_.codec_settings->startBitrate);
|
2017-02-07 03:54:04 -08:00
|
|
|
printf(" Width : %d\n", config_.codec_settings->width);
|
|
|
|
|
printf(" Height : %d\n", config_.codec_settings->height);
|
|
|
|
|
printf(" Codec type : %s\n",
|
|
|
|
|
CodecTypeToPayloadName(config_.codec_settings->codecType)
|
|
|
|
|
.value_or("Unknown"));
|
|
|
|
|
printf(" Encoder implementation name: %s\n",
|
|
|
|
|
encoder_->ImplementationName());
|
|
|
|
|
printf(" Decoder implementation name: %s\n",
|
|
|
|
|
decoder_->ImplementationName());
|
|
|
|
|
if (config_.codec_settings->codecType == kVideoCodecVP8) {
|
|
|
|
|
printf(" Denoising : %d\n",
|
|
|
|
|
config_.codec_settings->VP8()->denoisingOn);
|
|
|
|
|
printf(" Error concealment: %d\n",
|
|
|
|
|
config_.codec_settings->VP8()->errorConcealmentOn);
|
|
|
|
|
printf(" Frame dropping : %d\n",
|
|
|
|
|
config_.codec_settings->VP8()->frameDroppingOn);
|
|
|
|
|
printf(" Resilience : %d\n",
|
|
|
|
|
config_.codec_settings->VP8()->resilience);
|
|
|
|
|
} else if (config_.codec_settings->codecType == kVideoCodecVP9) {
|
|
|
|
|
printf(" Resilience : %d\n",
|
|
|
|
|
config_.codec_settings->VP9()->resilience);
|
|
|
|
|
}
|
2011-12-08 07:42:18 +00:00
|
|
|
}
|
2011-10-06 06:44:54 +00:00
|
|
|
initialized_ = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VideoProcessorImpl::~VideoProcessorImpl() {
|
2017-02-10 00:16:07 -08:00
|
|
|
encoder_->RegisterEncodeCompleteCallback(nullptr);
|
|
|
|
|
decoder_->RegisterDecodeCompleteCallback(nullptr);
|
2011-10-06 06:44:54 +00:00
|
|
|
}
|
|
|
|
|
|
2012-06-05 21:07:28 +00:00
|
|
|
void VideoProcessorImpl::SetRates(int bit_rate, int frame_rate) {
|
2016-11-16 16:41:30 +01:00
|
|
|
int set_rates_result = encoder_->SetRateAllocation(
|
|
|
|
|
bitrate_allocator_->GetAllocation(bit_rate * 1000, frame_rate),
|
|
|
|
|
frame_rate);
|
2017-02-10 00:16:07 -08:00
|
|
|
RTC_CHECK_GE(set_rates_result, 0) << "Failed to update encoder with new rate "
|
|
|
|
|
<< bit_rate;
|
2012-06-05 21:07:28 +00:00
|
|
|
num_dropped_frames_ = 0;
|
|
|
|
|
num_spatial_resizes_ = 0;
|
|
|
|
|
}
|
|
|
|
|
|
Use size_t more consistently for packet/payload lengths.
See design doc at https://docs.google.com/a/chromium.org/document/d/1I6nmE9D_BmCY-IoV6MDPY2V6WYpEI-dg2apWXTfZyUI/edit?usp=sharing for more information.
This CL was reviewed and approved in pieces in the following CLs:
https://webrtc-codereview.appspot.com/24209004/
https://webrtc-codereview.appspot.com/24229004/
https://webrtc-codereview.appspot.com/24259004/
https://webrtc-codereview.appspot.com/25109004/
https://webrtc-codereview.appspot.com/26099004/
https://webrtc-codereview.appspot.com/27069004/
https://webrtc-codereview.appspot.com/27969004/
https://webrtc-codereview.appspot.com/27989004/
https://webrtc-codereview.appspot.com/29009004/
https://webrtc-codereview.appspot.com/30929004/
https://webrtc-codereview.appspot.com/30939004/
https://webrtc-codereview.appspot.com/31999004/
Committing as TBR to the original reviewers.
BUG=chromium:81439
TEST=none
TBR=pthatcher,henrik.lundin,tina.legrand,stefan,tkchin,glaznev,kjellander,perkj,mflodman,henrika,asapersson,niklas.enbom
Review URL: https://webrtc-codereview.appspot.com/23129004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@7726 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-11-20 22:28:14 +00:00
|
|
|
size_t VideoProcessorImpl::EncodedFrameSize() {
|
2012-06-05 21:07:28 +00:00
|
|
|
return encoded_frame_size_;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-19 02:39:06 -07:00
|
|
|
FrameType VideoProcessorImpl::EncodedFrameType() {
|
2015-09-17 12:16:04 -07:00
|
|
|
return encoded_frame_type_;
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-05 21:07:28 +00:00
|
|
|
int VideoProcessorImpl::NumberDroppedFrames() {
|
|
|
|
|
return num_dropped_frames_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int VideoProcessorImpl::NumberSpatialResizes() {
|
|
|
|
|
return num_spatial_resizes_;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-06 06:44:54 +00:00
|
|
|
bool VideoProcessorImpl::ProcessFrame(int frame_number) {
|
2017-02-22 01:26:59 -08:00
|
|
|
RTC_CHECK_GE(frame_number, 0);
|
2017-02-10 00:16:07 -08:00
|
|
|
RTC_CHECK(initialized_) << "Attempting to use uninitialized VideoProcessor";
|
|
|
|
|
|
2017-02-22 01:26:59 -08:00
|
|
|
rtc::scoped_refptr<VideoFrameBuffer> buffer(
|
|
|
|
|
analysis_frame_reader_->ReadFrame());
|
2017-02-21 05:24:03 -08:00
|
|
|
if (buffer) {
|
2017-02-22 01:26:59 -08:00
|
|
|
if (source_frame_writer_) {
|
|
|
|
|
// TODO(brandtr): Introduce temp buffer as data member, to avoid
|
|
|
|
|
// allocating for every frame.
|
|
|
|
|
size_t length = CalcBufferSize(kI420, buffer->width(), buffer->height());
|
|
|
|
|
std::unique_ptr<uint8_t[]> extracted_buffer(new uint8_t[length]);
|
|
|
|
|
int extracted_length =
|
|
|
|
|
ExtractBuffer(buffer, length, extracted_buffer.get());
|
|
|
|
|
RTC_CHECK_EQ(extracted_length, source_frame_writer_->FrameLength());
|
|
|
|
|
source_frame_writer_->WriteFrame(extracted_buffer.get());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Use the frame number as basis for timestamp to identify frames. Let the
|
|
|
|
|
// first timestamp be non-zero, to not make the IvfFileWriter believe that
|
|
|
|
|
// we want to use capture timestamps in the IVF files.
|
|
|
|
|
VideoFrame source_frame(buffer,
|
|
|
|
|
(frame_number + 1) * k90khzTimestampFrameDiff, 0,
|
|
|
|
|
webrtc::kVideoRotation_0);
|
2011-10-06 06:44:54 +00:00
|
|
|
|
2017-02-15 05:19:51 -08:00
|
|
|
// Ensure we have a new statistics data object we can fill.
|
2011-10-06 06:44:54 +00:00
|
|
|
FrameStatistic& stat = stats_->NewFrame(frame_number);
|
|
|
|
|
|
2017-02-15 05:19:51 -08:00
|
|
|
// Decide if we are going to force a keyframe.
|
2015-10-23 15:58:18 +02:00
|
|
|
std::vector<FrameType> frame_types(1, kVideoFrameDelta);
|
2011-10-07 06:50:22 +00:00
|
|
|
if (config_.keyframe_interval > 0 &&
|
|
|
|
|
frame_number % config_.keyframe_interval == 0) {
|
2015-10-23 15:58:18 +02:00
|
|
|
frame_types[0] = kVideoFrameKey;
|
2011-10-07 06:50:22 +00:00
|
|
|
}
|
2012-06-05 21:07:28 +00:00
|
|
|
|
|
|
|
|
// For dropped frames, we regard them as zero size encoded frames.
|
|
|
|
|
encoded_frame_size_ = 0;
|
2015-10-23 15:58:18 +02:00
|
|
|
encoded_frame_type_ = kVideoFrameDelta;
|
2012-06-05 21:07:28 +00:00
|
|
|
|
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.
|
|
|
|
|
encode_start_ns_ = rtc::TimeNanos();
|
2017-02-10 00:16:07 -08:00
|
|
|
int32_t encode_result =
|
|
|
|
|
encoder_->Encode(source_frame, nullptr, &frame_types);
|
2012-06-05 21:07:28 +00:00
|
|
|
|
2011-10-06 06:44:54 +00:00
|
|
|
if (encode_result != WEBRTC_VIDEO_CODEC_OK) {
|
|
|
|
|
fprintf(stderr, "Failed to encode frame %d, return code: %d\n",
|
|
|
|
|
frame_number, encode_result);
|
|
|
|
|
}
|
|
|
|
|
stat.encode_return_code = encode_result;
|
2017-02-15 05:19:51 -08:00
|
|
|
|
2011-10-06 06:44:54 +00:00
|
|
|
return true;
|
|
|
|
|
} else {
|
2017-02-15 05:19:51 -08:00
|
|
|
// Last frame has been reached.
|
|
|
|
|
return false;
|
2011-10-06 06:44:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-27 01:36:03 -08:00
|
|
|
void VideoProcessorImpl::FrameEncoded(
|
|
|
|
|
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.
|
2015-12-21 03:04:49 -08:00
|
|
|
int num_dropped_from_prev_encode =
|
2017-02-22 01:26:59 -08:00
|
|
|
(encoded_image._timeStamp - prev_time_stamp_) / k90khzTimestampFrameDiff -
|
|
|
|
|
1;
|
2015-12-21 03:04:49 -08:00
|
|
|
num_dropped_frames_ += num_dropped_from_prev_encode;
|
|
|
|
|
prev_time_stamp_ = encoded_image._timeStamp;
|
2012-06-05 21:07:28 +00:00
|
|
|
if (num_dropped_from_prev_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_prev_encode; i++) {
|
2017-02-22 01:26:59 -08:00
|
|
|
RTC_CHECK(analysis_frame_writer_->WriteFrame(
|
|
|
|
|
last_successful_frame_buffer_.get()));
|
|
|
|
|
if (decoded_frame_writer_) {
|
|
|
|
|
RTC_CHECK(decoded_frame_writer_->WriteFrame(
|
|
|
|
|
last_successful_frame_buffer_.get()));
|
|
|
|
|
}
|
2012-06-05 21:07:28 +00:00
|
|
|
}
|
|
|
|
|
}
|
2017-02-22 01:26:59 -08:00
|
|
|
|
2012-06-05 21:07:28 +00:00
|
|
|
// Frame is not dropped, so update the encoded frame size
|
|
|
|
|
// (encoder callback is only called for non-zero length frames).
|
2014-12-01 15:23:21 +00:00
|
|
|
encoded_frame_size_ = encoded_image._length;
|
2015-09-17 12:16:04 -07:00
|
|
|
encoded_frame_type_ = encoded_image._frameType;
|
2017-02-22 01:26:59 -08:00
|
|
|
int frame_number = encoded_image._timeStamp / k90khzTimestampFrameDiff - 1;
|
2011-10-06 06:44:54 +00:00
|
|
|
FrameStatistic& stat = stats_->stats_[frame_number];
|
2015-12-21 03:04:49 -08:00
|
|
|
stat.encode_time_in_us =
|
2016-05-10 16:31:47 +02:00
|
|
|
GetElapsedTimeMicroseconds(encode_start_ns_, encode_stop_ns);
|
2011-10-06 06:44:54 +00:00
|
|
|
stat.encoding_successful = true;
|
2014-12-01 15:23:21 +00:00
|
|
|
stat.encoded_frame_length_in_bytes = encoded_image._length;
|
2017-02-22 01:26:59 -08:00
|
|
|
stat.frame_number = frame_number;
|
2014-12-01 15:23:21 +00:00
|
|
|
stat.frame_type = encoded_image._frameType;
|
2017-02-23 01:33:04 -08:00
|
|
|
stat.qp = encoded_image.qp_;
|
2014-12-01 15:23:21 +00:00
|
|
|
stat.bit_rate_in_kbps = encoded_image._length * bit_rate_factor_;
|
2015-12-21 03:04:49 -08:00
|
|
|
stat.total_packets =
|
|
|
|
|
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) {
|
|
|
|
|
stat.packets_dropped =
|
2015-12-21 03:04:49 -08:00
|
|
|
packet_manipulator_->ManipulatePackets(&copied_image);
|
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.
|
|
|
|
|
decode_start_ns_ = rtc::TimeNanos();
|
2014-12-01 15:23:21 +00:00
|
|
|
int32_t decode_result =
|
2017-02-10 00:16:07 -08:00
|
|
|
decoder_->Decode(copied_image, last_frame_missing_, nullptr);
|
2011-10-06 06:44:54 +00:00
|
|
|
stat.decode_return_code = decode_result;
|
2017-02-15 05:19:51 -08:00
|
|
|
|
2011-10-06 06:44:54 +00:00
|
|
|
if (decode_result != WEBRTC_VIDEO_CODEC_OK) {
|
|
|
|
|
// 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-02-22 01:26:59 -08:00
|
|
|
RTC_CHECK(analysis_frame_writer_->WriteFrame(
|
|
|
|
|
last_successful_frame_buffer_.get()));
|
|
|
|
|
if (decoded_frame_writer_) {
|
|
|
|
|
RTC_CHECK(decoded_frame_writer_->WriteFrame(
|
|
|
|
|
last_successful_frame_buffer_.get()));
|
|
|
|
|
}
|
2011-10-06 06:44:54 +00:00
|
|
|
}
|
2017-02-15 05:19:51 -08:00
|
|
|
|
|
|
|
|
// Save status for losses so we can inform the decoder for the next frame.
|
2014-12-01 15:23:21 +00:00
|
|
|
last_frame_missing_ = copied_image._length == 0;
|
2011-10-06 06:44:54 +00:00
|
|
|
}
|
|
|
|
|
|
2015-05-29 17:21:40 -07:00
|
|
|
void VideoProcessorImpl::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
|
|
|
|
|
|
|
|
// Report stats.
|
2017-02-22 01:26:59 -08:00
|
|
|
int frame_number = image.timestamp() / k90khzTimestampFrameDiff - 1;
|
2011-10-06 06:44:54 +00:00
|
|
|
FrameStatistic& stat = stats_->stats_[frame_number];
|
2015-12-21 03:04:49 -08:00
|
|
|
stat.decode_time_in_us =
|
2016-05-10 16:31:47 +02:00
|
|
|
GetElapsedTimeMicroseconds(decode_start_ns_, decode_stop_ns);
|
2011-10-06 06:44:54 +00:00
|
|
|
stat.decoding_successful = true;
|
|
|
|
|
|
2017-02-15 05:19:51 -08:00
|
|
|
// Check for resize action (either down or up).
|
2012-10-24 18:33:04 +00:00
|
|
|
if (static_cast<int>(image.width()) != last_encoder_frame_width_ ||
|
2015-12-21 03:04:49 -08:00
|
|
|
static_cast<int>(image.height()) != last_encoder_frame_height_) {
|
2012-06-05 21:07:28 +00:00
|
|
|
++num_spatial_resizes_;
|
2012-10-24 18:33:04 +00:00
|
|
|
last_encoder_frame_width_ = image.width();
|
|
|
|
|
last_encoder_frame_height_ = image.height();
|
2012-06-05 21:07:28 +00:00
|
|
|
}
|
|
|
|
|
// Check if codec size is different from native/original size, and if so,
|
2017-02-15 05:19:51 -08:00
|
|
|
// upsample back to original size. This is needed for PSNR and SSIM
|
|
|
|
|
// calculations.
|
2015-12-21 03:04:49 -08:00
|
|
|
if (image.width() != config_.codec_settings->width ||
|
2012-10-24 18:33:04 +00:00
|
|
|
image.height() != config_.codec_settings->height) {
|
2016-06-13 13:06:01 +02:00
|
|
|
rtc::scoped_refptr<I420Buffer> up_image(
|
2016-06-20 03:38:52 -07:00
|
|
|
I420Buffer::Create(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-02-20 04:35:52 -08:00
|
|
|
if (image.video_frame_buffer()->native_handle()) {
|
|
|
|
|
up_image->ScaleFrom(*image.video_frame_buffer()->NativeToI420Buffer());
|
|
|
|
|
} else {
|
|
|
|
|
up_image->ScaleFrom(*image.video_frame_buffer());
|
|
|
|
|
}
|
2016-06-13 13:06:01 +02:00
|
|
|
|
2012-10-24 18:33:04 +00:00
|
|
|
// TODO(mikhal): Extracting the buffer for now - need to update test.
|
2016-06-13 13:06:01 +02:00
|
|
|
size_t length =
|
|
|
|
|
CalcBufferSize(kI420, up_image->width(), up_image->height());
|
2016-02-29 05:51:59 -08:00
|
|
|
std::unique_ptr<uint8_t[]> image_buffer(new uint8_t[length]);
|
Use size_t more consistently for packet/payload lengths.
See design doc at https://docs.google.com/a/chromium.org/document/d/1I6nmE9D_BmCY-IoV6MDPY2V6WYpEI-dg2apWXTfZyUI/edit?usp=sharing for more information.
This CL was reviewed and approved in pieces in the following CLs:
https://webrtc-codereview.appspot.com/24209004/
https://webrtc-codereview.appspot.com/24229004/
https://webrtc-codereview.appspot.com/24259004/
https://webrtc-codereview.appspot.com/25109004/
https://webrtc-codereview.appspot.com/26099004/
https://webrtc-codereview.appspot.com/27069004/
https://webrtc-codereview.appspot.com/27969004/
https://webrtc-codereview.appspot.com/27989004/
https://webrtc-codereview.appspot.com/29009004/
https://webrtc-codereview.appspot.com/30929004/
https://webrtc-codereview.appspot.com/30939004/
https://webrtc-codereview.appspot.com/31999004/
Committing as TBR to the original reviewers.
BUG=chromium:81439
TEST=none
TBR=pthatcher,henrik.lundin,tina.legrand,stefan,tkchin,glaznev,kjellander,perkj,mflodman,henrika,asapersson,niklas.enbom
Review URL: https://webrtc-codereview.appspot.com/23129004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@7726 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-11-20 22:28:14 +00:00
|
|
|
int extracted_length = ExtractBuffer(up_image, length, image_buffer.get());
|
2017-02-22 01:26:59 -08:00
|
|
|
RTC_CHECK_GT(extracted_length, 0);
|
2017-02-15 05:19:51 -08:00
|
|
|
// Update our copy of the last successful frame.
|
2017-02-10 00:16:07 -08:00
|
|
|
memcpy(last_successful_frame_buffer_.get(), image_buffer.get(),
|
|
|
|
|
extracted_length);
|
2017-02-22 01:26:59 -08:00
|
|
|
|
|
|
|
|
RTC_CHECK(analysis_frame_writer_->WriteFrame(image_buffer.get()));
|
|
|
|
|
if (decoded_frame_writer_) {
|
|
|
|
|
RTC_CHECK(decoded_frame_writer_->WriteFrame(image_buffer.get()));
|
2012-06-05 21:07:28 +00:00
|
|
|
}
|
|
|
|
|
} else { // No resize.
|
2017-02-15 05:19:51 -08:00
|
|
|
// Update our copy of the last successful frame.
|
2012-10-24 18:33:04 +00:00
|
|
|
// TODO(mikhal): Add as a member function, so won't be allocated per frame.
|
Use size_t more consistently for packet/payload lengths.
See design doc at https://docs.google.com/a/chromium.org/document/d/1I6nmE9D_BmCY-IoV6MDPY2V6WYpEI-dg2apWXTfZyUI/edit?usp=sharing for more information.
This CL was reviewed and approved in pieces in the following CLs:
https://webrtc-codereview.appspot.com/24209004/
https://webrtc-codereview.appspot.com/24229004/
https://webrtc-codereview.appspot.com/24259004/
https://webrtc-codereview.appspot.com/25109004/
https://webrtc-codereview.appspot.com/26099004/
https://webrtc-codereview.appspot.com/27069004/
https://webrtc-codereview.appspot.com/27969004/
https://webrtc-codereview.appspot.com/27989004/
https://webrtc-codereview.appspot.com/29009004/
https://webrtc-codereview.appspot.com/30929004/
https://webrtc-codereview.appspot.com/30939004/
https://webrtc-codereview.appspot.com/31999004/
Committing as TBR to the original reviewers.
BUG=chromium:81439
TEST=none
TBR=pthatcher,henrik.lundin,tina.legrand,stefan,tkchin,glaznev,kjellander,perkj,mflodman,henrika,asapersson,niklas.enbom
Review URL: https://webrtc-codereview.appspot.com/23129004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@7726 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-11-20 22:28:14 +00:00
|
|
|
size_t length = CalcBufferSize(kI420, image.width(), image.height());
|
2016-02-29 05:51:59 -08:00
|
|
|
std::unique_ptr<uint8_t[]> image_buffer(new uint8_t[length]);
|
2017-02-20 04:35:52 -08:00
|
|
|
int extracted_length;
|
|
|
|
|
if (image.video_frame_buffer()->native_handle()) {
|
|
|
|
|
extracted_length =
|
|
|
|
|
ExtractBuffer(image.video_frame_buffer()->NativeToI420Buffer(),
|
|
|
|
|
length, image_buffer.get());
|
|
|
|
|
} else {
|
|
|
|
|
extracted_length =
|
|
|
|
|
ExtractBuffer(image.video_frame_buffer(), length, image_buffer.get());
|
|
|
|
|
}
|
2017-02-22 01:26:59 -08:00
|
|
|
RTC_CHECK_GT(extracted_length, 0);
|
2017-02-10 00:16:07 -08:00
|
|
|
memcpy(last_successful_frame_buffer_.get(), image_buffer.get(),
|
|
|
|
|
extracted_length);
|
2012-10-24 18:33:04 +00:00
|
|
|
|
2017-02-22 01:26:59 -08:00
|
|
|
RTC_CHECK(analysis_frame_writer_->WriteFrame(image_buffer.get()));
|
|
|
|
|
if (decoded_frame_writer_) {
|
|
|
|
|
RTC_CHECK(decoded_frame_writer_->WriteFrame(image_buffer.get()));
|
2012-06-05 21:07:28 +00:00
|
|
|
}
|
2011-10-06 06:44:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-10 16:31:47 +02:00
|
|
|
int VideoProcessorImpl::GetElapsedTimeMicroseconds(int64_t start,
|
|
|
|
|
int64_t stop) {
|
2016-11-28 01:54:54 -08:00
|
|
|
int64_t encode_time = (stop - start) / rtc::kNumNanosecsPerMicrosec;
|
|
|
|
|
RTC_DCHECK_GE(encode_time, std::numeric_limits<int>::min());
|
|
|
|
|
RTC_DCHECK_LE(encode_time, std::numeric_limits<int>::max());
|
2011-10-06 06:44:54 +00:00
|
|
|
return static_cast<int>(encode_time);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace test
|
|
|
|
|
} // namespace webrtc
|