stream.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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: stream.c,v 1.12 2004/02/05 09:02:39 rob Exp $
  20. */
  21. # include "libmad_config.h"
  22. # include "libmad_global.h"
  23. # include <stdlib.h>
  24. # include "bit.h"
  25. # include "stream.h"
  26. /*
  27. * NAME: stream->init()
  28. * DESCRIPTION: initialize stream struct
  29. */
  30. void mad_stream_init(struct mad_stream *stream)
  31. {
  32. stream->buffer = 0;
  33. stream->bufend = 0;
  34. stream->skiplen = 0;
  35. stream->sync = 0;
  36. stream->freerate = 0;
  37. stream->this_frame = 0;
  38. stream->next_frame = 0;
  39. mad_bit_init(&stream->ptr, 0);
  40. mad_bit_init(&stream->anc_ptr, 0);
  41. stream->anc_bitlen = 0;
  42. stream->main_data = 0;
  43. stream->md_len = 0;
  44. stream->options = 0;
  45. stream->error = MAD_ERROR_NONE;
  46. }
  47. /*
  48. * NAME: stream->finish()
  49. * DESCRIPTION: deallocate any dynamic memory associated with stream
  50. */
  51. void mad_stream_finish(struct mad_stream *stream)
  52. {
  53. if (stream->main_data) {
  54. free(stream->main_data);
  55. stream->main_data = 0;
  56. }
  57. mad_bit_finish(&stream->anc_ptr);
  58. mad_bit_finish(&stream->ptr);
  59. }
  60. /*
  61. * NAME: stream->buffer()
  62. * DESCRIPTION: set stream buffer pointers
  63. */
  64. void mad_stream_buffer(struct mad_stream *stream,
  65. unsigned char const *buffer, unsigned long length)
  66. {
  67. stream->buffer = buffer;
  68. stream->bufend = buffer + length;
  69. stream->this_frame = buffer;
  70. stream->next_frame = buffer;
  71. stream->sync = 1;
  72. mad_bit_init(&stream->ptr, buffer);
  73. }
  74. /*
  75. * NAME: stream->skip()
  76. * DESCRIPTION: arrange to skip bytes before the next frame
  77. */
  78. void mad_stream_skip(struct mad_stream *stream, unsigned long length)
  79. {
  80. stream->skiplen += length;
  81. }
  82. /*
  83. * NAME: stream->sync()
  84. * DESCRIPTION: locate the next stream sync word
  85. */
  86. int mad_stream_sync(struct mad_stream *stream)
  87. {
  88. register unsigned char const *ptr, *end;
  89. ptr = mad_bit_nextbyte(&stream->ptr);
  90. end = stream->bufend;
  91. while (ptr < end - 1 &&
  92. !(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0))
  93. ++ptr;
  94. if (end - ptr < MAD_BUFFER_GUARD)
  95. return -1;
  96. mad_bit_init(&stream->ptr, ptr);
  97. return 0;
  98. }
  99. /*
  100. * NAME: stream->errorstr()
  101. * DESCRIPTION: return a string description of the current error condition
  102. */
  103. char const *mad_stream_errorstr(struct mad_stream const *stream)
  104. {
  105. switch (stream->error) {
  106. case MAD_ERROR_NONE: return "no error";
  107. case MAD_ERROR_BUFLEN: return "input buffer too small (or EOF)";
  108. case MAD_ERROR_BUFPTR: return "invalid (null) buffer pointer";
  109. case MAD_ERROR_NOMEM: return "not enough memory";
  110. case MAD_ERROR_LOSTSYNC: return "lost synchronization";
  111. case MAD_ERROR_BADLAYER: return "reserved header layer value";
  112. case MAD_ERROR_BADBITRATE: return "forbidden bitrate value";
  113. case MAD_ERROR_BADSAMPLERATE: return "reserved sample frequency value";
  114. case MAD_ERROR_BADEMPHASIS: return "reserved emphasis value";
  115. case MAD_ERROR_BADCRC: return "CRC check failed";
  116. case MAD_ERROR_BADBITALLOC: return "forbidden bit allocation value";
  117. case MAD_ERROR_BADSCALEFACTOR: return "bad scalefactor index";
  118. case MAD_ERROR_BADMODE: return "bad bitrate/mode combination";
  119. case MAD_ERROR_BADFRAMELEN: return "bad frame length";
  120. case MAD_ERROR_BADBIGVALUES: return "bad big_values count";
  121. case MAD_ERROR_BADBLOCKTYPE: return "reserved block_type";
  122. case MAD_ERROR_BADSCFSI: return "bad scalefactor selection info";
  123. case MAD_ERROR_BADDATAPTR: return "bad main_data_begin pointer";
  124. case MAD_ERROR_BADPART3LEN: return "bad audio data length";
  125. case MAD_ERROR_BADHUFFTABLE: return "bad Huffman table select";
  126. case MAD_ERROR_BADHUFFDATA: return "Huffman data overrun";
  127. case MAD_ERROR_BADSTEREO: return "incompatible block_type for JS";
  128. }
  129. return 0;
  130. }