summaryrefslogtreecommitdiff
path: root/simple-yuv.c
blob: 052983ada1c951f8227617e0300863e4e58b9f80 (plain)
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/*
 * Copyright © 2011 Benjamin Franzke
 * Copyright © 2010 Intel Corporation
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting documentation, and
 * that the name of the copyright holders not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  The copyright holders make no representations
 * about the suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THIS SOFTWARE.
 */

#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
#include <unistd.h>
#include <sys/mman.h>
#include <signal.h>

#include <libdrm/intel_bufmgr.h>
#include <xf86drm.h>
#include <wayland-client.h>

#include "wayland-drm-client-protocol.h"

struct display {
	struct wl_display *display;
	struct wl_compositor *compositor;
	struct wl_shell *shell;
	struct wl_drm *drm;
	drm_intel_bufmgr *bufmgr;
	int fd;
	int authenticated;
	uint32_t formats[10];
	int format_count;
	uint32_t mask;
};

struct buffer {
	drm_intel_bo *bo;
	struct wl_buffer *buffer;
	int offset0, stride0;
	int offset1, stride1;
	int offset2, stride2;
};

struct window {
	struct display *display;
	int width, height;
	struct wl_surface *surface;
	struct wl_shell_surface *shell_surface;
	struct wl_callback *callback;

	struct buffer *front, *back;
	uint32_t frame_duration, next_frame;
	uint32_t format;
	int has_timestamp;
	unsigned char *y, *u, *v;
	FILE *source;

	void (*paint)(struct window *window, uint32_t time);
};

static void
handle_ping(void *data, struct wl_shell_surface *shell_surface,
							uint32_t serial)
{
	wl_shell_surface_pong(shell_surface, serial);
}

static void
handle_configure(void *data, struct wl_shell_surface *shell_surface,
		 uint32_t edges, int32_t width, int32_t height)
{
}

static void
handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
{
}

static const struct wl_shell_surface_listener shell_surface_listener = {
	handle_ping,
	handle_configure,
	handle_popup_done
};

static void
paint_source(struct window *window, uint32_t time)
{
	int size = window->width * window->height;
	char buf[256];

	fgets(buf, sizeof buf, window->source);
	fread(window->y, 1, size, window->source);
	fread(window->u, 1, size / 4, window->source);
	fread(window->v, 1, size / 4, window->source);
}

static void
paint_pixels(struct window *window, uint32_t time)
{
	int width = window->width, height = window->height;
	const int halfh = height / 2;
	const int halfw = width / 2;
	int ir, or;
	uint8_t *y = window->y;
	uint8_t *u = window->u;
	uint8_t *v = window->v;
	int i, x;

	/* squared radii thresholds */
	or = (halfw < halfh ? halfw : halfh) - 8;
	ir = or - 32;
	or *= or;
	ir *= ir;

	for (i = 0; i < height; i++) {
		int x;
		int y2 = (i - halfh) * (i - halfh);

		for (x = 0; x < width; x++) {
			uint32_t v;

			/* squared distance from center */
			int r2 = (x - halfw) * (x - halfw) + y2;

			if (r2 < ir)
				v = (r2 / 32 + time / 64) * 0x0080401;
			else if (r2 < or)
				v = (i + time / 32) * 0x0080401;
			else
				v = (x + time / 16) * 0x0080401;
			v &= 0x00ffffff;

			*y++ = v;
		}
	}

	for (i = 0; i < halfh; i++) {
		for (x = 0; x < halfw; x++) {
			*u++ = i > x + 30 ? 200 : 10;
			*v++ = i > x - 30 ? 150 : 50;
		}
	}
}

static void
convert_to_yuyv(struct window *window)
{
	struct buffer *buffer = window->back;
	unsigned char *y, *u, *v, *yuyv_out, *p;
	int i, j;

	for (i = 0; i < window->height; i++) {
		p = buffer->bo->virtual;
		yuyv_out = p + buffer->offset0 + i * window->back->stride0;

		y = window->y + i * window->width;
		u = window->u + (i / 2) * window->width / 2;
		v = window->v + (i / 2) * window->width / 2;

		for (j = 0; j < window->width / 2; j++) {
			*yuyv_out++ = *y++;
			*yuyv_out++ = *u++;
			*yuyv_out++ = *y++;
			*yuyv_out++ = *v++;
		}
	}
}

static void
convert_to_nv12(struct window *window)
{
	struct buffer *buffer = window->back;
	unsigned char *u, *v, *uv_out, *p;
	int i;

	p = buffer->bo->virtual;
	uv_out = p + buffer->offset1;
	u = window->u;
	v = window->v;
	for (i = 0; i < window->width * window->height / 4; i++) {
		*uv_out++ = *u++;
		*uv_out++ = *v++;
	}
}

