]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/mpegvideo.c
minor optimizations
[ffmpeg] / libavcodec / mpegvideo.c
index 69788e97c812ba8f16a908811f135712aa838796..0cbe85934ae7441dd44ed01b76e87705f36029e8 100644 (file)
@@ -38,6 +38,8 @@ static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale,
 
 int (*dct_quantize)(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow)= dct_quantize_c;
 void (*draw_edges)(UINT8 *buf, int wrap, int width, int height, int w)= draw_edges_c;
+static void emulated_edge_mc(MpegEncContext *s, UINT8 *src, int linesize, int block_w, int block_h, 
+                                    int src_x, int src_y, int w, int h);
 
 #define EDGE_WIDTH 16
 
@@ -179,7 +181,7 @@ int MPV_common_init(MpegEncContext *s)
       s->ip_buffer_count= 2;
     }
     
-    CHECKED_ALLOCZ(s->edge_emu_buffer, (s->width+32)*2*17);
+    CHECKED_ALLOCZ(s->edge_emu_buffer, (s->width+64)*2*17*2); //(width + edge + align)*interlaced*MBsize*tolerance
     
     if (s->encoding) {
         int j;
@@ -268,9 +270,9 @@ int MPV_common_init(MpegEncContext *s)
         /* cbp, ac_pred, pred_dir */
         CHECKED_ALLOCZ(s->cbp_table  , s->mb_num * sizeof(UINT8))
         CHECKED_ALLOCZ(s->pred_dir_table, s->mb_num * sizeof(UINT8))
-        
-        CHECKED_ALLOCZ(s->qscale_table  , s->mb_num * sizeof(UINT8))
     }
+    CHECKED_ALLOCZ(s->qscale_table  , s->mb_num * sizeof(UINT8))
+    
     /* default structure is frame */
     s->picture_structure = PICT_FRAME;
     
@@ -635,6 +637,12 @@ void MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
         for(i=0;i<3;i++) {
             if(avctx->flags&CODEC_FLAG_DR1)
                 s->aux_picture[i]= avctx->dr_buffer[i];
+            
+            //FIXME the following should never be needed, the decoder should drop b frames if no reference is available
+            if(s->next_picture[i]==NULL)
+                s->next_picture[i]= s->aux_picture[i];
+            if(s->last_picture[i]==NULL)
+                s->last_picture[i]= s->next_picture[i];
 
             s->current_picture[i] = s->aux_picture[i];
         }
