/************* Create a graph code *************/
Hresult hR = e_fail;
HR = _ createfiltergraph (& m_ndecode.pgraph, & m_ndecode.pbuilder );
If (failed (HR ))
{
Afxmessagebox ("uninitialize com library! ");
Return-7;
}
Int iresult =-7;
Do
{
********** *****************/
HR = m_ndecode.pgraph-> QueryInterface (iid_imediacontrol, (void **) & m_ndecode.pcontrol );
If (failed (HR) // query failed
Break;
HR = m_ndecode.pgraph-> QueryInterface (iid_imediaeventex, (void **) & m_ndecode.pevent );
If (failed (HR) // query failed
Break;
HR = m_ndecode.pgraph-> QueryInterface (iid_imediaseeking, (void **) & m_ndecode.pseek );
If (failed (HR) // query failed
Break;
HR = m_ndecode.pgraph-> QueryInterface (iid_imediafilter, (void **) & m_ndecode.pmedia );
If (failed (HR) // query failed
Break;
/************************* Assists in filter creation ********** ***********/
HR = m_ndecode.pgraph-> addsourcefilter (strsourcepathname. allocsysstring (), L "Source Filter", & m_ndecodefilter.psrc );
If (failed (HR) // Add failed
Break;
HR = cocreateinstance (clsid_avidest, null, clsctx_inproc, iid_ibasefilter, (void **) & m_ndecodefilter.pavimux );
If (failed (HR) // creation failed
Break;
HR = m_ndecodefilter.pavimux-> QueryInterface (iid_imediaseeking, (void **) & m_ndecode.pseek );
If (failed (HR) // query failed
Break;
HR = cocreateinstance (clsid_filewriter, null, clsctx_inproc, iid_ifilesinkfilter2, (void **) & m_ndecodefilter.psink );
If (failed (HR) // creation failed
Break;
HR = m_ndecodefilter.psink-> QueryInterface (iid_ibasefilter, (void **) & m_ndecodefilter.pfilewrite );
If (failed (HR) // creation failed
Break;
If (! M_ndecodefilter.hsf-decode) // hsf-encode filter is not created
Break;
HR = m_ndecode.pgraph-> addfilter (m_ndecodefilter.hsf-decode, l "h264 uncompress filter ");
/******************** Create an upstream Link (hsf-decode) in the graph) *****************/
HR = m_ndecode.pbuilder-> renderstream (null, null, m_ndecodefilter.psrc, null, m_ndecodefilter.hsf-decode );
If (failed (HR) // link creation failed
{
Afxmessagebox ("/r this decoder does not support the selected media type. Please download the corresponding plug-in. /Rcreate psrc-> pavisplitter-> hsf-uncompressfilter failed! ");
Break;
}
/********************* Third-party encoder detection ************* *******/
If (strcompressorname. isempty () // The name of the compressed device is null.
M_ndecodefilter.pencode = NULL;
Else
If (! _ Createcompressdevice (strcompressorname, & m_ndecodefilter.pencode) // An error occurred while creating the compression device.
{
Iresult =-3;
Break;
}
/************************ Add filter to graph ********** **********/
If (m_ndecodefilter.pencode)
{
HR = m_ndecode.pgraph-> addfilter (m_ndecodefilter.pencode, l "compress filter ");
If (failed (HR ))
Break;
}
HR = m_ndecode.pgraph-> addfilter (m_ndecodefilter.pavimux, l "Avi DEST filter ");
If (failed (HR ))
Break;
HR = m_ndecode.pgraph-> addfilter (ibasefilter *) m_ndecodefilter.pfilewrite, l "file write filter ");
If (failed (HR ))
Break;
HR = m_ndecodefilter.psink-> setmode (am_file_overwrite );
If (failed (HR ))
Break;
HR = m_ndecodefilter.psink-> setfilename (strdestpathname. allocsysstring (), null );
If (failed (HR ))
{
Iresult =-5;
Break;
}
/********************* Create a downstream Link (hsf-decode) in the graph) *****************/
HR = m_ndecode.pbuilder-> renderstream (null, null, m_ndecodefilter.hsf-decode, m_ndecodefilter.pencode, m_ndecodefilter.pfilewrite );
If (failed (HR ))
{
Afxmessagebox ("/r this decoder does not support the selected media type. Please download the corresponding plug-in. /Rcreate h264 filter-> ----> file write filter failed! ");
Break;
}
HR = m_ndecode.pmedia-> setsyncsource (null );
HR = m_ndecode.pcontrol-> Run (); // run the filter graph
If (succeeded (HR ))
{
Iresult = 1;
M_ndecodectl.bstopflag = true;
}
} While (false );
Return iresult;
}
Int cencode: getprogress (bool bflag)
{
Hresult hR = e_fail;
If (bflag) // compression status
{
If (m_nencodectl.bcompleted) // data transmission is completed
{
M_nencodectl.bcompleted = false;
Return 100;
}
}
Else // unzipping status
{
If (m_ndecodectl.bcompleted) // data transmission is completed
{
M_ndecodectl.bcompleted = false;
Return 100;
}
}
/******************* Get the current status of data transmission ************* ********/
Imediaseeking * effecek = bflag? M_nencode.w.ek: m_ndecode.w.ek;
Longlong lduration = 1;
If (then EK = NULL)
Return-1;
HR = effecek-> getduration (& lduration );
If (succeeded (HR) // get the total duration successful
{
Longlong start = 0;
HR = effecek-> getcurrentposition (& START); // obtain the current position
If (succeeded (HR) // The current location is obtained successfully.
{
Int iprogress = (INT) (100 * (start/(double) lduration )));
If (bflag) // compression status
{
M_nencodectl.iprogress = iprogress;
Return m_nencodectl.iprogress;
}
Else // decommission status
{
M_ndecodectl.iprogress = iprogress;
Return m_ndecodectl.iprogress;
}
}
Return-1;
}
Else
Return-1;
}
Bool cencode: terminiateprogress (bool bflag)
{
Hresult hR = e_fail;
If (bflag)
{
**************** ***/
If (m_nencode.pcontrol! = NULL & m_nencodectl.bstopflag)
{
Oafilterstate state = state_stopped;
If (succeeded (m_nencode.pcontrol-> getstate (15, & State)/* Get the current state */))
{
If (State! = State_stopped)
HR = m_nencode.pcontrol-> stop (); // stop filter graph
}
_ Destroyencodegraph ();
}
Return succeeded (HR );
}
Else
{
/********************* Terminate decoding **************** ***/
If (m_ndecode.pcontrol! = NULL & m_ndecodectl.bstopflag)
{
Oafilterstate state = state_stopped;
If (succeeded (m_ndecode.pcontrol-> getstate (15, & State)/* Get the current state */))
{
If (State! = State_stopped)
HR = m_ndecode.pcontrol-> stop (); // stop filter graph
}
_ Destroydecodegraph ();
}
Return succeeded (HR );
}
}
Bool cencode: setpolicywindow (hwnd inwindow, bool bflag)
{
Hresult hR = e_fail;
If (bflag) // compression process
{
If (m_nencode.pevent)
{
HR = m_nencode.pevent-> setpolicywindow (oahwnd) inwindow, wm_encode_notify, 0 );
Return succeeded (HR );
}
}
Else // decompression process
If (m_ndecode.pevent)
{
HR = m_ndecode.pevent-> setpolicywindow (oahwnd) inwindow, wm_decode_notify, 0 );
Return succeeded (HR );
}
Return false;
}
Void cencode: oncodecnotify (wparam inwparam, lparam inlparam, bool bflags)
{
Imediaeventex * pevent = bflags? M_nencode.pevent: m_ndecode.pevent;
If (pevent)
{
Long eventcode = 0, eventparam1 = 0, eventparam2 = 0;
While (succeeded (pevent-> getevent (& eventcode, & eventparam1, & eventparam2, 0 )))
{
Switch (eventcode)
{
Case ec_complete: // Data Transmission completed
Terminiateprogress (bflags );
Bflags? (M_nencodectl.bcompleted = true): (m_ndecodectl.bcompleted = true );
Break;
Case ec_userabort: // An error occurred while running
Case ec_errorabort:
Terminiateprogress (bflags );
Bflags? (M_nencodectl.bcompleted = true): (m_ndecodectl.bcompleted = true );
Break;
Default:
Break;
}
Pevent-> freeeventparams (eventcode, eventparam1, eventparam2 );
}
}
}
Int cencode: _ ishpolicencoded (cstring strfilename)
{
Igraphbuilder * pgraph = NULL;
Ibasefilter * psrc = NULL;
Ibasefilter * pavisplitter = NULL;
Hresult hR = e_fail;
Int nresult =-1;
Do
{
/***************** Create filter graph manager ***************** */
HR = cocreateinstance (clsid_filtergraph, null, clsctx_inproc_server, iid_igraphbuilder, (void **) & pgraph );
If (failed (HR) // An error occurred while creating the filter graph manager.
{
Afxmessagebox ("create filter graph Manager failed! ");
Return nresult;
}
Int nlen = multibytetowidechar (cp_acp, 0, strfilename,-1, null, null );
Lpwstr pstrfile = new wchar [nlen];
Multibytetowidechar (cp_acp, 0, strfilename,-1, pstrfile, nlen );
/************** Create the source filter and AVI splitter filter ***************/
HR = pgraph-> addsourcefilter (pstrfile, l "Source Filter", & psrc );
If (pstrfile)
Delete pstrfile; // release resources
If (failed (HR) // failed to add Source Filter
{
Afxmessagebox ("create Source Filter failed! ");
Return nresult;
}
HR = cocreateinstance (clsid_avisplitter, null, clsctx_inproc_server, iid_ibasefilter, (void **) & pavisplitter );
If (failed (HR) // An error occurred while creating the AVI splitter filter.
{
Afxmessagebox ("add Avi splitter filer failed! ");
Return nresult;
}
Pgraph-> addfilter (pavisplitter, l "Avi splitter ");
/*************** Link Source Filter and AVI splitter filter **************/
Ipin * psrcoutpin = NULL;
Ipin * paviinpin = NULL;
Ipin * pavioutpin = NULL;
If (_ findpins (psrc, false, & psrcoutpin) & _ findpins (pavisplitter, true, & paviinpin ))
{
HR = pgraph-> connect (psrcoutpin, paviinpin );
If (failed (HR) // connection failed
{
Psrcoutpin-> release ();
Paviinpin-> release ();
Afxmessagebox ("Connect Source Filter to Avi splitter failed! ");
Break;
}
Else // connection successful
{
If (_ findpins (pavisplitter, false, & pavioutpin ))
{
Ienummediatypes * penumtype = NULL;
HR = pavioutpin-> enummediatypes (& penumtype );
If (failed (HR) // enumeration pin media type failed
{
Pavioutpin-> release ();
Afxmessagebox ("Enum mediatypes failed! ");
Break;
}
Else // enumeration pin media type successful
{
Am_media_type * mediatype;
Ulong nfeched = 0;
Penumtype-> Reset ();
While (succeeded (penumtype-> next (1, & mediatype, & nfeched) // enumerate media types
{
Static const guid mediasubtype_dt264 =
{0x52445444, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }};
If (mediatype-> subtype = mediasubtype_dt264) // encode the h264 file.
{
Nresult = 1;
Deletemediatype (mediatype );
Break;
}
Else // not an h264 encoded file
Nresult = 0;
Deletemediatype (mediatype );
}
Penumtype-> release ();
}
Pavioutpin-> release ();
}
Pgraph-> disconnect (psrcoutpin );
Pgraph-> disconnect (paviinpin );
Psrcoutpin-> release ();
Paviinpin-> release ();
}
}
Else // query the output pin of the source filter or the input pin of the AVI splitter filter.
Return nresult;
} While (false );
/********************* Release resources ***************** ****/
If (pavisplitter)
{
Pgraph-> removefilter (pavisplitter );
Saferelitter (pavisplitter );
}
If (psrc)
{
Pgraph-> removefilter (psrc );
Saferelease (psrc );
}
Saferelgraph (pgraph );
Return nresult;
}
Bool cencode: _ isfileopenedcorrectly (cstring strfilename)
{
Bool bopened = false;
Char * pfilename = (lpstr) (lpctstr) strfilename;
Try
{
Cfile F (pfilename, cfile: moderead | cfile: sharedenynone );
Bopened = true;
F. Close ();
}
Catch (cfileexception E)
{
Bopened = false;
Throw;
}
Return bopened;
}
Bool cencode: _ issuitedfiletype (cstring strfilename)
{
If (strfilename. isempty ())
Return false;
If (strfilename. getlength () <5) // the file name length is less than 5
Return false;
Else // file name length greater than 5
{
If (strfilename. Right (4). Compare (". Avi ")! = 0) // the last four digits of the file name are not. Avi
Return false;
}
Return true;
}