2 * Copyright (C) 2009 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file fecdec_filter.c A filter that fec-decodes an audio stream. */
15 #include "buffer_tree.h"
18 #include "portable_io.h"
23 * How many FEC groups to store in memory.
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
29 #define NUM_FEC_GROUPS 3
31 /** Data read from the header of a slice. */
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. */
41 /** Size of the data in this FEC group. */
43 /** Number of this slice in the group. */
45 /** Used data bytes of this slice. */
47 /** Non-zero if this group is the beginning of the stream. */
49 /** Non-zero if this stream embeds audio headers into fec groups. */
50 uint8_t header_stream;
54 * The status of one partially received FEC group.
57 /** The header read from the last slice. */
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. */
65 /** Array of indices of the received slices. */
67 /** Content of the received slices. */
72 * Data private to the fecdec filter.
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. */
81 /** Points to the first received group. */
82 struct fecdec_group *first_complete_group;
83 struct btr_pool *btrp;
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)++)
90 static int group_complete(struct fecdec_group *fg)
92 return fg->num_received_slices >= fg->h.data_slices_per_group;
95 static int group_empty(struct fecdec_group *fg)
97 return fg->num_received_slices == 0;
100 static void clear_group(struct fecdec_group *fg)
104 for (i = 0; i < fg->num_slices; i++)
108 memset(fg, 0, sizeof(*fg));
111 static int find_group(struct fec_header *h,
112 struct private_fecdec_data *pfd, struct fecdec_group **result)
114 struct fecdec_group *fg;
116 FOR_EACH_FECDEC_GROUP(fg, pfd) {
117 if (fg->h.group_num != h->group_num)
119 if (fg->num_received_slices == 0)
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;
134 static struct fecdec_group *find_unused_group(struct private_fecdec_data *pfd)
136 struct fecdec_group *fg;
138 FOR_EACH_FECDEC_GROUP(fg, pfd) {
139 if (fg->num_received_slices == 0)
145 static struct fecdec_group *try_to_free_group(struct private_fecdec_data *pfd)
147 struct fecdec_group *fg;
149 FOR_EACH_FECDEC_GROUP(fg, pfd) {
150 if (!group_complete(fg))
153 * Don't clear the first complete group if it has not yet been
156 if (fg == pfd->first_complete_group)
164 static struct fecdec_group *free_oldest_group(struct private_fecdec_data *pfd)
166 struct fecdec_group *fg, *oldest = NULL;
168 FOR_EACH_FECDEC_GROUP(fg, pfd) {
169 if (!oldest || oldest->h.group_num > fg->h.group_num)
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;
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)
186 struct fecdec_group *fg;
187 int ret = find_group(h, pfd, &fg);
191 if (ret > 0) /* found group */
193 /* group not found */
194 fg = find_unused_group(pfd);
197 fg = try_to_free_group(pfd);
200 fg = free_oldest_group(pfd);
208 static bool test_and_set_slice_bit(struct fecdec_group *fg, uint8_t slice_num)
210 uint8_t *p = fg->received_slices + slice_num / 8, old = *p;
212 *p |= 1 << (slice_num % 8);
217 * returns 1 if slice was added, zero otherwise (because the group was already
218 * complete or a slice has been received twice).
220 static int add_slice(char *buf, struct fecdec_group *fg)
223 uint8_t slice_num = fg->h.slice_num;
225 if (group_complete(fg)) {
226 PARA_DEBUG_LOG("group %u complete, ignoring slice %d\n",
227 fg->h.group_num, slice_num);
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 *));
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,
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++;
250 * The different states of a complete FEC group.
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.
256 enum fec_group_usability {
257 /** Drop the group (because we did not receive the header yet). */
259 /** Use all data in the group. */
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
267 static enum fec_group_usability group_is_usable(struct fecdec_group *fg,
268 struct private_fecdec_data *pfd)
270 struct fec_header *h = &fg->h;
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;
280 return FEC_GROUP_USABLE;
281 if (fg->h.audio_header_size)
282 return FEC_GROUP_USABLE_WITH_HEADER;
283 return FEC_GROUP_UNUSABLE;
286 static int decode_group(struct fecdec_group *fg, struct filter_node *fn)
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);
294 if (u == FEC_GROUP_UNUSABLE) {
295 PARA_INFO_LOG("dropping unusable group %u\n", fg->h.group_num);
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);
303 pfd->have_header = 1;
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);
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");
319 for (i = 0; i < fg->h.data_slices_per_group; i++) {
321 if (written >= fg->h.audio_header_size)
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);
330 for (; i < fg->h.data_slices_per_group; i++) {
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);
341 * Read a fec header from a buffer.
343 * \param buf The buffer to write to.
344 * \param h The fec header to write.
346 static int read_fec_header(char *buf, size_t len, struct fec_header *h)
350 if (len < FEC_HEADER_SIZE)
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);
359 h->group_num = read_u32(buf + 10);
360 h->group_bytes = read_u32(buf + 14);
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);
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)
377 struct fecdec_group *fg;
379 struct private_fecdec_data *pfd = fn->private_data;
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;
385 ret = get_group(h, pfd, &fg);
388 if (!add_slice(buf, fg)) /* group already complete */
390 if (!group_complete(fg))
392 /* this slice completed the group */
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 */
401 pfd->first_complete_group = fg; /* remember it */
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);
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);
416 clear_group(pfd->first_complete_group);
417 pfd->first_complete_group = NULL;
419 ret = decode_group(fg, fn);
425 static void fecdec_close(struct filter_node *fn)
427 struct private_fecdec_data *pfd = fn->private_data;
428 struct fecdec_group *fg;
430 FOR_EACH_FECDEC_GROUP(fg, pfd)
433 btr_pool_free(pfd->btrp);
434 free(fn->private_data);
435 fn->private_data = NULL;
438 static int fecdec_post_select(__a_unused struct sched *s, void *context)
440 struct filter_node *fn = context;
441 struct btr_node *btrn = fn->btrn;
448 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
451 btr_merge(btrn, fn->min_iqs);
452 len = btr_next_buffer(btrn, &buf);
453 ret = read_fec_header(buf, len, &h);
456 ret = -E_BAD_SLICE_SIZE;
459 ret = -E_BAD_SLICE_NUM;
460 if (h.slice_num > h.slices_per_group)
462 ret = dispatch_slice(buf + FEC_HEADER_SIZE, len - FEC_HEADER_SIZE,
464 //PARA_INFO_LOG("ret: %d, len: %d, slice_bytes: %d\n", ret, len, h.slice_bytes);
467 btr_consume(btrn, FEC_HEADER_SIZE + h.slice_bytes);
471 btr_remove_node(&fn->btrn);
475 static void fecdec_open(struct filter_node *fn)
477 struct private_fecdec_data *pfd;
478 pfd = para_calloc(sizeof(*pfd));
479 fn->private_data = pfd;
480 fn->min_iqs = FEC_HEADER_SIZE;
483 const struct filter lsg_filter_cmd_com_fecdec_user_data = {
485 .pre_select = generic_filter_pre_select,
486 .post_select = fecdec_post_select,
487 .close = fecdec_close,