Play an MP3 with Media Foundation

An under 100 lines of C++ code sample that plays an MP3 file with Microsoft Media Foundation.

https://github.com/roman380/MediaFoundationMinimalAudioPlayback

Comes from this StackOverflow question, however unlike Microsoft’s sample it does not spread simple code among multiple source code files, gives a quick way to build & run, shows modern C++ practices (including Microsoft Windows Implementation Library in wil branch – the library I recommend to use).

The sample quickly goes over main concepts: topology, nodes, session, events, asynchronous processing. Media Foundation’s “Intelligent Connect” automatically & transparently supplying two Media Foundation Transforms to decode MP3 audio and to fit audio format to acceptable by audio playback device.

Event: MESessionTopologySet
  Node Count: 4
  Node 0: MF_TOPOLOGY_SOURCESTREAM_NODE
    MF_TOPONODE_MEDIASTART
    MF_TOPONODE_SOURCE
    MF_TOPONODE_PRESENTATION_DESCRIPTOR
    MF_TOPONODE_STREAM_DESCRIPTOR
  Node 1: MF_TOPOLOGY_OUTPUT_NODE
    MF_TOPONODE_STREAMID
    MF_TOPONODE_TRANSFORM_OBJECTID {D23E6476-B104-4707-81CB-E1CA19A07016}
  Node 2: MF_TOPOLOGY_TRANSFORM_NODE
    MF_TOPONODE_MARKIN_HERE
    MF_TOPONODE_MARKOUT_HERE
    MF_TOPONODE_DECODER
    MF_TOPONODE_TRANSFORM_OBJECTID {BBEEA841-0A63-4F52-A7AB-A9B3A84ED38A}
  Node 3: MF_TOPOLOGY_TRANSFORM_NODE
    MF_TOPONODE_TRANSFORM_OBJECTID {F447B69E-1884-4A7E-8055-346F74D6EDB3}
Event: MESessionNotifyPresentationTime
Event: MESessionCapabilitiesChanged
  Capabilities: MFSESSIONCAP_START MFSESSIONCAP_SEEK MFSESSIONCAP_RATE_FORWARD MFSESSIONCAP_DOES_NOT_USE_NETWORK
  Change: MFSESSIONCAP_START MFSESSIONCAP_SEEK MFSESSIONCAP_RATE_FORWARD MFSESSIONCAP_DOES_NOT_USE_NETWORK
Event: MESessionTopologyStatus MF_TOPOSTATUS_READY
Event: MESessionTopologyStatus MF_TOPOSTATUS_STARTED_SOURCE
Event: MEExtendedType
Event: MESessionCapabilitiesChanged
  Capabilities: MFSESSIONCAP_START MFSESSIONCAP_SEEK MFSESSIONCAP_PAUSE MFSESSIONCAP_RATE_FORWARD MFSESSIONCAP_DOES_NOT_USE_NETWORK
  Change: MFSESSIONCAP_PAUSE
Event: MESessionStarted
...
Event: MEEndOfPresentation
Event: MESessionTopologyStatus MF_TOPOSTATUS_ENDED
Event: MESessionCapabilitiesChanged
  Capabilities: MFSESSIONCAP_START MFSESSIONCAP_SEEK MFSESSIONCAP_RATE_FORWARD MFSESSIONCAP_DOES_NOT_USE_NETWORK
  Change: MFSESSIONCAP_PAUSE
Event: MESessionEnded

Have fun!

Getting married in Ukraine under martial law

TLDR: You will have hard time marrying a disabled girl in Ukraine 2022/2023.

As you might have heard, Ukraine does not allow its men citizen aged 18 through 60 to leave the country. You might want to think it’s a smart idea but let’s have a dive into the implementation.

According to current legislation in Ukraine there are a few scenarios when the country prohibit border crossing to its citizens on their outbound travel. And those, surprisingly, do NOT include the case of martial law and those who can potentially be drafted. So what the hell is going on?

