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