[Sumover-dev] [svn commit] r3941 - in vic/branches/mpeg4: codec

sumover-dev at cs.ucl.ac.uk sumover-dev at cs.ucl.ac.uk
Mon Nov 20 11:18:24 GMT 2006


Author: barz
Date: Mon Nov 20 11:17:48 2006
New Revision: 3941

Modified:
   vic/branches/mpeg4/codec/decoder-h264.cpp
   vic/branches/mpeg4/codec/encoder-h264.cpp
   vic/branches/mpeg4/codec/packetbuffer.cpp
   vic/branches/mpeg4/codec/x264encoder.cpp
   vic/branches/mpeg4/tcl/ui-ctrlmenu.tcl
   vic/branches/mpeg4/tcl/ui-resource.tcl

Log:
* x264 codec fine tune
* fix H.264 deplayloader packet loss
* reduce H.264 payloader header to 14 bytes
* set NTSC as default
* set MPEG4 as default



Modified: vic/branches/mpeg4/codec/decoder-h264.cpp
==============================================================================
--- vic/branches/mpeg4/codec/decoder-h264.cpp	(original)
+++ vic/branches/mpeg4/codec/decoder-h264.cpp	Mon Nov 20 11:17:48 2006
@@ -35,8 +35,6 @@
     UCHAR bitstream[MAX_CODED_SIZE];	/* bitstream data */
 
     /* collecting data for a frame */
-    int b_off;			/* how much bitstream data we have */
-    int b_all;			/* got all packets for so far? */
     int idx;
     int last_mbit;
     int last_iframe;
@@ -86,8 +84,6 @@
     h264.init(false, CODEC_ID_H264, PIX_FMT_YUV420P);
     h264.init_decoder();
 
-    b_all = 1;
-    b_off = 0;
     idx = 0;
     last_mbit = 0;
     last_iframe = 0;
