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