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