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