changeset 25251:80804f0631f4

Skip empty vobsub streams when selecting subtitles.
author ulion
date Mon, 03 Dec 2007 04:08:36 +0000
parents 9fd84c723f76
children ca7e863c97bb
files command.c mplayer.c vobsub.c vobsub.h
diffstat 4 files changed, 63 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/command.c	Mon Dec 03 03:29:55 2007 +0000
+++ b/command.c	Mon Dec 03 04:08:36 2007 +0000
@@ -1309,9 +1309,7 @@
 #endif
 
     if (source == SUB_SOURCE_VOBSUB) {
-	vobsub_id =
-	    mpctx->global_sub_pos -
-	    mpctx->global_sub_indices[SUB_SOURCE_VOBSUB];
+        vobsub_id = vobsub_get_id_by_index(vo_vobsub, mpctx->global_sub_pos - mpctx->global_sub_indices[SUB_SOURCE_VOBSUB]);
     } else if (source == SUB_SOURCE_SUBS) {
 	mpctx->set_of_sub_pos =
 	    mpctx->global_sub_pos - mpctx->global_sub_indices[SUB_SOURCE_SUBS];
@@ -1472,7 +1470,13 @@
     case M_PROPERTY_GET:
         if (!arg)
             return M_PROPERTY_ERROR;
-        *(int *) arg = (is_cur_source) ? mpctx->global_sub_pos - offset : -1;
+        if (is_cur_source) {
+            *(int *) arg = mpctx->global_sub_pos - offset;
+            if (source == SUB_SOURCE_VOBSUB)
+                *(int *) arg = vobsub_get_id_by_index(vo_vobsub, *(int *) arg);
+        }
+        else
+            *(int *) arg = -1;
         return M_PROPERTY_OK;
     case M_PROPERTY_PRINT:
         if (!arg)
@@ -1487,8 +1491,11 @@
         if (!arg)
             return M_PROPERTY_ERROR;
         if (*(int *) arg >= 0) {
-            mpctx->global_sub_pos = offset + *(int *) arg;
-            if (mpctx->global_sub_pos >= mpctx->global_sub_size
+            int index = *(int *)arg;
+            if (source == SUB_SOURCE_VOBSUB)
+                index = vobsub_get_index_by_id(vo_vobsub, index);
+            mpctx->global_sub_pos = offset + index;
+            if (index < 0 || mpctx->global_sub_pos >= mpctx->global_sub_size
                     || sub_source(mpctx) != source) {
                 mpctx->global_sub_pos = -1;
                 *(int *) arg = -1;
--- a/mplayer.c	Mon Dec 03 03:29:55 2007 +0000
+++ b/mplayer.c	Mon Dec 03 04:08:36 2007 +0000
@@ -3172,9 +3172,10 @@
 
 if (mpctx->global_sub_size) {
   // find the best sub to use
-  if (vobsub_id >= 0) {
+  int vobsub_index_id = vobsub_get_index_by_id(vo_vobsub, vobsub_id);
+  if (vobsub_index_id >= 0) {
     // if user asks for a vobsub id, use that first.
-    mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_id;
+    mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_VOBSUB] + vobsub_index_id;
   } else if (dvdsub_id >= 0 && mpctx->global_sub_indices[SUB_SOURCE_DEMUX] >= 0) {
     // if user asks for a dvd sub id, use that next.
     mpctx->global_sub_pos = mpctx->global_sub_indices[SUB_SOURCE_DEMUX] + dvdsub_id;
--- a/vobsub.c	Mon Dec 03 03:29:55 2007 +0000
+++ b/vobsub.c	Mon Dec 03 04:08:36 2007 +0000
@@ -26,6 +26,9 @@
 #include "libavutil/common.h"
 
 extern int vobsub_id;
+// Record the original -vobsubid set by commandline, since vobsub_id will be
+// overridden if slang match any of vobsub streams.
+static int vobsubid = -2;
 
 /**********************************************************************
  * RAR stream handling
@@ -606,6 +609,7 @@
     packet_queue_t *spu_streams;
     unsigned int spu_streams_size;
     unsigned int spu_streams_current;
+    unsigned int spu_valid_streams_size;
 } vobsub_t;
 
 /* Make sure that the spu stream idx exists. */
@@ -1066,6 +1070,8 @@
     vobsub_t *vob = malloc(sizeof(vobsub_t));
     if(spu)
       *spu = NULL;
+    if (vobsubid == -2)
+      vobsubid = vobsub_id;
     if (vob) {
 	char *buf;
 	vob->custom = 0;
@@ -1075,6 +1081,7 @@
 	vob->spu_streams = NULL;
 	vob->spu_streams_size = 0;
 	vob->spu_streams_current = 0;
+	vob->spu_valid_streams_size = 0;
 	vob->delay = 0;
 	vob->forced_subs=0;
 	buf = malloc(strlen(name) + 5);
@@ -1173,8 +1180,12 @@
 		    }
 		}
 		vob->spu_streams_current = vob->spu_streams_size;
-		while (vob->spu_streams_current-- > 0)
+		while (vob->spu_streams_current-- > 0) {
 		    vob->spu_streams[vob->spu_streams_current].current_index = 0;
+		    if (vobsubid == vob->spu_streams_current ||
+			    vob->spu_streams[vob->spu_streams_current].packets_size > 0)
+			++vob->spu_valid_streams_size;
+		}
 		mpeg_free(mpg);
 	    }
 	    free(buf);
@@ -1199,7 +1210,7 @@
 vobsub_get_indexes_count(void *vobhandle)
 {
     vobsub_t *vob = (vobsub_t *) vobhandle;
-    return vob->spu_streams_size;
+    return vob->spu_valid_streams_size;
 }
 
 char *
@@ -1209,6 +1220,35 @@
     return (index < vob->spu_streams_size) ? vob->spu_streams[index].id : NULL;
 }
 
+int vobsub_get_id_by_index(void *vobhandle, unsigned int index)
+{
+    vobsub_t *vob = vobhandle;
+    int i, j;
+    if (vob == NULL)
+        return -1;
+    for (i = 0, j = 0; i < vob->spu_streams_size; ++i)
+        if (i == vobsubid || vob->spu_streams[i].packets_size > 0) {
+            if (j == index)
+                return i;
+            ++j;
+        }
+    return -1;
+}
+
+int vobsub_get_index_by_id(void *vobhandle, int id)
+{
+    vobsub_t *vob = vobhandle;
+    int i, j;
+    if (vob == NULL || id < 0 || id >= vob->spu_streams_size)
+        return -1;
+    if (id != vobsubid && !vob->spu_streams[id].packets_size)
+        return -1;
+    for (i = 0, j = 0; i < id; ++i)
+        if (i == vobsubid || vob->spu_streams[i].packets_size > 0)
+            ++j;
+    return j;
+}
+
 unsigned int 
 vobsub_get_forced_subs_flag(void const * const vobhandle)
 {
--- a/vobsub.h	Mon Dec 03 03:29:55 2007 +0000
+++ b/vobsub.h	Mon Dec 03 04:08:36 2007 +0000
@@ -10,6 +10,11 @@
 extern unsigned int vobsub_get_indexes_count(void * /* vobhandle */);
 extern char *vobsub_get_id(void * /* vobhandle */, unsigned int /* index */);
 
+/// Get vobsub id by its index in the valid streams.
+extern int vobsub_get_id_by_index(void *vobhandle, unsigned int index);
+/// Get index in the valid streams by vobsub id.
+extern int vobsub_get_index_by_id(void *vobhandle, int id);
+
 extern void *vobsub_out_open(const char *basename, const unsigned int *palette, unsigned int orig_width, unsigned int orig_height, const char *id, unsigned int index);
 extern void vobsub_out_output(void *me, const unsigned char *packet, int len, double pts);
 extern void vobsub_out_close(void *me);