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