]> git.sesse.net Git - vlc/blob - src/input/input_programs.c
* Code to manage es_descriptors and program_descriptors, from benny's
[vlc] / src / input / input_programs.c
1 /*****************************************************************************
2  * input_programs.c: es_descriptor_t, pgrm_descriptor_t management
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  *
6  * Authors:
7  *
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.
12  * 
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.
17  *
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  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * input_InitStream: init the stream descriptor of the given input
29  *****************************************************************************/
30 void input_InitStream( input_thread_t * p_input )
31 {
32     p_input->stream->i_pgrm_number = 0;
33     p_input->stream->pp_programs = NULL;
34 }
35
36 /*****************************************************************************
37  * input_AddProgram: add and init a program descriptor
38  *****************************************************************************
39  * This program descriptor will be referenced in the given stream descriptor
40  *****************************************************************************/
41 pgrm_descriptor_t * input_AddProgram( input_thread_t * p_input,
42                                       u16 i_pgrm_id)
43 {
44     int i_pgrm_index = p_stream->i_pgrm_number;     /* Where to add the pgrm */
45
46     intf_DbgMsg("Adding description for pgrm %d", i_pgrm_id);
47
48     /* Add an entry to the list of program associated with the stream */
49     p_input->stream.i_pgrm_number++;
50     p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
51                                            p_input->stream.i_pgrm_number
52                                             * sizeof(pgrm_descriptor_t *) );
53
54     /* Allocate the structure to store this description */
55     p_input->stream.pp_programs[i_pgrm_index] =
56                                         malloc( sizeof(pgrm_descriptor_t) );
57
58     /* Init this entry */
59     p_input->stream.pp_programs[i_pgrm_index]->i_number = i_pgrm_id;
60     p_input->stream.pp_programs[i_pgrm_index]->b_is_ok = 0;
61
62     p_input->stream.pp_programs[i_pgrm_index]->i_es_number = 0;
63     p_input->stream.pp_programs[i_pgrm_index]->pp_es = NULL;
64
65     p_input->stream.pp_programs[i_pgrm_index]->delta_cr = 0;
66     p_input->stream.pp_programs[i_pgrm_index]->delta_absolute = 0;
67     p_input->stream.pp_programs[i_pgrm_index]->last_cr = 0;
68     p_input->stream.pp_programs[i_pgrm_index]->c_average_count = 0;
69     p_input->stream.pp_programs[i_pgrm_index]->i_synchro_state
70                                                 = SYNCHRO_NOT_STARTED;
71
72     p_input->stream.pp_programs[i_pgrm_index]->p_vout
73                                             = p_input->p_default_vout;
74     p_input->stream.pp_programs[i_pgrm_index]->p_aout
75                                             = p_input->p_default_aout;
76
77     return p_input->stream.pp_programs[i_pgrm_index];
78 }
79
80 /*****************************************************************************
81  * input_DelProgram: destroy a program descriptor
82  *****************************************************************************
83  * All ES descriptions referenced in the descriptor will be deleted.
84  *****************************************************************************/
85 void input_DelProgram( input_thread_t * p_input, u16 i_pgrm_id )
86 {
87     int i_index, i_pgrm_index = -1;
88     pgrm_descriptor_t * p_pgrm = NULL;
89
90     intf_DbgMsg("Deleting description for pgrm %d", i_pgrm_id);
91
92     /* Find where this program is described */
93     for( i_index = 0; i_index < p_input->stream.i_pgrm_number; i_index++ )
94     {
95         if( p_input->stream.pp_programs[i_index]->i_number == i_pgrm_id )
96         {
97             i_pgrm_index = i_index;
98             p_pgrm = p_input->stream.pp_programs[ i_pgrm_index ];
99             break;
100         }
101     }
102
103     /* Make sure that the pgrm exists */
104     ASSERT(i_pgrm_index >= 0);
105     ASSERT(p_pgrm);
106
107     /* Free the structures that describe the es that belongs to that program */
108     for( i_index = 0; i_index < p_pgrm->i_es_number; i_index++ )
109     {
110         DestroyESDescr( p_input, p_pgrm, p_pgrm->pp_es[i_index]->i_id );
111     }
112
113     /* Free the table of es descriptors */
114     free( p_pgrm->pp_es );
115
116     /* Free the description of this stream */
117     free( p_pgrm );
118
119     /* Remove this program from the stream's list of programs */
120     p_input->stream.i_pgrm_number--;
121     p_input->stream.pp_programs[i_pgrm_index] =
122         p_input->stream.pp_programs[p_input->stream.i_pgrm_number];
123     p_input->stream.pp_programs = realloc( p_input->stream.pp_programs,
124                                            p_input->stream.i_pgrm_number
125                                             * sizeof(pgrm_descriptor_t *) );
126 }
127
128 /*****************************************************************************
129  * input_AddES:
130  *****************************************************************************
131  * Reserve a slot in the table of ES descriptors for the ES and add it to the
132  * list of ES of p_pgrm. If p_pgrm if NULL, then the ES is considered as stand
133  * alone (PSI ?)
134  *****************************************************************************/
135 es_descriptor_t * input_AddES( input_thread_t * p_input,
136                                pgrm_descriptor_t * p_pgrm, u16 i_es_id)
137 {
138     int i_index;
139     es_descriptor_t * p_es = NULL;
140
141     intf_DbgMsg("Adding description for ES %d", i_es_id);
142
143     /* Find an empty slot to store the description of that es */
144     for( i_index = 0; i_index < INPUT_MAX_ES &&
145          p_input->p_es[i_index].i_id != EMPTY_ID; i_index++ );
146
147     if( i_index >= INPUT_MAX_ES )
148     {
149         /* No slot is empty */
150         intf_ErrMsg("Stream carries too many ES for our decoder");
151     }
152     else
153     {
154         /* Reserve the slot for that ES */
155         p_es = &p_input->p_es[i_index];
156         p_es->i_id = i_es_id;
157         intf_DbgMsg("Slot %d in p_es table assigned to ES %d",
158                     i_index, i_es_pid);
159
160         /* Init its values */
161         p_es->b_discontinuity = 0;
162         p_es->p_pes_packet = NULL;
163         p_es->p_decoder_fifo = NULL;
164
165         /* Add this ES to the program definition if one is given */
166         if( p_pgrm )
167         {
168             p_pgrm->i_es_number++;
169             p_pgrm->pp_es = realloc( p_pgrm->pp_es,
170                                      p_pgrm->i_es_number
171                                       * sizeof(es_descriptor_t *) );
172             p_pgrm->pp_es[p_pgrm->i_es_number - 1] = p_es;
173             p_es->p_pgrm = p_pgrm;
174         }
175         else
176         {
177             p_es->p_pgrm = NULL;
178         }
179     }
180
181     return p_es;
182 }
183
184 /*****************************************************************************
185  * input_DelES:
186  *****************************************************************************/
187 void input_DelES( input_thread_t * p_input, u16 i_id)
188 {
189     int                     i_index;
190     program_descriptor_t *  p_pgrm;
191     es_descriptor_t *       p_es;
192
193     /* Look for the description of the ES */
194     for( i_index = 0; i_index < INPUT_MAX_ES; i_index++ )
195     {
196         if( p_input->p_es[i_index].i_id == i_id )
197         {
198             p_es = &p_input->p_es[i_index];
199             p_pgrm = p_input->p_es[i_index].p_pgrm;
200             break;
201         }
202     }
203
204     ASSERT( p_es );
205
206     /* Remove this ES from the description of the program if it is associated to
207      * one */
208     if( p_pgrm )
209     {
210         for( i_index = 0; ; i_index++ )
211         {
212             if( p_pgrm->p_es[i_index].i_id == i_id )
213             {
214                 p_pgrm->i_es_number--;
215                 p_pgrm->pp_es[i_index] = p_pgrm->p_es[p_pgrm->i_es_number];
216                 p_pgrm->pp_es = realloc( p_pgrm->pp_es,
217                                          p_pgrm->i_es_number
218                                           * sizeof(es_decriptor_t *));
219                 break;
220             }
221         }
222     }
223
224     /* The table of stream descriptors is static, so don't free memory
225      * but just mark the slot as unused */
226     p_es->i_id = EMPTY_ID;
227 }