root/src/input/decoder.c

Revision 4b6c78a5b9ba1f5905f3c55c7838f5d4a540bc5c, 53.7 kB (checked in by Rémi Denis-Courmont <rdenis@simphalempin.com>, 2 months ago)

typo

  • Property mode set to 100644
Line 
1 /*****************************************************************************
2  * decoder.c: Functions for the management of decoders
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *          Laurent Aimar <fenrir@via.ecp.fr>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32 #include <assert.h>
33
34 #include <vlc_common.h>
35
36 #include <vlc_block.h>
37 #include <vlc_vout.h>
38 #include <vlc_aout.h>
39 #include <vlc_sout.h>
40 #include <vlc_codec.h>
41 #include <vlc_osd.h>
42
43 #include <vlc_interface.h>
44 #include "audio_output/aout_internal.h"
45 #include "stream_output/stream_output.h"
46 #include "input_internal.h"
47 #include "input_clock.h"
48 #include "input_decoder.h"
49
50 #include "../video_output/vout_internal.h"
51
52 static decoder_t *CreateDecoder( input_thread_t *, es_format_t *, int, sout_instance_t *p_sout );
53 static void       DeleteDecoder( decoder_t * );
54
55 static void      *DecoderThread( vlc_object_t * );
56 static int        DecoderDecode( decoder_t * p_dec, block_t *p_block );
57 static void       DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date );
58
59 /* Buffers allocation callbacks for the decoders */
60 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
61 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
62
63 static picture_t *vout_new_buffer( decoder_t * );
64 static void vout_del_buffer( decoder_t *, picture_t * );
65 static void vout_link_picture( decoder_t *, picture_t * );
66 static void vout_unlink_picture( decoder_t *, picture_t * );
67
68 static subpicture_t *spu_new_buffer( decoder_t * );
69 static void spu_del_buffer( decoder_t *, subpicture_t * );
70
71 static es_format_t null_es_format;
72
73 struct decoder_owner_sys_t
74 {
75     bool      b_own_thread;
76
77     int64_t         i_preroll_end;
78
79     input_thread_t  *p_input;
80     input_clock_t   *p_clock;
81
82     vout_thread_t   *p_spu_vout;
83     int              i_spu_channel;
84     int64_t          i_spu_order;
85
86     sout_instance_t         *p_sout;
87     sout_packetizer_input_t *p_sout_input;
88
89     /* Some decoders require already packetized data (ie. not truncated) */
90     decoder_t *p_packetizer;
91
92     /* Current format in use by the output */
93     video_format_t video;
94     audio_format_t audio;
95     es_format_t    sout;
96
97     /* fifo */
98     block_fifo_t *p_fifo;
99
100     /* Lock for communication with decoder thread */
101     vlc_mutex_t lock;
102     vlc_cond_t  wait;
103
104     /* -- These variables need locking on write(only) -- */
105     aout_instance_t *p_aout;
106     aout_input_t    *p_aout_input;
107
108     vout_thread_t   *p_vout;
109
110     /* -- Theses variables need locking on read *and* write -- */
111     /* Pause */
112     bool b_paused;
113     mtime_t i_pause_date;
114     /* CC */
115     bool b_cc_supported;
116     bool pb_cc_present[4];
117     decoder_t *pp_cc[4];
118 };
119
120 /* */
121 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
122 {
123     msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
124              "VLC probably does not support this sound or video format.",
125              (char*)&codec );
126     intf_UserFatal( p_dec, false, _("No suitable decoder module"),
127                     _("VLC does not support the audio or video format \"%4.4s\". "
128                       "Unfortunately there is no way for you to fix this."), (char*)&codec );
129 }
130
131 /* decoder_GetInputAttachment:
132  */
133 input_attachment_t *decoder_GetInputAttachment( decoder_t *p_dec,
134                                                 const char *psz_name )
135 {
136     input_attachment_t *p_attachment;
137     if( input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENT, &p_attachment, psz_name ) )
138         return NULL;
139     return p_attachment;
140 }
141 /* decoder_GetInputAttachments:
142  */
143 int decoder_GetInputAttachments( decoder_t *p_dec,
144                                  input_attachment_t ***ppp_attachment,
145                                  int *pi_attachment )
146 {
147     return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
148                           ppp_attachment, pi_attachment );
149 }
150 /* decoder_GetDisplayDate:
151  */
152 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
153 {
154     decoder_owner_sys_t *p_owner = p_dec->p_owner;
155
156     assert( p_owner->p_clock );
157     return input_clock_GetTS( p_owner->p_clock, p_owner->p_input->i_pts_delay, i_ts );
158 }
159 /* decoder_GetDisplayRate:
160  */
161 int decoder_GetDisplayRate( decoder_t *p_dec )
162 {
163     decoder_owner_sys_t *p_owner = p_dec->p_owner;
164
165     assert( p_owner->p_clock );
166     return input_clock_GetRate( p_owner->p_clock );
167 }
168
169 /**
170  * Spawns a new decoder thread
171  *
172  * \param p_input the input thread
173  * \param p_es the es descriptor
174  * \return the spawned decoder object
175  */
176 decoder_t *input_DecoderNew( input_thread_t *p_input,
177                              es_format_t *fmt, input_clock_t *p_clock, sout_instance_t *p_sout  )
178 {
179     decoder_t   *p_dec = NULL;
180     vlc_value_t val;
181
182 #ifndef ENABLE_SOUT
183     (void)b_force_decoder;
184 #else
185     /* If we are in sout mode, search for packetizer module */
186     if( p_sout )
187     {
188         /* Create the decoder configuration structure */
189         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER, p_sout );
190         if( p_dec == NULL )
191         {
192             msg_Err( p_input, "could not create packetizer" );
193             intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
194                             _("VLC could not open the packetizer module.") );
195             return NULL;
196         }
197     }
198     else
199 #endif
200     {
201         /* Create the decoder configuration structure */
202         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER, p_sout );
203         if( p_dec == NULL )
204         {
205             msg_Err( p_input, "could not create decoder" );
206             intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
207                             _("VLC could not open the decoder module.") );
208             return NULL;
209         }
210     }
211
212     if( !p_dec->p_module )
213     {
214         DecoderUnsupportedCodec( p_dec, fmt->i_codec );
215
216         DeleteDecoder( p_dec );
217         vlc_object_release( p_dec );
218         return NULL;
219     }
220
221     p_dec->p_owner->p_clock = p_clock;
222
223     if( p_sout && p_sout == p_input->p->p_sout && p_input->p->input.b_can_pace_control )
224     {
225         msg_Dbg( p_input, "stream out mode -> no decoder thread" );
226         p_dec->p_owner->b_own_thread = false;
227     }
228     else
229     {
230         var_Get( p_input, "minimize-threads", &val );
231         p_dec->p_owner->b_own_thread = !val.b_bool;
232     }
233
234     if( p_dec->p_owner->b_own_thread )
235     {
236         int i_priority;
237         if( fmt->i_cat == AUDIO_ES )
238             i_priority = VLC_THREAD_PRIORITY_AUDIO;
239         else
240             i_priority = VLC_THREAD_PRIORITY_VIDEO;
241
242         /* Spawn the decoder thread */
243         if( vlc_thread_create( p_dec, "decoder", DecoderThread,
244                                i_priority, false ) )
245         {
246             msg_Err( p_dec, "cannot spawn decoder thread" );
247             module_unneed( p_dec, p_dec->p_module );
248             DeleteDecoder( p_dec );
249             vlc_object_release( p_dec );
250             return NULL;
251         }
252     }
253
254     return p_dec;
255 }
256
257 /**
258  * Kills a decoder thread and waits until it's finished
259  *
260  * \param p_input the input thread
261  * \param p_es the es descriptor
262  * \return nothing
263  */
264 void input_DecoderDelete( decoder_t *p_dec )
265 {
266     decoder_owner_sys_t *p_owner = p_dec->p_owner;
267
268     vlc_object_kill( p_dec );
269
270     if( p_owner->b_own_thread )
271     {
272         /* Make sure we aren't paused anymore */
273         vlc_mutex_lock( &p_owner->lock );
274         if( p_owner->b_paused )
275         {
276             p_owner->b_paused = false;
277             vlc_cond_signal( &p_owner->wait );
278         }
279         vlc_mutex_unlock( &p_owner->lock );
280
281         /* Make sure the thread leaves the function by
282          * sending it an empty block. */
283         block_t *p_block = block_New( p_dec, 0 );
284         input_DecoderDecode( p_dec, p_block );
285
286         vlc_thread_join( p_dec );
287
288         /* Don't module_unneed() here because of the dll loader that wants
289          * close() in the same thread than open()/decode() */
290     }
291     else
292     {
293         /* Flush */
294         input_DecoderDecode( p_dec, NULL );
295
296         module_unneed( p_dec, p_dec->p_module );
297     }
298
299     /* */
300     if( p_dec->p_owner->b_cc_supported )
301     {
302         int i;
303         for( i = 0; i < 4; i++ )
304             input_DecoderSetCcState( p_dec, false, i );
305     }
306
307     /* Delete decoder configuration */
308     DeleteDecoder( p_dec );
309
310     /* Delete the decoder */
311     vlc_object_release( p_dec );
312 }
313
314 /**
315  * Put a block_t in the decoder's fifo.
316  *
317  * \param p_dec the decoder object
318  * \param p_block the data block
319  */
320 void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
321 {
322     decoder_owner_sys_t *p_owner = p_dec->p_owner;
323
324     if( p_owner->b_own_thread )
325     {
326         if( p_owner->p_input->p->b_out_pace_control )
327         {
328             /* FIXME !!!!! */
329             while( !p_dec->b_die && !p_dec->b_error &&
330                    block_FifoCount( p_owner->p_fifo ) > 10 )
331             {
332                 msleep( 1000 );
333             }
334         }
335         else if( block_FifoSize( p_owner->p_fifo ) > 50000000 /* 50 MB */ )
336         {
337             /* FIXME: ideally we would check the time amount of data
338              * in the fifo instead of its size. */
339             msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
340                       "consumed quickly enough), resetting fifo!" );
341             block_FifoEmpty( p_owner->p_fifo );
342         }
343
344         block_FifoPut( p_owner->p_fifo, p_block );
345     }
346     else
347     {
348         if( p_dec->b_error || ( p_block && p_block->i_buffer <= 0 ) )
349         {
350             if( p_block )
351                 block_Release( p_block );
352         }
353         else
354         {
355             DecoderDecode( p_dec, p_block );
356         }
357     }
358 }
359
360 void input_DecoderDiscontinuity( decoder_t * p_dec, bool b_flush )
361 {
362     decoder_owner_sys_t *p_owner = p_dec->p_owner;
363     block_t *p_null;
364
365     /* Empty the fifo */
366     if( p_owner->b_own_thread && b_flush )
367         block_FifoEmpty( p_owner->p_fifo );
368
369     /* Send a special block */
370     p_null = block_New( p_dec, 128 );
371     p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
372     if( b_flush && p_dec->fmt_in.i_cat == SPU_ES )
373         p_null->i_flags |= BLOCK_FLAG_CORE_FLUSH;
374     /* FIXME check for p_packetizer or b_packitized from es_format_t of input ? */
375     if( p_owner->p_packetizer && b_flush )
376         p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
377     memset( p_null->p_buffer, 0, p_null->i_buffer );
378
379     input_DecoderDecode( p_dec, p_null );
380 }
381
382 bool input_DecoderEmpty( decoder_t * p_dec )
383 {
384     if( p_dec->p_owner->b_own_thread &&
385         block_FifoCount( p_dec->p_owner->p_fifo ) > 0 )
386     {
387         return false;
388     }
389     return true;
390 }
391
392 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
393 {
394     decoder_owner_sys_t *p_owner = p_dec->p_owner;
395     int i;
396
397     vlc_mutex_lock( &p_owner->lock );
398     for( i = 0; i < 4; i++ )
399         pb_present[i] =  p_owner->pb_cc_present[i];
400     vlc_mutex_unlock( &p_owner->lock );
401 }
402 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
403 {
404     decoder_owner_sys_t *p_owner = p_dec->p_owner;
405
406     //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
407
408     if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
409         return VLC_EGENERIC;
410
411     if( b_decode )
412     {
413         static const vlc_fourcc_t fcc[4] = {
414             VLC_FOURCC('c', 'c', '1', ' '),
415             VLC_FOURCC('c', 'c', '2', ' '),
416             VLC_FOURCC('c', 'c', '3', ' '),
417             VLC_FOURCC('c', 'c', '4', ' '),
418         };
419         decoder_t *p_cc;
420         es_format_t fmt;
421
422         es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
423         p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER, p_owner->p_sout );
424         if( !p_cc )
425         {
426             msg_Err( p_dec, "could not create decoder" );
427             intf_UserFatal( p_dec, false, _("Streaming / Transcoding failed"),
428                             _("VLC could not open the decoder module.") );
429             return VLC_EGENERIC;
430         }
431         else if( !p_cc->p_module )
432         {
433             DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
434             DeleteDecoder( p_cc );
435             vlc_object_release( p_cc );
436             return VLC_EGENERIC;
437         }
438         p_cc->p_owner->p_clock = p_owner->p_clock;
439
440         vlc_mutex_lock( &p_owner->lock );
441         p_owner->pp_cc[i_channel] = p_cc;
442         vlc_mutex_unlock( &p_owner->lock );
443     }
444     else
445     {
446         decoder_t *p_cc;
447
448         vlc_mutex_lock( &p_owner->lock );
449         p_cc = p_owner->pp_cc[i_channel];
450         p_owner->pp_cc[i_channel] = NULL;
451         vlc_mutex_unlock( &p_owner->lock );
452
453         if( p_cc )
454         {
455             vlc_object_kill( p_cc );
456             module_unneed( p_cc, p_cc->p_module );
457             DeleteDecoder( p_cc );
458             vlc_object_release( p_cc );
459         }
460     }
461     return VLC_SUCCESS;
462 }
463 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
464 {
465     decoder_owner_sys_t *p_owner = p_dec->p_owner;
466
467     *pb_decode = false;
468     if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
469         return VLC_EGENERIC;
470
471     vlc_mutex_lock( &p_owner->lock );
472     *pb_decode = p_owner->pp_cc[i_channel] != NULL;
473     vlc_mutex_unlock( &p_owner->lock );
474     return VLC_EGENERIC;
475 }
476
477 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
478 {
479     decoder_owner_sys_t *p_owner = p_dec->p_owner;
480
481     vlc_mutex_lock( &p_owner->lock );
482
483     assert( (!p_owner->b_paused) != (!b_paused) );
484     p_owner->b_paused = b_paused;
485     p_owner->i_pause_date = i_date;
486     if( p_owner->b_own_thread )
487         vlc_cond_signal( &p_owner->wait );
488
489     DecoderOutputChangePause( p_dec, b_paused, i_date );
490     vlc_mutex_unlock( &p_owner->lock );
491 }
492 /**
493  * Create a decoder object
494  *
495  * \param p_input the input thread
496  * \param p_es the es descriptor
497  * \param i_object_type Object type as define in include/vlc_objects.h
498  * \return the decoder object
499  */
500 static decoder_t * CreateDecoder( input_thread_t *p_input,
501                                   es_format_t *fmt, int i_object_type, sout_instance_t *p_sout )
502 {
503     decoder_t *p_dec;
504     decoder_owner_sys_t *p_owner;
505     int i;
506
507     p_dec = vlc_object_create( p_input, i_object_type );
508     if( p_dec == NULL )
509         return NULL;
510
511     p_dec->pf_decode_audio = NULL;
512     p_dec->pf_decode_video = NULL;
513     p_dec->pf_decode_sub = NULL;
514     p_dec->pf_get_cc = NULL;
515     p_dec->pf_packetize = NULL;
516
517     /* Initialize the decoder fifo */
518     p_dec->p_module = NULL;
519
520     memset( &null_es_format, 0, sizeof(es_format_t) );
521     es_format_Copy( &p_dec->fmt_in, fmt );
522     es_format_Copy( &p_dec->fmt_out, &null_es_format );
523
524     /* Allocate our private structure for the decoder */
525     p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
526     if( p_dec->p_owner == NULL )
527     {
528         vlc_object_release( p_dec );
529         return NULL;
530     }
531     p_dec->p_owner->b_own_thread = true;
532     p_dec->p_owner->i_preroll_end = -1;
533     p_dec->p_owner->p_input = p_input;
534     p_dec->p_owner->p_aout = NULL;
535     p_dec->p_owner->p_aout_input = NULL;
536     p_dec->p_owner->p_vout = NULL;
537     p_dec->p_owner->p_spu_vout = NULL;
538     p_dec->p_owner->i_spu_channel = 0;
539     p_dec->p_owner->i_spu_order = 0;
540     p_dec->p_owner->p_sout = p_sout;
541     p_dec->p_owner->p_sout_input = NULL;
542     p_dec->p_owner->p_packetizer = NULL;
543
544     /* decoder fifo */
545     if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
546     {
547         free( p_dec->p_owner );
548         vlc_object_release( p_dec );
549         return NULL;
550     }
551
552     /* Set buffers allocation callbacks for the decoders */
553     p_dec->pf_aout_buffer_new = aout_new_buffer;
554     p_dec->pf_aout_buffer_del = aout_del_buffer;
555     p_dec->pf_vout_buffer_new = vout_new_buffer;
556     p_dec->pf_vout_buffer_del = vout_del_buffer;
557     p_dec->pf_picture_link    = vout_link_picture;
558     p_dec->pf_picture_unlink  = vout_unlink_picture;
559     p_dec->pf_spu_buffer_new  = spu_new_buffer;
560     p_dec->pf_spu_buffer_del  = spu_del_buffer;
561
562     vlc_object_attach( p_dec, p_input );
563
564     /* Find a suitable decoder/packetizer module */
565     if( i_object_type == VLC_OBJECT_DECODER )
566         p_dec->p_module = module_need( p_dec, "decoder", "$codec", 0 );
567     else
568         p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", 0 );
569
570     /* Check if decoder requires already packetized data */
571     if( i_object_type == VLC_OBJECT_DECODER &&
572         p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
573     {
574         p_dec->p_owner->p_packetizer =
575             vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
576         if( p_dec->p_owner->p_packetizer )
577         {
578             es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
579                             &p_dec->fmt_in );
580
581             es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
582                             &null_es_format );
583
584             vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
585
586             p_dec->p_owner->p_packetizer->p_module =
587                 module_need( p_dec->p_owner->p_packetizer,
588                              "packetizer", "$packetizer", 0 );
589
590             if( !p_dec->p_owner->p_packetizer->p_module )
591             {
592                 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
593                 vlc_object_detach( p_dec->p_owner->p_packetizer );
594                 vlc_object_release( p_dec->p_owner->p_packetizer );
595             }
596         }
597     }
598
599     /* Copy ourself the input replay gain */
600     if( fmt->i_cat == AUDIO_ES )
601     {
602         for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
603         {
604             if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
605             {
606                 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
607                 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
608             }
609             if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
610             {
611                 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
612                 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
613             }
614         }
615     }
616     /* */
617     p_owner->b_cc_supported = false;
618     if( i_object_type == VLC_OBJECT_DECODER )
619     {
620         if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
621             p_owner->b_cc_supported = true;
622         if( p_dec->pf_get_cc )
623             p_owner->b_cc_supported = true;
624     }
625
626     vlc_mutex_init( &p_owner->lock );
627     vlc_cond_init( &p_owner->wait );
628     p_owner->b_paused = false;
629     p_owner->i_pause_date = 0;
630     for( i = 0; i < 4; i++ )
631     {
632         p_owner->pb_cc_present[i] = false;
633         p_owner->pp_cc[i] = NULL;
634     }
635     return p_dec;
636 }
637
638 /**
639  * The decoding main loop
640  *
641  * \param p_dec the decoder
642  */
643 static void* DecoderThread( vlc_object_t *p_this )
644 {
645     decoder_t *p_dec = (decoder_t *)p_this;
646     decoder_owner_sys_t *p_owner = p_dec->p_owner;
647
648     block_t *p_block;
649     int canc = vlc_savecancel();
650
651     /* The decoder's main loop */
652     while( vlc_object_alive( p_dec ) && !p_dec->b_error )
653     {
654         if( ( p_block = block_FifoGet( p_owner->p_fifo ) ) == NULL )
655         {
656             p_dec->b_error = 1;
657             break;
658         }
659
660         if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
661             break;
662     }
663
664     while( vlc_object_alive( p_dec ) )
665     {
666         /* Trash all received PES packets */
667         p_block = block_FifoGet( p_owner->p_fifo );
668         if( p_block )
669             block_Release( p_block );
670     }
671
672     /* We do it here because of the dll loader that wants close() in the
673      * same thread than open()/decode() */
674     module_unneed( p_dec, p_dec->p_module );
675     vlc_restorecancel( canc );
676     return NULL;
677 }
678
679 static void DecoderWaitUnpause( decoder_t *p_dec )
680 {
681     decoder_owner_sys_t *p_owner = p_dec->p_owner;
682
683     vlc_mutex_lock( &p_owner->lock );
684
685     while( p_owner->b_paused )
686         vlc_cond_wait( &p_owner->wait, &p_owner->lock );
687
688     vlc_mutex_unlock( &p_owner->lock );
689 }
690
691 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
692 {
693     decoder_owner_sys_t *p_owner = p_dec->p_owner;
694
695     if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
696         return;
697
698     if( p_dec->fmt_in.i_cat == AUDIO_ES )
699     {
700         // TODO
701         //if( p_own->p_vout )
702         //    aout_ChangePause( p_own->p_aout, p_own->p_aout_input, b_paused, i_date );
703     }
704     else if( p_dec->fmt_in.i_cat == VIDEO_ES )
705     {
706         if( p_owner->p_vout )
707             vout_ChangePause( p_owner->p_vout, b_paused, i_date );
708     }
709     else if( p_dec->fmt_in.i_cat == SPU_ES )
710     {
711         /* XXX is it needed, maybe the vout should simply pause it */
712     }
713 }
714 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
715 {
716     if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
717         *pi_preroll = INT64_MAX;
718     else if( p->i_pts > 0 )
719         *pi_preroll = __MIN( *pi_preroll, p->i_pts );
720     else if( p->i_dts > 0 )
721         *pi_preroll = __MIN( *pi_preroll, p->i_dts );
722 }
723
724 static mtime_t DecoderTeletextFixTs( mtime_t i_ts, mtime_t i_ts_delay )
725 {
726     mtime_t current_date = mdate();
727
728     /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
729     if( !i_ts || i_ts > current_date + 10000000 || i_ts < current_date )
730     {
731         /* ETSI EN 300 472 Annex A : do not take into account the PTS
732          * for teletext streams. */
733         return current_date + 400000 + i_ts_delay;
734     }
735     return i_ts;
736 }
737
738 static void DecoderSoutBufferFixTs( block_t *p_block,
739                                     input_clock_t *p_clock, mtime_t i_ts_delay,
740                                     bool b_teletext )
741 {
742     assert( p_clock );
743
744     p_block->i_rate = input_clock_GetRate( p_clock );
745
746     if( p_block->i_dts > 0 )
747         p_block->i_dts = input_clock_GetTS( p_clock, i_ts_delay, p_block->i_dts );
748
749     if( p_block->i_pts > 0 )
750         p_block->i_pts = input_clock_GetTS( p_clock, i_ts_delay, p_block->i_pts );
751
752     if( p_block->i_length > 0 )
753         p_block->i_length = ( p_block->i_length * p_block->i_rate +
754                                 INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
755
756     if( b_teletext )
757         p_block->i_pts = DecoderTeletextFixTs( p_block->i_pts, i_ts_delay );
758 }
759 static void DecoderAoutBufferFixTs( aout_buffer_t *p_buffer,
760                                     input_clock_t *p_clock, mtime_t i_ts_delay )
761 {
762     /* sout display module does not set clock */
763     if( !p_clock )
764         return;
765
766     if( p_buffer->start_date )
767         p_buffer->start_date = input_clock_GetTS( p_clock, i_ts_delay, p_buffer->start_date );
768
769     if( p_buffer->end_date )
770         p_buffer->end_date = input_clock_GetTS( p_clock, i_ts_delay, p_buffer->end_date );
771 }
772 static void DecoderVoutBufferFixTs( picture_t *p_picture,
773                                     input_clock_t *p_clock, mtime_t i_ts_delay )
774 {
775     /* sout display module does not set clock */
776     if( !p_clock )
777         return;
778
779     if( p_picture->date )
780         p_picture->date = input_clock_GetTS( p_clock, i_ts_delay, p_picture->date );
781 }
782 static void DecoderSpuBufferFixTs( subpicture_t *p_subpic,
783                                    input_clock_t *p_clock, mtime_t i_ts_delay,
784                                    bool b_teletext )
785 {
786     bool b_ephemere = p_subpic->i_start == p_subpic->i_stop;
787
788     /* sout display module does not set clock */
789     if( !p_clock )
790         return;
791
792     if( p_subpic->i_start )
793         p_subpic->i_start = input_clock_GetTS( p_clock, i_ts_delay, p_subpic->i_start );
794
795     if( p_subpic->i_stop )
796         p_subpic->i_stop = input_clock_GetTS( p_clock, i_ts_delay, p_subpic->i_stop );
797
798     /* Do not create ephemere picture because of rounding errors */
799     if( !b_ephemere && p_subpic->i_start == p_subpic->i_stop )
800         p_subpic->i_stop++;
801
802     if( b_teletext )
803         p_subpic->i_start = DecoderTeletextFixTs( p_subpic->i_start, i_ts_delay );
804 }
805
806 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
807 {
808     decoder_owner_sys_t *p_owner = p_dec->p_owner;
809     input_thread_t  *p_input = p_owner->p_input;
810     input_clock_t   *p_clock = p_owner->p_clock;
811     aout_buffer_t   *p_aout_buf;
812
813     while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
814     {
815         aout_instance_t *p_aout = p_owner->p_aout;
816         aout_input_t    *p_aout_input = p_owner->p_aout_input;
817
818         if( p_dec->b_die )
819         {
820             /* It prevent freezing VLC in case of broken decoder */
821             aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
822             if( p_block )
823                 block_Release( p_block );
824             break;
825         }
826         vlc_mutex_lock( &p_input->p->counters.counters_lock );
827         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio, 1, NULL );
828         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
829
830         if( p_aout_buf->start_date < p_owner->i_preroll_end )
831         {
832             aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
833             continue;
834         }
835
836         if( p_owner->i_preroll_end > 0 )
837         {
838             /* FIXME TODO flush audio output (don't know how to do that) */
839             msg_Dbg( p_dec, "End of audio preroll" );
840             p_owner->i_preroll_end = -1;
841         }
842
843         DecoderWaitUnpause( p_dec );
844
845         const int i_rate = p_clock ? input_clock_GetRate( p_clock ) : p_block->i_rate;
846
847         DecoderAoutBufferFixTs( p_aout_buf, p_clock, p_input->i_pts_delay );
848         if( i_rate >= INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE &&
849             i_rate <= INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
850             aout_DecPlay( p_aout, p_aout_input, p_aout_buf, i_rate );
851         else
852             aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
853     }
854 }
855 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
856 {
857     decoder_owner_sys_t *p_owner = p_dec->p_owner;
858     block_t *p_cc;
859     bool pb_present[4];
860     int i;
861     int i_cc_decoder;
862
863     assert( p_dec_cc->pf_get_cc != NULL );
864
865     /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
866     if( !p_owner->b_cc_supported )
867         return;
868
869     p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
870     if( !p_cc )
871         return;
872
873     vlc_mutex_lock( &p_owner->lock );
874     for( i = 0, i_cc_decoder = 0; i < 4; i++ )
875     {
876         p_owner->pb_cc_present[i] |= pb_present[i];
877         if( p_owner->pp_cc[i] )
878             i_cc_decoder++;
879     }
880
881     for( i = 0; i < 4; i++ )
882     {
883         if( !p_owner->pp_cc[i] )
884             continue;
885
886         if( i_cc_decoder > 1 )
887             DecoderDecode( p_owner->pp_cc[i], block_Duplicate( p_cc ) );
888         else
889             DecoderDecode( p_owner->pp_cc[i], p_cc );
890         i_cc_decoder--;
891     }
892     vlc_mutex_unlock( &p_owner->lock );
893 }
894 static void VoutDisplayedPicture( vout_thread_t *p_vout, picture_t *p_pic )
895 {
896     vlc_mutex_lock( &p_vout->picture_lock );
897
898     if( p_pic->i_status == READY_PICTURE )
899     {
900         /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
901         p_pic->date = 1;
902     }
903     else if( p_pic->i_refcount > 0 )
904     {
905         p_pic->i_status = DISPLAYED_PICTURE;
906     }
907     else
908     {
909         p_pic->i_status = DESTROYED_PICTURE;
910         picture_CleanupQuant( p_pic );
911         p_vout->i_heap_size--;
912     }
913
914     vlc_mutex_unlock( &p_vout->picture_lock );
915 }
916 static void VoutFlushPicture( vout_thread_t *p_vout )
917 {
918     int i;
919     vlc_mutex_lock( &p_vout->picture_lock );
920     for( i = 0; i < p_vout->render.i_pictures; i++ )
921     {
922         picture_t *p_pic = p_vout->render.pp_picture[i];
923
924         if( p_pic->i_status == READY_PICTURE ||
925             p_pic->i_status == DISPLAYED_PICTURE )
926         {
927             /* We cannot change picture status if it is in READY_PICTURE state,
928              * Just make sure they won't be displayed */
929             p_pic->date = 1;
930         }
931     }
932     vlc_mutex_unlock( &p_vout->picture_lock );
933 }
934
935 #if 0
936 static void DecoderOptimizePtsDelay( decoder_t *p_dec )
937 {
938     input_thread_t *p_input = p_dec->p_owner->p_input;
939     vout_thread_t *p_vout = p_dec->p_owner->p_vout;
940     input_thread_private_t *p_priv = p_input->p;
941
942     picture_t *p_old = NULL;
943     picture_t *p_young = NULL;
944     int i;
945
946     /* Enable with --auto-adjust-pts-delay */
947     if( !p_priv->pts_adjust.b_auto_adjust )
948         return;
949
950     for( i = 0; i < I_RENDERPICTURES; i++ )
951     {
952         picture_t *p_pic = PP_RENDERPICTURE[i];
953
954         if( p_pic->i_status != READY_PICTURE )
955             continue;
956
957         if( !p_old || p_pic->date < p_old->date )
958             p_old = p_pic;
959         if( !p_young || p_pic->date > p_young->date )
960             p_young = p_pic;
961     }
962
963     if( !p_young || !p_old )
964         return;
965
966     /* Try to find if we can reduce the pts
967      * This first draft is way too simple, and we can't say if the
968      * algo will converge. It's also full of constants.
969      * But this simple algo allows to reduce the latency
970      * to the minimum.
971      * The whole point of this, is to bypass the pts_delay set
972      * by the access but also the delay arbitraly set by
973      * the remote server.
974      * Actually the remote server's muxer may set up a
975      * pts<->dts delay in the muxed stream. Th