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