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