Made input_clock_t in charge of pts_delay.
[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 #include "ressource.h"
52
53 #include "../video_output/vout_control.h"
54
55 static decoder_t *CreateDecoder( input_thread_t *, es_format_t *, int, sout_instance_t *p_sout );
56 static void       DeleteDecoder( decoder_t * );
57
58 static void      *DecoderThread( vlc_object_t * );
59 static void       DecoderProcess( decoder_t *, block_t * );
60 static void       DecoderError( decoder_t *p_dec, block_t *p_block );
61 static void       DecoderOutputChangePause( decoder_t *, bool b_paused, mtime_t i_date );
62 static void       DecoderFlush( decoder_t * );
63 static void       DecoderSignalBuffering( decoder_t *, bool );
64 static void       DecoderFlushBuffering( decoder_t * );
65
66 static void       DecoderUnsupportedCodec( decoder_t *, vlc_fourcc_t );
67
68 /* Buffers allocation callbacks for the decoders */
69 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
70 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
71
72 static picture_t *vout_new_buffer( decoder_t * );
73 static void vout_del_buffer( decoder_t *, picture_t * );
74 static void vout_link_picture( decoder_t *, picture_t * );
75 static void vout_unlink_picture( decoder_t *, picture_t * );
76
77 static subpicture_t *spu_new_buffer( decoder_t * );
78 static void spu_del_buffer( decoder_t *, subpicture_t * );
79
80 static es_format_t null_es_format;
81
82 struct decoder_owner_sys_t
83 {
84     int64_t         i_preroll_end;
85
86     input_thread_t  *p_input;
87     input_clock_t   *p_clock;
88     int             i_last_rate;
89
90     vout_thread_t   *p_spu_vout;
91     int              i_spu_channel;
92     int64_t          i_spu_order;
93
94     sout_instance_t         *p_sout;
95     sout_packetizer_input_t *p_sout_input;
96
97     /* Some decoders require already packetized data (ie. not truncated) */
98     decoder_t *p_packetizer;
99
100     /* Current format in use by the output */
101     video_format_t video;
102     audio_format_t audio;
103     es_format_t    sout;
104
105     /* */
106     bool           b_fmt_description;
107     es_format_t    fmt_description;
108     vlc_meta_t     *p_description;
109
110     /* fifo */
111     block_fifo_t *p_fifo;
112
113     /* Lock for communication with decoder thread */
114     vlc_mutex_t lock;
115     vlc_cond_t  wait;
116
117     /* -- These variables need locking on write(only) -- */
118     aout_instance_t *p_aout;
119     aout_input_t    *p_aout_input;
120
121     vout_thread_t   *p_vout;
122
123     /* -- Theses variables need locking on read *and* write -- */
124     /* */
125     /* Pause */
126     bool b_paused;
127     struct
128     {
129         mtime_t i_date;
130         int     i_ignore;
131     } pause;
132
133     /* Buffering */
134     bool b_buffering;
135     struct
136     {
137         bool b_first;
138         bool b_full;
139         int  i_count;
140
141         picture_t     *p_picture;
142         picture_t     **pp_picture_next;
143
144         subpicture_t  *p_subpic;
145         subpicture_t  **pp_subpic_next;
146
147         aout_buffer_t *p_audio;
148         aout_buffer_t **pp_audio_next;
149
150         block_t       *p_block;
151         block_t       **pp_block_next;
152     } buffer;
153
154     /* Flushing */
155     bool b_flushing;
156
157     /* CC */
158     struct
159     {
160         bool b_supported;
161         bool pb_present[4];
162         decoder_t *pp_decoder[4];
163     } cc;
164
165     /* Delay */
166     mtime_t i_ts_delay;
167 };
168
169 #define DECODER_MAX_BUFFERING_COUNT (4)
170 #define DECODER_MAX_BUFFERING_AUDIO_DURATION (AOUT_MAX_PREPARE_TIME)
171 #define DECODER_MAX_BUFFERING_VIDEO_DURATION (1*CLOCK_FREQ)
172
173 /* Pictures which are DECODER_BOGUS_VIDEO_DELAY or more in advance probably have
174  * a bogus PTS and won't be displayed */
175 #define DECODER_BOGUS_VIDEO_DELAY                ((mtime_t)(DEFAULT_PTS_DELAY * 30))
176
177 /* */
178 #define DECODER_SPU_VOUT_WAIT_DURATION ((int)(0.200*CLOCK_FREQ))
179
180
181 /*****************************************************************************
182  * Public functions
183  *****************************************************************************/
184 picture_t *decoder_NewPicture( decoder_t *p_decoder )
185 {
186     picture_t *p_picture = p_decoder->pf_vout_buffer_new( p_decoder );
187     if( !p_picture )
188         msg_Warn( p_decoder, "can't get output picture" );
189     return p_picture;
190 }
191 void decoder_DeletePicture( decoder_t *p_decoder, picture_t *p_picture )
192 {
193     p_decoder->pf_vout_buffer_del( p_decoder, p_picture );
194 }
195 void decoder_LinkPicture( decoder_t *p_decoder, picture_t *p_picture )
196 {
197     p_decoder->pf_picture_link( p_decoder, p_picture );
198 }
199 void decoder_UnlinkPicture( decoder_t *p_decoder, picture_t *p_picture )
200 {
201     p_decoder->pf_picture_unlink( p_decoder, p_picture );
202 }
203
204 aout_buffer_t *decoder_NewAudioBuffer( decoder_t *p_decoder, int i_size )
205 {
206     if( !p_decoder->pf_aout_buffer_new )
207         return NULL;
208     return p_decoder->pf_aout_buffer_new( p_decoder, i_size );
209 }
210 void decoder_DeleteAudioBuffer( decoder_t *p_decoder, aout_buffer_t *p_buffer )
211 {
212     p_decoder->pf_aout_buffer_del( p_decoder, p_buffer );
213 }
214
215 subpicture_t *decoder_NewSubpicture( decoder_t *p_decoder )
216 {
217     subpicture_t *p_subpicture = p_decoder->pf_spu_buffer_new( p_decoder );
218     if( !p_subpicture )
219         msg_Warn( p_decoder, "can't get output subpicture" );
220     return p_subpicture;
221 }
222 void decoder_DeleteSubpicture( decoder_t *p_decoder, subpicture_t *p_subpicture )
223 {
224     p_decoder->pf_spu_buffer_del( p_decoder, p_subpicture );
225 }
226
227 /* decoder_GetInputAttachments:
228  */
229 int decoder_GetInputAttachments( decoder_t *p_dec,
230                                  input_attachment_t ***ppp_attachment,
231                                  int *pi_attachment )
232 {
233     if( !p_dec->pf_get_attachments )
234         return VLC_EGENERIC;
235
236     return p_dec->pf_get_attachments( p_dec, ppp_attachment, pi_attachment );
237 }
238 /* decoder_GetDisplayDate:
239  */
240 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
241 {
242     if( !p_dec->pf_get_display_date )
243         return 0;
244
245     return p_dec->pf_get_display_date( p_dec, i_ts );
246 }
247 /* decoder_GetDisplayRate:
248  */
249 int decoder_GetDisplayRate( decoder_t *p_dec )
250 {
251     if( !p_dec->pf_get_display_rate )
252         return INPUT_RATE_DEFAULT;
253
254     return p_dec->pf_get_display_rate( p_dec );
255 }
256
257 /**
258  * Spawns a new decoder thread
259  *
260  * \param p_input the input thread
261  * \param p_es the es descriptor
262  * \return the spawned decoder object
263  */
264 decoder_t *input_DecoderNew( input_thread_t *p_input,
265                              es_format_t *fmt, input_clock_t *p_clock, sout_instance_t *p_sout  )
266 {
267     decoder_t *p_dec = NULL;
268     int i_priority;
269
270 #ifdef ENABLE_SOUT
271     /* If we are in sout mode, search for packetizer module */
272     if( p_sout )
273     {
274         /* Create the decoder configuration structure */
275         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER, p_sout );
276         if( p_dec == NULL )
277         {
278             msg_Err( p_input, "could not create packetizer" );
279             intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
280                             _("VLC could not open the packetizer module.") );
281             return NULL;
282         }
283     }
284     else
285 #endif
286     {
287         /* Create the decoder configuration structure */
288         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER, p_sout );
289         if( p_dec == NULL )
290         {
291             msg_Err( p_input, "could not create decoder" );
292             intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
293                             _("VLC could not open the decoder module.") );
294             return NULL;
295         }
296     }
297
298     if( !p_dec->p_module )
299     {
300         DecoderUnsupportedCodec( p_dec, fmt->i_codec );
301
302         DeleteDecoder( p_dec );
303         vlc_object_release( p_dec );
304         return NULL;
305     }
306
307     p_dec->p_owner->p_clock = p_clock;
308
309     if( fmt->i_cat == AUDIO_ES )
310         i_priority = VLC_THREAD_PRIORITY_AUDIO;
311     else
312         i_priority = VLC_THREAD_PRIORITY_VIDEO;
313
314     /* Spawn the decoder thread */
315     if( vlc_thread_create( p_dec, "decoder", DecoderThread, i_priority ) )
316     {
317         msg_Err( p_dec, "cannot spawn decoder thread" );
318         module_unneed( p_dec, p_dec->p_module );
319         DeleteDecoder( p_dec );
320         vlc_object_release( p_dec );
321         return NULL;
322     }
323
324     return p_dec;
325 }
326
327 /**
328  * Kills a decoder thread and waits until it's finished
329  *
330  * \param p_input the input thread
331  * \param p_es the es descriptor
332  * \return nothing
333  */
334 void input_DecoderDelete( decoder_t *p_dec )
335 {
336     decoder_owner_sys_t *p_owner = p_dec->p_owner;
337
338     vlc_object_kill( p_dec );
339
340     /* Make sure we aren't paused/buffering/waiting anymore */
341     vlc_mutex_lock( &p_owner->lock );
342     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, i_ts, INT64_MAX );
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     }
882     return NULL;
883 }
884
885 static void DecoderFlush( decoder_t *p_dec )
886 {
887     decoder_owner_sys_t *p_owner = p_dec->p_owner;
888     block_t *p_null;
889
890     vlc_assert_locked( &p_owner->lock );
891
892     /* Empty the fifo */
893     block_FifoEmpty( p_owner->p_fifo );
894
895     /* Monitor for flush end */
896     p_owner->b_flushing = true;
897     vlc_cond_signal( &p_owner->wait );
898
899     /* Send a special block */
900     p_null = block_New( p_dec, 128 );
901     if( !p_null )
902         return;
903     p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
904     p_null->i_flags |= BLOCK_FLAG_CORE_FLUSH;
905     if( !p_dec->fmt_in.b_packetized )
906         p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
907     memset( p_null->p_buffer, 0, p_null->i_buffer );
908
909     input_DecoderDecode( p_dec, p_null );
910
911     /* */
912     while( vlc_object_alive( p_dec ) && p_owner->b_flushing )
913         vlc_cond_wait( &p_owner->wait, &p_owner->lock );
914 }
915
916 static void DecoderSignalFlushed( decoder_t *p_dec )
917 {
918     decoder_owner_sys_t *p_owner = p_dec->p_owner;
919
920     vlc_mutex_lock( &p_owner->lock );
921
922     if( p_owner->b_flushing )
923     {
924         p_owner->b_flushing = false;
925         vlc_cond_signal( &p_owner->wait );
926     }
927
928     vlc_mutex_unlock( &p_owner->lock );
929 }
930
931 static void DecoderSignalBuffering( decoder_t *p_dec, bool b_full )
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_buffering )
938     {
939         if( b_full )
940             p_owner->buffer.b_full = true;
941         vlc_cond_signal( &p_owner->wait );
942     }
943
944     vlc_mutex_unlock( &p_owner->lock );
945 }
946
947 static bool DecoderIsFlushing( decoder_t *p_dec )
948 {
949     decoder_owner_sys_t *p_owner = p_dec->p_owner;
950     bool b_flushing;
951
952     vlc_mutex_lock( &p_owner->lock );
953
954     b_flushing = p_owner->b_flushing;
955
956     vlc_mutex_unlock( &p_owner->lock );
957
958     return b_flushing;
959 }
960
961 static void DecoderWaitUnblock( decoder_t *p_dec, bool *pb_reject )
962 {
963     decoder_owner_sys_t *p_owner = p_dec->p_owner;
964
965     vlc_assert_locked( &p_owner->lock );
966
967     while( !p_owner->b_flushing )
968     {
969         if( p_owner->b_paused )
970         {
971             if( p_owner->b_buffering && !p_owner->buffer.b_full )
972                 break;
973             if( p_owner->pause.i_ignore > 0 )
974             {
975                 p_owner->pause.i_ignore--;
976                 break;
977             }
978         }
979         else
980         {
981             if( !p_owner->b_buffering || !p_owner->buffer.b_full )
982                 break;
983         }
984         vlc_cond_wait( &p_owner->wait, &p_owner->lock );
985     }
986
987     if( pb_reject )
988         *pb_reject = p_owner->b_flushing;
989 }
990
991 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
992 {
993     decoder_owner_sys_t *p_owner = p_dec->p_owner;
994
995     vlc_assert_locked( &p_owner->lock );
996
997     /* XXX only audio and video output have to be paused.
998      * - for sout it is useless
999      * - for subs, it is done by the vout
1000      */
1001     if( p_dec->fmt_in.i_cat == AUDIO_ES )
1002     {
1003         if( p_owner->p_aout && p_owner->p_aout_input )
1004             aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
1005                                  b_paused, i_date );
1006     }
1007     else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1008     {
1009         if( p_owner->p_vout )
1010             vout_ChangePause( p_owner->p_vout, b_paused, i_date );
1011     }
1012 }
1013 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
1014 {
1015     if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
1016         *pi_preroll = INT64_MAX;
1017     else if( p->i_pts > 0 )
1018         *pi_preroll = __MIN( *pi_preroll, p->i_pts );
1019     else if( p->i_dts > 0 )
1020         *pi_preroll = __MIN( *pi_preroll, p->i_dts );
1021 }
1022
1023 static mtime_t DecoderTeletextFixTs( mtime_t i_ts )
1024 {
1025     mtime_t current_date = mdate();
1026
1027     /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
1028     if( !i_ts || i_ts > current_date + 10000000 || i_ts < current_date )
1029     {
1030         /* ETSI EN 300 472 Annex A : do not take into account the PTS
1031          * for teletext streams. */
1032         return current_date + 400000;
1033     }
1034     return i_ts;
1035 }
1036
1037 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
1038                           mtime_t *pi_duration, int *pi_rate, mtime_t i_ts_bound, bool b_telx )
1039 {
1040     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1041     input_clock_t   *p_clock = p_owner->p_clock;
1042     int i_rate = 0;
1043
1044     vlc_assert_locked( &p_owner->lock );
1045
1046     const mtime_t i_es_delay = p_owner->i_ts_delay;
1047
1048     if( p_clock )
1049     {
1050         const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
1051
1052         if( *pi_ts0 > 0 )
1053             *pi_ts0 = input_clock_GetTS( p_clock, &i_rate, *pi_ts0 + i_es_delay, i_ts_bound );
1054         if( pi_ts1 && *pi_ts1 > 0 )
1055         {
1056             if( *pi_ts0 > 0 )
1057                 *pi_ts1 = input_clock_GetTS( p_clock, &i_rate, *pi_ts1 + i_es_delay, INT64_MAX );
1058             else
1059                 *pi_ts1 = 0;
1060         }
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 );
1079             if( pi_ts1 && *pi_ts1 <= 0 )
1080                 *pi_ts1 = *pi_ts0;
1081         }
1082     }
1083 }
1084
1085 static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
1086                               int *pi_played_sum, int *pi_lost_sum )
1087 {
1088     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1089     aout_instance_t *p_aout = p_owner->p_aout;
1090     aout_input_t    *p_aout_input = p_owner->p_aout_input;
1091
1092     /* */
1093     if( p_audio->start_date <= 0 )
1094     {
1095         msg_Warn( p_dec, "non-dated audio buffer received" );
1096         *pi_lost_sum += 1;
1097         aout_BufferFree( p_audio );
1098         return;
1099     }
1100
1101     /* */
1102     vlc_mutex_lock( &p_owner->lock );
1103
1104     if( p_owner->b_buffering || p_owner->buffer.p_audio )
1105     {
1106         p_audio->p_next = NULL;
1107
1108         *p_owner->buffer.pp_audio_next = p_audio;
1109         p_owner->buffer.pp_audio_next = &p_audio->p_next;
1110
1111         p_owner->buffer.i_count++;
1112         if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1113             p_audio->start_date - p_owner->buffer.p_audio->start_date > DECODER_MAX_BUFFERING_AUDIO_DURATION )
1114         {
1115             p_owner->buffer.b_full = true;
1116             vlc_cond_signal( &p_owner->wait );
1117         }
1118     }
1119
1120     for( ;; )
1121     {
1122         bool b_has_more = false;
1123         bool b_reject;
1124         DecoderWaitUnblock( p_dec, &b_reject );
1125
1126         if( p_owner->b_buffering )
1127         {
1128             vlc_mutex_unlock( &p_owner->lock );
1129             return;
1130         }
1131
1132         /* */
1133         if( p_owner->buffer.p_audio )
1134         {
1135             p_audio = p_owner->buffer.p_audio;
1136
1137             p_owner->buffer.p_audio = p_audio->p_next;
1138             p_owner->buffer.i_count--;
1139
1140             b_has_more = p_owner->buffer.p_audio != NULL;
1141             if( !b_has_more )
1142                 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1143         }
1144
1145         /* */
1146         const bool b_dated = p_audio->start_date > 0;
1147         int i_rate = INPUT_RATE_DEFAULT;
1148
1149         DecoderFixTs( p_dec, &p_audio->start_date, &p_audio->end_date, NULL,
1150                       &i_rate, AOUT_MAX_ADVANCE_TIME, false );
1151
1152         vlc_mutex_unlock( &p_owner->lock );
1153
1154         if( !p_aout || !p_aout_input ||
1155             p_audio->start_date <= 0 ||
1156             i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE ||
1157             i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
1158             b_reject = true;
1159
1160         /* Do not wait against unprotected date */
1161         const mtime_t i_deadline = p_audio->start_date - AOUT_MAX_PREPARE_TIME;
1162         while( !b_reject && i_deadline > mdate() )
1163         {
1164             vlc_mutex_lock( &p_owner->lock );
1165             if( p_owner->b_flushing || p_dec->b_die )
1166             {
1167                 b_reject = true;
1168                 vlc_mutex_unlock( &p_owner->lock );
1169                 break;
1170             }
1171             vlc_cond_timedwait( &p_owner->wait, &p_owner->lock, i_deadline );
1172             vlc_mutex_unlock( &p_owner->lock );
1173         }
1174
1175         if( !b_reject )
1176         {
1177             if( !aout_DecPlay( p_aout, p_aout_input, p_audio, i_rate ) )
1178                 *pi_played_sum += 1;
1179             *pi_lost_sum += aout_DecGetResetLost( p_aout, p_aout_input );
1180         }
1181         else
1182         {
1183             if( b_dated )
1184                 msg_Warn( p_aout, "received buffer in the future" );
1185             else
1186                 msg_Warn( p_dec, "non-dated audio buffer received" );
1187
1188             *pi_lost_sum += 1;
1189             aout_BufferFree( p_audio );
1190         }
1191
1192         if( !b_has_more )
1193             break;
1194
1195         vlc_mutex_lock( &p_owner->lock );
1196         if( !p_owner->buffer.p_audio )
1197         {
1198             vlc_mutex_unlock( &p_owner->lock );
1199             break;
1200         }
1201     }
1202 }
1203
1204 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1205 {
1206     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1207     input_thread_t  *p_input = p_owner->p_input;
1208     aout_buffer_t   *p_aout_buf;
1209     int i_decoded = 0;
1210     int i_lost = 0;
1211     int i_played = 0;
1212
1213     while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1214     {
1215         aout_instance_t *p_aout = p_owner->p_aout;
1216         aout_input_t    *p_aout_input = p_owner->p_aout_input;
1217
1218         if( p_dec->b_die )
1219         {
1220             /* It prevent freezing VLC in case of broken decoder */
1221             aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1222             if( p_block )
1223                 block_Release( p_block );
1224             break;
1225         }
1226         i_decoded++;
1227
1228         if( p_aout_buf->start_date < p_owner->i_preroll_end )
1229         {
1230             aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1231             continue;
1232         }
1233
1234         if( p_owner->i_preroll_end > 0 )
1235         {
1236             msg_Dbg( p_dec, "End of audio preroll" );
1237             if( p_owner->p_aout && p_owner->p_aout_input )
1238                 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1239             /* */
1240             p_owner->i_preroll_end = -1;
1241         }
1242
1243         DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1244     }
1245
1246     /* Update ugly stat */
1247     if( i_decoded > 0 || i_lost > 0 || i_played > 0 )
1248     {
1249         vlc_mutex_lock( &p_input->p->counters.counters_lock);
1250
1251         stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
1252                              i_lost, NULL );
1253         stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
1254                              i_played, NULL );
1255         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
1256                              i_decoded, NULL );
1257
1258         vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1259     }
1260 }
1261 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1262 {
1263     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1264     block_t *p_cc;
1265     bool pb_present[4];
1266     int i;
1267     int i_cc_decoder;
1268
1269     assert( p_dec_cc->pf_get_cc != NULL );
1270
1271     /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1272     if( !p_owner->cc.b_supported )
1273         return;
1274
1275     p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1276     if( !p_cc )
1277         return;
1278
1279     vlc_mutex_lock( &p_owner->lock );
1280     for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1281     {
1282         p_owner->cc.pb_present[i] |= pb_present[i];
1283         if( p_owner->cc.pp_decoder[i] )
1284             i_cc_decoder++;
1285     }
1286
1287     for( i = 0; i < 4; i++ )
1288     {
1289         if( !p_owner->cc.pp_decoder[i] )
1290             continue;
1291
1292         if( i_cc_decoder > 1 )
1293             DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
1294         else
1295             DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
1296         i_cc_decoder--;
1297     }
1298     vlc_mutex_unlock( &p_owner->lock );
1299 }
1300
1301 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
1302                               int *pi_played_sum, int *pi_lost_sum )
1303 {
1304     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1305     vout_thread_t  *p_vout = p_owner->p_vout;
1306     bool b_first_buffered;
1307
1308     if( p_picture->date <= 0 )
1309     {
1310         msg_Warn( p_vout, "non-dated video buffer received" );
1311         *pi_lost_sum += 1;
1312         vout_DropPicture( p_vout, p_picture );
1313         return;
1314     }
1315
1316     /* */
1317     vlc_mutex_lock( &p_owner->lock );
1318
1319     if( ( p_owner->b_buffering && !p_owner->buffer.b_first ) || p_owner->buffer.p_picture )
1320     {
1321         p_picture->p_next = NULL;
1322
1323         *p_owner->buffer.pp_picture_next = p_picture;
1324         p_owner->buffer.pp_picture_next = &p_picture->p_next;
1325
1326         p_owner->buffer.i_count++;
1327         if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1328             p_picture->date - p_owner->buffer.p_picture->date > DECODER_MAX_BUFFERING_VIDEO_DURATION )
1329         {
1330             p_owner->buffer.b_full = true;
1331             vlc_cond_signal( &p_owner->wait );
1332         }
1333     }
1334     b_first_buffered = p_owner->buffer.p_picture != NULL;
1335
1336     for( ;; b_first_buffered = false )
1337     {
1338         bool b_has_more = false;
1339
1340         bool b_reject;
1341
1342         DecoderWaitUnblock( p_dec, &b_reject );
1343
1344         if( p_owner->b_buffering && !p_owner->buffer.b_first )
1345         {
1346             vlc_mutex_unlock( &p_owner->lock );
1347             return;
1348         }
1349         bool b_buffering_first = p_owner->b_buffering;
1350
1351         /* */
1352         if( p_owner->buffer.p_picture )
1353         {
1354             p_picture = p_owner->buffer.p_picture;
1355
1356             p_owner->buffer.p_picture = p_picture->p_next;
1357             p_owner->buffer.i_count--;
1358
1359             b_has_more = p_owner->buffer.p_picture != NULL;
1360             if( !b_has_more )
1361                 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1362         }
1363
1364         /* */
1365         if( b_buffering_first )
1366         {
1367             assert( p_owner->buffer.b_first );
1368             assert( !p_owner->buffer.i_count );
1369             msg_Dbg( p_dec, "Received first picture" );
1370             p_owner->buffer.b_first = false;
1371             p_picture->b_force = true;
1372         }
1373
1374         const bool b_dated = p_picture->date > 0;
1375         int i_rate = INPUT_RATE_DEFAULT;
1376         DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1377                       &i_rate, DECODER_BOGUS_VIDEO_DELAY, false );
1378
1379         vlc_mutex_unlock( &p_owner->lock );
1380
1381         /* */
1382         if( !p_picture->b_force && p_picture->date <= 0 )
1383             b_reject = true;
1384
1385         if( !b_reject )
1386         {
1387             if( i_rate != p_owner->i_last_rate || b_first_buffered )
1388             {
1389                 /* Be sure to not display old picture after our own */
1390                 vout_Flush( p_vout, p_picture->date );
1391                 p_owner->i_last_rate = i_rate;
1392             }
1393             vout_DisplayPicture( p_vout, p_picture );
1394         }
1395         else
1396         {
1397             if( b_dated )
1398                 msg_Warn( p_vout, "early picture skipped" );
1399             else
1400                 msg_Warn( p_vout, "non-dated video buffer received" );
1401
1402             *pi_lost_sum += 1;
1403             vout_DropPicture( p_vout, p_picture );
1404         }
1405         int i_tmp_display;
1406         int i_tmp_lost;
1407         vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1408
1409         *pi_played_sum += i_tmp_display;
1410         *pi_lost_sum += i_tmp_lost;
1411
1412         if( !b_has_more || b_buffering_first )
1413             break;
1414
1415         vlc_mutex_lock( &p_owner->lock );
1416         if( !p_owner->buffer.p_picture )
1417         {
1418             vlc_mutex_unlock( &p_owner->lock );
1419             break;
1420         }
1421     }
1422 }
1423
1424 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1425 {
1426     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1427     input_thread_t *p_input = p_owner->p_input;
1428     picture_t      *p_pic;
1429     int i_lost = 0;
1430     int i_decoded = 0;
1431     int i_displayed = 0;
1432
1433     while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1434     {
1435         vout_thread_t  *p_vout = p_owner->p_vout;
1436         if( p_dec->b_die )
1437         {
1438             /* It prevent freezing VLC in case of broken decoder */
1439             vout_DropPicture( p_vout, p_pic );
1440             if( p_block )
1441                 block_Release( p_block );
1442             break;
1443         }
1444
1445         i_decoded++;
1446
1447         if( p_pic->date < p_owner->i_preroll_end )
1448         {
1449             vout_DropPicture( p_vout, p_pic );
1450             continue;
1451         }
1452
1453         if( p_owner->i_preroll_end > 0 )
1454         {
1455             msg_Dbg( p_dec, "End of video preroll" );
1456             if( p_vout )
1457                 vout_Flush( p_vout, 1 );
1458             /* */
1459             p_owner->i_preroll_end = -1;
1460         }
1461
1462         if( p_dec->pf_get_cc &&
1463             ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1464             DecoderGetCc( p_dec, p_dec );
1465
1466         DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
1467     }
1468     if( i_decoded > 0 || i_lost > 0 || i_displayed > 0 )
1469     {
1470         vlc_mutex_lock( &p_input->p->counters.counters_lock );
1471
1472         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
1473                              i_decoded, NULL );
1474         stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
1475                              i_lost , NULL);
1476
1477         stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
1478                              i_displayed, NULL);
1479
1480         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1481     }
1482 }
1483
1484 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic,
1485                             bool b_telx )
1486 {
1487     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1488     vout_thread_t *p_vout = p_owner->p_spu_vout;
1489
1490     /* */
1491     if( p_subpic->i_start <= 0 )
1492     {
1493         msg_Warn( p_dec, "non-dated spu buffer received" );
1494         subpicture_Delete( p_subpic );
1495         return;
1496     }
1497
1498     /* */
1499     vlc_mutex_lock( &p_owner->lock );
1500
1501     if( p_owner->b_buffering || p_owner->buffer.p_subpic )
1502     {
1503         p_subpic->p_next = NULL;
1504
1505         *p_owner->buffer.pp_subpic_next = p_subpic;
1506         p_owner->buffer.pp_subpic_next = &p_subpic->p_next;
1507
1508         p_owner->buffer.i_count++;
1509         /* XXX it is important to be full after the first one */
1510         if( p_owner->buffer.i_count > 0 )
1511         {
1512             p_owner->buffer.b_full = true;
1513             vlc_cond_signal( &p_owner->wait );
1514         }
1515     }
1516
1517     for( ;; )
1518     {
1519         bool b_has_more = false;
1520         bool b_reject;
1521         DecoderWaitUnblock( p_dec, &b_reject );
1522
1523         if( p_owner->b_buffering )
1524         {
1525             vlc_mutex_unlock( &p_owner->lock );
1526             return;
1527         }
1528
1529         /* */
1530         if( p_owner->buffer.p_subpic )
1531         {
1532             p_subpic = p_owner->buffer.p_subpic;
1533
1534             p_owner->buffer.p_subpic = p_subpic->p_next;
1535             p_owner->buffer.i_count--;
1536
1537             b_has_more = p_owner->buffer.p_subpic != NULL;
1538             if( !b_has_more )
1539                 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1540         }
1541
1542         /* */
1543         DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1544                       NULL, INT64_MAX, b_telx );
1545
1546         vlc_mutex_unlock( &p_owner->lock );
1547
1548         if( !b_reject )
1549             spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
1550         else
1551             subpicture_Delete( p_subpic );
1552
1553         if( !b_has_more )
1554             break;
1555         vlc_mutex_lock( &p_owner->lock );
1556         if( !p_owner->buffer.p_subpic )
1557         {
1558             vlc_mutex_unlock( &p_owner->lock );
1559             break;
1560         }
1561     }
1562 }
1563
1564 static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block,
1565                              bool b_telx )
1566 {
1567     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1568
1569     assert( p_owner->p_clock );
1570     assert( !p_sout_block->p_next );
1571
1572     vlc_mutex_lock( &p_owner->lock );
1573
1574     if( p_owner->b_buffering || p_owner->buffer.p_block )
1575     {
1576         block_ChainLastAppend( &p_owner->buffer.pp_block_next, p_sout_block );
1577
1578         p_owner->buffer.i_count++;
1579         /* XXX it is important to be full after the first one */
1580         if( p_owner->buffer.i_count > 0 )
1581         {
1582             p_owner->buffer.b_full = true;
1583             vlc_cond_signal( &p_owner->wait );
1584         }
1585     }
1586
1587     for( ;; )
1588     {
1589         bool b_has_more = false;
1590         bool b_reject;
1591         DecoderWaitUnblock( p_dec, &b_reject );
1592
1593         if( p_owner->b_buffering )
1594         {
1595             vlc_mutex_unlock( &p_owner->lock );
1596             return;
1597         }
1598
1599         /* */
1600         if( p_owner->buffer.p_block )
1601         {
1602             p_sout_block = p_owner->buffer.p_block;
1603
1604             p_owner->buffer.p_block = p_sout_block->p_next;
1605             p_owner->buffer.i_count--;
1606
1607             b_has_more = p_owner->buffer.p_block != NULL;
1608             if( !b_has_more )
1609                 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1610         }
1611         p_sout_block->p_next = NULL;
1612
1613         DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
1614                       &p_sout_block->i_length,
1615                       &p_sout_block->i_rate, INT64_MAX, b_telx );
1616
1617         vlc_mutex_unlock( &p_owner->lock );
1618
1619         if( !b_reject )
1620             sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block );
1621         else
1622             block_Release( p_sout_block );
1623
1624         if( !b_has_more )
1625             break;
1626         vlc_mutex_lock( &p_owner->lock );
1627         if( !p_owner->buffer.p_block )
1628         {
1629             vlc_mutex_unlock( &p_owner->lock );
1630             break;
1631         }
1632     }
1633 }
1634
1635 /* */
1636 static void DecoderFlushBuffering( decoder_t *p_dec )
1637 {
1638     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1639
1640     vlc_assert_locked( &p_owner->lock );
1641
1642     while( p_owner->buffer.p_picture )
1643     {
1644         picture_t *p_picture = p_owner->buffer.p_picture;
1645
1646         p_owner->buffer.p_picture = p_picture->p_next;
1647         p_owner->buffer.i_count--;
1648
1649         if( p_owner->p_vout )
1650             vout_DropPicture( p_owner->p_vout, p_picture );
1651
1652         if( !p_owner->buffer.p_picture )
1653             p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1654     }
1655     while( p_owner->buffer.p_audio )
1656     {
1657         aout_buffer_t *p_audio = p_owner->buffer.p_audio;
1658
1659         p_owner->buffer.p_audio = p_audio->p_next;
1660         p_owner->buffer.i_count--;
1661
1662         aout_BufferFree( p_audio );
1663
1664         if( !p_owner->buffer.p_audio )
1665             p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1666     }
1667     while( p_owner->buffer.p_subpic )
1668     {
1669         subpicture_t *p_subpic = p_owner->buffer.p_subpic;
1670
1671         p_owner->buffer.p_subpic = p_subpic->p_next;
1672         p_owner->buffer.i_count--;
1673
1674         subpicture_Delete( p_subpic );
1675
1676         if( !p_owner->buffer.p_subpic )
1677             p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1678     }
1679     if( p_owner->buffer.p_block )
1680     {
1681         block_ChainRelease( p_owner->buffer.p_block );
1682
1683         p_owner->buffer.i_count = 0;
1684         p_owner->buffer.p_block = NULL;
1685         p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1686     }
1687 }
1688
1689 /* This function process a block for sout
1690  */
1691 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1692 {
1693     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1694     const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1695     block_t *p_sout_block;
1696
1697     while( ( p_sout_block =
1698                  p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1699     {
1700         if( !p_owner->p_sout_input )
1701         {
1702             es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1703
1704             p_owner->sout.i_group = p_dec->fmt_in.i_group;
1705             p_owner->sout.i_id = p_dec->fmt_in.i_id;
1706             if( p_dec->fmt_in.psz_language )
1707             {
1708                 if( p_owner->sout.psz_language )
1709                     free( p_owner->sout.psz_language );
1710                 p_owner->sout.psz_language =
1711                     strdup( p_dec->fmt_in.psz_language );
1712             }
1713
1714             p_owner->p_sout_input =
1715                 sout_InputNew( p_owner->p_sout,
1716                                &p_owner->sout );
1717
1718             if( p_owner->p_sout_input == NULL )
1719             {
1720                 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1721                          (char *)&p_owner->sout.i_codec );
1722                 p_dec->b_error = true;
1723
1724                 while( p_sout_block )
1725                 {
1726                     block_t *p_next = p_sout_block->p_next;
1727                     block_Release( p_sout_block );
1728                     p_sout_block = p_next;
1729                 }
1730                 break;
1731             }
1732         }
1733
1734         while( p_sout_block )
1735         {
1736             block_t *p_next = p_sout_block->p_next;
1737
1738             p_sout_block->p_next = NULL;
1739
1740             DecoderPlaySout( p_dec, p_sout_block, b_telx );
1741
1742             p_sout_block = p_next;
1743         }
1744
1745         /* For now it's enough, as only sout impact on this flag */
1746         if( p_owner->p_sout->i_out_pace_nocontrol > 0 &&
1747             p_owner->p_input->p->b_out_pace_control )
1748         {
1749             msg_Dbg( p_dec, "switching to sync mode" );
1750             p_owner->p_input->p->b_out_pace_control = false;
1751         }
1752         else if( p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
1753                  !p_owner->p_input->p->b_out_pace_control )
1754         {
1755             msg_Dbg( p_dec, "switching to async mode" );
1756             p_owner->p_input->p->b_out_pace_control = true;
1757         }
1758     }
1759 }
1760
1761 /* This function process a video block
1762  */
1763 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1764 {
1765     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1766
1767     if( p_owner->p_packetizer )
1768     {
1769         block_t *p_packetized_block;
1770         decoder_t *p_packetizer = p_owner->p_packetizer;
1771
1772         while( (p_packetized_block =
1773                 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1774         {
1775             if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1776             {
1777                 es_format_Clean( &p_dec->fmt_in );
1778                 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1779             }
1780             if( p_packetizer->pf_get_cc )
1781                 DecoderGetCc( p_dec, p_packetizer );
1782
1783             while( p_packetized_block )
1784             {
1785                 block_t *p_next = p_packetized_block->p_next;
1786                 p_packetized_block->p_next = NULL;
1787
1788                 DecoderDecodeVideo( p_dec, p_packetized_block );
1789
1790                 p_packetized_block = p_next;
1791             }
1792         }
1793     }
1794     else if( p_block )
1795     {
1796         DecoderDecodeVideo( p_dec, p_block );
1797     }
1798
1799     if( b_flush && p_owner->p_vout )
1800         vout_Flush( p_owner->p_vout, 1 );
1801 }
1802
1803 /* This function process a audio block
1804  */
1805 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1806 {
1807     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1808
1809     if( p_owner->p_packetizer )
1810     {
1811         block_t *p_packetized_block;
1812         decoder_t *p_packetizer = p_owner->p_packetizer;
1813
1814         while( (p_packetized_block =
1815                 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1816         {
1817             if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1818             {
1819                 es_format_Clean( &p_dec->fmt_in );
1820                 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1821             }
1822
1823             while( p_packetized_block )
1824             {
1825                 block_t *p_next = p_packetized_block->p_next;
1826                 p_packetized_block->p_next = NULL;
1827
1828                 DecoderDecodeAudio( p_dec, p_packetized_block );
1829
1830                 p_packetized_block = p_next;
1831             }
1832         }
1833     }
1834     else if( p_block )
1835     {
1836         DecoderDecodeAudio( p_dec, p_block );
1837     }
1838
1839     if( b_flush && p_owner->p_aout && p_owner->p_aout_input )
1840         aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1841 }
1842
1843 /* This function process a subtitle block
1844  */
1845 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1846 {
1847     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1848     const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1849
1850     input_thread_t *p_input = p_owner->p_input;
1851     vout_thread_t *p_vout;
1852     subpicture_t *p_spu;
1853
1854     while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1855     {
1856         vlc_mutex_lock( &p_input->p->counters.counters_lock );
1857         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1858         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1859
1860         p_vout = input_ressource_HoldVout( p_input->p->p_ressource );
1861         if( p_vout && p_owner->p_spu_vout == p_vout )
1862         {
1863             /* Preroll does not work very well with subtitle */
1864             if( p_spu->i_start > 0 &&
1865                 p_spu->i_start < p_owner->i_preroll_end &&
1866                 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_owner->i_preroll_end ) )
1867             {
1868                 subpicture_Delete( p_spu );
1869             }
1870             else
1871             {
1872                 DecoderPlaySpu( p_dec, p_spu, b_telx );
1873             }
1874         }
1875         else
1876         {
1877             subpicture_Delete( p_spu );
1878         }
1879         if( p_vout )
1880             vlc_object_release( p_vout );
1881     }
1882
1883     if( b_flush && p_owner->p_spu_vout )
1884     {
1885         p_vout = input_ressource_HoldVout( p_input->p->p_ressource );
1886
1887         if( p_vout && p_owner->p_spu_vout == p_vout )
1888             spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1889                          p_owner->i_spu_channel );
1890
1891         if( p_vout )
1892             vlc_object_release( p_vout );
1893     }
1894 }
1895
1896 /* */
1897 static void DecoderProcessOnFlush( decoder_t *p_dec )
1898 {
1899     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1900
1901     vlc_mutex_lock( &p_owner->lock );
1902     DecoderFlushBuffering( p_dec );
1903     vlc_mutex_unlock( &p_owner->lock );
1904
1905     DecoderSignalFlushed( p_dec );
1906 }
1907 /**
1908  * Decode a block
1909  *
1910  * \param p_dec the decoder object
1911  * \param p_block the block to decode
1912  * \return VLC_SUCCESS or an error code
1913  */
1914 static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
1915 {
1916     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1917     const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1918
1919     if( p_block && p_block->i_buffer <= 0 )
1920     {
1921         assert( !b_flush_request );
1922         block_Release( p_block );
1923         return;
1924     }
1925
1926 #ifdef ENABLE_SOUT
1927     if( vlc_internals( p_dec )->i_object_type == VLC_OBJECT_PACKETIZER )
1928     {
1929         if( p_block )
1930             p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1931
1932         DecoderProcessSout( p_dec, p_block );
1933     }
1934     else
1935 #endif
1936     {
1937         bool b_flush = false;
1938
1939         if( p_block )
1940         {
1941             const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1942             DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1943
1944             b_flush = !b_flushing && b_flush_request;
1945
1946             p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1947         }
1948
1949         if( p_dec->fmt_in.i_cat == AUDIO_ES )
1950         {
1951             DecoderProcessAudio( p_dec, p_block, b_flush );
1952         }
1953         else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1954         {
1955             DecoderProcessVideo( p_dec, p_block, b_flush );
1956         }
1957         else if( p_dec->fmt_in.i_cat == SPU_ES )
1958         {
1959             DecoderProcessSpu( p_dec, p_block, b_flush );
1960         }
1961         else
1962         {
1963             msg_Err( p_dec, "unknown ES format" );
1964             p_dec->b_error = true;
1965         }
1966     }
1967
1968     /* */
1969     if( b_flush_request )
1970         DecoderProcessOnFlush( p_dec );
1971 }
1972
1973 static void DecoderError( decoder_t *p_dec, block_t *p_block )
1974 {
1975     const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1976
1977     /* */
1978     if( p_block )
1979         block_Release( p_block );
1980
1981     if( b_flush_request )
1982         DecoderProcessOnFlush( p_dec );
1983 }
1984
1985
1986 /**
1987  * Destroys a decoder object
1988  *
1989  * \param p_dec the decoder object
1990  * \return nothing
1991  */
1992 static void DeleteDecoder( decoder_t * p_dec )
1993 {
1994     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1995
1996     msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
1997              (char*)&p_dec->fmt_in.i_codec,
1998              (unsigned)block_FifoCount( p_owner->p_fifo ) );
1999
2000     /* Free all packets still in the decoder fifo. */
2001     block_FifoEmpty( p_owner->p_fifo );
2002     block_FifoRelease( p_owner->p_fifo );
2003
2004     /* */
2005     vlc_mutex_lock( &p_owner->lock );
2006     DecoderFlushBuffering( p_dec );
2007     vlc_mutex_unlock( &p_owner->lock );
2008
2009     /* Cleanup */
2010     if( p_owner->p_aout_input )
2011         aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
2012     if( p_owner->p_aout )
2013     {
2014         input_ressource_RequestAout( p_owner->p_input->p->p_ressource,
2015                                      p_owner->p_aout );
2016         input_SendEventAout( p_owner->p_input );
2017         p_owner->p_aout = NULL;
2018     }
2019     if( p_owner->p_vout )
2020     {
2021         /* Hack to make sure all the the pictures are freed by the decoder */
2022         vout_FixLeaks( p_owner->p_vout, true );
2023
2024         /* */
2025         input_ressource_RequestVout( p_owner->p_input->p->p_ressource, p_owner->p_vout, NULL );
2026         input_SendEventVout( p_owner->p_input );
2027     }
2028
2029 #ifdef ENABLE_SOUT
2030     if( p_owner->p_sout_input )
2031     {
2032         sout_InputDelete( p_owner->p_sout_input );
2033         es_format_Clean( &p_owner->sout );
2034     }
2035 #endif
2036
2037     if( p_dec->fmt_in.i_cat == SPU_ES )
2038     {
2039         vout_thread_t *p_vout;
2040
2041         p_vout = input_ressource_HoldVout( p_owner->p_input->p->p_ressource );
2042         if( p_vout )
2043         {
2044             if( p_owner->p_spu_vout == p_vout )
2045                 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR, p_owner->i_spu_channel );
2046             vlc_object_release( p_vout );
2047         }
2048     }
2049
2050     es_format_Clean( &p_dec->fmt_in );
2051     es_format_Clean( &p_dec->fmt_out );
2052     if( p_dec->p_description )
2053         vlc_meta_Delete( p_dec->p_description );
2054     es_format_Clean( &p_owner->fmt_description );
2055     if( p_owner->p_description )
2056         vlc_meta_Delete( p_owner->p_description );
2057
2058     if( p_owner->p_packetizer )
2059     {
2060         module_unneed( p_owner->p_packetizer,
2061                        p_owner->p_packetizer->p_module );
2062         es_format_Clean( &p_owner->p_packetizer->fmt_in );
2063         es_format_Clean( &p_owner->p_packetizer->fmt_out );
2064         if( p_owner->p_packetizer->p_description )
2065             vlc_meta_Delete( p_owner->p_packetizer->p_description );
2066         vlc_object_detach( p_owner->p_packetizer );
2067         vlc_object_release( p_owner->p_packetizer );
2068     }
2069
2070     vlc_cond_destroy( &p_owner->wait );
2071     vlc_mutex_destroy( &p_owner->lock );
2072
2073     vlc_object_detach( p_dec );
2074
2075     free( p_owner );
2076 }
2077
2078 /*****************************************************************************
2079  * Buffers allocation callbacks for the decoders
2080  *****************************************************************************/
2081 static void DecoderUpdateFormatLocked( decoder_t *p_dec )
2082 {
2083     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2084
2085     vlc_assert_locked( &p_owner->lock );
2086
2087     p_owner->b_fmt_description = true;
2088
2089     /* Copy es_format */
2090     es_format_Clean( &p_owner->fmt_description );
2091     es_format_Copy( &p_owner->fmt_description, &p_dec->fmt_out );
2092
2093     /* Move p_description */
2094     if( p_owner->p_description && p_dec->p_description )
2095         vlc_meta_Delete( p_owner->p_description );
2096     p_owner->p_description = p_dec->p_description;
2097     p_dec->p_description = NULL;
2098 }
2099 static vout_thread_t *aout_request_vout( void *p_private,
2100                                          vout_thread_t *p_vout, video_format_t *p_fmt )
2101 {
2102     decoder_t *p_dec = p_private;
2103
2104     p_vout = input_ressource_RequestVout( p_dec->p_owner->p_input->p->p_ressource, p_vout, p_fmt );
2105     input_SendEventVout( p_dec->p_owner->p_input );
2106
2107     return p_vout;
2108 }
2109
2110 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
2111 {
2112     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2113     aout_buffer_t *p_buffer;
2114
2115     if( p_owner->p_aout_input != NULL &&
2116         ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
2117           p_dec->fmt_out.audio.i_original_channels !=
2118               p_owner->audio.i_original_channels ||
2119           p_dec->fmt_out.audio.i_bytes_per_frame !=
2120               p_owner->audio.i_bytes_per_frame ) )
2121     {
2122         aout_input_t *p_aout_input = p_owner->p_aout_input;
2123
2124         /* Parameters changed, restart the aout */
2125         vlc_mutex_lock( &p_owner->lock );
2126
2127         DecoderFlushBuffering( p_dec );
2128
2129         p_owner->p_aout_input = NULL;
2130         aout_DecDelete( p_owner->p_aout, p_aout_input );
2131
2132         vlc_mutex_unlock( &p_owner->lock );
2133     }
2134
2135     if( p_owner->p_aout_input == NULL )
2136     {
2137         const int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
2138         audio_sample_format_t format;
2139         aout_input_t *p_aout_input;
2140         aout_instance_t *p_aout;
2141         aout_request_vout_t request_vout;
2142
2143         p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
2144         p_owner->audio = p_dec->fmt_out.audio;
2145
2146         memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
2147         if( i_force_dolby &&
2148             (format.i_original_channels&AOUT_CHAN_PHYSMASK) ==
2149                 (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
2150         {
2151             if( i_force_dolby == 1 )
2152             {
2153                 format.i_original_channels = format.i_original_channels |
2154                                              AOUT_CHAN_DOLBYSTEREO;
2155             }
2156             else /* i_force_dolby == 2 */
2157             {
2158                 format.i_original_channels = format.i_original_channels &
2159                                              ~AOUT_CHAN_DOLBYSTEREO;
2160             }
2161         }
2162
2163         request_vout.pf_request_vout = aout_request_vout;
2164         request_vout.p_private = p_dec;
2165
2166         p_aout = p_owner->p_aout;
2167         if( !p_aout )
2168             p_aout = input_ressource_RequestAout( p_owner->p_input->p->p_ressource, NULL );
2169         p_aout_input = aout_DecNew( p_dec, &p_aout,
2170                                     &format, &p_dec->fmt_out.audio_replay_gain, &request_vout );
2171
2172         vlc_mutex_lock( &p_owner->lock );
2173
2174         p_owner->p_aout = p_aout;
2175         p_owner->p_aout_input = p_aout_input;
2176         DecoderUpdateFormatLocked( p_dec );
2177
2178         vlc_mutex_unlock( &p_owner->lock );
2179
2180         input_SendEventAout( p_owner->p_input );
2181
2182         if( p_owner->p_aout_input == NULL )
2183         {
2184             msg_Err( p_dec, "failed to create audio output" );
2185             p_dec->b_error = true;
2186             return NULL;
2187         }
2188         p_dec->fmt_out.audio.i_bytes_per_frame =
2189             p_owner->audio.i_bytes_per_frame;
2190     }
2191
2192     p_buffer = aout_DecNewBuffer( p_owner->p_aout_input, i_samples );
2193
2194     return p_buffer;
2195 }
2196
2197 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
2198 {
2199     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2200
2201     aout_DecDeleteBuffer( p_owner->p_aout,
2202                           p_owner->p_aout_input, p_buffer );
2203 }
2204
2205
2206 int vout_CountPictureAvailable( vout_thread_t *p_vout );
2207
2208 static picture_t *vout_new_buffer( decoder_t *p_dec )
2209 {
2210     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2211
2212     if( p_owner->p_vout == NULL ||
2213         p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
2214         p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
2215         p_dec->fmt_out.video.i_chroma != p_owner->video.i_chroma ||
2216         p_dec->fmt_out.video.i_aspect != p_owner->video.i_aspect )
2217     {
2218         vout_thread_t *p_vout;
2219
2220         if( !p_dec->fmt_out.video.i_width ||
2221             !p_dec->fmt_out.video.i_height )
2222         {
2223             /* Can't create a new vout without display size */
2224             return NULL;
2225         }
2226
2227         if( !p_dec->fmt_out.video.i_visible_width ||
2228             !p_dec->fmt_out.video.i_visible_height )
2229         {
2230             if( p_dec->fmt_in.video.i_visible_width &&
2231                 p_dec->fmt_in.video.i_visible_height )
2232             {
2233                 p_dec->fmt_out.video.i_visible_width =
2234                     p_dec->fmt_in.video.i_visible_width;
2235                 p_dec->fmt_out.video.i_visible_height =
2236                     p_dec->fmt_in.video.i_visible_height;
2237             }
2238             else
2239             {
2240                 p_dec->fmt_out.video.i_visible_width =
2241                     p_dec->fmt_out.video.i_width;
2242                 p_dec->fmt_out.video.i_visible_height =
2243                     p_dec->fmt_out.video.i_height;
2244             }
2245         }
2246
2247         if( p_dec->fmt_out.video.i_visible_height == 1088 &&
2248             var_CreateGetBool( p_dec, "hdtv-fix" ) )
2249         {
2250             p_dec->fmt_out.video.i_visible_height = 1080;
2251             p_dec->fmt_out.video.i_sar_num *= 135;
2252             p_dec->fmt_out.video.i_sar_den *= 136;
2253             msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
2254         }
2255
2256         if( !p_dec->fmt_out.video.i_sar_num ||
2257             !p_dec->fmt_out.video.i_sar_den )
2258         {
2259             p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
2260               p_dec->fmt_out.video.i_visible_height;
2261
2262             p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
2263               p_dec->fmt_out.video.i_visible_width;
2264         }
2265
2266         vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
2267                      &p_dec->fmt_out.video.i_sar_den,
2268                      p_dec->fmt_out.video.i_sar_num,
2269                      p_dec->fmt_out.video.i_sar_den, 50000 );
2270
2271         p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
2272         p_owner->video = p_dec->fmt_out.video;
2273
2274         vlc_mutex_lock( &p_owner->lock );
2275
2276         DecoderFlushBuffering( p_dec );
2277
2278         p_vout = p_owner->p_vout;
2279         p_owner->p_vout = NULL;
2280         vlc_mutex_unlock( &p_owner->lock );
2281
2282         p_vout = input_ressource_RequestVout( p_owner->p_input->p->p_ressource,
2283                                               p_vout, &p_dec->fmt_out.video );
2284
2285         vlc_mutex_lock( &p_owner->lock );
2286         p_owner->p_vout = p_vout;
2287
2288         DecoderUpdateFormatLocked( p_dec );
2289
2290         vlc_mutex_unlock( &p_owner->lock );
2291
2292         input_SendEventVout( p_owner->p_input );
2293         if( p_vout == NULL )
2294         {
2295             msg_Err( p_dec, "failed to create video output" );
2296             p_dec->b_error = true;
2297             return NULL;
2298         }
2299
2300         if( p_owner->video.i_rmask )
2301             p_owner->p_vout->render.i_rmask = p_owner->video.i_rmask;
2302         if( p_owner->video.i_gmask )
2303             p_owner->p_vout->render.i_gmask = p_owner->video.i_gmask;
2304         if( p_owner->video.i_bmask )
2305             p_owner->p_vout->render.i_bmask = p_owner->video.i_bmask;
2306     }
2307
2308     /* Get a new picture
2309      */
2310     for( ;; )
2311     {
2312         picture_t *p_picture;
2313
2314         if( p_dec->b_die || p_dec->b_error )
2315             return NULL;
2316
2317         /* The video filter chain required that there is always 1 free buffer
2318          * that it will use as temporary one. It will release the temporary
2319          * buffer once its work is done, so this check is safe even if we don't
2320          * lock around both count() and create().
2321          */
2322         if( vout_CountPictureAvailable( p_owner->p_vout ) >= 2 )
2323         {
2324             p_picture = vout_CreatePicture( p_owner->p_vout, 0, 0, 0 );
2325             if( p_picture )
2326                 return p_picture;
2327         }
2328
2329         if( DecoderIsFlushing( p_dec ) )
2330             return NULL;
2331
2332         /* */
2333         DecoderSignalBuffering( p_dec, true );
2334
2335         /* Check the decoder doesn't leak pictures */
2336         vout_FixLeaks( p_owner->p_vout, false );
2337
2338         /* FIXME add a vout_WaitPictureAvailable (timedwait) */
2339         msleep( VOUT_OUTMEM_SLEEP );
2340     }
2341 }
2342
2343 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
2344 {
2345     vout_DropPicture( p_dec->p_owner->p_vout, p_pic );
2346 }
2347
2348 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
2349 {
2350     vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
2351 }
2352
2353 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
2354 {
2355     vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
2356 }
2357
2358 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2359 {
2360     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2361     vout_thread_t *p_vout = NULL;
2362     subpicture_t *p_subpic;
2363     int i_attempts = 30;
2364
2365     while( i_attempts-- )
2366     {
2367         if( p_dec->b_die || p_dec->b_error )
2368             break;
2369
2370         p_vout = input_ressource_HoldVout( p_owner->p_input->p->p_ressource );
2371         if( p_vout )
2372             break;
2373
2374         msleep( DECODER_SPU_VOUT_WAIT_DURATION );
2375     }
2376
2377     if( !p_vout )
2378     {
2379         msg_Warn( p_dec, "no vout found, dropping subpicture" );
2380         return NULL;
2381     }
2382
2383     if( p_owner->p_spu_vout != p_vout )
2384     {
2385         vlc_mutex_lock( &p_owner->lock );
2386
2387         DecoderFlushBuffering( p_dec );
2388
2389         vlc_mutex_unlock( &p_owner->lock );
2390
2391         spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
2392                      &p_owner->i_spu_channel );
2393         p_owner->i_spu_order = 0;
2394         p_owner->p_spu_vout = p_vout;
2395     }
2396
2397     p_subpic = subpicture_New();
2398     if( p_subpic )
2399     {
2400         p_subpic->i_channel = p_owner->i_spu_channel;
2401         p_subpic->i_order = p_owner->i_spu_order++;
2402         p_subpic->b_subtitle = true;
2403     }
2404
2405     vlc_object_release( p_vout );
2406
2407     return p_subpic;
2408 }
2409
2410 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2411 {
2412     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2413     vout_thread_t *p_vout = NULL;
2414
2415     p_vout = input_ressource_HoldVout( p_owner->p_input->p->p_ressource );
2416     if( !p_vout || p_owner->p_spu_vout != p_vout )
2417     {
2418         if( p_vout )
2419             vlc_object_release( p_vout );
2420         msg_Warn( p_dec, "no vout found, leaking subpicture" );
2421         return;
2422     }
2423
2424     subpicture_Delete( p_subpic );
2425
2426     vlc_object_release( p_vout );
2427 }
2428