[FFmpeg-devel] [PATCH 2/2] v3 - SCTE-35 support in hlsenc
Carlos Fernandez Sanz
carlos at ccextractor.org
Wed Aug 3 22:36:34 EEST 2016
From: carlos <carlos at ccextractor.org>
Signed-off-by: carlos <carlos at ccextractor.org>
---
libavformat/Makefile | 2 +-
libavformat/hlsenc.c | 110 +++++++++---
libavformat/scte_35.c | 482 ++++++++++++++++++++++++++++++++++++++++++++++++++
libavformat/scte_35.h | 76 ++++++++
4 files changed, 646 insertions(+), 24 deletions(-)
create mode 100644 libavformat/scte_35.c
create mode 100644 libavformat/scte_35.h
diff --git a/libavformat/Makefile b/libavformat/Makefile
index e2cb474..7da9e67 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -204,7 +204,7 @@ OBJS-$(CONFIG_HDS_MUXER) += hdsenc.o
OBJS-$(CONFIG_HEVC_DEMUXER) += hevcdec.o rawdec.o
OBJS-$(CONFIG_HEVC_MUXER) += rawenc.o
OBJS-$(CONFIG_HLS_DEMUXER) += hls.o
-OBJS-$(CONFIG_HLS_MUXER) += hlsenc.o
+OBJS-$(CONFIG_HLS_MUXER) += hlsenc.o scte_35.o
OBJS-$(CONFIG_HNM_DEMUXER) += hnm.o
OBJS-$(CONFIG_ICO_DEMUXER) += icodec.o
OBJS-$(CONFIG_ICO_MUXER) += icoenc.o
diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index 5dc518d..94e058e 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -38,6 +38,7 @@
#include "avio_internal.h"
#include "internal.h"
#include "os_support.h"
+#include "scte_35.h"
#define KEYSIZE 16
#define LINE_BUFFER_SIZE 1024
@@ -48,6 +49,10 @@ typedef struct HLSSegment {
double duration; /* in seconds */
int64_t pos;
int64_t size;
+ struct scte_35_event *event;
+ int out;
+ int adv_count;
+ int64_t start_pts;
char key_uri[LINE_BUFFER_SIZE + 1];
char iv_string[KEYSIZE*2 + 1];
@@ -89,6 +94,8 @@ typedef struct HLSContext {
uint32_t flags; // enum HLSFlags
uint32_t pl_type; // enum PlaylistType
char *segment_filename;
+ char *adv_filename;
+ char *adv_subfilename;
int use_localtime; ///< flag to expand filename with localtime
int use_localtime_mkdir;///< flag to mkdir dirname in timebased filename
@@ -104,6 +111,7 @@ typedef struct HLSContext {
int nb_entries;
int discontinuity_set;
+ struct scte_35_interface *scte_iface;
HLSSegment *segments;
HLSSegment *last_segment;
HLSSegment *old_segments;
@@ -203,6 +211,8 @@ static int hls_delete_old_segments(HLSContext *hls) {
av_freep(&path);
previous_segment = segment;
segment = previous_segment->next;
+ if (hls->scte_iface)
+ hls->scte_iface->unref_scte35_event(&previous_segment->event);
av_free(previous_segment);
}
@@ -314,8 +324,8 @@ static int hls_mux_init(AVFormatContext *s)
}
/* Create a new segment and append it to the segment list */
-static int hls_append_segment(struct AVFormatContext *s, HLSContext *hls, double duration,
- int64_t pos, int64_t size)
+static int hls_append_segment(struct AVFormatContext *s, HLSContext *hls, double duration, int64_t pos,
+ int64_t start_pts, struct scte_35_event *event, int64_t size)
{
HLSSegment *en = av_malloc(sizeof(*en));
char *tmp, *p;
@@ -349,11 +359,25 @@ static int hls_append_segment(struct AVFormatContext *s, HLSContext *hls, double
else
en->sub_filename[0] = '\0';
- en->duration = duration;
- en->pos = pos;
- en->size = size;
+ en->duration = duration;
+ en->pos = pos;
+ en->event = event;
+ en->size = size;
+ en->start_pts = start_pts;
en->next = NULL;
+ if (hls->scte_iface) {
+ if (hls->scte_iface->event_out == EVENT_OUT_CONT) {
+ en->adv_count = hls->scte_iface->adv_count;
+ hls->scte_iface->adv_count++;
+ en->out = hls->scte_iface->event_out;
+ } else {
+ hls->scte_iface->adv_count = 0;
+ en->out = hls->scte_iface->event_out;
+ }
+ }
+
+
if (hls->key_info_file) {
av_strlcpy(en->key_uri, hls->key_uri, sizeof(en->key_uri));
av_strlcpy(en->iv_string, hls->iv_string, sizeof(en->iv_string));
@@ -475,9 +499,23 @@ static int hls_window(AVFormatContext *s, int last)
if (hls->flags & HLS_SINGLE_FILE)
avio_printf(out, "#EXT-X-BYTERANGE:%"PRIi64"@%"PRIi64"\n",
en->size, en->pos);
- if (hls->baseurl)
- avio_printf(out, "%s", hls->baseurl);
- avio_printf(out, "%s\n", en->filename);
+ if (hls->scte_iface && (en->event || en->out) ) {
+ char *str;
+ char fname[1024] = "";
+ if (hls->adv_filename) {
+ str = hls->scte_iface->get_hls_string(hls->scte_iface, en->event, hls->adv_filename, en->out, en->adv_count, en->start_pts);
+ } else {
+ if (hls->baseurl)
+ strncat(fname, hls->baseurl, 1024);
+ strncat(fname, en->filename, 1024);
+ str = hls->scte_iface->get_hls_string(hls->scte_iface, en->event, fname, en->out, -1, en->start_pts);
+ }
+ avio_printf(out, "%s", str);
+ } else {
+ if (hls->baseurl)
+ avio_printf(out, "%s", hls->baseurl);
+ avio_printf(out, "%s\n", en->filename);
+ }
}
if (last && (hls->flags & HLS_OMIT_ENDLIST)==0)
@@ -502,9 +540,15 @@ static int hls_window(AVFormatContext *s, int last)
if (hls->flags & HLS_SINGLE_FILE)
avio_printf(sub_out, "#EXT-X-BYTERANGE:%"PRIi64"@%"PRIi64"\n",
en->size, en->pos);
- if (hls->baseurl)
- avio_printf(sub_out, "%s", hls->baseurl);
- avio_printf(sub_out, "%s\n", en->sub_filename);
+ if (hls->scte_iface && (en->event || en->out) ) {
+ char *str = hls->scte_iface->get_hls_string(hls->scte_iface, en->event, hls->adv_subfilename, en->out, en->adv_count, en->pos);
+ avio_printf(out, "%s", str);
+ } else {
+ if (hls->baseurl)
+ avio_printf(out, "%s", hls->baseurl);
+ avio_printf(sub_out, "%s\n", en->sub_filename);
+ }
+
}
if (last)
@@ -645,6 +689,7 @@ static int hls_write_header(AVFormatContext *s)
AVDictionary *options = NULL;
int basename_size;
int vtt_basename_size;
+ int ts_index = 0;
hls->sequence = hls->start_sequence;
hls->recording_time = hls->time * AV_TIME_BASE;
@@ -761,19 +806,21 @@ static int hls_write_header(AVFormatContext *s)
goto fail;
}
//av_assert0(s->nb_streams == hls->avf->nb_streams);
- for (i = 0; i < s->nb_streams; i++) {
+ for (ts_index = 0, i = 0; i < s->nb_streams; i++) {
AVStream *inner_st;
AVStream *outer_st = s->streams[i];
- if (outer_st->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE)
- inner_st = hls->avf->streams[i];
- else if (hls->vtt_avf)
+ if (hls->vtt_avf && outer_st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
inner_st = hls->vtt_avf->streams[0];
- else {
- /* We have a subtitle stream, when the user does not want one */
- inner_st = NULL;
+ avpriv_set_pts_info(outer_st, inner_st->pts_wrap_bits, inner_st->time_base.num, inner_st->time_base.den);
+ } else if (outer_st->codecpar->codec_type == AVMEDIA_TYPE_DATA) {
+ inner_st = hls->avf->streams[ts_index];
+ hls->scte_iface = ff_alloc_scte35_parser(hls, outer_st->time_base);
continue;
+ } else {
+ inner_st = hls->avf->streams[ts_index];
+ avpriv_set_pts_info(outer_st, inner_st->pts_wrap_bits, inner_st->time_base.num, inner_st->time_base.den);
+ ts_index++;
}
- avpriv_set_pts_info(outer_st, inner_st->pts_wrap_bits, inner_st->time_base.num, inner_st->time_base.den);
}
fail:
@@ -799,7 +846,12 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
int is_ref_pkt = 1;
int ret, can_split = 1;
int stream_index = 0;
+ struct scte_35_event *event = NULL;
+ if (st->codecpar->codec_id == AV_CODEC_ID_SCTE_35) {
+ ret = ff_parse_scte35_pkt(hls->scte_iface, pkt);
+ return ret;
+ }
if( st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE ) {
oc = hls->vtt_avf;
stream_index = 0;
@@ -824,14 +876,24 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
hls->duration = (double)(pkt->pts - hls->end_pts)
* st->time_base.num / st->time_base.den;
- if (can_split && av_compare_ts(pkt->pts - hls->start_pts, st->time_base,
- end_pts, AV_TIME_BASE_Q) >= 0) {
+ if (hls->scte_iface)
+ hls->scte_iface->get_event_pts(hls->scte_iface, pkt->pts * st->time_base.num / st->time_base.den);
+
+
+ if (can_split && (( av_compare_ts(pkt->pts - hls->start_pts, st->time_base,
+ end_pts, AV_TIME_BASE_Q) >= 0) ||
+ (hls->scte_iface && hls->scte_iface->event_out == EVENT_OUT)) ) {
int64_t new_start_pos;
av_write_frame(oc, NULL); /* Flush any buffered data */
new_start_pos = avio_tell(hls->avf->pb);
hls->size = new_start_pos - hls->start_pos;
- ret = hls_append_segment(s, hls, hls->duration, hls->start_pos, hls->size);
+ if (hls->scte_iface) {
+ event = hls->scte_iface->get_event_cache(hls->scte_iface);
+ if (event)
+ hls->scte_iface->ref_scte35_event(event);
+ }
+ ret = hls_append_segment(s, hls, hls->duration, hls->start_pos, pkt->pts, event, hls->size);
hls->start_pos = new_start_pos;
if (ret < 0)
return ret;
@@ -878,7 +940,7 @@ static int hls_write_trailer(struct AVFormatContext *s)
if (oc->pb) {
hls->size = avio_tell(hls->avf->pb) - hls->start_pos;
ff_format_io_close(s, &oc->pb);
- hls_append_segment(s, hls, hls->duration, hls->start_pos, hls->size);
+ hls_append_segment(s, hls, hls->duration, hls->start_pos, hls->end_pts, NULL, hls->size);
}
if (vtt_oc) {
@@ -899,6 +961,7 @@ static int hls_write_trailer(struct AVFormatContext *s)
hls->avf = NULL;
hls_window(s, 1);
+ ff_delete_scte35_parser(hls->scte_iface);
hls_free_segments(hls->segments);
hls_free_segments(hls->old_segments);
return 0;
@@ -951,6 +1014,7 @@ AVOutputFormat ff_hls_muxer = {
.audio_codec = AV_CODEC_ID_AAC,
.video_codec = AV_CODEC_ID_H264,
.subtitle_codec = AV_CODEC_ID_WEBVTT,
+ .data_codec = AV_CODEC_ID_SCTE_35,
.flags = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH,
.write_header = hls_write_header,
.write_packet = hls_write_packet,
diff --git a/libavformat/scte_35.c b/libavformat/scte_35.c
new file mode 100644
index 0000000..4962de9
--- /dev/null
+++ b/libavformat/scte_35.c
@@ -0,0 +1,482 @@
+/*
+ * SCTE 35 decoder
+ * Copyright (c) 2016 Carlos Fernandez
+ *
+ * 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
+ */
+/*
+ * Refrence Material Used
+ *
+ * ANSI/SCTE 35 2013 ( Digital Program Insertion Cueing Message for Cable )
+ *
+ * SCTE 67 2014 (Recommended Practice for SCTE 35
+ * Digital Program Insertion Cueing Message for Cable )
+ */
+
+
+
+#include "libavcodec/bytestream.h"
+#include "libavcodec/avcodec.h"
+#include "libavcodec/get_bits.h"
+#include "libavutil/buffer_internal.h"
+#include "libavutil/base64.h"
+#include "scte_35.h"
+
+#define SCTE_CMD_NULL 0x00
+#define SCTE_CMD_SCHEDULE 0x04
+#define SCTE_CMD_INSERT 0x05
+#define SCTE_CMD_SIGNAL 0x06
+#define SCTE_CMD_BANDWIDTH_RESERVATION 0x07
+
+
+static char* get_hls_string(struct scte_35_interface *iface, struct scte_35_event *event,
+ const char *filename, int out_state, int seg_count, int64_t pos)
+{
+ int ret;
+ av_bprint_clear(&iface->avbstr);
+ if (out_state == EVENT_IN ) {
+ av_bprintf(&iface->avbstr, "#EXT-OATCLS-SCTE35:%s\n", iface->pkt_base64);
+ av_bprintf(&iface->avbstr, "#EXT-X-CUE-IN\n");
+ av_bprintf(&iface->avbstr, "#EXT-X-DISCONTINUITY\n");
+ } else if (out_state == EVENT_OUT) {
+ if (event)
+ {
+ av_bprintf(&iface->avbstr, "#EXT-OATCLS-SCTE35:%s\n", iface->pkt_base64);
+ if(event->duration != AV_NOPTS_VALUE) {
+ int64_t dur = ceil(((double)event->duration * iface->timebase.num) /iface->timebase.den);
+ av_bprintf(&iface->avbstr, "#EXT-X-CUE-OUT:%"PRIu64"\n", dur);
+ } else {
+ av_bprintf(&iface->avbstr, "#EXT-X-CUE-OUT\n");
+ }
+ av_bprintf(&iface->avbstr, "#EXT-X-DISCONTINUITY\n");
+ }
+ } else if (out_state == EVENT_OUT_CONT) {
+ if(event && event->duration != AV_NOPTS_VALUE) {
+ int64_t dur = ceil(((double)event->duration * iface->timebase.num) /iface->timebase.den);
+ int64_t elapsed_time = ceil(((double)pos * iface->timebase.num) /iface->timebase.den) - event->out_pts;
+ av_bprintf(&iface->avbstr, "#EXT-X-CUE-OUT-CONT:ElapsedTime=%"PRIu64",Duration=%"PRIu64",SCTE35=%s\n",
+ elapsed_time, dur, iface->pkt_base64);
+ } else {
+ av_bprintf(&iface->avbstr, "#EXT-X-CUE-OUT-CONT:SCTE35=%s\n", iface->pkt_base64);
+ }
+ }
+ if (seg_count >= 0)
+ av_bprintf(&iface->avbstr, filename, seg_count);
+ else
+ av_bprintf(&iface->avbstr, "%s", filename);
+ av_bprintf(&iface->avbstr, "\n");
+
+ ret = av_bprint_is_complete(&iface->avbstr);
+ if( ret == 0) {
+ av_log(NULL, AV_LOG_DEBUG, "Out of Memory");
+ return NULL;
+ }
+
+ av_log(iface->parent, AV_LOG_DEBUG, "%s", iface->avbstr.str);
+ return iface->avbstr.str;
+}
+
+static struct scte_35_event* alloc_scte35_event(int id)
+{
+ struct scte_35_event* event = av_malloc(sizeof(struct scte_35_event));
+ event->id = id;
+ event->in_pts = AV_NOPTS_VALUE;
+ event->nearest_in_pts = AV_NOPTS_VALUE;
+ event->out_pts = AV_NOPTS_VALUE;
+ event->lock = 0;
+ event->cancel = 1;
+ event->next = NULL;
+ event->prev = NULL;
+ return event;
+}
+
+static void ref_scte35_event(struct scte_35_event *event)
+{
+ event->ref_count++;
+}
+static void unref_scte35_event(struct scte_35_event **event)
+{
+ if(!(*event))
+ return;
+ if(!(*event)->ref_count) {
+ av_freep(event);
+ } else {
+ (*event)->ref_count--;
+ }
+}
+
+static void unlink_scte35_event(struct scte_35_interface *iface, struct scte_35_event *event)
+{
+ if (!event)
+ return;
+ if (!event->prev)
+ iface->event_list = event->next;
+ else
+ event->prev->next = event->next;
+ if(event->next)
+ event->next->prev = event->prev;
+ unref_scte35_event(&event);
+}
+
+static struct scte_35_event* get_event_id(struct scte_35_interface *iface, int id)
+{
+ struct scte_35_event *event = iface->event_list;
+ struct scte_35_event *pevent = NULL;
+
+ while(event) {
+
+ if(event->id == id)
+ break;
+ pevent = event;
+ event = event->next;
+ }
+ if (!event) {
+ event = alloc_scte35_event(id);
+ if (pevent)
+ pevent->next = event;
+ else
+ iface->event_list = event;
+ }
+
+ return event;
+}
+/**
+ * save the parsed time in ctx pts_time
+ @return length of buffer consumed
+*/
+static int parse_splice_time(struct scte_35_interface *iface, const uint8_t *buf, uint64_t *pts, int64_t pts_adjust)
+{
+ GetBitContext gb;
+ int ret;
+ init_get_bits(&gb, buf, 40);
+ /* is time specified */
+ ret = get_bits(&gb, 1);
+ if(ret) {
+ skip_bits(&gb, 6);
+ *pts = get_bits64(&gb,33) + pts_adjust;
+ return 5;
+ } else {
+ skip_bits(&gb, 7);
+ return 1;
+ }
+}
+static int parse_schedule_cmd(struct scte_35_interface *iface, const uint8_t *buf)
+{
+ const uint8_t *sbuf = buf;
+ av_log(iface->parent, AV_LOG_DEBUG, "Schedule cmd\n");
+ return buf - sbuf;
+}
+/**
+ @return length of buffer used
+ */
+static int parse_insert_cmd(struct scte_35_interface *iface,
+ const uint8_t *buf,const int len, int64_t pts_adjust, int64_t current_pts)
+{
+ GetBitContext gb;
+ int ret;
+ const uint8_t *sbuf = buf;
+ int program_splice_flag;
+ int duration_flag;
+ int splice_immediate_flag;
+ int component_tag;
+ int auto_return;
+ uint16_t u_program_id;
+ uint8_t avail_num;
+ uint8_t avail_expect;
+ int inout;
+ int event_id;
+ struct scte_35_event *event;
+ char buffer[128];
+
+
+ av_log(iface->parent, AV_LOG_DEBUG, "%s Insert cmd\n", buffer);
+ event_id = AV_RB32(buf);
+ av_log(iface->parent, AV_LOG_DEBUG, "event_id = %x\n", event_id);
+ event = get_event_id(iface, event_id);
+ buf +=4;
+ event->cancel = *buf & 0x80;
+ av_log(iface->parent, AV_LOG_DEBUG, "splice_event_cancel_indicator = %d\n", event->cancel );
+ buf++;
+
+ if (!event->cancel) {
+ init_get_bits(&gb, buf, 8);
+ inout = get_bits(&gb, 1);
+ program_splice_flag = get_bits(&gb, 1);
+ duration_flag = get_bits(&gb, 1);
+ splice_immediate_flag = get_bits(&gb, 1);
+ skip_bits(&gb, 4);
+
+ } else {
+ /* Delete event only if its not already started */
+ if (!event->lock) {
+ unlink_scte35_event(iface, event);
+ }
+ }
+ buf++;
+
+
+ av_log(iface->parent, AV_LOG_DEBUG, "out_of_network_indicator = %d\n", inout);
+ av_log(iface->parent, AV_LOG_DEBUG, "program_splice_flag = %d\n", program_splice_flag);
+ av_log(iface->parent, AV_LOG_DEBUG, "duration_flag = %d\n", duration_flag);
+ av_log(iface->parent, AV_LOG_DEBUG, "splice_immediate_flag = %d\n", splice_immediate_flag);
+
+ if (program_splice_flag && !splice_immediate_flag) {
+ if(inout) {
+ ret = parse_splice_time(iface, buf, &event->out_pts, pts_adjust);
+ event->out_pts = event->out_pts * iface->timebase.num / iface->timebase.den;
+ } else {
+ ret = parse_splice_time(iface, buf, &event->in_pts, pts_adjust);
+ event->in_pts = event->in_pts * iface->timebase.num / iface->timebase.den;
+ }
+
+ buf += ret;
+ } else if (program_splice_flag && splice_immediate_flag) {
+ if(inout)
+ event->out_pts = current_pts * iface->timebase.num / iface->timebase.den;
+ else
+ event->in_pts = current_pts * iface->timebase.num / iface->timebase.den;
+ }
+ if ( program_splice_flag == 0) {
+ int comp_cnt = *buf++;
+ int i;
+ av_log(iface->parent, AV_LOG_DEBUG, "component_count = %d\n", comp_cnt);
+ for ( i = 0; i < comp_cnt; i++) {
+ component_tag = *buf++;
+ av_log(iface->parent, AV_LOG_DEBUG, "component_tag = %d\n", component_tag);
+ if (splice_immediate_flag) {
+ if(inout)
+ ret = parse_splice_time(iface, buf, &event->in_pts, pts_adjust);
+ else
+ ret = parse_splice_time(iface, buf, &event->out_pts, pts_adjust);
+ buf += ret;
+ }
+ }
+ }
+ if ( duration_flag ) {
+ init_get_bits(&gb, buf, 40);
+ auto_return = get_bits(&gb, 1);
+ av_log(iface->parent, AV_LOG_DEBUG, "autoreturn = %d\n", auto_return);
+ skip_bits(&gb, 6);
+ event->duration = get_bits64(&gb,33) + pts_adjust;
+ buf += 5;
+ }
+ u_program_id = AV_RB16(buf);
+ av_log(iface->parent, AV_LOG_DEBUG, "u_program_id = %hd\n", u_program_id);
+ buf += 2;
+ avail_num = *buf++;
+ av_log(iface->parent, AV_LOG_DEBUG, "avail_num = %hhd\n", avail_num);
+ avail_expect = *buf++;
+ av_log(iface->parent, AV_LOG_DEBUG, "avail_expect = %hhd\n", avail_expect);
+
+ return buf - sbuf;
+}
+static int parse_time_signal_cmd(struct scte_35_interface *iface, const uint8_t *buf)
+{
+ const uint8_t *sbuf = buf;
+ av_log(iface->parent, AV_LOG_DEBUG, "Time Signal cmd\n");
+ return buf - sbuf;
+}
+static int parse_bandwidth_reservation_cmd(struct scte_35_interface *iface, const uint8_t *buf)
+{
+ const uint8_t *sbuf = buf;
+ av_log(iface->parent, AV_LOG_DEBUG, "Band Width reservation cmd\n");
+ return buf - sbuf;
+}
+
+int ff_parse_scte35_pkt(struct scte_35_interface *iface, const AVPacket *avpkt)
+{
+ const uint8_t *buf = avpkt->data;
+ int section_length;
+ int cmd_length;
+ uint8_t cmd_type;
+ int16_t tier;
+ GetBitContext gb;
+ int ret;
+ int64_t pts_adjust;
+
+ if (!buf)
+ return AVERROR_EOF;
+
+
+ /* check table id */
+ if (*buf != 0xfc)
+ av_log(iface->parent, AV_LOG_ERROR, "Invalid SCTE packet\n");
+
+
+ init_get_bits(&gb, buf + 1, 104);
+
+ /* section_syntax_indicator should be 0 */
+ ret = get_bits(&gb,1);
+ if (ret)
+ av_log(iface->parent, AV_LOG_DEBUG, "Section indicator should be 0, since MPEG short sections are to be used.\n");
+
+ /* private indicator */
+ ret = get_bits(&gb,1);
+ if (ret)
+ av_log(iface->parent, AV_LOG_DEBUG, "corrupt packet\n");
+
+ skip_bits(&gb,2);
+
+ /* section length may be there */
+ section_length = get_bits(&gb,12);
+ if( section_length > 4093)
+ if(ret) {
+ av_log(iface->parent, AV_LOG_ERROR, "Invalid length of section\n");
+ return AVERROR_INVALIDDATA;
+ }
+
+ av_base64_encode( iface->pkt_base64, AV_BASE64_SIZE(section_length + 3), buf, section_length + 3);
+
+ /* protocol version */
+ skip_bits(&gb,8);
+
+ ret = get_bits(&gb,1);
+ if(ret) {
+ av_log(iface->parent, AV_LOG_ERROR, "Encrytion not yet supported\n");
+ return AVERROR_PATCHWELCOME;
+ }
+ /* encryption algo */
+ skip_bits(&gb,6);
+
+ pts_adjust = get_bits64(&gb, 33);
+
+ /* cw_index: used in encryption */
+ skip_bits(&gb,8);
+
+
+ /* tier */
+ tier = get_bits(&gb,12);
+ if( (tier & 0xfff) == 0xfff)
+ tier = -1;
+
+ cmd_length = get_bits(&gb,12);
+ if(cmd_length == 0xfff ) {
+ /* Setting max limit to cmd_len so it does not cross memory barrier */
+ cmd_length = section_length - 17;
+ } else if ( cmd_length != 0xfff && ( cmd_length > (section_length - 17) ) ) {
+ av_log(iface->parent, AV_LOG_ERROR, "Command length %d invalid\n", cmd_length);
+ return AVERROR_INVALIDDATA;
+ }
+
+ cmd_type = get_bits(&gb,8);
+ switch(cmd_type) {
+ case SCTE_CMD_NULL:
+ break;
+ case SCTE_CMD_SCHEDULE:
+ ret = parse_schedule_cmd(iface, buf + 14);
+ break;
+ case SCTE_CMD_INSERT:
+ ret = parse_insert_cmd(iface, buf + 14, cmd_length, pts_adjust, avpkt->pts);
+ break;
+ case SCTE_CMD_SIGNAL:
+ ret = parse_time_signal_cmd(iface, buf + 14);
+ break;
+ case SCTE_CMD_BANDWIDTH_RESERVATION:
+ ret = parse_bandwidth_reservation_cmd(iface, buf + 14);
+ break;
+ default:
+ break;
+ /* reserved yet */
+ }
+ if(ret < 0)
+ goto fail;
+ buf += ret;
+
+fail:
+ return ret;
+}
+static struct scte_35_event* get_event_ciel_out(struct scte_35_interface *iface, uint64_t pts)
+{
+ struct scte_35_event *event = iface->event_list;
+ while(event) {
+ if(!event->lock && event->out_pts < pts) {
+ iface->event_out = EVENT_OUT;
+ break;
+ }
+ event = event->next;
+ }
+ return event;
+}
+static struct scte_35_event* get_event_floor_in(struct scte_35_interface *iface, uint64_t pts)
+{
+ struct scte_35_event *event = iface->event_list;
+ while(event) {
+ if(event->lock && event->in_pts != AV_NOPTS_VALUE && event->in_pts < pts &&
+ (event->nearest_in_pts == AV_NOPTS_VALUE || pts <= event->nearest_in_pts) ) {
+ event->nearest_in_pts = pts;
+ iface->event_out = EVENT_IN;
+ break;
+ }
+ event = event->next;
+ }
+ return event;
+}
+
+static struct scte_35_event* get_event_pts(struct scte_35_interface *iface, uint64_t pts)
+{
+ struct scte_35_event *event = NULL;
+ if(iface->event_out == EVENT_NONE) {
+ event = get_event_ciel_out(iface, pts);
+ if(event)
+ event->lock = 1;
+ } else {
+ event = get_event_floor_in(iface, pts);
+ unlink_scte35_event(iface, event);
+ }
+ if (event)
+ iface->cache_event = event;
+
+ return event;
+}
+
+static struct scte_35_event* get_event_cache(struct scte_35_interface *iface)
+{
+
+ struct scte_35_event* event = iface->cache_event;
+ if (iface->prev_event_state == EVENT_IN)
+ iface->event_out = EVENT_NONE;
+ else if (iface->prev_event_state == EVENT_OUT)
+ iface->event_out = EVENT_OUT_CONT;
+
+ if(iface->event_out == EVENT_NONE)
+ iface->cache_event = NULL;
+
+ iface->prev_event_state = iface->event_out;
+ return event;
+}
+
+struct scte_35_interface* ff_alloc_scte35_parser(void *parent, AVRational timebase)
+{
+ struct scte_35_interface* iface = av_mallocz(sizeof(struct scte_35_interface));
+
+ iface->parent = parent;
+ iface->timebase = timebase;
+ iface->get_event_pts = get_event_pts;
+ iface->get_event_cache = get_event_cache;
+ av_bprint_init(&iface->avbstr, 0, AV_BPRINT_SIZE_UNLIMITED);
+ iface->get_hls_string = get_hls_string;
+ iface->unref_scte35_event = unref_scte35_event;
+ iface->ref_scte35_event = ref_scte35_event;
+ iface->event_out = EVENT_NONE;
+ iface->prev_event_state = EVENT_NONE;
+ return iface;
+}
+
+void ff_delete_scte35_parser(struct scte_35_interface* iface)
+{
+ av_freep(&iface);
+}
diff --git a/libavformat/scte_35.h b/libavformat/scte_35.h
new file mode 100644
index 0000000..35a84c9
--- /dev/null
+++ b/libavformat/scte_35.h
@@ -0,0 +1,76 @@
+/*
+ * SCTE-35 parser
+ * Copyright (c) 2016 Carlos Fernandez
+ *
+ * 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_SCTE_35_H
+#define AVFORMAT_SCTE_35_H
+
+#include "libavutil/bprint.h"
+
+struct scte_35_event {
+ int32_t id;
+ uint64_t in_pts;
+ uint64_t nearest_in_pts;
+ uint64_t out_pts;
+ int64_t duration;
+ int64_t start_pos;
+ int cancel;
+ /*if advertisement have already started cancel command can't delete advertisement */
+ volatile int lock;
+ volatile int ref_count;
+ struct scte_35_event *next;
+ struct scte_35_event *prev;
+};
+struct scte_35_interface {
+ struct scte_35_event *event_list;
+ char adv_filename[1024];
+ char filename[1024];
+ int event_out;
+ AVRational timebase;
+ int adv_count;
+ struct scte_35_event *cache_event;
+ int prev_event_state;
+ //TODO use AV_BASE64_SIZE to dynamically allocate the array
+ char pkt_base64[1024];
+ /* keep context of its parent for log */
+ void *parent;
+
+ struct scte_35_event* (*get_event_pts)(struct scte_35_interface *iface, uint64_t pts);
+ struct scte_35_event* (*get_event_cache)(struct scte_35_interface *iface);
+ /* general purpose str */
+ AVBPrint avbstr;
+ char* (*get_hls_string)(struct scte_35_interface *iface, struct scte_35_event *event,
+ const char *adv_filename, int out_state, int seg_count, int64_t pos);
+
+ void (*unref_scte35_event)(struct scte_35_event **event);
+ void (*ref_scte35_event)(struct scte_35_event *event);
+};
+
+enum event_state {
+ EVENT_NONE,
+ EVENT_IN,
+ EVENT_OUT,
+ EVENT_OUT_CONT,
+};
+
+int ff_parse_scte35_pkt(struct scte_35_interface *iface, const AVPacket *avpkt);
+
+struct scte_35_interface* ff_alloc_scte35_parser(void *parent, AVRational timebase);
+void ff_delete_scte35_parser(struct scte_35_interface* iface);
+#endif
--
2.7.4
More information about the ffmpeg-devel
mailing list