📄 ugst-utl.c
字号:
/* Get 1st bit ... */ tmp = (unsigned)(*bs++==EID_ONE)?1:0; /* Parallelize all the other bits ... */ for (k=1; k<resol; k++) { tmp += (unsigned)( ( (*bs++) == EID_ONE ? 1 : 0) << k); } /* Sign extension is needed if last bit was a `1' ... */ if (*(bs-1) == EID_ONE) for (;k<16;k++) tmp += (1 << k); /* Save word as short */ par_buf[j] = (short)tmp; } /* * Convert samples from right- to left-justified in output buffer * (only needed if resol is not 16!) */ if ((resol = 16-resol)!=0) for (j=0; j<n; j++) { /* Shift up */ k = (long)par_buf[j] << resol; /* Check for overflow and save back to output vector */ par_buf[j] = (k > 32767) ? 32767 : (k < -32768? (-32768) : (short)k ); } /* * ......... RETURN ......... */ return((long)n);}#undef EID_ONE#undef EID_ZERO#undef SYNC_WORD/* .............. End of parallelize_left_justifiedstl92() .............. */ /* ********************************************************************** *//* ********************************************************************** *//* ********************************************************************** *//* ********* THE FUNCTIONS BELOW ARE NEW/MODIFIED FOR THE STL96 ********* *//* ********************************************************************** *//* ********************************************************************** *//* ********************************************************************** *//* ============================================================================ long serialize_right_justifiedstl96 (short *par_buf, ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ short *bit_stm, long n, long resol, char sync); Description: ~~~~~~~~~~~~ Routine to convert a frame of `n' right-justified samples with a resolution `resol' into a bitstream of length n*resol. The bit- stream 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 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 shorts - without sync word: .................. n*resol shorts ---------------------------------------------------------------- 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 below). Example: ADPCM input paralell sequence, in blocks of 256 samples, for 10 blocks (note that for each frame an individual call to this function is needed!): Input contents: frame no.: | 1st | | 2nd ...| ... | last one | sample no.: 1 2 ... 256 257 ... (last one) sample: 1011,0101,...,1001 1111 ... 1010 Output: Word no. (in output bitstream array): 1 2 3 4 5 6 7 ... * 1027 ... ... x +----+-+-+-+-+-+-+ ... +-+-+-+-+----+-+-+-+-+-+ ... +-+-+-+-+ |Sync|#|1|1|0|1|1| ... |1|0|0|1|Sync|#|1|1|1|1| ... |0|1|0|1| +----+-+-+-+-+-+-+ ... +-+-+-+-+----+-+-+-+-+-+ ... +-+-+-+-+ # = 1024 (i.e., 256 samples/frame * 4 bits/sample = 1024 bits/frame) * = word no.1022 x = word no.10260 Here, 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 right-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 right-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 total number of softbits in the output bitstream, including the including sync word and frame length. If the value returned is 0, the number of converted samples is zero. 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_right_justifiedstl96 (par_buf, bit_stm, n, resol, sync) short *par_buf, *bit_stm; long n, resol; char sync;{ register unsigned short tmp, *bs; register unsigned short bs_length; long j,k; /* * ......... INITIALIZATIONS ......... */ /* Calculate size of softbits in bitstream */ 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 and length 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 every sample in parallel buffer into a bitstream, including a sync word if requested */ for (j=0; j<n; j++) { /* Convert input right-justified word to unsigned */ tmp = (unsigned short)par_buf[j]; /* 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_right_justifiedstl96() ............... */ /*============================================================================ long parallelize_right_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(*) right-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 the same word. Then, when mounting a word from the bitstream, bits from the bitstream that comes first are converted to lower significant bits (see example below). (*)PS: if the bitstream is compatible with G.192 Annex B, the number of parallel 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. Example: ADPCM input paralell sequence, in blocks of 256 samples, for 10 blocks (note that for each frame an individual call to this function is needed!): Input contents: Word no. (in input bitstream array): 1 2 3 4 5 6 7 ... * 1027 ... ... x +----+-+-+-+-+-+-+ ... +-+-+-+-+----+-+-+-+-+-+ ... +-+-+-+-+ |Sync|#|1|1|0|1|1| ... |1|0|0|1|Sync|#|1|1|1|1| ... |0|1|0|1| +----+-+-+-+-+-+-+ ... +-+-+-+-+----+-+-+-+-+-+ ... +-+-+-+-+ # = 1024 (i.e., 256 samples/frame * 4 bits/sample = 1024 bits/frame) * = word no.1022 x = word no.10260 Output: frame no.: | 1st | | 2nd ...| ... | last one | sample no.: 1 2 ... 256 257 ... (last one) sample: 1011,0101,...,1001 1111 ... 1010 Here, 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 right-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 right-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_right_justifiedstl96 (bit_stm, par_buf, bs_len, resol, sync) short *bit_stm, *par_buf; long bs_len, resol;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -