dec_test.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2021 StarFive Technology Co., Ltd.
  4. */
  5. #include <stdio.h>
  6. #include <signal.h>
  7. #include <getopt.h>
  8. #include <fcntl.h>
  9. #include "libavformat/avformat.h"
  10. #include <OMX_Core.h>
  11. #include <OMX_Component.h>
  12. #include <OMX_Video.h>
  13. #include <OMX_IndexExt.h>
  14. #include <sys/time.h>
  15. #include <sys/types.h>
  16. #include <sys/ipc.h>
  17. #include <sys/msg.h>
  18. #define OMX_INIT_STRUCTURE(a) \
  19. memset(&(a), 0, sizeof(a)); \
  20. (a).nSize = sizeof(a); \
  21. (a).nVersion.nVersion = 1; \
  22. (a).nVersion.s.nVersionMajor = 1; \
  23. (a).nVersion.s.nVersionMinor = 1; \
  24. (a).nVersion.s.nRevision = 1; \
  25. (a).nVersion.s.nStep = 1
  26. typedef struct Message
  27. {
  28. long msg_type;
  29. OMX_S32 msg_flag;
  30. OMX_BUFFERHEADERTYPE *pBuffer;
  31. } Message;
  32. typedef struct DecodeTestContext
  33. {
  34. OMX_HANDLETYPE hComponentDecoder;
  35. char sOutputFilePath[64];
  36. char sInputFilePath[64];
  37. char sOutputFormat[64];
  38. OMX_BUFFERHEADERTYPE *pInputBufferArray[64];
  39. OMX_BUFFERHEADERTYPE *pOutputBufferArray[64];
  40. AVFormatContext *avContext;
  41. int msgid;
  42. } DecodeTestContext;
  43. DecodeTestContext *decodeTestContext;
  44. static OMX_S32 FillInputBuffer(DecodeTestContext *decodeTestContext, OMX_BUFFERHEADERTYPE *pInputBuffer);
  45. static OMX_ERRORTYPE event_handler(
  46. OMX_HANDLETYPE hComponent,
  47. OMX_PTR pAppData,
  48. OMX_EVENTTYPE eEvent,
  49. OMX_U32 nData1,
  50. OMX_U32 nData2,
  51. OMX_PTR pEventData)
  52. {
  53. DecodeTestContext *pDecodeTestContext = (DecodeTestContext *)pAppData;
  54. switch (eEvent)
  55. {
  56. case OMX_EventPortSettingsChanged:
  57. {
  58. OMX_PARAM_PORTDEFINITIONTYPE pOutputPortDefinition;
  59. OMX_INIT_STRUCTURE(pOutputPortDefinition);
  60. pOutputPortDefinition.nPortIndex = 1;
  61. OMX_GetParameter(pDecodeTestContext->hComponentDecoder, OMX_IndexParamPortDefinition, &pOutputPortDefinition);
  62. OMX_U32 nOutputBufferSize = pOutputPortDefinition.nBufferSize;
  63. OMX_U32 nOutputBufferCount = pOutputPortDefinition.nBufferCountMin;
  64. for (int i = 0; i < nOutputBufferCount; i++)
  65. {
  66. OMX_BUFFERHEADERTYPE *pBuffer = NULL;
  67. OMX_AllocateBuffer(hComponent, &pBuffer, 1, NULL, nOutputBufferSize);
  68. pDecodeTestContext->pOutputBufferArray[i] = pBuffer;
  69. OMX_FillThisBuffer(hComponent, pBuffer);
  70. }
  71. }
  72. break;
  73. case OMX_EventBufferFlag:
  74. {
  75. Message data;
  76. data.msg_type = 1;
  77. data.msg_flag = -1;
  78. if (msgsnd(pDecodeTestContext->msgid, (void *)&data, sizeof(data) - sizeof(data.msg_type), 0) == -1)
  79. {
  80. fprintf(stderr, "msgsnd failed\n");
  81. }
  82. }
  83. break;
  84. default:
  85. break;
  86. }
  87. return OMX_ErrorNone;
  88. }
  89. static void help()
  90. {
  91. printf("./omx_dec_test -i <input file> -o <output file> \r\n");
  92. }
  93. static OMX_ERRORTYPE fill_output_buffer_done_handler(
  94. OMX_HANDLETYPE hComponent,
  95. OMX_PTR pAppData,
  96. OMX_BUFFERHEADERTYPE *pBuffer)
  97. {
  98. DecodeTestContext *pDecodeTestContext = (DecodeTestContext *)pAppData;
  99. Message data;
  100. data.msg_type = 1;
  101. if ((pBuffer->nFlags) & (OMX_BUFFERFLAG_EOS == OMX_BUFFERFLAG_EOS))
  102. {
  103. data.msg_flag = -1;
  104. }
  105. else
  106. {
  107. data.msg_flag = 1;
  108. data.pBuffer = pBuffer;
  109. }
  110. if (msgsnd(pDecodeTestContext->msgid, (void *)&data, sizeof(data) - sizeof(data.msg_type), 0) == -1)
  111. {
  112. fprintf(stderr, "msgsnd failed\n");
  113. }
  114. return OMX_ErrorNone;
  115. }
  116. static OMX_ERRORTYPE empty_buffer_done_handler(
  117. OMX_HANDLETYPE hComponent,
  118. OMX_PTR pAppData,
  119. OMX_BUFFERHEADERTYPE *pBuffer)
  120. {
  121. DecodeTestContext *pDecodeTestContext = (DecodeTestContext *)pAppData;
  122. Message data;
  123. data.msg_type = 1;
  124. data.msg_flag = 0;
  125. data.pBuffer = pBuffer;
  126. if (msgsnd(pDecodeTestContext->msgid, (void *)&data, sizeof(data) - sizeof(data.msg_type), 0) == -1)
  127. {
  128. fprintf(stderr, "msgsnd failed\n");
  129. }
  130. return OMX_ErrorNone;
  131. }
  132. static void signal_handle(int sig)
  133. {
  134. printf("[%s,%d]: receive sig=%d \n", __FUNCTION__, __LINE__, sig);
  135. Message data;
  136. data.msg_type = 1;
  137. data.msg_flag = -1;
  138. if (msgsnd(decodeTestContext->msgid, (void *)&data, sizeof(data) - sizeof(data.msg_type), 0) == -1)
  139. {
  140. fprintf(stderr, "msgsnd failed\n");
  141. }
  142. }
  143. static OMX_S32 FillInputBuffer(DecodeTestContext *decodeTestContext, OMX_BUFFERHEADERTYPE *pInputBuffer)
  144. {
  145. AVFormatContext *avFormatContext = decodeTestContext->avContext;
  146. AVPacket avpacket;
  147. OMX_S32 error;
  148. av_init_packet(&avpacket);
  149. error = av_read_frame(avFormatContext, &avpacket);
  150. if (error < 0)
  151. {
  152. if (error == AVERROR_EOF || avFormatContext->pb->eof_reached == OMX_TRUE)
  153. {
  154. pInputBuffer->nFlags = 0x1;
  155. pInputBuffer->nFilledLen = 0;
  156. return 0;
  157. }
  158. else
  159. {
  160. printf("%s:%d failed to av_read_frame error(0x%08x)\n", __FUNCTION__, __LINE__, error);
  161. return 0;
  162. }
  163. }
  164. pInputBuffer->nFlags = 0x10;
  165. pInputBuffer->nFilledLen = avpacket.size;
  166. memcpy(pInputBuffer->pBuffer, avpacket.data, avpacket.size);
  167. return avpacket.size;
  168. }
  169. int main(int argc, char **argv)
  170. {
  171. printf("=============================\r\n");
  172. OMX_S32 error;
  173. decodeTestContext = malloc(sizeof(DecodeTestContext));
  174. memset(decodeTestContext, 0, sizeof(DecodeTestContext));
  175. OMX_S32 msgid = -1;
  176. msgid = msgget(IPC_PRIVATE, 0666 | IPC_CREAT);
  177. if (msgid < 0)
  178. {
  179. perror("get ipc_id error");
  180. return -1;
  181. }
  182. decodeTestContext->msgid = msgid;
  183. struct option longOpt[] = {
  184. {"output", required_argument, NULL, 'o'},
  185. {"input", required_argument, NULL, 'i'},
  186. {"format", required_argument, NULL, 'f'},
  187. {NULL, no_argument, NULL, 0},
  188. };
  189. char *shortOpt = "i:o:f:";
  190. OMX_U32 c;
  191. OMX_S32 l;
  192. if (argc == 0)
  193. {
  194. help();
  195. return -1;
  196. }
  197. while ((c = getopt_long(argc, argv, shortOpt, longOpt, (int *)&l)) != -1)
  198. {
  199. switch (c)
  200. {
  201. case 'i':
  202. printf("input: %s\r\n", optarg);
  203. if (access(optarg, R_OK) != -1)
  204. {
  205. memcpy(decodeTestContext->sInputFilePath, optarg, strlen(optarg));
  206. }
  207. else
  208. {
  209. printf("input file not exist!\r\n");
  210. return -1;
  211. }
  212. break;
  213. case 'o':
  214. printf("output: %s\r\n", optarg);
  215. memcpy(decodeTestContext->sOutputFilePath, optarg, strlen(optarg));
  216. break;
  217. case 'f':
  218. printf("format: %s\r\n", optarg);
  219. memcpy(decodeTestContext->sOutputFormat, optarg, strlen(optarg));
  220. break;
  221. case 'h':
  222. default:
  223. help();
  224. return -1;
  225. }
  226. }
  227. if (decodeTestContext->sInputFilePath == NULL || decodeTestContext->sOutputFilePath == NULL)
  228. {
  229. help();
  230. return -1;
  231. }
  232. /*ffmpeg init*/
  233. printf("init ffmpeg\r\n");
  234. AVFormatContext *avContext = NULL;
  235. AVCodecParameters *codecParameters = NULL;
  236. AVInputFormat *fmt = NULL;
  237. OMX_S32 videoIndex;
  238. if ((avContext = avformat_alloc_context()) == NULL)
  239. {
  240. printf("avformat_alloc_context fail\r\n");
  241. return -1;
  242. }
  243. avContext->flags |= AV_CODEC_FLAG_TRUNCATED;
  244. printf("avformat_open_input\r\n");
  245. if ((error = avformat_open_input(&avContext, decodeTestContext->sInputFilePath, fmt, NULL)))
  246. {
  247. printf("%s:%d failed to av_open_input_file error(%d), %s\n",
  248. __FILE__, __LINE__, error, decodeTestContext->sInputFilePath);
  249. return -1;
  250. }
  251. printf("avformat_find_stream_info\r\n");
  252. if ((error = avformat_find_stream_info(avContext, NULL)) < 0)
  253. {
  254. printf("%s:%d failed to avformat_find_stream_info. error(%d)\n",
  255. __FUNCTION__, __LINE__, error);
  256. return -1;
  257. }
  258. printf("av_find_best_stream\r\n");
  259. videoIndex = av_find_best_stream(avContext, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
  260. if (videoIndex < 0)
  261. {
  262. printf("%s:%d failed to av_find_best_stream.\n", __FUNCTION__, __LINE__);
  263. return -1;
  264. }
  265. printf("video index = %d\r\n", videoIndex);
  266. decodeTestContext->avContext = avContext;
  267. /*get video info*/
  268. codecParameters = avContext->streams[videoIndex]->codecpar;
  269. printf("codec_id = %d, width = %d, height = %d\r\n", (int)codecParameters->codec_id,
  270. codecParameters->width, codecParameters->height);
  271. /*omx init*/
  272. OMX_HANDLETYPE hComponentDecoder = NULL;
  273. OMX_CALLBACKTYPE callbacks;
  274. int ret = OMX_ErrorNone;
  275. signal(SIGINT, signal_handle);
  276. printf("init omx\r\n");
  277. ret = OMX_Init();
  278. if (ret != OMX_ErrorNone)
  279. {
  280. printf("[%s,%d]: run OMX_Init failed. ret is %d \n", __FUNCTION__, __LINE__, ret);
  281. return 1;
  282. }
  283. callbacks.EventHandler = event_handler;
  284. callbacks.FillBufferDone = fill_output_buffer_done_handler;
  285. callbacks.EmptyBufferDone = empty_buffer_done_handler;
  286. printf("get handle\r\n");
  287. if (codecParameters->codec_id == AV_CODEC_ID_H264)
  288. {
  289. OMX_GetHandle(&hComponentDecoder, "sf.dec.decoder.h264", decodeTestContext, &callbacks);
  290. }
  291. else if (codecParameters->codec_id == AV_CODEC_ID_HEVC)
  292. {
  293. OMX_GetHandle(&hComponentDecoder, "sf.dec.decoder.h265", decodeTestContext, &callbacks);
  294. }
  295. if (hComponentDecoder == NULL)
  296. {
  297. printf("could not get handle\r\n");
  298. return 0;
  299. }
  300. decodeTestContext->hComponentDecoder = hComponentDecoder;
  301. OMX_PARAM_PORTDEFINITIONTYPE pOutputPortDefinition;
  302. OMX_INIT_STRUCTURE(pOutputPortDefinition);
  303. pOutputPortDefinition.nPortIndex = 1;
  304. OMX_GetParameter(decodeTestContext->hComponentDecoder, OMX_IndexParamPortDefinition, &pOutputPortDefinition);
  305. pOutputPortDefinition.format.video.nFrameWidth = codecParameters->width;
  306. pOutputPortDefinition.format.video.nFrameHeight = codecParameters->height;
  307. if (strstr(decodeTestContext->sOutputFormat, "nv12") != NULL)
  308. {
  309. pOutputPortDefinition.format.video.eColorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
  310. }
  311. else if (strstr(decodeTestContext->sOutputFormat, "nv21") != NULL)
  312. {
  313. pOutputPortDefinition.format.video.eColorFormat = OMX_COLOR_FormatYVU420PackedSemiPlanar;
  314. }
  315. else if (strstr(decodeTestContext->sOutputFormat, "i420") != NULL)
  316. {
  317. pOutputPortDefinition.format.video.eColorFormat = OMX_COLOR_FormatYUV420Planar;
  318. }
  319. else
  320. {
  321. printf("Unsupported color format!\r\n");
  322. goto end;
  323. }
  324. OMX_SetParameter(hComponentDecoder, OMX_IndexParamPortDefinition, &pOutputPortDefinition);
  325. OMX_SendCommand(hComponentDecoder, OMX_CommandStateSet, OMX_StateIdle, NULL);
  326. OMX_PARAM_PORTDEFINITIONTYPE pInputPortDefinition;
  327. OMX_INIT_STRUCTURE(pInputPortDefinition);
  328. pInputPortDefinition.nPortIndex = 0;
  329. OMX_GetParameter(hComponentDecoder, OMX_IndexParamPortDefinition, &pInputPortDefinition);
  330. pInputPortDefinition.format.video.nFrameWidth = codecParameters->width;
  331. pInputPortDefinition.format.video.nFrameHeight = codecParameters->height;
  332. OMX_SetParameter(hComponentDecoder, OMX_IndexParamPortDefinition, &pInputPortDefinition);
  333. /*Alloc input buffer*/
  334. OMX_U32 nInputWidth = codecParameters->width;
  335. OMX_U32 nInputHeight = codecParameters->height;
  336. OMX_U32 nInputBufferSize = nInputWidth * nInputHeight * 2;
  337. OMX_U32 nInputBufferCount = pInputPortDefinition.nBufferCountActual;
  338. for (int i = 0; i < nInputBufferCount; i++)
  339. {
  340. OMX_BUFFERHEADERTYPE *pBuffer = NULL;
  341. OMX_AllocateBuffer(hComponentDecoder, &pBuffer, 0, NULL, nInputBufferSize);
  342. decodeTestContext->pInputBufferArray[i] = pBuffer;
  343. /*Fill Input Buffer*/
  344. FillInputBuffer(decodeTestContext, pBuffer);
  345. OMX_EmptyThisBuffer(hComponentDecoder, pBuffer);
  346. }
  347. OMX_SendCommand(hComponentDecoder, OMX_CommandStateSet, OMX_StateExecuting, NULL);
  348. /*wait until decode finished*/
  349. Message data;
  350. while (OMX_TRUE)
  351. {
  352. if (msgrcv(msgid, (void *)&data, BUFSIZ, 0, 0) == -1)
  353. {
  354. fprintf(stderr, "msgrcv failed with errno: %d\n", errno);
  355. goto end;
  356. }
  357. switch (data.msg_flag)
  358. {
  359. case 0:
  360. {
  361. OMX_BUFFERHEADERTYPE *pBuffer = data.pBuffer;
  362. FillInputBuffer(decodeTestContext, pBuffer);
  363. OMX_EmptyThisBuffer(decodeTestContext->hComponentDecoder, pBuffer);
  364. }
  365. break;
  366. case 1:
  367. {
  368. OMX_BUFFERHEADERTYPE *pBuffer = data.pBuffer;
  369. OMX_STRING sFilePath = decodeTestContext->sOutputFilePath;
  370. FILE *fb = fopen(sFilePath, "ab+");
  371. fwrite(pBuffer->pBuffer, 1, pBuffer->nFilledLen, fb);
  372. fclose(fb);
  373. if ((pBuffer->nFlags) & (OMX_BUFFERFLAG_EOS == OMX_BUFFERFLAG_EOS))
  374. {
  375. goto end;
  376. }
  377. else
  378. {
  379. OMX_FillThisBuffer(decodeTestContext->hComponentDecoder, pBuffer);
  380. }
  381. }
  382. break;
  383. case 2:
  384. break;
  385. default:
  386. goto end;
  387. }
  388. }
  389. end:
  390. /*free resource*/
  391. OMX_FreeHandle(hComponentDecoder);
  392. OMX_Deinit();
  393. }