language: C (gcc-4.7.2)
date: 560 days 10 hours ago
link:
visibility: public
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
#include "Decoder_OMX.h"
 
#include "OMX_Core.h"
#include "OMX_Types.h"
#include "OMX_Other.h"
#include "OMX_Component.h"
#include <stdlib.h>
#include "logjam.h"
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <unistd.h>
#include <time.h>
#include <unistd.h>
#include "libswscale/swscale.h"
 
#include "TCPClient.h"
 
#define OMX_FROM_FILE 0
#define OMX_FROM_NET 1
 
int g_omx_input_type = OMX_FROM_FILE;
 
FILE* g_pVideoFile_OMX = NULL;
 
 
int g_nums[100];
 
int       g_iCompressedDataSize = 1024*1024*30;
char* g_pCompressedData = NULL;
char* g_pCompressedData_Travel = NULL;
 
 
//int iRet = TCP_ReceiveRaw_VideoConnection(buf,buf_size);
 
#define         OMX_MAX_STRINGNAME_SIZE   128  // why isn't this defined in OMX_Core.h ???
/*static OMX_BUFFERHEADERTYPE **omx_buffers_out;
 static OMX_BUFFERHEADERTYPE **omx_buffers_in;*/
 
//static int buffer_out_mask;//, buffer_out_nb;
 
static int locked_output[100];
static int ready_output[100];
 
//static int locked_input[100];
static int locked_input[100];
static int ready_input[100];
 
//static int buffer_in_mask;//, buffer_in_nb, buffer_in_size;
 
char current_frame[(640*480*3)/2];
struct SwsContext               *g_scale_context = NULL;
 
typedef OMX_ERRORTYPE OMX_APIENTRY(*OMX_Init_FPTR)
(void);
OMX_Init_FPTR my_OMX_Init = NULL;
 
typedef OMX_ERRORTYPE OMX_APIENTRY(*OMX_ComponentNameEnum_FPTR)
(
                OMX_OUT OMX_STRING cComponentName,
                OMX_IN OMX_U32 nNameLength,
                OMX_IN OMX_U32 nIndex);
OMX_ComponentNameEnum_FPTR my_OMX_ComponentNameEnum = NULL;
 
typedef OMX_ERRORTYPE (*OMX_GetRolesOfComponent_FPTR)(OMX_IN OMX_STRING compName, OMX_INOUT OMX_U32 *pNumRoles, OMX_OUT OMX_U8 **roles);
OMX_GetRolesOfComponent_FPTR my_OMX_GetRolesOfComponent = NULL;
 
typedef OMX_ERRORTYPE (*OMX_GetHandle_FPTR)(
                OMX_OUT OMX_HANDLETYPE* pHandle,
                OMX_IN OMX_STRING cComponentName,
                OMX_IN OMX_PTR pAppData,
                OMX_IN OMX_CALLBACKTYPE* pCallBacks);
OMX_GetHandle_FPTR my_OMX_GetHandle = NULL;
 
OMX_HANDLETYPE decoder_handle = 0;
//OMX_HANDLETYPE vrenderer = 0;
int input_port = -1;
int output_port = -1;
int input_buffer_count = -1;
int input_buffer_size = -1;
 
// this is hardcoded !!!
// remember to make this dynamic
#define PICTURE_BUFFERS_NUM_INPUT 10
#define PICTURE_BUFFERS_NUM_OUTPUT 4
 
OMX_BUFFERHEADERTYPE* g_BuffersIn[PICTURE_BUFFERS_NUM_INPUT];
OMX_BUFFERHEADERTYPE* g_BuffersOut[PICTURE_BUFFERS_NUM_OUTPUT];
 
 
/*void PrintBuffer1_Out_Info()
{
        void* temp = g_BuffersOut[1]->pAppPrivate;
        LOGD("buff=0x%08X,priv=0x%08X, priv_val=%d", g_BuffersOut[1],g_BuffersOut[1]->pAppPrivate,  *((int*)temp));
}*/
 
int g_iCurrentInput = 0;
int g_iCurrentOutput = 0;
 
//FILE* g_pVidFile = NULL;
 
/*********************/
// Callbacks
/*********************/
 
void Print_Event_Type(OMX_EVENTTYPE event) {
        //LOGI("blah=%d",OMX_EventPortSettingsChanged);
        switch (event) {
        case OMX_EventCmdComplete:
                LOGI("event=OMX_EventCmdComplete");
                break;
 
        case OMX_EventError:
                LOGI("event=OMX_EventError");
                break;
        case OMX_EventMark:
                LOGI("event=OMX_EventMark");
                break;
        case OMX_EventPortSettingsChanged:
                LOGI("Oevent=MX_EventPortSettingsChanged");
                break;
        case OMX_EventBufferFlag:
                LOGI("event=OMX_EventBufferFlag");
                break;
        case OMX_EventResourcesAcquired:
                LOGI("event=OMX_EventResourcesAcquired");
                break;
        case OMX_EventComponentResumed:
                LOGI("event=OMX_EventComponentResumed");
                break;
        case OMX_EventDynamicResourcesAvailable:
                LOGI("event=OMX_EventDynamicResourcesAvailable");
                break;
        case OMX_EventPortFormatDetected:
                LOGI("event=OMX_EventPortFormatDetected");
                break;
        default:
                LOGI("[Unkown OMX_EVENTTYPE!]");
                break;
        }
}
 
