]> git.sesse.net Git - vlc/blob - src/misc/decoder_fifo.c
Encore un commit venu tout droit des abysses de l'enfer, d�sol� pour
[vlc] / src / misc / decoder_fifo.c
1 /*****************************************************************************
2  * decoder_fifo.c: auxiliaries functions used in decoder_fifo.h
3  *****************************************************************************
4  * Copyright (C) 1998, 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 GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public
19  * License along with this program; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  *****************************************************************************/
23
24 #include <sys/types.h>                        /* on BSD, uio.h needs types.h */
25 #include <sys/uio.h>                                          /* for input.h */
26
27 #include "config.h"
28 #include "common.h"
29 #include "mtime.h"
30 #include "threads.h"
31
32 #include "debug.h"                 /* XXX?? temporaire, requis par netlist.h */
33
34 #include "input.h"
35 #include "input_netlist.h"
36 #include "decoder_fifo.h"
37
38 void decoder_fifo_next( bit_stream_t * p_bit_stream )
39 {
40     /* We are looking for the next TS packet that contains real data,
41      * and not just a PES header */
42     do
43     {
44         /* We were reading the last TS packet of this PES packet... It's
45          * time to jump to the next PES packet */
46         if ( p_bit_stream->p_ts->p_next_ts == NULL )
47         {
48             /* We are going to read/write the start and end indexes of the
49              * decoder fifo and to use the fifo's conditional variable,
50              * that's why we need to take the lock before */
51             vlc_mutex_lock( &p_bit_stream->p_decoder_fifo->data_lock );
52
53             /* Is the input thread dying ? */
54             if ( p_bit_stream->p_input->b_die )
55             {
56                 vlc_mutex_unlock( &(p_bit_stream->p_decoder_fifo->data_lock) );
57                 return;
58             }
59
60             /* We should increase the start index of the decoder fifo, but
61              * if we do this now, the input thread could overwrite the
62              * pointer to the current PES packet, and we weren't able to
63              * give it back to the netlist. That's why we free the PES
64              * packet first. */
65             input_NetlistFreePES( p_bit_stream->p_input, DECODER_FIFO_START(*p_bit_stream->p_decoder_fifo) );
66             DECODER_FIFO_INCSTART( *p_bit_stream->p_decoder_fifo );
67
68             while ( DECODER_FIFO_ISEMPTY(*p_bit_stream->p_decoder_fifo) )
69             {
70                 vlc_cond_wait( &p_bit_stream->p_decoder_fifo->data_wait, &p_bit_stream->p_decoder_fifo->data_lock );
71                 if ( p_bit_stream->p_input->b_die )
72                 {
73                     vlc_mutex_unlock( &(p_bit_stream->p_decoder_fifo->data_lock) );
74                     return;
75                 }
76             }
77
78             /* The next byte could be found in the next PES packet */
79             p_bit_stream->p_ts = DECODER_FIFO_START( *p_bit_stream->p_decoder_fifo )->p_first_ts;
80
81             /* We can release the fifo's data lock */
82             vlc_mutex_unlock( &p_bit_stream->p_decoder_fifo->data_lock );
83         }
84         /* Perhaps the next TS packet of the current PES packet contains
85          * real data (ie its payload's size is greater than 0) */
86         else
87         {
88             p_bit_stream->p_ts = p_bit_stream->p_ts->p_next_ts;
89         }
90     } while ( p_bit_stream->p_ts->i_payload_start == p_bit_stream->p_ts->i_payload_end );
91
92     /* We've found a TS packet which contains interesting data... */
93     p_bit_stream->p_byte = p_bit_stream->p_ts->buffer + p_bit_stream->p_ts->i_payload_start;
94     p_bit_stream->p_end = p_bit_stream->p_ts->buffer + p_bit_stream->p_ts->i_payload_end;
95 }