blob: 45828b5bb18d54a03564ce045c5ac39c1b2ab5e4 [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 */
James Zerne1cbb132018-08-22 14:10:36 -070011#ifndef AOM_TEST_IVF_VIDEO_SOURCE_H_
12#define AOM_TEST_IVF_VIDEO_SOURCE_H_
Wan-Teh Chang01ee6ee2018-05-14 12:40:48 -070013
Yunqing Wang15dffcf2012-10-04 12:59:36 -070014#include <cstdio>
15#include <cstdlib>
16#include <new>
17#include <string>
Wan-Teh Chang01ee6ee2018-05-14 12:40:48 -070018
19#include "aom_ports/sanitizer.h"
Yunqing Wang15dffcf2012-10-04 12:59:36 -070020#include "test/video_source.h"
21
Yaowu Xuc27fc142016-08-22 16:08:15 -070022namespace libaom_test {
Wan-Teh Chang01ee6ee2018-05-14 12:40:48 -070023const unsigned int kCodeBufferSize = 256 * 1024 * 1024;
Yunqing Wang15dffcf2012-10-04 12:59:36 -070024const unsigned int kIvfFileHdrSize = 32;
25const unsigned int kIvfFrameHdrSize = 12;
26
27static unsigned int MemGetLe32(const uint8_t *mem) {
28 return (mem[3] << 24) | (mem[2] << 16) | (mem[1] << 8) | (mem[0]);
29}
30
31// This class extends VideoSource to allow parsing of ivf files,
32// so that we can do actual file decodes.
33class IVFVideoSource : public CompressedVideoSource {
34 public:
Yaowu Xuafffa3d2013-09-05 08:45:56 -070035 explicit IVFVideoSource(const std::string &file_name)
James Zern664f04d2022-05-24 17:30:58 -070036 : file_name_(file_name), input_file_(nullptr),
37 compressed_frame_buf_(nullptr), frame_sz_(0), frame_(0),
38 end_of_file_(false) {}
Yunqing Wang15dffcf2012-10-04 12:59:36 -070039
40 virtual ~IVFVideoSource() {
41 delete[] compressed_frame_buf_;
42
clang-format3a826f12016-08-11 17:46:05 -070043 if (input_file_) fclose(input_file_);
Yunqing Wang15dffcf2012-10-04 12:59:36 -070044 }
45
46 virtual void Init() {
47 // Allocate a buffer for read in the compressed video frame.
Wan-Teh Chang01ee6ee2018-05-14 12:40:48 -070048 compressed_frame_buf_ = new uint8_t[kCodeBufferSize];
James Zern9dea04e2022-04-28 13:18:36 -070049 ASSERT_NE(compressed_frame_buf_, nullptr) << "Allocate frame buffer failed";
Wan-Teh Chang01ee6ee2018-05-14 12:40:48 -070050 ASAN_POISON_MEMORY_REGION(compressed_frame_buf_, kCodeBufferSize);
Yunqing Wang15dffcf2012-10-04 12:59:36 -070051 }
52
53 virtual void Begin() {
54 input_file_ = OpenTestDataFile(file_name_);
James Zern9dea04e2022-04-28 13:18:36 -070055 ASSERT_NE(input_file_, nullptr)
clang-format4eafefe2017-09-04 12:51:20 -070056 << "Input file open failed. Filename: " << file_name_;
Yunqing Wang15dffcf2012-10-04 12:59:36 -070057
58 // Read file header
59 uint8_t file_hdr[kIvfFileHdrSize];
60 ASSERT_EQ(kIvfFileHdrSize, fread(file_hdr, 1, kIvfFileHdrSize, input_file_))
61 << "File header read failed.";
62 // Check file header
clang-format3a826f12016-08-11 17:46:05 -070063 ASSERT_TRUE(file_hdr[0] == 'D' && file_hdr[1] == 'K' &&
64 file_hdr[2] == 'I' && file_hdr[3] == 'F')
65 << "Input is not an IVF file.";
Yunqing Wang15dffcf2012-10-04 12:59:36 -070066
67 FillFrame();
68 }
69
70 virtual void Next() {
71 ++frame_;
72 FillFrame();
73 }
74
75 void FillFrame() {
James Zern9dea04e2022-04-28 13:18:36 -070076 ASSERT_NE(input_file_, nullptr);
Yunqing Wang15dffcf2012-10-04 12:59:36 -070077 uint8_t frame_hdr[kIvfFrameHdrSize];
78 // Check frame header and read a frame from input_file.
clang-format3a826f12016-08-11 17:46:05 -070079 if (fread(frame_hdr, 1, kIvfFrameHdrSize, input_file_) !=
80 kIvfFrameHdrSize) {
Yunqing Wang15dffcf2012-10-04 12:59:36 -070081 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";
Wan-Teh Chang01ee6ee2018-05-14 12:40:48 -070088 ASAN_UNPOISON_MEMORY_REGION(compressed_frame_buf_, kCodeBufferSize);
Yunqing Wang15dffcf2012-10-04 12:59:36 -070089 ASSERT_EQ(frame_sz_,
90 fread(compressed_frame_buf_, 1, frame_sz_, input_file_))
91 << "Failed to read complete frame";
Wan-Teh Chang01ee6ee2018-05-14 12:40:48 -070092 ASAN_POISON_MEMORY_REGION(compressed_frame_buf_ + frame_sz_,
93 kCodeBufferSize - frame_sz_);
Yunqing Wang15dffcf2012-10-04 12:59:36 -070094 }
95 }
96
97 virtual const uint8_t *cxdata() const {
James Zern664f04d2022-05-24 17:30:58 -070098 return end_of_file_ ? nullptr : compressed_frame_buf_;
Yunqing Wang15dffcf2012-10-04 12:59:36 -070099 }
Tom Fineganeb2325e2014-02-19 14:17:55 -0800100 virtual size_t frame_size() const { return frame_sz_; }
101 virtual unsigned int frame_number() const { return frame_; }
Yunqing Wang15dffcf2012-10-04 12:59:36 -0700102
103 protected:
104 std::string file_name_;
105 FILE *input_file_;
106 uint8_t *compressed_frame_buf_;
Tom Fineganeb2325e2014-02-19 14:17:55 -0800107 size_t frame_sz_;
Yunqing Wang15dffcf2012-10-04 12:59:36 -0700108 unsigned int frame_;
109 bool end_of_file_;
110};
111
Yaowu Xuc27fc142016-08-22 16:08:15 -0700112} // namespace libaom_test
Yunqing Wang15dffcf2012-10-04 12:59:36 -0700113
James Zerne1cbb132018-08-22 14:10:36 -0700114#endif // AOM_TEST_IVF_VIDEO_SOURCE_H_