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