Diffstat (limited to 'core/multimedia/opieplayer/libmad/decoder.c') (more/less context) (ignore whitespace changes)
-rw-r--r-- | core/multimedia/opieplayer/libmad/decoder.c | 60 |
1 files changed, 38 insertions, 22 deletions
diff --git a/core/multimedia/opieplayer/libmad/decoder.c b/core/multimedia/opieplayer/libmad/decoder.c index dcf7cf3..b2b6cbb 100644 --- a/core/multimedia/opieplayer/libmad/decoder.c +++ b/core/multimedia/opieplayer/libmad/decoder.c | |||
@@ -1,8 +1,8 @@ | |||
1 | /* | 1 | /* |
2 | * mad - MPEG audio decoder | 2 | * libmad - MPEG audio decoder library |
3 | * Copyright (C) 2000-2001 Robert Leslie | 3 | * Copyright (C) 2000-2001 Robert Leslie |
4 | * | 4 | * |
5 | * This program is free software; you can redistribute it and/or modify | 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 | 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 | 7 | * the Free Software Foundation; either version 2 of the License, or |
8 | * (at your option) any later version. | 8 | * (at your option) any later version. |
@@ -17,52 +17,57 @@ | |||
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 | * | 18 | * |
19 | * $Id$ | 19 | * $Id$ |
20 | */ | 20 | */ |
21 | 21 | ||
22 | # ifdef HAVE_CONFIG_H | 22 | # ifdef HAVE_CONFIG_H |
23 | # include "libmad_config.h" | 23 | # include "libmad_config.h" |
24 | # else | ||
25 | # ifndef WEXITSTATUS | ||
26 | # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8) | ||
27 | # endif | ||
28 | # ifndef WIFEXITED | ||
29 | # define WIFEXITED(stat_val) (((stat_val) & 255) == 0) | ||
30 | # endif | ||
31 | # endif | 24 | # endif |
32 | 25 | ||
33 | # include "libmad_global.h" | 26 | # include "libmad_global.h" |
34 | 27 | ||
35 | # include <sys/types.h> | 28 | # ifdef HAVE_SYS_TYPES_H |
29 | # include <sys/types.h> | ||
30 | # endif | ||
36 | 31 | ||
37 | # ifdef HAVE_SYS_WAIT_H | 32 | # ifdef HAVE_SYS_WAIT_H |
38 | # include <sys/wait.h> | 33 | # include <sys/wait.h> |
39 | # endif | 34 | # endif |
40 | 35 | ||
41 | # ifdef HAVE_UNISTD_H | 36 | # ifdef HAVE_UNISTD_H |
42 | # include <unistd.h> | 37 | # include <unistd.h> |
43 | # endif | 38 | # endif |
44 | 39 | ||
45 | # include <fcntl.h> | 40 | # ifdef HAVE_FCNTL_H |
41 | # include <fcntl.h> | ||
42 | # endif | ||
43 | |||
46 | # include <stdlib.h> | 44 | # include <stdlib.h> |
47 | # include <errno.h> | 45 | |
46 | # ifdef HAVE_ERRNO_H | ||
47 | # include <errno.h> | ||
48 | # endif | ||
48 | 49 | ||
49 | # include "stream.h" | 50 | # include "stream.h" |
50 | # include "frame.h" | 51 | # include "frame.h" |
51 | # include "synth.h" | 52 | # include "synth.h" |
52 | # include "decoder.h" | 53 | # include "decoder.h" |
53 | 54 | ||
54 | void mad_decoder_init(struct mad_decoder *decoder, void *data, | 55 | void mad_decoder_init(struct mad_decoder *decoder, void *data, |
55 | enum mad_flow (*input_func)(void *, struct mad_stream *), | 56 | enum mad_flow (*input_func)(void *, |
57 | struct mad_stream *), | ||
56 | enum mad_flow (*header_func)(void *, | 58 | enum mad_flow (*header_func)(void *, |
57 | struct mad_header const *), | 59 | struct mad_header const *), |
58 | enum mad_flow (*filter_func)(void *, struct mad_frame *), | 60 | enum mad_flow (*filter_func)(void *, |
61 | struct mad_stream const *, | ||
62 | struct mad_frame *), | ||
59 | enum mad_flow (*output_func)(void *, | 63 | enum mad_flow (*output_func)(void *, |
60 | struct mad_header const *, | 64 | struct mad_header const *, |
61 | struct mad_pcm *), | 65 | struct mad_pcm *), |
62 | enum mad_flow (*error_func)(void *, struct mad_stream *, | 66 | enum mad_flow (*error_func)(void *, |
67 | struct mad_stream *, | ||
63 | struct mad_frame *), | 68 | struct mad_frame *), |
64 | enum mad_flow (*message_func)(void *, | 69 | enum mad_flow (*message_func)(void *, |
65 | void *, unsigned int *)) | 70 | void *, unsigned int *)) |
66 | { | 71 | { |
67 | decoder->mode = -1; | 72 | decoder->mode = -1; |
68 | 73 | ||
@@ -83,21 +88,21 @@ void mad_decoder_init(struct mad_decoder *decoder, void *data, | |||
83 | decoder->error_func = error_func; | 88 | decoder->error_func = error_func; |
84 | decoder->message_func = message_func; | 89 | decoder->message_func = message_func; |
85 | } | 90 | } |
86 | 91 | ||
87 | int mad_decoder_finish(struct mad_decoder *decoder) | 92 | int mad_decoder_finish(struct mad_decoder *decoder) |
88 | { | 93 | { |
94 | # if defined(USE_ASYNC) | ||
89 | if (decoder->mode == MAD_DECODER_MODE_ASYNC && decoder->async.pid) { | 95 | if (decoder->mode == MAD_DECODER_MODE_ASYNC && decoder->async.pid) { |
90 | pid_t pid; | 96 | pid_t pid; |
91 | int status; | 97 | int status; |
92 | 98 | ||
93 | close(decoder->async.in); | 99 | close(decoder->async.in); |
94 | 100 | ||
95 | do { | 101 | do |
96 | pid = waitpid(decoder->async.pid, &status, 0); | 102 | pid = waitpid(decoder->async.pid, &status, 0); |
97 | } | ||
98 | while (pid == -1 && errno == EINTR); | 103 | while (pid == -1 && errno == EINTR); |
99 | 104 | ||
100 | decoder->mode = -1; | 105 | decoder->mode = -1; |
101 | 106 | ||
102 | close(decoder->async.out); | 107 | close(decoder->async.out); |
103 | 108 | ||
@@ -107,26 +112,27 @@ int mad_decoder_finish(struct mad_decoder *decoder) | |||
107 | 112 | ||
108 | if (pid == -1) | 113 | if (pid == -1) |
109 | return -1; | 114 | return -1; |
110 | 115 | ||
111 | return (!WIFEXITED(status) || WEXITSTATUS(status)) ? -1 : 0; | 116 | return (!WIFEXITED(status) || WEXITSTATUS(status)) ? -1 : 0; |
112 | } | 117 | } |
118 | # endif | ||
113 | 119 | ||
114 | return 0; | 120 | return 0; |
115 | } | 121 | } |
116 | 122 | ||
123 | # if defined(USE_ASYNC) | ||
117 | static | 124 | static |
118 | enum mad_flow send_io(int fd, void const *data, size_t len) | 125 | enum mad_flow send_io(int fd, void const *data, size_t len) |
119 | { | 126 | { |
120 | char const *ptr = data; | 127 | char const *ptr = data; |
121 | ssize_t count; | 128 | ssize_t count; |
122 | 129 | ||
123 | while (len) { | 130 | while (len) { |
124 | do { | 131 | do |
125 | count = write(fd, ptr, len); | 132 | count = write(fd, ptr, len); |
126 | } | ||
127 | while (count == -1 && errno == EINTR); | 133 | while (count == -1 && errno == EINTR); |
128 | 134 | ||
129 | if (count == -1) | 135 | if (count == -1) |
130 | return MAD_FLOW_BREAK; | 136 | return MAD_FLOW_BREAK; |
131 | 137 | ||
132 | len -= count; | 138 | len -= count; |
@@ -140,15 +146,14 @@ static | |||
140 | enum mad_flow receive_io(int fd, void *buffer, size_t len) | 146 | enum mad_flow receive_io(int fd, void *buffer, size_t len) |
141 | { | 147 | { |
142 | char *ptr = buffer; | 148 | char *ptr = buffer; |
143 | ssize_t count; | 149 | ssize_t count; |
144 | 150 | ||
145 | while (len) { | 151 | while (len) { |
146 | do { | 152 | do |
147 | count = read(fd, ptr, len); | 153 | count = read(fd, ptr, len); |
148 | } | ||
149 | while (count == -1 && errno == EINTR); | 154 | while (count == -1 && errno == EINTR); |
150 | 155 | ||
151 | if (count == -1) | 156 | if (count == -1) |
152 | return (errno == EAGAIN) ? MAD_FLOW_IGNORE : MAD_FLOW_BREAK; | 157 | return (errno == EAGAIN) ? MAD_FLOW_IGNORE : MAD_FLOW_BREAK; |
153 | else if (count == 0) | 158 | else if (count == 0) |
154 | return MAD_FLOW_STOP; | 159 | return MAD_FLOW_STOP; |
@@ -278,12 +283,13 @@ enum mad_flow check_message(struct mad_decoder *decoder) | |||
278 | 283 | ||
279 | if (message) | 284 | if (message) |
280 | free(message); | 285 | free(message); |
281 | 286 | ||
282 | return result; | 287 | return result; |
283 | } | 288 | } |
289 | # endif | ||
284 | 290 | ||
285 | static | 291 | static |
286 | enum mad_flow error_default(void *data, struct mad_stream *stream, | 292 | enum mad_flow error_default(void *data, struct mad_stream *stream, |
287 | struct mad_frame *frame) | 293 | struct mad_frame *frame) |
288 | { | 294 | { |
289 | int *bad_last_frame = data; | 295 | int *bad_last_frame = data; |
@@ -345,23 +351,25 @@ int run_sync(struct mad_decoder *decoder) | |||
345 | continue; | 351 | continue; |
346 | case MAD_FLOW_CONTINUE: | 352 | case MAD_FLOW_CONTINUE: |
347 | break; | 353 | break; |
348 | } | 354 | } |
349 | 355 | ||
350 | while (1) { | 356 | while (1) { |
357 | # if defined(USE_ASYNC) | ||
351 | if (decoder->mode == MAD_DECODER_MODE_ASYNC) { | 358 | if (decoder->mode == MAD_DECODER_MODE_ASYNC) { |
352 | switch (check_message(decoder)) { | 359 | switch (check_message(decoder)) { |
353 | case MAD_FLOW_IGNORE: | 360 | case MAD_FLOW_IGNORE: |
354 | case MAD_FLOW_CONTINUE: | 361 | case MAD_FLOW_CONTINUE: |
355 | break; | 362 | break; |
356 | case MAD_FLOW_BREAK: | 363 | case MAD_FLOW_BREAK: |
357 | goto fail; | 364 | goto fail; |
358 | case MAD_FLOW_STOP: | 365 | case MAD_FLOW_STOP: |
359 | goto done; | 366 | goto done; |
360 | } | 367 | } |
361 | } | 368 | } |
369 | # endif | ||
362 | 370 | ||
363 | if (decoder->header_func) { | 371 | if (decoder->header_func) { |
364 | if (mad_header_decode(&frame->header, stream) == -1) { | 372 | if (mad_header_decode(&frame->header, stream) == -1) { |
365 | if (!MAD_RECOVERABLE(stream->error)) | 373 | if (!MAD_RECOVERABLE(stream->error)) |
366 | break; | 374 | break; |
367 | 375 | ||
@@ -406,13 +414,13 @@ int run_sync(struct mad_decoder *decoder) | |||
406 | } | 414 | } |
407 | } | 415 | } |
408 | else | 416 | else |
409 | bad_last_frame = 0; | 417 | bad_last_frame = 0; |
410 | 418 | ||
411 | if (decoder->filter_func) { | 419 | if (decoder->filter_func) { |
412 | switch (decoder->filter_func(decoder->cb_data, frame)) { | 420 | switch (decoder->filter_func(decoder->cb_data, stream, frame)) { |
413 | case MAD_FLOW_STOP: | 421 | case MAD_FLOW_STOP: |
414 | goto done; | 422 | goto done; |
415 | case MAD_FLOW_BREAK: | 423 | case MAD_FLOW_BREAK: |
416 | goto fail; | 424 | goto fail; |
417 | case MAD_FLOW_IGNORE: | 425 | case MAD_FLOW_IGNORE: |
418 | continue; | 426 | continue; |
@@ -447,12 +455,13 @@ int run_sync(struct mad_decoder *decoder) | |||
447 | mad_frame_finish(frame); | 455 | mad_frame_finish(frame); |
448 | mad_stream_finish(stream); | 456 | mad_stream_finish(stream); |
449 | 457 | ||
450 | return result; | 458 | return result; |
451 | } | 459 | } |
452 | 460 | ||
461 | # if defined(USE_ASYNC) | ||
453 | static | 462 | static |
454 | int run_async(struct mad_decoder *decoder) | 463 | int run_async(struct mad_decoder *decoder) |
455 | { | 464 | { |
456 | pid_t pid; | 465 | pid_t pid; |
457 | int ptoc[2], ctop[2], flags; | 466 | int ptoc[2], ctop[2], flags; |
458 | 467 | ||
@@ -508,25 +517,28 @@ int run_async(struct mad_decoder *decoder) | |||
508 | 517 | ||
509 | _exit(run_sync(decoder)); | 518 | _exit(run_sync(decoder)); |
510 | 519 | ||
511 | /* not reached */ | 520 | /* not reached */ |
512 | return -1; | 521 | return -1; |
513 | } | 522 | } |
523 | # endif | ||
514 | 524 | ||
515 | int mad_decoder_run(struct mad_decoder *decoder, enum mad_decoder_mode mode) | 525 | int mad_decoder_run(struct mad_decoder *decoder, enum mad_decoder_mode mode) |
516 | { | 526 | { |
517 | int result; | 527 | int result; |
518 | int (*run)(struct mad_decoder *) = 0; | 528 | int (*run)(struct mad_decoder *) = 0; |
519 | 529 | ||
520 | switch (decoder->mode = mode) { | 530 | switch (decoder->mode = mode) { |
521 | case MAD_DECODER_MODE_SYNC: | 531 | case MAD_DECODER_MODE_SYNC: |
522 | run = run_sync; | 532 | run = run_sync; |
523 | break; | 533 | break; |
524 | 534 | ||
525 | case MAD_DECODER_MODE_ASYNC: | 535 | case MAD_DECODER_MODE_ASYNC: |
536 | # if defined(USE_ASYNC) | ||
526 | run = run_async; | 537 | run = run_async; |
538 | # endif | ||
527 | break; | 539 | break; |
528 | } | 540 | } |
529 | 541 | ||
530 | if (run == 0) | 542 | if (run == 0) |
531 | return -1; | 543 | return -1; |
532 | 544 | ||
@@ -542,13 +554,17 @@ int mad_decoder_run(struct mad_decoder *decoder, enum mad_decoder_mode mode) | |||
542 | return result; | 554 | return result; |
543 | } | 555 | } |
544 | 556 | ||
545 | int mad_decoder_message(struct mad_decoder *decoder, | 557 | int mad_decoder_message(struct mad_decoder *decoder, |
546 | void *message, unsigned int *len) | 558 | void *message, unsigned int *len) |
547 | { | 559 | { |
560 | # if defined(USE_ASYNC) | ||
548 | if (decoder->mode != MAD_DECODER_MODE_ASYNC || | 561 | if (decoder->mode != MAD_DECODER_MODE_ASYNC || |
549 | send(decoder->async.out, message, *len) != MAD_FLOW_CONTINUE || | 562 | send(decoder->async.out, message, *len) != MAD_FLOW_CONTINUE || |
550 | receive(decoder->async.in, &message, len) != MAD_FLOW_CONTINUE) | 563 | receive(decoder->async.in, &message, len) != MAD_FLOW_CONTINUE) |
551 | return -1; | 564 | return -1; |
552 | 565 | ||
553 | return 0; | 566 | return 0; |
567 | # else | ||
568 | return -1; | ||
569 | # endif | ||
554 | } | 570 | } |