1 /*--------------------------------------------------------------------------
2 Copyright (c) 2010 - 2020, The Linux Foundation. All rights reserved.
3 
4   Redistribution and use in source and binary forms, with or without
5   modification, are permitted provided that the following conditions
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_H__
31 #define __OMX_VDEC_H__
32 /*============================================================================
33                             O p e n M A X   Component
34                                 Video Decoder
35 
36 *//** @file comx_vdec.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 #include <dlfcn.h>
51 #include <cutils/atomic.h>
52 #include <qdMetaData.h>
53 #include <color_metadata.h>
54 #define STRINGIFY_ENUMS
55 #include "media/hardware/VideoAPI.h"
56 #include "media/hardware/HardwareAPI.h"
57 #include <unordered_map>
58 #include <media/msm_media_info.h>
59 #include <list>
60 
61 #include <linux/msm_ion.h>
62 #if TARGET_ION_ABI_VERSION >= 2
63 #include <ion/ion.h>
64 #include <linux/dma-buf.h>
65 #else
66 #include <linux/ion.h>
67 #endif
68 
69 #include "C2DColorConverter.h"
70 
71 static ptrdiff_t x;
72 
73 extern "C" {
74 #include <utils/Log.h>
75 }
76 
77 #ifdef _ANDROID_
78 #undef LOG_TAG
79 #define LOG_TAG "OMX-VDEC-1080P"
80 
81 #ifdef USE_ION
82 #include <linux/msm_ion.h>
83 //#include <binder/MemoryHeapIon.h>
84 //#else
85 #endif
86 
87 #ifdef USE_GBM
88 #include "gbm.h"
89 #include "gbm_priv.h"
90 #endif
91 
92 #ifndef NATIVE_BASE_DISABLE
93 #include <nativebase/nativebase.h>
94 #endif
95 #include <linux/videodev2.h>
96 #define VALID_TS(ts)      ((ts < LLONG_MAX)? true : false)
97 #include <poll.h>
98 #include "hevc_utils.h"
99 #define TIMEOUT 5000
100 #endif // _ANDROID_
101 
102 #if defined (_ANDROID_HONEYCOMB_) || defined (_ANDROID_ICS_)
103 #define STRINGIFY_ENUMS
104 #include <media/hardware/HardwareAPI.h>
105 #endif
106 
107 #include <unistd.h>
108 
109 #if defined (_ANDROID_ICS_)
110 #include <gralloc_priv.h>
111 #endif
112 
113 #include <pthread.h>
114 #ifndef PC_DEBUG
115 #include <semaphore.h>
116 #endif
117 #include "OMX_Core.h"
118 #include "OMX_QCOMExtns.h"
119 #include "OMX_Skype_VideoExtensions.h"
120 #include "OMX_VideoExt.h"
121 #include "OMX_IndexExt.h"
122 #include "qc_omx_component.h"
123 #include "media/msm_vidc_utils.h"
124 #include "frameparser.h"
125 #include "extra_data_handler.h"
126 #include "ts_parser.h"
127 #include "vidc_debug.h"
128 #include "vidc_common.h"
129 #include "vidc_vendor_extensions.h"
130 #ifdef _ANDROID_
131 #include <cutils/properties.h>
132 #else
133 #define PROPERTY_VALUE_MAX 92
134 #endif
135 extern "C" {
136     OMX_API void * get_omx_component_factory_fn(void);
137 }
138 
139 //////////////////////////////////////////////////////////////////////////////
140 //                       Module specific globals
141 //////////////////////////////////////////////////////////////////////////////
142 #define OMX_SPEC_VERSION  0x00000101
143 #define OMX_INIT_STRUCT(_s_, _name_)         \
144     memset((_s_), 0x0, sizeof(_name_));      \
145 (_s_)->nSize = sizeof(_name_);               \
146 (_s_)->nVersion.nVersion = OMX_SPEC_VERSION  \
147 
148 
149 //////////////////////////////////////////////////////////////////////////////
150 //               Macros
151 //////////////////////////////////////////////////////////////////////////////
152 #define PrintFrameHdr(bufHdr) DEBUG_PRINT("bufHdr %x buf %x size %d TS %d\n",\
153         (unsigned) bufHdr,\
154         (unsigned)((OMX_BUFFERHEADERTYPE *)bufHdr)->pBuffer,\
155         (unsigned)((OMX_BUFFERHEADERTYPE *)bufHdr)->nFilledLen,\
156         (unsigned)((OMX_BUFFERHEADERTYPE *)bufHdr)->nTimeStamp)
157 
158 #define OMX_CORE_CONTROL_CMDQ_SIZE   100
159 #define OMX_CORE_QCIF_HEIGHT         144
160 #define OMX_CORE_QCIF_WIDTH          176
161 #define OMX_CORE_VGA_HEIGHT          480
162 #define OMX_CORE_VGA_WIDTH           640
163 #define OMX_CORE_WVGA_HEIGHT         480
164 #define OMX_CORE_WVGA_WIDTH          800
165 #define OMX_CORE_FWVGA_HEIGHT        480
166 #define OMX_CORE_FWVGA_WIDTH         864
167 
168 #define DESC_BUFFER_SIZE (8192 * 16)
169 
170 #ifdef _ANDROID_
171 #define MAX_NUM_INPUT_OUTPUT_BUFFERS 64
172 #endif
173 
174 // Aligning to MAX NUM INPUT & OUTPUT
175 #define MIN_NUM_INPUT_OUTPUT_EXTRADATA_BUFFERS MAX_NUM_INPUT_OUTPUT_BUFFERS
176 
177 #define OMX_FRAMEINFO_EXTRADATA 0x00010000
178 #define OMX_INTERLACE_EXTRADATA 0x00020000
179 #define OMX_TIMEINFO_EXTRADATA  0x00040000
180 #define OMX_PORTDEF_EXTRADATA   0x00080000
181 #define OMX_EXTNUSER_EXTRADATA  0x00100000
182 #define OMX_FRAMEDIMENSION_EXTRADATA  0x00200000
183 #define OMX_FRAMEPACK_EXTRADATA 0x00400000
184 #define OMX_QP_EXTRADATA        0x00800000
185 #define OMX_BITSINFO_EXTRADATA  0x01000000
186 #define OMX_VQZIPSEI_EXTRADATA  0x02000000
187 #define OMX_OUTPUTCROP_EXTRADATA 0x04000000
188 #define OMX_MB_ERROR_MAP_EXTRADATA 0x08000000
189 
190 #define OMX_VUI_DISPLAY_INFO_EXTRADATA  0x08000000
191 #define OMX_MPEG2_SEQDISP_INFO_EXTRADATA 0x10000000
192 #define OMX_VPX_COLORSPACE_INFO_EXTRADATA  0x20000000
193 #define OMX_VC1_SEQDISP_INFO_EXTRADATA  0x40000000
194 #define OMX_DISPLAY_INFO_EXTRADATA  0x80000000
195 #define OMX_HDR_COLOR_INFO_EXTRADATA  0x100000000
196 #define OMX_UBWC_CR_STATS_INFO_EXTRADATA  0x200000000
197 #define DRIVER_EXTRADATA_MASK   0x0000FFFF
198 
199 #define OMX_INTERLACE_EXTRADATA_SIZE ((sizeof(OMX_OTHER_EXTRADATATYPE) +\
200             sizeof(OMX_STREAMINTERLACEFORMAT) + 3)&(~3))
201 #define OMX_FRAMEINFO_EXTRADATA_SIZE ((sizeof(OMX_OTHER_EXTRADATATYPE) +\
202             sizeof(OMX_QCOM_EXTRADATA_FRAMEINFO) + 3)&(~3))
203 #define OMX_PORTDEF_EXTRADATA_SIZE ((sizeof(OMX_OTHER_EXTRADATATYPE) +\
204             sizeof(OMX_PARAM_PORTDEFINITIONTYPE) + 3)&(~3))
205 #define OMX_FRAMEDIMENSION_EXTRADATA_SIZE (sizeof(OMX_OTHER_EXTRADATATYPE) +\
206             sizeof(OMX_QCOM_EXTRADATA_FRAMEDIMENSION) + 3)&(~3)
207 #define OMX_FRAMEPACK_EXTRADATA_SIZE ((sizeof(OMX_OTHER_EXTRADATATYPE) +\
208             sizeof(OMX_QCOM_FRAME_PACK_ARRANGEMENT) + 3)&(~3))
209 #define OMX_QP_EXTRADATA_SIZE ((sizeof(OMX_OTHER_EXTRADATATYPE) +\
210             sizeof(OMX_QCOM_EXTRADATA_QP) + 3)&(~3))
211 #define OMX_BITSINFO_EXTRADATA_SIZE ((sizeof(OMX_OTHER_EXTRADATATYPE) +\
212             sizeof(OMX_QCOM_EXTRADATA_BITS_INFO) + 3)&(~3))
213 #define OMX_VQZIPSEI_EXTRADATA_SIZE ((sizeof(OMX_OTHER_EXTRADATATYPE) +\
214             sizeof(OMX_QCOM_EXTRADATA_VQZIPSEI) + 3)&(~3))
215 #define OMX_USERDATA_EXTRADATA_SIZE ((sizeof(OMX_OTHER_EXTRADATATYPE) +\
216             + 3)&(~3))
217 #define OMX_OUTPUTCROP_EXTRADATA_SIZE ((sizeof(OMX_OTHER_EXTRADATATYPE) +\
218             sizeof(OMX_QCOM_OUTPUT_CROP) + 3)&(~3))
219 
220 /* STATUS CODES */
221 /* Base value for status codes */
222 #define VDEC_S_BASE	0x40000000
223 /* Success */
224 #define VDEC_S_SUCCESS	(VDEC_S_BASE)
225 /* General failure */
226 #define VDEC_S_EFAIL	(VDEC_S_BASE + 1)
227 /* Fatal irrecoverable  failure. Need to  tear down session. */
228 #define VDEC_S_EFATAL   (VDEC_S_BASE + 2)
229 /* Error with input bistream */
230 #define VDEC_S_INPUT_BITSTREAM_ERR (VDEC_S_BASE + 3)
231 
232 #define VDEC_MSG_BASE	0x0000000
233 /* Codes to identify asynchronous message responses and events that driver
234   wants to communicate to the app.*/
235 #define VDEC_MSG_RESP_INPUT_BUFFER_DONE	(VDEC_MSG_BASE + 1)
236 #define VDEC_MSG_RESP_OUTPUT_BUFFER_DONE	(VDEC_MSG_BASE + 2)
237 #define VDEC_MSG_RESP_INPUT_FLUSHED	(VDEC_MSG_BASE + 3)
238 #define VDEC_MSG_RESP_OUTPUT_FLUSHED	(VDEC_MSG_BASE + 4)
239 #define VDEC_MSG_RESP_FLUSH_INPUT_DONE	(VDEC_MSG_BASE + 5)
240 #define VDEC_MSG_RESP_FLUSH_OUTPUT_DONE	(VDEC_MSG_BASE + 6)
241 #define VDEC_MSG_RESP_START_DONE	(VDEC_MSG_BASE + 7)
242 #define VDEC_MSG_RESP_STOP_DONE	(VDEC_MSG_BASE + 8)
243 #define VDEC_MSG_RESP_PAUSE_DONE	(VDEC_MSG_BASE + 9)
244 #define VDEC_MSG_RESP_RESUME_DONE	(VDEC_MSG_BASE + 10)
245 #define VDEC_MSG_EVT_CONFIG_CHANGED	(VDEC_MSG_BASE + 11)
246 #define VDEC_MSG_EVT_HW_ERROR	(VDEC_MSG_BASE + 12)
247 #define VDEC_MSG_EVT_INFO_FIELD_DROPPED	(VDEC_MSG_BASE + 13)
248 #define VDEC_MSG_EVT_HW_OVERLOAD	(VDEC_MSG_BASE + 14)
249 #define VDEC_MSG_EVT_MAX_CLIENTS	(VDEC_MSG_BASE + 15)
250 #define VDEC_MSG_EVT_HW_UNSUPPORTED	(VDEC_MSG_BASE + 16)
251 
252 
253 //  Define next macro with required values to enable default extradata,
254 //    VDEC_EXTRADATA_MB_ERROR_MAP
255 //    OMX_INTERLACE_EXTRADATA
256 //    OMX_FRAMEINFO_EXTRADATA
257 //    OMX_TIMEINFO_EXTRADATA
258 
259 //#define DEFAULT_EXTRADATA (OMX_FRAMEINFO_EXTRADATA|OMX_INTERLACE_EXTRADATA)
260 
261 using namespace android;
262 
263 enum port_indexes {
264     OMX_CORE_INPUT_PORT_INDEX        =0,
265     OMX_CORE_OUTPUT_PORT_INDEX       =1,
266     OMX_CORE_INPUT_EXTRADATA_INDEX   =2,
267     OMX_CORE_OUTPUT_EXTRADATA_INDEX  =3
268 };
269 
270 enum arb_mode_codecs {
271         VDEC_ARB_CODEC_H264 = 0x1,
272         VDEC_ARB_CODEC_HEVC = 0x2,
273         VDEC_ARB_CODEC_MPEG2 = 0x4,
274 };
275 
276 enum vdec_codec {
277 	VDEC_CODECTYPE_H264 = 0x1,
278 	VDEC_CODECTYPE_H263 = 0x2,
279 	VDEC_CODECTYPE_MPEG4 = 0x3,
280 	VDEC_CODECTYPE_DIVX_3 = 0x4,
281 	VDEC_CODECTYPE_DIVX_4 = 0x5,
282 	VDEC_CODECTYPE_DIVX_5 = 0x6,
283 	VDEC_CODECTYPE_DIVX_6 = 0x7,
284 	VDEC_CODECTYPE_XVID = 0x8,
285 	VDEC_CODECTYPE_MPEG1 = 0x9,
286 	VDEC_CODECTYPE_MPEG2 = 0xa,
287 	VDEC_CODECTYPE_VC1 = 0xb,
288 	VDEC_CODECTYPE_VC1_RCV = 0xc,
289 	VDEC_CODECTYPE_HEVC = 0xd,
290 	VDEC_CODECTYPE_MVC = 0xe,
291 	VDEC_CODECTYPE_VP8 = 0xf,
292 	VDEC_CODECTYPE_VP9 = 0x10,
293 };
294 
295 enum vdec_output_format {
296 	VDEC_YUV_FORMAT_NV12 = 0x1,
297 	VDEC_YUV_FORMAT_TILE_4x2 = 0x2,
298 	VDEC_YUV_FORMAT_NV12_UBWC = 0x3,
299 	VDEC_YUV_FORMAT_NV12_TP10_UBWC = 0x4,
300 	VDEC_YUV_FORMAT_P010_VENUS = 0x5,
301 };
302 
303 enum vdec_interlaced_format {
304     VDEC_InterlaceFrameProgressive = 0x1,
305     VDEC_InterlaceInterleaveFrameTopFieldFirst = 0x2,
306     VDEC_InterlaceInterleaveFrameBottomFieldFirst = 0x4,
307     VDEC_InterlaceFrameTopFieldFirst = 0x8,
308     VDEC_InterlaceFrameBottomFieldFirst = 0x10,
309 };
310 
311 enum vdec_output_order {
312 	VDEC_ORDER_DISPLAY = 0x1,
313 	VDEC_ORDER_DECODE = 0x2
314 };
315 
316 struct vdec_framesize {
317 	uint32_t   left;
318 	uint32_t   top;
319 	uint32_t   right;
320 	uint32_t   bottom;
321 };
322 
323 struct vdec_picsize {
324 	uint32_t frame_width;
325 	uint32_t frame_height;
326 	uint32_t stride;
327 	uint32_t scan_lines;
328 };
329 
330 enum vdec_buffer {
331 	VDEC_BUFFER_TYPE_INPUT,
332 	VDEC_BUFFER_TYPE_OUTPUT
333 };
334 
335 struct vdec_allocatorproperty {
336 	enum vdec_buffer buffer_type;
337 	uint32_t mincount;
338 	uint32_t maxcount;
339 	uint32_t actualcount;
340 	size_t buffer_size;
341 	uint32_t alignment;
342 	uint32_t buf_poolid;
343 	size_t meta_buffer_size;
344 };
345 
346 struct vdec_bufferpayload {
347 	void *bufferaddr;
348 	size_t buffer_len;
349 	int pmem_fd;
350 	size_t offset;
351 	size_t mmaped_size;
352 };
353 
354 enum vdec_picture {
355 	PICTURE_TYPE_I,
356 	PICTURE_TYPE_P,
357 	PICTURE_TYPE_B,
358 	PICTURE_TYPE_BI,
359 	PICTURE_TYPE_SKIP,
360 	PICTURE_TYPE_IDR,
361 	PICTURE_TYPE_UNKNOWN
362 };
363 
364 struct vdec_aspectratioinfo {
365 	uint32_t aspect_ratio;
366 	uint32_t par_width;
367 	uint32_t par_height;
368 };
369 
370 struct vdec_sep_metadatainfo {
371 	void *metabufaddr;
372 	uint32_t size;
373 	int fd;
374 	int offset;
375 	uint32_t buffer_size;
376 };
377 
378 struct vdec_misrinfo {
379         uint32_t misr_dpb_luma;
380         uint32_t misr_dpb_chroma;
381         uint32_t misr_opb_luma;
382         uint32_t misr_opb_chroma;
383 };
384 
385 struct vdec_output_frameinfo {
386 	void *bufferaddr;
387 	size_t offset;
388 	size_t len;
389 	uint32_t flags;
390 	int64_t time_stamp;
391 	enum vdec_picture pic_type;
392 	void *client_data;
393 	void *input_frame_clientdata;
394 	struct vdec_picsize picsize;
395 	struct vdec_framesize framesize;
396 	enum vdec_interlaced_format interlaced_format;
397 	struct vdec_aspectratioinfo aspect_ratio_info;
398 	struct vdec_sep_metadatainfo metadata_info;
399         struct vdec_misrinfo misrinfo[2];
400 };
401 
402 union vdec_msgdata {
403 	struct vdec_output_frameinfo output_frame;
404 	void *input_frame_clientdata;
405 };
406 
407 struct vdec_msginfo {
408 	uint32_t status_code;
409 	uint32_t msgcode;
410 	union vdec_msgdata msgdata;
411 	size_t msgdatasize;
412 };
413 
414 struct vdec_framerate {
415 	unsigned long fps_denominator;
416 	unsigned long fps_numerator;
417 };
418 
419 struct hdr10plusInfo {
420     bool is_new;
421     unsigned int cookie;
422     OMX_TICKS timestamp;
423     OMX_U32 nSize;
424     OMX_VERSIONTYPE nVersion;
425     OMX_U32 nPortIndex;
426     OMX_U32 nParamSize;
427     OMX_U32 nParamSizeUsed;
428     OMX_U8 payload[MAX_HDR10PLUSINFO_SIZE];
429 };
430 
431 #ifdef USE_ION
432 struct vdec_ion {
433     int dev_fd;
434     struct ion_allocation_data alloc_data;
435     int data_fd;
436 };
437 #endif
438 
439 #ifdef USE_GBM
440 struct vdec_gbm {
441     int gbm_device_fd;
442     struct gbm_device *gbm;
443     struct gbm_bo *bo;
444     unsigned long bo_fd;
445     unsigned long meta_fd;
446 };
447 #endif
448 
449 
450 struct extradata_buffer_info {
451     unsigned long buffer_size;
452     char* uaddr;
453     int count;
454     int size;
455 #ifdef USE_ION
456     struct vdec_ion ion;
457 #endif
458 };
459 
460 struct video_driver_context {
461     int video_driver_fd;
462     enum vdec_codec decoder_format;
463     enum vdec_output_format output_format;
464     enum vdec_interlaced_format interlace;
465     enum vdec_output_order picture_order;
466     struct vdec_framesize frame_size;
467     struct vdec_picsize video_resolution;
468     struct vdec_allocatorproperty ip_buf;
469     struct vdec_allocatorproperty op_buf;
470     struct vdec_bufferpayload *ptr_inputbuffer;
471     struct vdec_bufferpayload *ptr_outputbuffer;
472     struct vdec_output_frameinfo *ptr_respbuffer;
473     struct vdec_bufferpayload *ptr_intermediate_outputbuffer;
474     struct vdec_output_frameinfo *ptr_intermediate_respbuffer;
475 #ifdef USE_ION
476     struct vdec_ion *ip_buf_ion_info;
477     struct vdec_ion *op_buf_ion_info;
478     struct vdec_ion *op_intermediate_buf_ion_info;
479     struct vdec_ion h264_mv;
480     struct vdec_ion meta_buffer;
481     struct vdec_ion meta_buffer_iommu;
482 #endif
483 #ifdef USE_GBM
484     int gbm_device_fd;
485     struct vdec_gbm *op_buf_gbm_info;
486     struct vdec_gbm *op_intermediate_buf_gbm_info;
487 #endif
488     struct vdec_framerate frame_rate;
489     unsigned extradata;
490     bool timestamp_adjust;
491     char kind[128];
492     bool idr_only_decoding;
493     unsigned disable_dmx;
494     struct extradata_buffer_info extradata_info;
495     int num_planes;
496 };
497 
498 struct video_decoder_capability {
499     unsigned int min_width;
500     unsigned int max_width;
501     unsigned int min_height;
502     unsigned int max_height;
503 };
504 
505 struct debug_cap {
506     bool in_buffer_log;
507     bool out_buffer_log;
508     bool out_cc_buffer_log;
509     bool out_meta_buffer_log;
510     char infile_name[PROPERTY_VALUE_MAX + 36];
511     char outfile_name[PROPERTY_VALUE_MAX + 36];
512     char ccoutfile_name[PROPERTY_VALUE_MAX + 36];
513     char out_ymetafile_name[PROPERTY_VALUE_MAX + 36];
514     char out_uvmetafile_name[PROPERTY_VALUE_MAX + 36];
515     char log_loc[PROPERTY_VALUE_MAX];
516     FILE *infile;
517     FILE *outfile;
518     FILE *ccoutfile;
519     FILE *out_ymeta_file;
520     FILE *out_uvmeta_file;
521     int64_t session_id;
522     int seq_count;
523 };
524 
525 struct dynamic_buf_list {
526     long fd;
527     long dup_fd;
528     OMX_U32 offset;
529     OMX_U32 ref_count;
530     void *buffaddr;
531     long mapped_size;
532 };
533 
534 struct extradata_info {
535     OMX_BOOL output_crop_updated;
536     OMX_CONFIG_RECTTYPE output_crop_rect;
537     OMX_U32 output_width;
538     OMX_U32 output_height;
539     OMX_QCOM_MISR_INFO misr_info[2];
540 };
541 
542 struct reconfig_client_data {
543     OMX_U32 width;
544     OMX_U32 height;
545     OMX_U32 dpb_bit_depth;
546     OMX_U32 m_progressive;
547     bool isPortReconfigInsufficient;
548 };
549 
550 struct reconfig_client_crop_data {
551     OMX_U32 width;
552     OMX_U32 height;
553     OMX_U32 left;
554     OMX_U32 top;
555     bool isPortReconfigInsufficient;
556 };
557 
558 typedef std::unordered_map <int, int> ColorSubMapping;
559 typedef std::unordered_map <int, ColorSubMapping> DecColorMapping;
560 typedef std::unordered_map <enum ColorAspects::Primaries, ColorPrimaries> PrimariesMap;
561 typedef std::unordered_map <enum ColorAspects::Transfer, GammaTransfer> TransferMap;
562 typedef std::unordered_map <enum ColorAspects::MatrixCoeffs, MatrixCoEfficients> MatrixCoeffMap;
563 typedef std::unordered_map <enum ColorAspects::Range, ColorRange> RangeMap;
564 
565 // OMX video decoder class
566 class omx_vdec: public qc_omx_component
567 {
568 
569     public:
570         omx_vdec();  // constructor
571         virtual ~omx_vdec();  // destructor
572 
573         static int async_message_process (void *context, void* message);
574         static void process_event_cb(void *ctxt);
575 
576         OMX_ERRORTYPE allocate_buffer(
577                 OMX_HANDLETYPE hComp,
578                 OMX_BUFFERHEADERTYPE **bufferHdr,
579                 OMX_U32 port,
580                 OMX_PTR appData,
581                 OMX_U32 bytes
582                 );
583 
584 
585         OMX_ERRORTYPE component_deinit(OMX_HANDLETYPE hComp);
586 
587         OMX_ERRORTYPE component_init(OMX_STRING role);
588 
589         OMX_ERRORTYPE component_role_enum(
590                 OMX_HANDLETYPE hComp,
591                 OMX_U8 *role,
592                 OMX_U32 index
593                 );
594 
595         OMX_ERRORTYPE component_tunnel_request(
596                 OMX_HANDLETYPE hComp,
597                 OMX_U32 port,
598                 OMX_HANDLETYPE  peerComponent,
599                 OMX_U32 peerPort,
600                 OMX_TUNNELSETUPTYPE *tunnelSetup
601                 );
602 
603         OMX_ERRORTYPE empty_this_buffer(
604                 OMX_HANDLETYPE hComp,
605                 OMX_BUFFERHEADERTYPE *buffer
606                 );
607 
608 
609 
610         OMX_ERRORTYPE fill_this_buffer(
611                 OMX_HANDLETYPE hComp,
612                 OMX_BUFFERHEADERTYPE *buffer
613                 );
614 
615 
616         OMX_ERRORTYPE free_buffer(
617                 OMX_HANDLETYPE hComp,
618                 OMX_U32 port,
619                 OMX_BUFFERHEADERTYPE *buffer
620                 );
621 
622         OMX_ERRORTYPE get_component_version(
623                 OMX_HANDLETYPE hComp,
624                 OMX_STRING componentName,
625                 OMX_VERSIONTYPE *componentVersion,
626                 OMX_VERSIONTYPE *specVersion,
627                 OMX_UUIDTYPE *componentUUID
628                 );
629 
630         OMX_ERRORTYPE get_config(
631                 OMX_HANDLETYPE hComp,
632                 OMX_INDEXTYPE configIndex,
633                 OMX_PTR configData
634                 );
635 
636         OMX_ERRORTYPE get_extension_index(
637                 OMX_HANDLETYPE hComp,
638                 OMX_STRING paramName,
639                 OMX_INDEXTYPE *indexType
640                 );
641 
642         OMX_ERRORTYPE get_parameter(OMX_HANDLETYPE hComp,
643                 OMX_INDEXTYPE  paramIndex,
644                 OMX_PTR        paramData);
645 
646         OMX_ERRORTYPE get_state(OMX_HANDLETYPE hComp,
647                 OMX_STATETYPE *state);
648 
649 
650 
651         OMX_ERRORTYPE send_command(OMX_HANDLETYPE  hComp,
652                 OMX_COMMANDTYPE cmd,
653                 OMX_U32         param1,
654                 OMX_PTR         cmdData);
655 
656 
657         OMX_ERRORTYPE set_callbacks(OMX_HANDLETYPE   hComp,
658                 OMX_CALLBACKTYPE *callbacks,
659                 OMX_PTR          appData);
660 
661         OMX_ERRORTYPE set_config(OMX_HANDLETYPE hComp,
662                 OMX_INDEXTYPE  configIndex,
663                 OMX_PTR        configData);
664 
665         OMX_ERRORTYPE set_parameter(OMX_HANDLETYPE hComp,
666                 OMX_INDEXTYPE  paramIndex,
667                 OMX_PTR        paramData);
668 
669         OMX_ERRORTYPE use_buffer(OMX_HANDLETYPE      hComp,
670                 OMX_BUFFERHEADERTYPE **bufferHdr,
671                 OMX_U32              port,
672                 OMX_PTR              appData,
673                 OMX_U32              bytes,
674                 OMX_U8               *buffer);
675 
676         OMX_ERRORTYPE  use_input_heap_buffers(
677                 OMX_HANDLETYPE            hComp,
678                 OMX_BUFFERHEADERTYPE** bufferHdr,
679                 OMX_U32                   port,
680                 OMX_PTR                   appData,
681                 OMX_U32                   bytes,
682                 OMX_U8*                   buffer);
683 
684         OMX_ERRORTYPE use_EGL_image(OMX_HANDLETYPE     hComp,
685                 OMX_BUFFERHEADERTYPE **bufferHdr,
686                 OMX_U32              port,
687                 OMX_PTR              appData,
688                 void *               eglImage);
689         void complete_pending_buffer_done_cbs();
690         struct video_driver_context drv_ctx;
691         int m_poll_efd;
692         char *ion_map(int fd, int len);
693         OMX_ERRORTYPE ion_unmap(int fd, void *bufaddr, int len);
694         OMX_ERRORTYPE allocate_extradata();
695         void free_extradata();
696         int update_resolution(int width, int height, int stride, int scan_lines);
697         Signal signal;
698         pthread_t msg_thread_id;
699         pthread_t async_thread_id;
700         bool is_component_secure();
701         OMX_BUFFERHEADERTYPE* get_omx_output_buffer_header(int index);
702         OMX_ERRORTYPE set_dpb(bool is_split_mode);
703         OMX_ERRORTYPE decide_dpb_buffer_mode();
704         int dpb_bit_depth;
705         bool check_supported_flexible_formats(OMX_COLOR_FORMATTYPE required_format);
706         bool is_flexible_format;//To save status if required format is flexible color formats
707         bool async_thread_force_stop;
708         volatile bool message_thread_stop;
709         struct extradata_info m_extradata_info;
710         int m_progressive;
711         bool is_mbaff;
712 
713         enum dither_type {
714             DITHER_DISABLE = 0,
715             DITHER_COLORSPACE_EXCEPTBT2020,
716             DITHER_ALL_COLORSPACE
717         };
718         enum dither_type m_dither_config;
719 
720         enum color_space_type {
721             BT2020 = 0,
722             EXCEPT_BT2020,
723             UNKNOWN
724         };
725         enum color_space_type m_color_space;
726 
727         /*
728          * Class variables to track client set profile and level
729          * via OMX_QTIIndexParamClientConfiguredMaxProfileLevelForSufficiency
730          */
731         bool mClientSessionForSufficiency;
732         bool isPortReconfigInsufficient;
733         OMX_U32 mClientSetProfile;
734         OMX_U32 mClientSetLevel;
735         inline int get_session_codec_type();
736 
737     private:
738         // Bit Positions
739         enum flags_bit_positions {
740             // Defer transition to IDLE
741             OMX_COMPONENT_IDLE_PENDING            =0x1,
742             // Defer transition to LOADING
743             OMX_COMPONENT_LOADING_PENDING         =0x2,
744             // First  Buffer Pending
745             OMX_COMPONENT_FIRST_BUFFER_PENDING    =0x3,
746             // Second Buffer Pending
747             OMX_COMPONENT_SECOND_BUFFER_PENDING   =0x4,
748             // Defer transition to Enable
749             OMX_COMPONENT_INPUT_ENABLE_PENDING    =0x5,
750             // Defer transition to Enable
751             OMX_COMPONENT_OUTPUT_ENABLE_PENDING   =0x6,
752             // Defer transition to Disable
753             OMX_COMPONENT_INPUT_DISABLE_PENDING   =0x7,
754             // Defer transition to Disable
755             OMX_COMPONENT_OUTPUT_DISABLE_PENDING  =0x8,
756             //defer flush notification
757             OMX_COMPONENT_OUTPUT_FLUSH_PENDING    =0x9,
758             OMX_COMPONENT_INPUT_FLUSH_PENDING    =0xA,
759             OMX_COMPONENT_PAUSE_PENDING          =0xB,
760             OMX_COMPONENT_EXECUTE_PENDING        =0xC,
761             OMX_COMPONENT_OUTPUT_FLUSH_IN_DISABLE_PENDING =0xD,
762             OMX_COMPONENT_DISABLE_OUTPUT_DEFERRED=0xE,
763             OMX_COMPONENT_FLUSH_DEFERRED = 0xF
764         };
765 
766         // Deferred callback identifiers
767         enum {
768             //Event Callbacks from the vdec component thread context
769             OMX_COMPONENT_GENERATE_EVENT       = 0x1,
770             //Buffer Done callbacks from the vdec component thread context
771             OMX_COMPONENT_GENERATE_BUFFER_DONE = 0x2,
772             //Frame Done callbacks from the vdec component thread context
773             OMX_COMPONENT_GENERATE_FRAME_DONE  = 0x3,
774             //Buffer Done callbacks from the vdec component thread context
775             OMX_COMPONENT_GENERATE_FTB         = 0x4,
776             //Frame Done callbacks from the vdec component thread context
777             OMX_COMPONENT_GENERATE_ETB         = 0x5,
778             //Command
779             OMX_COMPONENT_GENERATE_COMMAND     = 0x6,
780             //Push-Pending Buffers
781             OMX_COMPONENT_PUSH_PENDING_BUFS    = 0x7,
782             // Empty Buffer Done callbacks
783             OMX_COMPONENT_GENERATE_EBD         = 0x8,
784             //Flush Event Callbacks from the vdec component thread context
785             OMX_COMPONENT_GENERATE_EVENT_FLUSH       = 0x9,
786             OMX_COMPONENT_GENERATE_EVENT_INPUT_FLUSH = 0x0A,
787             OMX_COMPONENT_GENERATE_EVENT_OUTPUT_FLUSH = 0x0B,
788             OMX_COMPONENT_GENERATE_FBD = 0xC,
789             OMX_COMPONENT_GENERATE_START_DONE = 0xD,
790             OMX_COMPONENT_GENERATE_PAUSE_DONE = 0xE,
791             OMX_COMPONENT_GENERATE_RESUME_DONE = 0xF,
792             OMX_COMPONENT_GENERATE_STOP_DONE = 0x10,
793             OMX_COMPONENT_GENERATE_HARDWARE_ERROR = 0x11,
794             OMX_COMPONENT_GENERATE_ETB_ARBITRARY = 0x12,
795             OMX_COMPONENT_GENERATE_PORT_RECONFIG = 0x13,
796             OMX_COMPONENT_GENERATE_EOS_DONE = 0x14,
797             OMX_COMPONENT_GENERATE_INFO_PORT_RECONFIG = 0x15,
798             OMX_COMPONENT_GENERATE_INFO_FIELD_DROPPED = 0x16,
799             OMX_COMPONENT_GENERATE_UNSUPPORTED_SETTING = 0x17,
800             OMX_COMPONENT_GENERATE_HARDWARE_OVERLOAD = 0x18,
801             OMX_COMPONENT_GENERATE_ION_PREFETCH_PIXEL = 0x19,
802             OMX_COMPONENT_GENERATE_ION_PREFETCH_NON_PIXEL = 0x1A,
803             OMX_COMPONENT_CLOSE_MSG = 0x1B
804         };
805 
806         enum vc1_profile_type {
807             VC1_SP_MP_RCV = 1,
808             VC1_AP = 2
809         };
810 
811         enum v4l2_ports {
812             CAPTURE_PORT,
813             OUTPUT_PORT,
814             MAX_PORT
815         };
816 
817         struct omx_event {
818             unsigned long param1;
819             unsigned long param2;
820             unsigned long id;
821         };
822 
823         struct omx_cmd_queue {
824             omx_event m_q[OMX_CORE_CONTROL_CMDQ_SIZE];
825             unsigned long m_read;
826             unsigned long m_write;
827             unsigned long m_size;
828 
829             omx_cmd_queue();
830             ~omx_cmd_queue();
831             bool insert_entry(unsigned long p1, unsigned long p2, unsigned long id);
832             bool pop_entry(unsigned long *p1,unsigned long *p2, unsigned long *id);
833             // get msgtype of the first ele from the queue
834             unsigned get_q_msg_type();
835 
836         };
837         struct v4l2_capability cap;
838 #ifdef _ANDROID_
839         struct ts_entry {
840             OMX_TICKS timestamp;
841             bool valid;
842         };
843 
844         struct ts_arr_list {
845             ts_entry m_ts_arr_list[MAX_NUM_INPUT_OUTPUT_BUFFERS];
846 
847             ts_arr_list();
848             ~ts_arr_list();
849 
850             bool insert_ts(OMX_TICKS ts);
851             bool pop_min_ts(OMX_TICKS &ts);
852             bool reset_ts_list();
853         };
854 #endif
855 
856         struct desc_buffer_hdr {
857             OMX_U8 *buf_addr;
858             OMX_U32 desc_data_size;
859         };
860         bool allocate_done(void);
861         bool allocate_input_done(void);
862         bool allocate_output_done(void);
863         bool allocate_output_extradata_done(void);
864 
865         OMX_ERRORTYPE free_input_buffer(OMX_BUFFERHEADERTYPE *bufferHdr);
866         OMX_ERRORTYPE free_input_buffer(unsigned int bufferindex,
867                 OMX_BUFFERHEADERTYPE *pmem_bufferHdr);
868         OMX_ERRORTYPE free_output_buffer(OMX_BUFFERHEADERTYPE *bufferHdr,
869                                          bool                 intermediate=false);
870         void free_output_buffer_header(bool intermediate=false);
871         void free_input_buffer_header();
872         void free_output_extradata_buffer_header();
873 
874         OMX_ERRORTYPE allocate_input_heap_buffer(OMX_HANDLETYPE       hComp,
875                 OMX_BUFFERHEADERTYPE **bufferHdr,
876                 OMX_U32              port,
877                 OMX_PTR              appData,
878                 OMX_U32              bytes);
879 
880 
881         OMX_ERRORTYPE allocate_input_buffer(OMX_HANDLETYPE       hComp,
882                 OMX_BUFFERHEADERTYPE **bufferHdr,
883                 OMX_U32              port,
884                 OMX_PTR              appData,
885                 OMX_U32              bytes);
886 
887         OMX_ERRORTYPE allocate_output_buffer(OMX_HANDLETYPE       hComp,
888                 OMX_BUFFERHEADERTYPE **bufferHdr,
889                 OMX_U32 port,OMX_PTR appData,
890                 OMX_U32              bytes,
891                 bool                 intermediate=false,
892                 int                  index=-1);
893         OMX_ERRORTYPE use_output_buffer(OMX_HANDLETYPE hComp,
894                 OMX_BUFFERHEADERTYPE   **bufferHdr,
895                 OMX_U32                port,
896                 OMX_PTR                appData,
897                 OMX_U32                bytes,
898                 OMX_U8                 *buffer);
899         OMX_ERRORTYPE use_client_output_extradata_buffer(OMX_HANDLETYPE hComp,
900                 OMX_BUFFERHEADERTYPE   **bufferHdr,
901                 OMX_U32                port,
902                 OMX_PTR                appData,
903                 OMX_U32                bytes,
904                 OMX_U8                 *buffer);
905         OMX_ERRORTYPE get_supported_profile_level(OMX_VIDEO_PARAM_PROFILELEVELTYPE *profileLevelType);
906 
907         OMX_ERRORTYPE allocate_desc_buffer(OMX_U32 index);
908         OMX_ERRORTYPE allocate_output_headers(bool intermediate=false);
909         OMX_ERRORTYPE allocate_client_output_extradata_headers();
910         bool execute_omx_flush(OMX_U32);
911         bool execute_output_flush();
912         bool execute_input_flush();
913         void notify_flush_done(void *ctxt);
914         OMX_ERRORTYPE empty_buffer_done(OMX_HANDLETYPE hComp,
915                 OMX_BUFFERHEADERTYPE * buffer);
916 
917         OMX_ERRORTYPE fill_buffer_done(OMX_HANDLETYPE hComp,
918                 OMX_BUFFERHEADERTYPE * buffer);
919         OMX_ERRORTYPE empty_this_buffer_proxy(OMX_HANDLETYPE       hComp,
920                 OMX_BUFFERHEADERTYPE *buffer);
921 
922         OMX_ERRORTYPE empty_this_buffer_proxy_arbitrary(OMX_HANDLETYPE hComp,
923                 OMX_BUFFERHEADERTYPE *buffer
924                 );
925 
926         OMX_ERRORTYPE push_input_buffer (OMX_HANDLETYPE hComp);
927         OMX_ERRORTYPE push_input_sc_codec (OMX_HANDLETYPE hComp);
928         OMX_ERRORTYPE push_input_h264 (OMX_HANDLETYPE hComp);
929         OMX_ERRORTYPE push_input_hevc (OMX_HANDLETYPE hComp);
930 
931         OMX_ERRORTYPE fill_this_buffer_proxy(OMX_HANDLETYPE       hComp,
932                 OMX_BUFFERHEADERTYPE *buffer);
933         bool release_done();
934 
935         bool release_output_done();
936         bool release_input_done();
937         bool release_output_extradata_done();
938         OMX_ERRORTYPE get_buffer_req(vdec_allocatorproperty *buffer_prop);
939         OMX_ERRORTYPE set_buffer_req(vdec_allocatorproperty *buffer_prop);
940         OMX_ERRORTYPE start_port_reconfig();
941         OMX_ERRORTYPE update_picture_resolution();
942         int stream_off(OMX_U32 port);
943         void adjust_timestamp(OMX_S64 &act_timestamp);
944         void set_frame_rate(OMX_S64 act_timestamp);
945         bool handle_extradata(OMX_BUFFERHEADERTYPE *p_buf_hdr);
946         void convert_color_space_info(OMX_U32 primaries, OMX_U32 range,
947             OMX_U32 transfer, OMX_U32 matrix,
948             ColorAspects *aspects);
949         bool handle_color_space_info(void *data);
950         void print_debug_color_aspects(ColorAspects *aspects, const char *prefix);
951         void print_debug_hdr_color_info(HDRStaticInfo *hdr_info, const char *prefix);
952         void print_debug_hdr_color_info_mdata(ColorMetaData* color_mdata);
953         void print_debug_hdr10plus_metadata(ColorMetaData& color_mdata);
954         bool handle_content_light_level_info(void* data);
955         bool handle_mastering_display_color_info(void* data);
956         void print_debug_extradata(OMX_OTHER_EXTRADATATYPE *extra);
957         void set_colormetadata_in_handle(ColorMetaData *color_mdata, unsigned int buf_index);
958         void prepare_color_aspects_metadata(OMX_U32 primaries, OMX_U32 range,
959                                             OMX_U32 transfer, OMX_U32 matrix,
960                                             ColorMetaData *color_mdata);
961         void append_interlace_extradata(OMX_OTHER_EXTRADATATYPE *extra,
962                 OMX_U32 interlaced_format_type);
963         OMX_ERRORTYPE enable_extradata(OMX_U64 requested_extradata, bool is_internal,
964                 bool enable = true);
965         void append_frame_info_extradata(OMX_OTHER_EXTRADATATYPE *extra,
966                 OMX_U32 num_conceal_mb,
967                 OMX_U32 recovery_sei_flag,
968                 OMX_U32 picture_type,
969                 OMX_U32 frame_rate,
970                 OMX_TICKS time_stamp,
971                 struct msm_vidc_panscan_window_payload *panscan_payload,
972                 struct vdec_aspectratioinfo *aspect_ratio_info);
973         void append_frame_info_extradata(OMX_OTHER_EXTRADATATYPE *extra,
974                 OMX_U32 num_conceal_mb,
975                 OMX_U32 recovery_sei_flag,
976                 OMX_U32 picture_type,
977                 OMX_S64 timestamp,
978                 OMX_U32 frame_rate,
979                 struct vdec_aspectratioinfo *aspect_ratio_info);
980         void fill_aspect_ratio_info(struct vdec_aspectratioinfo *aspect_ratio_info,
981                 OMX_QCOM_EXTRADATA_FRAMEINFO *frame_info);
982         void append_terminator_extradata(OMX_OTHER_EXTRADATATYPE *extra);
983         OMX_ERRORTYPE update_portdef(OMX_PARAM_PORTDEFINITIONTYPE *portDefn);
984         void append_portdef_extradata(OMX_OTHER_EXTRADATATYPE *extra);
985         void append_frame_dimension_extradata(OMX_OTHER_EXTRADATATYPE *extra);
986         void append_extn_extradata(OMX_OTHER_EXTRADATATYPE *extra, OMX_OTHER_EXTRADATATYPE *p_extn);
987         void append_user_extradata(OMX_OTHER_EXTRADATATYPE *extra, OMX_OTHER_EXTRADATATYPE *p_user);
988         void append_concealmb_extradata(OMX_OTHER_EXTRADATATYPE *extra,
989                 OMX_OTHER_EXTRADATATYPE *p_concealmb, OMX_U8 *conceal_mb_data);
990         void append_outputcrop_extradata(OMX_OTHER_EXTRADATATYPE *extra,
991                 struct msm_vidc_output_crop_payload *output_crop_payload);
992         void append_framepack_extradata(OMX_OTHER_EXTRADATATYPE *extra,
993                 struct msm_vidc_s3d_frame_packing_payload *s3d_frame_packing_payload);
994         void append_qp_extradata(OMX_OTHER_EXTRADATATYPE *extra,
995                 struct msm_vidc_frame_qp_payload *qp_payload);
996         void append_bitsinfo_extradata(OMX_OTHER_EXTRADATATYPE *extra,
997                 struct msm_vidc_frame_bits_info_payload *bits_payload);
998         void append_vqzip_extradata(OMX_OTHER_EXTRADATATYPE *extra,
999                 struct msm_vidc_vqzip_sei_payload *vqzip_payload);
1000         void insert_demux_addr_offset(OMX_U32 address_offset);
1001         void extract_demux_addr_offsets(OMX_BUFFERHEADERTYPE *buf_hdr);
1002         OMX_ERRORTYPE handle_demux_data(OMX_BUFFERHEADERTYPE *buf_hdr);
1003         OMX_U32 count_MB_in_extradata(OMX_OTHER_EXTRADATATYPE *extra);
1004 
1005         bool align_pmem_buffers(int pmem_fd, OMX_U32 buffer_size,
1006                 OMX_U32 alignment);
1007 #ifdef USE_ION
1008         bool alloc_map_ion_memory(OMX_U32 buffer_size, vdec_ion *ion_info, int flag);
1009         void free_ion_memory(struct vdec_ion *buf_ion_info);
1010         void do_cache_operations(int fd);
1011 #endif
1012 
1013 #ifdef USE_GBM
1014         bool alloc_map_gbm_memory(OMX_U32 w,OMX_U32 h,int gbm_device_fd,
1015               struct vdec_gbm *gbm_info, int flag);
1016         void free_gbm_memory(struct vdec_gbm *buf_gbm_info);
1017 #endif
1018 
1019 
1020 
1021         OMX_ERRORTYPE send_command_proxy(OMX_HANDLETYPE  hComp,
1022                 OMX_COMMANDTYPE cmd,
1023                 OMX_U32         param1,
1024                 OMX_PTR         cmdData);
1025         bool post_event( unsigned long p1,
1026                 unsigned long p2,
1027                 unsigned long id
1028                    );
clip2(int x)1029         inline int clip2(int x) {
1030             x = x -1;
1031             x = x | x >> 1;
1032             x = x | x >> 2;
1033             x = x | x >> 4;
1034             x = x | x >> 16;
1035             x = x + 1;
1036             return x;
1037         }
1038 
1039         OMX_ERRORTYPE vdec_alloc_h264_mv();
1040         void vdec_dealloc_h264_mv();
1041         OMX_ERRORTYPE vdec_alloc_meta_buffers();
1042         void vdec_dealloc_meta_buffers();
1043 
omx_report_error()1044         inline void omx_report_error () {
1045             if (m_cb.EventHandler && !m_error_propogated && m_state != OMX_StateLoaded) {
1046                 DEBUG_PRINT_ERROR("ERROR: Sending OMX_ErrorHardware to Client");
1047                 m_error_propogated = true;
1048                 m_cb.EventHandler(&m_cmp,m_app_data,
1049                         OMX_EventError,OMX_ErrorHardware,0,NULL);
1050             }
1051         }
1052 
omx_report_unsupported_setting()1053         inline void omx_report_unsupported_setting () {
1054             if (m_cb.EventHandler && !m_error_propogated && m_state != OMX_StateLoaded) {
1055                 DEBUG_PRINT_ERROR(
1056                         "ERROR: Sending OMX_ErrorUnsupportedSetting to Client");
1057                 m_error_propogated = true;
1058                 m_cb.EventHandler(&m_cmp, m_app_data,
1059                         OMX_EventError, OMX_ErrorUnsupportedSetting, 0, NULL);
1060             }
1061         }
omx_report_hw_overload()1062         inline void omx_report_hw_overload () {
1063             if (m_cb.EventHandler && !m_error_propogated && m_state != OMX_StateLoaded) {
1064                 DEBUG_PRINT_ERROR(
1065                         "ERROR: Sending OMX_ErrorInsufficientResources to Client");
1066                 m_error_propogated = true;
1067                 m_cb.EventHandler(&m_cmp, m_app_data,
1068                         OMX_EventError, OMX_ErrorInsufficientResources, 0, NULL);
1069             }
1070         }
1071 
1072 #if defined (_ANDROID_HONEYCOMB_) || defined (_ANDROID_ICS_)
1073         OMX_ERRORTYPE use_android_native_buffer(OMX_IN OMX_HANDLETYPE hComp, OMX_PTR data);
1074 #endif
1075 #if defined (_ANDROID_ICS_)
1076         struct nativebuffer {
1077             native_handle_t *nativehandle;
1078             private_handle_t *privatehandle;
1079             int inuse;
1080         };
1081         nativebuffer native_buffer[MAX_NUM_INPUT_OUTPUT_BUFFERS];
1082 #endif
1083 
1084         //*************************************************************
1085         //*******************MEMBER VARIABLES *************************
1086         //*************************************************************
1087         pthread_mutex_t       m_lock;
1088         pthread_mutex_t       c_lock;
1089         pthread_mutex_t       buf_lock;
1090         //sem to handle the minimum procesing of commands
1091         sem_t                 m_cmd_lock;
1092         sem_t                 m_safe_flush;
1093         bool              m_error_propogated;
1094         // compression format
1095         OMX_VIDEO_CODINGTYPE eCompressionFormat;
1096         // OMX State
1097         OMX_STATETYPE m_state;
1098         // Application data
1099         OMX_PTR m_app_data;
1100         // Application callbacks
1101         OMX_CALLBACKTYPE m_cb;
1102         OMX_PRIORITYMGMTTYPE m_priority_mgm ;
1103         OMX_PARAM_BUFFERSUPPLIERTYPE m_buffer_supplier;
1104         // fill this buffer queue
1105         omx_cmd_queue         m_ftb_q;
1106         // Command Q for rest of the events
1107         omx_cmd_queue         m_cmd_q;
1108         omx_cmd_queue         m_etb_q;
1109         // Input memory pointer
1110         OMX_BUFFERHEADERTYPE  *m_inp_mem_ptr;
1111         // Output memory pointer
1112         OMX_BUFFERHEADERTYPE  *m_out_mem_ptr;
1113         // Output memory pointer
1114         OMX_BUFFERHEADERTYPE  *m_intermediate_out_mem_ptr;
1115         // Client extradata memory pointer
1116         OMX_BUFFERHEADERTYPE  *m_client_output_extradata_mem_ptr;
1117         // number of input bitstream error frame count
1118         unsigned int m_inp_err_count;
1119 
1120         pthread_mutex_t m_hdr10pluslock;
1121         std::list<hdr10plusInfo> m_hdr10pluslist;
1122 #ifdef _ANDROID_
1123         // Timestamp list
1124         ts_arr_list           m_timestamp_list;
1125 #endif
1126 
1127         bool input_flush_progress;
1128         bool output_flush_progress;
1129         bool input_use_buffer;
1130         bool output_use_buffer;
1131         bool ouput_egl_buffers;
1132         OMX_BOOL m_use_output_pmem;
1133 
1134         int pending_input_buffers;
1135         int pending_output_buffers;
1136         // bitmask array size for output side
1137         uint64_t m_out_bm_count;
1138         // bitmask array size for input side
1139         uint64_t m_inp_bm_count;
1140         // bitmask array size for extradata
1141         uint64_t m_out_extradata_bm_count;
1142         //Input port Populated
1143         OMX_BOOL m_inp_bPopulated;
1144         //Output port Populated
1145         OMX_BOOL m_out_bPopulated;
1146         // encapsulate the waiting states.
1147         uint64_t m_flags;
1148 
1149         OMX_U32 m_etb_count;
1150         OMX_TICKS m_etb_timestamp;
1151         // store I/P PORT state
1152         OMX_BOOL m_inp_bEnabled;
1153         // store O/P PORT state
1154         OMX_BOOL m_out_bEnabled;
1155         OMX_U32 m_in_alloc_cnt;
1156         OMX_U8                m_cRole[OMX_MAX_STRINGNAME_SIZE];
1157         // Platform specific details
1158         OMX_QCOM_PLATFORM_PRIVATE_LIST      *m_platform_list;
1159         OMX_QCOM_PLATFORM_PRIVATE_ENTRY     *m_platform_entry;
1160         OMX_QCOM_PLATFORM_PRIVATE_PMEM_INFO *m_pmem_info;
1161         // SPS+PPS sent as part of set_config
1162         OMX_VENDOR_EXTRADATATYPE            m_vendor_config;
1163 
1164         /*Variables for arbitrary Byte parsing support*/
1165         frame_parse m_frame_parser;
1166         h264_stream_parser *h264_parser;
1167         HEVC_Utils m_hevc_utils;
1168 
1169         omx_cmd_queue m_input_pending_q;
1170         omx_cmd_queue m_input_free_q;
1171         bool arbitrary_bytes;
1172         OMX_BUFFERHEADERTYPE  h264_scratch;
1173         OMX_BUFFERHEADERTYPE  *psource_frame;
1174         OMX_BUFFERHEADERTYPE  *pdest_frame;
1175         OMX_BUFFERHEADERTYPE  *m_inp_heap_ptr;
1176         OMX_BUFFERHEADERTYPE  **m_phdr_pmem_ptr;
1177         unsigned int m_heap_inp_bm_count;
1178         codec_type codec_type_parse;
1179         bool first_frame_meta;
1180         unsigned frame_count;
1181         unsigned nal_count;
1182         unsigned nal_length;
1183         bool look_ahead_nal;
1184         int first_frame;
1185         unsigned char *first_buffer;
1186         int first_frame_size;
1187         unsigned char m_hwdevice_name[80];
1188         FILE *m_device_file_ptr;
1189         enum vc1_profile_type m_vc1_profile;
1190         OMX_S64 h264_last_au_ts;
1191         OMX_U32 h264_last_au_flags;
1192         OMX_U32 m_demux_offsets[8192];
1193         OMX_U32 m_demux_entries;
1194         OMX_U32 m_disp_hor_size;
1195         OMX_U32 m_disp_vert_size;
1196         OMX_S64 prev_ts;
1197         OMX_S64 prev_ts_actual;
1198         bool rst_prev_ts;
1199         OMX_U32 frm_int;
1200         OMX_U32 m_fps_received;
1201         float   m_fps_prev;
1202         bool m_drc_enable;
1203 
1204         struct vdec_allocatorproperty op_buf_rcnfg;
1205         bool in_reconfig;
1206         bool c2d_enable_pending;
1207         OMX_NATIVE_WINDOWTYPE m_display_id;
1208         OMX_U32 client_extradata;
1209 #ifdef _ANDROID_
1210         bool perf_flag;
1211         OMX_U32 proc_frms, latency;
1212         perf_metrics fps_metrics;
1213         perf_metrics dec_time;
1214         bool m_enable_android_native_buffers;
1215         bool m_use_android_native_buffers;
1216         bool m_debug_extradata;
1217         bool m_disable_dynamic_buf_mode;
1218         OMX_U32 m_conceal_color;
1219 #endif
1220 
1221 
1222         struct h264_mv_buffer {
1223             unsigned char* buffer;
1224             int size;
1225             int count;
1226             int pmem_fd;
1227             int offset;
1228         };
1229         h264_mv_buffer h264_mv_buff;
1230 
1231         struct meta_buffer {
1232             unsigned char* buffer;
1233             int size;
1234             int count;
1235             int pmem_fd;
1236             int pmem_fd_iommu;
1237             int offset;
1238         };
1239         meta_buffer meta_buff;
1240         OMX_PARAM_PORTDEFINITIONTYPE m_port_def;
1241         OMX_QCOM_FRAME_PACK_ARRANGEMENT m_frame_pack_arrangement;
1242         omx_time_stamp_reorder time_stamp_dts;
1243         desc_buffer_hdr *m_desc_buffer_ptr;
1244         bool secure_mode;
1245         bool allocate_native_handle;
1246         bool external_meta_buffer;
1247         bool external_meta_buffer_iommu;
1248         OMX_QCOM_EXTRADATA_FRAMEINFO *m_extradata;
1249         bool codec_config_flag;
1250         uint32_t capture_capability;
1251         int output_capability;
1252         bool streaming[MAX_PORT];
1253         OMX_FRAMESIZETYPE framesize;
1254         OMX_CONFIG_RECTTYPE rectangle;
1255         OMX_U32 prev_n_filled_len;
1256         bool m_force_down_scalar;
1257         struct custom_buffersize {
1258             OMX_U32 input_buffersize;
1259         } m_custom_buffersize;
1260         bool m_power_hinted;
1261         bool is_q6_platform;
1262         OMX_ERRORTYPE power_module_register();
1263         OMX_ERRORTYPE power_module_deregister();
1264         bool msg_thread_created;
1265         bool async_thread_created;
1266 
1267         OMX_VIDEO_PARAM_PROFILELEVELTYPE m_profile_lvl;
1268         QOMX_EXTNINDEX_VIDEO_LOW_LATENCY_MODE m_sParamLowLatency;
1269 
1270         //variables to handle dynamic buffer mode
1271         bool dynamic_buf_mode;
1272         OMX_U32 m_reconfig_width;
1273         OMX_U32 m_reconfig_height;
1274         bool m_smoothstreaming_mode;
1275         bool m_decode_order_mode;
1276         bool m_client_req_turbo_mode;
1277 
1278         bool m_input_pass_buffer_fd;
1279         DescribeColorAspectsParams m_client_color_space;
1280         DescribeColorAspectsParams m_internal_color_space;
1281 
1282         // HDRStaticInfo defined in HardwareAPI.h
1283         DescribeHDRStaticInfoParams m_client_hdr_info;
1284         DescribeHDRStaticInfoParams m_internal_hdr_info;
1285 
1286 
1287         OMX_U32 operating_frame_rate;
1288 
1289         OMX_U32 m_smoothstreaming_width;
1290         OMX_U32 m_smoothstreaming_height;
1291         OMX_ERRORTYPE enable_smoothstreaming();
1292         OMX_ERRORTYPE enable_adaptive_playback(unsigned long width, unsigned long height);
1293         bool m_disable_ubwc_mode;
1294         OMX_U32 m_downscalar_width;
1295         OMX_U32 m_downscalar_height;
1296         int decide_downscalar();
1297         int enable_downscalar();
1298         int disable_downscalar();
1299 
1300         unsigned int m_fill_output_msg;
1301         bool client_set_fps;
1302         unsigned int stereo_output_mode;
1303         class allocate_color_convert_buf
1304         {
1305             public:
1306                 allocate_color_convert_buf();
~allocate_color_convert_buf()1307                 ~allocate_color_convert_buf() {};
1308                 void set_vdec_client(void *);
1309                 void update_client();
1310                 bool set_color_format(OMX_COLOR_FORMATTYPE dest_color_format);
1311                 bool get_color_format(OMX_COLOR_FORMATTYPE &dest_color_format);
1312                 bool update_buffer_req();
1313                 bool get_buffer_req(unsigned int &buffer_size);
1314                 OMX_ERRORTYPE set_buffer_req(OMX_U32 buffer_size, OMX_U32 actual_count);
1315                 OMX_BUFFERHEADERTYPE* get_il_buf_hdr();
1316                 OMX_BUFFERHEADERTYPE* get_il_buf_hdr(OMX_BUFFERHEADERTYPE *input_hdr);
1317                 OMX_BUFFERHEADERTYPE* convert(OMX_BUFFERHEADERTYPE *header);
1318                 OMX_BUFFERHEADERTYPE* queue_buffer(OMX_BUFFERHEADERTYPE *header);
1319                 OMX_ERRORTYPE allocate_buffers_color_convert(OMX_HANDLETYPE hComp,
1320                         OMX_BUFFERHEADERTYPE **bufferHdr,OMX_U32 port,OMX_PTR appData,
1321                         OMX_U32 bytes);
1322                 OMX_ERRORTYPE free_output_buffer(OMX_BUFFERHEADERTYPE *bufferHdr);
is_color_conversion_enabled()1323                 bool is_color_conversion_enabled() {return enabled;}
1324                 void enable_color_conversion(bool enable);
is_client_buffers_disabled()1325                 bool is_client_buffers_disabled() {return client_buffers_disabled;}
set_client_buffers_disabled(bool val)1326                 void set_client_buffers_disabled(bool val) {client_buffers_disabled = val;}
1327                 // Returns a specific C2D state, if true current buffers should undergo C2D conversion
1328                 // otherwise C2D is in quasi enabled state where in C2D src and dst formats are set but
1329                 // color conversion doesnt take place. This state is needed to handle color conversion
1330                 // of interlaced clips during port reconfig.
client_buffers_invalid()1331                 bool client_buffers_invalid() {return (!enabled || client_buffers_disabled);}
1332             private:
1333 #define MAX_COUNT MAX_NUM_INPUT_OUTPUT_BUFFERS
1334                 omx_vdec *omx;
1335                 bool enabled;
1336                 bool client_buffers_disabled;
1337                 OMX_COLOR_FORMATTYPE ColorFormat;
1338                 void init_members();
1339                 bool color_convert_mode;
1340                 ColorConvertFormat dest_format;
1341                 ColorConvertFormat src_format;
1342                 C2DColorConverter c2dcc;
1343                 unsigned int allocated_count;
1344                 unsigned int buffer_size_req;
1345                 unsigned int buffer_alignment_req;
1346                 OMX_U32 m_c2d_width;
1347                 OMX_U32 m_c2d_height;
1348                 OMX_QCOM_PLATFORM_PRIVATE_LIST      m_platform_list_client[MAX_COUNT];
1349                 OMX_QCOM_PLATFORM_PRIVATE_ENTRY     m_platform_entry_client[MAX_COUNT];
1350                 OMX_QCOM_PLATFORM_PRIVATE_PMEM_INFO m_pmem_info_client[MAX_COUNT];
1351                 OMX_BUFFERHEADERTYPE  m_out_mem_ptr_client[MAX_COUNT];
1352                 DecColorMapping mMapOutput2DriverColorFormat;
1353                 ColorSubMapping mMapOutput2Convert;
1354 #ifdef USE_ION
1355                 struct vdec_ion op_buf_ion_info[MAX_COUNT];
1356 #endif
1357 #ifdef USE_GBM
1358                 struct vdec_gbm op_buf_gbm_info[MAX_COUNT];
1359 #endif
1360 
1361                 unsigned char *pmem_baseaddress[MAX_COUNT];
1362                 int pmem_fd[MAX_COUNT];
1363         };
1364         allocate_color_convert_buf client_buffers;
1365         struct video_decoder_capability m_decoder_capability;
1366         struct debug_cap m_debug;
1367         int log_input_buffers(const char *, int, uint64_t, int);
1368         int log_output_buffers(OMX_BUFFERHEADERTYPE *);
1369         int log_cc_output_buffers(OMX_BUFFERHEADERTYPE *);
1370         void send_codec_config();
1371         OMX_TICKS m_last_rendered_TS;
1372         int32_t m_dec_hfr_fps;
1373         int32_t m_dec_secure_prefetch_size_internal;
1374         int32_t m_dec_secure_prefetch_size_output;
1375         int32_t m_arb_mode_override;
1376         volatile int32_t m_queued_codec_config_count;
1377         OMX_U32 current_perf_level;
1378         bool secure_scaling_to_non_secure_opb;
1379         bool m_force_compressed_for_dpb;
1380         bool m_is_display_session;
1381 
1382         class perf_control {
1383             typedef int (*perf_lock_acquire_t)(int, int, int*, int);
1384             typedef int (*perf_lock_release_t)(int);
1385 
1386             private:
1387                 void *m_perf_lib;
1388                 int m_perf_handle;
1389                 perf_lock_acquire_t m_perf_lock_acquire;
1390                 perf_lock_release_t m_perf_lock_release;
1391 
1392             public:
1393                 perf_control();
1394                 ~perf_control();
1395                 bool load_perf_library();
1396                 int perf_lock_acquire();
1397                 void perf_lock_release();
1398                 int m_perf_control_enable;
1399         };
1400         perf_control m_perf_control;
1401 
getPreferredColorFormatNonSurfaceMode(OMX_U32 index)1402         static OMX_COLOR_FORMATTYPE getPreferredColorFormatNonSurfaceMode(OMX_U32 index) {
1403             //On Android, we default to standard YUV formats for non-surface use-cases
1404             //where apps prefer known color formats.
1405             OMX_COLOR_FORMATTYPE formatsNonSurfaceMode[] = {
1406                 [0] = OMX_COLOR_FormatYUV420SemiPlanar,
1407                 [1] = OMX_COLOR_FormatYUV420Planar,
1408                 [2] = (OMX_COLOR_FORMATTYPE)QOMX_COLOR_FORMATYUV420PackedSemiPlanar32m,
1409                 [3] = (OMX_COLOR_FORMATTYPE)QOMX_COLOR_FORMATYUV420PackedSemiPlanar32mMultiView,
1410                 [4] = (OMX_COLOR_FORMATTYPE)QOMX_COLOR_FORMATYUV420PackedSemiPlanar32mCompressed,
1411             };
1412             return (index < sizeof(formatsNonSurfaceMode) / sizeof(OMX_COLOR_FORMATTYPE)) ?
1413                 formatsNonSurfaceMode[index] : OMX_COLOR_FormatMax;
1414         }
1415 
getPreferredColorFormatDefaultMode(OMX_U32 index)1416         OMX_COLOR_FORMATTYPE getPreferredColorFormatDefaultMode(OMX_U32 index) {
1417             //for surface mode (normal playback), advertise native/accelerated formats first
1418             OMX_COLOR_FORMATTYPE format = (OMX_COLOR_FORMATTYPE)QOMX_COLOR_FORMATYUV420PackedSemiPlanar32m;
1419 
1420             if (!m_disable_ubwc_mode) {
1421                 OMX_COLOR_FORMATTYPE formatsDefault[] = {
1422                     [0] = (OMX_COLOR_FORMATTYPE)QOMX_COLOR_FORMATYUV420PackedSemiPlanar32mCompressed,
1423                     [1] = (OMX_COLOR_FORMATTYPE)QOMX_COLOR_FORMATYUV420PackedSemiPlanar32m,
1424                     [2] = OMX_COLOR_FormatYUV420SemiPlanar,
1425                     [3] = OMX_COLOR_FormatYUV420Planar,
1426                     [4] = (OMX_COLOR_FORMATTYPE)QOMX_COLOR_FORMATYUV420PackedSemiPlanar32mMultiView,
1427                 };
1428                 format = (index < sizeof(formatsDefault) / sizeof(OMX_COLOR_FORMATTYPE)) ?
1429                     formatsDefault[index] : OMX_COLOR_FormatMax;
1430             } else {
1431                 OMX_COLOR_FORMATTYPE formatsDefault[] = {
1432                     [0] = (OMX_COLOR_FORMATTYPE)QOMX_COLOR_FORMATYUV420PackedSemiPlanar32m,
1433                     [1] = OMX_COLOR_FormatYUV420SemiPlanar,
1434                     [2] = OMX_COLOR_FormatYUV420Planar,
1435                     [3] = (OMX_COLOR_FORMATTYPE)QOMX_COLOR_FORMATYUV420PackedSemiPlanar32mMultiView,
1436                 };
1437                 format = (index < sizeof(formatsDefault) / sizeof(OMX_COLOR_FORMATTYPE)) ?
1438                     formatsDefault[index] : OMX_COLOR_FormatMax;
1439             }
1440             return format;
1441         }
1442 
1443         static OMX_ERRORTYPE describeColorFormat(OMX_PTR params);
1444         bool prefetch_buffers(unsigned long prefetch_count,
1445                     unsigned long prefetch_size, unsigned ioctl_code,
1446                     unsigned ion_flag);
1447         unsigned char m_prefetch_done;
1448 
1449         client_extradata_info m_client_out_extradata_info;
1450 
1451         OMX_ERRORTYPE get_vendor_extension_config(
1452                 OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE *ext);
1453         OMX_ERRORTYPE set_vendor_extension_config(
1454                 OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE *ext);
1455 
1456         void init_vendor_extensions (VendorExtensionStore&);
1457 
1458         // list of extensions is not mutable after initialization
1459         const VendorExtensionStore mVendorExtensionStore;
1460 
1461         // Map of ColorAspects (VideoAPI.h) to ColorMetaData (color_metadata.h)
1462         PrimariesMap mPrimariesMap;
1463         TransferMap mTransferMap;
1464         MatrixCoeffMap mMatrixCoeffMap;
1465         RangeMap mColorRangeMap;
1466 
1467         void init_color_aspects_map();
1468         void convert_color_aspects_to_metadata(ColorAspects& aspects, ColorMetaData &color_mdata);
1469         void convert_hdr_info_to_metadata(HDRStaticInfo& hdr_info, ColorMetaData &color_mdata);
1470         void get_preferred_color_aspects(ColorAspects& preferredColorAspects);
1471         void get_preferred_hdr_info(HDRStaticInfo& preferredHDRInfo);
1472         bool vdec_query_cap(struct v4l2_queryctrl &cap);
1473         bool store_vp9_hdr10plusinfo(DescribeHDR10PlusInfoParams *hdr10plusinfo);
1474         bool store_hevc_hdr10plusinfo(uint32_t payload_size,
1475             msm_vidc_stream_userdata_payload *hdr10plusdata);
1476         void update_hdr10plusinfo_cookie_using_timestamp(OMX_PTR cookie, OMX_TICKS timestamp);
1477         void convert_hdr10plusinfo_to_metadata(OMX_PTR cookie, ColorMetaData &colorData);
1478         void remove_hdr10plusinfo_using_cookie(OMX_PTR cookie);
1479         void clear_hdr10plusinfo();
1480         void get_hdr10plusinfo(DescribeHDR10PlusInfoParams *hdr10plusdata);
1481         void print_hdr10plusinfo(DescribeHDR10PlusInfoParams *hdr10plusdata);
1482 public:
1483         bool is_down_scalar_enabled;
1484         bool m_is_split_mode;
1485         bool m_buffer_error;
1486 };
1487 
1488 enum instance_state {
1489     MSM_VIDC_CORE_UNINIT_DONE = 0x0001,
1490     MSM_VIDC_CORE_INIT,
1491     MSM_VIDC_CORE_INIT_DONE,
1492     MSM_VIDC_OPEN,
1493     MSM_VIDC_OPEN_DONE,
1494     MSM_VIDC_LOAD_RESOURCES,
1495     MSM_VIDC_LOAD_RESOURCES_DONE,
1496     MSM_VIDC_START,
1497     MSM_VIDC_START_DONE,
1498     MSM_VIDC_STOP,
1499     MSM_VIDC_STOP_DONE,
1500     MSM_VIDC_RELEASE_RESOURCES,
1501     MSM_VIDC_RELEASE_RESOURCES_DONE,
1502     MSM_VIDC_CLOSE,
1503     MSM_VIDC_CLOSE_DONE,
1504     MSM_VIDC_CORE_UNINIT,
1505 };
1506 
1507 enum vidc_resposes_id {
1508     MSM_VIDC_DECODER_FLUSH_DONE = 0x11,
1509     MSM_VIDC_DECODER_EVENT_CHANGE,
1510 };
1511 
1512 #endif // __OMX_VDEC_H__
1513