]> git.sesse.net Git - vlc/blob - src/video_parser/video_fifo.c
Un bon morceau 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         p_vpar->vfifo.p_undec_p[i].p_mb_info = NULL;
59     }
60 }
61
62 /*****************************************************************************
63  * vpar_GetPicture : return a picture to be decoded
64  *****************************************************************************/
65 undec_picture_t * vpar_GetPicture( video_fifo_t * p_fifo )
66 {
67     undec_picture_t *   p_undec_p;
68
69     vlc_mutex_lock( &p_fifo->lock );
70     while( VIDEO_FIFO_ISEMPTY( *p_fifo ) )
71     {
72         vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
73         if( p_fifo->p_vpar->b_die )
74         {
75             vlc_mutex_unlock( &p_fifo->lock );
76             return( NULL );
77         }
78     }
79     
80     p_undec_p = VIDEO_FIFO_START( *p_fifo );
81     VIDEO_FIFO_INCSTART( *p_fifo );
82     
83     vlc_mutex_unlock( &p_fifo->lock );
84     
85     return( p_undec_p );
86 }
87
88 /*****************************************************************************
89  * vpar_NewPicture : return a buffer for the parser
90  *****************************************************************************/
91 undec_picture_t * vpar_NewPicture( video_fifo_t * p_fifo )
92 {
93     undec_picture_t *   p_undec_p;
94
95 #define P_buffer p_fifo->p_vpar.vbuffer
96     vlc_mutex_lock( &P_buffer->lock );
97     if( P_buffer.i_index == -1 )
98     {
99         /* No more structures available. This should not happen ! */
100         return NULL;
101     }
102
103     p_undec_p = P_buffer->pp_undec_free[ P_buffer->i_index-- ];
104 #undef P_buffer
105
106     vlc_mutex_unlock( &P_buffer->lock );
107     return( p_undec_p );
108 }
109
110 /*****************************************************************************
111  * vpar_DecodePicture : put a picture in the video fifo, if it is decodable
112  *****************************************************************************/
113 void vpar_DecodePicture( video_fifo_t * p_fifo, undec_picture_t * p_undec_p )
114 {
115     boolean_t           b_decodable;
116     
117     switch( p_undec_p )
118     {
119     case B_CODING_TYPE:
120         b_decodable = ((p_undec_p->p_backward_p != NULL) &&
121                        (p_undec_p->p_forward_p != NULL));
122         break;
123     case P_CODING_TYPE:
124         b_decodable = (p_undec_p->p_backward_p != NULL);
125         break;
126     case I_CODING_TYPE:
127     case D_CODING_TYPE:
128         b_decodable = TRUE;
129         break;
130     default:
131         /* That should not happen */
132     }
133
134     if( b_decodable )
135     {
136         /* Place picture in the video FIFO */
137         vlc_mutex_lock( &p_fifo->lock );
138         
139         /* By construction, the video FIFO cannot be full */
140         VIDEO_FIFO_END( *p_fifo ) = p_undec_p;
141         VIDEO_FIFO_INCEND( *p_fifo );
142         
143         vlc_mutex_unlock( &p_fifo->lock );
144     }
145 }
146
147 /*****************************************************************************
148  * vpar_ReleasePicture : put a picture in the video_output fifo, and update
149  *                  links and buffers
150  *****************************************************************************/
151 void vpar_ReleasePicture( video_fifo_t * p_fifo, undec_picture_t * p_undec_p )
152 {
153     int         i_ref;
154     
155     /* Tell referencing pictures so that they can be decoded */
156     for( i_ref = 0; p_undec_p->pp_referencing_undec[i_ref].p_undec != NULL
157                     && i_ref < MAX_REFERENCING_UNDEC; i_ref++ )
158     {
159         *p_undec_p->pp_referencing_undec[i_ref].pp_frame = p_undec_p->p_picture;
160         vout_LinkPicture( p_fifo->p_vpar.p_vout, p_picture );
161         
162         /* Try to put the referencing picture in the video FIFO */
163         vpar_DecodePicture( p_fifo, p_undec_p->pp_referencing_undec[i_ref].p_undec );
164     }
165
166     /* Unlink referenced pictures */
167     if( p_undec_p->p_forward_ref != NULL )
168     {
169         vout_UnlinkPicture( p_fifo->p_vpar.p_vout, p_undec_p->p_forward_ref );
170         if( p_undec_p->p_backward_ref != NULL )
171         {
172             vout_UnlinkPicture( p_fifo->p_vpar.p_vout, p_undec_p->p_backward_ref );
173         }
174     }
175     
176     /* Mark the picture to be displayed */
177     vout_DisplayPicture( p_fifo->p_vpar.p_vout, p_undec_p->p_picture );
178
179     /* Release the undec_picture_t structure */
180 #define P_buffer p_fifo->p_vpar.vbuffer
181     vlc_mutex_lock( &P_buffer->lock );
182
183     P_buffer->pp_undec_free[ ++P_buffer->i_index ] = p_undec_p;
184     
185     vlc_mutex_unlock( &P_buffer->lock );
186 #undef P_buffer
187     }
188 }
189
190 /*****************************************************************************
191  * vpar_DestroyPicture : destroy a picture in case of error
192  *****************************************************************************/
193 void vpar_DestroyPicture( video_fifo_t * p_fifo, undec_picture_t * p_undec_p )
194 {
195     int         i_ref;
196     
197     /* Destroy referencing pictures */
198     for( i_ref = 0; p_undec_p->pp_referencing_undec[i_ref].p_undec != NULL
199                     && i_ref < MAX_REFERENCING_UNDEC; i_ref++ )
200     {
201         /* Try to put the referencing picture in the video FIFO */
202         vpar_DestroyPicture( p_fifo, p_undec_p->pp_referencing_undec[i_ref].p_undec );
203     }
204
205     /* Unlink referenced pictures */
206     if( p_undec_p->p_forward_ref != NULL )
207     {
208         vout_UnlinkPicture( p_fifo->p_vpar.p_vout, p_undec_p->p_forward_ref );
209         if( p_undec_p->p_backward_ref != NULL )
210         {
211             vout_UnlinkPicture( p_fifo->p_vpar.p_vout, p_undec_p->p_backward_ref );
212         }
213     }
214     
215     /* Release the picture buffer */
216     vout_DestroyPicture( p_fifo->p_vpar.p_vout, p_undec_p->p_picture );
217     
218     /* Release the undec_picture_t structure */
219 #define P_buffer p_fifo->p_vpar.vbuffer
220     vlc_mutex_lock( &P_buffer->lock );
221
222     P_buffer->pp_undec_free[ ++P_buffer->i_index ] = p_undec_p;
223     
224     vlc_mutex_unlock( &P_buffer->lock );
225 #undef P_buffer
226     }
227 }