mjpeg_dec_test.c 23 KB

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