cuticle/hi/bs.h

102 lines
2.1 KiB
C

#ifndef _CUTI_BS_H
#define _CUTI_BS_H
#ifdef __cplusplus
extern "C" {
#endif
#include<stdlib.h>
#include<string.h>
#define CUTIHI_BS_FLAG_KEY 1
#define CUTIHI_BS_SETUP_PACKET 2
typedef struct {
uint64_t timestamp;
uint32_t sz;
uint8_t flags;
void *ptr;
} CHiBSFrame;
typedef struct {
size_t count;
CHiBSFrame data[];
} CHiBSFrames;
static inline CHiBSFrames *CHi_BS_Combine(CHiBSFrames *A, const CHiBSFrames *B) {
CHiBSFrames *ret = NULL;
size_t aoffset = 0;
if(A) {
aoffset = A->count;
ret = (CHiBSFrames*) calloc(1, sizeof(*ret) + sizeof(CHiBSFrame) * (A->count + B->count));
ret->count = A->count + B->count;
memcpy(ret->data, A->data, sizeof(*A->data) * A->count);
free(A);
} else {
ret = (CHiBSFrames*) calloc(1, sizeof(*ret) + sizeof(CHiBSFrame) * B->count);
ret->count = B->count;
}
for(size_t i = 0; i < B->count; i++) {
void *copy = malloc(B->data[i].sz);
memcpy(copy, B->data[i].ptr, B->data[i].sz);
ret->data[aoffset + i] = (CHiBSFrame) {
.timestamp = B->data[i].timestamp,
.sz = B->data[i].sz,
.flags = B->data[i].flags,
.ptr = copy
};
}
return ret;
}
static inline CHiBSFrames *CHi_BS_Empty() {
return (CHiBSFrames*) calloc(1, sizeof(CHiBSFrames));
}
static inline void CHi_BS_Pop(CHiBSFrames *bsfs, size_t num) {
for(size_t i = 0; i < num; i++) {
free(bsfs->data[i].ptr);
}
memmove(&bsfs->data[0], &bsfs->data[num], sizeof(bsfs->data[0]) * (bsfs->count - num));
bsfs->count -= num;
}
static inline void CHi_BS_Clear(CHiBSFrames *bsfs) {
CHi_BS_Pop(bsfs, bsfs->count);
}
static inline void CHi_BS_Free(CHiBSFrames *bsfs) {
if(bsfs) {
CHi_BS_Clear(bsfs);
free(bsfs);
}
}
static inline CHiBSFrames *CHi_BS_Grow(CHiBSFrames *bsfs, size_t num) {
size_t oldsz = sizeof(*bsfs) + sizeof(CHiBSFrame) * bsfs->count;
size_t newsz = sizeof(*bsfs) + sizeof(CHiBSFrame) * (bsfs->count + num);
CHiBSFrames *ret = (CHiBSFrames*) realloc(bsfs, newsz);
memset((uint8_t*) ret + oldsz, 0, newsz - oldsz);
return ret;
}
typedef struct {
uint16_t width;
uint16_t height;
} CHiVPxBSSettings;
#ifdef __cplusplus
}
#endif
#endif