blob: 83fbbc7136a8064fd37f47db6063b698af615e77 [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{
52 return _buf.m.fd;
53}
54
Wind Yuan75564b12015-01-15 06:51:15 -050055V4l2BufferProxy::V4l2BufferProxy (SmartPtr<V4l2Buffer> &buf, SmartPtr<V4l2Device> &device)
Wind Yuan18d4cb52015-04-02 18:31:39 +080056 : BufferProxy (buf)
Wind Yuan75564b12015-01-15 06:51:15 -050057 , _device (device)
58{
59 VideoBufferInfo info;
60 struct timeval ts = buf->get_buf().timestamp;
61
Wind Yuan75564b12015-01-15 06:51:15 -050062 v4l2_format_to_video_info (buf->get_format(), info);
63 set_video_info (info);
64 set_timestamp (XCAM_TIMEVAL_2_USEC (ts));
65}
66
67V4l2BufferProxy::~V4l2BufferProxy ()
68{
Wind Yuan18d4cb52015-04-02 18:31:39 +080069 SmartPtr<BufferData> data = get_buffer_data ();
70 SmartPtr<V4l2Buffer> v4l2_data = data.dynamic_cast_ptr<V4l2Buffer> ();
71 if (_device.ptr () && v4l2_data.ptr ())
72 _device->queue_buffer (v4l2_data);
Wind Yuan75564b12015-01-15 06:51:15 -050073 XCAM_LOG_DEBUG ("v4l2 buffer released");
74}
75
76void
77V4l2BufferProxy::v4l2_format_to_video_info (
78 const struct v4l2_format &format, VideoBufferInfo &info)
79{
Wind Yuan75564b12015-01-15 06:51:15 -050080 info.format = format.fmt.pix.pixelformat;
Wind Yuanba55d872015-03-03 10:31:20 +080081 info.color_bits = 8;
Wind Yuan75564b12015-01-15 06:51:15 -050082 info.width = format.fmt.pix.width;
83 info.height = format.fmt.pix.height;
Wind Yuanada12e42015-03-25 17:26:56 +080084 info.aligned_width = 0;
85 info.aligned_height = 0;
Wind Yuan75564b12015-01-15 06:51:15 -050086 info.size = format.fmt.pix.sizeimage;
87 switch (format.fmt.pix.pixelformat) {
88 case V4L2_PIX_FMT_NV12: // 420
89 case V4L2_PIX_FMT_NV21:
90 info.components = 2;
91 info.strides [0] = format.fmt.pix.bytesperline * 2 / 3;
Wind Yuan817a06f2015-02-13 17:17:16 +080092 info.strides [1] = info.strides [0];
Wind Yuan75564b12015-01-15 06:51:15 -050093 info.offsets[0] = 0;
94 info.offsets[1] = info.strides [0] * format.fmt.pix.height;
95 break;
John Ye1503c6b2015-01-22 13:26:33 +080096 case V4L2_PIX_FMT_YUV422P: // 422 Planar
Wind Yuan75564b12015-01-15 06:51:15 -050097 info.components = 3;
98 info.strides [0] = format.fmt.pix.bytesperline / 2;
99 info.strides [1] = info.strides [0] / 2 ;
100 info.strides [2] = info.strides [0] / 2 ;
101 info.offsets[0] = 0;
102 info.offsets[1] = info.strides [0] * format.fmt.pix.height;
103 info.offsets[2] = info.offsets[1] + info.strides [1] * format.fmt.pix.height;
104 break;
John Ye1503c6b2015-01-22 13:26:33 +0800105 case V4L2_PIX_FMT_YUYV: // 422
106 info.components = 1;
107 info.strides [0] = format.fmt.pix.bytesperline;
108 info.offsets[0] = 0;
Wind Yuanada12e42015-03-25 17:26:56 +0800109 info.aligned_width = info.strides [0] / 2;
John Ye1503c6b2015-01-22 13:26:33 +0800110 break;
Wind Yuanba55d872015-03-03 10:31:20 +0800111 case V4L2_PIX_FMT_SBGGR10:
112 case V4L2_PIX_FMT_SGBRG10:
113 case V4L2_PIX_FMT_SGRBG10:
114 case V4L2_PIX_FMT_SRGGB10:
115 info.color_bits = 10;
116 info.components = 1;
117 info.strides [0] = format.fmt.pix.bytesperline;
118 info.offsets[0] = 0;
119 break;
ShincyTu1a4de9d2015-06-01 18:04:51 +0800120 case V4L2_PIX_FMT_SBGGR12:
121 case V4L2_PIX_FMT_SGBRG12:
122 case V4L2_PIX_FMT_SGRBG12:
123 case V4L2_PIX_FMT_SRGGB12:
124 info.color_bits = 12;
125 info.components = 1;
126 info.strides [0] = format.fmt.pix.bytesperline;
127 info.offsets[0] = 0;
128 break;
Wind Yuan75564b12015-01-15 06:51:15 -0500129 default:
130 XCAM_LOG_WARNING (
131 "unknown v4l2 format(%s) to video info",
132 xcam_fourcc_to_string (format.fmt.pix.pixelformat));
133 break;
134 }
135
Wind Yuanada12e42015-03-25 17:26:56 +0800136 if (!info.aligned_width)
137 info.aligned_width = info.strides [0];
138
139 if (!info.aligned_height)
140 info.aligned_height = info.height;
141
Wind Yuan75564b12015-01-15 06:51:15 -0500142}
143
144const struct v4l2_buffer &
Wind Yuanb90a89c2015-04-08 18:38:53 +0800145V4l2BufferProxy::get_v4l2_buf ()
Wind Yuan75564b12015-01-15 06:51:15 -0500146{
Wind Yuanb90a89c2015-04-08 18:38:53 +0800147 SmartPtr<BufferData> &data = get_buffer_data ();
Wind Yuan18d4cb52015-04-02 18:31:39 +0800148 SmartPtr<V4l2Buffer> v4l2_data = data.dynamic_cast_ptr<V4l2Buffer> ();
149 XCAM_ASSERT (v4l2_data.ptr ());
150 return v4l2_data->get_buf ();
Wind Yuan75564b12015-01-15 06:51:15 -0500151}
152
153};