Debian Codegraph @

external/mp3/mad_decoder.c

raw
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 "mad_config.h"
24 /*# endif*/
25
26 # include "mad_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 # include <stdlib.h>
45
46 # ifdef HAVE_ERRNO_H
47 # include <errno.h>
48 # endif
49
50 # include "mad_stream.h"
51 # include "mad_frame.h"
52 # include "mad_synth.h"
53 # include "mad_decoder.h"
54
55 /*
56 * NAME: decoder->init()
57 * DESCRIPTION: initialize a decoder object with callback routines
58 */
59 void mad_decoder_init(struct mad_decoder *decoder, void *data,
60 enum mad_flow (*input_func)(void *,
61 struct mad_stream *),
62 enum mad_flow (*header_func)(void *,
63 struct mad_header const *),
64 enum mad_flow (*filter_func)(void *,
65 struct mad_stream const *,
66 struct mad_frame *),
67 enum mad_flow (*output_func)(void *,
68 struct mad_header const *,
69 struct mad_pcm *),
70 enum mad_flow (*error_func)(void *,
71 struct mad_stream *,
72 struct mad_frame *),
73 enum mad_flow (*message_func)(void *,
74 void *, unsigned int *))
75 {
76 decoder->mode = -1;
77
78 decoder->options = 0;
79
80 decoder->async.pid = 0;
81 decoder->async.in = -1;
82 decoder->async.out = -1;
83
84 decoder->sync = 0;
85
86 decoder->cb_data = data;
87
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;
94 }
95
96 int mad_decoder_finish(struct mad_decoder *decoder)
97 {
98 # if defined(USE_ASYNC)
99 if (decoder->mode == MAD_DECODER_MODE_ASYNC && decoder->async.pid) {
100 pid_t pid;
101 int status;
102
103 close(decoder->async.in);
104
105 do
106 pid = waitpid(decoder->async.pid, &status, 0);
107 while (pid == -1 && errno == EINTR);
108
109 decoder->mode = -1;
110
111 close(decoder->async.out);
112
113 decoder->async.pid = 0;
114 decoder->async.in = -1;
115 decoder->async.out = -1;
116
117 if (pid == -1)
118 return -1;
119
120 return (!WIFEXITED(status) || WEXITSTATUS(status)) ? -1 : 0;
121 }
122 # else
123 (void) decoder;
124 # endif
125
126 return 0;
127 }
128
129 # if defined(USE_ASYNC)
130 static
131 enum mad_flow send_io(int fd, void const *data, size_t len)
132 {
133 char const *ptr = data;
134 ssize_t count;
135
136 while (len) {
137 do
138 count = write(fd, ptr, len);
139 while (count == -1 && errno == EINTR);
140
141 if (count == -1)
142 return MAD_FLOW_BREAK;
143
144 len -= count;
145 ptr += count;
146 }
147
148 return MAD_FLOW_CONTINUE;
149 }
150
151 static
152 enum mad_flow receive_io(int fd, void *buffer, size_t len)
153 {
154 char *ptr = buffer;
155 ssize_t count;
156
157 while (len) {
158 do
159 count = read(fd, ptr, len);
160 while (count == -1 && errno == EINTR);
161
162 if (count == -1)
163 return (errno == EAGAIN) ? MAD_FLOW_IGNORE : MAD_FLOW_BREAK;
164 else if (count == 0)
165 return MAD_FLOW_STOP;
166
167 len -= count;
168 ptr += count;
169 }
170
171 return MAD_FLOW_CONTINUE;
172 }
173
174 static
175 enum mad_flow receive_io_blocking(int fd, void *buffer, size_t len)
176 {
177 int flags, blocking;
178 enum mad_flow result;
179
180 flags = fcntl(fd, F_GETFL);
181 if (flags == -1)
182 return MAD_FLOW_BREAK;
183
184 blocking = flags & ~O_NONBLOCK;
185
186 if (blocking != flags &&
187 fcntl(fd, F_SETFL, blocking) == -1)
188 return MAD_FLOW_BREAK;
189
190 result = receive_io(fd, buffer, len);
191
192 if (flags != blocking &&
193 fcntl(fd, F_SETFL, flags) == -1)
194 return MAD_FLOW_BREAK;
195
196 return result;
197 }
198
199 static
200 enum mad_flow send(int fd, void const *message, unsigned int size)
201 {
202 enum mad_flow result;
203
204 /* send size */
205
206 result = send_io(fd, &size, sizeof(size));
207
208 /* send message */
209
210 if (result == MAD_FLOW_CONTINUE)
211 result = send_io(fd, message, size);
212
213 return result;
214 }
215
216 static
217 enum mad_flow receive(int fd, void **message, unsigned int *size)
218 {
219 enum mad_flow result;
220 unsigned int actual;
221
222 if (*message == 0)
223 *size = 0;
224
225 /* receive size */
226
227 result = receive_io(fd, &actual, sizeof(actual));
228
229 /* receive message */
230
231 if (result == MAD_FLOW_CONTINUE) {
232 if (actual > *size)
233 actual -= *size;
234 else {
235 *size = actual;
236 actual = 0;
237 }
238
239 if (*size > 0) {
240 if (*message == 0) {
241 *message = malloc(*size);
242 if (*message == 0)
243 return MAD_FLOW_BREAK;
244 }
245
246 result = receive_io_blocking(fd, *message, *size);
247 }
248
249 /* throw away remainder of message */
250
251 while (actual && result == MAD_FLOW_CONTINUE) {
252 char sink[256];
253 unsigned int len;
254
255 len = actual > sizeof(sink) ? sizeof(sink) : actual;
256
257 result = receive_io_blocking(fd, sink, len);
258
259 actual -= len;
260 }
261 }
262
263 return result;
264 }
265
266 static
267 enum mad_flow check_message(struct mad_decoder *decoder)
268 {
269 enum mad_flow result;
270 void *message = 0;
271 unsigned int size;
272
273 result = receive(decoder->async.in, &message, &size);
274
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);
280
281 if (result == MAD_FLOW_IGNORE ||
282 result == MAD_FLOW_BREAK)
283 size = 0;
284 }
285
286 if (send(decoder->async.out, message, size) != MAD_FLOW_CONTINUE)
287 result = MAD_FLOW_BREAK;
288 }
289
290 if (message)
291 free(message);
292
293 return result;
294 }
295 # endif
296
297 static
298 enum mad_flow error_default(void *data, struct mad_stream *stream,
299 struct mad_frame *frame)
300 {
301 int *bad_last_frame = data;
302
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;
309
310 return MAD_FLOW_IGNORE;
311
312 default:
313 return MAD_FLOW_CONTINUE;
314 }
315 }
316
317 static
318 int run_sync(struct mad_decoder *decoder)
319 {
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;
327
328 if (decoder->input_func == 0)
329 return 0;
330
331 if (decoder->error_func) {
332 error_func = decoder->error_func;
333 error_data = decoder->cb_data;
334 }
335 else {
336 error_func = error_default;
337 error_data = &bad_last_frame;
338 }
339
340 stream = &decoder->sync->stream;
341 frame = &decoder->sync->frame;
342 synth = &decoder->sync->synth;
343
344 mad_stream_init(stream);
345 mad_frame_init(frame);
346 mad_synth_init(synth);
347
348 mad_stream_options(stream, decoder->options);
349
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;
360 }
361
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;
373 }
374 }
375 # endif
376
377 if (decoder->header_func) {
378 if (mad_header_decode(&frame->header, stream) == -1) {
379 if (!MAD_RECOVERABLE(stream->error))
380 break;
381
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;
391 }
392 }
393
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;
403 }
404 }
405
406 if (mad_frame_decode(frame, stream) == -1) {
407 if (!MAD_RECOVERABLE(stream->error))
408 break;
409
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;
420 }
421 }
422 else
423 bad_last_frame = 0;
424
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;
435 }
436 }
437
438 mad_synth_frame(synth, frame);
439
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;
450 }
451 }
452 }
453 }
454 while (stream->error == MAD_ERROR_BUFLEN);
455
456 fail:
457 result = -1;
458
459 done:
460 mad_synth_finish(synth);
461 mad_frame_finish(frame);
462 mad_stream_finish(stream);
463
464 return result;
465 }
466
467 # if defined(USE_ASYNC)
468 static
469 int run_async(struct mad_decoder *decoder)
470 {
471 pid_t pid;
472 int ptoc[2], ctop[2], flags;
473
474 if (pipe(ptoc) == -1)
475 return -1;
476
477 if (pipe(ctop) == -1) {
478 close(ptoc[0]);
479 close(ptoc[1]);
480 return -1;
481 }
482
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;
491 }
492
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;
500 }
501
502 decoder->async.pid = pid;
503
504 if (pid) {
505 /* parent */
506
507 close(ptoc[0]);
508 close(ctop[1]);
509
510 decoder->async.in = ctop[0];
511 decoder->async.out = ptoc[1];
512
513 return 0;
514 }
515
516 /* child */
517
518 close(ptoc[1]);
519 close(ctop[0]);
520
521 decoder->async.in = ptoc[0];
522 decoder->async.out = ctop[1];
523
524 _exit(run_sync(decoder));
525
526 /* not reached */
527 return -1;
528 }
529 # endif
530
531 /*
532 * NAME: decoder->run()
533 * DESCRIPTION: run the decoder thread either synchronously or asynchronously
534 */
535 int mad_decoder_run(struct mad_decoder *decoder, enum mad_decoder_mode mode)
536 {
537 int result;
538 int (*run)(struct mad_decoder *) = 0;
539
540 switch (decoder->mode = mode) {
541 case MAD_DECODER_MODE_SYNC:
542 run = run_sync;
543 break;
544
545 case MAD_DECODER_MODE_ASYNC:
546 # if defined(USE_ASYNC)
547 run = run_async;
548 # endif
549 break;
550 }
551
552 if (run == 0)
553 return -1;
554
555 decoder->sync = malloc(sizeof(*decoder->sync));
556 if (decoder->sync == 0)
557 return -1;
558
559 result = run(decoder);
560
561 free(decoder->sync);
562 decoder->sync = 0;
563
564 return result;
565 }
566
567 /*
568 * NAME: decoder->message()
569 * DESCRIPTION: send a message to and receive a reply from the decoder process
570 */
571 int mad_decoder_message(struct mad_decoder *decoder,
572 void *message, unsigned int *len)
573 {
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;
579
580 return 0;
581 # else
582 (void) decoder;
583 (void) message;
584 (void) len;
585 return -1;
586 # endif
587 }