00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 # ifdef HAVE_CONFIG_H
00023 # include "config.h"
00024 # endif
00025
00026 # include "global.h"
00027
00028 # include <stdlib.h>
00029
00030 # include "bit.h"
00031 # include "stream.h"
00032
00033
00034
00035
00036
00037 void mad_stream_init(struct mad_stream *stream)
00038 {
00039 stream->buffer = 0;
00040 stream->bufend = 0;
00041 stream->skiplen = 0;
00042
00043 stream->sync = 0;
00044 stream->freerate = 0;
00045
00046 stream->this_frame = 0;
00047 stream->next_frame = 0;
00048 mad_bit_init(&stream->ptr, 0);
00049
00050 mad_bit_init(&stream->anc_ptr, 0);
00051 stream->anc_bitlen = 0;
00052
00053 stream->main_data = 0;
00054 stream->md_len = 0;
00055
00056 stream->options = 0;
00057 stream->error = MAD_ERROR_NONE;
00058 }
00059
00060
00061
00062
00063
00064 void mad_stream_finish(struct mad_stream *stream)
00065 {
00066 if (stream->main_data) {
00067 free(stream->main_data);
00068 stream->main_data = 0;
00069 }
00070
00071 mad_bit_finish(&stream->anc_ptr);
00072 mad_bit_finish(&stream->ptr);
00073 }
00074
00075
00076
00077
00078
00079 void mad_stream_buffer(struct mad_stream *stream,
00080 unsigned char const *buffer, unsigned long length)
00081 {
00082 stream->buffer = buffer;
00083 stream->bufend = buffer + length;
00084
00085 stream->this_frame = buffer;
00086 stream->next_frame = buffer;
00087
00088 stream->sync = 1;
00089
00090 mad_bit_init(&stream->ptr, buffer);
00091 }
00092
00093
00094
00095
00096
00097 void mad_stream_skip(struct mad_stream *stream, unsigned long length)
00098 {
00099 stream->skiplen += length;
00100 }
00101
00102
00103
00104
00105
00106 int mad_stream_sync(struct mad_stream *stream)
00107 {
00108 register unsigned char const *ptr, *end;
00109
00110 ptr = mad_bit_nextbyte(&stream->ptr);
00111 end = stream->bufend;
00112
00113 while (ptr < end - 1 &&
00114 !(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0))
00115 ++ptr;
00116
00117 if (end - ptr < MAD_BUFFER_GUARD)
00118 return -1;
00119
00120 mad_bit_init(&stream->ptr, ptr);
00121
00122 return 0;
00123 }
00124
00125
00126
00127
00128
00129 char const *mad_stream_errorstr(struct mad_stream const *stream)
00130 {
00131 switch (stream->error) {
00132 case MAD_ERROR_NONE: return "no error";
00133
00134 case MAD_ERROR_BUFLEN: return "input buffer too small (or EOF)";
00135 case MAD_ERROR_BUFPTR: return "invalid (null) buffer pointer";
00136
00137 case MAD_ERROR_NOMEM: return "not enough memory";
00138
00139 case MAD_ERROR_LOSTSYNC: return "lost synchronization";
00140 case MAD_ERROR_BADLAYER: return "reserved header layer value";
00141 case MAD_ERROR_BADBITRATE: return "forbidden bitrate value";
00142 case MAD_ERROR_BADSAMPLERATE: return "reserved sample frequency value";
00143 case MAD_ERROR_BADEMPHASIS: return "reserved emphasis value";
00144
00145 case MAD_ERROR_BADCRC: return "CRC check failed";
00146 case MAD_ERROR_BADBITALLOC: return "forbidden bit allocation value";
00147 case MAD_ERROR_BADSCALEFACTOR: return "bad scalefactor index";
00148 case MAD_ERROR_BADFRAMELEN: return "bad frame length";
00149 case MAD_ERROR_BADBIGVALUES: return "bad big_values count";
00150 case MAD_ERROR_BADBLOCKTYPE: return "reserved block_type";
00151 case MAD_ERROR_BADSCFSI: return "bad scalefactor selection info";
00152 case MAD_ERROR_BADDATAPTR: return "bad main_data_begin pointer";
00153 case MAD_ERROR_BADPART3LEN: return "bad audio data length";
00154 case MAD_ERROR_BADHUFFTABLE: return "bad Huffman table select";
00155 case MAD_ERROR_BADHUFFDATA: return "Huffman data overrun";
00156 case MAD_ERROR_BADSTEREO: return "incompatible block_type for JS";
00157 }
00158
00159 return 0;
00160 }