On 12/29/2014 06:08 PM, Michael Niedermayer wrote:
> On Mon, Dec 29, 2014 at 01:31:18PM +0530, Anshul wrote:
>> On December 18, 2014 4:08:56 PM IST, Michael Niedermayer
>> <michae...@gmx.at> wrote:
>>> On Wed, Dec 17, 2014 at 11:53:10PM +0530, Anshul wrote:
>>>> Hi
>>>>
>>>> I was doing development of scte-35  message cue in ffmpeg.
>>>>
>>>> I have attached a draft patch.
>>>>
>>>> I would like to have some comments of this community.
>>>> -Anshul
>>>>  libavcodec/Makefile     |    2 ++
>>>>  libavcodec/allcodecs.c  |    2 ++
>>>>  libavcodec/avcodec.h    |    1 +
>>>>  libavcodec/codec_desc.c |    6 ++++++
>>>>  libavformat/mpegts.c    |   40
>>> +++++++++++++++++++++++++++++++++-------
>>>>  5 files changed, 44 insertions(+), 7 deletions(-)
>>>> 4b584484a54a78c130c4bbaa48c38551b3ca75fb 
>>> 0001-trying-muxing-demuxing-of-scte_35-message-cue.patch
>>>> From 2ff4edfdeae131df72ea19005eb6ad85d5332f7c Mon Sep 17 00:00:00
>>> 2001
>>>> From: Anshul Maheshwari <anshul.ffm...@gmail.com>
>>>> Date: Wed, 17 Dec 2014 22:13:10 +0530
>>>> Subject: [PATCH] trying muxing demuxing of scte_35 message cue
>>>>
>>>> ---
>>>>  libavcodec/Makefile     |  2 ++
>>>>  libavcodec/allcodecs.c  |  2 ++
>>>>  libavcodec/avcodec.h    |  1 +
>>>>  libavcodec/codec_desc.c |  6 ++++++
>>>>  libavformat/mpegts.c    | 40
>>> +++++++++++++++++++++++++++++++++-------
>>>>  5 files changed, 44 insertions(+), 7 deletions(-)
>>>>
>>>> diff --git a/libavcodec/Makefile b/libavcodec/Makefile
>>>> index b35a796..9d2a49f 100644
>>>> --- a/libavcodec/Makefile
>>>> +++ b/libavcodec/Makefile
>>>> @@ -408,6 +408,7 @@ OBJS-$(CONFIG_SAMI_DECODER)            +=
>>> samidec.o ass.o
>>>>  OBJS-$(CONFIG_S302M_DECODER)           += s302m.o
>>>>  OBJS-$(CONFIG_S302M_ENCODER)           += s302menc.o
>>>>  OBJS-$(CONFIG_SANM_DECODER)            += sanm.o
>>>> +OBJS-$(CONFIG_SCTE_35_DECODER)         += scte_35.o
>>> this patch doesnt contain a scte_35.c
>>> did you forget git add ?
>>>
>>> [...]
>> I have attached another patch, still its draft.
>> I do want some opinion about the approach of this development.
>>
>> to use this patch, one can pass below command
>> ./ffmpeg -loglevel debug -i ~/test_videos/mpegwithscte35.ts -dcodec
>> cue_xml -map 0:0 -f rawvideo some.raw
>>
>> There still some issue with this patch, like I have disabled estimate
>> timing of all stream since data stream does not have similar timing, i
>> will add condition there.
>>
>> FFmpeg hangs after running command, it might be because of EOF not
>> handled properly, looking at the issue.
>>
>>
>> My vision for this patch is that we can add custom advertisement
>> if stream have scte-cue.
>>
>> This patch might also help end-user to remove advertisements automatically.
>>
>> for broadcaster it will help to show advertisement in local language.
>> or for local product.
>> just now I am very far away from my goal but will not always be.
>>
>>
>> This patch have all files which I have developed.
> ar: libavcodec/scte_35.o: No such file or directory
>
> [...]
>
>
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
I am sorry, I attached the wrong patch file.
-Anshul
>From 6e04f65c33c4d4f873bbadabcc9d1d8464d49314 Mon Sep 17 00:00:00 2001
From: Anshul Maheshwari <anshul.ffm...@gmail.com>
Date: Mon, 29 Dec 2014 12:42:41 +0530
Subject: [PATCH] trying handle scte_35 messagecue

---
 ffmpeg.c                |  40 +++++++
 ffmpeg_opt.c            |   9 +-
 libavcodec/Makefile     |   2 +
 libavcodec/allcodecs.c  |   2 +
 libavcodec/avcodec.h    |  17 +++
 libavcodec/codec_desc.c |  12 ++
 libavcodec/cue_xml.c    | 196 ++++++++++++++++++++++++++++++++
 libavcodec/scte_35.c    | 294 ++++++++++++++++++++++++++++++++++++++++++++++++
 libavcodec/scte_35.h    |  31 +++++
 libavcodec/utils.c      |  33 +++++-
 libavformat/avformat.h  |  16 +++
 libavformat/mpegts.c    |  44 ++++++--
 libavformat/utils.c     |   3 +-
 libavutil/avstring.c    |  26 ++++-
 libavutil/avstring.h    |  15 +++
 15 files changed, 724 insertions(+), 16 deletions(-)
 create mode 100644 libavcodec/cue_xml.c
 create mode 100644 libavcodec/scte_35.c
 create mode 100644 libavcodec/scte_35.h