static void
paint(struct window *window, uint32_t time)
{
	struct buffer *buffer = window->back;
	unsigned char *p = buffer->bo->virtual;

	switch (window->format) {
	case WL_DRM_FORMAT_YUV420:
		window->y = p + buffer->offset0;
		window->u = p + buffer->offset1;
		window->v = p + buffer->offset2;
		break;
	case WL_DRM_FORMAT_NV12:
		window->y = p + buffer->offset0;
		break;
	}

	window->paint(window, time);

	switch (window->format) {
	case WL_DRM_FORMAT_YUYV:
		convert_to_yuyv(window);
		break;
	case WL_DRM_FORMAT_NV12:
		convert_to_nv12(window);
		break;
	}
}

static const struct wl_callback_listener frame_listener;

static void
redraw(void *data, struct wl_callback *callback, uint32_t time)
{
	struct window *window = data;
	struct buffer *tmp;

	if (!window->has_timestamp || window->next_frame < time ||
	    window->frame_duration == 0) {
		paint(window, time);
		wl_surface_attach(window->surface, window->back->buffer, 0, 0);

		tmp = window->back;
		window->back = window->front;
		window->front = tmp;
		window->has_timestamp = 1;
		window->next_frame = time + window->frame_duration;
	}

	wl_surface_damage(window->surface,
			  0, 0, window->width, window->height);

	if (callback)
		wl_callback_destroy(callback);

	window->callback = wl_surface_frame(window->surface);
	wl_callback_add_listener(window->callback, &frame_listener, window);
}

static const struct wl_callback_listener frame_listener = {
	redraw
};

static inline int
align(int value, int size)
{
	return (value + size - 1) & ~(size - 1);
}

struct buffer *
create_buffer(struct window *window, uint32_t format)
{
	struct display *display = window->display;
	struct buffer *buffer;
	uint32_t name;
	int total;

	buffer = malloc(sizeof *buffer);
	if (format == WL_DRM_FORMAT_YUYV)
		buffer->stride0 = window->width * 4;
	else
		buffer->stride0 = window->width;

	buffer->offset0 = 0;
	total = buffer->stride0 * window->height;

	if (format == WL_DRM_FORMAT_YUV420)
		buffer->stride1 = window->width / 2;
	else if (format == WL_DRM_FORMAT_NV12)
		buffer->stride1 = window->width;
	else
		buffer->stride1 = 0;

	buffer->offset1 = align(total, 4096);
	total = buffer->offset1 + buffer->stride1 * window->height / 2;

	if (format == WL_DRM_FORMAT_YUV420)
		buffer->stride2 = window->width / 2;
	else
		buffer->stride2 = 0;

	buffer->offset2 = align(total, 4096);
	total = buffer->offset2 + buffer->stride2 * window->height / 2;

	buffer->bo =
		drm_intel_bo_alloc(display->bufmgr, "simple-yuv", total, 0);
	drm_intel_gem_bo_map_gtt(buffer->bo);

	drm_intel_bo_flink(buffer->bo, &name);

	switch (format) {
	case WL_DRM_FORMAT_YUV420:
	case WL_DRM_FORMAT_NV12:
		buffer->buffer =
			wl_drm_create_planar_buffer(display->drm, name,
						    window->width,
						    window->height,
						    format,
						    buffer->offset0,
						    buffer->stride0,
						    buffer->offset1,
						    buffer->stride1,
						    buffer->offset2,
						    buffer->stride2);
		break;
	case WL_DRM_FORMAT_YUYV:
		buffer->buffer =
			wl_drm_create_buffer(display->drm, name,
					     window->width, window->height,
					     buffer->stride0,
					     WL_DRM_FORMAT_YUYV);
		break;
	}

	return buffer;
}

static struct window *
create_window(struct display *display, uint32_t format, int width, int height)
{
	struct window *window;
	int i;

	for (i = 0; i < display->format_count; i++) {
		if (display->formats[i] == WL_DRM_FORMAT_YUV420)
			break;
	}

	if (i == display->format_count)  {
		fprintf(stderr,
			"format WL_BUFFER_FORMAT_ARGB8888 not supported\n");
		return NULL;
	}		

	window = malloc(sizeof *window);

	window->format = format;
	window->callback = NULL;
	window->display = display;
	window->width = width;
	window->height = height;
	window->surface = wl_compositor_create_surface(display->compositor);
	window->shell_surface = wl_shell_get_shell_surface(display->shell,
							   window->surface);

	if (window->shell_surface)
		wl_shell_surface_add_listener(window->shell_surface,
					      &shell_surface_listener, window);

	wl_shell_surface_set_toplevel(window->shell_surface);

	window->front = create_buffer(window, window->format);
	window->back = create_buffer(window, window->format);

	switch (window->format) {
	case WL_DRM_FORMAT_YUV420:
		break;
	case WL_DRM_FORMAT_YUYV:
		window->y = malloc(window->width * window->height);
		window->u = malloc(window->width * window->height / 4);
		window->v = malloc(window->width * window->height / 4);
		break;
	case WL_DRM_FORMAT_NV12:
		window->u = malloc(window->width * window->height / 4);
		window->v = malloc(window->width * window->height / 4);
		break;
	}

	return window;
}

