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