File: wavpack.c

package info (click to toggle)
cmus 2.7.1%2Bgit20160225-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,848 kB
  • sloc: ansic: 35,866; sh: 1,548; makefile: 260; python: 159
file content (433 lines) | stat: -rw-r--r-- 9,752 bytes parent folder | download | duplicates (2)
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
 * Copyright 2008-2013 Various Authors
 * Copyright 2007 Johannes Weißl
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include "ip.h"
#include "ape.h"
#include "id3.h"
#include "xmalloc.h"
#include "read_wrapper.h"
#include "debug.h"
#include "buffer.h"
#include "comment.h"

#include <wavpack/wavpack.h>

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

#define WV_CHANNEL_MAX 2

struct wavpack_file {
	int fd;
	off_t len;
	int push_back_byte;
};

struct wavpack_private {
	WavpackContext *wpc;
	int32_t samples[CHUNK_SIZE * WV_CHANNEL_MAX];
	struct wavpack_file wv_file;
	struct wavpack_file wvc_file;
	unsigned int has_wvc : 1;
};

/* http://www.wavpack.com/lib_use.txt */

static int32_t read_bytes(void *data, void *ptr, int32_t count)
{
	struct wavpack_file *file = data;
	int32_t rc, n = 0;

	if (file->push_back_byte != EOF) {
		char *p = ptr;
		*p = (char) file->push_back_byte;
		ptr = p + 1;
		file->push_back_byte = EOF;
		count--;
		n++;
	}

	rc = read(file->fd, ptr, count);
	if (rc == -1) {
		d_print("error: %s\n", strerror(errno));
		return 0;
	}
	if (rc == 0) {
		errno = 0;
		return 0;
	}
	return rc + n;
}

static uint32_t get_pos(void *data)
{
	struct wavpack_file *file = data;

	return lseek(file->fd, 0, SEEK_CUR);
}

static int set_pos_rel(void *data, int32_t delta, int mode)
{
	struct wavpack_file *file = data;

	if (lseek(file->fd, delta, mode) == -1)
		return -1;

	file->push_back_byte = EOF;
	return 0;
}

static int set_pos_abs(void *data, uint32_t pos)
{
	return set_pos_rel(data, pos, SEEK_SET);
}

static int push_back_byte(void *data, int c)
{
	struct wavpack_file *file = data;

	if (file->push_back_byte != EOF) {
		d_print("error: only one byte push back possible!\n");
		return EOF;
	}
	file->push_back_byte = c;
	return c;
}

static uint32_t get_length(void *data)
{
	struct wavpack_file *file = data;
	return file->len;
}

static int can_seek(void *data)
{
	struct wavpack_file *file = data;
	return file->len != -1;
}

static int32_t write_bytes(void *data, void *ptr, int32_t count)
{
	/* we shall not write any bytes */
	return 0;
}


/*
 * typedef struct {
 *     int32_t (*read_bytes)(void *id, void *data, int32_t bcount);
 *     uint32_t (*get_pos)(void *id);
 *     int (*set_pos_abs)(void *id, uint32_t pos);
 *     int (*set_pos_rel)(void *id, int32_t delta, int mode);
 *     int (*push_back_byte)(void *id, int c);
 *     uint32_t (*get_length)(void *id);
 *     int (*can_seek)(void *id);
 *
 *     // this callback is for writing edited tags only
 *     int32_t (*write_bytes)(void *id, void *data, int32_t bcount);
 * } WavpackStreamReader;
 */
static WavpackStreamReader callbacks = {
	.read_bytes = read_bytes,
	.get_pos = get_pos,
	.set_pos_abs = set_pos_abs,
	.set_pos_rel = set_pos_rel,
	.push_back_byte = push_back_byte,
	.get_length = get_length,
	.can_seek = can_seek,
	.write_bytes = write_bytes
};

