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