1 /* getbits.c, bit level routines */
4 * All modifications (mpeg2decode -> mpeg2play) are
5 * Copyright (C) 1996, Stefan Eckart. All Rights Reserved.
8 /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
11 * Disclaimer of Warranty
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.
21 * This disclaimer of warranty extends to the user of these programs and user's
22 * customers, employees, agents, transferees, successors, and assigns.
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
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
41 /* initialize buffer, call once before first getbits or showbits */
43 void Initialize_Buffer()
46 ld->Rdptr = ld->Rdbfr + 2048;
47 ld->Rdmax = ld->Rdptr;
50 /* only the verifier uses this particular bit counter
51 * Bitcnt keeps track of the current parser position with respect
52 * to the video elementary stream being decoded, regardless
53 * of whether or not it is wrapped within a systems layer stream
59 Flush_Buffer(0); /* fills valid data into bfr */
66 /*Buffer_Level = read(ld->Infile,ld->Rdbfr,2048);*/
67 Buffer_Level = ld->read_stream(ld->Infile,ld->Rdbfr,2048);
68 ld->Rdptr = ld->Rdbfr;
70 if (System_Stream_Flag)
74 /* end of the bitstream file */
75 if (Buffer_Level < 2048)
81 /* pad until the next to the next 32-bit word boundary */
82 while (Buffer_Level & 3)
83 ld->Rdbfr[Buffer_Level++] = 0;
85 /* pad the buffer with sequence end codes */
86 while (Buffer_Level < 2048)
88 ld->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE>>24;
89 ld->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE>>16;
90 ld->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE>>8;
91 ld->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE&0xff;
97 /* MPEG-1 system layer demultiplexer */
101 while(ld->Rdptr >= ld->Rdbfr+2048)
103 /*read(ld->Infile,ld->Rdbfr,2048);*/
104 ld->read_stream(ld->Infile,ld->Rdbfr,2048);
111 /* extract a 16-bit word from the bitstream buffer */
117 return (Val<<8) | Get_Byte();
121 /* return next n bits (right adjusted) without advancing */
123 unsigned int Show_Bits(N)
126 return ld->Bfr >> (32-N);
130 /* return next bit (could be made faster than Get_Bits(1)) */
132 unsigned int Get_Bits1()
138 /* advance by n bits */
147 Incnt = ld->Incnt -= N;
151 if (System_Stream_Flag && (ld->Rdptr >= ld->Rdmax-4))
155 if (ld->Rdptr >= ld->Rdmax)
157 ld->Bfr |= Get_Byte() << (24 - Incnt);
162 else if (ld->Rdptr < ld->Rdbfr+2044)
166 ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
175 if (ld->Rdptr >= ld->Rdbfr+2048)
177 ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
192 /* return next n bits (right adjusted) */
194 unsigned int Get_Bits(N)