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: decoder.c,v 1.22 2004/01/23 09:41:32 rob Exp $
28 # ifdef HAVE_SYS_TYPES_H
29 # include <sys/types.h>
32 # ifdef HAVE_SYS_WAIT_H
33 # include <sys/wait.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 *,
60 enum mad_flow (*header_func)(void *,
61 struct mad_header const *),
62 enum mad_flow (*filter_func)(void *,
63 struct mad_stream const *,
65 enum mad_flow (*output_func)(void *,
66 struct mad_header const *,
68 enum mad_flow (*error_func)(void *,
71 enum mad_flow (*message_func)(void *,
72 void *, unsigned int *))
78 decoder->async.pid = 0;
79 decoder->async.in = -1;
80 decoder->async.out = -1;
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) {
101 close(decoder->async.in);
104 pid = waitpid(decoder->async.pid, &status, 0);
105 while (pid == -1 && errno == EINTR);
109 close(decoder->async.out);
111 decoder->async.pid = 0;
112 decoder->async.in = -1;
113 decoder->async.out = -1;
118 return (!WIFEXITED(status) || WEXITSTATUS(status)) ? -1 : 0;
125 # if defined(USE_ASYNC)
127 enum mad_flow send_io(int fd, void const *data, size_t len)
129 char const *ptr = data;
134 count = write(fd, ptr, len);
135 while (count == -1 && errno == EINTR);
138 return MAD_FLOW_BREAK;
144 return MAD_FLOW_CONTINUE;
148 enum mad_flow receive_io(int fd, void *buffer, size_t len)
155 count = read(fd, ptr, len);
156 while (count == -1 && errno == EINTR);
159 return (errno == EAGAIN) ? MAD_FLOW_IGNORE : MAD_FLOW_BREAK;
161 return MAD_FLOW_STOP;
167 return MAD_FLOW_CONTINUE;
171 enum mad_flow receive_io_blocking(int fd, void *buffer, size_t len)
174 enum mad_flow result;
176 flags = fcntl(fd, F_GETFL);
178 return MAD_FLOW_BREAK;
180 blocking = flags & ~O_NONBLOCK;
182 if (blocking != flags &&
183 fcntl(fd, F_SETFL, blocking) == -1)
184 return MAD_FLOW_BREAK;
186 result = receive_io(fd, buffer, len);
188 if (flags != blocking &&
189 fcntl(fd, F_SETFL, flags) == -1)
190 return MAD_FLOW_BREAK;
196 enum mad_flow send(int fd, void const *message, unsigned int size)
198 enum mad_flow result;
202 result = send_io(fd, &size, sizeof(size));
206 if (result == MAD_FLOW_CONTINUE)
207 result = send_io(fd, message, size);
213 enum mad_flow receive(int fd, void **message, unsigned int *size)
215 enum mad_flow result;
223 result = receive_io(fd, &actual, sizeof(actual));
225 /* receive message */
227 if (result == MAD_FLOW_CONTINUE) {
237 *message = malloc(*size);
239 return MAD_FLOW_BREAK;
242 result = receive_io_blocking(fd, *message, *size);
245 /* throw away remainder of message */
247 while (actual && result == MAD_FLOW_CONTINUE) {
251 len = actual > sizeof(sink) ? sizeof(sink) : actual;
253 result = receive_io_blocking(fd, sink, len);
263 enum mad_flow check_message(struct mad_decoder *decoder)
265 enum mad_flow result;
269 result = receive(decoder->async.in, &message, &size);
271 if (result == MAD_FLOW_CONTINUE) {
272 if (decoder->message_func == 0)
275 result = decoder->message_func(decoder->cb_data, message, &size);
277 if (result == MAD_FLOW_IGNORE ||
278 result == MAD_FLOW_BREAK)
282 if (send(decoder->async.out, message, size) != MAD_FLOW_CONTINUE)
283 result = MAD_FLOW_BREAK;
294 enum mad_flow error_default(void *data, struct mad_stream *stream,
295 struct mad_frame *frame)
297 int *bad_last_frame = data;
299 switch (stream->error) {
300 case MAD_ERROR_BADCRC:
302 mad_frame_mute(frame);
306 return MAD_FLOW_IGNORE;
309 return MAD_FLOW_CONTINUE;
314 int run_sync(struct mad_decoder *decoder)
316 enum mad_flow (*error_func)(void *, struct mad_stream *, struct mad_frame *);
318 int bad_last_frame = 0;
319 struct mad_stream *stream;
320 struct mad_frame *frame;
321 struct mad_synth *synth;
324 if (decoder->input_func == 0)
327 if (decoder->error_func) {
328 error_func = decoder->error_func;
329 error_data = decoder->cb_data;
332 error_func = error_default;
333 error_data = &bad_last_frame;
336 stream = &decoder->sync->stream;
337 frame = &decoder->sync->frame;
338 synth = &decoder->sync->synth;
340 mad_stream_init(stream);
341 mad_frame_init(frame);
342 mad_synth_init(synth);
344 mad_stream_options(stream, decoder->options);
347 switch (decoder->input_func(decoder->cb_data, stream)) {
352 case MAD_FLOW_IGNORE:
354 case MAD_FLOW_CONTINUE:
359 # if defined(USE_ASYNC)
360 if (decoder->mode == MAD_DECODER_MODE_ASYNC) {
361 switch (check_message(decoder)) {
362 case MAD_FLOW_IGNORE:
363 case MAD_FLOW_CONTINUE:
373 if (decoder->header_func) {
374 if (mad_header_decode(&frame->header, stream) == -1) {
375 if (!MAD_RECOVERABLE(stream->error))
378 switch (error_func(error_data, stream, frame)) {
383 case MAD_FLOW_IGNORE:
384 case MAD_FLOW_CONTINUE:
390 switch (decoder->header_func(decoder->cb_data, &frame->header)) {
395 case MAD_FLOW_IGNORE:
397 case MAD_FLOW_CONTINUE:
402 if (mad_frame_decode(frame, stream) == -1) {
403 if (!MAD_RECOVERABLE(stream->error))
406 switch (error_func(error_data, stream, frame)) {
411 case MAD_FLOW_IGNORE:
413 case MAD_FLOW_CONTINUE:
421 if (decoder->filter_func) {
422 switch (decoder->filter_func(decoder->cb_data, stream, frame)) {
427 case MAD_FLOW_IGNORE:
429 case MAD_FLOW_CONTINUE:
434 mad_synth_frame(synth, frame);
436 if (decoder->output_func) {
437 switch (decoder->output_func(decoder->cb_data,
438 &frame->header, &synth->pcm)) {
443 case MAD_FLOW_IGNORE:
444 case MAD_FLOW_CONTINUE:
450 while (stream->error == MAD_ERROR_BUFLEN);
456 mad_synth_finish(synth);
457 mad_frame_finish(frame);
458 mad_stream_finish(stream);
463 # if defined(USE_ASYNC)
465 int run_async(struct mad_decoder *decoder)
468 int ptoc[2], ctop[2], flags;
470 if (pipe(ptoc) == -1)
473 if (pipe(ctop) == -1) {
479 flags = fcntl(ptoc[0], F_GETFL);
481 fcntl(ptoc[0], F_SETFL, flags | O_NONBLOCK) == -1) {
498 decoder->async.pid = pid;
506 decoder->async.in = ctop[0];
507 decoder->async.out = ptoc[1];
517 decoder->async.in = ptoc[0];
518 decoder->async.out = ctop[1];
520 _exit(run_sync(decoder));
528 * NAME: decoder->run()
529 * DESCRIPTION: run the decoder thread either synchronously or asynchronously
531 int mad_decoder_run(struct mad_decoder *decoder, enum mad_decoder_mode mode)
534 int (*run)(struct mad_decoder *) = 0;
536 switch (decoder->mode = mode) {
537 case MAD_DECODER_MODE_SYNC:
541 case MAD_DECODER_MODE_ASYNC:
542 # if defined(USE_ASYNC)
551 decoder->sync = malloc(sizeof(*decoder->sync));
552 if (decoder->sync == 0)
555 result = run(decoder);
564 * NAME: decoder->message()
565 * DESCRIPTION: send a message to and receive a reply from the decoder process
567 int mad_decoder_message(struct mad_decoder *decoder,
568 void *message, unsigned int *len)
570 # if defined(USE_ASYNC)
571 if (decoder->mode != MAD_DECODER_MODE_ASYNC ||
572 send(decoder->async.out, message, *len) != MAD_FLOW_CONTINUE ||
573 receive(decoder->async.in, &message, len) != MAD_FLOW_CONTINUE)