codec/hxxx_helper: removing redundant new-line from call to msg_Dbg
[vlc.git] / modules / access / vdr.c
blob81b549cb756e539eebe19340766a9a6c5bfc9f94
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 it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * 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 00001.ts, 00002.ts, 00003.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 #include <sys/types.h>
49 #include <sys/stat.h>
50 #include <fcntl.h>
51 #include <unistd.h>
53 #include <ctype.h>
54 #include <time.h>
55 #include <errno.h>
57 #include <vlc_common.h>
58 #include <vlc_plugin.h>
59 #include <vlc_access.h>
60 #include <vlc_input.h>
61 #include <vlc_fs.h>
62 #include <vlc_charset.h>
63 #include <vlc_dialog.h>
64 #include <vlc_configuration.h>
66 /*****************************************************************************
67 * Module descriptor
68 *****************************************************************************/
69 static int Open ( vlc_object_t * );
70 static void Close( vlc_object_t * );
72 #define HELP_TEXT N_("Support for VDR recordings (http://www.tvdr.de/).")
74 #define CHAPTER_OFFSET_TEXT N_("Chapter offset in ms")
75 #define CHAPTER_OFFSET_LONGTEXT N_( \
76 "Move all chapters. This value should be set in milliseconds." )
78 #define FPS_TEXT N_("Frame rate")
79 #define FPS_LONGTEXT N_( \
80 "Default frame rate for chapter import." )
82 vlc_module_begin ()
83 set_category( CAT_INPUT )
84 set_shortname( N_("VDR") )
85 set_help( HELP_TEXT )
86 set_subcategory( SUBCAT_INPUT_ACCESS )
87 set_description( N_("VDR recordings") )
88 add_integer( "vdr-chapter-offset", 0,
89 CHAPTER_OFFSET_TEXT, CHAPTER_OFFSET_LONGTEXT, true )
90 add_float_with_range( "vdr-fps", 25, 1, 1000,
91 FPS_TEXT, FPS_LONGTEXT, true )
92 set_capability( "access", 60 )
93 add_shortcut( "vdr" )
94 add_shortcut( "directory" )
95 add_shortcut( "dir" )
96 add_shortcut( "file" )
97 set_callbacks( Open, Close )
98 vlc_module_end ()
100 /*****************************************************************************
101 * Local prototypes, constants, structures
102 *****************************************************************************/
104 /* minimum chapter size in seconds */
105 #define MIN_CHAPTER_SIZE 5
107 TYPEDEF_ARRAY( uint64_t, size_array_t );
109 struct access_sys_t
111 /* file sizes of all parts */
112 size_array_t file_sizes;
113 uint64_t offset;
114 uint64_t size; /* total size */
116 /* index and fd of current open file */
117 unsigned i_current_file;
118 int fd;
120 /* meta data */
121 vlc_meta_t *p_meta;
123 /* cut marks */
124 input_title_t *p_marks;
125 uint64_t *offsets;
126 unsigned cur_seekpoint;
127 float fps;
129 /* file format: true=TS, false=PES */
130 bool b_ts_format;
133 #define CURRENT_FILE_SIZE ARRAY_VAL(p_sys->file_sizes, p_sys->i_current_file)
134 #define FILE_SIZE(pos) ARRAY_VAL(p_sys->file_sizes, pos)
135 #define FILE_COUNT (unsigned)p_sys->file_sizes.i_size
137 static int Control( access_t *, int, va_list );
138 static ssize_t Read( access_t *p_access, void *p_buffer, size_t i_len );
139 static int Seek( access_t *p_access, uint64_t i_pos);
140 static void FindSeekpoint( access_t *p_access );
141 static bool ScanDirectory( access_t *p_access );
142 static char *GetFilePath( access_t *p_access, unsigned i_file );
143 static bool ImportNextFile( access_t *p_access );
144 static bool SwitchFile( access_t *p_access, unsigned i_file );
145 static void OptimizeForRead( int fd );
146 static void UpdateFileSize( access_t *p_access );
147 static FILE *OpenRelativeFile( access_t *p_access, const char *psz_file );
148 static bool ReadLine( char **ppsz_line, size_t *pi_size, FILE *p_file );
149 static void ImportMeta( access_t *p_access );
150 static void ImportMarks( access_t *p_access );
151 static bool ReadIndexRecord( FILE *p_file, bool b_ts, int64_t i_frame,
152 uint64_t *pi_offset, uint16_t *pi_file_num );
153 static int64_t ParseFrameNumber( const char *psz_line, float fps );
154 static const char *BaseName( const char *psz_path );
156 /*****************************************************************************
157 * Open a directory
158 *****************************************************************************/
159 static int Open( vlc_object_t *p_this )
161 access_t *p_access = (access_t*)p_this;
163 if( !p_access->psz_filepath )
164 return VLC_EGENERIC;
166 /* Some tests can be skipped if this module was explicitly requested.
167 * That way, the user can play "corrupt" recordings if necessary
168 * and we can avoid false positives in the general case. */
169 bool b_strict = strcmp( p_access->psz_name, "vdr" );
171 /* Do a quick test based on the directory name to see if this
172 * directory might contain a VDR recording. We can be reasonably
173 * sure if ScanDirectory() actually finds files. */
174 if( b_strict )
176 char psz_extension[4];
177 int i_length = 0;
178 const char *psz_name = BaseName( p_access->psz_filepath );
179 if( sscanf( psz_name, "%*u-%*u-%*u.%*u.%*u.%*u%*[-.]%*u.%3s%n",
180 psz_extension, &i_length ) != 1 || strcasecmp( psz_extension, "rec" ) ||
181 ( psz_name[i_length] != DIR_SEP_CHAR && psz_name[i_length] != '\0' ) )
182 return VLC_EGENERIC;
185 /* Only directories can be recordings */
186 struct stat st;
187 if( vlc_stat( p_access->psz_filepath, &st ) ||
188 !S_ISDIR( st.st_mode ) )
189 return VLC_EGENERIC;
191 access_sys_t *p_sys = calloc( 1, sizeof( *p_sys ) );
193 if( unlikely(p_sys == NULL) )
194 return VLC_ENOMEM;
196 p_access->p_sys = p_sys;
197 p_sys->fd = -1;
198 p_sys->cur_seekpoint = 0;
199 p_sys->fps = var_InheritFloat( p_access, "vdr-fps" );
200 ARRAY_INIT( p_sys->file_sizes );
202 /* Import all files and prepare playback. */
203 if( !ScanDirectory( p_access ) ||
204 !SwitchFile( p_access, 0 ) )
206 Close( p_this );
207 return VLC_EGENERIC;
210 ACCESS_SET_CALLBACKS( Read, NULL, Control, Seek );
211 return VLC_SUCCESS;
214 /*****************************************************************************
215 * Close files and free resources
216 *****************************************************************************/
217 static void Close( vlc_object_t * p_this )
219 access_t *p_access = (access_t*)p_this;
220 access_sys_t *p_sys = p_access->p_sys;
222 if( p_sys->fd != -1 )
223 vlc_close( p_sys->fd );
224 ARRAY_RESET( p_sys->file_sizes );
226 if( p_sys->p_meta )
227 vlc_meta_Delete( p_sys->p_meta );
229 size_t count = p_sys->p_marks->i_seekpoint;
230 TAB_CLEAN( count, p_sys->offsets );
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 )
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 /* get all remaining parts */
252 while( ImportNextFile( p_access ) )
253 continue;
255 /* import meta data etc. */
256 ImportMeta( p_access );
258 /* cut marks depend on meta data and file sizes */
259 ImportMarks( p_access );
261 return true;
264 /*****************************************************************************
265 * Control input stream
266 *****************************************************************************/
267 static int Control( access_t *p_access, int i_query, va_list args )
269 access_sys_t *p_sys = p_access->p_sys;
270 input_title_t ***ppp_title;
271 int i;
272 int64_t *pi64;
273 vlc_meta_t *p_meta;
275 switch( i_query )
277 case STREAM_CAN_SEEK:
278 case STREAM_CAN_FASTSEEK:
279 case STREAM_CAN_PAUSE:
280 case STREAM_CAN_CONTROL_PACE:
281 *va_arg( args, bool* ) = true;
282 break;
284 case STREAM_GET_SIZE:
285 *va_arg( args, uint64_t* ) = p_sys->size;
286 break;
288 case STREAM_GET_PTS_DELAY:
289 pi64 = va_arg( args, int64_t * );
290 *pi64 = INT64_C(1000)
291 * var_InheritInteger( p_access, "file-caching" );
292 break;
294 case STREAM_SET_PAUSE_STATE:
295 /* nothing to do */
296 break;
298 case STREAM_GET_TITLE_INFO:
299 /* return a copy of our seek points */
300 if( !p_sys->p_marks )
301 return VLC_EGENERIC;
302 ppp_title = va_arg( args, input_title_t*** );
303 *va_arg( args, int* ) = 1;
304 *ppp_title = malloc( sizeof( **ppp_title ) );
305 if( !*ppp_title )
306 return VLC_ENOMEM;
307 **ppp_title = vlc_input_title_Duplicate( p_sys->p_marks );
308 break;
310 case STREAM_GET_TITLE:
311 *va_arg( args, unsigned * ) = 0;
312 break;
314 case STREAM_GET_SEEKPOINT:
315 *va_arg( args, unsigned * ) = p_sys->cur_seekpoint;
316 break;
318 case STREAM_GET_CONTENT_TYPE:
319 *va_arg( args, char ** ) =
320 strdup( p_sys->b_ts_format ? "video/MP2T" : "video/MP2P" );
321 break;
323 case STREAM_SET_TITLE:
324 /* ignore - only one title */
325 break;
327 case STREAM_SET_SEEKPOINT:
328 i = va_arg( args, int );
329 return Seek( p_access, p_sys->offsets[i] );
331 case STREAM_GET_META:
332 p_meta = va_arg( args, vlc_meta_t* );
333 vlc_meta_Merge( p_meta, p_sys->p_meta );
334 break;
336 default:
337 return VLC_EGENERIC;
339 return VLC_SUCCESS;
342 /*****************************************************************************
343 * Read and concatenate files
344 *****************************************************************************/
345 static ssize_t Read( access_t *p_access, void *p_buffer, size_t i_len )
347 access_sys_t *p_sys = p_access->p_sys;
349 if( p_sys->fd == -1 )
350 /* no more data */
351 return 0;
353 ssize_t i_ret = read( p_sys->fd, p_buffer, i_len );
355 if( i_ret > 0 )
357 /* success */
358 p_sys->offset += i_ret;
359 UpdateFileSize( p_access );
360 FindSeekpoint( p_access );
361 return i_ret;
363 else if( i_ret == 0 )
365 /* check for new files in case the recording is still active */
366 if( p_sys->i_current_file >= FILE_COUNT - 1 )
367 ImportNextFile( p_access );
368 /* play next file */
369 SwitchFile( p_access, p_sys->i_current_file + 1 );
370 return -1;
372 else if( errno == EINTR )
374 /* try again later */
375 return -1;
377 else
379 /* abort on read error */
380 msg_Err( p_access, "failed to read (%s)", vlc_strerror_c(errno) );
381 vlc_dialog_display_error( p_access, _("File reading failed"),
382 _("VLC could not read the file (%s)."),
383 vlc_strerror(errno) );
384 SwitchFile( p_access, -1 );
385 return 0;
389 /*****************************************************************************
390 * Seek to a specific location in a file
391 *****************************************************************************/
392 static int Seek( access_t *p_access, uint64_t i_pos )
394 access_sys_t *p_sys = p_access->p_sys;
396 /* might happen if called by STREAM_SET_SEEKPOINT */
397 i_pos = __MIN( i_pos, p_sys->size );
399 p_sys->offset = i_pos;
401 /* find correct chapter */
402 FindSeekpoint( p_access );
404 /* find correct file */
405 unsigned i_file = 0;
406 while( i_file < FILE_COUNT - 1 &&
407 i_pos >= FILE_SIZE( i_file ) )
409 i_pos -= FILE_SIZE( i_file );
410 i_file++;
412 if( !SwitchFile( p_access, i_file ) )
413 return VLC_EGENERIC;
415 /* adjust position within that file */
416 return lseek( p_sys->fd, i_pos, SEEK_SET ) != -1 ?
417 VLC_SUCCESS : VLC_EGENERIC;
420 /*****************************************************************************
421 * Change the chapter index to match the current position
422 *****************************************************************************/
423 static void FindSeekpoint( access_t *p_access )
425 access_sys_t *p_sys = p_access->p_sys;
426 if( !p_sys->p_marks )
427 return;
429 int new_seekpoint = p_sys->cur_seekpoint;
430 if( p_sys->offset < p_sys->offsets[p_sys->cur_seekpoint] )
432 /* i_pos moved backwards, start fresh */
433 new_seekpoint = 0;
436 /* only need to check the following seekpoints */
437 while( new_seekpoint + 1 < p_sys->p_marks->i_seekpoint &&
438 p_sys->offset >= p_sys->offsets[new_seekpoint + 1] )
440 new_seekpoint++;
443 p_sys->cur_seekpoint = new_seekpoint;
446 /*****************************************************************************
447 * Returns the path of a certain part
448 *****************************************************************************/
449 static char *GetFilePath( access_t *p_access, unsigned i_file )
451 access_sys_t *sys = p_access->p_sys;
452 char *psz_path;
454 if( asprintf( &psz_path, sys->b_ts_format ?
455 "%s" DIR_SEP "%05u.ts" : "%s" DIR_SEP "%03u.vdr",
456 p_access->psz_filepath, i_file + 1 ) == -1 )
457 return NULL;
458 else
459 return psz_path;
462 /*****************************************************************************
463 * Check if another part exists and import it
464 *****************************************************************************/
465 static bool ImportNextFile( access_t *p_access )
467 access_sys_t *p_sys = p_access->p_sys;
469 char *psz_path = GetFilePath( p_access, FILE_COUNT );
470 if( !psz_path )
471 return false;
473 struct stat st;
474 if( vlc_stat( psz_path, &st ) )
476 msg_Dbg( p_access, "could not stat %s: %s", psz_path,
477 vlc_strerror_c(errno) );
478 free( psz_path );
479 return false;
481 if( !S_ISREG( st.st_mode ) )
483 msg_Dbg( p_access, "%s is not a regular file", psz_path );
484 free( psz_path );
485 return false;
487 msg_Dbg( p_access, "%s exists", psz_path );
488 free( psz_path );
490 ARRAY_APPEND( p_sys->file_sizes, st.st_size );
491 p_sys->size += st.st_size;
493 return true;
496 /*****************************************************************************
497 * Close the current file and open another
498 *****************************************************************************/
499 static bool SwitchFile( access_t *p_access, unsigned i_file )
501 access_sys_t *p_sys = p_access->p_sys;
503 /* requested file already open? */
504 if( p_sys->fd != -1 && p_sys->i_current_file == i_file )
505 return true;
507 /* close old file */
508 if( p_sys->fd != -1 )
510 vlc_close( p_sys->fd );
511 p_sys->fd = -1;
514 /* switch */
515 if( i_file >= FILE_COUNT )
516 return false;
517 p_sys->i_current_file = i_file;
519 /* open new file */
520 char *psz_path = GetFilePath( p_access, i_file );
521 if( !psz_path )
522 return false;
523 p_sys->fd = vlc_open( psz_path, O_RDONLY );
525 if( p_sys->fd == -1 )
527 msg_Err( p_access, "Failed to open %s: %s", psz_path,
528 vlc_strerror_c(errno) );
529 goto error;
532 /* cannot handle anything except normal files */
533 struct stat st;
534 if( fstat( p_sys->fd, &st ) || !S_ISREG( st.st_mode ) )
536 msg_Err( p_access, "%s is not a regular file", psz_path );
537 goto error;
540 OptimizeForRead( p_sys->fd );
542 msg_Dbg( p_access, "opened %s", psz_path );
543 free( psz_path );
544 return true;
546 error:
547 vlc_dialog_display_error (p_access, _("File reading failed"), _("VLC could not"
548 " open the file \"%s\" (%s)."), psz_path, vlc_strerror(errno) );
549 if( p_sys->fd != -1 )
551 vlc_close( p_sys->fd );
552 p_sys->fd = -1;
554 free( psz_path );
555 return false;
558 /*****************************************************************************
559 * Some tweaks to speed up read()
560 *****************************************************************************/
561 static void OptimizeForRead( int fd )
563 /* cf. Open() in file access module */
564 VLC_UNUSED(fd);
565 #ifdef HAVE_POSIX_FADVISE
566 posix_fadvise( fd, 0, 4096, POSIX_FADV_WILLNEED );
567 posix_fadvise( fd, 0, 0, POSIX_FADV_NOREUSE );
568 #endif
569 #ifdef F_RDAHEAD
570 fcntl( fd, F_RDAHEAD, 1 );
571 #endif
572 #ifdef F_NOCACHE
573 fcntl( fd, F_NOCACHE, 0 );
574 #endif
577 /*****************************************************************************
578 * Fix size if the (last) part is still growing
579 *****************************************************************************/
580 static void UpdateFileSize( access_t *p_access )
582 access_sys_t *p_sys = p_access->p_sys;
583 struct stat st;
585 if( p_sys->size >= p_sys->offset )
586 return;
588 /* TODO: not sure if this can happen or what to do in this case */
589 if( fstat( p_sys->fd, &st ) )
590 return;
591 if( (uint64_t)st.st_size <= CURRENT_FILE_SIZE )
592 return;
594 p_sys->size -= CURRENT_FILE_SIZE;
595 CURRENT_FILE_SIZE = st.st_size;
596 p_sys->size += CURRENT_FILE_SIZE;
599 /*****************************************************************************
600 * Open file relative to base directory for reading.
601 *****************************************************************************/
602 static FILE *OpenRelativeFile( access_t *p_access, const char *psz_file )
604 access_sys_t *sys = p_access->p_sys;
606 /* build path and add extension */
607 char *psz_path;
608 if( asprintf( &psz_path, "%s" DIR_SEP "%s%s", p_access->psz_filepath,
609 psz_file, sys->b_ts_format ? "" : ".vdr" ) == -1 )
610 return NULL;
612 FILE *file = vlc_fopen( psz_path, "rb" );
613 if( !file )
614 msg_Warn( p_access, "Failed to open %s: %s", psz_path,
615 vlc_strerror_c(errno) );
616 free( psz_path );
618 return file;
621 /*****************************************************************************
622 * Read a line of text. Returns false on error or EOF.
623 *****************************************************************************/
624 static bool ReadLine( char **ppsz_line, size_t *pi_size, FILE *p_file )
626 ssize_t read = getline( ppsz_line, pi_size, p_file );
628 if( read == -1 )
630 /* automatically free buffer on eof */
631 free( *ppsz_line );
632 *ppsz_line = NULL;
633 return false;
636 if( read > 0 && (*ppsz_line)[ read - 1 ] == '\n' )
637 (*ppsz_line)[ read - 1 ] = '\0';
638 EnsureUTF8( *ppsz_line );
640 return true;
643 /*****************************************************************************
644 * Import meta data
645 *****************************************************************************/
646 static void ImportMeta( access_t *p_access )
648 access_sys_t *p_sys = p_access->p_sys;
650 FILE *infofile = OpenRelativeFile( p_access, "info" );
651 if( !infofile )
652 return;
654 vlc_meta_t *p_meta = vlc_meta_New();
655 p_sys->p_meta = p_meta;
656 if( !p_meta )
658 fclose( infofile );
659 return;
662 char *line = NULL;
663 size_t line_len;
664 char *psz_title = NULL, *psz_smalltext = NULL, *psz_date = NULL;
666 while( ReadLine( &line, &line_len, infofile ) )
668 if( !isalpha( (unsigned char)line[0] ) || line[1] != ' ' )
669 continue;
671 char tag = line[0];
672 char *text = line + 2;
674 if( tag == 'C' )
676 char *psz_name = strchr( text, ' ' );
677 if( psz_name )
679 *psz_name = '\0';
680 vlc_meta_AddExtra( p_meta, "Channel", psz_name + 1 );
682 vlc_meta_AddExtra( p_meta, "Transponder", text );
685 else if( tag == 'E' )
687 unsigned i_id, i_start, i_length;
688 if( sscanf( text, "%u %u %u", &i_id, &i_start, &i_length ) == 3 )
690 char str[50];
691 struct tm tm;
692 time_t start = i_start;
693 localtime_r( &start, &tm );
695 /* TODO: locale */
696 strftime( str, sizeof(str), "%Y-%m-%d %H:%M", &tm );
697 vlc_meta_AddExtra( p_meta, "Date", str );
698 free( psz_date );
699 psz_date = strdup( str );
701 /* display in minutes */
702 i_length = ( i_length + 59 ) / 60;
703 snprintf( str, sizeof(str), "%u:%02u", i_length / 60, i_length % 60 );
704 vlc_meta_AddExtra( p_meta, "Duration", str );
708 else if( tag == 'T' )
710 free( psz_title );
711 psz_title = strdup( text );
712 vlc_meta_AddExtra( p_meta, "Title", text );
715 else if( tag == 'S' )
717 free( psz_smalltext );
718 psz_smalltext = strdup( text );
719 vlc_meta_AddExtra( p_meta, "Info", text );
722 else if( tag == 'D' )
724 for( char *p = text; *p; ++p )
726 if( *p == '|' )
727 *p = '\n';
729 vlc_meta_SetDescription( p_meta, text );
732 /* FPS are required to convert between timestamps and frames */
733 else if( tag == 'F' )
735 float fps = atof( text );
736 if( fps >= 1 )
737 p_sys->fps = fps;
738 vlc_meta_AddExtra( p_meta, "Frame Rate", text );
741 else if( tag == 'P' )
743 vlc_meta_AddExtra( p_meta, "Priority", text );
746 else if( tag == 'L' )
748 vlc_meta_AddExtra( p_meta, "Lifetime", text );
752 /* create a meaningful title */
753 int i_len = 10 +
754 ( psz_title ? strlen( psz_title ) : 0 ) +
755 ( psz_smalltext ? strlen( psz_smalltext ) : 0 ) +
756 ( psz_date ? strlen( psz_date ) : 0 );
757 char *psz_display = malloc( i_len );
759 if( psz_display )
761 *psz_display = '\0';
762 if( psz_title )
763 strcat( psz_display, psz_title );
764 if( psz_title && psz_smalltext )
765 strcat( psz_display, " - " );
766 if( psz_smalltext )
767 strcat( psz_display, psz_smalltext );
768 if( ( psz_title || psz_smalltext ) && psz_date )
770 strcat( psz_display, " (" );
771 strcat( psz_display, psz_date );
772 strcat( psz_display, ")" );
774 if( *psz_display )
775 vlc_meta_SetTitle( p_meta, psz_display );
778 free( psz_display );
779 free( psz_title );
780 free( psz_smalltext );
781 free( psz_date );
783 fclose( infofile );
786 /*****************************************************************************
787 * Import cut marks and convert them to seekpoints (chapters).
788 *****************************************************************************/
789 static void ImportMarks( access_t *p_access )
791 access_sys_t *p_sys = p_access->p_sys;
793 FILE *marksfile = OpenRelativeFile( p_access, "marks" );
794 if( !marksfile )
795 return;
797 FILE *indexfile = OpenRelativeFile( p_access, "index" );
798 if( !indexfile )
800 fclose( marksfile );
801 return;
804 /* get the length of this recording (index stores 8 bytes per frame) */
805 struct stat st;
806 if( fstat( fileno( indexfile ), &st ) )
808 fclose( marksfile );
809 fclose( indexfile );
810 return;
812 int64_t i_frame_count = st.st_size / 8;
814 /* Put all cut marks in a "dummy" title */
815 input_title_t *p_marks = vlc_input_title_New();
816 if( !p_marks )
818 fclose( marksfile );
819 fclose( indexfile );
820 return;
822 p_marks->psz_name = strdup( _("VDR Cut Marks") );
823 p_marks->i_length = i_frame_count * (int64_t)( CLOCK_FREQ / p_sys->fps );
825 uint64_t *offsetv = NULL;
826 size_t offsetc = 0;
828 /* offset for chapter positions */
829 int i_chapter_offset = p_sys->fps / 1000 *
830 var_InheritInteger( p_access, "vdr-chapter-offset" );
832 /* minimum chapter size in frames */
833 int i_min_chapter_size = p_sys->fps * MIN_CHAPTER_SIZE;
835 /* the last chapter started at this frame (init to 0 so
836 * we skip useless chapters near the beginning as well) */
837 int64_t i_prev_chapter = 0;
839 /* parse lines of the form "0:00:00.00 foobar" */
840 char *line = NULL;
841 size_t line_len;
842 while( ReadLine( &line, &line_len, marksfile ) )
844 int64_t i_frame = ParseFrameNumber( line, p_sys->fps );
846 /* skip chapters which are near the end or too close to each other */
847 if( i_frame - i_prev_chapter < i_min_chapter_size ||
848 i_frame >= i_frame_count - i_min_chapter_size )
849 continue;
850 i_prev_chapter = i_frame;
852 /* move chapters (simple workaround for inaccurate cut marks) */
853 if( i_frame > -i_chapter_offset )
854 i_frame += i_chapter_offset;
855 else
856 i_frame = 0;
858 uint64_t i_offset;
859 uint16_t i_file_number;
860 if( !ReadIndexRecord( indexfile, p_sys->b_ts_format,
861 i_frame, &i_offset, &i_file_number ) )
862 continue;
863 if( i_file_number < 1 || i_file_number > FILE_COUNT )
864 continue;
866 /* add file sizes to get the "global" offset */
867 seekpoint_t *sp = vlc_seekpoint_New();
868 if( !sp )
869 continue;
870 sp->i_time_offset = i_frame * (int64_t)( CLOCK_FREQ / p_sys->fps );
871 sp->psz_name = strdup( line );
873 TAB_APPEND( p_marks->i_seekpoint, p_marks->seekpoint, sp );
874 TAB_APPEND( offsetc, offsetv, i_offset );
876 for( int i = 0; i + 1 < i_file_number; ++i )
877 offsetv[offsetc - 1] += FILE_SIZE( i );
880 /* add a chapter at the beginning if missing */
881 if( p_marks->i_seekpoint > 0 && offsetv[0] > 0 )
883 seekpoint_t *sp = vlc_seekpoint_New();
884 if( sp )
886 sp->i_time_offset = 0;
887 sp->psz_name = strdup( _("Start") );
888 TAB_INSERT( p_marks->i_seekpoint, p_marks->seekpoint, sp, 0 );
889 TAB_INSERT( offsetc, offsetv, UINT64_C(0), 0 );
893 if( p_marks->i_seekpoint > 0 )
895 p_sys->p_marks = p_marks;
896 p_sys->offsets = offsetv;
898 else
900 vlc_input_title_Delete( p_marks );
901 TAB_CLEAN( offsetc, offsetv );
904 fclose( marksfile );
905 fclose( indexfile );
908 /*****************************************************************************
909 * Lookup frame offset in index file
910 *****************************************************************************/
911 static bool ReadIndexRecord( FILE *p_file, bool b_ts, int64_t i_frame,
912 uint64_t *pi_offset, uint16_t *pi_file_num )
914 uint8_t index_record[8];
915 if( fseek( p_file, sizeof(index_record) * i_frame, SEEK_SET ) != 0 )
916 return false;
917 if( fread( &index_record, sizeof(index_record), 1, p_file ) < 1 )
918 return false;
920 /* VDR usually (only?) runs on little endian machines, but VLC has a
921 * broader audience. See recording.* in VDR source for data layout. */
922 if( b_ts )
924 uint64_t i_index_entry = GetQWLE( &index_record );
925 *pi_offset = i_index_entry & UINT64_C(0xFFFFFFFFFF);
926 *pi_file_num = i_index_entry >> 48;
928 else
930 *pi_offset = GetDWLE( &index_record );
931 *pi_file_num = index_record[5];
934 return true;
937 /*****************************************************************************
938 * Convert time stamp from file to frame number
939 *****************************************************************************/
940 static int64_t ParseFrameNumber( const char *psz_line, float fps )
942 unsigned h, m, s, f, n;
944 /* hour:min:sec.frame (frame is optional) */
945 n = sscanf( psz_line, "%u:%u:%u.%u", &h, &m, &s, &f );
946 if( n >= 3 )
948 if( n < 4 )
949 f = 1;
950 int64_t i_seconds = (int64_t)h * 3600 + (int64_t)m * 60 + s;
951 return (int64_t)( i_seconds * (double)fps ) + __MAX(1, f) - 1;
954 /* only a frame number */
955 int64_t i_frame = strtoll( psz_line, NULL, 10 );
956 return __MAX(1, i_frame) - 1;
959 /*****************************************************************************
960 * Return the last path component (including trailing separators)
961 *****************************************************************************/
962 static const char *BaseName( const char *psz_path )
964 const char *psz_name = psz_path + strlen( psz_path );
966 /* skip superfluous separators at the end */
967 while( psz_name > psz_path && psz_name[-1] == DIR_SEP_CHAR )
968 --psz_name;
970 /* skip last component */
971 while( psz_name > psz_path && psz_name[-1] != DIR_SEP_CHAR )
972 --psz_name;
974 return psz_name;