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