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