ivf_video_source.h 3.2 KB

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