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