Let vdr access return true for CAN_FASTSEEK.
[vlc.git] / modules / access / vdr.c
blob284a36bac19fdc634d817da83cfbb87b2a4d5309
1 /*****************************************************************************
2 * vdr.c: VDR recordings access plugin
3 *****************************************************************************
4 * Copyright (C) 2010 Tobias Güntner
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
21 /***
22 VDR splits recordings into multiple files and stores each recording in a
23 separate directory. If VLC opens a normal directory, the filesystem module
24 will add all files to the playlist. If, however, VLC opens a VDR recording,
25 this module will join all files within that directory and provide a single
26 continuous stream instead.
28 VDR recordings have either of two directory layouts:
29 1) PES format:
30 /path/to/0000-00-00.00.00.00.00.rec/
31 001.vdr, 002.vdr, 003.vdr, ...
32 index.vdr, info.vdr, marks.vdr, ...
33 2) TS format:
34 /path/to/0000-00-00.00.00.0.0.rec/
35 001.ts, 002.ts, 003.ts, ...
36 index, info, marks, ...
37 See http://www.vdr-wiki.de/ and http://www.tvdr.de/ for more information.
38 ***/
40 /*****************************************************************************
41 * Preamble
42 *****************************************************************************/
44 #ifdef HAVE_CONFIG_H
45 # include "config.h"
46 #endif
48 #ifdef HAVE_SYS_TYPES_H
49 # include <sys/types.h>
50 #endif
51 #ifdef HAVE_SYS_STAT_H
52 # include <sys/stat.h>
53 #endif
54 #ifdef HAVE_FCNTL_H
55 # include <fcntl.h>
56 #endif
57 #ifdef HAVE_UNISTD_H
58 # include <unistd.h>
59 #elif defined( WIN32 ) && !defined( UNDER_CE )
60 # include <io.h>
61 #endif
63 #include <ctype.h>
64 #include <time.h>
65 #include <errno.h>
67 #if defined( WIN32 ) && !defined( UNDER_CE )
68 # undef lseek
69 # define lseek _lseeki64
70 #endif
72 #include <vlc_common.h>
73 #include <vlc_plugin.h>
74 #include <vlc_access.h>
75 #include <vlc_input.h>
76 #include <vlc_fs.h>
77 #include <vlc_charset.h>
78 #include <vlc_dialog.h>
79 #include <vlc_configuration.h>
81 /*****************************************************************************
82 * Module descriptor
83 *****************************************************************************/
84 static int Open ( vlc_object_t * );
85 static void Close( vlc_object_t * );
87 #define HELP_TEXT N_("Support for VDR recordings (http://www.tvdr.de/).")
89 #define CHAPTER_OFFSET_TEXT N_("Chapter offset in ms")
90 #define CHAPTER_OFFSET_LONGTEXT N_( \
91 "Move all chapters. This value should be set in milliseconds." )
93 #define FPS_TEXT N_("Frame rate")
94 #define FPS_LONGTEXT N_( \
95 "Default frame rate for chapter import." )
97 vlc_module_begin ()
98 set_category( CAT_INPUT )
99 set_shortname( N_("VDR") )
100 set_help( HELP_TEXT )
101 set_subcategory( SUBCAT_INPUT_ACCESS )
102 set_description( N_("VDR recordings") )
103 add_integer( "vdr-chapter-offset", 0,
104 CHAPTER_OFFSET_TEXT, CHAPTER_OFFSET_LONGTEXT, true )
105 add_float_with_range( "vdr-fps", 25, 1, 1000,
106 FPS_TEXT, FPS_LONGTEXT, true )
107 set_capability( "access", 60 )
108 add_shortcut( "vdr" )
109 add_shortcut( "directory" )
110 add_shortcut( "dir" )
111 add_shortcut( "file" )
112 set_callbacks( Open, Close )
113 vlc_module_end ()
115 /*****************************************************************************
116 * Local prototypes, constants, structures
117 *****************************************************************************/
119 /* minimum chapter size in seconds */
120 #define MIN_CHAPTER_SIZE 5
122 TYPEDEF_ARRAY( uint64_t, size_array_t );
124 struct access_sys_t
126 /* file sizes of all parts */
127 size_array_t file_sizes;
129 /* index and fd of current open file */
130 unsigned i_current_file;
131 int fd;
133 /* meta data */
134 vlc_meta_t *p_meta;
136 /* cut marks */
137 input_title_t *p_marks;
138 float fps;
140 /* file format: true=TS, false=PES */
141 bool b_ts_format;
144 #define CURRENT_FILE_SIZE ARRAY_VAL(p_sys->file_sizes, p_sys->i_current_file)
145 #define FILE_SIZE(pos) ARRAY_VAL(p_sys->file_sizes, pos)
146 #define FILE_COUNT (unsigned)p_sys->file_sizes.i_size
148 static int Control( access_t *, int, va_list );
149 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len );
150 static int Seek( access_t *p_access, uint64_t i_pos);
151 static void FindSeekpoint( access_t *p_access );
152 static bool ScanDirectory( access_t *p_access, bool b_strict );
153 static char *GetFilePath( access_t *p_access, unsigned i_file );
154 static bool ImportNextFile( access_t *p_access );
155 static bool SwitchFile( access_t *p_access, unsigned i_file );
156 static void OptimizeForRead( int fd );
157 static void UpdateFileSize( access_t *p_access );
158 static int StatRelativeFile( access_t *p_access, const char *psz_file,
159 struct stat *p_stat );
160 static FILE *OpenRelativeFile( access_t *p_access, const char *psz_file );
161 static bool ReadLine( char **ppsz_line, size_t *pi_size, FILE *p_file );
162 static void ImportMeta( access_t *p_access );
163 static void ImportMarks( access_t *p_access );
164 static bool ReadIndexRecord( FILE *p_file, bool b_ts, int64_t i_frame,
165 uint64_t *pi_offset, uint16_t *pi_file_num );
166 static int64_t ParseFrameNumber( const char *psz_line, float fps );
168 /*****************************************************************************
169 * Open a directory
170 *****************************************************************************/
171 static int Open( vlc_object_t *p_this )
173 access_t *p_access = (access_t*)p_this;
175 if( !p_access->psz_filepath )
176 return VLC_EGENERIC;
178 /* Some tests can be skipped if this module was explicitly requested.
179 * That way, the user can play "corrupt" recordings if necessary
180 * and we can avoid false positives in the general case. */
181 bool b_strict = strcmp( p_access->psz_access, "vdr" );
183 /* Do a quick test based on the directory extension to see if this
184 * directory might contain a VDR recording. We can be reasonably
185 * sure if ScanDirectory() actually finds files. */
186 if( b_strict )
188 const char *psz_ext = strrchr( p_access->psz_filepath, '.' );
189 if( !psz_ext || strcasecmp( psz_ext, ".rec" ) )
190 return VLC_EGENERIC;
193 /* Only directories can be recordings */
194 struct stat st;
195 if( vlc_stat( p_access->psz_filepath, &st ) ||
196 !S_ISDIR( st.st_mode ) )
197 return VLC_EGENERIC;
199 access_sys_t *p_sys;
200 STANDARD_READ_ACCESS_INIT;
201 p_sys->fd = -1;
202 p_sys->fps = var_InheritFloat( p_access, "vdr-fps" );
203 ARRAY_INIT( p_sys->file_sizes );
205 /* Import all files and prepare playback. */
206 if( !ScanDirectory( p_access, b_strict ) ||
207 !SwitchFile( p_access, 0 ) )
209 Close( p_this );
210 return VLC_EGENERIC;
213 return VLC_SUCCESS;
216 /*****************************************************************************
217 * Close files and free resources
218 *****************************************************************************/
219 static void Close( vlc_object_t * p_this )
221 access_t *p_access = (access_t*)p_this;
222 access_sys_t *p_sys = p_access->p_sys;
224 if( p_sys->fd != -1 )
225 close( p_sys->fd );
226 ARRAY_RESET( p_sys->file_sizes );
228 if( p_sys->p_meta )
229 vlc_meta_Delete( p_sys->p_meta );
231 vlc_input_title_Delete( p_sys->p_marks );
232 free( p_sys );
235 /*****************************************************************************
236 * Determine format and import files
237 *****************************************************************************/
238 static bool ScanDirectory( access_t *p_access, bool b_strict )
240 access_sys_t *p_sys = p_access->p_sys;
242 /* find first part and determine directory format */
243 p_sys->b_ts_format = true;
244 if( !ImportNextFile( p_access ) )
246 p_sys->b_ts_format = !p_sys->b_ts_format;
247 if( !ImportNextFile( p_access ) )
248 return false;
251 /* meta data and index should exist */
252 if( b_strict )
254 struct stat st;
255 if( StatRelativeFile( p_access, "info", &st ) ||
256 StatRelativeFile( p_access, "index", &st ) )
257 return false;
260 /* get all remaining parts */
261 while( ImportNextFile( p_access ) )
262 continue;
264 /* import meta data etc. */
265 ImportMeta( p_access );
267 /* cut marks depend on meta data and file sizes */
268 ImportMarks( p_access );
270 return true;
273 /*****************************************************************************
274 * Control input stream
275 *****************************************************************************/
276 static int Control( access_t *p_access, int i_query, va_list args )
278 access_sys_t *p_sys = p_access->p_sys;
279 input_title_t ***ppp_title;
280 int i;
281 int64_t *pi64;
282 vlc_meta_t *p_meta;
284 switch( i_query )
286 case ACCESS_CAN_SEEK:
287 case ACCESS_CAN_FASTSEEK:
288 case ACCESS_CAN_PAUSE:
289 case ACCESS_CAN_CONTROL_PACE:
290 *va_arg( args, bool* ) = true;
291 break;
293 case ACCESS_GET_PTS_DELAY:
294 pi64 = va_arg( args, int64_t * );
295 *pi64 = INT64_C(1000)
296 * var_InheritInteger( p_access, "file-caching" );
297 break;
299 case ACCESS_SET_PAUSE_STATE:
300 /* nothing to do */
301 break;
303 case ACCESS_GET_TITLE_INFO:
304 /* return a copy of our seek points */
305 if( !p_sys->p_marks )
306 return VLC_EGENERIC;
307 ppp_title = va_arg( args, input_title_t*** );
308 *va_arg( args, int* ) = 1;
309 *ppp_title = malloc( sizeof( input_title_t** ) );
310 if( !*ppp_title )
311 return VLC_ENOMEM;
312 **ppp_title = vlc_input_title_Duplicate( p_sys->p_marks );
313 break;
315 case ACCESS_SET_TITLE:
316 /* ignore - only one title */
317 break;
319 case ACCESS_SET_SEEKPOINT:
320 i = va_arg( args, int );
321 /* Seek updates p_access->info */
322 return Seek( p_access, p_sys->p_marks->seekpoint[i]->i_byte_offset );
324 case ACCESS_GET_META:
325 if( !p_sys->p_meta )
326 return VLC_EGENERIC;
327 p_meta = va_arg( args, vlc_meta_t* );
328 vlc_meta_Merge( p_meta, p_sys->p_meta );
329 break;
331 case ACCESS_SET_PRIVATE_ID_STATE:
332 case ACCESS_GET_CONTENT_TYPE:
333 return VLC_EGENERIC;
335 default:
336 msg_Warn( p_access, "unimplemented query in control" );
337 return VLC_EGENERIC;
339 return VLC_SUCCESS;
342 /*****************************************************************************
343 * Read and concatenate files
344 *****************************************************************************/
345 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
347 access_sys_t *p_sys = p_access->p_sys;
349 if( p_sys->fd == -1 )
351 /* no more data */
352 p_access->info.b_eof = true;
353 return 0;
356 ssize_t i_ret = read( p_sys->fd, p_buffer, i_len );
358 if( i_ret > 0 )
360 /* success */
361 p_access->info.i_pos += i_ret;
362 UpdateFileSize( p_access );
363 FindSeekpoint( p_access );
364 return i_ret;
366 else if( i_ret == 0 )
368 /* check for new files in case the recording is still active */
369 if( p_sys->i_current_file >= FILE_COUNT - 1 )
370 ImportNextFile( p_access );
371 /* play next file */
372 SwitchFile( p_access, p_sys->i_current_file + 1 );
373 return -1;
375 else if( errno == EINTR )
377 /* try again later */
378 return -1;
380 else
382 /* abort on read error */
383 msg_Err( p_access, "failed to read (%m)" );
384 dialog_Fatal( p_access, _("File reading failed"), "%s (%m)",
385 _("VLC could not read the file.") );
386 SwitchFile( p_access, -1 );
387 return 0;
391 /*****************************************************************************
392 * Seek to a specific location in a file
393 *****************************************************************************/
394 static int Seek( access_t *p_access, uint64_t i_pos )
396 access_sys_t *p_sys = p_access->p_sys;
398 /* might happen if called by ACCESS_SET_SEEKPOINT */
399 i_pos = __MIN( i_pos, p_access->info.i_size );
401 p_access->info.i_pos = i_pos;
402 p_access->info.b_eof = false;
404 /* find correct chapter */
405 FindSeekpoint( p_access );
407 /* find correct file */
408 unsigned i_file = 0;
409 while( i_pos >= FILE_SIZE( i_file ) &&
410 i_file < FILE_COUNT - 1 )
412 i_pos -= FILE_SIZE( i_file );
413 i_file++;
415 if( !SwitchFile( p_access, i_file ) )
416 return VLC_EGENERIC;
418 /* adjust position within that file */
419 return lseek( p_sys->fd, i_pos, SEEK_SET ) != -1 ?
420 VLC_SUCCESS : VLC_EGENERIC;
423 /*****************************************************************************
424 * Change the chapter index to match the current position
425 *****************************************************************************/
426 static void FindSeekpoint( access_t *p_access )
428 access_sys_t *p_sys = p_access->p_sys;
429 if( !p_sys->p_marks )
430 return;
432 int i_new_seekpoint = p_access->info.i_seekpoint;
433 if( p_access->info.i_pos < (uint64_t)p_sys->p_marks->
434 seekpoint[ p_access->info.i_seekpoint ]->i_byte_offset )
436 /* i_pos moved backwards, start fresh */
437 i_new_seekpoint = 0;
440 /* only need to check the following seekpoints */
441 while( i_new_seekpoint + 1 < p_sys->p_marks->i_seekpoint &&
442 p_access->info.i_pos >= (uint64_t)p_sys->p_marks->
443 seekpoint[ i_new_seekpoint + 1 ]->i_byte_offset )
445 i_new_seekpoint++;
448 /* avoid unnecessary events */
449 if( p_access->info.i_seekpoint != i_new_seekpoint )
451 p_access->info.i_seekpoint = i_new_seekpoint;
452 p_access->info.i_update |= INPUT_UPDATE_SEEKPOINT;
456 /*****************************************************************************
457 * Returns the path of a certain part
458 *****************************************************************************/
459 static char *GetFilePath( access_t *p_access, unsigned i_file )
461 char *psz_path;
462 if( asprintf( &psz_path, p_access->p_sys->b_ts_format ?
463 "%s" DIR_SEP "%05u.ts" : "%s" DIR_SEP "%03u.vdr",
464 p_access->psz_filepath, i_file + 1 ) == -1 )
465 return NULL;
466 else
467 return psz_path;
470 /*****************************************************************************
471 * Check if another part exists and import it
472 *****************************************************************************/
473 static bool ImportNextFile( access_t *p_access )
475 access_sys_t *p_sys = p_access->p_sys;
477 char *psz_path = GetFilePath( p_access, FILE_COUNT );
478 if( !psz_path )
479 return false;
481 struct stat st;
482 if( vlc_stat( psz_path, &st ) )
484 msg_Dbg( p_access, "could not stat %s: %m", psz_path );
485 free( psz_path );
486 return false;
488 if( !S_ISREG( st.st_mode ) )
490 msg_Dbg( p_access, "%s is not a regular file", psz_path );
491 free( psz_path );
492 return false;
494 msg_Dbg( p_access, "%s exists", psz_path );
495 free( psz_path );
497 ARRAY_APPEND( p_sys->file_sizes, st.st_size );
498 p_access->info.i_size += st.st_size;
499 p_access->info.i_update |= INPUT_UPDATE_SIZE;
501 return true;
504 /*****************************************************************************
505 * Close the current file and open another
506 *****************************************************************************/
507 static bool SwitchFile( access_t *p_access, unsigned i_file )
509 access_sys_t *p_sys = p_access->p_sys;
511 /* requested file already open? */
512 if( p_sys->fd != -1 && p_sys->i_current_file == i_file )
513 return true;
515 /* close old file */
516 if( p_sys->fd != -1 )
518 close( p_sys->fd );
519 p_sys->fd = -1;
522 /* switch */
523 if( i_file >= FILE_COUNT )
524 return false;
525 p_sys->i_current_file = i_file;
527 /* open new file */
528 char *psz_path = GetFilePath( p_access, i_file );
529 if( !psz_path )
530 return false;
531 p_sys->fd = vlc_open( psz_path, O_RDONLY );
533 if( p_sys->fd == -1 )
535 msg_Err( p_access, "Failed to open %s: %m", psz_path );
536 goto error;
539 /* cannot handle anything except normal files */
540 struct stat st;
541 if( fstat( p_sys->fd, &st ) || !S_ISREG( st.st_mode ) )
543 msg_Err( p_access, "%s is not a regular file", psz_path );
544 goto error;
547 OptimizeForRead( p_sys->fd );
549 msg_Dbg( p_access, "opened %s", psz_path );
550 free( psz_path );
551 return true;
553 error:
554 dialog_Fatal (p_access, _("File reading failed"), _("VLC could not"
555 " open the file \"%s\". (%m)"), psz_path);
556 if( p_sys->fd != -1 )
558 close( p_sys->fd );
559 p_sys->fd = -1;
561 free( psz_path );
562 return false;
565 /*****************************************************************************
566 * Some tweaks to speed up read()
567 *****************************************************************************/
568 static void OptimizeForRead( int fd )
570 /* cf. Open() in file access module */
571 VLC_UNUSED(fd);
572 #ifdef HAVE_POSIX_FADVISE
573 posix_fadvise( fd, 0, 4096, POSIX_FADV_WILLNEED );
574 posix_fadvise( fd, 0, 0, POSIX_FADV_NOREUSE );
575 #endif
576 #ifdef HAVE_FCNTL
577 #ifdef F_RDAHEAD
578 fcntl( fd, F_RDAHEAD, 1 );
579 #endif
580 #ifdef F_NOCACHE
581 fcntl( fd, F_NOCACHE, 1 );
582 #endif
583 #endif
586 /*****************************************************************************
587 * Fix size if the (last) part is still growing
588 *****************************************************************************/
589 static void UpdateFileSize( access_t *p_access )
591 access_sys_t *p_sys = p_access->p_sys;
592 struct stat st;
594 if( p_access->info.i_size >= p_access->info.i_pos )
595 return;
597 /* TODO: not sure if this can happen or what to do in this case */
598 if( fstat( p_sys->fd, &st ) )
599 return;
600 if( (uint64_t)st.st_size <= CURRENT_FILE_SIZE )
601 return;
603 p_access->info.i_size -= CURRENT_FILE_SIZE;
604 CURRENT_FILE_SIZE = st.st_size;
605 p_access->info.i_size += CURRENT_FILE_SIZE;
606 p_access->info.i_update |= INPUT_UPDATE_SIZE;
609 /*****************************************************************************
610 * Stat file relative to base directory
611 *****************************************************************************/
612 static int StatRelativeFile( access_t *p_access, const char *psz_file,
613 struct stat *p_stat )
615 /* build path and add extension */
616 char *psz_path;
617 if( asprintf( &psz_path, "%s" DIR_SEP "%s%s",
618 p_access->psz_filepath, psz_file,
619 p_access->p_sys->b_ts_format ? "" : ".vdr" ) == -1 )
620 return -1;
622 int ret = vlc_stat( psz_path, p_stat );
623 if( ret )
624 msg_Dbg( p_access, "could not stat %s: %m", psz_path );
625 free( psz_path );
627 return ret;
630 /*****************************************************************************
631 * Open file relative to base directory for reading.
632 *****************************************************************************/
633 static FILE *OpenRelativeFile( access_t *p_access, const char *psz_file )
635 /* build path and add extension */
636 char *psz_path;
637 if( asprintf( &psz_path, "%s" DIR_SEP "%s%s",
638 p_access->psz_filepath, psz_file,
639 p_access->p_sys->b_ts_format ? "" : ".vdr" ) == -1 )
640 return NULL;
642 FILE *file = vlc_fopen( psz_path, "rb" );
643 if( !file )
644 msg_Warn( p_access, "Failed to open %s: %m", psz_path );
645 free( psz_path );
647 return file;
650 /*****************************************************************************
651 * Read a line of text. Returns false on error or EOF.
652 *****************************************************************************/
653 static bool ReadLine( char **ppsz_line, size_t *pi_size, FILE *p_file )
655 ssize_t read = getline( ppsz_line, pi_size, p_file );
657 if( read == -1 )
659 /* automatically free buffer on eof */
660 free( *ppsz_line );
661 *ppsz_line = NULL;
662 return false;
665 if( read > 0 && (*ppsz_line)[ read - 1 ] == '\n' )
666 (*ppsz_line)[ read - 1 ] = '\0';
667 EnsureUTF8( *ppsz_line );
669 return true;
672 /*****************************************************************************
673 * Import meta data
674 *****************************************************************************/
675 static void ImportMeta( access_t *p_access )
677 access_sys_t *p_sys = p_access->p_sys;
679 FILE *infofile = OpenRelativeFile( p_access, "info" );
680 if( !infofile )
681 return;
683 vlc_meta_t *p_meta = vlc_meta_New();
684 p_sys->p_meta = p_meta;
685 if( !p_meta )
687 fclose( infofile );
688 return;
691 char *line = NULL;
692 size_t line_len;
693 char *psz_title = NULL, *psz_smalltext = NULL, *psz_date = NULL;
695 while( ReadLine( &line, &line_len, infofile ) )
697 if( !isalpha( (unsigned char)line[0] ) || line[1] != ' ' )
698 continue;
700 char tag = line[0];
701 char *text = line + 2;
703 if( tag == 'C' )
705 char *psz_name = strchr( text, ' ' );
706 if( psz_name )
708 *psz_name = '\0';
709 vlc_meta_AddExtra( p_meta, "Channel", psz_name + 1 );
711 vlc_meta_AddExtra( p_meta, "Transponder", text );
714 else if( tag == 'E' )
716 unsigned i_id, i_start, i_length;
717 if( sscanf( text, "%u %u %u", &i_id, &i_start, &i_length ) == 3 )
719 char str[50];
720 struct tm tm;
721 time_t start = i_start;
722 localtime_r( &start, &tm );
724 /* TODO: locale */
725 strftime( str, sizeof(str), "%Y-%m-%d %H:%M", &tm );
726 vlc_meta_AddExtra( p_meta, "Date", str );
727 free( psz_date );
728 psz_date = strdup( str );
730 /* display in minutes */
731 i_length = ( i_length + 59 ) / 60;
732 snprintf( str, sizeof(str), "%u:%02u", i_length / 60, i_length % 60 );
733 vlc_meta_AddExtra( p_meta, "Duration", str );
737 else if( tag == 'T' )
739 free( psz_title );
740 psz_title = strdup( text );
741 vlc_meta_AddExtra( p_meta, "Title", text );
744 else if( tag == 'S' )
746 free( psz_smalltext );
747 psz_smalltext = strdup( text );
748 vlc_meta_AddExtra( p_meta, "Info", text );
751 else if( tag == 'D' )
753 for( char *p = text; *p; ++p )
755 if( *p == '|' )
756 *p = '\n';
758 vlc_meta_SetDescription( p_meta, text );
761 /* FPS are required to convert between timestamps and frames */
762 else if( tag == 'F' )
764 float fps = atof( text );
765 if( fps >= 1 )
766 p_sys->fps = fps;
767 vlc_meta_AddExtra( p_meta, "Frame Rate", text );
770 else if( tag == 'P' )
772 vlc_meta_AddExtra( p_meta, "Priority", text );
775 else if( tag == 'L' )
777 vlc_meta_AddExtra( p_meta, "Lifetime", text );
781 /* create a meaningful title */
782 int i_len = 10 +
783 ( psz_title ? strlen( psz_title ) : 0 ) +
784 ( psz_smalltext ? strlen( psz_smalltext ) : 0 ) +
785 ( psz_date ? strlen( psz_date ) : 0 );
786 char *psz_display = malloc( i_len );
788 if( psz_display )
790 *psz_display = '\0';
791 if( psz_title )
792 strcat( psz_display, psz_title );
793 if( psz_title && psz_smalltext )
794 strcat( psz_display, " - " );
795 if( psz_smalltext )
796 strcat( psz_display, psz_smalltext );
797 if( ( psz_title || psz_smalltext ) && psz_date )
799 strcat( psz_display, " (" );
800 strcat( psz_display, psz_date );
801 strcat( psz_display, ")" );
803 if( *psz_display )
804 vlc_meta_SetTitle( p_meta, psz_display );
807 free( psz_display );
808 free( psz_title );
809 free( psz_smalltext );
810 free( psz_date );
812 fclose( infofile );
815 /*****************************************************************************
816 * Import cut marks and convert them to seekpoints (chapters).
817 *****************************************************************************/
818 static void ImportMarks( access_t *p_access )
820 access_sys_t *p_sys = p_access->p_sys;
822 FILE *marksfile = OpenRelativeFile( p_access, "marks" );
823 if( !marksfile )
824 return;
826 FILE *indexfile = OpenRelativeFile( p_access, "index" );
827 if( !indexfile )
829 fclose( marksfile );
830 return;
833 /* get the length of this recording (index stores 8 bytes per frame) */
834 struct stat st;
835 if( fstat( fileno( indexfile ), &st ) )
837 fclose( marksfile );
838 fclose( indexfile );
839 return;
841 int64_t i_frame_count = st.st_size / 8;
843 /* Put all cut marks in a "dummy" title */
844 input_title_t *p_marks = vlc_input_title_New();
845 if( !p_marks )
847 fclose( marksfile );
848 fclose( indexfile );
849 return;
851 p_marks->psz_name = strdup( _("VDR Cut Marks") );
853 /* offset for chapter positions */
854 int i_chapter_offset = p_sys->fps / 1000 *
855 var_InheritInteger( p_access, "vdr-chapter-offset" );
857 /* minimum chapter size in frames */
858 int i_min_chapter_size = p_sys->fps * MIN_CHAPTER_SIZE;
860 /* the last chapter started at this frame (init to 0 so
861 * we skip useless chapters near the beginning as well) */
862 int64_t i_prev_chapter = 0;
864 /* parse lines of the form "0:00:00.00 foobar" */
865 char *line = NULL;
866 size_t line_len;
867 while( ReadLine( &line, &line_len, marksfile ) )
869 int64_t i_frame = ParseFrameNumber( line, p_sys->fps );
871 /* skip chapters which are near the end or too close to each other */
872 if( i_frame - i_prev_chapter < i_min_chapter_size ||
873 i_frame >= i_frame_count - i_min_chapter_size )
874 continue;
875 i_prev_chapter = i_frame;
877 /* move chapters (simple workaround for inaccurate cut marks) */
878 if( i_frame > -i_chapter_offset )
879 i_frame += i_chapter_offset;
880 else
881 i_frame = 0;
883 uint64_t i_offset;
884 uint16_t i_file_number;
885 if( !ReadIndexRecord( indexfile, p_sys->b_ts_format,
886 i_frame, &i_offset, &i_file_number ) )
887 continue;
888 if( i_file_number < 1 || i_file_number > FILE_COUNT )
889 continue;
891 /* add file sizes to get the "global" offset */
892 seekpoint_t *sp = vlc_seekpoint_New();
893 if( !sp )
894 continue;
895 sp->i_time_offset = i_frame * (int64_t)( CLOCK_FREQ / p_sys->fps );
896 sp->i_byte_offset = i_offset;
897 for( int i = 0; i + 1 < i_file_number; ++i )
898 sp->i_byte_offset += FILE_SIZE( i );
899 sp->psz_name = strdup( line );
901 TAB_APPEND( p_marks->i_seekpoint, p_marks->seekpoint, sp );
904 /* add a chapter at the beginning if missing */
905 if( p_marks->i_seekpoint > 0 && p_marks->seekpoint[0]->i_byte_offset > 0 )
907 seekpoint_t *sp = vlc_seekpoint_New();
908 if( sp )
910 sp->i_byte_offset = 0;
911 sp->i_time_offset = 0;
912 sp->psz_name = strdup( _("Start") );
913 TAB_INSERT( p_marks->i_seekpoint, p_marks->seekpoint, sp, 0 );
917 if( p_marks->i_seekpoint > 0 )
918 p_sys->p_marks = p_marks;
919 else
920 vlc_input_title_Delete( p_marks );
922 fclose( marksfile );
923 fclose( indexfile );
926 /*****************************************************************************
927 * Lookup frame offset in index file
928 *****************************************************************************/
929 static bool ReadIndexRecord( FILE *p_file, bool b_ts, int64_t i_frame,
930 uint64_t *pi_offset, uint16_t *pi_file_num )
932 uint8_t index_record[8];
933 if( fseek( p_file, sizeof(index_record) * i_frame, SEEK_SET ) != 0 )
934 return false;
935 if( fread( &index_record, sizeof(index_record), 1, p_file ) <= 0 )
936 return false;
938 /* VDR usually (only?) runs on little endian machines, but VLC has a
939 * broader audience. See recording.* in VDR source for data layout. */
940 if( b_ts )
942 uint64_t i_index_entry = GetQWLE( &index_record );
943 *pi_offset = i_index_entry & UINT64_C(0xFFFFFFFFFF);
944 *pi_file_num = i_index_entry >> 48;
946 else
948 *pi_offset = GetDWLE( &index_record );
949 *pi_file_num = index_record[5];
952 return true;
955 /*****************************************************************************
956 * Convert time stamp from file to frame number
957 *****************************************************************************/
958 static int64_t ParseFrameNumber( const char *psz_line, float fps )
960 unsigned h, m, s, f, n;
962 /* hour:min:sec.frame (frame is optional) */
963 n = sscanf( psz_line, "%u:%u:%u.%u", &h, &m, &s, &f );
964 if( n >= 3 )
966 if( n < 4 )
967 f = 1;
968 int64_t i_seconds = (int64_t)h * 3600 + (int64_t)m * 60 + s;
969 return (int64_t)( i_seconds * (double)fps ) + __MAX(1, f) - 1;
972 /* only a frame number */
973 int64_t i_frame = strtoll( psz_line, NULL, 10 );
974 return __MAX(1, i_frame) - 1;