summaryrefslogtreecommitdiffstats
path: root/exynos4/hal/libhwjpeg/jpeg_hal_unit.c
blob: 00363f5d26513679a3d0e857d33c5fabe4a195b1 (plain)
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/*
 * Copyright@ Samsung Electronics Co. LTD
 *
 * 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.
 */

#define ALOG_TAG "libhwjpeg"

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/mman.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <math.h>
#include <sys/poll.h>

#include <cutils/log.h>

#include "jpeg_hal.h"

#ifdef JPEG_PERF_MEAS
unsigned long measure_time(struct timeval *start, struct timeval *stop)
{
    unsigned long sec, usec, time;

    sec = stop->tv_sec - start->tv_sec;

    if (stop->tv_usec >= start->tv_usec) {
        usec = stop->tv_usec - start->tv_usec;
    } else {
        usec = stop->tv_usec + 1000000 - start->tv_usec;
        sec--;
    }

    time = (sec * 1000000) + usec;

    return time;
}
#endif

static int jpeg_v4l2_querycap(int fd)
{
    struct v4l2_capability cap;
    int ret = 0;

    ret = ioctl(fd, VIDIOC_QUERYCAP, &cap);

    if (!(cap.capabilities & V4L2_CAP_STREAMING))
        ALOGE("[%s]: does not support streaming", __func__);

    if (!(cap.capabilities & V4L2_CAP_VIDEO_OUTPUT))
        ALOGE("[%s]: does not support output", __func__);

    if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
        ALOGE("[%s]: does not support capture", __func__);

    return ret;
}

static int jpeg_v4l2_s_jpegcomp(int fd, int quality)
{
    struct v4l2_jpegcompression arg;
    int ret = 0;

    arg.quality = quality;

    ret = ioctl(fd, VIDIOC_S_JPEGCOMP, &arg);

    return ret;
}

static int jpeg_v4l2_s_fmt(int fd, enum v4l2_buf_type type, struct jpeg_config *config)
{
    struct v4l2_format fmt;
    int ret = 0;

    fmt.type = type;
    fmt.fmt.pix_mp.width = config->width;
    fmt.fmt.pix_mp.height = config->height;
    fmt.fmt.pix_mp.field = V4L2_FIELD_ANY;
    fmt.fmt.pix_mp.num_planes = config->num_planes;

    if (config->mode == JPEG_ENCODE)
        fmt.fmt.pix_mp.colorspace = V4L2_COLORSPACE_JPEG;

    switch (fmt.type) {
    case V4L2_BUF_TYPE_VIDEO_OUTPUT:    /* fall through */
    case V4L2_BUF_TYPE_VIDEO_CAPTURE:
        break;
    case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
        if (config->mode == JPEG_ENCODE) {
            fmt.fmt.pix_mp.pixelformat = config->pix.enc_fmt.in_fmt;
        } else {
            fmt.fmt.pix_mp.pixelformat = config->pix.dec_fmt.in_fmt;
            fmt.fmt.pix_mp.plane_fmt[0].sizeimage = config->sizeJpeg;
        }
        break;
    case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
        if (config->mode == JPEG_ENCODE) {
            fmt.fmt.pix_mp.pixelformat = config->pix.enc_fmt.out_fmt;
        } else {
            fmt.fmt.pix_mp.pixelformat = config->pix.dec_fmt.out_fmt;
            fmt.fmt.pix_mp.width = config->scaled_width;
            fmt.fmt.pix_mp.height = config->scaled_height;
        }
        break;
    default:
            ALOGE("[%s]: invalid v4l2 buf type", __func__);
            return -1;
    }

    ret = ioctl(fd, VIDIOC_S_FMT, &fmt);

    return ret;
}

