From 8fdd542ce4c357d38b4601e9a327f499c9a096f1 Mon Sep 17 00:00:00 2001 From: Baptiste Coudurier Date: Tue, 9 Mar 2010 00:18:49 +0000 Subject: [PATCH] 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 --- libavformat/Makefile | 2 +- libavformat/adts.h | 45 +++++++++++++++++++++++++++++++++++++++++ libavformat/adtsenc.c | 40 ++++++++++++++---------------------- libavformat/mpegtsenc.c | 37 +++++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 26 deletions(-) create mode 100644 libavformat/adts.h diff --git a/libavformat/Makefile b/libavformat/Makefile index eb710cc967..67a19e4e6a 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/adts.h b/libavformat/adts.h new file mode 100644 index 0000000000..1da57beff9 --- /dev/null +++ b/libavformat/adts.h @@ -0,0 +1,45 @@ +/* + * ADTS muxer. + * Copyright (c) 2006 Baptiste Coudurier + * Mans Rullgard + * + * 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 */ diff --git a/libavformat/adtsenc.c b/libavformat/adtsenc.c index 260c5932a3..ecc8dc40de 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 64e6fddcf0..b818964e21 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) { -- GitLab