Debian Codegraph @

code/client/snd_codec_opus.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4 Copyright (C) 2005 Stuart Dalton ([email protected])
5 Copyright (C) 2005-2006 Joerg Dietrich <[email protected]>
6
7 This file is part of Quake III Arena source code.
8
9 Quake III Arena source code is free software; you can redistribute it
10 and/or modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 Quake III Arena source code is distributed in the hope that it will be
15 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with Quake III Arena source code; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 ===========================================================================
23 */
24
25 // Ogg Opus support is enabled by this define
26 #ifdef USE_CODEC_OPUS
27
28 // includes for the Q3 sound system
29 #include "client.h"
30 #include "snd_codec.h"
31
32 // includes for the Ogg Opus codec
33 #include <errno.h>
34 #include <opusfile.h>
35
36 // samples are 16 bit
37 #define OPUS_SAMPLEWIDTH 2
38
39 // Q3 Ogg Opus codec
40 snd_codec_t opus_codec =
41 {
42 "opus",
43 S_OggOpus_CodecLoad,
44 S_OggOpus_CodecOpenStream,
45 S_OggOpus_CodecReadStream,
46 S_OggOpus_CodecCloseStream,
47 NULL
48 };
49
50 // callbacks for opusfile
51
52 // fread() replacement
53 int S_OggOpus_Callback_read(void *datasource, unsigned char *ptr, int size )
54 {
55 snd_stream_t *stream;
56 int bytesRead = 0;
57
58 // check if input is valid
59 if(!ptr)
60 {
61 errno = EFAULT;
62 return -1;
63 }
64
65 if(!size)
66 {
67 // It's not an error, caller just wants zero bytes!
68 errno = 0;
69 return 0;
70 }
71
72 if (size < 0)
73 {
74 errno = EINVAL;
75 return -1;
76 }
77
78 if(!datasource)
79 {
80 errno = EBADF;
81 return -1;
82 }
83
84 // we use a snd_stream_t in the generic pointer to pass around
85 stream = (snd_stream_t *) datasource;
86
87 // read it with the Q3 function FS_Read()
88 bytesRead = FS_Read(ptr, size, stream->file);
89
90 // update the file position
91 stream->pos += bytesRead;
92
93 return bytesRead;
94 }
95
96 // fseek() replacement
97 int S_OggOpus_Callback_seek(void *datasource, opus_int64 offset, int whence)
98 {
99 snd_stream_t *stream;
100 int retVal = 0;
101
102 // check if input is valid
103 if(!datasource)
104 {
105 errno = EBADF;
106 return -1;
107 }
108
109 // snd_stream_t in the generic pointer
110 stream = (snd_stream_t *) datasource;
111
112 // we must map the whence to its Q3 counterpart
113 switch(whence)
114 {
115 case SEEK_SET :
116 {
117 // set the file position in the actual file with the Q3 function
118 retVal = FS_Seek(stream->file, (long) offset, FS_SEEK_SET);
119
120 // something has gone wrong, so we return here
121 if(retVal < 0)
122 {
123 return retVal;
124 }
125
126 // keep track of file position
127 stream->pos = (int) offset;
128 break;
129 }
130
131 case SEEK_CUR :
132 {
133 // set the file position in the actual file with the Q3 function
134 retVal = FS_Seek(stream->file, (long) offset, FS_SEEK_CUR);
135
136 // something has gone wrong, so we return here
137 if(retVal < 0)
138 {
139 return retVal;
140 }
141
142 // keep track of file position
143 stream->pos += (int) offset;
144 break;
145 }
146
147 case SEEK_END :
148 {
149 // set the file position in the actual file with the Q3 function
150 retVal = FS_Seek(stream->file, (long) offset, FS_SEEK_END);
151
152 // something has gone wrong, so we return here
153 if(retVal < 0)
154 {
155 return retVal;
156 }
157
158 // keep track of file position
159 stream->pos = stream->length + (int) offset;
160 break;
161 }
162
163 default :
164 {
165 // unknown whence, so we return an error
166 errno = EINVAL;
167 return -1;
168 }
169 }
170
171 // stream->pos shouldn't be smaller than zero or bigger than the filesize
172 stream->pos = (stream->pos < 0) ? 0 : stream->pos;
173 stream->pos = (stream->pos > stream->length) ? stream->length : stream->pos;
174
175 return 0;
176 }
177
178 // fclose() replacement
179 int S_OggOpus_Callback_close(void *datasource)
180 {
181 // we do nothing here and close all things manually in S_OggOpus_CodecCloseStream()
182 return 0;
183 }
184
185 // ftell() replacement
186 opus_int64 S_OggOpus_Callback_tell(void *datasource)
187 {
188 snd_stream_t *stream;
189
190 // check if input is valid
191 if(!datasource)
192 {
193 errno = EBADF;
194 return -1;
195 }
196
197 // snd_stream_t in the generic pointer
198 stream = (snd_stream_t *) datasource;
199
200 return (opus_int64) FS_FTell(stream->file);
201 }
202
203 // the callback structure
204 const OpusFileCallbacks S_OggOpus_Callbacks =
205 {
206 &S_OggOpus_Callback_read,
207 &S_OggOpus_Callback_seek,
208 &S_OggOpus_Callback_tell,
209 &S_OggOpus_Callback_close
210 };
211
212 /*
213 =================
214 S_OggOpus_CodecOpenStream
215 =================
216 */
217 snd_stream_t *S_OggOpus_CodecOpenStream(const char *filename)
218 {
219 snd_stream_t *stream;
220
221 // Opus codec control structure
222 OggOpusFile *of;
223
224 // some variables used to get informations about the file
225 const OpusHead *opusInfo;
226 ogg_int64_t numSamples;
227
228 // check if input is valid
229 if(!filename)
230 {
231 return NULL;
232 }
233
234 // Open the stream
235 stream = S_CodecUtilOpen(filename, &opus_codec);
236 if(!stream)
237 {
238 return NULL;
239 }
240
241 // open the codec with our callbacks and stream as the generic pointer
242 of = op_open_callbacks(stream, &S_OggOpus_Callbacks, NULL, 0, NULL );
243 if (!of)
244 {
245 S_CodecUtilClose(&stream);
246
247 return NULL;
248 }
249
250 // the stream must be seekable
251 if(!op_seekable(of))
252 {
253 op_free(of);
254
255 S_CodecUtilClose(&stream);
256
257 return NULL;
258 }
259
260 // get the info about channels and rate
261 opusInfo = op_head(of, -1);
262 if(!opusInfo)
263 {
264 op_free(of);
265
266 S_CodecUtilClose(&stream);
267
268 return NULL;
269 }
270
271 if(opusInfo->stream_count != 1)
272 {
273 op_free(of);
274
275 S_CodecUtilClose(&stream);
276
277 Com_Printf("Only Ogg Opus files with one stream are support\n");
278 return NULL;
279 }
280
281 if(opusInfo->channel_count != 1 && opusInfo->channel_count != 2)
282 {
283 op_free(of);
284
285 S_CodecUtilClose(&stream);
286
287 Com_Printf("Only mono and stereo Ogg Opus files are supported\n");
288 return NULL;
289 }
290
291 // get the number of sample-frames in the file
292 numSamples = op_pcm_total(of, -1);
293
294 // fill in the info-structure in the stream
295 stream->info.rate = 48000;
296 stream->info.width = OPUS_SAMPLEWIDTH;
297 stream->info.channels = opusInfo->channel_count;
298 stream->info.samples = numSamples;
299 stream->info.size = stream->info.samples * stream->info.channels * stream->info.width;
300 stream->info.dataofs = 0;
301
302 // We use stream->pos for the file pointer in the compressed ogg file
303 stream->pos = 0;
304
305 // We use the generic pointer in stream for the opus codec control structure
306 stream->ptr = of;
307
308 return stream;
309 }
310
311 /*
312 =================
313 S_OggOpus_CodecCloseStream
314 =================
315 */
316 void S_OggOpus_CodecCloseStream(snd_stream_t *stream)
317 {
318 // check if input is valid
319 if(!stream)
320 {
321 return;
322 }
323
324 // let the opus codec cleanup its stuff
325 op_free((OggOpusFile *) stream->ptr);
326
327 // close the stream
328 S_CodecUtilClose(&stream);
329 }
330
331 /*
332 =================
333 S_OggOpus_CodecReadStream
334 =================
335 */
336 int S_OggOpus_CodecReadStream(snd_stream_t *stream, int bytes, void *buffer)
337 {
338 // buffer handling
339 int samplesRead, samplesLeft, c;
340 opus_int16 *bufPtr;
341
342 // check if input is valid
343 if(!(stream && buffer))
344 {
345 return 0;
346 }
347
348 if(bytes <= 0)
349 {
350 return 0;
351 }
352
353 samplesRead = 0;
354 samplesLeft = bytes / stream->info.channels / stream->info.width;
355 bufPtr = buffer;
356
357 if(samplesLeft <= 0)
358 {
359 return 0;
360 }
361
362 // cycle until we have the requested or all available bytes read
363 while(-1)
364 {
365 // read some samples from the opus codec
366 c = op_read((OggOpusFile *) stream->ptr, bufPtr + samplesRead * stream->info.channels, samplesLeft * stream->info.channels, NULL);
367
368 // no more samples are left
369 if(c <= 0)
370 {
371 break;
372 }
373
374 samplesRead += c;
375 samplesLeft -= c;
376
377 // we have enough samples
378 if(samplesLeft <= 0)
379 {
380 break;
381 }
382 }
383
384 return samplesRead * stream->info.channels * stream->info.width;
385 }
386
387 /*
388 =====================================================================
389 S_OggOpus_CodecLoad
390
391 We handle S_OggOpus_CodecLoad as a special case of the streaming functions
392 where we read the whole stream at once.
393 ======================================================================
394 */
395 void *S_OggOpus_CodecLoad(const char *filename, snd_info_t *info)
396 {
397 snd_stream_t *stream;
398 byte *buffer;
399 int bytesRead;
400
401 // check if input is valid
402 if(!(filename && info))
403 {
404 return NULL;
405 }
406
407 // open the file as a stream
408 stream = S_OggOpus_CodecOpenStream(filename);
409 if(!stream)
410 {
411 return NULL;
412 }
413
414 // copy over the info
415 info->rate = stream->info.rate;
416 info->width = stream->info.width;
417 info->channels = stream->info.channels;
418 info->samples = stream->info.samples;
419 info->size = stream->info.size;
420 info->dataofs = stream->info.dataofs;
421
422 // allocate a buffer
423 // this buffer must be free-ed by the caller of this function
424 buffer = Hunk_AllocateTempMemory(info->size);
425 if(!buffer)
426 {
427 S_OggOpus_CodecCloseStream(stream);
428
429 return NULL;
430 }
431
432 // fill the buffer
433 bytesRead = S_OggOpus_CodecReadStream(stream, info->size, buffer);
434
435 // we don't even have read a single byte
436 if(bytesRead <= 0)
437 {
438 Hunk_FreeTempMemory(buffer);
439 S_OggOpus_CodecCloseStream(stream);
440
441 return NULL;
442 }
443
444 S_OggOpus_CodecCloseStream(stream);
445
446 return buffer;
447 }
448
449 #endif // USE_CODEC_OPUS