PageRenderTime 27ms CodeModel.GetById 14ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 1ms

/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/*
 21 * RAW decoder for SDL_sound. This is as simple as it gets.
 22 *
 23 * This driver handles raw audio data. You must, regardless of where the
 24 *  data is actually coming from, specify the string "RAW" in the extension
 25 *  parameter of Sound_NewSample() (or, alternately, open a file with the
 26 *  extension ".raw" in Sound_NewSampleFromFile()). The string is checked
 27 *  case-insensitive. We need this check, because raw data, being raw, has
 28 *  no headers or magic number we can use to determine if we should handle a
 29 *  given file, so we needed some way to have this "decoder" discriminate.
 30 *
 31 * When calling Sound_NewSample*(), you must also specify a "desired"
 32 *  audio format. The "actual" format will always match what you specify, so
 33 *  there will be no conversion overhead, but these routines need to know how
 34 *  to treat the bits, since it's all random garbage otherwise.
 35 *
 36 * Please see the file COPYING in the source's root directory.
 37 *
 38 *  This file written by Ryan C. Gordon. (icculus@icculus.org)
 39 */
 40
 41#if HAVE_CONFIG_H
 42#  include <config.h>
 43#endif
 44
 45#ifdef SOUND_SUPPORTS_RAW
 46
 47#include <stdio.h>
 48#include <stdlib.h>
 49#include <string.h>
 50
 51#include "SDL_sound.h"
 52
 53#define __SDL_SOUND_INTERNAL__
 54#include "SDL_sound_internal.h"
 55
 56static int RAW_init(void);
 57static void RAW_quit(void);
 58static int RAW_open(Sound_Sample *sample, const char *ext);
 59static void RAW_close(Sound_Sample *sample);
 60static Uint32 RAW_read(Sound_Sample *sample);
 61static int RAW_rewind(Sound_Sample *sample);
 62static int RAW_seek(Sound_Sample *sample, Uint32 ms);
 63
 64static const char *extensions_raw[] = { "RAW", NULL };
 65const Sound_DecoderFunctions __Sound_DecoderFunctions_RAW =
 66{
 67    {
 68        extensions_raw,
 69        "Raw audio",
 70        "Ryan C. Gordon <icculus@icculus.org>",
 71        "http://www.icculus.org/SDL_sound/"
 72    },
 73
 74    RAW_init,       /*   init() method */
 75    RAW_quit,       /*   quit() method */
 76    RAW_open,       /*   open() method */
 77    RAW_close,      /*  close() method */
 78    RAW_read,       /*   read() method */
 79    RAW_rewind,     /* rewind() method */
 80    RAW_seek        /*   seek() method */
 81};
 82
 83
 84static int RAW_init(void)
 85{
 86    return(1);  /* always succeeds. */
 87} /* RAW_init */
 88
 89
 90static void RAW_quit(void)
 91{
 92    /* it's a no-op. */
 93} /* RAW_quit */
 94
 95
 96static int RAW_open(Sound_Sample *sample, const char *ext)
 97{
 98        /*
 99         * We check this explicitly, since we have no other way to
100         *  determine whether we should handle this data or not.
101         */
102    if (__Sound_strcasecmp(ext, "RAW") != 0)
103        BAIL_MACRO("RAW: extension isn't explicitly \"RAW\".", 0);
104
105        /*
106         * You must also specify a desired format, so we know how to
107         *  treat the bits that are otherwise binary garbage.
108         */
109    if ( (sample->desired.channels < 1)  ||
110         (sample->desired.channels > 2)  ||
111         (sample->desired.rate == 0)     ||
112         (sample->desired.format == 0) )
113    {
114        BAIL_MACRO("RAW: invalid desired format.", 0);
115    } /* if */
116
117    SNDDBG(("RAW: Accepting data stream.\n"));
118
119        /*
120         * We never convert raw samples; what you ask for is what you get.
121         */
122    memcpy(&sample->actual, &sample->desired, sizeof (Sound_AudioInfo));
123    sample->flags = SOUND_SAMPLEFLAG_CANSEEK;
124
125    return(1); /* we'll handle this data. */
126} /* RAW_open */
127
128
129static void RAW_close(Sound_Sample *sample)
130{
131    /* we don't allocate anything that we need to free. That's easy, eh? */
132} /* RAW_close */
133
134
135static Uint32 RAW_read(Sound_Sample *sample)
136{
137    Uint32 retval;
138    Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
139
140        /*
141         * We don't actually do any decoding, so we read the raw data
142         *  directly into the internal buffer...
143         */
144    retval = SDL_RWread(internal->rw, internal->buffer,
145                        1, internal->buffer_size);
146
147        /* Make sure the read went smoothly... */
148    if (retval == 0)
149        sample->flags |= SOUND_SAMPLEFLAG_EOF;
150
151    else if (retval == -1)
152        sample->flags |= SOUND_SAMPLEFLAG_ERROR;
153
154        /* (next call this EAGAIN may turn into an EOF or error.) */
155    else if (retval < internal->buffer_size)
156        sample->flags |= SOUND_SAMPLEFLAG_EAGAIN;
157
158    return(retval);
159} /* RAW_read */
160
161
162static int RAW_rewind(Sound_Sample *sample)
163{
164    Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
165    BAIL_IF_MACRO(SDL_RWseek(internal->rw, 0, SEEK_SET) != 0, ERR_IO_ERROR, 0);
166    return(1);
167} /* RAW_rewind */
168
169
170static int RAW_seek(Sound_Sample *sample, Uint32 ms)
171{
172    Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
173    int pos = (int) __Sound_convertMsToBytePos(&sample->actual, ms);
174    int err = (SDL_RWseek(internal->rw, pos, SEEK_SET) != pos);
175    BAIL_IF_MACRO(err, ERR_IO_ERROR, 0);
176    return(1);
177} /* RAW_seek */
178
179
180#endif /* SOUND_SUPPORTS_RAW */
181
182
183/* end of raw.c ... */
184