?? readframe.c
字號:
/*===========================================================================* * readframe.c * * * * procedures to read in frames * * * * EXPORTED PROCEDURES: * * ReadFrame * * SetFileType * * SetFileFormat * * JMovie2JPEG * * * *===========================================================================*//* * 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"#include "jpeglib.h"#define HEADER_SIZE 607 /*JFIF header size used on output images*/#define PPM_READ_STATE_MAGIC 0#define PPM_READ_STATE_WIDTH 1#define PPM_READ_STATE_HEIGHT 2#define PPM_READ_STATE_MAXVAL 3#define PPM_READ_STATE_DONE 4/*==================* * STATIC VARIABLES * *==================*/static int fileType = BASE_FILE_TYPE;/*===============================* * INTERNAL PROCEDURE prototypes * *===============================*/static char *ScanNextString _ANSI_ARGS_((char *inputLine, char *string));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));static void ReadSub4 _ANSI_ARGS_((MpegFrame * mf, FILE *fpointer, int width, int height));static void ReadJPEG _ANSI_ARGS_((MpegFrame * mf, File *fp));/*=====================* * 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 * *===========================================================================*/voidReadFrame(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 = popen(command, "r")) == 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 { if ( (ifp = fopen(fullFileName, "r")) == NULL ) { fprintf(stderr, "ERROR: Couldn't open input file %s\n", fullFileName); exit(1); } } switch(baseFormat) { case YUV_FILE_TYPE: ReadYUV(frame, ifp, realWidth, realHeight); break; case PPM_FILE_TYPE: if ( ! ReadPPM(frame, ifp) ) { fprintf(stderr, "Error reading PPM input file!!!\n"); exit(1); } PPMtoYUV(frame); break; case PNM_FILE_TYPE: ReadPNM(ifp, frame); PNMtoYUV(frame); break; case SUB4_FILE_TYPE: ReadSub4(frame, ifp, yuvWidth, yuvHeight); break; case JPEG_FILE_TYPE: ReadJPEG(frame,ifp); break; default: break; } if ( fileType == ANY_FILE_TYPE ) { pclose(ifp); } else { fclose(ifp); }#ifdef BLEAHtime(&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 * *===========================================================================*/voidSetFileType(conversion) char *conversion;{ if ( strcmp(conversion, "*") == 0 ) { fileType = BASE_FILE_TYPE; } else { fileType = ANY_FILE_TYPE; }}/*===========================================================================* * * SetFileFormat * * set the file format (PPM, PNM, YUV, JPEG, JMOVIE) * * RETURNS: nothing * * SIDE EFFECTS: baseFormat * *===========================================================================*/voidSetFileFormat(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 ) { baseFormat = JPEG_FILE_TYPE; } else if ( strcmp(format,"JMOVIE") == 0){ baseFormat = JMOVIE_FILE_TYPE; } else if ( strcmp(format, "SUB4") == 0 ) { baseFormat = SUB4_FILE_TYPE; } else { fprintf(stderr, "ERROR: Invalid file format: %s\n", format); exit(1); }}/*************************JPEG LIBRARY INTERFACE*********************//* * THE BIG PICTURE: * * The rough outline this JPEG decompression operation is: * * allocate and initialize JPEG decompression object * specify data source (eg, a file) * jpeg_read_header(); // obtain image dimensions and other parameters * set parameters for decompression * jpeg_start_decompress(); * while (scan lines remain to be read) * jpeg_read_scanlines(...); * jpeg_finish_decompress(); * release JPEG decompression object * *//*===========================================================================* * * ReadJPEG contributed by James Arthur Boucher of Boston University's * Multimedia Communications Lab * * read a JPEG file and copy data into frame original data arrays * * RETURNS: mf modified * * SIDE EFFECTS: none * *===========================================================================*/static voidReadJPEG(mf, fp) MpegFrame *mf; FILE *fp;{ /* This struct contains the JPEG decompression parameters and pointers to * working data (which is allocated as needed by the JPEG library). */ struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr jerr; /* More stuff */ JSAMPARRAY scanarray[3]; JSAMPLE *in_data_ptr; uint8 *out_data_ptr_y; uint8 *out_data_ptr_cb; uint8 *out_data_ptr_cr; int ci,cb,cd,cp; JDIMENSION ncols[3]; JDIMENSION nrows[3]; jpeg_component_info *compptr; int buffer_height; int current_row[3]; uint8 **orig[3]; int h_samp[3],v_samp[3]; int max_h_samp,max_v_samp; int temp_h, temp_v; int temp; /* Allocate and initialize JPEG decompression object */ cinfo.err = jpeg_std_error(&jerr); /* Now we can initialize the JPEG decompression object. */ jpeg_create_decompress(&cinfo); /* specify data source (eg, a file) */ jpeg_stdio_src(&cinfo, fp); /* read file parameters with jpeg_read_header() */ (void) jpeg_read_header(&cinfo, TRUE); /* We can ignore the return value from jpeg_read_header since * (a) suspension is not possible with the stdio data source, and * (b) we passed TRUE to reject a tables-only JPEG file as an error. */ /* set parameters for decompression */ cinfo.want_raw_output = TRUE; cinfo.out_color_space =JCS_YCbCr; /* calculate image output dimensions */ jpeg_calc_output_dimensions(&cinfo); /* the above calculation will set these soon */ /* for now we'll set them ourselves */ /* tell mpeg_encode the size of the JPEG Image*/ Fsize_Note(mf->id,(int)(cinfo.image_width),(int)(cinfo.image_height)); /* Allocate memory for the raw YCbCr data to occupy*/ Frame_AllocYCC(mf); /*allocate space for mpeg frame*/ /* copy pointers to array structure- this make the following code more compact */ orig[0] = mf->orig_y; orig[1] = mf->orig_cb; orig[2] = mf->orig_cr; /* Note that we can use the info obtained from jpeg_read_header. */ /* Start decompressor */ jpeg_start_decompress(&cinfo); /* JSAMPLEs per row in output buffer */ /* collect component subsample values*/ for(cp=0,compptr = cinfo.comp_info;cp<cinfo.num_components; cp++,compptr++) { h_samp[cp] = compptr->h_samp_factor; v_samp[cp] = compptr->v_samp_factor; } /* calculate max subsample values*/ temp_h = (h_samp[0]<h_samp[1]) ? h_samp[1] : h_samp[0]; max_h_samp = (temp_h<h_samp[2]) ? h_samp[2]:temp_h; temp_v = (v_samp[0]<v_samp[1]) ? v_samp[1] : v_samp[0]; max_v_samp = (temp_v<v_samp[2]) ? v_samp[2]:temp_v;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -