[FFmpeg-soc] [soc]: r553 - in dvbmuxer/muxer_dev: Makefile dvbmuxer.diff mpeg_pes.h mpeg_pes_enc.c mpegtsenc.c

realsun subversion at mplayerhq.hu
Mon Jul 30 14:50:30 CEST 2007


Author: realsun
Date: Mon Jul 30 14:50:30 2007
New Revision: 553

Log:
modified pcr calculation.
generated ts stream still could not played well under vlc.

Modified:
   dvbmuxer/muxer_dev/Makefile
   dvbmuxer/muxer_dev/dvbmuxer.diff
   dvbmuxer/muxer_dev/mpeg_pes.h
   dvbmuxer/muxer_dev/mpeg_pes_enc.c
   dvbmuxer/muxer_dev/mpegtsenc.c

Modified: dvbmuxer/muxer_dev/Makefile
==============================================================================
--- dvbmuxer/muxer_dev/Makefile	(original)
+++ dvbmuxer/muxer_dev/Makefile	Mon Jul 30 14:50:30 2007
@@ -6,7 +6,7 @@ include ../config.mak
 
 CFLAGS+=-I$(SRC_PATH)/libavcodec
 
-OBJS= utils.o cutils.o os_support.o allformats.o
+OBJS= utils.o cutils.o os_support.o allformats.o sdp.o
 
 HEADERS = avformat.h avio.h rtp.h rtsp.h rtspcodes.h
 
@@ -15,7 +15,7 @@ OBJS-$(CONFIG_AAC_DEMUXER)              
 OBJS-$(CONFIG_AC3_DEMUXER)               += raw.o
 OBJS-$(CONFIG_AC3_MUXER)                 += raw.o
 OBJS-$(CONFIG_ADTS_MUXER)                += adtsenc.o
-OBJS-$(CONFIG_AIFF_DEMUXER)              += aiff.o riff.o
+OBJS-$(CONFIG_AIFF_DEMUXER)              += aiff.o riff.o raw.o
 OBJS-$(CONFIG_AIFF_MUXER)                += aiff.o riff.o
 OBJS-$(CONFIG_AMR_DEMUXER)               += amr.o
 OBJS-$(CONFIG_AMR_MUXER)                 += amr.o
@@ -23,16 +23,16 @@ OBJS-$(CONFIG_APC_DEMUXER)              
 OBJS-$(CONFIG_ASF_DEMUXER)               += asf.o riff.o
 OBJS-$(CONFIG_ASF_MUXER)                 += asf-enc.o riff.o
 OBJS-$(CONFIG_ASF_STREAM_MUXER)          += asf-enc.o riff.o
-OBJS-$(CONFIG_AU_DEMUXER)                += au.o riff.o
-OBJS-$(CONFIG_AU_MUXER)                  += au.o riff.o
+OBJS-$(CONFIG_AU_DEMUXER)                += au.o raw.o
+OBJS-$(CONFIG_AU_MUXER)                  += au.o
 OBJS-$(CONFIG_AUDIO_DEMUXER)             += audio.o
 OBJS-$(CONFIG_AUDIO_MUXER)               += audio.o
 OBJS-$(CONFIG_AVI_DEMUXER)               += avidec.o riff.o
 OBJS-$(CONFIG_AVI_MUXER)                 += avienc.o riff.o
 OBJS-$(CONFIG_AVISYNTH)                  += avisynth.o
-OBJS-$(CONFIG_AVS_DEMUXER)               += avs.o vocdec.o voc.o riff.o
+OBJS-$(CONFIG_AVS_DEMUXER)               += avs.o vocdec.o voc.o
 OBJS-$(CONFIG_BETHSOFTVID_DEMUXER)       += bethsoftvid.o
-OBJS-$(CONFIG_C93_DEMUXER)               += c93.o
+OBJS-$(CONFIG_C93_DEMUXER)               += c93.o vocdec.o voc.o
 OBJS-$(CONFIG_CRC_MUXER)                 += crc.o
 OBJS-$(CONFIG_DAUD_DEMUXER)              += daud.o
 OBJS-$(CONFIG_DC1394_DEMUXER)            += dc1394.o
@@ -40,8 +40,8 @@ OBJS-$(CONFIG_DSICIN_DEMUXER)           
 OBJS-$(CONFIG_DTS_DEMUXER)               += raw.o
 OBJS-$(CONFIG_DV_DEMUXER)                += dv.o
 OBJS-$(CONFIG_DV_MUXER)                  += dvenc.o
-OBJS-$(CONFIG_DV1394_DEMUXER)            += dv1394.o
-OBJS-$(CONFIG_DXA_DEMUXER)               += dxa.o
+OBJS-$(CONFIG_DV1394_DEMUXER)            += dv1394.o dv.o
+OBJS-$(CONFIG_DXA_DEMUXER)               += dxa.o riff.o
 OBJS-$(CONFIG_EA_DEMUXER)                += electronicarts.o
 OBJS-$(CONFIG_FFM_DEMUXER)               += ffm.o
 OBJS-$(CONFIG_FFM_MUXER)                 += ffm.o
@@ -75,7 +75,7 @@ OBJS-$(CONFIG_MATROSKA_DEMUXER)         
 OBJS-$(CONFIG_MJPEG_DEMUXER)             += raw.o
 OBJS-$(CONFIG_MJPEG_MUXER)               += raw.o
 OBJS-$(CONFIG_MM_DEMUXER)                += mm.o
-OBJS-$(CONFIG_MMF_DEMUXER)               += mmf.o riff.o
+OBJS-$(CONFIG_MMF_DEMUXER)               += mmf.o raw.o
 OBJS-$(CONFIG_MMF_MUXER)                 += mmf.o riff.o
 OBJS-$(CONFIG_MOV_DEMUXER)               += mov.o riff.o isom.o
 OBJS-$(CONFIG_MOV_MUXER)                 += movenc.o riff.o isom.o
@@ -93,12 +93,13 @@ OBJS-$(CONFIG_MPEG2VIDEO_MUXER)         
 OBJS-$(CONFIG_MPEG2VOB_MUXER)            += mpegenc.o mpeg_pes_enc.o
 OBJS-$(CONFIG_MPEGPS_DEMUXER)            += mpeg.o
 OBJS-$(CONFIG_MPEGTS_DEMUXER)            += mpegts.o
+OBJS-$(CONFIG_MPEGTSRAW_DEMUXER)         += mpegts.o
 OBJS-$(CONFIG_MPEGTS_MUXER)              += mpegtsenc.o
 OBJS-$(CONFIG_MPEGVIDEO_DEMUXER)         += raw.o
 OBJS-$(CONFIG_MPJPEG_MUXER)              += mpjpeg.o
 OBJS-$(CONFIG_MTV_DEMUXER)               += mtv.o
 OBJS-$(CONFIG_MXF_DEMUXER)               += mxf.o
-OBJS-$(CONFIG_NSV_DEMUXER)               += nsvdec.o riff.o
+OBJS-$(CONFIG_NSV_DEMUXER)               += nsvdec.o
 OBJS-$(CONFIG_NULL_MUXER)                += raw.o
 OBJS-$(CONFIG_NUT_DEMUXER)               += nutdec.o riff.o
 OBJS-$(CONFIG_NUV_DEMUXER)               += nuv.o riff.o
