]> git.sesse.net Git - vlc/blob - src/input/decoder.c
12241353290a20292cef14f538b5b93d9aba9d61
[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 "input_clock.h"
48 #include "input_decoder.h"
49
50 #include "../video_output/vout_internal.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        DecoderDecode( decoder_t * p_dec, block_t *p_block );
57 static void       DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date );
58
59 /* Buffers allocation callbacks for the decoders */
60 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
61 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
62
63 static picture_t *vout_new_buffer( decoder_t * );
64 static void vout_del_buffer( decoder_t *, picture_t * );
65 static void vout_link_picture( decoder_t *, picture_t * );
66 static void vout_unlink_picture( decoder_t *, picture_t * );
67
68 static subpicture_t *spu_new_buffer( decoder_t * );
69 static void spu_del_buffer( decoder_t *, subpicture_t * );
70
71 static es_format_t null_es_format;
72
73 struct decoder_owner_sys_t
74 {
75     bool      b_own_thread;
76
77     int64_t         i_preroll_end;
78
79     input_thread_t  *p_input;
80     input_clock_t   *p_clock;
81
82     vout_thread_t   *p_spu_vout;
83     int              i_spu_channel;
84     int64_t          i_spu_order;
85
86     sout_instance_t         *p_sout;
87     sout_packetizer_input_t *p_sout_input;
88
89     /* Some decoders require already packetized data (ie. not truncated) */
90     decoder_t *p_packetizer;
91
92     /* Current format in use by the output */
93     video_format_t video;
94     audio_format_t audio;
95     es_format_t    sout;
96
97     /* fifo */
98     block_fifo_t *p_fifo;
99
100     /* Lock for communication with decoder thread */
101     vlc_mutex_t lock;
102     vlc_cond_t  wait;
103
104     /* -- These variables need locking on write(only) -- */
105     aout_instance_t *p_aout;
106     aout_input_t    *p_aout_input;
107
108     vout_thread_t   *p_vout;
109
110     /* -- Theses variables need locking on read *and* write -- */
111     /* Pause */
112     bool b_paused;
113     mtime_t i_pause_date;
114
115     /* CC */
116     bool b_cc_supported;
117     bool pb_cc_present[4];
118     decoder_t *pp_cc[4];
119
120     /* Delay */
121     mtime_t i_ts_delay;
122 };
123
124 /* */
125 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
126 {
127     msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
128              "VLC probably does not support this sound or video format.",
129              (char*)&codec );
130     intf_UserFatal( p_dec, false, _("No suitable decoder module"), 
131                     _("VLC does not support the audio or video format \"%4.4s\". "
132                       "Unfortunately there is no way for you to fix this."), (char*)&codec );
133 }
134
135 /* decoder_GetInputAttachment:
136  */
137 input_attachment_t *decoder_GetInputAttachment( decoder_t *p_dec,
138                                                 const char *psz_name )
139 {
140     input_attachment_t *p_attachment;
141     if( input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENT, &p_attachment, psz_name ) )
142         return NULL;
143     return p_attachment;
144 }
145 /* decoder_GetInputAttachments:
146  */
147 int decoder_GetInputAttachments( decoder_t *p_dec,
148                                  input_attachment_t ***ppp_attachment,
149                                  int *pi_attachment )
150 {
151     return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
152                           ppp_attachment, pi_attachment );
153 }
154 /* decoder_GetDisplayDate:
155  */
156 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
157 {
158     decoder_owner_sys_t *p_owner = p_dec->p_owner;
159
160     if( !p_owner->p_clock )
161         return i_ts;
162     return input_clock_GetTS( p_owner->p_clock, p_owner->p_input->i_pts_delay, i_ts );
163 }
164 /* decoder_GetDisplayRate:
165  */
166 int decoder_GetDisplayRate( decoder_t *p_dec )
167 {
168     decoder_owner_sys_t *p_owner = p_dec->p_owner;
169
170     if( !p_owner->p_clock )
171         return INPUT_RATE_DEFAULT;
172     return input_clock_GetRate( p_owner->p_clock );
173 }
174
175 /**
176  * Spawns a new decoder thread
177  *
178  * \param p_input the input thread
179  * \param p_es the es descriptor
180  * \return the spawned decoder object
181  */
182 decoder_t *input_DecoderNew( input_thread_t *p_input,
183                              es_format_t *fmt, input_clock_t *p_clock, sout_instance_t *p_sout  )
184 {
185     decoder_t   *p_dec = NULL;
186     vlc_value_t val;
187
188 #ifndef ENABLE_SOUT
189     (void)b_force_decoder;
190 #else
191     /* If we are in sout mode, search for packetizer module */
192     if( p_sout )
193     {
194         /* Create the decoder configuration structure */
195         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER, p_sout );
196         if( p_dec == NULL )
197         {
198             msg_Err( p_input, "could not create packetizer" );
199             intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
200                             _("VLC could not open the packetizer module.") );
201             return NULL;
202         }
203     }
204     else
205 #endif
206     {
207         /* Create the decoder configuration structure */
208         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER, p_sout );
209         if( p_dec == NULL )
210         {
211             msg_Err( p_input, "could not create decoder" );
212             intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
213                             _("VLC could not open the decoder module.") );
214             return NULL;
215         }
216     }
217
218     if( !p_dec->p_module )
219     {
220         DecoderUnsupportedCodec( p_dec, fmt->i_codec );
221
222         DeleteDecoder( p_dec );
223         vlc_object_release( p_dec );
224         return NULL;
225     }
226
227     p_dec->p_owner->p_clock = p_clock;
228
229     if( p_sout && p_sout == p_input->p->p_sout && p_input->p->input.b_can_pace_control )
230     {
231         msg_Dbg( p_input, "stream out mode -> no decoder thread" );
232         p_dec->p_owner->b_own_thread = false;
233     }
234     else
235     {
236         var_Get( p_input, "minimize-threads", &val );
237         p_dec->p_owner->b_own_thread = !val.b_bool;
238     }
239
240     if( p_dec->p_owner->b_own_thread )
241     {
242         int i_priority;
243         if( fmt->i_cat == AUDIO_ES )
244             i_priority = VLC_THREAD_PRIORITY_AUDIO;
245         else
246             i_priority = VLC_THREAD_PRIORITY_VIDEO;
247
248         /* Spawn the decoder thread */
249         if( vlc_thread_create( p_dec, "decoder", DecoderThread,
250                                i_priority, false ) )
251         {
252             msg_Err( p_dec, "cannot spawn decoder thread" );
253             module_unneed( p_dec, p_dec->p_module );
254             DeleteDecoder( p_dec );
255             vlc_object_release( p_dec );
256             return NULL;
257         }
258     }
259
260     return p_dec;
261 }
262
263 /**
264  * Kills a decoder thread and waits until it's finished
265  *
266  * \param p_input the input thread
267  * \param p_es the es descriptor
268  * \return nothing
269  */
270 void input_DecoderDelete( decoder_t *p_dec )
271 {
272     decoder_owner_sys_t *p_owner = p_dec->p_owner;
273
274     vlc_object_kill( p_dec );
275
276     if( p_owner->b_own_thread )
277     {
278         /* Make sure we aren't paused anymore */
279         vlc_mutex_lock( &p_owner->lock );
280         if( p_owner->b_paused )
281         {
282             p_owner->b_paused = false;
283             vlc_cond_signal( &p_owner->wait );
284         }
285         vlc_mutex_unlock( &p_owner->lock );
286
287         /* Make sure the thread leaves the function by
288          * sending it an empty block. */
289         block_t *p_block = block_New( p_dec, 0 );
290         input_DecoderDecode( p_dec, p_block );
291
292         vlc_thread_join( p_dec );
293
294         /* Don't module_unneed() here because of the dll loader that wants
295          * close() in the same thread than open()/decode() */
296     }
297     else
298     {
299         /* Flush */
300         input_DecoderDecode( p_dec, NULL );
301
302         module_unneed( p_dec, p_dec->p_module );
303     }
304
305     /* */
306     if( p_dec->p_owner->b_cc_supported )
307     {
308         int i;
309         for( i = 0; i < 4; i++ )
310             input_DecoderSetCcState( p_dec, false, i );
311     }
312
313     /* Delete decoder configuration */
314     DeleteDecoder( p_dec );
315
316     /* Delete the decoder */
317     vlc_object_release( p_dec );
318 }
319
320 /**
321  * Put a block_t in the decoder's fifo.
322  *
323  * \param p_dec the decoder object
324  * \param p_block the data block
325  */
326 void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
327 {
328     decoder_owner_sys_t *p_owner = p_dec->p_owner;
329
330     if( p_owner->b_own_thread )
331     {
332         if( p_owner->p_input->p->b_out_pace_control )
333         {
334             /* FIXME !!!!! */
335             while( !p_dec->b_die && !p_dec->b_error &&
336                    block_FifoCount( p_owner->p_fifo ) > 10 )
337             {
338                 msleep( 1000 );
339             }
340         }
341         else if( block_FifoSize( p_owner->p_fifo ) > 50000000 /* 50 MB */ )
342         {
343             /* FIXME: ideally we would check the time amount of data
344              * in the fifo instead of its size. */
345             msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
346                       "consumed quickly enough), resetting fifo!" );
347             block_FifoEmpty( p_owner->p_fifo );
348         }
349
350         block_FifoPut( p_owner->p_fifo, p_block );
351     }
352     else
353     {
354         if( p_dec->b_error || ( p_block && p_block->i_buffer <= 0 ) )
355         {
356             if( p_block )
357                 block_Release( p_block );
358         }
359         else
360         {
361             DecoderDecode( p_dec, p_block );
362         }
363     }
364 }
365
366 void input_DecoderDiscontinuity( decoder_t * p_dec, bool b_flush )
367 {
368     decoder_owner_sys_t *p_owner = p_dec->p_owner;
369     block_t *p_null;
370
371     /* Empty the fifo */
372     if( p_owner->b_own_thread && b_flush )
373         block_FifoEmpty( p_owner->p_fifo );
374
375     /* Send a special block */
376     p_null = block_New( p_dec, 128 );
377     p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
378     if( b_flush && p_dec->fmt_in.i_cat == SPU_ES )
379         p_null->i_flags |= BLOCK_FLAG_CORE_FLUSH;
380     /* FIXME check for p_packetizer or b_packitized from es_format_t of input ? */
381     if( p_owner->p_packetizer && b_flush )
382         p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
383     memset( p_null->p_buffer, 0, p_null->i_buffer );
384
385     input_DecoderDecode( p_dec, p_null );
386 }
387
388 bool input_DecoderEmpty( decoder_t * p_dec )
389 {
390     if( p_dec->p_owner->b_own_thread &&
391         block_FifoCount( p_dec->p_owner->p_fifo ) > 0 )
392     {
393         return false;
394     }
395     return true;
396 }
397
398 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
399 {
400     decoder_owner_sys_t *p_owner = p_dec->p_owner;
401     int i;
402
403     vlc_mutex_lock( &p_owner->lock );
404     for( i = 0; i < 4; i++ )
405         pb_present[i] =  p_owner->pb_cc_present[i];
406     vlc_mutex_unlock( &p_owner->lock );
407 }
408 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
409 {
410     decoder_owner_sys_t *p_owner = p_dec->p_owner;
411
412     //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
413
414     if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
415         return VLC_EGENERIC;
416
417     if( b_decode )
418     {
419         static const vlc_fourcc_t fcc[4] = {
420             VLC_FOURCC('c', 'c', '1', ' '),
421             VLC_FOURCC('c', 'c', '2', ' '),
422             VLC_FOURCC('c', 'c', '3', ' '),
423             VLC_FOURCC('c', 'c', '4', ' '),
424         };
425         decoder_t *p_cc;
426         es_format_t fmt;
427
428         es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
429         p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER, p_owner->p_sout );
430         if( !p_cc )
431         {
432             msg_Err( p_dec, "could not create decoder" );
433             intf_UserFatal( p_dec, false, _("Streaming / Transcoding failed"),
434                             _("VLC could not open the decoder module.") );
435             return VLC_EGENERIC;
436         }
437         else if( !p_cc->p_module )
438         {
439             DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
440             DeleteDecoder( p_cc );
441             vlc_object_release( p_cc );
442             return VLC_EGENERIC;
443         }
444         p_cc->p_owner->p_clock = p_owner->p_clock;
445
446         vlc_mutex_lock( &p_owner->lock );
447         p_owner->pp_cc[i_channel] = p_cc;
448         vlc_mutex_unlock( &p_owner->lock );
449     }
450     else
451     {
452         decoder_t *p_cc;
453
454         vlc_mutex_lock( &p_owner->lock );
455         p_cc = p_owner->pp_cc[i_channel];
456         p_owner->pp_cc[i_channel] = NULL;
457         vlc_mutex_unlock( &p_owner->lock );
458
459         if( p_cc )
460         {
461             vlc_object_kill( p_cc );
462             module_unneed( p_cc, p_cc->p_module );
463             DeleteDecoder( p_cc );
464             vlc_object_release( p_cc );
465         }
466     }
467     return VLC_SUCCESS;
468 }
469 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
470 {
471     decoder_owner_sys_t *p_owner = p_dec->p_owner;
472
473     *pb_decode = false;
474     if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
475         return VLC_EGENERIC;
476
477     vlc_mutex_lock( &p_owner->lock );
478     *pb_decode = p_owner->pp_cc[i_channel] != NULL;
479     vlc_mutex_unlock( &p_owner->lock );
480     return VLC_EGENERIC;
481 }
482
483 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
484 {
485     decoder_owner_sys_t *p_owner = p_dec->p_owner;
486
487     vlc_mutex_lock( &p_owner->lock );
488
489     assert( (!p_owner->b_paused) != (!b_paused) );
490     p_owner->b_paused = b_paused;
491     p_owner->i_pause_date = i_date;
492     if( p_owner->b_own_thread )
493         vlc_cond_signal( &p_owner->wait );
494
495     DecoderOutputChangePause( p_dec, b_paused, i_date );
496     vlc_mutex_unlock( &p_owner->lock );
497 }
498
499 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
500 {
501     decoder_owner_sys_t *p_owner = p_dec->p_owner;
502
503     vlc_mutex_lock( &p_owner->lock );
504     p_owner->i_ts_delay = i_delay;
505     vlc_mutex_unlock( &p_owner->lock );
506 }
507 /**
508  * Create a decoder object
509  *
510  * \param p_input the input thread
511  * \param p_es the es descriptor
512  * \param i_object_type Object type as define in include/vlc_objects.h
513  * \return the decoder object
514  */
515 static decoder_t * CreateDecoder( input_thread_t *p_input,
516                                   es_format_t *fmt, int i_object_type, sout_instance_t *p_sout )
517 {
518     decoder_t *p_dec;
519     decoder_owner_sys_t *p_owner;
520     int i;
521
522     p_dec = vlc_object_create( p_input, i_object_type );
523     if( p_dec == NULL )
524         return NULL;
525
526     p_dec->pf_decode_audio = NULL;
527     p_dec->pf_decode_video = NULL;
528     p_dec->pf_decode_sub = NULL;
529     p_dec->pf_get_cc = NULL;
530     p_dec->pf_packetize = NULL;
531
532     /* Initialize the decoder fifo */
533     p_dec->p_module = NULL;
534
535     memset( &null_es_format, 0, sizeof(es_format_t) );
536     es_format_Copy( &p_dec->fmt_in, fmt );
537     es_format_Copy( &p_dec->fmt_out, &null_es_format );
538
539     /* Allocate our private structure for the decoder */
540     p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
541     if( p_dec->p_owner == NULL )
542     {
543         vlc_object_release( p_dec );
544         return NULL;
545     }
546     p_dec->p_owner->b_own_thread = true;
547     p_dec->p_owner->i_preroll_end = -1;
548     p_dec->p_owner->p_input = p_input;
549     p_dec->p_owner->p_aout = NULL;
550     p_dec->p_owner->p_aout_input = NULL;
551     p_dec->p_owner->p_vout = NULL;
552     p_dec->p_owner->p_spu_vout = NULL;
553     p_dec->p_owner->i_spu_channel = 0;
554     p_dec->p_owner->i_spu_order = 0;
555     p_dec->p_owner->p_sout = p_sout;
556     p_dec->p_owner->p_sout_input = NULL;
557     p_dec->p_owner->p_packetizer = NULL;
558
559     /* decoder fifo */
560     if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
561     {
562         free( p_dec->p_owner );
563         vlc_object_release( p_dec );
564         return NULL;
565     }
566
567     /* Set buffers allocation callbacks for the decoders */
568     p_dec->pf_aout_buffer_new = aout_new_buffer;
569     p_dec->pf_aout_buffer_del = aout_del_buffer;
570     p_dec->pf_vout_buffer_new = vout_new_buffer;
571     p_dec->pf_vout_buffer_del = vout_del_buffer;
572     p_dec->pf_picture_link    = vout_link_picture;
573     p_dec->pf_picture_unlink  = vout_unlink_picture;
574     p_dec->pf_spu_buffer_new  = spu_new_buffer;
575     p_dec->pf_spu_buffer_del  = spu_del_buffer;
576
577     vlc_object_attach( p_dec, p_input );
578
579     /* Find a suitable decoder/packetizer module */
580     if( i_object_type == VLC_OBJECT_DECODER )
581         p_dec->p_module = module_need( p_dec, "decoder", "$codec", 0 );
582     else
583         p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", 0 );
584
585     /* Check if decoder requires already packetized data */
586     if( i_object_type == VLC_OBJECT_DECODER &&
587         p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
588     {
589         p_dec->p_owner->p_packetizer =
590             vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
591         if( p_dec->p_owner->p_packetizer )
592         {
593             es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
594                             &p_dec->fmt_in );
595
596             es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
597                             &null_es_format );
598
599             vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
600
601             p_dec->p_owner->p_packetizer->p_module =
602                 module_need( p_dec->p_owner->p_packetizer,
603                              "packetizer", "$packetizer", 0 );
604
605             if( !p_dec->p_owner->p_packetizer->p_module )
606             {
607                 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
608                 vlc_object_detach( p_dec->p_owner->p_packetizer );
609                 vlc_object_release( p_dec->p_owner->p_packetizer );
610             }
611         }
612     }
613
614     /* Copy ourself the input replay gain */
615     if( fmt->i_cat == AUDIO_ES )
616     {
617         for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
618         {
619             if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
620             {
621                 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
622                 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
623             }
624             if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
625             {
626                 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
627                 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
628             }
629         }
630     }
631     /* */
632     p_owner->b_cc_supported = false;
633     if( i_object_type == VLC_OBJECT_DECODER )
634     {
635         if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
636             p_owner->b_cc_supported = true;
637         if( p_dec->pf_get_cc )
638             p_owner->b_cc_supported = true;
639     }
640
641     vlc_mutex_init( &p_owner->lock );
642     vlc_cond_init( &p_owner->wait );
643     p_owner->b_paused = false;
644     p_owner->i_pause_date = 0;
645     for( i = 0; i < 4; i++ )
646     {
647         p_owner->pb_cc_present[i] = false;
648         p_owner->pp_cc[i] = NULL;
649     }
650     p_owner->i_ts_delay = 0;
651     return p_dec;
652 }
653
654 /**
655  * The decoding main loop
656  *
657  * \param p_dec the decoder
658  */
659 static void* DecoderThread( vlc_object_t *p_this )
660 {
661     decoder_t *p_dec = (decoder_t *)p_this;
662     decoder_owner_sys_t *p_owner = p_dec->p_owner;
663
664     block_t *p_block;
665     int canc = vlc_savecancel();
666
667     /* The decoder's main loop */
668     while( vlc_object_alive( p_dec ) && !p_dec->b_error )
669     {
670         if( ( p_block = block_FifoGet( p_owner->p_fifo ) ) == NULL )
671         {
672             p_dec->b_error = 1;
673             break;
674         }
675
676         if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
677             break;
678     }
679
680     while( vlc_object_alive( p_dec ) )
681     {
682         /* Trash all received PES packets */
683         p_block = block_FifoGet( p_owner->p_fifo );
684         if( p_block )
685             block_Release( p_block );
686     }
687
688     /* We do it here because of the dll loader that wants close() in the
689      * same thread than open()/decode() */
690     module_unneed( p_dec, p_dec->p_module );
691     vlc_restorecancel( canc );
692     return NULL;
693 }
694
695 static void DecoderWaitUnpause( decoder_t *p_dec )
696 {
697     decoder_owner_sys_t *p_owner = p_dec->p_owner;
698
699     vlc_mutex_lock( &p_owner->lock );
700
701     while( p_owner->b_paused )
702         vlc_cond_wait( &p_owner->wait, &p_owner->lock );
703
704     vlc_mutex_unlock( &p_owner->lock );
705 }
706
707 static mtime_t DecoderGetTotalDelay( decoder_t *p_dec )
708 {
709     decoder_owner_sys_t *p_owner = p_dec->p_owner;
710
711     vlc_mutex_lock( &p_owner->lock );
712
713     mtime_t i_delay = p_owner->i_ts_delay;
714
715     vlc_mutex_unlock( &p_owner->lock );
716
717     return p_owner->p_input->i_pts_delay + i_delay;
718 }
719
720 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
721 {
722     decoder_owner_sys_t *p_owner = p_dec->p_owner;
723
724     vlc_assert_locked( &p_owner->lock );
725
726     /* XXX only audio and video output have to be paused.
727      * - for sout it is useless
728      * - for subs, it is done by the vout
729      */
730     if( p_dec->fmt_in.i_cat == AUDIO_ES )
731     {
732         if( p_owner->p_aout && p_owner->p_aout_input )
733             aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
734                                  b_paused, i_date );
735     }
736     else if( p_dec->fmt_in.i_cat == VIDEO_ES )
737     {
738         if( p_owner->p_vout )
739             vout_ChangePause( p_owner->p_vout, b_paused, i_date );
740     }
741 }
742 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
743 {
744     if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
745         *pi_preroll = INT64_MAX;
746     else if( p->i_pts > 0 )
747         *pi_preroll = __MIN( *pi_preroll, p->i_pts );
748     else if( p->i_dts > 0 )
749         *pi_preroll = __MIN( *pi_preroll, p->i_dts );
750 }
751
752 static mtime_t DecoderTeletextFixTs( mtime_t i_ts, mtime_t i_ts_delay )
753 {
754     mtime_t current_date = mdate();
755
756     /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
757     if( !i_ts || i_ts > current_date + 10000000 || i_ts < current_date )
758     {
759         /* ETSI EN 300 472 Annex A : do not take into account the PTS
760          * for teletext streams. */
761         return current_date + 400000 + i_ts_delay;
762     }
763     return i_ts;
764 }
765
766 static void DecoderSoutBufferFixTs( block_t *p_block,
767                                     input_clock_t *p_clock, mtime_t i_ts_delay,
768                                     bool b_teletext )
769 {
770     assert( p_clock );
771
772     p_block->i_rate = input_clock_GetRate( p_clock );
773
774     if( p_block->i_dts > 0 )
775         p_block->i_dts = input_clock_GetTS( p_clock, i_ts_delay, p_block->i_dts );
776
777     if( p_block->i_pts > 0 )
778         p_block->i_pts = input_clock_GetTS( p_clock, i_ts_delay, p_block->i_pts );
779
780     if( p_block->i_length > 0 )
781         p_block->i_length = ( p_block->i_length * p_block->i_rate +
782                                 INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
783
784     if( b_teletext )
785         p_block->i_pts = DecoderTeletextFixTs( p_block->i_pts, i_ts_delay );
786 }
787 static void DecoderAoutBufferFixTs( aout_buffer_t *p_buffer,
788                                     input_clock_t *p_clock, mtime_t i_ts_delay )
789 {
790     /* sout display module does not set clock */
791     if( !p_clock )
792         return;
793
794     if( p_buffer->start_date )
795         p_buffer->start_date = input_clock_GetTS( p_clock, i_ts_delay, p_buffer->start_date );
796
797     if( p_buffer->end_date )
798         p_buffer->end_date = input_clock_GetTS( p_clock, i_ts_delay, p_buffer->end_date );
799 }
800 static void DecoderVoutBufferFixTs( picture_t *p_picture,
801                                     input_clock_t *p_clock, mtime_t i_ts_delay )
802 {
803     /* sout display module does not set clock */
804     if( !p_clock )
805         return;
806
807     if( p_picture->date )
808         p_picture->date = input_clock_GetTS( p_clock, i_ts_delay, p_picture->date );
809 }
810 static void DecoderSpuBufferFixTs( subpicture_t *p_subpic,
811                                    input_clock_t *p_clock, mtime_t i_ts_delay,
812                                    bool b_teletext )
813 {
814     bool b_ephemere = p_subpic->i_start == p_subpic->i_stop;
815
816     /* sout display module does not set clock */
817     if( !p_clock )
818         return;
819
820     if( p_subpic->i_start )
821         p_subpic->i_start = input_clock_GetTS( p_clock, i_ts_delay, p_subpic->i_start );
822
823     if( p_subpic->i_stop )
824         p_subpic->i_stop = input_clock_GetTS( p_clock, i_ts_delay, p_subpic->i_stop );
825
826     /* Do not create ephemere picture because of rounding errors */
827     if( !b_ephemere && p_subpic->i_start == p_subpic->i_stop )
828         p_subpic->i_stop++;
829
830     if( b_teletext )
831         p_subpic->i_start = DecoderTeletextFixTs( p_subpic->i_start, i_ts_delay );
832 }
833
834 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
835 {
836     decoder_owner_sys_t *p_owner = p_dec->p_owner;
837     input_thread_t  *p_input = p_owner->p_input;
838     input_clock_t   *p_clock = p_owner->p_clock;
839     aout_buffer_t   *p_aout_buf;
840     int i_decoded = 0;
841     int i_lost = 0;
842     int i_played = 0;
843
844     while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
845     {
846         aout_instance_t *p_aout = p_owner->p_aout;
847         aout_input_t    *p_aout_input = p_owner->p_aout_input;
848         int i_rate;
849         int i_lost;
850         int i_played;
851
852         if( p_dec->b_die )
853         {
854             /* It prevent freezing VLC in case of broken decoder */
855             aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
856             if( p_block )
857                 block_Release( p_block );
858             break;
859         }
860         i_decoded++;
861
862         if( p_aout_buf->start_date < p_owner->i_preroll_end )
863         {
864             aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
865             continue;
866         }
867
868         if( p_owner->i_preroll_end > 0 )
869         {
870             /* FIXME TODO flush audio output (don't know how to do that) */
871             msg_Dbg( p_dec, "End of audio preroll" );
872             p_owner->i_preroll_end = -1;
873         }
874
875         DecoderWaitUnpause( p_dec );
876
877         const mtime_t i_delay = DecoderGetTotalDelay( p_dec );
878
879         DecoderAoutBufferFixTs( p_aout_buf, p_clock, i_delay );
880
881         if( p_clock )
882             i_rate = input_clock_GetRate( p_clock );
883         else if( p_block && p_block->i_rate > 0 )
884             i_rate = p_block->i_rate;
885         else
886             i_rate = INPUT_RATE_DEFAULT;
887
888         /* FIXME TODO take care of audio-delay for mdate check */
889         const mtime_t i_max_date = mdate() + i_delay + AOUT_MAX_ADVANCE_TIME;
890
891         if( p_aout_buf->start_date > 0 &&
892             p_aout_buf->start_date <= i_max_date &&
893             i_rate >= INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE &&
894             i_rate <= INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
895         {
896             /* Wait if we are too early
897              * FIXME that's plain ugly to do it here */
898             mwait( p_aout_buf->start_date - AOUT_MAX_PREPARE_TIME );
899
900             if( !aout_DecPlay( p_aout, p_aout_input, p_aout_buf, i_rate ) )
901                 i_played++;
902             i_lost += aout_DecGetResetLost( p_aout, p_aout_input );
903         }
904         else
905         {
906             if( p_aout_buf->start_date <= 0 )
907             {
908                 msg_Warn( p_dec, "non-dated audio buffer received" );
909             }
910             else if( p_aout_buf->start_date > i_max_date )
911             {
912                 msg_Warn( p_aout, "received buffer in the future (%"PRId64")",
913                           p_aout_buf->start_date - mdate() );
914             }
915             i_lost++;
916             aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
917         }
918
919     }
920
921     /* Update ugly stat */
922     if( i_decoded > 0 || i_lost > 0 || i_played > 0 )
923     {
924         vlc_mutex_lock( &p_input->p->counters.counters_lock);
925
926         stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
927                              i_lost, NULL );
928         stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
929                              i_played, NULL );
930         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
931                              i_decoded, NULL );
932
933         vlc_mutex_unlock( &p_input->p->counters.counters_lock);
934     }
935 }
936 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
937 {
938     decoder_owner_sys_t *p_owner = p_dec->p_owner;
939     block_t *p_cc;
940     bool pb_present[4];
941     int i;
942     int i_cc_decoder;
943
944     assert( p_dec_cc->pf_get_cc != NULL );
945
946     /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
947     if( !p_owner->b_cc_supported )
948         return;
949
950     p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
951     if( !p_cc )
952         return;
953
954     vlc_mutex_lock( &p_owner->lock );
955     for( i = 0, i_cc_decoder = 0; i < 4; i++ )
956     {
957         p_owner->pb_cc_present[i] |= pb_present[i];
958         if( p_owner->pp_cc[i] )
959             i_cc_decoder++;
960     }
961
962     for( i = 0; i < 4; i++ )
963     {
964         if( !p_owner->pp_cc[i] )
965             continue;
966
967         if( i_cc_decoder > 1 )
968             DecoderDecode( p_owner->pp_cc[i], block_Duplicate( p_cc ) );
969         else
970             DecoderDecode( p_owner->pp_cc[i], p_cc );
971         i_cc_decoder--;
972     }
973     vlc_mutex_unlock( &p_owner->lock );
974 }
975 static void VoutDisplayedPicture( vout_thread_t *p_vout, picture_t *p_pic )
976 {
977     vlc_mutex_lock( &p_vout->picture_lock );
978
979     if( p_pic->i_status == READY_PICTURE )
980     {
981         /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
982         p_pic->date = 1;
983     }
984     else if( p_pic->i_refcount > 0 )
985     {
986         p_pic->i_status = DISPLAYED_PICTURE;
987     }
988     else
989     {
990         p_pic->i_status = DESTROYED_PICTURE;
991         picture_CleanupQuant( p_pic );
992         p_vout->i_heap_size--;
993     }
994
995     vlc_mutex_unlock( &p_vout->picture_lock );
996 }
997 static void VoutFlushPicture( vout_thread_t *p_vout )
998 {
999     int i;
1000     vlc_mutex_lock( &p_vout->picture_lock );
1001     for( i = 0; i < p_vout->render.i_pictures; i++ )
1002     {
1003         picture_t *p_pic = p_vout->render.pp_picture[i];
1004
1005         if( p_pic->i_status == READY_PICTURE ||
1006             p_pic->i_status == DISPLAYED_PICTURE )
1007         {
1008             /* We cannot change picture status if it is in READY_PICTURE state,
1009              * Just make sure they won't be displayed */
1010             p_pic->date = 1;
1011         }
1012     }
1013     vlc_mutex_unlock( &p_vout->picture_lock );
1014 }
1015
1016 #if 0
1017 static void DecoderOptimizePtsDelay( decoder_t *p_dec )
1018 {
1019     input_thread_t *p_input = p_dec->p_owner->p_input;
1020     vout_thread_t *p_vout = p_dec->p_owner->p_vout;
1021     input_thread_private_t *p_priv = p_input->p;
1022
1023     picture_t *p_old = NULL;
1024     picture_t *p_young = NULL;
1025     int i;
1026
1027     /* Enable with --auto-adjust-pts-delay */
1028     if( !p_priv->pts_adjust.b_auto_adjust )
1029         return;
1030
1031     for( i = 0; i < I_RENDERPICTURES; i++ )
1032     {
1033         picture_t *p_pic = PP_RENDERPICTURE[i];
1034
1035         if( p_pic->i_status != READY_PICTURE )
1036             continue;
1037
1038         if( !p_old || p_pic->date < p_old->date )
1039             p_old = p_pic;
1040         if( !p_young || p_pic->date > p_young->date )
1041             p_young = p_pic;
1042     }
1043
1044     if( !p_young || !p_old )
1045         return;
1046
1047     /* Try to find if we can reduce the pts
1048      * This first draft is way too simple, and we can't say if the
1049      * algo will converge. It's also full of constants.
1050      * But this simple algo allows to reduce the latency
1051      * to the minimum.
1052      * The whole point of this, is to bypass the pts_delay set
1053      * by the access but also the delay arbitraly set by
1054      * the remote server.
1055      * Actually the remote server's muxer may set up a
1056      * pts<->dts delay in the muxed stream. That is
1057      * why we may end up in having a negative pts_delay,
1058      * to compensate that artificial delay. */
1059     const mtime_t i_buffer_length = p_young->date - p_old->date;
1060     int64_t i_pts_slide = 0;
1061     if( i_buffer_length < 10000 )
1062     {
1063         if( p_priv->pts_adjust.i_num_faulty > 10 )
1064         {
1065             i_pts_slide = __MAX(p_input->i_pts_delay *3 / 2, 10000);
1066             p_priv->pts_adjust.i_num_faulty = 0;
1067         }
1068         if( p_priv->pts_adjust.b_to_high )
1069         {
1070             p_priv->pts_adjust.b_to_high = !p_priv->pts_adjust.b_to_high;
1071             p_priv->pts_adjust.i_num_faulty = 0;
1072         }
1073         p_priv->pts_adjust.i_num_faulty++;
1074     }
1075     else if( i_buffer_length > 100000 )
1076     {
1077         if( p_priv->pts_adjust.i_num_faulty > 25 )
1078         {
1079             i_pts_slide = -i_buffer_length/2;
1080             p_priv->pts_adjust.i_num_faulty = 0;
1081         }
1082         if( p_priv->pts_adjust.b_to_high )
1083         {
1084             p_priv->pts_adjust.b_to_high = !p_priv->pts_adjust.b_to_high;
1085             p_priv->pts_adjust.i_num_faulty = 0;
1086         }
1087         p_priv->pts_adjust.i_num_faulty++;
1088     }
1089     if( i_pts_slide != 0 )
1090     {
1091         const mtime_t i_pts_delay_org = p_input->i_pts_delay;
1092
1093         p_input->i_pts_delay += i_pts_slide;
1094
1095         /* Don't play with the pts delay for more than -2<->3sec */
1096         if( p_input->i_pts_delay < -2000000 )
1097             p_input->i_pts_delay = -2000000;
1098         else if( p_input->i_pts_delay > 3000000 )
1099             p_input->i_pts_delay = 3000000;
1100         i_pts_slide = p_input->i_pts_delay - i_pts_delay_org;
1101
1102         msg_Dbg( p_input, "Sliding the pts by %dms pts delay at %dms picture buffer was %dms",
1103             (int)i_pts_slide/1000, (int)p_input->i_pts_delay/1000, (int)i_buffer_length/1000);
1104
1105         vlc_mutex_lock( &p_vout->picture_lock );
1106         /* Slide all the picture */
1107         for( i = 0; i < I_RENDERPICTURES; i++ )
1108             PP_RENDERPICTURE[i]->date += i_pts_slide;
1109         /* FIXME: slide aout/spu */
1110         vlc_mutex_unlock( &p_vout->picture_lock );
1111     }
1112 }
1113 #endif
1114
1115 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1116 {
1117     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1118     input_thread_t *p_input = p_owner->p_input;
1119     picture_t      *p_pic;
1120     int i_lost = 0;
1121     int i_decoded = 0;
1122     int i_displayed = 0;
1123
1124     while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1125     {
1126         vout_thread_t  *p_vout = p_owner->p_vout;
1127         if( p_dec->b_die )
1128         {
1129             /* It prevent freezing VLC in case of broken decoder */
1130             VoutDisplayedPicture( p_vout, p_pic );
1131             if( p_block )
1132                 block_Release( p_block );
1133             break;
1134         }
1135
1136         i_decoded++;
1137
1138         if( p_pic->date < p_owner->i_preroll_end )
1139         {
1140             VoutDisplayedPicture( p_vout, p_pic );
1141             continue;
1142         }
1143
1144         if( p_owner->i_preroll_end > 0 )
1145         {
1146             msg_Dbg( p_dec, "End of video preroll" );
1147             if( p_vout )
1148                 VoutFlushPicture( p_vout );
1149             /* */
1150             p_owner->i_preroll_end = -1;
1151         }
1152
1153         if( p_dec->pf_get_cc &&
1154             ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1155             DecoderGetCc( p_dec, p_dec );
1156
1157         DecoderWaitUnpause( p_dec );
1158
1159         const mtime_t i_delay = DecoderGetTotalDelay( p_dec );
1160
1161         DecoderVoutBufferFixTs( p_pic, p_owner->p_clock, i_delay );
1162
1163         /* Video is never delayed so simple */
1164         const mtime_t i_max_date = mdate() + i_delay + VOUT_BOGUS_DELAY;
1165
1166         if( p_pic->date > 0 && p_pic->date < i_max_date )
1167         {
1168             vout_DatePicture( p_vout, p_pic, p_pic->date );
1169
1170             /* Re-enable it but do it right this time */
1171             //DecoderOptimizePtsDelay( p_dec );
1172             vout_DisplayPicture( p_vout, p_pic );
1173         }
1174         else
1175         {
1176             if( p_pic->date <= 0 )
1177             {
1178                 msg_Warn( p_vout, "non-dated video buffer received" );
1179             }
1180             else
1181             {
1182                 msg_Warn( p_vout, "early picture skipped (%"PRId64")",
1183                           p_pic->date - mdate() );
1184             }
1185             i_lost++;
1186             VoutDisplayedPicture( p_vout, p_pic );
1187         }
1188         int i_tmp_display;
1189         int i_tmp_lost;
1190         vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1191
1192         i_displayed += i_tmp_display;
1193         i_lost += i_tmp_lost;
1194     }
1195     if( i_decoded > 0 || i_lost > 0 || i_displayed > 0 )
1196     {
1197         vlc_mutex_lock( &p_input->p->counters.counters_lock );
1198
1199         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
1200                              i_decoded, NULL );
1201         stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
1202                              i_lost , NULL);
1203
1204         stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
1205                              i_displayed, NULL);
1206
1207         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1208     }
1209 }
1210
1211 /**
1212  * Decode a block
1213  *
1214  * \param p_dec the decoder object
1215  * \param p_block the block to decode
1216  * \return VLC_SUCCESS or an error code
1217  */
1218 static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
1219 {
1220     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1221     const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1222
1223     if( p_block && p_block->i_buffer <= 0 )
1224     {
1225         block_Release( p_block );
1226         return VLC_SUCCESS;
1227     }
1228
1229 #ifdef ENABLE_SOUT
1230     if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
1231     {
1232         block_t *p_sout_block;
1233
1234         while( ( p_sout_block =
1235                      p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1236         {
1237             if( !p_owner->p_sout_input )
1238             {
1239                 es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1240
1241                 p_owner->sout.i_group = p_dec->fmt_in.i_group;
1242                 p_owner->sout.i_id = p_dec->fmt_in.i_id;
1243                 if( p_dec->fmt_in.psz_language )
1244                 {
1245                     if( p_owner->sout.psz_language )
1246                         free( p_owner->sout.psz_language );
1247                     p_owner->sout.psz_language =
1248                         strdup( p_dec->fmt_in.psz_language );
1249                 }
1250
1251                 p_owner->p_sout_input =
1252                     sout_InputNew( p_owner->p_sout,
1253                                    &p_owner->sout );
1254
1255                 if( p_owner->p_sout_input == NULL )
1256                 {
1257                     msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1258                              (char *)&p_owner->sout.i_codec );
1259                     p_dec->b_error = true;
1260
1261                     while( p_sout_block )
1262                     {
1263                         block_t *p_next = p_sout_block->p_next;
1264                         block_Release( p_sout_block );
1265                         p_sout_block = p_next;
1266                     }
1267                     break;
1268                 }
1269             }
1270
1271             while( p_sout_block )
1272             {
1273                 block_t *p_next = p_sout_block->p_next;
1274
1275                 p_sout_block->p_next = NULL;
1276
1277                 DecoderWaitUnpause( p_dec );
1278
1279                 const mtime_t i_delay = DecoderGetTotalDelay( p_dec );
1280
1281                 DecoderSoutBufferFixTs( p_sout_block,
1282                                         p_owner->p_clock, i_delay, b_telx );
1283
1284                 sout_InputSendBuffer( p_owner->p_sout_input,
1285                                       p_sout_block );
1286
1287                 p_sout_block = p_next;
1288             }
1289
1290             /* For now it's enough, as only sout impact on this flag */
1291             if( p_owner->p_sout->i_out_pace_nocontrol > 0 &&
1292                 p_owner->p_input->p->b_out_pace_control )
1293             {
1294                 msg_Dbg( p_dec, "switching to sync mode" );
1295                 p_owner->p_input->p->b_out_pace_control = false;
1296             }
1297             else if( p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
1298                      !p_owner->p_input->p->b_out_pace_control )
1299             {
1300                 msg_Dbg( p_dec, "switching to async mode" );
1301                 p_owner->p_input->p->b_out_pace_control = true;
1302             }
1303         }
1304     }
1305     else
1306 #endif
1307     if( p_dec->fmt_in.i_cat == AUDIO_ES )
1308     {
1309         if( p_block )
1310             DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1311
1312         if( p_owner->p_packetizer )
1313         {
1314             block_t *p_packetized_block;
1315             decoder_t *p_packetizer = p_owner->p_packetizer;
1316
1317             while( (p_packetized_block =
1318                     p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1319             {
1320                 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1321                 {
1322                     es_format_Clean( &p_dec->fmt_in );
1323                     es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1324                 }
1325
1326                 while( p_packetized_block )
1327                 {
1328                     block_t *p_next = p_packetized_block->p_next;
1329                     p_packetized_block->p_next = NULL;
1330
1331                     DecoderDecodeAudio( p_dec, p_packetized_block );
1332
1333                     p_packetized_block = p_next;
1334                 }
1335             }
1336         }
1337         else if( p_block )
1338         {
1339             DecoderDecodeAudio( p_dec, p_block );
1340         }
1341     }
1342     else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1343     {
1344         if( p_block )
1345             DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1346
1347         if( p_owner->p_packetizer )
1348         {
1349             block_t *p_packetized_block;
1350             decoder_t *p_packetizer = p_owner->p_packetizer;
1351
1352             while( (p_packetized_block =
1353                     p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1354             {
1355                 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1356                 {
1357                     es_format_Clean( &p_dec->fmt_in );
1358                     es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1359                 }
1360                 if( p_packetizer->pf_get_cc )
1361                     DecoderGetCc( p_dec, p_packetizer );
1362
1363                 while( p_packetized_block )
1364                 {
1365                     block_t *p_next = p_packetized_block->p_next;
1366                     p_packetized_block->p_next = NULL;
1367
1368                     DecoderDecodeVideo( p_dec, p_packetized_block );
1369
1370                     p_packetized_block = p_next;
1371                 }
1372             }
1373         }
1374         else if( p_block )
1375         {
1376             DecoderDecodeVideo( p_dec, p_block );
1377         }
1378     }
1379     else if( p_dec->fmt_in.i_cat == SPU_ES )
1380     {
1381         input_thread_t *p_input = p_owner->p_input;
1382         vout_thread_t *p_vout;
1383         subpicture_t *p_spu;
1384         bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1385         bool b_flush = false;
1386
1387         if( p_block )
1388         {
1389             DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1390             b_flush = (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH) != 0;
1391         }
1392
1393         if( !b_flushing && b_flush && p_owner->p_spu_vout )
1394         {
1395             p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1396
1397             if( p_vout && p_owner->p_spu_vout == p_vout )
1398                 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1399                              p_owner->i_spu_channel );
1400
1401             if( p_vout )
1402                 vlc_object_release( p_vout );
1403         }
1404
1405         while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1406         {
1407             vlc_mutex_lock( &p_input->p->counters.counters_lock );
1408             stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1409             vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1410
1411             p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1412             if( p_vout && p_owner->p_spu_vout == p_vout )
1413             {
1414                 /* Preroll does not work very well with subtitle */
1415                 if( p_spu->i_start > 0 &&
1416                     p_spu->i_start < p_owner->i_preroll_end &&
1417                     ( p_spu->i_stop <= 0 || p_spu->i_stop < p_owner->i_preroll_end ) )
1418                 {
1419                     subpicture_Delete( p_spu );
1420                 }
1421                 else
1422                 {
1423                     DecoderWaitUnpause( p_dec );
1424
1425                     DecoderSpuBufferFixTs( p_spu, p_owner->p_clock, p_input->i_pts_delay, b_telx );
1426                     spu_DisplaySubpicture( p_vout->p_spu, p_spu );
1427                 }
1428             }
1429             else
1430             {
1431                 msg_Warn( p_dec, "no vout found, leaking subpicture" );
1432             }
1433             if( p_vout )
1434                 vlc_object_release( p_vout );
1435         }
1436     }
1437     else
1438     {
1439         msg_Err( p_dec, "unknown ES format" );
1440         p_dec->b_error = 1;
1441     }
1442
1443     return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
1444 }
1445
1446 /**
1447  * Destroys a decoder object
1448  *
1449  * \param p_dec the decoder object
1450  * \return nothing
1451  */
1452 static void DeleteDecoder( decoder_t * p_dec )
1453 {
1454     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1455
1456     msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
1457              (char*)&p_dec->fmt_in.i_codec,
1458              (unsigned)block_FifoCount( p_owner->p_fifo ) );
1459
1460     /* Free all packets still in the decoder fifo. */
1461     block_FifoEmpty( p_owner->p_fifo );
1462     block_FifoRelease( p_owner->p_fifo );
1463
1464     /* Cleanup */
1465     if( p_owner->p_aout_input )
1466         aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
1467     if( p_owner->p_aout )
1468     {
1469         vlc_object_release( p_owner->p_aout );
1470         p_owner->p_aout = NULL;
1471     }
1472     if( p_owner->p_vout )
1473     {
1474         int i_pic;
1475
1476 #define p_pic p_owner->p_vout->render.pp_picture[i_pic]
1477         /* Hack to make sure all the the pictures are freed by the decoder */
1478         for( i_pic = 0; i_pic < p_owner->p_vout->render.i_pictures;
1479              i_pic++ )
1480         {
1481             if( p_pic->i_status == RESERVED_PICTURE )
1482                 vout_DestroyPicture( p_owner->p_vout, p_pic );
1483             if( p_pic->i_refcount > 0 )
1484                 vout_UnlinkPicture( p_owner->p_vout, p_pic );
1485         }
1486 #undef p_pic
1487
1488         /* We are about to die. Reattach video output to p_vlc. */
1489         vout_Request( p_dec, p_owner->p_vout, NULL );
1490         var_SetBool( p_owner->p_input, "intf-change-vout", true );
1491     }
1492
1493 #ifdef ENABLE_SOUT
1494     if( p_owner->p_sout_input )
1495     {
1496         sout_InputDelete( p_owner->p_sout_input );
1497         es_format_Clean( &p_owner->sout );
1498     }
1499 #endif
1500
1501     if( p_dec->fmt_in.i_cat == SPU_ES )
1502     {
1503         vout_thread_t *p_vout;
1504
1505         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1506         if( p_vout && p_owner->p_spu_vout == p_vout )
1507         {
1508             spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1509                          p_owner->i_spu_channel );
1510             vlc_object_release( p_vout );
1511         }
1512     }
1513
1514     es_format_Clean( &p_dec->fmt_in );
1515     es_format_Clean( &p_dec->fmt_out );
1516
1517     if( p_owner->p_packetizer )
1518     {
1519         module_unneed( p_owner->p_packetizer,
1520                        p_owner->p_packetizer->p_module );
1521         es_format_Clean( &p_owner->p_packetizer->fmt_in );
1522         es_format_Clean( &p_owner->p_packetizer->fmt_out );
1523         vlc_object_detach( p_owner->p_packetizer );
1524         vlc_object_release( p_owner->p_packetizer );
1525     }
1526
1527     vlc_cond_destroy( &p_owner->wait );
1528     vlc_mutex_destroy( &p_owner->lock );
1529
1530     vlc_object_detach( p_dec );
1531
1532     free( p_owner );
1533 }
1534
1535 /*****************************************************************************
1536  * Buffers allocation callbacks for the decoders
1537  *****************************************************************************/
1538 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
1539 {
1540     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1541     aout_buffer_t *p_buffer;
1542
1543     if( p_owner->p_aout_input != NULL &&
1544         ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
1545           p_dec->fmt_out.audio.i_original_channels !=
1546               p_owner->audio.i_original_channels ||
1547           p_dec->fmt_out.audio.i_bytes_per_frame !=
1548               p_owner->audio.i_bytes_per_frame ) )
1549     {
1550         aout_input_t *p_aout_input = p_owner->p_aout_input;
1551
1552         /* Parameters changed, restart the aout */
1553         vlc_mutex_lock( &p_owner->lock );
1554
1555         p_owner->p_aout_input = NULL;
1556         aout_DecDelete( p_owner->p_aout, p_aout_input );
1557
1558         vlc_mutex_unlock( &p_owner->lock );
1559     }
1560
1561     if( p_owner->p_aout_input == NULL )
1562     {
1563         const int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
1564         audio_sample_format_t format;
1565         aout_input_t *p_aout_input;
1566         aout_instance_t *p_aout;
1567
1568         p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
1569         p_owner->audio = p_dec->fmt_out.audio;
1570
1571         memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
1572         if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
1573                                     == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
1574         {
1575             if ( i_force_dolby == 1 )
1576             {
1577                 format.i_original_channels = format.i_original_channels |
1578                                              AOUT_CHAN_DOLBYSTEREO;
1579             }
1580             else /* i_force_dolby == 2 */
1581             {
1582                 format.i_original_channels = format.i_original_channels &
1583                                              ~AOUT_CHAN_DOLBYSTEREO;
1584             }
1585         }
1586
1587         p_aout = p_owner->p_aout;
1588         p_aout_input = aout_DecNew( p_dec, &p_aout,
1589                                     &format, &p_dec->fmt_out.audio_replay_gain );
1590
1591         vlc_mutex_lock( &p_owner->lock );
1592         p_owner->p_aout = p_aout;
1593         p_owner->p_aout_input = p_aout_input;
1594         vlc_mutex_unlock( &p_owner->lock );
1595
1596         if( p_owner->p_aout_input == NULL )
1597         {
1598             msg_Err( p_dec, "failed to create audio output" );
1599             p_dec->b_error = true;
1600             return NULL;
1601         }
1602         p_dec->fmt_out.audio.i_bytes_per_frame =
1603             p_owner->audio.i_bytes_per_frame;
1604     }
1605
1606     p_buffer = aout_DecNewBuffer( p_owner->p_aout_input, i_samples );
1607
1608     return p_buffer;
1609 }
1610
1611 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1612 {
1613     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1614
1615     aout_DecDeleteBuffer( p_owner->p_aout,
1616                           p_owner->p_aout_input, p_buffer );
1617 }
1618
1619
1620 int vout_CountPictureAvailable( vout_thread_t *p_vout );
1621
1622 static picture_t *vout_new_buffer( decoder_t *p_dec )
1623 {
1624     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1625     picture_t *p_pic;
1626
1627     if( p_owner->p_vout == NULL ||
1628         p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
1629         p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
1630         p_dec->fmt_out.video.i_chroma != p_owner->video.i_chroma ||
1631         p_dec->fmt_out.video.i_aspect != p_owner->video.i_aspect )
1632     {
1633         vout_thread_t *p_vout;
1634
1635         if( !p_dec->fmt_out.video.i_width ||
1636             !p_dec->fmt_out.video.i_height )
1637         {
1638             /* Can't create a new vout without display size */
1639             return NULL;
1640         }
1641
1642         if( !p_dec->fmt_out.video.i_visible_width ||
1643             !p_dec->fmt_out.video.i_visible_height )
1644         {
1645             if( p_dec->fmt_in.video.i_visible_width &&
1646                 p_dec->fmt_in.video.i_visible_height )
1647             {
1648                 p_dec->fmt_out.video.i_visible_width =
1649                     p_dec->fmt_in.video.i_visible_width;
1650                 p_dec->fmt_out.video.i_visible_height =
1651                     p_dec->fmt_in.video.i_visible_height;
1652             }
1653             else
1654             {
1655                 p_dec->fmt_out.video.i_visible_width =
1656                     p_dec->fmt_out.video.i_width;
1657                 p_dec->fmt_out.video.i_visible_height =
1658                     p_dec->fmt_out.video.i_height;
1659             }
1660         }
1661
1662         if( p_dec->fmt_out.video.i_visible_height == 1088 &&
1663             var_CreateGetBool( p_dec, "hdtv-fix" ) )
1664         {
1665             p_dec->fmt_out.video.i_visible_height = 1080;
1666             p_dec->fmt_out.video.i_sar_num *= 135;
1667             p_dec->fmt_out.video.i_sar_den *= 136;
1668             msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
1669         }
1670
1671         if( !p_dec->fmt_out.video.i_sar_num ||
1672             !p_dec->fmt_out.video.i_sar_den )
1673         {
1674             p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
1675               p_dec->fmt_out.video.i_visible_height;
1676
1677             p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
1678               p_dec->fmt_out.video.i_visible_width;
1679         }
1680
1681         vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
1682                      &p_dec->fmt_out.video.i_sar_den,
1683                      p_dec->fmt_out.video.i_sar_num,
1684                      p_dec->fmt_out.video.i_sar_den, 50000 );
1685
1686         p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
1687         p_owner->video = p_dec->fmt_out.video;
1688
1689         vlc_mutex_lock( &p_owner->lock );
1690         p_vout = p_owner->p_vout;
1691         p_owner->p_vout = NULL;
1692         vlc_mutex_unlock( &p_owner->lock );
1693
1694         p_vout = vout_Request( p_dec, p_vout, &p_dec->fmt_out.video );
1695
1696         vlc_mutex_lock( &p_owner->lock );
1697         p_owner->p_vout = p_vout;
1698         vlc_mutex_unlock( &p_owner->lock );
1699
1700         var_SetBool( p_owner->p_input, "intf-change-vout", true );
1701         if( p_vout == NULL )
1702         {
1703             msg_Err( p_dec, "failed to create video output" );
1704             p_dec->b_error = true;
1705             return NULL;
1706         }
1707
1708         if( p_owner->video.i_rmask )
1709             p_owner->p_vout->render.i_rmask = p_owner->video.i_rmask;
1710         if( p_owner->video.i_gmask )
1711             p_owner->p_vout->render.i_gmask = p_owner->video.i_gmask;
1712         if( p_owner->video.i_bmask )
1713             p_owner->p_vout->render.i_bmask = p_owner->video.i_bmask;
1714     }
1715
1716     /* Get a new picture
1717      */
1718     for( p_pic = NULL; ; )
1719     {
1720         int i_pic, i_ready_pic;
1721
1722         if( p_dec->b_die || p_dec->b_error )
1723             return NULL;
1724
1725         /* The video filter chain required that there is always 1 free buffer
1726          * that it will use as temporary one. It will release the temporary
1727          * buffer once its work is done, so this check is safe even if we don't
1728          * lock around both count() and create().
1729          */
1730         if( vout_CountPictureAvailable( p_owner->p_vout ) >= 2 )
1731         {
1732             p_pic = vout_CreatePicture( p_owner->p_vout, 0, 0, 0 );
1733             if( p_pic )
1734                 break;
1735         }
1736
1737 #define p_pic p_owner->p_vout->render.pp_picture[i_pic]
1738         /* Check the decoder doesn't leak pictures */
1739         for( i_pic = 0, i_ready_pic = 0; i_pic < p_owner->p_vout->render.i_pictures; i_pic++ )
1740         {
1741             if( p_pic->i_status == READY_PICTURE )
1742             {
1743                 i_ready_pic++;
1744                 /* If we have at least 2 ready pictures, wait for the vout thread to
1745                  * process one */
1746                 if( i_ready_pic >= 2 )
1747                     break;
1748
1749                 continue;
1750             }
1751
1752             if( p_pic->i_status == DISPLAYED_PICTURE )
1753             {
1754                 /* If at least one displayed picture is not referenced
1755                  * let vout free it */
1756                 if( p_pic->i_refcount == 0 )
1757                     break;
1758             }
1759         }
1760         if( i_pic == p_owner->p_vout->render.i_pictures )
1761         {
1762             /* Too many pictures are still referenced, there is probably a bug
1763              * with the decoder */
1764             msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
1765
1766             /* Just free all the pictures */
1767             for( i_pic = 0; i_pic < p_owner->p_vout->render.i_pictures;
1768                  i_pic++ )
1769             {
1770                 if( p_pic->i_status == RESERVED_PICTURE )
1771                     vout_DestroyPicture( p_owner->p_vout, p_pic );
1772                 if( p_pic->i_refcount > 0 )
1773                 vout_UnlinkPicture( p_owner->p_vout, p_pic );
1774             }
1775         }
1776 #undef p_pic
1777
1778         msleep( VOUT_OUTMEM_SLEEP );
1779     }
1780
1781     return p_pic;
1782 }
1783
1784 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
1785 {
1786     VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
1787 }
1788
1789 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
1790 {
1791     vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
1792 }
1793
1794 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
1795 {
1796     vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1797 }
1798
1799 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
1800 {
1801     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1802     vout_thread_t *p_vout = NULL;
1803     subpicture_t *p_subpic;
1804     int i_attempts = 30;
1805
1806     while( i_attempts-- )
1807     {
1808         if( p_dec->b_die || p_dec->b_error )
1809             break;
1810
1811         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1812         if( p_vout )
1813             break;
1814
1815         msleep( VOUT_DISPLAY_DELAY );
1816     }
1817
1818     if( !p_vout )
1819     {
1820         msg_Warn( p_dec, "no vout found, dropping subpicture" );
1821         return NULL;
1822     }
1823
1824     if( p_owner->p_spu_vout != p_vout )
1825     {
1826         spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
1827                      &p_owner->i_spu_channel );
1828         p_owner->i_spu_order = 0;
1829         p_owner->p_spu_vout = p_vout;
1830     }
1831
1832     p_subpic = subpicture_New();
1833     if( p_subpic )
1834     {
1835         p_subpic->i_channel = p_owner->i_spu_channel;
1836         p_subpic->i_order = p_owner->i_spu_order++;
1837         p_subpic->b_subtitle = true;
1838     }
1839
1840     vlc_object_release( p_vout );
1841
1842     return p_subpic;
1843 }
1844
1845 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1846 {
1847     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1848     vout_thread_t *p_vout = NULL;
1849
1850     p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1851     if( !p_vout || p_owner->p_spu_vout != p_vout )
1852     {
1853         if( p_vout )
1854             vlc_object_release( p_vout );
1855         msg_Warn( p_dec, "no vout found, leaking subpicture" );
1856         return;
1857     }
1858
1859     subpicture_Delete( p_subpic );
1860
1861     vlc_object_release( p_vout );
1862 }
1863