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 #define MODULE_NAME vcd
24 #include "modules_inner.h"
26 /*****************************************************************************
28 *****************************************************************************/
39 #include <sys/types.h>
43 #ifdef STRNCASECMP_IN_STRINGS_H
48 # include <io.h> /* read() */
50 # include <sys/uio.h> /* struct iovec */
60 # include "input_iovec.h"
63 #include "stream_control.h"
64 #include "input_ext-intf.h"
65 #include "input_ext-dec.h"
66 #include "input_ext-plugins.h"
71 #include "modules_export.h"
73 #include "../mpeg_system/input_ps.h"
74 #include "input_vcd.h"
75 #include "linux_cdrom_tools.h"
77 /* how many blocks VCDRead will read in each loop */
78 #define VCD_BLOCKS_ONCE 64
79 #define VCD_DATA_ONCE (2 * VCD_BLOCKS_ONCE)
80 #define BUFFER_SIZE VCD_DATA_SIZE
84 /*****************************************************************************
86 *****************************************************************************/
87 /* called from outside */
88 static int VCDProbe ( probedata_t *p_data );
89 static void VCDInit ( struct input_thread_s * );
90 static int VCDRead ( struct input_thread_s *, data_packet_t ** );
91 static int VCDSetArea ( struct input_thread_s *, struct input_area_s * );
92 static int VCDSetProgram ( struct input_thread_s *, pgrm_descriptor_t * );
93 static void VCDOpen ( struct input_thread_s *);
94 static void VCDClose ( struct input_thread_s *);
95 static void VCDEnd ( struct input_thread_s *);
96 static void VCDSeek ( struct input_thread_s *, off_t );
97 static int VCDRewind ( struct input_thread_s * );
99 /*****************************************************************************
100 * Declare a buffer manager
101 *****************************************************************************/
102 #define FLAGS BUFFERS_NOFLAGS
104 DECLARE_BUFFERS_EMBEDDED( FLAGS, NB_LIFO );
105 DECLARE_BUFFERS_INIT( FLAGS, NB_LIFO );
106 DECLARE_BUFFERS_END( FLAGS, NB_LIFO );
107 DECLARE_BUFFERS_NEWPACKET( FLAGS, NB_LIFO );
108 DECLARE_BUFFERS_DELETEPACKET( FLAGS, NB_LIFO, 150 );
109 DECLARE_BUFFERS_NEWPES( FLAGS, NB_LIFO );
110 DECLARE_BUFFERS_DELETEPES( FLAGS, NB_LIFO, 150 );
113 /*****************************************************************************
114 * Functions exported as capabilities. They are declared as static so that
115 * we don't pollute the namespace too much.
116 *****************************************************************************/
117 void _M( input_getfunctions )( function_list_t * p_function_list )
119 #define input p_function_list->functions.input
120 p_function_list->pf_probe = VCDProbe;
121 input.pf_init = VCDInit;
122 input.pf_open = VCDOpen;
123 input.pf_close = VCDClose;
124 input.pf_end = VCDEnd;
125 input.pf_init_bit_stream = InitBitstream;
126 input.pf_read = VCDRead;
127 input.pf_set_area = VCDSetArea;
128 input.pf_set_program = VCDSetProgram;
129 input.pf_demux = input_DemuxPS;
130 input.pf_new_packet = input_NewPacket;
131 input.pf_new_pes = input_NewPES;
132 input.pf_delete_packet = input_DeletePacket;
133 input.pf_delete_pes = input_DeletePES;
134 input.pf_rewind = VCDRewind;
135 input.pf_seek = VCDSeek;
140 * Data reading functions
143 /*****************************************************************************
144 * VCDProbe: verifies that the stream is a PS stream
145 *****************************************************************************/
146 static int VCDProbe( probedata_t *p_data )
149 input_thread_t * p_input = (input_thread_t *)p_data;
151 char * psz_name = p_input->p_source;
154 if( TestMethod( INPUT_METHOD_VAR, "vcd" ) )
159 if( ( strlen(psz_name) > 4 ) && !strncasecmp( psz_name, "vcd:", 4 ) )
161 /* If the user specified "vcd:" then it's probably a VCD */
168 /*****************************************************************************
170 *****************************************************************************/
171 static void VCDOpen( struct input_thread_s *p_input )
175 vlc_mutex_lock( &p_input->stream.stream_lock );
177 /* If we are here we can control the pace... */
178 p_input->stream.b_pace_control = 1;
180 p_input->stream.b_seekable = 1;
181 p_input->stream.p_selected_area->i_size = 0;
183 p_input->stream.p_selected_area->i_tell = 0;
185 vlc_mutex_unlock( &p_input->stream.stream_lock );
188 /* XXX: put this shit in an access plugin */
189 if( strlen( p_input->p_source ) > 4
190 && !strncasecmp( p_input->p_source, "vcd:", 4 ) )
192 vcdhandle = open( p_input->p_source + 4, O_RDONLY | O_NONBLOCK );
196 vcdhandle = open( p_input->p_source + 4, O_RDONLY | O_NONBLOCK );
199 if( vcdhandle == -1 )
201 p_input->b_error = 1;
205 p_input->i_handle = (int) vcdhandle;
208 /*****************************************************************************
209 * VCDClose: close vcd
210 *****************************************************************************/
211 static void VCDClose( struct input_thread_s *p_input )
213 close( p_input->i_handle );
216 /*****************************************************************************
217 * VCDInit: initializes VCD structures
218 *****************************************************************************/
219 static void VCDInit( input_thread_t * p_input )
221 thread_vcd_data_t * p_vcd;
225 input_area_t * p_area;
226 es_descriptor_t * p_es;
228 p_vcd = malloc( sizeof(thread_vcd_data_t) );
232 intf_ErrMsg( "vcd error: out of memory" );
233 p_input->b_error = 1;
237 p_input->p_plugin_data = (void *)p_vcd;
239 if( (p_input->p_method_data = input_BuffersInit()) == NULL )
241 p_input->b_error = 1;
245 p_vcd->vcdhandle = p_input->i_handle;
246 p_vcd->b_end_of_track = 0;
248 /* we read the Table Of Content information */
249 if ( read_toc(p_vcd) == -1 )
251 intf_ErrMsg("An error occured when reading vcd's TOC");
254 p_input->i_read_once = VCD_DATA_ONCE;
256 /* Set stream and area data */
257 vlc_mutex_lock( &p_input->stream.stream_lock );
259 /* Initialize ES structures */
260 input_InitStream( p_input, sizeof( stream_ps_data_t ) );
262 /* disc input method */
263 p_input->stream.i_method = INPUT_METHOD_VCD;
266 #define area p_input->stream.pp_areas
267 for( i = 1 ; i <= p_vcd->nb_tracks - 1 ; i++ )
269 input_AddArea( p_input );
271 /* Titles are Program Chains */
274 /* Absolute start offset and size */
275 area[i]->i_start = p_vcd->tracks_sector[i];
276 area[i]->i_size = p_vcd->tracks_sector[i+1] - p_vcd->tracks_sector[i];
278 /* Number of chapters */
279 area[i]->i_part_nb = 0; // will be the entry points
282 /* Number of angles */
283 area[i]->i_angle_nb = 1; // no angle support in VCDs
284 area[i]->i_angle = 1;
286 area[i]->i_plugin_data = p_vcd->tracks_sector[i];
290 /* Get requested title - if none try the first title */
291 i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
297 // p_vcd->current_track = i_title-1 ;
299 /* Get requested chapter - if none defaults to first one */
300 i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
307 p_input->stream.pp_areas[i_title]->i_part = i_chapter;
309 p_area = p_input->stream.pp_areas[i_title];
311 VCDSetArea( p_input, p_area );
313 /* Set program information. */
315 input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
316 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
318 /* No PSM to read in disc mode, we already have all information */
319 p_input->stream.p_selected_program->b_is_ok = 1;
321 p_es = input_AddES( p_input, p_input->stream.p_selected_program, 0xe0, 0 );
322 p_es->i_stream_id = 0xe0;
323 p_es->i_type = MPEG1_VIDEO_ES;
324 p_es->i_cat = VIDEO_ES;
326 if( p_main->b_video )
328 input_SelectES( p_input, p_es );
331 p_es = input_AddES( p_input,
332 p_input->stream.p_selected_program, 0xc0, 0 );
333 p_es->i_stream_id = 0xc0;
334 p_es->i_type = MPEG1_AUDIO_ES;
336 p_es->i_cat = AUDIO_ES;
338 if( p_main->b_audio )
340 input_SelectES( p_input, p_es );
343 vlc_mutex_unlock( &p_input->stream.stream_lock );
350 /*****************************************************************************
351 * VCDEnd: frees unused data
352 *****************************************************************************/
353 static void VCDEnd( input_thread_t * p_input )
355 thread_vcd_data_t * p_vcd;
357 p_vcd = (thread_vcd_data_t*)p_input->p_plugin_data;
361 input_BuffersEnd( p_input->p_method_data );
364 /*****************************************************************************
365 * VCDSetProgram: Does nothing since a VCD is mono_program
366 *****************************************************************************/
367 static int VCDSetProgram( input_thread_t * p_input,
368 pgrm_descriptor_t * p_program)
374 /*****************************************************************************
375 * VCDSetArea: initialize input data for title x, chapter y.
376 * It should be called for each user navigation request.
377 ****************************************************************************/
378 static int VCDSetArea( input_thread_t * p_input, input_area_t * p_area )
380 thread_vcd_data_t * p_vcd;
382 p_vcd = (thread_vcd_data_t*)p_input->p_plugin_data;
384 /* we can't use the interface slider until initilization is complete */
385 p_input->stream.b_seekable = 0;
387 if ( p_area != p_input->stream.p_selected_area )
389 /* Reset the Chapter position of the old title */
390 p_input->stream.p_selected_area->i_part = 0;
391 p_input->stream.p_selected_area->i_tell = 0;
393 /* Change the default area */
394 p_input->stream.p_selected_area = p_area;
396 /* Change the current track */
397 /* The first track is not a valid one */
398 p_vcd->current_track = p_area->i_id ;
399 p_vcd->current_sector = p_vcd->tracks_sector[p_vcd->current_track] ;
401 /* warn interface that something has changed */
402 p_input->stream.b_seekable = 1;
403 p_input->stream.b_changed = 1;
410 /*****************************************************************************
411 * VCDRead: reads from the VCD into PES packets.
412 *****************************************************************************
413 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
415 *****************************************************************************/
416 static int VCDRead( input_thread_t * p_input,
417 data_packet_t ** pp_packets )
419 thread_vcd_data_t * p_vcd;
420 data_packet_t * p_data;
425 boolean_t b_no_packet;
426 /* boolean_t b_eoc; No chapters yet */
428 p_vcd = (thread_vcd_data_t *)p_input->p_plugin_data;
431 p_buffer = malloc ( VCD_DATA_SIZE );
433 if ( p_buffer == NULL )
435 intf_ErrMsg("Could not malloc the read buffer");
443 while( i_packet < VCD_DATA_ONCE )
447 if ( VCD_sector_read( p_vcd, p_buffer ) == -1 )
452 while (i_index < BUFFER_SIZE - 6)
455 if( (U32_AT(p_buffer + i_index) & 0xFFFFFF00) != 0x100L )
457 /* This is not the startcode of a packet. Read the stream
458 * until we find one. */
460 if( !U32_AT( p_buffer + i_index ) )
462 /* It is common for MPEG-1 streams to pad with zeros
463 * (although it is forbidden by the recommendation), so
464 * don't bother everybody in this case. */
465 intf_WarnMsg( 3, "Garbage at input" );
468 while( ( (U32_AT(p_buffer + i_index) & 0xFFFFFF00) != 0x100L )
469 && ( i_index < BUFFER_SIZE - 4 ) )
474 if ( i_index == BUFFER_SIZE - 4 )
484 intf_WarnMsg(3, "No packet found on sector %d\n",
485 p_vcd->current_sector -1 );
486 break; /* go to the next sector */
490 intf_DbgMsg("packet start code : %X\n",
491 U32_AT(p_buffer + i_index));
493 /* 0x1B9 == SYSTEM_END_CODE, it is only 4 bytes long. */
494 if( U32_AT(p_buffer + i_index) != 0x1B9 )
496 /* The packet is at least 6 bytes long. */
498 if( U32_AT(p_buffer + i_index) != 0x1BA )
500 /* That's the case for all packets, except pack header. */
501 i_packet_size = U16_AT((p_buffer + ( i_index + 4 )));
506 if( ( *( p_buffer + ( i_index + 4 ) ) & 0xC0) == 0x40 )
511 else if( (*(p_buffer + ( i_index + 4 ) ) & 0xF0) == 0x20 )
518 intf_ErrMsg( "Unable to determine stream type" );
525 /* System End Code */
529 intf_DbgMsg("i_index : %d\n", i_index);
530 intf_DbgMsg("i_packet_size : %d\n", i_packet_size);
532 if ( i_index + i_packet_size > BUFFER_SIZE )
534 intf_ErrMsg( "input error: packet too long (%i)",
535 i_index + i_packet_size );
539 /* Fetch a packet of the appropriate size. */
541 p_data = p_input->pf_new_packet( p_input->p_method_data,
546 intf_ErrMsg( "Out of memory" );
550 if( U32_AT(p_buffer) != 0x1B9 )
552 p_main->fast_memcpy( p_data->p_demux_start, p_buffer + i_index,
554 i_index += ( 6 + i_packet_size );
559 /* Copy the small header. */
560 memcpy( p_data->p_demux_start, p_buffer + i_index, 4 );
564 /* Give the packet to the other input stages. */
565 pp_packets[i_packet] = p_data;
569 if ( p_vcd->b_end_of_track )
575 vlc_mutex_lock( &p_input->stream.stream_lock );
577 p_input->stream.p_selected_area->i_tell =
578 p_vcd->current_sector -
579 p_input->stream.p_selected_area->i_start ;
581 /* no chapter for the moment*/
584 * We modify i_part only at end of chapter not to erase
585 * some modification from the interface *
586 p_input->stream.p_selected_area->i_part = p_vcd->i_chapter;
589 if( p_vcd->b_end_of_track )
591 input_area_t *p_area;
594 if( p_vcd->current_track >= p_vcd->nb_tracks - 1 )
596 vlc_mutex_unlock( &p_input->stream.stream_lock );
600 intf_WarnMsg( 4, "vcd info: new title" );
602 p_vcd->b_end_of_track = 0;
604 p_area = p_input->stream.pp_areas[
605 p_input->stream.p_selected_area->i_id + 1 ];
608 VCDSetArea( p_input, p_area );
611 vlc_mutex_unlock( &p_input->stream.stream_lock );
616 /*****************************************************************************
617 * VCDRewind : reads a stream backward
618 *****************************************************************************/
619 static int VCDRewind( input_thread_t * p_input )
625 /****************************************************************************
627 ****************************************************************************/
628 static void VCDSeek( input_thread_t * p_input, off_t i_off )
630 thread_vcd_data_t * p_vcd;
632 p_vcd = (thread_vcd_data_t *) p_input->p_plugin_data;
634 p_vcd->current_sector = p_vcd->tracks_sector[p_vcd->current_track]
637 p_input->stream.p_selected_area->i_tell = p_vcd->current_sector
638 - p_input->stream.p_selected_area->i_start;