]> git.sesse.net Git - vlc/blob - src/video_parser/video_fifo.c
D�but du d�codeur + d�but du parseur.
[vlc] / src / video_parser / video_fifo.c
1 /*******************************************************************************
2  * video_fifo.c : video FIFO management
3  * (c)1999 VideoLAN
4  *******************************************************************************/
5
6 /*******************************************************************************
7  * Preamble
8  *******************************************************************************/
9 #include <errno.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <string.h>
14 #include <sys/uio.h>
15 #include <X11/Xlib.h>
16 #include <X11/extensions/XShm.h>
17
18 #include "config.h"
19 #include "common.h"
20 #include "mtime.h"
21 #include "vlc_thread.h"
22
23 #include "intf_msg.h"
24 #include "debug.h"                      /* ?? temporaire, requis par netlist.h */
25
26 #include "input.h"
27 #include "input_netlist.h"
28 #include "decoder_fifo.h"
29 #include "video.h"
30 #include "video_output.h"
31 #include "video_parser.h"
32
33 #include "undec_picture.h"
34 #include "video_fifo.h"
35 #include "video_decoder.h"
36
37 /*****************************************************************************
38  * vpar_InitFIFO : initialize the video FIFO
39  *****************************************************************************/
40 void vpar_InitFIFO( vpar_thread_t * p_vpar )
41 {
42     int i_dummy;
43     
44     /* Initialize mutex and cond */
45     vlc_mutex_init( p_vpar->vfifo.lock );
46     vlc_cond_init( p_vpar->vfifo.wait );
47     vlc_mutex_init( p_vpar->vbuffer.lock );
48     
49     /* Initialize FIFO properties */
50     p_vpar->vfifo.i_start = p_vpar->vfifo.i_end = 0;
51     p_vpar->vfifo.p_vpar = p_vpar;
52     
53     /* Initialize buffer properties */
54     i_index = VFIFO_SIZE; /* all structures are available */
55     for( i_dummy = 0; i_dummy < VFIFO_SIZE + 1; i_dummy++ )
56     {
57         p_vpar->vfifo.pp_undec_free[i_dummy] = p_vpar->vfifo.p_undec_p + i;
58     }
59 }
60
61 /*****************************************************************************
62  * vpar_GetPicture : return a picture to be decoded
63  *****************************************************************************/
64 undec_picture_t * vpar_GetPicture( video_fifo_t * p_fifo )
65 {
66     undec_picture_t *   p_undec_p;
67
68     vlc_mutex_lock( &p_fifo->lock );
69     while( VIDEO_FIFO_ISEMPTY( *p_fifo ) )
70     {
71         vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
72         if( p_fifo->p_vpar->b_die )
73         {
74             vlc_mutex_unlock( &p_fifo->lock );
75             return( NULL );
76         }
77     }
78     
79     p_undec_p = VIDEO_FIFO_START( *p_fifo );
80     VIDEO_FIFO_INCSTART( *p_fifo );
81     
82     vlc_mutex_unlock( &p_fifo->lock );
83     
84     return( p_undec_p );
85 }
86
87 /*****************************************************************************
88  * vpar_NewPicture : return a buffer for the parser
89  *****************************************************************************/
90 undec_picture_t * vpar_NewPicture( video_fifo_t * p_fifo )
91 {
92     undec_picture_t *   p_undec_p;
93
94 #define P_buffer p_fifo->p_vpar.vbuffer
95     vlc_mutex_lock( &P_buffer->lock );
96     if( P_buffer.i_index == -1 )
97     {
98         /* No more structures available. This should not happen ! */
99         return NULL;
100     }
101
102     p_undec_p = P_buffer->pp_undec_free[ P_buffer->i_index-- ];
103 #undef P_buffer
104
105     vlc_mutex_unlock( &P_buffer->lock );
106     return( p_undec_p );
107 }
108
109 /*****************************************************************************
110  * vpar_DecodePicture : put a picture in the video fifo, if it is decodable
111  *****************************************************************************/
112 void vpar_DecodePicture( video_fifo_t * p_fifo, undec_picture_t * p_undec_p )
113 {
114     boolean_t           b_decodable;
115     
116     switch( p_undec_p )
117     {
118     case B_CODING_TYPE:
119         b_decodable = ((p_undec_p->p_backward_p != NULL) &&
120                        (p_undec_p->p_forward_p != NULL));
121         break;
122     case P_CODING_TYPE:
123         b_decodable = (p_undec_p->p_backward_p != NULL);
124         break;
125     case I_CODING_TYPE:
126     case D_CODING_TYPE:
127         b_decodable = TRUE;
128         break;
129     default:
130         /* That should not happen */
131     }
132
133     if( b_decodable )
134     {
135         /* Place picture in the video FIFO */
136         vlc_mutex_lock( &p_fifo->lock );
137         
138         /* By construction, the video FIFO cannot be full */
139         VIDEO_FIFO_END( *p_fifo ) = p_undec_p;
140         VIDEO_FIFO_INCEND( *p_fifo );
141         
142         vlc_mutex_unlock( &p_fifo->lock );
143     }
144 }
145
146 /*****************************************************************************
147  * vpar_ReleasePicture : put a picture in the video_output fifo, and update
148  *                  links and buffers
149  *****************************************************************************/
150 void vpar_ReleasePicture( video_fifo_t * p_fifo, undec_picture_t * p_undec_p )
151 {
152     int         i_ref;
153     
154     /* Tell referencing pictures so that they can be decoded */
155     for( i_ref = 0; p_undec_p->pp_referencing_undec[i_ref].p_undec != NULL
156                     && i_ref < MAX_REFERENCING_UNDEC; i_ref++ )
157     {
158         *p_undec_p->pp_referencing_undec[i_ref].pp_frame = p_undec_p->p_picture;
159         vout_LinkPicture( p_fifo->p_vpar.p_vout, p_picture );
160         
161         /* Try to put the referencing picture in the video FIFO */
162         vpar_DecodePicture( p_fifo, p_undec_p->pp_referencing_undec[i_ref].p_undec );
163     }
164
165     /* Unlink referenced pictures */
166     if( p_undec_p->p_forward_ref != NULL )
167     {
168         vout_UnlinkPicture( p_fifo->p_vpar.p_vout, p_undec_p->p_forward_ref );
169         if( p_undec_p->p_backward_ref != NULL )
170         {
171             vout_UnlinkPicture( p_fifo->p_vpar.p_vout, p_undec_p->p_backward_ref );
172         }
173     }
174     
175     /* Mark the picture to be displayed */
176     vout_DisplayPicture( p_fifo->p_vpar.p_vout, p_undec_p->p_picture );
177
178     /* Release the undec_picture_t structure */
179 #define P_buffer p_fifo->p_vpar.vbuffer
180     vlc_mutex_lock( &P_buffer->lock );
181
182     P_buffer->pp_undec_free[ ++P_buffer->i_index ] = p_undec_p;
183     
184     vlc_mutex_unlock( &P_buffer->lock );
185 #undef P_buffer
186     }
187 }
188
189 /*****************************************************************************
190  * vpar_DestroyPicture : destroy a picture in case of error
191  *****************************************************************************/
192 void vpar_DestroyPicture( video_fifo_t * p_fifo, undec_picture_t * p_undec_p )
193 {
194     int         i_ref;
195     
196     /* Destroy referencing pictures */
197     for( i_ref = 0; p_undec_p->pp_referencing_undec[i_ref].p_undec != NULL
198                     && i_ref < MAX_REFERENCING_UNDEC; i_ref++ )
199     {
200         /* Try to put the referencing picture in the video FIFO */
201         vpar_DestroyPicture( p_fifo, p_undec_p->pp_referencing_undec[i_ref].p_undec );
202     }
203
204     /* Unlink referenced pictures */
205     if( p_undec_p->p_forward_ref != NULL )
206     {
207         vout_UnlinkPicture( p_fifo->p_vpar.p_vout, p_undec_p->p_forward_ref );
208         if( p_undec_p->p_backward_ref != NULL )
209         {
210             vout_UnlinkPicture( p_fifo->p_vpar.p_vout, p_undec_p->p_backward_ref );
211         }
212     }
213     
214     /* Release the picture buffer */
215     vout_DestroyPicture( p_fifo->p_vpar.p_vout, p_undec_p->p_picture );
216     
217     /* Release the undec_picture_t structure */
218 #define P_buffer p_fifo->p_vpar.vbuffer
219     vlc_mutex_lock( &P_buffer->lock );
220
221     P_buffer->pp_undec_free[ ++P_buffer->i_index ] = p_undec_p;
222     
223     vlc_mutex_unlock( &P_buffer->lock );
224 #undef P_buffer
225     }
226 }