blob: fbc7135e41cd99f688ca346bee9aa46bb6a1b569
1 | /* |
2 | * libmad - MPEG audio decoder library |
3 | * Copyright (C) 2000-2004 Underbit Technologies, Inc. |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation; either version 2 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License |
16 | * along with this program; if not, write to the Free Software |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 | * |
19 | * $Id: decoder.c,v 1.22 2004/01/23 09:41:32 rob Exp $ |
20 | */ |
21 | |
22 | #include<stdlib.h> |
23 | # include <stdio.h> |
24 | #include <string.h> |
25 | |
26 | # ifdef HAVE_CONFIG_H |
27 | # include "config.h" |
28 | # endif |
29 | |
30 | # include "global.h" |
31 | |
32 | # ifdef HAVE_SYS_TYPES_H |
33 | # include <sys/types.h> |
34 | # endif |
35 | |
36 | # ifdef HAVE_SYS_WAIT_H |
37 | # include <sys/wait.h> |
38 | # endif |
39 | |
40 | # ifdef HAVE_UNISTD_H |
41 | # include <unistd.h> |
42 | # endif |
43 | |
44 | # ifdef HAVE_FCNTL_H |
45 | # include <fcntl.h> |
46 | # endif |
47 | |
48 | # include <stdlib.h> |
49 | |
50 | # ifdef HAVE_ERRNO_H |
51 | # include <errno.h> |
52 | # endif |
53 | |
54 | # include "stream.h" |
55 | # include "frame.h" |
56 | # include "synth.h" |
57 | # include "decoder.h" |
58 | |
59 | #ifndef _WIN32 |
60 | #include "../../amadec/adec-armdec-mgt.h" |
61 | #include <android/log.h> |
62 | #define LOG_TAG "MadDecoder" |
63 | #define audio_codec_print(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__) |
64 | #else |
65 | #define audio_codec_print printf |
66 | #endif |
67 | |
68 | #define MUTE_S 0.08 |
69 | |
70 | |
71 | struct mad_decoder decoder; |
72 | char *pcm_out_data; |
73 | int *pcm_out_len; |
74 | enum mad_flow(*error_func)(void *, struct mad_stream *, struct mad_frame *); |
75 | void *error_data; |
76 | int bad_last_frame = 0; |
77 | struct mad_stream *stream; |
78 | struct mad_frame *frame; |
79 | struct mad_synth *synth; |
80 | int result = 0; |
81 | static int last_sr = -1; |
82 | static int last_ch_num = -1; |
83 | |
84 | /* |
85 | * This is a private message structure. A generic pointer to this structure |
86 | * is passed to each of the callback functions. Put here any data you need |
87 | * to access from within the callbacks. |
88 | */ |
89 | |
90 | struct buffer { |
91 | unsigned char *start; |
92 | unsigned long length; |
93 | }; |
94 | |
95 | |
96 | enum tagtype { |
97 | TAGTYPE_NONE = 0, |
98 | TAGTYPE_ID3V1, |
99 | TAGTYPE_ID3V2, |
100 | TAGTYPE_ID3V2_FOOTER |
101 | }; |
102 | |
103 | unsigned long id3_parse_uint(char **ptr, unsigned int bytes) |
104 | { |
105 | unsigned long value = 0; |
106 | |
107 | switch (bytes) { |
108 | case 4: |
109 | value = (value << 8) | *(*ptr)++; |
110 | case 3: |
111 | value = (value << 8) | *(*ptr)++; |
112 | case 2: |
113 | value = (value << 8) | *(*ptr)++; |
114 | case 1: |
115 | value = (value << 8) | *(*ptr)++; |
116 | } |
117 | |
118 | return value; |
119 | } |
120 | |
121 | unsigned long id3_parse_syncsafe(char **ptr, unsigned int bytes) |
122 | { |
123 | unsigned long value = 0; |
124 | |
125 | switch (bytes) { |
126 | case 5: |
127 | value = (value << 4) | (*(*ptr)++ & 0x0f); |
128 | case 4: |
129 | value = (value << 7) | (*(*ptr)++ & 0x7f); |
130 | value = (value << 7) | (*(*ptr)++ & 0x7f); |
131 | value = (value << 7) | (*(*ptr)++ & 0x7f); |
132 | value = (value << 7) | (*(*ptr)++ & 0x7f); |
133 | } |
134 | |
135 | return value; |
136 | } |
137 | static |
138 | void parse_header(char **ptr, |
139 | unsigned int *version, int *flags, int *size) |
140 | { |
141 | *ptr += 3; |
142 | |
143 | *version = id3_parse_uint(ptr, 2); |
144 | *flags = id3_parse_uint(ptr, 1); |
145 | *size = id3_parse_syncsafe(ptr, 4); |
146 | } |
147 | |
148 | enum tagtype tagtype(char *data, int length) |
149 | { |
150 | if (length >= 3 && |
151 | data[0] == 'T' && data[1] == 'A' && data[2] == 'G') { |
152 | return TAGTYPE_ID3V1; |
153 | } |
154 | |
155 | if (length >= 10 && |
156 | ((data[0] == 'I' && data[1] == 'D' && data[2] == '3') || |
157 | (data[0] == '3' && data[1] == 'D' && data[2] == 'I')) && |
158 | data[3] < 0xff && data[4] < 0xff && |
159 | data[6] < 0x80 && data[7] < 0x80 && data[8] < 0x80 && data[9] < 0x80) { |
160 | return data[0] == 'I' ? TAGTYPE_ID3V2 : TAGTYPE_ID3V2_FOOTER; |
161 | } |
162 | |
163 | return TAGTYPE_NONE; |
164 | } |
165 | |
166 | int id3_tag_query(char *data, int length) |
167 | { |
168 | unsigned int version; |
169 | int flags; |
170 | int size; |
171 | |
172 | switch (tagtype(data, length)) { |
173 | case TAGTYPE_ID3V1: |
174 | return 128; |
175 | |
176 | case TAGTYPE_ID3V2: |
177 | parse_header(&data, &version, &flags, &size); |
178 | |
179 | if (flags) { |
180 | size += 10; |
181 | } |
182 | |
183 | return 10 + size; |
184 | |
185 | case TAGTYPE_ID3V2_FOOTER: |
186 | parse_header(&data, &version, &flags, &size); |
187 | return -size - 10; |
188 | |
189 | case TAGTYPE_NONE: |
190 | break; |
191 | } |
192 | |
193 | return 0; |
194 | } |
195 | |
196 | |
197 | /* |
198 | * NAME: decoder->init() |
199 | * DESCRIPTION: initialize a decoder object with callback routines |
200 | */ |
201 | void mad_decoder_init(struct mad_decoder *decoder, void *data, |
202 | enum mad_flow(*input_func)(void *, |
203 | struct mad_stream *), |
204 | enum mad_flow(*header_func)(void *, |
205 | struct mad_header const *), |
206 | enum mad_flow(*filter_func)(void *, |
207 | struct mad_stream const *, |
208 | struct mad_frame *), |
209 | enum mad_flow(*output_func)(void *, |
210 | struct mad_header const *, |
211 | struct mad_pcm *), |
212 | enum mad_flow(*error_func)(void *, |
213 | struct mad_stream *, |
214 | struct mad_frame *), |
215 | enum mad_flow(*message_func)(void *, |
216 | void *, unsigned int *)) |
217 | { |
218 | decoder->mode = -1; |
219 | |
220 | decoder->options = 0; |
221 | |
222 | decoder->async.pid = 0; |
223 | decoder->async.in = -1; |
224 | decoder->async.out = -1; |
225 | |
226 | decoder->sync = 0; |
227 | |
228 | decoder->cb_data = 0;//data; |
229 | |
230 | decoder->input_func = input_func; |
231 | decoder->header_func = header_func; |
232 | decoder->filter_func = filter_func; |
233 | decoder->output_func = output_func; |
234 | decoder->error_func = error_func; |
235 | decoder->message_func = message_func; |
236 | } |
237 | |
238 | int mad_decoder_finish(struct mad_decoder *decoder) |
239 | { |
240 | # if defined(USE_ASYNC) |
241 | if (decoder->mode == MAD_DECODER_MODE_ASYNC && decoder->async.pid) { |
242 | pid_t pid; |
243 | int status; |
244 | |
245 | close(decoder->async.in); |
246 | |
247 | do { |
248 | pid = waitpid(decoder->async.pid, &status, 0); |
249 | } while (pid == -1 && errno == EINTR); |
250 | |
251 | decoder->mode = -1; |
252 | |
253 | close(decoder->async.out); |
254 | |
255 | decoder->async.pid = 0; |
256 | decoder->async.in = -1; |
257 | decoder->async.out = -1; |
258 | |
259 | if (pid == -1) { |
260 | return -1; |
261 | } |
262 | |
263 | return (!WIFEXITED(status) || WEXITSTATUS(status)) ? -1 : 0; |
264 | } |
265 | # endif |
266 | |
267 | return 0; |
268 | } |
269 | |
270 | # if defined(USE_ASYNC) |
271 | static |
272 | enum mad_flow send_io(int fd, void const *data, size_t len) |
273 | { |
274 | char const *ptr = data; |
275 | ssize_t count; |
276 | |
277 | while (len) { |
278 | do { |
279 | count = write(fd, ptr, len); |
280 | } while (count == -1 && errno == EINTR); |
281 | |
282 | if (count == -1) { |
283 | return MAD_FLOW_BREAK; |
284 | } |
285 | |
286 | len -= count; |
287 | ptr += count; |
288 | } |
289 | |
290 | return MAD_FLOW_CONTINUE; |
291 | } |
292 | |
293 | static |
294 | enum mad_flow receive_io(int fd, void *buffer, size_t len) |
295 | { |
296 | char *ptr = buffer; |
297 | ssize_t count; |
298 | |
299 | while (len) { |
300 | do { |
301 | count = read(fd, ptr, len); |
302 | } while (count == -1 && errno == EINTR); |
303 | |
304 | if (count == -1) { |
305 | return (errno == EAGAIN) ? MAD_FLOW_IGNORE : MAD_FLOW_BREAK; |
306 | } else if (count == 0) { |
307 | return MAD_FLOW_STOP; |
308 | } |
309 | |
310 | len -= count; |
311 | ptr += count; |
312 | } |
313 | |
314 | return MAD_FLOW_CONTINUE; |
315 | } |
316 | |
317 | static |
318 | enum mad_flow receive_io_blocking(int fd, void *buffer, size_t len) |
319 | { |
320 | int flags, blocking; |
321 | enum mad_flow result; |
322 | |
323 | flags = fcntl(fd, F_GETFL); |
324 | if (flags == -1) { |
325 | return MAD_FLOW_BREAK; |
326 | } |
327 | |
328 | blocking = flags & ~O_NONBLOCK; |
329 | |
330 | if (blocking != flags && |
331 | fcntl(fd, F_SETFL, blocking) == -1) { |
332 | return MAD_FLOW_BREAK; |
333 | } |
334 | |
335 | result = receive_io(fd, buffer, len); |
336 | |
337 | if (flags != blocking && |
338 | fcntl(fd, F_SETFL, flags) == -1) { |
339 | return MAD_FLOW_BREAK; |
340 | } |
341 | |
342 | return result; |
343 | } |
344 | |
345 | static |
346 | enum mad_flow send(int fd, void const *message, unsigned int size) |
347 | { |
348 | enum mad_flow result; |
349 | |
350 | /* send size */ |
351 | |
352 | result = send_io(fd, &size, sizeof(size)); |
353 | |
354 | /* send message */ |
355 | |
356 | if (result == MAD_FLOW_CONTINUE) { |
357 | result = send_io(fd, message, size); |
358 | } |
359 | |
360 | return result; |
361 | } |
362 | |
363 | static |
364 | enum mad_flow receive(int fd, void **message, unsigned int *size) |
365 | { |
366 | enum mad_flow result; |
367 | unsigned int actual; |
368 | |
369 | if (*message == 0) { |
370 | *size = 0; |
371 | } |
372 | |
373 | /* receive size */ |
374 | |
375 | result = receive_io(fd, &actual, sizeof(actual)); |
376 | |
377 | /* receive message */ |
378 | |
379 | if (result == MAD_FLOW_CONTINUE) { |
380 | if (actual > *size) { |
381 | actual -= *size; |
382 | } else { |
383 | *size = actual; |
384 | actual = 0; |
385 | } |
386 | |
387 | if (*size > 0) { |
388 | if (*message == 0) { |
389 | *message = malloc(*size); |
390 | if (*message == 0) { |
391 | return MAD_FLOW_BREAK; |
392 | } |
393 | } |
394 | |
395 | result = receive_io_blocking(fd, *message, *size); |
396 | } |
397 | |
398 | /* throw away remainder of message */ |
399 | |
400 | while (actual && result == MAD_FLOW_CONTINUE) { |
401 | char sink[256]; |
402 | unsigned int len; |
403 | |
404 | len = actual > sizeof(sink) ? sizeof(sink) : actual; |
405 | |
406 | result = receive_io_blocking(fd, sink, len); |
407 | |
408 | actual -= len; |
409 | } |
410 | } |
411 | |
412 | return result; |
413 | } |
414 | |
415 | static |
416 | enum mad_flow check_message(struct mad_decoder *decoder) |
417 | { |
418 | enum mad_flow result; |
419 | void *message = 0; |
420 | unsigned int size; |
421 | |
422 | result = receive(decoder->async.in, &message, &size); |
423 | |
424 | if (result == MAD_FLOW_CONTINUE) { |
425 | if (decoder->message_func == 0) { |
426 | size = 0; |
427 | } else { |
428 | result = decoder->message_func(decoder->cb_data, message, &size); |
429 | |
430 | if (result == MAD_FLOW_IGNORE || |
431 | result == MAD_FLOW_BREAK) { |
432 | size = 0; |
433 | } |
434 | } |
435 | |
436 | if (send(decoder->async.out, message, size) != MAD_FLOW_CONTINUE) { |
437 | result = MAD_FLOW_BREAK; |
438 | } |
439 | } |
440 | |
441 | if (message) { |
442 | free(message); |
443 | } |
444 | |
445 | return result; |
446 | } |
447 | # endif |
448 | |
449 | static |
450 | enum mad_flow error_default(void *data, struct mad_stream *stream, |
451 | struct mad_frame *frame) |
452 | { |
453 | int *bad_last_frame = data; |
454 | |
455 | switch (stream->error) { |
456 | case MAD_ERROR_BADCRC: |
457 | if (*bad_last_frame) { |
458 | mad_frame_mute(frame); |
459 | } else { |
460 | *bad_last_frame = 1; |
461 | } |
462 | |
463 | return MAD_FLOW_IGNORE; |
464 | |
465 | default: |
466 | return MAD_FLOW_CONTINUE; |
467 | } |
468 | } |
469 | |
470 | static |
471 | int run_sync(struct mad_decoder *decoder) |
472 | { |
473 | |
474 | /* |
475 | if (decoder->input_func == 0) |
476 | return 0; |
477 | |
478 | if (decoder->error_func) { |
479 | error_func = decoder->error_func; |
480 | //error_data = decoder->cb_data; |
481 | } |
482 | else { |
483 | error_func = error_default; |
484 | error_data = &bad_last_frame; |
485 | } |
486 | |
487 | stream = &decoder->sync->stream; |
488 | frame = &decoder->sync->frame; |
489 | synth = &decoder->sync->synth; |
490 | |
491 | mad_stream_init(stream); |
492 | mad_frame_init(frame); |
493 | mad_synth_init(synth); |
494 | |
495 | mad_stream_options(stream, decoder->options); |
496 | */ |
497 | do { |
498 | switch (decoder->input_func(decoder->cb_data, stream)) { |
499 | case MAD_FLOW_STOP: |
500 | goto done; |
501 | case MAD_FLOW_BREAK: |
502 | goto fail; |
503 | case MAD_FLOW_IGNORE: |
504 | continue; |
505 | case MAD_FLOW_CONTINUE: |
506 | break; |
507 | } |
508 | |
509 | while (1) { |
510 | # if defined(USE_ASYNC) |
511 | if (decoder->mode == MAD_DECODER_MODE_ASYNC) { |
512 | switch (check_message(decoder)) { |
513 | case MAD_FLOW_IGNORE: |
514 | case MAD_FLOW_CONTINUE: |
515 | break; |
516 | case MAD_FLOW_BREAK: |
517 | goto fail; |
518 | case MAD_FLOW_STOP: |
519 | goto done; |
520 | } |
521 | } |
522 | # endif |
523 | |
524 | if (decoder->header_func) { |
525 | if (mad_header_decode(&frame->header, stream) == -1) { |
526 | if (!MAD_RECOVERABLE(stream->error)) { |
527 | break; |
528 | } |
529 | |
530 | switch (error_func(decoder->cb_data, stream, frame)) { |
531 | case MAD_FLOW_STOP: |
532 | goto done; |
533 | case MAD_FLOW_BREAK: |
534 | goto fail; |
535 | case MAD_FLOW_IGNORE: |
536 | case MAD_FLOW_CONTINUE: |
537 | default: |
538 | continue; |
539 | } |
540 | } |
541 | |
542 | switch (decoder->header_func(decoder->cb_data, &frame->header)) { |
543 | case MAD_FLOW_STOP: |
544 | goto done; |
545 | case MAD_FLOW_BREAK: |
546 | goto fail; |
547 | case MAD_FLOW_IGNORE: |
548 | continue; |
549 | case MAD_FLOW_CONTINUE: |
550 | break; |
551 | } |
552 | } |
553 | |
554 | if (mad_frame_decode(frame, stream) == -1) { |
555 | if (!MAD_RECOVERABLE(stream->error)) { |
556 | break; |
557 | } |
558 | |
559 | switch (error_func(decoder->cb_data, stream, frame)) { |
560 | case MAD_FLOW_STOP: |
561 | goto done; |
562 | case MAD_FLOW_BREAK: |
563 | goto fail; |
564 | case MAD_FLOW_IGNORE: |
565 | // for this error,we should skip one bytes for another frame sync,otherwise no chance to cosume data again |
566 | if (stream->this_frame == stream->buffer) { |
567 | stream->this_frame = stream->buffer + 1; |
568 | } |
569 | audio_codec_print("[%s,%d] MAD_FLOW_IGNORE\n", __FUNCTION__, __LINE__); |
570 | goto fail; |
571 | break; |
572 | case MAD_FLOW_CONTINUE: |
573 | default: |
574 | continue; |
575 | } |
576 | } else { |
577 | bad_last_frame = 0; |
578 | } |
579 | |
580 | if (decoder->filter_func) { |
581 | switch (decoder->filter_func(decoder->cb_data, stream, frame)) { |
582 | case MAD_FLOW_STOP: |
583 | goto done; |
584 | case MAD_FLOW_BREAK: |
585 | goto fail; |
586 | case MAD_FLOW_IGNORE: |
587 | continue; |
588 | case MAD_FLOW_CONTINUE: |
589 | break; |
590 | } |
591 | } |
592 | |
593 | mad_synth_frame(synth, frame); |
594 | |
595 | if (decoder->output_func) { |
596 | switch (decoder->output_func(decoder->cb_data, |
597 | &frame->header, &synth->pcm)) { |
598 | case MAD_FLOW_STOP: |
599 | goto done; |
600 | case MAD_FLOW_BREAK: |
601 | goto fail; |
602 | case MAD_FLOW_IGNORE: |
603 | case MAD_FLOW_CONTINUE: |
604 | break; |
605 | } |
606 | } |
607 | } |
608 | } while (stream->error == MAD_ERROR_BUFLEN); |
609 | |
610 | fail: |
611 | result = -1; |
612 | |
613 | done: |
614 | /* |
615 | mad_synth_finish(synth); |
616 | mad_frame_finish(frame); |
617 | mad_stream_finish(stream); |
618 | */ |
619 | //return result; |
620 | return stream->this_frame - stream->buffer; |
621 | } |
622 | |
623 | # if defined(USE_ASYNC) |
624 | static |
625 | int run_async(struct mad_decoder *decoder) |
626 | { |
627 | pid_t pid; |
628 | int ptoc[2], ctop[2], flags; |
629 | |
630 | if (pipe(ptoc) == -1) { |
631 | return -1; |
632 | } |
633 | |
634 | if (pipe(ctop) == -1) { |
635 | close(ptoc[0]); |
636 | close(ptoc[1]); |
637 | return -1; |
638 | } |
639 | |
640 | flags = fcntl(ptoc[0], F_GETFL); |
641 | if (flags == -1 || |
642 | fcntl(ptoc[0], F_SETFL, flags | O_NONBLOCK) == -1) { |
643 | close(ctop[0]); |
644 | close(ctop[1]); |
645 | close(ptoc[0]); |
646 | close(ptoc[1]); |
647 | return -1; |
648 | } |
649 | |
650 | pid = fork(); |
651 | if (pid == -1) { |
652 | close(ctop[0]); |
653 | close(ctop[1]); |
654 | close(ptoc[0]); |
655 | close(ptoc[1]); |
656 | return -1; |
657 | } |
658 | |
659 | decoder->async.pid = pid; |
660 | |
661 | if (pid) { |
662 | /* parent */ |
663 | |
664 | close(ptoc[0]); |
665 | close(ctop[1]); |
666 | |
667 | decoder->async.in = ctop[0]; |
668 | decoder->async.out = ptoc[1]; |
669 | |
670 | return 0; |
671 | } |
672 | |
673 | /* child */ |
674 | |
675 | close(ptoc[1]); |
676 | close(ctop[0]); |
677 | |
678 | decoder->async.in = ptoc[0]; |
679 | decoder->async.out = ctop[1]; |
680 | |
681 | _exit(run_sync(decoder)); |
682 | |
683 | /* not reached */ |
684 | return -1; |
685 | } |
686 | # endif |
687 | |
688 | /* |
689 | * NAME: decoder->run() |
690 | * DESCRIPTION: run the decoder thread either synchronously or asynchronously |
691 | */ |
692 | int mad_decoder_run(struct mad_decoder *decoder, enum mad_decoder_mode mode) |
693 | { |
694 | int result; |
695 | int (*run)(struct mad_decoder *) = 0; |
696 | |
697 | switch (decoder->mode = mode) { |
698 | case MAD_DECODER_MODE_SYNC: |
699 | run = run_sync; |
700 | break; |
701 | |
702 | case MAD_DECODER_MODE_ASYNC: |
703 | # if defined(USE_ASYNC) |
704 | run = run_async; |
705 | # endif |
706 | break; |
707 | } |
708 | |
709 | if (run == 0) { |
710 | return -1; |
711 | } |
712 | |
713 | //decoder->sync = malloc(sizeof(*decoder->sync)); |
714 | if (decoder->sync == 0) { |
715 | return -1; |
716 | } |
717 | |
718 | result = run(decoder); |
719 | |
720 | //free(decoder->sync); |
721 | //decoder->sync = 0; |
722 | |
723 | return result; |
724 | } |
725 | |
726 | /* |
727 | * NAME: decoder->message() |
728 | * DESCRIPTION: send a message to and receive a reply from the decoder process |
729 | */ |
730 | int mad_decoder_message(struct mad_decoder *decoder, |
731 | void *message, unsigned int *len) |
732 | { |
733 | # if defined(USE_ASYNC) |
734 | if (decoder->mode != MAD_DECODER_MODE_ASYNC || |
735 | send(decoder->async.out, message, *len) != MAD_FLOW_CONTINUE || |
736 | receive(decoder->async.in, &message, len) != MAD_FLOW_CONTINUE) { |
737 | return -1; |
738 | } |
739 | |
740 | return 0; |
741 | # else |
742 | return -1; |
743 | # endif |
744 | } |
745 | |
746 | |
747 | /* |
748 | * This is the input callback. The purpose of this callback is to (re)fill |
749 | * the stream buffer which is to be decoded. In this example, an entire file |
750 | * has been mapped into memory, so we just call mad_stream_buffer() with the |
751 | * address and length of the mapping. When this callback is called a second |
752 | * time, we are finished decoding. |
753 | */ |
754 | |
755 | static |
756 | enum mad_flow input(void *data, |
757 | struct mad_stream *stream) |
758 | { |
759 | struct buffer *buffer = data; |
760 | |
761 | if (!buffer->length) { |
762 | return MAD_FLOW_STOP; |
763 | } |
764 | |
765 | mad_stream_buffer(stream, buffer->start, buffer->length); |
766 | |
767 | buffer->length = 0; |
768 | |
769 | return MAD_FLOW_CONTINUE; |
770 | } |
771 | |
772 | /* |
773 | * The following utility routine performs simple rounding, clipping, and |
774 | * scaling of MAD's high-resolution samples down to 16 bits. It does not |
775 | * perform any dithering or noise shaping, which would be recommended to |
776 | * obtain any exceptional audio quality. It is therefore not recommended to |
777 | * use this routine if high-quality output is desired. |
778 | */ |
779 | |
780 | static |
781 | signed int scale(mad_fixed_t sample) |
782 | { |
783 | /* round */ |
784 | sample += (1L << (MAD_F_FRACBITS - 16)); |
785 | |
786 | /* clip */ |
787 | if (sample >= MAD_F_ONE) { |
788 | sample = MAD_F_ONE - 1; |
789 | } else if (sample < -MAD_F_ONE) { |
790 | sample = -MAD_F_ONE; |
791 | } |
792 | |
793 | /* quantize */ |
794 | return sample >> (MAD_F_FRACBITS + 1 - 16); |
795 | } |
796 | |
797 | /* |
798 | * This is the output callback function. It is called after each frame of |
799 | * MPEG audio data has been completely decoded. The purpose of this callback |
800 | * is to output (or play) the decoded PCM audio. |
801 | */ |
802 | |
803 | static |
804 | enum mad_flow output(void *data, |
805 | struct mad_header const *header, |
806 | struct mad_pcm *pcm) |
807 | { |
808 | unsigned int nchannels, nsamples; |
809 | mad_fixed_t const *left_ch, *right_ch; |
810 | |
811 | /* pcm->samplerate contains the sampling frequency */ |
812 | |
813 | nchannels = pcm->channels; |
814 | nsamples = pcm->length; |
815 | left_ch = pcm->samples[0]; |
816 | right_ch = pcm->samples[1]; |
817 | //*pcm_out_len += 4608; |
818 | /*store the last channel num and sr info */ |
819 | if (last_ch_num != nchannels) { |
820 | last_ch_num = nchannels; |
821 | } |
822 | if (last_sr != pcm->samplerate) { |
823 | last_sr = pcm->samplerate; |
824 | } |
825 | *pcm_out_len += pcm->length * 2 * (header->mode > 0 ? 2 : 1);; |
826 | |
827 | if (stream->muted_samples == 0) { |
828 | stream->muted_samples = pcm->samplerate * pcm->channels * MUTE_S; |
829 | } |
830 | if (stream->muted_count < stream->muted_samples) { |
831 | memset(pcm_out_data, 0, 2 * nsamples); |
832 | stream->muted_count += nsamples; |
833 | goto output1; |
834 | } |
835 | |
836 | while (nsamples--) { |
837 | signed int sample_l; |
838 | signed int sample_r; |
839 | |
840 | /* output sample(s) in 16-bit signed little-endian PCM */ |
841 | |
842 | sample_l = scale(*left_ch++); |
843 | //putchar((sample >> 0) & 0xff); |
844 | //putchar((sample >> 8) & 0xff); |
845 | pcm_out_data[0] = sample_l >> 0; |
846 | pcm_out_data[1] = sample_l >> 8; |
847 | pcm_out_data += 2; |
848 | if (nchannels == 2) { |
849 | sample_r = scale(*right_ch++); |
850 | //putchar((sample >> 0) & 0xff); |
851 | //putchar((sample >> 8) & 0xff); |
852 | pcm_out_data[0] = sample_r >> 0; |
853 | pcm_out_data[1] = sample_r >> 8; |
854 | pcm_out_data += 2; |
855 | } |
856 | |
857 | } |
858 | output1: |
859 | stream->this_frame = stream->next_frame; |
860 | return MAD_FLOW_STOP; |
861 | //return MAD_FLOW_CONTINUE; |
862 | } |
863 | |
864 | /* |
865 | * This is the error callback function. It is called whenever a decoding |
866 | * error occurs. The error is indicated by stream->error; the list of |
867 | * possible MAD_ERROR_* errors can be found in the mad.h (or stream.h) |
868 | * header file. |
869 | */ |
870 | |
871 | static |
872 | enum mad_flow error(void *data, |
873 | struct mad_stream *stream, |
874 | struct mad_frame *frame) |
875 | { |
876 | struct buffer *buffer = data; |
877 | int tagsize; |
878 | switch (stream->error) { |
879 | case MAD_ERROR_LOSTSYNC: |
880 | tagsize = id3_tag_query(stream->this_frame, |
881 | stream->bufend - stream->this_frame); |
882 | if (tagsize > 0) { |
883 | stream->skiplen = tagsize; |
884 | |
885 | audio_codec_print("id3 info, size = %d, just skip it!\n", tagsize); |
886 | |
887 | return MAD_FLOW_CONTINUE; |
888 | } |
889 | |
890 | /* fall through */ |
891 | |
892 | default: |
893 | break; |
894 | } |
895 | audio_codec_print("decoding error 0x%04x (%s) at byte offset %u\n", |
896 | stream->error, mad_stream_errorstr(stream), |
897 | stream->this_frame - stream->buffer); |
898 | |
899 | /* return MAD_FLOW_BREAK here to stop decoding (and propagate an error) */ |
900 | if (stream->error == MAD_ERROR_BADBITALLOC) { |
901 | return MAD_FLOW_IGNORE; |
902 | } |
903 | |
904 | return MAD_FLOW_CONTINUE; |
905 | } |
906 | |
907 | /* |
908 | * This is the function called by main() above to perform all the decoding. |
909 | * It instantiates a decoder object and configures it with the input, |
910 | * output, and error callback functions above. A single call to |
911 | * mad_decoder_run() continues until a callback function returns |
912 | * MAD_FLOW_STOP (to stop decoding) or MAD_FLOW_BREAK (to stop decoding and |
913 | * signal an error). |
914 | */ |
915 | |
916 | int audio_dec_decode( |
917 | #ifndef _WIN32 |
918 | audio_decoder_operations_t *adec_ops, |
919 | #endif |
920 | char *outbuf, int *outlen, char *inbuf, int inlen/*unsigned char const *start, unsigned long length*/) |
921 | { |
922 | int result; |
923 | struct buffer buffer; |
924 | |
925 | buffer.start = inbuf; |
926 | buffer.length = inlen; |
927 | |
928 | /* initialize our private message structure */ |
929 | |
930 | pcm_out_data = outbuf; |
931 | pcm_out_len = outlen; |
932 | *pcm_out_len = 0; |
933 | |
934 | /* configure input, output, and error functions */ |
935 | decoder.cb_data = &buffer; |
936 | |
937 | /* start decoding */ |
938 | result = mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC); |
939 | |
940 | /* release the decoder */ |
941 | |
942 | return result; |
943 | } |
944 | |
945 | int audio_dec_init( |
946 | #ifndef _WIN32 |
947 | audio_decoder_operations_t *adec_ops |
948 | #endif |
949 | ) |
950 | { |
951 | //audio_codec_print("\n\n[%s]BuildDate--%s BuildTime--%s", __FUNCTION__, __DATE__, __TIME__); |
952 | memset(&decoder, 0, sizeof(struct mad_decoder)); |
953 | |
954 | mad_decoder_init(&decoder, 0/*&buffer*/, |
955 | input, 0 /* header */, 0 /* filter */, output, |
956 | error, 0 /* message */); |
957 | |
958 | if (decoder.input_func == 0) { |
959 | return -1; |
960 | } |
961 | |
962 | if (decoder.error_func) { |
963 | error_func = decoder.error_func; |
964 | //error_data = decoder->cb_data; |
965 | } else { |
966 | error_func = error_default; |
967 | error_data = &bad_last_frame; |
968 | } |
969 | |
970 | decoder.sync = malloc(sizeof(*decoder.sync)); |
971 | stream = &decoder.sync->stream; |
972 | frame = &decoder.sync->frame; |
973 | synth = &decoder.sync->synth; |
974 | |
975 | mad_stream_init(stream); |
976 | mad_frame_init(frame); |
977 | mad_synth_init(synth); |
978 | |
979 | mad_stream_options(stream, decoder.options); |
980 | |
981 | #ifndef _WIN32 |
982 | adec_ops->nInBufSize = 5 * 1024; |
983 | adec_ops->nOutBufSize = 64 * 1024; |
984 | #endif |
985 | |
986 | stream->muted_samples = 0; |
987 | stream->muted_count = 0; |
988 | audio_codec_print("libmad init ok!\n"); |
989 | |
990 | return 0; |
991 | } |
992 | |
993 | #ifndef _WIN32 |
994 | int audio_dec_getinfo(audio_decoder_operations_t *adec_ops, void *pAudioInfo) |
995 | { |
996 | if (last_ch_num <= 0 || last_sr <= 0) { |
997 | return 0; |
998 | } |
999 | ((AudioInfo *)pAudioInfo)->channels = last_ch_num; |
1000 | ((AudioInfo *)pAudioInfo)->samplerate = last_sr; |
1001 | adec_ops->NchOriginal = last_ch_num; |
1002 | return 0; |
1003 | } |
1004 | #endif |
1005 | |
1006 | int audio_dec_release( |
1007 | #ifndef _WIN32 |
1008 | audio_decoder_operations_t *adec_ops |
1009 | #endif |
1010 | ) |
1011 | { |
1012 | |
1013 | |
1014 | mad_synth_finish(synth); |
1015 | mad_frame_finish(frame); |
1016 | mad_stream_finish(stream); |
1017 | |
1018 | free(decoder.sync); |
1019 | |
1020 | mad_decoder_finish(&decoder); |
1021 | |
1022 | stream->muted_samples = 0; |
1023 | stream->muted_count = 0; |
1024 | |
1025 | audio_codec_print("libmad release ok!\n"); |
1026 | |
1027 | return 0; |
1028 | } |
1029 | // win test |
1030 | #ifdef _WIN32 |
1031 | char *filename = "mnjr.mp3"; |
1032 | char *filename2 = "mnjr.pcm"; |
1033 | FILE *fp; |
1034 | FILE *fp2; |
1035 | int main(int argc, char *argv[]) |
1036 | { |
1037 | char *outbuf; |
1038 | int *outlen; |
1039 | char *inbuf; |
1040 | int inlen; |
1041 | int size; |
1042 | int rlen; |
1043 | |
1044 | size = 0; |
1045 | inlen = 0; |
1046 | inbuf = (char *)malloc(50001); |
1047 | outbuf = (char *)malloc(1920000); |
1048 | outlen = (int *)malloc(5); |
1049 | |
1050 | //init |
1051 | fp = fopen(filename, "rb"); |
1052 | if (fp == NULL) { |
1053 | printf("open input file failed!\n"); |
1054 | return 0; |
1055 | } |
1056 | fp2 = fopen(filename2, "wb"); |
1057 | |
1058 | audio_dec_init(); |
1059 | |
1060 | do { |
1061 | //input |
1062 | if (size) { |
1063 | memmove(inbuf, inbuf + size, 20000 - size); |
1064 | rlen = size; |
1065 | } else { |
1066 | rlen = 20000; |
1067 | } |
1068 | |
1069 | inlen = fread(inbuf + 20000 - rlen, 1, rlen, fp); |
1070 | |
1071 | if (inlen == rlen) { |
1072 | inlen = 20000; |
1073 | } |
1074 | |
1075 | //decode |
1076 | size = audio_dec_decode( |
1077 | #ifndef _WIN32 |
1078 | adec_ops, |
1079 | #endif |
1080 | outbuf, outlen, inbuf, inlen); |
1081 | |
1082 | //output |
1083 | fwrite(outbuf, 1, *outlen, fp2); |
1084 | |
1085 | } while (inlen == 20000); |
1086 | |
1087 | //release |
1088 | audio_dec_release(); |
1089 | *pcm_out_data = NULL; |
1090 | *pcm_out_len = NULL; |
1091 | free(inbuf); |
1092 | free(outbuf); |
1093 | free(outlen); |
1094 | fclose(fp); |
1095 | fclose(fp2); |
1096 | inbuf = NULL; |
1097 | outbuf = NULL; |
1098 | outlen = NULL; |
1099 | |
1100 | return size; |
1101 | } |
1102 | #endif |
1103 |