@@ -114,108 +110,66 @@
 
 void H264Decoder::recv(pktbuf * pb)
 {
-/*    
-    // simulate the pack loss
-    if(rand() % 10 == 0){
-      printf("drop the packet\n");
-      pb->release();
-      return;      
-    }
-    if(rand() % 50 == 0){
-      printf("packet error\n");	
-      int l = rand() % pb->len;
-      for(int i=0; i < l; i++)
-        pb->dp[rand()% pb->len] = u_char(rand() % 256);
-    }    
-*/
     rtphdr *rh = (rtphdr *) pb->dp;
-    int hdrsize = sizeof(rtphdr) + hdrlen() + 2;
+    int hdrsize = sizeof(rtphdr) + hdrlen();
     u_char *bp = pb->dp + hdrsize;
     int cc = pb->len - hdrsize;
     static int iframe_c = 0, pframe_c = 0;
 
-    //(sizeof(*rh) + 4);
-    /* RTP header  */
-    /* Basic RTP header 
-       struct rtphdr {
-       u_int16_t rh_flags;      // T:2 P:1 X:1 CC:4 M:1 PT:7 
-       u_int16_t rh_seqno;      // sequence number 
-       u_int32_t rh_ts; // media-specific time stamp 
-       u_int32_t rh_ssrc;       // synchronization src id 
-       // data sources follow per cc 
-       }; */
-
     int mbit = ntohs(rh->rh_flags) >> 7 & 1;
     int seq = ntohs(rh->rh_seqno);
     int ts = ntohl(rh->rh_ts);
 
-    /* H.263 payload */
-    u_char *pl = (u_char *) (rh + 1);
-    /* 5 bits reserved */
-    int pbit = (pl[0] >> 2) & 1;	/* 1 bit */
-    int ibit = (pl[0] >> 1) & 1;	/* 1 bit */
-
-    int ms = 0x0000FFFF & ntohl(*((u_int *) pl));
-    //std::cout << "dec: "<< ms << "\n";
-
-    if (pbit) {
-	/* start of a new frame */
-	b_all = 1;
-	b_off = 0;
-	stream->clear();
-	idx = seq;
-	startPkt = true;
+    if (!startPkt) {
+       stream->clear();
+       startPkt = true;
+       idx = seq;
+    }
+	  
+    int pktIdx = seq - idx;
+    if (pktIdx < 0) {
+        pktIdx = (0xFFFF - idx) + seq;
     }
 
-
-    if (startPkt) {
-	int pktIdx = seq - idx;
-	if (pktIdx < 0) {
-	    pktIdx = (0xFFFF - idx) + seq;
-	}
-	//copy packet
-	stream->write(pktIdx, cc, (char *) bp);
-	if (last_seq + 1 != seq) {
-	    /* oops - missing packet */
-	    debug_msg("h264 dec: missing packet\n");
-	}
-	last_seq = seq;
-
-	int len;
-	if (mbit) {
+    if (pktIdx - last_seq > 5) {
+       debug_msg("mp4dec: sequece interrupt!\n");
+       idx = seq;
+       pktIdx = 0;
+       stream->clear();
+    }
+    
+    //copy packet
+    stream->write(pktIdx, cc, (char *) bp);
+    // printf("pktIdx=%d, cc=%d, seq=%d\n", pktIdx, cc, seq);
+    
+    if (last_seq + 1 != seq) {
+       /* oops - missing packet */
+       debug_msg("h264dec: missing packet\n");
+    }
+	
+    last_seq = seq;
+    int len=0;
+	
+    if (mbit) {
 	    stream->setTotalPkts(pktIdx + 1);
-	    /* yeah! have a complete frame */
-	    if (ibit) {
-		last_iframe = 1;
-	    }
-	    else if (!last_iframe) {
-		debug_msg("h264dec: no I-frame yet\n");
-		pb->release();
-		return;
-	    }
-
-	    assert(b_off < MAX_CODED_SIZE);
-	    //printf("receive %d\n", b_off);
-	    //fwrite(bitstream, b_off, 1, fptr);    //File decoding problem occurs
 
+	    DataBuffer *f;	    
 	    if (stream->isComplete()) {
-		DataBuffer *f = stream->getStream();
-		len =
-		    h264.decode((UCHAR *) f->getData(), f->getDataSize(),
+		f = stream->getStream();
+		len =  h264.decode((UCHAR *) f->getData(), f->getDataSize(),
 				xxx_frame);
 	    }
-	    else {
-		cout << "packet loss...\n";
-		len = 0;
+	    
+            if(len == -2){
+               debug_msg("h264dec: resize\n");
+    	       h264.release();
+               h264.init_decoder();
+	       len = h264.decode((UCHAR *) f->getData(), f->getDataSize(), xxx_frame);
 	    }
-
-	    if (len < 0) {
-		cout << "decoder : frame error\n";
-		pb->release();
-		debug_msg("h264dec: frame error\n");
-		return;
+	   if (len <= 0) {
+	       debug_msg("h264dec: frame error\n");
 	    }
-
+	   
 	    if (inw_ != h264.width || inh_ != h264.height) {
 		inw_ = h264.width;
 		inh_ = h264.height;
@@ -224,12 +178,9 @@
 	    else {
 		Decoder::redraw(xxx_frame);
 	    }
-
-	    //NEXT Should Be P bit
-	    b_all = 1;
-	    stream->clear();
-	    //b_off = 0;
-	}
+            stream->clear();
+	    idx = seq+1;
+		     
     }
     pb->release();
 }

Modified: vic/branches/mpeg4/codec/encoder-h264.cpp
==============================================================================
--- vic/branches/mpeg4/codec/encoder-h264.cpp	(original)
+++ vic/branches/mpeg4/codec/encoder-h264.cpp	Mon Nov 20 11:17:48 2006
@@ -4,7 +4,7 @@
 #include <math.h>
 #include <errno.h>
 #include <assert.h>
-#ifdef _WIN32
+#ifdef _WIN32
 #include <time.h>
 #else
 #include <sys/time.h>
@@ -139,7 +139,6 @@
     pktbuf *pb;
     rtphdr *rh;
     //int n,ps, len;
-    //int send_psize = tx_->mtu() - 14;      // 12 RTP + 2 Payload
     ts = vf->ts_;
     tx = tx_;
     pool = pool_;
@@ -162,13 +161,6 @@
 	prevTimeMS = timeStamp.tv_sec;
     }
 
-    //Encode
-    gettimeofday(&timeStamp, NULL);
-    int ms =
-	(timeStamp.tv_sec - prevTime) * 1000 + timeStamp.tv_usec - prevTimeMS;
-    prevTime = timeStamp.tv_sec;
-    prevTimeMS = timeStamp.tv_usec;
-    //printf("ms:%d\n", ms);
     frame_size = vf->width_ * vf->height_;
     char *data = fIn->getData();
     memcpy(data, vf->bp_, frame_size * 3 >> 1);
@@ -196,39 +188,25 @@
 	while (nalSize > 0) {
 	    pb = pool_->alloc(vf->ts_, RTP_PT_H264);
 	    rh = (rtphdr *) pb->data;
-	    *(u_int *) (rh + 1) = 0;
-	    *(u_int *) (rh + 1) = htonl(ms);
 	    if (nalSize > 1000) {
-		if (first) {
-		    first = false;
-		    *(u_int *) (rh + 1) |= htonl(0x04000000);	// set P bit
-		}
-		memcpy(&pb->data[14 + 2], data + offset, 1000);
+		memcpy(&pb->data[14], data + offset, 1000);
 		//DEBUG
 		//fwrite(data+offset, 1000, 1, fptr);
-		pb->len = 1000 + 14 + 2;
+		pb->len = 1000 + 14;
 		offset += 1000;
 		nalSize -= 1000;
-		//sent_size += 14;
 	    }
 	    else {
-		if (first) {
-		    first = false;
-		    *(u_int *) (rh + 1) |= htonl(0x04000000);	// set P bit
-		}
 		if (i == i_nal - 1) {
 		    //Last Packet
 		    rh->rh_flags |= htons(RTP_M);	// set M bit
-		    //Currently we set I bit for every frame
-		    *(u_int *) (rh + 1) |= htonl(0x02000000);	// set I bit
 		}
-		memcpy(&pb->data[14 + 2], data + offset, nalSize);
+		memcpy(&pb->data[14], data + offset, nalSize);
 		//DEBUG
 		//printf("encode a frame...\n");
 		//fwrite(data+offset, nalSize, 1, fptr);
 
-		pb->len = nalSize + 14 + 2;
-		//sent_size += 14;
+		pb->len = nalSize + 14;
 		nalSize = 0;
 	    }
 	    //printf("send out %d\n", pb->len);
@@ -237,13 +215,6 @@
 	}
     }
     frame_seq++;
-    //std::cout << "encoder: frame size : " << sent_size << "\n";
-    //return sent_size;
 
-    /*
-     * champ
-     * VIC will grab a frame with a time interval according to this return value.
-     * Since this module adopts ratecontrol by bps value, it returns a constant value for a reason of smoonthness.
-     */
     return (kbps*1024) / (fps*8);
 }

Modified: vic/branches/mpeg4/codec/packetbuffer.cpp
==============================================================================
--- vic/branches/mpeg4/codec/packetbuffer.cpp	(original)
+++ vic/branches/mpeg4/codec/packetbuffer.cpp	Mon Nov 20 11:17:48 2006
@@ -38,9 +38,12 @@
 {
     if (totalPkts == 0)
 	return false;
-    for (int i = 0; i < totalPkts; i++)
-	if (!isDataRecv[i])
-	    return false;
+    for (int i = 0; i < totalPkts; i++){	
+	if (!isDataRecv[i]){
+	    printf("lost packet %d\n", i);	
+	    //return false;
+	}
+    }
     return true;
 }
 

Modified: vic/branches/mpeg4/codec/x264encoder.cpp
==============================================================================
--- vic/branches/mpeg4/codec/x264encoder.cpp	(original)
+++ vic/branches/mpeg4/codec/x264encoder.cpp	Mon Nov 20 11:17:48 2006
@@ -1,18 +1,14 @@
-#include "x264encoder.h"
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <inttypes.h>
+#include "x264encoder.h"
 #include "ffmpeg/avcodec.h"
-extern "C"
-{
-// #include "x264/common/common.h"
+#include "databuffer.h"
+extern "C"{
 #include "x264.h"
 }
 
-#include "databuffer.h"
-
 typedef struct
 {
     x264_t *h;
@@ -28,25 +24,8 @@
 {
     x264 *enc;
     enc = (x264 *) malloc(sizeof(x264));
-
-    x264_param_t *param = &(enc->param);
-
-    x264_param_default(param);
-    //param->analyse.inter = X264_ANALYSE_PSUB16x16;
-    //DISALBE PARTITION MODE
-    param->analyse.inter = 0;
-    //param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_NONE;
-    //DISABLE CABAC for more frame rate
-    param->b_cabac = 0;
-    //DONOT ENABLE PSNR ANALYSE
-    // param->analyse.b_psnr = 0;
-    param->i_keyint_max = 60;
-    param->i_keyint_min = 20;
- 
     enc->h = NULL;
-
     encoder = (void *) enc;
-
     isFrameEncoded = false;
 }
 
@@ -64,21 +43,41 @@
 {
     x264 *enc = (x264 *) encoder;
     x264_param_t *param = &(enc->param);
+    x264_param_default(param);
 
+    // * seting rate control
     param->rc.i_bitrate = bps;
-    // param->rc.i_rc_method = X264_RC_ABR;
-    param->rc.i_rc_method = X264_RC_CRF;
-    // param->b_cabac = FF_CODER_TYPE_AC;
-    // param->b_deblocking_filter = 1;
+    param->rc.i_rc_method = X264_RC_ABR;
+    
+    param->analyse.inter = X264_ANALYSE_PSUB16x16;
+    //DISALBE PARTITION MODE
+    param->analyse.inter = 0;
+    param->analyse.i_direct_mv_pred = X264_DIRECT_PRED_NONE;
+
+    //DISABLE CABAC for more frame rate
+    param->b_cabac = 0;
+    
+    //DONOT ENABLE PSNR ANALYSE
+    param->analyse.b_psnr = 1;
+    
+    param->i_keyint_max = 100;
+    param->i_keyint_min = 60;
+    param->i_bframe = 0;
+    
+    // deblocking filter
+    // i_deblocking_filter_alphac0, [-6, 6] -6 light filter, 6 strong    
+    param->b_deblocking_filter = 1;
+    param->i_deblocking_filter_alphac0 = 3;
+    
     param->i_fps_num = fps * 1000;
     param->i_fps_den = 1000;
-    param->rc.f_qcompress = 0;  /* 0.0 => cbr, 1.0 => constant qp */
-    param->analyse.i_me_method = X264_ME_UMH;
 
-    //Currently X264 only handle (16*n)x(16*m)
-    if (w % 16 != 0 && h % 16 != 0) {
-	return 0;
-    }
+    // set frame reference to 1 to reduce encoding latency
+    param->i_frame_reference = 1;
+    // param->b_interlaced = 1;
+    
+    // motion estimation method, using umh if higher quality is essential.
+    param->analyse.i_me_method = X264_ME_HEX;
 
     param->i_width = w;
     param->i_height = h;

Modified: vic/branches/mpeg4/tcl/ui-ctrlmenu.tcl
==============================================================================
--- vic/branches/mpeg4/tcl/ui-ctrlmenu.tcl	(original)
+++ vic/branches/mpeg4/tcl/ui-ctrlmenu.tcl	Mon Nov 20 11:17:48 2006
@@ -937,8 +937,8 @@
 		-value secam -variable inputType -font $f
 
 	global inputType typeButton
-	set inputType auto
-    	#set inputType [string tolower [option get . inputType Vic]]
+	#set inputType auto
+    	set inputType [string tolower [option get . inputType Vic]]
 	set typeButton $w
 }
 

Modified: vic/branches/mpeg4/tcl/ui-resource.tcl
==============================================================================
--- vic/branches/mpeg4/tcl/ui-resource.tcl	(original)
+++ vic/branches/mpeg4/tcl/ui-resource.tcl	Mon Nov 20 11:17:48 2006
@@ -132,15 +132,15 @@
 	option add Vic.geometry 250x225 startupFile
 	option add Vic.mtu 1024 startupFile
 	option add Vic.network ip startupFile
-	option add Vic.framerate 8 startupFile
+	option add Vic.framerate 15 startupFile
 	option add Vic.defaultTTL 16 startupFile
 	option add Vic.maxbw -1 startupFile
-	option add Vic.bandwidth 128 startupFile
+	option add Vic.bandwidth 512 startupFile
 	option add Vic.iconPrefix vic: startupFile
 	option add Vic.priority 10 startupFile
 	option add Vic.confBusChannel 0 startupFile
 
-	option add Vic.defaultFormat h.261 startupFile
+	option add Vic.defaultFormat mpeg4 startupFile
 	option add Vic.sessionType rtpv2 startupFile
 	option add Vic.grabber none startupFile
 	option add Vic.stampInterval 1000 startupFile
@@ -156,6 +156,7 @@
 	option add Vic.stillGrabber false startupFile 
 	option add Vic.siteDropTime "300" startupFile
 	option add Vic.quality "0" startupFile
+	option add Vic.inputType "ntsc" startupFile
 
 	option add Vic.brightness "0" startupFile
 	option add Vic.contrast "0" startupFile



More information about the Sumover-dev mailing list