--- libavcodec/mpeg12enc.c.orig	2019-08-05 22:52:21.000000000 +0200
+++ libavcodec/mpeg12enc.c	2019-09-08 19:21:09.578572070 +0200
@@ -256,7 +256,7 @@
 
         put_sbits(&s->pb, 12, s->width  & 0xFFF);
         put_sbits(&s->pb, 12, s->height & 0xFFF);
-
+#if 0 // MEANX
         for (i = 1; i < 15; i++) {
             int64_t error = aspect_ratio.num * (1LL<<32) / aspect_ratio.den;
             if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO || i <= 1)
@@ -271,12 +271,41 @@
                 s->aspect_ratio_info = i;
             }
         }
+#endif // MEANX
+            // MEANX put_bits(&s->pb, 4, s->aspect_ratio_info);
+            // MEANX put_bits(&s->pb, 4, s->frame_rate_index);
+ // MEANX 4:3
+   if(s->avctx->sample_aspect_ratio.num==16 && s->avctx->sample_aspect_ratio.den==9)
+             {
+                 //printf("FFmpeg : Wide\n");
+                 put_bits(&s->pb,4,3); //16:9
+             }
+             else        //4:3
+             {
+               if(s->codec_id == AV_CODEC_ID_MPEG2VIDEO)
+                 put_bits(&s->pb, 4, 2);
+               else
+                 put_bits(&s->pb, 4, 12); // MPEG1
+             }
+ // /MEANX
+// MEANX : Pulldown
+            if((s->avctx->flags2 & AV_CODEC_FLAG2_32_PULLDOWN) && (s->codec_id == AV_CODEC_ID_MPEG2VIDEO))
+             {           
+                 put_bits(&s->pb, 4,4);
+             }
+             else
+             {                                  
+                 put_bits(&s->pb, 4, s->frame_rate_index);
+             } //MEANX pulldown
+
+            // MEANX if(s->avctx->rc_max_rate){
+                // MEANX v = (s->avctx->rc_max_rate + 399) / 400;
+           // MEANX  if(s->avctx->rc_max_rate_header){ //MEANX we use header
+           // MEANX      v = (s->avctx->rc_max_rate_header + 399) / 400;
+            if(s->avctx->rc_buffer_size_header) // MEANX we use header
+            {
+                vbv_buffer_size = s->avctx->rc_buffer_size_header;
 
-        put_bits(&s->pb, 4, s->aspect_ratio_info);
-        put_bits(&s->pb, 4, s->frame_rate_index);
-
-        if (s->avctx->rc_max_rate) {
-            v = (s->avctx->rc_max_rate + 399) / 400;
             if (v > 0x3ffff && s->codec_id == AV_CODEC_ID_MPEG1VIDEO)
                 v = 0x3ffff;
         } else {
@@ -326,7 +355,19 @@
             put_bits(&s->pb, 3, s->avctx->profile); // profile
             put_bits(&s->pb, 4, s->avctx->level);   // level
 
-            put_bits(&s->pb, 1, s->progressive_sequence);
+   // MEANX pulldown put_bits(&s->pb, 1, s->progressive_sequence);
+  // MEANX Pulldown
+ if(s->avctx->flags2 & AV_CODEC_FLAG2_32_PULLDOWN) //MEANX
+                        put_bits(&s->pb, 1, 0);
+                else
+                        put_bits(&s->pb, 1, s->progressive_sequence);
+
+
+// /MEANX
+
+
+
+// MEANX                 put_bits(&s->pb, 1, s->progressive_sequence);
             put_bits(&s->pb, 2, s->chroma_format);
             put_bits(&s->pb, 2, s->width  >> 12);
             put_bits(&s->pb, 2, s->height >> 12);
@@ -423,6 +464,7 @@
 
 void ff_mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
 {
+    int tff, rff; //MEANX
     AVFrameSideData *side_data;
     mpeg1_encode_sequence_header(s);
 
@@ -461,6 +503,46 @@
 
     s->frame_pred_frame_dct = 1;
     if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
+// MEANX
+        if(s->avctx->flags2 & AV_CODEC_FLAG2_32_PULLDOWN)
+        {
+        
+                switch((s->picture_number - 
+                          s->gop_picture_number)&3)
+                {
+                        case 0:
+                        default:
+                                rff=1;
+                                tff=1;
+                                break;
+                        case 1:
+                                rff=0;
+                                tff=0;
+                                break;
+                        case 2:
+                                rff=1;
+                                tff=0;
+                                break;
+                        case 3:
+                                rff=0;
+                                tff=1;
+                                break;
+                }               
+           }
+        else
+        {
+                if (s->progressive_sequence) 
+                {
+                        tff=0; /* no repeat */
+                } else 
+                {
+                        tff=s->current_picture_ptr->f->top_field_first;
+                }
+                rff=s->repeat_first_field;
+        
+        }
+
+// /MEANX
         put_header(s, EXT_START_CODE);
         put_bits(&s->pb, 4, 8);                 /* pic ext */
         if (s->pict_type == AV_PICTURE_TYPE_P ||
@@ -480,10 +562,14 @@
 
         av_assert0(s->picture_structure == PICT_FRAME);
         put_bits(&s->pb, 2, s->picture_structure);
+#if 0 // MEANX
         if (s->progressive_sequence)
             put_bits(&s->pb, 1, 0);             /* no repeat */
         else
             put_bits(&s->pb, 1, s->current_picture_ptr->f->top_field_first);
+#endif // MEANX
+        put_bits(&s->pb, 1, tff);  //MEANX PULLDOWN
+
         /* XXX: optimize the generation of this flag with entropy measures */
         s->frame_pred_frame_dct = s->progressive_sequence;
 
@@ -492,7 +578,8 @@
         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);
+        // MEANX put_bits(&s->pb, 1, s->top_field_first);
+        put_bits(&s->pb, 1, rff); // MEANX
         s->progressive_frame = s->progressive_sequence;
         /* chroma_420_type */
         put_bits(&s->pb, 1, s->chroma_format ==
