1 /*--------------------------------------------------------------------------
2 Copyright (c) 2010-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 
30 #ifndef __OMX_VIDEO_BASE_H__
31 #define __OMX_VIDEO_BASE_H__
32 /*============================================================================
33                             O p e n M A X   Component
34                                 Video Encoder
35 
36 *//** @file comx_video_base.h
37   This module contains the class definition for openMAX decoder component.
38 
39 *//*========================================================================*/
40 
41 //////////////////////////////////////////////////////////////////////////////
42 //                             Include Files
43 //////////////////////////////////////////////////////////////////////////////
44 
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <sys/mman.h>
48 #ifdef _ANDROID_
49 #ifdef _ANDROID_ICS_
50 #include "QComOMXMetadata.h"
51 #endif
52 #endif // _ANDROID_
53 #include <pthread.h>
54 #include <semaphore.h>
55 #include <media/hardware/HardwareAPI.h>
56 #include "vidc_common.h"
57 #include "OMX_Core.h"
58 #include "OMX_QCOMExtns.h"
59 #include "OMX_Skype_VideoExtensions.h"
60 #include "OMX_VideoExt.h"
61 #include "OMX_IndexExt.h"
62 #include "qc_omx_component.h"
63 #include "omx_video_common.h"
64 #include "extra_data_handler.h"
65 #include <linux/videodev2.h>
66 #include <dlfcn.h>
67 #include "C2DColorConverter.h"
68 #include "vidc_debug.h"
69 #include <vector>
70 #include "vidc_vendor_extensions.h"
71 
72 #include <linux/msm_ion.h>
73 #if TARGET_ION_ABI_VERSION >= 2
74 #include <ion/ion.h>
75 #include <linux/dma-buf.h>
76 #else
77 #include <linux/ion.h>
78 #endif
79 
80 #undef LOG_TAG
81 #define LOG_TAG "OMX-VENC"
82 
83 #ifdef _ANDROID_
84 using namespace android;
85 #include <utils/Log.h>
86 
87 #endif // _ANDROID_
88 
89 #ifdef USE_ION
90 static const char* MEM_DEVICE = "/dev/ion";
91 #define MEM_HEAP_ID ION_IOMMU_HEAP_ID
92 #endif
93 
94 //////////////////////////////////////////////////////////////////////////////
95 //                       Module specific globals
96 //////////////////////////////////////////////////////////////////////////////
97 #define OMX_SPEC_VERSION 0x00000101
98 #define OMX_INIT_STRUCT(_s_, _name_)            \
99     memset((_s_), 0x0, sizeof(_name_));          \
100 (_s_)->nSize = sizeof(_name_);               \
101 (_s_)->nVersion.nVersion = OMX_SPEC_VERSION
102 
103 //////////////////////////////////////////////////////////////////////////////
104 //               Macros
105 //////////////////////////////////////////////////////////////////////////////
106 #define PrintFrameHdr(bufHdr) DEBUG_PRINT("bufHdr %x buf %x size %d TS %d\n",\
107         (unsigned) bufHdr,\
108         (unsigned)((OMX_BUFFERHEADERTYPE *)bufHdr)->pBuffer,\
109         (unsigned)((OMX_BUFFERHEADERTYPE *)bufHdr)->nFilledLen,\
110         (unsigned)((OMX_BUFFERHEADERTYPE *)bufHdr)->nTimeStamp)
111 
112 /** STATUS CODES*/
113 /* Base value for status codes */
114 #define VEN_S_BASE	0x00000000
115 #define VEN_S_SUCCESS	(VEN_S_BASE)/* Success */
116 #define VEN_S_EFAIL	(VEN_S_BASE+1)/* General failure */
117 
118 /*Asynchronous messages from driver*/
119 #define VEN_MSG_INDICATION	0
120 #define VEN_MSG_INPUT_BUFFER_DONE	1
121 #define VEN_MSG_OUTPUT_BUFFER_DONE	2
122 #define VEN_MSG_NEED_OUTPUT_BUFFER	3
123 #define VEN_MSG_FLUSH_INPUT_DONE	4
124 #define VEN_MSG_FLUSH_OUPUT_DONE	5
125 #define VEN_MSG_START	6
126 #define VEN_MSG_STOP	7
127 #define VEN_MSG_PAUSE	8
128 #define VEN_MSG_RESUME	9
129 #define VEN_MSG_LTRUSE_FAILED	    10
130 #define VEN_MSG_HW_OVERLOAD	11
131 #define VEN_MSG_MAX_CLIENTS	12
132 
133 #define MAX_NUM_INPUT_BUFFERS 64
134 #define MAX_NUM_OUTPUT_BUFFERS 64
135 
136 #ifdef USE_NATIVE_HANDLE_SOURCE
137 #define LEGACY_CAM_SOURCE kMetadataBufferTypeNativeHandleSource
138 #define LEGACY_CAM_METADATA_TYPE encoder_nativehandle_buffer_type
139 #else
140 #define LEGACY_CAM_SOURCE kMetadataBufferTypeCameraSource
141 #define LEGACY_CAM_METADATA_TYPE encoder_media_buffer_type
142 #endif
143 
144 class omx_video;
145 void post_message(omx_video *omx, unsigned char id);
146 void* message_thread_enc(void *);
147 bool is_ubwc_interlaced(private_handle_t *handle);
148 
149 enum omx_venc_extradata_types {
150     VENC_EXTRADATA_SLICEINFO = 0x100,
151     VENC_EXTRADATA_LTRINFO = 0x200,
152     VENC_EXTRADATA_MBINFO = 0x400,
153     VENC_EXTRADATA_FRAMEDIMENSION = 0x1000000,
154     VENC_EXTRADATA_YUV_STATS = 0x800,
155     VENC_EXTRADATA_VQZIP = 0x02000000,
156     VENC_EXTRADATA_ROI = 0x04000000,
157 };
158 
159 struct output_metabuffer {
160     OMX_U32 type;
161     native_handle_t *nh;
162 };
163 
164 struct venc_buffer{
165  unsigned char *ptrbuffer;
166  unsigned long	sz;
167  unsigned long	len;
168  unsigned long	offset;
169  long long	timestamp;
170  unsigned long	flags;
171  void	*clientdata;
172 };
173 
174 struct venc_bufferpayload{
175 	unsigned char *pbuffer;
176 	size_t	sz;
177 	int	fd;
178 	unsigned int	offset;
179 	unsigned int	maped_size;
180 	unsigned long	filled_len;
181 };
182 
183 struct	venc_voptimingcfg{
184 	unsigned long	voptime_resolution;
185 };
186 
187 struct venc_framerate{
188 	unsigned long	fps_denominator;
189 	unsigned long	fps_numerator;
190 };
191 
192 struct venc_headerextension{
193 	 unsigned long	header_extension;
194 };
195 
196 struct venc_multiclicecfg{
197 	unsigned long	mslice_mode;
198 	unsigned long	mslice_size;
199 };
200 
201 struct venc_msg{
202 	unsigned long	statuscode;
203 	unsigned long	msgcode;
204 	struct venc_buffer	buf;
205 	unsigned long	msgdata_size;
206 };
207 
208 typedef struct encoder_meta_buffer_payload_type {
209     char data[sizeof(LEGACY_CAM_METADATA_TYPE) + sizeof(int)];
210 } encoder_meta_buffer_payload_type;
211 
212 // OMX video class
213 class omx_video: public qc_omx_component
214 {
215     protected:
216 #ifdef _ANDROID_ICS_
217         bool meta_mode_enable;
218         bool c2d_opened;
219         encoder_meta_buffer_payload_type meta_buffers[MAX_NUM_INPUT_BUFFERS];
220         OMX_BUFFERHEADERTYPE *opaque_buffer_hdr[MAX_NUM_INPUT_BUFFERS];
221         bool get_syntaxhdr_enable;
222         OMX_BUFFERHEADERTYPE  *psource_frame;
223         OMX_BUFFERHEADERTYPE  *pdest_frame;
224         bool secure_session;
225         bool hier_b_enabled;
226         bool m_ubwc_supported;
227         C2DColorConverter c2dcc;
228         ColorMapping mMapPixelFormat2Converter;
229 
230 #endif
231     public:
232 
233         bool mUseProxyColorFormat;
234         //RGB or non-native input, and we have pre-allocated conversion buffers
235         bool mUsesColorConversion;
236         ColorConvertFormat mC2dSrcFmt;
237         ColorConvertFormat  mC2dDestFmt;
238         OMX_U32 mC2DFrameHeight;
239         OMX_U32 mC2DFrameWidth;
240 
241         omx_video();  // constructor
242         virtual ~omx_video();  // destructor
243 
244         // virtual int async_message_process (void *context, void* message);
245         void process_event_cb(void *ctxt);
246 
247         OMX_ERRORTYPE allocate_buffer(
248                 OMX_HANDLETYPE hComp,
249                 OMX_BUFFERHEADERTYPE **bufferHdr,
250                 OMX_U32 port,
251                 OMX_PTR appData,
252                 OMX_U32 bytes
253                 );
254 
255 
256         virtual OMX_ERRORTYPE component_deinit(OMX_HANDLETYPE hComp)= 0;
257 
258         virtual OMX_ERRORTYPE component_init(OMX_STRING role)= 0;
259 
260         virtual OMX_U32 dev_stop(void) = 0;
261         virtual OMX_U32 dev_pause(void) = 0;
262         virtual OMX_U32 dev_start(void) = 0;
263         virtual OMX_U32 dev_flush(unsigned) = 0;
264         virtual OMX_U32 dev_resume(void) = 0;
265         virtual OMX_U32 dev_start_done(void) = 0;
266         virtual OMX_U32 dev_set_message_thread_id(pthread_t) = 0;
267         virtual bool dev_use_buf(unsigned) = 0;
268         virtual bool dev_handle_empty_eos_buffer(void) = 0;
269         virtual bool dev_free_buf(void *,unsigned) = 0;
270         virtual bool dev_empty_buf(void *, void *,unsigned,unsigned) = 0;
271         virtual bool dev_fill_buf(void *buffer, void *,unsigned,unsigned) = 0;
272         virtual bool dev_get_buf_req(OMX_U32 *,OMX_U32 *,OMX_U32 *,OMX_U32) = 0;
273         virtual bool dev_get_dimensions(OMX_U32 ,OMX_U32 *,OMX_U32 *) = 0;
274         virtual bool is_streamon_done(OMX_U32 port) = 0;
275         virtual bool dev_get_seq_hdr(void *, unsigned, unsigned *) = 0;
276         virtual bool dev_loaded_start(void) = 0;
277         virtual bool dev_loaded_stop(void) = 0;
278         virtual bool dev_loaded_start_done(void) = 0;
279         virtual bool dev_loaded_stop_done(void) = 0;
280         virtual bool is_secure_session(void) = 0;
281         virtual int dev_handle_output_extradata(void*, int) = 0;
282         virtual int dev_set_format(int) = 0;
283         virtual bool dev_query_cap(struct v4l2_queryctrl &) = 0;
284         virtual bool dev_is_video_session_supported(OMX_U32 width, OMX_U32 height) = 0;
285         virtual bool dev_get_capability_ltrcount(OMX_U32 *, OMX_U32 *, OMX_U32 *) = 0;
286         virtual bool dev_get_vui_timing_info(OMX_U32 *) = 0;
287         virtual bool dev_get_vqzip_sei_info(OMX_U32 *) = 0;
288         virtual bool dev_get_peak_bitrate(OMX_U32 *) = 0;
289         virtual bool dev_get_batch_size(OMX_U32 *) = 0;
290         virtual bool dev_buffer_ready_to_queue(OMX_BUFFERHEADERTYPE *buffer) = 0;
291         virtual bool dev_get_temporal_layer_caps(OMX_U32 * /*nMaxLayers*/,
292                 OMX_U32 * /*nMaxBLayers*/, OMX_VIDEO_ANDROID_TEMPORALLAYERINGPATTERNTYPE */*SupportedPattern*/) = 0;
293         virtual OMX_ERRORTYPE dev_get_supported_profile_level(OMX_VIDEO_PARAM_PROFILELEVELTYPE *) = 0;
294         virtual bool dev_get_supported_color_format(unsigned index, OMX_U32 *colorFormat) = 0;
295 #ifdef _ANDROID_ICS_
296         void omx_release_meta_buffer(OMX_BUFFERHEADERTYPE *buffer);
297 #endif
298         virtual bool dev_color_align(OMX_BUFFERHEADERTYPE *buffer, OMX_U32 width,
299                         OMX_U32 height) = 0;
300         virtual bool dev_get_output_log_flag() = 0;
301         virtual int dev_output_log_buffers(const char *buffer_addr, int buffer_len, uint64_t timestamp) = 0;
302         virtual int dev_extradata_log_buffers(char *buffer_addr, bool input) = 0;
303         virtual bool dev_get_hevc_profile(OMX_U32*) = 0;
304         virtual bool dev_handle_client_input_extradata(void*) = 0;
305         virtual void dev_get_color_format_as_string(char * buf, int buf_len, unsigned colorformat) = 0;
306         virtual void dev_get_consumer_usage(OMX_U32*) = 0;
307         OMX_ERRORTYPE component_role_enum(
308                 OMX_HANDLETYPE hComp,
309                 OMX_U8 *role,
310                 OMX_U32 index
311                 );
312 
313         OMX_ERRORTYPE component_tunnel_request(
314                 OMX_HANDLETYPE hComp,
315                 OMX_U32 port,
316                 OMX_HANDLETYPE  peerComponent,
317                 OMX_U32 peerPort,
318                 OMX_TUNNELSETUPTYPE *tunnelSetup
319                 );
320 
321         OMX_ERRORTYPE empty_this_buffer(
322                 OMX_HANDLETYPE hComp,
323                 OMX_BUFFERHEADERTYPE *buffer
324                 );
325 
326 
327 
328         OMX_ERRORTYPE fill_this_buffer(
329                 OMX_HANDLETYPE hComp,
330                 OMX_BUFFERHEADERTYPE *buffer
331                 );
332 
333 
334         OMX_ERRORTYPE free_buffer(
335                 OMX_HANDLETYPE hComp,
336                 OMX_U32 port,
337                 OMX_BUFFERHEADERTYPE *buffer
338                 );
339 
340         OMX_ERRORTYPE get_component_version(
341                 OMX_HANDLETYPE hComp,
342                 OMX_STRING componentName,
343                 OMX_VERSIONTYPE *componentVersion,
344                 OMX_VERSIONTYPE *specVersion,
345                 OMX_UUIDTYPE *componentUUID
346                 );
347 
348         OMX_ERRORTYPE get_config(
349                 OMX_HANDLETYPE hComp,
350                 OMX_INDEXTYPE configIndex,
351                 OMX_PTR configData
352                 );
353 
354         OMX_ERRORTYPE get_extension_index(
355                 OMX_HANDLETYPE hComp,
356                 OMX_STRING paramName,
357                 OMX_INDEXTYPE *indexType
358                 );
359 
360         OMX_ERRORTYPE get_parameter(OMX_HANDLETYPE hComp,
361                 OMX_INDEXTYPE  paramIndex,
362                 OMX_PTR        paramData);
363 
364         OMX_ERRORTYPE get_state(OMX_HANDLETYPE hComp,
365                 OMX_STATETYPE *state);
366 
367 
368 
369         OMX_ERRORTYPE send_command(OMX_HANDLETYPE  hComp,
370                 OMX_COMMANDTYPE cmd,
371                 OMX_U32         param1,
372                 OMX_PTR         cmdData);
373 
374 
375         OMX_ERRORTYPE set_callbacks(OMX_HANDLETYPE   hComp,
376                 OMX_CALLBACKTYPE *callbacks,
377                 OMX_PTR          appData);
378 
379         virtual OMX_ERRORTYPE set_config(OMX_HANDLETYPE hComp,
380                 OMX_INDEXTYPE  configIndex,
381                 OMX_PTR        configData) = 0;
382 
383         virtual OMX_ERRORTYPE set_parameter(OMX_HANDLETYPE hComp,
384                 OMX_INDEXTYPE  paramIndex,
385                 OMX_PTR        paramData) =0;
386 
387         OMX_ERRORTYPE use_buffer(OMX_HANDLETYPE      hComp,
388                 OMX_BUFFERHEADERTYPE **bufferHdr,
389                 OMX_U32              port,
390                 OMX_PTR              appData,
391                 OMX_U32              bytes,
392                 OMX_U8               *buffer);
393 
394 
395         OMX_ERRORTYPE use_EGL_image(OMX_HANDLETYPE     hComp,
396                 OMX_BUFFERHEADERTYPE **bufferHdr,
397                 OMX_U32              port,
398                 OMX_PTR              appData,
399                 void *               eglImage);
400 
401         Signal signal;
402 
403         bool reject_param_for_TME_mode(int index);
404         bool reject_config_for_TME_mode(int index);
405 
406         pthread_t msg_thread_id;
407         pthread_t async_thread_id;
408         bool async_thread_created;
409         bool msg_thread_created;
410         volatile bool msg_thread_stop;
411 
412         OMX_U8 m_nkind[128];
413 
414 
415         //int *input_pmem_fd;
416         //int *output_pmem_fd;
417         struct pmem *m_pInput_pmem;
418         struct pmem *m_pOutput_pmem;
419 #ifdef USE_ION
420         struct venc_ion *m_pInput_ion;
421         struct venc_ion *m_pOutput_ion;
422 #endif
423 
424 
425 
426     public:
427         // Bit Positions
428         enum flags_bit_positions {
429             // Defer transition to IDLE
430             OMX_COMPONENT_IDLE_PENDING            =0x1,
431             // Defer transition to LOADING
432             OMX_COMPONENT_LOADING_PENDING         =0x2,
433             // First  Buffer Pending
434             OMX_COMPONENT_FIRST_BUFFER_PENDING    =0x3,
435             // Second Buffer Pending
436             OMX_COMPONENT_SECOND_BUFFER_PENDING   =0x4,
437             // Defer transition to Enable
438             OMX_COMPONENT_INPUT_ENABLE_PENDING    =0x5,
439             // Defer transition to Enable
440             OMX_COMPONENT_OUTPUT_ENABLE_PENDING   =0x6,
441             // Defer transition to Disable
442             OMX_COMPONENT_INPUT_DISABLE_PENDING   =0x7,
443             // Defer transition to Disable
444             OMX_COMPONENT_OUTPUT_DISABLE_PENDING  =0x8,
445             //defer flush notification
446             OMX_COMPONENT_OUTPUT_FLUSH_PENDING    =0x9,
447             OMX_COMPONENT_INPUT_FLUSH_PENDING    =0xA,
448             OMX_COMPONENT_PAUSE_PENDING          =0xB,
449             OMX_COMPONENT_EXECUTE_PENDING        =0xC,
450             OMX_COMPONENT_LOADED_START_PENDING = 0xD,
451             OMX_COMPONENT_LOADED_STOP_PENDING = 0xF,
452 
453         };
454 
455         // Deferred callback identifiers
456         enum {
457             //Event Callbacks from the venc component thread context
458             OMX_COMPONENT_GENERATE_EVENT       = 0x1,
459             //Buffer Done callbacks from the venc component thread context
460             OMX_COMPONENT_GENERATE_BUFFER_DONE = 0x2,
461             //Frame Done callbacks from the venc component thread context
462             OMX_COMPONENT_GENERATE_FRAME_DONE  = 0x3,
463             //Buffer Done callbacks from the venc component thread context
464             OMX_COMPONENT_GENERATE_FTB         = 0x4,
465             //Frame Done callbacks from the venc component thread context
466             OMX_COMPONENT_GENERATE_ETB         = 0x5,
467             //Command
468             OMX_COMPONENT_GENERATE_COMMAND     = 0x6,
469             //Push-Pending Buffers
470             OMX_COMPONENT_PUSH_PENDING_BUFS    = 0x7,
471             // Empty Buffer Done callbacks
472             OMX_COMPONENT_GENERATE_EBD         = 0x8,
473             //Flush Event Callbacks from the venc component thread context
474             OMX_COMPONENT_GENERATE_EVENT_FLUSH       = 0x9,
475             OMX_COMPONENT_GENERATE_EVENT_INPUT_FLUSH = 0x0A,
476             OMX_COMPONENT_GENERATE_EVENT_OUTPUT_FLUSH = 0x0B,
477             OMX_COMPONENT_GENERATE_FBD = 0xc,
478             OMX_COMPONENT_GENERATE_START_DONE = 0xD,
479             OMX_COMPONENT_GENERATE_PAUSE_DONE = 0xE,
480             OMX_COMPONENT_GENERATE_RESUME_DONE = 0xF,
481             OMX_COMPONENT_GENERATE_STOP_DONE = 0x10,
482             OMX_COMPONENT_GENERATE_HARDWARE_ERROR = 0x11,
483             OMX_COMPONENT_GENERATE_LTRUSE_FAILED = 0x12,
484             OMX_COMPONENT_GENERATE_ETB_OPQ = 0x13,
485             OMX_COMPONENT_GENERATE_UNSUPPORTED_SETTING = 0x14,
486             OMX_COMPONENT_GENERATE_HARDWARE_OVERLOAD = 0x15,
487             OMX_COMPONENT_CLOSE_MSG = 0x16
488         };
489 
490         struct omx_event {
491             unsigned long param1;
492             unsigned long param2;
493             unsigned long id;
494         };
495 
496         struct omx_cmd_queue {
497             omx_event m_q[OMX_CORE_CONTROL_CMDQ_SIZE];
498             unsigned long m_read;
499             unsigned long m_write;
500             unsigned long m_size;
501 
502             omx_cmd_queue();
503             ~omx_cmd_queue();
504             bool insert_entry(unsigned long p1, unsigned long p2, unsigned long id);
505             bool pop_entry(unsigned long *p1,unsigned long *p2, unsigned long *id);
506             // get msgtype of the first ele from the queue
507             unsigned get_q_msg_type();
508 
509         };
510 
511         bool allocate_done(void);
512         bool allocate_input_done(void);
513         bool allocate_output_done(void);
514         bool allocate_input_extradata_done(void);
515         bool allocate_output_extradata_done(void);
516 
517         OMX_ERRORTYPE free_input_buffer(OMX_BUFFERHEADERTYPE *bufferHdr);
518         OMX_ERRORTYPE free_output_buffer(OMX_BUFFERHEADERTYPE *bufferHdr);
519         void free_input_extradata_buffer_header();
520         void free_output_extradata_buffer_header();
521 
522         OMX_ERRORTYPE allocate_client_input_extradata_headers();
523         OMX_ERRORTYPE allocate_client_output_extradata_headers();
524 
525         OMX_ERRORTYPE allocate_input_buffer(OMX_HANDLETYPE       hComp,
526                 OMX_BUFFERHEADERTYPE **bufferHdr,
527                 OMX_U32              port,
528                 OMX_PTR              appData,
529                 OMX_U32              bytes);
530 #ifdef _ANDROID_ICS_
531         OMX_ERRORTYPE allocate_input_meta_buffer(OMX_HANDLETYPE       hComp,
532                 OMX_BUFFERHEADERTYPE **bufferHdr,
533                 OMX_PTR              appData,
534                 OMX_U32              bytes);
535 #endif
536         OMX_ERRORTYPE allocate_output_buffer(OMX_HANDLETYPE       hComp,
537                 OMX_BUFFERHEADERTYPE **bufferHdr,
538                 OMX_U32 port,OMX_PTR appData,
539                 OMX_U32              bytes);
540 
541         OMX_ERRORTYPE use_input_buffer(OMX_HANDLETYPE hComp,
542                 OMX_BUFFERHEADERTYPE  **bufferHdr,
543                 OMX_U32               port,
544                 OMX_PTR               appData,
545                 OMX_U32               bytes,
546                 OMX_U8                *buffer);
547 
548         OMX_ERRORTYPE use_output_buffer(OMX_HANDLETYPE hComp,
549                 OMX_BUFFERHEADERTYPE   **bufferHdr,
550                 OMX_U32                port,
551                 OMX_PTR                appData,
552                 OMX_U32                bytes,
553                 OMX_U8                 *buffer);
554 
555         OMX_ERRORTYPE use_client_input_extradata_buffer(OMX_HANDLETYPE hComp,
556                 OMX_BUFFERHEADERTYPE   **bufferHdr,
557                 OMX_U32                port,
558                 OMX_PTR                appData,
559                 OMX_U32                bytes,
560                 OMX_U8                 *buffer);
561 
562         OMX_ERRORTYPE use_client_output_extradata_buffer(OMX_HANDLETYPE hComp,
563                 OMX_BUFFERHEADERTYPE   **bufferHdr,
564                 OMX_U32                port,
565                 OMX_PTR                appData,
566                 OMX_U32                bytes,
567                 OMX_U8                 *buffer);
568 
569         bool execute_omx_flush(OMX_U32);
570         bool execute_output_flush(void);
571         bool execute_input_flush(void);
572         bool execute_flush_all(void);
573         OMX_ERRORTYPE empty_buffer_done(OMX_HANDLETYPE hComp,
574                 OMX_BUFFERHEADERTYPE * buffer);
575 
576         OMX_ERRORTYPE fill_buffer_done(OMX_HANDLETYPE hComp,
577                 OMX_BUFFERHEADERTYPE * buffer);
578         OMX_ERRORTYPE empty_this_buffer_proxy(OMX_HANDLETYPE hComp,
579                 OMX_BUFFERHEADERTYPE *buffer);
580         OMX_ERRORTYPE empty_this_buffer_opaque(OMX_HANDLETYPE hComp,
581                 OMX_BUFFERHEADERTYPE *buffer);
582         OMX_ERRORTYPE push_input_buffer(OMX_HANDLETYPE hComp);
583         OMX_ERRORTYPE convert_queue_buffer(OMX_HANDLETYPE hComp,
584                 struct pmem &Input_pmem_info,unsigned long &index);
585         OMX_ERRORTYPE queue_meta_buffer(OMX_HANDLETYPE hComp);
586         OMX_ERRORTYPE fill_this_buffer_proxy(OMX_HANDLETYPE hComp,
587                 OMX_BUFFERHEADERTYPE *buffer);
588         bool release_done();
589         OMX_ERRORTYPE handle_empty_eos_buffer();
590 
591         bool release_output_done();
592         bool release_input_done();
593         bool release_input_extradata_done();
594         bool release_output_extradata_done();
595 
596         OMX_ERRORTYPE send_command_proxy(OMX_HANDLETYPE  hComp,
597                 OMX_COMMANDTYPE cmd,
598                 OMX_U32         param1,
599                 OMX_PTR         cmdData);
600         bool post_event( unsigned long p1,
601                 unsigned long p2,
602                 unsigned long id
603                    );
604 
omx_report_error()605         inline void omx_report_error () {
606             if (m_pCallbacks.EventHandler && !m_error_propogated && m_state != OMX_StateLoaded) {
607                 m_error_propogated = true;
608                 DEBUG_PRINT_ERROR("ERROR: send OMX_ErrorHardware to Client");
609                 m_pCallbacks.EventHandler(&m_cmp,m_app_data,
610                         OMX_EventError,OMX_ErrorHardware,0,NULL);
611             }
612         }
613 
omx_report_hw_overload()614         inline void omx_report_hw_overload ()
615         {
616             if (m_pCallbacks.EventHandler && !m_error_propogated && m_state != OMX_StateLoaded) {
617                 m_error_propogated = true;
618                 DEBUG_PRINT_ERROR("ERROR: send OMX_ErrorInsufficientResources to Client");
619                 m_pCallbacks.EventHandler(&m_cmp, m_app_data,
620                         OMX_EventError, OMX_ErrorInsufficientResources, 0, NULL);
621             }
622         }
623 
omx_report_unsupported_setting()624         inline void omx_report_unsupported_setting () {
625             if (m_pCallbacks.EventHandler && !m_error_propogated && m_state != OMX_StateLoaded) {
626                 m_error_propogated = true;
627                 m_pCallbacks.EventHandler(&m_cmp,m_app_data,
628                         OMX_EventError,OMX_ErrorUnsupportedSetting,0,NULL);
629             }
630         }
631 
632         client_extradata_info m_client_out_extradata_info;
633         client_extradata_info m_client_in_extradata_info;
634 
635         void complete_pending_buffer_done_cbs();
636         bool is_conv_needed(private_handle_t *handle);
637         void print_debug_color_aspects(ColorAspects *aspects, const char *prefix);
638 
639         OMX_ERRORTYPE get_vendor_extension_config(
640                 OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE *ext);
641         OMX_ERRORTYPE set_vendor_extension_config(
642                 OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE *ext);
643         void init_vendor_extensions(VendorExtensionStore&);
644         // Extensions-store is immutable after initialization (i.e cannot add/remove/change
645         //  extensions once added !)
646         const VendorExtensionStore mVendorExtensionStore;
647 
648         char *ion_map(int fd, int len);
649         OMX_ERRORTYPE ion_unmap(int fd, void *bufaddr, int len);
650 #ifdef USE_ION
651         bool alloc_map_ion_memory(int size, venc_ion *ion_info,
652                                  int flag);
653         void free_ion_memory(struct venc_ion *buf_ion_info);
654         void do_cache_operations(int fd);
655 #endif
656 
657         //*************************************************************
658         //*******************MEMBER VARIABLES *************************
659         //*************************************************************
660 
661         pthread_mutex_t       m_lock;
662         sem_t                 m_cmd_lock;
663         bool              m_error_propogated;
664 
665         //sem to handle the minimum procesing of commands
666 
667 
668         // compression format
669         //OMX_VIDEO_CODINGTYPE eCompressionFormat;
670         // OMX State
671         OMX_STATETYPE m_state;
672         // Application data
673         OMX_PTR m_app_data;
674         OMX_BOOL m_use_input_pmem;
675         OMX_BOOL m_use_output_pmem;
676         // Application callbacks
677         OMX_CALLBACKTYPE m_pCallbacks;
678         OMX_PORT_PARAM_TYPE m_sPortParam;
679         OMX_VIDEO_PARAM_PROFILELEVELTYPE m_sParamProfileLevel;
680         OMX_VIDEO_PARAM_PORTFORMATTYPE m_sInPortFormat;
681         OMX_VIDEO_PARAM_PORTFORMATTYPE m_sOutPortFormat;
682         OMX_PARAM_PORTDEFINITIONTYPE m_sInPortDef;
683         OMX_PARAM_PORTDEFINITIONTYPE m_sOutPortDef;
684         OMX_VIDEO_PARAM_MPEG4TYPE m_sParamMPEG4;
685         OMX_VIDEO_PARAM_H263TYPE m_sParamH263;
686         OMX_VIDEO_PARAM_AVCTYPE m_sParamAVC;
687         OMX_VIDEO_PARAM_VP8TYPE m_sParamVP8;
688         OMX_VIDEO_PARAM_ANDROID_VP8ENCODERTYPE m_sParamVP8Encoder;
689         OMX_VIDEO_PARAM_HEVCTYPE m_sParamHEVC;
690         QOMX_VIDEO_PARAM_TMETYPE m_sParamTME;
691         OMX_U32 tme_payload_version;
692         OMX_PORT_PARAM_TYPE m_sPortParam_img;
693         OMX_PORT_PARAM_TYPE m_sPortParam_audio;
694         OMX_VIDEO_CONFIG_BITRATETYPE m_sConfigBitrate;
695         OMX_CONFIG_FRAMERATETYPE m_sConfigFramerate;
696         OMX_VIDEO_PARAM_BITRATETYPE m_sParamBitrate;
697         OMX_PRIORITYMGMTTYPE m_sPriorityMgmt;
698         OMX_PARAM_BUFFERSUPPLIERTYPE m_sInBufSupplier;
699         OMX_PARAM_BUFFERSUPPLIERTYPE m_sOutBufSupplier;
700         OMX_CONFIG_ROTATIONTYPE m_sConfigFrameRotation;
701         OMX_CONFIG_MIRRORTYPE m_sConfigFrameMirror;
702         OMX_CONFIG_INTRAREFRESHVOPTYPE m_sConfigIntraRefreshVOP;
703         OMX_U32 m_QPSet;
704         OMX_VIDEO_PARAM_QUANTIZATIONTYPE m_sSessionQuantization;
705         OMX_QCOM_VIDEO_PARAM_IPB_QPRANGETYPE m_sSessionQPRange;
706         OMX_VIDEO_PARAM_AVCSLICEFMO m_sAVCSliceFMO;
707         QOMX_VIDEO_INTRAPERIODTYPE m_sIntraperiod;
708         OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE m_sErrorCorrection;
709         QOMX_VIDEO_PARAM_SLICE_SPACING_TYPE m_sSliceSpacing;
710         OMX_VIDEO_PARAM_INTRAREFRESHTYPE m_sIntraRefresh;
711         QOMX_VIDEO_PARAM_LTRCOUNT_TYPE m_sParamLTRCount;
712         QOMX_VIDEO_CONFIG_LTRUSE_TYPE m_sConfigLTRUse;
713         QOMX_VIDEO_CONFIG_LTRMARK_TYPE m_sConfigLTRMark;
714         OMX_VIDEO_CONFIG_AVCINTRAPERIOD m_sConfigAVCIDRPeriod;
715         OMX_VIDEO_CONFIG_DEINTERLACE m_sConfigDeinterlace;
716         OMX_VIDEO_VP8REFERENCEFRAMETYPE m_sConfigVp8ReferenceFrame;
717         QOMX_VIDEO_HIERARCHICALLAYERS m_sHierLayers;
718         QOMX_EXTNINDEX_VIDEO_HIER_P_LAYERS m_sHPlayers;
719         OMX_SKYPE_VIDEO_CONFIG_BASELAYERPID m_sBaseLayerID;
720         OMX_SKYPE_VIDEO_PARAM_DRIVERVER m_sDriverVer;
721         OMX_QCOM_VIDEO_CONFIG_QP m_sConfigQP;
722         QOMX_EXTNINDEX_VIDEO_VENC_SAR m_sSar;
723         QOMX_VIDEO_H264ENTROPYCODINGTYPE m_sParamEntropy;
724         QOMX_INDEXDOWNSCALAR m_sParamDownScalar;
725         PrependSPSPPSToIDRFramesParams m_sPrependSPSPPS;
726         struct timestamp_info {
727             OMX_S64 ts;
728             omx_cmd_queue deferred_inbufq;
729             pthread_mutex_t m_lock;
730         } m_TimeStampInfo;
731         OMX_U32 m_sExtraData;
732         OMX_U32 m_sParamConsumerUsage;
733         OMX_U32 m_input_msg_id;
734         OMX_VIDEO_CONFIG_ANDROID_INTRAREFRESHTYPE m_sConfigIntraRefresh;
735         OMX_QTI_VIDEO_CONFIG_BLURINFO       m_blurInfo;
736         DescribeColorAspectsParams m_sConfigColorAspects;
737         OMX_VIDEO_PARAM_ANDROID_TEMPORALLAYERINGTYPE m_sParamTemporalLayers;
738         OMX_VIDEO_CONFIG_ANDROID_TEMPORALLAYERINGTYPE m_sConfigTemporalLayers;
739         QOMX_ENABLETYPE m_sParamAVTimerTimestampMode;   // use VT-timestamps in gralloc-handle
740         QOMX_ENABLETYPE m_sParamControlInputQueue;
741         OMX_TIME_CONFIG_TIMESTAMPTYPE m_sConfigInputTrigTS;
742         QOMX_EXTNINDEX_VIDEO_LOW_LATENCY_MODE m_sParamLowLatency;
743         OMX_U32 m_nOperatingRate;
744         QOMX_ENABLETYPE m_sParamColorSpaceConversion;
745         OMX_VIDEO_PARAM_ANDROID_IMAGEGRIDTYPE m_sParamAndroidImageGrid;
746         QOMX_ENABLETYPE m_sParamLinearColorFormat;
747         QOMX_ENABLETYPE m_sParamNativeRecorder;
748 
749         // fill this buffer queue
750         omx_cmd_queue m_ftb_q;
751         // Command Q for rest of the events
752         omx_cmd_queue m_cmd_q;
753         omx_cmd_queue m_etb_q;
754         // Input memory pointer
755         OMX_BUFFERHEADERTYPE *m_inp_mem_ptr;
756         // Output memory pointer
757         OMX_BUFFERHEADERTYPE *m_out_mem_ptr;
758         // Client extradata memory pointer
759         OMX_BUFFERHEADERTYPE  *m_client_input_extradata_mem_ptr;
760         OMX_BUFFERHEADERTYPE  *m_client_output_extradata_mem_ptr;
761         omx_cmd_queue m_opq_meta_q;
762         omx_cmd_queue m_opq_pmem_q;
763         OMX_BUFFERHEADERTYPE meta_buffer_hdr[MAX_NUM_INPUT_BUFFERS];
764         pthread_mutex_t m_buf_lock;
765 
766         bool input_flush_progress;
767         bool output_flush_progress;
768         bool input_use_buffer;
769         bool output_use_buffer;
770         int pending_input_buffers;
771         int pending_output_buffers;
772 
773         bool allocate_native_handle;
774 
775         uint64_t m_out_bm_count;
776         uint64_t m_client_out_bm_count;
777         uint64_t m_client_in_bm_count;
778         uint64_t m_inp_bm_count;
779         // bitmask array size for extradata
780         uint64_t m_in_extradata_bm_count;
781         uint64_t m_out_extradata_bm_count;
782         uint64_t m_flags;
783         uint64_t m_etb_count;
784         uint64_t m_fbd_count;
785         OMX_TICKS m_etb_timestamp;
786         // to know whether Event Port Settings change has been triggered or not.
787         bool m_event_port_settings_sent;
788         OMX_U8                m_cRole[OMX_MAX_STRINGNAME_SIZE];
789         bool hw_overload;
790         size_t m_graphicbuffer_size;
791         char m_platform[OMX_MAX_STRINGNAME_SIZE];
792 
793         bool m_buffer_freed;
794         bool profile_mode;
795         int profile_frame_count;
796         OMX_U64 profile_start_time;
797         OMX_U64 profile_last_time;
798         bool profile_etb();
799 };
800 
801 #endif // __OMX_VIDEO_BASE_H__
802