]> granicus.if.org Git - handbrake/commitdiff
Fix for missing subtitles
authortiter <eric@lapsus.org>
Wed, 22 Feb 2006 19:32:43 +0000 (19:32 +0000)
committertiter <eric@lapsus.org>
Wed, 22 Feb 2006 19:32:43 +0000 (19:32 +0000)
git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk/libhb@28 b64f7644-9d1e-0410-96f1-a4d463321fa5

decsub.c
fifo.c
internal.h
render.c
sync.c

index 72f742bd9be08dfa204a47ff70d7ca5e5c2f6c62..de844a1c1b48147b404c8c8980fafc895b2415a5 100644 (file)
--- a/decsub.c
+++ b/decsub.c
@@ -222,6 +222,9 @@ static void ParseControls( hb_work_object_t * w )
     int command;
     int date, next;
 
+    w->pts_start = 0;
+    w->pts_stop  = 0;
+
     for( i = w->size_rle; ; )
     {
         date = ( w->buf[i] << 8 ) | w->buf[i+1]; i += 2;
@@ -300,6 +303,12 @@ static void ParseControls( hb_work_object_t * w )
         }
         i = next;
     }
+
+    if( !w->pts_stop )
+    {
+        /* Show it for 3 seconds */
+        w->pts_stop = w->pts_start + 3 * 90000;
+    }
 }
 
 /***********************************************************************
diff --git a/fifo.c b/fifo.c
index 81ca2a1844d074a516ea8482b2c3fdfb1e32a86a..acdb211888be63da1fde1c1cad70c8bbc3397862 100644 (file)
--- a/fifo.c
+++ b/fifo.c
@@ -136,6 +136,22 @@ hb_buffer_t * hb_fifo_see( hb_fifo_t * f )
     return b;
 }
 
+hb_buffer_t * hb_fifo_see2( hb_fifo_t * f )
+{
+    hb_buffer_t * b;
+
+    hb_lock( f->lock );
+    if( f->size < 2 )
+    {
+        hb_unlock( f->lock );
+        return NULL;
+    }
+    b = f->first->next;
+    hb_unlock( f->lock );
+
+    return b;
+}
+
 void hb_fifo_push( hb_fifo_t * f, hb_buffer_t * b )
 {
     if( !b )
index 02fb6157837fd6848e783236cecd0e41a5158f09..c93d48245c88be8e41ed1da5db2f1614e669a7a1 100644 (file)
@@ -61,6 +61,7 @@ int           hb_fifo_size( hb_fifo_t * );
 int           hb_fifo_is_full( hb_fifo_t * );
 hb_buffer_t * hb_fifo_get( hb_fifo_t * );
 hb_buffer_t * hb_fifo_see( hb_fifo_t * );
+hb_buffer_t * hb_fifo_see2( hb_fifo_t * );
 void          hb_fifo_push( hb_fifo_t *, hb_buffer_t * );
 void          hb_fifo_close( hb_fifo_t ** );
 
index 914fa39955c7bd016c50f130de43cb43fab5db8a..cdada7f1891e9da76be10de6b4d4d9c59d110382 100644 (file)
--- a/render.c
+++ b/render.c
@@ -34,24 +34,29 @@ static void ApplySub( hb_job_t * job, hb_buffer_t * buf,
         return;
     }
 
-    if( sub->width > title->width - job->crop[0] - job->crop[1] - 40 ||
-        sub->height > title->height - job->crop[2] - job->crop[3] - 40 )
-    {
-        /* The subtitle won't fit */
-        hb_buffer_close( _sub );
-        return;
-    }
-
-    /* If necessary, move the subtitle so it is 20 pixels far from each
-       border of the cropped picture */
-    offset_top = sub->y;
-    offset_top = MAX( offset_top, job->crop[0] + 20 );
-    offset_top = MIN( offset_top,
-            title->height - job->crop[1] - 20 - sub->height );
-    offset_left = sub->x;
-    offset_left = MAX( offset_left, job->crop[2] + 20 );
-    offset_left = MIN( offset_left,
-            title->width - job->crop[3] - 20 - sub->width );
+    /* If necessary, move the subtitle so it is not in a cropped zone.
+       When it won't fit, we center it so we loose as much on both ends.
+       Otherwise we try to leave a 20px margin around it. */
+
+    if( sub->height > title->height - job->crop[0] - job->crop[1] - 40 )
+        offset_top = job->crop[0] + ( title->height - job->crop[0] -
+                job->crop[1] - sub->height ) / 2;
+    else if( sub->y < job->crop[0] + 20 )
+        offset_top = job->crop[0] + 20;
+    else if( sub->y > title->height - job->crop[1] - 20 - sub->height )
+        offset_top = title->height - job->crop[1] - 20 - sub->height;
+    else
+        offset_top = sub->y;
+
+    if( sub->width > title->width - job->crop[2] - job->crop[3] - 40 )
+        offset_left = job->crop[2] + ( title->width - job->crop[2] -
+                job->crop[3] - sub->width ) / 2;
+    else if( sub->x < job->crop[2] + 20 )
+        offset_left = job->crop[2] + 20;
+    else if( sub->x > title->width - job->crop[3] - 20 - sub->width )
+        offset_left = title->width - job->crop[3] - 20 - sub->width;
+    else
+        offset_left = sub->x;
 
     lum   = sub->data;
     alpha = lum + sub->width * sub->height;
@@ -59,11 +64,18 @@ static void ApplySub( hb_job_t * job, hb_buffer_t * buf,
 
     for( i = 0; i < sub->height; i++ )
     {
-        for( j = 0; j < sub->width; j++ )
+        if( offset_top + i >= 0 && offset_top + i < title->height )
         {
-            out[j] = ( (uint16_t) out[j] * ( 16 - (uint16_t) alpha[j] ) +
-                       (uint16_t) lum[j] * (uint16_t) alpha[j] ) >> 4;
+            for( j = 0; j < sub->width; j++ )
+            {
+                if( offset_left + j >= 0 && offset_left + j < title->width )
+                {
+                    out[j] = ( (uint16_t) out[j] * ( 16 - (uint16_t) alpha[j] ) +
+                               (uint16_t) lum[j] * (uint16_t) alpha[j] ) >> 4;
+                }
+            }
         }
+
         lum   += sub->width;
         alpha += sub->width;
         out   += title->width;
diff --git a/sync.c b/sync.c
index e0530a03afdacbb01844438f126a894741559faf..866f872b55d4007dce2a3eb30bc618dcd86c5b19 100644 (file)
--- a/sync.c
+++ b/sync.c
@@ -307,10 +307,19 @@ static int SyncVideo( hb_work_object_t * w )
         /* Look for a subtitle for this frame */
         if( w->subtitle )
         {
-            /* Trash subtitles older than this picture */
-            while( ( sub = hb_fifo_see( w->subtitle->fifo_raw ) ) &&
-                    sub->stop < cur->start )
+            hb_buffer_t * sub2;
+            while( ( sub = hb_fifo_see( w->subtitle->fifo_raw ) ) )
             {
+                /* If two subtitles overlap, make the first one stop
+                   when the second one starts */
+                sub2 = hb_fifo_see2( w->subtitle->fifo_raw );
+                if( sub2 && sub->stop > sub2->start )
+                    sub->stop = sub2->start;
+
+                if( sub->stop > cur->start )
+                    break;
+
+                /* The subtitle is older than this picture, trash it */
                 sub = hb_fifo_get( w->subtitle->fifo_raw );
                 hb_buffer_close( &sub );
             }