blob: 0d87a8008d47eb1132bbd666c73522a8d1b0b72e [file] [log] [blame]
Wind Yuan75564b12015-01-15 06:51:15 -05001/*
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
24namespace XCam {
25V4l2Buffer::V4l2Buffer (const struct v4l2_buffer &buf, const struct v4l2_format &format)
26{
27 _buf = buf;
28 _format = format;
29}
30
31V4l2Buffer::~V4l2Buffer ()
32{
33}
34
Wind Yuan18d4cb52015-04-02 18:31:39 +080035uint8_t *
36V4l2Buffer::map ()
37{
38 if (_buf.memory == V4L2_MEMORY_DMABUF)
39 return NULL;
40 return (uint8_t *)(_buf.m.userptr);
41}
42
43bool
44V4l2Buffer::unmap ()
45{
46 return true;
47}
48
Jia Meng10a616d2015-04-23 20:57:09 +000049int
50V4l2Buffer::get_fd ()
51{
Jia Meng813e02d2015-06-02 16:34:37 +000052 if (_buf.memory == V4L2_MEMORY_MMAP)
Wind Yuan716ab942015-06-17 16:43:21 +080053 return -1;
Jia Meng10a616d2015-04-23 20:57:09 +000054 return _buf.m.fd;
55}
56
Wind Yuan75564b12015-01-15 06:51:15 -050057V4l2BufferProxy::V4l2BufferProxy (SmartPtr<V4l2Buffer> &buf, SmartPtr<V4l2Device> &device)
Wind Yuan18d4cb52015-04-02 18:31:39 +080058 : BufferProxy (buf)
Wind Yuan75564b12015-01-15 06:51:15 -050059 , _device (device)
60{
61 VideoBufferInfo info;
62 struct timeval ts = buf->get_buf().timestamp;
63
Wind Yuan75564b12015-01-15 06:51:15 -050064 v4l2_format_to_video_info (buf->get_format(), info);
65 set_video_info (info);
66 set_timestamp (XCAM_TIMEVAL_2_USEC (ts));
67}
68
69V4l2BufferProxy::~V4l2BufferProxy ()
70{
Wind Yuan18d4cb52015-04-02 18:31:39 +080071 SmartPtr<BufferData> data = get_buffer_data ();
72 SmartPtr<V4l2Buffer> v4l2_data = data.dynamic_cast_ptr<V4l2Buffer> ();
73 if (_device.ptr () && v4l2_data.ptr ())
74 _device->queue_buffer (v4l2_data);
Wind Yuan75564b12015-01-15 06:51:15 -050075 XCAM_LOG_DEBUG ("v4l2 buffer released");
76}
77
78void
79V4l2BufferProxy::v4l2_format_to_video_info (
80 const struct v4l2_format &format, VideoBufferInfo &info)
81{
Wind Yuan75564b12015-01-15 06:51:15 -050082 info.format = format.fmt.pix.pixelformat;
Wind Yuanba55d872015-03-03 10:31:20 +080083 info.color_bits = 8;
Wind Yuan75564b12015-01-15 06:51:15 -050084 info.width = format.fmt.pix.width;
85 info.height = format.fmt.pix.height;
Wind Yuanada12e42015-03-25 17:26:56 +080086 info.aligned_width = 0;
87 info.aligned_height = 0;
Wind Yuan75564b12015-01-15 06:51:15 -050088 info.size = format.fmt.pix.sizeimage;
89 switch (format.fmt.pix.pixelformat) {
90 case V4L2_PIX_FMT_NV12: // 420
91 case V4L2_PIX_FMT_NV21:
92 info.components = 2;
93 info.strides [0] = format.fmt.pix.bytesperline * 2 / 3;
Wind Yuan817a06f2015-02-13 17:17:16 +080094 info.strides [1] = info.strides [0];
Wind Yuan75564b12015-01-15 06:51:15 -050095 info.offsets[0] = 0;
96 info.offsets[1] = info.strides [0] * format.fmt.pix.height;
97 break;
John Ye1503c6b2015-01-22 13:26:33 +080098 case V4L2_PIX_FMT_YUV422P: // 422 Planar
Wind Yuan75564b12015-01-15 06:51:15 -050099 info.components = 3;
100 info.strides [0] = format.fmt.pix.bytesperline / 2;
101 info.strides [1] = info.strides [0] / 2 ;
102 info.strides [2] = info.strides [0] / 2 ;
103 info.offsets[0] = 0;
104 info.offsets[1] = info.strides [0] * format.fmt.pix.height;
105 info.offsets[2] = info.offsets[1] + info.strides [1] * format.fmt.pix.height;
106 break;
John Ye1503c6b2015-01-22 13:26:33 +0800107 case V4L2_PIX_FMT_YUYV: // 422
108 info.components = 1;
109 info.strides [0] = format.fmt.pix.bytesperline;
110 info.offsets[0] = 0;
Wind Yuanada12e42015-03-25 17:26:56 +0800111 info.aligned_width = info.strides [0] / 2;
John Ye1503c6b2015-01-22 13:26:33 +0800112 break;
Wind Yuanba55d872015-03-03 10:31:20 +0800113 case V4L2_PIX_FMT_SBGGR10:
114 case V4L2_PIX_FMT_SGBRG10:
115 case V4L2_PIX_FMT_SGRBG10:
116 case V4L2_PIX_FMT_SRGGB10:
117 info.color_bits = 10;
118 info.components = 1;
119 info.strides [0] = format.fmt.pix.bytesperline;
120 info.offsets[0] = 0;
121 break;
ShincyTu1a4de9d2015-06-01 18:04:51 +0800122 case V4L2_PIX_FMT_SBGGR12:
123 case V4L2_PIX_FMT_SGBRG12:
124 case V4L2_PIX_FMT_SGRBG12:
125 case V4L2_PIX_FMT_SRGGB12:
126 info.color_bits = 12;
127 info.components = 1;
128 info.strides [0] = format.fmt.pix.bytesperline;
129 info.offsets[0] = 0;
130 break;
Wind Yuan75564b12015-01-15 06:51:15 -0500131 default:
132 XCAM_LOG_WARNING (
133 "unknown v4l2 format(%s) to video info",
134 xcam_fourcc_to_string (format.fmt.pix.pixelformat));
135 break;
136 }
137
Wind Yuanada12e42015-03-25 17:26:56 +0800138 if (!info.aligned_width)
139 info.aligned_width = info.strides [0];
140
141 if (!info.aligned_height)
142 info.aligned_height = info.height;
143
Wind Yuan75564b12015-01-15 06:51:15 -0500144}
145
146const struct v4l2_buffer &
Wind Yuanb90a89c2015-04-08 18:38:53 +0800147V4l2BufferProxy::get_v4l2_buf ()
Wind Yuan75564b12015-01-15 06:51:15 -0500148{
Wind Yuanb90a89c2015-04-08 18:38:53 +0800149 SmartPtr<BufferData> &data = get_buffer_data ();
Wind Yuan18d4cb52015-04-02 18:31:39 +0800150 SmartPtr<V4l2Buffer> v4l2_data = data.dynamic_cast_ptr<V4l2Buffer> ();
151 XCAM_ASSERT (v4l2_data.ptr ());
152 return v4l2_data->get_buf ();
Wind Yuan75564b12015-01-15 06:51:15 -0500153}
154
155};