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