diff --git a/ffmpeg.c b/ffmpeg.c
index 1fd0ece..4817004 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -2002,6 +2002,41 @@ fail:
     return err < 0 ? err : ret;
 }
 
+static int transcode_data(InputStream *ist, AVPacket *pkt, int *got_output)
+{
+    AVCodecContext *dec = ist->dec_ctx;
+    enum AVCodecID in_codec_id = AV_CODEC_ID_NONE;
+    AVData input;
+    AVData output;
+    AVPacket outpkt;
+    int ret, i;
+
+    if (dec->codec)
+        in_codec_id = dec->codec->id;
+
+    input.valid = 0;
+    update_benchmark(NULL);
+    ret = avcodec_decode_data(dec, &input, pkt);
+    update_benchmark("decode_data %d.%d", ist->file_index, ist->st->index);
+    *got_output = input.valid;
+
+    for (i = 0; i < nb_output_streams && *got_output; i++) {
+        OutputStream *ost = output_streams[i];
+
+        if (!check_output_constraints(ist, ost) || !ost->encoding_needed
+            || ost->enc->type != AVMEDIA_TYPE_DATA)
+            continue;
+        ret = avcodec_encode_data(output_streams[i]->enc_ctx, &input, &output, in_codec_id);
+        av_init_packet(&outpkt);
+        outpkt.data = output.data;
+        outpkt.size = output.len;
+        write_frame(output_files[ost->file_index]->ctx, &outpkt, ost);
+
+    }
+
+    return ret;
+
+}
 static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output)
 {
     AVSubtitle subtitle;
@@ -2142,6 +2177,9 @@ static int process_input_packet(InputStream *ist, const AVPacket *pkt)
         case AVMEDIA_TYPE_SUBTITLE:
             ret = transcode_subtitles(ist, &avpkt, &got_output);
             break;
+        case AVMEDIA_TYPE_DATA:
+            ret = transcode_data(ist, &avpkt, &got_output);
+            break;
         default:
             return -1;
         }
@@ -2827,6 +2865,8 @@ static int transcode_init(void)
                     enc_ctx->height    = input_streams[ost->source_index]->st->codec->height;
                 }
                 break;
+            case AVMEDIA_TYPE_DATA:
+                break;
             default:
                 abort();
                 break;
diff --git a/ffmpeg_opt.c b/ffmpeg_opt.c
index 27e0890..2a265d3 100644
--- a/ffmpeg_opt.c
+++ b/ffmpeg_opt.c
@@ -794,6 +794,7 @@ static int open_input_file(OptionsContext *o, const char *filename)
     char *   video_codec_name = NULL;
     char *   audio_codec_name = NULL;
     char *subtitle_codec_name = NULL;
