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
|
/*===========================================================================*
* main.c
*
* Main procedure
*
*===========================================================================*/
/*
* 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 _XOPEN_SOURCE 500 /* Make sure strdup() is in string.h */
#define _BSD_SOURCE /* Make sure strdup() is in string.h */
#include <assert.h>
#include "all.h"
#include "mtypes.h"
#include "dct.h"
#include "mpeg.h"
#include "motion_search.h"
#include "prototypes.h"
#include "param.h"
#include "parallel.h"
#include "readframe.h"
#include "combine.h"
#include "frames.h"
#include "jpeg.h"
#include "specifics.h"
#include "opts.h"
#include "frametype.h"
#include "input.h"
#include "gethostname.h"
#include "pm_c_util.h"
#include "ppm.h"
#include "nstring.h"
#include <time.h>
int main _ANSI_ARGS_((int argc, char **argv));
/*==================*
* GLOBAL VARIABLES *
*==================*/
bool showBitRatePerFrame;
bool computeMVHist = FALSE;
boolean frameSummary;
extern time_t IOtime;
int whichGOP = -1;
boolean ioServer = FALSE;
boolean outputServer = FALSE;
boolean decodeServer = FALSE;
int quietTime = 0;
boolean realQuiet = FALSE;
boolean noFrameSummaryOption = FALSE;
boolean debugSockets = FALSE;
boolean debugMachines = FALSE;
boolean bitRateInfoOption = FALSE;
int baseFormat;
extern FrameSpecList *fsl;
boolean pureDCT=FALSE;
char encoder_name[1024];
const char * hostname;
/*================================*
* External PROCEDURE prototypes *
*================================*/
void init_idctref _ANSI_ARGS_((void));
struct cmdlineInfo {
bool childProcess;
majorProgramFunction function;
const char * masterHostname;
int masterPortNumber;
unsigned int outputFrames;
int maxMachines;
const char * paramFileName;
bool specificFrames;
unsigned int frameStart;
unsigned int frameEnd;
};
static void
parseArgs(int const argc,
char ** const argv,
struct cmdlineInfo * const cmdlineP) {
int idx;
if (argc-1 < 1)
pm_error("You must specify at least one argument: the parameter "
"file name");
cmdlineP->function = ENCODE_FRAMES;
cmdlineP->childProcess = FALSE; /* initial assumption */
cmdlineP->outputFrames = 0;
cmdlineP->maxMachines = MAXINT;
cmdlineP->specificFrames = FALSE;
/* parse the arguments */
idx = 1;
while (idx < argc-1) {
if (argv[idx][0] != '-')
pm_error("argument '%s', which must be an option because "
"it is not the last argument, "
"does not start with '-'", argv[idx]);
if (streq(argv[idx], "-stat")) {
if (idx+1 < argc-1) {
SetStatFileName(argv[idx+1]);
idx += 2;
} else
pm_error("Invalid -stat option");
} else if (streq(argv[idx], "-gop")) {
if (cmdlineP->function != ENCODE_FRAMES ||
cmdlineP->specificFrames)
pm_error("Invalid -gop option");
if (idx+1 < argc-1) {
whichGOP = atoi(argv[idx+1]);
idx += 2;
} else
pm_error("Invalid -gop option");
} else if (streq(argv[idx], "-frames")) {
if (cmdlineP->function != ENCODE_FRAMES || whichGOP != -1)
pm_error("invalid -frames option");
if (idx+2 < argc-1) {
int const frameStart = atoi(argv[idx+1]);
int const frameEnd = atoi(argv[idx+2]);
if (frameStart > frameEnd)
pm_error("Start frame number %d is greater than end "
"frame number %d", frameStart, frameEnd);
if (frameStart < 0)
pm_error("Start frame number %d is less than zero",
frameStart);
cmdlineP->specificFrames = TRUE;
cmdlineP->frameStart = frameStart;
cmdlineP->frameEnd = frameEnd;
idx += 3;
} else
pm_error("-frames needs to be followed by two values");
} else if (streq(argv[idx], "-combine_gops")) {
if (cmdlineP->function != ENCODE_FRAMES || whichGOP != -1 ||
cmdlineP->specificFrames) {
pm_error("Invalid -combine_gops option");
}
cmdlineP->function = COMBINE_GOPS;
++idx;
} else if (streq(argv[idx], "-combine_frames")) {
if (cmdlineP->function != ENCODE_FRAMES || whichGOP != -1 ||
cmdlineP->specificFrames)
pm_error("Invalid -combine_frames option");
cmdlineP->function = COMBINE_FRAMES;
++idx;
} else if (streq(argv[idx], "-child")) {
if (idx+7 < argc-1) {
int combinePortNumber;
/* This used to be important information, when the child
notified the combine server. Now the master notifies
the combine server after the child notifies the master
it is done. So this value is unused.
*/
cmdlineP->masterHostname = argv[idx+1];
cmdlineP->masterPortNumber = atoi(argv[idx+2]);
ioPortNumber = atoi(argv[idx+3]);
combinePortNumber = atoi(argv[idx+4]);
decodePortNumber = atoi(argv[idx+5]);
machineNumber = atoi(argv[idx+6]);
remoteIO = atoi(argv[idx+7]);
IOhostName = cmdlineP->masterHostname;
} else
pm_error("Not enough option values for -child option. "
"Need 7.");
cmdlineP->childProcess = TRUE;
idx += 8;
} else if (streq(argv[idx], "-io_server")) {
if (idx+2 < argc-1) {
cmdlineP->masterHostname = argv[idx+1];
cmdlineP->masterPortNumber = atoi(argv[idx+2]);
} else
pm_error("Invalid -io_server option");
ioServer = TRUE;
idx += 3;
} else if (streq(argv[idx], "-output_server")) {
if (idx+3 < argc-1) {
cmdlineP->masterHostname = argv[idx+1];
cmdlineP->masterPortNumber = atoi(argv[idx+2]);
cmdlineP->outputFrames = atoi(argv[idx+3]);
} else
pm_error("-output_server option requires 3 option values. "
"You specified %u", argc-1 - idx);
outputServer = TRUE;
idx += 4;
} else if (streq(argv[idx], "-decode_server")) {
if (idx+3 < argc-1) {
cmdlineP->masterHostname = argv[idx+1];
cmdlineP->masterPortNumber = atoi(argv[idx+2]);
cmdlineP->outputFrames = atoi(argv[idx+3]);
} else
pm_error("Invalid -decode_server option");
cmdlineP->function = COMBINE_FRAMES;
decodeServer = TRUE;
idx += 4;
} else if (streq(argv[idx], "-nice")) {
niceProcesses = TRUE;
idx++;
} else if (streq(argv[idx], "-max_machines")) {
if (idx+1 < argc-1) {
cmdlineP->maxMachines = atoi(argv[idx+1]);
} else
pm_error("Invalid -max_machines option");
idx += 2;
} else if (streq(argv[idx], "-quiet")) {
if (idx+1 < argc-1)
quietTime = atoi(argv[idx+1]);
else
pm_error("Invalid -quiet option");
idx += 2;
} else if (streq(argv[idx], "-realquiet")) {
realQuiet = TRUE;
++idx;
} else if (streq(argv[idx], "-float_dct") ||
streq(argv[idx], "-float-dct")) {
pureDCT = TRUE;
init_idctref();
init_fdct();
++idx;
} else if (streq(argv[idx], "-no_frame_summary")) {
if (idx < argc-1)
noFrameSummaryOption = TRUE;
else
pm_error("Invalid -no_frame_summary option");
++idx;
} else if (streq(argv[idx], "-snr")) {
printSNR = TRUE;
++idx;
} else if (streq(argv[idx], "-mse")) {
printSNR = printMSE = TRUE;
++idx;
} else if (streq(argv[idx], "-debug_sockets")) {
debugSockets = TRUE;
++idx;
} else if (streq(argv[idx], "-debug_machines")) {
debugMachines = TRUE;
++idx;
} else if (streq(argv[idx], "-bit_rate_info")) {
if (idx+1 < argc-1) {
bitRateInfoOption = TRUE;
SetBitRateFileName(argv[idx+1]);
idx += 2;
} else
pm_error("Invalid -bit_rate_info option");
} else if (streq(argv[idx], "-mv_histogram")) {
computeMVHist = TRUE;
++idx;
} else
pm_error("Unrecognized option: '%s'", argv[idx]);
}
cmdlineP->paramFileName = argv[argc-1];
}
static void
compileTests() {
/* There was code here (assert() calls) that verified that uint16
is 16 bits, etc. It caused compiler warnings that said, "Of
course it is!" (actually, "statement has no effect"). There
isn't enough chance that uint16, etc. are defined incorrectly
to warrant those asserts. 2000.05.07
*/
if ( (-8 >> 3) != -1 ) {
fprintf(stderr, "ERROR: Right shifts are NOT arithmetic!!!\n");
fprintf(stderr, "Change >> to multiplies by powers of 2\n");
exit(1);
}
}
static void
announceJob(enum frameContext const context,
bool const childProcess,
unsigned int const frameStart,
unsigned int const frameEnd,
const char * const outputFileName) {
if (!realQuiet) {
const char * outputDest;
const char * combineDest;
if (context == CONTEXT_JUSTFRAMES)
pm_asprintf(&outputDest, "to individual frame files");
else
pm_asprintf(&outputDest, "to file '%s'", outputFileName);
if (childProcess)
combineDest = strdup("for delivery to combine server");
else
combineDest = strdup("");
pm_message("%s: ENCODING FRAMES %u-%u to %s %s",
hostname, frameStart, frameEnd, outputDest, combineDest);
pm_strfree(combineDest);
pm_strfree(outputDest);
}
}
static void
encodeSomeFrames(struct inputSource * const inputSourceP,
boolean const childProcess,
enum frameContext const context,
unsigned int const frameStart,
unsigned int const frameEnd,
int32 const qtable[],
int32 const niqtable[],
FILE * const ofp,
const char * const outputFileName,
bool const wantVbvUnderflowWarning,
bool const wantVbvOverflowWarning,
boolean const printStats,
unsigned int * const encodeTimeP) {
time_t framesTimeStart, framesTimeEnd;
unsigned int inputFrameBits;
unsigned int totalBits;
announceJob(context, childProcess, frameStart, frameEnd, outputFileName);
time(&framesTimeStart);
if (printStats)
PrintStartStats(framesTimeStart, context == CONTEXT_JUSTFRAMES,
frameStart, frameEnd, inputSourceP);
GenMPEGStream(inputSourceP, context, frameStart, frameEnd,
qtable, niqtable, childProcess, ofp, outputFileName,
wantVbvUnderflowWarning, wantVbvOverflowWarning,
&inputFrameBits, &totalBits);
time(&framesTimeEnd);
*encodeTimeP = (unsigned int)(framesTimeEnd - framesTimeStart);
if (!realQuiet)
pm_message("%s: COMPLETED FRAMES %u-%u (%u seconds)",
hostname, frameStart, frameEnd, *encodeTimeP);
if (printStats)
PrintEndStats(framesTimeStart, framesTimeEnd,
inputFrameBits, totalBits);
}
static void
encodeFrames(struct inputSource * const inputSourceP,
boolean const childProcess,
const char * const masterHostname,
int const masterPortNumber,
int const whichGOP,
bool const specificFrames,
unsigned int const whichFrameStart,
unsigned int const whichFrameEnd,
int32 const qtable[],
int32 const niqtable[],
FILE * const ofp,
const char * const outputFileName,
bool const wantVbvUnderflowWarning,
bool const wantVbvOverflowWarning) {
/*----------------------------------------------------------------------------
Encode the stream. If 'specificFrames' is true, then encode frames
'whichFrameStart' through 'whichFrameEnd' individually. Otherwise,
encode the entire input stream as a complete MPEG stream.
'childProcess' means to do it as a child process that is under the
supervision of a master process and is possibly doing only part of
a larger batch.
(If we had proper modularity, we wouldn't care, but parallel operation
was glued on to this program after it was complete).
One thing we don't do when running as a child process is print
statistics; our master will do that for the whole job.
----------------------------------------------------------------------------*/
unsigned int frameStart, frameEnd;
enum frameContext context;
unsigned int lastEncodeTime;
/* How long it took the encoder to do the last set of frames */
boolean printStats;
/* We want the encoder to print start & end stats */
if (whichGOP != -1) {
/* He wants just one particular GOP from the middle of the movie. */
ComputeGOPFrames(whichGOP, &frameStart, &frameEnd,
inputSourceP->numInputFiles);
context = CONTEXT_GOP;
} else if (specificFrames) {
/* He wants some pure frames from the middle of the movie */
if (whichFrameStart > whichFrameEnd)
pm_error("You specified a starting frame number (%d) that is "
"greater than the ending frame number (%d) "
"you specified.", whichFrameStart, whichFrameEnd);
if (whichFrameEnd + 1 > inputSourceP->numInputFiles)
pm_error("You specified ending frame number %d, which is "
"beyond the number of input files you supplied (%u)",
whichFrameEnd, inputSourceP->numInputFiles);
frameStart = whichFrameStart;
frameEnd = whichFrameEnd;
context = CONTEXT_JUSTFRAMES;
} else {
/* He wants the whole movie */
frameStart = 0;
frameEnd = inputSourceP->numInputFiles - 1;
context = CONTEXT_WHOLESTREAM;
}
printStats = !childProcess;
encodeSomeFrames(inputSourceP, childProcess, context, frameStart, frameEnd,
customQtable, customNIQtable,
ofp, outputFileName,
wantVbvUnderflowWarning, wantVbvOverflowWarning,
printStats,
&lastEncodeTime);
if (childProcess) {
boolean moreWorkToDo;
/* A child is not capable of generating GOP or stream headers */
assert(context == CONTEXT_JUSTFRAMES);
moreWorkToDo = TRUE; /* initial assumption */
while (moreWorkToDo) {
int nextFrameStart, nextFrameEnd;
NotifyMasterDone(masterHostname, masterPortNumber, machineNumber,
lastEncodeTime, &moreWorkToDo,
&nextFrameStart, &nextFrameEnd);
if (moreWorkToDo)
encodeSomeFrames(inputSourceP, childProcess,
CONTEXT_JUSTFRAMES,
nextFrameStart, nextFrameEnd,
customQtable, customNIQtable,
NULL, outputFileName,
wantVbvUnderflowWarning,
wantVbvOverflowWarning,
FALSE,
&lastEncodeTime);
}
if (!realQuiet)
pm_message("%s: Child exiting. Master has no more work.",
hostname);
}
}
static void
runMaster(struct inputSource * const inputSourceP,
const char * const paramFileName,
const char * const outputFileName) {
if (paramFileName[0] != '/' && paramFileName[0] != '~')
pm_error("For parallel mode, you must "
"use an absolute path for parameter file. "
"You specified '%s'", paramFileName);
else
MasterServer(inputSourceP, paramFileName, outputFileName);
}
static void
getUserFrameFile(void * const handle,
unsigned int const frameNumber,
FILE ** const ifPP) {
struct inputSource * const inputSourceP = (struct inputSource *) handle;
if (inputSourceP->stdinUsed)
pm_error("You cannot combine frames from Standard Input.");
/* Because Caller detects end of frames by EOF */
if (frameNumber >= inputSourceP->numInputFiles)
*ifPP = NULL;
else {
const char * fileName;
const char * inputFileName;
GetNthInputFileName(inputSourceP, frameNumber, &inputFileName);
pm_asprintf(&fileName, "%s/%s", currentFramePath, inputFileName);
*ifPP = fopen(fileName, "rb");
if (*ifPP == NULL)
pm_error("Unable to open file '%s'. Errno = %d (%s)",
fileName, errno, strerror(errno));
pm_strfree(inputFileName);
pm_strfree(fileName);
}
}
static void
nullDisposeFile(void * const handle,
unsigned int const frameNumber) {
}
static unsigned int
framePoolSize(bool const sequentialInput) {
/*----------------------------------------------------------------------------
Return the number of frames that our frame memory pool needs to have.
'sequentialInput' means we'll be reading from input that comes in frame
number order and we can't go back, so we'll have to have a bigger buffer
of frames than otherwise.
-----------------------------------------------------------------------------*/
unsigned int numOfFrames;
numOfFrames = 0; /* initial value */
if (sequentialInput) {
unsigned int idx;
unsigned int bcount;
for ( idx = 0, bcount = 0; idx < strlen(framePattern); idx++) {
/* counts the maximum number of B frames between two reference
* frames.
*/
switch( framePattern[idx] ) {
case 'b':
bcount++;
break;
case 'i':
case 'p':
if (bcount > numOfFrames)
numOfFrames = bcount;
bcount = 0;
break;
}
/* add 2 to hold the forward and past reference frames in addition
* to the maximum number of B's
*/
}
numOfFrames += 2;
} else {
/* non-interactive, only 3 frames needed */
numOfFrames = 3;
}
return numOfFrames;
}
int
main(int argc, char **argv) {
FILE *ofP;
time_t initTimeStart;
struct cmdlineInfo cmdline;
struct params params;
ppm_init(&argc, argv);
strcpy(encoder_name, argv[0]);
compileTests();
time(&initTimeStart);
SetStatFileName("");
hostname = GetHostName();
parseArgs(argc, argv, &cmdline);
ReadParamFile(cmdline.paramFileName, cmdline.function, ¶ms);
/* Jim Boucher's stuff:
if we are using a movie format then break up into frames
*/
if ((!cmdline.childProcess) && (baseFormat == JMOVIE_FILE_TYPE))
JM2JPEG(params.inputSourceP);
if (printSNR || (referenceFrame == DECODED_FRAME))
decodeRefFrames = TRUE;
showBitRatePerFrame = (bitRateInfoOption && !cmdline.childProcess);
frameSummary = (!noFrameSummaryOption && !cmdline.childProcess);
numMachines = min(numMachines, cmdline.maxMachines);
Tune_Init();
Frame_Init(framePoolSize(params.inputSourceP->stdinUsed));
if (specificsOn)
Specifics_Init();
ComputeFrameTable(params.inputSourceP->stdinUsed ?
0 : params.inputSourceP->numInputFiles);
if (ioServer) {
IoServer(params.inputSourceP, cmdline.masterHostname,
cmdline.masterPortNumber);
return 0;
} else if (outputServer) {
CombineServer(cmdline.outputFrames,
cmdline.masterHostname, cmdline.masterPortNumber,
outputFileName);
} else if (decodeServer) {
DecodeServer(cmdline.outputFrames, outputFileName,
cmdline.masterHostname, cmdline.masterPortNumber);
} else {
if (!cmdline.specificFrames &&
(numMachines == 0 || cmdline.function != ENCODE_FRAMES) ) {
ofP = fopen(outputFileName, "wb");
if (ofP == NULL)
pm_error("Could not open output file!");
} else
ofP = NULL;
if (cmdline.function == ENCODE_FRAMES) {
if (numMachines == 0 || cmdline.specificFrames) {
encodeFrames(params.inputSourceP,
cmdline.childProcess,
cmdline.masterHostname, cmdline.masterPortNumber,
whichGOP, cmdline.specificFrames,
cmdline.frameStart, cmdline.frameEnd,
customQtable, customNIQtable,
ofP, outputFileName,
params.warnUnderflow, params.warnOverflow);
} else
runMaster(params.inputSourceP,
cmdline.paramFileName, outputFileName);
} else if (cmdline.function == COMBINE_GOPS)
GOPsToMPEG(params.inputSourceP, outputFileName, ofP);
else if (cmdline.function == COMBINE_FRAMES)
FramesToMPEG(ofP, params.inputSourceP,
&getUserFrameFile, &nullDisposeFile);
}
Frame_Exit();
pm_strfree(hostname);
return 0;
}
|