File: filter.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 (766 lines) | stat: -rw-r--r-- 23,504 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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
/* MPEGSTAT - analyzing tool for MPEG-I video streams
 * 
 *
 *  Copyright (c) 1995 The Regents of the University of California.
 * All rights reserved.
 *
 * Technical University of Berlin, Germany, Dept. of Computer Science
 * Tom Pfeifer - Multimedia systems project - pfeifer@fokus.gmd.de
 *
 * Jens Brettin, Harald Masche, Alexander Schulze, Dirk Schubert
 *
 * ---------------------------
 *
 * Copyright (c) 1993 Technical University of Berlin, Germany
 * 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 notices and the following
 * two paragraphs appear in all copies of this software.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA 
 * or the Technical University of Berlin 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 or the Technical University of Berlin HAS BEEN ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 * THE UNIVERSITY OF CALIFORNIA and the Technical University of Berlin 
 * SPECIFICALLY DISCLAIM 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 and the Technical University of Berlin HAVE NO 
 * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, 
 * OR MODIFICATIONS.
 */

/*
 * analyzing routines
 * Most of the specilized statistics routines are here.
 */

#include <stdio.h>
#include "video.h"
#include "opts.h"
#include "proto.h"
#ifndef MIPS
#include <sys/time.h>
#else
#include <sys/types.h>
#include <sys/system.h>
#endif

/* Variables from Video.c */
extern Statval stat_a[4];
extern BlockVals blks;
extern char *VidRate[16];
extern double VidRateNum[16];
extern char *PelRatio[16];
extern int sys_layer, vidBytes, audBytes, sysBytes;
extern int rate_disp, rate_max, rate_min, f_code_ok;

/* Local Routines */
#define max(a,b)  (a>b?a:b)
static void init_stat_struct();

/*
 * mvstat
 * 
 * Keep motion vector statistics
 *
 */
int mvstat (value, mode)
int value, mode;
{
  static int max_horizontal_f = 0, max_vertical_f = 0;
  static int  max_horizontal_b = 0, max_vertical_b = 0;
  static int ave_h_b = 0, ave_h_f = 0, ave_v_b = 0, ave_v_f = 0;
  static int count_h_b = 0, count_h_f = 0, count_v_b = 0, count_v_f = 0;
  int *ip;
  
  switch (mode) {
  case 0:
    ip = &max_horizontal_f;
    count_h_f++;
    ave_h_f += abs(value);
    break;
  case 1:
    ip = &max_vertical_f;
    count_v_f++;
    ave_v_f += abs(value);
    break;
  case 2:
    ip = &max_horizontal_b;
    count_h_b++;
    ave_h_b += abs(value);
    break;
  case 3:
    ip = &max_vertical_b;
      count_v_b++;
      ave_v_b += abs(value);
    break;
  case 5:
    return max_horizontal_f;
  case 6:
    return max_vertical_f;
  case 7:
    return max_horizontal_b;
  case 8:
    return max_vertical_b;
  case 10 :
    if (count_h_f > 0)
      return( ave_h_f / count_h_f);
    else return -1;
  case 11 :
    if (count_v_f > 0)
      return( ave_v_f / count_v_f);
    else return -1;
  case 12 :
    if (count_h_b > 0)
      return( ave_h_b / count_h_b);
    else return -1;
  case 13 :
    if (count_v_b > 0)
      return( ave_v_b / count_v_b);
    else return -1;
    
  }
  *ip = max (*ip, abs (value));
  return 0;
}


/*
 * printtype prints the frame type during data parsing.
 */
printtype(vs)
VidStream *vs;
{

  static int first_time = 1;
  
  if(first_time) {
    printf("Picture coding types (bitstream order / display order):\n");
    first_time = 0;
  }
  
  switch (vs->picture.code_type) {
  case B_TYPE:
    printf("B");
    break;
  case P_TYPE:
    printf("P");
    break;
  case I_TYPE:
    printf("I");
    break;
  default:
    printf("?");
  }
  fflush(stdout);
}

