wave511test.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2021 StarFive Technology Co., Ltd.
  4. */
  5. #include <stdio.h>
  6. #include <signal.h>
  7. #include <OMX_Core.h>
  8. #include <OMX_Component.h>
  9. #include <OMX_Video.h>
  10. #include <OMX_IndexExt.h>
  11. #include <sys/time.h>
  12. #define OMX_INIT_STRUCTURE(a) \
  13. memset(&(a), 0, sizeof(a)); \
  14. (a).nSize = sizeof(a); \
  15. (a).nVersion.nVersion = 1; \
  16. (a).nVersion.s.nVersionMajor = 1; \
  17. (a).nVersion.s.nVersionMinor = 1; \
  18. (a).nVersion.s.nRevision = 1; \
  19. (a).nVersion.s.nStep = 1
  20. OMX_BUFFERHEADERTYPE *pInputBuffer, *pOutputBuffer;
  21. static int g_is_run = 0;
  22. static int numBuffer = 0;
  23. static OMX_ERRORTYPE event_handler(
  24. OMX_HANDLETYPE hComponent,
  25. OMX_PTR pAppData,
  26. OMX_EVENTTYPE eEvent,
  27. OMX_U32 nData1,
  28. OMX_U32 nData2,
  29. OMX_PTR pEventData)
  30. {
  31. return OMX_ErrorNone;
  32. }
  33. static OMX_ERRORTYPE fill_output_buffer_done_handler(
  34. OMX_HANDLETYPE hComponent,
  35. OMX_PTR pAppData,
  36. OMX_BUFFERHEADERTYPE *pBuffer)
  37. {
  38. struct timeval tv;
  39. gettimeofday(&tv,NULL);
  40. printf("[%ld], frame = %d\n",tv.tv_sec*1000000 + tv.tv_usec, numBuffer); //微秒
  41. numBuffer ++;
  42. //write to file
  43. FILE *fb = fopen("./out.dat", "ab+");
  44. fwrite(pBuffer->pBuffer, 1, pBuffer->nFilledLen, fb);
  45. fclose(fb);
  46. OMX_FillThisBuffer(hComponent, pOutputBuffer);
  47. return OMX_ErrorNone;
  48. }
  49. static OMX_ERRORTYPE empty_buffer_done_handler(
  50. OMX_HANDLETYPE hComponent,
  51. OMX_PTR pAppData,
  52. OMX_BUFFERHEADERTYPE *pBuffer)
  53. {
  54. printf("%s:%d:!!!!!!!!!!!!!!!!!!\r\n",__FUNCTION__, __LINE__);
  55. //write to file
  56. return OMX_ErrorNone;
  57. }
  58. static void block_until_state_changed(OMX_HANDLETYPE hComponent, OMX_STATETYPE wanted_eState)
  59. {
  60. OMX_STATETYPE eState;
  61. while (eState != wanted_eState && g_is_run)
  62. {
  63. OMX_GetState(hComponent, &eState);
  64. if (eState != wanted_eState)
  65. {
  66. printf("state = %d\r\n", eState);
  67. usleep(10000 * 1000);
  68. }
  69. }
  70. }
  71. static void signal_handle(int sig)
  72. {
  73. printf("[%s,%d]: receive sig=%d \n", __FUNCTION__, __LINE__, sig);
  74. if (g_is_run) {
  75. OMX_Deinit();
  76. g_is_run = 0;
  77. }
  78. }
  79. //TODO: callback, parameter, buffer
  80. int main(void)
  81. {
  82. printf("=============================\r\n");
  83. OMX_HANDLETYPE hComponentDecoder;
  84. OMX_CALLBACKTYPE callbacks;
  85. int ret = OMX_ErrorNone;
  86. signal(SIGINT, signal_handle);
  87. ret = OMX_Init();
  88. if (ret != OMX_ErrorNone) {
  89. printf("[%s,%d]: run OMX_Init failed. ret is %d \n", __FUNCTION__, __LINE__, ret);
  90. return 1;
  91. }
  92. g_is_run = 1;
  93. callbacks.EventHandler = event_handler;
  94. callbacks.FillBufferDone = fill_output_buffer_done_handler;
  95. callbacks.EmptyBufferDone = empty_buffer_done_handler;
  96. OMX_GetHandle(&hComponentDecoder, "sf.dec.decoder", NULL, &callbacks);
  97. if (hComponentDecoder == NULL)
  98. {
  99. printf("could not get handle\r\n");
  100. return 0;
  101. }
  102. OMX_SetParameter(hComponentDecoder, OMX_IndexParamVideoHevc, NULL);
  103. OMX_SendCommand(hComponentDecoder, OMX_CommandStateSet, OMX_StateIdle, NULL);
  104. FILE *fb = fopen("./stream/hevc/akiyo.cfg_ramain_tv0.cfg.265", "r");
  105. if (fb == NULL)
  106. {
  107. printf("%s:%d can not open file\r\n", __FUNCTION__, __LINE__);
  108. }
  109. int curpos = ftell(fb);
  110. fseek(fb,0L,SEEK_END);
  111. int size = ftell(fb);
  112. fseek(fb,curpos,SEEK_SET);
  113. printf("%s:%d size = %d\r\n", __FUNCTION__, __LINE__, size);
  114. OMX_AllocateBuffer(hComponentDecoder, &pInputBuffer, 0, NULL, size);
  115. OMX_AllocateBuffer(hComponentDecoder, &pOutputBuffer, 0, NULL, 0x20000);
  116. int len = fread(pInputBuffer->pBuffer, 1, size, fb);
  117. printf("%s:%d:size = %d\r\n",__FUNCTION__, __LINE__, len);
  118. OMX_SendCommand(hComponentDecoder, OMX_CommandStateSet, OMX_StateExecuting, NULL);
  119. OMX_EmptyThisBuffer(hComponentDecoder, pInputBuffer);
  120. OMX_FillThisBuffer(hComponentDecoder, pOutputBuffer);
  121. // OMX_FillThisBuffer(hComponentDecoder, pOutputBuffer);
  122. block_until_state_changed(hComponentDecoder, OMX_StateIdle);
  123. OMX_FreeHandle(hComponentDecoder);
  124. return 0;
  125. }