/* Read until we get a frame, EOF or error.  Return -1 on error, 0 on EOF, 1 if we have a frame. */
int FFMpeg_Helper::GetFrame()
{
	while( 1 )
	{
		int ret = DecodePacket();
		if( ret == 1 )
			break;
		if( ret == -1 )
			return -1;
		if( ret == 0 && eof > 0 )
			return 0; /* eof */

		ASSERT( ret == 0 );
		ret = ReadPacket();
		if( ret < 0 )
			return ret; /* error */
	}

	++FrameNumber;

	if( FrameNumber == 1 )
	{
		/* Some videos start with a timestamp other than 0.  I think this is used
		 * when audio starts before the video.  We don't want to honor that, since
		 * the DShow renderer doesn't and we don't want to break sync compatibility.
		 *
		 * Look at the second frame.  (If we have B-frames, the first frame will be an
		 * I-frame with the timestamp of the next P-frame, not its own timestamp, and we
		 * want to ignore that and look at the next B-frame.) */
		const float expect = LastFrameDelay;
		const float actual = CurrentTimestamp;
		if( actual - expect > 0 )
		{
			LOG->Trace("Expect %f, got %f -> %f", expect, actual, actual - expect );
			TimestampOffset = actual - expect;
		}
	}

	return 1;
}
Example #2
0
/*****************************************************************************
 * ProcessPacket: processes a kate packet.
 *****************************************************************************/
