Add some more FEC source code documentation.
[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 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 #define INPUT_BUFFER_SIZE 16384
31
32 /** Size of the output buffer of the fecdec filter. */
33 #define FECDEC_OUTBUF_SIZE 81920
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 };
52
53 struct fec_group {
54 struct fec_header h;
55 int num_received_slices;
56 int num_slices;
57 int *idx;
58 unsigned char **data;
59 };
60
61 struct private_fecdec_data {
62 struct fec_parms *fec;
63 struct fec_group groups[NUM_FEC_GROUPS];
64 };
65
66 #define FOR_EACH_FEC_GROUP(g, d) for (g = (d)->groups; \
67 (g) - (d)->groups < NUM_FEC_GROUPS; (g)++)
68
69 #define UNUSED_GROUP_NUM 0xffffffff
70
71 static int group_complete(struct fec_group *fg)
72 {
73 if (fg->h.group_num == UNUSED_GROUP_NUM)
74 return 0;
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;
77 }
78
79 static int group_empty(struct fec_group *fg)
80 {
81 return fg->num_received_slices == 0;
82 }
83
84 static void clear_group(struct fec_group *fg)
85 {
86 int i;
87
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++) {
93 free(fg->data[i]);
94 fg->data[i] = NULL;
95 fg->idx[i] = -1;
96 }
97 free(fg->data);
98 free(fg->idx);
99 fg->num_slices = 0;
100 memset(&fg->h, 0, sizeof(struct fec_header));
101 fg->num_received_slices = 0;
102 fg->h.group_num = UNUSED_GROUP_NUM;
103 }
104
105 static int find_group(struct fec_header *h,
106 struct private_fecdec_data *pfd, struct fec_group **result)
107 {
108 struct fec_group *fg;
109
110 FOR_EACH_FEC_GROUP(fg, pfd) {
111 if (fg->h.group_num != h->group_num)
112 continue;
113 *result = fg;
114 return 1;
115 }
116 return 0;
117 }
118
119 static struct fec_group *find_unused_group(struct private_fecdec_data *pfd)
120 {
121 struct fec_group *fg;
122
123 FOR_EACH_FEC_GROUP(fg, pfd) {
124 if (fg->num_received_slices == 0)
125 return fg;
126 }
127 return NULL;
128 }
129
130 static struct fec_group *try_to_free_group(struct private_fecdec_data *pfd)
131 {
132 struct fec_group *fg;
133
134 FOR_EACH_FEC_GROUP(fg, pfd) {
135 if (!group_complete(fg))
136 continue;
137 clear_group(fg);
138 return fg;
139 }
140 return NULL;
141 }
142
143 static struct fec_group *free_oldest_group(struct private_fecdec_data *pfd)
144 {
145 struct fec_group *fg, *oldest = NULL;
146
147 FOR_EACH_FEC_GROUP(fg, pfd) {
148 if (!oldest || oldest->h.group_num > fg->h.group_num)
149 oldest = fg;
150 }
151 clear_group(oldest);
152 return oldest;
153 }
154
155 static int get_group(struct fec_header *h, struct private_fecdec_data *pfd,
156 struct fec_group **result)
157 {
158 struct fec_group *fg;
159 int ret = find_group(h, pfd, &fg);
160
161 if (ret < 0)
162 return ret;
163 if (ret > 0) /* found group */
164 goto success;
165 /* group not found */
166 fg = find_unused_group(pfd);
167 if (fg)
168 goto update_header;
169 fg = try_to_free_group(pfd);
170 if (fg)
171 goto update_header;
172 fg = free_oldest_group(pfd);
173 update_header:
174 fg->h = *h;
175 success:
176 *result = fg;
177 return 1;
178 }
179
180 static int add_slice(char *buf, struct fec_group *fg)
181 {
182 int r, slice_num;
183
184 if (group_complete(fg))
185 return 0;
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 *));
192 }
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++;
198 return 1;
199 }
200
201 static int decode_group(struct fec_group *fg, struct filter_node *fn)
202 {
203 int i, ret, sb = fg->h.slice_bytes;
204 size_t written = 0;
205 struct private_fecdec_data *pfd = fn->private_data;
206
207 ret = fec_decode(pfd->fec, fg->data, fg->idx, sb);
208 if (ret < 0)
209 return ret;
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++) {
214 size_t n = sb;
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);
220 fn->loaded += n;
221 written += n;
222 }
223 return 0;
224 }
225
226 /**
227 * Read a fec header from a buffer.
228 *
229 * \param buf The buffer to write to.
230 * \param h The fec header to write.
231 */
232 static int read_fec_header(char *buf, size_t len, struct fec_header *h)
233 {
234 uint32_t magic;
235
236 if (len < FEC_HEADER_SIZE)
237 return 0;
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);
244
245 h->group_num = read_u32(buf + 10);
246 h->group_bytes = read_u32(buf + 14);
247
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);
254 return 1;
255 }
256
257 static int dispatch_slice(char *buf, size_t len, struct fec_header *h,
258 struct filter_node *fn)
259 {
260 struct fec_group *fg;
261 int ret;
262 struct private_fecdec_data *pfd = fn->private_data;
263
264 if (h->slice_bytes > len) /* can not use the thing, try to read more */
265 return 0;
266 ret = get_group(h, pfd, &fg);
267 if (ret < 0)
268 return ret;
269 if (group_complete(fg)) {
270 PARA_DEBUG_LOG("group complete, ignoring slice %d\n",
271 h->slice_num);
272 return 1;
273 }
274 fg->h = *h;
275 ret = add_slice(buf, fg);
276 if (ret < 0)
277 return ret;
278 if (group_complete(fg)) {
279 if (!pfd->fec) {
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);
283 if (ret < 0)
284 return ret;
285 }
286 ret = decode_group(fg, fn);
287 if (ret < 0)
288 return ret;
289 }
290 return 1;
291 }
292
293 static int fecdec(char *buf, size_t len, struct filter_node *fn)
294 {
295 int ret;
296 struct fec_header h;
297
298 ret = read_fec_header(buf, len, &h);
299 if (ret <= 0)
300 return ret;
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,
306 &h, fn);
307 //PARA_INFO_LOG("ret: %d, len: %d, slice_bytes: %d\n", ret, len, h.slice_bytes);
308 if (ret <= 0)
309 return ret;
310 return FEC_HEADER_SIZE + h.slice_bytes;
311 }
312
313 static void fecdec_close(struct filter_node *fn)
314 {
315 struct private_fecdec_data *pfd = fn->private_data;
316 struct fec_group *fg;
317
318 FOR_EACH_FEC_GROUP(fg, pfd)
319 clear_group(fg);
320 free(fn->buf);
321 fn->buf = NULL;
322 free(fn->private_data);
323 fn->private_data = NULL;
324 }
325
326 static void fecdec_open(struct filter_node *fn)
327 {
328 fn->bufsize = FECDEC_OUTBUF_SIZE;
329 fn->buf = para_malloc(fn->bufsize);
330 fn->private_data = para_calloc(sizeof(struct private_fecdec_data));
331 fn->loaded = 0;
332 }
333
334 /**
335 * The init function of the fecdec filter.
336 *
337 * \param f struct to initialize.
338 */
339 void fecdec_filter_init(struct filter *f)
340 {
341 f->convert = fecdec;
342 f->close = fecdec_close;
343 f->open = fecdec_open;
344 }