xref: /aosp_15_r20/external/webrtc/rtc_tools/video_file_reader.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker #ifndef RTC_TOOLS_VIDEO_FILE_READER_H_
11*d9f75844SAndroid Build Coastguard Worker #define RTC_TOOLS_VIDEO_FILE_READER_H_
12*d9f75844SAndroid Build Coastguard Worker 
13*d9f75844SAndroid Build Coastguard Worker #include <stddef.h>
14*d9f75844SAndroid Build Coastguard Worker 
15*d9f75844SAndroid Build Coastguard Worker #include <cstdio>
16*d9f75844SAndroid Build Coastguard Worker #include <iterator>
17*d9f75844SAndroid Build Coastguard Worker #include <string>
18*d9f75844SAndroid Build Coastguard Worker 
19*d9f75844SAndroid Build Coastguard Worker #include "api/scoped_refptr.h"
20*d9f75844SAndroid Build Coastguard Worker #include "api/video/video_frame_buffer.h"
21*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/ref_count.h"
22*d9f75844SAndroid Build Coastguard Worker 
23*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
24*d9f75844SAndroid Build Coastguard Worker namespace test {
25*d9f75844SAndroid Build Coastguard Worker 
26*d9f75844SAndroid Build Coastguard Worker // Iterable class representing a sequence of I420 buffers. This class is not
27*d9f75844SAndroid Build Coastguard Worker // thread safe because it is expected to be backed by a file.
28*d9f75844SAndroid Build Coastguard Worker class Video : public rtc::RefCountInterface {
29*d9f75844SAndroid Build Coastguard Worker  public:
30*d9f75844SAndroid Build Coastguard Worker   class Iterator {
31*d9f75844SAndroid Build Coastguard Worker    public:
32*d9f75844SAndroid Build Coastguard Worker     typedef int value_type;
33*d9f75844SAndroid Build Coastguard Worker     typedef std::ptrdiff_t difference_type;
34*d9f75844SAndroid Build Coastguard Worker     typedef int* pointer;
35*d9f75844SAndroid Build Coastguard Worker     typedef int& reference;
36*d9f75844SAndroid Build Coastguard Worker     typedef std::input_iterator_tag iterator_category;
37*d9f75844SAndroid Build Coastguard Worker 
38*d9f75844SAndroid Build Coastguard Worker     Iterator(const rtc::scoped_refptr<const Video>& video, size_t index);
39*d9f75844SAndroid Build Coastguard Worker     Iterator(const Iterator& other);
40*d9f75844SAndroid Build Coastguard Worker     Iterator(Iterator&& other);
41*d9f75844SAndroid Build Coastguard Worker     Iterator& operator=(Iterator&&);
42*d9f75844SAndroid Build Coastguard Worker     Iterator& operator=(const Iterator&);
43*d9f75844SAndroid Build Coastguard Worker     ~Iterator();
44*d9f75844SAndroid Build Coastguard Worker 
45*d9f75844SAndroid Build Coastguard Worker     rtc::scoped_refptr<I420BufferInterface> operator*() const;
46*d9f75844SAndroid Build Coastguard Worker     bool operator==(const Iterator& other) const;
47*d9f75844SAndroid Build Coastguard Worker     bool operator!=(const Iterator& other) const;
48*d9f75844SAndroid Build Coastguard Worker 
49*d9f75844SAndroid Build Coastguard Worker     Iterator operator++(int);
50*d9f75844SAndroid Build Coastguard Worker     Iterator& operator++();
51*d9f75844SAndroid Build Coastguard Worker 
52*d9f75844SAndroid Build Coastguard Worker    private:
53*d9f75844SAndroid Build Coastguard Worker     rtc::scoped_refptr<const Video> video_;
54*d9f75844SAndroid Build Coastguard Worker     size_t index_;
55*d9f75844SAndroid Build Coastguard Worker   };
56*d9f75844SAndroid Build Coastguard Worker 
57*d9f75844SAndroid Build Coastguard Worker   Iterator begin() const;
58*d9f75844SAndroid Build Coastguard Worker   Iterator end() const;
59*d9f75844SAndroid Build Coastguard Worker 
60*d9f75844SAndroid Build Coastguard Worker   virtual int width() const = 0;
61*d9f75844SAndroid Build Coastguard Worker   virtual int height() const = 0;
62*d9f75844SAndroid Build Coastguard Worker   virtual size_t number_of_frames() const = 0;
63*d9f75844SAndroid Build Coastguard Worker   virtual rtc::scoped_refptr<I420BufferInterface> GetFrame(
64*d9f75844SAndroid Build Coastguard Worker       size_t index) const = 0;
65*d9f75844SAndroid Build Coastguard Worker };
66*d9f75844SAndroid Build Coastguard Worker 
67*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<Video> OpenY4mFile(const std::string& file_name);
68*d9f75844SAndroid Build Coastguard Worker 
69*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<Video> OpenYuvFile(const std::string& file_name,
70*d9f75844SAndroid Build Coastguard Worker                                       int width,
71*d9f75844SAndroid Build Coastguard Worker                                       int height);
72*d9f75844SAndroid Build Coastguard Worker 
73*d9f75844SAndroid Build Coastguard Worker // This is a helper function for the two functions above. It reads the file
74*d9f75844SAndroid Build Coastguard Worker // extension to determine whether it is a .yuv or a .y4m file.
75*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<Video> OpenYuvOrY4mFile(const std::string& file_name,
76*d9f75844SAndroid Build Coastguard Worker                                            int width,
77*d9f75844SAndroid Build Coastguard Worker                                            int height);
78*d9f75844SAndroid Build Coastguard Worker 
79*d9f75844SAndroid Build Coastguard Worker }  // namespace test
80*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
81*d9f75844SAndroid Build Coastguard Worker 
82*d9f75844SAndroid Build Coastguard Worker #endif  // RTC_TOOLS_VIDEO_FILE_READER_H_
83