dec_test.c 17 KB

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