bit_rate.c
上传用户:poi891205
上传日期:2013-07-15
资源大小:9745k
文件大小:12k
源码类别:

DVD

开发平台:

C/C++

  1. /*
  2. *  Terry,2004/3/25 04:10PM
  3. */
  4. #include "bit_rate.h"
  5. #include "global.h"
  6. #include "util.h"
  7. #include "atapi_if.h"
  8. #include "image.h"
  9. #include "kinf.h"
  10. #include "stc.h"
  11. #ifndef DVDRELEASE
  12.     //#define DBG_BIT_RATE    1
  13. #endif
  14. #ifdef DBG_BIT_RATE
  15.     //#define DBG_PRE_PARSE_MP3
  16.     //#define DBG_MP3_HEADER_STREAM
  17.     //#define DBG_AV_BITRATE
  18. #endif
  19. #ifndef DBG_BIT_RATE
  20.     #undef printf
  21.     #undef print_block
  22.     #define printf(f, a...) do {} while (0)
  23.     #define print_block(x,y) do {} while (0)
  24. #endif
  25. // **************************************************************************************** //
  26. #ifdef DISPLAY_DATA_BIT_RATE
  27. UINT16 bit_rate_val = 0;
  28. UINT32 sample_addr  = 0;
  29. // **************************************************************************************** //
  30. void init_bit_rate(void)
  31. {
  32.     bit_rate_val    =   0;
  33.     sample_addr     =   0;
  34. }
  35. // **************************************************************************************** //
  36. //bit-rate range: 8K ~ 10Mbits
  37. //bit_rate_val:   8~10K 
  38. #define MAX_BIT_RATE  (10*1024*1024)
  39. #define MAX_BYTE_RATE (MAX_BIT_RATE/8)
  40. #define MAX_SECTOR_NS (MAX_BYTE_RATE/2048)
  41. #define MAX_KB_NS     (MAX_BYTE_RATE/1024) //1280
  42. #define MIN_BIT_RATE  (8*1024)
  43. #define MIN_BYTE_RATE (MIN_BIT_RATE/8)
  44. #define MIN_SECTOR_NS (MIN_BYTE_RATE/2048)
  45. #define MIN_KB_NS     (MIN_BYTE_RATE/1024) //1
  46. // **************************************************************************************** //
  47. void sample_bit_rate_per_sec(void)
  48. {
  49.     int tmp;
  50.     
  51.     if( adv_search_time == 0 )
  52.     {
  53.         if( cd_type_loaded == CDDVD ) 
  54.             tmp = play_abs_addr;
  55.         else 
  56.             tmp = msf2l( s_msf );
  57.         
  58.         if( tmp > sample_addr )
  59.         {
  60.             tmp = ( tmp - sample_addr ) * 2;
  61.             if( ( tmp >= MIN_KB_NS ) && ( tmp <= MAX_KB_NS ) )
  62.             {
  63.                 bit_rate_val = tmp * 8; // 8 ~ 10240
  64.                 printf( "<%d>n", bit_rate_val );
  65.             }
  66.         }
  67.         
  68.         sample_addr = tmp;
  69.     }
  70. }
  71. // **************************************************************************************** //
  72. #else
  73. void    init_bit_rate(void)             {}
  74. void    sample_bit_rate_per_sec(void)   {}
  75. #endif
  76. #ifdef SUPPORT_PRECALCULATE_VIDEOBITRATE_FOR_FILE_MODE
  77. // **************************************************************************************** //
  78. UINT32 GetBPS()
  79. {
  80.     if( adv_search_time != 0 )
  81.         return 0;
  82.         
  83.     UINT32 video_bitrate    = ki_get_video_bitrate();
  84.     if( video_bitrate == 0 )
  85.         return 0;
  86.     
  87.     UINT32 audio_bitrate    = ( ( *(bpcm_ya_ptr+44) | ( *(bpcm_ya_ptr+43) << 8) ) ) << 10;
  88.     UINT32 uiBitrate;
  89.     if( pDSV->dAv_AST_Ns > 0 )
  90.         uiBitrate = video_bitrate + audio_bitrate * pDSV->dAv_AST_Ns;
  91.     else
  92.         uiBitrate = video_bitrate + audio_bitrate;
  93.     #ifdef DBG_AV_BITRATE
  94.     printf("tt: %d, v: %d, a: %dn", uiBitrate, video_bitrate, audio_bitrate );
  95.     #endif
  96.         
  97.     return uiBitrate;
  98. }
  99. #else
  100. // **************************************************************************************** //
  101. extern void     StartBPS( UINT8 uiInterval )    { uiInterval = uiInterval; }
  102. extern UINT32   GetBPS()                        { return 0; }
  103. // **************************************************************************************** //
  104. #endif
  105. #ifdef REMAIN_TIME
  106. // **************************************************************************************** //
  107. typedef enum 
  108. {
  109.     VBR_MP3_PARSE_FIRST_FRAME,
  110.     VBR_MP3_PARSE_ALL_FRAME,
  111. }   VBR_MP3_FRAME_TYPE;
  112. typedef struct{
  113.     int bitrate;
  114.     char *nextHeaderPtr;
  115. }   MP3INFORM;
  116. // **************************************************************************************** //
  117. static void GetBitRateInform( MP3INFORM* pHeaderInform,UINT32 length )
  118. {
  119.     if( !pHeaderInform || length == 0 )
  120.         return;
  121.     UINT8   layerTable[4] = {0,3,2,1};
  122.     
  123.     UINT16  freqTable[9] = { 44100, 48000, 32000, 22050, 24000, 16000 , 11025 , 12000 , 8000 };
  124.     
  125.     UINT16  bitrateTable[2][3][16] = { 
  126.        { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,},
  127.          {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,},
  128.          {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} },
  129.     
  130.        { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,},
  131.          {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
  132.          {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} }
  133.     };
  134.     
  135.     char*   stream          = pHeaderInform->nextHeaderPtr;
  136.     UINT32  Header          = 0xFFFFFFFF;
  137.     UINT32  NextHeader      = 0xFFFFFFFF;
  138.     UINT32  syncwordMask    = 0xFFF00000;
  139.     UINT32  layerMask       = 0x00060000;
  140.     UINT32  bitrateMask     = 0x0000F000;
  141.     UINT32  sampleMask      = 0x00000C00;
  142.     char*   NextSyncPtr     = NULL;
  143.     int     syncshift       = 20;
  144.     int     layershift      = 17;
  145.     int     bitrateshift    = 12;
  146.     int     sampleshift     = 10;
  147.     int     hitSyncWord     = 0;
  148.     int     layerNo         = 0;
  149.     int     bitrate         = 0;
  150.     int     freqs           = 0;
  151.     int     bitrateIndex, ID,slotNo;
  152.     UINT32  syncLag;
  153.     
  154.     Header=((unsigned int)(*stream))<<24;                   stream++;
  155.     Header=Header|((((unsigned int)(*stream))<<24)>>8);     stream++;
  156.     Header=Header|((((unsigned int)(*stream))<<24)>>16);    stream++;
  157.     Header=Header|(((unsigned int)(*stream)<<24)>>24);      stream++;
  158.     length=length-4;
  159.     #ifdef DBG_MP3_HEADER_STREAM    
  160.     printf("Header = %x n", Header);
  161.     #endif
  162.     //find the sync word
  163.     while((hitSyncWord==0)&&(length!=0))
  164.     {
  165.         if(((Header&syncwordMask)>>syncshift)==0xFFF) //check sync word
  166.         {
  167.          layerNo=layerTable[((Header&layerMask)>>layershift)];
  168.          if(layerNo!=0)  //check layer No
  169.          {
  170.          bitrateIndex=((Header&bitrateMask)>>bitrateshift);
  171.          if(bitrateIndex!=0xf&&bitrateIndex!=0x0)//check bitrate index
  172.          {
  173.          ID=(Header & (1<<19)) ? 0x0 : 0x1;
  174.          bitrate=bitrateTable[ID][layerNo-1][bitrateIndex];
  175.          if(((Header&sampleMask)>>sampleshift)!=0x3) //check sampling frequency
  176.          {
  177.          freqs=freqTable[((Header&sampleMask)>>sampleshift)];
  178.          if(layerNo==1)
  179.          {//layer I
  180.          syncLag=12*bitrate*1000/freqs;
  181.          }
  182.          else
  183.          {//layer II,III
  184.          syncLag=144*bitrate*1000/freqs;
  185.          }
  186.          for(slotNo=0;slotNo<2;slotNo++)
  187.          {
  188.          NextSyncPtr=stream-4+syncLag+slotNo;
  189.          if(length>syncLag)
  190.          {
  191.          NextHeader=((unsigned int)(*NextSyncPtr))<<24;                      NextSyncPtr++;
  192.          NextHeader=NextHeader|((((unsigned int)(*NextSyncPtr))<<24)>>8);    NextSyncPtr++;
  193.          NextHeader=NextHeader|((((unsigned int)(*NextSyncPtr))<<24)>>16);   NextSyncPtr++;
  194.          NextHeader=NextHeader|(((unsigned int)(*NextSyncPtr)<<24)>>24);     NextSyncPtr++;
  195.         
  196.          if(((NextHeader&syncwordMask)>>syncshift)==0xFFF) //confirm sync word
  197.          {
  198.          if(((NextHeader&layerMask)>>layershift)!=0x0)
  199.          {
  200.          if(((NextHeader&bitrateMask)>>bitrateshift)!=0xf&&((NextHeader&bitrateMask)>>bitrateshift)!=0x0)
  201.          {
  202.          if(((NextHeader&sampleMask)>>sampleshift)!=0x3)
  203.          {
  204.          hitSyncWord=1;
  205.         
  206.     #ifdef DBG_MP3_HEADER_STREAM
  207.          printf("len=%d,header=%x ",length,Header);
  208.     #endif
  209.     break;
  210.          }
  211.               }
  212.                                  }
  213.          }
  214.          }
  215.          else
  216.          {
  217.     #ifdef DBG_MP3_HEADER_STREAM
  218.              printf("syncLag=%d,length=%dn",syncLag,length);
  219.     #endif
  220.              bitrate=0;
  221.          }
  222.          }
  223.          }
  224.          }
  225.          }
  226.         }
  227.         
  228.         if(hitSyncWord==0) //shift
  229.         {
  230.             Header=Header<<8;  
  231.             Header=Header|(((unsigned int)(*stream)<<24)>>24);
  232.             stream++;
  233.             length--;
  234.         }
  235.     }
  236.     
  237.     #ifdef DBG_PRE_PARSE_MP3    
  238.     printf("bitrate =%d,layer=%d,freq=%d n",bitrate,layerNo,freqs);
  239.     #endif    
  240.     
  241.     if( hitSyncWord == 1 )
  242.     {
  243.      pHeaderInform->bitrate          = bitrate;
  244.      pHeaderInform->nextHeaderPtr    = stream;
  245.     }
  246.     else
  247.     {
  248.      pHeaderInform->bitrate          = 0;
  249.      pHeaderInform->nextHeaderPtr    = 0;
  250.     }
  251. }
  252. // **************************************************************************************** //
  253. static UINT32 ParseMP3BitRateFromAudioStream( VBR_MP3_FRAME_TYPE enType, char* pBuf, UINT32 uiLen )
  254. {
  255.     if( !pBuf || uiLen == 0 )
  256.         return 0;
  257.     MP3INFORM HeaderInform;
  258.     HeaderInform.nextHeaderPtr = pBuf;
  259.     
  260.     if( enType == VBR_MP3_PARSE_FIRST_FRAME )
  261.     {   
  262.         GetBitRateInform( &HeaderInform, uiLen );
  263.         return HeaderInform.bitrate;
  264.     }
  265.     else if( enType == VBR_MP3_PARSE_ALL_FRAME )
  266.     {
  267.         int     lastpBuf        = (int)pBuf + uiLen;
  268.         int     sumBitRate      = 0;
  269.         UINT32  uiBitRateCount  = 0;
  270.         GetBitRateInform( &HeaderInform, uiLen );
  271.         
  272.         while( HeaderInform.bitrate != 0 )
  273.         {
  274.             sumBitRate += HeaderInform.bitrate;
  275.             uiBitRateCount++;
  276.             int lengthRemain = lastpBuf - (UINT32)(HeaderInform.nextHeaderPtr);
  277.             GetBitRateInform( &HeaderInform, lengthRemain );
  278.         }
  279.     #ifdef DBG_PRE_PARSE_MP3
  280.         printf( "average bitrate of LBA %dn", ( uiBitRateCount > 0 ) ? ( sumBitRate / uiBitRateCount ) : 0 );
  281.     #endif    
  282.         return ( uiBitRateCount > 0 ) ? ( sumBitRate / uiBitRateCount ) : 0;    
  283.     }
  284.     
  285.     return 0;
  286. }
  287. // **************************************************************************************** //
  288. UINT32 PreParseMP3BitRate( VBR_MP3_LBA_TYPE enType, UINT32 uiParam, UINT32 uiLba, UINT32 uiLen )
  289. {
  290.     int     i           = 0;
  291.     UINT32  uiLbaOffset = 0;
  292.     UINT32  uiBitRate   = 0;
  293.     UINT32  uiCount     = 0;
  294.     UINT32  uiLbaCount  = 0;
  295.     UINT32  uiStart     = uiLba;
  296.         
  297.     switch( enType )
  298.     {
  299.     case VBR_MP3_INTERVAL_N_LBA:
  300.         uiLbaCount  = uiLen;
  301.         uiLbaOffset = uiParam;
  302.         break;
  303.         
  304.     case VBR_MP3_FIRST_N_LBA:
  305.         uiLbaCount  = uiParam > uiLen ? uiLen : uiParam;
  306.         uiLbaOffset = 1;
  307.         break;
  308.     case VBR_MP3_CENTER_N_LBA:  // 2004/08/10 yltseng
  309.     {
  310.         UINT32 uiTmp = ( uiLen + uiParam ) >> 1;
  311.         uiLbaCount  = uiParam > uiTmp ? uiTmp : uiParam;
  312.         uiLbaOffset = 1;
  313.         uiStart     = uiLba + ( ( uiLen - uiParam ) > 1 );
  314.     }
  315.         break;
  316.         
  317.     case VBR_MP3_N_LBA:
  318.         uiLbaCount  = uiLen;
  319.         uiLbaOffset = uiLen / uiParam;
  320.         break;
  321.     }
  322.   
  323.     while( i < (int) uiLbaCount )
  324.     {
  325.         if( ran_read( pwb, l2msf( ( uiStart + i ) ), 2048 ) == ATAPI_OK )
  326.         {
  327.             UINT32 uiCurLbaBitRate = ParseMP3BitRateFromAudioStream( VBR_MP3_PARSE_FIRST_FRAME, pwb, 2048 );    // 2004/12/17 yltseng
  328.             if( uiCurLbaBitRate )
  329.             {
  330.                 uiBitRate += uiCurLbaBitRate;
  331.                 uiCount++;
  332.             }
  333.         }
  334.         i += uiLbaOffset;
  335.     }
  336.     
  337.     #ifdef DBG_PRE_PARSE_MP3
  338.     printf( "average bitrate of MP3 file %dn", ( uiCount > 0 ) ? ( uiBitRate / uiCount ) : 0 );
  339.     #endif
  340.     return ( uiCount > 0 ) ? ( uiBitRate / uiCount ) : 0;
  341. }
  342. // **************************************************************************************** //
  343. #else
  344. UINT32 PreParseMP3BitRate( VBR_MP3_LBA_TYPE enType, UINT32 uiParam, UINT32 uiLba, UINT32 uiLen ) 
  345. { enType = enType; uiParam = uiParam; uiLba = uiLba, uiLen = uiLen; return 0; }
  346. #endif
  347. // **************************************************************************************** //