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