Debian Codegraph @

extras/decoders/libvorbis/miniaudio_libvorbis.c

raw symbols SCIP
1 #ifndef miniaudio_libvorbis_c
2 #define miniaudio_libvorbis_c
3
4 #include "miniaudio_libvorbis.h"
5
6 #if !defined(MA_NO_LIBVORBIS)
7 #ifndef OV_EXCLUDE_STATIC_CALLBACKS
8 #define OV_EXCLUDE_STATIC_CALLBACKS
9 #endif
10 #include <vorbis/vorbisfile.h>
11 #endif
12
13 #include <string.h> /* For memset(). */
14 #include <assert.h>
15
16 static ma_result ma_libvorbis_ds_read(ma_data_source* pDataSource, void* pFramesOut, ma_uint64 frameCount, ma_uint64* pFramesRead)
17 {
18 return ma_libvorbis_read_pcm_frames((ma_libvorbis*)pDataSource, pFramesOut, frameCount, pFramesRead);
19 }
20
21 static ma_result ma_libvorbis_ds_seek(ma_data_source* pDataSource, ma_uint64 frameIndex)
22 {
23 return ma_libvorbis_seek_to_pcm_frame((ma_libvorbis*)pDataSource, frameIndex);
24 }
25
26 static ma_result ma_libvorbis_ds_get_data_format(ma_data_source* pDataSource, ma_format* pFormat, ma_uint32* pChannels, ma_uint32* pSampleRate, ma_channel* pChannelMap, size_t channelMapCap)
27 {
28 return ma_libvorbis_get_data_format((ma_libvorbis*)pDataSource, pFormat, pChannels, pSampleRate, pChannelMap, channelMapCap);
29 }
30
31 static ma_result ma_libvorbis_ds_get_cursor(ma_data_source* pDataSource, ma_uint64* pCursor)
32 {
33 return ma_libvorbis_get_cursor_in_pcm_frames((ma_libvorbis*)pDataSource, pCursor);
34 }
35
36 static ma_result ma_libvorbis_ds_get_length(ma_data_source* pDataSource, ma_uint64* pLength)
37 {
38 return ma_libvorbis_get_length_in_pcm_frames((ma_libvorbis*)pDataSource, pLength);
39 }
40
41 static ma_data_source_vtable g_ma_libvorbis_ds_vtable =
42 {
43 ma_libvorbis_ds_read,
44 ma_libvorbis_ds_seek,
45 ma_libvorbis_ds_get_data_format,
46 ma_libvorbis_ds_get_cursor,
47 ma_libvorbis_ds_get_length,
48 NULL, /* onSetLooping */
49 0 /* flags */
50 };
51
52
53 #if !defined(MA_NO_LIBVORBIS)
54 static size_t ma_libvorbis_vf_callback__read(void* pBufferOut, size_t size, size_t count, void* pUserData)
55 {
56 ma_libvorbis* pVorbis = (ma_libvorbis*)pUserData;
57 ma_result result;
58 size_t bytesToRead;
59 size_t bytesRead;
60
61 /* For consistency with fread(). If `size` of `count` is 0, return 0 immediately without changing anything. */
62 if (size == 0 || count == 0) {
63 return 0;
64 }
65
66 bytesToRead = size * count;
67 result = pVorbis->onRead(pVorbis->pReadSeekTellUserData, pBufferOut, bytesToRead, &bytesRead);
68 if (result != MA_SUCCESS) {
69 /* Not entirely sure what to return here. What if an error occurs, but some data was read and bytesRead is > 0? */
70 return 0;
71 }
72
73 return bytesRead / size;
74 }
75
76 static int ma_libvorbis_vf_callback__seek(void* pUserData, ogg_int64_t offset, int whence)
77 {
78 ma_libvorbis* pVorbis = (ma_libvorbis*)pUserData;
79 ma_result result;
80 ma_seek_origin origin;
81
82 if (whence == SEEK_SET) {
83 origin = ma_seek_origin_start;
84 } else if (whence == SEEK_END) {
85 origin = ma_seek_origin_end;
86 } else {
87 origin = ma_seek_origin_current;
88 }
89
90 result = pVorbis->onSeek(pVorbis->pReadSeekTellUserData, offset, origin);
91 if (result != MA_SUCCESS) {
92 return -1;
93 }
94
95 return 0;
96 }
97
98 static long ma_libvorbis_vf_callback__tell(void* pUserData)
99 {
100 ma_libvorbis* pVorbis = (ma_libvorbis*)pUserData;
101 ma_result result;
102 ma_int64 cursor;
103
104 result = pVorbis->onTell(pVorbis->pReadSeekTellUserData, &cursor);
105 if (result != MA_SUCCESS) {
106 return -1;
107 }
108
109 return (long)cursor;
110 }
111 #endif
112
113 static ma_result ma_libvorbis_init_internal(const ma_decoding_backend_config* pConfig, const ma_allocation_callbacks* pAllocationCallbacks, ma_libvorbis* pVorbis)
114 {
115 if (pVorbis == NULL) {
116 return MA_INVALID_ARGS;
117 }
118
119 memset(pVorbis, 0, sizeof(*pVorbis));
120 pVorbis->format = ma_format_f32; /* f32 by default. */
121
122 if (pConfig != NULL && (pConfig->preferredFormat == ma_format_f32 || pConfig->preferredFormat == ma_format_s16)) {
123 pVorbis->format = pConfig->preferredFormat;
124 } else {
125 /* Getting here means something other than f32 and s16 was specified. Just leave this unset to use the default format. */
126 }
127
128 #if !defined(MA_NO_LIBVORBIS)
129 {
130 ma_result result;
131 ma_data_source_config dataSourceConfig;
132
133 dataSourceConfig = ma_data_source_config_init();
134 dataSourceConfig.vtable = &g_ma_libvorbis_ds_vtable;
135
136 result = ma_data_source_init(&dataSourceConfig, &pVorbis->ds);
137 if (result != MA_SUCCESS) {
138 return result; /* Failed to initialize the base data source. */
139 }
140
141 pVorbis->vf = (OggVorbis_File*)ma_malloc(sizeof(OggVorbis_File), pAllocationCallbacks);
142 if (pVorbis->vf == NULL) {
143 ma_data_source_uninit(&pVorbis->ds);
144 return MA_OUT_OF_MEMORY;
145 }
146
147 return MA_SUCCESS;
148 }
149 #else
150 {
151 /* libvorbis is disabled. */
152 (void)pAllocationCallbacks;
153 return MA_NOT_IMPLEMENTED;
154 }
155 #endif
156 }
157
158 MA_API ma_result ma_libvorbis_init(ma_read_proc onRead, ma_seek_proc onSeek, ma_tell_proc onTell, void* pReadSeekTellUserData, const ma_decoding_backend_config* pConfig, const ma_allocation_callbacks* pAllocationCallbacks, ma_libvorbis* pVorbis)
159 {
160 ma_result result;
161
162 (void)pAllocationCallbacks; /* Can't seem to find a way to configure memory allocations in libvorbis. */
163
164 if (onRead == NULL || onSeek == NULL) {
165 return MA_INVALID_ARGS; /* onRead and onSeek are mandatory. */
166 }
167
168 result = ma_libvorbis_init_internal(pConfig, pAllocationCallbacks, pVorbis);
169 if (result != MA_SUCCESS) {
170 return result;
171 }
172
173 pVorbis->onRead = onRead;
174 pVorbis->onSeek = onSeek;
175 pVorbis->onTell = onTell;
176 pVorbis->pReadSeekTellUserData = pReadSeekTellUserData;
177
178 #if !defined(MA_NO_LIBVORBIS)
179 {
180 int libvorbisResult;
181 ov_callbacks libvorbisCallbacks;
182
183 /* We can now initialize the vorbis decoder. This must be done after we've set up the callbacks. */
184 libvorbisCallbacks.read_func = ma_libvorbis_vf_callback__read;
185 libvorbisCallbacks.seek_func = ma_libvorbis_vf_callback__seek;
186 libvorbisCallbacks.close_func = NULL;
187 libvorbisCallbacks.tell_func = ma_libvorbis_vf_callback__tell;
188
189 libvorbisResult = ov_open_callbacks(pVorbis, (OggVorbis_File*)pVorbis->vf, NULL, 0, libvorbisCallbacks);
190 if (libvorbisResult < 0) {
191 ma_data_source_uninit(&pVorbis->ds);
192 ma_free(pVorbis->vf, pAllocationCallbacks);
193 return MA_INVALID_FILE;
194 }
195
196 return MA_SUCCESS;
197 }
198 #else
199 {
200 /* libvorbis is disabled. */
201 return MA_NOT_IMPLEMENTED;
202 }
203 #endif
204 }
205
206 MA_API ma_result ma_libvorbis_init_file(const char* pFilePath, const ma_decoding_backend_config* pConfig, const ma_allocation_callbacks* pAllocationCallbacks, ma_libvorbis* pVorbis)
207 {
208 ma_result result;
209
210 (void)pAllocationCallbacks; /* Can't seem to find a way to configure memory allocations in libvorbis. */
211
212 result = ma_libvorbis_init_internal(pConfig, pAllocationCallbacks, pVorbis);
213 if (result != MA_SUCCESS) {
214 return result;
215 }
216
217 #if !defined(MA_NO_LIBVORBIS)
218 {
219 int libvorbisResult;
220
221 libvorbisResult = ov_fopen(pFilePath, (OggVorbis_File*)pVorbis->vf);
222 if (libvorbisResult < 0) {
223 ma_data_source_uninit(&pVorbis->ds);
224 ma_free(pVorbis->vf, pAllocationCallbacks);
225 return MA_INVALID_FILE;
226 }
227
228 return MA_SUCCESS;
229 }
230 #else
231 {
232 /* libvorbis is disabled. */
233 (void)pFilePath;
234 return MA_NOT_IMPLEMENTED;
235 }
236 #endif
237 }
238
239 MA_API void ma_libvorbis_uninit(ma_libvorbis* pVorbis, const ma_allocation_callbacks* pAllocationCallbacks)
240 {
241 if (pVorbis == NULL) {
242 return;
243 }
244
245 (void)pAllocationCallbacks;
246
247 #if !defined(MA_NO_LIBVORBIS)
248 {
249 ov_clear((OggVorbis_File*)pVorbis->vf);
250 }
251 #else
252 {
253 /* libvorbis is disabled. Should never hit this since initialization would have failed. */
254 assert(MA_FALSE);
255 }
256 #endif
257
258 ma_data_source_uninit(&pVorbis->ds);
259 ma_free(pVorbis->vf, pAllocationCallbacks);
260 }
261
262 MA_API ma_result ma_libvorbis_read_pcm_frames(ma_libvorbis* pVorbis, void* pFramesOut, ma_uint64 frameCount, ma_uint64* pFramesRead)
263 {
264 if (pFramesRead != NULL) {
265 *pFramesRead = 0;
266 }
267
268 if (frameCount == 0) {
269 return MA_INVALID_ARGS;
270 }
271
272 if (pVorbis == NULL) {
273 return MA_INVALID_ARGS;
274 }
275
276 #if !defined(MA_NO_LIBVORBIS)
277 {
278 /* We always use floating point format. */
279 ma_result result = MA_SUCCESS; /* Must be initialized to MA_SUCCESS. */
280 ma_uint64 totalFramesRead;
281 ma_format format;
282 ma_uint32 channels;
283
284 ma_libvorbis_get_data_format(pVorbis, &format, &channels, NULL, NULL, 0);
285
286 totalFramesRead = 0;
287 while (totalFramesRead < frameCount) {
288 long libvorbisResult;
289 ma_uint64 framesToRead;
290 ma_uint64 framesRemaining;
291
292 framesRemaining = (frameCount - totalFramesRead);
293 framesToRead = 1024;
294 if (framesToRead > framesRemaining) {
295 framesToRead = framesRemaining;
296 }
297
298 if (format == ma_format_f32) {
299 float** ppFramesF32;
300
301 libvorbisResult = ov_read_float((OggVorbis_File*)pVorbis->vf, &ppFramesF32, (int)framesToRead, NULL);
302 if (libvorbisResult < 0) {
303 result = MA_ERROR; /* Error while decoding. */
304 break;
305 } else {
306 /* Frames need to be interleaved. */
307 ma_interleave_pcm_frames(format, channels, libvorbisResult, (const void**)ppFramesF32, ma_offset_pcm_frames_ptr(pFramesOut, totalFramesRead, format, channels));
308 totalFramesRead += libvorbisResult;
309
310 if (libvorbisResult == 0) {
311 result = MA_AT_END;
312 break;
313 }
314 }
315 } else {
316 libvorbisResult = ov_read((OggVorbis_File*)pVorbis->vf, (char*)ma_offset_pcm_frames_ptr(pFramesOut, totalFramesRead, format, channels), (int)(framesToRead * ma_get_bytes_per_frame(format, channels)), 0, 2, 1, NULL);
317 if (libvorbisResult < 0) {
318 result = MA_ERROR; /* Error while decoding. */
319 break;
320 } else {
321 /* Conveniently, there's no need to interleaving when using ov_read(). I'm not sure why ov_read_float() is different in that regard... */
322 totalFramesRead += libvorbisResult / ma_get_bytes_per_frame(format, channels);
323
324 if (libvorbisResult == 0) {
325 result = MA_AT_END;
326 break;
327 }
328 }
329 }
330 }
331
332 if (pFramesRead != NULL) {
333 *pFramesRead = totalFramesRead;
334 }
335
336 if (result == MA_SUCCESS && totalFramesRead == 0) {
337 result = MA_AT_END;
338 }
339
340 return result;
341 }
342 #else
343 {
344 /* libvorbis is disabled. Should never hit this since initialization would have failed. */
345 assert(MA_FALSE);
346
347 (void)pFramesOut;
348 (void)frameCount;
349 (void)pFramesRead;
350
351 return MA_NOT_IMPLEMENTED;
352 }
353 #endif
354 }
355
356 MA_API ma_result ma_libvorbis_seek_to_pcm_frame(ma_libvorbis* pVorbis, ma_uint64 frameIndex)
357 {
358 if (pVorbis == NULL) {
359 return MA_INVALID_ARGS;
360 }
361
362 #if !defined(MA_NO_LIBVORBIS)
363 {
364 int libvorbisResult = ov_pcm_seek((OggVorbis_File*)pVorbis->vf, (ogg_int64_t)frameIndex);
365 if (libvorbisResult != 0) {
366 if (libvorbisResult == OV_ENOSEEK) {
367 return MA_INVALID_OPERATION; /* Not seekable. */
368 } else if (libvorbisResult == OV_EINVAL) {
369 return MA_INVALID_ARGS;
370 } else {
371 return MA_ERROR;
372 }
373 }
374
375 return MA_SUCCESS;
376 }
377 #else
378 {
379 /* libvorbis is disabled. Should never hit this since initialization would have failed. */
380 assert(MA_FALSE);
381
382 (void)frameIndex;
383
384 return MA_NOT_IMPLEMENTED;
385 }
386 #endif
387 }
388
389 MA_API ma_result ma_libvorbis_get_data_format(ma_libvorbis* pVorbis, ma_format* pFormat, ma_uint32* pChannels, ma_uint32* pSampleRate, ma_channel* pChannelMap, size_t channelMapCap)
390 {
391 /* Defaults for safety. */
392 if (pFormat != NULL) {
393 *pFormat = ma_format_unknown;
394 }
395 if (pChannels != NULL) {
396 *pChannels = 0;
397 }
398 if (pSampleRate != NULL) {
399 *pSampleRate = 0;
400 }
401 if (pChannelMap != NULL) {
402 memset(pChannelMap, 0, sizeof(*pChannelMap) * channelMapCap);
403 }
404
405 if (pVorbis == NULL) {
406 return MA_INVALID_OPERATION;
407 }
408
409 if (pFormat != NULL) {
410 *pFormat = pVorbis->format;
411 }
412
413 #if !defined(MA_NO_LIBVORBIS)
414 {
415 vorbis_info* pInfo = ov_info((OggVorbis_File*)pVorbis->vf, 0);
416 if (pInfo == NULL) {
417 return MA_INVALID_OPERATION;
418 }
419
420 if (pChannels != NULL) {
421 *pChannels = pInfo->channels;
422 }
423
424 if (pSampleRate != NULL) {
425 *pSampleRate = pInfo->rate;
426 }
427
428 if (pChannelMap != NULL) {
429 ma_channel_map_init_standard(ma_standard_channel_map_vorbis, pChannelMap, channelMapCap, pInfo->channels);
430 }
431
432 return MA_SUCCESS;
433 }
434 #else
435 {
436 /* libvorbis is disabled. Should never hit this since initialization would have failed. */
437 assert(MA_FALSE);
438 return MA_NOT_IMPLEMENTED;
439 }
440 #endif
441 }
442
443 MA_API ma_result ma_libvorbis_get_cursor_in_pcm_frames(ma_libvorbis* pVorbis, ma_uint64* pCursor)
444 {
445 if (pCursor == NULL) {
446 return MA_INVALID_ARGS;
447 }
448
449 *pCursor = 0; /* Safety. */
450
451 if (pVorbis == NULL) {
452 return MA_INVALID_ARGS;
453 }
454
455 #if !defined(MA_NO_LIBVORBIS)
456 {
457 ogg_int64_t offset = ov_pcm_tell((OggVorbis_File*)pVorbis->vf);
458 if (offset < 0) {
459 return MA_INVALID_FILE;
460 }
461
462 *pCursor = (ma_uint64)offset;
463
464 return MA_SUCCESS;
465 }
466 #else
467 {
468 /* libvorbis is disabled. Should never hit this since initialization would have failed. */
469 assert(MA_FALSE);
470 return MA_NOT_IMPLEMENTED;
471 }
472 #endif
473 }
474
475 MA_API ma_result ma_libvorbis_get_length_in_pcm_frames(ma_libvorbis* pVorbis, ma_uint64* pLength)
476 {
477 if (pLength == NULL) {
478 return MA_INVALID_ARGS;
479 }
480
481 *pLength = 0; /* Safety. */
482
483 if (pVorbis == NULL) {
484 return MA_INVALID_ARGS;
485 }
486
487 #if !defined(MA_NO_LIBVORBIS)
488 {
489 /*
490 Will work in the supermajority of cases where a file has a single logical bitstream. Concatenated streams
491 are much harder to determine the length of since they can have sample rate changes, but they should be
492 extremely rare outside of unseekable livestreams anyway.
493 */
494 if (ov_streams((OggVorbis_File*)pVorbis->vf) == 1) {
495 ogg_int64_t length = ov_pcm_total((OggVorbis_File*)pVorbis->vf, 0);
496 if(length != OV_EINVAL) {
497 *pLength = (ma_uint64)length;
498 } else {
499 /* Unseekable. */
500 }
501 } else {
502 /* Concatenated stream. */
503 }
504
505 return MA_SUCCESS;
506 }
507 #else
508 {
509 /* libvorbis is disabled. Should never hit this since initialization would have failed. */
510 assert(MA_FALSE);
511 return MA_NOT_IMPLEMENTED;
512 }
513 #endif
514 }
515
516
517 /*
518 The code below defines the vtable that you'll plug into your `ma_decoder_config` object.
519 */
520 #if !defined(MA_NO_LIBVORBIS)
521 static ma_result ma_decoding_backend_init__libvorbis(void* pUserData, ma_read_proc onRead, ma_seek_proc onSeek, ma_tell_proc onTell, void* pReadSeekTellUserData, const ma_decoding_backend_config* pConfig, const ma_allocation_callbacks* pAllocationCallbacks, ma_data_source** ppBackend)
522 {
523 ma_result result;
524 ma_libvorbis* pVorbis;
525
526 (void)pUserData;
527
528 pVorbis = (ma_libvorbis*)ma_malloc(sizeof(*pVorbis), pAllocationCallbacks);
529 if (pVorbis == NULL) {
530 return MA_OUT_OF_MEMORY;
531 }
532
533 result = ma_libvorbis_init(onRead, onSeek, onTell, pReadSeekTellUserData, pConfig, pAllocationCallbacks, pVorbis);
534 if (result != MA_SUCCESS) {
535 ma_free(pVorbis, pAllocationCallbacks);
536 return result;
537 }
538
539 *ppBackend = pVorbis;
540
541 return MA_SUCCESS;
542 }
543
544 static ma_result ma_decoding_backend_init_file__libvorbis(void* pUserData, const char* pFilePath, const ma_decoding_backend_config* pConfig, const ma_allocation_callbacks* pAllocationCallbacks, ma_data_source** ppBackend)
545 {
546 ma_result result;
547 ma_libvorbis* pVorbis;
548
549 (void)pUserData;
550
551 pVorbis = (ma_libvorbis*)ma_malloc(sizeof(*pVorbis), pAllocationCallbacks);
552 if (pVorbis == NULL) {
553 return MA_OUT_OF_MEMORY;
554 }
555
556 result = ma_libvorbis_init_file(pFilePath, pConfig, pAllocationCallbacks, pVorbis);
557 if (result != MA_SUCCESS) {
558 ma_free(pVorbis, pAllocationCallbacks);
559 return result;
560 }
561
562 *ppBackend = pVorbis;
563
564 return MA_SUCCESS;
565 }
566
567 static void ma_decoding_backend_uninit__libvorbis(void* pUserData, ma_data_source* pBackend, const ma_allocation_callbacks* pAllocationCallbacks)
568 {
569 ma_libvorbis* pVorbis = (ma_libvorbis*)pBackend;
570
571 (void)pUserData;
572
573 ma_libvorbis_uninit(pVorbis, pAllocationCallbacks);
574 ma_free(pVorbis, pAllocationCallbacks);
575 }
576
577
578 static ma_decoding_backend_vtable ma_gDecodingBackendVTable_libvorbis =
579 {
580 ma_decoding_backend_init__libvorbis,
581 ma_decoding_backend_init_file__libvorbis,
582 NULL, /* onInitFileW() */
583 NULL, /* onInitMemory() */
584 ma_decoding_backend_uninit__libvorbis
585 };
586 ma_decoding_backend_vtable* ma_decoding_backend_libvorbis = &ma_gDecodingBackendVTable_libvorbis;
587 #else
588 ma_decoding_backend_vtable* ma_decoding_backend_libvorbis = NULL;
589 #endif
590
591 #endif /* miniaudio_libvorbis_c */