blob: 2f93d7ab61b84d541704db9001bcfae68e6a875e [file] [log] [blame]
andresp@webrtc.orgab654952013-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 */
10#include "webrtc/common_video/test/frame_generator.h"
11
12#include <stdio.h>
13
14#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
15
16namespace webrtc {
17namespace test {
18namespace {
19
20class YuvFileGenerator : public FrameGenerator {
21 public:
22 YuvFileGenerator(FILE* file, size_t width, size_t height)
23 : file_(file), width_(width), height_(height) {
24 assert(file);
25 assert(width > 0);
26 assert(height > 0);
27 frame_size_ = CalcBufferSize(
28 kI420, static_cast<int>(width_), static_cast<int>(height_));
29 frame_buffer_ = new uint8_t[frame_size_];
30 frame_.CreateEmptyFrame(static_cast<int>(width),
31 static_cast<int>(height),
32 static_cast<int>(width),
33 static_cast<int>((width + 1) / 2),
34 static_cast<int>((width + 1) / 2));
35 }
36
37 virtual ~YuvFileGenerator() {
38 fclose(file_);
39 delete[] frame_buffer_;
40 }
41
42 virtual I420VideoFrame& NextFrame() OVERRIDE {
43 size_t count = fread(frame_buffer_, 1, frame_size_, file_);
44 if (count < frame_size_) {
45 rewind(file_);
46 return NextFrame();
47 }
48
49 ConvertToI420(kI420,
50 frame_buffer_,
51 0,
52 0,
53 static_cast<int>(width_),
54 static_cast<int>(height_),
55 0,
56 kRotateNone,
57 &frame_);
58 return frame_;
59 }
60
61 private:
62 FILE* file_;
63 size_t width_;
64 size_t height_;
65 size_t frame_size_;
66 uint8_t* frame_buffer_;
67 I420VideoFrame frame_;
68};
69} // namespace
70
71FrameGenerator* FrameGenerator::CreateFromYuvFile(const char* file,
72 size_t width,
73 size_t height) {
74 FILE* file_handle = fopen(file, "r");
75 assert(file_handle);
76 return new YuvFileGenerator(file_handle, width, height);
77}
78
79} // namespace test
80} // namespace webrtc