Index: mpegenc.c
===================================================================
--- mpegenc.c	(revision 2048)
+++ mpegenc.c	(working copy)
@@ -303,6 +303,16 @@
             goto fail;
         st->priv_data = stream;
 
+        /*set PESStream format*/
+        if(s->is_dvd){
+           stream->format == PES_FMT_DVD;
+        }else if(s->is_svcd){
+           stream->format == PES_FMT_SVCD;
+        }else if(s->is_mpeg2){
+           stream->format == PES_FMT_MPEG2;
+        }else{
+           stream->format == PES_FMT_VCD;
+        }
         av_set_pts_info(st, 64, 1, 90000);
 
         switch(st->codec->codec_type) {
@@ -712,84 +722,11 @@
     packet_size -= pad_packet_bytes + zero_trail_bytes;
 
     if (packet_size > 0) {
+        ff_pes_cal_header(id,stream,
+          &packet_size,&header_len,&pts,&dts,
+          &payload_size,&startcode,&stuffing_size,
+          &trailer_size,&pad_packet_bytes);
 
-        /* packet header size */
-        packet_size -= 6;
-
-        /* packet header */
-        if (s->is_mpeg2) {
-            header_len = 3;
-            if (stream->packet_number==0)
-                header_len += 3; /* PES extension */
-            header_len += 1; /* obligatory stuffing byte */
-        } else {
-            header_len = 0;
-        }
-        if (pts != AV_NOPTS_VALUE) {
-            if (dts != pts)
-                header_len += 5 + 5;
-            else
-                header_len += 5;
-        } else {
-            if (!s->is_mpeg2)
-                header_len++;
-        }
-
-        payload_size = packet_size - header_len;
-        if (id < 0xc0) {
-            startcode = PRIVATE_STREAM_1;
-            payload_size -= 1;
-            if (id >= 0x40) {
-                payload_size -= 3;
-                if (id >= 0xa0)
-                    payload_size -= 3;
-            }
-        } else {
-            startcode = 0x100 + id;
-        }
-
-        stuffing_size = payload_size - av_fifo_size(&stream->fifo);
-
-        // first byte does not fit -> reset pts/dts + stuffing
-        if(payload_size <= trailer_size && pts != AV_NOPTS_VALUE){
-            int timestamp_len=0;
-            if(dts != pts)
-                timestamp_len += 5;
-            if(pts != AV_NOPTS_VALUE)
-                timestamp_len += s->is_mpeg2 ? 5 : 4;
-            pts=dts= AV_NOPTS_VALUE;
-            header_len -= timestamp_len;
-            if (s->is_dvd && stream->align_iframe) {
-                pad_packet_bytes += timestamp_len;
-                packet_size -= timestamp_len;
-            } else {
-                payload_size += timestamp_len;
-            }
-            stuffing_size += timestamp_len;
-            if(payload_size > trailer_size)
-                stuffing_size += payload_size - trailer_size;
-        }
-
-        if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) { // can't use padding, so use stuffing
-            packet_size += pad_packet_bytes;
-            payload_size += pad_packet_bytes; // undo the previous adjustment
-            if (stuffing_size < 0) {
-                stuffing_size = pad_packet_bytes;
-            } else {
-                stuffing_size += pad_packet_bytes;
-            }
-            pad_packet_bytes = 0;
-        }
-
-        if (stuffing_size < 0)
-            stuffing_size = 0;
-        if (stuffing_size > 16) {    /*<=16 for MPEG-1, <=32 for MPEG-2*/
-            pad_packet_bytes += stuffing_size;
-            packet_size -= stuffing_size;
-            payload_size -= stuffing_size;
-            stuffing_size = 0;
-        }
-
         nb_frames= ff_pes_get_nb_frames(ctx, stream, payload_size - stuffing_size);
 
         put_be32(ctx->pb, startcode);
Index: mpegpes.h
===================================================================
--- mpegpes.h	(revision 2048)
+++ mpegpes.h	(working copy)
@@ -30,6 +30,12 @@
 #include "avformat.h"
 #include "fifo.h"
 
+#define PES_FMT_MPEG2 0x01
+#define PES_FMT_VCD 0x02
+#define PES_FMT_SVCD 0x04
+#define PES_FMT_DVD 0x08
+#define PES_FMT_TS 0x10
+
 /**
  * PES packet description
  */
@@ -48,6 +54,7 @@
 typedef struct {
     AVFifoBuffer fifo;
     uint8_t id;
+    int format;
     int max_buffer_size; /**< in bytes */
     int buffer_index;
     PacketDesc *predecode_packet;
@@ -118,6 +125,26 @@
 int ff_pes_get_nb_frames(AVFormatContext *ctx, PESStream *stream, int len);
 
 /**
+ * Caculate the PES header to flush
+ * @param[in] id    the stream id
+ * @param[in] stream    the PES stream
+ * @param[in] packet_size    the packet size for PES
+ * @param[in] header_len     the PES header length
+ * @param[in] pts    the PTS
+ * @param[in] dts   the DTS
+ * @param[in] payload_size the PES palyload size 
+ * @param[in] startcode  the startcode 
+ * @param[in]stuffing_size the  PES stuff  size
+ * @param[in] trailer_size the trailer_ size
+ * @param[in] pad_packet_bytes the padding size for packet
+ * @return  NULL
+ */
+void ff_pes_cal_header(int id,PESStream *stream,
+          int *packet_size,int *header_len,int64_t *pts,int64_t *dts,
+          int *payload_size,int *startcode,int *stuffing_size,
+          int *trailer_size,int *pad_packet_bytes);
+
+/**
  * Mux one stream into PES stream.
  * @param [in]      ctx            the AVFormatContext which contains streams
  * @param [in]      stream_index   the stream index to write
Index: mpegpesenc.c
===================================================================
--- mpegpesenc.c	(revision 2048)
+++ mpegpesenc.c	(working copy)
@@ -100,7 +100,112 @@
 
     return nb_frames;
 }
+/**
+ * Caculate the PES header to flush
+ * @param[in] id    the stream id
+ * @param[in] stream    the PES stream
+ * @param[in] packet_size    the packet size for PES
+ * @param[in] header_len     the PES header length
+ * @param[in] pts    the PTS
+ * @param[in] dts   the DTS
+ * @param[in] payload_size the PES palyload size 
+ * @param[in] startcode  the startcode 
+ * @param[in]stuffing_size the  PES stuff  size
+ * @param[in] trailer_size the trailer_ size
+ * @param[in] pad_packet_bytes the padding size for packet
+ * @return  NULL
+ */
+void ff_pes_cal_header(int id,PESStream * stream,
+          int *packet_size,int *header_len,int64_t *pts,int64_t *dts,
+    int *payload_size,int *startcode,int *stuffing_size,
+          int *trailer_size,int *pad_packet_bytes)
+{
+     /* packet header size */
+        *packet_size -= 6;
 
+        /* packet header */
+        if (stream->format & PES_FMT_TS) {
+            *header_len = 3;
+            *header_len += 1; /* obligatory stuffing byte */
+        }else if (stream->format & PES_FMT_MPEG2){
+             *header_len = 3;
+             if (stream->packet_number==0)
+                 *header_len += 3; /* PES extension */
+             *header_len += 1; /* obligatory stuffing byte */
+        } else {
+              *header_len = 0;
+        }
+
+        if (*pts != AV_NOPTS_VALUE) {
+            if (*dts != *pts)
+                *header_len += 5 + 5;
+            else
+                *header_len += 5;
+        }if(!(stream->format & PES_FMT_MPEG2)){
+            (*header_len)++;
+        }
+
+        *payload_size = *packet_size - *header_len;
+        if(!(stream->format & PES_FMT_TS)){
+            if (id < 0xc0) {
+                *startcode = PRIVATE_STREAM_1;
+                *payload_size -= 1;
+                if (id >= 0x40) {
+                    *payload_size -= 3;
+                    if (id >= 0xa0)
+                        *payload_size -= 3;
+                }
+            } else {
+                  *startcode = 0x100 + id;
+            }
+        }
+        *stuffing_size = *payload_size - av_fifo_size(&stream->fifo);
+
+        // first byte does not fit -> reset pts/dts + stuffing
+        if(*payload_size <= *trailer_size && *pts != AV_NOPTS_VALUE){
+            int timestamp_len=0;
+            if(*dts != *pts)
+                timestamp_len += 5;
+            if(*pts != AV_NOPTS_VALUE){
+                timestamp_len += stream->format & PES_FMT_MPEG2 ? 5 : 4;
+            }
+            *pts=*dts=AV_NOPTS_VALUE;
+            *header_len -= timestamp_len;
+            if ((stream->format & PES_FMT_DVD) && stream->align_iframe){
+                    *pad_packet_bytes += timestamp_len;
+                    *packet_size -= timestamp_len;
+                }
+            else {
+                *payload_size += timestamp_len;
+              }     
+                *stuffing_size += timestamp_len;
+                if(*payload_size > *trailer_size)
+                    *stuffing_size += *payload_size - *trailer_size;
+            }
+
+        if(!(stream->format & PES_FMT_TS)){
+            if (*pad_packet_bytes > 0 && *pad_packet_bytes <= 7) { // can't use padding, so use stuffing
+                *packet_size += *pad_packet_bytes;
+                *payload_size += *pad_packet_bytes; // undo the previous adjustment
+                if (*stuffing_size < 0) {
+                    *stuffing_size = *pad_packet_bytes;
+                } else {
+                    *stuffing_size += *pad_packet_bytes;
+                }
+                *pad_packet_bytes = 0;
+            }
+        }
+
+        if (*stuffing_size < 0)
+            *stuffing_size = 0;
+        if (*stuffing_size > 16) {    /*<=16 for MPEG-1, <=32 for MPEG-2*/
+            *pad_packet_bytes += *stuffing_size;
+            *packet_size -= *stuffing_size;
+            *payload_size -= *stuffing_size;
+            *stuffing_size = 0;
+        }
+}
+
 /**
  * Mux one stream into PES stream.
  * @param [in]      ctx            the AVFormatContext which contains streams
Index: mpegtsenc.c
===================================================================
--- mpegtsenc.c	(revision 2048)
+++ mpegtsenc.c	(working copy)
@@ -623,57 +623,23 @@
     MpegTSWrite *s = ctx->priv_data;
     MpegTSWriteStream *stream = ctx->streams[stream_index]->priv_data;
     PESStream *pes_stream = &stream->pes_stream;
-    int payload_size, id, stuffing_size, i, header_len;
+    int payload_size, id, startcode, stuffing_size, i, header_len;
     int packet_size, es_size;
     int zero_trail_bytes = 0;
     int pad_packet_bytes = 0;
     int general_pack = 0;  /*"general" pack without data specific to one stream?*/
     int pes_size;
     uint8_t* q = stream->payload;
+    pes_stream->format = PES_FMT_TS|PES_FMT_MPEG2;
 
     id = stream->id;
     packet_size = s->packet_size;
 
     if (packet_size > 0) {
-        /* packet header size */
-        packet_size -= 6;
-
-        /* packet header */
-        header_len = 3;
-        header_len += 1; /* obligatory stuffing byte */
-        if (pts != AV_NOPTS_VALUE) {
-            if (dts != pts)
-                header_len += 5 + 5;
-            else
-                header_len += 5;
-        }
-        payload_size = packet_size - header_len;
-
-        stuffing_size = payload_size - av_fifo_size(&pes_stream->fifo);
-
-        // first byte does not fit -> reset pts/dts + stuffing
-        if(payload_size <= trailer_size && pts != AV_NOPTS_VALUE){
-            int timestamp_len=0;
-            if(dts != pts)
-                timestamp_len += 5;
-            if(pts != AV_NOPTS_VALUE)
-                timestamp_len += 5;
-            pts=dts= AV_NOPTS_VALUE;
-            header_len -= timestamp_len;
-            payload_size += timestamp_len;
-            stuffing_size += timestamp_len;
-            if(payload_size > trailer_size)
-                stuffing_size += payload_size - trailer_size;
-        }
-
-        if (stuffing_size < 0)
-            stuffing_size = 0;
-        if (stuffing_size > 16) {    /*<=16 for MPEG-1, <=32 for MPEG-2*/
-            pad_packet_bytes += stuffing_size;
-            packet_size -= stuffing_size;
-            payload_size -= stuffing_size;
-            stuffing_size = 0;
-        }
+        ff_pes_cal_header(id,pes_stream,
+          &packet_size,&header_len,&pts,&dts,
+          &payload_size,&startcode,&stuffing_size,
+          &trailer_size,&pad_packet_bytes);
         pes_size = ff_pes_muxer_write(ctx, stream_index, stream->payload, pts, dts, id, stream->startcode, NULL, 0,
                  header_len, packet_size, payload_size, stuffing_size);
         if(pes_size < 0)
