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