[FFmpeg-soc] [soc]: r2089 - in dvbmuxer: mpegenc.c mpegpes.h mpegpesenc.c mpegtsenc.c

bcoudurier subversion at mplayerhq.hu
Fri Apr 4 13:07:56 CEST 2008


Author: bcoudurier
Date: Fri Apr  4 13:07:56 2008
New Revision: 2089

Log:
move common code to ff_pes_cal_header

Modified:
   dvbmuxer/mpegenc.c
   dvbmuxer/mpegpes.h
   dvbmuxer/mpegpesenc.c
   dvbmuxer/mpegtsenc.c

Modified: dvbmuxer/mpegenc.c
==============================================================================
--- dvbmuxer/mpegenc.c	(original)
+++ dvbmuxer/mpegenc.c	Fri Apr  4 13:07:56 2008
@@ -303,6 +303,15 @@ static int mpeg_mux_init(AVFormatContext
             goto fail;
         st->priv_data = stream;
 
+        /* set PESStream format */
+        if (s->is_dvd)
+            stream->format = PES_FMT_DVD;
+        if (s->is_svcd)
+            stream->format = PES_FMT_SVCD;
+        if (s->is_mpeg2)
+            stream->format = PES_FMT_MPEG2;
+        if (s->is_vcd)
+            stream->format = PES_FMT_VCD;
         av_set_pts_info(st, 64, 1, 90000);
 
         switch(st->codec->codec_type) {
@@ -712,83 +721,10 @@ static int flush_packet(AVFormatContext 
     packet_size -= pad_packet_bytes + zero_trail_bytes;
 
     if (packet_size > 0) {
-
-        /* 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;
-        }
+        ff_pes_cal_header(id, stream,
+                          &packet_size, &header_len, &pts, &dts,
+                          &payload_size, &startcode, &stuffing_size,
+                          &trailer_size, &pad_packet_bytes);
 
         nb_frames= ff_pes_get_nb_frames(ctx, stream, payload_size - stuffing_size);
 

Modified: dvbmuxer/mpegpes.h
==============================================================================
--- dvbmuxer/mpegpes.h	(original)
+++ dvbmuxer/mpegpes.h	Fri Apr  4 13:07:56 2008
@@ -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 | PES_FMT_MPEG2)
+#define PES_FMT_DVD (0x08 | PES_FMT_MPEG2)
+#define PES_FMT_TS (0x10 | PES_FMT_MPEG2)
+
 /**
  * PES packet description
  */
@@ -48,6 +54,7 @@ typedef struct PacketDesc {
 typedef struct {
     AVFifoBuffer fifo;
     uint8_t id;
+    int format;
     int max_buffer_size; /**< in bytes */
     int buffer_index;
     PacketDesc *predecode_packet;
@@ -117,6 +124,26 @@ int ff_pes_find_beststream(AVFormatConte
  */
 int ff_pes_get_nb_frames(AVFormatContext *ctx, PESStream *stream, int len);
 
+
+/**
+ * Caculate the PES header size
+ * @param[in] id                stream id
+ * @param[in] stream            pes stream
+ * @param[in] packet_size       pes packet size
+ * @param[in] header_len        pes header length
+ * @param[in] pts               current pts
+ * @param[in] dts               current dts
+ * @param[in] payload_size      pes payload size
+ * @param[in] startcode         pes startcode
+ * @param[in] stuffing_size     pes stuffing size
+ * @param[in] trailer_size      unwritten trailer size
+ * @param[in] pad_packet_bytes  padding size for packet
+ */
+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

Modified: dvbmuxer/mpegpesenc.c
==============================================================================
--- dvbmuxer/mpegpesenc.c	(original)
+++ dvbmuxer/mpegpesenc.c	Fri Apr  4 13:07:56 2008
@@ -100,6 +100,105 @@ int ff_pes_get_nb_frames(AVFormatContext
 
     return nb_frames;
 }
+/**
+ * Caculate the PES header size
+ * @param[in] id                stream id
+ * @param[in] stream            pes stream
+ * @param[in] packet_size       pes packet size
+ * @param[in] header_len        pes header length
+ * @param[in] pts               current pts
+ * @param[in] dts               current dts
+ * @param[in] payload_size      pes payload size
+ * @param[in] startcode         pes startcode
+ * @param[in] stuffing_size     pes stuffing size
+ * @param[in] trailer_size      unwritten trailer size
+ * @param[in] pad_packet_bytes  padding size for packet
+ */
+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_MPEG2) {
+        *header_len = 3;
+        if (stream->packet_number==0 && stream->format != PES_FMT_TS)
+            *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 (!(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.

Modified: dvbmuxer/mpegtsenc.c
==============================================================================
--- dvbmuxer/mpegtsenc.c	(original)
+++ dvbmuxer/mpegtsenc.c	Fri Apr  4 13:07:56 2008
@@ -623,7 +623,7 @@ static int flush_packet(AVFormatContext 
     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;
@@ -631,49 +631,15 @@ static int flush_packet(AVFormatContext 
     int pes_size;
     uint8_t* q = stream->payload;
 
+    pes_stream->format = PES_FMT_TS;
     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)



More information about the FFmpeg-soc mailing list