📄 mpgread.c
字号:
mexCallMATLAB(1,filestr,1,fid,"fclose"); mxDestroyArray(fid[0]); mxDestroyArray(filestr[0]);#ifdef WIN32 input = fopen(filename, "rb");#else input = fopen(filename, "r");#endif if (input == NULL) { mexErrMsgTxt("Could not open file."); } /* initialization */ ditherType = ORDERED2_DITHER; LUM_RANGE = 8; CR_RANGE = CB_RANGE = 4; noDisplayFlag = 0;#ifdef SH_MEM shmemFlag = 1;#endif init_tables(); InitColor(); InitOrdered2Dither(); EOF_flag = 0; curBits = 0; bitOffset = 0; bufLength = 0; bitBuffer = NULL; totNumFrames = 0; start_decode = 1; curVidStream = NULL; theStream = NewVidStream(BUF_LENGTH); if (theStream == NULL) mexErrMsgTxt("Out of memory."); /* parse the MPEG header */ mpegVidRsrc(0, theStream); /* create the movie and colormap Matrices */ n_colors = LUM_RANGE*CB_RANGE*CR_RANGE; /*realTimeStart = ReadSysClock();*/ if (n_req_frames == 0) { /* if user did not specify frames to get, one pass is needed to determine * the number of frames before actually putting them into the matrix */ vid_status = 0; while (vid_status != 1) { vid_status = mpegVidRsrc(0, theStream); } n_req_frames = last_frame = totNumFrames; req_frames = (double *)mxCalloc(n_req_frames, sizeof(double)); for (i = 0; i < n_req_frames; i++) req_frames[i] = i+1; DestroyVidStream(theStream); rewind(input); EOF_flag = 0; curBits = 0; bitOffset = 0; bufLength = 0; bitBuffer = NULL; totNumFrames = 0; theStream = NewVidStream(BUF_LENGTH); mpegVidRsrc(0, theStream); } /* create the output matrices */ if (NewOutput) { char *fieldnames[] = {"cdata", "colormap"}; plhs[0] = mxCreateStructMatrix(1, n_req_frames, 2, (const char **)fieldnames); if (NewTrueColorOutput) { int i; int dims[3]; dims[0] = theStream->v_size; dims[1] = theStream->h_size; dims[2] = 3; for (i = 0; i < n_req_frames; i++) { mxArray *cdata = mxCreateNumericArray(3, dims, mxUINT8_CLASS, mxREAL); mxSetField(plhs[0], i, "cdata", cdata); } } else { int i; int dims[2]; dims[0] = theStream->v_size; dims[1] = theStream->h_size; for (i = 0; i < n_req_frames; i++) { mxArray *cdata = mxCreateNumericArray(2, dims, mxUINT8_CLASS, mxREAL); mxArray *colormap = mxCreateDoubleMatrix(n_colors, 3, mxREAL); mxSetField(plhs[0], i, "cdata", cdata); mxSetField(plhs[0], i, "colormap", colormap); { /* Fill in the colormap */ double *cm = mxGetPr(colormap); int j; for (j = 0; j < n_colors; j++) { /* fprintf(stderr, "Color: %d\n", j); */ ConvertColor(lum_values[(j/(CR_RANGE*CB_RANGE))%LUM_RANGE], cr_values[(j/CB_RANGE)%CR_RANGE], cb_values[j%CB_RANGE], &r, &g, &b); cm[j] = (double)r/255; cm[n_colors + j] = (double)g/255; cm[n_colors * 2 + j] = (double)b/255; } } } } } else { if (rgb_format) { plhs[0] = mxCreateDoubleMatrix(theStream->v_size, theStream->h_size*n_req_frames, mxREAL); if (plhs[0] == NULL) mexErrMsgTxt("Out of memory."); plhs[1] = mxCreateDoubleMatrix(theStream->v_size, theStream->h_size*n_req_frames, mxREAL); if (plhs[1] == NULL) mexErrMsgTxt("Out of memory."); plhs[2] = mxCreateDoubleMatrix(theStream->v_size, theStream->h_size*n_req_frames, mxREAL); if (plhs[2] == NULL) mexErrMsgTxt("Out of memory."); red = mxGetPr(plhs[0]); green = mxGetPr(plhs[1]); blue = mxGetPr(plhs[2]); movie_rows = mxGetM(plhs[0]); } else { num_pix_doubles = (int)((theStream->h_size*theStream->v_size + 7)/8); if (n_req_frames > 0) plhs[0] = mxCreateDoubleMatrix(388 + num_pix_doubles, n_req_frames, mxREAL); if (plhs[0] == NULL) mexErrMsgTxt("Out of memory."); movie = mxGetPr(plhs[0]); movie_rows = mxGetM(plhs[0]); } if (!no_colormap) { plhs[1] = mxCreateDoubleMatrix(n_colors, 3, mxREAL); if (plhs[1] == NULL) mexErrMsgTxt("Out of memory."); cm = mxGetPr(plhs[1]); /* create a Matlab colormap */ /* fprintf(stderr, "About to create colormap\n");*/ for (j = 0; j < n_colors; j++) { /* fprintf(stderr, "Color: %d\n", j); */ ConvertColor(lum_values[(j/(CR_RANGE*CB_RANGE))%LUM_RANGE], cr_values[(j/CB_RANGE)%CR_RANGE], cb_values[j%CB_RANGE], &r, &g, &b); cm[j] = (double)r/255; cm[n_colors + j] = (double)g/255; cm[n_colors * 2 + j] = (double)b/255; } } } /*fprintf(stderr, "movie rows: %d\n", movie_rows);*/ /* get requested frames and store them in output martrix (matrices).*/ frames_stored = 0; for (j = 1; j <= last_frame; j++) { /*fprintf(stderr, "Generating frame #%d\n", j);*/ while (totNumFrames < j) { vid_status = mpegVidRsrc(0, theStream); if (vid_status == 1) { sprintf(err_text, "Frame(s) requested beyond last frame (%d).", totNumFrames); mexErrMsgTxt(err_text); } } /* store this frame wherever it has been requested */ for (i = 0; i < n_req_frames; i++) { if ((int)req_frames[i] == j) { frames_stored++; if (NewOutput) { mxArray *cdata = mxGetField(plhs[0], i, "cdata"); unsigned char *pr = (unsigned char *)mxGetPr(cdata); if (NewTrueColorOutput) { unsigned char *pg = pr + theStream->v_size * theStream->h_size; unsigned char *pb = pg + theStream->v_size * theStream->h_size; lum = 0; crom = 0; for (h = 0; h < theStream->h_size; h++) { lum = h; crom = h/2; for (k = 0; k < theStream->v_size; k++) { ConvertColor(*(curVidStream->current->luminance + lum), *(curVidStream->current->Cr + crom), *(curVidStream->current->Cb + crom), &r, &g, &b); *pr = (unsigned char)r; *pg = (unsigned char)g; *pb = (unsigned char)b; pr++; pg++; pb++; lum += theStream->h_size; if ((k % 2) == 1) { crom += theStream->h_size / 2; } } } } else { int index = 0; for (h = 0; h < theStream->h_size; h++) { for (k = 0; k < theStream->v_size; k++) { index = k * theStream->h_size + h; *pr = *(curVidStream->current->display + index); pr++; } } } } else { if (rgb_format) { lum = 0; crom = 0; for (h = 0; h < theStream->v_size; h++) { r_ptr = red + theStream->v_size*theStream->h_size * i + h; g_ptr = green + theStream->v_size*theStream->h_size * i + h; b_ptr = blue + theStream->v_size*theStream->h_size * i + h; for (k = 0; k < theStream->h_size; k++) { ConvertColor(*(curVidStream->current->luminance + lum), *(curVidStream->current->Cr + crom), *(curVidStream->current->Cb + crom), &r, &g, &b); *r_ptr = (double)r/255; *g_ptr = (double)g/255; *b_ptr = (double)b/255; r_ptr += theStream->v_size; g_ptr += theStream->v_size; b_ptr += theStream->v_size; lum++; if (k%2 == 0) crom++; } if (h%2 == 0) crom -= theStream->h_size / 2; } } else { /* create Matlab movie frame header */ m_ptr = (unsigned char *)(movie + (movie_rows * i)); *((double *)m_ptr) = curVidStream->h_size; m_ptr += sizeof(double); *((double *)m_ptr) = curVidStream->v_size; m_ptr += sizeof(double); *((double *)m_ptr) = 0; m_ptr += sizeof(double); if (MOVIE_GetClientByteOrder() == ELITTLE_ENDIAN) { *((int *)m_ptr) = 0; m_ptr += sizeof(int); *((int *)m_ptr) = LUM_RANGE*CB_RANGE*CR_RANGE; m_ptr += sizeof(int); } else { *((int *)m_ptr) = LUM_RANGE*CB_RANGE*CR_RANGE; m_ptr += sizeof(int); *((int *)m_ptr) = 0; m_ptr += sizeof(int); } m_ptr += 256*3*sizeof(float); /* copy frame data into movie matrix */ memcpy(m_ptr, curVidStream->current->display, (theStream->h_size*theStream->v_size)); if (MOVIE_GetClientByteOrder() == ELITTLE_ENDIAN) { for (k = 0; k < (movie_rows - 388); k++) { temp_int = FLIPBYTES(*((unsigned int *)m_ptr)); *((unsigned int *)m_ptr) = FLIPBYTES(*((unsigned int *)m_ptr + 1)); *((unsigned int *)m_ptr + 1) = temp_int; m_ptr += sizeof(double); } } } } } } } /* destroy the video stream */ DestroyVidStream(theStream); fclose(input);}/* *-------------------------------------------------------------- * * DoDitherImage -- * * Called when image needs to be dithered. Selects correct * dither routine based on info in ditherType. * * Results: * None. * * Side effects: * None. * *-------------------------------------------------------------- */voidDoDitherImage(unsigned char *l, unsigned char *Cr, unsigned char *Cb, unsigned char *disp, int h, int w){ Ordered2DitherImage(l, Cr, Cb, disp, h, w);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -