1 /**************************************************************************
2 *
3 * Copyright 2009 Younes Manton.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #ifndef U_VIDEO_H
29 #define U_VIDEO_H
30
31 #include "pipe/p_defines.h"
32 #include "pipe/p_video_enums.h"
33
34 /* u_reduce_video_profile() needs these */
35 #include "util/compiler.h"
36 #include "util/u_debug.h"
37 #include "util/u_math.h"
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43 static inline enum pipe_video_format
u_reduce_video_profile(enum pipe_video_profile profile)44 u_reduce_video_profile(enum pipe_video_profile profile)
45 {
46 switch (profile)
47 {
48 case PIPE_VIDEO_PROFILE_MPEG1:
49 case PIPE_VIDEO_PROFILE_MPEG2_SIMPLE:
50 case PIPE_VIDEO_PROFILE_MPEG2_MAIN:
51 return PIPE_VIDEO_FORMAT_MPEG12;
52
53 case PIPE_VIDEO_PROFILE_MPEG4_SIMPLE:
54 case PIPE_VIDEO_PROFILE_MPEG4_ADVANCED_SIMPLE:
55 return PIPE_VIDEO_FORMAT_MPEG4;
56
57 case PIPE_VIDEO_PROFILE_VC1_SIMPLE:
58 case PIPE_VIDEO_PROFILE_VC1_MAIN:
59 case PIPE_VIDEO_PROFILE_VC1_ADVANCED:
60 return PIPE_VIDEO_FORMAT_VC1;
61
62 case PIPE_VIDEO_PROFILE_MPEG4_AVC_BASELINE:
63 case PIPE_VIDEO_PROFILE_MPEG4_AVC_CONSTRAINED_BASELINE:
64 case PIPE_VIDEO_PROFILE_MPEG4_AVC_MAIN:
65 case PIPE_VIDEO_PROFILE_MPEG4_AVC_EXTENDED:
66 case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH:
67 case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH10:
68 case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH422:
69 case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH444:
70 return PIPE_VIDEO_FORMAT_MPEG4_AVC;
71
72 case PIPE_VIDEO_PROFILE_HEVC_MAIN:
73 case PIPE_VIDEO_PROFILE_HEVC_MAIN_10:
74 case PIPE_VIDEO_PROFILE_HEVC_MAIN_STILL:
75 case PIPE_VIDEO_PROFILE_HEVC_MAIN_12:
76 case PIPE_VIDEO_PROFILE_HEVC_MAIN_444:
77 return PIPE_VIDEO_FORMAT_HEVC;
78
79 case PIPE_VIDEO_PROFILE_JPEG_BASELINE:
80 return PIPE_VIDEO_FORMAT_JPEG;
81
82 case PIPE_VIDEO_PROFILE_VP9_PROFILE0:
83 case PIPE_VIDEO_PROFILE_VP9_PROFILE2:
84 return PIPE_VIDEO_FORMAT_VP9;
85
86 case PIPE_VIDEO_PROFILE_AV1_MAIN:
87 case PIPE_VIDEO_PROFILE_AV1_PROFILE2:
88 return PIPE_VIDEO_FORMAT_AV1;
89
90 default:
91 return PIPE_VIDEO_FORMAT_UNKNOWN;
92 }
93 }
94
95 static inline void
u_copy_nv12_to_yv12(void * const * destination_data,uint32_t const * destination_pitches,int src_plane,int src_field,int src_stride,int num_fields,uint8_t const * src,int width,int height)96 u_copy_nv12_to_yv12(void *const *destination_data,
97 uint32_t const *destination_pitches,
98 int src_plane, int src_field,
99 int src_stride, int num_fields,
100 uint8_t const *src,
101 int width, int height)
102 {
103 int x, y;
104 unsigned u_stride = destination_pitches[2] * num_fields;
105 unsigned v_stride = destination_pitches[1] * num_fields;
106 uint8_t *u_dst = (uint8_t *)destination_data[2] + destination_pitches[2] * src_field;
107 uint8_t *v_dst = (uint8_t *)destination_data[1] + destination_pitches[1] * src_field;
108
109 /* TODO: SIMD */
110 for (y = 0; y < height; y++) {
111 for (x = 0; x < width; x++) {
112 u_dst[x] = src[2*x];
113 v_dst[x] = src[2*x+1];
114 }
115 u_dst += u_stride;
116 v_dst += v_stride;
117 src += src_stride;
118 }
119 }
120
121 /**
122 * \brief Copy YV12 chroma data while converting it NV12
123 *
124 * Given a set of YV12 source pointers and -pitches, copy the data to a
125 * layout typical for NV12 video buffers.
126 *
127 * \param source data[in] The plane data pointers. Array of 3.
128 * \param source_pitches[in] The plane pitches. Array of 3.
129 * \param dst_plane[in] The destination plane to copy to. For NV12 always 1.
130 * \param dst_field[in] The destination field if interlaced.
131 * \param dst_stride[in] The destination stride for this plane.
132 * \param num_fields[in] The number of fields in the video buffer.
133 * \param dst[in] The destination plane pointer.
134 * \param width[in] The source plane width.
135 * \param height[in] The source plane height.
136 */
137 static inline void
u_copy_nv12_from_yv12(const void * const * source_data,uint32_t const * source_pitches,int dst_plane,int dst_field,int dst_stride,int num_fields,uint8_t * dst,int width,int height)138 u_copy_nv12_from_yv12(const void *const *source_data,
139 uint32_t const *source_pitches,
140 int dst_plane, int dst_field,
141 int dst_stride, int num_fields,
142 uint8_t *dst,
143 int width, int height)
144 {
145 int x, y;
146 unsigned u_stride = source_pitches[2] * num_fields;
147 unsigned v_stride = source_pitches[1] * num_fields;
148 uint8_t *u_src = (uint8_t *)source_data[2] + source_pitches[2] * dst_field;
149 uint8_t *v_src = (uint8_t *)source_data[1] + source_pitches[1] * dst_field;
150
151 /* TODO: SIMD */
152 for (y = 0; y < height; y++) {
153 for (x = 0; x < width; x++) {
154 dst[2*x] = u_src[x];
155 dst[2*x+1] = v_src[x];
156 }
157 u_src += u_stride;
158 v_src += v_stride;
159 dst += dst_stride;
160 }
161 }
162
163 static inline void
u_copy_yv12_to_nv12(void * const * destination_data,uint32_t const * destination_pitches,int src_plane,int src_field,int src_stride,int num_fields,uint8_t const * src,int width,int height)164 u_copy_yv12_to_nv12(void *const *destination_data,
165 uint32_t const *destination_pitches,
166 int src_plane, int src_field,
167 int src_stride, int num_fields,
168 uint8_t const *src,
169 int width, int height)
170 {
171 int x, y;
172 unsigned offset = 2 - src_plane;
173 unsigned stride = destination_pitches[1] * num_fields;
174 uint8_t *dst = (uint8_t *)destination_data[1] + destination_pitches[1] * src_field;
175
176 /* TODO: SIMD */
177 for (y = 0; y < height; y++) {
178 for (x = 0; x < 2 * width; x += 2) {
179 dst[x+offset] = src[x>>1];
180 }
181 dst += stride;
182 src += src_stride;
183 }
184 }
185
186 static inline void
u_copy_swap422_packed(void * const * destination_data,uint32_t const * destination_pitches,int src_plane,int src_field,int src_stride,int num_fields,uint8_t const * src,int width,int height)187 u_copy_swap422_packed(void *const *destination_data,
188 uint32_t const *destination_pitches,
189 int src_plane, int src_field,
190 int src_stride, int num_fields,
191 uint8_t const *src,
192 int width, int height)
193 {
194 int x, y;
195 unsigned stride = destination_pitches[0] * num_fields;
196 uint8_t *dst = (uint8_t *)destination_data[0] + destination_pitches[0] * src_field;
197
198 /* TODO: SIMD */
199 for (y = 0; y < height; y++) {
200 for (x = 0; x < 4 * width; x += 4) {
201 dst[x+0] = src[x+1];
202 dst[x+1] = src[x+0];
203 dst[x+2] = src[x+3];
204 dst[x+3] = src[x+2];
205 }
206 dst += stride;
207 src += src_stride;
208 }
209 }
210
211 static inline uint32_t
u_get_h264_level(uint32_t width,uint32_t height,uint32_t * max_reference)212 u_get_h264_level(uint32_t width, uint32_t height, uint32_t *max_reference)
213 {
214 uint32_t max_dpb_mbs;
215
216 width = align(width, 16);
217 height = align(height, 16);
218
219 /* Max references will be used for caculation of number of DPB buffers
220 in the UVD driver, limitation of max references is 16. Some client
221 like mpv application for VA-API, it requires references more than that,
222 so we have to set max of references to 16 here. */
223 *max_reference = MIN2(*max_reference, 16);
224 max_dpb_mbs = (width / 16) * (height / 16) * *max_reference;
225
226 /* The calculation is based on "Decoded picture buffering" section
227 from http://en.wikipedia.org/wiki/H.264/MPEG-4_AVC */
228 if (max_dpb_mbs <= 8100)
229 return 30;
230 else if (max_dpb_mbs <= 18000)
231 return 31;
232 else if (max_dpb_mbs <= 20480)
233 return 32;
234 else if (max_dpb_mbs <= 32768)
235 return 41;
236 else if (max_dpb_mbs <= 34816)
237 return 42;
238 else if (max_dpb_mbs <= 110400)
239 return 50;
240 else if (max_dpb_mbs <= 184320)
241 return 51;
242 else
243 return 52;
244 }
245
246 static inline uint32_t
u_get_h264_profile_idc(enum pipe_video_profile profile)247 u_get_h264_profile_idc(enum pipe_video_profile profile)
248 {
249 switch (profile) {
250 case PIPE_VIDEO_PROFILE_MPEG4_AVC_CONSTRAINED_BASELINE:
251 case PIPE_VIDEO_PROFILE_MPEG4_AVC_BASELINE:
252 return 66;
253 case PIPE_VIDEO_PROFILE_MPEG4_AVC_MAIN:
254 return 77;
255 case PIPE_VIDEO_PROFILE_MPEG4_AVC_EXTENDED:
256 return 88;
257 case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH:
258 return 100;
259 case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH10:
260 return 110;
261 case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH422:
262 return 122;
263 case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH444:
264 return 244;
265 default:
266 return 66; //use baseline profile instead
267 }
268 }
269
270 #ifdef __cplusplus
271 }
272 #endif
273
274 #endif /* U_VIDEO_H */
275