/project/jni/sdl_sound/decoders/ogg.c

https://github.com/aichunyu/FFPlayer · C · 375 lines · 211 code · 57 blank · 107 comment · 24 complexity · eed78659c66d552d892474ff616aa8ef 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. * Ogg Vorbis decoder for SDL_sound.
  21. *
  22. * This driver handles .OGG audio files, and depends on libvorbisfile to
  23. * do the actual decoding work. libvorbisfile is part of libvorbis, which
  24. * is part of the Ogg Vorbis project.
  25. *
  26. * Ogg Vorbis: http://www.xiph.org/ogg/vorbis/
  27. * vorbisfile documentation: http://www.xiph.org/ogg/vorbis/doc/vorbisfile/
  28. *
  29. * Please see the file COPYING in the source's root directory.
  30. *
  31. * This file written by Ryan C. Gordon. (icculus@icculus.org)
  32. */
  33. #if HAVE_CONFIG_H
  34. # include <config.h>
  35. #endif
  36. #ifdef SOUND_SUPPORTS_OGG
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <math.h>
  41. #include "SDL_sound.h"
  42. #define __SDL_SOUND_INTERNAL__
  43. #include "SDL_sound_internal.h"
  44. #include <vorbis/codec.h>
  45. #include <vorbis/vorbisfile.h>
  46. static int OGG_init(void);
  47. static void OGG_quit(void);
  48. static int OGG_open(Sound_Sample *sample, const char *ext);
  49. static void OGG_close(Sound_Sample *sample);
  50. static Uint32 OGG_read(Sound_Sample *sample);
  51. static int OGG_rewind(Sound_Sample *sample);
  52. static int OGG_seek(Sound_Sample *sample, Uint32 ms);
  53. static const char *extensions_ogg[] = { "OGG", NULL };
  54. const Sound_DecoderFunctions __Sound_DecoderFunctions_OGG =
  55. {
  56. {
  57. extensions_ogg,
  58. "Ogg Vorbis audio through VorbisFile",
  59. "Ryan C. Gordon <icculus@icculus.org>",
  60. "http://www.icculus.org/SDL_sound/"
  61. },
  62. OGG_init, /* init() method */
  63. OGG_quit, /* quit() method */
  64. OGG_open, /* open() method */
  65. OGG_close, /* close() method */
  66. OGG_read, /* read() method */
  67. OGG_rewind, /* rewind() method */
  68. OGG_seek /* seek() method */
  69. };
  70. static int OGG_init(void)
  71. {
  72. return(1); /* always succeeds. */
  73. } /* OGG_init */
  74. static void OGG_quit(void)
  75. {
  76. /* it's a no-op. */
  77. } /* OGG_quit */
  78. /*
  79. * These are callbacks from vorbisfile that let them read data from
  80. * a RWops...
  81. */
  82. static size_t RWops_ogg_read(void *ptr, size_t size, size_t nmemb, void *datasource)
  83. {
  84. return((size_t) SDL_RWread((SDL_RWops *) datasource, ptr, size, nmemb));
  85. } /* RWops_ogg_read */
  86. static int RWops_ogg_seek(void *datasource, ogg_int64_t offset, int whence)
  87. {
  88. return(SDL_RWseek((SDL_RWops *) datasource, offset, whence));
  89. } /* RWops_ogg_seek */
  90. static int RWops_ogg_close(void *datasource)
  91. {
  92. /* do nothing; SDL_sound will delete the RWops at a higher level. */
  93. return(0); /* this is success in fclose(), so I guess that's okay. */
  94. } /* RWops_ogg_close */
  95. static long RWops_ogg_tell(void *datasource)
  96. {
  97. return((long) SDL_RWtell((SDL_RWops *) datasource));
  98. } /* RWops_ogg_tell */
  99. static const ov_callbacks RWops_ogg_callbacks =
  100. {
  101. RWops_ogg_read,
  102. RWops_ogg_seek,
  103. RWops_ogg_close,
  104. RWops_ogg_tell
  105. };
  106. /* Return a human readable version of an VorbisFile error code... */
  107. #if (defined DEBUG_CHATTER)
  108. static const char *ogg_error(int errnum)
  109. {
  110. switch(errnum)
  111. {
  112. case OV_EREAD:
  113. return("i/o error");
  114. case OV_ENOTVORBIS:
  115. return("not a vorbis file");
  116. case OV_EVERSION:
  117. return("Vorbis version mismatch");
  118. case OV_EBADHEADER:
  119. return("invalid Vorbis bitstream header");
  120. case OV_EFAULT:
  121. return("internal logic fault in Vorbis library");
  122. } /* switch */
  123. return("unknown error");
  124. } /* ogg_error */
  125. #endif
  126. static __inline__ void output_ogg_comments(OggVorbis_File *vf)
  127. {
  128. #if (defined DEBUG_CHATTER)
  129. int i;
  130. vorbis_comment *vc = ov_comment(vf, -1);
  131. if (vc == NULL)
  132. return;
  133. SNDDBG(("OGG: vendor == [%s].\n", vc->vendor));
  134. for (i = 0; i < vc->comments; i++)
  135. {
  136. SNDDBG(("OGG: user comment [%s].\n", vc->user_comments[i]));
  137. } /* for */
  138. #endif
  139. } /* output_ogg_comments */
  140. static int OGG_open(Sound_Sample *sample, const char *ext)
  141. {
  142. int rc;
  143. OggVorbis_File *vf;
  144. vorbis_info *info;
  145. Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
  146. vf = (OggVorbis_File *) malloc(sizeof (OggVorbis_File));
  147. BAIL_IF_MACRO(vf == NULL, ERR_OUT_OF_MEMORY, 0);
  148. rc = ov_open_callbacks(internal->rw, vf, NULL, 0, RWops_ogg_callbacks);
  149. if (rc != 0)
  150. {
  151. SNDDBG(("OGG: can't grok data. reason: [%s].\n", ogg_error(rc)));
  152. free(vf);
  153. BAIL_MACRO("OGG: Not valid Ogg Vorbis data.", 0);
  154. } /* if */
  155. info = ov_info(vf, -1);
  156. if (info == NULL)
  157. {
  158. ov_clear(vf);
  159. free(vf);
  160. BAIL_MACRO("OGG: failed to retrieve bitstream info", 0);
  161. } /* if */
  162. SNDDBG(("OGG: Accepting data stream.\n"));
  163. output_ogg_comments(vf);
  164. SNDDBG(("OGG: bitstream version == (%d).\n", info->version));
  165. SNDDBG(("OGG: bitstream channels == (%d).\n", info->channels));
  166. SNDDBG(("OGG: bitstream sampling rate == (%ld).\n", info->rate));
  167. SNDDBG(("OGG: seekable == {%s}.\n", ov_seekable(vf) ? "TRUE" : "FALSE"));
  168. SNDDBG(("OGG: number of logical bitstreams == (%ld).\n", ov_streams(vf)));
  169. SNDDBG(("OGG: serial number == (%ld).\n", ov_serialnumber(vf, -1)));
  170. SNDDBG(("OGG: total seconds of sample == (%f).\n", ov_time_total(vf, -1)));
  171. internal->decoder_private = vf;
  172. sample->flags = SOUND_SAMPLEFLAG_CANSEEK;
  173. sample->actual.rate = (Uint32) info->rate;
  174. sample->actual.channels = (Uint8) info->channels;
  175. /*
  176. * Since we might have more than one logical bitstream in the OGG file,
  177. * and these bitstreams may be in different formats, we might be
  178. * converting two or three times: once in vorbisfile, once again in
  179. * SDL_sound, and perhaps a third time to get it to the sound device's
  180. * format. That's wickedly inefficient.
  181. *
  182. * To combat this a little, if the user specified a desired format, we
  183. * claim that to be the "actual" format of the collection of logical
  184. * bitstreams. This means that VorbisFile will do a conversion as
  185. * necessary, and SDL_sound will not. If the user didn't specify a
  186. * desired format, then we pretend the "actual" format is something that
  187. * OGG files are apparently commonly encoded in.
  188. */
  189. sample->actual.format = (sample->desired.format == 0) ?
  190. AUDIO_S16LSB : sample->desired.format;
  191. return(1);
  192. } /* OGG_open */
  193. static void OGG_close(Sound_Sample *sample)
  194. {
  195. Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
  196. OggVorbis_File *vf = (OggVorbis_File *) internal->decoder_private;
  197. ov_clear(vf);
  198. free(vf);
  199. } /* OGG_close */
  200. /* Note: According to the Vorbis documentation:
  201. * "ov_read() will decode at most one vorbis packet per invocation,
  202. * so the value returned will generally be less than length."
  203. * Due to this, for buffer sizes like 16384, SDL_Sound was always getting
  204. * an underfilled buffer and always setting the EAGAIN flag.
  205. * Since the SDL_Sound API implies that the entire buffer
  206. * should be filled unless EOF, additional code has been added
  207. * to this function to call ov_read() until the buffer is filled.
  208. * However, there may still be some corner cases where the buffer
  209. * cannot be entirely filled. So be aware.
  210. */
  211. static Uint32 OGG_read(Sound_Sample *sample)
  212. {
  213. int rc;
  214. int bitstream;
  215. Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
  216. OggVorbis_File *vf = (OggVorbis_File *) internal->decoder_private;
  217. rc = ov_read(vf, internal->buffer, internal->buffer_size,
  218. ((sample->actual.format & 0x1000) ? 1 : 0), /* bigendian? */
  219. ((sample->actual.format & 0xFF) / 8), /* bytes per sample point */
  220. ((sample->actual.format & 0x8000) ? 1 : 0), /* signed data? */
  221. &bitstream);
  222. /* Make sure the read went smoothly... */
  223. if (rc == 0)
  224. sample->flags |= SOUND_SAMPLEFLAG_EOF;
  225. else if (rc < 0)
  226. sample->flags |= SOUND_SAMPLEFLAG_ERROR;
  227. /* If the buffer isn't filled, keep trying to fill it
  228. * until no more data can be grabbed */
  229. else if ((Uint32) rc < internal->buffer_size)
  230. {
  231. /* Creating a pointer to the buffer that denotes where to start
  232. * writing new data. */
  233. Uint8* buffer_start_point = NULL;
  234. int total_bytes_read = rc;
  235. int bytes_remaining = internal->buffer_size - rc;
  236. /* Keep grabbing data until something prevents
  237. * us from getting more. (Could be EOF,
  238. * packets are too large to fit in remaining
  239. * space, or an error.)
  240. */
  241. while( (rc > 0) && (bytes_remaining > 0) )
  242. {
  243. /* Set buffer pointer to end of last write */
  244. /* All the messiness is to get rid of the warning for
  245. * dereferencing a void*
  246. */
  247. buffer_start_point = &(((Uint8*)internal->buffer)[total_bytes_read]);
  248. rc = ov_read(vf, buffer_start_point, bytes_remaining,
  249. ((sample->actual.format & 0x1000) ? 1 : 0), /* bigendian? */
  250. ((sample->actual.format & 0xFF) / 8), /* bytes per sample point */
  251. ((sample->actual.format & 0x8000) ? 1 : 0), /* signed data? */
  252. &bitstream);
  253. /* Make sure rc > 0 because we don't accidently want
  254. * to change the counters if there was an error
  255. */
  256. if(rc > 0)
  257. {
  258. total_bytes_read += rc;
  259. bytes_remaining = bytes_remaining - rc;
  260. }
  261. }
  262. /* I think the minimum read size is 2, though I'm
  263. * not sure about this. (I've hit cases where I
  264. * couldn't read less than 4.) What I don't want to do is
  265. * accidently claim we hit EOF when the reason rc == 0
  266. * is because the requested amount of data was smaller
  267. * than the minimum packet size.
  268. * For now, I will be conservative
  269. * and not set the EOF flag, and let the next call to
  270. * this function figure it out.
  271. * I think the ERROR flag is safe to set because
  272. * it looks like OGG simply returns 0 if the
  273. * read size is too small.
  274. * And in most cases for sensible buffer sizes,
  275. * this fix will fill the buffer,
  276. * so we can set the EAGAIN flag without worrying
  277. * that it will always be set.
  278. */
  279. if(rc < 0)
  280. {
  281. sample->flags |= SOUND_SAMPLEFLAG_ERROR;
  282. }
  283. else if(rc == 0)
  284. {
  285. /* Do nothing for now until there is a better solution */
  286. /* sample->flags |= SOUND_SAMPLEFLAG_EOF; */
  287. }
  288. /* Test for a buffer underrun. It should occur less frequently
  289. * now, but it still may happen and not necessarily mean
  290. * anything useful. */
  291. if ((Uint32) total_bytes_read < internal->buffer_size)
  292. {
  293. sample->flags |= SOUND_SAMPLEFLAG_EAGAIN;
  294. }
  295. /* change rc to the total bytes read so function
  296. * can return the correct value.
  297. */
  298. rc = total_bytes_read;
  299. }
  300. return((Uint32) rc);
  301. } /* OGG_read */
  302. static int OGG_rewind(Sound_Sample *sample)
  303. {
  304. Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
  305. OggVorbis_File *vf = (OggVorbis_File *) internal->decoder_private;
  306. BAIL_IF_MACRO(ov_raw_seek(vf, 0) < 0, ERR_IO_ERROR, 0);
  307. return(1);
  308. } /* OGG_rewind */
  309. static int OGG_seek(Sound_Sample *sample, Uint32 ms)
  310. {
  311. Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
  312. OggVorbis_File *vf = (OggVorbis_File *) internal->decoder_private;
  313. double timepos = (((double) ms) / 1000.0);
  314. BAIL_IF_MACRO(ov_time_seek(vf, timepos) < 0, ERR_IO_ERROR, 0);
  315. return(1);
  316. } /* OGG_seek */
  317. #endif /* SOUND_SUPPORTS_OGG */
  318. /* end of ogg.c ... */