📄 mpeg2enc.c
字号:
#include <windows.h>#include <commdlg.h>#include <assert.h>#include <setjmp.h>#include <io.h>#include "resource.h"#define APPNAME "Mpeg2Encode" /* The name of this application */#define BASEWINDOWTITLE "MPEG2 Software Simulation" /* The title bar text */#define MAXFILENAME 256 /* maximum length of file pathname */#define MAXCUSTFILTER 40 /* maximum size of custom filter buffer */// MS Windows related variablesLRESULT CALLBACK WndProc(HWND hWnd, // window handle UINT message, // type of message WPARAM uParam, // additional information LPARAM lParam); // additional informationstatic HINSTANCE hInst; // current instancestatic HWND ghWnd =NULL; // handle to main windowstatic char szFileName[MAXFILENAME]="";static unsigned short gusState;static jmp_buf env;extern int __argc;extern char **__argv;#define argc __argc#define argv __argv#define START 0#define READY 1#define PLAYING 2#define PAUSED 3#define FINISH 4#define STOP 5#include <stdio.h>#include <stdlib.h>#define GLOBAL /* used by global.h */#include "config.h"#include "global.h"/* private prototypes */static void init _ANSI_ARGS_((void));static void readparmfile _ANSI_ARGS_((char *fname));static void readquantmat _ANSI_ARGS_((void));// MS Windows related functions///////////////////////////////////////////////////////////////////////////////// yield function to simulate multitasking on MS Windows//void myYield(void){ MSG msg; if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg);// Translates virtual key codes DispatchMessage(&msg); // Dispatches message to window } if (gusState==PAUSED) longjmp(env,error);}//// initializations//static BOOL InitApplication(HINSTANCE hInstance){ WNDCLASS wc; // Fill in window class structure with parameters that describe the // main window. wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;// Class style(s). wc.lpfnWndProc = (WNDPROC)WndProc; // Window Procedure wc.cbClsExtra = 0; // No per-class extra data. wc.cbWndExtra = 0; // No per-window extra data. wc.hInstance = hInstance; // Owner of this class wc.hIcon = LoadIcon (hInstance, APPNAME); // Icon name from .RC wc.hCursor = LoadCursor(NULL, IDC_ARROW);// Cursor wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); // Default color wc.lpszMenuName = APPNAME; // Menu name from .RC wc.lpszClassName = APPNAME; // Name to register as // Register the window class and return success/failure code. return (RegisterClass(&wc));}static BOOL InitInstance(HINSTANCE hInstance,int nCmdShow){ // Save the instance handle in static variable, which will be used in // many subsequence calls from this application to Windows. hInst = hInstance; // Store instance handle in our global variable // Create a main window for this application instance. ghWnd = CreateWindow(APPNAME, // See RegisterClass() call. BASEWINDOWTITLE, // Text for window title bar. WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX, // Window style. CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, // Use default positioning NULL, // Overlapped windows have no parent. NULL, // Use the window class menu. hInstance, // This instance owns this window. NULL // We don't use any data in our WM_CREATE ); // If window could not be created, return "failure" if (!ghWnd) return (FALSE); // Make the window visible; update its client area; and return "success" ShowWindow(ghWnd, nCmdShow); // Show the window UpdateWindow(ghWnd); // Sends WM_PAINT message return (TRUE); // We succeeded...}//// prompts the user for the efile name//static BOOL AskUserFileName(char szFileName[],char szFileTitle[], char szFilterSpec[], BOOL bSave){ /* new variables for common dialogs */ static char szInitialDirectory[MAXFILENAME]=""; OPENFILENAME ofn; memset(&ofn,0,sizeof(ofn)); /* fill in non-variant fields of OPENFILENAME struct. */ ofn.lStructSize = sizeof(OPENFILENAME); ofn.hwndOwner = ghWnd; ofn.lpstrFilter = szFilterSpec; ofn.lpstrCustomFilter = NULL; ofn.nMaxCustFilter = 0; ofn.nFilterIndex = 0; ofn.lpstrFile = szFileName; ofn.nMaxFile = MAXFILENAME; if (!szInitialDirectory[0]) GetModuleFileName(hInst, szInitialDirectory, MAXFILENAME); // strip the file name *(LPSTR)strrchr(szInitialDirectory,'\\')=0; ofn.lpstrInitialDir = szInitialDirectory; ofn.lpstrFileTitle = szFileTitle; ofn.nMaxFileTitle = MAXFILENAME; ofn.lpstrTitle = NULL; ofn.lpstrDefExt = "MPG"; ofn.Flags = 0; if (bSave) { if (!GetSaveFileName ((LPOPENFILENAME)&ofn)) return FALSE; } else if (!GetOpenFileName ((LPOPENFILENAME)&ofn)) return FALSE; // save the current directory lstrcpy(szInitialDirectory,szFileName); return TRUE;}/********************************************************************************* * * About box ***********************************************************************************/BOOL CALLBACK About(HWND hDlg, // window handle of the dialog box UINT message, // type of message WPARAM uParam, // message-specific information LPARAM lParam){ switch (message) { case WM_COMMAND: // message: received a command if (LOWORD(uParam) == IDOK || LOWORD(uParam) == IDCANCEL) { EndDialog(hDlg, TRUE); // Exit the dialog return (TRUE); } break; } return (FALSE); // Didn't process the message lParam; // This will prevent 'unused formal parameter' warnings}//// Main window procedure//LRESULT CALLBACK WndProc(HWND hWnd, // window handle UINT message, // type of message WPARAM uParam, // additional information LPARAM lParam) // additional information{ switch (message) { case WM_CLOSE: if (gusState==PLAYING) gusState = PAUSED; myYield(); return (DefWindowProc(hWnd, message, uParam, lParam)); break; case WM_DESTROY: // message: window being destroyed if (gusState==PLAYING) gusState = PAUSED; myYield(); PostQuitMessage(0); break; case WM_COMMAND: // message: command from application menu { int wmId, wmEvent; wmId = LOWORD(uParam); wmEvent = HIWORD(uParam); switch (wmId) { case IDM_FILE_OPEN_PARAMS: { static char szFilterSpec[]="Encoder Parameter File\0*.PAR\0\All Files\0\*.*\0\0"; char szWindowTitle[80]; char szFileTitle[MAXFILENAME]; szFileName[0]=0; if (!AskUserFileName (szFileName,szFileTitle,szFilterSpec,0)) return FALSE; lstrcpy(szWindowTitle, BASEWINDOWTITLE); lstrcat(szWindowTitle, " - "); lstrcat(szWindowTitle, szFileTitle); SetWindowText(hWnd, szFileTitle); // enable play menu EnableMenuItem(GetMenu(hWnd), IDM_RUN, MF_ENABLED); gusState = READY; break; } case IDM_RUN: { static char szFilterSpec[]="MPEG2 Video Files\0*.M2V\0MPEG1 Video Files\0*.M1V\0MPEG Files\0*.MPG\0All Files (*.*)\0*.*\0"; char szWindowTitle[256]; char szOldWindowTitle[256]; char szTargetName[MAXFILENAME]; char szFileTitle [MAXFILENAME]; char *argv[]={NULL,szFileName,szTargetName}; int e; szTargetName[0]=0; if (!AskUserFileName (szTargetName,szFileTitle,szFilterSpec,1)) return FALSE; GetWindowText(hWnd, szOldWindowTitle, 256); lstrcpy(szWindowTitle, szOldWindowTitle); lstrcat(szWindowTitle, " - "); lstrcat(szWindowTitle, szTargetName); SetWindowText(hWnd, szWindowTitle); // disable play menu while playing EnableMenuItem(GetMenu(hWnd), IDM_STOP, MF_ENABLED); EnableMenuItem(GetMenu(hWnd), IDM_RUN, MF_GRAYED | MF_DISABLED); EnableMenuItem(GetMenu(hWnd), IDM_FILE_OPEN_PARAMS, MF_GRAYED | MF_DISABLED); // play the movie. It returns after the playing ended gusState = PLAYING; e=setjmp(env); if (!e) main(3,argv); gusState = STOP; // disable stop menu EnableMenuItem(GetMenu(hWnd), IDM_STOP, MF_GRAYED | MF_DISABLED); EnableMenuItem(GetMenu(hWnd), IDM_RUN, MF_ENABLED); EnableMenuItem(GetMenu(hWnd), IDM_FILE_OPEN_PARAMS, MF_ENABLED); SetWindowText(hWnd, szOldWindowTitle); break; } case IDM_STOP: if (gusState==PLAYING) gusState=PAUSED; // force a task switch to allow the decoder to see the stop command myYield(); break; case IDM_EXIT: if (gusState==PLAYING) gusState=PAUSED; // force a task switch to allow the decoder to see the stop command myYield(); SendMessage(hWnd, WM_CLOSE, 0, 0l); break; case IDM_ABOUT: { FARPROC lpProcAbout; // pointer to the "About" function lpProcAbout = MakeProcInstance((FARPROC)About, hInst); DialogBox(hInst, // current instance "AboutBox", // dlg resource to use hWnd, // parent handle (DLGPROC)lpProcAbout); // About() instance address FreeProcInstance(lpProcAbout); break; } } break; } default: // Passes it on if unproccessed return (DefWindowProc(hWnd, message, uParam, lParam)); } return (0);}//// program entry point//int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){ MSG msg; if (!hPrevInstance) // Other instances of app running? if (!InitApplication(hInstance)) // Initialize shared things return (FALSE); // Exits if unable to initialize /* Perform initializations that apply to a specific instance */ if (!InitInstance(hInstance, nCmdShow)) return (FALSE); gusState = STOP; /* 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}void Output(char *pFormatInfo, ...){ va_list marker; char szPrintBuffer[512]; va_start(marker, pFormatInfo); wvsprintf(szPrintBuffer, pFormatInfo, marker); OutputDebugString(szPrintBuffer); myYield();}void message(char *pFormatInfo, ...){ va_list marker; char szPrintBuffer[512]; va_start(marker, pFormatInfo); wvsprintf(szPrintBuffer, pFormatInfo, marker); { HDC hDC; hDC=GetDC(ghWnd); TextOut(hDC,10,10,szPrintBuffer,strlen(szPrintBuffer)); ReleaseDC(ghWnd,hDC); } myYield();}int main(argc,argv)int argc;char *argv[];{ if (argc!=3) { printf("\n%s, %s\n",version,author); printf("Usage: mpeg2encode in.par out.m2v\n"); exit(0); } /* read parameter file */ readparmfile(argv[1]); /* read quantization matrices */ readquantmat(); /* open output file */ if (!(outfile=fopen(argv[2],"wb"))) { sprintf(errortext,"Couldn't create output file %s",argv[1]); error(errortext); } init(); putseq(); fclose(outfile); fclose(statfile); return 0;}static void init(){ int i, size; static int block_count_tab[3] = {6,8,12}; initbits(); init_fdct(); init_idct(); /* Non-linear qscale not yet implemented */ q_scale_type=0; /* round picture dimensions to nearest multiple of 16 or 32 */ mb_width = (horizontal_size+15)/16; mb_height = prog_seq ? (vertical_size+15)/16 : 2*((vertical_size+31)/32); mb_height2 = fieldpic ? mb_height>>1 : mb_height; /* for field pictures */ width = 16*mb_width; height = 16*mb_height; chrom_width = (chroma_format==CHROMA444) ? width : width>>1; chrom_height = (chroma_format!=CHROMA420) ? height : height>>1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -