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