summaryrefslogtreecommitdiff
path: root/modules/vidinfo/vidinfo.c
blob: 601d38787a2f8245b2d80f4c864d3e281dbf2eae (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
/**
 * @file vidinfo.c  Video-info filter
 *
 * Copyright (C) 2010 - 2015 Creytiv.com
 */
#include <re.h>
#include <rem.h>
#include <baresip.h>
#include "vidinfo.h"


/**
 * @defgroup vidinfo vidinfo
 *
 * Display video-info overlay on the encode/decode streams
 *
 * Displays info like framerate and packet timing, this is mainly
 * for development and debugging.
 */


struct vidinfo_enc {
	struct vidfilt_enc_st vf;  /* base member (inheritance) */

	struct panel *panel;
};


struct vidinfo_dec {
	struct vidfilt_dec_st vf;  /* base member (inheritance) */

	struct panel *panel;
};


static void encode_destructor(void *arg)
{
	struct vidinfo_enc *st = arg;

	list_unlink(&st->vf.le);
	mem_deref(st->panel);
}


static void decode_destructor(void *arg)
{
	struct vidinfo_dec *st = arg;

	list_unlink(&st->vf.le);
	mem_deref(st->panel);
}


static int encode_update(struct vidfilt_enc_st **stp, void **ctx,
			 const struct vidfilt *vf)
{
	struct vidinfo_enc *st;
	int err = 0;

	if (!stp || !ctx || !vf)
		return EINVAL;

	if (*stp)
		return 0;

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

	if (err)
		mem_deref(st);
	else
		*stp = (struct vidfilt_enc_st *)st;

	return err;
}


static int decode_update(struct vidfilt_dec_st **stp, void **ctx,
			 const struct vidfilt *vf)
{
	struct vidinfo_dec *st;
	int err = 0;

	if (!stp || !ctx || !vf)
		return EINVAL;

	if (*stp)
		return 0;

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

	if (err)
		mem_deref(st);
	else
		*stp = (struct vidfilt_dec_st *)st;

	return err;
}


static int encode(struct vidfilt_enc_st *_st, struct vidframe *frame)
{
	struct vidinfo_enc *st = (struct vidinfo_enc *)_st;
	int err = 0;

	if (!st->panel) {

		unsigned width = frame->size.w;
		unsigned height = MIN(PANEL_HEIGHT, frame->size.h);

		err = panel_alloc(&st->panel, "encode", 0, width, height);
		if (err)
			return err;
	}

	panel_add_frame(st->panel, tmr_jiffies());

	panel_draw(st->panel, frame);

	return 0;
}


static int decode(struct vidfilt_dec_st *_st, struct vidframe *frame)
{
	struct vidinfo_dec *st = (struct vidinfo_dec *)_st;
	int err = 0;

	if (!st->panel) {

		unsigned width = frame->size.w;
		unsigned height = MIN(PANEL_HEIGHT, frame->size.h);
		unsigned yoffs = frame->size.h - PANEL_HEIGHT;

		err = panel_alloc(&st->panel, "decode", yoffs, width, height);
		if (err)
			return err;
	}

	panel_add_frame(st->panel, tmr_jiffies());

	panel_draw(st->panel, frame);

	return 0;
}


static struct vidfilt vidinfo = {
	LE_INIT, "vidinfo", encode_update, encode, decode_update, decode
};


static int module_init(void)
{
	vidfilt_register(&vidinfo);

	return 0;
}


static int module_close(void)
{
	vidfilt_unregister(&vidinfo);

	return 0;
}


EXPORT_SYM const struct mod_export DECL_EXPORTS(vidinfo) = {
	"vidinfo",
	"vidfilt",
	module_init,
	module_close
};