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 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
|
/*******************************************************************************
* Goggles Audio Player Library *
********************************************************************************
* Copyright (C) 2010-2021 by Sander Jansen. 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 3 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 "ap_defs.h"
#include "ap_vorbis.h"
#include "ap_opus.h"
#include "ap_packet.h"
#include "ap_reader_plugin.h"
#include "ap_input_plugin.h"
#include <ogg/ogg.h>
#if defined(HAVE_VORBIS)
#include <vorbis/codec.h>
#elif defined(HAVE_TREMOR)
#include <tremor/ivorbiscodec.h>
#endif
#ifdef HAVE_OPUS
#include <opus/opus.h>
#endif
namespace ap {
class AudioEngine;
#if defined(HAVE_VORBIS) || defined(HAVE_TREMOR)
class VorbisCodec {
protected:
FXulong blockflag = 0; // up to 64 flags for each mode
FXuchar modecount = 0; // up to 64 entries
FXuchar modenbits = 0; // number of bits needed to store modecount.
FXint blocksize[2] = {0,0};
public:
/// Parse Info Header for blocksize
FXbool parseInfo(const FXuchar * packet,FXuint size);
// Parse Setup Header
FXbool parseSetup(const FXuchar * packet,FXuint size);
// Given the packet data, return blocksize
FXint getBlockSize(const FXuchar * packet) const;
// Clear Codec
void clear();
};
void VorbisCodec::clear() {
blockflag = 0;
modecount = 0;
modenbits = 0;
blocksize[0] = 0;
blocksize[1] = 0;
}
FXbool VorbisCodec::parseInfo(const FXuchar * packet,FXuint size) {
if (size>=28) {
blocksize[0] = 1<<(packet[28]&0xF);
blocksize[1] = 1<<(packet[28]>>4);
return true;
}
return false;
}
FXbool VorbisCodec::parseSetup(const FXuchar * packet,FXuint size) {
// Minimum packet size
const FXuint MIN_PACKET_SIZE = 7 + // header
1 + // number of codebooks
3 + // codebook sync bytes
5 + // codebook dims + entry counts
2 + // 1 vorbis_time_count value (at least 1)
2 + // 1 vorbis_floor_types value (at least 1)
2 + // 1 vorbis_residue_types value (at least 1)
2 + // 1 vorbis_mapping_type value (at least 1)
3; // 4 x 6 bits count fields: vorbis_time_count,vorbis_floor_count,vorbis_residue_count,vorbis_mapping_count
FXulong bits;
FXuchar nbits=0;
FXuchar found=0;
FXuint p = size-1;
// Clear State
blockflag = 0;
modecount = 0;
modenbits = 0;
// Find the framing bit
while(nbits==0 && p>MIN_PACKET_SIZE) {
bits=packet[p--];
for (nbits=8; nbits && (bits&(1<<(nbits-1)))==0; nbits--);
}
if (nbits==0) return false;
nbits--;
// read mode entry:
// vorbis_mode_blockflag:1
// vorbis_mode_windowtype:16 => should be 0
// vorbis_mode_transformtype:16 => should be 0
// vorbis_mode_mapping:8
//
// also read potential vorbis_mode_count:6
for (;nbits<47;nbits+=8){
bits<<=8;
bits|=(static_cast<FXulong>(packet[p--]));
}
// scan backwards to start of modes section
while(p>MIN_PACKET_SIZE && found<=64) {
const FXulong entry = (bits>>(nbits-41));
// expect 0s for window type and transform type
if (entry&0x1FFFFFFFELL) break;
// for now assume this is a valid entry
found++;
// Store block size flag
blockflag <<= 1;
blockflag |= (entry&0x1);
// if this is the first entry, this is preceded by vorbis_mode_count
const FXuchar size = 1 + ((bits>>(nbits-47))&0x3f);
if (size==found)
modecount = found;
// fill buffer with next entry
for (nbits-=41;nbits<47;nbits+=8) {
bits<<=8;
bits |= static_cast<FXulong>(packet[p--]);
}
}
// calculate modebits
if (modecount) {
for (FXint v = modecount-1; v>0; modenbits++) {
v>>=1;
}
return true;
}
return false;
}
FXint VorbisCodec::getBlockSize(const FXuchar * packet) const {
const FXuchar mode = (packet[0]>>1)&modenbits;
return blocksize[(blockflag>>mode)&0x1];
}
#endif
struct OggReaderState {
FXbool has_stream = false;
FXbool has_eos = false;
FXbool has_page = false;
FXbool has_packet = false;
FXbool header_written = false;
FXuint bytes_written = 0;
OggReaderState() {}
void reset() {has_stream=false;has_eos=false;has_page=false; has_packet=false; header_written=false;bytes_written=0; }
};
class OggReader : public ReaderPlugin {
protected:
enum {
FLAG_OGG_VORBIS = 0x2,
FLAG_OGG_FLAC = 0x4,
FLAG_OGG_OPUS = 0x8,
FLAG_VORBIS_INFO = 0x10,
FLAG_VORBIS_COMMENT = 0x20,
FLAG_VORBIS_BLOCK = 0x40,
FLAG_VORBIS_MASK = FLAG_VORBIS_INFO|FLAG_VORBIS_COMMENT|FLAG_VORBIS_BLOCK
};
protected:
OggReaderState state;
ogg_stream_state stream = {};
ogg_sync_state sync = {};
ogg_page page = {};
ogg_packet op = {};
protected:
MemoryBuffer cached_packets;
protected:
#if defined(HAVE_VORBIS) || defined(HAVE_TREMOR)
VorbisCodec vorbis;
#endif
protected:
Packet * packet = nullptr;
ReadStatus status = ReadError;
FXuchar codec = Codec::Invalid;
FXlong stream_start = 0;
FXlong input_position = -1;
FXushort stream_offset_start = 0;
FXushort stream_offset_end = 0;
FXlong stream_position = -1;
protected:
FXbool match_page();
FXbool fetch_next_page();
FXbool fetch_next_packet(FXbool nocache=false);
void submit_ogg_packet();
void cache_ogg_packet();
FXlong find_lastpage_position();
ReadStatus parse();
#if defined(HAVE_VORBIS) || defined(HAVE_TREMOR)
ReadStatus parse_vorbis_stream();
void check_vorbis_length();
FXbool parse_vorbis_setup();
#endif
#ifdef HAVE_FLAC
ReadStatus parse_flac_stream();
#endif
#ifdef HAVE_OPUS
ReadStatus parse_opus_stream();
void check_opus_length();
#endif
public:
OggReader(InputContext *);
FXuchar format() const override { return Format::OGG; };
FXbool init(InputPlugin*) override;
FXlong seek_offset(FXdouble) const;
FXbool seek(FXlong offset) override;
FXbool can_seek() const override;
ReadStatus process(Packet*) override;
virtual ~OggReader();
};
OggReader::OggReader(InputContext * ctx) : ReaderPlugin(ctx), cached_packets(0) {
ogg_sync_init(&sync);
}
OggReader::~OggReader() {
ogg_sync_clear(&sync);
if (state.has_stream) {
ogg_stream_clear(&stream);
state.has_stream=false;
}
}
FXbool OggReader::can_seek() const {
return stream_length>0;
}
FXlong OggReader::seek_offset(FXdouble pos) const{
if (stream_length>0)
return stream_start + (stream_length*pos);
else
return -1;
}
FXbool OggReader::seek(FXlong target){
FXASSERT(stream_length>0);
if (packet) {
packet->unref();
packet=nullptr;
}
state.has_eos=false;
state.has_packet=false;
state.has_page=false;
state.header_written=false;
state.bytes_written=0;
ogg_sync_reset(&sync);
ogg_stream_reset(&stream);
/*
When seeking within an Ogg Opus stream, the decoder should start decoding (and discarding the output) at least 3840 samples (80 ms)
prior to the seek point in order to ensure that the output audio is correct at the seek point.
*/
if (codec==Codec::Opus)
target = FXMAX(0,target-3840);
FXlong offset = input->size() * (target/stream_length);
FXlong lastpos = -1;
input_position = input->position(offset,FXIO::Begin);
GM_DEBUG_PRINT("[ogg] target seek %ld / %ld => %ld\n",target,stream_length,offset);
while(fetch_next_page()) {
if (ogg_page_granulepos(&page)>target) {
GM_DEBUG_PRINT("[ogg] found %ld %ld %ld\n",ogg_page_granulepos(&page),lastpos,offset);
if (lastpos>=0 || offset==0) {
input->position((lastpos>=0) ? lastpos : offset,FXIO::Begin);
ogg_sync_reset(&sync);
ogg_stream_reset(&stream);
return true;
}
else {
offset=FXMIN(0,offset>>1);
GM_DEBUG_PRINT("went to far. start at %ld\n",offset);
input_position=input->position(offset,FXIO::Begin);
lastpos=-1;
ogg_sync_reset(&sync);
ogg_stream_reset(&stream);
}
}
else {
lastpos=input_position;
stream_position=ogg_page_granulepos(&page);
}
}
if (lastpos==-1) {
GM_DEBUG_PRINT("seeking beyond end of stream\n");
}
else {
GM_DEBUG_PRINT("seeking to %ld\n",lastpos);
input->position(lastpos,FXIO::Begin);
ogg_sync_reset(&sync);
ogg_stream_reset(&stream);
}
return true;
}
FXbool OggReader::init(InputPlugin*plugin) {
ReaderPlugin::init(plugin);
ogg_sync_clear(&sync);
ogg_sync_reset(&sync);
flags&=~(FLAG_PARSED|FLAG_OGG_VORBIS|FLAG_OGG_FLAC|FLAG_OGG_OPUS|FLAG_VORBIS_MASK);
status=ReadOk;
input_position=-1;
stream_start=0;
stream_offset_start=0;
stream_offset_end=0;
if (state.has_stream) {
ogg_stream_clear(&stream);
state.has_stream=false;
}
return true;
}
#define BUFFERSIZE 32768
FXbool OggReader::match_page() {
if (state.has_stream) {
if (ogg_page_serialno(&page)==stream.serialno)
return true;
GM_DEBUG_PRINT("[ogg] page serial not matching\n");
if (input->serial() && state.has_eos) {
GM_DEBUG_PRINT("[ogg] serial input and eos was set\n");
ogg_stream_clear(&stream);
state.has_stream=false;
if (ogg_page_bos(&page)) {
FXint serial = ogg_page_serialno(&page);
GM_DEBUG_PRINT("[ogg] begin of new stream %d\n",serial);
ogg_stream_init(&stream,serial);
state.has_stream=true;
state.has_eos=false;
return true;
}
else {
GM_DEBUG_PRINT("[ogg] expected bos page not found\n");
}
}
else {
GM_DEBUG_PRINT("non-matching page %d (expected %ld)\n",ogg_page_serialno(&page),stream.serialno);
}
}
else {
if (ogg_page_bos(&page)) {
GM_DEBUG_PRINT("[ogg] beginning of stream\n");
FXint serial = ogg_page_serialno(&page);
ogg_stream_init(&stream,serial);
state.has_stream=true;
return true;
}
}
GM_DEBUG_PRINT("[ogg] failed to match page\n");
return false;
}
FXlong OggReader::find_lastpage_position() {
FXlong size = input->size();
FXlong pos = -1;
/// TODO need a smart way of finding the last page in stream.
if (size>=0xFFFF) {
input->position((size-0xFFFF),FXIO::Begin);
ogg_sync_reset(&sync);
}
/// Go to last page of stream to find out last pcm position
while(fetch_next_page()) {
pos=ogg_page_granulepos(&page);
if (ogg_page_eos(&page)) {
return pos;
}
}
/// eos not found, use last found pos
GM_DEBUG_PRINT("[ogg] no page found with eos. Truncated file?\n");
return pos;
}
#ifdef HAVE_OPUS
void OggReader::check_opus_length() {
stream_length=0;
stream_start=0;
if (!input->serial()) {
FXlong cpos = input_position;
FXlong nsamples = 0;
/// First determine the pcm offset at the start of a stream
while(fetch_next_packet()) {
nsamples += opus_packet_get_nb_samples((unsigned char*)op.packet,op.bytes,48000);
if (op.granulepos!=-1) {
stream_start=op.granulepos-nsamples;
GM_DEBUG_PRINT("[ogg] stream start=%ld %ld %ld\n",op.granulepos,nsamples,stream_start);
break;
}
}
FXASSERT(stream_start>=0);
/// Find end of stream
FXlong pos = find_lastpage_position();
if (pos>0) {
stream_length = pos - stream_start - stream_offset_start;
GM_DEBUG_STREAM_LENGTH("ogg",stream_length,af.rate);
}
input->position(cpos,FXIO::Begin);
ogg_sync_reset(&sync);
ogg_stream_reset(&stream);
}
// Mark beginning of stream
if (stream_start>0)
stream_position = stream_start;
else
stream_position = 0;
}
#endif
#if defined(HAVE_VORBIS) || defined(HAVE_TREMOR)
void OggReader::check_vorbis_length() {
stream_length=-1;
stream_start=0;
if (!input->serial()) {
FXlong cpos = input_position;
/// First Determine the pcm offset at the start of the stream
FXint cb,lb=-1,tb=0;
while(fetch_next_packet()) {
cb=vorbis.getBlockSize(op.packet);
if (lb!=-1) tb+=(lb+cb)>>2;
lb=cb;
if (op.granulepos!=-1) {
stream_start=op.granulepos-tb;
GM_DEBUG_PRINT("[ogg] stream offset=%ld %d %ld\n",op.granulepos,tb,stream_start);
break;
}
}
// handle negative offset
if (stream_start < 0) {
stream_offset_start = -stream_start;
}
/// Find end of stream
FXlong pos = find_lastpage_position();
if (pos>0) {
stream_length = pos - stream_start;
GM_DEBUG_STREAM_LENGTH("ogg",stream_length,af.rate);
}
input->position(cpos,FXIO::Begin);
ogg_sync_reset(&sync);
ogg_stream_reset(&stream);
}
else {
/// First Determine the pcm offset at the start of the stream
FXint cb,lb=-1,tb=0;
while(fetch_next_packet(true)) {
// cache this packet
cache_ogg_packet();
cb=vorbis.getBlockSize(op.packet);
if (lb!=-1) tb+=(lb+cb)>>2;
lb=cb;
if (op.granulepos!=-1) {
stream_start=op.granulepos-tb;
GM_DEBUG_PRINT("[ogg] stream offset=%ld %d %ld\n",op.granulepos,tb,stream_start);
break;
}
}
// handle negative offset
if (stream_start < 0) {
stream_offset_start = -stream_start;
}
}
// Mark beginning of stream
if (stream_start>0)
stream_position = stream_start;
else
stream_position = 0;
}
#endif
extern void ap_replaygain_from_vorbis_comment(ReplayGain & gain,const FXchar * comment,FXint len);
extern void ap_meta_from_vorbis_comment(MetaInfo * meta, const FXchar * comment,FXint len);
#if defined(HAVE_OPUS) || defined(HAVE_VORBIS) || defined(HAVE_TREMOR)
extern const FXuint vorbis_channel_map[];
extern void ap_parse_vorbiscomment(const FXuchar * buffer,FXint len,ReplayGain & gain,MetaInfo * meta);
#endif
#ifdef HAVE_OPUS
ReadStatus OggReader::parse_opus_stream() {
OpusConfig * opus_config = new OpusConfig;
if (op.bytes<19) {
GM_DEBUG_PRINT("[ogg] packet size too small for opushead\n");
return ReadError;
}
#if FOX_BIGENDIAN == 0
stream_offset_start = (op.packet[10] | op.packet[11]<<8);
#else
stream_offset_start = (op.packet[10]<<8 | op.packet[11]);
#endif
// channel mapping family
switch(op.packet[18]) {
// RTP mapping
case 0: af.set(AP_FORMAT_FLOAT,48000,op.packet[9]);
break;
// vorbis mapping family
case 1:
// Support 1-8 channels
if (op.packet[9]<1 || op.packet[9]>8)
return ReadError;
af.set(AP_FORMAT_FLOAT,48000,op.packet[9],vorbis_channel_map[op.packet[9]-1]);
// Make sure stream map is correct
if (af.channels!=op.bytes-21)
return ReadError;
break;
// Undefined, most players shouldn't play this
default: return ReadError;
}
opus_config->setOpusInfo(op.packet,op.bytes);
// OpusTags
if (!fetch_next_packet())
return ReadError;
if (FXString::compare((const FXchar*)op.packet,"OpusTags",8))
return ReadError;
codec=Codec::Opus;
ConfigureEvent * config = new ConfigureEvent(af,codec);
MetaInfo * meta = new MetaInfo();
ap_parse_vorbiscomment(op.packet+8,op.bytes-8,config->replaygain,meta);
config->dc = opus_config;
config->stream_offset_start = stream_offset_start;
// Now we are ready to init the decoder
context->post_configuration(config);
// Send Meta Info
context->post_meta(meta);
// find out stream length
check_opus_length();
// Success
flags|=FLAG_PARSED;
return ReadOk;
}
#endif
#if defined(HAVE_VORBIS) || defined(HAVE_TREMOR)
ReadStatus OggReader::parse_vorbis_stream() {
ReplayGain gain;
MetaInfo * meta = nullptr;
ConfigureEvent * config = nullptr;
// Store Vorbis Config
VorbisConfig * vorbis_config = new VorbisConfig();
//FXASSERT(state.has_packet);
FXASSERT(op.packet[0]==1);
// Vorbis Info Packet
#ifdef FOX_BIGENDIAN
FXuint rate = (op.packet[15] << 24 | op.packet[14] << 16 | op.packet[13] << 8 | op.packet[12]);
#else
FXuint rate = (op.packet[12] << 24 | op.packet[13] << 16 | op.packet[14] << 8 | op.packet[15]);
#endif
FXuchar channels = op.packet[11];
if (channels<1 || channels>8) {
goto x;
}
if (!vorbis.parseInfo(op.packet,op.bytes)) {
goto x;
}
vorbis_config->setVorbisInfo(op.packet,op.bytes);
// Vorbis Comment Packet
if (!fetch_next_packet()) {
goto x;
}
// Check make sure its a comment packet
if (FXString::compare((const FXchar*)op.packet,"\x03""vorbis",7)) {
goto x;
}
// Parse the vorbis comments for useful information
meta = new MetaInfo();
ap_parse_vorbiscomment(op.packet+8,op.bytes-8,gain,meta);
// Vorbis Setup Packet
if (!fetch_next_packet()) {
goto x;
}
// Check make sure its the correct packet
if (FXString::compare((const FXchar*)op.packet,"\x05""vorbis",7)) {
goto x;
}
// Parse Setup Packet
if (!vorbis.parseSetup(op.packet,op.bytes)) {
goto x;
}
vorbis_config->setVorbisSetup(op.packet,op.bytes);
af.set(AP_FORMAT_FLOAT,rate,channels,vorbis_channel_map[channels-1]);
codec=Codec::Vorbis;
config = new ConfigureEvent(af,codec);
config->dc = vorbis_config;
config->replaygain = gain;
// Check vorbis length
check_vorbis_length();
config->stream_offset_start = stream_offset_start;
config->stream_offset_end = stream_offset_end;
// Initialize decode
context->post_configuration(config);
// Post meta information
context->post_meta(meta);
// Success
flags|=FLAG_PARSED;
return ReadOk;
x:
GM_DEBUG_PRINT("[ogg] failed to parse vorbis stream\n");
vorbis.clear();
if (meta) meta->unref();
delete vorbis_config;
return ReadError;
}
#endif
#ifdef HAVE_FLAC
extern void flac_parse_vorbiscomment(const FXuchar * buffer,FXint len,ReplayGain & gain,MetaInfo * meta);
extern FXbool flac_parse_streaminfo(const FXuchar * buffer,AudioFormat & config,FXlong & nframes);
extern FXbool flac_audioformat(const FXuchar * info,AudioFormat & af,FXlong & stream_length);
ReadStatus OggReader::parse_flac_stream() {
// First packet with StreamInfo
if (op.bytes!=51)
return ReadError;
// Packet Type
if (op.packet[0]!=0x7f)
return ReadError;
// Check Ogg Flac mapping version
if (op.packet[5]!=0x01 || op.packet[6]!=0x00)
return ReadError;
// FLAC Signature
if (op.packet[9]!='f' || op.packet[10]!='L' || op.packet[11]!='a' || op.packet[12]!='C')
return ReadError;
// Check for Metadata Block
if ((op.packet[13]&0x7f)!=0)
return ReadError;
// Check metablock size
if (34!=((FXuint)op.packet[14]<<16 | ((FXuint)op.packet[15]<<8) | ((FXuint)op.packet[16])))
return ReadError;
// Get audio format from header
if (!flac_audioformat(op.packet+27,af,stream_length))
return ReadError;
// Get VorbisComments
if (!fetch_next_packet())
return ReadError;
codec=Codec::FLAC;
ConfigureEvent * config = new ConfigureEvent(af,codec,stream_length);
MetaInfo * meta = new MetaInfo;
flac_parse_vorbiscomment(op.packet,op.bytes,config->replaygain,meta);
// Initialize decode
context->post_configuration(config);
// Post meta information
context->post_meta(meta);
flags|=FLAG_PARSED;
return ReadOk;
}
#endif
ReadStatus OggReader::parse() {
if (input_position==-1)
input_position = input->position();
if (!state.has_packet && !fetch_next_packet()){
GM_DEBUG_PRINT("[ogg] parse failure\n");
return ReadError;
}
#if defined(HAVE_VORBIS) || defined(HAVE_TREMOR)
if (FXString::compare((const FXchar*)op.packet,"\x01""vorbis",7)==0) {
return parse_vorbis_stream();
}
#endif
#ifdef HAVE_OPUS
if (FXString::compare((const FXchar*)op.packet,"OpusHead",8)==0){
return parse_opus_stream();
}
#endif
#ifdef HAVE_FLAC
if (FXString::compare((const FXchar*)op.packet,"\x7f""FLAC",5)==0) {
return parse_flac_stream();
}
#endif
GM_DEBUG_PRINT("[ogg] unknown ogg header %hhd -> %s\n",op.packet[0],(const FXchar*)&op.packet[1]);
return ReadError;
}
void OggReader::cache_ogg_packet() {
FXuint nbytes = op.bytes;
cached_packets.append(&nbytes,4);
cached_packets.append(op.packet,op.bytes);
}
void OggReader::submit_ogg_packet() {
FXuint nbytes;
state.has_packet=true;
// Make sure data fits into packet or allow partial if it exceeds capacity
if ((packet->space()>=(op.bytes+4)) ||
((packet->space()>=4) && ((op.bytes+4)>packet->capacity()))) {
// write packet size
if (state.header_written==false) {
if (codec==Codec::Vorbis || codec==Codec::Opus) {
nbytes = op.bytes;
packet->append(&nbytes,4);
state.header_written=true;
if (stream_position!=-1) {
packet->stream_position=stream_position;
stream_position=-1;
}
}
else {
// no
state.header_written=true;
}
}
// write packet data
nbytes = FXMIN((op.bytes-state.bytes_written),packet->space());
if (nbytes) {
packet->append(&op.packet[state.bytes_written],nbytes);
state.bytes_written+=nbytes;
}
// Check if we're done with the packet.
if (state.header_written && state.bytes_written==op.bytes) {
state.header_written=false;
state.bytes_written=0;
state.has_packet=false;
if (op.e_o_s) {
packet->flags|=FLAG_EOS;
state.has_eos=true;
}
}
if (packet->flags&FLAG_EOS) {
packet->af=af;
packet->stream_length=stream_length;
context->post_packet(packet);
packet=nullptr;
}
}
else {
packet->af=af;
packet->stream_length=stream_length;
context->post_packet(packet);
packet=nullptr;
}
}
FXbool OggReader::fetch_next_page() {
/*
// Available from Ogg 1.1.4
if(ogg_sync_check(&sync))
return false;
*/
while(1) {
long result=ogg_sync_pageseek(&sync,&page);
if (result>0) { /// Return page with size result
input_position+=result;
if (match_page()) {
return true;
}
}
else if (result==0) { /// Need more bytes
FXchar * buffer = ogg_sync_buffer(&sync,BUFFERSIZE);
if (buffer==nullptr) return false;
FXival nbytes = input->read(buffer,BUFFERSIZE);
if (nbytes<=0) return false;
ogg_sync_wrote(&sync,nbytes);
}
else { // skipped some bytes
input_position+=(-result);
}
}
return false;
}
FXbool OggReader::fetch_next_packet(FXbool nocache/*=false*/) {
FXint result;
while(1) {
if (__unlikely(cached_packets.size() && nocache==false)) {
op.packetno = 0;
op.granulepos = -1;
op.b_o_s = 0;
op.e_o_s = 0;
FXuint nbytes;
cached_packets.read(&nbytes,4);
op.bytes=nbytes;
cached_packets.read(op.packet,op.bytes);
return true;
}
if (state.has_page==false) {
if (!fetch_next_page()){
return false;
}
if (ogg_stream_pagein(&stream,&page))
return false;
// For streaming check for eos and determine stream_length)
if (stream_length==-1 && ogg_page_eos(&page)) {
stream_length = ogg_page_granulepos(&page);
}
state.has_page=true;
}
do {
result=ogg_stream_packetout(&stream,&op);
if (result==1) {
return true;
}
}
while(result==-1);
state.has_page=false;
}
return false;
}
ReadStatus OggReader::process(Packet * p) {
packet = p;
packet->stream_position = -1;
if (__unlikely((flags&FLAG_PARSED)==0)) {
if (parse()!=ReadOk)
return ReadError;
if (__unlikely((flags&FLAG_PARSED)==0))
return ReadOk;
}
if (state.has_packet)
submit_ogg_packet();
while(packet) {
if (!fetch_next_packet()){
GM_DEBUG_PRINT("[ogg] unexpected end of stream\n");
packet->flags|=FLAG_EOS;
state.has_eos=true;
context->post_packet(packet);
return ReadDone;
}
submit_ogg_packet();
}
if (input->serial() && state.has_eos && !input->eof()) {
flags&=~(FLAG_PARSED|FLAG_OGG_VORBIS|FLAG_OGG_FLAC|FLAG_OGG_OPUS|FLAG_VORBIS_MASK);
if (state.has_stream) {
ogg_stream_clear(&stream);
state.has_stream=false;
}
state.has_eos=false;
return ReadOk;
}
if (state.has_eos)
return ReadDone;
else
return ReadOk;
}
ReaderPlugin * ap_ogg_reader(InputContext * ctx) {
return new OggReader(ctx);
}
}
|