/*
 * init_block_struct
 *
 * Setup the block data structure to collect detailed data.
 *
 */
void init_block_struct(a)
BlockVals *a;
{
   int i,j;
   a->frame=-1;
   a->slice=-1;
   a->block=0;
   a->btype=0;
   a->qs=0;
   a->mb_skipped=0;
   a->mb_coded=0;
   a->cblks=0;
   a->nblks=0;
   for (i=0; i<6; i++) a->chist[i]=0;
   for (i=1; i<4; i++) {
     for (j=1; j<32;j++) {
       a->q[i][j]=0;
     }}
 }


/*
 * init_stats
 *
 * Setup the statistics structures
 *
 */
void
init_stats()
{
  int i, j;

  for (i = 0; i < 4; i++) {
    init_stat_struct(&(stat_a[i]));
    stat_a[i].frametype = i;
  }
  bitCount = 0;
  init_block_struct(&blks);
}

/*
 * PrintAllStats
 *
 * Print the summary statistics for the entire stream
 *
 */
void
PrintAllStats()
{
  int i, j;
  int average, pixelnum;

  unsigned int supertot, supernum;
  double supertime;

  pixelnum = curVidStream->mb_width * 16 * curVidStream->mb_height * 16;

  printf("\nSUMMARY:\n\n");

  supertot = stat_a[1].totsize + stat_a[2].totsize + stat_a[3].totsize;
  supernum = stat_a[1].number + stat_a[2].number + stat_a[3].number;
  supertime = stat_a[1].tottime + stat_a[2].tottime + stat_a[3].tottime;

  if (sys_layer) {
    printf("Total number of frames: %d.  Length is %2.2f sec\n\n",
	   supernum, supernum*1.0/VidRateNum[curVidStream->picture_rate]);
    } else {
      printf("Total Bytes read: %d. Total number of frames: %d.  Length is %2.2f sec\n\n",
	     bitCountRead()/8, supernum, supernum*1.0/VidRateNum[curVidStream->picture_rate]);
    }
  printf("Width: %d\tHeight: %d\n", curVidStream->h_size,curVidStream->v_size);

  printf("Avg. Frame Size: %d bytes + %d bits  (average rate %2.2f bits/sec)\n",
	 supertot / (8 * supernum), (supertot / supernum) % 8,
	 1.0*supertot* VidRateNum[curVidStream->picture_rate] / supernum );
  if (opts&RATE_INFO) {
    if (opts&RATE_LENGTH_SET) {
      if (sys_layer) {
	int totBytes=vidBytes+audBytes+sysBytes;
	printf("Breakdown of system layer:\n");
	printf("\tVideo (with packet headers): %d bytes (%2.2f%%) %d/(%d frames)\n",
	       vidBytes,vidBytes*100.0/totBytes, vidBytes/rate_disp,rate_disp);
	printf("\tAudio (with packet headers): %d bytes (%2.2f%%) %d/(%d frames)\n",
	       audBytes,audBytes*100.0/totBytes,audBytes/rate_disp,rate_disp);
	printf("\tSystem (other headers, other packets, etc): %d bytes (%2.2f%%) %d/(%d frames)\n",
	       sysBytes,sysBytes*100.0/totBytes,sysBytes/rate_disp,rate_disp);
      }
      printf("Max instantaneous video rate %d, minimum %d bits/(%d frames)\n",
	     rate_max,rate_min,rate_disp);
    } else {
      if (sys_layer) {
	int totBytes=vidBytes+audBytes+sysBytes;
	int rate_disp=(int)(VidRateNum[curVidStream->picture_rate]+0.5);
	printf("Breakdown of system layer:\n");
	printf("    Video (no packet headers): %d bytes (%2.2f%%) %d bytes/sec\n",
	       vidBytes,vidBytes*100.0/totBytes, vidBytes/rate_disp);
	printf("    Audio (no packet headers): %d bytes (%2.2f%%) %d bytes/sec\n",
	       audBytes,audBytes*100.0/totBytes,audBytes/rate_disp);
	printf("    System (other headers, packets, etc): %d bytes (%2.2f%%) %d bytes/sec\n",
	       sysBytes,sysBytes*100.0/totBytes,sysBytes/rate_disp);
      }
      printf("Max instantaneous video rate %d, minimum %d bits/sec\n",
	     rate_max,rate_min);
    }
  } else {
    if (sys_layer) {
      int totBytes=vidBytes+audBytes+sysBytes;
      int rate_disp=(int)(VidRateNum[curVidStream->picture_rate]+0.5);
      printf("Breakdown of system layer:\n");
      printf("    Video (no packet headers): %d bytes (%2.2f%%) %d bytes/sec\n",
	     vidBytes,vidBytes*100.0/totBytes, vidBytes/rate_disp);
      printf("    Audio (no packet headers): %d bytes (%2.2f%%) %d bytes/sec\n",
	     audBytes,audBytes*100.0/totBytes,audBytes/rate_disp);
      printf("    System (other headers, packets, etc): %d bytes (%2.2f%%) %d bytes/sec\n",
	     sysBytes,sysBytes*100.0/totBytes,sysBytes/rate_disp);
    }
  }
  
  printf("\nTotal Compression Rate: %5.2f %% of uncompressed 24 bit images\n", 
		((float)supertot/(8.0*(float)supernum))*100.0 / 
                ((float)pixelnum*3.0) );

  printf("                        = %5.2f bits per pixel\n\n", 
		.24 * (((float)supertot/(8.0*(float)supernum))*100.0 / 
                ((float)pixelnum*3.0)) );

  printf("Number of Macroblocks");
  printf(" [width * height = sum]: %d x %d = %d per frame\n", 
         curVidStream->mb_width, curVidStream->mb_height,
	 curVidStream->mb_width * curVidStream->mb_height);
  printf("Skipped Macroblocks = %d (%2.2f%%), Coded Macroblocks = %d (%2.2f%%)\n",
	 blks.mb_skipped,blks.mb_skipped*100.0/(blks.mb_skipped+blks.mb_coded),
	 blks.mb_coded,blks.mb_coded*100.0/(blks.mb_skipped+blks.mb_coded));
  if (blks.cblks==0) printf("\tNo partially coded macroblocks.\n\n");
  else {
    printf("\tCoded blocks: %2.2f%%\t[",100.0*blks.cblks/blks.nblks);
    for (i=0; i<6; i++) {
      printf(" %2.2f ",100.0*blks.chist[i]/blks.cblks);
    }
    printf("]\n\n");
  }

  if (opts&TIME_MEASURE)
    printf("Total Time Decoding: %.4g secs. %.4g sec/frame or %.4g frames/sec.\n\n", 
	   supertime,supertime / ((double) supernum),((double) supernum) / supertime);

  printf("MPEG-Viewer requirements:\n");
  printf("\tPixel aspect ratio of %s\n",PelRatio[curVidStream->aspect_ratio]);
  printf("\tRequired display speed: %s\n",VidRate[curVidStream->picture_rate]);
  printf("\tSpecified bit rate is ");
  if (curVidStream->bit_rate==0x3FFFF) printf ("variable\n");
  else {
    int rt=curVidStream->bit_rate;
    if (rt<3) printf("%d bits/sec",400*rt);
    else if (rt<2500) printf("%4.2f KBits/sec",rt*0.4);
    else printf("%4.2f MBits/sec",rt*0.0004);
    printf(" (%d * 400bits/sec)\n",rt);
  }
  printf("\tRequested buffer size is %dK ints (16 bits/int).\n",
	 curVidStream->vbv_buffer_size);
  printf("\tAnd the constrained parameter flag is %s.\n",
	 curVidStream->const_param_flag?"on":"off");
  { /* Check to see if it actually *meets* the constrained parameters */
    int num_mb = curVidStream->mb_width * curVidStream->mb_height;
    int pict_rate = curVidStream->orig_picture_rate;

    if ((curVidStream->bit_rate <= 4640) &&
	(curVidStream->vbv_buffer_size <= 20) &&
	(pict_rate >= 1) &&
	(pict_rate <= 5) &&
	(curVidStream->h_size <= 768) &&
	(curVidStream->v_size <= 576) &&
	(num_mb * VidRateNum[pict_rate] <= 9900) &&
	f_code_ok &&
	(num_mb <= 396))
      printf("\tThe stream meets the constrained parameter requirements.\n");
    else {
      if ((curVidStream->vbv_buffer_size <= 20) &&
	  (num_mb*15 <= 9900) &&
	  (curVidStream->h_size <= 768) &&
	  (curVidStream->v_size <= 576) &&
	  f_code_ok &&
	  (pict_rate==9) &&  /* Illegal Xing sequence */
	  (num_mb <= 396)) {
	printf("\tOther than its invalid picture & bit rates, the stream meets the\n");
	printf("\t\tconstrained parameters (with rate corrected to 15).\n");
      } else {
	printf("\tThe stream does not meet the constrained parameter requirements,\n");
	printf("\tdue to the following factors:\n");
	if (curVidStream->bit_rate > 4640) 
	  printf("\t\tBit rate is %s.\n",
		 (curVidStream->bit_rate==0x3FFFF) ? "variable":"too high");
	if (curVidStream->vbv_buffer_size > 20) 
	  printf("\t\tVBV buffer is too large (%d bits).\n",
		 curVidStream->vbv_buffer_size*20*1024);
	if (pict_rate == 0) 
	  printf("\t\tPicture rate is invalid (0).\n");
	if (VidRateNum[pict_rate] > 31) 
	  printf("\t\tPicture rate is too high.\n");
	if (curVidStream->h_size > 768) 
	  printf("\t\tPicture is too wide.\n");
	if (curVidStream->v_size > 576) 
	  printf("\t\tPicture is too high (tall).\n");
	if (pict_rate > 8) 
	  printf("\t\tPicture rate is invalid (code is %d).\n",pict_rate);
	if (num_mb*VidRateNum[pict_rate] > 9900)
	  printf("\t\tPixels per second is too high.\n");
	if (!f_code_ok) 
	  printf("\t\tMotion vectors are larger than 8 (f_code>4)\n");
	if (num_mb > 396) 
	  printf("\t\tThere are too many macroblocks per second.\n");
      }}}
  
  if (opts & BITS_INFO) {
    extern long ones, zeros;
    printf("\t%ld (%4.2f%%) ones, %ld (%4.2f%%) zeros\n",
	   ones, (100.0*ones)/((float) ones+zeros),
	   zeros, (100.0*zeros)/((float) ones+zeros));
  }

  if (mvstat(0,5)==0 && mvstat(0,6)==0 && mvstat(0,7)==0 && mvstat(8,0)==0) {
    printf("\nNo motion vectors.\n");
  } else {
    printf("\nLength of vectors in %s pixels:\n", 
	   curVidStream->picture.full_pel_forw_vector ? "full" : "half");
    
    printf ("\tHorizontal forward vectors, maximum : %3d\taverage: %3d\n", 
	    mvstat (0, 5), mvstat(0, 10));
    printf ("\tVertical forward vectors, maximum   : %3d\taverage: %3d\n\n", 
	    mvstat (0, 6), mvstat(0, 11));
    if (mvstat(0,7)==0 && mvstat(8,0)==0) {
      printf("\tNo backward vectors.\n");
    } else {
      printf ("\tHorizontal backward vectors, maximum: %3d\taverage: %3d\n", 
	      mvstat (0, 7), mvstat(0, 12));
      printf ("\tVertical backward vectors, maximum  : %3d\taverage: %3d\n", 
	      mvstat (0, 8), mvstat(0, 13));
    }
  }

  printf("\n\nFrame specific information:\n\n");
  
  for (i = 1; i < 4; i++) {
    if (stat_a[i].number == 0)
      continue;

    printf("    %-3d %c FRAMES, average is:\n", stat_a[i].number, "IPB"[i-1]);

    average = stat_a[i].totsize / (8 * stat_a[i].number);
    printf("\tSize: %d bytes + %d bits (%2.2f%%)\n",
	   average, (stat_a[i].totsize / stat_a[i].number) % 8,
	   100.0*stat_a[i].totsize/supertot);

    printf("\tCompression Rate: %5.2f%%\n", 
    (float)average*100.0/ ( (float) pixelnum*3.0));

    printf("\tQ Factor [scales quantization matrix]: %2.2f\n", 
	   (1.0*stat_a[i].qual)/stat_a[i].qnum);

    if (stat_a[i].bi_mbnum > 0) {
      printf("\t%5.2f%% interpolated Macro Blocks\n", 
	((float) stat_a[i].bi_mbnum*100.0) / (stat_a[i].i_mbnum + 
	stat_a[i].p_mbnum + stat_a[i].b_mbnum + stat_a[i].bi_mbnum));
    }
    if (opts&TIME_MEASURE) 
      printf("\tTime to Decode: %f secs.\n",
	     (stat_a[i].tottime / ((double) stat_a[i].number)));
    printf("\n");
    if (opts&QSCALE_INFO) {
      fprintf(qscale_fp,"Block Quality Information (for %c Frames)\n\tQuality\tNumber\n",
	      "0IPB"[i]);
      for (j=1; j<32; j++) {
	if (blks.q[i][j]!=0) {
	  fprintf(qscale_fp,"\t%d\t%d\n",j,blks.q[i][j]);
	}}
      fprintf(qscale_fp,"\n");
    }
  }
  if (curVidStream->user_data != NULL) {
    printf("\tUser Data Specified (at sequence level):\n");
    print_binary(stdout, curVidStream->user_data, curVidStream->user_size);
  }

  if (opts&HIST_INFO) {
    PrintSummaryStat(1);
    PrintSummaryStat(2);
    PrintSummaryStat(3);
  }
}



