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>
56 * NAME: decoder->init()
57 * DESCRIPTION: initialize a decoder object with callback routines
59 void mad_decoder_init(struct mad_decoder *decoder, void *data,
60 enum mad_flow (*input_func)(void *,
62 enum mad_flow (*header_func)(void *,
63 struct mad_header const *),
64 enum mad_flow (*filter_func)(void *,
65 struct mad_stream const *,
67 enum mad_flow (*output_func)(void *,
68 struct mad_header const *,
70 enum mad_flow (*error_func)(void *,
73 enum mad_flow (*message_func)(void *,
74 void *, unsigned int *))
80 decoder->async.pid = 0;
81 decoder->async.in = -1;
82 decoder->async.out = -1;
86 decoder->cb_data = data;
88 decoder->input_func = input_func;
89 decoder->header_func = header_func;
90 decoder->filter_func = filter_func;
91 decoder->output_func = output_func;
92 decoder->error_func = error_func;
93 decoder->message_func = message_func;
96 int mad_decoder_finish(struct mad_decoder *decoder)
98 # if defined(USE_ASYNC)
99 if (decoder->mode == MAD_DECODER_MODE_ASYNC && decoder->async.pid) {
103 close(decoder->async.in);
106 pid = waitpid(decoder->async.pid, &status, 0);
107 while (pid == -1 && errno == EINTR);
111 close(decoder->async.out);
113 decoder->async.pid = 0;
114 decoder->async.in = -1;
115 decoder->async.out = -1;
120 return (!WIFEXITED(status) || WEXITSTATUS(status)) ? -1 : 0;
127 # if defined(USE_ASYNC)
129 enum mad_flow send_io(int fd, void const *data, size_t len)
131 char const *ptr = data;
136 count = write(fd, ptr, len);
137 while (count == -1 && errno == EINTR);
140 return MAD_FLOW_BREAK;
146 return MAD_FLOW_CONTINUE;
150 enum mad_flow receive_io(int fd, void *buffer, size_t len)
157 count = read(fd, ptr, len);
158 while (count == -1 && errno == EINTR);
161 return (errno == EAGAIN) ? MAD_FLOW_IGNORE : MAD_FLOW_BREAK;
163 return MAD_FLOW_STOP;
169 return MAD_FLOW_CONTINUE;
173 enum mad_flow receive_io_blocking(int fd, void *buffer, size_t len)
176 enum mad_flow result;
178 flags = fcntl(fd, F_GETFL);
180 return MAD_FLOW_BREAK;
182 blocking = flags & ~O_NONBLOCK;
184 if (blocking != flags &&
185 fcntl(fd, F_SETFL, blocking) == -1)
186 return MAD_FLOW_BREAK;
188 result = receive_io(fd, buffer, len);
190 if (flags != blocking &&
191 fcntl(fd, F_SETFL, flags) == -1)
192 return MAD_FLOW_BREAK;
198 enum mad_flow send(int fd, void const *message, unsigned int size)
200 enum mad_flow result;
204 result = send_io(fd, &size, sizeof(size));
208 if (result == MAD_FLOW_CONTINUE)
209 result = send_io(fd, message, size);
215 enum mad_flow receive(int fd, void **message, unsigned int *size)
217 enum mad_flow result;
225 result = receive_io(fd, &actual, sizeof(actual));
227 /* receive message */
229 if (result == MAD_FLOW_CONTINUE) {
239 *message = malloc(*size);
241 return MAD_FLOW_BREAK;
244 result = receive_io_blocking(fd, *message, *size);
247 /* throw away remainder of message */
249 while (actual && result == MAD_FLOW_CONTINUE) {
253 len = actual > sizeof(sink) ? sizeof(sink) : actual;
255 result = receive_io_blocking(fd, sink, len);
265 enum mad_flow check_message(struct mad_decoder *decoder)
267 enum mad_flow result;
271 result = receive(decoder->async.in, &message, &size);
273 if (result == MAD_FLOW_CONTINUE) {
274 if (decoder->message_func == 0)
277 result = decoder->message_func(decoder->cb_data, message, &size);
279 if (result == MAD_FLOW_IGNORE ||
280 result == MAD_FLOW_BREAK)
284 if (send(decoder->async.out, message, size) != MAD_FLOW_CONTINUE)
285 result = MAD_FLOW_BREAK;
296 enum mad_flow error_default(void *data, struct mad_stream *stream,
297 struct mad_frame *frame)
299 int *bad_last_frame = data;
301 switch (stream->error) {
302 case MAD_ERROR_BADCRC:
304 mad_frame_mute(frame);
308 return MAD_FLOW_IGNORE;
311 return MAD_FLOW_CONTINUE;
316 int run_sync(struct mad_decoder *decoder)
318 enum mad_flow (*error_func)(void *, struct mad_stream *, struct mad_frame *);
320 int bad_last_frame = 0;
321 struct mad_stream *stream;
322 struct mad_frame *frame;
323 struct mad_synth *synth;
326 if (decoder->input_func == 0)
329 if (decoder->error_func) {
330 error_func = decoder->error_func;
331 error_data = decoder->cb_data;
334 error_func = error_default;
335 error_data = &bad_last_frame;
338 stream = &decoder->sync->stream;
339 frame = &decoder->sync->frame;
340 synth = &decoder->sync->synth;
342 mad_stream_init(stream);
343 mad_frame_init(frame);
344 mad_synth_init(synth);
346 mad_stream_options(stream, decoder->options);
349 switch (decoder->input_func(decoder->cb_data, stream)) {
354 case MAD_FLOW_IGNORE:
356 case MAD_FLOW_CONTINUE:
361 # if defined(USE_ASYNC)
362 if (decoder->mode == MAD_DECODER_MODE_ASYNC) {
363 switch (check_message(decoder)) {
364 case MAD_FLOW_IGNORE:
365 case MAD_FLOW_CONTINUE:
375 if (decoder->header_func) {
376 if (mad_header_decode(&frame->header, stream) == -1) {
377 if (!MAD_RECOVERABLE(stream->error))
380 switch (error_func(error_data, stream, frame)) {
385 case MAD_FLOW_IGNORE:
386 case MAD_FLOW_CONTINUE:
392 switch (decoder->header_func(decoder->cb_data, &frame->header)) {
397 case MAD_FLOW_IGNORE:
399 case MAD_FLOW_CONTINUE:
404 if (mad_frame_decode(frame, stream) == -1) {
405 if (!MAD_RECOVERABLE(stream->error))
408 switch (error_func(error_data, stream, frame)) {
413 case MAD_FLOW_IGNORE:
415 case MAD_FLOW_CONTINUE:
423 if (decoder->filter_func) {
424 switch (decoder->filter_func(decoder->cb_data, stream, frame)) {
429 case MAD_FLOW_IGNORE:
431 case MAD_FLOW_CONTINUE:
436 mad_synth_frame(synth, frame);
438 if (decoder->output_func) {
439 switch (decoder->output_func(decoder->cb_data,
440 &frame->header, &synth->pcm)) {
445 case MAD_FLOW_IGNORE:
446 case MAD_FLOW_CONTINUE:
452 while (stream->error == MAD_ERROR_BUFLEN);
458 mad_synth_finish(synth);
459 mad_frame_finish(frame);
460 mad_stream_finish(stream);
465 # if defined(USE_ASYNC)
467 int run_async(struct mad_decoder *decoder)
470 int ptoc[2], ctop[2], flags;
472 if (pipe(ptoc) == -1)
475 if (pipe(ctop) == -1) {
481 flags = fcntl(ptoc[0], F_GETFL);
483 fcntl(ptoc[0], F_SETFL, flags | O_NONBLOCK) == -1) {
500 decoder->async.pid = pid;
508 decoder->async.in = ctop[0];
509 decoder->async.out = ptoc[1];
519 decoder->async.in = ptoc[0];
520 decoder->async.out = ctop[1];
522 _exit(run_sync(decoder));
530 * NAME: decoder->run()
531 * DESCRIPTION: run the decoder thread either synchronously or asynchronously
533 int mad_decoder_run(struct mad_decoder *decoder, enum mad_decoder_mode mode)
536 int (*run)(struct mad_decoder *) = 0;
538 switch (decoder->mode = mode) {
539 case MAD_DECODER_MODE_SYNC:
543 case MAD_DECODER_MODE_ASYNC:
544 # if defined(USE_ASYNC)
553 decoder->sync = malloc(sizeof(*decoder->sync));
554 if (decoder->sync == 0)
557 result = run(decoder);
566 * NAME: decoder->message()
567 * DESCRIPTION: send a message to and receive a reply from the decoder process
569 int mad_decoder_message(struct mad_decoder *decoder,
570 void *message, unsigned int *len)
572 # if defined(USE_ASYNC)
573 if (decoder->mode != MAD_DECODER_MODE_ASYNC ||
574 send(decoder->async.out, message, *len) != MAD_FLOW_CONTINUE ||
575 receive(decoder->async.in, &message, len) != MAD_FLOW_CONTINUE)