]> git.sesse.net Git - vlc/blob - src/input/input_programs.c
96e4d83c25d29fa341026f2bbcbbdd0b36571f36
[vlc] / src / input / input_programs.c
1 /*****************************************************************************
2  * input_programs.c: es_descriptor_t, pgrm_descriptor_t management
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: input_programs.c,v 1.69 2001/12/07 18:33:08 sam Exp $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include "defs.h"
28
29 #include <stdlib.h>
30 #include <string.h>                                    /* memcpy(), memset() */
31 #include <sys/types.h>                                              /* off_t */
32
33 #include "common.h"
34 #include "intf_msg.h"
35 #include "threads.h"
36 #include "mtime.h"
37 #include "debug.h"
38
39 #include "stream_control.h"
40 #include "input_ext-intf.h"
41 #include "input_ext-dec.h"
42 #include "input_ext-plugins.h"
43
44 /*
45  * NOTICE : all of these functions expect you to have taken the lock on
46  * p_input->stream.lock
47  */
48
49 /*****************************************************************************
50  * input_InitStream: init the stream descriptor of the given input
51  *****************************************************************************/
52 int input_InitStream( input_thread_t * p_input, size_t i_data_len )
53 {
54
55     p_input->stream.i_stream_id = 0;
56
57     /* initialized to 0 since we don't give the signal to the interface
58      * before the end of input initialization */
59     p_input->stream.b_changed = 0;
60     p_input->stream.pp_es = NULL;
61     p_input->stream.pp_selected_es = NULL;
62     p_input->stream.p_removed_es = NULL;
63     p_input->stream.p_newly_selected_es = NULL;
64     p_input->stream.pp_programs = NULL;
65     p_input->stream.p_selected_program = NULL;
66     p_input->stream.p_new_program = NULL;
67     
68     if( i_data_len )
69     {
70         if ( (p_input->stream.p_demux_data = malloc( i_data_len )) == NULL )
71         {
72             intf_ErrMsg( "Unable to allocate memory in input_InitStream");
73             return 1;
74         }
75         memset( p_input->stream.p_demux_data, 0, i_data_len );
76     }
77     else
78     {
79         p_input->stream.p_demux_data = NULL;
80     }
81
82     return 0;
83 }
84
85 /*****************************************************************************
86  * input_EndStream: free all stream descriptors
87  *****************************************************************************/
88 void input_EndStream( input_thread_t * p_input )
89 {
90     /* Free all programs and associated ES, and associated decoders. */
91     while( p_input->stream.i_pgrm_number )
92     {
93         input_DelProgram( p_input, p_input->stream.pp_programs[0] );
94     }
95
96     /* Free standalone ES */
97     while( p_input->stream.i_es_number )
98     {
99         input_DelES( p_input, p_input->stream.pp_es[0] );
100     }
101
102     /* Free all areas */
103     while( p_input->stream.i_area_nb )
104     {
105         input_DelArea( p_input, p_input->stream.pp_areas[0] );
106     }
107
108     /* Free selected ES */
109     if( p_input->stream.pp_selected_es != NULL )
110     {
111         free( p_input->stream.pp_selected_es );
112     }
113     
114     if( p_input->stream.p_demux_data != NULL )
115     {
116         free( p_input->stream.p_demux_data );
117     }
118 }
119
120 /*****************************************************************************
121  * input_FindProgram: returns a pointer to a program described by its ID
122  *****************************************************************************/
123 pgrm_descriptor_t * input_FindProgram( input_thread_t * p_input, u16 i_pgrm_id )
124 {
125     int     i;
126
127     for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
128     {
129         if( p_input->stream.pp_programs[i]->i_number == i_pgrm_id )
130         {
131             return p_input->stream.pp_programs[i];
132         }
133     }
134
135     return( NULL );
136 }
137
138 /*****************************************************************************
139  * input_AddProgram: add and init a program descriptor
140  *****************************************************************************
141  * This program descriptor will be referenced in the given stream descriptor
142  *****************************************************************************/
143 pgrm_descriptor_t * input_AddProgram( input_thread_t * p_input,
144                                       u16 i_pgrm_id, size_t i_data_len )
145 {
146     /* Where to add the pgrm */
147     int i_pgrm_index = p_input->stream.i_pgrm_number;
148
149     intf_DbgMsg("Adding description for pgrm %d", i_pgrm_id);
150
151     /* Add an entry to the list of program associated with the stream */
152     p_input->stream.i_pgrm_number++;
153     p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
154                                            p_input->stream.i_pgrm_number
155                                             * sizeof(pgrm_descriptor_t *) );
156     if( p_input->stream.pp_programs == NULL )
157     {
158         intf_ErrMsg( "Unable to realloc memory in input_AddProgram" );
159         return( NULL );
160     }
161     
162     /* Allocate the structure to store this description */
163     p_input->stream.pp_programs[i_pgrm_index] =
164                                         malloc( sizeof(pgrm_descriptor_t) );
165     if( p_input->stream.pp_programs[i_pgrm_index] == NULL )
166     {
167         intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
168         return( NULL );
169     }
170     
171     /* Init this entry */
172     p_input->stream.pp_programs[i_pgrm_index]->i_number = i_pgrm_id;
173     p_input->stream.pp_programs[i_pgrm_index]->b_is_ok = 0;
174     p_input->stream.pp_programs[i_pgrm_index]->i_version = 0;
175
176     p_input->stream.pp_programs[i_pgrm_index]->i_es_number = 0;
177     p_input->stream.pp_programs[i_pgrm_index]->pp_es = NULL;
178
179     input_ClockInit( p_input->stream.pp_programs[i_pgrm_index] );
180
181     p_input->stream.pp_programs[i_pgrm_index]->i_synchro_state
182                                                 = SYNCHRO_START;
183
184     if( i_data_len )
185     {
186         p_input->stream.pp_programs[i_pgrm_index]->p_demux_data =
187             malloc( i_data_len );
188         if( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data == NULL )
189         {
190             intf_ErrMsg( "Unable to allocate memory in input_AddProgram" );
191             return( NULL );
192         }
193         memset( p_input->stream.pp_programs[i_pgrm_index]->p_demux_data, 0,
194                 i_data_len );
195     }
196     else
197     {
198         p_input->stream.pp_programs[i_pgrm_index]->p_demux_data = NULL;
199     }
200
201     return p_input->stream.pp_programs[i_pgrm_index];
202 }
203
204 /*****************************************************************************
205  * input_DelProgram: destroy a program descriptor
206  *****************************************************************************
207  * All ES descriptions referenced in the descriptor will be deleted.
208  *****************************************************************************/
209 void input_DelProgram( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm )
210 {
211     int i_pgrm_index;
212
213     ASSERT( p_pgrm );
214
215     intf_DbgMsg("Deleting description for pgrm %d", p_pgrm->i_number);
216
217     /* Free the structures that describe the es that belongs to that program */
218     while( p_pgrm->i_es_number )
219     {
220         input_DelES( p_input, p_pgrm->pp_es[0] );
221     }
222
223     /* Free the demux data */
224     if( p_pgrm->p_demux_data != NULL )
225     {
226         free( p_pgrm->p_demux_data );
227     }
228
229     /* Find the program in the programs table */
230     for( i_pgrm_index = 0; i_pgrm_index < p_input->stream.i_pgrm_number;
231          i_pgrm_index++ )
232     {
233         if( p_input->stream.pp_programs[i_pgrm_index] == p_pgrm )
234             break;
235     }
236
237     /* Remove this program from the stream's list of programs */
238     p_input->stream.i_pgrm_number--;
239
240     p_input->stream.pp_programs[i_pgrm_index] =
241         p_input->stream.pp_programs[p_input->stream.i_pgrm_number];
242     p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
243                                            p_input->stream.i_pgrm_number
244                                             * sizeof(pgrm_descriptor_t *) );
245
246     if( p_input->stream.i_pgrm_number && p_input->stream.pp_programs == NULL)
247     {
248         intf_ErrMsg( "input error: unable to realloc program list"
249                      " in input_DelProgram" );
250     }
251
252     /* Free the description of this program */
253     free( p_pgrm );
254 }
255
256 /*****************************************************************************
257  * input_AddArea: add and init an area descriptor
258  *****************************************************************************
259  * This area descriptor will be referenced in the given stream descriptor
260  *****************************************************************************/
261 input_area_t * input_AddArea( input_thread_t * p_input )
262 {
263     /* Where to add the pgrm */
264     int i_area_index = p_input->stream.i_area_nb;
265
266     intf_DbgMsg("Adding description for area %d", i_area_index );
267
268     /* Add an entry to the list of program associated with the stream */
269     p_input->stream.i_area_nb++;
270     p_input->stream.pp_areas = realloc( p_input->stream.pp_areas,
271                                         p_input->stream.i_area_nb
272                                             * sizeof(input_area_t *) );
273     if( p_input->stream.pp_areas == NULL )
274     {
275         intf_ErrMsg( "Unable to realloc memory in input_AddArea" );
276         return( NULL );
277     }
278     
279     /* Allocate the structure to store this description */
280     p_input->stream.pp_areas[i_area_index] =
281                                         malloc( sizeof(input_area_t) );
282     if( p_input->stream.pp_areas[i_area_index] == NULL )
283     {
284         intf_ErrMsg( "Unable to allocate memory in input_AddArea" );
285         return( NULL );
286     }
287     
288     /* Init this entry */
289     p_input->stream.pp_areas[i_area_index]->i_id = 0;
290     p_input->stream.pp_areas[i_area_index]->i_start = 0;
291     p_input->stream.pp_areas[i_area_index]->i_size = 0;
292     p_input->stream.pp_areas[i_area_index]->i_tell = 0;
293     p_input->stream.pp_areas[i_area_index]->i_seek = NO_SEEK;
294     p_input->stream.pp_areas[i_area_index]->i_part_nb = 1;
295     p_input->stream.pp_areas[i_area_index]->i_part= 0;
296     p_input->stream.pp_areas[i_area_index]->i_angle_nb = 1;
297     p_input->stream.pp_areas[i_area_index]->i_angle = 0;
298
299     return p_input->stream.pp_areas[i_area_index];
300 }
301
302 /*****************************************************************************
303  * input_SetProgram: changes the current program
304  *****************************************************************************/
305 int input_SetProgram( input_thread_t * p_input, pgrm_descriptor_t * p_new_prg )
306 {
307     int i_es_index;
308 #define old_prg p_input->stream.p_selected_program
309     for ( i_es_index = 0 ; i_es_index < old_prg->i_es_number ; i_es_index ++ )
310     {
311         input_UnselectES( p_input , old_prg->pp_es[i_es_index] );
312     }
313 #undef old_prg
314     for (i_es_index = 0 ; i_es_index < p_new_prg->i_es_number ; i_es_index ++ )
315     {
316         input_SelectES( p_input , p_new_prg->pp_es[i_es_index] );
317     }
318
319     p_input->stream.p_selected_program = p_new_prg;
320
321     return( 0 );
322 }
323
324
325 /*****************************************************************************
326  * input_DelArea: destroy a area descriptor
327  *****************************************************************************
328  * All ES descriptions referenced in the descriptor will be deleted.
329  *****************************************************************************/
330 void input_DelArea( input_thread_t * p_input, input_area_t * p_area )
331 {
332     int i_area_index;
333
334     ASSERT( p_area );
335
336     intf_DbgMsg("Deleting description for area %d", p_area->i_id );
337
338     /* Find the area in the areas table */
339     for( i_area_index = 0; i_area_index < p_input->stream.i_area_nb;
340          i_area_index++ )
341     {
342         if( p_input->stream.pp_areas[i_area_index] == p_area )
343             break;
344     }
345
346     /* Remove this area from the stream's list of areas */
347     p_input->stream.i_area_nb--;
348
349     p_input->stream.pp_areas[i_area_index] =
350         p_input->stream.pp_areas[p_input->stream.i_area_nb];
351     p_input->stream.pp_areas = realloc( p_input->stream.pp_areas,
352                                            p_input->stream.i_area_nb
353                                             * sizeof(input_area_t *) );
354
355     if( p_input->stream.i_area_nb && p_input->stream.pp_areas == NULL)
356     {
357         intf_ErrMsg( "input error: unable to realloc area list"
358                      " in input_DelArea" );
359     }
360
361     /* Free the description of this area */
362     free( p_area );
363 }
364
365
366 /*****************************************************************************
367  * input_FindES: returns a pointer to an ES described by its ID
368  *****************************************************************************/
369 es_descriptor_t * input_FindES( input_thread_t * p_input, u16 i_es_id )
370 {
371     int     i;
372
373     for( i = 0; i < p_input->stream.i_es_number; i++ )
374     {
375         if( p_input->stream.pp_es[i]->i_id == i_es_id )
376         {
377             return p_input->stream.pp_es[i];
378         }
379     }
380
381     return( NULL );
382 }
383
384 /*****************************************************************************
385  * input_AddES:
386  *****************************************************************************
387  * Reserve a slot in the table of ES descriptors for the ES and add it to the
388  * list of ES of p_pgrm. If p_pgrm if NULL, then the ES is considered as stand
389  * alone (PSI ?)
390  *****************************************************************************/
391 es_descriptor_t * input_AddES( input_thread_t * p_input,
392                                pgrm_descriptor_t * p_pgrm, u16 i_es_id,
393                                size_t i_data_len )
394 {
395     es_descriptor_t * p_es;
396
397     intf_DbgMsg("Adding description for ES 0x%x", i_es_id);
398
399     p_es = (es_descriptor_t *)malloc( sizeof(es_descriptor_t) );
400     if( p_es == NULL )
401     {
402         intf_ErrMsg( "Unable to allocate memory in input_AddES" );
403         return( NULL);
404     }
405     p_input->stream.i_es_number++;
406     p_input->stream.pp_es = realloc( p_input->stream.pp_es,
407                                      p_input->stream.i_es_number
408                                       * sizeof(es_descriptor_t *) );
409     if( p_input->stream.pp_es == NULL )
410     {
411         intf_ErrMsg( "Unable to realloc memory in input_AddES" );
412         return( NULL );
413     }
414     p_input->stream.pp_es[p_input->stream.i_es_number - 1] = p_es;
415
416     /* Init its values */
417     p_es->i_id = i_es_id;
418     p_es->psz_desc[0] = '\0';
419     p_es->p_pes = NULL;
420     p_es->p_decoder_fifo = NULL;
421     p_es->b_audio = 0;
422     p_es->i_cat = UNKNOWN_ES;
423
424     if( i_data_len )
425     {
426         p_es->p_demux_data = malloc( i_data_len );
427         if( p_es->p_demux_data == NULL )
428         {
429             intf_ErrMsg( "Unable to allocate memory in input_AddES" );
430             return( NULL );
431         }
432         memset( p_es->p_demux_data, 0, i_data_len );
433     }
434     else
435     {
436         p_es->p_demux_data = NULL;
437     }
438
439     /* Add this ES to the program definition if one is given */
440     if( p_pgrm )
441     {
442         p_pgrm->i_es_number++;
443         p_pgrm->pp_es = realloc( p_pgrm->pp_es,
444                                  p_pgrm->i_es_number
445                                   * sizeof(es_descriptor_t *) );
446         if( p_pgrm->pp_es == NULL )
447         {
448             intf_ErrMsg( "Unable to realloc memory in input_AddES" );
449             return( NULL );
450         }
451         p_pgrm->pp_es[p_pgrm->i_es_number - 1] = p_es;
452         p_es->p_pgrm = p_pgrm;
453     }
454     else
455     {
456         p_es->p_pgrm = NULL;
457     }
458
459     return p_es;
460 }
461
462 /*****************************************************************************
463  * input_DelES:
464  *****************************************************************************/
465 void input_DelES( input_thread_t * p_input, es_descriptor_t * p_es )
466 {
467     int                     i_index, i_es_index;
468     pgrm_descriptor_t *     p_pgrm;
469
470     ASSERT( p_es );
471     p_pgrm = p_es->p_pgrm;
472
473     /* Kill associated decoder, if any. */
474     if( p_es->p_decoder_fifo != NULL )
475     {
476         input_EndDecoder( p_input, p_es );
477     }
478
479     /* Remove this ES from the description of the program if it is associated to
480      * one */
481     if( p_pgrm )
482     {
483         for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
484         {
485             if( p_pgrm->pp_es[i_index] == p_es )
486             {
487                 p_pgrm->i_es_number--;
488                 p_pgrm->pp_es[i_index] = p_pgrm->pp_es[p_pgrm->i_es_number];
489                 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
490                                          p_pgrm->i_es_number
491                                           * sizeof(es_descriptor_t *));
492                 if( p_pgrm->i_es_number && p_pgrm->pp_es == NULL )
493                 {
494                     intf_ErrMsg( "Unable to realloc memory in input_DelES" );
495                 }
496                 break;
497             }
498         }
499     }
500
501     /* Free the demux data */
502     if( p_es->p_demux_data != NULL )
503     {
504         free( p_es->p_demux_data );
505     }
506
507     /* Find the ES in the ES table */
508     for( i_es_index = 0; i_es_index < p_input->stream.i_es_number;
509          i_es_index++ )
510     {
511         if( p_input->stream.pp_es[i_es_index] == p_es )
512             break;
513     }
514
515     /* Free the ES */
516     free( p_es );
517     p_input->stream.i_es_number--;
518     p_input->stream.pp_es[i_es_index] =
519                     p_input->stream.pp_es[p_input->stream.i_es_number];
520     p_input->stream.pp_es = realloc( p_input->stream.pp_es,
521                                      p_input->stream.i_es_number
522                                       * sizeof(es_descriptor_t *));
523     if( p_input->stream.i_es_number && p_input->stream.pp_es == NULL )
524     {
525         intf_ErrMsg( "Unable to realloc memory in input_DelES" );
526     }
527     
528 }
529
530 /*****************************************************************************
531  * input_SelectES: selects an ES and spawns the associated decoder
532  *****************************************************************************
533  * Remember we are still supposed to have stream_lock when entering this
534  * function ?
535  *****************************************************************************/
536 int input_SelectES( input_thread_t * p_input, es_descriptor_t * p_es )
537 {
538     if( p_es == NULL )
539     {
540         intf_ErrMsg( "Nothing to do in input_SelectES" );
541         return -1;
542     }
543
544 #ifdef TRACE_INPUT
545     intf_DbgMsg( "Selecting ES 0x%x", p_es->i_id );
546 #endif
547
548     if( p_es->p_decoder_fifo != NULL )
549     {
550         intf_ErrMsg( "ES 0x%x is already selected", p_es->i_id );
551         return( -1 );
552     }
553
554     switch( p_es->i_type )
555     {
556     case AC3_AUDIO_ES:
557     case MPEG1_AUDIO_ES:
558     case MPEG2_AUDIO_ES:
559     case LPCM_AUDIO_ES:
560         if( p_main->b_audio )
561         {
562             /* This kludge should be removed */
563             p_main->b_ac3 = ( p_es->i_type == AC3_AUDIO_ES );
564
565             /* Release the lock, not to block the input thread during
566              * the creation of the thread. */
567             vlc_mutex_unlock( &p_input->stream.stream_lock );
568             p_es->thread_id = input_RunDecoder( p_input, p_es );
569             vlc_mutex_lock( &p_input->stream.stream_lock );
570         }
571         break;
572
573     case MPEG1_VIDEO_ES:
574     case MPEG2_VIDEO_ES:
575     case DVD_SPU_ES:
576         if( p_main->b_video )
577         {
578             /* Release the lock, not to block the input thread during
579              * the creation of the thread. */
580             vlc_mutex_unlock( &p_input->stream.stream_lock );
581             p_es->thread_id = input_RunDecoder( p_input, p_es );
582             vlc_mutex_lock( &p_input->stream.stream_lock );
583         }
584         break;
585
586     default:
587         intf_ErrMsg( "Unknown stream type 0x%x", p_es->i_type );
588         return( -1 );
589         break;
590     }
591
592     if( p_es->thread_id == 0 )
593     {
594         return( -1 );
595     }
596
597     return( 0 );
598 }
599
600 /*****************************************************************************
601  * input_UnselectES: removes an ES from the list of selected ES
602  *****************************************************************************/
603 int input_UnselectES( input_thread_t * p_input, es_descriptor_t * p_es )
604 {
605
606     int     i_index = 0;
607
608     if( p_es == NULL )
609     {
610         intf_ErrMsg( "Nothing to do in input_UnselectES" );
611         return -1;
612     }
613
614 #ifdef TRACE_INPUT
615     intf_DbgMsg( "Unselecting ES 0x%x", p_es->i_id );
616 #endif
617
618     if( p_es->p_decoder_fifo == NULL )
619     {
620         intf_ErrMsg( "ES 0x%x is not selected", p_es->i_id );
621         return( -1 );
622     }
623
624     input_EndDecoder( p_input, p_es );
625
626     if( ( p_es->p_decoder_fifo == NULL ) &&
627         ( p_input->stream.i_selected_es_number > 0 ) )
628     {
629         p_input->stream.i_selected_es_number--;
630
631         while( ( i_index < p_input->stream.i_selected_es_number ) &&
632                ( p_input->stream.pp_selected_es[i_index] != p_es ) )
633         {
634             i_index++;
635         }
636
637         p_input->stream.pp_selected_es[i_index] = 
638           p_input->stream.pp_selected_es[p_input->stream.i_selected_es_number];
639
640         p_input->stream.pp_selected_es = realloc(
641                                            p_input->stream.pp_selected_es,
642                                            p_input->stream.i_selected_es_number
643                                             * sizeof(es_descriptor_t *) );
644
645         if( p_input->stream.pp_selected_es == NULL )
646         {
647 #ifdef TRACE_INPUT
648             intf_DbgMsg( "No more selected ES in input_UnselectES" );
649 #endif
650             return( 1 );
651         }
652     }
653
654     return( 0 );
655 }