1 /******************************************************************************\
2 Copyright (c) 2005-2019, Intel Corporation
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6 
7 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8 
9 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.
10 
11 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.
12 
13 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.
14 
15 This sample was distributed or derived from the Intel's Media Samples package.
16 The original version of this sample may be obtained from https://software.intel.com/en-us/intel-media-server-studio
17 or https://software.intel.com/en-us/media-client-solutions-support.
18 \**********************************************************************************/
19 
20 #include <math.h>
21 #include "vm/strings_defs.h"
22 #include <time.h>
23 #include <stdlib.h>
24 
25 #include "vm/time_defs.h"
26 
27 #include "sample_vpp_utils.h"
28 #include "sample_vpp_roi.h"
29 
30 #define VPP_ALIGN2(SZ)  (((SZ + 1) >> 1) << 1) // round up to a multiple of 2
31 #define VPP_MSDK_ALIGN16(SZ)  (((SZ + 15) >> 4) << 4) // round up to a multiple of 16
32 #define VPP_MSDK_ALIGN32(SZ)  (((SZ + 31) >> 5) << 5) // round up to a multiple of 32
33 
34 #ifndef MFX_VERSION
35 #error MFX_VERSION not defined
36 #endif
37 
ownRandomGenerator_32s(int lowest,int highest)38 int ownRandomGenerator_32s( int lowest, int highest )
39 {
40     int random_integer;
41     int range=(highest-lowest)+1;
42 
43     random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
44 
45     return random_integer;
46 }
47 
48 /* *************************************************************************** */
49 
ROIGenerator(void)50 ROIGenerator::ROIGenerator( void )
51 {
52     m_width = m_height = 0;
53     m_seed = 0;
54 } // ROIGenerator::ROIGenerator( void )
55 
56 
~ROIGenerator(void)57 ROIGenerator::~ROIGenerator( void )
58 {
59     Close();
60 
61 } // ROIGenerator::~ROIGenerator( void )
62 
Init(mfxU16 width,mfxU16 height,int seed)63 mfxStatus ROIGenerator::Init(  mfxU16  width, mfxU16  height, int seed)
64 {
65     m_width  = width;
66     m_height = height;
67 
68     m_seed = seed;
69 
70     if( 0 == seed )
71     {
72         m_seed = (int)time(NULL);
73     }
74 
75     srand( m_seed );
76 
77     //printf("\nroi seed = %i \n", m_seed);
78 
79     return MFX_ERR_NONE;
80 
81 } // mfxStatus ROIGenerator::Init( mfxVideoParam *par )
82 
Close(void)83 mfxStatus ROIGenerator::Close( void )
84 {
85     m_width = m_height = 0;
86 
87     return MFX_ERR_NONE;
88 
89 } // mfxStatus ROIGenerator::Close( void )
90 
91 
SetROI(mfxFrameInfo * pInfo)92 mfxStatus ROIGenerator:: SetROI(mfxFrameInfo* pInfo)
93 {
94     int lowest, highest;
95     int result;
96 
97     // roi_width
98     lowest = 16;
99     highest = (int)(m_width - 16);
100 
101     result = ownRandomGenerator_32s(lowest, highest);
102     result = (((result + 15) >> 4) << 4);
103     pInfo->CropW = (mfxU16)result;
104 
105     // roi_height
106     lowest = 32;
107     highest = (int)(m_height - 32);
108 
109     result = (mfxU16)ownRandomGenerator_32s(lowest, highest);
110     result = (((result + 31) >> 5) << 5);
111     pInfo->CropH = (mfxU16)result;
112 
113     // roi_x
114     lowest = 0;
115     highest = (int)(m_width - pInfo->CropW);
116 
117     result = ownRandomGenerator_32s(lowest, highest);
118     result = (((result + 1) >> 1) << 1);
119     pInfo->CropX = (mfxU16)result;
120 
121     // roi_y
122     lowest = 0;
123     highest = (int)(m_height - pInfo->CropH);
124 
125     result = ownRandomGenerator_32s(lowest, highest);
126     result = (((result + 1) >> 1) << 1);
127     pInfo->CropY = (mfxU16)result;
128 
129     //printf("\nroi (x, y, w, h) = (%i, %i, %i, %i)\n", pInfo->CropX, pInfo->CropY, pInfo->CropW, pInfo->CropH);
130 
131     return MFX_ERR_NONE;
132 
133 } // mfxStatus ROIGenerator::SetROI(mfxFrameInfo* pInfo)
134 
GetSeed(void)135 mfxI32 ROIGenerator::GetSeed( void )
136 {
137     return m_seed;
138 }
139 
140 /* EOF */