OMX_ERRORTYPE decoder_event_handler(
                OMX_IN OMX_HANDLETYPE hComponent,
                OMX_IN OMX_PTR pAppData,
                OMX_IN OMX_EVENTTYPE eEvent,
                OMX_IN OMX_U32 nData1,
                OMX_IN OMX_U32 nData2,
                OMX_IN OMX_PTR pEventData)
{
        LOGI("[[decoder_event_handler]]: Got event %x %x %x\n", eEvent, (unsigned int)nData1, (unsigned int)nData2);
        //LOGI("Event=");
        Print_Event_Type(eEvent);
 
        OMX_COMMANDTYPE cmd_type;
 
        switch(eEvent)
        {
                case OMX_EventCmdComplete:
                switch (nData1) {
                        case OMX_CommandPortDisable:
                        LOGI("-- OMX_CommandPortDisable");
                        //DCHECK_EQ(data2, output_port_);
                        //OnOutputPortDisabled();
                        return;
                        case OMX_CommandPortEnable:
                        LOGI("-- OMX_CommandPortEnable on port %d",nData2);
 
                        if (nData2 == output_port)
                        {
                                //OnOutputPortEnabled();
                        } else if (nData2 == input_port)
                        {
                                //OnInputPortEnabled();
                        } else
                        LOGE("Unkown port!!!");
 
                        //DCHECK_EQ(data2, output_port_);
                        //OnOutputPortEnabled();
                        return;
                        case OMX_CommandStateSet:
                        LOGI("-- OMX_CommandStateSet");
                        //DispatchStateReached(static_cast<OMX_STATETYPE>(data2));
                        return;
                        case OMX_CommandFlush:
                        LOGI("-- OMX_CommandFlush");
                        /*if (current_state_change_ == DESTROYING)
                         return;
                         DCHECK(current_state_change_ == RESETTING);
                         if (data2 == input_port_)
                         InputPortFlushDone();
                         else if (data2 == output_port_)
                         OutputPortFlushDone();
                         else
                         NOTREACHED() << "Unexpected port flushed: " << data2;*/
                        return;
                        default:
                        LOGE("Unknown command completed: %d",nData1);
                }
                return;
                case OMX_EventPortSettingsChanged:
                // i got a reference for this
                LOGI("OMX_EventPortSettingsChanged ou~\n");
                break;
                case OMX_EventBufferFlag:
                LOGI("OMX_EventBufferFlag\n");
                break;
                case OMX_EventError:
                LOGE("fail\n");
                break;
                default:
                LOGI("Unkown event.");
        }
 
        return OMX_ErrorNone;
 
}
 
void Print_OMX_BUFFERHEADERTYPE(struct OMX_BUFFERHEADERTYPE* pVal) {
        LOGI("****** OMX_BUFFERHEADERTYPE = 0x%8X ******", pVal);
 
        LOGI("pVal->nSize = %u", pVal->nSize);
        LOGI("pVal->nVersion = 0x%X", pVal->nVersion);
        LOGI("pVal->pBuffer = 0x%X", pVal->pBuffer);
 
        LOGI("pVal->nAllocLen = %u", pVal->nAllocLen);
        LOGI("pVal->nFilledLen = %u", pVal->nFilledLen);
        LOGI("pVal->nOffset = %u", pVal->nOffset);
        LOGI("pVal->pAppPrivate = 0x%X", pVal->pAppPrivate);
        LOGI("pVal->nFlags = %u", pVal->nFlags);
 
        LOGI("pVal->nTickCount = %u", pVal->nTickCount);
        LOGI("pVal->nTimeStamp = %d,%d", (&pVal->nTimeStamp)[0],(&pVal->nTimeStamp)[1]);
 
        LOGI("pVal->nOutputPortIndex = %u", pVal->nOutputPortIndex);
        LOGI("pVal->nInputPortIndex = %u", pVal->nInputPortIndex);
 
        LOGI("******************");
}
 
OMX_ERRORTYPE decoder_empty_buffer_done(
                OMX_IN OMX_HANDLETYPE hComponent,
                OMX_IN OMX_PTR pAppData,
                OMX_IN OMX_BUFFERHEADERTYPE* pBuffer)
{
        int buff_num = *(int*)pBuffer->pAppPrivate;
 
        LOGI("[[ empty buffer cb ]]: %d\n", buff_num);
 
        //buffer_in_mask |= 1<< (*(int*)pBuffer->pPlatformPrivate);
 
        ready_input[buff_num] = 1; // commented for debugging
 
        /*if(pBuffer->pPlatformPrivate < 102400)
         exit(1);
 
         buffer_in_mask |= 1<<*(short*)pBuffer->pPlatformPrivate;
         sem_post(&wait_buff);
 
         return 0;
         */
 
        LOGI("[[/ empty buffer cb ]]: %d\n", buff_num);
 
        return OMX_ErrorNone;
}
 
OMX_ERRORTYPE decoder_fill_buffer_done(
                OMX_OUT OMX_HANDLETYPE hComponent,
                OMX_OUT OMX_PTR pAppData,
                OMX_OUT OMX_BUFFERHEADERTYPE* pBuffer)
{
 
        int buff_num = *(int*)pBuffer->pAppPrivate;
 
        //LOGI("[[decoder_fill_buffer_done]]: (buff=0x%08X,private=0x%08X,val=%d)",pBuffer,pBuffer->pAppPrivate,buff_num);
        LOGI("[[decoder_fill_buffer_done]]: buff=%d",buff_num);
 
        Print_OMX_BUFFERHEADERTYPE(pBuffer);
 
        // commented for debugging
        ready_output[buff_num] = 1; //the data in this buffer is ready.
 
        //LOGI("[Done receiving decoder_fill_buffer_done callback");
        return OMX_ErrorNone;
}
 
/*********************/
/*********************/
 
static void setHeader(OMX_PTR header, OMX_U32 size) {
 
        memset(header, 0, size);
 
        OMX_VERSIONTYPE* ver = (OMX_VERSIONTYPE*) (header + sizeof(OMX_U32));
        *((OMX_U32*) header) = size;
 
        ver->s.nVersionMajor = 1;
        ver->s.nVersionMinor = 1;
        ver->s.nRevision = 0;
        ver->s.nStep = 0;
}
 
// This is to initialize the OMX data structures to default values.
/*template <typename T>
 static void InitParam(const OmxVideoDecodeAccelerator& dec, T* param) {
 memset(param, 0, sizeof(T));
 param->nVersion.nVersion = 0x00000101;
 param->nSize = sizeof(T);
 }*/
 
 
OMX_STATETYPE Get_CurrentState(OMX_HANDLETYPE h)
{
        OMX_STATETYPE curr_state = OMX_StateInvalid;
 
        int err = OMX_GetState(h, &curr_state);
        if (err != OMX_ErrorNone)
        {
                LOGE("ERROR OMX_GetState - Error=%X!!!", err);
                return OMX_StateInvalid;
        }
 
        return curr_state;
}
 
void Print_CurrentState(OMX_STATETYPE curr_state)
{
 
        //LOGI("OMX_GetState ok. curr_state is %d",curr_state);
        switch (curr_state)
        {
                case OMX_StateInvalid:
                        LOGI("OMX_StateInvalid.");
                        break;
                case OMX_StateLoaded:
                        LOGI("OMX_StateLoaded.");
                        break;
                case OMX_StateIdle:
                        LOGI("OMX_StateIdle.");
                        break;
                case OMX_StateExecuting:
                        LOGI("OMX_StateExecuting.");
                        break;
                case OMX_StatePause:
                        LOGI("OMX_StatePause.");
                        break;
                case OMX_StateWaitForResources:
                        LOGI("OMX_StateWaitForResources.");
                        break;
 
                default:
                        LOGE("Unkown state.");
                        break;
        };
 
}
 
 
 
void Print_OMX_PORT_PARAM_TYPE(struct OMX_PORT_PARAM_TYPE* pVal) {
        LOGI("****** OMX_PORT_PARAM_TYPE = 0x%8X ******", pVal);
 
        LOGI("pVal->nSize = %u", pVal->nSize);
        LOGI("pVal->nVersion = 0x%X", pVal->nVersion);
        LOGI("pVal->nPorts = %u", pVal->nPorts);
        LOGI("pVal->nStartPortNumber = %u", pVal->nStartPortNumber);
 
        LOGI("******************");
}
 
void Print_OMX_PARAM_PORTDEFINITIONTYPE(struct OMX_PARAM_PORTDEFINITIONTYPE* port_def)
{
        LOGI("****** port_def = 0x%8X ******", port_def);
 
        LOGI("nSize = %u", port_def->nSize);
        LOGI("nVersion = %X", port_def->nVersion);
        LOGI("nPortIndex = %u", port_def->nPortIndex);
        LOGI("eDir = %u", port_def->eDir);
 
        LOGI("nBufferCountActual = %u", port_def->nBufferCountActual);
        LOGI("nBufferCountMin = %u", port_def->nBufferCountMin);
        LOGI("nBufferSize = %u", port_def->nBufferSize);
        LOGI("bEnabled = %u", port_def->bEnabled);
        LOGI("bPopulated = %u", port_def->bPopulated);
        LOGI("eDomain = %u", port_def->eDomain);
 
        LOGI("format.video.cMIMEType = %s", port_def->format.video.cMIMEType);
        LOGI("format.video.pNativeRender = %u",
                        port_def->format.video.pNativeRender);
        LOGI("format.video.nFrameWidth = %u", port_def->format.video.nFrameWidth);
        LOGI("format.video.nFrameHeight = %u", port_def->format.video.nFrameHeight);
        LOGI("format.video.nStride = %u", port_def->format.video.nStride);
        LOGI("format.video.nSliceHeight = %u", port_def->format.video.nSliceHeight);
 
        LOGI("format.video.nBitrate = %u", port_def->format.video.nBitrate);
        LOGI("format.video.xFramerate = %u", port_def->format.video.xFramerate);
        LOGI("format.video.bFlagErrorConcealment = %u",
                        port_def->format.video.bFlagErrorConcealment);
        LOGI("format.video.eCompressionFormat = %u",
                        port_def->format.video.eCompressionFormat);
        LOGI("format.video.eColorFormat = %u", port_def->format.video.eColorFormat);
 
        LOGI("******************************");
}
 
struct Decoder_OMX* CreateDecoder_OMX()
{
        int i;
 
 
        for (i=0;i<100;i++)
        {
                g_nums[i] = i;
 
                locked_output[i] = 0;
                ready_output[i] = 0;
 
                locked_input[i] = 0;
                ready_input[i] = 0;
        }
 
        for (i=0;i<PICTURE_BUFFERS_NUM_INPUT;i++)
                g_BuffersIn[i] = NULL;
 
        for (i=0;i<PICTURE_BUFFERS_NUM_OUTPUT;i++)
                g_BuffersOut[i] = NULL;
 
        /*if (g_pVidFile == NULL) {
                g_pVidFile = fopen("/sdcard/out.yuv", "rb");
                LOGI("Opened video file - got file handle %X",
                                (unsigned int) g_pVidFile);
                //fclose(g_pVideoFile_OMX);
        } else {
                LOGE("Failed trying to open video file.");
        }*/
 
        LOGI("Creating swscale object - to use for the yuv420 -> rgb convertion");
 
        SwsFilter* filter = sws_getDefaultFilter(0.f,0.f,0.f,0.f,0.f,0.f,0);
 
        g_scale_context = sws_getCachedContext(NULL,
                        640,480,PIX_FMT_YUV420P, //yuv422 ?
                        640,480,PIX_FMT_BGRA,
                SWS_POINT,NULL,NULL,NULL);
 
        LOGI("Starting to send client into...");
 
        struct tIntro_FromClient client_intro;
        client_intro.type = DT_FFMPEG;
        TCP_SendRaw_VideoConnection(&client_intro,sizeof(struct tIntro_FromClient));
 
        LOGI("Done.");
 
        LOGI("Starting to receive server into...");
 
        struct tIntro_FromServer server_intro;
        TCP_ReceiveRaw_VideoConnection(&server_intro,sizeof(struct tIntro_FromServer));
 
        LOGI("Done.");
 
 
        //[debug]
        /*input_buffer_size = (640*320*3)/2;
        char* tempBuffer = (char*)malloc(input_buffer_size);
 
        while(1)
        {
                int read_len = TCP_ReceiveRaw_VideoConnection(tempBuffer,input_buffer_size);
                //LOGI("read %d bytes from the server",read_len);
        }*/
 
        //[/debug]
 
        struct Decoder_OMX *instance = (struct Decoder_OMX *) malloc(
                        sizeof(struct Decoder_OMX));
 
        void* hModule;
        hModule = dlopen("/system/lib/libnvomx.so", RTLD_NOW);//dlopen("libpvnvomx.so",RTLD_NOW);
        if (hModule == NULL) {
                LOGE("libnvomx.so Load Fails\n");
                return NULL;
        }
 
        LOGI("libnvomx.so Load Success\n");
 
        my_OMX_Init = (OMX_Init_FPTR) dlsym(hModule, "OMX_Init");
        LOGI("my_OMX_Init=0x%8X", my_OMX_Init);
 
        OMX_ERRORTYPE err;
 
        char ComponentName[128];
        OMX_STRING str = ComponentName;
 
        err = my_OMX_Init();
        if (err != OMX_ErrorNone) {
                LOGE("OMX_Init Failed! [%d]", err);
                return NULL;
        }
 
        LOGI("OMX_Init ok.");
 
        my_OMX_ComponentNameEnum = (OMX_ComponentNameEnum_FPTR) dlsym(hModule,
                        "OMX_ComponentNameEnum");
        LOGI("my_OMX_ComponentNameEnum=0x%8X", my_OMX_ComponentNameEnum);
 
        err = OMX_ErrorNone;
 
        for (i = 0; err != OMX_ErrorNoMore; i++) {
                err = my_OMX_ComponentNameEnum(str, 128, i);
                if (err == OMX_ErrorNone) {
                        //LOGI("The %dth ComponentName is %s\n", i, str);
                }
        }
 
        my_OMX_GetRolesOfComponent = (OMX_GetRolesOfComponent_FPTR) dlsym(hModule,
                        "OMX_GetRolesOfComponent");
        LOGI("my_OMX_GetRolesOfComponent=0x%8X", my_OMX_GetRolesOfComponent);
 
        OMX_U32 NumRoles;
        OMX_U8 **roles;
 
        my_OMX_GetRolesOfComponent("OMX.Nvidia.h264.decode", &NumRoles, NULL); // get number of roles
 
        LOGI("%d Roles found.", NumRoles);
 
        roles = (OMX_U8**) malloc(NumRoles * sizeof(OMX_U8*));
        for (i = 0; i < NumRoles; i++) {
                roles[i] = (OMX_U8*) malloc(OMX_MAX_STRINGNAME_SIZE);
        }
 
        my_OMX_GetRolesOfComponent("OMX.Nvidia.h264.decode", &NumRoles, roles);
 
        for (i = 0; i < NumRoles; i++) {
                LOGI("H.264 Decoder Roles:%s\n", roles[i]);
        }
        for (i = 0; i < NumRoles; i++) {
                free(roles[i]);
                roles[i] = NULL;
        }
 
        free(roles);
        roles = NULL;
 
        //FILE* pVidFile = fopen("/sdcard/out.yuv", "rb");
        //LOGI("pVidFile=0x%X", pVidFile);
 
        my_OMX_GetHandle = (OMX_GetHandle_FPTR) dlsym(hModule, "OMX_GetHandle");
 
        OMX_CALLBACKTYPE decoder_callbacks = { decoder_event_handler,
                        decoder_empty_buffer_done, decoder_fill_buffer_done };
 
        err = my_OMX_GetHandle(&decoder_handle, "OMX.Nvidia.h264ext.decode", NULL,
                        &decoder_callbacks);
 
        if (err != OMX_ErrorNone) {
                LOGE("GetHandle for OMX.Nvidia.h264ext.decode Fails - [0x%x]\n", err);
                return NULL;
        }
 
        /*err = my_OMX_GetHandle(&vrenderer, "OMX.Nvidia.std.iv_renderer.overlay.yuv420", NULL,
                                &decoder_callbacks);
 
        if (err != OMX_ErrorNone) {
                LOGE("GetHandle for OMX.Nvidia.std.iv_renderer.overlay.yuv420 Fails - [0x%x]\n", err);
                return NULL;
        }*/
 
        LOGI("GetHandle ok.");
 
        // Get the port information. This will obtain information about the number of
        // ports and index of the first port.
        OMX_PORT_PARAM_TYPE port_param;
        setHeader(&port_param, sizeof(port_param));
 
        err     = OMX_GetParameter(decoder_handle, OMX_IndexParamVideoInit,
                                        &port_param);
 
        if (err != OMX_ErrorNone)
                LOGE("1 OMX_GetParameter on OMX_IndexParamVideoInit failed! [0x%X]",err);
        else
                LOGI("1 OMX_GetParameter on OMX_IndexParamVideoInit ok.");
 
        input_port = port_param.nStartPortNumber;
        output_port = input_port + 1;
 
        //Print_OMX_PORT_PARAM_TYPE(&port_param);
 
        // Set role for the component because components can have multiple roles.
 
        // skip it for now (seems like there's only one role in this component)
 
        // Populate input-buffer-related members based on input port data.
        OMX_PARAM_PORTDEFINITIONTYPE port_format;
        setHeader(&port_format, sizeof(port_format));
 
        //Print_OMX_PARAM_PORTDEFINITIONTYPE(&port_format);
        port_format.nPortIndex = input_port + 0;
 
        err = OMX_GetParameter(decoder_handle, OMX_IndexParamPortDefinition,
                        &port_format);
 
        if (err != OMX_ErrorNone)
                LOGE("2 OMX_GetParameter on OMX_IndexParamPortDefinition port 0 failed! [0x%X]",
                                err);
        else
                LOGI("2 OMX_GetParameter on OMX_IndexParamPortDefinition port 0 ok.");
 
        input_buffer_count = port_format.nBufferCountActual;
        input_buffer_size = port_format.nBufferSize;
        LOGD("input_buffer_size=%d",input_buffer_size);
        Print_OMX_PARAM_PORTDEFINITIONTYPE(&port_format);
 
        // Verify output port conforms to our expectations.
        setHeader(&port_format, sizeof(port_format));
        port_format.nPortIndex = output_port;
        err = OMX_GetParameter(decoder_handle, OMX_IndexParamPortDefinition,
                        &port_format);
 
        if (err != OMX_ErrorNone)
                LOGE("3 OMX_GetParameter on OMX_IndexParamPortDefinition port 1 failed! [0x%X]", err);
        else
                LOGI("3 OMX_GetParameter on OMX_IndexParamPortDefinition port 1 ok.");
 
        if (OMX_DirOutput != port_format.eDir) {
                LOGE("Expected output port eDir to be of type, well ... OMX_DirOutput!");
        } else {
                LOGI("Output port eDir is ok.");
        }
 
        Print_OMX_PARAM_PORTDEFINITIONTYPE(&port_format);
 
 
 
        // Set output port parameters.
        port_format.nBufferCountActual = PICTURE_BUFFERS_NUM_OUTPUT; //it wanted 10 originally, so look at this later as well!
        port_format.nBufferCountMin = PICTURE_BUFFERS_NUM_OUTPUT; //it wanted 10 originally, so look at this later as well!
 
        if (PICTURE_BUFFERS_NUM_OUTPUT != port_format.nBufferCountMin) {
                LOGE(
                                "ERROR - PICTURE_BUFFERS_NUM_OUTPUT != port_format.nBufferCountMin - nBufferCountMin=%d ",
                                port_format.nBufferCountMin);
        }
 
        // Force an OMX_EventPortSettingsChanged event to be sent once we know the
        // stream's real dimensions (which can only happen once some Decode() work has
        // been done).
        port_format.format.video.nFrameWidth = 640;
        port_format.format.video.nFrameHeight = 480;
 
        //port_format.format.video.xFramerate = 30;
        //port_format.format.video.nBitrate = 40000;
        port_format.nBufferSize=640*480*3/2;
 
        //port_format.nBufferSize=40000;
        //port_format.video.n
 
        int output_buff_size = port_format.nBufferSize;
 
        err = OMX_SetParameter(decoder_handle, OMX_IndexParamPortDefinition,
                        &port_format);
 
        if (err != OMX_ErrorNone)
                LOGE(
                                "4 OMX_SetParameter on OMX_IndexParamPortDefinition port 1 failed! [0x%X]",
                                err);
        else
                LOGI("4 OMX_SetParameter on OMX_IndexParamPortDefinition port 1 ok.");
 
        // allocate output buffers
 
 
         LOGI("--- Starting to allocate output buffers --- ");
 
         //int count = -1;
 
         //int* pCount = malloc(500);
 
         for(i=0;i<PICTURE_BUFFERS_NUM_OUTPUT;++i)
         {
                 LOGD("Sending private data 0x%08X",&g_nums[i]);
                 err = OMX_AllocateBuffer(decoder_handle, &g_BuffersOut[i], output_port, &g_nums[i],(640*480*3)/2);
                 if (err!=OMX_ErrorNone)
 
                 {
                         LOGE("Error on OMX_AllocateBuffer output %d! - Error=%X!!!",i,err);
                 } else
                 {
                         void* pVal = g_BuffersOut[i]->pAppPrivate;
 
                         LOGD("OMX_AllocateBuffer output %d ok. - buff=0x%08X,priv=0x%08X, priv_val=%d", i, g_BuffersOut[i],pVal,*((int*)pVal));
                         Print_OMX_BUFFERHEADERTYPE(g_BuffersOut[i]);
                 }
         }
 
        setHeader(&port_format, sizeof(port_format));
        port_format.nPortIndex = input_port;
        err = OMX_GetParameter(decoder_handle, OMX_IndexParamPortDefinition,
                        &port_format);
 
        if (err != OMX_ErrorNone)
                LOGE(
                                "5 OMX_GetParameter on OMX_IndexParamPortDefinition port 0 failed! [0x%X]",
                                err);
        else
                LOGI("5 OMX_GetParameter on OMX_IndexParamPortDefinition port 0 ok.");
 
        LOGI("*** Input port: ***");
 
        Print_OMX_PARAM_PORTDEFINITIONTYPE(&port_format);
 
        if (PICTURE_BUFFERS_NUM_INPUT != port_format.nBufferCountMin)
        {
                LOGE(
                                "input port_format.nBufferCountMin != PICTURE_BUFFERS_NUM !! it equals %d",
                                port_format.nBufferCountMin);
        }
 
        //input_buffer_size = (640*480*3)/2;
 
        LOGI("--- Starting to allocate input buffers at size=%d--- ",input_buffer_size);
 
 
         for(i=0;i<PICTURE_BUFFERS_NUM_INPUT;++i)
         {
                 err = OMX_AllocateBuffer(decoder_handle, &g_BuffersIn[i], input_port, &g_nums[i], input_buffer_size);
 
                 if (err!=OMX_ErrorNone)
                 {
                         LOGE("Error on OMX_AllocateBuffer input %d! - Error=%X!!!",i,err);
                 } else
                 {
                         //LOGI("OMX_AllocateBuffer input %d ok.",i);
                         Print_OMX_BUFFERHEADERTYPE(g_BuffersIn[i]);
                 }
 
 
                 //buffer_in_mask |= 1<<i;
 
                 //g_BuffersIn[i]->pPlatformPrivate = pCount;
                 //*pCount = i;
                 //pCount++;
 
                 //g_BuffersIn[i]->pPlatformPrivate = count;
                 //*count = i;
                 //count++;
 
                 LOGI("buf_in[%d]=%p\n", i, g_BuffersIn[i]);
 
         }
 
 
        // Fill the component with fake output buffers.  This seems to be required for
        // the component to move from Loaded to Idle.  How bogus.
 
        //char * pTempOutputBuff = (char*) malloc(640*480*4);
 
        /*for (i = 0; i < PICTURE_BUFFERS_NUM_OUTPUT; ++i)
        {
                OMX_BUFFERHEADERTYPE* buffer;
                err = OMX_UseBuffer(decoder_handle, &buffer, output_port, NULL, 0,      (OMX_U8*) (0x1));
 
                //err = OMX_UseBuffer(decoder_handle, &buffer, output_port,
                //                                         NULL, 0, (OMX_U8*)(0x1));
 
                if (err != OMX_ErrorNone) {
                        LOGE("Error on OMX_UseBuffer output %d! - Error=%X!!!", i, err);
                } else {
                        LOGI("OMX_UseBuffer output %d ok.", i);
                }
 
                buffer->pAppPrivate = NULL;
                buffer->nTimeStamp = -1;
                buffer->nOutputPortIndex = output_port;
                //CHECK(fake_output_buffers_.insert(buffer).second);
        }*/
 
        //1. add OMX_CommandPortEnable on both ports !!! (needed to move to state idle)
        //2. consider removing the fake use buffer if it's not needed.
 
 
 
        sleep(2);
 
        LOGI("Changing State...");
        // change state into OMX_Idle
 
        Print_CurrentState(Get_CurrentState(decoder_handle));
 
        err     = OMX_SendCommand(decoder_handle, OMX_CommandStateSet,
                                        OMX_StateIdle, 0);
        if (err != OMX_ErrorNone) {
                LOGE("ERROR OMX_SendCommand - OMX_CommandStateSet Error=%X!!!", err);
        } else {
                LOGI("OMX_SendCommand OMX_StateIdle ok.");
        }
 
        /*for (i = 0; i < PICTURE_BUFFERS_NUM_INPUT; ++i)
        {
                OMX_BUFFERHEADERTYPE* buffer;
                // While registering the buffer header we use fake buffer information
                // (length 0, at memory address 0x1) to fake out the "safety" check in
                // OMX_UseBuffer.  When it comes time to actually use this header in Decode
                // we set these fields to their real values (for the duration of that
                // Decode).
                OMX_ERRORTYPE result = OMX_UseBuffer(decoder_handle, &buffer,
                                input_port, NULL, // pAppPrivate gets set in Decode().
                                0, (OMX_U8*) (0x1));
                if (err != OMX_ErrorNone) {
                        LOGE("Error on OMX_UseBuffer input %d! - Error=%X!!!", i, err);
                } else {
                        LOGI("OMX_UseBuffer input %d ok.", i);
                }
 
                buffer->nInputPortIndex = input_port;
                buffer->nOffset = 0;
                buffer->nFlags = 0;
                //free_input_buffers_.push(buffer);
        }*/
 
        err = OMX_SendCommand(decoder_handle, OMX_CommandStateSet,
                        OMX_StateExecuting, 0);
        if (err != OMX_ErrorNone) {
                LOGE("ERROR OMX_SendCommand - OMX_StateExecuting Error=%X!!!", err);
        } else {
                LOGI("OMX_SendCommand OMX_StateExecuting ok.");
        }
 
 
        if (OMX_FROM_FILE==g_omx_input_type)
        {
                if (g_pVideoFile_OMX == NULL)
                {
                        g_pVideoFile_OMX = fopen("/preinstall/out.yuv","rb");
 
                        if (!g_pVideoFile_OMX)
                        {
                                LOGE("Failed trying to open video file.");
                        } else
                        {
                                LOGI("Opened video file - got file handle %X",(unsigned int)g_pVideoFile_OMX);
                        }
                        //fclose(g_pVideoFile_OMX);
                }
 
                g_pCompressedData = (char*)malloc(g_iCompressedDataSize); // 10mb
                int read_len = fread(g_pCompressedData,1,g_iCompressedDataSize,g_pVideoFile_OMX);
                g_pCompressedData_Travel = g_pCompressedData;
 
                LOGD("File to mem - read_len - %d",read_len);
 
                //
        }
 
        LOGI("Activating output port...");
 
                 err = OMX_SendCommand(decoder_handle, OMX_CommandPortEnable, output_port, 0);
                 if (err!=OMX_ErrorNone)
                 {
                 LOGE("ERROR OMX_SendCommand - OMX_CommandPortEnable Error=%X port=%d!!!",err,output_port);
                 } else
                 {
                 LOGI("OMX_SendCommand OMX_CommandPortEnable ok - port=%d.",output_port);
                 }
 
                LOGI("Activating input port...");
 
                 err = OMX_SendCommand(decoder_handle, OMX_CommandPortEnable, input_port, 0);
                 if (err!=OMX_ErrorNone)
                 {
                 LOGE("ERROR OMX_SendCommand - OMX_CommandPortEnable Error=%X port=%d!!!",err,input_port);
                 } else
                 {
                 LOGI("OMX_SendCommand OMX_CommandPortEnable ok - port=%d.",input_port);
                 }
 
        return instance;
}
 
int OMX_CanStartDecode()
{
        //LOGI("Printing Current State:");
        OMX_STATETYPE state = OMX_StateInvalid;
 
        state = Get_CurrentState(decoder_handle);
        //Print_CurrentState(state);
 
        if (OMX_StateExecuting==state)
        {
                return 1;
        }
 
        return 0;
}
 
void OMX_GetFrameData(unsigned char* pFrameData,int iDestPitch)
{
        /*unsigned char* pSrcTravel = (unsigned char*)current_frame;
        unsigned char* pDestTravel = pFrameData;
 
        int l,x;
        for (l=0;l<480;l++) // for each line
        {
                //memcpy(pDestTravel, pSrcTravel, 640*4);
                for (x=0;x<640;x++)
                {
                        pDestTravel[x*4] = pSrcTravel[x]; //copy y into grayscaled rgba...
                }
 
                pSrcTravel+= 640;
                pDestTravel+= iDestPitch;
        }*/
 
        //memcpy(pFrameData,current_frame,(640*480*3)/2);
 
        static unsigned char * temp_data = NULL;
 
        if (!temp_data)
        {
                int iSize = (640*480)/4;
                temp_data = malloc(iSize); //enough for u or v component
                int i;
                for (i=0;i<iSize;i++)
                {
                        temp_data[i] = 0;
                }
        }
 
        uint8_t *src_data[4];
        src_data[0] = current_frame;
        src_data[1] = &current_frame[640*480];
        src_data[2] = &current_frame[640*480+((640*480)/4)];
        src_data[3] = 0;
 
        uint8_t *dst_data[4];
        dst_data[0] = pFrameData;
        dst_data[1] = NULL;
        dst_data[2] = NULL;
        dst_data[3] = NULL;
 
        int linesize_src[4];
        linesize_src[0] = 640;
        linesize_src[1] = 320;
        linesize_src[2] = 320;
        linesize_src[3] = 320;
 
        int linesize_dst[4];
        linesize_dst[0] = 640*4;
        linesize_dst[1] = 0;
        linesize_dst[2] = 0;
        linesize_dst[3] = 0;
 
        int iRes = sws_scale(g_scale_context,
                        (const uint8_t * const*)src_data, //srcSlice
                        linesize_src, //srcStride
                        0, //srcSliceY
                        480,  //srcSliceH
                        dst_data, //dst
                        linesize_dst ); //dstStride
 
        //LOGI("sws_scale returnd %d",iRes);
}
 
