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
|
/*
* Copyright 2008-2013 Various Authors
* Copyright 2004-2005 Timo Hirvonen
*
* 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 "file.h"
#include "xmalloc.h"
#include "debug.h"
#include "utils.h"
#include "comment.h"
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#define WAVE_FORMAT_PCM 0x0001U
#define WAVE_FORMAT_EXTENSIBLE 0xfffeU
#define WAVE_WRONG_HEADER 1
struct wav_private {
off_t pcm_start;
unsigned int pcm_size;
unsigned int pos;
/* size of one second of data */
unsigned int sec_size;
unsigned int frame_size;
};
static int read_chunk_header(int fd, char *name, unsigned int *size)
{
int rc;
char buf[8];
rc = read_all(fd, buf, 8);
if (rc == -1)
return -IP_ERROR_ERRNO;
if (rc != 8)
return -IP_ERROR_FILE_FORMAT;
*size = read_le32(buf + 4);
memmove(name, buf, 4);
return 0;
}
static int read_named_chunk_header(int fd, const char *name, unsigned int *size)
{
int rc;
char buf[4];
rc = read_chunk_header(fd, buf, size);
if (rc)
return rc;
if (memcmp(buf, name, 4))
return WAVE_WRONG_HEADER;
return 0;
}
static int find_chunk(int fd, const char *name, unsigned int *size)
{
int rc;
do {
rc = read_named_chunk_header(fd, name, size);
if (rc != WAVE_WRONG_HEADER)
return rc;
d_print("seeking %u\n", *size);
if (lseek(fd, *size, SEEK_CUR) == -1) {
d_print("seek failed\n");
return -IP_ERROR_ERRNO;
}
} while (1);
}
static int wav_open(struct input_plugin_data *ip_data)
{
struct wav_private *priv;
char buf[4];
char *fmt;
int rc;
unsigned int riff_size, fmt_size;
int save;
d_print("file: %s\n", ip_data->filename);
priv = xnew(struct wav_private, 1);
ip_data->private = priv;
rc = read_named_chunk_header(ip_data->fd, "RIFF", &riff_size);
if (rc == WAVE_WRONG_HEADER)
rc = -IP_ERROR_FILE_FORMAT;
if (rc)
goto error_exit;
rc = read_all(ip_data->fd, buf, 4);
if (rc == -1) {
rc = -IP_ERROR_ERRNO;
goto error_exit;
}
if (rc != 4 || memcmp(buf, "WAVE", 4) != 0) {
rc = -IP_ERROR_FILE_FORMAT;
goto error_exit;
}
rc = find_chunk(ip_data->fd, "fmt ", &fmt_size);
if (rc)
goto error_exit;
if (fmt_size < 16) {
d_print("size of \"fmt \" chunk is invalid (%u)\n", fmt_size);
rc = -IP_ERROR_FILE_FORMAT;
goto error_exit;
}
fmt = xnew(char, fmt_size);
rc = read_all(ip_data->fd, fmt, fmt_size);
if (rc == -1) {
save = errno;
free(fmt);
errno = save;
rc = -IP_ERROR_ERRNO;
goto error_exit;
}
if (rc != fmt_size) {
save = errno;
free(fmt);
errno = save;
rc = -IP_ERROR_FILE_FORMAT;
goto error_exit;
}
{
unsigned int format_tag, channels, rate, bits, channel_mask = 0;
format_tag = read_le16(fmt + 0);
channels = read_le16(fmt + 2);
rate = read_le32(fmt + 4);
/* 4 bytes, bytes per second */
/* 2 bytes, bytes per sample */
bits = read_le16(fmt + 14);
if (format_tag == WAVE_FORMAT_EXTENSIBLE) {
unsigned int ext_size, valid_bits;
if (fmt_size < 18) {
free(fmt);
d_print("size of \"fmt \" chunk is invalid (%u)\n", fmt_size);
rc = -IP_ERROR_FILE_FORMAT;
goto error_exit;
}
ext_size = read_le16(fmt + 16);
if (ext_size < 22) {
free(fmt);
d_print("size of \"fmt \" chunk extension is invalid (%u)\n", ext_size);
rc = -IP_ERROR_FILE_FORMAT;
goto error_exit;
}
valid_bits = read_le16(fmt + 18);
if (valid_bits != bits) {
free(fmt);
d_print("padded samples are not supported (%u != %u)\n", bits, valid_bits);
rc = -IP_ERROR_FILE_FORMAT;
goto error_exit;
}
channel_mask = read_le32(fmt + 20);
format_tag = read_le16(fmt + 24);
/* ignore rest of extension tag */
}
free(fmt);
if (format_tag != WAVE_FORMAT_PCM) {
d_print("unsupported format tag %u, should be 1\n", format_tag);
rc = -IP_ERROR_UNSUPPORTED_FILE_TYPE;
goto error_exit;
}
if ((bits != 8 && bits != 16 && bits != 24 && bits != 32) || channels < 1) {
rc = -IP_ERROR_SAMPLE_FORMAT;
goto error_exit;
}
ip_data->sf = sf_channels(channels) | sf_rate(rate) | sf_bits(bits) |
sf_signed(bits > 8);
channel_map_init_waveex(channels, channel_mask, ip_data->channel_map);
}
rc = find_chunk(ip_data->fd, "data", &priv->pcm_size);
if (rc)
goto error_exit;
priv->pcm_start = lseek(ip_data->fd, 0, SEEK_CUR);
if (priv->pcm_start == -1) {
rc = -IP_ERROR_ERRNO;
goto error_exit;
}
priv->sec_size = sf_get_second_size(ip_data->sf);
priv->frame_size = sf_get_frame_size(ip_data->sf);
priv->pos = 0;
d_print("pcm start: %u\n", (unsigned int)priv->pcm_start);
d_print("pcm size: %u\n", priv->pcm_size);
d_print("\n");
d_print("sr: %d, ch: %d, bits: %d, signed: %d\n", sf_get_rate(ip_data->sf),
sf_get_channels(ip_data->sf), sf_get_bits(ip_data->sf),
sf_get_signed(ip_data->sf));
/* clamp pcm_size to full frames (file might be corrupt or truncated) */
priv->pcm_size -= priv->pcm_size % sf_get_frame_size(ip_data->sf);
return 0;
error_exit:
save = errno;
free(priv);
errno = save;
return rc;
}
static int wav_close(struct input_plugin_data *ip_data)
{
struct wav_private *priv;
priv = ip_data->private;
free(priv);
ip_data->private = NULL;
return 0;
}
static int wav_read(struct input_plugin_data *ip_data, char *buffer, int _count)
{
struct wav_private *priv = ip_data->private;
unsigned int count = _count;
int rc;
if (priv->pos == priv->pcm_size) {
/* eof */
return 0;
}
if (count > priv->pcm_size - priv->pos)
count = priv->pcm_size - priv->pos;
rc = read(ip_data->fd, buffer, count);
if (rc == -1) {
d_print("read error\n");
return -IP_ERROR_ERRNO;
}
if (rc == 0) {
d_print("eof\n");
return 0;
}
priv->pos += rc;
return rc;
}
static int wav_seek(struct input_plugin_data *ip_data, double _offset)
{
struct wav_private *priv = ip_data->private;
unsigned int offset;
offset = (unsigned int)(_offset * (double)priv->sec_size + 0.5);
/* align to frame size */
offset -= offset % priv->frame_size;
priv->pos = offset;
if (lseek(ip_data->fd, priv->pcm_start + offset, SEEK_SET) == -1)
return -1;
return 0;
}
static struct {
const char *old;
const char *new;
} key_map[] = {
{ "IART", "artist" },
{ "ICMT", "comment" },
{ "ICOP", "copyright" },
{ "ICRD", "date" },
{ "IGNR", "genre" },
{ "INAM", "title" },
{ "IPRD", "album" },
{ "IPRT", "tracknumber" },
{ "ISFT", "software" },
{ NULL, NULL }
};
static const char *lookup_key(const char *key)
{
int i;
for (i = 0; key_map[i].old; i++) {
if (!strcasecmp(key, key_map[i].old))
return key_map[i].new;
}
return NULL;
}
static int wav_read_comments(struct input_plugin_data *ip_data,
struct keyval **comments)
{
GROWING_KEYVALS(c);
struct wav_private *priv;
unsigned int size;
char id[4+1];
int rc = 0;
priv = ip_data->private;
id[4] = '\0';
if (lseek(ip_data->fd, 12, SEEK_SET) == -1) {
rc = -1;
goto out;
}
while (1) {
rc = read_chunk_header(ip_data->fd, id, &size);
if (rc)
break;
if (strcmp(id, "data") == 0) {
rc = 0;
break;
} else if (strcmp(id, "LIST") == 0) {
char buf[4];
rc = read_all(ip_data->fd, buf, 4);
if (rc == -1)
break;
if (memcmp(buf, "INFO", 4) == 0)
continue;
size -= 4;
} else {
const char *key = lookup_key(id);
if (key) {
char *val = xnew(char, size + 1);
rc = read_all(ip_data->fd, val, size);
if (rc == -1) {
free(val);
break;
}
val[rc] = '\0';
comments_add(&c, key, val);
continue;
}
}
if (lseek(ip_data->fd, size, SEEK_CUR) == -1) {
rc = -1;
break;
}
}
out:
lseek(ip_data->fd, priv->pcm_start, SEEK_SET);
keyvals_terminate(&c);
if (rc && c.count == 0) {
keyvals_free(c.keyvals);
return -1;
}
*comments = c.keyvals;
return 0;
}
static int wav_duration(struct input_plugin_data *ip_data)
{
struct wav_private *priv;
int duration;
priv = ip_data->private;
duration = priv->pcm_size / priv->sec_size;
return duration;
}
static long wav_bitrate(struct input_plugin_data *ip_data)
{
sample_format_t sf = ip_data->sf;
return sf_get_bits(sf) * sf_get_rate(sf) * sf_get_channels(sf);
}
static char *wav_codec(struct input_plugin_data *ip_data)
{
char buf[16];
snprintf(buf, 16, "pcm_%c%u%s",
sf_get_signed(ip_data->sf) ? 's' : 'u',
sf_get_bits(ip_data->sf),
sf_get_bigendian(ip_data->sf) ? "be" : "le");
return xstrdup(buf);
}
static char *wav_codec_profile(struct input_plugin_data *ip_data)
{
return NULL;
}
const struct input_plugin_ops ip_ops = {
.open = wav_open,
.close = wav_close,
.read = wav_read,
.seek = wav_seek,
.read_comments = wav_read_comments,
.duration = wav_duration,
.bitrate = wav_bitrate,
.bitrate_current = wav_bitrate,
.codec = wav_codec,
.codec_profile = wav_codec_profile
};
const int ip_priority = 50;
const char * const ip_extensions[] = { "wav", NULL };
const char * const ip_mime_types[] = { NULL };
const char * const ip_options[] = { NULL };
|