-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
330 lines (292 loc) · 12.2 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
//#include <vdpau/vdpau.h>
#include <vdpau/vdpau_x11.h>
#include "vdpau_layer.h"
#define WIDTH 1280
#define HEIGHT 720
static Display *x11_display;
static int x11_screen;
static Window x11_window;
static GC x11_gc;
// VDPAU variables
static VdpDevice vdp_device;
static VdpDecoder vdp_decoder;
static VdpVideoSurface vdp_video_surface;
static VdpOutputSurface vdp_output_surface;
static VdpPresentationQueueTarget vdp_target;
static VdpPresentationQueue vdp_queue;
static uint32_t vid_width, vid_height;
static VdpChromaType vdp_chroma_type;
static VdpVideoMixer vdp_video_mixer;
// VDPAU functions (That will be dynamically linked)
VdpDeviceDestroy *vdp_device_destroy;
VdpGetProcAddress *vdp_get_proc_address;
VdpGetErrorString *vdp_get_error_string;
VdpVideoSurfaceCreate *vdp_video_surface_create;
VdpVideoSurfaceDestroy *vdp_video_surface_destroy;
VdpVideoSurfaceQueryCapabilities *vdp_video_surface_query_capabilities;
VdpVideoSurfaceQueryGetPutBitsYCbCrCapabilities *vdp_video_surface_query_ycbcr_capabilities;
VdpVideoSurfaceGetParameters *vdp_video_surface_get_parameters;
VdpVideoSurfaceGetBitsYCbCr *vdp_video_surface_get_bits_ycbcr;
VdpVideoSurfacePutBitsYCbCr *vdp_video_surface_put_bits_ycbcr;
VdpDecoderCreate *vdp_decoder_create;
VdpDecoderDestroy *vdp_decoder_destroy;
VdpDecoderRender *vdp_decoder_render;
VdpDecoderQueryCapabilities *vdp_decoder_query_capabilities;
VdpDecoderGetParameters *vdp_decoder_get_parameters;
VdpVideoMixerCreate *vdp_video_mixer_create;
VdpVideoMixerDestroy *vdp_video_mixer_destroy;
VdpVideoMixerRender *vdp_video_mixer_render;
VdpVideoMixerSetFeatureEnables *vdp_video_mixer_set_feature_enables;
VdpVideoMixerSetAttributeValues *vdp_video_mixer_set_attribute_values;
VdpOutputSurfaceCreate *vdp_output_surface_create;
VdpOutputSurfaceDestroy *vdp_output_surface_destroy;
VdpOutputSurfaceQueryCapabilities *vdp_output_surface_query_capabilities;
VdpOutputSurfaceGetBitsNative *vdp_output_surface_get_bits_native;
VdpPresentationQueueTargetCreateX11 *vdp_presentation_queue_target_create_x11;
VdpPresentationQueueTargetDestroy *vdp_presentation_queue_target_destroy;
VdpPresentationQueueCreate *vdp_presentation_queue_create;
VdpPresentationQueueDestroy *vdp_presentation_queue_destroy;
VdpPresentationQueueDisplay *vdp_presentation_queue_display;
VdpPresentationQueueBlockUntilSurfaceIdle *vdp_presentation_queue_block_until_surface_idle;
VdpPresentationQueueSetBackgroundColor *vdp_presentation_queue_set_background_color;
VdpPresentationQueueQuerySurfaceStatus *vdp_presentation_queue_query_surface_status;
VdpGetInformationString *vdp_get_information_string;
int init_x11() {
unsigned long black, white;
x11_display = XOpenDisplay((char*)0); //open display 0
if (!x11_display) {
fprintf(stderr, "Failed to open XDisplay\n");
return -1;
}
x11_screen = DefaultScreen(x11_display);
black = BlackPixel(x11_display, x11_screen);
white = WhitePixel(x11_display, x11_screen);
x11_window = XCreateSimpleWindow(x11_display, DefaultRootWindow(x11_display), 0, 0,
300, 300, 5, black, white);
XSetStandardProperties(x11_display, x11_window, "VDPAU Test", "VDPAU",
None, NULL, 0, NULL);
XSelectInput(x11_display,x11_window,ExposureMask|ButtonPressMask|KeyPressMask);
x11_gc = XCreateGC(x11_display, x11_window, 0, NULL);
XSetBackground(x11_display, x11_gc, white);
XSetBackground(x11_display, x11_gc, black);
XClearWindow(x11_display, x11_window);
XMapWindow(x11_display, x11_window);
printf("X11 Display created\n");
return 0;
}
VdpStatus init_vdpau() {
VdpStatus retval = VDP_STATUS_OK;
struct VdpFunction {
const int id;
void *pointer;
};
struct VdpFunction vdp_function[] = {
{VDP_FUNC_ID_DEVICE_DESTROY, &vdp_device_destroy},
{VDP_FUNC_ID_VIDEO_SURFACE_CREATE,
&vdp_video_surface_create},
{VDP_FUNC_ID_VIDEO_SURFACE_DESTROY,
&vdp_video_surface_destroy},
{VDP_FUNC_ID_VIDEO_SURFACE_QUERY_CAPABILITIES,
&vdp_video_surface_query_capabilities},
{VDP_FUNC_ID_VIDEO_SURFACE_QUERY_GET_PUT_BITS_Y_CB_CR_CAPABILITIES,
&vdp_video_surface_query_ycbcr_capabilities},
{VDP_FUNC_ID_VIDEO_SURFACE_GET_BITS_Y_CB_CR,
&vdp_video_surface_get_bits_ycbcr},
{VDP_FUNC_ID_VIDEO_SURFACE_PUT_BITS_Y_CB_CR,
&vdp_video_surface_put_bits_ycbcr},
{VDP_FUNC_ID_VIDEO_SURFACE_GET_PARAMETERS,
&vdp_video_surface_get_parameters},
{VDP_FUNC_ID_DECODER_CREATE, &vdp_decoder_create},
{VDP_FUNC_ID_DECODER_RENDER, &vdp_decoder_render},
{VDP_FUNC_ID_DECODER_DESTROY, &vdp_decoder_destroy},
{VDP_FUNC_ID_DECODER_QUERY_CAPABILITIES,
&vdp_decoder_query_capabilities},
{VDP_FUNC_ID_DECODER_GET_PARAMETERS,
&vdp_decoder_get_parameters},
{VDP_FUNC_ID_VIDEO_MIXER_CREATE, &vdp_video_mixer_create},
{VDP_FUNC_ID_VIDEO_MIXER_DESTROY, &vdp_video_mixer_destroy},
{VDP_FUNC_ID_VIDEO_MIXER_RENDER, &vdp_video_mixer_render},
{VDP_FUNC_ID_VIDEO_MIXER_SET_FEATURE_ENABLES,
&vdp_video_mixer_set_feature_enables},
{VDP_FUNC_ID_VIDEO_MIXER_SET_ATTRIBUTE_VALUES,
&vdp_video_mixer_set_attribute_values},
{VDP_FUNC_ID_OUTPUT_SURFACE_CREATE, &vdp_output_surface_create},
{VDP_FUNC_ID_OUTPUT_SURFACE_DESTROY, &vdp_output_surface_destroy},
{VDP_FUNC_ID_OUTPUT_SURFACE_QUERY_CAPABILITIES,
&vdp_output_surface_query_capabilities},
{VDP_FUNC_ID_OUTPUT_SURFACE_GET_BITS_NATIVE,
&vdp_output_surface_get_bits_native},
{VDP_FUNC_ID_PRESENTATION_QUEUE_TARGET_CREATE_X11,
&vdp_presentation_queue_target_create_x11},
{VDP_FUNC_ID_PRESENTATION_QUEUE_TARGET_DESTROY,
&vdp_presentation_queue_target_destroy},
{VDP_FUNC_ID_PRESENTATION_QUEUE_CREATE,
&vdp_presentation_queue_create},
{VDP_FUNC_ID_PRESENTATION_QUEUE_DESTROY,
&vdp_presentation_queue_destroy},
{VDP_FUNC_ID_PRESENTATION_QUEUE_DISPLAY,
&vdp_presentation_queue_display},
{VDP_FUNC_ID_PRESENTATION_QUEUE_BLOCK_UNTIL_SURFACE_IDLE,
&vdp_presentation_queue_block_until_surface_idle},
{VDP_FUNC_ID_PRESENTATION_QUEUE_SET_BACKGROUND_COLOR,
&vdp_presentation_queue_set_background_color},
{VDP_FUNC_ID_PRESENTATION_QUEUE_QUERY_SURFACE_STATUS,
&vdp_presentation_queue_query_surface_status},
{VDP_FUNC_ID_GET_INFORMATION_STRING, &vdp_get_information_string},
{0,NULL}
};
retval = vdp_device_create_x11(x11_display, x11_screen, &vdp_device,&vdp_get_proc_address);
if (retval != VDP_STATUS_OK) {
fprintf(stderr, "Failed to create X11 device\n");
return -1;
}
fprintf(stdout,"X11 device created\n");
const struct VdpFunction *dsc;
for (dsc = vdp_function; dsc->pointer; dsc++) {
retval = vdp_get_proc_address(vdp_device, dsc->id,dsc->pointer);
if (retval != VDP_STATUS_OK) {
fprintf(stderr,"Failed to link function id: %d\n",dsc->id);
return retval;
}
}
fprintf(stdout,"Linked all functions\n");
return retval;
}
int init_decoder() {
VdpStatus retval = VDP_STATUS_OK;
retval = vdp_decoder_create(vdp_device,VDP_DECODER_PROFILE_H264_MAIN,
WIDTH,HEIGHT,2,&vdp_decoder);
if (retval != VDP_STATUS_OK) {
fprintf(stderr,"Decoder create failed with error %d\n",retval);
return retval;
}
fprintf(stdout,"Decoder created\n");
return retval;
}
int init_surfaces() {
VdpStatus vdpret;
vdpret = vdp_video_surface_create(vdp_device, VDP_CHROMA_TYPE_420,
WIDTH,HEIGHT,&vdp_video_surface);
if (vdpret != VDP_STATUS_OK) {
fprintf(stderr,"Failed to create video surface\n");
return vdpret;
}
vdpret = vdp_output_surface_create(vdp_device, VDP_RGBA_FORMAT_B8G8R8A8,
WIDTH,HEIGHT, &vdp_output_surface);
if (vdpret != VDP_STATUS_OK) {
fprintf(stderr, "Failed to create output surface: %d\n",vdpret);
return vdpret;
}
return vdpret;
}
int init_video_mixer() {
#define VDP_NUM_MIXER_PARAMETER 3
#define MAX_NUM_FEATURES 6
VdpStatus vdpret;
int feature_count = 0;
VdpVideoMixerFeature features[MAX_NUM_FEATURES];
VdpBool features_enables[MAX_NUM_FEATURES];
static const VdpVideoMixerParameter parameters[VDP_NUM_MIXER_PARAMETER] = {
VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_WIDTH,
VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_HEIGHT,
VDP_VIDEO_MIXER_PARAMETER_CHROMA_TYPE
};
const void *const parameter_value[VDP_NUM_MIXER_PARAMETER] = {
&vid_width,
&vid_height,
&vdp_chroma_type
};
vdpret = vdp_video_mixer_create(vdp_device, feature_count, features,
VDP_NUM_MIXER_PARAMETER, parameters,
parameter_value, &vdp_video_mixer);
if (vdpret != VDP_STATUS_OK) {
fprintf(stderr,"Failed to create mixer %d\n", vdpret);
return vdpret;
}
fprintf(stdout,"Created videomixer\n");
return vdpret;
}
int init_presentation_queue() {
VdpStatus vdpret;
vdpret = vdp_presentation_queue_target_create_x11(vdp_device, x11_window,
&vdp_target);
if (vdpret != VDP_STATUS_OK) {
fprintf(stderr,"Failed to create x11 target queue\n");
return vdpret;
}
vdpret = vdp_presentation_queue_create(vdp_device, vdp_target, &vdp_queue);
if (vdpret != VDP_STATUS_OK) {
fprintf(stderr,"Failed to create queue\n");
return vdpret;
}
fprintf(stdout,"Created presentation queue\n");
return vdpret;
}
char *generate_garbage_buffer(int width, int height) {
long i;
char *buf = malloc(width * height);
if (!buf) return NULL;
for (i = 0; i < (width * height); i++) {
buf[i] = (i & 0xFF);
}
return buf;
}
int main() {
/*XEvent event;
int retval;
vid_width = 300;
vid_height = 300;
char *garbage;
const char *info;
VdpStatus vdpret = VDP_STATUS_OK;
retval = init_x11();
if (retval) {
fprintf(stderr,"Failed to init X11");
return -1;
}
vdpret = init_vdpau();
if (vdpret != VDP_STATUS_OK) {
fprintf(stderr,"Failed to init VDPAU");
return -1;
}
vdpret = vdp_get_information_string(&info);
fprintf(stdout,"%s\n",info);
retval = init_decoder();
if (retval != VDP_STATUS_OK) {
fprintf(stderr,"Failed to init decoder");
return -1;
}
vdpret = init_surfaces();
if (vdpret != VDP_STATUS_OK) {
fprintf(stderr, "Failed to init surfaces");
return -1;
}
fprintf(stdout,"VDP surfaces created\n");
garbage = generate_garbage_buffer(WIDTH,HEIGHT);
fprintf(stdout,"Generated some garbage\n");
vdpret = init_video_mixer();
if (vdpret != VDP_STATUS_OK) {
fprintf(stderr,"Failed to create video mixer");
return -1;
}
vdpret = init_presentation_queue();
if (vdpret != VDP_STATUS_OK) {
fprintf(stderr,"Failed to presentation queue\n");
return -1;
}
while(1) {
XNextEvent(x11_display, &event);
if (event.type == KeyPress) {
XFreeGC(x11_display, x11_gc);
XDestroyWindow(x11_display, x11_window);
XCloseDisplay(x11_display);
exit(0);
}
}*/
init_vpdau_ctx();
}