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