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