static int wavpack_open(struct input_plugin_data *ip_data)
{
	struct wavpack_private *priv;
	struct stat st;
	char msg[80];
	int channel_mask = 0;

	const struct wavpack_private priv_init = {
		.wv_file = {
			.fd = ip_data->fd,
			.push_back_byte = EOF
		}
	};

	priv = xnew(struct wavpack_private, 1);
	*priv = priv_init;
	if (!ip_data->remote && fstat(ip_data->fd, &st) == 0) {
		char *filename_wvc;

		priv->wv_file.len = st.st_size;

		filename_wvc = xnew(char, strlen(ip_data->filename) + 2);
		sprintf(filename_wvc, "%sc", ip_data->filename);
		if (stat(filename_wvc, &st) == 0) {
			priv->wvc_file.fd = open(filename_wvc, O_RDONLY);
			if (priv->wvc_file.fd != -1) {
				priv->wvc_file.len = st.st_size;
				priv->wvc_file.push_back_byte = EOF;
				priv->has_wvc = 1;
				d_print("use correction file: %s\n", filename_wvc);
			}
		}
		free(filename_wvc);
	} else
		priv->wv_file.len = -1;
	ip_data->private = priv;

	*msg = '\0';

	priv->wpc = WavpackOpenFileInputEx(&callbacks, &priv->wv_file,
			priv->has_wvc ? &priv->wvc_file : NULL, msg,
			OPEN_NORMALIZE, 0);

	if (!priv->wpc) {
		d_print("WavpackOpenFileInputEx failed: %s\n", msg);
		free(priv);
		return -IP_ERROR_FILE_FORMAT;
	}

	ip_data->sf = sf_rate(WavpackGetSampleRate(priv->wpc))
		| sf_channels(WavpackGetReducedChannels(priv->wpc))
		| sf_bits(WavpackGetBitsPerSample(priv->wpc))
		| sf_signed(1);
#if CUR_STREAM_VERS > 0x404
	channel_mask = WavpackGetChannelMask(priv->wpc);
#endif
	channel_map_init_waveex(sf_get_channels(ip_data->sf), channel_mask, ip_data->channel_map);
	return 0;
}

static int wavpack_close(struct input_plugin_data *ip_data)
{
	struct wavpack_private *priv;

	priv = ip_data->private;
	priv->wpc = WavpackCloseFile(priv->wpc);
	if (priv->has_wvc)
		close(priv->wvc_file.fd);
	free(priv);
	ip_data->private = NULL;
	return 0;
}

/* from wv_engine.cpp (C) 2006 by Peter Lemenkov <lemenkov@newmail.ru> */
static char *format_samples(int bps, char *dst, int32_t *src, uint32_t count)
{
	int32_t temp;

	switch (bps) {
	case 1:
		while (count--)
			*dst++ = *src++ + 128;
		break;
	case 2:
		while (count--) {
			*dst++ = (char) (temp = *src++);
			*dst++ = (char) (temp >> 8);
		}
		break;
	case 3:
		while (count--) {
			*dst++ = (char) (temp = *src++);
			*dst++ = (char) (temp >> 8);
			*dst++ = (char) (temp >> 16);
		}
		break;
	case 4:
		while (count--) {
			*dst++ = (char) (temp = *src++);
			*dst++ = (char) (temp >> 8);
			*dst++ = (char) (temp >> 16);
			*dst++ = (char) (temp >> 24);
		}
		break;
	}

	return dst;
}

static int wavpack_read(struct input_plugin_data *ip_data, char *buffer, int count)
{
	struct wavpack_private *priv;
	int rc, bps, sample_count, channels;

	priv = ip_data->private;
	channels = sf_get_channels(ip_data->sf);
	bps = WavpackGetBytesPerSample(priv->wpc);

	sample_count = count / bps;

	rc = WavpackUnpackSamples(priv->wpc, priv->samples, sample_count / channels);
	format_samples(bps, buffer, priv->samples, rc * channels);
	return rc * channels * bps;
}

static int wavpack_seek(struct input_plugin_data *ip_data, double offset)
{
	struct wavpack_private *priv = ip_data->private;

	if (!WavpackSeekSample(priv->wpc, WavpackGetSampleRate(priv->wpc) * offset))
		return -IP_ERROR_INTERNAL;
	return 0;
}

