[Sumover-dev] [svn commit] r4869 - in vic/branches/cc: . codec rtp video

sumover-dev at cs.ucl.ac.uk sumover-dev at cs.ucl.ac.uk
Wed Jul 14 23:12:50 BST 2010


Author: piers
Date: Wed Jul 14 23:12:50 2010
New Revision: 4869

Modified:
   vic/branches/cc/codec/decoder-h261.cpp
   vic/branches/cc/codec/decoder.cpp
   vic/branches/cc/codec/decoder.h
   vic/branches/cc/codec/encoder-h261.cpp
   vic/branches/cc/module.h
   vic/branches/cc/rtp/rtp.h
   vic/branches/cc/rtp/session.cpp
   vic/branches/cc/rtp/transmitter.cpp
   vic/branches/cc/tcl/ui-ctrlmenu.tcl
   vic/branches/cc/tcl/ui-grabber.tcl
   vic/branches/cc/tcl/ui-main.tcl
   vic/branches/cc/video/grabber-still.cpp
   vic/branches/cc/video/grabber.h

Log:
First cut at Adding PSNR calculation - for use with still grabber. It's a bit hackey - as it only works with h261 and uses a non-standard RTP header extension.


Modified: vic/branches/cc/codec/decoder-h261.cpp
==============================================================================
--- vic/branches/cc/codec/decoder-h261.cpp	(original)
+++ vic/branches/cc/codec/decoder-h261.cpp	Wed Jul 14 23:12:50 2010
@@ -232,7 +232,7 @@
 	if (ntohs(rh->rh_flags) & RTP_M) {
 		codec_->sync();
 		ndblk_ = codec_->ndblk();
-		render_frame(codec_->frame());
+		render_frame(codec_->frame(),rh->frame_no);
 		codec_->resetndblk();
 	}
 	pb->release();

Modified: vic/branches/cc/codec/decoder.cpp
==============================================================================
--- vic/branches/cc/codec/decoder.cpp	(original)
+++ vic/branches/cc/codec/decoder.cpp	Wed Jul 14 23:12:50 2010
@@ -36,6 +36,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <math.h>
 #ifndef WIN32
 #include <sys/param.h>
 #endif
@@ -59,6 +60,7 @@
 #endif
 }
 
+
 //SV-XXX: rearranged intialistaion order to shut upp gcc4
 Decoder::Decoder(int hdrlen) : PacketHandler(hdrlen),
 	nstat_(0), color_(1), decimation_(422), inw_(0), inh_(0), 
@@ -148,6 +150,11 @@
 			colorhist(ch->histogram());
 			return (TCL_OK);
 		}
+		if (strcmp(argv[1], "grabber") == 0) {
+			grabber_ = (Grabber*)TclObject::lookup(argv[2]);
+			fprintf(stderr,"Setting this %d grabber_:%d\n", this,grabber_);
+			return (TCL_OK);
+		}
 	}
 	return (TclObject::command(argc, argv));
 }
@@ -270,7 +277,54 @@
 	}
 }
 
