Debian Codegraph @

extras/miniaudio_libopus.h

raw symbols SCIP
1 /* THIS HAS BEEN DEPRECATED! Use the libopus decoder in extras/decoders/libopus instead. */
2
3 /*
4 This implements a data source that decodes Opus streams via libopus + libopusfile
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_libopus_h
12 #define miniaudio_libopus_h
13
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17
18 #if !defined(MA_NO_LIBOPUS)
19 #include <opusfile.h>
20 #endif
21
22 typedef struct
23 {
24 ma_data_source_base ds; /* The libopus decoder can be used independently as a data source. */
25 ma_read_proc onRead;
26 ma_seek_proc onSeek;
27 ma_tell_proc onTell;
28 void* pReadSeekTellUserData;
29 ma_format format; /* Will be either f32 or s16. */
30 #if !defined(MA_NO_LIBOPUS)
31 OggOpusFile* of;
32 #endif
33 } ma_libopus;
34
35 MA_API ma_result ma_libopus_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_libopus* pOpus);
36 MA_API ma_result ma_libopus_init_file(const char* pFilePath, const ma_decoding_backend_config* pConfig, const ma_allocation_callbacks* pAllocationCallbacks, ma_libopus* pOpus);
37 MA_API void ma_libopus_uninit(ma_libopus* pOpus, const ma_allocation_callbacks* pAllocationCallbacks);
38 MA_API ma_result ma_libopus_read_pcm_frames(ma_libopus* pOpus, void* pFramesOut, ma_uint64 frameCount, ma_uint64* pFramesRead);
39 MA_API ma_result ma_libopus_seek_to_pcm_frame(ma_libopus* pOpus, ma_uint64 frameIndex);
40 MA_API ma_result ma_libopus_get_data_format(ma_libopus* pOpus, ma_format* pFormat, ma_uint32* pChannels, ma_uint32* pSampleRate, ma_channel* pChannelMap, size_t channelMapCap);
41 MA_API ma_result ma_libopus_get_cursor_in_pcm_frames(ma_libopus* pOpus, ma_uint64* pCursor);
42 MA_API ma_result ma_libopus_get_length_in_pcm_frames(ma_libopus* pOpus, ma_uint64* pLength);
43
44 #ifdef __cplusplus
45 }
46 #endif
47 #endif
48
49 #if defined(MINIAUDIO_IMPLEMENTATION) || defined(MA_IMPLEMENTATION)
50
51 static ma_result ma_libopus_ds_read(ma_data_source* pDataSource, void* pFramesOut, ma_uint64 frameCount, ma_uint64* pFramesRead)
52 {
53 return ma_libopus_read_pcm_frames((ma_libopus*)pDataSource, pFramesOut, frameCount, pFramesRead);
54 }
55
56 static ma_result ma_libopus_ds_seek(ma_data_source* pDataSource, ma_uint64 frameIndex)
57 {
58 return ma_libopus_seek_to_pcm_frame((ma_libopus*)pDataSource, frameIndex);
59 }
60
61 static ma_result ma_libopus_ds_get_data_format(ma_data_source* pDataSource, ma_format* pFormat, ma_uint32* pChannels, ma_uint32* pSampleRate, ma_channel* pChannelMap, size_t channelMapCap)
62 {
63 return ma_libopus_get_data_format((ma_libopus*)pDataSource, pFormat, pChannels, pSampleRate, pChannelMap, channelMapCap);
64 }
65
66 static ma_result ma_libopus_ds_get_cursor(ma_data_source* pDataSource, ma_uint64* pCursor)
67 {
68 return ma_libopus_get_cursor_in_pcm_frames((ma_libopus*)pDataSource, pCursor);
69 }
70
71 static ma_result ma_libopus_ds_get_length(ma_data_source* pDataSource, ma_uint64* pLength)
72 {
73 return ma_libopus_get_length_in_pcm_frames((ma_libopus*)pDataSource, pLength);
74 }
75
76 static ma_data_source_vtable g_ma_libopus_ds_vtable =
77 {
78 ma_libopus_ds_read,
79 ma_libopus_ds_seek,
80 ma_libopus_ds_get_data_format,
81 ma_libopus_ds_get_cursor,
82 ma_libopus_ds_get_length
83 };
84
85
86 #if !defined(MA_NO_LIBOPUS)
87 static int ma_libopus_of_callback__read(void* pUserData, unsigned char* pBufferOut, int bytesToRead)
88 {
89 ma_libopus* pOpus = (ma_libopus*)pUserData;
90 ma_result result;
91 size_t bytesRead;
92
93 result = pOpus->onRead(pOpus->pReadSeekTellUserData, (void*)pBufferOut, bytesToRead, &bytesRead);
94
95 if (result != MA_SUCCESS) {
96 return -1;
97 }
98
99 return (int)bytesRead;
100 }
101
102 static int ma_libopus_of_callback__seek(void* pUserData, ogg_int64_t offset, int whence)
103 {
104 ma_libopus* pOpus = (ma_libopus*)pUserData;
105 ma_result result;
106 ma_seek_origin origin;
107
108 if (whence == SEEK_SET) {
109 origin = ma_seek_origin_start;
110 } else if (whence == SEEK_END) {
111 origin = ma_seek_origin_end;
112 } else {
113 origin = ma_seek_origin_current;
114 }
115
116 result = pOpus->onSeek(pOpus->pReadSeekTellUserData, offset, origin);
117 if (result != MA_SUCCESS) {
118 return -1;
119 }
120
121 return 0;
122 }
123
124 static opus_int64 ma_libopus_of_callback__tell(void* pUserData)
125 {
126 ma_libopus* pOpus = (ma_libopus*)pUserData;
127 ma_result result;
128 ma_int64 cursor;
129
130 if (pOpus->onTell == NULL) {
131 return -1;
132 }
133
134 result = pOpus->onTell(pOpus->pReadSeekTellUserData, &cursor);
135 if (result != MA_SUCCESS) {
136 return -1;
137 }
138
139 return cursor;
140 }
141 #endif
142
143 static ma_result ma_libopus_init_internal(const ma_decoding_backend_config* pConfig, ma_libopus* pOpus)
144 {
145 ma_result result;
146 ma_data_source_config dataSourceConfig;
147
148 if (pOpus == NULL) {
149 return MA_INVALID_ARGS;
150 }
151
152 MA_ZERO_OBJECT(pOpus);
153 pOpus->format = ma_format_f32; /* f32 by default. */
154
155 if (pConfig != NULL && (pConfig->preferredFormat == ma_format_f32 || pConfig->preferredFormat == ma_format_s16)) {
156 pOpus->format = pConfig->preferredFormat;
157 } else {
158 /* Getting here means something other than f32 and s16 was specified. Just leave this unset to use the default format. */
159 }
160
161 dataSourceConfig = ma_data_source_config_init();
162 dataSourceConfig.vtable = &g_ma_libopus_ds_vtable;
163
164 result = ma_data_source_init(&dataSourceConfig, &pOpus->ds);
165 if (result != MA_SUCCESS) {
166 return result; /* Failed to initialize the base data source. */
167 }
168
169 return MA_SUCCESS;
170 }
171
172 MA_API ma_result ma_libopus_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_libopus* pOpus)
173 {
174 ma_result result;
175
176 (void)pAllocationCallbacks; /* Can't seem to find a way to configure memory allocations in libopus. */
177
178 result = ma_libopus_init_internal(pConfig, pOpus);
179 if (result != MA_SUCCESS) {
180 return result;
181 }
182
183 if (onRead == NULL || onSeek == NULL) {
184 return MA_INVALID_ARGS; /* onRead and onSeek are mandatory. */
185 }
186
187 pOpus->onRead = onRead;
188 pOpus->onSeek = onSeek;
189 pOpus->onTell = onTell;
190 pOpus->pReadSeekTellUserData = pReadSeekTellUserData;
191
192 #if !defined(MA_NO_LIBOPUS)
193 {
194 int libopusResult;
195 OpusFileCallbacks libopusCallbacks;
196
197 /* We can now initialize the Opus decoder. This must be done after we've set up the callbacks. */
198 libopusCallbacks.read = ma_libopus_of_callback__read;
199 libopusCallbacks.seek = ma_libopus_of_callback__seek;
200 libopusCallbacks.close = NULL;
201 libopusCallbacks.tell = ma_libopus_of_callback__tell;
202
203 pOpus->of = op_open_callbacks(pOpus, &libopusCallbacks, NULL, 0, &libopusResult);
204 if (pOpus->of == NULL) {
205 return MA_INVALID_FILE;
206 }
207
208 return MA_SUCCESS;
209 }
210 #else
211 {
212 /* libopus is disabled. */
213 return MA_NOT_IMPLEMENTED;
214 }
215 #endif
216 }
217
218 MA_API ma_result ma_libopus_init_file(const char* pFilePath, const ma_decoding_backend_config* pConfig, const ma_allocation_callbacks* pAllocationCallbacks, ma_libopus* pOpus)
219 {
220 ma_result result;
221
222 (void)pAllocationCallbacks; /* Can't seem to find a way to configure memory allocations in libopus. */
223
224 result = ma_libopus_init_internal(pConfig, pOpus);
225 if (result != MA_SUCCESS) {
226 return result;
227 }
228
229 #if !defined(MA_NO_LIBOPUS)
230 {
231 int libopusResult;
232
233 pOpus->of = op_open_file(pFilePath, &libopusResult);
234 if (pOpus->of == NULL) {
235 return MA_INVALID_FILE;
236 }
237
238 return MA_SUCCESS;
239 }
240 #else
241 {
242 /* libopus is disabled. */
243 (void)pFilePath;
244 return MA_NOT_IMPLEMENTED;
245 }
246 #endif
247 }
248
249 MA_API void ma_libopus_uninit(ma_libopus* pOpus, const ma_allocation_callbacks* pAllocationCallbacks)
250 {
251 if (pOpus == NULL) {
252 return;
253 }
254
255 (void)pAllocationCallbacks;
256
257 #if !defined(MA_NO_LIBOPUS)
258 {
259 op_free(pOpus->of);
260 }
261 #else
262 {
263 /* libopus is disabled. Should never hit this since initialization would have failed. */
264 MA_ASSERT(MA_FALSE);
265 }
266 #endif
267
268 ma_data_source_uninit(&pOpus->ds);
269 }
270
271 MA_API ma_result ma_libopus_read_pcm_frames(ma_libopus* pOpus, void* pFramesOut, ma_uint64 frameCount, ma_uint64* pFramesRead)
272 {
273 if (pFramesRead != NULL) {
274 *pFramesRead = 0;
275 }
276
277 if (frameCount == 0) {
278 return MA_INVALID_ARGS;
279 }
280
281 if (pOpus == NULL) {
282 return MA_INVALID_ARGS;
283 }
284
285 #if !defined(MA_NO_LIBOPUS)
286 {
287 /* We always use floating point format. */
288 ma_result result = MA_SUCCESS; /* Must be initialized to MA_SUCCESS. */
289 ma_uint64 totalFramesRead;
290 ma_format format;
291 ma_uint32 channels;
292
293 ma_libopus_get_data_format(pOpus, &format, &channels, NULL, NULL, 0);
294
295 totalFramesRead = 0;
296 while (totalFramesRead < frameCount) {
297 long libopusResult;
298 int framesToRead;
299 ma_uint64 framesRemaining;
300
301 framesRemaining = (frameCount - totalFramesRead);
302 framesToRead = 1024;
303 if (framesToRead > framesRemaining) {
304 framesToRead = (int)framesRemaining;
305 }
306
307 if (format == ma_format_f32) {
308 libopusResult = op_read_float(pOpus->of, (float*)ma_offset_pcm_frames_ptr(pFramesOut, totalFramesRead, format, channels), framesToRead * channels, NULL);
309 } else {
310 libopusResult = op_read (pOpus->of, (opus_int16*)ma_offset_pcm_frames_ptr(pFramesOut, totalFramesRead, format, channels), framesToRead * channels, NULL);
311 }
312
313 if (libopusResult < 0) {
314 result = MA_ERROR; /* Error while decoding. */
315 break;
316 } else {
317 totalFramesRead += libopusResult;
318
319 if (libopusResult == 0) {
320 result = MA_AT_END;
321 break;
322 }
323 }
324 }
325
326 if (pFramesRead != NULL) {
327 *pFramesRead = totalFramesRead;
328 }
329
330 if (result == MA_SUCCESS && totalFramesRead == 0) {
331 result = MA_AT_END;
332 }
333
334 return result;
335 }
336 #else
337 {
338 /* libopus is disabled. Should never hit this since initialization would have failed. */
339 MA_ASSERT(MA_FALSE);
340
341 (void)pFramesOut;
342 (void)frameCount;
343 (void)pFramesRead;
344
345 return MA_NOT_IMPLEMENTED;
346 }
347 #endif
348 }
349
350 MA_API ma_result ma_libopus_seek_to_pcm_frame(ma_libopus* pOpus, ma_uint64 frameIndex)
351 {
352 if (pOpus == NULL) {
353 return MA_INVALID_ARGS;
354 }
355
356 #if !defined(MA_NO_LIBOPUS)
357 {
358 int libopusResult = op_pcm_seek(pOpus->of, (ogg_int64_t)frameIndex);
359 if (libopusResult != 0) {
360 if (libopusResult == OP_ENOSEEK) {
361 return MA_INVALID_OPERATION; /* Not seekable. */
362 } else if (libopusResult == OP_EINVAL) {
363 return MA_INVALID_ARGS;
364 } else {
365 return MA_ERROR;
366 }
367 }
368
369 return MA_SUCCESS;
370 }
371 #else
372 {
373 /* libopus is disabled. Should never hit this since initialization would have failed. */
374 MA_ASSERT(MA_FALSE);
375
376 (void)frameIndex;
377
378 return MA_NOT_IMPLEMENTED;
379 }
380 #endif
381 }
382
383 MA_API ma_result ma_libopus_get_data_format(ma_libopus* pOpus, ma_format* pFormat, ma_uint32* pChannels, ma_uint32* pSampleRate, ma_channel* pChannelMap, size_t channelMapCap)
384 {
385 /* Defaults for safety. */
386 if (pFormat != NULL) {
387 *pFormat = ma_format_unknown;
388 }
389 if (pChannels != NULL) {
390 *pChannels = 0;
391 }
392 if (pSampleRate != NULL) {
393 *pSampleRate = 0;
394 }
395 if (pChannelMap != NULL) {
396 MA_ZERO_MEMORY(pChannelMap, sizeof(*pChannelMap) * channelMapCap);
397 }
398
399 if (pOpus == NULL) {
400 return MA_INVALID_OPERATION;
401 }
402
403 if (pFormat != NULL) {
404 *pFormat = pOpus->format;
405 }
406
407 #if !defined(MA_NO_LIBOPUS)
408 {
409 ma_uint32 channels = op_channel_count(pOpus->of, -1);
410
411 if (pChannels != NULL) {
412 *pChannels = channels;
413 }
414
415 if (pSampleRate != NULL) {
416 *pSampleRate = 48000;
417 }
418
419 if (pChannelMap != NULL) {
420 ma_channel_map_init_standard(ma_standard_channel_map_vorbis, pChannelMap, channelMapCap, channels);
421 }
422
423 return MA_SUCCESS;
424 }
425 #else
426 {
427 /* libopus is disabled. Should never hit this since initialization would have failed. */
428 MA_ASSERT(MA_FALSE);
429 return MA_NOT_IMPLEMENTED;
430 }
431 #endif
432 }
433
434 MA_API ma_result ma_libopus_get_cursor_in_pcm_frames(ma_libopus* pOpus, ma_uint64* pCursor)
435 {
436 if (pCursor == NULL) {
437 return MA_INVALID_ARGS;
438 }
439
440 *pCursor = 0; /* Safety. */
441
442 if (pOpus == NULL) {
443 return MA_INVALID_ARGS;
444 }
445
446 #if !defined(MA_NO_LIBOPUS)
447 {
448 ogg_int64_t offset = op_pcm_tell(pOpus->of);
449 if (offset < 0) {
450 return MA_INVALID_FILE;
451 }
452
453 *pCursor = (ma_uint64)offset;
454
455 return MA_SUCCESS;
456 }
457 #else
458 {
459 /* libopus is disabled. Should never hit this since initialization would have failed. */
460 MA_ASSERT(MA_FALSE);
461 return MA_NOT_IMPLEMENTED;
462 }
463 #endif
464 }
465
466 MA_API ma_result ma_libopus_get_length_in_pcm_frames(ma_libopus* pOpus, ma_uint64* pLength)
467 {
468 if (pLength == NULL) {
469 return MA_INVALID_ARGS;
470 }
471
472 *pLength = 0; /* Safety. */
473
474 if (pOpus == NULL) {
475 return MA_INVALID_ARGS;
476 }
477
478 #if !defined(MA_NO_LIBOPUS)
479 {
480 ogg_int64_t length = op_pcm_total(pOpus->of, -1);
481 if (length < 0) {
482 return MA_ERROR;
483 }
484
485 *pLength = (ma_uint64)length;
486
487 return MA_SUCCESS;
488 }
489 #else
490 {
491 /* libopus is disabled. Should never hit this since initialization would have failed. */
492 MA_ASSERT(MA_FALSE);
493 return MA_NOT_IMPLEMENTED;
494 }
495 #endif
496 }
497
498 #endif