Comment unused code in libmad. Clean up initialization and memset'ing of decoder...
[kugel-rb.git] / apps / codecs / libmad / decoder.c
blobaf8e96962d2d8912252846d64ab0c8eb3714669c
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"
53 #if 0 /* rockbox: not used */
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 *, unsigned int *))
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 int 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 int 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 # else
122 /* Avoid compiler warning */
123 (void)decoder;
124 # endif
126 return 0;
129 # if defined(USE_ASYNC)
130 static
131 enum mad_flow send_io(int fd, void const *data, size_t len)
133 char const *ptr = data;
134 ssize_t count;
136 while (len) {
138 count = write(fd, ptr, len);
139 while (count == -1 && errno == EINTR);
141 if (count == -1)
142 return MAD_FLOW_BREAK;
144 len -= count;
145 ptr += count;
148 return MAD_FLOW_CONTINUE;
151 static
152 enum mad_flow receive_io(int fd, void *buffer, size_t len)
154 char *ptr = buffer;
155 ssize_t count;
157 while (len) {
159 count = read(fd, ptr, len);
160 while (count == -1 && errno == EINTR);
162 if (count == -1)
163 return (errno == EAGAIN) ? MAD_FLOW_IGNORE : MAD_FLOW_BREAK;
164 else if (count == 0)
165 return MAD_FLOW_STOP;
167 len -= count;
168 ptr += count;
171 return MAD_FLOW_CONTINUE;
174 static
175 enum mad_flow receive_io_blocking(int fd, void *buffer, size_t len)
177 int flags, blocking;
178 enum mad_flow result;
180 flags = fcntl(fd, F_GETFL);
181 if (flags == -1)
182 return MAD_FLOW_BREAK;
184 blocking = flags & ~O_NONBLOCK;
186 if (blocking != flags &&
187 fcntl(fd, F_SETFL, blocking) == -1)
188 return MAD_FLOW_BREAK;
190 result = receive_io(fd, buffer, len);
192 if (flags != blocking &&
193 fcntl(fd, F_SETFL, flags) == -1)
194 return MAD_FLOW_BREAK;
196 return result;
199 static
200 enum mad_flow send(int fd, void const *message, unsigned int size)
202 enum mad_flow result;
204 /* send size */
206 result = send_io(fd, &size, sizeof(size));
208 /* send message */
210 if (result == MAD_FLOW_CONTINUE)
211 result = send_io(fd, message, size);
213 return result;
216 static
217 enum mad_flow receive(int fd, void **message, unsigned int *size)
219 enum mad_flow result;
220 unsigned int actual;
222 if (*message == 0)
223 *size = 0;
225 /* receive size */
227 result = receive_io(fd, &actual, sizeof(actual));
229 /* receive message */
231 if (result == MAD_FLOW_CONTINUE) {
232 if (actual > *size)
233 actual -= *size;
234 else {
235 *size = actual;
236 actual = 0;
239 if (*size > 0) {
240 if (*message == 0) {
241 *message = malloc(*size);
242 if (*message == 0)
243 return MAD_FLOW_BREAK;
246 result = receive_io_blocking(fd, *message, *size);
249 /* throw away remainder of message */
251 while (actual && result == MAD_FLOW_CONTINUE) {
252 char sink[256];
253 unsigned int len;
255 len = actual > sizeof(sink) ? sizeof(sink) : actual;
257 result = receive_io_blocking(fd, sink, len);
259 actual -= len;
263 return result;
266 static
267 enum mad_flow check_message(struct mad_decoder *decoder)
269 enum mad_flow result;
270 void *message = 0;
271 unsigned int size;
273 result = receive(decoder->async.in, &message, &size);
275 if (result == MAD_FLOW_CONTINUE) {
276 if (decoder->message_func == 0)
277 size = 0;
278 else {
279 result = decoder->message_func(decoder->cb_data, message, &size);
281 if (result == MAD_FLOW_IGNORE ||
282 result == MAD_FLOW_BREAK)
283 size = 0;
286 if (send(decoder->async.out, message, size) != MAD_FLOW_CONTINUE)
287 result = MAD_FLOW_BREAK;
290 if (message)
291 free(message);
293 return result;
295 # endif
297 static
298 enum mad_flow error_default(void *data, struct mad_stream *stream,
299 struct mad_frame *frame)
301 int *bad_last_frame = data;
303 switch (stream->error) {
304 case MAD_ERROR_BADCRC:
305 if (*bad_last_frame)
306 mad_frame_mute(frame);
307 else
308 *bad_last_frame = 1;
310 return MAD_FLOW_IGNORE;
312 default:
313 return MAD_FLOW_CONTINUE;
317 static
318 int run_sync(struct mad_decoder *decoder)
320 enum mad_flow (*error_func)(void *, struct mad_stream *, struct mad_frame *);
321 void *error_data;
322 int bad_last_frame = 0;
323 struct mad_stream *stream;
324 struct mad_frame *frame;
325 struct mad_synth *synth;
326 int result = 0;
328 if (decoder->input_func == 0)
329 return 0;
331 if (decoder->error_func) {
332 error_func = decoder->error_func;
333 error_data = decoder->cb_data;
335 else {
336 error_func = error_default;
337 error_data = &bad_last_frame;
340 stream = &decoder->sync->stream;
341 frame = &decoder->sync->frame;
342 synth = &decoder->sync->synth;
344 mad_stream_init(stream);
345 mad_frame_init(frame);
346 mad_synth_init(synth);
348 mad_stream_options(stream, decoder->options);
350 do {
351 switch (decoder->input_func(decoder->cb_data, stream)) {
352 case MAD_FLOW_STOP:
353 goto done;
354 case MAD_FLOW_BREAK:
355 goto fail;
356 case MAD_FLOW_IGNORE:
357 continue;
358 case MAD_FLOW_CONTINUE:
359 break;
362 while (1) {
363 # if defined(USE_ASYNC)
364 if (decoder->mode == MAD_DECODER_MODE_ASYNC) {
365 switch (check_message(decoder)) {
366 case MAD_FLOW_IGNORE:
367 case MAD_FLOW_CONTINUE:
368 break;
369 case MAD_FLOW_BREAK:
370 goto fail;
371 case MAD_FLOW_STOP:
372 goto done;
375 # endif
377 if (decoder->header_func) {
378 if (mad_header_decode(&frame->header, stream) == -1) {
379 if (!MAD_RECOVERABLE(stream->error))
380 break;
382 switch (error_func(error_data, stream, frame)) {
383 case MAD_FLOW_STOP:
384 goto done;
385 case MAD_FLOW_BREAK:
386 goto fail;
387 case MAD_FLOW_IGNORE:
388 case MAD_FLOW_CONTINUE:
389 default:
390 continue;
394 switch (decoder->header_func(decoder->cb_data, &frame->header)) {
395 case MAD_FLOW_STOP:
396 goto done;
397 case MAD_FLOW_BREAK:
398 goto fail;
399 case MAD_FLOW_IGNORE:
400 continue;
401 case MAD_FLOW_CONTINUE:
402 break;
406 if (mad_frame_decode(frame, stream) == -1) {
407 if (!MAD_RECOVERABLE(stream->error))
408 break;
410 switch (error_func(error_data, stream, frame)) {
411 case MAD_FLOW_STOP:
412 goto done;
413 case MAD_FLOW_BREAK:
414 goto fail;
415 case MAD_FLOW_IGNORE:
416 break;
417 case MAD_FLOW_CONTINUE:
418 default:
419 continue;
422 else
423 bad_last_frame = 0;
425 if (decoder->filter_func) {
426 switch (decoder->filter_func(decoder->cb_data, stream, frame)) {
427 case MAD_FLOW_STOP:
428 goto done;
429 case MAD_FLOW_BREAK:
430 goto fail;
431 case MAD_FLOW_IGNORE:
432 continue;
433 case MAD_FLOW_CONTINUE:
434 break;
438 mad_synth_frame(synth, frame);
440 if (decoder->output_func) {
441 switch (decoder->output_func(decoder->cb_data,
442 &frame->header, &synth->pcm)) {
443 case MAD_FLOW_STOP:
444 goto done;
445 case MAD_FLOW_BREAK:
446 goto fail;
447 case MAD_FLOW_IGNORE:
448 case MAD_FLOW_CONTINUE:
449 break;
454 while (stream->error == MAD_ERROR_BUFLEN);
456 fail:
457 result = -1;
459 done:
460 mad_synth_finish(synth);
461 mad_frame_finish(frame);
462 mad_stream_finish(stream);
464 return result;
467 # if defined(USE_ASYNC)
468 static
469 int run_async(struct mad_decoder *decoder)
471 pid_t pid;
472 int ptoc[2], ctop[2], flags;
474 if (pipe(ptoc) == -1)
475 return -1;
477 if (pipe(ctop) == -1) {
478 close(ptoc[0]);
479 close(ptoc[1]);
480 return -1;
483 flags = fcntl(ptoc[0], F_GETFL);
484 if (flags == -1 ||
485 fcntl(ptoc[0], F_SETFL, flags | O_NONBLOCK) == -1) {
486 close(ctop[0]);
487 close(ctop[1]);
488 close(ptoc[0]);
489 close(ptoc[1]);
490 return -1;
493 pid = fork();
494 if (pid == -1) {
495 close(ctop[0]);
496 close(ctop[1]);
497 close(ptoc[0]);
498 close(ptoc[1]);
499 return -1;
502 decoder->async.pid = pid;
504 if (pid) {
505 /* parent */
507 close(ptoc[0]);
508 close(ctop[1]);
510 decoder->async.in = ctop[0];
511 decoder->async.out = ptoc[1];
513 return 0;
516 /* child */
518 close(ptoc[1]);
519 close(ctop[0]);
521 decoder->async.in = ptoc[0];
522 decoder->async.out = ctop[1];
524 _exit(run_sync(decoder));
526 /* not reached */
527 return -1;
529 # endif
532 * NAME: decoder->run()
533 * DESCRIPTION: run the decoder thread either synchronously or asynchronously
535 int mad_decoder_run(struct mad_decoder *decoder, enum mad_decoder_mode mode)
537 int result;
538 int (*run)(struct mad_decoder *) = 0;
540 switch (decoder->mode = mode) {
541 case MAD_DECODER_MODE_SYNC:
542 run = run_sync;
543 break;
545 case MAD_DECODER_MODE_ASYNC:
546 # if defined(USE_ASYNC)
547 run = run_async;
548 # endif
549 break;
552 if (run == 0)
553 return -1;
555 decoder->sync = malloc(sizeof(*decoder->sync));
556 if (decoder->sync == 0)
557 return -1;
559 result = run(decoder);
561 free(decoder->sync);
562 decoder->sync = 0;
564 return result;
568 * NAME: decoder->message()
569 * DESCRIPTION: send a message to and receive a reply from the decoder process
571 int mad_decoder_message(struct mad_decoder *decoder,
572 void *message, unsigned int *len)
574 # if defined(USE_ASYNC)
575 if (decoder->mode != MAD_DECODER_MODE_ASYNC ||
576 send(decoder->async.out, message, *len) != MAD_FLOW_CONTINUE ||
577 receive(decoder->async.in, &message, len) != MAD_FLOW_CONTINUE)
578 return -1;
580 return 0;
581 # else
582 /* Avoid compiler warnings */
583 (void)decoder;
584 (void)message;
585 (void)len;
586 return -1;
587 # endif
589 #endif