a6218e436506d8000192a3cbb8f7a44da1133d32
[paraslash.git] / fecdec_filter.c
1 /*
2 * Copyright (C) 2009 Andre Noll <maan@systemlinux.org>
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 <dirent.h>
10 #include "para.h"
11 #include "error.h"
12 #include "list.h"
13 #include "sched.h"
14 #include "ggo.h"
15 #include "filter.h"
16 #include "string.h"
17 #include "portable_io.h"
18 #include "fec.h"
19 #include "fd.h"
20
21 /**
22 * How many FEC groups to store in memory.
23 *
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
26 * is complete.
27 */
28 #define NUM_FEC_GROUPS 3
29
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)
34
35 /** Data read from the header of a slice. */
36 struct fec_header {
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. */
44 uint32_t group_num;
45 /** Size of the data in this FEC group. */
46 uint32_t group_bytes;
47 /** Number of this slice in the group. */
48 uint8_t slice_num;
49 /** Used data bytes of this slice. */
50 uint16_t slice_bytes;
51 /** Non-zero if this group is the beginning of the stream. */
52 uint8_t bos;
53 /** Non-zero if this stream embedds audio headers into fec groups. */
54 uint8_t header_stream;
55 };
56
57 /**
58 * The status of one partially received FEC group.
59 */
60 struct fecdec_group {
61 /** The header read from the last slice. */
62 struct fec_header h;
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. */
66 int num_slices;
67 /** Array of indices of the received slices. */
68 int *idx;
69 /** Content of the received slices. */
70 unsigned char **data;
71 };
72
73 /**
74 * Data private to the fecdec filter.
75 */
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. */
82 int have_header;
83 /** Points to the first received group. */
84 struct fecdec_group *first_complete_group;
85 };
86
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)++)
90
91 static int group_complete(struct fecdec_group *fg)
92 {
93 return fg->num_received_slices >= fg->h.data_slices_per_group;
94 }
95
96 static int group_empty(struct fecdec_group *fg)
97 {
98 return fg->num_received_slices == 0;
99 }
100
101 static void clear_group(struct fecdec_group *fg)
102 {
103 int i;
104
105 for (i = 0; i < fg->num_slices; i++) {
106 free(fg->data[i]);
107 fg->data[i] = NULL;
108 fg->idx[i] = -1;
109 }
110 free(fg->data);
111 free(fg->idx);
112 fg->num_slices = 0;
113 memset(&fg->h, 0, sizeof(struct fec_header));
114 fg->num_received_slices = 0;
115 }
116
117 static int find_group(struct fec_header *h,
118 struct private_fecdec_data *pfd, struct fecdec_group **result)
119 {
120 struct fecdec_group *fg;
121
122 FOR_EACH_FECDEC_GROUP(fg, pfd) {
123 if (fg->h.group_num != h->group_num)
124 continue;
125 if (fg->num_received_slices == 0)
126 goto success;
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;
133 success:
134 *result = fg;
135 return 1;
136 }
137 return 0;
138 }
139
140 static struct fecdec_group *find_unused_group(struct private_fecdec_data *pfd)
141 {
142 struct fecdec_group *fg;
143
144 FOR_EACH_FECDEC_GROUP(fg, pfd) {
145 if (fg->num_received_slices == 0)
146 return fg;
147 }
148 return NULL;
149 }
150
151 static struct fecdec_group *try_to_free_group(struct private_fecdec_data *pfd)
152 {
153 struct fecdec_group *fg;
154
155 FOR_EACH_FECDEC_GROUP(fg, pfd) {
156 if (!group_complete(fg))
157 continue;
158 /*
159 * Don't clear the first complete group if it has not yet been
160 * decoded.
161 */
162 if (fg == pfd->first_complete_group)
163 continue;
164 clear_group(fg);
165 return fg;
166 }
167 return NULL;
168 }
169
170 static struct fecdec_group *free_oldest_group(struct private_fecdec_data *pfd)
171 {
172 struct fecdec_group *fg, *oldest = NULL;
173
174 FOR_EACH_FECDEC_GROUP(fg, pfd) {
175 if (!oldest || oldest->h.group_num > fg->h.group_num)
176 oldest = fg;
177 }
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;
184 clear_group(oldest);
185 return oldest;
186 }
187
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)
191 {
192 struct fecdec_group *fg;
193 int ret = find_group(h, pfd, &fg);
194
195 if (ret < 0)
196 return ret;
197 if (ret > 0) /* found group */
198 goto success;
199 /* group not found */
200 fg = find_unused_group(pfd);
201 if (fg)
202 goto success;
203 fg = try_to_free_group(pfd);
204 if (fg)
205 goto success;
206 fg = free_oldest_group(pfd);
207 ret = 0;
208 success:
209 fg->h = *h;
210 *result = fg;
211 return ret;
212 }
213
214 /*
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
217 * one.
218 */
219 static int add_slice(char *buf, struct fecdec_group *fg)
220 {
221 int r, slice_num;
222
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++;
227 return 0;
228 }
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 *));
235 }
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++;
241 return 1;
242 }
243
244 enum fec_group_usability {
245 FEC_GROUP_UNUSABLE,
246 FEC_GROUP_USABLE,
247 FEC_GROUP_USABLE_SKIP_HEADER,
248 FEC_GROUP_USABLE_WITH_HEADER
249 };
250
251 static enum fec_group_usability group_is_usable(struct fecdec_group *fg,
252 struct private_fecdec_data *pfd)
253 {
254 struct fec_header *h = &fg->h;
255
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;
262 }
263 if (fg->h.bos)
264 return FEC_GROUP_USABLE;
265 if (fg->h.audio_header_size)
266 return FEC_GROUP_USABLE_WITH_HEADER;
267 return FEC_GROUP_UNUSABLE;
268 }
269
270 static int decode_group(struct fecdec_group *fg, struct filter_node *fn)
271 {
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);
276
277 if (u == FEC_GROUP_UNUSABLE) {
278 PARA_INFO_LOG("dropping unusable group %d\n", fg->h.group_num);
279 return 0;
280 }
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);
284 if (ret < 0)
285 return ret;
286 pfd->have_header = 1;
287 i = 0;
288 if (u == FEC_GROUP_USABLE_SKIP_HEADER) {
289 i = ROUND_UP(fg->h.audio_header_size, fg->h.slice_bytes)
290 / fg->h.slice_bytes;
291 PARA_DEBUG_LOG("skipping %d header slices\n", i);
292 }
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);
303 }
304 if (u == FEC_GROUP_USABLE_WITH_HEADER) {
305 PARA_INFO_LOG("writing audio file header\n");
306 written = 0;
307 for (i = 0; i < fg->h.data_slices_per_group; i++) {
308 size_t n = sb;
309 if (written >= fg->h.audio_header_size)
310 break;
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);
314 fn->loaded += n;
315 written += n;
316 }
317 }
318 written = 0;
319 for (; i < fg->h.data_slices_per_group; i++) {
320 size_t n = sb;
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);
324 fn->loaded += n;
325 written += n;
326 }
327 return 0;
328 }
329
330 /**
331 * Read a fec header from a buffer.
332 *
333 * \param buf The buffer to write to.
334 * \param h The fec header to write.
335 */
336 static int read_fec_header(char *buf, size_t len, struct fec_header *h)
337 {
338 uint32_t magic;
339
340 if (len < FEC_HEADER_SIZE)
341 return 0;
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);
348
349 h->group_num = read_u32(buf + 10);
350 h->group_bytes = read_u32(buf + 14);
351
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);
360 return 1;
361 }
362
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)
366 {
367 struct fecdec_group *fg;
368 int ret, k, n;
369 struct private_fecdec_data *pfd = fn->private_data;
370
371 if (h->slice_bytes > len) /* can not use the thing, try to read more */
372 return 0;
373 ret = get_group(h, pfd, &fg);
374 if (ret < 0)
375 return ret;
376 if (!add_slice(buf, fg)) /* group already complete */
377 return 1;
378 if (!group_complete(fg))
379 return 1;
380 /* this slice completed the group */
381 if (pfd->fec)
382 goto decode;
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 */
388 return 1;
389 pfd->first_complete_group = fg; /* remember it */
390 return 1;
391 }
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);
397 if (ret < 0)
398 return ret;
399 /* decode and clear the first group */
400 ret = decode_group(pfd->first_complete_group, fn);
401 if (ret < 0)
402 return ret;
403 clear_group(pfd->first_complete_group);
404 pfd->first_complete_group = NULL;
405 decode:
406 ret = decode_group(fg, fn);
407 if (ret < 0)
408 return ret;
409 return 1;
410 }
411
412 static ssize_t fecdec(char *buf, size_t len, struct filter_node *fn)
413 {
414 int ret;
415 struct fec_header h;
416
417 ret = read_fec_header(buf, len, &h);
418 if (ret <= 0)
419 return ret;
420 if (!h.slice_bytes)
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,
425 &h, fn);
426 //PARA_INFO_LOG("ret: %d, len: %d, slice_bytes: %d\n", ret, len, h.slice_bytes);
427 if (ret <= 0)
428 return ret;
429 return FEC_HEADER_SIZE + h.slice_bytes;
430 }
431
432 static void fecdec_close(struct filter_node *fn)
433 {
434 struct private_fecdec_data *pfd = fn->private_data;
435 struct fecdec_group *fg;
436
437 FOR_EACH_FECDEC_GROUP(fg, pfd)
438 clear_group(fg);
439 free(fn->buf);
440 fn->buf = NULL;
441 fec_free(pfd->fec);
442 free(fn->private_data);
443 fn->private_data = NULL;
444 }
445
446 static void fecdec_open(struct filter_node *fn)
447 {
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;
453 fn->loaded = 0;
454 }
455
456 /**
457 * The init function of the fecdec filter.
458 *
459 * \param f struct to initialize.
460 */
461 void fecdec_filter_init(struct filter *f)
462 {
463 f->convert = fecdec;
464 f->close = fecdec_close;
465 f->open = fecdec_open;
466 }