summaryrefslogtreecommitdiff
path: root/sys/src/cmd/audio/flacdec/flacdec.c
blob: fcdd2e57af0e6baec5a3193e33506dcb755b2981 (plain)
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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#define _PLAN9_SOURCE
#include <utf.h>
#include <lib9.h>
#include "FLAC/stream_decoder.h"

static int ifd = -1;
static int sts;

static void
flushout(void)
{
	if(ifd >= 0){
		close(ifd);
		wait(&sts);
	}
}

static FLAC__StreamDecoderReadStatus
decinput(FLAC__StreamDecoder *dec, FLAC__byte buffer[], size_t *bytes, void *client_data)
{
	int n = *bytes;

	n = fread(buffer, 1, n, stdin);
	if(n < 0)
		return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
	if(n == 0)
		return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;

	*bytes = n;
	return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
}

static FLAC__StreamDecoderSeekStatus
decseek(FLAC__StreamDecoder *dec, FLAC__uint64 absolute_byte_offset, void *client_data)
{
	if(fseeko(stdin, absolute_byte_offset, SEEK_SET) < 0)
		return FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED;

	return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
}

static FLAC__StreamDecoderTellStatus
dectell(FLAC__StreamDecoder *dec, FLAC__uint64 *absolute_byte_offset, void *client_data)
{
	off_t off;

	if((off = ftello(stdin)) < 0)
		return FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED;

	*absolute_byte_offset = off;
	return FLAC__STREAM_DECODER_TELL_STATUS_OK;
}

static FLAC__StreamDecoderTellStatus
declen(FLAC__StreamDecoder *dec, FLAC__uint64 *stream_length, void *client_data)
{
	off_t off;

	if((off = ftello(stdin)) < 0 || fseeko(stdin, 0, SEEK_END) < 0)
		return FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED;

	*stream_length = ftello(stdin);
	fseeko(stdin, off, SEEK_SET);

	return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
}

static FLAC__StreamDecoderWriteStatus
decoutput(FLAC__StreamDecoder *dec, FLAC__Frame *frame, FLAC__int32 *buffer[], void *client_data)
{
	static int rate, chans, bits;
	static unsigned char *buf;
	static int nbuf;
	FLAC__int32 *s, v;
	unsigned char *p;
	int i, j, n, b, len;

	/* start converter if format changed */
	if(rate != frame->header.sample_rate
	|| chans != frame->header.channels
	|| bits != frame->header.bits_per_sample){
		int pid, pfd[2];
		char fmt[32];

		rate = frame->header.sample_rate;
		chans = frame->header.channels;
		bits = frame->header.bits_per_sample;
		sprintf(fmt, "s%dr%dc%d", bits, rate, chans);

		flushout();
		if(pipe(pfd) < 0){
			fprintf(stderr, "Error creating pipe\n");
			exit(1);
		}
		pid = fork();
		if(pid < 0){
			fprintf(stderr, "Error forking\n");
			exit(1);
		}
		if(pid == 0){
			dup2(pfd[1], 0);
			close(pfd[1]);
			close(pfd[0]);
			execl("/bin/audio/pcmconv", "pcmconv", "-i", fmt, NULL);
			fprintf(stderr, "Error executing converter\n");
			exit(1);
		}
		close(pfd[1]);
		ifd = pfd[0];
		atexit(flushout);
	}
	len = frame->header.blocksize;
	b = (bits+7)/8;
	n = b * chans * len;
	if(n > nbuf){
		nbuf = n;
		buf = realloc(buf, nbuf);
		if(buf == NULL){
			fprintf(stderr, "Error allocating memory\n");
			exit(1);
		}
	}
	p = buf;
	for(j=0; j < chans; j++){
		s = buffer[j];
		p = buf + j*b;
		for(i=0; i < len; i++){
			n = 0;
			v = *s++;
			switch(b){
			case 4:
				p[n++] = v, v>>=8;
			case 3:
				p[n++] = v, v>>=8;
			case 2:
				p[n++] = v, v>>=8;
			case 1:
				p[n] = v;
			}
			p += chans*b;
		}
	}
	n = b * chans * len;
	if(n > 0 && write(ifd, buf, n) != n)
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;

	return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}

static void
decerror(FLAC__StreamDecoder *dec, FLAC__StreamDecoderErrorStatus status, void *client_data)
{
	fprintf(stderr, "decode error: %s (%d)\n", FLAC__StreamDecoderErrorStatusString[status], status);
}

static FLAC__bool
checkeof(const FLAC__StreamDecoder*, void*)
{
	return feof(stdin);
}

void
usage(void)
{
	fprintf(stderr, "usage: %s [-s SECONDS]\n", argv0);
	exit(1);
}

int main(int argc, char *argv[])
{
	FLAC__bool ok = true;
	FLAC__StreamDecoder *dec = 0;
	double seek;

	seek = 0.0;
	ARGBEGIN{
	case 's':
		seek = atof(EARGF(usage()));
		if(seek >= 0.0)
			break;
	default:
		usage();
	}ARGEND

	dec = FLAC__stream_decoder_new();
	FLAC__stream_decoder_init_stream(dec, decinput, decseek, dectell, declen, checkeof, decoutput, NULL, decerror, NULL);
	if(seek > 0.0){
		FLAC__uint64 srate;
		do{
			if(!FLAC__stream_decoder_process_single(dec))
				break;
			srate = FLAC__stream_decoder_get_sample_rate(dec);
		}while(srate == 0);
		if(!FLAC__stream_decoder_seek_absolute(dec, srate*seek)){
			FLAC__stream_decoder_flush(dec);
			seek = 0.0;
		}
		fprintf(stderr, "time: %g\n", seek);
	}
	FLAC__stream_decoder_process_until_end_of_stream(dec);
	FLAC__stream_decoder_finish(dec);
	return 0;
}