DirectShow experience


[Posted 18/7/2007 in http://allencch.multiply.com%5D

After a long time doing about DirectShow, finally I can grasp what is going on in DirectShow.

For me, the purpose of DirectShow is to get the multimedia input then display the output, eg: *.avi file and video capture device such as webcam. And everything is dealt with “filter” (Please read the documentation for detailed information). And the filters are written in COM (Component Object Module).

To deal with DirectShow, you must download DirectX 9 SDK from MSDN, or download Windows Platform SDK from MSDN, because the DirectShow SDK has removed from DirectX newer SDK. I suggest DirectX 9 SDK, but I don’t know whether one can download it or not.

Because I started the DirectShow with zero knowledge about the COM, it is very very hard to understand the example of the coding. Therefore, one should read the documentation of DirectShow and try some simple examples in the SDK. For learning, trial and error is a very effective method.

GraphEdit.exe is the program you must try first, because this will determine how your algorithm will go.

First of all, remember to use CoInitialize() or CoInitializeEx() (for _WINNT=0400) for COM, and remember to include “atlbase.h” header file.

There are 2 ways for writing the coding about COM. One is using “template”, however I am not familiar with it. But actually they are same, only in different form of expression.

The basic thing we need is IGraphBuilder. All the filters will be added in this IGraphBuilder. Remember to use CoCreateInstance() to create IGraphBuilder. Mostly every filter will be created using CoCreateInstance().

After the IGraphBuilder is created, the filters need to be added in. Which filters you want to add depend on the purpose of your project. Please use GraphEdit.exe to simulate your algorithm first. Then declare your filter in IBaseFilter. This is important, because IGraphBuilder will only deal with IBaseFilter. Besides that, IBaseFilter allows you to enumerate the pin. Please declare/add a filter as a “source filter“, because without “source”, I think nothing can work.

The function of pins is to connect the filters. One filter can have several pins. There are two types of pins, input pin and output pin. Therefore, when two filters are connected, that means the output pin of A filter is connected with the input pin of the B filter. So that, the data will flow from A filter to B filter.

Because all filter are declared using IBaseFilter, the difference is the CLSID (I also don¡¯t know what is this, it is something about the COM). Please refer the documentation about CLSID for each filter. For example, the File Source (Async) Filter uses CLSID_AsyncReader.

After the declaration of IBaseFilter, then add it to the IGraphBuilder using AddFilter() function. However, if it deals with a specific file, then need to use AddSourceFilter(). Like what GraphEdit.exe does, the source file is important. Therefore, using AddSourceFilter() with the specific filename, then it is enough. Why is the file important? I think because DirectShow need to determine what type of file beforehand, then the connection of the filter can be determined.

After the source, you need to add in the other filters. For me, I will find the output pin of the filter first. Pin is very important thing. This is because just using the pin you can render the file already. To deal with the pin, one needs to use IPin interface. Then, use the functions from the documentation and DirectShow example from SDK, “GetUnconnectedPin()“, “GetPin()“, “GetInPin()“, “GetOutPin()” and else, modify it if you like. By using these functions (which I like them very much), you can find the pin of the filter.

After finding the output pin of the source filter, you can render it using render() of IGraphBuilder. What happened if render()? You can try to see it using GraphEdit.exe. But if you want to specify what you want to get from the source, then you will need to add in the filter you like. Therefore, add the filter that you want. Then find the input pin. Then, connect the output pin from the source filter and the input pin from your second filter. Remember to make sure both filters can be connected using GraphEdit.exe. Otherwise, there will be no output.

For me, I would always like to find the output pin only after the filter is connected. This is because, when using the filter like AVI Splitter Filter, if you use GraphEdit.exe, you will find that when you add in the filter, you cannot see any output pin. You can see the output pin only if when you connect the AVI Splitter Filter with the source filter. That is why I will find the output pin only after the filter is connected, or after the source file is determined.

After the second filter is added and connected, find the output pin. Then render() it. Because of DirectShow has “Intelligent Connect“, when you render() the pin, the filters will automatically added without your acknowledgement. Besides that, you can also just add in the other filters without any connection, then you render the output pin, the Intelligent Connection will help you to connect them. (Please read the documentation for detailed information). You can try Intelligent Connection using GraphEdit.exe.

Using GraphEdit.exe, you have filters, source, and connected. Then you would like to run the “run”. Then you click the “play” button at toolbar, then the video works. However, IGraphBuilder does not have any “run: function. Therefore, you need to query interface from other interface (this is a COM method). First, you need to declare IMediaControl. This is to control the media, provides run() function. Then, using QueryInterface() from IGraphBuilder, to query IMediaControl’s interface. QueryInterface() involves IID_IInterfaceName.

For me, query interface is to get the interface of another COM object. So that, IGraphBuilder can run() through IMediaControl. This is similar to the Sample Grabber Filter which query ISampleGrabber interface.

Finally, you can run() the stream using IMediaControl. But, please release() the filter before that. And remember to CoUninitialize() for the COM.

You might debug your program during the development. However, I found an “Error Protection” message that prevents me to debug especially after “render()”. This is because there are 3rd party DirectShow filter. For me, I got installed Nero 6. So, removing the NeVideo.ax filter by using DirectShow Filter Manager from http://www.softella.com, the debugging problem was fixed.

/* If you don’t like what I write, you can bypass the following part */

What you would like to do after the file is split? Why you want to split the file? Because I need the audio stream while manipulating the video stream. To manipulate the video, that is to get the video buffer (data) and access the buffer, either modify it or copy it. For me, I use the buffer for OpenGL.

Because the stream (I mean the file data) is split into video and audio using AVI Splitter Filter, there are 2 output pins. You should use GraphEdit.exe to check which pin is video and which pin is audio. Using GetPin() function from the GrabBitmaps example of DirectShow SDK, you can get both pins. Both pins are needed because you would like the sound.

Next, you need to add in Sample Grabber Filter to grab the buffer from the output video pin. Thus, you add in the Sample Grabber Filter and then find the input pin as usual. However, you cannot connect the filter first. This is because you need to determine the Media Type of the input stream beforehand. To determine the media type, IBaseFilter cannot do it. Therefore, the filter needs to query interface from ISampleGrabber. So you need to query interface as usual. Then you can SetMediaType() using ISampleGrabber. You can use AM_MEDIA_TYPE variable for SetMediaType(). After SetMediaType(), you can connect the filter.

After the filter is connected, find the output pin. Then, again use AM_MEDIA_TYPE for another variable (remember, it is another variable, should not use the variable before, it might work with same variable, but the program might crash). This variable is used for GetConnectedMediaType(), (if both functions can use the same variable, why does it need 2 functions?).

You need a class that inherits ISampleGrabberCB. ISampleGrabberCB has BufferCB() method which is needed. Then, you need to modify the BufferCB() that the buffer can be copied to the public property (or variable), so that the buffer can be accessed publicly. (Please see the example of GrabBitmaps from SDK).

After GetConnectedMediaType(), you might need to SetSampleBuffer(), SetOneShot() and else. Then, SetCallback() is the important one. This function is used to call ISampleGrabber BufferCB() function. Then, from the ISampleGrabberCB object, you can access the buffer.

By doing these, you can access the buffer of the video file. If you copy the buffer for the OpenGL, you might no need the DirectShow video output. Therefore, you can add in a Null Renderer Filter. Then you render the output pin of the Sample Grabber Filter, it will connect to Null Renderer Filter so that you will not see any video. However, you can access the buffer and produce it in OpenGL.

Besides the Source File (Async) Filter, you can use the webcam as source filter. Therefore, use the FindCaptureDevice() function from PlayCap example from SDK, you can get the video capturing source.

Advertisements

Medical and fate


When I was taking care of my father in the hospital, I got some questions.
當我在醫院照顧爸爸的時候,我想到了些問題。

If the medical technology of today, can preserve a person from death, what will be going on? Are human beings really want to live as long as possible in this world?
如果現今的醫學科技,能夠讓一個人不死,這將會怎樣?人類是否真的想在這世界長生?

If the medical technology of today, can preserve a person from dying, but because of carelessness of family members, nurses and doctors, a patient was died. What does it mean? Are we going to accuse the family members or nurses or doctors? I don’t think we should accuse them if the accident is really happened. Because what is happened, is happened. If we accuse someone can change the situation (I mean, the patient will resurrect), then we should accuse someone.
如果現今的醫學科技,能夠讓一個人不死,但因為家庭成員、護士和醫生的疏忽,而造成病人的喪亡。那意味著什麽?我們應該譴責家庭成員、護士或醫生嗎?如果事情真的已經發生了,我是不認為應該譴責他們。因為發生了的事,就已經發生了。如果我們譴責某人能改變狀況(我是指,病人能夠復活),那么我們就應該譴責某人。

Even though the medical treatment is very good, someone is still dying, because of other factors. This is not what we can control now, or may be forever. The fate calls someone to leave, then someone must leave.
縱然現在的醫藥發達,但是有些人還是會因為其它的因素而喪亡。這不是我們現在能掌握的,可能永遠都無法掌握。命運叫人離開時,人就必須離開。

Musou Orochi: Lubu’s item 無雙大蛇:呂布道具


終於開完畫廊全部的圖畫了。最難拿的,大概就是呂布道具。因為吳國外傳終章,士兵實在少。而且要在9分鐘內千人斬。真是難上加難。有人介紹說玩雙人,加起來一千,就可以了。但是我想到,士兵那么少,而且如果一人玩雙人,也挺麻煩的。結果用了他們所說的SL大法(Save-Load大法)。終於在快要用完九分鐘內,千人斬了。
Finally, all the images in the gallery are opened. The most difficult image, should be the image that needs Lubu’s item. Because, Wu’s final chapter extra, the soldiers are too few. Even more, we need to use Lubu to kill 1000 men within 9 minutes. It is too difficult. Some people suggest use 2 players, the sum of both players killing number is 1000 can invoke the item. However, I think that the soldiers are too few, if 1 person play 2 players more, also trouble some. So, I use the other way, that is “save-load technique” (SL technique). Finally, almost finish using 9 minutes, I killed 1000 men.

其實如果用SL大法是很簡單的。最重要要有耐心。只要看到前面有一堆敵人,砍個兩三下(要用蓄力攻擊也可以),儘量在敵人倒地消失前,馬上SL,某些敵人可能還會在。SL個越多次,越有效。所以,可以縮短找敵人的時間。而且,如果敵人很多,揮個幾刀,就可以砍到大概十個左右。但是,一個地點打久了,敵人就會漸漸減少。所以,還是要花個時間騎馬去找敵人。總之,我用SL大法,一次打,就過了。但是,我的武器屬性,雷的等級是10。這我不知道有沒有影響。
Actually, if using SL technique, it is easy. The most important thing is patience. When we see a group of enemies in front, go to slash 2 or 3 times (you can also use charge attack), as far as possible before the enemies falling down and disappeared, immediately save-load, some enemies might still be there. The more you save-load, the more effective. Therefore, this can shorten the time to find the enemy. Even more, if the enemies are many, slash several times, can kill about 10. However, if you spend more time on a place, the enemies will become less and less. So, you still need to ride the horse to find the enemy. As a conclusion, I used this SL technique once, and get the item. But, my Lubu’s weapon element, there is a “thunder” which is level 10. I don’t know whether this affect the result or not.

AVG 8.0 review


Just installed AVG 8.0 recently. The interface has a great change. Besides that, a feature I like most, which AVG 7 doesn’t have, that is “Exceptions”. That is a feature, which you can add in the path or files, that you know there are threats, but you still want them remain in your computer.

This feature is very good. Because sometimes, I want to run a program, but AVG 7 Resident Shield will protect the computer to run the program. In order to continue running the program, I need to turn off Resident Shield. However, doing this is dangerous, because the whole Resident Shield is turned off. With this “exceptions” feature, Resident Shield is still running and protection is still going on, and I can run the program I want.

About the Anti-Spyware feature, I don’t have any comment. Because I also installed other spyware protection software.

Law of Attraction


My brother introduced a book to me, “The Secret” (by Rhonda Byrne). But I didn’t read the book.

The secret in the book is the “Law of Attraction“. And I searched through Wikipedia what is that Law of Attraction. Then I found that, the Law of Attraction is very familiar. Because, you ask, you believe, you work for it, and feel gratitude, then you will get it. Doesn’t the Law of Attraction just seems like “faith” that I know as a Christian?

Diencephalon 間腦


沒有聽過,都不知道;沒有看到,都不相信。
If I never heard it, I don’t know; if I never see it, I don’t believe.

我的親戚的小孩,上了個課程。那個課程可以開發間腦(Diencephalon)。開發了之後有什麽用?蒙上眼睛,還能“看見”。
我們用她做了示範:把她的眼睛蒙上。然後,把許多藍色和紅色的小球放在個盒子里。然後叫她把紅色的拿出來。她都拿了出來。不止如此。我們還拿了小孩的書,有圖案和英文名稱的書,隨便翻了翻,擺在她面前,她摸了摸,都可以知道是什麽圖案,什麽顏色(眼睛還是蒙著的)。
My relative’s child, attended a course. That course can help the children to open their “diencephalon” (middle brain, or inter brain). Why open it? When the children are blindfolded, they still can “see”.
We asked the girl to demonstrate the ability: we blindfolded her. Then, put a lot of blue and read small balls inside a box. Then we asked her to take out all red balls. She could take them out. Not only that, we used a children book, which contains a lot of picture with english name, and randomly opened a page, put in front of her, she touched, and she knew what is the picture, even the colour. She was still blindfolded.

實在不可思議。問她怎么知道,她講她會“看到”。但是,如果把她額頭和額頭以上遮住,她說她“看不見”了。
Too mysterious. Asked her how she knew, she said she can “see”. But, if we blocked her forehead and above her forehead, she said she “cannot see”.

真的實在是神奇,實在不明白。那個課程據說只給小孩。聽說大人很難像小孩那樣開發間腦。
Too mysterious, cannot understand. And they say the course is for children only. And I heard that, adults cannot open their “diencephalon” like children.

可以瀏覽/Can visit the official site:
官方網站Genius Mind Consultancy

Video1
Video2
(以上視頻我還沒看過/Above video I haven’t watched)

File Hosting


Recently, I found that Badongo is not like before. It has the CAPTCHA feature like other 1-click hosting website. So, I try to search for other alternatives.

And I found that, Windows Live SkyDrive offer 5G online file storage. Besides that, it can store personal files, share the files with friends, and also share the files publicly. Therefore, I think this is an alternative to share the file publicly. However, we need to sign up an account before using the service.