Subversion Repositories shark

Rev

Rev 1624 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1624 giacomo 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
int read(int Infile, void *Rdbfr, int rdsize) {
64
 
65
  if (ld->actual_file_ptr + 2048 > ld->end_file_ptr)
66
    rdsize = (int)(ld->end_file_ptr - ld->actual_file_ptr);
67
 
68
  if (rdsize < 0) return 0;
69
 
70
  memcpy(Rdbfr,ld->actual_file_ptr,rdsize);
71
  ld->actual_file_ptr += rdsize;
72
 
73
  return rdsize;
74
 
75
}
76
 
77
void Fill_Buffer()
78
{
79
  int Buffer_Level;
80
 
81
  Buffer_Level = read(ld->Infile,ld->Rdbfr,2048);
82
  ld->Rdptr = ld->Rdbfr;
83
 
84
  if (System_Stream_Flag)
85
    ld->Rdmax -= 2048;
86
 
87
 
88
  /* end of the bitstream file */
89
  if (Buffer_Level < 2048)
90
  {
91
    /* just to be safe */
92
    if (Buffer_Level < 0)
93
      Buffer_Level = 0;
94
 
95
    /* pad until the next to the next 32-bit word boundary */
96
    while (Buffer_Level & 3)
97
      ld->Rdbfr[Buffer_Level++] = 0;
98
 
99
        /* pad the buffer with sequence end codes */
100
    while (Buffer_Level < 2048)
101
    {
102
      ld->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE>>24;
103
      ld->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE>>16;
104
      ld->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE>>8;
105
      ld->Rdbfr[Buffer_Level++] = SEQUENCE_END_CODE&0xff;
106
    }
107
  }
108
}
109
 
110
 
111
/* MPEG-1 system layer demultiplexer */
112
 
113
int Get_Byte()
114
{
115
  while(ld->Rdptr >= ld->Rdbfr+2048)
116
  {
117
    read(ld->Infile,ld->Rdbfr,2048);
118
    ld->Rdptr -= 2048;
119
    ld->Rdmax -= 2048;
120
  }
121
  return *ld->Rdptr++;
122
}
123
 
124
/* extract a 16-bit word from the bitstream buffer */
125
int Get_Word()
126
{
127
  int Val;
128
 
129
  Val = Get_Byte();
130
  return (Val<<8) | Get_Byte();
131
}
132
 
133
 
134
/* return next n bits (right adjusted) without advancing */
135
 
136
unsigned int Show_Bits(N)
137
int N;
138
{
139
  return ld->Bfr >> (32-N);
140
}
141
 
142
 
143
/* return next bit (could be made faster than Get_Bits(1)) */
144
 
145
unsigned int Get_Bits1()
146
{
147
  return Get_Bits(1);
148
}
149
 
150
 
151
/* advance by n bits */
152
 
153
void Flush_Buffer(N)
154
int N;
155
{
156
  int Incnt;
157
 
158
  ld->Bfr <<= N;
159
 
160
  Incnt = ld->Incnt -= N;
161
 
162
  if (Incnt <= 24)
163
  {
164
    if (System_Stream_Flag && (ld->Rdptr >= ld->Rdmax-4))
165
    {
166
      do
167
      {
168
        if (ld->Rdptr >= ld->Rdmax)
169
          Next_Packet();
170
        ld->Bfr |= Get_Byte() << (24 - Incnt);
171
        Incnt += 8;
172
      }
173
      while (Incnt <= 24);
174
    }
175
    else if (ld->Rdptr < ld->Rdbfr+2044)
176
    {
177
      do
178
      {
179
        ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
180
        Incnt += 8;
181
      }
182
      while (Incnt <= 24);
183
    }
184
    else
185
    {
186
      do
187
      {
188
        if (ld->Rdptr >= ld->Rdbfr+2048)
189
          Fill_Buffer();
190
        ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
191
        Incnt += 8;
192
      }
193
      while (Incnt <= 24);
194
    }
195
    ld->Incnt = Incnt;
196
  }
197
 
198
#ifdef VERIFY 
199
  ld->Bitcnt += N;
200
#endif /* VERIFY */
201
 
202
}
203
 
204
 
205
/* return next n bits (right adjusted) */
206
 
207
unsigned int Get_Bits(N)
208
int N;
209
{
210
  unsigned int Val;
211
 
212
  Val = Show_Bits(N);
213
  Flush_Buffer(N);
214
 
215
  return Val;
216
}
217