• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef ANDROID_V4L2_CODEC2_COMPONENTS_VIDEO_ENCODER_H
6 #define ANDROID_V4L2_CODEC2_COMPONENTS_VIDEO_ENCODER_H
7 
8 #include <stdint.h>
9 #include <memory>
10 #include <vector>
11 
12 #include <base/callback.h>
13 #include <ui/Size.h>
14 
15 #include <v4l2_codec2/common/Common.h>
16 #include <v4l2_codec2/common/VideoPixelFormat.h>
17 #include <v4l2_codec2/common/VideoTypes.h>
18 
19 namespace android {
20 
21 struct BitstreamBuffer;
22 
23 class VideoEncoder {
24 public:
25     // The InputFrame class can be used to store raw video frames.
26     // Note: The InputFrame does not take ownership of the data. The file descriptor is not
27     //       duplicated and the caller is responsible for keeping the data alive until the buffer
28     //       is returned by an InputBufferDoneCB() call.
29     class InputFrame {
30     public:
31         InputFrame(std::vector<int>&& fds, std::vector<VideoFramePlane>&& planes,
32                    VideoPixelFormat pixelFormat, uint64_t index, int64_t timestamp);
33         ~InputFrame() = default;
34 
fds()35         const std::vector<int>& fds() const { return mFds; }
planes()36         const std::vector<VideoFramePlane>& planes() const { return mPlanes; }
pixelFormat()37         VideoPixelFormat pixelFormat() const { return mPixelFormat; }
index()38         uint64_t index() const { return mIndex; }
timestamp()39         int64_t timestamp() const { return mTimestamp; }
40 
41     private:
42         const std::vector<int> mFds;
43         std::vector<VideoFramePlane> mPlanes;
44         VideoPixelFormat mPixelFormat;
45         uint64_t mIndex = 0;
46         int64_t mTimestamp = 0;
47     };
48 
49     using FetchOutputBufferCB =
50             base::RepeatingCallback<void(uint32_t, std::unique_ptr<BitstreamBuffer>* buffer)>;
51     // TODO(dstaessens): Change callbacks to OnceCallback provided when requesting encode/drain.
52     using InputBufferDoneCB = base::RepeatingCallback<void(uint64_t)>;
53     using OutputBufferDoneCB = base::RepeatingCallback<void(
54             size_t, int64_t, bool, std::unique_ptr<BitstreamBuffer> buffer)>;
55     using DrainDoneCB = base::RepeatingCallback<void(bool)>;
56     using ErrorCB = base::RepeatingCallback<void()>;
57 
58     virtual ~VideoEncoder() = default;
59 
60     // Encode the frame, |InputBufferDoneCB| and |OutputBufferDoneCB| will be called when done.
61     virtual bool encode(std::unique_ptr<InputFrame> buffer) = 0;
62     // Drain the encoder, |mDrainDoneCb| will be called when done.
63     virtual void drain() = 0;
64     // Flush the encoder, pending drain operations will be aborted.
65     virtual void flush() = 0;
66 
67     // Set the target bitrate to the specified value, will affect all non-processed frames.
68     virtual bool setBitrate(uint32_t bitrate) = 0;
69     // Set the peak bitrate to the specified value. The peak bitrate must be larger or equal to the
70     // target bitrate and is ignored if the bitrate mode is constant.
71     virtual bool setPeakBitrate(uint32_t peakBitrate) = 0;
72 
73     // Set the framerate to the specified value, will affect all non-processed frames.
74     virtual bool setFramerate(uint32_t framerate) = 0;
75     // Request the next frame encoded to be a key frame, will affect the next non-processed frame.
76     virtual void requestKeyframe() = 0;
77 
78     virtual VideoPixelFormat inputFormat() const = 0;
79     virtual const ui::Size& visibleSize() const = 0;
80     virtual const ui::Size& codedSize() const = 0;
81 };
82 
83 }  // namespace android
84 
85 #endif  // ANDROID_V4L2_CODEC2_COMPONENTS_VIDEO_ENCODER_H
86