Example #1
0
static void sound_fill_in(struct ubuf *ubuf)
{
    size_t size;
    uint8_t sample_size;
    ubase_assert(ubuf_sound_size(ubuf, &size, &sample_size));
    int octets = size * sample_size;

    const char *channel = NULL;
    while (ubase_check(ubuf_sound_plane_iterate(ubuf, &channel)) &&
           channel != NULL) {
        uint8_t *buffer;
        ubase_assert(ubuf_sound_plane_write_uint8_t(ubuf, channel, 0, -1,
                                                    &buffer));

        for (int x = 0; x < octets; x++)
            buffer[x] = (uint8_t)channel[0] + x;
        ubase_assert(ubuf_sound_plane_unmap(ubuf, channel, 0, -1));
    }
}
Example #2
0
/** @internal @This handles data from sound allocator.
 *
 * @param upipe description structure of the pipe
 * @param uref uref structure
 * @param upump_p reference to pump that generated the buffer
 */
static void upipe_tblk_handle_sound(struct upipe *upipe, struct uref *uref,
                                    struct upump **upump_p)
{
    struct upipe_tblk *upipe_tblk = upipe_tblk_from_upipe(upipe);

    /* Always operate on the first channel plane. */
    const char *channel = NULL;
    if (unlikely(uref->ubuf == NULL ||
                 !ubase_check(ubuf_sound_plane_iterate(uref->ubuf, &channel)) ||
                 channel == NULL)) {
        uref_free(uref);
        upipe_throw_error(upipe, UBASE_ERR_INVALID);
        return;
    }

    /* First try the ubuf_mem_shared method. */
    struct ubuf *ubuf = ubuf_block_mem_alloc_from_sound(upipe_tblk->ubuf_mgr,
                                                        uref->ubuf, channel);
    if (unlikely(ubuf == NULL)) {
        /* We have to memcpy the thing. */
        size_t samples;
        uint8_t sample_size;
        if (unlikely(!ubase_check(uref_sound_size(uref, &samples,
                                                  &sample_size)))) {
            uref_free(uref);
            upipe_throw_error(upipe, UBASE_ERR_INVALID);
            return;
        }

        size_t size = samples * sample_size;
        ubuf = ubuf_block_alloc(upipe_tblk->ubuf_mgr, size);
        if (unlikely(ubuf == NULL)) {
            uref_free(uref);
            upipe_throw_error(upipe, UBASE_ERR_ALLOC);
            return;
        }

        const uint8_t *r;
        if (unlikely(!ubase_check(uref_sound_plane_read_uint8_t(uref, channel,
                                                                0, -1, &r)))) {
            ubuf_free(ubuf);
            uref_free(uref);
            upipe_throw_error(upipe, UBASE_ERR_ALLOC);
            return;
        }

        uint8_t *w;
        int end = -1;
        if (unlikely(!ubase_check(ubuf_block_write(ubuf, 0, &end, &w)))) {
            uref_sound_plane_unmap(uref, channel, 0, -1);
            ubuf_free(ubuf);
            uref_free(uref);
            upipe_throw_error(upipe, UBASE_ERR_ALLOC);
            return;
        }

        memcpy(w, r, size);
        ubuf_block_unmap(ubuf, 0);
        uref_sound_plane_unmap(uref, channel, 0, -1);
    }