@@ -123,7 +124,7 @@ OBJS-$(CONFIG_SDP_DEMUXER)              
 OBJS-$(CONFIG_SEGAFILM_DEMUXER)          += segafilm.o
 OBJS-$(CONFIG_SHORTEN_DEMUXER)           += raw.o
 OBJS-$(CONFIG_SMACKER_DEMUXER)           += smacker.o
-OBJS-$(CONFIG_SOL_DEMUXER)               += sol.o
+OBJS-$(CONFIG_SOL_DEMUXER)               += sol.o raw.o
 OBJS-$(CONFIG_STR_DEMUXER)               += psxstr.o
 OBJS-$(CONFIG_SWF_DEMUXER)               += swf.o
 OBJS-$(CONFIG_SWF_MUXER)                 += swf.o
@@ -134,12 +135,13 @@ OBJS-$(CONFIG_TIERTEXSEQ_DEMUXER)       
 OBJS-$(CONFIG_TTA_DEMUXER)               += tta.o
 OBJS-$(CONFIG_TXD_DEMUXER)               += txd.o
 OBJS-$(CONFIG_V4L2_DEMUXER)              += v4l2.o
+OBJS-$(CONFIG_VC1_DEMUXER)               += raw.o
 OBJS-$(CONFIG_VIDEO_GRAB_BKTR_DEMUXER)   += grab_bktr.o
 OBJS-$(CONFIG_VIDEO_GRAB_V4L_DEMUXER)    += grab.o
 OBJS-$(CONFIG_VMD_DEMUXER)               += sierravmd.o
-OBJS-$(CONFIG_VOC_DEMUXER)               += vocdec.o voc.o riff.o
-OBJS-$(CONFIG_VOC_MUXER)                 += vocenc.o voc.o riff.o
-OBJS-$(CONFIG_WAV_DEMUXER)               += wav.o riff.o
+OBJS-$(CONFIG_VOC_DEMUXER)               += vocdec.o voc.o
+OBJS-$(CONFIG_VOC_MUXER)                 += vocenc.o voc.o
+OBJS-$(CONFIG_WAV_DEMUXER)               += wav.o riff.o raw.o
 OBJS-$(CONFIG_WAV_MUXER)                 += wav.o riff.o
 OBJS-$(CONFIG_WC3_DEMUXER)               += wc3movie.o
 OBJS-$(CONFIG_WSAUD_DEMUXER)             += westwood.o
@@ -153,7 +155,7 @@ OBJS-$(CONFIG_YUV4MPEGPIPE_DEMUXER)     
 OBJS-$(CONFIG_LIBNUT_DEMUXER)            += libnut.o riff.o
 OBJS-$(CONFIG_LIBNUT_MUXER)              += libnut.o riff.o
 
-OBJS+= framehook.o
+OBJS-$(CONFIG_VHOOK)                     += framehook.o
 
 EXTRALIBS := -L$(BUILD_ROOT)/libavutil -lavutil$(BUILDSUF) \
              -lavcodec$(BUILDSUF) -L$(BUILD_ROOT)/libavcodec $(EXTRALIBS)

Modified: dvbmuxer/muxer_dev/dvbmuxer.diff
==============================================================================
--- dvbmuxer/muxer_dev/dvbmuxer.diff	(original)
+++ dvbmuxer/muxer_dev/dvbmuxer.diff	Mon Jul 30 14:50:30 2007
@@ -631,7 +631,7 @@ Index: mpeg_pes_enc.c
      int nb_frames=0;
      PacketDesc *pkt_desc= stream->premux_packet;
  
-@@ -629,365 +99,13 @@
+@@ -629,402 +99,108 @@
      return nb_frames;
  }
  
@@ -651,9 +651,14 @@ Index: mpeg_pes_enc.c
 -    int pes_flags;
 -    int general_pack = 0;  /*"general" pack without data specific to one stream?*/
 -    int nb_frames;
--
++    int i;
+ 
 -    id = stream->id;
--
++    for(i=0; i<ctx->nb_streams; i++){
++        AVStream *st = ctx->streams[i];
++        PESStream *stream = st->priv_data;
++        PacketDesc *pkt_desc;
+ 
 -#if 0
 -    printf("packet ID=%2x PTS=%0.3f\n",
 -           id, pts / 90000.0);
@@ -675,7 +680,15 @@ Index: mpeg_pes_enc.c
 -            if (stream->packet_number==0) {
 -                size = put_system_header(ctx, buf_ptr, id);
 -                buf_ptr += size;
--            }
++        while((pkt_desc= stream->predecode_packet)
++              && scr > pkt_desc->dts){ //FIXME > vs >=
++            if(stream->buffer_index < pkt_desc->size ||
++               stream->predecode_packet == stream->premux_packet){
++                   av_log(ctx, AV_LOG_ERROR,
++                       "buffer underflow i=%d bufi=%d size=%d\n",
++                       i, stream->buffer_index, pkt_desc->size);
++                break;
+             }
 -        } else if (s->is_dvd) {
 -            if (stream->align_iframe || s->packet_number == 0){
 -                int PES_bytes_to_fill = s->packet_size - size - 10;
@@ -723,8 +736,11 @@ Index: mpeg_pes_enc.c
 -                size = put_system_header(ctx, buf_ptr, 0);
 -                buf_ptr += size;
 -            }
--        }
--    }
++            stream->buffer_index -= pkt_desc->size;
++            stream->predecode_packet= pkt_desc->next;
++            av_freep(&pkt_desc);
+         }
+     }
 -    size = buf_ptr - buffer;
 -    put_buffer(&ctx->pb, buffer, size);
 -
@@ -939,39 +955,66 @@ Index: mpeg_pes_enc.c
 -        stream->packet_number++;
 -
 -    return payload_size - stuffing_size;
--}
--
++    return 0;
+ }
+ 
 -static void put_vcd_padding_sector(AVFormatContext *ctx)
