aboutsummaryrefslogtreecommitdiff
path: root/camera/QCamera2/stack/mm-jpeg-interface/src/mm_jpegdec_interface.c
blob: df6656b00ac14f30047a45f7b4791477cdf267bc (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
/* Copyright (c) 2013-2014, 2016, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 *       copyright notice, this list of conditions and the following
 *       disclaimer in the documentation and/or other materials provided
 *       with the distribution.
 *     * Neither the name of The Linux Foundation nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

// System dependencies
#include <pthread.h>

// JPEG dependencies
#include "mm_jpeg_dbg.h"
#include "mm_jpeg_interface.h"
#include "mm_jpeg.h"

static pthread_mutex_t g_dec_intf_lock = PTHREAD_MUTEX_INITIALIZER;

static mm_jpeg_obj* g_jpegdec_obj = NULL;

/** mm_jpeg_intf_start_job:
 *
 *  Arguments:
 *    @client_hdl: client handle
 *    @job: jpeg job object
 *    @jobId: job id
 *
 *  Return:
 *       0 success, failure otherwise
 *
 *  Description:
 *       start the jpeg job
 *
 **/
static int32_t mm_jpegdec_intf_start_job(mm_jpeg_job_t* job, uint32_t* job_id)
{
  int32_t rc = -1;

  if (NULL == job ||
    NULL == job_id) {
    LOGE("invalid parameters for job or jobId");
    return rc;
  }

  pthread_mutex_lock(&g_dec_intf_lock);
  if (NULL == g_jpegdec_obj) {
    /* mm_jpeg obj not exists, return error */
    LOGE("mm_jpeg is not opened yet");
    pthread_mutex_unlock(&g_dec_intf_lock);
    return rc;
  }
  rc = mm_jpegdec_start_decode_job(g_jpegdec_obj, job, job_id);
  pthread_mutex_unlock(&g_dec_intf_lock);
  return rc;
}

/** mm_jpeg_intf_create_session:
 *
 *  Arguments:
 *    @client_hdl: client handle
 *    @p_params: encode parameters
 *    @p_session_id: session id
 *
 *  Return:
 *       0 success, failure otherwise
 *
 *  Description:
 *       Create new jpeg session
 *
 **/
static int32_t mm_jpegdec_intf_create_session(uint32_t client_hdl,
    mm_jpeg_decode_params_t *p_params,
    uint32_t *p_session_id)
{
  int32_t rc = -1;

  if (0 == client_hdl || NULL == p_params || NULL == p_session_id) {
    LOGE("invalid client_hdl or jobId");
    return rc;
  }

  pthread_mutex_lock(&g_dec_intf_lock);
  if (NULL == g_jpegdec_obj) {
    /* mm_jpeg obj not exists, return error */
    LOGE("mm_jpeg is not opened yet");
    pthread_mutex_unlock(&g_dec_intf_lock);
    return rc;
  }

  rc = mm_jpegdec_create_session(g_jpegdec_obj, client_hdl, p_params, p_session_id);
  pthread_mutex_unlock(&g_dec_intf_lock);
  return rc;
}

/** mm_jpeg_intf_destroy_session:
 *
 *  Arguments:
 *    @session_id: session id
 *
 *  Return:
 *       0 success, failure otherwise
 *
 *  Description:
 *       Destroy jpeg session
 *
 **/
static int32_t mm_jpegdec_intf_destroy_session(uint32_t session_id)
{
  int32_t rc = -1;

  if (0 == session_id) {
    LOGE("invalid client_hdl or jobId");
    return rc;
  }

  pthread_mutex_lock(&g_dec_intf_lock);
  if (NULL == g_jpegdec_obj) {
    /* mm_jpeg obj not exists, return error */
    LOGE("mm_jpeg is not opened yet");
    pthread_mutex_unlock(&g_dec_intf_lock);
    return rc;
  }

  rc = mm_jpegdec_destroy_session_by_id(g_jpegdec_obj, session_id);
  pthread_mutex_unlock(&g_dec_intf_lock);
  return rc;
}

/** mm_jpegdec_intf_abort_job:
 *
 *  Arguments:
 *    @jobId: job id
 *
 *  Return:
 *       0 success, failure otherwise
 *
 *  Description:
 *       Abort the jpeg job
 *
 **/
static int32_t mm_jpegdec_intf_abort_job(uint32_t job_id)
{
  int32_t rc = -1;

  if (0 == job_id) {
    LOGE("invalid jobId");
    return rc;
  }

  pthread_mutex_lock(&g_dec_intf_lock);
  if (NULL == g_jpegdec_obj) {
    /* mm_jpeg obj not exists, return error */
    LOGE("mm_jpeg is not opened yet");
    pthread_mutex_unlock(&g_dec_intf_lock);
    return rc;
  }

  rc = mm_jpegdec_abort_job(g_jpegdec_obj, job_id);
  pthread_mutex_unlock(&g_dec_intf_lock);
  return rc;
}

/** mm_jpeg_intf_close:
 *
 *  Arguments:
 *    @client_hdl: client handle
 *
 *  Return:
 *       0 success, failure otherwise
 *
 *  Description:
 *       Close the jpeg job
 *
 **/
static int32_t mm_jpegdec_intf_close(uint32_t client_hdl)
{
  int32_t rc = -1;

  if (0 == client_hdl) {
    LOGE("invalid client_hdl");
    return rc;
  }

  pthread_mutex_lock(&g_dec_intf_lock);
  if (NULL == g_jpegdec_obj) {
    /* mm_jpeg obj not exists, return error */
    LOGE("mm_jpeg is not opened yet");
    pthread_mutex_unlock(&g_dec_intf_lock);
    return rc;
  }

  rc = mm_jpeg_close(g_jpegdec_obj, client_hdl);
  g_jpegdec_obj->num_clients--;
  if(0 == rc) {
    if (0 == g_jpegdec_obj->num_clients) {
      /* No client, close jpeg internally */
      rc = mm_jpegdec_deinit(g_jpegdec_obj);
      free(g_jpegdec_obj);
      g_jpegdec_obj = NULL;
    }
  }

  pthread_mutex_unlock(&g_dec_intf_lock);
  return rc;
}



/** jpegdec_open:
 *
 *  Arguments:
 *    @ops: ops table pointer
 *
 *  Return:
 *       0 failure, success otherwise
 *
 *  Description:
 *       Open a jpeg client
 *
 **/
uint32_t jpegdec_open(mm_jpegdec_ops_t *ops)
{
  int32_t rc = 0;
  uint32_t clnt_hdl = 0;
  mm_jpeg_obj* jpeg_obj = NULL;

  pthread_mutex_lock(&g_dec_intf_lock);
  /* first time open */
  if(NULL == g_jpegdec_obj) {
    jpeg_obj = (mm_jpeg_obj *)malloc(sizeof(mm_jpeg_obj));
    if(NULL == jpeg_obj) {
      LOGE("no mem");
      pthread_mutex_unlock(&g_dec_intf_lock);
      return clnt_hdl;
    }

    /* initialize jpeg obj */
    memset(jpeg_obj, 0, sizeof(mm_jpeg_obj));
    rc = mm_jpegdec_init(jpeg_obj);
    if(0 != rc) {
      LOGE("mm_jpeg_init err = %d", rc);
      free(jpeg_obj);
      pthread_mutex_unlock(&g_dec_intf_lock);
      return clnt_hdl;
    }

    /* remember in global variable */
    g_jpegdec_obj = jpeg_obj;
  }

  /* open new client */
  clnt_hdl = mm_jpeg_new_client(g_jpegdec_obj);
  if (clnt_hdl > 0) {
    /* valid client */
    if (NULL != ops) {
      /* fill in ops tbl if ptr not NULL */
      ops->start_job = mm_jpegdec_intf_start_job;
      ops->abort_job = mm_jpegdec_intf_abort_job;
      ops->create_session = mm_jpegdec_intf_create_session;
      ops->destroy_session = mm_jpegdec_intf_destroy_session;
      ops->close = mm_jpegdec_intf_close;
    }
  } else {
    /* failed new client */
    LOGE("mm_jpeg_new_client failed");

    if (0 == g_jpegdec_obj->num_clients) {
      /* no client, close jpeg */
      mm_jpegdec_deinit(g_jpegdec_obj);
      free(g_jpegdec_obj);
      g_jpegdec_obj = NULL;
    }
  }

  pthread_mutex_unlock(&g_dec_intf_lock);
  return clnt_hdl;
}