File: playframe2.c

package info (click to toggle)
ucbmpeg 1r2-6
  • links: PTS
  • area: non-free
  • in suites: hamm, potato, slink
  • size: 9,504 kB
  • ctags: 7,643
  • sloc: ansic: 79,920; tcl: 2,985; perl: 313; asm: 284; makefile: 269; csh: 13
file content (709 lines) | stat: -rw-r--r-- 14,988 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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
/*
 * Copyright (c) 1995 The Regents of the University of California.
 * All rights reserved.
 * 
 * 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 UNIVERSITY OF CALIFORNIA 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 UNIVERSITY OF CALIFORNIA 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 UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 */
#include "video.h"
#include "proto.h"
#include <sys/types.h>
#include <signal.h>
#include <math.h>
#include "util.h"
#include "dither.h"
#include "tcl.h"
#include "tk.h"
#include "show.h"
#ifndef MIPS
#include <netinet/in.h>
#else
#include <bsd/netinet/in.h>
#endif


/* Function return type declarations */
void usage();

/* External declaration of main decoding call. */

extern VidStream *mpegVidRsrc();
extern VidStream *NewVidStream();

/* Declaration of global variable to hold dither info. */

int ditherType;

/* Global file pointer to incoming data. */
FILE *input = NULL;

/* End of File flag. */
int EOF_flag = 0;

/* Loop flag. */
int loopFlag = 0;

/* Shared memory flag. */
int shmemFlag = 0;

/* Quiet flag. */
int quietFlag = 0;

/* Minimum time to display a frame. */
double displayTime = 500.0;

/* Display image on screen? */
int noDisplayFlag = 0;

/* Setjmp/Longjmp env. */
jmp_buf env;

int bytesRead;
int oldBytesRead = 0;
double	stoppedTime;
int numSkipped = 0;

extern int movieOpen;

extern double chrom_mult;
extern double chrom_add;
extern double lum_mult;
extern double lum_add;
extern double atof();

VidStream *theStream = NULL;

StateHolder   *stateList = NULL;
int            stateListSize;
int            startedGOP = FALSE;
int            justRewound = FALSE;

void	ContinuePlay();



/*
 *--------------------------------------------------------------
 *
 * get_more_data --
 *
 *	Called by correct_underflow in bit parsing utilities to
 *      read in more data.
 *
 * Results:
 *	Input buffer updated, buffer length updated.
 *      Returns 1 if data read, 0 if EOF, -1 if error.
 *
 * Side effects:
 *      None.
 *
 *--------------------------------------------------------------
 */

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;

    if ( bytesRead != 0 )
    {
	UpdateStatusBar(bytesRead);
	ShowFPS();
    }

    oldBytesRead = bytesRead;	/* for expose events */
    bytesRead += num_read;

  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;
}

/*
 *--------------------------------------------------------------
 *
 * int_handler --
 *
 *	Handles Cntl-C interupts..
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	None.
 *
 *--------------------------------------------------------------
 */
#ifndef SIG_ONE_PARAM
void
int_handler()
{
  if (!quietFlag) {
    fprintf(stderr, "Interrupted!\n");
  }
  if (curVidStream != NULL)
    DestroyVidStream(&curVidStream);
  exit(1);
}
#else
void
int_handler(signum)
int signum;
{
  if (!quietFlag) {
    fprintf(stderr, "Interrupted!\n");
  }
  if (curVidStream != NULL)
    DestroyVidStream(&curVidStream);
  exit(1);
}
#endif

/*
 *--------------------------------------------------------------
 *
 * main --
 *
 *	Parses command line, starts decoding and displaying.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	None.
 *
 *--------------------------------------------------------------
 */