--{
++int ff_pes_muxer_write(AVFormatContext *ctx, int stream_index, uint8_t *pes_buffer,
++    int64_t pts,int64_t dts, int  id, int startcode,
++    uint8_t* pes_content, int pes_content_len,
++    int header_len, int packet_size, int payload_size, int stuffing_size)
+ {
 -    /* There are two ways to do this padding: writing a sector/pack
 -       of 0 values, or writing an MPEG padding pack. Both seem to
 -       work with most decoders, BUT the VCD standard only allows a 0-sector
 -       (see standard p. IV-4, IV-5).
 -       So a 0-sector it is...*/
--
++    PESStream *stream = ctx->streams[stream_index]->priv_data;
++    PESContext *context = ctx->priv_data;
++    int pes_flags, i;
++    int data_size = payload_size - stuffing_size;
++    uint8_t *q = pes_buffer;
+ 
 -    MpegMuxContext *s = ctx->priv_data;
-     int i;
+-    int i;
++    bytestream_put_be32(&q, startcode);
  
 -    for(i=0;i<s->packet_size;i++)
 -        put_byte(&ctx->pb, 0);
--
++    bytestream_put_be16(&q, packet_size);
++    bytestream_put_byte(&q, 0x80); /* mpeg2 id */
+ 
 -    s->vcd_padding_bytes_written += s->packet_size;
--
++    pes_flags=0;
+ 
 -    put_flush_packet(&ctx->pb);
--
++    if (pts != AV_NOPTS_VALUE) {
++        pes_flags |= 0x80;
++        if (dts != pts)
++            pes_flags |= 0x40;
++    }
+ 
 -    /* increasing the packet number is correct. The SCR of the following packs
 -       is calculated from the packet_number and it has to include the padding
 -       sector (it represents the sector index, not the MPEG pack index)
 -       (see VCD standard p. IV-6)*/
 -    s->packet_number++;
 -}
--
++    /* Both the MPEG-2 and the SVCD standards demand that the
++       P-STD_buffer_size field be included in the first packet of
++       every stream. (see SVCD standard p. 26 V.2.3.1 and V.2.3.2
++       and MPEG-2 standard 2.7.7) */
++    if (context->packet_number == 0 && context->muxer_type == PESMUXER_PS)
++        pes_flags |= 0x01;
+ 
 -#if 0 /* unused, remove? */
 -static int64_t get_vcd_scr(AVFormatContext *ctx,int stream_index,int64_t pts)
 -{
 -    MpegMuxContext *s = ctx->priv_data;
 -    int64_t scr;
--
++    bytestream_put_byte(&q, pes_flags);
++    bytestream_put_byte(&q, header_len - 3 + stuffing_size);
+ 
 -        /* Since the data delivery rate is constant, SCR is computed
 -           using the formula C + i * 1200 where C is the start constant
 -           and i is the pack index.
@@ -983,111 +1026,73 @@ Index: mpeg_pes_enc.c
 -           will still be correct according to the standard. It just won't have
 -           the "recommended" value).*/
 -        scr = 36000 + s->packet_number * 1200;
--
--    return scr;
--}
--#endif
--
--static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr){
--//    MpegMuxContext *s = ctx->priv_data;
--    int i;
--
-     for(i=0; i<ctx->nb_streams; i++){
-         AVStream *st = ctx->streams[i];
--        StreamInfo *stream = st->priv_data;
-+        PESStream *stream = st->priv_data;
-         PacketDesc *pkt_desc;
- 
-         while((pkt_desc= stream->predecode_packet)
-@@ -1000,31 +118,89 @@
-                 break;
-             }
-             stream->buffer_index -= pkt_desc->size;
--
-             stream->predecode_packet= pkt_desc->next;
-             av_freep(&pkt_desc);
-         }
-     }
--
-     return 0;
- }
- 
--static int output_packet(AVFormatContext *ctx, int flush){
--    MpegMuxContext *s = ctx->priv_data;
--    AVStream *st;
--    StreamInfo *stream;
--    int i, avail_space, es_size, trailer_size;
--    int best_i= -1;
--    int best_score= INT_MIN;
--    int ignore_constraints=0;
--    int64_t scr= s->last_scr;
--    PacketDesc *timestamp_packet;
-+int ff_pes_muxer_write(AVFormatContext *ctx, int stream_index, uint8_t *pes_buffer,
-+    int64_t pts,int64_t dts, int  id, int startcode,
-+    uint8_t* pes_content, int pes_content_len,
-+    int header_len, int packet_size, int payload_size, int stuffing_size)
-+{
-+    PESStream *stream = ctx->streams[stream_index]->priv_data;
-+    PESContext *context = ctx->priv_data;
-+    int pes_flags, i;
-+    int data_size = payload_size - stuffing_size;
-+    uint8_t *q = pes_buffer;
-+
-+    bytestream_put_be32(&q, startcode);
-+
-+    bytestream_put_be16(&q, packet_size);
-+    bytestream_put_byte(&q, 0x80); /* mpeg2 id */
-+
-+    pes_flags=0;
-+
-+    if (pts != AV_NOPTS_VALUE) {
-+        pes_flags |= 0x80;
-+        if (dts != pts)
-+            pes_flags |= 0x40;
-+    }
-+
-+    /* Both the MPEG-2 and the SVCD standards demand that the
-+       P-STD_buffer_size field be included in the first packet of
-+       every stream. (see SVCD standard p. 26 V.2.3.1 and V.2.3.2
-+       and MPEG-2 standard 2.7.7) */
-+    if (context->packet_number == 0 && context->muxer_type == PESMUXER_PS)
-+        pes_flags |= 0x01;
-+
-+    bytestream_put_byte(&q, pes_flags);
-+    bytestream_put_byte(&q, header_len - 3 + stuffing_size);
-+
 +    if (pes_flags & 0x80)  /*write pts*/
 +        put_timestamp_bytestream(&q, (pes_flags & 0x40) ? 0x03 : 0x02, pts);
 +    if (pes_flags & 0x40)  /*write dts*/
 +        put_timestamp_bytestream(&q, 0x01, dts);
-+
+ 
+-    return scr;
+-}
+-#endif
 +    if (pes_flags & 0x01) {  /*write pes extension*/
 +        bytestream_put_byte(&q, 0x10);
-+
+ 
+-static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr){
+-//    MpegMuxContext *s = ctx->priv_data;
+-    int i;
 +        /* P-STD buffer info */
 +        if (id == AUDIO_ID)
 +            bytestream_put_be16(&q, 0x4000 | stream ->max_buffer_size/128);
 +        else
 +            bytestream_put_be16(&q, 0x6000 | stream ->max_buffer_size/1024);
 +    }
-+
+ 
+-    for(i=0; i<ctx->nb_streams; i++){
+-        AVStream *st = ctx->streams[i];
+-        StreamInfo *stream = st->priv_data;
+-        PacketDesc *pkt_desc;
 +    /* special stuffing byte that is always written
 +       to prevent accidental generation of startcodes. */
 +    bytestream_put_byte(&q, 0xff);
-+
+ 
+-        while((pkt_desc= stream->predecode_packet)
+-              && scr > pkt_desc->dts){ //FIXME > vs >=
+-            if(stream->buffer_index < pkt_desc->size ||
+-               stream->predecode_packet == stream->premux_packet){
+-                av_log(ctx, AV_LOG_ERROR,
+-                       "buffer underflow i=%d bufi=%d size=%d\n",
+-                       i, stream->buffer_index, pkt_desc->size);
+-                break;
+-            }
+-            stream->buffer_index -= pkt_desc->size;
 +    for (i=0;i<stuffing_size;i++)
 +        bytestream_put_byte(&q, 0xff);
-+
+ 
+-            stream->predecode_packet= pkt_desc->next;
+-            av_freep(&pkt_desc);
+-        }
+-    }
 +    if(pes_content != NULL)
 +        bytestream_put_buffer(&q, pes_content, pes_content_len);
-+
+ 
+-    return 0;
 +   /* output data */
 +    if(av_fifo_read(&stream->fifo, q, data_size) < 0)
 +        return -1;
 +    return (q - pes_buffer + data_size);
-+}
-+
-+int ff_pes_find_beststream(AVFormatContext *ctx, int packet_size, int flush, int64_t scr, int* best_i)
+ }
+ 
+-static int output_packet(AVFormatContext *ctx, int flush){
+-    MpegMuxContext *s = ctx->priv_data;
+-    AVStream *st;
+-    StreamInfo *stream;
+-    int i, avail_space, es_size, trailer_size;
+-    int best_i= -1;
+-    int best_score= INT_MIN;
+-    int ignore_constraints=0;
+-    int64_t scr= s->last_scr;
+-    PacketDesc *timestamp_packet;
++int ff_pes_find_beststream(AVFormatContext *ctx, int packet_size, int flush, int64_t *scr, int* best_i)
 +{
 +    int best_score = INT_MIN;
 +    int i, avail_space = 0;
@@ -1102,7 +1107,7 @@ Index: mpeg_pes_enc.c
          const int avail_data=  av_fifo_size(&stream->fifo);
          const int space= stream->max_buffer_size - stream->buffer_index;
          int rel_space= 1024*space / stream->max_buffer_size;
-@@ -1032,14 +208,14 @@
+@@ -1032,118 +208,69 @@
  
          /* for subtitle, a single PES packet must be generated,
             so we flush after every single subtitle packet */
@@ -1118,8 +1123,9 @@ Index: mpeg_pes_enc.c
 +        if(space < packet_size && !ignore_constraints)
              continue;
  
-         if(next_pkt && next_pkt->dts - scr > max_delay)
-@@ -1047,26 +223,27 @@
+-        if(next_pkt && next_pkt->dts - scr > max_delay)
++        if(next_pkt && next_pkt->dts - *scr > max_delay)
+             continue;
  
          if(rel_space > best_score){
              best_score= rel_space;
@@ -1151,12 +1157,15 @@ Index: mpeg_pes_enc.c
          if(best_dts == INT64_MAX)
              return 0;
  
-@@ -1075,75 +252,25 @@
+-        if(scr >= best_dts+1 && !ignore_constraints){
++        if(*scr >= best_dts+1 && !ignore_constraints){
+             av_log(ctx, AV_LOG_ERROR, "packet too large, ignoring buffer limits to mux it\n");
              ignore_constraints= 1;
          }
-         scr= FFMAX(best_dts+1, scr);
+-        scr= FFMAX(best_dts+1, scr);
 -        if(remove_decoded_packets(ctx, scr) < 0)
-+        if(ff_pes_remove_decoded_packets(ctx, scr) < 0)
++        *scr= FFMAX(best_dts+1, *scr);
++        if(ff_pes_remove_decoded_packets(ctx, *scr) < 0)
              return -1;
          goto retry;
      }
@@ -1234,7 +1243,7 @@ Index: mpeg_pes_enc.c
  
      pts= pkt->pts;
      dts= pkt->dts;
-@@ -1151,13 +278,12 @@
+@@ -1151,7 +278,6 @@
      if(pts != AV_NOPTS_VALUE) pts += preload;
      if(dts != AV_NOPTS_VALUE) dts += preload;
  
@@ -1242,14 +1251,6 @@ Index: mpeg_pes_enc.c
      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->pts= pkt->pts;
-+    pkt_desc->dts= pkt->dts;
-     pkt_desc->unwritten_size=
-     pkt_desc->size= size;
-     if(!stream->predecode_packet)
 @@ -1165,126 +291,18 @@
      stream->next_packet= &pkt_desc->next;
  
@@ -1382,7 +1383,7 @@ Index: mpeg_pes_enc.c
 -#endif
 Index: Makefile
 ===================================================================
---- Makefile	(revision 9635)
+--- Makefile	(revision 9832)
 +++ Makefile	(working copy)
 @@ -84,13 +84,13 @@
  OBJS-$(CONFIG_MP3_MUXER)                 += mp3.o
@@ -1402,10 +1403,22 @@ Index: Makefile
 +OBJS-$(CONFIG_MPEG2VOB_MUXER)            += mpegenc.o mpeg_pes_enc.o
  OBJS-$(CONFIG_MPEGPS_DEMUXER)            += mpeg.o
  OBJS-$(CONFIG_MPEGTS_DEMUXER)            += mpegts.o
- OBJS-$(CONFIG_MPEGTS_MUXER)              += mpegtsenc.o
+ OBJS-$(CONFIG_MPEGTSRAW_DEMUXER)         += mpegts.o
+Index: mpegenc.c
+===================================================================
+--- mpegenc.c	(revision 9832)
++++ mpegenc.c	(working copy)
+@@ -1153,6 +1153,7 @@
+     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;
 Index: mpegtsenc.c
 ===================================================================
---- mpegtsenc.c	(revision 9635)
+--- mpegtsenc.c	(revision 9832)
 +++ mpegtsenc.c	(working copy)
 @@ -21,6 +21,8 @@
  #include "avformat.h"
@@ -1416,7 +1429,18 @@ Index: mpegtsenc.c
  
  /* write DVB SI sections */
  
-@@ -137,7 +139,24 @@
+@@ -60,8 +62,9 @@
+             b |= 0x40;
+         *q++ = b;
+         *q++ = s->pid;
+-        s->cc = (s->cc + 1) & 0xf;
++        s->cc = (s->cc) & 0xf;
+         *q++ = 0x10 | s->cc;
++        s->cc++;
+         if (first)
+             *q++ = 0; /* 0 offset */
+         len1 = TS_PACKET_SIZE - (q - packet);
+@@ -137,7 +140,24 @@
  #define PAT_RETRANS_TIME 100
  #define PCR_RETRANS_TIME 20
  
@@ -1441,7 +1465,7 @@ Index: mpegtsenc.c
      struct MpegTSService *service;
      int pid; /* stream associated pid */
      int cc;
-@@ -158,6 +177,7 @@
+@@ -158,6 +178,7 @@
  } MpegTSService;
  
  typedef struct MpegTSWrite {
@@ -1449,7 +1473,7 @@ Index: mpegtsenc.c
      MpegTSSection pat; /* MPEG2 pat table */
      MpegTSSection sdt; /* MPEG2 sdt table context */
      MpegTSService **services;
-@@ -168,6 +188,10 @@
+@@ -168,6 +189,10 @@
      int nb_services;
      int onid;
      int tsid;
@@ -1460,7 +1484,7 @@ Index: mpegtsenc.c
  } MpegTSWrite;
  
  static void mpegts_write_pat(AVFormatContext *s)
-@@ -362,7 +386,8 @@
+@@ -362,7 +387,8 @@
      MpegTSWriteStream *ts_st;
      MpegTSService *service;
      AVStream *st;
@@ -1470,7 +1494,7 @@ Index: mpegtsenc.c
      const char *service_name;
  
      ts->tsid = DEFAULT_TSID;
-@@ -376,6 +401,14 @@
+@@ -376,6 +402,14 @@
      service->pmt.write_packet = section_write_packet;
      service->pmt.opaque = s;
  
@@ -1485,7 +1509,7 @@ Index: mpegtsenc.c
      ts->pat.pid = PAT_PID;
      ts->pat.cc = 0;
      ts->pat.write_packet = section_write_packet;
-@@ -387,7 +420,6 @@
+@@ -387,7 +421,6 @@
      ts->sdt.opaque = s;
  
      /* assign pids to each stream */
@@ -1493,7 +1517,7 @@ Index: mpegtsenc.c
      for(i = 0;i < s->nb_streams; i++) {
          st = s->streams[i];
          ts_st = av_mallocz(sizeof(MpegTSWriteStream));
-@@ -402,7 +434,13 @@
+@@ -402,7 +435,13 @@
          if (st->codec->codec_type == CODEC_TYPE_VIDEO &&
              service->pcr_pid == 0x1fff)
              service->pcr_pid = ts_st->pid;
@@ -1508,7 +1532,7 @@ Index: mpegtsenc.c
      }
  
      /* if no video stream, use the first stream as PCR */
-@@ -411,13 +449,39 @@
+@@ -411,13 +450,29 @@
          service->pcr_pid = ts_st->pid;
      }
  
@@ -1520,28 +1544,18 @@ Index: mpegtsenc.c
 +
 +    bitrate = 0;
 +    for(i=0;i<s->nb_streams;i++) {
-+        int codec_rate;
 +        st = s->streams[i];
 +        ts_st = (MpegTSWriteStream*) st->priv_data;
-+
-+        if(st->codec->rc_max_rate || ts_st->id==VIDEO_ID)
-+            codec_rate= st->codec->rc_max_rate;
-+        else
-+            codec_rate= st->codec->bit_rate;
-+        bitrate += codec_rate;
++        bitrate += st->codec->bit_rate;
 +    }
 +
-+    if(s->mux_rate){
-+        ts->mux_rate= (s->mux_rate + (8 * 50) - 1) / (8 * 50);
-+    } else {
-+        bitrate += bitrate*5/100;
-+        bitrate = bitrate * 25 / (8 *  DEFAULT_PES_PAYLOAD_SIZE) +  /* PES header size */
-+                   bitrate * 4 / (8 * TS_PACKET_SIZE) +             /* TS  header size */
-+                   500 * 12 +                                       /* SDT size */
-+                   100 * 16;                                        /* PAT size */
-+        ts->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
-+    }
-+    ts->last_pcr = 0;
++    //bitrate += bitrate*5/100;
++    bitrate += bitrate * 25 / (8 *  DEFAULT_PES_PAYLOAD_SIZE) +  /* PES header size */
++               bitrate * 4 / (8 * TS_PACKET_SIZE) +             /* TS  header size */
++               500 * 12 +                                       /* SDT size */
++               100 * 16;                                        /* PAT size */
++    ts->mux_rate = bitrate;
++    ts->last_pcr = 10000;  /* add a preload value */
 +
 +    service->pcr_packet_freq = (ts->mux_rate * PCR_RETRANS_TIME) /
          (TS_PACKET_SIZE * 8 * 1000);
@@ -1553,7 +1567,7 @@ Index: mpegtsenc.c
          (TS_PACKET_SIZE * 8 * 1000);
  #if 0
      printf("%d %d %d\n",
-@@ -462,31 +526,17 @@
+@@ -462,55 +517,41 @@
      }
  }
  
