1 /*--------------------------------------------------------------------------
2 Copyright (c) 2014-2019, 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 
44 extern "C" {
45     OMX_API void * get_omx_component_factory_fn(void);
46 }
47 
48 struct swvenc_video_capability {
49     unsigned int min_width;
50     unsigned int max_width;
51     unsigned int min_height;
52     unsigned int max_height;
53 };
54 
55 
56 class omx_venc: public omx_video
57 {
58     public:
59         omx_venc();
60         ~omx_venc();
61         OMX_ERRORTYPE component_init(OMX_STRING role);
62         OMX_ERRORTYPE set_parameter(OMX_HANDLETYPE hComp,
63                 OMX_INDEXTYPE  paramIndex,
64                 OMX_PTR        paramData);
65         OMX_ERRORTYPE set_config(OMX_HANDLETYPE hComp,
66                 OMX_INDEXTYPE  configIndex,
67                 OMX_PTR        configData);
68         OMX_ERRORTYPE component_deinit(OMX_HANDLETYPE hComp);
69         bool is_secure_session();
70         //OMX strucutres
71         OMX_U32 m_nVenc_format;
72 
73         SWVENC_HANDLE m_hSwVenc;
74         SWVENC_CODEC  m_codec;
75         swvenc_video_capability m_capability;
76         bool m_max_allowed_bitrate_check;
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         void dev_set_extradata_cookie(void *);
85         int dev_set_format(int);
86         bool dev_query_cap(struct v4l2_queryctrl &);
87 
88         static SWVENC_STATUS swvenc_empty_buffer_done_cb
89         (
90           SWVENC_HANDLE    swvenc,
91           SWVENC_IPBUFFER *p_ipbuffer,
92           void            *p_client
93         );
94         SWVENC_STATUS swvenc_empty_buffer_done
95         (
96           SWVENC_IPBUFFER *p_ipbuffer
97         );
98         static SWVENC_STATUS swvenc_fill_buffer_done_cb
99         (
100             SWVENC_HANDLE    swvenc,
101             SWVENC_OPBUFFER *p_opbuffer,
102             void            *p_client
103         );
104         static SWVENC_STATUS swvenc_handle_event_cb
105         (
106             SWVENC_HANDLE swvenc,
107             SWVENC_EVENT  event,
108             void         *p_client
109         );
110 
111         static void init_sw_vendor_extensions(VendorExtensionStore &store);
112 
113     private:
114         venc_debug_cap m_debug;
115         bool m_bSeqHdrRequested;
116 
117         bool m_bDimensionsNeedFlip;
118         bool m_bIsRotationSupported;
119         bool m_bIsInFrameSizeSet;
120         bool m_bIsOutFrameSizeSet;
121         bool m_bIsInFlipDone;
122         bool m_bIsOutFlipDone;
123         bool m_bUseAVTimerTimestamps;
124         bool m_bIsIntraperiodSet;
125         sp<GraphicBuffer> dstBuffer;
126         SWVENC_IPBUFFER *m_pIpbuffers;
127 
128         enum color_format
129         {
130             COLOR_FMT_NV12,
131             COLOR_FMT_NV21,
132             COLOR_FMT_NV12_ZSL,
133         };
134         OMX_U32 dev_stop(void);
135         OMX_U32 dev_pause(void);
136         OMX_U32 dev_start(void);
137         OMX_U32 dev_flush(unsigned);
138         OMX_U32 dev_resume(void);
139         OMX_U32 dev_start_done(void);
140         OMX_U32 dev_set_message_thread_id(pthread_t);
141         bool dev_use_buf( unsigned);
142         bool dev_handle_empty_eos_buffer(void);
143         bool dev_free_buf( void *,unsigned);
144         bool dev_empty_buf(void *, void *,unsigned,unsigned);
145         bool dev_fill_buf(void *, void *,unsigned,unsigned);
146         bool dev_get_buf_req(OMX_U32 *,OMX_U32 *,OMX_U32 *,OMX_U32);
147         bool is_streamon_done(OMX_U32 port);
148         bool dev_set_buf_req(OMX_U32 const *,OMX_U32 const *,OMX_U32 const *,OMX_U32);
149         bool dev_get_seq_hdr(void *, unsigned, unsigned *);
150         bool dev_loaded_start(void);
151         bool dev_loaded_stop(void);
152         bool dev_loaded_start_done(void);
153         bool dev_loaded_stop_done(void);
154         bool dev_get_capability_ltrcount(OMX_U32 *, OMX_U32 *, OMX_U32 *);
155         bool dev_get_vui_timing_info(OMX_U32 *);
156         bool dev_get_vqzip_sei_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, 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 alignment, stride = 0;
211             if (!width)
212                 goto invalid_input;
213             switch (color_fmt)
214             {
215             case COLOR_FMT_NV21:
216             case COLOR_FMT_NV12:
217             case COLOR_FMT_NV12_ZSL:
218                 alignment = 128;
219                 stride = ALIGN(width, alignment);
220                 break;
221             default:
222                 break;
223             }
224             invalid_input:
225                 return stride;
226         }
SWVENC_Y_SCANLINES(int color_fmt,int height)227         static inline unsigned int SWVENC_Y_SCANLINES(int color_fmt, int height)
228         {
229 	    unsigned int alignment, scanlines = 0;
230             if (!height)
231                 goto invalid_input;
232             switch (color_fmt)
233             {
234             case COLOR_FMT_NV21:
235             case COLOR_FMT_NV12:
236                 alignment = 32;
237                 break;
238             case COLOR_FMT_NV12_ZSL:
239                 alignment = 64;
240                 break;
241             default:
242                 return 0;
243             }
244             scanlines = ALIGN(height, alignment);
245             invalid_input:
246                 return scanlines;
247         }
SWVENC_UV_SCANLINES(int color_fmt,int height)248         static inline unsigned int SWVENC_UV_SCANLINES(int color_fmt, int height)
249         {
250             unsigned int alignment, scanlines = 0;
251             if (!height)
252                 goto invalid_input;
253             switch (color_fmt)
254             {
255                 case COLOR_FMT_NV21:
256                 case COLOR_FMT_NV12:
257                 case COLOR_FMT_NV12_ZSL:
258                     alignment = 16;
259                     break;
260                 default:
261                     goto invalid_input;
262             }
263             scanlines = ALIGN((height+1)>>1, alignment);
264             invalid_input:
265                 return scanlines;
266         }
SWVENC_BUFFER_SIZE(int color_fmt,int width,int height)267         static inline unsigned int SWVENC_BUFFER_SIZE(int color_fmt, int width, int height)
268         {
269             unsigned int uv_alignment = 0, size = 0;
270             unsigned int y_plane, uv_plane, y_stride,uv_stride, y_scanlines, uv_scanlines;
271             if (!width || !height)
272                 goto invalid_input;
273             y_stride = SWVENC_Y_STRIDE(color_fmt, width);
274             uv_stride = y_stride;
275             y_scanlines = SWVENC_Y_SCANLINES(color_fmt, height);
276             uv_scanlines = SWVENC_UV_SCANLINES(color_fmt, height);
277             switch (color_fmt)
278             {
279                 case COLOR_FMT_NV21:
280                 case COLOR_FMT_NV12:
281                 case COLOR_FMT_NV12_ZSL:
282                     uv_alignment = 4096;
283                     y_plane = y_stride * y_scanlines;
284                     uv_plane = uv_stride * uv_scanlines + uv_alignment;
285                     size = ALIGN((y_plane + uv_plane),4096);
286                     break;
287                default:
288                    break;
289             }
290             invalid_input:
291                 return size;
292         }
293 
294 };
295 
296 #endif //__OMX_VENC__H
297