/project/jni/sdl_sound/decoders/raw.c

https://github.com/aichunyu/FFPlayer · C · 184 lines · 89 code · 37 blank · 58 comment · 14 complexity · 244fabcf8c9622c3fa0b0bd9eaf50237 MD5 · raw file

  1. /*
  2. * SDL_sound -- An abstract sound format decoding API.
  3. * Copyright (C) 2001 Ryan C. Gordon.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library 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 GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /*
  20. * RAW decoder for SDL_sound. This is as simple as it gets.
  21. *
  22. * This driver handles raw audio data. You must, regardless of where the
  23. * data is actually coming from, specify the string "RAW" in the extension
  24. * parameter of Sound_NewSample() (or, alternately, open a file with the
  25. * extension ".raw" in Sound_NewSampleFromFile()). The string is checked
  26. * case-insensitive. We need this check, because raw data, being raw, has
  27. * no headers or magic number we can use to determine if we should handle a
  28. * given file, so we needed some way to have this "decoder" discriminate.
  29. *
  30. * When calling Sound_NewSample*(), you must also specify a "desired"
  31. * audio format. The "actual" format will always match what you specify, so
  32. * there will be no conversion overhead, but these routines need to know how
  33. * to treat the bits, since it's all random garbage otherwise.
  34. *
  35. * Please see the file COPYING in the source's root directory.
  36. *
  37. * This file written by Ryan C. Gordon. (icculus@icculus.org)
  38. */
  39. #if HAVE_CONFIG_H
  40. # include <config.h>
  41. #endif
  42. #ifdef SOUND_SUPPORTS_RAW
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include "SDL_sound.h"
  47. #define __SDL_SOUND_INTERNAL__
  48. #include "SDL_sound_internal.h"
  49. static int RAW_init(void);
  50. static void RAW_quit(void);
  51. static int RAW_open(Sound_Sample *sample, const char *ext);
  52. static void RAW_close(Sound_Sample *sample);
  53. static Uint32 RAW_read(Sound_Sample *sample);
  54. static int RAW_rewind(Sound_Sample *sample);
  55. static int RAW_seek(Sound_Sample *sample, Uint32 ms);
  56. static const char *extensions_raw[] = { "RAW", NULL };
  57. const Sound_DecoderFunctions __Sound_DecoderFunctions_RAW =
  58. {
  59. {
  60. extensions_raw,
  61. "Raw audio",
  62. "Ryan C. Gordon <icculus@icculus.org>",
  63. "http://www.icculus.org/SDL_sound/"
  64. },
  65. RAW_init, /* init() method */
  66. RAW_quit, /* quit() method */
  67. RAW_open, /* open() method */
  68. RAW_close, /* close() method */
  69. RAW_read, /* read() method */
  70. RAW_rewind, /* rewind() method */
  71. RAW_seek /* seek() method */
  72. };
  73. static int RAW_init(void)
  74. {
  75. return(1); /* always succeeds. */
  76. } /* RAW_init */
  77. static void RAW_quit(void)
  78. {
  79. /* it's a no-op. */
  80. } /* RAW_quit */
  81. static int RAW_open(Sound_Sample *sample, const char *ext)
  82. {
  83. /*
  84. * We check this explicitly, since we have no other way to
  85. * determine whether we should handle this data or not.
  86. */
  87. if (__Sound_strcasecmp(ext, "RAW") != 0)
  88. BAIL_MACRO("RAW: extension isn't explicitly \"RAW\".", 0);
  89. /*
  90. * You must also specify a desired format, so we know how to
  91. * treat the bits that are otherwise binary garbage.
  92. */
  93. if ( (sample->desired.channels < 1) ||
  94. (sample->desired.channels > 2) ||
  95. (sample->desired.rate == 0) ||
  96. (sample->desired.format == 0) )
  97. {
  98. BAIL_MACRO("RAW: invalid desired format.", 0);
  99. } /* if */
  100. SNDDBG(("RAW: Accepting data stream.\n"));
  101. /*
  102. * We never convert raw samples; what you ask for is what you get.
  103. */
  104. memcpy(&sample->actual, &sample->desired, sizeof (Sound_AudioInfo));
  105. sample->flags = SOUND_SAMPLEFLAG_CANSEEK;
  106. return(1); /* we'll handle this data. */
  107. } /* RAW_open */
  108. static void RAW_close(Sound_Sample *sample)
  109. {
  110. /* we don't allocate anything that we need to free. That's easy, eh? */
  111. } /* RAW_close */
  112. static Uint32 RAW_read(Sound_Sample *sample)
  113. {
  114. Uint32 retval;
  115. Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
  116. /*
  117. * We don't actually do any decoding, so we read the raw data
  118. * directly into the internal buffer...
  119. */
  120. retval = SDL_RWread(internal->rw, internal->buffer,
  121. 1, internal->buffer_size);
  122. /* Make sure the read went smoothly... */
  123. if (retval == 0)
  124. sample->flags |= SOUND_SAMPLEFLAG_EOF;
  125. else if (retval == -1)
  126. sample->flags |= SOUND_SAMPLEFLAG_ERROR;
  127. /* (next call this EAGAIN may turn into an EOF or error.) */
  128. else if (retval < internal->buffer_size)
  129. sample->flags |= SOUND_SAMPLEFLAG_EAGAIN;
  130. return(retval);
  131. } /* RAW_read */
  132. static int RAW_rewind(Sound_Sample *sample)
  133. {
  134. Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
  135. BAIL_IF_MACRO(SDL_RWseek(internal->rw, 0, SEEK_SET) != 0, ERR_IO_ERROR, 0);
  136. return(1);
  137. } /* RAW_rewind */
  138. static int RAW_seek(Sound_Sample *sample, Uint32 ms)
  139. {
  140. Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
  141. int pos = (int) __Sound_convertMsToBytePos(&sample->actual, ms);
  142. int err = (SDL_RWseek(internal->rw, pos, SEEK_SET) != pos);
  143. BAIL_IF_MACRO(err, ERR_IO_ERROR, 0);
  144. return(1);
  145. } /* RAW_seek */
  146. #endif /* SOUND_SUPPORTS_RAW */
  147. /* end of raw.c ... */