changeset 1421:340c90faa1dc libavcodec

mpeg2 encoding
author michaelni
date Mon, 25 Aug 2003 22:47:32 +0000
parents ff0220970711
children efeed6e29f9b
files allcodecs.c avcodec.h motion_est.c mpeg12.c mpegvideo.c mpegvideo.h
diffstat 6 files changed, 222 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- a/allcodecs.c	Mon Aug 25 12:50:15 2003 +0000
+++ b/allcodecs.c	Mon Aug 25 22:47:32 2003 +0000
@@ -52,6 +52,7 @@
     register_avcodec(&mpeg1video_encoder);
 //    register_avcodec(&h264_encoder);
 #ifdef CONFIG_RISKY
+    register_avcodec(&mpeg2video_encoder);
     register_avcodec(&h263_encoder);
     register_avcodec(&h263p_encoder);
     register_avcodec(&flv_encoder);
--- a/avcodec.h	Mon Aug 25 12:50:15 2003 +0000
+++ b/avcodec.h	Mon Aug 25 22:47:32 2003 +0000
@@ -15,14 +15,15 @@
 
 #define LIBAVCODEC_VERSION_INT 0x000406
 #define LIBAVCODEC_VERSION     "0.4.6"
-#define LIBAVCODEC_BUILD       4675
-#define LIBAVCODEC_BUILD_STR   "4675"
+#define LIBAVCODEC_BUILD       4676
+#define LIBAVCODEC_BUILD_STR   "4676"
 
 #define LIBAVCODEC_IDENT	"FFmpeg" LIBAVCODEC_VERSION "b" LIBAVCODEC_BUILD_STR
 
 enum CodecID {
     CODEC_ID_NONE, 
     CODEC_ID_MPEG1VIDEO,
+    CODEC_ID_MPEG2VIDEO,
     CODEC_ID_MPEG2VIDEO_XVMC,
     CODEC_ID_H263,
     CODEC_ID_RV10,
@@ -219,6 +220,7 @@
 #define CODEC_CAP_PARSE_ONLY      0x0004
 #define CODEC_CAP_TRUNCATED       0x0008
 
+
 #define FF_COMMON_FRAME \
     /**\
      * pointer to the picture planes.\
@@ -462,7 +464,7 @@
      *                    do something for a generic case as well.
      * - decoding: set by lavc.
      */
-    enum PixelFormat pix_fmt;
+    enum PixelFormat pix_fmt; //FIXME move to AVFrme
  
     /**
      * Frame rate emulation. If not zero lower layer (i.e. format handler) 
@@ -710,7 +712,7 @@
                        data is returned. Only MPEG codecs support this now. */
     
     /**
-     * 0-> h263 quant 1-> mpeg quant.
+     * 0-> default, 1-> mpeg quant.
      * - encoding: set by user.
      * - decoding: unused
      */
@@ -1281,6 +1283,7 @@
 extern AVCodec mp3lame_encoder;
 extern AVCodec oggvorbis_encoder;
 extern AVCodec mpeg1video_encoder;
+extern AVCodec mpeg2video_encoder;
 extern AVCodec h263_encoder;
 extern AVCodec h263p_encoder;
 extern AVCodec flv_encoder;
@@ -1587,7 +1590,7 @@
 int avcodec(void* handle, avc_cmd_t cmd, void* pin, void* pout);
 
 /* memory */
-void *av_malloc(unsigned int size);
+void *av_malloc(unsigned int size); //FIXME unsigned could be bad, dunno, need thinking
 void *av_mallocz(unsigned int size);
 void *av_realloc(void *ptr, unsigned int size);
 void av_free(void *ptr);
--- a/motion_est.c	Mon Aug 25 12:50:15 2003 +0000
+++ b/motion_est.c	Mon Aug 25 22:47:32 2003 +0000
@@ -32,8 +32,8 @@
 #include "dsputil.h"
 #include "mpegvideo.h"
 
-//#undef NDEBUG
-//#include <assert.h>
+#undef NDEBUG
+#include <assert.h>
 
 #define SQ(a) ((a)*(a))
 
@@ -1507,7 +1507,7 @@
         type = MB_TYPE_FORWARD;
         
         // RAL: No MB_TYPE_DIRECT in MPEG-1 video (only MPEG-4)
-        if (s->codec_id != CODEC_ID_MPEG1VIDEO && dmin <= score){
+        if (s->codec_id == CODEC_ID_MPEG4 && dmin <= score){
             score = dmin;
             type = MB_TYPE_DIRECT;
         }
@@ -1586,8 +1586,9 @@
 {
     const int f_code= s->f_code;
     int y, range;
+    assert(s->pict_type==P_TYPE);
 
-    range = (((s->codec_id == CODEC_ID_MPEG1VIDEO) ? 8 : 16) << f_code);
+    range = (((s->out_format == FMT_MPEG1) ? 8 : 16) << f_code);
     
     if(s->msmpeg4_version) range= 16;
     
@@ -1647,7 +1648,7 @@
     int y;
 
     // RAL: 8 in MPEG-1, 16 in MPEG-4
-    int range = (((s->codec_id == CODEC_ID_MPEG1VIDEO) ? 8 : 16) << f_code);
+    int range = (((s->out_format == FMT_MPEG1) ? 8 : 16) << f_code);
     
     if(s->avctx->me_range && range > s->avctx->me_range) range= s->avctx->me_range;
 
@@ -1673,3 +1674,53 @@
         }
     }
 }
