File: mpegtest.c

package info (click to toggle)
libmpeg1 1.3.1-2.1
  • links: PTS
  • area: main
  • in suites: sarge, woody
  • size: 784 kB
  • ctags: 644
  • sloc: ansic: 8,520; sh: 1,333; makefile: 169
file content (321 lines) | stat: -rw-r--r-- 7,824 bytes parent folder | download | duplicates (3)
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
/*
 * mpegtest.c - decode an MPEG without displaying anything; just used
 * for benchmarking the MPEG Library.  Based on easympeg.c
 *
 * This is Unix-specific; it requires either the BSD-style getrusage()
 * system call, or the SysV/POSIX-style times() system call.
 *
 * By Greg Ward, 1995/05/27
 */

#include <config.h>

#include <stdlib.h>
#if HAVE_GETRUSAGE
# include <sys/time.h>
# include <sys/resource.h>
#else
# include <sys/types.h>
# include <sys/times.h>
# include <time.h>		/* for CLK_TCK */
#endif
#include <unistd.h>
#include <errno.h>
#include "ParseArgv.h"
#include "mpeg.h"

int   checksum;

#if (ENABLE_DITHER)
char *dither_name = NULL;
char *DitherNames [] = 
{
   "hybrid",
   "hybrid2",
   "fs4",
   "fs2",
   "fs2fast",
   "2x2",
   "gray",
   "fullcolor",
   "none",
   "ordered",
   "mono",
   "mono_threshold",
   "ordered2",
   "mbordered"
};
#define NUM_DITHER_TYPES 14
#endif  /* ENABLE_DITHER */

/*
 * Command-line options
 */
static ArgvInfo ArgTable [] = 
{
#if (ENABLE_DITHER)
   { "-dither", ARGV_STRING, NULL, (char *) &dither_name, "" },
#endif
   { "-checksum", ARGV_CONSTANT, (char *) 1, (char *) &checksum, "" },
   { NULL, ARGV_END, 0, 0, NULL }
};


void usage (char *name, char *msg)
{
#if (ENABLE_DITHER)
   fprintf (stderr, "Usage: %s [-dither <mode>] [-checksum] mpegfile\n", name);
#else
   fprintf (stderr, "Usage: %s [-checksum] mpegfile\n", name);
#endif
   if (strlen (msg) > 0)
      fprintf (stderr, "%s\n", msg);
   
   exit (1);
}   


/* ----------------------------- MNI Header -----------------------------------
@NAME       : SearchStringTable
@INPUT      : s - string to search for
              Table - table of strings in which to search
	      TableSize - number of strings in Table[]
	      MinUniqueLen - the minimum length, over all strings in
                             the table, required to determine uniqueness
	      ErrMsg - a format string for fprintf to print if s is not
	               found in Table.  ErrMsg should contain two instances
		       of "%s"; the first will be replaced with either 
		       "ambiguous" or "unknown" (depending on the nature
		       of the error), and the second will be replaced with
		       the search string s.
@OUTPUT     : 
@RETURNS    : The position of s in Table, or -1 if not found, or -2 if
              s is an ambiguous match.  Will print an error message
              (constructed from ErrMsg and s) to stderr if s not found
              in Table.
@DESCRIPTION: Searches a list of strings for a specific string.  The
              search string only has to match enough characters to be
	      non-ambiguous.
@METHOD     : (inspired by (i.e. stolen from) ParseArgv.c)
@GLOBALS    : (none)
@CALLS      : 
@CREATED    : 94/6/22, Greg Ward
@MODIFIED   : 94/7/29, GW: added MinUniqueLen arg and changed strcmp
                           to strncmp
              95/3/5, GW: finally changed so that it always finds non-
                          ambiguous matches without any help from caller
@COMMENTS   : This should be modified so that s just has to have enough
              characters to uniquely match one of the strings in Table[].
---------------------------------------------------------------------------- */
int SearchStringTable (char *s, char *Table[], int TableSize, char *ErrMsg)
{
   int	  i, match, len;

   /* Loop through the entire string table */

   match = -1;			/* indicate no match (yet) */
   len = strlen (s);
   
   for (i = 0; i < TableSize; i++) 
   {
      /* Skip to next table entry if this is definitely not a match */

      if (strncmp (s, Table [i], len) != 0)
	 continue;

      /* If we have an *exact* match, then get outta here now */

      if (Table[i][len] == (char) 0)
      {
	 match = i;
	 break;
      }
      
      /* If we found a match in a previous iteration, then it's ambiguous */
      
      if (match != -1)
      {
	 fprintf (stderr, ErrMsg, "ambiguous", s);
	 return (-2);
      }
              
      /* Otherwise record the current entry as a match. */

      match = i;
   }

   if (match == -1)
   {
      fprintf (stderr, ErrMsg, "unknown", s);
   }
   return (match);
}     /* SearchStringTable () */



int Checksum (ImageDesc *img, char *pixels)
{
   int   i;
   unsigned int   s = 0;
   
   for (i = 0; i < img->Size; i++)
   {
/*
      if (i % (img->Size/16) == 0) printf ("%02x ", (unsigned int) pixels[i]);
      if (i < 16) printf ("%02x ", (unsigned int) pixels[i]);
*/
      s += (unsigned int) pixels[i];
   }
   return s;
}   


float current_cpu_usage ()
{
#if HAVE_GETRUSAGE
   struct rusage  usage;

   getrusage (RUSAGE_SELF, &usage);
   return (float) usage.ru_utime.tv_sec + (usage.ru_utime.tv_usec / 1e6);
#else
   struct tms tms;
   
   times (&tms);
   return (float) tms.tms_utime / CLK_TCK;
#endif
}


int main (int argc, char *argv[])
{
   FILE       *mpeg;
   ImageDesc   img;
   char       *pixels;
   Boolean     moreframes = TRUE;
#if (ENABLE_DITHER)
   DitherEnum  dither_mode;
#endif
   int         num_frames;
   float       prev_time;	/* all times are in seconds */
   float       cur_time;
   float       frame_elapsed;
   float       total_time;

   if (ParseArgv (&argc, argv, ArgTable, ARGV_NO_DEFAULTS))
   {
      usage (argv[0], "");
   }

   if (argc != 2) 
   {
      usage (argv[0], "Wrong number of arguments");
   }

#if (ENABLE_DITHER)
   if (dither_name != NULL)
   {
      dither_mode = (DitherEnum)
	 SearchStringTable (dither_name, DitherNames,
			    NUM_DITHER_TYPES, "%s dithering type: %s\n");
      if (dither_mode < 0)
	 usage (argv[0], "");
   }
   else
   {
      dither_mode = FULL_COLOR_DITHER;
   }
#endif  /* ENABLE_DITHER */
   
   mpeg = fopen (argv[1], "rb");
   if (!mpeg)
   {
      perror (argv[1]);
      exit (1);
   }
      
#if (ENABLE_DITHER)
   SetMPEGOption (MPEG_DITHER, dither_mode);
#endif
   if (!OpenMPEG (mpeg, &img))
   {
      fprintf (stderr, "OpenMPEG on %s failed\n", argv[1]);
      exit (1);
   }

#if (ENABLE_DITHER)
   if (img.Colormap != NULL)	/* will images be colour-mapped? */
   {
      int  i;
      ColormapEntry *entry;
      
      printf ("Colour map:\n");
      
      for (i = 0; i < img.ColormapSize; i++)
      {
	 entry = img.Colormap + i;
	 
	 printf ("%3d: %u %u %u\n", i, 
		 (int) entry->red, 
		 (int) entry->green,
		 (int) entry->blue);
      }
   }
#endif
		 

   pixels = (char *) malloc (img.Size * sizeof(char));
   printf ("Movie is %d x %d pixels\n", img.Width, img.Height);
   printf ("Required picture rate = %d, required bit rate = %d\n",
	   img.PictureRate, img.BitRate);
#if (ENABLE_DITHER)
   printf ("Requested dithering mode = %s\n", DitherNames[dither_mode]);
#endif

   prev_time = current_cpu_usage ();
   total_time = 0.0;
   num_frames = 0;

   while (moreframes)	/* play frames until the movie ends */
   {
      moreframes = GetMPEGFrame (pixels);
      num_frames++;
      if (checksum)
      {
	 printf ("frame %d: 0x%08x\n", 
		 num_frames, Checksum (&img, pixels));
      }
      else
      {
	 putchar ('.'); fflush (stdout);
      }
	 

      cur_time = current_cpu_usage ();
      frame_elapsed = cur_time - prev_time;
      prev_time = cur_time;
/*
      printf ("time for frame %d: %g sec\n",
	      num_frames, frame_elapsed);
*/
      total_time += frame_elapsed;
   }

   /* 
    * Calling RewindMPEG() here has two purposes: 1) to assure me that a
    * former memory corruption bug is gone, and 2) to remind me that the
    * library leaks memory like a sieve when you use RewindMPEG().  Sigh.
    */
   RewindMPEG (mpeg, &img);
   CloseMPEG ();
   fclose (mpeg);
   free (pixels);

   total_time /= num_frames;
   printf ("\nAverage time per frame: %g sec ");
   if (total_time > 0)
      printf ("(%g frames/sec)\n", total_time, 1/total_time);
   else
      printf ("\n");

   return 0;
}