hc
2024-08-12 233ab1bd4c5697f5cdec94e60206e8c6ac609b4c
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * v4l2_device.h - v4l2 device
 *
 *  Copyright (c) 2014-2015 Intel Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author: Wind Yuan <feng.yuan@intel.com>
 */
 
#ifndef XCAM_V4L2_DEVICE_H
#define XCAM_V4L2_DEVICE_H
 
#include <xcam_std.h>
#include <xcam_mutex.h>
#include <linux/videodev2.h>
#include <linux/v4l2-subdev.h>
#include <list>
#include <vector>
 
extern "C" {
    struct v4l2_event;
    struct v4l2_format;
    struct v4l2_fmtdesc;
    struct v4l2_frmsizeenum;
}
 
namespace XCam {
#define FMT_NUM_PLANES 1
#define POLL_STOP_RET 3
 
class V4l2Buffer;
 
class V4l2Device {
    friend class V4l2BufferProxy;
    typedef std::vector<SmartPtr<V4l2Buffer>> BufferPool;
 
public:
    V4l2Device (const char *name = NULL);
    virtual ~V4l2Device ();
 
    // before device open
    bool set_device_name (const char *name);
    bool set_sensor_id (int id);
    bool set_capture_mode (uint32_t capture_mode);
    bool set_mplanes_count (uint32_t planes_count);
 
    int get_fd () const {
        return _fd;
    }
    const char *get_device_name () const {
        return _name;
    }
    bool is_opened () const    {
        return (_fd != -1);
    }
    bool is_activated () const {
        return _active;
    }
 
    // set_mem_type must before set_format
    bool set_mem_type (enum v4l2_memory type);
    enum v4l2_memory get_mem_type () const {
        return _memory_type;
    }
    bool set_buf_type (enum v4l2_buf_type type);
    bool set_buf_sync (bool sync);
    enum v4l2_buf_type get_buf_type () const {
        return _buf_type;
    }
    void get_size (uint32_t &width, uint32_t &height) const {
        width = _format.fmt.pix.width;
        height = _format.fmt.pix.height;
    }
    uint32_t get_pixel_format () const {
        return _format.fmt.pix.pixelformat;
    }
 
    bool set_buffer_count (uint32_t buf_count);
    int get_buffer_count () {
        return _buf_count;
    }
    int get_queued_bufcnt () {
        return _queued_bufcnt;
    }
 
    // set_framerate must before set_format
    bool set_framerate (uint32_t n, uint32_t d);
    void get_framerate (uint32_t &n, uint32_t &d);
 
    virtual XCamReturn open (bool nonblock = false);
    virtual XCamReturn close ();
 
    XCamReturn query_cap(struct v4l2_capability &cap);
    XCamReturn get_crop (struct v4l2_crop &crop);
    XCamReturn set_crop (struct v4l2_crop &crop);
    XCamReturn set_selection (struct v4l2_selection &select);
    // set_format
    virtual XCamReturn get_format (struct v4l2_format &format);
    XCamReturn set_format (struct v4l2_format &format);
    XCamReturn set_format (
        uint32_t width, uint32_t height, uint32_t pixelformat,
        enum v4l2_field field = V4L2_FIELD_NONE, uint32_t bytes_perline = 0);
 
    std::list<struct v4l2_fmtdesc> enum_formats ();
 
    virtual XCamReturn start (bool prepared = false);
    virtual XCamReturn stop ();
    XCamReturn prepare ();
 
    virtual int poll_event (int timeout_msec, int stop_fd);
 
    SmartPtr<V4l2Buffer> get_buffer_by_index (int index);
    virtual XCamReturn dequeue_buffer (SmartPtr<V4l2Buffer> &buf);
    virtual XCamReturn queue_buffer (SmartPtr<V4l2Buffer> &buf, bool locked = false);
    XCamReturn return_buffer (SmartPtr<V4l2Buffer> &buf);
    XCamReturn return_buffer_to_pool (SmartPtr<V4l2Buffer> &buf);
    // get free buf for type V4L2_BUF_TYPE_xxx_OUTPUT
    XCamReturn get_buffer (SmartPtr<V4l2Buffer> &buf, int index = -1) const;
    // use as less as possible
    virtual int io_control (int cmd, void *arg);
    virtual int get_use_type() { return 0;}
    virtual void set_use_type(int type) {}
    SmartPtr<V4l2Buffer> get_available_buffer ();
    virtual XCamReturn subscribe_event (int event);
    virtual XCamReturn unsubscribe_event (int event);
    virtual XCamReturn dequeue_event (struct v4l2_event &event);
 
protected:
 
    //virtual functions, handle private actions on set_format
    virtual XCamReturn pre_set_format (struct v4l2_format &format);
    virtual XCamReturn post_set_format (struct v4l2_format &format);
    virtual XCamReturn allocate_buffer (
        SmartPtr<V4l2Buffer> &buf,
        const struct v4l2_format &format,
        const uint32_t index);
    virtual XCamReturn release_buffer (SmartPtr<V4l2Buffer> &buf);
 
private:
    XCamReturn request_buffer ();
    XCamReturn init_buffer_pool ();
    XCamReturn fini_buffer_pool ();
 
    XCAM_DEAD_COPY (V4l2Device);
 
protected:
    char               *_name;
    int                 _fd;
    int32_t             _sensor_id;
    uint32_t            _capture_mode;
    enum v4l2_buf_type  _buf_type;
    bool                _buf_sync;
    enum v4l2_memory    _memory_type;
    struct v4l2_plane  *_planes;
 
    struct v4l2_format  _format;
    uint32_t            _fps_n;
    uint32_t            _fps_d;
 
    bool                _active;
 
    // buffer pool
    BufferPool          _buf_pool;
    uint32_t            _buf_count;
    uint32_t            _queued_bufcnt;
    mutable Mutex      _buf_mutex;
    int32_t            _mplanes_count;
    XCamReturn buffer_new();
    XCamReturn buffer_del();
};
 
class V4l2SubDevice
    : public V4l2Device
{
public:
    explicit V4l2SubDevice (const char *name = NULL);
 
    virtual XCamReturn get_selection (int pad, uint32_t target, struct v4l2_subdev_selection &select);
    virtual XCamReturn setFormat(struct v4l2_subdev_format &aFormat);
    virtual XCamReturn getFormat(struct v4l2_subdev_format &aFormat);
    virtual XCamReturn set_selection (struct v4l2_subdev_selection &aSelection);
 
    virtual XCamReturn start (bool prepared = false);
    virtual XCamReturn stop ();
 
private:
    XCAM_DEAD_COPY (V4l2SubDevice);
};
 
};
#endif // XCAM_V4L2_DEVICE_H