I was building this revision
https://github.com/libav/libav/tree/d6251368772a170987387bdc508433c8fcf54cda
on arm/termux it required --disable-asm but othervise worked.
I applied 4 patches I fished out of mailbox archive with plain cat | patch
- p1
patches attached.
procedure seems to be documented in patches themselves:
===
These changes add support for copying overlay streams.
So to extract IGS menu from m2ts file:
$ avconv -i source.m2ts -vn -an -sn output.mnu
To mux IGS menu with h264 video in ts:
$ avconv -i video.264 -i test.mnu -f mpegts out.ts
Still todo: update mpegts muxer to set correct PID so
output may be compatible to BDAV format.
Signed-off-by: David Girault <david at dhgirault.fr>
====
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
These changes add support for copying overlay streams.
So to extract IGS menu from m2ts file:
$ avconv -i source.m2ts -vn -an -sn output.mnu
To mux IGS menu with h264 video in ts:
$ avconv -i video.264 -i test.mnu -f mpegts out.ts
Still todo: update mpegts muxer to set correct PID so
output may be compatible to BDAV format.
Signed-off-by: David Girault <david at dhgirault.fr>
---
avconv.c | 26 ++++++++++++++++++++++++++
cmdutils.h | 1 +
2 files changed, 27 insertions(+)
diff --git a/avconv.c b/avconv.c
index 23ee164..481813f 100644
--- a/avconv.c
+++ b/avconv.c
@@ -341,6 +341,7 @@ typedef struct OptionsContext {
int video_disable;
int audio_disable;
int subtitle_disable;
+ int overlay_disable;
int data_disable;
/* indexed by output file stream index */
@@ -2440,6 +2441,7 @@ static int transcode_init(void)
codec->width = icodec->width;
codec->height = icodec->height;
break;
+ case AVMEDIA_TYPE_OVERLAY:
case AVMEDIA_TYPE_DATA:
case AVMEDIA_TYPE_ATTACHMENT:
break;
@@ -3305,6 +3307,7 @@ static void add_input_streams(OptionsContext *o, AVFormatContext *ic)
case AVMEDIA_TYPE_DATA:
case AVMEDIA_TYPE_SUBTITLE:
case AVMEDIA_TYPE_ATTACHMENT:
+ case AVMEDIA_TYPE_OVERLAY:
case AVMEDIA_TYPE_UNKNOWN:
break;
default:
@@ -3896,6 +3899,15 @@ static OutputStream *new_subtitle_stream(OptionsContext *o, AVFormatContext *oc)
return ost;
}
+static OutputStream *new_overlay_stream(OptionsContext *o, AVFormatContext *oc)
+{
+ OutputStream *ost;
+
+ ost = new_output_stream(o, oc, AVMEDIA_TYPE_OVERLAY);
+ ost->stream_copy = 1;
+ return ost;
+}
+
/* arg format is "output-stream-index:streamid-value". */
static int opt_streamid(OptionsContext *o, const char *opt, const char *arg)
{
@@ -4046,6 +4058,7 @@ static void opt_output_file(void *optctx, const char *filename)
case AVMEDIA_TYPE_VIDEO: o->video_disable = 1; break;
case AVMEDIA_TYPE_AUDIO: o->audio_disable = 1; break;
case AVMEDIA_TYPE_SUBTITLE: o->subtitle_disable = 1; break;
+ case AVMEDIA_TYPE_OVERLAY: o->overlay_disable = 1; break;
}
init_output_filter(ofilter, o, oc);
}
@@ -4098,6 +4111,15 @@ static void opt_output_file(void *optctx, const char *filename)
break;
}
}
+
+ /* overlay: pick first */
+ if (!o->overlay_disable && oc->oformat->overlay_codec != CODEC_ID_NONE) {
+ for (i = 0; i < nb_input_streams; i++)
+ if (input_streams[i]->st->codec->codec_type == AVMEDIA_TYPE_OVERLAY) {
+ NEW_STREAM(overlay, i);
+ break;
+ }
+ }
/* do something with data? */
} else {
for (i = 0; i < o->nb_stream_maps; i++) {
@@ -4135,6 +4157,7 @@ loop_end:
case AVMEDIA_TYPE_AUDIO: ost = new_audio_stream(o, oc); break;
case AVMEDIA_TYPE_SUBTITLE: ost = new_subtitle_stream(o, oc); break;
case AVMEDIA_TYPE_DATA: ost = new_data_stream(o, oc); break;
+ case AVMEDIA_TYPE_OVERLAY: ost = new_overlay_stream(o, oc); break;
case AVMEDIA_TYPE_ATTACHMENT: ost = new_attachment_stream(o, oc); break;
default:
av_log(NULL, AV_LOG_FATAL, "Cannot map stream #%d:%d - unsupported type.\n",
@@ -4786,6 +4809,9 @@ static const OptionDef options[] = {
{ "scodec", HAS_ARG | OPT_SUBTITLE | OPT_FUNC2, {(void*)opt_subtitle_codec}, "force subtitle codec ('copy' to copy stream)", "codec" },
{ "stag", HAS_ARG | OPT_EXPERT | OPT_SUBTITLE | OPT_FUNC2, {(void*)opt_subtitle_tag}, "force subtitle tag/fourcc", "fourcc/tag" },
+ /* overlay options */
+ { "on", OPT_BOOL | OPT_OVERLAY | OPT_OFFSET, {.off = OFFSET(overlay_disable)}, "disable overlay" },
+
/* grab options */
{ "isync", OPT_BOOL | OPT_EXPERT | OPT_GRAB, {(void*)&input_sync}, "sync read on input", "" },
diff --git a/cmdutils.h b/cmdutils.h
index 5dac130..bcc33c7 100644
--- a/cmdutils.h
+++ b/cmdutils.h
@@ -144,6 +144,7 @@ typedef struct {
an int containing element count in the array. */
#define OPT_TIME 0x10000
#define OPT_DOUBLE 0x20000
+#define OPT_OVERLAY 0x40000
union {
void *dst_ptr;
int (*func_arg)(const char *, const char *);
--
1.7.9.5
These changes allow testing Bluray IGS HDMV menu with avplay:
- add support for display overlay pictures,
- add support for openning a second input file as subpath,
- add some basic navigation commands to simulate and view
result of pages and buttons (p/u/d/l/r/enter keys).
One thing don't work well now: backup/restore of YUV buffer
to allow display of menu even if video stream reach the end
(like mpeg2video stream with only one frame used in static
menu)
version 4:
- Remove RGBA->YUVA palette convertion for overlay. Palette
already in YUVA CCIR now.
Signed-off-by: David Girault <david at dhgirault.fr>
---
avplay.c | 593 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
1 file changed, 542 insertions(+), 51 deletions(-)
diff --git a/avplay.c b/avplay.c
index c01e446..5eae23e 100644
--- a/avplay.c
+++ b/avplay.c
@@ -118,6 +118,11 @@ typedef struct SubPicture {
AVSubtitle sub;
} SubPicture;
+typedef struct OverlayPicture {
+ double pts; /* presentation time stamp for this picture */
+ AVOverlay overlay;
+} OverlayPicture;
+
enum {
AV_SYNC_AUDIO_MASTER, /* default choice */
AV_SYNC_VIDEO_MASTER,
@@ -188,6 +193,23 @@ typedef struct VideoState {
SDL_mutex *subpq_mutex;
SDL_cond *subpq_cond;
+ char overlay_subpath[1024];
+ int overlay_use_subpath;
+ AVFormatContext *overlay_ic;
+
+ SDL_Thread *overlay_tid;
+ int overlay_stream;
+ int overlay_stream_changed;
+ AVStream *overlay_st;
+ SDL_Overlay *overlay_bmp; ///< Used to display an overlay picture when no new video picture is available
+ double overlay_last_pts;
+ PacketQueue overlayq;
+ OverlayPicture ovepq[SUBPICTURE_QUEUE_SIZE];
+ int ovepq_size, ovepq_rindex, ovepq_windex;
+ SDL_mutex *ovepq_mutex;
+ SDL_cond *ovepq_cond;
+
+ double aspect_ratio; ///< calculated when video picture is displayed
double frame_timer;
double frame_last_pts;
double frame_last_delay;
@@ -208,6 +230,7 @@ typedef struct VideoState {
// QETimer *video_timer;
char filename[1024];
+
int width, height, xleft, ytop;
PtsCorrectionContext pts_ctx;
@@ -225,10 +248,12 @@ typedef struct VideoState {
} VideoState;
static void show_help(void);
+static void stream_handle_event(VideoState *is, SDL_Event *event);
/* options specified by the user */
static AVInputFormat *file_iformat;
static const char *input_filename;
+static const char *subpath_filename;
static const char *window_title;
static int fs_screen_width;
static int fs_screen_height;
@@ -240,6 +265,7 @@ static int wanted_stream[AVMEDIA_TYPE_NB] = {
[AVMEDIA_TYPE_AUDIO] = -1,
[AVMEDIA_TYPE_VIDEO] = -1,
[AVMEDIA_TYPE_SUBTITLE] = -1,
+ [AVMEDIA_TYPE_OVERLAY] = -1,
};
static int seek_by_bytes = -1;
static int display_disable;
@@ -442,7 +468,9 @@ static inline void fill_rectangle(SDL_Surface *screen,
#define BPP 1
-static void blend_subrect(AVPicture *dst, const AVSubtitleRect *rect, int imgw, int imgh)
+static void blend_rect(AVPicture *dst, int imgw, int imgh,
+ int rw, int rh, int rx, int ry,
+ Uint8 *pixels, Uint8 *palette, int linesize)
{
int wrap, wrap3, width2, skip2;
int y, u, v, a, u1, v1, a1, w, h;
@@ -451,10 +479,10 @@ static void blend_subrect(AVPicture *dst, const AVSubtitleRect *rect, int imgw,
const uint32_t *pal;
int dstx, dsty, dstw, dsth;
- dstw = av_clip(rect->w, 0, imgw);
- dsth = av_clip(rect->h, 0, imgh);
- dstx = av_clip(rect->x, 0, imgw - dstw);
- dsty = av_clip(rect->y, 0, imgh - dsth);
+ dstw = av_clip(rw, 0, imgw);
+ dsth = av_clip(rh, 0, imgh);
+ dstx = av_clip(rx, 0, imgw - dstw);
+ dsty = av_clip(ry, 0, imgh - dsth);
lum = dst->data[0] + dsty * dst->linesize[0];
cb = dst->data[1] + (dsty >> 1) * dst->linesize[1];
cr = dst->data[2] + (dsty >> 1) * dst->linesize[2];
@@ -462,9 +490,9 @@ static void blend_subrect(AVPicture *dst, const AVSubtitleRect *rect, int imgw,
width2 = ((dstw + 1) >> 1) + (dstx & ~dstw & 1);
skip2 = dstx >> 1;
wrap = dst->linesize[0];
- wrap3 = rect->pict.linesize[0];
- p = rect->pict.data[0];
- pal = (const uint32_t *)rect->pict.data[1]; /* Now in YCrCb! */
+ wrap3 = linesize;
+ p = pixels;
+ pal = (const uint32_t *)palette; /* Now in YCrCb! */
if (dsty & 1) {
lum += dstx;
@@ -642,17 +670,181 @@ static void blend_subrect(AVPicture *dst, const AVSubtitleRect *rect, int imgw,
}
}
+static inline void blend_subrect(AVPicture *dst, const AVSubtitleRect *rect, int imgw, int imgh)
+{
+ blend_rect(dst, imgw, imgh,
+ rect->w, rect->h, rect->x, rect->y,
+ rect->pict.data[0],
+ rect->pict.data[1],
+ rect->pict.linesize[0]);
+}
+
+static inline void blend_overlayrect(AVPicture *dst, const AVOverlayRect *rect, int imgw, int imgh)
+{
+ blend_rect(dst, imgw, imgh,
+ rect->w, rect->h, rect->x, rect->y,
+ rect->pict.data[0],
+ rect->pict.data[1],
+ rect->pict.linesize[0]);
+}
+
static void free_subpicture(SubPicture *sp)
{
avsubtitle_free(&sp->sub);
}
+static void free_overlaypicture(OverlayPicture *op)
+{
+ avoverlay_free(&op->overlay);
+}
+
+
+static inline void overlay_img_backup(SDL_Overlay *bmp, Uint8 **bakpixels)
+{
+ int i;
+ for (i=0;i<bmp->planes;i++) {
+ bakpixels[i] = av_malloc(bmp->pitches[i]);
+ memcpy(bakpixels[i], bmp->pixels[i], bmp->pitches[i]);
+ }
+}
+static inline void overlay_img_restore(SDL_Overlay *bmp, Uint8 **bakpixels)
+{
+ int i;
+ for (i=0;i<bmp->planes;i++) {
+ memcpy(bmp->pixels[i], bakpixels[i], bmp->pitches[i]);
+ av_freep(&bakpixels[i]);
+ }
+}
+
+static void overlay_image_display(VideoState *is, SDL_Overlay *bmp, SDL_Rect *rectp)
+{
+ // if rectp not NULL, we are called from video_image_display, after video picture was displayed.
+ // need to backup altered surfaces but don't require to restore previous saved surfaces.
+ // if rectp is NULL, we need to restore previous saved surfaces before drawing new overlay pic.
+ OverlayPicture *sp;
+ AVPicture pict;
+ SDL_Rect rect;
+ int i, deleted=0;
+
+ if (rectp == NULL) {
+ int width, height, x, y;
+ //
+ height = is->height;
+ width = ((int)rint(height * is->aspect_ratio)) & ~1;
+ if (width > is->width) {
+ width = is->width;
+ height = ((int)rint(width / is->aspect_ratio)) & ~1;
+ }
+ x = (is->width - width) / 2;
+ y = (is->height - height) / 2;
+ rect.x = is->xleft + x;
+ rect.y = is->ytop + y;
+ rect.w = width;
+ rect.h = height;
+ rectp = ▭
+ }
+
+ if (is->overlay_st)
+ {
+ int num = 2; // number of overlay pictures to keep + 1
+ if (is->overlay_stream_changed) num--; // purge all on changed stream
+
+ while (is->ovepq_size >= num) {
+ if (is->ovepq_size == num && !bmp && num == 1) {
+ /* previous displayed overlay pictures need to be removed from display */
+ av_dlog(NULL, "Remove older overlay picture by restoring original saved one: %dx%d@%d:%d\n", rectp->w,rectp->h,rectp->x,rectp->y);
+ SDL_DisplayYUVOverlay(is->overlay_bmp, rectp);
+ }
+ free_overlaypicture(&is->ovepq[is->ovepq_rindex]);
+ /* update queue size and signal for next picture */
+ if (++is->ovepq_rindex == SUBPICTURE_QUEUE_SIZE)
+ is->ovepq_rindex = 0;
+ is->ovepq_size--;
+ deleted++;
+ }
+
+ if (is->ovepq_size > 0)
+ {
+ sp = &is->ovepq[is->ovepq_rindex];
+
+ /*if (is->video_current_pts > (sp->pts + ((float) sp->overlay.end_display_time / 1000))) {
+ if (!bmp) {
+ // restore last backup
+ fprintf(stderr, "Overlay picture timed-out, !\n");
+ SDL_DisplayYUVOverlay(is->overlay_bmp, rectp);
+ }
+ else {
+ // do nothing, caller will display unmodified picture
+ }
+ }
+ else */
+ if (is->video_current_pts >= sp->pts + ((float) sp->overlay.start_display_time / 1000))
+ {
+ Uint8 *bakpixels[3]; // 3 pixels planes max
+ if (bmp) {
+ // we have a new source, backup it then use it directly
+ av_dlog(NULL, "Display current overlay picture on new video picture (%dx%d@%d:%d)\n", rectp->w,rectp->h,rectp->x,rectp->y);
+ SDL_LockYUVOverlay(bmp);
+ overlay_img_backup(bmp, bakpixels); // first, save unmodified picture
+
+ // apply overlay pictures
+ pict.data[0] = bmp->pixels[0];
+ pict.data[1] = bmp->pixels[2];
+ pict.data[2] = bmp->pixels[1];
+ pict.linesize[0] = bmp->pitches[0];
+ pict.linesize[1] = bmp->pitches[2];
+ pict.linesize[2] = bmp->pitches[1];
+ for (i = 0; i < sp->overlay.num_rects; i++)
+ blend_overlayrect(&pict, sp->overlay.rects[i], bmp->w, bmp->h);
+ SDL_UnlockYUVOverlay (bmp);
+
+ // modified bmp displayed by caller, don't do it now
+
+ // store backup in our overlay_bmp
+ SDL_LockYUVOverlay(is->overlay_bmp);
+ overlay_img_restore(is->overlay_bmp, bakpixels);
+ SDL_UnlockYUVOverlay(is->overlay_bmp);
+ is->overlay_last_pts = is->video_current_pts;
+ }
+ else if (deleted) {
+ // we don't have a source, duplicate backup and use it
+ av_dlog(NULL, "Display new overlay picture on old video picture (%dx%d@%d:%d)\n", rectp->w,rectp->h,rectp->x,rectp->y);
+ SDL_LockYUVOverlay (is->overlay_bmp);
+ overlay_img_backup(is->overlay_bmp, bakpixels);
+
+ // apply overlay pictures
+ pict.data[0] = is->overlay_bmp->pixels[0];
+ pict.data[1] = is->overlay_bmp->pixels[2];
+ pict.data[2] = is->overlay_bmp->pixels[1];
+ pict.linesize[0] = is->overlay_bmp->pitches[0];
+ pict.linesize[1] = is->overlay_bmp->pitches[2];
+ pict.linesize[2] = is->overlay_bmp->pitches[1];
+ for (i = 0; i < sp->overlay.num_rects; i++)
+ blend_overlayrect(&pict, sp->overlay.rects[i], is->overlay_bmp->w, is->overlay_bmp->h);
+ SDL_UnlockYUVOverlay (is->overlay_bmp);
+
+ // display it right now
+ SDL_DisplayYUVOverlay(is->overlay_bmp, rectp);
+
+ // restore backup in our overlay_bmp
+ SDL_LockYUVOverlay (is->overlay_bmp);
+ overlay_img_restore(is->overlay_bmp, bakpixels);
+ SDL_UnlockYUVOverlay (is->overlay_bmp);
+ }
+ }
+ else {
+ av_log(NULL, AV_LOG_DEBUG, "Don't display new overlay picture because of pts: %lf < %lf+(%d/1000)\n",
+ is->video_current_pts, sp->pts, sp->overlay.start_display_time);
+ }
+ }
+ }
+}
+
static void video_image_display(VideoState *is)
{
VideoPicture *vp;
SubPicture *sp;
AVPicture pict;
- float aspect_ratio;
int width, height, x, y;
SDL_Rect rect;
int i;
@@ -661,22 +853,22 @@ static void video_image_display(VideoState *is)
if (vp->bmp) {
#if CONFIG_AVFILTER
if (vp->picref->video->pixel_aspect.num == 0)
- aspect_ratio = 0;
+ is->aspect_ratio = 0;
else
- aspect_ratio = av_q2d(vp->picref->video->pixel_aspect);
+ is->aspect_ratio = av_q2d(vp->picref->video->pixel_aspect);
#else
/* XXX: use variable in the frame */
if (is->video_st->sample_aspect_ratio.num)
- aspect_ratio = av_q2d(is->video_st->sample_aspect_ratio);
+ is->aspect_ratio = av_q2d(is->video_st->sample_aspect_ratio);
else if (is->video_st->codec->sample_aspect_ratio.num)
- aspect_ratio = av_q2d(is->video_st->codec->sample_aspect_ratio);
+ is->aspect_ratio = av_q2d(is->video_st->codec->sample_aspect_ratio);
else
- aspect_ratio = 0;
+ is->aspect_ratio = 0;
#endif
- if (aspect_ratio <= 0.0)
- aspect_ratio = 1.0;
- aspect_ratio *= (float)vp->width / (float)vp->height;
+ if (is->aspect_ratio <= 0.0)
+ is->aspect_ratio = 1.0;
+ is->aspect_ratio *= (float)vp->width / (float)vp->height;
if (is->subtitle_st)
{
@@ -705,13 +897,12 @@ static void video_image_display(VideoState *is)
}
}
-
/* XXX: we suppose the screen has a 1.0 pixel ratio */
height = is->height;
- width = ((int)rint(height * aspect_ratio)) & ~1;
+ width = ((int)rint(height * is->aspect_ratio)) & ~1;
if (width > is->width) {
width = is->width;
- height = ((int)rint(width / aspect_ratio)) & ~1;
+ height = ((int)rint(width / is->aspect_ratio)) & ~1;
}
x = (is->width - width) / 2;
y = (is->height - height) / 2;
@@ -720,7 +911,11 @@ static void video_image_display(VideoState *is)
rect.y = is->ytop + y;
rect.w = width;
rect.h = height;
+
+ overlay_image_display(is, vp->bmp, &rect);
+
SDL_DisplayYUVOverlay(vp->bmp, &rect);
+
}
}
@@ -1090,7 +1285,7 @@ static void video_refresh_timer(void *opaque)
if (is->video_st) {
retry:
if (is->pictq_size == 0) {
- // nothing to do, no picture to display in the que
+ // nothing to do, no picture to display in the queue
} else {
double time = av_gettime() / 1000000.0;
double next_target;
@@ -1182,6 +1377,12 @@ retry:
SDL_CondSignal(is->pictq_cond);
SDL_UnlockMutex(is->pictq_mutex);
}
+
+ /* overlay may work even if no more video picture to display */
+ if (is->overlay_st) {
+ overlay_image_display(is, NULL, NULL);
+ }
+
} else if (is->audio_st) {
/* draw the next audio frame */
@@ -1195,7 +1396,7 @@ retry:
if (show_status) {
static int64_t last_time;
int64_t cur_time;
- int aqsize, vqsize, sqsize;
+ int aqsize, vqsize, sqsize, oqsize;
double av_diff;
cur_time = av_gettime();
@@ -1203,18 +1404,21 @@ retry:
aqsize = 0;
vqsize = 0;
sqsize = 0;
+ oqsize = 0;
if (is->audio_st)
aqsize = is->audioq.size;
if (is->video_st)
vqsize = is->videoq.size;
if (is->subtitle_st)
sqsize = is->subtitleq.size;
+ if (is->overlay_st)
+ oqsize = is->overlayq.size;
av_diff = 0;
if (is->audio_st && is->video_st)
av_diff = get_audio_clock(is) - get_video_clock(is);
- printf("%7.2f A-V:%7.3f s:%3.1f aq=%5dKB vq=%5dKB sq=%5dB f=%"PRId64"/%"PRId64" \r",
+ printf("%7.2f A-V:%7.3f s:%3.1f aq=%5dKB vq=%5dKB sq=%5dB oq=%5dB f=%"PRId64"/%"PRId64" \r",
get_master_clock(is), av_diff, FFMAX(is->skip_frames - 1, 0), aqsize / 1024,
- vqsize / 1024, sqsize, is->pts_ctx.num_faulty_dts, is->pts_ctx.num_faulty_pts);
+ vqsize / 1024, sqsize, oqsize, is->pts_ctx.num_faulty_dts, is->pts_ctx.num_faulty_pts);
fflush(stdout);
last_time = cur_time;
}
@@ -1248,6 +1452,12 @@ static void stream_close(VideoState *is)
SDL_DestroyCond(is->pictq_cond);
SDL_DestroyMutex(is->subpq_mutex);
SDL_DestroyCond(is->subpq_cond);
+ SDL_DestroyMutex(is->ovepq_mutex);
+ SDL_DestroyCond(is->ovepq_cond);
+ if (is->overlay_bmp) {
+ SDL_FreeYUVOverlay(is->overlay_bmp);
+ is->overlay_bmp = NULL;
+ }
#if !CONFIG_AVFILTER
if (is->img_convert_ctx)
sws_freeContext(is->img_convert_ctx);
@@ -1311,6 +1521,20 @@ static void alloc_picture(void *opaque)
do_exit();
}
+ if (!is->overlay_bmp && is->overlay_stream>=0) {
+ is->overlay_bmp = SDL_CreateYUVOverlay(vp->width, vp->height,
+ SDL_YV12_OVERLAY,
+ screen);
+ if (!is->overlay_bmp || is->overlay_bmp->pitches[0] < vp->width) {
+ fprintf(stderr, "Error: the video system does not support an image\n"
+ "size of %dx%d pixels. Try using -vf \"scale=w:h\"\n"
+ "to reduce the image size.\n", vp->width, vp->height );
+ do_exit();
+ }
+
+ }
+
+
SDL_LockMutex(is->pictq_mutex);
vp->allocated = 1;
SDL_CondSignal(is->pictq_cond);
@@ -1781,6 +2005,66 @@ static int subtitle_thread(void *arg)
return 0;
}
+static int overlay_thread(void *arg)
+{
+ VideoState *is = arg;
+ OverlayPicture *sp;
+ AVPacket pkt1, *pkt = &pkt1;
+ int got_overlay;
+ double pts;
+ //int i, j;
+ //int r, g, b, y, u, v, a;
+
+ for (;;) {
+ while (is->paused && !is->overlayq.abort_request) {
+ SDL_Delay(10);
+ }
+ if (packet_queue_get(&is->overlayq, pkt, 1) < 0)
+ break;
+
+ if (pkt->data == flush_pkt.data) {
+ avcodec_flush_buffers(is->overlay_st->codec);
+ continue;
+ }
+ SDL_LockMutex(is->ovepq_mutex);
+ while (is->ovepq_size >= SUBPICTURE_QUEUE_SIZE &&
+ !is->overlayq.abort_request) {
+ SDL_CondWait(is->ovepq_cond, is->ovepq_mutex);
+ }
+ SDL_UnlockMutex(is->ovepq_mutex);
+
+ if (is->overlayq.abort_request)
+ return 0;
+
+ sp = &is->ovepq[is->ovepq_windex];
+
+ /* NOTE: ipts is the PTS of the _first_ picture beginning in
+ this packet, if any */
+ pts = 0;
+ if (pkt->pts != AV_NOPTS_VALUE)
+ pts = av_q2d(is->overlay_st->time_base) * pkt->pts;
+
+ avcodec_decode_overlay2(is->overlay_st->codec, &sp->overlay,
+ &got_overlay, pkt);
+
+ if (got_overlay && sp->overlay.format == 0) {
+ sp->pts = pts;
+ /* now we can update the picture count */
+ if (++is->ovepq_windex == SUBPICTURE_QUEUE_SIZE)
+ is->ovepq_windex = 0;
+
+ SDL_LockMutex(is->ovepq_mutex);
+ is->ovepq_size++;
+ SDL_UnlockMutex(is->ovepq_mutex);
+
+ /* TODO: if got overlay with extra data, may be we want to execute
+ these commands within an HDMV virtual machine */
+ }
+ av_free_packet(pkt);
+ }
+ return 0;
+}
+
/* copy samples for viewing in editor window */
static void update_sample_display(VideoState *is, short *samples, int samples_size)
{
@@ -2071,17 +2355,22 @@ static void sdl_audio_callback(void *opaque, Uint8 *stream, int len)
static int stream_component_open(VideoState *is, int stream_index)
{
AVFormatContext *ic = is->ic;
+ AVFormatContext *sc = is->overlay_ic;
AVCodecContext *avctx;
AVCodec *codec;
+ AVStream *stream;
SDL_AudioSpec wanted_spec, spec;
AVDictionary *opts;
AVDictionaryEntry *t = NULL;
- if (stream_index < 0 || stream_index >= ic->nb_streams)
+ if (stream_index < 0 || stream_index >= (ic->nb_streams + (sc?sc->nb_streams:0)))
return -1;
- avctx = ic->streams[stream_index]->codec;
-
- opts = filter_codec_opts(codec_opts, avctx->codec_id, ic, ic->streams[stream_index]);
+ if (stream_index >= ic->nb_streams)
+ stream = sc->streams[stream_index-ic->nb_streams];
+ else
+ stream = ic->streams[stream_index];
+ avctx = stream->codec;
+ opts = filter_codec_opts(codec_opts, avctx->codec_id, (stream_index>=ic->nb_streams ? sc : ic), stream);
codec = avcodec_find_decoder(avctx->codec_id);
avctx->debug_mv = debug_mv;
@@ -2137,11 +2426,12 @@ static int stream_component_open(VideoState *is, int stream_index)
is->resample_channel_layout = is->sdl_channel_layout;
}
- ic->streams[stream_index]->discard = AVDISCARD_DEFAULT;
+ stream->discard = AVDISCARD_DEFAULT;
+
switch (avctx->codec_type) {
case AVMEDIA_TYPE_AUDIO:
is->audio_stream = stream_index;
- is->audio_st = ic->streams[stream_index];
+ is->audio_st = stream;
is->audio_buf_size = 0;
is->audio_buf_index = 0;
@@ -2158,18 +2448,22 @@ static int stream_component_open(VideoState *is, int stream_index)
break;
case AVMEDIA_TYPE_VIDEO:
is->video_stream = stream_index;
- is->video_st = ic->streams[stream_index];
-
+ is->video_st = stream;
packet_queue_init(&is->videoq);
is->video_tid = SDL_CreateThread(video_thread, is);
break;
case AVMEDIA_TYPE_SUBTITLE:
is->subtitle_stream = stream_index;
- is->subtitle_st = ic->streams[stream_index];
+ is->subtitle_st = stream;
packet_queue_init(&is->subtitleq);
-
is->subtitle_tid = SDL_CreateThread(subtitle_thread, is);
break;
+ case AVMEDIA_TYPE_OVERLAY:
+ is->overlay_stream = stream_index;
+ is->overlay_st = stream;
+ packet_queue_init(&is->overlayq);
+ is->overlay_tid = SDL_CreateThread(overlay_thread, is);
+ break;
default:
break;
}
@@ -2179,11 +2473,18 @@ static int stream_component_open(VideoState *is, int stream_index)
static void stream_component_close(VideoState *is, int stream_index)
{
AVFormatContext *ic = is->ic;
+ AVFormatContext *sc = is->overlay_ic;
AVCodecContext *avctx;
+ AVStream *stream;
- if (stream_index < 0 || stream_index >= ic->nb_streams)
+ if (stream_index < 0 || stream_index >= (ic->nb_streams + (sc?sc->nb_streams:0)))
return;
- avctx = ic->streams[stream_index]->codec;
+ if (stream_index >= ic->nb_streams)
+ stream = sc->streams[stream_index-ic->nb_streams];
+ else
+ stream = ic->streams[stream_index];
+
+ avctx = stream->codec;
switch (avctx->codec_type) {
case AVMEDIA_TYPE_AUDIO:
@@ -2234,11 +2535,26 @@ static void stream_component_close(VideoState *is, int stream_index)
packet_queue_end(&is->subtitleq);
break;
+ case AVMEDIA_TYPE_OVERLAY:
+ packet_queue_abort(&is->overlayq);
+
+ /* note: we also signal this mutex to make sure we deblock the
+ video thread in all cases */
+ SDL_LockMutex(is->ovepq_mutex);
+ is->overlay_stream_changed = 1;
+
+ SDL_CondSignal(is->ovepq_cond);
+ SDL_UnlockMutex(is->ovepq_mutex);
+
+ SDL_WaitThread(is->overlay_tid, NULL);
+
+ packet_queue_end(&is->overlayq);
+ break;
default:
break;
}
- ic->streams[stream_index]->discard = AVDISCARD_ALL;
+ stream->discard = AVDISCARD_ALL;
avcodec_close(avctx);
free_buffer_pool(&is->buffer_pool);
switch (avctx->codec_type) {
@@ -2254,6 +2570,10 @@ static void stream_component_close(VideoState *is, int stream_index)
is->subtitle_st = NULL;
is->subtitle_stream = -1;
break;
+ case AVMEDIA_TYPE_OVERLAY:
+ is->overlay_st = NULL;
+ is->overlay_stream = -1;
+ break;
default:
break;
}
@@ -2273,6 +2593,7 @@ static int decode_thread(void *arg)
{
VideoState *is = arg;
AVFormatContext *ic = NULL;
+ AVFormatContext *sc = NULL;
int err, i, ret;
int st_index[AVMEDIA_TYPE_NB];
AVPacket pkt1, *pkt = &pkt1;
@@ -2286,6 +2607,7 @@ static int decode_thread(void *arg)
is->video_stream = -1;
is->audio_stream = -1;
is->subtitle_stream = -1;
+ is->overlay_stream = -1;
global_video_state = is;
@@ -2320,6 +2642,36 @@ static int decode_thread(void *arg)
av_dict_free(&opts[i]);
av_freep(&opts);
+ if (is->overlay_use_subpath) {
+ sc = avformat_alloc_context();
+ sc->interrupt_callback.callback = decode_interrupt_cb;
+ err = avformat_open_input(&sc, is->overlay_subpath, NULL, &format_opts);
+ if (err < 0) {
+ print_error(is->overlay_subpath, err);
+ ret = -1;
+ goto fail;
+ }
+ if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
+ av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
+ ret = AVERROR_OPTION_NOT_FOUND;
+ goto fail;
+ }
+ is->overlay_ic = sc;
+
+ opts = setup_find_stream_info_opts(sc, codec_opts);
+ orig_nb_streams = sc->nb_streams;
+
+ err = avformat_find_stream_info(sc, opts);
+ if (err < 0) {
+ fprintf(stderr, "%s: could not find codec parameters\n", is->filename);
+ ret = -1;
+ goto fail;
+ }
+ for (i = 0; i < orig_nb_streams; i++)
+ av_dict_free(&opts[i]);
+ av_freep(&opts);
+ }
+
if (ic->pb)
ic->pb->eof_reached = 0; // FIXME hack, avplay maybe should not use url_feof() to test for the end
@@ -2343,6 +2695,10 @@ static int decode_thread(void *arg)
for (i = 0; i < ic->nb_streams; i++)
ic->streams[i]->discard = AVDISCARD_ALL;
+ if (sc)
+ for (i = 0; i < sc->nb_streams; i++)
+ sc->streams[i]->discard = AVDISCARD_ALL;
+
if (!video_disable)
st_index[AVMEDIA_TYPE_VIDEO] =
av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO,
@@ -2353,7 +2709,7 @@ static int decode_thread(void *arg)
wanted_stream[AVMEDIA_TYPE_AUDIO],
st_index[AVMEDIA_TYPE_VIDEO],
NULL, 0);
- if (!video_disable)
+ if (!video_disable) {
st_index[AVMEDIA_TYPE_SUBTITLE] =
av_find_best_stream(ic, AVMEDIA_TYPE_SUBTITLE,
wanted_stream[AVMEDIA_TYPE_SUBTITLE],
@@ -2361,8 +2717,35 @@ static int decode_thread(void *arg)
st_index[AVMEDIA_TYPE_AUDIO] :
st_index[AVMEDIA_TYPE_VIDEO]),
NULL, 0);
+ if (!is->overlay_use_subpath) {
+ st_index[AVMEDIA_TYPE_OVERLAY] =
+ av_find_best_stream(ic, AVMEDIA_TYPE_OVERLAY,
+ wanted_stream[AVMEDIA_TYPE_OVERLAY],
+ (st_index[AVMEDIA_TYPE_AUDIO] >= 0 ?
+ st_index[AVMEDIA_TYPE_AUDIO] :
+ st_index[AVMEDIA_TYPE_VIDEO]),
+ NULL, 0);
+ }
+ else {
+ st_index[AVMEDIA_TYPE_OVERLAY] =
+ av_find_best_stream(sc, AVMEDIA_TYPE_OVERLAY,
+ wanted_stream[AVMEDIA_TYPE_OVERLAY],
+ (st_index[AVMEDIA_TYPE_AUDIO] >= 0 ?
+ st_index[AVMEDIA_TYPE_AUDIO] :
+ st_index[AVMEDIA_TYPE_VIDEO]),
+ NULL, 0);
+ // found one, adjust index
+ if (st_index[AVMEDIA_TYPE_OVERLAY] >= 0) {
+ st_index[AVMEDIA_TYPE_OVERLAY] += ic->nb_streams;
+ }
+ }
+ }
+
if (show_status) {
av_dump_format(ic, 0, is->filename, 0);
+ if (is->overlay_use_subpath) {
+ av_dump_format(sc, 1, is->overlay_subpath, 0);
+ }
}
/* open the streams */
@@ -2384,6 +2767,10 @@ static int decode_thread(void *arg)
stream_component_open(is, st_index[AVMEDIA_TYPE_SUBTITLE]);
}
+ if (st_index[AVMEDIA_TYPE_OVERLAY] >= 0) {
+ stream_component_open(is, st_index[AVMEDIA_TYPE_OVERLAY]);
+ }
+
if (is->video_stream < 0 && is->audio_stream < 0) {
fprintf(stderr, "%s: could not open codecs\n", is->filename);
ret = -1;
@@ -2419,6 +2806,13 @@ static int decode_thread(void *arg)
if (ret < 0) {
fprintf(stderr, "%s: error while seeking\n", is->ic->filename);
} else {
+ if (is->overlay_ic) {
+ // also seek subpath if
+ ret = avformat_seek_file(is->overlay_ic, -1, seek_min, seek_target, seek_max, is->seek_flags);
+ if (ret < 0) {
+ fprintf(stderr, "%s: error while seeking\n", is->ic->filename);
+ }
+ }
if (is->audio_stream >= 0) {
packet_queue_flush(&is->audioq);
packet_queue_put(&is->audioq, &flush_pkt);
@@ -2427,20 +2821,26 @@ static int decode_thread(void *arg)
packet_queue_flush(&is->subtitleq);
packet_queue_put(&is->subtitleq, &flush_pkt);
}
+ if (is->overlay_stream >= 0) {
+ packet_queue_flush(&is->overlayq);
+ packet_queue_put(&is->overlayq, &flush_pkt);
+ }
if (is->video_stream >= 0) {
packet_queue_flush(&is->videoq);
packet_queue_put(&is->videoq, &flush_pkt);
}
}
+
is->seek_req = 0;
eof = 0;
}
/* if the queue are full, no need to read more */
- if ( is->audioq.size + is->videoq.size + is->subtitleq.size > MAX_QUEUE_SIZE
+ if ( is->audioq.size + is->videoq.size + is->subtitleq.size + is->overlayq.size > MAX_QUEUE_SIZE
|| ( (is->audioq .size > MIN_AUDIOQ_SIZE || is->audio_stream < 0)
&& (is->videoq .nb_packets > MIN_FRAMES || is->video_stream < 0)
- && (is->subtitleq.nb_packets > MIN_FRAMES || is->subtitle_stream < 0))) {
+ && (is->subtitleq.nb_packets > MIN_FRAMES || is->subtitle_stream < 0)
+ && (is->overlayq.nb_packets > MIN_FRAMES || is->overlay_stream < 0))) {
/* wait 10 ms */
SDL_Delay(10);
continue;
@@ -2462,7 +2862,7 @@ static int decode_thread(void *arg)
packet_queue_put(&is->audioq, pkt);
}
SDL_Delay(10);
- if (is->audioq.size + is->videoq.size + is->subtitleq.size == 0) {
+ if (is->audioq.size + is->videoq.size + is->subtitleq.size + is->overlayq.size == 0) {
if (loop != 1 && (!loop || --loop)) {
stream_seek(cur_stream, start_time != AV_NOPTS_VALUE ? start_time : 0, 0, 0);
} else if (autoexit) {
@@ -2493,9 +2893,33 @@ static int decode_thread(void *arg)
packet_queue_put(&is->videoq, pkt);
} else if (pkt->stream_index == is->subtitle_stream && pkt_in_play_range) {
packet_queue_put(&is->subtitleq, pkt);
+ } else if (pkt->stream_index == is->overlay_stream && pkt_in_play_range) {
+ packet_queue_put(&is->overlayq, pkt);
} else {
av_free_packet(pkt);
}
+
+ if (is->overlay_ic) {
+ ret = av_read_frame(is->overlay_ic, pkt);
+ if (ret < 0) {
+ if (is->overlay_ic->pb && is->overlay_ic->pb->error)
+ break;
+ continue;
+ }
+ /* check if packet is in play range specified by user, then queue, otherwise discard */
+ pkt_in_play_range = duration == AV_NOPTS_VALUE ||
+ (pkt->pts - ic->streams[pkt->stream_index]->start_time) *
+ av_q2d(ic->streams[pkt->stream_index]->time_base) -
+ (double)(start_time != AV_NOPTS_VALUE ? start_time : 0) / 1000000
+ <= ((double)duration / 1000000);
+ if (pkt->stream_index == (is->subtitle_stream-ic->nb_streams) && pkt_in_play_range) {
+ packet_queue_put(&is->subtitleq, pkt);
+ } else if (pkt->stream_index == (is->overlay_stream-ic->nb_streams) && pkt_in_play_range) {
+ packet_queue_put(&is->overlayq, pkt);
+ } else {
+ av_free_packet(pkt);
+ }
+ }
}
/* wait until the end */
while (!is->abort_request) {
@@ -2514,9 +2938,14 @@ static int decode_thread(void *arg)
stream_component_close(is, is->video_stream);
if (is->subtitle_stream >= 0)
stream_component_close(is, is->subtitle_stream);
+ if (is->overlay_stream >= 0)
+ stream_component_close(is, is->overlay_stream);
if (is->ic) {
avformat_close_input(&is->ic);
}
+ if (is->overlay_ic) {
+ avformat_close_input(&is->overlay_ic);
+ }
if (ret != 0) {
SDL_Event event;
@@ -2528,7 +2957,7 @@ static int decode_thread(void *arg)
return 0;
}
-static VideoState *stream_open(const char *filename, AVInputFormat *iformat)
+static VideoState *stream_open(const char *filename, const char *subpath, AVInputFormat *iformat)
{
VideoState *is;
@@ -2536,6 +2965,10 @@ static VideoState *stream_open(const char *filename, AVInputFormat *iformat)
if (!is)
return NULL;
av_strlcpy(is->filename, filename, sizeof(is->filename));
+ if (subpath) {
+ av_strlcpy(is->overlay_subpath, subpath, sizeof(is->overlay_subpath));
+ is->overlay_use_subpath = 1;
+ }
is->iformat = iformat;
is->ytop = 0;
is->xleft = 0;
@@ -2547,6 +2980,9 @@ static VideoState *stream_open(const char *filename, AVInputFormat *iformat)
is->subpq_mutex = SDL_CreateMutex();
is->subpq_cond = SDL_CreateCond();
+ is->ovepq_mutex = SDL_CreateMutex();
+ is->ovepq_cond = SDL_CreateCond();
+
is->av_sync_type = av_sync_type;
is->parse_tid = SDL_CreateThread(decode_thread, is);
if (!is->parse_tid) {
@@ -2556,6 +2992,34 @@ static VideoState *stream_open(const char *filename, AVInputFormat *iformat)
return is;
}
+static void stream_handle_event(VideoState *is, SDL_Event *event)
+{
+ AVPacket pkt1, *pkt = &pkt1;
+ char key = 0;
+ if (event != NULL && event->type == SDL_KEYDOWN) switch(event->key.keysym.sym) {
+ case SDLK_RETURN: key = 13; break; // activate current button
+ case SDLK_u: key = 'u'; break; // up
+ case SDLK_d: key = 'd'; break; // down
+ case SDLK_l: key = 'l'; break; // left
+ case SDLK_r: key = 'r'; break; // right
+ case SDLK_p: key = 'p'; break; // roll pages
+ }
+
+ //pass key to decoder using a fake packet
+ if (is->overlay_stream >= 0) {
+ av_init_packet(pkt);
+ pkt->size = (key != 0 ? 4 : 3);
+ pkt->data = av_mallocz(pkt->size);
+ *(pkt->data) = 0x80;
+ *(pkt->data+1) = 0x00;
+ *(pkt->data+2) = (key != 0 ? 1 : 0);
+ if (key != 0)
+ *(char*)(pkt->data+3) = key;
+ pkt->stream_index = is->overlay_stream;
+ packet_queue_put(&is->overlayq, pkt);
+ }
+}
+
static void stream_cycle_channel(VideoState *is, int codec_type)
{
AVFormatContext *ic = is->ic;
@@ -2566,15 +3030,17 @@ static void stream_cycle_channel(VideoState *is, int codec_type)
start_index = is->video_stream;
else if (codec_type == AVMEDIA_TYPE_AUDIO)
start_index = is->audio_stream;
- else
+ else if (codec_type == AVMEDIA_TYPE_SUBTITLE)
start_index = is->subtitle_stream;
- if (start_index < (codec_type == AVMEDIA_TYPE_SUBTITLE ? -1 : 0))
+ else
+ start_index = is->overlay_stream;
+ if (start_index < (codec_type == AVMEDIA_TYPE_SUBTITLE || codec_type == AVMEDIA_TYPE_OVERLAY ? -1 : 0))
return;
stream_index = start_index;
for (;;) {
if (++stream_index >= is->ic->nb_streams)
{
- if (codec_type == AVMEDIA_TYPE_SUBTITLE)
+ if (codec_type == AVMEDIA_TYPE_SUBTITLE || codec_type == AVMEDIA_TYPE_OVERLAY)
{
stream_index = -1;
goto the_end;
@@ -2594,6 +3060,7 @@ static void stream_cycle_channel(VideoState *is, int codec_type)
break;
case AVMEDIA_TYPE_VIDEO:
case AVMEDIA_TYPE_SUBTITLE:
+ case AVMEDIA_TYPE_OVERLAY:
goto the_end;
default:
break;
@@ -2670,7 +3137,6 @@ static void event_loop(void)
case SDLK_f:
toggle_full_screen();
break;
- case SDLK_p:
case SDLK_SPACE:
toggle_pause();
break;
@@ -2689,6 +3155,20 @@ static void event_loop(void)
if (cur_stream)
stream_cycle_channel(cur_stream, AVMEDIA_TYPE_SUBTITLE);
break;
+ case SDLK_o:
+ if (cur_stream)
+ stream_cycle_channel(cur_stream, AVMEDIA_TYPE_OVERLAY);
+ break;
+ case SDLK_RETURN:
+ case SDLK_u:
+ case SDLK_d:
+ case SDLK_l:
+ case SDLK_r:
+ case SDLK_p:
+ if (cur_stream)
+ stream_handle_event(cur_stream, &event);
+ break;
+
case SDLK_w:
toggle_audio_display();
break;
@@ -2939,24 +3419,35 @@ static void show_help(void)
printf("\nWhile playing:\n"
"q, ESC quit\n"
"f toggle full screen\n"
- "p, SPC pause\n"
+ "SPC pause\n"
"a cycle audio channel\n"
"v cycle video channel\n"
"t cycle subtitle channel\n"
+ "o cycle overlay channel\n"
"w show audio waves\n"
"s activate frame-step mode\n"
"left/right seek backward/forward 10 seconds\n"
"down/up seek backward/forward 1 minute\n"
"mouse click seek to percentage in file corresponding to fraction of width\n"
+ "u navigate 'up' in overlay\n"
+ "d navigate 'down' in overlay\n"
+ "l navigate 'left' in overlay\n"
+ "r navigate 'right' in overlay\n"
+ "ENTER activate selected overlay button\n"
+ "p toggle overlay display (popup menu)\n"
);
}
static void opt_input_file(void *optctx, const char *filename)
{
if (input_filename) {
- fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
- filename, input_filename);
- exit(1);
+ if (subpath_filename) {
+ fprintf(stderr, "Argument '%s' provided as subpath filename, but '%s' was already specified.\n",
+ filename, subpath_filename);
+ exit(1);
+ }
+ subpath_filename = filename;
+ return;
}
if (!strcmp(filename, "-"))
filename = "pipe:";
@@ -3022,7 +3513,7 @@ int main(int argc, char **argv)
av_init_packet(&flush_pkt);
flush_pkt.data = "FLUSH";
- cur_stream = stream_open(input_filename, file_iformat);
+ cur_stream = stream_open(input_filename, subpath_filename, file_iformat);
event_loop();
--
1.7.9.5
This new decoder will allow to decode menu stream found
in m2ts files in Bluray or AVCHD HDMV disks.
The new decoder was developped using AVMEDIA_TYPE_OVERLAY,
inspired from AVMEDIA_TYPE_SUBTITLE, since both may send
multiple decoded pictures to add to video currently
displayed.
version 4:
- Added some fixes for x264 demo disk that have button without
picture.
- Change palette conversion to avoid convertion in avplay.
Signed-off-by: David Girault <david at dhgirault.fr>
---
libavcodec/Makefile | 1 +
libavcodec/allcodecs.c | 3 +
libavcodec/avcodec.h | 53 ++++
libavcodec/igsmnudec.c | 743 ++++++++++++++++++++++++++++++++++++++++++++++++
libavcodec/utils.c | 36 +++
libavutil/avutil.h | 1 +
6 files changed, 837 insertions(+)
create mode 100644 libavcodec/igsmnudec.c
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 3bfd78b..805c7aa 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -310,6 +310,7 @@ OBJS-$(CONFIG_PGM_ENCODER) += pnmenc.o pnm.o
OBJS-$(CONFIG_PGMYUV_DECODER) += pnmdec.o pnm.o
OBJS-$(CONFIG_PGMYUV_ENCODER) += pnmenc.o pnm.o
OBJS-$(CONFIG_PGSSUB_DECODER) += pgssubdec.o
+OBJS-$(CONFIG_IGSMENU_DECODER) += igsmnudec.o
OBJS-$(CONFIG_PICTOR_DECODER) += pictordec.o cga_data.o
OBJS-$(CONFIG_PNG_DECODER) += png.o pngdec.o pngdsp.o
OBJS-$(CONFIG_PNG_ENCODER) += png.o pngenc.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 01d13d5..3e529d7 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -375,6 +375,9 @@ void avcodec_register_all(void)
REGISTER_DECODER (SRT, srt);
REGISTER_ENCDEC (XSUB, xsub);
+ /* specific data */
+ REGISTER_DECODER (IGSMENU, igsmenu);
+
/* external libraries */
REGISTER_ENCODER (LIBFAAC, libfaac);
REGISTER_ENCDEC (LIBGSM, libgsm);
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 4a07d6d..aab4c7a 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -411,6 +411,8 @@ enum CodecID {
CODEC_ID_FIRST_UNKNOWN = 0x18000, ///< A dummy ID pointing at the start of various fake codecs.
CODEC_ID_TTF = 0x18000,
+ CODEC_ID_HDMV_IGS_MENU,
+
CODEC_ID_PROBE = 0x19000, ///< codec_id is not known (like CODEC_ID_NONE) but lavf should attempt to identify it
CODEC_ID_MPEG2TS = 0x20000, /**< _FAKE_ codec to indicate a raw MPEG-2 TS
@@ -3096,6 +3098,30 @@ typedef struct AVSubtitle {
int64_t pts; ///< Same as packet pts, in AV_TIME_BASE
} AVSubtitle;
+typedef struct AVOverlayRect {
+ int x; ///< top left corner of pict, undefined when pict is not set
+ int y; ///< top left corner of pict, undefined when pict is not set
+ int w; ///< width of pict, undefined when pict is not set
+ int h; ///< height of pict, undefined when pict is not set
+ int nb_colors; ///< number of colors in pict, undefined when pict is not set
+
+ /**
+ * data+linesize for the bitmap of this overlay.
+ */
+ AVPicture pict;
+} AVOverlayRect;
+
+typedef struct AVOverlay {
+ uint16_t format; /* 0 = HDMV Interactive Graphic menu */
+ uint32_t start_display_time; /* relative to packet pts, in ms */
+ uint32_t end_display_time; /* relative to packet pts, in ms */
+ unsigned num_rects;
+ AVOverlayRect **rects;
+ int64_t pts; ///< Same as packet pts, in AV_TIME_BASE
+ void *extra; ///< Extra data
+ int extra_sz; ///< Size in bytes of extra data
+} AVOverlay;
+
/**
* If c is NULL, returns the first registered codec,
* if c is non-NULL, returns the next registered codec after c,
@@ -3262,6 +3288,13 @@ int avcodec_close(AVCodecContext *avctx);
void avsubtitle_free(AVSubtitle *sub);
/**
+ * Free all allocated data in the given overlay struct.
+ *
+ * @param overlay AVOverlay to free.
+ */
+void avoverlay_free(AVOverlay *overlay);
+
+/**
* @}
*/
@@ -3582,6 +3615,26 @@ int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
AVPacket *avpkt);
/**
+ * Decode a overlay message.
+ * Return a negative value on error, otherwise return the number of bytes used.
+ * If no overlay could be decompressed, got_sub_ptr is zero.
+ * Otherwise, the overlay is stored in *sub.
+ * Note that CODEC_CAP_DR1 is not available for overlay codecs. This is for
+ * simplicity, because the performance difference is expect to be negligible
+ * and reusing a get_buffer written for video codecs would probably perform badly
+ * due to a potentially very different allocation pattern.
+ *
+ * @param avctx the codec context
+ * @param[out] overlay The AVOverlay in which the decoded overlay will be stored, must be
+ freed with avoverlay_free if *got_overlay_ptr is set.
+ * @param[in,out] got_overlay_ptr Zero if no overlay could be decompressed, otherwise, it is nonzero.
+ * @param[in] avpkt The input AVPacket containing the input buffer.
+ */
+int avcodec_decode_overlay2(AVCodecContext *avctx, AVOverlay *overlay,
+ int *got_overlay_ptr,
+ AVPacket *avpkt);
+
+/**
* @defgroup lavc_parsing Frame parsing
* @{
*/
diff --git a/libavcodec/igsmnudec.c b/libavcodec/igsmnudec.c
new file mode 100644
index 0000000..e45d305
--- /dev/null
+++ b/libavcodec/igsmnudec.c
@@ -0,0 +1,743 @@
+/*
+ * IGS menu decoder
+ * 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
+ */
+
+/**
+ * @file
+ * IGS menu decoder
+ */
+
+#include "avcodec.h"
+#include "dsputil.h"
+#include "bytestream.h"
+#include "libavutil/colorspace.h"
+#include "libavutil/imgutils.h"
+
+#define YUVA(y,u,v,a) (((a) << 24) | ((y) << 16) | ((u) << 8) | (v))
+
+enum SegmentType {
+ PALETTE_SEGMENT = 0x14,
+ PICTURE_SEGMENT = 0x15,
+ BUTTON_SEGMENT = 0x18,
+ DISPLAY_SEGMENT = 0x80,
+};
+
+typedef struct IGSPicture {
+ uint16_t id;
+ int w;
+ int h;
+ uint8_t *rle;
+ unsigned int rle_buffer_size, rle_data_len;
+ unsigned int rle_remaining_len;
+} IGSPicture;
+
+typedef struct IGSButton {
+ uint16_t id;
+ uint16_t v;
+ uint8_t f;
+ uint16_t x;
+ uint16_t y;
+ uint16_t n[4];//0:up,1:down,2:left,3:right
+ uint16_t pstart[3]; //0:normal,1:selected,2:activated
+ uint16_t pstop[3];
+ uint16_t cmds_count;
+ uint32_t *cmds;
+} IGSButton;
+
+typedef struct IGSBOG {
+ uint16_t def_button;
+ uint16_t cur_button;
+ IGSButton *buttons;
+ int buttons_count;
+} IGSBOG;
+
+typedef struct IGSPage {
+ int w;
+ int h;
+ int def_button;
+ int sel_button;
+ int in_time;
+ int timeout;
+ int palette;
+ IGSBOG *bogs;
+ int bogs_count;
+} IGSPage;
+
+typedef struct IGSPalette {
+ uint32_t clut[256];
+} IGSPalette;
+
+typedef struct IGSContext {
+ IGSPalette *palettes;
+ int palettes_count;
+ IGSPicture *pictures;
+ int pictures_count;
+ IGSPage *pages;
+ int pages_count;
+ int page;
+} IGSContext;
+
+static av_cold int init_decoder(AVCodecContext *avctx)
+{
+ avctx->pix_fmt = PIX_FMT_PAL8;
+ return 0;
+}
+
+static av_cold int close_decoder(AVCodecContext *avctx)
+{
+ int i;
+
+ IGSContext *ctx = avctx->priv_data;
+
+ for (i=0;i<ctx->pictures_count;i++) {
+ av_freep(&ctx->pictures[i].rle);
+ }
+ av_freep(&ctx->pictures);
+ av_freep(&ctx->palettes);
+
+ for (i=0;i<ctx->pages_count;i++) {
+ IGSPage *page = &ctx->pages[i];
+ int j;
+ for (j=0;j<page->bogs_count;j++) {
+ IGSBOG *bog = &page->bogs[j];
+ int k;
+ for (k=0;k<bog->buttons_count;k++) {
+ IGSButton *button = &bog->buttons[k];
+ if (button->cmds) av_freep(&button->cmds);
+ }
+ av_freep(&bog->buttons);
+ }
+ av_freep(&page->bogs);
+ }
+ av_freep(&ctx->pages);
+ return 0;
+}
+
+/**
+ * Decode the RLE data.
+ *
+ * The subtitle is stored as an Run Length Encoded image.
+ *
+ * @param avctx contains the current codec context
+ * @param ove pointer to the processed overlay data
+ * @param buf pointer to the RLE data to process
+ * @param buf_size size of the RLE data to process
+ */
+static int decode_rle(AVCodecContext *avctx, AVOverlay *ove, int i,
+ const uint8_t *buf, unsigned int buf_size)
+{
+ const uint8_t *rle_bitmap_end;
+ int pixel_count, line_count;
+
+ rle_bitmap_end = buf + buf_size;
+
+ ove->rects[i]->pict.data[0] = av_malloc(ove->rects[i]->w * ove->rects[i]->h);
+
+ if (!ove->rects[i]->pict.data[0])
+ return -1;
+
+ pixel_count = 0;
+ line_count = 0;
+
+ while (buf < rle_bitmap_end && line_count < ove->rects[i]->h) {
+ uint8_t flags, color;
+ int run;
+
+ color = bytestream_get_byte(&buf);
+ run = 1;
+
+ if (color == 0x00) {
+ flags = bytestream_get_byte(&buf);
+ run = flags & 0x3f;
+ if (flags & 0x40)
+ run = (run << 8) + bytestream_get_byte(&buf);
+ color = flags & 0x80 ? bytestream_get_byte(&buf) : 0;
+ }
+
+ if (run > 0 && pixel_count + run <= ove->rects[i]->w * ove->rects[i]->h) {
+ memset(ove->rects[i]->pict.data[0] + pixel_count, color, run);
+ pixel_count += run;
+ } else if (!run) {
+ /*
+ * New Line. Check if correct pixels decoded, if not display warning
+ * and adjust bitmap pointer to correct new line position.
+ */
+ if (pixel_count % ove->rects[i]->w > 0)
+ av_log(avctx, AV_LOG_ERROR, "Decoded %d pixels, when line should be %d pixels\n",
+ pixel_count % ove->rects[i]->w, ove->rects[i]->w);
+ line_count++;
+ }
+ }
+
+ if (pixel_count < ove->rects[i]->w * ove->rects[i]->h) {
+ av_log(avctx, AV_LOG_ERROR, "Insufficient RLE data for overlay button %d\n", i);
+ return -1;
+ }
+
+ av_dlog(avctx, " Pixel Count = %d, Area = %d\n", pixel_count, ove->rects[i]->w * ove->rects[i]->h);
+
+ return 0;
+}
+
+/**
+ * Parse the picture segment packet.
+ *
+ * The picture segment contains details on the sequence id,
+ * width, height and Run Length Encoded (RLE) bitmap data.
+ *
+ * @param avctx contains the current codec context
+ * @param buf pointer to the packet to process
+ * @param buf_size size of packet to process
+ * @todo TODO: Enable support for RLE data over multiple packets
+ */
+static int parse_picture_segment(AVCodecContext *avctx,
+ const uint8_t *buf, int buf_size)
+{
+ IGSContext *ctx = avctx->priv_data;
+ IGSPicture *pic = NULL;
+ uint16_t oid;
+ uint8_t sequence_desc;
+ unsigned int rle_bitmap_len, width, height;
+
+ if (buf_size <= 4)
+ return -1;
+ buf_size -= 4;
+
+ /* skip 3 unknown bytes: Object ID (2 bytes), Version Number */
+ oid = bytestream_get_be16(&buf);
+ buf += 1;
+
+ /* Read the Sequence Description to determine if start of RLE data or appended to previous RLE */
+ sequence_desc = bytestream_get_byte(&buf);
+
+ if (!(sequence_desc & 0x80)) {
+ /* Additional RLE data to last picture */
+ pic = &(ctx->pictures[ctx->pictures_count-1]);
+ if (buf_size > pic->rle_remaining_len)
+ return -1;
+ av_dlog(avctx, "Continuing bitmap @ idx %d (%p).\n", ctx->pictures_count-1, pic);
+ memcpy(pic->rle + pic->rle_data_len, buf, buf_size);
+ pic->rle_data_len += buf_size;
+ pic->rle_remaining_len -= buf_size;
+ return 0;
+ }
+
+ if (buf_size <= 7)
+ return -1;
+ buf_size -= 7;
+
+ /* Decode rle bitmap length, stored size includes width/height data */
+ rle_bitmap_len = bytestream_get_be24(&buf) - 2*2;
+
+ /* Get bitmap dimensions from data */
+ width = bytestream_get_be16(&buf);
+ height = bytestream_get_be16(&buf);
+
+ /* Make sure the bitmap is not too large */
+ if (avctx->width < width || avctx->height < height) {
+ av_log(avctx, AV_LOG_ERROR, "Bitmap dimensions (%dx%d) larger than video.\n", width, height);
+ return -1;
+ }
+
+ /* add this picture */
+ ctx->pictures_count++;
+ pic = av_realloc(ctx->pictures, ctx->pictures_count * sizeof(IGSPicture));
+ if (pic == NULL) {
+ av_log(avctx, AV_LOG_ERROR, "Can't reallocate bitmaps table.\n");
+ ctx->pictures_count--;
+ return -2;
+ }
+ ctx->pictures = pic;
+ pic = &(ctx->pictures[ctx->pictures_count-1]);
+
+ av_log(avctx, AV_LOG_DEBUG, "New bitmap %04X: %dx%d @ idx %d (%p).\n",
+ oid, width, height, ctx->pictures_count-1, pic);
+
+ pic->id = oid;
+ pic->w = width;
+ pic->h = height;
+
+ pic->rle = av_malloc(rle_bitmap_len);
+ if (!pic->rle)
+ return -1;
+
+ pic->rle_buffer_size=rle_bitmap_len;
+
+ av_dlog(avctx, "Bitmap RLE %d bytes @ %p (%d, %d).\n", pic->rle_buffer_size, pic->rle, rle_bitmap_len, buf_size);
+ if (buf_size>pic->rle_buffer_size) buf_size=pic->rle_buffer_size;
+
+ memcpy(pic->rle, buf, buf_size);
+ pic->rle_data_len = buf_size;
+ pic->rle_remaining_len = rle_bitmap_len - buf_size;
+
+ return 0;
+}
+
+/**
+ * Parse the palette segment packet.
+ *
+ * The palette segment contains details of the palette,
+ * a maximum of 256 colors can be defined.
+ *
+ * @param avctx contains the current codec context
+ * @param buf pointer to the packet to process
+ * @param buf_size size of packet to process
+ */
+static int parse_palette_segment(AVCodecContext *avctx,
+ const uint8_t *buf, int buf_size)
+{
+ IGSContext *ctx = avctx->priv_data;
+ IGSPalette *pal = NULL;
+ uint16_t oid;
+
+ const uint8_t *buf_end = buf + buf_size;
+ int color_id;
+ int y, cb, cr, alpha;
+
+ const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
+ int r, g, b, r_add, g_add, b_add;
+
+ /* add this picture */
+ ctx->palettes_count++;
+ pal = av_realloc(ctx->palettes, ctx->palettes_count * sizeof(IGSPalette));
+ if (pal == NULL) {
+ av_log(avctx, AV_LOG_ERROR, "Can't reallocate palettes table.\n");
+ ctx->palettes_count--;
+ return -2;
+ }
+ ctx->palettes = pal;
+ pal = &(ctx->palettes[ctx->palettes_count-1]);
+
+ /* Skip two null bytes */
+ oid = bytestream_get_be16(&buf);
+
+ av_log(avctx, AV_LOG_DEBUG, "New palette %04X @ %p: idx=%d, size=%d, %d colors.\n",
+ oid, pal, ctx->palettes_count-1, buf_size-2, (buf_size-2)/5);
+ while (buf < buf_end) {
+ color_id = bytestream_get_byte(&buf);
+
+ y = bytestream_get_byte(&buf);
+ cr = bytestream_get_byte(&buf);
+ cb = bytestream_get_byte(&buf);
+ alpha = bytestream_get_byte(&buf);
+
+ YUV_TO_RGB1(cb, cr);
+ YUV_TO_RGB2(r, g, b, y);
+ y = RGB_TO_Y_CCIR(r, g, b);
+ cr = RGB_TO_U_CCIR(r, g, b, 0);
+ cb = RGB_TO_V_CCIR(r, g, b, 0);
+
+ av_dlog(avctx, " Color %d := (%d,%d,%d,%d)\n", color_id, y, cr, cb, alpha);
+
+ /* Store color in palette */
+ pal->clut[color_id] = YUVA(y,cr,cb,alpha);
+ }
+ return 0;
+}
+
+/**
+ * Parse the button segment packet.
+ *
+ * The button segment contains details on the interactive graphics.
+ *
+ * @param avctx contains the current codec context
+ * @param buf pointer to the packet to process
+ * @param buf_size size of packet to process
+ * @todo TODO: Implement cropping
+ */
+static int parse_button_segment(AVCodecContext *avctx,
+ const uint8_t *buf, int buf_size)
+{
+ IGSContext *ctx = avctx->priv_data;
+ const uint8_t *start = buf;
+ int i, page_cnt;
+ uint32_t to;
+ int w = bytestream_get_be16(&buf);
+ int h = bytestream_get_be16(&buf);
+
+ av_log(avctx, AV_LOG_DEBUG, "Interactive Graphics dimensions %dx%d\n", w, h);
+ if (av_image_check_size(w, h, 0, avctx) >= 0)
+ avcodec_set_dimensions(avctx, w, h);
+
+ buf+=1; // skip framerate (0x20 => 2 => 24fps, 0x60 => 50fps)
+ buf+=2; // unknown (0x0000)
+ buf+=1; // skip some flags (0x80)
+ buf+=4; // skip IN_time (0xc0000163)
+ to = bytestream_get_be32(&buf); // user timeout (bit 31 seems to be a flags)
+ // ten more byte skipped (output generated by TMpegEnc Authoring 5)
+ if (to == 0) buf+=10;
+
+ page_cnt = bytestream_get_byte(&buf);
+ av_log(avctx, AV_LOG_DEBUG, "Interactive Graphics had %d pages\n", page_cnt);
+
+ ctx->pages = av_mallocz(page_cnt*sizeof(IGSPage));
+ if (ctx->pages) ctx->pages_count = page_cnt;
+
+ /* pages loop */
+ for (i=0;i<ctx->pages_count;i++) {
+ IGSPage *page = &(ctx->pages[i]);
+ int j, bog_cnt;
+ buf+=1; /* skip page_id */
+ buf+=1; /* skip unknown */
+ buf+=8; /* skip UO */
+ buf+=4; /* skip in/out effects count (this may be followed by effect data) */
+ buf+=1; /* skip framerate divider */
+ page->def_button = bytestream_get_be16(&buf);
+ page->sel_button = page->def_button;
+
+ buf+=2; /* skip def_activated, palette */
+ page->palette = bytestream_get_byte(&buf);
+
+ bog_cnt = bytestream_get_byte(&buf);
+ av_log(avctx, AV_LOG_DEBUG, "Page %d @ %X has %d BOGs, use palette %d\n", i, ((int)(buf-start))-21, bog_cnt, page->palette);
+
+ page->bogs = av_mallocz(bog_cnt*sizeof(IGSBOG));
+ if (page->bogs) page->bogs_count = bog_cnt;
+ else break;
+
+ /* bogs loop */
+ for (j=0;j<page->bogs_count;j++) {
+ IGSBOG *bog = &(page->bogs[j]);
+ int k, button_cnt;
+
+ bog->def_button = bytestream_get_be16(&buf);
+ bog->cur_button = bog->def_button;
+
+ button_cnt = bytestream_get_byte(&buf);
+ av_log(avctx, AV_LOG_DEBUG, " BOG %d had %d buttons, default=%04X\n", j, button_cnt, bog->def_button);
+
+ bog->buttons = av_mallocz(button_cnt*sizeof(IGSButton));
+ if (bog->buttons) bog->buttons_count = button_cnt;
+ else break;
+
+ /* buttons loop */
+ for (k=0;k<bog->buttons_count;k++) {
+ IGSButton *but = &(bog->buttons[k]);
+ but->id = bytestream_get_be16(&buf);
+ but->v = bytestream_get_be16(&buf);
+ but->f = bytestream_get_byte(&buf);
+ but->x = bytestream_get_be16(&buf);
+ but->y = bytestream_get_be16(&buf);
+ but->n[0] = bytestream_get_be16(&buf);
+ but->n[1] = bytestream_get_be16(&buf);
+ but->n[2] = bytestream_get_be16(&buf);
+ but->n[3] = bytestream_get_be16(&buf);
+ av_log(avctx, AV_LOG_DEBUG, " Button %04X @ %p: x=%d, y=%d, navig=%04X/%04X/%04X/%04X, ",
+ but->id, but, but->x, but->y, but->n[0], but->n[1], but->n[2], but->n[3]);
+
+ but->pstart[0] = bytestream_get_be16(&buf);
+ but->pstop[0] = bytestream_get_be16(&buf);
+ buf+=2;//repeat, sound
+
+ but->pstart[1] = bytestream_get_be16(&buf);
+ but->pstop[1] = bytestream_get_be16(&buf);
+ buf+=2;//repeat, sound
+
+ but->pstart[2] = bytestream_get_be16(&buf);
+ but->pstop[2] = bytestream_get_be16(&buf);
+ but->cmds_count = bytestream_get_be16(&buf);
+ if (but->cmds_count) {
+ size_t sz = 3*sizeof(uint32_t) * but->cmds_count;
+ but->cmds = av_mallocz(sz);
+ if (but->cmds) memcpy(but->cmds, buf, sz);
+ buf+= sz; // skip commands
+ }
+ av_log(avctx, AV_LOG_DEBUG, "pics=%04X/%04X/%04X, cmds=%d\n",
+ but->pstart[0], but->pstart[1], but->pstart[2], but->cmds_count);
+
+ // set this button current if not already set
+ if (bog->cur_button == 0xffff) bog->cur_button = but->id;
+ }
+
+
+ }
+
+ // set selected button if not already set
+ if (page->sel_button == 0xffff) {
+ for (j=0;j<page->bogs_count;j++) {
+ IGSBOG *bog = &(page->bogs[j]);
+ int k;
+ for (k=0;k<bog->buttons_count;k++) {
+ IGSButton *but = &(bog->buttons[k]);
+ // search first button with nav to other button
+ if (but->n[0] != but->id ||
+ but->n[1] != but->id ||
+ but->n[2] != but->id ||
+ but->n[3] != but->id) {
+ page->sel_button = but->id;
+ break;
+ }
+ }
+ if (page->sel_button != 0xffff) break;
+ }
+ }
+ }
+ return 0;
+}
+
+/**
+ * Parse the display segment packet.
+ *
+ * The display segment controls the updating of the display.
+ *
+ * @param avctx contains the current codec context
+ * @param data pointer to the data pertaining the subtitle to display
+ * @param buf pointer to the packet to process
+ * @param buf_size size of packet to process
+ * @todo TODO: Fix start time, relies on correct PTS, currently too late
+ *
+ * @todo TODO: Fix end time, normally cleared by a second display
+ * @todo segment, which is currently ignored as it clears
+ * @todo the subtitle too early.
+ */
+static int display_end_segment(AVCodecContext *avctx, void *data,
+ const uint8_t *buf, int buf_size)
+{
+ AVOverlay *ove = (AVOverlay *)data;
+ IGSContext *ctx = avctx->priv_data;
+ IGSPage *page = NULL;
+
+ int i, activated = 0;
+
+ /*
+ * The end display time is a timeout value and is only reached
+ * if the next subtitle is later then timeout or subtitle has
+ * not been cleared by a subsequent empty display command.
+ */
+
+ memset(ove, 0, sizeof(*ove));
+ // Blank if last object_number was 0.
+ // Note that this may be wrong for more complex subtitles.
+ if (!ctx->pages_count) {
+ av_log(avctx, AV_LOG_INFO, "No page to display!!!\n");
+ return 1;
+ }
+
+ page = &(ctx->pages[ctx->page]);
+
+ if (buf_size >= 1) {
+ // this may be a fake display segment, with a navigation key embedded
+ IGSButton *button=NULL;
+ // handle page switch early
+ if (*(const char*)buf == 'p') {
+ //Todo: handle extra parameters to allow page/button selection from HDMV command 'SetButtonPage'
+ if (buf_size >= 2) {
+ if (buf[1]<ctx->pages_count) ctx->page = buf[1];
+ }
+ else {
+ if (++ctx->page >= ctx->pages_count) ctx->page=0;
+ }
+ page = &(ctx->pages[ctx->page]);
+ if (buf_size >= 4) {
+ page->sel_button = *(const uint16_t*)(buf+2);
+ }
+ }
+
+ for (i=0;i<page->bogs_count;i++) {
+ IGSBOG *bog = &(page->bogs[i]);
+ int j;
+
+ for (j=0;j<bog->buttons_count;j++) {
+ button = &(bog->buttons[j]);
+ if (button->id == page->sel_button) break;
+ }
+ if (j<bog->buttons_count) break;
+ button=NULL;
+ }
+ if (button != NULL) {
+ uint16_t next = button->id;
+ switch(*(const char*)buf) {
+ case 13: activated=1; break;
+ case 'u': next = button->n[0]; break;
+ case 'd': next = button->n[1]; break;
+ case 'l': next = button->n[2]; break;
+ case 'r': next = button->n[3]; break;
+ }
+ page->sel_button = next;
+ }
+ }
+
+ ove->start_display_time = 0;
+ ove->end_display_time = 3600000;
+ ove->format = 0;
+
+ ove->num_rects = page->bogs_count;
+ ove->rects = av_mallocz(sizeof(AVOverlayRect*)*ove->num_rects);
+
+ av_log(avctx, AV_LOG_INFO, "Displaying menu page %d, %d bog, button %d %s.\n",
+ ctx->page, ove->num_rects, page->sel_button, (activated?"activated":"selected"));
+
+ for (i=0;i<ove->num_rects;i++) {
+ IGSBOG *bog = &(page->bogs[i]);
+ IGSButton *button=NULL;
+ IGSPicture *picture=NULL;
+ AVOverlayRect *rect;
+ int j;
+ uint16_t oid;
+
+ av_dlog(avctx, " BOG %d, Current Button %04X\n", i, bog->cur_button);
+
+ // search button in bog
+ for (j=0;j<bog->buttons_count;j++) {
+ button = &(bog->buttons[j]);
+ if (button->id == bog->cur_button) break;
+ }
+ if (j==bog->buttons_count) {
+ button = &(bog->buttons[0]);
+ bog->cur_button = button->id;
+ }
+ if (button->f & 0x80) activated=1; // auto-activated button
+
+ // get picture for button
+ oid = (page->sel_button == button->id ? (activated?button->pstart[2]:button->pstart[1]) : button->pstart[0]);
+
+ for (j=0;j<ctx->pictures_count;j++) {
+ picture = &(ctx->pictures[j]);
+ if (picture->id == oid) break;
+ picture = NULL;
+ }
+
+ av_dlog(avctx, " Button %04X @ %p => Picture %04X @ %p\n", button->id, button, oid, picture);
+
+ rect = av_mallocz(sizeof(AVOverlayRect));
+ rect->x = button->x;
+ rect->y = button->y;
+
+ ove->rects[i] = rect;
+
+ if (j==ctx->pictures_count) {
+ // bitmap not found for that button!!!
+ rect->w = 0;
+ rect->h = 0;
+ }
+ else {
+ // process bitmap
+ rect->w = picture->w;
+ rect->h = picture->h;
+ rect->pict.linesize[0] = picture->w;
+ if (picture->rle) {
+ if (picture->rle_remaining_len)
+ av_log(avctx, AV_LOG_ERROR, "RLE data length %u is %u bytes shorter than expected\n",
+ picture->rle_data_len, picture->rle_remaining_len);
+ if(decode_rle(avctx, ove, i, picture->rle, picture->rle_data_len) < 0) {
+ av_log(avctx, AV_LOG_ERROR, "Error decoding RLE picture\n");
+ return 0;
+ }
+ }
+
+ /* Allocate memory for colors */
+ rect->nb_colors = 256;
+ rect->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
+ memcpy(rect->pict.data[1], ctx->palettes[page->palette].clut, rect->nb_colors * sizeof(uint32_t));
+ }
+
+ av_dlog(avctx, " Rect %d @ %p: x=%d, y=%d, w=%d, h=%d, data=%p, pal=%p\n",
+ i, rect, rect->x, rect->y, rect->w, rect->h, rect->pict.data[0], rect->pict.data[1]);
+
+ /* Add extra data */
+ if ((page->sel_button == button->id) && activated) {
+ // TODO: button HDMV program must be included when activated
+ if (button->cmds_count > 0) {
+ ove->extra_sz = 3*sizeof(uint32_t) * button->cmds_count;
+ ove->extra = av_mallocz(ove->extra_sz);
+ if (ove->extra) memcpy(ove->extra, button->cmds, ove->extra_sz);
+ else ove->extra_sz = 0; // ignore alloc error for now
+ av_dlog(avctx, " Extra data for %d commands @ %p\n", button->cmds_count, ove->extra);
+ }
+ }
+ }
+ return 1;
+}
+
+static int decode(AVCodecContext *avctx, void *data, int *data_size,
+ AVPacket *avpkt)
+{
+ const uint8_t *buf = avpkt->data;
+ int buf_size = avpkt->size;
+
+ const uint8_t *buf_end;
+ uint8_t segment_type;
+ int segment_length;
+ int ret = 0;
+
+ *data_size = 0;
+
+ /* Ensure that we have received at a least a segment code and segment length */
+ if (buf_size < 3)
+ return -1;
+
+ buf_end = buf + buf_size;
+
+ /* Step through buffer to identify segments */
+ while (buf < buf_end) {
+ segment_type = bytestream_get_byte(&buf);
+ segment_length = bytestream_get_be16(&buf);
+
+ if (segment_type != DISPLAY_SEGMENT && segment_length > buf_end - buf)
+ break;
+
+ switch (segment_type) {
+ case PALETTE_SEGMENT:
+ ret = parse_palette_segment(avctx, buf, segment_length);
+ break;
+ case PICTURE_SEGMENT:
+ ret = parse_picture_segment(avctx, buf, segment_length);
+ break;
+ case BUTTON_SEGMENT:
+ ret = parse_button_segment(avctx, buf, segment_length);
+ break;
+ case DISPLAY_SEGMENT:
+ *data_size = display_end_segment(avctx, data, buf, segment_length);
+ break;
+ default:
+ av_log(avctx, AV_LOG_ERROR, "Unknown interactive segment type 0x%x, length %d\n",
+ segment_type, segment_length);
+ ret = -1;
+ break;
+ }
+ if (ret<0) {
+ int i;
+ av_log(avctx, AV_LOG_DEBUG, "Error returned for segment length %d with type %x\n", segment_length, segment_type);
+ av_log(avctx, AV_LOG_DEBUG, "Segment Dump:");
+ for (i = 0; i < segment_length; i++) {
+ if (i%16 == 0)
+ av_log(avctx, AV_LOG_DEBUG, "\n%03X: %02x", i, buf[i]);
+ else
+ av_log(avctx, AV_LOG_DEBUG, " %02x", buf[i]);
+ }
+ av_log(avctx, AV_LOG_DEBUG, "\n");
+ }
+
+ buf += segment_length;
+ }
+
+ return buf_size;
+}
+
+AVCodec ff_igsmenu_decoder = {
+ .name = "igsmenu",
+ .type = AVMEDIA_TYPE_OVERLAY,
+ .id = CODEC_ID_HDMV_IGS_MENU,
+ .priv_data_size = sizeof(IGSContext),
+ .init = init_decoder,
+ .close = close_decoder,
+ .decode = decode,
+ .long_name = NULL_IF_CONFIG_SMALL("HDMV Interactive Graphic Stream menus"),
+};
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index d2ee9f8..15b2d5d 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -1368,6 +1368,39 @@ void avsubtitle_free(AVSubtitle *sub)
memset(sub, 0, sizeof(AVSubtitle));
}
+int avcodec_decode_overlay2(AVCodecContext *avctx, AVOverlay *overlay,
+ int *got_overlay_ptr,
+ AVPacket *avpkt)
+{
+ int ret;
+
+ avctx->pkt = avpkt;
+ *got_overlay_ptr = 0;
+ ret = avctx->codec->decode(avctx, overlay, got_overlay_ptr, avpkt);
+ if (*got_overlay_ptr)
+ avctx->frame_number++;
+ return ret;
+}
+
+void avoverlay_free(AVOverlay *overlay)
+{
+ int i;
+
+ for (i = 0; i < overlay->num_rects; i++)
+ {
+ av_freep(&overlay->rects[i]->pict.data[0]);
+ av_freep(&overlay->rects[i]->pict.data[1]);
+ av_freep(&overlay->rects[i]->pict.data[2]);
+ av_freep(&overlay->rects[i]->pict.data[3]);
+ av_freep(&overlay->rects[i]);
+ }
+
+ av_freep(&overlay->rects);
+ if (overlay->extra) av_freep(&overlay->extra);
+
+ memset(overlay, 0, sizeof(AVOverlay));
+}
+
av_cold int avcodec_close(AVCodecContext *avctx)
{
/* If there is a user-supplied mutex locking routine, call it. */
@@ -1600,6 +1633,9 @@ void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
case AVMEDIA_TYPE_SUBTITLE:
snprintf(buf, buf_size, "Subtitle: %s", codec_name);
break;
+ case AVMEDIA_TYPE_OVERLAY:
+ snprintf(buf, buf_size, "Overlay: %s", codec_name);
+ break;
case AVMEDIA_TYPE_ATTACHMENT:
snprintf(buf, buf_size, "Attachment: %s", codec_name);
break;
diff --git a/libavutil/avutil.h b/libavutil/avutil.h
index 1c8e076..d81b01c 100644
--- a/libavutil/avutil.h
+++ b/libavutil/avutil.h
@@ -230,6 +230,7 @@ enum AVMediaType {
AVMEDIA_TYPE_DATA, ///< Opaque data information usually continuous
AVMEDIA_TYPE_SUBTITLE,
AVMEDIA_TYPE_ATTACHMENT, ///< Opaque data information usually sparse
+ AVMEDIA_TYPE_OVERLAY,
AVMEDIA_TYPE_NB
};
--
1.7.9.5
--
Cin mailing list
[email protected]
https://lists.cinelerra-gg.org/mailman/listinfo/cin