forked from shantur/camera-hal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QCameraHWI_Still.cpp
executable file
·2545 lines (2232 loc) · 85.3 KB
/
QCameraHWI_Still.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** Copyright (c) 2011-2012 The Linux Foundation. All rights reserved.
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
/*#error uncomment this for compiler test!*/
#define ALOG_NDEBUG 0
#define ALOG_NDDEBUG 0
#define ALOG_NIDEBUG 0
#define ALOG_TAG "QCameraHWI_Still"
#include <utils/Log.h>
#include <utils/threads.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <media/mediarecorder.h>
#include <math.h>
#include "QCameraHAL.h"
#include "QCameraHWI.h"
/* following code implement the still image capture & encoding logic of this class*/
namespace android {
typedef enum {
SNAPSHOT_STATE_ERROR,
SNAPSHOT_STATE_UNINIT,
SNAPSHOT_STATE_CH_ACQUIRED,
SNAPSHOT_STATE_BUF_NOTIF_REGD,
SNAPSHOT_STATE_BUF_INITIALIZED,
SNAPSHOT_STATE_INITIALIZED,
SNAPSHOT_STATE_IMAGE_CAPTURE_STRTD,
SNAPSHOT_STATE_YUV_RECVD,
SNAPSHOT_STATE_JPEG_ENCODING,
SNAPSHOT_STATE_JPEG_ENCODE_DONE,
SNAPSHOT_STATE_JPEG_COMPLETE_ENCODE_DONE,
/*Add any new state above*/
SNAPSHOT_STATE_MAX
} snapshot_state_type_t;
//-----------------------------------------------------------------------
// Constants
//----------------------------------------------------------------------
static const int PICTURE_FORMAT_JPEG = 1;
static const int PICTURE_FORMAT_RAW = 2;
static const int POSTVIEW_SMALL_HEIGHT = 144;
#define MEDIA_RECORDER_MSG_COMPRESSED_IMAGE 8
// ---------------------------------------------------------------------------
/* static functions*/
// ---------------------------------------------------------------------------
/* TBD: Temp: to be removed*/
static pthread_mutex_t g_s_mutex;
static int g_status = 0;
static pthread_cond_t g_s_cond_v;
static void mm_app_snapshot_done()
{
pthread_mutex_lock(&g_s_mutex);
g_status = TRUE;
pthread_cond_signal(&g_s_cond_v);
pthread_mutex_unlock(&g_s_mutex);
}
static void mm_app_snapshot_wait()
{
pthread_mutex_lock(&g_s_mutex);
if(FALSE == g_status) pthread_cond_wait(&g_s_cond_v, &g_s_mutex);
pthread_mutex_unlock(&g_s_mutex);
g_status = FALSE;
}
static int mm_app_dump_snapshot_frame(char *filename,
const void *buffer,
uint32_t len)
{
char bufp[128];
int file_fdp;
int rc = 0;
file_fdp = open(filename, O_RDWR | O_CREAT, 0777);
if (file_fdp < 0) {
rc = -1;
goto end;
}
write(file_fdp,
(const void *)buffer, len);
close(file_fdp);
end:
return rc;
}
/* Callback received when a frame is available after snapshot*/
static void snapshot_notify_cb(mm_camera_ch_data_buf_t *recvd_frame,
void *user_data)
{
QCameraStream_Snapshot *pme = (QCameraStream_Snapshot *)user_data;
ALOGD("%s: E", __func__);
if (pme != NULL) {
pme->receiveRawPicture(recvd_frame);
}
else{
ALOGW("%s: Snapshot obj NULL in callback", __func__);
}
ALOGD("%s: X", __func__);
}
/* Once we give frame for encoding, we get encoded jpeg image
fragments by fragment. We'll need to store them in a buffer
to form complete JPEG image */
static void snapshot_jpeg_fragment_cb(uint8_t *ptr,
uint32_t size,
void *user_data)
{
QCameraStream_Snapshot *pme = (QCameraStream_Snapshot *)user_data;
ALOGE("%s: E",__func__);
if (pme != NULL) {
pme->receiveJpegFragment(ptr,size);
}
else
ALOGW("%s: Receive jpeg fragment cb obj Null", __func__);
ALOGD("%s: X",__func__);
}
/* This callback is received once the complete JPEG encoding is done */
static void snapshot_jpeg_cb(jpeg_event_t event, void *user_data)
{
QCameraStream_Snapshot *pme = (QCameraStream_Snapshot *)user_data;
ALOGV("%s: E ",__func__);
switch(event) {
case JPEG_EVENT_DONE:
break;
case JPEG_EVENT_THUMBNAIL_DROPPED:
ALOGE("%s: Error in thumbnail encoding (event: %d) : X !!!",
__func__, event);
return;
case JPEG_EVENT_ERROR:
case JPEG_EVENT_ABORTED:
if (NULL != pme) {
pme->jpegErrorHandler(event);
if (!(pme->isZSLMode())) {
pme->stop();
}
}
ALOGE("Error event handled from JPEG \n");
return;
default:
ALOGE("Unsupported JPEG event %d \n", event);
break;
}
if (pme != NULL) {
pme->receiveCompleteJpegPicture(event);
ALOGE(" Completed issuing JPEG callback");
/* deinit only if we are done taking requested number of snapshots */
if (pme->getSnapshotState() == SNAPSHOT_STATE_JPEG_COMPLETE_ENCODE_DONE) {
ALOGE(" About to issue deinit callback");
/* If it's ZSL Mode, we don't deinit now. We'll stop the polling thread and
deinit the channel/buffers only when we change the mode from zsl to
non-zsl. */
if (!(pme->isZSLMode())) {
pme->stop();
}
}
}
else
ALOGW("%s: Receive jpeg cb Obj Null", __func__);
ALOGD("%s: X",__func__);
}
// ---------------------------------------------------------------------------
/* private functions*/
// ---------------------------------------------------------------------------
void QCameraStream_Snapshot::
receiveJpegFragment(uint8_t *ptr, uint32_t size)
{
ALOGE("%s: E", __func__);
#if 0
if (mJpegHeap != NULL) {
ALOGE("%s: Copy jpeg...", __func__);
memcpy((uint8_t *)mJpegHeap->mHeap->base()+ mJpegOffset, ptr, size);
mJpegOffset += size;
}
else {
ALOGE("%s: mJpegHeap is NULL!", __func__);
}
#else
if(mHalCamCtrl->mJpegMemory.camera_memory[0] != NULL && ptr != NULL && size > 0) {
memcpy((uint8_t *)((uint32_t)mHalCamCtrl->mJpegMemory.camera_memory[0]->data + mJpegOffset), ptr, size);
mJpegOffset += size;
/*
memcpy((uint8_t *)((uint32_t)mHalCamCtrl->mJpegMemory.camera_memory[0]->data + mJpegOffset), ptr, size);
mJpegOffset += size;
*/
} else {
ALOGE("%s: mJpegHeap is NULL!", __func__);
}
#endif
ALOGD("%s: X", __func__);
}
void QCameraStream_Snapshot::
jpegErrorHandler(jpeg_event_t event)
{
ALOGV("%s: E", __func__);
mStopCallbackLock.lock( );
if(mCurrentFrameEncoded) {
free(mCurrentFrameEncoded);
mCurrentFrameEncoded = NULL;
}
setSnapshotState(SNAPSHOT_STATE_ERROR);
if (!mSnapshotQueue.isEmpty()) {
ALOGI("%s: JPEG Queue not empty. flush the queue in "
"error case.", __func__);
mSnapshotQueue.flush();
}
mStopCallbackLock.unlock( );
if (NULL != mHalCamCtrl->mDataCb)
mHalCamCtrl->mDataCb (CAMERA_MSG_COMPRESSED_IMAGE,
NULL, 0, NULL,
mHalCamCtrl->mCallbackCookie);
ALOGV("%s: X", __func__);
}
void QCameraStream_Snapshot::
receiveCompleteJpegPicture(jpeg_event_t event)
{
int msg_type = CAMERA_MSG_COMPRESSED_IMAGE;
ALOGE("%s: E", __func__);
camera_memory_t *encodedMem = NULL;
camera_data_callback jpg_data_cb = NULL;
bool fail_cb_flag = false;
if(!mActive && !isLiveSnapshot()) {
ALOGE("%s : Cancel Picture",__func__);
fail_cb_flag = true;
goto end;
}
mStopCallbackLock.lock( );
if(mCurrentFrameEncoded!=NULL /*&& !isLiveSnapshot()*/){
ALOGV("<DEBUG>: Calling buf done for snapshot buffer");
cam_evt_buf_done(mCameraId, mCurrentFrameEncoded);
}
mHalCamCtrl->dumpFrameToFile(mHalCamCtrl->mJpegMemory.camera_memory[0]->data, mJpegOffset, (char *)"debug", (char *)"jpg", 0);
end:
msg_type = CAMERA_MSG_COMPRESSED_IMAGE;
if (mHalCamCtrl->mDataCb && (mHalCamCtrl->mMsgEnabled & msg_type)) {
jpg_data_cb = mHalCamCtrl->mDataCb;
}else{
ALOGE("%s: JPEG callback was cancelled--not delivering image.", __func__);
}
setSnapshotState(SNAPSHOT_STATE_JPEG_ENCODE_DONE);
mNumOfRecievedJPEG++;
mHalCamCtrl->deinitExifData();
/* free the resource we allocated to maintain the structure */
//mm_camera_do_munmap(main_fd, (void *)main_buffer_addr, mSnapshotStreamBuf.frame_len);
if(mCurrentFrameEncoded) {
free(mCurrentFrameEncoded);
mCurrentFrameEncoded = NULL;
}
/* Before leaving check the jpeg queue. If it's not empty give the available
frame for encoding*/
if (!mSnapshotQueue.isEmpty()) {
ALOGI("%s: JPEG Queue not empty. Dequeue and encode.", __func__);
mm_camera_ch_data_buf_t* buf =
(mm_camera_ch_data_buf_t *)mSnapshotQueue.dequeue();
//encodeDisplayAndSave(buf, 1);
if ( NO_ERROR != encodeDisplayAndSave(buf, 1)){
fail_cb_flag = true;
}
} else if (mNumOfRecievedJPEG == mNumOfSnapshot) {
ALOGD("%s: Before omxJpegFinish", __func__);
omxJpegFinish();
ALOGD("%s: After omxJpegFinish", __func__);
/* getRemainingSnapshots call will give us number of snapshots still
remaining after flushing current zsl buffer once*/
ALOGD("%s: Complete JPEG Encoding Done!", __func__);
setSnapshotState(SNAPSHOT_STATE_JPEG_COMPLETE_ENCODE_DONE);
mBurstModeFlag = false;
/* in case of zsl, we need to reset some of the zsl attributes */
if (isZSLMode()){
ALOGD("%s: Resetting the ZSL attributes", __func__);
setZSLChannelAttribute();
}
if (!isZSLMode() && !isLiveSnapshot()){
//Stop polling before calling datacb for if not ZSL mode
stopPolling();
}
} else {
ALOGD("%s: mNumOfRecievedJPEG(%d), mNumOfSnapshot(%d)", __func__, mNumOfRecievedJPEG, mNumOfSnapshot);
}
if(fail_cb_flag && mHalCamCtrl->mDataCb &&
(mHalCamCtrl->mMsgEnabled & CAMERA_MSG_COMPRESSED_IMAGE)) {
/* get picture failed. Give jpeg callback with NULL data
* to the application to restore to preview mode
*/
jpg_data_cb = mHalCamCtrl->mDataCb;
}
if(!fail_cb_flag) {
camera_memory_t *encodedMem = mHalCamCtrl->mGetMemory(
mHalCamCtrl->mJpegMemory.fd[0], mJpegOffset, 1, mHalCamCtrl);
if (!encodedMem || !encodedMem->data) {
ALOGE("%s: mGetMemory failed.\n", __func__);
}
mStopCallbackLock.unlock( );
if ((mActive || isLiveSnapshot()) && jpg_data_cb != NULL) {
ALOGV("%s: Calling upperlayer callback to store JPEG image", __func__);
jpg_data_cb (msg_type,encodedMem, 0, NULL,mHalCamCtrl->mCallbackCookie);
}
encodedMem->release( encodedMem );
jpg_data_cb = NULL;
}else{
ALOGV("Image Encoding Failed... Notify Upper layer");
mStopCallbackLock.unlock( );
if((mActive || isLiveSnapshot()) && jpg_data_cb != NULL) {
jpg_data_cb (CAMERA_MSG_COMPRESSED_IMAGE,NULL,0,NULL,
mHalCamCtrl->mCallbackCookie);
}
}
//reset jpeg_offset
mJpegOffset = 0;
if(isLiveSnapshot() && mHalCamCtrl->mStateLiveshot) {
deInitBuffer();
}
mHalCamCtrl->mStateLiveshot = false;
ALOGD("%s: X", __func__);
}
status_t QCameraStream_Snapshot::
configSnapshotDimension(cam_ctrl_dimension_t* dim)
{
bool matching = true;
cam_format_t img_format;
status_t ret = NO_ERROR;
ALOGD("%s: E", __func__);
ALOGI("%s:Passed picture size: %d X %d", __func__,
dim->picture_width, dim->picture_height);
ALOGI("%s:Passed postview size: %d X %d", __func__,
dim->ui_thumbnail_width, dim->ui_thumbnail_height);
/* First check if the picture resolution is the same, if not, change it*/
mHalCamCtrl->getPictureSize(&mPictureWidth, &mPictureHeight);
ALOGD("%s: Picture size received: %d x %d", __func__,
mPictureWidth, mPictureHeight);
mPostviewWidth = mHalCamCtrl->mParameters.getInt(CameraParameters::KEY_JPEG_THUMBNAIL_WIDTH);
mPostviewHeight = mHalCamCtrl->mParameters.getInt(CameraParameters::KEY_JPEG_THUMBNAIL_HEIGHT);
/*If application requested thumbnail size to be (0,0)
then configure second outout to a default size.
Jpeg encoder will drop thumbnail as reflected in encodeParams.
*/
mDropThumbnail = false;
if (mPostviewWidth == 0 && mPostviewHeight == 0) {
mPostviewWidth = THUMBNAIL_DEFAULT_WIDTH;
mPostviewHeight = THUMBNAIL_DEFAULT_HEIGHT;
mDropThumbnail = true;
}
if (isZSLMode() && ((mPictureWidth < mPostviewWidth) &&
(mPictureHeight < mPostviewHeight))) {
ALOGD("Setting input thumbnail size to PreviewWidth = %d PreviewHeight = %d in ZSL mode",
mHalCamCtrl->mPreviewWidth, mHalCamCtrl->mPreviewHeight);
mPostviewHeight = mHalCamCtrl->mPreviewHeight;
mPostviewWidth = mHalCamCtrl->mPreviewWidth;
}
ALOGD("%s: Postview size received: %d x %d", __func__,
mPostviewWidth, mPostviewHeight);
matching = (mPictureWidth == dim->picture_width) &&
(mPictureHeight == dim->picture_height);
matching &= (dim->ui_thumbnail_width == mPostviewWidth) &&
(dim->ui_thumbnail_height == mPostviewHeight);
/* picture size currently set do not match with the one wanted
by user.*/
if (!matching) {
if (mPictureWidth < mPostviewWidth || mPictureHeight < mPostviewHeight) {
//Changes to Handle VFE limitation.
mActualPictureWidth = mPictureWidth;
mActualPictureHeight = mPictureHeight;
mPictureWidth = mPostviewWidth;
mPictureHeight = mPostviewHeight;
mJpegDownscaling = TRUE;
}else{
mJpegDownscaling = FALSE;
}
dim->picture_width = mPictureWidth;
dim->picture_height = mPictureHeight;
dim->ui_thumbnail_height = mThumbnailHeight = mPostviewHeight;
dim->ui_thumbnail_width = mThumbnailWidth = mPostviewWidth;
}
#if 0
img_format = mHalCamCtrl->getPreviewFormat();
if (img_format) {
matching &= (img_format == dim->main_img_format);
if (!matching) {
dim->main_img_format = img_format;
dim->thumb_format = img_format;
}
}
#endif
if (!matching) {
ALOGD("%s: Image Sizes before set parm call: main: %dx%d thumbnail: %dx%d",
__func__,
dim->picture_width, dim->picture_height,
dim->ui_thumbnail_width, dim->ui_thumbnail_height);
ret = cam_config_set_parm(mCameraId, MM_CAMERA_PARM_DIMENSION,dim);
if (NO_ERROR != ret) {
ALOGE("%s: error - can't config snapshot parms!", __func__);
ret = FAILED_TRANSACTION;
goto end;
}
}
/* set_parm will return corrected dimension based on aspect ratio and
ceiling size */
mPictureWidth = dim->picture_width;
mPictureHeight = dim->picture_height;
mPostviewHeight = mThumbnailHeight = dim->ui_thumbnail_height;
mPostviewWidth = mThumbnailWidth = dim->ui_thumbnail_width;
mPictureFormat= dim->main_img_format;
mThumbnailFormat = dim->thumb_format;
ALOGD("%s: Image Format: %d", __func__, dim->main_img_format);
ALOGI("%s: Image Sizes: main: %dx%d thumbnail: %dx%d", __func__,
dim->picture_width, dim->picture_height,
dim->ui_thumbnail_width, dim->ui_thumbnail_height);
end:
ALOGD("%s: X", __func__);
return ret;
}
status_t QCameraStream_Snapshot::
initRawSnapshotChannel(cam_ctrl_dimension_t *dim,
int num_of_snapshots)
{
status_t ret = NO_ERROR;
mm_camera_ch_image_fmt_parm_t fmt;
mm_camera_channel_attr_t ch_attr;
mm_camera_raw_streaming_type_t raw_stream_type =
MM_CAMERA_RAW_STREAMING_CAPTURE_SINGLE;
ALOGD("%s: E", __func__);
/* Initialize stream - set format, acquire channel */
/*TBD: Currently we only support single raw capture*/
ALOGE("num_of_snapshots = %d",num_of_snapshots);
if (num_of_snapshots == 1) {
raw_stream_type = MM_CAMERA_RAW_STREAMING_CAPTURE_SINGLE;
}
/* Set channel attribute */
ALOGD("%s: Set Raw Snapshot Channel attribute", __func__);
memset(&ch_attr, 0, sizeof(ch_attr));
ch_attr.type = MM_CAMERA_CH_ATTR_RAW_STREAMING_TYPE;
ch_attr.raw_streaming_mode = raw_stream_type;
if( NO_ERROR !=
cam_ops_ch_set_attr(mCameraId, MM_CAMERA_CH_RAW, &ch_attr)) {
ALOGD("%s: Failure setting Raw channel attribute.", __func__);
ret = FAILED_TRANSACTION;
goto end;
}
memset(&fmt, 0, sizeof(mm_camera_ch_image_fmt_parm_t));
fmt.ch_type = MM_CAMERA_CH_RAW;
fmt.def.fmt = CAMERA_BAYER_SBGGR10;
fmt.def.dim.width = dim->raw_picture_width;
fmt.def.dim.height = dim->raw_picture_height;
ALOGV("%s: Raw snapshot channel fmt: %d", __func__,
fmt.def.fmt);
ALOGV("%s: Raw snapshot resolution: %dX%d", __func__,
dim->raw_picture_width, dim->raw_picture_height);
ALOGD("%s: Set Raw Snapshot channel image format", __func__);
ret = cam_config_set_parm(mCameraId, MM_CAMERA_PARM_CH_IMAGE_FMT, &fmt);
if (NO_ERROR != ret) {
ALOGE("%s: Set Raw Snapshot Channel format err=%d\n", __func__, ret);
ret = FAILED_TRANSACTION;
goto end;
}
end:
if (ret != NO_ERROR) {
handleError();
}
ALOGE("%s: X", __func__);
return ret;
}
status_t QCameraStream_Snapshot::
setZSLChannelAttribute(void)
{
status_t ret = NO_ERROR;
mm_camera_channel_attr_t ch_attr;
ALOGD("%s: E", __func__);
memset(&ch_attr, 0, sizeof(mm_camera_channel_attr_t));
ch_attr.type = MM_CAMERA_CH_ATTR_BUFFERING_FRAME;
ch_attr.buffering_frame.look_back = mHalCamCtrl->getZSLBackLookCount();
ch_attr.buffering_frame.water_mark = mHalCamCtrl->getZSLQueueDepth();
ALOGE("%s: ZSL queue_depth = %d, back_look_count = %d", __func__,
ch_attr.buffering_frame.water_mark,
ch_attr.buffering_frame.look_back);
if( NO_ERROR !=
cam_ops_ch_set_attr(mCameraId, MM_CAMERA_CH_SNAPSHOT, &ch_attr)) {
ALOGD("%s: Failure setting ZSL channel attribute.", __func__);
ret = FAILED_TRANSACTION;
goto end;
}
end:
ALOGD("%s: X", __func__);
return ret;
}
status_t QCameraStream_Snapshot::
initSnapshotFormat(cam_ctrl_dimension_t *dim)
{
status_t ret = NO_ERROR;
mm_camera_ch_image_fmt_parm_t fmt;
ALOGD("%s: E", __func__);
/* For ZSL mode we'll need to set channel attribute */
if (isZSLMode()) {
ret = setZSLChannelAttribute();
if (ret != NO_ERROR) {
goto end;
}
}
memset(&fmt, 0, sizeof(mm_camera_ch_image_fmt_parm_t));
fmt.ch_type = MM_CAMERA_CH_SNAPSHOT;
fmt.snapshot.main.fmt = dim->main_img_format;
fmt.snapshot.main.dim.width = dim->picture_width;
fmt.snapshot.main.dim.height = dim->picture_height;
fmt.snapshot.thumbnail.fmt = dim->thumb_format;
fmt.snapshot.thumbnail.dim.width = dim->ui_thumbnail_width;
fmt.snapshot.thumbnail.dim.height = dim->ui_thumbnail_height;
ALOGV("%s: Snapshot channel fmt = main: %d thumbnail: %d", __func__,
dim->main_img_format, dim->thumb_format);
ALOGV("%s: Snapshot channel resolution = main: %dX%d thumbnail: %dX%d",
__func__, dim->picture_width, dim->picture_height,
dim->ui_thumbnail_width, dim->ui_thumbnail_height);
ALOGD("%s: Set Snapshot channel image format", __func__);
ret = cam_config_set_parm(mCameraId, MM_CAMERA_PARM_CH_IMAGE_FMT, &fmt);
if (NO_ERROR != ret) {
ALOGE("%s: Set Snapshot Channel format err=%d\n", __func__, ret);
ret = FAILED_TRANSACTION;
goto end;
}
end:
if (ret != NO_ERROR) {
handleError();
}
ALOGE("%s: X", __func__);
return ret;
}
void QCameraStream_Snapshot::
deinitSnapshotChannel(mm_camera_channel_type_t ch_type)
{
ALOGD("%s: E", __func__);
/* unreg buf notify*/
if (getSnapshotState() >= SNAPSHOT_STATE_BUF_NOTIF_REGD){
if (NO_ERROR != cam_evt_register_buf_notify(mCameraId,
ch_type, NULL,(mm_camera_register_buf_cb_type_t)NULL,NULL, this)) {
ALOGE("%s: Failure to unregister buf notification", __func__);
}
}
if (getSnapshotState() >= SNAPSHOT_STATE_CH_ACQUIRED) {
ALOGD("%s: Release snapshot channel", __func__);
cam_ops_ch_release(mCameraId, ch_type);
}
ALOGD("%s: X",__func__);
}
status_t QCameraStream_Snapshot::
initRawSnapshotBuffers(cam_ctrl_dimension_t *dim, int num_of_buf)
{
status_t ret = NO_ERROR;
struct msm_frame *frame;
uint32_t frame_len;
uint8_t num_planes;
uint32_t planes[VIDEO_MAX_PLANES];
mm_camera_reg_buf_t reg_buf;
ALOGD("%s: E", __func__);
memset(®_buf, 0, sizeof(mm_camera_reg_buf_t));
memset(&mSnapshotStreamBuf, 0, sizeof(mSnapshotStreamBuf));
if ((num_of_buf == 0) || (num_of_buf > MM_CAMERA_MAX_NUM_FRAMES)) {
ALOGE("%s: Invalid number of buffers (=%d) requested!", __func__, num_of_buf);
ret = BAD_VALUE;
goto end;
}
reg_buf.def.buf.mp = new mm_camera_mp_buf_t[num_of_buf];
if (!reg_buf.def.buf.mp) {
ALOGE("%s Error allocating memory for mplanar struct ", __func__);
ret = NO_MEMORY;
goto end;
}
memset(reg_buf.def.buf.mp, 0, num_of_buf * sizeof(mm_camera_mp_buf_t));
/* Get a frame len for buffer to be allocated*/
frame_len = mm_camera_get_msm_frame_len(CAMERA_BAYER_SBGGR10,
myMode,
dim->raw_picture_width,
dim->raw_picture_height,
OUTPUT_TYPE_S,
&num_planes, planes);
ALOGE("Got frame_len=%d",frame_len);
if (mHalCamCtrl->initHeapMem(&mHalCamCtrl->mRawMemory, num_of_buf,
frame_len, 0, planes[0], MSM_PMEM_RAW_MAINIMG,
&mSnapshotStreamBuf, ®_buf.def,
num_planes, planes) < 0) {
ret = NO_MEMORY;
goto end;
}
/* register the streaming buffers for the channel*/
reg_buf.ch_type = MM_CAMERA_CH_RAW;
reg_buf.def.num = mSnapshotStreamBuf.num;
ret = cam_config_prepare_buf(mCameraId, ®_buf);
if(ret != NO_ERROR) {
ALOGV("%s:reg snapshot buf err=%d\n", __func__, ret);
ret = FAILED_TRANSACTION;
mHalCamCtrl->releaseHeapMem(&mHalCamCtrl->mRawMemory);
goto end;
}
/* If we have reached here successfully, we have allocated buffer.
Set state machine.*/
setSnapshotState(SNAPSHOT_STATE_BUF_INITIALIZED);
end:
/* If it's error, we'll need to do some needful */
if (ret != NO_ERROR) {
handleError();
}
if (reg_buf.def.buf.mp)
delete []reg_buf.def.buf.mp;
ALOGD("%s: X", __func__);
return ret;
}
status_t QCameraStream_Snapshot::deinitRawSnapshotBuffers(void)
{
int ret = NO_ERROR;
ALOGD("%s: E", __func__);
int err = getSnapshotState();
/* deinit buffers only if we have already allocated */
if (err >= SNAPSHOT_STATE_BUF_INITIALIZED || err == SNAPSHOT_STATE_ERROR){
ALOGD("%s: Unpreparing Snapshot Buffer", __func__);
ret = cam_config_unprepare_buf(mCameraId, MM_CAMERA_CH_RAW);
if(ret != NO_ERROR) {
ALOGE("%s:Unreg Raw snapshot buf err=%d\n", __func__, ret);
ret = FAILED_TRANSACTION;
goto end;
}
mHalCamCtrl->releaseHeapMem(&mHalCamCtrl->mRawMemory);
}
end:
ALOGD("%s: X", __func__);
return ret;
}
status_t QCameraStream_Snapshot::
initSnapshotBuffers(cam_ctrl_dimension_t *dim, int num_of_buf)
{
status_t ret = NO_ERROR;
struct msm_frame *frame;
uint32_t frame_len, y_off, cbcr_off;
uint8_t num_planes;
uint32_t planes[VIDEO_MAX_PLANES];
mm_camera_reg_buf_t reg_buf;
int rotation = 0;
ALOGD("%s: E", __func__);
mNumOfRecievedJPEG = 0;
memset(®_buf, 0, sizeof(mm_camera_reg_buf_t));
memset(&mSnapshotStreamBuf, 0, sizeof(mSnapshotStreamBuf));
if ((num_of_buf == 0) || (num_of_buf > MM_CAMERA_MAX_NUM_FRAMES)) {
ALOGE("%s: Invalid number of buffers (=%d) requested!",
__func__, num_of_buf);
ret = BAD_VALUE;
goto end;
}
ALOGD("%s: Mode: %d Num_of_buf: %d ImageSizes: main: %dx%d thumb: %dx%d",
__func__, myMode, num_of_buf,
dim->picture_width, dim->picture_height,
dim->ui_thumbnail_width, dim->ui_thumbnail_height);
if (!isLiveSnapshot()) {
reg_buf.snapshot.main.buf.mp = new mm_camera_mp_buf_t[num_of_buf];
if (!reg_buf.snapshot.main.buf.mp) {
ALOGE("%s Error allocating memory for mplanar struct ", __func__);
ret = NO_MEMORY;
goto end;
}
memset(reg_buf.snapshot.main.buf.mp, 0,
num_of_buf * sizeof(mm_camera_mp_buf_t));
}
if (!isFullSizeLiveshot() || !isLiveSnapshot()) {
reg_buf.snapshot.thumbnail.buf.mp = new mm_camera_mp_buf_t[num_of_buf];
if (!reg_buf.snapshot.thumbnail.buf.mp) {
ALOGE("%s Error allocating memory for mplanar struct ", __func__);
ret = NO_MEMORY;
goto end;
}
memset(reg_buf.snapshot.thumbnail.buf.mp, 0,
num_of_buf * sizeof(mm_camera_mp_buf_t));
}
/* Number of buffers to be set*/
/* Set the JPEG Rotation here since get_buffer_offset needs
* the value of rotation.*/
mHalCamCtrl->setJpegRotation(isZSLMode());
rotation = mHalCamCtrl->getJpegRotation();
if(rotation != dim->rotation) {
dim->rotation = rotation;
ret = cam_config_set_parm(mHalCamCtrl->mCameraId, MM_CAMERA_PARM_DIMENSION, dim);
}
if(isLiveSnapshot()) {
ret = cam_config_set_parm(mHalCamCtrl->mCameraId, MM_CAMERA_PARM_DIMENSION, dim);
}
/* num_planes = 2;
planes[0] = dim->picture_frame_offset.mp[0].len;
planes[1] = dim->picture_frame_offset.mp[1].len;
frame_len = dim->picture_frame_offset.frame_len;
y_off = dim->picture_frame_offset.mp[0].offset;
cbcr_off = dim->picture_frame_offset.mp[1].offset;*/
mm_jpeg_encoder_get_buffer_offset( dim->picture_width, dim->picture_height,&y_off, &cbcr_off, &frame_len,&num_planes, planes);
ALOGE("%s: main image: rotation = %d, yoff = %d, cbcroff = %d, size = %d, width = %d, height = %d",
__func__, dim->rotation, y_off, cbcr_off, frame_len, dim->picture_width, dim->picture_height);
if (mHalCamCtrl->initHeapMem (&mHalCamCtrl->mJpegMemory, 1, frame_len, 0, 0,
MSM_PMEM_MAX, NULL, NULL, num_planes, planes) < 0) {
ALOGE("%s: Error allocating JPEG memory", __func__);
ret = NO_MEMORY;
goto end;
}
if(!isLiveSnapshot()) {
if (mHalCamCtrl->initHeapMem(&mHalCamCtrl->mSnapshotMemory, num_of_buf,
frame_len, y_off, cbcr_off, MSM_PMEM_MAINIMG, &mSnapshotStreamBuf,
®_buf.snapshot.main, num_planes, planes) < 0) {
ret = NO_MEMORY;
mHalCamCtrl->releaseHeapMem(&mHalCamCtrl->mJpegMemory);
goto end;
};
/* num_planes = 2;
planes[0] = dim->thumb_frame_offset.mp[0].len;
planes[1] = dim->thumb_frame_offset.mp[1].len;
frame_len = planes[0] + planes[1];*/
if (!isFullSizeLiveshot()) {
// y_off = dim->thumb_frame_offset.mp[0].offset;
// cbcr_off = dim->thumb_frame_offset.mp[1].offset;
ALOGE("%s: thumbnail: rotation = %d, yoff = %d, cbcroff = %d, size = %d, width = %d, height = %d",
__func__, dim->rotation, y_off, cbcr_off, frame_len,
dim->thumbnail_width, dim->thumbnail_height);
mm_jpeg_encoder_get_buffer_offset( dim->ui_thumbnail_width, dim->ui_thumbnail_height,
&y_off, &cbcr_off, &frame_len,
&num_planes, planes);
if (mHalCamCtrl->initHeapMem(&mHalCamCtrl->mThumbnailMemory, num_of_buf,
frame_len, y_off, cbcr_off, MSM_PMEM_THUMBNAIL, &mPostviewStreamBuf,
®_buf.snapshot.thumbnail, num_planes, planes) < 0) {
ret = NO_MEMORY;
mHalCamCtrl->releaseHeapMem(&mHalCamCtrl->mSnapshotMemory);
mHalCamCtrl->releaseHeapMem(&mHalCamCtrl->mJpegMemory);
goto end;
};
}
/* register the streaming buffers for the channel*/
reg_buf.ch_type = MM_CAMERA_CH_SNAPSHOT;
reg_buf.snapshot.main.num = mSnapshotStreamBuf.num;
if (!isFullSizeLiveshot())
reg_buf.snapshot.thumbnail.num = mPostviewStreamBuf.num;
else
reg_buf.snapshot.thumbnail.num = 0;
ret = cam_config_prepare_buf(mCameraId, ®_buf);
if(ret != NO_ERROR) {
ALOGV("%s:reg snapshot buf err=%d\n", __func__, ret);
ret = FAILED_TRANSACTION;
if (!isFullSizeLiveshot()) {
mHalCamCtrl->releaseHeapMem(&mHalCamCtrl->mThumbnailMemory);
}
mHalCamCtrl->releaseHeapMem(&mHalCamCtrl->mSnapshotMemory);
mHalCamCtrl->releaseHeapMem(&mHalCamCtrl->mJpegMemory);
goto end;
}
}
/* If we have reached here successfully, we have allocated buffer.
Set state machine.*/
setSnapshotState(SNAPSHOT_STATE_BUF_INITIALIZED);
end:
if (ret != NO_ERROR) {
handleError();
}
if (reg_buf.snapshot.main.buf.mp)
delete []reg_buf.snapshot.main.buf.mp;
if (reg_buf.snapshot.thumbnail.buf.mp)
delete []reg_buf.snapshot.thumbnail.buf.mp;
ALOGD("%s: X", __func__);
return ret;
}
status_t QCameraStream_Snapshot::
deinitSnapshotBuffers(void)
{
int ret = NO_ERROR;
ALOGD("%s: E", __func__);
int err = getSnapshotState();
/* Deinit only if we have already initialized*/
if (err >= SNAPSHOT_STATE_BUF_INITIALIZED || err == SNAPSHOT_STATE_ERROR){
if(!isLiveSnapshot()) {
ALOGD("%s: Unpreparing Snapshot Buffer", __func__);
ret = cam_config_unprepare_buf(mCameraId, MM_CAMERA_CH_SNAPSHOT);
if(ret != NO_ERROR) {
ALOGE("%s:unreg snapshot buf err=%d\n", __func__, ret);
ret = FAILED_TRANSACTION;
goto end;
}
}
/* Clear main and thumbnail heap*/
if(!isLiveSnapshot()) {
mHalCamCtrl->releaseHeapMem(&mHalCamCtrl->mSnapshotMemory);
if (!isFullSizeLiveshot())
mHalCamCtrl->releaseHeapMem(&mHalCamCtrl->mThumbnailMemory);
}
mHalCamCtrl->releaseHeapMem(&mHalCamCtrl->mJpegMemory);
}
end:
ALOGD("%s: X", __func__);
return ret;
}
void QCameraStream_Snapshot::deInitBuffer(void)
{
mm_camera_channel_type_t ch_type;
ALOGI("%s: E", __func__);
if( getSnapshotState() == SNAPSHOT_STATE_UNINIT) {
ALOGD("%s: Already deinit'd!", __func__);
return;
}
if (mSnapshotFormat == PICTURE_FORMAT_RAW) {
/* deinit buffer */
deinitRawSnapshotBuffers();
}
else
{
if (!isZSLMode() &&
((mHalCamCtrl->getHDRMode() == HDR_MODE) || (mHalCamCtrl->isWDenoiseEnabled()))) {
/*register main and thumbnail buffers at back-end for frameproc*/
for (int i = 0; i < mHalCamCtrl->mSnapshotMemory.buffer_count; i++) {
if (NO_ERROR != mHalCamCtrl->sendUnMappingBuf(MSM_V4L2_EXT_CAPTURE_MODE_MAIN, i)) {
ALOGE("%s: sending unmapping data Msg Failed", __func__);
}
if (NO_ERROR != mHalCamCtrl->sendUnMappingBuf(MSM_V4L2_EXT_CAPTURE_MODE_THUMBNAIL, i)) {
ALOGE("%s: sending unmapping data Msg Failed", __func__);
}
}
}
deinitSnapshotBuffers();
}
/* deinit jpeg buffer if allocated */
if(mJpegHeap != NULL) {
mJpegHeap.clear();
}
mJpegHeap = NULL;
/* memset some global structure */
memset(&mSnapshotStreamBuf, 0, sizeof(mSnapshotStreamBuf));
memset(&mPostviewStreamBuf, 0, sizeof(mPostviewStreamBuf));
mSnapshotQueue.flush();
mWDNQueue.flush();
setSnapshotState(SNAPSHOT_STATE_UNINIT);
ALOGD("%s: X", __func__);
}
/*Temp: to be removed once event handling is enabled in mm-camera.
We need an event - one event for
stream-off to disable OPS_SNAPSHOT*/
void QCameraStream_Snapshot::runSnapshotThread(void *data)
{
ALOGD("%s: E", __func__);
if (mSnapshotFormat == PICTURE_FORMAT_RAW) {
/* TBD: Temp: Needs to be removed once event handling is enabled.
We cannot call mm-camera interface to stop snapshot from callback
function as it causes deadlock. Hence handling it here temporarily
in this thread. Later mm-camera intf will give us event in separate
thread context */
mm_app_snapshot_wait();
/* Send command to stop snapshot polling thread*/
stop();
}
ALOGD("%s: X", __func__);
}
/*Temp: to be removed once event handling is enabled in mm-camera*/
static void *snapshot_thread(void *obj)
{
QCameraStream_Snapshot *pme = (QCameraStream_Snapshot *)obj;
ALOGD("%s: E", __func__);
if (pme != 0) {
pme->runSnapshotThread(obj);
}
else ALOGW("not starting snapshot thread: the object went away!");
ALOGD("%s: X", __func__);
return NULL;
}
/*Temp: to be removed later*/
static pthread_t mSnapshotThread;
status_t QCameraStream_Snapshot::initJPEGSnapshot(int num_of_snapshots)
{
status_t ret = NO_ERROR;
cam_ctrl_dimension_t dim;
mm_camera_op_mode_type_t op_mode;
ALOGV("%s: E", __func__);
if (isFullSizeLiveshot())
goto end;
ALOGD("%s: Get current dimension", __func__);
/* Query mm_camera to get current dimension */
memset(&dim, 0, sizeof(cam_ctrl_dimension_t));
ret = cam_config_get_parm(mCameraId,
MM_CAMERA_PARM_DIMENSION, &dim);