about summary refs log tree commit diff
path: root/converter/ppm/ppmtompeg/iframe.c
blob: 66f5ea3b2b3be4dc32632a788164d8cc16dcc64d (plain) (blame)
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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
/*===========================================================================*
 * iframe.c                                  *
 *                                       *
 *  Procedures concerned with the I-frame encoding               *
 *                                       *
 * EXPORTED PROCEDURES:                              *
 *  GenIFrame                                *
 *  SetSlicesPerFrame                            *
 *  SetBlocksPerSlice                            *
 *  SetIQScale                               *
 *  GetIQScale                               *
 *  ResetIFrameStats                             *
 *  ShowIFrameSummary                            *
 *  EncodeYDC                                *
 *  EncodeCDC                                *
 *  time_elapsed                                                         *
 *                                       *
 *===========================================================================*/

/*
 * 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.
 */

/*==============*
 * HEADER FILES *
 *==============*/

#include <time.h>  /* Defines CLOCKS_PER_SEC, if this system has clock() */

#ifndef CLOCKS_PER_SEC
  /* System doesn't have clock(); we assume it has times() instead */
  #include <sys/times.h>
#endif

#include <sys/types.h>
#include <sys/param.h>

#include "netpbm/nstring.h"

#include "all.h"
#include "mtypes.h"
#include "frames.h"
#include "prototypes.h"
#include "block.h"
#include "mpeg.h"
#include "param.h"
#include "mheaders.h"
#include "fsize.h"
#include "parallel.h"
#include "postdct.h"
#include "rate.h"
#include "specifics.h"
#include "opts.h"

#include "iframe.h"

/*==================*
 * STATIC VARIABLES *
 *==================*/

static int lastNumBits = 0;
static int lastIFrame = 0;
static int numBlocks = 0;
static int numBits;
static int numFrames = 0;
static int numFrameBits = 0;
static int32 totalTime = 0;
static float    totalSNR = 0.0;
static float    totalPSNR = 0.0;

static int lengths[256] = {
    0, 1, 2, 2, 3, 3, 3, 3,     /* 0 - 7 */
    4, 4, 4, 4, 4, 4, 4, 4,     /* 8 - 15 */
    5, 5, 5, 5, 5, 5, 5, 5,     /* 16 - 31 */
    5, 5, 5, 5, 5, 5, 5, 5,
    6, 6, 6, 6, 6, 6, 6, 6,     /* 32 - 63 */
    6, 6, 6, 6, 6, 6, 6, 6,
    6, 6, 6, 6, 6, 6, 6, 6,
    6, 6, 6, 6, 6, 6, 6, 6,
    7, 7, 7, 7, 7, 7, 7, 7,     /* 64 - 127 */
    7, 7, 7, 7, 7, 7, 7, 7,
    7, 7, 7, 7, 7, 7, 7, 7,
    7, 7, 7, 7, 7, 7, 7, 7,
    7, 7, 7, 7, 7, 7, 7, 7,
    7, 7, 7, 7, 7, 7, 7, 7,
    7, 7, 7, 7, 7, 7, 7, 7,
    7, 7, 7, 7, 7, 7, 7, 7,
    8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8,
    8, 8, 8, 8, 8, 8, 8, 8
};


/*==================*
 * GLOBAL VARIABLES *
 *==================*/

int qscaleI;
int slicesPerFrame;
int blocksPerSlice;
int fCodeI, fCodeP, fCodeB;
boolean printSNR = FALSE;
boolean printMSE = FALSE;
boolean decodeRefFrames = FALSE;
int  TIME_RATE;



static int
SetFCodeHelper(int const SR) {

    int     range,fCode;

    if ( pixelFullSearch ) {
        range = SR;
    } else {
        range = SR*2;
    }

    if ( range < 256 ) {
        if ( range < 64 ) {
            if ( range < 32 ) {
                fCode = 1;
            } else {
                fCode = 2;
            }
        } else {
            if ( range < 128 ) {
                fCode = 3;
            } else {
                fCode = 4;
            }
        }
    } else {
        if ( range < 1024 ) {
            if ( range < 512 ) {
                fCode = 5;
            } else {
                fCode = 6;
            }
        } else {
            if ( range < 2048 ) {
                fCode = 7;
            } else {
                fprintf(stderr, "ERROR:  INVALID SEARCH RANGE!!!\n");
                exit(1);
            }
        }
    }
    return fCode;
}



