2011-10-06 06:44:54 +00:00
|
|
|
/*
|
2012-06-05 21:07:28 +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
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#ifndef MODULES_VIDEO_CODING_CODECS_TEST_VIDEOPROCESSOR_H_
|
|
|
|
|
#define MODULES_VIDEO_CODING_CODECS_TEST_VIDEOPROCESSOR_H_
|
2011-10-06 06:44:54 +00:00
|
|
|
|
2017-08-30 06:29:51 -07:00
|
|
|
#include <map>
|
2016-11-16 16:41:30 +01:00
|
|
|
#include <memory>
|
2011-10-06 06:44:54 +00:00
|
|
|
#include <string>
|
2017-03-07 01:41:43 -08:00
|
|
|
#include <vector>
|
2011-10-06 06:44:54 +00:00
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "api/video/video_frame.h"
|
2018-03-08 16:45:54 +01:00
|
|
|
#include "common_video/include/video_bitrate_allocator.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/video_coding/codecs/test/stats.h"
|
2017-10-19 14:05:50 +02:00
|
|
|
#include "modules/video_coding/codecs/test/test_config.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/video_coding/utility/ivf_file_writer.h"
|
|
|
|
|
#include "rtc_base/buffer.h"
|
|
|
|
|
#include "rtc_base/constructormagic.h"
|
|
|
|
|
#include "rtc_base/sequenced_task_checker.h"
|
|
|
|
|
#include "rtc_base/task_queue.h"
|
|
|
|
|
#include "test/testsupport/frame_reader.h"
|
|
|
|
|
#include "test/testsupport/frame_writer.h"
|
2011-10-06 06:44:54 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
namespace test {
|
|
|
|
|
|
|
|
|
|
// Handles encoding/decoding of video using the VideoEncoder/VideoDecoder
|
|
|
|
|
// interfaces. This is done in a sequential manner in order to be able to
|
|
|
|
|
// measure times properly.
|
|
|
|
|
// The class processes a frame at the time for the configured input file.
|
|
|
|
|
// It maintains state of where in the source input file the processing is at.
|
|
|
|
|
class VideoProcessor {
|
|
|
|
|
public:
|
2018-02-01 13:25:17 +01:00
|
|
|
using VideoDecoderList = std::vector<std::unique_ptr<VideoDecoder>>;
|
|
|
|
|
using IvfFileWriterList = std::vector<std::unique_ptr<IvfFileWriter>>;
|
|
|
|
|
using FrameWriterList = std::vector<std::unique_ptr<FrameWriter>>;
|
|
|
|
|
|
2017-08-07 08:12:33 -07:00
|
|
|
VideoProcessor(webrtc::VideoEncoder* encoder,
|
2018-02-01 13:25:17 +01:00
|
|
|
VideoDecoderList* decoders,
|
|
|
|
|
FrameReader* input_frame_reader,
|
2017-08-07 08:12:33 -07:00
|
|
|
const TestConfig& config,
|
2018-02-20 09:48:26 +01:00
|
|
|
Stats* stats,
|
2018-02-01 13:25:17 +01:00
|
|
|
IvfFileWriterList* encoded_frame_writers,
|
|
|
|
|
FrameWriterList* decoded_frame_writers);
|
2017-08-07 08:12:33 -07:00
|
|
|
~VideoProcessor();
|
2011-10-06 06:44:54 +00:00
|
|
|
|
2018-02-01 13:25:17 +01:00
|
|
|
// Reads a frame and sends it to the encoder. When the encode callback
|
|
|
|
|
// is received, the encoded frame is buffered. After encoding is finished
|
|
|
|
|
// buffered frame is sent to decoder. Quality evaluation is done in
|
|
|
|
|
// the decode callback.
|
2017-09-06 01:53:22 -07:00
|
|
|
void ProcessFrame();
|
2012-06-05 21:07:28 +00:00
|
|
|
|
2017-08-21 01:34:04 -07:00
|
|
|
// Updates the encoder with target rates. Must be called at least once.
|
2018-01-17 15:11:44 +01:00
|
|
|
void SetRates(size_t bitrate_kbps, size_t framerate_fps);
|
2011-10-06 06:44:54 +00:00
|
|
|
|
|
|
|
|
private:
|
2017-02-15 05:19:51 -08:00
|
|
|
class VideoProcessorEncodeCompleteCallback
|
|
|
|
|
: public webrtc::EncodedImageCallback {
|
|
|
|
|
public:
|
2017-08-07 08:12:33 -07:00
|
|
|
explicit VideoProcessorEncodeCompleteCallback(
|
|
|
|
|
VideoProcessor* video_processor)
|
2017-08-21 06:44:16 -07:00
|
|
|
: video_processor_(video_processor),
|
2018-02-07 13:56:16 +01:00
|
|
|
task_queue_(rtc::TaskQueue::Current()) {
|
2018-02-28 17:17:15 +01:00
|
|
|
RTC_DCHECK(video_processor_);
|
2018-02-07 13:56:16 +01:00
|
|
|
RTC_DCHECK(task_queue_);
|
|
|
|
|
}
|
2017-08-21 06:44:16 -07:00
|
|
|
|
2017-02-15 05:19:51 -08:00
|
|
|
Result OnEncodedImage(
|
|
|
|
|
const webrtc::EncodedImage& encoded_image,
|
|
|
|
|
const webrtc::CodecSpecificInfo* codec_specific_info,
|
|
|
|
|
const webrtc::RTPFragmentationHeader* fragmentation) override {
|
|
|
|
|
RTC_CHECK(codec_specific_info);
|
2017-08-21 06:44:16 -07:00
|
|
|
|
2018-02-07 13:56:16 +01:00
|
|
|
// Post the callback to the right task queue, if needed.
|
|
|
|
|
if (!task_queue_->IsCurrent()) {
|
2017-08-22 03:33:11 -07:00
|
|
|
task_queue_->PostTask(
|
|
|
|
|
std::unique_ptr<rtc::QueuedTask>(new EncodeCallbackTask(
|
|
|
|
|
video_processor_, encoded_image, codec_specific_info)));
|
2017-08-21 06:44:16 -07:00
|
|
|
return Result(Result::OK, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-01 13:25:17 +01:00
|
|
|
video_processor_->FrameEncoded(encoded_image, *codec_specific_info);
|
2017-02-15 05:19:51 -08:00
|
|
|
return Result(Result::OK, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2017-08-21 06:44:16 -07:00
|
|
|
class EncodeCallbackTask : public rtc::QueuedTask {
|
|
|
|
|
public:
|
|
|
|
|
EncodeCallbackTask(VideoProcessor* video_processor,
|
|
|
|
|
const webrtc::EncodedImage& encoded_image,
|
2017-08-22 03:33:11 -07:00
|
|
|
const webrtc::CodecSpecificInfo* codec_specific_info)
|
2017-08-21 06:44:16 -07:00
|
|
|
: video_processor_(video_processor),
|
|
|
|
|
buffer_(encoded_image._buffer, encoded_image._length),
|
|
|
|
|
encoded_image_(encoded_image),
|
|
|
|
|
codec_specific_info_(*codec_specific_info) {
|
|
|
|
|
encoded_image_._buffer = buffer_.data();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Run() override {
|
2018-02-01 13:25:17 +01:00
|
|
|
video_processor_->FrameEncoded(encoded_image_, codec_specific_info_);
|
2017-08-21 06:44:16 -07:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
VideoProcessor* const video_processor_;
|
|
|
|
|
rtc::Buffer buffer_;
|
|
|
|
|
webrtc::EncodedImage encoded_image_;
|
|
|
|
|
const webrtc::CodecSpecificInfo codec_specific_info_;
|
|
|
|
|
};
|
|
|
|
|
|
2017-08-07 08:12:33 -07:00
|
|
|
VideoProcessor* const video_processor_;
|
2017-08-21 06:44:16 -07:00
|
|
|
rtc::TaskQueue* const task_queue_;
|
2017-02-15 05:19:51 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class VideoProcessorDecodeCompleteCallback
|
|
|
|
|
: public webrtc::DecodedImageCallback {
|
|
|
|
|
public:
|
2017-08-07 08:12:33 -07:00
|
|
|
explicit VideoProcessorDecodeCompleteCallback(
|
|
|
|
|
VideoProcessor* video_processor)
|
2017-08-21 06:44:16 -07:00
|
|
|
: video_processor_(video_processor),
|
2018-02-07 13:56:16 +01:00
|
|
|
task_queue_(rtc::TaskQueue::Current()) {
|
2018-02-28 17:17:15 +01:00
|
|
|
RTC_DCHECK(video_processor_);
|
2018-02-07 13:56:16 +01:00
|
|
|
RTC_DCHECK(task_queue_);
|
|
|
|
|
}
|
2017-08-21 06:44:16 -07:00
|
|
|
|
2017-02-15 05:19:51 -08:00
|
|
|
int32_t Decoded(webrtc::VideoFrame& image) override {
|
2018-02-07 13:56:16 +01:00
|
|
|
// Post the callback to the right task queue, if needed.
|
|
|
|
|
if (!task_queue_->IsCurrent()) {
|
2017-08-21 06:44:16 -07:00
|
|
|
task_queue_->PostTask(
|
|
|
|
|
[this, image]() { video_processor_->FrameDecoded(image); });
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2017-02-15 05:19:51 -08:00
|
|
|
video_processor_->FrameDecoded(image);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2017-08-21 06:44:16 -07:00
|
|
|
|
2017-02-15 05:19:51 -08:00
|
|
|
int32_t Decoded(webrtc::VideoFrame& image,
|
|
|
|
|
int64_t decode_time_ms) override {
|
2017-02-20 04:35:52 -08:00
|
|
|
return Decoded(image);
|
2017-02-15 05:19:51 -08:00
|
|
|
}
|
2017-08-21 06:44:16 -07:00
|
|
|
|
2017-02-20 04:35:52 -08:00
|
|
|
void Decoded(webrtc::VideoFrame& image,
|
2017-02-15 05:19:51 -08:00
|
|
|
rtc::Optional<int32_t> decode_time_ms,
|
|
|
|
|
rtc::Optional<uint8_t> qp) override {
|
2017-08-07 03:36:54 -07:00
|
|
|
Decoded(image);
|
2017-02-15 05:19:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2017-08-07 08:12:33 -07:00
|
|
|
VideoProcessor* const video_processor_;
|
2017-08-21 06:44:16 -07:00
|
|
|
rtc::TaskQueue* const task_queue_;
|
2017-02-15 05:19:51 -08:00
|
|
|
};
|
|
|
|
|
|
2017-08-21 01:34:04 -07:00
|
|
|
// Invoked by the callback adapter when a frame has completed encoding.
|
2018-02-01 13:25:17 +01:00
|
|
|
void FrameEncoded(const webrtc::EncodedImage& encoded_image,
|
|
|
|
|
const webrtc::CodecSpecificInfo& codec_specific);
|
2017-02-15 05:19:51 -08:00
|
|
|
|
2017-08-21 01:34:04 -07:00
|
|
|
// Invoked by the callback adapter when a frame has completed decoding.
|
2015-05-29 17:21:40 -07:00
|
|
|
void FrameDecoded(const webrtc::VideoFrame& image);
|
2017-02-15 05:19:51 -08:00
|
|
|
|
2018-02-01 13:25:17 +01:00
|
|
|
void CopyEncodedImage(const EncodedImage& encoded_image,
|
|
|
|
|
const VideoCodecType codec,
|
|
|
|
|
size_t frame_number,
|
|
|
|
|
size_t simulcast_svc_idx);
|
|
|
|
|
|
2018-03-08 16:45:54 +01:00
|
|
|
// Test input/output.
|
2017-09-07 07:53:45 -07:00
|
|
|
TestConfig config_ RTC_GUARDED_BY(sequence_checker_);
|
2018-02-01 13:25:17 +01:00
|
|
|
const size_t num_simulcast_or_spatial_layers_;
|
2018-03-08 16:45:54 +01:00
|
|
|
Stats* const stats_;
|
2018-02-01 13:25:17 +01:00
|
|
|
|
2018-03-08 16:45:54 +01:00
|
|
|
// Codecs.
|
2017-02-10 00:16:07 -08:00
|
|
|
webrtc::VideoEncoder* const encoder_;
|
2018-02-01 13:25:17 +01:00
|
|
|
VideoDecoderList* const decoders_;
|
2017-02-28 07:13:47 -08:00
|
|
|
const std::unique_ptr<VideoBitrateAllocator> bitrate_allocator_;
|
2018-01-17 15:11:44 +01:00
|
|
|
BitrateAllocation bitrate_allocation_ RTC_GUARDED_BY(sequence_checker_);
|
2018-03-08 16:45:54 +01:00
|
|
|
uint32_t framerate_fps_ RTC_GUARDED_BY(sequence_checker_);
|
2017-02-28 07:13:47 -08:00
|
|
|
|
|
|
|
|
// Adapters for the codec callbacks.
|
2017-08-21 01:34:04 -07:00
|
|
|
VideoProcessorEncodeCompleteCallback encode_callback_;
|
|
|
|
|
VideoProcessorDecodeCompleteCallback decode_callback_;
|
2017-02-28 07:13:47 -08:00
|
|
|
|
2018-03-08 16:45:54 +01:00
|
|
|
// Each call to ProcessFrame() will read one frame from |input_frame_reader_|.
|
|
|
|
|
FrameReader* const input_frame_reader_;
|
|
|
|
|
|
|
|
|
|
// Input frames are used as reference for frame quality evaluations.
|
2017-11-17 14:47:32 +01:00
|
|
|
// Async codecs might queue frames. To handle that we keep input frame
|
|
|
|
|
// and release it after corresponding coded frame is decoded and quality
|
|
|
|
|
// measurement is done.
|
2018-03-08 16:45:54 +01:00
|
|
|
// frame_number -> frame.
|
|
|
|
|
std::map<size_t, VideoFrame> input_frames_ RTC_GUARDED_BY(sequence_checker_);
|
2017-11-17 14:47:32 +01:00
|
|
|
|
2018-03-08 16:45:54 +01:00
|
|
|
// Encoder delivers coded frame layer-by-layer. We store coded frames and
|
|
|
|
|
// then, after all layers are encoded, decode them. Such separation of
|
|
|
|
|
// frame processing on superframe level simplifies encoding/decoding time
|
|
|
|
|
// measurement.
|
|
|
|
|
std::map<size_t, EncodedImage> last_encoded_frames_
|
|
|
|
|
RTC_GUARDED_BY(sequence_checker_);
|
2017-02-28 07:13:47 -08:00
|
|
|
|
2017-08-07 08:30:43 -07:00
|
|
|
// These (optional) file writers are used to persistently store the encoded
|
2018-03-08 16:45:54 +01:00
|
|
|
// and decoded bitstreams. Each frame writer is enabled by being non-null.
|
2018-02-01 13:25:17 +01:00
|
|
|
IvfFileWriterList* const encoded_frame_writers_;
|
|
|
|
|
FrameWriterList* const decoded_frame_writers_;
|
2018-03-08 16:45:54 +01:00
|
|
|
rtc::Buffer tmp_i420_buffer_; // Temp storage for format conversion.
|
2017-02-22 01:26:59 -08:00
|
|
|
|
2018-03-08 16:45:54 +01:00
|
|
|
// Metadata of inputed/encoded/decoded frames. Used for frame drop detection
|
|
|
|
|
// and other purposes.
|
2018-01-17 15:11:44 +01:00
|
|
|
size_t last_inputed_frame_num_ RTC_GUARDED_BY(sequence_checker_);
|
2018-03-08 16:45:54 +01:00
|
|
|
size_t last_inputed_timestamp_ RTC_GUARDED_BY(sequence_checker_);
|
|
|
|
|
bool first_encoded_frame RTC_GUARDED_BY(sequence_checker_);
|
2018-01-17 15:11:44 +01:00
|
|
|
size_t last_encoded_frame_num_ RTC_GUARDED_BY(sequence_checker_);
|
2018-02-01 13:25:17 +01:00
|
|
|
size_t last_encoded_simulcast_svc_idx_ RTC_GUARDED_BY(sequence_checker_);
|
2018-01-17 15:11:44 +01:00
|
|
|
size_t last_decoded_frame_num_ RTC_GUARDED_BY(sequence_checker_);
|
2017-03-07 01:41:43 -08:00
|
|
|
|
2018-02-01 13:25:17 +01:00
|
|
|
// Map of frame size (in pixels) to simulcast/spatial layer index.
|
|
|
|
|
std::map<size_t, size_t> frame_wxh_to_simulcast_svc_idx_
|
|
|
|
|
RTC_GUARDED_BY(sequence_checker_);
|
|
|
|
|
|
2018-03-08 16:45:54 +01:00
|
|
|
// This class must be operated on a TaskQueue.
|
2017-08-21 06:44:16 -07:00
|
|
|
rtc::SequencedTaskChecker sequence_checker_;
|
2017-08-21 01:34:04 -07:00
|
|
|
|
|
|
|
|
RTC_DISALLOW_COPY_AND_ASSIGN(VideoProcessor);
|
2011-10-06 06:44:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace test
|
|
|
|
|
} // namespace webrtc
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#endif // MODULES_VIDEO_CODING_CODECS_TEST_VIDEOPROCESSOR_H_
|