1. 程式人生 > >H264解碼SPS獲取寬高和幀率

H264解碼SPS獲取寬高和幀率

  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <string.h>
  4. #include <math.h>
  5. typedef  unsigned intUINT;  
  6. typedef  unsigned charBYTE;  
  7. typedef  unsigned longDWORD;  
  8. UINT Ue(BYTE *pBuff, UINT nLen, UINT &nStartBit)  
  9. {  
  10.     //計算0bit的個數
  11.     UINT nZeroNum = 0;  
  12.     while (nStartBit < nLen * 8)  
  13.     {  
  14.         if (pBuff[nStartBit / 8] & (0x80 >> (nStartBit % 8))) //&:按位與,%取餘
  15.         {  
  16.             break;  
  17.         }  
  18.         nZeroNum++;  
  19.         nStartBit++;  
  20.     }  
  21.     nStartBit ++;  
  22.     //計算結果
  23.     DWORD dwRet = 0;  
  24.     for (UINT i=0; i<nZeroNum; i++)  
  25.     {  
  26.         dwRet <<= 1;  
  27.         if (pBuff[nStartBit / 8] & (0x80 >> (nStartBit % 8)))  
  28.         {  
  29.             dwRet += 1;  
  30.         }  
  31.         nStartBit++;  
  32.     }  
  33.     return (1 << nZeroNum) - 1 + dwRet;  
  34. }  
  35. int Se(BYTE *pBuff, UINT nLen, UINT &nStartBit)  
  36. {  
  37.     int UeVal=Ue(pBuff,nLen,nStartBit);  
  38.     double k=UeVal;  
  39.     int nValue=ceil(k/2);//ceil函式:ceil函式的作用是求不小於給定實數的最小整數。ceil(2)=ceil(1.2)=cei(1.5)=2.00
  40.     if (UeVal % 2==0)  
  41.         nValue=-nValue;  
  42.     return nValue;  
  43. }  
  44. DWORD u(UINT BitCount,BYTE * buf,UINT &nStartBit)  
  45. {  
  46.     DWORD dwRet = 0;  
  47.     for (UINT i=0; i<BitCount; i++)  
  48.     {  
  49.         dwRet <<= 1;  
  50.         if (buf[nStartBit / 8] & (0x80 >> (nStartBit % 8)))  
  51.         {  
  52.             dwRet += 1;  
  53.         }  
  54.         nStartBit++;  
  55.     }  
  56.     return dwRet;  
  57. }  
  58. /** 
  59.  * H264的NAL起始碼防競爭機制 
  60.  * 
  61.  * @param buf SPS資料內容 
  62.  * 
  63.  * @無返回值 
  64.  */
  65. void de_emulation_prevention(BYTE* buf,unsigned int* buf_size)  
  66. {  
  67.     int i=0,j=0;  
  68.     BYTE* tmp_ptr=NULL;  
  69.     unsigned int tmp_buf_size=0;  
  70.     int val=0;  
  71.     tmp_ptr=buf;  
  72.     tmp_buf_size=*buf_size;  
  73.     for(i=0;i<(tmp_buf_size-2);i++)  
  74.     {  
  75.         //check for 0x000003
  76.         val=(tmp_ptr[i]^0x00) +(tmp_ptr[i+1]^0x00)+(tmp_ptr[i+2]^0x03);  
  77.         if(val==0)  
  78.         {  
  79.             //kick out 0x03
  80.             for(j=i+2;j<tmp_buf_size-1;j++)  
  81.                 tmp_ptr[j]=tmp_ptr[j+1];  
  82.             //and so we should devrease bufsize
  83.             (*buf_size)--;  
  84.         }  
  85.     }  
  86. }  
  87. /** 
  88.  * 解碼SPS,獲取視訊影象寬、高和幀率資訊 
  89.  * 
  90.  * @param buf SPS資料內容 
  91.  * @param nLen SPS資料的長度 
  92.  * @param width 影象寬度 
  93.  * @param height 影象高度 
  94.  * @成功則返回true , 失敗則返回false 
  95.  */
  96. bool h264_decode_sps(BYTE * buf,unsigned int nLen,int &width,int &height,int &fps)  
  97. {  
  98.     UINT StartBit=0;  
  99.     fps=0;  
  100.     de_emulation_prevention(buf,&nLen);  
  101.     int forbidden_zero_bit=u(1,buf,StartBit);  
  102.     int nal_ref_idc=u(2,buf,StartBit);  
  103.     int nal_unit_type=u(5,buf,StartBit);  
  104.     if(nal_unit_type==7)  
  105.     {  
  106.         int profile_idc=u(8,buf,StartBit);  
  107.         int constraint_set0_flag=u(1,buf,StartBit);//(buf[1] & 0x80)>>7;
  108.         int constraint_set1_flag=u(1,buf,StartBit);//(buf[1] & 0x40)>>6;
  109.         int constraint_set2_flag=u(1,buf,StartBit);//(buf[1] & 0x20)>>5;
  110.         int constraint_set3_flag=u(1,buf,StartBit);//(buf[1] & 0x10)>>4;
  111.         int reserved_zero_4bits=u(4,buf,StartBit);  
  112.         int level_idc=u(8,buf,StartBit);  
  113.         int seq_parameter_set_id=Ue(buf,nLen,StartBit);  
  114.         if( profile_idc == 100 || profile_idc == 110 ||  
  115.             profile_idc == 122 || profile_idc == 144 )  
  116.         {  
  117.             int chroma_format_idc=Ue(buf,nLen,StartBit);  
  118.             if( chroma_format_idc == 3 )  
  119.                 int residual_colour_transform_flag=u(1,buf,StartBit);  
  120.             int bit_depth_luma_minus8=Ue(buf,nLen,StartBit);  
  121.             int bit_depth_chroma_minus8=Ue(buf,nLen,StartBit);  
  122.             int qpprime_y_zero_transform_bypass_flag=u(1,buf,StartBit);  
  123.             int seq_scaling_matrix_present_flag=u(1,buf,StartBit);  
  124.             int seq_scaling_list_present_flag[8];  
  125.             if( seq_scaling_matrix_present_flag )  
  126.             {  
  127.                 forint i = 0; i < 8; i++ ) {  
  128.                     seq_scaling_list_present_flag[i]=u(1,buf,StartBit);  
  129.                 }  
  130.             }  
  131.         }  
  132.         int log2_max_frame_num_minus4=Ue(buf,nLen,StartBit);  
  133.         int pic_order_cnt_type=Ue(buf,nLen,StartBit);  
  134.         if( pic_order_cnt_type == 0 )  
  135.             int log2_max_pic_order_cnt_lsb_minus4=Ue(buf,nLen,StartBit);  
  136.         elseif( pic_order_cnt_type == 1 )  
  137.         {  
  138.             int delta_pic_order_always_zero_flag=u(1,buf,StartBit);  
  139.             int offset_for_non_ref_pic=Se(buf,nLen,StartBit);  
  140.             int offset_for_top_to_bottom_field=Se(buf,nLen,StartBit);  
  141.             int num_ref_frames_in_pic_order_cnt_cycle=Ue(buf,nLen,StartBit);  
  142.             int *offset_for_ref_frame=newint[num_ref_frames_in_pic_order_cnt_cycle];  
  143.             forint i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++ )  
  144.                 offset_for_ref_frame[i]=Se(buf,nLen,StartBit);  
  145.             delete [] offset_for_ref_frame;  
  146.         }  
  147.         int num_ref_frames=Ue(buf,nLen,StartBit);  
  148.         int gaps_in_frame_num_value_allowed_flag=u(1,buf,StartBit);  
  149.         int pic_width_in_mbs_minus1=Ue(buf,nLen,StartBit);  
  150.         int pic_height_in_map_units_minus1=Ue(buf,nLen,StartBit);  
  151.         width=(pic_width_in_mbs_minus1+1)*16;  
  152.         height=(pic_height_in_map_units_minus1+1)*16;  
  153.         int

    相關推薦

    H264解碼SPS獲取

    #include <stdio.h> #include <stdint.h> #include <string.h> #include <math.h> typedef  unsigned intUINT;   typed

    H.264(H264解碼SPS獲取解析度

    #include <stdio.h> #include <stdint.h> #include <string.h> #include <math.h> typedef unsigned int UINT; typedef

    H264/H265碼流中獲取

    在做碼流分析時,影象解析度、幀率這類的基本資訊,當然不可少。本文介紹如何從NAL中計算到影象寬、高,還有解析度。於是H264和H265有相似性,就在一起寫了。 一、從碼流獲得寬、高 1、H264 寬高可從SPS欄位計算得到,公式如下: Width = (pic_width_in_mb

    js 獲取圖片 圖片大小

    src 查看 nts 執行 input java image 創建 wid         獲取要查看大小的img   varimg_url = ‘http://img5.imgtn.bdimg.com/it/u=4267222417,1017407570&fm=

    C# 獲取當前屏幕的位置

    rim 如何獲取 寬高 尺寸 裏的 獲取 建議 nds prim 上一篇博客《C# 獲取當前屏幕DPI》,介紹了如何獲取當前屏幕的DPI設置 本章主要介紹如何獲取當前窗口所在屏幕的信息 當前屏幕信息 如果當前是單屏幕,可以直接獲取主屏幕 var prim

    獲取遠端圖片的體積大小(php封裝方法)

    /** * 獲取遠端圖片的寬高和體積大小 * * @param string $url 遠端圖片的連結 * @param string $type 獲取遠端圖片資源的方式, 預設為 curl 可選 fread * @param boolean $isGetFiles

    【Android】獲取控制元件的位置

    獲取控制元件的絕對位置(包括狀態列)可以用這種方法,呼叫方法後,location[0]和location[1]就分別被賦值了。 int[] location = new int[2]; view.getLocationOnScreen(loc

    獲取當前螢幕的畫素點

    import React, {Component} from 'react'; import { AppRegistry,//註冊 StyleSheet,//樣式 Text,/

    Android——View的設定多種獲取的方法、layout_grivaty與grivaty的區別

    一、設定控制元件寬高 設定Layout_width/height引發的寬高思考 方式一: 結果不符合預期。 執行結果: 方式二: 結果符合預期。 如下圖: Android控制元件寬高的規則:   Android下的控制元件預設沒有寬高,是由

    JS 、JQ 獲取總結

    bsp image 技術 獲取 cnblogs 技術分享 src 寬高 png JS 、JQ 獲取寬高總結

    div的最小最大

    才會 width 問題 就會 裏的 屬性 解決 auto 內容 div的最小寬高和最大寬高很少使用但是很實用,今天敲代碼,就遇到了,要在div裏設置滾動條,眾所周知,一般是設overflow-y:auto,但需要一個高度,只有div裏的內容超過這個高度時,才會有滾動條效果。

    小程序之創建節點選擇器獲取

    http cli GC idt exe class 創建 程序 sele 1.獲取方法 onLoad: function (e) { var that = this; wx.createSelectorQuery().select(‘

    JavaScript 中禁止用戶右鍵菜單,復制,選取,Ctrl,Alt,Shift. 獲取

    UNC 瀏覽器 func nth 包括 窗口 etl ron menu //禁用右鍵菜單 document.oncontextmenu = function(){ event.returnValue = false; } //禁用選取內容 document.ons

    ImageView獲取

    public @override eas ide red 獲取 runnable creat 圖片 在Android裏放置一個ImageView,寬和高都是200.1、在Android OnCreate裏如果直接使用iv.GetWidth()返回值為0. 2、如果使用網上

    ***獲取螢幕 :width(),height(),clientHeight,clientWidth 獲取的區別*

    獲取螢幕寬高 :width(),height(),clientHeight,clientWidth 獲取寬高的區別 $(window).width() & $(window).height():獲得的是螢幕可視區域的寬高,不包括滾動條與工具條。 $(window).heigh

    Java小遊戲DanceWithStars(二):修改本地文件中的圖片(圖片圖片型別)以及將圖片設定為JButton的影象

    1. 修改本地文件中的圖片(圖片寬高和圖片型別) 為了使下載的圖片素材都具有統一的width和height,需要對下載來的圖片重新處理並另存 主要思想是:構造一個BufferedImage物件,用ImageIO.read()讀入,再用ImageIO.write()重畫 程式碼如下: 1 pac

    H264解析sps獲取解析度等資訊

    #include <stdio.h> #include <stdint.h> #include <string.h> #include <math.h>   typedef  unsigned int UINT; typedef &

    DOM中獲取、位置總結

    原生JS 一、文件、視窗的寬高和位置 // 獲取螢幕的寬高 window.screen.height | window.screen.width // 螢幕可用工作區寬高 window.screen.availHeight | window.screen.availWidth //

    CCD靶面尺寸對應的對角線長度

    CCD靶面規格尺寸:單位mm 規格 寬 高 對角線 1/6" 2.4 1.8 3 1/4" 3.2 2.4 4 3.6 2.7 4.5 1/3.6" 4 3 5 1/3.2" 4.536

    View在屬性為wrap_content/match_parent時獲取不準確的解決辦法

    View 或ViewGroup在建立時設定寬度高度為match_parent或者wrap_content時,通過getWidth()、getHeight()或者getMeasuredWidth()、getMeasuredHeight()不能獲取到真實的寬高.