2 * Copyright (C) 2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file fecdec_filter.c A filter that fec-decodes an audio stream. */
17 #include "portable_io.h"
22 * How many FEC groups to store in memory.
24 * Packet reordering requires to keep more than one FEC group in memory because
25 * slices belonging to the next FEC group may arrive before the current FEC group
28 #define NUM_FEC_GROUPS 3
30 /** Default size of the output buffer of the fecdec filter. */
31 #define FECDEC_DEFAULT_OUTBUF_SIZE (3 * 1024)
32 /** Maximal size of the output buffer of the fecdec filter. */
33 #define FECDEC_MAX_OUTBUF_SIZE (1024 * 1024)
35 /** Data read from the header of a slice. */
37 /** Total number of slices in this group. */
38 uint8_t slices_per_group;
39 /** Number of slices needed to start decoding. */
40 uint8_t data_slices_per_group;
41 /** Size of the ogg vorbis header (zero for mp3, aac). */
42 uint32_t audio_header_size;
43 /** Number of the FEC group this slice belongs to. */
45 /** Size of the data in this FEC group. */
47 /** Number of this slice in the group. */
49 /** Used data bytes of this slice. */
51 /** Non-zero if this group is the beginning of the stream. */
53 /** Non-zero if this stream embedds audio headers into fec groups. */
54 uint8_t header_stream;
58 * The status of one partially received FEC group.
61 /** The header read from the last slice. */
63 /** How many slices received so far. */
64 int num_received_slices;
65 /** The size of the \a idx and the \a data arrays below. */
67 /** Array of indices of the received slices. */
69 /** Content of the received slices. */
74 * Data private to the fecdec filter.
76 struct private_fecdec_data {
77 /** Used by the fec core code. */
78 struct fec_parms *fec;
79 /** Keeps track of what was received so far. */
80 struct fecdec_group groups[NUM_FEC_GROUPS];
81 /** Whether an audio file header was already received. */
83 /** Points to the first received group. */
84 struct fecdec_group *first_complete_group;
87 /** Iterate over all fecdec groups. */
88 #define FOR_EACH_FECDEC_GROUP(g, d) for (g = (d)->groups; \
89 (g) < (d)->groups + NUM_FEC_GROUPS; (g)++)
91 static int group_complete(struct fecdec_group *fg)
93 return fg->num_received_slices >= fg->h.data_slices_per_group;
96 static int group_empty(struct fecdec_group *fg)
98 return fg->num_received_slices == 0;
101 static void clear_group(struct fecdec_group *fg)
105 for (i = 0; i < fg->num_slices; i++) {
113 memset(&fg->h, 0, sizeof(struct fec_header));
114 fg->num_received_slices = 0;
117 static int find_group(struct fec_header *h,
118 struct private_fecdec_data *pfd, struct fecdec_group **result)
120 struct fecdec_group *fg;
122 FOR_EACH_FECDEC_GROUP(fg, pfd) {
123 if (fg->h.group_num != h->group_num)
125 if (fg->num_received_slices == 0)
127 if (fg->h.slices_per_group != h->slices_per_group)
128 return -E_BAD_FEC_HEADER;
129 if (fg->h.data_slices_per_group != h->data_slices_per_group)
130 return -E_BAD_FEC_HEADER;
131 if (fg->h.group_bytes != h->group_bytes)
132 return -E_BAD_FEC_HEADER;
140 static struct fecdec_group *find_unused_group(struct private_fecdec_data *pfd)
142 struct fecdec_group *fg;
144 FOR_EACH_FECDEC_GROUP(fg, pfd) {
145 if (fg->num_received_slices == 0)
151 static struct fecdec_group *try_to_free_group(struct private_fecdec_data *pfd)
153 struct fecdec_group *fg;
155 FOR_EACH_FECDEC_GROUP(fg, pfd) {
156 if (!group_complete(fg))
159 * Don't clear the first complete group if it has not yet been
162 if (fg == pfd->first_complete_group)
170 static struct fecdec_group *free_oldest_group(struct private_fecdec_data *pfd)
172 struct fecdec_group *fg, *oldest = NULL;
174 FOR_EACH_FECDEC_GROUP(fg, pfd) {
175 if (!oldest || oldest->h.group_num > fg->h.group_num)
178 if (!group_complete(oldest) && !group_empty(oldest))
179 PARA_WARNING_LOG("Clearing incomplete group %d "
180 "(contains %d slices)\n", oldest->h.group_num,
181 oldest->num_received_slices);
182 if (oldest == pfd->first_complete_group)
183 pfd->first_complete_group = NULL;
188 /* returns 1 if the group was found, 0 if not, negative on errors */
189 static int get_group(struct fec_header *h, struct private_fecdec_data *pfd,
190 struct fecdec_group **result)
192 struct fecdec_group *fg;
193 int ret = find_group(h, pfd, &fg);
197 if (ret > 0) /* found group */
199 /* group not found */
200 fg = find_unused_group(pfd);
203 fg = try_to_free_group(pfd);
206 fg = free_oldest_group(pfd);
215 * returns 1 if slice was added, zero otherwise (because the group was already
216 * complete). In any case the number of received slices is being increased by
219 static int add_slice(char *buf, struct fecdec_group *fg)
223 if (group_complete(fg)) {
224 PARA_DEBUG_LOG("group %d complete, ignoring slice %d\n",
225 fg->h.group_num, fg->h.slice_num);
226 fg->num_received_slices++;
229 slice_num = fg->h.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_malloc(fg->num_slices * sizeof(unsigned char *));
234 memset(fg->data, 0, fg->num_slices * sizeof(unsigned char *));
236 r = fg->num_received_slices;
237 fg->idx[r] = slice_num;
238 fg->data[r] = para_malloc(fg->h.slice_bytes);
239 memcpy(fg->data[r], buf, fg->h.slice_bytes);
240 fg->num_received_slices++;
244 enum fec_group_usability {
247 FEC_GROUP_USABLE_SKIP_HEADER,
248 FEC_GROUP_USABLE_WITH_HEADER
251 static enum fec_group_usability group_is_usable(struct fecdec_group *fg,
252 struct private_fecdec_data *pfd)
254 struct fec_header *h = &fg->h;
256 if (!h->header_stream)
257 return FEC_GROUP_USABLE;
258 if (pfd->have_header) {
259 if (h->audio_header_size)
260 return FEC_GROUP_USABLE_SKIP_HEADER;
261 return FEC_GROUP_USABLE;
264 return FEC_GROUP_USABLE;
265 if (fg->h.audio_header_size)
266 return FEC_GROUP_USABLE_WITH_HEADER;
267 return FEC_GROUP_UNUSABLE;
270 static int decode_group(struct fecdec_group *fg, struct filter_node *fn)
272 int i, ret, sb = fg->h.slice_bytes;
273 size_t written, need;
274 struct private_fecdec_data *pfd = fn->private_data;
275 enum fec_group_usability u = group_is_usable(fg, pfd);
277 if (u == FEC_GROUP_UNUSABLE) {
278 PARA_INFO_LOG("dropping unusable group %d\n", fg->h.group_num);
281 PARA_DEBUG_LOG("decoding group %d (%d slices)\n", fg->h.group_num,
282 fg->h.data_slices_per_group);
283 ret = fec_decode(pfd->fec, fg->data, fg->idx, sb);
286 pfd->have_header = 1;
288 if (u == FEC_GROUP_USABLE_SKIP_HEADER) {
289 i = ROUND_UP(fg->h.audio_header_size, fg->h.slice_bytes)
291 PARA_DEBUG_LOG("skipping %d header slices\n", i);
293 PARA_DEBUG_LOG("writing group %d (%d/%d decoded data bytes)\n",
294 fg->h.group_num, fg->h.group_bytes,
295 fg->h.data_slices_per_group * sb);
296 need = fn->loaded + (fg->h.data_slices_per_group - i)* sb;
297 if (need > fn->bufsize) {
298 fn->bufsize = PARA_MAX(fn->bufsize * 2, need);
299 if (fn->bufsize > FECDEC_MAX_OUTBUF_SIZE)
300 return -E_FECDEC_OVERRUN;
301 PARA_INFO_LOG("increasing fec buf to %zu\n", fn->bufsize);
302 fn->buf = para_realloc(fn->buf, fn->bufsize);
304 if (u == FEC_GROUP_USABLE_WITH_HEADER) {
305 PARA_INFO_LOG("writing audio file header\n");
307 for (i = 0; i < fg->h.data_slices_per_group; i++) {
309 if (written >= fg->h.audio_header_size)
311 if (sb + written > fg->h.audio_header_size)
312 n = fg->h.audio_header_size - written;
313 memcpy(fn->buf + fn->loaded, fg->data[i], n);
319 for (; i < fg->h.data_slices_per_group; i++) {
321 if (n + written > fg->h.group_bytes)
322 n = fg->h.group_bytes - written;
323 memcpy(fn->buf + fn->loaded, fg->data[i], n);
331 * Read a fec header from a buffer.
333 * \param buf The buffer to write to.
334 * \param h The fec header to write.
336 static int read_fec_header(char *buf, size_t len, struct fec_header *h)
340 if (len < FEC_HEADER_SIZE)
342 magic = read_u32(buf);
343 if (magic != FEC_MAGIC)
344 return -E_BAD_FEC_HEADER;
345 h->slices_per_group = read_u8(buf + 4);
346 h->data_slices_per_group = read_u8(buf + 5);
347 h->audio_header_size = read_u32(buf + 6);
349 h->group_num = read_u32(buf + 10);
350 h->group_bytes = read_u32(buf + 14);
352 h->slice_num = read_u8(buf + 18);
353 h->slice_bytes = read_u16(buf + 20);
354 h->bos = read_u8(buf + 22);
355 h->header_stream = read_u8(buf + 23);
356 if (!memcmp(buf, FEC_EOF_PACKET, FEC_EOF_PACKET_LEN))
357 return -E_FECDEC_EOF;
358 // PARA_DEBUG_LOG("group %u, slize %u, slices per group: %u\n",
359 // h->group_num, h->slice_num, h->slices_per_group);
363 /* returns 1 if we used the buffer, 0 if we didn't, negative on errors */
364 static int dispatch_slice(char *buf, size_t len, struct fec_header *h,
365 struct filter_node *fn)
367 struct fecdec_group *fg;
369 struct private_fecdec_data *pfd = fn->private_data;
371 if (h->slice_bytes > len) /* can not use the thing, try to read more */
373 ret = get_group(h, pfd, &fg);
376 if (!add_slice(buf, fg)) /* group already complete */
378 if (!group_complete(fg))
380 /* this slice completed the group */
383 /* it's either the first or the second complete group */
384 if (!pfd->first_complete_group) { /* it's the first group */
385 enum fec_group_usability u = group_is_usable(fg, pfd);
386 assert(u != FEC_GROUP_USABLE_SKIP_HEADER);
387 if (u == FEC_GROUP_UNUSABLE) /* forget it */
389 pfd->first_complete_group = fg; /* remember it */
392 /* we have two complete groups, let's go */
393 k = h->data_slices_per_group;
394 n = h->slices_per_group;
395 PARA_NOTICE_LOG("init fec (%d, %d)\n", k, n);
396 ret = fec_new(k, n, &pfd->fec);
399 /* decode and clear the first group */
400 ret = decode_group(pfd->first_complete_group, fn);
403 clear_group(pfd->first_complete_group);
404 pfd->first_complete_group = NULL;
406 ret = decode_group(fg, fn);
412 static ssize_t fecdec(char *buf, size_t len, struct filter_node *fn)
417 ret = read_fec_header(buf, len, &h);
421 return -E_BAD_SLICE_SIZE;
422 if (h.slice_num > h.slices_per_group)
423 return -E_BAD_SLICE_NUM;
424 ret = dispatch_slice(buf + FEC_HEADER_SIZE, len - FEC_HEADER_SIZE,
426 //PARA_INFO_LOG("ret: %d, len: %d, slice_bytes: %d\n", ret, len, h.slice_bytes);
429 return FEC_HEADER_SIZE + h.slice_bytes;
432 static void fecdec_close(struct filter_node *fn)
434 struct private_fecdec_data *pfd = fn->private_data;
435 struct fecdec_group *fg;
437 FOR_EACH_FECDEC_GROUP(fg, pfd)
442 free(fn->private_data);
443 fn->private_data = NULL;
446 static void fecdec_open(struct filter_node *fn)
448 struct private_fecdec_data *pfd;
449 fn->bufsize = FECDEC_DEFAULT_OUTBUF_SIZE;
450 fn->buf = para_malloc(fn->bufsize);
451 pfd = para_calloc(sizeof(*pfd));
452 fn->private_data = pfd;
457 * The init function of the fecdec filter.
459 * \param f struct to initialize.
461 void fecdec_filter_init(struct filter *f)
464 f->close = fecdec_close;
465 f->open = fecdec_open;