static int jpeg_v4l2_g_fmt(int fd, enum v4l2_buf_type type, struct jpeg_config *config)
{
    struct v4l2_format fmt;
    int ret = 0;

    fmt.type = type;
    ret = ioctl(fd, VIDIOC_G_FMT, &fmt);
    if (ret < 0)
        return -1;

    config->width = fmt.fmt.pix_mp.width;
    config->height = fmt.fmt.pix_mp.height;

    switch (fmt.type) {
    case V4L2_BUF_TYPE_VIDEO_OUTPUT:    /* fall through */
    case V4L2_BUF_TYPE_VIDEO_CAPTURE:
        break;
    case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
        if (config->mode == JPEG_ENCODE)
            config->pix.enc_fmt.in_fmt = fmt.fmt.pix_mp.pixelformat;
        else
            config->pix.dec_fmt.in_fmt = fmt.fmt.pix_mp.pixelformat;
        break;
    case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
        if (config->mode == JPEG_ENCODE)
            config->pix.enc_fmt.out_fmt = fmt.fmt.pix_mp.pixelformat;
        else
            config->pix.dec_fmt.out_fmt = fmt.fmt.pix_mp.pixelformat;
        break;
    default:
        ALOGE("[%s]: invalid v4l2 buf type", __func__);
        return -1;
    }

    return ret;
}

int jpeghal_getconfig(int fd, struct jpeg_config *config)
{
    int ret = 0;

    ret = jpeg_v4l2_g_fmt(fd, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, config);
    if (ret < 0) {
        ALOGE("[%s]: input G_FMT failed", __func__);
        return -1;
    }

    ret = jpeg_v4l2_g_fmt(fd, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, config);
    if (ret < 0)
        ALOGE("[%s]: output G_FMT failed", __func__);

    return ret;
}

static int jpeg_v4l2_reqbufs(int fd, int buf_cnt, struct jpeg_buf *buf)
{
    struct v4l2_requestbuffers req;
    int ret = 0;

    memset(&req, 0, sizeof(req));

    req.type = buf->buf_type;
    req.memory = buf->memory;

    req.count = buf_cnt;

    ret = ioctl(fd, VIDIOC_REQBUFS, &req);

    return ret;
}

static int jpeg_v4l2_querybuf(int fd, struct jpeg_buf *buf)
{
    struct v4l2_buffer v4l2_buf;
    struct v4l2_plane plane[JPEG_MAX_PLANE_CNT];
    int i;
    int ret = 0;

    memset(plane, 0, (int)JPEG_MAX_PLANE_CNT * sizeof(struct v4l2_plane));

    v4l2_buf.index = 0;
    v4l2_buf.type = buf->buf_type;
    v4l2_buf.memory = buf->memory;
    v4l2_buf.length = buf->num_planes;
    v4l2_buf.m.planes = plane;

    ret = ioctl(fd, VIDIOC_QUERYBUF, &v4l2_buf);
    if (ret < 0) {
        ALOGE("[%s:%d]: VIDIOC_QUERYBUF failed", __func__, ret);
        return ret;
    }

    for (i= 0; i < buf->num_planes; i++) {
        buf->length[i] = v4l2_buf.m.planes[i].length;
        buf->start[i] = (char *) mmap(0, buf->length[i],
                    PROT_READ | PROT_WRITE, MAP_SHARED, fd,
                    v4l2_buf.m.planes[i].m.mem_offset);

        //ALOGI("[%s]: buf.start[%d] = %p, length = %d", __func__, 0, buf->start[0], buf->length[0]);
        if (buf->start[0] == MAP_FAILED) {
            ALOGE("[%s]: mmap failed", __func__);
            return -1;
        }
    }

    return ret;
}

static int jpeg_v4l2_qbuf(int fd, struct jpeg_buf *buf)
{
    struct v4l2_buffer v4l2_buf;
    struct v4l2_plane plane[JPEG_MAX_PLANE_CNT];
    int i;
    int ret = 0;

    memset(&v4l2_buf, 0, sizeof(struct v4l2_buffer));
    memset(plane, 0, (int)JPEG_MAX_PLANE_CNT * sizeof(struct v4l2_plane));

    v4l2_buf.index = 0;
    v4l2_buf.type = buf->buf_type;
    v4l2_buf.memory = buf->memory;
    v4l2_buf.length = buf->num_planes;
    v4l2_buf.m.planes = plane;

    if (buf->memory == V4L2_MEMORY_USERPTR) {
        for (i = 0; i < buf->num_planes; i++) {
            v4l2_buf.m.planes[i].m.userptr = (unsigned long)buf->start[i];
            v4l2_buf.m.planes[i].length = buf->length[i];
        }
    }

    ret = ioctl(fd, VIDIOC_QBUF, &v4l2_buf);
    if (ret < 0) {
        ALOGE("[%s:%d] QBUF failed", __func__, ret);
        return -1;
    }

    return ret;
}