/*===========================================================================*
 *
 * SetFCode
 *
 *  set the forward_f_code and backward_f_code according to the search
 *  range.  Must be called AFTER pixelFullSearch and searchRange have
 *  been initialized.  Irrelevant for I-frames, but computation is
 *  negligible (done only once, as well)
 *
 * RETURNS: nothing
 *
 * SIDE EFFECTS:    fCodeI,fCodeP,fCodeB
 *
 *===========================================================================*/
void
SetFCode(void) {
    fCodeI = SetFCodeHelper(1); /* GenIFrame ignores value */
    fCodeP = SetFCodeHelper(searchRangeP);
    fCodeB = SetFCodeHelper(searchRangeB);
}



/*===========================================================================*
 *
 * SetSlicesPerFrame
 *
 *  set the number of slices per frame
 *
 * RETURNS: nothing
 *
 * SIDE EFFECTS:    slicesPerFrame
 *
 *===========================================================================*/
void
SetSlicesPerFrame(int const number) {

    slicesPerFrame = number;
}



/*===========================================================================*
 *
 * SetBlocksPerSlice
 *
 *  set the number of blocks per slice, based on slicesPerFrame
 *
 * RETURNS: nothing
 *
 * SIDE EFFECTS:    blocksPerSlice
 *
 *===========================================================================*/
void
SetBlocksPerSlice(void) {

    int     totalBlocks;
    
    totalBlocks = (Fsize_y>>4)*(Fsize_x>>4);

    if ( slicesPerFrame > totalBlocks ) {
        blocksPerSlice = 1;
    } else {
        blocksPerSlice = totalBlocks/slicesPerFrame;
    }
}



/*===========================================================================*
 *
 * SetIQScale
 *
 *  set the I-frame Q-scale
 *
 * RETURNS: nothing
 *
 * SIDE EFFECTS:    qscaleI
 *
 *===========================================================================*/
void
SetIQScale(int const qI) {
    qscaleI = qI;
}



/*===========================================================================*
 *
 * GetIQScale
 *
 *  Get the I-frame Q-scale
 *
 * RETURNS: the Iframe Qscale
 *
 * SIDE EFFECTS:    none
 *
 *===========================================================================*/
int
GetIQScale(void) {
    return qscaleI;
}