/*
 * PrintSummaryStat
 *
 * Print the detailed Historgrams on particular frame types
 *
 */
void
PrintSummaryStat(typ)
int typ;
{
  int i;
  
  fprintf(hist_fp,"\n");
  switch (stat_a[typ].frametype) {
  case I_TYPE:
    fprintf(hist_fp,"I FRAME\n");
    break;
  case P_TYPE:
    fprintf(hist_fp,"P FRAME\n");
    break;
  case B_TYPE:
    fprintf(hist_fp,"B FRAME\n");
    break;
  }
  
  fprintf(hist_fp,"Size: %d bytes + %d bits\n", stat_a[typ].totsize / 8, stat_a[typ].totsize % 8);
  if (stat_a[typ].i_mbnum > 0) {
    fprintf(hist_fp,"\tI Macro Block Stats:\n");
    fprintf(hist_fp,"\t%d I Macroblocks\n", stat_a[typ].i_mbnum);
    fprintf(hist_fp,"\tAvg. Size: %d bytes + %d bits\n",
	    stat_a[typ].i_mbsize / (8 * stat_a[typ].i_mbnum),
	    (stat_a[typ].i_mbsize * stat_a[typ].i_mbnum) % 8);
    fprintf(hist_fp,"\t\tCoded Block Pattern Histogram:\n");
    for (i = 0; i < 64; i += 8) {
      fprintf(hist_fp,"\t%6d %6d %6d %6d %6d %6d %6d %6d\n", stat_a[typ].i_mbcbp[i],
	      stat_a[typ].i_mbcbp[i + 1], stat_a[typ].i_mbcbp[i + 2], stat_a[typ].i_mbcbp[i + 3],
	      stat_a[typ].i_mbcbp[i + 4], stat_a[typ].i_mbcbp[i + 5], stat_a[typ].i_mbcbp[i + 6],
	      stat_a[typ].i_mbcbp[i + 7]);
    }
    fprintf(hist_fp,"\n\t\tNumber of Coefficients/Block Histogram:\n");
    for (i = 0; i < 64; i += 8) {
      fprintf(hist_fp,"\t%6d %6d %6d %6d %6d %6d %6d %6d\n", stat_a[typ].i_mbcoeff[i],
	      stat_a[typ].i_mbcoeff[i + 1], stat_a[typ].i_mbcoeff[i + 2],
	      stat_a[typ].i_mbcoeff[i + 3], stat_a[typ].i_mbcoeff[i + 4],
	      stat_a[typ].i_mbcoeff[i + 5], stat_a[typ].i_mbcoeff[i + 6],
	      stat_a[typ].i_mbcoeff[i + 7]);
    }
  }
  if (stat_a[typ].p_mbnum > 0) {
    fprintf(hist_fp,"\tP Macro Block Stats:\n");
    fprintf(hist_fp,"\t%d P Macroblocks\n", stat_a[typ].p_mbnum);
    fprintf(hist_fp,"\tAvg. Size: %d bytes + %d bits\n",
	    stat_a[typ].p_mbsize / (8 * stat_a[typ].p_mbnum),
	    (stat_a[typ].p_mbsize / stat_a[typ].p_mbnum) % 8);
    fprintf(hist_fp,"\t\tCoded Block Pattern Histogram:\n");
    for (i = 0; i < 64; i += 8) {
      fprintf(hist_fp,"\t%6d %6d %6d %6d %6d %6d %6d %6d\n", stat_a[typ].p_mbcbp[i],
	      stat_a[typ].p_mbcbp[i + 1], stat_a[typ].p_mbcbp[i + 2], stat_a[typ].p_mbcbp[i + 3],
	      stat_a[typ].p_mbcbp[i + 4], stat_a[typ].p_mbcbp[i + 5], stat_a[typ].p_mbcbp[i + 6],
	      stat_a[typ].p_mbcbp[i + 7]);
    }
    fprintf(hist_fp,"\n\t\tNumber of Coefficients/Block Histogram:\n");
    for (i = 0; i < 64; i += 8) {
      fprintf(hist_fp,"\t%6d %6d %6d %6d %6d %6d %6d %6d\n", stat_a[typ].p_mbcoeff[i],
	      stat_a[typ].p_mbcoeff[i + 1], stat_a[typ].p_mbcoeff[i + 2],
	      stat_a[typ].p_mbcoeff[i + 3], stat_a[typ].p_mbcoeff[i + 4],
	      stat_a[typ].p_mbcoeff[i + 5], stat_a[typ].p_mbcoeff[i + 6],
	      stat_a[typ].p_mbcoeff[i + 7]);
    }
  }
  if (stat_a[typ].b_mbnum > 0) {
    fprintf(hist_fp,"\tB Macro Block Stats:\n");
    fprintf(hist_fp,"\t%d B Macroblocks\n", stat_a[typ].b_mbnum);
    fprintf(hist_fp,"\tAvg. Size: %d bytes + %d bits\n",
	    stat_a[typ].b_mbsize / (8 * stat_a[typ].b_mbnum),
	    (stat_a[typ].b_mbsize / stat_a[typ].b_mbnum) % 8);
    fprintf(hist_fp,"\t\tCoded Block Pattern Histogram:\n");
    for (i = 0; i < 64; i += 8) {
      fprintf(hist_fp,"\t%6d %6d %6d %6d %6d %6d %6d %6d\n", stat_a[typ].b_mbcbp[i],
	      stat_a[typ].b_mbcbp[i + 1], stat_a[typ].b_mbcbp[i + 2], stat_a[typ].b_mbcbp[i + 3],
	      stat_a[typ].b_mbcbp[i + 4], stat_a[typ].b_mbcbp[i + 5], stat_a[typ].b_mbcbp[i + 6],
	      stat_a[typ].b_mbcbp[i + 7]);
    }
    fprintf(hist_fp,"\n\t\tNumber of Coefficients/Block Histogram:\n");
    for (i = 0; i < 64; i += 8) {
      fprintf(hist_fp,"\t%6d %6d %6d %6d %6d %6d %6d %6d\n", stat_a[typ].b_mbcoeff[i],
	      stat_a[typ].b_mbcoeff[i + 1], stat_a[typ].b_mbcoeff[i + 2],
	      stat_a[typ].b_mbcoeff[i + 3], stat_a[typ].b_mbcoeff[i + 4],
	      stat_a[typ].b_mbcoeff[i + 5], stat_a[typ].b_mbcoeff[i + 6],
	      stat_a[typ].b_mbcoeff[i + 7]);
    }
  }
  if (stat_a[typ].bi_mbnum > 0) {
    fprintf(hist_fp,"\tBi Macro Block Stats:\n");
    fprintf(hist_fp,"\t%d Bi Macroblocks\n", stat_a[typ].bi_mbnum);
    fprintf(hist_fp,"\tAvg. Size: %d bytes + %d bits\n",
	    stat_a[typ].bi_mbsize / (8 * stat_a[typ].bi_mbnum),
	    (stat_a[typ].bi_mbsize * stat_a[typ].bi_mbnum) % 8);
    fprintf(hist_fp,"\t\tCoded Block Pattern Histogram:\n");
    for (i = 0; i < 64; i += 8) {
      fprintf(hist_fp,"\t%6d %6d %6d %6d %6d %6d %6d %6d\n", stat_a[typ].bi_mbcbp[i],
	      stat_a[typ].bi_mbcbp[i + 1], stat_a[typ].bi_mbcbp[i + 2], stat_a[typ].bi_mbcbp[i + 3],
	      stat_a[typ].bi_mbcbp[i + 4], stat_a[typ].bi_mbcbp[i + 5], stat_a[typ].bi_mbcbp[i + 6],
	      stat_a[typ].bi_mbcbp[i + 7]);
    }
    fprintf(hist_fp,"\n\t\tNumber of Coefficients/Block Histogram:\n");
    for (i = 0; i < 64; i += 8) {
      fprintf(hist_fp,"\t%6d %6d %6d %6d %6d %6d %6d %6d\n", stat_a[typ].bi_mbcoeff[i],
	      stat_a[typ].bi_mbcoeff[i + 1], stat_a[typ].bi_mbcoeff[i + 2],
	      stat_a[typ].bi_mbcoeff[i + 3], stat_a[typ].bi_mbcoeff[i + 4],
	      stat_a[typ].bi_mbcoeff[i + 5], stat_a[typ].bi_mbcoeff[i + 6],
	      stat_a[typ].bi_mbcoeff[i + 7]);
    }
  }
}

/*
 * init_stat_struct
 *
 * clear for use a sinlge statistics structure
 */
static void
init_stat_struct(astat)
  Statval *astat;
{
  int j;

  astat->frametype = 0;
  astat->totsize = 0;
  astat->number = 0;
  astat->i_mbsize = 0;
  astat->p_mbsize = 0;
  astat->b_mbsize = 0;
  astat->bi_mbsize = 0;
  astat->i_mbnum = 0;
  astat->p_mbnum = 0;
  astat->b_mbnum = 0;
  astat->bi_mbnum = 0;

  for (j = 0; j < 64; j++) {

    astat->i_mbcbp[j] = 0;
    astat->p_mbcbp[j] = 0;
    astat->b_mbcbp[j] = 0;
    astat->bi_mbcbp[j] = 0;
    astat->i_mbcoeff[j] = 0;
    astat->p_mbcoeff[j] = 0;
    astat->b_mbcoeff[j] = 0;
    astat->bi_mbcoeff[j] = 0;
  }
  astat->tottime = 0.0;
  astat->qual=0;  
  astat->qnum=0;
}


/*
 * CollectStats
 *
 * Move the proggressively collected data into its home
 *
 */
void
CollectStats()
{
  int i, j;

  i = stat_a[0].frametype;

  stat_a[i].totsize += stat_a[0].totsize;
  stat_a[i].number += stat_a[0].number;
  stat_a[i].i_mbsize += stat_a[0].i_mbsize;
  stat_a[i].p_mbsize += stat_a[0].p_mbsize;
  stat_a[i].b_mbsize += stat_a[0].b_mbsize;
  stat_a[i].bi_mbsize += stat_a[0].bi_mbsize;
  stat_a[i].i_mbnum += stat_a[0].i_mbnum;
  stat_a[i].p_mbnum += stat_a[0].p_mbnum;
  stat_a[i].b_mbnum += stat_a[0].b_mbnum;
  stat_a[i].bi_mbnum += stat_a[0].bi_mbnum;

  for (j = 0; j < 64; j++) {

    stat_a[i].i_mbcbp[j] += stat_a[0].i_mbcbp[j];
    stat_a[i].p_mbcbp[j] += stat_a[0].p_mbcbp[j];
    stat_a[i].b_mbcbp[j] += stat_a[0].b_mbcbp[j];
    stat_a[i].bi_mbcbp[j] += stat_a[0].bi_mbcbp[j];
    stat_a[i].i_mbcoeff[j] += stat_a[0].i_mbcoeff[j];
    stat_a[i].p_mbcoeff[j] += stat_a[0].p_mbcoeff[j];
    stat_a[i].b_mbcoeff[j] += stat_a[0].b_mbcoeff[j];
    stat_a[i].bi_mbcoeff[j] += stat_a[0].bi_mbcoeff[j];
  }

  stat_a[i].tottime += stat_a[0].tottime;

  init_stat_struct(&(stat_a[0]));

}


