1 // Copyright (c) 2018-2019 Intel Corporation
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in all
11 // copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 // SOFTWARE.
20 
21 #ifndef __ASGLOG_H__
22 #define __ASGLOG_H__
23 
24 #include "mfxvideo.h"
25 
26 #if MFX_VERSION >= MFX_VERSION_NEXT
27 
28 #include <iostream>
29 #include <string>
30 #include <sstream>
31 #include <tuple>
32 #include <iomanip>
33 
34 #include "mfxdefs.h"
35 #include "block_structures.h"
36 
37 //Logger object, basically a tee into cout and log file
38 class ASGLog
39 {
40 public:
41     //Tuple field designations
42     enum { MVX = 0, MVY = 1, REFIDX = 2 };
43 
Init(const InputParams & params)44     void Init(const InputParams& params)
45     {
46         m_bVerbose = params.m_bVerbose;
47         if (params.m_bUseLog)
48         {
49             m_LogFileOutput.open(params.m_LogFileName.c_str(), std::ofstream::out);
50             if (!m_LogFileOutput.is_open())
51             {
52                 throw std::string("ERROR: ASGLog: unable to open log file");
53             }
54         }
55     }
56 
57     //Convenient ofstream-style operator<< with chaining feature
58     template<class T>
59     ASGLog& operator<<(const T& output)
60     {
61         if (m_bVerbose)
62         {
63             std::cout << output;
64         }
65         if (m_LogFileOutput.is_open())
66         {
67             m_LogFileOutput << output;
68         }
69         return *this;
70     }
71 
72     //Specialization for basic block info output
73     ASGLog& operator<< (const CTUDescriptor& outCTU)
74     {
75         return (*this << "CTU " << static_cast<BaseBlock>(outCTU));
76     }
77 
78     ASGLog& operator<< (const CUBlock& outCU)
79     {
80         return (*this << "CU " << static_cast<BaseBlock>(outCU));
81     }
82 
83     ASGLog& operator<< (const PUBlock& outPU)
84     {
85         return (*this << "PU " << static_cast<BaseBlock>(outPU));
86     }
87 
88     //Specialization for basic block info output
89     ASGLog& operator<< (const BaseBlock& outBB)
90     {
91         std::stringstream ss;
92         ss << outBB.m_BHeight << "x" << outBB.m_BWidth <<
93             " (" << outBB.m_AdrX << ";" << outBB.m_AdrY << ")";
94         return (*this << ss.str());
95     }
96 
97     //Specialization for MV log output
98     ASGLog& operator<< (const std::tuple<mfxI32, mfxI32, mfxU32>& MV)
99     {
100         std::stringstream ss;
101 
102         ss << "(" << std::get<MVX>(MV) << ";" << std::get<MVY>(MV) << ";" << std::get<REFIDX>(MV) << ")";
103 
104         return (*this << ss.str());
105     }
106 
107     //Specialization for I/O stream manipulators (i.e. std::endl)
108     ASGLog& operator<< (std::ostream&(*pMan)(std::ostream&))
109     {
110         if (m_bVerbose)
111         {
112             std::cout << *pMan;
113         }
114         if (m_LogFileOutput.is_open())
115         {
116             m_LogFileOutput << *pMan;
117         }
118         return *this;
119     }
~ASGLog()120     ~ASGLog()
121     {
122         if (m_LogFileOutput.is_open())
123         {
124             m_LogFileOutput.close();
125         }
126     }
127 
128 private:
129     bool m_bVerbose = false;
130     std::ofstream m_LogFileOutput;
131 };
132 
133 #endif // MFX_VERSION > 1024
134 
135 #endif //__ASGLOG_H__
136