From 2e66214e2a9529aaf281fe7c790ca4c009f97388 Mon Sep 17 00:00:00 2001
From: Steven Liu <lingjiujianke@gmail.com>
Date: Tue, 28 Jun 2016 21:15:38 +0800
Subject: [PATCH] Automatically inserted bitstream filter 'h264_mp4toannexb' into hlsenc


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

diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index a9fa5d8..dcf2002 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -267,7 +267,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;
 
@@ -633,7 +633,7 @@ fail:
     return err;
 }
 
-static int hls_write_header(AVFormatContext *s)
+static int hls_init(AVFormatContext *s)
 {
     HLSContext *hls = s->priv_data;
     int ret, i;
@@ -903,6 +903,26 @@ 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;
+    if (oc->oformat->check_bitstream) {
+        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[] = {
@@ -950,8 +970,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_init,
     .write_packet   = hls_write_packet,
     .write_trailer  = hls_write_trailer,
+    .check_bitstream = hls_check_bitstream,
     .priv_class     = &hls_class,
 };
-- 
1.7.1

