QCameraPostProc.cpp revision 6f83d735d8e3b918da42e6b559fcd0efb78133e5
1/* Copyright (c) 2012-2013, The Linux Foundataion. All rights reserved.
2*
3* Redistribution and use in source and binary forms, with or without
4* modification, are permitted provided that the following conditions are
5* met:
6*     * Redistributions of source code must retain the above copyright
7*       notice, this list of conditions and the following disclaimer.
8*     * Redistributions in binary form must reproduce the above
9*       copyright notice, this list of conditions and the following
10*       disclaimer in the documentation and/or other materials provided
11*       with the distribution.
12*     * Neither the name of The Linux Foundation nor the names of its
13*       contributors may be used to endorse or promote products derived
14*       from this software without specific prior written permission.
15*
16* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19* ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*
28*/
29
30#define LOG_TAG "QCameraPostProc"
31
32#include <stdlib.h>
33#include <utils/Errors.h>
34
35#include "QCamera2HWI.h"
36#include "QCameraPostProc.h"
37
38namespace qcamera {
39
40/*===========================================================================
41 * FUNCTION   : QCameraPostProcessor
42 *
43 * DESCRIPTION: constructor of QCameraPostProcessor.
44 *
45 * PARAMETERS :
46 *   @cam_ctrl : ptr to HWI object
47 *
48 * RETURN     : None
49 *==========================================================================*/
50QCameraPostProcessor::QCameraPostProcessor(QCamera2HardwareInterface *cam_ctrl)
51    : m_parent(cam_ctrl),
52      mJpegCB(NULL),
53      mJpegUserData(NULL),
54      mJpegClientHandle(0),
55      mJpegSessionId(0),
56      m_pJpegOutputMem(NULL),
57      m_pJpegExifObj(NULL),
58      m_bThumbnailNeeded(TRUE),
59      m_pReprocChannel(NULL),
60      m_inputPPQ(releasePPInputData, this),
61      m_ongoingPPQ(releaseOngoingPPData, this),
62      m_inputJpegQ(releaseJpegData, this),
63      m_ongoingJpegQ(releaseJpegData, this),
64      m_inputRawQ(releasePPInputData, this)
65{
66    memset(&mJpegHandle, 0, sizeof(mJpegHandle));
67}
68
69/*===========================================================================
70 * FUNCTION   : ~QCameraPostProcessor
71 *
72 * DESCRIPTION: deconstructor of QCameraPostProcessor.
73 *
74 * PARAMETERS : None
75 *
76 * RETURN     : None
77 *==========================================================================*/
78QCameraPostProcessor::~QCameraPostProcessor()
79{
80    if (m_pJpegOutputMem != NULL) {
81        m_pJpegOutputMem->deallocate();
82        delete m_pJpegOutputMem;
83        m_pJpegOutputMem = NULL;
84    }
85    if (m_pJpegExifObj != NULL) {
86        delete m_pJpegExifObj;
87        m_pJpegExifObj = NULL;
88    }
89    if (m_pReprocChannel != NULL) {
90        m_pReprocChannel->stop();
91        delete m_pReprocChannel;
92        m_pReprocChannel = NULL;
93    }
94}
95
96/*===========================================================================
97 * FUNCTION   : init
98 *
99 * DESCRIPTION: initialization of postprocessor
100 *
101 * PARAMETERS :
102 *   @jpeg_cb      : callback to handle jpeg event from mm-camera-interface
103 *   @user_data    : user data ptr for jpeg callback
104 *
105 * RETURN     : int32_t type of status
106 *              NO_ERROR  -- success
107 *              none-zero failure code
108 *==========================================================================*/
109int32_t QCameraPostProcessor::init(jpeg_encode_callback_t jpeg_cb, void *user_data)
110{
111    mJpegCB = jpeg_cb;
112    mJpegUserData = user_data;
113
114    mJpegClientHandle = jpeg_open(&mJpegHandle);
115    if(!mJpegClientHandle) {
116        ALOGE("%s : jpeg_open did not work", __func__);
117        return UNKNOWN_ERROR;
118    }
119
120    m_dataProcTh.launch(dataProcessRoutine, this);
121
122    return NO_ERROR;
123}
124
125/*===========================================================================
126 * FUNCTION   : deinit
127 *
128 * DESCRIPTION: de-initialization of postprocessor
129 *
130 * PARAMETERS : None
131 *
132 * RETURN     : int32_t type of status
133 *              NO_ERROR  -- success
134 *              none-zero failure code
135 *==========================================================================*/
136int32_t QCameraPostProcessor::deinit()
137{
138    m_dataProcTh.exit();
139
140    if(mJpegClientHandle > 0) {
141        int rc = mJpegHandle.close(mJpegClientHandle);
142        ALOGE("%s: Jpeg closed, rc = %d, mJpegClientHandle = %x",
143              __func__, rc, mJpegClientHandle);
144        mJpegClientHandle = 0;
145        memset(&mJpegHandle, 0, sizeof(mJpegHandle));
146    }
147
148    return NO_ERROR;
149}
150
151/*===========================================================================
152 * FUNCTION   : start
153 *
154 * DESCRIPTION: start postprocessor. Data process thread and data notify thread
155 *              will be launched.
156 *
157 * PARAMETERS :
158 *   @pSrcChannel : source channel obj ptr that possibly needs reprocess
159 *
160 * RETURN     : int32_t type of status
161 *              NO_ERROR  -- success
162 *              none-zero failure code
163 *
164 * NOTE       : if any reprocess is needed, a reprocess channel/stream
165 *              will be started.
166 *==========================================================================*/
167int32_t QCameraPostProcessor::start(QCameraChannel *pSrcChannel)
168{
169    int32_t rc = NO_ERROR;
170    if (m_parent->needReprocess()) {
171        if (m_pReprocChannel != NULL) {
172            delete m_pReprocChannel;
173            m_pReprocChannel = NULL;
174        }
175        // if reprocess is needed, start reprocess channel
176        m_pReprocChannel = m_parent->addOnlineReprocChannel(pSrcChannel);
177        if (m_pReprocChannel == NULL) {
178            ALOGE("%s: cannot add reprocess channel", __func__);
179            return UNKNOWN_ERROR;
180        }
181
182        rc = m_pReprocChannel->start(m_parent->mParameters);
183        if (rc != 0) {
184            ALOGE("%s: cannot start reprocess channel", __func__);
185            delete m_pReprocChannel;
186            m_pReprocChannel = NULL;
187            return rc;
188        }
189    }
190
191    m_dataProcTh.sendCmd(CAMERA_CMD_TYPE_START_DATA_PROC, FALSE, FALSE);
192    m_parent->m_cbNotifier.startSnapshots();
193
194    return rc;
195}
196
197/*===========================================================================
198 * FUNCTION   : stop
199 *
200 * DESCRIPTION: stop postprocessor. Data process and notify thread will be stopped.
201 *
202 * PARAMETERS : None
203 *
204 * RETURN     : int32_t type of status
205 *              NO_ERROR  -- success
206 *              none-zero failure code
207 *
208 * NOTE       : reprocess channel will be stopped and deleted if there is any
209 *==========================================================================*/
210int32_t QCameraPostProcessor::stop()
211{
212    m_parent->m_cbNotifier.stopSnapshots();
213    // dataProc Thread need to process "stop" as sync call because abort jpeg job should be a sync call
214    m_dataProcTh.sendCmd(CAMERA_CMD_TYPE_STOP_DATA_PROC, TRUE, TRUE);
215
216    return NO_ERROR;
217}
218
219/*===========================================================================
220 * FUNCTION   : getJpegEncodingConfig
221 *
222 * DESCRIPTION: function to prepare encoding job information
223 *
224 * PARAMETERS :
225 *   @encode_parm   : param to be filled with encoding configuration
226 *
227 * RETURN     : int32_t type of status
228 *              NO_ERROR  -- success
229 *              none-zero failure code
230 *==========================================================================*/
231int32_t QCameraPostProcessor::getJpegEncodingConfig(mm_jpeg_encode_params_t& encode_parm,
232                                                    QCameraStream *main_stream,
233                                                    QCameraStream *thumb_stream)
234{
235    ALOGV("%s : E", __func__);
236    int32_t ret = NO_ERROR;
237    camera_memory_t *jpeg_mem = NULL;
238
239    encode_parm.jpeg_cb = mJpegCB;
240    encode_parm.userdata = mJpegUserData;
241
242    m_bThumbnailNeeded = TRUE; // need encode thumbnail by default
243    cam_dimension_t thumbnailSize;
244    memset(&thumbnailSize, 0, sizeof(cam_dimension_t));
245    m_parent->getThumbnailSize(thumbnailSize);
246    if (thumbnailSize.width == 0 && thumbnailSize.height == 0) {
247        // (0,0) means no thumbnail
248        m_bThumbnailNeeded = FALSE;
249    }
250    encode_parm.encode_thumbnail = m_bThumbnailNeeded;
251
252    // get color format
253    cam_format_t img_fmt = CAM_FORMAT_YUV_420_NV12;
254    main_stream->getFormat(img_fmt);
255    encode_parm.color_format = getColorfmtFromImgFmt(img_fmt);
256
257    // get jpeg quality
258    encode_parm.quality = m_parent->getJpegQuality();
259    if (encode_parm.quality <= 0) {
260        encode_parm.quality = 85;
261    }
262
263    // get exif data
264    if (m_pJpegExifObj != NULL) {
265        delete m_pJpegExifObj;
266        m_pJpegExifObj = NULL;
267    }
268    m_pJpegExifObj = m_parent->getExifData();
269    if (m_pJpegExifObj != NULL) {
270        encode_parm.exif_info.exif_data = m_pJpegExifObj->getEntries();
271        encode_parm.exif_info.numOfEntries = m_pJpegExifObj->getNumOfEntries();
272    }
273
274    cam_frame_len_offset_t main_offset;
275    memset(&main_offset, 0, sizeof(cam_frame_len_offset_t));
276    main_stream->getFrameOffset(main_offset);
277
278    // src buf config
279    QCameraMemory *pStreamMem = main_stream->getStreamBufs();
280    if (pStreamMem == NULL) {
281        ALOGE("%s: cannot get stream bufs from main stream", __func__);
282        ret = BAD_VALUE;
283        goto on_error;
284    }
285    encode_parm.num_src_bufs = pStreamMem->getCnt();
286    for (uint32_t i = 0; i < encode_parm.num_src_bufs; i++) {
287        camera_memory_t *stream_mem = pStreamMem->getMemory(i, false);
288        if (stream_mem != NULL) {
289            encode_parm.src_main_buf[i].index = i;
290            encode_parm.src_main_buf[i].buf_size = stream_mem->size;
291            encode_parm.src_main_buf[i].buf_vaddr = (uint8_t *)stream_mem->data;
292            encode_parm.src_main_buf[i].fd = pStreamMem->getFd(i);
293            encode_parm.src_main_buf[i].format = MM_JPEG_FMT_YUV;
294            encode_parm.src_main_buf[i].offset = main_offset;
295        }
296    }
297
298    if (m_bThumbnailNeeded == TRUE) {
299        if (thumb_stream == NULL) {
300            thumb_stream = main_stream;
301        }
302        pStreamMem = thumb_stream->getStreamBufs();
303        if (pStreamMem == NULL) {
304            ALOGE("%s: cannot get stream bufs from thumb stream", __func__);
305            ret = BAD_VALUE;
306            goto on_error;
307        }
308        cam_frame_len_offset_t thumb_offset;
309        memset(&thumb_offset, 0, sizeof(cam_frame_len_offset_t));
310        thumb_stream->getFrameOffset(thumb_offset);
311        for (int i = 0; i < pStreamMem->getCnt(); i++) {
312            camera_memory_t *stream_mem = pStreamMem->getMemory(i, false);
313            if (stream_mem != NULL) {
314                encode_parm.src_thumb_buf[i].index = i;
315                encode_parm.src_thumb_buf[i].buf_size = stream_mem->size;
316                encode_parm.src_thumb_buf[i].buf_vaddr = (uint8_t *)stream_mem->data;
317                encode_parm.src_thumb_buf[i].fd = pStreamMem->getFd(i);
318                encode_parm.src_thumb_buf[i].format = MM_JPEG_FMT_YUV;
319                encode_parm.src_thumb_buf[i].offset = thumb_offset;
320            }
321        }
322    }
323
324    // allocate output buf for jpeg encoding
325    if (m_pJpegOutputMem != NULL) {
326        m_pJpegOutputMem->deallocate();
327        delete m_pJpegOutputMem;
328        m_pJpegOutputMem = NULL;
329    }
330    m_pJpegOutputMem = new QCameraStreamMemory(m_parent->mGetMemory);
331    if (NULL == m_pJpegOutputMem) {
332        ret = NO_MEMORY;
333        ALOGE("%s : No memory for m_pJpegOutputMem", __func__);
334        goto on_error;
335    }
336    ret = m_pJpegOutputMem->allocate(1, main_offset.frame_len);
337    if(ret != OK) {
338        ret = NO_MEMORY;
339        ALOGE("%s : No memory for m_pJpegOutputMem", __func__);
340        goto on_error;
341    }
342    jpeg_mem = m_pJpegOutputMem->getMemory(0, false);
343    if (NULL == jpeg_mem) {
344        ret = NO_MEMORY;
345        ALOGE("%s : initHeapMem for jpeg, ret = NO_MEMORY", __func__);
346        goto on_error;
347    }
348    encode_parm.num_dst_bufs = 1;
349    encode_parm.dest_buf[0].index = 0;
350    encode_parm.dest_buf[0].buf_size = jpeg_mem->size;
351    encode_parm.dest_buf[0].buf_vaddr = (uint8_t *)jpeg_mem->data;
352    encode_parm.dest_buf[0].fd = m_pJpegOutputMem->getFd(0);
353    encode_parm.dest_buf[0].format = MM_JPEG_FMT_YUV;
354    encode_parm.dest_buf[0].offset = main_offset;
355
356    ALOGV("%s : X", __func__);
357    return NO_ERROR;
358
359on_error:
360    if (m_pJpegOutputMem != NULL) {
361        m_pJpegOutputMem->deallocate();
362        delete m_pJpegOutputMem;
363        m_pJpegOutputMem = NULL;
364    }
365    if (m_pJpegExifObj != NULL) {
366        delete m_pJpegExifObj;
367        m_pJpegExifObj = NULL;
368    }
369    ALOGV("%s : X with error %d", __func__, ret);
370    return ret;
371}
372
373/*===========================================================================
374 * FUNCTION   : sendEvtNotify
375 *
376 * DESCRIPTION: send event notify through notify callback registered by upper layer
377 *
378 * PARAMETERS :
379 *   @msg_type: msg type of notify
380 *   @ext1    : extension
381 *   @ext2    : extension
382 *
383 * RETURN     : int32_t type of status
384 *              NO_ERROR  -- success
385 *              none-zero failure code
386 *==========================================================================*/
387int32_t QCameraPostProcessor::sendEvtNotify(int32_t msg_type,
388                                            int32_t ext1,
389                                            int32_t ext2)
390{
391    return m_parent->sendEvtNotify(msg_type, ext1, ext2);
392}
393
394/*===========================================================================
395 * FUNCTION   : sendDataNotify
396 *
397 * DESCRIPTION: enqueue data into dataNotify thread
398 *
399 * PARAMETERS :
400 *   @msg_type: data callback msg type
401 *   @data    : ptr to data memory struct
402 *   @index   : index to data buffer
403 *   @metadata: ptr to meta data buffer if there is any
404 *   @release_data : ptr to struct indicating if data need to be released
405 *                   after notify
406 *
407 * RETURN     : int32_t type of status
408 *              NO_ERROR  -- success
409 *              none-zero failure code
410 *==========================================================================*/
411int32_t QCameraPostProcessor::sendDataNotify(int32_t msg_type,
412                                             camera_memory_t *data,
413                                             uint8_t index,
414                                             camera_frame_metadata_t *metadata,
415                                             qcamera_release_data_t *release_data)
416{
417    qcamera_data_argm_t *data_cb = (qcamera_data_argm_t *)malloc(sizeof(qcamera_data_argm_t));
418    if (NULL == data_cb) {
419        ALOGE("%s: no mem for acamera_data_argm_t", __func__);
420        return NO_MEMORY;
421    }
422    memset(data_cb, 0, sizeof(qcamera_data_argm_t));
423    data_cb->msg_type = msg_type;
424    data_cb->data = data;
425    data_cb->index = index;
426    data_cb->metadata = metadata;
427    if (release_data != NULL) {
428        data_cb->release_data = *release_data;
429    }
430
431    qcamera_callback_argm_t cbArg;
432    memset(&cbArg, 0, sizeof(qcamera_callback_argm_t));
433    cbArg.cb_type = QCAMERA_DATA_SNAPSHOT_CALLBACK;
434    cbArg.msg_type = msg_type;
435    cbArg.data = data;
436    cbArg.metadata = metadata;
437    cbArg.user_data = data_cb;
438    cbArg.cookie = this;
439    cbArg.release_cb = releaseNotifyData;
440    int rc = m_parent->m_cbNotifier.notifyCallback(cbArg);
441    if ( NO_ERROR != rc ) {
442        ALOGE("%s: Error enqueuing jpeg data into notify queue", __func__);
443        free(data_cb);
444        return UNKNOWN_ERROR;
445    }
446
447    return rc;
448}
449
450/*===========================================================================
451 * FUNCTION   : processData
452 *
453 * DESCRIPTION: enqueue data into dataProc thread
454 *
455 * PARAMETERS :
456 *   @frame   : process frame received from mm-camera-interface
457 *
458 * RETURN     : int32_t type of status
459 *              NO_ERROR  -- success
460 *              none-zero failure code
461 *
462 * NOTE       : depends on if offline reprocess is needed, received frame will
463 *              be sent to either input queue of postprocess or jpeg encoding
464 *==========================================================================*/
465int32_t QCameraPostProcessor::processData(mm_camera_super_buf_t *frame)
466{
467    if (m_parent->needReprocess()) {
468        ALOGD("%s: need reprocess", __func__);
469        // enqueu to post proc input queue
470        m_inputPPQ.enqueue((void *)frame);
471    } else {
472        ALOGD("%s: no need offline reprocess, sending to jpeg encoding", __func__);
473        qcamera_jpeg_data_t *jpeg_job =
474            (qcamera_jpeg_data_t *)malloc(sizeof(qcamera_jpeg_data_t));
475        if (jpeg_job == NULL) {
476            ALOGE("%s: No memory for jpeg job", __func__);
477            return NO_MEMORY;
478        }
479
480        memset(jpeg_job, 0, sizeof(qcamera_jpeg_data_t));
481        jpeg_job->src_frame = frame;
482
483        // enqueu to jpeg input queue
484        m_inputJpegQ.enqueue((void *)jpeg_job);
485    }
486    m_dataProcTh.sendCmd(CAMERA_CMD_TYPE_DO_NEXT_JOB, FALSE, FALSE);
487
488    return NO_ERROR;
489}
490
491/*===========================================================================
492 * FUNCTION   : processRawData
493 *
494 * DESCRIPTION: enqueue raw data into dataProc thread
495 *
496 * PARAMETERS :
497 *   @frame   : process frame received from mm-camera-interface
498 *
499 * RETURN     : int32_t type of status
500 *              NO_ERROR  -- success
501 *              none-zero failure code
502 *==========================================================================*/
503int32_t QCameraPostProcessor::processRawData(mm_camera_super_buf_t *frame)
504{
505    // enqueu to raw input queue
506    m_inputRawQ.enqueue((void *)frame);
507    m_dataProcTh.sendCmd(CAMERA_CMD_TYPE_DO_NEXT_JOB, FALSE, FALSE);
508    return NO_ERROR;
509}
510
511/*===========================================================================
512 * FUNCTION   : processJpegEvt
513 *
514 * DESCRIPTION: process jpeg event from mm-jpeg-interface.
515 *
516 * PARAMETERS :
517 *   @evt     : payload of jpeg event, including information about jpeg encoding
518 *              status, jpeg size and so on.
519 *
520 * RETURN     : int32_t type of status
521 *              NO_ERROR  -- success
522 *              none-zero failure code
523 *
524 * NOTE       : This event will also trigger DataProc thread to move to next job
525 *              processing (i.e., send a new jpeg encoding job to mm-jpeg-interface
526 *              if there is any pending job in jpeg input queue)
527 *==========================================================================*/
528int32_t QCameraPostProcessor::processJpegEvt(qcamera_jpeg_evt_payload_t *evt)
529{
530    int32_t rc = NO_ERROR;
531    camera_memory_t *jpeg_mem = NULL;
532
533    // find job by jobId
534    qcamera_jpeg_data_t *job = findJpegJobByJobId(evt->jobId);
535
536    if (job == NULL) {
537        ALOGE("%s: Cannot find jpeg job by jobId(%d)", __func__, evt->jobId);
538        rc = BAD_VALUE;
539        goto end;
540    }
541
542    if (m_parent->mDataCb == NULL ||
543        m_parent->msgTypeEnabledWithLock(CAMERA_MSG_COMPRESSED_IMAGE) == 0 ) {
544        ALOGD("%s: No dataCB or CAMERA_MSG_COMPRESSED_IMAGE not enabled",
545              __func__);
546        rc = NO_ERROR;
547        goto end;
548    }
549
550    if(evt->status == JPEG_JOB_STATUS_ERROR) {
551        ALOGE("%s: Error event handled from jpeg, status = %d",
552              __func__, evt->status);
553        rc = FAILED_TRANSACTION;
554        goto end;
555    }
556
557    m_parent->dumpFrameToFile(evt->out_data.buf_vaddr,
558                              evt->out_data.buf_filled_len,
559                              evt->jobId,
560                              QCAMERA_DUMP_FRM_JPEG);
561    ALOGD("%s: Dump jpeg_size=%d", __func__, evt->out_data.buf_filled_len);
562
563    // alloc jpeg memory to pass to upper layer
564    jpeg_mem = m_parent->mGetMemory(-1, evt->out_data.buf_filled_len, 1, m_parent->mCallbackCookie);
565    if (NULL == jpeg_mem) {
566        rc = NO_MEMORY;
567        ALOGE("%s : getMemory for jpeg, ret = NO_MEMORY", __func__);
568        goto end;
569    }
570    memcpy(jpeg_mem->data, evt->out_data.buf_vaddr, evt->out_data.buf_filled_len);
571
572    ALOGE("%s : Calling upperlayer callback to store JPEG image", __func__);
573    qcamera_release_data_t release_data;
574    memset(&release_data, 0, sizeof(qcamera_release_data_t));
575    release_data.data = jpeg_mem;
576    rc = sendDataNotify(CAMERA_MSG_COMPRESSED_IMAGE,
577                        jpeg_mem,
578                        0,
579                        NULL,
580                        &release_data);
581
582end:
583    if (rc != NO_ERROR) {
584        // send error msg to upper layer
585        sendDataNotify(CAMERA_MSG_COMPRESSED_IMAGE,
586                       NULL,
587                       0,
588                       NULL,
589                       NULL);
590
591        if (NULL != jpeg_mem) {
592            jpeg_mem->release(jpeg_mem);
593            jpeg_mem = NULL;
594        }
595    }
596
597    // release internal data for jpeg job
598    if (job != NULL) {
599        releaseJpegJobData(job);
600        free(job);
601    }
602
603    // wait up data proc thread to do next job,
604    // if previous request is blocked due to ongoing jpeg job
605    m_dataProcTh.sendCmd(CAMERA_CMD_TYPE_DO_NEXT_JOB, FALSE, FALSE);
606
607    return rc;
608}
609
610/*===========================================================================
611 * FUNCTION   : processPPData
612 *
613 * DESCRIPTION: process received frame after reprocess.
614 *
615 * PARAMETERS :
616 *   @frame   : received frame from reprocess channel.
617 *
618 * RETURN     : int32_t type of status
619 *              NO_ERROR  -- success
620 *              none-zero failure code
621 *
622 * NOTE       : The frame after reprocess need to send to jpeg encoding.
623 *==========================================================================*/
624int32_t QCameraPostProcessor::processPPData(mm_camera_super_buf_t *frame)
625{
626    qcamera_pp_data_t *job = (qcamera_pp_data_t *)m_ongoingPPQ.dequeue();
627
628    if (job == NULL || job->src_frame == NULL) {
629        ALOGE("%s: Cannot find reprocess job", __func__);
630        return BAD_VALUE;
631    }
632
633    qcamera_jpeg_data_t *jpeg_job =
634        (qcamera_jpeg_data_t *)malloc(sizeof(qcamera_jpeg_data_t));
635    if (jpeg_job == NULL) {
636        ALOGE("%s: No memory for jpeg job", __func__);
637        return NO_MEMORY;
638    }
639
640    memset(jpeg_job, 0, sizeof(qcamera_jpeg_data_t));
641    jpeg_job->src_frame = frame;
642    jpeg_job->src_reproc_frame = job->src_frame;
643
644    // free pp job buf
645    free(job);
646
647    // enqueu reprocessed frame to jpeg input queue
648    m_inputJpegQ.enqueue((void *)jpeg_job);
649
650    // wait up data proc thread
651    m_dataProcTh.sendCmd(CAMERA_CMD_TYPE_DO_NEXT_JOB, FALSE, FALSE);
652
653    return NO_ERROR;
654}
655
656/*===========================================================================
657 * FUNCTION   : findJpegJobByJobId
658 *
659 * DESCRIPTION: find a jpeg job from ongoing Jpeg queue by its job ID
660 *
661 * PARAMETERS :
662 *   @jobId   : job Id of the job
663 *
664 * RETURN     : ptr to a jpeg job struct. NULL if not found.
665 *
666 * NOTE       : Currently only one job is sending to mm-jpeg-interface for jpeg
667 *              encoding. Therefore simply dequeue from the ongoing Jpeg Queue
668 *              will serve the purpose to find the jpeg job.
669 *==========================================================================*/
670qcamera_jpeg_data_t *QCameraPostProcessor::findJpegJobByJobId(uint32_t jobId)
671{
672    qcamera_jpeg_data_t * job = NULL;
673    if (jobId == 0) {
674        ALOGE("%s: not a valid jpeg jobId", __func__);
675        return NULL;
676    }
677
678    // currely only one jpeg job ongoing, so simply dequeue the head
679    job = (qcamera_jpeg_data_t *)m_ongoingJpegQ.dequeue();
680    return job;
681}
682
683/*===========================================================================
684 * FUNCTION   : releasePPInputData
685 *
686 * DESCRIPTION: callback function to release post process input data node
687 *
688 * PARAMETERS :
689 *   @data      : ptr to post process input data
690 *   @user_data : user data ptr (QCameraReprocessor)
691 *
692 * RETURN     : None
693 *==========================================================================*/
694void QCameraPostProcessor::releasePPInputData(void *data, void *user_data)
695{
696    QCameraPostProcessor *pme = (QCameraPostProcessor *)user_data;
697    if (NULL != pme) {
698        pme->releaseSuperBuf((mm_camera_super_buf_t *)data);
699    }
700}
701
702/*===========================================================================
703 * FUNCTION   : releaseJpegData
704 *
705 * DESCRIPTION: callback function to release jpeg job node
706 *
707 * PARAMETERS :
708 *   @data      : ptr to ongoing jpeg job data
709 *   @user_data : user data ptr (QCameraReprocessor)
710 *
711 * RETURN     : None
712 *==========================================================================*/
713void QCameraPostProcessor::releaseJpegData(void *data, void *user_data)
714{
715    QCameraPostProcessor *pme = (QCameraPostProcessor *)user_data;
716    if (NULL != pme) {
717        pme->releaseJpegJobData((qcamera_jpeg_data_t *)data);
718    }
719}
720
721/*===========================================================================
722 * FUNCTION   : releaseOngoingPPData
723 *
724 * DESCRIPTION: callback function to release ongoing postprocess job node
725 *
726 * PARAMETERS :
727 *   @data      : ptr to onging postprocess job
728 *   @user_data : user data ptr (QCameraReprocessor)
729 *
730 * RETURN     : None
731 *==========================================================================*/
732void QCameraPostProcessor::releaseOngoingPPData(void *data, void *user_data)
733{
734    QCameraPostProcessor *pme = (QCameraPostProcessor *)user_data;
735    if (NULL != pme) {
736        qcamera_pp_data_t *pp_job = (qcamera_pp_data_t *)data;
737        if (NULL != pp_job->src_frame) {
738            pme->releaseSuperBuf(pp_job->src_frame);
739            free(pp_job->src_frame);
740            pp_job->src_frame = NULL;
741        }
742    }
743}
744
745/*===========================================================================
746 * FUNCTION   : releaseNotifyData
747 *
748 * DESCRIPTION: function to release internal resources in notify data struct
749 *
750 * PARAMETERS :
751 *   @user_data  : ptr user data
752 *   @cookie     : callback cookie
753 *
754 * RETURN     : None
755 *
756 * NOTE       : deallocate jpeg heap memory if it's not NULL
757 *==========================================================================*/
758void QCameraPostProcessor::releaseNotifyData(void *user_data, void *cookie)
759{
760    qcamera_data_argm_t *app_cb = ( qcamera_data_argm_t * ) user_data;
761    QCameraPostProcessor *postProc = ( QCameraPostProcessor * ) cookie;
762    if ( ( NULL != app_cb ) && ( NULL != postProc ) ) {
763        if (app_cb && NULL != app_cb->release_data.data) {
764            app_cb->release_data.data->release(app_cb->release_data.data);
765            app_cb->release_data.data = NULL;
766        }
767        if (app_cb && NULL != app_cb->release_data.frame) {
768            postProc->releaseSuperBuf(app_cb->release_data.frame);
769            free(app_cb->release_data.frame);
770            app_cb->release_data.frame = NULL;
771        }
772        free(app_cb);
773    }
774}
775
776/*===========================================================================
777 * FUNCTION   : releaseSuperBuf
778 *
779 * DESCRIPTION: function to release a superbuf frame by returning back to kernel
780 *
781 * PARAMETERS :
782 *   @super_buf : ptr to the superbuf frame
783 *
784 * RETURN     : None
785 *==========================================================================*/
786void QCameraPostProcessor::releaseSuperBuf(mm_camera_super_buf_t *super_buf)
787{
788    if (NULL != super_buf) {
789        QCameraChannel *pChannel = m_parent->getChannelByHandle(super_buf->ch_id);
790        if (pChannel != NULL) {
791            pChannel->bufDone(super_buf);
792        }
793    }
794}
795
796/*===========================================================================
797 * FUNCTION   : releaseJpegJobData
798 *
799 * DESCRIPTION: function to release internal resources in jpeg job struct
800 *
801 * PARAMETERS :
802 *   @job     : ptr to jpeg job struct
803 *
804 * RETURN     : None
805 *
806 * NOTE       : original source frame need to be queued back to kernel for
807 *              future use. Output buf of jpeg job need to be released since
808 *              it's allocated for each job. Exif object need to be deleted.
809 *==========================================================================*/
810void QCameraPostProcessor::releaseJpegJobData(qcamera_jpeg_data_t *job)
811{
812    ALOGV("%s: E", __func__);
813    if (NULL != job) {
814        if (NULL != job->src_reproc_frame) {
815            releaseSuperBuf(job->src_reproc_frame);
816            free(job->src_reproc_frame);
817            job->src_reproc_frame = NULL;
818        }
819
820        if (NULL != job->src_frame) {
821            releaseSuperBuf(job->src_frame);
822            free(job->src_frame);
823            job->src_frame = NULL;
824        }
825    }
826    ALOGV("%s: X", __func__);
827}
828
829/*===========================================================================
830 * FUNCTION   : getColorfmtFromImgFmt
831 *
832 * DESCRIPTION: function to return jpeg color format based on its image format
833 *
834 * PARAMETERS :
835 *   @img_fmt : image format
836 *
837 * RETURN     : jpeg color format that can be understandable by omx lib
838 *==========================================================================*/
839mm_jpeg_color_format QCameraPostProcessor::getColorfmtFromImgFmt(cam_format_t img_fmt)
840{
841    switch (img_fmt) {
842    case CAM_FORMAT_YUV_420_NV21:
843        return MM_JPEG_COLOR_FORMAT_YCRCBLP_H2V2;
844    case CAM_FORMAT_YUV_420_NV21_ADRENO:
845        return MM_JPEG_COLOR_FORMAT_YCRCBLP_H2V2;
846    case CAM_FORMAT_YUV_420_NV12:
847        return MM_JPEG_COLOR_FORMAT_YCBCRLP_H2V2;
848    case CAM_FORMAT_YUV_420_YV12:
849        return MM_JPEG_COLOR_FORMAT_YCBCRLP_H2V2;
850    case CAM_FORMAT_YUV_422_NV61:
851        return MM_JPEG_COLOR_FORMAT_YCRCBLP_H2V1;
852    case CAM_FORMAT_YUV_422_NV16:
853        return MM_JPEG_COLOR_FORMAT_YCBCRLP_H2V1;
854    default:
855        return MM_JPEG_COLOR_FORMAT_YCRCBLP_H2V2;
856    }
857}
858
859/*===========================================================================
860 * FUNCTION   : getJpegImgTypeFromImgFmt
861 *
862 * DESCRIPTION: function to return jpeg encode image type based on its image format
863 *
864 * PARAMETERS :
865 *   @img_fmt : image format
866 *
867 * RETURN     : return jpeg source image format (YUV or Bitstream)
868 *==========================================================================*/
869mm_jpeg_format_t QCameraPostProcessor::getJpegImgTypeFromImgFmt(cam_format_t img_fmt)
870{
871    switch (img_fmt) {
872    case CAM_FORMAT_YUV_420_NV21:
873    case CAM_FORMAT_YUV_420_NV21_ADRENO:
874    case CAM_FORMAT_YUV_420_NV12:
875    case CAM_FORMAT_YUV_420_YV12:
876    case CAM_FORMAT_YUV_422_NV61:
877    case CAM_FORMAT_YUV_422_NV16:
878        return MM_JPEG_FMT_YUV;
879    default:
880        return MM_JPEG_FMT_YUV;
881    }
882}
883
884/*===========================================================================
885 * FUNCTION   : encodeData
886 *
887 * DESCRIPTION: function to prepare encoding job information and send to
888 *              mm-jpeg-interface to do the encoding job
889 *
890 * PARAMETERS :
891 *   @jpeg_job_data : ptr to a struct saving job related information
892 *   @needNewSess   : flag to indicate if a new jpeg encoding session need
893 *                    to be created. After creation, this flag will be toggled
894 *
895 * RETURN     : int32_t type of status
896 *              NO_ERROR  -- success
897 *              none-zero failure code
898 *==========================================================================*/
899int32_t QCameraPostProcessor::encodeData(qcamera_jpeg_data_t *jpeg_job_data,
900                                         uint8_t &needNewSess)
901{
902    ALOGV("%s : E", __func__);
903    int32_t ret = NO_ERROR;
904    mm_jpeg_job_t jpg_job;
905    uint32_t jobId = 0;
906    QCameraStream *main_stream = NULL;
907    mm_camera_buf_def_t *main_frame = NULL;
908    QCameraStream *thumb_stream = NULL;
909    mm_camera_buf_def_t *thumb_frame = NULL;
910    mm_camera_super_buf_t *recvd_frame = jpeg_job_data->src_frame;
911
912    // find channel
913    QCameraChannel *pChannel = m_parent->getChannelByHandle(recvd_frame->ch_id);
914    // check reprocess channel if not found
915    if (pChannel == NULL) {
916        if (m_pReprocChannel != NULL &&
917            m_pReprocChannel->getMyHandle() == recvd_frame->ch_id) {
918            pChannel = m_pReprocChannel;
919        }
920    }
921    if (pChannel == NULL) {
922        ALOGE("%s: No corresponding channel (ch_id = %d) exist, return here",
923              __func__, recvd_frame->ch_id);
924        return BAD_VALUE;
925    }
926
927    // find snapshot frame and thumnail frame
928    for (int i = 0; i < recvd_frame->num_bufs; i++) {
929        QCameraStream *pStream =
930            pChannel->getStreamByHandle(recvd_frame->bufs[i]->stream_id);
931        if (pStream != NULL) {
932            if (pStream->isTypeOf(CAM_STREAM_TYPE_SNAPSHOT) ||
933                pStream->isTypeOf(CAM_STREAM_TYPE_OFFLINE_PROC)) {
934                main_stream = pStream;
935                main_frame = recvd_frame->bufs[i];
936            } else if (pStream->isTypeOf(CAM_STREAM_TYPE_PREVIEW) ||
937                       pStream->isTypeOf(CAM_STREAM_TYPE_POSTVIEW)) {
938                thumb_stream = pStream;
939                thumb_frame = recvd_frame->bufs[i];
940            }
941        }
942    }
943
944    if(NULL == main_frame){
945       ALOGE("%s : Main frame is NULL", __func__);
946       return BAD_VALUE;
947    }
948
949    QCameraMemory *memObj = (QCameraMemory *)main_frame->mem_info;
950    if (NULL == memObj) {
951        ALOGE("%s : Memeory Obj of main frame is NULL", __func__);
952        return NO_MEMORY;
953    }
954
955    // clean and invalidate cache ops through mem obj of the frame
956    memObj->cleanInvalidateCache(main_frame->buf_idx);
957
958    // dump snapshot frame if enabled
959    m_parent->dumpFrameToFile(main_frame->buffer, main_frame->frame_len,
960                              main_frame->frame_idx, QCAMERA_DUMP_FRM_SNAPSHOT);
961
962    // send upperlayer callback for raw image
963    camera_memory_t *mem = memObj->getMemory(main_frame->buf_idx, false);
964    if (NULL != m_parent->mDataCb &&
965        m_parent->msgTypeEnabledWithLock(CAMERA_MSG_RAW_IMAGE) > 0) {
966        qcamera_callback_argm_t cbArg;
967        memset(&cbArg, 0, sizeof(qcamera_callback_argm_t));
968        cbArg.cb_type = QCAMERA_DATA_CALLBACK;
969        cbArg.msg_type = CAMERA_MSG_RAW_IMAGE;
970        cbArg.data = mem;
971        cbArg.index = 1;
972        m_parent->m_cbNotifier.notifyCallback(cbArg);
973    }
974    if (NULL != m_parent->mNotifyCb &&
975        m_parent->msgTypeEnabledWithLock(CAMERA_MSG_RAW_IMAGE_NOTIFY) > 0) {
976        qcamera_callback_argm_t cbArg;
977        memset(&cbArg, 0, sizeof(qcamera_callback_argm_t));
978        cbArg.cb_type = QCAMERA_NOTIFY_CALLBACK;
979        cbArg.msg_type = CAMERA_MSG_RAW_IMAGE_NOTIFY;
980        cbArg.ext1 = 0;
981        cbArg.ext2 = 0;
982        m_parent->m_cbNotifier.notifyCallback(cbArg);
983    }
984
985    if (thumb_frame != NULL) {
986        QCameraMemory *thumb_memObj = (QCameraMemory *)thumb_frame->mem_info;
987        if (NULL != thumb_memObj) {
988            // clean and invalidate cache ops through mem obj of the frame
989            thumb_memObj->cleanInvalidateCache(thumb_frame->buf_idx);
990        }
991
992        // dump thumbnail frame if enabled
993        m_parent->dumpFrameToFile(thumb_frame->buffer, thumb_frame->frame_len,
994                                  thumb_frame->frame_idx, QCAMERA_DUMP_FRM_THUMBNAIL);
995    }
996
997    if (mJpegClientHandle <= 0) {
998        ALOGE("%s: Error: bug here, mJpegClientHandle is 0", __func__);
999        return UNKNOWN_ERROR;
1000    }
1001
1002    if (needNewSess) {
1003        // create jpeg encoding session
1004        mm_jpeg_encode_params_t encodeParam;
1005        memset(&encodeParam, 0, sizeof(mm_jpeg_encode_params_t));
1006        getJpegEncodingConfig(encodeParam, main_stream, thumb_stream);
1007        ret = mJpegHandle.create_session(mJpegClientHandle, &encodeParam, &mJpegSessionId);
1008        if (ret != NO_ERROR) {
1009            ALOGE("%s: error creating a new jpeg encoding session", __func__);
1010            return ret;
1011        }
1012        needNewSess = FALSE;
1013    }
1014
1015    // Fill in new job
1016    memset(&jpg_job, 0, sizeof(mm_jpeg_job_t));
1017    jpg_job.job_type = JPEG_JOB_TYPE_ENCODE;
1018    jpg_job.encode_job.session_id = mJpegSessionId;
1019    jpg_job.encode_job.src_index = main_frame->buf_idx;
1020    jpg_job.encode_job.dst_index = 0;
1021
1022    cam_rect_t crop;
1023    memset(&crop, 0, sizeof(cam_rect_t));
1024    main_stream->getCropInfo(crop);
1025
1026    cam_dimension_t src_dim;
1027    memset(&src_dim, 0, sizeof(cam_dimension_t));
1028    main_stream->getFrameDimension(src_dim);
1029
1030    // main dim
1031    jpg_job.encode_job.main_dim.src_dim = src_dim;
1032    jpg_job.encode_job.main_dim.dst_dim = src_dim;
1033    jpg_job.encode_job.main_dim.crop = crop;
1034
1035    // thumbnail dim
1036    if (m_bThumbnailNeeded == TRUE) {
1037        if (thumb_stream == NULL) {
1038            // need jpeg thumbnail, but no postview/preview stream exists
1039            // we use the main stream/frame to encode thumbnail
1040            thumb_stream = main_stream;
1041            thumb_frame = main_frame;
1042        }
1043        memset(&crop, 0, sizeof(cam_rect_t));
1044        thumb_stream->getCropInfo(crop);
1045        memset(&src_dim, 0, sizeof(cam_dimension_t));
1046        thumb_stream->getFrameDimension(src_dim);
1047        jpg_job.encode_job.thumb_dim.src_dim = src_dim;
1048        m_parent->getThumbnailSize(jpg_job.encode_job.thumb_dim.dst_dim);
1049        jpg_job.encode_job.thumb_dim.crop = crop;
1050        jpg_job.encode_job.thumb_index = thumb_frame->buf_idx;
1051    }
1052
1053    // set rotation only when no online rotation or offline pp rotation is done before
1054    if (!m_parent->needRotationReprocess() &&
1055        !m_parent->needOnlineRotation()) {
1056        jpg_job.encode_job.rotation = m_parent->getJpegRotation();
1057    }
1058    ALOGV("%s: jpeg rotation is set to %d", __func__, jpg_job.encode_job.rotation);
1059
1060    // find meta data frame
1061    mm_camera_buf_def_t *meta_frame = NULL;
1062    for (int i = 0; i < jpeg_job_data->src_frame->num_bufs; i++) {
1063        // look through input superbuf
1064        if (jpeg_job_data->src_frame->bufs[i]->stream_type == CAM_STREAM_TYPE_METADATA) {
1065            meta_frame = jpeg_job_data->src_frame->bufs[i];
1066            break;
1067        }
1068    }
1069    if (meta_frame == NULL && jpeg_job_data->src_reproc_frame != NULL) {
1070        // look through reprocess source superbuf
1071        for (int i = 0; i < jpeg_job_data->src_reproc_frame->num_bufs; i++) {
1072            if (jpeg_job_data->src_reproc_frame->bufs[i]->stream_type == CAM_STREAM_TYPE_METADATA) {
1073                meta_frame = jpeg_job_data->src_reproc_frame->bufs[i];
1074                break;
1075            }
1076        }
1077    }
1078    if (meta_frame != NULL) {
1079        // fill in meta data frame ptr
1080        jpg_job.encode_job.p_metadata = (cam_metadata_info_t *)meta_frame->buffer;
1081    }
1082
1083    ret = mJpegHandle.start_job(&jpg_job, &jobId);
1084    if (ret == NO_ERROR) {
1085        // remember job info
1086        jpeg_job_data->jobId = jobId;
1087    }
1088
1089    ALOGD("%s : X", __func__);
1090    return ret;
1091}
1092
1093/*===========================================================================
1094 * FUNCTION   : processRawImageImpl
1095 *
1096 * DESCRIPTION: function to send raw image to upper layer
1097 *
1098 * PARAMETERS :
1099 *   @recvd_frame   : frame to be encoded
1100 *
1101 * RETURN     : int32_t type of status
1102 *              NO_ERROR  -- success
1103 *              none-zero failure code
1104 *==========================================================================*/
1105int32_t QCameraPostProcessor::processRawImageImpl(mm_camera_super_buf_t *recvd_frame)
1106{
1107    int32_t rc = NO_ERROR;
1108
1109    mm_camera_buf_def_t *frame = recvd_frame->bufs[0];
1110    QCameraMemory *rawMemObj = (QCameraMemory *)frame->mem_info;
1111    camera_memory_t *raw_mem = NULL;
1112
1113    if (rawMemObj != NULL) {
1114        raw_mem = rawMemObj->getMemory(frame->buf_idx, false);
1115    }
1116
1117    if (NULL != rawMemObj && NULL != raw_mem) {
1118        // send data callback for COMPRESSED_IMAGE
1119        rawMemObj->cleanCache(frame->buf_idx);
1120
1121        // dump frame into file
1122        m_parent->dumpFrameToFile(frame->buffer, frame->frame_len,
1123                                  frame->frame_idx, QCAMERA_DUMP_FRM_RAW);
1124
1125        // send data callback / notify for RAW_IMAGE
1126        if (NULL != m_parent->mDataCb &&
1127            m_parent->msgTypeEnabledWithLock(CAMERA_MSG_RAW_IMAGE) > 0) {
1128            qcamera_callback_argm_t cbArg;
1129            memset(&cbArg, 0, sizeof(qcamera_callback_argm_t));
1130            cbArg.cb_type = QCAMERA_DATA_CALLBACK;
1131            cbArg.msg_type = CAMERA_MSG_RAW_IMAGE;
1132            cbArg.data = raw_mem;
1133            cbArg.index = 0;
1134            m_parent->m_cbNotifier.notifyCallback(cbArg);
1135        }
1136        if (NULL != m_parent->mNotifyCb &&
1137            m_parent->msgTypeEnabledWithLock(CAMERA_MSG_RAW_IMAGE_NOTIFY) > 0) {
1138            qcamera_callback_argm_t cbArg;
1139            memset(&cbArg, 0, sizeof(qcamera_callback_argm_t));
1140            cbArg.cb_type = QCAMERA_NOTIFY_CALLBACK;
1141            cbArg.msg_type = CAMERA_MSG_RAW_IMAGE_NOTIFY;
1142            cbArg.ext1 = 0;
1143            cbArg.ext2 = 0;
1144            m_parent->m_cbNotifier.notifyCallback(cbArg);
1145        }
1146
1147        if ((m_parent->mDataCb != NULL) &&
1148            m_parent->msgTypeEnabledWithLock(CAMERA_MSG_COMPRESSED_IMAGE) > 0) {
1149            qcamera_release_data_t release_data;
1150            memset(&release_data, 0, sizeof(qcamera_release_data_t));
1151            release_data.frame = recvd_frame;
1152            sendDataNotify(CAMERA_MSG_COMPRESSED_IMAGE,
1153                           raw_mem,
1154                           0,
1155                           NULL,
1156                           &release_data);
1157        }
1158    } else {
1159        ALOGE("%s: Cannot get raw mem", __func__);
1160        rc = UNKNOWN_ERROR;
1161    }
1162
1163    return rc;
1164}
1165
1166/*===========================================================================
1167 * FUNCTION   : dataProcessRoutine
1168 *
1169 * DESCRIPTION: data process routine that handles input data either from input
1170 *              Jpeg Queue to do jpeg encoding, or from input PP Queue to do
1171 *              reprocess.
1172 *
1173 * PARAMETERS :
1174 *   @data    : user data ptr (QCameraPostProcessor)
1175 *
1176 * RETURN     : None
1177 *==========================================================================*/
1178void *QCameraPostProcessor::dataProcessRoutine(void *data)
1179{
1180    int running = 1;
1181    int ret;
1182    uint8_t is_active = FALSE;
1183    uint8_t needNewSess = TRUE;
1184    QCameraPostProcessor *pme = (QCameraPostProcessor *)data;
1185    QCameraCmdThread *cmdThread = &pme->m_dataProcTh;
1186
1187    ALOGD("%s: E", __func__);
1188    do {
1189        do {
1190            ret = cam_sem_wait(&cmdThread->cmd_sem);
1191            if (ret != 0 && errno != EINVAL) {
1192                ALOGE("%s: cam_sem_wait error (%s)",
1193                           __func__, strerror(errno));
1194                return NULL;
1195            }
1196        } while (ret != 0);
1197
1198        // we got notified about new cmd avail in cmd queue
1199        camera_cmd_type_t cmd = cmdThread->getCmd();
1200        switch (cmd) {
1201        case CAMERA_CMD_TYPE_START_DATA_PROC:
1202            ALOGD("%s: start data proc", __func__);
1203            is_active = TRUE;
1204            needNewSess = TRUE;
1205            break;
1206        case CAMERA_CMD_TYPE_STOP_DATA_PROC:
1207            {
1208                ALOGD("%s: stop data proc", __func__);
1209                is_active = FALSE;
1210
1211                // cancel all ongoing jpeg jobs
1212                qcamera_jpeg_data_t *jpeg_job =
1213                    (qcamera_jpeg_data_t *)pme->m_ongoingJpegQ.dequeue();
1214                while (jpeg_job != NULL) {
1215                    pme->mJpegHandle.abort_job(jpeg_job->jobId);
1216
1217                    pme->releaseJpegJobData(jpeg_job);
1218                    free(jpeg_job);
1219
1220                    jpeg_job = (qcamera_jpeg_data_t *)pme->m_ongoingJpegQ.dequeue();
1221                }
1222
1223                // destroy jpeg encoding session
1224                if ( 0 < pme->mJpegSessionId ) {
1225                    pme->mJpegHandle.destroy_session(pme->mJpegSessionId);
1226                    pme->mJpegSessionId = 0;
1227                }
1228
1229                // free jpeg out buf and exif obj
1230                if (pme->m_pJpegOutputMem != NULL) {
1231                    pme->m_pJpegOutputMem->deallocate();
1232                    delete pme->m_pJpegOutputMem;
1233                    pme->m_pJpegOutputMem = NULL;
1234                }
1235                if (pme->m_pJpegExifObj != NULL) {
1236                    delete pme->m_pJpegExifObj;
1237                    pme->m_pJpegExifObj = NULL;
1238                }
1239                needNewSess = TRUE;
1240
1241                // stop reproc channel if exists
1242                if (pme->m_pReprocChannel != NULL) {
1243                    pme->m_pReprocChannel->stop();
1244                    delete pme->m_pReprocChannel;
1245                    pme->m_pReprocChannel = NULL;
1246                }
1247
1248                // flush ongoing postproc Queue
1249                pme->m_ongoingPPQ.flush();
1250
1251                // flush input jpeg Queue
1252                pme->m_inputJpegQ.flush();
1253
1254                // flush input Postproc Queue
1255                pme->m_inputPPQ.flush();
1256
1257                // flush input raw Queue
1258                pme->m_inputRawQ.flush();
1259
1260                // signal cmd is completed
1261                cam_sem_post(&cmdThread->sync_sem);
1262            }
1263            break;
1264        case CAMERA_CMD_TYPE_DO_NEXT_JOB:
1265            {
1266                ALOGD("%s: Do next job, active is %d", __func__, is_active);
1267                if (is_active == TRUE) {
1268                    // check if there is any ongoing jpeg jobs
1269                    if (pme->m_ongoingJpegQ.isEmpty()) {
1270                        // no ongoing jpeg job, we are fine to send jpeg encoding job
1271                        qcamera_jpeg_data_t *jpeg_job =
1272                            (qcamera_jpeg_data_t *)pme->m_inputJpegQ.dequeue();
1273
1274                        if (NULL != jpeg_job) {
1275                            //play shutter sound
1276                            pme->m_parent->playShutter();
1277
1278                            // add into ongoing jpeg job Q
1279                            pme->m_ongoingJpegQ.enqueue((void *)jpeg_job);
1280                            ret = pme->encodeData(jpeg_job, needNewSess);
1281                            if (NO_ERROR != ret) {
1282                                // dequeue the last one
1283                                pme->m_ongoingJpegQ.dequeue(false);
1284
1285                                pme->releaseJpegJobData(jpeg_job);
1286                                free(jpeg_job);
1287                                pme->sendDataNotify(CAMERA_MSG_COMPRESSED_IMAGE,
1288                                                    NULL,
1289                                                    0,
1290                                                    NULL,
1291                                                    NULL);
1292                            }
1293                        }
1294                    }
1295
1296                    // process raw data if any
1297                    mm_camera_super_buf_t *super_buf =
1298                        (mm_camera_super_buf_t *)pme->m_inputRawQ.dequeue();
1299
1300                    if (NULL != super_buf) {
1301                        //play shutter sound
1302                        pme->m_parent->playShutter();
1303                        ret = pme->processRawImageImpl(super_buf);
1304                        if (NO_ERROR != ret) {
1305                            pme->releaseSuperBuf(super_buf);
1306                            free(super_buf);
1307                            pme->sendDataNotify(CAMERA_MSG_COMPRESSED_IMAGE,
1308                                                NULL,
1309                                                0,
1310                                                NULL,
1311                                                NULL);
1312                        }
1313                    }
1314
1315                    mm_camera_super_buf_t *pp_frame =
1316                        (mm_camera_super_buf_t *)pme->m_inputPPQ.dequeue();
1317                    if (NULL != pp_frame) {
1318                        qcamera_pp_data_t *pp_job =
1319                            (qcamera_pp_data_t *)malloc(sizeof(qcamera_pp_data_t));
1320                        if (pp_job != NULL) {
1321                            memset(pp_job, 0, sizeof(qcamera_pp_data_t));
1322                            if (pme->m_pReprocChannel != NULL) {
1323                                // add into ongoing PP job Q
1324                                pp_job->src_frame = pp_frame;
1325                                pme->m_ongoingPPQ.enqueue((void *)pp_job);
1326                                ret = pme->m_pReprocChannel->doReprocess(pp_frame);
1327                                if (NO_ERROR != ret) {
1328                                    // remove from ongoing PP job Q
1329                                    pme->m_ongoingPPQ.dequeue(false);
1330                                }
1331                            } else {
1332                                ALOGE("%s: Reprocess channel is NULL", __func__);
1333                                ret = -1;
1334                            }
1335                        } else {
1336                            ALOGE("%s: no mem for qcamera_pp_data_t", __func__);
1337                            ret = -1;
1338                        }
1339
1340                        if (0 != ret) {
1341                            // free pp_job
1342                            if (pp_job != NULL) {
1343                                free(pp_job);
1344                            }
1345                            // free frame
1346                            if (pp_frame != NULL) {
1347                                pme->releaseSuperBuf(pp_frame);
1348                                free(pp_frame);
1349                            }
1350                            // send error notify
1351                            pme->sendDataNotify(CAMERA_MSG_COMPRESSED_IMAGE,
1352                                                NULL,
1353                                                0,
1354                                                NULL,
1355                                                NULL);
1356                        }
1357                    }
1358                } else {
1359                    // not active, simply return buf and do no op
1360                    mm_camera_super_buf_t *super_buf =
1361                        (mm_camera_super_buf_t *)pme->m_inputJpegQ.dequeue();
1362                    if (NULL != super_buf) {
1363                        pme->releaseSuperBuf(super_buf);
1364                        free(super_buf);
1365                    }
1366                    super_buf = (mm_camera_super_buf_t *)pme->m_inputRawQ.dequeue();
1367                    if (NULL != super_buf) {
1368                        pme->releaseSuperBuf(super_buf);
1369                        free(super_buf);
1370                    }
1371                    super_buf = (mm_camera_super_buf_t *)pme->m_inputPPQ.dequeue();
1372                    if (NULL != super_buf) {
1373                        pme->releaseSuperBuf(super_buf);
1374                        free(super_buf);
1375                    }
1376                }
1377            }
1378            break;
1379        case CAMERA_CMD_TYPE_EXIT:
1380            running = 0;
1381            break;
1382        default:
1383            break;
1384        }
1385    } while (running);
1386    ALOGD("%s: X", __func__);
1387    return NULL;
1388}
1389
1390/*===========================================================================
1391 * FUNCTION   : getJpegPaddingReq
1392 *
1393 * DESCRIPTION: function to add an entry to exif data
1394 *
1395 * PARAMETERS :
1396 *   @padding_info : jpeg specific padding requirement
1397 *
1398 * RETURN     : int32_t type of status
1399 *              NO_ERROR  -- success
1400 *              none-zero failure code
1401 *==========================================================================*/
1402int32_t QCameraPostProcessor::getJpegPaddingReq(cam_padding_info_t &padding_info)
1403{
1404    // TODO: hardcode for now, needs to query from mm-jpeg-interface
1405    padding_info.width_padding  = CAM_PAD_NONE;
1406    padding_info.height_padding  = CAM_PAD_TO_16;
1407    padding_info.plane_padding  = CAM_PAD_TO_WORD;
1408    return NO_ERROR;
1409}
1410
1411/*===========================================================================
1412 * FUNCTION   : QCameraExif
1413 *
1414 * DESCRIPTION: constructor of QCameraExif
1415 *
1416 * PARAMETERS : None
1417 *
1418 * RETURN     : None
1419 *==========================================================================*/
1420QCameraExif::QCameraExif()
1421    : m_nNumEntries(0)
1422{
1423    memset(m_Entries, 0, sizeof(m_Entries));
1424}
1425
1426/*===========================================================================
1427 * FUNCTION   : ~QCameraExif
1428 *
1429 * DESCRIPTION: deconstructor of QCameraExif. Will release internal memory ptr.
1430 *
1431 * PARAMETERS : None
1432 *
1433 * RETURN     : None
1434 *==========================================================================*/
1435QCameraExif::~QCameraExif()
1436{
1437    for (uint32_t i = 0; i < m_nNumEntries; i++) {
1438        switch (m_Entries[i].tag_entry.type) {
1439        case EXIF_BYTE:
1440            {
1441                if (m_Entries[i].tag_entry.count > 1 &&
1442                    m_Entries[i].tag_entry.data._bytes != NULL) {
1443                    free(m_Entries[i].tag_entry.data._bytes);
1444                    m_Entries[i].tag_entry.data._bytes = NULL;
1445                }
1446            }
1447            break;
1448        case EXIF_ASCII:
1449            {
1450                if (m_Entries[i].tag_entry.data._ascii != NULL) {
1451                    free(m_Entries[i].tag_entry.data._ascii);
1452                    m_Entries[i].tag_entry.data._ascii = NULL;
1453                }
1454            }
1455            break;
1456        case EXIF_SHORT:
1457            {
1458                if (m_Entries[i].tag_entry.count > 1 &&
1459                    m_Entries[i].tag_entry.data._shorts != NULL) {
1460                    free(m_Entries[i].tag_entry.data._shorts);
1461                    m_Entries[i].tag_entry.data._shorts = NULL;
1462                }
1463            }
1464            break;
1465        case EXIF_LONG:
1466            {
1467                if (m_Entries[i].tag_entry.count > 1 &&
1468                    m_Entries[i].tag_entry.data._longs != NULL) {
1469                    free(m_Entries[i].tag_entry.data._longs);
1470                    m_Entries[i].tag_entry.data._longs = NULL;
1471                }
1472            }
1473            break;
1474        case EXIF_RATIONAL:
1475            {
1476                if (m_Entries[i].tag_entry.count > 1 &&
1477                    m_Entries[i].tag_entry.data._rats != NULL) {
1478                    free(m_Entries[i].tag_entry.data._rats);
1479                    m_Entries[i].tag_entry.data._rats = NULL;
1480                }
1481            }
1482            break;
1483        case EXIF_UNDEFINED:
1484            {
1485                if (m_Entries[i].tag_entry.data._undefined != NULL) {
1486                    free(m_Entries[i].tag_entry.data._undefined);
1487                    m_Entries[i].tag_entry.data._undefined = NULL;
1488                }
1489            }
1490            break;
1491        case EXIF_SLONG:
1492            {
1493                if (m_Entries[i].tag_entry.count > 1 &&
1494                    m_Entries[i].tag_entry.data._slongs != NULL) {
1495                    free(m_Entries[i].tag_entry.data._slongs);
1496                    m_Entries[i].tag_entry.data._slongs = NULL;
1497                }
1498            }
1499            break;
1500        case EXIF_SRATIONAL:
1501            {
1502                if (m_Entries[i].tag_entry.count > 1 &&
1503                    m_Entries[i].tag_entry.data._srats != NULL) {
1504                    free(m_Entries[i].tag_entry.data._srats);
1505                    m_Entries[i].tag_entry.data._srats = NULL;
1506                }
1507            }
1508            break;
1509        }
1510    }
1511}
1512
1513/*===========================================================================
1514 * FUNCTION   : addEntry
1515 *
1516 * DESCRIPTION: function to add an entry to exif data
1517 *
1518 * PARAMETERS :
1519 *   @tagid   : exif tag ID
1520 *   @type    : data type
1521 *   @count   : number of data in uint of its type
1522 *   @data    : input data ptr
1523 *
1524 * RETURN     : int32_t type of status
1525 *              NO_ERROR  -- success
1526 *              none-zero failure code
1527 *==========================================================================*/
1528int32_t QCameraExif::addEntry(exif_tag_id_t tagid,
1529                              exif_tag_type_t type,
1530                              uint32_t count,
1531                              void *data)
1532{
1533    int32_t rc = NO_ERROR;
1534    if(m_nNumEntries >= MAX_EXIF_TABLE_ENTRIES) {
1535        ALOGE("%s: Number of entries exceeded limit", __func__);
1536        return NO_MEMORY;
1537    }
1538
1539    m_Entries[m_nNumEntries].tag_id = tagid;
1540    m_Entries[m_nNumEntries].tag_entry.type = type;
1541    m_Entries[m_nNumEntries].tag_entry.count = count;
1542    m_Entries[m_nNumEntries].tag_entry.copy = 1;
1543    switch (type) {
1544    case EXIF_BYTE:
1545        {
1546            if (count > 1) {
1547                uint8_t *values = (uint8_t *)malloc(count);
1548                if (values == NULL) {
1549                    ALOGE("%s: No memory for byte array", __func__);
1550                    rc = NO_MEMORY;
1551                } else {
1552                    memcpy(values, data, count);
1553                    m_Entries[m_nNumEntries].tag_entry.data._bytes = values;
1554                }
1555            } else {
1556                m_Entries[m_nNumEntries].tag_entry.data._byte = *(uint8_t *)data;
1557            }
1558        }
1559        break;
1560    case EXIF_ASCII:
1561        {
1562            char *str = NULL;
1563            str = (char *)malloc(count + 1);
1564            if (str == NULL) {
1565                ALOGE("%s: No memory for ascii string", __func__);
1566                rc = NO_MEMORY;
1567            } else {
1568                memset(str, 0, count + 1);
1569                memcpy(str, data, count);
1570                m_Entries[m_nNumEntries].tag_entry.data._ascii = str;
1571            }
1572        }
1573        break;
1574    case EXIF_SHORT:
1575        {
1576            if (count > 1) {
1577                uint16_t *values = (uint16_t *)malloc(count * sizeof(uint16_t));
1578                if (values == NULL) {
1579                    ALOGE("%s: No memory for short array", __func__);
1580                    rc = NO_MEMORY;
1581                } else {
1582                    memcpy(values, data, count * sizeof(uint16_t));
1583                    m_Entries[m_nNumEntries].tag_entry.data._shorts = values;
1584                }
1585            } else {
1586                m_Entries[m_nNumEntries].tag_entry.data._short = *(uint16_t *)data;
1587            }
1588        }
1589        break;
1590    case EXIF_LONG:
1591        {
1592            if (count > 1) {
1593                uint32_t *values = (uint32_t *)malloc(count * sizeof(uint32_t));
1594                if (values == NULL) {
1595                    ALOGE("%s: No memory for long array", __func__);
1596                    rc = NO_MEMORY;
1597                } else {
1598                    memcpy(values, data, count * sizeof(uint32_t));
1599                    m_Entries[m_nNumEntries].tag_entry.data._longs = values;
1600                }
1601            } else {
1602                m_Entries[m_nNumEntries].tag_entry.data._long = *(uint32_t *)data;
1603            }
1604        }
1605        break;
1606    case EXIF_RATIONAL:
1607        {
1608            if (count > 1) {
1609                rat_t *values = (rat_t *)malloc(count * sizeof(rat_t));
1610                if (values == NULL) {
1611                    ALOGE("%s: No memory for rational array", __func__);
1612                    rc = NO_MEMORY;
1613                } else {
1614                    memcpy(values, data, count * sizeof(rat_t));
1615                    m_Entries[m_nNumEntries].tag_entry.data._rats = values;
1616                }
1617            } else {
1618                m_Entries[m_nNumEntries].tag_entry.data._rat = *(rat_t *)data;
1619            }
1620        }
1621        break;
1622    case EXIF_UNDEFINED:
1623        {
1624            uint8_t *values = (uint8_t *)malloc(count);
1625            if (values == NULL) {
1626                ALOGE("%s: No memory for undefined array", __func__);
1627                rc = NO_MEMORY;
1628            } else {
1629                memcpy(values, data, count);
1630                m_Entries[m_nNumEntries].tag_entry.data._undefined = values;
1631            }
1632        }
1633        break;
1634    case EXIF_SLONG:
1635        {
1636            if (count > 1) {
1637                int32_t *values = (int32_t *)malloc(count * sizeof(int32_t));
1638                if (values == NULL) {
1639                    ALOGE("%s: No memory for signed long array", __func__);
1640                    rc = NO_MEMORY;
1641                } else {
1642                    memcpy(values, data, count * sizeof(int32_t));
1643                    m_Entries[m_nNumEntries].tag_entry.data._slongs = values;
1644                }
1645            } else {
1646                m_Entries[m_nNumEntries].tag_entry.data._slong = *(int32_t *)data;
1647            }
1648        }
1649        break;
1650    case EXIF_SRATIONAL:
1651        {
1652            if (count > 1) {
1653                srat_t *values = (srat_t *)malloc(count * sizeof(srat_t));
1654                if (values == NULL) {
1655                    ALOGE("%s: No memory for signed rational array", __func__);
1656                    rc = NO_MEMORY;
1657                } else {
1658                    memcpy(values, data, count * sizeof(srat_t));
1659                    m_Entries[m_nNumEntries].tag_entry.data._srats = values;
1660                }
1661            } else {
1662                m_Entries[m_nNumEntries].tag_entry.data._srat = *(srat_t *)data;
1663            }
1664        }
1665        break;
1666    }
1667
1668    // Increase number of entries
1669    m_nNumEntries++;
1670    return rc;
1671}
1672
1673}; // namespace qcamera
1674