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