GetBits.cpp
上传用户:panstart
上传日期:2022-04-12
资源大小:199k
文件大小:6k
源码类别:

IP电话/视频会议

开发平台:

C++ Builder

  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. //
  4. //    Project     : VideoNet version 1.1.
  5. //    Description : Peer to Peer Video Conferencing over the LAN.
  6. //   Author      : Nagareshwar Y Talekar ( nsry2002@yahoo.co.in)
  7. //    Date        : 15-6-2004.
  8. //
  9. //    This is the modified version of tmndecode (H.263 decoder) 
  10. //    written by Karl & Robert.It was in ANSI C. I have converted into C++
  11. //    so that it can be integrated into any windows application. I have 
  12. //    removed some of the files which had display and file storing 
  13. //    functions.I have removed the unnecessary code and also added some
  14. //    new files..
  15. //   Original library dealt with files. Input & Output , both were files.
  16. //    I have done some major changes so that it can be used for real time 
  17. //    decoding process. Now one can use this library for decoding H263 frames. 
  18. //
  19. //
  20. //    File description : 
  21. //    Name    : GetBits.cpp
  22. //
  23. /////////////////////////////////////////////////////////////////////////////
  24. /************************************************************************
  25.  *
  26.  *  getbits.c, bit level routines for tmndecode (H.263 decoder)
  27.  *  Copyright (C) 1996  Telenor R&D, Norway
  28.  *        Karl Olav Lillevold <Karl.Lillevold@nta.no>
  29.  *
  30.  *  This program is free software; you can redistribute it and/or modify
  31.  *  it under the terms of the GNU General Public License as published by
  32.  *  the Free Software Foundation; either version 2 of the License, or
  33.  *  (at your option) any later version.
  34.  *
  35.  *  This program is distributed in the hope that it will be useful,
  36.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  37.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  38.  *  GNU General Public License for more details.
  39.  *
  40.  *  You should have received a copy of the GNU General Public License
  41.  *  along with this program; if not, write to the Free Software
  42.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  43.  *
  44.  *  Karl Olav Lillevold               <Karl.Lillevold@nta.no>
  45.  *  Telenor Research and Development
  46.  *  P.O.Box 83                        tel.:   +47 63 84 84 00
  47.  *  N-2007 Kjeller, Norway            fax.:   +47 63 81 00 76
  48.  *
  49.  *  Robert Danielsen                  e-mail: Robert.Danielsen@nta.no
  50.  *  Telenor Research and Development  www:    http://www.nta.no/brukere/DVC/
  51.  *  P.O.Box 83                        tel.:   +47 63 84 84 00
  52.  *  N-2007 Kjeller, Norway            fax.:   +47 63 81 00 76
  53.  *  
  54.  ************************************************************************/
  55. /*
  56.  * based on mpeg2decode, (C) 1994, MPEG Software Simulation Group
  57.  * and mpeg2play, (C) 1994 Stefan Eckart
  58.  *                         <stefan@lis.e-technik.tu-muenchen.de>
  59.  *
  60.  */
  61. #include "GetBits.h"
  62. /* to mask the n least significant bits of an integer */
  63. static unsigned int msk[33] =
  64. {
  65.   0x00000000,0x00000001,0x00000003,0x00000007,
  66.   0x0000000f,0x0000001f,0x0000003f,0x0000007f,
  67.   0x000000ff,0x000001ff,0x000003ff,0x000007ff,
  68.   0x00000fff,0x00001fff,0x00003fff,0x00007fff,
  69.   0x0000ffff,0x0001ffff,0x0003ffff,0x0007ffff,
  70.   0x000fffff,0x001fffff,0x003fffff,0x007fffff,
  71.   0x00ffffff,0x01ffffff,0x03ffffff,0x07ffffff,
  72.   0x0fffffff,0x1fffffff,0x3fffffff,0x7fffffff,
  73.   0xffffffff
  74. };
  75. /* initialize buffer, call once before first getbits or showbits */
  76. void initbits()
  77. {
  78.   ld->incnt = 0;
  79.   ld->rdptr = ld->rdbfr + 2048;
  80.   ld->bitcnt = 0;
  81. }
  82. /*
  83. * Read bytes from buffer
  84. *
  85. */
  86. void fillbfr()
  87. {
  88.   int l;
  89.   // Store prev 4 bytes into first 4 bytes....
  90.   // new data is appended to these prev bytes....
  91.   ld->inbfr[0] = ld->inbfr[8];
  92.   ld->inbfr[1] = ld->inbfr[9];
  93.   ld->inbfr[2] = ld->inbfr[10];
  94.   ld->inbfr[3] = ld->inbfr[11];
  95. // check if whether we have to read the data. ..or it already exist
  96.   if (ld->rdptr>=ld->rdbfr+2048)
  97.   {
  98.       // copy the data from buffer..
  99.   // ....2048 bytes at a time...
  100.   if(csize>=2048)
  101.   {
  102.   memcpy(ld->rdbfr,cframe+cindex,2048);
  103.   l=2048;
  104.   cindex+=2048;
  105.   csize-=2048;
  106.   }
  107.   else
  108.   {
  109. // Read all available data from buffer
  110. if(csize>0)
  111. {
  112.     memcpy(ld->rdbfr,cframe+cindex,csize);
  113. l=csize;
  114. cindex+=csize;
  115. csize=0;
  116. }
  117. else  // buffer is empty
  118. {
  119. l=0;
  120. csize=0;
  121. }
  122.   }
  123.    
  124. // reset the pointer...to start of data
  125.   ld->rdptr = ld->rdbfr;
  126.     
  127.   
  128.   // if less data is read....then append "End Sequence code"
  129.   if (l<2048)
  130.   {
  131.   if (l<0)
  132. l = 0;
  133.   while (l<2048)   /* Add recognizable sequence end code */
  134.   {
  135. ld->rdbfr[l++] = 0;
  136. ld->rdbfr[l++] = 0;
  137. ld->rdbfr[l++] = (1<<7) | (SE_CODE<<2);
  138.   }
  139.   }
  140.   
  141.   
  142.   }
  143.   // store 8 byte of data into in buffer
  144.   for (l=0; l<8; l++)
  145.     ld->inbfr[l+4] = ld->rdptr[l];
  146.   //update pointers..
  147.   ld->rdptr+= 8; // 8 bytes..
  148.   ld->incnt+= 64;       // 64 bits...
  149. }
  150. /* return next n bits (right adjusted) without advancing */
  151. unsigned int showbits(int n)
  152. {
  153.   unsigned char *v;
  154.   unsigned int b;
  155.   int c;
  156. //if inbuf contains less bits...then read again....
  157.   if (ld->incnt<n)
  158.     fillbfr();
  159.   // get pointer to start data in inbuffer
  160.   //
  161.   v = ld->inbfr + ((96 - ld->incnt)>>3);
  162.   
  163.   // combine bytes to form int
  164.   b = (v[0]<<24) | (v[1]<<16) | (v[2]<<8) | v[3];
  165.   
  166.   c = ((ld->incnt-1) & 7) + 25;
  167.   
  168.   //send the righ adjusted value....
  169.   return (b>>(c-n)) & msk[n];
  170. }
  171. /* return next bit (could be made faster than getbits(1)) */
  172. unsigned int getbits1()
  173. {
  174.   return getbits(1);
  175. }
  176. /* advance by n bits */
  177. // Update the pointer....
  178. // === Remove data from the buffer...
  179. void flushbits(int n)
  180. {
  181.   ld->bitcnt+= n;
  182.   ld->incnt-= n;
  183.   if (ld->incnt < 0)
  184.     fillbfr();
  185. }
  186. /* return next n bits (right adjusted) */
  187. //
  188. // Get next n bits from the file header...
  189. //
  190. unsigned int getbits(int n)
  191. {
  192.   unsigned int l;
  193.   l = showbits(n);
  194.   flushbits(n);
  195.   return l;
  196. }