summaryrefslogtreecommitdiff
path: root/modules/omx/module.c
blob: a5b6fa804d5f1a88a8d20ac266ddda2965f0ed10 (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
/**
 * @file omx/module.c     Raspberry Pi VideoCoreIV OpenMAX interface
 *
 * Copyright (C) 2016 - 2017 Creytiv.com
 * Copyright (C) 2016 - 2017 Jonathan Sieber
 */


#include "omx.h"

#include <stdlib.h>

#include <re.h>
#include <rem.h>
#include <baresip.h>

int omx_vidisp_alloc(struct vidisp_st **vp, const struct vidisp* vd,
	struct vidisp_prm *prm, const char *dev, vidisp_resize_h *resizeh,
	void *arg);
int omx_vidisp_display(struct vidisp_st *st, const char *title,
	const struct vidframe *frame);

struct vidisp_st {
	const struct vidisp *vd;  /* inheritance */
	struct vidsz size;
	struct omx_state* omx;
};

static struct vidisp* vid;

static struct omx_state omx;


static void destructor(void *arg)
{
	struct vidisp_st *st = arg;
	omx_display_disable(st->omx);
}


int omx_vidisp_alloc(struct vidisp_st **vp, const struct vidisp* vd,
		     struct vidisp_prm *prm, const char *dev,
		     vidisp_resize_h *resizeh, void *arg)
{
	struct vidisp_st *st;

	/* Not used by OMX */
	(void) prm;
	(void) dev;
	(void) resizeh;
	(void) arg;

	info("omx: vidisp_alloc\n");

	st = mem_zalloc(sizeof(*st), destructor);
	if (!st)
		return ENOMEM;

	st->vd = vd;
	*vp = st;

	st->omx = &omx;

	return 0;
}


int omx_vidisp_display(struct vidisp_st *st, const char *title,
		   const struct vidframe *frame)
{
	int err = 0;
	void* buf;
	uint32_t len;

	struct vidframe omx_frame;

	(void)title;

	if (frame->fmt != VID_FMT_YUV420P) {
		return EINVAL;
	}

	if (!vidsz_cmp(&st->size, &frame->size)) {
		info("omx: new frame size: w=%d h=%d\n",
			frame->size.w, frame->size.h);
		info("omx: linesize[0]=%d\tlinesize[1]=%d\tlinesize[2]=%d\n",
			frame->linesize[0], frame->linesize[1],
			frame->linesize[2]);
		err = omx_display_enable(st->omx,
			frame->size.w, frame->size.h, frame->size.w);
		if (err) {
			warning("omx_display_enable failed");
			return err;
		}
		st->size = frame->size;
	}

	/* Get Buffer Pointer */
	omx_display_input_buffer(st->omx, &buf, &len);

	vidframe_init_buf(&omx_frame, VID_FMT_YUV420P, &frame->size,
			  buf);

	vidconv(&omx_frame, frame, 0);

	omx_display_flush_buffer(st->omx);
	return 0;
}


static int module_init(void)
{
	if (omx_init(&omx) != 0) {
		warning("Could not initialize OpenMAX");
		return ENODEV;
	}

	return vidisp_register(&vid, baresip_vidispl(), "omx",
		omx_vidisp_alloc, NULL, omx_vidisp_display, NULL);
}


static int module_close(void)
{
	/* HACK: not deinitializing OMX because of a hangup */
	/* omx_deinit(&omx) */
	vid = mem_deref(vid);
	return 0;
}


EXPORT_SYM const struct mod_export DECL_EXPORTS(omx) = {
	"omx",
	"vidisp",
	module_init,
	module_close
};