summaryrefslogtreecommitdiff
path: root/amadec/adec_write.c (plain)
blob: 0cad7fe6fad4b54efc0f1d15d97049a727c73473
1#include <adec_write.h>
2
3int init_buff(buffer_stream_t *bs, int length)
4{
5 //unsigned char *buffer=malloc(DEFAULT_BUFFER_SIZE);
6 unsigned char *buffer = malloc(length);
7 if (!buffer) {
8 printf("Err:malloc failed \n");
9 bs->data = NULL;
10 return -1;
11 }
12 bs->data = buffer;
13 //bs->buf_length=DEFAULT_BUFFER_SIZE;
14 bs->buf_length = length;
15 bs->buf_level = 0;
16 bs->rd_ptr = bs->wr_ptr = bs->data;
17 bs->bInited = 1;
18 bs->nMutex = 1;
19 pthread_mutex_init(&bs->nMutex1, NULL);
20 return 1;
21}
22
23int reset_buffer(buffer_stream_t *bs)
24{
25 if (bs->bInited == 0) {
26 return -1;
27 }
28 //bs->buf_length=DEFAULT_BUFFER_SIZE;
29 bs->buf_level = 0;
30 bs->rd_ptr = bs->wr_ptr = bs->data;
31 bs->nMutex = 1;
32 return 0;
33}
34
35int release_buffer(buffer_stream_t *bs)
36{
37 if (bs->data) {
38 free(bs->data);
39 }
40 free(bs);
41 bs = NULL;
42 return 1;
43}
44
45//1 empty 0 not empty -1 not inited
46int is_buffer_empty(buffer_stream_t *bs)
47{
48 if (bs->bInited == 0) {
49 return -1;
50 }
51 if (bs->buf_level == 0) {
52 return 1;
53 } else {
54 return 0;
55 }
56}
57//1 full 0 not full -1 not inited
58int is_buffer_full(buffer_stream_t *bs)
59{
60 if (bs->bInited == 0) {
61 return -1;
62 }
63 if (bs->buf_level == bs->buf_length) {
64 return 1;
65 } else {
66 return 0;
67 }
68
69}
70
71int get_buffer_length(buffer_stream_t *bs)
72{
73 if (bs->bInited == 0) {
74 return -1;
75 }
76 return bs->buf_level;
77}
78
79static int read_data(char * out, buffer_stream_t *bs, int size)
80{
81 if (bs->bInited == 0) {
82 return -1; //read failed
83 }
84 int ret = is_buffer_empty(bs);
85 if (ret == 1) {
86 //printf("=====buffer empty \n");
87 return 0;//buffer empty
88 }
89 int len = MIN(bs->buf_level, size);
90 if (bs->wr_ptr > bs->rd_ptr) {
91 memcpy(out, bs->rd_ptr, len);
92 bs->rd_ptr += len;
93 bs->buf_level -= len;
94 if (bs->rd_ptr == (bs->data + bs->buf_length)) {
95 bs->rd_ptr = bs->data;
96 }
97 //printf("=====read ok: condition 1 read :%d byte \n",len);
98 return len;
99 } else if (len < (bs->data + bs->buf_length - bs->rd_ptr)) {
100 memcpy(out, bs->rd_ptr, len);
101 bs->rd_ptr += len;
102 bs->buf_level -= len;
103 if (bs->rd_ptr == (bs->data + bs->buf_length)) {
104 bs->rd_ptr = bs->data;
105 }
106 //printf("=====read ok: condition 2 read :%d byte \n",len);
107 return len;
108
109 } else {
110 int tail_len = (bs->data + bs->buf_length - bs->rd_ptr);
111 memcpy(out, bs->rd_ptr, tail_len);
112 memcpy(out + tail_len, bs->data, len - tail_len);
113 bs->rd_ptr = bs->data + len - tail_len;
114 bs->buf_level -= len;
115 if (bs->rd_ptr == (bs->data + bs->buf_length)) {
116 bs->rd_ptr = bs->data;
117 }
118 //printf("=====read ok: condition 3 read :%d byte \n",len);
119 return len;
120 }
121
122}
123
124int read_pcm_buffer(char * out, buffer_stream_t *bs, int size)
125{
126 int ret = 0;
127 pthread_mutex_lock(&bs->nMutex1);
128 ret = read_data(out, bs, size);
129 pthread_mutex_unlock(&bs->nMutex1);
130 return ret;
131}
132static int write_data(char *in, buffer_stream_t *bs, int size)
133{
134 if (bs->bInited == 0) {
135 return -1; //not inited
136 }
137 int ret = is_buffer_full(bs);
138 if (ret == 1) {
139
140 //printf("=====buffer full \n");
141 return 0;//buffer full
142 }
143 //start write data
144 int len = MIN(bs->buf_length - bs->buf_level, size);
145 if (bs->wr_ptr < bs->rd_ptr) {
146 memcpy(bs->wr_ptr, in, len);
147 bs->wr_ptr += len;
148 bs->buf_level += len;
149 if (bs->wr_ptr == (bs->data + bs->buf_length)) {
150 bs->wr_ptr = bs->data;
151 }
152 //printf("=====write ok: condition 1 write :%d byte \n",len);
153 return len;
154 } else if (len < (bs->data + bs->buf_length - bs->wr_ptr)) {
155 memcpy(bs->wr_ptr, in, len);
156 bs->wr_ptr += len;
157 bs->buf_level += len;
158 if (bs->wr_ptr == (bs->data + bs->buf_length)) {
159 bs->wr_ptr = bs->data;
160 }
161 //printf("=====write ok: condition 2 write :%d byte \n",len);
162 return len;
163
164 } else {
165 int tail_len = (bs->data + bs->buf_length - bs->wr_ptr);
166 memcpy(bs->wr_ptr, in, tail_len);
167 memcpy(bs->data, in + tail_len, len - tail_len);
168 bs->wr_ptr = bs->data + len - tail_len;
169 bs->buf_level += len;
170 if (bs->wr_ptr == (bs->data + bs->buf_length)) {
171 bs->wr_ptr = bs->data;
172 }
173 //printf("=====write ok: condition 3 write :%d byte \n",len);
174 return len;
175 }
176
177}
178
179int write_pcm_buffer(char * in, buffer_stream_t *bs, int size)
180{
181 int ret = 0;
182 pthread_mutex_lock(&bs->nMutex1);
183 ret = write_data(in, bs, size);
184 pthread_mutex_unlock(&bs->nMutex1);
185 return ret;
186}
187
188
189
190