xref: /aosp_15_r20/external/v4l2_codec2/v4l2/include/v4l2_codec2/v4l2/V4L2Encoder.h (revision 0ec5a0ec62797f775085659156625e7f1bdb369f)
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_V4L2_V4L2_ENCODER_H
6 #define ANDROID_V4L2_CODEC2_V4L2_V4L2_ENCODER_H
7 
8 #include <stdint.h>
9 #include <memory>
10 #include <optional>
11 #include <queue>
12 #include <vector>
13 
14 #include <base/memory/weak_ptr.h>
15 #include <base/sequenced_task_runner.h>
16 #include <ui/Size.h>
17 
18 #include <v4l2_codec2/common/Common.h>
19 #include <v4l2_codec2/components/VideoEncoder.h>
20 
21 namespace android {
22 
23 struct BitstreamBuffer;
24 struct VideoFramePlane;
25 class V4L2Device;
26 class V4L2Queue;
27 
28 class V4L2Encoder : public VideoEncoder {
29 public:
30     static std::unique_ptr<VideoEncoder> create(
31             C2Config::profile_t profile, std::optional<uint8_t> level, const ui::Size& visibleSize,
32             uint32_t stride, uint32_t keyFramePeriod, C2Config::bitrate_mode_t bitrateMode,
33             uint32_t bitrate, std::optional<uint32_t> peakBitrate,
34             FetchOutputBufferCB fetchOutputBufferCb, InputBufferDoneCB inputBufferDoneCb,
35             OutputBufferDoneCB outputBufferDoneCb, DrainDoneCB drainDoneCb, ErrorCB errorCb,
36             scoped_refptr<::base::SequencedTaskRunner> taskRunner);
37     ~V4L2Encoder() override;
38 
39     bool encode(std::unique_ptr<InputFrame> frame) override;
40     void drain() override;
41     void flush() override;
42 
43     bool setBitrate(uint32_t bitrate) override;
44     bool setPeakBitrate(uint32_t peakBitrate) override;
45     bool setFramerate(uint32_t framerate) override;
46     void requestKeyframe() override;
47 
48     VideoPixelFormat inputFormat() const override;
visibleSize()49     const ui::Size& visibleSize() const override { return mVisibleSize; }
codedSize()50     const ui::Size& codedSize() const override { return mInputCodedSize; }
51 
52 private:
53     // Possible encoder states.
54     enum class State {
55         UNINITIALIZED,            // Not initialized yet or initialization failed.
56         WAITING_FOR_INPUT_FRAME,  // Waiting for frames to be queued.
57         WAITING_FOR_V4L2_BUFFER,  // Waiting for V4L2 input queue buffers.
58         ENCODING,                 // Queuing input buffers.
59         DRAINING,                 // Draining encoder.
60         ERROR,                    // Encoder encountered an error.
61     };
62 
63     // Contains a single encode request.
64     struct EncodeRequest {
EncodeRequestEncodeRequest65         EncodeRequest(std::unique_ptr<InputFrame> video_frame)
66               : video_frame(std::move(video_frame)) {}
67         ~EncodeRequest() = default;
68         EncodeRequest(EncodeRequest&&) = default;
69         EncodeRequest& operator=(EncodeRequest&&) = default;
70 
71         std::unique_ptr<InputFrame> video_frame;
72         bool end_of_stream = false;
73     };
74 
75     V4L2Encoder(scoped_refptr<::base::SequencedTaskRunner> taskRunner,
76                 FetchOutputBufferCB fetchOutputBufferCb, InputBufferDoneCB mInputBufferDoneCb,
77                 OutputBufferDoneCB mOutputBufferDoneCb, DrainDoneCB drainDoneCb, ErrorCB errorCb);
78 
79     // Initialize the V4L2 encoder for specified parameters.
80     bool initialize(C2Config::profile_t outputProfile, std::optional<uint8_t> level,
81                     const ui::Size& visibleSize, uint32_t stride, uint32_t keyFramePeriod,
82                     C2Config::bitrate_mode_t bitrateMode, uint32_t bitrate,
83                     std::optional<uint32_t> peakBitrate);
84 
85     // Handle the next encode request on the queue.
86     void handleEncodeRequest();
87     // Handle a request to flush the encoder.
88     void handleFlushRequest();
89     // Handle a request to drain the encoder.
90     void handleDrainRequest();
91     // Called when draining the encoder has completed.
92     void onDrainDone(bool done);
93 
94     // Configure input format on the V4L2 device.
95     bool configureInputFormat(VideoPixelFormat inputFormat, uint32_t stride);
96     // Configure output format on the V4L2 device.
97     bool configureOutputFormat(C2Config::profile_t outputProfile);
98     // Configure required and optional controls on the V4L2 device.
99     bool configureDevice(C2Config::profile_t outputProfile,
100                          std::optional<const uint8_t> outputH264Level);
101     // Configure required and optional H.264 controls on the V4L2 device.
102     bool configureH264(C2Config::profile_t outputProfile,
103                        std::optional<const uint8_t> outputH264Level);
104     // Configure the specified bitrate mode on the V4L2 device.
105     bool configureBitrateMode(C2Config::bitrate_mode_t bitrateMode);
106 
107     // Attempt to start the V4L2 device poller.
108     bool startDevicePoll();
109     // Attempt to stop the V4L2 device poller.
110     bool stopDevicePoll();
111     // Called by the V4L2 device poller whenever an error occurred.
112     void onPollError();
113     // Service I/O on the V4L2 device, called by the V4L2 device poller.
114     void serviceDeviceTask(bool event);
115 
116     // Enqueue an input buffer to be encoded on the device input queue. Returns whether the
117     // operation was successful.
118     bool enqueueInputBuffer(std::unique_ptr<InputFrame> frame);
119     // Enqueue an output buffer to store the encoded bitstream on the device output queue. Returns
120     // whether the operation was successful.
121     bool enqueueOutputBuffer();
122     // Dequeue an input buffer the V4L2 device has finished encoding on the device input queue.
123     // Returns whether a buffer could be dequeued.
124     bool dequeueInputBuffer();
125     // Dequeue an output buffer containing the encoded bitstream from the device output queue.
126     // Returns whether the operation was successful.
127     bool dequeueOutputBuffer();
128 
129     // Create input buffers on the V4L2 device input queue.
130     bool createInputBuffers();
131     // Create output buffers on the V4L2 device output queue.
132     bool createOutputBuffers();
133     // Destroy the input buffers on the V4L2 device input queue.
134     void destroyInputBuffers();
135     // Destroy the output buffers on the V4L2 device output queue.
136     void destroyOutputBuffers();
137 
138     // Notify the client an error occurred and switch to the error state.
139     void onError();
140 
141     // Change the state of the encoder.
142     void setState(State state);
143     // Get the specified encoder |state| as string.
144     static const char* stateToString(State state);
145 
146     // The list of currently queued encode requests.
147     std::queue<EncodeRequest> mEncodeRequests;
148 
149     // The video stream's visible size.
150     ui::Size mVisibleSize;
151     // The video stream's coded size.
152     ui::Size mInputCodedSize;
153     // The input layout configured on the V4L2 device.
154     std::optional<VideoFrameLayout> mInputLayout;
155     // Required output buffer byte size.
156     uint32_t mOutputBufferSize = 0;
157 
158     // How often we want to request the V4L2 device to create a key frame.
159     uint32_t mKeyFramePeriod = 0;
160     // Key frame counter, a key frame will be requested each time it reaches zero.
161     uint32_t mKeyFrameCounter = 0;
162 
163     // Whether we need to manually cache and prepend SPS and PPS to IDR frames.
164     bool mInjectParamsBeforeIDR = false;
165     // The latest cached SPS and PPS (without H.264 start code).
166     std::vector<uint8_t> mCachedSPS;
167     std::vector<uint8_t> mCachedPPS;
168 
169     // The V4L2 device and associated queues used to interact with the device.
170     scoped_refptr<V4L2Device> mDevice;
171     scoped_refptr<V4L2Queue> mInputQueue;
172     scoped_refptr<V4L2Queue> mOutputQueue;
173 
174     // List of frames associated with each buffer in the V4L2 device input queue.
175     std::vector<std::unique_ptr<InputFrame>> mInputBuffers;
176     // List of bitstream buffers associated with each buffer in the V4L2 device output queue.
177     std::vector<std::unique_ptr<BitstreamBuffer>> mOutputBuffers;
178 
179     // Callbacks to be triggered on various events.
180     FetchOutputBufferCB mFetchOutputBufferCb;
181     InputBufferDoneCB mInputBufferDoneCb;
182     OutputBufferDoneCB mOutputBufferDoneCb;
183     DrainDoneCB mDrainDoneCb;
184     ErrorCB mErrorCb;
185 
186     // The current state of the encoder.
187     State mState = State::UNINITIALIZED;
188 
189     scoped_refptr<::base::SequencedTaskRunner> mTaskRunner;
190 
191     ::base::WeakPtr<V4L2Encoder> mWeakThis;
192     ::base::WeakPtrFactory<V4L2Encoder> mWeakThisFactory{this};
193 };
194 
195 }  // namespace android
196 
197 #endif  // ANDROID_V4L2_CODEC2_V4L2_V4L2_ENCODER_H
198