summaryrefslogtreecommitdiff
path: root/camera/mm-camera-interface/mm_jpeg_encoder.c
blob: 9fda928d326a75c3c242e1a0c3e7d22b15c69318 (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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
/*
Copyright (c) 2011-2012, 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.
*/

#include <sys/time.h>
#include <pthread.h>
#include <semaphore.h>
#include <errno.h>
#include "mm_jpeg_encoder.h"
#include "mm_camera_dbg.h"
#include <sys/system_properties.h>
#include "mm_camera_interface2.h"

#ifdef JPG_DBG
#undef CDBG
  #ifdef _ANDROID_
    #undef LOG_NIDEBUG
    #undef LOG_TAG
    #define LOG_NIDEBUG 0
    #define LOG_TAG "mm-camera jpeg"
    #include <utils/Log.h>
    #define CDBG(fmt, args...) ALOGV(fmt, ##args)
    #endif
#endif

#define JPEG_DEFAULT_MAINIMAGE_QUALITY 75
#define JPEG_DEFAULT_THUMBNAIL_QUALITY 75

int is_encoding = 0;
pthread_mutex_t jpege_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t jpegcb_mutex = PTHREAD_MUTEX_INITIALIZER;

int rc;
jpege_src_t jpege_source;
jpege_dst_t jpege_dest;
jpege_cfg_t jpege_config;
jpege_img_data_t main_img_info, tn_img_info;
jpeg_buffer_t temp;
jpege_obj_t jpeg_encoder;
exif_info_obj_t exif_info;
exif_tag_entry_t sample_tag;
struct timeval tdBefore, tdAfter;
struct timezone tz;
static uint32_t jpegMainimageQuality = JPEG_DEFAULT_MAINIMAGE_QUALITY;
static uint32_t jpegThumbnailQuality = JPEG_DEFAULT_THUMBNAIL_QUALITY;
static uint32_t jpegRotation = 0;
static int8_t usethumbnail = 1;
static int8_t use_thumbnail_padding = 0;
#ifdef HW_ENCODE
static uint8_t hw_encode = true;
#else
static uint8_t hw_encode = false;
#endif
static int8_t is_3dmode = 0;
static cam_3d_frame_format_t img_format_3d;
jpegfragment_callback_t mmcamera_jpegfragment_callback = NULL;
jpeg_callback_t mmcamera_jpeg_callback = NULL;

void* user_data = NULL;
#define JPEGE_FRAGMENT_SIZE (64*1024)

/*===========================================================================
FUNCTION      jpege_event_handler

DESCRIPTION   Handler function for jpeg encoder events
===========================================================================*/
inline void jpege_use_thumb_padding(uint8_t a_use_thumb_padding)
{
  use_thumbnail_padding = a_use_thumb_padding;
}

void mm_jpeg_encoder_cancel()
{
    pthread_mutex_lock(&jpegcb_mutex);
    mmcamera_jpegfragment_callback = NULL;
    mmcamera_jpeg_callback = NULL;
    user_data = NULL;
    pthread_mutex_unlock(&jpegcb_mutex);
    mm_jpeg_encoder_join();
}

void set_callbacks(
   jpegfragment_callback_t fragcallback,
   jpeg_callback_t eventcallback,
   void* userdata

){
    pthread_mutex_lock(&jpegcb_mutex);
    mmcamera_jpegfragment_callback = fragcallback;
    mmcamera_jpeg_callback = eventcallback;
    user_data = userdata;
    pthread_mutex_unlock(&jpegcb_mutex);
}

/*===========================================================================
FUNCTION      jpege_event_handler

DESCRIPTION   Handler function for jpeg encoder events
===========================================================================*/
void mm_jpege_event_handler(void *p_user_data, jpeg_event_t event, void *p_arg)
{
  uint32_t buf_size;
  uint8_t *buf_ptr = NULL;
  int mainimg_fd, thumbnail_fd;

  if (event == JPEG_EVENT_DONE) {

    jpeg_buffer_t thumbnail_buffer, snapshot_buffer;

    thumbnail_buffer = tn_img_info.p_fragments[0].color.yuv.luma_buf;
    thumbnail_fd = jpeg_buffer_get_pmem_fd(thumbnail_buffer);
    jpeg_buffer_get_actual_size(thumbnail_buffer, &buf_size);
    jpeg_buffer_get_addr(thumbnail_buffer, &buf_ptr);

    snapshot_buffer = main_img_info.p_fragments[0].color.yuv.luma_buf;
    mainimg_fd = jpeg_buffer_get_pmem_fd(snapshot_buffer);
    jpeg_buffer_get_actual_size(snapshot_buffer, &buf_size);
    jpeg_buffer_get_addr(snapshot_buffer, &buf_ptr);

#if 0
    gettimeofday(&tdAfter, &tz);
    CDBG("Profiling: JPEG encoding latency %ld microseconds\n",
      1000000 * (tdAfter.tv_sec - tdBefore.tv_sec) + tdAfter.tv_usec -
      tdBefore.tv_usec);
#endif
//    mmcamera_util_profile("encoder done");
  }

  if(mmcamera_jpeg_callback)
    mmcamera_jpeg_callback(event, user_data);
}

/*===========================================================================
FUNCTION      jpege_output_produced_handler

DESCRIPTION   Handler function for when jpeg encoder has output produced
===========================================================================*/
void mm_jpege_output_produced_handler(void *p_user_data, void *p_arg,
  jpeg_buffer_t buffer)
{
  uint32_t buf_size;
  uint8_t *buf_ptr;

  /*  The mutex is to prevent the very rare case where the file writing is */
  /*  so slow that the next ping-pong output is delivered before the */
  /*  current one is finished writing, in which case the writing of the new */
  /*  buffer will be performed after the first one finishes (because of the lock) */

  jpeg_buffer_get_actual_size(buffer, &buf_size);
  jpeg_buffer_get_addr(buffer, &buf_ptr);

  pthread_mutex_lock(&jpegcb_mutex);
  if(mmcamera_jpegfragment_callback)
    mmcamera_jpegfragment_callback(buf_ptr, buf_size, user_data);
  pthread_mutex_unlock(&jpegcb_mutex);
}

#if !defined(_TARGET_7x2x_) && !defined(_TARGET_7x27A_)
/*===========================================================================
FUNCTION      jpege_output_produced_handler2

DESCRIPTION   Handler function for when jpeg encoder has output produced
===========================================================================*/
int mm_jpege_output_produced_handler2(void *p_user_data, void *p_arg,
  jpeg_buffer_t buffer, uint8_t last_buf_flag)
{
  uint32_t buf_size;
  uint8_t *buf_ptr;
  int rv;

  /*  The mutex is to prevent the very rare case where the file writing is */
  /*  so slow that the next ping-pong output is delivered before the */
  /*  current one is finished writing, in which case the writing of the new */
  /*  buffer will be performed after the first one finishes (because of the lock) */
  jpeg_buffer_get_actual_size(buffer, &buf_size);
  jpeg_buffer_get_addr(buffer, &buf_ptr);

  pthread_mutex_lock(&jpegcb_mutex);
  if(mmcamera_jpegfragment_callback)
    mmcamera_jpegfragment_callback(buf_ptr, buf_size, user_data);
  pthread_mutex_unlock(&jpegcb_mutex);

  rv = jpeg_buffer_set_actual_size(buffer, 0);
  if(rv == JPEGERR_SUCCESS){
      rv = jpege_enqueue_output_buffer(
          jpeg_encoder,
          &buffer, 1);
  }
  return rv;
}
#endif

static int jpeg_encoder_initialized = 0;

void mm_jpeg_encoder_set_3D_info(cam_3d_frame_format_t format)
{
  pthread_mutex_lock(&jpege_mutex);
  is_3dmode = 1;
  img_format_3d = format;
  pthread_mutex_unlock(&jpege_mutex);
}

extern int8_t mm_jpeg_encoder_init()
{
  pthread_mutex_lock(&jpege_mutex);
  is_3dmode = 0;
  /*  Initialize jpeg encoder */
  rc = jpege_init(&jpeg_encoder, mm_jpege_event_handler, NULL);
  if (rc) {
    //CDBG("jpege_init failed: %d\n", rc);
    pthread_mutex_unlock(&jpege_mutex);
    return FALSE;
  }

  jpeg_encoder_initialized = 1;
  pthread_mutex_unlock(&jpege_mutex);

  return TRUE;
}

void mm_jpeg_encoder_join(void)
{
  pthread_mutex_lock(&jpege_mutex);
  if (jpeg_encoder_initialized) {
    jpeg_encoder_initialized = 0;
    pthread_mutex_destroy(&jpege_mutex);
    jpege_abort(jpeg_encoder);
    jpeg_buffer_destroy(&temp);
    if (usethumbnail) {
        jpeg_buffer_destroy(&tn_img_info.p_fragments[0].color.yuv.luma_buf);
        jpeg_buffer_destroy(&tn_img_info.p_fragments[0].color.yuv.chroma_buf);
    }
    jpeg_buffer_destroy(&main_img_info.p_fragments[0].color.yuv.luma_buf);
    jpeg_buffer_destroy(&main_img_info.p_fragments[0].color.yuv.chroma_buf);
    jpeg_buffer_destroy(&jpege_dest.buffers[0]);
    jpeg_buffer_destroy(&jpege_dest.buffers[1]);
    exif_destroy(&exif_info);
    jpege_destroy(&jpeg_encoder);
  }
  is_3dmode = 0;
  pthread_mutex_unlock(&jpege_mutex);
}
/* This function returns the Yoffset and CbCr offset requirements for the Jpeg encoding*/
int8_t mm_jpeg_encoder_get_buffer_offset(uint32_t width, uint32_t height,
                                      uint32_t* p_y_offset, uint32_t* p_cbcr_offset,
                                      uint32_t* p_buf_size, uint8_t *num_planes,
                                      uint32_t planes[])
{
  CDBG("jpeg_encoder_get_buffer_offset");
  if ((NULL == p_y_offset) || (NULL == p_cbcr_offset)) {
    return FALSE;
  }
  /* Hardcode num planes and planes array for now. TBD Check if this
   * needs to be set based on format. */
  *num_planes = 2;
  if (hw_encode) {
    int cbcr_offset = 0;
    uint32_t actual_size = width*height;
    uint32_t padded_size = width * CEILING16(height);
    *p_y_offset = 0;
    *p_cbcr_offset = 0;
    if ((jpegRotation == 90) || (jpegRotation == 180)) {
      *p_y_offset = padded_size - actual_size;
      *p_cbcr_offset = ((padded_size - actual_size) >> 1);
    }
    *p_buf_size = padded_size * 3/2;
    planes[0] = width * CEILING16(height);
    planes[1] = width * CEILING16(height)/2;
  } else {
    *p_y_offset = 0;
    *p_cbcr_offset = PAD_TO_WORD(width*CEILING16(height));
    *p_buf_size = *p_cbcr_offset * 3/2;
    planes[0] = PAD_TO_WORD(width*CEILING16(height));
    planes[1] = PAD_TO_WORD(width*CEILING16(height)/2);
  }
  return TRUE;
}

int8_t mm_jpeg_encoder_encode(const cam_ctrl_dimension_t * dimension,
                              const uint8_t * thumbnail_buf,
                              int thumbnail_fd, uint32_t thumbnail_offset,
                              const uint8_t * snapshot_buf,
                              int snapshot_fd,
                              uint32_t snapshot_offset,
                              common_crop_t *scaling_params,
                              exif_tags_info_t *exif_data,
                              int exif_numEntries,
                              const int32_t a_cbcroffset,
                              cam_point_t* main_crop_offset,
                              cam_point_t* thumb_crop_offset)
{
  int buf_size = 0;
  int ret = 0;
  int i = 0;
  int cbcroffset = 0;
  int actual_size = 0, padded_size = 0;
  usethumbnail = thumbnail_buf ? 1 : 0;
  int w_scale_factor = (is_3dmode && img_format_3d == SIDE_BY_SIDE_FULL) ? 2 : 1;

  pthread_mutex_lock(&jpege_mutex);
  //mmcamera_util_profile("encoder configure");

  /*  Do not allow snapshot if the previous one is not done */
  /*  Alternately we can queue the snapshot to be done after the one in progress is completed, */
  /*  but it involves more complex logic */
  if (is_encoding) {
    CDBG("Previous Jpeg Encoding is not done!\n");
    pthread_mutex_unlock(&jpege_mutex);
    return FALSE;
  }
  CDBG("jpeg_encoder_encode: thumbnail_fd = %d snapshot_fd = %d usethumbnail %d\n",
    thumbnail_fd, snapshot_fd, usethumbnail);

  gettimeofday(&tdBefore, &tz);
  /*  Initialize exif info */
  exif_init(&exif_info);
  /*  Zero out supporting structures */
  memset(&main_img_info, 0, sizeof(jpege_img_data_t));
  memset(&tn_img_info, 0, sizeof(jpege_img_data_t));
  memset(&jpege_source, 0, sizeof(jpege_src_t));
  memset(&jpege_dest, 0, sizeof(jpege_dst_t));

  /*  Initialize JPEG buffers */
  jpege_dest.buffer_cnt = 2;
  if ((rc = jpeg_buffer_init(&temp)) ||
    (usethumbnail && (rc = jpeg_buffer_init(&tn_img_info.p_fragments[0].color.yuv.luma_buf))) ||
    (usethumbnail && (rc = jpeg_buffer_init(&tn_img_info.p_fragments[0].color.yuv.chroma_buf))) ||
    (rc = jpeg_buffer_init(&main_img_info.p_fragments[0].color.yuv.luma_buf)) ||
    (rc = jpeg_buffer_init(&main_img_info.p_fragments[0].color.yuv.chroma_buf))
    || (rc = jpeg_buffer_init(&jpege_dest.buffers[0]))
    || (rc = jpeg_buffer_init(&jpege_dest.buffers[1]))) {
    CDBG_ERROR("jpeg_buffer_init failed: %d\n", rc);
    pthread_mutex_unlock(&jpege_mutex);
    jpege_dest.buffer_cnt = 0;
    return FALSE;
  }
#if !defined(_TARGET_7x2x_) && !defined(_TARGET_7x27A_)
  jpege_dest.p_buffer = &jpege_dest.buffers[0];
#endif

#if defined(_TARGET_7x27A_)
  /*  Allocate 2 ping-pong buffers on the heap for jpeg encoder outputs */
  if ((rc = jpeg_buffer_allocate(jpege_dest.buffers[0], JPEGE_FRAGMENT_SIZE, 1)) ||
    (rc = jpeg_buffer_allocate(jpege_dest.buffers[1], JPEGE_FRAGMENT_SIZE, 1))) {
    CDBG("jpeg_buffer_allocate failed: %d\n", rc);
    pthread_mutex_unlock(&jpege_mutex);
    return FALSE;
  }
#else
  /*  Allocate 2 ping-pong buffers on the heap for jpeg encoder outputs */
  if ((rc = jpeg_buffer_allocate(jpege_dest.buffers[0], JPEGE_FRAGMENT_SIZE, 0)) ||
    (rc = jpeg_buffer_allocate(jpege_dest.buffers[1], JPEGE_FRAGMENT_SIZE, 0))) {
    CDBG("jpeg_buffer_allocate failed: %d\n", rc);
    pthread_mutex_unlock(&jpege_mutex);
    return FALSE;
  }
#endif


  if (usethumbnail) {
    tn_img_info.width = dimension->thumbnail_width * w_scale_factor;
    tn_img_info.height = dimension->thumbnail_height;
    buf_size = tn_img_info.width * tn_img_info.height * 2;
    tn_img_info.fragment_cnt = 1;
    tn_img_info.color_format = YCRCBLP_H2V2;
    tn_img_info.p_fragments[0].width = tn_img_info.width;
    tn_img_info.p_fragments[0].height = CEILING16(dimension->thumbnail_height);
    jpeg_buffer_reset(tn_img_info.p_fragments[0].color.yuv.luma_buf);
    jpeg_buffer_reset(tn_img_info.p_fragments[0].color.yuv.chroma_buf);

    CDBG("%s: Thumbnail: fd: %d offset: %d  Main: fd: %d offset: %d", __func__,
         thumbnail_fd, thumbnail_offset, snapshot_fd, snapshot_offset);
    rc = jpeg_buffer_use_external_buffer(
              tn_img_info.p_fragments[0].color.yuv.luma_buf,
              (uint8_t *)thumbnail_buf, buf_size,
              thumbnail_fd);

    if (rc == JPEGERR_EFAILED) {
      CDBG_ERROR("jpeg_buffer_use_external_buffer Thumbnail pmem failed...\n");
      pthread_mutex_unlock(&jpege_mutex);
      return FALSE;
    }

    cbcroffset = PAD_TO_WORD(tn_img_info.width * tn_img_info.height);
    if (hw_encode) {
      actual_size = dimension->thumbnail_width * dimension->thumbnail_height;
      padded_size = dimension->thumbnail_width *
        CEILING16(dimension->thumbnail_height);
      cbcroffset = padded_size;
    }

    // The chroma plane in YUV4:2:0 semiplanar is at the end of the luma plane,
    // so we attach the chroma buf to the luma buffer, which we've allocated to
    // be large enough to hold the entire YUV image.
    //
    jpeg_buffer_attach_existing(tn_img_info.p_fragments[0].color.yuv.chroma_buf,
      tn_img_info.p_fragments[0].color.yuv.luma_buf,
      cbcroffset);
    jpeg_buffer_set_actual_size(tn_img_info.p_fragments[0].color.yuv.luma_buf,
      tn_img_info.width * tn_img_info.height);
    jpeg_buffer_set_actual_size(
      tn_img_info.p_fragments[0].color.yuv.chroma_buf, tn_img_info.width *
      tn_img_info.height / 2);

    if (hw_encode) {
      if ((jpegRotation == 90) || (jpegRotation == 180)) {
        jpeg_buffer_set_start_offset(tn_img_info.p_fragments[0].color.yuv.luma_buf, (padded_size - actual_size));
        jpeg_buffer_set_start_offset(tn_img_info.p_fragments[0].color.yuv.chroma_buf, ((padded_size - actual_size) >> 1));
      }
    }
  }

  /* Set phy offset */
  jpeg_buffer_set_phy_offset(tn_img_info.p_fragments[0].color.yuv.luma_buf, thumbnail_offset);

  CDBG("jpeg_encoder_encode size %dx%d\n",dimension->orig_picture_dx,dimension->orig_picture_dy);
  main_img_info.width = dimension->orig_picture_dx * w_scale_factor;
  main_img_info.height = dimension->orig_picture_dy;
  buf_size = main_img_info.width * main_img_info.height * 2;
  main_img_info.fragment_cnt = 1;
  main_img_info.color_format = YCRCBLP_H2V2;
  main_img_info.p_fragments[0].width = main_img_info.width;
  main_img_info.p_fragments[0].height = CEILING16(main_img_info.height);
  jpeg_buffer_reset(main_img_info.p_fragments[0].color.yuv.luma_buf);
  jpeg_buffer_reset(main_img_info.p_fragments[0].color.yuv.chroma_buf);

  rc =
    jpeg_buffer_use_external_buffer(
            main_img_info.p_fragments[0].color.yuv.luma_buf,
            (uint8_t *)snapshot_buf, buf_size,
            snapshot_fd);

  if (rc == JPEGERR_EFAILED) {
    CDBG("jpeg_buffer_use_external_buffer Snapshot pmem failed...\n");
    pthread_mutex_unlock(&jpege_mutex);
    return FALSE;
  }
  cbcroffset = PAD_TO_WORD(main_img_info.width * CEILING16(main_img_info.height));
  actual_size = 0;
  padded_size = 0;
  if (a_cbcroffset >= 0) {
    cbcroffset = a_cbcroffset;
  } else {
    if (hw_encode) {
      actual_size = dimension->orig_picture_dx * dimension->orig_picture_dy;
      padded_size = dimension->orig_picture_dx * CEILING16(dimension->orig_picture_dy);
      cbcroffset = padded_size;
    }
  }

  CDBG("jpeg_encoder_encode: cbcroffset %d",cbcroffset);
  jpeg_buffer_attach_existing(main_img_info.p_fragments[0].color.yuv.chroma_buf,
    main_img_info.p_fragments[0].color.yuv.luma_buf,
    cbcroffset);
  jpeg_buffer_set_actual_size(main_img_info.p_fragments[0].color.yuv.luma_buf, main_img_info.width * main_img_info.height);
  jpeg_buffer_set_actual_size(main_img_info.p_fragments[0].color.yuv.chroma_buf, main_img_info.width * main_img_info.height / 2);

  if (hw_encode) {
    if ((jpegRotation == 90) || (jpegRotation == 180)) {
      jpeg_buffer_set_start_offset(main_img_info.p_fragments[0].color.yuv.luma_buf, (padded_size - actual_size));
      jpeg_buffer_set_start_offset(main_img_info.p_fragments[0].color.yuv.chroma_buf, ((padded_size - actual_size) >> 1));
    }
  }

  jpeg_buffer_set_phy_offset(main_img_info.p_fragments[0].color.yuv.luma_buf, snapshot_offset);

  /*  Set Source */
  jpege_source.p_main = &main_img_info;
  if (usethumbnail) {
    jpege_source.p_thumbnail = &tn_img_info;
    CDBG("fragment_cnt: thumb %d \n", jpege_source.p_thumbnail->fragment_cnt);
  }

  CDBG("fragment_cnt: main %d \n", jpege_source.p_main->fragment_cnt);

  rc = jpege_set_source(jpeg_encoder, &jpege_source);
  if (rc) {
    CDBG("jpege_set_source failed: %d\n", rc);
    pthread_mutex_unlock(&jpege_mutex);
    return FALSE;
  }

#if defined(_TARGET_7x2x_) || defined(_TARGET_7x27A_)
  jpege_dest.p_output_handler = (jpege_output_handler_t) mm_jpege_output_produced_handler;
#else
  jpege_dest.p_output_handler = mm_jpege_output_produced_handler2;
#endif

  jpege_dest.buffer_cnt = 2;
  rc = jpege_set_destination(jpeg_encoder, &jpege_dest);
  if (rc) {
    CDBG("jpege_set_desination failed: %d\n", rc);
    pthread_mutex_unlock(&jpege_mutex);
    return FALSE;
  }
  /*  Get default configuration */
  jpege_get_default_config(&jpege_config);
  jpege_config.thumbnail_present = usethumbnail;
  if(hw_encode)
    jpege_config.preference = JPEG_ENCODER_PREF_HW_ACCELERATED_PREFERRED;
  else
    jpege_config.preference = JPEG_ENCODER_PREF_SOFTWARE_ONLY;

  CDBG("%s: preference %d ", __func__, jpege_config.preference);
  jpege_config.main_cfg.quality = jpegMainimageQuality;
  jpege_config.thumbnail_cfg.quality = jpegThumbnailQuality;

  CDBG("Scaling params thumb in1_w %d in1_h %d out1_w %d out1_h %d "
       "main_img in2_w %d in2_h %d out2_w %d out2_h %d\n",
         scaling_params->in1_w, scaling_params->in1_h,
         scaling_params->out1_w, scaling_params->out1_h,
         scaling_params->in2_w, scaling_params->in2_h,
         scaling_params->out2_w, scaling_params->out2_h);

  if(scaling_params->in2_w && scaling_params->in2_h) {

    if(jpegRotation)
      jpege_config.preference = JPEG_ENCODER_PREF_SOFTWARE_ONLY;

    /* Scaler information  for main image */
    jpege_config.main_cfg.scale_cfg.enable = TRUE;

    jpege_config.main_cfg.scale_cfg.input_width = CEILING2(scaling_params->in2_w);
    jpege_config.main_cfg.scale_cfg.input_height = CEILING2(scaling_params->in2_h);

    if (main_crop_offset) {
      jpege_config.main_cfg.scale_cfg.h_offset = main_crop_offset->x;
      jpege_config.main_cfg.scale_cfg.v_offset = main_crop_offset->y;
    } else {
      jpege_config.main_cfg.scale_cfg.h_offset = 0;
      jpege_config.main_cfg.scale_cfg.v_offset = 0;
    }

    jpege_config.main_cfg.scale_cfg.output_width = scaling_params->out2_w;
    jpege_config.main_cfg.scale_cfg.output_height = scaling_params->out2_h;
  } else {
    CDBG("There is no scaling information for JPEG main image scaling.");
  }

  if(scaling_params->in1_w  && scaling_params->in1_h) {
    /* Scaler information  for thumbnail */
    jpege_config.thumbnail_cfg.scale_cfg.enable = TRUE;

    jpege_config.thumbnail_cfg.scale_cfg.input_width = CEILING2(scaling_params->in1_w);
    jpege_config.thumbnail_cfg.scale_cfg.input_height = CEILING2(scaling_params->in1_h);

    if (thumb_crop_offset) {
      jpege_config.thumbnail_cfg.scale_cfg.h_offset = thumb_crop_offset->x;
      jpege_config.thumbnail_cfg.scale_cfg.v_offset = thumb_crop_offset->y;
    } else {
      jpege_config.thumbnail_cfg.scale_cfg.h_offset = 0;
      jpege_config.thumbnail_cfg.scale_cfg.v_offset = 0;
    }

    jpege_config.thumbnail_cfg.scale_cfg.output_width = scaling_params->out1_w;
    jpege_config.thumbnail_cfg.scale_cfg.output_height = scaling_params->out1_h;
  } else {
    CDBG("There is no scaling information for JPEG thumbnail upscaling.");
  }

  /* Set rotation based on the mode selected */
  CDBG(" Setting Jpeg Rotation mode to %d ", jpegRotation );
  jpege_config.main_cfg.rotation_degree_clk = jpegRotation;
  jpege_config.thumbnail_cfg.rotation_degree_clk = jpegRotation;

  if( exif_data != NULL) {
    for(i = 0; i < exif_numEntries; i++) {
      rc = exif_set_tag(exif_info, exif_data[i].tag_id,
                         &(exif_data[i].tag_entry));
      if (rc) {
        CDBG("exif_set_tag failed: %d\n", rc);
        pthread_mutex_unlock(&jpege_mutex);
        return FALSE;
      }
    }
  }

#if 0 /* Enable when JPS/MPO is ready */
  /* 3D config */
  CDBG("%s: is_3dmode %d ", __func__, is_3dmode );
  if (is_3dmode) {
    jps_cfg_3d_t cfg_3d;
    if (jpege_config.main_cfg.scale_cfg.enable ||
      (jpege_config.main_cfg.rotation_degree_clk > 0)) {
      CDBG("%s: img_format_3d %d ", __func__, img_format_3d );
      return FALSE;
    }
    CDBG("%s: img_format_3d %d ", __func__, img_format_3d );

    switch (img_format_3d) {
    case TOP_DOWN_HALF:
      cfg_3d.layout = OVER_UNDER;
      cfg_3d.width_flag = FULL_WIDTH;
      cfg_3d.height_flag = HALF_HEIGHT;
      cfg_3d.field_order = LEFT_FIELD_FIRST;
      cfg_3d.separation = 0;
      break;
    case TOP_DOWN_FULL:
      cfg_3d.layout = OVER_UNDER;
      cfg_3d.width_flag = FULL_WIDTH;
      cfg_3d.height_flag = FULL_HEIGHT;
      cfg_3d.field_order = LEFT_FIELD_FIRST;
      cfg_3d.separation = 0;
      break;
    case SIDE_BY_SIDE_HALF:
      cfg_3d.layout = SIDE_BY_SIDE;
      cfg_3d.width_flag = HALF_WIDTH;
      cfg_3d.height_flag = FULL_HEIGHT;
      cfg_3d.field_order = LEFT_FIELD_FIRST;
      cfg_3d.separation = 0;
      break;
    default:
    case SIDE_BY_SIDE_FULL:
      cfg_3d.layout = SIDE_BY_SIDE;
      cfg_3d.width_flag = FULL_WIDTH;
      cfg_3d.height_flag = FULL_HEIGHT;
      cfg_3d.field_order = LEFT_FIELD_FIRST;
      cfg_3d.separation = 0;
      break;
    }

    rc = jpse_config_3d(jpeg_encoder, cfg_3d);
    if (rc) {
     CDBG_ERROR("%s: jpse_config_3d failed: %d\n", __func__, rc);
      pthread_mutex_unlock(&jpege_mutex);
      return FALSE;
    }
  }
#endif

  /*  Start encoder */
/*
  if( jpege_config.main_cfg.scale_cfg.enable) {
    mmcamera_util_profile("SW encoder starting encoding");
  } else {
    mmcamera_util_profile("HW encoder starting encoding");
  }
*/
  rc = jpege_start(jpeg_encoder, &jpege_config, &exif_info);
  if (rc) {
    CDBG("jpege_start failed: %d\n", rc);
    pthread_mutex_unlock(&jpege_mutex);
    return FALSE;
  }

  pthread_mutex_unlock(&jpege_mutex);
  return TRUE;
}

int8_t mm_jpeg_encoder_setMainImageQuality(uint32_t quality)
{
  pthread_mutex_lock(&jpege_mutex);
  CDBG(" jpeg_encoder_setMainImageQuality current main inage quality %d ," \
       " new quality : %d\n", jpegMainimageQuality, quality);
  if (quality <= 100)
    jpegMainimageQuality = quality;
  pthread_mutex_unlock(&jpege_mutex);
  return TRUE;
}

int8_t mm_jpeg_encoder_setThumbnailQuality(uint32_t quality)
{
  pthread_mutex_lock(&jpege_mutex);
  CDBG(" jpeg_encoder_setThumbnailQuality current thumbnail quality %d ," \
       " new quality : %d\n", jpegThumbnailQuality, quality);
  if (quality <= 100)
    jpegThumbnailQuality = quality;
  pthread_mutex_unlock(&jpege_mutex);
  return TRUE;
}

int8_t mm_jpeg_encoder_setRotation(int rotation)
{
  pthread_mutex_lock(&jpege_mutex);
  /* Set rotation configuration */
  switch(rotation)
  {
      case 0:
      case 90:
      case 180:
      case 270:
          jpegRotation = rotation;
          break;
      default:
          /* Invalid rotation mode, set to default */
          CDBG(" Setting Default rotation mode ");
          jpegRotation = 0;
          break;
  }
  pthread_mutex_unlock(&jpege_mutex);
  return TRUE;
}