aboutsummaryrefslogtreecommitdiff
path: root/webrtc/test/frame_generator.cc
blob: 4dd76aeea6dedc5bf14f715acc108ebfa4e0a27f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 *  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.
 */
#include "webrtc/test/frame_generator.h"

#include <math.h>
#include <stdio.h>
#include <string.h>

#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"

namespace webrtc {
namespace test {
namespace {

class ChromaGenerator : public FrameGenerator {
 public:
  ChromaGenerator(size_t width, size_t height)
      : angle_(0.0), width_(width), height_(height) {
    assert(width > 0);
    assert(height > 0);
  }

  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));
    angle_ += 30.0;
    uint8_t u = fabs(sin(angle_)) * 0xFF;
    uint8_t v = fabs(cos(angle_)) * 0xFF;

    memset(frame_.buffer(kYPlane), 0x80, frame_.allocated_size(kYPlane));
    memset(frame_.buffer(kUPlane), u, frame_.allocated_size(kUPlane));
    memset(frame_.buffer(kVPlane), v, frame_.allocated_size(kVPlane));
    return &frame_;
  }

 private:
  double angle_;
  size_t width_;
  size_t height_;
  I420VideoFrame frame_;
};

class YuvFileGenerator : public FrameGenerator {
 public:
  YuvFileGenerator(FILE* file, size_t width, size_t height)
      : file_(file), width_(width), height_(height) {
    assert(file);
    assert(width > 0);
    assert(height > 0);
    frame_size_ = CalcBufferSize(
        kI420, static_cast<int>(width_), static_cast<int>(height_));
    frame_buffer_ = new uint8_t[frame_size_];
  }

  virtual ~YuvFileGenerator() {
    fclose(file_);
    delete[] frame_buffer_;
  }

  virtual I420VideoFrame* NextFrame() OVERRIDE {
    size_t count = fread(frame_buffer_, 1, frame_size_, file_);
    if (count < frame_size_) {
      rewind(file_);
      return NextFrame();
    }

    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_,
                  0,
                  0,
                  static_cast<int>(width_),
                  static_cast<int>(height_),
                  0,
                  kRotateNone,
                  &frame_);
    return &frame_;
  }

 private:
  FILE* file_;
  size_t width_;
  size_t height_;
  size_t frame_size_;
  uint8_t* frame_buffer_;
  I420VideoFrame frame_;
};
}  // namespace

FrameGenerator* FrameGenerator::Create(size_t width, size_t height) {
  return new ChromaGenerator(width, height);
}

FrameGenerator* FrameGenerator::CreateFromYuvFile(const char* file,
                                                  size_t width,
                                                  size_t height) {
  FILE* file_handle = fopen(file, "rb");
  assert(file_handle);
  return new YuvFileGenerator(file_handle, width, height);
}

}  // namespace test
}  // namespace webrtc