-void Decoder::render_frame(const u_char* frm)
+
+static inline unsigned long long int
+sqr (unsigned long long int x)
+{
+        return (x*x);
+}
+
+static inline double
+psnr_equ (unsigned long long int ssd, unsigned long long int pixels) {
+        double mse = (double)ssd / (double)pixels;
+        double psnr = 10.0 * log10 (sqr(255.0)/mse);
+
+        return psnr;
+}
+
+double
+calc_psnr(u_int8_t *recv_frame, u_int8_t *orig_frame, int width, int height) {
+  double psnr, psnr_y, psnr_u, psnr_v;
+  unsigned long long int total_ssd_y, total_ssd_u, total_ssd_v;
+  unsigned long long int ssd, local_ssd_y = 0, local_ssd_u = 0, local_ssd_v = 0;
+  //  *ssd_y, *ssd_u, *ssd_v, 
+  //int    image1_u0, image1_y0, image1_v0, image1_y1, i, 
+  //       image2_u0, image2_y0, image2_v0, image2_y1;
+  int i;
+
+  for (i=0; i<width*height; i++) 
+     local_ssd_y += sqr( abs( *recv_frame++ - *orig_frame++ ));
+  for (i=0; i<width/2*height/2; i++) 
+     local_ssd_v += sqr( abs( *recv_frame++ - *orig_frame++ ));
+  for (i=0; i<width/2*height/2; i++) 
+     local_ssd_u += sqr( abs( *recv_frame++ - *orig_frame++ ));
+  
+  ssd            = local_ssd_y + local_ssd_u + local_ssd_v;
+  total_ssd_y    += local_ssd_y;
+  total_ssd_u    += local_ssd_u;
+  total_ssd_v    += local_ssd_v;
+
+  psnr           = psnr_equ (ssd,        width * height * 2);
+  psnr_y         = psnr_equ (local_ssd_y, width * height);
+  psnr_u         = psnr_equ (local_ssd_u, (width * height) / 2);
+  psnr_v         = psnr_equ (local_ssd_v, (width * height) / 2);
+ 
+  return psnr;
+}
+
+extern Grabber *ggrabber;
+
+void Decoder::render_frame(const u_char* frm, int frame_no)
 {
 	/*
 	 * Go through all the timestamps and smash the time that
@@ -282,6 +336,7 @@
 	 * an unchanging block, this approach renders it 2 times out of 256,
 	 * rather than 128 out of 256.
 	 */
+        double psnr;
 	int wraptime = now_ ^ 0x80;
 	u_char* ts = rvts_;
 	for (int k = nblk_; --k >= 0; ++ts) {
@@ -289,7 +344,11 @@
 			*ts = now_;
 	}
 
-	YuvFrame f(now_, (u_int8_t*)frm, rvts_, inw_, inh_);
+	YuvFrame f(now_, (u_int8_t*)frm, rvts_, inw_, inh_,0, frame_no);
+	if (inw_*inh_ == grabber_->framesize_) {
+           psnr = calc_psnr((u_int8_t*)frm, (u_int8_t*)grabber_->frame_+frame_no*(grabber_->framesize_+(grabber_->framesize_>>1)), inw_, inh_);
+  	   fprintf(stderr,"Frame (%dx%d;fsz:%d,%d): %d, PSNR: %f\n",inw_,inh_, grabber_,grabber_->framesize_, frame_no, psnr);
+	}
 	for (Renderer* p = engines_; p != 0; p = p->next_)
 		if ((p->ft() & FT_HW) == 0)
 			p->consume(&f);

Modified: vic/branches/cc/codec/decoder.h
==============================================================================
--- vic/branches/cc/codec/decoder.h	(original)
+++ vic/branches/cc/codec/decoder.h	Wed Jul 14 23:12:50 2010
@@ -49,6 +49,7 @@
 #include "vic_tcl.h"
 #include "source.h"
 #include "ntp-time.h"
+#include "grabber.h"
 
 /*
  * Rendering vector.  We keep a vector of timestamps of when each individual
@@ -110,6 +111,7 @@
 	int decimation_;	/* 422 or 411 */
 	int inw_;		/* native image width */
 	int inh_;		/* native image height */
+        Grabber* grabber_;
 
  public:
 	/*
@@ -123,7 +125,7 @@
 	virtual void redraw() = 0;
 	Renderer* engines_;
 
-	void render_frame(const u_char* frm);
+	void render_frame(const u_char* frm, int frame_no=0);
 	int now_;
 	u_char* rvts_;
 	int nblk_;		/* number of 8x8 blocks */

Modified: vic/branches/cc/codec/encoder-h261.cpp
==============================================================================
--- vic/branches/cc/codec/encoder-h261.cpp	(original)
+++ vic/branches/cc/codec/encoder-h261.cpp	Wed Jul 14 23:12:50 2010
@@ -896,7 +896,7 @@
 	// ---------------------------------------------------------------*
 
 	// adjust quantizer
-	adjust_quantizer(txq_beg_, avg_packets_per_frame());
+	//adjust_quantizer(txq_beg_, avg_packets_per_frame());
 
 	// increment frame number
 	if (vfno_++%FHSIZE == 0) 
@@ -971,6 +971,8 @@
 	sbit_ = 0;
 	/* RTP/H.261 header */
 	rtphdr* rh = (rtphdr*)pb->data;
+	// Set frame_no in MOD RTP hdr
+	rh->frame_no=vf->frame_no_;
 	*(u_int*)(rh + 1) = 1 << 25 | lq_ << 10;
 
 	/* PSC */
@@ -1019,6 +1021,9 @@
 					pktbuf* npb;
 					npb = pool_->alloc(vf->ts_, RTP_PT_H261);
 					pb->tag = true;	// turn on tag (start new frame)
+					rtphdr* rh = (rtphdr*)pb->data;
+					// Add frame_no to RTP hdr
+					rh->frame_no=vf->frame_no_;
 					cc += flush(pb, nbit, npb);
 					cbits -= nbit;
 					pb = npb;
@@ -1054,6 +1059,9 @@
 		}
 		pb->tag = false; // turn off tag (end of frame)
 	}
