Revert unrelated part of r25491 : fuzev1 init code works again
[kugel-rb.git] / apps / codecs / libmad / decoder.c
blob46f4dfe5f7bea272d6ebd63f8a9f6d22720bc30c
1 /*
2 * libmad - MPEG audio decoder library
3 * Copyright (C) 2000-2004 Underbit Technologies, Inc.
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.
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.
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
19 * $Id$
22 # ifdef HAVE_CONFIG_H
23 # include "config.h"
24 # endif
26 # include "global.h"
28 # ifdef HAVE_SYS_TYPES_H
29 # include <sys/types.h>
30 # endif
32 # ifdef HAVE_SYS_WAIT_H
33 # include <sys/wait.h>
34 # endif
36 # ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 # endif
40 # ifdef HAVE_FCNTL_H
41 # include <fcntl.h>
42 # endif
44 # ifdef HAVE_ERRNO_H
45 # include <errno.h>
46 # endif
48 # include "stream.h"
49 # include "frame.h"
50 # include "synth.h"
51 # include "decoder.h"
54 * NAME: decoder->init()
55 * DESCRIPTION: initialize a decoder object with callback routines
57 void mad_decoder_init(struct mad_decoder *decoder, void *data,
58 enum mad_flow (*input_func)(void *,
59 struct mad_stream *),
60 enum mad_flow (*header_func)(void *,
61 struct mad_header const *),
62 enum mad_flow (*filter_func)(void *,
63 struct mad_stream const *,
64 struct mad_frame *),
65 enum mad_flow (*output_func)(void *,
66 struct mad_header const *,
67 struct mad_pcm *),
68 enum mad_flow (*error_func)(void *,
69 struct mad_stream *,
70 struct mad_frame *),
71 enum mad_flow (*message_func)(void *,
72 void *, unsigned int *))
74 decoder->mode = -1;
76 decoder->options = 0;
78 decoder->async.pid = 0;
79 decoder->async.in = -1;
80 decoder->async.out = -1;
82 decoder->sync = 0;
84 decoder->cb_data = data;
86 decoder->input_func = input_func;
87 decoder->header_func = header_func;
88 decoder->filter_func = filter_func;
89 decoder->output_func = output_func;
90 decoder->error_func = error_func;
91 decoder->message_func = message_func;
94 int mad_decoder_finish(struct mad_decoder *decoder)
96 # if defined(USE_ASYNC)
97 if (decoder->mode == MAD_DECODER_MODE_ASYNC && decoder->async.pid) {
98 pid_t pid;
99 int status;
101 close(decoder->async.in);
104 pid = waitpid(decoder->async.pid, &status, 0);
105 while (pid == -1 && errno == EINTR);
107 decoder->mode = -1;
109 close(decoder->async.out);
111 decoder->async.pid = 0;
112 decoder->async.in = -1;
113 decoder->async.out = -1;
115 if (pid == -1)
116 return -1;
118 return (!WIFEXITED(status) || WEXITSTATUS(status)) ? -1 : 0;
120 # else
121 /* Avoid compiler warning */
122 (void)decoder;
123 # endif
125 return 0;
128 # if defined(USE_ASYNC)
129 static
130 enum mad_flow send_io(int fd, void const *data, size_t len)
132 char const *ptr = data;
133 ssize_t count;
135 while (len) {
137 count = write(fd, ptr, len);
138 while (count == -1 && errno == EINTR);
140 if (count == -1)
141 return MAD_FLOW_BREAK;
143 len -= count;
144 ptr += count;
147 return MAD_FLOW_CONTINUE;
150 static
151 enum mad_flow receive_io(int fd, void *buffer, size_t len)
153 char *ptr = buffer;
154 ssize_t count;
156 while (len) {
158 count = read(fd, ptr, len);
159 while (count == -1 && errno == EINTR);
161 if (count == -1)
162 return (errno == EAGAIN) ? MAD_FLOW_IGNORE : MAD_FLOW_BREAK;
163 else if (count == 0)
164 return MAD_FLOW_STOP;
166 len -= count;
167 ptr += count;
170 return MAD_FLOW_CONTINUE;
173 static
174 enum mad_flow receive_io_blocking(int fd, void *buffer, size_t len)
176 int flags, blocking;
177 enum mad_flow result;
179 flags = fcntl(fd, F_GETFL);
180 if (flags == -1)
181 return MAD_FLOW_BREAK;
183 blocking = flags & ~O_NONBLOCK;
185 if (blocking != flags &&
186 fcntl(fd, F_SETFL, blocking) == -1)
187 return MAD_FLOW_BREAK;
189 result = receive_io(fd, buffer, len);
191 if (flags != blocking &&
192 fcntl(fd, F_SETFL, flags) == -1)
193 return MAD_FLOW_BREAK;
195 return result;
198 static
199 enum mad_flow send(int fd, void const *message, unsigned int size)
201 enum mad_flow result;
203 /* send size */
205 result = send_io(fd, &size, sizeof(size));
207 /* send message */
209 if (result == MAD_FLOW_CONTINUE)
210 result = send_io(fd, message, size);
212 return result;
215 static
216 enum mad_flow receive(int fd, void **message, unsigned int *size)
218 enum mad_flow result;
219 unsigned int actual;
221 if (*message == 0)
222 *size = 0;
224 /* receive size */
226 result = receive_io(fd, &actual, sizeof(actual));
228 /* receive message */
230 if (result == MAD_FLOW_CONTINUE) {
231 if (actual > *size)
232 actual -= *size;
233 else {
234 *size = actual;
235 actual = 0;
238 if (*size > 0) {
239 if (*message == 0) {
240 *message = malloc(*size);
241 if (*message == 0)
242 return MAD_FLOW_BREAK;
245 result = receive_io_blocking(fd, *message, *size);
248 /* throw away remainder of message */
250 while (actual && result == MAD_FLOW_CONTINUE) {
251 char sink[256];
252 unsigned int len;
254 len = actual > sizeof(sink) ? sizeof(sink) : actual;
256 result = receive_io_blocking(fd, sink, len);
258 actual -= len;
262 return result;
265 static
266 enum mad_flow check_message(struct mad_decoder *decoder)
268 enum mad_flow result;
269 void *message = 0;
270 unsigned int size;
272 result = receive(decoder->async.in, &message, &size);
274 if (result == MAD_FLOW_CONTINUE) {
275 if (decoder->message_func == 0)
276 size = 0;
277 else {
278 result = decoder->message_func(decoder->cb_data, message, &size);
280 if (result == MAD_FLOW_IGNORE ||
281 result == MAD_FLOW_BREAK)
282 size = 0;
285 if (send(decoder->async.out, message, size) != MAD_FLOW_CONTINUE)
286 result = MAD_FLOW_BREAK;
289 if (message)
290 free(message);
292 return result;
294 # endif
296 static
297 enum mad_flow error_default(void *data, struct mad_stream *stream,
298 struct mad_frame *frame)
300 int *bad_last_frame = data;
302 switch (stream->error) {
303 case MAD_ERROR_BADCRC:
304 if (*bad_last_frame)
305 mad_frame_mute(frame);
306 else
307 *bad_last_frame = 1;
309 return MAD_FLOW_IGNORE;
311 default:
312 return MAD_FLOW_CONTINUE;
316 static
317 int run_sync(struct mad_decoder *decoder)
319 enum mad_flow (*error_func)(void *, struct mad_stream *, struct mad_frame *);
320 void *error_data;
321 int bad_last_frame = 0;
322 struct mad_stream *stream;
323 struct mad_frame *frame;
324 struct mad_synth *synth;
325 int result = 0;
327 if (decoder->input_func == 0)
328 return 0;
330 if (decoder->error_func) {
331 error_func = decoder->error_func;
332 error_data = decoder->cb_data;
334 else {
335 error_func = error_default;
336 error_data = &bad_last_frame;
339 stream = &decoder->sync->stream;
340 frame = &decoder->sync->frame;
341 synth = &decoder->sync->synth;
343 mad_stream_init(stream);
344 mad_frame_init(frame);
345 mad_synth_init(synth);
347 mad_stream_options(stream, decoder->options);
349 do {
350 switch (decoder->input_func(decoder->cb_data, stream)) {
351 case MAD_FLOW_STOP:
352 goto done;
353 case MAD_FLOW_BREAK:
354 goto fail;
355 case MAD_FLOW_IGNORE:
356 continue;
357 case MAD_FLOW_CONTINUE:
358 break;
361 while (1) {
362 # if defined(USE_ASYNC)
363 if (decoder->mode == MAD_DECODER_MODE_ASYNC) {
364 switch (check_message(decoder)) {
365 case MAD_FLOW_IGNORE:
366 case MAD_FLOW_CONTINUE:
367 break;
368 case MAD_FLOW_BREAK:
369 goto fail;
370 case MAD_FLOW_STOP:
371 goto done;
374 # endif
376 if (decoder->header_func) {
377 if (mad_header_decode(&frame->header, stream) == -1) {
378 if (!MAD_RECOVERABLE(stream->error))
379 break;
381 switch (error_func(error_data, stream, frame)) {
382 case MAD_FLOW_STOP:
383 goto done;
384 case MAD_FLOW_BREAK:
385 goto fail;
386 case MAD_FLOW_IGNORE:
387 case MAD_FLOW_CONTINUE:
388 default:
389 continue;
393 switch (decoder->header_func(decoder->cb_data, &frame->header)) {
394 case MAD_FLOW_STOP:
395 goto done;
396 case MAD_FLOW_BREAK:
397 goto fail;
398 case MAD_FLOW_IGNORE:
399 continue;
400 case MAD_FLOW_CONTINUE:
401 break;
405 if (mad_frame_decode(frame, stream) == -1) {
406 if (!MAD_RECOVERABLE(stream->error))
407 break;
409 switch (error_func(error_data, stream, frame)) {
410 case MAD_FLOW_STOP:
411 goto done;
412 case MAD_FLOW_BREAK:
413 goto fail;
414 case MAD_FLOW_IGNORE:
415 break;
416 case MAD_FLOW_CONTINUE:
417 default:
418 continue;
421 else
422 bad_last_frame = 0;
424 if (decoder->filter_func) {
425 switch (decoder->filter_func(decoder->cb_data, stream, frame)) {
426 case MAD_FLOW_STOP:
427 goto done;
428 case MAD_FLOW_BREAK:
429 goto fail;
430 case MAD_FLOW_IGNORE:
431 continue;
432 case MAD_FLOW_CONTINUE:
433 break;
437 mad_synth_frame(synth, frame);
439 if (decoder->output_func) {
440 switch (decoder->output_func(decoder->cb_data,
441 &frame->header, &synth->pcm)) {
442 case MAD_FLOW_STOP:
443 goto done;
444 case MAD_FLOW_BREAK:
445 goto fail;
446 case MAD_FLOW_IGNORE:
447 case MAD_FLOW_CONTINUE:
448 break;
453 while (stream->error == MAD_ERROR_BUFLEN);
455 fail:
456 result = -1;
458 done:
459 mad_synth_finish(synth);
460 mad_frame_finish(frame);
461 mad_stream_finish(stream);
463 return result;
466 # if defined(USE_ASYNC)
467 static
468 int run_async(struct mad_decoder *decoder)
470 pid_t pid;
471 int ptoc[2], ctop[2], flags;
473 if (pipe(ptoc) == -1)
474 return -1;
476 if (pipe(ctop) == -1) {
477 close(ptoc[0]);
478 close(ptoc[1]);
479 return -1;
482 flags = fcntl(ptoc[0], F_GETFL);
483 if (flags == -1 ||
484 fcntl(ptoc[0], F_SETFL, flags | O_NONBLOCK) == -1) {
485 close(ctop[0]);
486 close(ctop[1]);
487 close(ptoc[0]);
488 close(ptoc[1]);
489 return -1;
492 pid = fork();
493 if (pid == -1) {
494 close(ctop[0]);
495 close(ctop[1]);
496 close(ptoc[0]);
497 close(ptoc[1]);
498 return -1;
501 decoder->async.pid = pid;
503 if (pid) {
504 /* parent */
506 close(ptoc[0]);
507 close(ctop[1]);
509 decoder->async.in = ctop[0];
510 decoder->async.out = ptoc[1];
512 return 0;
515 /* child */
517 close(ptoc[1]);
518 close(ctop[0]);
520 decoder->async.in = ptoc[0];
521 decoder->async.out = ctop[1];
523 _exit(run_sync(decoder));
525 /* not reached */
526 return -1;
528 # endif
531 * NAME: decoder->run()
532 * DESCRIPTION: run the decoder thread either synchronously or asynchronously
534 int mad_decoder_run(struct mad_decoder *decoder, enum mad_decoder_mode mode)
536 int result;
537 int (*run)(struct mad_decoder *) = 0;
539 switch (decoder->mode = mode) {
540 case MAD_DECODER_MODE_SYNC:
541 run = run_sync;
542 break;
544 case MAD_DECODER_MODE_ASYNC:
545 # if defined(USE_ASYNC)
546 run = run_async;
547 # endif
548 break;
551 if (run == 0)
552 return -1;
554 decoder->sync = malloc(sizeof(*decoder->sync));
555 if (decoder->sync == 0)
556 return -1;
558 result = run(decoder);
560 free(decoder->sync);
561 decoder->sync = 0;
563 return result;
567 * NAME: decoder->message()
568 * DESCRIPTION: send a message to and receive a reply from the decoder process
570 int mad_decoder_message(struct mad_decoder *decoder,
571 void *message, unsigned int *len)
573 # if defined(USE_ASYNC)
574 if (decoder->mode != MAD_DECODER_MODE_ASYNC ||
575 send(decoder->async.out, message, *len) != MAD_FLOW_CONTINUE ||
576 receive(decoder->async.in, &message, len) != MAD_FLOW_CONTINUE)
577 return -1;
579 return 0;
580 # else
581 /* Avoid compiler warnings */
582 (void)decoder;
583 (void)message;
584 (void)len;
585 return -1;
586 # endif