summaryrefslogtreecommitdiff
path: root/simple-yuv.c
blob: f21e26f5202160a2d97c94fb683252bff5811452 (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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
/*
 * 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 <linux/input.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_registry *registry;
	struct wl_compositor *compositor;
	struct wl_shell *shell;
	struct wl_drm *drm;
	struct wl_seat *seat;
	struct wl_keyboard *keyboard;
	drm_intel_bufmgr *bufmgr;
	int fd;
	int authenticated;
	int has_prime;
	uint32_t formats[10];
	int format_count;
	uint32_t mask;
	struct wl_surface *keyboard_focus;
};

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;
	int fullscreen;

	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);

	wl_surface_commit(window->surface);
}

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, fd;

	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);


	if (display->has_prime) {
		drm_intel_bo_gem_export_to_prime(buffer->bo, &fd);
		buffer->buffer =
			wl_drm_create_prime_buffer(display->drm, fd,
						   window->width,
						   window->height,
						   format,
						   buffer->offset0,
						   buffer->stride0,
						   buffer->offset1,
						   buffer->stride1,
						   buffer->offset2,
						   buffer->stride2);
		close(fd);
	} else {
		drm_intel_bo_flink(buffer->bo, &name);

		if (format == WL_DRM_FORMAT_YUYV) {
			buffer->buffer =
				wl_drm_create_buffer(display->drm, name,
						     window->width,
						     window->height,
						     buffer->stride0,
						     WL_DRM_FORMAT_YUYV);
		} else {
			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);
		}
	}

	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->fullscreen = 0;
	window->surface = wl_compositor_create_surface(display->compositor);
	wl_surface_set_user_data(window->surface, window);
	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 void
drm_handle_capabilities(void *data, struct wl_drm *drm, uint32_t value)
{
	struct display *d = data;

	switch (value) {
	case WL_DRM_CAPABILITY_PRIME:
		d->has_prime = 1;
		break;
	}
}

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

static void
keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
		       uint32_t format, int fd, uint32_t size)
{
}

static void
keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
		      uint32_t serial, struct wl_surface *surface,
		      struct wl_array *keys)
{
	struct display *d = data;

	d->keyboard_focus = surface;
}

static void
keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
		      uint32_t serial, struct wl_surface *surface)
{
	struct display *d = data;

	d->keyboard_focus = NULL;
}

static void
keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
		    uint32_t serial, uint32_t time, uint32_t key,
		    uint32_t state)
{
	struct display *d = data;
	struct window *window = wl_surface_get_user_data(d->keyboard_focus);

	if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
		return;

	switch (key) {
	case KEY_ESC:
		exit(1);
	case KEY_F11:
		if (window->fullscreen)
			wl_shell_surface_set_toplevel(window->shell_surface);
		else
			wl_shell_surface_set_fullscreen(window->shell_surface,
							WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE,
							0, NULL);
		window->fullscreen = !window->fullscreen;
		break;
	}
}

static void
keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
			  uint32_t serial, uint32_t mods_depressed,
			  uint32_t mods_latched, uint32_t mods_locked,
			  uint32_t group)
{
}

static const struct wl_keyboard_listener keyboard_listener = {
	keyboard_handle_keymap,
	keyboard_handle_enter,
	keyboard_handle_leave,
	keyboard_handle_key,
	keyboard_handle_modifiers,
};

static void
seat_handle_capabilities(void *data, struct wl_seat *seat,
			 enum wl_seat_capability caps)
{
	struct display *d = data;

	if (caps & WL_SEAT_CAPABILITY_KEYBOARD) {
		d->keyboard = wl_seat_get_keyboard(seat);
		wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
	}
}

static const struct wl_seat_listener seat_listener = {
	seat_handle_capabilities,
};

static void
registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
		       const char *interface, uint32_t version)
{
	struct display *d = data;

	if (strcmp(interface, "wl_compositor") == 0) {
		d->compositor =
			wl_registry_bind(d->registry, id,
					 &wl_compositor_interface, 1);
	} else if (strcmp(interface, "wl_shell") == 0) {
		d->shell = wl_registry_bind(d->registry, id, 
					   &wl_shell_interface, 1);
	} else if (strcmp(interface, "wl_drm") == 0) {
		d->drm = wl_registry_bind(d->registry, id,
					  &wl_drm_interface, 2);
		wl_drm_add_listener(d->drm, &drm_listener, d);
	} else if (strcmp(interface, "wl_seat") == 0) {
		d->seat = wl_registry_bind(d->registry,
					   id, &wl_seat_interface, 1);
		wl_seat_add_listener(d->seat, &seat_listener, d);
	}
}

static void
registry_handle_global_remove(void *data, struct wl_registry *registry,
			      uint32_t name)
{
}

static const struct wl_registry_listener registry_listener = {
	registry_handle_global,
	registry_handle_global_remove
};

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);
	display->registry = wl_display_get_registry(display->display);
	wl_registry_add_listener(display->registry,
				 &registry_listener, 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;
}

static void
fullscreen_window(struct window *window)
{
	window->fullscreen = 1;
	wl_shell_surface_set_fullscreen(window->shell_surface,
					WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE,
					0, NULL);
}

static void
usage(const char *p, int exit_code)
{
	if (p)
		printf("%s\n\n", p);

	printf("usage:\n\n"
	       "  --yuyv\tinput format is YUYV\n"
	       "  --yuv\t\tinput format is YUV420\n"
	       "  --nv12\tinput format is NV12\n"
	       "  -f\t\tfullscreen\n"
	       "  -\t\ttake input from stdin\n\n");

	exit(exit_code);
}

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

	for (i = 1; i < argc; i++) {
		if (strcmp(argv[i], "--yuyv") == 0)
			format = WL_DRM_FORMAT_YUYV;
		else if (strcmp(argv[i], "--yuv") == 0)
			format = WL_DRM_FORMAT_YUV420;
		else if (strcmp(argv[i], "--nv12") == 0)
			format = WL_DRM_FORMAT_NV12;
		else if (strcmp(argv[i], "-") == 0)
			source = stdin;
		else if (strcmp(argv[i], "-f") == 0)
			fullscreen = 1;
		else if (strcmp(argv[i], "-h") == 0 ||
			 strcmp(argv[i], "--help") == 0)
			usage(NULL, EXIT_SUCCESS);
		else
			usage("unrecognized option", EXIT_FAILURE);
	}

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

	if (display->has_prime)
		printf("compositor supports prime (dma_buf) buffers\n");

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

	if (fullscreen)
		fullscreen_window(window);

	redraw(window, NULL, 0);
	while (1)
		wl_display_dispatch(display->display);

	destroy_window(window);
	destroy_display(display);

	return 0;
}