]> git.sesse.net Git - vlc/blob - src/input/decoder.c
decoder: fix debug message
[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         msg_Err(p_dec, "Could not get display date for timestamp %"PRId64"", i_ts);
671         return VLC_TS_INVALID;
672     }
673
674     return i_ts;
675 }
676 static int DecoderGetDisplayRate( decoder_t *p_dec )
677 {
678     decoder_owner_sys_t *p_owner = p_dec->p_owner;
679
680     if( !p_owner->p_clock )
681         return INPUT_RATE_DEFAULT;
682     return input_clock_GetRate( p_owner->p_clock );
683 }
684
685 /* */
686 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
687 {
688     if (codec != VLC_FOURCC('u','n','d','f')) {
689         const char *desc = vlc_fourcc_GetDescription(p_dec->fmt_in.i_cat, codec);
690         if (!desc || !*desc)
691             desc = N_("No description for this codec");
692         msg_Err( p_dec, "Codec `%4.4s' (%s) is not supported.", (char*)&codec, desc );
693         dialog_Fatal( p_dec, _("Codec not supported"),
694                 _("VLC could not decode the format \"%4.4s\" (%s)"),
695                 (char*)&codec, desc );
696     } else {
697         msg_Err( p_dec, "could not identify codec" );
698         dialog_Fatal( p_dec, _("Unidentified codec"),
699             _("VLC could not identify the audio or video codec" ) );
700     }
701 }
702
703
704 /**
705  * Create a decoder object
706  *
707  * \param p_input the input thread
708  * \param p_es the es descriptor
709  * \param b_packetizer instead of a decoder
710  * \return the decoder object
711  */
712 static decoder_t * CreateDecoder( vlc_object_t *p_parent,
713                                   input_thread_t *p_input,
714                                   es_format_t *fmt, bool b_packetizer,
715                                   input_resource_t *p_resource,
716                                   sout_instance_t *p_sout )
717 {
718     decoder_t *p_dec;
719     decoder_owner_sys_t *p_owner;
720     es_format_t null_es_format;
721
722     p_dec = vlc_custom_create( p_parent, sizeof( *p_dec ), "decoder" );
723     if( p_dec == NULL )
724         return NULL;
725
726     p_dec->pf_decode_audio = NULL;
727     p_dec->pf_decode_video = NULL;
728     p_dec->pf_decode_sub = NULL;
729     p_dec->pf_get_cc = NULL;
730     p_dec->pf_packetize = NULL;
731
732     /* Initialize the decoder */
733     p_dec->p_module = NULL;
734
735     memset( &null_es_format, 0, sizeof(es_format_t) );
736     es_format_Copy( &p_dec->fmt_in, fmt );
737     es_format_Copy( &p_dec->fmt_out, &null_es_format );
738
739     p_dec->p_description = NULL;
740
741     /* Allocate our private structure for the decoder */
742     p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
743     if( unlikely(p_owner == NULL) )
744     {
745         vlc_object_release( p_dec );
746         return NULL;
747     }
748     p_owner->i_preroll_end = VLC_TS_INVALID;
749     p_owner->i_last_rate = INPUT_RATE_DEFAULT;
750     p_owner->p_input = p_input;
751     p_owner->p_resource = p_resource;
752     p_owner->p_aout = NULL;
753     p_owner->p_vout = NULL;
754     p_owner->p_spu_vout = NULL;
755     p_owner->i_spu_channel = 0;
756     p_owner->i_spu_order = 0;
757     p_owner->p_sout = p_sout;
758     p_owner->p_sout_input = NULL;
759     p_owner->p_packetizer = NULL;
760     p_owner->b_packetizer = b_packetizer;
761
762     /* decoder fifo */
763     p_owner->p_fifo = block_FifoNew();
764     if( unlikely(p_owner->p_fifo == NULL) )
765     {
766         free( p_owner );
767         vlc_object_release( p_dec );
768         return NULL;
769     }
770
771     /* Set buffers allocation callbacks for the decoders */
772     p_dec->pf_aout_format_update = aout_update_format;
773     p_dec->pf_vout_buffer_new = vout_new_buffer;
774     p_dec->pf_vout_buffer_del = vout_del_buffer;
775     p_dec->pf_picture_link    = vout_link_picture;
776     p_dec->pf_picture_unlink  = vout_unlink_picture;
777     p_dec->pf_spu_buffer_new  = spu_new_buffer;
778     p_dec->pf_spu_buffer_del  = spu_del_buffer;
779     /* */
780     p_dec->pf_get_attachments  = DecoderGetInputAttachments;
781     p_dec->pf_get_display_date = DecoderGetDisplayDate;
782     p_dec->pf_get_display_rate = DecoderGetDisplayRate;
783
784     /* Find a suitable decoder/packetizer module */
785     if( !b_packetizer )
786         p_dec->p_module = module_need( p_dec, "decoder", "$codec", false );
787     else
788         p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", false );
789
790     /* Check if decoder requires already packetized data */
791     if( !b_packetizer &&
792         p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
793     {
794         p_owner->p_packetizer =
795             vlc_custom_create( p_parent, sizeof( decoder_t ), "packetizer" );
796         if( p_owner->p_packetizer )
797         {
798             es_format_Copy( &p_owner->p_packetizer->fmt_in,
799                             &p_dec->fmt_in );
800
801             es_format_Copy( &p_owner->p_packetizer->fmt_out,
802                             &null_es_format );
803
804             p_owner->p_packetizer->p_module =
805                 module_need( p_owner->p_packetizer,
806                              "packetizer", "$packetizer", false );
807
808             if( !p_owner->p_packetizer->p_module )
809             {
810                 es_format_Clean( &p_owner->p_packetizer->fmt_in );
811                 vlc_object_release( p_owner->p_packetizer );
812             }
813         }
814     }
815
816     /* Copy ourself the input replay gain */
817     if( fmt->i_cat == AUDIO_ES )
818     {
819         for( unsigned i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
820         {
821             if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
822             {
823                 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
824                 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
825             }
826             if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
827             {
828                 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
829                 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
830             }
831         }
832     }
833
834     /* */
835     vlc_mutex_init( &p_owner->lock );
836     vlc_cond_init( &p_owner->wait_request );
837     vlc_cond_init( &p_owner->wait_acknowledge );
838
839     p_owner->b_fmt_description = false;
840     es_format_Init( &p_owner->fmt_description, UNKNOWN_ES, 0 );
841     p_owner->p_description = NULL;
842
843     p_owner->b_exit = false;
844
845     p_owner->b_paused = false;
846     p_owner->pause.i_date = VLC_TS_INVALID;
847     p_owner->pause.i_ignore = 0;
848
849     p_owner->b_waiting = false;
850     p_owner->b_first = true;
851     p_owner->b_has_data = false;
852
853     p_owner->b_flushing = false;
854
855     /* */
856     p_owner->cc.b_supported = false;
857     if( !b_packetizer )
858     {
859         if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
860             p_owner->cc.b_supported = true;
861         if( p_dec->pf_get_cc )
862             p_owner->cc.b_supported = true;
863     }
864
865     for( unsigned i = 0; i < 4; i++ )
866     {
867         p_owner->cc.pb_present[i] = false;
868         p_owner->cc.pp_decoder[i] = NULL;
869     }
870     p_owner->i_ts_delay = 0;
871     return p_dec;
872 }
873
874 /**
875  * The decoding main loop
876  *
877  * \param p_dec the decoder
878  */
879 static void *DecoderThread( void *p_data )
880 {
881     decoder_t *p_dec = (decoder_t *)p_data;
882     decoder_owner_sys_t *p_owner = p_dec->p_owner;
883
884     /* The decoder's main loop */
885     for( ;; )
886     {
887         block_t *p_block = block_FifoGet( p_owner->p_fifo );
888
889         /* Make sure there is no cancellation point other than this one^^.
890          * If you need one, be sure to push cleanup of p_block. */
891         bool end_wait = !p_block || p_block->i_flags & BLOCK_FLAG_CORE_EOS;
892         DecoderSignalWait( p_dec, end_wait );
893         if (end_wait)
894             input_DecoderStopWait( p_dec );
895
896         if( p_block )
897         {
898             int canc = vlc_savecancel();
899
900             if( p_block->i_flags & BLOCK_FLAG_CORE_EOS )
901             {
902                 /* calling DecoderProcess() with NULL block will make
903                  * decoders/packetizers flush their buffers */
904                 block_Release( p_block );
905                 p_block = NULL;
906             }
907
908             DecoderProcess( p_dec, p_block );
909
910             vlc_restorecancel( canc );
911         }
912     }
913     return NULL;
914 }
915
916 static block_t *DecoderBlockFlushNew()
917 {
918     block_t *p_null = block_Alloc( 128 );
919     if( !p_null )
920         return NULL;
921
922     p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY |
923                        BLOCK_FLAG_CORRUPTED |
924                        BLOCK_FLAG_CORE_FLUSH;
925     memset( p_null->p_buffer, 0, p_null->i_buffer );
926
927     return p_null;
928 }
929
930 static void DecoderFlush( decoder_t *p_dec )
931 {
932     decoder_owner_sys_t *p_owner = p_dec->p_owner;
933
934     vlc_assert_locked( &p_owner->lock );
935
936     /* Empty the fifo */
937     block_FifoEmpty( p_owner->p_fifo );
938
939     /* Monitor for flush end */
940     p_owner->b_flushing = true;
941     vlc_cond_signal( &p_owner->wait_request );
942
943     /* Send a special block */
944     block_t *p_null = DecoderBlockFlushNew();
945     if( !p_null )
946         return;
947     input_DecoderDecode( p_dec, p_null, false );
948
949     /* */
950     while( p_owner->b_flushing )
951         vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
952 }
953
954 static void DecoderSignalWait( decoder_t *p_dec, bool b_has_data )
955 {
956     decoder_owner_sys_t *p_owner = p_dec->p_owner;
957
958     vlc_mutex_lock( &p_owner->lock );
959
960     if( p_owner->b_waiting )
961     {
962         if( b_has_data )
963             p_owner->b_has_data = true;
964         vlc_cond_signal( &p_owner->wait_acknowledge );
965     }
966
967     vlc_mutex_unlock( &p_owner->lock );
968 }
969
970 static bool DecoderIsFlushing( decoder_t *p_dec )
971 {
972     decoder_owner_sys_t *p_owner = p_dec->p_owner;
973     bool b_flushing;
974
975     vlc_mutex_lock( &p_owner->lock );
976
977     b_flushing = p_owner->b_flushing;
978
979     vlc_mutex_unlock( &p_owner->lock );
980
981     return b_flushing;
982 }
983
984 static bool DecoderWaitUnblock( decoder_t *p_dec )
985 {
986     decoder_owner_sys_t *p_owner = p_dec->p_owner;
987
988     vlc_assert_locked( &p_owner->lock );
989
990     for( ;; )
991     {
992         if( p_owner->b_flushing )
993             break;
994         if( p_owner->b_paused )
995         {
996             if( p_owner->b_waiting && !p_owner->b_has_data )
997                 break;
998             if( p_owner->pause.i_ignore > 0 )
999             {
1000                 p_owner->pause.i_ignore--;
1001                 break;
1002             }
1003         }
1004         else
1005         {
1006             if( !p_owner->b_waiting || !p_owner->b_has_data )
1007                 break;
1008         }
1009         vlc_cond_wait( &p_owner->wait_request, &p_owner->lock );
1010     }
1011
1012     return p_owner->b_flushing;
1013 }
1014
1015 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
1016 {
1017     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1018
1019     vlc_assert_locked( &p_owner->lock );
1020
1021     /* XXX only audio and video output have to be paused.
1022      * - for sout it is useless
1023      * - for subs, it is done by the vout
1024      */
1025     if( p_dec->fmt_out.i_cat == AUDIO_ES )
1026     {
1027         if( p_owner->p_aout )
1028             aout_DecChangePause( p_owner->p_aout, b_paused, i_date );
1029     }
1030     else if( p_dec->fmt_out.i_cat == VIDEO_ES )
1031     {
1032         if( p_owner->p_vout )
1033             vout_ChangePause( p_owner->p_vout, b_paused, i_date );
1034     }
1035 }
1036 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
1037 {
1038     if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
1039         *pi_preroll = INT64_MAX;
1040     else if( p->i_dts > VLC_TS_INVALID )
1041         *pi_preroll = __MIN( *pi_preroll, p->i_dts );
1042     else if( p->i_pts > VLC_TS_INVALID )
1043         *pi_preroll = __MIN( *pi_preroll, p->i_pts );
1044 }
1045
1046 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
1047                           mtime_t *pi_duration, int *pi_rate, mtime_t i_ts_bound )
1048 {
1049     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1050     input_clock_t   *p_clock = p_owner->p_clock;
1051
1052     vlc_assert_locked( &p_owner->lock );
1053
1054     const mtime_t i_es_delay = p_owner->i_ts_delay;
1055
1056     if( !p_clock )
1057         return;
1058
1059     const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
1060     int i_rate;
1061
1062     if( *pi_ts0 > VLC_TS_INVALID )
1063     {
1064         *pi_ts0 += i_es_delay;
1065         if( pi_ts1 && *pi_ts1 > VLC_TS_INVALID )
1066             *pi_ts1 += i_es_delay;
1067         if( input_clock_ConvertTS( p_clock, &i_rate, pi_ts0, pi_ts1, i_ts_bound ) ) {
1068             msg_Err(p_dec, "Could not convert timestamps %"PRId64", %"PRId64"",
1069                     *pi_ts0, *pi_ts1);
1070             *pi_ts0 = VLC_TS_INVALID;
1071         }
1072     }
1073     else
1074     {
1075         i_rate = input_clock_GetRate( p_clock );
1076     }
1077
1078     /* Do not create ephemere data because of rounding errors */
1079     if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
1080         *pi_ts1 += 1;
1081
1082     if( pi_duration )
1083         *pi_duration = ( *pi_duration * i_rate + INPUT_RATE_DEFAULT-1 )
1084             / INPUT_RATE_DEFAULT;
1085
1086     if( pi_rate )
1087         *pi_rate = i_rate;
1088 }
1089
1090 static bool DecoderIsExitRequested( decoder_t *p_dec )
1091 {
1092     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1093
1094     vlc_mutex_lock( &p_owner->lock );
1095     bool b_exit = p_owner->b_exit;
1096     vlc_mutex_unlock( &p_owner->lock );
1097
1098     return b_exit;
1099 }
1100
1101 /**
1102  * If *pb_reject, it does nothing, otherwise it waits for the given
1103  * deadline or a flush request (in which case it set *pi_reject to true.
1104  */
1105 static void DecoderWaitDate( decoder_t *p_dec,
1106                              bool *pb_reject, mtime_t i_deadline )
1107 {
1108     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1109
1110     vlc_assert_locked( &p_owner->lock );
1111
1112     if( *pb_reject || i_deadline < 0 )
1113         return;
1114
1115     do
1116     {
1117         if( p_owner->b_flushing || p_owner->b_exit )
1118         {
1119             *pb_reject = true;
1120             break;
1121         }
1122     }
1123     while( vlc_cond_timedwait( &p_owner->wait_request, &p_owner->lock,
1124                                i_deadline ) == 0 );
1125 }
1126
1127 static void DecoderPlayAudio( decoder_t *p_dec, block_t *p_audio,
1128                               int *pi_played_sum, int *pi_lost_sum )
1129 {
1130     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1131     audio_output_t *p_aout = p_owner->p_aout;
1132
1133     /* */
1134     if( p_audio && p_audio->i_pts <= VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify audio_output/*
1135     {
1136         msg_Warn( p_dec, "non-dated audio buffer received" );
1137         *pi_lost_sum += 1;
1138         block_Release( p_audio );
1139         return;
1140     }
1141
1142     /* */
1143     vlc_mutex_lock( &p_owner->lock );
1144
1145     if( p_audio && p_owner->b_waiting )
1146     {
1147         p_owner->b_has_data = true;
1148         vlc_cond_signal( &p_owner->wait_acknowledge );
1149     }
1150
1151     for( ;; )
1152     {
1153         bool b_paused;
1154
1155         bool b_reject = DecoderWaitUnblock( p_dec );
1156
1157         b_paused = p_owner->b_paused;
1158
1159         if (!p_audio)
1160             break;
1161
1162         /* */
1163         int i_rate = INPUT_RATE_DEFAULT;
1164
1165         DecoderFixTs( p_dec, &p_audio->i_pts, NULL, &p_audio->i_length,
1166                       &i_rate, AOUT_MAX_ADVANCE_TIME );
1167
1168         if( p_audio->i_pts <= VLC_TS_INVALID
1169          || i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE
1170          || i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
1171             b_reject = true;
1172
1173         DecoderWaitDate( p_dec, &b_reject,
1174                          p_audio->i_pts - AOUT_MAX_PREPARE_TIME );
1175
1176         if( unlikely(p_owner->b_paused != b_paused) )
1177             continue; /* race with input thread? retry... */
1178         if( p_aout == NULL )
1179             b_reject = true;
1180
1181         if( !b_reject )
1182         {
1183             assert( !p_owner->b_paused );
1184             if( !aout_DecPlay( p_aout, p_audio, i_rate ) )
1185                 *pi_played_sum += 1;
1186             *pi_lost_sum += aout_DecGetResetLost( p_aout );
1187         }
1188         else
1189         {
1190             msg_Dbg( p_dec, "discarded audio buffer" );
1191             *pi_lost_sum += 1;
1192             block_Release( p_audio );
1193         }
1194
1195         break;
1196     }
1197     vlc_mutex_unlock( &p_owner->lock );
1198 }
1199
1200 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1201 {
1202     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1203     block_t *p_aout_buf;
1204     int i_decoded = 0;
1205     int i_lost = 0;
1206     int i_played = 0;
1207
1208     if (!p_block) {
1209         /* Play a NULL block to output buffered frames */
1210         DecoderPlayAudio( p_dec, NULL, &i_played, &i_lost );
1211     }
1212     else while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1213     {
1214         if( DecoderIsExitRequested( p_dec ) )
1215         {
1216             /* It prevent freezing VLC in case of broken decoder */
1217             block_Release( p_aout_buf );
1218             if( p_block )
1219                 block_Release( p_block );
1220             break;
1221         }
1222         i_decoded++;
1223
1224         if( p_owner->i_preroll_end > VLC_TS_INVALID &&
1225             p_aout_buf->i_pts < p_owner->i_preroll_end )
1226         {
1227             block_Release( p_aout_buf );
1228             continue;
1229         }
1230
1231         if( p_owner->i_preroll_end > VLC_TS_INVALID )
1232         {
1233             msg_Dbg( p_dec, "End of audio preroll" );
1234             if( p_owner->p_aout )
1235                 aout_DecFlush( p_owner->p_aout );
1236             /* */
1237             p_owner->i_preroll_end = VLC_TS_INVALID;
1238         }
1239
1240         DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1241     }
1242
1243     /* Update ugly stat */
1244     input_thread_t  *p_input = p_owner->p_input;
1245
1246     if( p_input != NULL && (i_decoded > 0 || i_lost > 0 || i_played > 0) )
1247     {
1248         vlc_mutex_lock( &p_input->p->counters.counters_lock);
1249         stats_Update( p_input->p->counters.p_lost_abuffers, i_lost, NULL );
1250         stats_Update( p_input->p->counters.p_played_abuffers, i_played, NULL );
1251         stats_Update( p_input->p->counters.p_decoded_audio, i_decoded, NULL );
1252         vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1253     }
1254 }
1255 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1256 {
1257     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1258     block_t *p_cc;
1259     bool pb_present[4];
1260     bool b_processed = false;
1261     int i;
1262     int i_cc_decoder;
1263
1264     assert( p_dec_cc->pf_get_cc != NULL );
1265
1266     /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1267     if( !p_owner->cc.b_supported )
1268         return;
1269
1270     p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1271     if( !p_cc )
1272         return;
1273
1274     vlc_mutex_lock( &p_owner->lock );
1275     for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1276     {
1277         p_owner->cc.pb_present[i] |= pb_present[i];
1278         if( p_owner->cc.pp_decoder[i] )
1279             i_cc_decoder++;
1280     }
1281
1282     for( i = 0; i < 4; i++ )
1283     {
1284         if( !p_owner->cc.pp_decoder[i] )
1285             continue;
1286
1287         block_FifoPut( p_owner->cc.pp_decoder[i]->p_owner->p_fifo,
1288             (i_cc_decoder > 1) ? block_Duplicate(p_cc) : p_cc);
1289
1290         i_cc_decoder--;
1291         b_processed = true;
1292     }
1293     vlc_mutex_unlock( &p_owner->lock );
1294
1295     if( !b_processed )
1296         block_Release( p_cc );
1297 }
1298
1299 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
1300                               int *pi_played_sum, int *pi_lost_sum )
1301 {
1302     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1303     vout_thread_t  *p_vout = p_owner->p_vout;
1304
1305     if( p_picture->date <= VLC_TS_INVALID )
1306     {
1307         msg_Warn( p_dec, "non-dated video buffer received" );
1308         *pi_lost_sum += 1;
1309         vout_ReleasePicture( p_vout, p_picture );
1310         return;
1311     }
1312
1313     /* */
1314     vlc_mutex_lock( &p_owner->lock );
1315
1316     if( p_owner->b_waiting && !p_owner->b_first )
1317     {
1318         p_owner->b_has_data = true;
1319         vlc_cond_signal( &p_owner->wait_acknowledge );
1320     }
1321     bool b_first_after_wait = p_owner->b_waiting && p_owner->b_has_data;
1322
1323     bool b_reject = DecoderWaitUnblock( p_dec );
1324
1325     if( p_owner->b_waiting )
1326     {
1327         assert( p_owner->b_first );
1328         msg_Dbg( p_dec, "Received first picture" );
1329         p_owner->b_first = false;
1330         p_picture->b_force = true;
1331     }
1332
1333     const bool b_dated = p_picture->date > VLC_TS_INVALID;
1334     int i_rate = INPUT_RATE_DEFAULT;
1335     DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1336                   &i_rate, DECODER_BOGUS_VIDEO_DELAY );
1337
1338     vlc_mutex_unlock( &p_owner->lock );
1339
1340     /* */
1341     if( !p_picture->b_force && p_picture->date <= VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify video_output/*
1342         b_reject = true;
1343
1344     if( !b_reject )
1345     {
1346         if( i_rate != p_owner->i_last_rate || b_first_after_wait )
1347         {
1348             /* Be sure to not display old picture after our own */
1349             vout_Flush( p_vout, p_picture->date );
1350             p_owner->i_last_rate = i_rate;
1351         }
1352         vout_PutPicture( p_vout, p_picture );
1353     }
1354     else
1355     {
1356         if( b_dated )
1357             msg_Warn( p_dec, "early picture skipped" );
1358         else
1359             msg_Warn( p_dec, "non-dated video buffer received" );
1360
1361         *pi_lost_sum += 1;
1362         vout_ReleasePicture( p_vout, p_picture );
1363     }
1364     int i_tmp_display;
1365     int i_tmp_lost;
1366     vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1367
1368     *pi_played_sum += i_tmp_display;
1369     *pi_lost_sum += i_tmp_lost;
1370 }
1371
1372 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1373 {
1374     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1375     picture_t      *p_pic;
1376     int i_lost = 0;
1377     int i_decoded = 0;
1378     int i_displayed = 0;
1379
1380     while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1381     {
1382         vout_thread_t  *p_vout = p_owner->p_vout;
1383         if( DecoderIsExitRequested( p_dec ) )
1384         {
1385             /* It prevent freezing VLC in case of broken decoder */
1386             vout_ReleasePicture( p_vout, p_pic );
1387             if( p_block )
1388                 block_Release( p_block );
1389             break;
1390         }
1391
1392         i_decoded++;
1393
1394         if( p_owner->i_preroll_end > VLC_TS_INVALID && p_pic->date < p_owner->i_preroll_end )
1395         {
1396             vout_ReleasePicture( p_vout, p_pic );
1397             continue;
1398         }
1399
1400         if( p_owner->i_preroll_end > VLC_TS_INVALID )
1401         {
1402             msg_Dbg( p_dec, "End of video preroll" );
1403             if( p_vout )
1404                 vout_Flush( p_vout, VLC_TS_INVALID+1 );
1405             /* */
1406             p_owner->i_preroll_end = VLC_TS_INVALID;
1407         }
1408
1409         if( p_dec->pf_get_cc &&
1410             ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1411             DecoderGetCc( p_dec, p_dec );
1412
1413         DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
1414     }
1415
1416     /* Update ugly stat */
1417     input_thread_t *p_input = p_owner->p_input;
1418
1419     if( p_input != NULL && (i_decoded > 0 || i_lost > 0 || i_displayed > 0) )
1420     {
1421         vlc_mutex_lock( &p_input->p->counters.counters_lock );
1422         stats_Update( p_input->p->counters.p_decoded_video, i_decoded, NULL );
1423         stats_Update( p_input->p->counters.p_lost_pictures, i_lost , NULL);
1424         stats_Update( p_input->p->counters.p_displayed_pictures,
1425                       i_displayed, NULL);
1426         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1427     }
1428 }
1429
1430 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic )
1431 {
1432     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1433     vout_thread_t *p_vout = p_owner->p_spu_vout;
1434
1435     /* */
1436     if( p_subpic->i_start <= VLC_TS_INVALID )
1437     {
1438         msg_Warn( p_dec, "non-dated spu buffer received" );
1439         subpicture_Delete( p_subpic );
1440         return;
1441     }
1442
1443     /* */
1444     vlc_mutex_lock( &p_owner->lock );
1445
1446     if( p_owner->b_waiting )
1447     {
1448         p_owner->b_has_data = true;
1449         vlc_cond_signal( &p_owner->wait_acknowledge );
1450     }
1451
1452     bool b_reject = DecoderWaitUnblock( p_dec );
1453
1454     DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1455                   NULL, INT64_MAX );
1456
1457     if( p_subpic->i_start <= VLC_TS_INVALID )
1458         b_reject = true;
1459
1460     DecoderWaitDate( p_dec, &b_reject,
1461                      p_subpic->i_start - SPU_MAX_PREPARE_TIME );
1462     vlc_mutex_unlock( &p_owner->lock );
1463
1464     if( !b_reject )
1465         vout_PutSubpicture( p_vout, p_subpic );
1466     else
1467         subpicture_Delete( p_subpic );
1468 }
1469
1470 #ifdef ENABLE_SOUT
1471 static int DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block )
1472 {
1473     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1474
1475     assert( p_owner->p_clock );
1476     assert( !p_sout_block->p_next );
1477
1478     vlc_mutex_lock( &p_owner->lock );
1479
1480     if( p_owner->b_waiting )
1481     {
1482         p_owner->b_has_data = true;
1483         vlc_cond_signal( &p_owner->wait_acknowledge );
1484     }
1485
1486     bool b_reject = DecoderWaitUnblock( p_dec );
1487
1488     DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
1489                   &p_sout_block->i_length, NULL, INT64_MAX );
1490
1491     vlc_mutex_unlock( &p_owner->lock );
1492
1493     if( !b_reject )
1494     {
1495         /* FIXME --VLC_TS_INVALID inspect stream_output*/
1496         return sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block );
1497     }
1498     else
1499     {
1500         block_Release( p_sout_block );
1501         return VLC_EGENERIC;
1502     }
1503 }
1504 #endif
1505
1506 #ifdef ENABLE_SOUT
1507 /* This function process a block for sout
1508  */
1509 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1510 {
1511     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1512     block_t *p_sout_block;
1513
1514     while( ( p_sout_block =
1515                  p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1516     {
1517         if( !p_owner->p_sout_input )
1518         {
1519             es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1520
1521             p_owner->sout.i_group = p_dec->fmt_in.i_group;
1522             p_owner->sout.i_id = p_dec->fmt_in.i_id;
1523             if( p_dec->fmt_in.psz_language )
1524             {
1525                 free( p_owner->sout.psz_language );
1526                 p_owner->sout.psz_language =
1527                     strdup( p_dec->fmt_in.psz_language );
1528             }
1529
1530             p_owner->p_sout_input =
1531                 sout_InputNew( p_owner->p_sout,
1532                                &p_owner->sout );
1533
1534             if( p_owner->p_sout_input == NULL )
1535             {
1536                 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1537                          (char *)&p_owner->sout.i_codec );
1538                 p_dec->b_error = true;
1539
1540                 block_ChainRelease(p_sout_block);
1541                 break;
1542             }
1543         }
1544
1545         while( p_sout_block )
1546         {
1547             block_t *p_next = p_sout_block->p_next;
1548
1549             p_sout_block->p_next = NULL;
1550
1551             if( DecoderPlaySout( p_dec, p_sout_block ) == VLC_EGENERIC )
1552             {
1553                 msg_Err( p_dec, "cannot continue streaming due to errors" );
1554
1555                 p_dec->b_error = true;
1556
1557                 /* Cleanup */
1558                 block_ChainRelease( p_next );
1559                 return;
1560             }
1561
1562             p_sout_block = p_next;
1563         }
1564     }
1565 }
1566 #endif
1567
1568 /* This function process a video block
1569  */
1570 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1571 {
1572     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1573
1574     if( p_owner->p_packetizer )
1575     {
1576         block_t *p_packetized_block;
1577         decoder_t *p_packetizer = p_owner->p_packetizer;
1578
1579         while( (p_packetized_block =
1580                 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1581         {
1582             if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1583             {
1584                 es_format_Clean( &p_dec->fmt_in );
1585                 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1586             }
1587
1588             /* If the packetizer provides aspect ratio information, pass it
1589              * to the decoder as a hint if the decoder itself can't provide
1590              * it. Copy it regardless of the current value of the decoder input
1591              * format aspect ratio, to properly propagate changes in aspect
1592              * ratio. */
1593             if( p_packetizer->fmt_out.video.i_sar_num > 0 &&
1594                     p_packetizer->fmt_out.video.i_sar_den > 0)
1595             {
1596                 p_dec->fmt_in.video.i_sar_num =
1597                     p_packetizer->fmt_out.video.i_sar_num;
1598                 p_dec->fmt_in.video.i_sar_den=
1599                     p_packetizer->fmt_out.video.i_sar_den;
1600             }
1601
1602             if( p_packetizer->pf_get_cc )
1603                 DecoderGetCc( p_dec, p_packetizer );
1604
1605             while( p_packetized_block )
1606             {
1607                 block_t *p_next = p_packetized_block->p_next;
1608                 p_packetized_block->p_next = NULL;
1609
1610                 DecoderDecodeVideo( p_dec, p_packetized_block );
1611
1612                 p_packetized_block = p_next;
1613             }
1614         }
1615         /* The packetizer does not output a block that tell the decoder to flush
1616          * do it ourself */
1617         if( b_flush )
1618         {
1619             block_t *p_null = DecoderBlockFlushNew();
1620             if( p_null )
1621                 DecoderDecodeVideo( p_dec, p_null );
1622         }
1623     }
1624     else
1625     {
1626         DecoderDecodeVideo( p_dec, p_block );
1627     }
1628
1629     if( b_flush && p_owner->p_vout )
1630         vout_Flush( p_owner->p_vout, VLC_TS_INVALID+1 );
1631 }
1632
1633 /* This function process a audio block
1634  */
1635 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1636 {
1637     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1638
1639     if( p_owner->p_packetizer )
1640     {
1641         block_t *p_packetized_block;
1642         decoder_t *p_packetizer = p_owner->p_packetizer;
1643
1644         while( (p_packetized_block =
1645                 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1646         {
1647             if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1648             {
1649                 es_format_Clean( &p_dec->fmt_in );
1650                 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1651             }
1652
1653             while( p_packetized_block )
1654             {
1655                 block_t *p_next = p_packetized_block->p_next;
1656                 p_packetized_block->p_next = NULL;
1657
1658                 DecoderDecodeAudio( p_dec, p_packetized_block );
1659
1660                 p_packetized_block = p_next;
1661             }
1662         }
1663         /* The packetizer does not output a block that tell the decoder to flush
1664          * do it ourself */
1665         if( b_flush )
1666         {
1667             block_t *p_null = DecoderBlockFlushNew();
1668             if( p_null )
1669                 DecoderDecodeAudio( p_dec, p_null );
1670         }
1671     }
1672     else
1673     {
1674         DecoderDecodeAudio( p_dec, p_block );
1675     }
1676
1677     if( b_flush && p_owner->p_aout )
1678         aout_DecFlush( p_owner->p_aout );
1679 }
1680
1681 /* This function process a subtitle block
1682  */
1683 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1684 {
1685     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1686
1687     input_thread_t *p_input = p_owner->p_input;
1688     vout_thread_t *p_vout;
1689     subpicture_t *p_spu;
1690
1691     while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1692     {
1693         if( p_input != NULL )
1694         {
1695             vlc_mutex_lock( &p_input->p->counters.counters_lock );
1696             stats_Update( p_input->p->counters.p_decoded_sub, 1, NULL );
1697             vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1698         }
1699
1700         p_vout = input_resource_HoldVout( p_owner->p_resource );
1701         if( p_vout && p_owner->p_spu_vout == p_vout )
1702         {
1703             /* Preroll does not work very well with subtitle */
1704             if( p_spu->i_start > VLC_TS_INVALID &&
1705                 p_spu->i_start < p_owner->i_preroll_end &&
1706                 ( p_spu->i_stop <= VLC_TS_INVALID || p_spu->i_stop < p_owner->i_preroll_end ) )
1707             {
1708                 subpicture_Delete( p_spu );
1709             }
1710             else
1711             {
1712                 DecoderPlaySpu( p_dec, p_spu );
1713             }
1714         }
1715         else
1716         {
1717             subpicture_Delete( p_spu );
1718         }
1719         if( p_vout )
1720             vlc_object_release( p_vout );
1721     }
1722
1723     if( b_flush && p_owner->p_spu_vout )
1724     {
1725         p_vout = input_resource_HoldVout( p_owner->p_resource );
1726
1727         if( p_vout && p_owner->p_spu_vout == p_vout )
1728             vout_FlushSubpictureChannel( p_vout, p_owner->i_spu_channel );
1729
1730         if( p_vout )
1731             vlc_object_release( p_vout );
1732     }
1733 }
1734
1735 /* */
1736 static void DecoderProcessOnFlush( decoder_t *p_dec )
1737 {
1738     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1739
1740     vlc_mutex_lock( &p_owner->lock );
1741
1742     if( p_owner->b_flushing )
1743     {
1744         p_owner->b_flushing = false;
1745         vlc_cond_signal( &p_owner->wait_acknowledge );
1746     }
1747     vlc_mutex_unlock( &p_owner->lock );
1748 }
1749
1750 /**
1751  * Decode a block
1752  *
1753  * \param p_dec the decoder object
1754  * \param p_block the block to decode
1755  * \return VLC_SUCCESS or an error code
1756  */
1757 static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
1758 {
1759     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1760     const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1761
1762     if( p_dec->b_error )
1763     {
1764         if( p_block )
1765             block_Release( p_block );
1766         goto flush;
1767     }
1768
1769     if( p_block && p_block->i_buffer <= 0 )
1770     {
1771         assert( !b_flush_request );
1772         block_Release( p_block );
1773         return;
1774     }
1775
1776 #ifdef ENABLE_SOUT
1777     if( p_owner->b_packetizer )
1778     {
1779         if( p_block )
1780             p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1781
1782         DecoderProcessSout( p_dec, p_block );
1783     }
1784     else
1785 #endif
1786     {
1787         bool b_flush = false;
1788
1789         if( p_block )
1790         {
1791             const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1792             DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1793
1794             b_flush = !b_flushing && b_flush_request;
1795
1796             p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1797         }
1798
1799         if( p_dec->fmt_out.i_cat == AUDIO_ES )
1800         {
1801             DecoderProcessAudio( p_dec, p_block, b_flush );
1802         }
1803         else if( p_dec->fmt_out.i_cat == VIDEO_ES )
1804         {
1805             DecoderProcessVideo( p_dec, p_block, b_flush );
1806         }
1807         else if( p_dec->fmt_out.i_cat == SPU_ES )
1808         {
1809             DecoderProcessSpu( p_dec, p_block, b_flush );
1810         }
1811         else
1812         {
1813             msg_Err( p_dec, "unknown ES format" );
1814             p_dec->b_error = true;
1815         }
1816     }
1817
1818     /* */
1819 flush:
1820     if( b_flush_request )
1821         DecoderProcessOnFlush( p_dec );
1822 }
1823
1824 /**
1825  * Destroys a decoder object
1826  *
1827  * \param p_dec the decoder object
1828  * \return nothing
1829  */
1830 static void DeleteDecoder( decoder_t * p_dec )
1831 {
1832     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1833
1834     msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
1835              (char*)&p_dec->fmt_in.i_codec,
1836              (unsigned)block_FifoCount( p_owner->p_fifo ) );
1837
1838     /* Free all packets still in the decoder fifo. */
1839     block_FifoEmpty( p_owner->p_fifo );
1840     block_FifoRelease( p_owner->p_fifo );
1841
1842     /* Cleanup */
1843     if( p_owner->p_aout )
1844     {
1845         /* TODO: REVISIT gap-less audio */
1846         aout_DecFlush( p_owner->p_aout );
1847         aout_DecDelete( p_owner->p_aout );
1848         input_resource_PutAout( p_owner->p_resource, p_owner->p_aout );
1849         if( p_owner->p_input != NULL )
1850             input_SendEventAout( p_owner->p_input );
1851     }
1852     if( p_owner->p_vout )
1853     {
1854         /* Hack to make sure all the the pictures are freed by the decoder
1855          * and that the vout is not paused anymore */
1856         vout_Reset( p_owner->p_vout );
1857
1858         /* */
1859         input_resource_RequestVout( p_owner->p_resource, p_owner->p_vout, NULL,
1860                                     0, true );
1861         if( p_owner->p_input != NULL )
1862             input_SendEventVout( p_owner->p_input );
1863     }
1864
1865 #ifdef ENABLE_SOUT
1866     if( p_owner->p_sout_input )
1867     {
1868         sout_InputDelete( p_owner->p_sout_input );
1869         es_format_Clean( &p_owner->sout );
1870     }
1871 #endif
1872
1873     if( p_dec->fmt_out.i_cat == SPU_ES )
1874     {
1875         vout_thread_t *p_vout = input_resource_HoldVout( p_owner->p_resource );
1876         if( p_vout )
1877         {
1878             if( p_owner->p_spu_vout == p_vout )
1879                 vout_FlushSubpictureChannel( p_vout, p_owner->i_spu_channel );
1880             vlc_object_release( p_vout );
1881         }
1882     }
1883
1884     es_format_Clean( &p_dec->fmt_in );
1885     es_format_Clean( &p_dec->fmt_out );
1886     if( p_dec->p_description )
1887         vlc_meta_Delete( p_dec->p_description );
1888     es_format_Clean( &p_owner->fmt_description );
1889     if( p_owner->p_description )
1890         vlc_meta_Delete( p_owner->p_description );
1891
1892     if( p_owner->p_packetizer )
1893     {
1894         module_unneed( p_owner->p_packetizer,
1895                        p_owner->p_packetizer->p_module );
1896         es_format_Clean( &p_owner->p_packetizer->fmt_in );
1897         es_format_Clean( &p_owner->p_packetizer->fmt_out );
1898         if( p_owner->p_packetizer->p_description )
1899             vlc_meta_Delete( p_owner->p_packetizer->p_description );
1900         vlc_object_release( p_owner->p_packetizer );
1901     }
1902
1903     vlc_cond_destroy( &p_owner->wait_acknowledge );
1904     vlc_cond_destroy( &p_owner->wait_request );
1905     vlc_mutex_destroy( &p_owner->lock );
1906
1907     vlc_object_release( p_dec );
1908
1909     free( p_owner );
1910 }
1911
1912 /*****************************************************************************
1913  * Buffers allocation callbacks for the decoders
1914  *****************************************************************************/
1915 static void DecoderUpdateFormatLocked( decoder_t *p_dec )
1916 {
1917     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1918
1919     vlc_assert_locked( &p_owner->lock );
1920
1921     p_owner->b_fmt_description = true;
1922
1923     /* Copy es_format */
1924     es_format_Clean( &p_owner->fmt_description );
1925     es_format_Copy( &p_owner->fmt_description, &p_dec->fmt_out );
1926
1927     /* Move p_description */
1928     if( p_owner->p_description && p_dec->p_description )
1929         vlc_meta_Delete( p_owner->p_description );
1930     p_owner->p_description = p_dec->p_description;
1931     p_dec->p_description = NULL;
1932 }
1933 static vout_thread_t *aout_request_vout( void *p_private,
1934                                          vout_thread_t *p_vout, video_format_t *p_fmt, bool b_recyle )
1935 {
1936     decoder_t *p_dec = p_private;
1937     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1938     input_thread_t *p_input = p_owner->p_input;
1939
1940     p_vout = input_resource_RequestVout( p_owner->p_resource, p_vout, p_fmt, 1,
1941                                          b_recyle );
1942     if( p_input != NULL )
1943         input_SendEventVout( p_input );
1944
1945     return p_vout;
1946 }
1947
1948 static int aout_update_format( decoder_t *p_dec )
1949 {
1950     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1951
1952     if( p_owner->p_aout
1953      && !AOUT_FMTS_IDENTICAL(&p_dec->fmt_out.audio, &p_owner->audio) )
1954     {
1955         audio_output_t *p_aout = p_owner->p_aout;
1956
1957         /* Parameters changed, restart the aout */
1958         vlc_mutex_lock( &p_owner->lock );
1959
1960         aout_DecDelete( p_owner->p_aout );
1961         p_owner->p_aout = NULL;
1962
1963         vlc_mutex_unlock( &p_owner->lock );
1964         input_resource_PutAout( p_owner->p_resource, p_aout );
1965     }
1966
1967     if( p_owner->p_aout == NULL )
1968     {
1969         const int i_force_dolby = var_InheritInteger( p_dec, "force-dolby-surround" );
1970         audio_sample_format_t format;
1971         audio_output_t *p_aout;
1972         aout_request_vout_t request_vout;
1973
1974         p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
1975         p_owner->audio = p_dec->fmt_out.audio;
1976         aout_FormatPrepare( &p_owner->audio );
1977
1978         memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
1979         if( i_force_dolby &&
1980             (format.i_original_channels&AOUT_CHAN_PHYSMASK) ==
1981                 (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
1982         {
1983             if( i_force_dolby == 1 )
1984             {
1985                 format.i_original_channels = format.i_original_channels |
1986                                              AOUT_CHAN_DOLBYSTEREO;
1987             }
1988             else /* i_force_dolby == 2 */
1989             {
1990                 format.i_original_channels = format.i_original_channels &
1991                                              ~AOUT_CHAN_DOLBYSTEREO;
1992             }
1993         }
1994
1995         request_vout.pf_request_vout = aout_request_vout;
1996         request_vout.p_private = p_dec;
1997
1998         assert( p_owner->p_aout == NULL );
1999         p_aout = input_resource_GetAout( p_owner->p_resource );
2000         if( p_aout )
2001         {
2002             if( aout_DecNew( p_aout, &format,
2003                              &p_dec->fmt_out.audio_replay_gain,
2004                              &request_vout ) )
2005             {
2006                 input_resource_PutAout( p_owner->p_resource, p_aout );
2007                 p_aout = NULL;
2008             }
2009         }
2010
2011         vlc_mutex_lock( &p_owner->lock );
2012
2013         p_owner->p_aout = p_aout;
2014         DecoderUpdateFormatLocked( p_dec );
2015         if( unlikely(p_owner->b_paused) ) /* fake pause if needed */
2016             aout_DecChangePause( p_aout, true, mdate() );
2017
2018         vlc_mutex_unlock( &p_owner->lock );
2019
2020         if( p_owner->p_input != NULL )
2021             input_SendEventAout( p_owner->p_input );
2022
2023         if( p_aout == NULL )
2024         {
2025             msg_Err( p_dec, "failed to create audio output" );
2026             p_dec->b_error = true;
2027             return -1;
2028         }
2029
2030         p_dec->fmt_out.audio.i_bytes_per_frame =
2031             p_owner->audio.i_bytes_per_frame;
2032         p_dec->fmt_out.audio.i_frame_length = p_owner->audio.i_frame_length;
2033     }
2034     return 0;
2035 }
2036
2037 static picture_t *vout_new_buffer( decoder_t *p_dec )
2038 {
2039     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2040
2041     if( p_owner->p_vout == NULL ||
2042         p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
2043         p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
2044         p_dec->fmt_out.video.i_visible_width != p_owner->video.i_visible_width ||
2045         p_dec->fmt_out.video.i_visible_height != p_owner->video.i_visible_height ||
2046         p_dec->fmt_out.video.i_x_offset != p_owner->video.i_x_offset  ||
2047         p_dec->fmt_out.video.i_y_offset != p_owner->video.i_y_offset  ||
2048         p_dec->fmt_out.i_codec != p_owner->video.i_chroma ||
2049         (int64_t)p_dec->fmt_out.video.i_sar_num * p_owner->video.i_sar_den !=
2050         (int64_t)p_dec->fmt_out.video.i_sar_den * p_owner->video.i_sar_num ||
2051         p_dec->fmt_out.video.orientation != p_owner->video.orientation )
2052     {
2053         vout_thread_t *p_vout;
2054
2055         if( !p_dec->fmt_out.video.i_width ||
2056             !p_dec->fmt_out.video.i_height )
2057         {
2058             /* Can't create a new vout without display size */
2059             return NULL;
2060         }
2061
2062         video_format_t fmt = p_dec->fmt_out.video;
2063         fmt.i_chroma = p_dec->fmt_out.i_codec;
2064         p_owner->video = fmt;
2065
2066         if( vlc_fourcc_IsYUV( fmt.i_chroma ) )
2067         {
2068             const vlc_chroma_description_t *dsc = vlc_fourcc_GetChromaDescription( fmt.i_chroma );
2069             for( unsigned int i = 0; dsc && i < dsc->plane_count; i++ )
2070             {
2071                 while( fmt.i_width % dsc->p[i].w.den )
2072                     fmt.i_width++;
2073                 while( fmt.i_height % dsc->p[i].h.den )
2074                     fmt.i_height++;
2075             }
2076         }
2077
2078         if( !fmt.i_visible_width || !fmt.i_visible_height )
2079         {
2080             if( p_dec->fmt_in.video.i_visible_width &&
2081                 p_dec->fmt_in.video.i_visible_height )
2082             {
2083                 fmt.i_visible_width  = p_dec->fmt_in.video.i_visible_width;
2084                 fmt.i_visible_height = p_dec->fmt_in.video.i_visible_height;
2085                 fmt.i_x_offset       = p_dec->fmt_in.video.i_x_offset;
2086                 fmt.i_y_offset       = p_dec->fmt_in.video.i_y_offset;
2087             }
2088             else
2089             {
2090                 fmt.i_visible_width  = fmt.i_width;
2091                 fmt.i_visible_height = fmt.i_height;
2092                 fmt.i_x_offset       = 0;
2093                 fmt.i_y_offset       = 0;
2094             }
2095         }
2096
2097         if( fmt.i_visible_height == 1088 &&
2098             var_CreateGetBool( p_dec, "hdtv-fix" ) )
2099         {
2100             fmt.i_visible_height = 1080;
2101             if( !(fmt.i_sar_num % 136))
2102             {
2103                 fmt.i_sar_num *= 135;
2104                 fmt.i_sar_den *= 136;
2105             }
2106             msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
2107         }
2108
2109         if( !fmt.i_sar_num || !fmt.i_sar_den )
2110         {
2111             fmt.i_sar_num = 1;
2112             fmt.i_sar_den = 1;
2113         }
2114
2115         vlc_ureduce( &fmt.i_sar_num, &fmt.i_sar_den,
2116                      fmt.i_sar_num, fmt.i_sar_den, 50000 );
2117
2118         vlc_mutex_lock( &p_owner->lock );
2119
2120         p_vout = p_owner->p_vout;
2121         p_owner->p_vout = NULL;
2122         vlc_mutex_unlock( &p_owner->lock );
2123
2124         unsigned dpb_size;
2125         switch( p_dec->fmt_in.i_codec )
2126         {
2127         case VLC_CODEC_HEVC:
2128         case VLC_CODEC_H264:
2129         case VLC_CODEC_DIRAC: /* FIXME valid ? */
2130             dpb_size = 18;
2131             break;
2132         case VLC_CODEC_VP5:
2133         case VLC_CODEC_VP6:
2134         case VLC_CODEC_VP6F:
2135         case VLC_CODEC_VP8:
2136             dpb_size = 3;
2137             break;
2138         default:
2139             dpb_size = 2;
2140             break;
2141         }
2142         p_vout = input_resource_RequestVout( p_owner->p_resource,
2143                                              p_vout, &fmt,
2144                                              dpb_size +
2145                                              p_dec->i_extra_picture_buffers + 1,
2146                                              true );
2147         vlc_mutex_lock( &p_owner->lock );
2148         p_owner->p_vout = p_vout;
2149
2150         DecoderUpdateFormatLocked( p_dec );
2151
2152         vlc_mutex_unlock( &p_owner->lock );
2153
2154         if( p_owner->p_input != NULL )
2155             input_SendEventVout( p_owner->p_input );
2156         if( p_vout == NULL )
2157         {
2158             msg_Err( p_dec, "failed to create video output" );
2159             p_dec->b_error = true;
2160             return NULL;
2161         }
2162     }
2163
2164     /* Get a new picture
2165      */
2166     for( ;; )
2167     {
2168         if( DecoderIsExitRequested( p_dec ) || p_dec->b_error )
2169             return NULL;
2170
2171         picture_t *p_picture = vout_GetPicture( p_owner->p_vout );
2172         if( p_picture )
2173             return p_picture;
2174
2175         if( DecoderIsFlushing( p_dec ) )
2176             return NULL;
2177
2178         /* */
2179         DecoderSignalWait( p_dec, true );
2180
2181         /* Check the decoder doesn't leak pictures */
2182         vout_FixLeaks( p_owner->p_vout );
2183
2184         /* FIXME add a vout_WaitPictureAvailable (timedwait) */
2185         msleep( VOUT_OUTMEM_SLEEP );
2186     }
2187 }
2188
2189 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
2190 {
2191     vout_ReleasePicture( p_dec->p_owner->p_vout, p_pic );
2192 }
2193
2194 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
2195 {
2196     vout_HoldPicture( p_dec->p_owner->p_vout, p_pic );
2197 }
2198
2199 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
2200 {
2201     vout_ReleasePicture( p_dec->p_owner->p_vout, p_pic );
2202 }
2203
2204 static subpicture_t *spu_new_buffer( decoder_t *p_dec,
2205                                      const subpicture_updater_t *p_updater )
2206 {
2207     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2208     vout_thread_t *p_vout = NULL;
2209     subpicture_t *p_subpic;
2210     int i_attempts = 30;
2211
2212     while( i_attempts-- )
2213     {
2214         if( DecoderIsExitRequested( p_dec ) || p_dec->b_error )
2215             break;
2216
2217         p_vout = input_resource_HoldVout( p_owner->p_resource );
2218         if( p_vout )
2219             break;
2220
2221         msleep( DECODER_SPU_VOUT_WAIT_DURATION );
2222     }
2223
2224     if( !p_vout )
2225     {
2226         msg_Warn( p_dec, "no vout found, dropping subpicture" );
2227         return NULL;
2228     }
2229
2230     if( p_owner->p_spu_vout != p_vout )
2231     {
2232         p_owner->i_spu_channel = vout_RegisterSubpictureChannel( p_vout );
2233         p_owner->i_spu_order = 0;
2234         p_owner->p_spu_vout = p_vout;
2235     }
2236
2237     p_subpic = subpicture_New( p_updater );
2238     if( p_subpic )
2239     {
2240         p_subpic->i_channel = p_owner->i_spu_channel;
2241         p_subpic->i_order = p_owner->i_spu_order++;
2242         p_subpic->b_subtitle = true;
2243     }
2244
2245     vlc_object_release( p_vout );
2246
2247     return p_subpic;
2248 }
2249
2250 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2251 {
2252     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2253     vout_thread_t *p_vout = NULL;
2254
2255     p_vout = input_resource_HoldVout( p_owner->p_resource );
2256     if( !p_vout || p_owner->p_spu_vout != p_vout )
2257     {
2258         if( p_vout )
2259             vlc_object_release( p_vout );
2260         msg_Warn( p_dec, "no vout found, leaking subpicture" );
2261         return;
2262     }
2263
2264     subpicture_Delete( p_subpic );
2265
2266     vlc_object_release( p_vout );
2267 }