+    char *    data_codec_name = NULL;
 
     if (o->format) {
         if (!(file_iformat = av_find_input_format(o->format))) {
@@ -853,6 +854,8 @@ static int open_input_file(OptionsContext *o, const char *filename)
         find_codec_or_die(audio_codec_name   , AVMEDIA_TYPE_AUDIO   , 0)->id : AV_CODEC_ID_NONE;
     ic->subtitle_codec_id= subtitle_codec_name ?
         find_codec_or_die(subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, 0)->id : AV_CODEC_ID_NONE;
+    ic->data_codec_id    = data_codec_name ?
+        find_codec_or_die(data_codec_name, AVMEDIA_TYPE_DATA, 0)->id : AV_CODEC_ID_NONE;
 
     if (video_codec_name)
         av_format_set_video_codec   (ic, find_codec_or_die(video_codec_name   , AVMEDIA_TYPE_VIDEO   , 0));
@@ -860,6 +863,8 @@ static int open_input_file(OptionsContext *o, const char *filename)
         av_format_set_audio_codec   (ic, find_codec_or_die(audio_codec_name   , AVMEDIA_TYPE_AUDIO   , 0));
     if (subtitle_codec_name)
         av_format_set_subtitle_codec(ic, find_codec_or_die(subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, 0));
+    if (data_codec_name)
+        av_format_set_data_codec(ic, find_codec_or_die(data_codec_name, AVMEDIA_TYPE_DATA, 0));
 
     ic->flags |= AVFMT_FLAG_NONBLOCK;
     ic->interrupt_callback = int_cb;
@@ -1501,10 +1506,6 @@ static OutputStream *new_data_stream(OptionsContext *o, AVFormatContext *oc, int
     OutputStream *ost;
 
     ost = new_output_stream(o, oc, AVMEDIA_TYPE_DATA, source_index);
-    if (!ost->stream_copy) {
-        av_log(NULL, AV_LOG_FATAL, "Data stream encoding not supported yet (only streamcopy)\n");
-        exit_program(1);
-    }
 
     return ost;
 }
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index b35a796..f393da5 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -290,6 +290,7 @@ OBJS-$(CONFIG_LOCO_DECODER)            += loco.o
 OBJS-$(CONFIG_MACE3_DECODER)           += mace.o
 OBJS-$(CONFIG_MACE6_DECODER)           += mace.o
 OBJS-$(CONFIG_MDEC_DECODER)            += mdec.o mpeg12.o mpeg12data.o
+OBJS-$(CONFIG_CUE_XML_ENCODER)         += cue_xml.o
 OBJS-$(CONFIG_METASOUND_DECODER)       += metasound.o metasound_data.o \
                                           twinvq.o
 OBJS-$(CONFIG_MICRODVD_DECODER)        += microdvddec.o ass.o
@@ -408,6 +409,7 @@ OBJS-$(CONFIG_SAMI_DECODER)            += samidec.o ass.o
 OBJS-$(CONFIG_S302M_DECODER)           += s302m.o
 OBJS-$(CONFIG_S302M_ENCODER)           += s302menc.o
 OBJS-$(CONFIG_SANM_DECODER)            += sanm.o
+OBJS-$(CONFIG_SCTE_35_DECODER)         += scte_35.o
 OBJS-$(CONFIG_SGI_DECODER)             += sgidec.o
 OBJS-$(CONFIG_SGI_ENCODER)             += sgienc.o rle.o
 OBJS-$(CONFIG_SGIRLE_DECODER)          += sgirledec.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 36bbfed..1392582 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -535,6 +535,8 @@ void avcodec_register_all(void)
     REGISTER_ENCODER(LIBAACPLUS,        libaacplus);
 
     /* text */
+    REGISTER_DECODER(SCTE_35,           scte_35)
+    REGISTER_ENCODER(CUE_XML,           cue_xml);
     REGISTER_DECODER(BINTEXT,           bintext);
     REGISTER_DECODER(XBIN,              xbin);
     REGISTER_DECODER(IDF,               idf);
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index eac3fc7..e5f3d06 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -522,6 +522,8 @@ enum AVCodecID {
     /* other specific kind of codecs (generally used for attachments) */
     AV_CODEC_ID_FIRST_UNKNOWN = 0x18000,           ///< A dummy ID pointing at the start of various fake codecs.
     AV_CODEC_ID_TTF = 0x18000,
+    AV_CODEC_ID_SCTE_35    = MKBETAG('C','U','E','I'),
+    AV_CODEC_ID_CUE_XML    = MKBETAG('C','U','E','X'),
     AV_CODEC_ID_BINTEXT    = MKBETAG('B','T','X','T'),
     AV_CODEC_ID_XBIN       = MKBETAG('X','B','I','N'),
     AV_CODEC_ID_IDF        = MKBETAG( 0 ,'I','D','F'),
@@ -3149,6 +3151,13 @@ typedef struct AVCodecDefault AVCodecDefault;
 
 struct AVSubtitle;
 
+typedef struct AVData {
+    void *data;
+    int len;
+    int ref_count;
+    int valid;
+} AVData;
+
 /**
  * AVCodec.
  */
@@ -3225,6 +3234,8 @@ typedef struct AVCodec {
     int (*init)(AVCodecContext *);
     int (*encode_sub)(AVCodecContext *, uint8_t *buf, int buf_size,
                       const struct AVSubtitle *sub);
+     int (*encode_data)(AVCodecContext *avctx, const AVData *input, AVData *output,
+                            enum AVCodecID in_codec_id);
     /**
      * Encode data to an AVPacket.
      *
@@ -3238,6 +3249,7 @@ typedef struct AVCodec {
     int (*encode2)(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame,
                    int *got_packet_ptr);
     int (*decode)(AVCodecContext *, void *outdata, int *outdata_size, AVPacket *avpkt);
+    int (*decode_data)(AVCodecContext *,AVData *output, const AVPacket *avpkt);
     int (*close)(AVCodecContext *);
     /**
      * Flush buffers.
@@ -4181,6 +4193,9 @@ int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
                             int *got_sub_ptr,
                             AVPacket *avpkt);
 
+int avcodec_decode_data(AVCodecContext *avctx,
+                                              AVData *output,
+                                              const AVPacket *avpkt);
 /**
  * @defgroup lavc_parsing Frame parsing
  * @{
@@ -4569,6 +4584,8 @@ int avcodec_encode_video2(AVCodecContext *avctx, AVPacket *avpkt,
 
 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
                             const AVSubtitle *sub);
+int avcodec_encode_data(AVCodecContext *avctx,const AVData *input, AVData *output,
+                            enum AVCodecID in_codec_id);
 
 
 /**
diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index eeb4505..eb24f55 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -2731,6 +2731,18 @@ static const AVCodecDescriptor codec_descriptors[] = {
         .long_name = NULL_IF_CONFIG_SMALL("binary data"),
         .mime_types= MT("application/octet-stream"),
     },
+    {
+        .id        = AV_CODEC_ID_SCTE_35,
+        .type      = AVMEDIA_TYPE_DATA,
+        .name      = "scte_35",
+        .long_name = NULL_IF_CONFIG_SMALL("SCTE 35 Message Queue"),
+    },
+    {
+        .id        = AV_CODEC_ID_CUE_XML,
+        .type      = AVMEDIA_TYPE_DATA,
+        .name      = "cue_xml",
+        .long_name = NULL_IF_CONFIG_SMALL("Message Queue to Insert Add"),
+    },
 
     /* deprecated codec ids */
     {
diff --git a/libavcodec/cue_xml.c b/libavcodec/cue_xml.c
new file mode 100644
index 0000000..4da97d5
--- /dev/null
+++ b/libavcodec/cue_xml.c
@@ -0,0 +1,196 @@
+/*
+ * SCTE 35 Cue Xml  encoder
+ * Copyright (c) 2014 Anshul Maheshwaari
+ *
+ * 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
+ */
+#include "avcodec.h"
+#include "libavutil/opt.h"
+#include "libavutil/bprint.h"
+#include "scte_35.h"
+
+struct varList{
+    char *var;
+    char *value;
+    struct varList *next;
+};
+struct node {
+    char *name;
+    struct varList *vlist;
+    struct node *child;
+    struct node *parent;
+    struct node *sibling;
+};
+
+struct cue_xml_ctx {
+    AVClass *class;
+    struct node xml_tree;
+};
+static void init_node(struct node* n,const char *name)
+{
+    n->name    = av_strdup(name);
+    n->vlist   = NULL;
+    n->child   = NULL;
+    n->parent  = NULL;
+    n->sibling = NULL;
+}
+static void add_child(struct node *parent, struct node* n)
+{
+    struct node *tmp;
+
+    /* add to parent node
+     * parent keep only address of one child , other children are kept in sibling */
+    if(!parent->child)
+        parent->child = n;
+    else {
+        tmp = parent->child;
+        while(tmp->sibling) {
+            tmp = tmp->sibling;
+        }
+        tmp->sibling = n;
+    }
+    n->parent = parent;
+}
+
+/**
+ * memory of value need to be preallocated
+ */
+static int add_varval_pair(struct node* n,const char *var,const char *val)
+{
+    struct varList *pair;
+
+    pair = av_mallocz(sizeof(*pair));
+    if(!pair)
+        return AVERROR(ENOMEM);
+
+    pair->var = av_strdup(var);
+    pair->value = val;
+
+    if(!n->vlist) {
+        n->vlist = pair;
+    } else {
+        struct varList *npair = n->vlist;
+        while (npair->next) {
+            npair = npair->next;
+        }
+        npair->next = pair;
+    }
+    return 0;
+
+}
+static int add_varval_pair_char(struct node* n,const char *var,const char *val)
+{
+    char *val2 = av_strdup(val);
+    return add_varval_pair(n, var, val2);
+}
+
+static int add_varval_pair_uint32_t(struct node* n,const char *var,const uint32_t val)
+{
+    char *val2 = av_uint2str(val);
+    return add_varval_pair(n, var, val2);
+}
+
+static int add_varval_pair_uint64_t(struct node* n,const char *var,const uint64_t val)
+{
+    char *val2 = av_uint64_t2str(val);
+    return add_varval_pair(n, var, val2);
+}
+static void write_tree_as_data(struct node* root, AVData *data)
+{
+    AVBPrint buffer;
+    struct node *tmp = root->child;
+    struct node *leaf;
+    av_bprint_init(&buffer, 0, AV_BPRINT_SIZE_UNLIMITED);
+
+    while(tmp) {
+        struct varList *npair = tmp->vlist;
+        av_bprintf(&buffer, "<%s", tmp->name);
+        while(npair) {
+            av_bprintf(&buffer, " %s=%s", npair->var, npair->value);
+            npair = npair->next;
+        }
+        av_bprintf(&buffer, ">\n");
+        leaf = tmp;
+        tmp = tmp->child;
+    }
+    tmp = leaf;
+    while(tmp->parent) {
+         av_bprintf(&buffer, "</%s>\n", tmp->name);
+         tmp = tmp->parent;
+    }
+    data->data = av_strdup(buffer.str);
+    data->len  = strlen(buffer.str);
+    data->valid = 1;
+
+    av_log(NULL, AV_LOG_DEBUG, "%s\n",buffer.str);
+    av_bprint_finalize( &buffer, NULL);
+
+}
+static av_cold int init_encoder(AVCodecContext *avctx)
+{
+    struct cue_xml_ctx *ctx = avctx->priv_data;
+    init_node(&ctx->xml_tree,"scte35");
+
+    return 0;
+}
+static av_cold int close_encoder(AVCodecContext *avctx)
+{
+    struct cue_xml_ctx *ctx = avctx->priv_data;
+    return 0;
+}
+
+static int encode(AVCodecContext *avctx, const AVData *input, AVData *output,
+                            enum AVCodecID in_codec_id)
+{
+    struct scte_35_interface *interface;
+    struct node event;
+    struct cue_xml_ctx *ctx = avctx->priv_data;
+
+    if(in_codec_id != AV_CODEC_ID_SCTE_35)
+        return -1;
+
+    interface = input->data;
+    init_node(&event,"event");
+    add_varval_pair_uint32_t(&event, "event_id", interface->event_id);
+    add_varval_pair_uint32_t(&event, "duration", interface->duration);
+    add_varval_pair_uint64_t(&event, "pts", interface->pts);
+    add_child(&ctx->xml_tree, &event);
+
+    write_tree_as_data(&ctx->xml_tree, output);
+    return 0;
+}
+static const AVOption options[] = {
+    {NULL}
+};
+static const AVClass cue_xml_dec_class = {
+    .class_name = "SCTE 35 Decoder",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
+AVCodec ff_cue_xml_encoder = {
+    .name           = "cue_xml",
+    .long_name      = NULL_IF_CONFIG_SMALL("XML Message Queue Ad Insertion"),
+    .type           = AVMEDIA_TYPE_DATA,
+    .id             = AV_CODEC_ID_CUE_XML,
+    .priv_data_size = sizeof(struct cue_xml_ctx),
+    .init           = init_encoder,
+    .close          = close_encoder,
+    .encode_data    = encode,
+    .priv_class     = &cue_xml_dec_class,
+};
diff --git a/libavcodec/scte_35.c b/libavcodec/scte_35.c
new file mode 100644
index 0000000..2b3af27
--- /dev/null
+++ b/libavcodec/scte_35.c
@@ -0,0 +1,294 @@
+/*
+ * SCTE 35 decoder
+ * Copyright (c) 2014 Anshul Maheshwaari
+ *
+ * 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
+ */
+#include "avcodec.h"
+#include "bytestream.h"
+#include "libavutil/opt.h"
+#include "libavcodec/get_bits.h"
+#include "libavcodec/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
+struct scte_35_ctx {
+    AVClass *class;
+    uint64_t pts_adjustment;
+    uint32_t event_id;
+    int component_tag;
+    int64_t duration;
+    uint16_t u_program_id;
+    uint8_t avail_num;
+    uint8_t avail_expect;
+    /* flags */
+    int splice_event_cancel_indicator;
+    int out_of_network_indicator;
+    int program_splice_flag;
+    int duration_flag;
+    int splice_immediate_flag;
+    int auto_return;
+};
+static av_cold int init_decoder(AVCodecContext *avctx)
+{
+    struct scte_35_ctx *ctx = avctx->priv_data;
+    return 0;
+}
+static av_cold int close_decoder(AVCodecContext *avctx)
+{
+    struct scte_35_ctx *ctx = avctx->priv_data;
+    return 0;
+}
+
+/**
+ * save the parsed time in ctx pts_time
+   @return length of buffer consumed
+*/
+static int parse_splice_time(struct scte_35_ctx *ctx, const uint8_t *buf, uint64_t *pts)
+{
+    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) + ctx->pts_adjustment;
+        av_log(ctx, AV_LOG_DEBUG, "time  = %lld\n", *pts/90000);
+        return 5;
+    } else {
+        skip_bits(&gb, 7);
+        return 1;
+    }
+}
+static int parse_schedule_cmd(struct scte_35_ctx *ctx, const uint8_t *buf)
+{
+    const uint8_t *sbuf = buf;
+    av_log(ctx, AV_LOG_DEBUG, "Schedule cmd\n");
+    return buf - sbuf;
+}
+/**
+     @return length of buffer used
+ */
+static int parse_insert_cmd(struct scte_35_ctx *ctx, const uint8_t *buf, AVData *output)
+{
+    GetBitContext gb;
+    int ret;
+    const uint8_t *sbuf = buf;
+    struct scte_35_interface *iface;
+
+    iface = av_mallocz(sizeof(struct scte_35_interface));
+    if(!iface)
+        return AVERROR(ENOMEM);
+    output->data = iface;
+    output->valid = 1;
+
+    av_log(ctx, AV_LOG_DEBUG, "Insert cmd\n");
+    ctx->event_id = AV_RB32(buf);
+    av_log(ctx, AV_LOG_DEBUG, "event_id  = %x\n", ctx->event_id);
+    buf +=4;
+    ctx->splice_event_cancel_indicator = *buf & 0x80;
+    av_log(ctx, AV_LOG_DEBUG, "splice_event_cancel_indicator  = %d\n", ctx->splice_event_cancel_indicator);
+    buf++;
+
+    iface->event_id = ctx->event_id;
+    if (!ctx->splice_event_cancel_indicator) {
+        init_get_bits(&gb, buf, 8);
+        ctx->out_of_network_indicator =  get_bits(&gb, 1);
+        ctx->program_splice_flag =  get_bits(&gb, 1);
+        ctx->duration_flag =  get_bits(&gb, 1);
+        ctx->splice_immediate_flag =  get_bits(&gb, 1);
+        skip_bits(&gb, 4);
+
+    }
+    buf++;
+
+
+    av_log(ctx, AV_LOG_DEBUG, "out_of_network_indicator  = %d\n", ctx->out_of_network_indicator);
+    av_log(ctx, AV_LOG_DEBUG, "program_splice_flag  = %d\n", ctx->program_splice_flag);
+    av_log(ctx, AV_LOG_DEBUG, "duration_flag  = %d\n", ctx->duration_flag);
+    av_log(ctx, AV_LOG_DEBUG, "splice_immediate_flag  = %d\n", ctx->splice_immediate_flag);
+
+    if (ctx->program_splice_flag &&  !ctx->splice_immediate_flag) {
+        ret = parse_splice_time(ctx, buf, &iface->pts);
+        buf += ret;
+    }
+    if ( ctx->program_splice_flag == 0) {
+        int comp_cnt = *buf++;
+        int  i;
+        av_log(ctx, AV_LOG_DEBUG, "component_count  = %d\n", comp_cnt);
+        for ( i = 0; i < comp_cnt; i++) {
+            ctx->component_tag = *buf++;
+            av_log(ctx, AV_LOG_DEBUG, "component_tag  = %d\n", ctx->component_tag);
+            if (ctx->splice_immediate_flag) {
+                ret = parse_splice_time(ctx, buf, &iface->pts);
+                buf += ret;
+            }
+        }
+    }
+    if ( ctx->duration_flag ) {
+        init_get_bits(&gb, buf, 40);
+        ctx->auto_return =  get_bits(&gb, 1);
+        av_log(ctx, AV_LOG_DEBUG, "autoreturn  = %d\n", ctx->auto_return);
+        skip_bits(&gb, 6);
+        ctx->duration = get_bits64(&gb,33) + ctx->pts_adjustment;
+        av_log(ctx, AV_LOG_DEBUG, "duration  = %lld\n", ctx->duration/90000);
+        buf += 5;
+    }
+    ctx->u_program_id = AV_RB16(buf);
+    av_log(ctx, AV_LOG_DEBUG, "u_program_id  = %hd\n", ctx->u_program_id);
+    buf += 2;
+    ctx->avail_num = *buf++;
+    av_log(ctx, AV_LOG_DEBUG, "avail_num  = %hhd\n", ctx->avail_num);
+    ctx->avail_expect = *buf++;
+    av_log(ctx, AV_LOG_DEBUG, "avail_expect  = %hhd\n", ctx->avail_expect);
+
+    return buf - sbuf;
+}
+static int parse_time_signal_cmd(struct scte_35_ctx *ctx, const uint8_t *buf)
+{
+    const uint8_t *sbuf = buf;
+    av_log(ctx, AV_LOG_DEBUG, "Time Signal cmd\n");
+    return buf - sbuf;
+}
+static int parse_bandwidth_reservation_cmd(struct scte_35_ctx *ctx, const uint8_t *buf)
+{
+    const uint8_t *sbuf = buf;
+    av_log(ctx, AV_LOG_DEBUG, "Band Width reservation cmd\n");
+    return buf - sbuf;
+}
+
+static int decode(AVCodecContext *avctx, AVData *output,
+                                              const AVPacket *avpkt)
+{
+    struct scte_35_ctx *ctx = avctx->priv_data;
+    const uint8_t *buf = avpkt->data;
+    int len = avpkt->size;
+    int section_length;
+    int cmd_length;
+    uint8_t cmd_type;
+    int16_t tier;
+    GetBitContext gb;
+    int ret;
+
+    if (!buf)
+        return 0;
+
+    /* check table id */
+    if (*buf != 0xfc)
+        av_log(ctx, 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(ctx, AV_LOG_WARNING, "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(ctx, AV_LOG_WARNING, "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(ctx, AV_LOG_ERROR, "Invalid length of section\n");
+        return AVERROR_INVALIDDATA;
+    }
+
+    /* protocol version */
+    skip_bits(&gb,8);
+
+    ret = get_bits(&gb,1);
+    if(ret) {
+        av_log(ctx, AV_LOG_ERROR, "Encrytion not yet supported\n");
+        return AVERROR_PATCHWELCOME;
+    }
+    /* encryption algo */
+    skip_bits(&gb,6);
+
+    ctx->pts_adjustment =  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) == 0xfff )
+        cmd_length = -1;
+
+    cmd_type = get_bits(&gb,8);
+    switch(cmd_type) {
+    case SCTE_CMD_NULL:
+        av_log(ctx, AV_LOG_DEBUG, "NULL packet\n");
+            break;
+        case SCTE_CMD_SCHEDULE:
+            ret = parse_schedule_cmd(ctx, buf + 14);
+            break;
+        case SCTE_CMD_INSERT:
+            ret = parse_insert_cmd(ctx, buf + 14, output);
+            break;
+        case SCTE_CMD_SIGNAL:
+            ret = parse_time_signal_cmd(ctx, buf + 14);
+            break;
+        case SCTE_CMD_BANDWIDTH_RESERVATION:
+            ret = parse_bandwidth_reservation_cmd(ctx, buf + 14);
+            break;
+        default:
+            break;
+        /* reserved yet */
+    }
+    if(ret < 0)
+        goto fail;
+    buf += ret;
+
+fail:
+    return ret;
+}
+static const AVOption options[] = {
+    {NULL}
+};
+static const AVClass scte_35_dec_class = {
+    .class_name = "SCTE 35 Decoder",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
+AVCodec ff_scte_35_decoder = {
+    .name           = "scte_35",
+    .long_name      = NULL_IF_CONFIG_SMALL("SCTE 35"),
+    .type           = AVMEDIA_TYPE_DATA,
+    .id             = AV_CODEC_ID_SCTE_35,
+    .priv_data_size = sizeof(struct scte_35_ctx),
+    .init           = init_decoder,
+    .close          = close_decoder,
+    .decode_data    = decode,
+    .priv_class     = &scte_35_dec_class,
+};
diff --git a/libavcodec/scte_35.h b/libavcodec/scte_35.h
new file mode 100644
index 0000000..63bbac5
--- /dev/null
+++ b/libavcodec/scte_35.h
@@ -0,0 +1,31 @@
+/*
+ * SCTE 35 decoder
+ * Copyright (c) 2014 Anshul Maheshwaari
+ *
+ * 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 SCTE_35_H
+#define SCTE_35_H
+struct scte_35_interface {
+    int32_t event_id;
+    uint64_t pts;
+    int64_t duration;
+    /* flag */
+    int inout;
+    int cancel;
+};
+#endif
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index b6ae1c0..7fae75d 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -183,12 +183,12 @@ static av_cold void avcodec_init(void)
 
 int av_codec_is_encoder(const AVCodec *codec)
 {
-    return codec && (codec->encode_sub || codec->encode2);
+    return codec && (codec->encode_sub || codec->encode2 || codec->encode_data);
 }
 
 int av_codec_is_decoder(const AVCodec *codec)
 {
-    return codec && codec->decode;
+    return codec && ( codec->decode || codec->decode_data);
 }
 
 av_cold void avcodec_register(AVCodec *codec)
@@ -2158,7 +2158,14 @@ int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
     avctx->frame_number++;
     return ret;
 }
-
+int avcodec_encode_data(AVCodecContext *avctx, const AVData *input, AVData *output,
+                            enum AVCodecID in_codec_id)
+{
+    int ret;
+    ret = avctx->codec->encode_data(avctx, input, output, in_codec_id);
+    avctx->frame_number++;
+    return ret;
+}
 /**
  * Attempt to guess proper monotonic timestamps for decoded video frames
  * which might have incorrect times. Input timestamps may wrap around, in
@@ -2609,6 +2616,26 @@ fail:
     return ret;
 }
 
+int attribute_align_arg avcodec_decode_data(AVCodecContext *avctx,
+                                              AVData *output,
+                                              const AVPacket *avpkt)
+{
+    AVCodecInternal *avci = avctx->internal;
+    int ret = 0;
+
+    if (!avctx->codec)
+        return AVERROR(EINVAL);
+    if (avctx->codec->type != AVMEDIA_TYPE_DATA) {
+        av_log(avctx, AV_LOG_ERROR, "Invalid media type for data\n");
+        return AVERROR(EINVAL);
+    }
+
+    ret = avctx->codec->decode_data(avctx, output, avpkt);
+
+    return ret;
+
+
+}
 #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
 static int recode_subtitle(AVCodecContext *avctx,
                            AVPacket *outpkt, const AVPacket *inpkt)
diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index f21a1d6..1665aa0 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -1352,6 +1352,12 @@ typedef struct AVFormatContext {
     enum AVCodecID subtitle_codec_id;
 
     /**
+     * Forced Data codec_id.
+     * Demuxing: Set by user.
+     */
+    enum AVCodecID data_codec_id;
+
+    /**
      * Maximum amount of memory in bytes to use for the index of each stream.
      * If the index exceeds this size, entries will be discarded as
      * needed to maintain a smaller size. This can lead to slower or less
@@ -1689,6 +1695,14 @@ typedef struct AVFormatContext {
     AVCodec *subtitle_codec;
 
     /**
+     * Forced data codec.
+     * This allows forcing a specific decoder, even when there are multiple with
+     * the same codec_id.
+     * Demuxing: Set by user via av_format_set_data_codec (NO direct access).
+     */
+    AVCodec *data_codec;
+
+    /**
      * Number of bytes to be written as padding in a metadata header.
      * Demuxing: Unused.
      * Muxing: Set by user via av_format_set_metadata_header_padding.
@@ -1748,6 +1762,8 @@ AVCodec * av_format_get_audio_codec(const AVFormatContext *s);
 void      av_format_set_audio_codec(AVFormatContext *s, AVCodec *c);
 AVCodec * av_format_get_subtitle_codec(const AVFormatContext *s);
 void      av_format_set_subtitle_codec(AVFormatContext *s, AVCodec *c);
+AVCodec * av_format_get_data_codec(const AVFormatContext *s);
+void      av_format_set_data_codec(AVFormatContext *s, AVCodec *c);
 int       av_format_get_metadata_header_padding(const AVFormatContext *s);
 void      av_format_set_metadata_header_padding(AVFormatContext *s, int c);
 void *    av_format_get_opaque(const AVFormatContext *s);
diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index c01f954..41c6f9b 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -57,6 +57,7 @@ enum MpegTSFilterType {
     MPEGTS_PES,
     MPEGTS_SECTION,
     MPEGTS_PCR,
+    MPEGTS_DATA,
 };
 
 typedef struct MpegTSFilter MpegTSFilter;
@@ -488,6 +489,11 @@ static MpegTSFilter *mpegts_open_pcr_filter(MpegTSContext *ts, unsigned int pid)
     return mpegts_open_filter(ts, pid, MPEGTS_PCR);
 }
 
+static MpegTSFilter *mpegts_open_data_filter(MpegTSContext *ts, unsigned int pid)
+{
+    return mpegts_open_filter(ts, pid, MPEGTS_DATA);
+}
+
 static void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
 {
     int pid;
@@ -666,6 +672,7 @@ static const StreamType ISO_types[] = {
     { 0x1b, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_H264       },
     { 0x24, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_HEVC       },
     { 0x42, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_CAVS       },
+    { 0x86, AVMEDIA_TYPE_DATA,  AV_CODEC_ID_SCTE_35    },
     { 0xd1, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_DIRAC      },
     { 0xea, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_VC1        },
     { 0 },
@@ -813,7 +820,12 @@ static void reset_pes_packet_state(PESContext *pes)
     pes->flags      = 0;
     av_buffer_unref(&pes->buffer);
 }
-
+static void new_data_packet(const uint8_t *buffer, int len, AVPacket *pkt)
+{
+    av_init_packet(pkt);
+    pkt->data = buffer;
+    pkt->size = len;
+}
 static void new_pes_packet(PESContext *pes, AVPacket *pkt)
 {
     av_init_packet(pkt);
@@ -1871,7 +1883,20 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len
                 pes->st->id = pes->pid;
             }
             st = pes->st;
-        } else if (stream_type != 0x13) {
+        } else if (stream_type == 0x86 ) {
+            int idx = ff_find_stream_index(ts->stream, pid);
+            if (idx >= 0) {
+                st = ts->stream->streams[idx];
+            } else {
+                st = avformat_new_stream(ts->stream, NULL);
+                if (!st)
+                    goto out;
+                st->id = pid;
+                st->codec->codec_type = AVMEDIA_TYPE_DATA;
+                mpegts_find_stream_type(st, stream_type, ISO_types);
+                mpegts_open_data_filter(ts, pid);
+            }
+        }else if (stream_type != 0x13 ) {
             if (ts->pids[pid])
                 mpegts_close_filter(ts, ts->pids[pid]); // wrongly added sdt filter probably
             pes = add_pes_stream(ts, pid, pcr_pid);
@@ -2202,14 +2227,17 @@ static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
             }
         }
 
-    } else {
+    } else if (tss->type == MPEGTS_DATA) {
+        /* may be pointer field is 0 and to be ignored*/
+        p++;
+        new_data_packet(p,p_end - p, ts->pkt);
+        ts->stop_parse = 1;
+    } else if (tss->type == MPEGTS_PES) {
         int ret;
         // Note: The position here points actually behind the current packet.
-        if (tss->type == MPEGTS_PES) {
-            if ((ret = tss->u.pes_filter.pes_cb(tss, p, p_end - p, is_start,
+        if ((ret = tss->u.pes_filter.pes_cb(tss, p, p_end - p, is_start,
                                                 pos - ts->raw_packet_size)) < 0)
-                return ret;
-        }
+            return ret;
     }
 
     return 0;
@@ -2595,6 +2623,8 @@ static int mpegts_read_packet(AVFormatContext *s, AVPacket *pkt)
                     ret = 0;
                     break;
                 }
+            } else if (ts->pids[i] && ts->pids[i]->type == MPEGTS_DATA) {
+                ret = 0;
             }
     }
 
diff --git a/libavformat/utils.c b/libavformat/utils.c
index 61421c0..621f64a 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -106,6 +106,7 @@ MAKE_ACCESSORS(AVStream, stream, AVRational, r_frame_rate)
 MAKE_ACCESSORS(AVFormatContext, format, AVCodec *, video_codec)
 MAKE_ACCESSORS(AVFormatContext, format, AVCodec *, audio_codec)
 MAKE_ACCESSORS(AVFormatContext, format, AVCodec *, subtitle_codec)
+MAKE_ACCESSORS(AVFormatContext, format, AVCodec *, data_codec)
 MAKE_ACCESSORS(AVFormatContext, format, int, metadata_header_padding)
 MAKE_ACCESSORS(AVFormatContext, format, void *, opaque)
 MAKE_ACCESSORS(AVFormatContext, format, av_format_control_message, control_message_cb)
@@ -2490,7 +2491,7 @@ static void estimate_timings(AVFormatContext *ic, int64_t old_offset)
         file_size = FFMAX(0, file_size);
     }
 
-    if ((!strcmp(ic->iformat->name, "mpeg") ||
+    if (0 && (!strcmp(ic->iformat->name, "mpeg") ||
          !strcmp(ic->iformat->name, "mpegts")) &&
         file_size && ic->pb->seekable) {
         /* get accurate estimate from the PTSes */
diff --git a/libavutil/avstring.c b/libavutil/avstring.c
index 25c65b4..508ae03 100644
--- a/libavutil/avstring.c
+++ b/libavutil/avstring.c
@@ -143,7 +143,31 @@ char *av_d2str(double d)
         snprintf(str, 16, "%f", d);
     return str;
 }
-
+char *av_int2str(int i)
+{
+    char *str = av_malloc(16);
+    if (str)
+        snprintf(str, 16, "%d", i);
+    return str;
+}
+char *av_uint2str(unsigned int i)
+{
+    char *str = av_malloc(16);
+    if (str)
+        snprintf(str, 16, "%u", i);
+    return str;
+}
+/**
+ * 0xff ff ff ff ff ff ff ff = 18446744073709551615
+ * therefor taking 20 digit
+ */
+char *av_uint64_t2str(uint64_t val)
+{
+    char *str = av_malloc(20);
+    if (str)
+        snprintf(str, 20, "%llu", val);
+    return str;
+}
 #define WHITESPACES " \n\t"
 
 char *av_get_token(const char **buf, const char *term)
diff --git a/libavutil/avstring.h b/libavutil/avstring.h
index ea7d1dc..8e7ba25 100644
--- a/libavutil/avstring.h
+++ b/libavutil/avstring.h
@@ -161,6 +161,21 @@ char *av_asprintf(const char *fmt, ...) av_printf_format(1, 2);
 char *av_d2str(double d);
 
 /**
+ * Convert a int to a av_malloced string.
+ */
+char *av_int2str(int i);
+
+/**
+ * Convert a unsigned int to a av_malloced string.
+ */
+char *av_uint2str(unsigned int ui);
+
+/**
+ * Convert a 64 bit decimal to a av_malloced string.
+ */
+char *av_uint64_t2str(uint64_t val);
+
+/**
  * Unescape the given string until a non escaped terminating char,
  * and return the token corresponding to the unescaped string.
  *
-- 
1.8.1.4

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel

Reply via email to