Exemple #1
0
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;
}
Exemple #2
0
int RawAudioInit(rawaudio* p)
{
	format_stream* s = Format_AddStream(&p->Format,sizeof(format_stream));
	p->Head = 0;
	if (s)
	{
		PacketFormatClear(&s->Format);
		s->Format.Type = PACKET_AUDIO;
		s->Format.Audio.Format = p->Type;
		s->PacketBurst = 1;
		s->Fragmented = 1;
		s->DisableDrop = 1;
		Format_PrepairStream(&p->Format,s);

		if (s->Comment.Node)
		{
			format_reader* Reader = p->Format.Reader;
			// id3v1
			char Buffer[ID3TAG1SIZE];
			char Head[ID3TAGQUERYSIZE];
			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))
					Id3TagParse(Buffer,sizeof(Buffer),&s->Comment);

				Reader->Input->Seek(Reader->Input,Save,SEEK_SET);
			}

			// id3v2
			if (Reader->Read(Reader,Head,sizeof(Head))==sizeof(Head))
			{
				int TagSize = Id3TagQuery(Head,sizeof(Head));
				if (TagSize > 0)
				{
					void* Buffer = malloc(TagSize);
					if (Buffer)
					{
						int Len = Reader->Read(Reader,Buffer,TagSize);
						Id3TagParse(Buffer,Len,&s->Comment);
						free(Buffer);
					}
					p->Head = TagSize;
				}
			}

			Reader->Seek(Reader,p->Head,SEEK_SET);
		}
	}

	p->SeekPos = p->Head;
	return ERR_NONE;
}
Exemple #3
0
static int Init(rawimage* p)
{
	format_stream* s = Format_AddStream(&p->Format,sizeof(format_stream));
	if (s)
	{
		PacketFormatClear(&s->Format);
		s->Format.Type = PACKET_VIDEO;
		s->Format.Format.Video.Pixel.Flags = PF_FOURCC; //|PF_NOPREROTATE;
		s->Format.Format.Video.Pixel.FourCC = p->FourCC;
		s->PacketBurst = 1;
		s->DisableDrop = 1;
		Format_PrepairStream(&p->Format,s);
	}
	p->Total = 0;
	p->Format.HeaderLoaded = 1;
	p->Format.TimeStamps = 0;
	return ERR_NONE;
}
Exemple #4
0
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;
}
Exemple #5
0
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;
					}
				}
Exemple #6
0
static int ReadPacket(flv* p, format_reader* Reader, format_packet* Packet)
{
	format_stream* s;
    int		i, type, size, TimeStamp, flags, is_audio;
	int64_t next;
	bool_t	isNewStream = 0;
	filepos_t	startfilepos;
	
	startfilepos = Reader->FilePos;

	for (;;) 
	{
		Reader->Skip(Reader, 4);//prvious type size
		type = Reader->Read8(Reader);
		size = ReadBE24(Reader);
		TimeStamp = ReadBE24(Reader);
		if (Reader->Eof(Reader))
		{
			return ERR_END_OF_FILE;
		}

		// Reserved
		Reader->Skip(Reader, 4);

		flags = 0;

		if (size == 0)
			continue;

		next = size + Reader->FilePos;

		if (type == FLV_TAG_TYPE_AUDIO)
		{
			is_audio = 1;
			flags = Reader->Read8(Reader);
			size--;
			p->bHasA = 1;
		} 
		else if (type == FLV_TAG_TYPE_VIDEO)
		{
			is_audio = 0;
			flags = Reader->Read8(Reader);
			size--;
			p->bHasV = 1;
		}
		else if (type == FLV_TAG_TYPE_META && size > 13+1+4)
		{
			if(flv_read_metabody(p,Reader, next)<0)
			{
				Reader->Seek(Reader,(filepos_t)next,SEEK_SET);
			}
			continue;
		}
		else
		{
			// Skip the packet
			Reader->Skip(Reader, size);
			continue;
		}

		// Now find stream
		s = NULL;
		for (i=0;i<p->Format.StreamCount;++i)
		{
			if (p->Format.Streams[i]->Id == is_audio)
			{
				s = p->Format.Streams[i];
				break;
			}
		}

		if (!s)
		{
			s = Format_AddStream(&p->Format,sizeof(format_stream));
	        if (!s)
		        return ERR_OUT_OF_MEMORY;

			s->Id = is_audio;
			isNewStream = 1;

			s->Format.PacketRate.Den = 24;
			s->Format.PacketRate.Num = 1000;
		}

		break;
	}

    if(is_audio)
	{
		if (isNewStream)
		{
			int bInited = 0;

			PacketFormatClear(&s->Format);
			s->Format.Type = PACKET_AUDIO;
			s->Format.Format.Audio.Channels = (flags&1)+1;
			if((flags >> 4) == 5)
				s->Format.Format.Audio.SampleRate = 8000;
			else
				s->Format.Format.Audio.SampleRate = (44100<<((flags>>2)&3))>>3;

			switch(flags >> 4)
			{/* 0: uncompressed 1: ADPCM 2: mp3 5: Nellymoser 8kHz mono 6: Nellymoser */
			case FLV_CODECID_PCM:
				s->Format.Format.Audio.Format = AUDIOFMT_PCM;
				if (!(flags&2))
				{
					s->Format.Format.Audio.Bits = 8;
					s->Format.Format.Audio.FracBits = 7;
					bInited = 1;
				}
				break;
			case FLV_CODECID_ADPCM:
				s->Format.Format.Audio.Format = AUDIOFMT_ADPCM_SWF;
				break;
			case FLV_CODECID_MP3: 
				s->Format.Format.Audio.Format = AUDIOFMT_MP3;
				break;
			case FLV_CODECID_PCM_LE:
				s->Format.Format.Audio.Format = AUDIOFMT_PCM;
				if (!(flags&2))
				{
					s->Format.Format.Audio.Bits = 8;
					s->Format.Format.Audio.FracBits = 7;
					bInited = 1;
				}
				break;
			case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
				s->Format.Format.Audio.SampleRate = 8000;
			case FLV_CODECID_NELLYMOSER:
				s->Format.Format.Audio.Format = AUDIOFMT_NELLYMOSER;
				break;
			case FLV_CODECID_AAC:
				{
					format_stream* s;
					int type = Reader->Read8(Reader);
					size--;
					if(type == 0)
					{
						s = p->Format.Streams[p->Format.StreamCount-1];

						if (PacketFormatExtra(&s->Format,size))
							Reader->Read(Reader,s->Format.Extra,size);
					}
					s->Format.Format.Audio.Format = AUDIOFMT_AAC;
					Format_PrepairStream(&p->Format, s);

				}
				return ERR_NONE;
			case FLV_CODECID_SPEEX:
				s->Format.Format.Audio.Format = AUDIOFMT_SPEEX;
				s->Format.Format.Audio.SampleRate = 16000;
				break;
			default:
				return ERR_NOT_SUPPORTED;
			}
			if(!bInited)
			{
				s->Format.Format.Audio.Bits = 16;
				s->Format.Format.Audio.FracBits = 15;
			}
			s->Format.ByteRate = s->Format.Format.Audio.SampleRate * s->Format.Format.Audio.Channels * 2;
			Format_PrepairStream(&p->Format,s);
		}
    }
Exemple #7
0
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;
}