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
23 # include "madconfig.h"
28 # ifdef HAVE_SYS_TYPES_H
29 # include <sys/types.h>
32 # ifdef HAVE_SYS_WAIT_H
33 # include <sys/wait.h>
55 void mad_decoder_init(struct mad_decoder
*decoder
, void *data
,
56 enum mad_flow (*input_func
)(void *,
58 enum mad_flow (*header_func
)(void *,
59 struct mad_header
const *),
60 enum mad_flow (*filter_func
)(void *,
61 struct mad_stream
const *,
63 enum mad_flow (*output_func
)(void *,
64 struct mad_header
const *,
66 enum mad_flow (*error_func
)(void *,
69 enum mad_flow (*message_func
)(void *,
70 void *, unsigned int *))
76 decoder
->async
.pid
= 0;
77 decoder
->async
.in
= -1;
78 decoder
->async
.out
= -1;
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
) {
99 close(decoder
->async
.in
);
102 pid
= waitpid(decoder
->async
.pid
, &status
, 0);
103 while (pid
== -1 && errno
== EINTR
);
107 close(decoder
->async
.out
);
109 decoder
->async
.pid
= 0;
110 decoder
->async
.in
= -1;
111 decoder
->async
.out
= -1;
116 return (!WIFEXITED(status
) || WEXITSTATUS(status
)) ? -1 : 0;
123 # if defined(USE_ASYNC)
125 enum mad_flow
send_io(int fd
, void const *data
, size_t len
)
127 char const *ptr
= data
;
132 count
= write(fd
, ptr
, len
);
133 while (count
== -1 && errno
== EINTR
);
136 return MAD_FLOW_BREAK
;
142 return MAD_FLOW_CONTINUE
;
146 enum mad_flow
receive_io(int fd
, void *buffer
, size_t len
)
153 count
= read(fd
, ptr
, len
);
154 while (count
== -1 && errno
== EINTR
);
157 return (errno
== EAGAIN
) ? MAD_FLOW_IGNORE
: MAD_FLOW_BREAK
;
159 return MAD_FLOW_STOP
;
165 return MAD_FLOW_CONTINUE
;
169 enum mad_flow
receive_io_blocking(int fd
, void *buffer
, size_t len
)
172 enum mad_flow result
;
174 flags
= fcntl(fd
, F_GETFL
);
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
;
194 enum mad_flow
send(int fd
, void const *message
, unsigned int size
)
196 enum mad_flow result
;
200 result
= send_io(fd
, &size
, sizeof(size
));
204 if (result
== MAD_FLOW_CONTINUE
)
205 result
= send_io(fd
, message
, size
);
211 enum mad_flow
receive(int fd
, void **message
, unsigned int *size
)
213 enum mad_flow result
;
221 result
= receive_io(fd
, &actual
, sizeof(actual
));
223 /* receive message */
225 if (result
== MAD_FLOW_CONTINUE
) {
235 *message
= malloc(*size
);
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
) {
249 len
= actual
> sizeof(sink
) ? sizeof(sink
) : actual
;
251 result
= receive_io_blocking(fd
, sink
, len
);
261 enum mad_flow
check_message(struct mad_decoder
*decoder
)
263 enum mad_flow result
;
267 result
= receive(decoder
->async
.in
, &message
, &size
);
269 if (result
== MAD_FLOW_CONTINUE
) {
270 if (decoder
->message_func
== 0)
273 result
= decoder
->message_func(decoder
->cb_data
, message
, &size
);
275 if (result
== MAD_FLOW_IGNORE
||
276 result
== MAD_FLOW_BREAK
)
280 if (send(decoder
->async
.out
, message
, size
) != MAD_FLOW_CONTINUE
)
281 result
= MAD_FLOW_BREAK
;
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
:
300 mad_frame_mute(frame
);
304 return MAD_FLOW_IGNORE
;
307 return MAD_FLOW_CONTINUE
;
312 int run_sync(struct mad_decoder
*decoder
)
314 enum mad_flow (*error_func
)(void *, struct mad_stream
*, struct mad_frame
*);
316 int bad_last_frame
= 0;
317 struct mad_stream
*stream
;
318 struct mad_frame
*frame
;
319 struct mad_synth
*synth
;
322 if (decoder
->input_func
== 0)
325 if (decoder
->error_func
) {
326 error_func
= decoder
->error_func
;
327 error_data
= decoder
->cb_data
;
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
);
345 switch (decoder
->input_func(decoder
->cb_data
, stream
)) {
350 case MAD_FLOW_IGNORE
:
352 case MAD_FLOW_CONTINUE
:
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
:
371 if (decoder
->header_func
) {
372 if (mad_header_decode(&frame
->header
, stream
) == -1) {
373 if (!MAD_RECOVERABLE(stream
->error
))
376 switch (error_func(error_data
, stream
, frame
)) {
381 case MAD_FLOW_IGNORE
:
382 case MAD_FLOW_CONTINUE
:
388 switch (decoder
->header_func(decoder
->cb_data
, &frame
->header
)) {
393 case MAD_FLOW_IGNORE
:
395 case MAD_FLOW_CONTINUE
:
400 if (mad_frame_decode(frame
, stream
) == -1) {
401 if (!MAD_RECOVERABLE(stream
->error
))
404 switch (error_func(error_data
, stream
, frame
)) {
409 case MAD_FLOW_IGNORE
:
411 case MAD_FLOW_CONTINUE
:
419 if (decoder
->filter_func
) {
420 switch (decoder
->filter_func(decoder
->cb_data
, stream
, frame
)) {
425 case MAD_FLOW_IGNORE
:
427 case MAD_FLOW_CONTINUE
:
432 mad_synth_frame(synth
, frame
);
434 if (decoder
->output_func
) {
435 switch (decoder
->output_func(decoder
->cb_data
,
436 &frame
->header
, &synth
->pcm
)) {
441 case MAD_FLOW_IGNORE
:
442 case MAD_FLOW_CONTINUE
:
448 while (stream
->error
== MAD_ERROR_BUFLEN
);
454 mad_synth_finish(synth
);
455 mad_frame_finish(frame
);
456 mad_stream_finish(stream
);
461 # if defined(USE_ASYNC)
463 int run_async(struct mad_decoder
*decoder
)
466 int ptoc
[2], ctop
[2], flags
;
468 if (pipe(ptoc
) == -1)
471 if (pipe(ctop
) == -1) {
477 flags
= fcntl(ptoc
[0], F_GETFL
);
479 fcntl(ptoc
[0], F_SETFL
, flags
| O_NONBLOCK
) == -1) {
496 decoder
->async
.pid
= pid
;
504 decoder
->async
.in
= ctop
[0];
505 decoder
->async
.out
= ptoc
[1];
515 decoder
->async
.in
= ptoc
[0];
516 decoder
->async
.out
= ctop
[1];
518 _exit(run_sync(decoder
));
525 int mad_decoder_run(struct mad_decoder
*decoder
, enum mad_decoder_mode mode
)
528 int (*run
)(struct mad_decoder
*) = 0;
530 switch (decoder
->mode
= mode
) {
531 case MAD_DECODER_MODE_SYNC
:
535 case MAD_DECODER_MODE_ASYNC
:
536 # if defined(USE_ASYNC)
545 decoder
->sync
= malloc(sizeof(*decoder
->sync
));
546 if (decoder
->sync
== 0)
549 result
= run(decoder
);
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
)