1 /*--------------------------------------------------------------------------
2 Copyright (c) 2013, 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_VDEC_HEVC_H__
31 #define __OMX_VDEC_HEVC_H__
32 /*============================================================================
33                             O p e n M A X   Component
34                                 Video Decoder
35 
36 *//** @file comx_vdec_hevc.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 <string.h>
48 #include <inttypes.h>
49 #include <cstddef>
50 
51 static ptrdiff_t x;
52 
53 #ifdef _ANDROID_
54 #ifdef MAX_RES_720P
55 #define LOG_TAG "OMX-VDEC-720P"
56 #elif MAX_RES_1080P
57 #define LOG_TAG "OMX-VDEC-1080P"
58 #else
59 #define LOG_TAG "OMX-VDEC"
60 #endif
61 
62 #ifdef USE_ION
63 #include <linux/msm_ion.h>
64 #endif
65 #include <binder/MemoryHeapBase.h>
66 #include <ui/ANativeObjectBase.h>
67 extern "C" {
68 #include <utils/Log.h>
69 }
70 #include <linux/videodev2.h>
71 #include <poll.h>
72 #include "hevc_utils.h"
73 #define TIMEOUT 5000
74 
75 #endif // _ANDROID_
76 
77 
78 #if defined (_ANDROID_HONEYCOMB_) || defined (_ANDROID_ICS_)
79 #include <media/hardware/HardwareAPI.h>
80 #endif
81 
82 #include <unistd.h>
83 
84 #if defined (_ANDROID_ICS_)
85 #include <gralloc_priv.h>
86 #endif
87 
88 #include <pthread.h>
89 #ifndef PC_DEBUG
90 #include <semaphore.h>
91 #endif
92 #include "OMX_Core.h"
93 #include "OMX_QCOMExtns.h"
94 #include "qc_omx_component.h"
95 #include <linux/msm_vidc_dec.h>
96 #include <media/msm_vidc.h>
97 #include "frameparser.h"
98 #ifdef MAX_RES_1080P
99 #include "mp4_utils.h"
100 #endif
101 #include <linux/android_pmem.h>
102 #include "extra_data_handler.h"
103 #include "ts_parser.h"
104 #include "vidc_color_converter.h"
105 #include "vidc_debug.h"
106 extern "C" {
107     OMX_API void * get_omx_component_factory_fn(void);
108 }
109 
110 #ifdef _ANDROID_
111 using namespace android;
112 #ifdef USE_ION
113 class VideoHeap : public MemoryHeapBase
114 {
115     public:
116         VideoHeap(int devicefd, size_t size, void* base,ion_user_handle_t handle,int mapfd);
~VideoHeap()117         virtual ~VideoHeap() {}
118     private:
119         int m_ion_device_fd;
120         ion_user_handle_t m_ion_handle;
121 };
122 #else
123 // local pmem heap object
124 class VideoHeap : public MemoryHeapBase
125 {
126     public:
127         VideoHeap(int fd, size_t size, void* base);
~VideoHeap()128         virtual ~VideoHeap() {}
129 };
130 #endif
131 #endif // _ANDROID_
132 //////////////////////////////////////////////////////////////////////////////
133 //                       Module specific globals
134 //////////////////////////////////////////////////////////////////////////////
135 #define OMX_SPEC_VERSION  0x00000101
136 
137 
138 //////////////////////////////////////////////////////////////////////////////
139 //               Macros
140 //////////////////////////////////////////////////////////////////////////////
141 #define PrintFrameHdr(bufHdr) DEBUG_PRINT("bufHdr %x buf %x size %d TS %d\n",\
142         (unsigned) bufHdr,\
143         (unsigned)((OMX_BUFFERHEADERTYPE *)bufHdr)->pBuffer,\
144         (unsigned)((OMX_BUFFERHEADERTYPE *)bufHdr)->nFilledLen,\
145         (unsigned)((OMX_BUFFERHEADERTYPE *)bufHdr)->nTimeStamp)
146 
147 // BitMask Management logic
148 #define BITS_PER_BYTE        32
149 #define BITMASK_SIZE(mIndex) (((mIndex) + BITS_PER_BYTE - 1)/BITS_PER_BYTE)
150 #define BITMASK_OFFSET(mIndex) ((mIndex)/BITS_PER_BYTE)
151 #define BITMASK_FLAG(mIndex) (1 << ((mIndex) % BITS_PER_BYTE))
152 #define BITMASK_CLEAR(mArray,mIndex) (mArray)[BITMASK_OFFSET(mIndex)] \
153     &=  ~(BITMASK_FLAG(mIndex))
154 #define BITMASK_SET(mArray,mIndex)  (mArray)[BITMASK_OFFSET(mIndex)] \
155     |=  BITMASK_FLAG(mIndex)
156 #define BITMASK_PRESENT(mArray,mIndex) ((mArray)[BITMASK_OFFSET(mIndex)] \
157         & BITMASK_FLAG(mIndex))
158 #define BITMASK_ABSENT(mArray,mIndex) (((mArray)[BITMASK_OFFSET(mIndex)] \
159             & BITMASK_FLAG(mIndex)) == 0x0)
160 #define BITMASK_PRESENT(mArray,mIndex) ((mArray)[BITMASK_OFFSET(mIndex)] \
161         & BITMASK_FLAG(mIndex))
162 #define BITMASK_ABSENT(mArray,mIndex) (((mArray)[BITMASK_OFFSET(mIndex)] \
163             & BITMASK_FLAG(mIndex)) == 0x0)
164 
165 #define OMX_CORE_CONTROL_CMDQ_SIZE   100
166 #define OMX_CORE_QCIF_HEIGHT         144
167 #define OMX_CORE_QCIF_WIDTH          176
168 #define OMX_CORE_VGA_HEIGHT          480
169 #define OMX_CORE_VGA_WIDTH           640
170 #define OMX_CORE_WVGA_HEIGHT         480
171 #define OMX_CORE_WVGA_WIDTH          800
172 
173 #define DESC_BUFFER_SIZE (8192 * 16)
174 
175 #ifdef _ANDROID_
176 #define MAX_NUM_INPUT_OUTPUT_BUFFERS 32
177 #endif
178 
179 #define OMX_FRAMEINFO_EXTRADATA 0x00010000
180 #define OMX_INTERLACE_EXTRADATA 0x00020000
181 #define OMX_TIMEINFO_EXTRADATA  0x00040000
182 #define OMX_PORTDEF_EXTRADATA   0x00080000
183 #define OMX_EXTNUSER_EXTRADATA  0x00100000
184 #define DRIVER_EXTRADATA_MASK   0x0000FFFF
185 
186 #define OMX_INTERLACE_EXTRADATA_SIZE ((sizeof(OMX_OTHER_EXTRADATATYPE) +\
187             sizeof(OMX_STREAMINTERLACEFORMAT) + 3)&(~3))
188 #define OMX_FRAMEINFO_EXTRADATA_SIZE ((sizeof(OMX_OTHER_EXTRADATATYPE) +\
189             sizeof(OMX_QCOM_EXTRADATA_FRAMEINFO) + 3)&(~3))
190 #define OMX_PORTDEF_EXTRADATA_SIZE ((sizeof(OMX_OTHER_EXTRADATATYPE) +\
191             sizeof(OMX_PARAM_PORTDEFINITIONTYPE) + 3)&(~3))
192 
193 //  Define next macro with required values to enable default extradata,
194 //    VDEC_EXTRADATA_MB_ERROR_MAP
195 //    OMX_INTERLACE_EXTRADATA
196 //    OMX_FRAMEINFO_EXTRADATA
197 //    OMX_TIMEINFO_EXTRADATA
198 
199 //#define DEFAULT_EXTRADATA (OMX_FRAMEINFO_EXTRADATA|OMX_INTERLACE_EXTRADATA)
200 
201 enum port_indexes {
202     OMX_CORE_INPUT_PORT_INDEX        =0,
203     OMX_CORE_OUTPUT_PORT_INDEX       =1
204 };
205 #ifdef USE_ION
206 struct vdec_ion {
207     int ion_device_fd;
208     struct ion_fd_data fd_ion_data;
209     struct ion_allocation_data ion_alloc_data;
210 };
211 #endif
212 
213 #ifdef _MSM8974_
214 struct extradata_buffer_info {
215     int buffer_size;
216     char* uaddr;
217     int count;
218     int size;
219 #ifdef USE_ION
220     struct vdec_ion ion;
221 #endif
222 };
223 #endif
224 
225 struct video_driver_context {
226     int video_driver_fd;
227     enum vdec_codec decoder_format;
228     enum vdec_output_fromat output_format;
229     enum vdec_interlaced_format interlace;
230     enum vdec_output_order picture_order;
231     struct vdec_picsize video_resolution;
232     struct vdec_allocatorproperty ip_buf;
233     struct vdec_allocatorproperty op_buf;
234     struct vdec_bufferpayload *ptr_inputbuffer;
235     struct vdec_bufferpayload *ptr_outputbuffer;
236     struct vdec_output_frameinfo *ptr_respbuffer;
237 #ifdef USE_ION
238     struct vdec_ion *ip_buf_ion_info;
239     struct vdec_ion *op_buf_ion_info;
240     struct vdec_ion h264_mv;
241     struct vdec_ion meta_buffer;
242     struct vdec_ion meta_buffer_iommu;
243 #endif
244     struct vdec_framerate frame_rate;
245     unsigned extradata;
246     bool timestamp_adjust;
247     char kind[128];
248     bool idr_only_decoding;
249     unsigned disable_dmx;
250 #ifdef _MSM8974_
251     struct extradata_buffer_info extradata_info;
252     int num_planes;
253 #endif
254 };
255 
256 // OMX video decoder class
257 class omx_vdec: public qc_omx_component
258 {
259 
260     public:
261         omx_vdec();  // constructor
262         virtual ~omx_vdec();  // destructor
263 
264         static int async_message_process (void *context, void* message);
265         static void process_event_cb(void *ctxt,unsigned char id);
266 
267         OMX_ERRORTYPE allocate_buffer(
268                 OMX_HANDLETYPE hComp,
269                 OMX_BUFFERHEADERTYPE **bufferHdr,
270                 OMX_U32 port,
271                 OMX_PTR appData,
272                 OMX_U32 bytes
273                 );
274 
275 
276         OMX_ERRORTYPE component_deinit(OMX_HANDLETYPE hComp);
277 
278         OMX_ERRORTYPE component_init(OMX_STRING role);
279 
280         OMX_ERRORTYPE component_role_enum(
281                 OMX_HANDLETYPE hComp,
282                 OMX_U8 *role,
283                 OMX_U32 index
284                 );
285 
286         OMX_ERRORTYPE component_tunnel_request(
287                 OMX_HANDLETYPE hComp,
288                 OMX_U32 port,
289                 OMX_HANDLETYPE  peerComponent,
290                 OMX_U32 peerPort,
291                 OMX_TUNNELSETUPTYPE *tunnelSetup
292                 );
293 
294         OMX_ERRORTYPE empty_this_buffer(
295                 OMX_HANDLETYPE hComp,
296                 OMX_BUFFERHEADERTYPE *buffer
297                 );
298 
299 
300 
301         OMX_ERRORTYPE fill_this_buffer(
302                 OMX_HANDLETYPE hComp,
303                 OMX_BUFFERHEADERTYPE *buffer
304                 );
305 
306 
307         OMX_ERRORTYPE free_buffer(
308                 OMX_HANDLETYPE hComp,
309                 OMX_U32 port,
310                 OMX_BUFFERHEADERTYPE *buffer
311                 );
312 
313         OMX_ERRORTYPE get_component_version(
314                 OMX_HANDLETYPE hComp,
315                 OMX_STRING componentName,
316                 OMX_VERSIONTYPE *componentVersion,
317                 OMX_VERSIONTYPE *specVersion,
318                 OMX_UUIDTYPE *componentUUID
319                 );
320 
321         OMX_ERRORTYPE get_config(
322                 OMX_HANDLETYPE hComp,
323                 OMX_INDEXTYPE configIndex,
324                 OMX_PTR configData
325                 );
326 
327         OMX_ERRORTYPE get_extension_index(
328                 OMX_HANDLETYPE hComp,
329                 OMX_STRING paramName,
330                 OMX_INDEXTYPE *indexType
331                 );
332 
333         OMX_ERRORTYPE get_parameter(OMX_HANDLETYPE hComp,
334                 OMX_INDEXTYPE  paramIndex,
335                 OMX_PTR        paramData);
336 
337         OMX_ERRORTYPE get_state(OMX_HANDLETYPE hComp,
338                 OMX_STATETYPE *state);
339 
340 
341 
342         OMX_ERRORTYPE send_command(OMX_HANDLETYPE  hComp,
343                 OMX_COMMANDTYPE cmd,
344                 OMX_U32         param1,
345                 OMX_PTR         cmdData);
346 
347 
348         OMX_ERRORTYPE set_callbacks(OMX_HANDLETYPE   hComp,
349                 OMX_CALLBACKTYPE *callbacks,
350                 OMX_PTR          appData);
351 
352         OMX_ERRORTYPE set_config(OMX_HANDLETYPE hComp,
353                 OMX_INDEXTYPE  configIndex,
354                 OMX_PTR        configData);
355 
356         OMX_ERRORTYPE set_parameter(OMX_HANDLETYPE hComp,
357                 OMX_INDEXTYPE  paramIndex,
358                 OMX_PTR        paramData);
359 
360         OMX_ERRORTYPE use_buffer(OMX_HANDLETYPE      hComp,
361                 OMX_BUFFERHEADERTYPE **bufferHdr,
362                 OMX_U32              port,
363                 OMX_PTR              appData,
364                 OMX_U32              bytes,
365                 OMX_U8               *buffer);
366 
367         OMX_ERRORTYPE  use_input_heap_buffers(
368                 OMX_HANDLETYPE            hComp,
369                 OMX_BUFFERHEADERTYPE** bufferHdr,
370                 OMX_U32                   port,
371                 OMX_PTR                   appData,
372                 OMX_U32                   bytes,
373                 OMX_U8*                   buffer);
374 
375         OMX_ERRORTYPE use_EGL_image(OMX_HANDLETYPE     hComp,
376                 OMX_BUFFERHEADERTYPE **bufferHdr,
377                 OMX_U32              port,
378                 OMX_PTR              appData,
379                 void *               eglImage);
380         void complete_pending_buffer_done_cbs();
381         struct video_driver_context drv_ctx;
382 #ifdef _MSM8974_
383         OMX_ERRORTYPE allocate_extradata();
384         void free_extradata();
385         void update_resolution(int width, int height);
386 #endif
387         int  m_pipe_in;
388         int  m_pipe_out;
389         pthread_t msg_thread_id;
390         pthread_t async_thread_id;
391         bool is_component_secure();
392 
393     private:
394         // Bit Positions
395         enum flags_bit_positions {
396             // Defer transition to IDLE
397             OMX_COMPONENT_IDLE_PENDING            =0x1,
398             // Defer transition to LOADING
399             OMX_COMPONENT_LOADING_PENDING         =0x2,
400             // First  Buffer Pending
401             OMX_COMPONENT_FIRST_BUFFER_PENDING    =0x3,
402             // Second Buffer Pending
403             OMX_COMPONENT_SECOND_BUFFER_PENDING   =0x4,
404             // Defer transition to Enable
405             OMX_COMPONENT_INPUT_ENABLE_PENDING    =0x5,
406             // Defer transition to Enable
407             OMX_COMPONENT_OUTPUT_ENABLE_PENDING   =0x6,
408             // Defer transition to Disable
409             OMX_COMPONENT_INPUT_DISABLE_PENDING   =0x7,
410             // Defer transition to Disable
411             OMX_COMPONENT_OUTPUT_DISABLE_PENDING  =0x8,
412             //defer flush notification
413             OMX_COMPONENT_OUTPUT_FLUSH_PENDING    =0x9,
414             OMX_COMPONENT_INPUT_FLUSH_PENDING    =0xA,
415             OMX_COMPONENT_PAUSE_PENDING          =0xB,
416             OMX_COMPONENT_EXECUTE_PENDING        =0xC,
417             OMX_COMPONENT_OUTPUT_FLUSH_IN_DISABLE_PENDING =0xD,
418             OMX_COMPONENT_DISABLE_OUTPUT_DEFERRED=0xE
419         };
420 
421         // Deferred callback identifiers
422         enum {
423             //Event Callbacks from the vdec component thread context
424             OMX_COMPONENT_GENERATE_EVENT       = 0x1,
425             //Buffer Done callbacks from the vdec component thread context
426             OMX_COMPONENT_GENERATE_BUFFER_DONE = 0x2,
427             //Frame Done callbacks from the vdec component thread context
428             OMX_COMPONENT_GENERATE_FRAME_DONE  = 0x3,
429             //Buffer Done callbacks from the vdec component thread context
430             OMX_COMPONENT_GENERATE_FTB         = 0x4,
431             //Frame Done callbacks from the vdec component thread context
432             OMX_COMPONENT_GENERATE_ETB         = 0x5,
433             //Command
434             OMX_COMPONENT_GENERATE_COMMAND     = 0x6,
435             //Push-Pending Buffers
436             OMX_COMPONENT_PUSH_PENDING_BUFS    = 0x7,
437             // Empty Buffer Done callbacks
438             OMX_COMPONENT_GENERATE_EBD         = 0x8,
439             //Flush Event Callbacks from the vdec component thread context
440             OMX_COMPONENT_GENERATE_EVENT_FLUSH       = 0x9,
441             OMX_COMPONENT_GENERATE_EVENT_INPUT_FLUSH = 0x0A,
442             OMX_COMPONENT_GENERATE_EVENT_OUTPUT_FLUSH = 0x0B,
443             OMX_COMPONENT_GENERATE_FBD = 0xc,
444             OMX_COMPONENT_GENERATE_START_DONE = 0xD,
445             OMX_COMPONENT_GENERATE_PAUSE_DONE = 0xE,
446             OMX_COMPONENT_GENERATE_RESUME_DONE = 0xF,
447             OMX_COMPONENT_GENERATE_STOP_DONE = 0x10,
448             OMX_COMPONENT_GENERATE_HARDWARE_ERROR = 0x11,
449             OMX_COMPONENT_GENERATE_ETB_ARBITRARY = 0x12,
450             OMX_COMPONENT_GENERATE_PORT_RECONFIG = 0x13,
451             OMX_COMPONENT_GENERATE_EOS_DONE = 0x14,
452             OMX_COMPONENT_GENERATE_INFO_PORT_RECONFIG = 0x15,
453             OMX_COMPONENT_GENERATE_INFO_FIELD_DROPPED = 0x16,
454         };
455 
456         enum vc1_profile_type {
457             VC1_SP_MP_RCV = 1,
458             VC1_AP = 2
459         };
460 
461 #ifdef _MSM8974_
462         enum v4l2_ports {
463             CAPTURE_PORT,
464             OUTPUT_PORT,
465             MAX_PORT
466         };
467 #endif
468 
469         struct omx_event {
470             unsigned param1;
471             unsigned param2;
472             unsigned id;
473         };
474 
475         struct omx_cmd_queue {
476             omx_event m_q[OMX_CORE_CONTROL_CMDQ_SIZE];
477             unsigned m_read;
478             unsigned m_write;
479             unsigned m_size;
480 
481             omx_cmd_queue();
482             ~omx_cmd_queue();
483             bool insert_entry(unsigned p1, unsigned p2, unsigned id);
484             bool pop_entry(unsigned *p1,unsigned *p2, unsigned *id);
485             // get msgtype of the first ele from the queue
486             unsigned get_q_msg_type();
487 
488         };
489 
490 #ifdef _ANDROID_
491         struct ts_entry {
492             OMX_TICKS timestamp;
493             bool valid;
494         };
495 
496         struct ts_arr_list {
497             ts_entry m_ts_arr_list[MAX_NUM_INPUT_OUTPUT_BUFFERS];
498 
499             ts_arr_list();
500             ~ts_arr_list();
501 
502             bool insert_ts(OMX_TICKS ts);
503             bool pop_min_ts(OMX_TICKS &ts);
504             bool reset_ts_list();
505         };
506 #endif
507 
508         struct desc_buffer_hdr {
509             OMX_U8 *buf_addr;
510             OMX_U32 desc_data_size;
511         };
512         bool allocate_done(void);
513         bool allocate_input_done(void);
514         bool allocate_output_done(void);
515 
516         OMX_ERRORTYPE free_input_buffer(OMX_BUFFERHEADERTYPE *bufferHdr);
517         OMX_ERRORTYPE free_input_buffer(unsigned int bufferindex,
518                 OMX_BUFFERHEADERTYPE *pmem_bufferHdr);
519         OMX_ERRORTYPE free_output_buffer(OMX_BUFFERHEADERTYPE *bufferHdr);
520         void free_output_buffer_header();
521         void free_input_buffer_header();
522 
523         OMX_ERRORTYPE allocate_input_heap_buffer(OMX_HANDLETYPE       hComp,
524                 OMX_BUFFERHEADERTYPE **bufferHdr,
525                 OMX_U32              port,
526                 OMX_PTR              appData,
527                 OMX_U32              bytes);
528 
529 
530         OMX_ERRORTYPE allocate_input_buffer(OMX_HANDLETYPE       hComp,
531                 OMX_BUFFERHEADERTYPE **bufferHdr,
532                 OMX_U32              port,
533                 OMX_PTR              appData,
534                 OMX_U32              bytes);
535 
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         OMX_ERRORTYPE use_output_buffer(OMX_HANDLETYPE hComp,
541                 OMX_BUFFERHEADERTYPE   **bufferHdr,
542                 OMX_U32                port,
543                 OMX_PTR                appData,
544                 OMX_U32                bytes,
545                 OMX_U8                 *buffer);
546 #ifdef MAX_RES_720P
547         OMX_ERRORTYPE get_supported_profile_level_for_720p(OMX_VIDEO_PARAM_PROFILELEVELTYPE *profileLevelType);
548 #endif
549 #ifdef MAX_RES_1080P
550         OMX_ERRORTYPE get_supported_profile_level_for_1080p(OMX_VIDEO_PARAM_PROFILELEVELTYPE *profileLevelType);
551 #endif
552 
553         OMX_ERRORTYPE allocate_desc_buffer(OMX_U32 index);
554         OMX_ERRORTYPE allocate_output_headers();
555         bool execute_omx_flush(OMX_U32);
556         bool execute_output_flush();
557         bool execute_input_flush();
558         OMX_ERRORTYPE empty_buffer_done(OMX_HANDLETYPE hComp,
559                 OMX_BUFFERHEADERTYPE * buffer);
560 
561         OMX_ERRORTYPE fill_buffer_done(OMX_HANDLETYPE hComp,
562                 OMX_BUFFERHEADERTYPE * buffer);
563         OMX_ERRORTYPE empty_this_buffer_proxy(OMX_HANDLETYPE       hComp,
564                 OMX_BUFFERHEADERTYPE *buffer);
565 
566         OMX_ERRORTYPE empty_this_buffer_proxy_arbitrary(OMX_HANDLETYPE hComp,
567                 OMX_BUFFERHEADERTYPE *buffer
568                 );
569 
570         OMX_ERRORTYPE push_input_buffer (OMX_HANDLETYPE hComp);
571         OMX_ERRORTYPE push_input_sc_codec (OMX_HANDLETYPE hComp);
572         OMX_ERRORTYPE push_input_h264 (OMX_HANDLETYPE hComp);
573         OMX_ERRORTYPE push_input_hevc (OMX_HANDLETYPE hComp);
574         OMX_ERRORTYPE push_input_vc1 (OMX_HANDLETYPE hComp);
575 
576         OMX_ERRORTYPE fill_this_buffer_proxy(OMX_HANDLETYPE       hComp,
577                 OMX_BUFFERHEADERTYPE *buffer);
578         bool release_done();
579 
580         bool release_output_done();
581         bool release_input_done();
582         OMX_ERRORTYPE get_buffer_req(vdec_allocatorproperty *buffer_prop);
583         OMX_ERRORTYPE set_buffer_req(vdec_allocatorproperty *buffer_prop);
584         OMX_ERRORTYPE start_port_reconfig();
585         OMX_ERRORTYPE update_picture_resolution();
586         int stream_off(OMX_U32 port);
587         void adjust_timestamp(OMX_S64 &act_timestamp);
588         void set_frame_rate(OMX_S64 act_timestamp);
589         void handle_extradata_secure(OMX_BUFFERHEADERTYPE *p_buf_hdr);
590         void handle_extradata(OMX_BUFFERHEADERTYPE *p_buf_hdr);
591         void print_debug_extradata(OMX_OTHER_EXTRADATATYPE *extra);
592 #ifdef _MSM8974_
593         void append_interlace_extradata(OMX_OTHER_EXTRADATATYPE *extra,
594                 OMX_U32 interlaced_format_type);
595         OMX_ERRORTYPE enable_extradata(OMX_U32 requested_extradata, bool is_internal,
596                 bool enable = true);
597         void append_frame_info_extradata(OMX_OTHER_EXTRADATATYPE *extra,
598                 OMX_U32 num_conceal_mb,
599                 OMX_U32 picture_type,
600                 OMX_U32 frame_rate,
601                 struct msm_vidc_panscan_window_payload *panscan_payload,
602                 struct vdec_aspectratioinfo *aspect_ratio_info);
603 #else
604         void append_interlace_extradata(OMX_OTHER_EXTRADATATYPE *extra,
605                 OMX_U32 interlaced_format_type, OMX_U32 buf_index);
606         OMX_ERRORTYPE enable_extradata(OMX_U32 requested_extradata, bool enable = true);
607 #endif
608         void append_frame_info_extradata(OMX_OTHER_EXTRADATATYPE *extra,
609                 OMX_U32 num_conceal_mb,
610                 OMX_U32 picture_type,
611                 OMX_S64 timestamp,
612                 OMX_U32 frame_rate,
613                 struct vdec_aspectratioinfo *aspect_ratio_info);
614         void fill_aspect_ratio_info(struct vdec_aspectratioinfo *aspect_ratio_info,
615                 OMX_QCOM_EXTRADATA_FRAMEINFO *frame_info);
616         void append_terminator_extradata(OMX_OTHER_EXTRADATATYPE *extra);
617         OMX_ERRORTYPE update_portdef(OMX_PARAM_PORTDEFINITIONTYPE *portDefn);
618         void append_portdef_extradata(OMX_OTHER_EXTRADATATYPE *extra);
619         void append_extn_extradata(OMX_OTHER_EXTRADATATYPE *extra, OMX_OTHER_EXTRADATATYPE *p_extn);
620         void append_user_extradata(OMX_OTHER_EXTRADATATYPE *extra, OMX_OTHER_EXTRADATATYPE *p_user);
621         void insert_demux_addr_offset(OMX_U32 address_offset);
622         void extract_demux_addr_offsets(OMX_BUFFERHEADERTYPE *buf_hdr);
623         OMX_ERRORTYPE handle_demux_data(OMX_BUFFERHEADERTYPE *buf_hdr);
624         OMX_U32 count_MB_in_extradata(OMX_OTHER_EXTRADATATYPE *extra);
625 
626         bool align_pmem_buffers(int pmem_fd, OMX_U32 buffer_size,
627                 OMX_U32 alignment);
628 #ifdef USE_ION
629         int alloc_map_ion_memory(OMX_U32 buffer_size,
630                 OMX_U32 alignment, struct ion_allocation_data *alloc_data,
631                 struct ion_fd_data *fd_data,int flag);
632         void free_ion_memory(struct vdec_ion *buf_ion_info);
633 #endif
634 
635 
636         OMX_ERRORTYPE send_command_proxy(OMX_HANDLETYPE  hComp,
637                 OMX_COMMANDTYPE cmd,
638                 OMX_U32         param1,
639                 OMX_PTR         cmdData);
640         bool post_event( unsigned int p1,
641                 unsigned int p2,
642                 unsigned int id
643                    );
clip2(int x)644         inline int clip2(int x) {
645             x = x -1;
646             x = x | x >> 1;
647             x = x | x >> 2;
648             x = x | x >> 4;
649             x = x | x >> 16;
650             x = x + 1;
651             return x;
652         }
653 
654 #ifdef MAX_RES_1080P
655         OMX_ERRORTYPE vdec_alloc_h264_mv();
656         void vdec_dealloc_h264_mv();
657         OMX_ERRORTYPE vdec_alloc_meta_buffers();
658         void vdec_dealloc_meta_buffers();
659 #endif
660 
omx_report_error()661         inline void omx_report_error () {
662             if (m_cb.EventHandler && !m_error_propogated) {
663                 ALOGE("\nERROR: Sending OMX_EventError to Client");
664                 m_error_propogated = true;
665                 m_cb.EventHandler(&m_cmp,m_app_data,
666                         OMX_EventError,OMX_ErrorHardware,0,NULL);
667             }
668         }
669 #if defined (_ANDROID_HONEYCOMB_) || defined (_ANDROID_ICS_)
670         OMX_ERRORTYPE use_android_native_buffer(OMX_IN OMX_HANDLETYPE hComp, OMX_PTR data);
671 #endif
672 #if defined (_ANDROID_ICS_)
673         struct nativebuffer {
674             native_handle_t *nativehandle;
675             private_handle_t *privatehandle;
676             int inuse;
677         };
678         nativebuffer native_buffer[MAX_NUM_INPUT_OUTPUT_BUFFERS];
679 #endif
680 
681 
682         //*************************************************************
683         //*******************MEMBER VARIABLES *************************
684         //*************************************************************
685         pthread_mutex_t       m_lock;
686         pthread_mutex_t       c_lock;
687         //sem to handle the minimum procesing of commands
688         sem_t                 m_cmd_lock;
689         bool              m_error_propogated;
690         // compression format
691         OMX_VIDEO_CODINGTYPE eCompressionFormat;
692         // OMX State
693         OMX_STATETYPE m_state;
694         // Application data
695         OMX_PTR m_app_data;
696         // Application callbacks
697         OMX_CALLBACKTYPE m_cb;
698         OMX_PRIORITYMGMTTYPE m_priority_mgm ;
699         OMX_PARAM_BUFFERSUPPLIERTYPE m_buffer_supplier;
700         // fill this buffer queue
701         omx_cmd_queue         m_ftb_q;
702         // Command Q for rest of the events
703         omx_cmd_queue         m_cmd_q;
704         omx_cmd_queue         m_etb_q;
705         // Input memory pointer
706         OMX_BUFFERHEADERTYPE  *m_inp_mem_ptr;
707         // Output memory pointer
708         OMX_BUFFERHEADERTYPE  *m_out_mem_ptr;
709         // number of input bitstream error frame count
710         unsigned int m_inp_err_count;
711 #ifdef _ANDROID_
712         // Timestamp list
713         ts_arr_list           m_timestamp_list;
714 #endif
715 
716         bool input_flush_progress;
717         bool output_flush_progress;
718         bool input_use_buffer;
719         bool output_use_buffer;
720         bool ouput_egl_buffers;
721         OMX_BOOL m_use_output_pmem;
722         OMX_BOOL m_out_mem_region_smi;
723         OMX_BOOL m_out_pvt_entry_pmem;
724 
725         int pending_input_buffers;
726         int pending_output_buffers;
727         // bitmask array size for output side
728         unsigned int m_out_bm_count;
729         // bitmask array size for input side
730         unsigned int m_inp_bm_count;
731         //Input port Populated
732         OMX_BOOL m_inp_bPopulated;
733         //Output port Populated
734         OMX_BOOL m_out_bPopulated;
735         // encapsulate the waiting states.
736         unsigned int m_flags;
737 
738 #ifdef _ANDROID_
739         // Heap pointer to frame buffers
740         struct vidc_heap {
741             sp<MemoryHeapBase>    video_heap_ptr;
742         };
743         struct vidc_heap *m_heap_ptr;
744         unsigned int m_heap_count;
745 #endif //_ANDROID_
746         // store I/P PORT state
747         OMX_BOOL m_inp_bEnabled;
748         // store O/P PORT state
749         OMX_BOOL m_out_bEnabled;
750         OMX_U32 m_in_alloc_cnt;
751         OMX_U8                m_cRole[OMX_MAX_STRINGNAME_SIZE];
752         // Platform specific details
753         OMX_QCOM_PLATFORM_PRIVATE_LIST      *m_platform_list;
754         OMX_QCOM_PLATFORM_PRIVATE_ENTRY     *m_platform_entry;
755         OMX_QCOM_PLATFORM_PRIVATE_PMEM_INFO *m_pmem_info;
756         // SPS+PPS sent as part of set_config
757         OMX_VENDOR_EXTRADATATYPE            m_vendor_config;
758 
759         /*Variables for arbitrary Byte parsing support*/
760         frame_parse m_frame_parser;
761         omx_cmd_queue m_input_pending_q;
762         omx_cmd_queue m_input_free_q;
763         bool arbitrary_bytes;
764         OMX_BUFFERHEADERTYPE  h264_scratch;
765         OMX_BUFFERHEADERTYPE  *psource_frame;
766         OMX_BUFFERHEADERTYPE  *pdest_frame;
767         OMX_BUFFERHEADERTYPE  *m_inp_heap_ptr;
768         OMX_BUFFERHEADERTYPE  **m_phdr_pmem_ptr;
769         unsigned int m_heap_inp_bm_count;
770         codec_type codec_type_parse;
771         bool first_frame_meta;
772         unsigned frame_count;
773         unsigned nal_count;
774         unsigned nal_length;
775         bool look_ahead_nal;
776         int first_frame;
777         unsigned char *first_buffer;
778         int first_frame_size;
779         unsigned char m_hwdevice_name[80];
780         FILE *m_device_file_ptr;
781         enum vc1_profile_type m_vc1_profile;
782         OMX_S64 h264_last_au_ts;
783         OMX_U32 h264_last_au_flags;
784         OMX_U32 m_demux_offsets[8192];
785         OMX_U32 m_demux_entries;
786         OMX_U32 m_disp_hor_size;
787         OMX_U32 m_disp_vert_size;
788 
789         OMX_S64 prev_ts;
790         bool rst_prev_ts;
791         OMX_U32 frm_int;
792 
793         struct vdec_allocatorproperty op_buf_rcnfg;
794         bool in_reconfig;
795         OMX_NATIVE_WINDOWTYPE m_display_id;
796         h264_stream_parser *h264_parser;
797         OMX_U32 client_extradata;
798 #ifdef _ANDROID_
799         bool m_debug_timestamp;
800         bool perf_flag;
801         OMX_U32 proc_frms, latency;
802         perf_metrics fps_metrics;
803         perf_metrics dec_time;
804         bool m_enable_android_native_buffers;
805         bool m_use_android_native_buffers;
806         bool m_debug_extradata;
807         bool m_debug_concealedmb;
808 #endif
809 #ifdef MAX_RES_1080P
810         MP4_Utils mp4_headerparser;
811 #endif
812 
813         struct h264_mv_buffer {
814             unsigned char* buffer;
815             int size;
816             int count;
817             int pmem_fd;
818             int offset;
819         };
820         h264_mv_buffer h264_mv_buff;
821 
822         struct meta_buffer {
823             unsigned char* buffer;
824             int size;
825             int count;
826             int pmem_fd;
827             int pmem_fd_iommu;
828             int offset;
829         };
830         meta_buffer meta_buff;
831         extra_data_handler extra_data_handle;
832         OMX_PARAM_PORTDEFINITIONTYPE m_port_def;
833         omx_time_stamp_reorder time_stamp_dts;
834         desc_buffer_hdr *m_desc_buffer_ptr;
835         bool secure_mode;
836         bool external_meta_buffer;
837         bool external_meta_buffer_iommu;
838         OMX_QCOM_EXTRADATA_FRAMEINFO *m_extradata;
839         bool codec_config_flag;
840 #ifdef _MSM8974_
841         int capture_capability;
842         int output_capability;
843         bool streaming[MAX_PORT];
844         OMX_CONFIG_RECTTYPE rectangle;
845 #endif
846         bool m_power_hinted;
847         OMX_ERRORTYPE power_module_register();
848         OMX_ERRORTYPE power_module_deregister();
849         bool msg_thread_created;
850         bool async_thread_created;
851 
852         unsigned int m_fill_output_msg;
853         class allocate_color_convert_buf
854         {
855             public:
856                 allocate_color_convert_buf();
857                 ~allocate_color_convert_buf();
858                 void set_vdec_client(void *);
859                 void update_client();
860                 bool set_color_format(OMX_COLOR_FORMATTYPE dest_color_format);
861                 bool get_color_format(OMX_COLOR_FORMATTYPE &dest_color_format);
862                 bool update_buffer_req();
863                 bool get_buffer_req(unsigned int &buffer_size);
864                 OMX_BUFFERHEADERTYPE* get_il_buf_hdr();
865                 OMX_BUFFERHEADERTYPE* get_il_buf_hdr(OMX_BUFFERHEADERTYPE *input_hdr);
866                 OMX_BUFFERHEADERTYPE* get_dr_buf_hdr(OMX_BUFFERHEADERTYPE *input_hdr);
867                 OMX_BUFFERHEADERTYPE* convert(OMX_BUFFERHEADERTYPE *header);
868                 OMX_BUFFERHEADERTYPE* queue_buffer(OMX_BUFFERHEADERTYPE *header);
869                 OMX_ERRORTYPE allocate_buffers_color_convert(OMX_HANDLETYPE hComp,
870                         OMX_BUFFERHEADERTYPE **bufferHdr,OMX_U32 port,OMX_PTR appData,
871                         OMX_U32 bytes);
872                 OMX_ERRORTYPE free_output_buffer(OMX_BUFFERHEADERTYPE *bufferHdr);
873             private:
874 #define MAX_COUNT 32
875                 omx_vdec *omx;
876                 bool enabled;
877                 OMX_COLOR_FORMATTYPE ColorFormat;
878                 void init_members();
879                 bool color_convert_mode;
880                 ColorConvertFormat dest_format;
881                 class omx_c2d_conv c2d;
882                 unsigned int allocated_count;
883                 unsigned int buffer_size_req;
884                 unsigned int buffer_alignment_req;
885                 OMX_QCOM_PLATFORM_PRIVATE_LIST      m_platform_list_client[MAX_COUNT];
886                 OMX_QCOM_PLATFORM_PRIVATE_ENTRY     m_platform_entry_client[MAX_COUNT];
887                 OMX_QCOM_PLATFORM_PRIVATE_PMEM_INFO m_pmem_info_client[MAX_COUNT];
888                 OMX_BUFFERHEADERTYPE  m_out_mem_ptr_client[MAX_COUNT];
889 #ifdef USE_ION
890                 struct vdec_ion op_buf_ion_info[MAX_COUNT];
891 #endif
892                 unsigned char *pmem_baseaddress[MAX_COUNT];
893                 int pmem_fd[MAX_COUNT];
894                 struct vidc_heap {
895                     sp<MemoryHeapBase>    video_heap_ptr;
896                 };
897                 struct vidc_heap m_heap_ptr[MAX_COUNT];
898         };
899 #if  defined (_MSM8960_) || defined (_MSM8974_)
900         allocate_color_convert_buf client_buffers;
901 #endif
902         HEVC_Utils mHEVCutils;
903 };
904 
905 #ifdef _MSM8974_
906 enum instance_state {
907     MSM_VIDC_CORE_UNINIT_DONE = 0x0001,
908     MSM_VIDC_CORE_INIT,
909     MSM_VIDC_CORE_INIT_DONE,
910     MSM_VIDC_OPEN,
911     MSM_VIDC_OPEN_DONE,
912     MSM_VIDC_LOAD_RESOURCES,
913     MSM_VIDC_LOAD_RESOURCES_DONE,
914     MSM_VIDC_START,
915     MSM_VIDC_START_DONE,
916     MSM_VIDC_STOP,
917     MSM_VIDC_STOP_DONE,
918     MSM_VIDC_RELEASE_RESOURCES,
919     MSM_VIDC_RELEASE_RESOURCES_DONE,
920     MSM_VIDC_CLOSE,
921     MSM_VIDC_CLOSE_DONE,
922     MSM_VIDC_CORE_UNINIT,
923 };
924 
925 enum vidc_resposes_id {
926     MSM_VIDC_DECODER_FLUSH_DONE = 0x11,
927     MSM_VIDC_DECODER_EVENT_CHANGE,
928 };
929 
930 #endif // _MSM8974_
931 
932 #endif // __OMX_VDEC_H__
933