Debian Codegraph @

extras/decoders/libopus/miniaudio_libopus.c

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