close Warning: Can't use blame annotator:
No changeset 2 in the repository

source: svn/newcon3bcm2_21bu/BSEAV/lib/utils/bvlc.c @ 46

Last change on this file since 46 was 46, checked in by megakiss, 11 years ago

459Mhz로 OTC 주파수 변경

  • Property svn:executable set to *
File size: 7.0 KB
RevLine 
1/***************************************************************************
2 *     Copyright (c) 2005-2010, Broadcom Corporation
3 *     All Rights Reserved
4 *     Confidential Property of Broadcom Corporation
5 *
6 *  THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE
7 *  AGREEMENT  BETWEEN THE USER AND BROADCOM.  YOU HAVE NO RIGHT TO USE OR
8 *  EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT.
9 *
10 * $brcm_Workfile: bvlc.c $
11 * $brcm_Revision: Hydra_Software_Devel/2 $
12 * $brcm_Date: 9/23/10 3:09p $
13 *
14 * Module Description: Converts startcode index to bcmplayer index
15 *
16 * Revision History:
17 *
18 * $brcm_Log: /magnum/commonutils/vlc/bvlc.c $
19 *
20 * Hydra_Software_Devel/2   9/23/10 3:09p jtna
21 * SW7405-4865: moved test code to rockford
22 *
23 * Hydra_Software_Devel/1   9/20/10 3:03p jtna
24 * SW7405-4865: added vlc
25 *
26 * $old_brcm_Workfile: bvlc.c $
27 * $old_brcm_Revision: 8 $
28 * $old_brcm_Date: 6/5/09 1:42p $
29 *
30 * $old_brcm_Log: /BSEAV/lib/utils/bvlc.c $
31 *
32 * 8   6/5/09 1:42p vsilyaev
33 * PR 55759: Fixed hanlding of rogue data when parsing VLC codes
34 *
35 * 7   3/6/09 11:21a vsilyaev
36 * PR 52903: Fixed use of initialized variable
37 *
38 * 6   9/1/05 3:36p erickson
39 * PR16964: added const to parameter
40 *
41 * 5   7/8/05 10:05a erickson
42 * PR16155: modified b_vlc_decode to require current_index instead of
43 * always starting from data[0]. This makes keeping track of where you
44 * are in the buffer much easier for the application. Also changed and
45 * expanded the examples.
46 *
47 * 4   4/4/05 1:28p erickson
48 * PR12728: fix warnings
49 *
50 * 3   3/23/05 9:02a erickson
51 * PR14451: use std debug interface
52 *
53 * 2   3/22/05 5:21p erickson
54 * PR14451: added optimizations
55 *
56 * 1   3/21/05 3:43p erickson
57 * PR14451: added generic vlc decode algorithm
58 *
59 ************************************************************/
60#include "bvlc.h"
61
62/* Magnum debug interface */
63#include "bstd.h"
64BDBG_MODULE(bvlc);
65
66/**
67Advance both bit and index by one through the byte array
68**/
69#define ADVANCE(bit, index, size) \
70        do {if (bit) {(bit)--;} else {(bit)=7;if(++(index)>=size) {goto err_eof;}} } while (0)
71
72/* set next_index and next_bit so any function call then would return EOF */
73static int BVLC_MarkEof(unsigned size, unsigned *next_index, unsigned *next_bit)
74{
75    *next_bit = 0;
76    if(size>0) {
77        *next_index = size-1;
78    } else {
79        *next_index = 0;
80    }
81    return -1;
82}
83
84static int BVLC_MeasurePrefix(const uint8_t *data,unsigned size,unsigned index,unsigned current_bit, 
85        unsigned *next_index,unsigned *next_bit,unsigned *prefix_length)
86{
87        int bit = current_bit;
88        int done = 0;
89
90    *next_bit = current_bit; /* always initialize result */
91        if (bit > 7) {
92                BDBG_ERR(("Invalid bit. Must be 0..7."));
93                return -1;
94        }
95       
96        *prefix_length = 0;
97       
98        /* count 0's to determine prefix_length */
99        for (;;) {
100                while (bit >= 0 && !done) {
101                        if (data[index] & (0x1 << bit))
102                                done = 1;
103                        else {
104                                (*prefix_length)++;
105                                bit--;
106                        }
107                }
108                if (done)  {
109                        break;
110        }
111        index++;
112        if(index<size) {
113                    bit = 7;
114        } else {
115            bit = 0;
116            if(size>0) {
117                index = size-1;
118            } else {
119                index = 0;
120            }
121            goto err_eof;
122        }
123        }
124        /* make sure we have enough data */
125        if (*prefix_length > (size * 8)/2-1) {
126                BDBG_ERR(("not enough data sent for this vlc value"));
127        goto err_eof;
128        }
129       
130        *next_index = index;
131        *next_bit = bit;
132        return 0;
133err_eof:
134    return BVLC_MarkEof(size, next_index, next_bit);
135}
136
137int BVLC_Skip(const uint8_t *data, unsigned size, unsigned current_index, unsigned current_bit, 
138        unsigned *next_index,unsigned *next_bit)
139{
140        unsigned prefix_length;
141       
142        if (BVLC_MeasurePrefix(data, size, current_index, current_bit, next_index, next_bit, &prefix_length)) {
143        goto err_eof;
144    }
145
146#if 1
147        /* Optimization: use integer math to advance by prefix_length + 1 */
148        prefix_length++; /* take into account the middle 1 */
149        if (prefix_length <= *next_bit)
150                *next_bit -= prefix_length;
151        else {
152        unsigned index;
153        prefix_length -= *next_bit + 1;
154        index = *next_index +  1 + prefix_length/8;
155        if(index<size) {
156            *next_index = index;
157            *next_bit = 7 - (prefix_length % 8);
158        } else {
159            goto err_eof;
160        }
161        }
162#else
163        /* advance one bit at a time */
164        /* skip the middle 1 */
165        ADVANCE(*next_bit, *next_index, size);
166       
167        /* skip the suffix - measure_prefix has already guaranteed we have enough data */
168        while (prefix_length--)
169                ADVANCE(*next_bit, *next_index, size);
170#endif
171               
172        return 0;
173err_eof:
174    return BVLC_MarkEof(size, next_index, next_bit);
175}
176
177int BVLC_Decode(const uint8_t *data,unsigned size,unsigned current_index,unsigned current_bit, 
178        unsigned *next_index,unsigned *next_bit)
179{
180        unsigned index;
181        int bit;
182        unsigned suffix_length, prefix_length;
183        unsigned value;
184        uint64_t suffix;
185               
186        BDBG_MSG(("vlc: %02x%02x %d, bit %d", data[current_index], (size>current_index+1)?data[current_index+1]:0, size, current_bit));
187
188        /* find the first 1, which determines prefix_length */
189        if (BVLC_MeasurePrefix(data, size, current_index, current_bit, &index, (unsigned*)&bit, &prefix_length)) {
190        goto err_eof;
191    }
192       
193        if (prefix_length > 64) {
194                BDBG_ERR(("vlc value too large for this algorithm"));
195                /* If you hit this, rework the algorithm. I'm assuming I can load the
196                entire suffix into a primitive datatype. */
197        goto err_eof;
198        }
199       
200        /* advance past middle 1 */
201        ADVANCE(bit, index, size);
202       
203        /* get suffix based on prefix_length */
204       
205        /* Optimization: no prefix, no suffix, value is 0 */
206        if (!prefix_length) {
207                value = 0;
208                BDBG_MSG(("  prefix len 0, done"));
209        }
210        else {
211                /* Optimization: if the suffix is completely contained in this byte, we
212                don't have to operate bitwise */
213                if (prefix_length <= 8 && (int)prefix_length <= bit+1) {
214                        static unsigned char mask[] = {0x0,0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xFF};
215                        suffix = (data[index] >> (bit-(prefix_length-1))) & mask[prefix_length];
216                        BDBG_MSG(("  prefix len %d, suffix (in byte) val %x", prefix_length, (int)suffix));
217                        bit -= prefix_length;
218                        if (bit == -1) {
219                                bit = 7;
220                                index++;
221                if(index>=size) { goto err_eof; }
222                        }
223                }
224                else {
225                        /* step through it bit by bit. this is the most general way and will
226                        work with any value. */
227                        suffix = 0;
228                        BDBG_MSG(("  prefix len %d, index %d bit %d", prefix_length, index, bit));
229                        for (suffix_length = 0; suffix_length < prefix_length; suffix_length++) {
230                                suffix <<= 1;
231                                if (data[index] & (0x1 << bit))
232                                        suffix |= 1;
233                                ADVANCE(bit, index, size);
234                        }
235                        BDBG_MSG(("  prefix len %d, suffix val %x", prefix_length, (int)suffix));
236                }
237       
238                /* now do the final calculation: 2^prefix_length - 1 + suffix */
239                value = 1;
240                while (prefix_length--) value *= 2;
241                value = value - 1 + suffix;
242        }
243
244        /* move to the next bit so that subsequent calls to b_vld_decode are easy
245        to make for adjacent values. */
246        if (next_index) *next_index = index;
247        if (next_bit) *next_bit = bit;
248               
249        BDBG_MSG(("  result = %d (%d, %d)", value, index, bit));
250        return value;
251err_eof:
252    return BVLC_MarkEof(size, next_index, next_bit);
253}
254
Note: See TracBrowser for help on using the repository browser.