Advertisement
Guest User

Untitled

a guest
Oct 5th, 2011
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 23.23 KB | None | 0 0
  1. #include "Decoder_OMX.h"
  2.  
  3. #include "OMX_Core.h"
  4. #include "OMX_Types.h"
  5. #include "OMX_Other.h"
  6. #include "OMX_Component.h"
  7. #include <stdlib.h>
  8. #include "logjam.h"
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <dlfcn.h>
  12. #include <unistd.h>
  13. #include <time.h>
  14. #include <unistd.h>
  15.  
  16. #define     OMX_MAX_STRINGNAME_SIZE   128  // why isn't this defined in OMX_Core.h ???
  17. /*static OMX_BUFFERHEADERTYPE **omx_buffers_out;
  18.  static OMX_BUFFERHEADERTYPE **omx_buffers_in;*/
  19.  
  20. static int buffer_out_mask;//, buffer_out_nb;
  21. static int buffer_in_mask;//, buffer_in_nb, buffer_in_size;
  22.  
  23.  
  24. typedef OMX_ERRORTYPE OMX_APIENTRY(*OMX_Init_FPTR)
  25. (void);
  26. OMX_Init_FPTR my_OMX_Init = NULL;
  27.  
  28. typedef OMX_ERRORTYPE OMX_APIENTRY(*OMX_ComponentNameEnum_FPTR)
  29. (
  30.         OMX_OUT OMX_STRING cComponentName,
  31.         OMX_IN OMX_U32 nNameLength,
  32.         OMX_IN OMX_U32 nIndex);
  33. OMX_ComponentNameEnum_FPTR my_OMX_ComponentNameEnum = NULL;
  34.  
  35. typedef OMX_ERRORTYPE (*OMX_GetRolesOfComponent_FPTR)(OMX_IN OMX_STRING compName, OMX_INOUT OMX_U32 *pNumRoles, OMX_OUT OMX_U8 **roles);
  36. OMX_GetRolesOfComponent_FPTR my_OMX_GetRolesOfComponent = NULL;
  37.  
  38. typedef OMX_ERRORTYPE (*OMX_GetHandle_FPTR)(
  39.         OMX_OUT OMX_HANDLETYPE* pHandle,
  40.         OMX_IN OMX_STRING cComponentName,
  41.         OMX_IN OMX_PTR pAppData,
  42.         OMX_IN OMX_CALLBACKTYPE* pCallBacks);
  43. OMX_GetHandle_FPTR my_OMX_GetHandle = NULL;
  44.  
  45. OMX_HANDLETYPE decoder_handle;
  46. int input_port = -1;
  47. int output_port = -1;
  48. int input_buffer_count = -1;
  49. int input_buffer_size = -1;
  50.  
  51. // this is hardcoded !!!
  52. // remember to make this dynamic
  53. #define PICTURE_BUFFERS_NUM_INPUT 10
  54. #define PICTURE_BUFFERS_NUM_OUTPUT 8
  55.  
  56. OMX_BUFFERHEADERTYPE* g_BuffersIn[PICTURE_BUFFERS_NUM_INPUT];
  57. OMX_BUFFERHEADERTYPE* g_BuffersOut[PICTURE_BUFFERS_NUM_OUTPUT];
  58.  
  59. int g_iCurrentInput = 0;
  60. int g_iCurrentOutput = 0;
  61.  
  62. FILE* g_pVidFile = NULL;
  63.  
  64. /*********************/
  65. // Callbacks
  66. /*********************/
  67.  
  68. void Print_Event_Type(OMX_EVENTTYPE event) {
  69.     //LOGI("blah=%d",OMX_EventPortSettingsChanged);
  70.     switch (event) {
  71.     case OMX_EventCmdComplete:
  72.         LOGI("event=OMX_EventCmdComplete");
  73.         break;
  74.  
  75.     case OMX_EventError:
  76.         LOGI("event=OMX_EventError");
  77.         break;
  78.     case OMX_EventMark:
  79.         LOGI("event=OMX_EventMark");
  80.         break;
  81.     case OMX_EventPortSettingsChanged:
  82.         LOGI("Oevent=MX_EventPortSettingsChanged");
  83.         break;
  84.     case OMX_EventBufferFlag:
  85.         LOGI("event=OMX_EventBufferFlag");
  86.         break;
  87.     case OMX_EventResourcesAcquired:
  88.         LOGI("event=OMX_EventResourcesAcquired");
  89.         break;
  90.     case OMX_EventComponentResumed:
  91.         LOGI("event=OMX_EventComponentResumed");
  92.         break;
  93.     case OMX_EventDynamicResourcesAvailable:
  94.         LOGI("event=OMX_EventDynamicResourcesAvailable");
  95.         break;
  96.     case OMX_EventPortFormatDetected:
  97.         LOGI("event=OMX_EventPortFormatDetected");
  98.         break;
  99.     default:
  100.         LOGI("[Unkown OMX_EVENTTYPE!]");
  101.         break;
  102.     }
  103. }
  104.  
  105. void OnOutputPortEnabled() {
  106.     LOGI("<<OnOutputPortEnabled>>");
  107.     int err =
  108.             OMX_FillThisBuffer(decoder_handle, g_BuffersOut[g_iCurrentOutput]);
  109.     if (err != OMX_ErrorNone)
  110.         LOGE("1 OMX_FillThisBuffer on output %d failed!! [0x%X]",
  111.                 g_iCurrentOutput, err);
  112.     else
  113.         LOGI("1 OMX_FillThisBuffer on output %d ok.", g_iCurrentOutput);
  114.  
  115.     g_iCurrentOutput++;
  116.     g_iCurrentOutput %= PICTURE_BUFFERS_NUM_OUTPUT;
  117. }
  118.  
  119. void OnInputPortEnabled() {
  120.     LOGI("<<OnInputPortEnabled>>");
  121.     OMX_BUFFERHEADERTYPE *buf = g_BuffersIn[g_iCurrentInput];
  122.  
  123.     int read_len = fread(buf->pBuffer, 1, input_buffer_size, g_pVidFile);
  124.     LOGI("read from file: %d bytes\n", read_len);
  125.     buf->nFilledLen = read_len;
  126.  
  127.     int err = OMX_EmptyThisBuffer(decoder_handle, buf);
  128.  
  129.     if (err == OMX_ErrorNotReady) {
  130.         LOGI("OMX_EmptyThisBuffer input %d! - Error=OMX_ErrorNotReady",
  131.                 g_iCurrentInput);
  132.     } else if (err != OMX_ErrorNone) {
  133.         LOGE("Error on OMX_EmptyThisBuffer input %d! - Error=%X!!!",
  134.                 g_iCurrentInput, err);
  135.     } else {
  136.         LOGI("OMX_EmptyThisBuffer input %d ok.", g_iCurrentInput);
  137.     }
  138.  
  139.     g_iCurrentInput++;
  140.     g_iCurrentInput %= PICTURE_BUFFERS_NUM_INPUT;
  141. }
  142.  
  143. OMX_ERRORTYPE decoder_event_handler(
  144.         OMX_IN OMX_HANDLETYPE hComponent,
  145.         OMX_IN OMX_PTR pAppData,
  146.         OMX_IN OMX_EVENTTYPE eEvent,
  147.         OMX_IN OMX_U32 nData1,
  148.         OMX_IN OMX_U32 nData2,
  149.         OMX_IN OMX_PTR pEventData)
  150. {
  151.     LOGI("[[decoder_event_handler]]: Got event %x %x %x\n", eEvent, (unsigned int)nData1, (unsigned int)nData2);
  152.     //LOGI("Event=");
  153.     Print_Event_Type(eEvent);
  154.  
  155.     OMX_COMMANDTYPE cmd_type;
  156.  
  157.     switch(eEvent)
  158.     {
  159.         case OMX_EventCmdComplete:
  160.         switch (nData1) {
  161.             case OMX_CommandPortDisable:
  162.             LOGI("-- OMX_CommandPortDisable");
  163.             //DCHECK_EQ(data2, output_port_);
  164.             //OnOutputPortDisabled();
  165.             return;
  166.             case OMX_CommandPortEnable:
  167.             LOGI("-- OMX_CommandPortEnable on port %d",nData2);
  168.  
  169.             if (nData2 == output_port)
  170.             {
  171.                 //OnOutputPortEnabled();
  172.             } else if (nData2 == input_port)
  173.             {
  174.                 //OnInputPortEnabled();
  175.             } else
  176.             LOGE("Unkown port!!!");
  177.  
  178.             //DCHECK_EQ(data2, output_port_);
  179.             //OnOutputPortEnabled();
  180.             return;
  181.             case OMX_CommandStateSet:
  182.             LOGI("-- OMX_CommandStateSet");
  183.             //DispatchStateReached(static_cast<OMX_STATETYPE>(data2));
  184.             return;
  185.             case OMX_CommandFlush:
  186.             LOGI("-- OMX_CommandFlush");
  187.             /*if (current_state_change_ == DESTROYING)
  188.              return;
  189.              DCHECK(current_state_change_ == RESETTING);
  190.              if (data2 == input_port_)
  191.              InputPortFlushDone();
  192.              else if (data2 == output_port_)
  193.              OutputPortFlushDone();
  194.              else
  195.              NOTREACHED() << "Unexpected port flushed: " << data2;*/
  196.             return;
  197.             default:
  198.             LOGE("Unknown command completed: %d",nData1);
  199.         }
  200.         return;
  201.         case OMX_EventPortSettingsChanged:
  202.         // i got a reference for this
  203.         LOGI("OMX_EventPortSettingsChanged ou~\n");
  204.         break;
  205.         case OMX_EventBufferFlag:
  206.         LOGI("OMX_EventBufferFlag\n");
  207.         break;
  208.         case OMX_EventError:
  209.         LOGE("fail\n");
  210.         break;
  211.         default:
  212.         LOGI("Unkown event.");
  213.     }
  214.  
  215.     return OMX_ErrorNone;
  216. }
  217.  
  218. OMX_ERRORTYPE decoder_empty_buffer_done(
  219.         OMX_IN OMX_HANDLETYPE hComponent,
  220.         OMX_IN OMX_PTR pAppData,
  221.         OMX_IN OMX_BUFFERHEADERTYPE* pBuffer)
  222. {
  223.     LOGI("[[decoder_empty_buffer_done]]: %x\n", pBuffer->pPlatformPrivate);
  224.     /*if(pBuffer->pPlatformPrivate < 102400)
  225.      exit(1);
  226.  
  227.      buffer_in_mask |= 1<<*(short*)pBuffer->pPlatformPrivate;
  228.      sem_post(&wait_buff);
  229.  
  230.      return 0;
  231.      */
  232.  
  233.     return OMX_ErrorNone;
  234. }
  235.  
  236. OMX_ERRORTYPE decoder_fill_buffer_done(
  237.         OMX_OUT OMX_HANDLETYPE hComponent,
  238.         OMX_OUT OMX_PTR pAppData,
  239.         OMX_OUT OMX_BUFFERHEADERTYPE* pBuffer)
  240. {
  241.  
  242.     static int got = 0, times=0;
  243.  
  244.     times++;
  245.  
  246.     LOGI("[[decoder_fill_buffer_done]]: (%d)",times);
  247.  
  248.     /*
  249.  
  250.      got +=  pBuffer->nFilledLen,
  251.      printf("filled %x %d %d %d\n", pBuffer,  pBuffer->nFilledLen, got, times );
  252.  
  253.      #ifdef DUMP
  254.      write(dumper, pBuffer->pBuffer,  pBuffer->nFilledLen);
  255.      fsync(dumper);
  256.      printf("sync...\n");
  257.      #endif
  258.  
  259.      buffer_out_mask |= 1<<*(short*)pBuffer->pPlatformPrivate;
  260.  
  261.      sem_post(&wait_buff);
  262.  
  263.      #if 0
  264.      int i;
  265.      printf("dump: ->");
  266.      for(i=0;i<pBuffer->nFilledLen;i++)
  267.      printf("%02x ", *(pBuffer->pBuffer+i));
  268.  
  269.      printf("<-\n");
  270.      #endif
  271.  
  272.      return 0;
  273.      */
  274.  
  275.     return OMX_ErrorNone;
  276. }
  277.  
  278. /*********************/
  279. /*********************/
  280.  
  281. static void setHeader(OMX_PTR header, OMX_U32 size) {
  282.  
  283.     memset(header, 0, size);
  284.  
  285.     OMX_VERSIONTYPE* ver = (OMX_VERSIONTYPE*) (header + sizeof(OMX_U32));
  286.     *((OMX_U32*) header) = size;
  287.  
  288.     ver->s.nVersionMajor = 1;
  289.     ver->s.nVersionMinor = 1;
  290.     ver->s.nRevision = 0;
  291.     ver->s.nStep = 0;
  292. }
  293.  
  294. // This is to initialize the OMX data structures to default values.
  295. /*template <typename T>
  296.  static void InitParam(const OmxVideoDecodeAccelerator& dec, T* param) {
  297.  memset(param, 0, sizeof(T));
  298.  param->nVersion.nVersion = 0x00000101;
  299.  param->nSize = sizeof(T);
  300.  }*/
  301.  
  302. void Print_CurrentState(OMX_HANDLETYPE h) {
  303.     OMX_STATETYPE curr_state = -1;
  304.  
  305.     int err = OMX_GetState(h, &curr_state);
  306.     if (err != OMX_ErrorNone) {
  307.         LOGE("ERROR OMX_GetState - Error=%X!!!", err);
  308.     } else {
  309.         //LOGI("OMX_GetState ok. curr_state is %d",curr_state);
  310.         switch (curr_state) {
  311.         case OMX_StateInvalid:
  312.             LOGI("OMX_StateInvalid.");
  313.             break;
  314.         case OMX_StateLoaded:
  315.             LOGI("OMX_StateLoaded.");
  316.             break;
  317.         case OMX_StateIdle:
  318.             LOGI("OMX_StateIdle.");
  319.             break;
  320.         case OMX_StateExecuting:
  321.             LOGI("OMX_StateExecuting.");
  322.             break;
  323.         case OMX_StatePause:
  324.             LOGI("OMX_StatePause.");
  325.             break;
  326.         case OMX_StateWaitForResources:
  327.             LOGI("OMX_StateWaitForResources.");
  328.             break;
  329.  
  330.         default:
  331.             LOGE("Unkown state.");
  332.             break;
  333.         };
  334.     }
  335. }
  336.  
  337. void Print_OMX_BUFFERHEADERTYPE(struct OMX_BUFFERHEADERTYPE* pVal) {
  338.     LOGI("****** OMX_BUFFERHEADERTYPE = 0x%8X ******", pVal);
  339.  
  340.     LOGI("pVal->nSize = %u", pVal->nSize);
  341.     LOGI("pVal->nVersion = 0x%X", pVal->nVersion);
  342.     LOGI("pVal->pBuffer = 0x%X", pVal->pBuffer);
  343.  
  344.     LOGI("pVal->nAllocLen = %u", pVal->nAllocLen);
  345.     LOGI("pVal->nFilledLen = %u", pVal->nFilledLen);
  346.     LOGI("pVal->nOffset = %u", pVal->nOffset);
  347.     LOGI("pVal->pAppPrivate = 0x%X", pVal->pAppPrivate);
  348.     LOGI("pVal->nFlags = %u", pVal->nFlags);
  349.     LOGI("pVal->nOutputPortIndex = %u", pVal->nOutputPortIndex);
  350.     LOGI("pVal->nInputPortIndex = %u", pVal->nInputPortIndex);
  351.  
  352.     LOGI("******************");
  353. }
  354.  
  355. void Print_OMX_PORT_PARAM_TYPE(struct OMX_PORT_PARAM_TYPE* pVal) {
  356.     LOGI("****** OMX_PORT_PARAM_TYPE = 0x%8X ******", pVal);
  357.  
  358.     LOGI("pVal->nSize = %u", pVal->nSize);
  359.     LOGI("pVal->nVersion = 0x%X", pVal->nVersion);
  360.     LOGI("pVal->nPorts = %u", pVal->nPorts);
  361.     LOGI("pVal->nStartPortNumber = %u", pVal->nStartPortNumber);
  362.  
  363.     LOGI("******************");
  364. }
  365.  
  366. void Print_OMX_PARAM_PORTDEFINITIONTYPE(
  367.         struct OMX_PARAM_PORTDEFINITIONTYPE* port_def) {
  368.     LOGI("****** port_def = 0x%8X ******", port_def);
  369.  
  370.     LOGI("nSize = %u", port_def->nSize);
  371.     LOGI("nVersion = %X", port_def->nVersion);
  372.     LOGI("nPortIndex = %u", port_def->nPortIndex);
  373.     LOGI("eDir = %u", port_def->eDir);
  374.  
  375.     LOGI("nBufferCountActual = %u", port_def->nBufferCountActual);
  376.     LOGI("nBufferCountMin = %u", port_def->nBufferCountMin);
  377.     LOGI("nBufferSize = %u", port_def->nBufferSize);
  378.     LOGI("bEnabled = %u", port_def->bEnabled);
  379.     LOGI("bPopulated = %u", port_def->bPopulated);
  380.     LOGI("eDomain = %u", port_def->eDomain);
  381.  
  382.     LOGI("format.video.cMIMEType = %s", port_def->format.video.cMIMEType);
  383.     LOGI("format.video.pNativeRender = %u",
  384.             port_def->format.video.pNativeRender);
  385.     LOGI("format.video.nFrameWidth = %u", port_def->format.video.nFrameWidth);
  386.     LOGI("format.video.nFrameHeight = %u", port_def->format.video.nFrameHeight);
  387.     LOGI("format.video.nStride = %u", port_def->format.video.nStride);
  388.     LOGI("format.video.nSliceHeight = %u", port_def->format.video.nSliceHeight);
  389.  
  390.     LOGI("format.video.nBitrate = %u", port_def->format.video.nBitrate);
  391.     LOGI("format.video.xFramerate = %u", port_def->format.video.xFramerate);
  392.     LOGI("format.video.bFlagErrorConcealment = %u",
  393.             port_def->format.video.bFlagErrorConcealment);
  394.     LOGI("format.video.eCompressionFormat = %u",
  395.             port_def->format.video.eCompressionFormat);
  396.     LOGI("format.video.eColorFormat = %u", port_def->format.video.eColorFormat);
  397.  
  398.     LOGI("******************************");
  399. }
  400.  
  401. struct Decoder_OMX* CreateDecoder_OMX() {
  402.  
  403.     if (g_pVidFile == NULL) {
  404.         g_pVidFile = fopen("/sdcard/out.yuv", "rb");
  405.         LOGI("Opened video file - got file handle %X",
  406.                 (unsigned int) g_pVidFile);
  407.         //fclose(g_pVideoFile);
  408.     } else {
  409.         LOGE("Failed trying to open video file.");
  410.  
  411.     }
  412.  
  413.     struct Decoder_OMX *instance = (struct Decoder_OMX *) malloc(
  414.             sizeof(struct Decoder_OMX));
  415.  
  416.     void* hModule;
  417.     hModule = dlopen("/system/lib/libnvomx.so", RTLD_NOW);//dlopen("libpvnvomx.so",RTLD_NOW);
  418.     if (hModule == NULL) {
  419.         LOGE("libnvomx.so Load Fails\n");
  420.         return NULL;
  421.     }
  422.  
  423.     LOGI("libnvomx.so Load Success\n");
  424.  
  425.     my_OMX_Init = (OMX_Init_FPTR) dlsym(hModule, "OMX_Init");
  426.     LOGI("my_OMX_Init=0x%8X", my_OMX_Init);
  427.  
  428.     OMX_ERRORTYPE err;
  429.     int i;
  430.     char ComponentName[128];
  431.     OMX_STRING str = ComponentName;
  432.  
  433.     err = my_OMX_Init();
  434.     if (err != OMX_ErrorNone) {
  435.         LOGE("OMX_Init Failed! [%d]", err);
  436.         return NULL;
  437.     }
  438.  
  439.     LOGI("OMX_Init ok.");
  440.  
  441.     my_OMX_ComponentNameEnum = (OMX_ComponentNameEnum_FPTR) dlsym(hModule,
  442.             "OMX_ComponentNameEnum");
  443.     LOGI("my_OMX_ComponentNameEnum=0x%8X", my_OMX_ComponentNameEnum);
  444.  
  445.     err = OMX_ErrorNone;
  446.  
  447.     for (i = 0; err != OMX_ErrorNoMore; i++) {
  448.         err = my_OMX_ComponentNameEnum(str, 128, i);
  449.         if (err == OMX_ErrorNone) {
  450.             //LOGI("The %dth ComponentName is %s\n", i, str);
  451.         }
  452.     }
  453.  
  454.     my_OMX_GetRolesOfComponent = (OMX_GetRolesOfComponent_FPTR) dlsym(hModule,
  455.             "OMX_GetRolesOfComponent");
  456.     LOGI("my_OMX_GetRolesOfComponent=0x%8X", my_OMX_GetRolesOfComponent);
  457.  
  458.     OMX_U32 NumRoles;
  459.     OMX_U8 **roles;
  460.  
  461.     my_OMX_GetRolesOfComponent("OMX.Nvidia.h264.decode", &NumRoles, NULL); // get number of roles
  462.  
  463.     LOGI("%d Roles found.", NumRoles);
  464.  
  465.     roles = (OMX_U8**) malloc(NumRoles * sizeof(OMX_U8*));
  466.     for (i = 0; i < NumRoles; i++) {
  467.         roles[i] = (OMX_U8*) malloc(OMX_MAX_STRINGNAME_SIZE);
  468.     }
  469.  
  470.     my_OMX_GetRolesOfComponent("OMX.Nvidia.h264.decode", &NumRoles, roles);
  471.  
  472.     for (i = 0; i < NumRoles; i++) {
  473.         LOGI("H.264 Decoder Roles:%s\n", roles[i]);
  474.     }
  475.     for (i = 0; i < NumRoles; i++) {
  476.         free(roles[i]);
  477.         roles[i] = NULL;
  478.     }
  479.  
  480.     free(roles);
  481.     roles = NULL;
  482.  
  483.     FILE* pVidFile = fopen("/sdcard/out.yuv", "rb");
  484.     LOGI("pVidFile=0x%X", pVidFile);
  485.  
  486.     my_OMX_GetHandle = (OMX_GetHandle_FPTR) dlsym(hModule, "OMX_GetHandle");
  487.  
  488.     OMX_CALLBACKTYPE decoder_callbacks = { decoder_event_handler,
  489.             decoder_empty_buffer_done, decoder_fill_buffer_done };
  490.  
  491.     err = my_OMX_GetHandle(&decoder_handle, "OMX.Nvidia.h264ext.decode", NULL,
  492.             &decoder_callbacks);
  493.  
  494.     if (err != OMX_ErrorNone) {
  495.         LOGE("GetHandle Fails - [0x%x]\n", err);
  496.         return NULL;
  497.     }
  498.  
  499.     LOGI("GetHandle ok.");
  500.  
  501.     // Get the port information. This will obtain information about the number of
  502.     // ports and index of the first port.
  503.     OMX_PORT_PARAM_TYPE port_param;
  504.     setHeader(&port_param, sizeof(port_param));
  505.  
  506.     err
  507.             = OMX_GetParameter(decoder_handle, OMX_IndexParamVideoInit,
  508.                     &port_param);
  509.  
  510.     if (err != OMX_ErrorNone)
  511.         LOGE("1 OMX_GetParameter on OMX_IndexParamVideoInit failed! [0x%X]",
  512.                 err);
  513.     else
  514.         LOGI("1 OMX_GetParameter on OMX_IndexParamVideoInit ok.");
  515.  
  516.     input_port = port_param.nStartPortNumber;
  517.     output_port = input_port + 1;
  518.  
  519.     Print_OMX_PORT_PARAM_TYPE(&port_param);
  520.  
  521.     // Set role for the component because components can have multiple roles.
  522.  
  523.     // skip it for now (seems like there's only one role in this component)
  524.  
  525.     // Populate input-buffer-related members based on input port data.
  526.     OMX_PARAM_PORTDEFINITIONTYPE port_format;
  527.     setHeader(&port_format, sizeof(port_format));
  528.  
  529.     LOGI("-- Before get parameter --");
  530.     Print_OMX_PARAM_PORTDEFINITIONTYPE(&port_format);
  531.     port_format.nPortIndex = input_port + 0;
  532.  
  533.     err = OMX_GetParameter(decoder_handle, OMX_IndexParamPortDefinition,
  534.             &port_format);
  535.  
  536.     if (err != OMX_ErrorNone)
  537.         LOGE(
  538.                 "2 OMX_GetParameter on OMX_IndexParamPortDefinition port 0 failed! [0x%X]",
  539.                 err);
  540.     else
  541.         LOGI("2 OMX_GetParameter on OMX_IndexParamPortDefinition port 0 ok.");
  542.  
  543.     input_buffer_count = port_format.nBufferCountActual;
  544.     input_buffer_size = port_format.nBufferSize;
  545.  
  546.     Print_OMX_PARAM_PORTDEFINITIONTYPE(&port_format);
  547.  
  548.     // Verify output port conforms to our expectations.
  549.     setHeader(&port_format, sizeof(port_format));
  550.     port_format.nPortIndex = output_port;
  551.     err = OMX_GetParameter(decoder_handle, OMX_IndexParamPortDefinition,
  552.             &port_format);
  553.  
  554.     if (err != OMX_ErrorNone)
  555.         LOGE(
  556.                 "3 OMX_GetParameter on OMX_IndexParamPortDefinition port 1 failed! [0x%X]",
  557.                 err);
  558.     else
  559.         LOGI("3 OMX_GetParameter on OMX_IndexParamPortDefinition port 1 ok.");
  560.  
  561.     if (OMX_DirOutput != port_format.eDir) {
  562.         LOGE("Expected output port eDir to be of type, well ... OMX_DirOutput!");
  563.     } else {
  564.         LOGI("Output port eDir is ok.");
  565.     }
  566.  
  567.     Print_OMX_PARAM_PORTDEFINITIONTYPE(&port_format);
  568.  
  569.     // Set output port parameters.
  570.     port_format.nBufferCountActual = PICTURE_BUFFERS_NUM_OUTPUT; //it wanted 10 originally, so look at this later as well!
  571.     port_format.nBufferCountMin = PICTURE_BUFFERS_NUM_OUTPUT; //it wanted 10 originally, so look at this later as well!
  572.  
  573.     if (PICTURE_BUFFERS_NUM_OUTPUT != port_format.nBufferCountMin) {
  574.         LOGE(
  575.                 "ERROR - PICTURE_BUFFERS_NUM_OUTPUT != port_format.nBufferCountMin - nBufferCountMin=%d ",
  576.                 port_format.nBufferCountMin);
  577.     }
  578.  
  579.     // Force an OMX_EventPortSettingsChanged event to be sent once we know the
  580.     // stream's real dimensions (which can only happen once some Decode() work has
  581.     // been done).
  582.     port_format.format.video.nFrameWidth = -1;//640;
  583.     port_format.format.video.nFrameHeight = -1;//480;
  584.     //port_format.nBufferSize=640*480*3/2;
  585.  
  586.     int output_buff_size = port_format.nBufferSize;
  587.  
  588.     err = OMX_SetParameter(decoder_handle, OMX_IndexParamPortDefinition,
  589.             &port_format);
  590.  
  591.     if (err != OMX_ErrorNone)
  592.         LOGE(
  593.                 "4 OMX_SetParameter on OMX_IndexParamPortDefinition port 1 failed! [0x%X]",
  594.                 err);
  595.     else
  596.         LOGI("4 OMX_SetParameter on OMX_IndexParamPortDefinition port 1 ok.");
  597.  
  598.     // allocate output buffers
  599.  
  600.     /*
  601.      LOGI("--- Starting to allocate output buffers --- ");
  602.  
  603.      for(i=0;i<PICTURE_BUFFERS_NUM_OUTPUT;++i)
  604.      {
  605.      err = OMX_AllocateBuffer(decoder_handle, &g_BuffersOut[i], output_port, NULL, port_format.nBufferSize);
  606.      if (err!=OMX_ErrorNone)
  607.      {
  608.      LOGE("Error on OMX_AllocateBuffer output %d! - Error=%X!!!",i,err);
  609.      } else
  610.      {
  611.      //LOGI("OMX_AllocateBuffer output %d ok.",i);
  612.      //Print_OMX_BUFFERHEADERTYPE(g_BuffersOut[i]);
  613.      }
  614.  
  615.      buffer_out_mask |= 1<<i;
  616.  
  617.      //g_BuffersOut[i]->pPlatformPrivate = count;
  618.      //*count = i;
  619.      //count++;
  620.  
  621.  
  622.      //LOGI("buf_out[%d]=%p\n", i, g_BuffersOut[i]);
  623.  
  624.      }
  625.      */
  626.  
  627.     setHeader(&port_format, sizeof(port_format));
  628.     port_format.nPortIndex = input_port;
  629.     err = OMX_GetParameter(decoder_handle, OMX_IndexParamPortDefinition,
  630.             &port_format);
  631.  
  632.     if (err != OMX_ErrorNone)
  633.         LOGE(
  634.                 "5 OMX_GetParameter on OMX_IndexParamPortDefinition port 0 failed! [0x%X]",
  635.                 err);
  636.     else
  637.         LOGI("5 OMX_GetParameter on OMX_IndexParamPortDefinition port 0 ok.");
  638.  
  639.     LOGI("*** Input port: ***");
  640.  
  641.     Print_OMX_PARAM_PORTDEFINITIONTYPE(&port_format);
  642.  
  643.     if (PICTURE_BUFFERS_NUM_INPUT != port_format.nBufferCountMin) {
  644.         LOGE(
  645.                 "input port_format.nBufferCountMin != PICTURE_BUFFERS_NUM !! it equals %d",
  646.                 port_format.nBufferCountMin);
  647.     }
  648.  
  649.     /*LOGI("--- Starting to allocate input buffers --- ");
  650.  
  651.      for(i=0;i<PICTURE_BUFFERS_NUM_INPUT;++i)
  652.      {
  653.      err = OMX_AllocateBuffer(decoder_handle, &g_BuffersIn[i], input_port, NULL, port_format.nBufferSize);
  654.  
  655.      if (err!=OMX_ErrorNone)
  656.      {
  657.      LOGE("Error on OMX_AllocateBuffer input %d! - Error=%X!!!",i,err);
  658.      } else
  659.      {
  660.      //LOGI("OMX_AllocateBuffer input %d ok.",i);
  661.      //Print_OMX_BUFFERHEADERTYPE(g_BuffersIn[i]);
  662.      }
  663.  
  664.  
  665.      buffer_in_mask |= 1<<i;
  666.  
  667.      //g_BuffersIn[i]->pPlatformPrivate = count;
  668.      //*count = i;
  669.      //count++;
  670.  
  671.      LOGI("buf_in[%d]=%p\n", i, g_BuffersIn[i]);
  672.  
  673.      }
  674.      */
  675.  
  676.     // Fill the component with fake output buffers.  This seems to be required for
  677.     // the component to move from Loaded to Idle.  How bogus.
  678.     for (i = 0; i < PICTURE_BUFFERS_NUM_OUTPUT; ++i) {
  679.         OMX_BUFFERHEADERTYPE* buffer;
  680.         err = OMX_UseBuffer(decoder_handle, &buffer, output_port, NULL, 0,
  681.                 (OMX_U8*) (0x1));
  682.  
  683.         //err = OMX_UseBuffer(decoder_handle, &buffer, output_port,
  684.         //                     NULL, 0, (OMX_U8*)(0x1));
  685.  
  686.         if (err != OMX_ErrorNone) {
  687.             LOGE("Error on OMX_UseBuffer output %d! - Error=%X!!!", i, err);
  688.         } else {
  689.             LOGI("OMX_UseBuffer output %d ok.", i);
  690.         }
  691.  
  692.         buffer->pAppPrivate = NULL;
  693.         buffer->nTimeStamp = -1;
  694.         buffer->nOutputPortIndex = output_port;
  695.         //CHECK(fake_output_buffers_.insert(buffer).second);
  696.     }
  697.  
  698.     //1. add OMX_CommandPortEnable on both ports !!! (needed to move to state idle)
  699.     //2. consider removing the fake use buffer if it's not needed.
  700.  
  701.     /*LOGI("Activating output port...");
  702.  
  703.      err = OMX_SendCommand(decoder_handle, OMX_CommandPortEnable, output_port, 0);
  704.      if (err!=OMX_ErrorNone)
  705.      {
  706.      LOGE("ERROR OMX_SendCommand - OMX_CommandPortEnable Error=%X port=%d!!!",err,output_port);
  707.      } else
  708.      {
  709.      LOGI("OMX_SendCommand OMX_CommandPortEnable ok - port=%d.",output_port);
  710.      }*/
  711.  
  712.     /*LOGI("Activating input port...");
  713.  
  714.      err = OMX_SendCommand(decoder_handle, OMX_CommandPortEnable, input_port, 0);
  715.      if (err!=OMX_ErrorNone)
  716.      {
  717.      LOGE("ERROR OMX_SendCommand - OMX_CommandPortEnable Error=%X port=%d!!!",err,input_port);
  718.      } else
  719.      {
  720.      LOGI("OMX_SendCommand OMX_CommandPortEnable ok - port=%d.",input_port);
  721.      }*/
  722.  
  723.     sleep(2);
  724.  
  725.     LOGI("Changing State...");
  726.     // change state into OMX_Idle
  727.  
  728.     Print_CurrentState(decoder_handle);
  729.  
  730.     err
  731.             = OMX_SendCommand(decoder_handle, OMX_CommandStateSet,
  732.                     OMX_StateIdle, 0);
  733.     if (err != OMX_ErrorNone) {
  734.         LOGE("ERROR OMX_SendCommand - OMX_CommandStateSet Error=%X!!!", err);
  735.     } else {
  736.         LOGI("OMX_SendCommand OMX_StateIdle ok.");
  737.     }
  738.  
  739.     for (i = 0; i < PICTURE_BUFFERS_NUM_INPUT; ++i) {
  740.         OMX_BUFFERHEADERTYPE* buffer;
  741.         // While registering the buffer header we use fake buffer information
  742.         // (length 0, at memory address 0x1) to fake out the "safety" check in
  743.         // OMX_UseBuffer.  When it comes time to actually use this header in Decode
  744.         // we set these fields to their real values (for the duration of that
  745.         // Decode).
  746.         OMX_ERRORTYPE result = OMX_UseBuffer(decoder_handle, &buffer,
  747.                 input_port, NULL, /* pAppPrivate gets set in Decode(). */
  748.                 0, (OMX_U8*) (0x1));
  749.         if (err != OMX_ErrorNone) {
  750.             LOGE("Error on OMX_UseBuffer input %d! - Error=%X!!!", i, err);
  751.         } else {
  752.             LOGI("OMX_UseBuffer input %d ok.", i);
  753.         }
  754.  
  755.         buffer->nInputPortIndex = input_port;
  756.         buffer->nOffset = 0;
  757.         buffer->nFlags = 0;
  758.         //free_input_buffers_.push(buffer);
  759.     }
  760.  
  761.     err = OMX_SendCommand(decoder_handle, OMX_CommandStateSet,
  762.             OMX_StateExecuting, 0);
  763.     if (err != OMX_ErrorNone) {
  764.         LOGE("ERROR OMX_SendCommand - OMX_StateExecuting Error=%X!!!", err);
  765.     } else {
  766.         LOGI("OMX_SendCommand OMX_StateExecuting ok.");
  767.     }
  768.  
  769.     while (1) {
  770.         Print_CurrentState(decoder_handle);
  771.         sleep(1);
  772.     }
  773.  
  774.     LOGI("Starting to decode...");
  775.  
  776.     //for (nt i=0;i<5000)
  777.  
  778.     int j = 0;
  779.  
  780.     //while (1)
  781.     for (j = 0; j < 10; j++) {
  782.         LOGI("Itearation (%d)", j);
  783.  
  784.         // Decode
  785.  
  786.         OMX_BUFFERHEADERTYPE *buf;
  787.  
  788.         int read_len;
  789.  
  790.         for (i = 0; i < PICTURE_BUFFERS_NUM_INPUT; i++) {
  791.  
  792.             buf = g_BuffersIn[i];
  793.  
  794.             if (!((1 << i) & buffer_in_mask))
  795.                 continue;
  796.  
  797.             //read_len = read(g_pVidFile, buf->pBuffer, input_buffer_size/4);
  798.             read_len = fread(buf->pBuffer, 1, input_buffer_size, g_pVidFile);
  799.             LOGI("read from file: %d bytes\n", read_len);
  800.             buf->nFilledLen = read_len;
  801.  
  802.             err = OMX_EmptyThisBuffer(decoder_handle, buf);
  803.  
  804.             if (err == OMX_ErrorNotReady) {
  805.                 LOGI("OMX_EmptyThisBuffer input %d! - Error=OMX_ErrorNotReady",
  806.                         i);
  807.             } else if (err != OMX_ErrorNone) {
  808.                 LOGE("Error on OMX_EmptyThisBuffer input %d! - Error=%X!!!", i,
  809.                         err);
  810.             } else {
  811.                 LOGI("OMX_EmptyThisBuffer input %d ok.", i);
  812.                 buffer_in_mask &= (1 << i) ^ 0xFFFFFFFF;
  813.             }
  814.         }
  815.  
  816.         for (i = 0; i < PICTURE_BUFFERS_NUM_OUTPUT; i++) {
  817.             if (!((1 << i) & buffer_out_mask))
  818.                 continue;
  819.  
  820.             err = OMX_FillThisBuffer(decoder_handle, g_BuffersOut[i]);
  821.             if (err == OMX_ErrorNotReady) {
  822.                 LOGI("OMX_FillThisBuffer output %d! - Error=OMX_ErrorNotReady",
  823.                         i);
  824.             } else if (err != OMX_ErrorNone) {
  825.                 LOGE("Error on OMX_FillThisBuffer output %d! - Error=%X!!!", i,
  826.                         err);
  827.             } else {
  828.                 LOGI("OMX_FillThisBuffer output %d ok.", i);
  829.                 buffer_out_mask &= (1 << i) ^ 0xFFFFFFFF;
  830.             }
  831.         }
  832.  
  833.     }
  834.  
  835.     return instance;
  836. }
  837.  
  838.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement