source: svn/trunk/newcon3bcm2_21bu/magnum/commonutils/udp/budp_bitread.c @ 30

Last change on this file since 30 was 2, checked in by phkim, 11 years ago

1.phkim

  1. revision copy newcon3sk r27
  • Property svn:executable set to *
File size: 6.0 KB
Line 
1/***************************************************************************
2 *     Copyright (c) 2003-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: budp_bitread.c $
11 * $brcm_Revision: Hydra_Software_Devel/1 $
12 * $brcm_Date: 7/27/10 5:05p $
13 *
14 * Module Description:
15 *
16 * Revision History:
17 *
18 ***************************************************************************/
19
20#include "bstd.h"
21#include "berr.h"
22#include "budp.h"
23#include "budp_bitread.h"
24
25BDBG_MODULE(BUDP_Bitread);
26
27/***************************************************************************
28* Private data structures
29***************************************************************************/
30
31/***************************************************************************
32* Forward declarations of static (private) functions
33***************************************************************************/
34
35
36/***************************************************************************
37* Implementation of "BUDP_Bitread_" API functions
38***************************************************************************/
39
40
41/* Swap 4 byte unsigned integers */
42#define SWAP_U_INT_4(tni4)                         \
43 (((((tni4)>>24)&0xff  ) |   (((tni4)&0xff  )<<24) |   \
44   (((tni4)>>8 )&0xff00) |   (((tni4)&0xff00)<<8 ))) 
45/***************************************************************************
46 *
47 */
48BERR_Code BUDP_Bitread_Init ( 
49        BUDP_Bitread_Context* pContext, 
50        bool                     bByteswap,
51        void*                    userdata_start
52) 
53{
54   
55        uint32_t temp;
56        uint32_t address = (uint32_t)userdata_start;
57        uint32_t rem = address & 0x00000003;
58
59        BDBG_ENTER(BUDP_Bitread_Open);
60
61        if(!pContext)
62        {
63                BDBG_ERR(("Invalid parameter\n"));
64                BDBG_LEAVE(BUDP_Bitread_Open);
65                return BERR_TRACE(BERR_INVALID_PARAMETER);
66        }
67
68        /* Take care of misalignment */
69        address -= rem;
70
71        /* Initialize the context */
72        pContext->userdata  = (uint32_t*)address;  /* aligned on dword */
73    pContext->bByteswap = bByteswap;           /* set the Endianess */
74        temp = *pContext->userdata++;
75    if(pContext->bByteswap)
76        pContext->cache = SWAP_U_INT_4(temp);
77    else
78        pContext->cache = temp;
79
80    /* make sure alignment did not cause too much to be read */
81    pContext->bitsleft  = 8 * (4 - rem); 
82        pContext->cache <<= (8 * rem);
83
84        BDBG_LEAVE(BUDP_Bitread_Open);
85        return BERR_SUCCESS;
86}
87
88
89/***************************************************************************
90 *
91 */
92uint32_t BUDP_Bitread_Read (
93        BUDP_Bitread_Context* pContext,
94        unsigned int                nbits
95) 
96{
97        uint32_t result = 0;
98
99        BDBG_ENTER(BUDP_Bitread_Read);
100
101        /* check parameters */
102        BDBG_ASSERT (pContext != NULL);
103
104        /* NOT OPTIMIZED!  SLOW! */
105        while (nbits-- > 0)
106        {
107                if (pContext->bitsleft == 0)
108                {
109                        uint32_t temp = *pContext->userdata++;
110                        if (pContext->bByteswap)
111                pContext->cache = SWAP_U_INT_4(temp);
112            else
113                pContext->cache = temp;
114               
115                        pContext->bitsleft = 32;
116                }
117                result = (result << 1) | ((pContext->cache >> 31) & 0x1);
118                pContext->cache <<= 1;
119                --pContext->bitsleft;
120        } 
121        BDBG_LEAVE(BUDP_Bitread_Read);
122        return result;
123}
124
125/***************************************************************************
126 *
127 */
128uint32_t BUDP_Bitread_Byte (BUDP_Bitread_Context* pContext)
129
130{
131    uint32_t result = 0;
132
133    BDBG_ENTER(BUDP_Bitread_Byte);
134
135    /* check parameters */
136    BDBG_ASSERT (pContext != NULL);
137    if (pContext->bitsleft < 8)
138        {
139        result = BUDP_Bitread_Read (pContext, 8); 
140
141        }
142    else
143    {
144        result = (pContext->cache >> 24 ) & 0xff;
145        pContext->cache <<= 8;
146        pContext->bitsleft -= 8;
147    }
148   
149    BDBG_LEAVE(BUDP_Bitread_Byte);
150    return result;
151}
152
153/***************************************************************************
154 *
155 */
156uint32_t BUDP_Bitread_GetByteOffset(BUDP_Bitread_Context* pContext)
157
158{
159    uint32_t result;
160
161    BDBG_ENTER(BUDP_Bitread_Byte);
162
163    /* check parameters */
164    BDBG_ASSERT (pContext != NULL);
165
166        result = 
167                sizeof(pContext->userdata[0]) * 
168                        (pContext->userdata - pContext->userdata_start);
169        result += (32 - pContext->bitsleft) / 8;
170   
171    BDBG_LEAVE(BUDP_Bitread_Byte);
172    return result;
173}
174
175/***************************************************************************
176 *
177 */
178uint32_t BUDP_Bitread_next_start_code(
179        BUDP_Bitread_Context* pContext, size_t length, size_t* pBytesParsed)
180{
181        uint8_t saved[4];
182        bool found = false;
183        size_t bytesParsed = 0;
184
185        /*
186         * Go to first MPEG startcode
187         */
188
189        /* MPEG start codes are byte aligned, so make it so. */
190        if (pContext->bitsleft & 0x7)
191        {
192                pContext->bitsleft &= ~(0x7);
193                --length;
194                ++bytesParsed;
195        }
196
197        /* Special case: not enough data */
198        if (length < 4)
199        {
200                while (length-- > 0)
201                {
202                        (void)BUDP_Bitread_Byte (pContext);
203                        ++bytesParsed;
204                }
205                *pBytesParsed = bytesParsed;
206                return 0x0;
207        }
208
209        /* Initialize */
210        saved[0] = 0x0;
211    saved[1] = BUDP_Bitread_Byte (pContext);
212    saved[2] = BUDP_Bitread_Byte (pContext);
213    saved[3] = BUDP_Bitread_Byte (pContext);
214        length -= 3;
215        bytesParsed += 3;
216
217    while (length > 0)
218    {
219        /* Read in another byte */
220        saved[0] = saved[1];
221        saved[1] = saved[2];
222        saved[2] = saved[3];
223        saved[3] = BUDP_Bitread_Byte (pContext);
224                --length;
225                ++bytesParsed;
226
227        if ((saved[0] == 0x00) &&
228            (saved[1] == 0x00) &&
229            (saved[2] == 0x01)    )
230        {
231            /* Found it! */
232                        found = true;
233            break;
234        }
235    }
236
237        *pBytesParsed = bytesParsed;
238    if (found)
239    {
240        /* found the pattern before the end of stream */
241        return 
242                        ((uint32_t)saved[3] << 24) |
243                        ((uint32_t)saved[2] << 16) |
244                        ((uint32_t)saved[1] <<  8) |
245                        ((uint32_t)saved[0]      ) ;
246    }
247    else
248    {
249        /* Didn't find any start code */
250        return 0x0;
251    }
252
253}
254
255/* End of File */
Note: See TracBrowser for help on using the repository browser.