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