This new muxer/demuxer will allow to read/write menu stream
from/to 'mnu' files. This will allow to extract menu from BDAV
m2ts file to a elementary stream file suitable for Bluray
authoring software (such as BDedit from doom9).

Mpeg TS muxer/demuxer was also updated to recognize these IGS
menu streams. More update required to allow muxing IGS menu
inside an mpegts file.

version 2:
- implement a better probe in mnudec.c

version 3:
- change alloc error return value in write_header

version 4:
- add a call to avpriv_set_pts_info in write_header
- add AVFMT_NOTIMESTAMPS in format flag to allow streamcopy by avconv
- add AV_PKT_FLAG_KEY for all igs streams packets in mpegts demuxer

Signed-off-by: David Girault <david at dhgirault.fr>
---
 libavformat/Makefile     |    2 +
 libavformat/allformats.c |    1 +
 libavformat/avformat.h   |    1 +
 libavformat/mnudec.c     |  128 ++++++++++++++++++++++++++++++++++++++++++++++
 libavformat/mnuenc.c     |   66 ++++++++++++++++++++++++
 libavformat/mpegts.c     |    2 +
 6 files changed, 200 insertions(+)
 create mode 100644 libavformat/mnudec.c
 create mode 100644 libavformat/mnuenc.c

diff --git a/libavformat/Makefile b/libavformat/Makefile
index ca4f7a0..182365a 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -140,6 +140,8 @@ OBJS-$(CONFIG_MLP_MUXER)                 += rawenc.o
 OBJS-$(CONFIG_MM_DEMUXER)                += mm.o
 OBJS-$(CONFIG_MMF_DEMUXER)               += mmf.o pcm.o
 OBJS-$(CONFIG_MMF_MUXER)                 += mmf.o
+OBJS-$(CONFIG_MNU_DEMUXER)               += mnudec.o
+OBJS-$(CONFIG_MNU_MUXER)                 += mnuenc.o
 OBJS-$(CONFIG_MOV_DEMUXER)               += mov.o isom.o mov_chan.o
 OBJS-$(CONFIG_MOV_MUXER)                 += movenc.o isom.o avc.o \
                                             movenchint.o rtpenc_chain.o \
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 1320a28..01802da 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -129,6 +129,7 @@ void av_register_all(void)
     REGISTER_MUXDEMUX (MLP, mlp);
     REGISTER_DEMUXER  (MM, mm);
     REGISTER_MUXDEMUX (MMF, mmf);
+    REGISTER_MUXDEMUX (MNU, mnu);
     REGISTER_MUXDEMUX (MOV, mov);
     REGISTER_MUXER    (MP2, mp2);
     REGISTER_MUXDEMUX (MP3, mp3);
diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index 040e35e..b564ea9 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -373,6 +373,7 @@ typedef struct AVOutputFormat {
     enum CodecID audio_codec;    /**< default audio codec */
     enum CodecID video_codec;    /**< default video codec */
     enum CodecID subtitle_codec; /**< default subtitle codec */
+    enum CodecID overlay_codec;  /**< default overlay codec */
     /**
      * can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_RAWPICTURE,
      * AVFMT_GLOBALHEADER, AVFMT_NOTIMESTAMPS, AVFMT_VARIABLE_FPS,
diff --git a/libavformat/mnudec.c b/libavformat/mnudec.c
new file mode 100644
index 0000000..2960c3b
--- /dev/null
+++ b/libavformat/mnudec.c
@@ -0,0 +1,128 @@
+/*
+ * MNU demuxer
+ * Copyright (c) 2012 David Girault
+ *
+ * This file is part of Libav.
+ *
+ * Libav 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.
+ *
+ * Libav 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 Libav; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/mathematics.h"
+#include "libavcodec/bytestream.h"
+#include "avformat.h"
+#include "internal.h"
+
+typedef struct MNUContext{
+    uint8_t *buffer;
+} MNUContext;
+
+enum SegmentType {
+    PALETTE_SEGMENT      = 0x14,
+    PICTURE_SEGMENT      = 0x15,
+    BUTTON_SEGMENT       = 0x18,
+    DISPLAY_SEGMENT      = 0x80,
+};
+
+static int probe(AVProbeData *p)
+{
+    const char header[]= "IG";
+    uint8_t *ptr;
+    uint16_t len;
+    uint8_t type;
+
+    if(memcmp(p->buf, header, 2))
+        return 0; // marker not found
+    if (p->buf_size < 13)
+        return 0; // too small
+
+    ptr = &(p->buf[10]); // skip pts/dts
+
+    type = bytestream_get_byte(&ptr);
+    if ((type < PALETTE_SEGMENT || type > BUTTON_SEGMENT) &&
+        (type != DISPLAY_SEGMENT))
+        return 0; // invalid segment type
+
+    len = bytestream_get_be16(&ptr);
+    if (p->buf_size > (len+13+2)) {
+        if(memcmp(p->buf, header, 2))
+            return 0; // marker not found at beginning of second frame
+        return AVPROBE_SCORE_MAX;
+    }
+    // buffer too small to check the second frame
+    return AVPROBE_SCORE_MAX / 2;
+}
+
+static int read_close(AVFormatContext *s)
+{
+    MNUContext *mnu = s->priv_data;
+    av_freep(&mnu->buffer);
+    return 0;
+}
+
+static int read_header(AVFormatContext *s)
+{
+    AVStream *st = avformat_new_stream(s, NULL);
+    if (!st)
+        return AVERROR(ENOMEM);
+    avpriv_set_pts_info(st, 33, 1, 90000);
+    st->codec->codec_type = AVMEDIA_TYPE_OVERLAY;
+    st->codec->codec_id= CODEC_ID_HDMV_IGS_MENU;
+    return 0;
+}
+
+static int read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+//    MNUContext *mnu = s->priv_data;
+    uint64_t pos = avio_tell(s->pb);
+    int res = AVERROR_EOF;
+
+    char header[2];
+    uint32_t pts, dts;
+    uint8_t seg_type;
+    uint16_t seg_length;
+
+    // "IG",PTS,DTS,SEG_TYPE,SEG_LENGTH
+    res = avio_read(s->pb, header, 2);
+    if (header[0]!='I'||header[1]!='G') return AVERROR_INVALIDDATA;
+    pts = avio_rb32(s->pb);
+    dts = avio_rb32(s->pb);
+    seg_type = avio_r8(s->pb);
+    seg_length = avio_rb16(s->pb);
+
+    res = av_new_packet(pkt, seg_length+3);
+    if (!res) {
+        uint8_t *buf = pkt->data;
+        bytestream_put_byte(&buf, seg_type);
+        bytestream_put_be16(&buf, seg_length);
+        avio_read(s->pb, buf, seg_length);
+        pkt->flags |= AV_PKT_FLAG_KEY;
+        pkt->pos = pos;
+        pkt->pts = pts;
+        pkt->dts = dts;
+        av_dlog(s, "New IG packet @ 0x%lx: type %d, length %d\n", 
+               pos, seg_type, seg_length);
+    }
+    return res;
+}
+
+AVInputFormat ff_mnu_demuxer = {
+    .name           = "mnu",
+    .long_name      = NULL_IF_CONFIG_SMALL("HDMV Interactive Graphic menus format"),
+    .priv_data_size = sizeof(MNUContext),
+    .read_probe     = probe,
+    .read_header    = read_header,
+    .read_packet    = read_packet,
+    .read_close     = read_close,
+};
diff --git a/libavformat/mnuenc.c b/libavformat/mnuenc.c
new file mode 100644
index 0000000..41a1f7a
--- /dev/null
+++ b/libavformat/mnuenc.c
@@ -0,0 +1,66 @@
+/*
+ * MNU demuxer
+ * Copyright (c) 2012 David Girault
+ *
+ * This file is part of Libav.
+ *
+ * Libav 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.
+ *
+ * Libav 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 Libav; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/mathematics.h"
+#include "libavcodec/bytestream.h"
+#include "avformat.h"
+#include "internal.h"
+
+static int mnu_write_header(AVFormatContext *s)
+{
+    AVCodecContext *ctx;
+
+    if (s->nb_streams != 1) {
+        av_log(s, AV_LOG_ERROR, "Format supports only exactly one overlay stream\n");
+        return AVERROR(EINVAL);
+    }
+    ctx = s->streams[0]->codec;
+    if (ctx->codec_type != AVMEDIA_TYPE_OVERLAY || ctx->codec_id != CODEC_ID_HDMV_IGS_MENU) {
+        av_log(s, AV_LOG_ERROR, "Currently only HDMV IGS menu is supported!\n");
+        return AVERROR(EINVAL);
+    }
+    avpriv_set_pts_info(s->streams[0], 33, 1, 90000);
+    return 0;
+}
+
+static int mnu_write_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    AVIOContext *pb = s->pb;
+    avio_write(pb, "IG", 2);
+    avio_wb32(pb, pkt->pts); // TODO: need conversion
+    avio_wb32(pb, pkt->dts); // TODO: need conversion
+    avio_write(pb, pkt->data, pkt->size);
+    avio_flush(pb);
+    return 0;
+}
+
+AVOutputFormat ff_mnu_muxer = {
+    .name         = "mnu",
+    .long_name    = NULL_IF_CONFIG_SMALL("HDMV Interactive Graphic menus format"),
+    .extensions   = "mnu",
+    .flags          = AVFMT_NOTIMESTAMPS,
+    .audio_codec    = CODEC_ID_NONE,
+    .video_codec    = CODEC_ID_NONE,
+    .subtitle_codec = CODEC_ID_NONE,
+    .overlay_codec  = CODEC_ID_HDMV_IGS_MENU,
+    .write_header   = mnu_write_header,
+    .write_packet   = mnu_write_packet,
+};
diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index 1b3eb8b..b961e47 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -537,6 +537,7 @@ static const StreamType HDMV_types[] = {
     { 0x85, AVMEDIA_TYPE_AUDIO, CODEC_ID_DTS }, /* DTS HD */
     { 0x86, AVMEDIA_TYPE_AUDIO, CODEC_ID_DTS }, /* DTS HD MASTER*/
     { 0x90, AVMEDIA_TYPE_SUBTITLE, CODEC_ID_HDMV_PGS_SUBTITLE },
+    { 0x91, AVMEDIA_TYPE_OVERLAY, CODEC_ID_HDMV_IGS_MENU },
     { 0 },
 };
 
@@ -657,6 +658,7 @@ static void new_pes_packet(PESContext *pes, AVPacket *pkt)
     /* store position of first TS packet of this PES packet */
     pkt->pos = pes->ts_packet_pos;
     pkt->flags = pes->flags;
+    if (pes->stream_type == 0x91) pkt->flags |= AV_PKT_FLAG_KEY; // force KEY flag
 
     /* reset pts values */
     pes->pts = AV_NOPTS_VALUE;
-- 
1.7.9.5

