]> git.sesse.net Git - vlc/blob - src/input/decoder.c
f239b62ceff5a559c68a4ea5a101a2b0198f2610
[vlc] / src / input / decoder.c
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     if( !p_owner->p_clock )
157         return i_ts;
158     return input_clock_GetTS( p_owner->p_clock, p_owner->p_input->i_pts_delay, i_ts );
159 }
160 /* decoder_GetDisplayRate:
161  */
162 int decoder_GetDisplayRate( decoder_t *p_dec )
163 {
164     decoder_owner_sys_t *p_owner = p_dec->p_owner;
165
166     if( !p_owner->p_clock )
167         return INPUT_RATE_DEFAULT;
168     return input_clock_GetRate( p_owner->p_clock );
169 }
170
171 /**
172  * Spawns a new decoder thread
173  *
174  * \param p_input the input thread
175  * \param p_es the es descriptor
176  * \return the spawned decoder object
177  */
178 decoder_t *input_DecoderNew( input_thread_t *p_input,
179                              es_format_t *fmt, input_clock_t *p_clock, sout_instance_t *p_sout  )
180 {
181     decoder_t   *p_dec = NULL;
182     vlc_value_t val;
183
184 #ifndef ENABLE_SOUT
185     (void)b_force_decoder;
186 #else
187     /* If we are in sout mode, search for packetizer module */
188     if( p_sout )
189     {
190         /* Create the decoder configuration structure */
191         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER, p_sout );
192         if( p_dec == NULL )
193         {
194             msg_Err( p_input, "could not create packetizer" );
195             intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
196                             _("VLC could not open the packetizer module.") );
197             return NULL;
198         }
199     }
200     else
201 #endif
202     {
203         /* Create the decoder configuration structure */
204         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER, p_sout );
205         if( p_dec == NULL )
206         {
207             msg_Err( p_input, "could not create decoder" );
208             intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
209                             _("VLC could not open the decoder module.") );
210             return NULL;
211         }
212     }
213
214     if( !p_dec->p_module )
215     {
216         DecoderUnsupportedCodec( p_dec, fmt->i_codec );
217
218         DeleteDecoder( p_dec );
219         vlc_object_release( p_dec );
220         return NULL;
221     }
222
223     p_dec->p_owner->p_clock = p_clock;
224
225     if( p_sout && p_sout == p_input->p->p_sout && p_input->p->input.b_can_pace_control )
226     {
227         msg_Dbg( p_input, "stream out mode -> no decoder thread" );
228         p_dec->p_owner->b_own_thread = false;
229     }
230     else
231     {
232         var_Get( p_input, "minimize-threads", &val );
233         p_dec->p_owner->b_own_thread = !val.b_bool;
234     }
235
236     if( p_dec->p_owner->b_own_thread )
237     {
238         int i_priority;
239         if( fmt->i_cat == AUDIO_ES )
240             i_priority = VLC_THREAD_PRIORITY_AUDIO;
241         else
242             i_priority = VLC_THREAD_PRIORITY_VIDEO;
243
244         /* Spawn the decoder thread */
245         if( vlc_thread_create( p_dec, "decoder", DecoderThread,
246                                i_priority, false ) )
247         {
248             msg_Err( p_dec, "cannot spawn decoder thread" );
249             module_unneed( p_dec, p_dec->p_module );
250             DeleteDecoder( p_dec );
251             vlc_object_release( p_dec );
252             return NULL;
253         }
254     }
255
256     return p_dec;
257 }
258
259 /**
260  * Kills a decoder thread and waits until it's finished
261  *
262  * \param p_input the input thread
263  * \param p_es the es descriptor
264  * \return nothing
265  */
266 void input_DecoderDelete( decoder_t *p_dec )
267 {
268     decoder_owner_sys_t *p_owner = p_dec->p_owner;
269
270     vlc_object_kill( p_dec );
271
272     if( p_owner->b_own_thread )
273     {
274         /* Make sure we aren't paused anymore */
275         vlc_mutex_lock( &p_owner->lock );
276         if( p_owner->b_paused )
277         {
278             p_owner->b_paused = false;
279             vlc_cond_signal( &p_owner->wait );
280         }
281         vlc_mutex_unlock( &p_owner->lock );
282
283         /* Make sure the thread leaves the function by
284          * sending it an empty block. */
285         block_t *p_block = block_New( p_dec, 0 );
286         input_DecoderDecode( p_dec, p_block );
287
288         vlc_thread_join( p_dec );
289
290         /* Don't module_unneed() here because of the dll loader that wants
291          * close() in the same thread than open()/decode() */
292     }
293     else
294     {
295         /* Flush */
296         input_DecoderDecode( p_dec, NULL );
297
298         module_unneed( p_dec, p_dec->p_module );
299     }
300
301     /* */
302     if( p_dec->p_owner->b_cc_supported )
303     {
304         int i;
305         for( i = 0; i < 4; i++ )
306             input_DecoderSetCcState( p_dec, false, i );
307     }
308
309     /* Delete decoder configuration */
310     DeleteDecoder( p_dec );
311
312     /* Delete the decoder */
313     vlc_object_release( p_dec );
314 }
315
316 /**
317  * Put a block_t in the decoder's fifo.
318  *
319  * \param p_dec the decoder object
320  * \param p_block the data block
321  */
322 void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
323 {
324     decoder_owner_sys_t *p_owner = p_dec->p_owner;
325
326     if( p_owner->b_own_thread )
327     {
328         if( p_owner->p_input->p->b_out_pace_control )
329         {
330             /* FIXME !!!!! */
331             while( !p_dec->b_die && !p_dec->b_error &&
332                    block_FifoCount( p_owner->p_fifo ) > 10 )
333             {
334                 msleep( 1000 );
335             }
336         }
337         else if( block_FifoSize( p_owner->p_fifo ) > 50000000 /* 50 MB */ )
338         {
339             /* FIXME: ideally we would check the time amount of data
340              * in the fifo instead of its size. */
341             msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
342                       "consumed quickly enough), resetting fifo!" );
343             block_FifoEmpty( p_owner->p_fifo );
344         }
345
346         block_FifoPut( p_owner->p_fifo, p_block );
347     }
348     else
349     {
350         if( p_dec->b_error || ( p_block && p_block->i_buffer <= 0 ) )
351         {
352             if( p_block )
353                 block_Release( p_block );
354         }
355         else
356         {
357             DecoderDecode( p_dec, p_block );
358         }
359     }
360 }
361
362 void input_DecoderDiscontinuity( decoder_t * p_dec, bool b_flush )
363 {
364     decoder_owner_sys_t *p_owner = p_dec->p_owner;
365     block_t *p_null;
366
367     /* Empty the fifo */
368     if( p_owner->b_own_thread && b_flush )
369         block_FifoEmpty( p_owner->p_fifo );
370
371     /* Send a special block */
372     p_null = block_New( p_dec, 128 );
373     p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
374     if( b_flush && p_dec->fmt_in.i_cat == SPU_ES )
375         p_null->i_flags |= BLOCK_FLAG_CORE_FLUSH;
376     /* FIXME check for p_packetizer or b_packitized from es_format_t of input ? */
377     if( p_owner->p_packetizer && b_flush )
378         p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
379     memset( p_null->p_buffer, 0, p_null->i_buffer );
380
381     input_DecoderDecode( p_dec, p_null );
382 }
383
384 bool input_DecoderEmpty( decoder_t * p_dec )
385 {
386     if( p_dec->p_owner->b_own_thread &&
387         block_FifoCount( p_dec->p_owner->p_fifo ) > 0 )
388     {
389         return false;
390     }
391     return true;
392 }
393
394 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
395 {
396     decoder_owner_sys_t *p_owner = p_dec->p_owner;
397     int i;
398
399     vlc_mutex_lock( &p_owner->lock );
400     for( i = 0; i < 4; i++ )
401         pb_present[i] =  p_owner->pb_cc_present[i];
402     vlc_mutex_unlock( &p_owner->lock );
403 }
404 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
405 {
406     decoder_owner_sys_t *p_owner = p_dec->p_owner;
407
408     //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
409
410     if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
411         return VLC_EGENERIC;
412
413     if( b_decode )
414     {
415         static const vlc_fourcc_t fcc[4] = {
416             VLC_FOURCC('c', 'c', '1', ' '),
417             VLC_FOURCC('c', 'c', '2', ' '),
418             VLC_FOURCC('c', 'c', '3', ' '),
419             VLC_FOURCC('c', 'c', '4', ' '),
420         };
421         decoder_t *p_cc;
422         es_format_t fmt;
423
424         es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
425         p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER, p_owner->p_sout );
426         if( !p_cc )
427         {
428             msg_Err( p_dec, "could not create decoder" );
429             intf_UserFatal( p_dec, false, _("Streaming / Transcoding failed"),
430                             _("VLC could not open the decoder module.") );
431             return VLC_EGENERIC;
432         }
433         else if( !p_cc->p_module )
434         {
435             DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
436             DeleteDecoder( p_cc );
437             vlc_object_release( p_cc );
438             return VLC_EGENERIC;
439         }
440         p_cc->p_owner->p_clock = p_owner->p_clock;
441
442         vlc_mutex_lock( &p_owner->lock );
443         p_owner->pp_cc[i_channel] = p_cc;
444         vlc_mutex_unlock( &p_owner->lock );
445     }
446     else
447     {
448         decoder_t *p_cc;
449
450         vlc_mutex_lock( &p_owner->lock );
451         p_cc = p_owner->pp_cc[i_channel];
452         p_owner->pp_cc[i_channel] = NULL;
453         vlc_mutex_unlock( &p_owner->lock );
454
455         if( p_cc )
456         {
457             vlc_object_kill( p_cc );
458             module_unneed( p_cc, p_cc->p_module );
459             DeleteDecoder( p_cc );
460             vlc_object_release( p_cc );
461         }
462     }
463     return VLC_SUCCESS;
464 }
465 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
466 {
467     decoder_owner_sys_t *p_owner = p_dec->p_owner;
468
469     *pb_decode = false;
470     if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
471         return VLC_EGENERIC;
472
473     vlc_mutex_lock( &p_owner->lock );
474     *pb_decode = p_owner->pp_cc[i_channel] != NULL;
475     vlc_mutex_unlock( &p_owner->lock );
476     return VLC_EGENERIC;
477 }
478
479 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
480 {
481     decoder_owner_sys_t *p_owner = p_dec->p_owner;
482
483     vlc_mutex_lock( &p_owner->lock );
484
485     assert( (!p_owner->b_paused) != (!b_paused) );
486     p_owner->b_paused = b_paused;
487     p_owner->i_pause_date = i_date;
488     if( p_owner->b_own_thread )
489         vlc_cond_signal( &p_owner->wait );
490
491     DecoderOutputChangePause( p_dec, b_paused, i_date );
492     vlc_mutex_unlock( &p_owner->lock );
493 }
494 /**
495  * Create a decoder object
496  *
497  * \param p_input the input thread
498  * \param p_es the es descriptor
499  * \param i_object_type Object type as define in include/vlc_objects.h
500  * \return the decoder object
501  */
502 static decoder_t * CreateDecoder( input_thread_t *p_input,
503                                   es_format_t *fmt, int i_object_type, sout_instance_t *p_sout )
504 {
505     decoder_t *p_dec;
506     decoder_owner_sys_t *p_owner;
507     int i;
508
509     p_dec = vlc_object_create( p_input, i_object_type );
510     if( p_dec == NULL )
511         return NULL;
512
513     p_dec->pf_decode_audio = NULL;
514     p_dec->pf_decode_video = NULL;
515     p_dec->pf_decode_sub = NULL;
516     p_dec->pf_get_cc = NULL;
517     p_dec->pf_packetize = NULL;
518
519     /* Initialize the decoder fifo */
520     p_dec->p_module = NULL;
521
522     memset( &null_es_format, 0, sizeof(es_format_t) );
523     es_format_Copy( &p_dec->fmt_in, fmt );
524     es_format_Copy( &p_dec->fmt_out, &null_es_format );
525
526     /* Allocate our private structure for the decoder */
527     p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
528     if( p_dec->p_owner == NULL )
529     {
530         vlc_object_release( p_dec );
531         return NULL;
532     }
533     p_dec->p_owner->b_own_thread = true;
534     p_dec->p_owner->i_preroll_end = -1;
535     p_dec->p_owner->p_input = p_input;
536     p_dec->p_owner->p_aout = NULL;
537     p_dec->p_owner->p_aout_input = NULL;
538     p_dec->p_owner->p_vout = NULL;
539     p_dec->p_owner->p_spu_vout = NULL;
540     p_dec->p_owner->i_spu_channel = 0;
541     p_dec->p_owner->i_spu_order = 0;
542     p_dec->p_owner->p_sout = p_sout;
543     p_dec->p_owner->p_sout_input = NULL;
544     p_dec->p_owner->p_packetizer = NULL;
545
546     /* decoder fifo */
547     if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
548     {
549         free( p_dec->p_owner );
550         vlc_object_release( p_dec );
551         return NULL;
552     }
553
554     /* Set buffers allocation callbacks for the decoders */
555     p_dec->pf_aout_buffer_new = aout_new_buffer;
556     p_dec->pf_aout_buffer_del = aout_del_buffer;
557     p_dec->pf_vout_buffer_new = vout_new_buffer;
558     p_dec->pf_vout_buffer_del = vout_del_buffer;
559     p_dec->pf_picture_link    = vout_link_picture;
560     p_dec->pf_picture_unlink  = vout_unlink_picture;
561     p_dec->pf_spu_buffer_new  = spu_new_buffer;
562     p_dec->pf_spu_buffer_del  = spu_del_buffer;
563
564     vlc_object_attach( p_dec, p_input );
565
566     /* Find a suitable decoder/packetizer module */
567     if( i_object_type == VLC_OBJECT_DECODER )
568         p_dec->p_module = module_need( p_dec, "decoder", "$codec", 0 );
569     else
570         p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", 0 );
571
572     /* Check if decoder requires already packetized data */
573     if( i_object_type == VLC_OBJECT_DECODER &&
574         p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
575     {
576         p_dec->p_owner->p_packetizer =
577             vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
578         if( p_dec->p_owner->p_packetizer )
579         {
580             es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
581                             &p_dec->fmt_in );
582
583             es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
584                             &null_es_format );
585
586             vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
587
588             p_dec->p_owner->p_packetizer->p_module =
589                 module_need( p_dec->p_owner->p_packetizer,
590                              "packetizer", "$packetizer", 0 );
591
592             if( !p_dec->p_owner->p_packetizer->p_module )
593             {
594                 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
595                 vlc_object_detach( p_dec->p_owner->p_packetizer );
596                 vlc_object_release( p_dec->p_owner->p_packetizer );
597             }
598         }
599     }
600
601     /* Copy ourself the input replay gain */
602     if( fmt->i_cat == AUDIO_ES )
603     {
604         for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
605         {
606             if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
607             {
608                 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
609                 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
610             }
611             if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
612             {
613                 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
614                 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
615             }
616         }
617     }
618     /* */
619     p_owner->b_cc_supported = false;
620     if( i_object_type == VLC_OBJECT_DECODER )
621     {
622         if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
623             p_owner->b_cc_supported = true;
624         if( p_dec->pf_get_cc )
625             p_owner->b_cc_supported = true;
626     }
627
628     vlc_mutex_init( &p_owner->lock );
629     vlc_cond_init( &p_owner->wait );
630     p_owner->b_paused = false;
631     p_owner->i_pause_date = 0;
632     for( i = 0; i < 4; i++ )
633     {
634         p_owner->pb_cc_present[i] = false;
635         p_owner->pp_cc[i] = NULL;
636     }
637     return p_dec;
638 }
639
640 /**
641  * The decoding main loop
642  *
643  * \param p_dec the decoder
644  */
645 static void* DecoderThread( vlc_object_t *p_this )
646 {
647     decoder_t *p_dec = (decoder_t *)p_this;
648     decoder_owner_sys_t *p_owner = p_dec->p_owner;
649
650     block_t *p_block;
651     int canc = vlc_savecancel();
652
653     /* The decoder's main loop */
654     while( vlc_object_alive( p_dec ) && !p_dec->b_error )
655     {
656         if( ( p_block = block_FifoGet( p_owner->p_fifo ) ) == NULL )
657         {
658             p_dec->b_error = 1;
659             break;
660         }
661
662         if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
663             break;
664     }
665
666     while( vlc_object_alive( p_dec ) )
667     {
668         /* Trash all received PES packets */
669         p_block = block_FifoGet( p_owner->p_fifo );
670         if( p_block )
671             block_Release( p_block );
672     }
673
674     /* We do it here because of the dll loader that wants close() in the
675      * same thread than open()/decode() */
676     module_unneed( p_dec, p_dec->p_module );
677     vlc_restorecancel( canc );
678     return NULL;
679 }
680
681 static void DecoderWaitUnpause( decoder_t *p_dec )
682 {
683     decoder_owner_sys_t *p_owner = p_dec->p_owner;
684
685     vlc_mutex_lock( &p_owner->lock );
686
687     while( p_owner->b_paused )
688         vlc_cond_wait( &p_owner->wait, &p_owner->lock );
689
690     vlc_mutex_unlock( &p_owner->lock );
691 }
692
693 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
694 {
695     decoder_owner_sys_t *p_owner = p_dec->p_owner;
696
697     /* XXX only audio and video output have to be paused.
698      * - for sout it is useless
699      * - for subs, it is done by the vout
700      */
701     if( p_dec->fmt_in.i_cat == AUDIO_ES )
702     {
703         // TODO
704         //if( p_own->p_vout )
705         //    aout_ChangePause( p_own->p_aout, p_own->p_aout_input, b_paused, i_date );
706     }
707     else if( p_dec->fmt_in.i_cat == VIDEO_ES )
708     {
709         if( p_owner->p_vout )
710             vout_ChangePause( p_owner->p_vout, b_paused, i_date );
711     }
712 }
713 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
714 {
715     if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
716         *pi_preroll = INT64_MAX;
717     else if( p->i_pts > 0 )
718         *pi_preroll = __MIN( *pi_preroll, p->i_pts );
719     else if( p->i_dts > 0 )
720         *pi_preroll = __MIN( *pi_preroll, p->i_dts );
721 }
722
723 static mtime_t DecoderTeletextFixTs( mtime_t i_ts, mtime_t i_ts_delay )
724 {
725     mtime_t current_date = mdate();
726
727     /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
728     if( !i_ts || i_ts > current_date + 10000000 || i_ts < current_date )
729     {
730         /* ETSI EN 300 472 Annex A : do not take into account the PTS
731          * for teletext streams. */
732         return current_date + 400000 + i_ts_delay;
733     }
734     return i_ts;
735 }
736
737 static void DecoderSoutBufferFixTs( block_t *p_block,
738                                     input_clock_t *p_clock, mtime_t i_ts_delay,
739                                     bool b_teletext )
740 {
741     assert( p_clock );
742
743     p_block->i_rate = input_clock_GetRate( p_clock );
744
745     if( p_block->i_dts > 0 )
746         p_block->i_dts = input_clock_GetTS( p_clock, i_ts_delay, p_block->i_dts );
747
748     if( p_block->i_pts > 0 )
749         p_block->i_pts = input_clock_GetTS( p_clock, i_ts_delay, p_block->i_pts );
750
751     if( p_block->i_length > 0 )
752         p_block->i_length = ( p_block->i_length * p_block->i_rate +
753                                 INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
754
755     if( b_teletext )
756         p_block->i_pts = DecoderTeletextFixTs( p_block->i_pts, i_ts_delay );
757 }
758 static void DecoderAoutBufferFixTs( aout_buffer_t *p_buffer,
759                                     input_clock_t *p_clock, mtime_t i_ts_delay )
760 {
761     /* sout display module does not set clock */
762     if( !p_clock )
763         return;
764
765     if( p_buffer->start_date )
766         p_buffer->start_date = input_clock_GetTS( p_clock, i_ts_delay, p_buffer->start_date );
767
768     if( p_buffer->end_date )
769         p_buffer->end_date = input_clock_GetTS( p_clock, i_ts_delay, p_buffer->end_date );
770 }
771 static void DecoderVoutBufferFixTs( picture_t *p_picture,
772                                     input_clock_t *p_clock, mtime_t i_ts_delay )
773 {
774     /* sout display module does not set clock */
775     if( !p_clock )
776         return;
777
778     if( p_picture->date )
779         p_picture->date = input_clock_GetTS( p_clock, i_ts_delay, p_picture->date );
780 }
781 static void DecoderSpuBufferFixTs( subpicture_t *p_subpic,
782                                    input_clock_t *p_clock, mtime_t i_ts_delay,
783                                    bool b_teletext )
784 {
785     bool b_ephemere = p_subpic->i_start == p_subpic->i_stop;
786
787     /* sout display module does not set clock */
788     if( !p_clock )
789         return;
790
791     if( p_subpic->i_start )
792         p_subpic->i_start = input_clock_GetTS( p_clock, i_ts_delay, p_subpic->i_start );
793
794     if( p_subpic->i_stop )
795         p_subpic->i_stop = input_clock_GetTS( p_clock, i_ts_delay, p_subpic->i_stop );
796
797     /* Do not create ephemere picture because of rounding errors */
798     if( !b_ephemere && p_subpic->i_start == p_subpic->i_stop )
799         p_subpic->i_stop++;
800
801     if( b_teletext )
802         p_subpic->i_start = DecoderTeletextFixTs( p_subpic->i_start, i_ts_delay );
803 }
804
805 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
806 {
807     decoder_owner_sys_t *p_owner = p_dec->p_owner;
808     input_thread_t  *p_input = p_owner->p_input;
809     input_clock_t   *p_clock = p_owner->p_clock;
810     aout_buffer_t   *p_aout_buf;
811
812     while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
813     {
814         aout_instance_t *p_aout = p_owner->p_aout;
815         aout_input_t    *p_aout_input = p_owner->p_aout_input;
816
817         if( p_dec->b_die )
818         {
819             /* It prevent freezing VLC in case of broken decoder */
820             aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
821             if( p_block )
822                 block_Release( p_block );
823             break;
824         }
825         vlc_mutex_lock( &p_input->p->counters.counters_lock );
826         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio, 1, NULL );
827         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
828
829         if( p_aout_buf->start_date < p_owner->i_preroll_end )
830         {
831             aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
832             continue;
833         }
834
835         if( p_owner->i_preroll_end > 0 )
836         {
837             /* FIXME TODO flush audio output (don't know how to do that) */
838             msg_Dbg( p_dec, "End of audio preroll" );
839             p_owner->i_preroll_end = -1;
840         }
841
842         DecoderWaitUnpause( p_dec );
843
844         const int i_rate = p_clock ? input_clock_GetRate( p_clock ) : p_block->i_rate;
845
846         DecoderAoutBufferFixTs( p_aout_buf, p_clock, p_input->i_pts_delay );
847         if( i_rate >= INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE &&
848             i_rate <= INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
849             aout_DecPlay( p_aout, p_aout_input, p_aout_buf, i_rate );
850         else
851             aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
852     }
853 }
854 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
855 {
856     decoder_owner_sys_t *p_owner = p_dec->p_owner;
857     block_t *p_cc;
858     bool pb_present[4];
859     int i;
860     int i_cc_decoder;
861
862     assert( p_dec_cc->pf_get_cc != NULL );
863
864     /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
865     if( !p_owner->b_cc_supported )
866         return;
867
868     p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
869     if( !p_cc )
870         return;
871
872     vlc_mutex_lock( &p_owner->lock );
873     for( i = 0, i_cc_decoder = 0; i < 4; i++ )
874     {
875         p_owner->pb_cc_present[i] |= pb_present[i];
876         if( p_owner->pp_cc[i] )
877             i_cc_decoder++;
878     }
879
880     for( i = 0; i < 4; i++ )
881     {
882         if( !p_owner->pp_cc[i] )
883             continue;
884
885         if( i_cc_decoder > 1 )
886             DecoderDecode( p_owner->pp_cc[i], block_Duplicate( p_cc ) );
887         else
888             DecoderDecode( p_owner->pp_cc[i], p_cc );
889         i_cc_decoder--;
890     }
891     vlc_mutex_unlock( &p_owner->lock );
892 }
893 static void VoutDisplayedPicture( vout_thread_t *p_vout, picture_t *p_pic )
894 {
895     vlc_mutex_lock( &p_vout->picture_lock );
896
897     if( p_pic->i_status == READY_PICTURE )
898     {
899         /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
900         p_pic->date = 1;
901     }
902     else if( p_pic->i_refcount > 0 )
903     {
904         p_pic->i_status = DISPLAYED_PICTURE;
905     }
906     else
907     {
908         p_pic->i_status = DESTROYED_PICTURE;
909         picture_CleanupQuant( p_pic );
910         p_vout->i_heap_size--;
911     }
912
913     vlc_mutex_unlock( &p_vout->picture_lock );
914 }
915 static void VoutFlushPicture( vout_thread_t *p_vout )
916 {
917     int i;
918     vlc_mutex_lock( &p_vout->picture_lock );
919     for( i = 0; i < p_vout->render.i_pictures; i++ )
920     {
921         picture_t *p_pic = p_vout->render.pp_picture[i];
922
923         if( p_pic->i_status == READY_PICTURE ||
924             p_pic->i_status == DISPLAYED_PICTURE )
925         {
926             /* We cannot change picture status if it is in READY_PICTURE state,
927              * Just make sure they won't be displayed */
928             p_pic->date = 1;
929         }
930     }
931     vlc_mutex_unlock( &p_vout->picture_lock );
932 }
933
934 #if 0
935 static void DecoderOptimizePtsDelay( decoder_t *p_dec )
936 {
937     input_thread_t *p_input = p_dec->p_owner->p_input;
938     vout_thread_t *p_vout = p_dec->p_owner->p_vout;
939     input_thread_private_t *p_priv = p_input->p;
940
941     picture_t *p_old = NULL;
942     picture_t *p_young = NULL;
943     int i;
944
945     /* Enable with --auto-adjust-pts-delay */
946     if( !p_priv->pts_adjust.b_auto_adjust )
947         return;
948
949     for( i = 0; i < I_RENDERPICTURES; i++ )
950     {
951         picture_t *p_pic = PP_RENDERPICTURE[i];
952
953         if( p_pic->i_status != READY_PICTURE )
954             continue;
955
956         if( !p_old || p_pic->date < p_old->date )
957             p_old = p_pic;
958         if( !p_young || p_pic->date > p_young->date )
959             p_young = p_pic;
960     }
961
962     if( !p_young || !p_old )
963         return;
964
965     /* Try to find if we can reduce the pts
966      * This first draft is way too simple, and we can't say if the
967      * algo will converge. It's also full of constants.
968      * But this simple algo allows to reduce the latency
969      * to the minimum.
970      * The whole point of this, is to bypass the pts_delay set
971      * by the access but also the delay arbitraly set by
972      * the remote server.
973      * Actually the remote server's muxer may set up a
974      * pts<->dts delay in the muxed stream. That is
975      * why we may end up in having a negative pts_delay,
976      * to compensate that artificial delay. */
977     const mtime_t i_buffer_length = p_young->date - p_old->date;
978     int64_t i_pts_slide = 0;
979     if( i_buffer_length < 10000 )
980     {
981         if( p_priv->pts_adjust.i_num_faulty > 10 )
982         {
983             i_pts_slide = __MAX(p_input->i_pts_delay *3 / 2, 10000);
984             p_priv->pts_adjust.i_num_faulty = 0;
985         }
986         if( p_priv->pts_adjust.b_to_high )
987         {
988             p_priv->pts_adjust.b_to_high = !p_priv->pts_adjust.b_to_high;
989             p_priv->pts_adjust.i_num_faulty = 0;
990         }
991         p_priv->pts_adjust.i_num_faulty++;
992     }
993     else if( i_buffer_length > 100000 )
994     {
995         if( p_priv->pts_adjust.i_num_faulty > 25 )
996         {
997             i_pts_slide = -i_buffer_length/2;
998             p_priv->pts_adjust.i_num_faulty = 0;
999         }
1000         if( p_priv->pts_adjust.b_to_high )
1001         {
1002             p_priv->pts_adjust.b_to_high = !p_priv->pts_adjust.b_to_high;
1003             p_priv->pts_adjust.i_num_faulty = 0;
1004         }
1005         p_priv->pts_adjust.i_num_faulty++;
1006     }
1007     if( i_pts_slide != 0 )
1008     {
1009         const mtime_t i_pts_delay_org = p_input->i_pts_delay;
1010
1011         p_input->i_pts_delay += i_pts_slide;
1012
1013         /* Don't play with the pts delay for more than -2<->3sec */
1014         if( p_input->i_pts_delay < -2000000 )
1015             p_input->i_pts_delay = -2000000;
1016         else if( p_input->i_pts_delay > 3000000 )
1017             p_input->i_pts_delay = 3000000;
1018         i_pts_slide = p_input->i_pts_delay - i_pts_delay_org;
1019
1020         msg_Dbg( p_input, "Sliding the pts by %dms pts delay at %dms picture buffer was %dms",
1021             (int)i_pts_slide/1000, (int)p_input->i_pts_delay/1000, (int)i_buffer_length/1000);
1022
1023         vlc_mutex_lock( &p_vout->picture_lock );
1024         /* Slide all the picture */
1025         for( i = 0; i < I_RENDERPICTURES; i++ )
1026             PP_RENDERPICTURE[i]->date += i_pts_slide;
1027         /* FIXME: slide aout/spu */
1028         vlc_mutex_unlock( &p_vout->picture_lock );
1029     }
1030 }
1031 #endif
1032
1033 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1034 {
1035     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1036     input_thread_t *p_input = p_owner->p_input;
1037     picture_t      *p_pic;
1038
1039     while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1040     {
1041         vout_thread_t  *p_vout = p_owner->p_vout;
1042         if( p_dec->b_die )
1043         {
1044             /* It prevent freezing VLC in case of broken decoder */
1045             VoutDisplayedPicture( p_vout, p_pic );
1046             if( p_block )
1047                 block_Release( p_block );
1048             break;
1049         }
1050
1051         vlc_mutex_lock( &p_input->p->counters.counters_lock );
1052         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video, 1, NULL );
1053         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1054
1055         if( p_pic->date < p_owner->i_preroll_end )
1056         {
1057             VoutDisplayedPicture( p_vout, p_pic );
1058             continue;
1059         }
1060
1061         if( p_owner->i_preroll_end > 0 )
1062         {
1063             msg_Dbg( p_dec, "End of video preroll" );
1064             if( p_vout )
1065                 VoutFlushPicture( p_vout );
1066             /* */
1067             p_owner->i_preroll_end = -1;
1068         }
1069
1070         if( p_dec->pf_get_cc &&
1071             ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1072             DecoderGetCc( p_dec, p_dec );
1073
1074         DecoderWaitUnpause( p_dec );
1075
1076         DecoderVoutBufferFixTs( p_pic, p_owner->p_clock, p_input->i_pts_delay );
1077
1078         vout_DatePicture( p_vout, p_pic, p_pic->date );
1079
1080         /* Re-enable it but do it right this time */
1081         //DecoderOptimizePtsDelay( p_dec );
1082
1083         vout_DisplayPicture( p_vout, p_pic );
1084     }
1085 }
1086
1087 /**
1088  * Decode a block
1089  *
1090  * \param p_dec the decoder object
1091  * \param p_block the block to decode
1092  * \return VLC_SUCCESS or an error code
1093  */
1094 static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
1095 {
1096     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1097     const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1098
1099     if( p_block && p_block->i_buffer <= 0 )
1100     {
1101         block_Release( p_block );
1102         return VLC_SUCCESS;
1103     }
1104
1105 #ifdef ENABLE_SOUT
1106     if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
1107     {
1108         block_t *p_sout_block;
1109
1110         while( ( p_sout_block =
1111                      p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1112         {
1113             if( !p_owner->p_sout_input )
1114             {
1115                 es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1116
1117                 p_owner->sout.i_group = p_dec->fmt_in.i_group;
1118                 p_owner->sout.i_id = p_dec->fmt_in.i_id;
1119                 if( p_dec->fmt_in.psz_language )
1120                 {
1121                     if( p_owner->sout.psz_language )
1122                         free( p_owner->sout.psz_language );
1123                     p_owner->sout.psz_language =
1124                         strdup( p_dec->fmt_in.psz_language );
1125                 }
1126
1127                 p_owner->p_sout_input =
1128                     sout_InputNew( p_owner->p_sout,
1129                                    &p_owner->sout );
1130
1131                 if( p_owner->p_sout_input == NULL )
1132                 {
1133                     msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1134                              (char *)&p_owner->sout.i_codec );
1135                     p_dec->b_error = true;
1136
1137                     while( p_sout_block )
1138                     {
1139                         block_t *p_next = p_sout_block->p_next;
1140                         block_Release( p_sout_block );
1141                         p_sout_block = p_next;
1142                     }
1143                     break;
1144                 }
1145             }
1146
1147             while( p_sout_block )
1148             {
1149                 block_t *p_next = p_sout_block->p_next;
1150
1151                 p_sout_block->p_next = NULL;
1152
1153                 DecoderWaitUnpause( p_dec );
1154
1155                 DecoderSoutBufferFixTs( p_sout_block,
1156                                         p_owner->p_clock, p_owner->p_input->i_pts_delay, b_telx );
1157
1158                 sout_InputSendBuffer( p_owner->p_sout_input,
1159                                       p_sout_block );
1160
1161                 p_sout_block = p_next;
1162             }
1163
1164             /* For now it's enough, as only sout impact on this flag */
1165             if( p_owner->p_sout->i_out_pace_nocontrol > 0 &&
1166                 p_owner->p_input->p->b_out_pace_control )
1167             {
1168                 msg_Dbg( p_dec, "switching to sync mode" );
1169                 p_owner->p_input->p->b_out_pace_control = false;
1170             }
1171             else if( p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
1172                      !p_owner->p_input->p->b_out_pace_control )
1173             {
1174                 msg_Dbg( p_dec, "switching to async mode" );
1175                 p_owner->p_input->p->b_out_pace_control = true;
1176             }
1177         }
1178     }
1179     else
1180 #endif
1181     if( p_dec->fmt_in.i_cat == AUDIO_ES )
1182     {
1183         if( p_block )
1184             DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1185
1186         if( p_owner->p_packetizer )
1187         {
1188             block_t *p_packetized_block;
1189             decoder_t *p_packetizer = p_owner->p_packetizer;
1190
1191             while( (p_packetized_block =
1192                     p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1193             {
1194                 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1195                 {
1196                     es_format_Clean( &p_dec->fmt_in );
1197                     es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1198                 }
1199
1200                 while( p_packetized_block )
1201                 {
1202                     block_t *p_next = p_packetized_block->p_next;
1203                     p_packetized_block->p_next = NULL;
1204
1205                     DecoderDecodeAudio( p_dec, p_packetized_block );
1206
1207                     p_packetized_block = p_next;
1208                 }
1209             }
1210         }
1211         else if( p_block )
1212         {
1213             DecoderDecodeAudio( p_dec, p_block );
1214         }
1215     }
1216     else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1217     {
1218         if( p_block )
1219             DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1220
1221         if( p_owner->p_packetizer )
1222         {
1223             block_t *p_packetized_block;
1224             decoder_t *p_packetizer = p_owner->p_packetizer;
1225
1226             while( (p_packetized_block =
1227                     p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1228             {
1229                 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1230                 {
1231                     es_format_Clean( &p_dec->fmt_in );
1232                     es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1233                 }
1234                 if( p_packetizer->pf_get_cc )
1235                     DecoderGetCc( p_dec, p_packetizer );
1236
1237                 while( p_packetized_block )
1238                 {
1239                     block_t *p_next = p_packetized_block->p_next;
1240                     p_packetized_block->p_next = NULL;
1241
1242                     DecoderDecodeVideo( p_dec, p_packetized_block );
1243
1244                     p_packetized_block = p_next;
1245                 }
1246             }
1247         }
1248         else if( p_block )
1249         {
1250             DecoderDecodeVideo( p_dec, p_block );
1251         }
1252     }
1253     else if( p_dec->fmt_in.i_cat == SPU_ES )
1254     {
1255         input_thread_t *p_input = p_owner->p_input;
1256         vout_thread_t *p_vout;
1257         subpicture_t *p_spu;
1258         bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1259         bool b_flush = false;
1260
1261         if( p_block )
1262         {
1263             DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1264             b_flush = (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH) != 0;
1265         }
1266
1267         if( !b_flushing && b_flush && p_owner->p_spu_vout )
1268         {
1269             p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1270
1271             if( p_vout && p_owner->p_spu_vout == p_vout )
1272                 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1273                              p_owner->i_spu_channel );
1274
1275             if( p_vout )
1276                 vlc_object_release( p_vout );
1277         }
1278
1279         while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1280         {
1281             vlc_mutex_lock( &p_input->p->counters.counters_lock );
1282             stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1283             vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1284
1285             p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1286             if( p_vout && p_owner->p_spu_vout == p_vout )
1287             {
1288                 /* Preroll does not work very well with subtitle */
1289                 if( p_spu->i_start < p_owner->i_preroll_end &&
1290                     ( p_spu->i_stop <= 0 || p_spu->i_stop < p_owner->i_preroll_end ) )
1291                 {
1292                     subpicture_Delete( p_spu );
1293                 }
1294                 else
1295                 {
1296                     DecoderWaitUnpause( p_dec );
1297
1298                     DecoderSpuBufferFixTs( p_spu, p_owner->p_clock, p_input->i_pts_delay, b_telx );
1299                     spu_DisplaySubpicture( p_vout->p_spu, p_spu );
1300                 }
1301             }
1302             else
1303             {
1304                 msg_Warn( p_dec, "no vout found, leaking subpicture" );
1305             }
1306             if( p_vout )
1307                 vlc_object_release( p_vout );
1308         }
1309     }
1310     else
1311     {
1312         msg_Err( p_dec, "unknown ES format" );
1313         p_dec->b_error = 1;
1314     }
1315
1316     return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
1317 }
1318
1319 /**
1320  * Destroys a decoder object
1321  *
1322  * \param p_dec the decoder object
1323  * \return nothing
1324  */
1325 static void DeleteDecoder( decoder_t * p_dec )
1326 {
1327     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1328
1329     msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
1330              (char*)&p_dec->fmt_in.i_codec,
1331              (unsigned)block_FifoCount( p_owner->p_fifo ) );
1332
1333     /* Free all packets still in the decoder fifo. */
1334     block_FifoEmpty( p_owner->p_fifo );
1335     block_FifoRelease( p_owner->p_fifo );
1336
1337     /* Cleanup */
1338     if( p_owner->p_aout_input )
1339         aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
1340     if( p_owner->p_aout )
1341     {
1342         vlc_object_release( p_owner->p_aout );
1343         p_owner->p_aout = NULL;
1344     }
1345     if( p_owner->p_vout )
1346     {
1347         int i_pic;
1348
1349 #define p_pic p_owner->p_vout->render.pp_picture[i_pic]
1350         /* Hack to make sure all the the pictures are freed by the decoder */
1351         for( i_pic = 0; i_pic < p_owner->p_vout->render.i_pictures;
1352              i_pic++ )
1353         {
1354             if( p_pic->i_status == RESERVED_PICTURE )
1355                 vout_DestroyPicture( p_owner->p_vout, p_pic );
1356             if( p_pic->i_refcount > 0 )
1357                 vout_UnlinkPicture( p_owner->p_vout, p_pic );
1358         }
1359 #undef p_pic
1360
1361         /* We are about to die. Reattach video output to p_vlc. */
1362         vout_Request( p_dec, p_owner->p_vout, NULL );
1363         var_SetBool( p_owner->p_input, "intf-change-vout", true );
1364     }
1365
1366 #ifdef ENABLE_SOUT
1367     if( p_owner->p_sout_input )
1368     {
1369         sout_InputDelete( p_owner->p_sout_input );
1370         es_format_Clean( &p_owner->sout );
1371     }
1372 #endif
1373
1374     if( p_dec->fmt_in.i_cat == SPU_ES )
1375     {
1376         vout_thread_t *p_vout;
1377
1378         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1379         if( p_vout && p_owner->p_spu_vout == p_vout )
1380         {
1381             spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1382                          p_owner->i_spu_channel );
1383             vlc_object_release( p_vout );
1384         }
1385     }
1386
1387     es_format_Clean( &p_dec->fmt_in );
1388     es_format_Clean( &p_dec->fmt_out );
1389
1390     if( p_owner->p_packetizer )
1391     {
1392         module_unneed( p_owner->p_packetizer,
1393                        p_owner->p_packetizer->p_module );
1394         es_format_Clean( &p_owner->p_packetizer->fmt_in );
1395         es_format_Clean( &p_owner->p_packetizer->fmt_out );
1396         vlc_object_detach( p_owner->p_packetizer );
1397         vlc_object_release( p_owner->p_packetizer );
1398     }
1399
1400     vlc_cond_destroy( &p_owner->wait );
1401     vlc_mutex_destroy( &p_owner->lock );
1402
1403     vlc_object_detach( p_dec );
1404
1405     free( p_owner );
1406 }
1407
1408 /*****************************************************************************
1409  * Buffers allocation callbacks for the decoders
1410  *****************************************************************************/
1411 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
1412 {
1413     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1414     aout_buffer_t *p_buffer;
1415
1416     if( p_owner->p_aout_input != NULL &&
1417         ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
1418           p_dec->fmt_out.audio.i_original_channels !=
1419               p_owner->audio.i_original_channels ||
1420           p_dec->fmt_out.audio.i_bytes_per_frame !=
1421               p_owner->audio.i_bytes_per_frame ) )
1422     {
1423         aout_input_t *p_aout_input = p_owner->p_aout_input;
1424
1425         /* Parameters changed, restart the aout */
1426         vlc_mutex_lock( &p_owner->lock );
1427
1428         p_owner->p_aout_input = NULL;
1429         aout_DecDelete( p_owner->p_aout, p_aout_input );
1430
1431         vlc_mutex_unlock( &p_owner->lock );
1432     }
1433
1434     if( p_owner->p_aout_input == NULL )
1435     {
1436         const int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
1437         audio_sample_format_t format;
1438         aout_input_t *p_aout_input;
1439         aout_instance_t *p_aout;
1440
1441         p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
1442         p_owner->audio = p_dec->fmt_out.audio;
1443
1444         memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
1445         if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
1446                                     == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
1447         {
1448             if ( i_force_dolby == 1 )
1449             {
1450                 format.i_original_channels = format.i_original_channels |
1451                                              AOUT_CHAN_DOLBYSTEREO;
1452             }
1453             else /* i_force_dolby == 2 */
1454             {
1455                 format.i_original_channels = format.i_original_channels &
1456                                              ~AOUT_CHAN_DOLBYSTEREO;
1457             }
1458         }
1459
1460         p_aout = p_owner->p_aout;
1461         p_aout_input = aout_DecNew( p_dec, &p_aout,
1462                                     &format, &p_dec->fmt_out.audio_replay_gain );
1463
1464         vlc_mutex_lock( &p_owner->lock );
1465         p_owner->p_aout = p_aout;
1466         p_owner->p_aout_input = p_aout_input;
1467         vlc_mutex_unlock( &p_owner->lock );
1468
1469         if( p_owner->p_aout_input == NULL )
1470         {
1471             msg_Err( p_dec, "failed to create audio output" );
1472             p_dec->b_error = true;
1473             return NULL;
1474         }
1475         p_dec->fmt_out.audio.i_bytes_per_frame =
1476             p_owner->audio.i_bytes_per_frame;
1477     }
1478
1479     p_buffer = aout_DecNewBuffer( p_owner->p_aout_input, i_samples );
1480
1481     return p_buffer;
1482 }
1483
1484 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1485 {
1486     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1487
1488     aout_DecDeleteBuffer( p_owner->p_aout,
1489                           p_owner->p_aout_input, p_buffer );
1490 }
1491
1492
1493 int vout_CountPictureAvailable( vout_thread_t *p_vout );
1494
1495 static picture_t *vout_new_buffer( decoder_t *p_dec )
1496 {
1497     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1498     picture_t *p_pic;
1499
1500     if( p_owner->p_vout == NULL ||
1501         p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
1502         p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
1503         p_dec->fmt_out.video.i_chroma != p_owner->video.i_chroma ||
1504         p_dec->fmt_out.video.i_aspect != p_owner->video.i_aspect )
1505     {
1506         vout_thread_t *p_vout;
1507
1508         if( !p_dec->fmt_out.video.i_width ||
1509             !p_dec->fmt_out.video.i_height )
1510         {
1511             /* Can't create a new vout without display size */
1512             return NULL;
1513         }
1514
1515         if( !p_dec->fmt_out.video.i_visible_width ||
1516             !p_dec->fmt_out.video.i_visible_height )
1517         {
1518             if( p_dec->fmt_in.video.i_visible_width &&
1519                 p_dec->fmt_in.video.i_visible_height )
1520             {
1521                 p_dec->fmt_out.video.i_visible_width =
1522                     p_dec->fmt_in.video.i_visible_width;
1523                 p_dec->fmt_out.video.i_visible_height =
1524                     p_dec->fmt_in.video.i_visible_height;
1525             }
1526             else
1527             {
1528                 p_dec->fmt_out.video.i_visible_width =
1529                     p_dec->fmt_out.video.i_width;
1530                 p_dec->fmt_out.video.i_visible_height =
1531                     p_dec->fmt_out.video.i_height;
1532             }
1533         }
1534
1535         if( p_dec->fmt_out.video.i_visible_height == 1088 &&
1536             var_CreateGetBool( p_dec, "hdtv-fix" ) )
1537         {
1538             p_dec->fmt_out.video.i_visible_height = 1080;
1539             p_dec->fmt_out.video.i_sar_num *= 135;
1540             p_dec->fmt_out.video.i_sar_den *= 136;
1541             msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
1542         }
1543
1544         if( !p_dec->fmt_out.video.i_sar_num ||
1545             !p_dec->fmt_out.video.i_sar_den )
1546         {
1547             p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
1548               p_dec->fmt_out.video.i_visible_height;
1549
1550             p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
1551               p_dec->fmt_out.video.i_visible_width;
1552         }
1553
1554         vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
1555                      &p_dec->fmt_out.video.i_sar_den,
1556                      p_dec->fmt_out.video.i_sar_num,
1557                      p_dec->fmt_out.video.i_sar_den, 50000 );
1558
1559         p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
1560         p_owner->video = p_dec->fmt_out.video;
1561
1562         vlc_mutex_lock( &p_owner->lock );
1563         p_vout = p_owner->p_vout;
1564         p_owner->p_vout = NULL;
1565         vlc_mutex_unlock( &p_owner->lock );
1566
1567         p_vout = vout_Request( p_dec, p_vout, &p_dec->fmt_out.video );
1568
1569         vlc_mutex_lock( &p_owner->lock );
1570         p_owner->p_vout = p_vout;
1571         vlc_mutex_unlock( &p_owner->lock );
1572
1573         var_SetBool( p_owner->p_input, "intf-change-vout", true );
1574         if( p_vout == NULL )
1575         {
1576             msg_Err( p_dec, "failed to create video output" );
1577             p_dec->b_error = true;
1578             return NULL;
1579         }
1580
1581         if( p_owner->video.i_rmask )
1582             p_owner->p_vout->render.i_rmask = p_owner->video.i_rmask;
1583         if( p_owner->video.i_gmask )
1584             p_owner->p_vout->render.i_gmask = p_owner->video.i_gmask;
1585         if( p_owner->video.i_bmask )
1586             p_owner->p_vout->render.i_bmask = p_owner->video.i_bmask;
1587     }
1588
1589     /* Get a new picture
1590      */
1591     for( p_pic = NULL; ; )
1592     {
1593         int i_pic, i_ready_pic;
1594
1595         if( p_dec->b_die || p_dec->b_error )
1596             return NULL;
1597
1598         /* The video filter chain required that there is always 1 free buffer
1599          * that it will use as temporary one. It will release the temporary
1600          * buffer once its work is done, so this check is safe even if we don't
1601          * lock around both count() and create().
1602          */
1603         if( vout_CountPictureAvailable( p_owner->p_vout ) >= 2 )
1604         {
1605             p_pic = vout_CreatePicture( p_owner->p_vout, 0, 0, 0 );
1606             if( p_pic )
1607                 break;
1608         }
1609
1610 #define p_pic p_owner->p_vout->render.pp_picture[i_pic]
1611         /* Check the decoder doesn't leak pictures */
1612         for( i_pic = 0, i_ready_pic = 0; i_pic < p_owner->p_vout->render.i_pictures; i_pic++ )
1613         {
1614             if( p_pic->i_status == READY_PICTURE )
1615             {
1616                 i_ready_pic++;
1617                 /* If we have at least 2 ready pictures, wait for the vout thread to
1618                  * process one */
1619                 if( i_ready_pic >= 2 )
1620                     break;
1621
1622                 continue;
1623             }
1624
1625             if( p_pic->i_status == DISPLAYED_PICTURE )
1626             {
1627                 /* If at least one displayed picture is not referenced
1628                  * let vout free it */
1629                 if( p_pic->i_refcount == 0 )
1630                     break;
1631             }
1632         }
1633         if( i_pic == p_owner->p_vout->render.i_pictures )
1634         {
1635             /* Too many pictures are still referenced, there is probably a bug
1636              * with the decoder */
1637             msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
1638
1639             /* Just free all the pictures */
1640             for( i_pic = 0; i_pic < p_owner->p_vout->render.i_pictures;
1641                  i_pic++ )
1642             {
1643                 if( p_pic->i_status == RESERVED_PICTURE )
1644                     vout_DestroyPicture( p_owner->p_vout, p_pic );
1645                 if( p_pic->i_refcount > 0 )
1646                 vout_UnlinkPicture( p_owner->p_vout, p_pic );
1647             }
1648         }
1649 #undef p_pic
1650
1651         msleep( VOUT_OUTMEM_SLEEP );
1652     }
1653
1654     return p_pic;
1655 }
1656
1657 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
1658 {
1659     VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
1660 }
1661
1662 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
1663 {
1664     vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
1665 }
1666
1667 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
1668 {
1669     vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1670 }
1671
1672 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
1673 {
1674     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1675     vout_thread_t *p_vout = NULL;
1676     subpicture_t *p_subpic;
1677     int i_attempts = 30;
1678
1679     while( i_attempts-- )
1680     {
1681         if( p_dec->b_die || p_dec->b_error )
1682             break;
1683
1684         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1685         if( p_vout )
1686             break;
1687
1688         msleep( VOUT_DISPLAY_DELAY );
1689     }
1690
1691     if( !p_vout )
1692     {
1693         msg_Warn( p_dec, "no vout found, dropping subpicture" );
1694         return NULL;
1695     }
1696
1697     if( p_owner->p_spu_vout != p_vout )
1698     {
1699         spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
1700                      &p_owner->i_spu_channel );
1701         p_owner->i_spu_order = 0;
1702         p_owner->p_spu_vout = p_vout;
1703     }
1704
1705     p_subpic = subpicture_New();
1706     if( p_subpic )
1707     {
1708         p_subpic->i_channel = p_owner->i_spu_channel;
1709         p_subpic->i_order = p_owner->i_spu_order++;
1710         p_subpic->b_subtitle = true;
1711     }
1712
1713     vlc_object_release( p_vout );
1714
1715     return p_subpic;
1716 }
1717
1718 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1719 {
1720     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1721     vout_thread_t *p_vout = NULL;
1722
1723     p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1724     if( !p_vout || p_owner->p_spu_vout != p_vout )
1725     {
1726         if( p_vout )
1727             vlc_object_release( p_vout );
1728         msg_Warn( p_dec, "no vout found, leaking subpicture" );
1729         return;
1730     }
1731
1732     subpicture_Delete( p_subpic );
1733
1734     vlc_object_release( p_vout );
1735 }
1736