Bring Your Animations to H264/HEVC Video
Shao Voon Wong - 26/Jul/2020
Shao Voon Wong - 26/Jul/2020
[SHOWTOGROUPS=4,20,22]
Bring your animations to H264/HEVC video using C++ and C# with h/w acceleration
Table of Contents
Last year, I introduced a single header windows-based video encoder for OpenGL that works on Windows 7 and above. See the above video demo! I have decoupled it from the OpenGL thread and make it simpler to encode a 2D frame. All you need is to fill in the frame buffer, frame by frame to create your animations. In this article, I use GDI+ since I am most familiar with it but you are welcome to use your favourite graphics library; The video encoder is not coupled with GDI+. HEVC codec used to come bundled with Windows 10 but now Microsoft has removed it and put it on sale in the Microsoft Store. That HEVC codec has a quality issue where higher bitrate has to be given to maintain the same quality as H264 encoded video. Make sure the video file is not opened or locked by video player before you begin to write to it. The new H264Writer C++ constructor is followed by C# counterpart:
Hide Copy Code
H264Writer(const wchar_t* mp3_file, const wchar_t* dest_file, VideoCodec codec,
int width, int height, int fps, int duration /*in milliseconds*/,
FrameRenderer* frameRenderer, UINT32 bitrate = 4000000);
Hide Copy Code
H264Writer(string mp3_file, string dest_file, VideoCodec codec,
int width, int height, int fps, int duration /*in milliseconds*/,
FrameRenderer* frameRenderer, uint bitrate);
The mp3_file parameter is a MP3 file path (which can be empty if you do not want any audio) and dest_file parameter is the resultant video file. codec parameter can be either H264 or HEVC. The width and height parameters refer to the video width and height. fps parameter is the frames per second of the video which I usually specified as 30 or 60. duration parameter refers to the video duration in milliseconds which can be set as -1 to indicate the video duration to be the same as the MP3. bitrate parameters refers to the video bitrate of bytes per second. Remember to set the bitrate higher for high resolution video and HEVC. The Render function signature of pure virtual FrameRenderer is below. The width and height is the video dimension. fps is the frames per second while frame_cnt is the frame count which auto-increments itself on every frame. pixels parameter is the single dimensional array to be filled up with your bitmap data. The return value should be false for catastrophic error which encoding shall be stopped. FrameRenderer is a class whose Render method is called on every frame, it should be implemented by the user.
Hide Copy Code
class FrameRenderer
{
public:
virtual bool Render(int width, int height, int fps, int frame_cnt, UINT32* pixels) = 0;
};
In C#, FrameRenderer is an interface.
Hide Copy Code
interface FrameRenderer
{
public bool Render(int width, int height, int fps, int frame_cnt, uint[] pixels);
};
[/SHOWTOGROUPS]
Bring your animations to H264/HEVC video using C++ and C# with h/w acceleration
Table of Contents
- Introduction
- Red Video
- One JPEG Video
- Two JPEG Video
- Text Animation Video
- Can I use this H264Writer for OpenGL?
- Hardware Acceleration
- Quality parameters in Constructor in v0.4.2
- History
Last year, I introduced a single header windows-based video encoder for OpenGL that works on Windows 7 and above. See the above video demo! I have decoupled it from the OpenGL thread and make it simpler to encode a 2D frame. All you need is to fill in the frame buffer, frame by frame to create your animations. In this article, I use GDI+ since I am most familiar with it but you are welcome to use your favourite graphics library; The video encoder is not coupled with GDI+. HEVC codec used to come bundled with Windows 10 but now Microsoft has removed it and put it on sale in the Microsoft Store. That HEVC codec has a quality issue where higher bitrate has to be given to maintain the same quality as H264 encoded video. Make sure the video file is not opened or locked by video player before you begin to write to it. The new H264Writer C++ constructor is followed by C# counterpart:
Hide Copy Code
H264Writer(const wchar_t* mp3_file, const wchar_t* dest_file, VideoCodec codec,
int width, int height, int fps, int duration /*in milliseconds*/,
FrameRenderer* frameRenderer, UINT32 bitrate = 4000000);
Hide Copy Code
H264Writer(string mp3_file, string dest_file, VideoCodec codec,
int width, int height, int fps, int duration /*in milliseconds*/,
FrameRenderer* frameRenderer, uint bitrate);
The mp3_file parameter is a MP3 file path (which can be empty if you do not want any audio) and dest_file parameter is the resultant video file. codec parameter can be either H264 or HEVC. The width and height parameters refer to the video width and height. fps parameter is the frames per second of the video which I usually specified as 30 or 60. duration parameter refers to the video duration in milliseconds which can be set as -1 to indicate the video duration to be the same as the MP3. bitrate parameters refers to the video bitrate of bytes per second. Remember to set the bitrate higher for high resolution video and HEVC. The Render function signature of pure virtual FrameRenderer is below. The width and height is the video dimension. fps is the frames per second while frame_cnt is the frame count which auto-increments itself on every frame. pixels parameter is the single dimensional array to be filled up with your bitmap data. The return value should be false for catastrophic error which encoding shall be stopped. FrameRenderer is a class whose Render method is called on every frame, it should be implemented by the user.
Hide Copy Code
class FrameRenderer
{
public:
virtual bool Render(int width, int height, int fps, int frame_cnt, UINT32* pixels) = 0;
};
In C#, FrameRenderer is an interface.
Hide Copy Code
interface FrameRenderer
{
public bool Render(int width, int height, int fps, int frame_cnt, uint[] pixels);
};
[/SHOWTOGROUPS]