1 /****************************************************************************
2 * input_vcd.c: VideoCD raw reading plugin.
3 *****************************************************************************
4 * Copyright (C) 1998-2001 VideoLAN
6 * Author: Johan Bilien <jobi@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
29 #include <videolan/vlc.h>
36 #include <sys/types.h>
40 #ifdef STRNCASECMP_IN_STRINGS_H
45 # include <io.h> /* read() */
47 # include <sys/uio.h> /* struct iovec */
51 # include "input_iovec.h"
54 #include "stream_control.h"
55 #include "input_ext-intf.h"
56 #include "input_ext-dec.h"
57 #include "input_ext-plugins.h"
61 #include "input_vcd.h"
62 #include "linux_cdrom_tools.h"
64 /* how many blocks VCDRead will read in each loop */
65 #define VCD_BLOCKS_ONCE 4
66 #define VCD_DATA_ONCE (2 * VCD_BLOCKS_ONCE)
67 #define BUFFER_SIZE VCD_DATA_SIZE
69 /*****************************************************************************
71 *****************************************************************************/
72 /* called from outside */
73 static int VCDProbe ( probedata_t *p_data );
74 static void VCDInit ( struct input_thread_s * );
75 static int VCDRead ( struct input_thread_s *, data_packet_t ** );
76 static int VCDSetArea ( struct input_thread_s *, struct input_area_s * );
77 static int VCDSetProgram ( struct input_thread_s *, pgrm_descriptor_t * );
78 static void VCDOpen ( struct input_thread_s *);
79 static void VCDClose ( struct input_thread_s *);
80 static void VCDEnd ( struct input_thread_s *);
81 static void VCDSeek ( struct input_thread_s *, off_t );
82 static int VCDRewind ( struct input_thread_s * );
84 /*****************************************************************************
85 * Declare a buffer manager
86 *****************************************************************************/
87 #define FLAGS BUFFERS_NOFLAGS
89 DECLARE_BUFFERS_EMBEDDED( FLAGS, NB_LIFO );
90 DECLARE_BUFFERS_INIT( FLAGS, NB_LIFO );
91 DECLARE_BUFFERS_END( FLAGS, NB_LIFO );
92 DECLARE_BUFFERS_NEWPACKET( FLAGS, NB_LIFO );
93 DECLARE_BUFFERS_DELETEPACKET( FLAGS, NB_LIFO, 1000 );
94 DECLARE_BUFFERS_NEWPES( FLAGS, NB_LIFO );
95 DECLARE_BUFFERS_DELETEPES( FLAGS, NB_LIFO, 1000 );
98 /*****************************************************************************
99 * Functions exported as capabilities. They are declared as static so that
100 * we don't pollute the namespace too much.
101 *****************************************************************************/
102 void _M( input_getfunctions )( function_list_t * p_function_list )
104 #define input p_function_list->functions.input
105 p_function_list->pf_probe = VCDProbe;
106 input.pf_init = VCDInit;
107 input.pf_open = VCDOpen;
108 input.pf_close = VCDClose;
109 input.pf_end = VCDEnd;
110 input.pf_init_bit_stream = InitBitstream;
111 input.pf_read = VCDRead;
112 input.pf_set_area = VCDSetArea;
113 input.pf_set_program = VCDSetProgram;
114 input.pf_demux = input_DemuxPS;
115 input.pf_new_packet = input_NewPacket;
116 input.pf_new_pes = input_NewPES;
117 input.pf_delete_packet = input_DeletePacket;
118 input.pf_delete_pes = input_DeletePES;
119 input.pf_rewind = VCDRewind;
120 input.pf_seek = VCDSeek;
125 * Data reading functions
128 /*****************************************************************************
129 * VCDProbe: verifies that the stream is a PS stream
130 *****************************************************************************/
131 static int VCDProbe( probedata_t *p_data )
133 input_thread_t * p_input = (input_thread_t *)p_data;
135 char * psz_name = p_input->p_source;
138 if( ( strlen(psz_name) > 4 ) && !strncasecmp( psz_name, "vcd:", 4 ) )
140 /* If the user specified "vcd:" then it's probably a VCD */
148 /*****************************************************************************
150 *****************************************************************************/
151 static void VCDOpen( struct input_thread_s *p_input )
153 vlc_mutex_lock( &p_input->stream.stream_lock );
155 /* If we are here we can control the pace... */
156 p_input->stream.b_pace_control = 1;
158 p_input->stream.b_seekable = 1;
159 p_input->stream.p_selected_area->i_size = 0;
160 p_input->stream.p_selected_area->i_tell = 0;
162 vlc_mutex_unlock( &p_input->stream.stream_lock );
164 /* XXX: put this shit in an access plugin */
165 if( strlen( p_input->p_source ) > 4
166 && !strncasecmp( p_input->p_source, "vcd:", 4 ) )
169 = open( p_input->p_source + 4, O_RDONLY | O_NONBLOCK );
174 = open( p_input->p_source, O_RDONLY | O_NONBLOCK );
177 if( p_input->i_handle == -1 )
179 p_input->b_error = 1;
183 /*****************************************************************************
184 * VCDClose: close vcd
185 *****************************************************************************/
186 static void VCDClose( struct input_thread_s *p_input )
188 close( p_input->i_handle );
191 /*****************************************************************************
192 * VCDInit: initializes VCD structures
193 *****************************************************************************/
194 static void VCDInit( input_thread_t * p_input )
196 thread_vcd_data_t * p_vcd;
200 input_area_t * p_area;
201 es_descriptor_t * p_es;
203 p_vcd = malloc( sizeof(thread_vcd_data_t) );
207 intf_ErrMsg( "vcd error: out of memory" );
208 p_input->b_error = 1;
212 p_input->p_plugin_data = (void *)p_vcd;
214 if( (p_input->p_method_data = input_BuffersInit()) == NULL )
217 p_input->b_error = 1;
221 p_vcd->i_handle = p_input->i_handle;
223 /* We read the Table Of Content information */
224 p_vcd->nb_tracks = ioctl_GetTrackCount( p_input->i_handle );
225 if( p_vcd->nb_tracks < 0 )
227 input_BuffersEnd( p_input->p_method_data );
229 p_input->b_error = 1;
233 p_vcd->p_sectors = ioctl_GetSectors( p_input->i_handle );
234 if ( p_vcd->p_sectors == NULL )
236 input_BuffersEnd( p_input->p_method_data );
238 p_input->b_error = 1;
242 /* Set stream and area data */
243 vlc_mutex_lock( &p_input->stream.stream_lock );
245 /* Initialize ES structures */
246 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
248 /* disc input method */
249 p_input->stream.i_method = INPUT_METHOD_VCD;
251 #define area p_input->stream.pp_areas
252 for( i = 1 ; i <= p_vcd->nb_tracks - 1 ; i++ )
254 input_AddArea( p_input );
256 /* Titles are Program Chains */
259 /* Absolute start offset and size */
260 area[i]->i_start = (off_t)p_vcd->p_sectors[i] * (off_t)VCD_DATA_SIZE;
261 area[i]->i_size = (off_t)(p_vcd->p_sectors[i+1] - p_vcd->p_sectors[i])
262 * (off_t)VCD_DATA_SIZE;
264 /* Number of chapters */
265 area[i]->i_part_nb = 0; // will be the entry points
268 /* Number of angles */
269 area[i]->i_angle_nb = 1; // no angle support in VCDs
270 area[i]->i_angle = 1;
272 area[i]->i_plugin_data = p_vcd->p_sectors[i];
276 /* Get requested title - if none try the first title */
277 i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
283 // p_vcd->i_track = i_title-1;
285 /* Get requested chapter - if none defaults to first one */
286 i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
292 p_input->stream.pp_areas[i_title]->i_part = i_chapter;
294 p_area = p_input->stream.pp_areas[i_title];
296 VCDSetArea( p_input, p_area );
298 /* Set program information. */
300 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
301 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
303 /* No PSM to read in disc mode, we already have all the information */
304 p_input->stream.p_selected_program->b_is_ok = 1;
306 p_es = input_AddES( p_input, p_input->stream.p_selected_program, 0xe0, 0 );
307 p_es->i_stream_id = 0xe0;
308 p_es->i_type = MPEG1_VIDEO_ES;
309 p_es->i_cat = VIDEO_ES;
311 if( p_main->b_video )
313 input_SelectES( p_input, p_es );
316 p_es = input_AddES( p_input, p_input->stream.p_selected_program, 0xc0, 0 );
317 p_es->i_stream_id = 0xc0;
318 p_es->i_type = MPEG1_AUDIO_ES;
320 p_es->i_cat = AUDIO_ES;
322 if( p_main->b_audio )
324 input_SelectES( p_input, p_es );
327 vlc_mutex_unlock( &p_input->stream.stream_lock );
330 /*****************************************************************************
331 * VCDEnd: frees unused data
332 *****************************************************************************/
333 static void VCDEnd( input_thread_t * p_input )
335 thread_vcd_data_t * p_vcd;
337 input_BuffersEnd( p_input->p_method_data );
339 p_vcd = (thread_vcd_data_t*)p_input->p_plugin_data;
344 /*****************************************************************************
345 * VCDSetProgram: Does nothing since a VCD is mono_program
346 *****************************************************************************/
347 static int VCDSetProgram( input_thread_t * p_input,
348 pgrm_descriptor_t * p_program)
354 /*****************************************************************************
355 * VCDSetArea: initialize input data for title x, chapter y.
356 * It should be called for each user navigation request.
357 ****************************************************************************/
358 static int VCDSetArea( input_thread_t * p_input, input_area_t * p_area )
360 thread_vcd_data_t * p_vcd;
362 p_vcd = (thread_vcd_data_t*)p_input->p_plugin_data;
364 /* we can't use the interface slider until initilization is complete */
365 p_input->stream.b_seekable = 0;
367 if( p_area != p_input->stream.p_selected_area )
369 /* Reset the Chapter position of the current title */
370 p_input->stream.p_selected_area->i_part = 1;
371 p_input->stream.p_selected_area->i_tell = 0;
373 /* Change the default area */
374 p_input->stream.p_selected_area = p_area;
376 /* Change the current track */
377 /* The first track is not a valid one */
378 p_vcd->i_track = p_area->i_id;
379 p_vcd->i_sector = p_vcd->p_sectors[p_vcd->i_track];
382 /* warn interface that something has changed */
383 p_input->stream.b_seekable = 1;
384 p_input->stream.b_changed = 1;
389 /*****************************************************************************
390 * VCDRead: reads from the VCD into PES packets.
391 *****************************************************************************
392 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
394 *****************************************************************************/
395 static int VCDRead( input_thread_t * p_input, data_packet_t ** pp_data )
397 thread_vcd_data_t * p_vcd;
398 data_packet_t * p_data;
403 byte_t p_buffer[ VCD_DATA_SIZE ];
404 boolean_t b_eot = 0; /* end of track */
405 /* boolean_t b_eoc; No chapters yet */
407 p_vcd = (thread_vcd_data_t *)p_input->p_plugin_data;
412 while( i_packet < VCD_DATA_ONCE )
414 if( ioctl_ReadSector( p_vcd->i_handle, p_vcd->i_sector, p_buffer ) )
416 /* Read error, but assume we reached the end of the track */
423 if( p_vcd->i_sector >= p_vcd->p_sectors[p_vcd->i_track + 1] )
431 while( i_index < BUFFER_SIZE-6 && i_packet < VCD_DATA_ONCE )
433 i_header = U32_AT(p_buffer + i_index);
435 /* It is common for MPEG-1 streams to pad with zeros
436 * (although it is forbidden by the recommendation), so
437 * don't bother everybody in this case. */
438 while( !i_header && (++i_index < BUFFER_SIZE - 4) )
440 i_header = U32_AT(p_buffer + i_index);
445 intf_WarnMsg( 12, "vcd warning: zero-padded packet" );
449 /* Read the stream until we find a startcode. */
450 while( (i_header & 0xFFFFFF00) != 0x100L
451 && (++i_index < BUFFER_SIZE - 4) )
453 i_header = U32_AT(p_buffer + i_index);
456 if( (i_header & 0xFFFFFF00) != 0x100L )
458 intf_WarnMsg( 3, "vcd warning: no packet at sector %d",
459 p_vcd->i_sector - 1 );
460 break; /* go to the next sector */
463 intf_DbgMsg( "packet start code : %X", i_header );
467 /* 0x1b9 == SYSTEM_END_CODE, it is only 4 bytes long. */
474 if( ( *( p_buffer + ( i_index + 4 ) ) & 0xC0) == 0x40 )
479 else if( (*(p_buffer + ( i_index + 4 ) ) & 0xF0) == 0x20 )
486 intf_ErrMsg( "vcd error: unable to determine "
492 /* The packet is at least 6 bytes long. */
494 /* That's the case for all packets, except pack header. */
495 i_packet_size = U16_AT((p_buffer + ( i_index + 4 )));
499 intf_DbgMsg( "i_index : %d", i_index );
500 intf_DbgMsg( "i_packet_size : %d", i_packet_size );
502 if ( i_index + i_packet_size > BUFFER_SIZE )
504 intf_ErrMsg( "vcd error: packet too long (%i)",
505 i_index + i_packet_size );
509 /* Fetch a packet of the appropriate size. */
510 p_data = p_input->pf_new_packet( p_input->p_method_data,
515 intf_ErrMsg( "vcd error: out of memory" );
519 if( U32_AT(p_buffer) != 0x1B9 )
521 FAST_MEMCPY( p_data->p_demux_start, p_buffer + i_index,
523 i_index += ( 6 + i_packet_size );
527 /* Copy the small header. */
528 memcpy( p_data->p_demux_start, p_buffer + i_index, 4 );
532 /* Give the packet to the other input stages. */
534 pp_data = &p_data->p_next;
540 vlc_mutex_lock( &p_input->stream.stream_lock );
542 p_input->stream.p_selected_area->i_tell =
543 (off_t)p_vcd->i_sector * (off_t)VCD_DATA_SIZE
544 - p_input->stream.p_selected_area->i_start;
546 /* no chapter for the moment*/
550 /* We modify i_part only at end of chapter not to erase
551 * some modification from the interface */
552 p_input->stream.p_selected_area->i_part = p_vcd->i_chapter;
558 input_area_t *p_area;
561 if( p_vcd->i_track >= p_vcd->nb_tracks - 1 )
563 vlc_mutex_unlock( &p_input->stream.stream_lock );
567 intf_WarnMsg( 4, "vcd info: new title" );
569 p_area = p_input->stream.pp_areas[
570 p_input->stream.p_selected_area->i_id + 1 ];
573 VCDSetArea( p_input, p_area );
576 vlc_mutex_unlock( &p_input->stream.stream_lock );
578 return( i_packet + 1 );
581 /*****************************************************************************
582 * VCDRewind : reads a stream backward
583 *****************************************************************************/
584 static int VCDRewind( input_thread_t * p_input )
589 /****************************************************************************
591 ****************************************************************************/
592 static void VCDSeek( input_thread_t * p_input, off_t i_off )
594 thread_vcd_data_t * p_vcd;
596 p_vcd = (thread_vcd_data_t *) p_input->p_plugin_data;
598 p_vcd->i_sector = p_vcd->p_sectors[p_vcd->i_track]
599 + i_off / (off_t)VCD_DATA_SIZE;
601 p_input->stream.p_selected_area->i_tell =
602 (off_t)p_vcd->i_sector * (off_t)VCD_DATA_SIZE
603 - p_input->stream.p_selected_area->i_start;