demux: mkv: handle WAVE_FORMAT_MPEG_ADTS_AAC
[vlc.git] / modules / access / vdr.c
blob22528c6cbf56578bcbbca0836bf1001c63c6e3ff
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 = vlc_calloc( p_this, 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 );
234 /*****************************************************************************
235 * Determine format and import files
236 *****************************************************************************/
237 static bool ScanDirectory( access_t *p_access )
239 access_sys_t *p_sys = p_access->p_sys;
241 /* find first part and determine directory format */
242 p_sys->b_ts_format = true;
243 if( !ImportNextFile( p_access ) )
245 p_sys->b_ts_format = !p_sys->b_ts_format;
246 if( !ImportNextFile( p_access ) )
247 return false;
250 /* get all remaining parts */
251 while( ImportNextFile( p_access ) )
252 continue;
254 /* import meta data etc. */
255 ImportMeta( p_access );
257 /* cut marks depend on meta data and file sizes */
258 ImportMarks( p_access );
260 return true;
263 /*****************************************************************************
264 * Control input stream
265 *****************************************************************************/
266 static int Control( access_t *p_access, int i_query, va_list args )
268 access_sys_t *p_sys = p_access->p_sys;
269 input_title_t ***ppp_title;
270 int i;
271 int64_t *pi64;
272 vlc_meta_t *p_meta;
274 switch( i_query )
276 case STREAM_CAN_SEEK:
277 case STREAM_CAN_FASTSEEK:
278 case STREAM_CAN_PAUSE:
279 case STREAM_CAN_CONTROL_PACE:
280 *va_arg( args, bool* ) = true;
281 break;
283 case STREAM_GET_SIZE:
284 *va_arg( args, uint64_t* ) = p_sys->size;
285 break;
287 case STREAM_GET_PTS_DELAY:
288 pi64 = va_arg( args, int64_t * );
289 *pi64 = INT64_C(1000)
290 * var_InheritInteger( p_access, "file-caching" );
291 break;
293 case STREAM_SET_PAUSE_STATE:
294 /* nothing to do */
295 break;
297 case STREAM_GET_TITLE_INFO:
298 /* return a copy of our seek points */
299 if( !p_sys->p_marks )
300 return VLC_EGENERIC;
301 ppp_title = va_arg( args, input_title_t*** );
302 *va_arg( args, int* ) = 1;
303 *ppp_title = malloc( sizeof( **ppp_title ) );
304 if( !*ppp_title )
305 return VLC_ENOMEM;
306 **ppp_title = vlc_input_title_Duplicate( p_sys->p_marks );
307 break;
309 case STREAM_GET_TITLE:
310 *va_arg( args, unsigned * ) = 0;
311 break;
313 case STREAM_GET_SEEKPOINT:
314 *va_arg( args, unsigned * ) = p_sys->cur_seekpoint;
315 break;
317 case STREAM_GET_CONTENT_TYPE:
318 *va_arg( args, char ** ) =
319 strdup( p_sys->b_ts_format ? "video/MP2T" : "video/MP2P" );
320 break;
322 case STREAM_SET_TITLE:
323 /* ignore - only one title */
324 break;
326 case STREAM_SET_SEEKPOINT:
327 i = va_arg( args, int );
328 return Seek( p_access, p_sys->offsets[i] );
330 case STREAM_GET_META:
331 p_meta = va_arg( args, vlc_meta_t* );
332 vlc_meta_Merge( p_meta, p_sys->p_meta );
333 break;
335 default:
336 return VLC_EGENERIC;
338 return VLC_SUCCESS;
341 /*****************************************************************************
342 * Read and concatenate files
343 *****************************************************************************/
344 static ssize_t Read( access_t *p_access, void *p_buffer, size_t i_len )
346 access_sys_t *p_sys = p_access->p_sys;
348 if( p_sys->fd == -1 )
349 /* no more data */
350 return 0;
352 ssize_t i_ret = read( p_sys->fd, p_buffer, i_len );
354 if( i_ret > 0 )
356 /* success */
357 p_sys->offset += i_ret;
358 UpdateFileSize( p_access );
359 FindSeekpoint( p_access );
360 return i_ret;
362 else if( i_ret == 0 )
364 /* check for new files in case the recording is still active */
365 if( p_sys->i_current_file >= FILE_COUNT - 1 )
366 ImportNextFile( p_access );
367 /* play next file */
368 SwitchFile( p_access, p_sys->i_current_file + 1 );
369 return -1;
371 else if( errno == EINTR )
373 /* try again later */
374 return -1;
376 else
378 /* abort on read error */
379 msg_Err( p_access, "failed to read (%s)", vlc_strerror_c(errno) );
380 vlc_dialog_display_error( p_access, _("File reading failed"),
381 _("VLC could not read the file (%s)."),
382 vlc_strerror(errno) );
383 SwitchFile( p_access, -1 );
384 return 0;
388 /*****************************************************************************
389 * Seek to a specific location in a file
390 *****************************************************************************/
391 static int Seek( access_t *p_access, uint64_t i_pos )
393 access_sys_t *p_sys = p_access->p_sys;
395 /* might happen if called by STREAM_SET_SEEKPOINT */
396 i_pos = __MIN( i_pos, p_sys->size );
398 p_sys->offset = i_pos;
400 /* find correct chapter */
401 FindSeekpoint( p_access );
403 /* find correct file */
404 unsigned i_file = 0;
405 while( i_file < FILE_COUNT - 1 &&
406 i_pos >= FILE_SIZE( i_file ) )
408 i_pos -= FILE_SIZE( i_file );
409 i_file++;
411 if( !SwitchFile( p_access, i_file ) )
412 return VLC_EGENERIC;
414 /* adjust position within that file */
415 return lseek( p_sys->fd, i_pos, SEEK_SET ) != -1 ?
416 VLC_SUCCESS : VLC_EGENERIC;
419 /*****************************************************************************
420 * Change the chapter index to match the current position
421 *****************************************************************************/
422 static void FindSeekpoint( access_t *p_access )
424 access_sys_t *p_sys = p_access->p_sys;
425 if( !p_sys->p_marks )
426 return;
428 int new_seekpoint = p_sys->cur_seekpoint;
429 if( p_sys->offset < p_sys->offsets[p_sys->cur_seekpoint] )
431 /* i_pos moved backwards, start fresh */
432 new_seekpoint = 0;
435 /* only need to check the following seekpoints */
436 while( new_seekpoint + 1 < p_sys->p_marks->i_seekpoint &&
437 p_sys->offset >= p_sys->offsets[new_seekpoint + 1] )
439 new_seekpoint++;
442 p_sys->cur_seekpoint = new_seekpoint;
445 /*****************************************************************************
446 * Returns the path of a certain part
447 *****************************************************************************/
448 static char *GetFilePath( access_t *p_access, unsigned i_file )
450 access_sys_t *sys = p_access->p_sys;
451 char *psz_path;
453 if( asprintf( &psz_path, sys->b_ts_format ?
454 "%s" DIR_SEP "%05u.ts" : "%s" DIR_SEP "%03u.vdr",
455 p_access->psz_filepath, i_file + 1 ) == -1 )
456 return NULL;
457 else
458 return psz_path;
461 /*****************************************************************************
462 * Check if another part exists and import it
463 *****************************************************************************/
464 static bool ImportNextFile( access_t *p_access )
466 access_sys_t *p_sys = p_access->p_sys;
468 char *psz_path = GetFilePath( p_access, FILE_COUNT );
469 if( !psz_path )
470 return false;
472 struct stat st;
473 if( vlc_stat( psz_path, &st ) )
475 msg_Dbg( p_access, "could not stat %s: %s", psz_path,
476 vlc_strerror_c(errno) );
477 free( psz_path );
478 return false;
480 if( !S_ISREG( st.st_mode ) )
482 msg_Dbg( p_access, "%s is not a regular file", psz_path );
483 free( psz_path );
484 return false;
486 msg_Dbg( p_access, "%s exists", psz_path );
487 free( psz_path );
489 ARRAY_APPEND( p_sys->file_sizes, st.st_size );
490 p_sys->size += st.st_size;
492 return true;
495 /*****************************************************************************
496 * Close the current file and open another
497 *****************************************************************************/
498 static bool SwitchFile( access_t *p_access, unsigned i_file )
500 access_sys_t *p_sys = p_access->p_sys;
502 /* requested file already open? */
503 if( p_sys->fd != -1 && p_sys->i_current_file == i_file )
504 return true;
506 /* close old file */
507 if( p_sys->fd != -1 )
509 vlc_close( p_sys->fd );
510 p_sys->fd = -1;
513 /* switch */
514 if( i_file >= FILE_COUNT )
515 return false;
516 p_sys->i_current_file = i_file;
518 /* open new file */
519 char *psz_path = GetFilePath( p_access, i_file );
520 if( !psz_path )
521 return false;
522 p_sys->fd = vlc_open( psz_path, O_RDONLY );
524 if( p_sys->fd == -1 )
526 msg_Err( p_access, "Failed to open %s: %s", psz_path,
527 vlc_strerror_c(errno) );
528 goto error;
531 /* cannot handle anything except normal files */
532 struct stat st;
533 if( fstat( p_sys->fd, &st ) || !S_ISREG( st.st_mode ) )
535 msg_Err( p_access, "%s is not a regular file", psz_path );
536 goto error;
539 OptimizeForRead( p_sys->fd );
541 msg_Dbg( p_access, "opened %s", psz_path );
542 free( psz_path );
543 return true;
545 error:
546 vlc_dialog_display_error (p_access, _("File reading failed"), _("VLC could not"
547 " open the file \"%s\" (%s)."), psz_path, vlc_strerror(errno) );
548 if( p_sys->fd != -1 )
550 vlc_close( p_sys->fd );
551 p_sys->fd = -1;
553 free( psz_path );
554 return false;
557 /*****************************************************************************
558 * Some tweaks to speed up read()
559 *****************************************************************************/
560 static void OptimizeForRead( int fd )
562 /* cf. Open() in file access module */
563 VLC_UNUSED(fd);
564 #ifdef HAVE_POSIX_FADVISE
565 posix_fadvise( fd, 0, 4096, POSIX_FADV_WILLNEED );
566 posix_fadvise( fd, 0, 0, POSIX_FADV_NOREUSE );
567 #endif
568 #ifdef F_RDAHEAD
569 fcntl( fd, F_RDAHEAD, 1 );
570 #endif
571 #ifdef F_NOCACHE
572 fcntl( fd, F_NOCACHE, 0 );
573 #endif
576 /*****************************************************************************
577 * Fix size if the (last) part is still growing
578 *****************************************************************************/
579 static void UpdateFileSize( access_t *p_access )
581 access_sys_t *p_sys = p_access->p_sys;
582 struct stat st;
584 if( p_sys->size >= p_sys->offset )
585 return;
587 /* TODO: not sure if this can happen or what to do in this case */
588 if( fstat( p_sys->fd, &st ) )
589 return;
590 if( (uint64_t)st.st_size <= CURRENT_FILE_SIZE )
591 return;
593 p_sys->size -= CURRENT_FILE_SIZE;
594 CURRENT_FILE_SIZE = st.st_size;
595 p_sys->size += CURRENT_FILE_SIZE;
598 /*****************************************************************************
599 * Open file relative to base directory for reading.
600 *****************************************************************************/
601 static FILE *OpenRelativeFile( access_t *p_access, const char *psz_file )
603 access_sys_t *sys = p_access->p_sys;
605 /* build path and add extension */
606 char *psz_path;
607 if( asprintf( &psz_path, "%s" DIR_SEP "%s%s", p_access->psz_filepath,
608 psz_file, sys->b_ts_format ? "" : ".vdr" ) == -1 )
609 return NULL;
611 FILE *file = vlc_fopen( psz_path, "rb" );
612 if( !file )
613 msg_Warn( p_access, "Failed to open %s: %s", psz_path,
614 vlc_strerror_c(errno) );
615 free( psz_path );
617 return file;
620 /*****************************************************************************
621 * Read a line of text. Returns false on error or EOF.
622 *****************************************************************************/
623 static bool ReadLine( char **ppsz_line, size_t *pi_size, FILE *p_file )
625 ssize_t read = getline( ppsz_line, pi_size, p_file );
627 if( read == -1 )
629 /* automatically free buffer on eof */
630 free( *ppsz_line );
631 *ppsz_line = NULL;
632 return false;
635 if( read > 0 && (*ppsz_line)[ read - 1 ] == '\n' )
636 (*ppsz_line)[ read - 1 ] = '\0';
637 EnsureUTF8( *ppsz_line );
639 return true;
642 /*****************************************************************************
643 * Import meta data
644 *****************************************************************************/
645 static void ImportMeta( access_t *p_access )
647 access_sys_t *p_sys = p_access->p_sys;
649 FILE *infofile = OpenRelativeFile( p_access, "info" );
650 if( !infofile )
651 return;
653 vlc_meta_t *p_meta = vlc_meta_New();
654 p_sys->p_meta = p_meta;
655 if( !p_meta )
657 fclose( infofile );
658 return;
661 char *line = NULL;
662 size_t line_len;
663 char *psz_title = NULL, *psz_smalltext = NULL, *psz_date = NULL;
665 while( ReadLine( &line, &line_len, infofile ) )
667 if( !isalpha( (unsigned char)line[0] ) || line[1] != ' ' )
668 continue;
670 char tag = line[0];
671 char *text = line + 2;
673 if( tag == 'C' )
675 char *psz_name = strchr( text, ' ' );
676 if( psz_name )
678 *psz_name = '\0';
679 vlc_meta_AddExtra( p_meta, "Channel", psz_name + 1 );
681 vlc_meta_AddExtra( p_meta, "Transponder", text );
684 else if( tag == 'E' )
686 unsigned i_id, i_start, i_length;
687 if( sscanf( text, "%u %u %u", &i_id, &i_start, &i_length ) == 3 )
689 char str[50];
690 struct tm tm;
691 time_t start = i_start;
692 localtime_r( &start, &tm );
694 /* TODO: locale */
695 strftime( str, sizeof(str), "%Y-%m-%d %H:%M", &tm );
696 vlc_meta_AddExtra( p_meta, "Date", str );
697 free( psz_date );
698 psz_date = strdup( str );
700 /* display in minutes */
701 i_length = ( i_length + 59 ) / 60;
702 snprintf( str, sizeof(str), "%u:%02u", i_length / 60, i_length % 60 );
703 vlc_meta_AddExtra( p_meta, "Duration", str );
707 else if( tag == 'T' )
709 free( psz_title );
710 psz_title = strdup( text );
711 vlc_meta_AddExtra( p_meta, "Title", text );
714 else if( tag == 'S' )
716 free( psz_smalltext );
717 psz_smalltext = strdup( text );
718 vlc_meta_AddExtra( p_meta, "Info", text );
721 else if( tag == 'D' )
723 for( char *p = text; *p; ++p )
725 if( *p == '|' )
726 *p = '\n';
728 vlc_meta_SetDescription( p_meta, text );
731 /* FPS are required to convert between timestamps and frames */
732 else if( tag == 'F' )
734 float fps = atof( text );
735 if( fps >= 1 )
736 p_sys->fps = fps;
737 vlc_meta_AddExtra( p_meta, "Frame Rate", text );
740 else if( tag == 'P' )
742 vlc_meta_AddExtra( p_meta, "Priority", text );
745 else if( tag == 'L' )
747 vlc_meta_AddExtra( p_meta, "Lifetime", text );
751 /* create a meaningful title */
752 int i_len = 10 +
753 ( psz_title ? strlen( psz_title ) : 0 ) +
754 ( psz_smalltext ? strlen( psz_smalltext ) : 0 ) +
755 ( psz_date ? strlen( psz_date ) : 0 );
756 char *psz_display = malloc( i_len );
758 if( psz_display )
760 *psz_display = '\0';
761 if( psz_title )
762 strcat( psz_display, psz_title );
763 if( psz_title && psz_smalltext )
764 strcat( psz_display, " - " );
765 if( psz_smalltext )
766 strcat( psz_display, psz_smalltext );
767 if( ( psz_title || psz_smalltext ) && psz_date )
769 strcat( psz_display, " (" );
770 strcat( psz_display, psz_date );
771 strcat( psz_display, ")" );
773 if( *psz_display )
774 vlc_meta_SetTitle( p_meta, psz_display );
777 free( psz_display );
778 free( psz_title );
779 free( psz_smalltext );
780 free( psz_date );
782 fclose( infofile );
785 /*****************************************************************************
786 * Import cut marks and convert them to seekpoints (chapters).
787 *****************************************************************************/
788 static void ImportMarks( access_t *p_access )
790 access_sys_t *p_sys = p_access->p_sys;
792 FILE *marksfile = OpenRelativeFile( p_access, "marks" );
793 if( !marksfile )
794 return;
796 FILE *indexfile = OpenRelativeFile( p_access, "index" );
797 if( !indexfile )
799 fclose( marksfile );
800 return;
803 /* get the length of this recording (index stores 8 bytes per frame) */
804 struct stat st;
805 if( fstat( fileno( indexfile ), &st ) )
807 fclose( marksfile );
808 fclose( indexfile );
809 return;
811 int64_t i_frame_count = st.st_size / 8;
813 /* Put all cut marks in a "dummy" title */
814 input_title_t *p_marks = vlc_input_title_New();
815 if( !p_marks )
817 fclose( marksfile );
818 fclose( indexfile );
819 return;
821 p_marks->psz_name = strdup( _("VDR Cut Marks") );
822 p_marks->i_length = i_frame_count * (int64_t)( CLOCK_FREQ / p_sys->fps );
824 uint64_t *offsetv = NULL;
825 size_t offsetc = 0;
827 /* offset for chapter positions */
828 int i_chapter_offset = p_sys->fps / 1000 *
829 var_InheritInteger( p_access, "vdr-chapter-offset" );
831 /* minimum chapter size in frames */
832 int i_min_chapter_size = p_sys->fps * MIN_CHAPTER_SIZE;
834 /* the last chapter started at this frame (init to 0 so
835 * we skip useless chapters near the beginning as well) */
836 int64_t i_prev_chapter = 0;
838 /* parse lines of the form "0:00:00.00 foobar" */
839 char *line = NULL;
840 size_t line_len;
841 while( ReadLine( &line, &line_len, marksfile ) )
843 int64_t i_frame = ParseFrameNumber( line, p_sys->fps );
845 /* skip chapters which are near the end or too close to each other */
846 if( i_frame - i_prev_chapter < i_min_chapter_size ||
847 i_frame >= i_frame_count - i_min_chapter_size )
848 continue;
849 i_prev_chapter = i_frame;
851 /* move chapters (simple workaround for inaccurate cut marks) */
852 if( i_frame > -i_chapter_offset )
853 i_frame += i_chapter_offset;
854 else
855 i_frame = 0;
857 uint64_t i_offset;
858 uint16_t i_file_number;
859 if( !ReadIndexRecord( indexfile, p_sys->b_ts_format,
860 i_frame, &i_offset, &i_file_number ) )
861 continue;
862 if( i_file_number < 1 || i_file_number > FILE_COUNT )
863 continue;
865 /* add file sizes to get the "global" offset */
866 seekpoint_t *sp = vlc_seekpoint_New();
867 if( !sp )
868 continue;
869 sp->i_time_offset = i_frame * (int64_t)( CLOCK_FREQ / p_sys->fps );
870 sp->psz_name = strdup( line );
872 TAB_APPEND( p_marks->i_seekpoint, p_marks->seekpoint, sp );
873 TAB_APPEND( offsetc, offsetv, i_offset );
875 for( int i = 0; i + 1 < i_file_number; ++i )
876 offsetv[offsetc - 1] += FILE_SIZE( i );
879 /* add a chapter at the beginning if missing */
880 if( p_marks->i_seekpoint > 0 && offsetv[0] > 0 )
882 seekpoint_t *sp = vlc_seekpoint_New();
883 if( sp )
885 sp->i_time_offset = 0;
886 sp->psz_name = strdup( _("Start") );
887 TAB_INSERT( p_marks->i_seekpoint, p_marks->seekpoint, sp, 0 );
888 TAB_INSERT( offsetc, offsetv, UINT64_C(0), 0 );
892 if( p_marks->i_seekpoint > 0 )
894 p_sys->p_marks = p_marks;
895 p_sys->offsets = offsetv;
897 else
899 vlc_input_title_Delete( p_marks );
900 TAB_CLEAN( offsetc, offsetv );
903 fclose( marksfile );
904 fclose( indexfile );
907 /*****************************************************************************
908 * Lookup frame offset in index file
909 *****************************************************************************/
910 static bool ReadIndexRecord( FILE *p_file, bool b_ts, int64_t i_frame,
911 uint64_t *pi_offset, uint16_t *pi_file_num )
913 uint8_t index_record[8];
914 if( fseek( p_file, sizeof(index_record) * i_frame, SEEK_SET ) != 0 )
915 return false;
916 if( fread( &index_record, sizeof(index_record), 1, p_file ) < 1 )
917 return false;
919 /* VDR usually (only?) runs on little endian machines, but VLC has a
920 * broader audience. See recording.* in VDR source for data layout. */
921 if( b_ts )
923 uint64_t i_index_entry = GetQWLE( &index_record );
924 *pi_offset = i_index_entry & UINT64_C(0xFFFFFFFFFF);
925 *pi_file_num = i_index_entry >> 48;
927 else
929 *pi_offset = GetDWLE( &index_record );
930 *pi_file_num = index_record[5];
933 return true;
936 /*****************************************************************************
937 * Convert time stamp from file to frame number
938 *****************************************************************************/
939 static int64_t ParseFrameNumber( const char *psz_line, float fps )
941 unsigned h, m, s, f, n;
943 /* hour:min:sec.frame (frame is optional) */
944 n = sscanf( psz_line, "%u:%u:%u.%u", &h, &m, &s, &f );
945 if( n >= 3 )
947 if( n < 4 )
948 f = 1;
949 int64_t i_seconds = (int64_t)h * 3600 + (int64_t)m * 60 + s;
950 return (int64_t)( i_seconds * (double)fps ) + __MAX(1, f) - 1;
953 /* only a frame number */
954 int64_t i_frame = strtoll( psz_line, NULL, 10 );
955 return __MAX(1, i_frame) - 1;
958 /*****************************************************************************
959 * Return the last path component (including trailing separators)
960 *****************************************************************************/
961 static const char *BaseName( const char *psz_path )
963 const char *psz_name = psz_path + strlen( psz_path );
965 /* skip superfluous separators at the end */
966 while( psz_name > psz_path && psz_name[-1] == DIR_SEP_CHAR )
967 --psz_name;
969 /* skip last component */
970 while( psz_name > psz_path && psz_name[-1] != DIR_SEP_CHAR )
971 --psz_name;
973 return psz_name;