-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplayer_flac.c
More file actions
180 lines (153 loc) · 4.28 KB
/
Copy pathplayer_flac.c
File metadata and controls
180 lines (153 loc) · 4.28 KB
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
/*
* Copyright (c) 2022, 2025 Omar Polo <op@omarpolo.com>
*
* 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 <inttypes.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <FLAC/stream_decoder.h>
#include "log.h"
#include "player.h"
struct write_args {
FLAC__StreamDecoder *decoder;
int seek_failed;
};
static int
sample_seek(struct write_args *wa, int64_t seek)
{
int ok;
ok = FLAC__stream_decoder_seek_absolute(wa->decoder, seek);
if (ok)
player_setpos(seek);
else
wa->seek_failed = 1;
return ok;
}
static FLAC__StreamDecoderWriteStatus
writecb(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame,
const int32_t * const *src, void *data)
{
struct write_args *wa = data;
static uint8_t buf[AMUSED_BUFSIZ];
int64_t seek;
int c, i, bps, chans;
size_t len;
bps = frame->header.bits_per_sample;
chans = frame->header.channels;
for (i = 0, len = 0; i < frame->header.blocksize; ++i) {
if (len + 4*chans >= sizeof(buf)) {
if (!play(buf, len, &seek))
goto quit;
if (seek != -1) {
if (sample_seek(wa, seek))
break;
else
goto quit;
}
len = 0;
}
for (c = 0; c < chans; ++c) {
switch (bps) {
case 8:
buf[len++] = src[c][i] & 0xff;
break;
case 16:
buf[len++] = src[c][i] & 0xff;
buf[len++] = (src[c][i] >> 8) & 0xff;
break;
case 24:
case 32:
buf[len++] = src[c][i] & 0xff;
buf[len++] = (src[c][i] >> 8) & 0xff;
buf[len++] = (src[c][i] >> 16) & 0xff;
buf[len++] = (src[c][i] >> 24) & 0xff;
break;
default:
log_warnx("unsupported flac bps=%d", bps);
goto quit;
}
}
}
if (len != 0 && !play(buf, len, &seek))
goto quit;
if (seek != -1 && !sample_seek(wa, seek))
goto quit;
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
quit:
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
static void
metacb(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *meta,
void *d)
{
uint32_t sample_rate;
int channels, bits;
if (meta->type == FLAC__METADATA_TYPE_STREAMINFO) {
bits = meta->data.stream_info.bits_per_sample;
sample_rate = meta->data.stream_info.sample_rate;
channels = meta->data.stream_info.channels;
if (player_setup(bits, sample_rate, channels) == -1)
fatal("player_setup");
player_setduration(meta->data.stream_info.total_samples);
}
}
static void
errcb(const FLAC__StreamDecoder *decoder,
FLAC__StreamDecoderErrorStatus status, void *data)
{
log_warnx("flac error: %s",
FLAC__StreamDecoderErrorStatusString[status]);
}
int
play_flac(int fd, const char **errstr)
{
FILE *f;
struct write_args wa;
int s, ok = 1;
FLAC__StreamDecoder *decoder = NULL;
FLAC__StreamDecoderInitStatus init_status;
if ((f = fdopen(fd, "r")) == NULL) {
*errstr = "fdopen failed";
close(fd);
return -1;
}
decoder = FLAC__stream_decoder_new();
if (decoder == NULL) {
*errstr = "FLAC__stream_decoder_new() failed";
fclose(f);
return -1;
}
FLAC__stream_decoder_set_md5_checking(decoder, 1);
memset(&wa, 0, sizeof(wa));
wa.decoder = decoder;
init_status = FLAC__stream_decoder_init_FILE(decoder, f, writecb,
metacb, errcb, &wa);
if (init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK)
fatalx("flac decoder: %s",
FLAC__StreamDecoderInitStatusString[init_status]);
ok = FLAC__stream_decoder_process_until_end_of_stream(decoder);
s = FLAC__stream_decoder_get_state(decoder);
FLAC__stream_decoder_delete(decoder);
if (s == FLAC__STREAM_DECODER_ABORTED && !wa.seek_failed)
return 1;
else if (!ok && !wa.seek_failed) {
*errstr = "flac decoding error";
return -1;
} else
return 0;
}