]> Creatis software - gdcm.git/blob - src/gdcmmpeg2/src/mpeg2dec/systems.c
Replace C++ style comments by C style comments, to avoid gcc warnings
[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 <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         printf("MPEG-2 PES packet\n");
70         return;
71       }
72       else if(code==0xff)
73       {
74         /* parse MPEG-1 packet header */
75         while((code=Get_Byte())== 0xFF);
76       }
77        
78       /* stuffing bytes */
79       if(code>=0x40)
80       {
81         if(code>=0x80)
82         {
83           fprintf(stderr,"Error in packet header\n");
84           exit(1);
85         }
86         /* skip STD_buffer_scale */
87         ld->Rdptr++;
88         code = Get_Byte();
89       }
90
91       if(code>=0x30)
92       {
93         if(code>=0x40)
94         {
95           fprintf(stderr,"Error in packet header\n");
96           exit(1);
97         }
98         /* skip presentation and decoding time stamps */
99         ld->Rdptr += 9;
100       }
101       else if(code>=0x20)
102       {
103         /* skip presentation time stamps */
104         ld->Rdptr += 4;
105       }
106       else if(code!=0x0f)
107       {
108         fprintf(stderr,"Error in packet header\n");
109         exit(1);
110       }
111       return;
112     case ISO_END_CODE: /* end */
113       /* simulate a buffer full of sequence end codes */
114       l = 0;
115       while (l<2048)
116       {
117         ld->Rdbfr[l++] = SEQUENCE_END_CODE>>24;
118         ld->Rdbfr[l++] = SEQUENCE_END_CODE>>16;
119         ld->Rdbfr[l++] = SEQUENCE_END_CODE>>8;
120         ld->Rdbfr[l++] = SEQUENCE_END_CODE&0xff;
121       }
122       ld->Rdptr = ld->Rdbfr;
123       ld->Rdmax = ld->Rdbfr + 2048;
124       return;
125     default:
126       if(code>=SYSTEM_START_CODE)
127       {
128         /* skip system headers and non-video packets*/
129         code = Get_Word();
130         ld->Rdptr += code;
131       }
132       else
133       {
134         fprintf(stderr,"Unexpected startcode %08x in system layer\n",code);
135         exit(1);
136       }
137       break;
138     }
139   }
140 }
141
142
143
144 void Flush_Buffer32()
145 {
146   int Incnt;
147
148   ld->Bfr = 0;
149
150   Incnt = ld->Incnt;
151   Incnt -= 32;
152
153   if (System_Stream_Flag && (ld->Rdptr >= ld->Rdmax-4))
154   {
155     while (Incnt <= 24)
156     {
157       if (ld->Rdptr >= ld->Rdmax)
158         Next_Packet();
159       ld->Bfr |= Get_Byte() << (24 - Incnt);
160       Incnt += 8;
161     }
162   }
163   else
164   {
165     while (Incnt <= 24)
166     {
167       if (ld->Rdptr >= ld->Rdbfr+2048)
168         Fill_Buffer();
169       ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
170       Incnt += 8;
171     }
172   }
173   ld->Incnt = Incnt;
174
175 #ifdef VERIFY 
176   ld->Bitcnt += 32;
177 #endif /* VERIFY */
178 }
179
180
181 unsigned int Get_Bits32()
182 {
183   unsigned int l;
184
185   l = Show_Bits(32);
186   Flush_Buffer32();
187
188   return l;
189 }
190
191
192 int Get_Long()
193 {
194   int i;
195
196   i = Get_Word();
197   return (i<<16) | Get_Word();
198 }
199
200