static int jpeg_v4l2_dqbuf(int fd, enum v4l2_buf_type type, enum v4l2_memory memory)
{
    struct v4l2_buffer buf;
    int ret = 0;

    memset(&buf, 0, sizeof(struct v4l2_buffer));

    buf.type = type;
    buf.memory = memory;

    ret = ioctl(fd, VIDIOC_DQBUF, &buf);
    if (ret < 0) {
        ALOGE("[%s:%d] DQBUF failed", __func__, ret);
        return -1;
    }

    return ret;
}

static int jpeg_v4l2_streamon(int fd, enum v4l2_buf_type type)
{
    int ret = 0;

    ret = ioctl(fd, VIDIOC_STREAMON, &type);
    if (ret < 0) {
        ALOGE("[%s:%d] STREAMON failed", __func__, ret);
        return -1;
    }

    return ret;
}

static int jpeg_v4l2_streamoff(int fd, enum v4l2_buf_type type)
{
    int ret = 0;

    ret = ioctl(fd, VIDIOC_STREAMOFF, &type);
    if (ret < 0) {
        ALOGE("[%s:%d] STREAMOFF failed", __func__, ret);
        return -1;
    }

    return ret;
}

int jpeghal_dec_init()
{
    int fd;
    int ret = 0;

    fd = open(JPEG_DEC_NODE, O_RDWR, 0);

    if (fd < 0) {
        ALOGE("[%s]: JPEG dec open failed", __func__);
        return -1;
    }

    ret = jpeg_v4l2_querycap(fd);
    if (ret < 0) {
        ALOGE("[%s]: QUERYCAP failed", __func__);
        return -1;
    }

    return fd;
}

int jpeghal_enc_init()
{
    int fd;
    int ret = 0;

    fd = open(JPEG_ENC_NODE, O_RDWR, 0);
    if (fd < 0) {
        ALOGE("[%s]: JPEG enc open failed", __func__);
        return -1;
    }

    ret = jpeg_v4l2_querycap(fd);
    if (ret < 0) {
        ALOGE("[%s]: QUERYCAP failed", __func__);
        return -1;
    }

    return fd;
}

int jpeghal_dec_setconfig(int fd, struct jpeg_config *config)
{
    int ret = 0;

    config->mode = JPEG_DECODE;

    ret = jpeg_v4l2_s_fmt(fd, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, config);
    if (ret < 0) {
        ALOGE("[%s]: decoder input S_FMT failed", __func__);
        return -1;
    }

    ret = jpeg_v4l2_s_fmt(fd, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, config);
    if (ret < 0) {
        ALOGE("[%s]: decoder output S_FMT failed", __func__);
        return -1;
    }

    return ret;
}

int jpeghal_dec_getconfig(int fd, struct jpeg_config *config)
{
    int ret = 0;

    jpeghal_getconfig(fd, config);

    return ret;
}

int jpeghal_enc_setconfig(int fd, struct jpeg_config *config)
{
    int ret = 0;

    ret = jpeg_v4l2_s_jpegcomp(fd, config->enc_qual);
    if (ret < 0) {
        ALOGE("[%s]: S_JPEGCOMP failed", __func__);
        return -1;
    }

    config->mode    = JPEG_ENCODE;

    ret = jpeg_v4l2_s_fmt(fd, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, config);
    if (ret < 0) {
        ALOGE("[%s]: encoder input S_FMT failed", __func__);
        return -1;
    }

    ret = jpeg_v4l2_s_fmt(fd, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, config);
    if (ret < 0) {
        ALOGE("[%s]: encoder output S_FMT failed", __func__);
        return -1;
    }

    return ret;
}

int jpeghal_enc_getconfig(int fd, struct jpeg_config *config)
{
    int ret = 0;

    jpeghal_getconfig(fd, config);

    return ret;
}

