ivf_video_source.h (3733B)
1 /* 2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved. 3 * 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. 10 */ 11 #ifndef AOM_TEST_IVF_VIDEO_SOURCE_H_ 12 #define AOM_TEST_IVF_VIDEO_SOURCE_H_ 13 14 #include <cstdio> 15 #include <cstdlib> 16 #include <new> 17 #include <string> 18 19 #include "aom_ports/sanitizer.h" 20 #include "test/video_source.h" 21 22 namespace libaom_test { 23 const unsigned int kCodeBufferSize = 256 * 1024 * 1024; 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: 35 explicit IVFVideoSource(const std::string &file_name) 36 : file_name_(file_name), input_file_(nullptr), 37 compressed_frame_buf_(nullptr), frame_sz_(0), frame_(0), 38 end_of_file_(false) {} 39 40 ~IVFVideoSource() override { 41 delete[] compressed_frame_buf_; 42 43 if (input_file_) fclose(input_file_); 44 } 45 46 void Init() override { 47 // Allocate a buffer for read in the compressed video frame. 48 compressed_frame_buf_ = new uint8_t[kCodeBufferSize]; 49 ASSERT_NE(compressed_frame_buf_, nullptr) << "Allocate frame buffer failed"; 50 ASAN_POISON_MEMORY_REGION(compressed_frame_buf_, kCodeBufferSize); 51 } 52 53 void Begin() override { 54 input_file_ = OpenTestDataFile(file_name_); 55 ASSERT_NE(input_file_, nullptr) 56 << "Input file open failed. Filename: " << file_name_; 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 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."; 66 67 FillFrame(); 68 } 69 70 void Next() override { 71 ++frame_; 72 FillFrame(); 73 } 74 75 void FillFrame() { 76 ASSERT_NE(input_file_, nullptr); 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 ASAN_UNPOISON_MEMORY_REGION(compressed_frame_buf_, kCodeBufferSize); 89 ASSERT_EQ(frame_sz_, 90 fread(compressed_frame_buf_, 1, frame_sz_, input_file_)) 91 << "Failed to read complete frame"; 92 ASAN_POISON_MEMORY_REGION(compressed_frame_buf_ + frame_sz_, 93 kCodeBufferSize - frame_sz_); 94 } 95 } 96 97 const uint8_t *cxdata() const override { 98 return end_of_file_ ? nullptr : compressed_frame_buf_; 99 } 100 size_t frame_size() const override { return frame_sz_; } 101 unsigned int frame_number() const override { return frame_; } 102 103 protected: 104 std::string file_name_; 105 FILE *input_file_; 106 uint8_t *compressed_frame_buf_; 107 size_t frame_sz_; 108 unsigned int frame_; 109 bool end_of_file_; 110 }; 111 112 } // namespace libaom_test 113 114 #endif // AOM_TEST_IVF_VIDEO_SOURCE_H_