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