blob: 7856476ca073b177e59c07e434a591dc2c124eaf [file] [log] [blame]
kjellander@webrtc.org5b97b122011-12-08 07:42:181/*
2 * Copyright (c) 2011 The WebRTC 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
Mirko Bonadei92ea95e2017-09-15 04:47:3111#ifndef TEST_TESTSUPPORT_FRAME_READER_H_
12#define TEST_TESTSUPPORT_FRAME_READER_H_
kjellander@webrtc.org5b97b122011-12-08 07:42:1813
pbos@webrtc.org12dc1a32013-08-05 16:22:5314#include <stdio.h>
15
kjellander@webrtc.org5b97b122011-12-08 07:42:1816#include <string>
17
Erik Språngebe5acb2020-12-03 15:18:4418#include "absl/types/optional.h"
Mirko Bonadeid9708072019-01-25 19:26:4819#include "api/scoped_refptr.h"
Sergey Silkin1985b5a2022-12-06 14:36:2120#include "api/video/resolution.h"
kjellander@webrtc.org5b97b122011-12-08 07:42:1821
22namespace webrtc {
nisse115bd152016-09-30 11:14:0723class I420Buffer;
kjellander@webrtc.org5b97b122011-12-08 07:42:1824namespace test {
25
nisse115bd152016-09-30 11:14:0726// Handles reading of I420 frames from video files.
kjellander@webrtc.org5b97b122011-12-08 07:42:1827class FrameReader {
28 public:
Sergey Silkin1985b5a2022-12-06 14:36:2129 struct Ratio {
30 int num = 1;
31 int den = 1;
32 };
33
34 static constexpr Ratio kNoScale = Ratio({.num = 1, .den = 1});
35
kjellander@webrtc.org5b97b122011-12-08 07:42:1836 virtual ~FrameReader() {}
37
Sergey Silkin1985b5a2022-12-06 14:36:2138 // Reads and returns next frame. Returns `nullptr` if reading failed or end of
39 // stream is reached.
40 virtual rtc::scoped_refptr<I420Buffer> PullFrame() = 0;
kjellander@webrtc.org5b97b122011-12-08 07:42:1841
Sergey Silkin1985b5a2022-12-06 14:36:2142 // Reads and returns next frame. `frame_num` stores unwrapped frame number
43 // which can be passed to `ReadFrame` to re-read this frame later. Returns
44 // `nullptr` if reading failed or end of stream is reached.
45 virtual rtc::scoped_refptr<I420Buffer> PullFrame(int* frame_num) = 0;
kjellander@webrtc.org5b97b122011-12-08 07:42:1846
Sergey Silkin1985b5a2022-12-06 14:36:2147 // Reads and returns frame specified by `frame_num`. Returns `nullptr` if
48 // reading failed.
49 virtual rtc::scoped_refptr<I420Buffer> ReadFrame(int frame_num) = 0;
kjellander@webrtc.org5b97b122011-12-08 07:42:1850
Sergey Silkin1985b5a2022-12-06 14:36:2151 // Reads next frame, resizes and returns it. `frame_num` stores unwrapped
52 // frame number which can be passed to `ReadFrame` to re-read this frame
53 // later. `resolution` specifies resolution of the returned frame.
54 // `framerate_scale` specifies frame rate scale factor. Frame rate scaling is
55 // done by skipping or repeating frames.
56 virtual rtc::scoped_refptr<I420Buffer> PullFrame(int* frame_num,
57 Resolution resolution,
58 Ratio framerate_scale) = 0;
59
60 // Reads frame specified by `frame_num`, resizes and returns it. Returns
61 // `nullptr` if reading failed.
62 virtual rtc::scoped_refptr<I420Buffer> ReadFrame(int frame_num,
63 Resolution resolution) = 0;
64
65 // Total number of retrievable frames.
66 virtual int num_frames() const = 0;
kjellander@webrtc.org5b97b122011-12-08 07:42:1867};
68
brandtrb78bc752017-02-22 09:26:5969class YuvFrameReaderImpl : public FrameReader {
kjellander@webrtc.org5b97b122011-12-08 07:42:1870 public:
Erik Språngebe5acb2020-12-03 15:18:4471 enum class RepeatMode { kSingle, kRepeat, kPingPong };
Erik Språngebe5acb2020-12-03 15:18:4472
Sergey Silkin1985b5a2022-12-06 14:36:2173 // Creates the frame reader for a YUV file specified by `filepath`.
74 // `resolution` specifies width and height of frames in pixels. `repeat_mode`
75 // specifies behaviour of the reader at reaching the end of file (stop, read
76 // it over from the beginning or read in reverse order). The file is assumed
77 // to exist, be readable and to contain at least 1 frame.
78 YuvFrameReaderImpl(std::string filepath,
79 Resolution resolution,
80 RepeatMode repeat_mode);
Erik Språngebe5acb2020-12-03 15:18:4481
brandtrb78bc752017-02-22 09:26:5982 ~YuvFrameReaderImpl() override;
Sergey Silkin1985b5a2022-12-06 14:36:2183
84 virtual void Init();
85
86 rtc::scoped_refptr<I420Buffer> PullFrame() override;
87
88 rtc::scoped_refptr<I420Buffer> PullFrame(int* frame_num) override;
89
90 rtc::scoped_refptr<I420Buffer> PullFrame(int* frame_num,
91 Resolution resolution,
92 Ratio framerate_scale) override;
93
94 rtc::scoped_refptr<I420Buffer> ReadFrame(int frame_num) override;
95
96 rtc::scoped_refptr<I420Buffer> ReadFrame(int frame_num,
97 Resolution resolution) override;
98
99 int num_frames() const override { return num_frames_; }
kjellander@webrtc.org5b97b122011-12-08 07:42:18100
Artem Titov645df9e2019-01-14 13:21:59101 protected:
Sergey Silkin1985b5a2022-12-06 14:36:21102 class RateScaler {
103 public:
104 int Skip(Ratio framerate_scale);
105
106 private:
107 absl::optional<int> ticks_;
108 };
109
110 const std::string filepath_;
111 Resolution resolution_;
Erik Språngebe5acb2020-12-03 15:18:44112 const RepeatMode repeat_mode_;
Sergey Silkin1985b5a2022-12-06 14:36:21113 int num_frames_;
114 int frame_num_;
115 int frame_size_bytes_;
116 int header_size_bytes_;
117 FILE* file_;
118 RateScaler framerate_scaler_;
kjellander@webrtc.org5b97b122011-12-08 07:42:18119};
120
Artem Titov645df9e2019-01-14 13:21:59121class Y4mFrameReaderImpl : public YuvFrameReaderImpl {
122 public:
Sergey Silkin1985b5a2022-12-06 14:36:21123 // Creates the frame reader for a Y4M file specified by `filepath`.
124 // `repeat_mode` specifies behaviour of the reader at reaching the end of file
125 // (stop, read it over from the beginning or read in reverse order). The file
126 // is assumed to exist, be readable and to contain at least 1 frame.
127 Y4mFrameReaderImpl(std::string filepath, RepeatMode repeat_mode);
Artem Titov645df9e2019-01-14 13:21:59128
Sergey Silkin1985b5a2022-12-06 14:36:21129 void Init() override;
Artem Titov645df9e2019-01-14 13:21:59130};
131
Sergey Silkin1985b5a2022-12-06 14:36:21132std::unique_ptr<FrameReader> CreateYuvFrameReader(std::string filepath,
133 Resolution resolution);
134
135std::unique_ptr<FrameReader> CreateYuvFrameReader(
136 std::string filepath,
137 Resolution resolution,
138 YuvFrameReaderImpl::RepeatMode repeat_mode);
139
140std::unique_ptr<FrameReader> CreateY4mFrameReader(std::string filepath);
141
142std::unique_ptr<FrameReader> CreateY4mFrameReader(
143 std::string filepath,
144 YuvFrameReaderImpl::RepeatMode repeat_mode);
145
kjellander@webrtc.org5b97b122011-12-08 07:42:18146} // namespace test
147} // namespace webrtc
148
Mirko Bonadei92ea95e2017-09-15 04:47:31149#endif // TEST_TESTSUPPORT_FRAME_READER_H_