1 /*
2  * Copyright (c) 1995 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Permission to use, copy, modify, and distribute this software and its
6  * documentation for any purpose, without fee, and without written agreement is
7  * hereby granted, provided that the above copyright notice and the following
8  * two paragraphs appear in all copies of this software.
9  *
10  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
11  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
12  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
13  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  *
15  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
16  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
18  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
19  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
20  */
21 
22 /*
23  * Portions of this software Copyright (c) 1995 Brown University.
24  * All rights reserved.
25  *
26  * Permission to use, copy, modify, and distribute this software and its
27  * documentation for any purpose, without fee, and without written agreement
28  * is hereby granted, provided that the above copyright notice and the
29  * following two paragraphs appear in all copies of this software.
30  *
31  * IN NO EVENT SHALL BROWN UNIVERSITY BE LIABLE TO ANY PARTY FOR
32  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
33  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF BROWN
34  * UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  *
36  * BROWN UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
37  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
38  * PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
39  * BASIS, AND BROWN UNIVERSITY HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
40  * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
41  */
42 
43 #ifndef MPEG_LIB_VIDEO_HEADER
44 #define MPEG_LIB_VIDEO_HEADER
45 #include <stdio.h>
46 
47 /*
48    Changes to make the code reentrant:
49      deglobalized: ditherFlags, totNumFrames, realTimeStart, matched_depth,
50        filename, ditherType, curBits, ReconPMBlock statics, stream id
51        variables, Parse_done, swap, seekValue, input, EOF_flag, ReadPacket
52        statics, sys_layer, curVidStream, curVidStream, jmb_buf env (removed)
53      X globals now in xinfo: ditherType, visual, depth (also name), hints,
54        owncmFlag, et al
55      now const: scan
56      film_has_ended instead of FilmState
57    Additional changes:
58      if DISABLE_DITHER defined, don't compile dithering code
59    -lsh@cs.brown.edu (Loring Holden)
60  */
61 
62 #include "MPEGvideo.h"
63 #include "MPEGaudio.h"
64 
65 typedef Sint32 INT32;
66 typedef Sint16 INT16;
67 typedef Sint8 INT8;
68 typedef Uint32 UINT32;
69 typedef Uint16 UINT16;
70 typedef Uint8 UINT8;
71 
72 /* Define Parsing error codes. */
73 
74 #define SKIP_PICTURE (-10)
75 #define SKIP_TO_START_CODE (-1)
76 #define PARSE_OK 1
77 
78 /* Define BOOLEAN, TRUE, and FALSE. */
79 
80 #define BOOLEAN int
81 #define TRUE 1
82 #define FALSE 0
83 
84 /* Set ring buffer size. */
85 
86 #define RING_BUF_SIZE 5
87 
88 /* Macros for picture code type. */
89 
90 #define I_TYPE 1
91 #define P_TYPE 2
92 #define B_TYPE 3
93 #define D_TYPE 4
94 
95 /* Start codes. */
96 
97 #define SEQ_END_CODE 0x000001b7
98 #define SEQ_START_CODE 0x000001b3
99 #define GOP_START_CODE 0x000001b8
100 #define PICTURE_START_CODE 0x00000100
101 #define SLICE_MIN_START_CODE 0x00000101
102 #define SLICE_MAX_START_CODE 0x000001af
103 #define EXT_START_CODE 0x000001b5
104 #define USER_START_CODE 0x000001b2
105 #define SEQUENCE_ERROR_CODE 0x000001b4
106 
107 /* Number of macroblocks to process in one call to mpegVidRsrc. */
108 
109 #define MB_QUANTUM 100
110 
111 /* Macros used with macroblock address decoding. */
112 
113 #define MB_STUFFING 34
114 #define MB_ESCAPE 35
115 
116 /* Lock flags for pict images. */
117 
118 #define DISPLAY_LOCK 0x01
119 #define PAST_LOCK 0x02
120 #define FUTURE_LOCK 0x04
121 
122 #define HYBRID_DITHER 0
123 #define HYBRID2_DITHER 1
124 #define FS4_DITHER 2
125 #define FS2_DITHER 3
126 #define FS2FAST_DITHER 4
127 #define Twox2_DITHER 5
128 #define GRAY_DITHER 6
129 #define FULL_COLOR_DITHER 7
130 #define NO_DITHER 8
131 #define ORDERED_DITHER 9
132 #define MONO_DITHER 10
133 #define MONO_THRESHOLD 11
134 #define ORDERED2_DITHER 12
135 #define MBORDERED_DITHER 13
136 #define GRAY256_DITHER 14
137 #define PPM_DITHER     15
138 #define FULL_COLOR2_DITHER 16
139 #define GRAY2_DITHER 17
140 #define GRAY2562_DITHER 18
141 
142 #ifdef DISABLE_DITHER
143 #define IS_2x2_DITHER(a) (0)
144 #else
145 #define IS_2x2_DITHER(a) ((a) == Twox2_DITHER || (a) == FULL_COLOR2_DITHER || (a) == GRAY2_DITHER || (a) == (GRAY2562_DITHER))
146 #endif
147 
148 /* Brown - changed to const int because it is a help variable */
149 extern const int scan[][8];
150 
151 /* Structure with reconstructed pixel values. */
152 
153 typedef struct pict_image {
154   unsigned char *image;                  /* YV12 format image  */
155   unsigned char *luminance;              /* Luminance plane.   */
156   unsigned char *Cr;                     /* Cr plane.          */
157   unsigned char *Cb;                     /* Cb plane.          */
158   unsigned short int *mb_qscale;         /* macroblock info    */
159   int locked;                            /* Lock flag.         */
160   TimeStamp show_time;                   /* Presentation time. */
161 } PictImage;
162 
163 /* Group of pictures structure. */
164 
165 typedef struct GoP {
166   BOOLEAN drop_flag;                     /* Flag indicating dropped frame. */
167   unsigned int tc_hours;                 /* Hour component of time code.   */
168   unsigned int tc_minutes;               /* Minute component of time code. */
169   unsigned int tc_seconds;               /* Second component of time code. */
170   unsigned int tc_pictures;              /* Picture counter of time code.  */
171   BOOLEAN closed_gop;                    /* Indicates no pred. vectors to
172 					    previous group of pictures.    */
173   BOOLEAN broken_link;                   /* B frame unable to be decoded.  */
174   char *ext_data;                        /* Extension data.                */
175   char *user_data;                       /* User data.                     */
176 } GoP;
177 
178 /* Picture structure. */
179 
180 typedef struct pict {
181   unsigned int temp_ref;                 /* Temporal reference.             */
182   unsigned int code_type;                /* Frame type: P, B, I             */
183   unsigned int vbv_delay;                /* Buffer delay.                   */
184   BOOLEAN full_pel_forw_vector;          /* Forw. vectors specified in full
185 					    pixel values flag.              */
186   unsigned int forw_r_size;              /* Used for vector decoding.       */
187   unsigned int forw_f;                   /* Used for vector decoding.       */
188   BOOLEAN full_pel_back_vector;          /* Back vectors specified in full
189 					    pixel values flag.              */
190   unsigned int back_r_size;              /* Used in decoding.               */
191   unsigned int back_f;                   /* Used in decoding.               */
192   char *extra_info;                      /* Extra bit picture info.         */
193   char *ext_data;                        /* Extension data.                 */
194   char *user_data;                       /* User data.                      */
195 } Pict;
196 
197 /* Slice structure. */
198 
199 typedef struct slice {
200   unsigned int vert_pos;                 /* Vertical position of slice. */
201   unsigned int quant_scale;              /* Quantization scale.         */
202   char *extra_info;                      /* Extra bit slice info.       */
203 } Slice;
204 
205 /* Macroblock structure. */
206 
207 typedef struct macroblock {
208   int mb_address;                        /* Macroblock address.              */
209   int past_mb_addr;                      /* Previous mblock address.         */
210   int motion_h_forw_code;                /* Forw. horiz. motion vector code. */
211   unsigned int motion_h_forw_r;          /* Used in decoding vectors.        */
212   int motion_v_forw_code;                /* Forw. vert. motion vector code.  */
213   unsigned int motion_v_forw_r;          /* Used in decdoinge vectors.       */
214   int motion_h_back_code;                /* Back horiz. motion vector code.  */
215   unsigned int motion_h_back_r;          /* Used in decoding vectors.        */
216   int motion_v_back_code;                /* Back vert. motion vector code.   */
217   unsigned int motion_v_back_r;          /* Used in decoding vectors.        */
218   unsigned int cbp;                      /* Coded block pattern.             */
219   BOOLEAN mb_intra;                      /* Intracoded mblock flag.          */
220   BOOLEAN bpict_past_forw;               /* Past B frame forw. vector flag.  */
221   BOOLEAN bpict_past_back;               /* Past B frame back vector flag.   */
222   int past_intra_addr;                   /* Addr of last intracoded mblock.  */
223   int recon_right_for_prev;              /* Past right forw. vector.         */
224   int recon_down_for_prev;               /* Past down forw. vector.          */
225   int recon_right_back_prev;             /* Past right back vector.          */
226   int recon_down_back_prev;              /* Past down back vector.           */
227 } Macroblock;
228 
229 /* Block structure. */
230 
231 typedef struct block {
232   short int dct_recon[8][8];             /* Reconstructed dct coeff matrix. */
233   short int dct_dc_y_past;               /* Past lum. dc dct coefficient.   */
234   short int dct_dc_cr_past;              /* Past cr dc dct coefficient.     */
235   short int dct_dc_cb_past;              /* Past cb dc dct coefficient.     */
236 } Block;
237 
238 /* Video stream structure. */
239 
240 typedef struct vid_stream {
241   unsigned int h_size;                         /* Horiz. size in pixels.     */
242   unsigned int v_size;                         /* Vert. size in pixels.      */
243   unsigned int mb_height;                      /* Vert. size in mblocks.     */
244   unsigned int mb_width;                       /* Horiz. size in mblocks.    */
245   unsigned char aspect_ratio;                  /* Code for aspect ratio.     */
246   unsigned char picture_rate;                  /* Code for picture rate.     */
247   unsigned int bit_rate;                       /* Bit rate.                  */
248   unsigned int vbv_buffer_size;                /* Minimum buffer size.       */
249   BOOLEAN const_param_flag;                    /* Contrained parameter flag. */
250   unsigned char intra_quant_matrix[8][8];      /* Quantization matrix for
251 						  intracoded frames.         */
252   unsigned char non_intra_quant_matrix[8][8];  /* Quanitization matrix for
253 						  non intracoded frames.     */
254   char *ext_data;                              /* Extension data.            */
255   char *user_data;                             /* User data.                 */
256   GoP group;                                   /* Current group of pict.     */
257   Pict picture;                                /* Current picture.           */
258   Slice slice;                                 /* Current slice.             */
259   Macroblock mblock;                           /* Current macroblock.        */
260   Block block;                                 /* Current block.             */
261   int state;                                   /* State of decoding.         */
262   int bit_offset;                              /* Bit offset in stream.      */
263   unsigned int *buffer;                        /* Pointer to next byte in
264 						  buffer.                    */
265   int buf_length;                              /* Length of remaining buffer.*/
266   unsigned int *buf_start;                     /* Pointer to buffer start.   */
267 
268 /* VC - beginning of added variables for noise computation */
269   short noise_base_matrix[8][8];               /* Square quantization error  */
270 /* VC - end of added variables */
271 
272 /* Brown - beginning of added variables that used to be static or global */
273   int max_buf_length;                          /* Max length of buffer.      */
274   int film_has_ended;                          /* Boolean - film has ended   */
275   unsigned int num_left;                       /* from ReadPacket - leftover */
276   unsigned int leftover_bytes;                 /* from ReadPacket - leftover */
277   int EOF_flag;                                /* stream is EOF              */
278   BOOLEAN Parse_done;                          /* from read_sys              */
279   int right_for,down_for;                      /* From ReconPMBlock, video.c */
280   int right_half_for, down_half_for;
281   unsigned int curBits;                        /* current bits               */
282   int ditherType;                              /* What type of dithering     */
283   char *ditherFlags;                           /* flags for MB Ordered dither*/
284   int totNumFrames;                            /* Total Number of Frames     */
285   double realTimeStart;                        /* When did the movie start?  */
286 /* Brown - end of added variables */
287 
288   PictImage *past;                             /* Past predictive frame.     */
289   PictImage *future;                           /* Future predictive frame.   */
290   PictImage *current;                          /* Current frame.             */
291   PictImage *ring[RING_BUF_SIZE];              /* Ring buffer of frames.     */
292 
293 /* KR - beginning of added variables */
294   double rate_deal;
295   int _skipFrame;
296   double _skipCount;
297   int _jumpFrame;
298   double _oneFrameTime;
299   MPEGvideo* _smpeg;
300 /* KR - end of added variables */
301 
302 /* SL - beginning of added variables for FPS calculation */
303 //#define CALCULATE_FPS
304 #define FPS_WINDOW 60
305 #ifdef CALCULATE_FPS
306   double frame_time[FPS_WINDOW];
307   int timestamp_index;
308 #endif
309 /* SL - end of added variables */
310 /* begining of added variables for system stream based sync */
311   double timestamp;
312   unsigned int *timestamp_mark;
313   bool timestamp_used;
314 /* begining of added variables */
315   bool need_frameadjust;
316   int  current_frame;
317 
318 } VidStream;
319 
320 /* Declaration of global display pointer. */
321 
322 /* Quiet mode flag. */
323 extern int quietFlag;
324 
325 /* Flag controlling the "Press return" prompt */
326 extern int requireKeypressFlag;
327 
328 /* Flag controlling speed vs. quality */
329 extern int qualityFlag;
330 
331 /* Gamma correction stuff */
332 extern int gammaCorrectFlag;
333 extern double gammaCorrect;
334 
335 /* Chroma correction stuff */
336 extern int chromaCorrectFlag;
337 extern double chromaCorrect;
338 
339 /* Definition of Contant integer scale factor. */
340 
341 #define CONST_BITS 13
342 
343 /* Misc DCT definitions */
344 #define DCTSIZE		8	/* The basic DCT block is 8x8 samples */
345 #define DCTSIZE2	64	/* DCTSIZE squared; # of elements in a block */
346 
347 #define GLOBAL			/* a function referenced thru EXTERNs */
348 
349 typedef short DCTELEM;
350 typedef DCTELEM DCTBLOCK[DCTSIZE2];
351 
352 
353 #ifdef ANALYSIS
354 extern unsigned int bitCount;
355 extern int showEachFlag;
356 extern unsigned int cacheHit[8][8];
357 extern unsigned int cacheMiss[8][8];
358 #endif
359 
360 #if !defined(__MIPSEL__) && (defined(MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__) || defined(__mipsel) || defined(__mipsel__))
361 #define __MIPSEL__ 1
362 #endif
363 
364 #if !defined(__MIPSEB__) && (defined(MIPSEB) || defined(__MIPSEB) || defined(__MIPSEB__) || defined(__mipseb) || defined(__mipseb__))
365 #define __MIPSEB__ 1
366 #endif
367 
368 #if !defined(__SPARC__) && (defined(SPARC) || defined(__SPARC) || defined(__SPARC__) || defined(__sparc) || defined(__sparc__))
369 #define __SPARC__ 1
370 #endif
371 
372 #if !defined(__alpha__) && (defined(ALPHA) || defined(__ALPHA) || defined(__ALPHA__) || defined(__alpha))
373 #define __alpha__ 1
374 #endif
375 
376 #if !defined(__680x0__) && (defined(__680x0) || defined(__680x0__))
377 #define __680x0__ 1
378 #endif
379 
380 #if !defined(__AIX__) && (defined(AIX) || defined(_AIX) || defined(__AIX) || defined(__AIX__))
381 #define __AIX__ 1
382 #endif
383 
384 #if !defined(__RS6000__) && (defined(__AIX__) || defined(RS6000) || defined(_RS6000) || defined(__RS6000) || defined(__RS6000__))
385 #define __RS6000__ 1
386 #endif
387 
388 #if !defined(__HPUX__) && (defined(HPUX) || defined(_HPUX) || defined(__HPUX) || defined(__HPUX__))
389 #define __HPUX__ 1
390 #endif
391 #if !defined(__HPUX__) && (defined(hpux) || defined(_hpux) || defined(__hpux) || defined(__hpux__))
392 #define __HPUX__ 1
393 #endif
394 
395 #if !defined(__VAX__) && (defined(VAX) || defined (__VAX))
396 #define __VAX__ 1
397 #endif
398 
399 #if !defined(__SCO__) && (defined(SCO) || defined(__SCO) || defined(sco) || defined(__sco__))
400 #define __SCO__ 1
401 #endif
402 
403 #include "SDL_endian.h"
404 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
405 #ifdef LITTLE_ENDIAN_ARCHITECTURE
406 #undef LITTLE_ENDIAN_ARCHITECTURE
407 #endif
408 #define BIG_ENDIAN_ARCHITECTURE 1
409 #else
410 #ifdef BIG_ENDIAN_ARCHITECTURE
411 #undef BIG_ENDIAN_ARCHITECTURE
412 #endif
413 #define LITTLE_ENDIAN_ARCHITECTURE 1
414 #endif
415 
416 #if !defined(LITTLE_ENDIAN_ARCHITECTURE) && !defined(BIG_ENDIAN_ARCHITECTURE)
417 #error Unknown endianism of architecture
418 #endif
419 
420 #ifdef __alpha__
421 #define SIXTYFOUR_BIT
422 #endif
423 
424 /* Warnings that may help in debugging MPEG streams */
425 //#define VERBOSE_WARNINGS
426 //#define VERBOSE_DEBUG
427 
428 #endif /* video.h already included */
429