static int Init(ape* p) { format_stream* s; p->Format.HeaderLoaded = 1; p->Format.TimeStamps = 0; if (!Load(p)) return ERR_NOT_SUPPORTED; p->Samples = 0; s = Format_AddStream(&p->Format,sizeof(format_stream)); if (s) { PacketFormatClear(&s->Format); s->Format.Type = PACKET_AUDIO; s->Format.Format.Audio.Format = AUDIOFMT_PCM; s->Format.Format.Audio.Bits = p->Decoder->GetInfo(APE_INFO_BITS_PER_SAMPLE, 0, 0); s->Format.Format.Audio.SampleRate = p->Decoder->GetInfo(APE_INFO_SAMPLE_RATE, 0, 0); s->Format.Format.Audio.Channels = p->Decoder->GetInfo(APE_INFO_CHANNELS, 0, 0); PacketFormatDefault(&s->Format); s->Format.ByteRate = p->Decoder->GetInfo(APE_INFO_AVERAGE_BITRATE, 0, 0) * 125; s->Fragmented = 1; s->DisableDrop = 1; p->Format.Duration = (tick_t)(((int64_t)p->Decoder->GetInfo(APE_INFO_LENGTH_MS, 0, 0) / 1000) * TICKSPERSEC); Format_PrepairStream(&p->Format,s); p->SampleRate = s->Format.Format.Audio.SampleRate; p->SampleSize = p->Decoder->GetInfo(APE_INFO_BYTES_PER_SAMPLE, 0, 0) * s->Format.Format.Audio.Channels; } //p->blockSize = p->Decoder->GetInfo(APE_INFO_BLOCK_ALIGN, 0, 0); p->BufferLen = p->SampleSize * 1024;//p->blockSize * 1024; /////?????? p->Buffer = (uint8_t*)malloc(p->SampleSize*p->BufferLen); if (!p->Buffer) return ERR_OUT_OF_MEMORY; return ERR_NONE; }
static void AudioTrack(format_stream* s,TrackInfo* Info,int Format,bool_t PacketBased) { PacketFormatClear(&s->Format); s->Format.Type = PACKET_AUDIO; s->Format.Format.Audio.Format = Format; Info->AV.Audio.Channels; s->Format.Format.Audio.Channels = Info->AV.Audio.Channels; //Mod2010: s->Format.Format.Audio.SampleRate = mkv_TruncFloat(Info->AV.Audio.SamplingFreq); //Mod2010: s->Format.Format.Audio.Bits = Info->AV.Audio.BitDepth; //Mod2010: s->Fragmented = !PacketBased; if (PacketBased) s->Format.Format.Audio.Flags |= PCM_PACKET_BASED; PacketFormatDefault(&s->Format); }
static int Init(asap* p) { format_stream* s; p->Format.HeaderLoaded = 1; p->Format.TimeStamps = 0; p->Format.Duration = 60*5*TICKSPERSEC; p->Total = Scale(p->Format.Duration,SAMPLE_RATE,TICKSPERSEC); ASAP_Initialize(SAMPLE_RATE, AUDIO_FORMAT_S16_NE, 3); if (!Load(p)) return ERR_NOT_SUPPORTED; p->Buffer = malloc(BUFFER_SIZE); if (!p->Buffer) return ERR_OUT_OF_MEMORY; s = Format_AddStream(&p->Format,sizeof(format_stream)); if (!s) return ERR_OUT_OF_MEMORY; PacketFormatClear(&s->Format); s->Format.Type = PACKET_AUDIO; s->Format.Format.Audio.Format = AUDIOFMT_PCM; s->Format.Format.Audio.Bits = 16; s->Format.Format.Audio.SampleRate = SAMPLE_RATE; s->Format.Format.Audio.Channels = ASAP_GetChannels(); PacketFormatDefault(&s->Format); s->Fragmented = 1; s->DisableDrop = 1; p->SamplePerBuffer = BUFFER_SIZE/2/s->Format.Format.Audio.Channels; Format_PrepairStream(&p->Format,s); return ERR_NONE; }
static int FillQueue(ogg* p,format_reader* Reader) { for (;;) { int Bytes = ogg_sync_pageseek(p->OggSync,&p->OggPage); if (Bytes == 0) // need more data { int Result; format_buffer* Buffer; if (!Reader->BufferAvailable && (!p->Format.SyncMode || p->Format.SyncRead<=0)) return ERR_NEED_MORE_DATA; Buffer = Format_BufferRemove(Reader); if (!Buffer && p->Format.SyncMode && p->Format.SyncRead>0 && Format_ReadBuffer(Reader,0)) Buffer = Format_BufferRemove(Reader); Result = AddBuffer(p,Buffer); if (Result != ERR_NONE) return Result; } else if (Bytes < 0) Reader->FilePos -= Bytes; else if (Bytes > 0) { int StreamNo; oggstream* s; int Id; Reader->FilePos += Bytes; Id = ogg_page_serialno(&p->OggPage); DEBUG_MSG4(DEBUG_FORMAT,T("OGG Page id:%d size:%d gran:%d filepos:%d"),Id,p->OggPage.body_len,(int)ogg_page_granulepos(&p->OggPage),Reader->FilePos - Bytes); for (StreamNo=0;StreamNo<p->Format.StreamCount;++StreamNo) if (p->Format.Streams[StreamNo]->Id == Id) break; if (StreamNo==p->Format.StreamCount) { // check for restarted audio http streaming (comments changed) if (p->Format.StreamCount==1 && p->Format.Streams[0]->Format.Type == PACKET_AUDIO && p->Format.Streams[0]->LastTime>0) { StreamNo = 0; s = (oggstream*) p->Format.Streams[0]; if (s->Vorbis) { // vorbis decoder have to release s->Info s->Stream.Format.Extra = NULL; s->Stream.Format.ExtraLength = 0; ConnectionUpdate((node*)&p->Format,FORMAT_STREAM+0,s->Stream.Pin.Node,s->Stream.Pin.No); } FreeStream(p,s); } else { s = (oggstream*) Format_AddStream(&p->Format,sizeof(oggstream)); if (!s) continue; } // init stream s->Stream.Id = Id; s->OggStream = ogg_stream_create(Id); s->NeedMorePage = 1; s->MediaTime = 0; s->Invalid = 0; s->Vorbis = 0; s->Native = 0; s->PacketNo = 0; vorbis_info_init(&s->Info); vorbis_comment_init(&s->Comment); } s = (oggstream*) p->Format.Streams[StreamNo]; if (s->Invalid) // drop invalid streams continue; if (s->PacketNo>=3) { if (!s->Stream.Pin.Node) // drop unused streams continue; if (p->Format.InSeek) { // reftime needed for SeekByPacket if ((s->MediaTime = ogg_page_granulepos(&p->OggPage)) != -1) { // no need for GlobalOffset here s->Stream.LastTime = (tick_t)(s->MediaTime * s->MediaRateDen / s->MediaRateNum); if (s->Stream.Format.Type == PACKET_AUDIO) { s->Stream.LastTime += p->Format.AVOffset; if (s->Stream.LastTime < 0) s->Stream.LastTime = 0; } } } } // add page to stream if (ogg_stream_pagein(s->OggStream,&p->OggPage) >= 0) { if (s->PacketNo<3) // header packet needed? { int i = ogg_stream_packetout(s->OggStream,&s->OggPacket); if (i == 0) // stream needs more pages continue; if (++s->PacketNo==1) // first packet? { ogg_reference* Ref; const void* Data; int Length; if (i < 0) { // first header packet is a must have s->Invalid = 1; continue; } if (p->Format.UseBufferBlock) { for (Length=0,Ref=s->OggPacket.packet;Ref;Ref=Ref->next) Length += Ref->length; if (s->Stream.BufferBlockLength<Length && !Format_AllocBufferBlock(&p->Format,&s->Stream,Length)) { Length = 0; Data = NULL; } else { for (Length=0,Ref=s->OggPacket.packet;Ref;Ref=Ref->next) { WriteBlock(&s->Stream.BufferBlock,Length,Ref->buffer->data + Ref->begin,Ref->length); Length += Ref->length; } Data = s->Stream.BufferBlock.Ptr; } } else { BufferDrop(&s->Stream.BufferMem); for (Ref=s->OggPacket.packet;Ref;Ref=Ref->next) BufferWrite(&s->Stream.BufferMem,Ref->buffer->data + Ref->begin, Ref->length, 16384); Data = s->Stream.BufferMem.Data; Length = s->Stream.BufferMem.WritePos; } if (OGMHeader(p,s,(char*)Data,Length) || SpeexHeader(p,s,(char*)Data,Length)) { PacketFormatDefault(&s->Stream.Format); s->PacketNo = 3; // no more headers } else if (!VorbisHeader(p,s)) { s->Invalid = 1; continue; } while (s->MediaRateNum > (1<<30)) { s->MediaRateDen >>= 1; s->MediaRateNum >>= 1; } Format_PrepairStream(&p->Format,&s->Stream); continue; } else { assert(s->Vorbis); // error in second or third header packet will not cause fatal error vorbis_synthesis_headerin(&s->Info,&s->Comment,&s->OggPacket); if (s->PacketNo == 3) { // got the three header packets: reinit codec with vorbis_info s->Stream.Format.Extra = &s->Info; s->Stream.Format.ExtraLength = -1; ConnectionUpdate((node*)&p->Format,FORMAT_STREAM+StreamNo,s->Stream.Pin.Node,s->Stream.Pin.No); SendComments(s); } continue; } }
static int Init(tta* p) { int ret; format_stream* s; p->Format.HeaderLoaded = 1; p->Format.TimeStamps = 0; p->Decoder.Reader = p->Format.Reader; ret = open_tta_file(&p->Decoder, 0); if (ret != 0) return ERR_INVALID_DATA; ret = player_init(&p->Decoder); if (ret != 0) return ERR_INVALID_DATA; p->Samples = 0; s = Format_AddStream(&p->Format,sizeof(format_stream)); if (s) { PacketFormatClear(&s->Format); s->Format.Type = PACKET_AUDIO; s->Format.Format.Audio.Format = AUDIOFMT_PCM; s->Format.Format.Audio.Bits = p->Decoder.BPS; s->Format.Format.Audio.SampleRate = p->Decoder.SAMPLERATE; s->Format.Format.Audio.Channels = p->Decoder.NCH; PacketFormatDefault(&s->Format); s->Format.ByteRate = (int)p->Decoder.BITRATE * 125; s->PacketBurst = 1; s->Fragmented = 1; s->DisableDrop = 1; p->Format.Duration = (tick_t)(((int64_t)p->Decoder.DATALENGTH * TICKSPERSEC) / s->Format.Format.Audio.SampleRate); Format_PrepairStream(&p->Format,s); if (p->Format.Comment.Node) { // id3v1 format_reader* Reader = p->Format.Reader; char Buffer[ID3V1_SIZE]; filepos_t Save = Reader->Input->Seek(Reader->Input,0,SEEK_CUR); if (Save>=0 && Reader->Input->Seek(Reader->Input,-(int)sizeof(Buffer),SEEK_END)>=0) { if (Reader->Input->Read(Reader->Input,Buffer,sizeof(Buffer)) == sizeof(Buffer)) ID3v1_Parse(Buffer,&p->Format.Comment); Reader->Input->Seek(Reader->Input,Save,SEEK_SET); } } p->SampleRate = s->Format.Format.Audio.SampleRate; p->SampleSize = p->Decoder.BSIZE * s->Format.Format.Audio.Channels; } p->BufferLen = PCM_BUFFER_LENGTH; p->Buffer = (uint8_t*)malloc(p->SampleSize*p->BufferLen); if (!p->Buffer) return ERR_OUT_OF_MEMORY; return ERR_NONE; }