Avoid a deadlock when a decoder is in error.
[vlc.git] / 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 "clock.h"
48 #include "decoder.h"
49 #include "event.h"
50
51 #include "../video_output/vout_control.h"
52
53 static decoder_t *CreateDecoder( input_thread_t *, es_format_t *, int, sout_instance_t *p_sout );
54 static void       DeleteDecoder( decoder_t * );
55
56 static void      *DecoderThread( vlc_object_t * );
57 static void       DecoderProcess( decoder_t *, block_t * );
58 static void       DecoderError( decoder_t *p_dec, block_t *p_block );
59 static void       DecoderOutputChangePause( decoder_t *, bool b_paused, mtime_t i_date );
60 static void       DecoderFlush( decoder_t * );
61 static void       DecoderSignalBuffering( decoder_t *, bool );
62 static void       DecoderFlushBuffering( decoder_t * );
63
64 static void       DecoderUnsupportedCodec( decoder_t *, vlc_fourcc_t );
65
66 /* Buffers allocation callbacks for the decoders */
67 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
68 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
69
70 static picture_t *vout_new_buffer( decoder_t * );
71 static void vout_del_buffer( decoder_t *, picture_t * );
72 static void vout_link_picture( decoder_t *, picture_t * );
73 static void vout_unlink_picture( decoder_t *, picture_t * );
74
75 static subpicture_t *spu_new_buffer( decoder_t * );
76 static void spu_del_buffer( decoder_t *, subpicture_t * );
77
78 static es_format_t null_es_format;
79
80 struct decoder_owner_sys_t
81 {
82     int64_t         i_preroll_end;
83
84     input_thread_t  *p_input;
85     input_clock_t   *p_clock;
86     int             i_last_rate;
87
88     vout_thread_t   *p_spu_vout;
89     int              i_spu_channel;
90     int64_t          i_spu_order;
91
92     sout_instance_t         *p_sout;
93     sout_packetizer_input_t *p_sout_input;
94
95     /* Some decoders require already packetized data (ie. not truncated) */
96     decoder_t *p_packetizer;
97
98     /* Current format in use by the output */
99     video_format_t video;
100     audio_format_t audio;
101     es_format_t    sout;
102
103     /* fifo */
104     block_fifo_t *p_fifo;
105
106     /* Lock for communication with decoder thread */
107     vlc_mutex_t lock;
108     vlc_cond_t  wait;
109
110     /* -- These variables need locking on write(only) -- */
111     aout_instance_t *p_aout;
112     aout_input_t    *p_aout_input;
113
114     vout_thread_t   *p_vout;
115
116     /* -- Theses variables need locking on read *and* write -- */
117     /* */
118     /* Pause */
119     bool b_paused;
120     struct
121     {
122         mtime_t i_date;
123         int     i_ignore;
124     } pause;
125
126     /* Buffering */
127     bool b_buffering;
128     struct
129     {
130         bool b_first;
131         bool b_full;
132         int  i_count;
133
134         picture_t     *p_picture;
135         picture_t     **pp_picture_next;
136
137         subpicture_t  *p_subpic;
138         subpicture_t  **pp_subpic_next;
139
140         aout_buffer_t *p_audio;
141         aout_buffer_t **pp_audio_next;
142
143         block_t       *p_block;
144         block_t       **pp_block_next;
145     } buffer;
146
147     /* Flushing */
148     bool b_flushing;
149
150     /* CC */
151     struct
152     {
153         bool b_supported;
154         bool pb_present[4];
155         decoder_t *pp_decoder[4];
156     } cc;
157
158     /* Delay */
159     mtime_t i_ts_delay;
160 };
161
162 #define DECODER_MAX_BUFFERING_COUNT (4)
163 #define DECODER_MAX_BUFFERING_AUDIO_DURATION (AOUT_MAX_PREPARE_TIME)
164 #define DECODER_MAX_BUFFERING_VIDEO_DURATION (1*CLOCK_FREQ)
165
166 /* Pictures which are DECODER_BOGUS_VIDEO_DELAY or more in advance probably have
167  * a bogus PTS and won't be displayed */
168 #define DECODER_BOGUS_VIDEO_DELAY                ((mtime_t)(DEFAULT_PTS_DELAY * 30))
169
170 /* */
171 #define DECODER_SPU_VOUT_WAIT_DURATION ((int)(0.200*CLOCK_FREQ))
172
173
174 /*****************************************************************************
175  * Public functions
176  *****************************************************************************/
177 picture_t *decoder_NewPicture( decoder_t *p_decoder )
178 {
179     picture_t *p_picture = p_decoder->pf_vout_buffer_new( p_decoder );
180     if( !p_picture )
181         msg_Warn( p_decoder, "can't get output picture" );
182     return p_picture;
183 }
184 void decoder_DeletePicture( decoder_t *p_decoder, picture_t *p_picture )
185 {
186     p_decoder->pf_vout_buffer_del( p_decoder, p_picture );
187 }
188 void decoder_LinkPicture( decoder_t *p_decoder, picture_t *p_picture )
189 {
190     p_decoder->pf_picture_link( p_decoder, p_picture );
191 }
192 void decoder_UnlinkPicture( decoder_t *p_decoder, picture_t *p_picture )
193 {
194     p_decoder->pf_picture_unlink( p_decoder, p_picture );
195 }
196
197 aout_buffer_t *decoder_NewAudioBuffer( decoder_t *p_decoder, int i_size )
198 {
199     if( !p_decoder->pf_aout_buffer_new )
200         return NULL;
201     return p_decoder->pf_aout_buffer_new( p_decoder, i_size );
202 }
203 void decoder_DeleteAudioBuffer( decoder_t *p_decoder, aout_buffer_t *p_buffer )
204 {
205     p_decoder->pf_aout_buffer_del( p_decoder, p_buffer );
206 }
207
208 subpicture_t *decoder_NewSubpicture( decoder_t *p_decoder )
209 {
210     subpicture_t *p_subpicture = p_decoder->pf_spu_buffer_new( p_decoder );
211     if( !p_subpicture )
212         msg_Warn( p_decoder, "can't get output subpicture" );
213     return p_subpicture;
214 }
215 void decoder_DeleteSubpicture( decoder_t *p_decoder, subpicture_t *p_subpicture )
216 {
217     p_decoder->pf_spu_buffer_del( p_decoder, p_subpicture );
218 }
219
220 /* decoder_GetInputAttachments:
221  */
222 int decoder_GetInputAttachments( decoder_t *p_dec,
223                                  input_attachment_t ***ppp_attachment,
224                                  int *pi_attachment )
225 {
226     if( !p_dec->pf_get_attachments )
227         return VLC_EGENERIC;
228
229     return p_dec->pf_get_attachments( p_dec, ppp_attachment, pi_attachment );
230 }
231 /* decoder_GetDisplayDate:
232  */
233 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
234 {
235     if( !p_dec->pf_get_display_date )
236         return 0;
237
238     return p_dec->pf_get_display_date( p_dec, i_ts );
239 }
240 /* decoder_GetDisplayRate:
241  */
242 int decoder_GetDisplayRate( decoder_t *p_dec )
243 {
244     if( !p_dec->pf_get_display_rate )
245         return INPUT_RATE_DEFAULT;
246
247     return p_dec->pf_get_display_rate( p_dec );
248 }
249
250 /**
251  * Spawns a new decoder thread
252  *
253  * \param p_input the input thread
254  * \param p_es the es descriptor
255  * \return the spawned decoder object
256  */
257 decoder_t *input_DecoderNew( input_thread_t *p_input,
258                              es_format_t *fmt, input_clock_t *p_clock, sout_instance_t *p_sout  )
259 {
260     decoder_t *p_dec = NULL;
261     int i_priority;
262
263 #ifdef ENABLE_SOUT
264     /* If we are in sout mode, search for packetizer module */
265     if( p_sout )
266     {
267         /* Create the decoder configuration structure */
268         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER, p_sout );
269         if( p_dec == NULL )
270         {
271             msg_Err( p_input, "could not create packetizer" );
272             intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
273                             _("VLC could not open the packetizer module.") );
274             return NULL;
275         }
276     }
277     else
278 #endif
279     {
280         /* Create the decoder configuration structure */
281         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER, p_sout );
282         if( p_dec == NULL )
283         {
284             msg_Err( p_input, "could not create decoder" );
285             intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
286                             _("VLC could not open the decoder module.") );
287             return NULL;
288         }
289     }
290
291     if( !p_dec->p_module )
292     {
293         DecoderUnsupportedCodec( p_dec, fmt->i_codec );
294
295         DeleteDecoder( p_dec );
296         vlc_object_release( p_dec );
297         return NULL;
298     }
299
300     p_dec->p_owner->p_clock = p_clock;
301
302     if( fmt->i_cat == AUDIO_ES )
303         i_priority = VLC_THREAD_PRIORITY_AUDIO;
304     else
305         i_priority = VLC_THREAD_PRIORITY_VIDEO;
306
307     /* Spawn the decoder thread */
308     if( vlc_thread_create( p_dec, "decoder", DecoderThread,
309                            i_priority, false ) )
310     {
311         msg_Err( p_dec, "cannot spawn decoder thread" );
312         module_unneed( p_dec, p_dec->p_module );
313         DeleteDecoder( p_dec );
314         vlc_object_release( p_dec );
315         return NULL;
316     }
317
318     return p_dec;
319 }
320
321 /**
322  * Kills a decoder thread and waits until it's finished
323  *
324  * \param p_input the input thread
325  * \param p_es the es descriptor
326  * \return nothing
327  */
328 void input_DecoderDelete( decoder_t *p_dec )
329 {
330     decoder_owner_sys_t *p_owner = p_dec->p_owner;
331
332     vlc_object_kill( p_dec );
333
334     /* Make sure we aren't paused anymore */
335     vlc_mutex_lock( &p_owner->lock );
336     if( p_owner->b_paused || p_owner->b_buffering )
337     {
338         p_owner->b_paused = false;
339         p_owner->b_buffering = false;
340         vlc_cond_signal( &p_owner->wait );
341     }
342     vlc_mutex_unlock( &p_owner->lock );
343
344     vlc_thread_join( p_dec );
345     module_unneed( p_dec, p_dec->p_module );
346
347     /* */
348     if( p_dec->p_owner->cc.b_supported )
349     {
350         int i;
351         for( i = 0; i < 4; i++ )
352             input_DecoderSetCcState( p_dec, false, i );
353     }
354
355     /* Delete decoder configuration */
356     DeleteDecoder( p_dec );
357
358     /* Delete the decoder */
359     vlc_object_release( p_dec );
360 }
361
362 /**
363  * Put a block_t in the decoder's fifo.
364  * Thread-safe w.r.t. the decoder. May be a cancellation point.
365  *
366  * \param p_dec the decoder object
367  * \param p_block the data block
368  */
369 void input_DecoderDecode( decoder_t *p_dec, block_t *p_block )
370 {
371     decoder_owner_sys_t *p_owner = p_dec->p_owner;
372
373     if( p_owner->p_input->p->b_out_pace_control )
374     {
375         /* The fifo is not consummed when buffering and so will
376          * deadlock vlc.
377          * There is no need to lock as b_buffering is never modify
378          * inside decoder thread. */
379         if( !p_owner->b_buffering )
380             block_FifoPace( p_owner->p_fifo, 10, SIZE_MAX );
381     }
382     else if( block_FifoSize( p_owner->p_fifo ) > 50000000 /* 50 MB */ )
383     {
384         /* FIXME: ideally we would check the time amount of data
385          * in the FIFO instead of its size. */
386         msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
387                   "consumed quickly enough), resetting fifo!" );
388         block_FifoEmpty( p_owner->p_fifo );
389     }
390
391     block_FifoPut( p_owner->p_fifo, p_block );
392 }
393
394 bool input_DecoderIsEmpty( decoder_t * p_dec )
395 {
396     assert( !p_dec->p_owner->b_buffering );
397
398     /* FIXME that's not really true */
399     return block_FifoCount( p_dec->p_owner->p_fifo ) <= 0;
400 }
401
402 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
403 {
404     decoder_owner_sys_t *p_owner = p_dec->p_owner;
405     int i;
406
407     vlc_mutex_lock( &p_owner->lock );
408     for( i = 0; i < 4; i++ )
409         pb_present[i] =  p_owner->cc.pb_present[i];
410     vlc_mutex_unlock( &p_owner->lock );
411 }
412 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
413 {
414     decoder_owner_sys_t *p_owner = p_dec->p_owner;
415
416     //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
417
418     if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
419         return VLC_EGENERIC;
420
421     if( b_decode )
422     {
423         static const vlc_fourcc_t fcc[4] = {
424             VLC_FOURCC('c', 'c', '1', ' '),
425             VLC_FOURCC('c', 'c', '2', ' '),
426             VLC_FOURCC('c', 'c', '3', ' '),
427             VLC_FOURCC('c', 'c', '4', ' '),
428         };
429         decoder_t *p_cc;
430         es_format_t fmt;
431
432         es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
433         p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER, p_owner->p_sout );
434         if( !p_cc )
435         {
436             msg_Err( p_dec, "could not create decoder" );
437             intf_UserFatal( p_dec, false, _("Streaming / Transcoding failed"),
438                             _("VLC could not open the decoder module.") );
439             return VLC_EGENERIC;
440         }
441         else if( !p_cc->p_module )
442         {
443             DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
444             DeleteDecoder( p_cc );
445             vlc_object_release( p_cc );
446             return VLC_EGENERIC;
447         }
448         p_cc->p_owner->p_clock = p_owner->p_clock;
449
450         vlc_mutex_lock( &p_owner->lock );
451         p_owner->cc.pp_decoder[i_channel] = p_cc;
452         vlc_mutex_unlock( &p_owner->lock );
453     }
454     else
455     {
456         decoder_t *p_cc;
457
458         vlc_mutex_lock( &p_owner->lock );
459         p_cc = p_owner->cc.pp_decoder[i_channel];
460         p_owner->cc.pp_decoder[i_channel] = NULL;
461         vlc_mutex_unlock( &p_owner->lock );
462
463         if( p_cc )
464         {
465             vlc_object_kill( p_cc );
466             module_unneed( p_cc, p_cc->p_module );
467             DeleteDecoder( p_cc );
468             vlc_object_release( p_cc );
469         }
470     }
471     return VLC_SUCCESS;
472 }
473 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
474 {
475     decoder_owner_sys_t *p_owner = p_dec->p_owner;
476
477     *pb_decode = false;
478     if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
479         return VLC_EGENERIC;
480
481     vlc_mutex_lock( &p_owner->lock );
482     *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
483     vlc_mutex_unlock( &p_owner->lock );
484     return VLC_EGENERIC;
485 }
486
487 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
488 {
489     decoder_owner_sys_t *p_owner = p_dec->p_owner;
490
491     vlc_mutex_lock( &p_owner->lock );
492
493     assert( !p_owner->b_paused || !b_paused );
494
495     p_owner->b_paused = b_paused;
496     p_owner->pause.i_date = i_date;
497     p_owner->pause.i_ignore = 0;
498     vlc_cond_signal( &p_owner->wait );
499
500     DecoderOutputChangePause( p_dec, b_paused, i_date );
501
502     vlc_mutex_unlock( &p_owner->lock );
503 }
504
505 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
506 {
507     decoder_owner_sys_t *p_owner = p_dec->p_owner;
508
509     vlc_mutex_lock( &p_owner->lock );
510     p_owner->i_ts_delay = i_delay;
511     vlc_mutex_unlock( &p_owner->lock );
512 }
513
514 void input_DecoderStartBuffering( decoder_t *p_dec )
515 {
516     decoder_owner_sys_t *p_owner = p_dec->p_owner;
517
518     vlc_mutex_lock( &p_owner->lock );
519
520     DecoderFlush( p_dec );
521
522     p_owner->buffer.b_first = true;
523     p_owner->buffer.b_full = false;
524     p_owner->buffer.i_count = 0;
525
526     assert( !p_owner->buffer.p_picture && !p_owner->buffer.p_subpic &&
527             !p_owner->buffer.p_audio && !p_owner->buffer.p_block );
528
529     p_owner->buffer.p_picture = NULL;
530     p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
531
532     p_owner->buffer.p_subpic = NULL;
533     p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
534
535     p_owner->buffer.p_audio = NULL;
536     p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
537
538     p_owner->buffer.p_block = NULL;
539     p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
540
541
542     p_owner->b_buffering = true;
543
544     vlc_cond_signal( &p_owner->wait );
545
546     vlc_mutex_unlock( &p_owner->lock );
547 }
548
549 void input_DecoderStopBuffering( decoder_t *p_dec )
550 {
551     decoder_owner_sys_t *p_owner = p_dec->p_owner;
552
553     vlc_mutex_lock( &p_owner->lock );
554
555     p_owner->b_buffering = false;
556
557     vlc_cond_signal( &p_owner->wait );
558
559     vlc_mutex_unlock( &p_owner->lock );
560 }
561
562 void input_DecoderWaitBuffering( decoder_t *p_dec )
563 {
564     decoder_owner_sys_t *p_owner = p_dec->p_owner;
565
566     vlc_mutex_lock( &p_owner->lock );
567
568     while( vlc_object_alive( p_dec ) && p_owner->b_buffering && !p_owner->buffer.b_full )
569     {
570         block_FifoWake( p_owner->p_fifo );
571         vlc_cond_wait( &p_owner->wait, &p_owner->lock );
572     }
573
574     vlc_mutex_unlock( &p_owner->lock );
575 }
576 void input_DecoderFrameNext( decoder_t *p_dec, mtime_t *pi_duration )
577 {
578     decoder_owner_sys_t *p_owner = p_dec->p_owner;
579
580     *pi_duration = 0;
581
582     vlc_mutex_lock( &p_owner->lock );
583     if( p_dec->fmt_in.i_cat == VIDEO_ES )
584     {
585         if( p_owner->b_paused && p_owner->p_vout )
586         {
587             vout_NextPicture( p_owner->p_vout, pi_duration );
588             p_owner->pause.i_ignore++;
589             vlc_cond_signal( &p_owner->wait );
590         }
591     }
592     else
593     {
594         /* TODO subtitle should not be flushed */
595         DecoderFlush( p_dec );
596     }
597     vlc_mutex_unlock( &p_owner->lock );
598 }
599
600 /*****************************************************************************
601  * Internal functions
602  *****************************************************************************/
603 static int DecoderGetInputAttachments( decoder_t *p_dec,
604                                        input_attachment_t ***ppp_attachment,
605                                        int *pi_attachment )
606 {
607     return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
608                           ppp_attachment, pi_attachment );
609 }
610 static mtime_t DecoderGetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
611 {
612     decoder_owner_sys_t *p_owner = p_dec->p_owner;
613
614     vlc_mutex_lock( &p_owner->lock );
615     if( p_owner->b_buffering || p_owner->b_paused )
616         i_ts = 0;
617     vlc_mutex_unlock( &p_owner->lock );
618
619     if( !p_owner->p_clock || !i_ts )
620         return i_ts;
621
622     return input_clock_GetTS( p_owner->p_clock, NULL, p_owner->p_input->i_pts_delay, i_ts );
623 }
624 static int DecoderGetDisplayRate( decoder_t *p_dec )
625 {
626     decoder_owner_sys_t *p_owner = p_dec->p_owner;
627
628     if( !p_owner->p_clock )
629         return INPUT_RATE_DEFAULT;
630     return input_clock_GetRate( p_owner->p_clock );
631 }
632
633 /* */
634 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
635 {
636     msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
637              "VLC probably does not support this sound or video format.",
638              (char*)&codec );
639     intf_UserFatal( p_dec, false, _("No suitable decoder module"), 
640                     _("VLC does not support the audio or video format \"%4.4s\". "
641                       "Unfortunately there is no way for you to fix this."), (char*)&codec );
642 }
643
644
645 /**
646  * Create a decoder object
647  *
648  * \param p_input the input thread
649  * \param p_es the es descriptor
650  * \param i_object_type Object type as define in include/vlc_objects.h
651  * \return the decoder object
652  */
653 static decoder_t * CreateDecoder( input_thread_t *p_input,
654                                   es_format_t *fmt, int i_object_type, sout_instance_t *p_sout )
655 {
656     decoder_t *p_dec;
657     decoder_owner_sys_t *p_owner;
658     int i;
659
660     p_dec = vlc_object_create( p_input, i_object_type );
661     if( p_dec == NULL )
662         return NULL;
663
664     p_dec->pf_decode_audio = NULL;
665     p_dec->pf_decode_video = NULL;
666     p_dec->pf_decode_sub = NULL;
667     p_dec->pf_get_cc = NULL;
668     p_dec->pf_packetize = NULL;
669
670     /* Initialize the decoder fifo */
671     p_dec->p_module = NULL;
672
673     memset( &null_es_format, 0, sizeof(es_format_t) );
674     es_format_Copy( &p_dec->fmt_in, fmt );
675     es_format_Copy( &p_dec->fmt_out, &null_es_format );
676
677     /* Allocate our private structure for the decoder */
678     p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
679     if( p_dec->p_owner == NULL )
680     {
681         vlc_object_release( p_dec );
682         return NULL;
683     }
684     p_dec->p_owner->i_preroll_end = -1;
685     p_dec->p_owner->i_last_rate = INPUT_RATE_DEFAULT;
686     p_dec->p_owner->p_input = p_input;
687     p_dec->p_owner->p_aout = NULL;
688     p_dec->p_owner->p_aout_input = NULL;
689     p_dec->p_owner->p_vout = NULL;
690     p_dec->p_owner->p_spu_vout = NULL;
691     p_dec->p_owner->i_spu_channel = 0;
692     p_dec->p_owner->i_spu_order = 0;
693     p_dec->p_owner->p_sout = p_sout;
694     p_dec->p_owner->p_sout_input = NULL;
695     p_dec->p_owner->p_packetizer = NULL;
696
697     /* decoder fifo */
698     if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
699     {
700         free( p_dec->p_owner );
701         vlc_object_release( p_dec );
702         return NULL;
703     }
704
705     /* Set buffers allocation callbacks for the decoders */
706     p_dec->pf_aout_buffer_new = aout_new_buffer;
707     p_dec->pf_aout_buffer_del = aout_del_buffer;
708     p_dec->pf_vout_buffer_new = vout_new_buffer;
709     p_dec->pf_vout_buffer_del = vout_del_buffer;
710     p_dec->pf_picture_link    = vout_link_picture;
711     p_dec->pf_picture_unlink  = vout_unlink_picture;
712     p_dec->pf_spu_buffer_new  = spu_new_buffer;
713     p_dec->pf_spu_buffer_del  = spu_del_buffer;
714     /* */
715     p_dec->pf_get_attachments  = DecoderGetInputAttachments;
716     p_dec->pf_get_display_date = DecoderGetDisplayDate;
717     p_dec->pf_get_display_rate = DecoderGetDisplayRate;
718
719     vlc_object_attach( p_dec, p_input );
720
721     /* Find a suitable decoder/packetizer module */
722     if( i_object_type == VLC_OBJECT_DECODER )
723         p_dec->p_module = module_need( p_dec, "decoder", "$codec", 0 );
724     else
725         p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", 0 );
726
727     /* Check if decoder requires already packetized data */
728     if( i_object_type == VLC_OBJECT_DECODER &&
729         p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
730     {
731         p_dec->p_owner->p_packetizer =
732             vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
733         if( p_dec->p_owner->p_packetizer )
734         {
735             es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
736                             &p_dec->fmt_in );
737
738             es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
739                             &null_es_format );
740
741             vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
742
743             p_dec->p_owner->p_packetizer->p_module =
744                 module_need( p_dec->p_owner->p_packetizer,
745                              "packetizer", "$packetizer", 0 );
746
747             if( !p_dec->p_owner->p_packetizer->p_module )
748             {
749                 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
750                 vlc_object_detach( p_dec->p_owner->p_packetizer );
751                 vlc_object_release( p_dec->p_owner->p_packetizer );
752             }
753         }
754     }
755
756     /* Copy ourself the input replay gain */
757     if( fmt->i_cat == AUDIO_ES )
758     {
759         for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
760         {
761             if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
762             {
763                 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
764                 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
765             }
766             if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
767             {
768                 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
769                 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
770             }
771         }
772     }
773
774     /* */
775     vlc_mutex_init( &p_owner->lock );
776     vlc_cond_init( &p_owner->wait );
777
778     p_owner->b_paused = false;
779     p_owner->pause.i_date = 0;
780     p_owner->pause.i_ignore = 0;
781
782     p_owner->b_buffering = false;
783     p_owner->buffer.b_first = true;
784     p_owner->buffer.b_full = false;
785     p_owner->buffer.i_count = 0;
786     p_owner->buffer.p_picture = NULL;
787     p_owner->buffer.p_subpic = NULL;
788     p_owner->buffer.p_audio = NULL;
789     p_owner->buffer.p_block = NULL;
790
791     p_owner->b_flushing = false;
792
793     /* */
794     p_owner->cc.b_supported = false;
795     if( i_object_type == VLC_OBJECT_DECODER )
796     {
797         if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
798             p_owner->cc.b_supported = true;
799         if( p_dec->pf_get_cc )
800             p_owner->cc.b_supported = true;
801     }
802
803     for( i = 0; i < 4; i++ )
804     {
805         p_owner->cc.pb_present[i] = false;
806         p_owner->cc.pp_decoder[i] = NULL;
807     }
808     p_owner->i_ts_delay = 0;
809     return p_dec;
810 }
811
812 /**
813  * The decoding main loop
814  *
815  * \param p_dec the decoder
816  */
817 static void *DecoderThread( vlc_object_t *p_this )
818 {
819     decoder_t *p_dec = (decoder_t *)p_this;
820     decoder_owner_sys_t *p_owner = p_dec->p_owner;
821
822     /* The decoder's main loop */
823     for( ;; )
824     {
825         block_t *p_block = block_FifoGet( p_owner->p_fifo );
826         /* Make sure there is no cancellation point other than this one^^.
827          * If you need one, be sure to push cleanup of p_block. */
828         DecoderSignalBuffering( p_dec, p_block == NULL );
829
830         if( p_block )
831         {
832             int canc = vlc_savecancel();
833
834             if( p_dec->b_error )
835                 DecoderError( p_dec, p_block );
836             else
837                 DecoderProcess( p_dec, p_block );
838
839             vlc_restorecancel( canc );
840         }
841     }
842
843     DecoderSignalBuffering( p_dec, true );
844     return NULL;
845 }
846
847 static void DecoderFlush( decoder_t *p_dec )
848 {
849     decoder_owner_sys_t *p_owner = p_dec->p_owner;
850     block_t *p_null;
851
852     vlc_assert_locked( &p_owner->lock );
853
854     /* Empty the fifo */
855     block_FifoEmpty( p_owner->p_fifo );
856
857     /* Monitor for flush end */
858     p_owner->b_flushing = true;
859     vlc_cond_signal( &p_owner->wait );
860
861     /* Send a special block */
862     p_null = block_New( p_dec, 128 );
863     if( !p_null )
864         return;
865     p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
866     p_null->i_flags |= BLOCK_FLAG_CORE_FLUSH;
867     if( !p_dec->fmt_in.b_packetized )
868         p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
869     memset( p_null->p_buffer, 0, p_null->i_buffer );
870
871     input_DecoderDecode( p_dec, p_null );
872
873     /* */
874     while( vlc_object_alive( p_dec ) && p_owner->b_flushing )
875         vlc_cond_wait( &p_owner->wait, &p_owner->lock );
876 }
877
878 static void DecoderSignalFlushed( decoder_t *p_dec )
879 {
880     decoder_owner_sys_t *p_owner = p_dec->p_owner;
881
882     vlc_mutex_lock( &p_owner->lock );
883
884     if( p_owner->b_flushing )
885     {
886         p_owner->b_flushing = false;
887         vlc_cond_signal( &p_owner->wait );
888     }
889
890     vlc_mutex_unlock( &p_owner->lock );
891 }
892
893 static void DecoderSignalBuffering( decoder_t *p_dec, bool b_full )
894 {
895     decoder_owner_sys_t *p_owner = p_dec->p_owner;
896
897     vlc_mutex_lock( &p_owner->lock );
898
899     if( p_owner->b_buffering )
900     {
901         if( b_full )
902             p_owner->buffer.b_full = true;
903         vlc_cond_signal( &p_owner->wait );
904     }
905
906     vlc_mutex_unlock( &p_owner->lock );
907 }
908
909 static bool DecoderIsFlushing( decoder_t *p_dec )
910 {
911     decoder_owner_sys_t *p_owner = p_dec->p_owner;
912     bool b_flushing;
913
914     vlc_mutex_lock( &p_owner->lock );
915
916     b_flushing = p_owner->b_flushing;
917
918     vlc_mutex_unlock( &p_owner->lock );
919
920     return b_flushing;
921 }
922
923 static void DecoderWaitUnblock( decoder_t *p_dec, bool *pb_reject )
924 {
925     decoder_owner_sys_t *p_owner = p_dec->p_owner;
926
927     vlc_assert_locked( &p_owner->lock );
928
929     while( !p_owner->b_flushing )
930     {
931         if( p_owner->b_paused )
932         {
933             if( p_owner->b_buffering && !p_owner->buffer.b_full )
934                 break;
935             if( p_owner->pause.i_ignore > 0 )
936             {
937                 p_owner->pause.i_ignore--;
938                 break;
939             }
940         }
941         else
942         {
943             if( !p_owner->b_buffering || !p_owner->buffer.b_full )
944                 break;
945         }
946         vlc_cond_wait( &p_owner->wait, &p_owner->lock );
947     }
948
949     if( pb_reject )
950         *pb_reject = p_owner->b_flushing;
951 }
952
953 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
954 {
955     decoder_owner_sys_t *p_owner = p_dec->p_owner;
956
957     vlc_assert_locked( &p_owner->lock );
958
959     /* XXX only audio and video output have to be paused.
960      * - for sout it is useless
961      * - for subs, it is done by the vout
962      */
963     if( p_dec->fmt_in.i_cat == AUDIO_ES )
964     {
965         if( p_owner->p_aout && p_owner->p_aout_input )
966             aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
967                                  b_paused, i_date );
968     }
969     else if( p_dec->fmt_in.i_cat == VIDEO_ES )
970     {
971         if( p_owner->p_vout )
972             vout_ChangePause( p_owner->p_vout, b_paused, i_date );
973     }
974 }
975 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
976 {
977     if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
978         *pi_preroll = INT64_MAX;
979     else if( p->i_pts > 0 )
980         *pi_preroll = __MIN( *pi_preroll, p->i_pts );
981     else if( p->i_dts > 0 )
982         *pi_preroll = __MIN( *pi_preroll, p->i_dts );
983 }
984
985 static mtime_t DecoderTeletextFixTs( mtime_t i_ts, mtime_t i_ts_delay )
986 {
987     mtime_t current_date = mdate();
988
989     /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
990     if( !i_ts || i_ts > current_date + 10000000 || i_ts < current_date )
991     {
992         /* ETSI EN 300 472 Annex A : do not take into account the PTS
993          * for teletext streams. */
994         return current_date + 400000 + i_ts_delay;
995     }
996     return i_ts;
997 }
998
999 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
1000                           mtime_t *pi_duration, int *pi_rate, mtime_t *pi_delay, bool b_telx )
1001 {
1002     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1003     input_clock_t   *p_clock = p_owner->p_clock;
1004     int i_rate = 0;
1005
1006     vlc_assert_locked( &p_owner->lock );
1007
1008     const mtime_t i_ts_delay = p_owner->p_input->i_pts_delay;
1009     const mtime_t i_es_delay = p_owner->i_ts_delay;
1010
1011     if( p_clock )
1012     {
1013         const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
1014
1015         if( *pi_ts0 > 0 )
1016             *pi_ts0 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
1017                                          *pi_ts0 + i_es_delay );
1018         if( pi_ts1 && *pi_ts1 > 0 )
1019             *pi_ts1 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
1020                                          *pi_ts1 + i_es_delay );
1021
1022         /* Do not create ephemere data because of rounding errors */
1023         if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
1024             *pi_ts1 += 1;
1025
1026         if( i_rate <= 0 )
1027             i_rate = input_clock_GetRate( p_clock ); 
1028
1029         if( pi_duration )
1030             *pi_duration = ( *pi_duration * i_rate +
1031                                     INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
1032
1033         if( pi_rate )
1034             *pi_rate = i_rate;
1035
1036         if( b_telx )
1037         {
1038             *pi_ts0 = DecoderTeletextFixTs( *pi_ts0, i_ts_delay );
1039             if( pi_ts1 && *pi_ts1 <= 0 )
1040                 *pi_ts1 = *pi_ts0;
1041         }
1042     }
1043     if( pi_delay )
1044     {
1045         const int r = i_rate > 0 ? i_rate : INPUT_RATE_DEFAULT;
1046         *pi_delay = i_ts_delay + i_es_delay * r / INPUT_RATE_DEFAULT;
1047     }
1048 }
1049
1050 static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
1051                               int *pi_played_sum, int *pi_lost_sum )
1052 {
1053     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1054     aout_instance_t *p_aout = p_owner->p_aout;
1055     aout_input_t    *p_aout_input = p_owner->p_aout_input;
1056
1057     /* */
1058     if( p_audio->start_date <= 0 )
1059     {
1060         msg_Warn( p_dec, "non-dated audio buffer received" );
1061         *pi_lost_sum += 1;
1062         aout_BufferFree( p_audio );
1063         return;
1064     }
1065
1066     /* */
1067     vlc_mutex_lock( &p_owner->lock );
1068
1069     if( p_owner->b_buffering || p_owner->buffer.p_audio )
1070     {
1071         p_audio->p_next = NULL;
1072
1073         *p_owner->buffer.pp_audio_next = p_audio;
1074         p_owner->buffer.pp_audio_next = &p_audio->p_next;
1075
1076         p_owner->buffer.i_count++;
1077         if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1078             p_audio->start_date - p_owner->buffer.p_audio->start_date > DECODER_MAX_BUFFERING_AUDIO_DURATION )
1079         {
1080             p_owner->buffer.b_full = true;
1081             vlc_cond_signal( &p_owner->wait );
1082         }
1083     }
1084
1085     for( ;; )
1086     {
1087         bool b_has_more = false;
1088         bool b_reject;
1089         DecoderWaitUnblock( p_dec, &b_reject );
1090
1091         if( p_owner->b_buffering )
1092         {
1093             vlc_mutex_unlock( &p_owner->lock );
1094             return;
1095         }
1096
1097         /* */
1098         if( p_owner->buffer.p_audio )
1099         {
1100             p_audio = p_owner->buffer.p_audio;
1101
1102             p_owner->buffer.p_audio = p_audio->p_next;
1103             p_owner->buffer.i_count--;
1104
1105             b_has_more = p_owner->buffer.p_audio != NULL;
1106             if( !b_has_more )
1107                 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1108         }
1109
1110         /* */
1111         int i_rate = INPUT_RATE_DEFAULT;
1112         mtime_t i_delay;
1113
1114         DecoderFixTs( p_dec, &p_audio->start_date, &p_audio->end_date, NULL,
1115                       &i_rate, &i_delay, false );
1116
1117         vlc_mutex_unlock( &p_owner->lock );
1118
1119         /* */
1120         const mtime_t i_max_date = mdate() + i_delay + AOUT_MAX_ADVANCE_TIME;
1121
1122         if( !p_aout || !p_aout_input ||
1123             p_audio->start_date <= 0 || p_audio->start_date > i_max_date ||
1124             i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE ||
1125             i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
1126             b_reject = true;
1127
1128         if( !b_reject )
1129         {
1130             /* Wait if we are too early
1131              * FIXME that's plain ugly to do it here */
1132             mwait( p_audio->start_date - AOUT_MAX_PREPARE_TIME );
1133
1134             if( !aout_DecPlay( p_aout, p_aout_input, p_audio, i_rate ) )
1135                 *pi_played_sum += 1;
1136             *pi_lost_sum += aout_DecGetResetLost( p_aout, p_aout_input );
1137         }
1138         else
1139         {
1140             if( p_audio->start_date <= 0 )
1141             {
1142                 msg_Warn( p_dec, "non-dated audio buffer received" );
1143             }
1144             else if( p_audio->start_date > i_max_date )
1145             {
1146                 msg_Warn( p_aout, "received buffer in the future (%"PRId64")",
1147                           p_audio->start_date - mdate() );
1148             }
1149             *pi_lost_sum += 1;
1150             aout_BufferFree( p_audio );
1151         }
1152
1153         if( !b_has_more )
1154             break;
1155
1156         vlc_mutex_lock( &p_owner->lock );
1157         if( !p_owner->buffer.p_audio )
1158         {
1159             vlc_mutex_unlock( &p_owner->lock );
1160             break;
1161         }
1162     }
1163 }
1164
1165 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1166 {
1167     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1168     input_thread_t  *p_input = p_owner->p_input;
1169     aout_buffer_t   *p_aout_buf;
1170     int i_decoded = 0;
1171     int i_lost = 0;
1172     int i_played = 0;
1173
1174     while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1175     {
1176         aout_instance_t *p_aout = p_owner->p_aout;
1177         aout_input_t    *p_aout_input = p_owner->p_aout_input;
1178         int i_lost = 0;
1179         int i_played;
1180
1181         if( p_dec->b_die )
1182         {
1183             /* It prevent freezing VLC in case of broken decoder */
1184             aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1185             if( p_block )
1186                 block_Release( p_block );
1187             break;
1188         }
1189         i_decoded++;
1190
1191         if( p_aout_buf->start_date < p_owner->i_preroll_end )
1192         {
1193             aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1194             continue;
1195         }
1196
1197         if( p_owner->i_preroll_end > 0 )
1198         {
1199             msg_Dbg( p_dec, "End of audio preroll" );
1200             if( p_owner->p_aout && p_owner->p_aout_input )
1201                 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1202             /* */
1203             p_owner->i_preroll_end = -1;
1204         }
1205
1206         DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1207     }
1208
1209     /* Update ugly stat */
1210     if( i_decoded > 0 || i_lost > 0 || i_played > 0 )
1211     {
1212         vlc_mutex_lock( &p_input->p->counters.counters_lock);
1213
1214         stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
1215                              i_lost, NULL );
1216         stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
1217                              i_played, NULL );
1218         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
1219                              i_decoded, NULL );
1220
1221         vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1222     }
1223 }
1224 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1225 {
1226     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1227     block_t *p_cc;
1228     bool pb_present[4];
1229     int i;
1230     int i_cc_decoder;
1231
1232     assert( p_dec_cc->pf_get_cc != NULL );
1233
1234     /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1235     if( !p_owner->cc.b_supported )
1236         return;
1237
1238     p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1239     if( !p_cc )
1240         return;
1241
1242     vlc_mutex_lock( &p_owner->lock );
1243     for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1244     {
1245         p_owner->cc.pb_present[i] |= pb_present[i];
1246         if( p_owner->cc.pp_decoder[i] )
1247             i_cc_decoder++;
1248     }
1249
1250     for( i = 0; i < 4; i++ )
1251     {
1252         if( !p_owner->cc.pp_decoder[i] )
1253             continue;
1254
1255         if( i_cc_decoder > 1 )
1256             DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
1257         else
1258             DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
1259         i_cc_decoder--;
1260     }
1261     vlc_mutex_unlock( &p_owner->lock );
1262 }
1263
1264 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
1265                               int *pi_played_sum, int *pi_lost_sum )
1266 {
1267     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1268     vout_thread_t  *p_vout = p_owner->p_vout;
1269     bool b_first_buffered;
1270
1271     if( p_picture->date <= 0 )
1272     {
1273         msg_Warn( p_vout, "non-dated video buffer received" );
1274         *pi_lost_sum += 1;
1275         vout_DropPicture( p_vout, p_picture );
1276         return;
1277     }
1278
1279     /* */
1280     vlc_mutex_lock( &p_owner->lock );
1281
1282     if( ( p_owner->b_buffering && !p_owner->buffer.b_first ) || p_owner->buffer.p_picture )
1283     {
1284         p_picture->p_next = NULL;
1285
1286         *p_owner->buffer.pp_picture_next = p_picture;
1287         p_owner->buffer.pp_picture_next = &p_picture->p_next;
1288
1289         p_owner->buffer.i_count++;
1290         if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1291             p_picture->date - p_owner->buffer.p_picture->date > DECODER_MAX_BUFFERING_VIDEO_DURATION )
1292         {
1293             p_owner->buffer.b_full = true;
1294             vlc_cond_signal( &p_owner->wait );
1295         }
1296     }
1297     b_first_buffered = p_owner->buffer.p_picture != NULL;
1298
1299     for( ;; b_first_buffered = false )
1300     {
1301         bool b_has_more = false;
1302
1303         bool b_reject;
1304
1305         DecoderWaitUnblock( p_dec, &b_reject );
1306
1307         if( p_owner->b_buffering && !p_owner->buffer.b_first )
1308         {
1309             vlc_mutex_unlock( &p_owner->lock );
1310             return;
1311         }
1312         bool b_buffering_first = p_owner->b_buffering;
1313
1314         /* */
1315         if( p_owner->buffer.p_picture )
1316         {
1317             p_picture = p_owner->buffer.p_picture;
1318
1319             p_owner->buffer.p_picture = p_picture->p_next;
1320             p_owner->buffer.i_count--;
1321
1322             b_has_more = p_owner->buffer.p_picture != NULL;
1323             if( !b_has_more )
1324                 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1325         }
1326
1327         /* */
1328         int i_rate = INPUT_RATE_DEFAULT;
1329         mtime_t i_delay;
1330
1331         if( b_buffering_first )
1332         {
1333             assert( p_owner->buffer.b_first );
1334             assert( !p_owner->buffer.i_count );
1335             msg_Dbg( p_dec, "Received first picture" );
1336             p_owner->buffer.b_first = false;
1337             p_picture->b_force = true;
1338             i_delay = 0;
1339             if( p_owner->p_clock )
1340                 i_rate = input_clock_GetRate( p_owner->p_clock );
1341         }
1342         DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1343                       &i_rate, &i_delay, false );
1344
1345         vlc_mutex_unlock( &p_owner->lock );
1346
1347         /* */
1348         const mtime_t i_max_date = mdate() + i_delay + DECODER_BOGUS_VIDEO_DELAY;
1349
1350         if( !p_picture->b_force && ( p_picture->date <= 0 || p_picture->date >= i_max_date ) )
1351             b_reject = true;
1352
1353         if( !b_reject )
1354         {
1355             if( i_rate != p_owner->i_last_rate || b_first_buffered )
1356             {
1357                 /* Be sure to not display old picture after our own */
1358                 vout_Flush( p_vout, p_picture->date );
1359                 p_owner->i_last_rate = i_rate;
1360             }
1361             vout_DisplayPicture( p_vout, p_picture );
1362         }
1363         else
1364         {
1365             if( p_picture->date <= 0 )
1366             {
1367                 msg_Warn( p_vout, "non-dated video buffer received" );
1368             }
1369             else
1370             {
1371                 msg_Warn( p_vout, "early picture skipped (%"PRId64")",
1372                           p_picture->date - mdate() );
1373             }
1374             *pi_lost_sum += 1;
1375             vout_DropPicture( p_vout, p_picture );
1376         }
1377         int i_tmp_display;
1378         int i_tmp_lost;
1379         vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1380
1381         *pi_played_sum += i_tmp_display;
1382         *pi_lost_sum += i_tmp_lost;
1383
1384         if( !b_has_more || b_buffering_first )
1385             break;
1386
1387         vlc_mutex_lock( &p_owner->lock );
1388         if( !p_owner->buffer.p_picture )
1389         {
1390             vlc_mutex_unlock( &p_owner->lock );
1391             break;
1392         }
1393     }
1394 }
1395
1396 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1397 {
1398     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1399     input_thread_t *p_input = p_owner->p_input;
1400     picture_t      *p_pic;
1401     int i_lost = 0;
1402     int i_decoded = 0;
1403     int i_displayed = 0;
1404
1405     while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1406     {
1407         vout_thread_t  *p_vout = p_owner->p_vout;
1408         if( p_dec->b_die )
1409         {
1410             /* It prevent freezing VLC in case of broken decoder */
1411             vout_DropPicture( p_vout, p_pic );
1412             if( p_block )
1413                 block_Release( p_block );
1414             break;
1415         }
1416
1417         i_decoded++;
1418
1419         if( p_pic->date < p_owner->i_preroll_end )
1420         {
1421             vout_DropPicture( p_vout, p_pic );
1422             continue;
1423         }
1424
1425         if( p_owner->i_preroll_end > 0 )
1426         {
1427             msg_Dbg( p_dec, "End of video preroll" );
1428             if( p_vout )
1429                 vout_Flush( p_vout, 1 );
1430             /* */
1431             p_owner->i_preroll_end = -1;
1432         }
1433
1434         if( p_dec->pf_get_cc &&
1435             ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1436             DecoderGetCc( p_dec, p_dec );
1437
1438         DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
1439     }
1440     if( i_decoded > 0 || i_lost > 0 || i_displayed > 0 )
1441     {
1442         vlc_mutex_lock( &p_input->p->counters.counters_lock );
1443
1444         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
1445                              i_decoded, NULL );
1446         stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
1447                              i_lost , NULL);
1448
1449         stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
1450                              i_displayed, NULL);
1451
1452         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1453     }
1454 }
1455
1456 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic,
1457                             bool b_telx )
1458 {
1459     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1460     vout_thread_t *p_vout = p_owner->p_spu_vout;
1461
1462     /* */
1463     if( p_subpic->i_start <= 0 )
1464     {
1465         msg_Warn( p_dec, "non-dated spu buffer received" );
1466         subpicture_Delete( p_subpic );
1467         return;
1468     }
1469
1470     /* */
1471     vlc_mutex_lock( &p_owner->lock );
1472
1473     if( p_owner->b_buffering || p_owner->buffer.p_subpic )
1474     {
1475         p_subpic->p_next = NULL;
1476
1477         *p_owner->buffer.pp_subpic_next = p_subpic;
1478         p_owner->buffer.pp_subpic_next = &p_subpic->p_next;
1479
1480         p_owner->buffer.i_count++;
1481         /* XXX it is important to be full after the first one */
1482         if( p_owner->buffer.i_count > 0 )
1483         {
1484             p_owner->buffer.b_full = true;
1485             vlc_cond_signal( &p_owner->wait );
1486         }
1487     }
1488
1489     for( ;; )
1490     {
1491         bool b_has_more = false;
1492         bool b_reject;
1493         DecoderWaitUnblock( p_dec, &b_reject );
1494
1495         if( p_owner->b_buffering )
1496         {
1497             vlc_mutex_unlock( &p_owner->lock );
1498             return;
1499         }
1500
1501         /* */
1502         if( p_owner->buffer.p_subpic )
1503         {
1504             p_subpic = p_owner->buffer.p_subpic;
1505
1506             p_owner->buffer.p_subpic = p_subpic->p_next;
1507             p_owner->buffer.i_count--;
1508
1509             b_has_more = p_owner->buffer.p_subpic != NULL;
1510             if( !b_has_more )
1511                 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1512         }
1513
1514         /* */
1515         DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1516                       NULL, NULL, b_telx );
1517
1518         vlc_mutex_unlock( &p_owner->lock );
1519
1520         if( !b_reject )
1521             spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
1522         else
1523             subpicture_Delete( p_subpic );
1524
1525         if( !b_has_more )
1526             break;
1527         vlc_mutex_lock( &p_owner->lock );
1528         if( !p_owner->buffer.p_subpic )
1529         {
1530             vlc_mutex_unlock( &p_owner->lock );
1531             break;
1532         }
1533     }
1534 }
1535
1536 static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block,
1537                              bool b_telx )
1538 {
1539     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1540
1541     assert( p_owner->p_clock );
1542     assert( !p_sout_block->p_next );
1543
1544     vlc_mutex_lock( &p_owner->lock );
1545
1546     if( p_owner->b_buffering || p_owner->buffer.p_block )
1547     {
1548         block_ChainLastAppend( &p_owner->buffer.pp_block_next, p_sout_block );
1549
1550         p_owner->buffer.i_count++;
1551         /* XXX it is important to be full after the first one */
1552         if( p_owner->buffer.i_count > 0 )
1553         {
1554             p_owner->buffer.b_full = true;
1555             vlc_cond_signal( &p_owner->wait );
1556         }
1557     }
1558
1559     for( ;; )
1560     {
1561         bool b_has_more = false;
1562         bool b_reject;
1563         DecoderWaitUnblock( p_dec, &b_reject );
1564
1565         if( p_owner->b_buffering )
1566         {
1567             vlc_mutex_unlock( &p_owner->lock );
1568             return;
1569         }
1570
1571         /* */
1572         if( p_owner->buffer.p_block )
1573         {
1574             p_sout_block = p_owner->buffer.p_block;
1575
1576             p_owner->buffer.p_block = p_sout_block->p_next;
1577             p_owner->buffer.i_count--;
1578
1579             b_has_more = p_owner->buffer.p_block != NULL;
1580             if( !b_has_more )
1581                 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1582         }
1583         p_sout_block->p_next = NULL;
1584
1585         DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
1586                       &p_sout_block->i_length,
1587                       &p_sout_block->i_rate, NULL, b_telx );
1588
1589         vlc_mutex_unlock( &p_owner->lock );
1590
1591         if( !b_reject )
1592             sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block );
1593         else
1594             block_Release( p_sout_block );
1595
1596         if( !b_has_more )
1597             break;
1598         vlc_mutex_lock( &p_owner->lock );
1599         if( !p_owner->buffer.p_block )
1600         {
1601             vlc_mutex_unlock( &p_owner->lock );
1602             break;
1603         }
1604     }
1605 }
1606
1607 /* */
1608 static void DecoderFlushBuffering( decoder_t *p_dec )
1609 {
1610     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1611
1612     vlc_assert_locked( &p_owner->lock );
1613
1614     while( p_owner->buffer.p_picture )
1615     {
1616         picture_t *p_picture = p_owner->buffer.p_picture;
1617
1618         p_owner->buffer.p_picture = p_picture->p_next;
1619         p_owner->buffer.i_count--;
1620
1621         if( p_owner->p_vout )
1622             vout_DropPicture( p_owner->p_vout, p_picture );
1623
1624         if( !p_owner->buffer.p_picture )
1625             p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1626     }
1627     while( p_owner->buffer.p_audio )
1628     {
1629         aout_buffer_t *p_audio = p_owner->buffer.p_audio;
1630
1631         p_owner->buffer.p_audio = p_audio->p_next;
1632         p_owner->buffer.i_count--;
1633
1634         aout_BufferFree( p_audio );
1635
1636         if( !p_owner->buffer.p_audio )
1637             p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1638     }
1639     while( p_owner->buffer.p_subpic )
1640     {
1641         subpicture_t *p_subpic = p_owner->buffer.p_subpic;
1642
1643         p_owner->buffer.p_subpic = p_subpic->p_next;
1644         p_owner->buffer.i_count--;
1645
1646         subpicture_Delete( p_subpic );
1647
1648         if( !p_owner->buffer.p_subpic )
1649             p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1650     }
1651     if( p_owner->buffer.p_block )
1652     {
1653         block_ChainRelease( p_owner->buffer.p_block );
1654
1655         p_owner->buffer.i_count = 0;
1656         p_owner->buffer.p_block = NULL;
1657         p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1658     }
1659 }
1660
1661 /* This function process a block for sout
1662  */
1663 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1664 {
1665     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1666     const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1667     block_t *p_sout_block;
1668
1669     while( ( p_sout_block =
1670                  p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1671     {
1672         if( !p_owner->p_sout_input )
1673         {
1674             es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1675
1676             p_owner->sout.i_group = p_dec->fmt_in.i_group;
1677             p_owner->sout.i_id = p_dec->fmt_in.i_id;
1678             if( p_dec->fmt_in.psz_language )
1679             {
1680                 if( p_owner->sout.psz_language )
1681                     free( p_owner->sout.psz_language );
1682                 p_owner->sout.psz_language =
1683                     strdup( p_dec->fmt_in.psz_language );
1684             }
1685
1686             p_owner->p_sout_input =
1687                 sout_InputNew( p_owner->p_sout,
1688                                &p_owner->sout );
1689
1690             if( p_owner->p_sout_input == NULL )
1691             {
1692                 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1693                          (char *)&p_owner->sout.i_codec );
1694                 p_dec->b_error = true;
1695
1696                 while( p_sout_block )
1697                 {
1698                     block_t *p_next = p_sout_block->p_next;
1699                     block_Release( p_sout_block );
1700                     p_sout_block = p_next;
1701                 }
1702                 break;
1703             }
1704         }
1705
1706         while( p_sout_block )
1707         {
1708             block_t *p_next = p_sout_block->p_next;
1709
1710             p_sout_block->p_next = NULL;
1711
1712             DecoderPlaySout( p_dec, p_sout_block, b_telx );
1713
1714             p_sout_block = p_next;
1715         }
1716
1717         /* For now it's enough, as only sout impact on this flag */
1718         if( p_owner->p_sout->i_out_pace_nocontrol > 0 &&
1719             p_owner->p_input->p->b_out_pace_control )
1720         {
1721             msg_Dbg( p_dec, "switching to sync mode" );
1722             p_owner->p_input->p->b_out_pace_control = false;
1723         }
1724         else if( p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
1725                  !p_owner->p_input->p->b_out_pace_control )
1726         {
1727             msg_Dbg( p_dec, "switching to async mode" );
1728             p_owner->p_input->p->b_out_pace_control = true;
1729         }
1730     }
1731 }
1732
1733 /* This function process a video block
1734  */
1735 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1736 {
1737     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1738
1739     if( p_owner->p_packetizer )
1740     {
1741         block_t *p_packetized_block;
1742         decoder_t *p_packetizer = p_owner->p_packetizer;
1743
1744         while( (p_packetized_block =
1745                 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1746         {
1747             if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1748             {
1749                 es_format_Clean( &p_dec->fmt_in );
1750                 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1751             }
1752             if( p_packetizer->pf_get_cc )
1753                 DecoderGetCc( p_dec, p_packetizer );
1754
1755             while( p_packetized_block )
1756             {
1757                 block_t *p_next = p_packetized_block->p_next;
1758                 p_packetized_block->p_next = NULL;
1759
1760                 DecoderDecodeVideo( p_dec, p_packetized_block );
1761
1762                 p_packetized_block = p_next;
1763             }
1764         }
1765     }
1766     else if( p_block )
1767     {
1768         DecoderDecodeVideo( p_dec, p_block );
1769     }
1770
1771     if( b_flush && p_owner->p_vout )
1772         vout_Flush( p_owner->p_vout, 1 );
1773 }
1774
1775 /* This function process a audio block
1776  */
1777 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1778 {
1779     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1780
1781     if( p_owner->p_packetizer )
1782     {
1783         block_t *p_packetized_block;
1784         decoder_t *p_packetizer = p_owner->p_packetizer;
1785
1786         while( (p_packetized_block =
1787                 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1788         {
1789             if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1790             {
1791                 es_format_Clean( &p_dec->fmt_in );
1792                 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1793             }
1794
1795             while( p_packetized_block )
1796             {
1797                 block_t *p_next = p_packetized_block->p_next;
1798                 p_packetized_block->p_next = NULL;
1799
1800                 DecoderDecodeAudio( p_dec, p_packetized_block );
1801
1802                 p_packetized_block = p_next;
1803             }
1804         }
1805     }
1806     else if( p_block )
1807     {
1808         DecoderDecodeAudio( p_dec, p_block );
1809     }
1810
1811     if( b_flush && p_owner->p_aout && p_owner->p_aout_input )
1812         aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1813 }
1814
1815 /* This function process a subtitle block
1816  */
1817 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1818 {
1819     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1820     const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1821
1822     input_thread_t *p_input = p_owner->p_input;
1823     vout_thread_t *p_vout;
1824     subpicture_t *p_spu;
1825
1826     while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1827     {
1828         vlc_mutex_lock( &p_input->p->counters.counters_lock );
1829         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1830         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1831
1832         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1833         if( p_vout && p_owner->p_spu_vout == p_vout )
1834         {
1835             /* Preroll does not work very well with subtitle */
1836             if( p_spu->i_start > 0 &&
1837                 p_spu->i_start < p_owner->i_preroll_end &&
1838                 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_owner->i_preroll_end ) )
1839             {
1840                 subpicture_Delete( p_spu );
1841             }
1842             else
1843             {
1844                 DecoderPlaySpu( p_dec, p_spu, b_telx );
1845             }
1846         }
1847         else
1848         {
1849             subpicture_Delete( p_spu );
1850         }
1851         if( p_vout )
1852             vlc_object_release( p_vout );
1853     }
1854
1855     if( b_flush && p_owner->p_spu_vout )
1856     {
1857         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1858
1859         if( p_vout && p_owner->p_spu_vout == p_vout )
1860             spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1861                          p_owner->i_spu_channel );
1862
1863         if( p_vout )
1864             vlc_object_release( p_vout );
1865     }
1866 }
1867
1868 /* */
1869 static void DecoderProcessOnFlush( decoder_t *p_dec )
1870 {
1871     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1872
1873     vlc_mutex_lock( &p_owner->lock );
1874     DecoderFlushBuffering( p_dec );
1875     vlc_mutex_unlock( &p_owner->lock );
1876
1877     DecoderSignalFlushed( p_dec );
1878 }
1879 /**
1880  * Decode a block
1881  *
1882  * \param p_dec the decoder object
1883  * \param p_block the block to decode
1884  * \return VLC_SUCCESS or an error code
1885  */
1886 static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
1887 {
1888     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1889     const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1890
1891     if( p_block && p_block->i_buffer <= 0 )
1892     {
1893         assert( !b_flush_request );
1894         block_Release( p_block );
1895         return;
1896     }
1897
1898 #ifdef ENABLE_SOUT
1899     if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
1900     {
1901         if( p_block )
1902             p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1903
1904         DecoderProcessSout( p_dec, p_block );
1905     }
1906     else
1907 #endif
1908     {
1909         bool b_flush = false;
1910
1911         if( p_block )
1912         {
1913             const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1914             DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1915
1916             b_flush = !b_flushing && b_flush_request;
1917
1918             p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1919         }
1920
1921         if( p_dec->fmt_in.i_cat == AUDIO_ES )
1922         {
1923             DecoderProcessAudio( p_dec, p_block, b_flush );
1924         }
1925         else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1926         {
1927             DecoderProcessVideo( p_dec, p_block, b_flush );
1928         }
1929         else if( p_dec->fmt_in.i_cat == SPU_ES )
1930         {
1931             DecoderProcessSpu( p_dec, p_block, b_flush );
1932         }
1933         else
1934         {
1935             msg_Err( p_dec, "unknown ES format" );
1936             p_dec->b_error = true;
1937         }
1938     }
1939
1940     /* */
1941     if( b_flush_request )
1942         DecoderProcessOnFlush( p_dec );
1943 }
1944
1945 static void DecoderError( decoder_t *p_dec, block_t *p_block )
1946 {
1947     const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1948
1949     /* */
1950     if( p_block )
1951         block_Release( p_block );
1952
1953     if( b_flush_request )
1954         DecoderProcessOnFlush( p_dec );
1955 }
1956
1957
1958 /**
1959  * Destroys a decoder object
1960  *
1961  * \param p_dec the decoder object
1962  * \return nothing
1963  */
1964 static void DeleteDecoder( decoder_t * p_dec )
1965 {
1966     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1967
1968     msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
1969              (char*)&p_dec->fmt_in.i_codec,
1970              (unsigned)block_FifoCount( p_owner->p_fifo ) );
1971
1972     /* Free all packets still in the decoder fifo. */
1973     block_FifoEmpty( p_owner->p_fifo );
1974     block_FifoRelease( p_owner->p_fifo );
1975
1976     /* */
1977     vlc_mutex_lock( &p_owner->lock );
1978     DecoderFlushBuffering( p_dec );
1979     vlc_mutex_unlock( &p_owner->lock );
1980
1981     /* Cleanup */
1982     if( p_owner->p_aout_input )
1983         aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
1984     if( p_owner->p_aout )
1985     {
1986         vlc_object_release( p_owner->p_aout );
1987         p_owner->p_aout = NULL;
1988     }
1989     if( p_owner->p_vout )
1990     {
1991         /* Hack to make sure all the the pictures are freed by the decoder */
1992         vout_FixLeaks( p_owner->p_vout, true );
1993
1994         /* We are about to die. Reattach video output to p_vlc. */
1995         vout_Request( p_dec, p_owner->p_vout, NULL );
1996         input_SendEventVout( p_owner->p_input );
1997     }
1998
1999 #ifdef ENABLE_SOUT
2000     if( p_owner->p_sout_input )
2001     {
2002         sout_InputDelete( p_owner->p_sout_input );
2003         es_format_Clean( &p_owner->sout );
2004     }
2005 #endif
2006
2007     if( p_dec->fmt_in.i_cat == SPU_ES )
2008     {
2009         vout_thread_t *p_vout;
2010
2011         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
2012         if( p_vout && p_owner->p_spu_vout == p_vout )
2013         {
2014             spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
2015                          p_owner->i_spu_channel );
2016             vlc_object_release( p_vout );
2017         }
2018     }
2019
2020     es_format_Clean( &p_dec->fmt_in );
2021     es_format_Clean( &p_dec->fmt_out );
2022
2023     if( p_owner->p_packetizer )
2024     {
2025         module_unneed( p_owner->p_packetizer,
2026                        p_owner->p_packetizer->p_module );
2027         es_format_Clean( &p_owner->p_packetizer->fmt_in );
2028         es_format_Clean( &p_owner->p_packetizer->fmt_out );
2029         vlc_object_detach( p_owner->p_packetizer );
2030         vlc_object_release( p_owner->p_packetizer );
2031     }
2032
2033     vlc_cond_destroy( &p_owner->wait );
2034     vlc_mutex_destroy( &p_owner->lock );
2035
2036     vlc_object_detach( p_dec );
2037
2038     free( p_owner );
2039 }
2040
2041 /*****************************************************************************
2042  * Buffers allocation callbacks for the decoders
2043  *****************************************************************************/
2044 static vout_thread_t *aout_request_vout( void *p_private,
2045                                          vout_thread_t *p_vout, video_format_t *p_fmt )
2046 {
2047     decoder_t *p_dec = p_private;
2048
2049     p_vout =  vout_Request( p_dec, p_vout, p_fmt );
2050     input_SendEventVout( p_dec->p_owner->p_input );
2051
2052     return p_vout;
2053 }
2054
2055 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
2056 {
2057     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2058     aout_buffer_t *p_buffer;
2059
2060     if( p_owner->p_aout_input != NULL &&
2061         ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
2062           p_dec->fmt_out.audio.i_original_channels !=
2063               p_owner->audio.i_original_channels ||
2064           p_dec->fmt_out.audio.i_bytes_per_frame !=
2065               p_owner->audio.i_bytes_per_frame ) )
2066     {
2067         aout_input_t *p_aout_input = p_owner->p_aout_input;
2068
2069         /* Parameters changed, restart the aout */
2070         vlc_mutex_lock( &p_owner->lock );
2071
2072         DecoderFlushBuffering( p_dec );
2073
2074         p_owner->p_aout_input = NULL;
2075         aout_DecDelete( p_owner->p_aout, p_aout_input );
2076
2077         vlc_mutex_unlock( &p_owner->lock );
2078     }
2079
2080     if( p_owner->p_aout_input == NULL )
2081     {
2082         const int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
2083         audio_sample_format_t format;
2084         aout_input_t *p_aout_input;
2085         aout_instance_t *p_aout;
2086         aout_request_vout_t request_vout;
2087
2088         p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
2089         p_owner->audio = p_dec->fmt_out.audio;
2090
2091         memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
2092         if( i_force_dolby &&
2093             (format.i_original_channels&AOUT_CHAN_PHYSMASK) ==
2094                 (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
2095         {
2096             if( i_force_dolby == 1 )
2097             {
2098                 format.i_original_channels = format.i_original_channels |
2099                                              AOUT_CHAN_DOLBYSTEREO;
2100             }
2101             else /* i_force_dolby == 2 */
2102             {
2103                 format.i_original_channels = format.i_original_channels &
2104                                              ~AOUT_CHAN_DOLBYSTEREO;
2105             }
2106         }
2107
2108         request_vout.pf_request_vout = aout_request_vout;
2109         request_vout.p_private = p_dec;
2110
2111         p_aout = p_owner->p_aout;
2112         p_aout_input = aout_DecNew( p_dec, &p_aout,
2113                                     &format, &p_dec->fmt_out.audio_replay_gain, &request_vout );
2114
2115         vlc_mutex_lock( &p_owner->lock );
2116         p_owner->p_aout = p_aout;
2117         p_owner->p_aout_input = p_aout_input;
2118         vlc_mutex_unlock( &p_owner->lock );
2119
2120         if( p_owner->p_aout_input == NULL )
2121         {
2122             msg_Err( p_dec, "failed to create audio output" );
2123             p_dec->b_error = true;
2124             return NULL;
2125         }
2126         p_dec->fmt_out.audio.i_bytes_per_frame =
2127             p_owner->audio.i_bytes_per_frame;
2128     }
2129
2130     p_buffer = aout_DecNewBuffer( p_owner->p_aout_input, i_samples );
2131
2132     return p_buffer;
2133 }
2134
2135 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
2136 {
2137     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2138
2139     aout_DecDeleteBuffer( p_owner->p_aout,
2140                           p_owner->p_aout_input, p_buffer );
2141 }
2142
2143
2144 int vout_CountPictureAvailable( vout_thread_t *p_vout );
2145
2146 static picture_t *vout_new_buffer( decoder_t *p_dec )
2147 {
2148     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2149
2150     if( p_owner->p_vout == NULL ||
2151         p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
2152         p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
2153         p_dec->fmt_out.video.i_chroma != p_owner->video.i_chroma ||
2154         p_dec->fmt_out.video.i_aspect != p_owner->video.i_aspect )
2155     {
2156         vout_thread_t *p_vout;
2157
2158         if( !p_dec->fmt_out.video.i_width ||
2159             !p_dec->fmt_out.video.i_height )
2160         {
2161             /* Can't create a new vout without display size */
2162             return NULL;
2163         }
2164
2165         if( !p_dec->fmt_out.video.i_visible_width ||
2166             !p_dec->fmt_out.video.i_visible_height )
2167         {
2168             if( p_dec->fmt_in.video.i_visible_width &&
2169                 p_dec->fmt_in.video.i_visible_height )
2170             {
2171                 p_dec->fmt_out.video.i_visible_width =
2172                     p_dec->fmt_in.video.i_visible_width;
2173                 p_dec->fmt_out.video.i_visible_height =
2174                     p_dec->fmt_in.video.i_visible_height;
2175             }
2176             else
2177             {
2178                 p_dec->fmt_out.video.i_visible_width =
2179                     p_dec->fmt_out.video.i_width;
2180                 p_dec->fmt_out.video.i_visible_height =
2181                     p_dec->fmt_out.video.i_height;
2182             }
2183         }
2184
2185         if( p_dec->fmt_out.video.i_visible_height == 1088 &&
2186             var_CreateGetBool( p_dec, "hdtv-fix" ) )
2187         {
2188             p_dec->fmt_out.video.i_visible_height = 1080;
2189             p_dec->fmt_out.video.i_sar_num *= 135;
2190             p_dec->fmt_out.video.i_sar_den *= 136;
2191             msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
2192         }
2193
2194         if( !p_dec->fmt_out.video.i_sar_num ||
2195             !p_dec->fmt_out.video.i_sar_den )
2196         {
2197             p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
2198               p_dec->fmt_out.video.i_visible_height;
2199
2200             p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
2201               p_dec->fmt_out.video.i_visible_width;
2202         }
2203
2204         vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
2205                      &p_dec->fmt_out.video.i_sar_den,
2206                      p_dec->fmt_out.video.i_sar_num,
2207                      p_dec->fmt_out.video.i_sar_den, 50000 );
2208
2209         p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
2210         p_owner->video = p_dec->fmt_out.video;
2211
2212         vlc_mutex_lock( &p_owner->lock );
2213
2214         DecoderFlushBuffering( p_dec );
2215
2216         p_vout = p_owner->p_vout;
2217         p_owner->p_vout = NULL;
2218         vlc_mutex_unlock( &p_owner->lock );
2219
2220         p_vout = vout_Request( p_dec, p_vout, &p_dec->fmt_out.video );
2221
2222         vlc_mutex_lock( &p_owner->lock );
2223         p_owner->p_vout = p_vout;
2224         vlc_mutex_unlock( &p_owner->lock );
2225
2226         input_SendEventVout( p_owner->p_input );
2227         if( p_vout == NULL )
2228         {
2229             msg_Err( p_dec, "failed to create video output" );
2230             p_dec->b_error = true;
2231             return NULL;
2232         }
2233
2234         if( p_owner->video.i_rmask )
2235             p_owner->p_vout->render.i_rmask = p_owner->video.i_rmask;
2236         if( p_owner->video.i_gmask )
2237             p_owner->p_vout->render.i_gmask = p_owner->video.i_gmask;
2238         if( p_owner->video.i_bmask )
2239             p_owner->p_vout->render.i_bmask = p_owner->video.i_bmask;
2240     }
2241
2242     /* Get a new picture
2243      */
2244     for( ;; )
2245     {
2246         picture_t *p_picture;
2247
2248         if( p_dec->b_die || p_dec->b_error )
2249             return NULL;
2250
2251         /* The video filter chain required that there is always 1 free buffer
2252          * that it will use as temporary one. It will release the temporary
2253          * buffer once its work is done, so this check is safe even if we don't
2254          * lock around both count() and create().
2255          */
2256         if( vout_CountPictureAvailable( p_owner->p_vout ) >= 2 )
2257         {
2258             p_picture = vout_CreatePicture( p_owner->p_vout, 0, 0, 0 );
2259             if( p_picture )
2260                 return p_picture;
2261         }
2262
2263         if( DecoderIsFlushing( p_dec ) )
2264             return NULL;
2265
2266         /* */
2267         DecoderSignalBuffering( p_dec, true );
2268
2269         /* Check the decoder doesn't leak pictures */
2270         vout_FixLeaks( p_owner->p_vout, false );
2271
2272         /* FIXME add a vout_WaitPictureAvailable (timedwait) */
2273         msleep( VOUT_OUTMEM_SLEEP );
2274     }
2275 }
2276
2277 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
2278 {
2279     vout_DropPicture( p_dec->p_owner->p_vout, p_pic );
2280 }
2281
2282 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
2283 {
2284     vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
2285 }
2286
2287 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
2288 {
2289     vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
2290 }
2291
2292 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2293 {
2294     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2295     vout_thread_t *p_vout = NULL;
2296     subpicture_t *p_subpic;
2297     int i_attempts = 30;
2298
2299     while( i_attempts-- )
2300     {
2301         if( p_dec->b_die || p_dec->b_error )
2302             break;
2303
2304         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
2305         if( p_vout )
2306             break;
2307
2308         msleep( DECODER_SPU_VOUT_WAIT_DURATION );
2309     }
2310
2311     if( !p_vout )
2312     {
2313         msg_Warn( p_dec, "no vout found, dropping subpicture" );
2314         return NULL;
2315     }
2316
2317     if( p_owner->p_spu_vout != p_vout )
2318     {
2319         vlc_mutex_lock( &p_owner->lock );
2320
2321         DecoderFlushBuffering( p_dec );
2322
2323         vlc_mutex_unlock( &p_owner->lock );
2324
2325         spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
2326                      &p_owner->i_spu_channel );
2327         p_owner->i_spu_order = 0;
2328         p_owner->p_spu_vout = p_vout;
2329     }
2330
2331     p_subpic = subpicture_New();
2332     if( p_subpic )
2333     {
2334         p_subpic->i_channel = p_owner->i_spu_channel;
2335         p_subpic->i_order = p_owner->i_spu_order++;
2336         p_subpic->b_subtitle = true;
2337     }
2338
2339     vlc_object_release( p_vout );
2340
2341     return p_subpic;
2342 }
2343
2344 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2345 {
2346     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2347     vout_thread_t *p_vout = NULL;
2348
2349     p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
2350     if( !p_vout || p_owner->p_spu_vout != p_vout )
2351     {
2352         if( p_vout )
2353             vlc_object_release( p_vout );
2354         msg_Warn( p_dec, "no vout found, leaking subpicture" );
2355         return;
2356     }
2357
2358     subpicture_Delete( p_subpic );
2359
2360     vlc_object_release( p_vout );
2361 }
2362