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