]> git.sesse.net Git - vlc/blob - src/input/decoder.c
Updated codec informations on decoder output change.
[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 anymore */
339     vlc_mutex_lock( &p_owner->lock );
340     if( p_owner->b_paused || p_owner->b_buffering )
341     {
342         p_owner->b_paused = false;
343         p_owner->b_buffering = false;
344         vlc_cond_signal( &p_owner->wait );
345     }
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 )
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         p_owner->b_fmt_description = false;
617     }
618     vlc_mutex_unlock( &p_owner->lock );
619     return b_changed;
620 }
621
622 /*****************************************************************************
623  * Internal functions
624  *****************************************************************************/
625 static int DecoderGetInputAttachments( decoder_t *p_dec,
626                                        input_attachment_t ***ppp_attachment,
627                                        int *pi_attachment )
628 {
629     return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
630                           ppp_attachment, pi_attachment );
631 }
632 static mtime_t DecoderGetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
633 {
634     decoder_owner_sys_t *p_owner = p_dec->p_owner;
635
636     vlc_mutex_lock( &p_owner->lock );
637     if( p_owner->b_buffering || p_owner->b_paused )
638         i_ts = 0;
639     vlc_mutex_unlock( &p_owner->lock );
640
641     if( !p_owner->p_clock || !i_ts )
642         return i_ts;
643
644     return input_clock_GetTS( p_owner->p_clock, NULL, p_owner->p_input->i_pts_delay, i_ts );
645 }
646 static int DecoderGetDisplayRate( decoder_t *p_dec )
647 {
648     decoder_owner_sys_t *p_owner = p_dec->p_owner;
649
650     if( !p_owner->p_clock )
651         return INPUT_RATE_DEFAULT;
652     return input_clock_GetRate( p_owner->p_clock );
653 }
654
655 /* */
656 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
657 {
658     msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
659              "VLC probably does not support this sound or video format.",
660              (char*)&codec );
661     intf_UserFatal( p_dec, false, _("No suitable decoder module"), 
662                     _("VLC does not support the audio or video format \"%4.4s\". "
663                       "Unfortunately there is no way for you to fix this."), (char*)&codec );
664 }
665
666
667 /**
668  * Create a decoder object
669  *
670  * \param p_input the input thread
671  * \param p_es the es descriptor
672  * \param i_object_type Object type as define in include/vlc_objects.h
673  * \return the decoder object
674  */
675 static decoder_t * CreateDecoder( input_thread_t *p_input,
676                                   es_format_t *fmt, int i_object_type, sout_instance_t *p_sout )
677 {
678     decoder_t *p_dec;
679     decoder_owner_sys_t *p_owner;
680     int i;
681
682     p_dec = vlc_object_create( p_input, i_object_type );
683     if( p_dec == NULL )
684         return NULL;
685
686     p_dec->pf_decode_audio = NULL;
687     p_dec->pf_decode_video = NULL;
688     p_dec->pf_decode_sub = NULL;
689     p_dec->pf_get_cc = NULL;
690     p_dec->pf_packetize = NULL;
691
692     /* Initialize the decoder fifo */
693     p_dec->p_module = NULL;
694
695     memset( &null_es_format, 0, sizeof(es_format_t) );
696     es_format_Copy( &p_dec->fmt_in, fmt );
697     es_format_Copy( &p_dec->fmt_out, &null_es_format );
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         if( !b_reject )
1154         {
1155             /* Wait if we are too early
1156              * FIXME that's plain ugly to do it here */
1157             mwait( p_audio->start_date - AOUT_MAX_PREPARE_TIME );
1158
1159             if( !aout_DecPlay( p_aout, p_aout_input, p_audio, i_rate ) )
1160                 *pi_played_sum += 1;
1161             *pi_lost_sum += aout_DecGetResetLost( p_aout, p_aout_input );
1162         }
1163         else
1164         {
1165             if( p_audio->start_date <= 0 )
1166             {
1167                 msg_Warn( p_dec, "non-dated audio buffer received" );
1168             }
1169             else if( p_audio->start_date > i_max_date )
1170             {
1171                 msg_Warn( p_aout, "received buffer in the future (%"PRId64")",
1172                           p_audio->start_date - mdate() );
1173             }
1174             *pi_lost_sum += 1;
1175             aout_BufferFree( p_audio );
1176         }
1177
1178         if( !b_has_more )
1179             break;
1180
1181         vlc_mutex_lock( &p_owner->lock );
1182         if( !p_owner->buffer.p_audio )
1183         {
1184             vlc_mutex_unlock( &p_owner->lock );
1185             break;
1186         }
1187     }
1188 }
1189
1190 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1191 {
1192     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1193     input_thread_t  *p_input = p_owner->p_input;
1194     aout_buffer_t   *p_aout_buf;
1195     int i_decoded = 0;
1196     int i_lost = 0;
1197     int i_played = 0;
1198
1199     while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1200     {
1201         aout_instance_t *p_aout = p_owner->p_aout;
1202         aout_input_t    *p_aout_input = p_owner->p_aout_input;
1203         int i_lost = 0;
1204         int i_played;
1205
1206         if( p_dec->b_die )
1207         {
1208             /* It prevent freezing VLC in case of broken decoder */
1209             aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1210             if( p_block )
1211                 block_Release( p_block );
1212             break;
1213         }
1214         i_decoded++;
1215
1216         if( p_aout_buf->start_date < p_owner->i_preroll_end )
1217         {
1218             aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1219             continue;
1220         }
1221
1222         if( p_owner->i_preroll_end > 0 )
1223         {
1224             msg_Dbg( p_dec, "End of audio preroll" );
1225             if( p_owner->p_aout && p_owner->p_aout_input )
1226                 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1227             /* */
1228             p_owner->i_preroll_end = -1;
1229         }
1230
1231         DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1232     }
1233
1234     /* Update ugly stat */
1235     if( i_decoded > 0 || i_lost > 0 || i_played > 0 )
1236     {
1237         vlc_mutex_lock( &p_input->p->counters.counters_lock);
1238
1239         stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
1240                              i_lost, NULL );
1241         stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
1242                              i_played, NULL );
1243         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
1244                              i_decoded, NULL );
1245
1246         vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1247     }
1248 }
1249 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1250 {
1251     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1252     block_t *p_cc;
1253     bool pb_present[4];
1254     int i;
1255     int i_cc_decoder;
1256
1257     assert( p_dec_cc->pf_get_cc != NULL );
1258
1259     /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1260     if( !p_owner->cc.b_supported )
1261         return;
1262
1263     p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1264     if( !p_cc )
1265         return;
1266
1267     vlc_mutex_lock( &p_owner->lock );
1268     for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1269     {
1270         p_owner->cc.pb_present[i] |= pb_present[i];
1271         if( p_owner->cc.pp_decoder[i] )
1272             i_cc_decoder++;
1273     }
1274
1275     for( i = 0; i < 4; i++ )
1276     {
1277         if( !p_owner->cc.pp_decoder[i] )
1278             continue;
1279
1280         if( i_cc_decoder > 1 )
1281             DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
1282         else
1283             DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
1284         i_cc_decoder--;
1285     }
1286     vlc_mutex_unlock( &p_owner->lock );
1287 }
1288
1289 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
1290                               int *pi_played_sum, int *pi_lost_sum )
1291 {
1292     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1293     vout_thread_t  *p_vout = p_owner->p_vout;
1294     bool b_first_buffered;
1295
1296     if( p_picture->date <= 0 )
1297     {
1298         msg_Warn( p_vout, "non-dated video buffer received" );
1299         *pi_lost_sum += 1;
1300         vout_DropPicture( p_vout, p_picture );
1301         return;
1302     }
1303
1304     /* */
1305     vlc_mutex_lock( &p_owner->lock );
1306
1307     if( ( p_owner->b_buffering && !p_owner->buffer.b_first ) || p_owner->buffer.p_picture )
1308     {
1309         p_picture->p_next = NULL;
1310
1311         *p_owner->buffer.pp_picture_next = p_picture;
1312         p_owner->buffer.pp_picture_next = &p_picture->p_next;
1313
1314         p_owner->buffer.i_count++;
1315         if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1316             p_picture->date - p_owner->buffer.p_picture->date > DECODER_MAX_BUFFERING_VIDEO_DURATION )
1317         {
1318             p_owner->buffer.b_full = true;
1319             vlc_cond_signal( &p_owner->wait );
1320         }
1321     }
1322     b_first_buffered = p_owner->buffer.p_picture != NULL;
1323
1324     for( ;; b_first_buffered = false )
1325     {
1326         bool b_has_more = false;
1327
1328         bool b_reject;
1329
1330         DecoderWaitUnblock( p_dec, &b_reject );
1331
1332         if( p_owner->b_buffering && !p_owner->buffer.b_first )
1333         {
1334             vlc_mutex_unlock( &p_owner->lock );
1335             return;
1336         }
1337         bool b_buffering_first = p_owner->b_buffering;
1338
1339         /* */
1340         if( p_owner->buffer.p_picture )
1341         {
1342             p_picture = p_owner->buffer.p_picture;
1343
1344             p_owner->buffer.p_picture = p_picture->p_next;
1345             p_owner->buffer.i_count--;
1346
1347             b_has_more = p_owner->buffer.p_picture != NULL;
1348             if( !b_has_more )
1349                 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1350         }
1351
1352         /* */
1353         int i_rate = INPUT_RATE_DEFAULT;
1354         mtime_t i_delay;
1355
1356         if( b_buffering_first )
1357         {
1358             assert( p_owner->buffer.b_first );
1359             assert( !p_owner->buffer.i_count );
1360             msg_Dbg( p_dec, "Received first picture" );
1361             p_owner->buffer.b_first = false;
1362             p_picture->b_force = true;
1363             i_delay = 0;
1364             if( p_owner->p_clock )
1365                 i_rate = input_clock_GetRate( p_owner->p_clock );
1366         }
1367         DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1368                       &i_rate, &i_delay, false );
1369
1370         vlc_mutex_unlock( &p_owner->lock );
1371
1372         /* */
1373         const mtime_t i_max_date = mdate() + i_delay + DECODER_BOGUS_VIDEO_DELAY;
1374
1375         if( !p_picture->b_force && ( p_picture->date <= 0 || p_picture->date >= i_max_date ) )
1376             b_reject = true;
1377
1378         if( !b_reject )
1379         {
1380             if( i_rate != p_owner->i_last_rate || b_first_buffered )
1381             {
1382                 /* Be sure to not display old picture after our own */
1383                 vout_Flush( p_vout, p_picture->date );
1384                 p_owner->i_last_rate = i_rate;
1385             }
1386             vout_DisplayPicture( p_vout, p_picture );
1387         }
1388         else
1389         {
1390             if( p_picture->date <= 0 )
1391             {
1392                 msg_Warn( p_vout, "non-dated video buffer received" );
1393             }
1394             else
1395             {
1396                 msg_Warn( p_vout, "early picture skipped (%"PRId64")",
1397                           p_picture->date - mdate() );
1398             }
1399             *pi_lost_sum += 1;
1400             vout_DropPicture( p_vout, p_picture );
1401         }
1402         int i_tmp_display;
1403         int i_tmp_lost;
1404         vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1405
1406         *pi_played_sum += i_tmp_display;
1407         *pi_lost_sum += i_tmp_lost;
1408
1409         if( !b_has_more || b_buffering_first )
1410             break;
1411
1412         vlc_mutex_lock( &p_owner->lock );
1413         if( !p_owner->buffer.p_picture )
1414         {
1415             vlc_mutex_unlock( &p_owner->lock );
1416             break;
1417         }
1418     }
1419 }
1420
1421 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1422 {
1423     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1424     input_thread_t *p_input = p_owner->p_input;
1425     picture_t      *p_pic;
1426     int i_lost = 0;
1427     int i_decoded = 0;
1428     int i_displayed = 0;
1429
1430     while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1431     {
1432         vout_thread_t  *p_vout = p_owner->p_vout;
1433         if( p_dec->b_die )
1434         {
1435             /* It prevent freezing VLC in case of broken decoder */
1436             vout_DropPicture( p_vout, p_pic );
1437             if( p_block )
1438                 block_Release( p_block );
1439             break;
1440         }
1441
1442         i_decoded++;
1443
1444         if( p_pic->date < p_owner->i_preroll_end )
1445         {
1446             vout_DropPicture( p_vout, p_pic );
1447             continue;
1448         }
1449
1450         if( p_owner->i_preroll_end > 0 )
1451         {
1452             msg_Dbg( p_dec, "End of video preroll" );
1453             if( p_vout )
1454                 vout_Flush( p_vout, 1 );
1455             /* */
1456             p_owner->i_preroll_end = -1;
1457         }
1458
1459         if( p_dec->pf_get_cc &&
1460             ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1461             DecoderGetCc( p_dec, p_dec );
1462
1463         DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
1464     }
1465     if( i_decoded > 0 || i_lost > 0 || i_displayed > 0 )
1466     {
1467         vlc_mutex_lock( &p_input->p->counters.counters_lock );
1468
1469         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
1470                              i_decoded, NULL );
1471         stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
1472                              i_lost , NULL);
1473
1474         stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
1475                              i_displayed, NULL);
1476
1477         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1478     }
1479 }
1480
1481 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic,
1482                             bool b_telx )
1483 {
1484     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1485     vout_thread_t *p_vout = p_owner->p_spu_vout;
1486
1487     /* */
1488     if( p_subpic->i_start <= 0 )
1489     {
1490         msg_Warn( p_dec, "non-dated spu buffer received" );
1491         subpicture_Delete( p_subpic );
1492         return;
1493     }
1494
1495     /* */
1496     vlc_mutex_lock( &p_owner->lock );
1497
1498     if( p_owner->b_buffering || p_owner->buffer.p_subpic )
1499     {
1500         p_subpic->p_next = NULL;
1501
1502         *p_owner->buffer.pp_subpic_next = p_subpic;
1503         p_owner->buffer.pp_subpic_next = &p_subpic->p_next;
1504
1505         p_owner->buffer.i_count++;
1506         /* XXX it is important to be full after the first one */
1507         if( p_owner->buffer.i_count > 0 )
1508         {
1509             p_owner->buffer.b_full = true;
1510             vlc_cond_signal( &p_owner->wait );
1511         }
1512     }
1513
1514     for( ;; )
1515     {
1516         bool b_has_more = false;
1517         bool b_reject;
1518         DecoderWaitUnblock( p_dec, &b_reject );
1519
1520         if( p_owner->b_buffering )
1521         {
1522             vlc_mutex_unlock( &p_owner->lock );
1523             return;
1524         }
1525
1526         /* */
1527         if( p_owner->buffer.p_subpic )
1528         {
1529             p_subpic = p_owner->buffer.p_subpic;
1530
1531             p_owner->buffer.p_subpic = p_subpic->p_next;
1532             p_owner->buffer.i_count--;
1533
1534             b_has_more = p_owner->buffer.p_subpic != NULL;
1535             if( !b_has_more )
1536                 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1537         }
1538
1539         /* */
1540         DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1541                       NULL, NULL, b_telx );
1542
1543         vlc_mutex_unlock( &p_owner->lock );
1544
1545         if( !b_reject )
1546             spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
1547         else
1548             subpicture_Delete( p_subpic );
1549
1550         if( !b_has_more )
1551             break;
1552         vlc_mutex_lock( &p_owner->lock );
1553         if( !p_owner->buffer.p_subpic )
1554         {
1555             vlc_mutex_unlock( &p_owner->lock );
1556             break;
1557         }
1558     }
1559 }
1560
1561 static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block,
1562                              bool b_telx )
1563 {
1564     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1565
1566     assert( p_owner->p_clock );
1567     assert( !p_sout_block->p_next );
1568
1569     vlc_mutex_lock( &p_owner->lock );
1570
1571     if( p_owner->b_buffering || p_owner->buffer.p_block )
1572     {
1573         block_ChainLastAppend( &p_owner->buffer.pp_block_next, p_sout_block );
1574
1575         p_owner->buffer.i_count++;
1576         /* XXX it is important to be full after the first one */
1577         if( p_owner->buffer.i_count > 0 )
1578         {
1579             p_owner->buffer.b_full = true;
1580             vlc_cond_signal( &p_owner->wait );
1581         }
1582     }
1583
1584     for( ;; )
1585     {
1586         bool b_has_more = false;
1587         bool b_reject;
1588         DecoderWaitUnblock( p_dec, &b_reject );
1589
1590         if( p_owner->b_buffering )
1591         {
1592             vlc_mutex_unlock( &p_owner->lock );
1593             return;
1594         }
1595
1596         /* */
1597         if( p_owner->buffer.p_block )
1598         {
1599             p_sout_block = p_owner->buffer.p_block;
1600
1601             p_owner->buffer.p_block = p_sout_block->p_next;
1602             p_owner->buffer.i_count--;
1603
1604             b_has_more = p_owner->buffer.p_block != NULL;
1605             if( !b_has_more )
1606                 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1607         }
1608         p_sout_block->p_next = NULL;
1609
1610         DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
1611                       &p_sout_block->i_length,
1612                       &p_sout_block->i_rate, NULL, b_telx );
1613
1614         vlc_mutex_unlock( &p_owner->lock );
1615
1616         if( !b_reject )
1617             sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block );
1618         else
1619             block_Release( p_sout_block );
1620
1621         if( !b_has_more )
1622             break;
1623         vlc_mutex_lock( &p_owner->lock );
1624         if( !p_owner->buffer.p_block )
1625         {
1626             vlc_mutex_unlock( &p_owner->lock );
1627             break;
1628         }
1629     }
1630 }
1631
1632 /* */
1633 static void DecoderFlushBuffering( decoder_t *p_dec )
1634 {
1635     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1636
1637     vlc_assert_locked( &p_owner->lock );
1638
1639     while( p_owner->buffer.p_picture )
1640     {
1641         picture_t *p_picture = p_owner->buffer.p_picture;
1642
1643         p_owner->buffer.p_picture = p_picture->p_next;
1644         p_owner->buffer.i_count--;
1645
1646         if( p_owner->p_vout )
1647             vout_DropPicture( p_owner->p_vout, p_picture );
1648
1649         if( !p_owner->buffer.p_picture )
1650             p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1651     }
1652     while( p_owner->buffer.p_audio )
1653     {
1654         aout_buffer_t *p_audio = p_owner->buffer.p_audio;
1655
1656         p_owner->buffer.p_audio = p_audio->p_next;
1657         p_owner->buffer.i_count--;
1658
1659         aout_BufferFree( p_audio );
1660
1661         if( !p_owner->buffer.p_audio )
1662             p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1663     }
1664     while( p_owner->buffer.p_subpic )
1665     {
1666         subpicture_t *p_subpic = p_owner->buffer.p_subpic;
1667
1668         p_owner->buffer.p_subpic = p_subpic->p_next;
1669         p_owner->buffer.i_count--;
1670
1671         subpicture_Delete( p_subpic );
1672
1673         if( !p_owner->buffer.p_subpic )
1674             p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1675     }
1676     if( p_owner->buffer.p_block )
1677     {
1678         block_ChainRelease( p_owner->buffer.p_block );
1679
1680         p_owner->buffer.i_count = 0;
1681         p_owner->buffer.p_block = NULL;
1682         p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1683     }
1684 }
1685
1686 /* This function process a block for sout
1687  */
1688 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1689 {
1690     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1691     const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1692     block_t *p_sout_block;
1693
1694     while( ( p_sout_block =
1695                  p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1696     {
1697         if( !p_owner->p_sout_input )
1698         {
1699             es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1700
1701             p_owner->sout.i_group = p_dec->fmt_in.i_group;
1702             p_owner->sout.i_id = p_dec->fmt_in.i_id;
1703             if( p_dec->fmt_in.psz_language )
1704             {
1705                 if( p_owner->sout.psz_language )
1706                     free( p_owner->sout.psz_language );
1707                 p_owner->sout.psz_language =
1708                     strdup( p_dec->fmt_in.psz_language );
1709             }
1710
1711             p_owner->p_sout_input =
1712                 sout_InputNew( p_owner->p_sout,
1713                                &p_owner->sout );
1714
1715             if( p_owner->p_sout_input == NULL )
1716             {
1717                 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1718                          (char *)&p_owner->sout.i_codec );
1719                 p_dec->b_error = true;
1720
1721                 while( p_sout_block )
1722                 {
1723                     block_t *p_next = p_sout_block->p_next;
1724                     block_Release( p_sout_block );
1725                     p_sout_block = p_next;
1726                 }
1727                 break;
1728             }
1729         }
1730
1731         while( p_sout_block )
1732         {
1733             block_t *p_next = p_sout_block->p_next;
1734
1735             p_sout_block->p_next = NULL;
1736
1737             DecoderPlaySout( p_dec, p_sout_block, b_telx );
1738
1739             p_sout_block = p_next;
1740         }
1741
1742         /* For now it's enough, as only sout impact on this flag */
1743         if( p_owner->p_sout->i_out_pace_nocontrol > 0 &&
1744             p_owner->p_input->p->b_out_pace_control )
1745         {
1746             msg_Dbg( p_dec, "switching to sync mode" );
1747             p_owner->p_input->p->b_out_pace_control = false;
1748         }
1749         else if( p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
1750                  !p_owner->p_input->p->b_out_pace_control )
1751         {
1752             msg_Dbg( p_dec, "switching to async mode" );
1753             p_owner->p_input->p->b_out_pace_control = true;
1754         }
1755     }
1756 }
1757
1758 /* This function process a video block
1759  */
1760 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1761 {
1762     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1763
1764     if( p_owner->p_packetizer )
1765     {
1766         block_t *p_packetized_block;
1767         decoder_t *p_packetizer = p_owner->p_packetizer;
1768
1769         while( (p_packetized_block =
1770                 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1771         {
1772             if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1773             {
1774                 es_format_Clean( &p_dec->fmt_in );
1775                 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1776             }
1777             if( p_packetizer->pf_get_cc )
1778                 DecoderGetCc( p_dec, p_packetizer );
1779
1780             while( p_packetized_block )
1781             {
1782                 block_t *p_next = p_packetized_block->p_next;
1783                 p_packetized_block->p_next = NULL;
1784
1785                 DecoderDecodeVideo( p_dec, p_packetized_block );
1786
1787                 p_packetized_block = p_next;
1788             }
1789         }
1790     }
1791     else if( p_block )
1792     {
1793         DecoderDecodeVideo( p_dec, p_block );
1794     }
1795
1796     if( b_flush && p_owner->p_vout )
1797         vout_Flush( p_owner->p_vout, 1 );
1798 }
1799
1800 /* This function process a audio block
1801  */
1802 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1803 {
1804     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1805
1806     if( p_owner->p_packetizer )
1807     {
1808         block_t *p_packetized_block;
1809         decoder_t *p_packetizer = p_owner->p_packetizer;
1810
1811         while( (p_packetized_block =
1812                 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1813         {
1814             if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1815             {
1816                 es_format_Clean( &p_dec->fmt_in );
1817                 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1818             }
1819
1820             while( p_packetized_block )
1821             {
1822                 block_t *p_next = p_packetized_block->p_next;
1823                 p_packetized_block->p_next = NULL;
1824
1825                 DecoderDecodeAudio( p_dec, p_packetized_block );
1826
1827                 p_packetized_block = p_next;
1828             }
1829         }
1830     }
1831     else if( p_block )
1832     {
1833         DecoderDecodeAudio( p_dec, p_block );
1834     }
1835
1836     if( b_flush && p_owner->p_aout && p_owner->p_aout_input )
1837         aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1838 }
1839
1840 /* This function process a subtitle block
1841  */
1842 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1843 {
1844     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1845     const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1846
1847     input_thread_t *p_input = p_owner->p_input;
1848     vout_thread_t *p_vout;
1849     subpicture_t *p_spu;
1850
1851     while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1852     {
1853         vlc_mutex_lock( &p_input->p->counters.counters_lock );
1854         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1855         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1856
1857         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1858         if( p_vout && p_owner->p_spu_vout == p_vout )
1859         {
1860             /* Preroll does not work very well with subtitle */
1861             if( p_spu->i_start > 0 &&
1862                 p_spu->i_start < p_owner->i_preroll_end &&
1863                 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_owner->i_preroll_end ) )
1864             {
1865                 subpicture_Delete( p_spu );
1866             }
1867             else
1868             {
1869                 DecoderPlaySpu( p_dec, p_spu, b_telx );
1870             }
1871         }
1872         else
1873         {
1874             subpicture_Delete( p_spu );
1875         }
1876         if( p_vout )
1877             vlc_object_release( p_vout );
1878     }
1879
1880     if( b_flush && p_owner->p_spu_vout )
1881     {
1882         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1883
1884         if( p_vout && p_owner->p_spu_vout == p_vout )
1885             spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1886                          p_owner->i_spu_channel );
1887
1888         if( p_vout )
1889             vlc_object_release( p_vout );
1890     }
1891 }
1892
1893 /* */
1894 static void DecoderProcessOnFlush( decoder_t *p_dec )
1895 {
1896     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1897
1898     vlc_mutex_lock( &p_owner->lock );
1899     DecoderFlushBuffering( p_dec );
1900     vlc_mutex_unlock( &p_owner->lock );
1901
1902     DecoderSignalFlushed( p_dec );
1903 }
1904 /**
1905  * Decode a block
1906  *
1907  * \param p_dec the decoder object
1908  * \param p_block the block to decode
1909  * \return VLC_SUCCESS or an error code
1910  */
1911 static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
1912 {
1913     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1914     const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1915
1916     if( p_block && p_block->i_buffer <= 0 )
1917     {
1918         assert( !b_flush_request );
1919         block_Release( p_block );
1920         return;
1921     }
1922
1923 #ifdef ENABLE_SOUT
1924     if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
1925     {
1926         if( p_block )
1927             p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1928
1929         DecoderProcessSout( p_dec, p_block );
1930     }
1931     else
1932 #endif
1933     {
1934         bool b_flush = false;
1935
1936         if( p_block )
1937         {
1938             const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1939             DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1940
1941             b_flush = !b_flushing && b_flush_request;
1942
1943             p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1944         }
1945
1946         if( p_dec->fmt_in.i_cat == AUDIO_ES )
1947         {
1948             DecoderProcessAudio( p_dec, p_block, b_flush );
1949         }
1950         else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1951         {
1952             DecoderProcessVideo( p_dec, p_block, b_flush );
1953         }
1954         else if( p_dec->fmt_in.i_cat == SPU_ES )
1955         {
1956             DecoderProcessSpu( p_dec, p_block, b_flush );
1957         }
1958         else
1959         {
1960             msg_Err( p_dec, "unknown ES format" );
1961             p_dec->b_error = true;
1962         }
1963     }
1964
1965     /* */
1966     if( b_flush_request )
1967         DecoderProcessOnFlush( p_dec );
1968 }
1969
1970 static void DecoderError( decoder_t *p_dec, block_t *p_block )
1971 {
1972     const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1973
1974     /* */
1975     if( p_block )
1976         block_Release( p_block );
1977
1978     if( b_flush_request )
1979         DecoderProcessOnFlush( p_dec );
1980 }
1981
1982
1983 /**
1984  * Destroys a decoder object
1985  *
1986  * \param p_dec the decoder object
1987  * \return nothing
1988  */
1989 static void DeleteDecoder( decoder_t * p_dec )
1990 {
1991     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1992
1993     msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
1994              (char*)&p_dec->fmt_in.i_codec,
1995              (unsigned)block_FifoCount( p_owner->p_fifo ) );
1996
1997     /* Free all packets still in the decoder fifo. */
1998     block_FifoEmpty( p_owner->p_fifo );
1999     block_FifoRelease( p_owner->p_fifo );
2000
2001     /* */
2002     vlc_mutex_lock( &p_owner->lock );
2003     DecoderFlushBuffering( p_dec );
2004     vlc_mutex_unlock( &p_owner->lock );
2005
2006     /* Cleanup */
2007     if( p_owner->p_aout_input )
2008         aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
2009     if( p_owner->p_aout )
2010     {
2011         vlc_object_release( p_owner->p_aout );
2012         p_owner->p_aout = NULL;
2013     }
2014     if( p_owner->p_vout )
2015     {
2016         /* Hack to make sure all the the pictures are freed by the decoder */
2017         vout_FixLeaks( p_owner->p_vout, true );
2018
2019         /* We are about to die. Reattach video output to p_vlc. */
2020         vout_Request( p_dec, p_owner->p_vout, NULL );
2021         input_SendEventVout( p_owner->p_input );
2022     }
2023
2024 #ifdef ENABLE_SOUT
2025     if( p_owner->p_sout_input )
2026     {
2027         sout_InputDelete( p_owner->p_sout_input );
2028         es_format_Clean( &p_owner->sout );
2029     }
2030 #endif
2031
2032     if( p_dec->fmt_in.i_cat == SPU_ES )
2033     {
2034         vout_thread_t *p_vout;
2035
2036         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
2037         if( p_vout && p_owner->p_spu_vout == p_vout )
2038         {
2039             spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
2040                          p_owner->i_spu_channel );
2041             vlc_object_release( p_vout );
2042         }
2043     }
2044
2045     es_format_Clean( &p_dec->fmt_in );
2046     es_format_Clean( &p_dec->fmt_out );
2047
2048     if( p_owner->p_packetizer )
2049     {
2050         module_unneed( p_owner->p_packetizer,
2051                        p_owner->p_packetizer->p_module );
2052         es_format_Clean( &p_owner->p_packetizer->fmt_in );
2053         es_format_Clean( &p_owner->p_packetizer->fmt_out );
2054         vlc_object_detach( p_owner->p_packetizer );
2055         vlc_object_release( p_owner->p_packetizer );
2056     }
2057
2058     es_format_Clean( &p_owner->fmt_description );
2059
2060     vlc_cond_destroy( &p_owner->wait );
2061     vlc_mutex_destroy( &p_owner->lock );
2062
2063     vlc_object_detach( p_dec );
2064
2065     free( p_owner );
2066 }
2067
2068 /*****************************************************************************
2069  * Buffers allocation callbacks for the decoders
2070  *****************************************************************************/
2071 static void DecoderUpdateFormatLocked( decoder_t *p_dec )
2072 {
2073     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2074
2075     vlc_assert_locked( &p_owner->lock );
2076
2077     es_format_Clean( &p_owner->fmt_description );
2078
2079     p_owner->b_fmt_description = true;
2080     es_format_Copy( &p_owner->fmt_description, &p_dec->fmt_out );
2081 }
2082 static vout_thread_t *aout_request_vout( void *p_private,
2083                                          vout_thread_t *p_vout, video_format_t *p_fmt )
2084 {
2085     decoder_t *p_dec = p_private;
2086
2087     p_vout =  vout_Request( p_dec, p_vout, p_fmt );
2088     input_SendEventVout( p_dec->p_owner->p_input );
2089
2090     return p_vout;
2091 }
2092
2093 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
2094 {
2095     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2096     aout_buffer_t *p_buffer;
2097
2098     if( p_owner->p_aout_input != NULL &&
2099         ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
2100           p_dec->fmt_out.audio.i_original_channels !=
2101               p_owner->audio.i_original_channels ||
2102           p_dec->fmt_out.audio.i_bytes_per_frame !=
2103               p_owner->audio.i_bytes_per_frame ) )
2104     {
2105         aout_input_t *p_aout_input = p_owner->p_aout_input;
2106
2107         /* Parameters changed, restart the aout */
2108         vlc_mutex_lock( &p_owner->lock );
2109
2110         DecoderFlushBuffering( p_dec );
2111
2112         p_owner->p_aout_input = NULL;
2113         aout_DecDelete( p_owner->p_aout, p_aout_input );
2114
2115         vlc_mutex_unlock( &p_owner->lock );
2116     }
2117
2118     if( p_owner->p_aout_input == NULL )
2119     {
2120         const int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
2121         audio_sample_format_t format;
2122         aout_input_t *p_aout_input;
2123         aout_instance_t *p_aout;
2124         aout_request_vout_t request_vout;
2125
2126         p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
2127         p_owner->audio = p_dec->fmt_out.audio;
2128
2129         memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
2130         if( i_force_dolby &&
2131             (format.i_original_channels&AOUT_CHAN_PHYSMASK) ==
2132                 (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
2133         {
2134             if( i_force_dolby == 1 )
2135             {
2136                 format.i_original_channels = format.i_original_channels |
2137                                              AOUT_CHAN_DOLBYSTEREO;
2138             }
2139             else /* i_force_dolby == 2 */
2140             {
2141                 format.i_original_channels = format.i_original_channels &
2142                                              ~AOUT_CHAN_DOLBYSTEREO;
2143             }
2144         }
2145
2146         request_vout.pf_request_vout = aout_request_vout;
2147         request_vout.p_private = p_dec;
2148
2149         p_aout = p_owner->p_aout;
2150         p_aout_input = aout_DecNew( p_dec, &p_aout,
2151                                     &format, &p_dec->fmt_out.audio_replay_gain, &request_vout );
2152
2153         vlc_mutex_lock( &p_owner->lock );
2154
2155         p_owner->p_aout = p_aout;
2156         p_owner->p_aout_input = p_aout_input;
2157         DecoderUpdateFormatLocked( p_dec );
2158
2159         vlc_mutex_unlock( &p_owner->lock );
2160
2161         if( p_owner->p_aout_input == NULL )
2162         {
2163             msg_Err( p_dec, "failed to create audio output" );
2164             p_dec->b_error = true;
2165             return NULL;
2166         }
2167         p_dec->fmt_out.audio.i_bytes_per_frame =
2168             p_owner->audio.i_bytes_per_frame;
2169     }
2170
2171     p_buffer = aout_DecNewBuffer( p_owner->p_aout_input, i_samples );
2172
2173     return p_buffer;
2174 }
2175
2176 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
2177 {
2178     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2179
2180     aout_DecDeleteBuffer( p_owner->p_aout,
2181                           p_owner->p_aout_input, p_buffer );
2182 }
2183
2184
2185 int vout_CountPictureAvailable( vout_thread_t *p_vout );
2186
2187 static picture_t *vout_new_buffer( decoder_t *p_dec )
2188 {
2189     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2190
2191     if( p_owner->p_vout == NULL ||
2192         p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
2193         p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
2194         p_dec->fmt_out.video.i_chroma != p_owner->video.i_chroma ||
2195         p_dec->fmt_out.video.i_aspect != p_owner->video.i_aspect )
2196     {
2197         vout_thread_t *p_vout;
2198
2199         if( !p_dec->fmt_out.video.i_width ||
2200             !p_dec->fmt_out.video.i_height )
2201         {
2202             /* Can't create a new vout without display size */
2203             return NULL;
2204         }
2205
2206         if( !p_dec->fmt_out.video.i_visible_width ||
2207             !p_dec->fmt_out.video.i_visible_height )
2208         {
2209             if( p_dec->fmt_in.video.i_visible_width &&
2210                 p_dec->fmt_in.video.i_visible_height )
2211             {
2212                 p_dec->fmt_out.video.i_visible_width =
2213                     p_dec->fmt_in.video.i_visible_width;
2214                 p_dec->fmt_out.video.i_visible_height =
2215                     p_dec->fmt_in.video.i_visible_height;
2216             }
2217             else
2218             {
2219                 p_dec->fmt_out.video.i_visible_width =
2220                     p_dec->fmt_out.video.i_width;
2221                 p_dec->fmt_out.video.i_visible_height =
2222                     p_dec->fmt_out.video.i_height;
2223             }
2224         }
2225
2226         if( p_dec->fmt_out.video.i_visible_height == 1088 &&
2227             var_CreateGetBool( p_dec, "hdtv-fix" ) )
2228         {
2229             p_dec->fmt_out.video.i_visible_height = 1080;
2230             p_dec->fmt_out.video.i_sar_num *= 135;
2231             p_dec->fmt_out.video.i_sar_den *= 136;
2232             msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
2233         }
2234
2235         if( !p_dec->fmt_out.video.i_sar_num ||
2236             !p_dec->fmt_out.video.i_sar_den )
2237         {
2238             p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
2239               p_dec->fmt_out.video.i_visible_height;
2240
2241             p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
2242               p_dec->fmt_out.video.i_visible_width;
2243         }
2244
2245         vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
2246                      &p_dec->fmt_out.video.i_sar_den,
2247                      p_dec->fmt_out.video.i_sar_num,
2248                      p_dec->fmt_out.video.i_sar_den, 50000 );
2249
2250         p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
2251         p_owner->video = p_dec->fmt_out.video;
2252
2253         vlc_mutex_lock( &p_owner->lock );
2254
2255         DecoderFlushBuffering( p_dec );
2256
2257         p_vout = p_owner->p_vout;
2258         p_owner->p_vout = NULL;
2259         vlc_mutex_unlock( &p_owner->lock );
2260
2261         p_vout = vout_Request( p_dec, p_vout, &p_dec->fmt_out.video );
2262
2263         vlc_mutex_lock( &p_owner->lock );
2264         p_owner->p_vout = p_vout;
2265
2266         DecoderUpdateFormatLocked( p_dec );
2267
2268         vlc_mutex_unlock( &p_owner->lock );
2269
2270         input_SendEventVout( p_owner->p_input );
2271         if( p_vout == NULL )
2272         {
2273             msg_Err( p_dec, "failed to create video output" );
2274             p_dec->b_error = true;
2275             return NULL;
2276         }
2277
2278         if( p_owner->video.i_rmask )
2279             p_owner->p_vout->render.i_rmask = p_owner->video.i_rmask;
2280         if( p_owner->video.i_gmask )
2281             p_owner->p_vout->render.i_gmask = p_owner->video.i_gmask;
2282         if( p_owner->video.i_bmask )
2283             p_owner->p_vout->render.i_bmask = p_owner->video.i_bmask;
2284     }
2285
2286     /* Get a new picture
2287      */
2288     for( ;; )
2289     {
2290         picture_t *p_picture;
2291
2292         if( p_dec->b_die || p_dec->b_error )
2293             return NULL;
2294
2295         /* The video filter chain required that there is always 1 free buffer
2296          * that it will use as temporary one. It will release the temporary
2297          * buffer once its work is done, so this check is safe even if we don't
2298          * lock around both count() and create().
2299          */
2300         if( vout_CountPictureAvailable( p_owner->p_vout ) >= 2 )
2301         {
2302             p_picture = vout_CreatePicture( p_owner->p_vout, 0, 0, 0 );
2303             if( p_picture )
2304                 return p_picture;
2305         }
2306
2307         if( DecoderIsFlushing( p_dec ) )
2308             return NULL;
2309
2310         /* */
2311         DecoderSignalBuffering( p_dec, true );
2312
2313         /* Check the decoder doesn't leak pictures */
2314         vout_FixLeaks( p_owner->p_vout, false );
2315
2316         /* FIXME add a vout_WaitPictureAvailable (timedwait) */
2317         msleep( VOUT_OUTMEM_SLEEP );
2318     }
2319 }
2320
2321 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
2322 {
2323     vout_DropPicture( p_dec->p_owner->p_vout, p_pic );
2324 }
2325
2326 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
2327 {
2328     vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
2329 }
2330
2331 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
2332 {
2333     vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
2334 }
2335
2336 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2337 {
2338     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2339     vout_thread_t *p_vout = NULL;
2340     subpicture_t *p_subpic;
2341     int i_attempts = 30;
2342
2343     while( i_attempts-- )
2344     {
2345         if( p_dec->b_die || p_dec->b_error )
2346             break;
2347
2348         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
2349         if( p_vout )
2350             break;
2351
2352         msleep( DECODER_SPU_VOUT_WAIT_DURATION );
2353     }
2354
2355     if( !p_vout )
2356     {
2357         msg_Warn( p_dec, "no vout found, dropping subpicture" );
2358         return NULL;
2359     }
2360
2361     if( p_owner->p_spu_vout != p_vout )
2362     {
2363         vlc_mutex_lock( &p_owner->lock );
2364
2365         DecoderFlushBuffering( p_dec );
2366
2367         vlc_mutex_unlock( &p_owner->lock );
2368
2369         spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
2370                      &p_owner->i_spu_channel );
2371         p_owner->i_spu_order = 0;
2372         p_owner->p_spu_vout = p_vout;
2373     }
2374
2375     p_subpic = subpicture_New();
2376     if( p_subpic )
2377     {
2378         p_subpic->i_channel = p_owner->i_spu_channel;
2379         p_subpic->i_order = p_owner->i_spu_order++;
2380         p_subpic->b_subtitle = true;
2381     }
2382
2383     vlc_object_release( p_vout );
2384
2385     return p_subpic;
2386 }
2387
2388 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2389 {
2390     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2391     vout_thread_t *p_vout = NULL;
2392
2393     p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
2394     if( !p_vout || p_owner->p_spu_vout != p_vout )
2395     {
2396         if( p_vout )
2397             vlc_object_release( p_vout );
2398         msg_Warn( p_dec, "no vout found, leaking subpicture" );
2399         return;
2400     }
2401
2402     subpicture_Delete( p_subpic );
2403
2404     vlc_object_release( p_vout );
2405 }
2406