home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2004 March / PCWELT_3_2004.ISO / pcwsoft / flaskmpeg_078_39_src.z.exe / flaskmpeg / Video / getbits.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-28  |  6.3 KB  |  293 lines

  1. /* getbits.c, bit level routines                                            */
  2.  
  3. /*
  4.  * All modifications (mpeg2decode -> mpeg2play) are
  5.  * Copyright (C) 1996, Stefan Eckart. All Rights Reserved.
  6.  */
  7.  
  8. /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
  9.  
  10. /*
  11.  * Disclaimer of Warranty
  12.  *
  13.  * These software programs are available to the user without any license fee or
  14.  * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims
  15.  * any and all warranties, whether express, implied, or statuary, including any
  16.  * implied warranties or merchantability or of fitness for a particular
  17.  * purpose.  In no event shall the copyright-holder be liable for any
  18.  * incidental, punitive, or consequential damages of any kind whatsoever
  19.  * arising from the use of these programs.
  20.  *
  21.  * This disclaimer of warranty extends to the user of these programs and user's
  22.  * customers, employees, agents, transferees, successors, and assigns.
  23.  *
  24.  * The MPEG Software Simulation Group does not represent or warrant that the
  25.  * programs furnished hereunder are free of infringement of any third-party
  26.  * patents.
  27.  *
  28.  * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
  29.  * are subject to royalty fees to patent holders.  Many of these patents are
  30.  * general enough such that they are unavoidable regardless of implementation
  31.  * design.
  32.  *
  33.  */
  34.  
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37.  
  38. #include "config.h"
  39. extern "C"
  40. {
  41. #include "global.h"
  42. }
  43.  
  44. #include "..\Video\VideoWrapper.h"
  45.  
  46. extern VideoWrapper *myVideo;
  47.  
  48. #define Get_Bits1() (Get_Bits(1))
  49.  
  50. void Fill_Buffer()
  51. {
  52.   bool bReadOk;
  53.  
  54.   bReadOk = myVideo->ReadVideoData( (ui8**)&ld->Rdbfr, &VIDEO_BUFFER_SIZE );
  55.  
  56.   ld->Rdmax=ld->Rdbfr + VIDEO_BUFFER_SIZE;
  57.  
  58.   ld->Rdptr = ld->Rdbfr;
  59.  
  60.   if (System_Stream_Flag)
  61.     ld->Rdmax -= VIDEO_BUFFER_SIZE;
  62.  
  63.   myVideo->m_bReadError = false;
  64.   /* end of the bitstream file */
  65.   if (!bReadOk)
  66.   {
  67.     // Flag this to the videowrapper
  68.     myVideo->m_bReadError = true;
  69.  
  70.     // Even when the read can fail, we can have something
  71.     // as payload.
  72.     // The size of the payload its at VIDEO_BUFFER_SIZE
  73.     ui32 BufferPtr = 0;
  74.  
  75.     /* pad until the next to the next 32-bit word boundary */
  76.     while (BufferPtr & 3)
  77.       ld->Rdbfr[BufferPtr++] = 0;
  78.  
  79.     /* pad the buffer with sequence end codes */
  80.     while (BufferPtr + 4 < VIDEO_BUFFER_SIZE )
  81.     {
  82.       ld->Rdbfr[BufferPtr++] = SEQUENCE_END_CODE>>24;
  83.       ld->Rdbfr[BufferPtr++] = SEQUENCE_END_CODE>>16;
  84.       ld->Rdbfr[BufferPtr++] = SEQUENCE_END_CODE>>8;
  85.       ld->Rdbfr[BufferPtr++] = SEQUENCE_END_CODE&0xff;
  86.     }
  87.   }
  88. }
  89.  
  90. /* initialize buffer, call once before first getbits or showbits */
  91.  
  92. void Initialize_Buffer()
  93. {
  94.   VIDEO_BUFFER_SIZE=65536; 
  95.   ld->Incnt = 0;
  96.   ld->Rdptr = ld->Rdbfr + VIDEO_BUFFER_SIZE;
  97.   //ld->Rdptr = ld->Rdbfr + 2048;
  98.   ld->Rdmax = ld->Rdptr;
  99.  
  100. #ifdef VERIFY
  101.   /*  only the verifier uses this particular bit counter 
  102.    *  Bitcnt keeps track of the current parser position with respect
  103.    *  to the video elementary stream being decoded, regardless 
  104.    *  of whether or not it is wrapped within a systems layer stream 
  105.    */
  106.   ld->Bitcnt = 0;
  107. #endif
  108.  
  109.   ld->Bfr = 0;
  110.   Flush_Buffer(0); /* fills valid data into bfr */
  111.   //Fill_Buffer();  
  112. }
  113.  
  114. void Reset_Bits_Buffer()
  115. {
  116.   ld->Incnt = 0;
  117.   ld->Rdptr = ld->Rdmax;
  118. }
  119.  
  120. /* return next n bits (right adjusted) without advancing */
  121.  
  122. unsigned int Show_Bits(int N)
  123. {
  124.   return ld->Bfr >> (32-N); 
  125. }
  126.  
  127.  
  128. /* return next bit (could be made faster than Get_Bits(1)) */
  129. /*
  130. unsigned int Get_Bits1()
  131. {
  132.   return Get_Bits(1);
  133. }
  134. */
  135.  
  136. /* advance by n bits */
  137.  
  138. void Flush_Buffer(int N)
  139. {
  140.   int Incnt;
  141.       
  142.   ld->Bfr <<= N;
  143.  
  144.   Incnt = ld->Incnt -= N;
  145.  
  146.   if (Incnt <= 24)
  147.   {
  148.    if (ld->Rdptr + 4 < ld->Rdbfr + VIDEO_BUFFER_SIZE )
  149.     {
  150.       do
  151.       {
  152.         ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
  153.         Incnt += 8;
  154.       }
  155.       while (Incnt <= 24);
  156.     }
  157.     else
  158.     {
  159.       do
  160.       {
  161.         if (ld->Rdptr >= ld->Rdbfr+VIDEO_BUFFER_SIZE)
  162.           Fill_Buffer();
  163.         ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
  164.         Incnt += 8;
  165.       }
  166.       while (Incnt <= 24);
  167.     }
  168.     ld->Incnt = Incnt;
  169.   }
  170.  
  171. #ifdef VERIFY 
  172.   ld->Bitcnt += N;
  173. #endif 
  174.  
  175. }
  176.  
  177.  
  178. /* return next n bits (right adjusted) */
  179. /* 
  180. unsigned int  Get_Bits(int N)
  181. {
  182.   unsigned int Val;
  183.  
  184.   Val = Show_Bits(N);
  185.   Flush_Buffer(N);
  186.  
  187.   return Val;
  188. }
  189. */
  190.  
  191. // This function returns the position of the 
  192. // stream in offset bytes from the buffer
  193. int Get_Buffer_Pos()
  194. {
  195.     // Work out number of bytes that were used to fill the current ld->Bfr
  196.     int nBytesBuffer = ( ld->Incnt + 7 ) / 8;
  197.     return ld->Rdptr - ld->Rdbfr - nBytesBuffer;
  198. }
  199. unsigned int Get_Buffer_Size()
  200. {
  201.     return VIDEO_BUFFER_SIZE;
  202. }
  203.  
  204. // Sets the bits buffer in a known state, removing previous data.
  205. unsigned int Set_Buffer_State( ui8 *buffer, ui32 offset, ui32 buffersize )
  206. {
  207.   VIDEO_BUFFER_SIZE = buffersize; 
  208.   ld->Incnt = 0;
  209.   ld->Rdbfr = (unsigned char *)buffer;
  210.   ld->Rdptr = ld->Rdbfr + offset;
  211.   ld->Rdmax = ld->Rdptr;
  212.   ld->Bfr = 0;
  213.   return 1;
  214. }
  215.  
  216. unsigned int  Get_Bits(int N)
  217. {
  218.   unsigned int Val;
  219.   int Incnt;
  220.   
  221.   Val = ld->Bfr >> (32-N);
  222.   ld->Bfr <<= N; 
  223.   
  224.   Incnt = ld->Incnt -= N;
  225.  
  226.   if (Incnt <= 24)
  227.   {
  228.     if (ld->Rdptr < ld->Rdbfr+VIDEO_BUFFER_SIZE-4)
  229.     {
  230.       do
  231.       {
  232.         ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
  233.         Incnt += 8;
  234.       }
  235.       while (Incnt <= 24);
  236.     }
  237.     else 
  238.     {
  239.       do
  240.       {
  241.         if (ld->Rdptr >= ld->Rdbfr+VIDEO_BUFFER_SIZE)
  242.           Fill_Buffer();
  243.         ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
  244.         Incnt += 8;
  245.       }
  246.       while (Incnt <= 24);
  247.     }
  248.     ld->Incnt = Incnt;
  249.   }
  250.  
  251. #ifdef VERIFY 
  252.   ld->Bitcnt += N;
  253. #endif /* VERIFY */
  254.  
  255.   return Val;
  256. }
  257.  
  258.  
  259. void Flush_Buffer32()
  260. {
  261.     int Incnt;
  262.     
  263.     ld->Bfr = 0;
  264.     
  265.     Incnt = ld->Incnt;
  266.     Incnt -= 32;
  267.     
  268.     while (Incnt <= 24)
  269.     {
  270.         if (ld->Rdptr >= ld->Rdbfr+VIDEO_BUFFER_SIZE)
  271.             Fill_Buffer();
  272.         ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
  273.         Incnt += 8;
  274.     }
  275.     
  276.     ld->Incnt = Incnt;
  277.     
  278. #ifdef VERIFY 
  279.     ld->Bitcnt += 32;
  280. #endif /* VERIFY */
  281. }
  282.  
  283.  
  284. unsigned int Get_Bits32()
  285. {
  286.     unsigned int l;
  287.     
  288.     l = Show_Bits(32);
  289.     Flush_Buffer32();
  290.     
  291.     return l;
  292. }
  293.