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