static void
CalcDistortion(MpegFrame * const current,
               int         const y,
               int         const x) {

    int qscale, distort=0;
    Block decblk;
    FlatBlock fblk;
    int datarate = 0;
  
    for (qscale = 1; qscale < 32; qscale ++) {
        distort = 0;
        datarate = 0;
        Mpost_QuantZigBlock(dct[y][x], fblk, qscale, TRUE);
        Mpost_UnQuantZigBlock(fblk, decblk, qscale, TRUE);
        if (collect_distortion_detailed) datarate += CalcRLEHuffLength(fblk);
        mpeg_jrevdct((int16 *)decblk);
        distort += mse(current->y_blocks[y][x], decblk);

        Mpost_QuantZigBlock(dct[y][x+1], fblk, qscale, TRUE);
        Mpost_UnQuantZigBlock(fblk, decblk, qscale, TRUE);
        if (collect_distortion_detailed) datarate += CalcRLEHuffLength(fblk);
        mpeg_jrevdct((int16 *)decblk);
        distort += mse(current->y_blocks[y][x+1], decblk);

        Mpost_QuantZigBlock(dct[y+1][x], fblk, qscale, TRUE);
        Mpost_UnQuantZigBlock(fblk, decblk, qscale, TRUE);
        if (collect_distortion_detailed) datarate += CalcRLEHuffLength(fblk);
        mpeg_jrevdct((int16 *)decblk);
        distort += mse(current->y_blocks[y+1][x], decblk);

        Mpost_QuantZigBlock(dct[y+1][x+1], fblk, qscale, TRUE);
        Mpost_UnQuantZigBlock(fblk, decblk, qscale, TRUE);
        if (collect_distortion_detailed) datarate += CalcRLEHuffLength(fblk);
        mpeg_jrevdct((int16 *)decblk);
        distort += mse(current->y_blocks[y+1][x+1], decblk);

        Mpost_QuantZigBlock(dctb[y >> 1][x >> 1], fblk, qscale, TRUE);
        Mpost_UnQuantZigBlock(fblk, decblk, qscale, TRUE);
        if (collect_distortion_detailed) datarate += CalcRLEHuffLength(fblk);
        mpeg_jrevdct((int16 *)decblk);
        distort += mse(current->cb_blocks[y>>1][x>>1], decblk);

        Mpost_QuantZigBlock(dctr[y >> 1][x >> 1], fblk, qscale, TRUE);
        Mpost_UnQuantZigBlock(fblk, decblk, qscale, TRUE);
        if (collect_distortion_detailed) datarate += CalcRLEHuffLength(fblk);
        mpeg_jrevdct((int16 *)decblk);
        distort += mse(current->cr_blocks[y >> 1][x >> 1], decblk);

        if (!collect_distortion_detailed) {
            fprintf(distortion_fp, "\t%d\n", distort);
        } else if (collect_distortion_detailed == 1) {
            fprintf(distortion_fp, "\t%d\t%d\n", distort, datarate);
        } else {
            fprintf(fp_table_rate[qscale-1], "%d\n", datarate);
            fprintf(fp_table_dist[qscale-1], "%d\n", distort);
        }
    }
}



/*===========================================================================*
 *
 * GenIFrame
 *
 *  generate an I-frame; appends result to bb
 *
 * RETURNS: I-frame appended to bb
 *
 * SIDE EFFECTS:    none
 *
 *===========================================================================*/