+#if 0
+/**
+ * estimates global motion and inits sprite_ref
+ */
+void ff_estimate_global_motion(MpegEncContext *s, int sprite_ref[3][2]){
+    int y;
+    int num= 16<<s->f_code;
+    int score[2][num];
+    int best_i[2]={0,0};
+    int best_score[2]={0,0};
+    
+    memset(score, 0, 2*num*sizeof(int));
+    
+    for(y=0; y<s->mb_height; y++){
+        int x;
+        int xy= (y+1)* (s->mb_width+2)+1;
+        int i= y*s->mb_width;
+        for(x=0; x<s->mb_width; x++){
+            int mv[2];
+           
+            if(!(s->mb_type[i]&MB_TYPE_INTER))
+                continue;
+            
+            mv[0]= s->p_mv_table[xy][0];
+            mv[1]= s->p_mv_table[xy][1];
+
+            if(mv[0]==0 && mv[1]==0) continue;
+            
+            score[0][mv[0] + num/2]++;
+            score[1][mv[1] + num/2]++;
+        }
+    }
+
+    for(n=0; n<2; n++){
+        for(i=1; i<num-1; i++){
+            int s= score[n][i-1] + score[n][i]*2 + score[n][i+1];
+    
+            if(s > best_score[n]){
+                best_score[n]= s;
+                best_i[n]= i;
+            }
+        }
+    }
+    
+    sprite_ref[0][0]= best_i[0] - num/2;
+    sprite_ref[0][1]= best_i[1] - num/2;
+    
+    // decide block type
+}
+#endif
--- a/mpeg12.c	Mon Aug 25 12:50:15 2003 +0000
+++ b/mpeg12.c	Mon Aug 25 22:47:32 2003 +0000
@@ -19,7 +19,7 @@
  
 /**
  * @file mpeg12.c
- * MPEG1 codec / MPEG2 decoder.
+ * MPEG1/2 codec
  */
  
 //#define DEBUG
@@ -228,9 +228,9 @@
             put_bits(&s->pb, 4, s->aspect_ratio_info);
             put_bits(&s->pb, 4, s->frame_rate_index);
             v = (s->bit_rate + 399) / 400;
-            if (v > 0x3ffff)
+            if (v > 0x3ffff && s->codec_id == CODEC_ID_MPEG1VIDEO)
                 v = 0x3ffff;
-            put_bits(&s->pb, 18, v);
+            put_bits(&s->pb, 18, v & 0x3FFFF);
             put_bits(&s->pb, 1, 1); /* marker */
 
             if(s->avctx->rc_buffer_size)
