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