void
GenIFrame(BitBucket * const bb, 
          MpegFrame * const current) {

    int x, y;
    int index;
    FlatBlock    fb[6];
    Block    dec[6];
    int32 y_dc_pred, cr_dc_pred, cb_dc_pred;
    int          totalBits;
    int          totalFrameBits;
    int32        startTime, endTime;
    float        snr[3], psnr[3];
    int          mbAddress;
    int          QScale;
    BlockMV      *info; /* Not used in Iframes, but nice to pass in anyway */
    int          bitstreamMode, newQScale;
    int          rc_blockStart=0;

    if (dct==NULL) AllocDctBlocks();
    if (collect_quant) {fprintf(collect_quant_fp, "# I\n");}

    /* set-up for statistics */
    numFrames++;
    totalFrameBits = bb->cumulativeBits;
    if ( showBitRatePerFrame ) {
        if ( lastNumBits == 0 ) {
            lastNumBits = bb->cumulativeBits;
            lastIFrame = current->id;
        } else {
            /* ASSUMES 30 FRAMES PER SECOND */
    
            if (! realQuiet) {
                fprintf(stdout, "I-to-I (frames %5d to %5d) bitrate:  %8d\n",
                        lastIFrame, current->id-1,
                        ((bb->cumulativeBits-lastNumBits)*30)/
                        (current->id-lastIFrame));
            }
    
            fprintf(bitRateFile, "I-to-I (frames %5d to %5d) bitrate:  %8d\n",
                    lastIFrame, current->id-1,
                    ((bb->cumulativeBits-lastNumBits)*30)/
                    (current->id-lastIFrame));
            lastNumBits = bb->cumulativeBits;       
            lastIFrame = current->id;
        }
    }
    
    startTime = time_elapsed();
    
    Frame_AllocBlocks(current);
    BlockifyFrame(current);

    DBG_PRINT(("Generating iframe\n"));
    QScale = GetIQScale();
    /*   Allocate bits for this frame for rate control purposes */
    bitstreamMode = getRateMode();
    if (bitstreamMode == FIXED_RATE) {
        targetRateControl(current);
    }

    Mhead_GenPictureHeader(bb, I_FRAME, current->id, fCodeI);
    /* Check for Qscale change */
    if (specificsOn) {
        newQScale = SpecLookup(current->id, 0, 0 /* junk */, &info, QScale);
        if (newQScale != -1) {
            QScale = newQScale;
        }
        /* check for slice */
        newQScale = SpecLookup(current->id, 1, 1, &info, QScale);
        if (newQScale != -1) {
            QScale = newQScale;
        }
    }
    Mhead_GenSliceHeader(bb, 1, QScale, NULL, 0);
    
    if ( referenceFrame == DECODED_FRAME ) {
        Frame_AllocDecoded(current, TRUE);
    } else if ( printSNR ) {
        Frame_AllocDecoded(current, FALSE);
    }
    
    y_dc_pred = cr_dc_pred = cb_dc_pred = 128;
    totalBits = bb->cumulativeBits;
    mbAddress = 0;

    /* DCT the macroblocks */
    for (y = 0;  y < (Fsize_y >> 3);  y += 2) {
        for (x = 0;  x < (Fsize_x >> 3);  x += 2) {
            if (collect_quant && (collect_quant_detailed & 1)) 
                fprintf(collect_quant_fp, "l\n");
            if (DoLaplace) {LaplaceCnum = 0;}
            mp_fwd_dct_block2(current->y_blocks[y][x], dct[y][x]);
            mp_fwd_dct_block2(current->y_blocks[y][x+1], dct[y][x+1]);
            mp_fwd_dct_block2(current->y_blocks[y+1][x], dct[y+1][x]);
            mp_fwd_dct_block2(current->y_blocks[y+1][x+1], dct[y+1][x+1]);
            if (collect_quant && (collect_quant_detailed & 1)) 
                fprintf(collect_quant_fp, "c\n");
            if (DoLaplace) {LaplaceCnum = 1;}
            mp_fwd_dct_block2(current->cb_blocks[y>>1][x>>1], 
                              dctb[y>>1][x>>1]);
            if (DoLaplace) {LaplaceCnum = 2;}
            mp_fwd_dct_block2(current->cr_blocks[y>>1][x>>1], 
                              dctr[y>>1][x>>1]);
        }
    }
    
    if (DoLaplace)
        CalcLambdas();

    for (y = 0;  y < (Fsize_y >> 3);  y += 2) {
        for (x = 0;  x < (Fsize_x >> 3);  x += 2) {
            /* Check for Qscale change */
            if (specificsOn) {
                newQScale = 
                    SpecLookup(current->id, 2, mbAddress, &info, QScale);
                if (newQScale != -1) {
                    QScale = newQScale;
                }
            }
    
            /*  Determine if new Qscale needed for Rate Control purposes  */
            if (bitstreamMode == FIXED_RATE) {
                rc_blockStart = bb->cumulativeBits;
                newQScale = needQScaleChange(qscaleI,
                                             current->y_blocks[y][x],
                                             current->y_blocks[y][x+1],
                                             current->y_blocks[y+1][x],
                                             current->y_blocks[y+1][x+1]);
                if (newQScale > 0) {
                    QScale = newQScale;
                }
            }
    
            if ( (mbAddress % blocksPerSlice == 0) && (mbAddress != 0) ) {
                /* create a new slice */
                if (specificsOn) {
                    /* Make sure no slice Qscale change */
                    newQScale = SpecLookup(current->id, 1,
                                           mbAddress/blocksPerSlice, &info, 
                                           QScale);
                    if (newQScale != -1) QScale = newQScale;
                }
                Mhead_GenSliceEnder(bb);
                Mhead_GenSliceHeader(bb, 1+(y>>1), QScale, NULL, 0);
                y_dc_pred = cr_dc_pred = cb_dc_pred = 128;
      
                GEN_I_BLOCK(I_FRAME, current, bb, 1+(x>>1), QScale);
            } else {
                GEN_I_BLOCK(I_FRAME, current, bb, 1, QScale);
            }

            if (WriteDistortionNumbers) {
                CalcDistortion(current, y, x);
            }
    
            if ( decodeRefFrames ) {
                /* now, reverse the DCT transform */
                LaplaceCnum = 0;
                for ( index = 0; index < 6; index++ ) {
                    if (!DoLaplace) {
                        Mpost_UnQuantZigBlock(fb[index], dec[index], QScale, 
                                              TRUE);
                    } else {
                        if (index == 4) {LaplaceCnum = 1;}
                        if (index == 5) {LaplaceCnum = 2;}
                        Mpost_UnQuantZigBlockLaplace(fb[index], dec[index], 
                                                     QScale, TRUE);
                    }
                    mpeg_jrevdct((int16 *)dec[index]);      
                }
      
                /* now, unblockify */
                BlockToData(current->decoded_y, dec[0], y, x);
                BlockToData(current->decoded_y, dec[1], y, x+1);
                BlockToData(current->decoded_y, dec[2], y+1, x);
                BlockToData(current->decoded_y, dec[3], y+1, x+1);
                BlockToData(current->decoded_cb, dec[4], y>>1, x>>1);
                BlockToData(current->decoded_cr, dec[5], y>>1, x>>1);
            }
    
            numBlocks++;
            mbAddress++;
            /*   Rate Control */
            if (bitstreamMode == FIXED_RATE) {
                incMacroBlockBits(bb->cumulativeBits - rc_blockStart);
                rc_blockStart = bb->cumulativeBits;
                MB_RateOut(TYPE_IFRAME);
            }
        }
    }
    
    if ( printSNR ) {
        BlockComputeSNR(current,snr,psnr);
        totalSNR += snr[0];
        totalPSNR += psnr[0];
    }
    
    numBits += (bb->cumulativeBits-totalBits);
    
    DBG_PRINT(("End of frame\n"));
    
    Mhead_GenSliceEnder(bb);
    /*   Rate Control  */
    if (bitstreamMode == FIXED_RATE) {
        updateRateControl(TYPE_IFRAME);
    }
    
    endTime = time_elapsed();
    totalTime += (endTime-startTime);
    
    numFrameBits += (bb->cumulativeBits-totalFrameBits);
    
    if ( showBitRatePerFrame ) {
        /* ASSUMES 30 FRAMES PER SECOND */
        fprintf(bitRateFile, "%5d\t%8d\n", current->id,
                30*(bb->cumulativeBits-totalFrameBits));
    }
    
    if ( frameSummary && !realQuiet ) {
        
        /* ASSUMES 30 FRAMES PER SECOND */
        fprintf(stdout, 
                "FRAME %d (I):  %ld seconds  (%d bits/s output)\n", 
                current->id, (long)((endTime-startTime)/TIME_RATE),
                30*(bb->cumulativeBits-totalFrameBits));
        if ( printSNR ) {
            fprintf(stdout, 
                    "FRAME %d:  SNR:  %.1f\t%.1f\t%.1f\t"
                    "PSNR:  %.1f\t%.1f\t%.1f\n",
                        current->id, snr[0], snr[1], snr[2],
                    psnr[0], psnr[1], psnr[2]);
        }
    }
}