@@ -238,12 +238,30 @@
             else
                 /* VBV calculation: Scaled so that a VCD has the proper VBV size of 40 kilobytes */
                 vbv_buffer_size = (( 20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024;	 
-            put_bits(&s->pb, 10, (vbv_buffer_size + 16383) / 16384); 
+            put_bits(&s->pb, 10, ((vbv_buffer_size + 16383) / 16384) & 0x3FF); 
             put_bits(&s->pb, 1, 1); /* constrained parameter flag */
             
             ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
             ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
 
+            if(s->codec_id == CODEC_ID_MPEG2VIDEO){
+                put_header(s, EXT_START_CODE);
+                put_bits(&s->pb, 4, 1); //seq ext
+                put_bits(&s->pb, 1, 0); //esc
+                put_bits(&s->pb, 3, 4); //profile
+                put_bits(&s->pb, 4, 8); //level
+                put_bits(&s->pb, 1, s->progressive_sequence=1);
+                put_bits(&s->pb, 2, 1); //chroma format 4:2:0
+                put_bits(&s->pb, 2, 0); //horizontal size ext
+                put_bits(&s->pb, 2, 0); //vertical size ext
+                put_bits(&s->pb, 12, v>>18); //bitrate ext
+                put_bits(&s->pb, 1, 1); //marker
+                put_bits(&s->pb, 8, vbv_buffer_size >>10); //vbv buffer ext
+                put_bits(&s->pb, 1, s->low_delay);
+                put_bits(&s->pb, 2, 0); // frame_rate_ext_n
+                put_bits(&s->pb, 5, 0); // frame_rate_ext_d
+            }
+            
             put_header(s, GOP_START_CODE);
             put_bits(&s->pb, 1, 0); /* do drop frame */
             /* time code : we must convert from the real frame rate to a
@@ -286,6 +304,8 @@
 /* insert a fake P picture */
 static void mpeg1_skip_picture(MpegEncContext *s, int pict_num)
 {
+    assert(s->codec_id == CODEC_ID_MPEG1VIDEO); // mpeg2 can do these repeat things
+
     /* mpeg1 picture header */
     put_header(s, PICTURE_START_CODE);
     /* temporal reference */
@@ -372,10 +392,31 @@
     if (s->pict_type == B_TYPE) {
         put_bits(&s->pb, 1, 0); /* half pel coordinates */
         put_bits(&s->pb, 3, s->b_code); /* backward_f_code */
-        }
+    }
 
     put_bits(&s->pb, 1, 0); /* extra bit picture */
     
+    if(s->codec_id == CODEC_ID_MPEG2VIDEO){
+        put_header(s, EXT_START_CODE);
+        put_bits(&s->pb, 4, 8); //pic ext
+        put_bits(&s->pb, 4, s->f_code);
+        put_bits(&s->pb, 4, s->f_code);
+        put_bits(&s->pb, 4, s->b_code);
+        put_bits(&s->pb, 4, s->b_code);
+        put_bits(&s->pb, 2, s->intra_dc_precision);
+        put_bits(&s->pb, 2, s->picture_structure= PICT_FRAME);
+        put_bits(&s->pb, 1, s->top_field_first);
+        put_bits(&s->pb, 1, s->frame_pred_frame_dct= 1);
+        put_bits(&s->pb, 1, s->concealment_motion_vectors);
+        put_bits(&s->pb, 1, s->q_scale_type);
+        put_bits(&s->pb, 1, s->intra_vlc_format);
+        put_bits(&s->pb, 1, s->alternate_scan);
+        put_bits(&s->pb, 1, s->repeat_first_field);
+        put_bits(&s->pb, 1, s->chroma_420_type=1);
+        put_bits(&s->pb, 1, s->progressive_frame=1);
+        put_bits(&s->pb, 1, 0); //composite_display_flag
+    }
+    
     s->mb_y=0;
     ff_mpeg1_encode_slice_header(s);
 }
@@ -396,7 +437,7 @@
             cbp |= 1 << (5 - i);
     }
 
-    if (cbp == 0 && !first_mb && (mb_x != s->mb_width - 1 || mb_y != s->mb_height - 1) && 
+    if (cbp == 0 && !first_mb && (mb_x != s->mb_width - 1 || (mb_y != s->mb_height - 1 && s->codec_id == CODEC_ID_MPEG1VIDEO)) && 
         ((s->pict_type == P_TYPE && (motion_x | motion_y) == 0) ||
         (s->pict_type == B_TYPE && s->mv_dir == s->last_mv_dir && (((s->mv_dir & MV_DIR_FORWARD) ? ((s->mv[0][0][0] - s->last_mv[0][0][0])|(s->mv[0][0][1] - s->last_mv[0][0][1])) : 0) |
         ((s->mv_dir & MV_DIR_BACKWARD) ? ((s->mv[1][0][0] - s->last_mv[1][0][0])|(s->mv[1][0][1] - s->last_mv[1][0][1])) : 0)) == 0))) {
@@ -708,8 +749,13 @@
     }
     s->me.mv_penalty= mv_penalty;
     s->fcode_tab= fcode_tab;
-    s->min_qcoeff=-255;
-    s->max_qcoeff= 255;
+    if(s->codec_id == CODEC_ID_MPEG1VIDEO){
+        s->min_qcoeff=-255;
+        s->max_qcoeff= 255;
+    }else{
+        s->min_qcoeff=-2047;
+        s->max_qcoeff= 2047;
+    }
     s->intra_ac_vlc_length=
     s->inter_ac_vlc_length= uni_mpeg1_ac_vlc_len;
 }
@@ -747,6 +793,12 @@
         encode_dc(s, diff, component);
         s->last_dc[component] = dc;
         i = 1;
