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