mjpeg_dec_test.c 16 KB

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