📄 mpgread.c
字号:
/*********************************************************************MPEG2MOV.CThis is the main file for the MPEG to Matlab movie decoder.**********************************************************************//* MPEG Decoder (mpeg_play) copyright notice *//* * Copyright (c) 1992 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. *//************************************************************************ R C S Information*************************************************************************//* $Log: mpgread.c,v $ * Revision 1.7 1994/01/14 15:19:54 daf * Added RCS info header * * revision 1.6 locked by: daf; * date: 1994/01/14 15:13:04; author: daf; state: Exp; lines: +16 -15 * Fixed calls to MATLAB to get matlab file path * * revision 1.5 * date: 1994/01/12 09:05:59; author: daf; state: Exp; lines: +2 -2 * Changed includes so string.h is always included. * * revision 1.4 * date: 1994/01/11 20:14:02; author: daf; state: Exp; lines: +27 -3 * Modified for PC compatiblity * * revision 1.3 * date: 1994/01/11 19:02:53; author: daf; state: Exp; lines: +27 -1 * Now adds .mpg extension if file is not found without it. * * revision 1.2 * date: 1994/01/11 14:48:41; author: daf; state: Exp; lines: +120 -52 * Added [R,G,B] = ... format * * revision 1.1 * date: 1994/01/07 16:52:26; author: daf; state: Exp; * Initial revision*/#include "video.h"#include "proto.h"#include <stdio.h>#include <sys/types.h>#include <signal.h>#ifndef WIN32#include <memory.h>#endif#include <string.h>#ifdef WIN32 /* need to reverse byte order */ /* note -- we assume here that htonl is called on a variable, not a * constant; thus, this is not for general use. */#define htonl(x) \ ((((unsigned char *)(&x))[0] << 24) | \ (((unsigned char *)(&x))[1] << 16) | \ (((unsigned char *)(&x))[2] << 8) | \ (((unsigned char *)(&x))[3]))#define ntohl(x) htonl(x)#define htons(x) \ ((((unsigned char *)(&x))[0] << 8) | \ ((unsigned char *)(&x))[1])#define ntohs(x) htons(x)#else#ifndef MIPS#include <netinet/in.h>#else#include <bsd/netinet/in.h>#endif /* MIPS */#endif /* WIN32 */#include "util.h"#include "dither.h"#include "mex.h"/* Define buffer length. */#define BUF_LENGTH 80000/* External function declarations */extern int mpegVidRsrc();extern VidStream *NewVidStream();extern void ConvertColor();/* global variable to indicate start of new stream */int start_decode;/* Declaration of global variable to hold dither info. */int ditherType;/* Global file pointer to incoming data. */FILE *input;/* End of File flag. */static int EOF_flag = 0;/* Loop flag. */int loopFlag = 0;/* Shared memory flag. */int shmemFlag = 0;/* Quiet flag. */int quietFlag = 0;/* Display image on screen? */int noDisplayFlag = 0;typedef enum {ELITTLE_ENDIAN, EBIG_ENDIAN} ByteOrder;ByteOrder MOVIE_GetClientByteOrder(void){ const short one = 1; return((*((char *) &one) == 1) ? ELITTLE_ENDIAN : EBIG_ENDIAN );}#define FLIPBYTES(x) \( \((x&0xff) << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24) \)/* *-------------------------------------------------------------- * * get_more_data -- * * Called by correct_underflow in bit parsing utilities to * read in more data. * * Results: * Input buffer updated, buffer length updated. * Returns 1 if data read, 0 if EOF, -1 if error. * * Side effects: * None. * *-------------------------------------------------------------- */int get_more_data(unsigned int *buf_start, int max_length, int *length_ptr, unsigned int **buf_ptr){ int length, num_read, i, request; unsigned char *buffer, *mark; unsigned int *lmark; if (EOF_flag) return 0; length = *length_ptr; buffer = (unsigned char *) *buf_ptr; if (length > 0) { memcpy((unsigned char *) buf_start, buffer, (length*4)); mark = ((unsigned char *) (buf_start + length)); } else { mark = (unsigned char *) buf_start; length = 0; } request = (max_length-length)*4; num_read = fread( mark, 1, request, input); /* Paulo Villegas - 26/1/1993: Correction for 4-byte alignment */ { int num_read_rounded; unsigned char *index; num_read_rounded = 4*(num_read/4); /* this can happen only if num_read<request; i.e. end of file reached */ if( num_read_rounded < num_read ) { num_read_rounded = 4*( num_read/4+1 ); /* fill in with zeros */ for( index=mark+num_read; index<mark+num_read_rounded; *(index++)=0 ); /* advance to the next 4-byte boundary */ num_read = num_read_rounded; } } if (num_read < 0) { return -1; } else if (num_read == 0) { *buf_ptr = buf_start; /* Make 32 bits after end equal to 0 and 32 bits after that equal to seq end code in order to prevent messy data from infinite recursion. */ *(buf_start + length) = 0x0; *(buf_start + length+1) = SEQ_END_CODE; EOF_flag = 1; return 0; } lmark = (unsigned int *) mark; num_read = num_read/4; for (i=0; i<num_read; i++) { *lmark = htonl((*lmark)); lmark++; } *buf_ptr = buf_start; *length_ptr = length + num_read; return 1;}/*======================================================================mexFunctionThis is the Matlab interface function for the MPEG to Matlab decoder.This routine initializes variables, creates the output matrices andcalls the MPEG encoder functions to decode the MPEG file.======================================================================*/voidmexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){ char filename[256]; /* input MPEG file name */ char err_text[80]; /* string to hold error message */ static VidStream *theStream; /* pointer to Video Stream struct. */ int h,i,j,k; /* loop counters */ double *cm; /* pointer to colormap matrix values */ double *movie; /* pointer to movie matrix values */ double *red; /* pointer to R,G,B matrix values */ double *green; double *blue; int movie_rows; /* number of rows in movie matrix */ int num_pix_doubles; /* number of doubles used by each frame */ int n_colors; /* number of colors in colormap */ unsigned char r,g,b; /* hold RGB values converted from YUV */ unsigned char *m_ptr; /* pointer to bytes in movie matrix */ double *r_ptr; /* pointers to doubles in R,G,B matrices */ double *g_ptr; double *b_ptr; unsigned int lum, crom; /* indices into luminance and crominance */ int vid_status; /* holds return from main decoder call */ int last_frame; /* holds last frame requested by user */ int n_req_frames; /* number of requested frames */ double *req_frames; /* points to array of frame numbers */ int frames_stored; /* counts # of frames stored in matrix */ int no_colormap; /* whether or not to return colormap */ int rgb_format; /* RGB if true, otherwise colormapped */ int NewOutput = 0; /* 0: Pre-5.3 movie format, 1: 5.3+ movie format */ int NewTrueColorOutput = 0; /* 0: Indexed movie, 1: TrueColor movie */ unsigned int temp_int; /* used as temp storage for flipping bytes*/ mxArray *fid[2], *filestr[2]; /* matrices used in searching Matlab path */ /* check arguments */ no_colormap = 1; rgb_format = 0; if (nlhs == 2) no_colormap = 0; else if (nlhs == 3) rgb_format = 1; else if (nlhs > 3) mexErrMsgTxt("Too many output arguments."); else if (nlhs < 1) mexErrMsgTxt("Too few output arguments."); if (nrhs < 1) mexErrMsgTxt("Too few input arguments."); if (nrhs > 3) mexErrMsgTxt("Too many input arguments."); if (!mxIsChar(prhs[0])) mexErrMsgTxt("First argument (file name) must be a string."); mxGetString(prhs[0], filename, 256); /* get list of frame numbers if passed*/ if ((nrhs >= 2) && (!mxIsEmpty(prhs[1]))) { if (mxGetN(prhs[1]) > 1) { n_req_frames = mxGetN(prhs[1]); if (mxGetM(prhs[1]) > 1) mexErrMsgTxt("Second argument must be a vector"); } else { n_req_frames = mxGetM(prhs[1]); } req_frames = mxGetPr(prhs[1]); /* find the highest number frame requested */ last_frame = 1; for (j = 0; j < n_req_frames; j++) { if ((int)req_frames[j] > last_frame) last_frame = (int)req_frames[j]; } } else { last_frame = 0; n_req_frames = 0; } if (nrhs == 3) { char str[100]; mxGetString(prhs[2], str, 100); if (strcmp(str, "truecolor") == 0) { NewTrueColorOutput = 1; } NewOutput = 1; } /* open the MPEG file */ /* call Matlab to search the Matlab path */ filestr[0] = mxCreateString(filename); mexCallMATLAB(1,fid,1,filestr,"fopen"); if (*(mxGetPr(fid[0])) == -1) { if (strchr(filename, '.') == NULL) { strcat(filename, ".mpg"); mxDestroyArray(filestr[0]); mxDestroyArray(fid[0]); filestr[0] = mxCreateString(filename); mexCallMATLAB(1,fid,1,filestr,"fopen"); if (*mxGetPr(fid[0]) == -1) mexErrMsgTxt("Could not open file."); } else mexErrMsgTxt("Could not open file."); } mxDestroyArray(filestr[0]); mexCallMATLAB(1,filestr,1,fid,"fopen"); mxGetString(filestr[0], filename, 255); mxDestroyArray(filestr[0]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -