1 // Copyright (c) 2017 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 __MFX_MJPEG_ENCODE_HW_UTILS_H__
22 #define __MFX_MJPEG_ENCODE_HW_UTILS_H__
23 
24 #include "mfx_common.h"
25 
26 #if defined (MFX_ENABLE_MJPEG_VIDEO_ENCODE)
27 
28 #if defined (MFX_VA_LINUX)
29 #include <va/va.h>
30 #include <va/va_enc_jpeg.h>
31 
32 #endif
33 
34 #include "mfxstructures.h"
35 
36 #include <vector>
37 #include <memory>
38 #include <assert.h>
39 
40 #include "vm_interlocked.h"
41 #include "umc_mutex.h"
42 
43 #include "mfxstructures.h"
44 #include "mfxjpeg.h"
45 
46 namespace MfxHwMJpegEncode
47 {
48     #define  JPEG_VIDEO_SURFACE_NUM    4
49     #define  JPEG_DDITASK_MAX_NUM      32
50 
51     enum
52     {
53         MFX_MEMTYPE_VIDEO_INT = MFX_MEMTYPE_FROM_ENCODE | MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET | MFX_MEMTYPE_INTERNAL_FRAME,
54         MFX_MEMTYPE_VIDEO_EXT = MFX_MEMTYPE_FROM_ENCODE | MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET | MFX_MEMTYPE_EXTERNAL_FRAME
55     };
56 
57     typedef struct {
58         mfxU32    Baseline;
59         mfxU32    Sequential;
60         mfxU32    Huffman;
61 
62         mfxU32    NonInterleaved;
63         mfxU32    Interleaved;
64 
65         mfxU32    MaxPicWidth;
66         mfxU32    MaxPicHeight;
67 
68         mfxU32    SampleBitDepth;
69         mfxU32    MaxNumComponent;
70         mfxU32    MaxNumScan;
71         mfxU32    MaxNumHuffTable;
72         mfxU32    MaxNumQuantTable;
73     } JpegEncCaps;
74 
75     typedef struct {
76         mfxU16 header;
77         mfxU8  lenH;
78         mfxU8  lenL;
79         mfxU8  s[5];
80         mfxU8  versionH;
81         mfxU8  versionL;
82         mfxU8  units;
83         mfxU16 xDensity;
84         mfxU16 yDensity;
85         mfxU8  xThumbnails;
86         mfxU8  yThumbnails;
87     } JpegApp0Data;
88 
89     typedef struct {
90         mfxU16 header;
91         mfxU8  lenH;
92         mfxU8  lenL;
93         mfxU8  s[5];
94         mfxU8  versionH;
95         mfxU8  versionL;
96         mfxU8  flags0H;
97         mfxU8  flags0L;
98         mfxU8  flags1H;
99         mfxU8  flags1L;
100         mfxU8  transform;
101     } JpegApp14Data;
102 
103     typedef struct {
104         mfxU8 * data;
105         mfxU32  length;
106         mfxU32  maxLength;
107     } JpegPayload;
108 
109     mfxStatus QueryHwCaps(
110         VideoCORE * core,
111         JpegEncCaps & hwCaps);
112 
113     bool IsJpegParamExtBufferIdSupported(
114         mfxU32 id);
115 
116     mfxStatus CheckExtBufferId(
117         mfxVideoParam const & par);
118 
119     mfxStatus CheckJpegParam(
120         VideoCORE         * core,
121         mfxVideoParam     & par,
122         JpegEncCaps const & hwCaps);
123 
124     mfxStatus FastCopyFrameBufferSys2Vid(
125         VideoCORE    * core,
126         mfxMemId       vidMemId,
127         mfxFrameData & sysSurf,
128         mfxFrameInfo & frmInfo
129         );
130 
131     struct ExecuteBuffers
132     {
ExecuteBuffersExecuteBuffers133         ExecuteBuffers()
134         {
135             memset(&m_pps, 0, sizeof(m_pps));
136             memset(&m_payload_base, 0, sizeof(m_payload_base));
137             memset(&m_app0_data, 0, sizeof(m_app0_data));
138             memset(&m_app14_data, 0, sizeof(m_app14_data));
139         }
140 
141         mfxStatus Init(mfxVideoParam const *par, mfxEncodeCtrl const * ctrl, JpegEncCaps const * hwCaps);
142         void      Close();
143 
144 #if defined (MFX_VA_LINUX)
145         VAEncPictureParameterBufferJPEG               m_pps;
146         std::vector<VAEncSliceParameterBufferJPEG>    m_scan_list;
147         std::vector<VAQMatrixBufferJPEG>              m_dqt_list;
148         std::vector<VAHuffmanTableBufferJPEGBaseline> m_dht_list;
149 
150 #endif
151         std::vector<JpegPayload>                      m_payload_list;
152         JpegPayload                                   m_payload_base;
153         JpegApp0Data                                  m_app0_data;
154         JpegApp14Data                                 m_app14_data;
155     };
156 
157     typedef struct {
158         mfxFrameSurface1 * surface;              // input raw surface
159         mfxBitstream     * bs;                   // output bitstream
160         mfxU32             m_idx;                // index of raw surface
161         mfxU32             m_idxBS;              // index of bitstream surface (always equal as m_idx for now)
162         mfxU32             lInUse;               // 0: free, 1: used.
163         mfxU32             m_statusReportNumber;
164         mfxU32             m_bsDataLength;       // output bitstream length
165         bool               m_cleanDdiData;
166         ExecuteBuffers   * m_pDdiData;
167     } DdiTask;
168 
169     class TaskManager
170     {
171     public:
172         TaskManager();
173         ~TaskManager();
174 
175         mfxStatus Init(mfxU32 maxTaskNum);
176         mfxStatus Reset();
177         mfxStatus Close();
178 
179         mfxStatus AssignTask(DdiTask *& newTask);
180         mfxStatus RemoveTask(DdiTask & task);
181     private:
182         DdiTask * m_pTaskList;
183         mfxU32    m_TaskNum;
184 
185         UMC::Mutex    m_mutex;
186     };
187 
188 }; // namespace MfxHwMJpegEncode
189 
190 #endif // #if defined (MFX_ENABLE_MJPEG_VIDEO_ENCODE) && defined (MFX_VA)
191 #endif // __MFX_MJPEG_ENCODE_HW_UTILS_H__
192