[FFmpeg-devel] [PATCH] AAC sequence header data setup if stream copy

Maksym Veremeyenko verem at m1stereo.tv
Wed Nov 18 16:04:06 CET 2015


Hi,

FLV remuxed from MPEG-TS stream does not contains *AAC sequence header* 
as result no audio with some RTMP servers like *crtmpserver* and YouTube.

I am not a first who faced with such problem:
http://lists.ffmpeg.org/pipermail/ffmpeg-devel/2014-May/157791.html
http://lists.ffmpeg.org/pipermail/ffmpeg-devel/2014-June/158342.html

Proposed solution differ from *Tudor SUCIU* a bit. It use the same idea 
but implementation was taken from *ff_aac_put_audio_specific_config*.

Attached patch implement setting *AAC sequence header* in two ways. 
First way is just to set given bytes into that header:

ffmpeg -re -f mpegts -i /usr/local/src/2015-11-15/M1_award.ts -acodec 
copy -vcodec copy -bsf:a aac_adtstoasc -aac_seq_header_data "1190" 
-flags +global_header -f flv rtmp://127.0.0.1/live/demo

11 90 is a two bytes of *AAC sequence header* that describe 
AAC/LC+48000Hz+stereo

another way is just build it from encoder parameters:

ffmpeg -re -f mpegts -i /usr/local/src/2015-11-15/M1_award.ts -acodec 
copy -vcodec copy -bsf:a aac_adtstoasc -flvflags aac_seq_header_detect 
-flags +global_header -f flv rtmp://127.0.0.1/live/demo

but it require copying *profile* value from decoder context in ffmpeg.c:

diff --git a/ffmpeg.c b/ffmpeg.c
index c4e9280..a976f61 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -2970,6 +2970,7 @@ static int transcode_init(void)
                  enc_ctx->audio_service_type = dec_ctx->audio_service_type;
                  enc_ctx->block_align        = dec_ctx->block_align;
                  enc_ctx->initial_padding    = dec_ctx->delay;
+                enc_ctx->profile            = dec_ctx->profile;
  #if FF_API_AUDIOENC_DELAY
                  enc_ctx->delay              = dec_ctx->delay;
  #endif

both methods makes possible to work with *crtmpserver* and YouTube by 
simple remuxing streams for publishing

-- 
Maksym Veremeyenko
-------------- next part --------------
From afdc10c4f496d1e72a3b955513cf6891767d731a Mon Sep 17 00:00:00 2001
From: Maksym Veremeyenko <verem at m1.tv>
Date: Tue, 17 Nov 2015 20:02:22 +0200
Subject: [PATCH] AAC sequence header data setup if stream copy

---
 ffmpeg.c             |    1 +
 libavformat/flvenc.c |   53 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+), 0 deletions(-)

diff --git a/ffmpeg.c b/ffmpeg.c
index c4e9280..a976f61 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -2970,6 +2970,7 @@ static int transcode_init(void)
                 enc_ctx->audio_service_type = dec_ctx->audio_service_type;
                 enc_ctx->block_align        = dec_ctx->block_align;
                 enc_ctx->initial_padding    = dec_ctx->delay;
+                enc_ctx->profile            = dec_ctx->profile;
 #if FF_API_AUDIOENC_DELAY
                 enc_ctx->delay              = dec_ctx->delay;
 #endif
diff --git a/libavformat/flvenc.c b/libavformat/flvenc.c
index e217ba8..6380fd7 100644
--- a/libavformat/flvenc.c
+++ b/libavformat/flvenc.c
@@ -28,6 +28,9 @@
 #include "flv.h"
 #include "internal.h"
 #include "metadata.h"
+#include "libavutil/opt.h"
+#include "libavcodec/put_bits.h"
+#include "libavcodec/aacenctab.h"
 
 
 static const AVCodecTag flv_video_codec_ids[] = {
@@ -68,6 +71,10 @@ typedef struct FLVContext {
     AVCodecContext *video_enc;
     double framerate;
     AVCodecContext *data_enc;
+
+    uint8_t *aac_seq_header_data;
+    int aac_seq_header_length;
+    int flags;
 } FLVContext;
 
 typedef struct FLVStreamContext {
@@ -452,6 +459,37 @@ static int flv_write_header(AVFormatContext *s)
             if (enc->codec_id == AV_CODEC_ID_AAC) {
                 avio_w8(pb, get_audio_flags(s, enc));
                 avio_w8(pb, 0); // AAC sequence header
+                if (!enc->extradata_size && flv->aac_seq_header_data)
+                {
+                    avio_write(pb, flv->aac_seq_header_data, flv->aac_seq_header_length);
+
+                    av_log(s, AV_LOG_WARNING, "AAC sequence header length=%d\n", flv->aac_seq_header_length);
+                }
+                else if (!enc->extradata_size && !flv->aac_seq_header_data && flv->flags & 1)
+                {
+                    PutBitContext pbc;
+                    int samplerate_index;
+                    int channels = flv->audio_enc->channels - (flv->audio_enc->channels == 8 ? 1 : 0);
+                    uint8_t data[2];
+
+                    for (samplerate_index = 0; samplerate_index < 16; samplerate_index++)
+                        if (flv->audio_enc->sample_rate == mpeg4audio_sample_rates[samplerate_index])
+                            break;
+
+                    init_put_bits(&pbc, data, sizeof(data));
+                    put_bits(&pbc, 5, flv->audio_enc->profile + 1); //profile
+                    put_bits(&pbc, 4, samplerate_index); //sample rate index
+                    put_bits(&pbc, 4, channels);
+                    put_bits(&pbc, 1, 0); //frame length - 1024 samples
+                    put_bits(&pbc, 1, 0); //does not depend on core coder
+                    put_bits(&pbc, 1, 0); //is not extension
+                    flush_put_bits(&pbc);
+
+                    avio_w8(pb, data[0]);
+                    avio_w8(pb, data[1]);
+
+                    av_log(s, AV_LOG_WARNING, "AAC sequence header: %02x %02x.\n", data[0], data[1]);
+                }
                 avio_write(pb, enc->extradata, enc->extradata_size);
             } else {
                 avio_w8(pb, enc->codec_tag | FLV_FRAME_KEY); // flags
@@ -655,6 +693,20 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
     return pb->error;
 }
 
+static const AVOption options[] = {
+    { "flvflags", "FLV muxer flags", offsetof(FLVContext, flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "flvflags" },
+    { "aac_seq_header_detect", "Put AAC sequence header based on stream data", 0, AV_OPT_TYPE_CONST, {.i64 = 1}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "flvflags" },
+    { "aac_seq_header_data", "Put AAC sequence header data specified", offsetof(FLVContext, aac_seq_header_data), AV_OPT_TYPE_BINARY, {.str = NULL}, .flags = AV_OPT_FLAG_ENCODING_PARAM },
+    { NULL },
+};
+
+static const AVClass flv_muxer_class = {
+    .class_name = "flv muxer",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
 AVOutputFormat ff_flv_muxer = {
     .name           = "flv",
     .long_name      = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),
@@ -671,4 +723,5 @@ AVOutputFormat ff_flv_muxer = {
                       },
     .flags          = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS |
                       AVFMT_TS_NONSTRICT,
+    .priv_class     = &flv_muxer_class,
 };
-- 
1.7.7.6



More information about the ffmpeg-devel mailing list