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