+        rh = (rtphdr*)pb->data;
+        // Add frame_no to RTP hdr
+        rh->frame_no=vf->frame_no_;
 	cc += flush(pb, ((bc_ - bs_) << 3) + nbb_, 0);
 
 	// time measurement

Modified: vic/branches/cc/module.h
==============================================================================
--- vic/branches/cc/module.h	(original)
+++ vic/branches/cc/module.h	Wed Jul 14 23:12:50 2010
@@ -50,23 +50,24 @@
 class VideoFrame {
     public:
 	inline VideoFrame(u_int32_t ts, u_int8_t* bp, int w, int h,
-			  int layer) :
-		ts_(ts), bp_(bp), width_(w), height_(h), layer_(layer) { }
+			  int layer, int frame_no=0) :
+		ts_(ts), bp_(bp), width_(w), height_(h), layer_(layer),frame_no_(frame_no) { }
 
 	u_int32_t ts_;
 	u_int8_t* bp_;
 	int width_;
 	int height_;
 	int layer_;
+	int frame_no_;
 };
 
 class YuvFrame : public VideoFrame {
     public:
 	    inline YuvFrame(u_int32_t ts, u_int8_t* bp, u_int8_t* crvec,
-			int w, int h, int layer=0) :
-			VideoFrame(ts, bp, w, h, layer), crvec_(crvec) {}
+			int w, int h, int layer=0, int frame_no=-1) :
+			VideoFrame(ts, bp, w, h, layer, frame_no), crvec_(crvec) {}
 
-	    const u_int8_t* crvec_;
+	const u_int8_t* crvec_;
 };
 
 class JpegFrame : public VideoFrame {

Modified: vic/branches/cc/rtp/rtp.h
==============================================================================
--- vic/branches/cc/rtp/rtp.h	(original)
+++ vic/branches/cc/rtp/rtp.h	Wed Jul 14 23:12:50 2010
@@ -91,6 +91,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 */
+	u_int32_t frame_no;	/* frame_no */
 	/* data sources follow per cc */
 };
 

Modified: vic/branches/cc/rtp/session.cpp
==============================================================================
--- vic/branches/cc/rtp/session.cpp	(original)
+++ vic/branches/cc/rtp/session.cpp	Wed Jul 14 23:12:50 2010
@@ -1081,9 +1081,9 @@
 
 	// RTP data receiver need to extract seqno
 	// and send XR report back to the sender.