void
SetUpPlayFrame(browserDisplay, browserGC, browserWin)
    Display *browserDisplay;
    GC browserGC;
    Window browserWin;
{
  char *name;

  name = "";
#ifdef BLEAH
  ditherType = ORDERED2_DITHER;
#endif
  LUM_RANGE = 8;
  CR_RANGE = CB_RANGE = 4;
  noDisplayFlag = 0;
  chrom_mult = 1.0;
  chrom_add = 0.0;
  lum_mult = 1.0;
  lum_add = 0.0;

  lum_values = (int *) malloc(LUM_RANGE*sizeof(int));
  cr_values = (int *) malloc(CR_RANGE*sizeof(int));
  cb_values = (int *) malloc(CB_RANGE*sizeof(int));

  signal(SIGINT, int_handler);

  init_tables();
  
  switch (ditherType) {
    
  case HYBRID_DITHER:
    
    InitColor();
    InitHybridDither();
    FindGC(browserGC);
    FindWindow(browserWin);
    display = browserDisplay;
    InitDisplay(name);
    break;
    
    case HYBRID2_DITHER:
    InitColor();
    InitHybridErrorDither();
    FindGC(browserGC);
    FindWindow(browserWin);
    display = browserDisplay;
    InitDisplay(name);
    break;
    
  case FS4_DITHER:
    InitColor();
    InitFS4Dither();
    FindGC(browserGC);
    FindWindow(browserWin);
    display = browserDisplay;
    InitDisplay(name);
    break;
    
  case FS2_DITHER:
    InitColor();
    InitFS2Dither();
    FindGC(browserGC);
    FindWindow(browserWin);
    display = browserDisplay;
    InitDisplay(name);
    break;
    
  case FS2FAST_DITHER:
    InitColor();
    InitFS2FastDither();
    FindGC(browserGC);
    FindWindow(browserWin);
    display = browserDisplay;
    InitDisplay(name);
    break;
    
  case Twox2_DITHER:
    InitColor();
    Init2x2Dither();
    FindGC(browserGC);
    FindWindow(browserWin);
    display = browserDisplay;
    InitDisplay(name);
    PostInit2x2Dither();
    break;

  case RGBL_DITHER:
    InitRGBLColor();
    InitRGBLDisplay(name);
    InitRGBLDither();
    break;

  case GRAY_DITHER:
    InitGrayDisplay(name);
    break;

  case FULL_COLOR_DITHER:
    InitColorDither();
    InitColorDisplay(name);
    break;

  case NO_DITHER:
    shmemFlag = 0;
    break;

  case ORDERED_DITHER:
    InitColor();
    InitOrderedDither();
    FindGC(browserGC);
    FindWindow(browserWin);
    display = browserDisplay;
    InitDisplay(name);
    break;

  case MONO_DITHER:
  case MONO_THRESHOLD:
  case MONO_FS4_DITHER:
  case HALFTONE_DITHER:
    InitMonoDisplay(name);
    break;

  case ORDERED2_DITHER:
    InitColor();
    FindGC(browserGC);
    FindWindow(browserWin);
    display = browserDisplay;
    InitDisplay(name);
    InitOrdered2Dither();
    break;

  case BIG_ORDERED_DITHER:
    InitColor();
    FindGC(browserGC);
    FindWindow(browserWin);
    display = browserDisplay;
    InitDisplay(name);
    InitBigOrderedDither();
    break;

  case MBORDERED_DITHER:
    InitColor();
    FindGC(browserGC);
    FindWindow(browserWin);
    display = browserDisplay;
    InitDisplay(name);
    InitMBOrderedDither();
    break;

  default:
    fprintf(stdout, "Error:  incorrect dither type: %d\n",
	    ditherType);
    ditherType = ORDERED2_DITHER;
    break;
  }

    
}



/* Opens a movie and displays the first frame. Clears any state of a previous
   movie */
XImage *
playframe(movie, x, y, width, height, justFirst)
    char *movie;
    int x;
    int y;
    int *width;
    int *height;
    int justFirst;
{
  extern XImage *GetImage _ANSI_ARGS_((void));
  extern int numSkipped;
  extern Tcl_Interp * interp;
  extern jmp_buf env2;
  extern int first;
  int i;
  char someText[256];
  
  SetFirstFrame();
  SetDisplayPosition(x, y);
  sprintf( someText, "setOffsets %d %d\n", x, y);
  Tcl_Eval( interp, someText );
  ShowStatusOutline();
  
  input = fopen(movie, "r");
  if (input == NULL) {
    sprintf(someText, "AnnounceError \"Could not open file %s\"", movie);
    Tcl_Eval( interp, someText);
    theStream = (VidStream *)NULL;
    longjmp( env2, 1 );
  }
  
  movieOpen = TRUE;
  bytesRead = 0;
  numSkipped = 0;
  
  EOF_flag = 0;
  curBits = 0;
  bitOffset = 0;
  bufLength = 0;
  bitBuffer = NULL;
  totNumFrames = 0;
  init_stats();
  init_clock();
  stoppedTime = 0.0;
  
  if ( theStream != NULL )
	DestroyVidStream(&theStream);
  
  if ( theStream == NULL )
    theStream = NewVidStream(BUF_LENGTH);
  
  startedGOP = FALSE;

  first = 1;
  mpegVidRsrc(0, theStream);
  
  *width = curVidStream->h_size;
  *height = curVidStream->v_size;
  ContinuePlay(justFirst);
  
  return GetImage();
}