/*===========================================================================*
 *
 * ResetIFrameStats
 *
 *  reset the I-frame statistics
 *
 * RETURNS: nothing
 *
 * SIDE EFFECTS:    none
 *
 *===========================================================================*/
void
ResetIFrameStats(void) {
    numBlocks = 0;
    numBits = 0;
    numFrames = 0;
    numFrameBits = 0;
    totalTime = 0;
}



float
IFrameTotalTime(void) {
    return (float)totalTime/(float)TIME_RATE;
}



void
ShowIFrameSummary(unsigned int const inputFrameBits, 
                  unsigned int const totalBits, 
                  FILE *       const fpointer) {
/*----------------------------------------------------------------------------
   Print out statistics on all I frames.
-----------------------------------------------------------------------------*/
    if (numFrames > 0) {
        fprintf(fpointer, "-------------------------\n");
        fprintf(fpointer, "*****I FRAME SUMMARY*****\n");
        fprintf(fpointer, "-------------------------\n");

        fprintf(fpointer, "  Blocks:    %5d     (%6d bits)     (%5d bpb)\n",
                numBlocks, numBits, numBits/numBlocks);
        fprintf(fpointer, "  Frames:    %5d     (%6d bits)     (%5d bpf)"
                "(%2.1f%% of total)\n",
                numFrames, numFrameBits, numFrameBits/numFrames,
                100.0*(float)numFrameBits/(float)totalBits);
        fprintf(fpointer, "  Compression:  %3d:1     (%9.4f bpp)\n",
                numFrames*inputFrameBits/numFrameBits,
                24.0*(float)numFrameBits/(float)(numFrames*inputFrameBits));
        if ( printSNR )
            fprintf(fpointer, "  Avg Y SNR/PSNR:  %.1f     %.1f\n",
                    totalSNR/(float)numFrames, totalPSNR/(float)numFrames);
        if ( totalTime == 0 ) {
            fprintf(fpointer, "  Seconds:  NONE\n");
        } else {
            fprintf(fpointer, "  Seconds:  %9ld     (%9.4f fps)  (%9ld pps)  "
                    "(%9ld mps)\n",
                    (long)(totalTime/TIME_RATE),
                    (float)((float)(TIME_RATE*numFrames)/(float)totalTime),
                    (long)((float)TIME_RATE * (float)numFrames *
                           (float)inputFrameBits/(24.0*(float)totalTime)),
                    (long)((float)TIME_RATE*(float)numFrames *
                           (float)inputFrameBits/(256.0*24.0 *
                                                  (float)totalTime)));
        }
    }
}



