2013-09-19 12:14:03 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2013-12-11 16:26:16 +00:00
|
|
|
#include "webrtc/test/frame_generator.h"
|
2013-09-19 12:14:03 +00:00
|
|
|
|
2013-10-15 09:15:47 +00:00
|
|
|
#include <math.h>
|
2013-09-19 12:14:03 +00:00
|
|
|
#include <stdio.h>
|
2013-10-15 09:15:47 +00:00
|
|
|
#include <string.h>
|
2013-09-19 12:14:03 +00:00
|
|
|
|
2015-02-18 12:46:06 +00:00
|
|
|
#include "webrtc/base/checks.h"
|
2013-09-19 12:14:03 +00:00
|
|
|
#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
|
|
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
namespace test {
|
|
|
|
|
namespace {
|
|
|
|
|
|
2013-10-15 09:15:47 +00:00
|
|
|
class ChromaGenerator : public FrameGenerator {
|
|
|
|
|
public:
|
2013-12-11 16:26:16 +00:00
|
|
|
ChromaGenerator(size_t width, size_t height)
|
|
|
|
|
: angle_(0.0), width_(width), height_(height) {
|
2013-10-15 09:15:47 +00:00
|
|
|
assert(width > 0);
|
|
|
|
|
assert(height > 0);
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-11 16:26:16 +00:00
|
|
|
virtual I420VideoFrame* NextFrame() OVERRIDE {
|
|
|
|
|
frame_.CreateEmptyFrame(static_cast<int>(width_),
|
|
|
|
|
static_cast<int>(height_),
|
|
|
|
|
static_cast<int>(width_),
|
|
|
|
|
static_cast<int>((width_ + 1) / 2),
|
|
|
|
|
static_cast<int>((width_ + 1) / 2));
|
2013-10-15 09:15:47 +00:00
|
|
|
angle_ += 30.0;
|
|
|
|
|
uint8_t u = fabs(sin(angle_)) * 0xFF;
|
|
|
|
|
uint8_t v = fabs(cos(angle_)) * 0xFF;
|
|
|
|
|
|
2013-12-11 16:26:16 +00:00
|
|
|
memset(frame_.buffer(kYPlane), 0x80, frame_.allocated_size(kYPlane));
|
2013-10-15 09:15:47 +00:00
|
|
|
memset(frame_.buffer(kUPlane), u, frame_.allocated_size(kUPlane));
|
|
|
|
|
memset(frame_.buffer(kVPlane), v, frame_.allocated_size(kVPlane));
|
2013-12-11 16:26:16 +00:00
|
|
|
return &frame_;
|
2013-10-15 09:15:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
double angle_;
|
2013-12-11 16:26:16 +00:00
|
|
|
size_t width_;
|
|
|
|
|
size_t height_;
|
2013-10-15 09:15:47 +00:00
|
|
|
I420VideoFrame frame_;
|
|
|
|
|
};
|
|
|
|
|
|
2013-09-19 12:14:03 +00:00
|
|
|
class YuvFileGenerator : public FrameGenerator {
|
|
|
|
|
public:
|
2015-02-18 12:46:06 +00:00
|
|
|
YuvFileGenerator(std::vector<FILE*> files,
|
|
|
|
|
size_t width,
|
|
|
|
|
size_t height,
|
|
|
|
|
int frame_repeat_count)
|
|
|
|
|
: file_index_(0),
|
|
|
|
|
files_(files),
|
|
|
|
|
width_(width),
|
|
|
|
|
height_(height),
|
|
|
|
|
frame_size_(CalcBufferSize(kI420,
|
|
|
|
|
static_cast<int>(width_),
|
|
|
|
|
static_cast<int>(height_))),
|
|
|
|
|
frame_buffer_(new uint8_t[frame_size_]),
|
|
|
|
|
frame_display_count_(frame_repeat_count),
|
|
|
|
|
current_display_count_(0) {
|
2013-09-19 12:14:03 +00:00
|
|
|
assert(width > 0);
|
|
|
|
|
assert(height > 0);
|
2015-02-18 12:46:06 +00:00
|
|
|
ReadNextFrame();
|
2013-09-19 12:14:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual ~YuvFileGenerator() {
|
2015-02-18 12:46:06 +00:00
|
|
|
for (FILE* file : files_)
|
|
|
|
|
fclose(file);
|
2013-09-19 12:14:03 +00:00
|
|
|
}
|
|
|
|
|
|
2013-12-11 16:26:16 +00:00
|
|
|
virtual I420VideoFrame* NextFrame() OVERRIDE {
|
2015-02-18 12:46:06 +00:00
|
|
|
if (frame_display_count_ > 0) {
|
|
|
|
|
if (current_display_count_ < frame_display_count_) {
|
|
|
|
|
++current_display_count_;
|
|
|
|
|
} else {
|
|
|
|
|
ReadNextFrame();
|
|
|
|
|
current_display_count_ = 0;
|
|
|
|
|
}
|
2013-09-19 12:14:03 +00:00
|
|
|
}
|
|
|
|
|
|
2015-02-18 12:46:06 +00:00
|
|
|
current_frame_.CopyFrame(last_read_frame_);
|
|
|
|
|
return ¤t_frame_;
|
|
|
|
|
}
|
2013-12-11 16:26:16 +00:00
|
|
|
|
2015-02-18 12:46:06 +00:00
|
|
|
void ReadNextFrame() {
|
|
|
|
|
size_t bytes_read =
|
|
|
|
|
fread(frame_buffer_.get(), 1, frame_size_, files_[file_index_]);
|
|
|
|
|
if (bytes_read < frame_size_) {
|
|
|
|
|
// No more frames to read in this file, rewind and move to next file.
|
|
|
|
|
rewind(files_[file_index_]);
|
|
|
|
|
file_index_ = (file_index_ + 1) % files_.size();
|
|
|
|
|
bytes_read = fread(frame_buffer_.get(), 1, frame_size_,
|
|
|
|
|
files_[file_index_]);
|
|
|
|
|
assert(bytes_read >= frame_size_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
last_read_frame_.CreateEmptyFrame(
|
|
|
|
|
static_cast<int>(width_), static_cast<int>(height_),
|
|
|
|
|
static_cast<int>(width_), static_cast<int>((width_ + 1) / 2),
|
|
|
|
|
static_cast<int>((width_ + 1) / 2));
|
|
|
|
|
|
|
|
|
|
ConvertToI420(kI420, frame_buffer_.get(), 0, 0, static_cast<int>(width_),
|
|
|
|
|
static_cast<int>(height_), 0, kRotateNone, &last_read_frame_);
|
2013-09-19 12:14:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2015-02-18 12:46:06 +00:00
|
|
|
size_t file_index_;
|
|
|
|
|
const std::vector<FILE*> files_;
|
|
|
|
|
const size_t width_;
|
|
|
|
|
const size_t height_;
|
|
|
|
|
const size_t frame_size_;
|
|
|
|
|
const scoped_ptr<uint8_t[]> frame_buffer_;
|
|
|
|
|
const int frame_display_count_;
|
|
|
|
|
int current_display_count_;
|
|
|
|
|
I420VideoFrame current_frame_;
|
|
|
|
|
I420VideoFrame last_read_frame_;
|
2013-09-19 12:14:03 +00:00
|
|
|
};
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2015-02-18 12:46:06 +00:00
|
|
|
FrameGenerator* FrameGenerator::CreateChromaGenerator(size_t width,
|
|
|
|
|
size_t height) {
|
2013-10-15 09:15:47 +00:00
|
|
|
return new ChromaGenerator(width, height);
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-18 12:46:06 +00:00
|
|
|
FrameGenerator* FrameGenerator::CreateFromYuvFile(
|
|
|
|
|
std::vector<std::string> filenames,
|
|
|
|
|
size_t width,
|
|
|
|
|
size_t height,
|
|
|
|
|
int frame_repeat_count) {
|
|
|
|
|
assert(!filenames.empty());
|
|
|
|
|
std::vector<FILE*> files;
|
|
|
|
|
for (const std::string& filename : filenames) {
|
|
|
|
|
FILE* file = fopen(filename.c_str(), "rb");
|
|
|
|
|
DCHECK(file != nullptr);
|
|
|
|
|
files.push_back(file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new YuvFileGenerator(files, width, height, frame_repeat_count);
|
2013-09-19 12:14:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace test
|
|
|
|
|
} // namespace webrtc
|