>From e10ad9ae7e0ab969e3ec008f4d2f92b576e5397a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tobias=20G=C3=BCntner?= <fatbull@web.de>
Date: Thu, 29 Sep 2011 08:50:39 +0000
Subject: [PATCH] Skip redundant chapters

---
 modules/access/vdr.c |   21 +++++++++++++++++----
 1 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/modules/access/vdr.c b/modules/access/vdr.c
index 873d93f..3ba50a7 100644
--- a/modules/access/vdr.c
+++ b/modules/access/vdr.c
@@ -116,6 +116,9 @@ vlc_module_end ()
  * Local prototypes, constants, structures
  *****************************************************************************/
 
+/* minimum gap between chapters in seconds */
+#define MIN_CHAPTER_GAP 5
+
 TYPEDEF_ARRAY( uint64_t, size_array_t );
 
 struct access_sys_t
@@ -845,6 +848,10 @@ static void ImportMarks( access_t *p_access )
     int i_chapter_offset = p_sys->fps / 1000 *
         var_InheritInteger( p_access, "vdr-chapter-offset" );
 
+    /* the last chapter started at this frame (init to 0 so
+     * we skip useless chapters near the beginning as well) */
+    int64_t i_prev_chapter = 0;
+
     /* parse lines of the form "0:00:00.00 foobar" */
     char *line = NULL;
     size_t line_len;
@@ -852,6 +859,11 @@ static void ImportMarks( access_t *p_access )
     {
         int64_t i_frame = ParseFrameNumber( line, p_sys->fps );
 
+        /* skip this chapter if we already added one a few seconds earlier */
+        if( i_frame - i_prev_chapter < (int)( p_sys->fps * MIN_CHAPTER_GAP ) )
+            continue;
+        i_prev_chapter = i_frame;
+
         /* move chapters (simple workaround for inaccurate cut marks) */
         if( i_frame > -i_chapter_offset )
             i_frame += i_chapter_offset;
@@ -879,7 +891,8 @@ static void ImportMarks( access_t *p_access )
         TAB_APPEND( p_marks->i_seekpoint, p_marks->seekpoint, sp );
     }
 
-    if( p_marks->i_seekpoint > 0 )
+    /* add a chapter at the beginning if missing */
+    if( p_marks->i_seekpoint > 0 && p_marks->seekpoint[0]->i_byte_offset > 0 )
     {
         seekpoint_t *sp = vlc_seekpoint_New();
         if( sp )
@@ -889,12 +902,12 @@ static void ImportMarks( access_t *p_access )
             sp->psz_name = strdup( _("Start") );
             TAB_INSERT( p_marks->i_seekpoint, p_marks->seekpoint, sp, 0 );
         }
-        p_sys->p_marks = p_marks;
     }
+
+    if( p_marks->i_seekpoint > 0 )
+        p_sys->p_marks = p_marks;
     else
-    {
         vlc_input_title_Delete( p_marks );
-    }
 
     fclose( marksfile );
     fclose( indexfile );
-- 
1.7.0.4

