getbits.c
上传用户:hkgotone
上传日期:2013-02-17
资源大小:293k
文件大小:4k
源码类别:

Windows Mobile

开发平台:

C/C++

  1. /* getbits.c, bit level routines                                            */
  2. /*
  3.  * All modifications (mpeg2decode -> mpeg2play) are
  4.  * Copyright (C) 1996, Stefan Eckart. All Rights Reserved.
  5.  */
  6. /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
  7. /*
  8.  * Disclaimer of Warranty
  9.  *
  10.  * These software programs are available to the user without any license fee or
  11.  * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims
  12.  * any and all warranties, whether express, implied, or statuary, including any
  13.  * implied warranties or merchantability or of fitness for a particular
  14.  * purpose.  In no event shall the copyright-holder be liable for any
  15.  * incidental, punitive, or consequential damages of any kind whatsoever
  16.  * arising from the use of these programs.
  17.  *
  18.  * This disclaimer of warranty extends to the user of these programs and user's
  19.  * customers, employees, agents, transferees, successors, and assigns.
  20.  *
  21.  * The MPEG Software Simulation Group does not represent or warrant that the
  22.  * programs furnished hereunder are free of infringement of any third-party
  23.  * patents.
  24.  *
  25.  * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
  26.  * are subject to royalty fees to patent holders.  Many of these patents are
  27.  * general enough such that they are unavoidable regardless of implementation
  28.  * design.
  29.  *
  30.  */
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include "config.h"
  34. #include "global.h"
  35. /* initialize buffer, call once before first getbits or showbits */
  36. void Initialize_Buffer()
  37. {
  38.   ld->Incnt = 0;
  39.   ld->Rdptr = ld->Rdbfr + 2048;
  40.   ld->Rdmax = ld->Rdptr;
  41. #ifdef VERIFY
  42.   /*  only the verifier uses this particular bit counter 
  43.    *  Bitcnt keeps track of the current parser position with respect
  44.    *  to the video elementary stream being decoded, regardless 
  45.    *  of whether or not it is wrapped within a systems layer stream 
  46.    */
  47.   ld->Bitcnt = 0;
  48. #endif
  49.   ld->Bfr = 0;
  50.   Flush_Buffer(0); /* fills valid data into bfr */
  51. }
  52. void Fill_Buffer()
  53. {
  54.   int Buffer_Level;
  55.   Buffer_Level = read(ld->Infile,ld->Rdbfr,2048);
  56.   ld->Rdptr = ld->Rdbfr;
  57.   if (System_Stream_Flag)
  58.     ld->Rdmax -= 2048;
  59.   
  60.   /* end of the bitstream file */
  61.   if (Buffer_Level < 2048)
  62.   {
  63.     /* just to be safe */
  64.     if (Buffer_Level < 0)
  65.       Buffer_Level = 0;
  66.     /* pad until the next to the next 32-bit word boundary */
  67.     while (Buffer_Level & 3)
  68.       ld->Rdbfr[Buffer_Level++] = 0;
  69. /* pad the buffer with sequence end codes */
  70.     while (Buffer_Level < 2048)
  71.     {
  72.       ld->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE>>24;
  73.       ld->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE>>16;
  74.       ld->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE>>8;
  75.       ld->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE&0xff;
  76.     }
  77.   }
  78. }
  79. /* MPEG-1 system layer demultiplexer */
  80. int Get_Byte()
  81. {
  82.   while(ld->Rdptr >= ld->Rdbfr+2048)
  83.   {
  84.     read(ld->Infile,ld->Rdbfr,2048);
  85.     ld->Rdptr -= 2048;
  86.     ld->Rdmax -= 2048;
  87.   }
  88.   return *ld->Rdptr++;
  89. }
  90. /* extract a 16-bit word from the bitstream buffer */
  91. int Get_Word()
  92. {
  93.   int Val;
  94.   Val = Get_Byte();
  95.   return (Val<<8) | Get_Byte();
  96. }
  97. /* return next n bits (right adjusted) without advancing */
  98. unsigned int Show_Bits(N)
  99. int N;
  100. {
  101.   return ld->Bfr >> (32-N);
  102. }
  103. /* return next bit (could be made faster than Get_Bits(1)) */
  104. unsigned int Get_Bits1()
  105. {
  106.   return Get_Bits(1);
  107. }
  108. /* advance by n bits */
  109. void Flush_Buffer(N)
  110. int N;
  111. {
  112.   int Incnt;
  113.   ld->Bfr <<= N;
  114.   Incnt = ld->Incnt -= N;
  115.   if (Incnt <= 24)
  116.   {
  117.     if (System_Stream_Flag && (ld->Rdptr >= ld->Rdmax-4))
  118.     {
  119.       do
  120.       {
  121.         if (ld->Rdptr >= ld->Rdmax)
  122.           Next_Packet();
  123.         ld->Bfr |= Get_Byte() << (24 - Incnt);
  124.         Incnt += 8;
  125.       }
  126.       while (Incnt <= 24);
  127.     }
  128.     else if (ld->Rdptr < ld->Rdbfr+2044)
  129.     {
  130.       do
  131.       {
  132.         ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
  133.         Incnt += 8;
  134.       }
  135.       while (Incnt <= 24);
  136.     }
  137.     else
  138.     {
  139.       do
  140.       {
  141.         if (ld->Rdptr >= ld->Rdbfr+2048)
  142.           Fill_Buffer();
  143.         ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
  144.         Incnt += 8;
  145.       }
  146.       while (Incnt <= 24);
  147.     }
  148.     ld->Incnt = Incnt;
  149.   }
  150. #ifdef VERIFY 
  151.   ld->Bitcnt += N;
  152. #endif /* VERIFY */
  153. }
  154. /* return next n bits (right adjusted) */
  155. unsigned int Get_Bits(N)
  156. int N;
  157. {
  158.   unsigned int Val;
  159.   Val = Show_Bits(N);
  160.   Flush_Buffer(N);
  161.   return Val;
  162. }