Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
5.8 KiB

  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: minimad.c,v 1.4 2004/01/23 09:41:32 rob Exp $
  20. */
  21. # include <stdio.h>
  22. # include <unistd.h>
  23. # include <sys/stat.h>
  24. # include <sys/mman.h>
  25. # include "mad.h"
  26. /*
  27. * This is perhaps the simplest example use of the MAD high-level API.
  28. * Standard input is mapped into memory via mmap(), then the high-level API
  29. * is invoked with three callbacks: input, output, and error. The output
  30. * callback converts MAD's high-resolution PCM samples to 16 bits, then
  31. * writes them to standard output in little-endian, stereo-interleaved
  32. * format.
  33. */
  34. static int decode(unsigned char const *, unsigned long);
  35. int main(int argc, char *argv[])
  36. {
  37. struct stat stat;
  38. void *fdm;
  39. if (argc != 1)
  40. return 1;
  41. if (fstat(STDIN_FILENO, &stat) == -1 ||
  42. stat.st_size == 0)
  43. return 2;
  44. fdm = mmap(0, stat.st_size, PROT_READ, MAP_SHARED, STDIN_FILENO, 0);
  45. if (fdm == MAP_FAILED)
  46. return 3;
  47. decode(fdm, stat.st_size);
  48. if (munmap(fdm, stat.st_size) == -1)
  49. return 4;
  50. return 0;
  51. }
  52. /*
  53. * This is a private message structure. A generic pointer to this structure
  54. * is passed to each of the callback functions. Put here any data you need
  55. * to access from within the callbacks.
  56. */
  57. struct buffer {
  58. unsigned char const *start;
  59. unsigned long length;
  60. };
  61. /*
  62. * This is the input callback. The purpose of this callback is to (re)fill
  63. * the stream buffer which is to be decoded. In this example, an entire file
  64. * has been mapped into memory, so we just call mad_stream_buffer() with the
  65. * address and length of the mapping. When this callback is called a second
  66. * time, we are finished decoding.
  67. */
  68. static
  69. enum mad_flow input(void *data,
  70. struct mad_stream *stream)
  71. {
  72. struct buffer *buffer = data;
  73. if (!buffer->length)
  74. return MAD_FLOW_STOP;
  75. mad_stream_buffer(stream, buffer->start, buffer->length);
  76. buffer->length = 0;
  77. return MAD_FLOW_CONTINUE;
  78. }
  79. /*
  80. * The following utility routine performs simple rounding, clipping, and
  81. * scaling of MAD's high-resolution samples down to 16 bits. It does not
  82. * perform any dithering or noise shaping, which would be recommended to
  83. * obtain any exceptional audio quality. It is therefore not recommended to
  84. * use this routine if high-quality output is desired.
  85. */
  86. static inline
  87. signed int scale(mad_fixed_t sample)
  88. {
  89. /* round */
  90. sample += (1L << (MAD_F_FRACBITS - 16));
  91. /* clip */
  92. if (sample >= MAD_F_ONE)
  93. sample = MAD_F_ONE - 1;
  94. else if (sample < -MAD_F_ONE)
  95. sample = -MAD_F_ONE;
  96. /* quantize */
  97. return sample >> (MAD_F_FRACBITS + 1 - 16);
  98. }
  99. /*
  100. * This is the output callback function. It is called after each frame of
  101. * MPEG audio data has been completely decoded. The purpose of this callback
  102. * is to output (or play) the decoded PCM audio.
  103. */
  104. static
  105. enum mad_flow output(void *data,
  106. struct mad_header const *header,
  107. struct mad_pcm *pcm)
  108. {
  109. unsigned int nchannels, nsamples;
  110. mad_fixed_t const *left_ch, *right_ch;
  111. /* pcm->samplerate contains the sampling frequency */
  112. nchannels = pcm->channels;
  113. nsamples = pcm->length;
  114. left_ch = pcm->samples[0];
  115. right_ch = pcm->samples[1];
  116. while (nsamples--) {
  117. signed int sample;
  118. /* output sample(s) in 16-bit signed little-endian PCM */
  119. sample = scale(*left_ch++);
  120. putchar((sample >> 0) & 0xff);
  121. putchar((sample >> 8) & 0xff);
  122. if (nchannels == 2) {
  123. sample = scale(*right_ch++);
  124. putchar((sample >> 0) & 0xff);
  125. putchar((sample >> 8) & 0xff);
  126. }
  127. }
  128. return MAD_FLOW_CONTINUE;
  129. }
  130. /*
  131. * This is the error callback function. It is called whenever a decoding
  132. * error occurs. The error is indicated by stream->error; the list of
  133. * possible MAD_ERROR_* errors can be found in the mad.h (or stream.h)
  134. * header file.
  135. */
  136. static
  137. enum mad_flow error(void *data,
  138. struct mad_stream *stream,
  139. struct mad_frame *frame)
  140. {
  141. struct buffer *buffer = data;
  142. fprintf(stderr, "decoding error 0x%04x (%s) at byte offset %u\n",
  143. stream->error, mad_stream_errorstr(stream),
  144. stream->this_frame - buffer->start);
  145. /* return MAD_FLOW_BREAK here to stop decoding (and propagate an error) */
  146. return MAD_FLOW_CONTINUE;
  147. }
  148. /*
  149. * This is the function called by main() above to perform all the decoding.
  150. * It instantiates a decoder object and configures it with the input,
  151. * output, and error callback functions above. A single call to
  152. * mad_decoder_run() continues until a callback function returns
  153. * MAD_FLOW_STOP (to stop decoding) or MAD_FLOW_BREAK (to stop decoding and
  154. * signal an error).
  155. */
  156. static
  157. int decode(unsigned char const *start, unsigned long length)
  158. {
  159. struct buffer buffer;
  160. struct mad_decoder decoder;
  161. int result;
  162. /* initialize our private message structure */
  163. buffer.start = start;
  164. buffer.length = length;
  165. /* configure input, output, and error functions */
  166. mad_decoder_init(&decoder, &buffer,
  167. input, 0 /* header */, 0 /* filter */, output,
  168. error, 0 /* message */);
  169. /* start decoding */
  170. result = mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC);
  171. /* release the decoder */
  172. mad_decoder_finish(&decoder);
  173. return result;
  174. }