| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#include "opus.h" |
| 18 |
|
| 19 |
int |
| 20 |
get_opus_metadata(PerlIO *infile, char *file, HV *info, HV *tags) |
| 21 |
{ |
| 22 |
return _opus_parse(infile, file, info, tags, 0); |
| 23 |
} |
| 24 |
|
| 25 |
#define OGG_HEADER_SIZE 28 |
| 26 |
int |
| 27 |
_opus_parse(PerlIO *infile, char *file, HV *info, HV *tags, uint8_t seeking) |
| 28 |
{ |
| 29 |
Buffer ogg_buf, vorbis_buf; |
| 30 |
unsigned char *bptr; |
| 31 |
unsigned char *last_bptr; |
| 32 |
unsigned int buf_size; |
| 33 |
|
| 34 |
unsigned int id3_size = 0; |
| 35 |
|
| 36 |
off_t file_size; |
| 37 |
off_t audio_size; |
| 38 |
off_t audio_offset = 0; |
| 39 |
off_t seek_position; |
| 40 |
|
| 41 |
unsigned char ogghdr[OGG_HEADER_SIZE]; |
| 42 |
char header_type; |
| 43 |
int serialno; |
| 44 |
int final_serialno; |
| 45 |
int pagenum; |
| 46 |
uint8_t num_segments; |
| 47 |
int pagelen; |
| 48 |
int page = 0; |
| 49 |
int packets = 0; |
| 50 |
int streams = 0; |
| 51 |
|
| 52 |
unsigned char opushdr[11]; |
| 53 |
unsigned char channels; |
| 54 |
unsigned int samplerate = 0; |
| 55 |
unsigned int preskip = 0; |
| 56 |
unsigned int input_samplerate = 0; |
| 57 |
uint64_t granule_pos = 0; |
| 58 |
|
| 59 |
unsigned char TOC_byte = 0; |
| 60 |
|
| 61 |
int i; |
| 62 |
int err = 0; |
| 63 |
|
| 64 |
buffer_init(&ogg_buf, OGG_BLOCK_SIZE); |
| 65 |
buffer_init(&vorbis_buf, 0); |
| 66 |
|
| 67 |
file_size = _file_size(infile); |
| 68 |
my_hv_store( info, "file_size", newSVuv(file_size) ); |
| 69 |
|
| 70 |
if ( !_check_buf(infile, &ogg_buf, 10, OGG_BLOCK_SIZE) ) { |
| 71 |
err = -1; |
| 72 |
goto out; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
bptr = (unsigned char *)buffer_ptr(&ogg_buf); |
| 77 |
if ( |
| 78 |
(bptr[0] == 'I' && bptr[1] == 'D' && bptr[2] == '3') && |
| 79 |
bptr[3] < 0xff && bptr[4] < 0xff && |
| 80 |
bptr[6] < 0x80 && bptr[7] < 0x80 && bptr[8] < 0x80 && bptr[9] < 0x80 |
| 81 |
) { |
| 82 |
|
| 83 |
id3_size = 10 + (bptr[6]<<21) + (bptr[7]<<14) + (bptr[8]<<7) + bptr[9]; |
| 84 |
|
| 85 |
if (bptr[5] & 0x10) { |
| 86 |
|
| 87 |
id3_size += 10; |
| 88 |
} |
| 89 |
|
| 90 |
buffer_clear(&ogg_buf); |
| 91 |
|
| 92 |
audio_offset += id3_size; |
| 93 |
|
| 94 |
DEBUG_TRACE("Skipping ID3v2 tag of size %d\n", id3_size); |
| 95 |
|
| 96 |
PerlIO_seek(infile, id3_size, SEEK_SET); |
| 97 |
} |
| 98 |
|
| 99 |
while (1) { |
| 100 |
|
| 101 |
if ( !_check_buf(infile, &ogg_buf, OGG_HEADER_SIZE, OGG_BLOCK_SIZE) ) { |
| 102 |
err = -1; |
| 103 |
goto out; |
| 104 |
} |
| 105 |
|
| 106 |
buffer_get(&ogg_buf, ogghdr, OGG_HEADER_SIZE); |
| 107 |
|
| 108 |
audio_offset += OGG_HEADER_SIZE; |
| 109 |
|
| 110 |
|
| 111 |
if ( ogghdr[0] != 'O' || ogghdr[1] != 'g' || ogghdr[2] != 'g' || ogghdr[3] != 'S' ) { |
| 112 |
PerlIO_printf(PerlIO_stderr(), "Not an Ogg file (bad OggS header): %s\n", file); |
| 113 |
goto out; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
header_type = ogghdr[5]; |
| 118 |
|
| 119 |
|
| 120 |
bptr = ogghdr + 6; |
| 121 |
granule_pos = (uint64_t)CONVERT_INT32LE(bptr); |
| 122 |
bptr += 4; |
| 123 |
granule_pos |= (uint64_t)CONVERT_INT32LE(bptr) << 32; |
| 124 |
|
| 125 |
|
| 126 |
serialno = CONVERT_INT32LE((ogghdr+14)); |
| 127 |
|
| 128 |
|
| 129 |
if ( header_type & 0x02 ) { |
| 130 |
streams++; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
if ( !(header_type & 0x01) ) { |
| 135 |
packets++; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
if (packets > 2 * streams && !buffer_len(&vorbis_buf) ) { |
| 140 |
break; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
pagenum = CONVERT_INT32LE((ogghdr+18)); |
| 145 |
|
| 146 |
if (page >= 0 && page == pagenum) { |
| 147 |
page++; |
| 148 |
} |
| 149 |
else { |
| 150 |
page = -1; |
| 151 |
DEBUG_TRACE("Missing page(s) in Ogg file: %s\n", file); |
| 152 |
} |
| 153 |
|
| 154 |
DEBUG_TRACE("OggS page %d / packet %d at %d\n", pagenum, packets, (int)(audio_offset - OGG_HEADER_SIZE)); |
| 155 |
DEBUG_TRACE(" granule_pos: %llu\n", granule_pos); |
| 156 |
|
| 157 |
|
| 158 |
num_segments = ogghdr[26]; |
| 159 |
|
| 160 |
|
| 161 |
pagelen = ogghdr[27]; |
| 162 |
if (num_segments > 1) { |
| 163 |
int i; |
| 164 |
|
| 165 |
if ( !_check_buf(infile, &ogg_buf, num_segments, OGG_BLOCK_SIZE) ) { |
| 166 |
err = -1; |
| 167 |
goto out; |
| 168 |
} |
| 169 |
|
| 170 |
for( i = 0; i < num_segments - 1; i++ ) { |
| 171 |
u_char x; |
| 172 |
x = buffer_get_char(&ogg_buf); |
| 173 |
pagelen += x; |
| 174 |
} |
| 175 |
|
| 176 |
audio_offset += num_segments - 1; |
| 177 |
} |
| 178 |
|
| 179 |
if ( !_check_buf(infile, &ogg_buf, pagelen, OGG_BLOCK_SIZE) ) { |
| 180 |
err = -1; |
| 181 |
goto out; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
if ( buffer_len(&ogg_buf) < pagelen ) { |
| 186 |
PerlIO_printf(PerlIO_stderr(), "Premature end of file: %s\n", file); |
| 187 |
|
| 188 |
err = -1; |
| 189 |
goto out; |
| 190 |
} |
| 191 |
|
| 192 |
audio_offset += pagelen; |
| 193 |
|
| 194 |
|
| 195 |
buffer_append( &vorbis_buf, buffer_ptr(&ogg_buf), pagelen ); |
| 196 |
DEBUG_TRACE(" Read %d into vorbis buffer\n", pagelen); |
| 197 |
|
| 198 |
if ( granule_pos != 0 ) { |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
buffer_consume( &ogg_buf, pagelen ); |
| 205 |
continue; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
TOC_byte = buffer_get_char(&vorbis_buf); |
| 210 |
if ( TOC_byte == 'O' ) { |
| 211 |
if ( strncmp( buffer_ptr(&vorbis_buf), "pusTags", 7 ) == 0) { |
| 212 |
buffer_consume(&vorbis_buf, 7); |
| 213 |
DEBUG_TRACE(" Found Opus tags TOC packet type\n"); |
| 214 |
if ( !seeking ) { |
| 215 |
_parse_vorbis_comments(infile, &vorbis_buf, tags, 0); |
| 216 |
} |
| 217 |
DEBUG_TRACE(" parsed vorbis comments\n"); |
| 218 |
|
| 219 |
buffer_clear(&vorbis_buf); |
| 220 |
} |
| 221 |
else { |
| 222 |
|
| 223 |
if ( strncmp( buffer_ptr(&vorbis_buf), "pusHead", 7 ) ) { |
| 224 |
PerlIO_printf(PerlIO_stderr(), "Not an Opus file (bad opus header): %s\n", file); |
| 225 |
goto out; |
| 226 |
} |
| 227 |
buffer_consume( &vorbis_buf, 7 ); |
| 228 |
|
| 229 |
DEBUG_TRACE(" Found Opus header TOC packet type\n"); |
| 230 |
|
| 231 |
|
| 232 |
if ( buffer_len(&vorbis_buf) < 11 ) { |
| 233 |
PerlIO_printf(PerlIO_stderr(), "Not an Opus file (opus header too short): %s\n", file); |
| 234 |
goto out; |
| 235 |
} |
| 236 |
|
| 237 |
buffer_get(&vorbis_buf, opushdr, 11); |
| 238 |
|
| 239 |
my_hv_store( info, "version", newSViv( opushdr[0] ) ); |
| 240 |
|
| 241 |
channels = opushdr[1]; |
| 242 |
my_hv_store( info, "channels", newSViv(channels) ); |
| 243 |
my_hv_store( info, "stereo", newSViv( channels == 2 ? 1 : 0 ) ); |
| 244 |
|
| 245 |
preskip = CONVERT_INT16LE((opushdr+2)); |
| 246 |
my_hv_store( info, "preskip", newSViv(preskip) ); |
| 247 |
|
| 248 |
samplerate = 48000; |
| 249 |
my_hv_store( info, "samplerate", newSViv(samplerate) ); |
| 250 |
|
| 251 |
input_samplerate = CONVERT_INT32LE((opushdr+4)); |
| 252 |
my_hv_store( info, "input_samplerate", newSViv(input_samplerate) ); |
| 253 |
|
| 254 |
DEBUG_TRACE(" parsed opus info header\n"); |
| 255 |
} |
| 256 |
buffer_clear(&vorbis_buf); |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
buffer_consume( &ogg_buf, pagelen ); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
audio_offset -= OGG_HEADER_SIZE; |
| 265 |
|
| 266 |
|
| 267 |
my_hv_store( info, "audio_offset", newSViv(audio_offset) ); |
| 268 |
|
| 269 |
audio_size = file_size - audio_offset; |
| 270 |
my_hv_store( info, "audio_size", newSVuv(audio_size) ); |
| 271 |
|
| 272 |
my_hv_store( info, "serial_number", newSVuv(serialno) ); |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
#define BUF_SIZE 8500 // from vlc |
| 277 |
|
| 278 |
seek_position = file_size - BUF_SIZE; |
| 279 |
while (1) { |
| 280 |
if ( seek_position < audio_offset ) { |
| 281 |
seek_position = audio_offset; |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
DEBUG_TRACE("Seeking to %d to calculate bitrate/duration\n", (int)seek_position); |
| 286 |
PerlIO_seek(infile, seek_position, SEEK_SET); |
| 287 |
|
| 288 |
buffer_clear(&ogg_buf); |
| 289 |
|
| 290 |
if ( !_check_buf(infile, &ogg_buf, OGG_HEADER_SIZE, BUF_SIZE) ) { |
| 291 |
err = -1; |
| 292 |
goto out; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
bptr = (unsigned char *)buffer_ptr(&ogg_buf); |
| 297 |
buf_size = buffer_len(&ogg_buf); |
| 298 |
last_bptr = bptr; |
| 299 |
|
| 300 |
while (buf_size >= OGG_HEADER_SIZE) { |
| 301 |
if (bptr[0] == 'O' && bptr[1] == 'g' && bptr[2] == 'g' && bptr[3] == 'S') { |
| 302 |
bptr += 6; |
| 303 |
|
| 304 |
|
| 305 |
granule_pos = (uint64_t)CONVERT_INT32LE(bptr); |
| 306 |
bptr += 4; |
| 307 |
granule_pos |= (uint64_t)CONVERT_INT32LE(bptr) << 32; |
| 308 |
bptr += 4; |
| 309 |
DEBUG_TRACE("found granule_pos %llu / samplerate %d to calculate bitrate/duration\n", granule_pos, samplerate); |
| 310 |
|
| 311 |
last_bptr = bptr; |
| 312 |
} |
| 313 |
else { |
| 314 |
bptr++; |
| 315 |
buf_size--; |
| 316 |
} |
| 317 |
} |
| 318 |
bptr = last_bptr; |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
final_serialno = CONVERT_INT32LE((bptr)); |
| 323 |
|
| 324 |
if ( granule_pos && samplerate && serialno == final_serialno ) { |
| 325 |
|
| 326 |
int length = (int)(((granule_pos-preskip) * 1.0 / samplerate) * 1000); |
| 327 |
my_hv_store( info, "song_length_ms", newSVuv(length) ); |
| 328 |
my_hv_store( info, "bitrate_average", newSVuv( _bitrate(audio_size, length) ) ); |
| 329 |
|
| 330 |
DEBUG_TRACE("Using granule_pos %llu / samplerate %d to calculate bitrate/duration\n", granule_pos, samplerate); |
| 331 |
break; |
| 332 |
} |
| 333 |
if ( seek_position == audio_offset ) { |
| 334 |
DEBUG_TRACE("Packet not found we won't be able to determine the length\n"); |
| 335 |
break; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
seek_position -= (BUF_SIZE - OGG_HEADER_SIZE); |
| 340 |
} |
| 341 |
|
| 342 |
out: |
| 343 |
buffer_free(&ogg_buf); |
| 344 |
buffer_free(&vorbis_buf); |
| 345 |
|
| 346 |
DEBUG_TRACE("Err %d\n", err); |
| 347 |
if (err) return err; |
| 348 |
|
| 349 |
return 0; |
| 350 |
} |
| 351 |
|
| 352 |
static off_t |
| 353 |
opus_find_frame(PerlIO *infile, char *file, int offset) |
| 354 |
{ |
| 355 |
int frame_offset = -1; |
| 356 |
uint16_t preskip; |
| 357 |
uint32_t samplerate; |
| 358 |
uint32_t song_length_ms; |
| 359 |
uint64_t target_sample; |
| 360 |
HV *info = newHV(); |
| 361 |
HV *tags = newHV(); |
| 362 |
|
| 363 |
if (offset < 0) { |
| 364 |
return -1; |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
if ( _opus_parse(infile, file, info, tags, 1) != 0 ) { |
| 369 |
goto out; |
| 370 |
} |
| 371 |
|
| 372 |
song_length_ms = SvUV( *(my_hv_fetch( info, "song_length_ms" )) ); |
| 373 |
if (offset >= song_length_ms) { |
| 374 |
goto out; |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
samplerate = SvIV( *(my_hv_fetch( info, "samplerate" )) ); |
| 379 |
preskip = SvIV( *(my_hv_fetch( info, "preskip" )) ); |
| 380 |
target_sample = (uint64_t)offset * samplerate / 1000; |
| 381 |
target_sample += preskip; |
| 382 |
|
| 383 |
DEBUG_TRACE("Looking for target sample %llu\n", target_sample); |
| 384 |
frame_offset = _ogg_binary_search_sample(infile, file, info, target_sample); |
| 385 |
|
| 386 |
out: |
| 387 |
|
| 388 |
SvREFCNT_dec(info); |
| 389 |
SvREFCNT_dec(tags); |
| 390 |
|
| 391 |
return frame_offset; |
| 392 |
} |