[MPlayer-dev-eng] [PATCH] show ogg subtitle language
Joey Parrish
joey at nicewarrior.org
Fri Sep 24 17:16:02 CEST 2004
On Fri, Sep 24, 2004 at 11:08:19AM +0200, Moritz Bunkus wrote:
> > else if (!strncasecmp(*cmt, "LANGUAGE=", 9))
> > {
> > val = *cmt + 9;
> > + // copy this language name into the array
> > + index = demux_ogg_sub_reverse_id(d, id);
> > + if (index >= 0) ogg_d->text_langs[index] = strdup(val);
> > // check for -slang if subs are uninitialized yet
> > if (os->text && d->sub->id == -1 && demux_ogg_check_lang(val, dvdsub_lang))
> > {
>
> In case of malicious files (several LANGUAGE= tags for one track) this
> leads to memleaks. Granted, this is uncommon...
Fixed.
> > +static int demux_ogg_sub_reverse_id(demuxer_t *demuxer, int id) {
>
> Functions must be documented :)
Fixed.
> > +int demux_ogg_sub_lang(demuxer_t *demuxer, int index) {
> > + ogg_demuxer_t *ogg_d = (ogg_demuxer_t *)demuxer->priv;
> > + return (index < 0) ? NULL : (index >= ogg_d->n_text) ? NULL : ogg_d->text_langs[index];
> > +}
>
> Same, but what the hell!? int demux... while it returns char * ? In fact
> it should return const char *.
Oops. Went crazy with cut and paste. Fixed.
> Otherwise it looks fine.
New version attached.
--Joey
--
"Living in the complex world of the future is somewhat
like having bees live in your head. But, there they are."
-------------- next part --------------
Index: mplayer.c
===================================================================
RCS file: /cvsroot/mplayer/main/mplayer.c,v
retrieving revision 1.789
diff -u -r1.789 mplayer.c
--- mplayer.c 19 Sep 2004 18:45:10 -0000 1.789
+++ mplayer.c 24 Sep 2004 15:11:29 -0000
@@ -3644,12 +3644,17 @@
language = vobsub_get_id(vo_vobsub, (unsigned int) vobsub_id);
snprintf(osd_text_tmp, 63, "Subtitles: (%d) %s", vobsub_id, language ? language : "unknown");
}
+#ifdef HAVE_OGGVORBIS
if (d_dvdsub && demuxer->type == DEMUXER_TYPE_OGG) {
if (dvdsub_id < 0)
snprintf(osd_text_tmp, 63, "Subtitles: (off)");
- else
- snprintf(osd_text_tmp, 63, "Subtitles: (%d)", dvdsub_id);
+ else {
+ char *lang = demux_ogg_sub_lang(demuxer, dvdsub_id);
+ if (!lang) lang = "unknown";
+ snprintf(osd_text_tmp, 63, "Subtitles: (%d) %s", dvdsub_id, lang);
+ }
}
+#endif
#ifdef USE_DVDREAD
if (vo_spudec && (demuxer->type != DEMUXER_TYPE_MATROSKA)) {
char lang[5] = "none";
Index: libmpdemux/demux_ogg.c
===================================================================
RCS file: /cvsroot/mplayer/main/libmpdemux/demux_ogg.c,v
retrieving revision 1.56
diff -u -r1.56 demux_ogg.c
--- libmpdemux/demux_ogg.c 24 Aug 2004 21:20:24 -0000 1.56
+++ libmpdemux/demux_ogg.c 24 Sep 2004 15:11:30 -0000
@@ -132,6 +132,7 @@
/* Used for subtitle switching. */
int n_text;
int *text_ids;
+ char **text_langs;
} ogg_demuxer_t;
#define NUM_VORBIS_HDR_PACKETS 3
@@ -428,6 +429,8 @@
{
char *hdr, *val;
char **cmt = vc->user_comments;
+ int index;
+ ogg_demuxer_t *ogg_d = (ogg_demuxer_t *)d->priv;
while(*cmt)
{
@@ -440,6 +443,13 @@
else if (!strncasecmp(*cmt, "LANGUAGE=", 9))
{
val = *cmt + 9;
+ // copy this language name into the array
+ index = demux_ogg_sub_reverse_id(d, id);
+ if (index >= 0) {
+ // in case of malicious files with more than one lang per track:
+ if (ogg_d->text_langs[index]) free(ogg_d->text_langs[index]);
+ ogg_d->text_langs[index] = strdup(val);
+ }
// check for -slang if subs are uninitialized yet
if (os->text && d->sub->id == -1 && demux_ogg_check_lang(val, dvdsub_lang))
{
@@ -683,6 +693,25 @@
return (index < 0) ? index : (index >= ogg_d->n_text) ? -1 : ogg_d->text_ids[index];
}
+// mplayer renumbers the ogg subtitles such that the first sub track is
+// addressed as -sid 0. so when storing ogg sub languages, we need to
+// translate the real track number into the one mplayer will want.
+static int demux_ogg_sub_reverse_id(demuxer_t *demuxer, int id) {
+ ogg_demuxer_t *ogg_d = (ogg_demuxer_t *)demuxer->priv;
+ int i;
+ for (i = 0; i < ogg_d->n_text; i++)
+ if (ogg_d->text_ids[i] == id) return i;
+ return -1;
+}
+
+// lookup the subtitle language by -sid index.
+// this will check bounds on the index and return either a string or NULL.
+// the caller MUST NOT free this string, it will be free'd on demux_close.
+char *demux_ogg_sub_lang(demuxer_t *demuxer, int index) {
+ ogg_demuxer_t *ogg_d = (ogg_demuxer_t *)demuxer->priv;
+ return (index < 0) ? NULL : (index >= ogg_d->n_text) ? NULL : ogg_d->text_langs[index];
+}
+
/// Open an ogg physical stream
int demux_ogg_open(demuxer_t* demuxer) {
ogg_demuxer_t* ogg_d;
@@ -932,6 +961,8 @@
ogg_d->n_text++;
ogg_d->text_ids = (int *)realloc(ogg_d->text_ids, sizeof(int) * ogg_d->n_text);
ogg_d->text_ids[ogg_d->n_text - 1] = ogg_d->num_sub;
+ ogg_d->text_langs = (char **)realloc(ogg_d->text_langs, sizeof(char *) * ogg_d->n_text);
+ ogg_d->text_langs[ogg_d->n_text - 1] = NULL;
demux_ogg_init_sub();
//// Unknown header type
} else
@@ -1380,6 +1411,7 @@
void demux_close_ogg(demuxer_t* demuxer) {
ogg_demuxer_t* ogg_d = demuxer->priv;
+ int i;
if(!ogg_d)
return;
@@ -1394,6 +1426,11 @@
free(ogg_d->syncpoints);
if (ogg_d->text_ids)
free(ogg_d->text_ids);
+ if (ogg_d->text_langs) {
+ for (i = 0; i < ogg_d->n_text; i++)
+ if (ogg_d->text_langs[i]) free(ogg_d->text_langs[i]);
+ free(ogg_d->text_langs);
+ }
free(ogg_d);
}
Index: libmpdemux/demuxer.h
===================================================================
RCS file: /cvsroot/mplayer/main/libmpdemux/demuxer.h,v
retrieving revision 1.69
diff -u -r1.69 demuxer.h
--- libmpdemux/demuxer.h 24 Aug 2004 21:20:24 -0000 1.69
+++ libmpdemux/demuxer.h 24 Sep 2004 15:11:30 -0000
@@ -282,6 +282,7 @@
/* Found in demux_ogg.c */
int demux_ogg_num_subs(demuxer_t *demuxer);
int demux_ogg_sub_id(demuxer_t *demuxer, int index);
+char *demux_ogg_sub_lang(demuxer_t *demuxer, int index);
#endif
#endif
More information about the MPlayer-dev-eng
mailing list