gallium/util: Add align_calloc
[mesa.git] / src / gallium / auxiliary / util / u_video.h
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 "pipe/p_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
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_MAIN:
64 case PIPE_VIDEO_PROFILE_MPEG4_AVC_EXTENDED:
65 case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH:
66 case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH10:
67 case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH422:
68 case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH444:
69 return PIPE_VIDEO_FORMAT_MPEG4_AVC;
70
71 case PIPE_VIDEO_PROFILE_HEVC_MAIN:
72 case PIPE_VIDEO_PROFILE_HEVC_MAIN_10:
73 case PIPE_VIDEO_PROFILE_HEVC_MAIN_STILL:
74 case PIPE_VIDEO_PROFILE_HEVC_MAIN_12:
75 case PIPE_VIDEO_PROFILE_HEVC_MAIN_444:
76 return PIPE_VIDEO_FORMAT_HEVC;
77
78 default:
79 return PIPE_VIDEO_FORMAT_UNKNOWN;
80 }
81 }
82
83 static inline void
84 u_copy_nv12_to_yv12(void *const *destination_data,
85 uint32_t const *destination_pitches,
86 int src_plane, int src_field,
87 int src_stride, int num_fields,
88 uint8_t const *src,
89 int width, int height)
90 {
91 int x, y;
92 unsigned u_stride = destination_pitches[2] * num_fields;
93 unsigned v_stride = destination_pitches[1] * num_fields;
94 uint8_t *u_dst = (uint8_t *)destination_data[2] + destination_pitches[2] * src_field;
95 uint8_t *v_dst = (uint8_t *)destination_data[1] + destination_pitches[1] * src_field;
96
97 /* TODO: SIMD */
98 for (y = 0; y < height; y++) {
99 for (x = 0; x < width; x++) {
100 u_dst[x] = src[2*x];
101 v_dst[x] = src[2*x+1];
102 }
103 u_dst += u_stride;
104 v_dst += v_stride;
105 src += src_stride;
106 }
107 }
108
109 static inline void
110 u_copy_yv12_to_nv12(void *const *destination_data,
111 uint32_t const *destination_pitches,
112 int src_plane, int src_field,
113 int src_stride, int num_fields,
114 uint8_t const *src,
115 int width, int height)
116 {
117 int x, y;
118 unsigned offset = 2 - src_plane;
119 unsigned stride = destination_pitches[1] * num_fields;
120 uint8_t *dst = (uint8_t *)destination_data[1] + destination_pitches[1] * src_field;
121
122 /* TODO: SIMD */
123 for (y = 0; y < height; y++) {
124 for (x = 0; x < 2 * width; x += 2) {
125 dst[x+offset] = src[x>>1];
126 }
127 dst += stride;
128 src += src_stride;
129 }
130 }
131
132 static inline void
133 u_copy_yv12_img_to_nv12_surf(ubyte *const *src,
134 ubyte *dst,
135 unsigned width,
136 unsigned height,
137 unsigned src_stride,
138 unsigned dst_stride,
139 int field)
140 {
141 if (field == 0) {
142 ubyte *src_0 = src[field];
143 for (int i = 0; i < height ; i++) {
144 memcpy(dst, src_0, width);
145 dst += dst_stride;
146 src_0 += src_stride;
147 }
148 } else if (field == 1) {
149 const ubyte *src_1 = src[field];
150 const ubyte *src_2 = src[field+1];
151 bool odd = true;
152 for (unsigned i = 0; i < height ; i++) {
153 for (unsigned j = 0; j < width*2 ; j++) {
154 if (odd == false) {
155 dst[j] = src_1[j/2];
156 odd = true;
157 } else {
158 dst[j] = src_2[j/2];
159 odd = false;
160 }
161 }
162 dst += dst_stride;
163 src_1 += src_stride;
164 src_2 += src_stride;
165 }
166 }
167 }
168
169 static inline void
170 u_copy_swap422_packed(void *const *destination_data,
171 uint32_t const *destination_pitches,
172 int src_plane, int src_field,
173 int src_stride, int num_fields,
174 uint8_t const *src,
175 int width, int height)
176 {
177 int x, y;
178 unsigned stride = destination_pitches[0] * num_fields;
179 uint8_t *dst = (uint8_t *)destination_data[0] + destination_pitches[0] * src_field;
180
181 /* TODO: SIMD */
182 for (y = 0; y < height; y++) {
183 for (x = 0; x < 4 * width; x += 4) {
184 dst[x+0] = src[x+1];
185 dst[x+1] = src[x+0];
186 dst[x+2] = src[x+3];
187 dst[x+3] = src[x+2];
188 }
189 dst += stride;
190 src += src_stride;
191 }
192 }
193
194 static inline uint32_t
195 u_get_h264_level(uint32_t width, uint32_t height, uint32_t *max_reference)
196 {
197 uint32_t max_dpb_mbs;
198
199 width = align(width, 16);
200 height = align(height, 16);
201
202 /* Max references will be used for caculation of number of DPB buffers
203 in the UVD driver, limitation of max references is 16. Some client
204 like mpv application for VA-API, it requires references more than that,
205 so we have to set max of references to 16 here. */
206 *max_reference = MIN2(*max_reference, 16);
207 max_dpb_mbs = (width / 16) * (height / 16) * *max_reference;
208
209 /* The calculation is based on "Decoded picture buffering" section
210 from http://en.wikipedia.org/wiki/H.264/MPEG-4_AVC */
211 if (max_dpb_mbs <= 8100)
212 return 30;
213 else if (max_dpb_mbs <= 18000)
214 return 31;
215 else if (max_dpb_mbs <= 20480)
216 return 32;
217 else if (max_dpb_mbs <= 32768)
218 return 41;
219 else if (max_dpb_mbs <= 34816)
220 return 42;
221 else if (max_dpb_mbs <= 110400)
222 return 50;
223 else if (max_dpb_mbs <= 184320)
224 return 51;
225 else
226 return 52;
227 }
228
229 #ifdef __cplusplus
230 }
231 #endif
232
233 #endif /* U_VIDEO_H */