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