about summary refs log tree commit diff
path: root/converter/ppm/ppmtompeg/mpeg.c
blob: 6557f3779a74b3aff7860c3df6a9daf1c26dcf92 (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
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
/*===========================================================================*
 * mpeg.c
 *
 *  Procedures to generate the MPEG sequence
 *===========================================================================*/

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

#define _BSD_SOURCE   /* Make sure strdup() is in string.h */

#include "all.h"
#include <time.h>
#include <errno.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#ifdef MIPS
#include <sys/types.h>
#endif
#include <sys/stat.h>

#include "nstring.h"
#include "nsleep.h"

#include "mtypes.h"
#include "frames.h"
#include "motion_search.h"
#include "prototypes.h"
#include "parallel.h"
#include "param.h"
#include "readframe.h"
#include "fsize.h"
#include "mheaders.h"
#include "rate.h"
#include "input.h"
#include "frametype.h"
#include "mpeg.h"


/*===========*
 *  VERSION  *
 *===========*/

#define VERSION "1.5b"


/*===========*
 * CONSTANTS *
 *===========*/

#define FPS_30  0x5   /* from MPEG standard sect. 2.4.3.2 */
#define ASPECT_1    0x1 /* aspect ratio, from MPEG standard sect. 2.4.3.2 */


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

static unsigned int framesOutput;
static int      realStart, realEnd;
static int  currentGOP;
static int      timeMask;
static int      numI, numP, numB;
static boolean  frameCountsUnknown;


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

/* important -- don't initialize anything here */
/* must be re-initted anyway in GenMPEGStream */

extern int  IOtime;
extern boolean  resizeFrame;
extern int outputWidth, outputHeight;
int     gopSize = 100;  /* default */
int32       tc_hrs, tc_min, tc_sec, tc_pict, tc_extra;
int     totalFramesSent;
int     yuvWidth, yuvHeight;
int     realWidth, realHeight;
char        currentPath[MAXPATHLEN];
char        statFileName[256];
char        bitRateFileName[256];
time_t      timeStart, timeEnd;
FILE       *statFile;
FILE       *bitRateFile = NULL;
char       *framePattern;
int     framePatternLen;
int     referenceFrame;
int     frameRate = FPS_30;
int     frameRateRounded = 30;
boolean     frameRateInteger = TRUE;
int     aspectRatio = ASPECT_1;
extern char userDataFileName[];
extern int mult_seq_headers;

int32 bit_rate, buf_size;

/*===============================*
 * INTERNAL PROCEDURE prototypes *
 *===============================*/

static void ComputeDHMSTime _ANSI_ARGS_((int32 someTime, char *timeText));
static void OpenBitRateFile _ANSI_ARGS_((void));
static void CloseBitRateFile _ANSI_ARGS_((void));


static void
ShowRemainingTime(boolean const childProcess) {
/*----------------------------------------------------------------------------
   Print out an estimate of the time left to encode
-----------------------------------------------------------------------------*/

    if (childProcess) {
        /* nothing */;
    } else if ( numI + numP + numB == 0 ) {
        /* no time left */
    } else if ( timeMask != 0 ) {   
        /* haven't encoded all types yet */
    } else {
        static int  lastTime = 0;
        float   total;
        time_t  nowTime;
        float   secondsPerFrame;
        
        time(&nowTime);
        secondsPerFrame = (nowTime-timeStart)/(float)framesOutput;
        total = secondsPerFrame*(float)(numI+numP+numB);

        if ((quietTime >= 0) && (!realQuiet) && (!frameCountsUnknown) &&
            ((lastTime < (int)total) || ((lastTime-(int)total) >= quietTime) ||
             (lastTime == 0) || (quietTime == 0))) {
            if (total > 270.0)
                pm_message("ESTIMATED TIME OF COMPLETION:  %d minutes",
                           ((int)total+30)/60);
            else
                pm_message("ESTIMATED TIME OF COMPLETION:  %d seconds",
                           (int)total);
        }
        lastTime = (int)total;
    }
}



static void
initTCTime(unsigned int const firstFrameNumber) {

    unsigned int frameNumber;
    
    tc_hrs = 0; tc_min = 0; tc_sec = 0; tc_pict = 0; tc_extra = 0;
    for (frameNumber = 0; frameNumber < firstFrameNumber; ++frameNumber)
        IncrementTCTime();
}



/*===========================================================================*
 *
 * IncrementTCTime
 *
 *  increment the tc time by one second (and update min, hrs if necessary)
 *  also increments totalFramesSent
 *
 * RETURNS: nothing
 *
 * SIDE EFFECTS:    totalFramesSent, tc_pict, tc_sec, tc_min, tc_hrs, tc_extra
 *
 *===========================================================================*/
void
IncrementTCTime() {
    /* if fps = an integer, then tc_extra = 0 and is ignored

       otherwise, it is the number of extra 1/1001 frames we've passed by

       so far; for example, if fps = 24000/1001, then 24 frames = 24024/24000
       seconds = 1 second + 24/24000 seconds = 1 + 1/1000 seconds; similary,
       if fps = 30000/1001, then 30 frames = 30030/30000 = 1 + 1/1000 seconds
       and if fps = 60000/1001, then 60 frames = 1 + 1/1000 seconds

       if fps = 24000/1001, then 1/1000 seconds = 24/1001 frames
       if fps = 30000/1001, then 1/1000 seconds = 30/1001 frames
       if fps = 60000/1001, then 1/1000 seconds = 60/1001 frames     
     */

    totalFramesSent++;
    tc_pict++;
    if ( tc_pict >= frameRateRounded ) {
        tc_pict = 0;
        tc_sec++;
        if ( tc_sec == 60 ) {
            tc_sec = 0;
            tc_min++;
            if ( tc_min == 60 ) {
                tc_min = 0;
                tc_hrs++;
            }
        }
        if ( ! frameRateInteger ) {
            tc_extra += frameRateRounded;
            if ( tc_extra >= 1001 ) {   /* a frame's worth */
                tc_pict++;
                tc_extra -= 1001;
            }
        }
    }
}



static void
initializeRateControl(bool const wantUnderflowWarning,
                      bool const wantOverflowWarning) {
/*----------------------------------------------------------------------------
   Initialize rate control
-----------------------------------------------------------------------------*/
    int32 const bitstreamMode = getRateMode();

    if (bitstreamMode == FIXED_RATE) {
        initRateControl(wantUnderflowWarning, wantOverflowWarning);
        /*
          SetFrameRate();
        */
    }
}
    


/*===========================================================================*
 *
 * SetReferenceFrameType
 *
 *  set the reference frame type to be original or decoded
 *
 * RETURNS: nothing
 *
 * SIDE EFFECTS:    referenceFrame
 *
 *===========================================================================*/
void
SetReferenceFrameType(const char * const type) {

    if (strcmp(type, "ORIGINAL") == 0)
        referenceFrame = ORIGINAL_FRAME;
    else if ( strcmp(type, "DECODED") == 0 )
        referenceFrame = DECODED_FRAME;
    else
        pm_error("INTERNAL ERROR: Illegal reference frame type: '%s'", type);
}



void
SetBitRateFileName(const char * const fileName) {

    strcpy(bitRateFileName, fileName);
}




static void
finishFrameOutput(MpegFrame * const frameP,
                  BitBucket * const bbP,
                  boolean     const separateFiles,
                  int         const referenceFrame,
                  boolean     const childProcess,
                  boolean     const remoteIO) {

    if ((referenceFrame == DECODED_FRAME) && 
        childProcess && NonLocalRefFrame(frameP->id)) {
        if (remoteIO)
            SendDecodedFrame(frameP);
        else
            WriteDecodedFrame(frameP);
            
        NotifyDecodeServerReady(frameP->id);
    }
    
    if (separateFiles) {
        if (remoteIO)
            SendRemoteFrame(frameP->id, bbP);
        else {
            Bitio_Flush(bbP);
            Bitio_Close(bbP);
        }
    }
}

    


static void
outputIFrame(MpegFrame * const frameP,
             BitBucket * const bbP,
             int         const realStart,
             int         const realEnd,
             MpegFrame * const pastRefFrameP,
             boolean     const separateFiles) {
      
    /* only start a new GOP with I */
    /* don't start GOP if only doing frames */
    if (!separateFiles && currentGOP >= gopSize) {
        boolean const closed = 
            (totalFramesSent == frameP->id || pastRefFrameP == NULL);

        static int num_gop = 0;
    
        /* first, check to see if closed GOP */
    
        /* new GOP */
        if (num_gop != 0 && mult_seq_headers && 
            num_gop % mult_seq_headers == 0) {
            if (!realQuiet) {
                fprintf(stdout, 
                        "Creating new Sequence before GOP %d\n", num_gop);
                fflush(stdout);
            }
      
            Mhead_GenSequenceHeader(
                bbP, Fsize_x, Fsize_y,
                /* pratio */    aspectRatio,
                /* pict_rate */ frameRate, /* bit_rate */ bit_rate,
                /* buf_size */  buf_size,  /* c_param_flag */ 1,
                /* iq_matrix */ customQtable, 
                /* niq_matrix */ customNIQtable,
                /* ext_data */ NULL,  /* ext_data_size */ 0,
                /* user_data */ NULL, /* user_data_size */ 0);
        }
    
        if (!realQuiet)
            pm_message("Creating new GOP (closed = %s) before frame %d\n",
                       closed ? "YES" : "NO", frameP->id);
    
        ++num_gop;
        Mhead_GenGOPHeader(bbP,  /* drop_frame_flag */ 0,
                           tc_hrs, tc_min, tc_sec, tc_pict,
                           closed, /* broken_link */ 0,
                           /* ext_data */ NULL, /* ext_data_size */ 0,
                           /* user_data */ NULL, /* user_data_size */ 0);
        currentGOP -= gopSize;
        if (pastRefFrameP == NULL)
            SetGOPStartTime(0);
        else
            SetGOPStartTime(pastRefFrameP->id + 1);
    }
      
    if (frameP->id >= realStart && frameP->id <= realEnd)
        GenIFrame(bbP, frameP);
      
    --numI;
    timeMask &= 0x6;
      
    ++currentGOP;
    IncrementTCTime();
}



static void
outputPFrame(MpegFrame * const frameP,
             BitBucket * const bbP,
             int         const realStart,
             int         const realEnd,
             MpegFrame * const pastRefFrameP) {

    if ((frameP->id >= realStart) && (frameP->id <= realEnd))
        GenPFrame(bbP, frameP, pastRefFrameP);

    --numP;
    timeMask &= 0x5;
    
    ++currentGOP;
    IncrementTCTime();
}



static BitBucket *
bitioNew(const char * const outputFileName,
         unsigned int const frameNumber,
         boolean      const remoteIO) {

    BitBucket * bbP;

    if (remoteIO)
        bbP = Bitio_New(NULL);
    else {
        const char * fileName;

        asprintfN(&fileName, "%s.frame.%d", outputFileName, frameNumber);

        bbP = Bitio_New_Filename(fileName);

        strfree(fileName);
    }
    return bbP;
}



static void
getBFrame(int                  const frameNum,
          struct inputSource * const inputSourceP,
          MpegFrame *          const pastRefFrameP,
          boolean              const childProcess,
          boolean              const remoteIO,
          MpegFrame **         const bFramePP,
          int *                const IOtimeP,
          unsigned int *       const framesReadP) {
/*----------------------------------------------------------------------------
   Get Frame 'frameNum', which is a B frame related to previous reference
   frame 'pastRefFrameP'.  Return it as *bFramePP.

   We have various ways of getting the frame, corresponding to the
   multitude of modes in which Ppmtompeg works.
-----------------------------------------------------------------------------*/
    if (!inputSourceP->stdinUsed) {
        time_t tempTimeStart, tempTimeEnd;
        MpegFrame * bFrameP;
        bool endOfStream;

        bFrameP = Frame_New(frameNum, 'b');

        time(&tempTimeStart);

        ReadNthFrame(inputSourceP, frameNum, remoteIO, childProcess,
                     separateConversion, slaveConversion, inputConversion,
                     bFrameP, &endOfStream);

        assert(!endOfStream);  /* Because it's not a stream */

        time(&tempTimeEnd);
        *IOtimeP += (tempTimeEnd-tempTimeStart);

        ++(*framesReadP);
        *bFramePP = bFrameP;
    } else {
        /* As the frame input is serial, we can't read the B frame now.
           Rather, Caller has already read it and chained it to 
           the previous reference frame.  So we get that copy now.
        */
        *bFramePP = pastRefFrameP->next;
        pastRefFrameP->next = (*bFramePP)->next;  /* unlink from list */
    }
}



static void
processBFrames(MpegFrame *          const pastRefFrameP,
               MpegFrame *          const futureRefFrameP,
               int                  const realStart,
               int                  const realEnd,
               struct inputSource * const inputSourceP,
               boolean              const remoteIo,
               boolean              const childProcess,
               int *                const IOtimeP,
               BitBucket *          const wholeStreamBbP,
               const char *         const outputFileName,
               unsigned int *       const framesReadP,
               unsigned int *       const framesOutputP,
               int *                const currentGopP) {
/*----------------------------------------------------------------------------
   Process the B frames that go between 'pastRefFrameP' and
   'futureRefFrame' in the movie (but go after 'futureRefFrameP' in the
   MPEG stream, so reader doesn't have to read ahead).

   Remember that a B frame is one which is described by data in the
   MPEG stream that describes the frame with respect to a frame somewhere
   before it, and a frame somewhere after it (i.e. reference frames).

   But do only those B frames whose frame numbers are within the range
   'realStart' through 'realEnd'.

   Output the frames to the output stream 'wholeStreamBbP'.  If NULL,
   output each frame to its own individual file instead.
-----------------------------------------------------------------------------*/
    boolean const separateFiles = (wholeStreamBbP == NULL);
    unsigned int const firstBFrameNum = pastRefFrameP->id + 1;

    int frameNum;

    assert(pastRefFrameP != NULL);
    assert(futureRefFrameP != NULL);
    
    for (frameNum = MAX(realStart, firstBFrameNum); 
         frameNum < MIN(realEnd, futureRefFrameP->id); 
         ++frameNum) {

        MpegFrame * bFrame;
        BitBucket * bbP;

        getBFrame(frameNum, inputSourceP, pastRefFrameP, childProcess, 
                  remoteIO,
                  &bFrame, IOtimeP, framesReadP);

        if (separateFiles)
            bbP = bitioNew(outputFileName, bFrame->id, remoteIO);
        else
            bbP = wholeStreamBbP;

        GenBFrame(bbP, bFrame, pastRefFrameP, futureRefFrameP);
        ++(*framesOutputP);

        if (separateFiles) {
            if (remoteIO)
                SendRemoteFrame(bFrame->id, bbP);
            else {
                Bitio_Flush(bbP);
                Bitio_Close(bbP);
            }
        }

        /* free this B frame right away */
        Frame_Free(bFrame);

        numB--;
        timeMask &= 0x3;
        ShowRemainingTime(childProcess);

        ++(*currentGopP);
        IncrementTCTime();
    }
}



static void
processRefFrame(MpegFrame *    const frameP, 
                BitBucket *    const wholeStreamBbP,
                int            const realStart,
                int            const realEnd,
                MpegFrame *    const pastRefFrameP,
                boolean        const childProcess,
                const char *   const outputFileName,
                unsigned int * const framesReadP,
                unsigned int * const framesOutputP) {
/*----------------------------------------------------------------------------
   Process an I or P frame.  Encode and output it.

   But only if its frame number is within the range 'realStart'
   through 'realEnd'.

   Output the frame to the output stream 'wholeStreamBbP'.  If NULL,
   output it to its own individual file instead.
-----------------------------------------------------------------------------*/
    if (frameP->id >= realStart && frameP->id <= realEnd) {
        bool const separateFiles = (wholeStreamBbP == NULL);
  
        BitBucket * bbP;
  
        if (separateFiles)
            bbP = bitioNew(outputFileName, frameP->id, remoteIO);
        else
            bbP = wholeStreamBbP;
  
        /* first, output this reference frame */
        switch (frameP->type) {
        case TYPE_IFRAME:
            outputIFrame(frameP, bbP, realStart, realEnd, pastRefFrameP, 
                         separateFiles);
            break;
        case TYPE_PFRAME:
            outputPFrame(frameP, bbP, realStart, realEnd, pastRefFrameP);
            ShowRemainingTime(childProcess);
            break;
        default:
            pm_error("INTERNAL ERROR: non-reference frame passed to "
                     "ProcessRefFrame()");
        }  
        
        ++(*framesOutputP);
        
        finishFrameOutput(frameP, bbP, separateFiles, referenceFrame,
                          childProcess, remoteIO);
    }
}



static void
countFrames(unsigned int const firstFrame,
            unsigned int const lastFrame,
            boolean      const stdinUsed,
            int *        const numIP,
            int *        const numPP,
            int *        const numBP,
            int *        const timeMaskP,
            boolean *    const frameCountsUnknownP) {
/*----------------------------------------------------------------------------
  Count number of I, P, and B frames
-----------------------------------------------------------------------------*/
    unsigned int numI, numP, numB;
    unsigned int timeMask;
            
    numI = 0; numP = 0; numB = 0;
    timeMask = 0;
    if (stdinUsed) {
        numI = numP = numB = MAXINT/4;
        *frameCountsUnknownP = TRUE;
    } else {
        unsigned int i;
        for (i = firstFrame; i <= lastFrame; ++i) {
            char const frameType = FType_Type(i);
            switch(frameType) {
            case 'i':        numI++;            timeMask |= 0x1;    break;
            case 'p':        numP++;            timeMask |= 0x2;    break;
            case 'b':        numB++;            timeMask |= 0x4;    break;
            }
        }
        *frameCountsUnknownP = FALSE;
    }

    *numIP     = numI;
    *numPP     = numP;
    *numBP     = numB;
    *timeMaskP = timeMask;
    *frameCountsUnknownP = frameCountsUnknown;
}



static void
readAndSaveFrame(struct inputSource * const inputSourceP,
                 unsigned int         const frameNumber,
                 char                 const frameType,
                 const char *         const inputConversion,
                 MpegFrame *          const pastRefFrameP,
                 unsigned int *       const framesReadP,
                 int *                const ioTimeP,
                 bool *               const endOfStreamP) {
/*----------------------------------------------------------------------------
   Read the next frame from Standard Input and add it to the linked list
   at *pastRefFrameP.  Assume it is Frame Number 'frameNumber' and is of
   type 'frameType'.

   Increment *framesReadP.
   
   Add the time it took to read it, in seconds, to *iotimeP.

   Iff we can't read because we hit end of file, return
   *endOfStreamP == TRUE and *framesReadP and *iotimeP untouched.
-----------------------------------------------------------------------------*/
    /* This really should be part of ReadNthFrame.  The frame should be chained
       to the input object, not the past reference frame.
    */
       
    MpegFrame * p;
    MpegFrame * frameP;
    time_t ioTimeStart, ioTimeEnd;
    
    time(&ioTimeStart);

    frameP = Frame_New(frameNumber, frameType);
    ReadFrame(frameP, inputSourceP, frameNumber, inputConversion,
              endOfStreamP);

    if (*endOfStreamP)
        Frame_Free(frameP);
    else {
        ++(*framesReadP);
    
        time(&ioTimeEnd);
        *ioTimeP += (ioTimeEnd - ioTimeStart);

        /* Add the B frame to the end of the queue of B-frames 
           for later encoding.
        */
        assert(pastRefFrameP != NULL);
        
        p = pastRefFrameP;
        while (p->next != NULL)
            p = p->next;
        p->next = frameP;
    }
}



static void
doFirstFrameStuff(enum frameContext const context,
                  const char *      const userDataFileName,
                  BitBucket *       const bbP,
                  int               const fsize_x,
                  int               const fsize_y,
                  int               const aspectRatio,
                  int               const frameRate,
                  int32             const qtable[],
                  int32             const niqtable[],
                  unsigned int *    const inputFrameBitsP) {
/*----------------------------------------------------------------------------
   Do stuff we have to do after reading the first frame in a sequence
   of frames requested of GenMPEGStream().

   *bbP is the output stream to which to write any header stuff we have
   to write.  If 'context' is such that there is no header stuff to write,
   then 'bbP' is irrelevant.
-----------------------------------------------------------------------------*/
    *inputFrameBitsP = 24 * Fsize_x * Fsize_y;
    SetBlocksPerSlice();
          
    if (context == CONTEXT_WHOLESTREAM) {
        int32 const bitstreamMode = getRateMode();
        char * userData;
        unsigned int userDataSize;

        assert(bbP != NULL);

        DBG_PRINT(("Generating sequence header\n"));
        if (bitstreamMode == FIXED_RATE) {
            bit_rate = getBitRate();
            buf_size = getBufferSize();
        } else {
            bit_rate = -1;
            buf_size = -1;
        }
        
        if (strlen(userDataFileName) != 0) {
            struct stat statbuf;
            FILE *fp;
          
            stat(userDataFileName,&statbuf);
            userDataSize = statbuf.st_size;
            userData = malloc(userDataSize);
            fp = fopen(userDataFileName,"rb");
            if (fp == NULL) {
                pm_message("Could not open userdata file '%s'.",
                           userDataFileName);
                userData = NULL;
                userDataSize = 0;
            } else {
                size_t bytesRead;

                bytesRead = fread(userData,1,userDataSize,fp);
                if (bytesRead != userDataSize) {
                    pm_message("Could not read %d bytes from "
                               "userdata file '%s'.",
                               userDataSize,userDataFileName);
                    userData = NULL;
                    userDataSize = 0;
                }
            }
        } else { /* Put in our UserData Header */
            const char * userDataString;
            time_t now;
                    
            time(&now);
            asprintfN(&userDataString,"MPEG stream encoded by UCB Encoder "
                      "(mpeg_encode) v%s on %s.",
                      VERSION, ctime(&now));
            userData = strdup(userDataString);
            userDataSize = strlen(userData);
            strfree(userDataString);
        }
        Mhead_GenSequenceHeader(bbP, Fsize_x, Fsize_y,
                                /* pratio */ aspectRatio,
                                /* pict_rate */ frameRate, 
                                /* bit_rate */ bit_rate,
                                /* buf_size */ buf_size,
                                /*c_param_flag */ 1,
                                /* iq_matrix */ qtable, 
                                /* niq_matrix */ niqtable,
                                /* ext_data */ NULL,
                                /* ext_data_size */ 0,
                                /* user_data */ (uint8*) userData,
                                /* user_data_size */ userDataSize);
    }
}



static void
getPreviousFrame(unsigned int         const frameStart,
                 int                  const referenceFrame,
                 struct inputSource * const inputSourceP,
                 boolean              const childProcess,
                 const char *         const slaveConversion,
                 const char *         const inputConversion,
                 MpegFrame **         const framePP,
                 unsigned int *       const framesReadP,
                 int *                const ioTimeP) {

    /* This needs to be modularized.  It shouldn't issue messages about
       encoding GOPs and B frames, since it knows nothing about those.
       It should work for Standard Input too, through a generic Standard
       Input reader that buffers stuff for backward reading.
    */

    MpegFrame * frameP;
    time_t ioTimeStart, ioTimeEnd;

    /* can't find the previous frame interactively */
    if (inputSourceP->stdinUsed)
        pm_error("Cannot encode GOP from stdin when "
                 "first frame is a B-frame.");

    if (frameStart < 1)
        pm_error("Cannot encode GOP when first frame is a B-frame "
                 "and is not preceded by anything.");

    /* need to load in previous frame; call it an I frame */
    frameP = Frame_New(frameStart-1, 'i');

    time(&ioTimeStart);

    if ((referenceFrame == DECODED_FRAME) && childProcess) {
        WaitForDecodedFrame(frameStart);

        if (remoteIO)
            GetRemoteDecodedRefFrame(frameP, frameStart - 1);
        else
            ReadDecodedRefFrame(frameP, frameStart - 1);
    } else {
        bool endOfStream;
        ReadNthFrame(inputSourceP, frameStart - 1, remoteIO, childProcess,
                     separateConversion, slaveConversion, inputConversion,
                     frameP, &endOfStream);
        assert(!endOfStream);  /* Because Stdin causes failure above */
    }            
    ++(*framesReadP);
    
    time(&ioTimeEnd);
    *ioTimeP += (ioTimeEnd-ioTimeStart);

    *framePP = frameP;
}



static void
computeFrameRange(unsigned int         const frameStart,
                  unsigned int         const frameEnd,
                  enum frameContext    const context, 
                  struct inputSource * const inputSourceP,
                  unsigned int *       const firstFrameP,
                  unsigned int *       const lastFrameP) {

    switch (context) {
    case CONTEXT_GOP:
        *firstFrameP = frameStart;
        *lastFrameP  = frameEnd;
        break;
    case CONTEXT_JUSTFRAMES: {
        /* if first frame is P or B, need to read in P or I frame before it */
        if (FType_Type(frameStart) != 'i') {
            /* can't find the previous frame interactively */
            if (inputSourceP->stdinUsed)
                pm_error("Cannot encode frames from Standard Input "
                         "when first frame is not an I-frame.");

            *firstFrameP = FType_PastRef(frameStart);
        } else
            *firstFrameP = frameStart;

        /* if last frame is B, need to read in P or I frame after it */
        if ((FType_Type(frameEnd) == 'b') && 
            (frameEnd != inputSourceP->numInputFiles-1)) {
            /* can't find the next reference frame interactively */
            if (inputSourceP->stdinUsed)
                pm_error("Cannot encode frames from Standard Input "
                         "when last frame is a B-frame.");
            
            *lastFrameP = FType_FutureRef(frameEnd);
        } else
            *lastFrameP = frameEnd;
    }
    break;
    case CONTEXT_WHOLESTREAM:
        *firstFrameP = frameStart;
        *lastFrameP  = frameEnd;
    }
}



static void
getFrame(MpegFrame **         const framePP,
         struct inputSource * const inputSourceP,
         unsigned int         const frameNumber,
         char                 const frameType,
         unsigned int         const realStart,
         unsigned int         const realEnd,
         int                  const referenceFrame,
         boolean              const childProcess,
         boolean              const remoteIo,
         boolean              const separateConversion,
         const char *         const slaveConversion,
         const char *         const inputConversion,
         unsigned int *       const framesReadP,
         int *                const ioTimeP) {
/*----------------------------------------------------------------------------
   Get frame with number 'frameNumber' as *frameP.

   Increment *framesReadP.

   Add to *ioTimeP the time in seconds we spent reading it.

   Iff we fail to get the frame because the stream ends, return
   *frameP == NULL, don't increment *framesReadP, and leave
   *ioTimeP unchanged.
-----------------------------------------------------------------------------*/
    time_t ioTimeStart, ioTimeEnd;
    MpegFrame * frameP;
    bool endOfStream;
    
    time(&ioTimeStart);

    frameP = Frame_New(frameNumber, frameType);
            
    if ((referenceFrame == DECODED_FRAME) &&
        ((frameNumber < realStart) || (frameNumber > realEnd)) ) {
        WaitForDecodedFrame(frameNumber);

        if (remoteIo)
            GetRemoteDecodedRefFrame(frameP, frameNumber);
        else
            ReadDecodedRefFrame(frameP, frameNumber);

        /* I don't know what this block of code does, so I don't know
           what endOfStream should be.  Here's a guess:
        */
        endOfStream = FALSE;
    } else
        ReadNthFrame(inputSourceP, frameNumber, remoteIO, childProcess,
                     separateConversion, slaveConversion, inputConversion,
                     frameP, &endOfStream);
    
    if (endOfStream) {
        Frame_Free(frameP);
        *framePP = NULL;
    } else {
        ++(*framesReadP);
            
        time(&ioTimeEnd);
        *ioTimeP += (ioTimeEnd - ioTimeStart);

        *framePP = frameP;
    }
}



static void
handleBitRate(unsigned int const realEnd,
              unsigned int const numBits,
              boolean      const childProcess,
              boolean      const showBitRatePerFrame) {

    extern void PrintItoIBitRate (int numBits, int frameNum);

    if (FType_Type(realEnd) != 'i')
        PrintItoIBitRate(numBits, realEnd+1);

    if ((!childProcess) && showBitRatePerFrame)
        CloseBitRateFile();
}



static void
doAFrame(unsigned int         const frameNumber,
         struct inputSource * const inputSourceP,
         enum frameContext    const context, 
         unsigned int         const frameStart, 
         unsigned int         const frameEnd, 
         unsigned int         const realStart,
         unsigned int         const realEnd,
         bool                 const childProcess,
         const char *         const outputFileName,
         MpegFrame *          const pastRefFrameP,
         MpegFrame **         const newPastRefFramePP,
         unsigned int *       const framesReadP,
         unsigned int *       const framesOutputP,
         bool *               const firstFrameDoneP,
         BitBucket *          const wholeStreamBbP,
         unsigned int *       const inputFrameBitsP,
         bool *               const endOfStreamP) {
/*----------------------------------------------------------------------------
   *endOfStreamP returned means we were unable to do a frame because
   the input stream has ended.  In that case, none of the other outputs
   are valid.

   Process an input frame.  This can involve writing its description
   to the output stream, saving it for later use, and/or writing
   descriptions of previously saved frames to the output stream
   because we now have enough information to do so.

   Output the frames to the output stream 'wholeStreamBbP'.  If NULL,
   output each frame to its own individual file instead.
-----------------------------------------------------------------------------*/
    char const frameType = FType_Type(frameNumber);
    
    *endOfStreamP = FALSE;  /* initial assumption */

    if (frameType == 'b') {
        /* We'll process this non-reference frame later.  If reading
           from stdin, we read it now and save it.  Otherwise, we can
           just read it later.
        */
        *newPastRefFramePP = pastRefFrameP;
        if (inputSourceP->stdinUsed) 
            readAndSaveFrame(inputSourceP,
                             frameNumber, frameType, inputConversion,
                             pastRefFrameP, framesReadP, &IOtime,
                             endOfStreamP);
    } else {
        MpegFrame * frameP;
        
        getFrame(&frameP, inputSourceP, frameNumber, frameType,
                 realStart, realEnd, referenceFrame, childProcess,
                 remoteIO,
                 separateConversion, slaveConversion, inputConversion,
                 framesReadP, &IOtime);
        
        if (frameP) {
            *endOfStreamP = FALSE;

            if (!*firstFrameDoneP) {
                doFirstFrameStuff(context, userDataFileName, wholeStreamBbP,
                                  Fsize_x, Fsize_y, aspectRatio,
                                  frameRate, qtable, niqtable, 
                                  inputFrameBitsP);
            
                *firstFrameDoneP = TRUE;
            }
            processRefFrame(frameP, wholeStreamBbP, frameStart, frameEnd,
                            pastRefFrameP, childProcess, outputFileName, 
                            framesReadP, framesOutputP);
                
            if (pastRefFrameP) {
                processBFrames(pastRefFrameP, frameP, realStart, realEnd,
                               inputSourceP, remoteIO, childProcess, 
                               &IOtime, wholeStreamBbP, outputFileName,
                               framesReadP, framesOutputP, &currentGOP);
            }
            if (pastRefFrameP != NULL)
                Frame_Free(pastRefFrameP);
        
            *newPastRefFramePP = frameP;
        } else
            *endOfStreamP = TRUE;
    }
}



void
GenMPEGStream(struct inputSource * const inputSourceP,
              enum frameContext    const context, 
              unsigned int         const frameStart, 
              unsigned int         const frameEnd, 
              int32                const qtable[], 
              int32                const niqtable[], 
              bool                 const childProcess,
              FILE *               const ofP, 
              const char *         const outputFileName,
              bool                 const wantVbvUnderflowWarning,
              bool                 const wantVbvOverflowWarning,
              unsigned int *       const inputFrameBitsP,
              unsigned int *       const totalBitsP) {
/*----------------------------------------------------------------------------
   Encode a bunch of frames into an MPEG sequence stream or a part thereof.

   'context' tells what in addition to the frames themselves must go into
   the stream:

      CONTEXT_JUSTFRAMES:  Nothing but the indicated frames
      CONTEXT_GOP:         GOP header/trailer stuff to make a single GOP
                           that contains the indicated frames
      CONTEXT_WHOLESTREAM: A whole stream consisting of the indicated
                           frames -- a sequence of whole GOPS, with stream
                           header/trailer stuff as well.

   'frameStart' and 'frameEnd' are the numbers of the first and last
   frames we are to encode, except that if the input source is a stream,
   we stop where the stream ends if that is before 'frameEnd'.

-----------------------------------------------------------------------------*/
    BitBucket * streamBbP;
        /* The output stream to which we write all the frames.  NULL
           means the frames are going to individual frame files.
        */
    unsigned int frameNumber;
    bool endOfStream;
    bool firstFrameDone;
    int numBits;
    unsigned int firstFrame, lastFrame;
        /* Frame numbers of the first and last frames we look at.
           This could be more than the the frames we actually encode
           because we may need context (i.e. to encode a B frame, we
           need the subsequent I or P frame).
        */
    unsigned int framesRead;
        /* Number of frames we have read; for statistical purposes */
    MpegFrame * pastRefFrameP;
        /* The frame that will be the past reference frame for the next
           B or P frame that we put into the stream
        */
    if (frameEnd + 1 > inputSourceP->numInputFiles)
        pm_error("Last frame (number %u) is beyond the end of the stream "
                 "(%u frames)", frameEnd, inputSourceP->numInputFiles);

    if (context == CONTEXT_WHOLESTREAM &&
        !inputSourceP->stdinUsed && 
        FType_Type(inputSourceP->numInputFiles-1) == 'b')
        pm_message("WARNING:  "
                   "One or more B-frames at end will not be encoded.  "
                   "See FORCE_ENCODE_LAST_FRAME parameter file statement.");

    time(&timeStart);

    framesRead = 0;

    ResetIFrameStats();
    ResetPFrameStats();
    ResetBFrameStats();

    Fsize_Reset();

    framesOutput = 0;

    if (childProcess && separateConversion)
        SetFileType(slaveConversion);
    else
        SetFileType(inputConversion);

    realStart = frameStart;
    realEnd   = frameEnd;

    computeFrameRange(frameStart, frameEnd, context, inputSourceP,
                      &firstFrame, &lastFrame);

    if (context == CONTEXT_GOP && FType_Type(frameStart) == 'b')
        getPreviousFrame(frameStart, referenceFrame, inputSourceP,
                         childProcess, slaveConversion, inputConversion,
                         &pastRefFrameP, &framesRead, &IOtime);
    else
        pastRefFrameP = NULL;

    countFrames(firstFrame, lastFrame, inputSourceP->stdinUsed,
                &numI, &numP, &numB, &timeMask, &frameCountsUnknown);

    if (showBitRatePerFrame)
        OpenBitRateFile();  /* May modify showBitRatePerFrame */

    if (context == CONTEXT_JUSTFRAMES)
        streamBbP = NULL;
    else
        streamBbP = Bitio_New(ofP);

    initTCTime(firstFrame);

    totalFramesSent = firstFrame;
    currentGOP = gopSize;        /* so first I-frame generates GOP Header */

    initializeRateControl(wantVbvUnderflowWarning, wantVbvOverflowWarning);

    firstFrameDone = FALSE;
    for (frameNumber = firstFrame, endOfStream = FALSE;
         frameNumber <= lastFrame && !endOfStream;
         ++frameNumber) {

        doAFrame(frameNumber, inputSourceP, context, 
                 frameStart, frameEnd, realStart, realEnd,
                 childProcess, outputFileName,
                 pastRefFrameP, &pastRefFrameP,
                 &framesRead, &framesOutput, &firstFrameDone, streamBbP,
                 inputFrameBitsP, &endOfStream);
    }
    
    if (pastRefFrameP != NULL)
        Frame_Free(pastRefFrameP);
    
    /* SEQUENCE END CODE */
    if (context == CONTEXT_WHOLESTREAM)
        Mhead_GenSequenceEnder(streamBbP);
    
    if (streamBbP)
        numBits = streamBbP->cumulativeBits;
    else {
        /* What should the correct value be?  Most likely 1.  "numBits" is
           used below, so we need to make sure it's properly initialized 
           to somthing (anything).  
        */
        numBits = 1;
    }

    if (streamBbP) {
        Bitio_Flush(streamBbP);
        fclose(ofP);
    }
    handleBitRate(realEnd, numBits, childProcess, showBitRatePerFrame);

    *totalBitsP  = numBits;
}



/*===========================================================================*
 *
 * SetStatFileName
 *
 *  set the statistics file name
 *
 * RETURNS: nothing
 *
 * SIDE EFFECTS:    statFileName
 *
 *===========================================================================*/
void
SetStatFileName(const char * const fileName) {
    strcpy(statFileName, fileName);
}


/*===========================================================================*
 *
 * SetGOPSize
 *
 *  set the GOP size (frames per GOP)
 *
 * RETURNS: nothing
 *
 * SIDE EFFECTS:    gopSize
 *
 *===========================================================================*/
void
SetGOPSize(size)
    int size;
{
    gopSize = size;
}


/*===========================================================================*
 *
 * PrintStartStats
 *
 *  print out the starting statistics (stuff from the param file)
 *
 * RETURNS: nothing
 *
 * SIDE EFFECTS:    none
 *
 *===========================================================================*/
void
PrintStartStats(time_t               const startTime, 
                bool                 const specificFrames,
                unsigned int         const firstFrame, 
                unsigned int         const lastFrame,
                struct inputSource * const inputSourceP) {

    FILE *fpointer;
    int i;

    if (statFileName[0] == '\0') {
        statFile = NULL;
    } else {
        statFile = fopen(statFileName, "a");    /* open for appending */
        if (statFile == NULL) {
            fprintf(stderr, "ERROR:  Could not open stat file:  %s\n",
                    statFileName);
            fprintf(stderr, "        Sending statistics to stdout only.\n");
            fprintf(stderr, "\n\n");
        } else if (! realQuiet) {
            fprintf(stdout, "Appending statistics to file:  %s\n",
                    statFileName);
            fprintf(stdout, "\n\n");
        }
    }
    
    for (i = 0; i < 2; ++i) {
        if ( ( i == 0 ) && (! realQuiet) ) {
            fpointer = stdout;
        } else if ( statFile != NULL ) {
            fpointer = statFile;
        } else {
            continue;
        }

        fprintf(fpointer, "MPEG ENCODER STATS (%s)\n",VERSION);
        fprintf(fpointer, "------------------------\n");
        fprintf(fpointer, "TIME STARTED:  %s", ctime(&startTime));
        if (getenv("HOST") != NULL)
            fprintf(fpointer, "MACHINE:  %s\n", getenv("HOST"));
        else
            fprintf(fpointer, "MACHINE:  unknown\n");

        if (inputSourceP->stdinUsed)
            fprintf(fpointer, "INPUT:  stdin\n");
        else {
            const char * inputFileName;

            fprintf(fpointer, "INPUT FROM FILES:\n");

            GetNthInputFileName(inputSourceP, 0, &inputFileName);
            fprintf(fpointer, "FIRST FILE:  %s/%s\n", 
                    currentPath, inputFileName);
            strfree(inputFileName);
            GetNthInputFileName(inputSourceP, inputSourceP->numInputFiles-1, 
                                &inputFileName);
            fprintf(fpointer, "LAST FILE:  %s/%s\n", 
                    currentPath, inputFileName);
            strfree(inputFileName);
        }    
        fprintf(fpointer, "OUTPUT:  %s\n", outputFileName);

        if (resizeFrame)
            fprintf(fpointer, "RESIZED TO:  %dx%d\n",
                    outputWidth, outputHeight);
        fprintf(fpointer, "PATTERN:  %s\n", framePattern);
        fprintf(fpointer, "GOP_SIZE:  %d\n", gopSize);
        fprintf(fpointer, "SLICES PER FRAME:  %d\n", slicesPerFrame);
        if (searchRangeP==searchRangeB)
            fprintf(fpointer, "RANGE:  +/-%d\n", searchRangeP/2);
        else fprintf(fpointer, "RANGES:  +/-%d %d\n", 
                     searchRangeP/2,searchRangeB/2);
        fprintf(fpointer, "PIXEL SEARCH:  %s\n", 
                pixelFullSearch ? "FULL" : "HALF");
        fprintf(fpointer, "PSEARCH:  %s\n", PSearchName());
        fprintf(fpointer, "BSEARCH:  %s\n", BSearchName());
        fprintf(fpointer, "QSCALE:  %d %d %d\n", qscaleI, 
                GetPQScale(), GetBQScale());
        if (specificsOn) 
            fprintf(fpointer, "(Except as modified by Specifics file)\n");
        if ( referenceFrame == DECODED_FRAME ) {
            fprintf(fpointer, "REFERENCE FRAME:  DECODED\n");
        } else if ( referenceFrame == ORIGINAL_FRAME ) {
            fprintf(fpointer, "REFERENCE FRAME:  ORIGINAL\n");
        } else
            pm_error("Illegal referenceFrame!!!");

        /*  For new Rate control parameters */
        if (getRateMode() == FIXED_RATE) {
            fprintf(fpointer, "PICTURE RATE:  %d\n", frameRateRounded);
            if (getBitRate() != -1) {
                fprintf(fpointer, "\nBIT RATE:  %d\n", getBitRate());
            }
            if (getBufferSize() != -1) {
                fprintf(fpointer, "BUFFER SIZE:  %d\n", getBufferSize());
            }
        }
    }
    if (!realQuiet)
        fprintf(stdout, "\n\n");
}



boolean
NonLocalRefFrame(int const id) {
/*----------------------------------------------------------------------------
   Return TRUE if frame number 'id' might be referenced from a non-local
   process.  This is a conservative estimate.  We return FALSE iff there
   is no way based on the information we have that the frame could be
   referenced by a non-local process.
-----------------------------------------------------------------------------*/
    boolean retval;

    int const lastIPid = FType_PastRef(id);
    
    /* might be accessed by B-frame */
    
    if (lastIPid+1 < realStart)
        retval = TRUE;
    else {
        unsigned int const nextIPid = FType_FutureRef(id);
        
        /* if B-frame is out of range, then current frame can be
           ref'd by it 
        */
        
        /* might be accessed by B-frame */
        if (nextIPid > realEnd+1)
            retval = TRUE;
        
        /* might be accessed by P-frame */
        if ((nextIPid > realEnd) && (FType_Type(nextIPid) == 'p'))
            retval = TRUE;
    }
    return retval;
}


 
/*===========================================================================*
 *
 * SetFrameRate
 *
 *  sets global frame rate variables.  value passed is MPEG frame rate code.
 *
 * RETURNS: TRUE or FALSE
 *
 * SIDE EFFECTS:    frameRateRounded, frameRateInteger
 *
 *===========================================================================*/
void
SetFrameRate()
{
    switch(frameRate) {
    case 1:
        frameRateRounded = 24;
        frameRateInteger = FALSE;
        break;
    case 2:
        frameRateRounded = 24;
        frameRateInteger = TRUE;
        break;
    case 3:
        frameRateRounded = 25;
        frameRateInteger = TRUE;
        break;
    case 4:
        frameRateRounded = 30;
        frameRateInteger = FALSE;
        break;
    case 5:
        frameRateRounded = 30;
        frameRateInteger = TRUE;
        break;
    case 6:
        frameRateRounded = 50;
        frameRateInteger = TRUE;
        break;
    case 7:
        frameRateRounded = 60;
        frameRateInteger = FALSE;
        break;
    case 8:
        frameRateRounded = 60;
        frameRateInteger = TRUE;
        break;
    }
    printf("frame rate(%d) set to %d\n", frameRate, frameRateRounded);
}


/*=====================*
 * INTERNAL PROCEDURES *
 *=====================*/

/*===========================================================================*
 *
 * ComputeDHMSTime
 *
 *  turn some number of seconds (someTime) into a string which
 *  summarizes that time according to scale (days, hours, minutes, or
 *  seconds)
 *
 * RETURNS: nothing
 *
 * SIDE EFFECTS:    none
 *
 *===========================================================================*/
static void
ComputeDHMSTime(someTime, timeText)
    int32 someTime;
    char *timeText;
{
    int     days, hours, mins, secs;

    days = someTime / (24*60*60);
    someTime -= days*24*60*60;
    hours = someTime / (60*60);
    someTime -= hours*60*60;
    mins = someTime / 60;
    secs = someTime - mins*60;

    if ( days > 0 ) {
        sprintf(timeText, "Total time:  %d days and %d hours", days, hours);
    } else if ( hours > 0 ) {
        sprintf(timeText, "Total time:  %d hours and %d minutes", hours, mins);
    } else if ( mins > 0 ) {
        sprintf(timeText, "Total time:  %d minutes and %d seconds", mins, secs);
    } else {
    sprintf(timeText, "Total time:  %d seconds", secs);
    }
}



void
ComputeGOPFrames(int            const whichGOP, 
                 unsigned int * const firstFrameP, 
                 unsigned int * const lastFrameP, 
                 unsigned int   const numFrames) {
/*----------------------------------------------------------------------------
   Figure out which frames are in GOP number 'whichGOP'.
-----------------------------------------------------------------------------*/
    unsigned int passedB;
    unsigned int currGOP;
    unsigned int gopNum;
    unsigned int frameNum;
    unsigned int firstFrame, lastFrame;
    bool foundGop;

    /* calculate first, last frames of whichGOP GOP */

    gopNum = 0;
    frameNum = 0;
    passedB = 0;
    currGOP = 0;
    foundGop = FALSE;

    while (!foundGop) {
        if (frameNum >= numFrames)
            pm_error("There aren't that many GOPs!");

        if (gopNum == whichGOP) {
            foundGop = TRUE;
            firstFrame = frameNum;
        }           

        /* go past one gop */
        /* must go past at least one frame */
        do {
            currGOP += (1 + passedB);

            ++frameNum;

            passedB = 0;
            while ((frameNum < numFrames) && (FType_Type(frameNum) == 'b')) {
                ++frameNum;
                ++passedB;
            }
        } while ((frameNum < numFrames) && 
                 ((FType_Type(frameNum) != 'i') || (currGOP < gopSize)));

        currGOP -= gopSize;

        if (gopNum == whichGOP)
            lastFrame = (frameNum - passedB - 1);

        ++gopNum;
    }
    *firstFrameP = firstFrame;
    *lastFrameP  = lastFrame;
}



static void
doEndStats(FILE *       const fpointer,
           time_t       const startTime,
           time_t       const endTime,
           unsigned int const inputFrameBits,
           unsigned int const totalBits,
           float        const totalCPU) {

    int32 const diffTime = endTime - startTime;

    char    timeText[256];

    ComputeDHMSTime(diffTime, timeText);

    fprintf(fpointer, "TIME COMPLETED:  %s", ctime(&endTime));
    fprintf(fpointer, "%s\n\n", timeText);
        
    ShowIFrameSummary(inputFrameBits, totalBits, fpointer);
    ShowPFrameSummary(inputFrameBits, totalBits, fpointer);
    ShowBFrameSummary(inputFrameBits, totalBits, fpointer);

    fprintf(fpointer, "---------------------------------------------\n");
    fprintf(fpointer, "Total Compression:  %3d:1     (%9.4f bpp)\n",
            framesOutput*inputFrameBits/totalBits,
            24.0*(float)(totalBits)/(float)(framesOutput*inputFrameBits));
    if (diffTime > 0) {
        fprintf(fpointer, "Total Frames Per Sec Elapsed:  %f (%ld mps)\n",
                (float)framesOutput/(float)diffTime,
                (long)((float)framesOutput * 
                       (float)inputFrameBits /
                       (256.0*24.0*(float)diffTime)));
    } else {
        fprintf(fpointer, "Total Frames Per Sec Elapsed:  Infinite!\n");
    }
    if ( totalCPU == 0.0 ) {
        fprintf(fpointer, "CPU Time:  NONE!\n");
    } else {
        fprintf(fpointer, "Total Frames Per Sec CPU    :  %f (%ld mps)\n",
                (float)framesOutput/totalCPU,
                (long)((float)framesOutput *
                       (float)inputFrameBits/(256.0*24.0*totalCPU)));
    }
    fprintf(fpointer, "Total Output Bit Rate (%d fps):  %d bits/sec\n",
            frameRateRounded, frameRateRounded*totalBits/framesOutput);
    fprintf(fpointer, "MPEG file created in :  %s\n", outputFileName);
    fprintf(fpointer, "\n\n");
        
    if ( computeMVHist ) {
        ShowPMVHistogram(fpointer);
        ShowBBMVHistogram(fpointer);
        ShowBFMVHistogram(fpointer);
    }
}



/*===========================================================================*
 *
 * PrintEndStats
 *
 *  print end statistics (summary, time information)
 *
 * RETURNS: nothing
 *
 * SIDE EFFECTS:    none
 *
 *===========================================================================*/
void
PrintEndStats(time_t       const startTime,
              time_t       const endTime,
              unsigned int const inputFrameBits, 
              unsigned int const totalBits) {

    float   totalCPU;

    totalCPU = 0.0;
    totalCPU += IFrameTotalTime();
    totalCPU += PFrameTotalTime();
    totalCPU += BFrameTotalTime();

    if (!realQuiet) {
        fprintf(stdout, "\n\n");
        doEndStats(stdout, startTime, endTime, inputFrameBits,
                   totalBits, totalCPU);
    }
    
    if (statFile) {
        doEndStats(statFile, startTime, endTime, inputFrameBits,
                   totalBits, totalCPU);

        fclose(statFile);
    }
}



void
ReadDecodedRefFrame(MpegFrame *  const frameP, 
                    unsigned int const frameNumber) {

    FILE    *fpointer;
    char    fileName[256];
    int width, height;
    register int y;

    width = Fsize_x;
    height = Fsize_y;

    sprintf(fileName, "%s.decoded.%u", outputFileName, frameNumber);
    if (! realQuiet) {
        fprintf(stdout, "reading %s\n", fileName);
        fflush(stdout);
    }

    if ((fpointer = fopen(fileName, "rb")) == NULL) {
        sleepN(1000);
        if ((fpointer = fopen(fileName, "rb")) == NULL) {
            fprintf(stderr, "Cannot open %s\n", fileName);
            exit(1);
        }}

    Frame_AllocDecoded(frameP, TRUE);
    
    for ( y = 0; y < height; y++ ) {
        size_t bytesRead;

        bytesRead = fread(frameP->decoded_y[y], 1, width, fpointer);
        if (bytesRead != width)
            pm_error("Could not read enough bytes from '%s;", fileName);
    }
    
    for (y = 0; y < (height >> 1); y++) {           /* U */
        size_t const bytesToRead = width/2;
        size_t bytesRead;

        bytesRead = fread(frameP->decoded_cb[y], 1, bytesToRead, fpointer);
        if (bytesRead != bytesToRead)
            pm_message("Could not read enough bytes from '%s'", fileName);
    }
    
    for (y = 0; y < (height >> 1); y++) {           /* V */
        size_t const bytesToRead = width/2;
        size_t bytesRead;
        bytesRead = fread(frameP->decoded_cr[y], 1, bytesToRead, fpointer);
        if (bytesRead != bytesToRead)
            pm_message("Could not read enough bytes from '%s'", fileName);
    }
    fclose(fpointer);
}



static void
OpenBitRateFile() {
    bitRateFile = fopen(bitRateFileName, "w");
    if ( bitRateFile == NULL ) {
        pm_message("ERROR:  Could not open bit rate file:  '%s'", 
                   bitRateFileName);
        showBitRatePerFrame = FALSE;
    }
}



static void
CloseBitRateFile() {
    fclose(bitRateFile);
}