null_fec.c

Go to the documentation of this file.
00001 
00034 #include <stdlib.h>
00035 #include <stdio.h>
00036 #include <math.h>
00037 #include <memory.h>
00038 #include <assert.h>
00039 
00040 #include "null_fec.h"
00041 
00042 trans_block_t* null_fec_encode_src_block(char *data, unsigned long long len,
00043                                                                                  unsigned int sbn, unsigned short es_len) {
00044         
00045         trans_block_t *tr_block;                /* transport block struct */
00046         trans_unit_t *tr_unit;                  /* transport unit struct */
00047         unsigned int nb_of_units;               /* number of units */
00048         unsigned int i;                                 /* loop variables */
00049         unsigned long long data_left;
00050         char *ptr;                                              /* pointer to left data */
00051 
00052         data_left = len;
00053 
00054         nb_of_units = (unsigned int)ceil((double)(unsigned int)len / (double)es_len);
00055 
00056         tr_block = create_block();
00057 
00058         if(tr_block == NULL) {
00059                 return tr_block;
00060         }
00061 
00062         tr_unit = create_units(nb_of_units);
00063 
00064         if(tr_unit == NULL) {
00065                 free(tr_block);
00066                 return NULL;
00067         }
00068 
00069         ptr = data;
00070 
00071         tr_block->unit_list = tr_unit;
00072         tr_block->sbn = sbn;
00073         tr_block->n = nb_of_units;
00074         tr_block->k = nb_of_units;
00075                 
00076         for(i = 0; i < nb_of_units; i++) {
00077                 tr_unit->esi = i;
00078                 tr_unit->len = data_left < es_len ? (unsigned short)data_left : es_len; /*min(eslen, data_left);*/
00079 
00080                 /* Alloc memory for TU data */
00081                 if(!(tr_unit->data = (char*)calloc(tr_unit->len, sizeof(char)))) {
00082                         printf("Could not alloc memory for transport unit's data!\n");
00083                         
00084                         tr_unit = tr_block->unit_list;  
00085 
00086                         while(tr_unit != NULL) {
00087                                 free(tr_unit->data);
00088                                 tr_unit++;
00089                         }
00090         
00091                         free(tr_block->unit_list);
00092                         free(tr_block);
00093                         return NULL;
00094                 }
00095 
00096                 memcpy(tr_unit->data, ptr, tr_unit->len);
00097                 
00098                 ptr += tr_unit->len;
00099                 data_left -= tr_unit->len;
00100                 tr_unit++;
00101         }
00102 
00103         return tr_block;
00104 }
00105 
00106 char *null_fec_decode_src_block(trans_block_t *tr_block, unsigned long long *block_len,
00107                                                                 unsigned short eslen) {
00108 
00109         char *buf = NULL; /* buffer where to construct the source block from data units */
00110 
00111     trans_unit_t *next_tu;
00112     trans_unit_t *tu;
00113 
00114         unsigned long long len;
00115         unsigned long long tmp;
00116 
00117     len = eslen*tr_block->k;
00118 
00119     /* Allocate memory for buf */
00120     if(!(buf = (char*)calloc((unsigned int)(len + 1), sizeof(char)))) {
00121         printf("Could not alloc memory for buf!\n");
00122         return NULL;
00123     }
00124 
00125     tmp = 0;
00126         
00127         next_tu = tr_block->unit_list;
00128                 
00129         while(next_tu != NULL) {
00130 
00131         tu = next_tu;
00132 
00133                 /*if(tu->data == NULL) {
00134                         printf("SB: %i, esi: %i\n", tr_block->sbn, tu->esi);
00135                         fflush(stdout);
00136                 }*/
00137 
00138         memcpy((buf+(unsigned int)tmp), tu->data, tu->len);
00139 
00140 #ifndef USE_RETRIEVE_UNIT
00141         free(tu->data);
00142         tu->data = NULL;
00143 #endif
00144 
00145         tmp += tu->len;
00146 
00147         next_tu = tu->next;
00148         }
00149 
00150         *block_len = len;
00151 
00152         return buf;
00153 }
00154 
00155 char *null_fec_decode_object(trans_obj_t *to, unsigned long long *data_len,
00156                                                          alc_session_t *s) {
00157         
00158         char *object = NULL;
00159         char *block = NULL;
00160 
00161         trans_block_t *tb;
00162 
00163         unsigned long long to_data_left;
00164         unsigned long long len;
00165         unsigned long long block_len;
00166         unsigned long long position;
00167         unsigned int i;
00168         
00169         /* Allocate memory for buf */
00170         if(!(object = (char*)calloc((unsigned int)(to->len+1), sizeof(char)))) {
00171                 printf("Could not alloc memory for buf!\n");
00172                 *data_len = 0;
00173                 return NULL;
00174         }
00175         
00176         to_data_left = to->len;
00177 
00178         tb = to->block_list;
00179         position = 0;
00180         
00181         for(i = 0; i < to->bs->N; i++) {
00182                 block = null_fec_decode_src_block(tb, &block_len, (unsigned short)to->es_len);
00183 
00184                 /* the last packet of the last source block might be padded with zeros */
00185                 len = to_data_left < block_len ? to_data_left : block_len;
00186 
00187                 assert (0 <= position);
00188                 assert (position < to->len+1);
00189                 assert (len <= (to->len-position));
00190 
00191                 memcpy(object+(unsigned int)position, block, (unsigned int)len);
00192                 position += len;
00193                 to_data_left -= len;
00194 
00195                 free(block);
00196                 tb = to->block_list+(i+1);
00197         }
00198         
00199         *data_len = to->len;
00200 
00201         return object;
00202 }

Generated on Fri Mar 9 20:08:51 2007 for MAD-FCL by  doxygen 1.5.0