[SCM] libav/experimental: In mpegts muxer, write adts header if aac bitstream does not contain it

siretart at users.alioth.debian.org siretart at users.alioth.debian.org
Sun Jun 30 17:03:42 UTC 2013


The following commit has been merged in the experimental branch:
commit 8fdd542ce4c357d38b4601e9a327f499c9a096f1
Author: Baptiste Coudurier <baptiste.coudurier at gmail.com>
Date:   Tue Mar 9 00:18:49 2010 +0000

    In mpegts muxer, write adts header if aac bitstream does not contain it
    
    Originally committed as revision 22355 to svn://svn.ffmpeg.org/ffmpeg/trunk

diff --git a/libavformat/Makefile b/libavformat/Makefile
index eb710cc..67a19e4 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -126,7 +126,7 @@ OBJS-$(CONFIG_MPEG1VIDEO_MUXER)          += raw.o
 OBJS-$(CONFIG_MPEG2VIDEO_MUXER)          += raw.o
 OBJS-$(CONFIG_MPEGPS_DEMUXER)            += mpeg.o
 OBJS-$(CONFIG_MPEGTS_DEMUXER)            += mpegts.o
-OBJS-$(CONFIG_MPEGTS_MUXER)              += mpegtsenc.o
+OBJS-$(CONFIG_MPEGTS_MUXER)              += mpegtsenc.o adtsenc.o
 OBJS-$(CONFIG_MPEGVIDEO_DEMUXER)         += raw.o
 OBJS-$(CONFIG_MPJPEG_MUXER)              += mpjpeg.o
 OBJS-$(CONFIG_MSNWC_TCP_DEMUXER)         += msnwc_tcp.o
diff --git a/libavformat/internal.h b/libavformat/adts.h
similarity index 53%
copy from libavformat/internal.h
copy to libavformat/adts.h
index 685b550..1da57be 100644
--- a/libavformat/internal.h
+++ b/libavformat/adts.h
@@ -1,5 +1,7 @@
 /*
- * copyright (c) 2001 Fabrice Bellard
+ * ADTS muxer.
+ * Copyright (c) 2006 Baptiste Coudurier <baptiste.coudurier at smartjog.com>
+ *                    Mans Rullgard <mans at mansr.com>
  *
  * This file is part of FFmpeg.
  *
@@ -18,23 +20,26 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#ifndef AVFORMAT_INTERNAL_H
-#define AVFORMAT_INTERNAL_H
+#ifndef AVFORMAT_ADTS_H
+#define AVFORMAT_ADTS_H
 
-#include <stdint.h>
 #include "avformat.h"
+#include "libavcodec/mpeg4audio.h"
 
-char *ff_data_to_hex(char *buf, const uint8_t *src, int size);
+#define ADTS_HEADER_SIZE 7
 
-void av_program_add_stream_index(AVFormatContext *ac, int progid, unsigned int idx);
+typedef struct {
+    int write_adts;
+    int objecttype;
+    int sample_rate_index;
+    int channel_conf;
+    int pce_size;
+    uint8_t pce_data[MAX_PCE_SIZE];
+} ADTSContext;
 
-/**
- * Add packet to AVFormatContext->packet_buffer list, determining its
- * interleaved position using compare() function argument.
- */
-void ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt,
-                              int (*compare)(AVFormatContext *, AVPacket *, AVPacket *));
-
-void av_read_frame_flush(AVFormatContext *s);
+int ff_adts_write_frame_header(ADTSContext *ctx, uint8_t *buf,
+                               int size, int pce_size);
+int ff_adts_decode_extradata(AVFormatContext *s, ADTSContext *adts,
+                             uint8_t *buf, int size);
 
-#endif /* AVFORMAT_INTERNAL_H */
+#endif /* AVFORMAT_ADTS_H */
diff --git a/libavformat/adtsenc.c b/libavformat/adtsenc.c
index 260c593..ecc8dc4 100644
--- a/libavformat/adtsenc.c
+++ b/libavformat/adtsenc.c
@@ -23,21 +23,10 @@
 #include "libavcodec/get_bits.h"
 #include "libavcodec/put_bits.h"
 #include "libavcodec/avcodec.h"
-#include "libavcodec/mpeg4audio.h"
 #include "avformat.h"
+#include "adts.h"
 
-#define ADTS_HEADER_SIZE 7
-
-typedef struct {
-    int write_adts;
-    int objecttype;
-    int sample_rate_index;
-    int channel_conf;
-    int pce_size;
-    uint8_t pce_data[MAX_PCE_SIZE];
-} ADTSContext;
-
-static int decode_extradata(AVFormatContext *s, ADTSContext *adts, uint8_t *buf, int size)
+int ff_adts_decode_extradata(AVFormatContext *s, ADTSContext *adts, uint8_t *buf, int size)
 {
     GetBitContext gb;
     PutBitContext pb;
@@ -86,17 +75,16 @@ static int adts_write_header(AVFormatContext *s)
     AVCodecContext *avc = s->streams[0]->codec;
 
     if(avc->extradata_size > 0 &&
-            decode_extradata(s, adts, avc->extradata, avc->extradata_size) < 0)
+            ff_adts_decode_extradata(s, adts, avc->extradata, avc->extradata_size) < 0)
         return -1;
 
     return 0;
 }
 
-static int adts_write_frame_header(AVFormatContext *s, int size)
+int ff_adts_write_frame_header(ADTSContext *ctx,
+                               uint8_t *buf, int size, int pce_size)
 {
-    ADTSContext *ctx = s->priv_data;
     PutBitContext pb;
-    uint8_t buf[ADTS_HEADER_SIZE];
 
     init_put_bits(&pb, buf, ADTS_HEADER_SIZE);
 
@@ -115,16 +103,11 @@ static int adts_write_frame_header(AVFormatContext *s, int size)
     /* adts_variable_header */
     put_bits(&pb, 1, 0);        /* copyright_identification_bit */
     put_bits(&pb, 1, 0);        /* copyright_identification_start */
-    put_bits(&pb, 13, ADTS_HEADER_SIZE + size + ctx->pce_size); /* aac_frame_length */
+    put_bits(&pb, 13, ADTS_HEADER_SIZE + size + pce_size); /* aac_frame_length */
     put_bits(&pb, 11, 0x7ff);   /* adts_buffer_fullness */
     put_bits(&pb, 2, 0);        /* number_of_raw_data_blocks_in_frame */
 
     flush_put_bits(&pb);
-    put_buffer(s->pb, buf, ADTS_HEADER_SIZE);
-    if (ctx->pce_size) {
-        put_buffer(s->pb, ctx->pce_data, ctx->pce_size);
-        ctx->pce_size = 0;
-    }
 
     return 0;
 }
@@ -133,11 +116,18 @@ static int adts_write_packet(AVFormatContext *s, AVPacket *pkt)
 {
     ADTSContext *adts = s->priv_data;
     ByteIOContext *pb = s->pb;
+    uint8_t buf[ADTS_HEADER_SIZE];
 
     if (!pkt->size)
         return 0;
-    if(adts->write_adts)
-        adts_write_frame_header(s, pkt->size);
+    if(adts->write_adts) {
+        ff_adts_write_frame_header(adts, buf, pkt->size, adts->pce_size);
+        put_buffer(pb, buf, ADTS_HEADER_SIZE);
+        if(adts->pce_size) {
+            put_buffer(pb, adts->pce_data, adts->pce_size);
+            adts->pce_size = 0;
+        }
+    }
     put_buffer(pb, pkt->data, pkt->size);
     put_flush_packet(pb);
 
diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
index 64e6fdd..b818964 100644
--- a/libavformat/mpegtsenc.c
+++ b/libavformat/mpegtsenc.c
@@ -24,6 +24,7 @@
 #include "libavcodec/mpegvideo.h"
 #include "avformat.h"
 #include "mpegts.h"
+#include "adts.h"
 
 /* write DVB SI sections */
 
@@ -177,6 +178,7 @@ typedef struct MpegTSWriteStream {
     int64_t payload_pts;
     int64_t payload_dts;
     uint8_t payload[DEFAULT_PES_PAYLOAD_SIZE];
+    ADTSContext *adts;
 } MpegTSWriteStream;
 
 static void mpegts_write_pat(AVFormatContext *s)
@@ -427,6 +429,15 @@ static int mpegts_write_header(AVFormatContext *s)
             service->pcr_pid = ts_st->pid;
             pcr_st = st;
         }
+        if (st->codec->codec_id == CODEC_ID_AAC &&
+            st->codec->extradata_size > 0) {
+            ts_st->adts = av_mallocz(sizeof(*ts_st->adts));
+            if (!ts_st->adts)
+                return AVERROR_NOMEM;
+            if (ff_adts_decode_extradata(s, ts_st->adts, st->codec->extradata,
+                                         st->codec->extradata_size) < 0)
+                return -1;
+        }
     }
 
     /* if no video stream, use the first stream as PCR */
@@ -809,6 +820,32 @@ static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
             buf  = data;
             size = pkt->size+6;
         }
+    } else if (st->codec->codec_id == CODEC_ID_AAC) {
+        if (pkt->size < 2)
+            return -1;
+        if ((AV_RB16(pkt->data) & 0xfff0) != 0xfff0) {
+            ADTSContext *adts = ts_st->adts;
+            int new_size;
+            if (!adts) {
+                av_log(s, AV_LOG_ERROR, "aac bitstream not in adts format "
+                       "and extradata missing\n");
+                return -1;
+            }
+            new_size = ADTS_HEADER_SIZE+adts->pce_size+pkt->size;
+            if ((unsigned)new_size >= INT_MAX)
+                return -1;
+            data = av_malloc(new_size);
+            if (!data)
+                return AVERROR_NOMEM;
+            ff_adts_write_frame_header(adts, data, pkt->size, adts->pce_size);
+            if (adts->pce_size) {
+                memcpy(data+ADTS_HEADER_SIZE, adts->pce_data, adts->pce_size);
+                adts->pce_size = 0;
+            }
+            memcpy(data+ADTS_HEADER_SIZE+adts->pce_size, pkt->data, pkt->size);
+            buf = data;
+            size = new_size;
+        }
     }
 
     if (st->codec->codec_type != CODEC_TYPE_AUDIO) {

-- 
Libav/FFmpeg packaging



More information about the pkg-multimedia-commits mailing list