?? cvt-head.c
字號:
/* v1.0 18.Jan.99 ============================================================================ CVT-HEAD.C ~~~~~~~~~~ Description: ~~~~~~~~~~~~ Program that converts a serial bitstream from the old STL92 format back/to STL96 format. The STL92 format for BS was SYNC_FLAG followed by softbits. The STL96 uses the G.192 bistream format, in which a sync HEADER is used (a sync header is composed of a sync flag followed by the frame length). Initial suppositions: - input file has a header (either STL92 or STL96) Usage: cvt-head Options: -n ............. number of frames to process -start ......... first frame to start operation -frame ......... payload size in no. of softbits -nosync ........ don't use sync headers in output file -sync .......... use sync headers in output file [default] -nofer ......... revert any frame erasure flags (0x6B20) to 0x6B21 -q ............. quiet operation -help, -? ...... display help message Original author: ~~~~~~~~~~~~~~~~ Simao Ferraz de Campos Neto, Sr.MTS *All comments are strictly my own* COMSAT Laboratories Tel: +1-301-428-4516 22300 Comsat Drive Fax: +1-301-428-9287 Clarksburg MD 20871 - USA E-mail: simao.campos@comsat.com History ~~~~~~~ 18.Jan.99 v1.0 Created ============================================================================*//* ..... Generic include files ..... */#include <stdio.h> /* Standard I/O Definitions */#include <stdlib.h> /* for atoi(), atol() */#include <string.h> /* for strstr() */#include <math.h> /* for ceil() */#include "ugstdemo.h" /* general UGST definitions *//* ..... Specific include files ..... */#if defined(VMS) /* for checking file sizes */#include <stat.h>#else#include <sys/stat.h>#endif#if defined (unix) && !defined(MSDOS)/* ^^^^^^^^^^^^^^^^^^ This strange construction is necessary for DJGPP, because "unix" is defined, even it being MSDOS! */#if defined(__ALPHA)#include <unistd.h> /* for SEEK_... definitions used by fseek() */#else#include <sys/unistd.h> /* for SEEK_... definitions used by fseek() */#endif#endif/* ..... Module definition files ..... */#include "ugst-utl.h" /* format conversion functions *//* ..... Module definition files ..... */#include "softbit.h" /* Soft bit definitions and prototypes *//* ... Local function prototypes ... */char check_bs_format ARGS((FILE *F, char *file, char *type));int check_sync ARGS((FILE *F, char *file, char *bs_type, long *fr_len, char *bs_format));void display_usage ARGS((int level));/* ......... End of local function prototypes ......... */ /* ... Local defines, pseudo-functions ... */#define OVERHEAD_STL92 1 /* Overhead is sync word */#define OVERHEAD_STL96 2 /* Overhead is sync word and length word*//* Operating modes */enum STL_versions {raw, STL92, STL96};/* ************************* AUXILIARY FUNCTIONS ************************* *//* -------------------------------------------------------------------------- char check_bs_format (FILE *F, char *file, char *type); ~~~~~~~~~~~~~~~~~~~~ Function that checks the format (g192, byte, bit) in a given bitstream, and tries to guess the type of data (bit stream or frame erasure pattern) Parameter: ~~~~~~~~~~ F ...... FILE * structure to file to be checked file ... name of file to be checked type ... pointer to guessed data type in file: NO_HEADER for headerless BS HAS_HEADER for sync header (sync flag followed by frame length) HAS_FLAG_ONLY for BS with sync flag only, but no frame length) Returned value: ~~~~~~~~~~~~~~~ Returns the data format (g192, byte, bit) found in file. Original author: <simao.campos@comsat.com> ~~~~~~~~~~~~~~~~ History: ~~~~~~~~ 20.Aug.97 v.1.0 Created. 18.Jan.99 v.1.1 Added case for HAS_FLAG_ONLY <simao> -------------------------------------------------------------------------- */char check_bs_format(F, file, type)FILE *F;char *file;char *type;{ short word; char ret_val; /* Get a 16-bit word from the file */ fread(&word, sizeof(short), 1, F); /* Use some heuristics to determine what type of file is this */ switch((unsigned)word) { case 0x7F7F: case 0x7F81: case 0x817F: case 0x8181: /* Byte-oriented G.192 bitstream */ *type = NO_HEADER; ret_val = byte; break; case 0x2020: case 0x2021: case 0x2120: case 0x2121: /* Byte-oriented G.192 syncs */ *type = HAS_HEADER; ret_val = byte; break; case 0x007F: case 0x0081: /* G.192 bitstream in natural order */ *type = NO_HEADER; ret_val = g192; break; case 0x6B21: case 0x6B20: /* G.192 sync header in natural order */ *type = HAS_HEADER; ret_val = g192; break; case 0x7F00: case 0x8100: case 0x216B: case 0x206B: /* G.192 format that needs to be byte-swapped */ fprintf(stderr, "File %s needs to be byte-swapped! Aborted.\n", file); exit(8); default: /* Assuming it is compact bit mode */ *type = nil; /* Not possible to infer type for binary format! */ ret_val = compact; } /* Final check to see if the input bitstream is a byte-oriented G.192 bitstream. In this case, the first byte is 0x2n (n=0..F) and the second byte must be the frame length */ if ((((unsigned)word>>8) & 0xF0) == 0x20) { *type = HAS_HEADER; ret_val = byte; } /* Rewind file & and return format identifier */ fseek(F, 0l, SEEK_SET); return(ret_val);}/* ...................... End of check_bs_format() ...................... *//* ------------------------------------------------------------------------ int check_sync (FILE *F, char *file, char *bs_type, long *fr_len, *bs_format); Check that the bit stream has a synchronization flag and header, returning the length of the overhead. For headerless BS, it is 0, for STL96 is 2, for STL92 is 1. Parameter: ~~~~~~~~~~ F ...... FILE * structure to file to be checked file ... name of file to be checked bs_type .... pointer to type of bitstream (G192, BYTE, or COMPACT) fr_len ...... pointer to frame length, in number of (soft)bits bs_format ... pointer to guessed data type in file: - NO_HEADER for headerless BS - HAS_HEADER for sync header (sync flag followed by frame length) - HAS_FLAG_ONLY for BS with sync flag only, but no frame length) Returned value: ~~~~~~~~~~~~~~~ Returns whether a synchonization HEADER was found in the bitstream. Also changes the bs_type, fr_len, and bs_format input variables. The file pointer is rewinded to the beginning of the file. Original author: <simao.campos@comsat.com> ~~~~~~~~~~~~~~~~ History: ~~~~~~~~ 20.Aug.97 v.1.0 Created. 18.Jan.99 v.1.1 Added case for HAS_FLAG_ONLY <simao> -------------------------------------------------------------------------- */int check_sync(F, file, bs_type, fr_len, bs_format)FILE *F;char *file;char *bs_type, *bs_format;long *fr_len;{ long i; char sync_header, sync_flag; /* Initializes expecting a non-G.192-compliant BS */ sync_header = sync_flag = 0; /* Get info on bitstream */ *bs_format = check_bs_format(F, file, bs_type); /* If the BS seems to have a header, search for two consecutive ones. If it finds, determines which is the frame size */ if (*bs_type == HAS_HEADER) { /* It has a sync flag, but is it a full sync header? */ sync_flag = 1; /* The input BS may have a G.192 synchronization header - verify */ if (*bs_format == g192) { short tmp[2]; /* Get presumed first G.192 sync header */ fread(tmp, sizeof(short), 2, F); /* tmp[1] should have the frame length */ i = tmp[1]; /* advance file to the (presumed) next G.192 sync header */ fseek(F, (long)(tmp[1])*sizeof(short), SEEK_CUR); /* get (presumed) next G.192 sync header */ fread(tmp, sizeof(short), 2, F); /* Verify whether ... */ if (((tmp[0] & 0xF0) == 0x20) && (i == tmp[1])) { /* ... found a sync header */ *fr_len = i; sync_flag = 1; sync_header = 2; } else sync_header = 1; } else if (*bs_format == byte) { char tmp[2]; /* Get presumed first byte-wise G.192 sync header */ fread(tmp, sizeof(char), 2, F); /* tmp[1] should have the frame length */ i = tmp[1]; /* advance file to the (presumed) next byte-wise G.192 sync header */ fseek(F, (long)tmp[1], SEEK_CUR); /* get (presumed) next G.192 sync header */ fread(tmp, sizeof(char), 2, F); /* Verify */ if (((tmp[0] & 0xF0) == 0x20) && (i == tmp[1])) { *fr_len = i; sync_flag = 1; sync_header = 2; } else sync_header = 1; } else sync_header = sync_flag = 0; /* Rewind file */ fseek(F, 0l, SEEK_SET); } else /* BS type is COMPACT; return 0 */ return(0); /* Check the case when it has sync flag but not sync header */ if (sync_flag==sync_header) { int tmp = 0x20; long flen = -1; /* Search first occurence of LSB of sync flag in BS */ while ((getc(F)&0xF0)!=tmp && !feof(F)); /* Search next occurence of LSB of sync flag in BS */ while ((getc(F)&0xF0)!=tmp && !feof(F)) flen++; /* Increase counter */ /* Use the one found; use as no.of bytes read if BYTE of no.of words if G192 */ *fr_len = (*bs_format == g192)? flen/2: flen; *bs_type = HAS_FLAG_ONLY; /* Rewind file */ fseek(F, 0l, SEEK_SET); } /* Return info on whether or not a frame sync header was found */ return(sync_header);}/* -------------------------------------------------------------------------- display_usage() Shows program usage. History: ~~~~~~~~ 20/Aug/1997 v1.0 Created <simao> --------------------------------------------------------------------------*/#define P(x) printf xvoid display_usage(level)int level;{}#undef P/* .................. End of display_usage() ....................... */ /* ------------------------------------------------------------------------ Return a string with the version of bitstream ------------------------------------------------------------------------*/char *cvt_type(i)int i;{ switch(i) { case raw: return "Headerless"; break; case STL92:
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -