File: simage_oggvorbis_reader.c

package info (click to toggle)
simage 1.7.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,908 kB
  • sloc: ansic: 14,231; sh: 12,587; cpp: 930; perl: 854; makefile: 617; lisp: 25
file content (256 lines) | stat: -rw-r--r-- 6,058 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include <config.h>
#ifdef SIMAGE_OGGVORBIS_SUPPORT

#include <simage.h>
#include <simage_private.h>
#include <simage_oggvorbis.h>

#include <stdio.h>
#include <stdlib.h>

#include <vorbis/codec.h>
#include <vorbis/vorbisfile.h>

typedef struct {
  FILE *file;
  OggVorbis_File vorbisfile;
  int current_section;
} oggvorbis_reader_context;


static void 
oggvorbis_reader_init_context(oggvorbis_reader_context *context)
{
  context->file = NULL;
  context->current_section = 0;
}

static void 
oggvorbis_reader_cleanup_context(oggvorbis_reader_context *context)
{
}

static size_t 
oggvorbis_reader_read_cb(void *ptr, size_t size, size_t nmemb, 
                                void *datasource)
{
  oggvorbis_reader_context *context = (oggvorbis_reader_context *)datasource;
  return fread(ptr, size, nmemb, context->file);
}

static int 
oggvorbis_reader_seek_cb(void *datasource, ogg_int64_t offset, int whence)
{
  oggvorbis_reader_context *context = (oggvorbis_reader_context *)datasource;
  return fseek(context->file, (int)offset, whence);
}

static long
oggvorbis_reader_tell_cb(void *datasource)
{
  oggvorbis_reader_context *context = (oggvorbis_reader_context *)datasource;
  return ftell(context->file);
}

static int 
oggvorbis_reader_close_cb(void *datasource)
{
  oggvorbis_reader_context *context = (oggvorbis_reader_context *)datasource;
  if (context->file != NULL)
    fclose(context->file);
  context->file = NULL;
  return 0;
}

static int 
oggvorbis_reader_open(oggvorbis_reader_context **contextp, 
                      const char *filename)
{
  ov_callbacks callbacks;
  oggvorbis_reader_context *context;  

  *contextp = (oggvorbis_reader_context *) 
    malloc(sizeof(oggvorbis_reader_context));
  oggvorbis_reader_init_context(*contextp);
  context = *contextp;  
  
  context->file = fopen(filename, "rb");
  if (context->file == NULL) {
    oggvorbis_reader_cleanup_context(context);
    free(context);
    return 0;
  }

  callbacks.read_func = oggvorbis_reader_read_cb;
  callbacks.seek_func = oggvorbis_reader_seek_cb;
  callbacks.close_func = oggvorbis_reader_close_cb;
  callbacks.tell_func = oggvorbis_reader_tell_cb;

  if(ov_open_callbacks((void *)context, &context->vorbisfile, NULL, 0, 
                       callbacks) < 0) {
    fclose(context->file);
    context->file = NULL;
    oggvorbis_reader_cleanup_context(context);
    free(context);
    return 0;
  }

  return 1;
}

static int 
oggvorbis_reader_read(oggvorbis_reader_context *context, 
                      char *buffer, int size)
{
  int readsize;
  int numread;
  int bigEndian;

  union {
    int testWord;
    char testByte[4];
  } endianTest;

  endianTest.testWord = 1;
  if (endianTest.testByte[0] == 1) {
    bigEndian = 0; // little endian
  } else {
    bigEndian = 1; // big endian
  }


  readsize = 0;
  numread = 0;

  while (readsize<size) {
    /* FIXME: Check how libvorbisfile handles odd sizes.  The
       libvorbisfile documentation doesn't say anything about this, so
       everything might be OK.  
       2002-01-10 thammer.  
    */
    numread=ov_read(&context->vorbisfile, 
                    buffer+readsize, 
                    size-readsize, bigEndian, 2, 1, 
                    &context->current_section);
    /* FIXME: Verify that EOF's (and numread=0) are
       handled correctly. 2003-01-09 thammer
     */
    if (numread<=0)
      return numread;
    else
      readsize += numread;
  }

  return readsize;
}

static void 
oggvorbis_reader_get_stream_info(oggvorbis_reader_context *context, 
                                 int *channels, int *samplerate)
{
  if (context->file) {
    vorbis_info * vi;
    vi = ov_info(&context->vorbisfile,-1);
    *channels = vi->channels;
    *samplerate = vi->rate;
  }
}


static void 
oggvorbis_reader_close(oggvorbis_reader_context *context)
{
  if (context->file != NULL)
    ov_clear(&context->vorbisfile);

  oggvorbis_reader_cleanup_context(context);
  free(context);
}


int 
oggvorbis_reader_stream_open(const char * filename, s_stream * stream,
                             s_params * params)
{
  oggvorbis_reader_context *context;
  int channels, samplerate;
  
  if (!oggvorbis_reader_open(&context, filename)) 
    return 0;

  s_stream_context_set(stream, (void *)context);

  oggvorbis_reader_get_stream_info(context, &channels, &samplerate);
  s_params_set(s_stream_params(stream),
               "channels",   S_INTEGER_PARAM_TYPE, channels,
               "samplerate", S_INTEGER_PARAM_TYPE, samplerate,
               NULL);
  return 1;
}

void * 
oggvorbis_reader_stream_get(s_stream * stream, void * buffer, int * size, s_params * params)
{
  int ret;
  oggvorbis_reader_context *context;

  context = (oggvorbis_reader_context *)s_stream_context_get(stream);

  if (context != NULL) {
    ret = oggvorbis_reader_read(context, (char*) buffer, *size);
    if (ret>0) {
      *size = ret;
      return buffer;
    }
    /* fixme 20020904 thammer: check params for conversion requests
     */
  }
  *size=0;
  return NULL;
}

void 
oggvorbis_reader_stream_close(s_stream * stream)
{
  oggvorbis_reader_context *context;

  context = (oggvorbis_reader_context *)s_stream_context_get(stream);
  if (context != NULL) {
    oggvorbis_reader_close(context);
  }
}

int
oggvorbis_reader_stream_seek(s_stream * stream, int offset, int whence,
                             s_params * params)
{
  oggvorbis_reader_context *context;

  context = (oggvorbis_reader_context *)s_stream_context_get(stream);

  if (context != NULL) {
    if (whence == SIMAGE_SEEK_SET)
      return ov_pcm_seek(&context->vorbisfile, offset);
    else
      return -1;
    /* FIXME: Support the whence parameter. 2003-03-10 thammer */
  }
  else
    return -1;
}

int
oggvorbis_reader_stream_tell(s_stream * stream, s_params * params)
{
  oggvorbis_reader_context *context;

  context = (oggvorbis_reader_context *)s_stream_context_get(stream);

  if (context != NULL) {
    return ov_pcm_tell(&context->vorbisfile);
  }
  else
    return -1;
}

#endif /* SIMAGE_OGGVORBIS_SUPPORT */