@@ -878,6 +886,7 @@ static inline void gmc1_motion(MpegEncContext *s,
     UINT8 *ptr;
     int offset, src_x, src_y, linesize, uvlinesize;
     int motion_x, motion_y;
+    int emu=0;
 
     if(s->real_sprite_warping_points>1) printf("more than 1 warp point isnt supported\n");
     motion_x= s->sprite_offset[0][0];
@@ -898,6 +907,14 @@ static inline void gmc1_motion(MpegEncContext *s,
     ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
 
     dest_y+=dest_offset;
+    if(s->flags&CODEC_FLAG_EMU_EDGE){
+        if(src_x<0 || src_y<0 || src_x + (motion_x&15) + 16 > s->width
+                              || src_y + (motion_y&15) + h  > s->height){
+            emulated_edge_mc(s, ptr, linesize, 17, h+1, src_x, src_y, s->width, s->height);
+            ptr= s->edge_emu_buffer;
+            emu=1;
+        }
+    }
     gmc1(dest_y  , ptr  , linesize, h, motion_x&15, motion_y&15, s->no_rounding);
     gmc1(dest_y+8, ptr+8, linesize, h, motion_x&15, motion_y&15, s->no_rounding);
 
@@ -916,18 +933,38 @@ static inline void gmc1_motion(MpegEncContext *s,
 
     offset = (src_y * uvlinesize) + src_x + (src_offset>>1);
     ptr = ref_picture[1] + offset;
+    if(emu){
+        emulated_edge_mc(s, ptr, uvlinesize, 9, (h>>1)+1, src_x, src_y, s->width>>1, s->height>>1);
+        ptr= s->edge_emu_buffer;
+    }
     gmc1(dest_cb + (dest_offset>>1), ptr, uvlinesize, h>>1, motion_x&15, motion_y&15, s->no_rounding);
+    
     ptr = ref_picture[2] + offset;
+    if(emu){
+        emulated_edge_mc(s, ptr, uvlinesize, 9, (h>>1)+1, src_x, src_y, s->width>>1, s->height>>1);
+        ptr= s->edge_emu_buffer;
+    }
     gmc1(dest_cr + (dest_offset>>1), ptr, uvlinesize, h>>1, motion_x&15, motion_y&15, s->no_rounding);
     
     return;
 }
 
-static void emulated_edge_mc(UINT8 *buf, UINT8 *src, int linesize, int block_w, int block_h, 
+static void emulated_edge_mc(MpegEncContext *s, UINT8 *src, int linesize, int block_w, int block_h, 
                                     int src_x, int src_y, int w, int h){
     int x, y;
     int start_y, start_x, end_y, end_x;
-    
+    UINT8 *buf= s->edge_emu_buffer;
+#if 0
+    if(s->codec_id!=CODEC_ID_MPEG4 || s->divx_version>=500){
+        if(block_w < 10){
+            w= (w+7)&(~7);
+            h= (h+7)&(~7);
+        }else{
+            w= (w+15)&(~15);
+            h= (h+15)&(~15);
+        }
+    }
+#endif
     if(src_y>= h){
         src+= (h-1-src_y)*linesize;
         src_y=h-1;
@@ -947,7 +984,7 @@ static void emulated_edge_mc(UINT8 *buf, UINT8 *src, int linesize, int block_w,
     start_x= MAX(0, -src_x);
     end_y= MIN(block_h, h-src_y);
     end_x= MIN(block_w, w-src_x);
-    
+
     // copy existing part
     for(y=start_y; y<end_y; y++){
         for(x=start_x; x<end_x; x++){
@@ -1020,7 +1057,7 @@ if(s->quarter_sample)
     if(s->flags&CODEC_FLAG_EMU_EDGE){
         if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 16 > s->width
                               || src_y + (motion_y&1) + h  > height){
-            emulated_edge_mc(s->edge_emu_buffer, ptr, linesize, 17, h+1, src_x, src_y, s->width, height);
+            emulated_edge_mc(s, ptr, linesize, 17, h+1, src_x, src_y, s->width, height);
             ptr= s->edge_emu_buffer;
             emu=1;
         }
@@ -1057,14 +1094,14 @@ if(s->quarter_sample)
     offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
     ptr = ref_picture[1] + offset;
     if(emu){
-        emulated_edge_mc(s->edge_emu_buffer, ptr, uvlinesize, 9, (h>>1)+1, src_x, src_y, s->width>>1, height>>1);
+        emulated_edge_mc(s, ptr, uvlinesize, 9, (h>>1)+1, src_x, src_y, s->width>>1, height>>1);
         ptr= s->edge_emu_buffer;
     }
     pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
 
     ptr = ref_picture[2] + offset;
     if(emu){
-        emulated_edge_mc(s->edge_emu_buffer, ptr, uvlinesize, 9, (h>>1)+1, src_x, src_y, s->width>>1, height>>1);
+        emulated_edge_mc(s, ptr, uvlinesize, 9, (h>>1)+1, src_x, src_y, s->width>>1, height>>1);
         ptr= s->edge_emu_buffer;
     }
     pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
@@ -1101,7 +1138,7 @@ static inline void qpel_motion(MpegEncContext *s,
     if(s->flags&CODEC_FLAG_EMU_EDGE){
         if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 16 > s->width
                               || src_y + (motion_y&3) + h  > height){
-            emulated_edge_mc(s->edge_emu_buffer, ptr, linesize, 17, h+1, src_x, src_y, s->width, height);
+            emulated_edge_mc(s, ptr, linesize, 17, h+1, src_x, src_y, s->width, height);
             ptr= s->edge_emu_buffer;
             emu=1;
         }
@@ -1136,14 +1173,14 @@ static inline void qpel_motion(MpegEncContext *s,
     offset = (src_y * s->uvlinesize) + src_x + (src_offset >> 1);
     ptr = ref_picture[1] + offset;
     if(emu){
-        emulated_edge_mc(s->edge_emu_buffer, ptr,  s->uvlinesize, 9, (h>>1)+1, src_x, src_y, s->width>>1, height>>1);
+        emulated_edge_mc(s, ptr,  s->uvlinesize, 9, (h>>1)+1, src_x, src_y, s->width>>1, height>>1);
         ptr= s->edge_emu_buffer;
     }
     pix_op[dxy](dest_cb + (dest_offset >> 1), ptr,  s->uvlinesize, h >> 1);
     
     ptr = ref_picture[2] + offset;
     if(emu){
-        emulated_edge_mc(s->edge_emu_buffer, ptr,  s->uvlinesize, 9, (h>>1)+1, src_x, src_y, s->width>>1, height>>1);
+        emulated_edge_mc(s, ptr,  s->uvlinesize, 9, (h>>1)+1, src_x, src_y, s->width>>1, height>>1);
         ptr= s->edge_emu_buffer;
     }
     pix_op[dxy](dest_cr + (dest_offset >> 1), ptr,  s->uvlinesize, h >> 1);
@@ -1211,7 +1248,7 @@ static inline void MPV_motion(MpegEncContext *s,
             if(s->flags&CODEC_FLAG_EMU_EDGE){
                 if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 8 > s->width
                                       || src_y + (motion_y&1) + 8 > s->height){
-                    emulated_edge_mc(s->edge_emu_buffer, ptr, s->linesize, 9, 9, src_x, src_y, s->width, s->height);
+                    emulated_edge_mc(s, ptr, s->linesize, 9, 9, src_x, src_y, s->width, s->height);
                     ptr= s->edge_emu_buffer;
                 }
             }
@@ -1258,7 +1295,7 @@ static inline void MPV_motion(MpegEncContext *s,
         if(s->flags&CODEC_FLAG_EMU_EDGE){
                 if(src_x<0 || src_y<0 || src_x + (dxy &1) + 8 > s->width >>1
                                       || src_y + (dxy>>1) + 8 > s->height>>1){
-                    emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->width>>1, s->height>>1);
+                    emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->width>>1, s->height>>1);
                     ptr= s->edge_emu_buffer;
                     emu=1;
                 }
@@ -1267,7 +1304,7 @@ static inline void MPV_motion(MpegEncContext *s,
 
         ptr = ref_picture[2] + offset;
         if(emu){
-            emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->width>>1, s->height>>1);
+            emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->width>>1, s->height>>1);
             ptr= s->edge_emu_buffer;
         }
         pix_op[dxy](dest_cr, ptr, s->uvlinesize, 8);
@@ -1377,8 +1414,10 @@ void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
     quant_store[mb_y][mb_x]=s->qscale;
     //printf("[%02d][%02d] %d\n",mb_x,mb_y,s->qscale);
 #else
+    /* even more obsolete, exists for mplayer xp only */
     if(s->avctx->quant_store) s->avctx->quant_store[mb_y*s->avctx->qstride+mb_x] = s->qscale;
 #endif
+    s->qscale_table[mb_xy]= s->qscale;
 
     /* update DC predictors for P macroblocks */
     if (!s->mb_intra) {
@@ -1603,9 +1642,15 @@ static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
     if (s->mb_intra) {
         UINT8 *ptr;
         int wrap;
+        int emu=0;
 
         wrap = s->linesize;
         ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
+        if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
+            emulated_edge_mc(s, ptr, wrap, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
+            ptr= s->edge_emu_buffer;
+            emu=1;
+        }
         get_pixels(s->block[0], ptr               , wrap);
         get_pixels(s->block[1], ptr            + 8, wrap);
         get_pixels(s->block[2], ptr + 8 * wrap    , wrap);
@@ -1617,9 +1662,17 @@ static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
         }else{
             wrap >>=1;
             ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
+            if(emu){
+                emulated_edge_mc(s, ptr, wrap, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
+                ptr= s->edge_emu_buffer;
+            }
             get_pixels(s->block[4], ptr, wrap);
 
             ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
+            if(emu){
+                emulated_edge_mc(s, ptr, wrap, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
+                ptr= s->edge_emu_buffer;
+            }
             get_pixels(s->block[5], ptr, wrap);
         }
     }else{
@@ -1628,6 +1681,7 @@ static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
         UINT8 *dest_y, *dest_cb, *dest_cr;
         UINT8 *ptr_y, *ptr_cb, *ptr_cr;
         int wrap_y, wrap_c;
+        int emu=0;
 
         dest_y  = s->current_picture[0] + (mb_y * 16 * s->linesize       ) + mb_x * 16;
         dest_cb = s->current_picture[1] + (mb_y * 8  * (s->uvlinesize)) + mb_x * 8;
@@ -1657,6 +1711,11 @@ static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
             MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
         }
 
+        if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
+            emulated_edge_mc(s, ptr_y, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
+            ptr_y= s->edge_emu_buffer;
+            emu=1;
+        }
         diff_pixels(s->block[0], ptr_y                 , dest_y                 , wrap_y);
         diff_pixels(s->block[1], ptr_y              + 8, dest_y              + 8, wrap_y);
         diff_pixels(s->block[2], ptr_y + 8 * wrap_y    , dest_y + 8 * wrap_y    , wrap_y);
@@ -1666,7 +1725,15 @@ static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
             skip_dct[4]= 1;
             skip_dct[5]= 1;
         }else{
+            if(emu){
+                emulated_edge_mc(s, ptr_cb, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
+                ptr_cb= s->edge_emu_buffer;
+            }
             diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
+            if(emu){
+                emulated_edge_mc(s, ptr_cr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
+                ptr_cr= s->edge_emu_buffer;
+            }
             diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
         }