forked from mosterta/libvdpau-sunxi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
video_mixer.c
377 lines (306 loc) · 11.1 KB
/
video_mixer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
* Copyright (c) 2013 Jens Kuske <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <math.h>
#include "vdpau_private.h"
VdpStatus vdp_video_mixer_create(VdpDevice device, uint32_t feature_count, VdpVideoMixerFeature const *features, uint32_t parameter_count, VdpVideoMixerParameter const *parameters, void const *const *parameter_values, VdpVideoMixer *mixer)
{
device_ctx_t *dev = handle_get(device);
if (!dev)
return VDP_STATUS_INVALID_HANDLE;
mixer_ctx_t *mix = handle_create(sizeof(*mix), mixer, htype_mixer);
if (!mix)
return VDP_STATUS_RESOURCES;
mix->device = dev;
mix->contrast = 1.0;
mix->saturation = 1.0;
handle_release(device);
return VDP_STATUS_OK;
}
VdpStatus vdp_video_mixer_destroy(VdpVideoMixer mixer)
{
mixer_ctx_t *mix = handle_get(mixer);
if (!mix)
return VDP_STATUS_INVALID_HANDLE;
handle_release(mixer);
handle_destroy(mixer);
return VDP_STATUS_OK;
}
VdpStatus vdp_video_mixer_render(VdpVideoMixer mixer, VdpOutputSurface background_surface, VdpRect const *background_source_rect, VdpVideoMixerPictureStructure current_picture_structure, uint32_t video_surface_past_count, VdpVideoSurface const *video_surface_past, VdpVideoSurface video_surface_current, uint32_t video_surface_future_count, VdpVideoSurface const *video_surface_future, VdpRect const *video_source_rect, VdpOutputSurface destination_surface, VdpRect const *destination_rect, VdpRect const *destination_video_rect, uint32_t layer_count, VdpLayer const *layers)
{
mixer_ctx_t *mix = handle_get(mixer);
if (!mix)
return VDP_STATUS_INVALID_HANDLE;
if (background_surface != VDP_INVALID_HANDLE)
VDPAU_DBG_ONCE("Requested unimplemented background_surface");
if (current_picture_structure != VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME)
VDPAU_DBG_ONCE("Requested unimplemented picture_structure");
output_surface_ctx_t *os = handle_get(destination_surface);
if (!os)
return VDP_STATUS_INVALID_HANDLE;
os->vs = handle_get(video_surface_current);
if (!(os->vs))
return VDP_STATUS_INVALID_HANDLE;
if (destination_video_rect)
{
os->video_dst_rect = *destination_video_rect;
if (video_source_rect)
os->video_src_rect = *video_source_rect;
else
{
os->video_src_rect.x0 = os->video_src_rect.y0 = 0;
os->video_src_rect.x1 = os->vs->width;
os->video_src_rect.y1 = os->vs->height;
}
}
os->csc_change = mix->csc_change;
os->brightness = mix->brightness;
os->contrast = mix->contrast;
os->saturation = mix->saturation;
os->hue = mix->hue;
mix->csc_change = 0;
if (layer_count != 0)
VDPAU_DBG_ONCE("Requested unimplemented additional layers");
handle_release(mixer);
handle_release(destination_surface);
handle_release(video_surface_current);
return VDP_STATUS_OK;
}
VdpStatus vdp_video_mixer_get_feature_support(VdpVideoMixer mixer, uint32_t feature_count, VdpVideoMixerFeature const *features, VdpBool *feature_supports)
{
if (feature_count == 0)
return VDP_STATUS_OK;
if (!features || !feature_supports)
return VDP_STATUS_INVALID_POINTER;
mixer_ctx_t *mix = handle_get(mixer);
if (!mix)
return VDP_STATUS_INVALID_HANDLE;
handle_release(mixer);
return VDP_STATUS_ERROR;
}
VdpStatus vdp_video_mixer_set_feature_enables(VdpVideoMixer mixer, uint32_t feature_count, VdpVideoMixerFeature const *features, VdpBool const *feature_enables)
{
if (feature_count == 0)
return VDP_STATUS_OK;
if (!features || !feature_enables)
return VDP_STATUS_INVALID_POINTER;
mixer_ctx_t *mix = handle_get(mixer);
if (!mix)
return VDP_STATUS_INVALID_HANDLE;
handle_release(mixer);
return VDP_STATUS_OK;
}
VdpStatus vdp_video_mixer_get_feature_enables(VdpVideoMixer mixer, uint32_t feature_count, VdpVideoMixerFeature const *features, VdpBool *feature_enables)
{
if (!features || !feature_enables)
return VDP_STATUS_INVALID_POINTER;
mixer_ctx_t *mix = handle_get(mixer);
if (!mix)
return VDP_STATUS_INVALID_HANDLE;
handle_release(mixer);
return VDP_STATUS_ERROR;
}
static void set_csc_matrix(mixer_ctx_t *mix, const VdpCSCMatrix *matrix)
{
mix->csc_change = 1;
// default contrast for full-range has 1.0 as luma coefficients
mix->contrast = ((*matrix)[0][0] + (*matrix)[1][0] + (*matrix)[2][0]) / 3;
// the way brightness and contrast work with this driver, brightness
// is the brightness of a "black" pixel
mix->brightness = ((*matrix)[0][1] + (*matrix)[1][1] + (*matrix)[2][1]) / 2 +
((*matrix)[0][2] + (*matrix)[1][2] + (*matrix)[2][2]) / 2 +
(*matrix)[0][3] + (*matrix)[1][3] + (*matrix)[2][3];
mix->brightness /= 3;
float sin = (*matrix)[0][1] + (*matrix)[2][2];
float cos = (*matrix)[0][2] + (*matrix)[2][1];
float e = 0.001;
if (-e < cos && cos < e) mix->hue = M_PI;
else mix->hue = atanf(sin/cos);
mix->saturation = sqrtf(sin * sin + cos * cos) / (1.403 + 1.773);
}
VdpStatus vdp_video_mixer_set_attribute_values(VdpVideoMixer mixer, uint32_t attribute_count, VdpVideoMixerAttribute const *attributes, void const *const *attribute_values)
{
if (!attributes || !attribute_values)
return VDP_STATUS_INVALID_POINTER;
mixer_ctx_t *mix = handle_get(mixer);
if (!mix)
return VDP_STATUS_INVALID_HANDLE;
uint32_t i;
for (i = 0; i < attribute_count; i++)
if (attributes[i] == VDP_VIDEO_MIXER_ATTRIBUTE_CSC_MATRIX)
set_csc_matrix(mix, (const VdpCSCMatrix *)attribute_values[i]);
handle_release(mixer);
return VDP_STATUS_OK;
}
VdpStatus vdp_video_mixer_get_parameter_values(VdpVideoMixer mixer, uint32_t parameter_count, VdpVideoMixerParameter const *parameters, void *const *parameter_values)
{
if (!parameters || !parameter_values)
return VDP_STATUS_INVALID_POINTER;
mixer_ctx_t *mix = handle_get(mixer);
if (!mix)
return VDP_STATUS_INVALID_HANDLE;
handle_release(mixer);
return VDP_STATUS_ERROR;
}
VdpStatus vdp_video_mixer_get_attribute_values(VdpVideoMixer mixer, uint32_t attribute_count, VdpVideoMixerAttribute const *attributes, void *const *attribute_values)
{
if (!attributes || !attribute_values)
return VDP_STATUS_INVALID_POINTER;
mixer_ctx_t *mix = handle_get(mixer);
if (!mix)
return VDP_STATUS_INVALID_HANDLE;
handle_release(mixer);
return VDP_STATUS_ERROR;
}
VdpStatus vdp_video_mixer_query_feature_support(VdpDevice device, VdpVideoMixerFeature feature, VdpBool *is_supported)
{
if (!is_supported)
return VDP_STATUS_INVALID_POINTER;
device_ctx_t *dev = handle_get(device);
if (!dev)
return VDP_STATUS_INVALID_HANDLE;
*is_supported = VDP_FALSE;
handle_release(device);
return VDP_STATUS_OK;
}
VdpStatus vdp_video_mixer_query_parameter_support(VdpDevice device, VdpVideoMixerParameter parameter, VdpBool *is_supported)
{
if (!is_supported)
return VDP_STATUS_INVALID_POINTER;
device_ctx_t *dev = handle_get(device);
if (!dev)
return VDP_STATUS_INVALID_HANDLE;
switch (parameter)
{
case VDP_VIDEO_MIXER_PARAMETER_CHROMA_TYPE:
case VDP_VIDEO_MIXER_PARAMETER_LAYERS:
case VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_HEIGHT:
case VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_WIDTH:
*is_supported = VDP_TRUE;
break;
default:
*is_supported = VDP_FALSE;
break;
}
handle_release(device);
return VDP_STATUS_OK;
}
VdpStatus vdp_video_mixer_query_parameter_value_range(VdpDevice device, VdpVideoMixerParameter parameter, void *min_value, void *max_value)
{
if (!min_value || !max_value)
return VDP_STATUS_INVALID_POINTER;
device_ctx_t *dev = handle_get(device);
if (!dev)
return VDP_STATUS_INVALID_HANDLE;
switch (parameter)
{
case VDP_VIDEO_MIXER_PARAMETER_LAYERS:
*(uint32_t *)min_value = 0;
*(uint32_t *)max_value = 0;
return VDP_STATUS_OK;
case VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_HEIGHT:
case VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_WIDTH:
*(uint32_t *)min_value = 0;
*(uint32_t *)max_value = 8192;
return VDP_STATUS_OK;
}
handle_release(device);
return VDP_STATUS_ERROR;
}
VdpStatus vdp_video_mixer_query_attribute_support(VdpDevice device, VdpVideoMixerAttribute attribute, VdpBool *is_supported)
{
if (!is_supported)
return VDP_STATUS_INVALID_POINTER;
device_ctx_t *dev = handle_get(device);
if (!dev)
return VDP_STATUS_INVALID_HANDLE;
*is_supported = VDP_FALSE;
handle_release(device);
return VDP_STATUS_OK;
}
VdpStatus vdp_video_mixer_query_attribute_value_range(VdpDevice device, VdpVideoMixerAttribute attribute, void *min_value, void *max_value)
{
int status = VDP_STATUS_ERROR;
if (!min_value || !max_value)
return VDP_STATUS_INVALID_POINTER;
device_ctx_t *dev = handle_get(device);
if (!dev)
return VDP_STATUS_INVALID_HANDLE;
switch (attribute)
{
case VDP_VIDEO_MIXER_ATTRIBUTE_BACKGROUND_COLOR:
case VDP_VIDEO_MIXER_ATTRIBUTE_CSC_MATRIX:
{
status = VDP_STATUS_ERROR;
break;
}
case VDP_VIDEO_MIXER_ATTRIBUTE_LUMA_KEY_MAX_LUMA:
case VDP_VIDEO_MIXER_ATTRIBUTE_LUMA_KEY_MIN_LUMA:
case VDP_VIDEO_MIXER_ATTRIBUTE_NOISE_REDUCTION_LEVEL:
{
*(float *)min_value = 0.0;
*(float *)max_value = 1.0;
status = VDP_STATUS_OK;
break;
}
case VDP_VIDEO_MIXER_ATTRIBUTE_SHARPNESS_LEVEL:
{
*(float *)min_value = -1.0;
*(float *)max_value = 1.0;
status = VDP_STATUS_OK;
break;
}
case VDP_VIDEO_MIXER_ATTRIBUTE_SKIP_CHROMA_DEINTERLACE:
{
*(uint8_t *)min_value = 0;
*(uint8_t *)max_value = 1;
status = VDP_STATUS_OK;
break;
}
}
handle_release(device);
return status;
}
VdpStatus vdp_generate_csc_matrix(VdpProcamp *procamp, VdpColorStandard standard, VdpCSCMatrix *csc_matrix)
{
if (!csc_matrix || !procamp)
return VDP_STATUS_INVALID_POINTER;
if (procamp->struct_version > VDP_PROCAMP_VERSION)
return VDP_STATUS_INVALID_STRUCT_VERSION;
// BT.601 table
(*csc_matrix)[0][1] = 0.000;
(*csc_matrix)[0][2] = 1.403;
(*csc_matrix)[1][1] = -0.344;
(*csc_matrix)[1][2] = -0.714;
(*csc_matrix)[2][1] = 1.773;
(*csc_matrix)[2][2] = 0.000;
float uvcos = procamp->saturation * cosf(procamp->hue);
float uvsin = procamp->saturation * sinf(procamp->hue);
int i;
for (i = 0; i < 3; i++) {
(*csc_matrix)[i][0] = procamp->contrast;
float u = (*csc_matrix)[i][1] * uvcos + (*csc_matrix)[i][2] * uvsin;
float v = (*csc_matrix)[i][1] * uvsin + (*csc_matrix)[i][2] * uvcos;
(*csc_matrix)[i][1] = u;
(*csc_matrix)[i][2] = v;
(*csc_matrix)[i][3] = - (u + v) / 2;
(*csc_matrix)[i][3] += 0.5 - procamp->contrast / 2;
(*csc_matrix)[i][3] += procamp->brightness;
}
return VDP_STATUS_OK;
}