-
Notifications
You must be signed in to change notification settings - Fork 0
/
codec.c
259 lines (244 loc) · 10 KB
/
codec.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* This file is part of dsp.
*
* Copyright (c) 2013-2024 Michael Barbour <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"
#include "codec.h"
#include "null.h"
#include "sgen.h"
#include "sndfile.h"
#include "ffmpeg.h"
#include "alsa.h"
#include "ao.h"
#include "mp3.h"
#include "pcm.h"
#include "pulse.h"
struct codec_info {
const char *type;
const char **ext; /* null terminated array of extension strings */
int modes;
/* args: path, type, encoding, fs, channels, endian, mode */
struct codec * (*init)(const char *, const char *, const char *, int, int, int, int);
void (*print_encodings)(const char *);
};
#ifdef HAVE_SNDFILE
static const char *wav_ext[] = { ".wav", NULL };
static const char *aiff_ext[] = { ".aif", ".aiff", ".aifc", NULL };
static const char *au_ext[] = { ".au", NULL };
static const char *raw_ext[] = { ".raw", NULL };
static const char *paf_ext[] = { ".paf", NULL };
static const char *svx_ext[] = { ".8svx", ".iff", NULL };
static const char *nist_ext[] = { ".nist", NULL };
static const char *voc_ext[] = { ".voc", NULL };
static const char *ircam_ext[] = { ".sf", NULL };
static const char *w64_ext[] = { ".w64", NULL };
static const char *mat4_ext[] = { ".mat", NULL };
static const char *mat5_ext[] = { ".mat", NULL };
static const char *pvf_ext[] = { ".pvf", NULL };
static const char *xi_ext[] = { ".xi", NULL };
static const char *htk_ext[] = { ".htk", NULL };
static const char *sds_ext[] = { ".sds", NULL };
static const char *avr_ext[] = { ".avr", NULL };
static const char *wavex_ext[] = { ".wav", ".wavex", NULL };
static const char *sd2_ext[] = { ".sd2", NULL };
static const char *flac_ext[] = { ".flac", NULL };
static const char *caf_ext[] = { ".caf", NULL };
static const char *wve_ext[] = { ".wve", NULL };
static const char *ogg_ext[] = { ".ogg", ".oga", ".ogv", NULL };
static const char *mpc2k_ext[] = { ".mpc2k", NULL };
static const char *rf64_ext[] = { ".wav", ".rf64", NULL };
#endif
#ifdef HAVE_MAD
static const char *mp3_ext[] = { ".mp3", NULL };
#endif
struct codec_info codecs[] = {
#ifndef LADSPA_FRONTEND
{ "null", NULL, CODEC_MODE_READ|CODEC_MODE_WRITE, null_codec_init, null_codec_print_encodings },
{ "sgen", NULL, CODEC_MODE_READ, sgen_codec_init, sgen_codec_print_encodings },
#endif
#ifdef HAVE_SNDFILE
{ "sndfile", NULL, CODEC_MODE_READ, sndfile_codec_init, sndfile_codec_print_encodings },
{ "wav", wav_ext, CODEC_MODE_READ|CODEC_MODE_WRITE, sndfile_codec_init, sndfile_codec_print_encodings },
{ "aiff", aiff_ext, CODEC_MODE_READ|CODEC_MODE_WRITE, sndfile_codec_init, sndfile_codec_print_encodings },
{ "au", au_ext, CODEC_MODE_READ|CODEC_MODE_WRITE, sndfile_codec_init, sndfile_codec_print_encodings },
{ "raw", raw_ext, CODEC_MODE_READ|CODEC_MODE_WRITE, sndfile_codec_init, sndfile_codec_print_encodings },
{ "paf", paf_ext, CODEC_MODE_READ|CODEC_MODE_WRITE, sndfile_codec_init, sndfile_codec_print_encodings },
{ "svx", svx_ext, CODEC_MODE_READ|CODEC_MODE_WRITE, sndfile_codec_init, sndfile_codec_print_encodings },
{ "nist", nist_ext, CODEC_MODE_READ|CODEC_MODE_WRITE, sndfile_codec_init, sndfile_codec_print_encodings },
{ "voc", voc_ext, CODEC_MODE_READ|CODEC_MODE_WRITE, sndfile_codec_init, sndfile_codec_print_encodings },
{ "ircam", ircam_ext, CODEC_MODE_READ|CODEC_MODE_WRITE, sndfile_codec_init, sndfile_codec_print_encodings },
{ "w64", w64_ext, CODEC_MODE_READ|CODEC_MODE_WRITE, sndfile_codec_init, sndfile_codec_print_encodings },
{ "mat4", mat4_ext, CODEC_MODE_READ|CODEC_MODE_WRITE, sndfile_codec_init, sndfile_codec_print_encodings },
{ "mat5", mat5_ext, CODEC_MODE_READ|CODEC_MODE_WRITE, sndfile_codec_init, sndfile_codec_print_encodings },
{ "pvf", pvf_ext, CODEC_MODE_READ|CODEC_MODE_WRITE, sndfile_codec_init, sndfile_codec_print_encodings },
{ "xi", xi_ext, CODEC_MODE_READ|CODEC_MODE_WRITE, sndfile_codec_init, sndfile_codec_print_encodings },
{ "htk", htk_ext, CODEC_MODE_READ|CODEC_MODE_WRITE, sndfile_codec_init, sndfile_codec_print_encodings },
{ "sds", sds_ext, CODEC_MODE_READ|CODEC_MODE_WRITE, sndfile_codec_init, sndfile_codec_print_encodings },
{ "avr", avr_ext, CODEC_MODE_READ|CODEC_MODE_WRITE, sndfile_codec_init, sndfile_codec_print_encodings },
{ "wavex", wavex_ext, CODEC_MODE_READ|CODEC_MODE_WRITE, sndfile_codec_init, sndfile_codec_print_encodings },
{ "sd2", sd2_ext, CODEC_MODE_READ|CODEC_MODE_WRITE, sndfile_codec_init, sndfile_codec_print_encodings },
{ "flac", flac_ext, CODEC_MODE_READ|CODEC_MODE_WRITE, sndfile_codec_init, sndfile_codec_print_encodings },
{ "caf", caf_ext, CODEC_MODE_READ|CODEC_MODE_WRITE, sndfile_codec_init, sndfile_codec_print_encodings },
{ "wve", wve_ext, CODEC_MODE_READ|CODEC_MODE_WRITE, sndfile_codec_init, sndfile_codec_print_encodings },
{ "ogg", ogg_ext, CODEC_MODE_READ|CODEC_MODE_WRITE, sndfile_codec_init, sndfile_codec_print_encodings },
{ "mpc2k", mpc2k_ext, CODEC_MODE_READ|CODEC_MODE_WRITE, sndfile_codec_init, sndfile_codec_print_encodings },
{ "rf64", rf64_ext, CODEC_MODE_READ|CODEC_MODE_WRITE, sndfile_codec_init, sndfile_codec_print_encodings },
#endif
#ifdef HAVE_FFMPEG
{ "ffmpeg", NULL, CODEC_MODE_READ, ffmpeg_codec_init, ffmpeg_codec_print_encodings },
#endif
#ifdef HAVE_ALSA
{ "alsa", NULL, CODEC_MODE_READ|CODEC_MODE_WRITE, alsa_codec_init, alsa_codec_print_encodings },
#endif
#ifdef HAVE_AO
{ "ao", NULL, CODEC_MODE_WRITE, ao_codec_init, ao_codec_print_encodings },
#endif
#ifdef HAVE_MAD
{ "mp3", mp3_ext, CODEC_MODE_READ, mp3_codec_init, mp3_codec_print_encodings },
#endif
#ifndef LADSPA_FRONTEND
{ "pcm", NULL, CODEC_MODE_READ|CODEC_MODE_WRITE, pcm_codec_init, pcm_codec_print_encodings },
#endif
#ifdef HAVE_PULSE
{ "pulse", NULL, CODEC_MODE_READ|CODEC_MODE_WRITE, pulse_codec_init, pulse_codec_print_encodings },
#endif
};
static const char *fallback_input_codecs[] = {
#ifdef HAVE_SNDFILE
"sndfile",
#endif
#ifdef HAVE_FFMPEG
"ffmpeg",
#endif
};
static const char *fallback_output_codecs[] = {
#ifdef HAVE_PULSE
"pulse",
#endif
#ifdef HAVE_ALSA
"alsa",
#endif
#ifdef HAVE_AO
"ao",
#endif
};
static struct codec_info * get_codec_info_by_type(const char *type)
{
int i;
for (i = 0; i < LENGTH(codecs); ++i)
if (strcmp(type, codecs[i].type) == 0)
return &codecs[i];
return NULL;
}
static struct codec_info * get_codec_info_by_ext(const char *ext)
{
int i, k;
for (i = 0; i < LENGTH(codecs); ++i) {
if (codecs[i].ext == NULL)
continue;
for (k = 0; codecs[i].ext[k] != NULL; ++k)
if (strcasecmp(ext, codecs[i].ext[k]) == 0)
return &codecs[i];
}
return NULL;
}
struct codec * init_codec(const char *path, const char *type, const char *enc, int fs, int channels, int endian, int mode)
{
int i, old_loglevel;
struct codec_info *info;
struct codec *c = NULL;
const char *ext;
ext = strrchr(path, '.');
if (type != NULL) {
info = get_codec_info_by_type(type);
if (info == NULL) {
LOG_FMT(LL_ERROR, "error: bad type: %s", type);
return NULL;
}
if (info->modes & mode)
return info->init(path, info->type, enc, fs, channels, endian, mode);
LOG_FMT(LL_ERROR, "%s: error: mode '%c' not supported", info->type, (mode == CODEC_MODE_READ) ? 'r' : 'w');
return NULL;
}
if (ext != NULL && (info = get_codec_info_by_ext(ext)) != NULL) {
if (info->modes & mode)
return info->init(path, info->type, enc, fs, channels, endian, mode);
LOG_FMT(LL_ERROR, "%s: error: mode '%c' not supported", info->type, (mode == CODEC_MODE_READ) ? 'r' : 'w');
return NULL;
}
c = NULL;
if ((old_loglevel = dsp_globals.loglevel) == LL_NORMAL)
dsp_globals.loglevel = LL_ERROR;
if (mode == CODEC_MODE_WRITE) {
if (LENGTH(fallback_output_codecs) == 0)
LOG_S(LL_ERROR, "error: no fallback output(s) available and no output given");
for (i = 0; i < LENGTH(fallback_output_codecs); ++i) {
info = get_codec_info_by_type(fallback_output_codecs[i]);
if (info != NULL && info->modes & mode && (c = info->init(path, info->type, enc, fs, channels, endian, mode)) != NULL)
break;
}
}
else {
for (i = 0; i < LENGTH(fallback_input_codecs); ++i) {
info = get_codec_info_by_type(fallback_input_codecs[i]);
if (info != NULL && info->modes & mode && (c = info->init(path, info->type, enc, fs, channels, endian, mode)) != NULL)
break;
}
}
dsp_globals.loglevel = old_loglevel;
return c;
}
void destroy_codec(struct codec *c)
{
c->destroy(c);
free(c);
}
void append_codec(struct codec_list *l, struct codec *c)
{
if (l->tail == NULL)
l->head = c;
else
l->tail->next = c;
l->tail = c;
c->next = NULL;
}
void destroy_codec_list_head(struct codec_list *l)
{
struct codec *h = l->head;
if (h == NULL)
return;
l->head = h->next;
destroy_codec(h);
}
void destroy_codec_list(struct codec_list *l)
{
while (l->head != NULL)
destroy_codec_list_head(l);
}
void print_all_codecs(void)
{
int i;
fprintf(stdout, "Types:\n Type: Modes: Encodings:\n");
for (i = 0; i < LENGTH(codecs); ++i) {
fprintf(stdout, " %-8s %c%c ", codecs[i].type,
(codecs[i].modes & CODEC_MODE_READ) ? 'r' : ' ', (codecs[i].modes & CODEC_MODE_WRITE) ? 'w' : ' ');
codecs[i].print_encodings(codecs[i].type);
fputc('\n', stdout);
}
}