• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*--------------------------------------------------------------------------
2 Copyright (c) 2013-2014, 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
6   are met:
7 
8     * Redistributions of source code must retain the above copyright
9   notice, this list of conditions and the following disclaimer.
10     * Redistributions in binary form must reproduce the above
11   copyright notice, this list of conditions and the following
12       disclaimer in the documentation and/or other materials provided
13       with the distribution.
14     * Neither the name of The Linux Foundation nor the names of its
15       contributors may be used to endorse or promote products derived
16       from this software without specific prior written permission.
17 
18 THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
19 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
21 ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
28 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 --------------------------------------------------------------------------*/
30 #ifndef __OMX_VDPP_H__
31 #define __OMX_VDPP_H__
32 /*============================================================================
33                             O p e n M A X   Component
34                                 Video Decoder
35 
36 *//** @file omx_vdpp.h
37   This module contains the class definition for openMAX video post-processing component.
38 
39 *//*========================================================================*/
40 
41 //////////////////////////////////////////////////////////////////////////////
42 //                             Include Files
43 //////////////////////////////////////////////////////////////////////////////
44 
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <string.h>
48 #include <inttypes.h>
49 #include <cstddef>
50 #include <math.h>
51 
52 #ifdef _ANDROID_
53 #define LOG_TAG "OMX-VDPP"
54 
55 #ifdef USE_ION
56 #include <linux/msm_ion.h>
57 #endif
58 #include <binder/MemoryHeapBase.h>
59 #include <ui/ANativeObjectBase.h>
60 extern "C"{
61 #include <utils/Log.h>
62 }
63 #include <linux/videodev2.h>
64 #include <poll.h>
65 #define TIMEOUT 5000
66 
67 
68 #define DEBUG_PRINT_LOW(x, ...) ALOGV("[Entry] "x, ##__VA_ARGS__)
69 #define DEBUG_PRINT_HIGH(x, ...) ALOGV("[Step] "x, ##__VA_ARGS__)
70 #define DEBUG_PRINT_ERROR(x, ...) ALOGE("[Error] "x, ##__VA_ARGS__)
71 
72 /*#define DEBUG_PRINT_LOW(x, ...)
73 #define DEBUG_PRINT_HIGH(x, ...)
74 #define DEBUG_PRINT_ERROR(x, ...) */
75 
76 #else //_ANDROID_
77 #define DEBUG_PRINT_LOW printf
78 #define DEBUG_PRINT_HIGH printf
79 #define DEBUG_PRINT_ERROR printf
80 #endif // _ANDROID_
81 
82 #if defined (_ANDROID_HONEYCOMB_) || defined (_ANDROID_ICS_)
83 #include <media/hardware/HardwareAPI.h>
84 #endif
85 
86 #include <unistd.h>
87 
88 #if defined (_ANDROID_ICS_)
89 #include <gralloc_priv.h>
90 #endif
91 
92 #include <pthread.h>
93 #ifndef PC_DEBUG
94 #include <semaphore.h>
95 #endif
96 #include "OMX_Core.h"
97 #include "OMX_QCOMExtns.h"
98 #include "OMX_CoreExt.h"
99 #include "OMX_IndexExt.h"
100 #include "qc_omx_component.h"
101 #include <linux/android_pmem.h>
102 #include <dlfcn.h>
103 
104 #include <media/msm_vpu.h>
105 extern "C" {
106   OMX_API void * get_omx_component_factory_fn(void); //used by qc_omx_core
107 }
108 
109 #ifdef _ANDROID_
110     using namespace android;
111 #endif // _ANDROID_
112 //////////////////////////////////////////////////////////////////////////////
113 //                       Module specific globals
114 //////////////////////////////////////////////////////////////////////////////
115 #define OMX_SPEC_VERSION  0x00000101
116 
117 // #define OUTPUT_BUFFER_LOG 1
118 // #define INPUT_BUFFER_LOG  1
119 //////////////////////////////////////////////////////////////////////////////
120 //               Macros
121 //////////////////////////////////////////////////////////////////////////////
122 #define PrintFrameHdr(bufHdr) DEBUG_PRINT("bufHdr %x buf %x size %d TS %d\n",\
123                        (unsigned) bufHdr,\
124                        (unsigned)((OMX_BUFFERHEADERTYPE *)bufHdr)->pBuffer,\
125                        (unsigned)((OMX_BUFFERHEADERTYPE *)bufHdr)->nFilledLen,\
126                        (unsigned)((OMX_BUFFERHEADERTYPE *)bufHdr)->nTimeStamp)
127 
128 // BitMask Management logic
129 #define BITS_PER_BYTE        32
130 #define BITMASK_SIZE(mIndex) (((mIndex) + BITS_PER_BYTE - 1)/BITS_PER_BYTE)
131 #define BITMASK_OFFSET(mIndex) ((mIndex)/BITS_PER_BYTE)
132 #define BITMASK_FLAG(mIndex) (1 << ((mIndex) % BITS_PER_BYTE))
133 #define BITMASK_CLEAR(mArray,mIndex) (mArray)[BITMASK_OFFSET(mIndex)] \
134         &=  ~(BITMASK_FLAG(mIndex))
135 #define BITMASK_SET(mArray,mIndex)  (mArray)[BITMASK_OFFSET(mIndex)] \
136         |=  BITMASK_FLAG(mIndex)
137 #define BITMASK_PRESENT(mArray,mIndex) ((mArray)[BITMASK_OFFSET(mIndex)] \
138         & BITMASK_FLAG(mIndex))
139 #define BITMASK_ABSENT(mArray,mIndex) (((mArray)[BITMASK_OFFSET(mIndex)] \
140         & BITMASK_FLAG(mIndex)) == 0x0)
141 #define BITMASK_PRESENT(mArray,mIndex) ((mArray)[BITMASK_OFFSET(mIndex)] \
142         & BITMASK_FLAG(mIndex))
143 #define BITMASK_ABSENT(mArray,mIndex) (((mArray)[BITMASK_OFFSET(mIndex)] \
144         & BITMASK_FLAG(mIndex)) == 0x0)
145 
146 #define OMX_CORE_CONTROL_CMDQ_SIZE   200//100
147 #define OMX_CORE_QCIF_HEIGHT         144
148 #define OMX_CORE_QCIF_WIDTH          176
149 #define OMX_CORE_VGA_HEIGHT          480
150 #define OMX_CORE_VGA_WIDTH           640
151 #define OMX_CORE_WVGA_HEIGHT         480
152 #define OMX_CORE_WVGA_WIDTH          800
153 
154 #ifdef _ANDROID_
155 #define MAX_NUM_INPUT_OUTPUT_BUFFERS 32
156 #endif
157 
158 #define OMX_FRAMEINFO_EXTRADATA 0x00010000
159 #define OMX_INTERLACE_EXTRADATA 0x00020000
160 #define OMX_TIMEINFO_EXTRADATA  0x00040000
161 #define OMX_PORTDEF_EXTRADATA   0x00080000
162 #define OMX_EXTNUSER_EXTRADATA  0x00100000
163 #define DRIVER_EXTRADATA_MASK   0x0000FFFF
164 
165 #define OMX_INTERLACE_EXTRADATA_SIZE ((sizeof(OMX_OTHER_EXTRADATATYPE) +\
166                                        sizeof(OMX_STREAMINTERLACEFORMAT) + 3)&(~3))
167 #define OMX_FRAMEINFO_EXTRADATA_SIZE ((sizeof(OMX_OTHER_EXTRADATATYPE) +\
168                                        sizeof(OMX_QCOM_EXTRADATA_FRAMEINFO) + 3)&(~3))
169 #define OMX_PORTDEF_EXTRADATA_SIZE ((sizeof(OMX_OTHER_EXTRADATATYPE) +\
170                                        sizeof(OMX_PARAM_PORTDEFINITIONTYPE) + 3)&(~3))
171 
172 #define VALID_TS(ts)      ((ts < LLONG_MAX)? true : false)
173 
174 /* STATUS CODES */
175 /* Base value for status codes */
176 #define VDPP_S_BASE	0x40000000
177 /* Success */
178 #define VDPP_S_SUCCESS	(VDPP_S_BASE)
179 /* General failure */
180 #define VDPP_S_EFAIL	(VDPP_S_BASE + 1)
181 /* Fatal irrecoverable  failure. Need to  tear down session. */
182 #define VDPP_S_EFATAL   (VDPP_S_BASE + 2)
183 /* Error detected in the passed  parameters */
184 #define VDPP_S_EBADPARAM	(VDPP_S_BASE + 3)
185 /* Command called in invalid  state. */
186 #define VDPP_S_EINVALSTATE	(VDPP_S_BASE + 4)
187  /* Insufficient OS  resources - thread, memory etc. */
188 #define VDPP_S_ENOSWRES	(VDPP_S_BASE + 5)
189  /* Insufficient HW resources -  core capacity  maxed  out. */
190 #define VDPP_S_ENOHWRES	(VDPP_S_BASE + 6)
191 /* Invalid command  called */
192 #define VDPP_S_EINVALCMD	(VDPP_S_BASE + 7)
193 /* Command timeout. */
194 #define VDPP_S_ETIMEOUT	(VDPP_S_BASE + 8)
195 /* Pre-requirement is  not met for API. */
196 #define VDPP_S_ENOPREREQ	(VDPP_S_BASE + 9)
197 /* Command queue is full. */
198 #define VDPP_S_ECMDQFULL	(VDPP_S_BASE + 10)
199 /* Command is not supported  by this driver */
200 #define VDPP_S_ENOTSUPP	(VDPP_S_BASE + 11)
201 /* Command is not implemented by thedriver. */
202 #define VDPP_S_ENOTIMPL	(VDPP_S_BASE + 12)
203 /* Command is not implemented by the driver.  */
204 #define VDPP_S_BUSY	(VDPP_S_BASE + 13)
205 #define VDPP_S_INPUT_BITSTREAM_ERR (VDPP_S_BASE + 14)
206 
207 #define VDPP_INTF_VER	1
208 #define VDPP_MSG_BASE	0x0000000
209 /* Codes to identify asynchronous message responses and events that driver
210   wants to communicate to the app.*/
211 #define VDPP_MSG_INVALID	(VDPP_MSG_BASE + 0)
212 #define VDPP_MSG_RESP_INPUT_BUFFER_DONE	(VDPP_MSG_BASE + 1)
213 #define VDPP_MSG_RESP_OUTPUT_BUFFER_DONE	(VDPP_MSG_BASE + 2)
214 #define VDPP_MSG_RESP_INPUT_FLUSHED	(VDPP_MSG_BASE + 3)
215 #define VDPP_MSG_RESP_OUTPUT_FLUSHED	(VDPP_MSG_BASE + 4)
216 #define VDPP_MSG_RESP_FLUSH_INPUT_DONE	(VDPP_MSG_BASE + 5)
217 #define VDPP_MSG_RESP_FLUSH_OUTPUT_DONE	(VDPP_MSG_BASE + 6)
218 #define VDPP_MSG_RESP_START_DONE	(VDPP_MSG_BASE + 7)
219 #define VDPP_MSG_RESP_STOP_DONE	(VDPP_MSG_BASE + 8)
220 #define VDPP_MSG_RESP_PAUSE_DONE	(VDPP_MSG_BASE + 9)
221 #define VDPP_MSG_RESP_RESUME_DONE	(VDPP_MSG_BASE + 10)
222 #define VDPP_MSG_RESP_RESOURCE_LOADED	(VDPP_MSG_BASE + 11)
223 #define VDPP_EVT_RESOURCES_LOST	(VDPP_MSG_BASE + 12)
224 #define VDPP_MSG_EVT_CONFIG_CHANGED	(VDPP_MSG_BASE + 13)
225 #define VDPP_MSG_EVT_HW_ERROR	(VDPP_MSG_BASE + 14)
226 #define VDPP_MSG_EVT_INFO_CONFIG_CHANGED	(VDPP_MSG_BASE + 15)
227 #define VDPP_MSG_EVT_INFO_FIELD_DROPPED	(VDPP_MSG_BASE + 16)
228 
229 #define VDPP_MSG_EVT_ACTIVE_REGION_DETECTION_STATUS	(VDPP_MSG_BASE + 17)
230 
231 
232 #define VP_INPUT_BUFFER_COUNT_INTERLACE 4
233 #define VP_OUTPUT_BUFFER_COUNT 4
234 
235 #define VDPP_SESSION 1
236 //#define STUB_VPU 1
237 //#define DEFAULT_EXTRADATA (OMX_FRAMEINFO_EXTRADATA|OMX_INTERLACE_EXTRADATA)
238 
239 enum port_indexes
240 {
241     OMX_CORE_INPUT_PORT_INDEX        =0,
242     OMX_CORE_OUTPUT_PORT_INDEX       =1
243 };
244 #ifdef USE_ION
245 struct vdpp_ion
246 {
247     int ion_device_fd;
248     struct ion_fd_data fd_ion_data;
249     struct ion_allocation_data ion_alloc_data;
250 };
251 #endif
252 
253 struct extradata_buffer_info {
254 	int buffer_size;
255 	char* uaddr;
256 	int count;
257 	int size;
258 #ifdef USE_ION
259 	struct vdpp_ion ion;
260 #endif
261 };
262 
263 struct vdpp_picsize {
264 	uint32_t frame_width;
265 	uint32_t frame_height;
266 	uint32_t stride;
267 	uint32_t scan_lines;
268 };
269 
270 enum vdpp_interlaced_format {
271 	VDPP_InterlaceFrameProgressive = 0x1,
272 	VDPP_InterlaceInterleaveFrameTopFieldFirst = 0x2,
273 	VDPP_InterlaceInterleaveFrameBottomFieldFirst = 0x4
274 };
275 
276 enum vdpp_buffer {
277 	VDPP_BUFFER_TYPE_INPUT,
278 	VDPP_BUFFER_TYPE_OUTPUT
279 };
280 
281 struct vdpp_allocatorproperty {
282 	enum vdpp_buffer buffer_type;
283 	uint32_t mincount;
284 	uint32_t maxcount;
285 	uint32_t actualcount;
286 	size_t buffer_size;
287 	uint32_t alignment;
288 	uint32_t buf_poolid;
289 	size_t frame_size;
290 };
291 
292 struct vdpp_bufferpayload {
293 	void *bufferaddr;
294 	size_t buffer_len;
295 	int pmem_fd;
296 	size_t offset;
297 	size_t mmaped_size;
298 };
299 
300 struct vdpp_framesize {
301 	uint32_t   left;
302 	uint32_t   top;
303 	uint32_t   right;
304 	uint32_t   bottom;
305 };
306 
307 struct vdpp_setbuffer_cmd {
308 	enum vdpp_buffer buffer_type;
309 	struct vdpp_bufferpayload buffer;
310 };
311 
312 struct vdpp_output_frameinfo {
313 	void *bufferaddr;
314 	size_t offset;
315 	size_t len;
316 	uint32_t flags;
317 	int64_t time_stamp;
318 	void *client_data;
319 	struct vdpp_framesize framesize;
320 
321 };
322 
323 union vdpp_msgdata {
324     struct v4l2_rect ar_result;
325 	struct vdpp_output_frameinfo output_frame;
326 	void *input_frame_clientdata;
327 };
328 
329 struct vdpp_msginfo {
330 	uint32_t status_code;
331 	uint32_t msgcode;
332 	union vdpp_msgdata msgdata;
333 	size_t msgdatasize;
334 };
335 
336 struct vdpp_framerate {
337 	unsigned long fps_denominator;
338 	unsigned long fps_numerator;
339 };
340 
341 #ifdef STUB_VPU
342 typedef struct vcap_etb_ftb_info
343 {
344     int etb_cnt; // only simulate 1-to-1
345     __u32	etb_index;
346 	size_t  etb_len;
347     int ftb_cnt;
348     __u32   ftb_index;
349 	size_t  ftb_len;
350 }vcap_etb_ftb_info;
351 #endif
352 
353 struct video_vpu_context
354 {
355     int video_vpu_fd;
356     uint32_t output_format;
357     enum   v4l2_field interlace;
358     struct vdpp_picsize video_resolution_input;
359     struct vdpp_picsize video_resolution_output;
360     struct vdpp_allocatorproperty ip_buf;
361     struct vdpp_allocatorproperty op_buf;
362     struct vdpp_bufferpayload *ptr_inputbuffer;
363     struct vdpp_bufferpayload *ptr_outputbuffer;
364     struct vdpp_output_frameinfo *ptr_respbuffer;
365 #ifdef USE_ION
366     struct vdpp_ion *ip_buf_ion_info;
367     struct vdpp_ion *op_buf_ion_info;
368 #endif
369     struct vdpp_framerate frame_rate;
370     bool timestamp_adjust;
371     char kind[128];
372 	struct extradata_buffer_info extradata_info;
373     int input_num_planes;
374     int output_num_planes;
375     double input_bytesperpixel[2];
376     double output_bytesperpixel[2];
377     int sessionsSupported;
378     int sessionAttached;
379 #ifdef STUB_VPU
380     bool thread_exit;
381     vcap_etb_ftb_info etb_ftb_info;
382     sem_t             async_lock;
383 #endif
384 };
385 
386 typedef struct _VdppExtensionData_t
387 {
388     bool                           activeRegionDetectionDirtyFlag;
389     QOMX_ACTIVEREGIONDETECTIONTYPE activeRegionDetection;
390     bool                                  activeRegionDetectionStatusDirtyFlag;
391     QOMX_ACTIVEREGIONDETECTION_STATUSTYPE activeRegionDetectionStatus;
392     bool                  scalingModeDirtyFlag;
393     QOMX_SCALINGMODETYPE  scalingMode;
394     bool                    noiseReductionDirtyFlag;
395     QOMX_NOISEREDUCTIONTYPE noiseReduction;
396     bool                      imageEnhancementDirtyFlag;
397     QOMX_IMAGEENHANCEMENTTYPE imageEnhancement;
398 } VdppExtensionData_t;
399 
400 // OMX video decoder class
401 class omx_vdpp: public qc_omx_component
402 {
403 
404 public:
405     omx_vdpp();  // constructor
406     virtual ~omx_vdpp();  // destructor
407 
408     static int async_message_process (void *context, void* message);
409     static void process_event_cb(void *ctxt,unsigned char id);
410     struct omx_event
411     {
412         unsigned param1;
413         unsigned param2;
414         unsigned id;
415     };
416     struct omx_cmd_queue
417     {
418         omx_event m_q[OMX_CORE_CONTROL_CMDQ_SIZE];
419         unsigned m_read;
420         unsigned m_write;
421         unsigned m_size;
422 
423         omx_cmd_queue();
424         ~omx_cmd_queue();
425         bool insert_entry(unsigned p1, unsigned p2, unsigned id);
426         bool pop_entry(unsigned *p1,unsigned *p2, unsigned *id);
427         // get msgtype of the first ele from the queue
428         unsigned get_q_msg_type();
429 
430     };
431 
432     OMX_ERRORTYPE allocate_buffer(
433                                    OMX_HANDLETYPE hComp,
434                                    OMX_BUFFERHEADERTYPE **bufferHdr,
435                                    OMX_U32 port,
436                                    OMX_PTR appData,
437                                    OMX_U32 bytes
438                                   );
439 
440 
441     OMX_ERRORTYPE component_deinit(OMX_HANDLETYPE hComp);
442 
443     OMX_ERRORTYPE component_init(OMX_STRING role);
444 
445     OMX_ERRORTYPE component_role_enum(
446                                        OMX_HANDLETYPE hComp,
447                                        OMX_U8 *role,
448                                        OMX_U32 index
449                                       );
450 
451     OMX_ERRORTYPE component_tunnel_request(
452                                             OMX_HANDLETYPE hComp,
453                                             OMX_U32 port,
454                                             OMX_HANDLETYPE  peerComponent,
455                                             OMX_U32 peerPort,
456                                             OMX_TUNNELSETUPTYPE *tunnelSetup
457                                            );
458 
459     OMX_ERRORTYPE empty_this_buffer(
460                                      OMX_HANDLETYPE hComp,
461                                      OMX_BUFFERHEADERTYPE *buffer
462                                     );
463 
464 
465 
466     OMX_ERRORTYPE fill_this_buffer(
467                                     OMX_HANDLETYPE hComp,
468                                     OMX_BUFFERHEADERTYPE *buffer
469                                    );
470 
471 
472     OMX_ERRORTYPE free_buffer(
473                               OMX_HANDLETYPE hComp,
474                               OMX_U32 port,
475                               OMX_BUFFERHEADERTYPE *buffer
476                               );
477 
478     OMX_ERRORTYPE get_component_version(
479                                         OMX_HANDLETYPE hComp,
480                                         OMX_STRING componentName,
481                                         OMX_VERSIONTYPE *componentVersion,
482                                         OMX_VERSIONTYPE *specVersion,
483                                         OMX_UUIDTYPE *componentUUID
484                                         );
485 
486     OMX_ERRORTYPE get_config(
487                               OMX_HANDLETYPE hComp,
488                               OMX_INDEXTYPE configIndex,
489                               OMX_PTR configData
490                              );
491 
492     OMX_ERRORTYPE get_extension_index(
493                                       OMX_HANDLETYPE hComp,
494                                       OMX_STRING paramName,
495                                       OMX_INDEXTYPE *indexType
496                                       );
497 
498     OMX_ERRORTYPE get_parameter(OMX_HANDLETYPE hComp,
499                                 OMX_INDEXTYPE  paramIndex,
500                                 OMX_PTR        paramData);
501 
502     OMX_ERRORTYPE get_state(OMX_HANDLETYPE hComp,
503                             OMX_STATETYPE *state);
504 
505 
506 
507     OMX_ERRORTYPE send_command(OMX_HANDLETYPE  hComp,
508                                OMX_COMMANDTYPE cmd,
509                                OMX_U32         param1,
510                                OMX_PTR         cmdData);
511 
512 
513     OMX_ERRORTYPE set_callbacks(OMX_HANDLETYPE   hComp,
514                                 OMX_CALLBACKTYPE *callbacks,
515                                 OMX_PTR          appData);
516 
517     OMX_ERRORTYPE set_config(OMX_HANDLETYPE hComp,
518                              OMX_INDEXTYPE  configIndex,
519                              OMX_PTR        configData);
520 
521     OMX_ERRORTYPE set_parameter(OMX_HANDLETYPE hComp,
522                                 OMX_INDEXTYPE  paramIndex,
523                                 OMX_PTR        paramData);
524 
525     OMX_ERRORTYPE use_buffer(OMX_HANDLETYPE      hComp,
526                              OMX_BUFFERHEADERTYPE **bufferHdr,
527                              OMX_U32              port,
528                              OMX_PTR              appData,
529                              OMX_U32              bytes,
530                              OMX_U8               *buffer);
531 
532     OMX_ERRORTYPE  use_input_heap_buffers(
533                           OMX_HANDLETYPE            hComp,
534                           OMX_BUFFERHEADERTYPE** bufferHdr,
535                           OMX_U32                   port,
536                           OMX_PTR                   appData,
537                           OMX_U32                   bytes,
538                           OMX_U8*                   buffer);
539 
540     OMX_ERRORTYPE  use_input_buffers(
541                           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_EGL_image(OMX_HANDLETYPE     hComp,
549                                 OMX_BUFFERHEADERTYPE **bufferHdr,
550                                 OMX_U32              port,
551                                 OMX_PTR              appData,
552                                 void *               eglImage);
553 
554     void complete_pending_buffer_done_cbs();
555     struct video_vpu_context drv_ctx;
556     int update_resolution(uint32_t width, uint32_t height, uint32_t stride, uint32_t scan_lines);
557 
558     int  m_pipe_in;
559     int  m_pipe_out;
560     int  m_ctrl_in;
561     int  m_ctrl_out;
562 
563     pthread_t msg_thread_id;
564     pthread_t async_thread_id;
565     omx_cmd_queue m_index_q_ftb;
566     omx_cmd_queue m_index_q_etb;
567     bool m_ar_callback_setup;
568 private:
569     // Bit Positions
570     enum flags_bit_positions
571     {
572         // Defer transition to IDLE
573         OMX_COMPONENT_IDLE_PENDING            =0x1,
574         // Defer transition to LOADING
575         OMX_COMPONENT_LOADING_PENDING         =0x2,
576         // First  Buffer Pending
577         OMX_COMPONENT_FIRST_BUFFER_PENDING    =0x3,
578         // Second Buffer Pending
579         OMX_COMPONENT_SECOND_BUFFER_PENDING   =0x4,
580         // Defer transition to Enable
581         OMX_COMPONENT_INPUT_ENABLE_PENDING    =0x5,
582         // Defer transition to Enable
583         OMX_COMPONENT_OUTPUT_ENABLE_PENDING   =0x6,
584         // Defer transition to Disable
585         OMX_COMPONENT_INPUT_DISABLE_PENDING   =0x7,
586         // Defer transition to Disable
587         OMX_COMPONENT_OUTPUT_DISABLE_PENDING  =0x8,
588         //defer flush notification
589         OMX_COMPONENT_OUTPUT_FLUSH_PENDING    =0x9,
590         OMX_COMPONENT_INPUT_FLUSH_PENDING    =0xA,
591         OMX_COMPONENT_PAUSE_PENDING          =0xB,
592         OMX_COMPONENT_EXECUTE_PENDING        =0xC,
593         OMX_COMPONENT_OUTPUT_FLUSH_IN_DISABLE_PENDING =0xD,
594         OMX_COMPONENT_DISABLE_OUTPUT_DEFERRED=0xE
595     };
596 
597     // Deferred callback identifiers
598     enum
599     {
600         //Event Callbacks from the vidpp component thread context
601         OMX_COMPONENT_GENERATE_EVENT       = 0x1,
602         //Buffer Done callbacks from the vidpp component thread context
603         OMX_COMPONENT_GENERATE_BUFFER_DONE = 0x2,
604         //Frame Done callbacks from the vidpp component thread context
605         OMX_COMPONENT_GENERATE_FRAME_DONE  = 0x3,
606         //Buffer Done callbacks from the vidpp component thread context
607         OMX_COMPONENT_GENERATE_FTB         = 0x4,
608         //Frame Done callbacks from the vidpp component thread context
609         OMX_COMPONENT_GENERATE_ETB         = 0x5,
610         //Command
611         OMX_COMPONENT_GENERATE_COMMAND     = 0x6,
612         //Push-Pending Buffers
613         OMX_COMPONENT_PUSH_PENDING_BUFS    = 0x7,
614         // Empty Buffer Done callbacks
615         OMX_COMPONENT_GENERATE_EBD         = 0x8,
616         //Flush Event Callbacks from the vidpp component thread context
617         OMX_COMPONENT_GENERATE_EVENT_FLUSH       = 0x9,
618         OMX_COMPONENT_GENERATE_EVENT_INPUT_FLUSH = 0x0A,
619         OMX_COMPONENT_GENERATE_EVENT_OUTPUT_FLUSH = 0x0B,
620         OMX_COMPONENT_GENERATE_FBD = 0xc,
621         OMX_COMPONENT_GENERATE_START_DONE = 0xD,
622         OMX_COMPONENT_GENERATE_PAUSE_DONE = 0xE,
623         OMX_COMPONENT_GENERATE_RESUME_DONE = 0xF,
624         OMX_COMPONENT_GENERATE_STOP_DONE = 0x10,
625         OMX_COMPONENT_GENERATE_HARDWARE_ERROR = 0x11,
626         OMX_COMPONENT_GENERATE_ETB_ARBITRARY = 0x12,
627         OMX_COMPONENT_GENERATE_PORT_RECONFIG = 0x13,
628         OMX_COMPONENT_GENERATE_EOS_DONE = 0x14,
629         OMX_COMPONENT_GENERATE_INFO_PORT_RECONFIG = 0x15,
630         OMX_COMPONENT_GENERATE_INFO_FIELD_DROPPED = 0x16,
631         OMX_COMPONENT_GENERATE_UNSUPPORTED_SETTING = 0x17,
632 
633         // extensions
634         OMX_COMPONENT_GENERATE_ACTIVE_REGION_DETECTION_STATUS = 0x18,
635     };
636 
637     enum v4l2_ports
638     {
639         CAPTURE_PORT,
640         OUTPUT_PORT,
641         MAX_PORT
642     };
643 
644 #ifdef _ANDROID_
645     struct ts_entry
646     {
647         OMX_TICKS timestamp;
648         bool valid;
649     };
650 
651     struct ts_arr_list
652     {
653         ts_entry m_ts_arr_list[MAX_NUM_INPUT_OUTPUT_BUFFERS];
654 
655         ts_arr_list();
656         ~ts_arr_list();
657 
658         bool insert_ts(OMX_TICKS ts);
659         bool pop_min_ts(OMX_TICKS &ts);
660         bool reset_ts_list();
661     };
662 #endif
663 
664     bool allocate_done(void);
665     bool allocate_input_done(void);
666     bool allocate_output_done(void);
667 
668     OMX_ERRORTYPE free_input_buffer(OMX_BUFFERHEADERTYPE *bufferHdr);
669     OMX_ERRORTYPE free_output_buffer(OMX_BUFFERHEADERTYPE *bufferHdr);
670     void free_output_buffer_header();
671     void free_input_buffer_header();
672 
673     OMX_ERRORTYPE allocate_input_heap_buffer(OMX_HANDLETYPE       hComp,
674                                              OMX_BUFFERHEADERTYPE **bufferHdr,
675                                              OMX_U32              port,
676                                              OMX_PTR              appData,
677                                              OMX_U32              bytes);
678 
679     OMX_ERRORTYPE allocate_input_buffer(OMX_HANDLETYPE       hComp,
680                                         OMX_BUFFERHEADERTYPE **bufferHdr,
681                                         OMX_U32              port,
682                                         OMX_PTR              appData,
683                                         OMX_U32              bytes);
684 
685     OMX_ERRORTYPE allocate_output_buffer(OMX_HANDLETYPE       hComp,
686                                          OMX_BUFFERHEADERTYPE **bufferHdr,
687                                          OMX_U32 port,OMX_PTR appData,
688                                          OMX_U32              bytes);
689     OMX_ERRORTYPE use_output_buffer(OMX_HANDLETYPE hComp,
690                                    OMX_BUFFERHEADERTYPE   **bufferHdr,
691                                    OMX_U32                port,
692                                    OMX_PTR                appData,
693                                    OMX_U32                bytes,
694                                    OMX_U8                 *buffer);
695 
696     OMX_ERRORTYPE allocate_output_headers();
697     bool execute_omx_flush(OMX_U32);
698     bool execute_output_flush();
699     bool execute_input_flush();
700     OMX_ERRORTYPE empty_buffer_done(OMX_HANDLETYPE hComp,
701                                     OMX_BUFFERHEADERTYPE * buffer);
702 
703     OMX_ERRORTYPE fill_buffer_done(OMX_HANDLETYPE hComp,
704                                     OMX_BUFFERHEADERTYPE * buffer);
705     OMX_ERRORTYPE empty_this_buffer_proxy(OMX_HANDLETYPE       hComp,
706                                         OMX_BUFFERHEADERTYPE *buffer);
707 
708     OMX_ERRORTYPE fill_this_buffer_proxy(OMX_HANDLETYPE       hComp,
709                                        OMX_BUFFERHEADERTYPE *buffer);
710     bool release_done();
711 
712     bool release_output_done();
713     bool release_input_done();
714     OMX_ERRORTYPE get_buffer_req(vdpp_allocatorproperty *buffer_prop);
715     OMX_ERRORTYPE set_buffer_req(vdpp_allocatorproperty *buffer_prop);
716     OMX_ERRORTYPE start_port_reconfig();
717 
718     int stream_off(OMX_U32 port);
719     void adjust_timestamp(OMX_S64 &act_timestamp);
720     void set_frame_rate(OMX_S64 act_timestamp);
721     OMX_ERRORTYPE enable_extradata(OMX_U32 requested_extradata, bool enable = true);
722     OMX_ERRORTYPE update_portdef(OMX_PARAM_PORTDEFINITIONTYPE *portDefn);
723     bool align_pmem_buffers(int pmem_fd, OMX_U32 buffer_size,
724                             OMX_U32 alignment);
725 #ifdef USE_ION
726     int alloc_map_ion_memory(OMX_U32 buffer_size,
727               OMX_U32 alignment, struct ion_allocation_data *alloc_data,
728               struct ion_fd_data *fd_data,int flag);
729     void free_ion_memory(struct vdpp_ion *buf_ion_info);
730 #endif
731 
732 
733     OMX_ERRORTYPE send_command_proxy(OMX_HANDLETYPE  hComp,
734                                      OMX_COMMANDTYPE cmd,
735                                      OMX_U32         param1,
736                                      OMX_PTR         cmdData);
737     bool post_event( unsigned int p1,
738                      unsigned int p2,
739                      unsigned int id
740                     );
741 
742     void setFormatParams(int pixelFormat, double bytesperpixel[], unsigned char *planesCount);
743 
744     int openInput(const char* inputName);
745 
746     /**
747       * int clip2 - return an integer value in 2 to the nth power
748       * legacy code from video decoder  (rounded up till 256)
749       *
750       */
clip2(int x)751     inline int clip2(int x)
752     {
753         x = x -1;
754         x = x | x >> 1;
755         x = x | x >> 2;
756         x = x | x >> 4;
757         x = x | x >> 16;
758         x = x + 1;
759         return x;
760     }
761     /**
762       * int paddedFrameWidth - return frame width in a multiple of 128 (rounded up).
763       *
764       * @width - the original frame width.
765       */
paddedFrameWidth128(int width)766     inline int paddedFrameWidth128(int width)
767     {
768         return  (((width + 127) / 128 )* 128);
769     }
770 
771     /**
772       * int paddedFrameWidth32 - return frame width in a multiple of 32 (rounded up).
773       *
774       * @width - the original frame width.
775       */
paddedFrameWidth32(int width)776     inline int paddedFrameWidth32(int width)
777     {
778         return  (((width + 31) / 32 )* 32);
779     }
780 
781     /**
782       * int roundToNearestInt - round to nearest integer value.
783       *
784       * @value - the decimal value to be rounded to nearest integer.
785       */
roundToNearestInt(double value)786     inline int roundToNearestInt(double value)
787     {
788 	    if((ceil(value) - value) > 0.5)
789 		    return floor(value);
790 	    else
791 		    return ceil(value);
792     }
793 
omx_report_error()794     inline void omx_report_error ()
795     {
796         if (m_cb.EventHandler && !m_error_propogated)
797         {
798             ALOGE("\nERROR: Sending OMX_EventError to Client");
799             m_error_propogated = true;
800             m_cb.EventHandler(&m_cmp,m_app_data,
801                   OMX_EventError,OMX_ErrorHardware,0,NULL);
802         }
803     }
804 
omx_report_unsupported_setting()805     inline void omx_report_unsupported_setting ()
806     {
807         if (m_cb.EventHandler && !m_error_propogated)
808         {
809             DEBUG_PRINT_ERROR(
810                "\nERROR: Sending OMX_ErrorUnsupportedSetting to Client");
811             m_error_propogated = true;
812             m_cb.EventHandler(&m_cmp,m_app_data,
813                   OMX_EventError,OMX_ErrorUnsupportedSetting,0,NULL);
814         }
815     }
816 
817 #if defined (_ANDROID_HONEYCOMB_) || defined (_ANDROID_ICS_)
818     OMX_ERRORTYPE use_android_native_buffer(OMX_IN OMX_HANDLETYPE hComp, OMX_PTR data);
819 #endif
820 #if defined (_ANDROID_ICS_)
821     struct nativebuffer{
822         native_handle_t *nativehandle;
823 	private_handle_t *privatehandle;
824         int inuse;
825     };
826     nativebuffer native_buffer[MAX_NUM_INPUT_OUTPUT_BUFFERS];
827 #endif
828 
829 
830     //*************************************************************
831     //*******************MEMBER VARIABLES *************************
832     //*************************************************************
833     pthread_mutex_t       m_lock;
834     //sem to handle the minimum procesing of commands
835     sem_t                 m_cmd_lock;
836     bool              m_error_propogated;
837 
838     // OMX State
839     OMX_STATETYPE m_state;
840     // Application data
841     OMX_PTR m_app_data;
842     // Application callbacks
843     OMX_CALLBACKTYPE m_cb;
844     OMX_PRIORITYMGMTTYPE m_priority_mgm ;
845     OMX_PARAM_BUFFERSUPPLIERTYPE m_buffer_supplier;
846     // fill this buffer queue
847     omx_cmd_queue         m_ftb_q;
848     // Command Q for rest of the events
849     omx_cmd_queue         m_cmd_q;
850     omx_cmd_queue         m_etb_q;
851     // Input memory pointer
852     OMX_BUFFERHEADERTYPE  *m_inp_mem_ptr;
853     // Output memory pointer
854     OMX_BUFFERHEADERTYPE  *m_out_mem_ptr;
855     // number of input bitstream error frame count
856     unsigned int m_inp_err_count;
857 #ifdef _ANDROID_
858     // Timestamp list
859     ts_arr_list           m_timestamp_list;
860 #endif
861 
862     bool input_flush_progress;
863     bool output_flush_progress;
864     bool input_use_buffer;
865     bool output_use_buffer;
866     bool ouput_egl_buffers;
867     OMX_BOOL m_use_output_pmem;
868     OMX_BOOL m_out_mem_region_smi;
869     OMX_BOOL m_out_pvt_entry_pmem;
870 
871     int pending_input_buffers;
872     int pending_output_buffers;
873 
874     int input_qbuf_count;
875     int input_dqbuf_count;
876     int output_qbuf_count;
877     int output_dqbuf_count;
878 
879 #ifdef OUTPUT_BUFFER_LOG
880     int output_buffer_write_counter;
881     int input_buffer_write_counter;
882 #endif
883     // bitmask array size for output side
884     unsigned int m_out_bm_count;
885     // bitmask array size for input side
886     unsigned int m_inp_bm_count;
887     //Input port Populated
888     OMX_BOOL m_inp_bPopulated;
889     //Output port Populated
890     OMX_BOOL m_out_bPopulated;
891     // encapsulate the waiting states.
892     unsigned int m_flags;
893 
894     // store I/P PORT state
895     OMX_BOOL m_inp_bEnabled;
896     // store O/P PORT state
897     OMX_BOOL m_out_bEnabled;
898     OMX_U32 m_in_alloc_cnt;
899     OMX_U8                m_cRole[OMX_MAX_STRINGNAME_SIZE];
900     // Platform specific details
901     OMX_QCOM_PLATFORM_PRIVATE_LIST      *m_platform_list;
902     OMX_QCOM_PLATFORM_PRIVATE_ENTRY     *m_platform_entry;
903     OMX_QCOM_PLATFORM_PRIVATE_PMEM_INFO *m_pmem_info;
904 
905     /*Variables for arbitrary Byte parsing support*/
906     omx_cmd_queue m_input_pending_q;
907     omx_cmd_queue m_input_free_q;
908 
909     OMX_BUFFERHEADERTYPE  *psource_frame;
910     OMX_BUFFERHEADERTYPE  *pdest_frame;
911     OMX_BUFFERHEADERTYPE  *m_inp_heap_ptr;
912     OMX_BUFFERHEADERTYPE  **m_phdr_pmem_ptr;
913     unsigned int m_heap_inp_bm_count;
914 
915     OMX_S64 prev_ts;
916     bool rst_prev_ts;
917     OMX_U32 frm_int;
918 
919     struct vdpp_allocatorproperty op_buf_rcnfg;
920     bool in_reconfig;
921     OMX_U32 client_extradata;
922 #ifdef _ANDROID_
923     bool m_debug_timestamp;
924     bool perf_flag;
925     OMX_U32 proc_frms, latency;
926     bool m_enable_android_native_buffers;
927     bool m_use_android_native_buffers;
928     bool m_debug_extradata;
929     bool m_debug_concealedmb;
930 #endif
931 
932     OMX_PARAM_PORTDEFINITIONTYPE m_port_def;
933 
934     int capture_capability;
935     int output_capability;
936     bool streaming[MAX_PORT];
937     OMX_CONFIG_RECTTYPE rectangle;
938 	int prev_n_filled_len;
939 
940     bool msg_thread_created;
941     bool async_thread_created;
942 
943     unsigned int m_fill_output_msg;
944     bool client_set_fps;
945     bool interlace_user_flag;
946     // OMX extensions
947     VdppExtensionData_t mExtensionData;
948 };
949 #endif // __OMX_VDPP_H__
950