/*********************************/
/* A bunch of "obvious" routines */
/*********************************/

unsigned int
bitCountRead()
{
  return bitCount;
}

void
StartTime()
{
  stat_a[0].tottime = ReadSysClock();
}

void
EndTime()
{
  stat_a[0].tottime = ReadSysClock() - stat_a[0].tottime;
}

double
ReadSysClock()
{
  struct timeval tv;
  (void) gettimeofday(&tv, (struct timezone *)NULL);
  return (tv.tv_sec + tv.tv_usec / 1000000.0);
}

void
PrintTimeInfo()
{
  double spent;

  spent = ReadSysClock() - realTimeStart;

}




/*
 *
 *-------------------------------------------------------------
 *
 * PrintQT --
 *
 *    Called to print out Q tables
 *
 * Results:
 *    adds info to stream.
 *
 * Side effects:
 *     Writes to stream.
 *
 *-------------------------------------------------------------
 */
void PrintQT(stream,title,changed,ptr)
FILE *stream;
char *title;
int changed;
unsigned char ptr[];
{
  int i;
  
  if (changed) {
    fprintf(stream,"\nCustom %s matrix (offset %d, frame %d):\n",
	    title,bitCountRead(), blks.frame+1);
    for (i = 0; i < 64; i++) {
      if (i%8!=7) fprintf(stream,"%2d ",ptr[i]);
      else fprintf(stream,"%2d\n",ptr[i]);
    }
    fprintf(stream,"\n");
  } else fprintf(stream,"\nCustom %s matrix repeated at (offset %d, frame %d)\n",
	    title,bitCountRead(),blks.frame+1);
}



/* Utility routine to print possibly binary data */
void print_binary(fp, buf, size)
FILE *fp;
char *buf;
int size;
{
  int i,line,num_lines;
  char printables[17];
  
  num_lines=size/16;
  printables[16]=0;
  for (i=0;i<16;i++) printables[i]=' ';
  for (line=0; line<=num_lines; line++) {
    for (i=0; (i<16) && (i<size); i++) {
      fprintf(fp, "%2x ",*buf);
      printables[i]= isprint(*buf) ? *buf: '.';
      buf++;
    }
    if (i!=16) for(;i<16; i++) fprintf(fp, "   ");
    size-=16;
    fprintf(fp, "\t%s\n",printables);
    for (i=0;i<16;i++) printables[i]=' ';
  }}