📄 ugst-utl.c
字号:
char sync;{ unsigned short tmp, *bs; long n,j,k; /* * ......... INITIALIZATIONS ......... */ /* Calculate size of bitstream */ n = (bs_len - (sync==1?2:0))/resol; /* Initialize unsigned pointer to input (potentially signed) and output buffers */ bs = (unsigned short *)bit_stm; /* Check if number of softbits in frame is consistent with the resolution */ if (n*resol + (sync==1?2:0) != bs_len) { /* The bitstream does not have an integer number of parallel samples of width resol ... */ /* ... set all samples in bitstream to -1 */ memset(par_buf, -1, n * sizeof(short)); /* ... and return error code */ return(-1); } else { /* No problems - reset samples in parallel sample buffer */ memset(par_buf, 0, n * sizeof(short)); }/* * ......... PROCEEDS NORMAL PROCESSING ......... */ /* Convert every softbit in serial buffer to a parallel sample format */ for (j=0; j<n; j++) { /* If bad frame indicator present, no valid samples are returned. The output buffer will contain just zero samples */ if (*bs == BAD_FRAME) return(0l); /* Skip sync word if present */ if (*bs == SYNC_WORD) { /* Skip sync word */ bs++; /* Check frame length for consistency */ if (*bs++ != (bs_len-2)) return(-bs_len); } /* Get 1st bit ... */ tmp = (unsigned short)(*bs++==EID_ONE)?1:0; /* Parallelize all the other bits ... */ for (k=1; k<resol; k++) { tmp += (unsigned short)( ( (*bs++) == EID_ONE ? 1 : 0) << k); } /* Save word as short */ par_buf[j] = (short)tmp; } /* * ......... RETURN ......... */ return((long)n);}#undef BAD_FRAME#undef EID_ONE#undef EID_ZERO#undef SYNC_WORD/* .............. End of parallelize_right_justifiedstl96() .............. */ /* ============================================================================ long serialize_left_justifiedstl96 (short *par_buf, ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ short *bit_stm, long n, long resol, char sync); Description: ~~~~~~~~~~~~ Routine to convert a frame of `n' left-justified samples with a resolution `resol' into a bitstream of length n*resol. Input data is converted first from left-adjusted to right-adjusted (both in 2's complement), according to the resolution specified, and then converted into a bitstream vector. The bitstream is a sequence of 16-bit words whose least significant bits are either EID_ONE or EID_ZERO (defined below), depending if the correspondent bit in the input word was 1 or 0. (Therefore, the bitstream 16-bit words are always right-justified!) If the G.192 Annex B bitstream format is to be used, a synchronism (SYNC_WORD) word will be included at frame boundaries, followed by a 16-bit word indicating the frame length, i.e., the number of softbits in the frame. In this case, the the length of the bit stream is increased to (n+2)*resol. The option of adding only the sync word, as implemented in the STL92, is no longer available with this function. ---------------------------------------------------------------- NOTE! Be sure that the memory reserved (allocated) for "bit_stm" is big enough to accomodate all the data: - with sync word + frame length: ...... (n+2)*resol - without sync word: .................. n*resol ---------------------------------------------------------------- Please note that the LEAST SIGNIFICANT BITS of the right-justified input words are serialized FIRST, such that the bitstream is a stream with less significant bits coming first! (Please see example in serialize_right_justified() above). The bitstream value `1' is defined by the softbit EID_ONE, `0' as the softbit EID_ZERO, and the sync word by SYNC_WORD: Hardbit '0' is represented as the softbit '0x007F' Hardbit '1' is represented as the softbit '0x0081' SYNC-word is represented as the word '0x6B21' # is represented in two-complement notation Parameters: ~~~~~~~~~~~ par_buf ... input buffer with left-adjusted samples to be serialized. bit_stm ... output buffer with bitstream. n ......... number of words in the input buffer, i.e., the number of parallel samples/frame. resol ..... resolution (number of bits) of the left-adjusted samples in par_buf. sync ...... flag to say whether the G.192 Annex B bitstream format (1) is to be used (i.e., a sync word and a bitstream length word are added at frame boundaries), or (0) not. Return value: ~~~~~~~~~~~~~ On success, returns the number of bits of the output bitstream (including sync word). Original author: ~~~~~~~~~~~~~~~~ Simao Ferraz de Campos Neto Comsat Laboratories Tel: +1-301-428-4516 22300 Comsat Drive Fax: +1-301-428-9287 Clarksburg MD 20871 - USA E-mail: simao@ctd.comsat.com History ~~~~~~~ 06.Mar.96 v1.0 Created based on the STL92 version, however using the bitstream definition in Annex B of G.192. <simao@ctd.comsat.com> ============================================================================*/#define EID_ZERO 0x007F#define EID_ONE 0x0081#define SYNC_WORD 0x6B21 long serialize_left_justifiedstl96 (par_buf, bit_stm, n, resol, sync) short *par_buf, *bit_stm; long n, resol; char sync;{ unsigned short tmp, *bs; long bs_length; long j,k,l; /* * ......... INITIALIZATIONS ......... */ /* Calculate number of softbits in bitstream frame*/ bs_length = n * resol; /* Initialize unsigned pointer to input (potentially signed) and output buffers */ bs = (unsigned short *)bit_stm; /* * ......... PROCEEDS NORMAL PROCESSING ......... */ /* Put a sync word at beginning of the frame, if requested */ if (sync) { /* Frame boundaries have the sync word and the number of softbits per frame */ *bs++ = SYNC_WORD; *bs++ = bs_length; } /* Convert samples from left- to right-justified in input buffer */ l = 16-resol; /* Convert every sample in parallel buffer into a bitstream, including a sync word if requested */ for (j=0; j<n; j++) { /* Convert input word to unsigned */ tmp = (unsigned short)(par_buf[j] >> l); /* Serialize all sample's bits ... */ for (k=0; k<resol; k++) { *bs++ = (tmp&0x0001)?EID_ONE:EID_ZERO; tmp >>= 1; } } /* * ......... RETURN bitstream length (include sync word) ......... */ return((long)((short *)bs-bit_stm)); }#undef EID_ONE#undef EID_ZERO#undef SYNC_WORD/* ............... End of serialize_left_justifiedstl96() ............... */ /*============================================================================ long parallelize_left_justifiedstl96 (short *bit_stm, short *par_buf, ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ long bs_len, long resol, char sync); Description: ~~~~~~~~~~~~ Routine to convert a bitstream of length `bs_len' into a frame of bs_len/resol(*) left-justified samples with a resolution `resol'. The bitstream is a sequence of 16-bit words whose least significant bits are either EID_ONE or EID_ZERO, depending if the correspondent bit in the originating word was 1 or 0. (Therefore, the bitstream 16-bit words are always right-justified!) Note that the bits that come first are less significant than the next ones, when referring to a same word. Then, when mounting a word from the bitstream, bits from the bitstream that comes first are allocated to lower significant bits, as for the right-justified routines above (see example there). Output data is converted from right-adjusted to left-adjusted (both in 2's complement) before returning, according to the resolution specified. (*)PS: if there are sync words in the bitstream, the number of samples per frame is (bs_len-2)/resol. If the G.192 Annex B bitstream format is used (parameter sync==1), a synchronism (SYNC_WORD) word followed by a 16-bit word (indicating the number of softbits per frame) are present at frame boundaries in the input buffer. In this case, the sync and frame lengthwords are removed from the bitstream and not copied to the output buffer. Note that all parallel samples are supposed to have a constant number of bits for the whole frame. This means that, by construction, the number of softbits divided by the resolution must be an integer number, or (bs_len-2)%resol==0. If this does not happen, probably the serial bitstream was not generated by one of the serialize_...() routines, and cannot be parallelized by this function. As an example, this is the case of the RPE-LTP bitstream: the 260 bits of the encoded bitstream are not divided equally among the 76 parameters of the bitstream. In cases like this, the user must write its own parallelization function when the number of bits is not constant for all parallel samples in the frame. The bitstream value `1' is defined by the softbit EID_ONE, `0' as the softbit EID_ZERO, and the sync word by SYNC_WORD: Hardbit '0' is represented as the softbit '0x007F' Hardbit '1' is represented as the softbit '0x0081' SYNC-word is represented as the word '0x6B21' # is represented in two-complement notation ---------------------------------------------------------------- NOTE! This routine assumes that memory HAS already been allocated to BOTH the bit stream bit_stm and to the parallel samples' buffer par_buf. ALSO! This routine considers that the first `bit' of the bit- stream corresponds to the least significant bit of the original word! ---------------------------------------------------------------- Parameters: ~~~~~~~~~~~ bit_stm ... input buffer with bitstream to be parallelized. par_buf ... output buffer with left-adjusted samples. bs_len .... number of bits per frame (ie, size of input buffer, which includes sync/length words if sync==1). resol ..... resolution (number of bits) of the left-adjusted samples in par_buf. sync ...... flag to say whether the G.192 Annex B bitstream format (1) is to be used (i.e., a sync word and a bitstream length word are added at frame boundaries), or (0) not. Return value: ~~~~~~~~~~~~~ On success, returns the number of parallel words extracted from the input bitstream (the sync and legth words are removed and not copied to the output buffer). If the number of sofbits (plus sync and legth words) expected by the user (as specified in parameter bs_len) does not match with the bitstream length word (bit_stm[1]), a negative number (-bs_len[1]) is returned, and all samples in the output frame are set to 0. If the number of softbits in the frame is not an integer multiple of the number parallel samples, then -1 is returned (all samples in par_buf are also set to -1). If a bad-frame indicator (0x6B20) is present, no valid samples are returned and the output buffer will contain just zero samples. Original author: ~~~~~~~~~~~~~~~~ Simao Ferraz de Campos Neto Comsat Laboratories Tel: +1-301-428-4516 22300 Comsat Drive Fax: +1-301-428-9287 Clarksburg MD 20871 - USA E-mail: simao@ctd.comsat.com History ~~~~~~~ 06.Mar.96 v1.0 Created based on the STL92 version, however using the bitstream definition in Annex B of G.192. <simao@ctd.comsat.com> ============================================================================*/#define EID_ZERO 0x007F#define EID_ONE 0x0081#define SYNC_WORD 0x6B21#define BAD_FRAME 0x6B20 long parallelize_left_justifiedstl96 (bit_stm, par_buf, bs_len, resol, sync) short *bit_stm, *par_buf; long bs_len, resol; char sync;{ unsigned short tmp, *bs; long n,j,k; /* * ..
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -