Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 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 { |
| 25 | V4l2Buffer::V4l2Buffer (const struct v4l2_buffer &buf, const struct v4l2_format &format) |
| 26 | { |
| 27 | _buf = buf; |
| 28 | _format = format; |
| 29 | } |
| 30 | |
| 31 | V4l2Buffer::~V4l2Buffer () |
| 32 | { |
| 33 | } |
| 34 | |
Wind Yuan | 18d4cb5 | 2015-04-02 18:31:39 +0800 | [diff] [blame] | 35 | uint8_t * |
| 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 |
| 44 | V4l2Buffer::unmap () |
| 45 | { |
| 46 | return true; |
| 47 | } |
| 48 | |
Jia Meng | 10a616d | 2015-04-23 20:57:09 +0000 | [diff] [blame] | 49 | int |
| 50 | V4l2Buffer::get_fd () |
| 51 | { |
Jia Meng | 813e02d | 2015-06-02 16:34:37 +0000 | [diff] [blame] | 52 | if (_buf.memory == V4L2_MEMORY_MMAP) |
Wind Yuan | 716ab94 | 2015-06-17 16:43:21 +0800 | [diff] [blame] | 53 | return -1; |
Jia Meng | 10a616d | 2015-04-23 20:57:09 +0000 | [diff] [blame] | 54 | return _buf.m.fd; |
| 55 | } |
| 56 | |
Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 57 | V4l2BufferProxy::V4l2BufferProxy (SmartPtr<V4l2Buffer> &buf, SmartPtr<V4l2Device> &device) |
Wind Yuan | 18d4cb5 | 2015-04-02 18:31:39 +0800 | [diff] [blame] | 58 | : BufferProxy (buf) |
Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 59 | , _device (device) |
| 60 | { |
| 61 | VideoBufferInfo info; |
| 62 | struct timeval ts = buf->get_buf().timestamp; |
| 63 | |
Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 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 | |
| 69 | V4l2BufferProxy::~V4l2BufferProxy () |
| 70 | { |
Wind Yuan | 18d4cb5 | 2015-04-02 18:31:39 +0800 | [diff] [blame] | 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); |
Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 75 | XCAM_LOG_DEBUG ("v4l2 buffer released"); |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | V4l2BufferProxy::v4l2_format_to_video_info ( |
| 80 | const struct v4l2_format &format, VideoBufferInfo &info) |
| 81 | { |
Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 82 | info.format = format.fmt.pix.pixelformat; |
Wind Yuan | ba55d87 | 2015-03-03 10:31:20 +0800 | [diff] [blame] | 83 | info.color_bits = 8; |
Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 84 | info.width = format.fmt.pix.width; |
| 85 | info.height = format.fmt.pix.height; |
Wind Yuan | ada12e4 | 2015-03-25 17:26:56 +0800 | [diff] [blame] | 86 | info.aligned_width = 0; |
| 87 | info.aligned_height = 0; |
Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 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; |
Wind Yuan | 817a06f | 2015-02-13 17:17:16 +0800 | [diff] [blame] | 94 | info.strides [1] = info.strides [0]; |
Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 95 | info.offsets[0] = 0; |
| 96 | info.offsets[1] = info.strides [0] * format.fmt.pix.height; |
| 97 | break; |
John Ye | 1503c6b | 2015-01-22 13:26:33 +0800 | [diff] [blame] | 98 | case V4L2_PIX_FMT_YUV422P: // 422 Planar |
Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 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; |
John Ye | 1503c6b | 2015-01-22 13:26:33 +0800 | [diff] [blame] | 107 | case V4L2_PIX_FMT_YUYV: // 422 |
| 108 | info.components = 1; |
| 109 | info.strides [0] = format.fmt.pix.bytesperline; |
| 110 | info.offsets[0] = 0; |
Wind Yuan | ada12e4 | 2015-03-25 17:26:56 +0800 | [diff] [blame] | 111 | info.aligned_width = info.strides [0] / 2; |
John Ye | 1503c6b | 2015-01-22 13:26:33 +0800 | [diff] [blame] | 112 | break; |
Wind Yuan | ba55d87 | 2015-03-03 10:31:20 +0800 | [diff] [blame] | 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; |
ShincyTu | 1a4de9d | 2015-06-01 18:04:51 +0800 | [diff] [blame] | 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; |
Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 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 | |
Wind Yuan | ada12e4 | 2015-03-25 17:26:56 +0800 | [diff] [blame] | 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 | |
Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | const struct v4l2_buffer & |
Wind Yuan | b90a89c | 2015-04-08 18:38:53 +0800 | [diff] [blame] | 147 | V4l2BufferProxy::get_v4l2_buf () |
Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 148 | { |
Wind Yuan | b90a89c | 2015-04-08 18:38:53 +0800 | [diff] [blame] | 149 | SmartPtr<BufferData> &data = get_buffer_data (); |
Wind Yuan | 18d4cb5 | 2015-04-02 18:31:39 +0800 | [diff] [blame] | 150 | SmartPtr<V4l2Buffer> v4l2_data = data.dynamic_cast_ptr<V4l2Buffer> (); |
| 151 | XCAM_ASSERT (v4l2_data.ptr ()); |
| 152 | return v4l2_data->get_buf (); |
Wind Yuan | 75564b1 | 2015-01-15 06:51:15 -0500 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | }; |