• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
24 namespace XCam {
V4l2Buffer(const struct v4l2_buffer & buf,const struct v4l2_format & format)25 V4l2Buffer::V4l2Buffer (const struct v4l2_buffer &buf, const struct v4l2_format &format)
26 {
27     _buf = buf;
28     _format = format;
29 }
30 
~V4l2Buffer()31 V4l2Buffer::~V4l2Buffer ()
32 {
33 }
34 
35 uint8_t *
map()36 V4l2Buffer::map ()
37 {
38     if (_buf.memory == V4L2_MEMORY_DMABUF)
39         return NULL;
40     return (uint8_t *)(_buf.m.userptr);
41 }
42 
43 bool
unmap()44 V4l2Buffer::unmap ()
45 {
46     return true;
47 }
48 
49 int
get_fd()50 V4l2Buffer::get_fd ()
51 {
52     if (_buf.memory == V4L2_MEMORY_MMAP)
53         return -1;
54     return _buf.m.fd;
55 }
56 
V4l2BufferProxy(SmartPtr<V4l2Buffer> & buf,SmartPtr<V4l2Device> & device)57 V4l2BufferProxy::V4l2BufferProxy (SmartPtr<V4l2Buffer> &buf, SmartPtr<V4l2Device> &device)
58     : BufferProxy (buf)
59     , _device (device)
60 {
61     VideoBufferInfo info;
62     struct timeval ts = buf->get_buf().timestamp;
63 
64     v4l2_format_to_video_info (buf->get_format(), info);
65     set_video_info (info);
66     set_timestamp (XCAM_TIMEVAL_2_USEC (ts));
67 }
68 
~V4l2BufferProxy()69 V4l2BufferProxy::~V4l2BufferProxy ()
70 {
71     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);
75     XCAM_LOG_DEBUG ("v4l2 buffer released");
76 }
77 
78 void
v4l2_format_to_video_info(const struct v4l2_format & format,VideoBufferInfo & info)79 V4l2BufferProxy::v4l2_format_to_video_info (
80     const struct v4l2_format &format, VideoBufferInfo &info)
81 {
82     info.format = format.fmt.pix.pixelformat;
83     info.color_bits = 8;
84     info.width = format.fmt.pix.width;
85     info.height = format.fmt.pix.height;
86     info.aligned_width = 0;
87     info.aligned_height = 0;
88     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;
94         info.strides [1] = info.strides [0];
95         info.offsets[0] = 0;
96         info.offsets[1] = info.strides [0] * format.fmt.pix.height;
97         break;
98     case V4L2_PIX_FMT_YUV422P: // 422 Planar
99         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;
107     case V4L2_PIX_FMT_YUYV: // 422
108         info.components = 1;
109         info.strides [0] = format.fmt.pix.bytesperline;
110         info.offsets[0] = 0;
111         info.aligned_width = info.strides [0] / 2;
112         break;
113     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;
122     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;
131     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 
138     if (!info.aligned_width)
139         info.aligned_width = info.strides [0];
140 
141     if (!info.aligned_height)
142         info.aligned_height = info.height;
143 
144 }
145 
146 const struct v4l2_buffer &
get_v4l2_buf()147 V4l2BufferProxy::get_v4l2_buf ()
148 {
149     SmartPtr<BufferData> &data = get_buffer_data ();
150     SmartPtr<V4l2Buffer> v4l2_data = data.dynamic_cast_ptr<V4l2Buffer> ();
151     XCAM_ASSERT (v4l2_data.ptr ());
152     return v4l2_data->get_buf ();
153 }
154 
155 };
156