blob: bfaf84b8352c930da6a2ff7825d7134b80ad3c73 [file] [log] [blame]
rjw6c1fd8f2022-11-30 14:33:01 +08001/*****************************************************************************
2* Copyright Statement:
3* --------------------
4* This software is protected by Copyright and the information contained
5* herein is confidential. The software may not be copied and the information
6* contained herein may not be used or disclosed except with the written
7* permission of MediaTek Inc. (C) 2005
8*
9* BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
10* THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
11* RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
12* AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
13* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
14* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
15* NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
16* SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
17* SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
18* THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
19* NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
20* SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
21*
22* BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
23* LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
24* AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
25* OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
26* MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
27*
28* THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
29* WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
30* LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
31* RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
32* THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
33*
34*****************************************************************************/
35
36/*****************************************************************************
37 *
38 * Filename:
39 * ---------
40 * StreamRB.c
41 *
42 * Project:
43 * --------
44 * MOLY_sw
45 *
46 * Description:
47 * ------------
48 * StreamRB interface and driver, providing stream ring buffer utility
49 *
50 * Author:
51 * -------
52 * -------
53 *============================================================================
54 * HISTORY
55 * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
56 *------------------------------------------------------------------------------
57 * removed!
58 * removed!
59 * removed!
60 *
61 * removed!
62 * removed!
63 * removed!
64 *
65 *
66 *
67 *------------------------------------------------------------------------------
68 * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
69 *============================================================================
70 ****************************************************************************/
71
72/*****************************************************************************
73* C O M P I L E R F L A G S
74******************************************************************************
75*/
76
77/*****************************************************************************
78* E X T E R N A L R E F E R E N C E S
79******************************************************************************
80*/
81#include "kal_public_api.h"
82#include "string.h"
83
84#include "l1aud_common_def.h"
85#include "streamrb.h"
86/*****************************************************************************
87* C O N S T A N T S
88******************************************************************************
89*/
90
91/*****************************************************************************
92* D A T A T Y P E S
93******************************************************************************
94*/
95
96/*****************************************************************************
97* P U B L I C D A T A
98******************************************************************************
99*/
100
101/*****************************************************************************
102* P R I V A T E D A T A
103******************************************************************************
104*/
105
106/*****************************************************************************
107* M A C R O S
108******************************************************************************
109*/
110
111/*****************************************************************************
112* F U N C T I O N D E C L A R A T I O N S
113******************************************************************************
114*/
115
116/*****************************************************************************
117* F U N C T I O N S
118******************************************************************************
119*/
120
121
122/*
123 * Description
124 * ---------
125 * The function initializes the ring buffer instance.
126 *
127 * Syntax
128 * ---------
129 * bool StreamRB_Init( StreamRB *rb, uint8 *buffer, int32 buffer_size )
130 *
131 * where
132 * rb The ring buffer instance
133 * buffer Address of the memory allocated for the ring buffer
134 * buffer_size Size of the memory allocated for the ring buffer
135 *
136 * Return Value
137 * ---------
138 * true
139 * false
140 */
141bool StreamRB_Init( StreamRB *rb, uint8 *buffer, int32 buffer_size )
142{
143 ASSERT( rb!=NULL );
144
145 memset( buffer, 0, buffer_size*sizeof(uint8) );
146 rb->buffer = buffer;
147 rb->size = buffer_size;
148 rb->read = 0;
149 rb->write = 0;
150
151 return true;
152}
153
154
155/*
156 * Description
157 * ---------
158 * The function returns the available free space of the ring buffer instance.
159 *
160 * Syntax
161 * ---------
162 * int32 StreamRB_GetFreeSpace( StreamRB *rb )
163 *
164 * where
165 * rb The ring buffer instance
166 *
167 * Return Value
168 * ---------
169 * the free space in words
170 */
171int32 StreamRB_GetFreeSpace( StreamRB *rb )
172{
173 int32 count;
174
175 ASSERT( rb!=NULL );
176
177 count = rb->read - rb->write - 1;
178 if( 0>count )
179 count += rb->size;
180 return count;
181}
182
183
184/*
185 * Description
186 * ---------
187 * The function returns available data count of the ring buffer instance
188 *
189 * Syntax
190 * ---------
191 * int32 StreamRB_GetDataCount( StreamRB *rb )
192 *
193 * where
194 * rb The ring buffer instance
195 *
196 * Return Value
197 * ---------
198 * the data count in words
199 */
200int32 StreamRB_GetDataCount( StreamRB *rb )
201{
202 int32 count;
203
204 ASSERT( rb!=NULL );
205
206 count = rb->write - rb->read;
207 if( 0>count )
208 count += rb->size;
209 return count;
210}
211
212
213/*
214 * Description
215 * ---------
216 * The function copies data from a linear array to a ring buffer instance.
217 * The free space of the ring buffer instance shall be enough, or assertion will happen
218 *
219 * Syntax
220 * ---------
221 * void StreamRB_Write( StreamRB *dest_rb, uint8 *src_buffer, int32 size )
222 *
223 * where
224 * dest_rb The ring buffer instance
225 * src_buffer Address of the linear array
226 * size Size of copy
227 *
228 * Return Value
229 * ---------
230 * none
231 */
232void StreamRB_Write( StreamRB *dest_rb, uint8 *src_buffer, int32 size )
233{
234 uint8 *write_ptr;
235 int32 write_cnt;
236/// kal_prompt_trace(MOD_L1SP, "StreamRB_Write size=%d",size);
237 ASSERT( dest_rb!=NULL );
238 ASSERT( StreamRB_GetFreeSpace( dest_rb )>=size );
239
240 StreamRB_GetWriteBlock( dest_rb, &write_ptr, &write_cnt );
241 if( write_cnt>size ) write_cnt = size;
242
243 if( src_buffer == (uint8 *)NULL ){
244 memset( write_ptr, 0, write_cnt*sizeof(uint8) );
245/// kal_prompt_trace(MOD_L1SP, "StreamRB_Write Null");
246 }else{
247 memcpy( write_ptr, src_buffer, write_cnt*sizeof(uint8) );
248/// kal_prompt_trace(MOD_L1SP, "StreamRB_Write %x %x %x %x", write_ptr, *(src_buffer+0),*(src_buffer+1),*(src_buffer+2));
249 }
250
251 StreamRB_ShiftWritePointer( dest_rb, write_cnt );
252 size -= write_cnt;
253
254 if( size>0 )
255 {
256 if( src_buffer != (uint8 *)NULL )
257 {
258 src_buffer += write_cnt;
259 }
260
261 StreamRB_GetWriteBlock( dest_rb, &write_ptr, &write_cnt );
262 if( write_cnt>size )
263 {
264 write_cnt = size;
265 }
266
267 if( src_buffer == (uint8 *)NULL )
268 {
269 memset( write_ptr, 0, write_cnt*sizeof(uint8) );
270 }
271 else
272 {
273 memcpy( write_ptr, src_buffer, write_cnt*sizeof(uint8) );
274 }
275
276 StreamRB_ShiftWritePointer( dest_rb, write_cnt );
277 }
278}
279
280
281/*
282 * Description
283 * ---------
284 * The function copies data from a ring buffer instance to a linear array
285 * The data count of the ring buffer instance shall be enough, or assertion will happen
286 *
287 * Syntax
288 * ---------
289 * void StreamRB_Read( StreamRB *src_rb, uint8 *dest_buffer, int32 size )
290 *
291 * where
292 * src_rb The ring buffer instance
293 * dest_buffer Address of the linear array
294 * size Size of copy
295 *
296 * Return Value
297 * ---------
298 * none
299 */
300void StreamRB_Read( StreamRB *src_rb, uint8 *dest_buffer, int32 size )
301{
302 uint8 *read_ptr;
303 int32 read_cnt;
304/// kal_prompt_trace(MOD_L1SP, "StreamRB_Read size=%d",size);
305 ASSERT( src_rb!=NULL );
306 ASSERT( StreamRB_GetDataCount( src_rb )>=size );
307
308 StreamRB_GetReadBlock( src_rb, &read_ptr, &read_cnt );
309 if( read_cnt>size )
310 {
311 read_cnt = size;
312 }
313 memcpy( dest_buffer, read_ptr, read_cnt*sizeof(uint8) );
314 StreamRB_ShiftReadPointer( src_rb, read_cnt );
315 size -= read_cnt;
316
317 if( size>0 )
318 {
319 dest_buffer += read_cnt;
320 StreamRB_GetReadBlock( src_rb, &read_ptr, &read_cnt );
321 if( read_cnt>size )
322 {
323 read_cnt = size;
324 }
325 memcpy( dest_buffer, read_ptr, read_cnt*sizeof(uint8) );
326 StreamRB_ShiftReadPointer( src_rb, read_cnt );
327 }
328}
329
330
331/*void StreamRB_ShiftReadPointer2( StreamRB *src_rb, int32 size )
332{
333 uint8 *read_ptr;
334 int32 read_cnt;
335
336 ASSERT( src_rb!=NULL );
337 ASSERT( StreamRB_GetDataCount( src_rb )>=size );
338
339 StreamRB_GetReadBlock( src_rb, &read_ptr, &read_cnt );
340 if( read_cnt>size )
341 {
342 read_cnt = size;
343 }
344 //memcpy( dest_buffer, read_ptr, read_cnt*sizeof(int16) );
345 StreamRB_ShiftReadPointer( src_rb, read_cnt );
346 size -= read_cnt;
347
348 if( size>0 )
349 {
350 //dest_buffer += read_cnt;
351 StreamRB_GetReadBlock( src_rb, &read_ptr, &read_cnt );
352 if( read_cnt>size )
353 {
354 read_cnt = size;
355 }
356 // memcpy( dest_buffer, read_ptr, read_cnt*sizeof(int16) );
357 StreamRB_ShiftReadPointer( src_rb, read_cnt );
358 }
359}*/
360
361
362/*
363 * Description
364 * ---------
365 * The function returns the linear write block and its size of a ring buffer instance
366 *
367 * Syntax
368 * ---------
369 * void StreamRB_GetWriteBlock( StreamRB *dest_rb, uint8 **write_ptr, int32 *write_cnt )
370 *
371 * where
372 * dest_rb The ring buffer instance
373 * write_ptr Address of the memory to put the address of the linear write block
374 * write_cnt Address of the memory to put the size of the linear write block
375 *
376 * Return Value
377 * ---------
378 * none
379 */
380void StreamRB_GetWriteBlock( StreamRB *dest_rb, uint8 **write_ptr, int32 *write_cnt )
381{
382 int32 cnt;
383
384 ASSERT( (dest_rb!=NULL) && (write_ptr!=NULL) && (write_cnt!=NULL) );
385
386 if( dest_rb->read>dest_rb->write )
387 cnt = dest_rb->read - dest_rb->write - 1;
388 else if( dest_rb->read==0 )
389 cnt = dest_rb->size - dest_rb->write - 1;
390 else
391 cnt = dest_rb->size - dest_rb->write;
392
393 *write_cnt = cnt;
394 *write_ptr = &dest_rb->buffer[dest_rb->write];
395}
396
397
398/*
399 * Description
400 * ---------
401 * The function returns the linear read block and its size of a ring buffer instance
402 *
403 * Syntax
404 * ---------
405 * void StreamRB_GetReadBlock( StreamRB *src_rb, uint8 **read_ptr, int32 *read_cnt )
406 *
407 * where
408 * src_rb The ring buffer instance
409 * read_ptr Address of the memory to put the address of the linear read block
410 * read_cnt Address of the memory to put the size of the linear read block
411 *
412 * Return Value
413 * ---------
414 * none
415 */
416void StreamRB_GetReadBlock( StreamRB *src_rb, uint8 **read_ptr, int32 *read_cnt )
417{
418 int32 cnt;
419
420 ASSERT( (src_rb!=NULL) && (read_ptr!=NULL) && (read_cnt!=NULL) );
421
422 if( src_rb->write == src_rb->read )
423 cnt = 0;
424 else if( src_rb->write>src_rb->read )
425 cnt = src_rb->write - src_rb->read;
426 else
427 cnt = src_rb->size - src_rb->read;
428
429 *read_cnt = cnt;
430 *read_ptr = &src_rb->buffer[src_rb->read];
431}
432
433
434/*
435 * Description
436 * ---------
437 * The function shifts the write pointer of a ring buffer instance in circular way
438 * Use this with StreamRB_GetWriteBlock
439 *
440 * Syntax
441 * ---------
442 * void StreamRB_ShiftWritePointer( StreamRB *rb, int32 shamt )
443 *
444 * where
445 * rb The ring buffer instance
446 * shamt shift amount
447 *
448 * Return Value
449 * ---------
450 * none
451 */
452void StreamRB_ShiftWritePointer( StreamRB *rb, int32 shamt )
453{
454 ASSERT( rb!=NULL );
455
456 rb->write += shamt;
457 if( rb->write>=rb->size )
458 rb->write -= rb->size;
459}
460
461
462/*
463 * Description
464 * ---------
465 * The function shifts the read pointer of a ring buffer instance in circular way
466 * Use this with StreamRB_GetReadBlock
467 *
468 * Syntax
469 * ---------
470 * void StreamRB_ShiftReadPointer( StreamRB *rb, int32 shamt )
471 *
472 * where
473 * rb The ring buffer instance
474 * shamt shift amount
475 *
476 * Return Value
477 * ---------
478 * none
479 */
480void StreamRB_ShiftReadPointer( StreamRB *rb, int32 shamt )
481{
482 ASSERT( rb!=NULL );
483
484 rb->read += shamt;
485 if( rb->read>=rb->size )
486 rb->read -= rb->size;
487}
488