/*===========================================================================*
 *
 * EncodeYDC
 *
 *  Encode the DC portion of a DCT of a luminance block
 *
 * RETURNS: result appended to bb
 *
 * SIDE EFFECTS:    updates pred_term
 *
 *===========================================================================*/
void
EncodeYDC(int32       const dc_term,
          int32 *     const pred_term,
          BitBucket * const bb) {

    /* see Table B.5a -- MPEG-I doc */
    static int codes[9] = {
    0x4, 0x0, 0x1, 0x5, 0x6, 0xe, 0x1e, 0x3e, 0x7e
    };
    static int codeLengths[9] = {
    3,   2,   2,   3,   3,   4,   5,    6,    7
    };
    int ydiff, ydiff_abs;
    int length;

    ydiff = (dc_term - (*pred_term));
    if (ydiff > 255) {
#ifdef BLEAH 
      fprintf(stdout, "TRUNCATED\n");
#endif
    ydiff = 255;
    } else if (ydiff < -255) {
#ifdef BLEAH 
      fprintf(stdout, "TRUNCATED\n");
#endif
    ydiff = -255;
    }

    ydiff_abs = ABS(ydiff);
    length = lengths[ydiff_abs];
    Bitio_Write(bb, codes[length], codeLengths[length]);
    if ( length != 0 ) {
    if ( ydiff > 0 ) {
        Bitio_Write(bb, ydiff_abs, length);
    } else {
        Bitio_Write(bb, ~ydiff_abs, length);
    }
    }

    (*pred_term) += ydiff;
}



/*===========================================================================*
 *
 * EncodeCDC
 *
 *  Encode the DC portion of a DCT of a chrominance block
 *
 * RETURNS: result appended to bb
 *
 * SIDE EFFECTS:    updates pred_term
 *
 *===========================================================================*/
void
EncodeCDC(int32       const dc_term,
          int32     * const pred_term,
          BitBucket * const bb) {

    /* see Table B.5b -- MPEG-I doc */
    static int codes[9] = {
        0x0, 0x1, 0x2, 0x6, 0xe, 0x1e, 0x3e, 0x7e, 0xfe
    };
    static int codeLengths[9] = {
        2,   2,   2,   3,   4,   5,    6,    7,    8
    };
    int cdiff, cdiff_abs;
    int length;

    cdiff = (dc_term - (*pred_term));
    if (cdiff > 255) {
#ifdef BLEAH
        fprintf(stdout, "TRUNCATED\n"); 
#endif
        cdiff = 255;
    } else if (cdiff < -255) {
#ifdef BLEAH
        fprintf(stdout, "TRUNCATED\n"); 
#endif
        cdiff = -255;
    }

    cdiff_abs = ABS(cdiff);
    length = lengths[cdiff_abs];
    Bitio_Write(bb, codes[length], codeLengths[length]);
    if ( length != 0 ) {
        if ( cdiff > 0 ) {
            Bitio_Write(bb, cdiff_abs, length);
        } else {
            Bitio_Write(bb, ~cdiff_abs, length);
        }
    }

    (*pred_term) += cdiff;
}



void
BlockComputeSNR(MpegFrame * const current,
                float *     const snr,
                float *     const psnr) {

    int32    tempInt;
    int y, x;
    int32 varDiff[3];
    double    ratio[3];
    double    total[3];
    uint8 **origY=current->orig_y, **origCr=current->orig_cr, 
        **origCb=current->orig_cb;
    uint8 **newY=current->decoded_y, **newCr=current->decoded_cr, 
        **newCb=current->decoded_cb;
    static int32       **SignalY,  **NoiseY;
    static int32       **SignalCb, **NoiseCb;
    static int32       **SignalCr, **NoiseCr;
    static short   ySize[3], xSize[3];
    static boolean needs_init=TRUE;
  
    /* Init */
    if (needs_init) {
        int ysz = (Fsize_y>>3) * sizeof(int32 *);
        int xsz = (Fsize_x>>3);
    
        needs_init = FALSE;
        for (y=0; y<3; y++) {
            varDiff[y] = ratio[y] = total[y] = 0.0;
        }
        ySize[0]=Fsize_y;     xSize[0]=Fsize_x;
        ySize[1]=Fsize_y>>1;  xSize[1]=Fsize_x>>1;
        ySize[2]=Fsize_y>>1;  xSize[2]=Fsize_x>>1;
        SignalY  = (int32 **) malloc(ysz);
        NoiseY   = (int32 **) malloc(ysz);
        SignalCb = (int32 **) malloc(ysz);
        NoiseCb  = (int32 **) malloc(ysz);
        SignalCr = (int32 **) malloc(ysz);
        NoiseCr  = (int32 **) malloc(ysz);
        if (SignalY == NULL || NoiseY == NULL || SignalCr == NULL || 
            NoiseCb == NULL || SignalCb == NULL || NoiseCr == NULL) {
            fprintf(stderr, "Out of memory in BlockComputeSNR\n");
            exit(-1);
        }
        for (y = 0; y < ySize[0]>>3; y++) {
            SignalY[y]  = (int32 *) calloc(xsz,4);
            SignalCr[y]  = (int32 *) calloc(xsz,4);
            SignalCb[y]  = (int32 *) calloc(xsz,4);
            NoiseY[y]  = (int32 *) calloc(xsz,4);
            NoiseCr[y]  = (int32 *) calloc(xsz,4);
            NoiseCb[y]  = (int32 *) calloc(xsz,4);
        }
    } else {
        for (y = 0; y < ySize[0]>>3; y++) {
            memset((char *) &NoiseY[y][0], 0, (xSize[0]>>3) * 4);
            memset((char *) &SignalY[y][0], 0, (xSize[0]>>3) * 4);
            memset((char *) &NoiseCb[y][0], 0, (xSize[0]>>3) * 4);
            memset((char *) &NoiseCr[y][0], 0, (xSize[0]>>3) * 4);
            memset((char *) &SignalCb[y][0], 0, (xSize[0]>>3) * 4);
            memset((char *) &SignalCr[y][0], 0, (xSize[0]>>3) * 4);
        }
    }
  
    /* find all the signal and noise */
    for (y = 0; y < ySize[0]; y++) {
        for (x = 0; x < xSize[0]; x++) {
            tempInt = (origY[y][x] - newY[y][x]);
            NoiseY[y>>4][x>>4] += tempInt*tempInt;
            total[0] += (double)abs(tempInt);
            tempInt = origY[y][x];
            SignalY[y>>4][x>>4] += tempInt*tempInt;
        }}
    for (y = 0; y < ySize[1]; y++) {
        for (x = 0; x < xSize[1]; x ++) {
            tempInt = (origCb[y][x] - newCb[y][x]);
            NoiseCb[y>>3][x>>3] += tempInt*tempInt;
            total[1] += (double)abs(tempInt);
            tempInt = origCb[y][x];
            SignalCb[y>>3][x>>3] += tempInt*tempInt;
            tempInt = (origCr[y][x]-newCr[y][x]);
            NoiseCr[y>>3][x>>3] += tempInt*tempInt;
            total[2] += (double)abs(tempInt);
            tempInt = origCr[y][x];
            SignalCr[y>>3][x>>3] += tempInt*tempInt;
        }}
  
    /* Now sum up that noise */
    for(y=0; y<Fsize_y>>4; y++){
        for(x=0; x<Fsize_x>>4; x++){
            varDiff[0] += NoiseY[y][x];
            varDiff[1] += NoiseCb[y][x];
            varDiff[2] += NoiseCr[y][x];
            if (printMSE) printf("%4d ",(int)(NoiseY[y][x]/256.0));
        }
        if (printMSE) puts("");
    }
  
    /* Now look at those ratios! */
    for(y=0; y<Fsize_y>>4; y++){
        for(x=0; x<Fsize_x>>4; x++){
            ratio[0] += (double)SignalY[y][x]/(double)varDiff[0];
            ratio[1] += (double)SignalCb[y][x]/(double)varDiff[1];
            ratio[2] += (double)SignalCr[y][x]/(double)varDiff[2];
        }}
  
    for (x=0; x<3; x++) {
        snr[x] = 10.0 * log10(ratio[x]);
        psnr[x] = 20.0 * log10(255.0/sqrt((double)varDiff[x]/
                                          (double)(ySize[x]*xSize[x])));

        if (! realQuiet) {
            fprintf(stdout, "Mean error[%1d]:  %f\n",
                    x, total[x] / (double)(xSize[x] * ySize[x]));
        }

    }
}



