mjpeg_dec_test.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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 <getopt.h>
  8. #include <fcntl.h>
  9. #include "libavformat/avformat.h"
  10. #include <OMX_Image.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[64];
  37. char sInputFilePath[64];
  38. char sOutputFormat[64];
  39. OMX_BUFFERHEADERTYPE *pInputBufferArray[64];
  40. OMX_BUFFERHEADERTYPE *pOutputBufferArray[64];
  41. AVFormatContext *avContext;
  42. int msgid;
  43. OMX_S32 RoiLeft;
  44. OMX_S32 RoiTop;
  45. OMX_U32 RoiWidth;
  46. OMX_U32 RoiHeight;
  47. OMX_U32 Rotation;
  48. OMX_U32 Mirror;
  49. OMX_U32 ScaleFactorH;
  50. OMX_U32 ScaleFactorV;
  51. } DecodeTestContext;
  52. DecodeTestContext *decodeTestContext;
  53. static OMX_S32 FillInputBuffer(DecodeTestContext *decodeTestContext, OMX_BUFFERHEADERTYPE *pInputBuffer);
  54. static OMX_ERRORTYPE event_handler(
  55. OMX_HANDLETYPE hComponent,
  56. OMX_PTR pAppData,
  57. OMX_EVENTTYPE eEvent,
  58. OMX_U32 nData1,
  59. OMX_U32 nData2,
  60. OMX_PTR pEventData)
  61. {
  62. DecodeTestContext *pDecodeTestContext = (DecodeTestContext *)pAppData;
  63. switch (eEvent)
  64. {
  65. case OMX_EventPortSettingsChanged:
  66. {
  67. OMX_PARAM_PORTDEFINITIONTYPE pOutputPortDefinition;
  68. OMX_INIT_STRUCTURE(pOutputPortDefinition);
  69. pOutputPortDefinition.nPortIndex = 1;
  70. OMX_GetParameter(pDecodeTestContext->hComponentDecoder, OMX_IndexParamPortDefinition, &pOutputPortDefinition);
  71. OMX_U32 nOutputBufferSize = pOutputPortDefinition.nBufferSize;
  72. OMX_U32 nOutputBufferCount = pOutputPortDefinition.nBufferCountMin;
  73. printf("allocate %d output buffers size %d\r\n", nOutputBufferCount, nOutputBufferSize);
  74. for (int i = 0; i < nOutputBufferCount; i++)
  75. {
  76. OMX_BUFFERHEADERTYPE *pBuffer = NULL;
  77. OMX_AllocateBuffer(hComponent, &pBuffer, 1, NULL, nOutputBufferSize);
  78. pDecodeTestContext->pOutputBufferArray[i] = pBuffer;
  79. OMX_FillThisBuffer(hComponent, pBuffer);
  80. }
  81. }
  82. break;
  83. case OMX_EventBufferFlag:
  84. {
  85. Message data;
  86. data.msg_type = 1;
  87. data.msg_flag = -1;
  88. if (msgsnd(pDecodeTestContext->msgid, (void *)&data, sizeof(data) - sizeof(data.msg_type), 0) == -1)
  89. {
  90. fprintf(stderr, "msgsnd failed\n");
  91. }
  92. }
  93. break;
  94. default:
  95. break;
  96. }
  97. return OMX_ErrorNone;
  98. }
  99. static void help()
  100. {
  101. printf("./omx_mjpeg_dec_test -i <input file> -o <output file> \r\n");
  102. }
  103. static OMX_ERRORTYPE fill_output_buffer_done_handler(
  104. OMX_HANDLETYPE hComponent,
  105. OMX_PTR pAppData,
  106. OMX_BUFFERHEADERTYPE *pBuffer)
  107. {
  108. DecodeTestContext *pDecodeTestContext = (DecodeTestContext *)pAppData;
  109. Message data;
  110. data.msg_type = 1;
  111. if ((pBuffer->nFlags) & (OMX_BUFFERFLAG_EOS == OMX_BUFFERFLAG_EOS))
  112. {
  113. data.msg_flag = -1;
  114. }
  115. else
  116. {
  117. data.msg_flag = 1;
  118. data.pBuffer = pBuffer;
  119. }
  120. if (msgsnd(pDecodeTestContext->msgid, (void *)&data, sizeof(data) - sizeof(data.msg_type), 0) == -1)
  121. {
  122. fprintf(stderr, "msgsnd failed\n");
  123. }
  124. return OMX_ErrorNone;
  125. }
  126. static OMX_ERRORTYPE empty_buffer_done_handler(
  127. OMX_HANDLETYPE hComponent,
  128. OMX_PTR pAppData,
  129. OMX_BUFFERHEADERTYPE *pBuffer)
  130. {
  131. DecodeTestContext *pDecodeTestContext = (DecodeTestContext *)pAppData;
  132. Message data;
  133. data.msg_type = 1;
  134. data.msg_flag = 0;
  135. data.pBuffer = pBuffer;
  136. if (msgsnd(pDecodeTestContext->msgid, (void *)&data, sizeof(data) - sizeof(data.msg_type), 0) == -1)
  137. {
  138. fprintf(stderr, "msgsnd failed\n");
  139. }
  140. return OMX_ErrorNone;
  141. }
  142. static void signal_handle(int sig)
  143. {
  144. printf("[%s,%d]: receive sig=%d \n", __FUNCTION__, __LINE__, sig);
  145. Message data;
  146. data.msg_type = 1;
  147. data.msg_flag = -1;
  148. if (msgsnd(decodeTestContext->msgid, (void *)&data, sizeof(data) - sizeof(data.msg_type), 0) == -1)
  149. {
  150. fprintf(stderr, "msgsnd failed\n");
  151. }
  152. }
  153. static OMX_S32 FillInputBuffer(DecodeTestContext *decodeTestContext, OMX_BUFFERHEADERTYPE *pInputBuffer)
  154. {
  155. AVFormatContext *avFormatContext = decodeTestContext->avContext;
  156. AVPacket avpacket;
  157. OMX_S32 error;
  158. av_init_packet(&avpacket);
  159. error = av_read_frame(avFormatContext, &avpacket);
  160. if (error < 0)
  161. {
  162. if (error == AVERROR_EOF || avFormatContext->pb->eof_reached == OMX_TRUE)
  163. {
  164. pInputBuffer->nFlags = 0x1;
  165. pInputBuffer->nFilledLen = 0;
  166. return 0;
  167. }
  168. else
  169. {
  170. printf("%s:%d failed to av_read_frame error(0x%08x)\n", __FUNCTION__, __LINE__, error);
  171. return 0;
  172. }
  173. }
  174. pInputBuffer->nFlags = 0x10;
  175. pInputBuffer->nFilledLen = avpacket.size;
  176. memcpy(pInputBuffer->pBuffer, avpacket.data, avpacket.size);
  177. printf("%s, address = %p, size = %d, flag = %x\r\n",
  178. __FUNCTION__, pInputBuffer->pBuffer, pInputBuffer->nFilledLen, pInputBuffer->nFlags);
  179. return avpacket.size;
  180. }
  181. int main(int argc, char **argv)
  182. {
  183. printf("=============================\r\n");
  184. OMX_S32 error;
  185. decodeTestContext = malloc(sizeof(DecodeTestContext));
  186. memset(decodeTestContext, 0, sizeof(DecodeTestContext));
  187. OMX_S32 msgid = -1;
  188. msgid = msgget(IPC_PRIVATE, 0666 | IPC_CREAT);
  189. if (msgid < 0)
  190. {
  191. perror("get ipc_id error");
  192. return -1;
  193. }
  194. decodeTestContext->msgid = msgid;
  195. struct option longOpt[] = {
  196. {"output", required_argument, NULL, 'o'},
  197. {"input", required_argument, NULL, 'i'},
  198. {"format", required_argument, NULL, 'f'},
  199. {"roi", required_argument, NULL, 'c'},
  200. {"rotation", required_argument, NULL, 'r'},
  201. {"mirror", required_argument, NULL, 'm'},
  202. {"scaleH", required_argument, NULL, 'H'},
  203. {"scaleV", required_argument, NULL, 'V'},
  204. {NULL, no_argument, NULL, 0},
  205. };
  206. char *shortOpt = "i:o:f:c:r:m:";
  207. OMX_U32 c;
  208. OMX_S32 l;
  209. OMX_STRING val;
  210. // if (argc == 0)
  211. // {
  212. // help();
  213. // return;
  214. // }
  215. while ((c = getopt_long(argc, argv, shortOpt, longOpt, (int *)&l)) != -1)
  216. {
  217. switch (c)
  218. {
  219. case 'i':
  220. printf("input: %s\r\n", optarg);
  221. if (access(optarg, R_OK) != -1)
  222. {
  223. memcpy(decodeTestContext->sInputFilePath, optarg, strlen(optarg));
  224. }
  225. else
  226. {
  227. printf("input file not exist!\r\n");
  228. return -1;
  229. }
  230. break;
  231. case 'o':
  232. printf("output: %s\r\n", optarg);
  233. memcpy(decodeTestContext->sOutputFilePath, optarg, strlen(optarg));
  234. break;
  235. case 'f':
  236. printf("format: %s\r\n", optarg);
  237. memcpy(decodeTestContext->sOutputFormat, optarg, strlen(optarg));
  238. break;
  239. case 'c':
  240. val = strtok(optarg, ",");
  241. if (val == NULL) {
  242. printf("Invalid ROI option: %s\n", optarg);
  243. return -1;
  244. }
  245. else {
  246. decodeTestContext->RoiLeft = atoi(val);
  247. }
  248. val = strtok(NULL, ",");
  249. if (val == NULL) {
  250. printf("Invalid ROI option: %s\n", optarg);
  251. return -1;
  252. }
  253. else {
  254. decodeTestContext->RoiTop = atoi(val);
  255. }
  256. val = strtok(NULL, ",");
  257. if (val == NULL) {
  258. printf("Invalid ROI option: %s\n", optarg);
  259. return -1;
  260. }
  261. else {
  262. decodeTestContext->RoiWidth = atoi(val);
  263. }
  264. val = strtok(NULL, ",");
  265. if (val == NULL) {
  266. printf("Invalid ROI option: %s\n", optarg);
  267. return -1;
  268. }
  269. else {
  270. decodeTestContext->RoiHeight = atoi(val);
  271. }
  272. break;
  273. case 'r':
  274. decodeTestContext->Rotation = atoi(optarg);
  275. break;
  276. case 'm':
  277. decodeTestContext->Mirror = atoi(optarg);
  278. break;
  279. case 'H':
  280. decodeTestContext->ScaleFactorH = atoi(optarg);
  281. break;
  282. case 'V':
  283. decodeTestContext->ScaleFactorV = atoi(optarg);
  284. break;
  285. case 'h':
  286. default:
  287. help();
  288. return -1;
  289. }
  290. }
  291. if (decodeTestContext->sInputFilePath == NULL || decodeTestContext->sOutputFilePath == NULL)
  292. {
  293. help();
  294. return -1;
  295. }
  296. /*ffmpeg init*/
  297. printf("init ffmpeg\r\n");
  298. AVFormatContext *avContext = NULL;
  299. AVCodecParameters *codecParameters = NULL;
  300. AVInputFormat *fmt = NULL;
  301. OMX_S32 imageIndex;
  302. if ((avContext = avformat_alloc_context()) == NULL)
  303. {
  304. printf("avformat_alloc_context fail\r\n");
  305. return -1;
  306. }
  307. avContext->flags |= AV_CODEC_FLAG_TRUNCATED;
  308. printf("avformat_open_input\r\n");
  309. if ((error = avformat_open_input(&avContext, decodeTestContext->sInputFilePath, fmt, NULL)))
  310. {
  311. printf("%s:%d failed to av_open_input_file error(%d), %s\n",
  312. __FILE__, __LINE__, error, decodeTestContext->sInputFilePath);
  313. return -1;
  314. }
  315. printf("avformat_find_stream_info\r\n");
  316. if ((error = avformat_find_stream_info(avContext, NULL)) < 0)
  317. {
  318. printf("%s:%d failed to avformat_find_stream_info. error(%d)\n",
  319. __FUNCTION__, __LINE__, error);
  320. return -1;
  321. }
  322. printf("av_find_best_stream\r\n");
  323. imageIndex = av_find_best_stream(avContext, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
  324. if (imageIndex < 0)
  325. {
  326. printf("%s:%d failed to av_find_best_stream.\n", __FUNCTION__, __LINE__);
  327. return -1;
  328. }
  329. printf("image index = %d\r\n", imageIndex);
  330. decodeTestContext->avContext = avContext;
  331. /*get image info*/
  332. codecParameters = avContext->streams[imageIndex]->codecpar;
  333. printf("codec_id = %d, width = %d, height = %d\r\n", (int)codecParameters->codec_id,
  334. codecParameters->width, codecParameters->height);
  335. /*omx init*/
  336. OMX_HANDLETYPE hComponentDecoder = NULL;
  337. OMX_CALLBACKTYPE callbacks;
  338. int ret = OMX_ErrorNone;
  339. signal(SIGINT, signal_handle);
  340. printf("init omx\r\n");
  341. ret = OMX_Init();
  342. if (ret != OMX_ErrorNone)
  343. {
  344. printf("[%s,%d]: run OMX_Init failed. ret is %d \n", __FUNCTION__, __LINE__, ret);
  345. return 1;
  346. }
  347. callbacks.EventHandler = event_handler;
  348. callbacks.FillBufferDone = fill_output_buffer_done_handler;
  349. callbacks.EmptyBufferDone = empty_buffer_done_handler;
  350. printf("get handle by codec id = %d\r\n", codecParameters->codec_id);
  351. if (codecParameters->codec_id == AV_CODEC_ID_MJPEG)
  352. {
  353. OMX_GetHandle(&hComponentDecoder, "sf.dec.decoder.mjpeg", decodeTestContext, &callbacks);
  354. }
  355. if (hComponentDecoder == NULL)
  356. {
  357. printf("could not get handle\r\n");
  358. return 0;
  359. }
  360. decodeTestContext->hComponentDecoder = hComponentDecoder;
  361. OMX_PARAM_PORTDEFINITIONTYPE pOutputPortDefinition;
  362. OMX_INIT_STRUCTURE(pOutputPortDefinition);
  363. pOutputPortDefinition.nPortIndex = 1;
  364. OMX_GetParameter(decodeTestContext->hComponentDecoder, OMX_IndexParamPortDefinition, &pOutputPortDefinition);
  365. pOutputPortDefinition.format.video.nFrameWidth = codecParameters->width;
  366. pOutputPortDefinition.format.video.nFrameHeight = codecParameters->height;
  367. if (strstr(decodeTestContext->sOutputFormat, "nv12") != NULL)
  368. {
  369. pOutputPortDefinition.format.video.eColorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
  370. }
  371. else if (strstr(decodeTestContext->sOutputFormat, "nv21") != NULL)
  372. {
  373. pOutputPortDefinition.format.video.eColorFormat = OMX_COLOR_FormatYVU420SemiPlanar;
  374. }
  375. else if (strstr(decodeTestContext->sOutputFormat, "i420") != NULL)
  376. {
  377. pOutputPortDefinition.format.video.eColorFormat = OMX_COLOR_FormatYUV420Planar;
  378. }
  379. else if (strstr(decodeTestContext->sOutputFormat, "i422") != NULL)
  380. {
  381. pOutputPortDefinition.format.video.eColorFormat = OMX_COLOR_FormatYUV422Planar;
  382. }
  383. else if (strstr(decodeTestContext->sOutputFormat, "nv16") != NULL)
  384. {
  385. pOutputPortDefinition.format.video.eColorFormat = OMX_COLOR_FormatYUV422SemiPlanar;
  386. }
  387. else if (strstr(decodeTestContext->sOutputFormat, "nv61") != NULL)
  388. {
  389. pOutputPortDefinition.format.video.eColorFormat = OMX_COLOR_FormatYVU422SemiPlanar;
  390. }
  391. else if (strstr(decodeTestContext->sOutputFormat, "yuyv") != NULL)
  392. {
  393. pOutputPortDefinition.format.video.eColorFormat = OMX_COLOR_FormatYCbYCr;
  394. }
  395. else if (strstr(decodeTestContext->sOutputFormat, "yvyu") != NULL)
  396. {
  397. pOutputPortDefinition.format.video.eColorFormat = OMX_COLOR_FormatYCrYCb;
  398. }
  399. else if (strstr(decodeTestContext->sOutputFormat, "uyvy") != NULL)
  400. {
  401. pOutputPortDefinition.format.video.eColorFormat = OMX_COLOR_FormatCbYCrY;
  402. }
  403. else if (strstr(decodeTestContext->sOutputFormat, "vyuy") != NULL)
  404. {
  405. pOutputPortDefinition.format.video.eColorFormat = OMX_COLOR_FormatCrYCbY;
  406. }
  407. else if (strstr(decodeTestContext->sOutputFormat, "i444") != NULL)
  408. {
  409. pOutputPortDefinition.format.video.eColorFormat = OMX_COLOR_FormatYUV444Planar;
  410. }
  411. else if (strstr(decodeTestContext->sOutputFormat, "yuv444packed") != NULL)
  412. {
  413. pOutputPortDefinition.format.video.eColorFormat = OMX_COLOR_FormatYUV444Interleaved;
  414. }
  415. else
  416. {
  417. printf("Unsupported color format!\r\n");
  418. goto end;
  419. }
  420. OMX_SetParameter(hComponentDecoder, OMX_IndexParamPortDefinition, &pOutputPortDefinition);
  421. /* Set Roi config setting*/
  422. if(decodeTestContext->RoiWidth && decodeTestContext->RoiHeight)
  423. {
  424. OMX_CONFIG_RECTTYPE RectConfig;
  425. OMX_INIT_STRUCTURE(RectConfig);
  426. RectConfig.nPortIndex = 1;
  427. OMX_GetConfig(hComponentDecoder, OMX_IndexConfigCommonOutputCrop, &RectConfig);
  428. RectConfig.nLeft = decodeTestContext->RoiLeft;
  429. RectConfig.nTop = decodeTestContext->RoiTop;
  430. RectConfig.nWidth = decodeTestContext->RoiWidth;
  431. RectConfig.nHeight = decodeTestContext->RoiHeight;
  432. OMX_SetConfig(hComponentDecoder, OMX_IndexConfigCommonOutputCrop, &RectConfig);
  433. }
  434. /* Set Rotation config setting*/
  435. if(decodeTestContext->Rotation)
  436. {
  437. OMX_CONFIG_ROTATIONTYPE RotatConfig;
  438. OMX_INIT_STRUCTURE(RotatConfig);
  439. RotatConfig.nPortIndex = 1;
  440. OMX_GetConfig(hComponentDecoder, OMX_IndexConfigCommonRotate, &RotatConfig);
  441. RotatConfig.nRotation = decodeTestContext->Rotation;
  442. OMX_SetConfig(hComponentDecoder, OMX_IndexConfigCommonRotate, &RotatConfig);
  443. }
  444. /* Set Mirror config setting*/
  445. if(decodeTestContext->Mirror)
  446. {
  447. OMX_CONFIG_MIRRORTYPE MirrorConfig;
  448. OMX_INIT_STRUCTURE(MirrorConfig);
  449. MirrorConfig.nPortIndex = 1;
  450. OMX_GetConfig(hComponentDecoder, OMX_IndexConfigCommonMirror, &MirrorConfig);
  451. MirrorConfig.eMirror = decodeTestContext->Mirror;
  452. OMX_SetConfig(hComponentDecoder, OMX_IndexConfigCommonMirror, &MirrorConfig);
  453. }
  454. /* Set Scale config setting*/
  455. if(decodeTestContext->ScaleFactorH || decodeTestContext->ScaleFactorV)
  456. {
  457. OMX_CONFIG_SCALEFACTORTYPE ScaleConfig;
  458. OMX_INIT_STRUCTURE(ScaleConfig);
  459. ScaleConfig.nPortIndex = 1;
  460. OMX_GetConfig(hComponentDecoder, OMX_IndexConfigCommonScale, &ScaleConfig);
  461. /* in Q16 format */
  462. ScaleConfig.xWidth = (1 << 16) >> (decodeTestContext->ScaleFactorH & 0x3);
  463. ScaleConfig.xHeight = (1 << 16) >> (decodeTestContext->ScaleFactorV & 0x3);
  464. OMX_SetConfig(hComponentDecoder, OMX_IndexConfigCommonScale, &ScaleConfig);
  465. }
  466. OMX_SendCommand(hComponentDecoder, OMX_CommandStateSet, OMX_StateIdle, NULL);
  467. OMX_PARAM_PORTDEFINITIONTYPE pInputPortDefinition;
  468. OMX_INIT_STRUCTURE(pInputPortDefinition);
  469. pInputPortDefinition.nPortIndex = 0;
  470. OMX_GetParameter(hComponentDecoder, OMX_IndexParamPortDefinition, &pInputPortDefinition);
  471. pInputPortDefinition.format.image.nFrameWidth = codecParameters->width;
  472. pInputPortDefinition.format.image.nFrameHeight = codecParameters->height;
  473. OMX_SetParameter(hComponentDecoder, OMX_IndexParamPortDefinition, &pInputPortDefinition);
  474. /*Alloc input buffer*/
  475. OMX_U32 nInputWidth = codecParameters->width;
  476. OMX_U32 nInputHeight = codecParameters->height;
  477. OMX_U32 nInputBufferSize = nInputWidth * nInputHeight * 2;
  478. OMX_U32 nInputBufferCount = pInputPortDefinition.nBufferCountActual;
  479. for (int i = 0; i < nInputBufferCount; i++)
  480. {
  481. OMX_BUFFERHEADERTYPE *pBuffer = NULL;
  482. OMX_AllocateBuffer(hComponentDecoder, &pBuffer, 0, NULL, nInputBufferSize);
  483. decodeTestContext->pInputBufferArray[i] = pBuffer;
  484. /*Fill Input Buffer*/
  485. FillInputBuffer(decodeTestContext, pBuffer);
  486. OMX_EmptyThisBuffer(hComponentDecoder, pBuffer);
  487. }
  488. OMX_SendCommand(hComponentDecoder, OMX_CommandStateSet, OMX_StateExecuting, NULL);
  489. /*wait until decode finished*/
  490. Message data;
  491. while (OMX_TRUE)
  492. {
  493. if (msgrcv(msgid, (void *)&data, BUFSIZ, 0, 0) == -1)
  494. {
  495. fprintf(stderr, "msgrcv failed with errno: %d\n", errno);
  496. goto end;
  497. }
  498. switch (data.msg_flag)
  499. {
  500. case 0:
  501. {
  502. OMX_BUFFERHEADERTYPE *pBuffer = data.pBuffer;
  503. FillInputBuffer(decodeTestContext, pBuffer);
  504. OMX_EmptyThisBuffer(decodeTestContext->hComponentDecoder, pBuffer);
  505. }
  506. break;
  507. case 1:
  508. {
  509. OMX_BUFFERHEADERTYPE *pBuffer = data.pBuffer;
  510. OMX_STRING sFilePath = decodeTestContext->sOutputFilePath;
  511. printf("write %d buffers to file\r\n", pBuffer->nFilledLen);
  512. FILE *fb = fopen(sFilePath, "ab+");
  513. size_t size = fwrite(pBuffer->pBuffer, 1, pBuffer->nFilledLen, fb);
  514. int error = ferror(fb);
  515. printf("write error = %d\r\n", error);
  516. fclose(fb);
  517. printf("write %d buffers finish\r\n", size);
  518. if ((pBuffer->nFlags) & (OMX_BUFFERFLAG_EOS == OMX_BUFFERFLAG_EOS))
  519. {
  520. goto end;
  521. }
  522. else
  523. {
  524. OMX_FillThisBuffer(decodeTestContext->hComponentDecoder, pBuffer);
  525. }
  526. }
  527. break;
  528. case 2:
  529. break;
  530. default:
  531. goto end;
  532. }
  533. }
  534. end:
  535. /*free resource*/
  536. OMX_FreeHandle(hComponentDecoder);
  537. OMX_Deinit();
  538. }