void	ContinuePlay(justFirst)
     int justFirst;
{
  extern int globalCommand;
  extern int	justShowOne;
  extern Tcl_Interp * interp;
  int lastNumFrames;
  
  if ( displayTime == 0.0 )
    lastNumFrames = totNumFrames;
  
  if ( (totNumFrames == 1) && justFirst )
    return;
  
  while ( mpegVidRsrc(0, theStream) != NULL ) {
    
    if ( (totNumFrames == 1) && justFirst )
      return;
    
    if ( justShowOne && (totNumFrames != lastNumFrames) )
      return;
    
    /* handle any TK events */
    if ( ! justFirst ) {
      /* COMMENT TO SELF - dsb - this is a hack - I don't get what's */
      /* up here, but sometimes you don't enter the while loop */
      /* under this if clause so you don't always stop when you should. */
      /* This should stop you, but it's all pretty confused. */
      if ( globalCommand != PLAY_COMMAND )
	return;
      while ( Tk_DoOneEvent(TK_X_EVENTS | TK_TIMER_EVENTS |
			    TK_DONT_WAIT | TK_IDLE_EVENTS) != 0 ) {
	extern int globalCommand;
	
	if ( tk_NumMainWindows == 0 )
	  exit(0);
	
	switch(globalCommand) {
	case REWIND_COMMAND:
	  fclose(input);
	  movieOpen = FALSE;
	  return;
	case STOP_COMMAND:
	  return;
	case PLAY_COMMAND:
	  Tcl_Eval( interp, "update idletasks\n");
	  break;
#ifndef NO_CHANGE
	case NEXT_COMMAND:
	  return;
#endif
	default:
	  break;
	}
      }
    }
  }
  oldBytesRead = bytesRead;
}



/*
 *--------------------------------------------------------------
 *
 * usage --
 *
 *	Print mpeg_show usage
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	exits with a return value -1
 *
 *--------------------------------------------------------------
 */

void
usage(s)
char *s;	/* program name */
{
#ifdef NO_CHANGE
    fprintf(stderr, "Usage:\n");
    fprintf(stderr, "mpeg_show\n");
    fprintf(stderr, "          [-nob]\n");
    fprintf(stderr, "          [-nop]\n");
    fprintf(stderr, "          [-dither {ordered|ordered2|mbordered|fs4|fs2|fs2fast|hybrid|\n");
    fprintf(stderr, "                    hybrid2|2x2|gray|color|none|mono|threshold}]\n");
    fprintf(stderr, "          [-loop]\n");
    fprintf(stderr, "          [-eachstat]\n");
    fprintf(stderr, "          [-no_display]\n");
    fprintf(stderr, "          [-quiet]\n");
    fprintf(stderr, "          [-l_range n]\n");
    fprintf(stderr, "          [-cr_range n]\n");
    fprintf(stderr, "          [-cb_range n]\n");
    fprintf(stderr, "          [-fps n]\n");
    fprintf(stderr, "          file_name\n");
#else
    fprintf(stderr, "Usage:\n");
    fprintf(stderr, "mpeg_analyzer [mpegfile] [dither]\n");
    fprintf(stderr, "\tdither\t{ordered|ordered2|mbordered|fs4|fs2|fs2fast|hybrid|\n");
    fprintf(stderr, "                    hybrid2]\n");

#endif
    exit (-1);
}



/*
 *--------------------------------------------------------------
 *
 * DoDitherImage --
 *
 *	Called when image needs to be dithered. Selects correct
 *      dither routine based on info in ditherType.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	None.
 *
 *--------------------------------------------------------------
 */

void
DoDitherImage(l, Cr, Cb, disp, h, w) 
unsigned char *l, *Cr, *Cb, *disp;
int h,w;
{
    static int counter = 0;
    char    fileName[256];
    FILE *filePtr;
    register int index;

#ifdef NO_CHANGE
    sprintf(fileName, "foobar%d.yuv", counter);
    counter++;
    filePtr = fopen(fileName, "w");
	fwrite(l, sizeof(unsigned char), h*w, filePtr);
	fwrite(Cr, sizeof(unsigned char), h*w/4, filePtr);
	fwrite(Cb, sizeof(unsigned char), h*w/4, filePtr);	    
    fclose(filePtr);
#endif    

  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 BIG_ORDERED_DITHER:
    BigOrderedDitherImage(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 MONO_FS4_DITHER:
    MonoFS4DitherImage(l, Cr, Cb, disp, h, w);
    break;

  case HALFTONE_DITHER:
    HalftoneDitherImage(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;

  case RGBL_DITHER:
    RGBLDitherImage(l, Cr, Cb, disp, h, w);
    break;
  }
}