-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathSkImageDecoder_libtijpeg.cpp
1438 lines (1229 loc) · 47.9 KB
/
SkImageDecoder_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
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) 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 SkImageDecoder_libtijpeg.cpp
*
* This file implements SkTIJPEGImageDecoder
*
*/
#include "SkImageDecoder_libtijpeg.h"
#ifdef DEBUG_LOG
# define PRINTF SkDebugf
#else
# define PRINTF
#endif
#define TIME_DECODE
#define JPEG_DECODER_DUMP_INPUT_AND_OUTPUT 0 // set directory persmissions for /temp as 777
#if JPEG_DECODER_DUMP_INPUT_AND_OUTPUT
int dOutputCount = 0;
int dInputCount = 0;
#endif
//////////////////////////////////////////////////////////////////////////
#include "SkTime.h"
class AutoTimeMillis {
public:
AutoTimeMillis(const char label[]) : fLabel(label) {
if (!fLabel) {
fLabel = "";
}
fNow = SkTime::GetMSecs();
width = 0;
height = 0;
}
~AutoTimeMillis() {
SkDebugf("---- Input file Resolution :%dx%d",width,height);
SkDebugf("---- TI JPEG Decode Time (ms): %s %d\n", fLabel, SkTime::GetMSecs() - fNow);
}
void setResolution(int width, int height){
this->width=width;
this->height=height;
}
private:
const char* fLabel;
SkMSec fNow;
int width;
int height;
};
typedef struct OMX_CUSTOM_RESOLUTION
{
OMX_U32 nWidth;
OMX_U32 nHeight;
} OMX_CUSTOM_RESOLUTION;
typedef struct OMX_CUSTOM_IMAGE_DECODE_SUBREGION
{
OMX_U32 nSize;
OMX_VERSIONTYPE nVersion;
OMX_U32 nXOrg; /*Sectional decoding: X origin*/
OMX_U32 nYOrg; /*Sectional decoding: Y origin*/
OMX_U32 nXLength; /*Sectional decoding: X length*/
OMX_U32 nYLength; /*Sectional decoding: Y length*/
}OMX_CUSTOM_IMAGE_DECODE_SUBREGION;
void* ThreadDecoderWrapper(void* me)
{
SkTIJPEGImageDecoder *decoder = static_cast<SkTIJPEGImageDecoder *>(me);
decoder->Run();
return NULL;
}
OMX_ERRORTYPE OMX_EventHandler(OMX_HANDLETYPE hComponent,
OMX_PTR pAppData,
OMX_EVENTTYPE eEvent,
OMX_U32 nData1,
OMX_U32 nData2,
OMX_PTR pEventData)
{
((SkTIJPEGImageDecoder *)pAppData)->EventHandler(hComponent, eEvent, nData1, nData2, pEventData);
return OMX_ErrorNone;
}
OMX_ERRORTYPE OMX_EmptyBufferDone(OMX_HANDLETYPE hComponent, OMX_PTR ptr, OMX_BUFFERHEADERTYPE* pBuffer)
{
SkTIJPEGImageDecoder * ImgDec = (SkTIJPEGImageDecoder *)ptr;
ImgDec->iLastState = ImgDec->iState;
ImgDec->iState = SkTIJPEGImageDecoder::STATE_EMPTY_BUFFER_DONE_CALLED;
sem_post(ImgDec->semaphore) ;
return OMX_ErrorNone;
}
OMX_ERRORTYPE OMX_FillBufferDone (OMX_HANDLETYPE hComponent, OMX_PTR ptr, OMX_BUFFERHEADERTYPE* pBuffHead)
{
//PRINTF("\nOMX_FillBufferDone:: nFilledLen = %ld \n", pBuffHead->nFilledLen);
((SkTIJPEGImageDecoder *)ptr)->FillBufferDone(pBuffHead->pBuffer, pBuffHead->nFilledLen);
return OMX_ErrorNone;
}
SkTIJPEGImageDecoder::~SkTIJPEGImageDecoder()
{
OMX_ERRORTYPE eError = OMX_ErrorNone;
#if OPTIMIZE
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) ;
}
else {
inStateTransition = true;
}
Run();
}else{
iState = STATE_EXIT;
sem_post(semaphore);
}
}
#endif
gTIJpegDecMutex.lock();
sem_destroy(semaphore);
if (semaphore != NULL) {
free(semaphore) ;
semaphore = NULL;
}
gTIJpegDecMutex.unlock();
}
SkTIJPEGImageDecoder::SkTIJPEGImageDecoder()
{
mLoad = 0;
mDeleteAttempts = 0;
pInBuffHead = NULL;
pOutBuffHead = NULL;
pOMXHandle = NULL;
mProgressive = 0;
semaphore = NULL;
nSubRegDecode = false;
inStateTransition = false;
//Initialize JpegDecoderParams structure
memset((void*)&jpegDecParams, 0, sizeof(JpegDecoderParams));
semaphore = (sem_t*)malloc(sizeof(sem_t)) ;
sem_init(semaphore, 0x00, 0x00);
}
OMX_S16 SkTIJPEGImageDecoder::Get16m(const void * Short)
{
return(((OMX_U8 *)Short)[0] << 8) | ((OMX_U8 *)Short)[1];
}
void SkTIJPEGImageDecoder::FixFrameSize(JPEG_HEADER_INFO* JpegHeaderInfo)
{
OMX_U32 nWidth=JpegHeaderInfo->nWidth, nHeight=JpegHeaderInfo->nHeight;
/*round up if nWidth is not multiple of 32*/
( (nWidth%32 ) !=0 ) ? nWidth=32 * ( ( nWidth/32 ) + 1 ) : nWidth;
//PRINTF("new nWidth %d \n", nWidth);
/*round up if nHeight is not multiple of 16*/
( (nHeight%16) !=0 ) ? nHeight=16 * ( ( nHeight/16 ) + 1 ) : nHeight;
//PRINTF("new nHeight %d \n", nHeight);
JpegHeaderInfo->nWidth = nWidth;
JpegHeaderInfo->nHeight = nHeight;
}
OMX_S16 SkTIJPEGImageDecoder::GetYUVformat(OMX_U8 * Data)
{
unsigned char Nf;
OMX_U32 j;
OMX_U32 temp_index;
OMX_U32 temp;
OMX_U32 image_format;
short H[4],V[4];
Nf = Data[7];
for (j = 0; j < Nf; j++)
{
temp_index = j * 3 + 7 + 2;
/*---------------------------------------------------------*/
/* H[j]: upper 4 bits of a byte, horizontal sampling fator. */
/* V[j]: lower 4 bits of a byte, vertical sampling factor. */
/*---------------------------------------------------------*/
H[j] = (0x0f & (Data[temp_index] >> 4));
V[j] = (0x0f & Data[temp_index]);
}
/*------------------------------------------------------------------*/
/* Set grayscale flag, namely if image is gray then set it to 1, */
/* else set it to 0. */
/*------------------------------------------------------------------*/
image_format = -1;
if (Nf == 1){
image_format = OMX_COLOR_FormatL8;
}
if (Nf == 3)
{
temp = (V[0]*H[0])/(V[1]*H[1]) ;
if (temp == 4 && H[0] == 2)
image_format = OMX_COLOR_FormatYUV420Planar;
if (temp == 4 && H[0] == 4)
image_format = OMX_COLOR_FormatYUV411Planar;
if (temp == 2)
image_format = OMX_COLOR_FormatCbYCrY; /* YUV422 interleaved, little endian */
if (temp == 1)
image_format = OMX_COLOR_FormatYUV444Interleaved;
}
return (image_format);
}
OMX_S32 SkTIJPEGImageDecoder::ParseJpegHeader (SkStream* stream, JPEG_HEADER_INFO* JpgHdrInfo)
{
OMX_U8 a = 0;
OMX_S32 lSize = 0;
lSize = stream->getLength();
stream->rewind();
JpgHdrInfo->nProgressive = 0; /*Default value is non progressive*/
JpgHdrInfo->SOF0_Nf = 0;
JpgHdrInfo->SOS_Ns = 0;
a = stream->readU8();
if ( a != 0xff || stream->readU8() != M_SOI ) {
return 0;
}
for ( ;; ) {
OMX_U32 itemlen = 0;
OMX_U32 marker = 0;
OMX_U32 ll = 0,lh = 0, got = 0, err = 0;
OMX_U8 *data = NULL;
for ( a=0;a<15 /* 7 originally */;a++ ) {
marker = stream->readU8();
//PRINTF("MARKER IS %x\n",marker);
if ( marker != 0xff )
break;
}
if ( marker == 0xff ) {
/* 0xff is legal padding, but if we get that many, something's wrong.*/
PRINTF("too many padding bytes!");
return 0;
}
/* Read the length of the section.*/
lh = stream->readU8();
ll = stream->readU8();
itemlen = (lh << 8) | ll;
if ( itemlen < 2 ) {
PRINTF("invalid marker");
return 0;
}
data = (OMX_U8 *)malloc(itemlen);
if ( data == NULL ) {
PRINTF("Could not allocate memory");
break;
}
/* Store first two pre-read bytes. */
data[0] = (OMX_U8)lh;
data[1] = (OMX_U8)ll;
got = stream->read(data+2, itemlen-2); /* Read the whole section.*/
if ( got != itemlen-2 ) {
PRINTF("Premature end of file?");
if ( data != NULL ) {
free(data);
data=NULL;
}
return 0;
}
//PRINTF("Jpeg section marker 0x%02x size %d\n",marker, itemlen);
err = JpegHeader_GetMarkerInfo(marker, data, JpgHdrInfo);
if ( data != NULL ) {
free(data);
data=NULL;
}
if(err == M_SOS)
return lSize;
else if(err == M_EOI)
return 0;
}
return 0;
}
OMX_S32 SkTIJPEGImageDecoder::ParseJpegHeader (OMX_U8* JpgBuffer, OMX_S32 lSize, JPEG_HEADER_INFO* JpgHdrInfo)
{
OMX_U8 a = 0;
OMX_U32 pos = 0;
JpgHdrInfo->nProgressive = 0; /*Default value is non progressive*/
JpgHdrInfo->SOF0_Nf = 0;
JpgHdrInfo->SOS_Ns = 0;
a = JpgBuffer[pos++];
if ( a != 0xff || JpgBuffer[pos++] != M_SOI ) {
return 0;
}
for ( ;; )
{
OMX_U32 itemlen = 0;
OMX_U32 marker = 0;
OMX_U32 ll = 0,lh = 0, err = 0;
OMX_U8 *data = NULL;
for ( a=0;a<15 /* 7 originally */;a++ ) {
marker = JpgBuffer[pos++];
//PRINTF("MARKER IS %x\n",marker);
if ( marker != 0xff )
break;
}
if ( marker == 0xff ) {
/* 0xff is legal padding, but if we get that many, something's wrong.*/
PRINTF("too many padding bytes!");
return 0;
}
/* Read the length of the section.*/
data = &JpgBuffer[pos];
lh = data[0];
ll = data[1];
itemlen = (lh << 8) | ll;
if ( itemlen < 2 ) {
PRINTF("invalid marker");
return 0;
}
pos += itemlen; /* Move position by the whole section.*/
//PRINTF("Jpeg section marker 0x%02x size %d\n",marker, itemlen);
err = JpegHeader_GetMarkerInfo(marker, data, JpgHdrInfo);
if(err == M_SOS)
return lSize;
else if(err == M_EOI)
return 0;
}
return 0;
}
OMX_U32 SkTIJPEGImageDecoder::JpegHeader_GetMarkerInfo (OMX_U32 Marker, OMX_U8* MarkerData, JPEG_HEADER_INFO* JpgHdrInfo)
{
switch ( Marker )
{
case M_SOS:
JpgHdrInfo->SOS_Ns = *(OMX_U8 *)(MarkerData+2);
if (( JpgHdrInfo->SOF0_Nf != 0 )
&& ( JpgHdrInfo->SOS_Ns != JpgHdrInfo->SOF0_Nf )) {
JpgHdrInfo->nProgressive = 1;
}
return M_SOS;
case M_EOI:
PRINTF("No image in jpeg!\n");
return M_EOI;
case M_COM: /* Comment section */
case M_JFIF:
case M_EXIF:
break;
case M_SOF2:
PRINTF("nProgressive IMAGE!\n");
JpgHdrInfo->nProgressive=1;
case M_SOF0:
if ( Marker == M_SOF0 ) {
JpgHdrInfo->SOF0_Nf = *(OMX_U8 *)(MarkerData+7);
}
case M_SOF1:
case M_SOF3:
case M_SOF5:
case M_SOF6:
case M_SOF7:
case M_SOF9:
case M_SOF10:
case M_SOF11:
case M_SOF13:
case M_SOF14:
case M_SOF15:
JpgHdrInfo->nHeight = Get16m(MarkerData+3);
JpgHdrInfo->nWidth = Get16m(MarkerData+5);
JpgHdrInfo->nFormat = GetYUVformat(MarkerData);
switch (JpgHdrInfo->nFormat) {
case OMX_COLOR_FormatYUV420Planar:
PRINTF("Image chroma format is OMX_COLOR_FormatYUV420Planar\n");
break;
case OMX_COLOR_FormatYUV411Planar:
PRINTF("Image chroma format is OMX_COLOR_FormatYUV411Planar\n");
break;
case OMX_COLOR_FormatCbYCrY:
PRINTF("Image chroma format is OMX_COLOR_FormatYUV422Interleaved\n");
break;
case OMX_COLOR_FormatYUV444Interleaved:
PRINTF("Image chroma format is OMX_COLOR_FormatYUV444Interleaved\n");
break;
case OMX_COLOR_FormatL8:
PRINTF("Image chroma format is OMX_COLOR_FormatL8 \n");
break;
default:
PRINTF("Cannot find Image chroma format \n");
JpgHdrInfo->nFormat = OMX_COLOR_FormatUnused;
break;
}
PRINTF("Image Width x Height = %u * %u\n", Get16m(MarkerData+5), Get16m(MarkerData+3) );
/*
PRINTF("JPEG image is %uw * %uh,\n", Get16m(Data+3), Get16m(Data+5) );
if ( *(Data+9)==0x41 ) {
PRINTF("THIS IS A YUV 411 ENCODED IMAGE \n");
JpgHdrInfo->format= 1;
}
*/
break;
default:
/* Skip any other sections.*/
break;
}
return 0;
}
void SkTIJPEGImageDecoder::FillBufferDone(OMX_U8* pBuffer, OMX_U32 nFilledLen)
{
char strOutResolution[] = "OMX.TI.JPEG.decoder.Param.OutputResolution";
OMX_INDEXTYPE nCustomIndex = OMX_IndexMax;
OMX_CUSTOM_RESOLUTION OutputResolution;
OMX_ERRORTYPE eError = OMX_ErrorNone;
// algo might have changed our desired output width and height
// check here and update bitmap accordingly
eError = OMX_GetExtensionIndex(pOMXHandle, strOutResolution, (OMX_INDEXTYPE*)&nCustomIndex);
if ( eError != OMX_ErrorNone ) {
PRINTF ("Error getting output resolution %d:error= %x\n", __LINE__, eError);
} else {
eError = OMX_GetConfig (pOMXHandle, nCustomIndex, &OutputResolution);
if ( eError != OMX_ErrorNone ) {
PRINTF ("Error getting output resolution %d:error= %x\n", __LINE__, eError);
} else{
PRINTF("output resolution: %dx%d", OutputResolution.nWidth, OutputResolution.nHeight);
bitmap->setConfig(bitmap->config(), OutputResolution.nWidth, OutputResolution.nHeight);
}
}
bitmap->setPixelRef(new SkMallocPixelRef(pBuffer, nFilledLen, NULL))->unref();
bitmap->lockPixels();
#if JPEG_DECODER_DUMP_INPUT_AND_OUTPUT
char path[50];
snprintf(path, sizeof(path), "/temp/JDO_%d_%d_%dx%d.raw", dOutputCount, bitmap->config(), bitmap->width(), bitmap->height());
PRINTF("rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr");
SkFILEWStream tempFile(path);
if (tempFile.write(pBuffer, nFilledLen) == false)
PRINTF("Writing to %s failed\n", path);
else
PRINTF("Writing to %s succeeded\n", path);
dOutputCount++;
PRINTF("rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr");
#endif
iLastState = iState;
iState = STATE_FILL_BUFFER_DONE_CALLED;
}
void SkTIJPEGImageDecoder::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");
inStateTransition = false;
iLastState = iState;
iState = STATE_IDLE;
sem_post(semaphore) ;
}
else if ((nData1 == OMX_CommandStateSet) && (nData2 == OMX_StateExecuting))
{
PRINTF ("Component State Changed To OMX_StateExecuting\n");
inStateTransition = false;
iLastState = iState;
iState = STATE_EXECUTING;
sem_post(semaphore) ;
}
else if ((nData1 == OMX_CommandStateSet) && (nData2 == OMX_StateLoaded))
{
PRINTF ("Component State Changed To OMX_StateLoaded\n");
inStateTransition = false;
iLastState = iState;
iState = STATE_LOADED;
sem_post(semaphore) ;
}
else if ((nData1 == OMX_CommandStateSet) && (nData2 == OMX_StateInvalid))
{
PRINTF ("Component State Changed To OMX_StateInvalid\n");
inStateTransition = false;
iState = STATE_INVALID;
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, ThreadDecoderWrapper, 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;
if (iLastState == STATE_EXIT) {
OMX_SendCommand(hComponent, OMX_CommandStateSet, OMX_StateInvalid, NULL);
inStateTransition = true;
//call sem_post(semaphore) at OMX_StateInvalid state set event.
}
else {
sem_post(semaphore);
}
}else{
PRINTF ("Libskiahw(decoder) already handling Error!!!");
}
break;
default:
break;
}
}
OMX_S32 SkTIJPEGImageDecoder::fill_data(OMX_U8* pBuf, SkStream* stream, OMX_S32 bufferSize)
{
OMX_U32 nFilledLen = stream->read(pBuf, bufferSize);
PRINTF ("Populated Input Buffer: nAllocLen = %d, nFilledLen =%ld\n", bufferSize, nFilledLen);
#if JPEG_DECODER_DUMP_INPUT_AND_OUTPUT
char path[50];
snprintf(path, sizeof(path), "/temp/JDI_%d.jpg", dInputCount);
PRINTF("rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr");
SkFILEWStream tempFile(path);
if (tempFile.write(pBuf, nFilledLen) == false)
PRINTF("Writing to %s failed\n", path);
else
PRINTF("Writing to %s succeeded\n", path);
dInputCount++;
PRINTF("rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr");
#endif
return nFilledLen;
}
bool SkTIJPEGImageDecoder::onDecode(SkImageDecoder* dec_impl, SkStream* stream, SkBitmap* bm, SkBitmap::Config prefConfig, SkImageDecoder::Mode mode)
{
gTIJpegDecMutex.lock();
/* Critical section */
PRINTF("Entering Critical Section \n");
AutoTrackLoad autotrack(mLoad);
#ifdef TIME_DECODE
AutoTimeMicros atm("TI JPEG Decode");
#endif
int reuseHandle = 0;
int nRetval;
int nIndex1;
int nIndex2;
int scaleFactor;
int bitsPerPixel;
int nRead = 0;
int nOutWidth, nInWidth;
int nOutHeight, nInHeight;
OMX_S32 inputFileSize;
OMX_S32 nCompId = 100;
OMX_U32 outBuffSize;
OMX_U32 tempSize;
OMX_ERRORTYPE eError = OMX_ErrorNone;
JPEG_HEADER_INFO JpegHeaderInfo;
OMX_PORT_PARAM_TYPE PortType;
OMX_CUSTOM_RESOLUTION MaxResolution;
OMX_CONFIG_SCALEFACTORTYPE ScaleFactor;
OMX_CUSTOM_IMAGE_DECODE_SUBREGION SubRegionDecode;
OMX_INDEXTYPE nCustomIndex = OMX_IndexMax;
void* inputBuffer = NULL;
void* outputBuffer = NULL;
char strTIJpegDec[] = "OMX.TI.JPEG.decoder";
char strColorFormat[] = "OMX.TI.JPEG.decoder.Config.OutputColorFormat";
char strProgressive[] = "OMX.TI.JPEG.decoder.Config.ProgressiveFactor";
char strMaxResolution[] = "OMX.TI.JPEG.decoder.Param.SetMaxResolution";
char strSubRegionDecode[] = "OMX.TI.JPEG.decoder.Param.SubRegionDecode";
OMX_CALLBACKTYPE JPEGCallBack ={OMX_EventHandler, OMX_EmptyBufferDone, OMX_FillBufferDone};
PRINTF("\nUsing TI Image Decoder.\n");
bitmap = bm;
inStream = stream;
SkBitmap::Config config = prefConfig;
scaleFactor = dec_impl->getSampleSize();
PRINTF("config = %d\n", config);
PRINTF("mode = %d\n", mode);
PRINTF("scaleFactor = %d ", scaleFactor);
//PRINTF("File size = %d\n", stream->getLength());
if (scaleFactor == 3)
scaleFactor = 2;
else if ((scaleFactor > 4) && (scaleFactor < 8))
scaleFactor = 4;
else if (scaleFactor > 8)
scaleFactor = 8;
PRINTF ("Modified scaleFactor = %d\n", scaleFactor);
if (SkImageDecoder::kDecodeBounds_Mode == mode) {
inputFileSize = ParseJpegHeader(stream , &JpegHeaderInfo);
}else{
inputFileSize = stream->getLength();
inputFileSize = (OMX_U32)((inputFileSize + ALIGN_128_BYTE - 1) & ~(ALIGN_128_BYTE - 1));
inputBuffer = memalign(ALIGN_128_BYTE, inputFileSize);
if (inputBuffer == NULL) {
PRINTF("%s():%d::ERROR!!!: Could not allocate memory for inputBuffer.\n",__FUNCTION__,__LINE__);
goto EXIT;
}
stream->rewind();
nRead = fill_data((OMX_U8*)inputBuffer, stream, stream->getLength());
inputFileSize = ParseJpegHeader((OMX_U8*)inputBuffer, inputFileSize, &JpegHeaderInfo);
}
#ifdef TIME_DECODE
atm.setResolution(JpegHeaderInfo.nWidth , JpegHeaderInfo.nHeight);
atm.setScaleFactor(scaleFactor);
#endif
if (inputFileSize == 0) {
PRINTF("The file size is 0. Maybe the format of the file is not correct\n");
goto EXIT;
}
// if no user preference, see what the device recommends
if (config == SkBitmap::kNo_Config)
config = SkImageDecoder::GetDeviceConfig();
//Configure the bitmap with the required W x H
if (jpegDecParams.nXOrg || jpegDecParams.nYOrg ||
jpegDecParams.nXLength || jpegDecParams.nYLength ) {
PRINTF ("\njpegDecParams.nXOrg = %d\n",jpegDecParams.nXOrg);
PRINTF ("jpegDecParams.nYOrg = %d\n",jpegDecParams.nYOrg);
PRINTF ("jpegDecParams.nXLength = %d\n",jpegDecParams.nXLength);
PRINTF ("jpegDecParams.nYLength = %d\n",jpegDecParams.nYLength);
nOutWidth = jpegDecParams.nXLength;
nOutHeight = jpegDecParams.nYLength;
//set the subregion decode flag So that the current decode will create a new OMX handle.
//This is required because the Subregion Decode parameters can't be changed in Execute
//state of the OMX component.
nSubRegDecode = true;
}
else {
nOutWidth = JpegHeaderInfo.nWidth;
nOutHeight = JpegHeaderInfo.nHeight;
}
switch(scaleFactor){
case(8):
//For scale factor 8, instead of 12.5% use 13%.
nOutWidth = nOutWidth * 13 / 100;
nOutHeight = nOutHeight * 13 / 100;
break;
default:
nOutWidth = floor(nOutWidth+scaleFactor-1)/scaleFactor;
nOutHeight = floor(nOutHeight+scaleFactor-1)/scaleFactor;
break;
}
nInWidth = JpegHeaderInfo.nWidth;
nInHeight = JpegHeaderInfo.nHeight;
/*check for the maximum resolution and set to its upper limit which is supported by the codec.
So that TI DSP Codec will take care of scaling down before decoding and updates the
outputresolution parameter in UALGOutParams structure.
*/
if(JpegHeaderInfo.nProgressive &&
(nOutWidth * nOutHeight > JPEGDEC_PROG_MAXRESOLUTION)) //progressive
{
if(nOutWidth > nOutHeight)
{
nOutWidth = nInWidth = PROG_WIDTH;
nOutHeight = nInHeight = PROG_HEIGHT;
}else
{
nOutWidth = nInWidth = PROG_HEIGHT;
nOutHeight = nInHeight = PROG_WIDTH;
}
}
else if(!JpegHeaderInfo.nProgressive &&
(nOutWidth * nOutHeight > JPEGDEC_BASELINE_MAXRESOLUTION)) //sequential image
{
if(nOutWidth > nOutHeight)
{
nOutWidth = nInWidth = SEQ_WIDTH;
nOutHeight = nInHeight = SEQ_HEIGHT;
}else
{
nOutWidth = nInWidth = SEQ_HEIGHT;
nOutHeight = nInHeight = SEQ_WIDTH;
}
}
bm->setConfig(config, nOutWidth, nOutHeight);
if (bm->config() == SkBitmap::kARGB_8888_Config)
bm->setIsOpaque(false);
else
bm->setIsOpaque(true);
PRINTF("bm->width() = %d\n", bm->width());
PRINTF("bm->height() = %d\n", bm->height());
PRINTF("bm->config() = %d\n", bm->config());
PRINTF("bm->getSize() = %d\n", bm->getSize());
PRINTF("InPortDef.format.image.nFrameWidth = %d\n", InPortDef.format.image.nFrameWidth);
PRINTF("InPortDef.format.image.nFrameHeight = %d\n", InPortDef.format.image.nFrameHeight);
if (SkImageDecoder::kDecodeBounds_Mode == mode) {
if(inputBuffer != NULL)
free(inputBuffer);
gTIJpegDecMutex.unlock();
PRINTF("Jpeg Header Parsing Done.\n");
PRINTF("Leaving Critical Section 1 \n");
return true;
}
FixFrameSize(&JpegHeaderInfo);
if (config == SkBitmap::kA8_Config) { /* 8-bits per pixel, with only alpha specified (0 is transparent, 0xFF is opaque) */
bitsPerPixel = 1;
}
else if (config == SkBitmap::kRGB_565_Config) { /* 16-bits per pixel*/
bitsPerPixel = 2;
}
else if (config == SkBitmap::kARGB_8888_Config) { /* 32-bits per pixel */
bitsPerPixel = 4;
}
else { /*Set DEFAULT color format*/
bitsPerPixel = 2;
}
outBuffSize = (nOutWidth * nOutHeight) * bitsPerPixel ;
#if OPTIMIZE
if(pOMXHandle == NULL ||
nSubRegDecode == true ||
(OMX_U32) nInWidth > InPortDef.format.image.nFrameWidth ||
(OMX_U32) nInHeight > InPortDef.format.image.nFrameHeight ||
OutPortDef.format.image.eColorFormat != SkBitmapToOMXColorFormat(bm->config()) ||
mProgressive != JpegHeaderInfo.nProgressive ||
InPortDef.format.image.eColorFormat != JPEGToOMXColorFormat(JpegHeaderInfo.nFormat) ||
outBuffSize > OutPortDef.nBufferSize ||
(OMX_U32) inputFileSize > InPortDef.nBufferSize)
{
//reset the subregion decode flag for next decode to use the current OMX handle
nSubRegDecode = false;
#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 JPEGDecoder Component */
//PRINTF("Calling OMX_GetHandle\n");
eError = TIOMX_GetHandle(&pOMXHandle, strTIJpegDec, (void *)this, &JPEGCallBack);
if ( (eError != OMX_ErrorNone) || (pOMXHandle == NULL) ) {
PRINTF ("Error in Get Handle function\n");
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 = nInWidth;
InPortDef.format.image.nFrameHeight = nInHeight;
InPortDef.format.image.nStride = -1;
InPortDef.format.image.nSliceHeight = -1;
InPortDef.format.image.bFlagErrorConcealment = OMX_FALSE;
InPortDef.format.image.eCompressionFormat = OMX_IMAGE_CodingJPEG;
InPortDef.nBufferSize = inputFileSize;
if (InPortDef.eDir == nIndex1 ) {
InPortDef.nPortIndex = nIndex1;
}
else {
InPortDef.nPortIndex = nIndex2;
}
InPortDef.format.image.eColorFormat = JPEGToOMXColorFormat(JpegHeaderInfo.nFormat);
//PRINTF("Calling OMX_SetParameter for Input Port\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 = JpegHeaderInfo.nWidth;
OutPortDef.format.image.nFrameHeight = JpegHeaderInfo.nHeight;
OutPortDef.format.image.nStride = -1;
OutPortDef.format.image.nSliceHeight = -1;
OutPortDef.format.image.bFlagErrorConcealment = OMX_FALSE;
OutPortDef.format.image.eCompressionFormat = OMX_IMAGE_CodingUnused;
OutPortDef.format.image.eColorFormat = SkBitmapToOMXColorFormat(config);
if (OutPortDef.eDir == nIndex1 ) {
OutPortDef.nPortIndex = nIndex1;
}
else {
OutPortDef.nPortIndex = nIndex2;
}
OutPortDef.nBufferSize = outBuffSize;