@@ -1585,22 +1599,31 @@ Index: mpegtsenc.c
 +    int val, is_start, len, header_len, write_pcr;
      int afc_len, stuffing_len;
      int64_t pcr = -1; /* avoid warning */
-+    int offset = 0;
- 
+-
++    int offset = 0, packet_count = 0;
++    static int p = 0;
      is_start = 1;
      while (payload_size > 0) {
-@@ -499,9 +549,7 @@
-                 ts_st->service->pcr_packet_freq) {
+         retransmit_si_info(s);
+-
++        p++;
+         write_pcr = 0;
+         if (ts_st->pid == ts_st->service->pcr_pid) {
++            packet_count ++;
+             ts_st->service->pcr_packet_count++;
+             if (ts_st->service->pcr_packet_count >=
+-                ts_st->service->pcr_packet_freq) {
++                ts_st->service->pcr_packet_freq && dts != AV_NOPTS_VALUE) {
                  ts_st->service->pcr_packet_count = 0;
                  write_pcr = 1;
 -                /* XXX: this is incorrect, but at least we have a PCR
 -                   value */
 -                pcr = pts;
-+                pcr = ts->last_pcr;
++                pcr = dts + packet_count * TS_PACKET_SIZE* 8*90000LL / ts->mux_rate;
              }
          }
- 
-@@ -509,8 +557,10 @@
+-
+         /* prepare packet header */
          q = buf;
          *q++ = 0x47;
          val = (ts_st->pid >> 8);
@@ -1612,7 +1635,7 @@ Index: mpegtsenc.c
          *q++ = val;
          *q++ = ts_st->pid;
          *q++ = 0x10 | ts_st->cc | (write_pcr ? 0x20 : 0);
-@@ -525,58 +575,6 @@
+@@ -525,58 +566,6 @@
              *q++ = (pcr & 1) << 7;
              *q++ = 0;
          }
