]> git.sesse.net Git - vlc/blob - src/input/input_dec.c
eb53c053311c62eab0b84924ac7166b376b9c6e2
[vlc] / src / input / input_dec.c
1 /*****************************************************************************
2  * input_dec.c: Functions for the management of decoders
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  * $Id: input_dec.c,v 1.13 2001/07/18 14:21:00 massiot 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 "config.h"
34 #include "common.h"
35 #include "threads.h"
36 #include "mtime.h"
37 #include "intf_msg.h"
38
39 #include "stream_control.h"
40 #include "input_ext-dec.h"
41 #include "input_ext-intf.h"
42 #include "input_ext-plugins.h"
43
44 /*****************************************************************************
45  * input_RunDecoder: spawns a new decoder thread
46  *****************************************************************************/
47 vlc_thread_t input_RunDecoder( decoder_capabilities_t * p_decoder,
48                                void * p_data )
49 {
50     return( p_decoder->pf_create_thread( p_data ) );
51 }
52
53 /*****************************************************************************
54  * input_EndDecoder: kills a decoder thread and waits until it's finished
55  *****************************************************************************/
56 void input_EndDecoder( input_thread_t * p_input, es_descriptor_t * p_es )
57 {
58     int i_dummy;
59
60     p_es->p_decoder_fifo->b_die = 1;
61
62     /* Make sure the thread leaves the NextDataPacket() function by
63      * sending it a few null packets. */
64     for( i_dummy = 0; i_dummy < PADDING_PACKET_NUMBER; i_dummy++ )
65     {
66         input_NullPacket( p_input, p_es );
67     }
68
69     if( p_es->p_pes != NULL )
70     {
71         input_DecodePES( p_es->p_decoder_fifo, p_es->p_pes );
72     }
73
74     /* Waiting for the thread to exit */
75     /* I thought that unlocking was better since thread join can be long
76      * but it actually creates late pictures and freezes --stef */
77 //    vlc_mutex_unlock( &p_input->stream.stream_lock );
78     vlc_thread_join( p_es->thread_id );
79 //    vlc_mutex_lock( &p_input->stream.stream_lock );
80
81     /* Freeing all packets still in the decoder fifo. */
82     while( !DECODER_FIFO_ISEMPTY( *p_es->p_decoder_fifo ) )
83     {
84         p_es->p_decoder_fifo->pf_delete_pes(
85                             p_es->p_decoder_fifo->p_packets_mgt,
86                             DECODER_FIFO_START( *p_es->p_decoder_fifo ) );
87         DECODER_FIFO_INCSTART( *p_es->p_decoder_fifo );
88     }
89
90     /* Destroy the lock and cond */
91     vlc_cond_destroy( &p_es->p_decoder_fifo->data_wait );
92     vlc_mutex_destroy( &p_es->p_decoder_fifo->data_lock );
93
94     free( p_es->p_decoder_fifo );
95     p_es->p_decoder_fifo = NULL;
96 }
97
98 /*****************************************************************************
99  * input_DecodePES
100  *****************************************************************************
101  * Put a PES in the decoder's fifo.
102  *****************************************************************************/
103 void input_DecodePES( decoder_fifo_t * p_decoder_fifo, pes_packet_t * p_pes )
104 {
105     vlc_mutex_lock( &p_decoder_fifo->data_lock );
106
107     if( !DECODER_FIFO_ISFULL( *p_decoder_fifo ) )
108     {
109         p_decoder_fifo->buffer[p_decoder_fifo->i_end] = p_pes;
110         DECODER_FIFO_INCEND( *p_decoder_fifo );
111
112         /* Warn the decoder that it's got work to do. */
113         vlc_cond_signal( &p_decoder_fifo->data_wait );
114     }
115     else
116     {
117         /* The FIFO is full !!! This should not happen. */
118         p_decoder_fifo->pf_delete_pes( p_decoder_fifo->p_packets_mgt,
119                                        p_pes );
120         intf_ErrMsg( "PES trashed - decoder fifo full !" );
121     }
122     vlc_mutex_unlock( &p_decoder_fifo->data_lock );
123 }
124
125 /*****************************************************************************
126  * input_EscapeDiscontinuity: send a NULL packet to the decoders
127  *****************************************************************************/
128 void input_EscapeDiscontinuity( input_thread_t * p_input,
129                                 pgrm_descriptor_t * p_pgrm )
130 {
131     int     i_es, i;
132
133     for( i_es = 0; i_es < p_pgrm->i_es_number; i_es++ )
134     {
135         es_descriptor_t * p_es = p_pgrm->pp_es[i_es];
136
137         if( p_es->p_decoder_fifo != NULL )
138         {
139             for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
140             {
141                 input_NullPacket( p_input, p_es );
142             }
143         }
144     }
145 }
146
147 /*****************************************************************************
148  * input_EscapeAudioDiscontinuity: send a NULL packet to the audio decoders
149  *****************************************************************************/
150 void input_EscapeAudioDiscontinuity( input_thread_t * p_input )
151 {
152     int     i_pgrm, i_es, i;
153
154     for( i_pgrm = 0; i_pgrm < p_input->stream.i_pgrm_number; i_pgrm++ )
155     {
156         pgrm_descriptor_t * p_pgrm = p_input->stream.pp_programs[i_pgrm];
157
158         for( i_es = 0; i_es < p_pgrm->i_es_number; i_es++ )
159         {
160             es_descriptor_t * p_es = p_pgrm->pp_es[i_es];
161
162             if( p_es->p_decoder_fifo != NULL && p_es->b_audio )
163             {
164                 for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
165                 {
166                     input_NullPacket( p_input, p_es );
167                 }
168             }
169         }
170     }
171 }
172