diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 0a263cbb08b6ea43961dc3006b32b0e8982e647c..70df54250064fee83ca6436fb24ada8253bdde20 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -915,8 +915,8 @@ typedef struct AVCodecContext {
     void (*release_buffer)(struct AVCodecContext *c, AVFrame *pic);
 
     /**
-     * is 1 if the decoded stream contains b frames, 0 otherwise.
-     * - encoding: unused
+     * if 1 the stream has a 1 frame delay during decoding.
+     * - encoding: set by lavc
      * - decoding: set by lavc
      */
     int has_b_frames;
@@ -1251,6 +1251,7 @@ typedef struct AVCodecContext {
 #define FF_CMP_ZERO 7
 #define FF_CMP_VSAD 8
 #define FF_CMP_VSSE 9
+#define FF_CMP_NSSE 10
 #define FF_CMP_CHROMA 256
     
     /**
diff --git a/libavcodec/dsputil.c b/libavcodec/dsputil.c
index 3e3e550b115c2b77ae47eb9f7bd2fbc0a5616179..5c84a139be3f8a13a1104b82c3a4e97f2b390f61 100644
--- a/libavcodec/dsputil.c
+++ b/libavcodec/dsputil.c
@@ -2587,6 +2587,54 @@ static int pix_abs8_xy2_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size,
     return s;
 }
 
+static int nsse16_c(/*MpegEncContext*/ void *c, uint8_t *s1, uint8_t *s2, int stride, int h){
+    int score1=0;
+    int score2=0;
+    int x,y;
+    
+    for(y=0; y<h; y++){
+        for(x=0; x<16; x++){
+            score1+= (s1[x  ] - s2[x ])*(s1[x  ] - s2[x ]);
+        }
+        if(y+1<h){
+            for(x=0; x<15; x++){
+                score2+= ABS(  s1[x  ] - s1[x  +stride]
+                             - s1[x+1] + s1[x+1+stride])
+                        -ABS(  s2[x  ] - s2[x  +stride]
+                             - s2[x+1] + s2[x+1+stride]);
+            }
+        }
+        s1+= stride;
+        s2+= stride;
+    }
+    
+    return score1 + ABS(score2)*8;
+}
+
+static int nsse8_c(/*MpegEncContext*/ void *c, uint8_t *s1, uint8_t *s2, int stride, int h){
+    int score1=0;
+    int score2=0;
+    int x,y;
+    
+    for(y=0; y<h; y++){
+        for(x=0; x<8; x++){
+            score1+= (s1[x  ] - s2[x ])*(s1[x  ] - s2[x ]);
+        }
+        if(y+1<h){
+            for(x=0; x<7; x++){
+                score2+= ABS(  s1[x  ] - s1[x  +stride]
+                             - s1[x+1] + s1[x+1+stride])
+                        -ABS(  s2[x  ] - s2[x  +stride]
+                             - s2[x+1] + s2[x+1+stride]);
+            }
+        }
+        s1+= stride;
+        s2+= stride;
+    }
+    
+    return score1 + ABS(score2)*8;
+}
+
 static int try_8x8basis_c(int16_t rem[64], int16_t weight[64], int16_t basis[64], int scale){
     int i;
     unsigned int sum=0;
@@ -2680,6 +2728,9 @@ void ff_set_cmp(DSPContext* c, me_cmp_func *cmp, int type){
         case FF_CMP_ZERO:
             cmp[i]= zero_cmp;
             break;
+        case FF_CMP_NSSE:
+            cmp[i]= c->nsse[i];
+            break;
         default:
             av_log(NULL, AV_LOG_ERROR,"internal error in cmp function selection\n");
         }
@@ -3313,6 +3364,8 @@ void dsputil_init(DSPContext* c, AVCodecContext *avctx)
     c->vsad[4]= vsad_intra16_c;
     c->vsse[0]= vsse16_c;
     c->vsse[4]= vsse_intra16_c;
+    c->nsse[0]= nsse16_c;
+    c->nsse[1]= nsse8_c;
         
     c->add_bytes= add_bytes_c;
     c->diff_bytes= diff_bytes_c;
diff --git a/libavcodec/dsputil.h b/libavcodec/dsputil.h
index cdc5a3b859abe925bed1c9417d5d5327ed2c327a..be53279797f8bdc2afd7d366dbb0055ad5468cd3 100644
--- a/libavcodec/dsputil.h
+++ b/libavcodec/dsputil.h
@@ -162,6 +162,7 @@ typedef struct DSPContext {
     me_cmp_func rd[5];
     me_cmp_func vsad[5];
     me_cmp_func vsse[5];
+    me_cmp_func nsse[5];
 
     me_cmp_func me_pre_cmp[5];
     me_cmp_func me_cmp[5];
diff --git a/libavcodec/motion_est.c b/libavcodec/motion_est.c
index b92b6dfad8dd0bd07b6dd03ce015bb64c7957878..bee1e07f008475ea873d096b47df4b55e30b8947 100644
--- a/libavcodec/motion_est.c
+++ b/libavcodec/motion_est.c
@@ -223,6 +223,7 @@ static inline int get_penalty_factor(MpegEncContext *s, int type){
     switch(type&0xFF){
     default:
     case FF_CMP_SAD:
+    case FF_CMP_NSSE:
         return s->lambda>>FF_LAMBDA_SHIFT;
     case FF_CMP_DCT:
         return (3*s->lambda)>>(FF_LAMBDA_SHIFT+1);
diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c
index bd2de7cd0feae75301da847385e213b52c829006..f402a81269eab94b0e681b212fd38e2053a460f3 100644
--- a/libavcodec/mpegvideo.c
+++ b/libavcodec/mpegvideo.c
@@ -3916,9 +3916,15 @@ static int sse_mb(MpegEncContext *s){
     if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
 
     if(w==16 && h==16)
+      if(s->avctx->mb_cmp == FF_CMP_NSSE){
+        return  s->dsp.nsse[0](NULL, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], s->linesize, 16)
+               +s->dsp.nsse[1](NULL, s->new_picture.data[1] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,s->dest[1], s->uvlinesize, 8)
+               +s->dsp.nsse[1](NULL, s->new_picture.data[2] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,s->dest[2], s->uvlinesize, 8);
+      }else{
         return  s->dsp.sse[0](NULL, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], s->linesize, 16)
                +s->dsp.sse[1](NULL, s->new_picture.data[1] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,s->dest[1], s->uvlinesize, 8)
                +s->dsp.sse[1](NULL, s->new_picture.data[2] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,s->dest[2], s->uvlinesize, 8);
+      }
     else
         return  sse(s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], w, h, s->linesize)
                +sse(s, s->new_picture.data[1] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,s->dest[1], w>>1, h>>1, s->uvlinesize)