+/*
+        if (s->intra_vlc_format)
+            rl = &rl_mpeg2;
+        else
+            rl = &rl_mpeg1;
+*/
     } else {
         /* encode the first coefficient : needs to be done here because
            it is handled slightly differently */
@@ -791,14 +843,18 @@
                 put_bits(&s->pb, mpeg1_vlc[111/*rl->n*/][1], mpeg1_vlc[111/*rl->n*/][0]);
                 /* escape: only clip in this case */
                 put_bits(&s->pb, 6, run);
-                if (alevel < 128) {
-                    put_bits(&s->pb, 8, level & 0xff);
-                } else {
-                    if (level < 0) {
-                        put_bits(&s->pb, 16, 0x8001 + level + 255);
+                if(s->codec_id == CODEC_ID_MPEG1VIDEO){
+                    if (alevel < 128) {
+                        put_bits(&s->pb, 8, level & 0xff);
                     } else {
-                        put_bits(&s->pb, 16, level & 0xffff);
+                        if (level < 0) {
+                            put_bits(&s->pb, 16, 0x8001 + level + 255);
+                        } else {
+                            put_bits(&s->pb, 16, level & 0xffff);
+                        }
                     }
+                }else{
+                    put_bits(&s->pb, 12, level & 0xfff);
                 }
             }
             last_non_zero = i;
@@ -868,7 +924,7 @@
 static inline int get_qscale(MpegEncContext *s)
 {
     int qscale = get_bits(&s->gb, 5);
-    if (s->mpeg2) {
+    if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
         if (s->q_scale_type) {
             return non_linear_qscale[qscale];
         } else {
@@ -987,7 +1043,7 @@
             memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
         s->mb_intra = 1;
 
-        if (s->mpeg2) {
+        if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
             for(i=0;i<6;i++) {
                 if (mpeg2_decode_block_intra(s, block[i], i) < 0)
                     return -1;
@@ -1165,7 +1221,7 @@
             }
             cbp++;
 
-            if (s->mpeg2) {
+            if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
                 for(i=0;i<6;i++) {
                     if (cbp & 32) {
                         if (mpeg2_decode_block_non_intra(s, block[i], i) < 0)
@@ -1648,9 +1704,12 @@
     int horiz_size_ext, vert_size_ext;
     int bit_rate_ext, vbv_buf_ext;
     int frame_rate_ext_n, frame_rate_ext_d;
+    int level, profile;
     float aspect;
 
-    skip_bits(&s->gb, 8); /* profil and level */
+    skip_bits(&s->gb, 1); /* profil and level esc*/
+    profile= get_bits(&s->gb, 3);
+    level= get_bits(&s->gb, 4);
     s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
     skip_bits(&s->gb, 2); /* chroma_format */
     horiz_size_ext = get_bits(&s->gb, 2);
@@ -1675,12 +1734,15 @@
         1<<30);
 
     dprintf("sequence extension\n");
-    s->mpeg2 = 1;
+    s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
     s->avctx->sub_id = 2; /* indicates mpeg2 found */
 
     aspect= mpeg2_aspect[s->aspect_ratio_info];
     if(aspect>0.0)      s->avctx->aspect_ratio= s->width/(aspect*s->height);
     else if(aspect<0.0) s->avctx->aspect_ratio= -1.0/aspect;
+    
+    if(s->avctx->debug & FF_DEBUG_PICT_INFO)
+        printf("profile: %d, level: %d \n", profile, level);
 }
 
 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
@@ -2046,7 +2108,7 @@
     if (/*s->mb_y<<field_pic == s->mb_height &&*/ !s->first_field) {
         /* end of image */
 
-        if(s->mpeg2){
+        if(s->codec_id == CODEC_ID_MPEG2VIDEO){
             s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG2;
         }else
             s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG1;
@@ -2089,7 +2151,7 @@
     width = get_bits(&s->gb, 12);
     height = get_bits(&s->gb, 12);
     s->aspect_ratio_info= get_bits(&s->gb, 4);
-    if(!s->mpeg2){
+    if(s->codec_id == CODEC_ID_MPEG1VIDEO){
         aspect= mpeg1_aspect[s->aspect_ratio_info];
         if(aspect!=0.0) avctx->aspect_ratio= width/(aspect*height);
     }
@@ -2187,7 +2249,7 @@
     s->progressive_frame = 1;
     s->picture_structure = PICT_FRAME;
     s->frame_pred_frame_dct = 1;
-    s->mpeg2 = 0;
+    s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG1VIDEO;
     avctx->sub_id = 1; /* indicates mpeg1 */
     return 0;
 }
@@ -2233,7 +2295,7 @@
     s->progressive_frame = 1;
     s->picture_structure = PICT_FRAME;
     s->frame_pred_frame_dct = 1;
-    s->mpeg2 = 1;
+    s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
     avctx->sub_id = 2; /* indicates mpeg2 */
     return 0;
 }
--- a/mpegvideo.c	Mon Aug 25 12:50:15 2003 +0000
+++ b/mpegvideo.c	Mon Aug 25 22:47:32 2003 +0000
@@ -34,8 +34,8 @@
 #include "fastmemcpy.h"
 #endif
 
-//#undef NDEBUG
-//#include <assert.h>
+#undef NDEBUG
+#include <assert.h>
 
 #ifdef CONFIG_ENCODERS
 static void encode_picture(MpegEncContext *s, int picture_number);
@@ -191,7 +191,7 @@
 #endif
         
 #ifdef HAVE_MMX
-    MPV_common_init_mmx(s);
+    MPV_common_init_mmx(s); //FIXME dont pass mpegenccontext to these, rather use dspcontext
 #endif
 #ifdef ARCH_ALPHA
     MPV_common_init_axp(s);
@@ -279,7 +279,7 @@
         }
 
         CHECKED_ALLOCZ(pic->mbskip_table , mb_array_size * sizeof(uint8_t)+2) //the +2 is for the slice end check
-        CHECKED_ALLOCZ(pic->qscale_table , mb_array_size * sizeof(uint8_t))
+        CHECKED_ALLOCZ(pic->qscale_table , mb_array_size * sizeof(uint8_t)+3) //+3 for mpeg2 SIMD >>1
         CHECKED_ALLOCZ(pic->mb_type_base , big_mb_num    * sizeof(int))
         pic->mb_type= pic->mb_type_base + s->mb_stride+1;
         if(s->out_format == FMT_H264){
@@ -424,7 +424,7 @@
         
     CHECKED_ALLOCZ(s->error_status_table, mb_array_size*sizeof(uint8_t))
     
-    if (s->out_format == FMT_H263 || s->encoding) {
+    if (s->out_format == FMT_H263 || s->encoding || 1) {
         int size;
 
         /* MV prediction */
@@ -581,7 +581,7 @@
     s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
     s->quarter_sample= (avctx->flags & CODEC_FLAG_QPEL)!=0;
     s->mpeg_quant= avctx->mpeg_quant;
-
+    
     if (s->gop_size <= 1) {
         s->intra_only = 1;
         s->gop_size = 12;
@@ -618,20 +618,20 @@
         return -1;
     }
     
-    if(s->max_b_frames && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG1VIDEO){
+    if(s->max_b_frames && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG1VIDEO && s->codec_id != CODEC_ID_MPEG2VIDEO){
         fprintf(stderr, "b frames not supporetd by codec\n");
         return -1;
     }
-    
+/*    
     if(s->mpeg_quant && s->codec_id != CODEC_ID_MPEG4){ //FIXME mpeg2 uses that too
         fprintf(stderr, "mpeg2 style quantization not supporetd by codec\n");
         return -1;
     }
-        
+  */      
     if(s->codec_id==CODEC_ID_MJPEG){
         s->intra_quant_bias= 1<<(QUANT_BIAS_SHIFT-1); //(a + x/2)/x
         s->inter_quant_bias= 0;
-    }else if(s->mpeg_quant || s->codec_id==CODEC_ID_MPEG1VIDEO){
+    }else if(s->mpeg_quant || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO){
         s->intra_quant_bias= 3<<(QUANT_BIAS_SHIFT-3); //(a + x*3/8)/x
         s->inter_quant_bias= 0;
     }else{
@@ -652,6 +652,13 @@
         s->low_delay= 0; //s->max_b_frames ? 0 : 1;
         avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
         break;
+    case CODEC_ID_MPEG2VIDEO:
+        s->out_format = FMT_MPEG1;
+        s->low_delay= 0; //s->max_b_frames ? 0 : 1;
+        avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
+        s->rtp_mode= 1; // mpeg2 must have slices
+        if(s->rtp_payload_size == 0) s->rtp_payload_size= 256*256*256;
+        break;
     case CODEC_ID_LJPEG:
     case CODEC_ID_MJPEG:
         s->out_format = FMT_MJPEG;
@@ -820,7 +827,7 @@
             s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
         }else
 #endif
-        { /* mpeg1 */
+        { /* mpeg1/2 */
             s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
             s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
         }
@@ -1002,6 +1009,7 @@
         
         if( alloc_picture(s, (Picture*)pic, 0) < 0)
             return -1;
+        assert(pic->data[0]);
 
         s->current_picture_ptr= &s->picture[i];
     }
@@ -1048,12 +1056,11 @@
 
     /* set dequantizer, we cant do it during init as it might change for mpeg4
        and we cant do it in the header decode as init isnt called for mpeg4 there yet */
-    if(s->out_format == FMT_H263){
-        if(s->mpeg_quant)
-            s->dct_unquantize = s->dct_unquantize_mpeg2;
-        else
-            s->dct_unquantize = s->dct_unquantize_h263;
-    }else 
+    if(s->mpeg_quant || s->codec_id == CODEC_ID_MPEG2VIDEO) 
+        s->dct_unquantize = s->dct_unquantize_mpeg2;
+    else if(s->out_format == FMT_H263)
+        s->dct_unquantize = s->dct_unquantize_h263;
+    else 
         s->dct_unquantize = s->dct_unquantize_mpeg1;
 
 #ifdef HAVE_XVMC
@@ -1074,7 +1081,7 @@
         XVMC_field_end(s);
     }else
 #endif
-    if(s->codec_id!=CODEC_ID_SVQ1 && s->codec_id != CODEC_ID_MPEG1VIDEO){
+    if(s->codec_id!=CODEC_ID_SVQ1 && s->out_format != FMT_MPEG1){
         if (s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
             draw_edges(s->current_picture.data[0], s->linesize  , s->h_edge_pos   , s->v_edge_pos   , EDGE_WIDTH  );
             draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
@@ -2345,7 +2352,8 @@
         }
     }
     break;
-
+    default:
+        printf("X");
     }
 }
 
@@ -2523,7 +2531,7 @@
             if(s->hurry_up>1) return;
 
             /* add dct residue */
-            if(s->encoding || !(   s->mpeg2 || s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO 
+            if(s->encoding || !(   s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO
                                 || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
                 add_dequant_dct(s, block[0], 0, dest_y, dct_linesize);
                 add_dequant_dct(s, block[1], 1, dest_y + 8, dct_linesize);
@@ -2552,7 +2560,7 @@
 #endif
         } else {
             /* dct only in intra block */
-            if(s->encoding || !(s->mpeg2 || s->codec_id==CODEC_ID_MPEG1VIDEO)){
+            if(s->encoding || !(s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO)){
                 put_dct(s, block[0], 0, dest_y, dct_linesize);
                 put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
                 put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
@@ -2997,7 +3005,9 @@
         for(i=0;i<6;i++) {
             if(!skip_dct[i]){
                 int overflow;
+START_TIMER;
                 s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
+STOP_TIMER("dct_quant");
             // FIXME we could decide to change to quantizer instead of clipping
             // JS: I don't think that would be a good idea it could lower quality instead
             //     of improve it. Just INTRADC clipping deserves changes in quantizer
@@ -3023,6 +3033,7 @@
     /* huffman encode */
     switch(s->codec_id){ //FIXME funct ptr could be slightly faster
     case CODEC_ID_MPEG1VIDEO:
+    case CODEC_ID_MPEG2VIDEO:
         mpeg1_encode_mb(s, s->block, motion_x, motion_y); break;
 #ifdef CONFIG_RISKY
     case CODEC_ID_MPEG4:
@@ -3316,7 +3327,8 @@
 #ifdef CONFIG_RISKY
     /* we need to initialize some time vars before we can encode b-frames */
     // RAL: Condition added for MPEG1VIDEO
-    if (s->codec_id == CODEC_ID_MPEG1VIDEO || (s->h263_pred && !s->h263_msmpeg4))
+    //FIXME figure out why mpeg1/2 need this !!!
+    if (s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->h263_msmpeg4))
         ff_set_mpeg4_time(s, s->picture_number); 
 #endif
         
@@ -3562,15 +3574,14 @@
 
             /* write gob / video packet header  */
 #ifdef CONFIG_RISKY
-            if(s->rtp_mode){
+            if(s->rtp_mode && mb_y + mb_x>0){
                 int current_packet_size, is_gob_start;
                 
                 current_packet_size= pbBufPtr(&s->pb) - s->ptr_lastgob;
                 is_gob_start=0;
                 
                 if(s->codec_id==CODEC_ID_MPEG4){
-                    if(current_packet_size >= s->rtp_payload_size
-                       && s->mb_y + s->mb_x>0){
+                    if(current_packet_size >= s->rtp_payload_size){
 
                         if(s->partitioned_frame){
                             ff_mpeg4_merge_partitions(s);
@@ -3588,14 +3599,21 @@
                     }
                 }else if(s->codec_id==CODEC_ID_MPEG1VIDEO){
                     if(   current_packet_size >= s->rtp_payload_size 
-                       && s->mb_y + s->mb_x>0 && s->mb_skip_run==0){
+                       && s->mb_skip_run==0){
+                        ff_mpeg1_encode_slice_header(s);
+                        ff_mpeg1_clean_buffers(s);
+                        is_gob_start=1;
+                    }
+                }else if(s->codec_id==CODEC_ID_MPEG2VIDEO){
+                    if(   (   current_packet_size >= s->rtp_payload_size || mb_x==0)
+                       && s->mb_skip_run==0){
                         ff_mpeg1_encode_slice_header(s);
                         ff_mpeg1_clean_buffers(s);
                         is_gob_start=1;
                     }
                 }else{
                     if(current_packet_size >= s->rtp_payload_size
-                       && s->mb_x==0 && s->mb_y>0 && s->mb_y%s->gob_index==0){
+                       && s->mb_x==0 && s->mb_y%s->gob_index==0){
                        
                         h263_encode_gob_header(s, mb_y);                       
                         is_gob_start=1;
@@ -4000,7 +4018,7 @@
         start_i = 1;
         last_non_zero = 0;
         qmat = s->q_intra_matrix[qscale];
-        if(s->mpeg_quant || s->codec_id== CODEC_ID_MPEG1VIDEO)
+        if(s->mpeg_quant)
             bias= 1<<(QMAT_SHIFT-1);
         length     = s->intra_ac_vlc_length;
         last_length= s->intra_ac_vlc_last_length;
@@ -4529,6 +4547,16 @@
 
 #ifdef CONFIG_RISKY
 
+AVCodec mpeg2video_encoder = {
+    "mpeg2video",
+    CODEC_TYPE_VIDEO,
+    CODEC_ID_MPEG2VIDEO,
+    sizeof(MpegEncContext),
+    MPV_encode_init,
+    MPV_encode_picture,
+    MPV_encode_end,
+};
+
 AVCodec h263_encoder = {
     "h263",
     CODEC_TYPE_VIDEO,
--- a/mpegvideo.h	Mon Aug 25 12:50:15 2003 +0000
+++ b/mpegvideo.h	Mon Aug 25 22:47:32 2003 +0000
@@ -48,8 +48,13 @@
 
 #define MAX_PICTURE_COUNT 15
 
+#if 1
+#define ME_MAP_SIZE 4096
+#define ME_MAP_SHIFT 6
+#else
 #define ME_MAP_SIZE 64
 #define ME_MAP_SHIFT 3
+#endif
 #define ME_MAP_MV_BITS 11
 
 /* run length table */
@@ -385,8 +390,10 @@
     int me_method;                       ///< ME algorithm 
     int scene_change_score;
     int mv_dir;
-#define MV_DIR_BACKWARD  1
-#define MV_DIR_FORWARD   2
+#define MV_DIR_L1        1
+#define MV_DIR_L0        2
+#define MV_DIR_BACKWARD  MV_DIR_L1
+#define MV_DIR_FORWARD   MV_DIR_L0
 #define MV_DIRECT        4 ///< bidirectional mode where the difference equals the MV of the last P/S/I-Frame (mpeg4)
     int mv_type;
 #define MV_TYPE_16X16       0   ///< 1 vector for the whole mb 
@@ -568,6 +575,11 @@
     uint8_t *tex_pb_buffer;          
     uint8_t *pb2_buffer;
     int mpeg_quant;
+#define FF_QUANT_DEFAULT 0
+#define FF_QUANT_MPEG2   1
+#define FF_QUANT_MPEG1   2
+#define FF_QUANT_H263    3
+
     int16_t (*field_mv_table)[2][2];   ///< used for interlaced b frame decoding 
     int8_t (*field_select_table)[2];   ///< wtf, no really another table for interlaced b frames 
     int t_frame;                       ///< time distance of first I -> B, used for interlaced b frames 
@@ -644,7 +656,6 @@
     int repeat_first_field;
     int chroma_420_type;
     int progressive_frame;
-    int mpeg2;
     int full_pel[2];
     int interlaced_dct;
     int first_slice;
@@ -652,7 +663,7 @@
     
     /* RTP specific */
     /* These are explained on avcodec.h */
-    int rtp_mode;
+    int rtp_mode; //FIXME simplify this mess (remove callback, merge mode + payload)
     int rtp_payload_size;
     void (*rtp_callback)(void *data, int size, int packet_number);
     uint8_t *ptr_lastgob;
@@ -870,6 +881,7 @@
 int ff_msmpeg4_decode_init(MpegEncContext *s);
 void ff_msmpeg4_encode_init(MpegEncContext *s);
 int ff_wmv2_decode_picture_header(MpegEncContext * s);
+int ff_wmv2_decode_secondary_picture_header(MpegEncContext * s);
 void ff_wmv2_add_mb(MpegEncContext *s, DCTELEM block[6][64], uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr);
 void ff_mspel_motion(MpegEncContext *s,
                                uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,