static subpicture_t *ProcessPacket( decoder_t *p_dec, kate_packet *p_kp,
                            block_t **pp_block )
{
    decoder_sys_t *p_sys = p_dec->p_sys;
    block_t *p_block = *pp_block;
    subpicture_t *p_buf = NULL;

    /* Date management */
    if( p_block->i_pts > VLC_TS_INVALID && p_block->i_pts != p_sys->i_pts )
    {
        p_sys->i_pts = p_block->i_pts;
    }

    *pp_block = NULL; /* To avoid being fed the same packet again */

#ifdef ENABLE_PACKETIZER
    if( p_sys->b_packetizer )
    {
        /* Date management */
        p_block->i_dts = p_block->i_pts = p_sys->i_pts;

        if( p_sys->i_headers >= p_sys->i_num_headers )
            p_block->i_length = p_sys->i_pts - p_block->i_pts;
        else
            p_block->i_length = 0;

        p_buf = p_block;
    }
    else
#endif
    {
        p_buf = DecodePacket( p_dec, p_kp, p_block );

        if( p_block )
            block_Release( p_block );
    }

    return p_buf;
}
Example #3
0
int Play(Context *pContext)
{
    AVFormatContext *pFormatCtx = pContext->pFormatCtx;
    AVCodecContext *pCodecCtx = pContext->pCodecCtx;
    AVPacket packet;
    int numBytes;
    uint8_t *buffer;
    AVFrame *pFrame = av_frame_alloc();
    AVFrame *pFrameRGB = av_frame_alloc();
    numBytes = avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height);
    buffer = (uint8_t *) av_malloc(numBytes * sizeof(uint8_t));
    avpicture_fill((AVPicture *) pFrameRGB, buffer, PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height);
    for(;;) {
        if(av_read_frame(pFormatCtx, &packet) < 0)
        {
            fprintf(stderr, "Couldn't read frame\n");
            return 1;
        }
        DecodePacket(pContext, pFrame, pFrameRGB, &packet);
        av_free_packet(&packet);
    }
}
Example #4
0
/* Read until we get a frame, EOF or error.  Return -1 on error, 0 on EOF, 1 if we have a frame. */
int MovieDecoder_FFMpeg::DecodeFrame( float fTargetTime )
{
	//hack to filter out stuttering
	if(fTargetTime<m_fLastFrame)
	{
		fTargetTime=m_fLastFrame;
	}
	else
	{
		m_fLastFrame=fTargetTime;
	}

	for(;;)
	{
		int ret = DecodePacket( fTargetTime );

		if( ret == 1 )
		{
			return 1;
		}
		if( ret == -1 )
		{
			return -1;
		}
		if( ret == 0 && m_iEOF > 0 )
		{
			return 0; /* eof */
		}
		ASSERT( ret == 0 );
		ret = ReadPacket();
		if( ret < 0 )
		{
			return ret; /* error */
		}
	}
}
//从队列中取下数据包,并解码的线程
/*UINT*/VOID
DecodeThreadFunction(void*)
{
	for(;;)
	{
		//适当休息一下
		/*不能简单地休息一段时间,必须有控制,否则会丢包*/
		
		unsigned char EventWrite[MY_MTU];

		EnterCriticalSection(&Global.cs_pq);
	    P_PACKET pPacket=Global.PacketQueue->DeQueue();
		LeaveCriticalSection(&Global.cs_pq);

	    if(pPacket==NULL)
		{
		    //WaitForSingleObject(Global.ListenerInfo->DecodeWaiter, 10);
			Sleep(1);
			continue;
		}

		else
		{
			memcpy(EventWrite, pPacket->pPacketContent, pPacket->PacketLenth);
		    free(pPacket->pPacketContent);

		    //进行包的解析,目前只支持EtherNet链路层格式和IP高层协议
            DecodePacket(EventWrite, pPacket->PacketLenth);
		}
	}

	CloseHandle(Global.ListenerInfo->DecodeWaiter);
	SetEvent(Global.ListenerInfo->DecodeEvent);
	//_endthread();
	return /*1*/;
}
Example #6
0
int OpAcidDb_Log(void *context, void *data)
{
    char timestamp[TIMEBUF_SIZE];
    Sid *sid = NULL;
    ClassType *class_type;
    UnifiedLogRecord *record = (UnifiedLogRecord *)data; 
    OpAcidDb_Data *op_data = (OpAcidDb_Data *)context;
    u_int32_t acid_sig_id;
    Packet p;

#if 0 /* this is broken */
    /* skip tagged packets, since the db does not have a mechanism to 
     * deal with them properly
     */
    if(record->log.event.event_reference)
    {
        LogMessage("Skipping tagged packet %i\n", record->log.event.event_reference);
        return 0;
    }
#endif
    

    RenderTimestamp(record->log.pkth.ts.tv_sec, timestamp, TIMEBUF_SIZE);
    sid = GetSid(record->log.event.sig_generator, record->log.event.sig_id);
    if(sid == NULL)
        sid = FakeSid(record->log.event.sig_generator, record->log.event.sig_id);
    class_type = GetClassType(record->log.event.classification);

    if((acid_sig_id = AcidDbGetSigId(op_data, sid, class_type, 
            record->log.event.priority)) == 0)
    {
        FatalError("op_acid_db:  Failed to retrieve ACID DB sig id\n");
    }

    /* Insert data into the event table */
    if(snprintf(sql_buffer, MAX_QUERY_SIZE,
            "INSERT INTO event(sid, cid, signature, timestamp) "
            "VALUES('%u', '%u', '%u', '%s')", 
            op_data->sensor_id, op_data->event_id, acid_sig_id,
            timestamp) < MAX_QUERY_SIZE)
    {
        //LogMessage("SQL: %s\n", sql_buffer);
        Insert(op_data, sql_buffer, NULL);  /* XXX: Error checking */
    }
    /* decode the packet */
    if(DecodePacket(&p, &record->log.pkth, record->pkt + 2) == 0)
    {
        if(p.iph)
        {
            /* Insert ip header information */
            InsertIPData(op_data, &p);

            /* store layer 4 data for non fragmented packets */
            if(!(p.pkt_flags & PKT_FRAG_FLAG))
            {
                switch(p.iph->ip_proto)
                {
                    case IPPROTO_ICMP:
                        InsertICMPData(op_data, &p);
                        break;
                    case IPPROTO_TCP:
                        InsertTCPData(op_data, &p);
                        break;
                    case IPPROTO_UDP:
                        InsertUDPData(op_data, &p);
                        break;
                }
            }

            /* Insert payload data */
            if(op_data->detail)
                InsertPayloadData(op_data, &p);
        }
    }
    ++op_data->event_id;
    return 0;
}
Example #7
0
File: speex.c Project: 0xheart0/vlc
/*****************************************************************************
 * ProcessPacket: processes a Speex packet.
 *****************************************************************************/
