]> Creatis software - gdcm.git/blob - src/gdcmmpeg2/src/mpeg2dec/systems.c
ENH: Trying to write the worse hack ever...
[gdcm.git] / src / gdcmmpeg2 / src / mpeg2dec / systems.c
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 "config.h"
31 #include "global.h"
32
33 /* initialize buffer, call once before first getbits or showbits */
34
35 /* parse system layer, ignore everything we don't need */
36 void Next_Packet()
37 {
38   unsigned int code;
39   int l;
40
41   for(;;)
42   {
43     code = Get_Long();
44
45     /* remove system layer byte stuffing */
46     while ((code & 0xffffff00) != 0x100)
47       code = (code<<8) | Get_Byte();
48
49     switch(code)
50     {
51     case PACK_START_CODE: /* pack header */
52       /* skip pack header (system_clock_reference and mux_rate) */
53       ld->Rdptr += 8;
54       break;
55     case VIDEO_ELEMENTARY_STREAM:   
56       code = Get_Word();             /* packet_length */
57       ld->Rdmax = ld->Rdptr + code;
58
59       code = Get_Byte();
60
61       if((code>>6)==0x02)
62       {
63         ld->Rdptr++;
64         code=Get_Byte();  /* parse PES_header_data_length */
65         ld->Rdptr+=code;    /* advance pointer by PES_header_data_length */
66         my_printf("MPEG-2 PES packet\n");
67         return;
68       }
69       else if(code==0xff)
70       {
71         /* parse MPEG-1 packet header */
72         while((code=Get_Byte())== 0xFF);
73       }
74        
75       /* stuffing bytes */
76       if(code>=0x40)
77       {
78         if(code>=0x80)
79         {
80           my_fprintf("Error in packet header\n");
81           my_exit(1);
82         }
83         /* skip STD_buffer_scale */
84         ld->Rdptr++;
85         code = Get_Byte();
86       }
87
88       if(code>=0x30)
89       {
90         if(code>=0x40)
91         {
92           my_fprintf("Error in packet header\n");
93           my_exit(1);
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         my_fprintf("Error in packet header\n");
106         my_exit(1);
107       }
108       return;
109     case ISO_END_CODE: /* end */
110       /* simulate a buffer full of sequence end codes */
111       l = 0;
112       while (l<2048)
113       {
114         ld->Rdbfr[l++] = SEQUENCE_END_CODE>>24;
115         ld->Rdbfr[l++] = SEQUENCE_END_CODE>>16;
116         ld->Rdbfr[l++] = SEQUENCE_END_CODE>>8;
117         ld->Rdbfr[l++] = SEQUENCE_END_CODE&0xff;
118       }
119       ld->Rdptr = ld->Rdbfr;
120       ld->Rdmax = ld->Rdbfr + 2048;
121       return;
122     default:
123       if(code>=SYSTEM_START_CODE)
124       {
125         /* skip system headers and non-video packets*/
126         code = Get_Word();
127         ld->Rdptr += code;
128       }
129       else
130       {
131         my_fprintf("Unexpected startcode %08x in system layer\n",code);
132         my_exit(1);
133       }
134       break;
135     }
136   }
137 }
138
139
140
141 void Flush_Buffer32()
142 {
143   int Incnt;
144
145   ld->Bfr = 0;
146
147   Incnt = ld->Incnt;
148   Incnt -= 32;
149
150   if (System_Stream_Flag && (ld->Rdptr >= ld->Rdmax-4))
151   {
152     while (Incnt <= 24)
153     {
154       if (ld->Rdptr >= ld->Rdmax)
155         Next_Packet();
156       ld->Bfr |= Get_Byte() << (24 - Incnt);
157       Incnt += 8;
158     }
159   }
160   else
161   {
162     while (Incnt <= 24)
163     {
164       if (ld->Rdptr >= ld->Rdbfr+2048)
165         Fill_Buffer();
166       ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
167       Incnt += 8;
168     }
169   }
170   ld->Incnt = Incnt;
171
172 #ifdef VERIFY 
173   ld->Bitcnt += 32;
174 #endif /* VERIFY */
175 }
176
177
178 unsigned int Get_Bits32()
179 {
180   unsigned int l;
181
182   l = Show_Bits(32);
183   Flush_Buffer32();
184
185   return l;
186 }
187
188
189 int Get_Long()
190 {
191   int i;
192
193   i = Get_Word();
194   return (i<<16) | Get_Word();
195 }
196
197