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