Skip to content

Commit 3e8b7d8

Browse files
committed
tests: add upipe_pad test
1 parent 72655e3 commit 3e8b7d8

2 files changed

Lines changed: 322 additions & 0 deletions

File tree

tests/Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ check_PROGRAMS = \
9797
upipe_match_attr_test \
9898
upipe_blit_test \
9999
upipe_crop_test \
100+
upipe_pad_test \
100101
upipe_audio_split_test \
101102
upipe_videocont_test \
102103
upipe_audiocont_test \
@@ -159,6 +160,7 @@ TESTS = \
159160
upipe_match_attr_test \
160161
upipe_blit_test \
161162
upipe_crop_test \
163+
upipe_pad_test \
162164
upipe_audio_split_test \
163165
upipe_videocont_test \
164166
upipe_audiocont_test \
@@ -451,6 +453,7 @@ upipe_chunk_stream_test_LDADD = $(LDADD) $(top_builddir)/lib/upipe-modules/libup
451453
upipe_htons_test_LDADD = $(LDADD) $(top_builddir)/lib/upipe-modules/libupipe_modules.la
452454
upipe_blit_test_LDADD = $(LDADD) $(top_builddir)/lib/upipe-modules/libupipe_modules.la
453455
upipe_crop_test_LDADD = $(LDADD) $(top_builddir)/lib/upipe-modules/libupipe_modules.la
456+
upipe_pad_test_LDADD = $(LDADD) $(top_builddir)/lib/upipe-modules/libupipe_modules.la
454457
upipe_qt_html_test_LDADD = $(LDADD) $(top_builddir)/lib/upipe-qt/libupipe_qt.la -L/usr/lib/x86_64-linux-gnu -lQtCore -lQtGui -lQtWebKit -lpthread $(top_builddir)/lib/upipe-modules/libupipe_modules.la $(top_builddir)/lib/upump-ev/libupump_ev.la $(top_builddir)/lib/upipe-pthread/libupipe_pthread.la -lev $(top_builddir)/lib/upump-ev/libupump_ev.la
455458
upipe_audio_split_test_LDADD = $(LDADD) $(top_builddir)/lib/upipe-modules/libupipe_modules.la
456459
upipe_videocont_test_LDADD = $(LDADD) $(top_builddir)/lib/upipe-modules/libupipe_modules.la

tests/upipe_pad_test.c