@@ -1671,7 +1694,7 @@ Index: mpegtsenc.c
          /* header size */
          header_len = q - buf;
          /* data len */
-@@ -605,71 +603,219 @@
+@@ -605,71 +594,225 @@
                  }
              }
          }
@@ -1781,6 +1804,7 @@ Index: mpegtsenc.c
 +                stuffing_size += payload_size - trailer_size;
 +        }
 +
++        //av_log(ctx, AV_LOG_INFO, "Cur pts is %"PRId64", Cur dts is %"PRId64", payload_size %d, tailer_size %d\n", pts, dts, 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
@@ -1813,8 +1837,8 @@ Index: mpegtsenc.c
      }
 -    return 0;
 +
-+    if (pad_packet_bytes > 0)
-+        put_padding_packet(ctx,&ctx->pb, pad_packet_bytes);
++    //if (pad_packet_bytes > 0)
++     //   put_padding_packet(ctx,&ctx->pb, pad_packet_bytes);
 +
 +    for(i=0;i<zero_trail_bytes;i++)
 +        put_byte(&ctx->pb, 0x00);
@@ -1849,10 +1873,10 @@ Index: mpegtsenc.c
 +    int es_size, trailer_size;
 +    int result;
 +    int best_i= -1;
-+    int64_t pcr= s->last_pcr;
++    int pcr = s->last_pcr;
 +    PacketDesc *timestamp_packet;
 +
-+    if((result = ff_pes_find_beststream(ctx, s->packet_size, flush, pcr, &best_i)) <= 0)
++    if((result = ff_pes_find_beststream(ctx, s->packet_size, flush, &pcr, &best_i)) <= 0)
 +        return result;
 +
 +    assert(best_i >= 0);
@@ -1879,8 +1903,7 @@ Index: mpegtsenc.c
 +    }
 +
 +
-+    s->last_pcr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
-+
++    s->last_pcr += s->packet_size*8*90000LL / (s->mux_rate); //FIXME rounding and first few bytes of each packet
 +    if(ff_pes_remove_decoded_packets(ctx, s->last_pcr) < 0)
 +        return -1;
 +
@@ -1891,11 +1914,17 @@ Index: mpegtsenc.c
 +static int mpegts_write_packet(AVFormatContext *ctx, AVPacket *pkt)
 +{
 +    int stream_index= pkt->stream_index;
++    int size = pkt->size;
++    static int total_size = 0;
 +    AVStream *st = ctx->streams[stream_index];
 +    MpegTSWriteStream *stream = st->priv_data;
 +    PESStream *pes_stream = &stream->pes_stream;
 +    int64_t pts;
 +
++    /* add a preload value to avoid negative dts*/
++    pkt->pts += 10000;
++    pkt->dts += 10000;
++    total_size += size;
 +    ff_pes_write_packet(ctx, pkt);
 +    pts= pes_stream->predecode_packet->pts;
 +
@@ -1939,7 +1968,174 @@ Index: mpeg_pes.h
 ===================================================================
 --- mpeg_pes.h	(revision 0)
 +++ mpeg_pes.h	(revision 0)
-@@ -0,0 +1,167 @@
+@@ -0,0 +1,334 @@
++/*
++ * Copyright (c) 2000-2002 Fabrice Bellard
++ * Copyright (c) 2007 Xiaohui Sun <sunxiaohui at dsp.ac.cn>
++ *
++ * This file is part of FFmpeg.
++ *
++ * FFmpeg is free software; you can redistribute it and/or
++ * modify it under the terms of the GNU Lesser General Public
++ * License as published by the Free Software Foundation; either
++ * version 2.1 of the License, or (at your option) any later version.
++ *
++ * FFmpeg is distributed in the hope that it will be useful,
++ * but WITHOUT ANY WARRANTY; without even the implied warranty of
++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
++ * Lesser General Public License for more details.
++ *
++ * You should have received a copy of the GNU Lesser General Public
++ * License along with FFmpeg; if not, write to the Free Software
++ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
++ */
++
++/**
++ * @file mpeg_pes.h
++ * MPEG PES packetizer API header
++ */
++
++#ifndef AVFORMAT_MPEG_PES_H
++#define AVFORMAT_MPEG_PES_H
++
++#include "avformat.h"
++#include "fifo.h"
++
++/**
++ * PES packet description
++ */
++typedef struct PacketDesc {
++    int64_t pts;
++    int64_t dts;
++    int size;
++    int unwritten_size;
++    int flags;
++    struct PacketDesc *next;
++} PacketDesc;
++
++/**
++ * muxer type for PES
++ */
++typedef enum {
++    PESMUXER_PS,
++    PESMUXER_TS,
++    PESMUXER_PES
++} PESMuxerType;
++
++/**
++ * PES context
++ */
++typedef struct {
++    PESMuxerType muxer_type;  /**< whether it is a Program Stream */
++    int packet_number;
++} PESContext;
++
++/**
++ * PES stream structure
++ */
++typedef struct {
++    AVFifoBuffer fifo;
++    int max_buffer_size; /**< in bytes */
++    int buffer_index;
++    PacketDesc *predecode_packet;
++    PacketDesc *premux_packet;
++    PacketDesc **next_packet;
++} PESStream;
++
++
++#define AUDIO_ID 0xc0
++#define VIDEO_ID 0xe0
++#define AC3_ID   0x80
++#define DTS_ID   0x8a
++#define LPCM_ID  0xa0
++#define SUB_ID   0x20
++
++#define PROGRAM_STREAM_MAP 0x1bc
++#define PRIVATE_STREAM_1   0x1bd
++#define PADDING_STREAM     0x1be
++#define PRIVATE_STREAM_2   0x1bf
++
++
++
++/**
++ * Initialization of PES muxer.
++ * @param[in] ctx the AVFormatContext which contains streams
++ * @return  On error a negative value is returned, on success zero.
++ */
++int ff_pes_muxer_init(AVFormatContext *ctx);
++
++/**
++ * Finalization of PES muxer.
++ * @param [in] ctx the AVFormatContext which contains streams.
++ * @return  NULL
++ */
++void ff_pes_muxer_end(AVFormatContext *ctx);
++
++/**
++ * Write packet into PES FIFO.
++ * @param [in] ctx  the AVFormatContext which contains streams.
++ * @param [in] pkt  the packet to write.
++ * @return  NULL
++ */
++void ff_pes_write_packet(AVFormatContext *ctx, AVPacket *pkt);
++
++/**
++ * Find the stream to mux into the PES stream.
++ * @param[in] ctx          the AVFormatContext
++ * @param[in] packet_size  PES stream packet size
++ * @param[in] flush        Flush after every single subtitle packet.Find the stream to mux into the PES strea
++ * @param[out] best_i      index of stream to be muxed
++ * @return  On error a negative or zero value is returned, on success 1 is returned.
++ */
++int ff_pes_find_beststream(AVFormatContext *ctx, int packet_size, int flush, int64_t *scr, int* best_i);
++
++/**
++ * Get total number of frames that have been muxed.
++ * @param[in] ctx    the AVFormatContext
++ * @param[in] stream the PES stream
++ * @param[in] len    PES packet size
++ * @return  the frame number to be muxed
++ */
++int ff_get_nb_frames(AVFormatContext *ctx, PESStream *stream, int len);
++
++/**
++ * Mux streams into a PES packet.
++ * @param [in]      ctx            the AVFormatContext which contains streams
++ * @param [in]      stream_index   the stream index to write
++ * @param [in]      pes_buffer     PES payload data
++ * @param [in]      pts            packet presentation timestamp
++ * @param [in]      dts            packet decoding timestamp
++ * @param [in]      id             stream ID
++ * @param [in]      start_code     PES packet start code
++ * @param [in]      header_len     PES header size
++ * @param [in]      packet_size    total packet size
++ * @param [in]      payload_size   packet payload size
++ * @param [in]      stuffing_size  packet stuffing size
++ * @return   bytes written to PES stream.
++ */
++int ff_pes_muxer_write(AVFormatContext *ctx, int stream_index, uint8_t *pes_buffer,
++          int64_t pts,int64_t dts, int  id, int startcode,
++          uint8_t* pes_content, int pes_content_len,
++          int header_len, int packet_size, int payload_size, int stuffing_size);
++
++/**
++ * Remove decoded packets of each stream.
++ * @param[in] ctx  the AVFormatContext
++ * @param[in] scr  System Clock Reference of PES stream
++ * @return  On error a negative or zero value is returned, on success 1 is returned.
++ */
++int ff_pes_remove_decoded_packets(AVFormatContext *ctx, int64_t scr);
++
++/*
++ * Insert a timestamp into ByteIOContext.
++ * @param[in] pb        the IOContext to be written to
++ * @param[in] id        stream ID
++ * @param[in] timestamp the time stamp
++ * @return  NULL
++ */
++void ff_put_timestamp(ByteIOContext *pb, int id, int64_t timestamp);
++
++#endif/* AVFORMAT_MPEG_PES_H */
 +/*
 + * Copyright (c) 2000-2002 Fabrice Bellard
 + * Copyright (c) 2007 Xiaohui Sun <sunxiaohui at dsp.ac.cn>

Modified: dvbmuxer/muxer_dev/mpeg_pes.h
==============================================================================
--- dvbmuxer/muxer_dev/mpeg_pes.h	(original)
+++ dvbmuxer/muxer_dev/mpeg_pes.h	Mon Jul 30 14:50:30 2007
@@ -116,6 +116,173 @@ void ff_pes_write_packet(AVFormatContext
  * @param[out] best_i      index of stream to be muxed
  * @return  On error a negative or zero value is returned, on success 1 is returned.
  */
+int ff_pes_find_beststream(AVFormatContext *ctx, int packet_size, int flush, int64_t *scr, int* best_i);
+
+/**
+ * Get total number of frames that have been muxed.
+ * @param[in] ctx    the AVFormatContext
+ * @param[in] stream the PES stream
+ * @param[in] len    PES packet size
+ * @return  the frame number to be muxed
+ */
+int ff_get_nb_frames(AVFormatContext *ctx, PESStream *stream, int len);
+
+/**
+ * Mux streams into a PES packet.
+ * @param [in]      ctx            the AVFormatContext which contains streams
+ * @param [in]      stream_index   the stream index to write
+ * @param [in]      pes_buffer     PES payload data
+ * @param [in]      pts            packet presentation timestamp
+ * @param [in]      dts            packet decoding timestamp
+ * @param [in]      id             stream ID
+ * @param [in]      start_code     PES packet start code
+ * @param [in]      header_len     PES header size
+ * @param [in]      packet_size    total packet size
+ * @param [in]      payload_size   packet payload size
+ * @param [in]      stuffing_size  packet stuffing size
+ * @return   bytes written to PES stream.
+ */
+int ff_pes_muxer_write(AVFormatContext *ctx, int stream_index, uint8_t *pes_buffer,
+          int64_t pts,int64_t dts, int  id, int startcode,
+          uint8_t* pes_content, int pes_content_len,
+          int header_len, int packet_size, int payload_size, int stuffing_size);
+
+/**
+ * Remove decoded packets of each stream.
+ * @param[in] ctx  the AVFormatContext
+ * @param[in] scr  System Clock Reference of PES stream
+ * @return  On error a negative or zero value is returned, on success 1 is returned.
+ */
+int ff_pes_remove_decoded_packets(AVFormatContext *ctx, int64_t scr);
+
+/*
+ * Insert a timestamp into ByteIOContext.
+ * @param[in] pb        the IOContext to be written to
+ * @param[in] id        stream ID
+ * @param[in] timestamp the time stamp
+ * @return  NULL
+ */
+void ff_put_timestamp(ByteIOContext *pb, int id, int64_t timestamp);
+
+#endif/* AVFORMAT_MPEG_PES_H */
+/*
+ * Copyright (c) 2000-2002 Fabrice Bellard
+ * Copyright (c) 2007 Xiaohui Sun <sunxiaohui at dsp.ac.cn>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file mpeg_pes.h
+ * MPEG PES packetizer API header
+ */
+
+#ifndef AVFORMAT_MPEG_PES_H
+#define AVFORMAT_MPEG_PES_H
+
+#include "avformat.h"
+#include "fifo.h"
+
+/**
+ * PES packet description
+ */
+typedef struct PacketDesc {
+    int64_t pts;
+    int64_t dts;
+    int size;
+    int unwritten_size;
+    int flags;
+    struct PacketDesc *next;
+} PacketDesc;
+
+/**
+ * muxer type for PES
+ */
+typedef enum {
+    PESMUXER_PS,
+    PESMUXER_TS,
+    PESMUXER_PES
+} PESMuxerType;
+
+/**
+ * PES context
+ */
+typedef struct {
+    PESMuxerType muxer_type;  /**< whether it is a Program Stream */
+    int packet_number;
+} PESContext;
+
+/**
+ * PES stream structure
+ */
+typedef struct {
+    AVFifoBuffer fifo;
+    int max_buffer_size; /**< in bytes */
+    int buffer_index;
+    PacketDesc *predecode_packet;
+    PacketDesc *premux_packet;
+    PacketDesc **next_packet;
+} PESStream;
+
+
+#define AUDIO_ID 0xc0
+#define VIDEO_ID 0xe0
+#define AC3_ID   0x80
+#define DTS_ID   0x8a
+#define LPCM_ID  0xa0
+#define SUB_ID   0x20
+
+#define PROGRAM_STREAM_MAP 0x1bc
+#define PRIVATE_STREAM_1   0x1bd
+#define PADDING_STREAM     0x1be
+#define PRIVATE_STREAM_2   0x1bf
+
+
+
+/**
+ * Initialization of PES muxer.
+ * @param[in] ctx the AVFormatContext which contains streams
+ * @return  On error a negative value is returned, on success zero.
+ */
+int ff_pes_muxer_init(AVFormatContext *ctx);
+
+/**
+ * Finalization of PES muxer.
+ * @param [in] ctx the AVFormatContext which contains streams.
+ * @return  NULL
+ */
+void ff_pes_muxer_end(AVFormatContext *ctx);
+
+/**
+ * Write packet into PES FIFO.
+ * @param [in] ctx  the AVFormatContext which contains streams.
+ * @param [in] pkt  the packet to write.
+ * @return  NULL
+ */
+void ff_pes_write_packet(AVFormatContext *ctx, AVPacket *pkt);
+
+/**
+ * Find the stream to mux into the PES stream.
+ * @param[in] ctx          the AVFormatContext
+ * @param[in] packet_size  PES stream packet size
+ * @param[in] flush        Flush after every single subtitle packet.Find the stream to mux into the PES strea
+ * @param[out] best_i      index of stream to be muxed
+ * @return  On error a negative or zero value is returned, on success 1 is returned.
+ */
 int ff_pes_find_beststream(AVFormatContext *ctx, int packet_size, int flush, int64_t scr, int* best_i);
 
 /**

Modified: dvbmuxer/muxer_dev/mpeg_pes_enc.c
==============================================================================
--- dvbmuxer/muxer_dev/mpeg_pes_enc.c	(original)
+++ dvbmuxer/muxer_dev/mpeg_pes_enc.c	Mon Jul 30 14:50:30 2007
@@ -112,7 +112,7 @@ int ff_pes_remove_decoded_packets(AVForm
               && scr > pkt_desc->dts){ //FIXME > vs >=
             if(stream->buffer_index < pkt_desc->size ||
                stream->predecode_packet == stream->premux_packet){
-                av_log(ctx, AV_LOG_ERROR,
+                   av_log(ctx, AV_LOG_ERROR,
                        "buffer underflow i=%d bufi=%d size=%d\n",
                        i, stream->buffer_index, pkt_desc->size);
                 break;
@@ -190,7 +190,7 @@ int ff_pes_muxer_write(AVFormatContext *
     return (q - pes_buffer + data_size);
 }
 
-int ff_pes_find_beststream(AVFormatContext *ctx, int packet_size, int flush, int64_t scr, int* best_i)
+int ff_pes_find_beststream(AVFormatContext *ctx, int packet_size, int flush, int64_t *scr, int* best_i)
 {
     int best_score = INT_MIN;
     int i, avail_space = 0;
@@ -218,7 +218,7 @@ retry:
         if(space < packet_size && !ignore_constraints)
             continue;
 
-        if(next_pkt && next_pkt->dts - scr > max_delay)
+        if(next_pkt && next_pkt->dts - *scr > max_delay)
             continue;
 
         if(rel_space > best_score){
@@ -247,12 +247,12 @@ retry:
         if(best_dts == INT64_MAX)
             return 0;
 
-        if(scr >= best_dts+1 && !ignore_constraints){
+        if(*scr >= best_dts+1 && !ignore_constraints){
             av_log(ctx, AV_LOG_ERROR, "packet too large, ignoring buffer limits to mux it\n");
             ignore_constraints= 1;
         }
-        scr= FFMAX(best_dts+1, scr);
-        if(ff_pes_remove_decoded_packets(ctx, scr) < 0)
+        *scr= FFMAX(best_dts+1, *scr);
+        if(ff_pes_remove_decoded_packets(ctx, *scr) < 0)
             return -1;
         goto retry;
     }
@@ -282,8 +282,8 @@ void ff_pes_write_packet(AVFormatContext
         stream->next_packet = &stream->premux_packet;
     *stream->next_packet=
     pkt_desc= av_mallocz(sizeof(PacketDesc));
-    pkt_desc->pts= pkt->pts;
-    pkt_desc->dts= pkt->dts;
+    pkt_desc->pts= pts;
+    pkt_desc->dts= dts;
     pkt_desc->unwritten_size=
     pkt_desc->size= size;
     if(!stream->predecode_packet)

Modified: dvbmuxer/muxer_dev/mpegtsenc.c
==============================================================================
--- dvbmuxer/muxer_dev/mpegtsenc.c	(original)
+++ dvbmuxer/muxer_dev/mpegtsenc.c	Mon Jul 30 14:50:30 2007
@@ -62,8 +62,9 @@ static void mpegts_write_section(MpegTSS
             b |= 0x40;
         *q++ = b;
         *q++ = s->pid;
-        s->cc = (s->cc + 1) & 0xf;
+        s->cc = (s->cc) & 0xf;
         *q++ = 0x10 | s->cc;
+        s->cc++;
         if (first)
             *q++ = 0; /* 0 offset */
         len1 = TS_PACKET_SIZE - (q - packet);
@@ -454,28 +455,18 @@ static int mpegts_write_header(AVFormatC
 
     bitrate = 0;
     for(i=0;i<s->nb_streams;i++) {
-        int codec_rate;
         st = s->streams[i];
         ts_st = (MpegTSWriteStream*) st->priv_data;
-
-        if(st->codec->rc_max_rate || ts_st->id==VIDEO_ID)
-            codec_rate= st->codec->rc_max_rate;
-        else
-            codec_rate= st->codec->bit_rate;
-        bitrate += codec_rate;
+        bitrate += st->codec->bit_rate;
     }
 
-    if(s->mux_rate){
-        ts->mux_rate= (s->mux_rate + (8 * 50) - 1) / (8 * 50);
-    } else {
-        bitrate += bitrate*5/100;
-        bitrate = bitrate * 25 / (8 *  DEFAULT_PES_PAYLOAD_SIZE) +  /* PES header size */
-                   bitrate * 4 / (8 * TS_PACKET_SIZE) +             /* TS  header size */
-                   500 * 12 +                                       /* SDT size */
-                   100 * 16;                                        /* PAT size */
-        ts->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
-    }
-    ts->last_pcr = 0;
+    //bitrate += bitrate*5/100;
+    bitrate += bitrate * 25 / (8 *  DEFAULT_PES_PAYLOAD_SIZE) +  /* PES header size */
+               bitrate * 4 / (8 * TS_PACKET_SIZE) +             /* TS  header size */
+               500 * 12 +                                       /* SDT size */
+               100 * 16;                                        /* PAT size */
+    ts->mux_rate = bitrate;
+    ts->last_pcr = 10000;  /* add a preload value */
 
     service->pcr_packet_freq = (ts->mux_rate * PCR_RETRANS_TIME) /
         (TS_PACKET_SIZE * 8 * 1000);
@@ -536,23 +527,23 @@ static void mpegts_write_pes(AVFormatCon
     int val, is_start, len, header_len, write_pcr;
     int afc_len, stuffing_len;
     int64_t pcr = -1; /* avoid warning */
-    int offset = 0;
-
+    int offset = 0, packet_count = 0;
+    static int p = 0;
     is_start = 1;
     while (payload_size > 0) {
         retransmit_si_info(s);
-
+        p++;
         write_pcr = 0;
         if (ts_st->pid == ts_st->service->pcr_pid) {
+            packet_count ++;
             ts_st->service->pcr_packet_count++;
             if (ts_st->service->pcr_packet_count >=
-                ts_st->service->pcr_packet_freq) {
+                ts_st->service->pcr_packet_freq && dts != AV_NOPTS_VALUE) {
                 ts_st->service->pcr_packet_count = 0;
                 write_pcr = 1;
-                pcr = ts->last_pcr;
+                pcr = dts + packet_count * TS_PACKET_SIZE* 8*90000LL / ts->mux_rate;
             }
         }
-
         /* prepare packet header */
         q = buf;
         *q++ = 0x47;
@@ -677,6 +668,7 @@ static int flush_packet(AVFormatContext 
                 stuffing_size += payload_size - trailer_size;
         }
 
+        //av_log(ctx, AV_LOG_INFO, "Cur pts is %"PRId64", Cur dts is %"PRId64", payload_size %d, tailer_size %d\n", pts, dts, 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
@@ -708,8 +700,8 @@ static int flush_packet(AVFormatContext 
         stuffing_size= 0;
     }
 
-    if (pad_packet_bytes > 0)
-        put_padding_packet(ctx,&ctx->pb, pad_packet_bytes);
+    //if (pad_packet_bytes > 0)
+     //   put_padding_packet(ctx,&ctx->pb, pad_packet_bytes);
 
     for(i=0;i<zero_trail_bytes;i++)
         put_byte(&ctx->pb, 0x00);
@@ -744,10 +736,10 @@ static int output_packet(AVFormatContext
     int es_size, trailer_size;
     int result;
     int best_i= -1;
-    int64_t pcr= s->last_pcr;
+    int pcr = s->last_pcr;
     PacketDesc *timestamp_packet;
 
-    if((result = ff_pes_find_beststream(ctx, s->packet_size, flush, pcr, &best_i)) <= 0)
+    if((result = ff_pes_find_beststream(ctx, s->packet_size, flush, &pcr, &best_i)) <= 0)
         return result;
 
     assert(best_i >= 0);
@@ -774,8 +766,7 @@ static int output_packet(AVFormatContext
     }
 
 
-    s->last_pcr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
-
+    s->last_pcr += s->packet_size*8*90000LL / (s->mux_rate); //FIXME rounding and first few bytes of each packet
     if(ff_pes_remove_decoded_packets(ctx, s->last_pcr) < 0)
         return -1;
 
@@ -786,11 +777,17 @@ static int output_packet(AVFormatContext
 static int mpegts_write_packet(AVFormatContext *ctx, AVPacket *pkt)
 {
     int stream_index= pkt->stream_index;
+    int size = pkt->size;
+    static int total_size = 0;
     AVStream *st = ctx->streams[stream_index];
     MpegTSWriteStream *stream = st->priv_data;
     PESStream *pes_stream = &stream->pes_stream;
     int64_t pts;
 
+    /* add a preload value to avoid negative dts*/
+    pkt->pts += 10000;
+    pkt->dts += 10000;
+    total_size += size;
     ff_pes_write_packet(ctx, pkt);
     pts= pes_stream->predecode_packet->pts;
 



More information about the FFmpeg-soc mailing list