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 #ifdef USE_ATI
155   struct vhar128_image *image;
156 #else
157   unsigned char *image;                  /* YV12 format image  */
158   unsigned char *luminance;              /* Luminance plane.   */
159   unsigned char *Cr;                     /* Cr plane.          */
160   unsigned char *Cb;                     /* Cb plane.          */
161 #endif
162   unsigned short int *mb_qscale;         /* macroblock info    */
163   int locked;                            /* Lock flag.         */
164   TimeStamp show_time;                   /* Presentation time. */
165 } PictImage;
166 
167 /* Group of pictures structure. */
168 
169 typedef struct GoP {
170   BOOLEAN drop_flag;                     /* Flag indicating dropped frame. */
171   unsigned int tc_hours;                 /* Hour component of time code.   */
172   unsigned int tc_minutes;               /* Minute component of time code. */
173   unsigned int tc_seconds;               /* Second component of time code. */
174   unsigned int tc_pictures;              /* Picture counter of time code.  */
175   BOOLEAN closed_gop;                    /* Indicates no pred. vectors to
176 					    previous group of pictures.    */
177   BOOLEAN broken_link;                   /* B frame unable to be decoded.  */
178   char *ext_data;                        /* Extension data.                */
179   char *user_data;                       /* User data.                     */
180 } GoP;
181 
182 /* Picture structure. */
183 
184 typedef struct pict {
185   unsigned int temp_ref;                 /* Temporal reference.             */
186   unsigned int code_type;                /* Frame type: P, B, I             */
187   unsigned int vbv_delay;                /* Buffer delay.                   */
188   BOOLEAN full_pel_forw_vector;          /* Forw. vectors specified in full
189 					    pixel values flag.              */
190   unsigned int forw_r_size;              /* Used for vector decoding.       */
191   unsigned int forw_f;                   /* Used for vector decoding.       */
192   BOOLEAN full_pel_back_vector;          /* Back vectors specified in full
193 					    pixel values flag.              */
194   unsigned int back_r_size;              /* Used in decoding.               */
195   unsigned int back_f;                   /* Used in decoding.               */
196   char *extra_info;                      /* Extra bit picture info.         */
197   char *ext_data;                        /* Extension data.                 */
198   char *user_data;                       /* User data.                      */
199 } Pict;
200 
201 /* Slice structure. */
202 
203 typedef struct slice {
204   unsigned int vert_pos;                 /* Vertical position of slice. */
205   unsigned int quant_scale;              /* Quantization scale.         */
206   char *extra_info;                      /* Extra bit slice info.       */
207 } Slice;
208 
209 /* Macroblock structure. */
210 
211 typedef struct macroblock {
212   int mb_address;                        /* Macroblock address.              */
213   int past_mb_addr;                      /* Previous mblock address.         */
214   int motion_h_forw_code;                /* Forw. horiz. motion vector code. */
215   unsigned int motion_h_forw_r;          /* Used in decoding vectors.        */
216   int motion_v_forw_code;                /* Forw. vert. motion vector code.  */
217   unsigned int motion_v_forw_r;          /* Used in decdoinge vectors.       */
218   int motion_h_back_code;                /* Back horiz. motion vector code.  */
219   unsigned int motion_h_back_r;          /* Used in decoding vectors.        */
220   int motion_v_back_code;                /* Back vert. motion vector code.   */
221   unsigned int motion_v_back_r;          /* Used in decoding vectors.        */
222   unsigned int cbp;                      /* Coded block pattern.             */
223   BOOLEAN mb_intra;                      /* Intracoded mblock flag.          */
224   BOOLEAN bpict_past_forw;               /* Past B frame forw. vector flag.  */
225   BOOLEAN bpict_past_back;               /* Past B frame back vector flag.   */
226   int past_intra_addr;                   /* Addr of last intracoded mblock.  */
227   int recon_right_for_prev;              /* Past right forw. vector.         */
228   int recon_down_for_prev;               /* Past down forw. vector.          */
229   int recon_right_back_prev;             /* Past right back vector.          */
230   int recon_down_back_prev;              /* Past down back vector.           */
231 } Macroblock;
232 
233 /* Block structure. */
234 
235 typedef struct block {
236 #ifdef USE_ATI
237   long int dct_recon[6][130];            /* Reconstructed dct runs & levels */
238 #else
239   short int dct_recon[8][8];             /* Reconstructed dct coeff matrix. */
240 #endif
241   short int dct_dc_y_past;               /* Past lum. dc dct coefficient.   */
242   short int dct_dc_cr_past;              /* Past cr dc dct coefficient.     */
243   short int dct_dc_cb_past;              /* Past cb dc dct coefficient.     */
244 } Block;
245 
246 /* Video stream structure. */
247 
248 typedef struct vid_stream {
249   unsigned int h_size;                         /* Horiz. size in pixels.     */
250   unsigned int v_size;                         /* Vert. size in pixels.      */
251   unsigned int mb_height;                      /* Vert. size in mblocks.     */
252   unsigned int mb_width;                       /* Horiz. size in mblocks.    */
253   unsigned char aspect_ratio;                  /* Code for aspect ratio.     */
254   unsigned char picture_rate;                  /* Code for picture rate.     */
255   unsigned int bit_rate;                       /* Bit rate.                  */
256   unsigned int vbv_buffer_size;                /* Minimum buffer size.       */
257   BOOLEAN const_param_flag;                    /* Contrained parameter flag. */
258   unsigned char intra_quant_matrix[8][8];      /* Quantization matrix for
259 						  intracoded frames.         */
260   unsigned char non_intra_quant_matrix[8][8];  /* Quanitization matrix for
261 						  non intracoded frames.     */
262   char *ext_data;                              /* Extension data.            */
263   char *user_data;                             /* User data.                 */
264   GoP group;                                   /* Current group of pict.     */
265   Pict picture;                                /* Current picture.           */
266   Slice slice;                                 /* Current slice.             */
267   Macroblock mblock;                           /* Current macroblock.        */
268   Block block;                                 /* Current block.             */
269   int state;                                   /* State of decoding.         */
270   int bit_offset;                              /* Bit offset in stream.      */
271   unsigned int *buffer;                        /* Pointer to next byte in
272 						  buffer.                    */
273   int buf_length;                              /* Length of remaining buffer.*/
274   unsigned int *buf_start;                     /* Pointer to buffer start.   */
275 
276 /* VC - beginning of added variables for noise computation */
277   short noise_base_matrix[8][8];               /* Square quantization error  */
278 /* VC - end of added variables */
279 
280 /* Brown - beginning of added variables that used to be static or global */
281   int max_buf_length;                          /* Max length of buffer.      */
282   int film_has_ended;                          /* Boolean - film has ended   */
283   unsigned int num_left;                       /* from ReadPacket - leftover */
284   unsigned int leftover_bytes;                 /* from ReadPacket - leftover */
285   int EOF_flag;                                /* stream is EOF              */
286   BOOLEAN Parse_done;                          /* from read_sys              */
287   int right_for,down_for;                      /* From ReconPMBlock, video.c */
288   int right_half_for, down_half_for;
289   unsigned int curBits;                        /* current bits               */
290   int matched_depth;                           /* depth of displayed movie   */
291   int ditherType;                              /* What type of dithering     */
292   char *ditherFlags;                           /* flags for MB Ordered dither*/
293   int totNumFrames;                            /* Total Number of Frames     */
294   double realTimeStart;                        /* When did the movie start?  */
295 /* Brown - end of added variables */
296 
297   PictImage *past;                             /* Past predictive frame.     */
298   PictImage *future;                           /* Future predictive frame.   */
299   PictImage *current;                          /* Current frame.             */
300   PictImage *ring[RING_BUF_SIZE];              /* Ring buffer of frames.     */
301 
302 /* KR - beginning of added variables */
303   double rate_deal;
304   int _skipFrame;
305   double _skipCount;
306   int _jumpFrame;
307   double _oneFrameTime;
308   MPEGvideo* _smpeg;
309 /* KR - end of added variables */
310 
311 /* SL - beginning of added variables for FPS calculation */
312 //#define CALCULATE_FPS
313 #define FPS_WINDOW 60
314 #ifdef CALCULATE_FPS
315   double frame_time[FPS_WINDOW];
316   int timestamp_index;
317 #endif
318 /* SL - end of added variables */
319 /* begining of added variables for system stream based sync */
320   double timestamp;
321   unsigned int *timestamp_mark;
322   bool timestamp_used;
323 /* begining of added variables */
324   bool need_frameadjust;
325   int  current_frame;
326 
327 #ifdef USE_ATI
328   unsigned int ati_handle;
329 #endif
330 
331 } VidStream;
332 
333 /* Declaration of global display pointer. */
334 
335 /* Quiet mode flag. */
336 extern int quietFlag;
337 
338 /* Flag controlling the "Press return" prompt */
339 extern int requireKeypressFlag;
340 
341 /* Flag controlling speed vs. quality */
342 extern int qualityFlag;
343 
344 /* Gamma correction stuff */
345 extern int gammaCorrectFlag;
346 extern double gammaCorrect;
347 
348 /* Chroma correction stuff */
349 extern int chromaCorrectFlag;
350 extern double chromaCorrect;
351 
352 /* Definition of Contant integer scale factor. */
353 
354 #define CONST_BITS 13
355 
356 /* Misc DCT definitions */
357 #define DCTSIZE		8	/* The basic DCT block is 8x8 samples */
358 #define DCTSIZE2	64	/* DCTSIZE squared; # of elements in a block */
359 
360 #define GLOBAL			/* a function referenced thru EXTERNs */
361 
362 typedef short DCTELEM;
363 typedef DCTELEM DCTBLOCK[DCTSIZE2];
364 
365 
366 #ifdef ANALYSIS
367 extern unsigned int bitCount;
368 extern int showEachFlag;
369 extern unsigned int cacheHit[8][8];
370 extern unsigned int cacheMiss[8][8];
371 #endif
372 
373 #if !defined(__MIPSEL__) && (defined(MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__) || defined(__mipsel) || defined(__mipsel__))
374 #define __MIPSEL__ 1
375 #endif
376 
377 #if !defined(__MIPSEB__) && (defined(MIPSEB) || defined(__MIPSEB) || defined(__MIPSEB__) || defined(__mipseb) || defined(__mipseb__))
378 #define __MIPSEB__ 1
379 #endif
380 
381 #if !defined(__SPARC__) && (defined(SPARC) || defined(__SPARC) || defined(__SPARC__) || defined(__sparc) || defined(__sparc__))
382 #define __SPARC__ 1
383 #endif
384 
385 #if !defined(__alpha__) && (defined(ALPHA) || defined(__ALPHA) || defined(__ALPHA__) || defined(__alpha))
386 #define __alpha__ 1
387 #endif
388 
389 #if !defined(__680x0__) && (defined(__680x0) || defined(__680x0__))
390 #define __680x0__ 1
391 #endif
392 
393 #if !defined(__AIX__) && (defined(AIX) || defined(_AIX) || defined(__AIX) || defined(__AIX__))
394 #define __AIX__ 1
395 #endif
396 
397 #if !defined(__RS6000__) && (defined(__AIX__) || defined(RS6000) || defined(_RS6000) || defined(__RS6000) || defined(__RS6000__))
398 #define __RS6000__ 1
399 #endif
400 
401 #if !defined(__HPUX__) && (defined(HPUX) || defined(_HPUX) || defined(__HPUX) || defined(__HPUX__))
402 #define __HPUX__ 1
403 #endif
404 #if !defined(__HPUX__) && (defined(hpux) || defined(_hpux) || defined(__hpux) || defined(__hpux__))
405 #define __HPUX__ 1
406 #endif
407 
408 #if !defined(__VAX__) && (defined(VAX) || defined (__VAX))
409 #define __VAX__ 1
410 #endif
411 
412 #if !defined(__SCO__) && (defined(SCO) || defined(__SCO) || defined(sco) || defined(__sco__))
413 #define __SCO__ 1
414 #endif
415 
416 #if defined(__i386__) || defined(__VAX__) || defined(__MIPSEL__) || defined(__alpha__) || defined(__SCO__)
417 #undef  BIG_ENDIAN_ARCHITECTURE
418 #define LITTLE_ENDIAN_ARCHITECTURE 1
419 #endif
420 
421 #if defined(__RS6000__) || defined(__SPARC__) || defined(__mc68000__) || defined(__HPUX__) || defined(__MIPSEB__) || defined(convex) || defined(__convex__) || defined(__powerpc__)
422 #undef  LITTLE_ENDIAN_ARCHITECTURE
423 #define BIG_ENDIAN_ARCHITECTURE 1
424 #endif
425 
426 #if !defined(LITTLE_ENDIAN_ARCHITECTURE) && !defined(BIG_ENDIAN_ARCHITECTURE)
427 #ifdef WIN32
428 #undef  BIG_ENDIAN_ARCHITECTURE
429 #define LITTLE_ENDIAN_ARCHITECTURE
430 #else
431 #ifdef __BIG_ENDIAN__
432 #undef  LITTLE_ENDIAN_ARCHITECTURE
433 #define BIG_ENDIAN_ARCHITECTURE 1
434 #else
435 #include <sys/endian.h>
436 #if __BYTE_ORDER == __LITTLE_ENDIAN
437 #undef  BIG_ENDIAN_ARCHITECTURE
438 #define LITTLE_ENDIAN_ARCHITECTURE 1
439 #endif
440 #if __BYTE_ORDER == __BIG_ENDIAN
441 #undef  LITTLE_ENDIAN_ARCHITECTURE
442 #define BIG_ENDIAN_ARCHITECTURE 1
443 #endif
444 #endif
445 #endif
446 #endif
447 
448 #if !defined(LITTLE_ENDIAN_ARCHITECTURE) && !defined(BIG_ENDIAN_ARCHITECTURE)
449 #error Unknown endianism of architecture
450 #endif
451 
452 #ifdef __alpha__
453 #define SIXTYFOUR_BIT
454 #endif
455 
456 /* Warnings that may help in debugging MPEG streams */
457 //#define VERBOSE_WARNINGS
458 //#define VERBOSE_DEBUG
459 
460 #endif /* video.h already included */
461