int OMX_Decode()
{
        if (0==OMX_CanStartDecode())
                return 0;
 
        //
        //LOGI("Starting to decode...");
        LOGI(".");
 
        //for (nt i=0;i<5000)
 
        int i=0;
        int j=0;
        int err=-1;
 
        static int decoded_frames_so_far = 0;
 
//while (1)
{
 
 
        //char* tempBuffer = (char*)malloc(input_buffer_size);
 
        // get data from the device
        for (i=0;i<PICTURE_BUFFERS_NUM_OUTPUT;i++)
        {
                if (ready_output[i]==1)
                {
                        memcpy(current_frame,g_BuffersOut[i]->pBuffer,g_BuffersOut[i]->nFilledLen);
 
                        decoded_frames_so_far++;
 
                        LOGV("decoded frame %d",decoded_frames_so_far);
 
                        ready_output[i] = 0;
                        locked_output[i] = 0;
                }
        }
 
        // request data from the device
        {
                //LOGI("Itearation (%d)", j);
 
                for (i = 0; i < PICTURE_BUFFERS_NUM_OUTPUT; i++)
                {
                        if (locked_output[i] == 1)
                                continue;
 
                        g_BuffersOut[i]->nTimeStamp = 0;
                        g_BuffersOut[i]->nFilledLen = 0;
                        //g_BuffersOut[i]->pPlatformPrivate = (void*) i;
 
                        locked_output[i] = 1;
 
                        //LOGI("Request output buffer %d (buff=0x%08X,private=0x%X,val=%d)",i,g_BuffersOut[i],g_BuffersOut[i]->pAppPrivate,*((int*)g_BuffersOut[i]->pAppPrivate));
                        LOGI("Request output buffer %d",i);
 
                        err = OMX_FillThisBuffer(decoder_handle, g_BuffersOut[i]);
                        if (err == OMX_ErrorNotReady)
                        {
                                LOGI("OMX_FillThisBuffer output %d! - Error=OMX_ErrorNotReady",
                                                i);
                        } else if (err != OMX_ErrorNone)
                        {
                                LOGE("Error on OMX_FillThisBuffer output %d! - Error=%X!!!", i,
                                                err);
                        } else
                        {
                                //LOGI("OMX_FillThisBuffer output %d ok.", i);
                        }
                }
 
                // Decode
 
                OMX_BUFFERHEADERTYPE *buf;
 
                int read_len;
 
                // upload data to the device
                for (i = 0; i < PICTURE_BUFFERS_NUM_INPUT; i++)
                //for (i = 0; i < 1; i++)
                {
                        if (ready_input[i]==1)
                        {
                                ready_input[i] = 0;
                                locked_input[i] = 0;
                        }
                }
 
                // request data upload to the device
                for (i = 0; i < PICTURE_BUFFERS_NUM_INPUT; i++)
                {
                        buf = g_BuffersIn[i];
 
                        if (locked_input[i]==1)
                                continue;
 
                        if (OMX_FROM_FILE==g_omx_input_type)
                        {
 
 
                                int bytes_num = input_buffer_size;
                                //int bytes_num = 50000;
 
                                //read_len = fread(buf->pBuffer,1,bytes_num,g_pVideoFile_OMX);
 
                                if (  g_pCompressedData_Travel - g_pCompressedData < g_iCompressedDataSize-bytes_num-1)
                                {
                                        memcpy(buf->pBuffer,g_pCompressedData_Travel,bytes_num);
                                        read_len = bytes_num;
                                        g_pCompressedData_Travel+= input_buffer_size;
                                        LOGD("Read %d bytes of compressed data so far",(g_pCompressedData_Travel - g_pCompressedData) + bytes_num);
                                } else
                                {
                                        static int s_first = 1;
                                        if (s_first)
                                                LOGE("No more mem to read. g_pCompressedData_Travel=0x%08X, g_pCompressedData_Travel=0x%08X",g_pCompressedData_Travel,g_pCompressedData);
 
                                        s_first = 0;
                                        break;
                                }
 
                        } else if (OMX_FROM_NET==g_omx_input_type)
                        {
                                // from net
                                read_len = TCP_ReceiveRaw_VideoConnection(buf->pBuffer,1024*20);
                        }
 
                        LOGI("Request input buffer emptying %d",i);
 
                        //read_len = TCP_ReceiveRaw_VideoConnection(buf->pBuffer,input_buffer_size);
 
                        //read_len = TCP_ReceiveRaw_VideoConnection(tempBuffer,input_buffer_size);
 
                        //LOGI("read from net: %d bytes\n", read_len);
                        buf->nFilledLen = read_len;
                        buf->nAllocLen = buf->nFilledLen;
 
                        buf->nFlags &= ~OMX_BUFFERFLAG_EOS;
                        buf->nTimeStamp = 0;
 
                        //buf->pPlatformPrivate = (void*) i;
 
                        locked_input[i] = 1;
 
 
                        //Print_OMX_BUFFERHEADERTYPE(buf);
                        err = OMX_EmptyThisBuffer(decoder_handle, buf);
 
                        if (err == OMX_ErrorNotReady)
                        {
                                LOGI("OMX_EmptyThisBuffer input %d! - Error=OMX_ErrorNotReady",
                                                i);
                        } else if (err != OMX_ErrorNone) {
                                LOGE("Error on OMX_EmptyThisBuffer input %d! - Error=%X!!!", i,
                                                err);
                        } else
                        {
                                //LOGI("OMX_EmptyThisBuffer input %d ok.", i);
                                //buffer_in_mask &= (1 << i) ^ 0xFFFFFFFF;
 
 
 
                                //break;
                        }
                }
        }
 
}
 
        return 0;
}
//test6