1 /*
2 * libmad - MPEG audio decoder library
3 * Copyright (C) 2000-2004 Underbit Technologies, Inc.
4 *
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.
9 *
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.
14 *
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
18 *
19 * $Id: decoder.c,v 1.22 2004/01/23 09:41:32 rob Exp $
20 */
21
22 # ifdef HAVE_CONFIG_H
23 # include "config.h"
24 # endif
25
26 # include "global.h"
27
28 # ifdef HAVE_SYS_TYPES_H
29 # include <sys/types.h>
30 # endif
31
32 # ifdef HAVE_SYS_WAIT_H
33 # include <sys/wait.h>
34 # endif
35
36 # ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 # endif
39
40 # ifdef HAVE_FCNTL_H
41 # include <fcntl.h>
42 # endif
43
44 # ifdef HAVE_ERRNO_H
45 # include <errno.h>
46 # endif
47
48 # include "stream.h"
49 # include "frame.h"
50 # include "synth.h"
51 # include "decoder.h"
52
53 /*
54 * NAME: decoder->init()
55 * DESCRIPTION: initialize a decoder object with callback routines
56 */
mad_decoder_init(struct mad_decoder * decoder,void * data,enum mad_flow (* input_func)(void *,struct mad_stream *),enum mad_flow (* header_func)(void *,struct mad_header const *),enum mad_flow (* filter_func)(void *,struct mad_stream const *,struct mad_frame *),enum mad_flow (* output_func)(void *,struct mad_header const *,struct mad_pcm *),enum mad_flow (* error_func)(void *,struct mad_stream *,struct mad_frame *),enum mad_flow (* message_func)(void *,void *,unsigned int *))57 void mad_decoder_init(struct mad_decoder *decoder, void *data,
58 enum mad_flow (*input_func)(void *,
59 struct mad_stream *),
60 enum mad_flow (*header_func)(void *,
61 struct mad_header const *),
62 enum mad_flow (*filter_func)(void *,
63 struct mad_stream const *,
64 struct mad_frame *),
65 enum mad_flow (*output_func)(void *,
66 struct mad_header const *,
67 struct mad_pcm *),
68 enum mad_flow (*error_func)(void *,
69 struct mad_stream *,
70 struct mad_frame *),
71 enum mad_flow (*message_func)(void *,
72 void *, unsigned int *))
73 {
74 decoder->mode = -1;
75
76 decoder->options = 0;
77
78 decoder->async.pid = 0;
79 decoder->async.in = -1;
80 decoder->async.out = -1;
81
82 decoder->sync = 0;
83
84 decoder->cb_data = data;
85
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;
92 }
93
mad_decoder_finish(struct mad_decoder * decoder)94 int mad_decoder_finish(struct mad_decoder *decoder)
95 {
96 # if defined(USE_ASYNC)
97 if (decoder->mode == MAD_DECODER_MODE_ASYNC && decoder->async.pid) {
98 pid_t pid;
99 int status;
100
101 close(decoder->async.in);
102
103 do
104 pid = waitpid(decoder->async.pid, &status, 0);
105 while (pid == -1 && errno == EINTR);
106
107 decoder->mode = -1;
108
109 close(decoder->async.out);
110
111 decoder->async.pid = 0;
112 decoder->async.in = -1;
113 decoder->async.out = -1;
114
115 if (pid == -1)
116 return -1;
117
118 return (!WIFEXITED(status) || WEXITSTATUS(status)) ? -1 : 0;
119 }
120 # endif
121
122 return 0;
123 }
124
125 # if defined(USE_ASYNC)
126 static
send_io(int fd,void const * data,size_t len)127 enum mad_flow send_io(int fd, void const *data, size_t len)
128 {
129 char const *ptr = data;
130 ssize_t count;
131
132 while (len) {
133 do
134 count = write(fd, ptr, len);
135 while (count == -1 && errno == EINTR);
136
137 if (count == -1)
138 return MAD_FLOW_BREAK;
139
140 len -= count;
141 ptr += count;
142 }
143
144 return MAD_FLOW_CONTINUE;
145 }
146
147 static
receive_io(int fd,void * buffer,size_t len)148 enum mad_flow receive_io(int fd, void *buffer, size_t len)
149 {
150 char *ptr = buffer;
151 ssize_t count;
152
153 while (len) {
154 do
155 count = read(fd, ptr, len);
156 while (count == -1 && errno == EINTR);
157
158 if (count == -1)
159 return (errno == EAGAIN) ? MAD_FLOW_IGNORE : MAD_FLOW_BREAK;
160 else if (count == 0)
161 return MAD_FLOW_STOP;
162
163 len -= count;
164 ptr += count;
165 }
166
167 return MAD_FLOW_CONTINUE;
168 }
169
170 static
receive_io_blocking(int fd,void * buffer,size_t len)171 enum mad_flow receive_io_blocking(int fd, void *buffer, size_t len)
172 {
173 int flags, blocking;
174 enum mad_flow result;
175
176 flags = fcntl(fd, F_GETFL);
177 if (flags == -1)
178 return MAD_FLOW_BREAK;
179
180 blocking = flags & ~O_NONBLOCK;
181
182 if (blocking != flags &&
183 fcntl(fd, F_SETFL, blocking) == -1)
184 return MAD_FLOW_BREAK;
185
186 result = receive_io(fd, buffer, len);
187
188 if (flags != blocking &&
189 fcntl(fd, F_SETFL, flags) == -1)
190 return MAD_FLOW_BREAK;
191
192 return result;
193 }
194
195 static
send(int fd,void const * message,unsigned int size)196 enum mad_flow send(int fd, void const *message, unsigned int size)
197 {
198 enum mad_flow result;
199
200 /* send size */
201
202 result = send_io(fd, &size, sizeof(size));
203
204 /* send message */
205
206 if (result == MAD_FLOW_CONTINUE)
207 result = send_io(fd, message, size);
208
209 return result;
210 }
211
212 static
receive(int fd,void ** message,unsigned int * size)213 enum mad_flow receive(int fd, void **message, unsigned int *size)
214 {
215 enum mad_flow result;
216 unsigned int actual;
217
218 if (*message == 0)
219 *size = 0;
220
221 /* receive size */
222
223 result = receive_io(fd, &actual, sizeof(actual));
224
225 /* receive message */
226
227 if (result == MAD_FLOW_CONTINUE) {
228 if (actual > *size)
229 actual -= *size;
230 else {
231 *size = actual;
232 actual = 0;
233 }
234
235 if (*size > 0) {
236 if (*message == 0) {
237 *message = malloc(*size);
238 if (*message == 0)
239 return MAD_FLOW_BREAK;
240 }
241
242 result = receive_io_blocking(fd, *message, *size);
243 }
244
245 /* throw away remainder of message */
246
247 while (actual && result == MAD_FLOW_CONTINUE) {
248 char sink[256];
249 unsigned int len;
250
251 len = actual > sizeof(sink) ? sizeof(sink) : actual;
252
253 result = receive_io_blocking(fd, sink, len);
254
255 actual -= len;
256 }
257 }
258
259 return result;
260 }
261
262 static
check_message(struct mad_decoder * decoder)263 enum mad_flow check_message(struct mad_decoder *decoder)
264 {
265 enum mad_flow result;
266 void *message = 0;
267 unsigned int size;
268
269 result = receive(decoder->async.in, &message, &size);
270
271 if (result == MAD_FLOW_CONTINUE) {
272 if (decoder->message_func == 0)
273 size = 0;
274 else {
275 result = decoder->message_func(decoder->cb_data, message, &size);
276
277 if (result == MAD_FLOW_IGNORE ||
278 result == MAD_FLOW_BREAK)
279 size = 0;
280 }
281
282 if (send(decoder->async.out, message, size) != MAD_FLOW_CONTINUE)
283 result = MAD_FLOW_BREAK;
284 }
285
286 if (message)
287 free(message);
288
289 return result;
290 }
291 # endif
292
293 static
error_default(void * data,struct mad_stream * stream,struct mad_frame * frame)294 enum mad_flow error_default(void *data, struct mad_stream *stream,
295 struct mad_frame *frame)
296 {
297 int *bad_last_frame = data;
298
299 switch (stream->error) {
300 case MAD_ERROR_BADCRC:
301 if (*bad_last_frame)
302 mad_frame_mute(frame);
303 else
304 *bad_last_frame = 1;
305
306 return MAD_FLOW_IGNORE;
307
308 default:
309 return MAD_FLOW_CONTINUE;
310 }
311 }
312
313 static
run_sync(struct mad_decoder * decoder)314 int run_sync(struct mad_decoder *decoder)
315 {
316 enum mad_flow (*error_func)(void *, struct mad_stream *, struct mad_frame *);
317 void *error_data;
318 int bad_last_frame = 0;
319 struct mad_stream *stream;
320 struct mad_frame *frame;
321 struct mad_synth *synth;
322 int result = 0;
323
324 if (decoder->input_func == 0)
325 return 0;
326
327 if (decoder->error_func) {
328 error_func = decoder->error_func;
329 error_data = decoder->cb_data;
330 }
331 else {
332 error_func = error_default;
333 error_data = &bad_last_frame;
334 }
335
336 stream = &decoder->sync->stream;
337 frame = &decoder->sync->frame;
338 synth = &decoder->sync->synth;
339
340 mad_stream_init(stream);
341 mad_frame_init(frame);
342 mad_synth_init(synth);
343
344 mad_stream_options(stream, decoder->options);
345
346 do {
347 switch (decoder->input_func(decoder->cb_data, stream)) {
348 case MAD_FLOW_STOP:
349 goto done;
350 case MAD_FLOW_BREAK:
351 goto fail;
352 case MAD_FLOW_IGNORE:
353 continue;
354 case MAD_FLOW_CONTINUE:
355 break;
356 }
357
358 while (1) {
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:
364 break;
365 case MAD_FLOW_BREAK:
366 goto fail;
367 case MAD_FLOW_STOP:
368 goto done;
369 }
370 }
371 # endif
372
373 if (decoder->header_func) {
374 if (mad_header_decode(&frame->header, stream) == -1) {
375 if (!MAD_RECOVERABLE(stream->error))
376 break;
377
378 switch (error_func(error_data, stream, frame)) {
379 case MAD_FLOW_STOP:
380 goto done;
381 case MAD_FLOW_BREAK:
382 goto fail;
383 case MAD_FLOW_IGNORE:
384 case MAD_FLOW_CONTINUE:
385 default:
386 continue;
387 }
388 }
389
390 switch (decoder->header_func(decoder->cb_data, &frame->header)) {
391 case MAD_FLOW_STOP:
392 goto done;
393 case MAD_FLOW_BREAK:
394 goto fail;
395 case MAD_FLOW_IGNORE:
396 continue;
397 case MAD_FLOW_CONTINUE:
398 break;
399 }
400 }
401
402 if (mad_frame_decode(frame, stream) == -1) {
403 if (!MAD_RECOVERABLE(stream->error))
404 break;
405
406 switch (error_func(error_data, stream, frame)) {
407 case MAD_FLOW_STOP:
408 goto done;
409 case MAD_FLOW_BREAK:
410 goto fail;
411 case MAD_FLOW_IGNORE:
412 break;
413 case MAD_FLOW_CONTINUE:
414 default:
415 continue;
416 }
417 }
418 else
419 bad_last_frame = 0;
420
421 if (decoder->filter_func) {
422 switch (decoder->filter_func(decoder->cb_data, stream, frame)) {
423 case MAD_FLOW_STOP:
424 goto done;
425 case MAD_FLOW_BREAK:
426 goto fail;
427 case MAD_FLOW_IGNORE:
428 continue;
429 case MAD_FLOW_CONTINUE:
430 break;
431 }
432 }
433
434 mad_synth_frame(synth, frame);
435
436 if (decoder->output_func) {
437 switch (decoder->output_func(decoder->cb_data,
438 &frame->header, &synth->pcm)) {
439 case MAD_FLOW_STOP:
440 goto done;
441 case MAD_FLOW_BREAK:
442 goto fail;
443 case MAD_FLOW_IGNORE:
444 case MAD_FLOW_CONTINUE:
445 break;
446 }
447 }
448 }
449 }
450 while (stream->error == MAD_ERROR_BUFLEN);
451
452 fail:
453 result = -1;
454
455 done:
456 mad_synth_finish(synth);
457 mad_frame_finish(frame);
458 mad_stream_finish(stream);
459
460 return result;
461 }
462
463 # if defined(USE_ASYNC)
464 static
run_async(struct mad_decoder * decoder)465 int run_async(struct mad_decoder *decoder)
466 {
467 pid_t pid;
468 int ptoc[2], ctop[2], flags;
469
470 if (pipe(ptoc) == -1)
471 return -1;
472
473 if (pipe(ctop) == -1) {
474 close(ptoc[0]);
475 close(ptoc[1]);
476 return -1;
477 }
478
479 flags = fcntl(ptoc[0], F_GETFL);
480 if (flags == -1 ||
481 fcntl(ptoc[0], F_SETFL, flags | O_NONBLOCK) == -1) {
482 close(ctop[0]);
483 close(ctop[1]);
484 close(ptoc[0]);
485 close(ptoc[1]);
486 return -1;
487 }
488
489 pid = fork();
490 if (pid == -1) {
491 close(ctop[0]);
492 close(ctop[1]);
493 close(ptoc[0]);
494 close(ptoc[1]);
495 return -1;
496 }
497
498 decoder->async.pid = pid;
499
500 if (pid) {
501 /* parent */
502
503 close(ptoc[0]);
504 close(ctop[1]);
505
506 decoder->async.in = ctop[0];
507 decoder->async.out = ptoc[1];
508
509 return 0;
510 }
511
512 /* child */
513
514 close(ptoc[1]);
515 close(ctop[0]);
516
517 decoder->async.in = ptoc[0];
518 decoder->async.out = ctop[1];
519
520 _exit(run_sync(decoder));
521
522 /* not reached */
523 return -1;
524 }
525 # endif
526
527 /*
528 * NAME: decoder->run()
529 * DESCRIPTION: run the decoder thread either synchronously or asynchronously
530 */
mad_decoder_run(struct mad_decoder * decoder,enum mad_decoder_mode mode)531 int mad_decoder_run(struct mad_decoder *decoder, enum mad_decoder_mode mode)
532 {
533 int result;
534 int (*run)(struct mad_decoder *) = 0;
535
536 switch (decoder->mode = mode) {
537 case MAD_DECODER_MODE_SYNC:
538 run = run_sync;
539 break;
540
541 case MAD_DECODER_MODE_ASYNC:
542 # if defined(USE_ASYNC)
543 run = run_async;
544 # endif
545 break;
546 }
547
548 if (run == 0)
549 return -1;
550
551 decoder->sync = malloc(sizeof(*decoder->sync));
552 if (decoder->sync == 0)
553 return -1;
554
555 result = run(decoder);
556
557 free(decoder->sync);
558 decoder->sync = 0;
559
560 return result;
561 }
562
563 /*
564 * NAME: decoder->message()
565 * DESCRIPTION: send a message to and receive a reply from the decoder process
566 */
mad_decoder_message(struct mad_decoder * decoder,void * message,unsigned int * len)567 int mad_decoder_message(struct mad_decoder *decoder,
568 void *message, unsigned int *len)
569 {
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)
574 return -1;
575
576 return 0;
577 # else
578 return -1;
579 # endif
580 }
581