static void
destroy_window(struct window *window)
{
	if (window->callback)
		wl_callback_destroy(window->callback);

	wl_shell_surface_destroy(window->shell_surface);
	wl_surface_destroy(window->surface);
	free(window);
}

static void
drm_handle_device(void *data, struct wl_drm *drm, const char *device)
{
	struct display *d = data;
	drm_magic_t magic;

	d->fd = open(device, O_RDWR | O_CLOEXEC);
	if (d->fd == -1) {
		fprintf(stderr, "could not open %s: %m\n", device);
		exit(-1);
	}

	drmGetMagic(d->fd, &magic);
	wl_drm_authenticate(d->drm, magic);
}

static void
drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)
{
   struct display *d = data;

   d->formats[d->format_count++] = format;
}

static void
drm_handle_authenticated(void *data, struct wl_drm *drm)
{
   struct display *d = data;

   d->authenticated = 1;

   d->bufmgr = drm_intel_bufmgr_gem_init(d->fd, 4096);
}

static const struct wl_drm_listener drm_listener = {
	drm_handle_device,
	drm_handle_format,
	drm_handle_authenticated
};

static void
display_handle_global(struct wl_display *display, uint32_t id,
		      const char *interface, uint32_t version, void *data)
{
	struct display *d = data;

	if (strcmp(interface, "wl_compositor") == 0) {
		d->compositor =
			wl_display_bind(display, id, &wl_compositor_interface);
	} else if (strcmp(interface, "wl_shell") == 0) {
		d->shell = wl_display_bind(display, id, &wl_shell_interface);
	} else if (strcmp(interface, "wl_drm") == 0) {
		d->drm = wl_display_bind(display, id, &wl_drm_interface);
		wl_drm_add_listener(d->drm, &drm_listener, d);
	}
}

static struct display *
create_display(void)
{
	struct display *display;

	display = malloc(sizeof *display);
	memset(display, 0, sizeof *display);
	display->display = wl_display_connect(NULL);
	assert(display->display);

	wl_display_add_global_listener(display->display,
				       display_handle_global, display);

	return display;
}

static void
destroy_display(struct display *display)
{
	if (display->drm)
		wl_drm_destroy(display->drm);

	if (display->shell)
		wl_shell_destroy(display->shell);

	if (display->compositor)
		wl_compositor_destroy(display->compositor);

	wl_display_flush(display->display);
	wl_display_disconnect(display->display);
	free(display);
}

static struct window *
parse_header(struct display *display, uint32_t format, FILE *source)
{
	struct window *window;
	int width, height, n, d;
	char buf[256], *p;
	uint32_t frame_duration;

	fgets(buf, sizeof buf, source);
	if (strncmp(buf, "YUV4MPEG2 ", 10) != 0)
		return NULL;
	frame_duration = 0;
	width = 0;
	height = 0;
	for (p = buf + 10; *p; p = strchrnul(p, ' ')) {
		p++;
		switch (*p) {
		case 'C':
			break;
		case 'W':
			width = atoi(p + 1);
			break;
		case 'H':
			height = atoi(p + 1);
			break;
		case 'F':
			sscanf(p + 1, "%d:%d", &n, &d);
			frame_duration = d * 1000 / n;
			break;
		}
	}

	if (width == 0 || height == 0) {
		fprintf(stderr, "didn't width or height\n");
		return NULL;
	}

	window = create_window(display, format, width, height);
	window->source = source;
	window->frame_duration = frame_duration;
	window->paint = paint_source;

	printf("incoming stream is %dx%d, %d:%d fps (%dms per frame)\n",
	       width, height, n, d, frame_duration);

	return window;
}

int
main(int argc, char **argv)
{
	struct display *display;
	struct window *window;
	uint32_t format = WL_DRM_FORMAT_YUV420;
	FILE *source = NULL;
	int i;

	for (i = 1; i < argc; i++) {
		if (strcmp(argv[i], "--yuyv") == 0)
			format = WL_DRM_FORMAT_YUYV;
		if (strcmp(argv[i], "--yuv") == 0)
			format = WL_DRM_FORMAT_YUV420;
		if (strcmp(argv[i], "--nv12") == 0)
			format = WL_DRM_FORMAT_NV12;
		if (strcmp(argv[i], "-") == 0)
			source = stdin;
	}

	display = create_display();
	while (!display->authenticated)
		wl_display_roundtrip(display->display);

	if (source) {
		window = parse_header(display, format, source);
	} else {
		window = create_window(display, format, 512, 512);
		window->paint = paint_pixels;
	}

	redraw(window, NULL, 0);
	while (1) {
		wl_display_flush(display->display);
		wl_display_iterate(display->display, WL_DISPLAY_READABLE);
	}

	destroy_window(window);
	destroy_display(display);

	return 0;
}