Yunqing Wang | 15dffcf | 2012-10-04 12:59:36 -0700 | [diff] [blame] | 1 | /* |
Yaowu Xu | 2ab7ff0 | 2016-09-02 12:04:54 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
Yunqing Wang | 15dffcf | 2012-10-04 12:59:36 -0700 | [diff] [blame] | 3 | * |
Yaowu Xu | 2ab7ff0 | 2016-09-02 12:04:54 -0700 | [diff] [blame] | 4 | * 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 Wang | 15dffcf | 2012-10-04 12:59:36 -0700 | [diff] [blame] | 10 | */ |
James Zern | e1cbb13 | 2018-08-22 14:10:36 -0700 | [diff] [blame] | 11 | #ifndef AOM_TEST_IVF_VIDEO_SOURCE_H_ |
| 12 | #define AOM_TEST_IVF_VIDEO_SOURCE_H_ |
Wan-Teh Chang | 01ee6ee | 2018-05-14 12:40:48 -0700 | [diff] [blame] | 13 | |
Yunqing Wang | 15dffcf | 2012-10-04 12:59:36 -0700 | [diff] [blame] | 14 | #include <cstdio> |
| 15 | #include <cstdlib> |
| 16 | #include <new> |
| 17 | #include <string> |
Wan-Teh Chang | 01ee6ee | 2018-05-14 12:40:48 -0700 | [diff] [blame] | 18 | |
| 19 | #include "aom_ports/sanitizer.h" |
Yunqing Wang | 15dffcf | 2012-10-04 12:59:36 -0700 | [diff] [blame] | 20 | #include "test/video_source.h" |
| 21 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 22 | namespace libaom_test { |
Wan-Teh Chang | 01ee6ee | 2018-05-14 12:40:48 -0700 | [diff] [blame] | 23 | const unsigned int kCodeBufferSize = 256 * 1024 * 1024; |
Yunqing Wang | 15dffcf | 2012-10-04 12:59:36 -0700 | [diff] [blame] | 24 | const unsigned int kIvfFileHdrSize = 32; |
| 25 | const unsigned int kIvfFrameHdrSize = 12; |
| 26 | |
| 27 | static 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. |
| 33 | class IVFVideoSource : public CompressedVideoSource { |
| 34 | public: |
Yaowu Xu | afffa3d | 2013-09-05 08:45:56 -0700 | [diff] [blame] | 35 | explicit IVFVideoSource(const std::string &file_name) |
James Zern | 664f04d | 2022-05-24 17:30:58 -0700 | [diff] [blame] | 36 | : file_name_(file_name), input_file_(nullptr), |
| 37 | compressed_frame_buf_(nullptr), frame_sz_(0), frame_(0), |
| 38 | end_of_file_(false) {} |
Yunqing Wang | 15dffcf | 2012-10-04 12:59:36 -0700 | [diff] [blame] | 39 | |
| 40 | virtual ~IVFVideoSource() { |
| 41 | delete[] compressed_frame_buf_; |
| 42 | |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 43 | if (input_file_) fclose(input_file_); |
Yunqing Wang | 15dffcf | 2012-10-04 12:59:36 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | virtual void Init() { |
| 47 | // Allocate a buffer for read in the compressed video frame. |
Wan-Teh Chang | 01ee6ee | 2018-05-14 12:40:48 -0700 | [diff] [blame] | 48 | compressed_frame_buf_ = new uint8_t[kCodeBufferSize]; |
James Zern | 9dea04e | 2022-04-28 13:18:36 -0700 | [diff] [blame] | 49 | ASSERT_NE(compressed_frame_buf_, nullptr) << "Allocate frame buffer failed"; |
Wan-Teh Chang | 01ee6ee | 2018-05-14 12:40:48 -0700 | [diff] [blame] | 50 | ASAN_POISON_MEMORY_REGION(compressed_frame_buf_, kCodeBufferSize); |
Yunqing Wang | 15dffcf | 2012-10-04 12:59:36 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | virtual void Begin() { |
| 54 | input_file_ = OpenTestDataFile(file_name_); |
James Zern | 9dea04e | 2022-04-28 13:18:36 -0700 | [diff] [blame] | 55 | ASSERT_NE(input_file_, nullptr) |
clang-format | 4eafefe | 2017-09-04 12:51:20 -0700 | [diff] [blame] | 56 | << "Input file open failed. Filename: " << file_name_; |
Yunqing Wang | 15dffcf | 2012-10-04 12:59:36 -0700 | [diff] [blame] | 57 | |
| 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-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 63 | 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 Wang | 15dffcf | 2012-10-04 12:59:36 -0700 | [diff] [blame] | 66 | |
| 67 | FillFrame(); |
| 68 | } |
| 69 | |
| 70 | virtual void Next() { |
| 71 | ++frame_; |
| 72 | FillFrame(); |
| 73 | } |
| 74 | |
| 75 | void FillFrame() { |
James Zern | 9dea04e | 2022-04-28 13:18:36 -0700 | [diff] [blame] | 76 | ASSERT_NE(input_file_, nullptr); |
Yunqing Wang | 15dffcf | 2012-10-04 12:59:36 -0700 | [diff] [blame] | 77 | uint8_t frame_hdr[kIvfFrameHdrSize]; |
| 78 | // Check frame header and read a frame from input_file. |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame] | 79 | if (fread(frame_hdr, 1, kIvfFrameHdrSize, input_file_) != |
| 80 | kIvfFrameHdrSize) { |
Yunqing Wang | 15dffcf | 2012-10-04 12:59:36 -0700 | [diff] [blame] | 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"; |
Wan-Teh Chang | 01ee6ee | 2018-05-14 12:40:48 -0700 | [diff] [blame] | 88 | ASAN_UNPOISON_MEMORY_REGION(compressed_frame_buf_, kCodeBufferSize); |
Yunqing Wang | 15dffcf | 2012-10-04 12:59:36 -0700 | [diff] [blame] | 89 | ASSERT_EQ(frame_sz_, |
| 90 | fread(compressed_frame_buf_, 1, frame_sz_, input_file_)) |
| 91 | << "Failed to read complete frame"; |
Wan-Teh Chang | 01ee6ee | 2018-05-14 12:40:48 -0700 | [diff] [blame] | 92 | ASAN_POISON_MEMORY_REGION(compressed_frame_buf_ + frame_sz_, |
| 93 | kCodeBufferSize - frame_sz_); |
Yunqing Wang | 15dffcf | 2012-10-04 12:59:36 -0700 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
| 97 | virtual const uint8_t *cxdata() const { |
James Zern | 664f04d | 2022-05-24 17:30:58 -0700 | [diff] [blame] | 98 | return end_of_file_ ? nullptr : compressed_frame_buf_; |
Yunqing Wang | 15dffcf | 2012-10-04 12:59:36 -0700 | [diff] [blame] | 99 | } |
Tom Finegan | eb2325e | 2014-02-19 14:17:55 -0800 | [diff] [blame] | 100 | virtual size_t frame_size() const { return frame_sz_; } |
| 101 | virtual unsigned int frame_number() const { return frame_; } |
Yunqing Wang | 15dffcf | 2012-10-04 12:59:36 -0700 | [diff] [blame] | 102 | |
| 103 | protected: |
| 104 | std::string file_name_; |
| 105 | FILE *input_file_; |
| 106 | uint8_t *compressed_frame_buf_; |
Tom Finegan | eb2325e | 2014-02-19 14:17:55 -0800 | [diff] [blame] | 107 | size_t frame_sz_; |
Yunqing Wang | 15dffcf | 2012-10-04 12:59:36 -0700 | [diff] [blame] | 108 | unsigned int frame_; |
| 109 | bool end_of_file_; |
| 110 | }; |
| 111 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 112 | } // namespace libaom_test |
Yunqing Wang | 15dffcf | 2012-10-04 12:59:36 -0700 | [diff] [blame] | 113 | |
James Zern | e1cbb13 | 2018-08-22 14:10:36 -0700 | [diff] [blame] | 114 | #endif // AOM_TEST_IVF_VIDEO_SOURCE_H_ |