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