yuv_video_source.h 4.05 KB
Newer Older
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3
 *
4 5 6 7 8 9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 11 12 13 14 15 16 17 18
 */
#ifndef TEST_YUV_VIDEO_SOURCE_H_
#define TEST_YUV_VIDEO_SOURCE_H_

#include <cstdio>
#include <cstdlib>
#include <string>

#include "test/video_source.h"
19
#include "aom/aom_image.h"
20

21
namespace libaom_test {
22 23 24 25 26 27

// This class extends VideoSource to allow parsing of raw YUV
// formats of various color sampling and bit-depths so that we can
// do actual file encodes.
class YUVVideoSource : public VideoSource {
 public:
28
  YUVVideoSource(const std::string &file_name, aom_img_fmt format,
clang-format's avatar
clang-format committed
29 30 31 32
                 unsigned int width, unsigned int height, int rate_numerator,
                 int rate_denominator, unsigned int start, int limit)
      : file_name_(file_name), input_file_(NULL), img_(NULL), start_(start),
        limit_(limit), frame_(0), width_(0), height_(0),
33
        format_(AOM_IMG_FMT_NONE), framerate_numerator_(rate_numerator),
34 35 36 37 38 39
        framerate_denominator_(rate_denominator) {
    // This initializes format_, raw_size_, width_, height_ and allocates img.
    SetSize(width, height, format);
  }

  virtual ~YUVVideoSource() {
40
    aom_img_free(img_);
clang-format's avatar
clang-format committed
41
    if (input_file_) fclose(input_file_);
42 43 44
  }

  virtual void Begin() {
clang-format's avatar
clang-format committed
45
    if (input_file_) fclose(input_file_);
46
    input_file_ = OpenTestDataFile(file_name_);
clang-format's avatar
clang-format committed
47 48
    ASSERT_TRUE(input_file_ != NULL)
        << "Input file open failed. Filename: " << file_name_;
49 50 51 52 53 54 55 56 57 58 59 60
    if (start_)
      fseek(input_file_, static_cast<unsigned>(raw_size_) * start_, SEEK_SET);

    frame_ = start_;
    FillFrame();
  }

  virtual void Next() {
    ++frame_;
    FillFrame();
  }

61
  virtual aom_image_t *img() const { return (frame_ < limit_) ? img_ : NULL; }
62 63

  // Models a stream where Timebase = 1/FPS, so pts == frame.
64
  virtual aom_codec_pts_t pts() const { return frame_; }
65 66 67

  virtual unsigned long duration() const { return 1; }

68 69
  virtual aom_rational_t timebase() const {
    const aom_rational_t t = { framerate_denominator_, framerate_numerator_ };
70 71 72 73 74 75 76 77
    return t;
  }

  virtual unsigned int frame() const { return frame_; }

  virtual unsigned int limit() const { return limit_; }

  virtual void SetSize(unsigned int width, unsigned int height,
78
                       aom_img_fmt format) {
79
    if (width != width_ || height != height_ || format != format_) {
80 81
      aom_img_free(img_);
      img_ = aom_img_alloc(NULL, format, width, height, 1);
82 83 84 85 86
      ASSERT_TRUE(img_ != NULL);
      width_ = width;
      height_ = height;
      format_ = format;
      switch (format) {
87 88 89 90 91 92 93 94
        case AOM_IMG_FMT_I420: raw_size_ = width * height * 3 / 2; break;
        case AOM_IMG_FMT_I422: raw_size_ = width * height * 2; break;
        case AOM_IMG_FMT_I440: raw_size_ = width * height * 2; break;
        case AOM_IMG_FMT_I444: raw_size_ = width * height * 3; break;
        case AOM_IMG_FMT_I42016: raw_size_ = width * height * 3; break;
        case AOM_IMG_FMT_I42216: raw_size_ = width * height * 4; break;
        case AOM_IMG_FMT_I44016: raw_size_ = width * height * 4; break;
        case AOM_IMG_FMT_I44416: raw_size_ = width * height * 6; break;
clang-format's avatar
clang-format committed
95
        default: ASSERT_TRUE(0);
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
      }
    }
  }

  virtual void FillFrame() {
    ASSERT_TRUE(input_file_ != NULL);
    // Read a frame from input_file.
    if (fread(img_->img_data, raw_size_, 1, input_file_) == 0) {
      limit_ = frame_;
    }
  }

 protected:
  std::string file_name_;
  FILE *input_file_;
111
  aom_image_t *img_;
112 113 114 115 116 117
  size_t raw_size_;
  unsigned int start_;
  unsigned int limit_;
  unsigned int frame_;
  unsigned int width_;
  unsigned int height_;
118
  aom_img_fmt format_;
119 120 121 122
  int framerate_numerator_;
  int framerate_denominator_;
};

123
}  // namespace libaom_test
124 125

#endif  // TEST_YUV_VIDEO_SOURCE_H_