static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
                            block_t **pp_block )
{
    decoder_sys_t *p_sys = p_dec->p_sys;
    block_t *p_block = *pp_block;

    if( p_block && p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
        Flush( p_dec );

    /* Date management */
    if( p_block && p_block->i_pts > VLC_TS_INVALID &&
        p_block->i_pts != date_Get( &p_sys->end_date ) )
    {
        date_Set( &p_sys->end_date, p_block->i_pts );
    }

    if( !date_Get( &p_sys->end_date ) )
    {
        /* We've just started the stream, wait for the first PTS. */
        if( p_block ) block_Release( p_block );
        return NULL;
    }

    *pp_block = NULL; /* To avoid being fed the same packet again */

    if( p_sys->b_packetizer )
    {
        if ( p_sys->p_header->frames_per_packet > 1 )
        {
            short *p_frame_holder = NULL;
            int i_bits_before = 0, i_bits_after = 0, i_bytes_in_speex_frame = 0,
                i_pcm_output_size = 0, i_bits_in_speex_frame = 0;
            block_t *p_new_block = NULL;

            i_pcm_output_size = p_sys->p_header->frame_size;
            p_frame_holder = (short*)xmalloc( sizeof(short)*i_pcm_output_size );

            speex_bits_read_from( &p_sys->bits, (char*)p_oggpacket->packet,
                p_oggpacket->bytes);
            i_bits_before = speex_bits_remaining( &p_sys->bits );
            speex_decode_int(p_sys->p_state, &p_sys->bits, p_frame_holder);
            i_bits_after = speex_bits_remaining( &p_sys->bits );

            i_bits_in_speex_frame = i_bits_before - i_bits_after;
            i_bytes_in_speex_frame = ( i_bits_in_speex_frame +
                (8 - (i_bits_in_speex_frame % 8)) )
                / 8;

            p_new_block = block_Alloc( i_bytes_in_speex_frame );
            memset( p_new_block->p_buffer, 0xff, i_bytes_in_speex_frame );

            /*
             * Copy the first frame in this packet to a new packet.
             */
            speex_bits_rewind( &p_sys->bits );
            speex_bits_write( &p_sys->bits,
                (char*)p_new_block->p_buffer,
                (int)i_bytes_in_speex_frame );

            /*
             * Move the remaining part of the original packet (subsequent
             * frames, if there are any) into the beginning
             * of the original packet so
             * they are preserved following the realloc.
             * Note: Any bits that
             * remain in the initial packet
             * are "filler" if they do not constitute
             * an entire byte.
             */
            if ( i_bits_after > 7 )
            {
                /* round-down since we rounded-up earlier (to include
             * the speex terminator code.
             */
                i_bytes_in_speex_frame--;
                speex_bits_write( &p_sys->bits,
                    (char*)p_block->p_buffer,
                    p_block->i_buffer - i_bytes_in_speex_frame );
                p_block = block_Realloc( p_block,
                    0,
                    p_block->i_buffer-i_bytes_in_speex_frame );
                *pp_block = p_block;
            }
            else
            {
                speex_bits_reset( &p_sys->bits );
            }

            free( p_frame_holder );
            return SendPacket( p_dec, p_new_block);
        }
        else
        {
                return SendPacket( p_dec, p_block );
        }
    }
    else
    {
        block_t *p_aout_buffer = DecodePacket( p_dec, p_oggpacket );

        if( p_block )
            block_Release( p_block );
        return p_aout_buffer;
    }
}
void FFMS_VideoSource::FlushFinalFrames() {
	AVPacket Packet;
	InitNullPacket(Packet);
	DecodePacket(&Packet);
}
Example #9
0
/****************************************************************************
 * DecodeBlock: the whole thing
 ****************************************************************************
 * This function must be fed with ogg packets.
 ****************************************************************************/
static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
{
    decoder_sys_t *p_sys = p_dec->p_sys;
    block_t *p_block;
    ogg_packet oggpacket;

    if( !pp_block ) return NULL;

    if( *pp_block )
    {
        /* Block to Ogg packet */
        oggpacket.packet = (*pp_block)->p_buffer;
        oggpacket.bytes = (*pp_block)->i_buffer;
    }
    else
    {
        /* Block to Ogg packet */
        oggpacket.packet = NULL;
        oggpacket.bytes = 0;
    }

    p_block = *pp_block;

    oggpacket.granulepos = -1;
    oggpacket.b_o_s = 0;
    oggpacket.e_o_s = 0;
    oggpacket.packetno = 0;

    if( p_sys->i_headers == 0 )
    {
        /* Take care of the initial Tarkin header */

        oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
        if( tarkin_synthesis_headerin( &p_sys->ti, &p_sys->tc, &oggpacket )
            < 0 )
        {
            msg_Err( p_dec, "this bitstream does not contain Tarkin "
                     "video data.");
            block_Release( p_block );
            return NULL;
        }
        p_sys->i_headers++;

        block_Release( p_block );
        return NULL;
    }

    if( p_sys->i_headers == 1 )
    {
        if( tarkin_synthesis_headerin( &p_sys->ti, &p_sys->tc, &oggpacket )
            < 0 )
        {
            msg_Err( p_dec, "2nd Tarkin header is corrupted." );
            block_Release( p_block );
            return NULL;
        }
        p_sys->i_headers++;
        block_Release( p_block );
        return NULL;
    }

    if( p_sys->i_headers == 2 )
    {
        if( tarkin_synthesis_headerin( &p_sys->ti, &p_sys->tc, &oggpacket )
            < 0 )
        {
            msg_Err( p_dec, "3rd Tarkin header is corrupted." );
            block_Release( p_block );
            return NULL;
        }
        p_sys->i_headers++;

        /* Initialize the tarkin decoder */
        tarkin_synthesis_init( p_sys->tarkin_stream, &p_sys->ti );

        msg_Err( p_dec, "Tarkin codec initialized");

        block_Release( p_block );
        return NULL;
    }

    return DecodePacket( p_dec, pp_block, &oggpacket );
}
Example #10
0
/* sguil only uses log */
int OpSguil_Log(void *context, void *data)
{
    char timestamp[TIMEBUF_SIZE];
    char syslogMessage[SYSLOG_BUF];
    char eventInfo[SYSLOG_BUF];
    //int MAX_INSERT_LEN = 1024;
    char insertColumns[MAX_QUERY_SIZE];
    char insertValues[MAX_QUERY_SIZE];
    char valuesTemp[MAX_QUERY_SIZE];
    char ipInfo[38];
    char portInfo[16];
    char *esc_message;
    Sid *sid = NULL;
    ClassType *class_type;
    UnifiedLogRecord *record = (UnifiedLogRecord *)data; 
    OpSguil_Data *op_data = (OpSguil_Data *)context;
    Packet p;

    bzero(syslogMessage, SYSLOG_BUF);
    bzero(insertColumns, MAX_QUERY_SIZE);
    bzero(insertValues, MAX_QUERY_SIZE);

#if 0 /* this is broken */
    /* skip tagged packets, since the db does not have a mechanism to 
     * deal with them properly
     */
    if(record->log.event.event_reference)
    {
        LogMessage("Skipping tagged packet %i\n", record->log.event.event_reference);
        return 0;
    }
#endif


    RenderTimestamp(record->log.pkth.ts.tv_sec, timestamp, TIMEBUF_SIZE);
    //fprintf(stdout, "Timestamp: %lu\n", GetMilliseconds());
    //fflush(stdout);
    sid = GetSid(record->log.event.sig_generator, record->log.event.sig_id);
    if(sid == NULL)
        sid = FakeSid(record->log.event.sig_generator, record->log.event.sig_id);
    class_type = GetClassType(record->log.event.classification);

    //sgBeginTransaction(op_data); /* XXX: Error checking */
    /* Build the event insert. */
    snprintf(insertColumns, MAX_QUERY_SIZE,
            "INSERT INTO event (status, sid, cid, signature_id, signature_rev, signature, timestamp, priority, class");

    esc_message = malloc(strlen(sid->msg)*2+1);
    mysql_real_escape_string(op_data->mysql, esc_message, sid->msg, strlen(sid->msg));

    if(class_type == NULL)
    {
        snprintf(valuesTemp, MAX_QUERY_SIZE,
                "VALUES ('0', '%u', '%u', '%d', '%d', '%s', '%s', '%u', 'unknown'",
                op_data->sensor_id, op_data->event_id, sid->sid, sid->rev, esc_message, timestamp, 
                record->log.event.priority);
        snprintf(eventInfo, SYSLOG_BUF, "RTEvent |0|%u|unknown|%s|%s|%u|%u|%s",
                record->log.event.priority, 
                pv.hostname, timestamp, op_data->sensor_id, op_data->event_id,
                sid->msg);
    }
    else
    {
        snprintf(valuesTemp, MAX_QUERY_SIZE,
                "VALUES ('0', '%u', '%u', '%d', '%d', '%s', '%s', '%u', '%s'",
                op_data->sensor_id, op_data->event_id, sid->sid, sid->rev, esc_message, timestamp, 
                record->log.event.priority, class_type->type);
        snprintf(eventInfo, SYSLOG_BUF, "RTEvent |0|%u|%s|%s|%s|%u|%u|%s",
                record->log.event.priority, class_type->type,
                pv.hostname, timestamp, op_data->sensor_id, op_data->event_id,
                sid->msg);
    }

    free(esc_message);

    insertValues[0] = '\0';
    strcat(insertValues, valuesTemp);

    syslogMessage[0] = '\0';
    strcat(syslogMessage, eventInfo);
    /* decode the packet */
    if(DecodePacket(&p, &record->log.pkth, record->pkt + 2) == 0)
    {

        if(p.iph)
        {
            /* Insert ip header information */
            //InsertIPData(op_data, &p);
            strcat(insertColumns,
                    ",src_ip, dst_ip, ip_proto, ip_ver, ip_hlen, ip_tos, ip_len, ip_id, ip_flags, ip_off, ip_ttl, ip_csum");
            snprintf(valuesTemp, MAX_QUERY_SIZE,
                    ",'%u', '%u', '%u', '%u', '%u', '%u', '%u', '%u', '%u', '%u', '%u', '%u'",
                    ntohl(p.iph->ip_src.s_addr), ntohl(p.iph->ip_dst.s_addr), p.iph->ip_proto, IP_VER(p.iph),
                    IP_HLEN(p.iph), p.iph->ip_tos, ntohs(p.iph->ip_len), ntohs(p.iph->ip_id),
#if defined(WORDS_BIGENDIAN)
                    ((p.iph->ip_off & 0xE000) >> 13),
                    htons(p.iph->ip_off & 0x1FFF),
#else
                    ((p.iph->ip_off & 0x00E0) >> 5),
                    htons(p.iph->ip_off & 0xFF1F),
#endif
                    p.iph->ip_ttl,
                    htons(p.iph->ip_csum) < MAX_QUERY_SIZE);

            strcat(insertValues, valuesTemp);


            /* SYSLOG - Changed to SguilSendEvent*/
            snprintf(ipInfo, 38, "|%u.%u.%u.%u|%u.%u.%u.%u|%u",
#if defined(WORDS_BIGENDIAN)
                    (p.iph->ip_src.s_addr & 0xff000000) >> 24,
                    (p.iph->ip_src.s_addr & 0x00ff0000) >> 16,
                    (p.iph->ip_src.s_addr & 0x0000ff00) >> 8,
                    (p.iph->ip_src.s_addr & 0x000000ff),
                    (p.iph->ip_dst.s_addr & 0xff000000) >> 24,
                    (p.iph->ip_dst.s_addr & 0x00ff0000) >> 16,
                    (p.iph->ip_dst.s_addr & 0x0000ff00) >> 8,
                    (p.iph->ip_dst.s_addr & 0x000000ff),
#else
                    (p.iph->ip_src.s_addr & 0x000000ff),
                    (p.iph->ip_src.s_addr & 0x0000ff00) >> 8,
                    (p.iph->ip_src.s_addr & 0x00ff0000) >> 16,
                    (p.iph->ip_src.s_addr & 0xff000000) >> 24,
                    (p.iph->ip_dst.s_addr & 0x000000ff),
                    (p.iph->ip_dst.s_addr & 0x0000ff00) >> 8,
                    (p.iph->ip_dst.s_addr & 0x00ff0000) >> 16,
                    (p.iph->ip_dst.s_addr & 0xff000000) >> 24,
#endif
                    p.iph->ip_proto);
            strcat(syslogMessage, ipInfo);

            /* store layer 4 data for non fragmented packets */
            if(!(p.pkt_flags & PKT_FRAG_FLAG))
            {
                switch(p.iph->ip_proto)
                {
                    case IPPROTO_ICMP:
                        snprintf(portInfo, 16, "|||");
                        if(!p.icmph) 
                            break;
                        strcat(insertColumns,
                                ", icmp_type, icmp_code)");
                        snprintf(valuesTemp, MAX_QUERY_SIZE,
                                ", '%u', '%u')", p.icmph->icmp_type,
                                p.icmph->icmp_code);
                        strcat(insertValues, valuesTemp);
                        strcat(insertColumns, insertValues);
                        sgInsert(op_data, insertColumns, NULL);
                        sgInsertICMPData(op_data, &p);
                        break;
                    case IPPROTO_TCP:
                        strcat(insertColumns,
                                ", src_port, dst_port)");
                        snprintf(valuesTemp, MAX_QUERY_SIZE,
                                ", '%u', '%u')", p.sp, p.dp);
                        strcat(insertValues, valuesTemp);
                        strcat(insertColumns, insertValues);
                        sgInsert(op_data, insertColumns, NULL);
                        sgInsertTCPData(op_data, &p);
                        snprintf(portInfo, 16, "|%u|%u|",
                                p.sp, p.dp);
                        break;
                    case IPPROTO_UDP:
                        strcat(insertColumns,
                                ", src_port, dst_port)");
                        snprintf(valuesTemp, MAX_QUERY_SIZE,
                                ", '%u', '%u')", p.sp, p.dp);
                        strcat(insertValues, valuesTemp);
                        strcat(insertColumns, insertValues);
                        sgInsert(op_data, insertColumns, NULL);
                        sgInsertUDPData(op_data, &p);
                        snprintf(portInfo, 16, "|%u|%u|",
                                p.sp, p.dp);
                        break;
                }
                strcat(syslogMessage, portInfo);
            }
            else
            {
                strcat(syslogMessage, "|||");
            }


            /* Insert payload data */
            sgInsertPayloadData(op_data, &p);
        }
        else
        {