Debian Codegraph @

extras/miniaudio_libvorbis.h

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