Add initial bits for Qt6 support
[carla.git] / source / modules / audio_decoder / ad_dr_mp3.c
blob5e41c5f3c3a815cb48421fd83c9d31ef7b29f906
1 /**
2 Copyright (C) 2011-2013 Robin Gareus <robin@gareus.org>
3 Copyright (C) 2014-2023 Filipe Coelho <falktx@falktx.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser Public License as published by
7 the Free Software Foundation; either version 2.1, or (at your option)
8 any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <math.h>
26 #include "ad_plugin.h"
28 #define DR_MP3_FLOAT_OUTPUT
29 #define DR_MP3_IMPLEMENTATION
30 #define DRMP3_DATA_CHUNK_SIZE DRMP3_MIN_DATA_CHUNK_SIZE*40
31 #include "dr_mp3.h"
33 /* internal abstraction */
35 #define DR_MP3_MAX_SEEK_POINTS 500
37 #ifdef _MSC_VER
38 #define strcasecmp stricmp
39 #endif
41 typedef struct {
42 drmp3 mp3;
43 drmp3_seek_point seekPoints[DR_MP3_MAX_SEEK_POINTS];
44 } drmp3_audio_decoder;
46 static int ad_info_dr_mp3(void *sf, struct adinfo *nfo) {
47 drmp3_audio_decoder *priv = (drmp3_audio_decoder*) sf;
48 if (!priv) return -1;
49 if (nfo) {
50 nfo->channels = priv->mp3.channels;
51 nfo->frames = drmp3_get_pcm_frame_count(&priv->mp3);
52 nfo->sample_rate = priv->mp3.sampleRate;
53 nfo->length = nfo->sample_rate ? (nfo->frames * 1000) / nfo->sample_rate : 0;
54 nfo->bit_depth = 16;
55 nfo->bit_rate = priv->mp3.frameInfo.bitrate_kbps * 1000;
56 nfo->meta_data = NULL;
57 nfo->can_seek = 1;
59 return 0;
62 static void *ad_open_dr_mp3(const char *filename, struct adinfo *nfo) {
63 drmp3_audio_decoder *priv = (drmp3_audio_decoder*)calloc (1, sizeof(drmp3_audio_decoder));
64 drmp3_bool32 res = drmp3_init_file(&priv->mp3, filename, NULL);
65 if (res == DRMP3_FALSE) {
66 dbg(0, "unable to open file '%s'.", filename);
67 free (priv);
68 return NULL;
70 drmp3_uint32 seekPointCount = DR_MP3_MAX_SEEK_POINTS;
71 drmp3_calculate_seek_points (&priv->mp3, &seekPointCount, priv->seekPoints);
72 drmp3_bind_seek_table (&priv->mp3, seekPointCount, priv->seekPoints);
73 ad_info_dr_mp3 (priv, nfo);
74 return (void*) priv;
77 static int ad_close_dr_mp3(void *sf) {
78 drmp3_audio_decoder *priv = (drmp3_audio_decoder*) sf;
79 if (!priv) return -1;
80 if (!sf) {
81 dbg (0, "fatal: bad file close.\n");
82 return -1;
84 drmp3_uninit (&priv->mp3);
85 free (priv);
86 return 0;
89 static int64_t ad_seek_dr_mp3(void *sf, int64_t pos)
91 drmp3_audio_decoder *priv = (drmp3_audio_decoder*) sf;
92 if (!priv) return -1;
93 return drmp3_seek_to_pcm_frame (&priv->mp3, pos);
96 static ssize_t ad_read_dr_mp3(void *sf, float* d, size_t len)
98 drmp3_audio_decoder *priv = (drmp3_audio_decoder*) sf;
99 if (!priv) return -1;
100 return drmp3_read_pcm_frames_f32 (&priv->mp3, len / priv->mp3.channels, d) * priv->mp3.channels;
103 static int ad_get_bitrate_dr_mp3(void *sf) {
104 drmp3_audio_decoder *priv = (drmp3_audio_decoder*) sf;
105 if (!priv) return -1;
106 return priv->mp3.frameInfo.bitrate_kbps * 1000;
109 static int ad_eval_dr_mp3(const char *f)
111 char *ext = strrchr(f, '.');
112 if (strstr (f, "://")) return 0;
113 if (!ext) return 5;
114 if (!strcasecmp(ext, ".mp3")) return 100;
115 return 0;
118 static const ad_plugin ad_dr_mp3 = {
119 #if 1
120 &ad_eval_dr_mp3,
121 &ad_open_dr_mp3,
122 &ad_close_dr_mp3,
123 &ad_info_dr_mp3,
124 &ad_seek_dr_mp3,
125 &ad_read_dr_mp3,
126 &ad_get_bitrate_dr_mp3
127 #else
128 &ad_eval_null,
129 &ad_open_null,
130 &ad_close_null,
131 &ad_info_null,
132 &ad_seek_null,
133 &ad_read_null,
134 &ad_bit_rate_null
135 #endif
138 /* dlopen handler */
139 const ad_plugin * adp_get_dr_mp3() {
140 return &ad_dr_mp3;