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