1 /*
2 * Copyright (c) 2012 The WebM 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 #ifndef TEST_IVF_VIDEO_SOURCE_H_
11 #define TEST_IVF_VIDEO_SOURCE_H_
12 #include <cstdio>
13 #include <cstdlib>
14 #include <new>
15 #include <string>
16 #include "test/video_source.h"
17
18 namespace libvpx_test {
19 const unsigned int kCodeBufferSize = 256 * 1024;
20 const unsigned int kIvfFileHdrSize = 32;
21 const unsigned int kIvfFrameHdrSize = 12;
22
MemGetLe32(const uint8_t * mem)23 static unsigned int MemGetLe32(const uint8_t *mem) {
24 return (mem[3] << 24) | (mem[2] << 16) | (mem[1] << 8) | (mem[0]);
25 }
26
27 // This class extends VideoSource to allow parsing of ivf files,
28 // so that we can do actual file decodes.
29 class IVFVideoSource : public CompressedVideoSource {
30 public:
IVFVideoSource(const std::string & file_name)31 explicit IVFVideoSource(const std::string &file_name)
32 : file_name_(file_name),
33 input_file_(NULL),
34 compressed_frame_buf_(NULL),
35 frame_sz_(0),
36 frame_(0),
37 end_of_file_(false) {
38 }
39
~IVFVideoSource()40 virtual ~IVFVideoSource() {
41 delete[] compressed_frame_buf_;
42
43 if (input_file_)
44 fclose(input_file_);
45 }
46
Init()47 virtual void Init() {
48 // Allocate a buffer for read in the compressed video frame.
49 compressed_frame_buf_ = new uint8_t[libvpx_test::kCodeBufferSize];
50 ASSERT_TRUE(compressed_frame_buf_ != NULL)
51 << "Allocate frame buffer failed";
52 }
53
Begin()54 virtual void Begin() {
55 input_file_ = OpenTestDataFile(file_name_);
56 ASSERT_TRUE(input_file_ != NULL) << "Input file open failed. Filename: "
57 << file_name_;
58
59 // Read file header
60 uint8_t file_hdr[kIvfFileHdrSize];
61 ASSERT_EQ(kIvfFileHdrSize, fread(file_hdr, 1, kIvfFileHdrSize, input_file_))
62 << "File header read failed.";
63 // Check file header
64 ASSERT_TRUE(file_hdr[0] == 'D' && file_hdr[1] == 'K' && file_hdr[2] == 'I'
65 && file_hdr[3] == 'F') << "Input is not an IVF file.";
66
67 FillFrame();
68 }
69
Next()70 virtual void Next() {
71 ++frame_;
72 FillFrame();
73 }
74
FillFrame()75 void FillFrame() {
76 ASSERT_TRUE(input_file_ != NULL);
77 uint8_t frame_hdr[kIvfFrameHdrSize];
78 // Check frame header and read a frame from input_file.
79 if (fread(frame_hdr, 1, kIvfFrameHdrSize, input_file_)
80 != kIvfFrameHdrSize) {
81 end_of_file_ = true;
82 } else {
83 end_of_file_ = false;
84
85 frame_sz_ = MemGetLe32(frame_hdr);
86 ASSERT_LE(frame_sz_, kCodeBufferSize)
87 << "Frame is too big for allocated code buffer";
88 ASSERT_EQ(frame_sz_,
89 fread(compressed_frame_buf_, 1, frame_sz_, input_file_))
90 << "Failed to read complete frame";
91 }
92 }
93
cxdata()94 virtual const uint8_t *cxdata() const {
95 return end_of_file_ ? NULL : compressed_frame_buf_;
96 }
frame_size()97 virtual size_t frame_size() const { return frame_sz_; }
frame_number()98 virtual unsigned int frame_number() const { return frame_; }
99
100 protected:
101 std::string file_name_;
102 FILE *input_file_;
103 uint8_t *compressed_frame_buf_;
104 size_t frame_sz_;
105 unsigned int frame_;
106 bool end_of_file_;
107 };
108
109 } // namespace libvpx_test
110
111 #endif // TEST_IVF_VIDEO_SOURCE_H_
112