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