1 /*--------------------------------------------------------------------------
2 Copyright (c) 2017 - 2018 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 #define LOG_TAG "OMX_COMMON"
30
31 #include <log/log.h>
32 #include "vidc_debug.h"
33 #include "vidc_common.h"
34 #include "OMX_Core.h"
35 #include "OMX_QCOMExtns.h"
36 #include "OMX_VideoExt.h"
37 #include "OMX_IndexExt.h"
38 #include <linux/videodev2.h>
39
40 int debug_level = PRIO_ERROR;
41
42
43 pl_map profile_level_converter::profile_avc_omx_to_v4l2 ({
44 {QOMX_VIDEO_AVCProfileConstrainedBaseline,
45 V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE},
46 {QOMX_VIDEO_AVCProfileBaseline,
47 V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE},
48 {QOMX_VIDEO_AVCProfileMain,
49 V4L2_MPEG_VIDEO_H264_PROFILE_MAIN},
50 {QOMX_VIDEO_AVCProfileConstrainedHigh,
51 V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_HIGH},
52 {QOMX_VIDEO_AVCProfileHigh,
53 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH}
54 });
55
56 pl_map profile_level_converter::profile_avc_v4l2_to_omx ({});
57
58 pl_map profile_level_converter::profile_hevc_omx_to_v4l2 ({
59 {OMX_VIDEO_HEVCProfileMain,
60 V4L2_MPEG_VIDC_VIDEO_HEVC_PROFILE_MAIN},
61 {OMX_VIDEO_HEVCProfileMain10HDR10,
62 V4L2_MPEG_VIDC_VIDEO_HEVC_PROFILE_MAIN10},
63 {OMX_VIDEO_HEVCProfileMainStill,
64 V4L2_MPEG_VIDC_VIDEO_HEVC_PROFILE_MAIN_STILL_PIC},
65 });
66
67 pl_map profile_level_converter::profile_hevc_v4l2_to_omx ({});
68
69 pl_map profile_level_converter::profile_mpeg2_omx_to_v4l2 ({
70 {OMX_VIDEO_MPEG2ProfileSimple,
71 V4L2_MPEG_VIDC_VIDEO_MPEG2_PROFILE_SIMPLE},
72 {OMX_VIDEO_MPEG2ProfileMain,
73 V4L2_MPEG_VIDC_VIDEO_MPEG2_PROFILE_MAIN},
74 });
75
76 pl_map profile_level_converter::profile_mpeg2_v4l2_to_omx ({});
77
78 pl_map profile_level_converter::profile_vp9_omx_to_v4l2 ({
79 {OMX_VIDEO_VP9Profile0, V4L2_MPEG_VIDC_VIDEO_VP9_PROFILE_P0},
80 {OMX_VIDEO_VP9Profile2HDR, V4L2_MPEG_VIDC_VIDEO_VP9_PROFILE_P2_10},
81 });
82
83 pl_map profile_level_converter::profile_vp9_v4l2_to_omx ({});
84
85 pl_map profile_level_converter::profile_tme_omx_to_v4l2 ({
86 {QOMX_VIDEO_TMEProfile0,
87 V4L2_MPEG_VIDC_VIDEO_TME_PROFILE_0},
88 {QOMX_VIDEO_TMEProfile1,
89 V4L2_MPEG_VIDC_VIDEO_TME_PROFILE_1},
90 {QOMX_VIDEO_TMEProfile2,
91 V4L2_MPEG_VIDC_VIDEO_TME_PROFILE_2},
92 {QOMX_VIDEO_TMEProfile3,
93 V4L2_MPEG_VIDC_VIDEO_TME_PROFILE_3},
94 });
95
96 pl_map profile_level_converter::profile_tme_v4l2_to_omx ({});
97
98 pl_map profile_level_converter::level_avc_omx_to_v4l2 ({
99 {OMX_VIDEO_AVCLevel1, V4L2_MPEG_VIDEO_H264_LEVEL_1_0},
100 {OMX_VIDEO_AVCLevel11, V4L2_MPEG_VIDEO_H264_LEVEL_1_1},
101 {OMX_VIDEO_AVCLevel12, V4L2_MPEG_VIDEO_H264_LEVEL_1_2},
102 {OMX_VIDEO_AVCLevel13, V4L2_MPEG_VIDEO_H264_LEVEL_1_3},
103 {OMX_VIDEO_AVCLevel1b, V4L2_MPEG_VIDEO_H264_LEVEL_1B},
104 {OMX_VIDEO_AVCLevel2, V4L2_MPEG_VIDEO_H264_LEVEL_2_0},
105 {OMX_VIDEO_AVCLevel21, V4L2_MPEG_VIDEO_H264_LEVEL_2_1},
106 {OMX_VIDEO_AVCLevel22, V4L2_MPEG_VIDEO_H264_LEVEL_2_2},
107 {OMX_VIDEO_AVCLevel3, V4L2_MPEG_VIDEO_H264_LEVEL_3_0},
108 {OMX_VIDEO_AVCLevel31, V4L2_MPEG_VIDEO_H264_LEVEL_3_1},
109 {OMX_VIDEO_AVCLevel32, V4L2_MPEG_VIDEO_H264_LEVEL_3_2},
110 {OMX_VIDEO_AVCLevel4, V4L2_MPEG_VIDEO_H264_LEVEL_4_0},
111 {OMX_VIDEO_AVCLevel41, V4L2_MPEG_VIDEO_H264_LEVEL_4_1},
112 {OMX_VIDEO_AVCLevel42, V4L2_MPEG_VIDEO_H264_LEVEL_4_2},
113 {OMX_VIDEO_AVCLevel5, V4L2_MPEG_VIDEO_H264_LEVEL_5_0},
114 {OMX_VIDEO_AVCLevel51, V4L2_MPEG_VIDEO_H264_LEVEL_5_1},
115 {OMX_VIDEO_AVCLevel52, V4L2_MPEG_VIDEO_H264_LEVEL_5_2},
116 });
117
118 pl_map profile_level_converter::level_avc_v4l2_to_omx ({});
119
120 pl_map profile_level_converter::level_hevc_omx_to_v4l2 ({
121 {OMX_VIDEO_HEVCMainTierLevel1, V4L2_MPEG_VIDC_VIDEO_HEVC_LEVEL_MAIN_TIER_LEVEL_1},
122 {OMX_VIDEO_HEVCMainTierLevel2, V4L2_MPEG_VIDC_VIDEO_HEVC_LEVEL_MAIN_TIER_LEVEL_2},
123 {OMX_VIDEO_HEVCMainTierLevel21, V4L2_MPEG_VIDC_VIDEO_HEVC_LEVEL_MAIN_TIER_LEVEL_2_1},
124 {OMX_VIDEO_HEVCMainTierLevel3, V4L2_MPEG_VIDC_VIDEO_HEVC_LEVEL_MAIN_TIER_LEVEL_3},
125 {OMX_VIDEO_HEVCMainTierLevel31, V4L2_MPEG_VIDC_VIDEO_HEVC_LEVEL_MAIN_TIER_LEVEL_3_1},
126 {OMX_VIDEO_HEVCMainTierLevel4, V4L2_MPEG_VIDC_VIDEO_HEVC_LEVEL_MAIN_TIER_LEVEL_4},
127 {OMX_VIDEO_HEVCMainTierLevel41, V4L2_MPEG_VIDC_VIDEO_HEVC_LEVEL_MAIN_TIER_LEVEL_4_1},
128 {OMX_VIDEO_HEVCMainTierLevel5, V4L2_MPEG_VIDC_VIDEO_HEVC_LEVEL_MAIN_TIER_LEVEL_5},
129 {OMX_VIDEO_HEVCMainTierLevel51, V4L2_MPEG_VIDC_VIDEO_HEVC_LEVEL_MAIN_TIER_LEVEL_5_1},
130 {OMX_VIDEO_HEVCMainTierLevel52, V4L2_MPEG_VIDC_VIDEO_HEVC_LEVEL_MAIN_TIER_LEVEL_5_2},
131 {OMX_VIDEO_HEVCMainTierLevel6, V4L2_MPEG_VIDC_VIDEO_HEVC_LEVEL_MAIN_TIER_LEVEL_6},
132 {OMX_VIDEO_HEVCMainTierLevel61, V4L2_MPEG_VIDC_VIDEO_HEVC_LEVEL_MAIN_TIER_LEVEL_6_1},
133 {OMX_VIDEO_HEVCMainTierLevel62, V4L2_MPEG_VIDC_VIDEO_HEVC_LEVEL_MAIN_TIER_LEVEL_6_2},
134 {OMX_VIDEO_HEVCHighTierLevel1, V4L2_MPEG_VIDC_VIDEO_HEVC_LEVEL_HIGH_TIER_LEVEL_1},
135 {OMX_VIDEO_HEVCHighTierLevel2, V4L2_MPEG_VIDC_VIDEO_HEVC_LEVEL_HIGH_TIER_LEVEL_2},
136 {OMX_VIDEO_HEVCHighTierLevel21, V4L2_MPEG_VIDC_VIDEO_HEVC_LEVEL_HIGH_TIER_LEVEL_2_1},
137 {OMX_VIDEO_HEVCHighTierLevel3, V4L2_MPEG_VIDC_VIDEO_HEVC_LEVEL_HIGH_TIER_LEVEL_3},
138 {OMX_VIDEO_HEVCHighTierLevel31, V4L2_MPEG_VIDC_VIDEO_HEVC_LEVEL_HIGH_TIER_LEVEL_3_1},
139 {OMX_VIDEO_HEVCHighTierLevel4, V4L2_MPEG_VIDC_VIDEO_HEVC_LEVEL_HIGH_TIER_LEVEL_4},
140 {OMX_VIDEO_HEVCHighTierLevel41, V4L2_MPEG_VIDC_VIDEO_HEVC_LEVEL_HIGH_TIER_LEVEL_4_1},
141 {OMX_VIDEO_HEVCHighTierLevel5, V4L2_MPEG_VIDC_VIDEO_HEVC_LEVEL_HIGH_TIER_LEVEL_5},
142 {OMX_VIDEO_HEVCHighTierLevel51, V4L2_MPEG_VIDC_VIDEO_HEVC_LEVEL_HIGH_TIER_LEVEL_5_1},
143 {OMX_VIDEO_HEVCHighTierLevel52, V4L2_MPEG_VIDC_VIDEO_HEVC_LEVEL_HIGH_TIER_LEVEL_5_2},
144 {OMX_VIDEO_HEVCHighTierLevel6, V4L2_MPEG_VIDC_VIDEO_HEVC_LEVEL_HIGH_TIER_LEVEL_6},
145 {OMX_VIDEO_HEVCHighTierLevel61, V4L2_MPEG_VIDC_VIDEO_HEVC_LEVEL_HIGH_TIER_LEVEL_6_1},
146 {OMX_VIDEO_HEVCHighTierLevel62, V4L2_MPEG_VIDC_VIDEO_HEVC_LEVEL_HIGH_TIER_LEVEL_6_2},
147 });
148
149 pl_map profile_level_converter::level_hevc_v4l2_to_omx ({});
150
151 pl_map profile_level_converter::level_vp8_omx_to_v4l2 ({
152 {OMX_VIDEO_VP8Level_Version0, V4L2_MPEG_VIDC_VIDEO_VP8_VERSION_0},
153 {OMX_VIDEO_VP8Level_Version1, V4L2_MPEG_VIDC_VIDEO_VP8_VERSION_1},
154 {OMX_VIDEO_VP8Level_Version2, V4L2_MPEG_VIDC_VIDEO_VP8_VERSION_2},
155 {OMX_VIDEO_VP8Level_Version3, V4L2_MPEG_VIDC_VIDEO_VP8_VERSION_3},
156 });
157
158 pl_map profile_level_converter::level_vp8_v4l2_to_omx ({});
159
160 pl_map profile_level_converter::level_mpeg2_omx_to_v4l2 ({
161 {OMX_VIDEO_MPEG2LevelLL, V4L2_MPEG_VIDC_VIDEO_MPEG2_LEVEL_0},
162 {OMX_VIDEO_MPEG2LevelML, V4L2_MPEG_VIDC_VIDEO_MPEG2_LEVEL_1},
163 {OMX_VIDEO_MPEG2LevelHL, V4L2_MPEG_VIDC_VIDEO_MPEG2_LEVEL_2},
164 });
165
166 pl_map profile_level_converter::level_mpeg2_v4l2_to_omx ({});
167
168 pl_map profile_level_converter::level_vp9_omx_to_v4l2 ({
169 {OMX_VIDEO_VP9Level1, V4L2_MPEG_VIDC_VIDEO_VP9_LEVEL_1},
170 {OMX_VIDEO_VP9Level11, V4L2_MPEG_VIDC_VIDEO_VP9_LEVEL_11},
171 {OMX_VIDEO_VP9Level2, V4L2_MPEG_VIDC_VIDEO_VP9_LEVEL_2},
172 {OMX_VIDEO_VP9Level21, V4L2_MPEG_VIDC_VIDEO_VP9_LEVEL_21},
173 {OMX_VIDEO_VP9Level3, V4L2_MPEG_VIDC_VIDEO_VP9_LEVEL_3},
174 {OMX_VIDEO_VP9Level31, V4L2_MPEG_VIDC_VIDEO_VP9_LEVEL_31},
175 {OMX_VIDEO_VP9Level4, V4L2_MPEG_VIDC_VIDEO_VP9_LEVEL_4},
176 {OMX_VIDEO_VP9Level41, V4L2_MPEG_VIDC_VIDEO_VP9_LEVEL_41},
177 {OMX_VIDEO_VP9Level5, V4L2_MPEG_VIDC_VIDEO_VP9_LEVEL_5},
178 {OMX_VIDEO_VP9Level51, V4L2_MPEG_VIDC_VIDEO_VP9_LEVEL_51},
179 });
180
181 pl_map profile_level_converter::level_vp9_v4l2_to_omx ({});
182
183 pl_map profile_level_converter::level_tme_omx_to_v4l2 ({
184 {QOMX_VIDEO_TMELevelInteger, V4L2_MPEG_VIDC_VIDEO_TME_LEVEL_INTEGER},
185 });
186
187 pl_map profile_level_converter::level_tme_v4l2_to_omx ({});
188
189 codec_map profile_level_converter::profile_omx_to_v4l2_map ({
190 {V4L2_PIX_FMT_H264, &profile_avc_omx_to_v4l2},
191 {V4L2_PIX_FMT_HEVC, &profile_hevc_omx_to_v4l2},
192 {V4L2_PIX_FMT_MPEG2, &profile_mpeg2_omx_to_v4l2},
193 {V4L2_PIX_FMT_VP9, &profile_vp9_omx_to_v4l2},
194 {V4L2_PIX_FMT_TME, &profile_tme_omx_to_v4l2},
195 });
196
197 codec_map profile_level_converter::profile_v4l2_to_omx_map ({
198 {V4L2_PIX_FMT_H264, &profile_avc_v4l2_to_omx},
199 {V4L2_PIX_FMT_HEVC, &profile_hevc_v4l2_to_omx},
200 {V4L2_PIX_FMT_MPEG2, &profile_mpeg2_v4l2_to_omx},
201 {V4L2_PIX_FMT_VP9, &profile_vp9_v4l2_to_omx},
202 {V4L2_PIX_FMT_TME, &profile_tme_v4l2_to_omx},
203 });
204
205 codec_map profile_level_converter::level_omx_to_v4l2_map ({
206 {V4L2_PIX_FMT_H264, &level_avc_omx_to_v4l2},
207 {V4L2_PIX_FMT_HEVC, &level_hevc_omx_to_v4l2},
208 {V4L2_PIX_FMT_MPEG2, &level_mpeg2_omx_to_v4l2},
209 {V4L2_PIX_FMT_VP8, &level_vp8_omx_to_v4l2},
210 {V4L2_PIX_FMT_VP9, &level_vp9_omx_to_v4l2},
211 {V4L2_PIX_FMT_TME, &level_tme_omx_to_v4l2},
212 });
213
214 codec_map profile_level_converter::level_v4l2_to_omx_map ({
215 {V4L2_PIX_FMT_H264, &level_avc_v4l2_to_omx},
216 {V4L2_PIX_FMT_HEVC, &level_hevc_v4l2_to_omx},
217 {V4L2_PIX_FMT_MPEG2, &level_mpeg2_v4l2_to_omx},
218 {V4L2_PIX_FMT_VP8, &level_vp8_v4l2_to_omx},
219 {V4L2_PIX_FMT_VP9, &level_vp9_v4l2_to_omx},
220 {V4L2_PIX_FMT_TME, &level_tme_v4l2_to_omx},
221 });
222
reverse_map(pl_map source_map,pl_map & dest_map)223 void reverse_map(pl_map source_map, pl_map &dest_map)
224 {
225 pl_map::iterator it;
226
227 for(it = source_map.begin(); it != source_map.end(); it++) {
228 dest_map[it->second] = it->first;
229 }
230 return;
231 }
232
init()233 void profile_level_converter::init()
234 {
235 reverse_map(profile_avc_omx_to_v4l2, profile_avc_v4l2_to_omx);
236 reverse_map(profile_hevc_omx_to_v4l2, profile_hevc_v4l2_to_omx);
237 reverse_map(profile_mpeg2_omx_to_v4l2, profile_mpeg2_v4l2_to_omx);
238 reverse_map(profile_vp9_omx_to_v4l2, profile_vp9_v4l2_to_omx);
239 reverse_map(profile_tme_omx_to_v4l2, profile_tme_v4l2_to_omx);
240 reverse_map(level_avc_omx_to_v4l2, level_avc_v4l2_to_omx);
241 reverse_map(level_hevc_omx_to_v4l2, level_hevc_v4l2_to_omx);
242 reverse_map(level_vp8_omx_to_v4l2, level_vp8_v4l2_to_omx);
243 reverse_map(level_mpeg2_omx_to_v4l2, level_mpeg2_v4l2_to_omx);
244 reverse_map(level_vp9_omx_to_v4l2, level_vp9_v4l2_to_omx);
245 reverse_map(level_tme_omx_to_v4l2, level_tme_v4l2_to_omx);
246 }
247
find_map(const codec_map & map,int key,pl_map ** value_map)248 bool profile_level_converter::find_map(const codec_map &map, int key, pl_map **value_map)
249 {
250 auto map_it = map.find (key);
251 if (map_it == map.end()) {
252 DEBUG_PRINT_ERROR(" Invalid codec : %d Cannot find map for this codec", key);
253 return false;
254 }
255 *value_map = map_it->second;
256 return true;
257 }
258
find_item(const pl_map & map,int key,int * value)259 bool profile_level_converter::find_item(const pl_map &map, int key, int *value)
260 {
261 auto it = map.find (key);
262 if (it == map.end()) {
263 DEBUG_PRINT_ERROR(" Invalid key : %d Cannot find key in map ", key);
264 return false;
265 }
266 *value = it->second;
267 return true;
268 }
269
convert_v4l2_profile_to_omx(int codec,int v4l2_profile,int * omx_profile)270 bool profile_level_converter::convert_v4l2_profile_to_omx(int codec, int v4l2_profile, int *omx_profile)
271 {
272 pl_map *profile_map;
273
274 if (!find_map(profile_v4l2_to_omx_map, codec, &profile_map))
275 return false;
276
277 return find_item(*profile_map, v4l2_profile, omx_profile);
278 }
279
convert_omx_profile_to_v4l2(int codec,int omx_profile,int * v4l2_profile)280 bool profile_level_converter::convert_omx_profile_to_v4l2(int codec, int omx_profile, int *v4l2_profile)
281 {
282 pl_map *profile_map;
283
284 if (!find_map(profile_omx_to_v4l2_map, codec, &profile_map))
285 return false;
286
287 return find_item(*profile_map, omx_profile, v4l2_profile);
288 }
289
convert_v4l2_level_to_omx(int codec,int v4l2_level,int * omx_level)290 bool profile_level_converter::convert_v4l2_level_to_omx(int codec, int v4l2_level, int *omx_level)
291 {
292 pl_map *level_map;
293
294 if (!find_map(level_v4l2_to_omx_map, codec, &level_map))
295 return false;
296
297 return find_item(*level_map, v4l2_level, omx_level);
298 }
299
convert_omx_level_to_v4l2(int codec,int omx_level,int * v4l2_level)300 bool profile_level_converter::convert_omx_level_to_v4l2(int codec, int omx_level, int *v4l2_level)
301 {
302 pl_map *level_map;
303
304 if (!find_map(level_omx_to_v4l2_map, codec, &level_map))
305 return false;
306
307 return find_item(*level_map, omx_level, v4l2_level);
308 }
309
IvfFileHeader()310 IvfFileHeader:: IvfFileHeader() :
311 signature{'D','K','I','F'},
312 version(),
313 size(32),
314 fourCC{'V','P','8','0'},
315 frameCount(0),
316 unused()
317 {
318 }
319
IvfFileHeader(bool isVp9,int width,int height,int rate,int scale,int frameCount)320 IvfFileHeader:: IvfFileHeader(bool isVp9, int width, int height,
321 int rate, int scale, int frameCount) :
322 IvfFileHeader() {
323 this->width = width;
324 this->height = height;
325 this->rate = rate;
326 this->scale = scale;
327 this->frameCount = frameCount;
328 fourCC[2] = isVp9 ? '9' : '8';
329 }
330
IvfFrameHeader()331 IvfFrameHeader:: IvfFrameHeader(): filledLen(), timeStamp() {}
332
IvfFrameHeader(uint32_t filledLen,uint64_t timeStamp)333 IvfFrameHeader:: IvfFrameHeader(uint32_t filledLen, uint64_t timeStamp) :
334 filledLen(filledLen),
335 timeStamp(timeStamp) {
336 }
337