• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Pipewireパッケージ(ちょっと変更)


Commit MetaInfo

Revisiónbe1a60c5f9c71577cff087bdbc4f98bb3e3646f5 (tree)
Tiempo2023-10-02 23:51:37
AutorWim Taymans <wtaymans@redh...>
CommiterWim Taymans

Log Message

pw-cat: add DFF file suppport

Cambiar Resumen

Diferencia incremental

--- /dev/null
+++ b/src/tools/dfffile.c
@@ -0,0 +1,313 @@
1+/* PipeWire */
2+/* SPDX-FileCopyrightText: Copyright © 2021 Wim Taymans */
3+/* SPDX-License-Identifier: MIT */
4+
5+#include <errno.h>
6+#include <sys/mman.h>
7+#include <sys/stat.h>
8+#include <unistd.h>
9+#include <fcntl.h>
10+#include <math.h>
11+
12+#include <spa/utils/string.h>
13+#include <spa/debug/mem.h>
14+
15+#include "dfffile.h"
16+
17+struct dff_file {
18+ uint8_t *data;
19+ size_t size;
20+
21+ int mode;
22+ int fd;
23+
24+ struct dff_file_info info;
25+
26+ uint8_t *p;
27+ size_t offset;
28+};
29+
30+struct dff_chunk {
31+ uint32_t id;
32+ uint64_t size;
33+ void *data;
34+};
35+
36+#define FOURCC(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
37+
38+static inline uint16_t parse_be16(const uint8_t *in)
39+{
40+ return (in[0] << 8) | in[1];
41+}
42+static inline uint32_t parse_be32(const uint8_t *in)
43+{
44+ return FOURCC(in[0], in[1], in[2], in[3]);
45+}
46+
47+static inline uint64_t parse_be64(const uint8_t *in)
48+{
49+ uint64_t res = in[7];
50+ res |= ((uint64_t)in[6]) << 8;
51+ res |= ((uint64_t)in[5]) << 16;
52+ res |= ((uint64_t)in[4]) << 24;
53+ res |= ((uint64_t)in[3]) << 32;
54+ res |= ((uint64_t)in[2]) << 40;
55+ res |= ((uint64_t)in[1]) << 48;
56+ res |= ((uint64_t)in[0]) << 56;
57+ return res;
58+}
59+
60+static inline int f_avail(struct dff_file *f)
61+{
62+ if (f->p < f->data + f->size)
63+ return f->size + f->data - f->p;
64+ return 0;
65+}
66+
67+static int read_chunk(struct dff_file *f, struct dff_chunk *c)
68+{
69+ if (f_avail(f) < 12)
70+ return -ENOSPC;
71+
72+ c->id = parse_be32(f->p); /* id of this chunk */
73+ c->size = parse_be64(f->p + 4); /* size of this chunk */
74+ f->p += 12;
75+ c->data = f->p;
76+ return 0;
77+}
78+
79+static int skip_chunk(struct dff_file *f, const struct dff_chunk *c)
80+{
81+ f->p = SPA_PTROFF(c->data, c->size, uint8_t);
82+ return 0;
83+}
84+
85+static int read_PROP(struct dff_file *f, struct dff_chunk *prop)
86+{
87+ struct dff_chunk c[1];
88+ int res;
89+
90+ if (f_avail(f) < 4 ||
91+ memcmp(prop->data, "SND ", 4) != 0)
92+ return -EINVAL;
93+ f->p += 4;
94+
95+ while (f->p < SPA_PTROFF(prop->data, prop->size, uint8_t)) {
96+ if ((res = read_chunk(f, &c[0])) < 0)
97+ return res;
98+
99+ switch (c[0].id) {
100+ case FOURCC('F', 'S', ' ', ' '):
101+ f->info.rate = parse_be32(f->p);
102+ break;
103+ case FOURCC('C', 'H', 'N', 'L'):
104+ f->info.channels = parse_be16(f->p);
105+ switch (f->info.channels) {
106+ case 2:
107+ f->info.channel_type = 2;
108+ break;
109+ case 5:
110+ f->info.channel_type = 6;
111+ break;
112+ case 6:
113+ f->info.channel_type = 7;
114+ break;
115+ }
116+ break;
117+ case FOURCC('C', 'M', 'P', 'R'):
118+ {
119+ uint32_t cmpr = parse_be32(f->p);
120+ if (cmpr != FOURCC('D', 'S', 'D', ' '))
121+ return -ENOTSUP;
122+ break;
123+ }
124+ case FOURCC('A', 'B', 'S', 'S'):
125+ break;
126+ case FOURCC('L', 'S', 'C', 'O'):
127+ break;
128+ default:
129+ break;
130+ }
131+ skip_chunk(f, &c[0]);
132+ }
133+ return 0;
134+}
135+
136+static int read_FRM8(struct dff_file *f)
137+{
138+ struct dff_chunk c[2];
139+ int res;
140+ bool found_dsd = false;
141+
142+ if ((res = read_chunk(f, &c[0])) < 0)
143+ return res;
144+ if (c[0].id != FOURCC('F','R','M','8'))
145+ return -EINVAL;
146+ if (f_avail(f) < 4 ||
147+ memcmp(c[0].data, "DSD ", 4) != 0)
148+ return -EINVAL;
149+ f->p += 4;
150+
151+ while (true) {
152+ if ((res = read_chunk(f, &c[1])) < 0)
153+ return res;
154+
155+ switch (c[1].id) {
156+ case FOURCC('F', 'V', 'E', 'R'):
157+ break;
158+ case FOURCC('P', 'R', 'O', 'P'):
159+ read_PROP(f, &c[1]);
160+ break;
161+ case FOURCC('D', 'S', 'D', ' '):
162+ {
163+ f->info.length = c[1].size;
164+ f->info.samples = c[1].size / f->info.channels;
165+ f->info.lsb = 0;
166+ f->info.blocksize = 1;
167+ found_dsd = true;
168+ break;
169+ }
170+ default:
171+ break;
172+ }
173+ if (found_dsd)
174+ break;
175+
176+ skip_chunk(f, &c[1]);
177+ }
178+ return 0;
179+}
180+
181+static int open_read(struct dff_file *f, const char *filename, struct dff_file_info *info)
182+{
183+ int res;
184+ struct stat st;
185+
186+ if ((f->fd = open(filename, O_RDONLY)) < 0) {
187+ res = -errno;
188+ goto exit;
189+ }
190+ if (fstat(f->fd, &st) < 0) {
191+ res = -errno;
192+ goto exit_close;
193+ }
194+ f->size = st.st_size;
195+
196+ f->data = mmap(NULL, f->size, PROT_READ, MAP_SHARED, f->fd, 0);
197+ if (f->data == MAP_FAILED) {
198+ res = -errno;
199+ goto exit_close;
200+ }
201+
202+ f->p = f->data;
203+
204+ if ((res = read_FRM8(f)) < 0)
205+ goto exit_unmap;
206+
207+ f->mode = 1;
208+ *info = f->info;
209+ return 0;
210+
211+exit_unmap:
212+ munmap(f->data, f->size);
213+exit_close:
214+ close(f->fd);
215+exit:
216+ return res;
217+}
218+
219+struct dff_file *
220+dff_file_open(const char *filename, const char *mode, struct dff_file_info *info)
221+{
222+ int res;
223+ struct dff_file *f;
224+
225+ f = calloc(1, sizeof(struct dff_file));
226+ if (f == NULL)
227+ return NULL;
228+
229+ if (spa_streq(mode, "r")) {
230+ if ((res = open_read(f, filename, info)) < 0)
231+ goto exit_free;
232+ } else {
233+ res = -EINVAL;
234+ goto exit_free;
235+ }
236+ return f;
237+
238+exit_free:
239+ free(f);
240+ errno = -res;
241+ return NULL;
242+}
243+
244+static const uint8_t bitrev[256] = {
245+ 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
246+ 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8, 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
247+ 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4, 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
248+ 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec, 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
249+ 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2, 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
250+ 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea, 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
251+ 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6, 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
252+ 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee, 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
253+ 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1, 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
254+ 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9, 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
255+ 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5, 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
256+ 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed, 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
257+ 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3, 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
258+ 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb, 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
259+ 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7, 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
260+ 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff,
261+};
262+
263+ssize_t
264+dff_file_read(struct dff_file *f, void *data, size_t samples, const struct dff_layout *layout)
265+{
266+ uint8_t *d = data;
267+ int32_t step = SPA_ABS(layout->interleave);
268+ uint32_t channels = f->info.channels;
269+ bool rev = layout->lsb != f->info.lsb;
270+ size_t total, offset, scale;
271+
272+ offset = f->offset;
273+ scale = SPA_CLAMP(f->info.rate / (44100u * 64u), 1u, 4u);
274+
275+ samples *= step;
276+ samples *= scale;
277+
278+ for (total = 0; total < samples && offset < f->info.length; total++) {
279+ uint32_t i;
280+ int32_t j;
281+ const uint8_t *s = f->p + offset;
282+
283+ for (i = 0; i < layout->channels; i++) {
284+ if (layout->interleave > 0) {
285+ for (j = 0; j < step; j++)
286+ *d++ = rev ?
287+ bitrev[s[j * channels + i]] :
288+ s[j * channels + i];
289+ } else {
290+ for (j = step-1; j >= 0; j--)
291+ *d++ = rev ?
292+ bitrev[s[j * channels + i]] :
293+ s[j * channels + i];
294+ }
295+ }
296+ offset += step * channels;
297+ }
298+ f->offset = offset;
299+
300+ return total;
301+}
302+
303+int dff_file_close(struct dff_file *f)
304+{
305+ if (f->mode == 1) {
306+ munmap(f->data, f->size);
307+ } else
308+ return -EINVAL;
309+
310+ close(f->fd);
311+ free(f);
312+ return 0;
313+}
--- /dev/null
+++ b/src/tools/dfffile.h
@@ -0,0 +1,31 @@
1+/* PipeWire */
2+/* SPDX-FileCopyrightText: Copyright © 2023 Wim Taymans */
3+/* SPDX-License-Identifier: MIT */
4+
5+#include <stdio.h>
6+
7+#include <spa/utils/defs.h>
8+
9+struct dff_file;
10+
11+struct dff_file_info {
12+ uint32_t channel_type;
13+ uint32_t channels;
14+ uint32_t rate;
15+ bool lsb;
16+ uint64_t samples;
17+ uint64_t length;
18+ uint32_t blocksize;
19+};
20+
21+struct dff_layout {
22+ int32_t interleave;
23+ uint32_t channels;
24+ bool lsb;
25+};
26+
27+struct dff_file * dff_file_open(const char *filename, const char *mode, struct dff_file_info *info);
28+
29+ssize_t dff_file_read(struct dff_file *f, void *data, size_t samples, const struct dff_layout *layout);
30+
31+int dff_file_close(struct dff_file *f);
--- a/src/tools/meson.build
+++ b/src/tools/meson.build
@@ -47,6 +47,7 @@ if get_option('pw-cat').allowed() and sndfile_dep.found()
4747 pwcat_sources = [
4848 'pw-cat.c',
4949 'midifile.c',
50+ 'dfffile.c',
5051 'dsffile.c',
5152 ]
5253
--- a/src/tools/pw-cat.c
+++ b/src/tools/pw-cat.c
@@ -41,6 +41,7 @@
4141 #endif
4242
4343 #include "midifile.h"
44+#include "dfffile.h"
4445 #include "dsffile.h"
4546
4647 #define DEFAULT_MEDIA_TYPE "Audio"
@@ -143,6 +144,11 @@ struct data {
143144 struct dsf_file_info info;
144145 struct dsf_layout layout;
145146 } dsf;
147+ struct {
148+ struct dff_file *file;
149+ struct dff_file_info info;
150+ struct dff_layout layout;
151+ } dff;
146152
147153 #ifdef HAVE_PW_CAT_FFMPEG_INTEGRATION
148154 struct {
@@ -798,6 +804,10 @@ on_param_changed(void *userdata, uint32_t id, const struct spa_pod *param)
798804 data->dsf.layout.channels = info.info.dsd.channels;
799805 data->dsf.layout.lsb = info.info.dsd.bitorder == SPA_PARAM_BITORDER_lsb;
800806
807+ data->dff.layout.interleave = info.info.dsd.interleave,
808+ data->dff.layout.channels = info.info.dsd.channels;
809+ data->dff.layout.lsb = info.info.dsd.bitorder == SPA_PARAM_BITORDER_lsb;
810+
801811 data->stride = data->dsf.layout.channels * SPA_ABS(data->dsf.layout.interleave);
802812
803813 if (data->verbose) {
@@ -1164,26 +1174,46 @@ static int dsf_play(struct data *d, void *src, unsigned int n_frames, bool *null
11641174 return dsf_file_read(d->dsf.file, src, n_frames, &d->dsf.layout);
11651175 }
11661176
1167-static int setup_dsffile(struct data *data)
1177+static int dff_play(struct data *d, void *src, unsigned int n_frames, bool *null_frame)
1178+{
1179+ return dff_file_read(d->dff.file, src, n_frames, &d->dff.layout);
1180+}
1181+
1182+static int setup_dsdfile(struct data *data)
11681183 {
11691184 if (data->mode == mode_record)
11701185 return -ENOTSUP;
11711186
11721187 data->dsf.file = dsf_file_open(data->filename, "r", &data->dsf.info);
11731188 if (data->dsf.file == NULL) {
1174- fprintf(stderr, "dsffile: can't read dsf file '%s': %m\n", data->filename);
1175- return -errno;
1189+ data->dff.file = dff_file_open(data->filename, "r", &data->dff.info);
1190+ if (data->dff.file == NULL) {
1191+ fprintf(stderr, "dsdfile: can't read dsd file '%s': %m\n", data->filename);
1192+ return -errno;
1193+ }
11761194 }
11771195
1178- if (data->verbose)
1179- printf("dsffile: opened file \"%s\" channels:%d rate:%d samples:%"PRIu64" bitorder:%s\n",
1196+ if (data->dsf.file != NULL) {
1197+ if (data->verbose)
1198+ printf("dsffile: opened file \"%s\" channels:%d rate:%d "
1199+ "samples:%"PRIu64" bitorder:%s\n",
11801200 data->filename,
11811201 data->dsf.info.channels, data->dsf.info.rate,
11821202 data->dsf.info.samples,
11831203 data->dsf.info.lsb ? "lsb" : "msb");
11841204
1185- data->fill = dsf_play;
1205+ data->fill = dsf_play;
1206+ } else {
1207+ if (data->verbose)
1208+ printf("dfffile: opened file \"%s\" channels:%d rate:%d "
1209+ "samples:%"PRIu64" bitorder:%s\n",
1210+ data->filename,
1211+ data->dff.info.channels, data->dff.info.rate,
1212+ data->dff.info.samples,
1213+ data->dff.info.lsb ? "lsb" : "msb");
11861214
1215+ data->fill = dff_play;
1216+ }
11871217 return 0;
11881218 }
11891219
@@ -1839,7 +1869,7 @@ int main(int argc, char *argv[])
18391869 ret = setup_midifile(&data);
18401870 break;
18411871 case TYPE_DSD:
1842- ret = setup_dsffile(&data);
1872+ ret = setup_dsdfile(&data);
18431873 break;
18441874 #ifdef HAVE_PW_CAT_FFMPEG_INTEGRATION
18451875 case TYPE_ENCODED:
@@ -1918,13 +1948,21 @@ int main(int argc, char *argv[])
19181948 case TYPE_DSD:
19191949 {
19201950 struct spa_audio_info_dsd info;
1951+ uint32_t channel_type;
19211952
19221953 spa_zero(info);
1923- info.channels = data.dsf.info.channels;
1924- info.rate = data.dsf.info.rate / 8;
1954+ if (data.dsf.file != NULL) {
1955+ info.channels = data.dsf.info.channels;
1956+ info.rate = data.dsf.info.rate / 8;
1957+ channel_type = data.dsf.info.channel_type;
1958+ } else {
1959+ info.channels = data.dff.info.channels;
1960+ info.rate = data.dff.info.rate / 8;
1961+ channel_type = data.dff.info.channel_type;
1962+ }
19251963
19261964 SPA_FOR_EACH_ELEMENT_VAR(dsd_layouts, i) {
1927- if (i->type != data.dsf.info.channel_type)
1965+ if (i->type != channel_type)
19281966 continue;
19291967 info.channels = i->info.n_channels;
19301968 memcpy(info.position, i->info.position,
@@ -2020,6 +2058,10 @@ error_no_main_loop:
20202058 sf_close(data.file);
20212059 if (data.midi.file)
20222060 midi_file_close(data.midi.file);
2061+ if (data.dsf.file)
2062+ dsf_file_close(data.dsf.file);
2063+ if (data.dff.file)
2064+ dff_file_close(data.dff.file);
20232065 #ifdef HAVE_PW_CAT_FFMPEG_INTEGRATION
20242066 if (data.encoded.packet)
20252067 av_packet_free(&data.encoded.packet);