home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DP Tool Club 9
/
CD_ASCQ_09_1193.iso
/
news
/
4441
/
mpegcode
/
src
/
readfram.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-09-27
|
13KB
|
522 lines
/*===========================================================================*
* readframe.c *
* *
* procedures to read in frames *
* *
* EXPORTED PROCEDURES: *
* ReadFrame *
* SetFileType *
* SetFileFormat *
* *
*===========================================================================*/
/*
* Copyright (c) 1993 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: /n/picasso/users/keving/encode/src/RCS/readframe.c,v 1.1 1993/07/22 22:23:43 keving Exp keving $
* $Log: readframe.c,v $
* Revision 1.1 1993/07/22 22:23:43 keving
* nothing
*
*/
/*==============*
* HEADER FILES *
*==============*/
#include "all.h"
#include <time.h>
#include <errno.h>
#include "mtypes.h"
#include "frames.h"
#include "prototypes.h"
#include "parallel.h"
#include "param.h"
#include "readframe.h"
#include "fsize.h"
#include "rgbtoycc.h"
/*==================*
* STATIC VARIABLES *
*==================*/
static int fileType = BASE_FILE_TYPE;
static int baseFormat;
/*===============================*
* INTERNAL PROCEDURE prototypes *
*===============================*/
static int ReadNextInteger _ANSI_ARGS_((FILE *fpointer));
static void ReadPNM _ANSI_ARGS_((FILE * fp, MpegFrame * mf));
static boolean ReadPPM _ANSI_ARGS_((MpegFrame *mf, FILE *fpointer));
static void ReadYUV _ANSI_ARGS_((MpegFrame * mf, FILE *fpointer,
int width, int height));
/*=====================*
* EXPORTED PROCEDURES *
*=====================*/
/*===========================================================================*
*
* ReadFrame
*
* reads the given frame, performing conversion as necessary
* if addPath = TRUE, then must add the current path before the
* file name
*
* RETURNS: frame modified
*
* SIDE EFFECTS: none
*
*===========================================================================*/
void
ReadFrame(frame, fileName, conversion, addPath)
MpegFrame *frame;
char *fileName;
char *conversion;
boolean addPath;
{
FILE *ifp;
char command[1024];
char fullFileName[1024];
#ifdef BLEAH
static int32 readDiskTime = 0;
int32 diskStartTime, diskEndTime;
time(&diskStartTime);
#endif
if ( addPath ) {
sprintf(fullFileName, "%s/%s", currentPath, fileName);
} else {
sprintf(fullFileName, "%s", fileName);
}
#ifdef BLEAH
if ( ! childProcess ) {
fprintf(stdout, "+++++READING Frame %d (type %d): %s\n", frame->id,
frame->type, fullFileName);
}
#endif
if ( fileType == ANY_FILE_TYPE ) {
char *convertPtr, *commandPtr, *charPtr;
/* replace every occurrence of '*' with fullFileName */
convertPtr = conversion;
commandPtr = command;
while ( *convertPtr != '\0' ) {
while ( (*convertPtr != '\0') && (*convertPtr != '*') ) {
*commandPtr = *convertPtr;
commandPtr++;
convertPtr++;
}
if ( *convertPtr == '*' ) {
/* copy fullFileName */
charPtr = fullFileName;
while ( *charPtr != '\0' ) {
*commandPtr = *charPtr;
commandPtr++;
charPtr++;
}
convertPtr++; /* go past '*' */
}
}
*commandPtr = '\0';
if ( (ifp = fopen(command, "rb")) == NULL ) {
fprintf(stderr, "ERROR: Couldn't execute input conversion command:\n");
fprintf(stderr, "\t%s\n", command);
fprintf(stderr, "errno = %d\n", errno);
if ( ioServer ) {
fprintf(stderr, "IO SERVER: EXITING!!!\n");
} else {
fprintf(stderr, "SLAVE EXITING!!!\n");
}
exit(1);
}
} else {
ifp = fopen(fullFileName, "rb");
}
ERRCHK(ifp, "fopen");
if ( baseFormat == YUV_FILE_TYPE ) {
ReadYUV(frame, ifp, yuvWidth, yuvHeight);
} else if ( baseFormat == PPM_FILE_TYPE ) {
if ( ! ReadPPM(frame, ifp) ) {
fprintf(stderr, "Error reading PPM input file!!!\n");
exit(1);
}
PPMtoYUV(frame);
} else { /* baseFormat == PNM_FILE_TYPE */
ReadPNM(ifp, frame);
PNMtoYUV(frame);
}
if ( fileType == ANY_FILE_TYPE ) {
fclose(ifp);
} else {
fclose(ifp);
}
#ifdef BLEAH
time(&diskEndTime);
readDiskTime += (diskEndTime-diskStartTime);
fprintf(stdout, "cumulative disk read time: %d seconds\n", readDiskTime);
#endif
MotionSearchPreComputation(frame);
}
/*===========================================================================*
*
* SetFileType
*
* set the file type to be either a base type (no conversion), or
* any type (conversion required)
*
* RETURNS: nothing
*
* SIDE EFFECTS: fileType
*
*===========================================================================*/
void
SetFileType()
{
if ( strcmp(inputConversion, "*") == 0 ) {
fileType = BASE_FILE_TYPE;
} else {
fileType = ANY_FILE_TYPE;
}
}
/*===========================================================================*
*
* SetFileFormat
*
* set the file format (PPM, PNM, YUV)
*
* RETURNS: nothing
*
* SIDE EFFECTS: baseFormat
*
*===========================================================================*/
void
SetFileFormat(format)
char *format;
{
if ( strcmp(format, "PPM") == 0 ) {
baseFormat = PPM_FILE_TYPE;
} else if ( strcmp(format, "YUV") == 0 ) {
baseFormat = YUV_FILE_TYPE;
} else if ( strcmp(format, "PNM") == 0 ) {
baseFormat = PNM_FILE_TYPE;
} else if ( strcmp(format, "JPEG") == 0 ) {
fprintf(stderr, "SORRY: JPEG not available YET\n");
exit(1);
} else {
fprintf(stderr, "ERROR: Invalid file format: %s\n", format);
exit(1);
}
}
/*===========================================================================*
*
* ReadPNM
*
* read a PNM file
*
* RETURNS: mf modified
*
* SIDE EFFECTS: none
*
*===========================================================================*/
static void
ReadPNM(fp, mf)
FILE *fp;
MpegFrame *mf;
{
int x, y;
xelval maxval;
int format;
mf->rgb_data = pnm_readpnm(fp, &x, &y, &maxval, &format);
ERRCHK(mf, "pnm_readpnm");
if (format != PPM_FORMAT) {
if (maxval < 255) {
pnm_promoteformat(mf->rgb_data, x, y, maxval, format, 255, PPM_FORMAT);
maxval = 255;
} else {
pnm_promoteformat(mf->rgb_data, x, y, maxval, format, maxval, PPM_FORMAT);
}
}
if (maxval < 255) {
pnm_promoteformat(mf->rgb_data, x, y, maxval, format, 255, format);
maxval = 255;
}
/*
* if this is the first frame read, set the global frame size
*/
Fsize_Note(mf->id, x, y);
mf->rgb_maxval = maxval;
mf->rgb_format = PPM_FORMAT;
}
/*===========================================================================*
*
* ReadIOConvert
*
* do conversion; return a pointer to the appropriate file
*
* RETURNS: pointer to the appropriate file
*
* SIDE EFFECTS: none
*
*===========================================================================*/
FILE *
ReadIOConvert(fileName)
char *fileName;
{
FILE *ifp;
char command[1024];
char fullFileName[1024];
char *convertPtr, *commandPtr, *charPtr;
sprintf(fullFileName, "%s/%s", currentPath, fileName);
#ifdef BLEAH
if ( ! childProcess ) {
fprintf(stdout, "+++++READING (IO CONVERT) Frame %d (type %d): %s\n", frame->id,
frame->type, fullFileName); }
#endif
if ( strcmp(ioConversion, "*") == 0 ) {
ifp = fopen(fullFileName, "rb");
ERRCHK(ifp, "fopen");
return ifp;
}
/* replace every occurrence of '*' with fullFileName */
convertPtr = ioConversion;
commandPtr = command;
while ( *convertPtr != '\0' ) {
while ( (*convertPtr != '\0') && (*convertPtr != '*') ) {
*commandPtr = *convertPtr;
commandPtr++;
convertPtr++;
}
if ( *convertPtr == '*' ) {
/* copy fullFileName */
charPtr = fullFileName;
while ( *charPtr != '\0' ) {
*commandPtr = *charPtr;
commandPtr++;
charPtr++;
}
convertPtr++; /* go past '*' */
}
}
*commandPtr = '\0';
if ( (ifp = fopen(command, "rb")) == NULL ) {
fprintf(stderr, "ERROR: Couldn't execute input conversion command:\n");
fprintf(stderr, "\t%s\n", command);
fprintf(stderr, "errno = %d\n", errno);
if ( ioServer ) {
fprintf(stderr, "IO SERVER: EXITING!!!\n");
} else {
fprintf(stderr, "SLAVE EXITING!!!\n");
}
exit(1);
}
return ifp;
}
/*===========================================================================*
*
* ReadPPM
*
* read a PPM file
*
* RETURNS: TRUE if successful; FALSE otherwise; mf modified
*
* SIDE EFFECTS: none
*
*===========================================================================*/
static boolean
ReadPPM(mf, fpointer)
MpegFrame *mf;
FILE *fpointer;
{
char input[256];
int height, width, maxVal;
uint8 junk[4096];
register int y;
if ( fread(input, sizeof(char), 2, fpointer) != 2 ) {
return FALSE;
}
if ( strncmp(input, "P6", 2) != 0 ) { /* magic number */
return FALSE;
}
width = ReadNextInteger(fpointer);
if ( width == -1 ) {
return FALSE;
}
height = ReadNextInteger(fpointer);
if ( height == -1 ) {
return FALSE;
}
maxVal = ReadNextInteger(fpointer);
if ( maxVal == -1 ) {
return FALSE;
}
Fsize_Note(mf->id, width, height);
mf->rgb_maxval = maxVal;
Frame_AllocPPM(mf);
for ( y = 0; y < Fsize_y; y++ ) {
fread(mf->ppm_data[y], sizeof(char), 3*Fsize_x, fpointer);
/* read the leftover stuff on the right side */
fread(junk, sizeof(char), 3*(width-Fsize_x), fpointer);
}
return TRUE;
}
/*===========================================================================*
*
* ReadYUV
*
* read a YUV file
*
* RETURNS: mf modified
*
* SIDE EFFECTS: none
*
*===========================================================================*/
static void
ReadYUV(mf, fpointer, width, height)
MpegFrame *mf;
FILE *fpointer;
int width;
int height;
{
register int y;
Fsize_Note(mf->id, width, height);
Frame_AllocYCC(mf);
for (y = 0; y < height; y++) { /* Y */
fread(mf->orig_y[y], 1, width, fpointer);
}
for (y = 0; y < height / 2; y++) { /* U */
fread(mf->orig_cb[y], 1, width / 2, fpointer);
}
for (y = 0; y < height / 2; y++) { /* V */
fread(mf->orig_cr[y], 1, width / 2, fpointer);
}
}
/*=====================*
* INTERNAL PROCEDURES *
*=====================*/
/*===========================================================================*
*
* ReadNextInteger
*
* read an integer from a file, ignoring whitespace
*
* RETURNS: the integer, or -1 if end of file is reached first
*
* SIDE EFFECTS: file stream munched a bit
*
*===========================================================================*/
static int
ReadNextInteger(fpointer)
FILE *fpointer;
{
char input[256];
int index;
/* skip whitespace */
while ( fgets(input, 2, fpointer) != NULL ) {
if ( isspace(input[0]) ) {
continue;
}
/* read rest of integer */
index = 1;
while ( fgets(&input[index], 2, fpointer) != NULL ) {
if ( isspace(input[index]) ) {
break;
}
index++;
}
input[index] = '\0';
return atoi(input);
}
return -1; /* end of file reached */
}