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