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 // Number of buffers on component delays. 26 static constexpr size_t kInputBufferCount = 2; 27 static constexpr size_t kOutputBufferCount = 2; 28 static constexpr VideoPixelFormat kInputPixelFormat = VideoPixelFormat::NV12; 29 30 // The peak bitrate in function of the target bitrate, used when the bitrate mode is VBR. 31 static constexpr uint32_t kPeakBitrateMultiplier = 2u; 32 33 // The InputFrame class can be used to store raw video frames. 34 // Note: The InputFrame does not take ownership of the data. The file descriptor is not 35 // duplicated and the caller is responsible for keeping the data alive until the buffer 36 // is returned by an InputBufferDoneCB() call. 37 class InputFrame { 38 public: 39 InputFrame(std::vector<int>&& fds, const std::vector<VideoFramePlane>& planes, 40 VideoPixelFormat pixelFormat, uint64_t index, int64_t timestamp); 41 ~InputFrame() = default; 42 fds()43 const std::vector<int>& fds() const { return mFds; } planes()44 const std::vector<VideoFramePlane>& planes() const { return mPlanes; } pixelFormat()45 VideoPixelFormat pixelFormat() const { return mPixelFormat; } index()46 uint64_t index() const { return mIndex; } timestamp()47 int64_t timestamp() const { return mTimestamp; } 48 49 private: 50 const std::vector<int> mFds; 51 std::vector<VideoFramePlane> mPlanes; 52 VideoPixelFormat mPixelFormat; 53 uint64_t mIndex = 0; 54 int64_t mTimestamp = 0; 55 }; 56 57 using FetchOutputBufferCB = 58 ::base::RepeatingCallback<void(uint32_t, std::unique_ptr<BitstreamBuffer>* buffer)>; 59 // TODO(dstaessens): Change callbacks to OnceCallback provided when requesting encode/drain. 60 using InputBufferDoneCB = ::base::RepeatingCallback<void(uint64_t)>; 61 using OutputBufferDoneCB = ::base::RepeatingCallback<void( 62 size_t, int64_t, bool, std::unique_ptr<BitstreamBuffer> buffer)>; 63 using DrainDoneCB = ::base::RepeatingCallback<void(bool)>; 64 using ErrorCB = ::base::RepeatingCallback<void()>; 65 66 virtual ~VideoEncoder() = default; 67 68 // Encode the frame, |InputBufferDoneCB| and |OutputBufferDoneCB| will be called when done. 69 virtual bool encode(std::unique_ptr<InputFrame> buffer) = 0; 70 // Drain the encoder, |mDrainDoneCb| will be called when done. 71 virtual void drain() = 0; 72 // Flush the encoder, pending drain operations will be aborted. 73 virtual void flush() = 0; 74 75 // Set the target bitrate to the specified value, will affect all non-processed frames. 76 virtual bool setBitrate(uint32_t bitrate) = 0; 77 // Set the peak bitrate to the specified value. The peak bitrate must be larger or equal to the 78 // target bitrate and is ignored if the bitrate mode is constant. 79 virtual bool setPeakBitrate(uint32_t peakBitrate) = 0; 80 81 // Set the framerate to the specified value, will affect all non-processed frames. 82 virtual bool setFramerate(uint32_t framerate) = 0; 83 // Request the next frame encoded to be a key frame, will affect the next non-processed frame. 84 virtual void requestKeyframe() = 0; 85 86 virtual VideoPixelFormat inputFormat() const = 0; 87 virtual const ui::Size& visibleSize() const = 0; 88 virtual const ui::Size& codedSize() const = 0; 89 }; 90 91 } // namespace android 92 93 #endif // ANDROID_V4L2_CODEC2_COMPONENTS_VIDEO_ENCODER_H 94