Ukraine has a legislative branch, a judicial branch, and an executive branch. The highest level normative act, the Constitution of Ukraine, defines that it is the legislative branch who has to define the exception for border crossing. Did legislative branch banned men leave? NO. Executive branch, represented by antisocial gang with the president as a frontman actor, issued its own leave ban and instructed the border service to refer to it and ignore the norms of the Constitution of Ukraine. The judicial branch is suppressed and threatened and so things have remained as this for some time. There are a few collisions that make it hard for legislative branch to issue the ban accurately too, and – no matter what – legislative branch after almost a year of martial law did not even make an attempt to legalize the ban in a nicer way.

Men in Ukraine are in a sort of a trap: explosions, no jobs, the usurpers of power are treating men as resource for fights where men are thrown and those who want to stay alive are supposed to purchase equipment at their own expense. And, of course, no leave from Ukraine. Many look for loopholes and exceptions, and one of the stories I recently heard encouraged me to share and write this post.

One person from my home city Kharkov, Ukraine complained that he had troubles with his plan. In frustration of being locked in Ukraine, he decided to find and marry disabled girl because this sort of disability and care needs opens an opportunity to cross border for a men who is a carer of such disabled person and is also a first degree relative. He found a woman and they got into agreement.

They went to register their marriage in Lvov, Ukraine in the Western part of the country assumed to be safer (not really for men) and what they heard was this: “We will not register your marriage because you’re trying to escape the draft and you want to flee. Your mutual consent to get married? We don’t give a fuck.”. They drove to another neighboring region to make another attempt and they found that they are already sort of banned by name across the offices, and new office refused immediately because “you already tried to do it yesterday in Lvov”.

On helping Ukraine (update)

Recently I explained why you should not express your support of Ukraine by sending money to various disgusting organizations even if they have fancy looking names, such as, including, and maybe even first of all those with labels “initiative of the president of Ukraine”.

The organization I recommended has been gradually shifting to forwarding the donations to military support, so I no longer recommend even them.

Seriously, consider better ways of wasting your money.

You might think you are expressing support of Ukraine by tearing your own penny from you, but the only thing you are really showing is that you were fooled and you couldn’t even figure out what happened.

If you are curious about the level of the lawlessness in Ukraine and what men aged 18 through 60 have to deal with, please ask your Russian speaking friends to re-tell the details of these clips below.

Continue reading →

On helping Ukraine

I have been asked a few times about helping Ukraine in connection with the happening on the East of Ukraine.

The first thing however, is to have clear understanding about who is fighting with whom and for what exactly. The mainstream media story about Russian president going nuts and attack of Ukraine, and then all this denazification, and that Ukrainian army is so well prepared that it defends successfully against the second army of the world… This is bullshit is literally every word of it. However after a few attempts to put this all together nicely in a compact way, I failed.

Back to helping Ukraine.

Ukraine as a state wants this war. In fact, the administrations of Ukraine and Russian Federation are much closer to one another than to their own population. They arranged this fight cooperatively and keep it running, neglecting the needs of people and the damage caused. For this reason, nearly each and every centralized way to support Ukraine will end up being fueling the war.

For example, United24 the initiative of the President of Ukraine… First, remember that it is a professional artist looking at you from the webpage. They created a way to collect donations in an easy way, for those who want to help it is the way to easily release the intent to help, and also to feel involvement that they are on the “right” side of the story. This money will not help real people and I would not recommend giving a single penny to the scoundrels.

The reality is this. Ukraine has no its own economy, it is all destroyed, and Ukraine is living now on external funding, like a seriously ill patient on a ventilator, it will crash the next second after the device is turned off. A dollar you spend to crooks like mentioned in the previous paragraph means that they will need a dollar less from external source (the funding which is generously given them anyway) without absolutely any effect. Regional authorities in Ukraine are fully dependent on the government and are not only spending funds to keep their positions, they even monopolized the helping activities and are pushing away competing volunteers.

The second important part is the definition of help. In Ukraine the government throws people into combat without training, without equipment. They force you into trenches “because of constitutional obligation” even though they trampled on the constitution. Same time you buy armor, drones, collimators, thermal visors… yourselves. Or you fundraise to get these purchases funded, and those “volunteers” who shop for this stuff abroad take their cut off the supply chain. So it is literally making war at expense of people you are throwing into this war. Donations given to random volunteers, if they pass the hands of government and militaries in which case they are simply stolen, will most likely end up in some buying a used all-wheel driving car in Europe for the needs of subdivision X. That is, what starts in helping Ukraine will just fuel the war further.

