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 | |
| 35 | V4l2BufferProxy::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 | |
| 48 | V4l2BufferProxy::~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 | |
| 56 | void |
| 57 | V4l2BufferProxy::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; |
| 71 | info.strides [1] = format.fmt.pix.bytesperline; |
| 72 | info.offsets[0] = 0; |
| 73 | info.offsets[1] = info.strides [0] * format.fmt.pix.height; |
| 74 | break; |
| 75 | case V4L2_PIX_FMT_YUV422P: // 422 |
| 76 | 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; |
| 84 | default: |
| 85 | XCAM_LOG_WARNING ( |
| 86 | "unknown v4l2 format(%s) to video info", |
| 87 | xcam_fourcc_to_string (format.fmt.pix.pixelformat)); |
| 88 | break; |
| 89 | } |
| 90 | |
| 91 | } |
| 92 | |
| 93 | const struct v4l2_buffer & |
| 94 | V4l2BufferProxy::get_v4l2_buf () const |
| 95 | { |
| 96 | XCAM_ASSERT (_buf.ptr()); |
| 97 | return _buf->get_buf (); |
| 98 | } |
| 99 | |
| 100 | uint8_t * |
| 101 | V4l2BufferProxy::map () |
| 102 | { |
| 103 | const struct v4l2_buffer & v4l2_buf = get_v4l2_buf (); |
| 104 | if (v4l2_buf.memory == V4L2_MEMORY_DMABUF) |
| 105 | return NULL; |
| 106 | return (uint8_t *)(v4l2_buf.m.userptr); |
| 107 | } |
| 108 | |
| 109 | bool |
| 110 | V4l2BufferProxy::unmap () |
| 111 | { |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | }; |