File: globals.c

package info (click to toggle)
libmpeg1 1.2.2-4
  • links: PTS
  • area: main
  • in suites: hamm, slink
  • size: 736 kB
  • ctags: 637
  • sloc: ansic: 8,415; sh: 1,328; makefile: 123
file content (400 lines) | stat: -rw-r--r-- 11,457 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
 * Copyright (c) 1994 by Gregory P. Ward.
 * All rights reserved.
 * 
 * This file is part of the MNI front end to the Berkeley MPEG decoder.
 * 
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice and the following
 * two paragraphs appear in all copies of this software.
 * 
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT,
 * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE
 * UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT
 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER
 * IS ON AN "AS IS" BASIS, AND THE AUTHOR HAS NO OBLIGATION TO PROVIDE
 * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.  
 */

/* ----------------------------- MNI Header -----------------------------------
@NAME       : globals.c
@INPUT      : 
@OUTPUT     : 
@RETURNS    : 
@DESCRIPTION: Definitions of global variables and functions used 
              internally by the MPEG video decoder.  The functions 
              defined in this file are those that are called by the 
              Berkeley decoding engine, but aren't really integral
              to decoding MPEG's, namely functions to read data from
              the stream and to "do something" with each frame after 
              it is decoded.

              Global variables defined here, and declared in globals.h
              (i.e. those used by the decoding engine itself)
                 ditherType
                 input

	      Functions defined here:
	         yiffprintf ()
	         get_more_data ()
		 DoDitherImage ()
		 ExecuteDisplay ()
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : Greg Ward, 94/6/16.
@MODIFIED   : 
---------------------------------------------------------------------------- */

#include <config.h>
#include <stdio.h>
#include <sys/types.h>   /* to make netinet/in.h happy */
#include <netinet/in.h>  /* for htonl() [there is no "real" net stuff here!] */
#include "video.h"
#include "globals.h"
#ifdef DEBUG
#include <stdarg.h>		/* for yiffprintf () */
#endif


/* Universal global variables -- those needed by the decoding engine: */

DitherEnum  ditherType = FULL_COLOR_DITHER;
FILE       *input;		/* file pointer to incoming data. */

/* Global variables used only in this source file: */

Boolean EOF_flag = FALSE; /* have we reached end of input stream? */

/* Declarations for global variables shared between this file and 
 * wrapper.c (all are defined in wrapper.c)
 */

extern Boolean   FrameDone;
extern char     *CurrentImage;
extern ImageDesc ImageInfo;
extern int       totNumFrames;

#ifdef DEBUG
int yiffprintf (char *format, ...)
{
   va_list  arglist;
   int      n;
   
   va_start (arglist, format);
   n = vprintf (format, arglist);
   va_end (arglist);
   return (n);
}
#else
int yiffprintf (char *format, ...) {}
#endif


