blob: 4dd76aeea6dedc5bf14f715acc108ebfa4e0a27f [file] [log] [blame]
andresp@webrtc.org28631e72013-09-19 12:14:03 +00001/*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
pbos@webrtc.orgc33d37c2013-12-11 16:26:16 +000010#include "webrtc/test/frame_generator.h"
andresp@webrtc.org28631e72013-09-19 12:14:03 +000011
pbos@webrtc.orge2c52d72013-10-15 09:15:47 +000012#include <math.h>
andresp@webrtc.org28631e72013-09-19 12:14:03 +000013#include <stdio.h>
pbos@webrtc.orge2c52d72013-10-15 09:15:47 +000014#include <string.h>
andresp@webrtc.org28631e72013-09-19 12:14:03 +000015
16#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
17
18namespace webrtc {
19namespace test {
20namespace {
21
pbos@webrtc.orge2c52d72013-10-15 09:15:47 +000022class ChromaGenerator : public FrameGenerator {
23 public:
pbos@webrtc.orgc33d37c2013-12-11 16:26:16 +000024 ChromaGenerator(size_t width, size_t height)
25 : angle_(0.0), width_(width), height_(height) {
pbos@webrtc.orge2c52d72013-10-15 09:15:47 +000026 assert(width > 0);
27 assert(height > 0);
pbos@webrtc.orge2c52d72013-10-15 09:15:47 +000028 }
29
pbos@webrtc.orgc33d37c2013-12-11 16:26:16 +000030 virtual I420VideoFrame* NextFrame() OVERRIDE {
31 frame_.CreateEmptyFrame(static_cast<int>(width_),
32 static_cast<int>(height_),
33 static_cast<int>(width_),
34 static_cast<int>((width_ + 1) / 2),
35 static_cast<int>((width_ + 1) / 2));
pbos@webrtc.orge2c52d72013-10-15 09:15:47 +000036 angle_ += 30.0;
37 uint8_t u = fabs(sin(angle_)) * 0xFF;
38 uint8_t v = fabs(cos(angle_)) * 0xFF;
39
pbos@webrtc.orgc33d37c2013-12-11 16:26:16 +000040 memset(frame_.buffer(kYPlane), 0x80, frame_.allocated_size(kYPlane));
pbos@webrtc.orge2c52d72013-10-15 09:15:47 +000041 memset(frame_.buffer(kUPlane), u, frame_.allocated_size(kUPlane));
42 memset(frame_.buffer(kVPlane), v, frame_.allocated_size(kVPlane));
pbos@webrtc.orgc33d37c2013-12-11 16:26:16 +000043 return &frame_;
pbos@webrtc.orge2c52d72013-10-15 09:15:47 +000044 }
45
46 private:
47 double angle_;
pbos@webrtc.orgc33d37c2013-12-11 16:26:16 +000048 size_t width_;
49 size_t height_;
pbos@webrtc.orge2c52d72013-10-15 09:15:47 +000050 I420VideoFrame frame_;
51};
52
andresp@webrtc.org28631e72013-09-19 12:14:03 +000053class YuvFileGenerator : public FrameGenerator {
54 public:
55 YuvFileGenerator(FILE* file, size_t width, size_t height)
56 : file_(file), width_(width), height_(height) {
57 assert(file);
58 assert(width > 0);
59 assert(height > 0);
60 frame_size_ = CalcBufferSize(
61 kI420, static_cast<int>(width_), static_cast<int>(height_));
62 frame_buffer_ = new uint8_t[frame_size_];
andresp@webrtc.org28631e72013-09-19 12:14:03 +000063 }
64
65 virtual ~YuvFileGenerator() {
66 fclose(file_);
67 delete[] frame_buffer_;
68 }
69
pbos@webrtc.orgc33d37c2013-12-11 16:26:16 +000070 virtual I420VideoFrame* NextFrame() OVERRIDE {
andresp@webrtc.org28631e72013-09-19 12:14:03 +000071 size_t count = fread(frame_buffer_, 1, frame_size_, file_);
72 if (count < frame_size_) {
73 rewind(file_);
74 return NextFrame();
75 }
76
pbos@webrtc.orgc33d37c2013-12-11 16:26:16 +000077 frame_.CreateEmptyFrame(static_cast<int>(width_),
78 static_cast<int>(height_),
79 static_cast<int>(width_),
80 static_cast<int>((width_ + 1) / 2),
81 static_cast<int>((width_ + 1) / 2));
82
andresp@webrtc.org28631e72013-09-19 12:14:03 +000083 ConvertToI420(kI420,
84 frame_buffer_,
85 0,
86 0,
87 static_cast<int>(width_),
88 static_cast<int>(height_),
89 0,
90 kRotateNone,
91 &frame_);
pbos@webrtc.orgc33d37c2013-12-11 16:26:16 +000092 return &frame_;
andresp@webrtc.org28631e72013-09-19 12:14:03 +000093 }
94
95 private:
96 FILE* file_;
97 size_t width_;
98 size_t height_;
99 size_t frame_size_;
100 uint8_t* frame_buffer_;
101 I420VideoFrame frame_;
102};
103} // namespace
104
pbos@webrtc.orge2c52d72013-10-15 09:15:47 +0000105FrameGenerator* FrameGenerator::Create(size_t width, size_t height) {
106 return new ChromaGenerator(width, height);
107}
108
andresp@webrtc.org28631e72013-09-19 12:14:03 +0000109FrameGenerator* FrameGenerator::CreateFromYuvFile(const char* file,
110 size_t width,
111 size_t height) {
pbos@webrtc.orgf2e99be2013-10-16 08:46:06 +0000112 FILE* file_handle = fopen(file, "rb");
andresp@webrtc.org28631e72013-09-19 12:14:03 +0000113 assert(file_handle);
114 return new YuvFileGenerator(file_handle, width, height);
115}
116
117} // namespace test
118} // namespace webrtc