Video file h264 encoding and decoding base class 3 using DirectShow

Source: Internet
Author: User

Bool cencode: _ hasdecodefilter (cstring strfilename)
{
Igraphbuilder * pgraph = NULL;
Ibasefilter * psrc = NULL;
Hresult hR = e_fail;
Bool bhas = false;
HR = cocreateinstance (clsid_filtergraph, null, clsctx_inproc_server, iid_igraphbuilder, (void **) & pgraph );
If (succeeded (HR ))
{
Int nlen = multibytetowidechar (cp_acp, 0, strfilename,-1, null, null );
Lpwstr pstrfile = new wchar [nlen];
Multibytetowidechar (cp_acp, 0, strfilename,-1, pstrfile, nlen );
HR = pgraph-> renderfile (pstrfile, null );
Delete pstrfile;
If (succeeded (HR ))
{
Bhas = true;
HR = pgraph-> findfilterbyname (pstrfile, & psrc );
If (psrc)
{
_ Nukedownstream (pgraph, psrc );
Pgraph-> removefilter (psrc );
Psrc-> release ();
}
}
Else
Bhas = false;
Pgraph-> release ();
}
Return bhas;
}

Int cencode: _ hash1_filter (bool bflag)
{
Hresult hR = e_fail;
Int iresult =-1;

/************* Create a system enumerator and obtain the icreatedevenum interface *****************/
Icreatedevenum * penumhardware = NULL; // defines a pointer to the icreatedevenum Interface
HR = cocreateinstance (clsid_systemdeviceenum, null, clsctx_all, iid_icreatedevenum, (void **) & penumhardware );
If (failed (HR) // return if creation fails
Return iresult;

/************* Create a type directory enumerator and obtain the ienummoniker interface **************** */
Cstring strtempname = bflag? "Dt264 compress filter": "dt264 decompress filter ";
Refclsid clsid_tempcategory = bflag? Clsid_videocompressorcategory: clsid_legacyamfiltercategory;
Ibasefilter * ptempfilter = NULL;
Ienummoniker * penummoniker = NULL; // defines a pointer to the ienummoniker Interface
HR = penumhardware-> createclassenumerator (clsid_tempcategory, & penummoniker, 0 );
If (penummoniker) // The type directory enumerator is successfully created and the ienummoniker interface is not empty
{
Penummoniker-> Reset (); // reset the enumerated sequence to the initial state.
Ulong fetched = 0;
Imoniker * pmoniker = NULL;
// Perform a for loop when the imoniker interface is queried successfully and the device filter is not created successfully.
While (succeeded (penummoniker-> next (1, & pmoniker, & fetched)
{
If (pmoniker) // The imoniker interface is queried successfully.
{
Ipropertybag * ppropertybag = NULL;
HR = pmoniker-> bindtostorage (0, 0, iid_ipropertybag, (void **) & ppropertybag );
If (failed (HR ))
{
Iresult =-1;
Continue;
}
Variant name;
Name. Vt = vt_bstr;
HR = ppropertybag-> Read (L "friendlyname", & name, null); // obtain the friendly name of the device
If (succeeded (HR) // The friendly name of the device is obtained successfully.
{
Char friendlyname [256];
Friendlyname [0] = 0;
Widechartomultibyte (cp_acp, 0, name. bstrval,-1, friendlyname, 256, null, null );
If (strcmp (friendlyname, (lpstr) (lpctstr) strtempname) = 0)
{
Iresult = 1;
HR = pmoniker-> bindtoobject (0, 0, iid_ibasefilter, (void **) & ptempfilter); // create a device Filter
If (failed (HR) // The device filter is created successfully.
Afxmessagebox ("/R codec for this file. h264 codec is required. download and install the corresponding plug-in. /Rcreate h264 filter failed! ");
Else
Bflag? (M_nencodefilter.hsf-encode = ptempfilter) :( m_ndecodefilter.hsf-decode = ptempfilter );
Break;

}
Else
Iresult = 0;
}
Ppropertybag-> release (); // when propertybag is not empty, its memory space is released.
Pmoniker-> release ();
}
}
Penummoniker-> release ();
}
Penumhardware-> release ();
Return iresult;
}

Bool cencode: _ findpins (ibasefilter * pinfilter, bool bflag, ipin ** poutpin)
{
Assert (pinfilter );
Hresult hR = e_fail;
Bool breturned = false;
Ienumpins * penumpins;
HR = pinfilter-> enumpins (& penumpins );
If (failed (HR ))
Return breturned;
Ipin * ppin = NULL;
Ulong nfetched = 0;
Penumpins-> Reset ();
While (succeeded (penumpins-> next (1, & ppin, & nfetched)
{
If (ppin) // The Pin is queried successfully.
{
Pin_direction pindir;
Pin_direction pindirection = bflag? Pindir_input: pindir_output;
HR = ppin-> querydirection (& pindir );
If (failed (HR) // query the direction of the pin fails.
{
Ppin-> release ();
Continue;
}
Else // The Pin direction is queried successfully.
{
If (pindir = pindirection)
{
Ipin * pconnectedpin = NULL;
HR = ppin-> connectedto (& pconnectedpin );
If (failed (HR) // The Pin has been connected
{
* Poutpin = ppin;
Breturned = true;
}
Else // The Pin is not connected
{
Ppin-> release ();
Pconnectedpin-> release ();
Continue;
}
}
}
}
}
Penumpins-> release ();
Return breturned;
}

Void cencode: _ nukedownstream (igraphbuilder * ingraph, ibasefilter * infilter)
{
If (! Ingraph |! Infilter)
Return;
Ienumpins * pinenum = NULL;
If (succeeded (infilter-> enumpins (& pinenum) // All pins are enumerated successfully.
{
Pinenum-> Reset ();
Ipin * pin = NULL;
Ulong fetched = 0;
While (succeeded (pinenum-> next (1, & pin, & fetched) // The Pin is queried successfully.
{
If (PIN)
{
Ipin * connectedpin = NULL;
Pin-> connectedto (& connectedpin); // obtain the Pin connected to the pin
If (connectedpin) // The Pin connected to the query pin is not empty.
{
Pin_info pininfo;
If (succeeded (connectedpin-> querypininfo (& pininfo) // the direction of the pin is queried successfully.
{
If (pininfo. dir = pindir_input) // The connected pin is the input pin.
{
_ Nukedownstream (ingraph, pininfo. pfilter); // recursive call
Ingraph-> disconnect (connectedpin );
Ingraph-> disconnect (PIN );
Ingraph-> removefilter (pininfo. pfilter); // remove from filter graph
}
Pininfo. pfilter-> release ();
}
Connectedpin-> release ();
}
Pin-> release ();
Pin = NULL;
}
}
Pinenum-> release ();
}
}

Bool cencode: _ createcompressdevice (cstring strdevice, ibasefilter ** pcompressorfilter)
{
Hresult hR = s_ OK;
Bool bcreated = false;
Ibasefilter * ptempfilter = NULL;
Icreatedevenum * enumhardware = NULL; // defines a pointer to the icreatedevenum Interface
HR = cocreateinstance (clsid_systemdeviceenum, null, clsctx_all, iid_icreatedevenum, (void **) & enumhardware); // create a system device enumerator and obtain the icreatedevenum Interface
If (failed (HR) // return if creation fails
Return false;
Ienummoniker * enummoniker = NULL; // defines a pointer to the ienummoniker Interface
HR = enumhardware-> createclassenumerator (clsid_videocompressorcategory, & enummoniker, 0); // create a type directory enumerator and obtain the ienummoniker Interface

If (enummoniker) // The type directory enumerator is successfully created and the ienummoniker interface is not empty
{
Enummoniker-> Reset (); // reset the enumerated sequence to the initial state.
Ulong fetched = 0;
Imoniker * moniker = NULL;
Char friendlyname [256];
// Perform a for loop when the imoniker interface is queried successfully and the device filter is not created successfully.
While (succeeded (enummoniker-> next (1, & moniker, & fetched)
{
If (moniker) // The imoniker interface is queried successfully.
{
Ipropertybag * propertybag = NULL;
Variant name;
Friendlyname [0] = 0;
HR = moniker-> bindtostorage (0, 0, iid_ipropertybag, (void **) & propertybag );
If (succeeded (HR ))
{
Name. Vt = vt_bstr;
HR = propertybag-> Read (L "friendlyname", & name, null); // get the friendly name of the device
}
If (succeeded (HR) // The friendly name of the device is obtained successfully.
{
Widechartomultibyte (cp_acp, 0, name. bstrval,-1, friendlyname, 256, null, null );
If (strcmp (friendlyname, (lpstr) (lpctstr) strdevice) = 0)
{
HR = moniker-> bindtoobject (0, 0, iid_ibasefilter, (void **) & ptempfilter); // create a device Filter
If (succeeded (HR) // The device filter is created successfully.
{
* Pcompressorfilter = ptempfilter;
Bcreated = true;
}
}
}
If (propertybag) // when propertybag is not empty, release its memory space
{
Propertybag-> release ();
Propertybag = NULL;
}
Moniker-> release ();
}
}
Enummoniker-> release ();
}
Enumhardware-> release ();
Return bcreated;
}

Hresult cencode: _ createfiltergraph (igraphbuilder ** ingraph, icapturegraphbuilder2 ** inbuilder)
{
Hresult hR = e_fail;
Igraphbuilder * pgraph = NULL;
Icapturegraphbuilder2 * pbuilder = NULL;
HR = cocreateinstance (clsid_filtergraph, null, clsctx_inproc_server, iid_igraphbuilder, (void **) & pgraph );
If (succeeded (HR) // filter graph manager is created successfully.
{
* Ingraph = pgraph;
HR = cocreateinstance (clsid_capturegraphbuilder2, null, clsctx_inproc_server, iid_icapturegraphbuilder2, (void **) & pbuilder );
If (succeeded (HR) // The secondary component is created successfully.
{
HR = pbuilder-> setfiltergraph (pgraph );
* Inbuilder = pbuilder;
}
Else // failed to create the secondary component
Afxmessagebox ("create capture graph builder object failed! ");
}
Else // An error occurred while creating the filter graph manager.
Afxmessagebox ("create filter graph Manager failed! ");
Return hr;
}

Void cencode: _ destroyencodegraph ()
{
***************/
Saferelease (m_nencode.w.ek );
Saferelease (m_nencode.pcontrol );
Saferelease (m_nencode.pevent );
Saferelease (m_nencodefilter.psink );
If (m_nencode.pgraph & m_nencodefilter.psrc)
{
_ Nukedownstream (m_nencode.pgraph, m_nencodefilter.psrc); // destroy downstream links
M_nencode.pgraph-> removefilter (m_nencodefilter.psrc );
Saferelease (m_nencodefilter.psrc );
}
_ Destroyfiltergraph (m_nencode.pgraph, m_nencode.pbuilder );
}

Void cencode: _ destroydecodegraph ()
{
***************/
Saferelease (m_ndecode.w.ek );
Saferelease (m_ndecode.pcontrol );
Saferelease (m_ndecode.pevent );
Saferelease (m_ndecodefilter.psink );
If (m_ndecode.pgraph & m_ndecodefilter.psrc)
{
_ Nukedownstream (m_ndecode.pgraph, m_ndecodefilter.psrc); // destroy downstream links
M_ndecode.pgraph-> removefilter (m_ndecodefilter.psrc );
Saferelease (m_ndecodefilter.psrc );
}
_ Destroyfiltergraph (m_ndecode.pgraph, m_ndecode.pbuilder );
}

Void cencode: _ destroyfiltergraph (igraphbuilder * ingraph, icapturegraphbuilder2 * inbuilder)
{
Saferelease (inbuilder );
Saferelease (ingraph );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.