1 /*
2 * v4l2_buffer_proxy.cpp - v4l2 buffer proxy
3 *
4 * Copyright (c) 2014-2015 Intel Corporation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Wind Yuan <feng.yuan@intel.com>
19 */
20
21 #include "v4l2_buffer_proxy.h"
22 #include "v4l2_device.h"
23 #include <linux/rk-video-format.h>
24
25 namespace XCam {
V4l2Buffer(const struct v4l2_buffer & buf,const struct v4l2_format & format)26 V4l2Buffer::V4l2Buffer (const struct v4l2_buffer &buf, const struct v4l2_format &format)
27 {
28 _buf = buf;
29 _format = format;
30 _queued = false;
31 }
32
~V4l2Buffer()33 V4l2Buffer::~V4l2Buffer ()
34 {
35 }
36
37 uint8_t *
map()38 V4l2Buffer::map ()
39 {
40 if (_buf.memory == V4L2_MEMORY_DMABUF)
41 return NULL;
42
43 if (_expbuf_usrptr)
44 return (uint8_t *)_expbuf_usrptr;
45
46 if (_buf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
47 _buf.type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
48 return (uint8_t *)_buf.m.planes[0].m.userptr;
49 else
50 return (uint8_t *)(_buf.m.userptr);
51 }
52
53 bool
unmap()54 V4l2Buffer::unmap ()
55 {
56 return true;
57 }
58
59 int
get_fd()60 V4l2Buffer::get_fd ()
61 {
62 return _expbuf_fd;
63 }
64
V4l2BufferProxy(SmartPtr<V4l2Buffer> & buf,SmartPtr<V4l2Device> & device)65 V4l2BufferProxy::V4l2BufferProxy (SmartPtr<V4l2Buffer> &buf, SmartPtr<V4l2Device> &device)
66 : BufferProxy (buf)
67 , _device (device)
68 {
69 VideoBufferInfo info;
70 struct timeval ts = buf->get_buf().timestamp;
71 uint32_t sequence = buf->get_buf().sequence;
72
73 v4l2_format_to_video_info (buf->get_format(), info);
74 set_video_info (info);
75 set_timestamp (XCAM_TIMEVAL_2_USEC (ts));
76 set_sequence (sequence);
77 }
78
~V4l2BufferProxy()79 V4l2BufferProxy::~V4l2BufferProxy ()
80 {
81 XCAM_LOG_DEBUG ("~V4l2BufferProxy");
82 SmartPtr<BufferData> data = get_buffer_data ();
83 SmartPtr<V4l2Buffer> v4l2_data = data.dynamic_cast_ptr<V4l2Buffer> ();
84 if (_device.ptr () && v4l2_data.ptr ())
85 _device->return_buffer (v4l2_data);
86 XCAM_LOG_DEBUG ("v4l2 buffer released");
87 }
88
89 void
v4l2_format_to_video_info(const struct v4l2_format & format,VideoBufferInfo & info)90 V4l2BufferProxy::v4l2_format_to_video_info (
91 const struct v4l2_format &format, VideoBufferInfo &info)
92 {
93 if (format.type == V4L2_BUF_TYPE_META_CAPTURE ||
94 format.type == V4L2_BUF_TYPE_META_OUTPUT) {
95 info.format = format.fmt.meta.dataformat;
96 } else {
97 info.format = format.fmt.pix.pixelformat;
98 }
99 info.color_bits = 8;
100 info.width = format.fmt.pix.width;
101 info.height = format.fmt.pix.height;
102 info.aligned_width = 0;
103 info.aligned_height = 0;
104 info.size = format.fmt.pix.sizeimage;
105 switch (info.format) {
106 case V4L2_PIX_FMT_NV12: // 420
107 case V4L2_PIX_FMT_NV21:
108 info.components = 2;
109 info.strides [0] = format.fmt.pix.bytesperline * 2 / 3;
110 info.strides [1] = info.strides [0];
111 info.offsets[0] = 0;
112 info.offsets[1] = info.strides [0] * format.fmt.pix.height;
113 break;
114 case V4L2_PIX_FMT_YUV422P: // 422 Planar
115 info.components = 3;
116 info.strides [0] = format.fmt.pix.bytesperline / 2;
117 info.strides [1] = info.strides [0] / 2 ;
118 info.strides [2] = info.strides [0] / 2 ;
119 info.offsets[0] = 0;
120 info.offsets[1] = info.strides [0] * format.fmt.pix.height;
121 info.offsets[2] = info.offsets[1] + info.strides [1] * format.fmt.pix.height;
122 break;
123 case V4L2_PIX_FMT_YUYV: // 422
124 info.components = 1;
125 info.strides [0] = format.fmt.pix.bytesperline;
126 info.offsets[0] = 0;
127 info.aligned_width = info.strides [0] / 2;
128 break;
129 case V4L2_PIX_FMT_SBGGR10:
130 case V4L2_PIX_FMT_SGBRG10:
131 case V4L2_PIX_FMT_SGRBG10:
132 case V4L2_PIX_FMT_SRGGB10:
133 info.color_bits = 10;
134 info.components = 1;
135 info.strides [0] = format.fmt.pix.bytesperline;
136 info.offsets[0] = 0;
137 break;
138 case V4L2_PIX_FMT_SBGGR12:
139 case V4L2_PIX_FMT_SGBRG12:
140 case V4L2_PIX_FMT_SGRBG12:
141 case V4L2_PIX_FMT_SRGGB12:
142 info.color_bits = 12;
143 info.components = 1;
144 info.strides [0] = format.fmt.pix.bytesperline;
145 info.offsets[0] = 0;
146 break;
147 case V4L2_PIX_FMT_SBGGR16:
148 case V4L2_PIX_FMT_SGBRG16:
149 case V4L2_PIX_FMT_SGRBG16:
150 case V4L2_PIX_FMT_SRGGB16:
151 info.color_bits = 16;
152 info.components = 1;
153 info.strides [0] = info.width * 2;
154 info.offsets[0] = 0;
155 break;
156 case V4L2_META_FMT_RK_ISP1_PARAMS:
157 case V4L2_META_FMT_RK_ISP1_STAT_3A:
158 case V4L2_META_FMT_RK_ISPP_PARAMS:
159 case V4L2_META_FMT_RK_ISPP_STAT:
160 case V4L2_META_FMT_RK_ISP1_STAT_LUMA :
161 case V4L2_PIX_FMT_FBC2:
162 case V4L2_PIX_FMT_FBC0:
163 // TODO
164 break;
165 default:
166 XCAM_LOG_WARNING (
167 "unknown v4l2 format(%s) to video info",
168 xcam_fourcc_to_string (format.fmt.pix.pixelformat));
169 break;
170 }
171
172 if (!info.aligned_width)
173 info.aligned_width = info.strides [0];
174
175 if (!info.aligned_height)
176 info.aligned_height = info.height;
177
178 }
179
180 const struct v4l2_buffer &
get_v4l2_buf()181 V4l2BufferProxy::get_v4l2_buf ()
182 {
183 SmartPtr<BufferData> &data = get_buffer_data ();
184 SmartPtr<V4l2Buffer> v4l2_data = data.dynamic_cast_ptr<V4l2Buffer> ();
185 XCAM_ASSERT (v4l2_data.ptr ());
186 return v4l2_data->get_buf ();
187 }
188
189 }
190