mjpeg_dec_test.c 15 KB

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