[FFmpeg-devel] [PATCH] AAC sequence header data setup if stream copy
Maksym Veremeyenko
verem at m1stereo.tv
Wed Nov 18 20:56:05 CET 2015
On 18.11.2015 19:37, Michael Niedermayer wrote:
[...]
> the first field of the context must be a AVClass
updated patch attached
--
Maksym Veremeyenko
-------------- next part --------------
From 665751883fd515c753ada7bbf4d24c89a24f3d30 Mon Sep 17 00:00:00 2001
From: Maksym Veremeyenko <verem at m1.tv>
Date: Wed, 18 Nov 2015 23:44:08 +0200
Subject: [PATCH] AAC sequence header data setup if stream copy
---
ffmpeg.c | 1 +
libavformat/flvenc.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 55 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..cff130c 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[] = {
@@ -58,6 +61,7 @@ static const AVCodecTag flv_audio_codec_ids[] = {
};
typedef struct FLVContext {
+ AVClass *av_class;
int reserved;
int64_t duration_offset;
int64_t filesize_offset;
@@ -68,6 +72,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 +460,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 +694,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 +724,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