summaryrefslogtreecommitdiffstats
path: root/bufferq.c
blob: df22ec3b2b2ef8f6dcd266d74e2c21a809162824 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <string.h>

#include "malloc.h"
#include "macro.h"
#include "bufferq.h"

int bufferq_init(bufferq_t *q, unsigned nchannels, size_t sample_size) {
    sa_assert(q);
    sa_assert(sample_size > 0);
    sa_assert(nchannels > 0);

    memset(q, 0, sizeof(*q));
    q->sample_size = sample_size;
    q->nchannels = nchannels;

    if (!(q->items = sa_new0(bufferq_item_t*, nchannels)))
        return SA_ERROR_OOM;

    if (!(q->last = sa_new0(bufferq_item_t*, nchannels))) {
        sa_free(q->items);
        q->items = NULL;
        return SA_ERROR_OOM;
    }
    
    return SA_SUCCESS;
}

void bufferq_done(bufferq_t *q) {
    unsigned u;
    sa_assert(q);

    for (u = 0; u < q->nchannels; u++) {
        bufferq_item_t *i;
        
        while ((i = q->items[u])) {
            SA_LLIST_REMOVE(bufferq_item_t, bufferq, q->items[u], i);
            sa_free(i);
        }
    }

    sa_free(q->items);
    sa_free(q->last);
}

static bufferq_item_t* bufferq_item_new(size_t size) {
    bufferq_item_t *i;
    sa_assert(size > 0);
    
    if (!(i = sa_malloc(ALIGN(sizeof(bufferq_item_t)) + size)))
        return i;

    SA_LLIST_ITEM_INIT(bufferq_item_t, bufferq, i);

    return i;
}

int bufferq_push(bufferq_t *q, unsigned channel, const void *data, size_t nbytes, int64_t offset, sa_seek_t whence) {
    int64_t idx;
    bufferq_item_t *i, *j;
    
    sa_assert(q);

    switch (whence) {
        case SA_SEEK_RELATIVE:
            idx = q->write_index + offset;
            break;
        case SA_SEEK_ABSOLUTE:
            idx = offset;
            break;
        case SA_SEEK_RELATIVE_END:
            idx = q->end_index + offset;
            break;
        case _SA_SEEK_MAX:
            sa_assert_not_reached();
    }

    if (q->read_index > idx) {
        int64_t l = q->read_index - idx;

        if (l > nbytes)
            l = nbytes;
        
        idx += l;
        data += l;
        nbytes -= l;
        
    }

    i = q->items[channel];
    
    while (i && nbytes > 0) {

        if (idx >= i->idx + i->size) {
            /* The new data belongs behind the current node */

            i = i->bufferq_next;

        } else if (idx < i->idx) {
            size_t l;
            /* The new data belongs before the current node (at least partially) */

            l = nbytes;

            if (l > i->idx - idx)
                l = i->idx - idx;
            
            if (!(j = bufferq_item_new(l)))
                return SA_ERROR_OOM;

            memcpy(BUFFERQ_ITEM_DATA(j), data, l);
            j->idx = idx;
            j->size = l;

            SA_LLIST_INSERT_BEFORE(bufferq_item_t, bufferq, q->items[channel], i, j);

            idx += l;
            data += l;
            nbytes -= l;
            
        } else {
            size_t l;

            /* The data belongs right in the current node somewhere */

            l = nbytes;

            if (l > i->idx + i->size - idx)
                l = i->idx + i->size - idx;

            memcpy((uint8_t*)  BUFFERQ_ITEM_DATA(i) + (idx - i->idx), data, l);

            idx += l;
            data += l;
            nbytes -= l;
        }
    }

    if (nbytes > 0) {
        sa_assert(!i);

        if (!(j = bufferq_item_new(nbytes)))
            return SA_ERROR_OOM;

        memcpy(BUFFERQ_ITEM_DATA(j), data, nbytes);
        j->idx = idx;
        j->size = nbytes;

        if (q->last[channel])
            SA_LLIST_INSERT_AFTER(bufferq_item_t, bufferq, q->items[channel], q->last[channel], j);
        else
            SA_LLIST_PREPEND(bufferq_item_t, bufferq, q->items[channel], j);
        
        q->last[channel] = j;
    }

    q->write_index = idx + nbytes;

    if (q->write_index > q->end_index)
        q->end_index = q->write_index;

    return SA_SUCCESS;
}

int bufferq_get(bufferq_t *q, void *i[], size_t *bytes) {
    int first = 1;
    unsigned u;
    sa_assert(q);

    *bytes = 0;
    
    for (u = 0; u < q->nchannels; u++) {

        if (q->items[u]) {

            if (q->items[u]->idx > q->read_index) {
                int64_t l;
                i[u] = NULL;

                l = q->items[u]->idx - q->read_index;

                if (first) {
                    *bytes = l;
                    first = 0;
                } else
                    *bytes = l < *bytes ? l : *bytes;
            } else {
                int64_t l;

                i[u] = (uint8_t*) BUFFERQ_ITEM_DATA(q->items[u]) +  q->read_index - q->items[u]->idx;

                l = q->items[u]->size - (q->read_index - q->items[u]->idx);
                
                if (first) {
                    *bytes = l;
                    first = 0;
                } else
                    *bytes = l < *bytes ? l : *bytes;
            }
            
        } else
            i[u] = NULL;
    }

    return SA_SUCCESS;
}


int bufferq_drop(bufferq_t *q, int64_t bytes) {
    unsigned u;
    
    sa_assert(q);

    q->read_index += bytes;

    for (u = 0; u < q->nchannels; u++) {
        
        bufferq_item_t *i;
        
        i = q->items[u];
        
        while (i && q->read_index >= i->idx + i->size) {
            bufferq_item_t *n = i->bufferq_next;

            SA_LLIST_REMOVE(bufferq_item_t, bufferq, q->items[u], i);

            sa_free(i);
            i = n;
        }

        if (!i)
            q->last[u] = NULL;
    }

    return SA_SUCCESS;
}