    uref_attach_ubuf(uref, ubuf);
    upipe_tblk_output(upipe, uref, upump_p);
}
Example #3
0
int main(int argc, char **argv)
{
    struct umem_mgr *umem_mgr = umem_alloc_mgr_alloc();
    assert(umem_mgr != NULL);

    struct ubuf_mgr *mgr;
    struct ubuf *ubuf1, *ubuf2;
    const char *channel;
    size_t size;
    uint8_t sample_size;
    uint8_t *w;
    const uint8_t *r;

    /* packed s16 stereo */
    mgr = ubuf_sound_mem_mgr_alloc(UBUF_POOL_DEPTH, UBUF_POOL_DEPTH, umem_mgr,
                                   4, 32);
    assert(mgr != NULL);
    ubase_assert(ubuf_sound_mem_mgr_add_plane(mgr, "lr"));

    ubuf1 = ubuf_sound_alloc(mgr, 32);
    assert(ubuf1 != NULL);

    ubase_assert(ubuf_sound_size(ubuf1, &size, &sample_size));
    assert(size == 32);
    assert(sample_size == 4);

    channel = NULL;
    unsigned int nb_planes = 0;
    while (ubase_check(ubuf_sound_plane_iterate(ubuf1, &channel)) &&
           channel != NULL) {
        nb_planes++;
        assert(!strcmp(channel, "lr"));
    }
    assert(nb_planes == 1);

    ubase_assert(ubuf_sound_plane_read_uint8_t(ubuf1, "lr", 0, -1, &r));
    ubase_assert(ubuf_sound_plane_unmap(ubuf1, "lr", 0, -1));

    fill_in(ubuf1);

    ubase_assert(ubuf_sound_plane_read_uint8_t(ubuf1, "lr", 2, 1, &r));
    assert(*r == 'l' + 8);
    ubase_assert(ubuf_sound_plane_unmap(ubuf1, "lr", 2, 1));

    ubuf2 = ubuf_dup(ubuf1);
    assert(ubuf2 != NULL);
    ubase_nassert(ubuf_sound_plane_write_uint8_t(ubuf1, "lr", 0, -1, &w));
    ubuf_free(ubuf2);

    ubase_nassert(ubuf_sound_resize(ubuf1, 0, 33));

    ubase_assert(ubuf_sound_resize(ubuf1, 2, -1));
    ubase_assert(ubuf_sound_plane_read_uint8_t(ubuf1, "lr", 0, -1, &r));
    assert(r[0] == 'l' + 8);
    ubase_assert(ubuf_sound_plane_unmap(ubuf1, "lr", 0, -1));

    ubase_assert(ubuf_sound_resize(ubuf1, 0, 29));

    ubuf_free(ubuf1);

    ubuf_mgr_release(mgr);

    /* planar float 5.1 */
    mgr = ubuf_sound_mem_mgr_alloc(UBUF_POOL_DEPTH, UBUF_POOL_DEPTH, umem_mgr,
                                   sizeof(float), 32);
    assert(mgr != NULL);
    ubase_assert(ubuf_sound_mem_mgr_add_plane(mgr, "l"));
    ubase_assert(ubuf_sound_mem_mgr_add_plane(mgr, "r"));
    ubase_assert(ubuf_sound_mem_mgr_add_plane(mgr, "c"));
    ubase_assert(ubuf_sound_mem_mgr_add_plane(mgr, "L"));
    ubase_assert(ubuf_sound_mem_mgr_add_plane(mgr, "R"));
    ubase_assert(ubuf_sound_mem_mgr_add_plane(mgr, "S"));

    ubuf1 = ubuf_sound_alloc(mgr, 32);
    assert(ubuf1 != NULL);

    ubase_assert(ubuf_sound_size(ubuf1, &size, &sample_size));
    assert(size == 32);
    assert(sample_size == sizeof(float));

    channel = NULL;
    nb_planes = 0;
    while (ubase_check(ubuf_sound_plane_iterate(ubuf1, &channel)) &&
           channel != NULL)
        nb_planes++;
    assert(nb_planes == 6);

    ubase_assert(ubuf_sound_plane_read_uint8_t(ubuf1, "l", 0, -1, &r));
    ubase_assert(ubuf_sound_plane_unmap(ubuf1, "l", 0, -1));

    fill_in(ubuf1);

    ubase_assert(ubuf_sound_plane_read_uint8_t(ubuf1, "l", 2, 1, &r));
    assert(*r == 'l' + 8);
    ubase_assert(ubuf_sound_plane_unmap(ubuf1, "l", 2, 1));
    ubase_assert(ubuf_sound_plane_read_uint8_t(ubuf1, "r", 2, 1, &r));
    assert(*r == 'r' + 8);
    ubase_assert(ubuf_sound_plane_unmap(ubuf1, "r", 2, 1));
    ubase_assert(ubuf_sound_plane_read_uint8_t(ubuf1, "c", 2, 1, &r));
    assert(*r == 'c' + 8);
    ubase_assert(ubuf_sound_plane_unmap(ubuf1, "c", 2, 1));
    ubase_assert(ubuf_sound_plane_read_uint8_t(ubuf1, "L", 2, 1, &r));
    assert(*r == 'L' + 8);
    ubase_assert(ubuf_sound_plane_unmap(ubuf1, "L", 2, 1));
    ubase_assert(ubuf_sound_plane_read_uint8_t(ubuf1, "R", 2, 1, &r));
    assert(*r == 'R' + 8);
    ubase_assert(ubuf_sound_plane_unmap(ubuf1, "R", 2, 1));
    ubase_assert(ubuf_sound_plane_read_uint8_t(ubuf1, "S", 2, 1, &r));
    assert(*r == 'S' + 8);
    ubase_assert(ubuf_sound_plane_unmap(ubuf1, "S", 2, 1));

    ubuf2 = ubuf_dup(ubuf1);
    assert(ubuf2 != NULL);
    ubase_nassert(ubuf_sound_plane_write_uint8_t(ubuf1, "l", 0, -1, &w));
    ubuf_free(ubuf2);

    ubase_nassert(ubuf_sound_resize(ubuf1, 0, 33));

    ubase_assert(ubuf_sound_resize(ubuf1, 2, -1));
    ubase_assert(ubuf_sound_plane_read_uint8_t(ubuf1, "l", 0, -1, &r));
    assert(r[0] == 'l' + 8);
    ubase_assert(ubuf_sound_plane_unmap(ubuf1, "l", 0, -1));
    ubase_assert(ubuf_sound_plane_read_uint8_t(ubuf1, "r", 0, -1, &r));
    assert(r[0] == 'r' + 8);
    ubase_assert(ubuf_sound_plane_unmap(ubuf1, "r", 0, -1));
    ubase_assert(ubuf_sound_plane_read_uint8_t(ubuf1, "c", 0, -1, &r));
    assert(r[0] == 'c' + 8);
    ubase_assert(ubuf_sound_plane_unmap(ubuf1, "c", 0, -1));
    ubase_assert(ubuf_sound_plane_read_uint8_t(ubuf1, "L", 0, -1, &r));
    assert(r[0] == 'L' + 8);
    ubase_assert(ubuf_sound_plane_unmap(ubuf1, "L", 0, -1));
    ubase_assert(ubuf_sound_plane_read_uint8_t(ubuf1, "R", 0, -1, &r));
    assert(r[0] == 'R' + 8);
    ubase_assert(ubuf_sound_plane_unmap(ubuf1, "R", 0, -1));
    ubase_assert(ubuf_sound_plane_read_uint8_t(ubuf1, "S", 0, -1, &r));
    assert(r[0] == 'S' + 8);
    ubase_assert(ubuf_sound_plane_unmap(ubuf1, "S", 0, -1));

    ubase_assert(ubuf_sound_resize(ubuf1, 0, 29));

    ubuf_free(ubuf1);
    ubuf_mgr_release(mgr);

    /* sound -> block transformation */
    mgr = ubuf_sound_mem_mgr_alloc(UBUF_POOL_DEPTH, UBUF_POOL_DEPTH, umem_mgr,
                                   4, 32);
    assert(mgr != NULL);
    ubase_assert(ubuf_sound_mem_mgr_add_plane(mgr, "lr"));

    ubuf1 = ubuf_sound_alloc(mgr, 32);
    assert(ubuf1 != NULL);
    fill_in(ubuf1);

    struct ubuf_mgr *block_mgr = ubuf_block_mem_mgr_alloc(UBUF_POOL_DEPTH,
            UBUF_POOL_DEPTH, umem_mgr, 0, 0, 0, 0);
    struct ubuf *ubuf_block = ubuf_block_mem_alloc_from_sound(block_mgr,
                                                              ubuf1, "lr");
    assert(ubuf_block != NULL);
    ubase_assert(ubuf_block_size(ubuf_block, &size));
    assert(size == 32 * 4);
    int size2 = -1;
    ubase_assert(ubuf_block_read(ubuf_block, 0, &size2, &r));
    assert(size2 == 32 * 4);
    assert(r[0] == 'l');

    ubuf_free(ubuf_block);
    ubuf_mgr_release(block_mgr);
    ubuf_free(ubuf1);

    ubuf_mgr_release(mgr);
    umem_mgr_release(umem_mgr);
    return 0;
}