Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1671 | tullio | 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 <stdlib.h> |
||
36 | |||
37 | #include "drivers/glib.h" |
||
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 | /********************************************************************** |
||
64 | * |
||
65 | * This function is override the operating system call read (Shark can't handle file read and write in runtime). |
||
66 | * This function reads a buffer of 2048 bytes from a port (this buffer comes from readTask). |
||
67 | * |
||
68 | * Author: Robert Bäckgren |
||
69 | ************************************************************************/ |
||
70 | |||
71 | int read(int Infile, void *Rdbfr, int rdsize) |
||
72 | { |
||
73 | |||
74 | struct readDecodeMessage msg; |
||
75 | extern void *start_file; |
||
76 | if(initfaseFlag) |
||
77 | { |
||
78 | memcpy(Rdbfr, start_file, rdsize); |
||
79 | initfaseFlag = 0; |
||
80 | return rdsize; |
||
81 | } |
||
82 | else |
||
83 | { |
||
84 | // if(!port_receive(decoderTaskPort->readTaskPort, (void*)&msg, NON_BLOCK)) |
||
85 | if(!port_receive(decoderTaskPort->readTaskPort, (void*)&msg, BLOCK)) |
||
86 | { |
||
87 | |||
88 | grx_text("\n\n getbits ******************** E R R O R E R R O R E R R O R **********************\n",10,10,rgb16(255,255,255),0); |
||
89 | return -1; |
||
90 | } |
||
91 | memcpy(Rdbfr, msg.buffer, msg.size); |
||
92 | return msg.size; |
||
93 | } |
||
94 | } |
||
95 | |||
96 | |||
97 | void Fill_Buffer() |
||
98 | { |
||
99 | int Buffer_Level; |
||
100 | |||
101 | Buffer_Level = read(ld->Infile,ld->Rdbfr,2048); |
||
102 | ld->Rdptr = ld->Rdbfr; |
||
103 | |||
104 | if (System_Stream_Flag) |
||
105 | ld->Rdmax -= 2048; |
||
106 | |||
107 | |||
108 | /* end of the bitstream file */ |
||
109 | if (Buffer_Level < 2048) |
||
110 | { |
||
111 | /* just to be safe */ |
||
112 | if (Buffer_Level < 0) |
||
113 | Buffer_Level = 0; |
||
114 | |||
115 | /* pad until the next to the next 32-bit word boundary */ |
||
116 | while (Buffer_Level & 3) |
||
117 | ld->Rdbfr[Buffer_Level++] = 0; |
||
118 | |||
119 | /* pad the buffer with sequence end codes */ |
||
120 | while (Buffer_Level < 2048) |
||
121 | { |
||
122 | ld->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE>>24; |
||
123 | ld->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE>>16; |
||
124 | ld->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE>>8; |
||
125 | ld->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE&0xff; |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 | |||
130 | |||
131 | /* MPEG-1 system layer demultiplexer */ |
||
132 | |||
133 | int Get_Byte() |
||
134 | { |
||
135 | while(ld->Rdptr >= ld->Rdbfr+2048) |
||
136 | { |
||
137 | read(ld->Infile,ld->Rdbfr,2048); |
||
138 | ld->Rdptr -= 2048; |
||
139 | ld->Rdmax -= 2048; |
||
140 | } |
||
141 | return *ld->Rdptr++; |
||
142 | } |
||
143 | |||
144 | /* extract a 16-bit word from the bitstream buffer */ |
||
145 | int Get_Word() |
||
146 | { |
||
147 | int Val; |
||
148 | |||
149 | Val = Get_Byte(); |
||
150 | return (Val<<8) | Get_Byte(); |
||
151 | } |
||
152 | |||
153 | |||
154 | /* return next n bits (right adjusted) without advancing */ |
||
155 | |||
156 | unsigned int Show_Bits(N) |
||
157 | int N; |
||
158 | { |
||
159 | return ld->Bfr >> (32-N); |
||
160 | } |
||
161 | |||
162 | |||
163 | /* return next bit (could be made faster than Get_Bits(1)) */ |
||
164 | |||
165 | unsigned int Get_Bits1() |
||
166 | { |
||
167 | return Get_Bits(1); |
||
168 | } |
||
169 | |||
170 | |||
171 | /* advance by n bits */ |
||
172 | |||
173 | void Flush_Buffer(N) |
||
174 | int N; |
||
175 | { |
||
176 | int Incnt; |
||
177 | |||
178 | ld->Bfr <<= N; |
||
179 | |||
180 | Incnt = ld->Incnt -= N; |
||
181 | |||
182 | if (Incnt <= 24) |
||
183 | { |
||
184 | if (System_Stream_Flag && (ld->Rdptr >= ld->Rdmax-4)) |
||
185 | { |
||
186 | do |
||
187 | { |
||
188 | if (ld->Rdptr >= ld->Rdmax) |
||
189 | Next_Packet(); |
||
190 | ld->Bfr |= Get_Byte() << (24 - Incnt); |
||
191 | Incnt += 8; |
||
192 | } |
||
193 | while (Incnt <= 24); |
||
194 | } |
||
195 | else if (ld->Rdptr < ld->Rdbfr+2044) |
||
196 | { |
||
197 | do |
||
198 | { |
||
199 | ld->Bfr |= *ld->Rdptr++ << (24 - Incnt); |
||
200 | Incnt += 8; |
||
201 | } |
||
202 | while (Incnt <= 24); |
||
203 | } |
||
204 | else |
||
205 | { |
||
206 | do |
||
207 | { |
||
208 | if (ld->Rdptr >= ld->Rdbfr+2048) |
||
209 | Fill_Buffer(); |
||
210 | ld->Bfr |= *ld->Rdptr++ << (24 - Incnt); |
||
211 | Incnt += 8; |
||
212 | } |
||
213 | while (Incnt <= 24); |
||
214 | } |
||
215 | ld->Incnt = Incnt; |
||
216 | } |
||
217 | |||
218 | #ifdef VERIFY |
||
219 | ld->Bitcnt += N; |
||
220 | #endif /* VERIFY */ |
||
221 | |||
222 | } |
||
223 | |||
224 | |||
225 | /* return next n bits (right adjusted) */ |
||
226 | |||
227 | unsigned int Get_Bits(N) |
||
228 | int N; |
||
229 | { |
||
230 | unsigned int Val; |
||
231 | |||
232 | Val = Show_Bits(N); |
||
233 | Flush_Buffer(N); |
||
234 | |||
235 | return Val; |
||
236 | } |
||
237 |