1 /*--------------------------------------------------------------------------
2 Copyright (c) 2017, The Linux Foundation. All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are
6 met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above
10 copyright notice, this list of conditions and the following
11 disclaimer in the documentation and/or other materials provided
12 with the distribution.
13 * Neither the name of The Linux Foundation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 --------------------------------------------------------------------------*/
29
30 #ifndef __VIDC_COMMON_H__
31 #define __VIDC_COMMON_H__
32
33 #include <unordered_map>
34
35 #include "OMX_QCOMExtns.h"
36 #include <linux/dma-buf.h>
37 #ifdef _ANDROID_
38 #include <gralloc_priv.h>
39 #endif
40 #ifdef _ANDROID_
41 #include <cutils/properties.h>
42 #else
43 #define PROPERTY_VALUE_MAX 92
44 #endif
45
46 // BitMask Management logic
47 #define BITS_PER_INDEX 64
48 #define BITMASK_FLAG(mIndex) ((uint64_t)1 \
49 << ((mIndex) % BITS_PER_INDEX))
50 #define BITMASK_CLEAR(pBufferMask_64,mIndex) ((*pBufferMask_64) \
51 &= ~(BITMASK_FLAG(mIndex)))
52 #define BITMASK_SET(pBufferMask_64,mIndex) ((*pBufferMask_64) \
53 |= BITMASK_FLAG(mIndex))
54 #define BITMASK_PRESENT(pBufferMask_64,mIndex) ((*pBufferMask_64) \
55 & BITMASK_FLAG(mIndex))
56 #define BITMASK_ABSENT(pBufferMask_64,mIndex) (((*pBufferMask_64) \
57 & BITMASK_FLAG(mIndex)) == 0x0)
58
59 using pl_map = std::unordered_map<int, int>;
60 using codec_map = std::unordered_map<int, pl_map *>;
61
62 class profile_level_converter {
63 static pl_map profile_avc_omx_to_v4l2;
64 static pl_map profile_hevc_omx_to_v4l2;
65 static pl_map profile_mpeg2_omx_to_v4l2;
66 static pl_map profile_vp8_omx_to_v4l2;
67 static pl_map profile_vp9_omx_to_v4l2;
68 static pl_map level_avc_omx_to_v4l2;
69 static pl_map level_hevc_omx_to_v4l2;
70 static pl_map level_vp8_omx_to_v4l2;
71 static pl_map level_mpeg2_omx_to_v4l2;
72 static pl_map level_vp9_omx_to_v4l2;
73 static pl_map profile_avc_v4l2_to_omx;
74 static pl_map profile_hevc_v4l2_to_omx;
75 static pl_map profile_mpeg2_v4l2_to_omx;
76 static pl_map profile_vp9_v4l2_to_omx;
77 static pl_map level_avc_v4l2_to_omx;
78 static pl_map level_hevc_v4l2_to_omx;
79 static pl_map level_vp8_v4l2_to_omx;
80 static pl_map level_mpeg2_v4l2_to_omx;
81 static pl_map profile_vp8_v4l2_to_omx;
82 static pl_map level_vp9_v4l2_to_omx;
83 static codec_map profile_omx_to_v4l2_map;
84 static codec_map profile_v4l2_to_omx_map;
85 static codec_map level_omx_to_v4l2_map;
86 static codec_map level_v4l2_to_omx_map;
87
88 //Constructor that initializes and performs the mapping
89 profile_level_converter() = delete;
90 static bool find_item(const pl_map &map, int key, int *value);
91 static bool find_map(const codec_map &map, int key, pl_map **value_map);
92
93 public:
94 static void init();
95 static bool convert_v4l2_profile_to_omx(int codec, int v4l2_profile, int *omx_profile);
96 static bool convert_omx_profile_to_v4l2(int codec, int omx_profile, int *v4l2_profile);
97 static bool convert_v4l2_level_to_omx(int codec, int v4l2_level, int *omx_level);
98 static bool find_tier(int codec, int omx_level, unsigned int *tire);
99 static bool convert_omx_level_to_v4l2(int codec, int omx_level, int *v4l2_level);
100 };
101
102 void get_gralloc_format_as_string(char * buf, int buf_len, int format);
103 void get_v4l2_color_format_as_string(char * buf, int buf_len, unsigned long v4l2Pixformat);
104
105 void do_sync_ioctl(int fd, struct dma_buf_sync* sync);
106
sync_start_write(int fd)107 static inline void sync_start_write(int fd) {
108 struct dma_buf_sync sync = {0};
109 sync.flags = DMA_BUF_SYNC_START | DMA_BUF_SYNC_WRITE;
110 do_sync_ioctl(fd, &sync);
111 }
112
sync_end_write(int fd)113 static inline void sync_end_write(int fd) {
114 struct dma_buf_sync sync = {0};
115 sync.flags = DMA_BUF_SYNC_END | DMA_BUF_SYNC_WRITE;
116 do_sync_ioctl(fd, &sync);
117 }
118
sync_start_read(int fd)119 static inline void sync_start_read(int fd) {
120 struct dma_buf_sync sync = {0};
121 sync.flags = DMA_BUF_SYNC_START | DMA_BUF_SYNC_READ;
122 do_sync_ioctl(fd, &sync);
123 }
124
sync_end_read(int fd)125 static inline void sync_end_read(int fd) {
126 struct dma_buf_sync sync = {0};
127 sync.flags = DMA_BUF_SYNC_END | DMA_BUF_SYNC_READ;
128 do_sync_ioctl(fd, &sync);
129 }
130
sync_start_rw(int fd)131 static inline void sync_start_rw(int fd) {
132 struct dma_buf_sync sync = {0};
133 sync.flags = DMA_BUF_SYNC_START | DMA_BUF_SYNC_RW;
134 do_sync_ioctl(fd, &sync);
135 }
136
sync_end_rw(int fd)137 static inline void sync_end_rw(int fd) {
138 struct dma_buf_sync sync = {0};
139 sync.flags = DMA_BUF_SYNC_END | DMA_BUF_SYNC_RW;
140 do_sync_ioctl(fd, &sync);
141 }
142
cache_clean(int fd)143 static inline void cache_clean(int fd) {
144 sync_start_write(fd);
145 sync_end_write(fd);
146 }
147
cache_invalidate(int fd)148 static inline void cache_invalidate(int fd) {
149 sync_start_write(fd);
150 sync_end_read(fd);
151 }
152
cache_clean_invalidate(int fd)153 static inline void cache_clean_invalidate(int fd) {
154 sync_start_rw(fd);
155 sync_end_rw(fd);
156 }
157 #endif // __VIDC_COMMON_H__
158