mjpeg_dec_test.c 20 KB

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