OpenCores
URL https://opencores.org/ocsvn/mpeg2fpga/mpeg2fpga/trunk

Subversion Repositories mpeg2fpga

[/] [mpeg2fpga/] [trunk/] [tools/] [mpeg2dec/] [systems.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 kdv
/* 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 <stdio.h>
31
#include <stdlib.h>
32
 
33
#include "config.h"
34
#include "global.h"
35
 
36
/* initialize buffer, call once before first getbits or showbits */
37
 
38
/* parse system layer, ignore everything we don't need */
39
void Next_Packet()
40
{
41
  unsigned int code;
42
  int l;
43
 
44
  for(;;)
45
  {
46
    code = Get_Long();
47
 
48
    /* remove system layer byte stuffing */
49
    while ((code & 0xffffff00) != 0x100)
50
      code = (code<<8) | Get_Byte();
51
 
52
    switch(code)
53
    {
54
    case PACK_START_CODE: /* pack header */
55
      /* skip pack header (system_clock_reference and mux_rate) */
56
      ld->Rdptr += 8;
57
      break;
58
    case VIDEO_ELEMENTARY_STREAM:
59
      code = Get_Word();             /* packet_length */
60
      ld->Rdmax = ld->Rdptr + code;
61
 
62
      code = Get_Byte();
63
 
64
      if((code>>6)==0x02)
65
      {
66
        ld->Rdptr++;
67
        code=Get_Byte();  /* parse PES_header_data_length */
68
        ld->Rdptr+=code;    /* advance pointer by PES_header_data_length */
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
          fprintf(stderr,"Error in packet header\n");
83
          exit(1);
84
        }
85
        /* skip STD_buffer_scale */
86
        ld->Rdptr++;
87
        code = Get_Byte();
88
      }
89
 
90
      if(code>=0x30)
91
      {
92
        if(code>=0x40)
93
        {
94
          fprintf(stderr,"Error in packet header\n");
95
          exit(1);
96
        }
97
        /* skip presentation and decoding time stamps */
98
        ld->Rdptr += 9;
99
      }
100
      else if(code>=0x20)
101
      {
102
        /* skip presentation time stamps */
103
        ld->Rdptr += 4;
104
      }
105
      else if(code!=0x0f)
106
      {
107
        fprintf(stderr,"Error in packet header\n");
108
        exit(1);
109
      }
110
      return;
111
    case ISO_END_CODE: /* end */
112
      /* simulate a buffer full of sequence end codes */
113
      l = 0;
114
      while (l<2048)
115
      {
116
        ld->Rdbfr[l++] = SEQUENCE_END_CODE>>24;
117
        ld->Rdbfr[l++] = SEQUENCE_END_CODE>>16;
118
        ld->Rdbfr[l++] = SEQUENCE_END_CODE>>8;
119
        ld->Rdbfr[l++] = SEQUENCE_END_CODE&0xff;
120
      }
121
      ld->Rdptr = ld->Rdbfr;
122
      ld->Rdmax = ld->Rdbfr + 2048;
123
      return;
124
    default:
125
      if(code>=SYSTEM_START_CODE)
126
      {
127
        /* skip system headers and non-video packets*/
128
        code = Get_Word();
129
        ld->Rdptr += code;
130
      }
131
      else
132
      {
133
        fprintf(stderr,"Unexpected startcode %08x in system layer\n",code);
134
        exit(1);
135
      }
136
      break;
137
    }
138
  }
139
}
140
 
141
 
142
 
143
void Flush_Buffer32()
144
{
145
  int Incnt;
146
 
147
  ld->Bfr = 0;
148
 
149
  Incnt = ld->Incnt;
150
  Incnt -= 32;
151
 
152
  if (System_Stream_Flag && (ld->Rdptr >= ld->Rdmax-4))
153
  {
154
    while (Incnt <= 24)
155
    {
156
      if (ld->Rdptr >= ld->Rdmax)
157
        Next_Packet();
158
      ld->Bfr |= Get_Byte() << (24 - Incnt);
159
      Incnt += 8;
160
    }
161
  }
162
  else
163
  {
164
    while (Incnt <= 24)
165
    {
166
      if (ld->Rdptr >= ld->Rdbfr+2048)
167
        Fill_Buffer();
168
      ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
169
      Incnt += 8;
170
    }
171
  }
172
  ld->Incnt = Incnt;
173
 
174
#ifdef VERIFY 
175
  ld->Bitcnt += 32;
176
#endif /* VERIFY */
177
}
178
 
179
 
180
unsigned int Get_Bits32()
181
{
182
  unsigned int l;
183
 
184
  l = Show_Bits(32);
185
  Flush_Buffer32();
186
 
187
  return l;
188
}
189
 
190
 
191
int Get_Long()
192
{
193
  int i;
194
 
195
  i = Get_Word();
196
  return (i<<16) | Get_Word();
197
}
198
 
199
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.