int jpeghal_set_inbuf(int fd, struct jpeg_buf *buf)
{
    int ret = 0;

    buf->buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;

    ret = jpeg_v4l2_reqbufs(fd, 1, buf);
    if (ret < 0) {
        ALOGE("[%s:%d]: Input REQBUFS failed", __func__, ret);
        return -1;
    }

    if (buf->memory == V4L2_MEMORY_MMAP) {
        ret = jpeg_v4l2_querybuf(fd, buf);
        if (ret < 0) {
            ALOGE("[%s:%d]: Input QUERYBUF failed", __func__, ret);
            return -1;
        }
    }

    return ret;
}

int jpeghal_set_outbuf(int fd, struct jpeg_buf *buf)
{
    int ret = 0;

    buf->buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;

    ret = jpeg_v4l2_reqbufs(fd, 1, buf);
    if (ret < 0) {
        ALOGE("[%s:%d]: Output REQBUFS failed", __func__, ret);
        return -1;
    }

    if (buf->memory == V4L2_MEMORY_MMAP) {
        ret = jpeg_v4l2_querybuf(fd, buf);
        if (ret < 0) {
            ALOGE("[%s:%d]: Output QUERYBUF failed", __func__, ret);
            return -1;
        }
    }

    return ret;
}

static int jpeg_exe(int fd, struct jpeg_buf *in_buf, struct jpeg_buf *out_buf)
{
    int ret = 0;

    ret = jpeg_v4l2_qbuf(fd, in_buf);
    if (ret < 0) {
        ALOGE("[%s:%d]: Input QBUF failed", __func__, ret);
        return -1;
    }

    ret = jpeg_v4l2_qbuf(fd, out_buf);
    if (ret < 0) {
        ALOGE("[%s:%d]: Output QBUF failed", __func__, ret);
        return -1;
    }

    ret = jpeg_v4l2_streamon(fd, in_buf->buf_type);
    ret = jpeg_v4l2_streamon(fd, out_buf->buf_type);

    ret = jpeg_v4l2_dqbuf(fd, in_buf->buf_type, in_buf->memory);
    ret = jpeg_v4l2_dqbuf(fd, out_buf->buf_type, out_buf->memory);

    return ret;
}

int jpeghal_dec_exe(int fd, struct jpeg_buf *in_buf, struct jpeg_buf *out_buf)
{
    int ret = 0;

    ret = jpeg_exe(fd, in_buf, out_buf);
    if (ret < 0)
        ALOGE("[%s]: JPEG decoding is failed", __func__);

    return ret;
}

int jpeghal_enc_exe(int fd, struct jpeg_buf *in_buf, struct jpeg_buf *out_buf)
{
    int ret = 0;

    ret = jpeg_exe(fd, in_buf, out_buf);
    if (ret < 0)
        ALOGE("[%s]: JPEG Encoding is failed", __func__);

    return ret;
}

int jpeghal_deinit(int fd, struct jpeg_buf *in_buf, struct jpeg_buf *out_buf)
{
    int ret = 0;

    jpeg_v4l2_streamoff(fd, in_buf->buf_type);
    jpeg_v4l2_streamoff(fd, out_buf->buf_type);

    if (in_buf->memory == V4L2_MEMORY_MMAP)
        munmap((char *)(in_buf->start[0]), in_buf->length[0]);

    if (out_buf->memory == V4L2_MEMORY_MMAP)
        munmap((char *)(out_buf->start[0]), out_buf->length[0]);

    jpeg_v4l2_reqbufs(fd, 0, in_buf);

    jpeg_v4l2_reqbufs(fd, 0, out_buf);

    ret = close(fd);

    return ret;
}

int jpeghal_s_ctrl(int fd, int cid, int value)
{
    struct v4l2_control vc;
    int ret = 0;

    vc.id = cid;
    vc.value = value;

    ret = ioctl(fd, VIDIOC_S_CTRL, &vc);
    if (ret != 0) {
        ALOGE("[%s] ioctl : cid(%d), value(%d)\n", __func__, cid, value);
        return -1;
    }

    return ret;
}

int jpeghal_g_ctrl(int fd, int id)
{
    struct v4l2_control ctrl;
    int ret = 0;

    ctrl.id = id;

    ret = ioctl(fd, VIDIOC_G_CTRL, &ctrl);
    if (ret < 0) {
        ALOGE("[%s] ioctl : cid(%d)\n", __func__, ctrl.id);
        return -1;
    }

    return ctrl.value;
}