dec_test.c 15 KB

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