server: Update status items on file renames.
[paraslash.git] / fecdec_filter.c
1 /* Copyright (C) 2009 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file fecdec_filter.c A filter that fec-decodes an audio stream. */
4
5 #include <regex.h>
6
7 #include "para.h"
8 #include "error.h"
9 #include "list.h"
10 #include "sched.h"
11 #include "buffer_tree.h"
12 #include "filter.h"
13 #include "string.h"
14 #include "portable_io.h"
15 #include "fec.h"
16 #include "fd.h"
17
18 /**
19 * How many FEC groups to store in memory.
20 *
21 * Packet reordering requires to keep more than one FEC group in memory because
22 * slices belonging to the next FEC group may arrive before the current FEC group
23 * is complete.
24 */
25 #define NUM_FEC_GROUPS 3
26
27 /** Data read from the header of a slice. */
28 struct fec_header {
29 /** Total number of slices in this group. */
30 uint8_t slices_per_group;
31 /** Number of slices needed to start decoding. */
32 uint8_t data_slices_per_group;
33 /** Size of the ogg vorbis/wma header (zero for mp3, aac). */
34 uint32_t audio_header_size;
35 /** Number of the FEC group this slice belongs to. */
36 uint32_t group_num;
37 /** Size of the data in this FEC group. */
38 uint32_t group_bytes;
39 /** Number of this slice in the group. */
40 uint8_t slice_num;
41 /** Used data bytes of this slice. */
42 uint16_t slice_bytes;
43 /** Non-zero if this group is the beginning of the stream. */
44 uint8_t bos;
45 /** Non-zero if this stream embeds audio headers into fec groups. */
46 uint8_t header_stream;
47 };
48
49 /**
50 * The status of one partially received FEC group.
51 */
52 struct fecdec_group {
53 /** The header read from the last slice. */
54 struct fec_header h;
55 /** How many slices received so far. */
56 int num_received_slices;
57 /** Bitmap of received slices. */
58 uint8_t received_slices[32];
59 /** The size of the \a idx and the \a data arrays below. */
60 int num_slices;
61 /** Array of indices of the received slices. */
62 int *idx;
63 /** Content of the received slices. */
64 unsigned char **data;
65 };
66
67 /**
68 * Data private to the fecdec filter.
69 */
70 struct private_fecdec_data {
71 /** Used by the fec core code. */
72 struct fec_parms *fec;
73 /** Keeps track of what was received so far. */
74 struct fecdec_group groups[NUM_FEC_GROUPS];
75 /** Whether an audio file header was already received. */
76 int have_header;
77 /** Points to the first received group. */
78 struct fecdec_group *first_complete_group;
79 struct btr_pool *btrp;
80 };
81
82 /** Iterate over all fecdec groups. */
83 #define FOR_EACH_FECDEC_GROUP(g, d) for (g = (d)->groups; \
84 (g) < (d)->groups + NUM_FEC_GROUPS; (g)++)
85
86 static int group_complete(struct fecdec_group *fg)
87 {
88 return fg->num_received_slices >= fg->h.data_slices_per_group;
89 }
90
91 static int group_empty(struct fecdec_group *fg)
92 {
93 return fg->num_received_slices == 0;
94 }
95
96 static void clear_group(struct fecdec_group *fg)
97 {
98 int i;
99
100 for (i = 0; i < fg->num_slices; i++)
101 free(fg->data[i]);
102 free(fg->data);
103 free(fg->idx);
104 memset(fg, 0, sizeof(*fg));
105 }
106
107 static int find_group(struct fec_header *h,
108 struct private_fecdec_data *pfd, struct fecdec_group **result)
109 {
110 struct fecdec_group *fg;
111
112 FOR_EACH_FECDEC_GROUP(fg, pfd) {
113 if (fg->h.group_num != h->group_num)
114 continue;
115 if (fg->num_received_slices == 0)
116 goto success;
117 if (fg->h.slices_per_group != h->slices_per_group)
118 return -E_BAD_FEC_HEADER;
119 if (fg->h.data_slices_per_group != h->data_slices_per_group)
120 return -E_BAD_FEC_HEADER;
121 if (fg->h.group_bytes != h->group_bytes)
122 return -E_BAD_FEC_HEADER;
123 success:
124 *result = fg;
125 return 1;
126 }
127 return 0;
128 }
129
130 static struct fecdec_group *find_unused_group(struct private_fecdec_data *pfd)
131 {
132 struct fecdec_group *fg;
133
134 FOR_EACH_FECDEC_GROUP(fg, pfd) {
135 if (fg->num_received_slices == 0)
136 return fg;
137 }
138 return NULL;
139 }
140
141 static struct fecdec_group *try_to_free_group(struct private_fecdec_data *pfd)
142 {
143 struct fecdec_group *fg;
144
145 FOR_EACH_FECDEC_GROUP(fg, pfd) {
146 if (!group_complete(fg))
147 continue;
148 /*
149 * Don't clear the first complete group if it has not yet been
150 * decoded.
151 */
152 if (fg == pfd->first_complete_group)
153 continue;
154 clear_group(fg);
155 return fg;
156 }
157 return NULL;
158 }
159
160 static struct fecdec_group *free_oldest_group(struct private_fecdec_data *pfd)
161 {
162 struct fecdec_group *fg, *oldest = NULL;
163
164 FOR_EACH_FECDEC_GROUP(fg, pfd) {
165 if (!oldest || oldest->h.group_num > fg->h.group_num)
166 oldest = fg;
167 }
168 if (!group_complete(oldest) && !group_empty(oldest))
169 PARA_WARNING_LOG("Clearing incomplete group %u "
170 "(contains %d slices)\n", oldest->h.group_num,
171 oldest->num_received_slices);
172 if (oldest == pfd->first_complete_group)
173 pfd->first_complete_group = NULL;
174 clear_group(oldest);
175 return oldest;
176 }
177
178 /* returns 1 if the group was found, 0 if not, negative on errors */
179 static int get_group(struct fec_header *h, struct private_fecdec_data *pfd,
180 struct fecdec_group **result)
181 {
182 struct fecdec_group *fg;
183 int ret = find_group(h, pfd, &fg);
184
185 if (ret < 0)
186 return ret;
187 if (ret > 0) /* found group */
188 goto success;
189 /* group not found */
190 fg = find_unused_group(pfd);
191 if (fg)
192 goto success;
193 fg = try_to_free_group(pfd);
194 if (fg)
195 goto success;
196 fg = free_oldest_group(pfd);
197 ret = 0;
198 success:
199 fg->h = *h;
200 *result = fg;
201 return ret;
202 }
203
204 static bool test_and_set_slice_bit(struct fecdec_group *fg, uint8_t slice_num)
205 {
206 uint8_t *p = fg->received_slices + slice_num / 8, old = *p;
207
208 *p |= 1 << (slice_num % 8);
209 return old == *p;
210 }
211
212 /*
213 * returns 1 if slice was added, zero otherwise (because the group was already
214 * complete or a slice has been received twice).
215 */
216 static int add_slice(char *buf, struct fecdec_group *fg)
217 {
218 int r;
219 uint8_t slice_num = fg->h.slice_num;
220
221 if (group_complete(fg)) {
222 PARA_DEBUG_LOG("group %u complete, ignoring slice %d\n",
223 fg->h.group_num, slice_num);
224 return 0;
225 }
226 if (fg->num_slices == 0) {
227 fg->num_slices = fg->h.slices_per_group;
228 fg->idx = para_malloc(fg->num_slices * sizeof(int));
229 fg->data = para_calloc(fg->num_slices * sizeof(unsigned char *));
230 }
231 r = fg->num_received_slices;
232 /* Check if we already have this slice. */
233 if (test_and_set_slice_bit(fg, slice_num)) {
234 PARA_INFO_LOG("ignoring duplicate slice %u:%d\n", fg->h.group_num,
235 slice_num);
236 return 0;
237 }
238 fg->idx[r] = slice_num;
239 fg->data[r] = para_malloc(fg->h.slice_bytes);
240 memcpy(fg->data[r], buf, fg->h.slice_bytes);
241 fg->num_received_slices++;
242 return 1;
243 }
244
245 /**
246 * The different states of a complete FEC group.
247 *
248 * Even if a FEC group has been received successfully, it probably can not be
249 * used right away because some streams (ogg, wma) need to receive an audio
250 * file header before decoding can start.
251 */
252 enum fec_group_usability {
253 /** Drop the group (because we did not receive the header yet). */
254 FEC_GROUP_UNUSABLE,
255 /** Use all data in the group. */
256 FEC_GROUP_USABLE,
257 /** Use the group, but drop its audio file header. */
258 FEC_GROUP_USABLE_SKIP_HEADER,
259 /** Use the group, including its header. */
260 FEC_GROUP_USABLE_WITH_HEADER
261 };
262
263 static enum fec_group_usability group_is_usable(struct fecdec_group *fg,
264 struct private_fecdec_data *pfd)
265 {
266 struct fec_header *h = &fg->h;
267
268 if (!h->header_stream)
269 return FEC_GROUP_USABLE;
270 if (pfd->have_header) {
271 if (h->audio_header_size)
272 return FEC_GROUP_USABLE_SKIP_HEADER;
273 return FEC_GROUP_USABLE;
274 }
275 if (fg->h.bos)
276 return FEC_GROUP_USABLE;
277 if (fg->h.audio_header_size)
278 return FEC_GROUP_USABLE_WITH_HEADER;
279 return FEC_GROUP_UNUSABLE;
280 }
281
282 static int decode_group(struct fecdec_group *fg, struct filter_node *fn)
283 {
284 int i, ret, sb = fg->h.slice_bytes;
285 size_t written, need;
286 struct private_fecdec_data *pfd = fn->private_data;
287 enum fec_group_usability u = group_is_usable(fg, pfd);
288 char *buf = NULL;
289
290 if (u == FEC_GROUP_UNUSABLE) {
291 PARA_INFO_LOG("dropping unusable group %u\n", fg->h.group_num);
292 return 0;
293 }
294 PARA_DEBUG_LOG("decoding group %u (%d slices)\n", fg->h.group_num,
295 fg->h.data_slices_per_group);
296 ret = fec_decode(pfd->fec, fg->data, fg->idx, sb);
297 if (ret < 0)
298 return ret;
299 pfd->have_header = 1;
300 i = 0;
301 if (u == FEC_GROUP_USABLE_SKIP_HEADER) {
302 i = DIV_ROUND_UP(fg->h.audio_header_size, fg->h.slice_bytes);
303 PARA_DEBUG_LOG("skipping %d header slices\n", i);
304 }
305 PARA_DEBUG_LOG("writing group %u (%u/%d decoded data bytes)\n",
306 fg->h.group_num, fg->h.group_bytes,
307 fg->h.data_slices_per_group * sb);
308 need = (fg->h.data_slices_per_group - i) * sb;
309 if (need > btr_pool_unused(pfd->btrp))
310 return -E_FECDEC_OVERRUN;
311 btr_pool_get_buffer(pfd->btrp, &buf);
312 if (u == FEC_GROUP_USABLE_WITH_HEADER) {
313 PARA_INFO_LOG("writing audio file header\n");
314 written = 0;
315 for (i = 0; i < fg->h.data_slices_per_group; i++) {
316 size_t n = sb;
317 if (written >= fg->h.audio_header_size)
318 break;
319 if (sb + written > fg->h.audio_header_size)
320 n = fg->h.audio_header_size - written;
321 btr_copy(fg->data[i], n, pfd->btrp, fn->btrn);
322 written += n;
323 }
324 }
325 written = 0;
326 for (; i < fg->h.data_slices_per_group; i++) {
327 size_t n = sb;
328 if (n + written > fg->h.group_bytes)
329 n = fg->h.group_bytes - written;
330 btr_copy(fg->data[i], n, pfd->btrp, fn->btrn);
331 written += n;
332 }
333 return 0;
334 }
335
336 /**
337 * Read a fec header from a buffer.
338 *
339 * \param buf The buffer to write to.
340 * \param h The fec header to write.
341 */
342 static int read_fec_header(char *buf, size_t len, struct fec_header *h)
343 {
344 uint32_t magic;
345
346 if (len < FEC_HEADER_SIZE)
347 return 0;
348 magic = read_u32(buf);
349 if (magic != FEC_MAGIC)
350 return -E_BAD_FEC_HEADER;
351 h->slices_per_group = read_u8(buf + 4);
352 h->data_slices_per_group = read_u8(buf + 5);
353 h->audio_header_size = read_u32(buf + 6);
354
355 h->group_num = read_u32(buf + 10);
356 h->group_bytes = read_u32(buf + 14);
357
358 h->slice_num = read_u8(buf + 18);
359 h->slice_bytes = read_u16(buf + 20);
360 h->bos = read_u8(buf + 22);
361 h->header_stream = read_u8(buf + 23);
362 if (!memcmp(buf, FEC_EOF_PACKET, FEC_EOF_PACKET_LEN))
363 return -E_FECDEC_EOF;
364 // PARA_DEBUG_LOG("group %u, slize %u, slices per group: %u\n",
365 // h->group_num, h->slice_num, h->slices_per_group);
366 return 1;
367 }
368
369 /* returns 1 if we used the buffer, 0 if we didn't, negative on errors */
370 static int dispatch_slice(char *buf, size_t len, struct fec_header *h,
371 struct filter_node *fn)
372 {
373 struct fecdec_group *fg;
374 int ret, k, n;
375 struct private_fecdec_data *pfd = fn->private_data;
376
377 if (h->slice_bytes > len) { /* can not use the thing, try to read more */
378 fn->min_iqs = h->slice_bytes + FEC_HEADER_SIZE;
379 return 0;
380 }
381 ret = get_group(h, pfd, &fg);
382 if (ret < 0)
383 return ret;
384 if (!add_slice(buf, fg)) /* group already complete */
385 return 1;
386 if (!group_complete(fg))
387 return 1;
388 /* this slice completed the group */
389 if (pfd->fec)
390 goto decode;
391 /* it's either the first or the second complete group */
392 if (!pfd->first_complete_group) { /* it's the first group */
393 enum fec_group_usability u = group_is_usable(fg, pfd);
394 assert(u != FEC_GROUP_USABLE_SKIP_HEADER);
395 if (u == FEC_GROUP_UNUSABLE) /* forget it */
396 return 1;
397 pfd->first_complete_group = fg; /* remember it */
398 return 1;
399 }
400 /* we have two complete groups, let's go */
401 k = h->data_slices_per_group;
402 n = h->slices_per_group;
403 PARA_NOTICE_LOG("init fec (%d, %d)\n", k, n);
404 ret = fec_new(k, n, &pfd->fec);
405 if (ret < 0)
406 return ret;
407 pfd->btrp = btr_pool_new("fecdec", 128 * 1024);
408 /* decode and clear the first group */
409 ret = decode_group(pfd->first_complete_group, fn);
410 if (ret < 0)
411 return ret;
412 clear_group(pfd->first_complete_group);
413 pfd->first_complete_group = NULL;
414 decode:
415 ret = decode_group(fg, fn);
416 if (ret < 0)
417 return ret;
418 return 1;
419 }
420
421 static void fecdec_close(struct filter_node *fn)
422 {
423 struct private_fecdec_data *pfd = fn->private_data;
424 struct fecdec_group *fg;
425
426 FOR_EACH_FECDEC_GROUP(fg, pfd)
427 clear_group(fg);
428 fec_free(pfd->fec);
429 btr_pool_free(pfd->btrp);
430 free(fn->private_data);
431 fn->private_data = NULL;
432 }
433
434 static int fecdec_post_select(__a_unused struct sched *s, void *context)
435 {
436 struct filter_node *fn = context;
437 struct btr_node *btrn = fn->btrn;
438 int ret;
439 struct fec_header h;
440 char *buf;
441 size_t len;
442
443 next_buffer:
444 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
445 if (ret <= 0)
446 goto out;
447 btr_merge(btrn, fn->min_iqs);
448 len = btr_next_buffer(btrn, &buf);
449 ret = read_fec_header(buf, len, &h);
450 if (ret <= 0)
451 goto out;
452 ret = -E_BAD_SLICE_SIZE;
453 if (!h.slice_bytes)
454 goto out;
455 ret = -E_BAD_SLICE_NUM;
456 if (h.slice_num > h.slices_per_group)
457 goto out;
458 ret = dispatch_slice(buf + FEC_HEADER_SIZE, len - FEC_HEADER_SIZE,
459 &h, fn);
460 //PARA_INFO_LOG("ret: %d, len: %d, slice_bytes: %d\n", ret, len, h.slice_bytes);
461 if (ret <= 0)
462 goto out;
463 btr_consume(btrn, FEC_HEADER_SIZE + h.slice_bytes);
464 goto next_buffer;
465 out:
466 if (ret < 0)
467 btr_remove_node(&fn->btrn);
468 return ret;
469 }
470
471 static void fecdec_open(struct filter_node *fn)
472 {
473 struct private_fecdec_data *pfd;
474 pfd = para_calloc(sizeof(*pfd));
475 fn->private_data = pfd;
476 fn->min_iqs = FEC_HEADER_SIZE;
477 }
478
479 const struct filter lsg_filter_cmd_com_fecdec_user_data = {
480 .open = fecdec_open,
481 .pre_select = generic_filter_pre_select,
482 .post_select = fecdec_post_select,
483 .close = fecdec_close,
484 };