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