[b74e2d]: tagutils / tagutils-ogg.c  Maximize  Restore  History

Download this file

543 lines (456 with data), 12.6 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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//=========================================================================
// FILENAME : tagutils-ogg.c
// DESCRIPTION : Ogg metadata reader
//=========================================================================
// Copyright (c) 2008- NETGEAR, Inc. All Rights Reserved.
//=========================================================================
/*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* This file is derived from mt-daap project.
*/
typedef struct _ogg_stream_processor {
void (*process_page)(struct _ogg_stream_processor *, ogg_page *, struct song_metadata *);
void (*process_end)(struct _ogg_stream_processor *, struct song_metadata *);
int isillegal;
int constraint_violated;
int shownillegal;
int isnew;
long seqno;
int lostseq;
int start;
int end;
int num;
char *type;
ogg_uint32_t serial;
ogg_stream_state os;
void *data;
} ogg_stream_processor;
typedef struct {
ogg_stream_processor *streams;
int allocated;
int used;
int in_headers;
} ogg_stream_set;
typedef struct {
vorbis_info vi;
vorbis_comment vc;
ogg_int64_t bytes;
ogg_int64_t lastgranulepos;
ogg_int64_t firstgranulepos;
int doneheaders;
} ogg_misc_vorbis_info;
#define CONSTRAINT_PAGE_AFTER_EOS 1
#define CONSTRAINT_MUXING_VIOLATED 2
static ogg_stream_set *
_ogg_create_stream_set(void)
{
ogg_stream_set *set = calloc(1, sizeof(ogg_stream_set));
set->streams = calloc(5, sizeof(ogg_stream_processor));
set->allocated = 5;
set->used = 0;
return set;
}
static void
_ogg_vorbis_process(ogg_stream_processor *stream, ogg_page *page,
struct song_metadata *psong)
{
ogg_packet packet;
ogg_misc_vorbis_info *inf = stream->data;
int i, header = 0;
ogg_stream_pagein(&stream->os, page);
if(inf->doneheaders < 3)
header = 1;
while(ogg_stream_packetout(&stream->os, &packet) > 0)
{
if(inf->doneheaders < 3)
{
if(vorbis_synthesis_headerin(&inf->vi, &inf->vc, &packet) < 0)
{
DPRINTF(E_WARN, L_SCANNER, "Could not decode vorbis header "
"packet - invalid vorbis stream (%d)\n", stream->num);
continue;
}
inf->doneheaders++;
if(inf->doneheaders == 3)
{
if(ogg_page_granulepos(page) != 0 || ogg_stream_packetpeek(&stream->os, NULL) == 1)
DPRINTF(E_WARN, L_SCANNER, "No header in vorbis stream %d\n", stream->num);
DPRINTF(E_DEBUG, L_SCANNER, "Vorbis headers parsed for stream %d, "
"information follows...\n", stream->num);
DPRINTF(E_DEBUG, L_SCANNER, "Channels: %d\n", inf->vi.channels);
DPRINTF(E_DEBUG, L_SCANNER, "Rate: %ld\n\n", inf->vi.rate);
psong->samplerate = inf->vi.rate;
psong->channels = inf->vi.channels;
if(inf->vi.bitrate_nominal > 0)
{
DPRINTF(E_DEBUG, L_SCANNER, "Nominal bitrate: %f kb/s\n",
(double)inf->vi.bitrate_nominal / 1000.0);
psong->bitrate = inf->vi.bitrate_nominal / 1000;
}
else
{
int upper_rate, lower_rate;
DPRINTF(E_DEBUG, L_SCANNER, "Nominal bitrate not set\n");
//
upper_rate = 0;
lower_rate = 0;
if(inf->vi.bitrate_upper > 0)
{
DPRINTF(E_DEBUG, L_SCANNER, "Upper bitrate: %f kb/s\n",
(double)inf->vi.bitrate_upper / 1000.0);
upper_rate = inf->vi.bitrate_upper;
}
else
{
DPRINTF(E_DEBUG, L_SCANNER, "Upper bitrate not set\n");
}
if(inf->vi.bitrate_lower > 0)
{
DPRINTF(E_DEBUG, L_SCANNER, "Lower bitrate: %f kb/s\n",
(double)inf->vi.bitrate_lower / 1000.0);
lower_rate = inf->vi.bitrate_lower;;
}
else
{
DPRINTF(E_DEBUG, L_SCANNER, "Lower bitrate not set\n");
}
if(upper_rate && lower_rate)
{
psong->bitrate = (upper_rate + lower_rate) / 2;
}
else
{
psong->bitrate = upper_rate + lower_rate;
}
}
if(inf->vc.comments > 0)
DPRINTF(E_DEBUG, L_SCANNER,
"User comments section follows...\n");
for(i = 0; i < inf->vc.comments; i++)
{
vc_scan(psong, inf->vc.user_comments[i], inf->vc.comment_lengths[i]);
}
}
}
}
if(!header)
{
ogg_int64_t gp = ogg_page_granulepos(page);
if(gp > 0)
{
if(gp < inf->lastgranulepos)
DPRINTF(E_WARN, L_SCANNER, "granulepos in stream %d decreases from %lld to %lld",
stream->num, inf->lastgranulepos, gp);
inf->lastgranulepos = gp;
}
else
{
DPRINTF(E_WARN, L_SCANNER, "Malformed vorbis strem.\n");
}
inf->bytes += page->header_len + page->body_len;
}
}
static void
_ogg_vorbis_end(ogg_stream_processor *stream, struct song_metadata *psong)
{
ogg_misc_vorbis_info *inf = stream->data;
long minutes, seconds;
double bitrate, time;
time = (double)inf->lastgranulepos / inf->vi.rate;
bitrate = inf->bytes * 8 / time / 1000;
if(psong != NULL)
{
if(psong->bitrate <= 0)
{
psong->bitrate = bitrate * 1000;
}
psong->song_length = time * 1000;
}
minutes = (long)time / 60;
seconds = (long)time - minutes * 60;
vorbis_comment_clear(&inf->vc);
vorbis_info_clear(&inf->vi);
free(stream->data);
}
static void
_ogg_process_null(ogg_stream_processor *stream, ogg_page *page, struct song_metadata *psong)
{
// invalid stream
}
static void
_ogg_process_other(ogg_stream_processor *stream, ogg_page *page, struct song_metadata *psong)
{
ogg_stream_pagein(&stream->os, page);
}
static void
_ogg_free_stream_set(ogg_stream_set *set)
{
int i;
for(i = 0; i < set->used; i++)
{
if(!set->streams[i].end)
{
// no EOS
if(set->streams[i].process_end)
set->streams[i].process_end(&set->streams[i], NULL);
}
ogg_stream_clear(&set->streams[i].os);
}
free(set->streams);
free(set);
}
static int
_ogg_streams_open(ogg_stream_set *set)
{
int i;
int res = 0;
for(i = 0; i < set->used; i++)
{
if(!set->streams[i].end)
res++;
}
return res;
}
static void
_ogg_null_start(ogg_stream_processor *stream)
{
stream->process_end = NULL;
stream->type = "invalid";
stream->process_page = _ogg_process_null;
}
static void
_ogg_other_start(ogg_stream_processor *stream, char *type)
{
if(type)
stream->type = type;
else
stream->type = "unknown";
stream->process_page = _ogg_process_other;
stream->process_end = NULL;
}
static void
_ogg_vorbis_start(ogg_stream_processor *stream)
{
ogg_misc_vorbis_info *info;
stream->type = "vorbis";
stream->process_page = _ogg_vorbis_process;
stream->process_end = _ogg_vorbis_end;
stream->data = calloc(1, sizeof(ogg_misc_vorbis_info));
info = stream->data;
vorbis_comment_init(&info->vc);
vorbis_info_init(&info->vi);
}
static ogg_stream_processor *
_ogg_find_stream_processor(ogg_stream_set *set, ogg_page *page)
{
ogg_uint32_t serial = ogg_page_serialno(page);
int i, found = 0;
int invalid = 0;
int constraint = 0;
ogg_stream_processor *stream;
for(i = 0; i < set->used; i++)
{
if(serial == set->streams[i].serial)
{
found = 1;
stream = &(set->streams[i]);
set->in_headers = 0;
if(stream->end)
{
stream->isillegal = 1;
stream->constraint_violated = CONSTRAINT_PAGE_AFTER_EOS;
return stream;
}
stream->isnew = 0;
stream->start = ogg_page_bos(page);
stream->end = ogg_page_eos(page);
stream->serial = serial;
return stream;
}
}
if(_ogg_streams_open(set) && !set->in_headers)
{
constraint = CONSTRAINT_MUXING_VIOLATED;
invalid = 1;
}
set->in_headers = 1;
if(set->allocated < set->used)
stream = &set->streams[set->used];
else
{
set->allocated += 5;
set->streams = realloc(set->streams, sizeof(ogg_stream_processor) * set->allocated);
stream = &set->streams[set->used];
}
set->used++;
stream->num = set->used; // count from 1
stream->isnew = 1;
stream->isillegal = invalid;
stream->constraint_violated = constraint;
{
int res;
ogg_packet packet;
ogg_stream_init(&stream->os, serial);
ogg_stream_pagein(&stream->os, page);
res = ogg_stream_packetout(&stream->os, &packet);
if(res <= 0)
{
DPRINTF(E_WARN, L_SCANNER, "Invalid header page, no packet found\n");
_ogg_null_start(stream);
}
else if(packet.bytes >= 7 && memcmp(packet.packet, "\001vorbis", 7) == 0)
_ogg_vorbis_start(stream);
else if(packet.bytes >= 8 && memcmp(packet.packet, "OggMIDI\0", 8) == 0)
_ogg_other_start(stream, "MIDI");
else
_ogg_other_start(stream, NULL);
res = ogg_stream_packetout(&stream->os, &packet);
if(res > 0)
{
DPRINTF(E_WARN, L_SCANNER, "Invalid header page in stream %d, "
"contains multiple packets\n", stream->num);
}
/* re-init, ready for processing */
ogg_stream_clear(&stream->os);
ogg_stream_init(&stream->os, serial);
}
stream->start = ogg_page_bos(page);
stream->end = ogg_page_eos(page);
stream->serial = serial;
return stream;
}
static int
_ogg_get_next_page(FILE *f, ogg_sync_state *sync, ogg_page *page,
ogg_int64_t *written)
{
int ret;
char *buffer;
int bytes;
while((ret = ogg_sync_pageout(sync, page)) <= 0)
{
if(ret < 0)
DPRINTF(E_WARN, L_SCANNER, "Hole in data found at approximate offset %lld bytes. Corrupted ogg.\n", *written);
buffer = ogg_sync_buffer(sync, 4500); // chunk=4500
bytes = fread(buffer, 1, 4500, f);
if(bytes <= 0)
{
ogg_sync_wrote(sync, 0);
return 0;
}
ogg_sync_wrote(sync, bytes);
*written += bytes;
}
return 1;
}
static int
_get_oggfileinfo(char *filename, struct song_metadata *psong)
{
FILE *file = fopen(filename, "rb");
ogg_sync_state sync;
ogg_page page;
ogg_stream_set *processors = _ogg_create_stream_set();
int gotpage = 0;
ogg_int64_t written = 0;
if(!file)
{
DPRINTF(E_FATAL, L_SCANNER,
"Error opening input file \"%s\": %s\n", filename, strerror(errno));
_ogg_free_stream_set(processors);
return -1;
}
DPRINTF(E_INFO, L_SCANNER, "Processing file \"%s\"...\n\n", filename);
ogg_sync_init(&sync);
while(_ogg_get_next_page(file, &sync, &page, &written))
{
ogg_stream_processor *p = _ogg_find_stream_processor(processors, &page);
gotpage = 1;
if(!p)
{
DPRINTF(E_FATAL, L_SCANNER, "Could not find a processor for stream, bailing\n");
_ogg_free_stream_set(processors);
return -1;
}
if(p->isillegal && !p->shownillegal)
{
char *constraint;
switch(p->constraint_violated)
{
case CONSTRAINT_PAGE_AFTER_EOS:
constraint = "Page found for stream after EOS flag";
break;
case CONSTRAINT_MUXING_VIOLATED:
constraint = "Ogg muxing constraints violated, new "
"stream before EOS of all previous streams";
break;
default:
constraint = "Error unknown.";
}
DPRINTF(E_WARN, L_SCANNER,
"Warning: illegally placed page(s) for logical stream %d\n"
"This indicates a corrupt ogg file: %s.\n",
p->num, constraint);
p->shownillegal = 1;
if(!p->isnew)
continue;
}
if(p->isnew)
{
DPRINTF(E_DEBUG, L_SCANNER, "New logical stream (#%d, serial: %08x): type %s\n",
p->num, p->serial, p->type);
if(!p->start)
DPRINTF(E_WARN, L_SCANNER,
"stream start flag not set on stream %d\n",
p->num);
}
else if(p->start)
DPRINTF(E_WARN, L_SCANNER, "stream start flag found in mid-stream "
"on stream %d\n", p->num);
if(p->seqno++ != ogg_page_pageno(&page))
{
if(!p->lostseq)
DPRINTF(E_WARN, L_SCANNER,
"sequence number gap in stream %d. Got page %ld "
"when expecting page %ld. Indicates missing data.\n",
p->num, ogg_page_pageno(&page), p->seqno - 1);
p->seqno = ogg_page_pageno(&page);
p->lostseq = 1;
}
else
p->lostseq = 0;
if(!p->isillegal)
{
p->process_page(p, &page, psong);
if(p->end)
{
if(p->process_end)
p->process_end(p, psong);
DPRINTF(E_DEBUG, L_SCANNER, "Logical stream %d ended\n", p->num);
p->isillegal = 1;
p->constraint_violated = CONSTRAINT_PAGE_AFTER_EOS;
}
}
}
_ogg_free_stream_set(processors);
ogg_sync_clear(&sync);
fclose(file);
if(!gotpage)
{
DPRINTF(E_ERROR, L_SCANNER, "No ogg data found in file \"%s\".\n", filename);
return -1;
}
return 0;
}