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

bcoudurier subversion at mplayerhq.hu
Tue Jun 3 22:14:58 CEST 2008


Author: bcoudurier
Date: Tue Jun  3 22:14:57 2008
New Revision: 2367

Log:
extract code from ps and merge it into ff_pes_write_buf

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

Modified: dvbmuxer/mpegenc.c
==============================================================================
--- dvbmuxer/mpegenc.c	(original)
+++ dvbmuxer/mpegenc.c	Tue Jun  3 22:14:57 2008
@@ -49,7 +49,7 @@ typedef struct {
 
     double vcd_padding_bitrate; //FIXME floats
     int64_t vcd_padding_bytes_written;
-
+    uint8_t *payload; ///< PES packet size
 } MpegMuxContext;
 
 extern AVOutputFormat mpeg1vcd_muxer;
@@ -284,7 +284,9 @@ static int mpeg_mux_init(AVFormatContext
         s->packet_size = ctx->packet_size;
     else
         s->packet_size = 2048;
-
+    s->payload = av_mallocz(s->packet_size);
+    if (!s->payload)
+        return AVERROR(ENOMEM);
     s->vcd_padding_bytes_written = 0;
     s->vcd_padding_bitrate=0;
 
@@ -609,14 +611,12 @@ static int flush_packet(AVFormatContext 
     MpegMuxContext *s = ctx->priv_data;
     StreamInfo *stream = ctx->streams[stream_index]->priv_data;
     uint8_t *buf_ptr;
-    int size, payload_size, startcode, id, stuffing_size, i, header_len;
+    int size, payload_size, id, stuffing_size, i;
     int packet_size;
     uint8_t buffer[128];
     int zero_trail_bytes = 0;
     int pad_packet_bytes = 0;
-    int pes_flags;
     int general_pack = 0;  /*"general" pack without data specific to one stream?*/
-    int nb_frames;
 
     id = stream->id;
 
@@ -717,98 +717,14 @@ static int flush_packet(AVFormatContext 
     packet_size -= pad_packet_bytes + zero_trail_bytes;
 
     if (packet_size > 0) {
-        ff_pes_cal_header(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);
-
-        put_be32(ctx->pb, startcode);
-
-        put_be16(ctx->pb, packet_size);
-
-        if (!s->is_mpeg2)
-            for(i=0;i<stuffing_size;i++)
-                put_byte(ctx->pb, 0xff);
-
-        if (s->is_mpeg2) {
-            put_byte(ctx->pb, 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 (stream->packet_number == 0)
-                pes_flags |= 0x01;
-
-            put_byte(ctx->pb, pes_flags); /* flags */
-            put_byte(ctx->pb, header_len - 3 + stuffing_size);
-
-            if (pes_flags & 0x80)  /*write pts*/
-                put_timestamp(ctx->pb, (pes_flags & 0x40) ? 0x03 : 0x02, pts);
-            if (pes_flags & 0x40)  /*write dts*/
-                put_timestamp(ctx->pb, 0x01, dts);
-
-            if (pes_flags & 0x01) {  /*write pes extension*/
-                put_byte(ctx->pb, 0x10); /* flags */
-
-                /* P-STD buffer info */
-                if (id == AUDIO_ID)
-                    put_be16(ctx->pb, 0x4000 | stream->max_buffer_size/128);
-                else
-                    put_be16(ctx->pb, 0x6000 | stream->max_buffer_size/1024);
-            }
-
-        } else {
-            if (pts != AV_NOPTS_VALUE) {
-                if (dts != pts) {
-                    put_timestamp(ctx->pb, 0x03, pts);
-                    put_timestamp(ctx->pb, 0x01, dts);
-                } else {
-                    put_timestamp(ctx->pb, 0x02, pts);
-                }
-            } else {
-                put_byte(ctx->pb, 0x0f);
-            }
-        }
-
-        if (s->is_mpeg2) {
-            /* special stuffing byte that is always written
-               to prevent accidental generation of start codes. */
-            put_byte(ctx->pb, 0xff);
-
-            for(i=0;i<stuffing_size;i++)
-                put_byte(ctx->pb, 0xff);
-        }
-
-        if (startcode == PRIVATE_STREAM_1) {
-            put_byte(ctx->pb, id);
-            if (id >= 0xa0) {
-                /* LPCM (XXX: check nb_frames) */
-                put_byte(ctx->pb, 7);
-                put_be16(ctx->pb, 4); /* skip 3 header bytes */
-                put_byte(ctx->pb, stream->lpcm_header[0]);
-                put_byte(ctx->pb, stream->lpcm_header[1]);
-                put_byte(ctx->pb, stream->lpcm_header[2]);
-            } else if (id >= 0x40) {
-                /* AC3 */
-                put_byte(ctx->pb, nb_frames);
-                put_be16(ctx->pb, trailer_size+1);
-            }
-        }
-
+        int pes_size = ff_pes_write_buf(ctx, stream_index, s->payload,
+                                        &pts, &dts, trailer_size,
+                                        &packet_size, &pad_packet_bytes,
+                                        &payload_size, &stuffing_size);
+        if(pes_size < 0)
+            return -1;
         /* output data */
-        assert(payload_size - stuffing_size <= av_fifo_size(&stream->fifo));
-        av_fifo_generic_read(&stream->fifo, payload_size - stuffing_size, &put_buffer, ctx->pb);
+        put_buffer(ctx->pb, s->payload, pes_size);
         stream->bytes_to_iframe -= payload_size - stuffing_size;
     }else{
         payload_size=
@@ -996,7 +912,7 @@ static int mpeg_mux_write_packet(AVForma
 
 static int mpeg_mux_end(AVFormatContext *ctx)
 {
-//    MpegMuxContext *s = ctx->priv_data;
+    MpegMuxContext *s = ctx->priv_data;
     StreamInfo *stream;
     int i;
 
@@ -1020,6 +936,7 @@ static int mpeg_mux_end(AVFormatContext 
         assert(av_fifo_size(&stream->fifo) == 0);
         av_fifo_free(&stream->fifo);
     }
+    av_free(s->payload);
     return 0;
 }
 

Modified: dvbmuxer/mpegpes.h
==============================================================================
--- dvbmuxer/mpegpes.h	(original)
+++ dvbmuxer/mpegpes.h	Tue Jun  3 22:14:57 2008
@@ -134,21 +134,22 @@ void ff_pes_cal_header(StreamInfo *strea
 
 /**
  * Write PES data from PES Stream into supplied buffer.
- * @param [in] ctx            AVFormatContext
- * @param [in] stream_index   Stream index to write from
- * @param [in] buf            Buffer to write to
- * @param [in] pts            Packet pts
- * @param [in] dts            Packet dts
- * @param [in] start_code     PES packet startcode
- * @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 buffer
+ * @param [in]  ctx               AVFormatContext
+ * @param [in]  stream_index      Stream index to write from
+ * @param [in]  buf               Buffer to write to
+ * @param [in]  pts               Packet pts
+ * @param [in]  dts               Packet dts
+ * @param [in]  trailer_size      Packet trailer size
+ * @param [in]  packet_size       Total packet size
+ * @param [in]  pad_packet_bytes  Packet padding size
+ * @param [out] payload_size      Packet payload size
+ * @param [out] stuffing_size     Packet stuffing size
+ * @return                        Bytes written to buffer
  */
 int ff_pes_write_buf(AVFormatContext *ctx, int stream_index, uint8_t *buf,
-          int64_t pts, int64_t dts, int startcode,
-          int header_len, int packet_size, int payload_size, int stuffing_size);
+          int64_t *pts, int64_t *dts,
+          int trailer_size, int *packet_size, int *pad_packet_bytes,
+          int *payload_size, int *stuffing_size);
 
 /**
  * Remove decoded packets of each stream.

Modified: dvbmuxer/mpegpesenc.c
==============================================================================
--- dvbmuxer/mpegpesenc.c	(original)
+++ dvbmuxer/mpegpesenc.c	Tue Jun  3 22:14:57 2008
@@ -20,6 +20,7 @@
  */
 
 #include "mpegpes.h"
+#include "mpeg.h"
 #include "libavcodec/bytestream.h"
 
 int ff_pes_muxer_init(AVFormatContext *ctx)
@@ -62,7 +63,7 @@ int ff_pes_muxer_init(AVFormatContext *c
     return 0;
 }
 
-static inline void insert_timestamp(uint8_t** p, int id, int64_t timestamp)
+static inline void put_timestamp(uint8_t** p, int id, int64_t timestamp)
 {
     bytestream_put_byte(p,
         (id << 4) |
@@ -171,43 +172,110 @@ void ff_pes_cal_header(StreamInfo *strea
 }
 
 int ff_pes_write_buf(AVFormatContext *ctx, int stream_index, uint8_t *buf,
-    int64_t pts, int64_t dts, int startcode,
-    int header_len, int packet_size, int payload_size, int stuffing_size)
+                     int64_t *pts, int64_t *dts,
+                     int trailer_size, int *packet_size, int *pad_packet_bytes,
+                     int *payload_size, int *stuffing_size)
 {
     StreamInfo *stream = ctx->streams[stream_index]->priv_data;
-    int i, flags = 0;
-    int data_size = payload_size - stuffing_size;
-    uint8_t *q = buf;
+    int startcode, i, header_len;
+    int pes_flags = 0;
+    uint8_t *p = buf;
+    int nb_frames;
 
-    bytestream_put_be32(&q, startcode);
-    bytestream_put_be16(&q, packet_size);
-    bytestream_put_byte(&q, 0x80); /* mpeg2 id */
+    ff_pes_cal_header(stream,
+                      packet_size, &header_len, pts, dts,
+                      payload_size, &startcode, stuffing_size,
+                      &trailer_size, pad_packet_bytes);
 
-    if (pts != AV_NOPTS_VALUE) {
-        flags |= 0x80;
-        if (dts != pts)
-           flags |= 0x40;
-    }
+    nb_frames= ff_pes_get_nb_frames(ctx, stream, *payload_size - *stuffing_size);
 
-    bytestream_put_byte(&q, flags);
-    bytestream_put_byte(&q, header_len - 3 + stuffing_size);
+    bytestream_put_be32(&p, startcode);
 
-    if (flags & 0x80)  /* write pts */
-        insert_timestamp(&q, (flags & 0x40) ? 0x03 : 0x02, pts);
-    if (flags & 0x40)  /* write dts */
-        insert_timestamp(&q, 0x01, dts);
+    bytestream_put_be16(&p, *packet_size);
 
-    /* special stuffing byte that is always written
-       to prevent accidental generation of startcodes. */
-    bytestream_put_byte(&q, 0xff);
+    if (!(stream->format & PES_FMT_MPEG2))
+        for(i=0;i<*stuffing_size;i++)
+            bytestream_put_byte(&p, 0xff);
 
-    for(i=0;i<stuffing_size;i++)
-        bytestream_put_byte(&q, 0xff);
+    if (stream->format & PES_FMT_MPEG2) {
+        bytestream_put_byte(&p, 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 (!(stream->format & PES_FMT_TS) && stream->packet_number == 0)
+            pes_flags |= 0x01;
+
+        bytestream_put_byte(&p, pes_flags); /* flags */
+        bytestream_put_byte(&p, header_len - 3 + *stuffing_size);
+
+        if (pes_flags & 0x80)  /*write pts*/
+            put_timestamp(&p, (pes_flags & 0x40) ? 0x03 : 0x02, *pts);
+        if (pes_flags & 0x40)  /*write dts*/
+            put_timestamp(&p, 0x01, *dts);
+
+        if (pes_flags & 0x01) {  /*write pes extension*/
+            bytestream_put_byte(&p, 0x10); /* flags */
+
+            /* P-STD buffer info */
+            if (stream->id == AUDIO_ID)
+                bytestream_put_be16(&p, 0x4000 | stream->max_buffer_size/128);
+            else
+                bytestream_put_be16(&p, 0x6000 | stream->max_buffer_size/1024);
+        }
+
+    } else {
+        if (*pts != AV_NOPTS_VALUE) {
+            if (*dts != *pts) {
+                put_timestamp(&p, 0x03, *pts);
+                put_timestamp(&p, 0x01, *dts);
+            } else {
+                put_timestamp(&p, 0x02, *pts);
+            }
+        } else {
+            bytestream_put_byte(&p, 0x0f);
+        }
+    }
+
+    if (stream->format & PES_FMT_MPEG2) {
+        /* special stuffing byte that is always written
+           to prevent accidental generation of start codes. */
+        bytestream_put_byte(&p, 0xff);
+
+        for(i=0;i<*stuffing_size;i++)
+            bytestream_put_byte(&p, 0xff);
+    }
+
+    if (!(stream->format & PES_FMT_TS) && startcode == PRIVATE_STREAM_1) {
+        bytestream_put_byte(&p, stream->id);
+        if (stream->id >= 0xa0) {
+            /* LPCM (XXX: check nb_frames) */
+            bytestream_put_byte(&p, 7);
+            bytestream_put_be16(&p, 4); /* skip 3 header bytes */
+            bytestream_put_byte(&p, stream->lpcm_header[0]);
+            bytestream_put_byte(&p, stream->lpcm_header[1]);
+            bytestream_put_byte(&p, stream->lpcm_header[2]);
+        } else if (stream->id >= 0x40) {
+            /* AC3 */
+            bytestream_put_byte(&p, nb_frames);
+            bytestream_put_be16(&p, trailer_size+1);
+        }
+    }
 
     /* output data */
-    if(av_fifo_read(&stream->fifo, q, data_size) < 0)
+    assert(payload_size - stuffing_size <= av_fifo_size(&stream->fifo));
+    if(av_fifo_read(&stream->fifo, p, *payload_size - *stuffing_size) < 0)
         return -1;
-    return q - buf + data_size;
+    return p - buf + *payload_size - *stuffing_size;
 }
 
 int ff_pes_remove_decoded_packets(AVFormatContext *ctx, int64_t scr)

Modified: dvbmuxer/mpegtsenc.c
==============================================================================
--- dvbmuxer/mpegtsenc.c	(original)
+++ dvbmuxer/mpegtsenc.c	Tue Jun  3 22:14:57 2008
@@ -604,7 +604,7 @@ static int flush_packet(AVFormatContext 
 {
     MpegTSWriteStream *stream = ctx->streams[stream_index]->priv_data;
     StreamInfo *pes_stream = &stream->pes_stream;
-    int payload_size, startcode, stuffing_size, i, header_len;
+    int payload_size, stuffing_size, i;
     int packet_size, es_size;
     int zero_trail_bytes = 0;
     int pad_packet_bytes = 0;
@@ -614,13 +614,10 @@ static int flush_packet(AVFormatContext 
     packet_size = DEFAULT_PES_PAYLOAD_SIZE;
 
     if (packet_size > 0) {
-        ff_pes_cal_header(pes_stream,
-                          &packet_size, &header_len, &pts, &dts,
-                          &payload_size, &startcode, &stuffing_size,
-                          &trailer_size, &pad_packet_bytes);
         pes_size = ff_pes_write_buf(ctx, stream_index, stream->payload,
-                 pts, dts, startcode, header_len,
-                 packet_size, payload_size, stuffing_size);
+                                    &pts, &dts, trailer_size,
+                                    &packet_size, &pad_packet_bytes,
+                                    &payload_size, &stuffing_size);
         if(pes_size < 0)
             return -1;
         q += pes_size;



More information about the FFmpeg-soc mailing list