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