diff --git a/.gitignore b/.gitignore index 3f60b12f3..9857d6531 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,6 @@ tests/gold/ tests/results/ tests/ted/__pycache__/ tests/test_results.json +.vs +samples/_build +*.user \ No newline at end of file diff --git a/README.md b/README.md index d343fd915..967ac0719 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ You may also wish to visit Intel Media Server Studio [support page](https://soft # System requirements -**Operating System:** Linux +**Operating System:** Linux , Windows (not all features are supported in Windows build - see Known Limitations for details) **Software:** * [LibVA](https://github.com/intel/libva) @@ -125,6 +125,8 @@ Once Media SDK was built with ITT support, enable it in a runtime creating per-u ```sh Output=0x10 ``` +# Known limitations +Windows build contains only samples and dispatcher library. MediaSDK library DLL is provided with Windows GFX driver. # Recommendations diff --git a/api/include/mfxdefs.h b/api/include/mfxdefs.h index ef662cff3..4d344698e 100644 --- a/api/include/mfxdefs.h +++ b/api/include/mfxdefs.h @@ -54,8 +54,13 @@ extern "C" #define __INT64 long long #define __UINT64 unsigned long long - #define MFX_CDECL - #define MFX_STDCALL +#ifdef _WIN32 + #define MFX_CDECL __cdecl + #define MFX_STDCALL __stdcall +#else + #define MFX_CDECL + #define MFX_STDCALL +#endif /* _WIN32 */ #define MFX_INFINITE 0xFFFFFFFF diff --git a/samples/AllSamples.sln b/samples/AllSamples.sln new file mode 100644 index 000000000..d6da2e97c --- /dev/null +++ b/samples/AllSamples.sln @@ -0,0 +1,158 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27703.2000 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sample_common", "sample_common\sample_common.vcxproj", "{5FADB243-53C3-4776-A20F-8BD65C10CF41}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sample_decode", "sample_decode\sample_decode.vcxproj", "{F22D082D-FDDD-4946-A11B-5276F0994C74}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sample_encode", "sample_encode\sample_encode.vcxproj", "{33834A6A-4466-4339-8C7C-FBA250BC1957}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sample_multi_transcode", "sample_multi_transcode\sample_multi_transcode.vcxproj", "{58D93FCA-FDE9-4D22-8E8D-402EEBACF2BD}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sample_rotate_plugin", "sample_plugins\rotate_cpu\sample_rotate_plugin.vcxproj", "{C5F515E3-379A-4678-B947-509B3603D826}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sample_plugin_opencl", "sample_plugins\rotate_opencl\sample_plugin_opencl.vcxproj", "{E90AE63E-F219-4389-A955-26E78BD1BD9A}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "vpp_plugin", "sample_plugins\vpp_plugin\vpp_plugin.vcxproj", "{474EF95D-3579-411B-9DDB-88B49F923D65}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Plugins", "Plugins", "{E4C0D92D-FE15-43AE-A86D-A9866C0A50E6}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sample_vpp", "sample_vpp\sample_vpp.vcxproj", "{A0872728-B771-49B5-93FA-D7140631A829}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sample_camera", "sample_camera\sample_camera.vcxproj", "{A812C44D-0255-4796-9042-BCAE99963E7D}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug_WithDebugAPI|Win32 = Debug_WithDebugAPI|Win32 + Debug_WithDebugAPI|x64 = Debug_WithDebugAPI|x64 + Debug|Win32 = Debug|Win32 + Debug|x64 = Debug|x64 + Release|Win32 = Release|Win32 + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Debug_WithDebugAPI|Win32.ActiveCfg = Debug_WithDebugAPI|Win32 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Debug_WithDebugAPI|Win32.Build.0 = Debug_WithDebugAPI|Win32 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Debug_WithDebugAPI|x64.ActiveCfg = Debug_WithDebugAPI|x64 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Debug_WithDebugAPI|x64.Build.0 = Debug_WithDebugAPI|x64 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Debug|Win32.ActiveCfg = Debug|Win32 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Debug|Win32.Build.0 = Debug|Win32 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Debug|x64.ActiveCfg = Debug|x64 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Debug|x64.Build.0 = Debug|x64 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Release|Win32.ActiveCfg = Release|Win32 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Release|Win32.Build.0 = Release|Win32 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Release|x64.ActiveCfg = Release|x64 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Release|x64.Build.0 = Release|x64 + {F22D082D-FDDD-4946-A11B-5276F0994C74}.Debug_WithDebugAPI|Win32.ActiveCfg = Debug_WithDebugAPI|Win32 + {F22D082D-FDDD-4946-A11B-5276F0994C74}.Debug_WithDebugAPI|Win32.Build.0 = Debug_WithDebugAPI|Win32 + {F22D082D-FDDD-4946-A11B-5276F0994C74}.Debug_WithDebugAPI|x64.ActiveCfg = Debug_WithDebugAPI|x64 + {F22D082D-FDDD-4946-A11B-5276F0994C74}.Debug_WithDebugAPI|x64.Build.0 = Debug_WithDebugAPI|x64 + {F22D082D-FDDD-4946-A11B-5276F0994C74}.Debug|Win32.ActiveCfg = Debug|Win32 + {F22D082D-FDDD-4946-A11B-5276F0994C74}.Debug|Win32.Build.0 = Debug|Win32 + {F22D082D-FDDD-4946-A11B-5276F0994C74}.Debug|x64.ActiveCfg = Debug|x64 + {F22D082D-FDDD-4946-A11B-5276F0994C74}.Debug|x64.Build.0 = Debug|x64 + {F22D082D-FDDD-4946-A11B-5276F0994C74}.Debug|x64.Deploy.0 = Debug|x64 + {F22D082D-FDDD-4946-A11B-5276F0994C74}.Release|Win32.ActiveCfg = Release|Win32 + {F22D082D-FDDD-4946-A11B-5276F0994C74}.Release|Win32.Build.0 = Release|Win32 + {F22D082D-FDDD-4946-A11B-5276F0994C74}.Release|x64.ActiveCfg = Release|x64 + {F22D082D-FDDD-4946-A11B-5276F0994C74}.Release|x64.Build.0 = Release|x64 + {33834A6A-4466-4339-8C7C-FBA250BC1957}.Debug_WithDebugAPI|Win32.ActiveCfg = Debug_WithDebugAPI|Win32 + {33834A6A-4466-4339-8C7C-FBA250BC1957}.Debug_WithDebugAPI|Win32.Build.0 = Debug_WithDebugAPI|Win32 + {33834A6A-4466-4339-8C7C-FBA250BC1957}.Debug_WithDebugAPI|x64.ActiveCfg = Debug_WithDebugAPI|x64 + {33834A6A-4466-4339-8C7C-FBA250BC1957}.Debug_WithDebugAPI|x64.Build.0 = Debug_WithDebugAPI|x64 + {33834A6A-4466-4339-8C7C-FBA250BC1957}.Debug|Win32.ActiveCfg = Debug|Win32 + {33834A6A-4466-4339-8C7C-FBA250BC1957}.Debug|Win32.Build.0 = Debug|Win32 + {33834A6A-4466-4339-8C7C-FBA250BC1957}.Debug|x64.ActiveCfg = Debug|x64 + {33834A6A-4466-4339-8C7C-FBA250BC1957}.Debug|x64.Build.0 = Debug|x64 + {33834A6A-4466-4339-8C7C-FBA250BC1957}.Debug|x64.Deploy.0 = Debug|x64 + {33834A6A-4466-4339-8C7C-FBA250BC1957}.Release|Win32.ActiveCfg = Release|Win32 + {33834A6A-4466-4339-8C7C-FBA250BC1957}.Release|Win32.Build.0 = Release|Win32 + {33834A6A-4466-4339-8C7C-FBA250BC1957}.Release|x64.ActiveCfg = Release|x64 + {33834A6A-4466-4339-8C7C-FBA250BC1957}.Release|x64.Build.0 = Release|x64 + {58D93FCA-FDE9-4D22-8E8D-402EEBACF2BD}.Debug_WithDebugAPI|Win32.ActiveCfg = Debug_WithDebugAPI|Win32 + {58D93FCA-FDE9-4D22-8E8D-402EEBACF2BD}.Debug_WithDebugAPI|Win32.Build.0 = Debug_WithDebugAPI|Win32 + {58D93FCA-FDE9-4D22-8E8D-402EEBACF2BD}.Debug_WithDebugAPI|x64.ActiveCfg = Debug_WithDebugAPI|x64 + {58D93FCA-FDE9-4D22-8E8D-402EEBACF2BD}.Debug_WithDebugAPI|x64.Build.0 = Debug_WithDebugAPI|x64 + {58D93FCA-FDE9-4D22-8E8D-402EEBACF2BD}.Debug|Win32.ActiveCfg = Debug|Win32 + {58D93FCA-FDE9-4D22-8E8D-402EEBACF2BD}.Debug|Win32.Build.0 = Debug|Win32 + {58D93FCA-FDE9-4D22-8E8D-402EEBACF2BD}.Debug|x64.ActiveCfg = Debug|x64 + {58D93FCA-FDE9-4D22-8E8D-402EEBACF2BD}.Debug|x64.Build.0 = Debug|x64 + {58D93FCA-FDE9-4D22-8E8D-402EEBACF2BD}.Release|Win32.ActiveCfg = Release|Win32 + {58D93FCA-FDE9-4D22-8E8D-402EEBACF2BD}.Release|Win32.Build.0 = Release|Win32 + {58D93FCA-FDE9-4D22-8E8D-402EEBACF2BD}.Release|x64.ActiveCfg = Release|x64 + {58D93FCA-FDE9-4D22-8E8D-402EEBACF2BD}.Release|x64.Build.0 = Release|x64 + {C5F515E3-379A-4678-B947-509B3603D826}.Debug_WithDebugAPI|Win32.ActiveCfg = Debug_WithDebugAPI|Win32 + {C5F515E3-379A-4678-B947-509B3603D826}.Debug_WithDebugAPI|Win32.Build.0 = Debug_WithDebugAPI|Win32 + {C5F515E3-379A-4678-B947-509B3603D826}.Debug_WithDebugAPI|x64.ActiveCfg = Debug_WithDebugAPI|x64 + {C5F515E3-379A-4678-B947-509B3603D826}.Debug_WithDebugAPI|x64.Build.0 = Debug_WithDebugAPI|x64 + {C5F515E3-379A-4678-B947-509B3603D826}.Debug|Win32.ActiveCfg = Debug|Win32 + {C5F515E3-379A-4678-B947-509B3603D826}.Debug|Win32.Build.0 = Debug|Win32 + {C5F515E3-379A-4678-B947-509B3603D826}.Debug|x64.ActiveCfg = Debug|x64 + {C5F515E3-379A-4678-B947-509B3603D826}.Debug|x64.Build.0 = Debug|x64 + {C5F515E3-379A-4678-B947-509B3603D826}.Release|Win32.ActiveCfg = Release|Win32 + {C5F515E3-379A-4678-B947-509B3603D826}.Release|Win32.Build.0 = Release|Win32 + {C5F515E3-379A-4678-B947-509B3603D826}.Release|x64.ActiveCfg = Release|x64 + {C5F515E3-379A-4678-B947-509B3603D826}.Release|x64.Build.0 = Release|x64 + {E90AE63E-F219-4389-A955-26E78BD1BD9A}.Debug_WithDebugAPI|Win32.ActiveCfg = Debug_WithDebugAPI|Win32 + {E90AE63E-F219-4389-A955-26E78BD1BD9A}.Debug_WithDebugAPI|Win32.Build.0 = Debug_WithDebugAPI|Win32 + {E90AE63E-F219-4389-A955-26E78BD1BD9A}.Debug_WithDebugAPI|x64.ActiveCfg = Debug_WithDebugAPI|x64 + {E90AE63E-F219-4389-A955-26E78BD1BD9A}.Debug_WithDebugAPI|x64.Build.0 = Debug_WithDebugAPI|x64 + {E90AE63E-F219-4389-A955-26E78BD1BD9A}.Debug|Win32.ActiveCfg = Debug|Win32 + {E90AE63E-F219-4389-A955-26E78BD1BD9A}.Debug|Win32.Build.0 = Debug|Win32 + {E90AE63E-F219-4389-A955-26E78BD1BD9A}.Debug|x64.ActiveCfg = Debug|x64 + {E90AE63E-F219-4389-A955-26E78BD1BD9A}.Debug|x64.Build.0 = Debug|x64 + {E90AE63E-F219-4389-A955-26E78BD1BD9A}.Release|Win32.ActiveCfg = Release|Win32 + {E90AE63E-F219-4389-A955-26E78BD1BD9A}.Release|Win32.Build.0 = Release|Win32 + {E90AE63E-F219-4389-A955-26E78BD1BD9A}.Release|x64.ActiveCfg = Release|x64 + {E90AE63E-F219-4389-A955-26E78BD1BD9A}.Release|x64.Build.0 = Release|x64 + {474EF95D-3579-411B-9DDB-88B49F923D65}.Debug_WithDebugAPI|Win32.ActiveCfg = Debug_WithDebugAPI|Win32 + {474EF95D-3579-411B-9DDB-88B49F923D65}.Debug_WithDebugAPI|Win32.Build.0 = Debug_WithDebugAPI|Win32 + {474EF95D-3579-411B-9DDB-88B49F923D65}.Debug_WithDebugAPI|x64.ActiveCfg = Debug_WithDebugAPI|x64 + {474EF95D-3579-411B-9DDB-88B49F923D65}.Debug_WithDebugAPI|x64.Build.0 = Debug_WithDebugAPI|x64 + {474EF95D-3579-411B-9DDB-88B49F923D65}.Debug|Win32.ActiveCfg = Debug|Win32 + {474EF95D-3579-411B-9DDB-88B49F923D65}.Debug|Win32.Build.0 = Debug|Win32 + {474EF95D-3579-411B-9DDB-88B49F923D65}.Debug|x64.ActiveCfg = Debug|x64 + {474EF95D-3579-411B-9DDB-88B49F923D65}.Debug|x64.Build.0 = Debug|x64 + {474EF95D-3579-411B-9DDB-88B49F923D65}.Release|Win32.ActiveCfg = Release|Win32 + {474EF95D-3579-411B-9DDB-88B49F923D65}.Release|Win32.Build.0 = Release|Win32 + {474EF95D-3579-411B-9DDB-88B49F923D65}.Release|x64.ActiveCfg = Release|x64 + {474EF95D-3579-411B-9DDB-88B49F923D65}.Release|x64.Build.0 = Release|x64 + {A0872728-B771-49B5-93FA-D7140631A829}.Debug_WithDebugAPI|Win32.ActiveCfg = Debug_WithDebugAPI|Win32 + {A0872728-B771-49B5-93FA-D7140631A829}.Debug_WithDebugAPI|Win32.Build.0 = Debug_WithDebugAPI|Win32 + {A0872728-B771-49B5-93FA-D7140631A829}.Debug_WithDebugAPI|x64.ActiveCfg = Debug_WithDebugAPI|x64 + {A0872728-B771-49B5-93FA-D7140631A829}.Debug_WithDebugAPI|x64.Build.0 = Debug_WithDebugAPI|x64 + {A0872728-B771-49B5-93FA-D7140631A829}.Debug|Win32.ActiveCfg = Debug|Win32 + {A0872728-B771-49B5-93FA-D7140631A829}.Debug|Win32.Build.0 = Debug|Win32 + {A0872728-B771-49B5-93FA-D7140631A829}.Debug|x64.ActiveCfg = Debug|x64 + {A0872728-B771-49B5-93FA-D7140631A829}.Debug|x64.Build.0 = Debug|x64 + {A0872728-B771-49B5-93FA-D7140631A829}.Release|Win32.ActiveCfg = Release|Win32 + {A0872728-B771-49B5-93FA-D7140631A829}.Release|Win32.Build.0 = Release|Win32 + {A0872728-B771-49B5-93FA-D7140631A829}.Release|x64.ActiveCfg = Release|x64 + {A0872728-B771-49B5-93FA-D7140631A829}.Release|x64.Build.0 = Release|x64 + {A812C44D-0255-4796-9042-BCAE99963E7D}.Debug_WithDebugAPI|Win32.ActiveCfg = Debug_WithDebugAPI|Win32 + {A812C44D-0255-4796-9042-BCAE99963E7D}.Debug_WithDebugAPI|Win32.Build.0 = Debug_WithDebugAPI|Win32 + {A812C44D-0255-4796-9042-BCAE99963E7D}.Debug_WithDebugAPI|x64.ActiveCfg = Debug_WithDebugAPI|x64 + {A812C44D-0255-4796-9042-BCAE99963E7D}.Debug_WithDebugAPI|x64.Build.0 = Debug_WithDebugAPI|x64 + {A812C44D-0255-4796-9042-BCAE99963E7D}.Debug|Win32.ActiveCfg = Debug|Win32 + {A812C44D-0255-4796-9042-BCAE99963E7D}.Debug|Win32.Build.0 = Debug|Win32 + {A812C44D-0255-4796-9042-BCAE99963E7D}.Debug|x64.ActiveCfg = Debug|x64 + {A812C44D-0255-4796-9042-BCAE99963E7D}.Debug|x64.Build.0 = Debug|x64 + {A812C44D-0255-4796-9042-BCAE99963E7D}.Release|Win32.ActiveCfg = Release|Win32 + {A812C44D-0255-4796-9042-BCAE99963E7D}.Release|Win32.Build.0 = Release|Win32 + {A812C44D-0255-4796-9042-BCAE99963E7D}.Release|x64.ActiveCfg = Release|x64 + {A812C44D-0255-4796-9042-BCAE99963E7D}.Release|x64.Build.0 = Release|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {C5F515E3-379A-4678-B947-509B3603D826} = {E4C0D92D-FE15-43AE-A86D-A9866C0A50E6} + {E90AE63E-F219-4389-A955-26E78BD1BD9A} = {E4C0D92D-FE15-43AE-A86D-A9866C0A50E6} + {474EF95D-3579-411B-9DDB-88B49F923D65} = {E4C0D92D-FE15-43AE-A86D-A9866C0A50E6} + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {E22D5BBD-4305-4E8F-B335-72F3061EEF73} + EndGlobalSection +EndGlobal diff --git a/samples/sample_camera/include/camera_render.h b/samples/sample_camera/include/camera_render.h new file mode 100644 index 000000000..b6e078493 --- /dev/null +++ b/samples/sample_camera/include/camera_render.h @@ -0,0 +1,107 @@ +/******************************************************************************\ +Copyright (c) 2005-2018, Intel Corporation +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +This sample was distributed or derived from the Intel's Media Samples package. +The original version of this sample may be obtained from https://software.intel.com/en-us/intel-media-server-studio +or https://software.intel.com/en-us/media-client-solutions-support. +\**********************************************************************************/ + + +#ifndef __CAMERA_D3D_RENDER_H__ +#define __CAMERA_D3D_RENDER_H__ + +#if defined(_WIN32) || defined(_WIN64) + +#pragma warning(disable : 4201) +#include +#include +#include +#include +#endif + +#include "mfxstructures.h" +#include "mfxvideo.h" + +#include "hw_device.h" + +typedef void* WindowHandle; +typedef void* Handle; + +#if defined(_WIN32) || defined(_WIN64) + +struct sWindowParams +{ + LPCTSTR lpClassName; + LPCTSTR lpWindowName; + DWORD dwStyle; + int nx; + int ny; + int ncell; + int nAdapter; + int nMaxFPS; + int nWidth; + int nHeight; + HWND hWndParent; + HMENU hMenu; + HINSTANCE hInstance; + LPVOID lpParam; + bool bFullScreen; // Stretch window to full screen +}; + +class CCameraD3DRender +{ +public: + + CCameraD3DRender(); + virtual ~CCameraD3DRender(); + + virtual mfxStatus Init(sWindowParams pWParams); + virtual mfxStatus RenderFrame(mfxFrameSurface1 *pSurface, mfxFrameAllocator *pmfxAlloc); + + virtual void Close(); + + + HWND GetWindowHandle() { return m_Hwnd; } + + VOID OnDestroy(HWND hwnd); + VOID OnKey(HWND hwnd, UINT vk, BOOL fDown, int cRepeat, UINT flags); + VOID ChangeWindowSize(bool bFullScreen); + + void SetHWDevice(CHWDevice *dev) + { + m_hwdev = dev; + } +protected: + void AdjustWindowRect(RECT *rect); + + CHWDevice *m_hwdev; + + sWindowParams m_sWindowParams; + HWND m_Hwnd; + RECT m_rect; + DWORD m_style; + + bool EnableDwmQueuing(); + static BOOL CALLBACK MonitorEnumProc(HMONITOR ,HDC ,LPRECT lprcMonitor,LPARAM dwData); + + bool m_bDwmEnabled; + LARGE_INTEGER m_LastInputTime; + LARGE_INTEGER m_Freq; + int m_nFrames; + int m_nMonitorCurrent; + ::RECT m_RectWindow; +}; +#endif // #if defined(_WIN32) || defined(_WIN64) + +#endif // __CAMERA_D3D_RENDER_H__ \ No newline at end of file diff --git a/samples/sample_camera/include/camera_sysmem_allocator.h b/samples/sample_camera/include/camera_sysmem_allocator.h new file mode 100644 index 000000000..ce533b123 --- /dev/null +++ b/samples/sample_camera/include/camera_sysmem_allocator.h @@ -0,0 +1,81 @@ +/******************************************************************************\ +Copyright (c) 2005-2018, Intel Corporation +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +This sample was distributed or derived from the Intel's Media Samples package. +The original version of this sample may be obtained from https://software.intel.com/en-us/intel-media-server-studio +or https://software.intel.com/en-us/media-client-solutions-support. +\**********************************************************************************/ + +#ifndef __CAM_SYSMEM_ALLOCATOR_H__ +#define __CAM_SYSMEM_ALLOCATOR_H__ + +#include +#include "base_allocator.h" + +struct sBuffer +{ + mfxU32 id; + mfxU32 nbytes; + mfxU16 type; +}; + +struct sFrame +{ + mfxU32 id; + mfxFrameInfo info; +}; + +struct CamSysMemAllocatorParams : mfxAllocatorParams +{ + CamSysMemAllocatorParams() + : mfxAllocatorParams(), + pBufferAllocator(0), alignment(0) { } + MFXBufferAllocator *pBufferAllocator; + mfxI32 alignment; +}; + +class CamSysMemFrameAllocator: public BaseFrameAllocator +{ +public: + CamSysMemFrameAllocator(); + virtual ~CamSysMemFrameAllocator(); + + virtual mfxStatus Init(mfxAllocatorParams *pParams); + virtual mfxStatus Close(); + virtual mfxStatus LockFrame(mfxMemId mid, mfxFrameData *ptr); + virtual mfxStatus UnlockFrame(mfxMemId mid, mfxFrameData *ptr); + virtual mfxStatus GetFrameHDL(mfxMemId mid, mfxHDL *handle); + +protected: + virtual mfxStatus CheckRequestType(mfxFrameAllocRequest *request); + virtual mfxStatus ReleaseResponse(mfxFrameAllocResponse *response); + virtual mfxStatus AllocImpl(mfxFrameAllocRequest *request, mfxFrameAllocResponse *response); + + MFXBufferAllocator *m_pBufferAllocator; + bool m_bOwnBufferAllocator; + mfxU32 m_alignment; +}; + +class CamSysMemBufferAllocator : public MFXBufferAllocator +{ +public: + CamSysMemBufferAllocator(); + virtual ~CamSysMemBufferAllocator(); + virtual mfxStatus AllocBuffer(mfxU32 nbytes, mfxU16 type, mfxMemId *mid); + virtual mfxStatus LockBuffer(mfxMemId mid, mfxU8 **ptr); + virtual mfxStatus UnlockBuffer(mfxMemId mid); + virtual mfxStatus FreeBuffer(mfxMemId mid); +}; + +#endif // __CAM_SYSMEM_ALLOCATOR_H__ \ No newline at end of file diff --git a/samples/sample_camera/include/pipeline_camera.h b/samples/sample_camera/include/pipeline_camera.h new file mode 100644 index 000000000..a95638b93 --- /dev/null +++ b/samples/sample_camera/include/pipeline_camera.h @@ -0,0 +1,192 @@ +/******************************************************************************\ +Copyright (c) 2005-2018, Intel Corporation +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +This sample was distributed or derived from the Intel's Media Samples package. +The original version of this sample may be obtained from https://software.intel.com/en-us/intel-media-server-studio +or https://software.intel.com/en-us/media-client-solutions-support. +\**********************************************************************************/ + +#ifndef __PIPELINE_DECODE_H__ +#define __PIPELINE_DECODE_H__ + +#include "sample_defs.h" + +#if D3D_SURFACES_SUPPORT +#pragma warning(disable : 4201) +#include +#include +#endif + +#include +#include "hw_device.h" +#include "camera_render.h" +#include + +#include "sample_camera_utils.h" + +#include "sample_defs.h" +#include "sample_utils.h" +#include "base_allocator.h" + +#include "mfxplugin.h" +#include "mfxplugin++.h" +#include "mfxvideo.h" +#include "mfxvideo++.h" + +#include "vm/thread_defs.h" + +// The only supported padding size is 8 +#define CAMERA_PADDING_SIZE 8 + +#define align(value) ((0x10) * ( (value) / (0x10) + (((value) % (0x10)) ? 1 : 0))) +#define align_32(value) ((0x20) * ( (value) / (0x20) + (((value) % (0x20)) ? 1 : 0))) +#define align_64(value) ((0x40) * ( (value) / (0x40) + (((value) % (0x40)) ? 1 : 0))) + +class CCameraPipeline +{ +public: + + CCameraPipeline(); + virtual ~CCameraPipeline(); + + virtual mfxStatus Init(sInputParams *pParams); + virtual mfxStatus Run(); + virtual void Close(); + virtual mfxStatus Reset(sInputParams *pParams); + + void SetExtBuffersFlag() { m_bIsExtBuffers = true; } + void SetRenderingFlag() { m_bIsRender = true; } + void SetOutputfileFlag(bool b) { m_bOutput = b; } + virtual void PrintInfo(); + + mfxU32 GetNumberProcessedFrames() {return m_nFrameIndex;} + + mfxStatus PrepareInputSurfaces(); + +protected: + + bool isBayerFormat(mfxU32 type); + + mfxCam3DLutEntry* m_3dlut_17; + mfxCam3DLutEntry* m_3dlut_33; + mfxCam3DLutEntry* m_3dlut_65; + CBmpWriter* m_pBmpWriter; + CVideoReader* m_pFileReader; + CRawVideoWriter* m_pRawFileWriter; + mfxU32 m_nInputFileIndex; + mfxU32 m_nFrameIndex; // index of processed frame + mfxU32 m_nFrameLimit; // limit number of frames to proceed + + MFXVideoSession m_mfxSession; + MFXVideoVPP* m_pmfxVPP; + mfxVideoParam m_mfxVideoParams; + std::auto_ptr m_pUserModule; + std::auto_ptr m_pCamera_plugin; + mfxPluginUID m_UID_Camera; + + mfxExtCamVignetteCorrection m_Vignette; + mfxExtCamBayerDenoise m_Denoise; + mfxExtCamHotPixelRemoval m_HP; + mfxExtCamGammaCorrection m_GammaCorrection; + mfxExtCamFwdGamma m_3DLUT_GammaCorrection; +#if MFX_VERSION >= 1023 + mfxExtCamTotalColorControl m_TotalColorControl; + mfxExtCamCscYuvRgb m_RGBToYUV; +#endif + mfxExtCamBlackLevelCorrection m_BlackLevelCorrection; + mfxExtCamWhiteBalance m_WhiteBalance; + mfxExtCamColorCorrection3x3 m_CCM; + mfxExtCam3DLut m_3DLUT; + mfxExtCamLensGeomDistCorrection m_Lens; + mfxExtCamPipeControl m_PipeControl; + mfxExtCamPadding m_Padding; + + std::vector m_ExtBuffers; + + MFXFrameAllocator* m_pMFXd3dAllocator; + mfxAllocatorParams* m_pmfxd3dAllocatorParams; + MFXFrameAllocator* m_pMFXsysAllocator; + mfxAllocatorParams* m_pmfxsysAllocatorParams; + + MFXFrameAllocator* m_pMFXAllocatorIn; + mfxAllocatorParams* m_pmfxAllocatorParamsIn; + MFXFrameAllocator* m_pMFXAllocatorOut; + mfxAllocatorParams* m_pmfxAllocatorParamsOut; + MemType m_memTypeOut; // memory type of output surfaces to use + MemType m_memTypeIn; // memory type of input surfaces to use + AccelType m_accelType; + bool m_bExternalAllocIn; // use memory allocator as external for Media SDK + bool m_bExternalAllocOut; // use memory allocator as external for Media SDK + mfxFrameSurface1* m_pmfxSurfacesIn; // frames array + mfxFrameSurface1* m_pmfxSurfacesOut; // frames array + mfxFrameAllocResponse m_mfxResponseIn; // memory allocation response + mfxFrameAllocResponse m_mfxResponseOut; // memory allocation response + + mfxFrameAllocResponse m_mfxResponseAux; // memory allocation response + mfxFrameSurface1* m_pmfxSurfacesAux; // frames array + + mfxFrameAllocRequest m_lastAllocRequest[2]; + + bool m_bIsExtBuffers; // indicates if external buffers were allocated + bool m_bIsRender; // enables rendering mode + bool m_bOutput; // enables/disables output file + bool m_bEnd; + bool b_3DLUT_Gamma; + + mfxI32 m_alphaValue; + mfxU32 m_BayerType; + + mfxU32 m_numberOfResets; + mfxU32 m_resetCnt; + mfxU32 m_resetInterval; + + CHWDevice *m_hwdev; +#if D3D_SURFACES_SUPPORT + CCameraD3DRender m_d3dRender; +#endif + virtual mfxStatus InitMfxParams(sInputParams *pParams); + + // function for allocating a specific external buffer + template + mfxStatus AllocateExtBuffer(); + virtual void DeleteExtBuffers(); + + virtual void AttachExtParam(); + + virtual mfxStatus AllocAndInitDenoise(sInputParams *pParams); + virtual mfxStatus AllocAndInitVignetteCorrection(sInputParams *pParams); + virtual mfxStatus AllocAndInitCamGammaCorrection(sInputParams *pParams); + virtual mfxStatus AllocAndInitCamWhiteBalance(sInputParams *pParams); + virtual mfxStatus AllocAndInitCamCCM(sInputParams *pParams); + virtual mfxStatus AllocAndInitCamLens(sInputParams *pParams); +#if MFX_VERSION >= 1023 + virtual mfxStatus AllocAndInitCamTotalColorControl(sInputParams *pParams); + virtual mfxStatus AllocAndInitCamRGBtoYUV(sInputParams *pParams); +#endif + virtual mfxStatus AllocAndInitCamBlackLevelCorrection(sInputParams *pParams); + virtual mfxStatus AllocAndInitCamHotPixelRemoval(sInputParams *pParams); + virtual void FreeCamGammaCorrection(); + virtual void FreeVignetteCorrection(); + + virtual mfxStatus CreateAllocator(); + virtual mfxStatus CreateHWDevice(); + virtual mfxStatus AllocFrames(); + virtual mfxStatus ReallocFrames(mfxVideoParam *par); + virtual void DeleteFrames(); + virtual void DeleteAllocator(); + + //virtual mfxStatus PrepareInSurface(mfxFrameSurface1 **ppSurface, mfxU32 indx = 0); +}; + +#endif // __PIPELINE_DECODE_H__ \ No newline at end of file diff --git a/samples/sample_camera/include/sample_camera_utils.h b/samples/sample_camera/include/sample_camera_utils.h new file mode 100644 index 000000000..4da51ff9c --- /dev/null +++ b/samples/sample_camera/include/sample_camera_utils.h @@ -0,0 +1,588 @@ +/******************************************************************************\ +Copyright (c) 2005-2018, Intel Corporation +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +This sample was distributed or derived from the Intel's Media Samples package. +The original version of this sample may be obtained from https://software.intel.com/en-us/intel-media-server-studio +or https://software.intel.com/en-us/media-client-solutions-support. +\**********************************************************************************/ + +#ifndef __SAMPLE_VPP_UTILS_H +#define __SAMPLE_VPP_UTILS_H + +/* ************************************************************************* */ + +#include "sample_defs.h" + +#if D3D_SURFACES_SUPPORT +#pragma warning(disable : 4201) +#include +#include +#include +#endif + +#if ! defined(_WIN32) && ! defined(_WIN64) +struct BITMAPFILEHEADER +{ + unsigned short bfType; + unsigned int bfSize; + unsigned short bfReserved1; + unsigned short bfReserved2; + unsigned int bfOffBits; +}; +struct BITMAPINFOHEADER +{ + unsigned int biSize; + unsigned int biWidth; + unsigned int biHeight; + unsigned short biPlanes; + unsigned short biBitCount; + unsigned int biCompression; + unsigned int biSizeImage; + unsigned int biXPelsPerMeter; + unsigned int biYPelsPerMeter; + unsigned int biClrUsed; + unsigned int biClrImportant; +}; + +#define BI_RGB 0L +#endif + +#include + +#include +#include +#include +#include +#include +#include + +#include "mfxvideo.h" +#include "mfxvideo++.h" +#include "sample_utils.h" +#include "base_allocator.h" +#include "mfxcamera.h" + +//#define CONVERT_TO_LSB +#define SHIFT_OUT_TO_LSB + +//#define CAM_SAMPLE_PERF + +/* ************************************************************************* */ +#define camera_printf +#define camera_msdk_printf +#define camera_fflush + +#define VPP_IN (0) +#define VPP_OUT (1) + +#define MFX_MAX_32U ( 0xFFFFFFFF ) + +#define NOT_INIT_VALUE (0xFFF7) + + +template<>struct mfx_ext_buffer_id{ + enum {id = MFX_EXTBUF_CAM_GAMMA_CORRECTION}; +}; + +template<>struct mfx_ext_buffer_id{ + enum {id = MFX_EXTBUF_CAM_BLACK_LEVEL_CORRECTION}; +}; + +template<>struct mfx_ext_buffer_id{ + enum {id = MFX_EXTBUF_CAM_WHITE_BALANCE}; +}; + +template<>struct mfx_ext_buffer_id{ + enum {id = MFX_EXTBUF_CAM_COLOR_CORRECTION_3X3}; +}; + +template<>struct mfx_ext_buffer_id{ + enum {id = MFX_EXTBUF_CAM_VIGNETTE_CORRECTION}; +}; +// we introduce new macros without error message (returned status only) +// it allows to remove final error message due to EOF +#define IOSTREAM_CHECK_NOT_EQUAL(P, X, ERR) {if ((X) != (P)) {return ERR;}} +#define IOSTREAM_CHECK_NOT_EQUAL_SAFE(P, X, ERR,CLEANUP) {if ((X) != (P)) {CLEANUP;return ERR;}} + +#define CAM_SAMPLE_ASYNC_DEPTH 4 +#define CAM_SAMPLE_NUM_BMP_FILES 20 + +enum AccelType { + D3D9 = 0x01, + D3D11 = 0x02 +}; + +enum MemoryType { + SYSTEM = 0x01, + VIDEO = 0x02 +}; + +enum MemType { + SYSTEM_MEMORY = 0x00, + D3D9_MEMORY = 0x01, + D3D11_MEMORY = 0x02, + UNDEFINED_MEMORY = 0x1000 +}; + + +typedef struct _ownFrameInfo +{ + mfxU32 nWidth; + mfxU32 nHeight; + // ROI + mfxU32 CropX; + mfxU32 CropY; + mfxU32 CropW; + mfxU32 CropH; + mfxU32 FourCC; + mfxF64 dFrameRate; + +} sOwnFrameInfo; + + +typedef struct _resetParams +{ + msdk_char strSrcFile[MSDK_MAX_FILENAME_LEN]; + msdk_char strDstFile[MSDK_MAX_FILENAME_LEN]; + mfxU32 width; + mfxU32 height; + mfxU16 cropW; + mfxU16 cropH; + mfxU16 cropX; + mfxU16 cropY; + mfxU32 inputType; + + bool bDenoise; + mfxU16 denoiseThreshold; + + bool bHP; + mfxU16 hp_diff; + mfxU16 hp_num; + + bool bBlackLevel; + mfxU16 black_level_B; + mfxU16 black_level_G0; + mfxU16 black_level_G1; + mfxU16 black_level_R; + + bool bWhiteBalance; + mfxF64 white_balance_B; + mfxF64 white_balance_G0; + mfxF64 white_balance_G1; + mfxF64 white_balance_R; + + bool bTCC; + mfxU8 tcc_red; + mfxU8 tcc_green; + mfxU8 tcc_blue; + mfxU8 tcc_cyan; + mfxU8 tcc_magenta; + mfxU8 tcc_yellow; + + bool bCCM; + mfxF64 CCM[3][3]; + + bool bVignette; + msdk_char strVignetteMaskFile[MSDK_MAX_FILENAME_LEN]; + _resetParams() : + inputType(0) + , denoiseThreshold(0) + , hp_diff(0) + , hp_num(0) + , black_level_B(0) + , black_level_G0(0) + , black_level_G1(0) + , black_level_R(0) + , white_balance_B(0) + , white_balance_G0(0) + , white_balance_G1(0) + , white_balance_R(0) + { + bHP = false; + bBlackLevel = false; + bTCC = false; + bWhiteBalance = false; + bCCM = false; + bDenoise = false; + bVignette = false; + width = height = cropX = cropY = cropW = cropH = 0; + tcc_red = 255; + tcc_green = 255; + tcc_blue = 255; + tcc_cyan = 255; + tcc_magenta = 255; + tcc_yellow = 255; + MSDK_ZERO_MEMORY(strSrcFile); + MSDK_ZERO_MEMORY(strDstFile); + MSDK_ZERO_MEMORY(strVignetteMaskFile); + MSDK_ZERO_MEMORY(CCM); + } +} sResetParams; + +struct sInputParams +{ + + sOwnFrameInfo frameInfo[2]; // [0] - in, [1] - out + + mfxU32 inputType; + bool bOutput; // if renderer is enabled, possibly no need in output file + mfxI32 maxNumBmpFiles; + AccelType accelType; + MemoryType memTypeIn; + MemoryType memTypeOut; + mfxU32 CameraPluginVersion; + bool bRendering; // true if d3d rendering is in use + mfxI32 asyncDepth; + mfxU32 bitDepth; + mfxI32 alphaValue; + + mfxU32 nWallCell; + mfxU32 nWallW;//number of windows located in each row + mfxU32 nWallH;//number of windows located in each column + mfxU32 nWallMonitor;//monitor id, 0,1,.. etc + mfxU32 nWallFPS;//rendering limited by certain fps + bool bWallNoTitle;//whether to show title for each window with fps value + mfxU32 nWallTimeout; //timeout for -wall option + + bool bHP; + mfxU16 hp_diff; + mfxU16 hp_num; + + bool bGamma; + bool b3DLUTGamma; + bool bRGBToYUV; + bool bExternalGammaLUT; + mfxU16 gamma_point[64]; + mfxU16 gamma_corrected[64]; + mfxF64 gamma_value; + mfxU16 gamma_mode; + + bool bBlackLevel; + mfxU16 black_level_B; + mfxU16 black_level_G0; + mfxU16 black_level_G1; + mfxU16 black_level_R; + + bool bTCC; + mfxU8 tcc_red; + mfxU8 tcc_green; + mfxU8 tcc_blue; + mfxU8 tcc_cyan; + mfxU8 tcc_magenta; + mfxU8 tcc_yellow; + bool bWhiteBalance; + mfxF64 white_balance_B; + mfxF64 white_balance_G0; + mfxF64 white_balance_G1; + mfxF64 white_balance_R; + + bool bLens; + mfxF32 lens_aR; + mfxF32 lens_bR; + mfxF32 lens_cR; + mfxF32 lens_dR; + mfxF32 lens_aG; + mfxF32 lens_bG; + mfxF32 lens_cG; + mfxF32 lens_dG; + mfxF32 lens_aB; + mfxF32 lens_bB; + mfxF32 lens_cB; + mfxF32 lens_dB; + + bool offset; + mfxF32 pre[3]; + mfxF32 post[3]; + + bool bCCM; + mfxF64 CCM[3][3]; + + bool bVignette; + msdk_char strVignetteMaskFile[MSDK_MAX_FILENAME_LEN]; + bool bBayerDenoise; + mfxU16 denoiseThreshold; + + mfxU32 nFramesToProceed; + + msdk_char strSrcFile[MSDK_MAX_FILENAME_LEN]; + msdk_char strDstFile[MSDK_MAX_FILENAME_LEN]; + msdk_char strPluginPath[MSDK_MAX_FILENAME_LEN]; + + std::vector resetParams; + mfxU32 resetInterval; + bool bPerf_opt; + bool bDoPadding; + + bool b3DLUT; + + sInputParams() + { + bOutput=0; + CameraPluginVersion=0; + bRendering=0; + + nWallCell=0; + nWallW=0; + nWallH=0; + nWallMonitor=0; + nWallFPS=0; + bWallNoTitle=0; + nWallTimeout=0; + + hp_diff=0; + hp_num=0; + + MSDK_ZERO_MEMORY(gamma_point); + MSDK_ZERO_MEMORY(gamma_corrected);; + gamma_value=0; + gamma_mode=0; + + black_level_B=0; + black_level_G0=0; + black_level_G1=0; + black_level_R=0; + + white_balance_B=0; + white_balance_G0=0; + white_balance_G1=0; + white_balance_R=0; + + lens_aR=0; + lens_bR=0; + lens_cR=0; + lens_dR=0; + lens_aG=0; + lens_bG=0; + lens_cG=0; + lens_dG=0; + lens_aB=0; + lens_bB=0; + lens_cB=0; + lens_dB=0; + + MSDK_ZERO_MEMORY(CCM); + + MSDK_ZERO_MEMORY(strVignetteMaskFile); + denoiseThreshold=0; + + nFramesToProceed=0; + + MSDK_ZERO_MEMORY(strSrcFile); + MSDK_ZERO_MEMORY(strDstFile); + MSDK_ZERO_MEMORY(strPluginPath); + bPerf_opt = false; + bDoPadding = false; + + CameraPluginVersion = 1; + inputType = MFX_CAM_BAYER_RGGB; + MSDK_ZERO_MEMORY(frameInfo); + + frameInfo[VPP_IN].nWidth = 4096; + frameInfo[VPP_IN].nHeight = 2160; + frameInfo[VPP_IN].CropH = frameInfo[VPP_IN].CropW = frameInfo[VPP_OUT].CropH = frameInfo[VPP_OUT].CropW = NOT_INIT_VALUE; + frameInfo[VPP_IN].dFrameRate = frameInfo[VPP_OUT].dFrameRate = 24.0; + frameInfo[VPP_OUT].FourCC = MFX_FOURCC_RGB4; + asyncDepth = CAM_SAMPLE_ASYNC_DEPTH; + maxNumBmpFiles = CAM_SAMPLE_NUM_BMP_FILES; + memTypeIn = SYSTEM; + memTypeOut = VIDEO; + accelType = D3D9; + bitDepth = 10; + bGamma = true; + b3DLUTGamma = false; + bHP = false; + bBlackLevel = false; + bWhiteBalance = false; + bCCM = false; + bBayerDenoise = false; + bVignette = false; + bLens = false; + b3DLUT = false; + bTCC = false; + tcc_red = 255; + tcc_green = 255; + tcc_blue = 255; + tcc_cyan = 255; + tcc_magenta = 255; + tcc_yellow = 255; + bRGBToYUV = false; + offset = false; + for (int i = 0; i < 3; i++) + pre[i] = 0; + post[0] = 2048; + post[1] = 16384; + post[2] = 16384; + alphaValue = -1; + resetInterval = 7; + bExternalGammaLUT = false; + } +}; + +struct sMemoryAllocator +{ + MFXFrameAllocator* pMfxAllocator; + mfxAllocatorParams* pAllocatorParams; + mfxFrameSurface1** pSurfaces; + mfxFrameAllocResponse* response; +}; + +class CVideoReader +{ +public: + virtual void Close() = 0; + virtual mfxStatus Init(sInputParams *pParams) = 0; + virtual mfxStatus LoadNextFrame(mfxFrameData* pData, mfxFrameInfo* pInfo, mfxU32 type) = 0; + virtual void SetStartFileNumber(mfxI32) = 0; + virtual ~CVideoReader(){}; +}; + +class CRawVideoReader: public CVideoReader +{ +public : + CRawVideoReader(); + virtual ~CRawVideoReader(); + + virtual void Close(); + virtual mfxStatus Init(sInputParams *pParams); + virtual mfxStatus LoadNextFrame(mfxFrameData* pData, mfxFrameInfo* pInfo, mfxU32 bayerType); + virtual void SetStartFileNumber(mfxI32); + +protected: + mfxStatus LoadNextFrameSingle (mfxFrameData* pData, mfxFrameInfo* pInfo, mfxU32 bayerType); + mfxStatus LoadNextFrameSequential(mfxFrameData* pData, mfxFrameInfo* pInfo, mfxU32 bayerType); + + FILE* m_fSrc; + msdk_char m_FileNameBase[MSDK_MAX_FILENAME_LEN]; + mfxU32 m_FileNum; +#ifdef CONVERT_TO_LSB + mfxU16 *m_pPaddingBuffer; + mfxU32 m_paddingBufSize; +#endif + bool m_DoPadding; + bool m_bSingleFileMode; + mfxU32 m_Width; + mfxU32 m_Height; +}; + +class CARGB16VideoReader: public CVideoReader +{ +public : + CARGB16VideoReader(): m_fSrc(0), m_bSingleFileMode(false),m_Height(0),m_Width(0),m_FileNum(0) {}; + virtual ~CARGB16VideoReader(); + + void Close(); + mfxStatus Init(sInputParams *pParams); + mfxStatus LoadNextFrame(mfxFrameData* pData, mfxFrameInfo* pInfo, mfxU32 type); + void SetStartFileNumber(mfxI32 fileNum) + { + m_FileNum = fileNum; + } + +protected: + mfxStatus LoadNextFrameSingle (mfxFrameData* pData, mfxFrameInfo* pInfo, mfxU32 type); + mfxStatus LoadNextFrameSequential(mfxFrameData* pData, mfxFrameInfo* pInfo, mfxU32 type); + + FILE* m_fSrc; + msdk_char m_FileNameBase[MSDK_MAX_FILENAME_LEN]; + mfxU32 m_FileNum; + bool m_bSingleFileMode; + mfxU32 m_Width; + mfxU32 m_Height; +}; + + +class CBufferedVideoReader : public CVideoReader +{ +public: + CBufferedVideoReader() : m_fSrc(0), m_Height(0), m_Width(0), m_FileNum(0), nCurrentFrame(0), nFramesToProceed(0), m_DoPadding(false) + { + MSDK_ZERO_MEMORY(m_FileNameBase); + }; + + virtual ~CBufferedVideoReader(); + void Close(); + mfxStatus Init(sInputParams *pParams); + mfxStatus LoadNextFrame(mfxFrameData* pData, mfxFrameInfo* pInfo, mfxU32 type); + void SetStartFileNumber(mfxI32 fileNum) + { + m_FileNum = fileNum; + } + +protected: + FILE* m_fSrc; + msdk_char m_FileNameBase[MSDK_MAX_FILENAME_LEN]; + mfxU32 m_FileNum; + bool m_DoPadding; + mfxU32 m_Width; + mfxU32 m_Height; + mfxU32 nFramesToProceed; + mfxU32 nCurrentFrame; + + std::vector buffer; +}; + +class CBmpWriter +{ +public : + + CBmpWriter(); + //~CBmpWriter() {}; + + mfxStatus Init(const msdk_char *strFileNameBase, mfxU32 width, mfxU32 height, mfxI32 maxNumFilesToCreate = -1); + mfxStatus WriteFrame(mfxFrameData* pData, const msdk_char *fileExt, mfxFrameInfo* pInfo); + +protected: + msdk_char m_FileNameBase[MSDK_MAX_FILENAME_LEN]; + mfxI32 m_FileNum; + mfxI32 m_maxNumFilesToCreate; + + BITMAPFILEHEADER m_bfh; + BITMAPINFOHEADER m_bih; + +}; + + +class CRawVideoWriter +{ +public : + + CRawVideoWriter(); + //~CRawVideoWriter(); + + mfxStatus Init(sInputParams *pParams); + mfxStatus WriteFrameARGB16(mfxFrameData* pData, const msdk_char *fileExt, mfxFrameInfo* pInfo); + mfxStatus WriteFrameNV12(mfxFrameData* pData, const msdk_char *fileExt, mfxFrameInfo* pInfo); + +protected: + msdk_char m_FileNameBase[MSDK_MAX_FILENAME_LEN]; + mfxI32 m_FileNum; + mfxI32 m_maxNumFilesToCreate; + //mfxU16 *m_pShiftBuffer; + //mfxU32 m_shiftBufSize; +}; + + +/* ******************************************************************* */ +/* service functions */ +/* ******************************************************************* */ + +mfxStatus GetFreeSurface(mfxFrameSurface1* pSurfacesPool, mfxU16 nPoolSize, mfxFrameSurface1** ppSurface); + +mfxStatus InitSurfaces(sMemoryAllocator* pAllocator, mfxFrameAllocRequest* pRequest, mfxFrameInfo* pInfo, bool bUsedAsExternalAllocator); + +void ReleaseSurface(mfxFrameSurface1* pSurface); + +msdk_char* FourCC2Str( mfxU32 FourCC ); + +#endif /* __SAMPLE_VPP_UTILS_H */ diff --git a/samples/sample_camera/sample_camera.sln b/samples/sample_camera/sample_camera.sln new file mode 100644 index 000000000..49abf64e9 --- /dev/null +++ b/samples/sample_camera/sample_camera.sln @@ -0,0 +1,38 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27703.2000 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sample_camera", "sample_camera.vcxproj", "{A812C44D-0255-4796-9042-BCAE99963E7D}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sample_common", "..\sample_common\sample_common.vcxproj", "{5FADB243-53C3-4776-A20F-8BD65C10CF41}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Debug|x64 = Debug|x64 + Release|Win32 = Release|Win32 + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {A812C44D-0255-4796-9042-BCAE99963E7D}.Debug|Win32.ActiveCfg = Debug|Win32 + {A812C44D-0255-4796-9042-BCAE99963E7D}.Debug|Win32.Build.0 = Debug|Win32 + {A812C44D-0255-4796-9042-BCAE99963E7D}.Debug|x64.ActiveCfg = Debug|x64 + {A812C44D-0255-4796-9042-BCAE99963E7D}.Debug|x64.Build.0 = Debug|x64 + {A812C44D-0255-4796-9042-BCAE99963E7D}.Release|Win32.ActiveCfg = Release|Win32 + {A812C44D-0255-4796-9042-BCAE99963E7D}.Release|Win32.Build.0 = Release|Win32 + {A812C44D-0255-4796-9042-BCAE99963E7D}.Release|x64.ActiveCfg = Release|x64 + {A812C44D-0255-4796-9042-BCAE99963E7D}.Release|x64.Build.0 = Release|x64 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Debug|Win32.ActiveCfg = Debug|Win32 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Debug|Win32.Build.0 = Debug|Win32 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Debug|x64.ActiveCfg = Debug|x64 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Debug|x64.Build.0 = Debug|x64 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Release|Win32.ActiveCfg = Release|Win32 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Release|Win32.Build.0 = Release|Win32 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Release|x64.ActiveCfg = Release|x64 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Release|x64.Build.0 = Release|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/samples/sample_camera/sample_camera.vcxproj b/samples/sample_camera/sample_camera.vcxproj new file mode 100644 index 000000000..2c1b9f24c --- /dev/null +++ b/samples/sample_camera/sample_camera.vcxproj @@ -0,0 +1,319 @@ + + + + + Debug_WithDebugAPI + Win32 + + + Debug_WithDebugAPI + x64 + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {A812C44D-0255-4796-9042-BCAE99963E7D} + sample_camera + Win32Proj + + + + Application + v141 + false + Unicode + true + + + Application + v141 + false + Unicode + + + Application + v141 + false + Unicode + + + Application + v141 + false + Unicode + + + Application + v141 + false + Unicode + + + Application + v141 + false + Unicode + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + <_ProjectFileVersion>11.0.61030.0 + + + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + true + sample_camera + + + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + true + sample_camera + + + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + true + sample_camera + + + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + true + sample_camera + + + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + false + sample_camera + + + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + false + sample_camera + + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\sample_common\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;%(AdditionalIncludeDirectories) + _WIN32;WIN32;_DEBUG;_CONSOLE;_ITERATOR_DEBUG_LEVEL=0;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebug + + Level4 + false + EditAndContinue + + + libmfx_vs2015.lib;dxva2.lib;d3d9.lib;dwmapi.lib;comsuppw.lib;d3d11.lib;dxgi.lib;%(AdditionalDependencies) + $(ProjectDir)..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(PlatformName);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;LIBCMT.lib;%(IgnoreSpecificDefaultLibraries) + dwmapi.dll;%(DelayLoadDLLs) + true + Console + MachineX86 + /SAFESEH %(AdditionalOptions) + + + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\sample_common\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;%(AdditionalIncludeDirectories) + _WIN32;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebug + + + Level4 + false + EditAndContinue + + + libmfx_vs2015_d.lib;dxva2.lib;d3d9.lib;dwmapi.lib;comsuppw.lib;d3d11.lib;dxgi.lib;%(AdditionalDependencies) + $(ProjectDir)..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(PlatformName);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;LIBCMT.lib;%(IgnoreSpecificDefaultLibraries) + dwmapi.dll;%(DelayLoadDLLs) + true + Console + MachineX86 + /SAFESEH %(AdditionalOptions) + + + + + X64 + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\sample_common\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;%(AdditionalIncludeDirectories) + _WIN64;WIN64;_DEBUG;_CONSOLE;_ITERATOR_DEBUG_LEVEL=0;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebug + + Level4 + true + ProgramDatabase + + + libmfx_vs2015.lib;dxva2.lib;d3d9.lib;dwmapi.lib;comsuppw.lib;d3d11.lib;dxgi.lib;%(AdditionalDependencies) + $(ProjectDir)..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(PlatformName);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;LIBCMT.lib;%(IgnoreSpecificDefaultLibraries) + dwmapi.dll;%(DelayLoadDLLs) + true + Console + MachineX64 + + + + + X64 + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\sample_common\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;%(AdditionalIncludeDirectories) + _WIN64;WIN64;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebug + + + Level4 + true + ProgramDatabase + + + libmfx_vs2015_d.lib;dxva2.lib;d3d9.lib;dwmapi.lib;comsuppw.lib;d3d11.lib;dxgi.lib;%(AdditionalDependencies) + $(ProjectDir)..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(PlatformName);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;LIBCMT.lib;%(IgnoreSpecificDefaultLibraries) + dwmapi.dll;%(DelayLoadDLLs) + true + Console + MachineX64 + + + + + $(ProjectDir)\include;$(ProjectDir)\..\sample_common\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;%(AdditionalIncludeDirectories) + _WIN32;WIN32;NDEBUG;_CONSOLE;SAVE_RECON;%(PreprocessorDefinitions) + Default + MultiThreaded + true + + Level3 + true + + + + libmfx_vs2015.lib;dxva2.lib;d3d9.lib;dwmapi.lib;comsuppw.lib;d3d11.lib;dxgi.lib;%(AdditionalDependencies) + $(ProjectDir)..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(PlatformName);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;%(IgnoreSpecificDefaultLibraries) + dwmapi.dll;%(DelayLoadDLLs) + false + true + Console + true + true + MachineX86 + /SAFESEH %(AdditionalOptions) + + + + + X64 + + + $(ProjectDir)\include;$(ProjectDir)\..\sample_common\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;%(AdditionalIncludeDirectories) + WIN64;NDEBUG;_CONSOLE;SAVE_RECON;%(PreprocessorDefinitions) + Default + MultiThreaded + true + + Level3 + true + ProgramDatabase + + + libmfx_vs2015.lib;dxva2.lib;d3d9.lib;dwmapi.lib;comsuppw.lib;d3d11.lib;dxgi.lib;%(AdditionalDependencies) + $(ProjectDir)..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(PlatformName);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;%(IgnoreSpecificDefaultLibraries) + dwmapi.dll;%(DelayLoadDLLs) + true + true + Console + true + true + MachineX64 + + + + + {5FADB243-53C3-4776-A20F-8BD65C10CF41} + false + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/samples/sample_camera/src/camera_render.cpp b/samples/sample_camera/src/camera_render.cpp new file mode 100644 index 000000000..9d89c41e2 --- /dev/null +++ b/samples/sample_camera/src/camera_render.cpp @@ -0,0 +1,354 @@ +/******************************************************************************\ +Copyright (c) 2005-2018, Intel Corporation +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +This sample was distributed or derived from the Intel's Media Samples package. +The original version of this sample may be obtained from https://software.intel.com/en-us/intel-media-server-studio +or https://software.intel.com/en-us/media-client-solutions-support. +\**********************************************************************************/ + +#if defined(_WIN32) || defined(_WIN64) + +#include +#include +#include + +#include "sample_defs.h" +#include "camera_render.h" +#pragma warning(disable : 4100) + +LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) +{ +#ifdef WIN64 + CCameraD3DRender* pRender = (CCameraD3DRender*)GetWindowLongPtr(hWnd, GWLP_USERDATA); +#else + CCameraD3DRender* pRender = (CCameraD3DRender*)LongToPtr(GetWindowLongPtr(hWnd, GWL_USERDATA)); +#endif + if (pRender) + { + switch(message) + { + HANDLE_MSG(hWnd, WM_DESTROY, pRender->OnDestroy); + HANDLE_MSG(hWnd, WM_KEYUP, pRender->OnKey); + } + } + + return DefWindowProc(hWnd, message, wParam, lParam); +} + +CCameraD3DRender::CCameraD3DRender() +{ + m_bDwmEnabled = false; + QueryPerformanceFrequency(&m_Freq); + MSDK_ZERO_MEMORY(m_LastInputTime); + m_nFrames = 0; + m_nMonitorCurrent = 0; + + m_hwdev = NULL; + MSDK_ZERO_MEMORY(m_sWindowParams); + m_Hwnd = 0; + MSDK_ZERO_MEMORY(m_rect); + MSDK_ZERO_MEMORY(m_RectWindow); + m_style = 0; + MSDK_ZERO_MEMORY(m_RectWindow); +} + +BOOL CALLBACK CCameraD3DRender::MonitorEnumProc(HMONITOR /*hMonitor*/, + HDC /*hdcMonitor*/, + LPRECT lprcMonitor, + LPARAM dwData) +{ + CCameraD3DRender * pRender = reinterpret_cast(dwData); + RECT r = {0}; + if (NULL == lprcMonitor) + lprcMonitor = &r; + + if (pRender->m_nMonitorCurrent++ == pRender->m_sWindowParams.nAdapter) + { + pRender->m_RectWindow = *lprcMonitor; + } + return TRUE; +} + +CCameraD3DRender::~CCameraD3DRender() +{ + Close(); + + //DestroyTimer(); +} + +void CCameraD3DRender::Close() +{ + if (m_Hwnd) + DestroyWindow(m_Hwnd); + //DestroyTimer(); +} + +mfxStatus CCameraD3DRender::Init(sWindowParams pWParams) +{ + // window part + m_sWindowParams = pWParams; + + WNDCLASS window; + MSDK_ZERO_MEMORY(window); + + window.lpfnWndProc= (WNDPROC)WindowProc; + window.hInstance= GetModuleHandle(NULL);; + window.hCursor= LoadCursor(NULL, IDC_ARROW); + window.lpszClassName= m_sWindowParams.lpClassName; + + UnregisterClass(m_sWindowParams.lpClassName, m_sWindowParams.hInstance); + + if (!RegisterClass(&window)) + return MFX_ERR_UNKNOWN; + + ::RECT displayRegion = {CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT}; + + //right and bottom fields consist of width and height values of displayed reqion + if (0 != m_sWindowParams.nx ) + { + EnumDisplayMonitors(NULL, NULL, &CCameraD3DRender::MonitorEnumProc, (LPARAM)this); + + displayRegion.right = (m_RectWindow.right - m_RectWindow.left) / m_sWindowParams.nx; + displayRegion.bottom = (m_RectWindow.bottom - m_RectWindow.top) / m_sWindowParams.ny; + displayRegion.left = displayRegion.right * (m_sWindowParams.ncell % m_sWindowParams.nx) + m_RectWindow.left; + displayRegion.top = displayRegion.bottom * (m_sWindowParams.ncell / m_sWindowParams.nx) + m_RectWindow.top; + } + else + { + m_sWindowParams.nMaxFPS = 10000;//hypotetical maximum + } + + //no title window style if required + DWORD dwStyle = NULL == m_sWindowParams.lpWindowName ? WS_POPUP|WS_BORDER|WS_MAXIMIZE : WS_OVERLAPPEDWINDOW; + + m_Hwnd = CreateWindowEx(NULL, + m_sWindowParams.lpClassName, + m_sWindowParams.lpWindowName, + !m_sWindowParams.bFullScreen ? dwStyle : (WS_POPUP), + !m_sWindowParams.bFullScreen ? displayRegion.left : 0, + !m_sWindowParams.bFullScreen ? displayRegion.top : 0, + !m_sWindowParams.bFullScreen ? displayRegion.right : GetSystemMetrics(SM_CXSCREEN), + !m_sWindowParams.bFullScreen ? displayRegion.bottom : GetSystemMetrics(SM_CYSCREEN), + m_sWindowParams.hWndParent, + m_sWindowParams.hMenu, + m_sWindowParams.hInstance, + m_sWindowParams.lpParam); + + if (!m_Hwnd) + return MFX_ERR_UNKNOWN; + + ShowWindow(m_Hwnd, SW_SHOWDEFAULT); + UpdateWindow(m_Hwnd); + +#ifdef WIN64 + SetWindowLongPtr(m_Hwnd, GWLP_USERDATA, (LONG_PTR)this); +#else + SetWindowLong(m_Hwnd, GWL_USERDATA, PtrToLong(this)); +#endif + return MFX_ERR_NONE; +} + + +mfxStatus CCameraD3DRender::RenderFrame(mfxFrameSurface1 *pSurface, mfxFrameAllocator *pmfxAlloc) +{ + MSG msg; + MSDK_ZERO_MEMORY(msg); + while (msg.message != WM_QUIT && PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) + { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + + RECT rect; + GetClientRect(m_Hwnd, &rect); + if (IsRectEmpty(&rect)) + return MFX_ERR_UNKNOWN; + + //EnableDwmQueuing(); + + mfxStatus sts = m_hwdev->RenderFrame(pSurface, pmfxAlloc); + MSDK_CHECK_STATUS(sts, "m_hwdev->RenderFrame failed"); + + if (NULL != m_sWindowParams.lpWindowName) + { + double dfps = 0.; + + if (0 == m_LastInputTime.QuadPart) + { + QueryPerformanceCounter(&m_LastInputTime); + } + else + { + LARGE_INTEGER timeEnd; + QueryPerformanceCounter(&timeEnd); + dfps = ++m_nFrames * (double)m_Freq.QuadPart / ((double)timeEnd.QuadPart - (double)m_LastInputTime.QuadPart); + } + + TCHAR str[40]; + _stprintf_s(str, 40, _T("fps=%.2lf frame %d"), dfps, m_nFrames); + SetWindowText(m_Hwnd, str); + } + + return sts; +} + +VOID CCameraD3DRender::OnDestroy(HWND /*hwnd*/) +{ + PostQuitMessage(0); +} + +VOID CCameraD3DRender::OnKey(HWND hwnd, UINT vk, BOOL fDown, int cRepeat, UINT flags) +{ + if (TRUE == fDown) + return; + + if ('1' == vk && false == m_sWindowParams.bFullScreen) + ChangeWindowSize(true); + else if (true == m_sWindowParams.bFullScreen) + ChangeWindowSize(false); +} + +void CCameraD3DRender::AdjustWindowRect(RECT *rect) +{ + int cxmax = GetSystemMetrics(SM_CXMAXIMIZED); + int cymax = GetSystemMetrics(SM_CYMAXIMIZED); + int cxmin = GetSystemMetrics(SM_CXMINTRACK); + int cymin = GetSystemMetrics(SM_CYMINTRACK); + int leftmax = cxmax - cxmin; + int topmax = cymax - cxmin; + if (rect->left < 0) + rect->left = 0; + if (rect->left > leftmax) + rect->left = leftmax; + if (rect->top < 0) + rect->top = 0; + if (rect->top > topmax) + rect->top = topmax; + + if (rect->right < rect->left + cxmin) + rect->right = rect->left + cxmin; + if (rect->right - rect->left > cxmax) + rect->right = rect->left + cxmax; + + if (rect->bottom < rect->top + cymin) + rect->bottom = rect->top + cymin; + if (rect->bottom - rect->top > cymax) + rect->bottom = rect->top + cymax; +} + +VOID CCameraD3DRender::ChangeWindowSize(bool bFullScreen) +{ + HMONITOR hMonitor = MonitorFromWindow(m_Hwnd, MONITOR_DEFAULTTONEAREST); + MONITORINFOEX mi; + mi.cbSize = sizeof(mi); + GetMonitorInfo(hMonitor, &mi); + + WINDOWINFO wndInfo; + wndInfo.cbSize = sizeof(WINDOWINFO); + GetWindowInfo(m_Hwnd, &wndInfo); + + if(!m_sWindowParams.bFullScreen) + { + m_rect = wndInfo.rcWindow; + m_style = wndInfo.dwStyle; + } + + m_sWindowParams.bFullScreen = bFullScreen; + + if(!bFullScreen) + { + AdjustWindowRectEx(&m_rect,0,0,0); + SetWindowLong(m_Hwnd, GWL_STYLE, m_style); + SetWindowPos(m_Hwnd, HWND_NOTOPMOST, + m_rect.left , m_rect.top , + abs(m_rect.right - m_rect.left), abs(m_rect.bottom - m_rect.top), + SWP_SHOWWINDOW); + } + else + { + SetWindowLong(m_Hwnd, GWL_STYLE, WS_POPUP); + SetWindowPos(m_Hwnd, HWND_NOTOPMOST,mi.rcMonitor.left , mi.rcMonitor.top, + abs(mi.rcMonitor.left - mi.rcMonitor.right), abs(mi.rcMonitor.top - mi.rcMonitor.bottom), SWP_SHOWWINDOW); + } +} + +bool CCameraD3DRender::EnableDwmQueuing() +{ + HRESULT hr; + + // DWM queuing is enabled already. + if (m_bDwmEnabled) + { + return true; + } + + // Check to see if DWM is currently enabled. + BOOL bDWM = FALSE; + + hr = DwmIsCompositionEnabled(&bDWM); + + if (FAILED(hr)) + { + _tprintf(_T("DwmIsCompositionEnabled failed with error 0x%x.\n"), (unsigned int)hr); + return false; + } + + // DWM queuing is disabled when DWM is disabled. + if (!bDWM) + { + m_bDwmEnabled = false; + return false; + } + + // Retrieve DWM refresh count of the last vsync. + DWM_TIMING_INFO dwmti = {0}; + + dwmti.cbSize = sizeof(dwmti); + + hr = DwmGetCompositionTimingInfo(NULL, &dwmti); + + if (FAILED(hr)) + { + _tprintf(_T("DwmGetCompositionTimingInfo failed with error 0x%x.\n"), (unsigned int)hr); + return false; + } + + // Enable DWM queuing from the next refresh. + DWM_PRESENT_PARAMETERS dwmpp = {0}; + + dwmpp.cbSize = sizeof(dwmpp); + dwmpp.fQueue = TRUE; + dwmpp.cRefreshStart = dwmti.cRefresh + 1; + dwmpp.cBuffer = 8; //maximum depth of DWM queue + dwmpp.fUseSourceRate = TRUE; + dwmpp.cRefreshesPerFrame = 1; + dwmpp.eSampling = DWM_SOURCE_FRAME_SAMPLING_POINT; + dwmpp.rateSource.uiDenominator = 1; + dwmpp.rateSource.uiNumerator = m_sWindowParams.nMaxFPS; + + + hr = DwmSetPresentParameters(m_Hwnd, &dwmpp); + + if (FAILED(hr)) + { + _tprintf(_T("DwmSetPresentParameters failed with error 0x%x.\n"), (unsigned int)hr); + return false; + } + + // DWM queuing is enabled. + m_bDwmEnabled = true; + + return true; +} +#endif // #if defined(_WIN32) || defined(_WIN64) diff --git a/samples/sample_camera/src/camera_sysmem_allocator.cpp b/samples/sample_camera/src/camera_sysmem_allocator.cpp new file mode 100644 index 000000000..a343d8d16 --- /dev/null +++ b/samples/sample_camera/src/camera_sysmem_allocator.cpp @@ -0,0 +1,380 @@ +/******************************************************************************\ +Copyright (c) 2005-2018, Intel Corporation +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +This sample was distributed or derived from the Intel's Media Samples package. +The original version of this sample may be obtained from https://software.intel.com/en-us/intel-media-server-studio +or https://software.intel.com/en-us/media-client-solutions-support. +\**********************************************************************************/ + +#include "camera_sysmem_allocator.h" + +#define MSDK_ALIGN32(X) (((mfxU32)((X)+31)) & (~ (mfxU32)31)) +#define ID_BUFFER MFX_MAKEFOURCC('B','U','F','F') +#define ID_FRAME MFX_MAKEFOURCC('F','R','M','E') + +#pragma warning(disable : 4100) + +CamSysMemFrameAllocator::CamSysMemFrameAllocator() +: m_pBufferAllocator(0), m_bOwnBufferAllocator(false), m_alignment(0) +{ +} + +CamSysMemFrameAllocator::~CamSysMemFrameAllocator() +{ + Close(); +} + +mfxStatus CamSysMemFrameAllocator::Init(mfxAllocatorParams *pParams) +{ + // check if any params passed from application + if (pParams) + { + CamSysMemAllocatorParams *pSysMemParams = 0; + pSysMemParams = dynamic_cast(pParams); + if (!pSysMemParams) + return MFX_ERR_NOT_INITIALIZED; + + if (pSysMemParams->pBufferAllocator) { + m_pBufferAllocator = pSysMemParams->pBufferAllocator; + m_bOwnBufferAllocator = false; + } + + if (pSysMemParams->alignment > 0) { + m_alignment = pSysMemParams->alignment; + for (mfxU32 i = 4; i < 31; i++) { // must be power of 2 + if (((mfxU32)1 << i) >= m_alignment) { + m_alignment = 1 << i; + break; + } + } + } + } + + // if buffer allocator wasn't passed from application create own + if (!m_pBufferAllocator) + { + m_pBufferAllocator = new CamSysMemBufferAllocator; + if (!m_pBufferAllocator) + return MFX_ERR_MEMORY_ALLOC; + + m_bOwnBufferAllocator = true; + } + + return MFX_ERR_NONE; +} + +mfxStatus CamSysMemFrameAllocator::Close() +{ + mfxStatus sts = BaseFrameAllocator::Close(); + + if (m_bOwnBufferAllocator) + { + delete m_pBufferAllocator; + m_pBufferAllocator = 0; + } + return sts; +} + +mfxStatus CamSysMemFrameAllocator::LockFrame(mfxMemId mid, mfxFrameData *ptr) +{ + if (!m_pBufferAllocator) + return MFX_ERR_NOT_INITIALIZED; + + if (!ptr) + return MFX_ERR_NULL_PTR; + + sFrame *fs = 0; + mfxStatus sts = m_pBufferAllocator->Lock(m_pBufferAllocator->pthis, mid,(mfxU8 **)&fs); + + if (MFX_ERR_NONE != sts) + return sts; + + if (ID_FRAME != fs->id) + { + m_pBufferAllocator->Unlock(m_pBufferAllocator->pthis, mid); + return MFX_ERR_INVALID_HANDLE; + } + + mfxU16 Width2 = (mfxU16)MSDK_ALIGN32(fs->info.Width); // ??? need this ??? + mfxU16 Height2 = (mfxU16)MSDK_ALIGN32(fs->info.Height); + + mfxU8 *alignedPtr = (mfxU8 *)(((size_t)((mfxU8 *)fs + MSDK_ALIGN32(sizeof(sFrame)) + m_alignment)) &~(size_t) (m_alignment - 1)); + + ptr->B = ptr->Y = alignedPtr; + + switch (fs->info.FourCC) + { + case MFX_FOURCC_NV12: + ptr->U = ptr->Y + Width2 * Height2; + ptr->V = ptr->U + 1; + ptr->Pitch = Width2; + break; + case MFX_FOURCC_YV12: + ptr->V = ptr->Y + Width2 * Height2; + ptr->U = ptr->V + (Width2 >> 1) * (Height2 >> 1); + ptr->Pitch = Width2; + break; + case MFX_FOURCC_YUY2: + ptr->U = ptr->Y + 1; + ptr->V = ptr->Y + 3; + ptr->Pitch = 2 * Width2; + break; + case MFX_FOURCC_RGB3: + ptr->G = ptr->B + 1; + ptr->R = ptr->B + 2; + ptr->Pitch = 3 * Width2; + break; + case MFX_FOURCC_RGB4: + ptr->G = ptr->B + 1; + ptr->R = ptr->B + 2; + ptr->A = ptr->B + 3; + ptr->Pitch = 4 * Width2; + break; + case MFX_FOURCC_ARGB16: + ptr->V16 = (mfxU16*)ptr->B; + ptr->U16 = ptr->V16 + 1; + ptr->Y16 = ptr->V16 + 2; + ptr->A = (mfxU8*)(ptr->V16 + 3); + ptr->PitchHigh = (mfxU16)((8 * (mfxU32)Width2) / (1 << 16)); + ptr->PitchLow = (mfxU16)((8 * (mfxU32)Width2) % (1 << 16)); + break; + case MFX_FOURCC_ABGR16: + ptr->Y16 = (mfxU16*)ptr->B; + ptr->U16 = ptr->Y16 + 1; + ptr->V16 = ptr->Y16 + 2; + ptr->A = (mfxU8*)(ptr->Y16 + 3); + ptr->PitchHigh = (mfxU16)((8 * (mfxU32)Width2) / (1 << 16)); + ptr->PitchLow = (mfxU16)((8 * (mfxU32)Width2) % (1 << 16)); + break; + case MFX_FOURCC_R16: + ptr->Y16 = (mfxU16 *)ptr->B; + ptr->Pitch = 2 * Width2; + break; + default: + return MFX_ERR_UNSUPPORTED; + } + + return MFX_ERR_NONE; +} + +mfxStatus CamSysMemFrameAllocator::UnlockFrame(mfxMemId mid, mfxFrameData *ptr) +{ + if (!m_pBufferAllocator) + return MFX_ERR_NOT_INITIALIZED; + + mfxStatus sts = m_pBufferAllocator->Unlock(m_pBufferAllocator->pthis, mid); + + if (MFX_ERR_NONE != sts) + return sts; + + if (NULL != ptr) + { + ptr->Pitch = 0; + ptr->Y = 0; + ptr->U = 0; + ptr->V = 0; + } + + return MFX_ERR_NONE; +} + +mfxStatus CamSysMemFrameAllocator::GetFrameHDL(mfxMemId mid, mfxHDL *handle) +{ + return MFX_ERR_UNSUPPORTED; +} + +mfxStatus CamSysMemFrameAllocator::CheckRequestType(mfxFrameAllocRequest *request) +{ + mfxStatus sts = BaseFrameAllocator::CheckRequestType(request); + if (MFX_ERR_NONE != sts) + return sts; + + if ((request->Type & MFX_MEMTYPE_SYSTEM_MEMORY) != 0) + return MFX_ERR_NONE; + else + return MFX_ERR_UNSUPPORTED; +} + +mfxStatus CamSysMemFrameAllocator::AllocImpl(mfxFrameAllocRequest *request, mfxFrameAllocResponse *response) +{ + if (!m_pBufferAllocator) + return MFX_ERR_NOT_INITIALIZED; + + mfxU32 numAllocated = 0; + + mfxU32 Width2 = MSDK_ALIGN32(request->Info.Width); + mfxU32 Height2 = MSDK_ALIGN32(request->Info.Height); + mfxU32 nbytes; + + switch (request->Info.FourCC) + { + case MFX_FOURCC_YV12: + case MFX_FOURCC_NV12: + nbytes = Width2*Height2 + (Width2>>1)*(Height2>>1) + (Width2>>1)*(Height2>>1); + break; + case MFX_FOURCC_RGB3: + nbytes = Width2*Height2 + Width2*Height2 + Width2*Height2; + break; + case MFX_FOURCC_RGB4: + nbytes = Width2*Height2 + Width2*Height2 + Width2*Height2 + Width2*Height2; + break; + case MFX_FOURCC_ARGB16: + case MFX_FOURCC_ABGR16: + nbytes = (Width2*Height2 + Width2*Height2 + Width2*Height2 + Width2*Height2) << 1; + break; + case MFX_FOURCC_YUY2: + nbytes = Width2*Height2 + (Width2>>1)*(Height2) + (Width2>>1)*(Height2); + break; + case MFX_FOURCC_R16: + nbytes = 2*Width2*Height2; + break; + + default: + return MFX_ERR_UNSUPPORTED; + } + + nbytes += m_alignment; + + std::unique_ptr mids(new (std::nothrow) mfxMemId[request->NumFrameSuggested]); + if (!mids.get()) + return MFX_ERR_MEMORY_ALLOC; + + // allocate frames + for (numAllocated = 0; numAllocated < request->NumFrameSuggested; numAllocated ++) + { + mfxStatus sts = m_pBufferAllocator->Alloc(m_pBufferAllocator->pthis, + nbytes + MSDK_ALIGN32(sizeof(sFrame)), request->Type, &(mids.get()[numAllocated])); + + if (MFX_ERR_NONE != sts) + break; + + sFrame *fs; + sts = m_pBufferAllocator->Lock(m_pBufferAllocator->pthis, mids.get()[numAllocated], (mfxU8 **)&fs); + + if (MFX_ERR_NONE != sts) + break; + + fs->id = ID_FRAME; + fs->info = request->Info; + m_pBufferAllocator->Unlock(m_pBufferAllocator->pthis, mids.get()[numAllocated]); + } + + // check the number of allocated frames + if (numAllocated < request->NumFrameMin) + { + return MFX_ERR_MEMORY_ALLOC; + } + + response->NumFrameActual = (mfxU16) numAllocated; + response->mids = mids.release(); + + return MFX_ERR_NONE; +} + +mfxStatus CamSysMemFrameAllocator::ReleaseResponse(mfxFrameAllocResponse *response) +{ + if (!response) + return MFX_ERR_NULL_PTR; + + if (!m_pBufferAllocator) + return MFX_ERR_NOT_INITIALIZED; + + mfxStatus sts = MFX_ERR_NONE; + + if (response->mids) + { + for (mfxU32 i = 0; i < response->NumFrameActual; i++) + { + if (response->mids[i]) + { + sts = m_pBufferAllocator->Free(m_pBufferAllocator->pthis, response->mids[i]); + if (MFX_ERR_NONE != sts) + return sts; + } + } + } + + delete [] response->mids; + response->mids = 0; + + return sts; +} + +CamSysMemBufferAllocator::CamSysMemBufferAllocator() +{ + +} + +CamSysMemBufferAllocator::~CamSysMemBufferAllocator() +{ + +} + +mfxStatus CamSysMemBufferAllocator::AllocBuffer(mfxU32 nbytes, mfxU16 type, mfxMemId *mid) +{ + if (!mid) + return MFX_ERR_NULL_PTR; + + if (0 == (type & MFX_MEMTYPE_SYSTEM_MEMORY)) + return MFX_ERR_UNSUPPORTED; + + mfxU32 header_size = MSDK_ALIGN32(sizeof(sBuffer)); + mfxU8 *buffer_ptr = (mfxU8 *)calloc(header_size + nbytes + 32, 1); + + if (!buffer_ptr) + return MFX_ERR_MEMORY_ALLOC; + + sBuffer *bs = (sBuffer *)buffer_ptr; + bs->id = ID_BUFFER; + bs->type = type; + bs->nbytes = nbytes; + *mid = (mfxHDL) bs; + return MFX_ERR_NONE; +} + +mfxStatus CamSysMemBufferAllocator::LockBuffer(mfxMemId mid, mfxU8 **ptr) +{ + if (!ptr) + return MFX_ERR_NULL_PTR; + + sBuffer *bs = (sBuffer *)mid; + + if (!bs) + return MFX_ERR_INVALID_HANDLE; + if (ID_BUFFER != bs->id) + return MFX_ERR_INVALID_HANDLE; + + *ptr = (mfxU8*)((size_t)((mfxU8 *)bs+MSDK_ALIGN32(sizeof(sBuffer))+31)&(~((size_t)31))); + return MFX_ERR_NONE; +} + +mfxStatus CamSysMemBufferAllocator::UnlockBuffer(mfxMemId mid) +{ + sBuffer *bs = (sBuffer *)mid; + + if (!bs || ID_BUFFER != bs->id) + return MFX_ERR_INVALID_HANDLE; + + return MFX_ERR_NONE; +} + +mfxStatus CamSysMemBufferAllocator::FreeBuffer(mfxMemId mid) +{ + sBuffer *bs = (sBuffer *)mid; + if (!bs || ID_BUFFER != bs->id) + return MFX_ERR_INVALID_HANDLE; + + free(bs); + return MFX_ERR_NONE; +} diff --git a/samples/sample_camera/src/pipeline_camera.cpp b/samples/sample_camera/src/pipeline_camera.cpp new file mode 100644 index 000000000..629a68726 --- /dev/null +++ b/samples/sample_camera/src/pipeline_camera.cpp @@ -0,0 +1,1900 @@ +/******************************************************************************\ +Copyright (c) 2005-2018, Intel Corporation +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +This sample was distributed or derived from the Intel's Media Samples package. +The original version of this sample may be obtained from https://software.intel.com/en-us/intel-media-server-studio +or https://software.intel.com/en-us/media-client-solutions-support. +\**********************************************************************************/ + +#if defined(_WIN32) || defined(_WIN64) +#include +#include +#endif +#include +#include +#include +#include "pipeline_camera.h" +#include "camera_sysmem_allocator.h" + +#if defined(_WIN32) || defined(_WIN64) +#include "d3d_allocator.h" +#include "d3d11_allocator.h" +#include "d3d_device.h" +#include "d3d11_device.h" +#endif + +#include "version.h" + +#pragma warning(disable : 4100) +#pragma warning(disable : 4996) + +//Library plugin UID +static const mfxU8 CAMERA_PIPE_UID[] = {0x54, 0x54, 0x26, 0x16, 0x24, 0x33, 0x41, 0xe6, 0x93, 0xae, 0x89, 0x99, 0x42, 0xce, 0x73, 0x55}; + +#define CAM_SYSMEM_ALIGNMENT 0x1000 // CM alignment + +//#define CAMP_PIPE_ITT +#ifdef CAMP_PIPE_ITT +#include "ittnotify.h" + + +__itt_domain* CamPipeAccel = __itt_domain_create(L"CamPipeAccel"); + +//__itt_string_handle* CPU_file_fread; +//__itt_string_handle* CPU_raw_unpack_; +__itt_string_handle* task1 = __itt_string_handle_create(L"submit");; +__itt_string_handle* task2 = __itt_string_handle_create(L"bmp_dump");; +__itt_string_handle* task3 = __itt_string_handle_create(L"rendering");; +__itt_string_handle* task4 = __itt_string_handle_create(L"LoadFrame");; + +__itt_string_handle* task5 = __itt_string_handle_create(L"AsyncSubmits");; + +#endif + +mfxU16 gamma_point[64] = +{ + 0, 94, 104, 114, 124, 134, 144, 154, 159, 164, 169, 174, 179, 184, 194, 199, + 204, 209, 214, 219, 224, 230, 236, 246, 256, 266, 276, 286, 296, 306, 316, 326, + 336, 346, 356, 366, 376, 386, 396, 406, 416, 426, 436, 446, 456, 466, 476, 486, + 496, 516, 526, 536, 546, 556, 566, 576, 586, 596, 606, 616, 626, 636, 646, 1023 +}; + +mfxU16 gamma_correct[64] = +{ + 0, 4, 20, 37, 56, 75, 96, 117, 128, 140, 150, 161, 171, 180, 198, 207, + 216, 224, 232, 240, 249, 258, 268, 283, 298, 310, 329, 344, 359, 374, 389, 404, + 420, 435, 451, 466, 482, 498, 515, 531, 548, 565, 582, 599, 617, 635, 653, 671, + 690, 729, 749, 769, 790, 811, 832, 854, 876, 899, 922, 945, 969, 994, 1019,1019 +}; + + + +mfxStatus CCameraPipeline::InitMfxParams(sInputParams *pParams) +{ + MSDK_CHECK_POINTER(m_pmfxVPP, MFX_ERR_NULL_PTR); + mfxStatus sts = MFX_ERR_NONE; + + if (pParams->bDoPadding) + { + // first 8 lines are padded data. Original image should be + // placed starting 8,8 position into the input frame + m_mfxVideoParams.vpp.In.CropX = CAMERA_PADDING_SIZE; + m_mfxVideoParams.vpp.In.CropY = CAMERA_PADDING_SIZE; + + // Take into account padding on each edge + m_mfxVideoParams.vpp.In.Width = (mfxU16)pParams->frameInfo[VPP_IN].nWidth + CAMERA_PADDING_SIZE + CAMERA_PADDING_SIZE; + m_mfxVideoParams.vpp.In.Height = (mfxU16)pParams->frameInfo[VPP_IN].nHeight + CAMERA_PADDING_SIZE + CAMERA_PADDING_SIZE; + } + else + { + m_mfxVideoParams.vpp.In.CropX = 0; + m_mfxVideoParams.vpp.In.CropY = 0; + m_mfxVideoParams.vpp.In.Width = (mfxU16)pParams->frameInfo[VPP_IN].nWidth; + m_mfxVideoParams.vpp.In.Height = (mfxU16)pParams->frameInfo[VPP_IN].nHeight; + } + + // Width and height values must be aligned in order to arhive maximum paerfromance + m_mfxVideoParams.vpp.In.Width = align_32(m_mfxVideoParams.vpp.In.Width); + m_mfxVideoParams.vpp.In.Height = align_32(m_mfxVideoParams.vpp.In.Height); + + m_mfxVideoParams.vpp.In.CropW = (mfxU16)pParams->frameInfo[VPP_IN].CropW; + m_mfxVideoParams.vpp.In.CropH = (mfxU16)pParams->frameInfo[VPP_IN].CropH; + + // Add additional CropX,CropY if any + m_mfxVideoParams.vpp.In.CropX += align_32((mfxU16)pParams->frameInfo[VPP_IN].CropX); + m_mfxVideoParams.vpp.In.CropY += align_32((mfxU16)pParams->frameInfo[VPP_IN].CropY); + m_mfxVideoParams.vpp.In.FourCC = pParams->frameInfo[VPP_IN].FourCC; + //Only R16 input supported now, should use chroma format monochrome + m_mfxVideoParams.vpp.In.ChromaFormat = MFX_CHROMAFORMAT_YUV444; + if (isBayerFormat(pParams->frameInfo[VPP_IN].FourCC)) + { + m_mfxVideoParams.vpp.In.ChromaFormat = MFX_CHROMAFORMAT_MONOCHROME; + } + + m_mfxVideoParams.vpp.In.BitDepthLuma = (mfxU16)pParams->bitDepth; + + // CropW of the output frame must be the same as CropW of the input. Resize is not supported + // The same for CropH + m_mfxVideoParams.vpp.Out.CropW = (mfxU16)pParams->frameInfo[VPP_OUT].CropW; + m_mfxVideoParams.vpp.Out.CropH = (mfxU16)pParams->frameInfo[VPP_OUT].CropH; + + m_mfxVideoParams.vpp.Out.Width = align_32((mfxU16)pParams->frameInfo[VPP_OUT].nWidth); + m_mfxVideoParams.vpp.Out.Height = align_32((mfxU16)pParams->frameInfo[VPP_OUT].nHeight); + m_mfxVideoParams.vpp.Out.CropX = align_32((mfxU16)pParams->frameInfo[VPP_OUT].CropX); + m_mfxVideoParams.vpp.Out.CropY = align_32((mfxU16)pParams->frameInfo[VPP_OUT].CropY); + m_mfxVideoParams.vpp.Out.FourCC = pParams->frameInfo[VPP_OUT].FourCC; + + if (m_mfxVideoParams.vpp.Out.FourCC == MFX_FOURCC_NV12) { + m_mfxVideoParams.vpp.Out.ChromaFormat = MFX_CHROMAFORMAT_YUV420; + m_mfxVideoParams.vpp.Out.BitDepthLuma = 8; + } + else { + //Only ARGB onput supported now, should use chroma format 444 + m_mfxVideoParams.vpp.Out.ChromaFormat = MFX_CHROMAFORMAT_YUV444; + m_mfxVideoParams.vpp.Out.BitDepthLuma = pParams->frameInfo[VPP_OUT].FourCC == MFX_FOURCC_RGB4 ? 8 : m_mfxVideoParams.vpp.In.BitDepthLuma; + } + ConvertFrameRate(pParams->frameInfo[VPP_IN].dFrameRate, &m_mfxVideoParams.vpp.In.FrameRateExtN, &m_mfxVideoParams.vpp.In.FrameRateExtD); + ConvertFrameRate(pParams->frameInfo[VPP_OUT].dFrameRate, &m_mfxVideoParams.vpp.Out.FrameRateExtN, &m_mfxVideoParams.vpp.Out.FrameRateExtD); + + // specify memory type + if (m_memTypeIn != SYSTEM_MEMORY) + m_mfxVideoParams.IOPattern = MFX_IOPATTERN_IN_VIDEO_MEMORY; + else + m_mfxVideoParams.IOPattern = MFX_IOPATTERN_IN_SYSTEM_MEMORY; + + if (m_memTypeOut != SYSTEM_MEMORY) + m_mfxVideoParams.IOPattern |= MFX_IOPATTERN_OUT_VIDEO_MEMORY; + else + m_mfxVideoParams.IOPattern |= MFX_IOPATTERN_OUT_SYSTEM_MEMORY; + + if ( m_mfxVideoParams.vpp.Out.Width > 8*1024 && m_mfxVideoParams.vpp.Out.Height > 8*1024) + { + // If frame resolution is bigger than 8Kx8K, force async to 1 in order to limit memory consumption. + pParams->asyncDepth = 1; + } + + if (pParams->asyncDepth >= 0) + m_mfxVideoParams.AsyncDepth = (mfxU16)pParams->asyncDepth; + else + m_mfxVideoParams.AsyncDepth = CAM_SAMPLE_ASYNC_DEPTH; + + b_3DLUT_Gamma = false; + if (pParams->bGamma) + { + sts = AllocAndInitCamGammaCorrection(pParams); + MSDK_CHECK_STATUS(sts, "AllocAndInitCamGammaCorrection failed"); + if ( pParams->b3DLUTGamma ) + { + b_3DLUT_Gamma = true; + m_ExtBuffers.push_back((mfxExtBuffer *)&m_3DLUT_GammaCorrection); + } + else + { + m_ExtBuffers.push_back((mfxExtBuffer *)&m_GammaCorrection); + } + } + + mfxU32 table_size = 17; // 17,33,65 otherwise error + if (pParams->b3DLUT) + { + + mfxU16 R_INC = 0; + mfxU16 G_INC = 0; + mfxU16 B_INC = 0; + mfxU16 TABLE_INC = 0; + mfxU16 SEG = 0; + + mfxCam3DLutEntry *table_3dlut = NULL; + + if (table_size == 17 ) + { + table_3dlut = m_3dlut_17; + memset(m_3dlut_17, 0, sizeof(mfxCam3DLutEntry)*MFX_CAM_3DLUT17_SIZE); + m_3DLUT.Size = MFX_CAM_3DLUT17_SIZE; + + TABLE_INC = 4096; + SEG = 17; + } + else if (table_size == 33 ) + { + table_3dlut = m_3dlut_33; + memset(m_3dlut_33, 0, sizeof(mfxCam3DLutEntry)*MFX_CAM_3DLUT33_SIZE); + m_3DLUT.Size = MFX_CAM_3DLUT33_SIZE; + + TABLE_INC = 4096>>1; + SEG = 33; + } + else if (table_size == 65 ) + { + table_3dlut = m_3dlut_65; + memset(m_3dlut_65, 0, sizeof(mfxCam3DLutEntry)*MFX_CAM_3DLUT65_SIZE); + m_3DLUT.Size = MFX_CAM_3DLUT65_SIZE; + + TABLE_INC = 4096>>2; + SEG = 65; + } + + for(int i = 0; i < SEG; i++) + { + for(int j = 0; j < SEG; j++) + { + for(int k = 0; k < SEG; k++) + { + table_3dlut[SEG*SEG*i+SEG*j+k].R = R_INC; + table_3dlut[SEG*SEG*i+SEG*j+k].G = G_INC; + table_3dlut[SEG*SEG*i+SEG*j+k].B = B_INC; + + if ( k == SEG-2 ) + B_INC += TABLE_INC-1; + else if ( k < (SEG-2) ) + B_INC += TABLE_INC; + } + + B_INC = 0; + if ( j == SEG-2 ) + G_INC += TABLE_INC-1; + else + G_INC += TABLE_INC; + + } + + B_INC = G_INC = 0; + if ( i == SEG-2 ) + R_INC += TABLE_INC-1; + else + R_INC += TABLE_INC; + + } + m_3DLUT.Table = table_3dlut; + m_ExtBuffers.push_back((mfxExtBuffer *)&m_3DLUT); + } + + if (pParams->bVignette) + { + sts = AllocAndInitVignetteCorrection(pParams); + MSDK_CHECK_STATUS(sts, "AllocAndInitVignetteCorrection failed"); + m_ExtBuffers.push_back((mfxExtBuffer *)&m_Vignette); + } + + if (pParams->bBlackLevel) + { + sts = AllocAndInitCamBlackLevelCorrection(pParams); + MSDK_CHECK_STATUS(sts, "AllocAndInitCamBlackLevelCorrection failed"); + m_ExtBuffers.push_back((mfxExtBuffer *)&m_BlackLevelCorrection); + } +#if MFX_VERSION >= 1023 + if (pParams->bTCC) + { + sts = AllocAndInitCamTotalColorControl(pParams); + MSDK_CHECK_STATUS(sts, "AllocAndInitCamTotalColorControl failed"); + m_ExtBuffers.push_back((mfxExtBuffer *)&m_TotalColorControl); + } + if (pParams->bRGBToYUV) + { + sts = AllocAndInitCamRGBtoYUV(pParams); + MSDK_CHECK_STATUS(sts, "AllocAndInitCamRGBToYUV failed"); + m_ExtBuffers.push_back((mfxExtBuffer *)&m_RGBToYUV); + } +#endif + if (pParams->bHP) + { + sts = AllocAndInitCamHotPixelRemoval(pParams); + MSDK_CHECK_STATUS(sts, "AllocAndInitCamHotPixelRemoval failed"); + m_ExtBuffers.push_back((mfxExtBuffer *)&m_HP); + } + + if (pParams->bBayerDenoise) + { + sts = AllocAndInitDenoise(pParams); + MSDK_CHECK_STATUS(sts, "AllocAndInitDenoise failed"); + m_ExtBuffers.push_back((mfxExtBuffer *)&m_Denoise); + } + + if (pParams->bWhiteBalance) + { + sts = AllocAndInitCamWhiteBalance(pParams); + MSDK_CHECK_STATUS(sts, "AllocAndInitCamWhiteBalance failed"); + m_ExtBuffers.push_back((mfxExtBuffer *)&m_WhiteBalance); + } + + if (pParams->bCCM) + { + sts = AllocAndInitCamCCM(pParams); + MSDK_CHECK_STATUS(sts, "AllocAndInitCamCCM failed"); + m_ExtBuffers.push_back((mfxExtBuffer *)&m_CCM); + } + + if (pParams->bLens) + { + sts = AllocAndInitCamLens(pParams); + MSDK_CHECK_STATUS(sts, "AllocAndInitCamLens failed"); + m_ExtBuffers.push_back((mfxExtBuffer *)&m_Lens); + } + + m_PipeControl.Header.BufferId = MFX_EXTBUF_CAM_PIPECONTROL; + m_PipeControl.Header.BufferSz = sizeof(m_PipeControl); + m_PipeControl.RawFormat = (mfxU16)pParams->inputType; + m_ExtBuffers.push_back((mfxExtBuffer *)&m_PipeControl); + + if (pParams->bDoPadding) + { + m_Padding.Header.BufferId = MFX_EXTBUF_CAM_PADDING; + m_Padding.Header.BufferSz = sizeof(m_Padding); + m_Padding.Top = m_Padding.Bottom = m_Padding.Left = m_Padding.Right = CAMERA_PADDING_SIZE; + m_ExtBuffers.push_back((mfxExtBuffer *)&m_Padding); + } + + if (!m_ExtBuffers.empty()) + AttachExtParam(); + + return MFX_ERR_NONE; +} + + +mfxStatus CCameraPipeline::CreateHWDevice() +{ +#if D3D_SURFACES_SUPPORT + mfxStatus sts = MFX_ERR_NONE; + + HWND window = NULL; + + if (!m_bIsRender) + window = 0; + else + window = m_d3dRender.GetWindowHandle(); + +#if MFX_D3D11_SUPPORT + if (D3D11 == m_accelType) + m_hwdev = new CD3D11Device(); + else +#endif // #if MFX_D3D11_SUPPORT + m_hwdev = new CD3D9Device(); + + if (NULL == m_hwdev) + return MFX_ERR_MEMORY_ALLOC; + sts = m_hwdev->Init( + window, + m_bIsRender ? 1 : 0, + MSDKAdapter::GetNumber(m_mfxSession)); + MSDK_CHECK_STATUS(sts, "m_hwdev->Init failed"); + + if (m_bIsRender) + m_d3dRender.SetHWDevice(m_hwdev); +#endif + return MFX_ERR_NONE; +} + +#define NUM_INPUT_FRAMES_MULTIPLIER 2 + +mfxStatus CCameraPipeline::AllocFrames() +{ + MSDK_CHECK_POINTER(m_pmfxVPP, MFX_ERR_NULL_PTR); + + mfxStatus sts = MFX_ERR_NONE; + + mfxFrameAllocRequest Request[2]; + + MSDK_ZERO_MEMORY(Request); + + // calculate number of surfaces required for camera pipe + sts = m_pmfxVPP->QueryIOSurf(&m_mfxVideoParams, Request); + if (MFX_WRN_PARTIAL_ACCELERATION == sts) + { + msdk_printf(MSDK_STRING("WARNING: partial acceleration\n")); + MSDK_IGNORE_MFX_STS(sts, MFX_WRN_PARTIAL_ACCELERATION); + } + MSDK_CHECK_STATUS(sts, "m_pmfxVPP->QueryIOSurf failed"); + + // kta !!! tmp ??? + Request[VPP_IN].NumFrameSuggested *= NUM_INPUT_FRAMES_MULTIPLIER; + + if (m_memTypeIn != SYSTEM_MEMORY) { + Request[VPP_IN].Type |= MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET; + } else { + Request[VPP_IN].Type |= MFX_MEMTYPE_SYSTEM_MEMORY; + } + + if (m_memTypeOut != SYSTEM_MEMORY) { + Request[VPP_OUT].Type |= MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET; + } else { + Request[VPP_OUT].Type |= MFX_MEMTYPE_SYSTEM_MEMORY; + } + + sMemoryAllocator allocatorIn = {m_pMFXAllocatorIn, m_pmfxAllocatorParamsIn, &m_pmfxSurfacesIn, &m_mfxResponseIn}; + // alloc frames for vpp + // [IN] + sts = InitSurfaces(&allocatorIn, &(Request[VPP_IN]), &(m_mfxVideoParams.vpp.In), m_bExternalAllocIn); + MSDK_CHECK_STATUS(sts, "InitSurfaces failed"); + + // [OUT] + sMemoryAllocator allocatorOut = {m_pMFXAllocatorOut, m_pmfxAllocatorParamsOut, &m_pmfxSurfacesOut, &m_mfxResponseOut}; + sts = InitSurfaces(&allocatorOut, &(Request[VPP_OUT]), &(m_mfxVideoParams.vpp.Out), m_bExternalAllocOut); + MSDK_CHECK_STATUS(sts, "InitSurfaces failed"); + + if (m_memTypeOut == SYSTEM_MEMORY && m_bIsRender) { + + mfxFrameAllocRequest req; + MSDK_ZERO_MEMORY(req); + + req.Info = m_mfxVideoParams.vpp.Out; + req.NumFrameMin = req.NumFrameSuggested = 1; + req.Type = MFX_MEMTYPE_FROM_VPPOUT | MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET; + + sMemoryAllocator allocator = {m_pMFXd3dAllocator, m_pmfxd3dAllocatorParams, &m_pmfxSurfacesAux, &m_mfxResponseAux}; + + sts = InitSurfaces(&allocator, &req, &(m_mfxVideoParams.vpp.Out), true); + MSDK_CHECK_STATUS(sts, "InitSurfaces failed"); + } + + m_lastAllocRequest[VPP_IN] = Request[VPP_IN]; + m_lastAllocRequest[VPP_OUT] = Request[VPP_OUT]; + + return MFX_ERR_NONE; +} + + +mfxStatus CCameraPipeline::ReallocFrames(mfxVideoParam *oldMfxPar) +{ + MSDK_CHECK_POINTER(m_pmfxVPP, MFX_ERR_NULL_PTR); + + mfxStatus sts = MFX_ERR_NONE; + + mfxFrameAllocRequest Request[2]; + MSDK_ZERO_MEMORY(Request); + + // calculate number of surfaces required for camera pipe + sts = m_pmfxVPP->QueryIOSurf(&m_mfxVideoParams, Request); + MSDK_CHECK_STATUS(sts, "m_pmfxVPP->QueryIOSurf failed"); + + if (m_memTypeIn != SYSTEM_MEMORY) { + Request[VPP_IN].Type |= MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET; + } else { + Request[VPP_IN].Type |= MFX_MEMTYPE_SYSTEM_MEMORY; + } + + if (m_memTypeOut != SYSTEM_MEMORY) { + Request[VPP_OUT].Type |= MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET; + } else { + Request[VPP_OUT].Type |= MFX_MEMTYPE_SYSTEM_MEMORY; + } + + if (Request[VPP_IN].Type != m_lastAllocRequest[VPP_IN].Type || Request[VPP_OUT].Type != m_lastAllocRequest[VPP_OUT].Type) + return MFX_ERR_INCOMPATIBLE_VIDEO_PARAM; + + Request[VPP_IN].NumFrameSuggested *= NUM_INPUT_FRAMES_MULTIPLIER; + + bool realloc[2] = {false, false}; + for (int i = 0; i < 2; i++) { + if (Request[i].NumFrameSuggested > m_lastAllocRequest[i].NumFrameSuggested) { + realloc[i] = true; + } + + if (Request[i].Info.Width > m_lastAllocRequest[i].Info.Width || Request[i].Info.Height > m_lastAllocRequest[i].Info.Height) { + realloc[i] = true; + } + } + + if (realloc[VPP_IN] || realloc[VPP_OUT]) // need to Close/Init + return MFX_ERR_INCOMPATIBLE_VIDEO_PARAM; + + for (int j = 0; j < m_mfxResponseOut.NumFrameActual; j++) { + m_pmfxSurfacesOut[j].Info = m_mfxVideoParams.vpp.Out; + } + + for (int j = 0; j < m_mfxResponseIn.NumFrameActual; j++) { + m_pmfxSurfacesIn[j].Info = m_mfxVideoParams.vpp.In; + } + + return MFX_ERR_NONE; +} + + +mfxStatus CCameraPipeline::CreateAllocator() +{ + mfxStatus sts = MFX_ERR_NONE; + mfxHDL hdl = NULL; + + m_bExternalAllocOut = false; + m_bExternalAllocIn = false; + + sts = CreateHWDevice(); + MSDK_CHECK_STATUS(sts, "CreateHWDevice failed"); + + // provide device manager to MediaSDK + //mfxHDL hdl = NULL; + mfxHandleType hdl_t = D3D11 == m_accelType ? MFX_HANDLE_D3D11_DEVICE : MFX_HANDLE_D3D9_DEVICE_MANAGER; + + sts = m_hwdev->GetHandle(hdl_t, &hdl); + MSDK_CHECK_STATUS(sts, "m_hwdev->GetHandle failed"); + sts = m_mfxSession.SetHandle(hdl_t, hdl); + MSDK_CHECK_STATUS(sts, "m_mfxSession.SetHandle failed"); + + if (m_memTypeIn != SYSTEM_MEMORY || m_memTypeOut != SYSTEM_MEMORY) + { +#if MFX_D3D11_SUPPORT + // create D3D allocator + if (D3D11_MEMORY == m_memTypeIn || D3D11_MEMORY == m_memTypeOut) + { + m_pMFXd3dAllocator = new D3D11FrameAllocator; + MSDK_CHECK_POINTER(m_pMFXd3dAllocator, MFX_ERR_MEMORY_ALLOC); + + D3D11AllocatorParams *pd3dAllocParams = new D3D11AllocatorParams; + MSDK_CHECK_POINTER(pd3dAllocParams, MFX_ERR_MEMORY_ALLOC); + pd3dAllocParams->pDevice = reinterpret_cast(hdl); + + m_pmfxd3dAllocatorParams = pd3dAllocParams; + } +#endif + + if (D3D9_MEMORY == m_memTypeIn || D3D9_MEMORY == m_memTypeOut) + { + m_pMFXd3dAllocator = new D3DFrameAllocator; + MSDK_CHECK_POINTER(m_pMFXd3dAllocator, MFX_ERR_MEMORY_ALLOC); + + D3DAllocatorParams *pd3dAllocParams = new D3DAllocatorParams; + MSDK_CHECK_POINTER(pd3dAllocParams, MFX_ERR_MEMORY_ALLOC); + pd3dAllocParams->pManager = reinterpret_cast(hdl); + + m_pmfxd3dAllocatorParams = pd3dAllocParams; + } + + if (m_memTypeIn != SYSTEM_MEMORY) + { + m_pMFXAllocatorIn = m_pMFXd3dAllocator; + m_pmfxAllocatorParamsIn = m_pmfxd3dAllocatorParams; + m_bExternalAllocIn = true; + } + + if (m_memTypeOut != SYSTEM_MEMORY) + { + m_pMFXAllocatorOut = m_pMFXd3dAllocator; + m_pmfxAllocatorParamsOut = m_pmfxd3dAllocatorParams; + m_bExternalAllocOut = true; + } + + + /* In case of video memory we must provide MediaSDK with external allocator + thus we demonstrate "external allocator" usage model. + Call SetAllocator to pass allocator to mediasdk */ + sts = m_mfxSession.SetFrameAllocator(m_pMFXd3dAllocator); + MSDK_CHECK_STATUS(sts, "m_mfxSession.SetFrameAllocator failed"); + } + + if (m_memTypeIn == SYSTEM_MEMORY || m_memTypeOut == SYSTEM_MEMORY) + { + // create system memory allocator + m_pMFXsysAllocator = new CamSysMemFrameAllocator; + MSDK_CHECK_POINTER(m_pMFXsysAllocator, MFX_ERR_MEMORY_ALLOC); + + CamSysMemAllocatorParams *pCamSysAllocParams = new CamSysMemAllocatorParams; + MSDK_CHECK_POINTER(pCamSysAllocParams, MFX_ERR_MEMORY_ALLOC); + pCamSysAllocParams->alignment = CAM_SYSMEM_ALIGNMENT; + m_pmfxsysAllocatorParams = pCamSysAllocParams; + + if (m_memTypeIn == SYSTEM_MEMORY) { + m_pMFXAllocatorIn = m_pMFXsysAllocator; + m_pmfxAllocatorParamsIn = m_pmfxsysAllocatorParams; + } + + if (m_memTypeOut == SYSTEM_MEMORY) { + m_pMFXAllocatorOut = m_pMFXsysAllocator; + m_pmfxAllocatorParamsOut = m_pmfxsysAllocatorParams; + } + + /* In case of system memory we demonstrate "no external allocator" usage model. + We don't call SetAllocator, MediaSDK uses internal allocator. + We use system memory allocator simply as a memory manager for application*/ + } + + if (m_memTypeOut == SYSTEM_MEMORY && m_bIsRender) { +#if MFX_D3D11_SUPPORT + m_pMFXd3dAllocator = new D3D11FrameAllocator; + MSDK_CHECK_POINTER(m_pMFXd3dAllocator, MFX_ERR_MEMORY_ALLOC); + + D3D11AllocatorParams *pd3dAllocParams = new D3D11AllocatorParams; + MSDK_CHECK_POINTER(pd3dAllocParams, MFX_ERR_MEMORY_ALLOC); + pd3dAllocParams->pDevice = reinterpret_cast(hdl); +#else + m_pMFXd3dAllocator = new D3DFrameAllocator; + MSDK_CHECK_POINTER(m_pMFXd3dAllocator, MFX_ERR_MEMORY_ALLOC); + + D3DAllocatorParams *pd3dAllocParams = new D3DAllocatorParams; + MSDK_CHECK_POINTER(pd3dAllocParams, MFX_ERR_MEMORY_ALLOC); + pd3dAllocParams->pManager = reinterpret_cast(hdl); +#endif + m_pmfxd3dAllocatorParams = pd3dAllocParams; + + sts = m_pMFXd3dAllocator->Init(m_pmfxd3dAllocatorParams); + MSDK_CHECK_STATUS(sts, "m_pMFXd3dAllocator->Init failed"); + + sts = m_mfxSession.SetFrameAllocator(m_pMFXd3dAllocator); + MSDK_CHECK_STATUS(sts, "m_mfxSession.SetFrameAllocator failed"); + } + // initialize memory allocator(s) + sts = m_pMFXAllocatorIn->Init(m_pmfxAllocatorParamsIn); + MSDK_CHECK_STATUS(sts, "m_pMFXAllocatorIn->Init failed"); + + if (m_pMFXAllocatorOut != m_pMFXAllocatorIn) { + sts = m_pMFXAllocatorOut->Init(m_pmfxAllocatorParamsOut); + MSDK_CHECK_STATUS(sts, "m_pMFXAllocatorOut->Init failed"); + } + + return MFX_ERR_NONE; +} + +void CCameraPipeline::DeleteFrames() +{ + // delete surfaces array + MSDK_SAFE_DELETE_ARRAY(m_pmfxSurfacesIn); + MSDK_SAFE_DELETE_ARRAY(m_pmfxSurfacesOut); + MSDK_SAFE_DELETE_ARRAY(m_pmfxSurfacesAux); + + // delete frames + if (m_pMFXAllocatorIn) + { + m_pMFXAllocatorIn->Free(m_pMFXAllocatorIn->pthis, &m_mfxResponseIn); + } + if (m_pMFXAllocatorOut) + { + m_pMFXAllocatorOut->Free(m_pMFXAllocatorOut->pthis, &m_mfxResponseOut); + } + if (m_pMFXd3dAllocator) + { + m_pMFXd3dAllocator->Free(m_pMFXd3dAllocator->pthis, &m_mfxResponseAux); + } + + return; +} + +void CCameraPipeline::DeleteAllocator() +{ + // delete allocator + MSDK_SAFE_DELETE(m_pMFXd3dAllocator); + MSDK_SAFE_DELETE(m_pMFXsysAllocator); + MSDK_SAFE_DELETE(m_pmfxd3dAllocatorParams); + MSDK_SAFE_DELETE(m_pmfxsysAllocatorParams); + MSDK_SAFE_DELETE(m_hwdev); + + m_pMFXAllocatorIn = m_pMFXAllocatorOut = NULL; + m_pmfxAllocatorParamsIn = m_pmfxAllocatorParamsIn = NULL; +} + +CCameraPipeline::CCameraPipeline() +{ + m_resetInterval = 0; + MSDK_ZERO_MEMORY(m_Padding); + MSDK_ZERO_MEMORY(m_PipeControl); + b_3DLUT_Gamma=false; + m_numberOfResets=0; + m_nFrameLimit=0; + m_BayerType=0; + + m_nFrameIndex = 0; + m_nInputFileIndex = 0; + m_pmfxVPP = NULL; + m_pMFXd3dAllocator = NULL; + m_pMFXsysAllocator = NULL; + m_pMFXAllocatorIn = NULL; + m_pMFXAllocatorOut = NULL; + m_pmfxd3dAllocatorParams = NULL; + m_pmfxsysAllocatorParams = NULL; + m_pmfxAllocatorParamsIn = NULL; + m_pmfxAllocatorParamsOut = NULL; + m_accelType = D3D9; + m_memTypeIn = SYSTEM_MEMORY; + m_memTypeOut = SYSTEM_MEMORY; + m_bExternalAllocIn = false; + m_bExternalAllocOut = false; + m_pmfxSurfacesIn = m_pmfxSurfacesOut = NULL; + m_pmfxSurfacesAux = NULL; + m_pFileReader = NULL; + m_pBmpWriter = NULL; + m_pRawFileWriter = NULL; + m_bIsExtBuffers = false; + m_bIsRender = false; + m_bOutput = true; + m_bEnd = false; + m_alphaValue = -1; + m_resetCnt = 0; + + m_3dlut_17 = (mfxCam3DLutEntry*) malloc (sizeof(mfxCam3DLutEntry)*MFX_CAM_3DLUT17_SIZE); + m_3dlut_33 = (mfxCam3DLutEntry*) malloc (sizeof(mfxCam3DLutEntry)*MFX_CAM_3DLUT33_SIZE); + m_3dlut_65 = (mfxCam3DLutEntry*) malloc (sizeof(mfxCam3DLutEntry)*MFX_CAM_3DLUT65_SIZE); + + m_hwdev = NULL; + + MSDK_ZERO_MEMORY(m_mfxVideoParams); + MSDK_ZERO_MEMORY(m_mfxResponseIn); + MSDK_ZERO_MEMORY(m_mfxResponseOut); + MSDK_ZERO_MEMORY(m_mfxResponseAux); + + MSDK_ZERO_MEMORY(m_GammaCorrection); + m_GammaCorrection.Header.BufferId = MFX_EXTBUF_CAM_GAMMA_CORRECTION; + m_GammaCorrection.Header.BufferSz = sizeof(m_GammaCorrection); + + MSDK_ZERO_MEMORY(m_3DLUT_GammaCorrection); + m_3DLUT_GammaCorrection.Header.BufferId = MFX_EXTBUF_CAM_FORWARD_GAMMA_CORRECTION; + m_3DLUT_GammaCorrection.Header.BufferSz = sizeof(m_3DLUT_GammaCorrection); +#if MFX_VERSION >= 1023 + MSDK_ZERO_MEMORY(m_TotalColorControl); + m_TotalColorControl.Header.BufferId = MFX_EXTBUF_CAM_TOTAL_COLOR_CONTROL; + m_TotalColorControl.Header.BufferSz = sizeof(m_TotalColorControl); + + MSDK_ZERO_MEMORY(m_RGBToYUV); + m_RGBToYUV.Header.BufferId = MFX_EXTBUF_CAM_CSC_YUV_RGB; + m_RGBToYUV.Header.BufferSz = sizeof(m_RGBToYUV); +#endif + MSDK_ZERO_MEMORY(m_BlackLevelCorrection); + m_BlackLevelCorrection.Header.BufferId = MFX_EXTBUF_CAM_BLACK_LEVEL_CORRECTION; + m_BlackLevelCorrection.Header.BufferSz = sizeof(m_BlackLevelCorrection); + + MSDK_ZERO_MEMORY(m_Denoise); + m_Denoise.Header.BufferId = MFX_EXTBUFF_VPP_DENOISE; + m_Denoise.Header.BufferSz = sizeof(m_Denoise); + + MSDK_ZERO_MEMORY(m_HP); + m_HP.Header.BufferId = MFX_EXTBUF_CAM_HOT_PIXEL_REMOVAL; + m_HP.Header.BufferSz = sizeof(m_HP); + + MSDK_ZERO_MEMORY(m_3DLUT); + m_3DLUT.Header.BufferId = MFX_EXTBUF_CAM_3DLUT; + m_3DLUT.Header.BufferSz = sizeof(m_3DLUT); + + MSDK_ZERO_MEMORY(m_WhiteBalance); + m_WhiteBalance.Header.BufferId = MFX_EXTBUF_CAM_WHITE_BALANCE; + m_WhiteBalance.Header.BufferSz = sizeof(m_WhiteBalance); + + MSDK_ZERO_MEMORY(m_CCM); + m_CCM.Header.BufferId = MFX_EXTBUF_CAM_COLOR_CORRECTION_3X3; + m_CCM.Header.BufferSz = sizeof(m_CCM); + + MSDK_ZERO_MEMORY(m_Vignette); + m_Vignette.Header.BufferId = MFX_EXTBUF_CAM_VIGNETTE_CORRECTION; + m_Vignette.Header.BufferSz = sizeof(m_Vignette); + + MSDK_ZERO_MEMORY(m_Lens); + m_Lens.Header.BufferId = MFX_EXTBUF_CAM_LENS_GEOM_DIST_CORRECTION; + m_Lens.Header.BufferSz = sizeof(m_Lens); +} + +CCameraPipeline::~CCameraPipeline() +{ + Close(); +} + +bool CCameraPipeline::isBayerFormat(mfxU32 type) +{ + if (MFX_FOURCC_ABGR16 == type || MFX_FOURCC_ARGB16 == type) + return false; + + return true; +} + +mfxStatus CCameraPipeline::Init(sInputParams *pParams) +{ + MSDK_CHECK_POINTER(pParams, MFX_ERR_NULL_PTR); + + mfxStatus sts = MFX_ERR_NONE; + + if (pParams->bPerf_opt) + m_pFileReader = new CBufferedVideoReader(); + else if ( isBayerFormat(pParams->inputType) ) + m_pFileReader = new CRawVideoReader(); + else + m_pFileReader = new CARGB16VideoReader(); + + m_nFrameLimit = pParams->nFramesToProceed; + sts = m_pFileReader->Init(pParams); + MSDK_CHECK_STATUS(sts, "m_pFileReader->Init failed"); + + { + if (pParams->frameInfo[VPP_IN].CropW == NOT_INIT_VALUE) + pParams->frameInfo[VPP_IN].CropW = pParams->frameInfo[VPP_IN].nWidth; + if (pParams->frameInfo[VPP_IN].CropH == NOT_INIT_VALUE) + pParams->frameInfo[VPP_IN].CropH = pParams->frameInfo[VPP_IN].nHeight; + + pParams->frameInfo[VPP_IN].CropW = pParams->frameInfo[VPP_IN].CropW; + pParams->frameInfo[VPP_IN].CropX = align(pParams->frameInfo[VPP_IN].CropX); + + pParams->frameInfo[VPP_OUT].nWidth = pParams->frameInfo[VPP_IN].CropW; + pParams->frameInfo[VPP_OUT].nHeight = pParams->frameInfo[VPP_IN].CropH; + pParams->frameInfo[VPP_OUT].CropW = pParams->frameInfo[VPP_OUT].nWidth; + pParams->frameInfo[VPP_OUT].CropH = pParams->frameInfo[VPP_OUT].nHeight; + + pParams->frameInfo[VPP_OUT].nWidth = align_32(pParams->frameInfo[VPP_OUT].nWidth); + pParams->frameInfo[VPP_IN].nWidth = align_32(pParams->frameInfo[VPP_IN].nWidth); + pParams->frameInfo[VPP_IN].nHeight = align(pParams->frameInfo[VPP_IN].nHeight); + } + + pParams->frameInfo[VPP_IN].FourCC = isBayerFormat(pParams->inputType) ? MFX_FOURCC_R16 : pParams->inputType; + + // set memory type + + m_memTypeIn = SYSTEM_MEMORY; + m_memTypeOut = SYSTEM_MEMORY; + m_accelType = pParams->accelType; + if ( pParams->accelType == D3D9 && pParams->memTypeOut == VIDEO) + { + m_memTypeOut = D3D9_MEMORY; + } + else if ( pParams->accelType == D3D11 && pParams->memTypeOut == VIDEO) + { + m_memTypeOut = D3D11_MEMORY; + } + + if ( pParams->accelType == D3D9 && pParams->memTypeIn == VIDEO) + { + m_memTypeIn = D3D9_MEMORY; + } + else if ( pParams->accelType == D3D11 && pParams->memTypeIn == VIDEO) + { + m_memTypeIn = D3D11_MEMORY; + } + + // API version + mfxVersion version = {10, MFX_VERSION_MAJOR}; + + // Init session + { + // try searching on all display adapters + mfxIMPL impl = MFX_IMPL_HARDWARE_ANY; + + // if d3d11 surfaces are used ask the library to run acceleration through D3D11 + // feature may be unsupported due to OS or MSDK API version + if (D3D11 == pParams->accelType) + impl |= MFX_IMPL_VIA_D3D11; + else if (D3D9 == pParams->accelType) + impl |= MFX_IMPL_VIA_D3D9; + + sts = m_mfxSession.Init(impl, &version); + + // MSDK API version may not support multiple adapters - then try initialize on the default + if (MFX_ERR_NONE != sts) + sts = m_mfxSession.Init(impl & !MFX_IMPL_HARDWARE_ANY | MFX_IMPL_HARDWARE, &version); + } + + MSDK_CHECK_STATUS(sts, "m_mfxSession.Init failed"); + + // create VPP + m_pmfxVPP = new MFXVideoVPP(m_mfxSession); + MSDK_CHECK_POINTER(m_pmfxVPP, MFX_ERR_MEMORY_ALLOC); + + + // Initialize rendering window + if (pParams->bRendering) + { +#if D3D_SURFACES_SUPPORT + sWindowParams windowParams; + + windowParams.lpWindowName = pParams->bWallNoTitle ? NULL : MSDK_STRING("sample_camera"); + windowParams.nx = pParams->nWallW; + windowParams.ny = pParams->nWallH; + windowParams.nWidth = CW_USEDEFAULT; + windowParams.nHeight = CW_USEDEFAULT; + windowParams.ncell = pParams->nWallCell; + windowParams.nAdapter = pParams->nWallMonitor; + windowParams.nMaxFPS = pParams->nWallFPS; + + windowParams.lpClassName = MSDK_STRING("Render Window Class"); + windowParams.dwStyle = WS_OVERLAPPEDWINDOW; + windowParams.hWndParent = NULL; + windowParams.hMenu = NULL; + windowParams.hInstance = GetModuleHandle(NULL); + windowParams.lpParam = NULL; + windowParams.bFullScreen = FALSE; + + m_d3dRender.Init(windowParams); + + SetRenderingFlag(); +#endif + } + + // create device and allocator. SetHandle must be called after session Init and before any other MSDK calls, + // otherwise an own device will be created by MSDK + + sts = CreateAllocator(); + MSDK_CHECK_STATUS(sts, "CreateAllocator failed"); + + //Load library plug-in + { + MSDK_MEMCPY(m_UID_Camera.Data, CAMERA_PIPE_UID, 16); + sts = MFXVideoUSER_Load(m_mfxSession, &m_UID_Camera, pParams->CameraPluginVersion); + MSDK_CHECK_STATUS(sts, "MFXVideoUSER_Load failed"); + } + + if (pParams->bGamma) + { + pParams->gamma_mode = MFX_CAM_GAMMA_LUT; // tmp ??? kta + if (pParams->gamma_mode == MFX_CAM_GAMMA_LUT && ! pParams->bExternalGammaLUT) { + for (int i = 0; i < 64; i++) { + pParams->gamma_point[i] = gamma_point[i]; + pParams->gamma_corrected[i] = gamma_correct[i]; + } + } + } + + sts = InitMfxParams(pParams); + MSDK_CHECK_STATUS(sts, "InitMfxParams failed"); + + SetOutputfileFlag(pParams->bOutput); + if (m_bOutput) + { + // prepare bmp file writer + if (pParams->frameInfo[VPP_OUT].FourCC == MFX_FOURCC_ARGB16 || + pParams->frameInfo[VPP_OUT].FourCC == MFX_FOURCC_ABGR16 || + pParams->frameInfo[VPP_OUT].FourCC == MFX_FOURCC_NV12) + { + m_pRawFileWriter = new CRawVideoWriter; + sts = m_pRawFileWriter->Init(pParams); + } + else if (pParams->frameInfo[VPP_OUT].FourCC == MFX_FOURCC_RGB4) + { + m_pBmpWriter = new CBmpWriter; + sts = m_pBmpWriter->Init(pParams->strDstFile, m_mfxVideoParams.vpp.Out.CropW, m_mfxVideoParams.vpp.Out.CropH, pParams->maxNumBmpFiles); + } + MSDK_CHECK_STATUS(sts, "Writer->Init failed"); + } + + sts = m_pmfxVPP->Query(&m_mfxVideoParams, &m_mfxVideoParams); + MSDK_IGNORE_MFX_STS(sts, MFX_WRN_INCOMPATIBLE_VIDEO_PARAM); + MSDK_IGNORE_MFX_STS(sts, MFX_WRN_PARTIAL_ACCELERATION); + MSDK_CHECK_STATUS(sts, "m_pmfxVPP->Query failed"); + + sts = m_pmfxVPP->Init(&m_mfxVideoParams); + if (MFX_WRN_PARTIAL_ACCELERATION == sts) + { + msdk_printf(MSDK_STRING("WARNING: partial acceleration\n")); + MSDK_IGNORE_MFX_STS(sts, MFX_WRN_PARTIAL_ACCELERATION); + } + MSDK_CHECK_STATUS(sts, "m_pmfxVPP->Init failed"); + + // ??? need this ? + sts = m_pmfxVPP->GetVideoParam(&m_mfxVideoParams); + MSDK_CHECK_STATUS(sts, "m_pmfxVPP->GetVideoParam failed"); + + m_alphaValue = pParams->alphaValue; + m_BayerType = pParams->inputType; + + m_numberOfResets = (mfxU32)pParams->resetParams.size(); + m_resetInterval = pParams->resetInterval; + + sts = AllocFrames(); + MSDK_CHECK_STATUS(sts, "AllocFrames failed"); + + return MFX_ERR_NONE; +} + +// function for allocating a specific external buffer +template +mfxStatus CCameraPipeline::AllocateExtBuffer() +{ + std::auto_ptr pExtBuffer (new Buffer()); + if (!pExtBuffer.get()) + return MFX_ERR_MEMORY_ALLOC; + + init_ext_buffer(*pExtBuffer); + + m_ExtBuffers.push_back(reinterpret_cast(pExtBuffer.release())); + + return MFX_ERR_NONE; +} + +void CCameraPipeline::AttachExtParam() +{ + m_mfxVideoParams.ExtParam = reinterpret_cast(&m_ExtBuffers[0]); + m_mfxVideoParams.NumExtParam = static_cast(m_ExtBuffers.size()); +} + +void CCameraPipeline::DeleteExtBuffers() +{ + for (std::vector::iterator it = m_ExtBuffers.begin(); it != m_ExtBuffers.end(); ++it) + delete *it; + m_ExtBuffers.clear(); +} + +mfxStatus CCameraPipeline::AllocAndInitCamBlackLevelCorrection(sInputParams *pParams) +{ + m_BlackLevelCorrection.B = pParams->black_level_B; + m_BlackLevelCorrection.G0 = pParams->black_level_G0; + m_BlackLevelCorrection.G1 = pParams->black_level_G1; + m_BlackLevelCorrection.R = pParams->black_level_R; + + return MFX_ERR_NONE; +} + +#if MFX_VERSION >= 1023 +mfxStatus CCameraPipeline::AllocAndInitCamRGBtoYUV(sInputParams *pParams) +{ + for (int i = 0; i < 3; i++) + m_RGBToYUV.PreOffset[i] = pParams->pre[i]; + for (int i = 0; i < 3; i++) + m_RGBToYUV.PostOffset[i] = pParams->post[i]; + //Coefs of CSC (RGB to YUV) are hardcoded but they can be handled by app through command line as pre and post offsets if it will be needed + m_RGBToYUV.Matrix[0][0] = 0.299f; + m_RGBToYUV.Matrix[0][1] = 0.587f; + m_RGBToYUV.Matrix[0][2] = 0.114f; + m_RGBToYUV.Matrix[1][0] = -0.147f; + m_RGBToYUV.Matrix[1][1] = -0.289f; + m_RGBToYUV.Matrix[1][2] = 0.436f; + m_RGBToYUV.Matrix[2][0] = 0.615f; + m_RGBToYUV.Matrix[2][1] = -0.515f; + m_RGBToYUV.Matrix[2][2] = -0.100f; + + return MFX_ERR_NONE; +} + +mfxStatus CCameraPipeline::AllocAndInitCamTotalColorControl(sInputParams *pParams) +{ + m_TotalColorControl.R = pParams->tcc_red; + m_TotalColorControl.G = pParams->tcc_green; + m_TotalColorControl.B = pParams->tcc_blue; + m_TotalColorControl.C = pParams->tcc_cyan; + m_TotalColorControl.M = pParams->tcc_magenta; + m_TotalColorControl.Y = pParams->tcc_yellow; + + return MFX_ERR_NONE; +} +#endif + +mfxStatus CCameraPipeline::AllocAndInitDenoise(sInputParams *pParams) +{ + m_Denoise.Threshold = pParams->denoiseThreshold; + return MFX_ERR_NONE; +} + +mfxStatus CCameraPipeline::AllocAndInitCamHotPixelRemoval(sInputParams *pParams) +{ + m_HP.PixelCountThreshold = pParams->hp_num; + m_HP.PixelThresholdDifference = pParams->hp_diff; + + return MFX_ERR_NONE; +} + +mfxStatus CCameraPipeline::AllocAndInitCamWhiteBalance(sInputParams *pParams) +{ + m_WhiteBalance.Mode = MFX_CAM_WHITE_BALANCE_MANUAL; + m_WhiteBalance.B = pParams->white_balance_B; + m_WhiteBalance.G0 = pParams->white_balance_G0; + m_WhiteBalance.G1 = pParams->white_balance_G1; + m_WhiteBalance.R = pParams->white_balance_R; + + return MFX_ERR_NONE; +} + +mfxStatus CCameraPipeline::AllocAndInitCamLens(sInputParams *pParams) +{ + m_Lens.a[0] = pParams->lens_aR; + m_Lens.a[1] = pParams->lens_aG; + m_Lens.a[2] = pParams->lens_aB; + m_Lens.b[0] = pParams->lens_bR; + m_Lens.b[1] = pParams->lens_bG; + m_Lens.b[2] = pParams->lens_bB; + m_Lens.c[0] = pParams->lens_cR; + m_Lens.c[1] = pParams->lens_cG; + m_Lens.c[2] = pParams->lens_cB; + m_Lens.d[0] = pParams->lens_dR; + m_Lens.d[1] = pParams->lens_dG; + m_Lens.d[2] = pParams->lens_dB; + return MFX_ERR_NONE; +} + +mfxStatus CCameraPipeline::AllocAndInitCamCCM(sInputParams *pParams) +{ + for (int i = 0; i < 3; i++) + for (int j = 0; j < 3; j++) + m_CCM.CCM[i][j] = pParams->CCM[i][j]; + + return MFX_ERR_NONE; +} + +mfxStatus CCameraPipeline::AllocAndInitVignetteCorrection(sInputParams *pParams) +{ + FILE *maskFile; + mfxU32 i, nBytesRead; + + /* Vignette correction mask must be ( 1/4 height + 1 ) (1/4 width + 1) dimension */ + /* If height is not divisible by 4, add extra +1 to result of division - that's equivalent to ceil() */ + m_Vignette.Height = pParams->frameInfo->nHeight/4 + (pParams->frameInfo->nHeight%4 ? 2 : 1); + m_Vignette.Width = align_64((pParams->frameInfo->nWidth/4 + (pParams->frameInfo->nWidth%4 ? 2 : 1) ) + * 4 /* for each channel */ * 2 /* 2 bytes each */); + + /* Vignette correction mask data must have 64-aligned pitch */ + m_Vignette.Pitch = align_64(m_Vignette.Width); + + m_Vignette.CorrectionMap = (mfxCamVignetteCorrectionParam *)malloc(m_Vignette.Pitch * m_Vignette.Height); + memset(m_Vignette.CorrectionMap, 0, m_Vignette.Pitch * m_Vignette.Height); + + /* Load vignette mask from file */ + MSDK_FOPEN(maskFile, pParams->strVignetteMaskFile, MSDK_STRING("rb")); + MSDK_CHECK_POINTER(maskFile, MFX_ERR_ABORTED); + for (i = 0; i < m_Vignette.Height; i++) + { +#if defined(_WIN32) || defined(_WIN64) + nBytesRead = (mfxU32)fread_s((mfxU8 *)m_Vignette.CorrectionMap + i * m_Vignette.Pitch, m_Vignette.Pitch, 1, m_Vignette.Width, maskFile); +#else + nBytesRead = (mfxU32)fread((mfxU8 *)m_Vignette.CorrectionMap + i * m_Vignette.Pitch, 1, m_Vignette.Width, maskFile); +#endif + IOSTREAM_CHECK_NOT_EQUAL_SAFE(nBytesRead, m_Vignette.Width, MFX_ERR_MORE_DATA,fclose(maskFile)); + } + fclose(maskFile); + return MFX_ERR_NONE; +} + +void CCameraPipeline::FreeVignetteCorrection() +{ + MSDK_SAFE_DELETE(m_Vignette.CorrectionMap); +} + +mfxStatus CCameraPipeline::AllocAndInitCamGammaCorrection(sInputParams *pParams) +{ + if ( pParams->b3DLUTGamma ) + { + m_3DLUT_GammaCorrection.NumSegments = 64; + m_3DLUT_GammaCorrection.Segment = new mfxCamFwdGammaSegment[64]; + MSDK_CHECK_POINTER(m_3DLUT_GammaCorrection.Segment, MFX_ERR_ABORTED); + for ( int i = 0; i < 64; i++) + { + // Use the same curve for R/G/B + m_3DLUT_GammaCorrection.Segment[i].Pixel = pParams->gamma_point[i]; + m_3DLUT_GammaCorrection.Segment[i].Red = pParams->gamma_corrected[i]; + m_3DLUT_GammaCorrection.Segment[i].Green = pParams->gamma_corrected[i]; + m_3DLUT_GammaCorrection.Segment[i].Blue = pParams->gamma_corrected[i]; + } + } + else + { + m_GammaCorrection.Mode = pParams->gamma_mode; + if (m_GammaCorrection.Mode == MFX_CAM_GAMMA_LUT) + { + m_GammaCorrection.NumPoints = 64; + MSDK_MEMCPY(m_GammaCorrection.GammaPoint, pParams->gamma_point, m_GammaCorrection.NumPoints*sizeof(mfxU16)); + MSDK_MEMCPY(m_GammaCorrection.GammaCorrected, pParams->gamma_corrected, m_GammaCorrection.NumPoints*sizeof(mfxU16)); + } + else if (m_GammaCorrection.Mode == MFX_CAM_GAMMA_VALUE) + { + m_GammaCorrection.GammaValue = pParams->gamma_value; + } + } + + return MFX_ERR_NONE; +} + +void CCameraPipeline::FreeCamGammaCorrection() +{ + if (b_3DLUT_Gamma) + { + if ( m_3DLUT_GammaCorrection.Segment ) + delete [] m_3DLUT_GammaCorrection.Segment; + } +} + +void CCameraPipeline::Close() +{ + if (m_pmfxVPP) + m_pmfxVPP->Close(); + MSDK_SAFE_DELETE(m_pmfxVPP); + + DeleteFrames(); + + FreeVignetteCorrection(); + FreeCamGammaCorrection(); + m_ExtBuffers.clear(); + + m_pCamera_plugin.reset(); + MFXVideoUSER_UnLoad(m_mfxSession, &m_UID_Camera); + m_mfxSession.Close(); + + if (m_pFileReader) { + m_pFileReader->Close(); + MSDK_SAFE_DELETE(m_pFileReader); + } + + if (m_pBmpWriter) { + MSDK_SAFE_DELETE(m_pBmpWriter); + } + + if (m_pRawFileWriter) { + MSDK_SAFE_DELETE(m_pRawFileWriter); + } + +#if D3D_SURFACES_SUPPORT + if (m_bIsRender) + m_d3dRender.Close(); +#endif + + MSDK_SAFE_FREE(m_3dlut_17); + MSDK_SAFE_FREE(m_3dlut_33); + MSDK_SAFE_FREE(m_3dlut_65); + + // allocator if used as external for MediaSDK must be deleted after decoder + DeleteAllocator(); + + + + return; +} + +mfxStatus CCameraPipeline::Reset(sInputParams *pParams) +{ + mfxStatus sts = MFX_ERR_NONE; + + mfxVideoParam oldMfxParams = m_mfxVideoParams; + + m_alphaValue = pParams->alphaValue; + m_BayerType = pParams->inputType; + + +// m_memTypeIn = pParams->memTypeIn; +// m_memTypeOut = pParams->memTypeOut; +// +// if (m_memTypeOut == UNDEFINED_MEMORY) { +// if (pParams->bRendering) { +//#if MFX_D3D11_SUPPORT +// m_memTypeOut = D3D11_MEMORY; +//#else +// m_memTypeOut = D3D9_MEMORY; +//#endif +// } else +// m_memTypeOut = SYSTEM_MEMORY; +// } +// +// m_memType = (MemType)((m_memTypeIn | m_memTypeOut) & (UNDEFINED_MEMORY - 1)); +// if (m_memType & D3D11_MEMORY) +// m_memType = D3D11_MEMORY; + + + MSDK_ZERO_MEMORY(m_mfxVideoParams); + m_ExtBuffers.clear(); + + sts = m_pFileReader->Init(pParams); + MSDK_CHECK_STATUS(sts, "m_pFileReader->Init failed"); + + sts = InitMfxParams(pParams); + MSDK_CHECK_STATUS(sts, "InitMfxParams failed"); + + sts = m_pmfxVPP->Reset(&m_mfxVideoParams); + MSDK_CHECK_STATUS(sts, "m_pmfxVPP->Reset failed"); + + sts = ReallocFrames(&oldMfxParams); + MSDK_CHECK_STATUS(sts, "ReallocFrames failed"); + + if (m_bOutput) + { + // prepare bmp file writer + if (pParams->frameInfo[VPP_OUT].FourCC == MFX_FOURCC_ARGB16 || + pParams->frameInfo[VPP_OUT].FourCC == MFX_FOURCC_ABGR16 || + pParams->frameInfo[VPP_OUT].FourCC == MFX_FOURCC_NV12) { + sts = m_pRawFileWriter->Init(pParams); + } else { + sts = m_pBmpWriter->Init(pParams->strDstFile, m_mfxVideoParams.vpp.Out.CropW, m_mfxVideoParams.vpp.Out.CropH, pParams->maxNumBmpFiles); + } + MSDK_CHECK_STATUS(sts, "Writer->Init failed"); + } + + return MFX_ERR_NONE; +} + +mfxStatus CCameraPipeline::PrepareInputSurfaces() +{ + mfxStatus sts = MFX_ERR_NONE; + mfxU32 asyncDepth = MSDK_MIN(m_mfxVideoParams.AsyncDepth, m_mfxResponseIn.NumFrameActual); + asyncDepth = MSDK_MIN(asyncDepth, m_mfxResponseOut.NumFrameActual); + + mfxU32 frnum = m_nInputFileIndex; + + m_pFileReader->SetStartFileNumber((mfxI32)frnum); + + for (;;) { + mfxFrameSurface1 *pSurface = 0; + for (mfxU32 i = 0; i < asyncDepth; i++) { + + if (m_nFrameLimit > 0 && frnum >= m_nFrameLimit) + return MFX_ERR_ABORTED; + + for (;;) { + if (m_bEnd) + return MFX_ERR_ABORTED; + pSurface = &m_pmfxSurfacesIn[i]; + if (pSurface->Data.Locked == 0) + break; + pSurface = &m_pmfxSurfacesIn[i + asyncDepth]; + if (pSurface->Data.Locked == 0) + break; + MSDK_SLEEP(1); + } + + if (pSurface->Data.MemId) + { +#ifdef CAMP_PIPE_ITT + __itt_task_begin(CamPipeAccel, __itt_null, __itt_null, task4); +#endif +#if D3D_SURFACES_SUPPORT + if (m_memTypeIn == D3D11_MEMORY) + sts = m_pMFXAllocatorIn->Lock(m_pMFXAllocatorIn->pthis, MFXReadWriteMid(pSurface->Data.MemId, MFXReadWriteMid::write), &pSurface->Data); + else +#endif + sts = m_pMFXAllocatorIn->Lock(m_pMFXAllocatorIn->pthis, pSurface->Data.MemId, &pSurface->Data); + if (MFX_ERR_NONE != sts) + break; + sts = m_pFileReader->LoadNextFrame(&pSurface->Data, &m_mfxVideoParams.vpp.In, m_BayerType); + if (MFX_ERR_NONE != sts) + break; +#if D3D_SURFACES_SUPPORT + if (m_memTypeIn == D3D11_MEMORY) + sts = m_pMFXAllocatorIn->Unlock(m_pMFXAllocatorIn->pthis, MFXReadWriteMid(pSurface->Data.MemId, MFXReadWriteMid::write), &pSurface->Data); + else +#endif + sts = m_pMFXAllocatorIn->Unlock(m_pMFXAllocatorIn->pthis, pSurface->Data.MemId, &pSurface->Data); + if (MFX_ERR_NONE != sts) + break; +#ifdef CAMP_PIPE_ITT + __itt_task_end(CamPipeAccel); +#endif + + } + else + { + +#ifdef CAMP_PIPE_ITT + __itt_task_begin(CamPipeAccel, __itt_null, __itt_null, task4); +#endif + + sts = m_pFileReader->LoadNextFrame(&pSurface->Data, &m_mfxVideoParams.vpp.In, m_BayerType); + +#ifdef CAMP_PIPE_ITT + __itt_task_end(CamPipeAccel); +#endif + + if (MFX_ERR_NONE != sts) + break; + + camera_printf("to surface %p \n", pSurface->Data.Y16); + camera_fflush(stdout); + } + + if (m_bEnd) + return MFX_ERR_ABORTED; + + msdk_atomic_inc16((volatile mfxU16*)&(pSurface->Data.Locked)); + + frnum++; + } + if (sts != MFX_ERR_NONE) { + //if (pSurface) + // msdk_atomic_dec16((volatile mfxU16*)&(pSurface->Data.Locked)); + return sts; + } + } +} + + +mfxU32 MFX_STDCALL InputThreadRoutine(void* par) +{ + CCameraPipeline *pPipe = (CCameraPipeline *)par; + + pPipe->PrepareInputSurfaces(); + + return 0; + +} + +mfxStatus CCameraPipeline::Run() +{ + mfxStatus sts = MFX_ERR_NONE; + m_bEnd = false; + bool quitOnFrameLimit = false; + + mfxU32 asyncDepth = MSDK_MIN(m_mfxVideoParams.AsyncDepth, m_mfxResponseIn.NumFrameActual); + asyncDepth = MSDK_MIN(asyncDepth, m_mfxResponseOut.NumFrameActual); + + mfxSyncPoint *syncpoints = new mfxSyncPoint[asyncDepth]; + mfxFrameSurface1 **ppOutSurf = new mfxFrameSurface1*[asyncDepth]; + mfxFrameSurface1 **ppInSurf = new mfxFrameSurface1*[asyncDepth]; + + MSDKThread * pthread = new MSDKThread(sts, InputThreadRoutine, (void*)this); + + mfxI32 asdepth; + mfxI32 tail_start = 0, tail_len = (mfxI32)asyncDepth - 1; + + mfxU8 *syncFlags = new mfxU8[asyncDepth]; + memset(syncFlags, 0, asyncDepth); + +#ifdef CAMP_PIPE_ITT + __itt_task_begin(CamPipeAccel, __itt_null, __itt_null, task5); +#endif + + for (asdepth = 0; asdepth < (mfxI32)asyncDepth; asdepth++) + { + mfxFrameSurface1 *pInSurf, *pOutSurf; + + pInSurf = &m_pmfxSurfacesIn[asdepth]; + while (pInSurf->Data.Locked != 1) { + MSDK_SLEEP(1); + if (pthread->GetExitCode() != MFX_TASK_WORKING) { + if (pInSurf->Data.Locked != 1) { + tail_len = asdepth; + sts = MFX_ERR_MORE_DATA; + break; + } + } + } + MSDK_BREAK_ON_ERROR(sts); + + ppInSurf[asdepth] = pInSurf; + + //mfxU16 idx = GetFreeSurface(m_pmfxSurfacesOut, m_mfxResponseOut.NumFrameActual); + //pOutSurf = &m_pmfxSurfacesOut[idx]; + //pOutSurf->Data.Locked++; + + pOutSurf = &m_pmfxSurfacesOut[asdepth]; + //sts = GetFreeSurface(m_pmfxSurfacesOut, m_mfxResponseOut.NumFrameActual, &pOutSurf); + MSDK_BREAK_ON_ERROR(sts); + + ppOutSurf[asdepth] = pOutSurf; + +#ifdef CAMP_PIPE_ITT + __itt_task_begin(CamPipeAccel, __itt_null, __itt_null, task1); +#endif + + pOutSurf->Data.FrameOrder = pInSurf->Data.FrameOrder; + + sts = m_pmfxVPP->RunFrameVPPAsync(pInSurf, pOutSurf, + NULL, + &syncpoints[asdepth]); + syncFlags[asdepth] = 1; + camera_printf("vpp_async %d in %p out %p %d \n", asdepth, pInSurf->Data.Y16, pOutSurf->Data.B, pInSurf->Data.FrameOrder); + camera_fflush(stdout); + + +#ifdef CAMP_PIPE_ITT + __itt_task_end(CamPipeAccel); +#endif + + MSDK_BREAK_ON_ERROR(sts); + + } + +#ifdef CAMP_PIPE_ITT + __itt_task_end(CamPipeAccel); +#endif + + + while (MFX_ERR_NONE <= sts) + { + if (MFX_WRN_DEVICE_BUSY == sts) + { + MSDK_SLEEP(1); // just wait and then repeat the same call to VPPFrameAsync (??? kta) + } + + mfxFrameSurface1 *pInSurf, *pOutSurf; + + for (asdepth = 0; asdepth < (mfxI32)asyncDepth; asdepth++) + { + camera_printf("sync --- %d %p %d %d \n", asdepth, ppInSurf[asdepth]->Data.Y16, ppInSurf[asdepth]->Data.Locked, ppInSurf[asdepth]->Data.FrameOrder); + + sts = m_mfxSession.SyncOperation(syncpoints[asdepth], MSDK_VPP_WAIT_INTERVAL); + + MSDK_BREAK_ON_ERROR(sts); + + syncFlags[asdepth] = 0; + + ReleaseSurface(ppInSurf[asdepth]); + + camera_printf("-------- %p %d %d \n", ppInSurf[asdepth]->Data.Y16, ppInSurf[asdepth]->Data.Locked, ppInSurf[asdepth]->Data.FrameOrder); camera_fflush(stdout); + + if (m_bIsRender) + { +#ifdef CAMP_PIPE_ITT + __itt_task_begin(CamPipeAccel, __itt_null, __itt_null, task3); +#endif + +#if D3D_SURFACES_SUPPORT + + if (m_memTypeOut == SYSTEM_MEMORY) { + // slow tmp solution !!! kta +#if MFX_D3D11_SUPPORT + if (m_accelType == D3D9) + sts = m_pMFXd3dAllocator->Lock(m_pMFXd3dAllocator->pthis, m_pmfxSurfacesAux->Data.MemId, &m_pmfxSurfacesAux->Data); + else + sts = m_pMFXd3dAllocator->Lock(m_pMFXd3dAllocator->pthis, MFXReadWriteMid(m_pmfxSurfacesAux->Data.MemId, MFXReadWriteMid::write), &m_pmfxSurfacesAux->Data); + MSDK_BREAK_ON_ERROR(sts); + if (ppOutSurf[asdepth]->Info.CropW * 4 == ppOutSurf[asdepth]->Data.Pitch && ppOutSurf[asdepth]->Data.Pitch == m_pmfxSurfacesAux->Data.Pitch) + memcpy_s(m_pmfxSurfacesAux->Data.B, m_pmfxSurfacesAux->Data.Pitch*m_pmfxSurfacesAux->Info.Height, ppOutSurf[asdepth]->Data.B, ppOutSurf[asdepth]->Info.CropW*ppOutSurf[asdepth]->Info.CropH*4); + else { + for (int i = 0; i < ppOutSurf[asdepth]->Info.CropH; i++) + memcpy_s(m_pmfxSurfacesAux->Data.B + i * m_pmfxSurfacesAux->Data.Pitch, m_pmfxSurfacesAux->Data.Pitch, ppOutSurf[asdepth]->Data.B + i*ppOutSurf[asdepth]->Data.Pitch, ppOutSurf[asdepth]->Info.CropW*4); + } + if (m_accelType == D3D9) + sts = m_pMFXd3dAllocator->Unlock(m_pMFXd3dAllocator->pthis, m_pmfxSurfacesAux->Data.MemId, &m_pmfxSurfacesAux->Data); + else + sts = m_pMFXd3dAllocator->Unlock(m_pMFXd3dAllocator->pthis, MFXReadWriteMid(m_pmfxSurfacesAux->Data.MemId, MFXReadWriteMid::write), &m_pmfxSurfacesAux->Data); +#else + sts = m_pMFXd3dAllocator->Lock(m_pMFXd3dAllocator->pthis, m_pmfxSurfacesAux->Data.MemId, &m_pmfxSurfacesAux->Data); + MSDK_BREAK_ON_ERROR(sts); + if (ppOutSurf[asdepth]->Info.CropW * 4 == ppOutSurf[asdepth]->Data.Pitch && ppOutSurf[asdepth]->Data.Pitch == m_pmfxSurfacesAux->Data.Pitch) + memcpy_s(m_pmfxSurfacesAux->Data.B, m_pmfxSurfacesAux->Data.Pitch*m_pmfxSurfacesAux->Info.Height, ppOutSurf[asdepth]->Data.B, ppOutSurf[asdepth]->Info.CropW*ppOutSurf[asdepth]->Info.CropH*4); + else { + for (int i = 0; i < ppOutSurf[asdepth]->Info.CropH; i++) + memcpy_s(m_pmfxSurfacesAux->Data.B + i * m_pmfxSurfacesAux->Data.Pitch, m_pmfxSurfacesAux->Data.Pitch, ppOutSurf[asdepth]->Data.B + i*ppOutSurf[asdepth]->Data.Pitch, ppOutSurf[asdepth]->Info.CropW*4); + } + sts = m_pMFXd3dAllocator->Unlock(m_pMFXd3dAllocator->pthis, m_pmfxSurfacesAux->Data.MemId, &m_pmfxSurfacesAux->Data); +#endif + MSDK_BREAK_ON_ERROR(sts); + sts = m_d3dRender.RenderFrame(m_pmfxSurfacesAux, m_pMFXd3dAllocator); + + } else + sts = m_d3dRender.RenderFrame(ppOutSurf[asdepth], m_pMFXd3dAllocator); +#endif + +#ifdef CAMP_PIPE_ITT + __itt_task_end(CamPipeAccel); +#endif + + if (sts == MFX_ERR_NULL_PTR) + sts = MFX_ERR_NONE; + MSDK_BREAK_ON_ERROR(sts); + } + + if (m_bOutput) { + + if (ppOutSurf[asdepth]->Data.MemId) { +#if D3D_SURFACES_SUPPORT + if (m_memTypeOut == D3D11_MEMORY) + sts = m_pMFXAllocatorOut->Lock(m_pMFXAllocatorOut->pthis, MFXReadWriteMid(ppOutSurf[asdepth]->Data.MemId, MFXReadWriteMid::read), &ppOutSurf[asdepth]->Data); + else +#endif + sts = m_pMFXAllocatorOut->Lock(m_pMFXAllocatorOut->pthis, ppOutSurf[asdepth]->Data.MemId, &ppOutSurf[asdepth]->Data); + MSDK_BREAK_ON_ERROR(sts); + } + + if (m_alphaValue >= 0) { + if (ppOutSurf[asdepth]->Info.FourCC == MFX_FOURCC_ARGB16) { + int stride = ppOutSurf[asdepth]->Data.Pitch >> 1; + for (int i = 0; i < ppOutSurf[asdepth]->Info.Height; i++) { + for (int j = 0; j < ppOutSurf[asdepth]->Info.Width; j++) { + *(ppOutSurf[asdepth]->Data.V16 + 3 + j*4 + i*stride) = (mfxU16)m_alphaValue; + } + } + } + else if (ppOutSurf[asdepth]->Info.FourCC == MFX_FOURCC_ABGR16) { + int stride = ppOutSurf[asdepth]->Data.Pitch >> 1; + for (int i = 0; i < ppOutSurf[asdepth]->Info.Height; i++) { + for (int j = 0; j < ppOutSurf[asdepth]->Info.Width; j++) { + *(ppOutSurf[asdepth]->Data.Y16 + 3 + j * 4 + i*stride) = (mfxU16)m_alphaValue; + } + } + } + else if (ppOutSurf[asdepth]->Info.FourCC == MFX_FOURCC_RGB4) { + int stride = ppOutSurf[asdepth]->Data.Pitch; + for (int i = 0; i < ppOutSurf[asdepth]->Info.Height; i++) { + for (int j = 0; j < ppOutSurf[asdepth]->Info.Width; j++) { + *(ppOutSurf[asdepth]->Data.A + j*4 + i*stride) = (mfxU8)m_alphaValue; + } + } + } + } +#ifdef CAMP_PIPE_ITT + __itt_task_begin(CamPipeAccel, __itt_null, __itt_null, task2); +#endif + + camera_printf("writing frame %d \n", ppOutSurf[asdepth]->Data.FrameOrder); + + if (m_pRawFileWriter) { + if (ppOutSurf[asdepth]->Info.FourCC == MFX_FOURCC_ARGB16 || + ppOutSurf[asdepth]->Info.FourCC == MFX_FOURCC_ABGR16) + m_pRawFileWriter->WriteFrameARGB16(&ppOutSurf[asdepth]->Data, 0, &ppOutSurf[asdepth]->Info); + else if (ppOutSurf[asdepth]->Info.FourCC == MFX_FOURCC_NV12) + m_pRawFileWriter->WriteFrameNV12(&ppOutSurf[asdepth]->Data, 0, &ppOutSurf[asdepth]->Info); + } else if (m_pBmpWriter) + m_pBmpWriter->WriteFrame(&ppOutSurf[asdepth]->Data, 0, &ppOutSurf[asdepth]->Info); + +#ifdef CAMP_PIPE_ITT + __itt_task_end(CamPipeAccel); +#endif + + if (ppOutSurf[asdepth]->Data.MemId) { +#if D3D_SURFACES_SUPPORT + if (m_memTypeOut == D3D11_MEMORY) + sts = m_pMFXAllocatorOut->Unlock(m_pMFXAllocatorOut->pthis, MFXReadWriteMid(ppOutSurf[asdepth]->Data.MemId, MFXReadWriteMid::read), &ppOutSurf[asdepth]->Data); + else +#endif + sts = m_pMFXAllocatorOut->Unlock(m_pMFXAllocatorOut->pthis, ppOutSurf[asdepth]->Data.MemId, &ppOutSurf[asdepth]->Data); + MSDK_BREAK_ON_ERROR(sts); + } + } + + //ReleaseSurface(pInSurf); + ReleaseSurface(ppOutSurf[asdepth]); + + ++m_nFrameIndex; + if (m_resetInterval > 0 && ((m_nInputFileIndex == m_resetInterval - 1)) && m_resetCnt < m_numberOfResets) { + m_nInputFileIndex = 0; + quitOnFrameLimit = true; + m_resetCnt++; + tail_start = asdepth + 1; + sts = MFX_WRN_VIDEO_PARAM_CHANGED; + break; + } + ++m_nInputFileIndex; + { + //progress + msdk_printf(MSDK_STRING("Frame number: %d\r"), m_nFrameIndex); + } + if (m_nFrameLimit > 0 && m_nFrameIndex >= m_nFrameLimit) + { + //m_bEnd = true; + quitOnFrameLimit = true; + sts = MFX_ERR_MORE_DATA; + break; + } + +// sts = PrepareInSurface(&pInSurf, asdepth); +// MSDK_BREAK_ON_ERROR(sts); + + pInSurf = &m_pmfxSurfacesIn[asdepth]; + mfxFrameSurface1 *pInSurf1 = &m_pmfxSurfacesIn[asdepth + asyncDepth]; + + for (;;) { + camera_printf("%p %p %d %d %d %d \n", pInSurf->Data.Y16, pInSurf1->Data.Y16, pInSurf->Data.Locked, pInSurf1->Data.Locked, pInSurf->Data.FrameOrder, pInSurf1->Data.FrameOrder); + camera_fflush(stdout); + + if (pInSurf->Data.Locked == 1 || pInSurf1->Data.Locked == 1) { + if ((pInSurf->Data.Locked != 1) || ((pInSurf1->Data.Locked == 1) && (pInSurf1->Data.FrameOrder < pInSurf->Data.FrameOrder))) + pInSurf = pInSurf1; + break; + } else if (pthread->GetExitCode() != MFX_TASK_WORKING) { + tail_start = asdepth + 1; + sts = MFX_ERR_MORE_DATA; + break; + } + MSDK_SLEEP(1); + } + MSDK_BREAK_ON_ERROR(sts); + + ppInSurf[asdepth] = pInSurf; + + pOutSurf = &m_pmfxSurfacesOut[asdepth]; + //sts = GetFreeSurface(m_pmfxSurfacesOut, m_mfxResponseOut.NumFrameActual, &pOutSurf); + //MSDK_BREAK_ON_ERROR(sts); + + ppOutSurf[asdepth] = pOutSurf; + +#ifdef CAMP_PIPE_ITT + __itt_task_begin(CamPipeAccel, __itt_null, __itt_null, task1); +#endif + + pOutSurf->Data.FrameOrder = pInSurf->Data.FrameOrder; + + sts = m_pmfxVPP->RunFrameVPPAsync(pInSurf, pOutSurf, + NULL, + &syncpoints[asdepth]); + syncFlags[asdepth] = 1; + + +#ifdef CAMP_PIPE_ITT + __itt_task_end(CamPipeAccel); +#endif + + MSDK_BREAK_ON_ERROR(sts); + + camera_printf("vpp_async %d in %p out %p %d \n", asdepth, pInSurf->Data.Y16, pOutSurf->Data.B, pInSurf->Data.FrameOrder); + camera_fflush(stdout); + } + MSDK_IGNORE_MFX_STS(sts, MFX_ERR_MORE_SURFACE); + MSDK_BREAK_ON_ERROR(sts); + } + MSDK_IGNORE_MFX_STS(sts, MFX_ERR_MORE_DATA); + + camera_printf("tail_start %d tail_len %d \n", tail_start, tail_len); + + if (sts == MFX_ERR_NONE || sts == MFX_WRN_VIDEO_PARAM_CHANGED) { + mfxI32 tail_asdepth, ii; + mfxI32 frameIdx = m_nFrameIndex; + + mfxStatus local_sts = MFX_ERR_NONE; + + tail_asdepth = tail_start; + for (ii = 0; ii < tail_len; ii++, tail_asdepth++) + { + if (tail_asdepth == (mfxI32)asyncDepth) + tail_asdepth = 0; + + if (syncFlags[tail_asdepth] == 0) + continue; + + camera_printf("sync tail --- %d %p %d %d \n", tail_asdepth, ppInSurf[tail_asdepth]->Data.Y16, ppInSurf[tail_asdepth]->Data.Locked, ppInSurf[tail_asdepth]->Data.FrameOrder); + + local_sts = m_mfxSession.SyncOperation(syncpoints[tail_asdepth], MSDK_VPP_WAIT_INTERVAL); + MSDK_BREAK_ON_ERROR(local_sts); + syncFlags[tail_asdepth] = 0; + + if (m_bIsRender && !quitOnFrameLimit) + { +#if D3D_SURFACES_SUPPORT + + if (m_memTypeOut == SYSTEM_MEMORY) { +#if MFX_D3D11_SUPPORT + if (m_accelType == D3D9) + local_sts = m_pMFXd3dAllocator->Lock(m_pMFXd3dAllocator->pthis, m_pmfxSurfacesAux->Data.MemId, &m_pmfxSurfacesAux->Data); + else + local_sts = m_pMFXd3dAllocator->Lock(m_pMFXd3dAllocator->pthis, MFXReadWriteMid(m_pmfxSurfacesAux->Data.MemId, MFXReadWriteMid::write), &m_pmfxSurfacesAux->Data); + MSDK_BREAK_ON_ERROR(local_sts); + if (ppOutSurf[tail_asdepth]->Info.CropW * 4 == ppOutSurf[tail_asdepth]->Data.Pitch && ppOutSurf[tail_asdepth]->Data.Pitch == m_pmfxSurfacesAux->Data.Pitch) + memcpy_s(m_pmfxSurfacesAux->Data.B, m_pmfxSurfacesAux->Data.Pitch*m_pmfxSurfacesAux->Info.Height, ppOutSurf[tail_asdepth]->Data.B, ppOutSurf[tail_asdepth]->Info.CropW*ppOutSurf[tail_asdepth]->Info.CropH*4); + else { + for (int i = 0; i < ppOutSurf[tail_asdepth]->Info.CropH; i++) + memcpy_s(m_pmfxSurfacesAux->Data.B + i * m_pmfxSurfacesAux->Data.Pitch, m_pmfxSurfacesAux->Data.Pitch, ppOutSurf[tail_asdepth]->Data.B + i*ppOutSurf[tail_asdepth]->Data.Pitch, ppOutSurf[tail_asdepth]->Info.CropW*4); + } + + if (m_accelType == D3D9) + local_sts = m_pMFXd3dAllocator->Unlock(m_pMFXd3dAllocator->pthis, m_pmfxSurfacesAux->Data.MemId, &m_pmfxSurfacesAux->Data); + else + local_sts = m_pMFXd3dAllocator->Unlock(m_pMFXd3dAllocator->pthis, MFXReadWriteMid(m_pmfxSurfacesAux->Data.MemId, MFXReadWriteMid::write), &m_pmfxSurfacesAux->Data); +#else + local_sts = m_pMFXd3dAllocator->Lock(m_pMFXd3dAllocator->pthis, m_pmfxSurfacesAux->Data.MemId, &m_pmfxSurfacesAux->Data); + MSDK_BREAK_ON_ERROR(local_sts); + if (ppOutSurf[tail_asdepth]->Info.CropW * 4 == ppOutSurf[tail_asdepth]->Data.Pitch && ppOutSurf[tail_asdepth]->Data.Pitch == m_pmfxSurfacesAux->Data.Pitch) + memcpy_s(m_pmfxSurfacesAux->Data.B, m_pmfxSurfacesAux->Data.Pitch*m_pmfxSurfacesAux->Info.Height, ppOutSurf[tail_asdepth]->Data.B, ppOutSurf[tail_asdepth]->Info.CropW*ppOutSurf[tail_asdepth]->Info.CropH*4); + else { + for (int i = 0; i < ppOutSurf[tail_asdepth]->Info.CropH; i++) + memcpy_s(m_pmfxSurfacesAux->Data.B + i * m_pmfxSurfacesAux->Data.Pitch, m_pmfxSurfacesAux->Data.Pitch, ppOutSurf[tail_asdepth]->Data.B + i*ppOutSurf[tail_asdepth]->Data.Pitch, ppOutSurf[tail_asdepth]->Info.CropW*4); + } + local_sts = m_pMFXd3dAllocator->Unlock(m_pMFXd3dAllocator->pthis, m_pmfxSurfacesAux->Data.MemId, &m_pmfxSurfacesAux->Data); +#endif + MSDK_BREAK_ON_ERROR(local_sts); + local_sts = m_d3dRender.RenderFrame(m_pmfxSurfacesAux, m_pMFXd3dAllocator); + + } else + local_sts = m_d3dRender.RenderFrame(ppOutSurf[tail_asdepth], m_pMFXd3dAllocator); +#endif + + if (local_sts == MFX_ERR_NULL_PTR) + local_sts = MFX_ERR_NONE; + MSDK_BREAK_ON_ERROR(local_sts); + } + + + if (m_bOutput && !quitOnFrameLimit) { + + if (ppOutSurf[tail_asdepth]->Data.MemId) { +#if D3D_SURFACES_SUPPORT + if (m_memTypeOut == D3D11_MEMORY) + local_sts = m_pMFXAllocatorOut->Lock(m_pMFXAllocatorOut->pthis, MFXReadWriteMid(ppOutSurf[tail_asdepth]->Data.MemId, MFXReadWriteMid::read), &ppOutSurf[tail_asdepth]->Data); + else +#endif + local_sts = m_pMFXAllocatorOut->Lock(m_pMFXAllocatorOut->pthis, ppOutSurf[tail_asdepth]->Data.MemId, &ppOutSurf[tail_asdepth]->Data); + } + camera_printf("--writing frame %d \n", ppOutSurf[tail_asdepth]->Data.FrameOrder); + + if (m_alphaValue >= 0) { + if (ppOutSurf[tail_asdepth]->Info.FourCC == MFX_FOURCC_ARGB16) { + int stride = ppOutSurf[tail_asdepth]->Data.Pitch >> 1; + for (int i = 0; i < ppOutSurf[tail_asdepth]->Info.Height; i++) { + for (int j = 0; j < ppOutSurf[tail_asdepth]->Info.Width; j++) { + *(ppOutSurf[tail_asdepth]->Data.V16 + 3 + j*4 + i*stride) = (mfxU16)m_alphaValue; + } + } + } + else if (ppOutSurf[tail_asdepth]->Info.FourCC == MFX_FOURCC_ABGR16) { + int stride = ppOutSurf[tail_asdepth]->Data.Pitch >> 1; + for (int i = 0; i < ppOutSurf[tail_asdepth]->Info.Height; i++) { + for (int j = 0; j < ppOutSurf[tail_asdepth]->Info.Width; j++) { + *(ppOutSurf[tail_asdepth]->Data.Y16 + 3 + j * 4 + i*stride) = (mfxU16)m_alphaValue; + } + } + } + else if (ppOutSurf[tail_asdepth]->Info.FourCC == MFX_FOURCC_RGB4) { + int stride = ppOutSurf[tail_asdepth]->Data.Pitch; + for (int i = 0; i < ppOutSurf[tail_asdepth]->Info.Height; i++) { + for (int j = 0; j < ppOutSurf[tail_asdepth]->Info.Width; j++) { + *(ppOutSurf[tail_asdepth]->Data.A + j*4 + i*stride) = (mfxU8)m_alphaValue; + } + } + } + } + if (m_pRawFileWriter) { + if (ppOutSurf[tail_asdepth]->Info.FourCC == MFX_FOURCC_ARGB16 || + ppOutSurf[tail_asdepth]->Info.FourCC == MFX_FOURCC_ABGR16) + m_pRawFileWriter->WriteFrameARGB16(&ppOutSurf[tail_asdepth]->Data, 0, &ppOutSurf[tail_asdepth]->Info); + else if (ppOutSurf[tail_asdepth]->Info.FourCC == MFX_FOURCC_NV12) + m_pRawFileWriter->WriteFrameNV12(&ppOutSurf[tail_asdepth]->Data, 0, &ppOutSurf[tail_asdepth]->Info); + } + else if (m_pBmpWriter) + m_pBmpWriter->WriteFrame(&ppOutSurf[tail_asdepth]->Data, 0, &ppOutSurf[tail_asdepth]->Info); + + if (ppOutSurf[tail_asdepth]->Data.MemId) { +#if D3D_SURFACES_SUPPORT + if (m_memTypeOut == D3D11_MEMORY) + local_sts = m_pMFXAllocatorOut->Unlock(m_pMFXAllocatorOut->pthis, MFXReadWriteMid(ppOutSurf[tail_asdepth]->Data.MemId, MFXReadWriteMid::read), &ppOutSurf[tail_asdepth]->Data); + else +#endif + local_sts = m_pMFXAllocatorOut->Unlock(m_pMFXAllocatorOut->pthis, ppOutSurf[tail_asdepth]->Data.MemId, &ppOutSurf[tail_asdepth]->Data); + } + } + + ReleaseSurface(ppInSurf[tail_asdepth]); + ReleaseSurface(ppOutSurf[tail_asdepth]); + ++frameIdx; + msdk_printf(MSDK_STRING("Frame number: %d\r"), frameIdx); + } + if (!quitOnFrameLimit) + m_nFrameIndex = frameIdx; + + sts = (local_sts != MFX_ERR_NONE) ? local_sts : sts; + } + + m_bEnd = true; + pthread->Wait(); + MSDK_SAFE_DELETE(pthread); + + MSDK_SAFE_DELETE_ARRAY(syncpoints); + MSDK_SAFE_DELETE_ARRAY(ppOutSurf); + MSDK_SAFE_DELETE_ARRAY(ppInSurf); + MSDK_SAFE_DELETE_ARRAY(syncFlags); + + for (int i = 0; i < m_mfxResponseIn.NumFrameActual; i++) + m_pmfxSurfacesIn[i].Data.Locked = 0; + + return sts; +} + +void CCameraPipeline::PrintInfo() +{ + msdk_printf(MSDK_STRING("Intel(R) Camera SDK Sample Version %s\n\n"), GetMSDKSampleVersion().c_str()); + + mfxFrameInfo Info = m_mfxVideoParams.vpp.In; + msdk_printf(MSDK_STRING("Input format\t%s\n"), FourCC2Str( Info.FourCC )); + msdk_printf(MSDK_STRING("Resolution\t%dx%d\n"), Info.Width, Info.Height); + msdk_printf(MSDK_STRING("Crop X,Y,W,H\t%d,%d,%d,%d\n"), Info.CropX, Info.CropY, Info.CropW, Info.CropH); + msdk_printf(MSDK_STRING("Frame rate\t%.2f\n"), (mfxF64)Info.FrameRateExtN / Info.FrameRateExtD); + + Info = m_mfxVideoParams.vpp.Out; + msdk_printf(MSDK_STRING("Output format\t%s\n"), FourCC2Str( Info.FourCC )); + msdk_printf(MSDK_STRING("Resolution\t%dx%d\n"), Info.Width, Info.Height); + msdk_printf(MSDK_STRING("Crop X,Y,W,H\t%d,%d,%d,%d\n"), Info.CropX, Info.CropY, Info.CropW, Info.CropH); + msdk_printf(MSDK_STRING("Frame rate\t%.2f\n"), (mfxF64)Info.FrameRateExtN / Info.FrameRateExtD); + + + //mfxFrameInfo Info = m_mfxVideoParams.mfx.FrameInfo; + //msdk_printf(MSDK_STRING("Resolution\t%dx%d\n"), Info.Width, Info.Height); + //msdk_printf(MSDK_STRING("Crop X,Y,W,H\t%d,%d,%d,%d\n"), Info.CropX, Info.CropY, Info.CropW, Info.CropH); + + //mfxF64 dFrameRate = CalculateFrameRate(Info.FrameRateExtN, Info.FrameRateExtD); + //msdk_printf(MSDK_STRING("Frame rate\t%.2f\n"), dFrameRate); + + const msdk_char* sMemTypeIn = m_memTypeIn == D3D9_MEMORY ? MSDK_STRING("d3d") + : (m_memTypeIn == D3D11_MEMORY ? MSDK_STRING("d3d11") + : MSDK_STRING("system")); + msdk_printf(MSDK_STRING("Input memory type\t\t%s\n"), sMemTypeIn); + const msdk_char* sMemTypeOut = m_memTypeOut == D3D9_MEMORY ? MSDK_STRING("d3d") + : (m_memTypeOut == D3D11_MEMORY ? MSDK_STRING("d3d11") + : MSDK_STRING("system")); + msdk_printf(MSDK_STRING("Output memory type\t\t%s\n"), sMemTypeOut); + + mfxIMPL impl; + m_mfxSession.QueryIMPL(&impl); + + const msdk_char* sImpl = (MFX_IMPL_VIA_D3D11 == MFX_IMPL_VIA_MASK(impl)) ? MSDK_STRING("hw_d3d11") + : (MFX_IMPL_HARDWARE & impl) ? MSDK_STRING("hw") + : MSDK_STRING("sw"); + msdk_printf(MSDK_STRING("MediaSDK impl\t\t%s\n"), sImpl); + + mfxVersion ver; + m_mfxSession.QueryVersion(&ver); + msdk_printf(MSDK_STRING("MediaSDK version\t%d.%d\n"), ver.Major, ver.Minor); + + msdk_printf(MSDK_STRING("\n")); + + return; +} diff --git a/samples/sample_camera/src/sample_camera.cpp b/samples/sample_camera/src/sample_camera.cpp new file mode 100644 index 000000000..ce45ab18c --- /dev/null +++ b/samples/sample_camera/src/sample_camera.cpp @@ -0,0 +1,795 @@ +/******************************************************************************\ +Copyright (c) 2005-2018, Intel Corporation +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +This sample was distributed or derived from the Intel's Media Samples package. +The original version of this sample may be obtained from https://software.intel.com/en-us/intel-media-server-studio +or https://software.intel.com/en-us/media-client-solutions-support. +\**********************************************************************************/ +#include "pipeline_camera.h" +#include +#include "version.h" + +void PrintHelp(msdk_char *strAppName, const msdk_char *strErrorMessage) +{ + msdk_printf(MSDK_STRING("Intel(R) Camera Expert SDK Sample Version %s\n\n"), GetMSDKSampleVersion().c_str()); + + if (strErrorMessage) + { + msdk_printf(MSDK_STRING("Error: %s\n"), strErrorMessage); + } + + msdk_printf(MSDK_STRING("Usage: %s [Options] -i InputFileNameBase -o OutputFileNameBase [numberOfFilesToDump]\n"), strAppName); + msdk_printf(MSDK_STRING("Options: \n")); + msdk_printf(MSDK_STRING(" [-plugin_version ver] - define camera plugin version\n")); + msdk_printf(MSDK_STRING(" [-a asyncDepth] / [-asyncDepth asyncDepth] - set async depth, default %d \n"), CAM_SAMPLE_ASYNC_DEPTH); + msdk_printf(MSDK_STRING(" [-b bitDepth] / [-bitDepth bitDepth] - set bit depth, default 10 \n")); + msdk_printf(MSDK_STRING(" [-f format] / [-format format] - input Bayer format: rggb, bggr, grbg or gbrg\n")); + msdk_printf(MSDK_STRING(" [-of format] / [-outFormat format] - output format: of argb16 or 16 meaning 16 bit ARGB, 8 bit ARGB, NV12\n")); + msdk_printf(MSDK_STRING(" [-offset pre1 pre2 pre3 post1 post2 post3] - In case of NV12 output, offset set color pre and post offsets\n")); + msdk_printf(MSDK_STRING(" [-3DLUT_gamma] - use 3D LUT gamma correction\n")); + msdk_printf(MSDK_STRING(" [-ng] / [-noGamma] - no gamma correction\n")); + msdk_printf(MSDK_STRING(" [-gamma_points] - set specific gamma points (64 points expected)\n")); + msdk_printf(MSDK_STRING(" [-gamma_corrected] - set specific gamma corrected values (64 values expected)\n")); + msdk_printf(MSDK_STRING(" -gamma_points and -gamma_corrected options must be used together\n")); + msdk_printf(MSDK_STRING(" [-bdn threshold] / [-bayerDenoise threshold] - bayer denoise on\n")); + msdk_printf(MSDK_STRING(" [-hot_pixel Diff Num] - bayer hot pixel removal\n")); + msdk_printf(MSDK_STRING(" [-bbl B G0 G1 R] / [-bayerBlackLevel B G0 G1 R] - bayer black level correction\n")); + msdk_printf(MSDK_STRING(" [-tcc R G B C M Y] / [-totalColorControl R G B C M Y] - total color control \n")); + msdk_printf(MSDK_STRING(" [-bwb B G0 G1 R] / [-bayerWhiteBalance B G0 G1 R] - bayer white balance\n")); + msdk_printf(MSDK_STRING(" [-ccm n00 n01 ... n33 ] - color correction 3x3 matrix\n")); + msdk_printf(MSDK_STRING(" [-vignette maskfile ] - enable vignette correction using mask from specified file\n")); + msdk_printf(MSDK_STRING(" [-lens a b c d ] - enable lens geometry distortion correction\n")); + msdk_printf(MSDK_STRING(" [-chroma_aberration aR bR cR dR aG bG cG dG aB bB cB dB ] - enable chroma aberration correction\n")); + msdk_printf(MSDK_STRING(" [-w width] / [-width width] - input width, default 4096\n")); + msdk_printf(MSDK_STRING(" [-h height] / [-height height] - input height, default 2160\n")); + msdk_printf(MSDK_STRING(" [-n numFrames] / [-numFramesToProcess numFrames] - number of frames to process\n")); + msdk_printf(MSDK_STRING(" [-alpha alpha] - write value to alpha channel of output surface \n")); + msdk_printf(MSDK_STRING(" [-pd] / [-padding] - do input surface padding \n")); + msdk_printf(MSDK_STRING(" [-perf_opt] - buffered reading of input (support: Bayer sequence \n")); + msdk_printf(MSDK_STRING(" [-resetInterval resetInterval] - reset interval in frames, default 7 \n")); + msdk_printf(MSDK_STRING(" [-reset -i ... -o ... -f ... -w ... -h ... -bbl ... -bwb ... -ccm ...] - params to be used after next reset.\n")); + msdk_printf(MSDK_STRING(" Only params listed above are supported, if a param is not set here, the originally set value is used. \n")); + msdk_printf(MSDK_STRING(" There can be any number of resets, applied in order of appearance in the command line, \n")); + msdk_printf(MSDK_STRING(" after resetInterval frames are processed with the current params \n")); + msdk_printf(MSDK_STRING(" [-imem] - input memory type (sys|video). Default is system\n")); +#if D3D_SURFACES_SUPPORT + msdk_printf(MSDK_STRING(" [-omem] - output memory type (sys|video). Default is system\n")); + msdk_printf(MSDK_STRING(" [-accel] - type of acceleration device (d3d9|d3d11). Default is d3d9\n")); + msdk_printf(MSDK_STRING(" [-r] / [-render] - render output in a separate window \n")); + msdk_printf(MSDK_STRING(" [-wall w h n m f t tmo] - same as -r, and positioned rendering window in a particular cell on specific monitor \n")); + msdk_printf(MSDK_STRING(" w - number of columns of video windows on selected monitor\n")); + msdk_printf(MSDK_STRING(" h - number of rows of video windows on selected monitor\n")); + msdk_printf(MSDK_STRING(" n(0,.,w*h-1) - order of video window in table that will be rendered\n")); + msdk_printf(MSDK_STRING(" m(0,1..) - monitor id \n")); + msdk_printf(MSDK_STRING(" f - rendering framerate\n")); + msdk_printf(MSDK_STRING(" t(0/1) - enable/disable window's title\n")); + msdk_printf(MSDK_STRING(" tmo - timeout for -wall option\n")); +#endif +#if defined(_WIN32) || defined(_WIN64) + msdk_printf(MSDK_STRING("\nFeatures: \n")); + msdk_printf(MSDK_STRING(" Press 1 to toggle fullscreen rendering on/off\n")); +#endif + msdk_printf(MSDK_STRING("\n")); +} + +#define GET_OPTION_POINTER(PTR) \ +{ \ + if (2 == msdk_strlen(strInput[i])) \ + { \ + i++; \ + if (strInput[i][0] == MSDK_CHAR('-')) \ + { \ + i = i - 1; \ + } \ + else \ + { \ + PTR = strInput[i]; \ + } \ + } \ + else \ + { \ + PTR = strInput[i] + 2; \ + } \ +} + +mfxStatus ParseInputString(msdk_char* strInput[], mfxU8 nArgNum, sInputParams* pParams) +{ + if (1 == nArgNum) + { + PrintHelp(strInput[0], NULL); + return MFX_ERR_UNSUPPORTED; + } + + sResetParams resPar; + + MSDK_CHECK_POINTER(pParams, MFX_ERR_NULL_PTR); + + for (mfxU8 i = 1; i < nArgNum; i++) + { + // multi-character options + if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-plugin_version"))) + { + msdk_opt_read(strInput[++i], pParams->CameraPluginVersion); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-accel"))) + { + if(i + 1 >= nArgNum) + { + PrintHelp(strInput[0], MSDK_STRING("Not enough parameters for -accel key")); + return MFX_ERR_UNSUPPORTED; + } + + if (0 == msdk_strcmp(strInput[i+1], MSDK_STRING("d3d9"))) + { + pParams->accelType = D3D9; + } + else if (0 == msdk_strcmp(strInput[i+1], MSDK_STRING("d3d11"))) + { + pParams->accelType = D3D11; + } + else + { + PrintHelp(strInput[0], MSDK_STRING("Unsupported value for -accel key")); + return MFX_ERR_UNSUPPORTED; + } + i++; + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-perf_opt"))) + { + pParams->bPerf_opt = true; + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-imem"))) + { + if(i + 1 >= nArgNum) + { + PrintHelp(strInput[0], MSDK_STRING("Not enough parameters for -imem key")); + return MFX_ERR_UNSUPPORTED; + } + if (0 == msdk_strcmp(strInput[i+1], MSDK_STRING("system"))) + { + pParams->memTypeIn = SYSTEM; + } + else if (0 == msdk_strcmp(strInput[i+1], MSDK_STRING("video"))) + { + pParams->memTypeIn = VIDEO; + } + else + { + PrintHelp(strInput[0], MSDK_STRING("Unsupported value for -imem key")); + return MFX_ERR_UNSUPPORTED; + } + i++; + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-omem"))) + { + if(i + 1 >= nArgNum) + { + PrintHelp(strInput[0], MSDK_STRING("Not enough parameters for -omem key")); + return MFX_ERR_UNSUPPORTED; + } + if (0 == msdk_strcmp(strInput[i+1], MSDK_STRING("system"))) + { + pParams->memTypeOut = SYSTEM; + } + else if (0 == msdk_strcmp(strInput[i+1], MSDK_STRING("video"))) + { + pParams->memTypeOut = VIDEO; + } + else + { + PrintHelp(strInput[0], MSDK_STRING("Unsupported value for -omem key")); + return MFX_ERR_UNSUPPORTED; + } + i++; + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-r")) || 0 == msdk_strcmp(strInput[i], MSDK_STRING("-render"))) + { + pParams->bRendering = true; + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-a")) || 0 == msdk_strcmp(strInput[i], MSDK_STRING("-asyncDepth"))) + { + msdk_opt_read(strInput[++i], pParams->asyncDepth); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-n")) || 0 == msdk_strcmp(strInput[i], MSDK_STRING("-numFramesToProcess"))) + { + msdk_opt_read(strInput[++i], pParams->nFramesToProceed); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-ng")) || 0 == msdk_strcmp(strInput[i], MSDK_STRING("-noGamma"))) + { + pParams->bGamma = false; + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-offset"))) + { + + if (i + 6>= nArgNum) + { + PrintHelp(strInput[0], MSDK_STRING("Not enough parameters for offset key")); + return MFX_ERR_UNSUPPORTED; + } + pParams->offset = true; + + msdk_opt_read(strInput[++i], pParams->pre[0]); + msdk_opt_read(strInput[++i], pParams->pre[1]); + msdk_opt_read(strInput[++i], pParams->pre[2]); + msdk_opt_read(strInput[++i], pParams->post[0]); + msdk_opt_read(strInput[++i], pParams->post[1]); + msdk_opt_read(strInput[++i], pParams->post[2]); + + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-3DLUT_gamma"))) + { + pParams->b3DLUTGamma = false; + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-bdn")) || 0 == msdk_strcmp(strInput[i], MSDK_STRING("-bayerDenoise"))) + { + if(i + 1 >= nArgNum) + { + PrintHelp(strInput[0], MSDK_STRING("Not enough parameters for -bdn key")); + return MFX_ERR_UNSUPPORTED; + } + pParams->bBayerDenoise = true; + msdk_opt_read(strInput[++i], pParams->denoiseThreshold); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-bbl")) || 0 == msdk_strcmp(strInput[i], MSDK_STRING("-bayerBlackLevel"))) + { + if(i + 4 >= nArgNum) + { + PrintHelp(strInput[0], MSDK_STRING("Not enough parameters for -bbl key")); + return MFX_ERR_UNSUPPORTED; + } + pParams->bBlackLevel = true; + msdk_opt_read(strInput[++i], pParams->black_level_B); + msdk_opt_read(strInput[++i], pParams->black_level_G0); + msdk_opt_read(strInput[++i], pParams->black_level_G1); + msdk_opt_read(strInput[++i], pParams->black_level_R); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-tcc")) || 0 == msdk_strcmp(strInput[i], MSDK_STRING("-totalColorControl"))) { + if (i + 6 >= nArgNum) + { + PrintHelp(strInput[0], MSDK_STRING("Not enough parameters for -tcc key")); + return MFX_ERR_UNSUPPORTED; + } + pParams->bTCC = true; + msdk_opt_read(strInput[++i], pParams->tcc_red); + msdk_opt_read(strInput[++i], pParams->tcc_green); + msdk_opt_read(strInput[++i], pParams->tcc_blue); + msdk_opt_read(strInput[++i], pParams->tcc_cyan); + msdk_opt_read(strInput[++i], pParams->tcc_magenta); + msdk_opt_read(strInput[++i], pParams->tcc_yellow); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-hot_pixel"))) + { + if(i + 2 >= nArgNum) + { + PrintHelp(strInput[0], MSDK_STRING("Not enough parameters for -hot_pixel key")); + return MFX_ERR_UNSUPPORTED; + } + pParams->bHP = true; + msdk_opt_read(strInput[++i], pParams->hp_diff); + msdk_opt_read(strInput[++i], pParams->hp_num); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-bwb")) || 0 == msdk_strcmp(strInput[i], MSDK_STRING("-bayerWhiteBalance"))) + { + if(i + 4 >= nArgNum) + { + PrintHelp(strInput[0], MSDK_STRING("Not enough parameters for -bwb key")); + return MFX_ERR_UNSUPPORTED; + } + pParams->bWhiteBalance = true; + msdk_opt_read(strInput[++i], pParams->white_balance_B); + msdk_opt_read(strInput[++i], pParams->white_balance_G0); + msdk_opt_read(strInput[++i], pParams->white_balance_G1); + msdk_opt_read(strInput[++i], pParams->white_balance_R); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-lens")) ) + { + if(i + 4 >= nArgNum) + { + PrintHelp(strInput[0], MSDK_STRING("Not enough parameters for -lens key")); + return MFX_ERR_UNSUPPORTED; + } + pParams->bLens = true; + msdk_opt_read(strInput[++i], pParams->lens_aR); + msdk_opt_read(strInput[++i], pParams->lens_bR); + msdk_opt_read(strInput[++i], pParams->lens_cR); + msdk_opt_read(strInput[++i], pParams->lens_dR); + pParams->lens_aB = pParams->lens_aG = pParams->lens_aR; + pParams->lens_bB = pParams->lens_bG = pParams->lens_bR; + pParams->lens_cB = pParams->lens_cG = pParams->lens_cR; + pParams->lens_dB = pParams->lens_dG = pParams->lens_dR; + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-chroma_aberration")) ) + { + if(i + 12 >= nArgNum) + { + PrintHelp(strInput[0], MSDK_STRING("Not enough parameters for -chroma_aberration key")); + return MFX_ERR_UNSUPPORTED; + } + pParams->bLens = true; + msdk_opt_read(strInput[++i], pParams->lens_aR); + msdk_opt_read(strInput[++i], pParams->lens_bR); + msdk_opt_read(strInput[++i], pParams->lens_cR); + msdk_opt_read(strInput[++i], pParams->lens_dR); + msdk_opt_read(strInput[++i], pParams->lens_aB); + msdk_opt_read(strInput[++i], pParams->lens_bB); + msdk_opt_read(strInput[++i], pParams->lens_cB); + msdk_opt_read(strInput[++i], pParams->lens_dB); + msdk_opt_read(strInput[++i], pParams->lens_aG); + msdk_opt_read(strInput[++i], pParams->lens_bG); + msdk_opt_read(strInput[++i], pParams->lens_cG); + msdk_opt_read(strInput[++i], pParams->lens_dG); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-ccm"))) + { + if(i + 9 >= nArgNum) + { + PrintHelp(strInput[0], MSDK_STRING("Not enough parameters for -ccm key.")); + return MFX_ERR_UNSUPPORTED; + } + pParams->bCCM = true; + for(int k = 0; k < 3; k++) + for (int z = 0; z < 3; z++) + msdk_opt_read(strInput[++i], pParams->CCM[k][z]); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-gamma_points"))) + { + if(i + 64 >= nArgNum) + { + PrintHelp(strInput[0], MSDK_STRING("There should be 64 points provided.")); + return MFX_ERR_UNSUPPORTED; + } + for(int k = 0; k < 64; k++) + msdk_opt_read(strInput[++i], pParams->gamma_point[k]); + + pParams->bExternalGammaLUT = true; + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-gamma_corrected"))) + { + if(i + 64 >= nArgNum) + { + PrintHelp(strInput[0], MSDK_STRING("There should be 64 points provided.")); + return MFX_ERR_UNSUPPORTED; + } + for(int k = 0; k < 64; k++) + msdk_opt_read(strInput[++i], pParams->gamma_corrected[k]); + + pParams->bExternalGammaLUT = true; + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-pd")) || 0 == msdk_strcmp(strInput[i], MSDK_STRING("-padding"))) + { + pParams->bDoPadding = true; + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-vignette"))) + { + pParams->bVignette = true; + msdk_strcopy(pParams->strVignetteMaskFile, strInput[++i]); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-i"))) + { + msdk_strcopy(pParams->strSrcFile, strInput[++i]); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-o"))) + { + msdk_strcopy(pParams->strDstFile, strInput[++i]); + pParams->bOutput = true; + if (i + 1 < nArgNum) { + int n; + if (msdk_opt_read(strInput[++i], n) == MFX_ERR_NONE) { + pParams->maxNumBmpFiles = n; + } + } + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-f")) || 0 == msdk_strcmp(strInput[i], MSDK_STRING("-format"))) + { + i++; + if (0 == msdk_strcmp(strInput[i], MSDK_STRING("bggr"))) + pParams->inputType = MFX_CAM_BAYER_BGGR; + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("rggb"))) + pParams->inputType = MFX_CAM_BAYER_RGGB; + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("grbg"))) + pParams->inputType = MFX_CAM_BAYER_GRBG; + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("gbrg"))) + pParams->inputType = MFX_CAM_BAYER_GBRG; + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("argb16"))) + pParams->inputType = MFX_FOURCC_ARGB16; + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("abgr16"))) + pParams->inputType = MFX_FOURCC_ABGR16; + else + { + PrintHelp(strInput[0], MSDK_STRING("Format %s is unknown.")); + return MFX_ERR_UNSUPPORTED; + } + + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-b")) || 0 == msdk_strcmp(strInput[i], MSDK_STRING("-bitDepth"))) + { + msdk_opt_read(strInput[++i], pParams->bitDepth); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-of")) || 0 == msdk_strcmp(strInput[i], MSDK_STRING("-outFormat"))) + { + i++; + if (0 == msdk_strcmp(strInput[i], MSDK_STRING("argb16")) || 0 == msdk_strcmp(strInput[i], MSDK_STRING("16"))) + pParams->frameInfo[VPP_OUT].FourCC = MFX_FOURCC_ARGB16; + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("abgr16"))) + pParams->frameInfo[VPP_OUT].FourCC = MFX_FOURCC_ABGR16; +#if MFX_VERSION >= 1023 + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("nv12"))) { + pParams->frameInfo[VPP_OUT].FourCC = MFX_FOURCC_NV12; + pParams->bRGBToYUV = true; + } +#endif + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("rgb4"))) + pParams->frameInfo[VPP_OUT].FourCC = MFX_FOURCC_RGB4; + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-w")) || 0 == msdk_strcmp(strInput[i], MSDK_STRING("-width"))) + { + msdk_opt_read(strInput[++i], pParams->frameInfo[VPP_IN].nWidth); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-3dlut"))) + { + pParams->b3DLUT = true; + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-h")) || 0 == msdk_strcmp(strInput[i], MSDK_STRING("-height"))) + { + msdk_opt_read(strInput[++i], pParams->frameInfo[VPP_IN].nHeight); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-cropW"))) + { + msdk_opt_read(strInput[++i], pParams->frameInfo[VPP_IN].CropW); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-cropH"))) + { + msdk_opt_read(strInput[++i], pParams->frameInfo[VPP_IN].CropH); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-cropX"))) + { + msdk_opt_read(strInput[++i], pParams->frameInfo[VPP_IN].CropX); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-cropY"))) + { + msdk_opt_read(strInput[++i], pParams->frameInfo[VPP_IN].CropY); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-alpha"))) + { + msdk_opt_read(strInput[++i], pParams->alphaValue); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-reset"))) + { + resPar.inputType = pParams->inputType; + msdk_strcopy(resPar.strSrcFile, pParams->strSrcFile); + msdk_strcopy(resPar.strDstFile, pParams->strDstFile); + resPar.width = pParams->frameInfo[VPP_IN].nWidth; + resPar.height = pParams->frameInfo[VPP_IN].nHeight; + resPar.bHP = pParams->bHP; + resPar.hp_diff = pParams->hp_diff; + resPar.hp_num = pParams->hp_num; + + resPar.bBlackLevel = pParams->bBlackLevel; + resPar.black_level_B = pParams->black_level_B; + resPar.black_level_G0 = pParams->black_level_G0; + resPar.black_level_G1 = pParams->black_level_G1; + resPar.black_level_R = pParams->black_level_R; + i++; + for (;i < nArgNum; i++) + { + if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-f"))) + { + i++; + if (0 == msdk_strcmp(strInput[i], MSDK_STRING("bggr"))) + resPar.inputType = MFX_CAM_BAYER_BGGR; + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("rggb"))) + resPar.inputType = MFX_CAM_BAYER_RGGB; + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("grbg"))) + resPar.inputType = MFX_CAM_BAYER_GRBG; + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("gbrg"))) + resPar.inputType = MFX_CAM_BAYER_GBRG; + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-bbl"))) + { + if(i + 4 >= nArgNum) + { + PrintHelp(strInput[0], MSDK_STRING("Not enough parameters for -bbl key")); + return MFX_ERR_UNSUPPORTED; + } + resPar.bBlackLevel = true; + msdk_opt_read(strInput[++i], resPar.black_level_B); + msdk_opt_read(strInput[++i], resPar.black_level_G0); + msdk_opt_read(strInput[++i], resPar.black_level_G1); + msdk_opt_read(strInput[++i], resPar.black_level_R); + } else if(0 == msdk_strcmp(strInput[i], MSDK_STRING("-tcc")) || 0 == msdk_strcmp(strInput[i], MSDK_STRING("-totalColorControl"))) { + if (i + 6 >= nArgNum) + { + PrintHelp(strInput[0], MSDK_STRING("Not enough parameters for -tcc key")); + return MFX_ERR_UNSUPPORTED; + } + pParams->bTCC = true; + msdk_opt_read(strInput[++i], pParams->tcc_red); + msdk_opt_read(strInput[++i], pParams->tcc_green); + msdk_opt_read(strInput[++i], pParams->tcc_blue); + msdk_opt_read(strInput[++i], pParams->tcc_cyan); + msdk_opt_read(strInput[++i], pParams->tcc_magenta); + msdk_opt_read(strInput[++i], pParams->tcc_yellow); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-hot_pixel"))) + { + if(i + 2 >= nArgNum) + { + PrintHelp(strInput[0], MSDK_STRING("Not enough parameters for -hot_pixel key")); + return MFX_ERR_UNSUPPORTED; + } + resPar.bHP = true; + msdk_opt_read(strInput[++i], resPar.hp_diff); + msdk_opt_read(strInput[++i], resPar.hp_num); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-bdn"))) + { + if(i + 1 >= nArgNum) + { + PrintHelp(strInput[0], MSDK_STRING("Not enough parameters for -bdn key")); + return MFX_ERR_UNSUPPORTED; + } + resPar.bDenoise = true; + msdk_opt_read(strInput[++i], resPar.denoiseThreshold); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-bwb"))) + { + if(i + 4 >= nArgNum) + { + PrintHelp(strInput[0], MSDK_STRING("Not enough parameters for -bwb key")); + return MFX_ERR_UNSUPPORTED; + } + resPar.bWhiteBalance = true; + msdk_opt_read(strInput[++i], resPar.white_balance_B); + msdk_opt_read(strInput[++i], resPar.white_balance_G0); + msdk_opt_read(strInput[++i], resPar.white_balance_G1); + msdk_opt_read(strInput[++i], resPar.white_balance_R); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-ccm"))) + { + if(i + 9 >= nArgNum) + { + PrintHelp(strInput[0], MSDK_STRING("Not enough parameters for -ccm key.")); + return MFX_ERR_UNSUPPORTED; + } + resPar.bCCM = true; + for(int k = 0; k < 3; k++) + for (int z = 0; z < 3; z++) + msdk_opt_read(strInput[++i], resPar.CCM[k][z]); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-w"))) + { + msdk_opt_read(strInput[++i], resPar.width); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-h"))) + { + msdk_opt_read(strInput[++i], resPar.height); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-cropW"))) + { + msdk_opt_read(strInput[++i], resPar.cropW); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-cropH"))) + { + msdk_opt_read(strInput[++i], resPar.cropH); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-cropX"))) + { + msdk_opt_read(strInput[++i], resPar.cropX); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-cropY"))) + { + msdk_opt_read(strInput[++i], resPar.cropY); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-vignette"))) + { + resPar.bVignette = true; + msdk_strcopy(resPar.strVignetteMaskFile, strInput[++i]); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-i"))) + { + msdk_strcopy(resPar.strSrcFile, strInput[++i]); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-o"))) + { + msdk_strcopy(resPar.strDstFile, strInput[++i]); + } + else + { + i--; + break; + } + } + pParams->resetParams.push_back(resPar); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-resetInterval"))) + { + msdk_opt_read(strInput[++i], pParams->resetInterval); + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-?"))) + { + PrintHelp(strInput[0], NULL); + return MFX_ERR_UNSUPPORTED; + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-wall"))) + { + if(i + 7 >= nArgNum) + { + PrintHelp(strInput[0], MSDK_STRING("Not enough parameters for -wall key")); + return MFX_ERR_UNSUPPORTED; + } + pParams->bRendering = true; + + msdk_opt_read(strInput[++i], pParams->nWallW); + msdk_opt_read(strInput[++i], pParams->nWallH); + msdk_opt_read(strInput[++i], pParams->nWallCell); + msdk_opt_read(strInput[++i], pParams->nWallMonitor); + msdk_opt_read(strInput[++i], pParams->nWallFPS); + + int nTitle; + msdk_opt_read(strInput[++i], nTitle); + + pParams->bWallNoTitle = 0 == nTitle; + + msdk_opt_read(strInput[++i], pParams->nWallTimeout); + } + else // 1-character options + { + std::basic_stringstream stream; + stream << MSDK_STRING("Unknown option: ") << strInput[i]; + PrintHelp(strInput[0], stream.str().c_str()); + return MFX_ERR_UNSUPPORTED; + } + } + + if (0 == msdk_strlen(pParams->strSrcFile)) + { + PrintHelp(strInput[0], MSDK_STRING("Source file name not found")); + return MFX_ERR_UNSUPPORTED; + } + + if (0 == msdk_strlen(pParams->strDstFile)) + { + pParams->bOutput = false; + } + + return MFX_ERR_NONE; +} + +#if defined(_WIN32) || defined(_WIN64) +int _tmain(int argc, TCHAR *argv[]) +#else +int main(int argc, char *argv[]) +#endif +{ + sInputParams Params; // input parameters from command line + CCameraPipeline Pipeline; // pipeline for decoding, includes input file reader, decoder and output file writer + + mfxStatus sts = MFX_ERR_NONE; // return value check + + sts = ParseInputString(argv, (mfxU8)argc, &Params); + MSDK_CHECK_PARSE_RESULT(sts, MFX_ERR_NONE, 1); + + sts = Pipeline.Init(&Params); + MSDK_CHECK_STATUS(sts, "Pipeline.Init failed"); + + // print stream info + Pipeline.PrintInfo(); + + msdk_printf(MSDK_STRING("Camera pipe started\n")); +#if defined(_WIN32) || defined(_WIN64) + LARGE_INTEGER timeBegin, timeEnd, m_Freq; + QueryPerformanceFrequency(&m_Freq); + + QueryPerformanceCounter(&timeBegin); +#endif + int resetNum = 0; + for (;;) { + sts = Pipeline.Run(); + if (MFX_WRN_VIDEO_PARAM_CHANGED == sts) { + sInputParams *pParams = &Params; + if (resetNum >= (int)Params.resetParams.size()) + break; + msdk_strcopy(Params.strSrcFile, Params.resetParams[resetNum].strSrcFile); + msdk_strcopy(pParams->strDstFile, Params.resetParams[resetNum].strDstFile); + pParams->frameInfo[VPP_IN].nWidth = (mfxU16)Params.resetParams[resetNum].width; + pParams->frameInfo[VPP_IN].nHeight = (mfxU16)Params.resetParams[resetNum].height; + pParams->frameInfo[VPP_IN].CropW = pParams->frameInfo[VPP_IN].nWidth; + if ( Params.resetParams[resetNum].cropW ) + pParams->frameInfo[VPP_IN].CropW = Params.resetParams[resetNum].cropW; + pParams->frameInfo[VPP_IN].CropH = pParams->frameInfo[VPP_IN].nHeight; + if ( Params.resetParams[resetNum].cropH ) + pParams->frameInfo[VPP_IN].CropH = Params.resetParams[resetNum].cropH; + pParams->frameInfo[VPP_IN].CropX = align(Params.resetParams[resetNum].cropX); + pParams->frameInfo[VPP_IN].CropY = Params.resetParams[resetNum].cropY; + + if ( ! pParams->frameInfo[VPP_IN].CropW ) + { + pParams->frameInfo[VPP_IN].CropW = pParams->frameInfo[VPP_IN].nWidth; + } + + if ( ! pParams->frameInfo[VPP_IN].CropH ) + { + pParams->frameInfo[VPP_IN].CropH = pParams->frameInfo[VPP_IN].nHeight; + } + + pParams->frameInfo[VPP_OUT].nWidth = pParams->frameInfo[VPP_IN].CropW; + pParams->frameInfo[VPP_OUT].nHeight = pParams->frameInfo[VPP_IN].CropH; + pParams->frameInfo[VPP_OUT].CropW = pParams->frameInfo[VPP_IN].CropW; + pParams->frameInfo[VPP_OUT].CropH = pParams->frameInfo[VPP_IN].CropH; + + pParams->inputType = Params.resetParams[resetNum].inputType; + pParams->bHP = Params.resetParams[resetNum].bHP; + pParams->hp_diff = Params.resetParams[resetNum].hp_diff; + pParams->hp_num = Params.resetParams[resetNum].hp_num; + + pParams->bBayerDenoise = Params.resetParams[resetNum].bDenoise; + pParams->denoiseThreshold = Params.resetParams[resetNum].denoiseThreshold; + + pParams->bBlackLevel = Params.resetParams[resetNum].bBlackLevel; + pParams->black_level_B = Params.resetParams[resetNum].black_level_B; + pParams->black_level_G0= Params.resetParams[resetNum].black_level_G0; + pParams->black_level_G1= Params.resetParams[resetNum].black_level_G1; + pParams->black_level_R = Params.resetParams[resetNum].black_level_R; + pParams->bWhiteBalance = Params.resetParams[resetNum].bWhiteBalance; + pParams->white_balance_B = Params.resetParams[resetNum].white_balance_B; + pParams->white_balance_G0= Params.resetParams[resetNum].white_balance_G0; + pParams->white_balance_G1= Params.resetParams[resetNum].white_balance_G1; + pParams->white_balance_R = Params.resetParams[resetNum].white_balance_R; + pParams->tcc_red = Params.resetParams[resetNum].tcc_red; + pParams->tcc_blue = Params.resetParams[resetNum].tcc_blue; + pParams->tcc_green = Params.resetParams[resetNum].tcc_green; + pParams->tcc_cyan = Params.resetParams[resetNum].tcc_cyan; + pParams->tcc_magenta = Params.resetParams[resetNum].tcc_magenta; + pParams->tcc_yellow = Params.resetParams[resetNum].tcc_yellow; + pParams->bCCM = Params.resetParams[resetNum].bCCM; + for (int k = 0; k < 3; k++) + for (int z = 0; z < 3; z++) + pParams->CCM[k][z] = Params.resetParams[resetNum].CCM[k][z]; + + //pParams->frameInfo[VPP_OUT].FourCC = MFX_FOURCC_ARGB16; + //pParams->memTypeIn = pParams->memTypeOut = SYSTEM_MEMORY; + + sts = Pipeline.Reset(&Params); + if (sts == MFX_ERR_INCOMPATIBLE_VIDEO_PARAM) + { + Pipeline.Close(); + sts = Pipeline.Init(&Params); + } + if (sts != MFX_ERR_NONE) + break; + resetNum++; + } else + break; + } + +#if defined(_WIN32) || defined(_WIN64) + QueryPerformanceCounter(&timeEnd); + + + double time = ((double)timeEnd.QuadPart - (double)timeBegin.QuadPart)/ (double)m_Freq.QuadPart; + + int frames = Pipeline.GetNumberProcessedFrames(); + _tprintf(_T("Total frames %d \n"), frames); + _tprintf(_T("Total time %.2lf sec\n"), time); + _tprintf(_T("Total FPS %.2lf fps\n"), frames/time); +#endif + //Pipeline.Close(); + + if(MFX_ERR_ABORTED != sts) + MSDK_CHECK_STATUS(sts, "Unexpected error!!"); + + msdk_printf(MSDK_STRING("\nCamera pipe finished\n")); + + return 0; +} diff --git a/samples/sample_camera/src/sample_camera_utils.cpp b/samples/sample_camera/src/sample_camera_utils.cpp new file mode 100644 index 000000000..5a26f173f --- /dev/null +++ b/samples/sample_camera/src/sample_camera_utils.cpp @@ -0,0 +1,1016 @@ +/******************************************************************************\ +Copyright (c) 2005-2018, Intel Corporation +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +This sample was distributed or derived from the Intel's Media Samples package. +The original version of this sample may be obtained from https://software.intel.com/en-us/intel-media-server-studio +or https://software.intel.com/en-us/media-client-solutions-support. +\**********************************************************************************/ + +#include + +#include "sample_camera_utils.h" +#include "sysmem_allocator.h" + +#ifdef D3D_SURFACES_SUPPORT +#include "d3d_allocator.h" +#endif + +/* ******************************************************************* */ + +msdk_char* FourCC2Str( mfxU32 FourCC ) +{ + msdk_char* strFourCC = MSDK_STRING("YV12");//default + + switch ( FourCC ) + { + case MFX_FOURCC_NV12: + strFourCC = MSDK_STRING("NV12"); + break; + + case MFX_FOURCC_YV12: + strFourCC = MSDK_STRING("YV12"); + break; + + case MFX_FOURCC_YUY2: + strFourCC = MSDK_STRING("YUY2"); + break; + + case MFX_FOURCC_RGB3: + strFourCC = MSDK_STRING("RGB3"); + break; + + case MFX_FOURCC_RGB4: + strFourCC = MSDK_STRING("RGB4"); + break; + + case MFX_FOURCC_P010: + strFourCC = MSDK_STRING("P010"); + break; + + case MFX_FOURCC_R16: + strFourCC = MSDK_STRING("R16"); + break; + } + + return strFourCC; +} // msdk_char* FourCC2Str( mfxU32 FourCC ) + +/* ******************************************************************* */ + +#ifdef D3D_SURFACES_SUPPORT +mfxStatus CreateDeviceManager(IDirect3DDeviceManager9** ppManager, mfxU32 nAdapterNum) +{ + MSDK_CHECK_POINTER(ppManager, MFX_ERR_NULL_PTR); + + IDirect3D9Ex* d3d; + Direct3DCreate9Ex(D3D_SDK_VERSION, &d3d); + + if (!d3d) + { + return MFX_ERR_NULL_PTR; + } + + POINT point = {0, 0}; + HWND window = WindowFromPoint(point); + + D3DPRESENT_PARAMETERS d3dParams; + memset(&d3dParams, 0, sizeof(d3dParams)); + d3dParams.Windowed = TRUE; + d3dParams.hDeviceWindow = window; + d3dParams.SwapEffect = D3DSWAPEFFECT_DISCARD; + d3dParams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; + d3dParams.Flags = D3DPRESENTFLAG_VIDEO; + d3dParams.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; + d3dParams.PresentationInterval = D3DPRESENT_INTERVAL_ONE; + d3dParams.BackBufferCount = 1; + d3dParams.BackBufferFormat = D3DFMT_X8R8G8B8; + d3dParams.BackBufferWidth = 0; + d3dParams.BackBufferHeight = 0; + + CComPtr d3dDevice = 0; + HRESULT hr = d3d->CreateDeviceEx( + nAdapterNum, + D3DDEVTYPE_HAL, + window, + D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED | D3DCREATE_FPU_PRESERVE, + &d3dParams, + NULL, + &d3dDevice); + + if (FAILED(hr) || !d3dDevice) + { + return MFX_ERR_NULL_PTR; + } + + UINT resetToken = 0; + CComPtr d3dDeviceManager = 0; + hr = DXVA2CreateDirect3DDeviceManager9(&resetToken, &d3dDeviceManager); + + if (FAILED(hr) || !d3dDeviceManager) + { + return MFX_ERR_NULL_PTR; + } + + hr = d3dDeviceManager->ResetDevice(d3dDevice, resetToken); + if (FAILED(hr)) + { + return MFX_ERR_UNDEFINED_BEHAVIOR; + } + + *ppManager = d3dDeviceManager.Detach(); + + if (NULL == *ppManager) + { + return MFX_ERR_NULL_PTR; + } + + return MFX_ERR_NONE; + +} // mfxStatus CreateDeviceManager(IDirect3DDeviceManager9** ppManager) +#endif + + +/* ******************************************************************* */ + +mfxStatus InitSurfaces(sMemoryAllocator* pAllocator, mfxFrameAllocRequest* pRequest, mfxFrameInfo* pInfo, bool bUsedAsExternalAllocator) +{ + mfxStatus sts = MFX_ERR_NONE; + mfxU16 nFrames, i; + + sts = pAllocator->pMfxAllocator->Alloc(pAllocator->pMfxAllocator->pthis, pRequest, pAllocator->response); + MSDK_CHECK_STATUS(sts, "pAllocator->pMfxAllocator->Alloc failed"); + + nFrames = pAllocator->response->NumFrameActual; + *(pAllocator->pSurfaces) = new mfxFrameSurface1 [nFrames]; + + for (i = 0; i < nFrames; i++) + { + memset(&(pAllocator->pSurfaces[0][i]), 0, sizeof(mfxFrameSurface1)); + pAllocator->pSurfaces[0][i].Info = *pInfo; + + if (bUsedAsExternalAllocator) + { + pAllocator->pSurfaces[0][i].Data.MemId = pAllocator->response->mids[i]; + } + else + { + sts = pAllocator->pMfxAllocator->Lock(pAllocator->pMfxAllocator->pthis, + pAllocator->response->mids[i], + &(pAllocator->pSurfaces[0][i].Data)); + MSDK_CHECK_STATUS(sts, "pAllocator->pMfxAllocator->Lock failed"); + } + } + + return sts; + +} // mfxStatus InitSurfaces(...) + +/* ******************************************************************* */ + +mfxStatus CARGB16VideoReader::Init(sInputParams* pParams) +{ + Close(); + +#if defined (WIN32) || defined(WIN64) + WIN32_FILE_ATTRIBUTE_DATA ffi; + if ( GetFileAttributesEx(pParams->strSrcFile, GetFileExInfoStandard, &ffi) ) + { + m_bSingleFileMode = true; + } +#endif + msdk_strcopy(m_FileNameBase, pParams->strSrcFile); + m_FileNum = 0; + + m_Width = pParams->frameInfo->nWidth; + m_Height = pParams->frameInfo->nHeight; + + return MFX_ERR_NONE; +} + +CARGB16VideoReader::~CARGB16VideoReader() +{ + Close(); +} + +void CARGB16VideoReader::Close() +{ + if (m_fSrc != 0) + { + fclose(m_fSrc); + m_fSrc = 0; + } +} + +mfxStatus CARGB16VideoReader::LoadNextFrame(mfxFrameData* pData, mfxFrameInfo* pInfo, mfxU32 type) +{ + return ( m_bSingleFileMode ) ? LoadNextFrameSingle(pData, pInfo, type) : LoadNextFrameSequential(pData, pInfo, type); +} + +mfxStatus CARGB16VideoReader::LoadNextFrameSingle(mfxFrameData* pData, mfxFrameInfo* pInfo, mfxU32) +{ + MSDK_CHECK_POINTER(pData, MFX_ERR_NOT_INITIALIZED); + MSDK_CHECK_POINTER(pInfo, MFX_ERR_NOT_INITIALIZED); + + if ( m_FileNum ) + { + // File has been read already + return MFX_ERR_MORE_DATA; + } + + MSDK_FOPEN(m_fSrc, m_FileNameBase, MSDK_STRING("rb")); + MSDK_CHECK_POINTER(m_fSrc, MFX_ERR_MORE_DATA); + + mfxI32 w, h, i, pitch; + mfxI32 nBytesRead; + mfxU16 *ptr = MSDK_MIN(MSDK_MIN(pData->Y16,pData->V16), pData->U16); + + w = m_Width; + h = m_Height; + int shift = 16 - pInfo->BitDepthLuma; + pitch = (mfxI32)(pData->Pitch >> 1); + for (i = 0; i < h; i++) + { +#if defined(_WIN32) || defined(_WIN64) + nBytesRead = (mfxI32)fread_s(ptr + i * pitch, pData->Pitch, sizeof(mfxU16), 4*w, m_fSrc); +#else + nBytesRead = (mfxI32)fread(ptr + i * pitch, sizeof(mfxU16), 4*w, m_fSrc); +#endif + IOSTREAM_CHECK_NOT_EQUAL(nBytesRead, 4*w, MFX_ERR_MORE_DATA); + + for (int j = 0; j < 4*w; j++) + ptr[i*pitch + j] = ptr[i*pitch + j] << shift; + } + pData->FrameOrder = m_FileNum; + m_FileNum++; + fclose(m_fSrc); + + return MFX_ERR_NONE; +} + +mfxStatus CARGB16VideoReader::LoadNextFrameSequential(mfxFrameData* pData, mfxFrameInfo* pInfo, mfxU32 type) +{ + MSDK_CHECK_POINTER(pData, MFX_ERR_NOT_INITIALIZED); + MSDK_CHECK_POINTER(pInfo, MFX_ERR_NOT_INITIALIZED); + + int filenameIndx = m_FileNum; + + msdk_char fname[MSDK_MAX_FILENAME_LEN]; + + const msdk_char *pExt; + switch (type) { + case MFX_FOURCC_ARGB16: + pExt = MSDK_STRING("argb16"); + break; + case MFX_FOURCC_ABGR16: + pExt = MSDK_STRING("abrg16"); + break; + default: + return MFX_ERR_UNSUPPORTED; + } + +#if defined(_WIN32) || defined(_WIN64) + msdk_sprintf(fname, MSDK_MAX_FILENAME_LEN, MSDK_STRING("%s%08d.%s"), m_FileNameBase, filenameIndx, pExt); +#else + msdk_sprintf(fname, MSDK_STRING("%s%08d.%s"), m_FileNameBase, filenameIndx, pExt); +#endif + + MSDK_FOPEN(m_fSrc, fname, MSDK_STRING("rb")); + MSDK_CHECK_POINTER(m_fSrc, MFX_ERR_MORE_DATA); + + mfxI32 w, h, i, pitch; + mfxI32 nBytesRead; + mfxU16 *ptr = MSDK_MIN(MSDK_MIN(pData->Y16,pData->V16), pData->U16); + + w = m_Width; + h = m_Height; + + pitch = (mfxI32)(pData->Pitch >> 1); + + for (i = 0; i < h; i++) + { +#if defined(_WIN32) || defined(_WIN64) + nBytesRead = (mfxI32)fread_s(ptr + i * pitch, pData->Pitch, sizeof(mfxU16), 4*w, m_fSrc); +#else + nBytesRead = (mfxI32)fread(ptr + i * pitch, sizeof(mfxU16), 4*w, m_fSrc); +#endif + IOSTREAM_CHECK_NOT_EQUAL(nBytesRead, 4*w, MFX_ERR_MORE_DATA); + } + + pData->FrameOrder = m_FileNum; + m_FileNum++; + + fclose(m_fSrc); + + return MFX_ERR_NONE; +} + +mfxStatus CBufferedVideoReader::LoadNextFrame(mfxFrameData* pData, mfxFrameInfo* pInfo, mfxU32 bayerType) +{ + bayerType; pInfo; + + pData->Y16 = buffer[nCurrentFrame]; + pData->FrameOrder = nCurrentFrame; + nCurrentFrame++; + return MFX_ERR_NONE; +} + +mfxStatus CBufferedVideoReader::Init(sInputParams *pParams) { + //Valid only for Bayer sequence input + msdk_strcopy(m_FileNameBase, pParams->strSrcFile); + m_FileNum = 0; + nFramesToProceed = pParams->nFramesToProceed; + m_DoPadding = pParams->bDoPadding; + printf("Buffering "); + while (m_FileNum < pParams->nFramesToProceed) { + int filenameIndx = m_FileNum; + msdk_char fname[MSDK_MAX_FILENAME_LEN]; + const msdk_char *pExt; + switch (pParams->inputType) { + case MFX_CAM_BAYER_GRBG: + pExt = MSDK_STRING("gr16"); + break; + case MFX_CAM_BAYER_GBRG: + pExt = MSDK_STRING("gb16"); + break; + case MFX_CAM_BAYER_BGGR: + pExt = MSDK_STRING("bg16"); + break; + case MFX_CAM_BAYER_RGGB: + default: + pExt = MSDK_STRING("rg16"); + break; + } + +#if defined(_WIN32) || defined(_WIN64) + msdk_sprintf(fname, MSDK_MAX_FILENAME_LEN, MSDK_STRING("%s%08d.%s"), m_FileNameBase, filenameIndx, pExt); +#else + msdk_sprintf(fname, MSDK_STRING("%s%08d.%s"), m_FileNameBase, filenameIndx, pExt); +#endif + MSDK_FOPEN(m_fSrc, fname, MSDK_STRING("rb")); + + MSDK_CHECK_POINTER(m_fSrc, MFX_ERR_MORE_DATA); + + mfxI32 w, h, i, pitch; + mfxI32 nBytesRead; + + w = m_Width = pParams->frameInfo->nWidth; + h = m_Height = pParams->frameInfo->nHeight; + + //buffer for FOURCC_R16 is w*h*2 + mfxU16* data = new mfxU16[w * h * 2]; + mfxU16 *ptr = data; + + pitch = w; + + if (!m_DoPadding) + { + for (i = 0; i < h; i++) + { + nBytesRead = (mfxI32)fread(ptr + i * pitch, sizeof(mfxU16), w, m_fSrc); + + IOSTREAM_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA); + } + } + buffer.push_back(data); + printf("."); + m_FileNum++; + + fclose(m_fSrc); + } + printf("\n"); + return MFX_ERR_NONE; +} + +CBufferedVideoReader::~CBufferedVideoReader() { + Close(); +} + +void CBufferedVideoReader::Close() { + if (m_fSrc != 0) + { + fclose(m_fSrc); + m_fSrc = 0; + } + while (buffer.size() > 0) { + if (buffer[0]) { + delete buffer[0]; + buffer.erase(buffer.begin()); + } + } +} + +CRawVideoReader::CRawVideoReader() +{ + m_fSrc = 0; + m_bSingleFileMode = false; + m_Height = m_Width = 0; + m_FileNum = 0; + m_DoPadding = false; +#ifdef CONVERT_TO_LSB + m_pPaddingBuffer = 0; +#endif +} // CRawVideoReader::CRawVideoReader() + +mfxStatus CRawVideoReader::Init(sInputParams* pParams) +{ + Close(); + +#if defined (WIN32) || defined(WIN64) + WIN32_FILE_ATTRIBUTE_DATA ffi; + if ( GetFileAttributesEx(pParams->strSrcFile, GetFileExInfoStandard, &ffi) ) + { + m_bSingleFileMode = true; + } +#endif + msdk_strcopy(m_FileNameBase, pParams->strSrcFile); + m_FileNum = 0; + m_DoPadding = pParams->bDoPadding; + +#ifdef CONVERT_TO_LSB + MSDK_SAFE_DELETE(m_pPaddingBuffer); + m_paddingBufSize = pParams->frameInfo[VPP_IN].nWidth; + m_pPaddingBuffer = new mfxU16[m_paddingBufSize]; +#endif + + m_Width = pParams->frameInfo->nWidth; + m_Height = pParams->frameInfo->nHeight; + + return MFX_ERR_NONE; + +} // mfxStatus CRawVideoReader::Init(const msdk_char *strFileName) + +CRawVideoReader::~CRawVideoReader() +{ + Close(); + +} // CRawVideoReader::~CRawVideoReader() + +void CRawVideoReader::Close() +{ + if (m_fSrc != 0) + { + fclose(m_fSrc); + m_fSrc = 0; + } + +#ifdef CONVERT_TO_LSB + MSDK_SAFE_DELETE(m_pPaddingBuffer) +#endif + +} // void CRawVideoReader::Close() + +void CRawVideoReader::SetStartFileNumber(mfxI32 fileNum) +{ + m_FileNum = fileNum; +} + +mfxStatus CRawVideoReader::LoadNextFrame(mfxFrameData* pData, mfxFrameInfo* pInfo, mfxU32 bayerType) +{ + return ( m_bSingleFileMode ) ? LoadNextFrameSingle(pData, pInfo, bayerType) : LoadNextFrameSequential(pData, pInfo, bayerType); +} + +mfxStatus CRawVideoReader::LoadNextFrameSingle(mfxFrameData* pData, mfxFrameInfo* pInfo, mfxU32) +{ + MSDK_CHECK_POINTER(pData, MFX_ERR_NOT_INITIALIZED); + MSDK_CHECK_POINTER(pInfo, MFX_ERR_NOT_INITIALIZED); + + if ( m_FileNum ) + { + // File has been read already + return MFX_ERR_MORE_DATA; + } + + MSDK_FOPEN(m_fSrc, m_FileNameBase, MSDK_STRING("rb")); + MSDK_CHECK_POINTER(m_fSrc, MFX_ERR_MORE_DATA); + + mfxI32 w, h, i, j, pitch; + mfxI32 nBytesRead; + mfxU16 *ptr = pData->Y16; + + w = m_Width; + h = m_Height; + + pitch = (mfxI32)(pData->Pitch >> 1); + +#ifdef CONVERT_TO_LSB + int shift = 16 - pInfo->BitDepthLuma; +#endif + + if (!m_DoPadding) + { + for (i = 0; i < h; i++) + { +#if defined(_WIN32) || defined(_WIN64) + nBytesRead = (mfxI32)fread_s(ptr + i * pitch, pData->Pitch, sizeof(mfxU16), w, m_fSrc); +#else + nBytesRead = (mfxI32)fread(ptr + i * pitch, sizeof(mfxU16), w, m_fSrc); +#endif + IOSTREAM_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA); + } + } + else + { + ptr = pData->Y16 + 8 + 8*pitch; + for (i = 0; i < h; i++) + { + mfxU16 *rowPtr = ptr + i * pitch; +#ifdef CONVERT_TO_LSB + nBytesRead = (mfxI32)fread_s(m_pPaddingBuffer, m_paddingBufSize*sizeof(mfxU16), sizeof(mfxU16), w, m_fSrc); + IOSTREAM_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA); + for (j = 0; j < w; j++) + rowPtr[j] = m_pPaddingBuffer[j] >> shift; +#else +#if defined(_WIN32) || defined(_WIN64) + nBytesRead = (mfxI32)fread_s(rowPtr, w*sizeof(mfxU16), sizeof(mfxU16), w, m_fSrc); +#else + nBytesRead = (mfxI32)fread(rowPtr, sizeof(mfxU16), w, m_fSrc); +#endif + IOSTREAM_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA); +#endif + for (j = 0; j < 7; j++) + { + rowPtr[-j - 1] = rowPtr[j + 1]; + rowPtr[w + j] = rowPtr[w - 2 - j]; + } + rowPtr[-8] = rowPtr[-7]; // not used + rowPtr[w + 7] = rowPtr[w + 6]; // not used + } + + for (j = 0; j < 7; j++) + { + mfxU16 *pDst = pData->Y16 + (7 - j)*pitch; + mfxU16 *pSrc = pData->Y16 + (9 + j)*pitch; + MSDK_MEMCPY(pDst, pSrc, (w + 16)*sizeof(mfxU16)); + } + MSDK_MEMCPY(pData->Y16, pData->Y16+pitch, (w + 16)*sizeof(mfxU16)); + + for (j = 0; j < 7; j++) + { + mfxU16 *pDst = pData->Y16 + (8 + h + j)*pitch; + mfxU16 *pSrc = pData->Y16 + (8 + h - 2 - j)*pitch; + MSDK_MEMCPY(pDst, pSrc, (w + 16)*sizeof(mfxU16)); + } + MSDK_MEMCPY(pData->Y16 + (15 + h)*pitch, pData->Y16 + (14 + h)*pitch, (w + 16)*sizeof(mfxU16)); + } + + pData->FrameOrder = m_FileNum; + m_FileNum++; + + fclose(m_fSrc); + + return MFX_ERR_NONE; +} + +mfxStatus CRawVideoReader::LoadNextFrameSequential(mfxFrameData* pData, mfxFrameInfo* pInfo, mfxU32 bayerType) +{ + MSDK_CHECK_POINTER(pData, MFX_ERR_NOT_INITIALIZED); + MSDK_CHECK_POINTER(pInfo, MFX_ERR_NOT_INITIALIZED); + + //if (m_FileNum == 0) + // m_FileNum++; + int filenameIndx = m_FileNum; + +#ifdef READING_LOOP + filenameIndx %= 118; + if (filenameIndx == 0) filenameIndx++; +#endif + msdk_char fname[MSDK_MAX_FILENAME_LEN]; + + const msdk_char *pExt; + + switch (bayerType) { + case MFX_CAM_BAYER_GRBG: + pExt = MSDK_STRING("gr16"); + break; + case MFX_CAM_BAYER_GBRG: + pExt = MSDK_STRING("gb16"); + break; + case MFX_CAM_BAYER_BGGR: + pExt = MSDK_STRING("bg16"); + break; + case MFX_CAM_BAYER_RGGB: + default: + pExt = MSDK_STRING("rg16"); + break; + } +#if defined(_WIN32) || defined(_WIN64) + msdk_sprintf(fname, MSDK_MAX_FILENAME_LEN, MSDK_STRING("%s%08d.%s"), m_FileNameBase, filenameIndx, pExt); +#else + msdk_sprintf(fname, MSDK_STRING("%s%08d.%s"), m_FileNameBase, filenameIndx, pExt); +#endif + //msdk_sprintf(fname, MSDK_MAX_FILENAME_LEN, MSDK_STRING("%s"), m_FileNameBase, filenameIndx); + + MSDK_FOPEN(m_fSrc, fname, MSDK_STRING("rb")); + MSDK_CHECK_POINTER(m_fSrc, MFX_ERR_MORE_DATA); + + //MSDK_FOPEN(m_fSrc, m_FileNameBase, MSDK_STRING("rb")); + //MSDK_CHECK_POINTER(m_fSrc, MFX_ERR_MORE_DATA); + + mfxI32 w, h, i, j, pitch; + mfxI32 nBytesRead; + mfxU16 *ptr = pData->Y16; + + w = m_Width; + h = m_Height; + + pitch = (mfxI32)(pData->Pitch >> 1); + +#ifdef CONVERT_TO_LSB + int shift = 16 - pInfo->BitDepthLuma; +#endif + + if (!m_DoPadding) + { + for (i = 0; i < h; i++) + { +#if defined(_WIN32) || defined(_WIN64) + nBytesRead = (mfxI32)fread_s(ptr + i * pitch, pData->Pitch, sizeof(mfxU16), w, m_fSrc); +#else + nBytesRead = (mfxI32)fread(ptr + i * pitch, sizeof(mfxU16), w, m_fSrc); +#endif + IOSTREAM_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA); + } + } + else + { + ptr = pData->Y16 + 8 + 8*pitch; + for (i = 0; i < h; i++) + { + mfxU16 *rowPtr = ptr + i * pitch; +#ifdef CONVERT_TO_LSB + nBytesRead = (mfxI32)fread_s(m_pPaddingBuffer, m_paddingBufSize*sizeof(mfxU16), sizeof(mfxU16), w, m_fSrc); + IOSTREAM_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA); + for (j = 0; j < w; j++) + rowPtr[j] = m_pPaddingBuffer[j] >> shift; +#else +#if defined(_WIN32) || defined(_WIN64) + nBytesRead = (mfxI32)fread_s(rowPtr, w*sizeof(mfxU16), sizeof(mfxU16), w, m_fSrc); +#else + nBytesRead = (mfxI32)fread(rowPtr, sizeof(mfxU16), w, m_fSrc); +#endif + IOSTREAM_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA); +#endif + for (j = 0; j < 7; j++) + { + rowPtr[-j - 1] = rowPtr[j + 1]; + rowPtr[w + j] = rowPtr[w - 2 - j]; + } + rowPtr[-8] = rowPtr[-7]; // not used + rowPtr[w + 7] = rowPtr[w + 6]; // not used + } + + for (j = 0; j < 7; j++) + { + mfxU16 *pDst = pData->Y16 + (7 - j)*pitch; + mfxU16 *pSrc = pData->Y16 + (9 + j)*pitch; + MSDK_MEMCPY(pDst, pSrc, (w + 16)*sizeof(mfxU16)); + } + MSDK_MEMCPY(pData->Y16, pData->Y16+pitch, (w + 16)*sizeof(mfxU16)); + + for (j = 0; j < 7; j++) + { + mfxU16 *pDst = pData->Y16 + (8 + h + j)*pitch; + mfxU16 *pSrc = pData->Y16 + (8 + h - 2 - j)*pitch; + MSDK_MEMCPY(pDst, pSrc, (w + 16)*sizeof(mfxU16)); + } + MSDK_MEMCPY(pData->Y16 + (15 + h)*pitch, pData->Y16 + (14 + h)*pitch, (w + 16)*sizeof(mfxU16)); + } + + pData->FrameOrder = m_FileNum; + m_FileNum++; + + fclose(m_fSrc); + + return MFX_ERR_NONE; +} + + +/* ******************************************************************* */ + +CBmpWriter::CBmpWriter() +{ + MSDK_ZERO_MEMORY(m_bih); + MSDK_ZERO_MEMORY(m_bfh); + m_maxNumFilesToCreate = -1; + m_FileNum = 0; + return; +} + +mfxStatus CBmpWriter::Init(const msdk_char *strFileNameBase, mfxU32 width, mfxU32 height, mfxI32 maxNumFiles) +{ + MSDK_CHECK_POINTER(strFileNameBase, MFX_ERR_NULL_PTR); + msdk_strcopy(m_FileNameBase, strFileNameBase); + + int buffSize = width * height * 4; //RGBA-8 + + m_bfh.bfType = 0x4D42; + m_bfh.bfSize = sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER) + buffSize; + m_bfh.bfOffBits = sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER); + + m_bih.biSize = sizeof(BITMAPINFOHEADER); + m_bih.biHeight = height ; + m_bih.biWidth = width; + m_bih.biBitCount = 32; + m_bih.biPlanes = 1; + m_bih.biCompression = BI_RGB; + + if (maxNumFiles > 0) + m_maxNumFilesToCreate = maxNumFiles; + + return MFX_ERR_NONE; +} + +#define CHECK_NOT_EQUAL(P, X, ERR) {if ((X) != (P)) {fclose(f); return ERR;}} + +mfxStatus CBmpWriter::WriteFrame(mfxFrameData* pData, const msdk_char *fileId, mfxFrameInfo* pInfo) +{ + msdk_char fname[MSDK_MAX_FILENAME_LEN]; + FILE *f; + + if (m_maxNumFilesToCreate > 0 && m_FileNum >= m_maxNumFilesToCreate) + return MFX_ERR_NONE; + + MSDK_CHECK_POINTER(pData, MFX_ERR_NOT_INITIALIZED); + MSDK_CHECK_POINTER(pInfo, MFX_ERR_NOT_INITIALIZED); + +#if defined(_WIN32) || defined(_WIN64) + if (fileId) + msdk_sprintf(fname, MSDK_MAX_FILENAME_LEN, MSDK_STRING("%s%s.bmp"), m_FileNameBase, fileId); + else + msdk_sprintf(fname, MSDK_MAX_FILENAME_LEN, MSDK_STRING("%s%d.bmp"), m_FileNameBase, m_FileNum); +#else + if (fileId) + msdk_sprintf(fname, MSDK_STRING("%s%s.bmp"), m_FileNameBase, fileId); + else + msdk_sprintf(fname, MSDK_STRING("%s%d.bmp"), m_FileNameBase, m_FileNum); +#endif + + m_FileNum++; + + MSDK_FOPEN(f, fname, MSDK_STRING("wb")); + MSDK_CHECK_POINTER(f, MFX_ERR_NULL_PTR); + + mfxU32 nbytes; + + nbytes = (mfxU32)fwrite(&m_bfh, 1, sizeof(BITMAPFILEHEADER), f); + CHECK_NOT_EQUAL(nbytes, sizeof(BITMAPFILEHEADER), MFX_ERR_UNDEFINED_BEHAVIOR); + + nbytes = (mfxU32)fwrite(&m_bih, 1, sizeof(BITMAPINFOHEADER), f); + CHECK_NOT_EQUAL(nbytes, sizeof(BITMAPINFOHEADER), MFX_ERR_UNDEFINED_BEHAVIOR); + + mfxU32 width_bytes = m_bih.biWidth * 4; + + if (pInfo->FourCC == MFX_FOURCC_ARGB16) + { + mfxU8 *pOut = new mfxU8[width_bytes]; + for (int y = m_bih.biHeight - 1; y >= 0; y--) + { + for (int x = 0; x < (int)width_bytes; x++) + { + pOut[x] = (mfxU8)(pData->V16[x + y*(pData->Pitch >> 1)] >> (pInfo->BitDepthLuma - 8)); + } + nbytes = (mfxU32)fwrite(pOut, 1, width_bytes, f); + if (nbytes != width_bytes) { + delete[] pOut; + fclose(f); + return MFX_ERR_UNDEFINED_BEHAVIOR; + } + } + delete[] pOut; + } + else + for (int y = m_bih.biHeight - 1; y >= 0; y--) + { + nbytes = (mfxU32)fwrite((mfxU8*)pData->B + y*pData->Pitch, 1, width_bytes, f); + CHECK_NOT_EQUAL(nbytes, width_bytes, MFX_ERR_UNDEFINED_BEHAVIOR); + } + + fclose(f); + + return MFX_ERR_NONE; +} + + + + +/* ******************************************************************* */ +CRawVideoWriter::CRawVideoWriter() : + m_FileNum(0) + , m_maxNumFilesToCreate(0) +{ + MSDK_ZERO_MEMORY(m_FileNameBase); +} + +mfxStatus CRawVideoWriter::Init(sInputParams* pParams) +{ + MSDK_CHECK_POINTER(pParams, MFX_ERR_NULL_PTR); + + msdk_strcopy(m_FileNameBase, pParams->strDstFile); + + if (pParams->maxNumBmpFiles > 0) + m_maxNumFilesToCreate = pParams->maxNumBmpFiles; +//#ifdef SHIFT_OUT_TO_LSB +// MSDK_SAFE_DELETE(m_pShiftBuffer); +// m_shiftBufSize = pParams->frameInfo[VPP_OUT].nWidth; +// m_pShiftBuffer = new mfxU16[m_shiftBufSize]; +//#endif + + return MFX_ERR_NONE; +} + +mfxStatus CRawVideoWriter::WriteFrameNV12(mfxFrameData* pData, const msdk_char *fileId, mfxFrameInfo* pInfo) { + msdk_char fname[MSDK_MAX_FILENAME_LEN]; + FILE *f; + mfxU32 i = 0, j = 0; + mfxU32 h = 0, cx = 0, cy = 0, cw = 0, ch = 0; + mfxU8* buf = pData->Y; + mfxU32 p = pData->Pitch; + + MSDK_CHECK_POINTER(pData, MFX_ERR_NOT_INITIALIZED); + MSDK_CHECK_POINTER(pInfo, MFX_ERR_NOT_INITIALIZED); + + #if defined(_WIN32) || defined(_WIN64) + if (fileId) + msdk_sprintf(fname, MSDK_MAX_FILENAME_LEN, MSDK_STRING("%s%s.nv12"), m_FileNameBase, fileId); + else + msdk_sprintf(fname, MSDK_MAX_FILENAME_LEN, MSDK_STRING("%s%d.nv12"), m_FileNameBase, m_FileNum); + #else + if (fileId) + msdk_sprintf(fname, MSDK_STRING("%s%s.argb16"), m_FileNameBase, fileId); + else + msdk_sprintf(fname, MSDK_STRING("%s%d.argb16"), m_FileNameBase, m_FileNum); + #endif + + m_FileNum++; + + MSDK_FOPEN(f, fname, MSDK_STRING("wb")); + MSDK_CHECK_POINTER(f, MFX_ERR_NULL_PTR); + + h = pInfo->Height; + cx = pInfo->CropX; + cy = pInfo->CropY; + cw = pInfo->CropW; + ch = pInfo->CropH; + + for (i = 0; i < ch; ++i) + { + fwrite(buf + cx + (cy + i)*p, 1, cw, f); + fflush(f); + } + buf += p*h + cx + cy*p / 2; + for (i = 0; i < ch / 2; ++i) + for (j = 1; j <= cw / 2; ++j) + { + fwrite(buf + p*i + 2 * j - 2, 1, 1, f); + fwrite(buf + p*i + 2 * j - 1, 1, 1, f); + } + + fclose(f); + + return MFX_ERR_NONE; + +} + + + +mfxStatus CRawVideoWriter::WriteFrameARGB16(mfxFrameData* pData, const msdk_char *fileId, mfxFrameInfo* pInfo) +{ + mfxU32 i, h, w, pitch; + mfxU16* ptr; + + msdk_char fname[MSDK_MAX_FILENAME_LEN]; + FILE *f; + + if (m_maxNumFilesToCreate > 0 && m_FileNum >= m_maxNumFilesToCreate) + return MFX_ERR_NONE; + + MSDK_CHECK_POINTER(pData, MFX_ERR_NOT_INITIALIZED); + MSDK_CHECK_POINTER(pInfo, MFX_ERR_NOT_INITIALIZED); + +#if defined(_WIN32) || defined(_WIN64) + if (fileId) { + if (pInfo->FourCC == MFX_FOURCC_ARGB16) + msdk_sprintf(fname, MSDK_MAX_FILENAME_LEN, MSDK_STRING("%s%s.argb16"), m_FileNameBase, fileId); + else if (pInfo->FourCC == MFX_FOURCC_ABGR16) + msdk_sprintf(fname, MSDK_MAX_FILENAME_LEN, MSDK_STRING("%s%s.abgr16"), m_FileNameBase, fileId); + } + else { + if (pInfo->FourCC == MFX_FOURCC_ARGB16) + msdk_sprintf(fname, MSDK_MAX_FILENAME_LEN, MSDK_STRING("%s%d.argb16"), m_FileNameBase, m_FileNum); + else if (pInfo->FourCC == MFX_FOURCC_ABGR16) + msdk_sprintf(fname, MSDK_MAX_FILENAME_LEN, MSDK_STRING("%s%d.abgr16"), m_FileNameBase, m_FileNum); + } +#else + if (fileId) { + if (pInfo->FourCC == MFX_FOURCC_ARGB16) + msdk_sprintf(fname, MSDK_STRING("%s%s.argb16"), m_FileNameBase, fileId); + else if (pInfo->FourCC == MFX_FOURCC_ABGR16) + msdk_sprintf(fname, MSDK_STRING("%s%s.abgr16"), m_FileNameBase, fileId); + } + else { + if (pInfo->FourCC == MFX_FOURCC_ARGB16) + msdk_sprintf(fname, MSDK_STRING("%s%d.argb16"), m_FileNameBase, m_FileNum); + else if (pInfo->FourCC == MFX_FOURCC_ABGR16) + msdk_sprintf(fname, MSDK_STRING("%s%d.abgr16"), m_FileNameBase, m_FileNum); + } +#endif + + m_FileNum++; + + MSDK_FOPEN(f, fname, MSDK_STRING("wb")); + MSDK_CHECK_POINTER(f, MFX_ERR_NULL_PTR); + + if (pInfo->CropH > 0 && pInfo->CropW > 0) + { + w = pInfo->CropW; + h = pInfo->CropH; + } + else + { + w = pInfo->Width; + h = pInfo->Height; + } + + pitch = ( pData->PitchLow + ((mfxU32)pData->PitchHigh << 16))>>1; + + ptr = MSDK_MIN(MSDK_MIN(pData->Y16,pData->V16), pData->U16) + pInfo->CropX * 4 + pInfo->CropY * pitch; +#ifdef SHIFT_OUT_TO_LSB + int shift = 16 - pInfo->BitDepthLuma; + for (i = 0; i < h; i++) + { + mfxU32 j; + for (j = 0; j < 4*w; j++) + ptr[i*pitch + j] = ptr[i*pitch + j] >> shift; + + if (fwrite(ptr + i*pitch, sizeof(mfxU16), 4*w, f) != (4*w)) + { + MSDK_CHECK_STATUS_SAFE(MFX_ERR_UNDEFINED_BEHAVIOR, "fwrite failed", fclose(f)); + } + } +#else + for (i = 0; i < h; i++) + { + MSDK_CHECK_NOT_EQUAL(fwrite(ptr + i*pitch, sizeof(mfxU16), 4*w, f), 4*w, MFX_ERR_UNDEFINED_BEHAVIOR); + } +#endif + fclose(f); + + return MFX_ERR_NONE; + +} // mfxStatus CRawVideoWriter::WriteFrame(...) + +mfxStatus GetFreeSurface(mfxFrameSurface1* pSurfacesPool, mfxU16 nPoolSize, mfxFrameSurface1** ppSurface) +{ + MSDK_CHECK_POINTER(pSurfacesPool, MFX_ERR_NULL_PTR); + MSDK_CHECK_POINTER(ppSurface, MFX_ERR_NULL_PTR); + + mfxU32 timeToSleep = 1; // milliseconds + mfxU32 numSleeps = MSDK_VPP_WAIT_INTERVAL / timeToSleep + 1; // at least 1 + + mfxU32 i = 0; + + //wait if there's no free surface + while ((MSDK_INVALID_SURF_IDX == GetFreeSurfaceIndex(pSurfacesPool, nPoolSize)) && (i < numSleeps)) + { + MSDK_SLEEP(timeToSleep); + i++; + } + + mfxU16 index = GetFreeSurfaceIndex(pSurfacesPool, nPoolSize); + + if (index < nPoolSize) + { + + *ppSurface = &(pSurfacesPool[index]); + +// (*ppSurface)->Data.Locked++; + msdk_atomic_inc16((volatile mfxU16*)&((*ppSurface)->Data.Locked)); + return MFX_ERR_NONE; + } + + return MFX_ERR_NOT_ENOUGH_BUFFER; + +} // mfxStatus GetFreeSurface(...) + + +void ReleaseSurface(mfxFrameSurface1* pSurface) +{ + if (pSurface->Data.Locked) { + msdk_atomic_dec16((volatile mfxU16*)&(pSurface->Data.Locked)); + //pSurface->Data.Locked--; + } +} + + +/* EOF */ diff --git a/samples/sample_common/include/avc_structures.h b/samples/sample_common/include/avc_structures.h index 831c2d37d..66815e099 100644 --- a/samples/sample_common/include/avc_structures.h +++ b/samples/sample_common/include/avc_structures.h @@ -20,6 +20,9 @@ or https://software.intel.com/en-us/media-client-solutions-support. #ifndef __AVC_STRUCTURES_H__ #define __AVC_STRUCTURES_H__ +#if defined(_WIN32) || defined(_WIN64) +#include +#endif #include #include #include diff --git a/samples/sample_common/include/brc_routines.h b/samples/sample_common/include/brc_routines.h index 6282af367..fa7607b72 100644 --- a/samples/sample_common/include/brc_routines.h +++ b/samples/sample_common/include/brc_routines.h @@ -18,6 +18,10 @@ or https://software.intel.com/en-us/media-client-solutions-support. \**********************************************************************************/ #include "sample_defs.h" +#ifndef MFX_VERSION +#error MFX_VERSION not defined +#endif + #if (MFX_VERSION >= 1024) #include "mfxbrc.h" diff --git a/samples/sample_common/include/current_date.h b/samples/sample_common/include/current_date.h index 4af21e54b..c3d9a2ef5 100644 --- a/samples/sample_common/include/current_date.h +++ b/samples/sample_common/include/current_date.h @@ -22,5 +22,6 @@ or https://software.intel.com/en-us/media-client-solutions-support. #define FILE_VERSION_STRING "1,0,0,0" #define FILTER_NAME_PREFIX "" #define FILTER_NAME_SUFFIX "" +#define PRODUCT_COPYRIGHT "Copyright\xa9 2003-2019 Intel Corporation" #define PRODUCT_VERSION 1,0,0,0 #define PRODUCT_VERSION_STRING "1,0,0,0" diff --git a/samples/sample_common/include/d3d11_allocator.h b/samples/sample_common/include/d3d11_allocator.h index 6975d817d..583653046 100644 --- a/samples/sample_common/include/d3d11_allocator.h +++ b/samples/sample_common/include/d3d11_allocator.h @@ -112,4 +112,151 @@ class MFXReadWriteMid mfxMemId m_mid_to_report; }; +#if (defined(_WIN32) || defined(_WIN64)) + +#include +#include +#include + +struct ID3D11VideoDevice; +struct ID3D11VideoContext; + +struct D3D11AllocatorParams : mfxAllocatorParams +{ + ID3D11Device *pDevice; + bool bUseSingleTexture; + DWORD uncompressedResourceMiscFlags; + + D3D11AllocatorParams() + : pDevice() + , bUseSingleTexture() + , uncompressedResourceMiscFlags() + { + } +}; + +class D3D11FrameAllocator: public BaseFrameAllocator +{ +public: + + D3D11FrameAllocator(); + virtual ~D3D11FrameAllocator(); + + virtual mfxStatus Init(mfxAllocatorParams *pParams); + virtual mfxStatus Close(); + virtual ID3D11Device * GetD3D11Device() + { + return m_initParams.pDevice; + }; + virtual mfxStatus LockFrame(mfxMemId mid, mfxFrameData *ptr); + virtual mfxStatus UnlockFrame(mfxMemId mid, mfxFrameData *ptr); + virtual mfxStatus GetFrameHDL(mfxMemId mid, mfxHDL *handle); + +protected: + static DXGI_FORMAT ConverColortFormat(mfxU32 fourcc); + virtual mfxStatus CheckRequestType(mfxFrameAllocRequest *request); + virtual mfxStatus ReleaseResponse(mfxFrameAllocResponse *response); + virtual mfxStatus AllocImpl(mfxFrameAllocRequest *request, mfxFrameAllocResponse *response); + + D3D11AllocatorParams m_initParams; + ID3D11DeviceContext *m_pDeviceContext; + + struct TextureResource + { + std::vector outerMids; + std::vector textures; + std::vector stagingTexture; + bool bAlloc; + + TextureResource() + : bAlloc(true) + { + } + + static bool isAllocated (TextureResource & that) + { + return that.bAlloc; + } + ID3D11Texture2D* GetTexture(mfxMemId id) + { + if (outerMids.empty()) + return NULL; + + return textures[((uintptr_t)id - (uintptr_t)outerMids.front()) % textures.size()]; + } + UINT GetSubResource(mfxMemId id) + { + if (outerMids.empty()) + return NULL; + + return (UINT)(((uintptr_t)id - (uintptr_t)outerMids.front()) / textures.size()); + } + void Release() + { + size_t i = 0; + for(i = 0; i < textures.size(); i++) + { + textures[i]->Release(); + } + textures.clear(); + + for(i = 0; i < stagingTexture.size(); i++) + { + stagingTexture[i]->Release(); + } + stagingTexture.clear(); + + //marking texture as deallocated + bAlloc = false; + } + }; + class TextureSubResource + { + TextureResource * m_pTarget; + ID3D11Texture2D * m_pTexture; + ID3D11Texture2D * m_pStaging; + UINT m_subResource; + public: + TextureSubResource(TextureResource * pTarget = NULL, mfxMemId id = 0) + : m_pTarget(pTarget) + , m_pTexture() + , m_subResource() + , m_pStaging(NULL) + { + if (NULL != m_pTarget && !m_pTarget->outerMids.empty()) + { + ptrdiff_t idx = (uintptr_t)MFXReadWriteMid(id).raw() - (uintptr_t)m_pTarget->outerMids.front(); + m_pTexture = m_pTarget->textures[idx % m_pTarget->textures.size()]; + m_subResource = (UINT)(idx / m_pTarget->textures.size()); + m_pStaging = m_pTarget->stagingTexture.empty() ? NULL : m_pTarget->stagingTexture[idx]; + } + } + ID3D11Texture2D* GetStaging()const + { + return m_pStaging; + } + ID3D11Texture2D* GetTexture()const + { + return m_pTexture; + } + UINT GetSubResource()const + { + return m_subResource; + } + void Release() + { + if (NULL != m_pTarget) + m_pTarget->Release(); + } + }; + + TextureSubResource GetResourceFromMid(mfxMemId); + + std::list m_resourcesByRequest;//each alloc request generates new item in list + + typedef std::list ::iterator referenceType; + std::vector m_memIdMap; +}; + +#endif // #if defined(_WIN32) || defined(_WIN64) #endif // __D3D11_ALLOCATOR_H__ diff --git a/samples/sample_common/include/d3d11_device.h b/samples/sample_common/include/d3d11_device.h index 81be68ccb..c9c0d126f 100644 --- a/samples/sample_common/include/d3d11_device.h +++ b/samples/sample_common/include/d3d11_device.h @@ -19,3 +19,68 @@ or https://software.intel.com/en-us/media-client-solutions-support. #pragma once +#if defined( _WIN32 ) || defined ( _WIN64 ) + +#include "sample_defs.h" // defines MFX_D3D11_SUPPORT + +#if MFX_D3D11_SUPPORT +#include "hw_device.h" +#include +#include +#include + +#include + +class CD3D11Device: public CHWDevice +{ +public: + CD3D11Device(); + virtual ~CD3D11Device(); + virtual mfxStatus Init( + mfxHDL hWindow, + mfxU16 nViews, + mfxU32 nAdapterNum); + virtual mfxStatus Reset(); + virtual mfxStatus GetHandle(mfxHandleType type, mfxHDL *pHdl); + virtual mfxStatus SetHandle(mfxHandleType type, mfxHDL hdl); + virtual mfxStatus RenderFrame(mfxFrameSurface1 * pSurface, mfxFrameAllocator * pmfxAlloc); + virtual void UpdateTitle(double /*fps*/) { } + virtual void Close(); + void DefineFormat(bool isA2rgb10) { m_bIsA2rgb10 = (isA2rgb10) ? TRUE : FALSE; } + virtual void SetMondelloInput(bool /*isMondelloInputEnabled*/ ) { } +protected: + virtual mfxStatus FillSCD(mfxHDL hWindow, DXGI_SWAP_CHAIN_DESC& scd); + virtual mfxStatus FillSCD1(DXGI_SWAP_CHAIN_DESC1& scd); + mfxStatus CreateVideoProcessor(mfxFrameSurface1 * pSrf); + + CComPtr m_pD3D11Device; + CComPtr m_pD3D11Ctx; + CComQIPtr m_pDX11VideoDevice; + CComQIPtr m_pVideoContext; + CComPtr m_VideoProcessorEnum; + + CComQIPtr m_pDXGIDev; + CComQIPtr m_pAdapter; + + CComPtr m_pDXGIFactory; + + CComPtr m_pSwapChain; + CComPtr m_pVideoProcessor; + +private: + CComPtr m_pInputViewLeft; + CComPtr m_pInputViewRight; + CComPtr m_pOutputView; + + CComPtr m_pDXGIBackBuffer; + CComPtr m_pTempTexture; + CComPtr m_pDisplayControl; + CComPtr m_pDXGIOutput; + mfxU16 m_nViews; + BOOL m_bDefaultStereoEnabled; + BOOL m_bIsA2rgb10; + HWND m_HandleWindow; +}; + +#endif //#if defined( _WIN32 ) || defined ( _WIN64 ) +#endif //#if MFX_D3D11_SUPPORT diff --git a/samples/sample_common/include/d3d_allocator.h b/samples/sample_common/include/d3d_allocator.h index c4421a5a5..bb017ea2a 100644 --- a/samples/sample_common/include/d3d_allocator.h +++ b/samples/sample_common/include/d3d_allocator.h @@ -20,4 +20,66 @@ or https://software.intel.com/en-us/media-client-solutions-support. #ifndef __D3D_ALLOCATOR_H__ #define __D3D_ALLOCATOR_H__ +#if defined( _WIN32 ) || defined ( _WIN64 ) + +#include +#include +#include +#include "base_allocator.h" +#include + +enum eTypeHandle +{ + DXVA2_PROCESSOR = 0x00, + DXVA2_DECODER = 0x01 +}; + +struct D3DAllocatorParams : mfxAllocatorParams +{ + IDirect3DDeviceManager9 *pManager; + DWORD surfaceUsage; + + D3DAllocatorParams() + : pManager() + , surfaceUsage() + { + } +}; + +class D3DFrameAllocator: public BaseFrameAllocator +{ +public: + D3DFrameAllocator(); + virtual ~D3DFrameAllocator(); + + virtual mfxStatus Init(mfxAllocatorParams *pParams); + virtual mfxStatus Close(); + + virtual IDirect3DDeviceManager9* GetDeviceManager() + { + return m_manager; + }; + + virtual mfxStatus LockFrame(mfxMemId mid, mfxFrameData *ptr); + virtual mfxStatus UnlockFrame(mfxMemId mid, mfxFrameData *ptr); + virtual mfxStatus GetFrameHDL(mfxMemId mid, mfxHDL *handle); + +protected: + virtual mfxStatus CheckRequestType(mfxFrameAllocRequest *request); + virtual mfxStatus ReleaseResponse(mfxFrameAllocResponse *response); + virtual mfxStatus AllocImpl(mfxFrameAllocRequest *request, mfxFrameAllocResponse *response); + + void DeallocateMids(mfxHDLPair** pairs, int n); + + std::vector m_midsAllocated; + + CComPtr m_manager; + CComPtr m_decoderService; + CComPtr m_processorService; + HANDLE m_hDecoder; + HANDLE m_hProcessor; + DWORD m_surfaceUsage; +}; + +#endif // #if defined( _WIN32 ) || defined ( _WIN64 ) #endif // __D3D_ALLOCATOR_H__ diff --git a/samples/sample_common/include/d3d_device.h b/samples/sample_common/include/d3d_device.h index 81be68ccb..80c5d12e3 100644 --- a/samples/sample_common/include/d3d_device.h +++ b/samples/sample_common/include/d3d_device.h @@ -19,3 +19,70 @@ or https://software.intel.com/en-us/media-client-solutions-support. #pragma once +#if defined( _WIN32 ) || defined ( _WIN64 ) + +#include "hw_device.h" + +#pragma warning(disable : 4201) +#include +#include +#include +#include + +#define VIDEO_MAIN_FORMAT D3DFMT_YUY2 + +/** Direct3D 9 device implementation. +@note Device always set D3DPRESENT_PARAMETERS::Windowed to TRUE. +*/ +class CD3D9Device : public CHWDevice +{ +public: + CD3D9Device(); + virtual ~CD3D9Device(); + + virtual mfxStatus Init( + mfxHDL hWindow, + mfxU16 nViews, + mfxU32 nAdapterNum); + virtual mfxStatus Reset(); + virtual mfxStatus GetHandle(mfxHandleType type, mfxHDL *pHdl); + virtual mfxStatus SetHandle(mfxHandleType type, mfxHDL hdl); + virtual mfxStatus RenderFrame(mfxFrameSurface1 * pSurface, mfxFrameAllocator * pmfxAlloc); + virtual void UpdateTitle(double /*fps*/) { } + virtual void Close() ; + void DefineFormat(bool isA2rgb10) { m_bIsA2rgb10 = (isA2rgb10) ? TRUE : FALSE; } + virtual void SetMondelloInput(bool /*isMondelloInputEnabled*/) { } +protected: + mfxStatus CreateVideoProcessors(); + bool CheckOverlaySupport(); + virtual mfxStatus FillD3DPP(mfxHDL hWindow, mfxU16 nViews, D3DPRESENT_PARAMETERS &D3DPP); +private: + IDirect3D9Ex* m_pD3D9; + IDirect3DDevice9Ex* m_pD3DD9; + IDirect3DDeviceManager9* m_pDeviceManager9; + D3DPRESENT_PARAMETERS m_D3DPP; + UINT m_resetToken; + + mfxU16 m_nViews; + + D3DSURFACE_DESC m_backBufferDesc; + + // service required to create video processors + IDirectXVideoProcessorService* m_pDXVAVPS; + //left channel processor + IDirectXVideoProcessor* m_pDXVAVP_Left; + // right channel processor + IDirectXVideoProcessor* m_pDXVAVP_Right; + + // target rectangle + RECT m_targetRect; + + // various structures for DXVA2 calls + DXVA2_VideoDesc m_VideoDesc; + DXVA2_VideoProcessBltParams m_BltParams; + DXVA2_VideoSample m_Sample; + + BOOL m_bIsA2rgb10; +}; + +#endif // #if defined( _WIN32 ) || defined ( _WIN64 ) diff --git a/samples/sample_common/include/decode_render.h b/samples/sample_common/include/decode_render.h index fad66a273..f0ea8051f 100644 --- a/samples/sample_common/include/decode_render.h +++ b/samples/sample_common/include/decode_render.h @@ -21,6 +21,14 @@ or https://software.intel.com/en-us/media-client-solutions-support. #ifndef __DECODE_D3D_RENDER_H__ #define __DECODE_D3D_RENDER_H__ +#if defined(_WIN32) || defined(_WIN64) + +#pragma warning(disable : 4201) +#include +#include +#include +#include +#endif #include "mfxstructures.h" #include "mfxvideo.h" @@ -30,5 +38,72 @@ or https://software.intel.com/en-us/media-client-solutions-support. typedef void* WindowHandle; typedef void* Handle; +#if defined(_WIN32) || defined(_WIN64) + +struct sWindowParams +{ + LPCTSTR lpClassName; + LPCTSTR lpWindowName; + DWORD dwStyle; + int nx; + int ny; + int ncell; + int nAdapter; + int nWidth; + int nHeight; + HWND hWndParent; + HMENU hMenu; + HINSTANCE hInstance; + LPVOID lpParam; + bool bFullScreen; // Stretch window to full screen +}; + +class CDecodeD3DRender +{ +public: + + CDecodeD3DRender(); + virtual ~CDecodeD3DRender(); + + virtual mfxStatus Init(sWindowParams pWParams); + virtual mfxStatus RenderFrame(mfxFrameSurface1 *pSurface, mfxFrameAllocator *pmfxAlloc); + virtual VOID UpdateTitle(double fps); + void Close(); + + HWND GetWindowHandle(); + + VOID OnDestroy(HWND hwnd); + VOID OnKey(HWND hwnd, UINT vk, BOOL fDown, int cRepeat, UINT flags); + VOID ChangeWindowSize(bool bFullScreen); + + void SetHWDevice(CHWDevice *dev) + { + m_hwdev = dev; + } +protected: + void AdjustWindowRect(RECT *rect); + + mfxStatus AllocateShiftedSurfaceIfNeeded(const mfxFrameSurface1* refSurface,mfxFrameAllocator* allocator); + mfxFrameAllocResponse shiftSurfaceResponse; + mfxFrameSurface1 shiftedSurface; + mfxFrameAllocator* pAllocator; + + CHWDevice *m_hwdev; + + + sWindowParams m_sWindowParams; + HWND m_Hwnd; + RECT m_rect; + DWORD m_style; + + bool EnableDwmQueuing(); + static BOOL CALLBACK MonitorEnumProc(HMONITOR ,HDC ,LPRECT lprcMonitor,LPARAM dwData); + static bool m_bIsMonitorFound; + + bool m_bDwmEnabled; + int m_nMonitorCurrent; + ::RECT m_RectWindow; +}; +#endif // #if defined(_WIN32) || defined(_WIN64) #endif // __DECODE_D3D_RENDER_H__ \ No newline at end of file diff --git a/samples/sample_common/include/mfx_buffering.h b/samples/sample_common/include/mfx_buffering.h index d2bd6ec1f..7fc03b43c 100644 --- a/samples/sample_common/include/mfx_buffering.h +++ b/samples/sample_common/include/mfx_buffering.h @@ -51,7 +51,7 @@ struct msdkOutputSurface * normal circumstances. Macro should be used where check in release mode is not * desirable and atually needed. */ - #define MSDK_SELF_CHECK(C) +#define MSDK_SELF_CHECK(C) class CBuffering; diff --git a/samples/sample_common/include/mfx_samples_config.h b/samples/sample_common/include/mfx_samples_config.h index 0fe88d80a..c475c6992 100644 --- a/samples/sample_common/include/mfx_samples_config.h +++ b/samples/sample_common/include/mfx_samples_config.h @@ -20,5 +20,4 @@ or https://software.intel.com/en-us/media-client-solutions-support. #ifndef __MFX_SAMPLES_CONFIG__ #define __MFX_SAMPLES_CONFIG__ - #endif //__MFX_SAMPLES_CONFIG__ diff --git a/samples/sample_common/include/plugin_loader.h b/samples/sample_common/include/plugin_loader.h index f7c17ecb6..1429e34b8 100644 --- a/samples/sample_common/include/plugin_loader.h +++ b/samples/sample_common/include/plugin_loader.h @@ -105,7 +105,7 @@ class PluginLoader : public MFXPlugin else if (AreGuidsEqual(guid, MFX_PLUGINID_HEVCE_GACC)) return MSDK_STRING("Intel (R) Media SDK GPU-Accelerated plugin for HEVC ENCODE"); else -#if MFX_VERSION >= 1027 +#if (MFX_VERSION >= 1027) && !defined(_WIN32) && !defined(_WIN64) if (AreGuidsEqual(guid, MFX_PLUGINID_HEVC_FEI_ENCODE)) return MSDK_STRING("Intel (R) Media SDK HW plugin for HEVC FEI ENCODE"); else diff --git a/samples/sample_common/include/plugin_utils.h b/samples/sample_common/include/plugin_utils.h index b01f36cb3..8a983ff1e 100644 --- a/samples/sample_common/include/plugin_utils.h +++ b/samples/sample_common/include/plugin_utils.h @@ -23,8 +23,13 @@ or https://software.intel.com/en-us/media-client-solutions-support. #include "sample_defs.h" #include "sample_types.h" +#if defined(_WIN32) || defined(_WIN64) + #define MSDK_CPU_ROTATE_PLUGIN MSDK_STRING("sample_rotate_plugin.dll") + #define MSDK_OCL_ROTATE_PLUGIN MSDK_STRING("sample_plugin_opencl.dll") +#else #define MSDK_CPU_ROTATE_PLUGIN MSDK_STRING("libsample_rotate_plugin.so") #define MSDK_OCL_ROTATE_PLUGIN MSDK_STRING("libsample_plugin_opencl.so") +#endif typedef mfxI32 msdkComponentType; enum diff --git a/samples/sample_common/include/preset_manager.h b/samples/sample_common/include/preset_manager.h new file mode 100644 index 000000000..df9fcb805 --- /dev/null +++ b/samples/sample_common/include/preset_manager.h @@ -0,0 +1,127 @@ +/******************************************************************************\ +Copyright (c) 2005-2018, Intel Corporation +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +This sample was distributed or derived from the Intel's Media Samples package. +The original version of this sample may be obtained from https://software.intel.com/en-us/intel-media-server-studio +or https://software.intel.com/en-us/media-client-solutions-support. +\**********************************************************************************/ + +#include "sample_defs.h" + +#pragma once +enum EPresetModes +{ + PRESET_DEFAULT, + PRESET_DSS, + PRESET_CONF, + PRESET_GAMING, + PRESET_MAX_MODES +}; + +enum EPresetCodecs +{ + PRESET_AVC, + PRESET_HEVC, + PRESET_MAX_CODECS +}; + +struct CPresetParameters +{ + mfxU16 GopRefDist; + + mfxU16 TargetUsage; + + mfxU16 RateControlMethod; + mfxU16 ExtBRCUsage; + mfxU16 AsyncDepth; + mfxU16 BRefType; + mfxU16 AdaptiveMaxFrameSize; + mfxU16 LowDelayBRC; + + mfxU16 IntRefType; + mfxU16 IntRefCycleSize; + mfxU16 IntRefQPDelta; + mfxU16 IntRefCycleDist; + + mfxU16 WeightedPred; + mfxU16 WeightedBiPred; + + bool EnableBPyramid; + bool EnablePPyramid; +// bool EnableLTR; +}; + +struct CDependentPresetParameters +{ + mfxU16 TargetKbps; + mfxU16 MaxKbps; + mfxU16 GopPicSize; + mfxU16 BufferSizeInKB; + mfxU16 LookAheadDepth; + mfxU32 MaxFrameSize; +}; + +struct COutputPresetParameters : public CPresetParameters,CDependentPresetParameters +{ + + msdk_string PresetName; + + void Clear() + { + memset(dynamic_cast(this), 0, sizeof(CPresetParameters)); + memset(dynamic_cast(this), 0, sizeof(CDependentPresetParameters)); + } + + COutputPresetParameters() + { + Clear(); + } + + COutputPresetParameters(CPresetParameters src) + { + Clear(); + *(CPresetParameters*)this = src; + } +}; + +class CPresetManager +{ +public: + ~CPresetManager(); + COutputPresetParameters GetPreset(EPresetModes mode, mfxU32 codecFourCC, mfxF64 fps, mfxU32 width, mfxU32 height, bool isHWLib); + COutputPresetParameters GetBasicPreset(EPresetModes mode, mfxU32 codecFourCC); + CDependentPresetParameters GetDependentPresetParameters(EPresetModes mode, mfxU32 codecFourCC, mfxF64 fps, mfxU32 width, mfxU32 height, mfxU16 targetUsage); + + static CPresetManager Inst; + static EPresetModes PresetNameToMode(const msdk_char* name); +protected: + CPresetManager(); + static CPresetParameters presets[PRESET_MAX_MODES][PRESET_MAX_CODECS]; + static msdk_string modesName[PRESET_MAX_MODES]; +}; + +#define MODIFY_AND_PRINT_PARAM(paramName,presetName,shouldPrintPresetInfo) \ +if (!paramName){ \ + paramName = presetParams.presetName; \ + if(shouldPrintPresetInfo){msdk_printf(MSDK_STRING(#presetName) MSDK_STRING(": %d\n"), (int)paramName);} \ +}else{ \ + if(shouldPrintPresetInfo){msdk_printf(MSDK_STRING(#presetName) MSDK_STRING(": %d (original preset value: %d)\n"), (int)paramName, (int)presetParams.presetName);}} + +#define MODIFY_AND_PRINT_PARAM_EXT(paramName,presetName,value,shouldPrintPresetInfo) \ +if (!paramName){ \ + paramName = (value); \ + if(shouldPrintPresetInfo){msdk_printf(MSDK_STRING(#presetName) MSDK_STRING(": %d\n"), (int)paramName);} \ +}else{ \ + if(shouldPrintPresetInfo){msdk_printf(MSDK_STRING(#presetName) MSDK_STRING(": %d (original preset value: %d)\n"), (int)paramName, (int)(value));}} + diff --git a/samples/sample_common/include/sample_defs.h b/samples/sample_common/include/sample_defs.h index aa8ce3d3a..207fcf1ae 100644 --- a/samples/sample_common/include/sample_defs.h +++ b/samples/sample_common/include/sample_defs.h @@ -1,5 +1,5 @@ /******************************************************************************\ -Copyright (c) 2005-2018, Intel Corporation +Copyright (c) 2005-2019, Intel Corporation All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: @@ -39,7 +39,10 @@ or https://software.intel.com/en-us/media-client-solutions-support. #if (MFX_VERSION >= 1026) #define ENABLE_MCTF +#if defined(MFX_VERSION_NEXT) && (MFX_VERSION >= MFX_VERSION_NEXT) +//---MCTF, extended interface #undef ENABLE_MCTF_EXT +#endif enum {MCTF_BITRATE_MULTIPLIER = 100000}; #endif diff --git a/samples/sample_common/include/sample_utils.h b/samples/sample_common/include/sample_utils.h index e8a0de58b..5cdb3090d 100644 --- a/samples/sample_common/include/sample_utils.h +++ b/samples/sample_common/include/sample_utils.h @@ -100,11 +100,17 @@ enum MFX_FOURCC_YUV444 = MFX_MAKEFOURCC('4','4','4','P'), #if (MFX_VERSION <= 1027) MFX_FOURCC_RGBP = MFX_MAKEFOURCC('R','G','B','P'), -#else #endif MFX_FOURCC_I420 = MFX_MAKEFOURCC('I','4','2','0') }; +enum ExtBRCType { + EXTBRC_DEFAULT, + EXTBRC_OFF, + EXTBRC_ON, + EXTBRC_IMPLICIT +}; + bool IsDecodeCodecSupported(mfxU32 codecFormat); bool IsEncodeCodecSupported(mfxU32 codecFormat); bool IsPluginCodecSupported(mfxU32 codecFormat); @@ -488,6 +494,7 @@ mfxStatus ConvertFrameRate(mfxF64 dFrameRate, mfxU32* pnFrameRateExtN, mfxU32* p mfxF64 CalculateFrameRate(mfxU32 nFrameRateExtN, mfxU32 nFrameRateExtD); mfxU16 GetFreeSurfaceIndex(mfxFrameSurface1* pSurfacesPool, mfxU16 nPoolSize); mfxU16 GetFreeSurface(mfxFrameSurface1* pSurfacesPool, mfxU16 nPoolSize); +void FreeSurfacePool(mfxFrameSurface1* pSurfacesPool, mfxU16 nPoolSize); mfxStatus InitMfxBitstream(mfxBitstream* pBitstream, mfxU32 nSize); //performs copy to end if possible, also move data to buffer begin if necessary @@ -698,13 +705,22 @@ template template mfxStatus msdk_opt_read(const msdk_char* string, msdk_char (&value)[S]) { + if (!S) + { + return MFX_ERR_UNKNOWN; + } value[0]=0; + #if defined(_WIN32) || defined(_WIN64) + value[S - 1] = 0; + return (0 == _tcsncpy_s(value, string,S-1))? MFX_ERR_NONE: MFX_ERR_UNKNOWN; + #else if (strlen(string) < S) { strncpy(value, string, S-1); value[S - 1] = 0; return MFX_ERR_NONE; } return MFX_ERR_UNKNOWN; + #endif } template diff --git a/samples/sample_common/include/version.h b/samples/sample_common/include/version.h index 483fb4474..981d2bc4d 100644 --- a/samples/sample_common/include/version.h +++ b/samples/sample_common/include/version.h @@ -26,11 +26,11 @@ or https://software.intel.com/en-us/media-client-solutions-support. #endif #ifndef MSDK_MINOR -#define MSDK_MINOR 3 +#define MSDK_MINOR 4 #endif #ifndef MSDK_RELEASE -#define MSDK_TARGETAPIMINOR 26 +#define MSDK_TARGETAPIMINOR 27 #endif #ifndef MSDK_BUILD diff --git a/samples/sample_common/include/vm/file_defs.h b/samples/sample_common/include/vm/file_defs.h index aa3b8014e..ab591bb93 100644 --- a/samples/sample_common/include/vm/file_defs.h +++ b/samples/sample_common/include/vm/file_defs.h @@ -24,10 +24,17 @@ or https://software.intel.com/en-us/media-client-solutions-support. #include +#if defined(_WIN32) || defined(_WIN64) + +#define MSDK_FOPEN(file, name, mode) _tfopen_s(&file, name, mode) + +#define msdk_fgets _fgetts +#else // #if defined(_WIN32) || defined(_WIN64) #include #define MSDK_FOPEN(file, name, mode) !(file = fopen(name, mode)) #define msdk_fgets fgets +#endif // #if defined(_WIN32) || defined(_WIN64) #endif // #ifndef __FILE_DEFS_H__ diff --git a/samples/sample_common/include/vm/strings_defs.h b/samples/sample_common/include/vm/strings_defs.h index 65f0bc870..2679b89bb 100644 --- a/samples/sample_common/include/vm/strings_defs.h +++ b/samples/sample_common/include/vm/strings_defs.h @@ -1,5 +1,5 @@ /******************************************************************************\ -Copyright (c) 2005-2018, Intel Corporation +Copyright (c) 2005-2019, Intel Corporation All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: @@ -28,6 +28,51 @@ or https://software.intel.com/en-us/media-client-solutions-support. #include #endif +#if defined(_WIN32) || defined(_WIN64) + +#include + +#define MSDK_STRING(x) _T(x) +#define MSDK_CHAR(x) _T(x) + +#ifdef __cplusplus +typedef std::basic_string msdk_tstring; +#endif +typedef TCHAR msdk_char; + +#define msdk_printf _tprintf +#define msdk_fprintf _ftprintf +#define msdk_sprintf _stprintf_s // to be removed +#define msdk_vprintf _vtprintf +#define msdk_strlen _tcslen +#define msdk_strcmp _tcscmp +#define msdk_stricmp _tcsicmp +#define msdk_strncmp _tcsnicmp +#define msdk_strstr _tcsstr +#define msdk_atoi _ttoi +#define msdk_strtol _tcstol +#define msdk_strtod _tcstod +#define msdk_strchr _tcschr +#define msdk_strnlen(str,lenmax) strnlen_s(str,lenmax) +#define msdk_sscanf _stscanf_s +#define msdk_itoa_decimal(value, str) _itot_s(value,str,10) + +// msdk_strcopy is intended to be used with 2 parmeters, i.e. msdk_strcopy(dst, src) +// for _tcscpy_s that's possible if DST is declared as: TCHAR DST[n]; +#define msdk_strcopy _tcscpy_s +#define msdk_strncopy_s _tcsncpy_s + +#define MSDK_MEMCPY_BITSTREAM(bitstream, offset, src, count) memcpy_s((bitstream).Data + (offset), (bitstream).MaxLength - (offset), (src), (count)) + +#define MSDK_MEMCPY_BUF(bufptr, offset, maxsize, src, count) memcpy_s((bufptr)+ (offset), (maxsize) - (offset), (src), (count)) + +#define MSDK_MEMCPY_VAR(dstVarName, src, count) memcpy_s(&(dstVarName), sizeof(dstVarName), (src), (count)) + +#define MSDK_MEMCPY(dst, src, count) memcpy_s(dst, (count), (src), (count)) + + + +#else // #if defined(_WIN32) || defined(_WIN64) #define MSDK_STRING(x) x #define MSDK_CHAR(x) x @@ -67,5 +112,6 @@ typedef char msdk_char; #define MSDK_MEMCPY(dst, src, count) memcpy(dst, (src), (count)) +#endif // #if defined(_WIN32) || defined(_WIN64) #endif //__STRING_DEFS_H__ diff --git a/samples/sample_common/include/vm/thread_defs.h b/samples/sample_common/include/vm/thread_defs.h index 5d2e307e0..2d236689a 100644 --- a/samples/sample_common/include/vm/thread_defs.h +++ b/samples/sample_common/include/vm/thread_defs.h @@ -25,6 +25,32 @@ or https://software.intel.com/en-us/media-client-solutions-support. typedef unsigned int (MFX_STDCALL * msdk_thread_callback)(void*); +#if defined(_WIN32) || defined(_WIN64) + +#include +#include + +struct msdkMutexHandle +{ + CRITICAL_SECTION m_CritSec; +}; + +struct msdkSemaphoreHandle +{ + void* m_semaphore; +}; + +struct msdkEventHandle +{ + void* m_event; +}; + +struct msdkThreadHandle +{ + void* m_thread; +}; + +#else // #if defined(_WIN32) || defined(_WIN64) #include #include @@ -79,6 +105,7 @@ struct msdkThreadHandle pthread_t m_thread; }; +#endif // #if defined(_WIN32) || defined(_WIN64) class MSDKMutex: public msdkMutexHandle { @@ -153,7 +180,9 @@ class MSDKThread: public msdkThreadHandle mfxStatus TimedWait(mfxU32 msec); mfxStatus GetExitCode(); +#if !defined(_WIN32) && !defined(_WIN64) friend void* msdk_thread_start(void* arg); +#endif private: MSDKThread(const MSDKThread&); diff --git a/samples/sample_common/include/vm/time_defs.h b/samples/sample_common/include/vm/time_defs.h index 6e46ce369..a0dd48786 100644 --- a/samples/sample_common/include/vm/time_defs.h +++ b/samples/sample_common/include/vm/time_defs.h @@ -23,6 +23,23 @@ or https://software.intel.com/en-us/media-client-solutions-support. #include "mfxdefs.h" #include "mfx_itt_trace.h" +#if defined(_WIN32) || defined(_WIN64) + +#include + +#define MSDK_SLEEP(msec) Sleep(msec) + +#define MSDK_USLEEP(usec) \ +{ \ + LARGE_INTEGER due; \ + due.QuadPart = -(10*(int)usec); \ + HANDLE t = CreateWaitableTimer(NULL, TRUE, NULL); \ + SetWaitableTimer(t, &due, 0, NULL, NULL, 0); \ + WaitForSingleObject(t, INFINITE); \ + CloseHandle(t); \ +} + +#else // #if defined(_WIN32) || defined(_WIN64) #include @@ -38,6 +55,7 @@ or https://software.intel.com/en-us/media-client-solutions-support. usleep(usec); \ } while(0) +#endif // #if defined(_WIN32) || defined(_WIN64) #define MSDK_GET_TIME(T,S,F) ((mfxF64)((T)-(S))/(mfxF64)(F)) diff --git a/samples/sample_common/props/winsdk_win32.props b/samples/sample_common/props/winsdk_win32.props new file mode 100644 index 000000000..ef0bc2b9c --- /dev/null +++ b/samples/sample_common/props/winsdk_win32.props @@ -0,0 +1,19 @@ + + + + <_ProjectFileVersion>10.0.30319.1 + + + NOMINMAX; + + + + %(AdditionalIncludeDirectories) + %(PreprocessorDefinitions) + + + $(VCInstallDir)PlatformSDK\include;%(AdditionalIncludeDirectories) + %(PreprocessorDefinitions) + + + \ No newline at end of file diff --git a/samples/sample_common/props/winsdk_x64.props b/samples/sample_common/props/winsdk_x64.props new file mode 100644 index 000000000..ef0bc2b9c --- /dev/null +++ b/samples/sample_common/props/winsdk_x64.props @@ -0,0 +1,19 @@ + + + + <_ProjectFileVersion>10.0.30319.1 + + + NOMINMAX; + + + + %(AdditionalIncludeDirectories) + %(PreprocessorDefinitions) + + + $(VCInstallDir)PlatformSDK\include;%(AdditionalIncludeDirectories) + %(PreprocessorDefinitions) + + + \ No newline at end of file diff --git a/samples/sample_common/sample_common.vcxproj b/samples/sample_common/sample_common.vcxproj new file mode 100644 index 000000000..7bdd67c8b --- /dev/null +++ b/samples/sample_common/sample_common.vcxproj @@ -0,0 +1,308 @@ + + + + + Debug_WithDebugAPI + Win32 + + + Debug_WithDebugAPI + x64 + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {5FADB243-53C3-4776-A20F-8BD65C10CF41} + sample_common + Win32Proj + + + + StaticLibrary + false + Unicode + true + v141 + + + StaticLibrary + false + Unicode + v141 + + + StaticLibrary + false + Unicode + v141 + + + StaticLibrary + false + Unicode + true + v141 + + + StaticLibrary + false + Unicode + v141 + + + StaticLibrary + false + Unicode + v141 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + <_ProjectFileVersion>10.0.30319.1 + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + $(OutDir)obj\$(ProjectName)\ + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + $(OutDir)obj\$(ProjectName)\ + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + sample_common + sample_common + sample_common + sample_common + sample_common + sample_common + + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\include\vm;%(AdditionalIncludeDirectories) + $(MfxDefinitions);WIN32;_WIN32;_DEBUG;_LIB;_ITERATOR_DEBUG_LEVEL=0;%(PreprocessorDefinitions) + true + Async + EnableFastChecks + MultiThreadedDebug + + + Level4 + true + EditAndContinue + + + $(OutDir)$(TargetName).lib + %(AdditionalLibraryDirectories) + true + + + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\include\vm;%(AdditionalIncludeDirectories) + $(MfxDefinitions);WIN32;_WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + true + Async + EnableFastChecks + MultiThreadedDebug + + + Level4 + true + EditAndContinue + + + $(OutDir)$(TargetName).lib + %(AdditionalLibraryDirectories) + true + + + + + X64 + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\include\vm;%(AdditionalIncludeDirectories) + $(MfxDefinitions);_WIN64;WIN64;_DEBUG;_LIB;_ITERATOR_DEBUG_LEVEL=0;%(PreprocessorDefinitions) + true + Async + EnableFastChecks + MultiThreadedDebug + + + Level4 + true + ProgramDatabase + + + $(OutDir)$(TargetName).lib + + + + + X64 + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\include\vm;%(AdditionalIncludeDirectories) + $(MfxDefinitions);_WIN64;WIN64;_DEBUG;_LIB;%(PreprocessorDefinitions) + true + Async + EnableFastChecks + MultiThreadedDebug + + + Level4 + true + ProgramDatabase + + + $(OutDir)$(TargetName).lib + + + + + /O2 %(AdditionalOptions) + $(ProjectDir)\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\include\vm;%(AdditionalIncludeDirectories) + $(MfxDefinitions);WIN32;_WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + Async + MultiThreaded + + + Level4 + true + ProgramDatabase + + + $(OutDir)$(TargetName).lib + + + + + X64 + + + $(ProjectDir)\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\include\vm;%(AdditionalIncludeDirectories) + $(MfxDefinitions);_WIN64;WIN64;NDEBUG;_LIB;%(PreprocessorDefinitions) + Async + MultiThreaded + + + Level4 + true + ProgramDatabase + + + $(OutDir)$(TargetName).lib + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/samples/sample_common/src/brc_routines.cpp b/samples/sample_common/src/brc_routines.cpp index 5037e737f..e5ae2eff0 100644 --- a/samples/sample_common/src/brc_routines.cpp +++ b/samples/sample_common/src/brc_routines.cpp @@ -1,5 +1,5 @@ /******************************************************************************\ -Copyright (c) 2005-2018, Intel Corporation +Copyright (c) 2005-2019, Intel Corporation All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: @@ -997,4 +997,4 @@ mfxStatus ExtBRC::Reset(mfxVideoParam *par ) return sts; } -#endif \ No newline at end of file +#endif diff --git a/samples/sample_common/src/d3d11_allocator.cpp b/samples/sample_common/src/d3d11_allocator.cpp index 9f1a77bc3..41a7a3d6c 100644 --- a/samples/sample_common/src/d3d11_allocator.cpp +++ b/samples/sample_common/src/d3d11_allocator.cpp @@ -19,3 +19,544 @@ or https://software.intel.com/en-us/media-client-solutions-support. #include "mfx_samples_config.h" +#if defined(_WIN32) || defined(_WIN64) + +#include "sample_defs.h" + +#if MFX_D3D11_SUPPORT + +#include +#include +#include +#include +#include +#include +#include "d3d11_allocator.h" + +#define D3DFMT_NV12 (DXGI_FORMAT)MAKEFOURCC('N','V','1','2') +#define D3DFMT_YV12 (DXGI_FORMAT)MAKEFOURCC('Y','V','1','2') + +//for generating sequence of mfx handles +template +struct sequence { + T x; + sequence(T seed) : x(seed) { } +}; + +template <> +struct sequence { + mfxHDL x; + sequence(mfxHDL seed) : x(seed) { } + + mfxHDL operator ()() + { + mfxHDL y = x; + x = (mfxHDL)(1 + (size_t)(x)); + return y; + } +}; + + +D3D11FrameAllocator::D3D11FrameAllocator() +{ + m_pDeviceContext = NULL; +} + +D3D11FrameAllocator::~D3D11FrameAllocator() +{ + Close(); +} + +D3D11FrameAllocator::TextureSubResource D3D11FrameAllocator::GetResourceFromMid(mfxMemId mid) +{ + size_t index = (size_t)MFXReadWriteMid(mid).raw() - 1; + + if(m_memIdMap.size() <= index) + return TextureSubResource(); + + //reverse iterator dereferencing + TextureResource * p = &(*m_memIdMap[index]); + if (!p->bAlloc) + return TextureSubResource(); + + return TextureSubResource(p, mid); +} + +mfxStatus D3D11FrameAllocator::Init(mfxAllocatorParams *pParams) +{ + D3D11AllocatorParams *pd3d11Params = 0; + pd3d11Params = dynamic_cast(pParams); + + if (NULL == pd3d11Params || + NULL == pd3d11Params->pDevice) + { + return MFX_ERR_NOT_INITIALIZED; + } + + m_initParams = *pd3d11Params; + MSDK_SAFE_RELEASE(m_pDeviceContext); + pd3d11Params->pDevice->GetImmediateContext(&m_pDeviceContext); + + return MFX_ERR_NONE; +} + +mfxStatus D3D11FrameAllocator::Close() +{ + mfxStatus sts = BaseFrameAllocator::Close(); + for(referenceType i = m_resourcesByRequest.begin(); i != m_resourcesByRequest.end(); i++) + { + i->Release(); + } + m_resourcesByRequest.clear(); + m_memIdMap.clear(); + MSDK_SAFE_RELEASE(m_pDeviceContext); + return sts; +} + +mfxStatus D3D11FrameAllocator::LockFrame(mfxMemId mid, mfxFrameData *ptr) +{ + HRESULT hRes = S_OK; + + D3D11_TEXTURE2D_DESC desc = {0}; + D3D11_MAPPED_SUBRESOURCE lockedRect = {0}; + + + //check that texture exists + TextureSubResource sr = GetResourceFromMid(mid); + if (!sr.GetTexture()) + return MFX_ERR_LOCK_MEMORY; + + D3D11_MAP mapType = D3D11_MAP_READ; + UINT mapFlags = D3D11_MAP_FLAG_DO_NOT_WAIT; + { + if (NULL == sr.GetStaging()) + { + hRes = m_pDeviceContext->Map(sr.GetTexture(), sr.GetSubResource(), D3D11_MAP_READ, D3D11_MAP_FLAG_DO_NOT_WAIT, &lockedRect); + desc.Format = DXGI_FORMAT_P8; + } + else + { + sr.GetTexture()->GetDesc(&desc); + + if (DXGI_FORMAT_NV12 != desc.Format && + DXGI_FORMAT_420_OPAQUE != desc.Format && + DXGI_FORMAT_YUY2 != desc.Format && + DXGI_FORMAT_P8 != desc.Format && + DXGI_FORMAT_B8G8R8A8_UNORM != desc.Format && + DXGI_FORMAT_R16_UINT != desc.Format && + DXGI_FORMAT_R16_UNORM != desc.Format && + DXGI_FORMAT_R10G10B10A2_UNORM != desc.Format && + DXGI_FORMAT_R16G16B16A16_UNORM != desc.Format && + DXGI_FORMAT_P010 != desc.Format && + DXGI_FORMAT_AYUV != desc.Format +#if (MFX_VERSION >= 1027) + && DXGI_FORMAT_Y210 != desc.Format + && DXGI_FORMAT_Y410 != desc.Format +#endif +) + { + return MFX_ERR_LOCK_MEMORY; + } + + //coping data only in case user wants to read from stored surface + { + + if (MFXReadWriteMid(mid, MFXReadWriteMid::reuse).isRead()) + { + m_pDeviceContext->CopySubresourceRegion(sr.GetStaging(), 0, 0, 0, 0, sr.GetTexture(), sr.GetSubResource(), NULL); + } + + do + { + hRes = m_pDeviceContext->Map(sr.GetStaging(), 0, mapType, mapFlags, &lockedRect); + if (S_OK != hRes && DXGI_ERROR_WAS_STILL_DRAWING != hRes) + { + msdk_printf(MSDK_STRING("ERROR: m_pDeviceContext->Map = 0x%08lx\n"), hRes); + } + } + while (DXGI_ERROR_WAS_STILL_DRAWING == hRes); + } + + } + } + + if (FAILED(hRes)) + return MFX_ERR_LOCK_MEMORY; + + switch (desc.Format) + { + case DXGI_FORMAT_P010: + case DXGI_FORMAT_NV12: + ptr->Pitch = (mfxU16)lockedRect.RowPitch; + ptr->Y = (mfxU8 *)lockedRect.pData; + ptr->U = (mfxU8 *)lockedRect.pData + desc.Height * lockedRect.RowPitch; + ptr->V = (desc.Format == DXGI_FORMAT_P010) ? ptr->U + 2 : ptr->U + 1; + break; + + case DXGI_FORMAT_420_OPAQUE: // can be unsupported by standard ms guid + ptr->Pitch = (mfxU16)lockedRect.RowPitch; + ptr->Y = (mfxU8 *)lockedRect.pData; + ptr->V = ptr->Y + desc.Height * lockedRect.RowPitch; + ptr->U = ptr->V + (desc.Height * lockedRect.RowPitch) / 4; + + break; + + case DXGI_FORMAT_YUY2: + ptr->Pitch = (mfxU16)lockedRect.RowPitch; + ptr->Y = (mfxU8 *)lockedRect.pData; + ptr->U = ptr->Y + 1; + ptr->V = ptr->Y + 3; + + break; + + case DXGI_FORMAT_P8 : + ptr->Pitch = (mfxU16)lockedRect.RowPitch; + ptr->Y = (mfxU8 *)lockedRect.pData; + ptr->U = 0; + ptr->V = 0; + + break; + + case DXGI_FORMAT_AYUV: + ptr->Pitch = (mfxU16)lockedRect.RowPitch; + ptr->V = (mfxU8 *)lockedRect.pData; + ptr->U = ptr->V + 1; + ptr->Y = ptr->V + 2; + ptr->A = ptr->V + 3; + break; + + case DXGI_FORMAT_B8G8R8A8_UNORM: + ptr->Pitch = (mfxU16)lockedRect.RowPitch; + ptr->B = (mfxU8 *)lockedRect.pData; + ptr->G = ptr->B + 1; + ptr->R = ptr->B + 2; + ptr->A = ptr->B + 3; + + break; + case DXGI_FORMAT_R10G10B10A2_UNORM : + ptr->Pitch = (mfxU16)lockedRect.RowPitch; + ptr->B = (mfxU8 *)lockedRect.pData; + ptr->G = ptr->B + 1; + ptr->R = ptr->B + 2; + ptr->A = ptr->B + 3; + + break; + case DXGI_FORMAT_R16G16B16A16_UNORM: + ptr->V16 = (mfxU16*)lockedRect.pData; + ptr->U16 = ptr->V16 + 1; + ptr->Y16 = ptr->V16 + 2; + ptr->A = (mfxU8*)(ptr->V16 + 3); + ptr->PitchHigh = (mfxU16)((mfxU32)lockedRect.RowPitch / (1 << 16)); + ptr->PitchLow = (mfxU16)((mfxU32)lockedRect.RowPitch % (1 << 16)); + break; + case DXGI_FORMAT_R16_UNORM : + case DXGI_FORMAT_R16_UINT : + ptr->Pitch = (mfxU16)lockedRect.RowPitch; + ptr->Y16 = (mfxU16 *)lockedRect.pData; + ptr->U16 = 0; + ptr->V16 = 0; + + break; +#if (MFX_VERSION >= 1027) + case DXGI_FORMAT_Y210: + ptr->PitchHigh = (mfxU16)(lockedRect.RowPitch / (1 << 16)); + ptr->PitchLow = (mfxU16)(lockedRect.RowPitch % (1 << 16)); + ptr->Y16 = (mfxU16 *)lockedRect.pData; + ptr->U16 = ptr->Y16 + 1; + ptr->V16 = ptr->Y16 + 3; + + break; + + case DXGI_FORMAT_Y410: + ptr->PitchHigh = (mfxU16)(lockedRect.RowPitch / (1 << 16)); + ptr->PitchLow = (mfxU16)(lockedRect.RowPitch % (1 << 16)); + ptr->Y410 = (mfxY410 *)lockedRect.pData; + ptr->Y = 0; + ptr->V = 0; + ptr->A = 0; + + break; +#endif + + default: + + return MFX_ERR_LOCK_MEMORY; + } + + return MFX_ERR_NONE; +} + +mfxStatus D3D11FrameAllocator::UnlockFrame(mfxMemId mid, mfxFrameData *ptr) +{ + //check that texture exists + TextureSubResource sr = GetResourceFromMid(mid); + if (!sr.GetTexture()) + return MFX_ERR_LOCK_MEMORY; + + if (NULL == sr.GetStaging()) + { + m_pDeviceContext->Unmap(sr.GetTexture(), sr.GetSubResource()); + } + else + { + m_pDeviceContext->Unmap(sr.GetStaging(), 0); + //only if user wrote something to texture + if (MFXReadWriteMid(mid, MFXReadWriteMid::reuse).isWrite()) + { + m_pDeviceContext->CopySubresourceRegion(sr.GetTexture(), sr.GetSubResource(), 0, 0, 0, sr.GetStaging(), 0, NULL); + } + } + + if (ptr) + { + ptr->Pitch=0; + ptr->U=ptr->V=ptr->Y=0; + ptr->A=ptr->R=ptr->G=ptr->B=0; + } + + return MFX_ERR_NONE; +} + + +mfxStatus D3D11FrameAllocator::GetFrameHDL(mfxMemId mid, mfxHDL *handle) +{ + if (NULL == handle) + return MFX_ERR_INVALID_HANDLE; + + TextureSubResource sr = GetResourceFromMid(mid); + + if (!sr.GetTexture()) + return MFX_ERR_INVALID_HANDLE; + + mfxHDLPair *pPair = (mfxHDLPair*)handle; + + pPair->first = sr.GetTexture(); + pPair->second = (mfxHDL)(UINT_PTR)sr.GetSubResource(); + + return MFX_ERR_NONE; +} + +mfxStatus D3D11FrameAllocator::CheckRequestType(mfxFrameAllocRequest *request) +{ + mfxStatus sts = BaseFrameAllocator::CheckRequestType(request); + if (MFX_ERR_NONE != sts) + return sts; + + if ((request->Type & (MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET | MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET)) != 0) + return MFX_ERR_NONE; + else + return MFX_ERR_UNSUPPORTED; +} + +mfxStatus D3D11FrameAllocator::ReleaseResponse(mfxFrameAllocResponse *response) +{ + if (NULL == response) + return MFX_ERR_NULL_PTR; + + if (response->mids && 0 != response->NumFrameActual) + { + //check whether texture exsist + TextureSubResource sr = GetResourceFromMid(response->mids[0]); + + if (!sr.GetTexture()) + return MFX_ERR_NULL_PTR; + + sr.Release(); + + //if texture is last it is possible to remove also all handles from map to reduce fragmentation + //search for allocated chunk + if (m_resourcesByRequest.end() == std::find_if(m_resourcesByRequest.begin(), m_resourcesByRequest.end(), TextureResource::isAllocated)) + { + m_resourcesByRequest.clear(); + m_memIdMap.clear(); + } + } + + return MFX_ERR_NONE; +} +mfxStatus D3D11FrameAllocator::AllocImpl(mfxFrameAllocRequest *request, mfxFrameAllocResponse *response) +{ + HRESULT hRes; + + DXGI_FORMAT colorFormat = ConverColortFormat(request->Info.FourCC); + + if (DXGI_FORMAT_UNKNOWN == colorFormat) + { + msdk_printf(MSDK_STRING("D3D11 Allocator: invalid fourcc is provided (%#X), exitting\n"),request->Info.FourCC); + return MFX_ERR_UNSUPPORTED; + } + + TextureResource newTexture; + + if (request->Info.FourCC == MFX_FOURCC_P8) + { + D3D11_BUFFER_DESC desc = { 0 }; + + desc.ByteWidth = request->Info.Width * request->Info.Height; + desc.Usage = D3D11_USAGE_STAGING; + desc.BindFlags = 0; + desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; + desc.MiscFlags = 0; + desc.StructureByteStride = 0; + + ID3D11Buffer * buffer = 0; + hRes = m_initParams.pDevice->CreateBuffer(&desc, 0, &buffer); + if (FAILED(hRes)) + return MFX_ERR_MEMORY_ALLOC; + + newTexture.textures.push_back(reinterpret_cast(buffer)); + } + else + { + D3D11_TEXTURE2D_DESC desc = {0}; + + desc.Width = request->Info.Width; + desc.Height = request->Info.Height; + + desc.MipLevels = 1; + //number of subresources is 1 in case of not single texture + desc.ArraySize = m_initParams.bUseSingleTexture ? request->NumFrameSuggested : 1; + desc.Format = ConverColortFormat(request->Info.FourCC); + desc.SampleDesc.Count = 1; + desc.Usage = D3D11_USAGE_DEFAULT; + desc.MiscFlags = m_initParams.uncompressedResourceMiscFlags | D3D11_RESOURCE_MISC_SHARED; +#if (MFX_VERSION >= 1025) + if ((request->Type&MFX_MEMTYPE_VIDEO_MEMORY_ENCODER_TARGET) && (request->Type & MFX_MEMTYPE_INTERNAL_FRAME)) + { + desc.BindFlags = D3D11_BIND_DECODER | D3D11_BIND_VIDEO_ENCODER; + } + else +#endif + desc.BindFlags = D3D11_BIND_DECODER; + + if ( (MFX_MEMTYPE_FROM_VPPIN & request->Type) && (DXGI_FORMAT_YUY2 == desc.Format) || + (DXGI_FORMAT_B8G8R8A8_UNORM == desc.Format) || + (DXGI_FORMAT_R10G10B10A2_UNORM == desc.Format) || + (DXGI_FORMAT_R16G16B16A16_UNORM == desc.Format) ) + { + desc.BindFlags = D3D11_BIND_RENDER_TARGET; + if (desc.ArraySize > 2) + return MFX_ERR_MEMORY_ALLOC; + } + + if ( (MFX_MEMTYPE_FROM_VPPOUT & request->Type) || + (MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET & request->Type)) + { + desc.BindFlags = D3D11_BIND_RENDER_TARGET; + if (desc.ArraySize > 2) + return MFX_ERR_MEMORY_ALLOC; + } + + if(request->Type&MFX_MEMTYPE_SHARED_RESOURCE) + { + desc.BindFlags |= D3D11_BIND_SHADER_RESOURCE; + desc.MiscFlags = D3D11_RESOURCE_MISC_SHARED; + } + + if( DXGI_FORMAT_P8 == desc.Format ) + { + desc.BindFlags = 0; + } + + ID3D11Texture2D* pTexture2D; + + for(size_t i = 0; i < request->NumFrameSuggested / desc.ArraySize; i++) + { + hRes = m_initParams.pDevice->CreateTexture2D(&desc, NULL, &pTexture2D); + + if (FAILED(hRes)) + { + msdk_printf(MSDK_STRING("CreateTexture2D(%lld) failed, hr = 0x%08lx\n"), (long long)i, hRes); + return MFX_ERR_MEMORY_ALLOC; + } + newTexture.textures.push_back(pTexture2D); + } + + desc.ArraySize = 1; + desc.Usage = D3D11_USAGE_STAGING; + desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; + desc.BindFlags = 0; + desc.MiscFlags = 0; + + for(size_t i = 0; i < request->NumFrameSuggested; i++) + { + hRes = m_initParams.pDevice->CreateTexture2D(&desc, NULL, &pTexture2D); + + if (FAILED(hRes)) + { + msdk_printf(MSDK_STRING("Create staging texture(%lld) failed hr = 0x%X\n"), (long long)i, (unsigned int)hRes); + return MFX_ERR_MEMORY_ALLOC; + } + newTexture.stagingTexture.push_back(pTexture2D); + } + } + + // mapping to self created handles array, starting from zero or from last assigned handle + 1 + sequence seq_initializer(m_resourcesByRequest.empty() ? 0 : m_resourcesByRequest.back().outerMids.back()); + + //incrementing starting index + //1. 0(NULL) is invalid memid + //2. back is last index not new one + seq_initializer(); + + std::generate_n(std::back_inserter(newTexture.outerMids), request->NumFrameSuggested, seq_initializer); + + //saving texture resources + m_resourcesByRequest.push_back(newTexture); + + //providing pointer to mids externally + response->mids = &m_resourcesByRequest.back().outerMids.front(); + response->NumFrameActual = request->NumFrameSuggested; + + //iterator prior end() + std::list ::iterator it_last = m_resourcesByRequest.end(); + //fill map + std::fill_n(std::back_inserter(m_memIdMap), request->NumFrameSuggested, --it_last); + + return MFX_ERR_NONE; +} + +DXGI_FORMAT D3D11FrameAllocator::ConverColortFormat(mfxU32 fourcc) +{ + switch (fourcc) + { + case MFX_FOURCC_NV12: + return DXGI_FORMAT_NV12; + + case MFX_FOURCC_YUY2: + return DXGI_FORMAT_YUY2; + + case MFX_FOURCC_RGB4: + return DXGI_FORMAT_B8G8R8A8_UNORM; + + case MFX_FOURCC_P8: + case MFX_FOURCC_P8_TEXTURE: + return DXGI_FORMAT_P8; + + case MFX_FOURCC_ARGB16: + case MFX_FOURCC_ABGR16: + return DXGI_FORMAT_R16G16B16A16_UNORM; + + case MFX_FOURCC_P010: + return DXGI_FORMAT_P010; + + case MFX_FOURCC_A2RGB10: + return DXGI_FORMAT_R10G10B10A2_UNORM; + + case DXGI_FORMAT_AYUV: + case MFX_FOURCC_AYUV: + return DXGI_FORMAT_AYUV; + +#if (MFX_VERSION >= 1027) + case MFX_FOURCC_Y210: + return DXGI_FORMAT_Y210; + case MFX_FOURCC_Y410: + return DXGI_FORMAT_Y410; +#endif + default: + return DXGI_FORMAT_UNKNOWN; + } +} + +#endif // #if MFX_D3D11_SUPPORT +#endif // #if defined(_WIN32) || defined(_WIN64) diff --git a/samples/sample_common/src/d3d11_device.cpp b/samples/sample_common/src/d3d11_device.cpp index 9f1a77bc3..37a433fb2 100644 --- a/samples/sample_common/src/d3d11_device.cpp +++ b/samples/sample_common/src/d3d11_device.cpp @@ -19,3 +19,377 @@ or https://software.intel.com/en-us/media-client-solutions-support. #include "mfx_samples_config.h" +#if defined(_WIN32) || defined(_WIN64) + +#include "sample_defs.h" + +#if MFX_D3D11_SUPPORT + +#include "d3d11_device.h" + +CD3D11Device::CD3D11Device(): + m_nViews(0), + m_bDefaultStereoEnabled(FALSE), + m_bIsA2rgb10(FALSE), + m_HandleWindow(NULL) +{ +} + +CD3D11Device::~CD3D11Device() +{ + Close(); +} + +mfxStatus CD3D11Device::FillSCD(mfxHDL hWindow, DXGI_SWAP_CHAIN_DESC& scd) +{ + scd.Windowed = TRUE; + scd.OutputWindow = (HWND)hWindow; + scd.SampleDesc.Count = 1; + scd.BufferDesc.Format = (m_bIsA2rgb10) ? DXGI_FORMAT_R10G10B10A2_UNORM : DXGI_FORMAT_B8G8R8A8_UNORM; + scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; + scd.BufferCount = 1; + + return MFX_ERR_NONE; +} + +mfxStatus CD3D11Device::FillSCD1(DXGI_SWAP_CHAIN_DESC1& scd1) +{ + scd1.Width = 0; // Use automatic sizing. + scd1.Height = 0; + scd1.Format = (m_bIsA2rgb10) ? DXGI_FORMAT_R10G10B10A2_UNORM : DXGI_FORMAT_B8G8R8A8_UNORM; + scd1.Stereo = m_nViews == 2 ? TRUE : FALSE; + scd1.SampleDesc.Count = 1; // Don't use multi-sampling. + scd1.SampleDesc.Quality = 0; + scd1.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; + scd1.BufferCount = 2; // Use double buffering to minimize latency. + scd1.Scaling = DXGI_SCALING_STRETCH; + scd1.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; + scd1.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; + + return MFX_ERR_NONE; +} + +mfxStatus CD3D11Device::Init( + mfxHDL hWindow, + mfxU16 nViews, + mfxU32 nAdapterNum) +{ + m_HandleWindow = (HWND)hWindow; + mfxStatus sts = MFX_ERR_NONE; + HRESULT hres = S_OK; + m_nViews = nViews; + if (2 < nViews) + return MFX_ERR_UNSUPPORTED; + m_bDefaultStereoEnabled = FALSE; + + static D3D_FEATURE_LEVEL FeatureLevels[] = { + D3D_FEATURE_LEVEL_11_1, + D3D_FEATURE_LEVEL_11_0, + D3D_FEATURE_LEVEL_10_1, + D3D_FEATURE_LEVEL_10_0 + }; + D3D_FEATURE_LEVEL pFeatureLevelsOut; + + hres = CreateDXGIFactory(__uuidof(IDXGIFactory2), (void**)(&m_pDXGIFactory) ); + if (FAILED(hres)) + return MFX_ERR_DEVICE_FAILED; + + if (m_nViews == 2) + { + hres = m_pDXGIFactory->QueryInterface(__uuidof(IDXGIDisplayControl), (void **)&m_pDisplayControl); + if (FAILED(hres)) + return MFX_ERR_DEVICE_FAILED; + + m_bDefaultStereoEnabled = m_pDisplayControl->IsStereoEnabled(); + if (!m_bDefaultStereoEnabled) + m_pDisplayControl->SetStereoEnabled(TRUE); + } + + hres = m_pDXGIFactory->EnumAdapters(nAdapterNum,&m_pAdapter); + if (FAILED(hres)) + return MFX_ERR_DEVICE_FAILED; + + hres = D3D11CreateDevice(m_pAdapter , + D3D_DRIVER_TYPE_UNKNOWN, + NULL, + 0, + FeatureLevels, + MSDK_ARRAY_LEN(FeatureLevels), + D3D11_SDK_VERSION, + &m_pD3D11Device, + &pFeatureLevelsOut, + &m_pD3D11Ctx); + + if (FAILED(hres)) + return MFX_ERR_DEVICE_FAILED; + + m_pDXGIDev = m_pD3D11Device; + m_pDX11VideoDevice = m_pD3D11Device; + m_pVideoContext = m_pD3D11Ctx; + + MSDK_CHECK_POINTER(m_pDXGIDev.p, MFX_ERR_NULL_PTR); + MSDK_CHECK_POINTER(m_pDX11VideoDevice.p, MFX_ERR_NULL_PTR); + MSDK_CHECK_POINTER(m_pVideoContext.p, MFX_ERR_NULL_PTR); + + // turn on multithreading for the Context + CComQIPtr p_mt(m_pVideoContext); + + if (p_mt) + p_mt->SetMultithreadProtected(true); + else + return MFX_ERR_DEVICE_FAILED; + + // create swap chain only for rendering use case (hWindow != 0) + if (hWindow) + { + MSDK_CHECK_POINTER(m_pDXGIFactory.p, MFX_ERR_NULL_PTR); + DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {0}; + + sts = FillSCD1(swapChainDesc); + MSDK_CHECK_STATUS(sts, "FillSCD1 failed"); + + hres = m_pDXGIFactory->CreateSwapChainForHwnd(m_pD3D11Device, + (HWND)hWindow, + &swapChainDesc, + NULL, + NULL, + reinterpret_cast(&m_pSwapChain)); + if (FAILED(hres)) + return MFX_ERR_DEVICE_FAILED; + } + + return sts; +} + +mfxStatus CD3D11Device::CreateVideoProcessor(mfxFrameSurface1 * pSrf) +{ + HRESULT hres = S_OK; + + if (m_VideoProcessorEnum.p || NULL == pSrf) + return MFX_ERR_NONE; + + //create video processor + D3D11_VIDEO_PROCESSOR_CONTENT_DESC ContentDesc; + MSDK_ZERO_MEMORY( ContentDesc ); + + ContentDesc.InputFrameFormat = D3D11_VIDEO_FRAME_FORMAT_PROGRESSIVE; + ContentDesc.InputFrameRate.Numerator = 30000; + ContentDesc.InputFrameRate.Denominator = 1000; + ContentDesc.InputWidth = pSrf->Info.CropW; + ContentDesc.InputHeight = pSrf->Info.CropH; + ContentDesc.OutputWidth = pSrf->Info.CropW; + ContentDesc.OutputHeight = pSrf->Info.CropH; + ContentDesc.OutputFrameRate.Numerator = 30000; + ContentDesc.OutputFrameRate.Denominator = 1000; + + ContentDesc.Usage = D3D11_VIDEO_USAGE_PLAYBACK_NORMAL; + + hres = m_pDX11VideoDevice->CreateVideoProcessorEnumerator( &ContentDesc, &m_VideoProcessorEnum ); + if (FAILED(hres)) + return MFX_ERR_DEVICE_FAILED; + + hres = m_pDX11VideoDevice->CreateVideoProcessor( m_VideoProcessorEnum, 0, &m_pVideoProcessor ); + if (FAILED(hres)) + return MFX_ERR_DEVICE_FAILED; + + return MFX_ERR_NONE; +} + +mfxStatus CD3D11Device::Reset() +{ + // Changing video mode back to the original state + if (2 == m_nViews && !m_bDefaultStereoEnabled) + m_pDisplayControl->SetStereoEnabled(FALSE); + + MSDK_CHECK_POINTER (m_pDXGIFactory.p, MFX_ERR_NULL_PTR); + DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {0}; + + mfxStatus sts = FillSCD1(swapChainDesc); + MSDK_CHECK_STATUS(sts, "FillSCD1 failed"); + + HRESULT hres = S_OK; + hres = m_pDXGIFactory->CreateSwapChainForHwnd(m_pD3D11Device, + (HWND)m_HandleWindow, + &swapChainDesc, + NULL, + NULL, + reinterpret_cast(&m_pSwapChain)); + + if (FAILED(hres)) + { + if (swapChainDesc.Stereo) + { + MSDK_PRINT_RET_MSG(MFX_ERR_DEVICE_FAILED,"Cannot create swap chain required for rendering. Possibly stereo mode is not supported."); + } + else + { + MSDK_PRINT_RET_MSG(MFX_ERR_DEVICE_FAILED, "Cannot create swap chain required for rendering."); + } + + return MFX_ERR_DEVICE_FAILED; + } + return MFX_ERR_NONE; +} + +mfxStatus CD3D11Device::GetHandle(mfxHandleType type, mfxHDL *pHdl) +{ + if (MFX_HANDLE_D3D11_DEVICE == type) + { + *pHdl = m_pD3D11Device.p; + return MFX_ERR_NONE; + } + return MFX_ERR_UNSUPPORTED; +} + +mfxStatus CD3D11Device::SetHandle(mfxHandleType type, mfxHDL hdl) +{ + if (MFX_HANDLE_DEVICEWINDOW == type && hdl != NULL) //for render window handle + { + m_HandleWindow = (HWND)hdl; + return MFX_ERR_NONE; + } + return MFX_ERR_UNSUPPORTED; +} + +mfxStatus CD3D11Device::RenderFrame(mfxFrameSurface1 * pSrf, mfxFrameAllocator * pAlloc) +{ + HRESULT hres = S_OK; + mfxStatus sts; + + sts = CreateVideoProcessor(pSrf); + MSDK_CHECK_STATUS(sts, "CreateVideoProcessor failed"); + + hres = m_pSwapChain->GetBuffer(0, __uuidof( ID3D11Texture2D ), (void**)&m_pDXGIBackBuffer.p); + if (FAILED(hres)) + return MFX_ERR_DEVICE_FAILED; + + D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC OutputViewDesc; + if (2 == m_nViews) + { + m_pVideoContext->VideoProcessorSetStreamStereoFormat(m_pVideoProcessor, 0, TRUE,D3D11_VIDEO_PROCESSOR_STEREO_FORMAT_SEPARATE, + TRUE, TRUE, D3D11_VIDEO_PROCESSOR_STEREO_FLIP_NONE, NULL); + m_pVideoContext->VideoProcessorSetOutputStereoMode(m_pVideoProcessor,TRUE); + + OutputViewDesc.ViewDimension = D3D11_VPOV_DIMENSION_TEXTURE2DARRAY; + OutputViewDesc.Texture2DArray.ArraySize = 2; + OutputViewDesc.Texture2DArray.MipSlice = 0; + OutputViewDesc.Texture2DArray.FirstArraySlice = 0; + } + else + { + OutputViewDesc.ViewDimension = D3D11_VPOV_DIMENSION_TEXTURE2D; + OutputViewDesc.Texture2D.MipSlice = 0; + } + + if (1 == m_nViews || 0 == pSrf->Info.FrameId.ViewId) + { + hres = m_pDX11VideoDevice->CreateVideoProcessorOutputView( + m_pDXGIBackBuffer, + m_VideoProcessorEnum, + &OutputViewDesc, + &m_pOutputView.p ); + if (FAILED(hres)) + return MFX_ERR_DEVICE_FAILED; + } + + D3D11_VIDEO_PROCESSOR_INPUT_VIEW_DESC InputViewDesc; + InputViewDesc.FourCC = 0; + InputViewDesc.ViewDimension = D3D11_VPIV_DIMENSION_TEXTURE2D; + InputViewDesc.Texture2D.MipSlice = 0; + InputViewDesc.Texture2D.ArraySlice = 0; + + mfxHDLPair pair = {NULL}; + sts = pAlloc->GetHDL(pAlloc->pthis, pSrf->Data.MemId, (mfxHDL*)&pair); + MSDK_CHECK_STATUS(sts, "pAlloc->GetHDL failed"); + + ID3D11Texture2D *pRTTexture2D = reinterpret_cast(pair.first); + D3D11_TEXTURE2D_DESC RTTexture2DDesc; + + if(!m_pTempTexture && m_nViews == 2) + { + pRTTexture2D->GetDesc(&RTTexture2DDesc); + hres = m_pD3D11Device->CreateTexture2D(&RTTexture2DDesc,NULL,&m_pTempTexture.p); + if (FAILED(hres)) + return MFX_ERR_DEVICE_FAILED; + } + + // Creating input views for left and righ eyes + if (1 == m_nViews) + { + hres = m_pDX11VideoDevice->CreateVideoProcessorInputView( + pRTTexture2D, + m_VideoProcessorEnum, + &InputViewDesc, + &m_pInputViewLeft.p ); + + } + else if (2 == m_nViews && 0 == pSrf->Info.FrameId.ViewId) + { + m_pD3D11Ctx->CopyResource(m_pTempTexture,pRTTexture2D); + hres = m_pDX11VideoDevice->CreateVideoProcessorInputView( + m_pTempTexture, + m_VideoProcessorEnum, + &InputViewDesc, + &m_pInputViewLeft.p ); + } + else + { + hres = m_pDX11VideoDevice->CreateVideoProcessorInputView( + pRTTexture2D, + m_VideoProcessorEnum, + &InputViewDesc, + &m_pInputViewRight.p ); + } + if (FAILED(hres)) + return MFX_ERR_DEVICE_FAILED; + + // NV12 surface to RGB backbuffer + RECT rect = {0}; + rect.right = pSrf->Info.CropW; + rect.bottom = pSrf->Info.CropH; + + D3D11_VIDEO_PROCESSOR_STREAM StreamData; + + if (1 == m_nViews || pSrf->Info.FrameId.ViewId == 1) + { + StreamData.Enable = TRUE; + StreamData.OutputIndex = 0; + StreamData.InputFrameOrField = 0; + StreamData.PastFrames = 0; + StreamData.FutureFrames = 0; + StreamData.ppPastSurfaces = NULL; + StreamData.ppFutureSurfaces = NULL; + StreamData.pInputSurface = m_pInputViewLeft; + StreamData.ppPastSurfacesRight = NULL; + StreamData.ppFutureSurfacesRight = NULL; + StreamData.pInputSurfaceRight = m_nViews == 2 ? m_pInputViewRight : NULL; + + m_pVideoContext->VideoProcessorSetStreamSourceRect(m_pVideoProcessor, 0, true, &rect); + m_pVideoContext->VideoProcessorSetStreamFrameFormat( m_pVideoProcessor, 0, D3D11_VIDEO_FRAME_FORMAT_PROGRESSIVE); + hres = m_pVideoContext->VideoProcessorBlt( m_pVideoProcessor, m_pOutputView, 0, 1, &StreamData ); + if (FAILED(hres)) + return MFX_ERR_DEVICE_FAILED; + } + + if (1 == m_nViews || 1 == pSrf->Info.FrameId.ViewId) + { + DXGI_PRESENT_PARAMETERS parameters = {0}; + hres = m_pSwapChain->Present1(0, 0, ¶meters); + if (FAILED(hres)) + return MFX_ERR_DEVICE_FAILED; + } + + return MFX_ERR_NONE; +} + +void CD3D11Device::Close() +{ + // Changing video mode back to the original state + if (2 == m_nViews && !m_bDefaultStereoEnabled) + m_pDisplayControl->SetStereoEnabled(FALSE); + + m_HandleWindow = NULL; +} + +#endif // #if MFX_D3D11_SUPPORT +#endif // #if defined(_WIN32) || defined(_WIN64) diff --git a/samples/sample_common/src/d3d_allocator.cpp b/samples/sample_common/src/d3d_allocator.cpp index 39ffb2854..5b08bbf22 100644 --- a/samples/sample_common/src/d3d_allocator.cpp +++ b/samples/sample_common/src/d3d_allocator.cpp @@ -1,5 +1,5 @@ /******************************************************************************\ -Copyright (c) 2005-2017, Intel Corporation +Copyright (c) 2005-2018, Intel Corporation All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: @@ -20,3 +20,415 @@ or https://software.intel.com/en-us/media-client-solutions-support. #include "mfx_samples_config.h" #include "sample_defs.h" +#if defined(_WIN32) || defined(_WIN64) + +#include +#include +#include +#include + +#include "d3d_allocator.h" + +#define D3DFMT_NV12 (D3DFORMAT)MAKEFOURCC('N','V','1','2') +#define D3DFMT_YV12 (D3DFORMAT)MAKEFOURCC('Y','V','1','2') +#define D3DFMT_NV16 (D3DFORMAT)MAKEFOURCC('N','V','1','6') +#define D3DFMT_P010 (D3DFORMAT)MAKEFOURCC('P','0','1','0') +#define D3DFMT_P210 (D3DFORMAT)MAKEFOURCC('P','2','1','0') +#define D3DFMT_IMC3 (D3DFORMAT)MAKEFOURCC('I','M','C','3') +#define D3DFMT_AYUV (D3DFORMAT)MAKEFOURCC('A','Y','U','V') +#if (MFX_VERSION >= 1027) +#define D3DFMT_Y210 (D3DFORMAT)MAKEFOURCC('Y','2','1','0') +#define D3DFMT_Y410 (D3DFORMAT)MAKEFOURCC('Y','4','1','0') +#endif + +#define MFX_FOURCC_IMC3 (MFX_MAKEFOURCC('I','M','C','3')) // This line should be moved into mfxstructures.h in new API version + +D3DFORMAT ConvertMfxFourccToD3dFormat(mfxU32 fourcc) +{ + switch (fourcc) + { + case MFX_FOURCC_NV12: + return D3DFMT_NV12; + case MFX_FOURCC_YV12: + return D3DFMT_YV12; + case MFX_FOURCC_NV16: + return D3DFMT_NV16; + case MFX_FOURCC_YUY2: + return D3DFMT_YUY2; + case MFX_FOURCC_RGB3: + return D3DFMT_R8G8B8; + case MFX_FOURCC_RGB4: + return D3DFMT_A8R8G8B8; + case MFX_FOURCC_P8: + return D3DFMT_P8; + case MFX_FOURCC_P010: + return D3DFMT_P010; + case MFX_FOURCC_AYUV: + return D3DFMT_AYUV; + case MFX_FOURCC_P210: + return D3DFMT_P210; +#if (MFX_VERSION >= 1027) + case MFX_FOURCC_Y210: + return D3DFMT_Y210; + case MFX_FOURCC_Y410: + return D3DFMT_Y410; +#endif + case MFX_FOURCC_A2RGB10: + return D3DFMT_A2R10G10B10; + case MFX_FOURCC_ABGR16: + case MFX_FOURCC_ARGB16: + return D3DFMT_A16B16G16R16; + case MFX_FOURCC_IMC3: + return D3DFMT_IMC3; + default: + return D3DFMT_UNKNOWN; + } +} + +D3DFrameAllocator::D3DFrameAllocator() +: m_decoderService(0), m_processorService(0), m_hDecoder(0), m_hProcessor(0), m_manager(0), m_surfaceUsage(0) +{ +} + +D3DFrameAllocator::~D3DFrameAllocator() +{ + Close(); + for (unsigned i = 0; i < m_midsAllocated.size(); i++) + MSDK_SAFE_FREE(m_midsAllocated[i]); +} + +mfxStatus D3DFrameAllocator::Init(mfxAllocatorParams *pParams) +{ + D3DAllocatorParams *pd3dParams = 0; + pd3dParams = dynamic_cast(pParams); + if (!pd3dParams) + return MFX_ERR_NOT_INITIALIZED; + + m_manager = pd3dParams->pManager; + m_surfaceUsage = pd3dParams->surfaceUsage; + + return MFX_ERR_NONE; +} + +mfxStatus D3DFrameAllocator::Close() +{ + if (m_manager && m_hDecoder) + { + m_manager->CloseDeviceHandle(m_hDecoder); + m_manager = 0; + m_hDecoder = 0; + } + + if (m_manager && m_hProcessor) + { + m_manager->CloseDeviceHandle(m_hProcessor); + m_manager = 0; + m_hProcessor = 0; + } + + return BaseFrameAllocator::Close(); +} + +mfxStatus D3DFrameAllocator::LockFrame(mfxMemId mid, mfxFrameData *ptr) +{ + if (!ptr || !mid) + return MFX_ERR_NULL_PTR; + + mfxHDLPair *dxmid = (mfxHDLPair*)mid; + IDirect3DSurface9 *pSurface = static_cast(dxmid->first); + if (pSurface == 0) + return MFX_ERR_INVALID_HANDLE; + + D3DSURFACE_DESC desc; + HRESULT hr = pSurface->GetDesc(&desc); + if (FAILED(hr)) + return MFX_ERR_LOCK_MEMORY; + + if (desc.Format != D3DFMT_NV12 && + desc.Format != D3DFMT_YV12 && + desc.Format != D3DFMT_YUY2 && + desc.Format != D3DFMT_R8G8B8 && + desc.Format != D3DFMT_A8R8G8B8 && + desc.Format != D3DFMT_P8 && + desc.Format != D3DFMT_P010 && + desc.Format != D3DFMT_A2R10G10B10 && + desc.Format != D3DFMT_A16B16G16R16 && + desc.Format != D3DFMT_IMC3 && + desc.Format != D3DFMT_AYUV +#if (MFX_VERSION >= 1027) + && desc.Format != D3DFMT_Y210 +#endif + ) + return MFX_ERR_LOCK_MEMORY; + + D3DLOCKED_RECT locked; + + hr = pSurface->LockRect(&locked, 0, D3DLOCK_NOSYSLOCK); + if (FAILED(hr)) + return MFX_ERR_LOCK_MEMORY; + + switch ((DWORD)desc.Format) + { + case D3DFMT_NV12: + case D3DFMT_P010: + ptr->Pitch = (mfxU16)locked.Pitch; + ptr->Y = (mfxU8 *)locked.pBits; + ptr->U = (mfxU8 *)locked.pBits + desc.Height * locked.Pitch; + ptr->V = (desc.Format == D3DFMT_P010) ? ptr->U + 2 : ptr->U + 1; + break; + case D3DFMT_YV12: + ptr->Pitch = (mfxU16)locked.Pitch; + ptr->Y = (mfxU8 *)locked.pBits; + ptr->V = ptr->Y + desc.Height * locked.Pitch; + ptr->U = ptr->V + (desc.Height * locked.Pitch) / 4; + break; + case D3DFMT_YUY2: + ptr->Pitch = (mfxU16)locked.Pitch; + ptr->Y = (mfxU8 *)locked.pBits; + ptr->U = ptr->Y + 1; + ptr->V = ptr->Y + 3; + break; + case D3DFMT_R8G8B8: + ptr->Pitch = (mfxU16)locked.Pitch; + ptr->B = (mfxU8 *)locked.pBits; + ptr->G = ptr->B + 1; + ptr->R = ptr->B + 2; + break; + case D3DFMT_A8R8G8B8: + case D3DFMT_A2R10G10B10: + ptr->Pitch = (mfxU16)locked.Pitch; + ptr->B = (mfxU8 *)locked.pBits; + ptr->G = ptr->B + 1; + ptr->R = ptr->B + 2; + ptr->A = ptr->B + 3; + break; + case D3DFMT_P8: + ptr->Pitch = (mfxU16)locked.Pitch; + ptr->Y = (mfxU8 *)locked.pBits; + ptr->U = 0; + ptr->V = 0; + break; + case D3DFMT_A16B16G16R16: + ptr->V16 = (mfxU16*)locked.pBits; + ptr->U16 = ptr->V16 + 1; + ptr->Y16 = ptr->V16 + 2; + ptr->A = (mfxU8*)(ptr->V16 + 3); + ptr->PitchHigh = (mfxU16)((mfxU32)locked.Pitch / (1 << 16)); + ptr->PitchLow = (mfxU16)((mfxU32)locked.Pitch % (1 << 16)); + break; + case D3DFMT_IMC3: + ptr->Pitch = (mfxU16)locked.Pitch; + ptr->Y = (mfxU8 *)locked.pBits; + ptr->V = ptr->Y + desc.Height * locked.Pitch; + ptr->U = ptr->Y + desc.Height * locked.Pitch *2; + break; + case D3DFMT_AYUV: + ptr->Pitch = (mfxU16)locked.Pitch; + ptr->V = (mfxU8 *)locked.pBits; + ptr->U = ptr->V + 1; + ptr->Y = ptr->V + 2; + ptr->A = ptr->V + 3; + break; +#if (MFX_VERSION >= 1027) + case D3DFMT_Y210: + ptr->Pitch = (mfxU16)locked.Pitch; + ptr->Y16 = (mfxU16 *)locked.pBits; + ptr->U16 = ptr->Y16 + 1; + ptr->V16 = ptr->Y16 + 3; + break; + case D3DFMT_Y410: + ptr->Pitch = (mfxU16)locked.Pitch; + ptr->Y410 = (mfxY410 *)locked.pBits; + ptr->Y = 0; + ptr->V = 0; + ptr->A = 0; + break; +#endif + } + + return MFX_ERR_NONE; +} + +mfxStatus D3DFrameAllocator::UnlockFrame(mfxMemId mid, mfxFrameData *ptr) +{ + if (!mid) + return MFX_ERR_NULL_PTR; + + mfxHDLPair *dxmid = (mfxHDLPair*)mid; + IDirect3DSurface9 *pSurface = static_cast(dxmid->first); + if (pSurface == 0) + return MFX_ERR_INVALID_HANDLE; + + pSurface->UnlockRect(); + + if (NULL != ptr) + { + ptr->Pitch = 0; + ptr->Y = 0; + ptr->U = 0; + ptr->V = 0; + } + + return MFX_ERR_NONE; +} + +mfxStatus D3DFrameAllocator::GetFrameHDL(mfxMemId mid, mfxHDL * handle) +{ + if (!mid || !handle) + return MFX_ERR_NULL_PTR; + + mfxHDLPair *dxMid = (mfxHDLPair*)mid; + *handle = dxMid->first; + return MFX_ERR_NONE; +} + +mfxStatus D3DFrameAllocator::CheckRequestType(mfxFrameAllocRequest *request) +{ + mfxStatus sts = BaseFrameAllocator::CheckRequestType(request); + if (MFX_ERR_NONE != sts) + return sts; + + if ((request->Type & (MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET | MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET)) != 0) + return MFX_ERR_NONE; + else + return MFX_ERR_UNSUPPORTED; +} + +mfxStatus D3DFrameAllocator::ReleaseResponse(mfxFrameAllocResponse *response) +{ + if (!response) + return MFX_ERR_NULL_PTR; + + mfxStatus sts = MFX_ERR_NONE; + + if (response->mids) { + for (mfxU32 i = 0; i < response->NumFrameActual; i++) { + if (response->mids[i]) { + mfxHDLPair *dxMids = (mfxHDLPair*)response->mids[i]; + if (dxMids->first) + { + static_cast(dxMids->first)->Release(); + } + MSDK_SAFE_FREE(dxMids); + } + } + } + + return sts; +} + +mfxStatus D3DFrameAllocator::AllocImpl(mfxFrameAllocRequest *request, mfxFrameAllocResponse *response) +{ + HRESULT hr; + + MSDK_CHECK_POINTER(request, MFX_ERR_NULL_PTR); + if (request->NumFrameSuggested == 0) + return MFX_ERR_UNKNOWN; + + D3DFORMAT format = ConvertMfxFourccToD3dFormat(request->Info.FourCC); + + if (format == D3DFMT_UNKNOWN) + { + msdk_printf(MSDK_STRING("D3D Allocator: invalid fourcc is provided (%#X), exitting\n"),request->Info.FourCC); + return MFX_ERR_UNSUPPORTED; + } + + DWORD target; + + if (MFX_MEMTYPE_DXVA2_DECODER_TARGET & request->Type) + { + target = DXVA2_VideoDecoderRenderTarget; + } + else if (MFX_MEMTYPE_DXVA2_PROCESSOR_TARGET & request->Type) + { + target = DXVA2_VideoProcessorRenderTarget; + } + else + return MFX_ERR_UNSUPPORTED; + + IDirectXVideoAccelerationService* videoService = NULL; + + if (target == DXVA2_VideoProcessorRenderTarget) { + if (!m_hProcessor) { + hr = m_manager->OpenDeviceHandle(&m_hProcessor); + if (FAILED(hr)) + return MFX_ERR_MEMORY_ALLOC; + + hr = m_manager->GetVideoService(m_hProcessor, IID_IDirectXVideoProcessorService, (void**)&m_processorService); + if (FAILED(hr)) + return MFX_ERR_MEMORY_ALLOC; + } + videoService = m_processorService; + } + else { + if (!m_hDecoder) + { + hr = m_manager->OpenDeviceHandle(&m_hDecoder); + if (FAILED(hr)) + return MFX_ERR_MEMORY_ALLOC; + + hr = m_manager->GetVideoService(m_hDecoder, IID_IDirectXVideoDecoderService, (void**)&m_decoderService); + if (FAILED(hr)) + return MFX_ERR_MEMORY_ALLOC; + } + videoService = m_decoderService; + } + + mfxHDLPair **dxMidPtrs = (mfxHDLPair**)calloc(request->NumFrameSuggested, sizeof(mfxHDLPair*)); + if (!dxMidPtrs) + return MFX_ERR_MEMORY_ALLOC; + + for (int i = 0; i < request->NumFrameSuggested; i++) + { + dxMidPtrs[i] = (mfxHDLPair*)calloc(1, sizeof(mfxHDLPair)); + if (!dxMidPtrs[i]) + { + DeallocateMids(dxMidPtrs, i); + return MFX_ERR_MEMORY_ALLOC; + } + } + + response->mids = (mfxMemId*)dxMidPtrs; + response->NumFrameActual = request->NumFrameSuggested; + + if (request->Type & MFX_MEMTYPE_EXTERNAL_FRAME) { + for (int i = 0; i < request->NumFrameSuggested; i++) { + hr = videoService->CreateSurface(request->Info.Width, request->Info.Height, 0, format, + D3DPOOL_DEFAULT, m_surfaceUsage, target, (IDirect3DSurface9**)&dxMidPtrs[i]->first, &dxMidPtrs[i]->second); + if (FAILED(hr)) { + ReleaseResponse(response); + return MFX_ERR_MEMORY_ALLOC; + } + } + } else { + std::unique_ptr dxSrf(new (std::nothrow) IDirect3DSurface9*[request->NumFrameSuggested]); + if (!dxSrf.get()) + { + DeallocateMids(dxMidPtrs, request->NumFrameSuggested); + return MFX_ERR_MEMORY_ALLOC; + } + hr = videoService->CreateSurface(request->Info.Width, request->Info.Height, request->NumFrameSuggested - 1, format, + D3DPOOL_DEFAULT, m_surfaceUsage, target, dxSrf.get(), NULL); + if (FAILED(hr)) + { + DeallocateMids(dxMidPtrs, request->NumFrameSuggested); + return MFX_ERR_MEMORY_ALLOC; + } + + for (int i = 0; i < request->NumFrameSuggested; i++) { + dxMidPtrs[i]->first = dxSrf[i]; + } + } + m_midsAllocated.push_back(dxMidPtrs); + return MFX_ERR_NONE; +} + +void D3DFrameAllocator::DeallocateMids(mfxHDLPair** pair, int n) +{ + for (int i = 0; i < n; i++) + { + MSDK_SAFE_FREE(pair[i]); + } + MSDK_SAFE_FREE(pair); +} +#endif // #if defined(_WIN32) || defined(_WIN64) diff --git a/samples/sample_common/src/d3d_device.cpp b/samples/sample_common/src/d3d_device.cpp index 9f1a77bc3..a7ca4f0e5 100644 --- a/samples/sample_common/src/d3d_device.cpp +++ b/samples/sample_common/src/d3d_device.cpp @@ -1,5 +1,5 @@ /******************************************************************************\ -Copyright (c) 2005-2018, Intel Corporation +Copyright (c) 2005-2019, Intel Corporation All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: @@ -18,4 +18,389 @@ or https://software.intel.com/en-us/media-client-solutions-support. \**********************************************************************************/ #include "mfx_samples_config.h" +#include +#if defined(WIN32) || defined(WIN64) + +//prefast singnature used in combaseapi.h +#ifndef _PREFAST_ + #pragma warning(disable:4068) +#endif + +#include "d3d_device.h" +#include "d3d_allocator.h" +#include "sample_defs.h" + +#include "atlbase.h" + +CD3D9Device::CD3D9Device() +{ + m_pD3D9 = NULL; + m_pD3DD9 = NULL; + m_pDeviceManager9 = NULL; + MSDK_ZERO_MEMORY(m_D3DPP); + m_resetToken = 0; + + m_nViews = 0; + + MSDK_ZERO_MEMORY(m_backBufferDesc); + m_pDXVAVPS = NULL; + m_pDXVAVP_Left = NULL; + m_pDXVAVP_Right = NULL; + + MSDK_ZERO_MEMORY(m_targetRect); + + MSDK_ZERO_MEMORY(m_VideoDesc); + MSDK_ZERO_MEMORY(m_BltParams); + MSDK_ZERO_MEMORY(m_Sample); + + // Initialize DXVA structures + + DXVA2_AYUVSample16 color = { + 0x8000, // Cr + 0x8000, // Cb + 0x1000, // Y + 0xffff // Alpha + }; + + DXVA2_ExtendedFormat format = { // DestFormat + DXVA2_SampleProgressiveFrame, // SampleFormat + DXVA2_VideoChromaSubsampling_MPEG2, // VideoChromaSubsampling + DXVA_NominalRange_0_255, // NominalRange + DXVA2_VideoTransferMatrix_BT709, // VideoTransferMatrix + DXVA2_VideoLighting_bright, // VideoLighting + DXVA2_VideoPrimaries_BT709, // VideoPrimaries + DXVA2_VideoTransFunc_709 // VideoTransferFunction + }; + + // init m_VideoDesc structure + MSDK_MEMCPY_VAR(m_VideoDesc.SampleFormat, &format, sizeof(DXVA2_ExtendedFormat)); + m_VideoDesc.SampleWidth = 0; + m_VideoDesc.SampleHeight = 0; + m_VideoDesc.InputSampleFreq.Numerator = 60; + m_VideoDesc.InputSampleFreq.Denominator = 1; + m_VideoDesc.OutputFrameFreq.Numerator = 60; + m_VideoDesc.OutputFrameFreq.Denominator = 1; + + // init m_BltParams structure + MSDK_MEMCPY_VAR(m_BltParams.DestFormat, &format, sizeof(DXVA2_ExtendedFormat)); + MSDK_MEMCPY_VAR(m_BltParams.BackgroundColor, &color, sizeof(DXVA2_AYUVSample16)); + + // init m_Sample structure + m_Sample.Start = 0; + m_Sample.End = 1; + m_Sample.SampleFormat = format; + m_Sample.PlanarAlpha.Fraction = 0; + m_Sample.PlanarAlpha.Value = 1; + + m_bIsA2rgb10 = FALSE; +} + +bool CD3D9Device::CheckOverlaySupport() +{ + D3DCAPS9 d3d9caps; + D3DOVERLAYCAPS d3doverlaycaps = {0}; + IDirect3D9ExOverlayExtension *d3d9overlay = NULL; + bool overlaySupported = false; + + memset(&d3d9caps, 0, sizeof(d3d9caps)); + HRESULT hr = m_pD3D9->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &d3d9caps); + if (FAILED(hr) || !(d3d9caps.Caps & D3DCAPS_OVERLAY)) + { + overlaySupported = false; + } + else + { + hr = m_pD3D9->QueryInterface(IID_PPV_ARGS(&d3d9overlay)); + if (FAILED(hr) || (d3d9overlay == NULL)) + { + overlaySupported = false; + } + else + { + hr = d3d9overlay->CheckDeviceOverlayType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, + m_D3DPP.BackBufferWidth, + m_D3DPP.BackBufferHeight, + m_D3DPP.BackBufferFormat, NULL, + D3DDISPLAYROTATION_IDENTITY, &d3doverlaycaps); + MSDK_SAFE_RELEASE(d3d9overlay); + + if (FAILED(hr)) + { + overlaySupported = false; + } + else + { + overlaySupported = true; + } + } + } + + return overlaySupported; +} + +mfxStatus CD3D9Device::FillD3DPP(mfxHDL hWindow, mfxU16 nViews, D3DPRESENT_PARAMETERS &D3DPP) +{ + mfxStatus sts = MFX_ERR_NONE; + + D3DPP.Windowed = true; + D3DPP.hDeviceWindow = (HWND)hWindow; + + D3DPP.Flags = D3DPRESENTFLAG_VIDEO; + D3DPP.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; + D3DPP.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; // note that this setting leads to an implicit timeBeginPeriod call + D3DPP.BackBufferCount = 1; + D3DPP.BackBufferFormat = (m_bIsA2rgb10) ? D3DFMT_A2R10G10B10 : D3DFMT_X8R8G8B8; + + if (hWindow) + { + RECT r; + GetClientRect((HWND)hWindow, &r); + int x = GetSystemMetrics(SM_CXSCREEN); + int y = GetSystemMetrics(SM_CYSCREEN); + D3DPP.BackBufferWidth = std::min(r.right - r.left, x); + D3DPP.BackBufferHeight = std::min(r.bottom - r.top, y); + } + else + { + D3DPP.BackBufferWidth = GetSystemMetrics(SM_CYSCREEN); + D3DPP.BackBufferHeight = GetSystemMetrics(SM_CYSCREEN); + } + // + // Mark the back buffer lockable if software DXVA2 could be used. + // This is because software DXVA2 device requires a lockable render target + // for the optimal performance. + // + { + D3DPP.Flags |= D3DPRESENTFLAG_LOCKABLE_BACKBUFFER; + } + + bool isOverlaySupported = CheckOverlaySupport(); + if (2 == nViews && !isOverlaySupported) + return MFX_ERR_UNSUPPORTED; + + bool needOverlay = (2 == nViews) ? true : false; + + D3DPP.SwapEffect = needOverlay ? D3DSWAPEFFECT_OVERLAY : D3DSWAPEFFECT_DISCARD; + + return sts; +} + +mfxStatus CD3D9Device::Init( + mfxHDL hWindow, + mfxU16 nViews, + mfxU32 nAdapterNum) +{ + mfxStatus sts = MFX_ERR_NONE; + + if (2 < nViews) + return MFX_ERR_UNSUPPORTED; + + m_nViews = nViews; + + HRESULT hr = Direct3DCreate9Ex(D3D_SDK_VERSION, &m_pD3D9); + if (!m_pD3D9 || FAILED(hr)) + return MFX_ERR_DEVICE_FAILED; + + ZeroMemory(&m_D3DPP, sizeof(m_D3DPP)); + sts = FillD3DPP(hWindow, nViews, m_D3DPP); + MSDK_CHECK_STATUS(sts, "FillD3DPP failed"); + + hr = m_pD3D9->CreateDeviceEx( + nAdapterNum, + D3DDEVTYPE_HAL, + (HWND)hWindow, + D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED | D3DCREATE_FPU_PRESERVE, + &m_D3DPP, + NULL, + &m_pD3DD9); + if (FAILED(hr)) + return MFX_ERR_NULL_PTR; + + if(hWindow) + { + hr = m_pD3DD9->ResetEx(&m_D3DPP, NULL); + if (FAILED(hr)) + return MFX_ERR_UNDEFINED_BEHAVIOR; + hr = m_pD3DD9->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); + if (FAILED(hr)) + return MFX_ERR_UNDEFINED_BEHAVIOR; + } + UINT resetToken = 0; + + hr = DXVA2CreateDirect3DDeviceManager9(&resetToken, &m_pDeviceManager9); + if (FAILED(hr)) + return MFX_ERR_NULL_PTR; + + hr = m_pDeviceManager9->ResetDevice(m_pD3DD9, resetToken); + if (FAILED(hr)) + return MFX_ERR_UNDEFINED_BEHAVIOR; + + m_resetToken = resetToken; + + return sts; +} + +mfxStatus CD3D9Device::Reset() +{ + HRESULT hr = NO_ERROR; + MSDK_CHECK_POINTER(m_pD3DD9, MFX_ERR_NULL_PTR); + + if (m_D3DPP.Windowed) + { + RECT r; + GetClientRect((HWND)m_D3DPP.hDeviceWindow, &r); + int x = GetSystemMetrics(SM_CXSCREEN); + int y = GetSystemMetrics(SM_CYSCREEN); + m_D3DPP.BackBufferWidth = std::min(r.right - r.left, x); + m_D3DPP.BackBufferHeight = std::min(r.bottom - r.top, y); + } + else + { + m_D3DPP.BackBufferWidth = GetSystemMetrics(SM_CXSCREEN); + m_D3DPP.BackBufferHeight = GetSystemMetrics(SM_CYSCREEN); + } + + // Reset will change the parameters, so use a copy instead. + D3DPRESENT_PARAMETERS d3dpp = m_D3DPP; + hr = m_pD3DD9->ResetEx(&d3dpp, NULL); + + if (FAILED(hr)) + return MFX_ERR_UNDEFINED_BEHAVIOR; + + hr = m_pDeviceManager9->ResetDevice(m_pD3DD9, m_resetToken); + if (FAILED(hr)) + return MFX_ERR_UNDEFINED_BEHAVIOR; + + return MFX_ERR_NONE; +} + +void CD3D9Device::Close() +{ + MSDK_SAFE_RELEASE(m_pDXVAVP_Left); + MSDK_SAFE_RELEASE(m_pDXVAVP_Right); + MSDK_SAFE_RELEASE(m_pDXVAVPS); + + MSDK_SAFE_RELEASE(m_pDeviceManager9); + MSDK_SAFE_RELEASE(m_pD3DD9); + MSDK_SAFE_RELEASE(m_pD3D9); +} + +CD3D9Device::~CD3D9Device() +{ + Close(); +} + +mfxStatus CD3D9Device::GetHandle(mfxHandleType type, mfxHDL *pHdl) +{ + if (MFX_HANDLE_DIRECT3D_DEVICE_MANAGER9 == type && pHdl != NULL) + { + *pHdl = m_pDeviceManager9; + + return MFX_ERR_NONE; + } + return MFX_ERR_UNSUPPORTED; +} + +mfxStatus CD3D9Device::SetHandle(mfxHandleType type, mfxHDL hdl) +{ + if (MFX_HANDLE_DEVICEWINDOW == type && hdl != NULL) //for render window handle + { + m_D3DPP.hDeviceWindow = (HWND)hdl; + return MFX_ERR_NONE; + } + return MFX_ERR_UNSUPPORTED; +} + +mfxStatus CD3D9Device::RenderFrame(mfxFrameSurface1 * pSurface, mfxFrameAllocator * pmfxAlloc) +{ + HRESULT hr = S_OK; + + // Rendering of MVC is not supported + if (2 == m_nViews) + return MFX_ERR_UNDEFINED_BEHAVIOR; + + MSDK_CHECK_POINTER(pSurface, MFX_ERR_NULL_PTR); + MSDK_CHECK_POINTER(m_pDeviceManager9, MFX_ERR_NOT_INITIALIZED); + MSDK_CHECK_POINTER(pmfxAlloc, MFX_ERR_NULL_PTR); + + hr = m_pD3DD9->TestCooperativeLevel(); + + switch (hr) + { + case D3D_OK : + break; + + case D3DERR_DEVICELOST : + { + return MFX_ERR_DEVICE_LOST; + } + + case D3DERR_DEVICENOTRESET : + { + return MFX_ERR_UNKNOWN; + } + + default : + { + return MFX_ERR_UNKNOWN; + } + } + + CComPtr pBackBuffer; + hr = m_pD3DD9->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer); + + mfxHDLPair* dxMemId = (mfxHDLPair*)pSurface->Data.MemId; + + hr = m_pD3DD9->StretchRect((IDirect3DSurface9*)dxMemId->first, NULL, pBackBuffer, NULL, D3DTEXF_LINEAR); + if (FAILED(hr)) + { + return MFX_ERR_UNKNOWN; + } + + if (SUCCEEDED(hr)) + { + hr = m_pD3DD9->Present(NULL, NULL, NULL, NULL); + } + + return SUCCEEDED(hr) ? MFX_ERR_NONE : MFX_ERR_DEVICE_FAILED; +} + +mfxStatus CD3D9Device::CreateVideoProcessors() +{ + if (2 == m_nViews) + return MFX_ERR_UNDEFINED_BEHAVIOR; + + MSDK_SAFE_RELEASE(m_pDXVAVP_Left); + MSDK_SAFE_RELEASE(m_pDXVAVP_Right); + + HRESULT hr ; + + ZeroMemory(&m_backBufferDesc, sizeof(m_backBufferDesc)); + IDirect3DSurface9 *backBufferTmp = NULL; + hr = m_pD3DD9->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backBufferTmp); + if (NULL != backBufferTmp) + backBufferTmp->GetDesc(&m_backBufferDesc); + MSDK_SAFE_RELEASE(backBufferTmp); + + if (SUCCEEDED(hr)) + { + // Create DXVA2 Video Processor Service. + hr = DXVA2CreateVideoService(m_pD3DD9, + IID_IDirectXVideoProcessorService, + (void**)&m_pDXVAVPS); + } + + if (SUCCEEDED(hr)) + { + hr = m_pDXVAVPS->CreateVideoProcessor(DXVA2_VideoProcProgressiveDevice, + &m_VideoDesc, + m_D3DPP.BackBufferFormat, + 1, + &m_pDXVAVP_Right); + } + + return SUCCEEDED(hr) ? MFX_ERR_NONE : MFX_ERR_DEVICE_FAILED; +} + +#endif // #if defined(WIN32) || defined(WIN64) diff --git a/samples/sample_common/src/decode_render.cpp b/samples/sample_common/src/decode_render.cpp index 9f1a77bc3..c3d22324c 100644 --- a/samples/sample_common/src/decode_render.cpp +++ b/samples/sample_common/src/decode_render.cpp @@ -19,3 +19,344 @@ or https://software.intel.com/en-us/media-client-solutions-support. #include "mfx_samples_config.h" +#if defined(_WIN32) || defined(_WIN64) + +#include +#include +#include + +#include "sample_defs.h" +#include "decode_render.h" +#include "winUser.h" +#pragma warning(disable : 4100) + +bool CDecodeD3DRender::m_bIsMonitorFound = false; + +LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) +{ +#ifdef _WIN64 + CDecodeD3DRender* pRender = (CDecodeD3DRender*)GetWindowLongPtr(hWnd, GWLP_USERDATA); +#else + CDecodeD3DRender* pRender = (CDecodeD3DRender*)LongToPtr(GetWindowLongPtr(hWnd, GWL_USERDATA)); +#endif + if (pRender) + { + switch(message) + { + HANDLE_MSG(hWnd, WM_DESTROY, pRender->OnDestroy); + HANDLE_MSG(hWnd, WM_KEYUP, pRender->OnKey); + } + } + + return DefWindowProc(hWnd, message, wParam, lParam); +} + +CDecodeD3DRender::CDecodeD3DRender() +{ + m_bDwmEnabled = false; + m_nMonitorCurrent = 0; + + m_hwdev = NULL; + MSDK_ZERO_MEMORY(m_sWindowParams); + m_Hwnd = 0; + MSDK_ZERO_MEMORY(m_rect); + m_style = 0; + + MSDK_ZERO_MEMORY(shiftedSurface); + MSDK_ZERO_MEMORY(shiftSurfaceResponse); + pAllocator=NULL; +} + +BOOL CALLBACK CDecodeD3DRender::MonitorEnumProc(HMONITOR /*hMonitor*/, + HDC /*hdcMonitor*/, + LPRECT lprcMonitor, + LPARAM dwData) +{ + CDecodeD3DRender * pRender = reinterpret_cast(dwData); + RECT r = {0}; + if (NULL == lprcMonitor) + lprcMonitor = &r; + + if (pRender->m_nMonitorCurrent++ == pRender->m_sWindowParams.nAdapter) + { + pRender->m_RectWindow = *lprcMonitor; + m_bIsMonitorFound = true; + } + return TRUE; +} + +CDecodeD3DRender::~CDecodeD3DRender() +{ + Close(); +} + +void CDecodeD3DRender::Close() +{ + if (m_Hwnd) + { + DestroyWindow(m_Hwnd); + m_Hwnd=NULL; + } + + if(pAllocator) + { + pAllocator->Free(pAllocator->pthis,&shiftSurfaceResponse); + pAllocator=NULL; + } +} + + +mfxStatus CDecodeD3DRender::Init(sWindowParams pWParams) +{ + mfxStatus sts = MFX_ERR_NONE; + + // window part + m_sWindowParams = pWParams; + + WNDCLASS window; + MSDK_ZERO_MEMORY(window); + + window.lpfnWndProc= (WNDPROC)WindowProc; + window.hInstance= GetModuleHandle(NULL); + window.hCursor= LoadCursor(NULL, IDC_ARROW); + window.lpszClassName= m_sWindowParams.lpClassName; + + if (!RegisterClass(&window)) + return MFX_ERR_UNKNOWN; + + EnumDisplayMonitors(NULL, NULL, &CDecodeD3DRender::MonitorEnumProc, (LPARAM)this); + if(!m_bIsMonitorFound) + return MFX_ERR_NOT_FOUND; + + ::RECT displayRegion = {CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT}; + + //right and bottom fields consist of width and height values of displayed reqion + if (0 != m_sWindowParams.nx ) + { + displayRegion.right = (m_RectWindow.right - m_RectWindow.left) / m_sWindowParams.nx; + displayRegion.bottom = (m_RectWindow.bottom - m_RectWindow.top) / m_sWindowParams.ny; + displayRegion.left = displayRegion.right * (m_sWindowParams.ncell % m_sWindowParams.nx) + m_RectWindow.left; + displayRegion.top = displayRegion.bottom * (m_sWindowParams.ncell / m_sWindowParams.nx) + m_RectWindow.top; + } + else + { + displayRegion.right = pWParams.nWidth; + displayRegion.bottom = pWParams.nHeight; + displayRegion.left = m_RectWindow.left; + displayRegion.top= m_RectWindow.top; + } + + //no title window style if required + DWORD dwStyle = NULL == m_sWindowParams.lpWindowName ? WS_POPUP|WS_BORDER|WS_MAXIMIZE : WS_OVERLAPPEDWINDOW; + + m_Hwnd = CreateWindowEx(NULL, + m_sWindowParams.lpClassName, + m_sWindowParams.lpWindowName, + !m_sWindowParams.bFullScreen ? dwStyle : (WS_POPUP), + !m_sWindowParams.bFullScreen ? displayRegion.left : 0, + !m_sWindowParams.bFullScreen ? displayRegion.top : 0, + !m_sWindowParams.bFullScreen ? displayRegion.right : GetSystemMetrics(SM_CXSCREEN), + !m_sWindowParams.bFullScreen ? displayRegion.bottom : GetSystemMetrics(SM_CYSCREEN), + m_sWindowParams.hWndParent, + m_sWindowParams.hMenu, + m_sWindowParams.hInstance, + m_sWindowParams.lpParam); + + if (!m_Hwnd) + return MFX_ERR_UNKNOWN; + + ShowWindow(m_Hwnd, SW_SHOWDEFAULT); + UpdateWindow(m_Hwnd); + +#ifdef _WIN64 + SetWindowLongPtr(m_Hwnd, GWLP_USERDATA, (LONG_PTR)this); +#else + SetWindowLong(m_Hwnd, GWL_USERDATA, PtrToLong(this)); +#endif + + m_hwdev->SetHandle((mfxHandleType)MFX_HANDLE_DEVICEWINDOW, m_Hwnd); + sts = m_hwdev->Reset(); + MSDK_CHECK_STATUS(sts, "m_hwdev->Reset failed"); + + return sts; +} + +mfxStatus CDecodeD3DRender::RenderFrame(mfxFrameSurface1 *pSurface, mfxFrameAllocator *pmfxAlloc) +{ + RECT rect; + mfxStatus sts = MFX_ERR_NONE; + + GetClientRect(m_Hwnd, &rect); + + if (IsRectEmpty(&rect)) + return MFX_ERR_UNKNOWN; + + //--- In case of 10 bit surfaces and SW library we have to copy it and shift its data + if(pSurface->Info.FourCC == MFX_FOURCC_P010 && !pSurface->Info.Shift) + { + sts = AllocateShiftedSurfaceIfNeeded(pSurface,pmfxAlloc); + MSDK_CHECK_STATUS(sts, "AllocateShiftedSurfaceIfNeeded failed"); + + sts = pAllocator->Lock(pAllocator->pthis,shiftedSurface.Data.MemId,&shiftedSurface.Data); + MSDK_CHECK_STATUS(sts, "pAllocator->Lock of shiftedSurface failed"); + sts = pAllocator->Lock(pAllocator->pthis,pSurface->Data.MemId,&pSurface->Data); + MSDK_CHECK_STATUS(sts, "pAllocator->Lock of pSurface failed"); + + int wordsNum = pSurface->Data.Pitch*pSurface->Info.Height*3/16; // Number of 8-byte words + mfxU64* pBuf = (mfxU64*)pSurface->Data.Y16; + mfxU64* pDestBuf = (mfxU64*)shiftedSurface.Data.Y16; + for(int i=0;iUnlock(pAllocator->pthis,shiftedSurface.Data.MemId,&shiftedSurface.Data); + MSDK_CHECK_STATUS(sts, "pAllocator->Unlock of shiftedSurface failed"); + sts = pAllocator->Unlock(pAllocator->pthis,pSurface->Data.MemId,&pSurface->Data); + MSDK_CHECK_STATUS(sts, "pAllocator->Unlock of pSurface failed"); + + sts = m_hwdev->RenderFrame(&shiftedSurface, pmfxAlloc); + } + else + { + sts = m_hwdev->RenderFrame(pSurface, pmfxAlloc); + } + MSDK_CHECK_STATUS(sts, "m_hwdev->RenderFrame failed"); + + return sts; +} + +HWND CDecodeD3DRender::GetWindowHandle() +{ + if (!m_Hwnd) + { + EnumDisplayMonitors(NULL, NULL, &CDecodeD3DRender::MonitorEnumProc, (LPARAM)this); + POINT point = {m_RectWindow.left, m_RectWindow.top}; + m_Hwnd = WindowFromPoint(point); + m_nMonitorCurrent = 0; + m_bIsMonitorFound = false; + } + return m_Hwnd; +} + +VOID CDecodeD3DRender::UpdateTitle(double fps) +{ + if (m_Hwnd) + { + MSG msg; + MSDK_ZERO_MEMORY(msg); + while (msg.message != WM_QUIT && PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) + { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + + if (NULL != m_sWindowParams.lpWindowName) { + TCHAR str[20]; + _stprintf_s(str, 20, MSDK_STRING("fps=%.2lf"), fps ); + + SetWindowText(m_Hwnd, str); + } + } +} + +VOID CDecodeD3DRender::OnDestroy(HWND /*hwnd*/) +{ + PostQuitMessage(0); +} + +VOID CDecodeD3DRender::OnKey(HWND hwnd, UINT vk, BOOL fDown, int cRepeat, UINT flags) +{ + if (TRUE == fDown) + return; + + if ('1' == vk && false == m_sWindowParams.bFullScreen) + ChangeWindowSize(true); + else if (true == m_sWindowParams.bFullScreen) + ChangeWindowSize(false); +} + +void CDecodeD3DRender::AdjustWindowRect(RECT *rect) +{ + int cxmax = GetSystemMetrics(SM_CXMAXIMIZED); + int cymax = GetSystemMetrics(SM_CYMAXIMIZED); + int cxmin = GetSystemMetrics(SM_CXMINTRACK); + int cymin = GetSystemMetrics(SM_CYMINTRACK); + int leftmax = cxmax - cxmin; + int topmax = cymax - cxmin; + if (rect->left < 0) + rect->left = 0; + if (rect->left > leftmax) + rect->left = leftmax; + if (rect->top < 0) + rect->top = 0; + if (rect->top > topmax) + rect->top = topmax; + + if (rect->right < rect->left + cxmin) + rect->right = rect->left + cxmin; + if (rect->right - rect->left > cxmax) + rect->right = rect->left + cxmax; + + if (rect->bottom < rect->top + cymin) + rect->bottom = rect->top + cymin; + if (rect->bottom - rect->top > cymax) + rect->bottom = rect->top + cymax; +} + +VOID CDecodeD3DRender::ChangeWindowSize(bool bFullScreen) +{ + HMONITOR hMonitor = MonitorFromWindow(m_Hwnd, MONITOR_DEFAULTTONEAREST); + MONITORINFOEX mi; + mi.cbSize = sizeof(mi); + GetMonitorInfo(hMonitor, &mi); + + WINDOWINFO wndInfo; + wndInfo.cbSize = sizeof(WINDOWINFO); + GetWindowInfo(m_Hwnd, &wndInfo); + + if(!m_sWindowParams.bFullScreen) + { + m_rect = wndInfo.rcWindow; + m_style = wndInfo.dwStyle; + } + + m_sWindowParams.bFullScreen = bFullScreen; + + if(!bFullScreen) + { + AdjustWindowRectEx(&m_rect,0,0,0); + SetWindowLong(m_Hwnd, GWL_STYLE, m_style); + SetWindowPos(m_Hwnd, HWND_NOTOPMOST, + m_rect.left , m_rect.top , + abs(m_rect.right - m_rect.left), abs(m_rect.bottom - m_rect.top), + SWP_SHOWWINDOW); + } + else + { + SetWindowLong(m_Hwnd, GWL_STYLE, WS_POPUP); + SetWindowPos(m_Hwnd, HWND_NOTOPMOST,mi.rcMonitor.left , mi.rcMonitor.top, + abs(mi.rcMonitor.left - mi.rcMonitor.right), abs(mi.rcMonitor.top - mi.rcMonitor.bottom), SWP_SHOWWINDOW); + } +} + +mfxStatus CDecodeD3DRender::AllocateShiftedSurfaceIfNeeded(const mfxFrameSurface1* refSurface,mfxFrameAllocator* allocator) +{ + if(!pAllocator) + { + mfxFrameAllocRequest request={}; + request.AllocId = 0xF000; // Unique alloc ID + request.NumFrameMin=request.NumFrameSuggested=1; + request.Info = refSurface->Info; + request.Type = MFX_MEMTYPE_EXTERNAL_FRAME | MFX_MEMTYPE_FROM_DECODE | MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET; + + pAllocator = allocator; + mfxStatus sts = allocator->Alloc(allocator->pthis, &request, &shiftSurfaceResponse); + MSDK_CHECK_STATUS(sts, "Renderer: Shifted Surface allocation failed"); + + shiftedSurface.Data.MemId=shiftSurfaceResponse.mids[0]; + shiftedSurface.Info = request.Info; + } + return MFX_ERR_NONE; +} + +#endif // #if defined(_WIN32) || defined(_WIN64) diff --git a/samples/sample_common/src/general_allocator.cpp b/samples/sample_common/src/general_allocator.cpp index fd0030ee7..53c7e7812 100644 --- a/samples/sample_common/src/general_allocator.cpp +++ b/samples/sample_common/src/general_allocator.cpp @@ -21,8 +21,13 @@ or https://software.intel.com/en-us/media-client-solutions-support. #include "general_allocator.h" +#if defined(_WIN32) || defined(_WIN64) +#include "d3d_allocator.h" +#include "d3d11_allocator.h" +#else #include #include "vaapi_allocator.h" +#endif #include "sysmem_allocator.h" @@ -40,6 +45,16 @@ mfxStatus GeneralAllocator::Init(mfxAllocatorParams *pParams) { mfxStatus sts = MFX_ERR_NONE; +#if defined(_WIN32) || defined(_WIN64) + D3DAllocatorParams *d3dAllocParams = dynamic_cast(pParams); + if (d3dAllocParams) + m_D3DAllocator.reset(new D3DFrameAllocator); +#if MFX_D3D11_SUPPORT + D3D11AllocatorParams *d3d11AllocParams = dynamic_cast(pParams); + if (d3d11AllocParams) + m_D3DAllocator.reset(new D3D11FrameAllocator); +#endif +#endif #ifdef LIBVA_SUPPORT vaapiAllocatorParams *vaapiAllocParams = dynamic_cast(pParams); diff --git a/samples/sample_common/src/preset_manager.cpp b/samples/sample_common/src/preset_manager.cpp new file mode 100644 index 000000000..19e89295a --- /dev/null +++ b/samples/sample_common/src/preset_manager.cpp @@ -0,0 +1,157 @@ +/******************************************************************************\ +Copyright (c) 2005-2018, Intel Corporation +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +This sample was distributed or derived from the Intel's Media Samples package. +The original version of this sample may be obtained from https://software.intel.com/en-us/intel-media-server-studio +or https://software.intel.com/en-us/media-client-solutions-support. +\**********************************************************************************/ + +#include "preset_manager.h" +#include "mfxvideo.h" +#include "brc_routines.h" + +CPresetManager CPresetManager::Inst; + +msdk_string CPresetManager::modesName[PRESET_MAX_MODES] = +{ + MSDK_STRING("Default"), + MSDK_STRING("DSS"), + MSDK_STRING("Conference"), + MSDK_STRING("Gaming"), +}; + +//GopRefDist, TargetUsage, RateControlMethod, ExtBRCType, AsyncDepth, BRefType +// AdaptiveMaxFrameSize, LowDelayBRC, IntRefType, IntRefCycleSize, IntRefQPDelta, IntRefCycleDist, WeightedPred, WeightedBiPred, EnableBPyramid, EnablePPyramid + +CPresetParameters CPresetManager::presets[PRESET_MAX_MODES][PRESET_MAX_CODECS] = +{ + // Default + { + {4, MFX_TARGETUSAGE_BALANCED, MFX_RATECONTROL_VBR, EXTBRC_DEFAULT, 4, MFX_B_REF_PYRAMID, + 0, 0, 0, 0, 0, 0, MFX_WEIGHTED_PRED_UNKNOWN, MFX_WEIGHTED_PRED_UNKNOWN, 0, 0}, + {0, MFX_TARGETUSAGE_BALANCED, MFX_RATECONTROL_VBR, EXTBRC_DEFAULT, 4, 0, + 0, 0, 0, 0, 0, 0, MFX_WEIGHTED_PRED_UNKNOWN, MFX_WEIGHTED_PRED_UNKNOWN, 0, 0 } + }, + // DSS + { + {1, MFX_TARGETUSAGE_BALANCED, MFX_RATECONTROL_QVBR, EXTBRC_DEFAULT, 1, 0, + 0, 0, 0, 0, 0, 0, MFX_WEIGHTED_PRED_UNKNOWN, MFX_WEIGHTED_PRED_UNKNOWN, 0, 0 }, + {1, MFX_TARGETUSAGE_BALANCED, MFX_RATECONTROL_QVBR, EXTBRC_DEFAULT, 1, 0, + 0, 0, 0, 0, 0, 0, MFX_WEIGHTED_PRED_UNKNOWN, MFX_WEIGHTED_PRED_UNKNOWN, 1, 1 }, + }, + // Conference + { + {1, MFX_TARGETUSAGE_BALANCED, MFX_RATECONTROL_VCM, EXTBRC_DEFAULT, 1, 0, + 0, 0, 0, 0, 0, 0, MFX_WEIGHTED_PRED_UNKNOWN, MFX_WEIGHTED_PRED_UNKNOWN, 0, 0 }, + {1, MFX_TARGETUSAGE_BALANCED, MFX_RATECONTROL_VBR, EXTBRC_ON, 1, 0, + 0, 0, 0, 0, 0, 0, MFX_WEIGHTED_PRED_UNKNOWN, MFX_WEIGHTED_PRED_UNKNOWN, 0, 0 }, + }, + // Gaming + { + {1, MFX_TARGETUSAGE_BALANCED, MFX_RATECONTROL_QVBR, EXTBRC_DEFAULT, 1, 0, + MFX_CODINGOPTION_ON, MFX_CODINGOPTION_ON, MFX_REFRESH_HORIZONTAL, 8, 0, 4, MFX_WEIGHTED_PRED_UNKNOWN, MFX_WEIGHTED_PRED_UNKNOWN, 0, 0 }, + {1, MFX_TARGETUSAGE_BALANCED, MFX_RATECONTROL_VBR, EXTBRC_ON, 1, 0, + MFX_CODINGOPTION_ON, MFX_CODINGOPTION_ON, MFX_REFRESH_HORIZONTAL, 8, 0, 4, MFX_WEIGHTED_PRED_UNKNOWN, MFX_WEIGHTED_PRED_UNKNOWN, 0, 0 }, + } +}; + +CPresetManager::CPresetManager() +{ +} + + +CPresetManager::~CPresetManager() +{ +} + +COutputPresetParameters CPresetManager::GetPreset(EPresetModes mode, mfxU32 codecFourCC, mfxF64 fps, mfxU32 width, mfxU32 height, bool isHWLib) +{ + COutputPresetParameters retVal = GetBasicPreset(mode, codecFourCC); + *(dynamic_cast(&retVal)) = GetDependentPresetParameters(mode, codecFourCC, fps, width, height,retVal.TargetUsage); + + if (!isHWLib) + { + // These features are unsupported in SW library + retVal.WeightedBiPred = 0; + retVal.WeightedPred = 0; + } + + return retVal; +} + +COutputPresetParameters CPresetManager::GetBasicPreset(EPresetModes mode, mfxU32 codecFourCC) +{ + COutputPresetParameters retVal; + + if (mode < 0 || mode >= PRESET_MAX_MODES) + { + mode = PRESET_DEFAULT; + } + + // Reading basic preset values + switch (codecFourCC) + { + case MFX_CODEC_AVC: + retVal = presets[mode][PRESET_AVC]; + break; + case MFX_CODEC_HEVC: + retVal = presets[mode][PRESET_HEVC]; + break; + default: + if (mode != PRESET_DEFAULT) + { + msdk_printf(MSDK_STRING("WARNING: Presets are available for h.264 or h.265 codecs only. Request for particular preset is ignored.\n")); + } + + if (codecFourCC != MFX_CODEC_JPEG) + { + retVal.TargetUsage = MFX_TARGETUSAGE_BALANCED; + retVal.RateControlMethod = MFX_RATECONTROL_CBR; + } + retVal.AsyncDepth = 4; + return retVal; + } + + retVal.PresetName = modesName[mode]; + return retVal; +} + +CDependentPresetParameters CPresetManager::GetDependentPresetParameters(EPresetModes mode, mfxU32 codecFourCC, mfxF64 fps, mfxU32 width, mfxU32 height,mfxU16 targetUsage) +{ + CDependentPresetParameters retVal = {}; + retVal.TargetKbps = codecFourCC != MFX_CODEC_JPEG ? CalculateDefaultBitrate(codecFourCC, targetUsage, width, height, fps) : 0; + + if (codecFourCC == MFX_CODEC_AVC || codecFourCC == MFX_CODEC_HEVC) + { + // Calculating dependent preset values + retVal.MaxKbps = (mode == PRESET_GAMING ? (mfxU16)(1.2*retVal.TargetKbps) : 0); + retVal.GopPicSize = (mode == PRESET_GAMING || mode == PRESET_DEFAULT ? 0 : (mfxU16)(2 * fps)); + retVal.BufferSizeInKB = (mode == PRESET_DEFAULT ? 0 : retVal.TargetKbps); // 1 second buffers + retVal.LookAheadDepth = 0; // Enable this setting if LA BRC will be enabled + retVal.MaxFrameSize = (mode == PRESET_GAMING ? (mfxU32)(retVal.TargetKbps*0.166) : 0); + } + return retVal; +} + +EPresetModes CPresetManager::PresetNameToMode(const msdk_char* name) +{ + for (int i = 0; i < PRESET_MAX_MODES; i++) + { + if (!msdk_stricmp(modesName[i].c_str(), name)) + { + return (EPresetModes)i; + } + } + return PRESET_MAX_MODES; +} \ No newline at end of file diff --git a/samples/sample_common/src/sample_utils.cpp b/samples/sample_common/src/sample_utils.cpp index 5a47117de..f83fd8ff0 100644 --- a/samples/sample_common/src/sample_utils.cpp +++ b/samples/sample_common/src/sample_utils.cpp @@ -1259,6 +1259,17 @@ mfxU16 GetFreeSurfaceIndex(mfxFrameSurface1* pSurfacesPool, mfxU16 nPoolSize) return MSDK_INVALID_SURF_IDX; } +void FreeSurfacePool(mfxFrameSurface1* pSurfacesPool, mfxU16 nPoolSize) +{ + if (pSurfacesPool) + { + for (mfxU16 i = 0; i < nPoolSize; i++) + { + pSurfacesPool[i].Data.Locked = 0; + } + } +} + mfxU16 GetFreeSurface(mfxFrameSurface1* pSurfacesPool, mfxU16 nPoolSize) { mfxU32 SleepInterval = 10; // milliseconds @@ -1456,6 +1467,15 @@ mfxU16 CalculateDefaultBitrate(mfxU32 nCodecId, mfxU32 nTargetUsage, mfxU32 nWid switch (nCodecId) { + case MFX_CODEC_HEVC : + { + fnc.AddPair(0, 0); + fnc.AddPair(25344, 225/1.3); + fnc.AddPair(101376, 1000/1.3); + fnc.AddPair(414720, 4000/1.3); + fnc.AddPair(2058240, 5000/1.3); + break; + } case MFX_CODEC_AVC : { fnc.AddPair(0, 0); diff --git a/samples/sample_common/src/sysmem_allocator.cpp b/samples/sample_common/src/sysmem_allocator.cpp index 811b58570..ba46855d8 100644 --- a/samples/sample_common/src/sysmem_allocator.cpp +++ b/samples/sample_common/src/sysmem_allocator.cpp @@ -143,11 +143,13 @@ mfxStatus SysMemFrameAllocator::LockFrame(mfxMemId mid, mfxFrameData *ptr) ptr->R = ptr->B + 2; ptr->Pitch = 3 * Width2; break; +#if !(defined(_WIN32) || defined(_WIN64)) case MFX_FOURCC_RGBP: ptr->G = ptr->B + Width2 * Height2; ptr->R = ptr->B + Width2 * Height2 * 2; ptr->Pitch = Width2; break; +#endif case MFX_FOURCC_RGB4: case MFX_FOURCC_A2RGB10: ptr->G = ptr->B + 1; @@ -176,6 +178,19 @@ mfxStatus SysMemFrameAllocator::LockFrame(mfxMemId mid, mfxFrameData *ptr) ptr->A = ptr->V + 3; ptr->Pitch = 4 * Width2; break; +#if (MFX_VERSION >= 1027) + case MFX_FOURCC_Y210: + ptr->Y16 = (mfxU16 *)ptr->B; + ptr->U16 = ptr->Y16 + 1; + ptr->V16 = ptr->Y16 + 3; + //4 words per macropixel -> 2 words per pixel -> 4 bytes per pixel + ptr->Pitch = 4 * Width2; + break; + case MFX_FOURCC_Y410: + ptr->U = ptr->V = ptr->A = ptr->Y; + ptr->Pitch = 4 * Width2; + break; +#endif default: return MFX_ERR_UNSUPPORTED; @@ -252,12 +267,17 @@ mfxStatus SysMemFrameAllocator::AllocImpl(mfxFrameAllocRequest *request, mfxFram nbytes = 2*Width2*Height2; break; #endif +#if !(defined(_WIN32) || defined(_WIN64)) case MFX_FOURCC_RGBP: +#endif case MFX_FOURCC_RGB3: nbytes = Width2*Height2 + Width2*Height2 + Width2*Height2; break; case MFX_FOURCC_RGB4: case MFX_FOURCC_AYUV: +#if (MFX_VERSION >= 1027) + case MFX_FOURCC_Y410: +#endif nbytes = Width2*Height2 + Width2*Height2 + Width2*Height2 + Width2*Height2; break; case MFX_FOURCC_UYVY: @@ -275,6 +295,9 @@ mfxStatus SysMemFrameAllocator::AllocImpl(mfxFrameAllocRequest *request, mfxFram nbytes = Width2*Height2*4; // 4 bytes per pixel break; case MFX_FOURCC_P210: +#if (MFX_VERSION >= 1027) + case MFX_FOURCC_Y210: +#endif nbytes = Width2*Height2 + (Width2>>1)*(Height2) + (Width2>>1)*(Height2); nbytes *= 2; // 16bits break; diff --git a/samples/sample_common/src/vm/atomic.cpp b/samples/sample_common/src/vm/atomic.cpp index bfcc7bdb4..634812b37 100644 --- a/samples/sample_common/src/vm/atomic.cpp +++ b/samples/sample_common/src/vm/atomic.cpp @@ -17,3 +17,44 @@ The original version of this sample may be obtained from https://software.intel. or https://software.intel.com/en-us/media-client-solutions-support. \**********************************************************************************/ +#if defined(_WIN32) || defined(_WIN64) + +#include "vm/atomic_defs.h" + +//#define _interlockedbittestandset fake_set +//#define _interlockedbittestandreset fake_reset +//#define _interlockedbittestandset64 fake_set64 +//#define _interlockedbittestandreset64 fake_reset64 +#include +//#undef _interlockedbittestandset +//#undef _interlockedbittestandreset +//#undef _interlockedbittestandset64 +//#undef _interlockedbittestandreset64 +#pragma intrinsic (_InterlockedIncrement16) +#pragma intrinsic (_InterlockedDecrement16) +#pragma intrinsic (_InterlockedIncrement) +#pragma intrinsic (_InterlockedDecrement) + +mfxU16 msdk_atomic_inc16(volatile mfxU16 *pVariable) +{ + return _InterlockedIncrement16((volatile short*)pVariable); +} + +/* Thread-safe 16-bit variable decrementing */ +mfxU16 msdk_atomic_dec16(volatile mfxU16 *pVariable) +{ + return _InterlockedDecrement16((volatile short*)pVariable); +} + +mfxU32 msdk_atomic_inc32(volatile mfxU32 *pVariable) +{ + return _InterlockedIncrement((volatile long*)pVariable); +} + +/* Thread-safe 16-bit variable decrementing */ +mfxU32 msdk_atomic_dec32(volatile mfxU32 *pVariable) +{ + return _InterlockedDecrement((volatile long*)pVariable); +} + +#endif // #if defined(_WIN32) || defined(_WIN64) diff --git a/samples/sample_common/src/vm/atomic_linux.cpp b/samples/sample_common/src/vm/atomic_linux.cpp index 052c93406..26aefd1cd 100644 --- a/samples/sample_common/src/vm/atomic_linux.cpp +++ b/samples/sample_common/src/vm/atomic_linux.cpp @@ -17,6 +17,7 @@ The original version of this sample may be obtained from https://software.intel. or https://software.intel.com/en-us/media-client-solutions-support. \**********************************************************************************/ +#if !defined(_WIN32) && !defined(_WIN64) #include "vm/atomic_defs.h" @@ -60,3 +61,4 @@ mfxU32 msdk_atomic_dec32(volatile mfxU32 *pVariable) return msdk_atomic_add32(pVariable, (mfxU32)-1) + 1; } +#endif // #if !defined(_WIN32) && !defined(_WIN64) diff --git a/samples/sample_common/src/vm/shared_object.cpp b/samples/sample_common/src/vm/shared_object.cpp index 9f1a77bc3..0b388c4e3 100644 --- a/samples/sample_common/src/vm/shared_object.cpp +++ b/samples/sample_common/src/vm/shared_object.cpp @@ -19,3 +19,28 @@ or https://software.intel.com/en-us/media-client-solutions-support. #include "mfx_samples_config.h" +#if defined(_WIN32) || defined(_WIN64) + +#include "vm/so_defs.h" + +#include + +msdk_so_handle msdk_so_load(const msdk_char *file_name) +{ + if (!file_name) return NULL; + return (msdk_so_handle) LoadLibrary((LPCTSTR)file_name); +} + +msdk_func_pointer msdk_so_get_addr(msdk_so_handle handle, const char *func_name) +{ + if (!handle) return NULL; + return (msdk_func_pointer)GetProcAddress((HMODULE)handle, /*(LPCSTR)*/func_name); +} + +void msdk_so_free(msdk_so_handle handle) +{ + if (!handle) return; + FreeLibrary((HMODULE)handle); +} + +#endif // #if defined(_WIN32) || defined(_WIN64) diff --git a/samples/sample_common/src/vm/shared_object_linux.cpp b/samples/sample_common/src/vm/shared_object_linux.cpp index 326af3442..9ab59d83f 100644 --- a/samples/sample_common/src/vm/shared_object_linux.cpp +++ b/samples/sample_common/src/vm/shared_object_linux.cpp @@ -17,6 +17,7 @@ The original version of this sample may be obtained from https://software.intel. or https://software.intel.com/en-us/media-client-solutions-support. \**********************************************************************************/ +#if !defined(_WIN32) && !defined(_WIN64) #include "vm/so_defs.h" #include @@ -39,3 +40,4 @@ void msdk_so_free(msdk_so_handle handle) dlclose(handle); } +#endif // #if !defined(_WIN32) && !defined(_WIN64) diff --git a/samples/sample_common/src/vm/thread_linux.cpp b/samples/sample_common/src/vm/thread_linux.cpp index 18a2e0e49..68e692cd2 100644 --- a/samples/sample_common/src/vm/thread_linux.cpp +++ b/samples/sample_common/src/vm/thread_linux.cpp @@ -17,6 +17,7 @@ The original version of this sample may be obtained from https://software.intel. or https://software.intel.com/en-us/media-client-solutions-support. \**********************************************************************************/ +#if !defined(_WIN32) && !defined(_WIN64) #include // std::bad_alloc #include // setrlimit @@ -315,3 +316,4 @@ mfxU32 msdk_get_current_pid() return syscall(SYS_getpid); } +#endif // #if !defined(_WIN32) && !defined(_WIN64) diff --git a/samples/sample_common/src/vm/thread_windows.cpp b/samples/sample_common/src/vm/thread_windows.cpp index 9f1a77bc3..bc24a6795 100644 --- a/samples/sample_common/src/vm/thread_windows.cpp +++ b/samples/sample_common/src/vm/thread_windows.cpp @@ -19,3 +19,153 @@ or https://software.intel.com/en-us/media-client-solutions-support. #include "mfx_samples_config.h" +#if defined(_WIN32) || defined(_WIN64) + +#include "vm/thread_defs.h" +#include + +MSDKMutex::MSDKMutex(void) +{ + InitializeCriticalSection(&m_CritSec); +} + +MSDKMutex::~MSDKMutex(void) +{ + DeleteCriticalSection(&m_CritSec); +} + +mfxStatus MSDKMutex::Lock(void) +{ + EnterCriticalSection(&m_CritSec); + return MFX_ERR_NONE; +} + +mfxStatus MSDKMutex::Unlock(void) +{ + LeaveCriticalSection(&m_CritSec); + return MFX_ERR_NONE; +} + +int MSDKMutex::Try(void) +{ + return TryEnterCriticalSection(&m_CritSec); +} + +/* ****************************************************************************** */ + +MSDKSemaphore::MSDKSemaphore(mfxStatus &sts, mfxU32 count) +{ + sts = MFX_ERR_NONE; + m_semaphore = CreateSemaphore(NULL, count, LONG_MAX, 0); + if (!m_semaphore) throw std::bad_alloc(); +} + +MSDKSemaphore::~MSDKSemaphore(void) +{ + CloseHandle(m_semaphore); +} + +mfxStatus MSDKSemaphore::Post(void) +{ + return (ReleaseSemaphore(m_semaphore, 1, NULL) == false) ? MFX_ERR_UNKNOWN : MFX_ERR_NONE; +} + +mfxStatus MSDKSemaphore::Wait(void) +{ + return (WaitForSingleObject(m_semaphore, INFINITE) != WAIT_OBJECT_0) ? MFX_ERR_UNKNOWN : MFX_ERR_NONE; +} + +/* ****************************************************************************** */ + +MSDKEvent::MSDKEvent(mfxStatus &sts, bool manual, bool state) +{ + sts = MFX_ERR_NONE; + m_event = CreateEvent(NULL, manual, state, NULL); + if (!m_event) throw std::bad_alloc(); +} + +MSDKEvent::~MSDKEvent(void) +{ + CloseHandle(m_event); +} + +mfxStatus MSDKEvent::Signal(void) +{ + return (SetEvent(m_event) == false) ? MFX_ERR_UNKNOWN : MFX_ERR_NONE; +} + +mfxStatus MSDKEvent::Reset(void) +{ + return (ResetEvent(m_event) == false) ? MFX_ERR_UNKNOWN : MFX_ERR_NONE; +} + +mfxStatus MSDKEvent::Wait(void) +{ + return (WaitForSingleObject(m_event, INFINITE) != WAIT_OBJECT_0) ? MFX_ERR_UNKNOWN : MFX_ERR_NONE; +} + +mfxStatus MSDKEvent::TimedWait(mfxU32 msec) +{ + if(MFX_INFINITE == msec) return MFX_ERR_UNSUPPORTED; + mfxStatus mfx_res = MFX_ERR_NOT_INITIALIZED; + DWORD res = WaitForSingleObject(m_event, msec); + + if(WAIT_OBJECT_0 == res) mfx_res = MFX_ERR_NONE; + else if (WAIT_TIMEOUT == res) mfx_res = MFX_TASK_WORKING; + else mfx_res = MFX_ERR_UNKNOWN; + + return mfx_res; +} + +MSDKThread::MSDKThread(mfxStatus &sts, msdk_thread_callback func, void* arg) +{ + sts = MFX_ERR_NONE; + m_thread = (void*)_beginthreadex(NULL, 0, func, arg, 0, NULL); + if (!m_thread) throw std::bad_alloc(); +} + +MSDKThread::~MSDKThread(void) +{ + CloseHandle(m_thread); +} + +mfxStatus MSDKThread::Wait(void) +{ + return (WaitForSingleObject(m_thread, INFINITE) != WAIT_OBJECT_0) ? MFX_ERR_UNKNOWN : MFX_ERR_NONE; +} + +mfxStatus MSDKThread::TimedWait(mfxU32 msec) +{ + if(MFX_INFINITE == msec) return MFX_ERR_UNSUPPORTED; + + mfxStatus mfx_res = MFX_ERR_NONE; + DWORD res = WaitForSingleObject(m_thread, msec); + + if(WAIT_OBJECT_0 == res) mfx_res = MFX_ERR_NONE; + else if (WAIT_TIMEOUT == res) mfx_res = MFX_TASK_WORKING; + else mfx_res = MFX_ERR_UNKNOWN; + + return mfx_res; +} + +mfxStatus MSDKThread::GetExitCode() +{ + mfxStatus mfx_res = MFX_ERR_NOT_INITIALIZED; + + DWORD code = 0; + int sts = 0; + sts = GetExitCodeThread(m_thread, &code); + + if (sts == 0) mfx_res = MFX_ERR_UNKNOWN; + else if (STILL_ACTIVE == code) mfx_res = MFX_TASK_WORKING; + else mfx_res = MFX_ERR_NONE; + + return mfx_res; +} + +mfxU32 msdk_get_current_pid() +{ + return GetCurrentProcessId(); +} + +#endif // #if defined(_WIN32) || defined(_WIN64) diff --git a/samples/sample_common/src/vm/time.cpp b/samples/sample_common/src/vm/time.cpp index 9f1a77bc3..bda240900 100644 --- a/samples/sample_common/src/vm/time.cpp +++ b/samples/sample_common/src/vm/time.cpp @@ -19,3 +19,28 @@ or https://software.intel.com/en-us/media-client-solutions-support. #include "mfx_samples_config.h" +#if defined(_WIN32) || defined(_WIN64) + +#include "vm/time_defs.h" + +msdk_tick msdk_time_get_tick(void) +{ + LARGE_INTEGER t1; + + QueryPerformanceCounter(&t1); + return t1.QuadPart; +} + +msdk_tick msdk_time_get_frequency(void) +{ + LARGE_INTEGER t1; + + QueryPerformanceFrequency(&t1); + return t1.QuadPart; +} + +mfxU64 rdtsc(){ + return __rdtsc(); +} + +#endif // #if defined(_WIN32) || defined(_WIN64) diff --git a/samples/sample_common/src/vm/time_linux.cpp b/samples/sample_common/src/vm/time_linux.cpp index 3b7336f66..454885108 100644 --- a/samples/sample_common/src/vm/time_linux.cpp +++ b/samples/sample_common/src/vm/time_linux.cpp @@ -17,6 +17,7 @@ The original version of this sample may be obtained from https://software.intel. or https://software.intel.com/en-us/media-client-solutions-support. \**********************************************************************************/ +#if !defined(_WIN32) && !defined(_WIN64) #include "vm/time_defs.h" #include @@ -43,3 +44,4 @@ mfxU64 rdtsc(void){ } +#endif // #if !defined(_WIN32) && !defined(_WIN64) diff --git a/samples/sample_decode/include/pipeline_decode.h b/samples/sample_decode/include/pipeline_decode.h index f5bc90dc6..97f276b28 100644 --- a/samples/sample_decode/include/pipeline_decode.h +++ b/samples/sample_decode/include/pipeline_decode.h @@ -96,6 +96,7 @@ struct sInputParams mfxU16 nAsyncDepth; // asyncronous queue mfxU16 nTimeout; // timeout in seconds mfxU16 gpuCopy; // GPU Copy mode (three-state option) + bool bSoftRobustFlag; mfxU16 nThreadsNum; mfxI32 SchedulingType; mfxI32 Priority; @@ -305,6 +306,7 @@ class CDecodingPipeline: mfxU16 m_diMode; bool m_bVppIsUsed; bool m_bVppFullColorRange; + bool m_bSoftRobustFlag; std::vector m_vLatency; msdk_tick m_startTick; diff --git a/samples/sample_decode/sample_decode.sln b/samples/sample_decode/sample_decode.sln new file mode 100644 index 000000000..d26c7f2ed --- /dev/null +++ b/samples/sample_decode/sample_decode.sln @@ -0,0 +1,38 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27703.2000 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sample_decode", "sample_decode.vcxproj", "{F22D082D-FDDD-4946-A11B-5276F0994C74}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sample_common", "..\sample_common\sample_common.vcxproj", "{5FADB243-53C3-4776-A20F-8BD65C10CF41}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Debug|x64 = Debug|x64 + Release|Win32 = Release|Win32 + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F22D082D-FDDD-4946-A11B-5276F0994C74}.Debug|Win32.ActiveCfg = Debug|Win32 + {F22D082D-FDDD-4946-A11B-5276F0994C74}.Debug|Win32.Build.0 = Debug|Win32 + {F22D082D-FDDD-4946-A11B-5276F0994C74}.Debug|x64.ActiveCfg = Debug|x64 + {F22D082D-FDDD-4946-A11B-5276F0994C74}.Debug|x64.Build.0 = Debug|x64 + {F22D082D-FDDD-4946-A11B-5276F0994C74}.Release|Win32.ActiveCfg = Release|Win32 + {F22D082D-FDDD-4946-A11B-5276F0994C74}.Release|Win32.Build.0 = Release|Win32 + {F22D082D-FDDD-4946-A11B-5276F0994C74}.Release|x64.ActiveCfg = Release|x64 + {F22D082D-FDDD-4946-A11B-5276F0994C74}.Release|x64.Build.0 = Release|x64 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Debug|Win32.ActiveCfg = Debug|Win32 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Debug|Win32.Build.0 = Debug|Win32 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Debug|x64.ActiveCfg = Debug|x64 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Debug|x64.Build.0 = Debug|x64 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Release|Win32.ActiveCfg = Release|Win32 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Release|Win32.Build.0 = Release|Win32 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Release|x64.ActiveCfg = Release|x64 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Release|x64.Build.0 = Release|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/samples/sample_decode/sample_decode.vcxproj b/samples/sample_decode/sample_decode.vcxproj new file mode 100644 index 000000000..3e4156c8e --- /dev/null +++ b/samples/sample_decode/sample_decode.vcxproj @@ -0,0 +1,309 @@ + + + + + Debug_WithDebugAPI + Win32 + + + Debug_WithDebugAPI + x64 + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {F22D082D-FDDD-4946-A11B-5276F0994C74} + video_enc_con + Win32Proj + + + + Application + false + Unicode + v141 + + + Application + false + Unicode + v141 + + + Application + false + Unicode + v141 + + + Application + false + Unicode + v141 + + + Application + false + Unicode + v141 + + + Application + false + Unicode + v141 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + <_ProjectFileVersion>10.0.30319.1 + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + $(OutDir)obj\$(ProjectName)\ + true + true + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + $(OutDir)obj\$(ProjectName)\ + true + true + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + false + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + false + sample_decode + sample_decode + sample_decode + sample_decode + sample_decode + sample_decode + + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\sample_common\include;%(AdditionalIncludeDirectories) + _WIN32;WIN32;_DEBUG;_CONSOLE;_ITERATOR_DEBUG_LEVEL=0;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebug + + + Level4 + true + EditAndContinue + + + /DYNAMICBASE /NXCOMPAT /SAFESEH %(AdditionalOptions) + libmfx_vs2015.lib;dxva2.lib;d3d9.lib;dwmapi.lib;comsuppw.lib;d3d11.lib;dxgi.lib;%(AdditionalDependencies) + $(ProjectDir)..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;LIBCMT.lib;%(IgnoreSpecificDefaultLibraries) + dwmapi.dll;%(DelayLoadDLLs) + true + Console + MachineX86 + + + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\sample_common\include;%(AdditionalIncludeDirectories) + _WIN32;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebug + + + Level4 + true + EditAndContinue + + + /DYNAMICBASE /NXCOMPAT /SAFESEH %(AdditionalOptions) + libmfx_vs2015_d.lib;dxva2.lib;d3d9.lib;dwmapi.lib;comsuppw.lib;d3d11.lib;dxgi.lib;%(AdditionalDependencies) + $(ProjectDir)..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;LIBCMT.lib;%(IgnoreSpecificDefaultLibraries) + dwmapi.dll;%(DelayLoadDLLs) + true + Console + MachineX86 + + + + + X64 + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\sample_common\include;%(AdditionalIncludeDirectories) + _WIN64;WIN64;_DEBUG;_CONSOLE;_ITERATOR_DEBUG_LEVEL=0;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreaded + + + Level4 + true + ProgramDatabase + + + /DYNAMICBASE /NXCOMPAT %(AdditionalOptions) + libmfx_vs2015.lib;dxva2.lib;d3d9.lib;dwmapi.lib;comsuppw.lib;d3d11.lib;dxgi.lib;%(AdditionalDependencies) + $(ProjectDir)..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;LIBCMT.lib;%(IgnoreSpecificDefaultLibraries) + dwmapi.dll;%(DelayLoadDLLs) + true + Console + MachineX64 + + + + + X64 + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\sample_common\include;%(AdditionalIncludeDirectories) + _WIN64;WIN64;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebug + + + Level4 + true + ProgramDatabase + + + /DYNAMICBASE /NXCOMPAT %(AdditionalOptions) + libmfx_vs2015_d.lib;dxva2.lib;d3d9.lib;dwmapi.lib;comsuppw.lib;d3d11.lib;dxgi.lib;%(AdditionalDependencies) + $(ProjectDir)..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;LIBCMT.lib;%(IgnoreSpecificDefaultLibraries) + dwmapi.dll;%(DelayLoadDLLs) + true + Console + MachineX64 + + + + + $(ProjectDir)\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\sample_common\include;%(AdditionalIncludeDirectories) + _WIN32;WIN32;NDEBUG;_CONSOLE;SAVE_RECON;%(PreprocessorDefinitions) + Default + MultiThreaded + true + + + Level3 + true + + + + + /DYNAMICBASE /NXCOMPAT /SAFESEH %(AdditionalOptions) + libmfx_vs2015.lib;dxva2.lib;d3d9.lib;dwmapi.lib;comsuppw.lib;d3d11.lib;dxgi.lib;%(AdditionalDependencies) + $(ProjectDir)..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;%(IgnoreSpecificDefaultLibraries) + dwmapi.dll;%(DelayLoadDLLs) + false + true + Console + true + true + MachineX86 + + + + + X64 + + + $(ProjectDir)\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\sample_common\include;%(AdditionalIncludeDirectories) + WIN64;NDEBUG;_CONSOLE;SAVE_RECON;%(PreprocessorDefinitions) + Default + MultiThreaded + true + + + Level3 + true + + + + + /DYNAMICBASE /NXCOMPAT %(AdditionalOptions) + libmfx_vs2015.lib;dxva2.lib;d3d9.lib;dwmapi.lib;comsuppw.lib;d3d11.lib;dxgi.lib;%(AdditionalDependencies) + $(ProjectDir)..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;%(IgnoreSpecificDefaultLibraries) + dwmapi.dll;%(DelayLoadDLLs) + false + true + Console + true + true + MachineX64 + + + + + + + + + + + + {5FADB243-53C3-4776-A20F-8BD65C10CF41} + false + + + + + + \ No newline at end of file diff --git a/samples/sample_decode/src/pipeline_decode.cpp b/samples/sample_decode/src/pipeline_decode.cpp index cf32534a2..740f16a4d 100644 --- a/samples/sample_decode/src/pipeline_decode.cpp +++ b/samples/sample_decode/src/pipeline_decode.cpp @@ -21,12 +21,22 @@ or https://software.intel.com/en-us/media-client-solutions-support. #include "sample_defs.h" #include +#if defined(_WIN32) || defined(_WIN64) +#include +#include +#endif #include #include #include "pipeline_decode.h" #include "sysmem_allocator.h" +#if defined(_WIN32) || defined(_WIN64) +#include "d3d_allocator.h" +#include "d3d11_allocator.h" +#include "d3d_device.h" +#include "d3d11_device.h" +#endif #if defined LIBVA_SUPPORT #include "vaapi_allocator.h" @@ -68,6 +78,8 @@ CDecodingPipeline::CDecodingPipeline() m_memType = SYSTEM_MEMORY; m_bExternalAlloc = false; m_bDecOutSysmem = false; + m_bSoftRobustFlag = false; + MSDK_ZERO_MEMORY(m_mfxResponse); MSDK_ZERO_MEMORY(m_mfxVppResponse); @@ -220,6 +232,7 @@ mfxStatus CDecodingPipeline::Init(sInputParams *pParams) m_bOutI420 = pParams->outI420; m_nTimeout = pParams->nTimeout; + m_bSoftRobustFlag = pParams->bSoftRobustFlag; // Initializing file reader totalBytesProcessed = 0; @@ -745,9 +758,10 @@ mfxStatus CDecodingPipeline::InitMfxParams(sInputParams *pParams) } // Only shifted P010 is supported now - if (m_mfxVideoParams.mfx.FrameInfo.FourCC == MFX_FOURCC_P010 + if (m_memType != SYSTEM_MEMORY && + (m_mfxVideoParams.mfx.FrameInfo.FourCC == MFX_FOURCC_P010 #if (MFX_VERSION >= 1027) - || m_mfxVideoParams.mfx.FrameInfo.FourCC == MFX_FOURCC_Y210 + || m_mfxVideoParams.mfx.FrameInfo.FourCC == MFX_FOURCC_Y210) #endif ) { @@ -919,11 +933,13 @@ mfxStatus CDecodingPipeline::InitVppParams() } // P010 video surfaces should be shifted - if ((m_mfxVppVideoParams.vpp.Out.FourCC == MFX_FOURCC_P010 + if (m_memType != SYSTEM_MEMORY && + ( m_mfxVppVideoParams.vpp.Out.FourCC == MFX_FOURCC_P010 #if (MFX_VERSION >= 1027) || m_mfxVppVideoParams.vpp.Out.FourCC == MFX_FOURCC_Y210 #endif - )&& m_memType != SYSTEM_MEMORY) + ) + ) { m_mfxVppVideoParams.vpp.Out.Shift = 1; } @@ -1016,7 +1032,7 @@ mfxStatus CDecodingPipeline::AllocFrames() MSDK_CHECK_STATUS(sts, "m_pmfxDEC->Query failed"); // Workaround for VP9 codec - if ((m_mfxVideoParams.mfx.FrameInfo.FourCC == MFX_FOURCC_P010 + if ((m_mfxVideoParams.mfx.FrameInfo.FourCC == MFX_FOURCC_P010 #if (MFX_VERSION >= 1027) || m_mfxVideoParams.mfx.FrameInfo.FourCC == MFX_FOURCC_Y210 #endif @@ -1593,7 +1609,7 @@ mfxStatus CDecodingPipeline::SyncOutputSurface(mfxU32 wait) mfxStatus sts = m_mfxSession.SyncOperation(m_pCurrentOutputSurface->syncp, wait); - if (MFX_ERR_GPU_HANG == sts) { + if (MFX_ERR_GPU_HANG == sts && m_bSoftRobustFlag) { msdk_printf(MSDK_STRING("GPU hang happened\n")); // Output surface can be corrupted // But should be delivered to output anyway @@ -1974,11 +1990,11 @@ void CDecodingPipeline::PrintInfo() msdk_printf(MSDK_STRING("\nInput video\t%s\n"), CodecIdToStr(m_mfxVideoParams.mfx.CodecId).c_str()); if (m_bVppIsUsed) { - msdk_printf(MSDK_STRING("Output format\t%s (using vpp)\n"), m_bOutI420 ? "I420(YUV)" : CodecIdToStr(m_mfxVppVideoParams.vpp.Out.FourCC).c_str()); + msdk_printf(MSDK_STRING("Output format\t%s (using vpp)\n"), m_bOutI420 ? MSDK_STRING("I420(YUV)") : CodecIdToStr(m_mfxVppVideoParams.vpp.Out.FourCC).c_str()); } else { - msdk_printf(MSDK_STRING("Output format\t%s\n"), m_bOutI420 ? "I420(YUV)" : CodecIdToStr(m_mfxVideoParams.mfx.FrameInfo.FourCC).c_str()); + msdk_printf(MSDK_STRING("Output format\t%s\n"), m_bOutI420 ? MSDK_STRING("I420(YUV)") : CodecIdToStr(m_mfxVideoParams.mfx.FrameInfo.FourCC).c_str()); } mfxFrameInfo Info = m_mfxVideoParams.mfx.FrameInfo; @@ -2001,7 +2017,11 @@ void CDecodingPipeline::PrintInfo() msdk_printf(MSDK_STRING("Frame rate\t%.2f\n"), dFrameRate); const msdk_char* sMemType = +#if defined(_WIN32) || defined(_WIN64) + m_memType == D3D9_MEMORY ? MSDK_STRING("d3d") +#else m_memType == D3D9_MEMORY ? MSDK_STRING("vaapi") +#endif : (m_memType == D3D11_MEMORY ? MSDK_STRING("d3d11") : MSDK_STRING("system")); msdk_printf(MSDK_STRING("Memory type\t\t%s\n"), sMemType); diff --git a/samples/sample_decode/src/sample_decode.cpp b/samples/sample_decode/src/sample_decode.cpp index d75066dde..97d0e511e 100644 --- a/samples/sample_decode/src/sample_decode.cpp +++ b/samples/sample_decode/src/sample_decode.cpp @@ -109,6 +109,7 @@ void PrintHelp(msdk_char *strAppName, const msdk_char *strErrorMessage) msdk_printf(MSDK_STRING(" [-calc_latency] - calculates latency during decoding and prints log (supported only for H.264 and JPEG codec)\n")); msdk_printf(MSDK_STRING(" [-async] - depth of asynchronous pipeline. default value is 4. must be between 1 and 20\n")); msdk_printf(MSDK_STRING(" [-gpucopy::] Enable or disable GPU copy mode\n")); + msdk_printf(MSDK_STRING(" [-robust:soft] - GPU hang recovery by inserting an IDR frame\n")); msdk_printf(MSDK_STRING(" [-timeout] - timeout in seconds\n")); #if MFX_VERSION >= 1022 msdk_printf(MSDK_STRING(" [-dec_postproc force/auto] - resize after decoder using direct pipe\n")); @@ -118,11 +119,20 @@ void PrintHelp(msdk_char *strAppName, const msdk_char *strErrorMessage) msdk_printf(MSDK_STRING(" or perform VPP operation through separate pipeline component for unsupported streams\n")); #endif //MFX_VERSION >= 1022 +#if !defined(_WIN32) && !defined(_WIN64) msdk_printf(MSDK_STRING(" [-threads_num] - number of mediasdk task threads\n")); msdk_printf(MSDK_STRING(" [-threads_schedtype] - scheduling type of mediasdk task threads\n")); msdk_printf(MSDK_STRING(" [-threads_priority] - priority of mediasdk task threads\n")); msdk_printf(MSDK_STRING("\n")); msdk_thread_printf_scheduling_help(); +#endif +#if defined(_WIN32) || defined(_WIN64) + msdk_printf(MSDK_STRING(" [-jpeg_rotate n] - rotate jpeg frame n degrees \n")); + msdk_printf(MSDK_STRING(" n(90,180,270) - number of degrees \n")); + + msdk_printf(MSDK_STRING("\nFeatures: \n")); + msdk_printf(MSDK_STRING(" Press 1 to toggle fullscreen rendering on/off\n")); +#endif msdk_printf(MSDK_STRING("\n")); msdk_printf(MSDK_STRING("Example:\n")); msdk_printf(MSDK_STRING(" %s h265 -i in.bit -o out.yuv -p 15dd936825ad475ea34e35f3f54217a6\n"), strAppName); @@ -402,6 +412,11 @@ mfxStatus ParseInputString(msdk_char* strInput[], mfxU8 nArgNum, sInputParams* p { pParams->gpuCopy = MFX_GPUCOPY_OFF; } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-robust:soft"))) + { + pParams->bSoftRobustFlag = true; + } +#if !defined(_WIN32) && !defined(_WIN64) #if (MFX_VERSION >= 1025) else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-d"))) { @@ -447,6 +462,7 @@ mfxStatus ParseInputString(msdk_char* strInput[], mfxU8 nArgNum, sInputParams* p return MFX_ERR_UNSUPPORTED; } } +#endif // #if !defined(_WIN32) && !defined(_WIN64) #if MFX_VERSION >= 1022 else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-dec_postproc"))) { @@ -659,7 +675,11 @@ mfxStatus ParseInputString(msdk_char* strInput[], mfxU8 nArgNum, sInputParams* p return MFX_ERR_NONE; } +#if defined(_WIN32) || defined(_WIN64) +int _tmain(int argc, TCHAR *argv[]) +#else int main(int argc, char *argv[]) +#endif { sInputParams Params; // input parameters from command line CDecodingPipeline Pipeline; // pipeline for decoding, includes input file reader, decoder and output file writer diff --git a/samples/sample_encode/include/pipeline_encode.h b/samples/sample_encode/include/pipeline_encode.h index d9cd5be91..3c1831795 100644 --- a/samples/sample_encode/include/pipeline_encode.h +++ b/samples/sample_encode/include/pipeline_encode.h @@ -1,5 +1,5 @@ /******************************************************************************\ -Copyright (c) 2005-2018, Intel Corporation +Copyright (c) 2005-2019, Intel Corporation All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: @@ -42,6 +42,8 @@ or https://software.intel.com/en-us/media-client-solutions-support. #include "plugin_loader.h" +#include "preset_manager.h" + #if defined (ENABLE_V4L2_SUPPORT) #include "v4l2_util.h" #endif @@ -71,13 +73,6 @@ enum MemType { D3D11_MEMORY = 0x02, }; -enum ExtBRCType { - EXTBRC_DEFAULT, - EXTBRC_OFF, - EXTBRC_ON, - EXTBRC_IMPLICIT -}; - struct sInputParams { mfxU16 nTargetUsage; @@ -146,6 +141,8 @@ struct sInputParams bool enableQSVFF; + bool bSoftRobustFlag; + mfxU32 nTimeout; mfxU16 nMemBuf; @@ -182,12 +179,17 @@ struct sInputParams bool shouldUseShifted10BitVPP; bool IsSourceMSB; + bool bSingleTexture; + #if (MFX_VERSION >= 1027) msdk_char *RoundingOffsetFile; #endif msdk_char DumpFileName[MSDK_MAX_FILENAME_LEN]; msdk_char uSEI[MSDK_MAX_USER_DATA_UNREG_SEI_LEN]; + EPresetModes PresetMode; + bool shouldPrintPresets; + #if defined (ENABLE_V4L2_SUPPORT) msdk_char DeviceName[MSDK_MAX_FILENAME_LEN]; msdk_char MipiModeName[MSDK_MAX_FILENAME_LEN]; @@ -196,6 +198,11 @@ struct sInputParams enum AtomISPMode MipiMode; #endif +#if (MFX_VERSION >= MFX_VERSION_NEXT) + mfxI16 DeblockingAlphaTcOffset; + mfxI16 DeblockingBetaOffset; +#endif + }; struct bufSet @@ -307,11 +314,15 @@ class CEncTaskPool virtual CTimeStatistics& GetOverallStatistics() { return m_statOverall;} virtual CTimeStatistics& GetFileStatistics() { return m_statFile;} virtual void Close(); + virtual void SetGpuHangRecoveryFlag(); + virtual void ClearTasks(); protected: sTask* m_pTasks; mfxU32 m_nPoolSize; mfxU32 m_nTaskBufferStart; + bool m_bGpuHangRecovery; + MFXVideoSession* m_pmfxSession; CTimeStatistics m_statOverall; @@ -391,6 +402,7 @@ class CEncodingPipeline mfxExtCodingOption2 m_CodingOption2; // HEVC mfxExtHEVCParam m_ExtHEVCParam; + mfxExtHEVCTiles m_ExtHEVCTiles; mfxExtCodingOption3 m_CodingOption3; // VP9 mfxExtVP9Param m_ExtVP9Param; @@ -410,11 +422,12 @@ class CEncodingPipeline CHWDevice *m_hwdev; + bool isV4L2InputEnabled; bufList m_encExtBufs; #if (MFX_VERSION >= 1027) FILE* m_round_in; #endif - bool isV4L2InputEnabled; + bool m_bSoftRobustFlag; mfxU32 m_nTimeout; @@ -425,6 +438,7 @@ class CEncodingPipeline bool m_bTimeOutExceed; bool m_bIsFieldSplitting; + bool m_bSingleTexture; mfxEncodeCtrl m_encCtrl; diff --git a/samples/sample_encode/sample_encode.sln b/samples/sample_encode/sample_encode.sln new file mode 100644 index 000000000..383f1ac6a --- /dev/null +++ b/samples/sample_encode/sample_encode.sln @@ -0,0 +1,41 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27703.2000 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sample_encode", "sample_encode.vcxproj", "{33834A6A-4466-4339-8C7C-FBA250BC1957}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sample_common", "..\sample_common\sample_common.vcxproj", "{5FADB243-53C3-4776-A20F-8BD65C10CF41}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Debug|x64 = Debug|x64 + Release|Win32 = Release|Win32 + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {33834A6A-4466-4339-8C7C-FBA250BC1957}.Debug|Win32.ActiveCfg = Debug|Win32 + {33834A6A-4466-4339-8C7C-FBA250BC1957}.Debug|Win32.Build.0 = Debug|Win32 + {33834A6A-4466-4339-8C7C-FBA250BC1957}.Debug|x64.ActiveCfg = Debug|x64 + {33834A6A-4466-4339-8C7C-FBA250BC1957}.Debug|x64.Build.0 = Debug|x64 + {33834A6A-4466-4339-8C7C-FBA250BC1957}.Release|Win32.ActiveCfg = Release|Win32 + {33834A6A-4466-4339-8C7C-FBA250BC1957}.Release|Win32.Build.0 = Release|Win32 + {33834A6A-4466-4339-8C7C-FBA250BC1957}.Release|x64.ActiveCfg = Release|x64 + {33834A6A-4466-4339-8C7C-FBA250BC1957}.Release|x64.Build.0 = Release|x64 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Debug|Win32.ActiveCfg = Debug|Win32 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Debug|Win32.Build.0 = Debug|Win32 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Debug|x64.ActiveCfg = Debug|x64 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Debug|x64.Build.0 = Debug|x64 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Release|Win32.ActiveCfg = Release|Win32 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Release|Win32.Build.0 = Release|Win32 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Release|x64.ActiveCfg = Release|x64 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Release|x64.Build.0 = Release|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {1DBFB2BA-C9A2-4CDB-A7A9-B0AED141B7A0} + EndGlobalSection +EndGlobal diff --git a/samples/sample_encode/sample_encode.vcxproj b/samples/sample_encode/sample_encode.vcxproj new file mode 100644 index 000000000..032db3ccb --- /dev/null +++ b/samples/sample_encode/sample_encode.vcxproj @@ -0,0 +1,309 @@ + + + + + Debug_WithDebugAPI + Win32 + + + Debug_WithDebugAPI + x64 + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {33834A6A-4466-4339-8C7C-FBA250BC1957} + video_enc_con + Win32Proj + + + + Application + false + Unicode + v141 + + + Application + false + Unicode + v141 + + + Application + false + Unicode + v141 + + + Application + false + Unicode + v141 + + + Application + false + Unicode + v141 + + + Application + false + Unicode + v141 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + <_ProjectFileVersion>10.0.30319.1 + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + $(OutDir)obj\$(ProjectName)\ + true + true + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + $(OutDir)obj\$(ProjectName)\ + true + true + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + false + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + false + sample_encode + sample_encode + sample_encode + sample_encode + sample_encode + sample_encode + + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\sample_common\include;$(ProjectDir)\..\sample_plugins\rotate_cpu\include;%(AdditionalIncludeDirectories) + TIME_STATS;WIN32;_DEBUG;_CONSOLE;_ITERATOR_DEBUG_LEVEL=0;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebug + + + Level4 + true + EditAndContinue + + + /DYNAMICBASE /NXCOMPAT /SAFESEH %(AdditionalOptions) + libmfx_vs2015.lib;dxva2.lib;d3d9.lib;d3d11.lib;dxgi.lib;%(AdditionalDependencies) + $(ProjectDir)..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;%(IgnoreSpecificDefaultLibraries) + true + $(TargetDir)$(TargetName).pdb + Console + MachineX86 + + + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\sample_common\include;$(ProjectDir)\..\sample_plugins\rotate_cpu\include;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebug + + + Level4 + true + EditAndContinue + + + /DYNAMICBASE /NXCOMPAT /SAFESEH %(AdditionalOptions) + libmfx_vs2015_d.lib;dxva2.lib;d3d9.lib;d3d11.lib;dxgi.lib;%(AdditionalDependencies) + $(ProjectDir)..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;%(IgnoreSpecificDefaultLibraries) + true + $(TargetDir)$(TargetName).pdb + Console + MachineX86 + + + + + X64 + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\sample_common\include;$(ProjectDir)\..\sample_plugins\rotate_cpu\include;%(AdditionalIncludeDirectories) + TIME_STATS;WIN64;_DEBUG;_CONSOLE;_ITERATOR_DEBUG_LEVEL=0;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebug + + + Level4 + true + ProgramDatabase + + + /DYNAMICBASE /NXCOMPAT %(AdditionalOptions) + libmfx_vs2015.lib;dxva2.lib;d3d9.lib;d3d11.lib;dxgi.lib;%(AdditionalDependencies) + $(ProjectDir)..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;%(IgnoreSpecificDefaultLibraries) + true + $(TargetDir)$(TargetName).pdb + Console + MachineX64 + + + + + X64 + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\sample_common\include;$(ProjectDir)\..\sample_plugins\rotate_cpu\include;%(AdditionalIncludeDirectories) + WIN64;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebug + + + Level4 + true + ProgramDatabase + + + /DYNAMICBASE /NXCOMPAT %(AdditionalOptions) + libmfx_vs2015_d.lib;dxva2.lib;d3d9.lib;d3d11.lib;dxgi.lib;%(AdditionalDependencies) + $(ProjectDir)..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;%(IgnoreSpecificDefaultLibraries) + true + $(TargetDir)$(TargetName).pdb + Console + MachineX64 + + + + + $(ProjectDir)\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\sample_common\include;$(ProjectDir)\..\sample_plugins\rotate_cpu\include;%(AdditionalIncludeDirectories) + WIN32;NDEBUG;_CONSOLE;SAVE_RECON;%(PreprocessorDefinitions) + Default + MultiThreaded + true + + + Level4 + true + + + + + /DYNAMICBASE /NXCOMPAT /SAFESEH %(AdditionalOptions) + libmfx_vs2015.lib;dxva2.lib;d3d9.lib;d3d11.lib;dxgi.lib;%(AdditionalDependencies) + $(ProjectDir)..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;%(IgnoreSpecificDefaultLibraries) + false + $(TargetDir)$(TargetName).pdb + true + Console + true + true + MachineX86 + + + + + X64 + + + $(ProjectDir)\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\sample_common\include;$(ProjectDir)\..\sample_plugins\rotate_cpu\include;%(AdditionalIncludeDirectories) + TIME_STATS;WIN64;NDEBUG;_CONSOLE;SAVE_RECON;%(PreprocessorDefinitions) + Default + MultiThreaded + true + + + Level4 + true + + + + + /DYNAMICBASE /NXCOMPAT %(AdditionalOptions) + libmfx_vs2015.lib;dxva2.lib;d3d9.lib;d3d11.lib;dxgi.lib;%(AdditionalDependencies) + $(ProjectDir)..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;%(IgnoreSpecificDefaultLibraries) + false + true + Console + true + true + MachineX64 + + + + + + + + + + + + + + + + {5FADB243-53C3-4776-A20F-8BD65C10CF41} + + + + + + \ No newline at end of file diff --git a/samples/sample_encode/src/pipeline_encode.cpp b/samples/sample_encode/src/pipeline_encode.cpp index 16cd6db74..b8803e4dd 100644 --- a/samples/sample_encode/src/pipeline_encode.cpp +++ b/samples/sample_encode/src/pipeline_encode.cpp @@ -1,5 +1,5 @@ /******************************************************************************\ -Copyright (c) 2005-2018, Intel Corporation +Copyright (c) 2005-2019, Intel Corporation All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: @@ -79,6 +79,7 @@ CEncTaskPool::CEncTaskPool() m_pmfxSession = NULL; m_nTaskBufferStart = 0; m_nPoolSize = 0; + m_bGpuHangRecovery = false; } CEncTaskPool::~CEncTaskPool() @@ -139,9 +140,17 @@ mfxStatus CEncTaskPool::SynchronizeFirstTask() if (NULL != m_pTasks[m_nTaskBufferStart].EncSyncP) { sts = m_pmfxSession->SyncOperation(m_pTasks[m_nTaskBufferStart].EncSyncP, MSDK_WAIT_INTERVAL); - if (sts == MFX_ERR_GPU_HANG) + if (sts == MFX_ERR_GPU_HANG && m_bGpuHangRecovery) { bGpuHang = true; + { + for (mfxU32 i = 0; i < m_nPoolSize; i++) + if (m_pTasks[i].EncSyncP != NULL) + { + sts = m_pmfxSession->SyncOperation(m_pTasks[i].EncSyncP, 0);//MSDK_WAIT_INTERVAL + } + } + ClearTasks(); sts = MFX_ERR_NONE; msdk_printf(MSDK_STRING("GPU hang happened\n")); } @@ -174,9 +183,10 @@ mfxStatus CEncTaskPool::SynchronizeFirstTask() { // find out if the error occurred in a VPP task to perform recovery procedure if applicable sts = m_pmfxSession->SyncOperation(*m_pTasks[m_nTaskBufferStart].DependentVppTasks.begin(), 0); - if (sts == MFX_ERR_GPU_HANG) + if (sts == MFX_ERR_GPU_HANG && m_bGpuHangRecovery) { bGpuHang = true; + ClearTasks(); sts = MFX_ERR_NONE; msdk_printf(MSDK_STRING("GPU hang happened\n")); } @@ -259,6 +269,20 @@ void CEncTaskPool::Close() m_nPoolSize = 0; } +void CEncTaskPool::SetGpuHangRecoveryFlag() +{ + m_bGpuHangRecovery = true; +} + +void CEncTaskPool::ClearTasks() +{ + for (size_t i = 0; i < m_nPoolSize; i++) + { + m_pTasks[i].Reset(); + } + m_nTaskBufferStart = 0; +} + sTask::sTask() : EncSyncP(0) , pWriter(NULL) @@ -471,7 +495,10 @@ mfxStatus CEncodingPipeline::InitMfxEncParams(sInputParams *pInParams) m_mfxEncParams.mfx.FrameInfo.CropW = pInParams->nDstWidth; m_mfxEncParams.mfx.FrameInfo.CropH = pInParams->nDstHeight; -#if MFX_VERSION >= 1029 + m_ExtHEVCTiles.NumTileRows = pInParams->nEncTileRows; + m_ExtHEVCTiles.NumTileColumns = pInParams->nEncTileCols; + +#if MFX_VERSION >= MFX_VERSION_NEXT m_ExtVP9Param.NumTileRows = pInParams->nEncTileRows; m_ExtVP9Param.NumTileColumns = pInParams->nEncTileCols; #endif @@ -602,8 +629,10 @@ mfxStatus CEncodingPipeline::InitMfxEncParams(sInputParams *pInParams) || pInParams->nPRefType || pInParams->IntRefCycleDist || pInParams->nAdaptiveMaxFrameSize || pInParams->nNumRefActiveP || pInParams->nNumRefActiveBL0 || pInParams->nNumRefActiveBL1 || pInParams->ExtBrcAdaptiveLTR || pInParams->QVBRQuality || pInParams->WinBRCSize - || pInParams->WinBRCMaxAvgKbps || pInParams->nTransformSkip) - { +#if (MFX_VERSION >= MFX_VERSION_NEXT) + || pInParams->DeblockingAlphaTcOffset || pInParams->DeblockingBetaOffset +#endif + || pInParams->WinBRCMaxAvgKbps || pInParams->nTransformSkip) { if (pInParams->CodecId == MFX_CODEC_HEVC) { m_CodingOption3.GPB = pInParams->nGPB; @@ -630,9 +659,23 @@ mfxStatus CEncodingPipeline::InitMfxEncParams(sInputParams *pInParams) m_CodingOption3.WinBRCSize = pInParams->WinBRCSize; m_CodingOption3.WinBRCMaxAvgKbps = pInParams->WinBRCMaxAvgKbps; +#if (MFX_VERSION >= MFX_VERSION_NEXT) + if (pInParams->DeblockingAlphaTcOffset || pInParams->DeblockingBetaOffset) + { + m_CodingOption3.DeblockingAlphaTcOffset = pInParams->DeblockingAlphaTcOffset; + m_CodingOption3.DeblockingBetaOffset = pInParams->DeblockingBetaOffset; + } +#endif m_EncExtParams.push_back((mfxExtBuffer *)&m_CodingOption3); } + if (m_ExtHEVCTiles.NumTileRows + && m_ExtHEVCTiles.NumTileColumns + && m_mfxEncParams.mfx.CodecId == MFX_CODEC_HEVC) + { + m_EncExtParams.push_back((mfxExtBuffer*)&m_ExtHEVCTiles); + } + // In case of HEVC when height and/or width divided with 8 but not divided with 16 // add extended parameter to increase performance if ( ( !((m_mfxEncParams.mfx.FrameInfo.CropW & 15 ) ^ 8 ) || @@ -972,7 +1015,7 @@ mfxStatus CEncodingPipeline::CreateAllocator() D3D11AllocatorParams *pd3dAllocParams = new D3D11AllocatorParams; MSDK_CHECK_POINTER(pd3dAllocParams, MFX_ERR_MEMORY_ALLOC); pd3dAllocParams->pDevice = reinterpret_cast(hdl); - + pd3dAllocParams->bUseSingleTexture = m_bSingleTexture; m_pmfxAllocatorParams = pd3dAllocParams; } else @@ -1112,6 +1155,9 @@ CEncodingPipeline::CEncodingPipeline() m_nFramesRead = 0; m_bFileWriterReset = false; + m_bSoftRobustFlag = false; + m_bSingleTexture = false; + m_MVCflags = MVC_DISABLED; m_nNumView = 0; @@ -1141,6 +1187,10 @@ CEncodingPipeline::CEncodingPipeline() m_ExtHEVCParam.Header.BufferId = MFX_EXTBUFF_HEVC_PARAM; m_ExtHEVCParam.Header.BufferSz = sizeof(m_ExtHEVCParam); + MSDK_ZERO_MEMORY(m_ExtHEVCTiles); + m_ExtHEVCTiles.Header.BufferId = MFX_EXTBUFF_HEVC_TILES; + m_ExtHEVCTiles.Header.BufferSz = sizeof(m_ExtHEVCTiles); + MSDK_ZERO_MEMORY(m_ExtVP9Param); m_ExtVP9Param.Header.BufferId = MFX_EXTBUFF_VP9_PARAM; m_ExtVP9Param.Header.BufferSz = sizeof(m_ExtVP9Param); @@ -1285,6 +1335,7 @@ mfxStatus CEncodingPipeline::Init(sInputParams *pParams) initPar.Version.Minor = 0; initPar.GPUCopy = pParams->gpuCopy; + m_bSingleTexture = pParams->bSingleTexture; // Init session if (pParams->bUseHWLib) { @@ -1404,7 +1455,8 @@ mfxStatus CEncodingPipeline::Init(sInputParams *pParams) } // Determine if we should shift P010 surfaces bool readerShift = false; - if (pParams->FileInputFourCC == MFX_FOURCC_P010 || pParams->FileInputFourCC == MFX_FOURCC_P210 + if (pParams->FileInputFourCC == MFX_FOURCC_P010 + || pParams->FileInputFourCC == MFX_FOURCC_P210 #if (MFX_VERSION >= 1027) || pParams->FileInputFourCC == MFX_FOURCC_Y210 #endif @@ -1452,6 +1504,8 @@ mfxStatus CEncodingPipeline::Init(sInputParams *pParams) m_memType = pParams->memType; m_nMemBuffer = pParams->nMemBuf; + m_bSoftRobustFlag = pParams->bSoftRobustFlag; + // create and init frame allocator sts = CreateAllocator(); MSDK_CHECK_STATUS(sts, "CreateAllocator failed"); @@ -1773,6 +1827,9 @@ mfxStatus CEncodingPipeline::ResetMFXComponents(sInputParams* pParams) sts = m_TaskPool.Init(&m_mfxSession, m_FileWriters.first, m_mfxEncParams.AsyncDepth, nEncodedDataBufferSize, m_FileWriters.second); MSDK_CHECK_STATUS(sts, "m_TaskPool.Init failed"); + if (m_bSoftRobustFlag) + m_TaskPool.SetGpuHangRecoveryFlag(); + sts = FillBuffers(); MSDK_CHECK_STATUS(sts, "FillBuffers failed"); @@ -1881,8 +1938,10 @@ mfxStatus CEncodingPipeline::GetFreeTask(sTask **ppTask) if (MFX_ERR_NOT_FOUND == sts) { sts = m_TaskPool.SynchronizeFirstTask(); - if (sts == MFX_ERR_GPU_HANG) + if (sts == MFX_ERR_GPU_HANG && m_bSoftRobustFlag) { + m_TaskPool.ClearTasks(); + FreeSurfacePool(m_pEncSurfaces, m_EncResponse.NumFrameActual); m_bInsertIDR = true; sts = MFX_ERR_NONE; } @@ -2239,9 +2298,11 @@ mfxStatus CEncodingPipeline::Run() while (MFX_ERR_NONE == sts) { sts = m_TaskPool.SynchronizeFirstTask(); - if (sts == MFX_ERR_GPU_HANG) + if (sts == MFX_ERR_GPU_HANG && m_bSoftRobustFlag) { m_bInsertIDR = true; + m_TaskPool.ClearTasks();//may be not needed + FreeSurfacePool(m_pEncSurfaces, m_EncResponse.NumFrameActual); sts = MFX_ERR_NONE; } } @@ -2347,7 +2408,11 @@ void CEncodingPipeline::PrintInfo() msdk_printf(MSDK_STRING("Target usage\t%s\n"), TargetUsageToStr(m_mfxEncParams.mfx.TargetUsage)); const msdk_char* sMemType = +#if defined(_WIN32) || defined(_WIN64) + m_memType == D3D9_MEMORY ? MSDK_STRING("d3d") +#else m_memType == D3D9_MEMORY ? MSDK_STRING("vaapi") +#endif : (m_memType == D3D11_MEMORY ? MSDK_STRING("d3d11") : MSDK_STRING("system")); msdk_printf(MSDK_STRING("Memory type\t%s\n"), sMemType); diff --git a/samples/sample_encode/src/sample_encode.cpp b/samples/sample_encode/src/sample_encode.cpp index 8c1271ca3..f359317b0 100644 --- a/samples/sample_encode/src/sample_encode.cpp +++ b/samples/sample_encode/src/sample_encode.cpp @@ -1,5 +1,5 @@ /******************************************************************************\ -Copyright (c) 2005-2018, Intel Corporation +Copyright (c) 2005-2019, Intel Corporation All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: @@ -36,7 +36,6 @@ or https://software.intel.com/en-us/media-client-solutions-support. } \ } - #ifndef MFX_VERSION #error MFX_VERSION not defined #endif @@ -96,6 +95,7 @@ void PrintHelp(msdk_char *strAppName, const msdk_char *strErrorMessage, ...) msdk_printf(MSDK_STRING(" [-path path] - path to plugin (valid only in pair with -p option)\n")); msdk_printf(MSDK_STRING(" [-async] - depth of asynchronous pipeline. default value is 4. must be between 1 and 20.\n")); msdk_printf(MSDK_STRING(" [-gpucopy::] Enable or disable GPU copy mode\n")); + msdk_printf(MSDK_STRING(" [-robust:soft] - Recovery from GPU hang by inserting an IDR\n")); msdk_printf(MSDK_STRING(" [-vbr] - variable bitrate control\n")); msdk_printf(MSDK_STRING(" [-cbr] - constant bitrate control\n")); msdk_printf(MSDK_STRING(" [-qvbr quality] - variable bitrate control algorithm with constant quality. Quality in range [1,51]. 1 is the best quality.\n")); @@ -152,6 +152,10 @@ void PrintHelp(msdk_char *strAppName, const msdk_char *strErrorMessage, ...) msdk_printf(MSDK_STRING(" [-dump fileName] - dump MSDK components configuration to the file in text form\n")); msdk_printf(MSDK_STRING(" [-usei] - insert user data unregistered SEI. eg: 7fc92488825d11e7bb31be2e44b06b34:0:MSDK (uuid:type<0-preifx/1-suffix>:message)\n")); msdk_printf(MSDK_STRING(" the suffix SEI for HEVCe can be inserted when CQP used or HRD disabled\n")); +#if (MFX_VERSION >= MFX_VERSION_NEXT) + msdk_printf(MSDK_STRING(" [-dblk_alpha] - alpha deblocking parameter. In range[-12,12]. 0 by default.\n")); + msdk_printf(MSDK_STRING(" [-dblk_beta] - beta deblocking parameter. In range[-12,12]. 0 by default.\n")); +#endif #if (MFX_VERSION >= 1024) msdk_printf(MSDK_STRING(" [-extbrc:] - External BRC for AVC and HEVC encoders\n")); #endif @@ -159,9 +163,12 @@ void PrintHelp(msdk_char *strAppName, const msdk_char *strErrorMessage, ...) msdk_printf(MSDK_STRING(" [-ExtBrcAdaptiveLTR:] - Set AdaptiveLTR for implicit extbrc")); #endif msdk_printf(MSDK_STRING("Example: %s h265 -i InputYUVFile -o OutputEncodedFile -w width -h height -hw -p 2fca99749fdb49aeb121a5b63ef568f7\n"), strAppName); + msdk_printf(MSDK_STRING(" [-preset ] - Use particular preset for encoding parameters\n")); + msdk_printf(MSDK_STRING(" [-pp] - Print preset parameters\n")); msdk_printf(MSDK_STRING("\nExample: %s h265 -i InputYUVFile -o OutputEncodedFile -w width -h height -hw -p 2fca99749fdb49aeb121a5b63ef568f7\n"), strAppName); #if D3D_SURFACES_SUPPORT msdk_printf(MSDK_STRING(" [-d3d] - work with d3d surfaces\n")); msdk_printf(MSDK_STRING(" [-d3d11] - work with d3d11 surfaces\n")); + msdk_printf(MSDK_STRING(" [-single_texture_d3d11 ] - single texture mode for d3d11 allocator \n")); msdk_printf(MSDK_STRING("Example: %s h264|h265|mpeg2|jpeg -i InputYUVFile -o OutputEncodedFile -w width -h height -d3d -hw \n"), strAppName); msdk_printf(MSDK_STRING("Example for MVC: %s mvc -i InputYUVFile_1 -i InputYUVFile_2 -o OutputEncodedFile -w width -h height \n"), strAppName); #endif @@ -443,6 +450,10 @@ mfxStatus ParseInputString(msdk_char* strInput[], mfxU8 nArgNum, sInputParams* p { pParams->nRateControlMethod = MFX_RATECONTROL_CBR; } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-single_texture_d3d11"))) + { + pParams->bSingleTexture = true; + } else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-qvbr"))) { pParams->nRateControlMethod = MFX_RATECONTROL_QVBR; @@ -857,6 +868,47 @@ mfxStatus ParseInputString(msdk_char* strInput[], mfxU8 nArgNum, sInputParams* p pParams->ExtBrcAdaptiveLTR = MFX_CODINGOPTION_OFF;; } #endif +#if (MFX_VERSION >= MFX_VERSION_NEXT) + else if(0 == msdk_strcmp(strInput[i], MSDK_STRING("-dblk_alpha"))) + { + VAL_CHECK(i + 1 >= nArgNum, i, strInput[i]); + if (MFX_ERR_NONE != msdk_opt_read(strInput[++i], pParams->DeblockingAlphaTcOffset)) + { + PrintHelp(strInput[0], MSDK_STRING("Alpha deblocking parameter is invalid")); + return MFX_ERR_UNSUPPORTED; + } + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-dblk_beta"))) + { + VAL_CHECK(i + 1 >= nArgNum, i, strInput[i]); + if (MFX_ERR_NONE != msdk_opt_read(strInput[++i], pParams->DeblockingBetaOffset)) + { + PrintHelp(strInput[0], MSDK_STRING("Beta deblocking parameter is invalid")); + return MFX_ERR_UNSUPPORTED; + } + } +#endif + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-pp"))) + { + pParams->shouldPrintPresets = true; + } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-preset"))) + { + msdk_char presetName[MSDK_MAX_FILENAME_LEN]; + VAL_CHECK(i + 1 >= nArgNum, i, strInput[i]); + if (MFX_ERR_NONE != msdk_opt_read(strInput[++i], presetName)) + { + PrintHelp(strInput[0], MSDK_STRING("Preset Name is not defined")); + return MFX_ERR_UNSUPPORTED; + } + + pParams->PresetMode = CPresetManager::PresetNameToMode(presetName); + if (pParams->PresetMode == PRESET_MAX_MODES) + { + PrintHelp(strInput[0], MSDK_STRING("Preset Name is invalid")); + return MFX_ERR_UNSUPPORTED; + } + } else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-amfs:on"))) { pParams->nAdaptiveMaxFrameSize = MFX_CODINGOPTION_ON; @@ -870,6 +922,10 @@ mfxStatus ParseInputString(msdk_char* strInput[], mfxU8 nArgNum, sInputParams* p { pParams->enableQSVFF=true; } + else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-robust:soft"))) + { + pParams->bSoftRobustFlag = true; + } else if (0 == msdk_strcmp(strInput[i], MSDK_STRING("-num_slice"))) { VAL_CHECK(i+1 >= nArgNum, i, strInput[i]); @@ -1182,11 +1238,6 @@ mfxStatus ParseInputString(msdk_char* strInput[], mfxU8 nArgNum, sInputParams* p pParams->numViews = nviews; } - if (MFX_TARGETUSAGE_BEST_QUALITY != pParams->nTargetUsage && MFX_TARGETUSAGE_BEST_SPEED != pParams->nTargetUsage) - { - pParams->nTargetUsage = MFX_TARGETUSAGE_BALANCED; - } - if (pParams->dFrameRate <= 0) { pParams->dFrameRate = 30; @@ -1203,13 +1254,6 @@ mfxStatus ParseInputString(msdk_char* strInput[], mfxU8 nArgNum, sInputParams* p pParams->nDstHeight = pParams->nHeight; } - // calculate default bitrate based on the resolution (a parameter for encoder, so Dst resolution is used) - if (pParams->nBitRate == 0) - { - pParams->nBitRate = CalculateDefaultBitrate(pParams->CodecId, pParams->nTargetUsage, pParams->nDstWidth, - pParams->nDstHeight, pParams->dFrameRate); - } - if (!pParams->nPicStruct) { pParams->nPicStruct = MFX_PICSTRUCT_PROGRESSIVE; @@ -1221,15 +1265,6 @@ mfxStatus ParseInputString(msdk_char* strInput[], mfxU8 nArgNum, sInputParams* p return MFX_ERR_UNSUPPORTED; } - if (pParams->nRateControlMethod == MFX_RATECONTROL_AVBR) - { - if (pParams->Accuracy > 100) - { - msdk_printf(MSDK_STRING("For AVBR BRC, the assigned accuracy exceeds 100, now set it to 100\n")); - pParams->Accuracy = 100; - } - } - if ((pParams->nMaxSliceSize) && (!pParams->bUseHWLib)) { PrintHelp(strInput[0], MSDK_STRING("MaxSliceSize option is supported only with -hw option!")); @@ -1284,22 +1319,12 @@ mfxStatus ParseInputString(msdk_char* strInput[], mfxU8 nArgNum, sInputParams* p return MFX_ERR_UNSUPPORTED; } - if (pParams->nAsyncDepth == 0) - { - pParams->nAsyncDepth = 4; //set by default; - } - // Ignoring user-defined Async Depth for LA if (pParams->nMaxSliceSize) { pParams->nAsyncDepth = 1; } - if (pParams->nRateControlMethod == 0) - { - pParams->nRateControlMethod = MFX_RATECONTROL_CBR; - } - if(pParams->UseRegionEncode) { if(pParams->CodecId != MFX_CODEC_HEVC) @@ -1324,6 +1349,61 @@ mfxStatus ParseInputString(msdk_char* strInput[], mfxU8 nArgNum, sInputParams* p return MFX_ERR_NONE; } + +void ModifyParamsUsingPresets(sInputParams& params) +{ + COutputPresetParameters presetParams = CPresetManager::Inst.GetPreset(params.PresetMode, params.CodecId,params.dFrameRate, params.nWidth, params.nHeight, params.bUseHWLib); + + if (presetParams.RateControlMethod == MFX_RATECONTROL_LA_EXT) + { + // Currently sample_encode does not support external LA, so we will use ExtBRC instead + presetParams.RateControlMethod = MFX_RATECONTROL_VBR; + presetParams.LookAheadDepth = 0; + presetParams.ExtBRCUsage = EXTBRC_ON; + } + + if (params.shouldPrintPresets) + { + msdk_printf(MSDK_STRING("Preset-controlled parameters (%s):\n"), presetParams.PresetName.c_str()); + } + MODIFY_AND_PRINT_PARAM(params.nAdaptiveMaxFrameSize, AdaptiveMaxFrameSize,params.shouldPrintPresets); + MODIFY_AND_PRINT_PARAM(params.nAsyncDepth, AsyncDepth, params.shouldPrintPresets); + MODIFY_AND_PRINT_PARAM(params.nBRefType, BRefType, params.shouldPrintPresets); +// MODIFY_AND_PRINT_PARAM(params., EnableBPyramid); +// MODIFY_AND_PRINT_PARAM(params., EnablePPyramid); + MODIFY_AND_PRINT_PARAM(params.nGopPicSize, GopPicSize, params.shouldPrintPresets); + MODIFY_AND_PRINT_PARAM(params.nGopRefDist, GopRefDist, params.shouldPrintPresets); + MODIFY_AND_PRINT_PARAM(params.IntRefCycleDist, IntRefCycleDist, params.shouldPrintPresets); + MODIFY_AND_PRINT_PARAM(params.IntRefCycleSize, IntRefCycleSize, params.shouldPrintPresets); + MODIFY_AND_PRINT_PARAM(params.IntRefQPDelta, IntRefQPDelta, params.shouldPrintPresets); + MODIFY_AND_PRINT_PARAM(params.IntRefType, IntRefType, params.shouldPrintPresets); + MODIFY_AND_PRINT_PARAM(params.nLADepth, LookAheadDepth, params.shouldPrintPresets); + MODIFY_AND_PRINT_PARAM(params.LowDelayBRC, LowDelayBRC, params.shouldPrintPresets); + MODIFY_AND_PRINT_PARAM(params.nMaxFrameSize, MaxFrameSize, params.shouldPrintPresets); + if (!params.nRateControlMethod) + { + // Use preset ExtBRC parameter only if rate control mode was not set manually + MODIFY_AND_PRINT_PARAM_EXT(params.nExtBRC, ExtBRCUsage, (ExtBRCType)presetParams.ExtBRCUsage, params.shouldPrintPresets); + } + MODIFY_AND_PRINT_PARAM(params.nRateControlMethod, RateControlMethod, params.shouldPrintPresets); + + if (params.nRateControlMethod != MFX_RATECONTROL_CQP) + { + MODIFY_AND_PRINT_PARAM(params.nBitRate, TargetKbps, params.shouldPrintPresets); + MODIFY_AND_PRINT_PARAM(params.MaxKbps, MaxKbps, params.shouldPrintPresets); + presetParams.BufferSizeInKB = params.nBitRate; // Update bitrate to reflect manually set bitrate. BufferSize should be enough for 1 second of video + MODIFY_AND_PRINT_PARAM(params.BufferSizeInKB, BufferSizeInKB, params.shouldPrintPresets); + } + + MODIFY_AND_PRINT_PARAM(params.nTargetUsage, TargetUsage, params.shouldPrintPresets); + MODIFY_AND_PRINT_PARAM(params.WeightedBiPred, WeightedBiPred, params.shouldPrintPresets); + MODIFY_AND_PRINT_PARAM(params.WeightedPred, WeightedPred, params.shouldPrintPresets); + if (params.shouldPrintPresets) + { + msdk_printf(MSDK_STRING("\n")); + } +} + CEncodingPipeline* CreatePipeline(const sInputParams& params) { if(params.UseRegionEncode) @@ -1341,14 +1421,21 @@ CEncodingPipeline* CreatePipeline(const sInputParams& params) } +#if defined(_WIN32) || defined(_WIN64) +int _tmain(int argc, msdk_char *argv[]) +#else int main(int argc, char *argv[]) +#endif { sInputParams Params = {}; // input parameters from command line std::unique_ptr pPipeline; mfxStatus sts = MFX_ERR_NONE; // return value check + // Parsing Input stream workign with presets sts = ParseInputString(argv, (mfxU8)argc, &Params); + ModifyParamsUsingPresets(Params); + MSDK_CHECK_PARSE_RESULT(sts, MFX_ERR_NONE, 1); // Choosing which pipeline to use diff --git a/samples/sample_hevc_fei_abr/include/dso/hevc2_parser.h b/samples/sample_hevc_fei_abr/include/dso/hevc2_parser.h index 0b092e1fd..b11a0d55b 100644 --- a/samples/sample_hevc_fei_abr/include/dso/hevc2_parser.h +++ b/samples/sample_hevc_fei_abr/include/dso/hevc2_parser.h @@ -1,4 +1,4 @@ -// Copyright (c) 2018 Intel Corporation +// Copyright (c) 2019 Intel Corporation // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -64,9 +64,6 @@ template Bs32u CntBits(T f, Bs32u n = 0) } inline Bs32u CeilLog2(Bs32u x) { Bs32u l = 0; while (x > (1U << l)) l++; return l; } -#if 0 -inline Bs32u FloorLog2(Bs32u x) { Bs32u l = 0; while (x >>= 1) ++l; return l; } -#else inline Bs32u CountOnes(Bs32u x) { x -= ((x >> 1) & 0x55555555); @@ -86,7 +83,7 @@ inline Bs32u FloorLog2(Bs32u x) return (CountOnes(x) - 1); //return CountOnes(x >> 1); } -#endif + template inline T CeilDiv(T x, T y) { return (x + y - 1) / y; } inline bool isSlice(NALU& nalu) { diff --git a/samples/sample_hevc_fei_abr/src/dso/bs_reader2.cpp b/samples/sample_hevc_fei_abr/src/dso/bs_reader2.cpp index 1b5d82273..4a1b48577 100644 --- a/samples/sample_hevc_fei_abr/src/dso/bs_reader2.cpp +++ b/samples/sample_hevc_fei_abr/src/dso/bs_reader2.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2018 Intel Corporation +// Copyright (c) 2019 Intel Corporation // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -71,22 +71,6 @@ bool File::UpdateBuffer(Bs8u*& start, Bs8u*& cur, Bs8u*& end, Bs32u keepBytes) end += fread(end, 1, m_b.size() - (end - start), m_f); -#if 0 - printf("\n"); - for (Bs32u i = 0; i < keepBytes; i++) - printf("%02X ", start[i]); - printf("\n"); - - for (Bs32u i = 0; i < (end - cur); i++) - { - if (i%16 == 0) - printf("\n"); - printf("%02X ", cur[i]); - } - printf("\n"); - fflush(stdout); -#endif - return true; } diff --git a/samples/sample_misc/wayland/src/class_wayland.cpp b/samples/sample_misc/wayland/src/class_wayland.cpp index 6fe7a87e2..5d0e62089 100644 --- a/samples/sample_misc/wayland/src/class_wayland.cpp +++ b/samples/sample_misc/wayland/src/class_wayland.cpp @@ -1,5 +1,5 @@ /******************************************************************************\ -Copyright (c) 2005-2018, Intel Corporation +Copyright (c) 2005-2019, Intel Corporation All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/samples/sample_multi_transcode/include/pipeline_transcode.h b/samples/sample_multi_transcode/include/pipeline_transcode.h index d382fb859..77013eb26 100644 --- a/samples/sample_multi_transcode/include/pipeline_transcode.h +++ b/samples/sample_multi_transcode/include/pipeline_transcode.h @@ -52,6 +52,7 @@ or https://software.intel.com/en-us/media-client-solutions-support. #include "plugin_loader.h" #include "sample_defs.h" #include "plugin_utils.h" +#include "preset_manager.h" #if (MFX_VERSION >= 1024) #include "brc_routines.h" @@ -184,6 +185,7 @@ namespace TranscodingSample bool bIsPerf; // special performance mode. Use pre-allocated bitstreams, output mfxU16 nThreadsNum; // number of internal session threads number bool bRobustFlag; // Robust transcoding mode. Allows auto-recovery after hardware errors + bool bSoftRobustFlag; mfxU32 EncodeId; // type of output coded video mfxU32 DecodeId; // type of input coded video @@ -197,6 +199,7 @@ namespace TranscodingSample mfxU16 nTargetUsage; mfxF64 dDecoderFrameRateOverride; mfxF64 dEncoderFrameRateOverride; + mfxU16 EncoderPicstructOverride; mfxF64 dVPPOutFramerate; mfxU16 nBitRate; mfxU16 nBitRateMultiplier; @@ -305,6 +308,16 @@ namespace TranscodingSample ExtBRCType nExtBRC; + mfxU16 nAdaptiveMaxFrameSize; + mfxU16 LowDelayBRC; + + mfxU16 IntRefType; + mfxU16 IntRefCycleSize; + mfxU16 IntRefQPDelta; + mfxU16 IntRefCycleDist; + + mfxU32 nMaxFrameSize; + #if (MFX_VERSION >= 1025) mfxU16 numMFEFrames; mfxU16 MFMode; @@ -322,6 +335,9 @@ namespace TranscodingSample #endif // defined(MFX_LIBVA_SUPPORT) CHWDevice *m_hwdev; + + EPresetModes PresetMode; + bool shouldPrintPresets; }; struct sInputParams: public __sInputParams @@ -648,6 +664,8 @@ namespace TranscodingSample mfxExtMVCSeqDesc GetDecMVCSeqDesc() const {return m_MVCSeqDesc;} + static void ModifyParamsUsingPresets(sInputParams& params, mfxF64 fps, mfxU32 width, mfxU32 height); + // alloc frames for all component mfxStatus AllocFrames(mfxFrameAllocRequest *pRequest, bool isDecAlloc); mfxStatus AllocFrames(); @@ -675,6 +693,8 @@ namespace TranscodingSample mfxStatus InitPluginMfxParams(sInputParams *pInParams); mfxStatus InitPreEncMfxParams(sInputParams *pInParams); + void FillFrameInfoForEncoding(mfxFrameInfo& info, sInputParams *pInParams); + mfxStatus AllocAndInitVppDoNotUse(sInputParams *pInParams); mfxStatus AllocMVCSeqDesc(); mfxStatus InitOpaqueAllocBuffers(); @@ -851,6 +871,7 @@ namespace TranscodingSample std::mutex m_mReset; std::mutex m_mStopSession; bool m_bRobustFlag; + bool m_bSoftGpuHangRecovery; bool isHEVCSW; diff --git a/samples/sample_multi_transcode/include/transcode_utils.h b/samples/sample_multi_transcode/include/transcode_utils.h index 3f82e9c42..d835b8938 100644 --- a/samples/sample_multi_transcode/include/transcode_utils.h +++ b/samples/sample_multi_transcode/include/transcode_utils.h @@ -1,5 +1,5 @@ /******************************************************************************\ -Copyright (c) 2005-2018, Intel Corporation +Copyright (c) 2005-2019, Intel Corporation All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: @@ -20,6 +20,12 @@ or https://software.intel.com/en-us/media-client-solutions-support. #ifndef __TRANSCODE_UTILS_H__ #define __TRANSCODE_UTILS_H__ +#if defined(_WIN32) || defined(_WIN64) +#include +#pragma warning(disable : 4201) +#include +#include +#endif #include #include @@ -29,15 +35,6 @@ struct D3DAllocatorParams; #pragma warning(disable: 4127) // constant expression -// Extensions for internal use, normally these macros are blank -#ifdef MOD_SMT -#include "extension_macros.h" -#else -#define MOD_SMT_CREATE_PIPELINE -#define MOD_SMT_PRINT_HELP -#define MOD_SMT_PARSE_INPUT -#endif - #ifndef MFX_VERSION #error MFX_VERSION not defined #endif @@ -90,6 +87,7 @@ namespace TranscodingSample msdk_tstring DumpLogFileName; mfxU32 m_nTimeout; bool bRobustFlag; + bool bSoftRobustFlag; bool shouldUseGreedyFormula; std::vector m_lines; private: diff --git a/samples/sample_multi_transcode/sample_multi_transcode.sln b/samples/sample_multi_transcode/sample_multi_transcode.sln new file mode 100644 index 000000000..cffb4b7ff --- /dev/null +++ b/samples/sample_multi_transcode/sample_multi_transcode.sln @@ -0,0 +1,48 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27703.2000 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sample_multi_transcode", "sample_multi_transcode.vcxproj", "{58D93FCA-FDE9-4D22-8E8D-402EEBACF2BD}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sample_common", "..\sample_common\sample_common.vcxproj", "{5FADB243-53C3-4776-A20F-8BD65C10CF41}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "vpp_plugin", "..\sample_plugins\vpp_plugin\vpp_plugin.vcxproj", "{474EF95D-3579-411B-9DDB-88B49F923D65}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Debug|x64 = Debug|x64 + Release|Win32 = Release|Win32 + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {58D93FCA-FDE9-4D22-8E8D-402EEBACF2BD}.Debug|Win32.ActiveCfg = Debug|Win32 + {58D93FCA-FDE9-4D22-8E8D-402EEBACF2BD}.Debug|Win32.Build.0 = Debug|Win32 + {58D93FCA-FDE9-4D22-8E8D-402EEBACF2BD}.Debug|x64.ActiveCfg = Debug|x64 + {58D93FCA-FDE9-4D22-8E8D-402EEBACF2BD}.Debug|x64.Build.0 = Debug|x64 + {58D93FCA-FDE9-4D22-8E8D-402EEBACF2BD}.Release|Win32.ActiveCfg = Release|Win32 + {58D93FCA-FDE9-4D22-8E8D-402EEBACF2BD}.Release|Win32.Build.0 = Release|Win32 + {58D93FCA-FDE9-4D22-8E8D-402EEBACF2BD}.Release|x64.ActiveCfg = Release|x64 + {58D93FCA-FDE9-4D22-8E8D-402EEBACF2BD}.Release|x64.Build.0 = Release|x64 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Debug|Win32.ActiveCfg = Debug|Win32 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Debug|Win32.Build.0 = Debug|Win32 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Debug|x64.ActiveCfg = Debug|x64 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Debug|x64.Build.0 = Debug|x64 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Release|Win32.ActiveCfg = Release|Win32 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Release|Win32.Build.0 = Release|Win32 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Release|x64.ActiveCfg = Release|x64 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Release|x64.Build.0 = Release|x64 + {474EF95D-3579-411B-9DDB-88B49F923D65}.Debug|Win32.ActiveCfg = Debug|Win32 + {474EF95D-3579-411B-9DDB-88B49F923D65}.Debug|Win32.Build.0 = Debug|Win32 + {474EF95D-3579-411B-9DDB-88B49F923D65}.Debug|x64.ActiveCfg = Debug|x64 + {474EF95D-3579-411B-9DDB-88B49F923D65}.Debug|x64.Build.0 = Debug|x64 + {474EF95D-3579-411B-9DDB-88B49F923D65}.Release|Win32.ActiveCfg = Release|Win32 + {474EF95D-3579-411B-9DDB-88B49F923D65}.Release|Win32.Build.0 = Release|Win32 + {474EF95D-3579-411B-9DDB-88B49F923D65}.Release|x64.ActiveCfg = Release|x64 + {474EF95D-3579-411B-9DDB-88B49F923D65}.Release|x64.Build.0 = Release|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/samples/sample_multi_transcode/sample_multi_transcode.vcxproj b/samples/sample_multi_transcode/sample_multi_transcode.vcxproj new file mode 100644 index 000000000..88d7378eb --- /dev/null +++ b/samples/sample_multi_transcode/sample_multi_transcode.vcxproj @@ -0,0 +1,284 @@ + + + + + Debug_WithDebugAPI + Win32 + + + Debug_WithDebugAPI + x64 + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {58D93FCA-FDE9-4D22-8E8D-402EEBACF2BD} + sample_multi_transcode + Win32Proj + + + + Application + false + Unicode + true + v141 + + + Application + false + Unicode + v141 + + + Application + false + Unicode + v141 + + + Application + false + Unicode + true + v141 + + + Application + false + Unicode + v141 + + + Application + false + Unicode + v141 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + <_ProjectFileVersion>10.0.30319.1 + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + $(OutDir)obj\$(ProjectName)\ + true + true + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + $(OutDir)obj\$(ProjectName)\ + true + true + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + false + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + false + sample_multi_transcode + sample_multi_transcode + sample_multi_transcode + sample_multi_transcode + sample_multi_transcode + sample_multi_transcode + + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\sample_common\include;$(ProjectDir)\..\sample_plugins\vpp_plugin\include;$(ProjectDir)\..\sample_plugins\rotate_cpu\include;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;_CONSOLE;_ITERATOR_DEBUG_LEVEL=0;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebug + + + Level4 + true + ProgramDatabase + + + /DYNAMICBASE /NXCOMPAT /SAFESEH %(AdditionalOptions) + libmfx_vs2015.lib;dxva2.lib;d3d9.lib;d3d11.lib;dxgi.lib;psapi.lib;%(AdditionalDependencies) + $(ProjectDir)..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + false + msvcrt.lib;msvcrtd.lib;LIBCMT.lib;%(IgnoreSpecificDefaultLibraries) + true + Console + MachineX86 + + + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\sample_common\include;$(ProjectDir)\..\sample_plugins\vpp_plugin\include;$(ProjectDir)\..\sample_plugins\rotate_cpu\include;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebug + + + Level4 + true + ProgramDatabase + + + /DYNAMICBASE /NXCOMPAT /SAFESEH %(AdditionalOptions) + libmfx_vs2015_d.lib;dxva2.lib;d3d9.lib;d3d11.lib;dxgi.lib;psapi.lib;%(AdditionalDependencies) + $(ProjectDir)..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + false + msvcrt.lib;msvcrtd.lib;LIBCMT.lib;%(IgnoreSpecificDefaultLibraries) + true + Console + MachineX86 + + + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\sample_common\include;$(ProjectDir)\..\sample_plugins\vpp_plugin\include;$(ProjectDir)\..\sample_plugins\rotate_cpu\include;%(AdditionalIncludeDirectories) + MultiThreadedDebug + Level4 + true + ProgramDatabase + WIN32;_DEBUG;_CONSOLE;_ITERATOR_DEBUG_LEVEL=0;%(PreprocessorDefinitions) + + + /DYNAMICBASE /NXCOMPAT %(AdditionalOptions) + libmfx_vs2015.lib;dxva2.lib;d3d9.lib;d3d11.lib;dxgi.lib;psapi.lib;%(AdditionalDependencies) + $(ProjectDir)..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;LIBCMT.lib;%(IgnoreSpecificDefaultLibraries) + true + $(TargetDir)$(TargetName).pdb + Console + + + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\sample_common\include;$(ProjectDir)\..\sample_plugins\vpp_plugin\include;$(ProjectDir)\..\sample_plugins\rotate_cpu\include;%(AdditionalIncludeDirectories) + MultiThreadedDebug + Level4 + true + ProgramDatabase + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + + + /DYNAMICBASE /NXCOMPAT %(AdditionalOptions) + libmfx_vs2015_d.lib;dxva2.lib;d3d9.lib;d3d11.lib;dxgi.lib;psapi.lib;%(AdditionalDependencies) + $(ProjectDir)..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;LIBCMT.lib;%(IgnoreSpecificDefaultLibraries) + true + $(TargetDir)$(TargetName).pdb + Console + + + + + false + $(ProjectDir)\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\sample_common\include;$(ProjectDir)\..\sample_plugins\vpp_plugin\include;$(ProjectDir)\..\sample_plugins\rotate_cpu\include;%(AdditionalIncludeDirectories) + WIN32;NDEBUG;_CONSOLE;SAVE_RECON;MFX_D3D11_SUPPORT=1;%(PreprocessorDefinitions) + MultiThreaded + + + Level4 + true + + + + + /DYNAMICBASE /NXCOMPAT /SAFESEH %(AdditionalOptions) + libmfx_vs2015.lib;dxva2.lib;d3d9.lib;d3d11.lib;dxgi.lib;psapi.lib;%(AdditionalDependencies) + $(ProjectDir)..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;%(IgnoreSpecificDefaultLibraries) + false + Console + true + true + MachineX86 + + + + + $(ProjectDir)\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\sample_common\include;$(ProjectDir)\..\sample_plugins\vpp_plugin\include;$(ProjectDir)\..\sample_plugins\rotate_cpu\include;%(AdditionalIncludeDirectories) + NDEBUG;_CONSOLE;SAVE_RECON;MFX_D3D11_SUPPORT=1;%(PreprocessorDefinitions) + MultiThreaded + Level4 + true + + + /DYNAMICBASE /NXCOMPAT %(AdditionalOptions) + libmfx_vs2015.lib;dxva2.lib;d3d9.lib;d3d11.lib;dxgi.lib;psapi.lib;%(AdditionalDependencies) + $(ProjectDir)..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;%(IgnoreSpecificDefaultLibraries) + false + Console + + + + + + + + + + + + + + + + + {5FADB243-53C3-4776-A20F-8BD65C10CF41} + false + + + {474EF95D-3579-411B-9DDB-88B49F923D65} + + + + + + \ No newline at end of file diff --git a/samples/sample_multi_transcode/src/pipeline_transcode.cpp b/samples/sample_multi_transcode/src/pipeline_transcode.cpp index 45b46e7be..e49c0de03 100644 --- a/samples/sample_multi_transcode/src/pipeline_transcode.cpp +++ b/samples/sample_multi_transcode/src/pipeline_transcode.cpp @@ -237,7 +237,7 @@ CTranscodingPipeline::CTranscodingPipeline(): m_VppCompParams.InputStream = NULL; m_VppCompParams.NumInputStream = 0; -#if MFX_VERSION >= 1023 +#if MFX_VERSION >= 1024 m_VppCompParams.NumTiles = 0; #endif m_CodingOption2.Header.BufferId = MFX_EXTBUFF_CODING_OPTION2; @@ -252,6 +252,7 @@ CTranscodingPipeline::CTranscodingPipeline(): outputStatistics.SetDirection(MSDK_STRING("Output")); m_numEncoders = 0; + m_encoderFourCC = 0; m_libvaBackend = 0; statisticsWindowSize=0; m_bIsFieldWeaving = false; @@ -260,6 +261,7 @@ CTranscodingPipeline::CTranscodingPipeline(): m_bForceStop = false; m_bIsInterOrJoined = false; m_bRobustFlag = false; + m_bSoftGpuHangRecovery = false; m_nRotationAngle = 0; m_bROIasQPMAP = false; m_bExtMBQP = false; @@ -2020,6 +2022,8 @@ mfxStatus CTranscodingPipeline::PutBS() { mfxStatus sts = MFX_ERR_NONE; ExtendedBS *pBitstreamEx = m_BSPool.front(); + MSDK_CHECK_POINTER(pBitstreamEx, MFX_ERR_NULL_PTR); + // get result coded stream, synchronize only if we still have sync point if(pBitstreamEx->Syncp) { @@ -2045,7 +2049,8 @@ mfxStatus CTranscodingPipeline::PutBS() pBitstreamEx->Bitstream.DataLength = 0; pBitstreamEx->Bitstream.DataOffset = 0; - m_BSPool.pop_front(); + if (m_BSPool.size()) + m_BSPool.pop_front(); m_pBSStore->Release(pBitstreamEx); return sts; @@ -2413,12 +2418,46 @@ mfxStatus CTranscodingPipeline::InitDecMfxParams(sInputParams *pInParams) return MFX_ERR_NONE; }// mfxStatus CTranscodingPipeline::InitDecMfxParams() + +void CTranscodingPipeline::FillFrameInfoForEncoding(mfxFrameInfo& info, sInputParams *pInParams) +{ + // Getting parameters from previous blocks + if (m_bIsVpp) + { + MSDK_MEMCPY_VAR(info, &m_mfxVppParams.vpp.Out, sizeof(mfxFrameInfo)); + } + else if (m_bIsPlugin) + { + MSDK_MEMCPY_VAR(info, &m_mfxPluginParams.vpp.Out, sizeof(mfxFrameInfo)); + } + else + { + MSDK_MEMCPY_VAR(info, &m_mfxDecParams.mfx.FrameInfo, sizeof(mfxFrameInfo)); + } + + if (pInParams->dEncoderFrameRateOverride) + { + ConvertFrameRate(pInParams->dEncoderFrameRateOverride, &info.FrameRateExtN, &info.FrameRateExtD); + } + else if (pInParams->dVPPOutFramerate) + { + ConvertFrameRate(pInParams->dVPPOutFramerate, &info.FrameRateExtN, &info.FrameRateExtD); + } +} + mfxStatus CTranscodingPipeline::InitEncMfxParams(sInputParams *pInParams) { MSDK_CHECK_POINTER(pInParams, MFX_ERR_NULL_PTR); + + FillFrameInfoForEncoding(m_mfxEncParams.mfx.FrameInfo,pInParams); + + MSDK_CHECK_ERROR(m_mfxEncParams.mfx.FrameInfo.FrameRateExtN * m_mfxEncParams.mfx.FrameInfo.FrameRateExtD, + 0, MFX_ERR_INVALID_VIDEO_PARAM); + m_mfxEncParams.mfx.CodecId = pInParams->EncodeId; m_mfxEncParams.mfx.TargetUsage = pInParams->nTargetUsage; // trade-off between quality and speed m_mfxEncParams.AsyncDepth = m_AsyncDepth; + #if (MFX_VERSION >= 1025) if(pInParams->numMFEFrames || pInParams->MFMode) { @@ -2458,31 +2497,13 @@ mfxStatus CTranscodingPipeline::InitEncMfxParams(sInputParams *pInParams) m_mfxEncParams.mfx.LowPower = MFX_CODINGOPTION_ON; } - if (m_bIsVpp) - { - MSDK_MEMCPY_VAR(m_mfxEncParams.mfx.FrameInfo, &m_mfxVppParams.vpp.Out, sizeof(mfxFrameInfo)); - } - else if (m_bIsPlugin) - { - MSDK_MEMCPY_VAR(m_mfxEncParams.mfx.FrameInfo, &m_mfxPluginParams.vpp.Out, sizeof(mfxFrameInfo)); - } - else - { - MSDK_MEMCPY_VAR(m_mfxEncParams.mfx.FrameInfo, &m_mfxDecParams.mfx.FrameInfo, sizeof(mfxFrameInfo)); - } - // leave PAR unset to avoid MPEG2 encoder rejecting streams with unsupported DAR m_mfxEncParams.mfx.FrameInfo.AspectRatioW = m_mfxEncParams.mfx.FrameInfo.AspectRatioH = 0; // calculate default bitrate based on resolution and framerate - - if (pInParams->dEncoderFrameRateOverride) - { - ConvertFrameRate(pInParams->dEncoderFrameRateOverride, &m_mfxEncParams.mfx.FrameInfo.FrameRateExtN, &m_mfxEncParams.mfx.FrameInfo.FrameRateExtD); - } - else if(pInParams->dVPPOutFramerate) + if (pInParams->EncoderPicstructOverride) { - ConvertFrameRate(pInParams->dVPPOutFramerate, &m_mfxEncParams.mfx.FrameInfo.FrameRateExtN, &m_mfxEncParams.mfx.FrameInfo.FrameRateExtD); + m_mfxEncParams.mfx.FrameInfo.PicStruct = pInParams->EncoderPicstructOverride; } MSDK_CHECK_ERROR(m_mfxEncParams.mfx.FrameInfo.FrameRateExtN * m_mfxEncParams.mfx.FrameInfo.FrameRateExtD, @@ -2568,12 +2589,18 @@ MFX_IOPATTERN_IN_VIDEO_MEMORY : MFX_IOPATTERN_IN_SYSTEM_MEMORY); // configure and attach external parameters if (pInParams->bLABRC || pInParams->nMaxSliceSize || pInParams->nBRefType - || (pInParams->nExtBRC && (pInParams->EncodeId == MFX_CODEC_HEVC || pInParams->EncodeId == MFX_CODEC_AVC))) + || (pInParams->nExtBRC && (pInParams->EncodeId == MFX_CODEC_HEVC || pInParams->EncodeId == MFX_CODEC_AVC)) || + pInParams->IntRefType || pInParams->IntRefCycleSize || pInParams->IntRefQPDelta || pInParams->nMaxFrameSize) { m_CodingOption2.LookAheadDepth = pInParams->nLADepth; m_CodingOption2.MaxSliceSize = pInParams->nMaxSliceSize; + m_CodingOption2.MaxFrameSize = pInParams->nMaxFrameSize; m_CodingOption2.BRefType = pInParams->nBRefType; + m_CodingOption2.IntRefType = pInParams->IntRefType; + m_CodingOption2.IntRefCycleSize = pInParams->IntRefCycleSize; + m_CodingOption2.IntRefQPDelta = pInParams->IntRefQPDelta; + if (pInParams->nExtBRC != EXTBRC_DEFAULT && (pInParams->EncodeId == MFX_CODEC_HEVC || pInParams->EncodeId == MFX_CODEC_AVC)) { m_CodingOption2.ExtBRC =(mfxU16)(pInParams->nExtBRC == EXTBRC_OFF ? MFX_CODINGOPTION_OFF : MFX_CODINGOPTION_ON); @@ -2615,10 +2642,16 @@ MFX_IOPATTERN_IN_VIDEO_MEMORY : MFX_IOPATTERN_IN_SYSTEM_MEMORY); } #endif - if (pInParams->WeightedPred || pInParams->WeightedBiPred) + if (pInParams->WeightedPred || pInParams->WeightedBiPred || + pInParams->IntRefCycleDist || + pInParams->nAdaptiveMaxFrameSize || pInParams->LowDelayBRC) { m_CodingOption3.WeightedPred = pInParams->WeightedPred; m_CodingOption3.WeightedBiPred = pInParams->WeightedBiPred; + m_CodingOption3.LowDelayBRC = pInParams->LowDelayBRC; + m_CodingOption3.IntRefCycleDist = pInParams->IntRefCycleDist; + m_CodingOption3.AdaptiveMaxFrameSize = pInParams->nAdaptiveMaxFrameSize; + addCodingOpt3 = true; } #if (MFX_VERSION >= 1026) @@ -2680,13 +2713,10 @@ MFX_IOPATTERN_IN_VIDEO_MEMORY : MFX_IOPATTERN_IN_SYSTEM_MEMORY); m_mfxEncParams.mfx.FrameInfo.ChromaFormat=FourCCToChroma(pInParams->EncoderFourCC); } - if (pInParams->GopPicSize) + // GopPicSize and GopRefDist values are always set using presets manager, but do not set them for JPEG + if (MFX_CODEC_JPEG != pInParams->EncodeId) { m_mfxEncParams.mfx.GopPicSize = pInParams->GopPicSize; - } - - if (pInParams->GopRefDist) - { m_mfxEncParams.mfx.GopRefDist = pInParams->GopRefDist; } @@ -3018,7 +3048,7 @@ mfxStatus CTranscodingPipeline::AddLaStreams(mfxU16 width, mfxU16 height) break; } -#if MFX_VERSION >= 1023 +#if MFX_VERSION >= 1024 m_VppCompParams.NumTiles = pInParams->numTiles4Comp; #endif @@ -3029,7 +3059,7 @@ mfxStatus CTranscodingPipeline::AddLaStreams(mfxU16 width, mfxU16 height) m_VppCompParams.InputStream[i].DstY = pInParams->pVppCompDstRects[i].DstY; m_VppCompParams.InputStream[i].DstW = pInParams->pVppCompDstRects[i].DstW; m_VppCompParams.InputStream[i].DstH = pInParams->pVppCompDstRects[i].DstH; -#if MFX_VERSION >= 1023 +#if MFX_VERSION >= 1024 m_VppCompParams.InputStream[i].TileId = pInParams->pVppCompDstRects[i].TileId; #endif m_VppCompParams.InputStream[i].GlobalAlpha = 0; @@ -3575,6 +3605,7 @@ mfxStatus CTranscodingPipeline::Init(sInputParams *pParams, m_numEncoders = 0; m_bUseOverlay = pParams->DecodeId == MFX_CODEC_RGB4 ? true : false; m_bRobustFlag = pParams->bRobustFlag; + m_bSoftGpuHangRecovery = pParams->bSoftRobustFlag; m_nRotationAngle = pParams->nRotationAngle; m_sGenericPluginPath = pParams->strVPPPluginDLLPath; m_decoderPluginParams = pParams->decoderPluginParams; @@ -3767,6 +3798,14 @@ mfxStatus CTranscodingPipeline::Init(sInputParams *pParams, sts = VPPPreInit(pParams); MSDK_CHECK_STATUS(sts, "VPPPreInit failed"); + // Adjusting encoding settings according to selected preset + if (Sink != pParams->eMode) + { + mfxFrameInfo eInfo; + FillFrameInfoForEncoding(eInfo, pParams); + ModifyParamsUsingPresets(*pParams, ((double)eInfo.FrameRateExtN) / eInfo.FrameRateExtD, eInfo.Width, eInfo.Height); + } + // LA component initialization sts = PreEncPreInit(pParams); MSDK_CHECK_STATUS(sts, "PreEncPreInit failed"); @@ -4077,11 +4116,18 @@ void CTranscodingPipeline::SetNumFramesForReset(mfxU32 nFrames) void CTranscodingPipeline::HandlePossibleGpuHang(mfxStatus & sts) { - if (sts == MFX_ERR_GPU_HANG && !m_bRobustFlag) + if (sts == MFX_ERR_GPU_HANG && m_bSoftGpuHangRecovery) { - msdk_printf(MSDK_STRING("GPU hang happened\n")); - sts = MFX_ERR_NONE; + msdk_printf(MSDK_STRING("[WARNING] GPU hang happened. Inserting an IDR and continuing transcoding.\n")); m_bInsertIDR = true; + for (BSList::iterator it = m_BSPool.begin(); it != m_BSPool.end(); it++) + { + (*it)->IsFree = true; + (*it)->Bitstream.DataOffset = 0; + (*it)->Bitstream.DataLength = 0; + } + m_BSPool.clear(); + sts = MFX_ERR_NONE; } } @@ -4373,7 +4419,7 @@ mfxStatus CTranscodingPipeline::Reset() { if (m_bIsPlugin && m_bIsVpp) { - mfxFrameAllocRequest request[2] {}; + mfxFrameAllocRequest request[2] = { }; sts = m_pmfxVPP->QueryIOSurf(&m_mfxPluginParams, request, &m_mfxVppParams); MSDK_CHECK_STATUS(sts, "m_pmfxVPP->QueryIOSurf failed"); @@ -4450,7 +4496,10 @@ mfxStatus CTranscodingPipeline::AllocateSufficientBuffer(mfxBitstream* pBS) // if encoder provided us information about buffer size if (0 != par.mfx.BufferSizeInKB) { - new_size = par.mfx.BufferSizeInKB * 1000; + //--- If value calculated basing on par.mfx.BufferSizeInKB is too low, just double the buffer size + new_size = par.mfx.BufferSizeInKB * 1000u > pBS->MaxLength ? + par.mfx.BufferSizeInKB * 1000u : + pBS->MaxLength*2; } else { @@ -4740,3 +4789,66 @@ mfxStatus FileBitstreamProcessor::ResetOutput() } return MFX_ERR_NONE; } + +void CTranscodingPipeline::ModifyParamsUsingPresets(sInputParams& params, mfxF64 fps, mfxU32 width, mfxU32 height) +{ + + COutputPresetParameters presetParams = CPresetManager::Inst.GetPreset(params.PresetMode, params.EncodeId, fps, width, height, params.libType != MFX_IMPL_SOFTWARE); + + if (params.shouldPrintPresets) + { + msdk_printf(MSDK_STRING("Preset-controlled parameters (%s):\n"), presetParams.PresetName.c_str()); + } + + if (!params.nRateControlMethod) + { + MODIFY_AND_PRINT_PARAM_EXT(params.nExtBRC, ExtBRCUsage, (ExtBRCType)presetParams.ExtBRCUsage, params.shouldPrintPresets); + } + if (presetParams.RateControlMethod == MFX_RATECONTROL_LA_EXT) + { + // Use external LA + params.bEnableExtLA = true; + params.nRateControlMethod = 0; + if(params.shouldPrintPresets) + { + msdk_printf(MSDK_STRING("RateControlMethod: ExtLA\n")); + } + } + else + { + MODIFY_AND_PRINT_PARAM(params.nRateControlMethod, RateControlMethod, params.shouldPrintPresets); + } + + MODIFY_AND_PRINT_PARAM(params.nAdaptiveMaxFrameSize, AdaptiveMaxFrameSize, params.shouldPrintPresets); + MODIFY_AND_PRINT_PARAM(params.nAsyncDepth, AsyncDepth, params.shouldPrintPresets); + MODIFY_AND_PRINT_PARAM(params.nBRefType, BRefType, params.shouldPrintPresets); + MODIFY_AND_PRINT_PARAM(params.bEnableBPyramid, EnableBPyramid, params.shouldPrintPresets); + // MODIFY_AND_PRINT_PARAM(params., EnablePPyramid, params.shouldPrintPresets); + MODIFY_AND_PRINT_PARAM(params.GopRefDist, GopRefDist, params.shouldPrintPresets); + MODIFY_AND_PRINT_PARAM(params.IntRefCycleDist, IntRefCycleDist, params.shouldPrintPresets); + MODIFY_AND_PRINT_PARAM(params.IntRefCycleSize, IntRefCycleSize, params.shouldPrintPresets); + MODIFY_AND_PRINT_PARAM(params.IntRefQPDelta, IntRefQPDelta, params.shouldPrintPresets); + MODIFY_AND_PRINT_PARAM(params.IntRefType, IntRefType, params.shouldPrintPresets); + MODIFY_AND_PRINT_PARAM(params.LowDelayBRC, LowDelayBRC, params.shouldPrintPresets); + MODIFY_AND_PRINT_PARAM(params.nTargetUsage, TargetUsage, params.shouldPrintPresets); + MODIFY_AND_PRINT_PARAM(params.WeightedBiPred, WeightedBiPred, params.shouldPrintPresets); + MODIFY_AND_PRINT_PARAM(params.WeightedPred, WeightedPred, params.shouldPrintPresets); + + MODIFY_AND_PRINT_PARAM(params.GopPicSize, GopPicSize, params.shouldPrintPresets); + + if (params.nRateControlMethod != MFX_RATECONTROL_CQP) + { + MODIFY_AND_PRINT_PARAM(params.MaxKbps, MaxKbps, params.shouldPrintPresets); + MODIFY_AND_PRINT_PARAM(params.nBitRate, TargetKbps, params.shouldPrintPresets); + presetParams.BufferSizeInKB = params.nBitRate; // Update bitrate to reflect manually set bitrate. BufferSize should be enough for 1 second of video + MODIFY_AND_PRINT_PARAM(params.BufferSizeInKB, BufferSizeInKB, params.shouldPrintPresets); + } + + MODIFY_AND_PRINT_PARAM(params.nMaxFrameSize, MaxFrameSize, params.shouldPrintPresets); + MODIFY_AND_PRINT_PARAM(params.nLADepth, LookAheadDepth, params.shouldPrintPresets); + if (params.shouldPrintPresets) + { + msdk_printf(MSDK_STRING("\n")); + } + +} diff --git a/samples/sample_multi_transcode/src/sample_multi_transcode.cpp b/samples/sample_multi_transcode/src/sample_multi_transcode.cpp index 66b101aa4..602b8e41a 100644 --- a/samples/sample_multi_transcode/src/sample_multi_transcode.cpp +++ b/samples/sample_multi_transcode/src/sample_multi_transcode.cpp @@ -1,5 +1,5 @@ /******************************************************************************\ -Copyright (c) 2005-2018, Intel Corporation +Copyright (c) 2005-2019, Intel Corporation All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: @@ -18,6 +18,9 @@ or https://software.intel.com/en-us/media-client-solutions-support. \**********************************************************************************/ #include "mfx_samples_config.h" +#if defined(_WIN32) || defined(_WIN64) +#include +#endif #include "sample_multi_transcode.h" @@ -46,8 +49,6 @@ Launcher::~Launcher() CTranscodingPipeline* CreatePipeline() { - MOD_SMT_CREATE_PIPELINE; - return new CTranscodingPipeline; } @@ -85,7 +86,62 @@ mfxStatus Launcher::Init(int argc, msdk_char *argv[]) sts = VerifyCrossSessionsOptions(); MSDK_CHECK_STATUS(sts, "VerifyCrossSessionsOptions failed"); -#if defined(LIBVA_X11_SUPPORT) || defined(LIBVA_DRM_SUPPORT) || defined(ANDROID) +#if defined(_WIN32) || defined(_WIN64) + if (m_eDevType == MFX_HANDLE_D3D9_DEVICE_MANAGER) + { + m_pAllocParam.reset(new D3DAllocatorParams); + m_hwdev.reset(new CD3D9Device()); + /* The last param set in vector always describe VPP+ENCODE or Only VPP + * So, if we want to do rendering we need to do pass HWDev to CTranscodingPipeline */ + if (m_InputParamsArray[m_InputParamsArray.size() -1].eModeExt == VppCompOnly) + { + /* Rendering case */ + sts = m_hwdev->Init(NULL, 1, MSDKAdapter::GetNumber(0,MFX_IMPL_VIA_D3D9) ); + m_InputParamsArray[m_InputParamsArray.size() -1].m_hwdev = m_hwdev.get(); + } + else /* NO RENDERING*/ + { + sts = m_hwdev->Init(NULL, 0, MSDKAdapter::GetNumber(0,MFX_IMPL_VIA_D3D9) ); + } + MSDK_CHECK_STATUS(sts, "m_hwdev->Init failed"); + sts = m_hwdev->GetHandle(MFX_HANDLE_D3D9_DEVICE_MANAGER, (mfxHDL*)&hdl); + MSDK_CHECK_STATUS(sts, "m_hwdev->GetHandle failed"); + // set Device Manager to external dx9 allocator + D3DAllocatorParams *pD3DParams = dynamic_cast(m_pAllocParam.get()); + pD3DParams->pManager =(IDirect3DDeviceManager9*)hdl; + } +#if MFX_D3D11_SUPPORT + else if (m_eDevType == MFX_HANDLE_D3D11_DEVICE) + { + + m_pAllocParam.reset(new D3D11AllocatorParams); + m_hwdev.reset(new CD3D11Device()); + /* The last param set in vector always describe VPP+ENCODE or Only VPP + * So, if we want to do rendering we need to do pass HWDev to CTranscodingPipeline */ + if (m_InputParamsArray[m_InputParamsArray.size() -1].eModeExt == VppCompOnly) + { + /* Rendering case */ + sts = m_hwdev->Init(NULL, 1, MSDKAdapter::GetNumber(0,MFX_IMPL_VIA_D3D11) ); + m_InputParamsArray[m_InputParamsArray.size() -1].m_hwdev = m_hwdev.get(); + } + else /* NO RENDERING*/ + { + sts = m_hwdev->Init(NULL, 0, MSDKAdapter::GetNumber(0,MFX_IMPL_VIA_D3D11) ); + } + MSDK_CHECK_STATUS(sts, "m_hwdev->Init failed"); + sts = m_hwdev->GetHandle(MFX_HANDLE_D3D11_DEVICE, (mfxHDL*)&hdl); + MSDK_CHECK_STATUS(sts, "m_hwdev->GetHandle failed"); + // set Device to external dx11 allocator + D3D11AllocatorParams *pD3D11Params = dynamic_cast(m_pAllocParam.get()); + pD3D11Params->pDevice =(ID3D11Device*)hdl; + + // All sessions use same allocator parameters, so we'll take settings for the 0 session and use it for all + // (bSingleTexture is set for all sessions of for no one in VerifyCrossSessionsOptions()) + pD3D11Params->bUseSingleTexture = m_InputParamsArray[0].bSingleTexture; + + } +#endif +#elif defined(LIBVA_X11_SUPPORT) || defined(LIBVA_DRM_SUPPORT) || defined(ANDROID) if (m_eDevType == MFX_HANDLE_VA_DISPLAY) { mfxI32 libvaBackend = 0; @@ -111,6 +167,7 @@ mfxStatus Launcher::Init(int argc, msdk_char *argv[]) CVAAPIDeviceDRM* drmdev = dynamic_cast(m_hwdev.get()); pVAAPIParams->m_export_mode = vaapiAllocatorParams::CUSTOM_FLINK; pVAAPIParams->m_exporter = dynamic_cast(drmdev->getRenderer()); + } else if (params.libvaBackend == MFX_LIBVA_X11) { @@ -186,6 +243,7 @@ mfxStatus Launcher::Init(int argc, msdk_char *argv[]) // create sessions, allocators for (i = 0; i < m_InputParamsArray.size(); i++) { + msdk_printf(MSDK_STRING("Session %d:\n"), i); std::unique_ptr pAllocator(new GeneralAllocator); sts = pAllocator->Init(m_pAllocParam.get()); MSDK_CHECK_STATUS(sts, "pAllocator->Init failed"); @@ -761,7 +819,11 @@ mfxStatus Launcher::VerifyCrossSessionsOptions() if (MFX_IMPL_SOFTWARE != m_InputParamsArray[i].libType) { // TODO: can we avoid ifdef and use MFX_IMPL_VIA_VAAPI? -#if defined(LIBVA_SUPPORT) +#if defined(_WIN32) || defined(_WIN64) + m_eDevType = (MFX_IMPL_VIA_D3D11 == MFX_IMPL_VIA_MASK(m_InputParamsArray[i].libType))? + MFX_HANDLE_D3D11_DEVICE : + MFX_HANDLE_D3D9_DEVICE_MANAGER; +#elif defined(LIBVA_SUPPORT) m_eDevType = MFX_HANDLE_VA_DISPLAY; #endif } @@ -885,7 +947,11 @@ void Launcher::Close() } } // void Launcher::Close() +#if defined(_WIN32) || defined(_WIN64) +int _tmain(int argc, TCHAR *argv[]) +#else int main(int argc, char *argv[]) +#endif { mfxStatus sts; Launcher transcode; diff --git a/samples/sample_multi_transcode/src/transcode_utils.cpp b/samples/sample_multi_transcode/src/transcode_utils.cpp index 833bdcc17..32338703f 100644 --- a/samples/sample_multi_transcode/src/transcode_utils.cpp +++ b/samples/sample_multi_transcode/src/transcode_utils.cpp @@ -1,5 +1,5 @@ /******************************************************************************\ -Copyright (c) 2005-2018, Intel Corporation +Copyright (c) 2005-2019, Intel Corporation All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: @@ -173,7 +173,8 @@ void TranscodingSample::PrintHelp() #endif //ENABLE_MCTF_EXT #endif //ENABLE_MCTF - msdk_printf(MSDK_STRING(" -robust Recover from gpu hang errors as the come (by resetting components)\n")); + msdk_printf(MSDK_STRING(" -robust Recover from gpu hang errors as they come (by resetting components)\n")); + msdk_printf(MSDK_STRING(" -robust:soft Recover from gpu hang errors by inserting an IDR\n")); msdk_printf(MSDK_STRING(" -async Depth of asynchronous pipeline. default value 1\n")); msdk_printf(MSDK_STRING(" -join Join session with other session(s), by default sessions are not joined\n")); @@ -188,6 +189,8 @@ void TranscodingSample::PrintHelp() msdk_printf(MSDK_STRING(" -ext_allocator Force usage of external allocators\n")); msdk_printf(MSDK_STRING(" -sys Force usage of external system allocator\n")); + msdk_printf(MSDK_STRING(" -vpp::sys Set vpp output to system memory\n")); + msdk_printf(MSDK_STRING(" -vpp::vid Set vpp output to video memory\n")); msdk_printf(MSDK_STRING(" -fps \n")); msdk_printf(MSDK_STRING(" Transcoding frame rate limit\n")); msdk_printf(MSDK_STRING(" -pe Set encoding plugin for this particular session.\n")); @@ -198,7 +201,6 @@ void TranscodingSample::PrintHelp() msdk_printf(MSDK_STRING(" Direct GUID number can be used as well\n")); msdk_printf(MSDK_STRING("\n")); msdk_printf(MSDK_STRING("Pipeline description (encoding options):\n")); - MOD_SMT_PRINT_HELP; msdk_printf(MSDK_STRING(" -b \n")); msdk_printf(MSDK_STRING(" Encoded bit rate, valid for H.264, MPEG2 and MVC encoders\n")); msdk_printf(MSDK_STRING(" -bm Bitrate multiplier. Use it when required bitrate isn't fit into 16-bit\n")); @@ -211,7 +213,8 @@ void TranscodingSample::PrintHelp() msdk_printf(MSDK_STRING(" Forces decoder output framerate to be set to provided value (overwriting actual framerate from decoder)\n")); msdk_printf(MSDK_STRING(" -override_encoder_framerate \n")); msdk_printf(MSDK_STRING(" Overwrites framerate of stream going into encoder input with provided value (this option does not enable FRC, it just ovewrites framerate value)\n")); - + msdk_printf(MSDK_STRING(" -override_encoder_picstruct \n")); + msdk_printf(MSDK_STRING(" Overwrites encoder picstruct with specific value")); msdk_printf(MSDK_STRING(" -u Target usage. Valid for H.265, H.264, MPEG2 and MVC encoders. Expected values:\n")); msdk_printf(MSDK_STRING(" veryslow(quality), slower, slow, medium(balanced), fast, faster, veryfast(speed)\n")); msdk_printf(MSDK_STRING(" -q Quality parameter for JPEG encoder; in range [1,100], 100 is the best quality\n")); @@ -223,6 +226,7 @@ void TranscodingSample::PrintHelp() msdk_printf(MSDK_STRING(" May be 1 in the case when -mss option is specified \n")); msdk_printf(MSDK_STRING(" -la_ext Use external LA plugin (compatible with h264 & hevc encoders)\n")); msdk_printf(MSDK_STRING(" -vbr Variable bitrate control\n")); + msdk_printf(MSDK_STRING(" -cbr Constant bitrate control\n")); msdk_printf(MSDK_STRING(" -hrd Maximum possible size of any compressed frames \n")); msdk_printf(MSDK_STRING(" -wb \n")); msdk_printf(MSDK_STRING(" Maximum bitrate for sliding window\n")); @@ -273,7 +277,6 @@ void TranscodingSample::PrintHelp() msdk_printf(MSDK_STRING(" -FRC::PT Enables FRC filter with Preserve Timestamp algorithm\n")); msdk_printf(MSDK_STRING(" -FRC::DT Enables FRC filter with Distributed Timestamp algorithm\n")); msdk_printf(MSDK_STRING(" -FRC::INTERP Enables FRC filter with Frame Interpolation algorithm\n")); - msdk_printf(MSDK_STRING(" NOTE: -FRC filters do not work with -i::sink pipelines !!!\n")); msdk_printf(MSDK_STRING(" -ec::nv12|rgb4|yuy2|nv16|p010|p210 Forces encoder input to use provided chroma mode\n")); msdk_printf(MSDK_STRING(" -dc::nv12|rgb4|yuy2 Forces decoder output to use provided chroma mode\n")); msdk_printf(MSDK_STRING(" NOTE: chroma transform VPP may be automatically enabled if -ec/-dc parameters are provide d\n")); @@ -284,9 +287,17 @@ void TranscodingSample::PrintHelp() msdk_printf(MSDK_STRING(" -field_processing t2t|t2b|b2t|b2b|fr2fr - Field Copy feature\n")); msdk_printf(MSDK_STRING(" -WeightedPred::default|implicit Enambles weighted prediction usage\n")); msdk_printf(MSDK_STRING(" -WeightedBiPred::default|implicit Enambles weighted bi-prediction usage\n")); + msdk_printf(MSDK_STRING(" -ir_type - Intra refresh type. 0 - no refresh, 1 - vertical refresh, 2 - horisontal refresh, 3 - slice refresh\n")); + msdk_printf(MSDK_STRING(" -ir_cycle_size - Number of pictures within refresh cycle starting from 2\n")); + msdk_printf(MSDK_STRING(" -ir_qp_delta - QP difference for inserted intra MBs. This is signed value in [-51, 51] range\n")); + msdk_printf(MSDK_STRING(" -ir_cycle_dist - Distance between the beginnings of the intra-refresh cycles in frames\n")); + msdk_printf(MSDK_STRING(" -LowDelayBRC - strictly obey average frame size set by MaxKbps\n")); + msdk_printf(MSDK_STRING(" -amfs: - adaptive max frame size. If set on, P or B frame size can exceed MaxFrameSize when the scene change is detected.\ + It can benefit the video quality \n")); + msdk_printf(MSDK_STRING(" -mfs - maximum frame size in bytes. Supported only with h264 and hevc codec for VBR mode.\n")); #if (MFX_VERSION >= 1024) - msdk_printf(MSDK_STRING(" -extbrc: Enables external BRC for AVC and HEVC encoders")); + msdk_printf(MSDK_STRING(" -extbrc:: Enables external BRC for AVC and HEVC encoders\n")); #endif #if (MFX_VERSION >= 1026) msdk_printf(MSDK_STRING(" -ExtBrcAdaptiveLTR: Set AdaptiveLTR for implicit extbrc")); @@ -309,6 +320,9 @@ void TranscodingSample::PrintHelp() msdk_printf(MSDK_STRING(" -dec_postproc Resize after decoder using direct pipe (should be used in decoder session)\n")); msdk_printf(MSDK_STRING(" -single_texture_d3d11 single texture mode for d3d11 allocator \n")); #endif //MFX_VERSION >= 1022 + msdk_printf(MSDK_STRING(" -preset Use particular preset for encoding parameters\n")); + msdk_printf(MSDK_STRING(" -pp Print preset parameters\n")); + msdk_printf(MSDK_STRING("\n")); msdk_printf(MSDK_STRING("ParFile format:\n")); msdk_printf(MSDK_STRING(" ParFile is extension of what can be achieved by setting pipeline in the command\n")); @@ -407,6 +421,7 @@ CmdProcessor::CmdProcessor() DumpLogFileName.clear(); shouldUseGreedyFormula=false; bRobustFlag = false; + bSoftRobustFlag = false; } //CmdProcessor::CmdProcessor() @@ -479,6 +494,10 @@ mfxStatus CmdProcessor::ParseCmdLine(int argc, msdk_char *argv[]) { bRobustFlag = true; } + else if (0 == msdk_strcmp(argv[0], MSDK_STRING("-robust:soft"))) + { + bSoftRobustFlag = true; + } else if (0 == msdk_strcmp(argv[0], MSDK_STRING("-?")) ) { PrintHelp(); @@ -1042,7 +1061,9 @@ mfxStatus CmdProcessor::ParseParamsForOneSession(mfxU32 argc, msdk_char *argv[]) if (m_nTimeout) InputParams.nTimeout = m_nTimeout; if (bRobustFlag) - InputParams.bRobustFlag = bRobustFlag; + InputParams.bRobustFlag = true; + if (bSoftRobustFlag) + InputParams.bSoftRobustFlag = true; InputParams.shouldUseGreedyFormula = shouldUseGreedyFormula; @@ -1204,6 +1225,10 @@ mfxStatus CmdProcessor::ParseParamsForOneSession(mfxU32 argc, msdk_char *argv[]) { InputParams.bRobustFlag = true; } + else if (0 == msdk_strcmp(argv[i], MSDK_STRING("-robust:soft"))) + { + InputParams.bSoftRobustFlag = true; + } else if (0 == msdk_strcmp(argv[i], MSDK_STRING("-threads"))) { VAL_CHECK(i+1 == argc, i, argv[i]); @@ -1848,6 +1873,10 @@ mfxStatus CmdProcessor::ParseParamsForOneSession(mfxU32 argc, msdk_char *argv[]) { InputParams.nRateControlMethod = MFX_RATECONTROL_VBR; } + else if (0 == msdk_strcmp(argv[i], MSDK_STRING("-cbr"))) + { + InputParams.nRateControlMethod = MFX_RATECONTROL_CBR; + } else if (0 == msdk_strcmp(argv[i], MSDK_STRING("-bpyr"))) { InputParams.bEnableBPyramid = true; @@ -1892,7 +1921,7 @@ mfxStatus CmdProcessor::ParseParamsForOneSession(mfxU32 argc, msdk_char *argv[]) i++; if (MFX_ERR_NONE != msdk_opt_read(argv[i], InputParams.dDecoderFrameRateOverride)) { - PrintError(MSDK_STRING("-n \"%s\" is invalid"), argv[i]); + PrintError(MSDK_STRING("Framerate \"%s\" is invalid"), argv[i]); return MFX_ERR_UNSUPPORTED; } } @@ -1902,7 +1931,17 @@ mfxStatus CmdProcessor::ParseParamsForOneSession(mfxU32 argc, msdk_char *argv[]) i++; if (MFX_ERR_NONE != msdk_opt_read(argv[i], InputParams.dEncoderFrameRateOverride)) { - PrintError(MSDK_STRING("-n \"%s\" is invalid"), argv[i]); + PrintError(MSDK_STRING("Framerate \"%s\" is invalid"), argv[i]); + return MFX_ERR_UNSUPPORTED; + } + } + else if (0 == msdk_strcmp(argv[i], MSDK_STRING("-override_encoder_picstruct"))) + { + VAL_CHECK(i + 1 == argc, i, argv[i]); + i++; + if (MFX_ERR_NONE != msdk_opt_read(argv[i], InputParams.EncoderPicstructOverride)) + { + PrintError(MSDK_STRING("Picstruct \"%s\" is invalid"), argv[i]); return MFX_ERR_UNSUPPORTED; } } @@ -1990,7 +2029,88 @@ mfxStatus CmdProcessor::ParseParamsForOneSession(mfxU32 argc, msdk_char *argv[]) } #endif - MOD_SMT_PARSE_INPUT + else if (0 == msdk_strcmp(argv[i], MSDK_STRING("-pp"))) + { + InputParams.shouldPrintPresets = true; + } + else if (0 == msdk_strcmp(argv[i], MSDK_STRING("-preset"))) + { + msdk_char presetName[MSDK_MAX_FILENAME_LEN]; + VAL_CHECK(i + 1 >= argc, i, argv[i]); + if (MFX_ERR_NONE != msdk_opt_read(argv[++i], presetName)) + { + PrintError(MSDK_STRING("Preset Name is not defined")); + return MFX_ERR_UNSUPPORTED; + } + + InputParams.PresetMode = CPresetManager::PresetNameToMode(presetName); + if (InputParams.PresetMode == PRESET_MAX_MODES) + { + PrintError(MSDK_STRING("Preset Name is invalid")); + return MFX_ERR_UNSUPPORTED; + } + } + else if (0 == msdk_strcmp(argv[i], MSDK_STRING("-ir_type"))) + { + VAL_CHECK(i + 1 >= argc, i, argv[i]); + + if (MFX_ERR_NONE != msdk_opt_read(argv[++i], InputParams.IntRefType)) + { + PrintError( MSDK_STRING("Intra refresh type is invalid")); + return MFX_ERR_UNSUPPORTED; + } + } + else if (0 == msdk_strcmp(argv[i], MSDK_STRING("-ir_cycle_size"))) + { + VAL_CHECK(i + 1 >= argc, i, argv[i]); + + if (MFX_ERR_NONE != msdk_opt_read(argv[++i], InputParams.IntRefCycleSize)) + { + PrintError( MSDK_STRING("IR refresh cycle size param is invalid")); + return MFX_ERR_UNSUPPORTED; + } + } + else if (0 == msdk_strcmp(argv[i], MSDK_STRING("-ir_qp_delta"))) + { + VAL_CHECK(i + 1 >= argc, i, argv[i]); + + if (MFX_ERR_NONE != msdk_opt_read(argv[++i], InputParams.IntRefQPDelta)) + { + PrintError( MSDK_STRING("IR QP delta param is invalid")); + return MFX_ERR_UNSUPPORTED; + } + } + else if (0 == msdk_strcmp(argv[i], MSDK_STRING("-ir_cycle_dist"))) + { + VAL_CHECK(i + 1 >= argc, i, argv[i]); + + if (MFX_ERR_NONE != msdk_opt_read(argv[++i], InputParams.IntRefCycleDist)) + { + PrintError( MSDK_STRING("IR cycle distance param is invalid")); + return MFX_ERR_UNSUPPORTED; + } + } + else if (0 == msdk_strcmp(argv[i], MSDK_STRING("-LowDelayBRC"))) + { + InputParams.LowDelayBRC = MFX_CODINGOPTION_ON; + } + else if (0 == msdk_strcmp(argv[i], MSDK_STRING("-amfs:on"))) + { + InputParams.nAdaptiveMaxFrameSize = MFX_CODINGOPTION_ON; + } + else if (0 == msdk_strcmp(argv[i], MSDK_STRING("-amfs:off"))) + { + InputParams.nAdaptiveMaxFrameSize = MFX_CODINGOPTION_OFF; + } + else if (0 == msdk_strcmp(argv[i], MSDK_STRING("-mfs"))) + { + VAL_CHECK(i + 1 >= argc, i, argv[i]); + if (MFX_ERR_NONE != msdk_opt_read(argv[++i], InputParams.nMaxFrameSize)) + { + PrintError( MSDK_STRING("MaxFrameSize is invalid")); + return MFX_ERR_UNSUPPORTED; + } + } else if((stsExtBuf = CVPPExtBuffersStorage::ParseCmdLine(argv,argc,i,&InputParams,skipped)) !=MFX_ERR_MORE_DATA) { @@ -2141,21 +2261,13 @@ mfxStatus CmdProcessor::VerifyAndCorrectInputParams(TranscodingSample::sInputPar } // valid target usage range is: [MFX_TARGETUSAGE_BEST_QUALITY .. MFX_TARGETUSAGE_BEST_SPEED] (at the moment [1..7]) - if ((InputParams.nTargetUsage < MFX_TARGETUSAGE_BEST_QUALITY) || + // If target usage is kept unknown - presets manager will fill in proper value + if ((InputParams.nTargetUsage < MFX_TARGETUSAGE_UNKNOWN) || (InputParams.nTargetUsage > MFX_TARGETUSAGE_BEST_SPEED) ) { - if (InputParams.nTargetUsage == MFX_TARGETUSAGE_UNKNOWN) - { - // if user did not specified target usage - use balanced - InputParams.nTargetUsage = MFX_TARGETUSAGE_BALANCED; - } - else - { - PrintError(NULL,"Unsupported target usage"); - // report error if unsupported target usage was set + PrintError(MSDK_STRING("Unsupported target usage")); return MFX_ERR_UNSUPPORTED; } - } // Ignoring user-defined Async Depth for LA if (InputParams.nMaxSliceSize) @@ -2163,6 +2275,12 @@ mfxStatus CmdProcessor::VerifyAndCorrectInputParams(TranscodingSample::sInputPar InputParams.nAsyncDepth = 1; } + // For decoder session of inter-session case, let's set AsyncDepth to 4 by default + if (InputParams.eMode == Sink && !InputParams.nAsyncDepth) + { + InputParams.nAsyncDepth = 4; + } + if (InputParams.bLABRC && !(InputParams.libType & MFX_IMPL_HARDWARE_ANY)) { PrintError(MSDK_STRING("Look ahead BRC is supported only with -hw option!")); @@ -2184,11 +2302,6 @@ mfxStatus CmdProcessor::VerifyAndCorrectInputParams(TranscodingSample::sInputPar } } - if (InputParams.nRateControlMethod == 0) - { - InputParams.nRateControlMethod = MFX_RATECONTROL_CBR; - } - if ((InputParams.nMaxSliceSize) && !(InputParams.libType & MFX_IMPL_HARDWARE_ANY)) { PrintError(MSDK_STRING("MaxSliceSize option is supported only with -hw option!")); @@ -2259,12 +2372,6 @@ mfxStatus CmdProcessor::VerifyAndCorrectInputParams(TranscodingSample::sInputPar msdk_printf(MSDK_STRING("WARNING: -dc option is used in session without decoder, this parameter will be ignored \n")); } - if(InputParams.FRCAlgorithm && InputParams.eMode == Sink) - { - PrintError(NULL,MSDK_STRING("-FRC option should not be used in -o::sink pipelines \n")); - return MFX_ERR_UNSUPPORTED; - } - if(InputParams.EncoderFourCC && InputParams.EncoderFourCC != MFX_FOURCC_NV12 && InputParams.EncoderFourCC != MFX_FOURCC_RGB4 && InputParams.EncoderFourCC != MFX_FOURCC_YUY2 && InputParams.EncodeId == MFX_CODEC_DUMP) diff --git a/samples/sample_plugins/plugins_common_files/mfx_plugin_module.cpp b/samples/sample_plugins/plugins_common_files/mfx_plugin_module.cpp index cd58d56de..31e21413d 100644 --- a/samples/sample_plugins/plugins_common_files/mfx_plugin_module.cpp +++ b/samples/sample_plugins/plugins_common_files/mfx_plugin_module.cpp @@ -25,7 +25,11 @@ extern "C" { #endif +#if defined(_WIN32) || defined(_WIN64) + #define MSDK_PLUGIN_API(ret_type) __declspec(dllexport) ret_type MFX_CDECL +#else #define MSDK_PLUGIN_API(ret_type) ret_type MFX_CDECL +#endif MSDK_PLUGIN_API(MFXDecoderPlugin*) mfxCreateDecoderPlugin() { if (!g_PluginModule.CreateDecoderPlugin) { diff --git a/samples/sample_plugins/rotate_cpu/res/resource.h b/samples/sample_plugins/rotate_cpu/res/resource.h new file mode 100644 index 000000000..f66b2f8d5 --- /dev/null +++ b/samples/sample_plugins/rotate_cpu/res/resource.h @@ -0,0 +1,14 @@ +//{{NO_DEPENDENCIES}} +// Microsoft Visual C++ generated include file. +// Used by sample_rotate_plugin.rc + +// Next default values for new objects +// +#ifdef APSTUDIO_INVOKED +#ifndef APSTUDIO_READONLY_SYMBOLS +#define _APS_NEXT_RESOURCE_VALUE 101 +#define _APS_NEXT_COMMAND_VALUE 40001 +#define _APS_NEXT_CONTROL_VALUE 1001 +#define _APS_NEXT_SYMED_VALUE 101 +#endif +#endif diff --git a/samples/sample_plugins/rotate_cpu/sample_rotate_plugin.vcxproj b/samples/sample_plugins/rotate_cpu/sample_rotate_plugin.vcxproj new file mode 100644 index 000000000..bba5aba95 --- /dev/null +++ b/samples/sample_plugins/rotate_cpu/sample_rotate_plugin.vcxproj @@ -0,0 +1,304 @@ + + + + + Debug_WithDebugAPI + Win32 + + + Debug_WithDebugAPI + x64 + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {C5F515E3-379A-4678-B947-509B3603D826} + sample_rotate_plugin + Win32Proj + + + + DynamicLibrary + false + Unicode + true + v141 + + + DynamicLibrary + false + Unicode + v141 + + + DynamicLibrary + false + Unicode + v141 + + + DynamicLibrary + false + Unicode + true + v141 + + + DynamicLibrary + false + Unicode + v141 + + + DynamicLibrary + false + Unicode + v141 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + <_ProjectFileVersion>10.0.30319.1 + $(ProjectDir)..\..\_build\$(Platform)\$(Configuration)\ + $(ProjectDir)..\..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + $(OutDir)obj\$(ProjectName)\ + true + true + $(ProjectDir)..\..\_build\$(Platform)\$(Configuration)\ + $(ProjectDir)..\..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + $(OutDir)obj\$(ProjectName)\ + true + true + $(ProjectDir)..\..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + false + $(ProjectDir)..\..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + false + sample_rotate_plugin + sample_rotate_plugin + sample_rotate_plugin + sample_rotate_plugin + sample_rotate_plugin + sample_rotate_plugin + + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\..\sample_common\include\;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;_WINDOWS;_ITERATOR_DEBUG_LEVEL=0;%(PreprocessorDefinitions) + true + Async + EnableFastChecks + MultiThreadedDebug + Level4 + true + ProgramDatabase + + + /DYNAMICBASE /NXCOMPAT /SAFESEH %(AdditionalOptions) + $(ProjectDir)..\..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;%(IgnoreSpecificDefaultLibraries) + ..\plugins_common_files\mfx_plugin.def + true + Windows + + + MachineX86 + + + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\..\sample_common\include\;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions) + true + Async + EnableFastChecks + MultiThreadedDebug + Level4 + true + ProgramDatabase + + + /DYNAMICBASE /NXCOMPAT /SAFESEH %(AdditionalOptions) + $(ProjectDir)..\..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;%(IgnoreSpecificDefaultLibraries) + ..\plugins_common_files\mfx_plugin.def + true + Windows + + + MachineX86 + + + + + X64 + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\..\sample_common\include\;%(AdditionalIncludeDirectories) + WIN64;_DEBUG;_WINDOWS;_ITERATOR_DEBUG_LEVEL=0;%(PreprocessorDefinitions) + true + Async + EnableFastChecks + MultiThreadedDebug + Level4 + true + ProgramDatabase + + + /DYNAMICBASE /NXCOMPAT %(AdditionalOptions) + $(ProjectDir)..\..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;%(IgnoreSpecificDefaultLibraries) + ..\plugins_common_files\mfx_plugin.def + true + Windows + + + MachineX64 + + + + + X64 + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\..\sample_common\include\;%(AdditionalIncludeDirectories) + WIN64;_DEBUG;_WINDOWS;%(PreprocessorDefinitions) + true + Async + EnableFastChecks + MultiThreadedDebug + Level4 + true + ProgramDatabase + + + /DYNAMICBASE /NXCOMPAT %(AdditionalOptions) + $(ProjectDir)..\..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;%(IgnoreSpecificDefaultLibraries) + ..\plugins_common_files\mfx_plugin.def + true + Windows + + + MachineX64 + + + + + $(CPPFLAGS) %(AdditionalOptions) + $(ProjectDir)\include;$(ProjectDir)\..\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\..\sample_common\include\;%(AdditionalIncludeDirectories) + WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions) + Async + MultiThreaded + Level4 + true + ProgramDatabase + + + /DYNAMICBASE /NXCOMPAT /SAFESEH %(AdditionalOptions) + $(ProjectDir)..\..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;%(IgnoreSpecificDefaultLibraries) + ..\plugins_common_files\mfx_plugin.def + false + Windows + true + true + + + MachineX86 + + + + + X64 + + + $(ProjectDir)\include;$(ProjectDir)\..\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\..\sample_common\include\;%(AdditionalIncludeDirectories) + WIN64;NDEBUG;_WINDOWS;%(PreprocessorDefinitions) + Async + MultiThreaded + Level4 + true + ProgramDatabase + + + /DYNAMICBASE /NXCOMPAT %(AdditionalOptions) + $(ProjectDir)..\..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;%(IgnoreSpecificDefaultLibraries) + $(ProjectDir)..\plugins_common_files\mfx_plugin.def + false + Windows + true + true + + + MachineX64 + + + + + + + + + + + + + + + {5FADB243-53C3-4776-A20F-8BD65C10CF41} + + + + + + \ No newline at end of file diff --git a/samples/sample_plugins/rotate_opencl/include/opencl_filter_dx11.h b/samples/sample_plugins/rotate_opencl/include/opencl_filter_dx11.h index ad72cf99e..1f495a3bd 100644 --- a/samples/sample_plugins/rotate_opencl/include/opencl_filter_dx11.h +++ b/samples/sample_plugins/rotate_opencl/include/opencl_filter_dx11.h @@ -19,4 +19,32 @@ or https://software.intel.com/en-us/media-client-solutions-support. #pragma once +#if defined(_WIN32) || defined(_WIN64) + +#include "opencl_filter.h" +#include "d3d11_allocator.h" +#include +#include + +class OpenCLFilterDX11 : public OpenCLFilterBase +{ +public: + OpenCLFilterDX11(); + virtual ~OpenCLFilterDX11(); + virtual cl_int OCLInit(mfxHDL device); + +private: + virtual cl_int InitSurfaceSharingExtension(); + virtual cl_int InitDevice(); + + virtual cl_mem CreateSharedSurface(mfxMemId mid, int nView, bool bIsReadOnly); + virtual bool EnqueueAcquireSurfaces(cl_mem* surfaces, int nSurfaces); + virtual bool EnqueueReleaseSurfaces(cl_mem* surfaces, int nSurfaces); + + ID3D11Device* m_pDevice; + ID3D11Texture2D* m_pSharedSurfaces[c_shared_surfaces_num]; + +}; + +#endif // #if defined(_WIN32) || defined(_WIN64) diff --git a/samples/sample_plugins/rotate_opencl/include/opencl_filter_dx9.h b/samples/sample_plugins/rotate_opencl/include/opencl_filter_dx9.h index 81be68ccb..ba341d290 100644 --- a/samples/sample_plugins/rotate_opencl/include/opencl_filter_dx9.h +++ b/samples/sample_plugins/rotate_opencl/include/opencl_filter_dx9.h @@ -19,3 +19,31 @@ or https://software.intel.com/en-us/media-client-solutions-support. #pragma once +#if defined(_WIN32) || defined(_WIN64) + +#include "opencl_filter.h" +#include "d3d_utils.h" +#include "d3d_allocator.h" + +class OpenCLFilterDX9 : public OpenCLFilterBase +{ +public: + OpenCLFilterDX9(); + virtual ~OpenCLFilterDX9(); + virtual cl_int OCLInit(mfxHDL device); + +protected: // functions + virtual cl_int InitDevice(); + virtual cl_int InitSurfaceSharingExtension(); + + virtual cl_mem CreateSharedSurface(mfxMemId mid, int nView, bool bIsReadOnly); + virtual bool EnqueueAcquireSurfaces(cl_mem* surfaces, int nSurfaces); + virtual bool EnqueueReleaseSurfaces(cl_mem* surfaces, int nSurfaces); +protected: // variables + std::auto_ptr m_SafeD3DDeviceManager; + + HANDLE m_hSharedSurfaces[c_shared_surfaces_num]; // d3d9 surface shared handles + IDirect3DSurface9* m_pSharedSurfaces[c_shared_surfaces_num]; +}; + +#endif // #if defined(_WIN32) || defined(_WIN64) diff --git a/samples/sample_plugins/rotate_opencl/include/opencl_filter_va.h b/samples/sample_plugins/rotate_opencl/include/opencl_filter_va.h index 722b7c3c4..278e71790 100644 --- a/samples/sample_plugins/rotate_opencl/include/opencl_filter_va.h +++ b/samples/sample_plugins/rotate_opencl/include/opencl_filter_va.h @@ -19,6 +19,7 @@ or https://software.intel.com/en-us/media-client-solutions-support. #pragma once +#if !defined(_WIN32) && !defined(_WIN64) #include "logger.h" #include @@ -54,3 +55,4 @@ class OpenCLFilterVA : public OpenCLFilterBase VASurfaceID m_SharedSurfaces[c_shared_surfaces_num]; }; +#endif // #if !defined(_WIN32) && !defined(_WIN64) diff --git a/samples/sample_plugins/rotate_opencl/include/sample_opencl_plugin.h b/samples/sample_plugins/rotate_opencl/include/sample_opencl_plugin.h index c832ca699..f7277c0c5 100644 --- a/samples/sample_plugins/rotate_opencl/include/sample_opencl_plugin.h +++ b/samples/sample_plugins/rotate_opencl/include/sample_opencl_plugin.h @@ -27,7 +27,12 @@ or https://software.intel.com/en-us/media-client-solutions-support. #include "rotate_plugin_api.h" #include "sample_defs.h" +#if defined(_WIN32) || defined(_WIN64) +#include "opencl_filter_dx9.h" +#include "opencl_filter_dx11.h" +#else #include "opencl_filter_va.h" +#endif // msvc ignores throw function specifications used in cl.hpp #pragma warning(disable : 4290) @@ -38,8 +43,10 @@ or https://software.intel.com/en-us/media-client-solutions-support. //#define _interlockedbittestandset64 fake_set64 //#define _interlockedbittestandreset64 fake_reset64 +#if !defined(_WIN32) && !defined(_WIN64) #include #define CL_USE_DEPRECATED_OPENCL_1_1_APIS 1 +#endif #undef CL_VERSION_1_2 #include "CL/cl.hpp" @@ -198,7 +205,11 @@ class Rotate : public MFXGenericPlugin bool m_bIsInOpaque; bool m_bIsOutOpaque; +#if defined(_WIN32) || defined(_WIN64) + std::unique_ptr m_OpenCLFilter; +#else std::unique_ptr m_OpenCLFilter; +#endif std::unique_ptr m_pOpenCLRotator180Context; mfxHDL m_device; diff --git a/samples/sample_plugins/rotate_opencl/res/resource.h b/samples/sample_plugins/rotate_opencl/res/resource.h new file mode 100644 index 000000000..f5d97f7ab --- /dev/null +++ b/samples/sample_plugins/rotate_opencl/res/resource.h @@ -0,0 +1,14 @@ +//{{NO_DEPENDENCIES}} +// Microsoft Visual C++ generated include file. +// Used by sample_plugin_opencl.rc + +// Next default values for new objects +// +#ifdef APSTUDIO_INVOKED +#ifndef APSTUDIO_READONLY_SYMBOLS +#define _APS_NEXT_RESOURCE_VALUE 101 +#define _APS_NEXT_COMMAND_VALUE 40001 +#define _APS_NEXT_CONTROL_VALUE 1001 +#define _APS_NEXT_SYMED_VALUE 101 +#endif +#endif diff --git a/samples/sample_plugins/rotate_opencl/sample_plugin_opencl.vcxproj b/samples/sample_plugins/rotate_opencl/sample_plugin_opencl.vcxproj new file mode 100644 index 000000000..187cf6401 --- /dev/null +++ b/samples/sample_plugins/rotate_opencl/sample_plugin_opencl.vcxproj @@ -0,0 +1,325 @@ + + + + + Debug_WithDebugAPI + Win32 + + + Debug_WithDebugAPI + x64 + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {E90AE63E-F219-4389-A955-26E78BD1BD9A} + sample_plugin_opencl + Win32Proj + + + + DynamicLibrary + false + Unicode + v141 + + + DynamicLibrary + false + Unicode + v141 + + + DynamicLibrary + false + Unicode + v141 + + + DynamicLibrary + false + Unicode + false + v141 + + + DynamicLibrary + false + Unicode + v141 + + + DynamicLibrary + false + Unicode + v141 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + <_ProjectFileVersion>10.0.30319.1 + $(ProjectDir)..\..\_build\$(Platform)\$(Configuration)\ + $(ProjectDir)..\..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + $(OutDir)obj\$(ProjectName)\ + true + true + $(ProjectDir)..\..\_build\$(Platform)\$(Configuration)\ + $(ProjectDir)..\..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + $(OutDir)obj\$(ProjectName)\ + true + true + $(ProjectDir)..\..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + false + $(ProjectDir)..\..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + false + sample_plugin_opencl + sample_plugin_opencl + sample_plugin_opencl + sample_plugin_opencl + sample_plugin_opencl + sample_plugin_opencl + + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\..\sample_common\include;$(INTELOCLSDKROOT)\include;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;_ITERATOR_DEBUG_LEVEL=0;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebug + + + Level3 + ProgramDatabase + + + OpenCL.lib;%(AdditionalDependencies) + $(INTELOCLSDKROOT)\lib\x86;$(ProjectDir)..\..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;%(IgnoreSpecificDefaultLibraries) + ..\plugins_common_files\mfx_plugin.def + true + Windows + MachineX86 + /DYNAMICBASE /NXCOMPAT /SAFESEH %(AdditionalOptions) + + + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\..\sample_common\include;$(INTELOCLSDKROOT)\include;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebug + + + Level3 + ProgramDatabase + + + OpenCL.lib;libmfx_vs2015_d.lib;%(AdditionalDependencies) + $(INTELOCLSDKROOT)\lib\x86;$(ProjectDir)..\..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;%(IgnoreSpecificDefaultLibraries) + ..\plugins_common_files\mfx_plugin.def + true + Windows + MachineX86 + /DYNAMICBASE /NXCOMPAT /SAFESEH %(AdditionalOptions) + + + + + X64 + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\..\sample_common\include;$(INTELOCLSDKROOT)\include;%(AdditionalIncludeDirectories) + WIN64;_WINDOWS;_DEBUG;_ITERATOR_DEBUG_LEVEL=0;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebug + + + Level3 + ProgramDatabase + + + OpenCL.lib;libmfx_vs2015.lib;%(AdditionalDependencies) + $(INTELOCLSDKROOT)\lib\x64;$(ProjectDir)..\..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;%(IgnoreSpecificDefaultLibraries) + ..\plugins_common_files\mfx_plugin.def + true + Windows + MachineX64 + + + + + X64 + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\..\sample_common\include;$(INTELOCLSDKROOT)\include;%(AdditionalIncludeDirectories) + WIN64;_WINDOWS;_DEBUG;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebug + + + Level3 + ProgramDatabase + + + OpenCL.lib;libmfx_vs2015_d.lib;%(AdditionalDependencies) + $(INTELOCLSDKROOT)\lib\x64;$(ProjectDir)..\..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;%(IgnoreSpecificDefaultLibraries) + ..\plugins_common_files\mfx_plugin.def + true + Windows + MachineX64 + + + + + $(ProjectDir)\include;$(ProjectDir)\..\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\..\sample_common\include;$(INTELOCLSDKROOT)\include;%(AdditionalIncludeDirectories) + WIN32;NDEBUG;%(PreprocessorDefinitions) + MultiThreaded + + + Level3 + ProgramDatabase + + + OpenCL.lib;libmfx_vs2015.lib;%(AdditionalDependencies) + $(INTELOCLSDKROOT)\lib\x86;$(ProjectDir)..\..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;%(IgnoreSpecificDefaultLibraries) + ..\plugins_common_files\mfx_plugin.def + false + Windows + true + true + MachineX86 + /DYNAMICBASE /NXCOMPAT /SAFESEH %(AdditionalOptions) + + + + + X64 + + + MaxSpeed + false + $(ProjectDir)\include;$(ProjectDir)\..\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\..\sample_common\include;$(INTELOCLSDKROOT)\include;%(AdditionalIncludeDirectories) + WIN64;_WINDOWS;_NDEBUG;%(PreprocessorDefinitions) + MultiThreaded + + + Level3 + ProgramDatabase + + + OpenCL.lib;libmfx_vs2015.lib;%(AdditionalDependencies) + $(INTELOCLSDKROOT)\lib\x64;$(ProjectDir)..\..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;%(IgnoreSpecificDefaultLibraries) + $(ProjectDir)..\plugins_common_files\mfx_plugin.def + false + Windows + true + true + MachineX64 + + + + + copy "%(FullPath)" "$(OutDir)" + + copy "%(FullPath)" "$(OutDir)" + + $(OutDir)%(Filename)%(Extension);%(Outputs) + $(OutDir)%(Filename)%(Extension);%(Outputs) + copy "%(FullPath)" "$(OutDir)" + + copy "%(FullPath)" "$(OutDir)" + + $(OutDir)%(Filename)%(Extension);%(Outputs) + $(OutDir)%(Filename)%(Extension);%(Outputs) + copy "%(FullPath)" "$(OutDir)" + + $(OutDir)%(Filename)%(Extension);%(Outputs) + copy "%(FullPath)" "$(OutDir)" + + $(OutDir)%(Filename)%(Extension);%(Outputs) + + + + + + + + + + + + + + + + + + + + + + + {5FADB243-53C3-4776-A20F-8BD65C10CF41} + + + + + + \ No newline at end of file diff --git a/samples/sample_plugins/rotate_opencl/src/opencl_filter.cpp b/samples/sample_plugins/rotate_opencl/src/opencl_filter.cpp index cdf27cb85..97e1604bc 100644 --- a/samples/sample_plugins/rotate_opencl/src/opencl_filter.cpp +++ b/samples/sample_plugins/rotate_opencl/src/opencl_filter.cpp @@ -353,12 +353,16 @@ std::string getPathToExe() const size_t module_length = 1024; char module_name[module_length]; +#if defined(_WIN32) || defined(_WIN64) + GetModuleFileNameA(0, module_name, module_length); +#else char id[module_length]; sprintf(id, "/proc/%d/exe", getpid()); ssize_t count = readlink(id, module_name, module_length-1); if (count == -1) return std::string(""); module_name[count] = '\0'; +#endif std::string exePath(module_name); return exePath.substr(0, exePath.find_last_of("\\/") + 1); diff --git a/samples/sample_plugins/rotate_opencl/src/opencl_filter_dx11.cpp b/samples/sample_plugins/rotate_opencl/src/opencl_filter_dx11.cpp index 0e9502671..e2496dca2 100644 --- a/samples/sample_plugins/rotate_opencl/src/opencl_filter_dx11.cpp +++ b/samples/sample_plugins/rotate_opencl/src/opencl_filter_dx11.cpp @@ -19,4 +19,150 @@ or https://software.intel.com/en-us/media-client-solutions-support. #include "mfx_samples_config.h" +#if defined(_WIN32) || defined(_WIN64) + +#include + +#include "opencl_filter_dx11.h" +#include "sample_defs.h" + +#include "stdexcept" + +// INTEL DX9 sharing functions declared deprecated in OpenCL 1.2 +#pragma warning( disable : 4996 ) + +// define for +#define DX9_MEDIA_SHARING +#define DX_MEDIA_SHARING +#define CL_DX9_MEDIA_SHARING_INTEL_EXT + +#include +#include + +using std::endl; + +#define INIT_CL_EXT_FUNC(x) x = (x ## _fn)clGetExtensionFunctionAddress(#x); +#define SAFE_OCL_FREE(P, FREE_FUNC) { if (P) { FREE_FUNC(P); P = NULL; } } + +#define MAX_PLATFORMS 32 +#define MAX_STRING_SIZE 1024 + +#define EXT_DECLARE(_name) _name##_fn _name +#define EXT_INIT(_p, _name) _name = (_name##_fn) clGetExtensionFunctionAddressForPlatform((_p), #_name); res &= (_name != NULL); + + +EXT_DECLARE(clGetDeviceIDsFromD3D11KHR); +EXT_DECLARE(clCreateFromD3D11Texture2DKHR); +EXT_DECLARE(clEnqueueAcquireD3D11ObjectsKHR); +EXT_DECLARE(clEnqueueReleaseD3D11ObjectsKHR); +inline int InitD11SharingFunctions(cl_platform_id platform) // get DX11 sharing functions +{ + bool res = true; + EXT_INIT(platform,clGetDeviceIDsFromD3D11KHR); + EXT_INIT(platform,clCreateFromD3D11Texture2DKHR); + EXT_INIT(platform,clEnqueueAcquireD3D11ObjectsKHR); + EXT_INIT(platform,clEnqueueReleaseD3D11ObjectsKHR); + return res; +} + +OpenCLFilterDX11::OpenCLFilterDX11() : m_pDevice(0) +{ + for (int i = 0; i(device); + return OpenCLFilterBase::OCLInit(device); +} + + +cl_int OpenCLFilterDX11::InitSurfaceSharingExtension() { + if (InitD11SharingFunctions(m_clplatform)) + return CL_SUCCESS; + return CL_INVALID_VALUE; +} + +cl_int OpenCLFilterDX11::InitDevice() { + cl_int error = CL_SUCCESS; + + cl_uint numDevices = 0; + error = clGetDeviceIDsFromD3D11KHR(m_clplatform, + CL_D3D11_DEVICE_KHR, + m_pDevice, + CL_PREFERRED_DEVICES_FOR_D3D11_KHR, + 1, + &m_cldevice, + &numDevices); + if(error) { + log.error() << "OpenCLFilter: clGetDeviceIDsFromD3D11KHR failed. Error code: " << error << endl; + return error; + } + + const cl_context_properties props[] = {CL_CONTEXT_D3D11_DEVICE_KHR, (cl_context_properties)m_pDevice, + CL_CONTEXT_INTEROP_USER_SYNC, CL_FALSE, NULL }; + m_clcontext = clCreateContext(props, 1, &m_cldevice, NULL, NULL, &error); + if(error) { + log.error() << "OpenCLFilter: clCreateContext failed. Error code: " << error << endl; + return error; + } + + log.debug() << "OpenCLFilter: OCL device inited" << endl; + + return error; +} + +cl_mem OpenCLFilterDX11::CreateSharedSurface(mfxMemId mid, int nView, bool bIsReadOnly) +{ + mfxHDLPair mid_pair = { 0 }; + mfxStatus sts = m_pAlloc->GetHDL(m_pAlloc->pthis, mid, reinterpret_cast(&mid_pair)); + if (sts) return 0; + + ID3D11Texture2D *surf = (ID3D11Texture2D*)mid_pair.first; + + cl_int error = CL_SUCCESS; + cl_mem mem = clCreateFromD3D11Texture2DKHR(m_clcontext, bIsReadOnly ? CL_MEM_READ_ONLY : CL_MEM_READ_WRITE, + surf, nView, &error); + if (error) { + log.error() << "clCreateFromD3D11Texture2DKHR failed. Error code: " << error << endl; + return 0; + } + return mem; +} + +bool OpenCLFilterDX11::EnqueueAcquireSurfaces(cl_mem* surfaces, int nSurfaces) +{ + cl_int error = clEnqueueAcquireD3D11ObjectsKHR(m_clqueue, nSurfaces, surfaces, 0, NULL, NULL); + if (error) { + log.error() << "clEnqueueAcquireD3D11ObjectsKHR failed. Error code: " << error << endl; + return false; + } + return true; +} + +bool OpenCLFilterDX11::EnqueueReleaseSurfaces(cl_mem* surfaces, int nSurfaces) +{ + cl_int error = clEnqueueReleaseD3D11ObjectsKHR(m_clqueue, nSurfaces, surfaces, 0, NULL, NULL); + if (error) { + log.error() << "clEnqueueReleaseD3D11ObjectsKHR failed. Error code: " << error << endl; + return false; + } + return true; +} + +#endif // #if defined(_WIN32) || defined(_WIN64) diff --git a/samples/sample_plugins/rotate_opencl/src/opencl_filter_dx9.cpp b/samples/sample_plugins/rotate_opencl/src/opencl_filter_dx9.cpp index 9f1a77bc3..5bffc236b 100644 --- a/samples/sample_plugins/rotate_opencl/src/opencl_filter_dx9.cpp +++ b/samples/sample_plugins/rotate_opencl/src/opencl_filter_dx9.cpp @@ -19,3 +19,172 @@ or https://software.intel.com/en-us/media-client-solutions-support. #include "mfx_samples_config.h" +#if defined(_WIN32) || defined(_WIN64) + +#include "opencl_filter_dx9.h" +#include "sample_defs.h" + +// INTEL DX9 sharing functions declared deprecated in OpenCL 1.2 +#pragma warning( disable : 4996 ) + +#define DX9_MEDIA_SHARING +#define DX_MEDIA_SHARING +#define CL_DX9_MEDIA_SHARING_INTEL_EXT + +#include +#include + +using std::endl; + + +clGetDeviceIDsFromDX9INTEL_fn clGetDeviceIDsFromDX9INTEL = NULL; +clCreateFromDX9MediaSurfaceINTEL_fn clCreateFromDX9MediaSurfaceINTEL = NULL; +clEnqueueAcquireDX9ObjectsINTEL_fn clEnqueueAcquireDX9ObjectsINTEL = NULL; +clEnqueueReleaseDX9ObjectsINTEL_fn clEnqueueReleaseDX9ObjectsINTEL = NULL; + +OpenCLFilterDX9::OpenCLFilterDX9() +{ + for(size_t i=0;i(device))); + } + catch (const std::exception &ex) + { + log.error() << ex.what() << endl; + return CL_DEVICE_NOT_AVAILABLE; + } + + return OpenCLFilterBase::OCLInit(device); +} + +cl_int OpenCLFilterDX9::InitSurfaceSharingExtension() +{ + cl_int error = CL_SUCCESS; + + // Check if surface sharing is available + size_t len = 0; + const size_t max_string_size = 1024; + char extensions[max_string_size]; + error = clGetPlatformInfo(m_clplatform, CL_PLATFORM_EXTENSIONS, max_string_size, extensions, &len); + log.info() << "OpenCLFilter: Platform extensions: " << extensions << endl; + if(NULL == strstr(extensions, "cl_intel_dx9_media_sharing")) + { + log.error() << "OpenCLFilter: DX9 media sharing not available!" << endl; + return CL_INVALID_PLATFORM; + } + + // Hook up the d3d sharing extension functions that we need + INIT_CL_EXT_FUNC(clGetDeviceIDsFromDX9INTEL); + INIT_CL_EXT_FUNC(clCreateFromDX9MediaSurfaceINTEL); + INIT_CL_EXT_FUNC(clEnqueueAcquireDX9ObjectsINTEL); + INIT_CL_EXT_FUNC(clEnqueueReleaseDX9ObjectsINTEL); + + // Check for success + if (!clGetDeviceIDsFromDX9INTEL || + !clCreateFromDX9MediaSurfaceINTEL || + !clEnqueueAcquireDX9ObjectsINTEL || + !clEnqueueReleaseDX9ObjectsINTEL) + { + log.error() << "OpenCLFilter: Couldn't get all of the media sharing routines" << endl; + return CL_INVALID_PLATFORM; + } + + return error; +} + +cl_int OpenCLFilterDX9::InitDevice() +{ + log.debug() << "OpenCLFilter: Try to init OCL device" << endl; + + cl_int error = CL_SUCCESS; + + try + { + LockedD3DDevice pD3DDevice = m_SafeD3DDeviceManager->LockDevice(); + error = clGetDeviceIDsFromDX9INTEL(m_clplatform, CL_D3D9EX_DEVICE_INTEL, // note - you must use D3D9DeviceEx + pD3DDevice.get(), CL_PREFERRED_DEVICES_FOR_DX9_INTEL, 1, &m_cldevice, NULL); + if(error) { + log.error() << "OpenCLFilter: clGetDeviceIDsFromDX9INTEL failed. Error code: " << error << endl; + return error; + } + + // Initialize the shared context + cl_context_properties props[] = + { + CL_CONTEXT_D3D9EX_DEVICE_INTEL, (cl_context_properties)pD3DDevice.get(), + CL_CONTEXT_PLATFORM, (cl_context_properties)m_clplatform, + CL_CONTEXT_INTEROP_USER_SYNC, (cl_context_properties)CL_TRUE, + 0 + }; + m_clcontext = clCreateContext(props, 1, &m_cldevice, NULL, NULL, &error); + if(error) { + log.error() << "OpenCLFilter: clCreateContext failed. Error code: " << error << endl; + return error; + } + } + catch (const std::exception &ex) + { + log.error() << ex.what() << endl; + return CL_DEVICE_NOT_AVAILABLE; + } + + log.debug() << "OpenCLFilter: OCL device inited" << endl; + + return error; +} + +cl_mem OpenCLFilterDX9::CreateSharedSurface(mfxMemId mid, int nView, bool bIsReadOnly) +{ + mfxHDLPair* mid_pair = static_cast(mid); + + IDirect3DSurface9 *surf = (IDirect3DSurface9*)mid_pair->first; + HANDLE handle = mid_pair->second; + + cl_int error = CL_SUCCESS; + cl_mem mem = clCreateFromDX9MediaSurfaceINTEL(m_clcontext, bIsReadOnly ? CL_MEM_READ_ONLY : CL_MEM_READ_WRITE, + surf, handle, nView, &error); + if (error) { + log.error() << "clCreateFromDX9MediaSurfaceINTEL failed. Error code: " << error << endl; + return 0; + } + return mem; +} + +bool OpenCLFilterDX9::EnqueueAcquireSurfaces(cl_mem* surfaces, int nSurfaces) +{ + cl_int error = clEnqueueAcquireDX9ObjectsINTEL(m_clqueue, nSurfaces, surfaces, 0, NULL, NULL); + if (error) { + log.error() << "clEnqueueAcquireDX9ObjectsINTEL failed. Error code: " << error << endl; + return false; + } + return true; +} + +bool OpenCLFilterDX9::EnqueueReleaseSurfaces(cl_mem* surfaces, int nSurfaces) +{ + cl_int error = clEnqueueReleaseDX9ObjectsINTEL(m_clqueue, nSurfaces, surfaces, 0, NULL, NULL); + if (error) { + log.error() << "clEnqueueReleaseDX9ObjectsINTEL failed. Error code: " << error << endl; + return false; + } + return true; +} + +#endif // #if defined(_WIN32) || defined(_WIN64) diff --git a/samples/sample_plugins/rotate_opencl/src/opencl_filter_va.cpp b/samples/sample_plugins/rotate_opencl/src/opencl_filter_va.cpp index 780c8a0a8..d69cc7b0c 100644 --- a/samples/sample_plugins/rotate_opencl/src/opencl_filter_va.cpp +++ b/samples/sample_plugins/rotate_opencl/src/opencl_filter_va.cpp @@ -17,6 +17,7 @@ The original version of this sample may be obtained from https://software.intel. or https://software.intel.com/en-us/media-client-solutions-support. \**********************************************************************************/ +#if !defined(_WIN32) && !defined(_WIN64) #include "opencl_filter_va.h" #include "sample_defs.h" @@ -140,3 +141,4 @@ bool OpenCLFilterVA::EnqueueReleaseSurfaces(cl_mem* surfaces, int nSurfaces) return true; } +#endif // #if !defined(_WIN32) && !defined(_WIN64) diff --git a/samples/sample_plugins/rotate_opencl/src/sample_opencl_plugin.cpp b/samples/sample_plugins/rotate_opencl/src/sample_opencl_plugin.cpp index a4d5d50bd..bcfbb92ac 100644 --- a/samples/sample_plugins/rotate_opencl/src/sample_opencl_plugin.cpp +++ b/samples/sample_plugins/rotate_opencl/src/sample_opencl_plugin.cpp @@ -78,7 +78,18 @@ mfxStatus Rotate::PluginInit(mfxCoreInterface *core) m_impl = par.Impl; mfxHDL hdl = 0; +#if defined(_WIN32) || defined(_WIN64) + if (MFX_IMPL_VIA_MASK(m_impl) == MFX_IMPL_VIA_D3D9) { + sts = m_pmfxCore->GetHandle(m_pmfxCore->pthis, MFX_HANDLE_D3D9_DEVICE_MANAGER, &m_device); + } + else if (MFX_IMPL_VIA_MASK(m_impl) == MFX_IMPL_VIA_D3D11) { + sts = m_pmfxCore->GetHandle(m_pmfxCore->pthis, MFX_HANDLE_D3D11_DEVICE, &m_device); + } else { + hdl = 0; + } +#else sts = m_pmfxCore->GetHandle(m_pmfxCore->pthis, MFX_HANDLE_VA_DISPLAY, &m_device); +#endif // SW lib is used if GetHandle return MFX_ERR_NOT_FOUND MSDK_IGNORE_MFX_STS(sts, MFX_ERR_NOT_FOUND); @@ -338,10 +349,23 @@ mfxStatus Rotate::Init(mfxVideoParam *mfxParam) // init OpenCLFilter cl_int error = CL_SUCCESS; +#if defined(_WIN32) || defined(_WIN64) + if (MFX_IMPL_VIA_MASK(m_impl) == MFX_IMPL_VIA_D3D11) { + m_OpenCLFilter.reset(new OpenCLFilterDX11()); + } else { + m_OpenCLFilter.reset(new OpenCLFilterDX9()); + } + error = m_OpenCLFilter.get()->AddKernel(readFile("ocl_rotate.cl").c_str(), "rotate_Y", "rotate_UV"); + if (error) return MFX_ERR_DEVICE_FAILED; + + error = m_OpenCLFilter.get()->OCLInit(m_device); + +#else m_OpenCLFilter.reset(new OpenCLFilterVA()); error = m_OpenCLFilter.get()->AddKernel(readFile("ocl_rotate.cl").c_str(), "rotate_Y", "rotate_UV"); if (error) return MFX_ERR_DEVICE_FAILED; error = m_OpenCLFilter.get()->OCLInit(m_device); +#endif if (error) { error = CL_SUCCESS; @@ -422,10 +446,17 @@ mfxStatus Rotate::SetAllocator(mfxFrameAllocator * pAlloc) mfxStatus Rotate::SetHandle(mfxHandleType type, mfxHDL hdl) { +#if defined(_WIN32) || defined(_WIN64) + if (MFX_HANDLE_D3D9_DEVICE_MANAGER == type) + { + m_device = hdl; + } +#else if (MFX_HANDLE_VA_DISPLAY == type) { m_device = hdl; } +#endif return MFX_ERR_NONE; } diff --git a/samples/sample_plugins/sample_plugins.sln b/samples/sample_plugins/sample_plugins.sln new file mode 100644 index 000000000..deea6b9b0 --- /dev/null +++ b/samples/sample_plugins/sample_plugins.sln @@ -0,0 +1,58 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27703.2000 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "vpp_plugin", "vpp_plugin\vpp_plugin.vcxproj", "{474EF95D-3579-411B-9DDB-88B49F923D65}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sample_common", "..\sample_common\sample_common.vcxproj", "{5FADB243-53C3-4776-A20F-8BD65C10CF41}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sample_rotate_plugin", "rotate_cpu\sample_rotate_plugin.vcxproj", "{C5F515E3-379A-4678-B947-509B3603D826}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sample_plugin_opencl", "rotate_opencl\sample_plugin_opencl.vcxproj", "{E90AE63E-F219-4389-A955-26E78BD1BD9A}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Debug|x64 = Debug|x64 + Release|Win32 = Release|Win32 + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {474EF95D-3579-411B-9DDB-88B49F923D65}.Debug|Win32.ActiveCfg = Debug|Win32 + {474EF95D-3579-411B-9DDB-88B49F923D65}.Debug|Win32.Build.0 = Debug|Win32 + {474EF95D-3579-411B-9DDB-88B49F923D65}.Debug|x64.ActiveCfg = Debug|x64 + {474EF95D-3579-411B-9DDB-88B49F923D65}.Debug|x64.Build.0 = Debug|x64 + {474EF95D-3579-411B-9DDB-88B49F923D65}.Release|Win32.ActiveCfg = Release|Win32 + {474EF95D-3579-411B-9DDB-88B49F923D65}.Release|Win32.Build.0 = Release|Win32 + {474EF95D-3579-411B-9DDB-88B49F923D65}.Release|x64.ActiveCfg = Release|x64 + {474EF95D-3579-411B-9DDB-88B49F923D65}.Release|x64.Build.0 = Release|x64 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Debug|Win32.ActiveCfg = Debug|Win32 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Debug|Win32.Build.0 = Debug|Win32 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Debug|x64.ActiveCfg = Debug|x64 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Debug|x64.Build.0 = Debug|x64 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Release|Win32.ActiveCfg = Release|Win32 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Release|Win32.Build.0 = Release|Win32 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Release|x64.ActiveCfg = Release|x64 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Release|x64.Build.0 = Release|x64 + {C5F515E3-379A-4678-B947-509B3603D826}.Debug|Win32.ActiveCfg = Debug|Win32 + {C5F515E3-379A-4678-B947-509B3603D826}.Debug|Win32.Build.0 = Debug|Win32 + {C5F515E3-379A-4678-B947-509B3603D826}.Debug|x64.ActiveCfg = Debug|x64 + {C5F515E3-379A-4678-B947-509B3603D826}.Debug|x64.Build.0 = Debug|x64 + {C5F515E3-379A-4678-B947-509B3603D826}.Release|Win32.ActiveCfg = Release|Win32 + {C5F515E3-379A-4678-B947-509B3603D826}.Release|Win32.Build.0 = Release|Win32 + {C5F515E3-379A-4678-B947-509B3603D826}.Release|x64.ActiveCfg = Release|x64 + {C5F515E3-379A-4678-B947-509B3603D826}.Release|x64.Build.0 = Release|x64 + {E90AE63E-F219-4389-A955-26E78BD1BD9A}.Debug|Win32.ActiveCfg = Debug|Win32 + {E90AE63E-F219-4389-A955-26E78BD1BD9A}.Debug|Win32.Build.0 = Debug|Win32 + {E90AE63E-F219-4389-A955-26E78BD1BD9A}.Debug|x64.ActiveCfg = Debug|x64 + {E90AE63E-F219-4389-A955-26E78BD1BD9A}.Debug|x64.Build.0 = Debug|x64 + {E90AE63E-F219-4389-A955-26E78BD1BD9A}.Release|Win32.ActiveCfg = Release|Win32 + {E90AE63E-F219-4389-A955-26E78BD1BD9A}.Release|Win32.Build.0 = Release|Win32 + {E90AE63E-F219-4389-A955-26E78BD1BD9A}.Release|x64.ActiveCfg = Release|x64 + {E90AE63E-F219-4389-A955-26E78BD1BD9A}.Release|x64.Build.0 = Release|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/samples/sample_plugins/vpp_plugin/vpp_plugin.vcxproj b/samples/sample_plugins/vpp_plugin/vpp_plugin.vcxproj new file mode 100644 index 000000000..bf7da3d8f --- /dev/null +++ b/samples/sample_plugins/vpp_plugin/vpp_plugin.vcxproj @@ -0,0 +1,248 @@ + + + + + Debug_WithDebugAPI + Win32 + + + Debug_WithDebugAPI + x64 + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {474EF95D-3579-411B-9DDB-88B49F923D65} + vpp_plugin + Win32Proj + + + + StaticLibrary + Unicode + true + v141 + + + StaticLibrary + Unicode + v141 + + + StaticLibrary + Unicode + v141 + + + StaticLibrary + Unicode + true + v141 + + + StaticLibrary + Unicode + v141 + + + StaticLibrary + Unicode + v141 + + + + + + + + + + + + + + + + + + + + + + + + + <_ProjectFileVersion>10.0.30319.1 + $(ProjectDir)..\..\_build\$(Platform)\$(Configuration)\ + $(ProjectDir)..\..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + $(OutDir)obj\$(ProjectName)\ + $(ProjectDir)..\..\_build\$(Platform)\$(Configuration)\ + $(ProjectDir)..\..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + $(OutDir)obj\$(ProjectName)\ + $(ProjectDir)..\..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + $(ProjectDir)..\..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + AllRules.ruleset + AllRules.ruleset + + + + + AllRules.ruleset + AllRules.ruleset + + + + + AllRules.ruleset + + + AllRules.ruleset + + + $(MINIDDK_ROOT)\Lib\win8\um\x86;$(LibraryPath) + $(MINIDDK_ROOT)\Lib\win8\um\x86;$(LibraryPath) + $(MINIDDK_ROOT)\Lib\win8\um\x86;$(LibraryPath) + $(MINIDDK_ROOT)\Lib\win8\um\x64;$(LibraryPath) + $(MINIDDK_ROOT)\Lib\win8\um\x64;$(LibraryPath) + $(MINIDDK_ROOT)\Lib\win8\um\x64;$(LibraryPath) + $(MINIDDK_ROOT)\Include\um;$(MINIDDK_ROOT)\Include\shared;$(IncludePath) + $(MINIDDK_ROOT)\Include\um;$(MINIDDK_ROOT)\Include\shared;$(IncludePath) + $(MINIDDK_ROOT)\Include\um;$(MINIDDK_ROOT)\Include\shared;$(IncludePath) + $(MINIDDK_ROOT)\Include\um;$(MINIDDK_ROOT)\Include\shared;$(IncludePath) + $(MINIDDK_ROOT)\Include\um;$(MINIDDK_ROOT)\Include\shared;$(IncludePath) + vpp_plugin + vpp_plugin + vpp_plugin + vpp_plugin + vpp_plugin + vpp_plugin + + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\..\sample_common\include;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;_LIB;_ITERATOR_DEBUG_LEVEL=0;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebug + + + Level4 + true + EditAndContinue + + + + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\..\sample_common\include;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebug + + + Level4 + true + EditAndContinue + + + + + + X64 + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\..\sample_common\include;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;_LIB;_ITERATOR_DEBUG_LEVEL=0;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebug + + + Level4 + true + ProgramDatabase + + + + + + X64 + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\..\sample_common\include;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebug + + + Level4 + true + ProgramDatabase + + + + + + $(ProjectDir)\include;$(ProjectDir)\..\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\..\sample_common\include;%(AdditionalIncludeDirectories) + WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + MultiThreaded + + + Level4 + true + ProgramDatabase + + + + + + X64 + + + $(ProjectDir)\include;$(ProjectDir)\..\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\..\sample_common\include;%(AdditionalIncludeDirectories) + WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + MultiThreaded + + + Level4 + true + ProgramDatabase + + + + + + + + + + + + + + \ No newline at end of file diff --git a/samples/sample_plugins/vpp_plugin/vpp_plugin_2015.vcxproj b/samples/sample_plugins/vpp_plugin/vpp_plugin_2015.vcxproj new file mode 100644 index 000000000..0b6b5dd65 --- /dev/null +++ b/samples/sample_plugins/vpp_plugin/vpp_plugin_2015.vcxproj @@ -0,0 +1,248 @@ + + + + + Debug_WithDebugAPI + Win32 + + + Debug_WithDebugAPI + x64 + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {7B440C2A-4F63-4749-9BE6-7E74B79FE1AB} + vpp_plugin + Win32Proj + + + + StaticLibrary + Unicode + true + v141 + + + StaticLibrary + Unicode + v141 + + + StaticLibrary + Unicode + v141 + + + StaticLibrary + Unicode + true + v141 + + + StaticLibrary + Unicode + v141 + + + StaticLibrary + Unicode + v141 + + + + + + + + + + + + + + + + + + + + + + + + + <_ProjectFileVersion>10.0.30319.1 + $(ProjectDir)..\..\_build\$(Platform)\$(Configuration)\ + $(ProjectDir)..\..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + $(OutDir)obj\$(ProjectName)\ + $(ProjectDir)..\..\_build\$(Platform)\$(Configuration)\ + $(ProjectDir)..\..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + $(OutDir)obj\$(ProjectName)\ + $(ProjectDir)..\..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + $(ProjectDir)..\..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + AllRules.ruleset + AllRules.ruleset + + + + + AllRules.ruleset + AllRules.ruleset + + + + + AllRules.ruleset + + + AllRules.ruleset + + + $(MINIDDK_ROOT)\Lib\win8\um\x86;$(LibraryPath) + $(MINIDDK_ROOT)\Lib\win8\um\x86;$(LibraryPath) + $(MINIDDK_ROOT)\Lib\win8\um\x86;$(LibraryPath) + $(MINIDDK_ROOT)\Lib\win8\um\x64;$(LibraryPath) + $(MINIDDK_ROOT)\Lib\win8\um\x64;$(LibraryPath) + $(MINIDDK_ROOT)\Lib\win8\um\x64;$(LibraryPath) + $(MINIDDK_ROOT)\Include\um;$(MINIDDK_ROOT)\Include\shared;$(IncludePath) + $(MINIDDK_ROOT)\Include\um;$(MINIDDK_ROOT)\Include\shared;$(IncludePath) + $(MINIDDK_ROOT)\Include\um;$(MINIDDK_ROOT)\Include\shared;$(IncludePath) + $(MINIDDK_ROOT)\Include\um;$(MINIDDK_ROOT)\Include\shared;$(IncludePath) + $(MINIDDK_ROOT)\Include\um;$(MINIDDK_ROOT)\Include\shared;$(IncludePath) + vpp_plugin + vpp_plugin + vpp_plugin + vpp_plugin + vpp_plugin + vpp_plugin + + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\..\sample_common\include;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;_LIB;_ITERATOR_DEBUG_LEVEL=0;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebug + + + Level4 + true + EditAndContinue + + + + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\..\sample_common\include;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebug + + + Level4 + true + EditAndContinue + + + + + + X64 + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\..\sample_common\include;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;_LIB;_ITERATOR_DEBUG_LEVEL=0;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebug + + + Level4 + true + ProgramDatabase + + + + + + X64 + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\..\sample_common\include;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebug + + + Level4 + true + ProgramDatabase + + + + + + $(ProjectDir)\include;$(ProjectDir)\..\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\..\sample_common\include;%(AdditionalIncludeDirectories) + WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + MultiThreaded + + + Level4 + true + ProgramDatabase + + + + + + X64 + + + $(ProjectDir)\include;$(ProjectDir)\..\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\..\sample_common\include;%(AdditionalIncludeDirectories) + WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + MultiThreaded + + + Level4 + true + ProgramDatabase + + + + + + + + + + + + + + \ No newline at end of file diff --git a/samples/sample_vpp/include/sample_vpp_utils.h b/samples/sample_vpp/include/sample_vpp_utils.h index f0725a897..81cc898d0 100644 --- a/samples/sample_vpp/include/sample_vpp_utils.h +++ b/samples/sample_vpp/include/sample_vpp_utils.h @@ -22,6 +22,9 @@ or https://software.intel.com/en-us/media-client-solutions-support. /* ************************************************************************* */ +#if defined(_WIN32) || defined(_WIN64) +#define D3D_SURFACES_SUPPORT +#endif // #if defined(_WIN32) || defined(_WIN64) #ifdef D3D_SURFACES_SUPPORT #pragma warning(disable : 4201) diff --git a/samples/sample_vpp/sample_vpp.sln b/samples/sample_vpp/sample_vpp.sln new file mode 100644 index 000000000..5c2742d8d --- /dev/null +++ b/samples/sample_vpp/sample_vpp.sln @@ -0,0 +1,38 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27703.2000 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sample_vpp", "sample_vpp.vcxproj", "{A0872728-B771-49B5-93FA-D7140631A829}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sample_common", "..\sample_common\sample_common.vcxproj", "{5FADB243-53C3-4776-A20F-8BD65C10CF41}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Debug|x64 = Debug|x64 + Release|Win32 = Release|Win32 + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {A0872728-B771-49B5-93FA-D7140631A829}.Debug|Win32.ActiveCfg = Debug|Win32 + {A0872728-B771-49B5-93FA-D7140631A829}.Debug|Win32.Build.0 = Debug|Win32 + {A0872728-B771-49B5-93FA-D7140631A829}.Debug|x64.ActiveCfg = Debug|x64 + {A0872728-B771-49B5-93FA-D7140631A829}.Debug|x64.Build.0 = Debug|x64 + {A0872728-B771-49B5-93FA-D7140631A829}.Release|Win32.ActiveCfg = Release|Win32 + {A0872728-B771-49B5-93FA-D7140631A829}.Release|Win32.Build.0 = Release|Win32 + {A0872728-B771-49B5-93FA-D7140631A829}.Release|x64.ActiveCfg = Release|x64 + {A0872728-B771-49B5-93FA-D7140631A829}.Release|x64.Build.0 = Release|x64 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Debug|Win32.ActiveCfg = Debug|Win32 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Debug|Win32.Build.0 = Debug|Win32 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Debug|x64.ActiveCfg = Debug|x64 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Debug|x64.Build.0 = Debug|x64 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Release|Win32.ActiveCfg = Release|Win32 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Release|Win32.Build.0 = Release|Win32 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Release|x64.ActiveCfg = Release|x64 + {5FADB243-53C3-4776-A20F-8BD65C10CF41}.Release|x64.Build.0 = Release|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/samples/sample_vpp/sample_vpp.vcxproj b/samples/sample_vpp/sample_vpp.vcxproj new file mode 100644 index 000000000..9fd7d574f --- /dev/null +++ b/samples/sample_vpp/sample_vpp.vcxproj @@ -0,0 +1,311 @@ + + + + + Debug_WithDebugAPI + Win32 + + + Debug_WithDebugAPI + x64 + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {A0872728-B771-49B5-93FA-D7140631A829} + sample_vpp + Win32Proj + + + + Application + false + Unicode + v141 + + + Application + false + Unicode + v141 + + + Application + false + Unicode + v141 + + + Application + false + Unicode + false + v141 + + + Application + false + Unicode + v141 + + + Application + false + Unicode + v141 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + <_ProjectFileVersion>10.0.30319.1 + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + $(OutDir)obj\$(ProjectName)\ + true + true + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + $(OutDir)obj\$(ProjectName)\ + true + true + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + false + $(ProjectDir)..\_build\$(Platform)\$(Configuration)\ + $(OutDir)obj\$(ProjectName)\ + false + sample_vpp + sample_vpp + sample_vpp + sample_vpp + sample_vpp + sample_vpp + + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\sample_common\include;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;_CONSOLE;_ITERATOR_DEBUG_LEVEL=0;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebug + + + Level4 + true + EditAndContinue + + + /DYNAMICBASE /NXCOMPAT /SAFESEH %(AdditionalOptions) + d3d11.lib;dxgi.lib;libmfx_vs2015.lib;dxva2.lib;d3d9.lib;psapi.lib;%(AdditionalDependencies) + $(ProjectDir)..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;LIBCMT.lib;%(IgnoreSpecificDefaultLibraries) + true + Console + MachineX86 + + + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\sample_common\include;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebug + + + Level4 + true + EditAndContinue + + + /DYNAMICBASE /NXCOMPAT /SAFESEH %(AdditionalOptions) + d3d11.lib;dxgi.lib;libmfx_vs2015_d.lib;dxva2.lib;d3d9.lib;psapi.lib;%(AdditionalDependencies) + $(ProjectDir)..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;LIBCMT.lib;%(IgnoreSpecificDefaultLibraries) + true + Console + MachineX86 + + + + + X64 + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\sample_common\include;%(AdditionalIncludeDirectories) + WIN64;_DEBUG;_CONSOLE;_ITERATOR_DEBUG_LEVEL=0;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebug + + + Level4 + true + ProgramDatabase + + + /DYNAMICBASE /NXCOMPAT %(AdditionalOptions) + d3d11.lib;dxgi.lib;libmfx_vs2015.lib;dxva2.lib;d3d9.lib;psapi.lib;%(AdditionalDependencies) + $(ProjectDir)..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;LIBCMT.lib;%(IgnoreSpecificDefaultLibraries) + true + $(TargetDir)$(TargetName).pdb + Console + MachineX64 + + + + + X64 + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\sample_common\include;%(AdditionalIncludeDirectories) + WIN64;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebug + + + Level4 + true + ProgramDatabase + + + /DYNAMICBASE /NXCOMPAT %(AdditionalOptions) + d3d11.lib;dxgi.lib;libmfx_vs2015_d.lib;dxva2.lib;d3d9.lib;psapi.lib;%(AdditionalDependencies) + $(ProjectDir)..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;LIBCMT.lib;%(IgnoreSpecificDefaultLibraries) + true + $(TargetDir)$(TargetName).pdb + Console + MachineX64 + + + + + $(ProjectDir)\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\sample_common\include;%(AdditionalIncludeDirectories) + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + MultiThreaded + + + Level4 + true + ProgramDatabase + + + /DYNAMICBASE /NXCOMPAT /SAFESEH %(AdditionalOptions) + d3d11.lib;dxgi.lib;libmfx_vs2015.lib;dxva2.lib;d3d9.lib;psapi.lib;%(AdditionalDependencies) + $(ProjectDir)..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;%(IgnoreSpecificDefaultLibraries) + true + Console + true + true + MachineX86 + + + + + X64 + + + Disabled + $(ProjectDir)\include;$(ProjectDir)\..\..\api\include;$(INTELMEDIASDKROOT)\include;$(ProjectDir)\..\sample_common\include;%(AdditionalIncludeDirectories) + WIN64;NDEBUG;_CONSOLE;SAVE_RECON;%(PreprocessorDefinitions) + Default + MultiThreaded + true + + + Level3 + true + + + + + /DYNAMICBASE /NXCOMPAT %(AdditionalOptions) + d3d11.lib;dxgi.lib;libmfx_vs2015.lib;dxva2.lib;d3d9.lib;psapi.lib;%(AdditionalDependencies) + $(ProjectDir)..\..\..\build\win_$(Platform)\lib\;$(INTELMEDIASDKROOT)\lib\$(Platform);%(AdditionalLibraryDirectories) + msvcrt.lib;msvcrtd.lib;%(IgnoreSpecificDefaultLibraries) + true + Console + true + true + MachineX64 + + + + + + + + + + + + + + + + + + + + + + + {5FADB243-53C3-4776-A20F-8BD65C10CF41} + false + + + + + + \ No newline at end of file diff --git a/samples/sample_vpp/src/sample_vpp.cpp b/samples/sample_vpp/src/sample_vpp.cpp index 48bedebe2..6638108ea 100644 --- a/samples/sample_vpp/src/sample_vpp.cpp +++ b/samples/sample_vpp/src/sample_vpp.cpp @@ -863,7 +863,7 @@ int main(int argc, msdk_char *argv[]) extBuffPtrStorage.push_back((mfxExtBuffer *)&m_ProcAmpData[pOutSurf]); m_extBuffPtrStorageForOutputSurf[pOutSurf] = extBuffPtrStorage; pOutSurf->Data.ExtParam = m_extBuffPtrStorageForOutputSurf[pOutSurf].data(); - pOutSurf->Data.NumExtParam = m_extBuffPtrStorageForOutputSurf[pOutSurf].size(); + pOutSurf->Data.NumExtParam = (mfxU16)m_extBuffPtrStorageForOutputSurf[pOutSurf].size(); } nOutFrames++; #endif @@ -1050,7 +1050,7 @@ int main(int argc, msdk_char *argv[]) extBuffPtrStorage.push_back((mfxExtBuffer *)&m_ProcAmpData[pOutSurf]); m_extBuffPtrStorageForOutputSurf[pOutSurf] = extBuffPtrStorage; pOutSurf->Data.ExtParam = m_extBuffPtrStorageForOutputSurf[pOutSurf].data(); - pOutSurf->Data.NumExtParam = m_extBuffPtrStorageForOutputSurf[pOutSurf].size(); + pOutSurf->Data.NumExtParam = (mfxU16)m_extBuffPtrStorageForOutputSurf[pOutSurf].size(); } nOutFrames++; #endif diff --git a/samples/sample_vpp/src/sample_vpp_parser.cpp b/samples/sample_vpp/src/sample_vpp_parser.cpp index c1a512116..8388eda23 100644 --- a/samples/sample_vpp/src/sample_vpp_parser.cpp +++ b/samples/sample_vpp/src/sample_vpp_parser.cpp @@ -64,7 +64,7 @@ msdk_printf(MSDK_STRING(" [-scrY y] - cropY of src video (d msdk_printf(MSDK_STRING(" [-scrW w] - cropW of src video (def: width)\n")); msdk_printf(MSDK_STRING(" [-scrH h] - cropH of src video (def: height)\n")); msdk_printf(MSDK_STRING(" [-sf frameRate] - frame rate of src video (def: 30.0)\n")); -msdk_printf(MSDK_STRING(" [-scc format] - format (FourCC) of src video (def: nv12. support i420|nv12|yv12|yuy2|rgb565|rgb3|rgb4|imc3|yuv400|yuv411|yuv422h|yuv422v|yuv444|uyvy|ayuv|p010)\n")); +msdk_printf(MSDK_STRING(" [-scc format] - format (FourCC) of src video (def: nv12. support i420|nv12|yv12|yuy2|rgb565|rgb3|rgb4|imc3|yuv400|yuv411|yuv422h|yuv422v|yuv444|uyvy|ayuv|p010|y210|y410)\n")); msdk_printf(MSDK_STRING(" [-sbitshift 0|1] - shift data to right or keep it the same way as in Microsoft's P010\n")); msdk_printf(MSDK_STRING(" [-sbitdepthluma value] - shift luma channel to right to \"16 - value\" bytes\n")); msdk_printf(MSDK_STRING(" [-sbitdepthchroma value] - shift chroma channel to right to \"16 - value\" bytes\n")); @@ -83,7 +83,11 @@ msdk_printf(MSDK_STRING(" [-dcrY y] - cropY of dst video (d msdk_printf(MSDK_STRING(" [-dcrW w] - cropW of dst video (def: width)\n")); msdk_printf(MSDK_STRING(" [-dcrH h] - cropH of dst video (def: height)\n")); msdk_printf(MSDK_STRING(" [-df frameRate] - frame rate of dst video (def: 30.0)\n")); -msdk_printf(MSDK_STRING(" [-dcc format] - format (FourCC) of dst video (def: nv12. support i420|nv12|yuy2|rgb4|rgbp|yv12|ayuv)\n")); +#if (MFX_VERSION >= 1027) +msdk_printf(MSDK_STRING(" [-dcc format] - format (FourCC) of dst video (def: nv12. support i420|nv12|yuy2|rgb4|rgbp|yv12|ayuv|a2rgb10|y210|y410)\n")); +#else +msdk_printf(MSDK_STRING(" [-dcc format] - format (FourCC) of dst video (def: nv12. support i420|nv12|yuy2|rgb4|rgbp|yv12|ayuv|a2rgb10)\n")); +#endif msdk_printf(MSDK_STRING(" [-dbitshift 0|1] - shift data to right or keep it the same way as in Microsoft's P010\n")); msdk_printf(MSDK_STRING(" [-dbitdepthluma value] - shift luma channel to left to \"16 - value\" bytes\n")); msdk_printf(MSDK_STRING(" [-dbitdepthchroma value] - shift chroma channel to left to \"16 - value\" bytes\n")); @@ -165,7 +169,12 @@ msdk_printf(MSDK_STRING(" [-deinterlace (type)] - enable deinterlace algorithm msdk_printf(MSDK_STRING(" type is tff (default) or bff \n")); msdk_printf(MSDK_STRING(" [-rotate (angle)] - enable rotation. Supported angles: 0, 90, 180, 270.\n")); -msdk_printf(MSDK_STRING(" [-scaling_mode (mode)] - specify type of scaling to be used for resize.\n")); + +msdk_printf(MSDK_STRING(" [-scaling_mode (mode)] - specify type of scaling to be used for resize\n")); +msdk_printf(MSDK_STRING(" 0 - default\n")); +msdk_printf(MSDK_STRING(" 1 - low power mode\n")); +msdk_printf(MSDK_STRING(" 2 - quality mode\n\n")); + msdk_printf(MSDK_STRING(" [-denoise (level)] - enable denoise algorithm. Level is optional \n")); msdk_printf(MSDK_STRING(" range of noise level is [0, 100]\n")); #if MFX_VERSION >= 1025 @@ -337,10 +346,12 @@ mfxU32 Str2FourCC( msdk_char* strInput ) { fourcc = MFX_FOURCC_RGB4; } +#if !(defined(_WIN32) || defined(_WIN64)) else if ( 0 == msdk_stricmp(strInput, MSDK_STRING("rgbp")) ) { fourcc = MFX_FOURCC_RGBP; } +#endif else if ( 0 == msdk_stricmp(strInput, MSDK_STRING("yuy2")) ) { fourcc = MFX_FOURCC_YUY2; diff --git a/samples/sample_vpp/src/sample_vpp_utils.cpp b/samples/sample_vpp/src/sample_vpp_utils.cpp index b17144339..bfa8fa692 100644 --- a/samples/sample_vpp/src/sample_vpp_utils.cpp +++ b/samples/sample_vpp/src/sample_vpp_utils.cpp @@ -82,8 +82,10 @@ static return MSDK_STRING("RGB3"); case MFX_FOURCC_RGB4: return MSDK_STRING("RGB4"); +#if !(defined(_WIN32) || defined(_WIN64)) case MFX_FOURCC_RGBP: return MSDK_STRING("RGBP"); +#endif case MFX_FOURCC_YUV400: return MSDK_STRING("YUV400"); case MFX_FOURCC_YUV411: @@ -108,6 +110,12 @@ static return MSDK_STRING("AYUV"); case MFX_FOURCC_I420: return MSDK_STRING("I420"); +#if (MFX_VERSION >= 1027) + case MFX_FOURCC_Y210: + return MSDK_STRING("Y210"); + case MFX_FOURCC_Y410: + return MSDK_STRING("Y410"); +#endif default: return MSDK_STRING("Unknown"); } @@ -275,12 +283,20 @@ mfxStatus ParseGUID(msdk_char strPlgGuid[MSDK_MAX_FILENAME_LEN], mfxU8 DataGUID[ for(i = 0; i != 16; i++) { hex = 0; +#if defined(_WIN32) || defined(_WIN64) + if (1 != _stscanf_s(uid + 2*i, L"%2x", &hex)) +#else if (1 != sscanf(uid + 2*i, "%2x", &hex)) +#endif { msdk_printf(MSDK_STRING("Failed to parse plugin uid: %s"), uid); return MFX_ERR_UNKNOWN; } +#if defined(_WIN32) || defined(_WIN64) + if (hex == 0 && (uid + 2*i != _tcsstr(uid + 2*i, L"00"))) +#else if (hex == 0 && (uid + 2*i != strstr(uid + 2*i, "00"))) +#endif { msdk_printf(MSDK_STRING("Failed to parse plugin uid: %s"), uid); return MFX_ERR_UNKNOWN; @@ -1286,6 +1302,28 @@ mfxStatus CRawVideoReader::LoadNextFrame(mfxFrameData* pData, mfxFrameInfo* pInf IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, 4*w, MFX_ERR_MORE_DATA); } } +#if (MFX_VERSION >= 1027) + else if (pInfo->FourCC == MFX_FOURCC_Y210) + { + ptr = (mfxU8*)(pData->Y16 + pInfo->CropX * 2) + pInfo->CropY * pitch; + + for(i = 0; i < h; i++) + { + nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, 4*w, m_fSrc); + IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, 4*w, MFX_ERR_MORE_DATA); + } + } + else if (pInfo->FourCC == MFX_FOURCC_Y410) + { + ptr = (mfxU8*)(pData->Y410 + pInfo->CropX) + pInfo->CropY * pitch; + + for(i = 0; i < h; i++) + { + nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, 4*w, m_fSrc); + IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, 4*w, MFX_ERR_MORE_DATA); + } + } +#endif else { return MFX_ERR_UNSUPPORTED; @@ -1833,6 +1871,7 @@ mfxStatus CRawVideoWriter::WriteFrame( MSDK_CHECK_NOT_EQUAL( fwrite(ptr + i * pitch, 1, 4*w, m_fDst), 4u*w, MFX_ERR_UNDEFINED_BEHAVIOR); } } +#if !(defined(_WIN32) || defined(_WIN64)) else if (pInfo->FourCC == MFX_FOURCC_RGBP) { MSDK_CHECK_POINTER(pData->R, MFX_ERR_NOT_INITIALIZED); @@ -1855,6 +1894,7 @@ mfxStatus CRawVideoWriter::WriteFrame( MSDK_CHECK_NOT_EQUAL( fwrite(ptr + i * pitch, 1, w, m_fDst), w, MFX_ERR_UNDEFINED_BEHAVIOR); } } +#endif else if (pInfo->FourCC == MFX_FOURCC_AYUV) { ptr = std::min( std::min(pData->Y, pData->U), std::min(pData->V, pData->A) ); @@ -1920,6 +1960,22 @@ mfxStatus GeneralWriter::Init( msdk_char out_buf[MSDK_MAX_FILENAME_LEN*4+20]; msdk_char fname[MSDK_MAX_FILENAME_LEN]; +#if defined(_WIN32) || defined(_WIN64) + { + msdk_char drive[MSDK_MAX_FILENAME_LEN]; + msdk_char dir[MSDK_MAX_FILENAME_LEN]; + msdk_char ext[MSDK_MAX_FILENAME_LEN]; + + _tsplitpath_s( + strFileName, + drive, + dir, + fname, + ext); + + msdk_sprintf(out_buf, MSDK_STRING("%s%s%s_layer%i.yuv"), drive, dir, fname, did); + } +#else { msdk_strncopy_s(fname, MSDK_MAX_FILENAME_LEN, strFileName, MSDK_MAX_FILENAME_LEN - 1); fname[MSDK_MAX_FILENAME_LEN - 1] = 0; @@ -1930,6 +1986,7 @@ mfxStatus GeneralWriter::Init( } msdk_sprintf(out_buf, MSDK_STRING("%s_layer%i.yuv"), fname, did); } +#endif sts = m_ofile[did]->Init( (1 == didCount) ? strFileName : out_buf, @@ -2002,6 +2059,38 @@ mfxStatus GetFreeSurface(mfxFrameSurface1* pSurfacesPool, mfxU16 nPoolSize, mfxF void PrintDllInfo() { +#if defined(_WIN32) || defined(_WIN64) + HANDLE hCurrent = GetCurrentProcess(); + HMODULE *pModules; + DWORD cbNeeded; + int nModules; + if (NULL == EnumProcessModules(hCurrent, NULL, 0, &cbNeeded)) + return; + + nModules = cbNeeded / sizeof(HMODULE); + + pModules = new HMODULE[nModules]; + if (NULL == pModules) + { + return; + } + if (NULL == EnumProcessModules(hCurrent, pModules, cbNeeded, &cbNeeded)) + { + delete []pModules; + return; + } + + for (int i = 0; i < nModules; i++) + { + msdk_char buf[2048]; + GetModuleFileName(pModules[i], buf, ARRAYSIZE(buf)); + if (_tcsstr(buf, MSDK_STRING("libmfx"))) + { + msdk_printf(MSDK_STRING("MFX dll %s\n"),buf); + } + } + delete []pModules; +#endif } // void PrintDllInfo() /* ******************************************************************* */