If, however, the understanding of help is to help to those who suffered, which I hope is the case, then finding people who stay away from military rage is really difficult. Only small unknown groups of people can perform this activity in first place because otherwise they would be absorbed my local authorities who eradicates competition in the field of helping.

I personally know just one group of people who sticks to suffered. I mentioned them before and my trust in them is because I know a few of them in person. These people have accounts on Facebook and Telegram, however they are not the best in arranging collection of donations: in Ukraine people are sending money instantly by card number and SWIFT is an option for foreigners even though it’s not something those foreigners are comfortable with. This is the best I know, and I am aware of one another who helps those who have problems with animals (dogs, horses, relocation of those etc.). If you decided to donate and you don’t know whom exactly, the best I can suggest is to get in touch with these people and talk to them.

Encoding multiple concurrent real time video streams with NVIDIA hardware video encoder

NVIDIA consumer grade hardware provides support nowadays for – mostly – three concurrent sessions. The support matrix is provided here: Video Encode and Decode GPU Support Matrix | NVIDIA Developer.

This small tool starts video encoding with given signal parameters (resolution, rate) and provides an easy way to measure load of GPU video encoding engine.

The application creates a Media Foundation pipeline with signal source based on software implementation of Direct2D and system memory backed RGB32 media samples. Then it is connected to H.264 video encoder MFT (not a stock one, a customer wrapper over NVIDIA SDK) with encoding profile around the lowest latency and maximal performance. The output is being fed into null media sink, with or without dumping created content to disk.

Video feed configuration is defined in associated JSON file:

{
    "Tasks": [
        {
            "RateN": 60,
            "RateD": 1,
            "SizeW": 1920,
            "SizeH": 1080
        },
        {
            "RateN": 60,
            "RateD": 1,
            "SizeW": 1920,
            "SizeH": 1080
        },
        {
            "RateN": 60,
            "RateD": 1,
            "SizeW": 1920,
            "SizeH": 1080
        }
    ],
    "Duration": 60
}

The GPU load is displayed by task manager as encoding goes, for example three 1920×1080@60 streams load NVIDIA GeForce RTX 2060 by ~45%.

Command line parameter -ws may be added to write streams to disk, the produced .h264 files can be played out by Media Player Classic or other players:

The archive below contains 32 and 64 bit builds of the utility with configuration files. Have fun!

For your own good we enforced real-time protection…

Ostatnio spedzilem dosc duzo czasu na sprawdzaniu problemu z nieoczekiwanie slaba wydajnoscia w… Oh wait I am writing in English here. So, recently I spent rather a lot of time checking a problem with unexpectedly poor performance in a legacy DirectShow real-time video encoding pipeline based on NVIDIA GPU hardware encoding.

There was also a pre-history in this story. A colleague engineer in a contracted organization complained on a similar symptom and it took me not too much time to see that some third party antivirus is showing unexpected load numbers along with video recording activity. It appeared that it was not so easy to disable the antivirus because it was managed by his company on the corporate development system, so I just left him with suggestion to find ways to kill the offender.

However then later a similar thing happened on my own system as well. Even though it was a good reason to fine tune other aspects of the pipeline it came to my attention that freezing parts are happening in disk access to produced file in MP4 Multiplexer filter here.

The whole situation is ridiculous. Geraint designed these filter back in 2009 and then those would work nicely even ten years earlier if they existed earlier. Now with all the performance progress in hardware the same code exhibits extremely weird performance behavior because… of this thing:

Real-time protection of Windows defender interferes with recording so bad that it holds the entire upstream pipeline and forces frame drops in already a few second after start. This thing is also known to automatically re-enable itself if you turn it off so ironically even when I turn the switch on the image above off, after just a few minutes of rock solid operation it re-enabled the protection and the recording defect immediately returned back (okay, maybe it popped a notification about disabled protection and I accidentally clicked).

What’s wrong with this thing at all? Is it really that important to routinely check MP4 files being written with an impact so high that you can no longer use the hardware to do that original most important operation.

Knowing this, of course, it is important to restrain the scoundrel: