📄 mpeg2dec.c
字号:
// start-up initialization verbose = 0;#ifdef DISPLAY outtype = T_X11; hDC = GetDC(ghWnd); if (GetDeviceCaps(hDC,BITSPIXEL)>=24) { CheckMenuItem(GetMenu(ghWnd), IDM_DISPLAY24, MF_CHECKED); nDitherType=24; } else { CheckMenuItem(GetMenu(ghWnd), IDM_DISPLAY8, MF_CHECKED); nDitherType=8; }#else outtype=T_YUV; CheckMenuItem(GetMenu(ghWnd), IDM_YUV_FILE, MF_CHECKED);#endif framestoreflag = 1; sflag = 0; refidct = 0; ld = &base; /* select base layer context */ options(&__argc,&__argv);#ifdef DISPLAY if (outtype==T_X11) outputname = ""; else#endif outputname="rec%d"; /* Acquire and dispatch messages until a WM_QUIT message is received. */ while (GetMessage(&msg, // message structure NULL, // handle of window receiving the message 0, // lowest message to examine 0)) // highest message to examine { TranslateMessage(&msg);// Translates virtual key codes DispatchMessage(&msg); // Dispatches message to window } return (msg.wParam); // Returns the value from PostQuitMessage lpCmdLine; // This will prevent 'unused formal parameter' warnings}#ifdef DISPLAY//// display initialization//extern int convmat[8][4]; // defined in display.cstatic void init_display(void){ pbmi = (PBITMAPINFO)malloc(sizeof(BITMAPINFOHEADER) + 240 * sizeof(RGBQUAD)); pbmi->bmiHeader.biSize = (LONG)sizeof(BITMAPINFOHEADER); pbmi->bmiHeader.biPlanes = 1; pbmi->bmiHeader.biCompression = 0l; pbmi->bmiHeader.biSizeImage = 0l; pbmi->bmiHeader.biXPelsPerMeter = 0l; pbmi->bmiHeader.biYPelsPerMeter = 0l; pbmi->bmiHeader.biClrUsed = 240; pbmi->bmiHeader.biClrImportant = 240; pbmi->bmiHeader.biBitCount = nDitherType; pbmi->bmiHeader.biWidth = coded_picture_width ; pbmi->bmiHeader.biHeight= coded_picture_height; if ( pbmi->bmiHeader.biBitCount==8 ) { // for 8 BPP, build the color palette LOGPALETTE *plgpl; short *pPalIndex; int crv, cbu, cgu, cgv; int y, u, v; int i; plgpl = (LOGPALETTE*) malloc(sizeof(LOGPALETTE) + 240 * sizeof(PALETTEENTRY)); plgpl->palNumEntries = 240; plgpl->palVersion = 0x300; pPalIndex=(short *)pbmi->bmiColors; /* matrix coefficients */ crv = convmat[matrix_coefficients][0]; cbu = convmat[matrix_coefficients][1]; cgu = convmat[matrix_coefficients][2]; cgv = convmat[matrix_coefficients][3]; for (i=16; i<240; i++) { /* color space conversion */ y = 16*((i>>4)&15) + 8; u = 32*((i>>2)&3) - 48; v = 32*(i&3) - 48; y = 76309 * (y - 16); /* (255/219)*65536 */ plgpl->palPalEntry[i].peRed = clp[(y + crv*v + 32768)>>16]; plgpl->palPalEntry[i].peGreen = clp[(y - cgu*u -cgv*v + 32768)>>16]; plgpl->palPalEntry[i].peBlue = clp[(y + cbu*u + 32786)>>16]; pPalIndex[i]=i; } hpal = CreatePalette(plgpl); free(plgpl); hPalPrev=SelectPalette(hDC,hpal,FALSE); RealizePalette(hDC); }}//// display clean-up//static void exit_display(){ if (pbmi) { free(pbmi); pbmi=NULL; } if (hPalPrev) { SelectPalette(hDC,hPalPrev,FALSE); DeleteObject(hpal); hPalPrev=NULL; }}//// display the image on the MS Windows screen//void display_image(dithered_image)unsigned char *dithered_image;{ /* display dithered image */ SetDIBitsToDevice(hDC,0,0,coded_picture_width,coded_picture_height, 0,0,0,coded_picture_height,dithered_image,pbmi,DIB_PAL_COLORS);}#endif//// decode a file and store and/or display it//static void play_movie(void){ int framenum; /* open MPEG input file(s) */ if ((base.infile=open(szFileName,O_RDONLY|O_BINARY))<0) { sprintf(errortext,"Input file %s not found\n",szFileName); error(errortext); } initbits(); if (getheader()) { RECT wr,cr; int d; // size window to film size SetRect(&cr,0,0,horizontal_size,vertical_size); wr=cr; AdjustWindowRect(&wr,GetWindowLong(ghWnd,GWL_STYLE),TRUE); OffsetRect(&wr,-wr.left,-wr.top); SetWindowPos(ghWnd,HWND_TOP,0,0,wr.right,wr.bottom,SWP_NOMOVE); // for QSIF movies the menu will wrap around so we need // to increase the height of the window GetClientRect(ghWnd, &wr); d=cr.bottom-(wr.bottom-wr.top); if (d>0) { GetWindowRect(ghWnd, &wr); SetWindowPos(ghWnd,HWND_TOP,0,0,wr.right-wr.left,wr.bottom-wr.top+d,SWP_NOMOVE); } initdecoder(); if (outtype==T_X11) { init_dither(nDitherType); init_display(); } framenum = 0; do { getpicture(framenum); if (!secondfield) framenum++; // give the user a chance to perform something myYield(); if (gusState==STOP) break; } while (getheader()); if (framenum!=0) { /* put last frame */ putlast(framenum); } if (outtype==T_X11) exit_display(); } close(base.infile);}///////////////////////////////////////////////////////////////////////// MPEG related functions//// (portable)//static void initdecoder(){ int i, cc, size; static int blk_cnt_tab[3] = {6,8,12}; /* check scalability mode of enhancement layer */ if (twostreams && enhan.scalable_mode!=SC_SNR && !(base.scalable_mode==SC_DP && base.scalable_mode==SC_DP)) error("unsupported scalability mode\n"); /* clip table */ if (!(clp=(unsigned char *)malloc(1024))) error("malloc failed\n"); clp += 384; for (i=-384; i<640; i++) clp[i] = (i<0) ? 0 : ((i>255) ? 255 : i); /* force MPEG-1 parameters */ if (!base.mpeg2) { prog_seq = 1; prog_frame = 1; pict_struct = FRAME_PICTURE; frame_pred_dct = 1; chroma_format = CHROMA420; matrix_coefficients = 5; } /* round to nearest multiple of coded macroblocks */ mb_width = (horizontal_size+15)/16; mb_height = (base.mpeg2 && !prog_seq) ? 2*((vertical_size+31)/32) : (vertical_size+15)/16; coded_picture_width = 16*mb_width; coded_picture_height = 16*mb_height; chrom_width = (chroma_format==CHROMA444) ? coded_picture_width : coded_picture_width>>1; chrom_height = (chroma_format!=CHROMA420) ? coded_picture_height : coded_picture_height>>1; blk_cnt = blk_cnt_tab[chroma_format-1]; for (cc=0; cc<3; cc++) { if (cc==0) size = coded_picture_width*coded_picture_height; else size = chrom_width*chrom_height; if (!(refframe[cc] = (unsigned char *)malloc(size))) error("malloc failed\n"); if (!(oldrefframe[cc] = (unsigned char *)malloc(size))) error("malloc failed\n"); if (!(auxframe[cc] = (unsigned char *)malloc(size))) error("malloc failed\n"); if (base.scalable_mode==SC_SPAT) { /* this assumes lower layer is 4:2:0 */ if (!(llframe0[cc] = (unsigned char *)malloc((llw*llh)/(cc?4:1)))) error("malloc failed\n"); if (!(llframe1[cc] = (unsigned char *)malloc((llw*llh)/(cc?4:1)))) error("malloc failed\n"); } } if (base.scalable_mode==SC_SPAT) { if (!(lltmp = (short *)malloc(llw*((llh*vn)/vm)*sizeof(short)))) error("malloc failed\n"); } /* IDCT */ if (refidct) init_idctref(); else init_idct();}void error(text)char *text;{ fprintf(stderr,text); exit(1);}/* compliance warning messages to user, but don't exit() */void warning(text)char *text;{ if (!quiet) { fprintf(stderr,"%s\n",text); }}/* trace output */void printbits(code,bits,len)int code,bits,len;{ int i; for (i=0; i<len; i++) printf("%d",(code>>(bits-1-i))&1);}/* option processing */static void options(argcp,argvp)int *argcp;char **argvp[];{ while (*argcp>1 && (*argvp)[1][0]=='-') { while ((*argvp)[1][1]) { switch (toupper((*argvp)[1][1])) { case 'V': verbose = getval(*argvp); break; case 'O': outtype = getval(*argvp);#ifdef DISPLAY if (outtype==T_X11HIQ) { hiQdither = 1; outtype=T_X11; }#endif break; case 'F': framestoreflag = 1; break; case 'S': sflag = 1; break; case 'R': refidct = 1; break; case 'T': trace = 1; break; case 'Q': quiet = 1; break; default: fprintf(stderr,"undefined option -%c ignored\n",(*argvp)[1][1]); } (*argvp)[1]++; } (*argvp)++; (*argcp)--; } if (sflag) { /* input file for spatial prediction */ llinputname = (*argvp)[1]; (*argvp)++; (*argcp)--; }#ifdef DISPLAY if (outtype==T_X11) { framestoreflag = 1; /* two avoid calling dither() twice */ (*argcp)++; /* fake outfile parameter */ }#endif#if 0 if (*argcp!=3 && *argcp!=4) { printf("\n%s, %s\n",version,author); printf("Usage: mpeg2decode {options} input.m2v {upper.m2v} {outfile}\n\Options: -vn verbose output (n: level)\n\ -on output format (0: YUV, 1: SIF, 2: TGA, 3:PPM, 4:X11, 5:X11 HiQ)\n\ -f store interlaced video in frame format\n\ -q disable warnings to stderr\n\ -r use double precision reference IDCT\n\ -s infile spatial scalable sequence\n\ -t enable low level tracing\n"); exit(0); }#endif}static int getval(argv)char *argv[];{ int val; if (sscanf(argv[1]+2,"%d",&val)!=1) return 0; while (isdigit(argv[1][2])) argv[1]++; return val;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -