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