1 /*--------------------------------------------------------------------------
2 Copyright (c) 2014-2020, The Linux Foundation. All rights reserved.
3 
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are
6 met:
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9     * Redistributions in binary form must reproduce the above
10       copyright notice, this list of conditions and the following
11       disclaimer in the documentation and/or other materials provided
12       with the distribution.
13     * Neither the name of The Linux Foundation nor the names of its
14       contributors may be used to endorse or promote products derived
15       from this software without specific prior written permission.
16 
17 THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 --------------------------------------------------------------------------*/
29 #ifndef __OMX_VENC__H
30 #define __OMX_VENC__H
31 
32 #define VEN_EXTRADATA_SLICEINFO     0x100
33 #define VEN_EXTRADATA_MBINFO        0x400
34 
35 #include <unistd.h>
36 #include "omx_video_base.h"
37 #include "video_encoder_device_v4l2.h"
38 
39 #include "swvenc_api.h"
40 #include "swvenc_types.h"
41 
42 #include <ui/GraphicBuffer.h>
43 #include <media/msm_media_info.h>
44 
45 extern "C" {
46     OMX_API void * get_omx_component_factory_fn(void);
47 }
48 
49 struct swvenc_video_capability {
50     unsigned int min_width;
51     unsigned int max_width;
52     unsigned int min_height;
53     unsigned int max_height;
54 };
55 
56 
57 class omx_venc: public omx_video
58 {
59     public:
60         omx_venc();
61         ~omx_venc();
62         OMX_ERRORTYPE component_init(OMX_STRING role);
63         OMX_ERRORTYPE set_parameter(OMX_HANDLETYPE hComp,
64                 OMX_INDEXTYPE  paramIndex,
65                 OMX_PTR        paramData);
66         OMX_ERRORTYPE set_config(OMX_HANDLETYPE hComp,
67                 OMX_INDEXTYPE  configIndex,
68                 OMX_PTR        configData);
69         OMX_ERRORTYPE component_deinit(OMX_HANDLETYPE hComp);
70         bool is_secure_session();
71         //OMX strucutres
72         OMX_U32 m_nVenc_format;
73 
74         SWVENC_HANDLE m_hSwVenc;
75         SWVENC_CODEC  m_codec;
76         swvenc_video_capability m_capability;
77         bool m_stopped;
78         bool set_format;
79         bool update_offset;
80         int dev_handle_output_extradata(void *, int);
81         int dev_handle_input_extradata(void *, int, int);
82         bool dev_buffer_ready_to_queue(OMX_BUFFERHEADERTYPE *buffer);
83         bool dev_get_dimensions(OMX_U32 ,OMX_U32 *,OMX_U32 *);
84         bool dev_is_meta_mode();
85         bool dev_is_avtimer_needed();
86         void dev_set_extradata_cookie(void *);
87         int dev_set_format(int);
88 
89         static SWVENC_STATUS swvenc_empty_buffer_done_cb
90         (
91           SWVENC_HANDLE    swvenc,
92           SWVENC_IPBUFFER *p_ipbuffer,
93           void            *p_client
94         );
95         SWVENC_STATUS swvenc_empty_buffer_done
96         (
97           SWVENC_IPBUFFER *p_ipbuffer
98         );
99         static SWVENC_STATUS swvenc_fill_buffer_done_cb
100         (
101             SWVENC_HANDLE    swvenc,
102             SWVENC_OPBUFFER *p_opbuffer,
103             void            *p_client
104         );
105         static SWVENC_STATUS swvenc_handle_event_cb
106         (
107             SWVENC_HANDLE swvenc,
108             SWVENC_EVENT  event,
109             void         *p_client
110         );
111 
112         static void init_sw_vendor_extensions(VendorExtensionStore &store);
113 
114     private:
115         venc_debug_cap m_debug;
116         bool m_bSeqHdrRequested;
117 
118         bool m_bDimensionsNeedFlip;
119         bool m_bIsRotationSupported;
120         bool m_bIsInFrameSizeSet;
121         bool m_bIsOutFrameSizeSet;
122         bool m_bIsInFlipDone;
123         bool m_bIsOutFlipDone;
124         bool m_bUseAVTimerTimestamps;
125         bool m_bIsIntraperiodSet;
126         sp<GraphicBuffer> dstBuffer;
127         SWVENC_IPBUFFER *m_pIpbuffers;
128 
129         enum color_format
130         {
131             COLOR_FMT_NV12,
132             COLOR_FMT_NV21,
133             COLOR_FMT_NV12_ZSL,
134         };
135         OMX_U32 dev_stop(void);
136         OMX_U32 dev_pause(void);
137         OMX_U32 dev_start(void);
138         OMX_U32 dev_flush(unsigned);
139         OMX_U32 dev_resume(void);
140         OMX_U32 dev_start_done(void);
141         OMX_U32 dev_set_message_thread_id(pthread_t);
142         bool dev_use_buf( unsigned);
143         bool dev_handle_empty_eos_buffer(void);
144         bool dev_free_buf( void *,unsigned);
145         bool dev_empty_buf(void *, void *,unsigned,unsigned);
146         bool dev_fill_buf(void *, void *,unsigned,unsigned);
147         bool dev_get_buf_req(OMX_U32 *,OMX_U32 *,OMX_U32 *,OMX_U32);
148         bool is_streamon_done(OMX_U32 port);
149         bool dev_set_buf_req(OMX_U32 const *,OMX_U32 const *,OMX_U32 const *,OMX_U32);
150         bool dev_get_seq_hdr(void *, unsigned, unsigned *);
151         bool dev_loaded_start(void);
152         bool dev_loaded_stop(void);
153         bool dev_loaded_start_done(void);
154         bool dev_loaded_stop_done(void);
155         bool dev_get_capability_ltrcount(OMX_U32 *, OMX_U32 *, OMX_U32 *);
156         bool dev_get_vui_timing_info(OMX_U32 *);
157         bool dev_get_peak_bitrate(OMX_U32 *);
158         bool dev_get_batch_size(OMX_U32 *);
dev_get_temporal_layer_caps(OMX_U32 *,OMX_U32 *,OMX_VIDEO_ANDROID_TEMPORALLAYERINGPATTERNTYPE *)159         bool dev_get_temporal_layer_caps(OMX_U32 * /*nMaxLayers*/,
160                     OMX_U32 * /*nMaxBLayers*/,
161                     OMX_VIDEO_ANDROID_TEMPORALLAYERINGPATTERNTYPE */*SupportedPattern*/) {
162             return false;
163         }
164         OMX_ERRORTYPE dev_get_supported_profile_level(OMX_VIDEO_PARAM_PROFILELEVELTYPE */*profileLevelType*/);
165         bool dev_get_supported_color_format(unsigned index, OMX_U32 *colorFormat);
166         bool dev_is_video_session_supported(OMX_U32 width, OMX_U32 height);
167         bool dev_color_align(OMX_BUFFERHEADERTYPE *buffer, OMX_U32 width,
168                         OMX_U32 height);
169         bool dev_get_output_log_flag();
170         int dev_output_log_buffers(const char *buffer_addr, int buffer_len, uint64_t timestamp);
171         int dev_extradata_log_buffers(char *buffer, int index, bool input);
172         bool swvenc_color_align(OMX_BUFFERHEADERTYPE *buffer, OMX_U32 width,
173                                 OMX_U32 height);
174         OMX_ERRORTYPE swvenc_do_flip_inport();
175         OMX_ERRORTYPE swvenc_do_flip_outport();
176         bool swvenc_do_rotate(int, SWVENC_IPBUFFER &, OMX_U32);
177 
178         template<typename T>
swvenc_delete_pointer(T * & ptr)179         inline void swvenc_delete_pointer(T * &ptr) {
180             if (ptr != nullptr) {
181                 delete ptr;
182                 ptr = nullptr;
183             }
184         }
185 
186         SWVENC_STATUS swvenc_set_rc_mode(OMX_VIDEO_CONTROLRATETYPE eControlRate);
187         SWVENC_STATUS swvenc_set_profile_level(OMX_U32 eProfile,OMX_U32 eLevel);
188         SWVENC_STATUS swvenc_set_intra_refresh(OMX_VIDEO_PARAM_INTRAREFRESHTYPE *IntraRefresh);
189         SWVENC_STATUS swvenc_set_frame_rate(OMX_U32 nFrameRate);
190         SWVENC_STATUS swvenc_set_bit_rate(OMX_U32 nTargetBitrate);
191         SWVENC_STATUS swvenc_set_intra_period(OMX_U32 nPFrame,OMX_U32 nBFrame);
192         SWVENC_STATUS swvenc_set_color_format(OMX_COLOR_FORMATTYPE);
193         SWVENC_STATUS swvenc_get_buffer_req
194         (
195            OMX_U32 *min_buff_count,
196            OMX_U32 *actual_buff_count,
197            OMX_U32 *buff_size,
198            OMX_U32 *buff_alignment,
199            OMX_U32 port
200         );
201         int swvenc_input_log_buffers(const char *buffer, int bufferlen);
202         int swvenc_input_log_rotated_buffers(const char *buffer, int bufferlen);
dev_get_hevc_profile(OMX_U32 *)203         bool dev_get_hevc_profile(OMX_U32*) { return false; }
dev_handle_client_input_extradata(void *)204         bool dev_handle_client_input_extradata(void*) { return false; }
dev_get_color_format_as_string(char *,int,unsigned)205         void dev_get_color_format_as_string(char *, int, unsigned) {};
206         /* Only NV12 is supported so not setting UBWC & 10Bit flag */
dev_get_consumer_usage(OMX_U32 * usage)207         void dev_get_consumer_usage(OMX_U32* usage) { *usage = 0; }
SWVENC_Y_STRIDE(int color_fmt,int width)208         static inline unsigned int SWVENC_Y_STRIDE(int color_fmt, int width)
209         {
210             unsigned int stride = 0;
211             if (!width)
212                 goto invalid_input;
213             /*mapping the ZSL to NV12 color format for now
214             * remove the mapping once ZSL design is in place */
215             if(color_fmt == COLOR_FMT_NV12_ZSL)
216                 color_fmt = COLOR_FMT_NV12;
217             switch (color_fmt)
218             {
219             case COLOR_FMT_NV21:
220                 stride = VENUS_Y_STRIDE(COLOR_FMT_NV21, width);
221                 break;
222             case COLOR_FMT_NV12:
223                 stride = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
224                 break;
225             case COLOR_FMT_NV12_ZSL:
226                 stride = VENUS_Y_STRIDE(COLOR_FMT_NV12_ZSL, width);
227                 break;
228             default:
229                 break;
230             }
231             invalid_input:
232                 return stride;
233         }
SWVENC_Y_SCANLINES(int color_fmt,int height)234         static inline unsigned int SWVENC_Y_SCANLINES(int color_fmt, int height)
235         {
236             unsigned int scanlines = 0;
237             if (!height)
238                 goto invalid_input;
239             /*mapping the ZSL to NV12 color format for now
240             * remove the mapping once ZSL design is in place */
241             if(color_fmt == COLOR_FMT_NV12_ZSL)
242                 color_fmt = COLOR_FMT_NV12;
243             switch (color_fmt)
244             {
245             case COLOR_FMT_NV21:
246                 scanlines = VENUS_Y_SCANLINES(COLOR_FMT_NV21, height);
247                 break;
248             case COLOR_FMT_NV12:
249                 scanlines = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
250                 break;
251             case COLOR_FMT_NV12_ZSL:
252                 scanlines = VENUS_Y_SCANLINES(COLOR_FMT_NV12_ZSL, height);
253                 break;
254             default:
255                 return 0;
256             }
257             invalid_input:
258                 return scanlines;
259         }
SWVENC_UV_SCANLINES(int color_fmt,int height)260         static inline unsigned int SWVENC_UV_SCANLINES(int color_fmt, int height)
261         {
262             unsigned int uv_canlines = 0;
263             if (!height)
264                 goto invalid_input;
265             /*mapping the ZSL to NV12 color format for now
266             * remove the mapping once ZSL design is in place */
267             if(color_fmt == COLOR_FMT_NV12_ZSL)
268                 color_fmt = COLOR_FMT_NV12;
269             switch (color_fmt)
270             {
271                 case COLOR_FMT_NV21:
272                     uv_canlines = VENUS_UV_SCANLINES(COLOR_FMT_NV21, height);
273                     break;
274                 case COLOR_FMT_NV12:
275                     uv_canlines = VENUS_UV_SCANLINES(COLOR_FMT_NV12, height);
276                     break;
277                 case COLOR_FMT_NV12_ZSL:
278                     uv_canlines = VENUS_UV_SCANLINES(COLOR_FMT_NV12_ZSL, height);
279                     break;
280                 default:
281                     goto invalid_input;
282             }
283             invalid_input:
284                 return uv_canlines;
285         }
SWVENC_BUFFER_SIZE(int color_fmt,int width,int height)286         static inline unsigned int SWVENC_BUFFER_SIZE(int color_fmt, int width, int height)
287         {
288             unsigned int size = 0;
289             if (!width || !height)
290                 goto invalid_input;
291             /*mapping the ZSL to NV12 color format for now
292             * remove the mapping once ZSL design is in place */
293             if(color_fmt == COLOR_FMT_NV12_ZSL)
294                 color_fmt = COLOR_FMT_NV12;
295             switch (color_fmt)
296             {
297                 case COLOR_FMT_NV21:
298                     size = VENUS_BUFFER_SIZE(COLOR_FMT_NV21, width, height);
299                     break;
300                 case COLOR_FMT_NV12:
301                     size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height);
302                     break;
303                 case COLOR_FMT_NV12_ZSL:
304                     size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_ZSL, width, height);
305                     break;
306                default:
307                    break;
308             }
309             invalid_input:
310                 return size;
311         }
312 
313 };
314 
315 #endif //__OMX_VENC__H
316