void
WriteDecodedFrame(MpegFrame * const frame) {

    FILE * fpointer;
    const char * fileName;
    int    width, height;
    int    y;

    /* need to save decoded frame to disk because it might be accessed
       by another process */

    width = Fsize_x;
    height = Fsize_y;

    pm_asprintf(&fileName, "%s.decoded.%d", outputFileName, frame->id);

    if (!realQuiet) {
        fprintf(stdout, "Outputting to %s\n", fileName);
        fflush(stdout);
    }

    fpointer = fopen(fileName, "wb");

    for ( y = 0; y < height; y++ ) {
        fwrite(frame->decoded_y[y], 1, width, fpointer);
    }

    for (y = 0; y < (height >> 1); y++) {           /* U */
        fwrite(frame->decoded_cb[y], 1, width >> 1, fpointer);
    }

    for (y = 0; y < (height >> 1); y++) {           /* V */
        fwrite(frame->decoded_cr[y], 1, width >> 1, fpointer);
    }
    fflush(fpointer);
    fclose(fpointer);
    pm_strfree(fileName);
}



void
PrintItoIBitRate(int const numBits,
                 int const frameNum) {

    if ( showBitRatePerFrame ) {
        /* ASSUMES 30 FRAMES PER SECOND */

        if (! realQuiet) {
            fprintf(stdout, "I-to-I (frames %5d to %5d) bitrate:  %8d\n",
                    lastIFrame, frameNum-1,
                    ((numBits-lastNumBits)*30)/
                    (frameNum-lastIFrame));
        }

        fprintf(bitRateFile, "I-to-I (frames %5d to %5d) bitrate:  %8d\n",
                lastIFrame, frameNum-1,
                ((numBits-lastNumBits)*30)/
                (frameNum-lastIFrame));
    }
}



/*======================================================================*
 *
 * time_elapsed
 *
 *     Handle different time systems on different machines
 *
 *  RETURNS number of seconds process time used
 *
 *======================================================================*/
int32 time_elapsed(void) {
#ifdef CLOCKS_PER_SEC
    /* ANSI C */
    TIME_RATE = CLOCKS_PER_SEC;
    return (int32) clock();
#else
    struct tms   timeBuffer;
    TIME_RATE = 60;
    times(&timeBuffer);
    return timeBuffer.tms_utime + timeBuffer.tms_stime;
#endif
}