diff -ur FMT_modify/mpegenc.c ff_pes_write_packet/mpegenc.c
--- FMT_modify/mpegenc.c	2008-04-07 15:14:44.000000000 +0800
+++ ff_pes_write_packet/mpegenc.c	2008-04-07 16:04:47.000000000 +0800
@@ -1011,46 +1011,8 @@
 static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)
 {
     MpegMuxContext *s = ctx->priv_data;
-    int stream_index= pkt->stream_index;
-    int size= pkt->size;
-    uint8_t *buf= pkt->data;
-    AVStream *st = ctx->streams[stream_index];
-    PESStream *stream = st->priv_data;
-    int64_t pts, dts;
-    PacketDesc *pkt_desc;
-    const int preload= av_rescale(ctx->preload, 90000, AV_TIME_BASE);
-    const int is_iframe = st->codec->codec_type == CODEC_TYPE_VIDEO && (pkt->flags & PKT_FLAG_KEY);
-
-    pts= pkt->pts;
-    dts= pkt->dts;
-
-    if(pts != AV_NOPTS_VALUE) pts += preload;
-    if(dts != AV_NOPTS_VALUE) dts += preload;
-
-//av_log(ctx, AV_LOG_DEBUG, "dts:%f pts:%f flags:%d stream:%d nopts:%d\n", dts/90000.0, pts/90000.0, pkt->flags, pkt->stream_index, pts != AV_NOPTS_VALUE);
-    if (!stream->premux_packet)
-        stream->next_packet = &stream->premux_packet;
-    *stream->next_packet=
-    pkt_desc= av_mallocz(sizeof(PacketDesc));
-    pkt_desc->pts= pts;
-    pkt_desc->dts= dts;
-    pkt_desc->unwritten_size=
-    pkt_desc->size= size;
-    if(!stream->predecode_packet)
-        stream->predecode_packet= pkt_desc;
-    stream->next_packet= &pkt_desc->next;
-
-    av_fifo_realloc(&stream->fifo, av_fifo_size(&stream->fifo) + size + 1);
-
-    if (s->is_dvd){
-        if (is_iframe && (s->packet_number == 0 || (pts - stream->vobu_start_pts >= 36000))) { // min VOBU length 0.4 seconds (mpucoder)
-            stream->bytes_to_iframe = av_fifo_size(&stream->fifo);
-            stream->align_iframe = 1;
-            stream->vobu_start_pts = pts;
-        }
-    }
-
-    av_fifo_write(&stream->fifo, buf, size);
+    int packet_number = s->packet_number;
+    ff_pes_write_packet(ctx,pkt,packet_number);
 
     for(;;){
         int ret= output_packet(ctx, 0);
diff -ur FMT_modify/mpegpesenc.c ff_pes_write_packet/mpegpesenc.c
--- FMT_modify/mpegpesenc.c	2008-04-07 15:14:18.000000000 +0800
+++ ff_pes_write_packet/mpegpesenc.c	2008-04-07 16:05:10.000000000 +0800
@@ -395,9 +395,10 @@
  * Write packet into PES FIFO.
  * @param [in] ctx  the AVFormatContext which contains streams.
  * @param [in] pkt  the packet to write.
+ * @param [in] packet_number the variable in PS struct
  * @return  NULL
  */
-void ff_pes_write_packet(AVFormatContext *ctx, AVPacket *pkt)
+void ff_pes_write_packet(AVFormatContext *ctx, AVPacket *pkt, int packet_number)
 {
     int stream_index= pkt->stream_index;
     int size= pkt->size;
@@ -407,6 +408,7 @@
     int64_t pts, dts;
     PacketDesc *pkt_desc;
     const int preload= av_rescale(ctx->preload, 90000, AV_TIME_BASE);
+    const int is_iframe = st->codec->codec_type == CODEC_TYPE_VIDEO && (pkt->flags & PKT_FLAG_KEY);
 
     pts= pkt->pts;
     dts= pkt->dts;
@@ -428,6 +430,13 @@
     stream->next_packet= &pkt_desc->next;
 
     av_fifo_realloc(&stream->fifo, av_fifo_size(&stream->fifo) + size + 1);
+    if (stream->format == PES_FMT_DVD){
+        if (is_iframe && (packet_number == 0 || (pts - stream->vobu_start_pts >= 36000))) { // min VOBU length 0.4 seconds (mpucoder)
+            stream->bytes_to_iframe = av_fifo_size(&stream->fifo);
+            stream->align_iframe = 1;
+            stream->vobu_start_pts = pts;
+        }
+    }
     av_fifo_write(&stream->fifo, buf, size);
 }
 
diff -ur FMT_modify/mpegpes.h ff_pes_write_packet/mpegpes.h
--- FMT_modify/mpegpes.h	2008-04-07 15:15:07.000000000 +0800
+++ ff_pes_write_packet/mpegpes.h	2008-04-07 16:05:27.000000000 +0800
@@ -101,9 +101,10 @@
  * Write packet into PES FIFO.
  * @param [in] ctx  the AVFormatContext which contains streams.
  * @param [in] pkt  the packet to write.
+ * @param [in] packet_number the variable in PS struct
  * @return  NULL
  */
-void ff_pes_write_packet(AVFormatContext *ctx, AVPacket *pkt);
+void ff_pes_write_packet(AVFormatContext *ctx, AVPacket *pkt, int packet_number);
 
 /**
  * Find the stream to mux into the PES stream.
diff -ur FMT_modify/mpegtsenc.c ff_pes_write_packet/mpegtsenc.c
--- FMT_modify/mpegtsenc.c	2008-04-07 15:14:33.000000000 +0800
+++ ff_pes_write_packet/mpegtsenc.c	2008-04-07 16:04:42.000000000 +0800
@@ -723,7 +723,8 @@
 
 static int mpegts_write_packet(AVFormatContext *ctx, AVPacket *pkt)
 {
-    ff_pes_write_packet(ctx, pkt);
+    int packet_number = 0;/* just a stuff param for function call */
+    ff_pes_write_packet(ctx, pkt,packet_number);
     for(;;){
         int ret = output_packet(ctx, 0);
         if(ret<=0)
