blob: e6c38452be2fc6eece122cfbad8f5fd211bb9886 [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
35V4l2BufferProxy::V4l2BufferProxy (SmartPtr<V4l2Buffer> &buf, SmartPtr<V4l2Device> &device)
36 : _buf (buf)
37 , _device (device)
38{
39 VideoBufferInfo info;
40 struct timeval ts = buf->get_buf().timestamp;
41
42 xcam_mem_clear (&info);
43 v4l2_format_to_video_info (buf->get_format(), info);
44 set_video_info (info);
45 set_timestamp (XCAM_TIMEVAL_2_USEC (ts));
46}
47
48V4l2BufferProxy::~V4l2BufferProxy ()
49{
50 XCAM_ASSERT (_buf.ptr());
51 if (_device.ptr())
52 _device->queue_buffer (_buf);
53 XCAM_LOG_DEBUG ("v4l2 buffer released");
54}
55
56void
57V4l2BufferProxy::v4l2_format_to_video_info (
58 const struct v4l2_format &format, VideoBufferInfo &info)
59{
60 xcam_mem_clear (&info);
61
62 info.format = format.fmt.pix.pixelformat;
63 info.width = format.fmt.pix.width;
64 info.height = format.fmt.pix.height;
65 info.size = format.fmt.pix.sizeimage;
66 switch (format.fmt.pix.pixelformat) {
67 case V4L2_PIX_FMT_NV12: // 420
68 case V4L2_PIX_FMT_NV21:
69 info.components = 2;
70 info.strides [0] = format.fmt.pix.bytesperline * 2 / 3;
Wind Yuan817a06f2015-02-13 17:17:16 +080071 info.strides [1] = info.strides [0];
Wind Yuan75564b12015-01-15 06:51:15 -050072 info.offsets[0] = 0;
73 info.offsets[1] = info.strides [0] * format.fmt.pix.height;
74 break;
John Ye1503c6b2015-01-22 13:26:33 +080075 case V4L2_PIX_FMT_YUV422P: // 422 Planar
Wind Yuan75564b12015-01-15 06:51:15 -050076 info.components = 3;
77 info.strides [0] = format.fmt.pix.bytesperline / 2;
78 info.strides [1] = info.strides [0] / 2 ;
79 info.strides [2] = info.strides [0] / 2 ;
80 info.offsets[0] = 0;
81 info.offsets[1] = info.strides [0] * format.fmt.pix.height;
82 info.offsets[2] = info.offsets[1] + info.strides [1] * format.fmt.pix.height;
83 break;
John Ye1503c6b2015-01-22 13:26:33 +080084 case V4L2_PIX_FMT_YUYV: // 422
85 info.components = 1;
86 info.strides [0] = format.fmt.pix.bytesperline;
87 info.offsets[0] = 0;
88 break;
Wind Yuan75564b12015-01-15 06:51:15 -050089 default:
90 XCAM_LOG_WARNING (
91 "unknown v4l2 format(%s) to video info",
92 xcam_fourcc_to_string (format.fmt.pix.pixelformat));
93 break;
94 }
95
96}
97
98const struct v4l2_buffer &
99V4l2BufferProxy::get_v4l2_buf () const
100{
101 XCAM_ASSERT (_buf.ptr());
102 return _buf->get_buf ();
103}
104
105uint8_t *
106V4l2BufferProxy::map ()
107{
108 const struct v4l2_buffer & v4l2_buf = get_v4l2_buf ();
109 if (v4l2_buf.memory == V4L2_MEMORY_DMABUF)
110 return NULL;
111 return (uint8_t *)(v4l2_buf.m.userptr);
112}
113
114bool
115V4l2BufferProxy::unmap ()
116{
117 return true;
118}
119
120};