Volume is shown numerical after a change
[kugel-rb.git] / uisimulator / common / libmad / decoder.c
blob3b00179d280b1d101df3bd885bafde5a1d576feb
1 /*
2 * libmad - MPEG audio decoder library
3 * Copyright (C) 2000-2001 Robert Leslie
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 "madconfig.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 # include <stdlib.h>
46 # ifdef HAVE_ERRNO_H
47 # include <errno.h>
48 # endif
50 # include "stream.h"
51 # include "frame.h"
52 # include "synth.h"
53 # include "decoder.h"
55 void mad_decoder_init(struct mad_decoder *decoder, void *data,
56 enum mad_flow (*input_func)(void *,
57 struct mad_stream *),
58 enum mad_flow (*header_func)(void *,
59 struct mad_header const *),
60 enum mad_flow (*filter_func)(void *,
61 struct mad_stream const *,
62 struct mad_frame *),
63 enum mad_flow (*output_func)(void *,
64 struct mad_header const *,
65 struct mad_pcm *),
66 enum mad_flow (*error_func)(void *,
67 struct mad_stream *,
68 struct mad_frame *),
69 enum mad_flow (*message_func)(void *,
70 void *, unsigned int *))
72 decoder->mode = -1;
74 decoder->options = 0;
76 decoder->async.pid = 0;
77 decoder->async.in = -1;
78 decoder->async.out = -1;
80 decoder->sync = 0;
82 decoder->cb_data = data;
84 decoder->input_func = input_func;
85 decoder->header_func = header_func;
86 decoder->filter_func = filter_func;
87 decoder->output_func = output_func;
88 decoder->error_func = error_func;
89 decoder->message_func = message_func;
92 int mad_decoder_finish(struct mad_decoder *decoder)
94 # if defined(USE_ASYNC)
95 if (decoder->mode == MAD_DECODER_MODE_ASYNC && decoder->async.pid) {
96 pid_t pid;
97 int status;
99 close(decoder->async.in);
102 pid = waitpid(decoder->async.pid, &status, 0);
103 while (pid == -1 && errno == EINTR);
105 decoder->mode = -1;
107 close(decoder->async.out);
109 decoder->async.pid = 0;
110 decoder->async.in = -1;
111 decoder->async.out = -1;
113 if (pid == -1)
114 return -1;
116 return (!WIFEXITED(status) || WEXITSTATUS(status)) ? -1 : 0;
118 # endif
120 return 0;
123 # if defined(USE_ASYNC)
124 static
125 enum mad_flow send_io(int fd, void const *data, size_t len)
127 char const *ptr = data;
128 ssize_t count;
130 while (len) {
132 count = write(fd, ptr, len);
133 while (count == -1 && errno == EINTR);
135 if (count == -1)
136 return MAD_FLOW_BREAK;
138 len -= count;
139 ptr += count;
142 return MAD_FLOW_CONTINUE;
145 static
146 enum mad_flow receive_io(int fd, void *buffer, size_t len)
148 char *ptr = buffer;
149 ssize_t count;
151 while (len) {
153 count = read(fd, ptr, len);
154 while (count == -1 && errno == EINTR);
156 if (count == -1)
157 return (errno == EAGAIN) ? MAD_FLOW_IGNORE : MAD_FLOW_BREAK;
158 else if (count == 0)
159 return MAD_FLOW_STOP;
161 len -= count;
162 ptr += count;
165 return MAD_FLOW_CONTINUE;
168 static
169 enum mad_flow receive_io_blocking(int fd, void *buffer, size_t len)
171 int flags, blocking;
172 enum mad_flow result;
174 flags = fcntl(fd, F_GETFL);
175 if (flags == -1)
176 return MAD_FLOW_BREAK;
178 blocking = flags & ~O_NONBLOCK;
180 if (blocking != flags &&
181 fcntl(fd, F_SETFL, blocking) == -1)
182 return MAD_FLOW_BREAK;
184 result = receive_io(fd, buffer, len);
186 if (flags != blocking &&
187 fcntl(fd, F_SETFL, flags) == -1)
188 return MAD_FLOW_BREAK;
190 return result;
193 static
194 enum mad_flow send(int fd, void const *message, unsigned int size)
196 enum mad_flow result;
198 /* send size */
200 result = send_io(fd, &size, sizeof(size));
202 /* send message */
204 if (result == MAD_FLOW_CONTINUE)
205 result = send_io(fd, message, size);
207 return result;
210 static
211 enum mad_flow receive(int fd, void **message, unsigned int *size)
213 enum mad_flow result;
214 unsigned int actual;
216 if (*message == 0)
217 *size = 0;
219 /* receive size */
221 result = receive_io(fd, &actual, sizeof(actual));
223 /* receive message */
225 if (result == MAD_FLOW_CONTINUE) {
226 if (actual > *size)
227 actual -= *size;
228 else {
229 *size = actual;
230 actual = 0;
233 if (*size > 0) {
234 if (*message == 0) {
235 *message = malloc(*size);
236 if (*message == 0)
237 return MAD_FLOW_BREAK;
240 result = receive_io_blocking(fd, *message, *size);
243 /* throw away remainder of message */
245 while (actual && result == MAD_FLOW_CONTINUE) {
246 char sink[256];
247 unsigned int len;
249 len = actual > sizeof(sink) ? sizeof(sink) : actual;
251 result = receive_io_blocking(fd, sink, len);
253 actual -= len;
257 return result;
260 static
261 enum mad_flow check_message(struct mad_decoder *decoder)
263 enum mad_flow result;
264 void *message = 0;
265 unsigned int size;
267 result = receive(decoder->async.in, &message, &size);
269 if (result == MAD_FLOW_CONTINUE) {
270 if (decoder->message_func == 0)
271 size = 0;
272 else {
273 result = decoder->message_func(decoder->cb_data, message, &size);
275 if (result == MAD_FLOW_IGNORE ||
276 result == MAD_FLOW_BREAK)
277 size = 0;
280 if (send(decoder->async.out, message, size) != MAD_FLOW_CONTINUE)
281 result = MAD_FLOW_BREAK;
284 if (message)
285 free(message);
287 return result;
289 # endif
291 static
292 enum mad_flow error_default(void *data, struct mad_stream *stream,
293 struct mad_frame *frame)
295 int *bad_last_frame = data;
297 switch (stream->error) {
298 case MAD_ERROR_BADCRC:
299 if (*bad_last_frame)
300 mad_frame_mute(frame);
301 else
302 *bad_last_frame = 1;
304 return MAD_FLOW_IGNORE;
306 default:
307 return MAD_FLOW_CONTINUE;
311 static
312 int run_sync(struct mad_decoder *decoder)
314 enum mad_flow (*error_func)(void *, struct mad_stream *, struct mad_frame *);
315 void *error_data;
316 int bad_last_frame = 0;
317 struct mad_stream *stream;
318 struct mad_frame *frame;
319 struct mad_synth *synth;
320 int result = 0;
322 if (decoder->input_func == 0)
323 return 0;
325 if (decoder->error_func) {
326 error_func = decoder->error_func;
327 error_data = decoder->cb_data;
329 else {
330 error_func = error_default;
331 error_data = &bad_last_frame;
334 stream = &decoder->sync->stream;
335 frame = &decoder->sync->frame;
336 synth = &decoder->sync->synth;
338 mad_stream_init(stream);
339 mad_frame_init(frame);
340 mad_synth_init(synth);
342 mad_stream_options(stream, decoder->options);
344 do {
345 switch (decoder->input_func(decoder->cb_data, stream)) {
346 case MAD_FLOW_STOP:
347 goto done;
348 case MAD_FLOW_BREAK:
349 goto fail;
350 case MAD_FLOW_IGNORE:
351 continue;
352 case MAD_FLOW_CONTINUE:
353 break;
356 while (1) {
357 # if defined(USE_ASYNC)
358 if (decoder->mode == MAD_DECODER_MODE_ASYNC) {
359 switch (check_message(decoder)) {
360 case MAD_FLOW_IGNORE:
361 case MAD_FLOW_CONTINUE:
362 break;
363 case MAD_FLOW_BREAK:
364 goto fail;
365 case MAD_FLOW_STOP:
366 goto done;
369 # endif
371 if (decoder->header_func) {
372 if (mad_header_decode(&frame->header, stream) == -1) {
373 if (!MAD_RECOVERABLE(stream->error))
374 break;
376 switch (error_func(error_data, stream, frame)) {
377 case MAD_FLOW_STOP:
378 goto done;
379 case MAD_FLOW_BREAK:
380 goto fail;
381 case MAD_FLOW_IGNORE:
382 case MAD_FLOW_CONTINUE:
383 default:
384 continue;
388 switch (decoder->header_func(decoder->cb_data, &frame->header)) {
389 case MAD_FLOW_STOP:
390 goto done;
391 case MAD_FLOW_BREAK:
392 goto fail;
393 case MAD_FLOW_IGNORE:
394 continue;
395 case MAD_FLOW_CONTINUE:
396 break;
400 if (mad_frame_decode(frame, stream) == -1) {
401 if (!MAD_RECOVERABLE(stream->error))
402 break;
404 switch (error_func(error_data, stream, frame)) {
405 case MAD_FLOW_STOP:
406 goto done;
407 case MAD_FLOW_BREAK:
408 goto fail;
409 case MAD_FLOW_IGNORE:
410 break;
411 case MAD_FLOW_CONTINUE:
412 default:
413 continue;
416 else
417 bad_last_frame = 0;
419 if (decoder->filter_func) {
420 switch (decoder->filter_func(decoder->cb_data, stream, frame)) {
421 case MAD_FLOW_STOP:
422 goto done;
423 case MAD_FLOW_BREAK:
424 goto fail;
425 case MAD_FLOW_IGNORE:
426 continue;
427 case MAD_FLOW_CONTINUE:
428 break;
432 mad_synth_frame(synth, frame);
434 if (decoder->output_func) {
435 switch (decoder->output_func(decoder->cb_data,
436 &frame->header, &synth->pcm)) {
437 case MAD_FLOW_STOP:
438 goto done;
439 case MAD_FLOW_BREAK:
440 goto fail;
441 case MAD_FLOW_IGNORE:
442 case MAD_FLOW_CONTINUE:
443 break;
448 while (stream->error == MAD_ERROR_BUFLEN);
450 fail:
451 result = -1;
453 done:
454 mad_synth_finish(synth);
455 mad_frame_finish(frame);
456 mad_stream_finish(stream);
458 return result;
461 # if defined(USE_ASYNC)
462 static
463 int run_async(struct mad_decoder *decoder)
465 pid_t pid;
466 int ptoc[2], ctop[2], flags;
468 if (pipe(ptoc) == -1)
469 return -1;
471 if (pipe(ctop) == -1) {
472 close(ptoc[0]);
473 close(ptoc[1]);
474 return -1;
477 flags = fcntl(ptoc[0], F_GETFL);
478 if (flags == -1 ||
479 fcntl(ptoc[0], F_SETFL, flags | O_NONBLOCK) == -1) {
480 close(ctop[0]);
481 close(ctop[1]);
482 close(ptoc[0]);
483 close(ptoc[1]);
484 return -1;
487 pid = fork();
488 if (pid == -1) {
489 close(ctop[0]);
490 close(ctop[1]);
491 close(ptoc[0]);
492 close(ptoc[1]);
493 return -1;
496 decoder->async.pid = pid;
498 if (pid) {
499 /* parent */
501 close(ptoc[0]);
502 close(ctop[1]);
504 decoder->async.in = ctop[0];
505 decoder->async.out = ptoc[1];
507 return 0;
510 /* child */
512 close(ptoc[1]);
513 close(ctop[0]);
515 decoder->async.in = ptoc[0];
516 decoder->async.out = ctop[1];
518 _exit(run_sync(decoder));
520 /* not reached */
521 return -1;
523 # endif
525 int mad_decoder_run(struct mad_decoder *decoder, enum mad_decoder_mode mode)
527 int result;
528 int (*run)(struct mad_decoder *) = 0;
530 switch (decoder->mode = mode) {
531 case MAD_DECODER_MODE_SYNC:
532 run = run_sync;
533 break;
535 case MAD_DECODER_MODE_ASYNC:
536 # if defined(USE_ASYNC)
537 run = run_async;
538 # endif
539 break;
542 if (run == 0)
543 return -1;
545 decoder->sync = malloc(sizeof(*decoder->sync));
546 if (decoder->sync == 0)
547 return -1;
549 result = run(decoder);
551 free(decoder->sync);
552 decoder->sync = 0;
554 return result;
557 int mad_decoder_message(struct mad_decoder *decoder,
558 void *message, unsigned int *len)
560 # if defined(USE_ASYNC)
561 if (decoder->mode != MAD_DECODER_MODE_ASYNC ||
562 send(decoder->async.out, message, *len) != MAD_FLOW_CONTINUE ||
563 receive(decoder->async.in, &message, len) != MAD_FLOW_CONTINUE)
564 return -1;
566 return 0;
567 # else
568 return -1;
569 # endif