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