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