Home | History | Annotate | Download | only in src
      1 /*
      2 Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
      3 
      4 Redistribution and use in source and binary forms, with or without
      5 modification, are permitted provided that the following conditions are
      6 met:
      7     * Redistributions of source code must retain the above copyright
      8       notice, this list of conditions and the following disclaimer.
      9     * Redistributions in binary form must reproduce the above
     10       copyright notice, this list of conditions and the following
     11       disclaimer in the documentation and/or other materials provided
     12       with the distribution.
     13     * Neither the name of The Linux Foundation nor the names of its
     14       contributors may be used to endorse or promote products derived
     15       from this software without specific prior written permission.
     16 
     17 THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
     18 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
     20 ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
     21 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
     24 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     25 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     26 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     27 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28 */
     29 
     30 #include <pthread.h>
     31 #include <stdbool.h>
     32 #include "mm_camera_dbg.h"
     33 #include <errno.h>
     34 #include <sys/ioctl.h>
     35 #include <sys/types.h>
     36 #include <sys/stat.h>
     37 #include <fcntl.h>
     38 #include <poll.h>
     39 #include "mm_qcamera_app.h"
     40 
     41 #define BUFF_SIZE_128 128
     42 
     43 //static mm_camera_ch_data_buf_t *mCurrentFrameEncoded;
     44 static int JpegOffset = 0;
     45 static int raw_snapshot_cnt = 0;
     46 static int snapshot_cnt = 0;
     47 static pthread_mutex_t g_s_mutex;
     48 static int g_status = 0;
     49 static pthread_cond_t g_s_cond_v;
     50 
     51 
     52 static void mm_app_snapshot_done()
     53 {
     54     pthread_mutex_lock(&g_s_mutex);
     55     g_status = true;
     56     pthread_cond_signal(&g_s_cond_v);
     57     pthread_mutex_unlock(&g_s_mutex);
     58 }
     59 
     60 static int mm_app_dump_snapshot_frame(struct msm_frame *frame,
     61                                       uint32_t len, int is_main, int is_raw)
     62 {
     63     char bufp[BUFF_SIZE_128];
     64     int file_fdp;
     65     int rc = 0;
     66 
     67     if (is_raw) {
     68         snprintf(bufp, BUFF_SIZE_128, "/data/main_raw_%d.yuv", raw_snapshot_cnt);
     69     } else {
     70         if (is_main) {
     71             snprintf(bufp, BUFF_SIZE_128, "/data/main_%d.yuv", snapshot_cnt);
     72         } else {
     73             snprintf(bufp, BUFF_SIZE_128, "/data/thumb_%d.yuv", snapshot_cnt);
     74         }
     75     }
     76 
     77     file_fdp = open(bufp, O_RDWR | O_CREAT, 0777);
     78 
     79     if (file_fdp < 0) {
     80         CDBG("cannot open file %s\n", bufp);
     81         rc = -1;
     82         goto end;
     83     }
     84     CDBG("%s:dump snapshot frame to '%s'\n", __func__, bufp);
     85     write(file_fdp,
     86           (const void *)frame->buffer, len);
     87     close(file_fdp);
     88     end:
     89     return rc;
     90 }
     91 
     92 
     93 static void mm_app_dump_jpeg_frame(const void * data, uint32_t size, char* name, char* ext, int index)
     94 {
     95     char buf[32];
     96     int file_fd;
     97     if ( data != NULL) {
     98         char * str;
     99         snprintf(buf, sizeof(buf), "/data/%s_%d.%s", name, index, ext);
    100         CDBG("%s size =%d", buf, size);
    101         file_fd = open(buf, O_RDWR | O_CREAT, 0777);
    102         write(file_fd, data, size);
    103         close(file_fd);
    104     }
    105 }
    106 
    107 static int mm_app_set_thumbnail_fmt(int cam_id,mm_camera_image_fmt_t *fmt)
    108 {
    109     int rc = MM_CAMERA_OK;
    110     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
    111 
    112     fmt->meta_header = MM_CAMEAR_META_DATA_TYPE_DEF;
    113     fmt->fmt = pme->dim.thumb_format;
    114     fmt->width = pme->dim.thumbnail_width;
    115     fmt->height = pme->dim.thumbnail_height;
    116     CDBG("Thumbnail Dimension = %dX%d",fmt->width,fmt->height);
    117     return rc;
    118 }
    119 
    120 int mm_app_set_snapshot_fmt(int cam_id,mm_camera_image_fmt_t *fmt)
    121 {
    122 
    123     int rc = MM_CAMERA_OK;
    124     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
    125     fmt->meta_header = MM_CAMEAR_META_DATA_TYPE_DEF;
    126     fmt->fmt = pme->dim.main_img_format;
    127     fmt->width = pme->dim.picture_width;
    128     fmt->height = pme->dim.picture_height;
    129     CDBG("Snapshot Dimension = %dX%d",fmt->width,fmt->height);
    130     return rc;
    131 }
    132 
    133 int mm_app_set_live_snapshot_fmt(int cam_id,mm_camera_image_fmt_t *fmt)
    134 {
    135     int rc = MM_CAMERA_OK;
    136     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
    137     fmt->meta_header = MM_CAMEAR_META_DATA_TYPE_DEF;
    138     fmt->fmt = pme->dim.enc_format;
    139     fmt->width = pme->dim.video_width;
    140     fmt->height = pme->dim.video_height;
    141     CDBG("Livesnapshot Dimension = %dX%d",fmt->width,fmt->height);
    142     return rc;
    143 }
    144 
    145 int mm_app_set_raw_snapshot_fmt(int cam_id)
    146 {
    147     int rc = MM_CAMERA_OK;
    148 #if 0
    149     /* now we hard code format */
    150     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
    151 
    152     mm_camera_ch_image_fmt_parm_t fmt;
    153 
    154     CDBG("%s: BEGIN\n", __func__);
    155     memset(&fmt, 0, sizeof(mm_camera_ch_image_fmt_parm_t));
    156     fmt.ch_type = MM_CAMERA_CH_RAW;
    157     fmt.def.fmt = CAMERA_BAYER_SBGGR10;
    158     fmt.def.dim.width = pme->dim.raw_picture_width;
    159     fmt.def.dim.height = pme->dim.raw_picture_height;
    160     rc = pme->cam->cfg->set_parm(pme->cam, MM_CAMERA_PARM_CH_IMAGE_FMT, &fmt);
    161     if (rc != MM_CAMERA_OK) {
    162         CDBG("%s:set raw snapshot format err=%d\n", __func__, rc);
    163     }
    164     end:
    165     CDBG("%s: END, rc=%d\n", __func__, rc);
    166 #endif
    167     return rc;
    168 }
    169 
    170 int mm_app_prepare_raw_snapshot_buf(int cam_id)
    171 {
    172     int rc = MM_CAMERA_OK;
    173 #if 0
    174     int j;
    175     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
    176     mm_camera_reg_buf_t reg_buf;
    177     uint32_t y_off, cbcr_off;
    178     uint8_t num_planes_main;
    179     uint32_t planes_main[VIDEO_MAX_PLANES];
    180 
    181     CDBG("%s: BEGIN, raw_w=%d, raw_h=%d\n",
    182          __func__, pme->dim.raw_picture_width, pme->dim.raw_picture_height);
    183     memset(&reg_buf,  0,  sizeof(reg_buf));
    184     reg_buf.def.buf.mp = malloc(sizeof(mm_camera_mp_buf_t));
    185     if (!reg_buf.def.buf.mp) {
    186         CDBG_ERROR("%s Error allocating memory for mplanar struct ", __func__);
    187         rc = -MM_CAMERA_E_NO_MEMORY;
    188         goto end;
    189     }
    190 
    191     // setup main buffer
    192     memset(&pme->raw_snapshot_buf, 0, sizeof(pme->raw_snapshot_buf));
    193     pme->raw_snapshot_buf.num = 1;
    194     pme->raw_snapshot_buf.frame_len =
    195     my_cam_app.hal_lib.mm_camera_get_msm_frame_len(CAMERA_BAYER_SBGGR10,
    196                                                    CAMERA_MODE_2D,
    197                                                    pme->dim.raw_picture_width,
    198                                                    pme->dim.raw_picture_height,
    199                                                    OUTPUT_TYPE_S,
    200                                                    &num_planes_main,
    201                                                    planes_main);
    202 #ifdef USE_ION
    203     pme->raw_snapshot_buf.frame[0].ion_alloc.len = pme->raw_snapshot_buf.frame_len;
    204     pme->raw_snapshot_buf.frame[0].ion_alloc.flags = (0x1 << CAMERA_ION_HEAP_ID);
    205     pme->raw_snapshot_buf.frame[0].ion_alloc.align = 4096;
    206 #endif
    207     pme->raw_snapshot_buf.frame[0].buffer = (unsigned long) my_cam_app.hal_lib.mm_camera_do_mmap(
    208                                                                                                 pme->raw_snapshot_buf.frame_len, &pme->raw_snapshot_buf.frame[0].fd);
    209 
    210 
    211     if (!pme->raw_snapshot_buf.frame[0].buffer) {
    212         CDBG("%s:no mem for snapshot buf\n", __func__);
    213         rc = -MM_CAMERA_E_NO_MEMORY;
    214         goto end;
    215     }
    216     pme->raw_snapshot_buf.frame[0].path = OUTPUT_TYPE_S;
    217     pme->preview_buf.frame[0].y_off = 0;
    218     pme->raw_snapshot_buf.frame[0].cbcr_off = planes_main[0];
    219 
    220     /*setup registration buffer*/
    221     reg_buf.def.buf.mp[0].frame = pme->raw_snapshot_buf.frame[0];
    222     reg_buf.def.buf.mp[0].frame_offset = 0;
    223     reg_buf.def.buf.mp[0].num_planes = num_planes_main;
    224 
    225     reg_buf.def.buf.mp[0].planes[0].length = planes_main[0];
    226     reg_buf.def.buf.mp[0].planes[0].m.userptr = pme->raw_snapshot_buf.frame[0].fd;
    227     reg_buf.def.buf.mp[0].planes[0].data_offset = 0;
    228     reg_buf.def.buf.mp[0].planes[0].reserved[0] = reg_buf.def.buf.mp[0].frame_offset;
    229     for (j = 1; j < num_planes_main; j++) {
    230         reg_buf.def.buf.mp[0].planes[j].length = planes_main[j];
    231         reg_buf.def.buf.mp[0].planes[j].m.userptr = pme->raw_snapshot_buf.frame[0].fd;
    232         reg_buf.def.buf.mp[0].planes[j].data_offset = 0;
    233         reg_buf.def.buf.mp[0].planes[j].reserved[0] = reg_buf.def.buf.mp[0].planes[j-1].reserved[0] +
    234                                                       reg_buf.def.buf.mp[0].planes[j-1].length;
    235     }
    236 
    237     reg_buf.ch_type = MM_CAMERA_CH_RAW;
    238     reg_buf.def.num = pme->raw_snapshot_buf.num;
    239     rc = pme->cam->cfg->prepare_buf(pme->cam, &reg_buf);
    240     if (rc != MM_CAMERA_OK) {
    241         CDBG("%s:reg snapshot buf err=%d\n", __func__, rc);
    242         goto end;
    243     }
    244     end:
    245     CDBG("%s: END, rc=%d\n", __func__, rc);
    246 #endif
    247     return rc;
    248 }
    249 
    250 static int mm_app_unprepare_raw_snapshot_buf(int cam_id)
    251 {
    252     int i, rc = MM_CAMERA_OK;
    253 #if 0
    254     /* now we hard code format */
    255     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
    256 
    257     CDBG("%s: BEGIN\n", __func__);
    258     rc = pme->cam->cfg->unprepare_buf(pme->cam, MM_CAMERA_CH_RAW);
    259     rc = my_cam_app.hal_lib.mm_camera_do_munmap(pme->raw_snapshot_buf.frame[0].fd,
    260                                                 (void *)pme->raw_snapshot_buf.frame[0].buffer,
    261                                                 pme->raw_snapshot_buf.frame_len);
    262     rc = my_cam_app.hal_lib.mm_camera_do_munmap(pme->jpeg_buf.frame[0].fd,
    263                                                 (void *)pme->jpeg_buf.frame[0].buffer,
    264                                                 pme->jpeg_buf.frame_len);
    265     /* zero out the buf stuct */
    266     memset(&pme->raw_snapshot_buf, 0, sizeof(pme->raw_snapshot_buf));
    267     memset(&pme->jpeg_buf, 0, sizeof(pme->jpeg_buf));
    268 
    269     end:
    270     CDBG("%s: END, rc=%d\n", __func__, rc);
    271 #endif
    272     return rc;
    273 }
    274 
    275 #ifndef DISABLE_JPEG_ENCODING
    276 /* Once we give frame for encoding, we get encoded jpeg image
    277    fragments by fragment. We'll need to store them in a buffer
    278    to form complete JPEG image */
    279 static void snapshot_jpeg_fragment_cb(uint8_t *ptr,
    280                                       uint32_t size,
    281                                       void *user_data)
    282 {
    283 #if 0
    284     mm_camera_app_obj_t *pme = user_data;
    285 
    286     CDBG("%s: E",__func__);
    287     if (pme) {
    288         memcpy((uint8_t *)((uint32_t)pme->jpeg_buf.frame[0].buffer + JpegOffset), ptr, size);
    289         JpegOffset += size;
    290     }
    291     CDBG("%s: X",__func__);
    292 #endif
    293 }
    294 #endif
    295 /* This callback is received once the complete JPEG encoding is done */
    296 static void snapshot_raw_cb(mm_camera_super_buf_t *bufs,
    297                             void *user_data)
    298 {
    299 
    300     int rc;
    301     int i = 0;
    302     mm_camera_buf_def_t *main_frame = NULL;
    303     mm_camera_buf_def_t *thumb_frame = NULL;
    304     mm_camera_app_obj_t *pme = NULL;
    305     CDBG("%s: BEGIN\n", __func__);
    306 
    307     pme = (mm_camera_app_obj_t *)user_data;
    308 
    309     CDBG("%s : total streams = %d",__func__,bufs->num_bufs);
    310     main_frame = bufs->bufs[0];
    311     thumb_frame = bufs->bufs[1];
    312 
    313     CDBG("mainframe frame_idx = %d fd = %d frame length = %d",main_frame->frame_idx,main_frame->fd,main_frame->frame_len);
    314     CDBG("thumnail frame_idx = %d fd = %d frame length = %d",thumb_frame->frame_idx,thumb_frame->fd,thumb_frame->frame_len);
    315 
    316     //dumpFrameToFile(main_frame->frame,pme->dim.picture_width,pme->dim.picture_height,"main", 1);
    317     //dumpFrameToFile(thumb_frame->frame,pme->dim.thumbnail_width,pme->dim.thumbnail_height,"thumb", 1);
    318 
    319     dumpFrameToFile(main_frame,pme->dim.picture_width,pme->dim.picture_height,"main", 1);
    320     dumpFrameToFile(thumb_frame,pme->dim.thumbnail_width,pme->dim.thumbnail_height,"thumb", 1);
    321 
    322     if (MM_CAMERA_OK != pme->cam->ops->qbuf(pme->cam->camera_handle,pme->ch_id,main_frame)) {
    323         CDBG_ERROR("%s: Failed in thumbnail Qbuf\n", __func__);
    324     }
    325     if (MM_CAMERA_OK != pme->cam->ops->qbuf(pme->cam->camera_handle,pme->ch_id,thumb_frame)) {
    326         CDBG_ERROR("%s: Failed in thumbnail Qbuf\n", __func__);
    327     }
    328 
    329     mm_app_snapshot_done();
    330     CDBG("%s: END\n", __func__);
    331 
    332 
    333 }
    334 
    335 #ifndef DISABLE_JPEG_ENCODING
    336 static int encodeData(mm_camera_super_buf_t* recvd_frame,
    337                       int frame_len,
    338                       int enqueued,
    339                       mm_camera_app_obj_t *pme)
    340 {
    341     int ret = -1;
    342 #if 0
    343 
    344     cam_ctrl_dimension_t dimension;
    345     struct msm_frame *postviewframe;
    346     struct msm_frame *mainframe;
    347     common_crop_t crop;
    348     cam_point_t main_crop_offset;
    349     cam_point_t thumb_crop_offset;
    350     int width, height;
    351     uint8_t *thumbnail_buf;
    352     uint32_t thumbnail_fd;
    353 
    354     omx_jpeg_encode_params encode_params;
    355     postviewframe = recvd_frame->snapshot.thumbnail.frame;
    356     mainframe = recvd_frame->snapshot.main.frame;
    357     dimension.orig_picture_dx = pme->dim.picture_width;
    358     dimension.orig_picture_dy = pme->dim.picture_height;
    359     dimension.thumbnail_width = pme->dim.ui_thumbnail_width;
    360     dimension.thumbnail_height = pme->dim.ui_thumbnail_height;
    361     dimension.main_img_format = pme->dim.main_img_format;
    362     dimension.thumb_format = pme->dim.thumb_format;
    363 
    364     CDBG("Setting callbacks, initializing encoder and start encoding.");
    365     my_cam_app.hal_lib.set_callbacks(snapshot_jpeg_fragment_cb, snapshot_jpeg_cb, pme,
    366                                      (void *)pme->jpeg_buf.frame[0].buffer, &JpegOffset);
    367     my_cam_app.hal_lib.omxJpegStart();
    368     my_cam_app.hal_lib.mm_jpeg_encoder_setMainImageQuality(85);
    369 
    370     /*TBD: Pass 0 as cropinfo for now as v4l2 doesn't provide
    371       cropinfo. It'll be changed later.*/
    372     memset(&crop,0,sizeof(common_crop_t));
    373     memset(&main_crop_offset,0,sizeof(cam_point_t));
    374     memset(&thumb_crop_offset,0,sizeof(cam_point_t));
    375 
    376     /*Fill in the encode parameters*/
    377     encode_params.dimension = (const cam_ctrl_dimension_t *)&dimension;
    378     encode_params.thumbnail_buf = (uint8_t *)postviewframe->buffer;
    379     encode_params.thumbnail_fd = postviewframe->fd;
    380     encode_params.thumbnail_offset = postviewframe->phy_offset;
    381     encode_params.snapshot_buf = (uint8_t *)mainframe->buffer;
    382     encode_params.snapshot_fd = mainframe->fd;
    383     encode_params.snapshot_offset = mainframe->phy_offset;
    384     encode_params.scaling_params = &crop;
    385     encode_params.exif_data = NULL;
    386     encode_params.exif_numEntries = 0;
    387     encode_params.a_cbcroffset = -1;
    388     encode_params.main_crop_offset = &main_crop_offset;
    389     encode_params.thumb_crop_offset = &thumb_crop_offset;
    390 
    391     if (!my_cam_app.hal_lib.omxJpegEncode(&encode_params)) {
    392         CDBG_ERROR("%s: Failure! JPEG encoder returned error.", __func__);
    393         ret = -1;
    394         goto end;
    395     }
    396 
    397     /* Save the pointer to the frame sent for encoding. we'll need it to
    398        tell kernel that we are done with the frame.*/
    399     mCurrentFrameEncoded = recvd_frame;
    400 
    401     end:
    402     CDBG("%s: X", __func__);
    403 #endif
    404     return ret;
    405 }
    406 
    407 static int encodeDisplayAndSave(mm_camera_super_buf_t* recvd_frame,
    408                                 int enqueued, mm_camera_app_obj_t *pme)
    409 {
    410     int ret = -1;
    411 #if 0
    412 
    413 
    414     CDBG("%s: Send frame for encoding", __func__);
    415     ret = encodeData(recvd_frame, pme->snapshot_buf.frame_len,
    416                      enqueued, pme);
    417     if (!ret) {
    418         CDBG_ERROR("%s: Failure configuring JPEG encoder", __func__);
    419     }
    420 
    421     LOGD("%s: X", __func__);
    422 #endif
    423     return ret;
    424 }
    425 #endif //DISABLE_JPEG_ENCODING
    426 static void mm_app_snapshot_notify_cb(mm_camera_super_buf_t *bufs,
    427                                       void *user_data)
    428 {
    429 #if 0
    430     mm_camera_app_obj_t *pme = user_data;
    431     int rc;
    432 
    433     CDBG("%s: BEGIN\n", __func__);
    434     snapshot_cnt++;
    435     mm_app_dump_snapshot_frame(bufs->snapshot.main.frame, pme->snapshot_buf.frame_len, TRUE, 0);
    436     mm_app_dump_snapshot_frame(bufs->snapshot.thumbnail.frame, pme->thumbnail_buf.frame_len, FALSE, 0);
    437 #ifndef DISABLE_JPEG_ENCODING
    438     /* The recvd_frame structre we receive from lower library is a local
    439 variable. So we'll need to save this structure so that we won't
    440 be later pointing to garbage data when that variable goes out of
    441 scope */
    442     mm_camera_ch_data_buf_t* frame =
    443     (mm_camera_ch_data_buf_t *)malloc(sizeof(mm_camera_ch_data_buf_t));
    444     if (frame == NULL) {
    445         CDBG_ERROR("%s: Error allocating memory to save received_frame structure.", __func__);
    446         goto error1;
    447     }
    448     memcpy(frame, bufs, sizeof(mm_camera_ch_data_buf_t));
    449     rc = encodeDisplayAndSave(frame, 0, pme);
    450     if (!rc) {
    451         CDBG_ERROR("%s: Error encoding buffer.", __func__);
    452         goto error;
    453     }
    454 #endif //DISABLE_JPEG_ENCODING
    455     /* return buffer back for taking next snapshot */
    456     pme->cam->evt->buf_done(pme->cam, bufs);
    457     mm_app_snapshot_done();
    458 /*
    459         CDBG("%s: calling mm_app_snapshot_done()\n", __func__);
    460         mm_app_snapshot_done();
    461 */
    462     CDBG("%s: END\n", __func__);
    463     return;
    464     error:
    465     /*if (frame != NULL)
    466       free(frame);*/
    467     error1:
    468     pme->cam->evt->buf_done(pme->cam, bufs);
    469     mm_app_snapshot_done();
    470 #endif
    471     return;
    472 }
    473 
    474 static void mm_app_raw_snapshot_notify_cb(mm_camera_super_buf_t *bufs,
    475                                           void *user_data)
    476 {
    477 #if 0
    478     mm_camera_app_obj_t *pme = user_data;
    479     static int loop = 0;
    480 
    481     CDBG("%s: BEGIN\n", __func__);
    482     raw_snapshot_cnt++;
    483     mm_app_dump_snapshot_frame(bufs->def.frame, pme->raw_snapshot_buf.frame_len, TRUE, 1);
    484     /* return buffer back for taking next snapshot */
    485     pme->cam->evt->buf_done(pme->cam, bufs);
    486     CDBG("%s: calling mm_app_snapshot_done()\n", __func__);
    487     mm_app_snapshot_done();
    488     CDBG("%s: END\n", __func__);
    489 #endif
    490 }
    491 static int mm_app_reg_snapshot_data_cb(int cam_id, int is_reg)
    492 {
    493     int rc = MM_CAMERA_OK;
    494 #if 0
    495     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
    496 
    497 
    498     CDBG("%s: BEGIN\n", __func__);
    499     if (is_reg) {
    500         rc = pme->cam->evt->register_buf_notify(pme->cam,
    501                                                 MM_CAMERA_CH_SNAPSHOT,
    502                                                 mm_app_snapshot_notify_cb,
    503                                                 MM_CAMERA_REG_BUF_CB_INFINITE, 0,
    504                                                 pme);
    505         if (rc != MM_CAMERA_OK) {
    506             CDBG("%s:register snapshot data notify cb err=%d\n",
    507                  __func__, rc);
    508             goto end;
    509         }
    510     } else {
    511         rc = pme->cam->evt->register_buf_notify(pme->cam,
    512                                                 MM_CAMERA_CH_SNAPSHOT,
    513                                                 NULL,
    514                                                 (mm_camera_register_buf_cb_type_t)NULL,
    515                                                 0, pme);
    516         if (rc != MM_CAMERA_OK) {
    517             CDBG("%s:unregister snapshot data notify cb err=%d\n",
    518                  __func__, rc);
    519             goto end;
    520         }
    521     }
    522     end:
    523     CDBG("%s: END, rc=%d\n", __func__, rc);
    524 #endif
    525     return rc;
    526 }
    527 static int mm_app_reg_raw_snapshot_data_cb(int cam_id, int is_reg)
    528 {
    529     int rc = MM_CAMERA_OK;
    530 #if 0
    531     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
    532 
    533 
    534     CDBG("%s: BEGIN\n", __func__);
    535     if (is_reg) {
    536         rc = pme->cam->evt->register_buf_notify(pme->cam,
    537                                                 MM_CAMERA_CH_RAW,
    538                                                 mm_app_raw_snapshot_notify_cb,
    539                                                 MM_CAMERA_REG_BUF_CB_INFINITE, 0,
    540                                                 pme);
    541         if (rc != MM_CAMERA_OK) {
    542             CDBG("%s:register raw snapshot data notify cb err=%d\n",
    543                  __func__, rc);
    544             goto end;
    545         }
    546     } else {
    547         rc = pme->cam->evt->register_buf_notify(pme->cam,
    548                                                 MM_CAMERA_CH_RAW,
    549                                                 NULL,
    550                                                 (mm_camera_register_buf_cb_type_t)NULL, 0, pme);
    551         if (rc != MM_CAMERA_OK) {
    552             CDBG("%s:unregister raw snapshot data notify cb err=%d\n",
    553                  __func__, rc);
    554             goto end;
    555         }
    556     }
    557     end:
    558     CDBG("%s: END, rc=%d\n", __func__, rc);
    559 #endif
    560     return rc;
    561 }
    562 
    563 int mm_app_add_snapshot_stream(int cam_id)
    564 {
    565     int rc = MM_CAMERA_OK;
    566     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
    567 
    568     pme->stream[MM_CAMERA_SNAPSHOT_MAIN].id = pme->cam->ops->add_stream(pme->cam->camera_handle,pme->ch_id,
    569                                                                         NULL,pme,
    570                                                                         MM_CAMERA_SNAPSHOT_MAIN, 0);
    571 
    572     CDBG("Add Snapshot main is successfull stream ID = %d",pme->stream[MM_CAMERA_SNAPSHOT_MAIN].id);
    573     if (!pme->stream[MM_CAMERA_SNAPSHOT_MAIN].id) {
    574         CDBG_ERROR("%s:preview streaming err=%d\n", __func__, rc);
    575         rc = -1;
    576         goto end;
    577     }
    578 
    579     pme->stream[MM_CAMERA_SNAPSHOT_THUMBNAIL].id = pme->cam->ops->add_stream(pme->cam->camera_handle,pme->ch_id,
    580                                                                              NULL,pme,
    581                                                                              MM_CAMERA_SNAPSHOT_THUMBNAIL, 0);
    582     if (!pme->stream[MM_CAMERA_SNAPSHOT_THUMBNAIL].id) {
    583         CDBG_ERROR("%s:preview streaming err=%d\n", __func__, rc);
    584         rc = -1;
    585         goto end;
    586     }
    587     end:
    588     CDBG("%s: END, rc=%d\n", __func__, rc);
    589     return rc;
    590 }
    591 
    592 void mm_app_set_snapshot_mode(int cam_id,int op_mode)
    593 {
    594     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
    595     pme->cam->ops->set_parm(pme->cam->camera_handle,MM_CAMERA_PARM_OP_MODE, &op_mode);
    596 
    597 }
    598 
    599 int mm_app_config_snapshot_format(int cam_id)
    600 {
    601     int rc = MM_CAMERA_OK;
    602     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
    603 
    604     mm_app_set_snapshot_fmt(cam_id,&pme->stream[MM_CAMERA_SNAPSHOT_MAIN].str_config.fmt);
    605 
    606     mm_app_set_thumbnail_fmt(cam_id,&pme->stream[MM_CAMERA_SNAPSHOT_THUMBNAIL].str_config.fmt);
    607 
    608     pme->stream[MM_CAMERA_SNAPSHOT_MAIN].str_config.need_stream_on = 1;
    609     pme->stream[MM_CAMERA_SNAPSHOT_MAIN].str_config.num_of_bufs = 1;
    610 
    611     if (MM_CAMERA_OK != (rc = pme->cam->ops->config_stream(pme->cam->camera_handle,pme->ch_id,pme->stream[MM_CAMERA_SNAPSHOT_MAIN].id,
    612                                                            &pme->stream[MM_CAMERA_SNAPSHOT_MAIN].str_config))) {
    613         CDBG_ERROR("%s:preview streaming err=%d\n", __func__, rc);
    614         goto end;
    615     }
    616 
    617     pme->stream[MM_CAMERA_SNAPSHOT_THUMBNAIL].str_config.need_stream_on = 1;
    618     pme->stream[MM_CAMERA_SNAPSHOT_THUMBNAIL].str_config.num_of_bufs = 1;
    619 
    620     if (MM_CAMERA_OK != (rc = pme->cam->ops->config_stream(pme->cam->camera_handle,pme->ch_id,pme->stream[MM_CAMERA_SNAPSHOT_THUMBNAIL].id,
    621                                                            &pme->stream[MM_CAMERA_SNAPSHOT_THUMBNAIL].str_config))) {
    622         CDBG_ERROR("%s:preview streaming err=%d\n", __func__, rc);
    623         goto end;
    624     }
    625     end:
    626     CDBG("%s: END, rc=%d\n", __func__, rc);
    627     return rc;
    628 
    629 }
    630 
    631 int mm_app_streamon_snapshot(int cam_id)
    632 {
    633     int rc = MM_CAMERA_OK;
    634     int stream[2];
    635     mm_camera_bundle_attr_t attr;
    636 
    637     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
    638 
    639     stream[0] = pme->stream[MM_CAMERA_SNAPSHOT_MAIN].id;
    640     stream[1] = pme->stream[MM_CAMERA_SNAPSHOT_THUMBNAIL].id;
    641 
    642     attr.notify_mode = MM_CAMERA_SUPER_BUF_NOTIFY_BURST;
    643     attr.burst_num = 1;
    644     attr.look_back = 2;
    645     attr.post_frame_skip = 0;
    646     attr.water_mark = 2;
    647 
    648     if (MM_CAMERA_OK != (rc = pme->cam->ops->init_stream_bundle(
    649                                                                pme->cam->camera_handle,pme->ch_id,snapshot_raw_cb,pme,&attr,2,stream))) {
    650         CDBG_ERROR("%s:init_stream_bundle err=%d\n", __func__, rc);
    651         goto end;
    652     }
    653 
    654     if (MM_CAMERA_OK != (rc = pme->cam->ops->start_streams(pme->cam->camera_handle,pme->ch_id,
    655                                                            2, stream))) {
    656         CDBG_ERROR("%s:start_streams err=%d\n", __func__, rc);
    657         goto end;
    658     }
    659 
    660     if (MM_CAMERA_OK != (rc =pme->cam->ops->request_super_buf(pme->cam->camera_handle,pme->ch_id))) {
    661         CDBG_ERROR("%s:request_super_buf err=%d\n", __func__, rc);
    662         goto end;
    663     }
    664     pme->cam_state = CAMERA_STATE_SNAPSHOT;
    665     end:
    666     CDBG("%s: END, rc=%d\n", __func__, rc);
    667     return rc;
    668 }
    669 
    670 int mm_app_streamoff_snapshot(int cam_id)
    671 {
    672     int rc = MM_CAMERA_OK;
    673     int stream[2];
    674 
    675     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
    676 
    677     stream[0] = pme->stream[MM_CAMERA_SNAPSHOT_MAIN].id;
    678     stream[1] = pme->stream[MM_CAMERA_SNAPSHOT_THUMBNAIL].id;
    679 
    680     if (MM_CAMERA_OK != (rc = pme->cam->ops->stop_streams(pme->cam->camera_handle,pme->ch_id,2,&stream))) {
    681         CDBG_ERROR("%s : Snapshot Stream off Error",__func__);
    682         goto end;
    683     }
    684     CDBG("Stop snapshot main successfull");
    685 
    686     if (MM_CAMERA_OK != (rc = pme->cam->ops->destroy_stream_bundle(pme->cam->camera_handle,pme->ch_id))) {
    687         CDBG_ERROR("%s : Snapshot destroy_stream_bundle Error",__func__);
    688         goto end;
    689     }
    690 
    691     if (MM_CAMERA_OK != (rc = pme->cam->ops->del_stream(pme->cam->camera_handle,pme->ch_id,pme->stream[MM_CAMERA_SNAPSHOT_MAIN].id))) {
    692         CDBG_ERROR("%s : Snapshot main image del_stream Error",__func__);
    693         goto end;
    694     }
    695     CDBG("del_stream successfull");
    696 
    697     if (MM_CAMERA_OK != (rc = pme->cam->ops->del_stream(pme->cam->camera_handle,pme->ch_id,pme->stream[MM_CAMERA_SNAPSHOT_THUMBNAIL].id))) {
    698         CDBG_ERROR("%s : Snapshot thumnail image del_stream Error",__func__);
    699         goto end;
    700     }
    701     CDBG("del_stream successfull");
    702 
    703     end:
    704     return rc;
    705 }
    706 
    707 int mm_app_start_snapshot(int cam_id)
    708 {
    709     int rc = MM_CAMERA_OK;
    710     int stream[2];
    711     int op_mode = 0;
    712 
    713     mm_camera_bundle_attr_t attr;
    714 
    715 
    716     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
    717 
    718     if (MM_CAMERA_OK != mm_app_stop_preview(cam_id)) {
    719         CDBG_ERROR("%s: Stop preview Failed cam_id=%d\n",__func__,cam_id);
    720         return -1;
    721     }
    722 
    723     op_mode = MM_CAMERA_OP_MODE_CAPTURE;
    724     mm_app_set_snapshot_mode(cam_id,op_mode);
    725 
    726     pme->cam->ops->prepare_snapshot(pme->cam->camera_handle,pme->ch_id,0);
    727 
    728     if (MM_CAMERA_OK != (rc = mm_app_add_snapshot_stream(cam_id))) {
    729         CDBG_ERROR("%s : Add Snapshot stream err",__func__);
    730     }
    731 
    732     if (MM_CAMERA_OK != (rc = mm_app_config_snapshot_format(cam_id))) {
    733         CDBG_ERROR("%s : Config Snapshot stream err",__func__);
    734     }
    735 
    736     if (MM_CAMERA_OK != (rc = mm_app_streamon_snapshot(cam_id))) {
    737         CDBG_ERROR("%s : Stream on Snapshot stream err",__func__);
    738     }
    739 
    740 #if 0
    741     /*start OMX Jpeg encoder*/
    742 #ifndef DISABLE_JPEG_ENCODING
    743     my_cam_app.hal_lib.omxJpegOpen();
    744 #endif
    745 
    746 #endif
    747     end:
    748     CDBG("%s: END, rc=%d\n", __func__, rc);
    749 
    750     return rc;
    751 }
    752 
    753 
    754 int mm_app_stop_snapshot(int cam_id)
    755 {
    756     int rc = MM_CAMERA_OK;
    757     int stream[2];
    758 
    759     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
    760 
    761     stream[0] = pme->stream[MM_CAMERA_SNAPSHOT_MAIN].id;
    762     stream[1] = pme->stream[MM_CAMERA_SNAPSHOT_THUMBNAIL].id;
    763 
    764     if (MM_CAMERA_OK != (rc = mm_app_streamoff_snapshot(cam_id))) {
    765         CDBG_ERROR("%s : Stream off Snapshot stream err",__func__);
    766     }
    767     pme->cam_state = CAMERA_STATE_OPEN;
    768 #if 0
    769 #ifndef DISABLE_JPEG_ENCODING
    770     my_cam_app.hal_lib.omxJpegClose();
    771 #endif
    772 #endif
    773     end:
    774     CDBG("%s: END, rc=%d\n", __func__, rc);
    775 
    776     return rc;
    777 }
    778 
    779 int mm_app_start_raw_snapshot(int cam_id)
    780 {
    781     int rc = MM_CAMERA_OK;
    782 #if 0
    783     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
    784     mm_camera_channel_attr_t attr;
    785 
    786 
    787     attr.type = MM_CAMERA_CH_ATTR_RAW_STREAMING_TYPE;
    788     attr.raw_streaming_mode = MM_CAMERA_RAW_STREAMING_CAPTURE_SINGLE;
    789 
    790     if (MM_CAMERA_OK != (rc = mm_app_set_op_mode(cam_id, MM_CAMERA_OP_MODE_CAPTURE))) {
    791         CDBG("%s:mm_app_set_op_mode(op_mode=%d) err=%d\n", __func__,
    792              MM_CAMERA_OP_MODE_CAPTURE, rc);
    793         goto end;
    794     }
    795     if (MM_CAMERA_OK != (rc = mm_app_open_ch(cam_id, MM_CAMERA_CH_RAW))) {
    796         CDBG("%s:open raw snapshot channel err=%d\n", __func__, rc);
    797         goto end;
    798     }
    799     if (MM_CAMERA_OK != (rc = mm_app_set_raw_snapshot_fmt(cam_id))) {
    800         CDBG("%s:set raw snapshot format err=%d\n", __func__, rc);
    801         goto end;
    802     }
    803     mm_app_get_dim(cam_id, NULL);
    804     if (MM_CAMERA_OK != (rc = mm_app_prepare_raw_snapshot_buf(cam_id))) {
    805         CDBG("%s:reg raw snapshot buf err=%d\n", __func__, rc);
    806         goto end;
    807     }
    808     if (MM_CAMERA_OK != (rc = mm_app_reg_raw_snapshot_data_cb(cam_id, TRUE))) {
    809         CDBG("%s:reg raw snapshot data cb err=%d\n", __func__, rc);
    810     }
    811     if (MM_CAMERA_OK != (rc = pme->cam->ops->ch_set_attr(pme->cam, MM_CAMERA_CH_RAW, &attr))) {
    812         CDBG("%s:set raw capture attribute err=%d\n", __func__, rc);
    813         goto end;
    814     }
    815     if (MM_CAMERA_OK != (rc = pme->cam->ops->action(pme->cam, TRUE, MM_CAMERA_OPS_RAW, 0))) {
    816         CDBG("%s:snapshot streaming err=%d\n", __func__, rc);
    817         goto end;
    818     }
    819     end:
    820     CDBG("%s: END, rc=%d\n", __func__, rc);
    821 #endif
    822     return rc;
    823 }
    824 
    825 int mm_app_stop_raw_snapshot(int cam_id)
    826 {
    827     int rc = MM_CAMERA_OK;
    828 #if 0
    829     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
    830 
    831 
    832     CDBG("%s: BEGIN\n", __func__);
    833     if (MM_CAMERA_OK != (rc = pme->cam->ops->action(pme->cam, FALSE, MM_CAMERA_OPS_RAW, 0))) {
    834         CDBG("%s:stop raw snapshot streaming err=%d\n", __func__, rc);
    835         goto end;
    836     }
    837     if (MM_CAMERA_OK != (rc = mm_app_unprepare_raw_snapshot_buf(cam_id))) {
    838         CDBG("%s:mm_app_unprepare_raw_snapshot_buf err=%d\n", __func__, rc);
    839         return rc;
    840     }
    841     if (MM_CAMERA_OK != (rc = mm_app_reg_raw_snapshot_data_cb(cam_id, FALSE))) {
    842         CDBG("%s:mm_app_reg_raw_snapshot_data_cb err=%d\n", __func__, rc);
    843         return rc;
    844     }
    845     mm_app_close_ch(cam_id, MM_CAMERA_CH_RAW);
    846     end:
    847     CDBG("%s: END, rc=%d\n", __func__, rc);
    848 #endif
    849     return rc;
    850 }
    851 
    852 static void mm_app_snapshot_wait(int cam_id)
    853 {
    854     pthread_mutex_lock(&g_s_mutex);
    855     if (false == g_status) {
    856         pthread_cond_wait(&g_s_cond_v, &g_s_mutex);
    857         g_status = false;
    858     }
    859     pthread_mutex_unlock(&g_s_mutex);
    860 }
    861 
    862 #if 0
    863 int mm_stream_deinit_thumbnail_buf(uint32_t camera_handle,
    864                                    uint32_t ch_id, uint32_t stream_id,
    865                                    void *user_data, uint8_t num_bufs,
    866                                    mm_camera_buf_def_t *bufs)
    867 {
    868     int i, rc = MM_CAMERA_OK;
    869     mm_camera_app_obj_t *pme = (mm_camera_app_obj_t *)user_data;
    870 
    871     for (i = 0; i < num_bufs; i++) {
    872         rc = my_cam_app.hal_lib.mm_camera_do_munmap_ion (pme->ionfd, &(pme->thumbnail_buf.frame[i].fd_data),
    873                                                          (void *)pme->thumbnail_buf.frame[i].buffer, pme->thumbnail_buf.frame_len);
    874         if (rc != MM_CAMERA_OK) {
    875             CDBG("%s: mm_camera_do_munmap err, pmem_fd = %d, rc = %d",
    876                  __func__, bufs[i].fd, rc);
    877         }
    878     }
    879     return rc;
    880 }
    881 
    882 int mm_stream_deinit_main_buf(uint32_t camera_handle,
    883                               uint32_t ch_id, uint32_t stream_id,
    884                               void *user_data, uint8_t num_bufs,
    885                               mm_camera_buf_def_t *bufs)
    886 {
    887     int i, rc = MM_CAMERA_OK;
    888     mm_camera_app_obj_t *pme = (mm_camera_app_obj_t *)user_data;
    889 
    890     for (i = 0; i < num_bufs; i++) {
    891         rc = my_cam_app.hal_lib.mm_camera_do_munmap_ion (pme->ionfd, &(pme->snapshot_buf.frame[i].fd_data),
    892                                                          (void *)pme->snapshot_buf.frame[i].buffer, pme->snapshot_buf.frame_len);
    893         if (rc != MM_CAMERA_OK) {
    894             CDBG("%s: mm_camera_do_munmap err, pmem_fd = %d, rc = %d",
    895                  __func__, bufs[i].fd, rc);
    896         }
    897     }
    898     return rc;
    899 }
    900 
    901 int mm_stream_init_main_buf(uint32_t camera_handle,
    902                             uint32_t ch_id, uint32_t stream_id,
    903                             void *user_data,
    904                             mm_camera_frame_len_offset *frame_offset_info,
    905                             uint8_t num_bufs,
    906                             uint8_t *initial_reg_flag,
    907                             mm_camera_buf_def_t *bufs)
    908 {
    909     int i,j,num_planes, frame_len, y_off, cbcr_off;
    910     uint32_t planes[VIDEO_MAX_PLANES];
    911     uint32_t pmem_addr = 0;
    912 
    913     mm_camera_app_obj_t *pme = (mm_camera_app_obj_t *)user_data;
    914 
    915     num_planes = frame_offset_info->num_planes;
    916     for ( i = 0; i < num_planes; i++) {
    917         planes[i] = frame_offset_info->mp[i].len;
    918     }
    919 
    920     frame_len = frame_offset_info->frame_len;
    921     y_off = frame_offset_info->mp[0].offset;
    922     cbcr_off = frame_offset_info->mp[1].offset;
    923 
    924     CDBG("Allocating main image Memory for %d buffers frame_len = %d",num_bufs,frame_offset_info->frame_len);
    925 
    926     for (i = 0; i < num_bufs ; i++) {
    927         int j;
    928         if (pme->cam_mode != RECORDER_MODE || pme->fullSizeSnapshot) {
    929             pme->snapshot_buf.reg[i] = 1;
    930             initial_reg_flag[i] = 1;
    931         } else {
    932             pme->snapshot_buf.reg[i] = 0;
    933             initial_reg_flag[i] = 0;
    934         }
    935 
    936         pme->snapshot_buf.frame_len = frame_len;
    937 
    938         pme->snapshot_buf.frame[i].ion_alloc.len = pme->snapshot_buf.frame_len;
    939         pme->snapshot_buf.frame[i].ion_alloc.flags =
    940         (0x1 << CAMERA_ION_HEAP_ID | 0x1 << ION_IOMMU_HEAP_ID);
    941         pme->snapshot_buf.frame[i].ion_alloc.align = 4096;
    942 
    943         pmem_addr = (unsigned long) my_cam_app.hal_lib.mm_camera_do_mmap_ion(pme->ionfd,
    944                                                                              &(pme->snapshot_buf.frame[i].ion_alloc), &(pme->snapshot_buf.frame[i].fd_data),
    945                                                                              &pme->snapshot_buf.frame[i].fd);
    946 
    947         pme->snapshot_buf.frame[i].buffer = pmem_addr;
    948         pme->snapshot_buf.frame[i].path = OUTPUT_TYPE_S;
    949         pme->snapshot_buf.frame[i].y_off = 0;
    950         pme->snapshot_buf.frame[i].cbcr_off = planes[0];
    951         pme->snapshot_buf.frame[i].phy_offset = 0;
    952 
    953         CDBG("Buffer allocated Successfully fd = %d",pme->snapshot_buf.frame[i].fd);
    954 
    955         bufs[i].fd = pme->snapshot_buf.frame[i].fd;
    956         //bufs[i].buffer = pmem_addr;
    957         bufs[i].frame_len = pme->snapshot_buf.frame[i].ion_alloc.len;
    958         bufs[i].num_planes = num_planes;
    959 
    960         bufs[i].frame = &pme->snapshot_buf.frame[i];
    961 
    962         /* Plane 0 needs to be set seperately. Set other planes
    963      * in a loop. */
    964         bufs[i].planes[0].length = planes[0];
    965         bufs[i].planes[0].m.userptr = bufs[i].fd;
    966         bufs[i].planes[0].data_offset = y_off;
    967         bufs[i].planes[0].reserved[0] = 0;
    968         //buf_def->buf.mp[i].frame_offset;
    969         for (j = 1; j < num_planes; j++) {
    970             bufs[i].planes[j].length = planes[j];
    971             bufs[i].planes[j].m.userptr = bufs[i].fd;
    972             bufs[i].planes[j].data_offset = cbcr_off;
    973             bufs[i].planes[j].reserved[0] =
    974             bufs[i].planes[j-1].reserved[0] +
    975             bufs[i].planes[j-1].length;
    976         }
    977     }
    978     return MM_CAMERA_OK;
    979 }
    980 
    981 int mm_stream_init_thumbnail_buf(uint32_t camera_handle,
    982                                  uint32_t ch_id, uint32_t stream_id,
    983                                  void *user_data,
    984                                  mm_camera_frame_len_offset *frame_offset_info,
    985                                  uint8_t num_bufs,
    986                                  uint8_t *initial_reg_flag,
    987                                  mm_camera_buf_def_t *bufs)
    988 {
    989     int i,j,num_planes, frame_len, y_off, cbcr_off;
    990     uint32_t planes[VIDEO_MAX_PLANES];
    991     uint32_t pmem_addr = 0;
    992 
    993     mm_camera_app_obj_t *pme = (mm_camera_app_obj_t *)user_data;
    994 
    995     num_planes = frame_offset_info->num_planes;
    996     for ( i = 0; i < num_planes; i++) {
    997         planes[i] = frame_offset_info->mp[i].len;
    998     }
    999 
   1000     frame_len = frame_offset_info->frame_len;
   1001     y_off = frame_offset_info->mp[0].offset;
   1002     cbcr_off = frame_offset_info->mp[1].offset;
   1003 
   1004     CDBG("Allocating thumanail image  Memory for %d buffers frame_len = %d",num_bufs,frame_offset_info->frame_len);
   1005 
   1006     for (i = 0; i < num_bufs ; i++) {
   1007         int j;
   1008         pme->thumbnail_buf.reg[i] = 1;
   1009         initial_reg_flag[i] = 1;
   1010 
   1011         pme->thumbnail_buf.frame_len = frame_len;
   1012         pme->thumbnail_buf.frame[i].ion_alloc.len = pme->thumbnail_buf.frame_len;
   1013         pme->thumbnail_buf.frame[i].ion_alloc.flags =
   1014         (0x1 << CAMERA_ION_HEAP_ID | 0x1 << ION_IOMMU_HEAP_ID);
   1015         pme->thumbnail_buf.frame[i].ion_alloc.align = 4096;
   1016 
   1017         pmem_addr = (unsigned long) my_cam_app.hal_lib.mm_camera_do_mmap_ion(pme->ionfd,
   1018                                                                              &(pme->thumbnail_buf.frame[i].ion_alloc), &(pme->thumbnail_buf.frame[i].fd_data),
   1019                                                                              &pme->thumbnail_buf.frame[i].fd);
   1020 
   1021         pme->thumbnail_buf.frame[i].buffer = pmem_addr;
   1022         pme->thumbnail_buf.frame[i].path = OUTPUT_TYPE_S;
   1023         pme->thumbnail_buf.frame[i].y_off = 0;
   1024         pme->thumbnail_buf.frame[i].cbcr_off = planes[0];
   1025         pme->thumbnail_buf.frame[i].phy_offset = 0;
   1026 
   1027         CDBG("Buffer allocated Successfully fd = %d",pme->thumbnail_buf.frame[i].fd);
   1028 
   1029         bufs[i].fd = pme->thumbnail_buf.frame[i].fd;
   1030         //bufs[i].buffer = pmem_addr;
   1031         bufs[i].frame_len = pme->thumbnail_buf.frame[i].ion_alloc.len;
   1032         bufs[i].num_planes = num_planes;
   1033 
   1034         bufs[i].frame = &pme->thumbnail_buf.frame[i];
   1035 
   1036         /* Plane 0 needs to be set seperately. Set other planes
   1037      * in a loop. */
   1038         bufs[i].planes[0].length = planes[0];
   1039         bufs[i].planes[0].m.userptr = bufs[i].fd;
   1040         bufs[i].planes[0].data_offset = y_off;
   1041         bufs[i].planes[0].reserved[0] = 0;
   1042         //buf_def->buf.mp[i].frame_offset;
   1043         for (j = 1; j < num_planes; j++) {
   1044             bufs[i].planes[j].length = planes[j];
   1045             bufs[i].planes[j].m.userptr = bufs[i].fd;
   1046             bufs[i].planes[j].data_offset = cbcr_off;
   1047             bufs[i].planes[j].reserved[0] =
   1048             bufs[i].planes[j-1].reserved[0] +
   1049             bufs[i].planes[j-1].length;
   1050         }
   1051     }
   1052     return MM_CAMERA_OK;
   1053 }
   1054 #endif
   1055 #if 0
   1056 int mm_app_bundle_zsl_stream(int cam_id)
   1057 {
   1058     int rc = MM_CAMERA_OK;
   1059     int stream[3];
   1060     mm_camera_bundle_attr_t attr;
   1061 
   1062     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
   1063 
   1064     stream[0] = pme->stream[MM_CAMERA_PREVIEW].id;
   1065     stream[1] = pme->stream[MM_CAMERA_SNAPSHOT_MAIN].id;
   1066 
   1067     attr.notify_mode = MM_CAMERA_SUPER_BUF_NOTIFY_BURST;
   1068     attr.burst_num = 1;
   1069     attr.look_back = 2;
   1070     attr.post_frame_skip = 0;
   1071     attr.water_mark = 2;
   1072 
   1073     if (MM_CAMERA_OK != (rc = pme->cam->ops->init_stream_bundle(
   1074                                                                pme->cam->camera_handle,pme->ch_id,mm_app_zsl_notify_cb,pme,&attr,2,stream))) {
   1075         CDBG_ERROR("%s:init_stream_bundle err=%d\n", __func__, rc);
   1076         rc = -1;
   1077         goto end;
   1078     }
   1079 
   1080     if (MM_CAMERA_OK != (rc = pme->cam->ops->start_streams(pme->cam->camera_handle,pme->ch_id,
   1081                                                            2, stream))) {
   1082         CDBG_ERROR("%s:start_streams err=%d\n", __func__, rc);
   1083         rc = -1;
   1084         goto end;
   1085     }
   1086     end:
   1087     return rc;
   1088 }
   1089 #endif
   1090 
   1091 static void mm_app_live_notify_cb(mm_camera_super_buf_t *bufs,
   1092                                   void *user_data)
   1093 {
   1094 
   1095     int rc;
   1096     int i = 0;
   1097     mm_camera_buf_def_t *main_frame = NULL;
   1098     mm_camera_app_obj_t *pme = NULL;
   1099     CDBG("%s: BEGIN\n", __func__);
   1100 
   1101     pme = (mm_camera_app_obj_t *)user_data;
   1102 
   1103     CDBG("%s : total streams = %d",__func__,bufs->num_bufs);
   1104     main_frame = bufs->bufs[0];
   1105     //thumb_frame = bufs->bufs[1];
   1106 
   1107     CDBG("mainframe frame_idx = %d fd = %d frame length = %d",main_frame->frame_idx,main_frame->fd,main_frame->frame_len);
   1108     //CDBG("thumnail frame_idx = %d fd = %d frame length = %d",thumb_frame->frame_idx,thumb_frame->fd,thumb_frame->frame_len);
   1109 
   1110     //dumpFrameToFile(main_frame->frame,pme->dim.picture_width,pme->dim.picture_height,"main", 1);
   1111 
   1112     dumpFrameToFile(main_frame,pme->dim.picture_width,pme->dim.picture_height,"liveshot_main", 1);
   1113 
   1114     if (MM_CAMERA_OK != pme->cam->ops->qbuf(pme->cam->camera_handle,pme->ch_id,main_frame)) {
   1115         CDBG_ERROR("%s: Failed in thumbnail Qbuf\n", __func__);
   1116     }
   1117     /*if(MM_CAMERA_OK != pme->cam->ops->qbuf(pme->cam->camera_handle,pme->ch_id,thumb_frame))
   1118     {
   1119             CDBG_ERROR("%s: Failed in thumbnail Qbuf\n", __func__);
   1120     }*/
   1121 
   1122     mm_app_snapshot_done();
   1123     CDBG("%s: END\n", __func__);
   1124 
   1125 
   1126 }
   1127 
   1128 int mm_app_prepare_live_snapshot(int cam_id)
   1129 {
   1130     int rc = 0;
   1131     int stream[1];
   1132     mm_camera_bundle_attr_t attr;
   1133     int value = 0;
   1134 
   1135     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
   1136     CDBG("%s:BEGIN, cam_id=%d\n",__func__,cam_id);
   1137 
   1138     stream[0] = pme->stream[MM_CAMERA_SNAPSHOT_MAIN].id;
   1139     //stream[1] = pme->stream[MM_CAMERA_PREVIEW].id;  //Need to clarify
   1140 
   1141     attr.notify_mode = MM_CAMERA_SUPER_BUF_NOTIFY_BURST;
   1142     attr.burst_num = 1;
   1143     attr.look_back = 2;
   1144     attr.post_frame_skip = 0;
   1145     attr.water_mark = 2;
   1146 
   1147 
   1148     if (MM_CAMERA_OK != (rc = pme->cam->ops->set_parm(
   1149                                                      pme->cam->camera_handle,MM_CAMERA_PARM_FULL_LIVESHOT, (void *)&value))) {
   1150         CDBG_ERROR("%s: set dimension err=%d\n", __func__, rc);
   1151     }
   1152 
   1153     if (MM_CAMERA_OK != (rc = pme->cam->ops->init_stream_bundle(
   1154                                                                pme->cam->camera_handle,pme->ch_id,mm_app_live_notify_cb,pme,&attr,1,stream))) {
   1155         CDBG_ERROR("%s:init_stream_bundle err=%d\n", __func__, rc);
   1156         rc = -1;
   1157         goto end;
   1158     }
   1159 
   1160     if (MM_CAMERA_OK != (rc = pme->cam->ops->start_streams(pme->cam->camera_handle,pme->ch_id,
   1161                                                            1, stream))) {
   1162         CDBG_ERROR("%s:start_streams err=%d\n", __func__, rc);
   1163         rc = -1;
   1164         goto end;
   1165     }
   1166 
   1167 
   1168     end:
   1169     CDBG("%s:END, cam_id=%d\n",__func__,cam_id);
   1170     return rc;
   1171 }
   1172 
   1173 int mm_app_unprepare_live_snapshot(int cam_id)
   1174 {
   1175     int rc = 0;
   1176     int stream[2];
   1177 
   1178     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
   1179     CDBG("%s:BEGIN, cam_id=%d\n",__func__,cam_id);
   1180 
   1181     stream[0] = pme->stream[MM_CAMERA_SNAPSHOT_MAIN].id;
   1182     if (MM_CAMERA_OK != (rc = pme->cam->ops->stop_streams(pme->cam->camera_handle,pme->ch_id,1,&stream))) {
   1183         CDBG_ERROR("%s : Snapshot Stream off Error",__func__);
   1184         return -1;
   1185     }
   1186 
   1187     if (MM_CAMERA_OK != (rc = pme->cam->ops->destroy_stream_bundle(pme->cam->camera_handle,pme->ch_id))) {
   1188         CDBG_ERROR("%s : Snapshot destroy_stream_bundle Error",__func__);
   1189         return -1;
   1190     }
   1191     CDBG("%s:END, cam_id=%d\n",__func__,cam_id);
   1192     return rc;
   1193 }
   1194 
   1195 int mm_app_take_live_snapshot(int cam_id)
   1196 {
   1197     int rc = 0;
   1198     int stream[3];
   1199     mm_camera_bundle_attr_t attr;
   1200 
   1201     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
   1202 
   1203     CDBG("%s:BEGIN, cam_id=%d\n",__func__,cam_id);
   1204 
   1205     if (pme->cam_mode == RECORDER_MODE &&
   1206         pme->cam_state == CAMERA_STATE_RECORD) {
   1207         //Code to get live shot
   1208         if (mm_app_prepare_live_snapshot(cam_id) != MM_CAMERA_OK) {
   1209             CDBG_ERROR("%s: Failed prepare liveshot",__func__);
   1210             return -1;
   1211         }
   1212         if (MM_CAMERA_OK != (rc =pme->cam->ops->request_super_buf(pme->cam->camera_handle,pme->ch_id))) {
   1213             CDBG_ERROR("%s:request_super_buf err=%d\n", __func__, rc);
   1214             return -1;
   1215         }
   1216         CDBG("%s:waiting images\n",__func__);
   1217         mm_app_snapshot_wait(cam_id);
   1218 
   1219         if (MM_CAMERA_OK !=mm_app_unprepare_live_snapshot(cam_id)) {
   1220             CDBG_ERROR("%s: Snapshot Stop error",__func__);
   1221         }
   1222 
   1223     } else {
   1224         CDBG_ERROR("%s: Should not come here for liveshot",__func__);
   1225     }
   1226     return rc;
   1227 }
   1228 
   1229 int mm_app_take_picture_zsl(int cam_id)
   1230 {
   1231     int rc = MM_CAMERA_OK;
   1232     int value = 1;
   1233     int op_mode;
   1234 
   1235     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
   1236 
   1237     CDBG("%s: Take picture ZSL",__func__);
   1238 
   1239     if (MM_CAMERA_OK != (rc =pme->cam->ops->request_super_buf(pme->cam->camera_handle,pme->ch_id))) {
   1240         CDBG_ERROR("%s:request_super_buf err=%d\n", __func__, rc);
   1241         goto end;
   1242     }
   1243 
   1244     CDBG("%s: Start ZSL Preview",__func__);
   1245 
   1246     end:
   1247     CDBG("%s: END, rc=%d\n", __func__, rc);
   1248     return rc;
   1249 }
   1250 
   1251 int mm_app_take_picture_yuv(int cam_id)
   1252 {
   1253     int rc;
   1254     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
   1255 
   1256     CDBG("%s:BEGIN, cam_id=%d\n",__func__,cam_id);
   1257 
   1258     if (MM_CAMERA_OK != mm_app_start_snapshot(cam_id)) {
   1259         CDBG_ERROR("%s: cam_id=%d\n",__func__,cam_id);
   1260         rc = -1;
   1261         goto end;
   1262     }
   1263 
   1264     CDBG("%s:waiting images\n",__func__);
   1265     mm_app_snapshot_wait(cam_id);
   1266 
   1267     if (MM_CAMERA_OK !=mm_app_stop_snapshot(cam_id)) {
   1268         CDBG_ERROR("%s: Snapshot Stop error",__func__);
   1269     }
   1270 
   1271     preview:
   1272     if (MM_CAMERA_OK != (rc = mm_app_start_preview(cam_id))) {
   1273         CDBG("%s:preview start stream err=%d\n", __func__, rc);
   1274     }
   1275     end:
   1276     CDBG("%s:END, cam_id=%d\n",__func__,cam_id);
   1277     return rc;
   1278 }
   1279 
   1280 int mm_app_take_zsl(int cam_id)
   1281 {
   1282     int rc = MM_CAMERA_OK;
   1283 
   1284     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
   1285 
   1286     CDBG("%s:BEGIN, cam_id=%d\n",__func__,cam_id);
   1287 
   1288     if (pme->cam_mode == RECORDER_MODE) {
   1289         switch (pme->cam_state) {
   1290         case CAMERA_STATE_RECORD:
   1291             if (MM_CAMERA_OK != mm_app_stop_video(cam_id)) {
   1292                 CDBG_ERROR("%s:Cannot stop video err=%d\n", __func__, rc);
   1293                 return -1;
   1294             }
   1295         case CAMERA_STATE_PREVIEW:
   1296             if (MM_CAMERA_OK != mm_app_open_zsl(cam_id)) {
   1297                 CDBG_ERROR("%s: Cannot switch to camera mode=%d\n", __func__);
   1298                 return -1;
   1299             }
   1300             break;
   1301         case CAMERA_STATE_SNAPSHOT:
   1302         default:
   1303             CDBG("%s: Cannot normal pciture in record mode\n", __func__);
   1304             break;
   1305         }
   1306     } else if (pme->cam_mode == CAMERA_MODE) {
   1307         switch (pme->cam_state) {
   1308         case CAMERA_STATE_PREVIEW:
   1309             mm_app_open_zsl(cam_id);
   1310             break;
   1311 
   1312         case CAMERA_STATE_SNAPSHOT:
   1313         case CAMERA_STATE_RECORD:
   1314         default:
   1315             CDBG("%s: Cannot normal pciture in record mode\n", __func__);
   1316             break;
   1317         }
   1318     }
   1319 
   1320     if (pme->cam_mode == ZSL_MODE && pme->cam_state == CAMERA_STATE_PREVIEW) {
   1321         mm_app_take_picture_zsl(cam_id);
   1322     }
   1323     return rc;
   1324 }
   1325 
   1326 int mm_app_take_picture(int cam_id)
   1327 {
   1328     int rc = 0;
   1329 
   1330     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
   1331 
   1332     CDBG("%s:BEGIN, cam_id=%d\n",__func__,cam_id);
   1333 
   1334     if (pme->cam_mode == RECORDER_MODE) {
   1335         switch (pme->cam_state) {
   1336         case CAMERA_STATE_RECORD:
   1337             if (MM_CAMERA_OK != mm_app_stop_video(cam_id)) {
   1338                 CDBG_ERROR("%s:Cannot stop video err=%d\n", __func__, rc);
   1339                 return -1;
   1340             }
   1341         case CAMERA_STATE_PREVIEW:
   1342             if (MM_CAMERA_OK != mm_app_open_camera(cam_id)) {
   1343                 CDBG_ERROR("%s: Cannot switch to camera mode=%d\n", __func__,rc);
   1344                 return -1;
   1345             }
   1346             break;
   1347         case CAMERA_STATE_SNAPSHOT:
   1348         default:
   1349             CDBG("%s: Cannot normal pciture in record mode\n", __func__);
   1350             break;
   1351         }
   1352     } else if (pme->cam_mode == ZSL_MODE) {
   1353         switch (pme->cam_state) {
   1354         case CAMERA_STATE_PREVIEW:
   1355             mm_app_open_camera(cam_id);
   1356             break;
   1357 
   1358         case CAMERA_STATE_SNAPSHOT:
   1359         case CAMERA_STATE_RECORD:
   1360         default:
   1361             CDBG("%s: Cannot normal pciture in record mode\n", __func__);
   1362             break;
   1363         }
   1364     }
   1365 
   1366     CDBG("%s : Takepicture : mode = %d state = %d, rc = %d",__func__,pme->cam_mode,pme->cam_state,rc);
   1367     if (pme->cam_mode == CAMERA_MODE && pme->cam_state == CAMERA_STATE_PREVIEW) {
   1368         mm_app_take_picture_yuv(cam_id);
   1369     }
   1370     return rc;
   1371 }
   1372 
   1373 int mm_app_take_raw_picture(int cam_id)
   1374 {
   1375     int rc;
   1376 #if 0
   1377     mm_camera_app_obj_t *pme = mm_app_get_cam_obj(cam_id);
   1378 
   1379     CDBG("%s:BEGIN, cam_id=%d\n",__func__,cam_id);
   1380     g_status = FALSE;
   1381     if (MM_CAMERA_OK != (rc = pme->cam->ops->action(pme->cam, TRUE, MM_CAMERA_OPS_PREPARE_SNAPSHOT, 0))) {
   1382         CDBG("%s:prepare snapshot err=%d\n", __func__, rc);
   1383         goto end;
   1384     }
   1385     if (MM_CAMERA_OK != (rc = mm_app_stop_preview(cam_id))) {
   1386         CDBG("%s:mm_app_stop_preview err=%d\n", __func__, rc);
   1387         goto end;
   1388     }
   1389     if (MM_CAMERA_OK != mm_app_start_raw_snapshot(cam_id))
   1390         goto preview;
   1391     CDBG("%s:waiting images\n",__func__);
   1392     mm_app_snapshot_wait(cam_id);
   1393     CDBG("%s:calling mm_app_stop_snapshot() \n",__func__);
   1394     mm_app_stop_raw_snapshot(cam_id);
   1395     preview:
   1396     mm_app_start_preview(cam_id);
   1397     end:
   1398     CDBG("%s:END, cam_id=%d\n",__func__,cam_id);
   1399 #endif
   1400     return rc;
   1401 }
   1402 
   1403