static int UpdateInput(codec* p) { int ResultOutput; int Result = ERR_NONE; Flush(p); if (!PacketFormatMatch(p->Node.Class, &p->In.Format)) ClearInput(p); if (p->In.Format.Type != PACKET_NONE && p->Process) p->In.Process = (packetprocess)Process; // using default codec process if (p->UpdateInput) Result = p->UpdateInput(p); if (Result != ERR_NONE) ClearInput(p); ResultOutput = ConnectionUpdate(&p->Node,CODEC_OUTPUT,p->Out.Pin.Node,p->Out.Pin.No); if (Result == ERR_NONE) Result = ResultOutput; return Result; }
static int Process( ffmpeg_video* p, const packet* Packet, const flowstate* State ) { int Picture; int Len; if (Packet) { if (State->DropLevel) { if (State->DropLevel>1) { p->SkipToKey = 1; p->DropToKey = 1; p->Dropping = 1; p->Context->hurry_up = 5; } else p->Context->hurry_up = 1; if (!SupportDrop(p)) p->Context->hurry_up = 0; } else p->Context->hurry_up = 0; if (!Packet->Key && p->DropToKey) { if (p->Dropping) { flowstate DropState; DropState.CurrTime = TIME_UNKNOWN; DropState.DropLevel = 1; p->Codec.Out.Process(p->Codec.Out.Pin.Node,NULL,&DropState); } if (SupportDrop(p)) avcodec_flush_buffers(p->Context); return ERR_DROPPING; } if (p->DropToKey) p->DropToKey = 0; if (Packet->RefTime >= 0) p->Codec.Packet.RefTime = Packet->RefTime; BufferPack(&p->Buffer,0); if(p->Codec.In.Pin.Node->Class==FOURCC('R','V','_','0')&& (p->Codec.In.Format.Format.Video.Pixel.FourCC == FOURCC('R','V','1','0')|| p->Codec.In.Format.Format.Video.Pixel.FourCC == FOURCC('R','V','2','0')|| p->Codec.In.Format.Format.Video.Pixel.FourCC == FOURCC('R','V','3','0')|| p->Codec.In.Format.Format.Video.Pixel.FourCC == FOURCC('R','V','4','0'))) { int32_t ret = rm_assemble_video_frame(p,Packet); if(ret != ERR_NONE) return ERR_NEED_MORE_DATA; } else { BufferWrite(&p->Buffer,Packet->Data[0],Packet->Length,2048); } } else { if (p->FrameTime<0) p->Codec.Packet.RefTime = TIME_UNKNOWN; else if (!State) p->Codec.Packet.RefTime += p->FrameTime; if (!State && p->Buffer.WritePos == p->Buffer.ReadPos) return ERR_NEED_MORE_DATA; } if (p->SkipToKey) p->Picture->pict_type = 0; Len = avcodec_decode_video(p->Context, p->Picture, &Picture, p->Buffer.Data + p->Buffer.ReadPos, p->Buffer.WritePos - p->Buffer.ReadPos); if (Len < 0) { BufferDrop(&p->Buffer); return ERR_INVALID_DATA; } p->Buffer.ReadPos += Len; if (!Picture) { if (p->SkipToKey>1 && p->Picture->pict_type) --p->SkipToKey; return ERR_NEED_MORE_DATA; } if (p->SkipToKey>0) { if ((!p->Picture->key_frame && p->Picture->pict_type) || p->SkipToKey>1) { if (p->SkipToKey>1) --p->SkipToKey; if (p->Dropping) { flowstate DropState; DropState.CurrTime = TIME_UNKNOWN; DropState.DropLevel = 1; p->Codec.Out.Process(p->Codec.Out.Pin.Node,NULL,&DropState); } return ERR_DROPPING; } p->SkipToKey = 0; } if (p->Context->pix_fmt != p->PixelFormat || p->Context->sample_aspect_ratio.num != p->Aspect.num || p->Context->sample_aspect_ratio.den != p->Aspect.den || p->Context->width != p->Codec.Out.Format.Format.Video.Width || p->Context->height != p->Codec.Out.Format.Format.Video.Height || p->Picture->linesize[0] != p->Codec.Out.Format.Format.Video.Pitch) { if (!BuildOutputFormat(p)) return ERR_INVALID_DATA; ConnectionUpdate(&p->Codec.Node,CODEC_OUTPUT,p->Codec.Out.Pin.Node,p->Codec.Out.Pin.No); } p->Codec.Packet.Data[0] = p->Picture->data[0]; p->Codec.Packet.Data[1] = p->Picture->data[1]; p->Codec.Packet.Data[2] = p->Picture->data[2]; 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; } }