| 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 |
#ifdef USE_CODEC_OPUS |
| 27 |
|
| 28 |
|
| 29 |
#include "client.h" |
| 30 |
#include "snd_codec.h" |
| 31 |
|
| 32 |
|
| 33 |
#include <errno.h> |
| 34 |
#include <opusfile.h> |
| 35 |
|
| 36 |
|
| 37 |
#define OPUS_SAMPLEWIDTH 2 |
| 38 |
|
| 39 |
|
| 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 |
|
| 51 |
|
| 52 |
|
| 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 |
|
| 59 |
if(!ptr) |
| 60 |
{ |
| 61 |
errno = EFAULT; |
| 62 |
return -1; |
| 63 |
} |
| 64 |
|
| 65 |
if(!size) |
| 66 |
{ |
| 67 |
|
| 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 |
|
| 85 |
stream = (snd_stream_t *) datasource; |
| 86 |
|
| 87 |
|
| 88 |
bytesRead = FS_Read(ptr, size, stream->file); |
| 89 |
|
| 90 |
|
| 91 |
stream->pos += bytesRead; |
| 92 |
|
| 93 |
return bytesRead; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 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 |
|
| 103 |
if(!datasource) |
| 104 |
{ |
| 105 |
errno = EBADF; |
| 106 |
return -1; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
stream = (snd_stream_t *) datasource; |
| 111 |
|
| 112 |
|
| 113 |
switch(whence) |
| 114 |
{ |
| 115 |
case SEEK_SET : |
| 116 |
{ |
| 117 |
|
| 118 |
retVal = FS_Seek(stream->file, (long) offset, FS_SEEK_SET); |
| 119 |
|
| 120 |
|
| 121 |
if(retVal < 0) |
| 122 |
{ |
| 123 |
return retVal; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
stream->pos = (int) offset; |
| 128 |
break; |
| 129 |
} |
| 130 |
|
| 131 |
case SEEK_CUR : |
| 132 |
{ |
| 133 |
|
| 134 |
retVal = FS_Seek(stream->file, (long) offset, FS_SEEK_CUR); |
| 135 |
|
| 136 |
|
| 137 |
if(retVal < 0) |
| 138 |
{ |
| 139 |
return retVal; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
stream->pos += (int) offset; |
| 144 |
break; |
| 145 |
} |
| 146 |
|
| 147 |
case SEEK_END : |
| 148 |
{ |
| 149 |
|
| 150 |
retVal = FS_Seek(stream->file, (long) offset, FS_SEEK_END); |
| 151 |
|
| 152 |
|
| 153 |
if(retVal < 0) |
| 154 |
{ |
| 155 |
return retVal; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
stream->pos = stream->length + (int) offset; |
| 160 |
break; |
| 161 |
} |
| 162 |
|
| 163 |
default : |
| 164 |
{ |
| 165 |
|
| 166 |
errno = EINVAL; |
| 167 |
return -1; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
|
| 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 |
|
| 179 |
int S_OggOpus_Callback_close(void *datasource) |
| 180 |
{ |
| 181 |
|
| 182 |
return 0; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
opus_int64 S_OggOpus_Callback_tell(void *datasource) |
| 187 |
{ |
| 188 |
snd_stream_t *stream; |
| 189 |
|
| 190 |
|
| 191 |
if(!datasource) |
| 192 |
{ |
| 193 |
errno = EBADF; |
| 194 |
return -1; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
stream = (snd_stream_t *) datasource; |
| 199 |
|
| 200 |
return (opus_int64) FS_FTell(stream->file); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 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 |
|
| 215 |
|
| 216 |
|
| 217 |
snd_stream_t *S_OggOpus_CodecOpenStream(const char *filename) |
| 218 |
{ |
| 219 |
snd_stream_t *stream; |
| 220 |
|
| 221 |
|
| 222 |
OggOpusFile *of; |
| 223 |
|
| 224 |
|
| 225 |
const OpusHead *opusInfo; |
| 226 |
ogg_int64_t numSamples; |
| 227 |
|
| 228 |
|
| 229 |
if(!filename) |
| 230 |
{ |
| 231 |
return NULL; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
stream = S_CodecUtilOpen(filename, &opus_codec); |
| 236 |
if(!stream) |
| 237 |
{ |
| 238 |
return NULL; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 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 |
|
| 251 |
if(!op_seekable(of)) |
| 252 |
{ |
| 253 |
op_free(of); |
| 254 |
|
| 255 |
S_CodecUtilClose(&stream); |
| 256 |
|
| 257 |
return NULL; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 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 |
|
| 292 |
numSamples = op_pcm_total(of, -1); |
| 293 |
|
| 294 |
|
| 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 |
|
| 303 |
stream->pos = 0; |
| 304 |
|
| 305 |
|
| 306 |
stream->ptr = of; |
| 307 |
|
| 308 |
return stream; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
void S_OggOpus_CodecCloseStream(snd_stream_t *stream) |
| 317 |
{ |
| 318 |
|
| 319 |
if(!stream) |
| 320 |
{ |
| 321 |
return; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
op_free((OggOpusFile *) stream->ptr); |
| 326 |
|
| 327 |
|
| 328 |
S_CodecUtilClose(&stream); |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
int S_OggOpus_CodecReadStream(snd_stream_t *stream, int bytes, void *buffer) |
| 337 |
{ |
| 338 |
|
| 339 |
int samplesRead, samplesLeft, c; |
| 340 |
opus_int16 *bufPtr; |
| 341 |
|
| 342 |
|
| 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 |
|
| 363 |
while(-1) |
| 364 |
{ |
| 365 |
|
| 366 |
c = op_read((OggOpusFile *) stream->ptr, bufPtr + samplesRead * stream->info.channels, samplesLeft * stream->info.channels, NULL); |
| 367 |
|
| 368 |
|
| 369 |
if(c <= 0) |
| 370 |
{ |
| 371 |
break; |
| 372 |
} |
| 373 |
|
| 374 |
samplesRead += c; |
| 375 |
samplesLeft -= c; |
| 376 |
|
| 377 |
|
| 378 |
if(samplesLeft <= 0) |
| 379 |
{ |
| 380 |
break; |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
return samplesRead * stream->info.channels * stream->info.width; |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 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 |
|
| 402 |
if(!(filename && info)) |
| 403 |
{ |
| 404 |
return NULL; |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
stream = S_OggOpus_CodecOpenStream(filename); |
| 409 |
if(!stream) |
| 410 |
{ |
| 411 |
return NULL; |
| 412 |
} |
| 413 |
|
| 414 |
|
| 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 |
|
| 423 |
|
| 424 |
buffer = Hunk_AllocateTempMemory(info->size); |
| 425 |
if(!buffer) |
| 426 |
{ |
| 427 |
S_OggOpus_CodecCloseStream(stream); |
| 428 |
|
| 429 |
return NULL; |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
bytesRead = S_OggOpus_CodecReadStream(stream, info->size, buffer); |
| 434 |
|
| 435 |
|
| 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 |