aboutsummaryrefslogtreecommitdiff
path: root/components/include/v4l2_codec2/components/VideoEncoder.h
blob: 5f23541bde6ec0032b26c9ac9a36fb7b569c8100 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ANDROID_V4L2_CODEC2_COMPONENTS_VIDEO_ENCODER_H
#define ANDROID_V4L2_CODEC2_COMPONENTS_VIDEO_ENCODER_H

#include <stdint.h>
#include <memory>
#include <vector>

#include <base/callback.h>
#include <ui/Size.h>

#include <v4l2_codec2/common/Common.h>
#include <v4l2_codec2/common/VideoPixelFormat.h>
#include <v4l2_codec2/common/VideoTypes.h>

namespace android {

struct BitstreamBuffer;

class VideoEncoder {
public:
    // The InputFrame class can be used to store raw video frames.
    // Note: The InputFrame does not take ownership of the data. The file descriptor is not
    //       duplicated and the caller is responsible for keeping the data alive until the buffer
    //       is returned by an InputBufferDoneCB() call.
    class InputFrame {
    public:
        InputFrame(std::vector<int>&& fds, std::vector<VideoFramePlane>&& planes,
                   VideoPixelFormat pixelFormat, uint64_t index, int64_t timestamp);
        ~InputFrame() = default;

        const std::vector<int>& fds() const { return mFds; }
        const std::vector<VideoFramePlane>& planes() const { return mPlanes; }
        VideoPixelFormat pixelFormat() const { return mPixelFormat; }
        uint64_t index() const { return mIndex; }
        int64_t timestamp() const { return mTimestamp; }

    private:
        const std::vector<int> mFds;
        std::vector<VideoFramePlane> mPlanes;
        VideoPixelFormat mPixelFormat;
        uint64_t mIndex = 0;
        int64_t mTimestamp = 0;
    };

    using FetchOutputBufferCB =
            base::RepeatingCallback<void(uint32_t, std::unique_ptr<BitstreamBuffer>* buffer)>;
    // TODO(dstaessens): Change callbacks to OnceCallback provided when requesting encode/drain.
    using InputBufferDoneCB = base::RepeatingCallback<void(uint64_t)>;
    using OutputBufferDoneCB = base::RepeatingCallback<void(
            size_t, int64_t, bool, std::unique_ptr<BitstreamBuffer> buffer)>;
    using DrainDoneCB = base::RepeatingCallback<void(bool)>;
    using ErrorCB = base::RepeatingCallback<void()>;

    virtual ~VideoEncoder() = default;

    // Encode the frame, |InputBufferDoneCB| and |OutputBufferDoneCB| will be called when done.
    virtual bool encode(std::unique_ptr<InputFrame> buffer) = 0;
    // Drain the encoder, |mDrainDoneCb| will be called when done.
    virtual void drain() = 0;
    // Flush the encoder, pending drain operations will be aborted.
    virtual void flush() = 0;

    // Set the target bitrate to the specified value, will affect all non-processed frames.
    virtual bool setBitrate(uint32_t bitrate) = 0;
    // Set the peak bitrate to the specified value. The peak bitrate must be larger or equal to the
    // target bitrate and is ignored if the bitrate mode is constant.
    virtual bool setPeakBitrate(uint32_t peakBitrate) = 0;

    // Set the framerate to the specified value, will affect all non-processed frames.
    virtual bool setFramerate(uint32_t framerate) = 0;
    // Request the next frame encoded to be a key frame, will affect the next non-processed frame.
    virtual void requestKeyframe() = 0;

    virtual VideoPixelFormat inputFormat() const = 0;
    virtual const ui::Size& visibleSize() const = 0;
    virtual const ui::Size& codedSize() const = 0;
};

}  // namespace android

#endif  // ANDROID_V4L2_CODEC2_COMPONENTS_VIDEO_ENCODER_H