PageRenderTime 37ms CodeModel.GetById 11ms app.highlight 20ms RepoModel.GetById 2ms app.codeStats 0ms

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

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