mjpeg_dec_test.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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_Image.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_BUFFERHEADERTYPE *pInputBufferArray[64];
  40. OMX_BUFFERHEADERTYPE *pOutputBufferArray[64];
  41. AVFormatContext *avContext;
  42. int msgid;
  43. } DecodeTestContext;
  44. DecodeTestContext *decodeTestContext;
  45. static OMX_S32 FillInputBuffer(DecodeTestContext *decodeTestContext, OMX_BUFFERHEADERTYPE *pInputBuffer);
  46. static OMX_ERRORTYPE event_handler(
  47. OMX_HANDLETYPE hComponent,
  48. OMX_PTR pAppData,
  49. OMX_EVENTTYPE eEvent,
  50. OMX_U32 nData1,
  51. OMX_U32 nData2,
  52. OMX_PTR pEventData)
  53. {
  54. DecodeTestContext *pDecodeTestContext = (DecodeTestContext *)pAppData;
  55. switch (eEvent)
  56. {
  57. case OMX_EventPortSettingsChanged:
  58. {
  59. OMX_PARAM_PORTDEFINITIONTYPE pOutputPortDefinition;
  60. OMX_INIT_STRUCTURE(pOutputPortDefinition);
  61. pOutputPortDefinition.nPortIndex = 1;
  62. OMX_GetParameter(pDecodeTestContext->hComponentDecoder, OMX_IndexParamPortDefinition, &pOutputPortDefinition);
  63. OMX_U32 nOutputBufferSize = pOutputPortDefinition.nBufferSize;
  64. OMX_U32 nOutputBufferCount = pOutputPortDefinition.nBufferCountMin;
  65. printf("allocate %d output buffers size %d\r\n", nOutputBufferCount, nOutputBufferSize);
  66. for (int i = 0; i < nOutputBufferCount; i++)
  67. {
  68. OMX_BUFFERHEADERTYPE *pBuffer = NULL;
  69. OMX_AllocateBuffer(hComponent, &pBuffer, 1, NULL, nOutputBufferSize);
  70. pDecodeTestContext->pOutputBufferArray[i] = pBuffer;
  71. OMX_FillThisBuffer(hComponent, pBuffer);
  72. }
  73. }
  74. break;
  75. case OMX_EventBufferFlag:
  76. {
  77. Message data;
  78. data.msg_type = 1;
  79. data.msg_flag = -1;
  80. if (msgsnd(pDecodeTestContext->msgid, (void *)&data, sizeof(data) - sizeof(data.msg_type), 0) == -1)
  81. {
  82. fprintf(stderr, "msgsnd failed\n");
  83. }
  84. }
  85. break;
  86. default:
  87. break;
  88. }
  89. return OMX_ErrorNone;
  90. }
  91. static void help()
  92. {
  93. printf("./omx_mjpeg_dec_test -i <input file> -o <output file> \r\n");
  94. }
  95. static OMX_ERRORTYPE fill_output_buffer_done_handler(
  96. OMX_HANDLETYPE hComponent,
  97. OMX_PTR pAppData,
  98. OMX_BUFFERHEADERTYPE *pBuffer)
  99. {
  100. DecodeTestContext *pDecodeTestContext = (DecodeTestContext *)pAppData;
  101. Message data;
  102. data.msg_type = 1;
  103. if ((pBuffer->nFlags) & (OMX_BUFFERFLAG_EOS == OMX_BUFFERFLAG_EOS))
  104. {
  105. data.msg_flag = -1;
  106. }
  107. else
  108. {
  109. data.msg_flag = 1;
  110. data.pBuffer = pBuffer;
  111. }
  112. if (msgsnd(pDecodeTestContext->msgid, (void *)&data, sizeof(data) - sizeof(data.msg_type), 0) == -1)
  113. {
  114. fprintf(stderr, "msgsnd failed\n");
  115. }
  116. return OMX_ErrorNone;
  117. }
  118. static OMX_ERRORTYPE empty_buffer_done_handler(
  119. OMX_HANDLETYPE hComponent,
  120. OMX_PTR pAppData,
  121. OMX_BUFFERHEADERTYPE *pBuffer)
  122. {
  123. DecodeTestContext *pDecodeTestContext = (DecodeTestContext *)pAppData;
  124. Message data;
  125. data.msg_type = 1;
  126. data.msg_flag = 0;
  127. data.pBuffer = pBuffer;
  128. if (msgsnd(pDecodeTestContext->msgid, (void *)&data, sizeof(data) - sizeof(data.msg_type), 0) == -1)
  129. {
  130. fprintf(stderr, "msgsnd failed\n");
  131. }
  132. return OMX_ErrorNone;
  133. }
  134. static void signal_handle(int sig)
  135. {
  136. printf("[%s,%d]: receive sig=%d \n", __FUNCTION__, __LINE__, sig);
  137. Message data;
  138. data.msg_type = 1;
  139. data.msg_flag = -1;
  140. if (msgsnd(decodeTestContext->msgid, (void *)&data, sizeof(data) - sizeof(data.msg_type), 0) == -1)
  141. {
  142. fprintf(stderr, "msgsnd failed\n");
  143. }
  144. }
  145. static OMX_S32 FillInputBuffer(DecodeTestContext *decodeTestContext, OMX_BUFFERHEADERTYPE *pInputBuffer)
  146. {
  147. AVFormatContext *avFormatContext = decodeTestContext->avContext;
  148. AVPacket avpacket;
  149. OMX_S32 error;
  150. av_init_packet(&avpacket);
  151. error = av_read_frame(avFormatContext, &avpacket);
  152. if (error < 0)
  153. {
  154. if (error == AVERROR_EOF || avFormatContext->pb->eof_reached == OMX_TRUE)
  155. {
  156. pInputBuffer->nFlags = 0x1;
  157. pInputBuffer->nFilledLen = 0;
  158. return 0;
  159. }
  160. else
  161. {
  162. printf("%s:%d failed to av_read_frame error(0x%08x)\n", __FUNCTION__, __LINE__, error);
  163. return 0;
  164. }
  165. }
  166. pInputBuffer->nFlags = 0x10;
  167. pInputBuffer->nFilledLen = avpacket.size;
  168. memcpy(pInputBuffer->pBuffer, avpacket.data, avpacket.size);
  169. printf("%s, address = %p, size = %d, flag = %x\r\n",
  170. __FUNCTION__, pInputBuffer->pBuffer, pInputBuffer->nFilledLen, pInputBuffer->nFlags);
  171. return avpacket.size;
  172. }
  173. int main(int argc, char **argv)
  174. {
  175. printf("=============================\r\n");
  176. OMX_S32 error;
  177. decodeTestContext = malloc(sizeof(DecodeTestContext));
  178. memset(decodeTestContext, 0, sizeof(DecodeTestContext));
  179. OMX_S32 msgid = -1;
  180. msgid = msgget(IPC_PRIVATE, 0666 | IPC_CREAT);
  181. if (msgid < 0)
  182. {
  183. perror("get ipc_id error");
  184. return -1;
  185. }
  186. decodeTestContext->msgid = msgid;
  187. struct option longOpt[] = {
  188. {"output", required_argument, NULL, 'o'},
  189. {"input", required_argument, NULL, 'i'},
  190. {"format", required_argument, NULL, 'f'},
  191. {NULL, no_argument, NULL, 0},
  192. };
  193. char *shortOpt = "i:o:f:";
  194. OMX_U32 c;
  195. OMX_S32 l;
  196. // if (argc == 0)
  197. // {
  198. // help();
  199. // return;
  200. // }
  201. while ((c = getopt_long(argc, argv, shortOpt, longOpt, (int *)&l)) != -1)
  202. {
  203. switch (c)
  204. {
  205. case 'i':
  206. printf("input: %s\r\n", optarg);
  207. if (access(optarg, R_OK) != -1)
  208. {
  209. memcpy(decodeTestContext->sInputFilePath, optarg, strlen(optarg));
  210. }
  211. else
  212. {
  213. printf("input file not exist!\r\n");
  214. return -1;
  215. }
  216. break;
  217. case 'o':
  218. printf("output: %s\r\n", optarg);
  219. memcpy(decodeTestContext->sOutputFilePath, optarg, strlen(optarg));
  220. break;
  221. case 'f':
  222. printf("format: %s\r\n", optarg);
  223. memcpy(decodeTestContext->sOutputFormat, optarg, strlen(optarg));
  224. break;
  225. case 'h':
  226. default:
  227. help();
  228. return -1;
  229. }
  230. }
  231. if (decodeTestContext->sInputFilePath == NULL || decodeTestContext->sOutputFilePath == NULL)
  232. {
  233. help();
  234. return -1;
  235. }
  236. /*ffmpeg init*/
  237. printf("init ffmpeg\r\n");
  238. AVFormatContext *avContext = NULL;
  239. AVCodecParameters *codecParameters = NULL;
  240. AVInputFormat *fmt = NULL;
  241. OMX_S32 imageIndex;
  242. if ((avContext = avformat_alloc_context()) == NULL)
  243. {
  244. printf("avformat_alloc_context fail\r\n");
  245. return -1;
  246. }
  247. avContext->flags |= AV_CODEC_FLAG_TRUNCATED;
  248. printf("avformat_open_input\r\n");
  249. if ((error = avformat_open_input(&avContext, decodeTestContext->sInputFilePath, fmt, NULL)))
  250. {
  251. printf("%s:%d failed to av_open_input_file error(%d), %s\n",
  252. __FILE__, __LINE__, error, decodeTestContext->sInputFilePath);
  253. return -1;
  254. }
  255. printf("avformat_find_stream_info\r\n");
  256. if ((error = avformat_find_stream_info(avContext, NULL)) < 0)
  257. {
  258. printf("%s:%d failed to avformat_find_stream_info. error(%d)\n",
  259. __FUNCTION__, __LINE__, error);
  260. return -1;
  261. }
  262. printf("av_find_best_stream\r\n");
  263. imageIndex = av_find_best_stream(avContext, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
  264. if (imageIndex < 0)
  265. {
  266. printf("%s:%d failed to av_find_best_stream.\n", __FUNCTION__, __LINE__);
  267. return -1;
  268. }
  269. printf("image index = %d\r\n", imageIndex);
  270. decodeTestContext->avContext = avContext;
  271. /*get image info*/
  272. codecParameters = avContext->streams[imageIndex]->codecpar;
  273. printf("codec_id = %d, width = %d, height = %d\r\n", (int)codecParameters->codec_id,
  274. codecParameters->width, codecParameters->height);
  275. /*omx init*/
  276. OMX_HANDLETYPE hComponentDecoder = NULL;
  277. OMX_CALLBACKTYPE callbacks;
  278. int ret = OMX_ErrorNone;
  279. signal(SIGINT, signal_handle);
  280. printf("init omx\r\n");
  281. ret = OMX_Init();
  282. if (ret != OMX_ErrorNone)
  283. {
  284. printf("[%s,%d]: run OMX_Init failed. ret is %d \n", __FUNCTION__, __LINE__, ret);
  285. return 1;
  286. }
  287. callbacks.EventHandler = event_handler;
  288. callbacks.FillBufferDone = fill_output_buffer_done_handler;
  289. callbacks.EmptyBufferDone = empty_buffer_done_handler;
  290. printf("get handle by codec id = %d\r\n", codecParameters->codec_id);
  291. if (codecParameters->codec_id == AV_CODEC_ID_MJPEG)
  292. {
  293. OMX_GetHandle(&hComponentDecoder, "sf.dec.decoder.mjpeg", 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.image.nFrameWidth = codecParameters->width;
  331. pInputPortDefinition.format.image.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. printf("write %d buffers to file\r\n", pBuffer->nFilledLen);
  371. FILE *fb = fopen(sFilePath, "ab+");
  372. size_t size = fwrite(pBuffer->pBuffer, 1, pBuffer->nFilledLen, fb);
  373. int error = ferror(fb);
  374. printf("write error = %d\r\n", error);
  375. fclose(fb);
  376. printf("write %d buffers finish\r\n", size);
  377. if ((pBuffer->nFlags) & (OMX_BUFFERFLAG_EOS == OMX_BUFFERFLAG_EOS))
  378. {
  379. goto end;
  380. }
  381. else
  382. {
  383. OMX_FillThisBuffer(decodeTestContext->hComponentDecoder, pBuffer);
  384. }
  385. }
  386. break;
  387. case 2:
  388. break;
  389. default:
  390. goto end;
  391. }
  392. }
  393. end:
  394. /*free resource*/
  395. OMX_FreeHandle(hComponentDecoder);
  396. OMX_Deinit();
  397. }