summaryrefslogtreecommitdiffstats
path: root/sub
diff options
context:
space:
mode:
authorUoti Urpala <uau@glyph.nonexistent.invalid>2011-01-26 20:42:15 +0200
committerUoti Urpala <uau@glyph.nonexistent.invalid>2011-01-26 20:42:15 +0200
commitf50f34245efa1012855a693558680bcfb7c348e5 (patch)
tree76e8fc186a96fdc1106b5c634d5937e514a06037 /sub
parentec55a188e4d0232acd50fcabc0a3db3be2b9538e (diff)
parentc9026cb3210205b07e2e068467a18ee40f9259a3 (diff)
downloadmpv-f50f34245efa1012855a693558680bcfb7c348e5.tar.bz2
mpv-f50f34245efa1012855a693558680bcfb7c348e5.tar.xz
Merge branch 'sub'
* sub: sub/OSD: move some related files to sub/ subtitles: options: enable -ass by default subtitles: change default libass rendering style demux_mkv, chapters: change millisecond arithmetic to ns cleanup: rename ass_* functions to mp_ass_* subs: use correct font aspect ratio for libass + converted subs cleanup: some random minor code simplification and cleanup vf_vo: fix EOSD change detection bug sd_ass: remove subreader use, support plaintext markup subtitles: style support for common SubRip tags and MicroDVD core: ordered chapters: fix bad subtitle parameter subs/demux: don't try to enable sub track when creating it subtitles/demux: store duration instead of endpts in demux packets subtitles: add framework for subtitle decoders options: add special -leak-report option subtitles: remove code trying to handle text subs with libavcodec cleanup: move MP_NOPTS_VALUE definition to mpcommon.h subtitles: move global ass_track to struct osd_state core: move most mpcommon.c contents to mplayer.c core: move global "subdata" and "vo_sub_last" to mpctx subtitles: remove sub_last_pts hack options: move -noconfig to option struct, simplify
Diffstat (limited to 'sub')
-rw-r--r--sub/ass_mp.c355
-rw-r--r--sub/ass_mp.h82
-rw-r--r--sub/av_sub.c119
-rw-r--r--sub/av_sub.h30
-rw-r--r--sub/dec_sub.c72
-rw-r--r--sub/dec_sub.h14
-rw-r--r--sub/find_sub.c175
-rw-r--r--sub/font_load.c356
-rw-r--r--sub/font_load.h115
-rw-r--r--sub/font_load_ft.c1176
-rw-r--r--sub/osd_font.h545
-rw-r--r--sub/sd.h16
-rw-r--r--sub/sd_ass.c154
-rw-r--r--sub/spudec.c1390
-rw-r--r--sub/spudec.h45
-rw-r--r--sub/sub.c1348
-rw-r--r--sub/sub.h167
-rw-r--r--sub/sub_cc.c347
-rw-r--r--sub/sub_cc.h29
-rw-r--r--sub/subassconvert.c502
-rw-r--r--sub/subassconvert.h27
-rw-r--r--sub/subreader.c2504
-rw-r--r--sub/subreader.h114
-rw-r--r--sub/unrar_exec.c235
-rw-r--r--sub/unrar_exec.h54
-rw-r--r--sub/vobsub.c1443
-rw-r--r--sub/vobsub.h47
27 files changed, 11461 insertions, 0 deletions
diff --git a/sub/ass_mp.c b/sub/ass_mp.c
new file mode 100644
index 0000000000..98602ace03
--- /dev/null
+++ b/sub/ass_mp.c
@@ -0,0 +1,355 @@
+/*
+ * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
+ *
+ * This file is part of MPlayer.
+ *
+ * MPlayer is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * MPlayer 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with libass; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <inttypes.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <stdbool.h>
+
+#include <ass/ass.h>
+#include <ass/ass_types.h>
+
+#include <libavutil/common.h>
+
+#include "config.h"
+#include "mp_msg.h"
+#include "path.h"
+#include "ass_mp.h"
+#include "subreader.h"
+#include "stream/stream.h"
+
+#ifdef CONFIG_FONTCONFIG
+#include <fontconfig/fontconfig.h>
+#endif
+
+// libass-related command line options
+ASS_Library *ass_library;
+float ass_font_scale = 1.;
+float ass_line_spacing = 0.;
+int ass_top_margin = 0;
+int ass_bottom_margin = 0;
+int use_embedded_fonts = 1;
+char **ass_force_style_list = NULL;
+int ass_use_margins = 0;
+char *ass_color = NULL;
+char *ass_border_color = NULL;
+char *ass_styles_file = NULL;
+int ass_hinting = ASS_HINTING_LIGHT + 4; // light hinting for unscaled osd
+
+#ifdef CONFIG_FONTCONFIG
+extern int font_fontconfig;
+#else
+static int font_fontconfig = -1;
+#endif
+extern char *font_name;
+extern char *sub_font_name;
+extern float text_font_scale_factor;
+extern int subtitle_autoscale;
+
+#ifdef CONFIG_ICONV
+extern char *sub_cp;
+#else
+static char *sub_cp = 0;
+#endif
+
+ASS_Track *mp_ass_default_track(ASS_Library *library)
+{
+ ASS_Track *track = ass_new_track(library);
+
+ track->track_type = TRACK_TYPE_ASS;
+ track->Timer = 100.;
+ track->PlayResY = 288;
+ track->WrapStyle = 0;
+
+ if (ass_styles_file)
+ ass_read_styles(track, ass_styles_file, sub_cp);
+
+ if (track->n_styles == 0) {
+ track->Kerning = true;
+ int sid = ass_alloc_style(track);
+ ASS_Style *style = track->styles + sid;
+ style->Name = strdup("Default");
+ style->FontName = (font_fontconfig >= 0
+ && sub_font_name) ? strdup(sub_font_name)
+ : (font_fontconfig >= 0
+ && font_name) ? strdup(font_name) : strdup("Sans");
+ style->treat_fontname_as_pattern = 1;
+
+ double fs = track->PlayResY * text_font_scale_factor / 100.;
+ /* The font size is always proportional to video height only;
+ * real -subfont-autoscale behavior is not implemented.
+ * Apply a correction that corresponds to about 4:3 aspect ratio
+ * video to get a size somewhat closer to what non-libass rendering
+ * would produce with the same text_font_scale_factor
+ * and subtitle_autoscale.
+ */
+ if (subtitle_autoscale == 2)
+ fs *= 1.3;
+ else if (subtitle_autoscale == 3)
+ fs *= 1.7;
+
+ uint32_t c1 = 0xFFFFFF00;
+ uint32_t c2 = 0x00000000;
+ if (ass_color)
+ c1 = strtoll(ass_color, NULL, 16);
+ if (ass_border_color)
+ c2 = strtoll(ass_border_color, NULL, 16);
+
+ style->FontSize = fs;
+ style->PrimaryColour = c1;
+ style->SecondaryColour = c1;
+ style->OutlineColour = c2;
+ style->BackColour = 0x00000000;
+ style->BorderStyle = 1;
+ style->Alignment = 2;
+ style->Outline = fs / 16;
+ style->MarginL = 10;
+ style->MarginR = 10;
+ style->MarginV = 5;
+ style->ScaleX = 1.;
+ style->ScaleY = 1.;
+ }
+
+ ass_process_force_style(track);
+ return track;
+}
+
+static int check_duplicate_plaintext_event(ASS_Track *track)
+{
+ int i;
+ ASS_Event *evt = track->events + track->n_events - 1;
+
+ for (i = 0; i < track->n_events - 1; ++i) // ignoring last event, it is the one we are comparing with
+ if (track->events[i].Start == evt->Start &&
+ track->events[i].Duration == evt->Duration &&
+ strcmp(track->events[i].Text, evt->Text) == 0)
+ return 1;
+ return 0;
+}
+
+/**
+ * \brief Convert subtitle to ASS_Events for the given track
+ * \param track track
+ * \param sub subtitle to convert
+ * \return event id
+ * note: assumes that subtitle is _not_ fps-based; caller must manually correct
+ * Start and Duration in other case.
+ **/
+static int ass_process_subtitle(ASS_Track *track, subtitle *sub)
+{
+ int eid;
+ ASS_Event *event;
+ int len = 0, j;
+ char *p;
+ char *end;
+
+ eid = ass_alloc_event(track);
+ event = track->events + eid;
+
+ event->Start = sub->start * 10;
+ event->Duration = (sub->end - sub->start) * 10;
+ event->Style = 0;
+
+ for (j = 0; j < sub->lines; ++j)
+ len += sub->text[j] ? strlen(sub->text[j]) : 0;
+
+ len += 2 * sub->lines; // '\N', including the one after the last line
+ len += 6; // {\anX}
+ len += 1; // '\0'
+
+ event->Text = malloc(len);
+ end = event->Text + len;
+ p = event->Text;
+
+ if (sub->alignment)
+ p += snprintf(p, end - p, "{\\an%d}", sub->alignment);
+
+ for (j = 0; j < sub->lines; ++j)
+ p += snprintf(p, end - p, "%s\\N", sub->text[j]);
+
+ if (sub->lines > 0)
+ p -= 2; // remove last "\N"
+ *p = 0;
+
+ if (check_duplicate_plaintext_event(track)) {
+ ass_free_event(track, eid);
+ track->n_events--;
+ return -1;
+ }
+
+ mp_msg(MSGT_ASS, MSGL_V,
+ "plaintext event at %" PRId64 ", +%" PRId64 ": %s \n",
+ (int64_t) event->Start, (int64_t) event->Duration, event->Text);
+
+ return eid;
+}
+
+
+/**
+ * \brief Convert subdata to ASS_Track
+ * \param subdata subtitles struct from subreader
+ * \param fps video framerate
+ * \return newly allocated ASS_Track, filled with subtitles from subdata
+ */
+ASS_Track *mp_ass_read_subdata(ASS_Library *library, sub_data *subdata,
+ double fps)
+{
+ ASS_Track *track;
+ int i;
+
+ track = mp_ass_default_track(library);
+ track->name = subdata->filename ? strdup(subdata->filename) : 0;
+
+ for (i = 0; i < subdata->sub_num; ++i) {
+ int eid = ass_process_subtitle(track, subdata->subtitles + i);
+ if (eid < 0)
+ continue;
+ if (!subdata->sub_uses_time) {
+ track->events[eid].Start *= 100. / fps;
+ track->events[eid].Duration *= 100. / fps;
+ }
+ }
+ return track;
+}
+
+ASS_Track *mp_ass_read_stream(ASS_Library *library, const char *fname,
+ char *charset)
+{
+ int i;
+ char *buf = NULL;
+ ASS_Track *track;
+ size_t sz = 0;
+ size_t buf_alloc = 0;
+ stream_t *fd;
+
+ fd = open_stream(fname, NULL, NULL);
+ if (!fd)
+ // Stream code should have printed an error already
+ return NULL;
+ if (fd->end_pos > STREAM_BUFFER_SIZE)
+ /* read entire file if size is known */
+ buf_alloc = fd->end_pos;
+ else
+ buf_alloc = 1000;
+ for (;;) {
+ if (sz > 100000000) {
+ mp_tmsg(MSGT_ASS, MSGL_ERR, "Refusing to load subtitle file "
+ "larger than 100 MB: %s\n", fname);
+ sz = 0;
+ break;
+ }
+ buf_alloc = FFMAX(buf_alloc, sz + (sz >> 1));
+ buf_alloc = FFMIN(buf_alloc, 100000001);
+ buf = realloc(buf, buf_alloc + 1);
+ i = stream_read(fd, buf + sz, buf_alloc - sz);
+ if (i <= 0)
+ break;
+ sz += i;
+ }
+ free_stream(fd);
+ if (!sz) {
+ free(buf);
+ return NULL;
+ }
+ buf[sz] = 0;
+ buf = realloc(buf, sz + 1);
+ track = ass_read_memory(library, buf, sz, charset);
+ if (track) {
+ free(track->name);
+ track->name = strdup(fname);
+ }
+ free(buf);
+ return track;
+}
+
+void mp_ass_configure(ASS_Renderer *priv, int w, int h, bool unscaled)
+{
+ int hinting;
+ ass_set_frame_size(priv, w, h);
+ ass_set_margins(priv, ass_top_margin, ass_bottom_margin, 0, 0);
+ ass_set_use_margins(priv, ass_use_margins);
+ ass_set_font_scale(priv, ass_font_scale);
+ if (!unscaled && (ass_hinting & 4))
+ hinting = 0;
+ else
+ hinting = ass_hinting & 3;
+ ass_set_hinting(priv, hinting);
+ ass_set_line_spacing(priv, ass_line_spacing);
+}
+
+void mp_ass_configure_fonts(ASS_Renderer *priv)
+{
+ char *dir, *path, *family;
+ dir = get_path("fonts");
+ if (font_fontconfig < 0 && sub_font_name)
+ path = strdup(sub_font_name);
+ else if (font_fontconfig < 0 && font_name)
+ path = strdup(font_name);
+ else
+ path = get_path("subfont.ttf");
+ if (font_fontconfig >= 0 && sub_font_name)
+ family = strdup(sub_font_name);
+ else if (font_fontconfig >= 0 && font_name)
+ family = strdup(font_name);
+ else
+ family = 0;
+
+ ass_set_fonts(priv, path, family, font_fontconfig + 1, NULL, 1);
+
+ free(dir);
+ free(path);
+ free(family);
+}
+
+static void message_callback(int level, const char *format, va_list va, void *ctx)
+{
+ mp_msg(MSGT_ASS, level, "[ass] ");
+ mp_msg_va(MSGT_ASS, level, format, va);
+ // libass messages lack trailing \n
+ mp_msg(MSGT_ASS, level, "\n");
+}
+
+ASS_Library *mp_ass_init(void)
+{
+ ASS_Library *priv;
+ char *path = get_path("fonts");
+ priv = ass_library_init();
+ ass_set_message_cb(priv, message_callback, NULL);
+ ass_set_fonts_dir(priv, path);
+ ass_set_extract_fonts(priv, use_embedded_fonts);
+ ass_set_style_overrides(priv, ass_force_style_list);
+ free(path);
+ return priv;
+}
+
+int ass_force_reload = 0; // flag set if global ass-related settings were changed
+
+ASS_Image *mp_ass_render_frame(ASS_Renderer *priv, ASS_Track *track,
+ long long now, int *detect_change)
+{
+ if (ass_force_reload) {
+ ass_set_margins(priv, ass_top_margin, ass_bottom_margin, 0, 0);
+ ass_set_use_margins(priv, ass_use_margins);
+ ass_set_font_scale(priv, ass_font_scale);
+ ass_force_reload = 0;
+ }
+ return ass_render_frame(priv, track, now, detect_change);
+}
diff --git a/sub/ass_mp.h b/sub/ass_mp.h
new file mode 100644
index 0000000000..965b063403
--- /dev/null
+++ b/sub/ass_mp.h
@@ -0,0 +1,82 @@
+/*
+ * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
+ *
+ * This file is part of MPlayer.
+ *
+ * MPlayer is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * MPlayer 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with libass; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef MPLAYER_ASS_MP_H
+#define MPLAYER_ASS_MP_H
+
+#include <stdint.h>
+#include <stdbool.h>
+
+#include "config.h"
+#include "subreader.h"
+
+#ifdef CONFIG_ASS
+#include <ass/ass.h>
+#include <ass/ass_types.h>
+
+extern ASS_Library *ass_library;
+extern float ass_font_scale;
+extern float ass_line_spacing;
+extern int ass_top_margin;
+extern int ass_bottom_margin;
+extern int use_embedded_fonts;
+extern char **ass_force_style_list;
+extern int ass_use_margins;
+extern char *ass_color;
+extern char *ass_border_color;
+extern char *ass_styles_file;
+extern int ass_hinting;
+
+ASS_Track *mp_ass_default_track(ASS_Library *library);
+ASS_Track *mp_ass_read_subdata(ASS_Library *library, sub_data *subdata,
+ double fps);
+ASS_Track *mp_ass_read_stream(ASS_Library *library, const char *fname,
+ char *charset);
+
+void mp_ass_configure(ASS_Renderer *priv, int w, int h, bool unscaled);
+void mp_ass_configure_fonts(ASS_Renderer *priv);
+ASS_Library *mp_ass_init(void);
+
+extern int ass_force_reload;
+ASS_Image *mp_ass_render_frame(ASS_Renderer *priv, ASS_Track *track,
+ long long now, int *detect_change);
+
+#else /* CONFIG_ASS */
+
+/* Needed for EOSD code using this type to compile */
+
+typedef struct ass_image {
+ int w, h;
+ int stride;
+ unsigned char *bitmap;
+ uint32_t color;
+ int dst_x, dst_y;
+ struct ass_image *next;
+} ASS_Image;
+
+#endif
+
+typedef struct {
+ ASS_Image *imgs;
+ int changed;
+} mp_eosd_images_t;
+
+
+#endif /* MPLAYER_ASS_MP_H */
diff --git a/sub/av_sub.c b/sub/av_sub.c
new file mode 100644
index 0000000000..a68fbce083
--- /dev/null
+++ b/sub/av_sub.c
@@ -0,0 +1,119 @@
+/*
+ * This file is part of MPlayer.
+ *
+ * MPlayer is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * MPlayer 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with MPlayer; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <libavcodec/avcodec.h>
+
+#include "libmpdemux/stheader.h"
+#include "sub.h"
+#include "spudec.h"
+#include "av_sub.h"
+
+void reset_avsub(struct sh_sub *sh)
+{
+ if (sh->context) {
+ avcodec_close(sh->context);
+ av_freep(&sh->context);
+ }
+}
+
+/**
+ * Decode a subtitle packet via libavcodec.
+ * \return < 0 on error, > 0 if further processing is needed
+ */
+int decode_avsub(struct sh_sub *sh, uint8_t *data, int size,
+ double pts, double duration)
+{
+ AVCodecContext *ctx = sh->context;
+ enum CodecID cid = CODEC_ID_NONE;
+ int res;
+ int got_sub;
+ AVSubtitle sub;
+ AVPacket pkt;
+
+ switch (sh->type) {
+ case 'b':
+ cid = CODEC_ID_DVB_SUBTITLE; break;
+ case 'p':
+ cid = CODEC_ID_HDMV_PGS_SUBTITLE; break;
+ case 'x':
+ cid = CODEC_ID_XSUB; break;
+ }
+
+ av_init_packet(&pkt);
+ pkt.data = data;
+ pkt.size = size;
+ pkt.pts = pts * 1000;
+ if (duration >= 0)
+ pkt.convergence_duration = duration * 1000;
+ if (!ctx) {
+ AVCodec *sub_codec;
+ avcodec_init();
+ avcodec_register_all();
+ ctx = avcodec_alloc_context();
+ sub_codec = avcodec_find_decoder(cid);
+ if (!ctx || !sub_codec || avcodec_open(ctx, sub_codec) < 0) {
+ mp_msg(MSGT_SUBREADER, MSGL_FATAL,
+ "Could not open subtitle decoder\n");
+ av_freep(&ctx);
+ return -1;
+ }
+ sh->context = ctx;
+ }
+ res = avcodec_decode_subtitle2(ctx, &sub, &got_sub, &pkt);
+ if (res < 0)
+ return res;
+ if (pts != MP_NOPTS_VALUE) {
+ if (sub.end_display_time > sub.start_display_time)
+ duration = (sub.end_display_time - sub.start_display_time) / 1000.0;
+ pts += sub.start_display_time / 1000.0;
+ }
+ double endpts = MP_NOPTS_VALUE;
+ if (pts != MP_NOPTS_VALUE && duration >= 0)
+ endpts = pts + duration;
+ if (got_sub && vo_spudec && sub.num_rects == 0)
+ spudec_set_paletted(vo_spudec, NULL, 0, NULL, 0, 0, 0, 0, pts, endpts);
+ if (got_sub && sub.num_rects > 0) {
+ switch (sub.rects[0]->type) {
+ case SUBTITLE_BITMAP:
+ if (!vo_spudec)
+ vo_spudec = spudec_new_scaled(NULL, ctx->width, ctx->height, NULL, 0);
+ spudec_set_paletted(vo_spudec,
+ sub.rects[0]->pict.data[0],
+ sub.rects[0]->pict.linesize[0],
+ sub.rects[0]->pict.data[1],
+ sub.rects[0]->x,
+ sub.rects[0]->y,
+ sub.rects[0]->w,
+ sub.rects[0]->h,
+ pts,
+ endpts);
+ vo_osd_changed(OSDTYPE_SPU);
+ break;
+ default:
+ mp_msg(MSGT_SUBREADER, MSGL_ERR, "sd_avsub: unsupported subtitle "
+ "type from libavcodec\n");
+ res = -1;
+ break;
+ }
+ }
+#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(52, 82, 0)
+ if (got_sub)
+ avsubtitle_free(&sub);
+#endif
+ return res;
+}
diff --git a/sub/av_sub.h b/sub/av_sub.h
new file mode 100644
index 0000000000..af3edc4d34
--- /dev/null
+++ b/sub/av_sub.h
@@ -0,0 +1,30 @@
+/*
+ * This file is part of MPlayer.
+ *
+ * MPlayer is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * MPlayer 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with MPlayer; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef MPLAYER_AV_SUB_H
+#define MPLAYER_AV_SUB_H
+
+#include <stdint.h>
+
+struct sh_sub;
+
+void reset_avsub(struct sh_sub *sh);
+int decode_avsub(struct sh_sub *sh, uint8_t *data, int size,
+ double pts, double endpts);
+
+#endif /* MPLAYER_AV_SUB_H */
diff --git a/sub/dec_sub.c b/sub/dec_sub.c
new file mode 100644
index 0000000000..6a6d1d77e5
--- /dev/null
+++ b/sub/dec_sub.c
@@ -0,0 +1,72 @@
+/*
+ * This file is part of MPlayer.
+ *
+ * MPlayer is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * MPlayer 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with MPlayer; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <stdlib.h>
+#include <stdbool.h>
+#include <assert.h>
+
+#include "config.h"
+#include "libmpdemux/stheader.h"
+#include "sd.h"
+#include "dec_sub.h"
+#include "options.h"
+
+extern const struct sd_functions sd_ass;
+
+void sub_init(struct sh_sub *sh, struct osd_state *osd)
+{
+ struct MPOpts *opts = sh->opts;
+
+#ifdef CONFIG_ASS
+ if (opts->ass_enabled && is_text_sub(sh->type))
+ sh->sd_driver = &sd_ass;
+#endif
+ if (sh->sd_driver) {
+ sh->sd_driver->init(sh, osd);
+ sh->initialized = true;
+ sh->active = true;
+ }
+}
+
+void sub_decode(struct sh_sub *sh, struct osd_state *osd, void *data,
+ int data_len, double pts, double duration)
+{
+ if (sh->active && sh->sd_driver->decode)
+ sh->sd_driver->decode(sh, osd, data, data_len, pts, duration);
+}
+
+void sub_reset(struct sh_sub *sh, struct osd_state *osd)
+{
+ if (sh->active && sh->sd_driver->reset)
+ sh->sd_driver->reset(sh, osd);
+}
+
+void sub_switchoff(struct sh_sub *sh, struct osd_state *osd)
+{
+ if (sh->active && sh->sd_driver->switch_off)
+ sh->sd_driver->switch_off(sh, osd);
+ sh->active = false;
+}
+
+void sub_uninit(struct sh_sub *sh)
+{
+ assert (!sh->active);
+ if (sh->initialized && sh->sd_driver->uninit)
+ sh->sd_driver->uninit(sh);
+ sh->initialized = false;
+}
diff --git a/sub/dec_sub.h b/sub/dec_sub.h
new file mode 100644
index 0000000000..d6fbef25f0
--- /dev/null
+++ b/sub/dec_sub.h
@@ -0,0 +1,14 @@
+struct sh_sub;
+struct osd_state;
+
+static inline bool is_text_sub(int type)
+{
+ return type == 't' || type == 'm' || type == 'a';
+}
+
+void sub_decode(struct sh_sub *sh, struct osd_state *osd, void *data,
+ int data_len, double pts, double duration);
+void sub_init(struct sh_sub *sh, struct osd_state *osd);
+void sub_reset(struct sh_sub *sh, struct osd_state *osd);
+void sub_switchoff(struct sh_sub *sh, struct osd_state *osd);
+void sub_uninit(struct sh_sub *sh);
diff --git a/sub/find_sub.c b/sub/find_sub.c
new file mode 100644
index 0000000000..97c232b1db
--- /dev/null
+++ b/sub/find_sub.c
@@ -0,0 +1,175 @@
+/*
+ * .SUB
+ *
+ * This file is part of MPlayer.
+ *
+ * MPlayer is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * MPlayer 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with MPlayer; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "config.h"
+
+#include <stdio.h>
+
+#include "libvo/video_out.h"
+#include "sub.h"
+#include "subreader.h"
+
+#include "mp_msg.h"
+#include "mpcommon.h"
+#include "mplayer.h"
+
+static int current_sub=0;
+
+//static subtitle* subtitles=NULL;
+static int nosub_range_start=-1;
+static int nosub_range_end=-1;
+static const sub_data *last_sub_data = NULL;
+
+void step_sub(sub_data *subd, float pts, int movement) {
+ subtitle *subs;
+ int key;
+
+ if (subd == NULL) return;
+ subs = subd->subtitles;
+ key = (pts+sub_delay) * (subd->sub_uses_time ? 100 : sub_fps);
+
+ /* Tell the OSD subsystem that the OSD contents will change soon */
+ vo_osd_changed(OSDTYPE_SUBTITLE);
+
+ /* If we are moving forward, don't count the next (current) subtitle
+ * if we haven't displayed it yet. Same when moving other direction.
+ */
+ if (movement > 0 && key < subs[current_sub].start)
+ movement--;
+ if (movement < 0 && key >= subs[current_sub].end)
+ movement++;
+
+ /* Never move beyond first or last subtitle. */
+ if (current_sub+movement < 0)
+ movement = 0-current_sub;
+ if (current_sub+movement >= subd->sub_num)
+ movement = subd->sub_num - current_sub - 1;
+
+ current_sub += movement;
+ sub_delay = subs[current_sub].start / (subd->sub_uses_time ? 100 : sub_fps) - pts;
+}
+
+void find_sub(struct MPContext *mpctx, sub_data* subd,int key){
+ subtitle *subs;
+ subtitle *new_sub = NULL;
+ int i,j;
+
+ if ( !subd || subd->sub_num == 0) return;
+ subs = subd->subtitles;
+
+ if (last_sub_data != subd) {
+ // Sub data changed, reset nosub range.
+ last_sub_data = subd;
+ nosub_range_start = -1;
+ nosub_range_end = -1;
+ }
+
+ if(vo_sub){
+ if(key>=vo_sub->start && key<=vo_sub->end) return; // OK!
+ } else {
+ if(key>nosub_range_start && key<nosub_range_end) return; // OK!
+ }
+ // sub changed!
+
+ /* Tell the OSD subsystem that the OSD contents will change soon */
+ vo_osd_changed(OSDTYPE_SUBTITLE);
+
+ if(key<=0){
+ // no sub here
+ goto update;
+ }
+
+// printf("\r---- sub changed ----\n");
+
+ // check next sub.
+ if(current_sub>=0 && current_sub+1 < subd->sub_num){
+ if(key>subs[current_sub].end && key<subs[current_sub+1].start){
+ // no sub
+ nosub_range_start=subs[current_sub].end;
+ nosub_range_end=subs[current_sub+1].start;
+ goto update;
+ }
+ // next sub?
+ ++current_sub;
+ new_sub=&subs[current_sub];
+ if(key>=new_sub->start && key<=new_sub->end) goto update; // OK!
+ }
+
+// printf("\r---- sub log search... ----\n");
+
+ // use logarithmic search:
+ i=0;
+ j = subd->sub_num - 1;
+// printf("Searching %d in %d..%d\n",key,subs[i].start,subs[j].end);
+ while(j>=i){
+ current_sub=(i+j+1)/2;
+ new_sub=&subs[current_sub];
+ if(key<new_sub->start) j=current_sub-1;
+ else if(key>new_sub->end) i=current_sub+1;
+ else goto update; // found!
+ }
+// if(key>=new_sub->start && key<=new_sub->end) r