Subversion Repositories shark

Rev

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

Rev Author Line No. Line
1562 trimarchi 1
/* systems.c, systems-specific routines                                 */
2
 
3
/* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
4
 
5
/*
6
 * Disclaimer of Warranty
7
 *
8
 * These software programs are available to the user without any license fee or
9
 * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims
10
 * any and all warranties, whether express, implied, or statuary, including any
11
 * implied warranties or merchantability or of fitness for a particular
12
 * purpose.  In no event shall the copyright-holder be liable for any
13
 * incidental, punitive, or consequential damages of any kind whatsoever
14
 * arising from the use of these programs.
15
 *
16
 * This disclaimer of warranty extends to the user of these programs and user's
17
 * customers, employees, agents, transferees, successors, and assigns.
18
 *
19
 * The MPEG Software Simulation Group does not represent or warrant that the
20
 * programs furnished hereunder are free of infringement of any third-party
21
 * patents.
22
 *
23
 * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
24
 * are subject to royalty fees to patent holders.  Many of these patents are
25
 * general enough such that they are unavoidable regardless of implementation
26
 * design.
27
 *
28
 */
29
 
30
#include <stdlib.h>
31
 
32
#include "config.h"
33
#include "global.h"
34
 
35
/* initialize buffer, call once before first getbits or showbits */
36
 
37
/* parse system layer, ignore everything we don't need */
38
void Next_Packet()
39
{
40
  unsigned int code;
41
  int l;
42
 
43
  for(;;)
44
  {
45
    code = Get_Long();
46
 
47
    /* remove system layer byte stuffing */
48
    while ((code & 0xffffff00) != 0x100)
49
      code = (code<<8) | Get_Byte();
50
 
51
    switch(code)
52
    {
53
    case PACK_START_CODE: /* pack header */
54
      /* skip pack header (system_clock_reference and mux_rate) */
55
      ld->Rdptr += 8;
56
      break;
57
    case VIDEO_ELEMENTARY_STREAM:  
58
      code = Get_Word();             /* packet_length */
59
      ld->Rdmax = ld->Rdptr + code;
60
 
61
      code = Get_Byte();
62
 
63
      if((code>>6)==0x02)
64
      {
65
        ld->Rdptr++;
66
        code=Get_Byte();  /* parse PES_header_data_length */
67
        ld->Rdptr+=code;    /* advance pointer by PES_header_data_length */
68
        cprintf("MPEG-2 PES packet\n");
69
        return;
70
      }
71
      else if(code==0xff)
72
      {
73
        /* parse MPEG-1 packet header */
74
        while((code=Get_Byte())== 0xFF);
75
      }
76
 
77
      /* stuffing bytes */
78
      if(code>=0x40)
79
      {
80
        if(code>=0x80)
81
        {
82
          cprintf("Error in packet header\n");
83
        }
84
        /* skip STD_buffer_scale */
85
        ld->Rdptr++;
86
        code = Get_Byte();
87
      }
88
 
89
      if(code>=0x30)
90
      {
91
        if(code>=0x40)
92
        {
93
          cprintf("Error in packet header\n");
94
        }
95
        /* skip presentation and decoding time stamps */
96
        ld->Rdptr += 9;
97
      }
98
      else if(code>=0x20)
99
      {
100
        /* skip presentation time stamps */
101
        ld->Rdptr += 4;
102
      }
103
      else if(code!=0x0f)
104
      {
105
        cprintf("Error in packet header\n");
106
      }
107
      return;
108
    case ISO_END_CODE: /* end */
109
      /* simulate a buffer full of sequence end codes */
110
      l = 0;
111
      while (l<2048)
112
      {
113
        ld->Rdbfr[l++] = SEQUENCE_END_CODE>>24;
114
        ld->Rdbfr[l++] = SEQUENCE_END_CODE>>16;
115
        ld->Rdbfr[l++] = SEQUENCE_END_CODE>>8;
116
        ld->Rdbfr[l++] = SEQUENCE_END_CODE&0xff;
117
      }
118
      ld->Rdptr = ld->Rdbfr;
119
      ld->Rdmax = ld->Rdbfr + 2048;
120
      return;
121
    default:
122
      if(code>=SYSTEM_START_CODE)
123
      {
124
        /* skip system headers and non-video packets*/
125
        code = Get_Word();
126
        ld->Rdptr += code;
127
      }
128
      else
129
      {
1563 trimarchi 130
        //cprintf("Unexpected startcode %08x in system layer\n",code);
1562 trimarchi 131
      }
132
      break;
133
    }
134
  }
135
}
136
 
137
 
138
 
139
void Flush_Buffer32()
140
{
141
  int Incnt;
142
 
143
  ld->Bfr = 0;
144
 
145
  Incnt = ld->Incnt;
146
  Incnt -= 32;
147
 
148
  if (System_Stream_Flag && (ld->Rdptr >= ld->Rdmax-4))
149
  {
150
    while (Incnt <= 24)
151
    {
152
      if (ld->Rdptr >= ld->Rdmax)
153
        Next_Packet();
154
      ld->Bfr |= Get_Byte() << (24 - Incnt);
155
      Incnt += 8;
156
    }
157
  }
158
  else
159
  {
160
    while (Incnt <= 24)
161
    {
162
      if (ld->Rdptr >= ld->Rdbfr+2048)
163
        Fill_Buffer();
164
      ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
165
      Incnt += 8;
166
    }
167
  }
168
  ld->Incnt = Incnt;
169
 
170
#ifdef VERIFY 
171
  ld->Bitcnt += 32;
172
#endif /* VERIFY */
173
}
174
 
175
 
176
unsigned int Get_Bits32()
177
{
178
  unsigned int l;
179
 
180
  l = Show_Bits(32);
181
  Flush_Buffer32();
182
 
183
  return l;
184
}
185
 
186
 
187
int Get_Long()
188
{
189
  int i;
190
 
191
  i = Get_Word();
192
  return (i<<16) | Get_Word();
193
}
194
 
195