From 4f0e3880f0564aa6c779ba6fb7fe4579a17aebdf Mon Sep 17 00:00:00 2001
From: Steven Liu <lingjiujianke@gmail.com>
Date: Wed, 13 Jul 2016 23:43:28 +0800
Subject: [PATCH] Automatically inserted bitstream filter 'h264_mp4toannexb'
 into hlsenc

Signed-off-by: LiuQi <liuqi@gosun.com>
---
 libavformat/hlsenc.c | 29 ++++++++++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)

diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index 5dc518d..c37c4b4 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -268,7 +268,7 @@ static int hls_encryption_start(AVFormatContext *s)
 static int hls_mux_init(AVFormatContext *s)
 {
     HLSContext *hls = s->priv_data;
-    AVFormatContext *oc;
+    AVFormatContext *oc = hls->avf;
     AVFormatContext *vtt_oc = NULL;
     int i, ret;
 
@@ -634,7 +634,7 @@ fail:
     return err;
 }
 
-static int hls_write_header(AVFormatContext *s)
+static int hls_write_header_init(AVFormatContext *s)
 {
     HLSContext *hls = s->priv_data;
     int ret, i;
@@ -904,6 +904,28 @@ static int hls_write_trailer(struct AVFormatContext *s)
     return 0;
 }
 
+static int hls_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
+{
+    HLSContext *hls = s->priv_data;
+    AVFormatContext *oc = hls->avf;
+    AVStream *st = s->streams[pkt->stream_index];
+
+    if (oc->oformat->check_bitstream && (st->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE)) {
+        int ret = oc->oformat->check_bitstream(oc, pkt);
+        if (ret == 1) {
+            AVStream *st = s->streams[pkt->stream_index];
+            AVStream *ost = oc->streams[pkt->stream_index];
+            st->internal->bsfcs = ost->internal->bsfcs;
+            st->internal->nb_bsfcs = ost->internal->nb_bsfcs;
+            ost->internal->bsfcs = NULL;
+            ost->internal->nb_bsfcs = 0;
+        }
+        return ret;
+    }
+    return 1;
+}
+
+
 #define OFFSET(x) offsetof(HLSContext, x)
 #define E AV_OPT_FLAG_ENCODING_PARAM
 static const AVOption options[] = {
@@ -952,8 +974,9 @@ AVOutputFormat ff_hls_muxer = {
     .video_codec    = AV_CODEC_ID_H264,
     .subtitle_codec = AV_CODEC_ID_WEBVTT,
     .flags          = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH,
-    .write_header   = hls_write_header,
+    .init           = hls_write_header_init,
     .write_packet   = hls_write_packet,
     .write_trailer  = hls_write_trailer,
+    .check_bitstream = hls_check_bitstream,
     .priv_class     = &hls_class,
 };
--
2.7.4 (Apple Git-66)

