谁能帮我翻译成DELPHI的呀,急,我的全部家当都给你了(145分)

  • 主题发起人 主题发起人 seddy
  • 开始时间 开始时间
S

seddy

Unregistered / Unconfirmed
GUEST, unregistred user!
我现在只有145分了 交个朋友嘛 谢谢大家了
是个分割WMV和ASF文件的程序 帮我分析和翻译一下主要部分就好了


#include <stdio.h>
#include <io.h>
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <string.h>
#include "asffixres.h"
#include <shellapi.h>
#include <process.h>
#define FILEPATH_SIZE 512
#define PROGRESS_SCALE 200
#define MOVEME_RESET 0
#define MOVEME_REFRESH 1
#define MOVEME_MOVE 2
#define STATUS_INFORMATION 1
#define STATUS_ERROR 2
#define STATUS_CRITICALERROR 3
#define STATUS_PROGRESS 4
#define STATUS_RESET 5
unsigned char working, pause, optionmode, brutemode,callindex=1,saveas;
char c_dir[256];
HWND hwndGlobalDlg;
HWND hwndProgress;
HWND hwndList;
HMENU hmenuPopup;
HIMAGELIST himlIcons;
HANDLE WorkingThread;
POINT MinTrackSize, MaxTrackSize;
HICON hicon;
BOOL summary_flag;
void MoveMe(int MoveMeType, int x, int y);
unsigned long mymain(char *argv[]);
void get_long(unsigned char *pos, unsigned long *val);
void put_long(unsigned char *pos, unsigned long val);
void get_quad(unsigned char *pos, unsigned long *hi, unsigned long *lo);
void put_quad(unsigned char *pos, unsigned long hi, unsigned long lo);
void showstatus (int StatusType, char *Message, ...);
BOOL set_timecode(unsigned char *Buffer,int frames,int offset);
static BOOL CALLBACK DialogFunc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam);
int APIENTRY WinMain(HINSTANCE hinst, HINSTANCE hinstPrev, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASS wc;
memset(&amp;wc,0,sizeof(wc));
wc.lpfnWndProc = DefDlgProc;
wc.cbWndExtra = DLGWINDOWEXTRA;
wc.hInstance = hinst;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
wc.lpszClassName = "ASFFix";
RegisterClass(&amp;wc);
hicon = LoadIcon(hinst, MAKEINTRESOURCE(IDI_ICON1));
return DialogBox(hinst, MAKEINTRESOURCE(IDD_MAINDIALOG), NULL, (DLGPROC) DialogFunc);
}
static int InitializeApp(HWND hDlg, WPARAM wParam, LPARAM lParam)
{
static RECT wndpl;
LVCOLUMN lvc;
/* Initialize global variables */
hwndGlobalDlg = hDlg;
working = 0;
pause = 0;
optionmode = 0;
hwndProgress = GetDlgItem(hDlg, ID_PROGRESS);
hwndList = GetDlgItem(hDlg, ID_LIST);
GetWindowRect(hwndGlobalDlg, &amp;wndpl);
wndpl.right -= wndpl.left;
wndpl.bottom -= wndpl.top;
MinTrackSize.x = wndpl.right;
MaxTrackSize.x = wndpl.right;
MaxTrackSize.y = 0;
MinTrackSize.y = wndpl.bottom - 159;
/* Initialize controls */
/* Popup menu */
hmenuPopup = CreatePopupMenu();
InsertMenu(hmenuPopup, 0, MF_BYCOMMAND, ID_CLEAR, "Clear");
/* Text */
SetDlgItemText(hDlg, ID_INPUT, "将wmv,asf文件拖放到这里!");
/* Progress bar */
SendMessage(hwndProgress, PBM_SETRANGE, 0, MAKELPARAM(0, PROGRESS_SCALE));
/* Always on top */
SetWindowPos(hDlg, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
//Check callindex
SendMessage(GetDlgItem(hDlg,ID_CALLGBSINDEX),BM_SETCHECK,callindex,0);
//get current dir
int i,j;
GetModuleFileName(NULL,c_dir,256);
j=strlen(c_dir)-1;
for(i=j;i>=0;i--)
if(c_dir=='//'){ c_dir=0;
break;
}
/* ListView */
lvc.mask = LVCF_TEXT;
lvc.fmt = LVCFMT_LEFT;
lvc.cx = LVSCW_AUTOSIZE;
lvc.pszText = "YouCantSeeThis";
lvc.iSubItem = 0;
ListView_InsertColumn(hwndList, 0, &amp;lvc);
ListView_SetColumnWidth(hwndList, 0, 500);
SendMessage(hwndList, LVM_SETEXTENDEDLISTVIEWSTYLE, (WPARAM)0, (LPARAM)LVS_EX_GRIDLINES);
// ListView_SetImageList(hwndList, himlIcons, LVSIL_NORMAL);
SendMessage(hwndList, LVM_SETBKCOLOR, 0, (LPARAM)0x0);
SendMessage(hwndList, LVM_SETTEXTBKCOLOR, 0, (LPARAM)0x0);
SendMessage(hwndList, LVM_SETTEXTCOLOR, 0, (LPARAM)0xff00);
MoveMe(MOVEME_RESET, 0, 0);
return 1;
}
static BOOL CALLBACK DialogFunc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
static OPENFILENAME ofn;
static char filepath[2][FILEPATH_SIZE];
static char *filepathPtr[2] = {filepath[0], filepath[1]};
static UINT WorkingThreadID;
char s[256];
switch (msg)
{
case WM_INITDIALOG:
InitializeApp(hwndDlg, wParam, lParam);
return TRUE;
case WM_PAINT:
DrawIcon((HDC)wParam,0,0,hicon);
return FALSE;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case ID_START:
ListView_DeleteAllItems(hwndList);
/* Spawn a new thread todo
the work */
summary_flag=FALSE;
if(!working)
{
if (GetDlgItemText(hwndDlg, ID_INPUT, filepath[0], sizeof(filepath[0])) != 0)
{
//callindex require saveas
if(callindex &amp;&amp;
!saveas){
//MessageBox(hwndDlg,"Please select 'Save As'!","note",0);
MessageBox(hwndDlg,"请使用'另存为'指定输出文件!","注意",0);
break;
}
/* Prepare to work */
//SetDlgItemText(hwndDlg, ID_CANCEL, "&amp;Cancel");
//SetDlgItemText(hwndDlg, ID_START, "&amp;Pause");
SetDlgItemText(hwndDlg, ID_CANCEL, "退出");
SetDlgItemText(hwndDlg, ID_START, "暂停");
EnableWindow(GetDlgItem(hwndDlg, ID_OPTION), FALSE);
/* Spawn the working thread */
if(WorkingThread) CloseHandle(WorkingThread);
WorkingThread = CreateThread(NULL, 0,(LPTHREAD_START_ROUTINE)mymain, filepathPtr, 0, (unsigned long *)&amp;WorkingThreadID);
}
}
else
{
/* Suspend &amp;
Resume the thread */
if(pause)
{
pause = 0;
//SetDlgItemText(hwndDlg, ID_START, "&amp;Pause");
SetDlgItemText(hwndDlg, ID_START, "暂停");
ResumeThread(WorkingThread);
}
else
{
pause = 1;
//SetDlgItemText(hwndDlg, ID_START, "Con&amp;tinue");
SetDlgItemText(hwndDlg, ID_START, "继续");
SuspendThread(WorkingThread);
}
}
break;
case ID_SELFILE:
// ListView_DeleteAllItems(hwndList);
/* Call a OpenFile Dialog */
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwndDlg;
ofn.lpstrFilter = "*.asf;*.asx;*.wmv;*.wma/x0*.asf;*.asx;*.wmv;*.wma/x0*.*/x0*.*/x0/x0";
ofn.lpstrCustomFilter = NULL;
ofn.nFilterIndex = 0;
ofn.lpstrFile = filepath[0];
ofn.nMaxFile = sizeof(filepath[0]);
ofn.lpstrFileTitle = NULL;
ofn.lpstrInitialDir = NULL;
ofn.lpstrTitle = NULL;
ofn.Flags = OFN_EXPLORER | OFN_HIDEREADONLY;
if(GetOpenFileName(&amp;ofn))
{
/* Put the result to the Input Field */
SetDlgItemText(hwndDlg, ID_INPUT, filepath[0]);
//ONLY get file summary
summary_flag=TRUE;
if(!working)
{
if (GetDlgItemText(hwndDlg, ID_INPUT, filepath[0], sizeof(filepath[0])) != 0)
{
//Disable "start" button when summary
EnableWindow(GetDlgItem(hwndDlg, ID_START), FALSE);
/* Spawn the working thread */
if(WorkingThread) CloseHandle(WorkingThread);
WorkingThread = CreateThread(NULL, 0,(LPTHREAD_START_ROUTINE)mymain, filepathPtr, 0, (unsigned long *)&amp;WorkingThreadID);
}
}
}
break;
case ID_SAVEAS:
saveas= !saveas;
if(saveas)
{
/* Call a SaveAs Dialog */
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwndDlg;
ofn.lpstrFilter = "*.asf;*.asx;*.wmv;*.wma/x0*.asf;*.asx;*.wmv;*.wma/x0*.*/x0*.*/x0/x0";
ofn.lpstrCustomFilter = NULL;
ofn.nFilterIndex = 0;
ofn.lpstrFile = filepath[1];
ofn.nMaxFile = sizeof(filepath[1]);
ofn.lpstrFileTitle = NULL;
ofn.lpstrInitialDir = NULL;
ofn.lpstrTitle = NULL;
ofn.Flags = OFN_EXPLORER | OFN_HIDEREADONLY;
if(GetSaveFileName(&amp;ofn))
{
/* Somethig selected, that's ok */
SendMessage(GetDlgItem(hwndDlg, ID_SAVEAS), BM_SETCHECK, BST_CHECKED, 0);
}
else
{
/* No file selected, brutemode must be cleared */
brutemode = 0;
SendMessage(GetDlgItem(hwndDlg, ID_BRUTE), BM_SETCHECK, BST_UNCHECKED, 0);
}
}
else
{
/* Save As deselected, clear status */
memset(filepath[1], 0, sizeof(filepath[1]));
SendMessage(GetDlgItem(hwndDlg, ID_SAVEAS), BM_SETCHECK, BST_UNCHECKED, 0);
/* Clear brutemode because it rely on "Save As" */
brutemode = 0;
SendMessage(GetDlgItem(hwndDlg, ID_BRUTE), BM_SETCHECK, BST_UNCHECKED, 0);
}
SetDlgItemText(hwndDlg, ID_SAVEASINPUT, filepath[1]);
break;
case ID_BRUTE:
if(brutemode)
{
brutemode = 0;
SendMessage(GetDlgItem(hwndDlg, ID_BRUTE), BM_SETCHECK, BST_UNCHECKED, 0);
}
else
{
brutemode = 1;
SendMessage(GetDlgItem(hwndDlg, ID_BRUTE), BM_SETCHECK, BST_CHECKED, 0);
/* If no SaveAs selected now,call the routine to select one */
if(strlen(filepath[1]) == 0)
SendMessage(hwndDlg, WM_COMMAND, (WPARAM)ID_SAVEAS, 0);
}
break;

case ID_CALLGBSINDEX:
callindex= !callindex;
SendMessage(GetDlgItem(hwndDlg,ID_CALLGBSINDEX),BM_SETCHECK,callindex,0);
break;
case ID_OPTION:
if(optionmode)
{
/* Deactivate option mode */
optionmode = 0;
/*ShowWindow(GetDlgItem(hwndDlg, ID_START), SW_SHOW);
ShowWindow(GetDlgItem(hwndDlg, ID_CANCEL), SW_SHOW);
ShowWindow(GetDlgItem(hwndDlg, ID_SELFILE), SW_SHOW);
ShowWindow(GetDlgItem(hwndDlg, ID_PROGRESS), SW_SHOW);
ShowWindow(GetDlgItem(hwndDlg, ID_INPUT), SW_SHOW);
ShowWindow(GetDlgItem(hwndDlg, ID_SAVEASINPUT), SW_HIDE);
ShowWindow(GetDlgItem(hwndDlg, ID_URL), SW_HIDE);
ShowWindow(GetDlgItem(hwndDlg, ID_SAVEAS), SW_HIDE);
ShowWindow(GetDlgItem(hwndDlg, ID_BRUTE), SW_HIDE);
*/
SetDlgItemText(hwndDlg, ID_OPTION, "&amp;Option");
}
else
{
/* Activate option mode */
optionmode = 1;
/* ShowWindow(GetDlgItem(hwndDlg, ID_START), SW_HIDE);
ShowWindow(GetDlgItem(hwndDlg, ID_CANCEL), SW_HIDE);
ShowWindow(GetDlgItem(hwndDlg, ID_SELFILE), SW_HIDE);
ShowWindow(GetDlgItem(hwndDlg, ID_PROGRESS), SW_HIDE);
ShowWindow(GetDlgItem(hwndDlg, ID_INPUT), SW_HIDE);
ShowWindow(GetDlgItem(hwndDlg, ID_SAVEAS), SW_SHOW);
ShowWindow(GetDlgItem(hwndDlg, ID_BRUTE), SW_SHOW);
ShowWindow(GetDlgItem(hwndDlg, ID_SAVEASINPUT), SW_SHOW);
ShowWindow(GetDlgItem(hwndDlg, ID_URL), SW_SHOW);
*/
SetDlgItemText(hwndDlg, ID_OPTION, "&amp;Okay");
}
break;
case ID_CLEAR:
/* Clear ListView's content if the clear menu item selected */
showstatus(STATUS_RESET, "");
break;
case ID_CANCEL:
/* If a file's being processed, cancel it */
/* else
exit program */
if (working)
{
working = 0;
if(pause == 1)
ResumeThread(WorkingThread);
break;
}
EndDialog(hwndDlg,0);
break;
case ID_HELP:
sprintf(s,"%s//zasffix.htm",c_dir);
ShellExecute(hwndDlg,"open",s,NULL,NULL, SW_SHOWNORMAL);
break;
}
return 1;
case WM_DROPFILES:
{
/* Parse Dropped file */
if(optionmode)
{
DragQueryFileA((struct HDROP__ *)wParam, 0, filepath[1], sizeof(filepath[1]));
SetDlgItemText(hwndDlg, ID_SAVEASINPUT, filepath[1]);
SendMessage(GetDlgItem(hwndDlg, ID_SAVEAS), BM_SETCHECK, BST_CHECKED, 0);
}
else
{
DragQueryFileA((struct HDROP__ *) wParam, 0, filepath[0], sizeof(filepath[0]));
SetDlgItemText(hwndDlg, ID_INPUT, filepath[0]);
}
//ONLY get file summary
summary_flag=TRUE;
if(!working)
{
if (GetDlgItemText(hwndDlg, ID_INPUT, filepath[0], sizeof(filepath[0])) != 0)
{
//Disable "start" button when summary
EnableWindow(GetDlgItem(hwndDlg, ID_START), FALSE);
/* Spawn the working thread */
if(WorkingThread) CloseHandle(WorkingThread);
WorkingThread = CreateThread(NULL, 0,(LPTHREAD_START_ROUTINE)mymain, filepathPtr, 0, (unsigned long *)&amp;WorkingThreadID);
}
}
return 1;
}
case WM_NOTIFY:
{
/* Right Click in ListView */
if(((NMHDR *)lParam)->code == NM_RCLICK &amp;&amp;
wParam == ID_LIST)
{
/* Show the popup menu */
POINT Coord;
GetCursorPos(&amp;Coord);
TrackPopupMenuEx(hmenuPopup, TPM_LEFTALIGN, Coord.x, Coord.y, hwndDlg, NULL);
}
return 1;
}
case WM_GETMINMAXINFO:
{
static MINMAXINFO *MinMaxInfo;
MinMaxInfo = (MINMAXINFO *)lParam;
if (MinTrackSize.x)
(MinMaxInfo->ptMinTrackSize).x = MinTrackSize.x;
if (MinTrackSize.y)
(MinMaxInfo->ptMinTrackSize).y = MinTrackSize.y;
if (MaxTrackSize.x)
(MinMaxInfo->ptMaxTrackSize).x = MaxTrackSize.x;
if (MaxTrackSize.y)
(MinMaxInfo->ptMaxTrackSize).y = MaxTrackSize.y;
return 1;
}
case WM_SIZING:
{
/* Resize ListView */
static RECT ClientRect;
GetClientRect(hwndDlg, &amp;ClientRect);
ClientRect.right -= ClientRect.left;
ClientRect.bottom -= ClientRect.top + 38;
SetWindowPos(hwndList, HWND_TOP, ClientRect.left, ClientRect.top,
ClientRect.right, ClientRect.bottom, SWP_NOMOVE);
AdjustWindowRect(&amp;ClientRect, 0, FALSE);
return 1;
}
case WM_CLOSE:
EndDialog(hwndDlg,0);
return TRUE;
}
return FALSE;
}
void MoveMe(int MoveMeType, int x, int y)
{
static UINT MyWidth, MyHeight;
static int StartX, StartY;
static RECT wndpl;
switch (MoveMeType)
{
case MOVEME_RESET:
/* Get window size */
GetWindowRect(hwndGlobalDlg, &amp;wndpl);
MyWidth = wndpl.right - wndpl.left;
MyHeight = wndpl.bottom - wndpl.top;
break;
case MOVEME_REFRESH:
StartX = x;
StartY = y;
break;
case MOVEME_MOVE:
x -= StartX;
y -= StartY;
GetWindowRect(hwndGlobalDlg, &amp;wndpl);
wndpl.left += x;
wndpl.right += x;
wndpl.top += y;
wndpl.bottom += y;
ClipCursor(&amp;wndpl);
SetWindowPos(hwndGlobalDlg, HWND_TOP, wndpl.left, wndpl.top, 0, 0, SWP_NOSIZE | SWP_NOZORDER );
break;
default:
break;
}
return;
}
void showstatus (int StatusType, char *Message, ...)
{
char MsgBuf[256];
static int ItemNumber = 0;
static LVITEM lvi;
va_list args;
if(StatusType == STATUS_RESET)
{
SendMessage(hwndList, LVM_DELETEALLITEMS, 0, 0);
ItemNumber = 0;
return;
}
//too many lines is very slow !
//Added by 微风工作室 (Gentle Breeze Studio) www.gentle-breeze.com
if(! ((ItemNumber+1)%1000) )
ListView_DeleteAllItems(hwndList);
va_start(args, Message);
vsprintf(MsgBuf, Message, args);
va_end(args);
lvi.mask = LVIF_TEXT | LVIF_STATE | LVIF_IMAGE;
lvi.iItem = ItemNumber;
lvi.iSubItem = 0;
lvi.pszText = MsgBuf;
//lvi.state = LVIS_SELECTED;
//lvi.stateMask = -1;
//lvi.iImage = 0;
switch(StatusType)
{
case STATUS_PROGRESS:
ListView_SetItem(hwndList, &amp;lvi);
ListView_EnsureVisible(hwndList, ItemNumber, FALSE);
break;
case STATUS_INFORMATION:
case STATUS_ERROR:
case STATUS_CRITICALERROR:
default:
ListView_InsertItem(hwndList, &amp;lvi);
ListView_EnsureVisible(hwndList, ItemNumber, FALSE);
ItemNumber++;
break;
}
}
void showprogress(int Progress)
{
SendMessage(hwndProgress, PBM_SETPOS, (WPARAM)Progress, 0);
}
void exitprocess(int ReturnValue)
{
_fcloseall();
//SetDlgItemText(hwndGlobalDlg, ID_CANCEL, "E&amp;xit");
//SetDlgItemText(hwndGlobalDlg, ID_START, "&amp;Start!");
SetDlgItemText(hwndGlobalDlg, ID_CANCEL, "退出");
SetDlgItemText(hwndGlobalDlg, ID_START, "开始处理");
EnableWindow(GetDlgItem(hwndGlobalDlg, ID_OPTION), TRUE);
//Because "start" button is disable when summary , so you must enable it after summary
if(summary_flag) EnableWindow(GetDlgItem(hwndGlobalDlg, ID_START), TRUE);
SendMessage(hwndProgress, PBM_SETPOS, 0, 0);
working = 0;
pause = 0;
ExitThread(ReturnValue);
}
/* ASFFix Core routine */
/* Some code are pasted from ASFRecorder's source */
#define MAX_CHUNK_SIZE 65535
unsigned char DAT_ID[16] = {0x36,0x26,0xb2,0x75,0x8e,0x66,0xcf,0x11,
0xa6,0xd9,0x00,0xaa,0x00,0x62,0xce,0x6c};
unsigned char HDR_ID[16] = {0xa1,0xdc,0xab,0x8c,0x47,0xa9,0xcf,0x11,
0x8e,0xe4,0x00,0xc0,0x0c,0x20,0x53,0x65};
/* Offsets of some critical information in ASF header */
#define ASF_HEADER_SIZE_8 0x10
#define HDR_ASF_FILESIZE_8 0x28
#define HDR_NUM_PACKETS_8 0x38
#define HDR_FINE_TOTALTIME_8 0x40
#define HDR_FINE_PLAYTIME_8 0x48
#define HDR_PLAYTIME_OFFSET_4 0x50
#define HDR_FLAGS_4 0x58
#define HDR_ASF_CHUNKLENGTH_4 0x5c
#define HDR_ASF_CHUNKLENGTH_CONFIRM_4 0x60
#define DATSEG_HDR_SIZE 0x32
#define DATSEG_NUMCHUNKS_4 0x28
/* get 32bit integer (Little endian) */
void get_long(unsigned char *pos, unsigned long *val)
{
unsigned char c1 = *pos++, c2 = *pos++, c3 = *pos++, c4 = *pos++;
(*val) = (c4<<24) + (c3<<16) + (c2<<8) + c1;
}
/* put 32 bit integer (Little endian) */
void put_long(unsigned char *pos, unsigned long val)
{
*pos++ = (val &amp;
0xff);
*pos++ = ((val>>8) &amp;
0xff), *pos++ = ((val>>16) &amp;
0xff), *pos++ = ((val>>24) &amp;
0xff);
}
/* get 64bit integer (Little endian) */
void get_quad(unsigned char *pos, unsigned long *hi, unsigned long *lo)
{
unsigned char c1 = *pos++, c2 = *pos++, c3 = *pos++, c4 = *pos++;
unsigned char c5 = *pos++, c6 = *pos++, c7 = *pos++, c8 = *pos++;
(*hi) = (c8<<24) + (c7<<16) + (c6<<8) + c5;
(*lo) = (c4<<24) + (c3<<16) + (c2<<8) + c1;
}
/* write 64bit integer to stream (Little endian) */
void put_quad(unsigned char *pos, unsigned long hi, unsigned long lo)
{
pos[0] = (lo &amp;
0xff);
pos[1] = ((lo>>8) &amp;
0xff);
pos[2] = ((lo>>16) &amp;
0xff);
pos[3] = ((lo>>24) &amp;
0xff);
pos[4] = (hi &amp;
0xff);
pos[5] = ((hi>>8) &amp;
0xff);
pos[6] = ((hi>>16) &amp;
0xff);
pos[7] = ((hi>>24) &amp;
0xff);

}
/* helper routine for ASF header parsing */
int find_id(unsigned char *id, unsigned char *Buffer, int bufsize)
{
int offs, i, found;
for (offs = 0;
offs < bufsize-16;
offs++) {
found=1;
for (i=0;
i<16;
i++) if (Buffer[offs+i] != id) {found = 0;
break;}
if (found) return(offs);
}
return -1;
}
unsigned long mymain(char *argv[])
{

unsigned char RBuffer[3][MAX_CHUNK_SIZE];
unsigned char *Buffer = RBuffer[0];
unsigned char BufferIndex, GoodBufferIndex;
unsigned int Buffersize = MAX_CHUNK_SIZE;
FILE *fpin = NULL;
FILE *fpout = NULL;
unsigned long chunksize,chunksize2,endofheader_offs;
int offs,asfheader_offs,asfheaderend_offs;
unsigned int bodylength;

unsigned char chunkflag;
unsigned int paddingsize;
unsigned char needwrite;
unsigned int unknownheader;
unsigned long filelength;
unsigned long read_pointer,write_pointer;
unsigned int Progress;
char s1[1024];
unsigned long bad_frames,current_frames,total_frames,start_frames,end_frames,good_frames;
unsigned long qh,ql,play_offs;
char old_argv1[256],indexcmd[256],indexarg[512];
working = 1;
showstatus(STATUS_INFORMATION, "----------------------------------------------------");
fpin = fopen(argv[0], "rb+");
if (fpin == NULL)
{
//showstatus(STATUS_CRITICALERROR, "Can not open file '%s'", argv[0]);
showstatus(STATUS_CRITICALERROR, "无法打开文件 '%s'", argv[0]);
exitprocess(1);
}
else
{
/* Parse the filename from full path */
char *filename = strrchr(argv[0], '//');
if(filename == NULL) filename = argv[0];
else
filename++;
showstatus(STATUS_INFORMATION, "File '%s' opened successfully!", filename);
fseek(fpin, 0, SEEK_END);
filelength = ftell(fpin);
}

if(callindex &amp;&amp;
!summary_flag){
strcpy(old_argv1,argv[1]);
sprintf(argv[1],"%s//zasffixx.wmv",c_dir);
}
if(strlen(argv[1]) &amp;&amp;
!summary_flag)
{
if(strcmp(argv[0], argv[1]) == 0)
{
showstatus(STATUS_CRITICALERROR, "Source and Destination file can not be same!");
exitprocess(1);
}
fpout = fopen(argv[1], "wb+");
if (fpout == NULL)
{
showstatus(STATUS_CRITICALERROR, "Can not open file '%s'", argv[1]);
exitprocess(1);
}
else
{
/* Parse the filename from full path */
char *filename = strrchr(argv[1], '//');
if(filename == NULL) filename = argv[1];
else
filename++;
showstatus(STATUS_INFORMATION, "File '%s' opened successfully!", filename);
}
}
/* Parse Header from file */
fseek(fpin, 0, SEEK_SET);
fread(Buffer, Buffersize, 1, fpin);
/* Get ASF header info */
asfheader_offs = offs = find_id(HDR_ID, Buffer, Buffersize);
if (offs == -1)
{
showstatus(STATUS_CRITICALERROR, "Unable to parse this ASF header!");
exitprocess(1);
}
/* extract required information */
get_long(&amp;Buffer[offs+HDR_ASF_CHUNKLENGTH_4], &amp;chunksize);
get_long(&amp;Buffer[offs+HDR_ASF_CHUNKLENGTH_CONFIRM_4], &amp;chunksize2);
/* check if the extracted chunk length looks good */
if ((chunksize > MAX_CHUNK_SIZE) || (chunksize != chunksize2))
{
showstatus(STATUS_CRITICALERROR, "Chunk size mismatch,unable to process this file!");
exitprocess(1);
}
showstatus(STATUS_INFORMATION, "Detected Chunk size: %X", chunksize);
/* ID 2 */
asfheaderend_offs = offs = find_id(DAT_ID, Buffer, Buffersize);

if (offs == -1)
{
showstatus(STATUS_CRITICALERROR, "Unable to parse this ASF header!");
exitprocess(1);
}
endofheader_offs = offs + DATSEG_HDR_SIZE;
showstatus(STATUS_INFORMATION, "Data Chunk starts from: %X", endofheader_offs);
if(summary_flag){
get_long(Buffer+asfheader_offs+HDR_ASF_FILESIZE_8,&amp;filelength);
get_long(Buffer+asfheader_offs+HDR_NUM_PACKETS_8,&amp;good_frames);
get_long(Buffer+asfheaderend_offs+DATSEG_NUMCHUNKS_4,&amp;ql);
showstatus(STATUS_INFORMATION, "GET: file size=%d , frames=%d , chunks=%d", filelength,good_frames,ql);
get_long(Buffer+asfheaderend_offs+DATSEG_NUMCHUNKS_4,&amp;ql);
get_quad(Buffer+asfheader_offs+HDR_FINE_TOTALTIME_8,&amp;qh,&amp;ql);
showstatus(STATUS_INFORMATION, "GET: total_time=%7.3f seconds", 429.497*qh+ql/10000000.0);
get_quad(Buffer+asfheader_offs+HDR_FINE_PLAYTIME_8,&amp;qh,&amp;ql);
showstatus(STATUS_INFORMATION, "GET: play_time =%7.3f seconds", 429.497*qh+ql/10000000.0);
get_long(Buffer+asfheader_offs+HDR_PLAYTIME_OFFSET_4,&amp;play_offs);
showstatus(STATUS_INFORMATION, "GET: play_offset=%d", play_offs);
}
total_frames=1.0*(_filelength(fileno(fpin))-endofheader_offs)/chunksize;
if(summary_flag){
sprintf(s1,"CACULATED: Total frames = %8.4f",1.0*(_filelength(fileno(fpin))-endofheader_offs)/chunksize);
showstatus(STATUS_INFORMATION, s1);
//sprintf(s1,"Total %ld", total_frames);
sprintf(s1,"总共 %ld", total_frames);
SetWindowText(GetDlgItem(hwndGlobalDlg,ID_TOTALFRAMES),s1);
SetDlgItemInt(hwndGlobalDlg,ID_STARTPOINT,0,FALSE);
SetDlgItemInt(hwndGlobalDlg,ID_ENDPOINT,total_frames-1,FALSE);
_fcloseall();
exitprocess(1);
}
else
{
start_frames=GetDlgItemInt(hwndGlobalDlg,ID_STARTPOINT,NULL,FALSE);
end_frames=GetDlgItemInt(hwndGlobalDlg,ID_ENDPOINT,NULL,FALSE);
if(start_frames>=end_frames || start_frames<0 || start_frames >total_frames-1){
//MessageBox(hwndGlobalDlg,"start frame is illegal!","ZAsfFix Error",MB_OK);
MessageBox(hwndGlobalDlg,"文件头错误!可能不是wmv,asf文件。","ZAsfFix Error",MB_OK);
exitprocess(1);
}
if(end_frames <= start_frames || end_frames<0 || end_frames >total_frames-1){
MessageBox(hwndGlobalDlg,"end frame is illegal!","ZAsfFix Error",MB_OK);
exitprocess(1);
}
}
/* This seems to be a valid file,so lets try fix it */
if(fpout != NULL) fwrite(Buffer, endofheader_offs, 1, fpout);
fseek(fpin, endofheader_offs, SEEK_SET);
unknownheader = 0;
BufferIndex = 0;
write_pointer = endofheader_offs;
bad_frames=good_frames=0;
for(current_frames=0;working;current_frames++)
{
/* Swap pointer between 3 buffers */
BufferIndex++;
BufferIndex%=3; /* 1,2,0,1,2,0......... */
GoodBufferIndex=(BufferIndex+1)%3;
/* 2,0,1,2,0,1..........*/
Buffer = RBuffer[BufferIndex];
needwrite = 0;
memset(Buffer, 0x00, Buffersize);
read_pointer = ftell(fpin);
Progress = (float)read_pointer / filelength * PROGRESS_SCALE;
showprogress(Progress);
if(read_pointer == filelength || feof(fpin) || good_frames>end_frames)
{
showstatus(STATUS_INFORMATION, "Process completed!");
break;
}
fread(Buffer, chunksize, 1, fpin);
if(Buffer[0]!=0x82 || Buffer[1]!=0x00 || Buffer[2]!=0x00)
{
bad_frames++;
unknownheader++;
if(brutemode)
showstatus(STATUS_ERROR, "Unknown header %02X:%02X:%02X at %lX,filled!", Buffer[0], Buffer[1], Buffer[2], read_pointer);
//if "save as",delete bad frames
else
if(fpout)
showstatus(STATUS_PROGRESS, "Error header %02X:%02X:%02X at %5.0f Frames,Deleted!", Buffer[0], Buffer[1], Buffer[2], 1.0*(read_pointer-endofheader_offs)/chunksize);
else

showstatus(STATUS_PROGRESS, "Unknown header %02X:%02X:%02X at %lX Frame%5.0f,Deleted!", Buffer[0], Buffer[1], Buffer[2], read_pointer,1.0*(read_pointer-endofheader_offs)/chunksize);
}
else
{
unknownheader = 0;
/* Get Data Chunk's Flags */
chunkflag=Buffer[3];
showstatus(STATUS_PROGRESS, "Header:%02X:%02X:%02X Flag:%02X at %lX", Buffer[0], Buffer[1], Buffer[2], chunkflag, read_pointer);
for (bodylength=chunksize-1;bodylength>0;bodylength--)
if (Buffer[bodylength]) break;
bodylength += 1;
//Update timecode according 100ms per frame
if(good_frames>=start_frames)
set_timecode(Buffer,good_frames-start_frames,play_offs);
good_frames++;
}
if(fpout != NULL)
{
if (brutemode &amp;&amp;
unknownheader &amp;&amp;
current_frames>=start_frames &amp;&amp;
current_frames<=end_frames )
{
/* Fill with next Buffer(which still store the good data 2 Chunks before) */
fwrite(RBuffer[GoodBufferIndex], chunksize, 1, fpout);
/* Whether this be the first one of (a series of) bad Chunk(s)? */
if(unknownheader == 1)
{
unsigned long tmppointer;
tmppointer = ftell(fpout);
/* Maybe the previous Chunk is also currupted, lets fix it */
if((tmppointer - 2*chunksize) > endofheader_offs) // Avoid overwrite header */
{
fseek(fpout, tmppointer - 2*chunksize, SEEK_SET);
fwrite(RBuffer[GoodBufferIndex], chunksize, 1, fpout);
fseek(fpout, tmppointer, SEEK_SET);
}
}
/* Point to previous Buffer,so that next loop still use the Buffer we currently use */
/* and the good data will never be destroyed(if we encounter more than two bad chunks) */
BufferIndex+=2; // Equals to Buffer--
BufferIndex %= 3;
// showstatus(STATUS_INFORMATION, "Fixed using data from %d, %d", GoodBufferIndex, BufferIndex);
}
else
{
if(!unknownheader &amp;&amp;
current_frames>=start_frames &amp;&amp;
current_frames<=end_frames)
fwrite(Buffer, chunksize, 1, fpout);
}
}
else
{
if (!unknownheader &amp;&amp;
current_frames>=start_frames &amp;&amp;
current_frames<=end_frames)
{
long old_pointer;
old_pointer=ftell(fpin);
fseek(fpin, write_pointer, SEEK_SET);
write_pointer += chunksize;
fwrite(Buffer, chunksize, 1, fpin);
fseek(fpin, old_pointer, SEEK_SET);
}
}
}
showstatus(STATUS_INFORMATION, "Total %ld frames , good_frames=%d , bad_frames=%ld", total_frames,good_frames,bad_frames);
//set end_frames=good_frames-1 if end_frames>good_frames-1
if(end_frames>good_frames-1) end_frames=good_frames-1;
//Refresh the "total frames","start point","end point"
//sprintf(s1,"Total %ld", end_frames-start_frames+1);
sprintf(s1,"总共 %ld", end_frames-start_frames+1);
SetWindowText(GetDlgItem(hwndGlobalDlg,ID_TOTALFRAMES),s1);
SetDlgItemInt(hwndGlobalDlg,ID_STARTPOINT,0,FALSE);
SetDlgItemInt(hwndGlobalDlg,ID_ENDPOINT,end_frames-start_frames,FALSE);

//Modify ASF header ,correct file_size,packets_num,total_time etc.
if(fpout==NULL){
fseek(fpin,0,SEEK_SET);
if(filelength>Buffersize)
fread(Buffer, Buffersize, 1, fpin);
else
fread(Buffer, filelength, 1, fpin);
}
else
{
fseek(fpout,0,SEEK_SET);
if(filelength>Buffersize)
fread(Buffer, Buffersize, 1, fpout);
else
fread(Buffer, filelength, 1, fpout);
}
// Set the file to seekable
put_long(Buffer+asfheader_offs + HDR_FLAGS_4,0x00000002);
// Set file size
filelength=asfheaderend_offs + DATSEG_HDR_SIZE + chunksize*(end_frames-start_frames+1);
put_quad(Buffer+asfheader_offs+HDR_ASF_FILESIZE_8,0,filelength);
// Set number of packets , in my view , total packets= data chunks+1 = frames+1
// But writer of 'asfr issue: packets=chunks=frames
put_quad(Buffer+asfheader_offs+HDR_NUM_PACKETS_8,0,(end_frames-start_frames+1));
// Set number of chunks
put_long(Buffer+asfheaderend_offs+DATSEG_NUMCHUNKS_4,(end_frames-start_frames+1));
/* write total size of data chunk segment */
ql = DATSEG_HDR_SIZE + chunksize*(end_frames-start_frames+1);
put_quad(Buffer+asfheaderend_offs + 0x10,0,ql);
showstatus(STATUS_INFORMATION, "PUT: file size=%d , frames=%d , data=%d", filelength,(end_frames-start_frames+1),ql);
// Set play_time offset
get_long(Buffer+asfheader_offs+HDR_PLAYTIME_OFFSET_4,&amp;play_offs);
showstatus(STATUS_INFORMATION, "play_offset=%d", play_offs);
//Notice: qh=1 , about 429.497 seconds ;
ql=1 , 100ns
qh=0.1*(end_frames-start_frames+1)/429.497;
ql=(0.1*(end_frames-start_frames+1)-429.497*qh)*10000000;
put_quad(Buffer+asfheader_offs+HDR_FINE_PLAYTIME_8,qh,ql);
showstatus(STATUS_INFORMATION, "PUT: play_time= %7.3f seconds", 429.497*qh+ql/10000000.0);
//total_time=play_time+play_offset
qh=0.1*(end_frames-start_frames+1+play_offs/100.0)/429.497;
ql=(0.1*(end_frames-start_frames+1+play_offs/100.0)-429.497*qh)*10000000;
put_quad(Buffer+asfheader_offs+HDR_FINE_TOTALTIME_8,qh,ql);
showstatus(STATUS_INFORMATION, "PUT: total_time=%7.3f seconds", 429.497*qh+ql/10000000.0);
/* qh=0.1*start_frames/429.497;
ql=(0.1*start_frames-429.497*qh)*10000000;
put_quad(Buffer+asfheader_offs+HDR_FINE_PLAYTIME_8,qh,ql);
showstatus(STATUS_INFORMATION, "PUT: play_time= %7.3f seconds", 429.497*qh+ql/10000000.0);
//total_time=play_time+play_offset
qh=0.1*(start_frames+play_offs/100.0)/429.497;
ql=(0.1*(start_frames+play_offs/100.0)-429.497*qh)*10000000;
put_quad(Buffer+asfheader_offs+HDR_FINE_TOTALTIME_8,qh,ql);
showstatus(STATUS_INFORMATION, "PUT: total_time=%7.3f seconds", 429.497*qh+ql/10000000.0);
*/
if(fpout==NULL){
fseek(fpin,0,SEEK_SET);
if(filelength>Buffersize)
fwrite(Buffer, Buffersize, 1, fpin);
else

fwrite(Buffer, filelength, 1, fpin);
_chsize(_fileno(fpin),filelength);
}
else
{
fseek(fpout,0,SEEK_SET);
if(filelength>Buffersize)
fwrite(Buffer, Buffersize, 1, fpout);
else

fwrite(Buffer, filelength, 1, fpout);
_chsize(_fileno(fpout),filelength);
}
//Call gbsindex.exe to index file
if(callindex){
_fcloseall();
sprintf(indexcmd,"%s//gbsindex.exe",c_dir);
sprintf(indexarg," -i %s -o %s",argv[1],old_argv1);
_spawnlp(_P_WAIT,indexcmd,indexcmd,indexarg,NULL);
_unlink(argv[1]);
}
//clear save as , because it's effect only once
//SendMessage(GetDlgItem(hwndGlobalDlg,ID_SAVEAS),BM_SETCHECK,BST_UNCHECKED,0);
//SetWindowText(GetDlgItem(hwndGlobalDlg,ID_SAVEASINPUT),"");
if(saveas) SendMessage(hwndGlobalDlg,WM_COMMAND,ID_SAVEAS,0);
exitprocess(0);
return 0;
}
//-----------------------------------------------------------------------------------------
//set timecode in millisecond in chunk buffer
BOOL set_timecode(unsigned char *Buffer,int frames,int offset)
{
int tc_start = -1;
unsigned long old_time;
/* timecode position varies with type of chunk */
switch(Buffer[3])
{
case 0x00: tc_start = 5; break;
case 0x01: tc_start = 5; break;
case 0x08: tc_start = 6; break;
case 0x09: tc_start = 6; break;
case 0x10: tc_start = 7; break;
case 0x11: tc_start = 7; break;
case 0x48: tc_start = 8; break;
case 0x49: tc_start = 8; break;
case 0x50: tc_start = 9; break;
case 0x51: tc_start = 9; break;
default: tc_start = -1; showstatus(STATUS_ERROR, "unknown format type: $%02x/n", (int)Buffer[3]);
}
if(tc_start==-1) return FALSE;
else
{
get_long(Buffer+tc_start,&amp;old_time);
showstatus(STATUS_PROGRESS ,"GET: Frames %05d , old_time=%07d",
frames,old_time);
// put_long(Buffer+tc_start,100*frames+offset);
return TRUE;
}
}
 
翻譯倒是容易﹐但是才那么點分~~哎~~~~
 
大哥 行行好 告诉我思路和大致的翻译就好了 我真的没有分了[:(]
 
大虾们 救救老弟吧
 
做成DLL在DELPHI中调用?
 
没有空啊,不然一定给你翻译,这个翻译不难
 
后退
顶部