FIMV_MFC_V1.0.rar display_optimization1.c - Read

来源:互联网 发布:c 编程思想 编辑:程序博客网 时间:2024/04/30 05:40
  1. 转自:http://read.pudn.com/downloads143/sourcecode/unix_linux/622436/FIMV_MFC_V1.0/mfc_app/API/display_optimization1.c__.htm
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. #include <sys/types.h>  
  5. #include <sys/stat.h>  
  6. #include <sys/ioctl.h>  
  7. #include <fcntl.h>  
  8. #include <ctype.h>  
  9. #include <unistd.h>  
  10. #include <sys/mman.h>  
  11. #include <string.h>  
  12. #include <errno.h>  
  13. #include <sys/time.h>  
  14. #include <signal.h>  
  15. #include <linux/vt.h>  
  16. #include <linux/fb.h>  
  17. #include <pthread.h>  
  18. #include <semaphore.h>  
  19.   
  20. #include "SsbSipH264Decode.h"  
  21. #include "SsbSipMpeg4Decode.h"  
  22. #include "SsbSipVC1Decode.h"  
  23. #include "FrameExtractor.h"  
  24. #include "MPEG4Frames.h"  
  25. #include "H263Frames.h"  
  26. #include "H264Frames.h"  
  27. #include "SsbSipLogMsg.h"  
  28. #include "performance.h"  
  29. #include "post.h"  
  30. #include "lcd.h"  
  31. #include "MfcDriver.h"  
  32. #include "FileRead.h"  
  33. #include "display_optimization1.h"  
  34.   
  35.   
  36. static unsigned char delimiter_h264[4]  = {0x00, 0x00, 0x00, 0x01};  
  37. static int nFrameLeng;  
  38.   
  39. #define INPUT_BUFFER_SIZE       (204800)  
  40.   
  41.   
  42. static void     *handle;  
  43. static int      in_fd;  
  44. static int      file_size;  
  45. static char     *in_addr;  
  46. static int      fb_size;  
  47. static int      pp_fd, fb_fd;  
  48. static char     *fb_addr;     
  49.   
  50. static void sig_del_h264(void);  
  51.   
  52. static q_instance   queue[10];  
  53. static sem_t        full;  
  54. static sem_t        empty;  
  55. static int          producer_idx, consumer_idx;  
  56. static int          frame_cnt;  
  57. static int          frame_need_cnt[2];  
  58. static pthread_t    th;  
  59.   
  60. static void *rendering(void *arg)  
  61. {  
  62.     pp_params   *pp_param = (pp_params *)arg;  
  63.       
  64.       
  65.     while(1) {  
  66.         sem_wait(&full);  
  67.   
  68.         pp_param->SrcFrmSt = queue[consumer_idx].phy_addr;  
  69.         ioctl(pp_fd, PPROC_SET_PARAMS, pp_param);  
  70.   
  71.         ioctl(pp_fd, PPROC_START);  
  72.   
  73.         consumer_idx++;  
  74.         consumer_idx %= frame_need_cnt[0];  
  75.   
  76.         if(nFrameLeng < 4)   
  77.             break;  
  78.           
  79.         sem_post(&empty);  
  80.     }  
  81.   
  82.     return NULL;  
  83. }  
  84.   
  85. int Test_Display_Optimization1(int argc, char **argv)  
  86. {  
  87.       
  88.     void                    *pStrmBuf;  
  89.     unsigned int            pYUVBuf[2];  
  90.       
  91.     struct stat             s;  
  92.     FRAMEX_CTX              *pFrameExCtx;   // frame extractor context  
  93.     FRAMEX_STRM_PTR         file_strm;  
  94.     SSBSIP_H264_STREAM_INFO stream_info;      
  95.       
  96.     pp_params               pp_param;  
  97.     s3c_win_info_t          osd_info_to_driver;  
  98.   
  99.     int                     r;  
  100.   
  101. #ifdef FPS  
  102.     struct timeval  start, stop;  
  103.     unsigned int    time = 0;  
  104. #endif  
  105.   
  106.   
  107.     if(signal(SIGINT, sig_del_h264) == SIG_ERR) {  
  108.         printf("Sinal Error\n");  
  109.     }  
  110.   
  111.     if (argc != 2) {  
  112.         printf("Usage : mfc <H.264 input filename>\n");  
  113.         return -1;  
  114.     }  
  115.   
  116. #ifndef RGB24BPP  
  117.     printf("Display optimization 1 supports 24bpp only.\n");  
  118.     printf("Because local path is used between post processor and lcd\n");  
  119.     printf("RGB24BPP macro must be added in Makefile\n");  
  120.     return -1;  
  121. #endif  
  122.   
  123.     // in file open  
  124.     in_fd   = open(argv[1], O_RDONLY);  
  125.     if(in_fd < 0) {  
  126.         printf("Input file open failed\n");  
  127.         return -1;  
  128.     }  
  129.   
  130.     // get input file size  
  131.     fstat(in_fd, &s);  
  132.     file_size = s.st_size;  
  133.       
  134.     // mapping input file to memory  
  135.     in_addr = (char *)mmap(0, file_size, PROT_READ, MAP_SHARED, in_fd, 0);  
  136.     if(in_addr == NULL) {  
  137.         printf("input file memory mapping failed\n");  
  138.         return -1;  
  139.     }  
  140.       
  141.     // Post processor open  
  142.     pp_fd = open(PP_DEV_NAME, O_RDWR|O_NDELAY);  
  143.     if(pp_fd < 0)  
  144.     {  
  145.         printf("Post processor open error\n");  
  146.         return -1;  
  147.     }  
  148.   
  149.     // LCD frame buffer open  
  150.     fb_fd = open(FB_DEV_NAME, O_RDWR|O_NDELAY);  
  151.     if(fb_fd < 0)  
  152.     {  
  153.         printf("LCD frame buffer open error\n");  
  154.         return -1;  
  155.     }  
  156.   
  157.     ///////////////////////////////////  
  158.     // FrameExtractor Initialization //  
  159.     ///////////////////////////////////  
  160.     pFrameExCtx = FrameExtractorInit(FRAMEX_IN_TYPE_MEM, delimiter_h264, sizeof(delimiter_h264), 1);     
  161.     file_strm.p_start = file_strm.p_cur = (unsigned char *)in_addr;  
  162.     file_strm.p_end = (unsigned char *)(in_addr + file_size);  
  163.     FrameExtractorFirst(pFrameExCtx, &file_strm);  
  164.       
  165.   
  166.     //////////////////////////////////////  
  167.     ///    1. Create new instance      ///  
  168.     ///      (SsbSipH264DecodeInit)    ///  
  169.     //////////////////////////////////////  
  170.     handle = SsbSipH264DecodeInit();  
  171.     if (handle == NULL) {  
  172.         printf("H264_Dec_Init Failed.\n");  
  173.         return -1;  
  174.     }  
  175.   
  176.     /////////////////////////////////////////////  
  177.     ///    2. Obtaining the Input Buffer      ///  
  178.     ///      (SsbSipH264DecodeGetInBuf)       ///  
  179.     /////////////////////////////////////////////  
  180.     pStrmBuf = SsbSipH264DecodeGetInBuf(handle, nFrameLeng);  
  181.     if (pStrmBuf == NULL) {  
  182.         printf("SsbSipH264DecodeGetInBuf Failed.\n");  
  183.         SsbSipH264DecodeDeInit(handle);  
  184.         return -1;  
  185.     }  
  186.   
  187.     ////////////////////////////////////  
  188.     //  H264 CONFIG stream extraction //  
  189.     ////////////////////////////////////  
  190.     nFrameLeng = ExtractConfigStreamH264(pFrameExCtx, &file_strm, pStrmBuf, INPUT_BUFFER_SIZE, NULL);  
  191.   
  192.   
  193.     ////////////////////////////////////////////////////////////////  
  194.     ///    3. Configuring the instance with the config stream    ///  
  195.     ///       (SsbSipH264DecodeExe)                             ///  
  196.     ////////////////////////////////////////////////////////////////  
  197.     if (SsbSipH264DecodeExe(handle, nFrameLeng) != SSBSIP_H264_DEC_RET_OK) {  
  198.         printf("H.264 Decoder Configuration Failed.\n");  
  199.         return -1;  
  200.     }  
  201.   
  202.   
  203.     /////////////////////////////////////  
  204.     ///   4. Get stream information   ///  
  205.     /////////////////////////////////////  
  206.     SsbSipH264DecodeGetConfig(handle, H264_DEC_GETCONF_STREAMINFO, &stream_info);  
  207.   
  208.     printf("\t<STREAMINFO> width=%d   height=%d.\n", stream_info.width, stream_info.height);  
  209.   
  210.   
  211.     // set post processor configuration  
  212.     pp_param.SrcFullWidth   = stream_info.width;  
  213.     pp_param.SrcFullHeight  = stream_info.height;  
  214.     pp_param.SrcCSpace      = YC420;  
  215.     pp_param.DstFullWidth   = 800;      // destination width  
  216.     pp_param.DstFullHeight  = 480;      // destination height  
  217.     pp_param.DstCSpace      = RGB24;  
  218.     // Post processor's output directly goes to FIFO in display controller using local path  
  219.     pp_param.OutPath        = POST_FIFO;      
  220.     pp_param.Mode           = ONE_SHOT;  
  221.       
  222.     // get LCD frame buffer address  
  223.     fb_size = pp_param.DstFullWidth * pp_param.DstFullHeight * 4;   // RGB888  
  224.   
  225.     fb_addr = (char *)mmap(0, fb_size, PROT_READ | PROT_WRITE, MAP_SHARED, fb_fd, 0);  
  226.     if (fb_addr == NULL) {  
  227.         printf("LCD frame buffer mmap failed\n");  
  228.         return -1;  
  229.     }  
  230.   
  231.     osd_info_to_driver.Bpp          = 24;   // RGB888  
  232.     osd_info_to_driver.LeftTop_x    = 0;      
  233.     osd_info_to_driver.LeftTop_y    = 0;  
  234.     osd_info_to_driver.Width        = 800;  // display width  
  235.     osd_info_to_driver.Height       = 480;  // display height  
  236.   
  237.       
  238.     // set OSD's information   
  239.     if(ioctl(fb_fd, SET_OSD_INFO, &osd_info_to_driver)) {  
  240.         printf("Some problem with the ioctl SET_OSD_INFO\n");  
  241.         return -1;  
  242.     }  
  243.   
  244.     //Default setting of window0 doesn't use  
  245.     ioctl(fb_fd, SET_OSD_STOP);  
  246.   
  247.     SsbSipH264DecodeGetConfig(handle, H264_DEC_GETCONF_FRAM_NEED_COUNT, frame_need_cnt);  
  248.   
  249.     printf("Frame need count is %d\n", frame_need_cnt[0]);  
  250.   
  251.     if(pthread_create(&th, NULL, rendering, (void *)&pp_param) < 0) {  
  252.         printf("Rendering thread creation error\n");  
  253.         return -1;  
  254.     }  
  255.   
  256.     r = sem_init(&full, 0, 0);  
  257.     if(r < 0) {  
  258.         printf("sem_init failed\n");  
  259.         return -1;  
  260.     }  
  261.   
  262.     r = sem_init(&empty, 0, frame_need_cnt[0]);  
  263.     if(r < 0) {  
  264.         printf("sem_init failed\n");  
  265.         return -1;  
  266.     }  
  267.   
  268.     #ifdef FPS  
  269.         gettimeofday(&start, NULL);  
  270.     #endif  
  271.       
  272.     while(1)  
  273.     {  
  274.   
  275.         sem_wait(&empty);  
  276.       
  277.         //////////////////////////////////  
  278.         ///       5. DECODE            ///  
  279.         ///    (SsbSipH264DecodeExe)   ///  
  280.         //////////////////////////////////  
  281.         if (SsbSipH264DecodeExe(handle, nFrameLeng) != SSBSIP_H264_DEC_RET_OK) {  
  282.             printf("frame count : %d\n", frame_cnt);  
  283.             break;  
  284.         }  
  285.       
  286.       
  287.         //////////////////////////////////////////////  
  288.         ///    6. Obtaining the Output Buffer      ///  
  289.         ///      (SsbSipH264DecodeGetOutBuf)       ///  
  290.         //////////////////////////////////////////////  
  291.         SsbSipH264DecodeGetConfig(handle, H264_DEC_GETCONF_PHYADDR_FRAM_BUF, pYUVBuf);  
  292.   
  293.         frame_cnt++;  
  294.         queue[producer_idx].phy_addr = pYUVBuf[0];  
  295.         queue[producer_idx].frame_number = frame_cnt;  
  296.         producer_idx++;  
  297.         producer_idx %= frame_need_cnt[0];  
  298.   
  299.         sem_post(&full);  
  300.           
  301.       
  302.         /////////////////////////////  
  303.         // Next H.264 VIDEO stream //  
  304.         /////////////////////////////  
  305.         nFrameLeng = NextFrameH264(pFrameExCtx, &file_strm, pStrmBuf, INPUT_BUFFER_SIZE, NULL);  
  306.         if (nFrameLeng < 4)  
  307.             break;  
  308.       
  309.         if(frame_cnt == 1)  
  310.             ioctl(fb_fd, SET_OSD_START);  
  311.     }  
  312.   
  313.     pthread_join(th, NULL);  
  314.   
  315. #ifdef FPS  
  316.     gettimeofday(&stop, NULL);  
  317.     time = measureTime(&start, &stop);  
  318.     printf("Display Time : %u, Frame Count : %d, FPS : %f\n", time, frame_cnt, (float)frame_cnt*1000/time);  
  319. #endif  
  320.   
  321.     SsbSipH264DecodeDeInit(handle);  
  322.   
  323.     munmap(in_addr, file_size);  
  324.     munmap(fb_addr, fb_size);  
  325.     close(pp_fd);  
  326.     close(fb_fd);  
  327.     close(in_fd);  
  328.       
  329.     return 0;  
  330. }  
  331.   
  332.   
  333.   
  334. static void sig_del_h264(void)  
  335. {  
  336.     printf("signal handling\n");  
  337.   
  338.     pthread_exit(0);  
  339.     pthread_join(th, NULL);  
  340.     SsbSipH264DecodeDeInit(handle);  
  341.   
  342.     munmap(in_addr, file_size);  
  343.     munmap(fb_addr, fb_size);  
  344.     close(pp_fd);  
  345.     close(fb_fd);  
  346.     close(in_fd);  
  347. }