Lines changed: 319 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,319 @@
1+
/*
2+
* Copyright (C) 2020 Open Broadcast Systems Ltd.
3+
*
4+
* Authors: James Darnley
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining
7+
* a copy of this software and associated documentation files (the
8+
* "Software"), to deal in the Software without restriction, including
9+
* without limitation the rights to use, copy, modify, merge, publish,
10+
* distribute, sublicense, and/or sell copies of the Software, and to
11+
* permit persons to whom the Software is furnished to do so, subject
12+
* to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be
15+
* included in all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21+
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
#undef NDEBUG
27+
28+
#include <upipe/uprobe.h>
29+
#include <upipe/uprobe_stdio.h>
30+
#include <upipe/uprobe_prefix.h>
31+
#include <upipe/uprobe_ubuf_mem.h>
32+
#include <upipe/umem.h>
33+
#include <upipe/umem_alloc.h>
34+
#include <upipe/udict.h>
35+
#include <upipe/udict_inline.h>
36+
#include <upipe/uref.h>
37+
#include <upipe/uref_std.h>
38+
#include <upipe/uref_dump.h>
39+
#include <upipe/upipe.h>
40+
#include <upipe/uref_pic.h>
41+
#include <upipe/uref_pic_flow.h>
42+
#include <upipe/ubuf_pic_mem.h>
43+
#include <upipe-modules/upipe_pad.h>
44+
45+
#include <stdio.h>
46+
#include <string.h>
47+
#include <stdlib.h>
48+
#include <stdbool.h>
49+
#include <assert.h>
50+
51+
#include <stdio.h>
52+
#include <string.h>
53+
#include <stdlib.h>
54+
#include <stdbool.h>
55+
#include <assert.h>
56+
57+
#define UDICT_POOL_DEPTH 0
58+
#define UREF_POOL_DEPTH 0
59+
#define UBUF_POOL_DEPTH 0
60+
#define UPROBE_LOG_LEVEL UPROBE_LOG_VERBOSE
61+
62+
#define PAD_L 2
63+
#define PAD_R 4
64+
#define PAD_T 5
65+
#define PAD_B 1
66+
67+
#define INPUT_W 32
68+
#define INPUT_H 16
69+
70+
#define TOTAL_W INPUT_W + PAD_L + PAD_R
71+
#define TOTAL_H INPUT_H + PAD_T + PAD_B
72+
73+
/** definition of our uprobe */
74+
static int catch(struct uprobe *uprobe, struct upipe *upipe,
75+
int event, va_list args)
76+
{
77+
switch (event) {
78+
case UPROBE_READY:
79+
case UPROBE_DEAD:
80+
case UPROBE_NEW_FLOW_DEF:
81+
break;
82+
default:
83+
assert(0);
84+
break;
85+
}
86+
return UBASE_ERR_NONE;
87+
}
88+
89+
/* fill picture with some reference */
90+
static void fill_in(struct uref *uref, const char *chroma, uint8_t val)
91+
{
92+
uint8_t hsub, vsub, macropixel_size;
93+
size_t hsize, vsize, stride;
94+
uint8_t *buffer;
95+
ubase_assert(uref_pic_plane_write(uref, chroma, 0, 0, -1, -1, &buffer));
96+
ubase_assert(uref_pic_plane_size(uref, chroma, &stride, &hsub, &vsub, &macropixel_size));
97+
assert(buffer != NULL);
98+
ubase_assert(uref_pic_size(uref, &hsize, &vsize, NULL));
99+
hsize /= hsub;
100+
hsize *= macropixel_size;
101+
vsize /= vsub;
102+
for (int y = 0; y < vsize; y++) {
103+
for (int x = 0; x < hsize; x++)
104+
buffer[x] = val;
105+
buffer += stride;
106+
}
107+
uref_pic_plane_unmap(uref, chroma, 0, 0, -1, -1);
108+
}
109+
110+
/* check a chroma */
111+
static void check_chroma(struct uref *uref, const char *chroma, uint8_t val)
112+
{
113+
uint8_t hsub, vsub, macropixel_size;
114+
size_t hsize, vsize, stride;
115+
const uint8_t *buffer;
116+
int x, y;
117+
118+
ubase_assert(uref_pic_plane_read(uref, chroma, 0, 0, -1, -1, &buffer));
119+
ubase_assert(uref_pic_plane_size(uref, chroma, &stride, &hsub, &vsub, &macropixel_size));
120+
ubase_assert(uref_pic_size(uref, &hsize, &vsize, NULL));
121+
hsize /= hsub;
122+
hsize *= macropixel_size;
123+
vsize /= vsub;
124+
125+
for (y = 0; y < vsize; y++) {
126+
for (x = 0; x < hsize; x++) {
127+
assert(buffer[x] == val);
128+
}
129+
buffer += stride;
130+
}
131+
132+
uref_pic_plane_unmap(uref, chroma, 0, 0, -1, -1);
133+
}
134+
135+
/** helper phony pipe */
136+
static struct upipe *test_alloc(struct upipe_mgr *mgr, struct uprobe *uprobe,
137+
uint32_t signature, va_list args)
138+
{
139+
struct upipe *upipe = malloc(sizeof(struct upipe));
140+
upipe_init(upipe, mgr, uprobe);
141+
upipe_throw_ready(upipe);
142+
return upipe;
143+
}
144+
145+
/** helper phony pipe */
146+
static void test_input(struct upipe *upipe, struct uref *uref,
147+
struct upump **upump_p)
148+
{
149+
assert(uref != NULL);
150+
upipe_dbg(upipe, "===> received input uref");
151+
uref_dump(uref, upipe->uprobe);
152+
153+
/* Check left. */
154+
ubase_assert(uref_pic_resize(uref, 0, 0, PAD_L, TOTAL_H));
155+
check_chroma(uref, "y8", 0);
156+
check_chroma(uref, "u8", 0x80);
157+
check_chroma(uref, "v8", 0x80);
158+
ubase_assert(uref_pic_resize(uref, 0, 0, TOTAL_W, TOTAL_H));
159+
160+
/* Check top. */
161+
ubase_assert(uref_pic_resize(uref, 0, 0, TOTAL_W, PAD_T));
162+
check_chroma(uref, "y8", 0);
163+
check_chroma(uref, "u8", 0x80);
164+
check_chroma(uref, "v8", 0x80);
165+
ubase_assert(uref_pic_resize(uref, 0, 0, TOTAL_W, TOTAL_H));
166+
167+
/* Check middle. */
168+
ubase_assert(uref_pic_resize(uref, PAD_L, PAD_T, INPUT_W, INPUT_H));
169+
check_chroma(uref, "y8", 1);
170+
check_chroma(uref, "u8", 128);
171+
check_chroma(uref, "v8", 255);
172+
ubase_assert(uref_pic_resize(uref, -PAD_L, -PAD_T, TOTAL_W, TOTAL_H));
173+
174+
/* Check right. */
175+
ubase_assert(uref_pic_resize(uref, INPUT_W + PAD_L, 0, PAD_R, TOTAL_H));
176+
check_chroma(uref, "y8", 0);
177+
check_chroma(uref, "u8", 0x80);
178+
check_chroma(uref, "v8", 0x80);
179+
ubase_assert(uref_pic_resize(uref, -(INPUT_W + PAD_L), 0, TOTAL_W, TOTAL_H));
180+
181+
/* Check bottom. */
182+
ubase_assert(uref_pic_resize(uref, 0, INPUT_H + PAD_T, TOTAL_W, PAD_B));
183+
check_chroma(uref, "y8", 0);
184+
check_chroma(uref, "u8", 0x80);
185+
check_chroma(uref, "v8", 0x80);
186+
ubase_assert(uref_pic_resize(uref, 0, -(INPUT_H + PAD_T), TOTAL_W, TOTAL_H));
187+
188+
uref_free(uref);
189+
}
190+
191+
/** helper phony pipe */
192+
static int test_control(struct upipe *upipe, int command, va_list args)
193+
{
194+
switch (command) {
195+
case UPIPE_SET_FLOW_DEF: {
196+
struct uref *flow_def = va_arg(args, struct uref *);
197+
ubase_assert(uref_flow_match_def(flow_def, "pic."));
198+
ubase_assert(uref_pic_flow_match_hsize(flow_def, TOTAL_W, TOTAL_W));
199+
ubase_assert(uref_pic_flow_match_vsize(flow_def, TOTAL_H, TOTAL_H));
200+
ubase_assert(uref_pic_flow_check_chroma(flow_def, 1, 1, 1, "y8"));
201+
ubase_assert(uref_pic_flow_check_chroma(flow_def, 2, 1, 1, "u8"));
202+
ubase_assert(uref_pic_flow_check_chroma(flow_def, 2, 1, 1, "v8"));
203+
return UBASE_ERR_NONE;
204+
}
205+
case UPIPE_REGISTER_REQUEST: {
206+
struct urequest *urequest = va_arg(args, struct urequest *);
207+
return upipe_throw_provide_request(upipe, urequest);
208+
}
209+
case UPIPE_UNREGISTER_REQUEST:
210+
return UBASE_ERR_NONE;
211+
default:
212+
assert(0);
213+
return UBASE_ERR_UNHANDLED;
214+
}
215+
}
216+
217+
/** helper phony pipe */
218+
static void test_free(struct upipe *upipe)
219+
{
220+
upipe_dbg(upipe, "releasing pipe");
221+
upipe_throw_dead(upipe);
222+
upipe_clean(upipe);
223+
free(upipe);
224+
}
225+
226+
/** helper phony pipe */
227+
static struct upipe_mgr test_mgr = {
228+
.refcount = NULL,
229+
.signature = 0,
230+
.upipe_alloc = test_alloc,
231+
.upipe_input = test_input,
232+
.upipe_control = test_control
233+
};
234+
235+
int main(int argc, char **argv)
236+
{
237+
printf("Compiled %s %s - %s\n", __DATE__, __TIME__, __FILE__);
238+
239+
/* uref and mem management */
240+
struct umem_mgr *umem_mgr = umem_alloc_mgr_alloc();
241+
assert(umem_mgr != NULL);
242+
struct udict_mgr *udict_mgr =
243+
udict_inline_mgr_alloc(UDICT_POOL_DEPTH, umem_mgr, -1, -1);
244+
assert(udict_mgr != NULL);
245+
struct uref_mgr *uref_mgr =
246+
uref_std_mgr_alloc(UREF_POOL_DEPTH, udict_mgr, 0);
247+
assert(uref_mgr != NULL);
248+
249+
struct ubuf_mgr *pic_mgr = ubuf_pic_mem_mgr_alloc_fourcc(UBUF_POOL_DEPTH,
250+
UBUF_POOL_DEPTH, umem_mgr, "YV16", 0, 0, 0, 0, 0, 0);
251+
assert(pic_mgr != NULL);
252+
253+
/* uprobe stuff */
254+
struct uprobe uprobe;
255+
uprobe_init(&uprobe, catch, NULL);
256+
struct uprobe *logger = uprobe_stdio_alloc(&uprobe, stdout,
257+
UPROBE_LOG_LEVEL);
258+
assert(logger != NULL);
259+
logger = uprobe_ubuf_mem_alloc(logger, umem_mgr, 0, 0);
260+
assert(logger != NULL);
261+
262+
/* build pad pipe */
263+
struct upipe_mgr *upipe_pad_mgr = upipe_pad_mgr_alloc();
264+
assert(upipe_pad_mgr);
265+
266+
struct uref *flow_def = uref_alloc(uref_mgr);
267+
assert(flow_def);
268+
ubase_assert(uref_flow_set_def(flow_def, "pic."));
269+
ubase_assert(uref_pic_set_lpadding(flow_def, PAD_L));
270+
ubase_assert(uref_pic_set_rpadding(flow_def, PAD_R));
271+
ubase_assert(uref_pic_set_tpadding(flow_def, PAD_T));
272+
ubase_assert(uref_pic_set_bpadding(flow_def, PAD_B));
273+
274+
struct upipe *pad = upipe_flow_alloc(upipe_pad_mgr,
275+
uprobe_pfx_alloc(uprobe_use(logger), UPROBE_LOG_LEVEL, "pad"),
276+
flow_def);
277+
assert(pad != NULL);
278+
uref_free(flow_def);
279+
280+
/* build phony pipe */
281+
struct upipe *test = upipe_void_alloc(&test_mgr,
282+
uprobe_pfx_alloc(uprobe_use(logger), UPROBE_LOG_LEVEL, "test"));
283+
assert(test != NULL);
284+
ubase_assert(upipe_set_output(pad, test));
285+
286+
flow_def = uref_pic_flow_alloc_def(uref_mgr, 1);
287+
assert(flow_def != NULL);
288+
ubase_assert(uref_pic_flow_add_plane(flow_def, 1, 1, 1, "y8"));
289+
ubase_assert(uref_pic_flow_add_plane(flow_def, 2, 1, 1, "u8"));
290+
ubase_assert(uref_pic_flow_add_plane(flow_def, 2, 1, 1, "v8"));
291+
ubase_assert(uref_pic_flow_set_hsize(flow_def, INPUT_W));
292+
ubase_assert(uref_pic_flow_set_vsize(flow_def, INPUT_H));
293+
ubase_assert(upipe_set_flow_def(pad, flow_def));
294+
uref_free(flow_def);
295+
296+
struct uref *uref;
297+
uref = uref_pic_alloc(uref_mgr, pic_mgr, INPUT_W, INPUT_H);
298+
assert(uref != NULL);
299+
uref_pic_set_progressive(uref);
300+
fill_in(uref, "y8", 1);
301+
fill_in(uref, "u8", 128);
302+
fill_in(uref, "v8", 255);
303+
upipe_input(pad, uref, NULL);
304+
305+
/* release pad pipe and subpipes */
306+
upipe_release(pad);
307+
test_free(test);
308+
309+
/* release managers */
310+
upipe_mgr_release(upipe_pad_mgr); // no-op
311+
ubuf_mgr_release(pic_mgr);
312+
uref_mgr_release(uref_mgr);
313+
umem_mgr_release(umem_mgr);
314+
udict_mgr_release(udict_mgr);
315+
uprobe_release(logger);
316+
uprobe_clean(&uprobe);
317+
318+
return 0;
319+
}

0 commit comments

Comments
 (0)