+	rtphdr* rh = (rtphdr*)pb->data;
 	if (!am_i_sender()) {
 		// retrieve RTP seqno
-		rtphdr* rh = (rtphdr*)pb->data;
 		seqno = ntohs(rh->rh_seqno);
 
 		switch (cc_type_) {
@@ -1103,6 +1103,7 @@
 		ch_->send_ackv();
 	}
 	
+        fprintf(stderr, "\n\treceived frame_no: %d\n\n", rh->frame_no);
 	//bp += sizeof(*rh);
 	//cc -= sizeof(*rh);
 	demux(pb, *addrp);

Modified: vic/branches/cc/rtp/transmitter.cpp
==============================================================================
--- vic/branches/cc/rtp/transmitter.cpp	(original)
+++ vic/branches/cc/rtp/transmitter.cpp	Wed Jul 14 23:12:50 2010
@@ -96,7 +96,7 @@
 	loopback_(0),
 	is_cc_active_(1),
 	is_buf_empty_(1),
-	cc_type_(WBCC),
+	cc_type_(NOCC),
 	cwnd_mode_(BYM)
 {
 	memset((char*)&mh_, 0, sizeof(mh_));
@@ -137,6 +137,7 @@
 {
 	int layer = pb->layer;
 	rtphdr* rh = (rtphdr*)pb->data;
+        fprintf(stderr, "loopback received frame_no: %d\n", rh->frame_no);
 	int cc = pb->len;
 	/*
 	 * Update statistics.
@@ -526,27 +527,29 @@
 void Transmitter::timeout()
 {
 	double now = gettimeofday_secs();
-    pktbuf* p = head_;
+        pktbuf* p = head_;
 
 	switch (cc_type_) {
 	case RBCC:
-      for (;;) {
-        if(p != 0) {
-          nextpkttime_ += txtime(p);
-          // call send(pktbuf *) here
-          send(p);
-          // goto sleep
-          int ms = int(1e-3 * (nextpkttime_ - now));
-          msched(ms);
-        }
-      }
+          for (;;) {
+            pktbuf* p = head_;
+            if(p != 0) {
+              nextpkttime_ += txtime(p);
+              // call send(pktbuf *) here
+              send(p);
+              // goto sleep
+              int ms = int(1e-3 * (nextpkttime_ - now));
+              msched(ms);
+            }
+          }
 	  break;
 	case WBCC:
-      // nothing to do when WBCC mode
-      break;
+          // nothing to do when WBCC mode
+          break;
 	case NOCC:
 	default:
 	  for (;;) {
+                pktbuf* p = head_;
 		if (p != 0) {
 			head_ = p->next;
 			nextpkttime_ += txtime(p);

Modified: vic/branches/cc/tcl/ui-ctrlmenu.tcl
==============================================================================
--- vic/branches/cc/tcl/ui-ctrlmenu.tcl	(original)
+++ vic/branches/cc/tcl/ui-ctrlmenu.tcl	Wed Jul 14 23:12:50 2010
@@ -830,8 +830,8 @@
 	}
 	foreach d $inputDeviceList {
 		if { [$d nickname] == "still" && ![yesno stillGrabber] } {
-			set defaultFormat($d) $videoFormat
-			continue
+		#	set defaultFormat($d) $videoFormat
+		#	continue
 		}
 		# this is fragile
 		$m add radiobutton -label [$d nickname] \

Modified: vic/branches/cc/tcl/ui-grabber.tcl
==============================================================================
--- vic/branches/cc/tcl/ui-grabber.tcl	(original)
+++ vic/branches/cc/tcl/ui-grabber.tcl	Wed Jul 14 23:12:50 2010
@@ -702,7 +702,7 @@
     frame $w.f -relief sunken -borderwidth 2
     label $w.f.label  -font $f -anchor e -text "File"
 
-    mk.entry $w.f set.still.frame "frame"
+    mk.entry $w.f set.still.frame "foreman.yuv"
 
     pack $w.title $w.f -fill x -expand 1
 

Modified: vic/branches/cc/tcl/ui-main.tcl
==============================================================================
--- vic/branches/cc/tcl/ui-main.tcl	(original)
+++ vic/branches/cc/tcl/ui-main.tcl	Wed Jul 14 23:12:50 2010
@@ -757,7 +757,7 @@
 }
 
 proc create_decoder src {
-	global numLayers
+	global numLayers V
 
 	set format [rtp_format $src]
 	set decoder [new decoder $format]
@@ -769,6 +769,9 @@
 	if { $format == "pvh" } {
 		$decoder maxChannel $numLayers
 	}
+       	# Inform decoder obj of grabber to enable PSNR calculation
+        $decoder grabber $V(grabber)
+
 	$src handler $decoder
 	return $decoder
 }
@@ -814,6 +817,7 @@
 	grid columnconfigure $V(grid) $V(curcol) -weight 1
 
 	update_decoder $src
+        $V(decoder) grabber $V(grabber)
 	after 1000 "update_src $src"
 
 	bump

Modified: vic/branches/cc/video/grabber-still.cpp
==============================================================================
--- vic/branches/cc/video/grabber-still.cpp	(original)
+++ vic/branches/cc/video/grabber-still.cpp	Wed Jul 14 23:12:50 2010
@@ -103,9 +103,9 @@
     virtual Grabber* yuv_grabber();
 
     void load_file(const char * const file);
-    char *frame_;
+    u_char *frame_;
     int len_;
-	int devstat_;	// device status
+    int devstat_;	// device status
 private:
 };
 
@@ -137,7 +137,7 @@
 	    const char* fmt = argv[2];
 	    TclObject* o = 0;
 	    if (strcmp(fmt, "cif") == 0)
-			o = yuv_grabber();
+	    		o = yuv_grabber();
 	    if (strcmp(fmt, "jpeg") == 0)
 			o = jpeg_grabber();
 	    if (o != 0)
@@ -186,7 +186,7 @@
     if (frame_)
 		delete[] frame_; //SV-XXX: Debian
     
-    frame_ = new char[len_ + 1];
+    frame_ = new u_char[len_ + 1];
 	r = fread(frame_, len_, 1, fp);
 	debug_msg("Successfully loaded %s\n", f);
 
@@ -334,6 +334,8 @@
 void StillYuvGrabber::start()
 {
 	target_->offset_ = grabber_ts_off_;
+	frame_=still_device.frame_;
+	fprintf(stderr,"frame_:%x\n",frame_);
 	Grabber::start();
 }
 
@@ -379,10 +381,14 @@
 
 	// "framesize_" is just the number of pixels, 
 	// so the number of bytes becomes "3 * framesize_ / 2"
+
+	int stride= framesize_ + (framesize_ >> 1);
+	int frame_no=nbytes_/stride;
+	fprintf(stderr,"Frame_no %d, number of bytes: %d (fsz:%d)\n",frame_no,  nbytes_, framesize_);
 	memcpy (frame_, still_device.frame_ + nbytes_, 
-			framesize_ + (framesize_ >> 1));
+			stride);
 
-	if ((nbytes_ += framesize_+(framesize_ >> 1)) < still_device.len_) {
+	if ((nbytes_ += stride) < still_device.len_) {
 	} else {
 		nbytes_=0;
 	}
@@ -393,7 +399,7 @@
 
 	suppress(frame_);
 	saveblks(frame_);
-	YuvFrame f(media_ts(), (u_int8_t *) frame_, crvec_, outw_, outh_);
+	YuvFrame f(media_ts(), (u_int8_t *) frame_, crvec_, outw_, outh_,0,frame_no);
 
 	// time measurement
 	end_grab_ = grabber_now() - grabber_ts_off_;

Modified: vic/branches/cc/video/grabber.h
==============================================================================
--- vic/branches/cc/video/grabber.h	(original)
+++ vic/branches/cc/video/grabber.h	Wed Jul 14 23:12:50 2010
@@ -87,6 +87,8 @@
 	double start_grab_;
 	double end_grab_;
 
+	u_int framesize_;
+	u_char* frame_;
  protected:
 	/* hooks for conditional replenishment algorithm */
 	void suppress(const u_char* devbuf);
@@ -106,9 +108,7 @@
 	int hstop_;
 	int threshold_ ; // when a block is changed ? (was constant 48)
 
-	u_int framesize_;
 	u_char* framebase_;
-	u_char* frame_;
 	u_char* crvec_;
 	u_char* ref_;/*XXX*/
 	int inw_;



More information about the Sumover-dev mailing list