summaryrefslogtreecommitdiffstats
path: root/exynos4/hal/libs5pjpeg/jpeg_api.c
blob: e5247e3bfc70690a5c33e8b050058507a1304da6 (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
/*
**
** Copyright 2008, The Android Open Source Project
** Copyright 2010, 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 LOG_NDEBUG 0
#define LOG_TAG "Jpeg-api"

#include <utils/Log.h>
#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 <sys/time.h>
#include <signal.h>
#include <math.h>
#include <sys/poll.h>

#ifdef S5P_VMEM
#include "s5p_vmem_api.h"
#endif
#include "jpeg_api.h"

static struct jpeg_lib  *jpeg_ctx = NULL;
#ifdef S5P_VMEM
static int mem_fp;
unsigned int cookie;
#endif /* S5P_VMEM */

static unsigned int get_yuv_size(enum jpeg_frame_format out_format,
                                unsigned int width, unsigned int height)
{
    switch (out_format) {
    case YUV_422 :
        if (width % 16 != 0)
            width += 16 - (width % 16);
        if (height % 8 != 0)
            height += 8 - (height % 8);
        break;

    case YUV_420 :
        if (width % 16 != 0)
            width += 16 - (width % 16);
        if (height % 16 != 0)
            height += 16 - (height % 16);
        break;

    default:
        ALOGV("get_yuv_size return fmt(%d)\n", out_format);
        return(0);
    }

    ALOGV("get_yuv_size width(%d) height(%d)\n", width, height);

    switch (out_format) {
    case YUV_422 :
        return(width*height*2);
    case YUV_420 :
        return((width*height) + (width*height >> 1));
    default :
        return(0);
    }
}

static int check_input_size(unsigned int width, unsigned int height)
{
    if ((width % 16) != 0 || (height % 8) != 0)
        return -1;

    return 0;
}

static void init_decode_param(void)
{
    jpeg_ctx = (struct jpeg_lib *)malloc(sizeof(struct jpeg_lib));
    memset(jpeg_ctx, 0x00, sizeof(struct jpeg_lib));

    jpeg_ctx->args.dec_param = (struct jpeg_dec_param *)malloc(sizeof(struct jpeg_dec_param));
    memset(jpeg_ctx->args.dec_param, 0x00, sizeof(struct jpeg_dec_param));
}

static void init_encode_param(void)
{
    jpeg_ctx = (struct jpeg_lib *)malloc(sizeof(struct jpeg_lib));
    memset(jpeg_ctx, 0x00, sizeof(struct jpeg_lib));

    jpeg_ctx->args.enc_param = (struct jpeg_enc_param *)malloc(sizeof(struct jpeg_enc_param));
    memset(jpeg_ctx->args.enc_param, 0x00, sizeof(struct jpeg_enc_param));
}

int api_jpeg_decode_init()
{
    init_decode_param();
    jpeg_ctx->jpeg_fd = open(JPEG_DRIVER_NAME, O_RDWR);

    if (jpeg_ctx->jpeg_fd < 0) {
        ALOGE("JPEG driver open failed\n");
        return -1;
    }

#ifdef S5P_VMEM
    mem_fp = s5p_vmem_open();
    ALOGV("s5p_vmem_open\n");
#else
    jpeg_ctx->args.mmapped_addr = (char *) mmap(0,
                        JPEG_TOTAL_BUF_SIZE,
                        PROT_READ | PROT_WRITE,
                        MAP_SHARED,
                        jpeg_ctx->jpeg_fd, 0);

    if (jpeg_ctx->args.mmapped_addr == NULL) {
        ALOGE("JPEG mmap failed\n");
        return -1;
    }
    ALOGV("api_jpeg_decode_init jpeg_ctx->args.mmapped_addr 0x%08x\n",
                                                jpeg_ctx->args.mmapped_addr);
#endif /* S5P_VMEM */

    return jpeg_ctx->jpeg_fd;
}

int api_jpeg_encode_init()
{
    init_encode_param();
    jpeg_ctx->jpeg_fd = open(JPEG_DRIVER_NAME, O_RDWR);

    if (jpeg_ctx->jpeg_fd < 0) {
        ALOGE("JPEG driver open failed %d\n", jpeg_ctx->jpeg_fd);
        return -1;
    }

#ifdef S5P_VMEM
    mem_fp = s5p_vmem_open();
    ALOGI("s5p_vmem_open\n");
#else

    jpeg_ctx->args.mmapped_addr = (char *) mmap(0,
            JPEG_TOTAL_BUF_SIZE,
            PROT_READ | PROT_WRITE,
            MAP_SHARED,
            jpeg_ctx->jpeg_fd, 0);

    if (jpeg_ctx->args.mmapped_addr == NULL) {
        ALOGE("JPEG mmap failed\n");
        return -1;
    }
    ALOGV("api_jpeg_encode_init jpeg_ctx->args.mmapped_addr 0x%08x\n",
                                                jpeg_ctx->args.mmapped_addr);
#endif /* S5P_VMEM */
    return jpeg_ctx->jpeg_fd;
}

int api_jpeg_decode_deinit(int dev_fd)
{
    if (jpeg_ctx->args.mmapped_addr != NULL)
        munmap(jpeg_ctx->args.mmapped_addr, JPEG_TOTAL_BUF_SIZE);

#ifdef S5P_VMEM
    s5p_free_share(mem_fp, jpeg_ctx->args.in_cookie, jpeg_ctx->args.in_buf);
    s5p_free_share(mem_fp, jpeg_ctx->args.out_cookie, jpeg_ctx->args.out_buf);
    s5p_vmem_close(mem_fp);
#endif

    close(jpeg_ctx->jpeg_fd);

    if (jpeg_ctx->args.dec_param != NULL)
        free(jpeg_ctx->args.dec_param);

    free(jpeg_ctx);

    return JPEG_OK;
}

int api_jpeg_encode_deinit(int dev_fd)
{
    if (jpeg_ctx->args.mmapped_addr != NULL)
        munmap(jpeg_ctx->args.mmapped_addr, JPEG_TOTAL_BUF_SIZE);

#ifdef S5P_VMEM
    s5p_free_share(mem_fp, jpeg_ctx->args.in_cookie, jpeg_ctx->args.in_buf);
    s5p_free_share(mem_fp, jpeg_ctx->args.out_cookie, jpeg_ctx->args.out_buf);
    s5p_vmem_close(mem_fp);
#endif
    close(jpeg_ctx->jpeg_fd);

    if (jpeg_ctx->args.enc_param != NULL)
        free(jpeg_ctx->args.enc_param);

    free(jpeg_ctx);

    return JPEG_OK;
}

void *api_jpeg_get_decode_in_buf(int dev_fd, unsigned int size)
{
    if (size < 0 || size > MAX_JPEG_RES) {
        ALOGE("Invalid decode input buffer size\r\n");
        return NULL;
    }
#ifdef S5P_VMEM
    jpeg_ctx->args.in_cookie = (unsigned int)ioctl(jpeg_ctx->jpeg_fd,
                                IOCTL_GET_DEC_IN_BUF, size);
    jpeg_ctx->args.in_buf = s5p_malloc_share(mem_fp,
                                        jpeg_ctx->args.in_cookie,
                                        &jpeg_ctx->args.in_buf_size);
#else
    jpeg_ctx->args.in_buf = (char *)ioctl(jpeg_ctx->jpeg_fd,
                                IOCTL_GET_DEC_IN_BUF,
                                jpeg_ctx->args.mmapped_addr);
#endif /* S5P_VMEM */
    return (void *)(jpeg_ctx->args.in_buf);
}

void *api_jpeg_get_encode_in_buf(int dev_fd, unsigned int size)
{
#ifdef S5P_VMEM
    jpeg_ctx->args.in_cookie = (unsigned int)ioctl(jpeg_ctx->jpeg_fd,
                                IOCTL_GET_ENC_IN_BUF, (size*3));
    jpeg_ctx->args.in_buf = s5p_malloc_share(mem_fp,
                                        jpeg_ctx->args.in_cookie,
                                        &jpeg_ctx->args.in_buf_size);
#else
    jpeg_ctx->args.enc_param->size = size;
    jpeg_ctx->args.in_buf = (char *)ioctl(jpeg_ctx->jpeg_fd,
                                IOCTL_GET_ENC_IN_BUF,
                                jpeg_ctx->args.mmapped_addr);
#endif

    ALOGV("api_jpeg_get_encode_in_buf: 0x%x\n",
                        jpeg_ctx->args.in_buf);

    return (void *)(jpeg_ctx->args.in_buf);
}

void *api_jpeg_get_decode_out_buf(int dev_fd)
{
#ifdef S5P_VMEM
    jpeg_ctx->args.out_cookie = (unsigned int)ioctl(jpeg_ctx->jpeg_fd,
                                IOCTL_GET_DEC_OUT_BUF, JPEG_FRAME_BUF_SIZE);
    jpeg_ctx->args.out_buf = s5p_malloc_share(mem_fp,
                                        jpeg_ctx->args.out_cookie,
                                        &jpeg_ctx->args.out_buf_size);
#else
    jpeg_ctx->args.out_buf = (char *)ioctl(jpeg_ctx->jpeg_fd,
                                IOCTL_GET_DEC_OUT_BUF,
                                jpeg_ctx->args.mmapped_addr);
#endif /* S5P_VMEM */
    /*
    ALOGV("api_jpeg_get_decode_out_buf: 0x%x\n",
                        jpeg_ctx->args.out_buf);
    */
    return (void *)(jpeg_ctx->args.out_buf);
}

void *api_jpeg_get_encode_out_buf(int dev_fd)
{
#ifdef S5P_VMEM
    jpeg_ctx->args.out_cookie = (unsigned int)ioctl(jpeg_ctx->jpeg_fd,
                                IOCTL_GET_ENC_OUT_BUF, JPEG_STREAM_BUF_SIZE);
    jpeg_ctx->args.out_buf = s5p_malloc_share(mem_fp,
                                        jpeg_ctx->args.out_cookie,
                                        &jpeg_ctx->args.out_buf_size);
#else
    jpeg_ctx->args.out_buf = (char *)ioctl(jpeg_ctx->jpeg_fd,
                                IOCTL_GET_ENC_OUT_BUF,
                                jpeg_ctx->args.mmapped_addr);
#endif /* S5P_VMEM */

    ALOGV("api_jpeg_get_encode_out_buf: 0x%x\n",
                        jpeg_ctx->args.out_buf);

    return (void *)(jpeg_ctx->args.out_buf);
}

void api_jpeg_set_decode_param(struct jpeg_dec_param *param)
{
    memcpy(jpeg_ctx->args.dec_param, param, sizeof(struct jpeg_dec_param));
    ioctl(jpeg_ctx->jpeg_fd, IOCTL_SET_DEC_PARAM, jpeg_ctx->args.dec_param);
}

void api_jpeg_set_encode_param(struct jpeg_enc_param *param)
{
    memcpy(jpeg_ctx->args.enc_param, param, sizeof(struct jpeg_enc_param));
    ioctl(jpeg_ctx->jpeg_fd, IOCTL_SET_ENC_PARAM, jpeg_ctx->args.enc_param);
}

enum jpeg_ret_type api_jpeg_decode_exe(int dev_fd,
                                    struct jpeg_dec_param *dec_param)
{
    struct jpeg_args *arg;

    arg = &(jpeg_ctx->args);

    ioctl(jpeg_ctx->jpeg_fd, IOCTL_JPEG_DEC_EXE, arg->dec_param);
    ALOGV("api_jpeg_decode_exe dec_param->out_fmt :%d \
                        dec_param->width : %d dec_param->height : %d\n",
                        arg->dec_param->out_fmt,
                        arg->dec_param->width,
                        arg->dec_param->height);
    dec_param->width = arg->dec_param->width;
    dec_param->height = arg->dec_param->height;
    dec_param->size = get_yuv_size(arg->dec_param->out_fmt,
                                arg->dec_param->width,
                                arg->dec_param->height);

    return JPEG_DECODE_OK;
}

enum jpeg_ret_type api_jpeg_encode_exe(int dev_fd,
                                        struct jpeg_enc_param *enc_param)
{
    struct jpeg_args     *arg;
    arg = &(jpeg_ctx->args);

    // check MCU validation width & height & sampling mode
    if (check_input_size(jpeg_ctx->args.enc_param->width,
                                jpeg_ctx->args.enc_param->height) < 0) {
        ALOGV("width/height doesn't match with MCU\r\n");
        return JPEG_FAIL;
    }

    ioctl(jpeg_ctx->jpeg_fd, IOCTL_JPEG_ENC_EXE, arg->enc_param);

    enc_param->size = arg->enc_param->size;

    return JPEG_ENCODE_OK;
}