main.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <errno.h>
  6. #include <unistd.h>
  7. #include <fcntl.h>
  8. #include <sys/types.h>
  9. #include <linux/ioctl.h>
  10. #include <sys/ioctl.h>
  11. #include <string.h>
  12. #include <sys/time.h>
  13. using namespace std;
  14. #include "ARKMirrorRender.h"
  15. extern "C"
  16. {
  17. #include <libavcodec/avcodec.h>
  18. #include <libavformat/avformat.h>
  19. #include <libswscale/swscale.h>
  20. }
  21. #define TEST_H264_FILE 1
  22. #define TEST_H264_STREAM 2
  23. struct test_info{
  24. int type;
  25. int mode;
  26. char filepath[64];
  27. };
  28. static const int FRAME_NUM = 305;
  29. //static const int FRAME_NUM = 100;
  30. static const int MAX_FILE_SIZE = 0x100000;
  31. struct test_info test;
  32. int ark_test_mode;
  33. int64_t get_current_time_us(void)
  34. {
  35. struct timeval tv;
  36. gettimeofday(&tv,NULL);
  37. return tv.tv_sec * 1000000ll + tv.tv_usec;
  38. }
  39. #if (ARK_COMPILE_MODE == COMPILE_TEST_ARK_API)
  40. static void *proc_h264_stream(void)
  41. {
  42. AVFormatContext *pFormatCtx;
  43. int i, videoindex;
  44. AVCodecContext *pCodecCtx;
  45. AVCodec *pCodec;
  46. AVFrame *pFrame;
  47. AVPacket *packet;
  48. int ret, got_picture;
  49. char *filepath = test.filepath;
  50. //char *filepath = "/media/test.h264";
  51. //////////////////////add////////////////////////
  52. ARKMirrorRender render;
  53. int screen_width, screen_height;
  54. screen_info screen;
  55. ECMirrorRender::MirrorDirection dir = ECMirrorRender::VERTICAL;
  56. char buf[64];
  57. ret = arkapi_display_get_screen_info(&screen);
  58. if(ret == 0){
  59. screen_width = screen.width;
  60. screen_height = screen.height;
  61. }else{
  62. screen_width = 1024;
  63. screen_height = 600;
  64. }
  65. printf("screen_width=%d, screen_height=%d \n",screen_width, screen_height);
  66. render.setScreenSize(screen_width, screen_height);
  67. render.setMirrorSize(800, 480);
  68. render.initialize();
  69. ////////////////////////////////////////////////////
  70. while(1){
  71. av_register_all();
  72. avformat_network_init();
  73. pFormatCtx = avformat_alloc_context();
  74. ret = avformat_open_input(&pFormatCtx, filepath,NULL,NULL);
  75. if(ret!=0){
  76. av_strerror(ret, buf, 64);
  77. printf("Couldn't open file %s: %d(%s)", filepath, ret, buf);
  78. return NULL;
  79. }
  80. printf("open input stream sucess, %s.\n",filepath);
  81. if(avformat_find_stream_info(pFormatCtx,NULL)<0){
  82. printf("Couldn't find stream information.\n");
  83. avformat_close_input(&pFormatCtx);
  84. return NULL;
  85. }
  86. videoindex=-1;
  87. for(i=0; i<pFormatCtx->nb_streams; i++) {
  88. if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
  89. videoindex=i;
  90. break;
  91. }
  92. }
  93. if(videoindex==-1){
  94. printf("Didn't find a video stream.\n");
  95. avformat_close_input(&pFormatCtx);
  96. return NULL;
  97. }
  98. pCodecCtx=pFormatCtx->streams[videoindex]->codec;
  99. pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
  100. if(pCodec==NULL){
  101. printf("Codec not found.\n");
  102. avcodec_close(pCodecCtx);
  103. avformat_close_input(&pFormatCtx);
  104. return NULL;
  105. }
  106. if(avcodec_open2(pCodecCtx, pCodec,NULL)<0){
  107. printf("Could not open codec.\n");
  108. avcodec_close(pCodecCtx);
  109. avformat_close_input(&pFormatCtx);
  110. return NULL;
  111. }
  112. pFrame=av_frame_alloc();
  113. packet=(AVPacket *)av_malloc(sizeof(AVPacket));
  114. av_dump_format(pFormatCtx,0,filepath,0);
  115. unsigned char *tmp = NULL;
  116. int payload_len = 0;
  117. int count = 0;
  118. int time1 = get_current_time_us()/1000;
  119. int time2 = 0;
  120. while(av_read_frame(pFormatCtx, packet)>=0){
  121. if(packet->stream_index==videoindex){
  122. //Decode
  123. tmp = (unsigned char *)packet->data;//packet->size
  124. //printf("size=%6d ", packet->size);
  125. //for (i = 0; i < 8; i++)
  126. // printf("%02x ", tmp[i]);
  127. //printf("\n");
  128. //add
  129. render.play(tmp, packet->size, dir);
  130. usleep(30000);
  131. if(count%300 == 0){
  132. time2 = get_current_time_us()/1000;
  133. printf("count=%d time=%d.\n",count, time2-time1);
  134. time1 = time2;
  135. }
  136. count++;
  137. }
  138. av_free_packet(packet);
  139. }
  140. if (pCodecCtx)
  141. avcodec_close(pCodecCtx);
  142. if (pCodecCtx)
  143. avformat_close_input(&pFormatCtx);
  144. }
  145. //add
  146. render.hide();
  147. render.release();
  148. return NULL;
  149. }
  150. #endif
  151. int process_h264_file(void)
  152. {
  153. ARKMirrorRender render;
  154. int ret, screen_width, screen_height;
  155. screen_info screen;
  156. ret = arkapi_display_get_screen_info(&screen);
  157. if(ret == 0){
  158. screen_width = screen.width;
  159. screen_height = screen.height;
  160. }else{
  161. screen_width = 1024;
  162. screen_height = 600;
  163. }
  164. printf("screen_width=%d, screen_height=%d \n",screen_width, screen_height);
  165. render.setScreenSize(screen_width, screen_height);
  166. render.setMirrorSize(720, 1280);
  167. render.initialize();
  168. ifstream infile;
  169. char filename[32];
  170. int filelen = 0;
  171. char *filedata = new char[MAX_FILE_SIZE];
  172. if (filedata == NULL) {
  173. cout << "alloc buffer fail." << endl;
  174. return -1;
  175. }
  176. #if (ARK_COMPILE_MODE == COMPILE_TEST_ARK_API)
  177. while(1){
  178. #endif
  179. for (int i = 0; i < FRAME_NUM; i++) {
  180. sprintf(filename, "%s/%.3d.h264",test.filepath, i);
  181. //sprintf(filename, "/usr/share/pieces/%.3d.h264", i);
  182. infile.open(filename, ios::binary);
  183. if (!infile.is_open()) {
  184. cout << "open file " << filename << " fail." << endl;
  185. continue;
  186. }
  187. infile.seekg(0, ios::end);
  188. filelen = infile.tellg();
  189. infile.seekg(0, ios::beg);
  190. infile.read(filedata, filelen);
  191. ECMirrorRender::MirrorDirection dir = ECMirrorRender::VERTICAL;
  192. if (i >= 264)
  193. dir = ECMirrorRender::VERTICAL;
  194. else if (i >= 212)
  195. dir = ECMirrorRender::LANDSCAPE;
  196. else if (i >= 168)
  197. dir = ECMirrorRender::VERTICAL;
  198. else if (i >= 90)
  199. dir = ECMirrorRender::LANDSCAPE;
  200. render.play(filedata, filelen, dir);
  201. infile.close();
  202. if(ark_test_mode == ARKN141_MFC_DISPLAY_API)
  203. usleep(25000);
  204. }
  205. #if (ARK_COMPILE_MODE == COMPILE_TEST_ARK_API)
  206. }
  207. #endif
  208. delete []filedata;
  209. render.hide();
  210. render.release();
  211. }
  212. static int parser_command_set_para(char* pcmd0, char* pcmd1)
  213. {
  214. int i;
  215. char *p;
  216. printf("pcmd0 parser==>%s\n",pcmd0);
  217. if(strstr(pcmd0,"video-api")){
  218. test.mode = USE_ARK_VIDEO_API;
  219. }
  220. else if(strstr(pcmd0,"2d-api")){
  221. test.mode = USE_ARK_DISPLAY_2D_API;
  222. }
  223. else if(strstr(pcmd0,"2draw-api")){
  224. test.mode = USE_ARK_RAW2D_DISPLAY_API;
  225. }
  226. else if(strstr(pcmd0,"display-api")){
  227. test.mode = ARK1668_MFC_DISPLAY_API;
  228. }
  229. else if(strstr(pcmd0,"n141-display")){
  230. test.mode = ARKN141_MFC_DISPLAY_API;
  231. }else{
  232. test.mode = USE_ARK_VIDEO_API;
  233. }
  234. printf("pcmd1 parser==>%s\n",pcmd1);
  235. if(strstr(pcmd1,"pieces")){
  236. test.type = TEST_H264_FILE;
  237. }else if(strstr(pcmd1,".h264")){
  238. test.type= TEST_H264_STREAM;
  239. }else{
  240. printf("cmd0 parser error.\n");
  241. test.type= TEST_H264_STREAM;
  242. //return -1;
  243. }
  244. p = pcmd1;
  245. i = 0;
  246. while(*p != '\0'){
  247. test.filepath[i++] = *p++;
  248. }
  249. return 0;
  250. }
  251. static void printf_command_help(void)
  252. {
  253. printf("\n");
  254. printf("***********************command help*****************************\n");
  255. printf("* *\n");
  256. printf("*run cmd: h264-rotate-render [cmd1] [cmd2] *\n");
  257. printf("* *\n");
  258. printf("*cmd1 option: 1 video-api 2 2d-api *\n");
  259. printf("* 3 2draw-api 4 display-api *\n");
  260. printf("* 5 n141-display *\n");
  261. printf("* *\n");
  262. printf("*cmd2 option: filepath *\n");
  263. printf("* *\n");
  264. printf("*example: *\n");
  265. printf("* h264-rotate-render video-api /media/pieces *\n");
  266. printf("* h264-rotate-render n141-display /media/test_800x480.h264 *\n");
  267. printf("* *\n");
  268. printf("****************************************************************\n");
  269. printf("\n");
  270. }
  271. int main(int argc, char *argv[])
  272. {
  273. char filepath[32]= "/media/pieces/";
  274. int ret, compile_mode;
  275. printf_command_help();
  276. if(argc == 1){
  277. test.type = TEST_H264_FILE;
  278. test.mode = USE_ARK_VIDEO_API;
  279. memcpy(&test.filepath, filepath, 32);
  280. }else if(argc == 3){
  281. printf("==>get run cmd: %s %s %s\n",argv[0],argv[1],argv[2]);
  282. ret = parser_command_set_para(argv[1],argv[2]);
  283. if(ret < 0){
  284. printf("run para error, exit1.\n");
  285. return 0;
  286. }
  287. }else{
  288. printf("run para error, exit2.\n");
  289. return 0;
  290. }
  291. ark_test_mode = test.mode;
  292. compile_mode = ARK_COMPILE_MODE;
  293. printf("ARK_COMPILE_MODE=%d, ark_test_mode=%d.\n", compile_mode, ark_test_mode);
  294. #if (ARK_COMPILE_MODE == COMPILE_TEST_ARK_API)
  295. if (test.type == TEST_H264_STREAM)
  296. proc_h264_stream();
  297. else
  298. #endif
  299. process_h264_file();
  300. return 0;
  301. }