decoder.c

00001 /*
00002  * libmad - MPEG audio decoder library
00003  * Copyright (C) 2000-2003 Underbit Technologies, Inc.
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  *
00019  * $Id: decoder.c,v 1.1 2003/08/31 18:59:46 gabest Exp $
00020  */
00021 
00022 # ifdef HAVE_CONFIG_H
00023 #  include "config.h"
00024 # endif
00025 
00026 # include "global.h"
00027 
00028 # ifdef HAVE_SYS_TYPES_H
00029 #  include <sys/types.h>
00030 # endif
00031 
00032 # ifdef HAVE_SYS_WAIT_H
00033 #  include <sys/wait.h>
00034 # endif
00035 
00036 # ifdef HAVE_UNISTD_H
00037 #  include <unistd.h>
00038 # endif
00039 
00040 # ifdef HAVE_FCNTL_H
00041 #  include <fcntl.h>
00042 # endif
00043 
00044 # include <stdlib.h>
00045 
00046 # ifdef HAVE_ERRNO_H
00047 #  include <errno.h>
00048 # endif
00049 
00050 # include "stream.h"
00051 # include "frame.h"
00052 # include "synth.h"
00053 # include "decoder.h"
00054 
00055 /*
00056  * NAME:        decoder->init()
00057  * DESCRIPTION: initialize a decoder object with callback routines
00058  */
00059 void mad_decoder_init(struct mad_decoder *decoder, void *data,
00060                       enum mad_flow (*input_func)(void *,
00061                                                   struct mad_stream *),
00062                       enum mad_flow (*header_func)(void *,
00063                                                    struct mad_header const *),
00064                       enum mad_flow (*filter_func)(void *,
00065                                                    struct mad_stream const *,
00066                                                    struct mad_frame *),
00067                       enum mad_flow (*output_func)(void *,
00068                                                    struct mad_header const *,
00069                                                    struct mad_pcm *),
00070                       enum mad_flow (*error_func)(void *,
00071                                                   struct mad_stream *,
00072                                                   struct mad_frame *),
00073                       enum mad_flow (*message_func)(void *,
00074                                                     void *, unsigned int *))
00075 {
00076   decoder->mode         = -1;
00077 
00078   decoder->options      = 0;
00079 
00080   decoder->async.pid    = 0;
00081   decoder->async.in     = -1;
00082   decoder->async.out    = -1;
00083 
00084   decoder->sync         = 0;
00085 
00086   decoder->cb_data      = data;
00087 
00088   decoder->input_func   = input_func;
00089   decoder->header_func  = header_func;
00090   decoder->filter_func  = filter_func;
00091   decoder->output_func  = output_func;
00092   decoder->error_func   = error_func;
00093   decoder->message_func = message_func;
00094 }
00095 
00096 int mad_decoder_finish(struct mad_decoder *decoder)
00097 {
00098 # if defined(USE_ASYNC)
00099   if (decoder->mode == MAD_DECODER_MODE_ASYNC && decoder->async.pid) {
00100     pid_t pid;
00101     int status;
00102 
00103     close(decoder->async.in);
00104 
00105     do
00106       pid = waitpid(decoder->async.pid, &status, 0);
00107     while (pid == -1 && errno == EINTR);
00108 
00109     decoder->mode = -1;
00110 
00111     close(decoder->async.out);
00112 
00113     decoder->async.pid = 0;
00114     decoder->async.in  = -1;
00115     decoder->async.out = -1;
00116 
00117     if (pid == -1)
00118       return -1;
00119 
00120     return (!WIFEXITED(status) || WEXITSTATUS(status)) ? -1 : 0;
00121   }
00122 # endif
00123 
00124   return 0;
00125 }
00126 
00127 # if defined(USE_ASYNC)
00128 static
00129 enum mad_flow send_io(int fd, void const *data, size_t len)
00130 {
00131   char const *ptr = data;
00132   ssize_t count;
00133 
00134   while (len) {
00135     do
00136       count = write(fd, ptr, len);
00137     while (count == -1 && errno == EINTR);
00138 
00139     if (count == -1)
00140       return MAD_FLOW_BREAK;
00141 
00142     len -= count;
00143     ptr += count;
00144   }
00145 
00146   return MAD_FLOW_CONTINUE;
00147 }
00148 
00149 static
00150 enum mad_flow receive_io(int fd, void *buffer, size_t len)
00151 {
00152   char *ptr = buffer;
00153   ssize_t count;
00154 
00155   while (len) {
00156     do
00157       count = read(fd, ptr, len);
00158     while (count == -1 && errno == EINTR);
00159 
00160     if (count == -1)
00161       return (errno == EAGAIN) ? MAD_FLOW_IGNORE : MAD_FLOW_BREAK;
00162     else if (count == 0)
00163       return MAD_FLOW_STOP;
00164 
00165     len -= count;
00166     ptr += count;
00167   }
00168 
00169   return MAD_FLOW_CONTINUE;
00170 }
00171 
00172 static
00173 enum mad_flow receive_io_blocking(int fd, void *buffer, size_t len)
00174 {
00175   int flags, blocking;
00176   enum mad_flow result;
00177 
00178   flags = fcntl(fd, F_GETFL);
00179   if (flags == -1)
00180     return MAD_FLOW_BREAK;
00181 
00182   blocking = flags & ~O_NONBLOCK;
00183 
00184   if (blocking != flags &&
00185       fcntl(fd, F_SETFL, blocking) == -1)
00186     return MAD_FLOW_BREAK;
00187 
00188   result = receive_io(fd, buffer, len);
00189 
00190   if (flags != blocking &&
00191       fcntl(fd, F_SETFL, flags) == -1)
00192     return MAD_FLOW_BREAK;
00193 
00194   return result;
00195 }
00196 
00197 static
00198 enum mad_flow send(int fd, void const *message, unsigned int size)
00199 {
00200   enum mad_flow result;
00201 
00202   /* send size */
00203 
00204   result = send_io(fd, &size, sizeof(size));
00205 
00206   /* send message */
00207 
00208   if (result == MAD_FLOW_CONTINUE)
00209     result = send_io(fd, message, size);
00210 
00211   return result;
00212 }
00213 
00214 static
00215 enum mad_flow receive(int fd, void **message, unsigned int *size)
00216 {
00217   enum mad_flow result;
00218   unsigned int actual;
00219 
00220   if (*message == 0)
00221     *size = 0;
00222 
00223   /* receive size */
00224 
00225   result = receive_io(fd, &actual, sizeof(actual));
00226 
00227   /* receive message */
00228 
00229   if (result == MAD_FLOW_CONTINUE) {
00230     if (actual > *size)
00231       actual -= *size;
00232     else {
00233       *size  = actual;
00234       actual = 0;
00235     }
00236 
00237     if (*size > 0) {
00238       if (*message == 0) {
00239         *message = malloc(*size);
00240         if (*message == 0)
00241           return MAD_FLOW_BREAK;
00242       }
00243 
00244       result = receive_io_blocking(fd, *message, *size);
00245     }
00246 
00247     /* throw away remainder of message */
00248 
00249     while (actual && result == MAD_FLOW_CONTINUE) {
00250       char sink[256];
00251       unsigned int len;
00252 
00253       len = actual > sizeof(sink) ? sizeof(sink) : actual;
00254 
00255       result = receive_io_blocking(fd, sink, len);
00256 
00257       actual -= len;
00258     }
00259   }
00260 
00261   return result;
00262 }
00263 
00264 static
00265 enum mad_flow check_message(struct mad_decoder *decoder)
00266 {
00267   enum mad_flow result;
00268   void *message = 0;
00269   unsigned int size;
00270 
00271   result = receive(decoder->async.in, &message, &size);
00272 
00273   if (result == MAD_FLOW_CONTINUE) {
00274     if (decoder->message_func == 0)
00275       size = 0;
00276     else {
00277       result = decoder->message_func(decoder->cb_data, message, &size);
00278 
00279       if (result == MAD_FLOW_IGNORE ||
00280           result == MAD_FLOW_BREAK)
00281         size = 0;
00282     }
00283 
00284     if (send(decoder->async.out, message, size) != MAD_FLOW_CONTINUE)
00285       result = MAD_FLOW_BREAK;
00286   }
00287 
00288   if (message)
00289     free(message);
00290 
00291   return result;
00292 }
00293 # endif
00294 
00295 static
00296 enum mad_flow error_default(void *data, struct mad_stream *stream,
00297                             struct mad_frame *frame)
00298 {
00299   int *bad_last_frame = data;
00300 
00301   switch (stream->error) {
00302   case MAD_ERROR_BADCRC:
00303     if (*bad_last_frame)
00304       mad_frame_mute(frame);
00305     else
00306       *bad_last_frame = 1;
00307 
00308     return MAD_FLOW_IGNORE;
00309 
00310   default:
00311     return MAD_FLOW_CONTINUE;
00312   }
00313 }
00314 
00315 static
00316 int run_sync(struct mad_decoder *decoder)
00317 {
00318   enum mad_flow (*error_func)(void *, struct mad_stream *, struct mad_frame *);
00319   void *error_data;
00320   int bad_last_frame = 0;
00321   struct mad_stream *stream;
00322   struct mad_frame *frame;
00323   struct mad_synth *synth;
00324   int result = 0;
00325 
00326   if (decoder->input_func == 0)
00327     return 0;
00328 
00329   if (decoder->error_func) {
00330     error_func = decoder->error_func;
00331     error_data = decoder->cb_data;
00332   }
00333   else {
00334     error_func = error_default;
00335     error_data = &bad_last_frame;
00336   }
00337 
00338   stream = &decoder->sync->stream;
00339   frame  = &decoder->sync->frame;
00340   synth  = &decoder->sync->synth;
00341 
00342   mad_stream_init(stream);
00343   mad_frame_init(frame);
00344   mad_synth_init(synth);
00345 
00346   mad_stream_options(stream, decoder->options);
00347 
00348   do {
00349     switch (decoder->input_func(decoder->cb_data, stream)) {
00350     case MAD_FLOW_STOP:
00351       goto done;
00352     case MAD_FLOW_BREAK:
00353       goto fail;
00354     case MAD_FLOW_IGNORE:
00355       continue;
00356     case MAD_FLOW_CONTINUE:
00357       break;
00358     }
00359 
00360     while (1) {
00361 # if defined(USE_ASYNC)
00362       if (decoder->mode == MAD_DECODER_MODE_ASYNC) {
00363         switch (check_message(decoder)) {
00364         case MAD_FLOW_IGNORE:
00365         case MAD_FLOW_CONTINUE:
00366           break;
00367         case MAD_FLOW_BREAK:
00368           goto fail;
00369         case MAD_FLOW_STOP:
00370           goto done;
00371         }
00372       }
00373 # endif
00374 
00375       if (decoder->header_func) {
00376         if (mad_header_decode(&frame->header, stream) == -1) {
00377           if (!MAD_RECOVERABLE(stream->error))
00378             break;
00379 
00380           switch (error_func(error_data, stream, frame)) {
00381           case MAD_FLOW_STOP:
00382             goto done;
00383           case MAD_FLOW_BREAK:
00384             goto fail;
00385           case MAD_FLOW_IGNORE:
00386           case MAD_FLOW_CONTINUE:
00387           default:
00388             continue;
00389           }
00390         }
00391 
00392         switch (decoder->header_func(decoder->cb_data, &frame->header)) {
00393         case MAD_FLOW_STOP:
00394           goto done;
00395         case MAD_FLOW_BREAK:
00396           goto fail;
00397         case MAD_FLOW_IGNORE:
00398           continue;
00399         case MAD_FLOW_CONTINUE:
00400           break;
00401         }
00402       }
00403 
00404       if (mad_frame_decode(frame, stream) == -1) {
00405         if (!MAD_RECOVERABLE(stream->error))
00406           break;
00407 
00408         switch (error_func(error_data, stream, frame)) {
00409         case MAD_FLOW_STOP:
00410           goto done;
00411         case MAD_FLOW_BREAK:
00412           goto fail;
00413         case MAD_FLOW_IGNORE:
00414           break;
00415         case MAD_FLOW_CONTINUE:
00416         default:
00417           continue;
00418         }
00419       }
00420       else
00421         bad_last_frame = 0;
00422 
00423       if (decoder->filter_func) {
00424         switch (decoder->filter_func(decoder->cb_data, stream, frame)) {
00425         case MAD_FLOW_STOP:
00426           goto done;
00427         case MAD_FLOW_BREAK:
00428           goto fail;
00429         case MAD_FLOW_IGNORE:
00430           continue;
00431         case MAD_FLOW_CONTINUE:
00432           break;
00433         }
00434       }
00435 
00436       mad_synth_frame(synth, frame);
00437 
00438       if (decoder->output_func) {
00439         switch (decoder->output_func(decoder->cb_data,
00440                                      &frame->header, &synth->pcm)) {
00441         case MAD_FLOW_STOP:
00442           goto done;
00443         case MAD_FLOW_BREAK:
00444           goto fail;
00445         case MAD_FLOW_IGNORE:
00446         case MAD_FLOW_CONTINUE:
00447           break;
00448         }
00449       }
00450     }
00451   }
00452   while (stream->error == MAD_ERROR_BUFLEN);
00453 
00454  fail:
00455   result = -1;
00456 
00457  done:
00458   mad_synth_finish(synth);
00459   mad_frame_finish(frame);
00460   mad_stream_finish(stream);
00461 
00462   return result;
00463 }
00464 
00465 # if defined(USE_ASYNC)
00466 static
00467 int run_async(struct mad_decoder *decoder)
00468 {
00469   pid_t pid;
00470   int ptoc[2], ctop[2], flags;
00471 
00472   if (pipe(ptoc) == -1)
00473     return -1;
00474 
00475   if (pipe(ctop) == -1) {
00476     close(ptoc[0]);
00477     close(ptoc[1]);
00478     return -1;
00479   }
00480 
00481   flags = fcntl(ptoc[0], F_GETFL);
00482   if (flags == -1 ||
00483       fcntl(ptoc[0], F_SETFL, flags | O_NONBLOCK) == -1) {
00484     close(ctop[0]);
00485     close(ctop[1]);
00486     close(ptoc[0]);
00487     close(ptoc[1]);
00488     return -1;
00489   }
00490 
00491   pid = fork();
00492   if (pid == -1) {
00493     close(ctop[0]);
00494     close(ctop[1]);
00495     close(ptoc[0]);
00496     close(ptoc[1]);
00497     return -1;
00498   }
00499 
00500   decoder->async.pid = pid;
00501 
00502   if (pid) {
00503     /* parent */
00504 
00505     close(ptoc[0]);
00506     close(ctop[1]);
00507 
00508     decoder->async.in  = ctop[0];
00509     decoder->async.out = ptoc[1];
00510 
00511     return 0;
00512   }
00513 
00514   /* child */
00515 
00516   close(ptoc[1]);
00517   close(ctop[0]);
00518 
00519   decoder->async.in  = ptoc[0];
00520   decoder->async.out = ctop[1];
00521 
00522   _exit(run_sync(decoder));
00523 
00524   /* not reached */
00525   return -1;
00526 }
00527 # endif
00528 
00529 /*
00530  * NAME:        decoder->run()
00531  * DESCRIPTION: run the decoder thread either synchronously or asynchronously
00532  */
00533 int mad_decoder_run(struct mad_decoder *decoder, enum mad_decoder_mode mode)
00534 {
00535   int result;
00536   int (*run)(struct mad_decoder *) = 0;
00537 
00538   switch (decoder->mode = mode) {
00539   case MAD_DECODER_MODE_SYNC:
00540     run = run_sync;
00541     break;
00542 
00543   case MAD_DECODER_MODE_ASYNC:
00544 # if defined(USE_ASYNC)
00545     run = run_async;
00546 # endif
00547     break;
00548   }
00549 
00550   if (run == 0)
00551     return -1;
00552 
00553   decoder->sync = malloc(sizeof(*decoder->sync));
00554   if (decoder->sync == 0)
00555     return -1;
00556 
00557   result = run(decoder);
00558 
00559   free(decoder->sync);
00560   decoder->sync = 0;
00561 
00562   return result;
00563 }
00564 
00565 /*
00566  * NAME:        decoder->message()
00567  * DESCRIPTION: send a message to and receive a reply from the decoder process
00568  */
00569 int mad_decoder_message(struct mad_decoder *decoder,
00570                         void *message, unsigned int *len)
00571 {
00572 # if defined(USE_ASYNC)
00573   if (decoder->mode != MAD_DECODER_MODE_ASYNC ||
00574       send(decoder->async.out, message, *len) != MAD_FLOW_CONTINUE ||
00575       receive(decoder->async.in, &message, len) != MAD_FLOW_CONTINUE)
00576     return -1;
00577 
00578   return 0;
00579 # else
00580   return -1;
00581 # endif
00582 }

Generated on Tue Dec 13 14:47:30 2005 for guliverkli by  doxygen 1.4.5