VHF deframer can now handle inverted bit streams from FMFSK
authorbaobrien <baobrien@01035d8c-6547-0410-b346-abe4f91aad63>
Tue, 16 Feb 2016 04:15:55 +0000 (04:15 +0000)
committerbaobrien <baobrien@01035d8c-6547-0410-b346-abe4f91aad63>
Tue, 16 Feb 2016 04:15:55 +0000 (04:15 +0000)
git-svn-id: https://svn.code.sf.net/p/freetel/code@2703 01035d8c-6547-0410-b346-abe4f91aad63

codec2-dev/src/freedv_api.c
codec2-dev/src/freedv_vhf_framing.c
codec2-dev/src/freedv_vhf_framing.h

index 90f335c58e9e8399f7b545f5809682b8fd09349e..29671bc7cc648630e23c330662cb21fd976fa184 100644 (file)
@@ -140,16 +140,17 @@ struct freedv *freedv_open(int mode) {
     }
     
     if ((mode == FREEDV_MODE_2400A) || (mode == FREEDV_MODE_2400B)){
-        /* Create the framer|deframer */
-        f->deframer = fvhff_create_deframer(FREEDV_VHF_FRAME_A);
-        if(f->deframer == NULL)
-            return NULL;
-        
+      
         /* Set up the C2 mode */
         codec2_mode = CODEC2_MODE_1300;
     }
     
     if (mode == FREEDV_MODE_2400A) {
+        /* Create the framer|deframer */
+        f->deframer = fvhff_create_deframer(FREEDV_VHF_FRAME_A,0);
+        if(f->deframer == NULL)
+            return NULL;
+  
         f->fsk = fsk_create_hbr(48000,1200,10,4,1200,1200);
         
         /* Note: fsk expects tx/rx bits as an array of uint8_ts, not ints */
@@ -170,6 +171,11 @@ struct freedv *freedv_open(int mode) {
     }
     
     if (mode == FREEDV_MODE_2400B) {
+        /* Create the framer|deframer */
+        f->deframer = fvhff_create_deframer(FREEDV_VHF_FRAME_A,1);
+        if(f->deframer == NULL)
+            return NULL;
+        
         f->fmfsk = fmfsk_create(48000,2400);
          
         if(f->fmfsk == NULL){
@@ -658,7 +664,6 @@ int freedv_floatrx(struct freedv *f, short speech_out[], float demod_in[]) {
             for(i=0;i<f->n_speech_samples;i++){
                 speech_out[i] = 0;
             }
-            f->sync = 0;
         }
         return f->n_speech_samples;
     }else { 
index d397f526cbbfd52baa7b1cb69a8ce22adae39eca..d4a9ced7cdc76b6662b220c870b7cf370ad30404 100644 (file)
@@ -115,32 +115,39 @@ void fvhff_frame_bits(  int frame_type,
 }
 
 /* Init and allocate memory for a freedv-vhf framer/deframer */
-struct freedv_vhf_deframer * fvhff_create_deframer(uint8_t frame_type){
+struct freedv_vhf_deframer * fvhff_create_deframer(uint8_t frame_type, int enable_bit_flip){
     struct freedv_vhf_deframer * deframer;
-    uint8_t * bits;
+    uint8_t *bits,*invbits;
     /* It's a Type A frame */
     if(frame_type == FREEDV_VHF_FRAME_A){
         /* Allocate memory for the thing */
         deframer = malloc(sizeof(struct freedv_vhf_deframer));
-        if(deframer == NULL){
+        if(deframer == NULL)
             return NULL;
+            
+        /* Allocate the not-bit buffer */
+        if(enable_bit_flip){
+            invbits = malloc(sizeof(uint8_t)*96);
+            if(invbits == NULL)
+                return NULL;
+        }else{
+            invbits = NULL;
         }
         
         /* Allocate the bit buffer */
         bits = malloc(sizeof(uint8_t)*96);
-        if(bits == NULL){
-            free(deframer);
+        if(bits == NULL)
             return NULL;
-        }
         
         deframer->bits = bits;
+        deframer->invbits = invbits;
         deframer->ftype = frame_type;
         deframer->state = ST_NOSYNC;
         deframer->bitptr = 0;
         deframer->last_uw = 0;
         deframer->miss_cnt = 0;
         deframer->frame_size = 96;
-        
+        deframer->on_inv_bits = 0;
         return deframer;
     }
     return NULL;
@@ -156,8 +163,7 @@ int fvhff_synchronized(struct freedv_vhf_deframer * def){
 }
 
 /* See if the UW is where it should be, to within a tolerance, in a bit buffer */
-static int fvhff_match_uw(struct freedv_vhf_deframer * def,int tol){
-    uint8_t * bits  = def->bits;
+static int fvhff_match_uw(struct freedv_vhf_deframer * def,uint8_t bits[],int tol){
     int frame_type  = def->ftype;
     int bitptr      = def->bitptr;
     int frame_size  = def->frame_size;
@@ -188,8 +194,7 @@ static int fvhff_match_uw(struct freedv_vhf_deframer * def,int tol){
     return diff <= tol;
 }
 
-static void fvhff_extract_frame(struct freedv_vhf_deframer * def,uint8_t codec2_out[],uint8_t proto_out[],uint8_t vc_out[]){
-    uint8_t * bits  = def->bits;
+static void fvhff_extract_frame(struct freedv_vhf_deframer * def,uint8_t bits[],uint8_t codec2_out[],uint8_t proto_out[],uint8_t vc_out[]){
     int frame_type  = def->ftype;
     int bitptr      = def->bitptr;
     int frame_size  = def->frame_size;
@@ -255,7 +260,10 @@ static void fvhff_extract_frame(struct freedv_vhf_deframer * def,uint8_t codec2_
  * Try to find the UW and extract codec/proto/vc bits in def->frame_size bits 
  */
 int fvhff_deframe_bits(struct freedv_vhf_deframer * def,uint8_t codec2_out[],uint8_t proto_out[],uint8_t vc_out[],uint8_t bits_in[]){
-    uint8_t * bits  = def->bits;
+    uint8_t * strbits  = def->bits;
+    uint8_t * invbits  = def->invbits;
+    uint8_t * bits;
+    int on_inv_bits = def->on_inv_bits;
     int frame_type  = def->ftype;
     int state       = def->state;
     int bitptr      = def->bitptr;
@@ -270,15 +278,16 @@ int fvhff_deframe_bits(struct freedv_vhf_deframer * def,uint8_t codec2_out[],uin
     
     /* Possibly set up frame-specific params here */
     if(frame_type == FREEDV_VHF_FRAME_A){
-        uw_first_tol = 2;   /* The UW bit-error tolerance for the first frame */
+        uw_first_tol = 1;   /* The UW bit-error tolerance for the first frame */
         uw_sync_tol = 1;    /* The UW bit error tolerance for frames after sync */
-        miss_tol = 2;       /* How many UWs may be missed before going into the de-synced state */
+        miss_tol = 5;       /* How many UWs may be missed before going into the de-synced state */
     }else{
         return 0;
     }
     for(i=0; i<frame_size; i++){
         /* Put a bit in the buffer */
-        bits[bitptr] = bits_in[i];
+        strbits[bitptr] = bits_in[i];
+        invbits[bitptr] = bits_in[i]?0:1;
         bitptr++;
         if(bitptr >= frame_size) bitptr = 0;
         def->bitptr = bitptr;
@@ -286,37 +295,58 @@ int fvhff_deframe_bits(struct freedv_vhf_deframer * def,uint8_t codec2_out[],uin
         if(state==ST_SYNC){
             /* Already synchronized, just wait till UW is back where it should be */
             last_uw++;
+            if(invbits!=NULL){
+                if(on_inv_bits)
+                    bits = invbits;
+                else
+                    bits = strbits;
+            }else{
+                bits=strbits;
+            }
             /* UW should be here. We're sunk, so deframe anyway */
             if(last_uw == frame_size){
                 last_uw = 0;
                 extracted_frame = 1;
                 
-                if(!fvhff_match_uw(def,uw_sync_tol))
+                if(!fvhff_match_uw(def,bits,uw_sync_tol))
                     miss_cnt++;
                 else
                     miss_cnt=0;
                 
                 /* If we go over the miss tolerance, go into no-sync */
-                if(miss_cnt>miss_tol)
+                if(miss_cnt>miss_tol){
                     state = ST_NOSYNC;
+                }
                 /* Extract the bits */
                 extracted_frame = 1;
-                fvhff_extract_frame(def,codec2_out,proto_out,vc_out);
+                fvhff_extract_frame(def,bits,codec2_out,proto_out,vc_out);
             }
         /* Not yet sunk */
         }else{
             /* It's a sync!*/
-            if(fvhff_match_uw(def,uw_first_tol)){
+            if(invbits!=NULL){
+                if(fvhff_match_uw(def,invbits,uw_first_tol)){
+                    state = ST_SYNC;
+                    last_uw = 0;
+                    miss_cnt = 0;
+                    extracted_frame = 1;
+                    on_inv_bits = 1;
+                    fvhff_extract_frame(def,invbits,codec2_out,proto_out,vc_out);
+                }
+            }
+            if(fvhff_match_uw(def,strbits,uw_first_tol)){
                 state = ST_SYNC;
                 last_uw = 0;
                 miss_cnt = 0;
                 extracted_frame = 1;
-                fvhff_extract_frame(def,codec2_out,proto_out,vc_out);
+                on_inv_bits = 0;
+                fvhff_extract_frame(def,strbits,codec2_out,proto_out,vc_out);
             }
         }
     }
     def->state = state;
     def->last_uw = last_uw;
     def->miss_cnt = miss_cnt;
+    def->on_inv_bits = on_inv_bits;
     return extracted_frame;
 }
index 6921b8c9acd761f2398ec8637483772688369539..c5ad034653067b7fa1447ab14f1709c81e224b91 100644 (file)
 
 /* Standard frame type */
 #define FREEDV_VHF_FRAME_A 1
-
 struct freedv_vhf_deframer {
     int ftype;          /* Type of frame to be looking for */
     int state;          /* State of deframer */
     uint8_t * bits;     /* Bits currently being decanted */
+    uint8_t * invbits;  /* Inversion of bits currently being decanted, for FMFSK */
+    
     int bitptr;         /* Pointer into circular bit buffer */
     int miss_cnt;       /* How many UWs have been missed */
     int last_uw;        /* How many bits since the last UW? */
     int frame_size;     /* How big is a frame? */
+    int on_inv_bits;    /* Are we using the inverted bits? */
 };
 
 /* Init and allocate memory for a freedv-vhf framer/deframer */
-struct freedv_vhf_deframer * fvhff_create_deframer(uint8_t frame_type);
+struct freedv_vhf_deframer * fvhff_create_deframer(uint8_t frame_type,int enable_bit_flip);
 
 /* Free the memory used by a freedv-vhf framer/deframer */
 void fvhff_destroy_deframer(struct freedv_vhf_deframer * def);