forked from corecode/spdif-loop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspdif-loop.c
376 lines (313 loc) · 8.17 KB
/
spdif-loop.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#define _GNU_SOURCE
#include <math.h>
#include <stdio.h>
#include <unistd.h>
#include <err.h>
#include <getopt.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavdevice/avdevice.h>
#include <ao/ao.h>
#define IO_BUFFER_SIZE 32768
struct alsa_read_state {
AVFormatContext *ctx;
AVPacket pkt;
int offset;
};
static int debug_data;
static void
usage(void)
{
fprintf(stderr,
"usage:\n"
" spdif-loop [-t | -i <hw:alsa-input-dev>] -d <alsa|pulse> -o <output-dev>\n");
exit(1);
}
static int
alsa_reader(void *data, uint8_t *buf, int buf_size)
{
struct alsa_read_state *st = data;
int read_size = 0;
while (buf_size > 0) {
if (st->pkt.size <= 0) {
int ret = av_read_frame(st->ctx, &st->pkt);
st->offset = 0;
if (ret != 0)
return (ret);
}
int pkt_left = st->pkt.size - st->offset;
int datsize = buf_size < pkt_left ? buf_size : pkt_left;
memcpy(buf, st->pkt.data + st->offset, datsize);
st->offset += datsize;
read_size += datsize;
buf += datsize;
buf_size -= datsize;
if (debug_data) {
static int had_zeros = 0;
for (int i = 0; i < read_size; ++i) {
const char zeros[16] = {0};
if (i % 16 == 0 && read_size - i >= 16 &&
memcmp((char *)buf + i, zeros, 16) == 0) {
i += 15;
if (had_zeros && had_zeros % 10000 == 0)
printf(" (%d)\n", had_zeros * 16);
if (!had_zeros)
printf("...\n");
had_zeros++;
continue;
}
if (had_zeros)
printf(" (%d)\n", had_zeros * 16);
had_zeros = 0;
printf("%02x%s", ((unsigned char *)buf)[i],
(i + 1) % 16 == 0 ? "\n" : " ");
}
}
if (st->offset >= st->pkt.size)
av_free_packet(&st->pkt);
}
return (read_size);
}
static enum CodecID
probe_codec(AVFormatContext *s)
{
AVPacket pkt;
av_init_packet(&pkt);
if (av_read_frame(s, &pkt) != 0)
return (CODEC_ID_NONE);
av_free_packet(&pkt);
if (s->nb_streams == 0)
return (CODEC_ID_NONE);
return (s->streams[0]->codec->codec_id);
}
static ao_device *
open_output(int driver_id, ao_option *dev_opts, int bits, int channels, int sample_rate)
{
printf("%d bit, %d channels, %dHz\n",
bits,
channels,
sample_rate);
ao_sample_format out_fmt = {
.bits = bits,
.channels = channels,
.rate = sample_rate,
.byte_format = AO_FMT_NATIVE,
.matrix = "L,R,C,LFE,BL,BR",
};
return (ao_open_live(driver_id, &out_fmt, dev_opts));
}
static int
test_audio_out(int driver_id, ao_option *dev_opts)
{
struct chan_map {
const char *name;
int freq;
int idx;
} map[] = {
/* This needs to match the order in open_output(). */
{ "left", 500, 0 },
{ "center", 500, 2 },
{ "right", 500, 1 },
{ "rear right", 500, 5 },
{ "rear left", 500, 4 },
{ "sub", 50, 3 }
};
ao_device *odev = open_output(driver_id, dev_opts, 16, 6, 48000);
if (!odev)
errx(1, "cannot open audio output");
for (int ch = 0; ch < 6; ++ch) {
const size_t buflen = 4800; /* 1/10 of a second */
int16_t buf[buflen * 6];
printf("channel %d: %s\n", map[ch].idx, map[ch].name);
/* prepare sine samples */
memset(buf, 0, sizeof(buf));
for (int i = 0; i < buflen; ++i) {
buf[i * 6 + map[ch].idx] = INT16_MAX / 10 * cos(2 * M_PI * map[ch].freq * i / 48000.0);
}
/* play for 2 sec, 1 sec pause */
for (int i = 0; i < 30; ++i) {
if (i == 20) {
/* now pause */
memset(buf, 0, sizeof(buf));
}
if (!ao_play(odev, (char *)buf, sizeof(buf)))
errx(1, "cannot play test audio");
}
}
return (0);
}
int
main(int argc, char **argv)
{
int opt_test = 0;
char *alsa_dev_name = NULL;
char *out_driver_name = NULL;
char *out_dev_name = NULL;
for (int opt = 0; (opt = getopt(argc, argv, "d:hi:o:tv")) != -1;) {
switch (opt) {
case 'd':
out_driver_name = optarg;
break;
case 'i':
alsa_dev_name = optarg;
break;
case 'o':
out_dev_name = optarg;
break;
case 't':
opt_test = 1;
break;
case 'v':
debug_data = 1;
break;
default:
usage();
/* NOTREACHED */
}
}
argc -= optind;
argv += optind;
if (argc != 0)
usage();
if (!(opt_test ^ !!alsa_dev_name)) {
fprintf(stderr, "please specify either input device or testing mode\n\n");
usage();
}
av_register_all();
avcodec_register_all();
avdevice_register_all();
ao_initialize();
ao_option *out_dev_opts = NULL;
if (out_dev_name) {
if (!ao_append_option(&out_dev_opts, "dev", out_dev_name))
errx(1, "cannot set output device `%s'", out_dev_name);
}
int out_driver_id = ao_default_driver_id();
if (out_driver_name)
out_driver_id = ao_driver_id(out_driver_name);
if (out_driver_id < 0)
errx(1, "invalid output driver `%s'",
out_driver_name ? out_driver_name : "default");
if (opt_test) {
exit(test_audio_out(out_driver_id, out_dev_opts));
/* NOTREACHED */
}
AVInputFormat *alsa_fmt = av_find_input_format("alsa");
if (!alsa_fmt)
errx(1, "cannot find alsa input driver");
AVInputFormat *spdif_fmt = av_find_input_format("spdif");
if (!spdif_fmt)
errx(1, "cannot find spdif demux driver");
const int alsa_buf_size = IO_BUFFER_SIZE;
unsigned char *alsa_buf = av_malloc(alsa_buf_size);
if (!alsa_buf)
errx(1, "cannot allocate input buffer");
AVFormatContext *spdif_ctx = NULL;
AVFormatContext *alsa_ctx = NULL;
ao_device *out_dev = NULL;
if (0) {
retry:
printf("failure...\n");
if (spdif_ctx)
avformat_close_input(&spdif_ctx);
if (alsa_ctx)
avformat_close_input(&alsa_ctx);
if (out_dev) {
ao_close(out_dev);
out_dev = NULL;
}
sleep(1);
printf("retrying.\n");
}
spdif_ctx = avformat_alloc_context();
if (!spdif_ctx)
errx(1, "cannot allocate spdif context");
if (avformat_open_input(&alsa_ctx, alsa_dev_name, alsa_fmt, NULL) != 0)
errx(1, "cannot open alsa input");
struct alsa_read_state read_state = {
.ctx = alsa_ctx,
};
av_init_packet(&read_state.pkt);
spdif_ctx->pb = avio_alloc_context(alsa_buf, alsa_buf_size, 0, &read_state, alsa_reader, NULL, NULL);
if (!spdif_ctx->pb)
errx(1, "cannot set up alsa reader");
if (avformat_open_input(&spdif_ctx, "internal", spdif_fmt, NULL) != 0)
errx(1, "cannot open spdif input");
enum CodecID spdif_codec_id = probe_codec(spdif_ctx);
#if HAVE_AVCODEC_GET_NAME
printf("detected spdif codec %s\n", avcodec_get_name(spdif_codec_id));
#endif
AVCodec *spdif_codec = avcodec_find_decoder(spdif_codec_id);
if (!spdif_codec) {
printf("could not find codec\n");
goto retry;
}
AVCodecContext *spdif_codec_ctx = avcodec_alloc_context3(spdif_codec);
if (!spdif_codec_ctx)
errx(1, "cannot allocate codec");
spdif_codec_ctx->request_sample_fmt = AV_SAMPLE_FMT_S16;
if (avcodec_open2(spdif_codec_ctx, spdif_codec, NULL) != 0)
errx(1, "cannot open codec");
AVPacket pkt, pkt1 = {.size = 0, .data = NULL};
av_init_packet(&pkt1);
pkt = pkt1;
AVFrame frame;
for (;;) {
if (pkt.size == 0) {
av_free_packet(&pkt1);
int e = av_read_frame(spdif_ctx, &pkt1);
if (e != 0) {
printf("reading frame failed: %d\n", e);
goto retry;
}
pkt = pkt1;
}
avcodec_get_frame_defaults(&frame);
int got_frame = 0;
int processed_len = avcodec_decode_audio4(spdif_codec_ctx, &frame, &got_frame, &pkt);
if (processed_len < 0)
errx(1, "cannot decode input");
pkt.data += processed_len;
pkt.size -= processed_len;
if (!got_frame)
continue;
if (!out_dev) {
/**
* We open the output only here, because we need a full frame decoded
* before we can know the output format.
*/
out_dev = open_output(out_driver_id,
out_dev_opts,
av_get_bytes_per_sample(spdif_codec_ctx->sample_fmt) * 8,
spdif_codec_ctx->channels,
spdif_codec_ctx->sample_rate);
if (!out_dev)
errx(1, "cannot open audio output");
}
int framesize = av_samples_get_buffer_size(NULL,
spdif_codec_ctx->channels,
frame.nb_samples,
spdif_codec_ctx->sample_fmt,
1);
#if DEBUG
int max = 0;
int16_t *fb = (void *)frame.data[0];
for (int i = 0; i < frame.nb_samples * spdif_codec_ctx->channels; ++i) {
int v = fb[i];
if (v < 0)
v = -v;
if (v > max)
max = v;
}
/* Debug latency */
for (int i = 0; i < max / 100; ++i)
putchar('*');
printf("\n");
//printf("%d\n", max);
#endif
if (!ao_play(out_dev, (void *)frame.data[0], framesize)) {
goto retry;
}
}
return (0);
}