Rev 5021 | Rev 6106 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4438 | Serge | 1 | |
2 | #include |
||
3 | #include |
||
4 | #include "libswresample/swresample.h" |
||
5 | |||
6 | |||
7 | #include |
||
8 | #include "winlib/winlib.h" |
||
5021 | Serge | 9 | #include "sound.h" |
4438 | Serge | 10 | #include "fplay.h" |
11 | |||
12 | |||
13 | |||
14 | |||
15 | |||
16 | int resampler_size; |
||
17 | volatile int sound_level_0; |
||
18 | volatile int sound_level_1; |
||
19 | |||
20 | |||
21 | volatile enum player_state decoder_state; |
||
22 | volatile enum player_state sound_state; |
||
23 | |||
24 | |||
5603 | serge | 25 | |
4438 | Serge | 26 | |
27 | |||
28 | |||
29 | int sample_rate; |
||
30 | |||
31 | |||
32 | double audio_base = -1.0; |
||
33 | |||
34 | |||
35 | |||
36 | |||
37 | { |
||
38 | int err; |
||
39 | int version =-1; |
||
40 | char *errstr; |
||
41 | |||
42 | |||
43 | |||
44 | |||
45 | { |
||
46 | mutex_unlock(&driver_lock); |
||
47 | errstr = "Sound service not installed\n\r"; |
||
48 | goto exit_whith_error; |
||
49 | }; |
||
50 | |||
51 | |||
52 | |||
53 | |||
54 | |||
55 | |||
56 | (SOUND_VERSION<(version >> 16))) |
||
57 | { |
||
58 | errstr = "Sound service version mismatch\n\r"; |
||
59 | goto exit_whith_error; |
||
60 | } |
||
61 | |||
62 | |||
63 | |||
64 | |||
65 | |||
66 | |||
67 | |||
68 | |||
69 | |||
70 | |||
71 | return 0; |
||
72 | }; |
||
73 | |||
74 | |||
75 | { |
||
76 | SetVolume(hBuff, left, right); |
||
77 | }; |
||
78 | |||
79 | |||
80 | static double audio_delta; |
||
81 | static double last_time_stamp; |
||
82 | |||
83 | |||
84 | |||
85 | { |
||
86 | double tstamp; |
||
87 | |||
88 | |||
89 | return tstamp - audio_delta; |
||
90 | }; |
||
91 | |||
92 | |||
93 | { |
||
94 | static struct SwrContext *swr_ctx; |
||
95 | static int64_t src_layout; |
||
96 | static int src_freq; |
||
97 | static int src_channels; |
||
98 | static enum AVSampleFormat src_fmt = -1; |
||
99 | static AVFrame *aFrame; |
||
100 | |||
101 | |||
102 | AVPacket pkt_tmp; |
||
103 | int64_t dec_channel_layout; |
||
104 | int len, len2; |
||
105 | int got_frame; |
||
106 | int data_size; |
||
107 | |||
108 | |||
109 | |||
110 | return -1; |
||
111 | |||
112 | |||
113 | return 0; |
||
114 | |||
115 | |||
116 | |||
117 | |||
118 | { |
||
119 | if (!(aFrame = avcodec_alloc_frame())) |
||
120 | return -1; |
||
121 | } else |
||
122 | avcodec_get_frame_defaults(aFrame); |
||
123 | |||
124 | |||
125 | |||
126 | |||
127 | { |
||
128 | data_size = 192000; |
||
129 | |||
130 | |||
131 | // &data_size, &pkt_tmp); |
||
132 | got_frame = 0; |
||
133 | len = avcodec_decode_audio4(ctx, aFrame, &got_frame, &pkt_tmp); |
||
134 | |||
135 | |||
136 | { |
||
137 | char *samples; |
||
138 | int ch, plane_size; |
||
139 | int planar = av_sample_fmt_is_planar(ctx->sample_fmt); |
||
140 | int data_size = av_samples_get_buffer_size(&plane_size, ctx->channels, |
||
141 | aFrame->nb_samples, |
||
142 | ctx->sample_fmt, 1); |
||
143 | |||
144 | |||
145 | // { |
||
146 | // if (pkt.pts != AV_NOPTS_VALUE) |
||
147 | // audio_base = get_audio_base() * pkt.pts; |
||
148 | // printf("audio base %f\n", audio_base); |
||
149 | // }; |
||
150 | |||
151 | |||
152 | pkt_tmp.size -= len; |
||
153 | |||
154 | |||
155 | (aFrame->channel_layout && aFrame->channels == av_get_channel_layout_nb_channels(aFrame->channel_layout)) ? |
||
156 | aFrame->channel_layout : av_get_default_channel_layout(aFrame->channels); |
||
157 | |||
158 | |||
159 | dec_channel_layout != src_layout || |
||
160 | aFrame->sample_rate != src_freq || |
||
161 | !swr_ctx) |
||
162 | { |
||
163 | swr_free(&swr_ctx); |
||
164 | swr_ctx = swr_alloc_set_opts(NULL, AV_CH_LAYOUT_STEREO, AV_SAMPLE_FMT_S16, |
||
165 | aFrame->sample_rate, dec_channel_layout,aFrame->format, |
||
166 | aFrame->sample_rate, 0, NULL); |
||
167 | if (!swr_ctx || swr_init(swr_ctx) < 0) |
||
168 | { |
||
169 | printf("Cannot create sample rate converter for conversion of %d Hz %s %d channels to %d Hz %s %d channels!\n", |
||
170 | aFrame->sample_rate, av_get_sample_fmt_name(aFrame->format), (int)aFrame->channels, |
||
171 | aFrame->sample_rate, av_get_sample_fmt_name(AV_SAMPLE_FMT_S16), 2); |
||
172 | break; |
||
173 | } |
||
174 | |||
175 | |||
176 | src_channels = aFrame->channels; |
||
177 | src_freq = aFrame->sample_rate; |
||
178 | src_fmt = aFrame->format; |
||
179 | }; |
||
180 | |||
181 | |||
182 | { |
||
183 | const uint8_t **in = (const uint8_t **)aFrame->extended_data; |
||
184 | uint8_t *out[] = {decoder_buffer}; |
||
185 | int out_count = 192000 * 3 / 2 / av_get_bytes_per_sample(AV_SAMPLE_FMT_S16); |
||
186 | len2 = swr_convert(swr_ctx, out, out_count, in, aFrame->nb_samples); |
||
187 | if (len2 < 0) { |
||
188 | printf("swr_convert() failed\n"); |
||
189 | break; |
||
190 | } |
||
191 | if (len2 == out_count) { |
||
192 | printf("warning: audio buffer is probably too small\n"); |
||
193 | swr_init(swr_ctx); |
||
194 | } |
||
195 | data_size = len2 * 2 * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16); |
||
196 | |||
197 | |||
198 | |||
199 | |||
200 | |||
201 | |||
202 | /* |
||
203 | memcpy(samples, aFrame->extended_data[0], plane_size); |
||
204 | |||
205 | |||
206 | { |
||
207 | uint8_t *out = ((uint8_t *)samples) + plane_size; |
||
208 | for (ch = 1; ch < ctx->channels; ch++) |
||
209 | { |
||
210 | memcpy(out, aFrame->extended_data[ch], plane_size); |
||
211 | out += plane_size; |
||
212 | } |
||
213 | } |
||
214 | */ |
||
215 | astream.count += data_size; |
||
216 | mutex_unlock(&astream.lock); |
||
217 | }; |
||
218 | } |
||
219 | else pkt_tmp.size = 0; |
||
220 | } |
||
221 | av_free_packet(&pkt); |
||
222 | return 1; |
||
223 | }; |
||
224 | |||
225 | |||
226 | |||
227 | { |
||
228 | SND_EVENT evnt; |
||
229 | uint32_t offset; |
||
230 | double time_stamp; |
||
231 | |||
232 | |||
233 | |||
234 | |||
235 | { |
||
236 | GetNotify(&evnt); |
||
237 | |||
238 | |||
239 | { |
||
240 | printf("invalid event code %d\n\r", evnt.code); |
||
241 | continue; |
||
242 | } |
||
243 | |||
244 | |||
245 | { |
||
246 | printf("invalid stream %x hBuff= %x\n\r", |
||
247 | evnt.stream, hbuff); |
||
248 | continue; |
||
249 | } |
||
250 | |||
251 | |||
252 | audio_delta = time_stamp - last_time_stamp; |
||
253 | |||
254 | |||
255 | |||
256 | |||
257 | { |
||
258 | if(astream.count < buffsize) |
||
259 | { |
||
260 | memset(astream.buffer+astream.count, |
||
261 | 0, buffsize-astream.count); |
||
262 | astream.count = buffsize; |
||
263 | }; |
||
264 | |||
265 | |||
266 | samples_written+= buffsize/4; |
||
267 | |||
268 | |||
269 | if(astream.count) |
||
270 | memcpy(astream.buffer, astream.buffer+buffsize, astream.count); |
||
271 | mutex_unlock(&astream.lock); |
||
272 | }; |
||
273 | break; |
||
274 | }; |
||
275 | #endif |
||
276 | |||
277 | |||
278 | |||
279 | |||
280 | { |
||
281 | SND_EVENT evnt; |
||
282 | |||
283 | |||
284 | int samples; |
||
285 | int err; |
||
286 | char *errstr; |
||
287 | int active; |
||
288 | |||
289 | |||
290 | { |
||
291 | errstr = "Cannot create sound buffer\n\r"; |
||
292 | goto exit_whith_error; |
||
293 | }; |
||
294 | |||
295 | |||
5603 | serge | 296 | |
4438 | Serge | 297 | |
298 | { |
||
299 | errstr = "Cannot get buffer size\n\r"; |
||
300 | goto exit_whith_error; |
||
301 | }; |
||
302 | |||
303 | |||
5603 | serge | 304 | |
305 | |||
4438 | Serge | 306 | |
307 | |||
308 | |||
309 | |||
310 | { |
||
311 | uint32_t offset; |
||
312 | double event_stamp, wait_stamp; |
||
313 | int too_late = 0; |
||
314 | |||
315 | |||
316 | { |
||
317 | case PREPARE: |
||
318 | |||
319 | |||
320 | if(astream.count < buffsize*2) |
||
321 | { |
||
322 | memset(astream.buffer+astream.count, |
||
323 | 0, buffsize*2-astream.count); |
||
324 | astream.count = buffsize*2; |
||
325 | }; |
||
326 | |||
327 | |||
328 | astream.count -= buffsize*2; |
||
329 | if(astream.count) |
||
330 | memcpy(astream.buffer, astream.buffer+buffsize*2, astream.count); |
||
331 | mutex_unlock(&astream.lock); |
||
332 | |||
333 | |||
334 | |||
335 | |||
336 | GetTimeStamp(hBuff, &last_time_stamp); |
||
337 | // printf("last audio time stamp %f\n", last_time_stamp); |
||
338 | |||
339 | |||
340 | { |
||
341 | errstr = "Cannot play buffer\n\r"; |
||
342 | goto exit_whith_error; |
||
343 | }; |
||
344 | active = 1; |
||
345 | sync_audio(hBuff, buffsize); |
||
346 | sound_state = PLAY; |
||
347 | // printf("render: set audio latency to %f\n", audio_delta); |
||
348 | |||
349 | |||
350 | |||
351 | |||
352 | GetNotify(&evnt); |
||
353 | |||
354 | |||
355 | { |
||
356 | printf("invalid event code %d\n\r", evnt.code); |
||
357 | continue; |
||
358 | } |
||
359 | |||
360 | |||
361 | { |
||
362 | printf("invalid stream %x hBuff= %x\n\r", |
||
363 | evnt.stream, hBuff); |
||
364 | continue; |
||
365 | }; |
||
366 | |||
367 | |||
368 | |||
369 | |||
370 | if(astream.count < buffsize) |
||
371 | { |
||
372 | memset(astream.buffer+astream.count, |
||
373 | 0, buffsize-astream.count); |
||
374 | astream.count = buffsize; |
||
375 | }; |
||
376 | |||
377 | |||
378 | |||
379 | |||
380 | double val = 0; |
||
381 | int16_t *src = (int16_t*)astream.buffer; |
||
382 | int samples = buffsize/2; |
||
383 | int i; |
||
384 | |||
385 | |||
386 | if(val < abs(*src)) |
||
387 | val= abs(*src); // * *src; |
||
388 | |||
389 | |||
390 | |||
391 | |||
392 | if(val < abs(*src)) |
||
393 | val= abs(*src); // * *src; |
||
394 | |||
395 | |||
396 | |||
397 | |||
398 | }; |
||
399 | |||
400 | |||
401 | |||
402 | |||
403 | if(astream.count) |
||
404 | memcpy(astream.buffer, astream.buffer+buffsize, astream.count); |
||
405 | mutex_unlock(&astream.lock); |
||
406 | break; |
||
407 | |||
408 | |||
409 | if( active ) |
||
410 | { |
||
411 | ResetBuffer(hBuff, SND_RESET_ALL); |
||
412 | audio_base = -1.0; |
||
413 | active = 0; |
||
414 | } |
||
415 | sound_state = STOP; |
||
416 | break; |
||
417 | |||
418 | |||
419 | if( active ) |
||
420 | { |
||
421 | StopBuffer(hBuff); |
||
422 | }; |
||
423 | sound_state = PAUSE; |
||
424 | |||
425 | |||
426 | case STOP: |
||
427 | delay(1); |
||
428 | }; |
||
429 | } |
||
430 | |||
431 | |||
5603 | serge | 432 | |
433 | |||
4438 | Serge | 434 | DestroyBuffer(hBuff); |
435 | |||
436 | |||
437 | |||
438 | |||
439 | |||
440 | |||
441 | return -1; |
||
442 | |||
443 | |||
444 |