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