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
28 # ifdef HAVE_SYS_TYPES_H
29 # include <sys/types.h>
32 # ifdef HAVE_SYS_WAIT_H
33 # include <sys/wait.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 *,
61 enum mad_flow (*header_func
)(void *,
62 struct mad_header
const *),
63 enum mad_flow (*filter_func
)(void *,
64 struct mad_stream
const *,
66 enum mad_flow (*output_func
)(void *,
67 struct mad_header
const *,
69 enum mad_flow (*error_func
)(void *,
72 enum mad_flow (*message_func
)(void *,
73 void *, unsigned int *))
79 decoder
->async
.pid
= 0;
80 decoder
->async
.in
= -1;
81 decoder
->async
.out
= -1;
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
) {
102 close(decoder
->async
.in
);
105 pid
= waitpid(decoder
->async
.pid
, &status
, 0);
106 while (pid
== -1 && errno
== EINTR
);
110 close(decoder
->async
.out
);
112 decoder
->async
.pid
= 0;
113 decoder
->async
.in
= -1;
114 decoder
->async
.out
= -1;
119 return (!WIFEXITED(status
) || WEXITSTATUS(status
)) ? -1 : 0;
122 /* Avoid compiler warning */
129 # if defined(USE_ASYNC)
131 enum mad_flow
send_io(int fd
, void const *data
, size_t len
)
133 char const *ptr
= data
;
138 count
= write(fd
, ptr
, len
);
139 while (count
== -1 && errno
== EINTR
);
142 return MAD_FLOW_BREAK
;
148 return MAD_FLOW_CONTINUE
;
152 enum mad_flow
receive_io(int fd
, void *buffer
, size_t len
)
159 count
= read(fd
, ptr
, len
);
160 while (count
== -1 && errno
== EINTR
);
163 return (errno
== EAGAIN
) ? MAD_FLOW_IGNORE
: MAD_FLOW_BREAK
;
165 return MAD_FLOW_STOP
;
171 return MAD_FLOW_CONTINUE
;
175 enum mad_flow
receive_io_blocking(int fd
, void *buffer
, size_t len
)
178 enum mad_flow result
;
180 flags
= fcntl(fd
, F_GETFL
);
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
;
200 enum mad_flow
send(int fd
, void const *message
, unsigned int size
)
202 enum mad_flow result
;
206 result
= send_io(fd
, &size
, sizeof(size
));
210 if (result
== MAD_FLOW_CONTINUE
)
211 result
= send_io(fd
, message
, size
);
217 enum mad_flow
receive(int fd
, void **message
, unsigned int *size
)
219 enum mad_flow result
;
227 result
= receive_io(fd
, &actual
, sizeof(actual
));
229 /* receive message */
231 if (result
== MAD_FLOW_CONTINUE
) {
241 *message
= malloc(*size
);
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
) {
255 len
= actual
> sizeof(sink
) ? sizeof(sink
) : actual
;
257 result
= receive_io_blocking(fd
, sink
, len
);
267 enum mad_flow
check_message(struct mad_decoder
*decoder
)
269 enum mad_flow result
;
273 result
= receive(decoder
->async
.in
, &message
, &size
);
275 if (result
== MAD_FLOW_CONTINUE
) {
276 if (decoder
->message_func
== 0)
279 result
= decoder
->message_func(decoder
->cb_data
, message
, &size
);
281 if (result
== MAD_FLOW_IGNORE
||
282 result
== MAD_FLOW_BREAK
)
286 if (send(decoder
->async
.out
, message
, size
) != MAD_FLOW_CONTINUE
)
287 result
= MAD_FLOW_BREAK
;
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
:
306 mad_frame_mute(frame
);
310 return MAD_FLOW_IGNORE
;
313 return MAD_FLOW_CONTINUE
;
318 int run_sync(struct mad_decoder
*decoder
)
320 enum mad_flow (*error_func
)(void *, struct mad_stream
*, struct mad_frame
*);
322 int bad_last_frame
= 0;
323 struct mad_stream
*stream
;
324 struct mad_frame
*frame
;
325 struct mad_synth
*synth
;
328 if (decoder
->input_func
== 0)
331 if (decoder
->error_func
) {
332 error_func
= decoder
->error_func
;
333 error_data
= decoder
->cb_data
;
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
);
351 switch (decoder
->input_func(decoder
->cb_data
, stream
)) {
356 case MAD_FLOW_IGNORE
:
358 case MAD_FLOW_CONTINUE
:
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
:
377 if (decoder
->header_func
) {
378 if (mad_header_decode(&frame
->header
, stream
) == -1) {
379 if (!MAD_RECOVERABLE(stream
->error
))
382 switch (error_func(error_data
, stream
, frame
)) {
387 case MAD_FLOW_IGNORE
:
388 case MAD_FLOW_CONTINUE
:
394 switch (decoder
->header_func(decoder
->cb_data
, &frame
->header
)) {
399 case MAD_FLOW_IGNORE
:
401 case MAD_FLOW_CONTINUE
:
406 if (mad_frame_decode(frame
, stream
) == -1) {
407 if (!MAD_RECOVERABLE(stream
->error
))
410 switch (error_func(error_data
, stream
, frame
)) {
415 case MAD_FLOW_IGNORE
:
417 case MAD_FLOW_CONTINUE
:
425 if (decoder
->filter_func
) {
426 switch (decoder
->filter_func(decoder
->cb_data
, stream
, frame
)) {
431 case MAD_FLOW_IGNORE
:
433 case MAD_FLOW_CONTINUE
:
438 mad_synth_frame(synth
, frame
);
440 if (decoder
->output_func
) {
441 switch (decoder
->output_func(decoder
->cb_data
,
442 &frame
->header
, &synth
->pcm
)) {
447 case MAD_FLOW_IGNORE
:
448 case MAD_FLOW_CONTINUE
:
454 while (stream
->error
== MAD_ERROR_BUFLEN
);
460 mad_synth_finish(synth
);
461 mad_frame_finish(frame
);
462 mad_stream_finish(stream
);
467 # if defined(USE_ASYNC)
469 int run_async(struct mad_decoder
*decoder
)
472 int ptoc
[2], ctop
[2], flags
;
474 if (pipe(ptoc
) == -1)
477 if (pipe(ctop
) == -1) {
483 flags
= fcntl(ptoc
[0], F_GETFL
);
485 fcntl(ptoc
[0], F_SETFL
, flags
| O_NONBLOCK
) == -1) {
502 decoder
->async
.pid
= pid
;
510 decoder
->async
.in
= ctop
[0];
511 decoder
->async
.out
= ptoc
[1];
521 decoder
->async
.in
= ptoc
[0];
522 decoder
->async
.out
= ctop
[1];
524 _exit(run_sync(decoder
));
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
)
538 int (*run
)(struct mad_decoder
*) = 0;
540 switch (decoder
->mode
= mode
) {
541 case MAD_DECODER_MODE_SYNC
:
545 case MAD_DECODER_MODE_ASYNC
:
546 # if defined(USE_ASYNC)
555 decoder
->sync
= malloc(sizeof(*decoder
->sync
));
556 if (decoder
->sync
== 0)
559 result
= run(decoder
);
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
)
582 /* Avoid compiler warnings */