mjpeg_dec_test.c 24 KB

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