]> Creatis software - gdcm.git/blob - src/gdcmmpeg2/src/mpeg2dec/getbits.c
COMP: Remove compiler warnings using gcc
[gdcm.git] / src / gdcmmpeg2 / src / mpeg2dec / getbits.c
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 #include <unistd.h> // for read
38
39 #include "config.h"
40 #include "global.h"
41
42 /* initialize buffer, call once before first getbits or showbits */
43
44 void Initialize_Buffer()
45 {
46   ld->Incnt = 0;
47   ld->Rdptr = ld->Rdbfr + 2048;
48   ld->Rdmax = ld->Rdptr;
49
50 #ifdef VERIFY
51   /*  only the verifier uses this particular bit counter 
52    *  Bitcnt keeps track of the current parser position with respect
53    *  to the video elementary stream being decoded, regardless 
54    *  of whether or not it is wrapped within a systems layer stream 
55    */
56   ld->Bitcnt = 0;
57 #endif
58
59   ld->Bfr = 0;
60   Flush_Buffer(0); /* fills valid data into bfr */
61 }
62
63 void Fill_Buffer()
64 {
65   int Buffer_Level;
66
67   Buffer_Level = read(ld->Infile,ld->Rdbfr,2048);
68   ld->Rdptr = ld->Rdbfr;
69
70   if (System_Stream_Flag)
71     ld->Rdmax -= 2048;
72
73   
74   /* end of the bitstream file */
75   if (Buffer_Level < 2048)
76   {
77     /* just to be safe */
78     if (Buffer_Level < 0)
79       Buffer_Level = 0;
80
81     /* pad until the next to the next 32-bit word boundary */
82     while (Buffer_Level & 3)
83       ld->Rdbfr[Buffer_Level++] = 0;
84
85     /* pad the buffer with sequence end codes */
86     while (Buffer_Level < 2048)
87     {
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;
92     }
93   }
94 }
95
96
97 /* MPEG-1 system layer demultiplexer */
98
99 int Get_Byte()
100 {
101   while(ld->Rdptr >= ld->Rdbfr+2048)
102   {
103     read(ld->Infile,ld->Rdbfr,2048);
104     ld->Rdptr -= 2048;
105     ld->Rdmax -= 2048;
106   }
107   return *ld->Rdptr++;
108 }
109
110 /* extract a 16-bit word from the bitstream buffer */
111 int Get_Word()
112 {
113   int Val;
114
115   Val = Get_Byte();
116   return (Val<<8) | Get_Byte();
117 }
118
119
120 /* return next n bits (right adjusted) without advancing */
121
122 unsigned int Show_Bits(N)
123 int N;
124 {
125   return ld->Bfr >> (32-N);
126 }
127
128
129 /* return next bit (could be made faster than Get_Bits(1)) */
130
131 unsigned int Get_Bits1()
132 {
133   return Get_Bits(1);
134 }
135
136
137 /* advance by n bits */
138
139 void Flush_Buffer(N)
140 int N;
141 {
142   int Incnt;
143
144   ld->Bfr <<= N;
145
146   Incnt = ld->Incnt -= N;
147
148   if (Incnt <= 24)
149   {
150     if (System_Stream_Flag && (ld->Rdptr >= ld->Rdmax-4))
151     {
152       do
153       {
154         if (ld->Rdptr >= ld->Rdmax)
155           Next_Packet();
156         ld->Bfr |= Get_Byte() << (24 - Incnt);
157         Incnt += 8;
158       }
159       while (Incnt <= 24);
160     }
161     else if (ld->Rdptr < ld->Rdbfr+2044)
162     {
163       do
164       {
165         ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
166         Incnt += 8;
167       }
168       while (Incnt <= 24);
169     }
170     else
171     {
172       do
173       {
174         if (ld->Rdptr >= ld->Rdbfr+2048)
175           Fill_Buffer();
176         ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
177         Incnt += 8;
178       }
179       while (Incnt <= 24);
180     }
181     ld->Incnt = Incnt;
182   }
183
184 #ifdef VERIFY 
185   ld->Bitcnt += N;
186 #endif /* VERIFY */
187
188 }
189
190
191 /* return next n bits (right adjusted) */
192
193 unsigned int Get_Bits(N)
194 int N;
195 {
196   unsigned int Val;
197
198   Val = Show_Bits(N);
199   Flush_Buffer(N);
200
201   return Val;
202 }
203