• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
11 #ifndef TEST_TESTSUPPORT_FRAME_READER_H_
12 #define TEST_TESTSUPPORT_FRAME_READER_H_
13 
14 #include <stdio.h>
15 
16 #include <string>
17 
18 #include "api/scoped_refptr.h"
19 
20 namespace webrtc {
21 class I420Buffer;
22 namespace test {
23 
24 // Handles reading of I420 frames from video files.
25 class FrameReader {
26  public:
~FrameReader()27   virtual ~FrameReader() {}
28 
29   // Initializes the frame reader, i.e. opens the input file.
30   // This must be called before reading of frames has started.
31   // Returns false if an error has occurred, in addition to printing to stderr.
32   virtual bool Init() = 0;
33 
34   // Reads a frame from the input file. On success, returns the frame.
35   // Returns nullptr if encountering end of file or a read error.
36   virtual rtc::scoped_refptr<I420Buffer> ReadFrame() = 0;
37 
38   // Closes the input file if open. Essentially makes this class impossible
39   // to use anymore. Will also be invoked by the destructor.
40   virtual void Close() = 0;
41 
42   // Frame length in bytes of a single frame image.
43   virtual size_t FrameLength() = 0;
44   // Total number of frames in the input video source.
45   virtual int NumberOfFrames() = 0;
46 };
47 
48 class YuvFrameReaderImpl : public FrameReader {
49  public:
50   // Creates a file handler. The input file is assumed to exist and be readable.
51   // Parameters:
52   //   input_filename          The file to read from.
53   //   width, height           Size of each frame to read.
54   YuvFrameReaderImpl(std::string input_filename, int width, int height);
55   ~YuvFrameReaderImpl() override;
56   bool Init() override;
57   rtc::scoped_refptr<I420Buffer> ReadFrame() override;
58   void Close() override;
59   size_t FrameLength() override;
60   int NumberOfFrames() override;
61 
62  protected:
63   const std::string input_filename_;
64   // It is not const, so subclasses will be able to add frame header size.
65   size_t frame_length_in_bytes_;
66   const int width_;
67   const int height_;
68   int number_of_frames_;
69   FILE* input_file_;
70 };
71 
72 class Y4mFrameReaderImpl : public YuvFrameReaderImpl {
73  public:
74   // Creates a file handler. The input file is assumed to exist and be readable.
75   // Parameters:
76   //   input_filename          The file to read from.
77   //   width, height           Size of each frame to read.
78   Y4mFrameReaderImpl(std::string input_filename, int width, int height);
79   ~Y4mFrameReaderImpl() override;
80   bool Init() override;
81   rtc::scoped_refptr<I420Buffer> ReadFrame() override;
82 
83  private:
84   // Buffer that is used to read file and frame headers.
85   uint8_t* buffer_;
86 };
87 
88 }  // namespace test
89 }  // namespace webrtc
90 
91 #endif  // TEST_TESTSUPPORT_FRAME_READER_H_
92