Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

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