1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
/*
* Copyright Samsung Electronics Co.,LTD.
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hwjpeg-internal.h"
#include "LibScalerForJpeg.h"
#define SCALER_DEV_NODE "/dev/video50"
static const char *getBufTypeString(unsigned int buftype)
{
if (buftype == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
return "source";
if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
return "destination";
return "unknown";
}
LibScalerForJpeg::LibScalerForJpeg()
{
m_fdScaler = open(SCALER_DEV_NODE, O_RDWR);
if (m_fdScaler < 0) {
ALOGE("Failed to open %s", SCALER_DEV_NODE);
return;
}
mSrcImage.init(m_fdScaler);
mDstImage.init(m_fdScaler);
ALOGD("LibScalerForJpeg Created: %p", this);
}
LibScalerForJpeg::~LibScalerForJpeg()
{
if (m_fdScaler > 0)
close(m_fdScaler);
m_fdScaler = -1;
ALOGD("LibScalerForJpeg Destroyed: %p", this);
}
bool LibScalerForJpeg::RunStream(int srcBuf[SCALER_MAX_PLANES], int srcLen[SCALER_MAX_PLANES], int dstBuf, size_t dstLen)
{
if (!mSrcImage.begin(V4L2_MEMORY_DMABUF) || !mDstImage.begin(V4L2_MEMORY_DMABUF))
return false;
return queue(srcBuf, srcLen, dstBuf, dstLen);
}
bool LibScalerForJpeg::RunStream(char *srcBuf[SCALER_MAX_PLANES], int srcLen[SCALER_MAX_PLANES], int dstBuf, size_t dstLen)
{
if (!mSrcImage.begin(V4L2_MEMORY_USERPTR) || !mDstImage.begin(V4L2_MEMORY_DMABUF))
return false;
return queue(srcBuf, srcLen, dstBuf, dstLen);
}
bool LibScalerForJpeg::Image::set(unsigned int width, unsigned int height, unsigned int format)
{
if (same(width, height, format))
return true;
if (memoryType != 0) {
v4l2_requestbuffers reqbufs{};
reqbufs.type = bufferType;
reqbufs.memory = memoryType;
reqbufs.count = 0;
if (ioctl(mDevFd, VIDIOC_REQBUFS, &reqbufs) < 0) {
ALOGE("Failed to REQBUFS for(0) %s", getBufTypeString(bufferType));
return false;
}
}
v4l2_format fmt{};
fmt.type = bufferType;
fmt.fmt.pix_mp.pixelformat = format;
fmt.fmt.pix_mp.width = width;
fmt.fmt.pix_mp.height = height;
if (ioctl(mDevFd, VIDIOC_S_FMT, &fmt) < 0) {
ALOGE("Failed to S_FMT for %s", getBufTypeString(bufferType));
return false;
}
memoryType = 0; // new reqbufs is required.
return true;
}
bool LibScalerForJpeg::Image::begin(unsigned int memtype)
{
if (memoryType != memtype) {
v4l2_requestbuffers reqbufs{};
reqbufs.type = bufferType;
if (memoryType != 0) {
reqbufs.memory = memoryType;
reqbufs.count = 0;
if (ioctl(mDevFd, VIDIOC_REQBUFS, &reqbufs) < 0) {
ALOGERR("Failed to REQBUFS(0) for %s", getBufTypeString(bufferType));
return false;
}
}
reqbufs.memory = memtype;
reqbufs.count = 1;
if (ioctl(mDevFd, VIDIOC_REQBUFS, &reqbufs) < 0) {
ALOGERR("Failed to REQBUFS(1) for %s with %d", getBufTypeString(bufferType), reqbufs.memory);
return false;
}
if (ioctl(mDevFd, VIDIOC_STREAMON, &bufferType) < 0) {
ALOGERR("Failed to STREAMON for %s", getBufTypeString(bufferType));
return false;
}
memoryType = memtype;
}
return true;
}
bool LibScalerForJpeg::Image::cancelBuffer()
{
if (ioctl(mDevFd, VIDIOC_STREAMOFF, &bufferType) < 0) {
ALOGE("Failed to STREAMOFF for %s", getBufTypeString(bufferType));
return false;
}
if (ioctl(mDevFd, VIDIOC_STREAMON, &bufferType) < 0) {
ALOGE("Failed to STREAMON for %s", getBufTypeString(bufferType));
return false;
}
return true;
}
bool LibScalerForJpeg::Image::queueBuffer(int buf, size_t len)
{
v4l2_buffer buffer{};
v4l2_plane plane[SCALER_MAX_PLANES];
memset(&plane, 0, sizeof(plane));
buffer.type = bufferType;
buffer.memory = memoryType;
buffer.length = 1;
buffer.m.planes = plane;
plane[0].m.fd = buf;
plane[0].length = len;
if (ioctl(mDevFd, VIDIOC_QBUF, &buffer) < 0) {
ALOGERR("Failed to QBUF for %s", getBufTypeString(bufferType));
return false;
}
return true;
}
bool LibScalerForJpeg::Image::queueBuffer(int buf[SCALER_MAX_PLANES], int len[SCALER_MAX_PLANES])
{
v4l2_buffer buffer{};
v4l2_plane plane[SCALER_MAX_PLANES];
memset(&plane, 0, sizeof(plane));
buffer.type = bufferType;
buffer.memory = memoryType;
buffer.length = SCALER_MAX_PLANES;
for (unsigned int i = 0; i < SCALER_MAX_PLANES; i++) {
plane[i].m.fd = buf[i];
plane[i].length = len[i];
}
buffer.m.planes = plane;
if (ioctl(mDevFd, VIDIOC_QBUF, &buffer) < 0) {
ALOGERR("Failed to QBUF for %s", getBufTypeString(bufferType));
return false;
}
return true;
}
bool LibScalerForJpeg::Image::queueBuffer(char *buf[SCALER_MAX_PLANES], int len[SCALER_MAX_PLANES])
{
v4l2_buffer buffer{};
v4l2_plane plane[SCALER_MAX_PLANES];
memset(&plane, 0, sizeof(plane));
buffer.type = bufferType;
buffer.memory = memoryType;
buffer.length = SCALER_MAX_PLANES;
for (unsigned int i = 0; i < SCALER_MAX_PLANES; i++) {
plane[i].m.userptr = reinterpret_cast<unsigned long>(buf[i]);
plane[i].length = len[i];
}
buffer.m.planes = plane;
if (ioctl(mDevFd, VIDIOC_QBUF, &buffer) < 0) {
ALOGERR("Failed to QBUF for %s", getBufTypeString(bufferType));
return false;
}
return true;
}
bool LibScalerForJpeg::Image::dequeueBuffer()
{
v4l2_buffer buffer{};
v4l2_plane plane[SCALER_MAX_PLANES];
memset(&plane, 0, sizeof(plane));
buffer.type = bufferType;
buffer.memory = memoryType;
buffer.length = SCALER_MAX_PLANES;
buffer.m.planes = plane;
if (ioctl(mDevFd, VIDIOC_DQBUF, &buffer) < 0 ) {
ALOGERR("Failed to DQBuf %s", getBufTypeString(bufferType));
return false;
}
return true;
}
|