-
Notifications
You must be signed in to change notification settings - Fork 0
/
cue.c
405 lines (302 loc) · 8.09 KB
/
cue.c
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
/*
* Copyright (C) 2008-2013 Various Authors
* Copyright (C) 2011 Gregory Petrosyan
*
* 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 "debug.h"
#include "input.h"
#include "utils.h"
#include "comment.h"
#include "xmalloc.h"
#include "cue_utils.h"
#include <stdio.h>
#include <fcntl.h>
struct cue_private {
struct input_plugin *child;
char *cue_filename;
int track_n;
double start_offset;
double current_offset;
double end_offset;
};
static int __parse_cue_url(const char *url, char **filename, int *track_n)
{
const char *slash;
long n;
if (!is_cue_url(url))
return 1;
url += 6;
slash = strrchr(url, '/');
if (!slash)
return 1;
if (str_to_int(slash + 1, &n) != 0)
return 1;
*filename = xstrndup(url, slash - url);
*track_n = n;
return 0;
}
static double __to_seconds(long v)
{
const int FRAMES_IN_SECOND = 75;
return (double)v / FRAMES_IN_SECOND;
}
static char *__make_absolute_path(const char *abs_filename, const char *rel_filename)
{
char *s;
const char *slash;
char buf[4096] = {0};
slash = strrchr(abs_filename, '/');
if (slash == NULL)
return xstrdup(rel_filename);
s = xstrndup(abs_filename, slash - abs_filename);
snprintf(buf, sizeof buf, "%s/%s", s, rel_filename);
free(s);
return xstrdup(buf);
}
static int cue_open(struct input_plugin_data *ip_data)
{
int rc;
FILE *cue;
Cd *cd;
Track *t;
char *child_filename;
struct cue_private *priv;
priv = xnew(struct cue_private, 1);
rc = __parse_cue_url(ip_data->filename, &priv->cue_filename, &priv->track_n);
if (rc) {
rc = -IP_ERROR_INVALID_URI;
goto url_parse_failed;
}
cue = fopen(priv->cue_filename, "r");
if (cue == NULL) {
rc = -IP_ERROR_ERRNO;
goto cue_open_failed;
}
cd = cue_parse_file__no_stderr_garbage(cue);
if (cd == NULL) {
rc = -IP_ERROR_FILE_FORMAT;
goto cue_parse_failed;
}
t = cd_get_track(cd, priv->track_n);
if (t == NULL) {
rc = -IP_ERROR_FILE_FORMAT;
goto cue_read_failed;
}
child_filename = track_get_filename(t);
if (child_filename == NULL) {
rc = -IP_ERROR_FILE_FORMAT;
goto cue_read_failed;
}
child_filename = __make_absolute_path(priv->cue_filename, child_filename);
priv->child = ip_new(child_filename);
free(child_filename);
rc = ip_open(priv->child);
if (rc)
goto ip_open_failed;
ip_setup(priv->child);
priv->start_offset = __to_seconds(track_get_start(t));
priv->current_offset = priv->start_offset;
rc = ip_seek(priv->child, priv->start_offset);
if (rc)
goto ip_open_failed;
if (track_get_length(t) != 0)
priv->end_offset = priv->start_offset + __to_seconds(track_get_length(t));
else
priv->end_offset = ip_duration(priv->child);
ip_data->fd = open(ip_get_filename(priv->child), O_RDONLY);
if (ip_data->fd == -1)
goto ip_open_failed;
ip_data->private = priv;
ip_data->sf = ip_get_sf(priv->child);
ip_get_channel_map(priv->child, ip_data->channel_map);
fclose(cue);
cd_delete(cd);
return 0;
ip_open_failed:
ip_delete(priv->child);
cue_read_failed:
cd_delete(cd);
cue_parse_failed:
fclose(cue);
cue_open_failed:
free(priv->cue_filename);
url_parse_failed:
free(priv);
return rc;
}
static int cue_close(struct input_plugin_data *ip_data)
{
struct cue_private *priv = ip_data->private;
close(ip_data->fd);
ip_data->fd = -1;
ip_delete(priv->child);
free(priv->cue_filename);
free(priv);
ip_data->private = NULL;
return 0;
}
static int cue_read(struct input_plugin_data *ip_data, char *buffer, int count)
{
int rc;
sample_format_t sf;
double len;
double rem_len;
struct cue_private *priv = ip_data->private;
if (priv->current_offset >= priv->end_offset)
return 0;
rc = ip_read(priv->child, buffer, count);
if (rc <= 0)
return rc;
sf = ip_get_sf(priv->child);
len = (double)rc / sf_get_second_size(sf);
rem_len = priv->end_offset - priv->current_offset;
priv->current_offset += len;
if (priv->current_offset >= priv->end_offset)
rc = (int)(rem_len * sf_get_rate(sf)) * sf_get_frame_size(sf);
return rc;
}
static int cue_seek(struct input_plugin_data *ip_data, double offset)
{
struct cue_private *priv = ip_data->private;
double new_offset = priv->start_offset + offset;
if (new_offset > priv->end_offset)
new_offset = priv->end_offset;
priv->current_offset = new_offset;
return ip_seek(priv->child, new_offset);
}
static int cue_read_comments(struct input_plugin_data *ip_data, struct keyval **comments)
{
int rc;
FILE *cue;
Cd *cd;
Rem *cd_rem;
Cdtext *cd_cdtext;
Track *t;
Rem *track_rem;
Cdtext *track_cdtext;
char *val;
char buf[32] = {0};
GROWING_KEYVALS(c);
struct cue_private *priv = ip_data->private;
cue = fopen(priv->cue_filename, "r");
if (cue == NULL) {
rc = -IP_ERROR_ERRNO;
goto cue_open_failed;
}
cd = cue_parse_file__no_stderr_garbage(cue);
if (cd == NULL) {
rc = -IP_ERROR_FILE_FORMAT;
goto cue_parse_failed;
}
t = cd_get_track(cd, priv->track_n);
if (t == NULL) {
rc = -IP_ERROR_FILE_FORMAT;
goto get_track_failed;
}
snprintf(buf, sizeof buf, "%d", priv->track_n);
comments_add(&c, "tracknumber", xstrdup(buf));
cd_rem = cd_get_rem(cd);
cd_cdtext = cd_get_cdtext(cd);
track_rem = track_get_rem(t);
track_cdtext = track_get_cdtext(t);
val = cdtext_get(PTI_TITLE, track_cdtext);
if (val != NULL)
comments_add(&c, "title", xstrdup(val));
val = cdtext_get(PTI_TITLE, cd_cdtext);
if (val != NULL)
comments_add(&c, "album", xstrdup(val));
val = cdtext_get(PTI_PERFORMER, track_cdtext);
if (val != NULL)
comments_add(&c, "artist", xstrdup(val));
val = cdtext_get(PTI_PERFORMER, cd_cdtext);
if (val != NULL)
comments_add(&c, "albumartist", xstrdup(val));
val = rem_get(REM_DATE, track_rem);
if (val != NULL) {
comments_add(&c, "date", xstrdup(val));
} else {
val = rem_get(REM_DATE, cd_rem);
if (val != NULL)
comments_add(&c, "date", xstrdup(val));
}
/*
* TODO:
* - replaygain REMs
* - genre?
*/
keyvals_terminate(&c);
*comments = c.keyvals;
cd_delete(cd);
fclose(cue);
return 0;
get_track_failed:
cd_delete(cd);
cue_parse_failed:
fclose(cue);
cue_open_failed:
return rc;
}
static int cue_duration(struct input_plugin_data *ip_data)
{
struct cue_private *priv = ip_data->private;
return priv->end_offset - priv->start_offset;
}
static long cue_bitrate(struct input_plugin_data *ip_data)
{
struct cue_private *priv = ip_data->private;
return ip_bitrate(priv->child);
}
static long cue_current_bitrate(struct input_plugin_data *ip_data)
{
struct cue_private *priv = ip_data->private;
return ip_current_bitrate(priv->child);
}
static char *cue_codec(struct input_plugin_data *ip_data)
{
struct cue_private *priv = ip_data->private;
return ip_codec(priv->child);
}
static char *cue_codec_profile(struct input_plugin_data *ip_data)
{
struct cue_private *priv = ip_data->private;
return ip_codec_profile(priv->child);
}
static int cue_set_option(int key, const char *val)
{
return -IP_ERROR_NOT_OPTION;
}
static int cue_get_option(int key, char **val)
{
return -IP_ERROR_NOT_OPTION;
}
const struct input_plugin_ops ip_ops = {
.open = cue_open,
.close = cue_close,
.read = cue_read,
.seek = cue_seek,
.read_comments = cue_read_comments,
.duration = cue_duration,
.bitrate = cue_bitrate,
.bitrate_current = cue_current_bitrate,
.codec = cue_codec,
.codec_profile = cue_codec_profile,
.set_option = cue_set_option,
.get_option = cue_get_option
};
const int ip_priority = 50;
const char * const ip_extensions[] = { NULL };
const char * const ip_mime_types[] = { "application/x-cue", NULL };
const char * const ip_options[] = { NULL };