minor changes
[libogc.git] / libmad / decoder.c
blob4861aa3cc4741bdaf2390e3dc1f5e88fc1753b07
1 /*
2 * libmad - MPEG audio decoder library
3 * Copyright (C) 2000-2003 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
21 # ifdef HAVE_CONFIG_H
22 # include "config.h"
23 # endif
25 # include "global.h"
27 # ifdef HAVE_SYS_TYPES_H
28 # include <sys/types.h>
29 # endif
31 # ifdef HAVE_SYS_WAIT_H
32 # include <sys/wait.h>
33 # endif
35 # ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 # endif
39 # ifdef HAVE_FCNTL_H
40 # include <fcntl.h>
41 # endif
43 # include <stdlib.h>
45 # ifdef HAVE_ERRNO_H
46 # include <errno.h>
47 # endif
49 # include "stream.h"
50 # include "frame.h"
51 # include "synth.h"
52 # include "decoder.h"
55 * NAME: decoder->init()
56 * DESCRIPTION: initialize a decoder object with callback routines
58 void mad_decoder_init(struct mad_decoder *decoder, void *data,
59 enum mad_flow (*input_func)(void *,
60 struct mad_stream *),
61 enum mad_flow (*header_func)(void *,
62 struct mad_header const *),
63 enum mad_flow (*filter_func)(void *,
64 struct mad_stream const *,
65 struct mad_frame *),
66 enum mad_flow (*output_func)(void *,
67 struct mad_header const *,
68 struct mad_pcm *),
69 enum mad_flow (*error_func)(void *,
70 struct mad_stream *,
71 struct mad_frame *),
72 enum mad_flow (*message_func)(void *,
73 void *, u32 *))
75 decoder->mode = -1;
77 decoder->options = 0;
79 decoder->async.pid = 0;
80 decoder->async.in = -1;
81 decoder->async.out = -1;
83 decoder->sync = 0;
85 decoder->cb_data = data;
87 decoder->input_func = input_func;
88 decoder->header_func = header_func;
89 decoder->filter_func = filter_func;
90 decoder->output_func = output_func;
91 decoder->error_func = error_func;
92 decoder->message_func = message_func;
95 s32 mad_decoder_finish(struct mad_decoder *decoder)
97 # if defined(USE_ASYNC)
98 if (decoder->mode == MAD_DECODER_MODE_ASYNC && decoder->async.pid) {
99 pid_t pid;
100 s32 status;
102 close(decoder->async.in);
105 pid = waitpid(decoder->async.pid, &status, 0);
106 while (pid == -1 && errno == EINTR);
108 decoder->mode = -1;
110 close(decoder->async.out);
112 decoder->async.pid = 0;
113 decoder->async.in = -1;
114 decoder->async.out = -1;
116 if (pid == -1)
117 return -1;
119 return (!WIFEXITED(status) || WEXITSTATUS(status)) ? -1 : 0;
121 # endif
123 return 0;
126 # if defined(USE_ASYNC)
127 static
128 enum mad_flow send_io(s32 fd, void const *data, size_t len)
130 s8 const *ptr = data;
131 ssize_t count;
133 while (len) {
135 count = write(fd, ptr, len);
136 while (count == -1 && errno == EINTR);
138 if (count == -1)
139 return MAD_FLOW_BREAK;
141 len -= count;
142 ptr += count;
145 return MAD_FLOW_CONTINUE;
148 static
149 enum mad_flow receive_io(s32 fd, void *buffer, size_t len)
151 s8 *ptr = buffer;
152 ssize_t count;
154 while (len) {
156 count = read(fd, ptr, len);
157 while (count == -1 && errno == EINTR);
159 if (count == -1)
160 return (errno == EAGAIN) ? MAD_FLOW_IGNORE : MAD_FLOW_BREAK;
161 else if (count == 0)
162 return MAD_FLOW_STOP;
164 len -= count;
165 ptr += count;
168 return MAD_FLOW_CONTINUE;
171 static
172 enum mad_flow receive_io_blocking(s32 fd, void *buffer, size_t len)
174 s32 flags, blocking;
175 enum mad_flow result;
177 flags = fcntl(fd, F_GETFL);
178 if (flags == -1)
179 return MAD_FLOW_BREAK;
181 blocking = flags & ~O_NONBLOCK;
183 if (blocking != flags &&
184 fcntl(fd, F_SETFL, blocking) == -1)
185 return MAD_FLOW_BREAK;
187 result = receive_io(fd, buffer, len);
189 if (flags != blocking &&
190 fcntl(fd, F_SETFL, flags) == -1)
191 return MAD_FLOW_BREAK;
193 return result;
196 static
197 enum mad_flow send(s32 fd, void const *message, u32 size)
199 enum mad_flow result;
201 /* send size */
203 result = send_io(fd, &size, sizeof(size));
205 /* send message */
207 if (result == MAD_FLOW_CONTINUE)
208 result = send_io(fd, message, size);
210 return result;
213 static
214 enum mad_flow receive(s32 fd, void **message, u32 *size)
216 enum mad_flow result;
217 u32 actual;
219 if (*message == 0)
220 *size = 0;
222 /* receive size */
224 result = receive_io(fd, &actual, sizeof(actual));
226 /* receive message */
228 if (result == MAD_FLOW_CONTINUE) {
229 if (actual > *size)
230 actual -= *size;
231 else {
232 *size = actual;
233 actual = 0;
236 if (*size > 0) {
237 if (*message == 0) {
238 *message = malloc(*size);
239 if (*message == 0)
240 return MAD_FLOW_BREAK;
243 result = receive_io_blocking(fd, *message, *size);
246 /* throw away remainder of message */
248 while (actual && result == MAD_FLOW_CONTINUE) {
249 s8 sink[256];
250 u32 len;
252 len = actual > sizeof(sink) ? sizeof(sink) : actual;
254 result = receive_io_blocking(fd, sink, len);
256 actual -= len;
260 return result;
263 static
264 enum mad_flow check_message(struct mad_decoder *decoder)
266 enum mad_flow result;
267 void *message = 0;
268 u32 size;
270 result = receive(decoder->async.in, &message, &size);
272 if (result == MAD_FLOW_CONTINUE) {
273 if (decoder->message_func == 0)
274 size = 0;
275 else {
276 result = decoder->message_func(decoder->cb_data, message, &size);
278 if (result == MAD_FLOW_IGNORE ||
279 result == MAD_FLOW_BREAK)
280 size = 0;
283 if (send(decoder->async.out, message, size) != MAD_FLOW_CONTINUE)
284 result = MAD_FLOW_BREAK;
287 if (message)
288 free(message);
290 return result;
292 # endif
294 static
295 enum mad_flow error_default(void *data, struct mad_stream *stream,
296 struct mad_frame *frame)
298 s32 *bad_last_frame = data;
300 switch (stream->error) {
301 case MAD_ERROR_BADCRC:
302 if (*bad_last_frame)
303 mad_frame_mute(frame);
304 else
305 *bad_last_frame = 1;
307 return MAD_FLOW_IGNORE;
309 default:
310 return MAD_FLOW_CONTINUE;
314 static
315 s32 run_sync(struct mad_decoder *decoder)
317 enum mad_flow (*error_func)(void *, struct mad_stream *, struct mad_frame *);
318 void *error_data;
319 s32 bad_last_frame = 0;
320 struct mad_stream *stream;
321 struct mad_frame *frame;
322 struct mad_synth *synth;
323 s32 result = 0;
325 if (decoder->input_func == 0)
326 return 0;
328 if (decoder->error_func) {
329 error_func = decoder->error_func;
330 error_data = decoder->cb_data;
332 else {
333 error_func = error_default;
334 error_data = &bad_last_frame;
337 stream = &decoder->sync->stream;
338 frame = &decoder->sync->frame;
339 synth = &decoder->sync->synth;
341 mad_stream_init(stream);
342 mad_frame_init(frame);
343 mad_synth_init(synth);
345 mad_stream_options(stream, decoder->options);
347 do {
348 switch (decoder->input_func(decoder->cb_data, stream)) {
349 case MAD_FLOW_STOP:
350 goto done;
351 case MAD_FLOW_BREAK:
352 goto fail;
353 case MAD_FLOW_IGNORE:
354 continue;
355 case MAD_FLOW_CONTINUE:
356 break;
359 while (1) {
360 # if defined(USE_ASYNC)
361 if (decoder->mode == MAD_DECODER_MODE_ASYNC) {
362 switch (check_message(decoder)) {
363 case MAD_FLOW_IGNORE:
364 case MAD_FLOW_CONTINUE:
365 break;
366 case MAD_FLOW_BREAK:
367 goto fail;
368 case MAD_FLOW_STOP:
369 goto done;
372 # endif
374 if (decoder->header_func) {
375 if (mad_header_decode(&frame->header, stream) == -1) {
376 if (!MAD_RECOVERABLE(stream->error))
377 break;
379 switch (error_func(error_data, stream, frame)) {
380 case MAD_FLOW_STOP:
381 goto done;
382 case MAD_FLOW_BREAK:
383 goto fail;
384 case MAD_FLOW_IGNORE:
385 case MAD_FLOW_CONTINUE:
386 default:
387 continue;
391 switch (decoder->header_func(decoder->cb_data, &frame->header)) {
392 case MAD_FLOW_STOP:
393 goto done;
394 case MAD_FLOW_BREAK:
395 goto fail;
396 case MAD_FLOW_IGNORE:
397 continue;
398 case MAD_FLOW_CONTINUE:
399 break;
403 if (mad_frame_decode(frame, stream) == -1) {
404 if (!MAD_RECOVERABLE(stream->error))
405 break;
407 switch (error_func(error_data, stream, frame)) {
408 case MAD_FLOW_STOP:
409 goto done;
410 case MAD_FLOW_BREAK:
411 goto fail;
412 case MAD_FLOW_IGNORE:
413 break;
414 case MAD_FLOW_CONTINUE:
415 default:
416 continue;
419 else
420 bad_last_frame = 0;
422 if (decoder->filter_func) {
423 switch (decoder->filter_func(decoder->cb_data, stream, frame)) {
424 case MAD_FLOW_STOP:
425 goto done;
426 case MAD_FLOW_BREAK:
427 goto fail;
428 case MAD_FLOW_IGNORE:
429 continue;
430 case MAD_FLOW_CONTINUE:
431 break;
435 mad_synth_frame(synth, frame);
437 if (decoder->output_func) {
438 switch (decoder->output_func(decoder->cb_data,
439 &frame->header, &synth->pcm)) {
440 case MAD_FLOW_STOP:
441 goto done;
442 case MAD_FLOW_BREAK:
443 goto fail;
444 case MAD_FLOW_IGNORE:
445 case MAD_FLOW_CONTINUE:
446 break;
451 while (stream->error == MAD_ERROR_BUFLEN);
453 fail:
454 result = -1;
456 done:
457 mad_synth_finish(synth);
458 mad_frame_finish(frame);
459 mad_stream_finish(stream);
461 return result;
464 # if defined(USE_ASYNC)
465 static
466 s32 run_async(struct mad_decoder *decoder)
468 pid_t pid;
469 s32 ptoc[2], ctop[2], flags;
471 if (pipe(ptoc) == -1)
472 return -1;
474 if (pipe(ctop) == -1) {
475 close(ptoc[0]);
476 close(ptoc[1]);
477 return -1;
480 flags = fcntl(ptoc[0], F_GETFL);
481 if (flags == -1 ||
482 fcntl(ptoc[0], F_SETFL, flags | O_NONBLOCK) == -1) {
483 close(ctop[0]);
484 close(ctop[1]);
485 close(ptoc[0]);
486 close(ptoc[1]);
487 return -1;
490 pid = fork();
491 if (pid == -1) {
492 close(ctop[0]);
493 close(ctop[1]);
494 close(ptoc[0]);
495 close(ptoc[1]);
496 return -1;
499 decoder->async.pid = pid;
501 if (pid) {
502 /* parent */
504 close(ptoc[0]);
505 close(ctop[1]);
507 decoder->async.in = ctop[0];
508 decoder->async.out = ptoc[1];
510 return 0;
513 /* child */
515 close(ptoc[1]);
516 close(ctop[0]);
518 decoder->async.in = ptoc[0];
519 decoder->async.out = ctop[1];
521 _exit(run_sync(decoder));
523 /* not reached */
524 return -1;
526 # endif
529 * NAME: decoder->run()
530 * DESCRIPTION: run the decoder thread either synchronously or asynchronously
532 s32 mad_decoder_run(struct mad_decoder *decoder, enum mad_decoder_mode mode)
534 s32 result;
535 s32 (*run)(struct mad_decoder *) = 0;
537 switch (decoder->mode = mode) {
538 case MAD_DECODER_MODE_SYNC:
539 run = run_sync;
540 break;
542 case MAD_DECODER_MODE_ASYNC:
543 # if defined(USE_ASYNC)
544 run = run_async;
545 # endif
546 break;
549 if (run == 0)
550 return -1;
552 decoder->sync = malloc(sizeof(*decoder->sync));
553 if (decoder->sync == 0)
554 return -1;
556 result = run(decoder);
558 free(decoder->sync);
559 decoder->sync = 0;
561 return result;
565 * NAME: decoder->message()
566 * DESCRIPTION: send a message to and receive a reply from the decoder process
568 s32 mad_decoder_message(struct mad_decoder *decoder,
569 void *message, u32 *len)
571 # if defined(USE_ASYNC)
572 if (decoder->mode != MAD_DECODER_MODE_ASYNC ||
573 send(decoder->async.out, message, *len) != MAD_FLOW_CONTINUE ||
574 receive(decoder->async.in, &message, len) != MAD_FLOW_CONTINUE)
575 return -1;
577 return 0;
578 # else
579 return -1;
580 # endif