-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathSkImageEncoder_libtijpeg.cpp
946 lines (803 loc) · 31.4 KB
/
SkImageEncoder_libtijpeg.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
/*
* Copyright (C) Texas Instruments - http://www.ti.com/
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* ==============================================================================
* Texas Instruments OMAP (TM) Platform Software
* (c) Copyright Texas Instruments, Incorporated. All Rights Reserved.
*
* Use of this software is controlled by the terms and conditions found
* in the license agreement under which this software has been supplied.
* ============================================================================ */
/**
* @file SkImageEncoder_libtijpeg.cpp
*
* This file implements SkTIJPEGImageEncoder
*
*/
#include "SkImageEncoder_libtijpeg.h"
#include "SkTime.h"
#ifdef DEBUG_LOG
# define PRINTF SkDebugf
#else
# define PRINTF
#endif
#define TIME_ENCODE
#define EVEN_RES_ONLY 1 //true if odd resolution encode support enabled using SW codec
#define MULTIPLE 2 //image width must be a multiple of this number
#define JPEG_ENCODER_DUMP_INPUT_AND_OUTPUT 0
#if JPEG_ENCODER_DUMP_INPUT_AND_OUTPUT
int eOutputCount = 0;
int eInputCount = 0;
#endif
void* ThreadEncoderWrapper(void* me)
{
SkTIJPEGImageEncoder *encoder = static_cast<SkTIJPEGImageEncoder *>(me);
encoder->Run();
return NULL;
}
OMX_ERRORTYPE OMX_JPEGE_FillBufferDone (OMX_HANDLETYPE hComponent, OMX_PTR ptr, OMX_BUFFERHEADERTYPE* pBuffHead)
{
//PRINTF("OMX_FillBufferDone: pBuffHead = %p, pBuffer = %p, n FilledLen = %ld \n", pBuffHead, pBuffHead->pBuffer, pBuffHead->nFilledLen);
((SkTIJPEGImageEncoder *)ptr)->FillBufferDone(pBuffHead->pBuffer, pBuffHead->nFilledLen);
return OMX_ErrorNone;
}
OMX_ERRORTYPE OMX_JPEGE_EmptyBufferDone(OMX_HANDLETYPE hComponent, OMX_PTR ptr, OMX_BUFFERHEADERTYPE* pBuffer)
{
SkTIJPEGImageEncoder * ImgEnc = (SkTIJPEGImageEncoder *)ptr;
ImgEnc->iLastState = ImgEnc->iState;
ImgEnc->iState = SkTIJPEGImageEncoder::STATE_EMPTY_BUFFER_DONE_CALLED;
sem_post(ImgEnc->semaphore) ;
return OMX_ErrorNone;
}
OMX_ERRORTYPE OMX_JPEGE_EventHandler(OMX_HANDLETYPE hComponent,
OMX_PTR pAppData,
OMX_EVENTTYPE eEvent,
OMX_U32 nData1,
OMX_U32 nData2,
OMX_PTR pEventData)
{
((SkTIJPEGImageEncoder *)pAppData)->EventHandler(hComponent, eEvent, nData1, nData2, pEventData);
return OMX_ErrorNone;
}
SkTIJPEGImageEncoder::SkTIJPEGImageEncoder()
{
mLoad = 0;
mDeleteAttempts = 0;
pInBuffHead = NULL;
pOutBuffHead = NULL;
pOMXHandle = NULL;
pEncodedOutputBuffer = NULL;
semaphore = NULL;
semaphore = (sem_t*)malloc(sizeof(sem_t)) ;
sem_init(semaphore, 0x00, 0x00);
}
SkTIJPEGImageEncoder::~SkTIJPEGImageEncoder()
{
#if OPTIMIZE
OMX_ERRORTYPE eError = OMX_ErrorNone;
if( ( iLastState || iState ) && ( NULL != pOMXHandle ) ){
if(iState == STATE_EXIT)
{
iState = STATE_EMPTY_BUFFER_DONE_CALLED;
eError = OMX_SendCommand(pOMXHandle,OMX_CommandStateSet, OMX_StateIdle, NULL);
if ( eError != OMX_ErrorNone ) {
PRINTF("\nError from SendCommand-Idle(nStop) State function\n");
iState = STATE_ERROR;
PRINTF("sem_post at %d", __LINE__);
sem_post(semaphore) ;
}
Run();
}else{
iState = STATE_EXIT;
sem_post(semaphore);
}
}
#endif
gTIJpegEncMutex.lock();
if(pEncodedOutputBuffer != NULL)
{
free(pEncodedOutputBuffer);
pEncodedOutputBuffer = NULL;
}
sem_destroy(semaphore);
if (semaphore != NULL) {
free(semaphore) ;
semaphore = NULL;
}
gTIJpegEncMutex.unlock();
}
void SkTIJPEGImageEncoder::FillBufferDone(OMX_U8* pBuffer, OMX_U32 size)
{
#if JPEG_ENCODER_DUMP_INPUT_AND_OUTPUT
char path[50];
snprintf(path, sizeof(path), "/temp/JEO_%d.jpg", eOutputCount);
PRINTF("rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr");
SkFILEWStream tempFile(path);
if (tempFile.write(pBuffer, size) == false)
PRINTF("Writing to %s failed\n", path);
else
PRINTF("Writing to %s succeeded\n", path);
eOutputCount++;
PRINTF("rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr");
#endif
iLastState = iState;
iState = STATE_FILL_BUFFER_DONE_CALLED;
jpegSize = size;
pEncodedOutputBuffer = pBuffer;
nEncodedOutputFilledLen = size;
//sem_post(semaphore) ;
}
void SkTIJPEGImageEncoder::EventHandler(OMX_HANDLETYPE hComponent,
OMX_EVENTTYPE eEvent,
OMX_U32 nData1,
OMX_U32 nData2,
OMX_PTR pEventData)
{
//PRINTF("\nEventHandler:: eEvent = %x, nData1 = %x, nData2 = %x\n\n", eEvent, (unsigned int)nData1, (unsigned int)nData2);
switch ( eEvent ) {
case OMX_EventCmdComplete:
/* Do not move the common stmts in these conditionals outside. */
/* We do not want to apply them in cases when these conditions are not met. */
if ((nData1 == OMX_CommandStateSet) && (nData2 == OMX_StateIdle))
{
//PRINTF ("Component State Changed To OMX_StateIdle\n");
iLastState = iState;
iState = STATE_IDLE;
sem_post(semaphore) ;
}
else if ((nData1 == OMX_CommandStateSet) && (nData2 == OMX_StateExecuting))
{
//PRINTF ("Component State Changed To OMX_StateExecuting\n");
iLastState = iState;
iState = STATE_EXECUTING;
sem_post(semaphore) ;
}
else if ((nData1 == OMX_CommandStateSet) && (nData2 == OMX_StateLoaded))
{
//PRINTF ("Component State Changed To OMX_StateLoaded\n");
iLastState = iState;
iState = STATE_LOADED;
sem_post(semaphore) ;
}
else if ((nData1 == OMX_CommandStateSet) && (nData2 == OMX_StateInvalid))
{
//PRINTF ("Component State Changed To OMX_StateInvalid\n");
iState = STATE_ERROR;
sem_post(semaphore) ;
#if OPTIMIZE
// Run() is not running under this condition. We won't clean anything without this.
// We also don't want to block here because we could deadlock the system
// OMX calls into this function, and Run() will call into OMX
if (iLastState == STATE_EXIT){
pthread_t thread;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_create(&thread, &attr, ThreadEncoderWrapper, this);
pthread_attr_destroy(&attr);
}
#endif
}
break;
case OMX_EventError:
PRINTF ("\n\n\nOMX Component reported an Error!!!!\n\n\n");
if(iState != STATE_ERROR)
{
iLastState = iState;
iState = STATE_ERROR;
OMX_SendCommand(hComponent, OMX_CommandStateSet, OMX_StateInvalid, NULL);
// call sem_post(semaphore) at OMX_StateInvalid state set event.
}else{
PRINTF ("Libskiahw(encoder) already handling Error!!!");
}
break;
default:
break;
}
}
bool SkTIJPEGImageEncoder::onEncodeSW(SkWStream* stream, const SkBitmap& bm, int quality) {
bool result = false;
SkJPEGImageEncoder* pSWJPGEHandle = new SkJPEGImageEncoder;
if (!pSWJPGEHandle) {
PRINTF("\nERROR:: !!Could not get the SW codec handle!!\n");
return false;
}
SkDebugf("\n#### Using SW(ARM) Image Encoder ####\n\n");
result = pSWJPGEHandle->encodeStream(stream, bm, quality);
delete pSWJPGEHandle;
return result;
}
bool SkTIJPEGImageEncoder::onEncode(SkImageEncoder* enc_impl, SkWStream* stream, const SkBitmap& bm, int quality)
{
int w, h, nWidthNew, nHeightNew, nMultFactor, nBytesPerPixel;
OMX_U32 inBuffSize;
w = bm.width();
h = bm.height();
PRINTF("\n******* WxH = %d x %d ******\n",w,h);
/*decide to choose the codec based on resolution W*H */
//If W or H is ODD or W*H < WVGA resolution then invoke SW codec (ARM)
if ( (w % 2) || (h % 2) || (w * h <= WVGA_RESOLUTION) ) {
return onEncodeSW (stream, bm, quality);
}
PRINTF("\nUsing TI Image Encoder.\n");
AutoTrackLoad autotrack(mLoad); //increase load here, should be fine even though it's done outside of critical section as long as we keep it 1 to 1 in this function
SkAutoLockPixels alp(bm);
if (NULL == bm.getPixels()) {
return false;
}
#ifdef TIME_ENCODE
AutoTimeMicros atm("TI JPEG Encode");
atm.setResolution(w , h);
#endif
nMultFactor = (w + 16 - 1)/16;
nWidthNew = (int)(nMultFactor) * 16;
nMultFactor = (h + 16 - 1)/16;
nHeightNew = (int)(nMultFactor) * 16;
if (bm.config() == SkBitmap::kRGB_565_Config)
nBytesPerPixel = 2;
else if (bm.config() == SkBitmap::kARGB_8888_Config)
nBytesPerPixel = 4;
else{
PRINTF("This format is not supported!");
return false;
}
inBuffSize = nWidthNew * nHeightNew * nBytesPerPixel;
if (inBuffSize < 1600)
inBuffSize = 1600;
inBuffSize = (OMX_U32)((inBuffSize + ALIGN_128_BYTE - 1) & ~(ALIGN_128_BYTE - 1));
void *inputBuffer = memalign(ALIGN_128_BYTE, inBuffSize);
if ( inputBuffer == NULL) {
PRINTF("\n %s():%d:: ERROR:: inputBuffer Allocation Failed. \n", __FUNCTION__,__LINE__);
return false;
}
#if EVEN_RES_ONLY
memcpy(inputBuffer, bm.getPixels(), bm.getSize());
#else
//This padding logic can be used for the odd sized image encoding
//when TI DSP codec used
int pad_width = w%MULTIPLE;
int pad_height = h%2;
int pixels_to_pad = 0;
if (pad_width)
pixels_to_pad = MULTIPLE - pad_width;
int bytes_to_pad = pixels_to_pad * nBytesPerPixel;
printf("\npixels_to_pad = %d\n", pixels_to_pad );
int src = (int)inputBuffer;
int dst = (int)bm.getPixels();
int i, j;
int row = w * nBytesPerPixel;
if (pad_height && pad_width)
{
printf("\ndealing with pad_width");
for(i = 0; i < h; i++)
{
memcpy((void *)src, (void *)dst, row);
dst = dst + row;
src = src + row;
for (j = 0; j < bytes_to_pad; j++){
*((char *)src) = 0;
src++;
}
}
printf("\ndealing with odd height");
memset((void *)src, 0, row+bytes_to_pad);
w+=pixels_to_pad;
h++;
}
else if (pad_height)
{
memcpy((void *)src, (void*)dst, (w*h*nBytesPerPixel));
printf("\nadding one line and making it zero");
memset((void *)(src + (w*h*nBytesPerPixel)), 0, row);
h++;
}
else if (pad_width)
{
printf("\ndealing with odd width");
for(i = 0; i < h; i++)
{
memcpy((void *)src, (void *)dst, row);
dst = dst + row;
src = src + row;
for (j = 0; j < bytes_to_pad; j++){
*((char *)src) = 0;
src++;
}
}
w+=pixels_to_pad;
}
else
{
printf("\nno padding");
memcpy((void *)src, (void*)dst, (w*h*nBytesPerPixel));
}
#endif
OMX_U32 outBuffSize = bm.width() * bm.height();
if( quality < 10){
outBuffSize /=10;
}
else if (quality <100){
outBuffSize /= (100/quality);
}
/*Adding memory to include Thumbnail, comments & markers information and header (depends on the app)*/
outBuffSize += 12288;
outBuffSize = (OMX_U32)((outBuffSize + ALIGN_128_BYTE - 1) & ~(ALIGN_128_BYTE - 1));
PRINTF("\nOriginal: w = %d, h = %d", bm.width(), bm.height());
if (encodeImage(outBuffSize,inputBuffer, inBuffSize, w, h, quality, bm.config())){
stream->write(pEncodedOutputBuffer, nEncodedOutputFilledLen);
free(inputBuffer);
return true;
}
free(inputBuffer);
return false;
}
bool SkTIJPEGImageEncoder::encodeImage(int outBuffSize, void *inputBuffer, int inBuffSize, int width, int height, int quality, SkBitmap::Config config)
{
gTIJpegEncMutex.lock();
/* Critical section */
PRINTF("Entering Critical Section \n");
int reuseHandle = 0;
int nRetval;
int nIndex1;
int nIndex2;
int bitsPerPixel;
int nMultFactor = 0;
int nHeightNew, nWidthNew;
char strTIJpegEnc[] = "OMX.TI.JPEG.encoder";
char strQFactor[] = "OMX.TI.JPEG.encoder.Config.QFactor";
char strInputWidth[] = "OMX.TI.JPEG.encoder.Config.InputFrameWidth";
char strInputHeight[] = "OMX.TI.JPEG.encoder.Config.InputFrameHeight";
/*Minimum buffer size requirement */
void *outputBuffer = NULL;
OMX_S32 nCompId = 300;
OMX_PORT_PARAM_TYPE PortType;
OMX_ERRORTYPE eError = OMX_ErrorNone;
OMX_IMAGE_PARAM_QFACTORTYPE QfactorType;
OMX_INDEXTYPE nCustomIndex = OMX_IndexMax;
OMX_CALLBACKTYPE JPEGCallBack ={OMX_JPEGE_EventHandler, OMX_JPEGE_EmptyBufferDone, OMX_JPEGE_FillBufferDone};
PRINTF("width = %d", width);
PRINTF("height = %d", height);
PRINTF("config = %d", config);
PRINTF("quality = %d", quality);
//PRINTF("inBuffSize = %d", inBuffSize);
//PRINTF("outBuffSize = %d", outBuffSize);
#if JPEG_ENCODER_DUMP_INPUT_AND_OUTPUT
char path[50];
snprintf(path, sizeof(path), "/temp/JEI_%d_%d_%dx%d.raw", eInputCount, config, width, height);
PRINTF("rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr");
SkFILEWStream tempFile(path);
if (tempFile.write(inputBuffer, inBuffSize) == false)
PRINTF("Writing to %s failed\n", path);
else
PRINTF("Writing to %s succeeded\n", path);
eInputCount++;
PRINTF("rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr");
#endif
#if OPTIMIZE
if(pOMXHandle == NULL ||
(OMX_U32) width > InPortDef.format.image.nFrameWidth ||
(OMX_U32) height > InPortDef.format.image.nFrameHeight ||
InPortDef.format.image.eColorFormat != SkBitmapToOMXColorFormat(config) ||
(OMX_U32) outBuffSize > OutPortDef.nBufferSize)
{
#endif
if (pOMXHandle != NULL) {
eError = TIOMX_FreeHandle(pOMXHandle);
if ( (eError != OMX_ErrorNone) ) {
PRINTF("Error in Free Handle function\n");
}
} else {
eError = TIOMX_Init();
if ( eError != OMX_ErrorNone ) {
PRINTF("%d :: Error returned by OMX_Init()\n",__LINE__);
goto EXIT;
}
}
/* Load the JPEGEncoder Component */
//PRINTF("Calling OMX_GetHandle\n");
eError = TIOMX_GetHandle(&pOMXHandle, strTIJpegEnc, (void *)this, &JPEGCallBack);
if ( (eError != OMX_ErrorNone) || (pOMXHandle == NULL) ) {
PRINTF ("Error in Get Handle function\n");
iState = STATE_ERROR;
goto EXIT;
}
if(pEncodedOutputBuffer != NULL)
{
free(pEncodedOutputBuffer);
pEncodedOutputBuffer = NULL;
}
outputBuffer = memalign(ALIGN_128_BYTE, outBuffSize);
if ( outputBuffer == NULL) {
PRINTF("\n %s():%d:: ERROR:: outputBuffer Allocation Failed. \n", __FUNCTION__,__LINE__);
iState = STATE_ERROR;
goto EXIT;
}
// reset semaphore if we previously used it
if(iState == STATE_EXIT){
sem_destroy(semaphore);
sem_init(semaphore, 0x00, 0x00);
}
iLastState = STATE_LOADED;
iState = STATE_LOADED;
PortType.nSize = sizeof(OMX_PORT_PARAM_TYPE);
PortType.nVersion.s.nVersionMajor = 0x1;
PortType.nVersion.s.nVersionMinor = 0x0;
PortType.nVersion.s.nRevision = 0x0;
PortType.nVersion.s.nStep = 0x0;
eError = OMX_GetParameter(pOMXHandle, OMX_IndexParamImageInit, &PortType);
if ( eError != OMX_ErrorNone ) {
iState = STATE_ERROR;
goto EXIT;
}
nIndex1 = PortType.nStartPortNumber;
nIndex2 = nIndex1 + 1;
/**********************************************************************/
/* Set the component's OMX_PARAM_PORTDEFINITIONTYPE structure (INPUT) */
/**********************************************************************/
InPortDef.nPortIndex = PortType.nStartPortNumber;
InPortDef.nSize = sizeof(OMX_PARAM_PORTDEFINITIONTYPE);
InPortDef.nVersion.s.nVersionMajor = 0x1;
InPortDef.nVersion.s.nVersionMinor = 0x0;
InPortDef.nVersion.s.nRevision = 0x0;
InPortDef.nVersion.s.nStep = 0x0;
eError = OMX_GetParameter (pOMXHandle, OMX_IndexParamPortDefinition, &InPortDef);
if ( eError != OMX_ErrorNone ) {
eError = OMX_ErrorBadParameter;
iState = STATE_ERROR;
goto EXIT;
}
InPortDef.eDir = OMX_DirInput;
InPortDef.nBufferCountActual =1;
InPortDef.nBufferCountMin = 1;
InPortDef.bEnabled = OMX_TRUE;
InPortDef.bPopulated = OMX_FALSE;
InPortDef.eDomain = OMX_PortDomainImage;
InPortDef.format.image.pNativeRender = NULL;
InPortDef.format.image.nFrameWidth = width;
InPortDef.format.image.nFrameHeight = height;
InPortDef.format.image.nStride = -1;
InPortDef.format.image.nSliceHeight = -1;
InPortDef.format.image.bFlagErrorConcealment = OMX_FALSE;
InPortDef.format.image.eCompressionFormat = OMX_IMAGE_CodingUnused;
InPortDef.nBufferSize = inBuffSize;
InPortDef.format.image.eColorFormat = SkBitmapToOMXColorFormat(config);
if (InPortDef.eDir == nIndex1 ) {
InPortDef.nPortIndex = nIndex1;
}
else {
InPortDef.nPortIndex = nIndex2;
}
//PRINTF("Calling OMX_SetParameter\n");
eError = OMX_SetParameter (pOMXHandle, OMX_IndexParamPortDefinition, &InPortDef);
if ( eError != OMX_ErrorNone ) {
eError = OMX_ErrorBadParameter;
iState = STATE_ERROR;
goto EXIT;
}
/***********************************************************************/
/* Set the component's OMX_PARAM_PORTDEFINITIONTYPE structure (OUTPUT) */
/***********************************************************************/
OutPortDef.nPortIndex = PortType.nStartPortNumber;
OutPortDef.nSize = sizeof(OMX_PARAM_PORTDEFINITIONTYPE);
OutPortDef.nVersion.s.nVersionMajor = 0x1;
OutPortDef.nVersion.s.nVersionMinor = 0x0;
OutPortDef.nVersion.s.nRevision = 0x0;
OutPortDef.nVersion.s.nStep = 0x0;
eError = OMX_GetParameter (pOMXHandle, OMX_IndexParamPortDefinition, &OutPortDef);
if ( eError != OMX_ErrorNone ) {
eError = OMX_ErrorBadParameter;
iState = STATE_ERROR;
goto EXIT;
}
OutPortDef.eDir = OMX_DirOutput;
OutPortDef.nBufferCountActual = 1;
OutPortDef.nBufferCountMin = 1;
OutPortDef.bEnabled = OMX_TRUE;
OutPortDef.bPopulated = OMX_FALSE;
OutPortDef.eDomain = OMX_PortDomainImage;
OutPortDef.format.image.pNativeRender = NULL;
OutPortDef.format.image.nFrameWidth = width;
OutPortDef.format.image.nFrameHeight = height;
OutPortDef.format.image.nStride = -1;
OutPortDef.format.image.nSliceHeight = -1;
OutPortDef.format.image.bFlagErrorConcealment = OMX_FALSE;
OutPortDef.format.image.eCompressionFormat = OMX_IMAGE_CodingJPEG;
OutPortDef.format.image.eColorFormat = OMX_COLOR_FormatCbYCrY;
OutPortDef.nBufferSize = outBuffSize;
if (OutPortDef.eDir == nIndex1 ) {
OutPortDef.nPortIndex = nIndex1;
}
else {
OutPortDef.nPortIndex = nIndex2;
}
eError = OMX_SetParameter (pOMXHandle, OMX_IndexParamPortDefinition, &OutPortDef);
if ( eError != OMX_ErrorNone ) {
eError = OMX_ErrorBadParameter;
iState = STATE_ERROR;
goto EXIT;
}
eError = OMX_UseBuffer(pOMXHandle, &pInBuffHead, InPortDef.nPortIndex, (void *)&nCompId, InPortDef.nBufferSize, (OMX_U8*)inputBuffer);
if ( eError != OMX_ErrorNone ) {
PRINTF ("JPEGEnc test:: %d:error= %x\n", __LINE__, eError);
iState = STATE_ERROR;
goto EXIT;
}
eError = OMX_UseBuffer(pOMXHandle, &pOutBuffHead, OutPortDef.nPortIndex, (void *)&nCompId, OutPortDef.nBufferSize, (OMX_U8*)outputBuffer);
if ( eError != OMX_ErrorNone ) {
PRINTF ("JPEGEnc test:: %d:error= %x\n", __LINE__, eError);
iState = STATE_ERROR;
goto EXIT;
}
#if OPTIMIZE
} else {
reuseHandle = 1;
}
#endif
QfactorType.nSize = sizeof(OMX_IMAGE_PARAM_QFACTORTYPE);
QfactorType.nQFactor = (OMX_U32) quality;
QfactorType.nVersion.s.nVersionMajor = 0x1;
QfactorType.nVersion.s.nVersionMinor = 0x0;
QfactorType.nVersion.s.nRevision = 0x0;
QfactorType.nVersion.s.nStep = 0x0;
QfactorType.nPortIndex = 0x0;
eError = OMX_GetExtensionIndex(pOMXHandle, strQFactor, (OMX_INDEXTYPE*)&nCustomIndex);
if ( eError != OMX_ErrorNone ) {
PRINTF("%d::APP_Error at function call: %x\n", __LINE__, eError);
iState = STATE_ERROR;
goto EXIT;
}
eError = OMX_SetConfig (pOMXHandle, nCustomIndex, &QfactorType);
if ( eError != OMX_ErrorNone ) {
PRINTF("%d::APP_Error at function call: %x\n", __LINE__, eError);
iState = STATE_ERROR;
goto EXIT;
}
eError = OMX_GetExtensionIndex(pOMXHandle, strInputWidth, (OMX_INDEXTYPE*)&nCustomIndex);
if ( eError != OMX_ErrorNone ) {
PRINTF("%d::APP_Error at function call: %x\n", __LINE__, eError);
iState = STATE_ERROR;
goto EXIT;
}
eError = OMX_SetConfig (pOMXHandle, nCustomIndex, &width);
if ( eError != OMX_ErrorNone ) {
PRINTF("%d::APP_Error at function call: %x\n", __LINE__, eError);
iState = STATE_ERROR;
goto EXIT;
}
eError = OMX_GetExtensionIndex(pOMXHandle, strInputHeight, (OMX_INDEXTYPE*)&nCustomIndex);
if ( eError != OMX_ErrorNone ) {
PRINTF("%d::APP_Error at function call: %x\n", __LINE__, eError);
iState = STATE_ERROR;
goto EXIT;
}
eError = OMX_SetConfig (pOMXHandle, nCustomIndex, &height);
if ( eError != OMX_ErrorNone ) {
PRINTF("%d::APP_Error at function call: %x\n", __LINE__, eError);
iState = STATE_ERROR;
goto EXIT;
}
pInBuffHead->nFilledLen = pInBuffHead->nAllocLen;
pInBuffHead->nFlags = OMX_BUFFERFLAG_EOS;
if(!reuseHandle)
{
eError = OMX_SendCommand(pOMXHandle, OMX_CommandStateSet, OMX_StateIdle ,NULL);
if ( eError != OMX_ErrorNone ) {
PRINTF ("Error from SendCommand-Idle(Init) State function\n");
iState = STATE_ERROR;
goto EXIT;
}
}
#if OPTIMIZE
else
{
PRINTF("REUSING HANDLE current state %d", iState);
pInBuffHead->pBuffer = (OMX_U8*)inputBuffer;
pInBuffHead->nFilledLen = inBuffSize;
pInBuffHead->nAllocLen = inBuffSize;
iState = STATE_EXECUTING;
{
int sem_val;
if(sem_getvalue(semaphore, &sem_val) == 0)
PRINTF("line %d: sem value = %d", __LINE__, sem_val);
}
PRINTF("sem_post at %d", __LINE__);
sem_post(semaphore);
}
#endif
Run();
gTIJpegEncMutex.unlock();
PRINTF("Leaving Critical Section 2 \n");
return true;
EXIT:
if(outputBuffer != NULL)
free(outputBuffer);
if (iState == STATE_ERROR) {
sem_post(semaphore);
Run();
}
gTIJpegEncMutex.unlock();
PRINTF("Leaving Critical Section 3 \n");
return false;
}
void SkTIJPEGImageEncoder::PrintState()
{
//PRINTF("\niLastState = %x\n", iLastState);
switch(iState)
{
case STATE_LOADED:
PRINTF("Current State is STATE_LOADED.\n");
break;
case STATE_IDLE:
PRINTF("Current State is STATE_IDLE.\n");
break;
case STATE_EXECUTING:
PRINTF("Current State is STATE_EXECUTING.\n");
break;
case STATE_EMPTY_BUFFER_DONE_CALLED:
PRINTF("Current State is STATE_EMPTY_BUFFER_DONE_CALLED.\n");
break;
case STATE_FILL_BUFFER_DONE_CALLED:
PRINTF("Current State is STATE_FILL_BUFFER_DONE_CALLED.\n");
break;
case STATE_ERROR:
PRINTF("Current State is STATE_ERROR.\n");
break;
case STATE_EXIT:
PRINTF("Current State is STATE_EXIT.\n");
break;
default:
PRINTF("Current State is Invalid.\n");
break;
}
}
void SkTIJPEGImageEncoder::Run()
{
int nRead;
int sem_retval;
OMX_ERRORTYPE eError = OMX_ErrorNone;
struct timespec timeout;
clock_gettime( CLOCK_REALTIME, &timeout );
timeout.tv_sec += 15; // set intial timeout to be arbitrarily high
timeout.tv_nsec += 0;
while(1){
while ((sem_retval = sem_timedwait(semaphore, &timeout)) == -1 && errno == EINTR)
continue; // sem_wait interrupted try again
if(sem_retval == -1)
{
iState = STATE_ERROR;
if(errno == ETIMEDOUT)
SkDebugf("Encoder waiting for semaphore timedout...");
else
SkDebugf("Encoder waiting for semaphore unknown error...");
}
PrintState();
switch(iState)
{
case STATE_IDLE:
if (iLastState == STATE_LOADED)
{
eError = OMX_SendCommand(pOMXHandle,OMX_CommandStateSet, OMX_StateExecuting, NULL);
if ( eError != OMX_ErrorNone ) {
PRINTF("eError from SendCommand-Executing State function\n");
iState = STATE_ERROR;
break;
}
}
else if (iLastState == STATE_EMPTY_BUFFER_DONE_CALLED || iLastState == STATE_FILL_BUFFER_DONE_CALLED)
{
eError = OMX_SendCommand(pOMXHandle,OMX_CommandStateSet, OMX_StateLoaded, NULL);
if ( eError != OMX_ErrorNone ) {
PRINTF("Error from SendCommand-Idle State function\n");
iState = STATE_ERROR;
break;
}
/*
eError = OMX_SendCommand(pOMXHandle, OMX_CommandPortDisable, 0x0, NULL);
if ( eError != OMX_ErrorNone ) {
PRINTF("Error from SendCommand-PortDisable function. Input port.\n");
iState = STATE_ERROR;
break;
}
eError = OMX_SendCommand(pOMXHandle, OMX_CommandPortDisable, 0x1, NULL);
if ( eError != OMX_ErrorNone ) {
PRINTF("Error from SendCommand-PortDisable function. Output port.\n");
iState = STATE_ERROR;
break;
}
*/
/* Free buffers */
eError = OMX_FreeBuffer(pOMXHandle, InPortDef.nPortIndex, pInBuffHead);
if ( eError != OMX_ErrorNone ) {
PRINTF("Error from OMX_FreeBuffer. Input port.\n");
iState = STATE_ERROR;
break;
}
eError = OMX_FreeBuffer(pOMXHandle, OutPortDef.nPortIndex, pOutBuffHead);
if ( eError != OMX_ErrorNone ) {
PRINTF("Error from OMX_FreeBuffer. Output port.\n");
iState = STATE_ERROR;
break;
}
}
else
{
iState = STATE_ERROR;
}
clock_gettime( CLOCK_REALTIME, &timeout );
timeout.tv_sec += 15;
break;
case STATE_EXECUTING:
OMX_EmptyThisBuffer(pOMXHandle, pInBuffHead);
OMX_FillThisBuffer(pOMXHandle, pOutBuffHead);
clock_gettime( CLOCK_REALTIME, &timeout );
timeout.tv_sec += 7;
break;
case STATE_EMPTY_BUFFER_DONE_CALLED:
#if OPTIMIZE
iState = STATE_EXIT;
#else
eError = OMX_SendCommand(pOMXHandle,OMX_CommandStateSet, OMX_StateIdle, NULL);
if ( eError != OMX_ErrorNone ) {
PRINTF("Error from SendCommand-Idle(nStop) State function\n");
iState = STATE_ERROR;
}
#endif
clock_gettime( CLOCK_REALTIME, &timeout );
timeout.tv_sec += 15;
break;
case STATE_LOADED:
case STATE_ERROR:
/*### Assume that the Bitmap object/file need not be closed. */
/*### More needs to be done here */
/*### Do different things based on iLastState */
if ( iState == STATE_ERROR ) {
if (pInBuffHead != NULL) {
/* Free buffers if it got allocated */
eError = OMX_FreeBuffer(pOMXHandle, InPortDef.nPortIndex, pInBuffHead);
if ( eError != OMX_ErrorNone ) {
PRINTF("Error from OMX_FreeBuffer. Input port.\n");
}
}
if (pOutBuffHead != NULL) {
eError = OMX_FreeBuffer(pOMXHandle, OutPortDef.nPortIndex, pOutBuffHead);
if ( eError != OMX_ErrorNone ) {
PRINTF("Error from OMX_FreeBuffer. Output port.\n");
}
}
}
if (pOMXHandle) {
eError = TIOMX_FreeHandle(pOMXHandle);
if ( (eError != OMX_ErrorNone) ) {
PRINTF("Error in Free Handle function\n");
}
pOMXHandle = NULL;
}
eError = TIOMX_Deinit();
if ( eError != OMX_ErrorNone ) {
PRINTF("Error returned by TIOMX_Deinit()\n");
}
iState = STATE_EXIT;
sem_post(semaphore);
break;
default:
break;
}
if (iState == STATE_EXIT) break;
}
}