dec_test.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2021 StarFive Technology Co., Ltd.
  4. */
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <signal.h>
  8. #include <getopt.h>
  9. #include <fcntl.h>
  10. #include "libavformat/avformat.h"
  11. #include <OMX_Core.h>
  12. #include <OMX_Component.h>
  13. #include <OMX_Video.h>
  14. #include <OMX_IndexExt.h>
  15. #include <sys/time.h>
  16. #include <sys/types.h>
  17. #include <sys/ipc.h>
  18. #include <sys/msg.h>
  19. #define OMX_INIT_STRUCTURE(a) \
  20. memset(&(a), 0, sizeof(a)); \
  21. (a).nSize = sizeof(a); \
  22. (a).nVersion.nVersion = 1; \
  23. (a).nVersion.s.nVersionMajor = 1; \
  24. (a).nVersion.s.nVersionMinor = 1; \
  25. (a).nVersion.s.nRevision = 1; \
  26. (a).nVersion.s.nStep = 1
  27. typedef struct Message
  28. {
  29. long msg_type;
  30. OMX_S32 msg_flag;
  31. OMX_BUFFERHEADERTYPE *pBuffer;
  32. } Message;
  33. typedef struct DecodeTestContext
  34. {
  35. OMX_HANDLETYPE hComponentDecoder;
  36. char sOutputFilePath[256];
  37. char sInputFilePath[256];
  38. char sOutputFormat[64];
  39. OMX_U32 ScaleWidth;
  40. OMX_U32 ScaleHeight;
  41. OMX_U32 OutputWidth;
  42. OMX_U32 OutputHeight;
  43. OMX_U32 OutputStride;
  44. OMX_U32 OutputSlice;
  45. OMX_COLOR_FORMATTYPE FormatType;
  46. OMX_BUFFERHEADERTYPE *pInputBufferArray[64];
  47. OMX_BUFFERHEADERTYPE *pOutputBufferArray[64];
  48. AVFormatContext *avContext;
  49. OMX_STATETYPE comState;
  50. int msgid;
  51. } DecodeTestContext;
  52. DecodeTestContext *decodeTestContext;
  53. static OMX_S32 FillInputBuffer(DecodeTestContext *decodeTestContext, OMX_BUFFERHEADERTYPE *pInputBuffer);
  54. static OMX_BOOL disableEVnt = OMX_FALSE;
  55. static OMX_BOOL useNormal = OMX_FALSE;
  56. static OMX_BOOL justQuit = OMX_FALSE;
  57. static OMX_ERRORTYPE event_handler(
  58. OMX_HANDLETYPE hComponent,
  59. OMX_PTR pAppData,
  60. OMX_EVENTTYPE eEvent,
  61. OMX_U32 nData1,
  62. OMX_U32 nData2,
  63. OMX_PTR pEventData)
  64. {
  65. DecodeTestContext *pDecodeTestContext = (DecodeTestContext *)pAppData;
  66. switch (eEvent)
  67. {
  68. case OMX_EventPortSettingsChanged:
  69. {
  70. OMX_PARAM_PORTDEFINITIONTYPE pOutputPortDefinition;
  71. OMX_INIT_STRUCTURE(pOutputPortDefinition);
  72. pOutputPortDefinition.nPortIndex = 1;
  73. OMX_GetParameter(pDecodeTestContext->hComponentDecoder, OMX_IndexParamPortDefinition, &pOutputPortDefinition);
  74. OMX_U32 nOutputBufferSize = pOutputPortDefinition.nBufferSize;
  75. OMX_U32 nOutputBufferCount = pOutputPortDefinition.nBufferCountMin;
  76. printf("allocate %u output buffers size %u\r\n", nOutputBufferCount, nOutputBufferSize);
  77. printf("enable output port and alloc buffer\n");
  78. printf("======================================\r\n");
  79. printf("out put resolution [%dx%d]\r\n", pOutputPortDefinition.format.video.nFrameWidth, pOutputPortDefinition.format.video.nFrameHeight);
  80. printf("======================================\r\n");
  81. pDecodeTestContext->OutputWidth = pOutputPortDefinition.format.video.nFrameWidth;
  82. pDecodeTestContext->OutputHeight = pOutputPortDefinition.format.video.nFrameHeight;
  83. pDecodeTestContext->OutputStride = pOutputPortDefinition.format.video.nStride;
  84. pDecodeTestContext->OutputSlice = pOutputPortDefinition.format.video.nSliceHeight;
  85. OMX_SendCommand(pDecodeTestContext->hComponentDecoder, OMX_CommandPortEnable, 1, NULL);
  86. for (int i = 0; i < nOutputBufferCount; i++)
  87. {
  88. OMX_BUFFERHEADERTYPE *pBuffer = NULL;
  89. OMX_AllocateBuffer(hComponent, &pBuffer, 1, NULL, nOutputBufferSize);
  90. pDecodeTestContext->pOutputBufferArray[i] = pBuffer;
  91. OMX_FillThisBuffer(hComponent, pBuffer);
  92. }
  93. }
  94. break;
  95. case OMX_EventBufferFlag:
  96. {
  97. Message data;
  98. data.msg_type = 1;
  99. data.msg_flag = -1;
  100. if (msgsnd(pDecodeTestContext->msgid, (void *)&data, sizeof(data) - sizeof(data.msg_type), 0) == -1)
  101. {
  102. fprintf(stderr, "msgsnd failed\n");
  103. }
  104. }
  105. break;
  106. case OMX_EventCmdComplete:
  107. {
  108. switch ((OMX_COMMANDTYPE)(nData1))
  109. {
  110. case OMX_CommandStateSet:
  111. {
  112. pDecodeTestContext->comState = (OMX_STATETYPE)(nData2);
  113. }
  114. case OMX_CommandPortDisable:
  115. {
  116. if (nData2 == 1)
  117. disableEVnt = OMX_TRUE;
  118. }
  119. break;
  120. default:
  121. break;
  122. }
  123. }
  124. break;
  125. case OMX_EventError:
  126. {
  127. printf("receive err event %d %d\n", nData1, nData2);
  128. switch (nData1)
  129. {
  130. case OMX_ErrorInsufficientResources:
  131. printf("out for memory for buffer, stop decode!\n");
  132. break;
  133. case OMX_ErrorInvalidState:
  134. printf("decoder component into invalid state!\n");
  135. pDecodeTestContext->comState = OMX_StateInvalid;
  136. break;
  137. default:
  138. break;
  139. }
  140. Message data;
  141. data.msg_type = 1;
  142. data.msg_flag = -1;
  143. if (msgsnd(decodeTestContext->msgid, (void *)&data, sizeof(data) - sizeof(data.msg_type), 0) == -1)
  144. {
  145. fprintf(stderr, "msgsnd failed\n");
  146. }
  147. justQuit = OMX_TRUE;
  148. }
  149. break;
  150. default:
  151. break;
  152. }
  153. return OMX_ErrorNone;
  154. }
  155. static void help()
  156. {
  157. printf("video_dec_test - omx video hardware decode unit test case\r\n\r\n");
  158. printf("Usage:\r\n\r\n");
  159. printf("./video_dec_test -i <input file> input file\r\n");
  160. printf(" -o <output file> output file\r\n");
  161. printf(" -f <format> i420/nv12/nv21\r\n");
  162. printf(" --scaleW=<width> (optional) scale width down. ceil8(width/8) <= scaledW <= width\r\n");
  163. printf(" --scaleH=<heitht> (optional) scale height down, ceil8(height/8) <= scaledH <= height\r\n\r\n");
  164. printf("./video_dec_test --help: show this message\r\n");
  165. }
  166. static OMX_ERRORTYPE fill_output_buffer_done_handler(
  167. OMX_HANDLETYPE hComponent,
  168. OMX_PTR pAppData,
  169. OMX_BUFFERHEADERTYPE *pBuffer)
  170. {
  171. DecodeTestContext *pDecodeTestContext = (DecodeTestContext *)pAppData;
  172. Message data;
  173. data.msg_type = 1;
  174. if ((pBuffer->nFlags) & (OMX_BUFFERFLAG_EOS == OMX_BUFFERFLAG_EOS))
  175. {
  176. data.msg_flag = -1;
  177. }
  178. else
  179. {
  180. data.msg_flag = 1;
  181. data.pBuffer = pBuffer;
  182. }
  183. if (msgsnd(pDecodeTestContext->msgid, (void *)&data, sizeof(data) - sizeof(data.msg_type), 0) == -1)
  184. {
  185. fprintf(stderr, "msgsnd failed\n");
  186. }
  187. return OMX_ErrorNone;
  188. }
  189. static OMX_ERRORTYPE empty_buffer_done_handler(
  190. OMX_HANDLETYPE hComponent,
  191. OMX_PTR pAppData,
  192. OMX_BUFFERHEADERTYPE *pBuffer)
  193. {
  194. DecodeTestContext *pDecodeTestContext = (DecodeTestContext *)pAppData;
  195. Message data;
  196. data.msg_type = 1;
  197. data.msg_flag = 0;
  198. data.pBuffer = pBuffer;
  199. if (msgsnd(pDecodeTestContext->msgid, (void *)&data, sizeof(data) - sizeof(data.msg_type), 0) == -1)
  200. {
  201. fprintf(stderr, "msgsnd failed\n");
  202. }
  203. return OMX_ErrorNone;
  204. }
  205. static void signal_handle(int sig)
  206. {
  207. printf("[%s,%d]: receive sig=%d \n", __FUNCTION__, __LINE__, sig);
  208. Message data;
  209. data.msg_type = 1;
  210. data.msg_flag = -1;
  211. if (msgsnd(decodeTestContext->msgid, (void *)&data, sizeof(data) - sizeof(data.msg_type), 0) == -1)
  212. {
  213. fprintf(stderr, "msgsnd failed\n");
  214. }
  215. justQuit = OMX_TRUE;
  216. }
  217. static OMX_S32 FillInputBuffer(DecodeTestContext *decodeTestContext, OMX_BUFFERHEADERTYPE *pInputBuffer)
  218. {
  219. AVFormatContext *avFormatContext = decodeTestContext->avContext;
  220. AVPacket *avpacket;
  221. OMX_S32 error;
  222. avpacket = av_packet_alloc();
  223. error = av_read_frame(avFormatContext, avpacket);
  224. if (error < 0)
  225. {
  226. if (error == AVERROR_EOF || avFormatContext->pb->eof_reached == OMX_TRUE)
  227. {
  228. pInputBuffer->nFlags = 0x1;
  229. pInputBuffer->nFilledLen = 0;
  230. return 0;
  231. }
  232. else
  233. {
  234. printf("%s:%d failed to av_read_frame, error: %s\n",
  235. __FUNCTION__, __LINE__, av_err2str(error));
  236. return 0;
  237. }
  238. }
  239. pInputBuffer->nFlags = 0x10;
  240. pInputBuffer->nFilledLen = avpacket->size;
  241. if(!pInputBuffer->pBuffer || !avpacket->data)
  242. return 0;
  243. memcpy(pInputBuffer->pBuffer, avpacket->data, avpacket->size);
  244. return avpacket->size;
  245. }
  246. static OMX_S32 WriteOutputBuffer(DecodeTestContext *decodeTestContext, OMX_BUFFERHEADERTYPE *pInputBuffer, FILE *fb)
  247. {
  248. OMX_U32 i, j, PlaneCnt = 0;
  249. OMX_U32 srcWidth[3] = {0};
  250. OMX_U32 srcStride[3] = {0};
  251. OMX_U32 srcHeight[3] = {0};
  252. OMX_U32 srcSlice[3] = {0};
  253. OMX_U8 *pBuffer = pInputBuffer->pBuffer;
  254. switch (decodeTestContext->FormatType)
  255. {
  256. case OMX_COLOR_FormatYUV420Planar :
  257. PlaneCnt = 3;
  258. srcWidth[0] = decodeTestContext->OutputWidth;
  259. srcStride[0] = decodeTestContext->OutputStride;
  260. srcHeight[0] = decodeTestContext->OutputHeight;
  261. srcSlice[0] = decodeTestContext->OutputSlice;
  262. srcWidth[1] = srcWidth[2] = srcWidth[0] / 2;
  263. srcStride[1] = srcStride[2] = srcStride[0] / 2;
  264. srcHeight[1] = srcHeight[2] = srcHeight[0] / 2;
  265. srcSlice[1] = srcSlice[2] = srcSlice[0] /2 ;
  266. break;
  267. case OMX_COLOR_FormatYUV420SemiPlanar:
  268. case OMX_COLOR_FormatYVU420SemiPlanar:
  269. PlaneCnt = 2;
  270. srcWidth[0] = decodeTestContext->OutputWidth;
  271. srcStride[0] = decodeTestContext->OutputStride;
  272. srcHeight[0] = decodeTestContext->OutputHeight;
  273. srcSlice[0] = decodeTestContext->OutputSlice;
  274. srcWidth[1] = srcWidth[0];
  275. srcStride[1] = srcStride[0];
  276. srcHeight[1] = srcHeight[0] / 2;
  277. srcSlice[1] = srcSlice[0] / 2 ;
  278. break;
  279. default:
  280. return -1;
  281. }
  282. for (i = 0; i < PlaneCnt; i++)
  283. {
  284. if (srcStride[i] > srcWidth[i])
  285. {
  286. for (j = 0; j < srcHeight[i]; i++)
  287. fwrite(pBuffer + j * srcStride[i], 1, srcWidth[i], fb);
  288. }
  289. else
  290. {
  291. fwrite(pBuffer, 1, srcStride[i] * srcHeight[i], fb);
  292. }
  293. pBuffer += srcStride[i] * srcSlice[i];
  294. }
  295. return 0;
  296. }
  297. int main(int argc, char **argv)
  298. {
  299. printf("=============================\r\n");
  300. OMX_S32 error;
  301. FILE *fb = NULL;
  302. decodeTestContext = malloc(sizeof(DecodeTestContext));
  303. memset(decodeTestContext, 0, sizeof(DecodeTestContext));
  304. OMX_S32 msgid = -1;
  305. msgid = msgget(IPC_PRIVATE, 0666 | IPC_CREAT);
  306. if (msgid < 0)
  307. {
  308. perror("get ipc_id error");
  309. return -1;
  310. }
  311. decodeTestContext->msgid = msgid;
  312. struct option longOpt[] = {
  313. {"output", required_argument, NULL, 'o'},
  314. {"input", required_argument, NULL, 'i'},
  315. {"format", required_argument, NULL, 'f'},
  316. {"scaleW", required_argument, NULL, 'w'},
  317. {"scaleH", required_argument, NULL, 'h'},
  318. {"normal", no_argument, NULL, 'n'},
  319. {"help", no_argument, NULL, '0'},
  320. {NULL, no_argument, NULL, 0},
  321. };
  322. char *shortOpt = "i:o:f:w:h:n";
  323. OMX_U32 c;
  324. OMX_S32 l;
  325. if (argc == 0)
  326. {
  327. help();
  328. return -1;
  329. }
  330. while ((c = getopt_long(argc, argv, shortOpt, longOpt, (int *)&l)) != -1)
  331. {
  332. switch (c)
  333. {
  334. case 'i':
  335. printf("input: %s\r\n", optarg);
  336. if (access(optarg, R_OK) != -1)
  337. {
  338. memcpy(decodeTestContext->sInputFilePath, optarg, strlen(optarg));
  339. }
  340. else
  341. {
  342. printf("input file not exist!\r\n");
  343. return -1;
  344. }
  345. break;
  346. case 'o':
  347. printf("output: %s\r\n", optarg);
  348. memcpy(decodeTestContext->sOutputFilePath, optarg, strlen(optarg));
  349. break;
  350. case 'f':
  351. printf("format: %s\r\n", optarg);
  352. memcpy(decodeTestContext->sOutputFormat, optarg, strlen(optarg));
  353. break;
  354. case 'w':
  355. printf("ScaleWidth: %s\r\n", optarg);
  356. decodeTestContext->ScaleWidth = atoi(optarg);
  357. break;
  358. case 'h':
  359. printf("ScaleHeight: %s\r\n", optarg);
  360. decodeTestContext->ScaleHeight = atoi(optarg);
  361. break;
  362. case 'n':
  363. printf("use normal buffer\r\n");
  364. useNormal = OMX_TRUE;
  365. break;
  366. case '0':
  367. default:
  368. help();
  369. return -1;
  370. }
  371. }
  372. if (decodeTestContext == NULL)
  373. {
  374. help();
  375. return -1;
  376. }
  377. /*ffmpeg init*/
  378. printf("init ffmpeg\r\n");
  379. AVFormatContext *avContext = NULL;
  380. AVCodecParameters *codecParameters = NULL;
  381. AVInputFormat *fmt = NULL;
  382. OMX_S32 videoIndex;
  383. if ((avContext = avformat_alloc_context()) == NULL)
  384. {
  385. printf("avformat_alloc_context fail\r\n");
  386. return -1;
  387. }
  388. avContext->flags |= AV_CODEC_FLAG_TRUNCATED;
  389. printf("avformat_open_input\r\n");
  390. if ((error = avformat_open_input(&avContext, decodeTestContext->sInputFilePath, fmt, NULL)))
  391. {
  392. printf("%s:%d failed to av_open_input_file error(%s), %s\n",
  393. __FILE__, __LINE__, av_err2str(error), decodeTestContext->sInputFilePath);
  394. avformat_free_context(avContext);
  395. return -1;
  396. }
  397. printf("avformat_find_stream_info\r\n");
  398. if ((error = avformat_find_stream_info(avContext, NULL)) < 0)
  399. {
  400. printf("%s:%d failed to avformat_find_stream_info. error(%s)\n",
  401. __FUNCTION__, __LINE__, av_err2str(error));
  402. avformat_close_input(&avContext);
  403. avformat_free_context(avContext);
  404. return -1;
  405. }
  406. printf("av_find_best_stream\r\n");
  407. videoIndex = av_find_best_stream(avContext, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
  408. if (videoIndex < 0)
  409. {
  410. printf("%s:%d failed to av_find_best_stream.\n", __FUNCTION__, __LINE__);
  411. return -1;
  412. }
  413. printf("video index = %d\r\n", videoIndex);
  414. decodeTestContext->avContext = avContext;
  415. /*get video info*/
  416. codecParameters = avContext->streams[videoIndex]->codecpar;
  417. printf("codec_id = %d, width = %d, height = %d\r\n", (int)codecParameters->codec_id,
  418. codecParameters->width, codecParameters->height);
  419. /*omx init*/
  420. OMX_HANDLETYPE hComponentDecoder = NULL;
  421. OMX_CALLBACKTYPE callbacks;
  422. int ret = OMX_ErrorNone;
  423. signal(SIGINT, signal_handle);
  424. printf("init omx\r\n");
  425. ret = OMX_Init();
  426. if (ret != OMX_ErrorNone)
  427. {
  428. printf("[%s,%d]: run OMX_Init failed. ret is %d \n", __FUNCTION__, __LINE__, ret);
  429. avformat_close_input(&avContext);
  430. avformat_free_context(avContext);
  431. return 1;
  432. }
  433. callbacks.EventHandler = event_handler;
  434. callbacks.FillBufferDone = fill_output_buffer_done_handler;
  435. callbacks.EmptyBufferDone = empty_buffer_done_handler;
  436. printf("get handle\r\n");
  437. if (codecParameters->codec_id == AV_CODEC_ID_H264)
  438. {
  439. if (useNormal)
  440. OMX_GetHandle(&hComponentDecoder, "OMX.sf.video_decoder.avc", decodeTestContext, &callbacks);
  441. else
  442. OMX_GetHandle(&hComponentDecoder, "OMX.sf.video_decoder.avc.internal", decodeTestContext, &callbacks);
  443. }
  444. else if (codecParameters->codec_id == AV_CODEC_ID_HEVC)
  445. {
  446. if (useNormal)
  447. OMX_GetHandle(&hComponentDecoder, "OMX.sf.video_decoder.hevc", decodeTestContext, &callbacks);
  448. else
  449. OMX_GetHandle(&hComponentDecoder, "OMX.sf.video_decoder.hevc.internal", decodeTestContext, &callbacks);
  450. }
  451. if (hComponentDecoder == NULL)
  452. {
  453. printf("could not get handle\r\n");
  454. OMX_Deinit();
  455. avformat_close_input(&avContext);
  456. avformat_free_context(avContext);
  457. return 0;
  458. }
  459. decodeTestContext->hComponentDecoder = hComponentDecoder;
  460. OMX_PARAM_PORTDEFINITIONTYPE pInputPortDefinition;
  461. OMX_INIT_STRUCTURE(pInputPortDefinition);
  462. pInputPortDefinition.nPortIndex = 0;
  463. OMX_GetParameter(hComponentDecoder, OMX_IndexParamPortDefinition, &pInputPortDefinition);
  464. pInputPortDefinition.format.video.nFrameWidth = codecParameters->width;
  465. pInputPortDefinition.format.video.nFrameHeight = codecParameters->height;
  466. OMX_SetParameter(hComponentDecoder, OMX_IndexParamPortDefinition, &pInputPortDefinition);
  467. OMX_PARAM_PORTDEFINITIONTYPE pOutputPortDefinition;
  468. OMX_INIT_STRUCTURE(pOutputPortDefinition);
  469. pOutputPortDefinition.nPortIndex = 1;
  470. OMX_GetParameter(decodeTestContext->hComponentDecoder, OMX_IndexParamPortDefinition, &pOutputPortDefinition);
  471. pOutputPortDefinition.format.video.nFrameWidth
  472. = decodeTestContext->ScaleWidth? decodeTestContext->ScaleWidth:codecParameters->width;
  473. pOutputPortDefinition.format.video.nFrameHeight
  474. = decodeTestContext->ScaleHeight? decodeTestContext->ScaleHeight:codecParameters->height;
  475. if (strstr(decodeTestContext->sOutputFormat, "nv12") != NULL)
  476. {
  477. pOutputPortDefinition.format.video.eColorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
  478. }
  479. else if (strstr(decodeTestContext->sOutputFormat, "nv21") != NULL)
  480. {
  481. pOutputPortDefinition.format.video.eColorFormat = OMX_COLOR_FormatYVU420SemiPlanar;
  482. }
  483. else if (strstr(decodeTestContext->sOutputFormat, "i420") != NULL)
  484. {
  485. pOutputPortDefinition.format.video.eColorFormat = OMX_COLOR_FormatYUV420Planar;
  486. }
  487. else
  488. {
  489. printf("Unsupported color format!\r\n");
  490. goto end;
  491. }
  492. decodeTestContext->FormatType = pOutputPortDefinition.format.video.eColorFormat;
  493. OMX_SetParameter(hComponentDecoder, OMX_IndexParamPortDefinition, &pOutputPortDefinition);
  494. disableEVnt = OMX_FALSE;
  495. OMX_SendCommand(hComponentDecoder, OMX_CommandPortDisable, 1, NULL);
  496. printf("wait for output port disable\r\n");
  497. while (!disableEVnt && !justQuit);
  498. if (justQuit)
  499. goto end;
  500. printf("output port disabled\r\n");
  501. OMX_SendCommand(hComponentDecoder, OMX_CommandStateSet, OMX_StateIdle, NULL);
  502. /*Alloc input buffer*/
  503. OMX_U32 nInputWidth = codecParameters->width;
  504. OMX_U32 nInputHeight = codecParameters->height;
  505. OMX_U32 nInputBufferSize = nInputWidth * nInputHeight * 2;
  506. OMX_U32 nInputBufferCount = pInputPortDefinition.nBufferCountActual;
  507. for (int i = 0; i < nInputBufferCount; i++)
  508. {
  509. OMX_BUFFERHEADERTYPE *pBuffer = NULL;
  510. OMX_AllocateBuffer(hComponentDecoder, &pBuffer, 0, NULL, nInputBufferSize);
  511. decodeTestContext->pInputBufferArray[i] = pBuffer;
  512. }
  513. printf("wait for Component idle\r\n");
  514. while (decodeTestContext->comState != OMX_StateIdle && !justQuit);
  515. if (justQuit)
  516. goto end;
  517. printf("Component in idle\r\n");
  518. for (int i = 0; i < nInputBufferCount; i++)
  519. {
  520. /*Fill Input Buffer*/
  521. FillInputBuffer(decodeTestContext, decodeTestContext->pInputBufferArray[i]);
  522. OMX_EmptyThisBuffer(hComponentDecoder, decodeTestContext->pInputBufferArray[i]);
  523. }
  524. fb = fopen(decodeTestContext->sOutputFilePath, "wb+");
  525. if (!fb)
  526. {
  527. fprintf(stderr, "output file open err or no output file patch %d\n", errno);
  528. goto end;
  529. }
  530. printf("start process\r\n");
  531. OMX_SendCommand(hComponentDecoder, OMX_CommandStateSet, OMX_StateExecuting, NULL);
  532. /*wait until decode finished*/
  533. Message data;
  534. while (OMX_TRUE)
  535. {
  536. if (msgrcv(msgid, (void *)&data, BUFSIZ, 0, 0) == -1)
  537. {
  538. fprintf(stderr, "msgrcv failed with errno: %d\n", errno);
  539. goto end;
  540. }
  541. switch (data.msg_flag)
  542. {
  543. case 0:
  544. {
  545. OMX_BUFFERHEADERTYPE *pBuffer = data.pBuffer;
  546. FillInputBuffer(decodeTestContext, pBuffer);
  547. OMX_EmptyThisBuffer(decodeTestContext->hComponentDecoder, pBuffer);
  548. }
  549. break;
  550. case 1:
  551. {
  552. OMX_BUFFERHEADERTYPE *pBuffer = data.pBuffer;
  553. WriteOutputBuffer(decodeTestContext, pBuffer, fb);
  554. if ((pBuffer->nFlags) & (OMX_BUFFERFLAG_EOS == OMX_BUFFERFLAG_EOS))
  555. {
  556. goto end;
  557. }
  558. else
  559. {
  560. OMX_FillThisBuffer(decodeTestContext->hComponentDecoder, pBuffer);
  561. }
  562. }
  563. break;
  564. case 2:
  565. break;
  566. default:
  567. goto end;
  568. }
  569. }
  570. end:
  571. /*free resource*/
  572. if (decodeTestContext->comState == OMX_StateExecuting)
  573. {
  574. OMX_SendCommand(hComponentDecoder, OMX_CommandStateSet, OMX_StateIdle, NULL);
  575. printf("wait for Component idle\r\n");
  576. while (decodeTestContext->comState != OMX_StateIdle);
  577. printf("Component in idle\r\n");
  578. }
  579. OMX_FreeHandle(hComponentDecoder);
  580. OMX_Deinit();
  581. if (fb)
  582. fclose(fb);
  583. avformat_close_input(&avContext);
  584. avformat_free_context(avContext);
  585. }