/* ----------------------------- MNI Header -----------------------------------
@NAME       : get_more_data
@INPUT      : buf_start
              max_length
              length_ptr
              buf_ptr
@OUTPUT     : 
@RETURNS    : Returns 1 if data read, 0 if EOF, -1 if error.
@DESCRIPTION: Called by correct_underflow in bit parsing utilities to
              read in more data.  Updates input buffer and buffer length.

              Note that this function is called in a rather sneaky,
              implicit manner -- the decoding routines call macros
              in util.h, and those macros call get_more_data.
@METHOD     : 
@GLOBALS    : input - file pointer to the open MPEG stream
@CALLS      : 
@CREATED    : (taken from the original Berkeley code)
@MODIFIED   : 
---------------------------------------------------------------------------- */
int 
get_more_data(buf_start, max_length, length_ptr, buf_ptr)
     unsigned int *buf_start;
     int max_length;
     int *length_ptr;
     unsigned int **buf_ptr;
{
  
  int length, num_read, i, request;
  unsigned char *buffer, *mark;
  unsigned int *lmark;

  if (EOF_flag) return 0;

  length = *length_ptr;
  buffer = (unsigned char *) *buf_ptr;

  if (length > 0) {
    memcpy((unsigned char *) buf_start, buffer, (length*4));
    mark = ((unsigned char *) (buf_start + length));
  }
  else {
    mark = (unsigned char *) buf_start;
    length = 0;
  }

  request = (max_length-length)*4;
  
  num_read = fread( mark, 1, request, input);

  /* Paulo Villegas - 26/1/1993: Correction for 4-byte alignment */
  {
    int num_read_rounded;
    unsigned char *index;
 
    num_read_rounded = 4*(num_read/4);
 
    /* this can happen only if num_read<request; i.e. end of file reached */
    if( num_read_rounded < num_read )
      { 
 	num_read_rounded = 4*( num_read/4+1 );
 	/* fill in with zeros */
 	for( index=mark+num_read; index<mark+num_read_rounded; *(index++)=0 );
 	/* advance to the next 4-byte boundary */
 	num_read = num_read_rounded;
      }
  }
  
  if   (num_read < 0) {
    return -1;
  }
  else if (num_read == 0) {
    *buf_ptr = buf_start;
    
    /* Make 32 bits after end equal to 0 and 32
       bits after that equal to seq end code
       in order to prevent messy data from infinite
       recursion.
    */

    *(buf_start + length) = 0x0;
    *(buf_start + length+1) = SEQ_END_CODE;

    EOF_flag = 1;
    return 0;
  }

  lmark = (unsigned int *) mark;

  num_read = num_read/4;

  for (i=0; i<num_read; i++) {
    *lmark = htonl(*lmark);
    lmark++;
  }

  *buf_ptr = buf_start;
  *length_ptr = length + num_read;
 
  return 1;
}    /* get_more_data () */



#if (ENABLE_DITHER)
/* ----------------------------- MNI Header -----------------------------------
@NAME       : DoDitherImage
@INPUT      : l, Cr, Cb - pointers to the luminance, Cr, and Cb planes
              disp - ?
              h, w - height and width of image (?)
@OUTPUT     : 
@RETURNS    : 
@DESCRIPTION: Called when image needs to be dithered. Selects correct
              dither routine based on info in ditherType.
@METHOD     : 
@GLOBALS    : ditherType
@CALLS      : One of the following, depending on the value of ditherType:
                 HybridDitherImage       (hybrid.c)
		 HybridErrorDitherImage  (hybriderr.c)
		 FS2FastDitherImage      (fs2fast.c)
		 FS2DitherImage          (fs2.c)
		 FS4DitherImage          (fs4.c)
		 Twox2DitherImage        (2x2.c)
		 ColorDitherImage        (24bit.c)
		 GrayDitherImage         (gray.c)
		 OrderedDitherImage      (ordered.c)
		 MonoDitherImage         (mono.c)
		 MonoThresholdImage      (mono.c)
		 Ordered2DitherImage     (ordered2.c)
		 MBOrderedDitherImage    (mb_ordered.c)
@CREATED    : (taken from the original Berkeley code)
@MODIFIED   : 
---------------------------------------------------------------------------- */
void
DoDitherImage(l, Cr, Cb, disp, h, w) 
unsigned char *l, *Cr, *Cb, *disp;
int h,w;
{

  switch(ditherType) {
  case HYBRID_DITHER:
    HybridDitherImage(l, Cr, Cb, disp, h, w);
    break;

  case HYBRID2_DITHER:
    HybridErrorDitherImage(l, Cr, Cb, disp, h, w);
    break;

  case FS2FAST_DITHER:
    FS2FastDitherImage(l, Cr, Cb, disp, h, w);
    break;

  case FS2_DITHER:
    FS2DitherImage(l, Cr, Cb, disp, h, w);
    break;

  case FS4_DITHER:
    FS4DitherImage(l, Cr, Cb, disp, h, w);
    break;

  case Twox2_DITHER:
    Twox2DitherImage(l, Cr, Cb, disp, h, w);
    break;

  case FULL_COLOR_DITHER:
    ColorDitherImage(l, Cr, Cb, disp, h, w);
    break;

  case GRAY_DITHER:
    GrayDitherImage(l, Cr, Cb, disp, h, w);
    break;

  case NO_DITHER:
    break;

  case ORDERED_DITHER:
    OrderedDitherImage(l, Cr, Cb, disp, h, w);
    break;

  case MONO_DITHER:
    MonoDitherImage(l, Cr, Cb, disp, h, w);
    break;

  case MONO_THRESHOLD:
    MonoThresholdImage(l, Cr, Cb, disp, h, w);
    break;

  case ORDERED2_DITHER:
    Ordered2DitherImage(l, Cr, Cb, disp, h, w);
    break;

  case MBORDERED_DITHER:
    MBOrderedDitherImage(l, Cr, Cb, disp, h, w);


    break;
  }
}   /* DoDitherImage () */

#endif



#ifdef DEBUG
long ImageCheckSum (long Size, char *Data)
{
   long	*LongData = (long *) Data;
   long	 Sum = 0;
   int	 i, LongSize;

   LongSize = Size / sizeof(long);

   for (i = 0; i < LongSize; i++)
      Sum += LongData[i];
  
   return (Sum);      
}
#endif


#ifdef DEBUG
void PrintVidStreamStatus (VidStream *stream)
{
   int	i;

   printf ("  bit offset in stream    = %8d\n", stream->bit_offset);
   printf ("  remaining buffer length = %8d\n", stream->buf_length);
   printf ("  buffer contents:");
   for (i = 0; i < 5; i++) printf (" %08X", stream->buf_start[i]);
   putchar ('\n');

   printf ("  ring buffer of images:");
   for (i = 0; i < RING_BUF_SIZE; i++) printf (" %08X", stream->ring[i]);
   putchar ('\n');

   printf ("     past image = %08X\n", stream->past);
   printf ("   future image = %08X\n", stream->future);
   printf ("  current image = %08X\n", stream->current);
   
}
#else
void PrintVidStreamStatus (VidStream *stream)
{
}
#endif



/* ----------------------------- MNI Header -----------------------------------
@NAME       : ExecuteDisplay
@INPUT      : vid_stream - the currently-being-decoded video stream.
              The frame with which we are concerned is points to by
	      vid_stream->current->display.
@OUTPUT     : 
@RETURNS    : 
@DESCRIPTION:Called by the MPEG decoder after each frame has been
             decoded.  In the original (Berkeley) implementation,
             ExecuteDisplay called Xlib routines (XCreateImage,
             XPutImage) to display the just-decoded image immediately,
             but now all it does is update the frame counter, set the
             global variable CurrentImage (shared between wrapper.c
             and this file) to point to the image data, and set 
	     FrameDone to TRUE so that GetMPEGFrame() will know that
	     we have received another finished frame.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    : Greg Ward, 94/6/16 (almost nothing remains of the 
              ExecuteDisplay() in the original Berkeley source,
	      so I'm claiming creator status.)
@MODIFIED   : 
---------------------------------------------------------------------------- */
void ExecuteDisplay(VidStream *vid_stream)
{
   if (ditherType == NO_DITHER) return;

#ifdef DEBUG
   printf ("ExecuteDisplay:\n");
   PrintVidStreamStatus (vid_stream);
   printf ("  frame %2d: image checksum = %08X\n\n", totNumFrames,
	    ImageCheckSum (vid_stream->h_size*vid_stream->v_size*4, 
			   vid_stream->current->display));
#endif
   
   totNumFrames++;
   
   /* 
    * vid_stream->current->display points to the actual image data;
    * the actual format is described by the ImageDesc structure (which
    * was based on the XImage structure created by ExecuteDisplay() in
    * the original Berkeley code), and can be displayed however 
    * you like.
    *
    * Right now, we just copy the image data to a newly-allocated
    * area, which is pointed to by ImageData[totNumFrames].  Later we
    * will display these saved images as fast as possible.  
    */
   
   CurrentImage = (char *) vid_stream->current->display;
   FrameDone = TRUE;
}  /* ExecuteDisplay () */