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