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