#ifndef MEDIA_GPU_TEST_VIDEO_ENCODER_BITSTREAM_VALIDATOR_H_
#define MEDIA_GPU_TEST_VIDEO_ENCODER_BITSTREAM_VALIDATOR_H_
#include <memory>
#include <optional>
#include <utility>
#include <vector>
#include "base/containers/lru_cache.h"
#include "base/memory/scoped_refptr.h"
#include "base/sequence_checker.h"
#include "base/synchronization/condition_variable.h"
#include "base/synchronization/lock.h"
#include "base/thread_annotations.h"
#include "base/threading/thread.h"
#include "media/base/bitstream_buffer.h"
#include "media/base/decoder_status.h"
#include "media/base/video_decoder.h"
#include "media/gpu/test/bitstream_helpers.h"
#include "media/gpu/test/video_frame_helpers.h"
#include "media/video/video_encode_accelerator.h"
#include "ui/gfx/buffer_types.h"
#include "ui/gfx/geometry/size.h"
namespace media {
class MediaLog;
class VideoFrame;
class VideoDecoderConfig;
namespace test {
class BitstreamValidator : public BitstreamProcessor {
public:
static std::unique_ptr<BitstreamValidator> Create(
const VideoDecoderConfig& decoder_config,
size_t last_frame_index,
std::vector<std::unique_ptr<VideoFrameProcessor>> video_frame_processors =
{},
std::optional<size_t> spatial_layer_index_to_decode_ = std::nullopt,
std::optional<size_t> temporal_layer_index_to_decode = std::nullopt,
const std::vector<gfx::Size>& spatial_layer_resolutions = {});
~BitstreamValidator() override;
void ProcessBitstream(scoped_refptr<BitstreamRef> bitstream,
size_t frame_index) override;
bool WaitUntilDone() override;
private:
BitstreamValidator(
std::unique_ptr<VideoDecoder> decoder,
std::unique_ptr<MediaLog> media_log,
size_t last_frame_index,
const gfx::Rect& decoding_rect,
std::optional<size_t> spatial_layer_index_to_decode_,
std::optional<size_t> temporal_layer_index_to_decode,
const std::vector<gfx::Size>& spatial_layer_resolutions,
std::vector<std::unique_ptr<VideoFrameProcessor>> video_frame_processors);
BitstreamValidator(const BitstreamValidator&) = delete;
BitstreamValidator& operator=(const BitstreamValidator&) = delete;
bool Initialize(const VideoDecoderConfig& decoder_config);
void InitializeVideoDecoder(const VideoDecoderConfig& decoder_config,
VideoDecoder::InitCB init_cb);
void ProcessBitstreamTask(scoped_refptr<BitstreamRef> decoder_buffer,
size_t frame_index);
void OutputFrameProcessed();
void DecodeDone(int64_t timestamp, DecoderStatus status);
void VerifyOutputFrame(scoped_refptr<VideoFrame> frame);
void ConstructSpatialIndices(
const std::vector<gfx::Size>& spatial_layer_resolutions);
std::unique_ptr<VideoDecoder> decoder_;
const std::unique_ptr<MediaLog> media_log_;
const size_t last_frame_index_;
const gfx::Rect desired_decoding_rect_;
const std::optional<size_t> spatial_layer_index_to_decode_;
const std::optional<size_t> temporal_layer_index_to_decode_;
const std::vector<gfx::Size> spatial_layer_resolutions_;
const std::vector<std::unique_ptr<VideoFrameProcessor>>
video_frame_processors_;
static constexpr size_t kDecoderBufferMapSize = 32;
base::LRUCache<int64_t, std::pair<size_t, scoped_refptr<BitstreamRef>>>
decoding_buffers_{kDecoderBufferMapSize};
std::vector<uint8_t> original_spatial_indices_;
base::Thread validator_thread_;
mutable base::ConditionVariable validator_cv_;
mutable base::Lock validator_lock_;
size_t num_buffers_validating_ GUARDED_BY(validator_lock_);
bool decode_error_ GUARDED_BY(validator_lock_);
bool waiting_flush_done_ GUARDED_BY(validator_lock_);
SEQUENCE_CHECKER(validator_sequence_checker_);
SEQUENCE_CHECKER(validator_thread_sequence_checker_);
};
}
}
#endif