1 /* Copyright (C) 2009 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file fecdec_filter.c A filter that fec-decodes an audio stream. */
11 #include "buffer_tree.h"
14 #include "portable_io.h"
19 * How many FEC groups to store in memory.
21 * Packet reordering requires to keep more than one FEC group in memory because
22 * slices belonging to the next FEC group may arrive before the current FEC group
25 #define NUM_FEC_GROUPS 3
27 /** Data read from the header of a slice. */
29 /** Total number of slices in this group. */
30 uint8_t slices_per_group
;
31 /** Number of slices needed to start decoding. */
32 uint8_t data_slices_per_group
;
33 /** Size of the ogg vorbis/wma header (zero for mp3, aac). */
34 uint32_t audio_header_size
;
35 /** Number of the FEC group this slice belongs to. */
37 /** Size of the data in this FEC group. */
39 /** Number of this slice in the group. */
41 /** Used data bytes of this slice. */
43 /** Non-zero if this group is the beginning of the stream. */
45 /** Non-zero if this stream embeds audio headers into fec groups. */
46 uint8_t header_stream
;
50 * The status of one partially received FEC group.
53 /** The header read from the last slice. */
55 /** How many slices received so far. */
56 int num_received_slices
;
57 /** Bitmap of received slices. */
58 uint8_t received_slices
[32];
59 /** The size of the \a idx and the \a data arrays below. */
61 /** Array of indices of the received slices. */
63 /** Content of the received slices. */
68 * Data private to the fecdec filter.
70 struct private_fecdec_data
{
71 /** Used by the fec core code. */
72 struct fec_parms
*fec
;
73 /** Keeps track of what was received so far. */
74 struct fecdec_group groups
[NUM_FEC_GROUPS
];
75 /** Whether an audio file header was already received. */
77 /** Points to the first received group. */
78 struct fecdec_group
*first_complete_group
;
79 struct btr_pool
*btrp
;
82 /** Iterate over all fecdec groups. */
83 #define FOR_EACH_FECDEC_GROUP(g, d) for (g = (d)->groups; \
84 (g) < (d)->groups + NUM_FEC_GROUPS; (g)++)
86 static int group_complete(struct fecdec_group
*fg
)
88 return fg
->num_received_slices
>= fg
->h
.data_slices_per_group
;
91 static int group_empty(struct fecdec_group
*fg
)
93 return fg
->num_received_slices
== 0;
96 static void clear_group(struct fecdec_group
*fg
)
100 for (i
= 0; i
< fg
->num_slices
; i
++)
104 memset(fg
, 0, sizeof(*fg
));
107 static int find_group(struct fec_header
*h
,
108 struct private_fecdec_data
*pfd
, struct fecdec_group
**result
)
110 struct fecdec_group
*fg
;
112 FOR_EACH_FECDEC_GROUP(fg
, pfd
) {
113 if (fg
->h
.group_num
!= h
->group_num
)
115 if (fg
->num_received_slices
== 0)
117 if (fg
->h
.slices_per_group
!= h
->slices_per_group
)
118 return -E_BAD_FEC_HEADER
;
119 if (fg
->h
.data_slices_per_group
!= h
->data_slices_per_group
)
120 return -E_BAD_FEC_HEADER
;
121 if (fg
->h
.group_bytes
!= h
->group_bytes
)
122 return -E_BAD_FEC_HEADER
;
130 static struct fecdec_group
*find_unused_group(struct private_fecdec_data
*pfd
)
132 struct fecdec_group
*fg
;
134 FOR_EACH_FECDEC_GROUP(fg
, pfd
) {
135 if (fg
->num_received_slices
== 0)
141 static struct fecdec_group
*try_to_free_group(struct private_fecdec_data
*pfd
)
143 struct fecdec_group
*fg
;
145 FOR_EACH_FECDEC_GROUP(fg
, pfd
) {
146 if (!group_complete(fg
))
149 * Don't clear the first complete group if it has not yet been
152 if (fg
== pfd
->first_complete_group
)
160 static struct fecdec_group
*free_oldest_group(struct private_fecdec_data
*pfd
)
162 struct fecdec_group
*fg
, *oldest
= NULL
;
164 FOR_EACH_FECDEC_GROUP(fg
, pfd
) {
165 if (!oldest
|| oldest
->h
.group_num
> fg
->h
.group_num
)
168 if (!group_complete(oldest
) && !group_empty(oldest
))
169 PARA_WARNING_LOG("Clearing incomplete group %u "
170 "(contains %d slices)\n", oldest
->h
.group_num
,
171 oldest
->num_received_slices
);
172 if (oldest
== pfd
->first_complete_group
)
173 pfd
->first_complete_group
= NULL
;
178 /* returns 1 if the group was found, 0 if not, negative on errors */
179 static int get_group(struct fec_header
*h
, struct private_fecdec_data
*pfd
,
180 struct fecdec_group
**result
)
182 struct fecdec_group
*fg
;
183 int ret
= find_group(h
, pfd
, &fg
);
187 if (ret
> 0) /* found group */
189 /* group not found */
190 fg
= find_unused_group(pfd
);
193 fg
= try_to_free_group(pfd
);
196 fg
= free_oldest_group(pfd
);
204 static bool test_and_set_slice_bit(struct fecdec_group
*fg
, uint8_t slice_num
)
206 uint8_t *p
= fg
->received_slices
+ slice_num
/ 8, old
= *p
;
208 *p
|= 1 << (slice_num
% 8);
213 * returns 1 if slice was added, zero otherwise (because the group was already
214 * complete or a slice has been received twice).
216 static int add_slice(char *buf
, struct fecdec_group
*fg
)
219 uint8_t slice_num
= fg
->h
.slice_num
;
221 if (group_complete(fg
)) {
222 PARA_DEBUG_LOG("group %u complete, ignoring slice %d\n",
223 fg
->h
.group_num
, slice_num
);
226 if (fg
->num_slices
== 0) {
227 fg
->num_slices
= fg
->h
.slices_per_group
;
228 fg
->idx
= para_malloc(fg
->num_slices
* sizeof(int));
229 fg
->data
= para_calloc(fg
->num_slices
* sizeof(unsigned char *));
231 r
= fg
->num_received_slices
;
232 /* Check if we already have this slice. */
233 if (test_and_set_slice_bit(fg
, slice_num
)) {
234 PARA_INFO_LOG("ignoring duplicate slice %u:%d\n", fg
->h
.group_num
,
238 fg
->idx
[r
] = slice_num
;
239 fg
->data
[r
] = para_malloc(fg
->h
.slice_bytes
);
240 memcpy(fg
->data
[r
], buf
, fg
->h
.slice_bytes
);
241 fg
->num_received_slices
++;
246 * The different states of a complete FEC group.
248 * Even if a FEC group has been received successfully, it probably can not be
249 * used right away because some streams (ogg, wma) need to receive an audio
250 * file header before decoding can start.
252 enum fec_group_usability
{
253 /** Drop the group (because we did not receive the header yet). */
255 /** Use all data in the group. */
257 /** Use the group, but drop its audio file header. */
258 FEC_GROUP_USABLE_SKIP_HEADER
,
259 /** Use the group, including its header. */
260 FEC_GROUP_USABLE_WITH_HEADER
263 static enum fec_group_usability
group_is_usable(struct fecdec_group
*fg
,
264 struct private_fecdec_data
*pfd
)
266 struct fec_header
*h
= &fg
->h
;
268 if (!h
->header_stream
)
269 return FEC_GROUP_USABLE
;
270 if (pfd
->have_header
) {
271 if (h
->audio_header_size
)
272 return FEC_GROUP_USABLE_SKIP_HEADER
;
273 return FEC_GROUP_USABLE
;
276 return FEC_GROUP_USABLE
;
277 if (fg
->h
.audio_header_size
)
278 return FEC_GROUP_USABLE_WITH_HEADER
;
279 return FEC_GROUP_UNUSABLE
;
282 static int decode_group(struct fecdec_group
*fg
, struct filter_node
*fn
)
284 int i
, ret
, sb
= fg
->h
.slice_bytes
;
285 size_t written
, need
;
286 struct private_fecdec_data
*pfd
= fn
->private_data
;
287 enum fec_group_usability u
= group_is_usable(fg
, pfd
);
290 if (u
== FEC_GROUP_UNUSABLE
) {
291 PARA_INFO_LOG("dropping unusable group %u\n", fg
->h
.group_num
);
294 PARA_DEBUG_LOG("decoding group %u (%d slices)\n", fg
->h
.group_num
,
295 fg
->h
.data_slices_per_group
);
296 ret
= fec_decode(pfd
->fec
, fg
->data
, fg
->idx
, sb
);
299 pfd
->have_header
= 1;
301 if (u
== FEC_GROUP_USABLE_SKIP_HEADER
) {
302 i
= DIV_ROUND_UP(fg
->h
.audio_header_size
, fg
->h
.slice_bytes
);
303 PARA_DEBUG_LOG("skipping %d header slices\n", i
);
305 PARA_DEBUG_LOG("writing group %u (%u/%d decoded data bytes)\n",
306 fg
->h
.group_num
, fg
->h
.group_bytes
,
307 fg
->h
.data_slices_per_group
* sb
);
308 need
= (fg
->h
.data_slices_per_group
- i
) * sb
;
309 if (need
> btr_pool_unused(pfd
->btrp
))
310 return -E_FECDEC_OVERRUN
;
311 btr_pool_get_buffer(pfd
->btrp
, &buf
);
312 if (u
== FEC_GROUP_USABLE_WITH_HEADER
) {
313 PARA_INFO_LOG("writing audio file header\n");
315 for (i
= 0; i
< fg
->h
.data_slices_per_group
; i
++) {
317 if (written
>= fg
->h
.audio_header_size
)
319 if (sb
+ written
> fg
->h
.audio_header_size
)
320 n
= fg
->h
.audio_header_size
- written
;
321 btr_copy(fg
->data
[i
], n
, pfd
->btrp
, fn
->btrn
);
326 for (; i
< fg
->h
.data_slices_per_group
; i
++) {
328 if (n
+ written
> fg
->h
.group_bytes
)
329 n
= fg
->h
.group_bytes
- written
;
330 btr_copy(fg
->data
[i
], n
, pfd
->btrp
, fn
->btrn
);
337 * Read a fec header from a buffer.
339 * \param buf The buffer to write to.
340 * \param h The fec header to write.
342 static int read_fec_header(char *buf
, size_t len
, struct fec_header
*h
)
346 if (len
< FEC_HEADER_SIZE
)
348 magic
= read_u32(buf
);
349 if (magic
!= FEC_MAGIC
)
350 return -E_BAD_FEC_HEADER
;
351 h
->slices_per_group
= read_u8(buf
+ 4);
352 h
->data_slices_per_group
= read_u8(buf
+ 5);
353 h
->audio_header_size
= read_u32(buf
+ 6);
355 h
->group_num
= read_u32(buf
+ 10);
356 h
->group_bytes
= read_u32(buf
+ 14);
358 h
->slice_num
= read_u8(buf
+ 18);
359 h
->slice_bytes
= read_u16(buf
+ 20);
360 h
->bos
= read_u8(buf
+ 22);
361 h
->header_stream
= read_u8(buf
+ 23);
362 if (!memcmp(buf
, FEC_EOF_PACKET
, FEC_EOF_PACKET_LEN
))
363 return -E_FECDEC_EOF
;
364 // PARA_DEBUG_LOG("group %u, slize %u, slices per group: %u\n",
365 // h->group_num, h->slice_num, h->slices_per_group);
369 /* returns 1 if we used the buffer, 0 if we didn't, negative on errors */
370 static int dispatch_slice(char *buf
, size_t len
, struct fec_header
*h
,
371 struct filter_node
*fn
)
373 struct fecdec_group
*fg
;
375 struct private_fecdec_data
*pfd
= fn
->private_data
;
377 if (h
->slice_bytes
> len
) { /* can not use the thing, try to read more */
378 fn
->min_iqs
= h
->slice_bytes
+ FEC_HEADER_SIZE
;
381 ret
= get_group(h
, pfd
, &fg
);
384 if (!add_slice(buf
, fg
)) /* group already complete */
386 if (!group_complete(fg
))
388 /* this slice completed the group */
391 /* it's either the first or the second complete group */
392 if (!pfd
->first_complete_group
) { /* it's the first group */
393 enum fec_group_usability u
= group_is_usable(fg
, pfd
);
394 assert(u
!= FEC_GROUP_USABLE_SKIP_HEADER
);
395 if (u
== FEC_GROUP_UNUSABLE
) /* forget it */
397 pfd
->first_complete_group
= fg
; /* remember it */
400 /* we have two complete groups, let's go */
401 k
= h
->data_slices_per_group
;
402 n
= h
->slices_per_group
;
403 PARA_NOTICE_LOG("init fec (%d, %d)\n", k
, n
);
404 ret
= fec_new(k
, n
, &pfd
->fec
);
407 pfd
->btrp
= btr_pool_new("fecdec", 128 * 1024);
408 /* decode and clear the first group */
409 ret
= decode_group(pfd
->first_complete_group
, fn
);
412 clear_group(pfd
->first_complete_group
);
413 pfd
->first_complete_group
= NULL
;
415 ret
= decode_group(fg
, fn
);
421 static void fecdec_close(struct filter_node
*fn
)
423 struct private_fecdec_data
*pfd
= fn
->private_data
;
424 struct fecdec_group
*fg
;
426 FOR_EACH_FECDEC_GROUP(fg
, pfd
)
429 btr_pool_free(pfd
->btrp
);
430 free(fn
->private_data
);
431 fn
->private_data
= NULL
;
434 static int fecdec_post_select(__a_unused
struct sched
*s
, void *context
)
436 struct filter_node
*fn
= context
;
437 struct btr_node
*btrn
= fn
->btrn
;
444 ret
= btr_node_status(btrn
, fn
->min_iqs
, BTR_NT_INTERNAL
);
447 btr_merge(btrn
, fn
->min_iqs
);
448 len
= btr_next_buffer(btrn
, &buf
);
449 ret
= read_fec_header(buf
, len
, &h
);
452 ret
= -E_BAD_SLICE_SIZE
;
455 ret
= -E_BAD_SLICE_NUM
;
456 if (h
.slice_num
> h
.slices_per_group
)
458 ret
= dispatch_slice(buf
+ FEC_HEADER_SIZE
, len
- FEC_HEADER_SIZE
,
460 //PARA_INFO_LOG("ret: %d, len: %d, slice_bytes: %d\n", ret, len, h.slice_bytes);
463 btr_consume(btrn
, FEC_HEADER_SIZE
+ h
.slice_bytes
);
467 btr_remove_node(&fn
->btrn
);
471 static void fecdec_open(struct filter_node
*fn
)
473 struct private_fecdec_data
*pfd
;
474 pfd
= para_calloc(sizeof(*pfd
));
475 fn
->private_data
= pfd
;
476 fn
->min_iqs
= FEC_HEADER_SIZE
;
479 const struct filter lsg_filter_cmd_com_fecdec_user_data
= {
481 .pre_select
= generic_filter_pre_select
,
482 .post_select
= fecdec_post_select
,
483 .close
= fecdec_close
,