changeset 5787:667736f463b3 libavformat

In mpegts muxer, write adts header if aac bitstream does not contain it
author bcoudurier
date Tue, 09 Mar 2010 00:18:49 +0000
parents 7d670040187e
children 8539cb618ae7
files Makefile adts.h adtsenc.c mpegtsenc.c
diffstat 4 files changed, 98 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Mon Mar 08 23:59:05 2010 +0000
+++ b/Makefile	Tue Mar 09 00:18:49 2010 +0000
@@ -126,7 +126,7 @@
 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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/adts.h	Tue Mar 09 00:18:49 2010 +0000
@@ -0,0 +1,45 @@
+/*
+ * ADTS muxer.
+ * Copyright (c) 2006 Baptiste Coudurier <baptiste.coudurier@smartjog.com>
+ *                    Mans Rullgard <mans@mansr.com>
+ *
+ * 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
+ */
+
+#ifndef AVFORMAT_ADTS_H
+#define AVFORMAT_ADTS_H
+
+#include "avformat.h"
+#include "libavcodec/mpeg4audio.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;
+
+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_ADTS_H */
--- a/adtsenc.c	Mon Mar 08 23:59:05 2010 +0000
+++ b/adtsenc.c	Tue Mar 09 00:18:49 2010 +0000
@@ -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"
-
-#define ADTS_HEADER_SIZE 7
+#include "adts.h"
 
-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 @@
     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 @@
     /* 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 @@
 {
     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);
 
--- a/mpegtsenc.c	Mon Mar 08 23:59:05 2010 +0000
+++ b/mpegtsenc.c	Tue Mar 09 00:18:49 2010 +0000
@@ -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 @@
     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 @@
             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 @@
             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) {