blob: 0d3e9f9cb4248559e7ffc389668415d9b5a1ce34 [file] [log] [blame]
Yunqing Wang15dffcf2012-10-04 12:59:36 -07001/*
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yunqing Wang15dffcf2012-10-04 12:59:36 -07003 *
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Yunqing Wang15dffcf2012-10-04 12:59:36 -070010 */
11#ifndef TEST_IVF_VIDEO_SOURCE_H_
12#define TEST_IVF_VIDEO_SOURCE_H_
13#include <cstdio>
14#include <cstdlib>
15#include <new>
16#include <string>
17#include "test/video_source.h"
18
Yaowu Xuc27fc142016-08-22 16:08:15 -070019namespace libaom_test {
Yunqing Wang15dffcf2012-10-04 12:59:36 -070020const unsigned int kCodeBufferSize = 256 * 1024;
21const unsigned int kIvfFileHdrSize = 32;
22const unsigned int kIvfFrameHdrSize = 12;
23
24static unsigned int MemGetLe32(const uint8_t *mem) {
25 return (mem[3] << 24) | (mem[2] << 16) | (mem[1] << 8) | (mem[0]);
26}
27
28// This class extends VideoSource to allow parsing of ivf files,
29// so that we can do actual file decodes.
30class IVFVideoSource : public CompressedVideoSource {
31 public:
Yaowu Xuafffa3d2013-09-05 08:45:56 -070032 explicit IVFVideoSource(const std::string &file_name)
clang-format3a826f12016-08-11 17:46:05 -070033 : file_name_(file_name), input_file_(NULL), compressed_frame_buf_(NULL),
34 frame_sz_(0), frame_(0), end_of_file_(false) {}
Yunqing Wang15dffcf2012-10-04 12:59:36 -070035
36 virtual ~IVFVideoSource() {
37 delete[] compressed_frame_buf_;
38
clang-format3a826f12016-08-11 17:46:05 -070039 if (input_file_) fclose(input_file_);
Yunqing Wang15dffcf2012-10-04 12:59:36 -070040 }
41
42 virtual void Init() {
43 // Allocate a buffer for read in the compressed video frame.
Yaowu Xuc27fc142016-08-22 16:08:15 -070044 compressed_frame_buf_ = new uint8_t[libaom_test::kCodeBufferSize];
James Zern104dbbb2013-07-18 16:13:39 -070045 ASSERT_TRUE(compressed_frame_buf_ != NULL)
46 << "Allocate frame buffer failed";
Yunqing Wang15dffcf2012-10-04 12:59:36 -070047 }
48
49 virtual void Begin() {
50 input_file_ = OpenTestDataFile(file_name_);
James Zern66c7dff2013-06-25 17:55:28 -070051 ASSERT_TRUE(input_file_ != NULL) << "Input file open failed. Filename: "
clang-format3a826f12016-08-11 17:46:05 -070052 << file_name_;
Yunqing Wang15dffcf2012-10-04 12:59:36 -070053
54 // Read file header
55 uint8_t file_hdr[kIvfFileHdrSize];
56 ASSERT_EQ(kIvfFileHdrSize, fread(file_hdr, 1, kIvfFileHdrSize, input_file_))
57 << "File header read failed.";
58 // Check file header
clang-format3a826f12016-08-11 17:46:05 -070059 ASSERT_TRUE(file_hdr[0] == 'D' && file_hdr[1] == 'K' &&
60 file_hdr[2] == 'I' && file_hdr[3] == 'F')
61 << "Input is not an IVF file.";
Yunqing Wang15dffcf2012-10-04 12:59:36 -070062
63 FillFrame();
64 }
65
66 virtual void Next() {
67 ++frame_;
68 FillFrame();
69 }
70
71 void FillFrame() {
James Zern66c7dff2013-06-25 17:55:28 -070072 ASSERT_TRUE(input_file_ != NULL);
Yunqing Wang15dffcf2012-10-04 12:59:36 -070073 uint8_t frame_hdr[kIvfFrameHdrSize];
74 // Check frame header and read a frame from input_file.
clang-format3a826f12016-08-11 17:46:05 -070075 if (fread(frame_hdr, 1, kIvfFrameHdrSize, input_file_) !=
76 kIvfFrameHdrSize) {
Yunqing Wang15dffcf2012-10-04 12:59:36 -070077 end_of_file_ = true;
78 } else {
79 end_of_file_ = false;
80
81 frame_sz_ = MemGetLe32(frame_hdr);
82 ASSERT_LE(frame_sz_, kCodeBufferSize)
83 << "Frame is too big for allocated code buffer";
84 ASSERT_EQ(frame_sz_,
85 fread(compressed_frame_buf_, 1, frame_sz_, input_file_))
86 << "Failed to read complete frame";
87 }
88 }
89
90 virtual const uint8_t *cxdata() const {
91 return end_of_file_ ? NULL : compressed_frame_buf_;
92 }
Tom Fineganeb2325e2014-02-19 14:17:55 -080093 virtual size_t frame_size() const { return frame_sz_; }
94 virtual unsigned int frame_number() const { return frame_; }
Yunqing Wang15dffcf2012-10-04 12:59:36 -070095
96 protected:
97 std::string file_name_;
98 FILE *input_file_;
99 uint8_t *compressed_frame_buf_;
Tom Fineganeb2325e2014-02-19 14:17:55 -0800100 size_t frame_sz_;
Yunqing Wang15dffcf2012-10-04 12:59:36 -0700101 unsigned int frame_;
102 bool end_of_file_;
103};
104
Yaowu Xuc27fc142016-08-22 16:08:15 -0700105} // namespace libaom_test
Yunqing Wang15dffcf2012-10-04 12:59:36 -0700106
107#endif // TEST_IVF_VIDEO_SOURCE_H_