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 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 #define INPUT_BUFFER_SIZE 16384
32 /** Size of the output buffer of the fecdec filter. */
33 #define FECDEC_OUTBUF_SIZE 81920
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. */
55 int num_received_slices;
61 struct private_fecdec_data {
62 struct fec_parms *fec;
63 struct fec_group groups[NUM_FEC_GROUPS];
66 #define FOR_EACH_FEC_GROUP(g, d) for (g = (d)->groups; \
67 (g) - (d)->groups < NUM_FEC_GROUPS; (g)++)
69 #define UNUSED_GROUP_NUM 0xffffffff
71 static int group_complete(struct fec_group *fg)
73 if (fg->h.group_num == UNUSED_GROUP_NUM)
75 //PARA_INFO_LOG("received slices: %u, slices per group: %u\n", fg->num_received_slices, fg->h.data_slices_per_group);
76 return fg->num_received_slices >= fg->h.data_slices_per_group;
79 static int group_empty(struct fec_group *fg)
81 return fg->num_received_slices == 0;
84 static void clear_group(struct fec_group *fg)
88 if (!group_complete(fg) && !group_empty(fg))
89 PARA_WARNING_LOG("Clearing incomplete group %d "
90 "(contains %d slices)\n", fg->h.group_num,
91 fg->num_received_slices);
92 for (i = 0; i < fg->num_slices; i++) {
100 memset(&fg->h, 0, sizeof(struct fec_header));
101 fg->num_received_slices = 0;
102 fg->h.group_num = UNUSED_GROUP_NUM;
105 static int find_group(struct fec_header *h,
106 struct private_fecdec_data *pfd, struct fec_group **result)
108 struct fec_group *fg;
110 FOR_EACH_FEC_GROUP(fg, pfd) {
111 if (fg->h.group_num != h->group_num)
119 static struct fec_group *find_unused_group(struct private_fecdec_data *pfd)
121 struct fec_group *fg;
123 FOR_EACH_FEC_GROUP(fg, pfd) {
124 if (fg->num_received_slices == 0)
130 static struct fec_group *try_to_free_group(struct private_fecdec_data *pfd)
132 struct fec_group *fg;
134 FOR_EACH_FEC_GROUP(fg, pfd) {
135 if (!group_complete(fg))
143 static struct fec_group *free_oldest_group(struct private_fecdec_data *pfd)
145 struct fec_group *fg, *oldest = NULL;
147 FOR_EACH_FEC_GROUP(fg, pfd) {
148 if (!oldest || oldest->h.group_num > fg->h.group_num)
155 static int get_group(struct fec_header *h, struct private_fecdec_data *pfd,
156 struct fec_group **result)
158 struct fec_group *fg;
159 int ret = find_group(h, pfd, &fg);
163 if (ret > 0) /* found group */
165 /* group not found */
166 fg = find_unused_group(pfd);
169 fg = try_to_free_group(pfd);
172 fg = free_oldest_group(pfd);
180 static int add_slice(char *buf, struct fec_group *fg)
184 if (group_complete(fg))
186 slice_num = fg->h.slice_num;
187 if (fg->num_slices == 0) {
188 fg->num_slices = fg->h.slices_per_group;
189 fg->idx = malloc(fg->num_slices * sizeof(int));
190 fg->data = malloc(fg->num_slices * sizeof(unsigned char *));
191 memset(fg->data, 0, fg->num_slices * sizeof(unsigned char *));
193 r = fg->num_received_slices;
194 fg->idx[r] = slice_num;
195 fg->data[r] = malloc(fg->h.slice_bytes);
196 memcpy(fg->data[r], buf, fg->h.slice_bytes);
197 fg->num_received_slices++;
201 static int decode_group(struct fec_group *fg, struct filter_node *fn)
203 int i, ret, sb = fg->h.slice_bytes;
205 struct private_fecdec_data *pfd = fn->private_data;
207 ret = fec_decode(pfd->fec, fg->data, fg->idx, sb);
210 PARA_DEBUG_LOG("writing group %d (%d/%d decoded data bytes)\n",
211 fg->h.group_num, fg->h.group_bytes,
212 fg->h.data_slices_per_group * sb);
213 for (i = 0; i < fg->h.data_slices_per_group; i++) {
215 if (n + written > fg->h.group_bytes)
216 n = fg->h.group_bytes - written;
217 if (fn->loaded + n > fn->bufsize)
218 return -E_FECDEC_OVERRUN;
219 memcpy(fn->buf + fn->loaded, fg->data[i], n);
227 * Read a fec header from a buffer.
229 * \param buf The buffer to write to.
230 * \param h The fec header to write.
232 static int read_fec_header(char *buf, size_t len, struct fec_header *h)
236 if (len < FEC_HEADER_SIZE)
238 magic = read_u32(buf);
239 if (magic != FEC_MAGIC)
240 return -E_BAD_FEC_HEADER;
241 h->slices_per_group = read_u8(buf + 4);
242 h->data_slices_per_group = read_u8(buf + 5);
243 h->audio_header_size = read_u32(buf + 6);
245 h->group_num = read_u32(buf + 10);
246 h->group_bytes = read_u32(buf + 14);
248 h->slice_num = read_u8(buf + 18);
249 h->slice_bytes = read_u16(buf + 20);
250 if (!h->group_bytes && & h->slice_bytes)
251 return -E_FECDEC_EOF;
252 // PARA_DEBUG_LOG("group %u, slize %u, slices per group: %u\n",
253 // h->group_num, h->slice_num, h->slices_per_group);
257 static int dispatch_slice(char *buf, size_t len, struct fec_header *h,
258 struct filter_node *fn)
260 struct fec_group *fg;
262 struct private_fecdec_data *pfd = fn->private_data;
264 if (h->slice_bytes > len) /* can not use the thing, try to read more */
266 ret = get_group(h, pfd, &fg);
269 if (group_complete(fg)) {
270 PARA_DEBUG_LOG("group complete, ignoring slice %d\n",
275 ret = add_slice(buf, fg);
278 if (group_complete(fg)) {
280 int k = h->data_slices_per_group, n = h->slices_per_group;
281 PARA_NOTICE_LOG("init fec (%d, %d)\n", k, n);
282 ret = fec_new(k, n, &pfd->fec);
286 ret = decode_group(fg, fn);
293 static int fecdec(char *buf, size_t len, struct filter_node *fn)
298 ret = read_fec_header(buf, len, &h);
301 if (h.slice_bytes > INPUT_BUFFER_SIZE)
302 return -E_BAD_SLICE_SIZE;
303 if (h.slice_num > h.slices_per_group)
304 return -E_BAD_SLICE_NUM;
305 ret = dispatch_slice(buf + FEC_HEADER_SIZE, len - FEC_HEADER_SIZE,
307 //PARA_INFO_LOG("ret: %d, len: %d, slice_bytes: %d\n", ret, len, h.slice_bytes);
310 return FEC_HEADER_SIZE + h.slice_bytes;
313 static void fecdec_close(struct filter_node *fn)
315 struct private_fecdec_data *pfd = fn->private_data;
316 struct fec_group *fg;
318 FOR_EACH_FEC_GROUP(fg, pfd)
322 free(fn->private_data);
323 fn->private_data = NULL;
326 static void fecdec_open(struct filter_node *fn)
328 fn->bufsize = FECDEC_OUTBUF_SIZE;
329 fn->buf = para_malloc(fn->bufsize);
330 fn->private_data = para_calloc(sizeof(struct private_fecdec_data));
335 * The init function of the fecdec filter.
337 * \param f struct to initialize.
339 void fecdec_filter_init(struct filter *f)
342 f->close = fecdec_close;
343 f->open = fecdec_open;