static int wavpack_read_comments(struct input_plugin_data *ip_data,
		struct keyval **comments)
{
	struct id3tag id3;
	APETAG(ape);
	GROWING_KEYVALS(c);
	int fd, rc, save, i;

	fd = open(ip_data->filename, O_RDONLY);
	if (fd == -1)
		return -1;
	d_print("filename: %s\n", ip_data->filename);

	id3_init(&id3);
	rc = id3_read_tags(&id3, fd, ID3_V1);
	save = errno;
	close(fd);
	errno = save;
	if (rc) {
		if (rc == -1) {
			d_print("error: %s\n", strerror(errno));
			return -1;
		}
		d_print("corrupted tag?\n");
		goto next;
	}

	for (i = 0; i < NUM_ID3_KEYS; i++) {
		char *val = id3_get_comment(&id3, i);
		if (val)
			comments_add(&c, id3_key_names[i], val);
	}

next:
	id3_free(&id3);

	rc = ape_read_tags(&ape, ip_data->fd, 1);
	if (rc < 0)
		goto out;

	for (i = 0; i < rc; i++) {
		char *k, *v;
		k = ape_get_comment(&ape, &v);
		if (!k)
			break;
		comments_add(&c, k, v);
		free(k);
	}

out:
	ape_free(&ape);

	keyvals_terminate(&c);
	*comments = c.keyvals;
	return 0;
}

static int wavpack_duration(struct input_plugin_data *ip_data)
{
	struct wavpack_private *priv;
	int duration;

	priv = ip_data->private;
	duration = WavpackGetNumSamples(priv->wpc) /
		WavpackGetSampleRate(priv->wpc);

	return duration;
}

static long wavpack_bitrate(struct input_plugin_data *ip_data)
{
	struct wavpack_private *priv = ip_data->private;
	double bitrate = WavpackGetAverageBitrate(priv->wpc, 1);
	if (!bitrate)
		return -IP_ERROR_FUNCTION_NOT_SUPPORTED;
	return (long) (bitrate + 0.5);
}

static long wavpack_current_bitrate(struct input_plugin_data *ip_data)
{
	struct wavpack_private *priv = ip_data->private;
	return WavpackGetInstantBitrate(priv->wpc);
}

static char *wavpack_codec(struct input_plugin_data *ip_data)
{
	return xstrdup("wavpack");
}

static char *wavpack_codec_profile(struct input_plugin_data *ip_data)
{
	struct wavpack_private *priv = ip_data->private;
	int m = WavpackGetMode(priv->wpc);
	char buf[32];

	buf[0] = '\0';

	if (m & MODE_FAST)
		strcat(buf, "fast");
#ifdef MODE_VERY_HIGH
	else if (m & MODE_VERY_HIGH)
		strcat(buf, "very high");
#endif
	else if (m & MODE_HIGH)
		strcat(buf, "high");
	else
		strcat(buf, "normal");

	if (m & MODE_HYBRID)
		strcat(buf, " hybrid");

#ifdef MODE_XMODE
	if ((m & MODE_EXTRA) && (m & MODE_XMODE)) {
		char xmode[] = " x0";
		xmode[2] = ((m & MODE_XMODE) >> 12) + '0';
		strcat(buf, xmode);
	}
#endif

	return xstrdup(buf);
}

const struct input_plugin_ops ip_ops = {
	.open = wavpack_open,
	.close = wavpack_close,
	.read = wavpack_read,
	.seek = wavpack_seek,
	.read_comments = wavpack_read_comments,
	.duration = wavpack_duration,
	.bitrate = wavpack_bitrate,
	.bitrate_current = wavpack_current_bitrate,
	.codec = wavpack_codec,
	.codec_profile = wavpack_codec_profile
};

const int ip_priority = 50;
const char * const ip_extensions[] = { "wv", NULL };
const char * const ip_mime_types[] = { "audio/x-wavpack", NULL };
const char * const ip_options[] = { NULL };