]> git.sesse.net Git - vlc/blob - src/input/decoder.c
block: remove block_FifoWake()
[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 *, 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 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         bool end_wait = !p_block || p_block->i_flags & BLOCK_FLAG_CORE_EOS;
892         DecoderSignalWait( p_dec, end_wait );
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, bool b_has_data )
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         if( b_has_data )
962             p_owner->b_has_data = true;
963         vlc_cond_signal( &p_owner->wait_acknowledge );
964     }
965
966     vlc_mutex_unlock( &p_owner->lock );
967 }
968
969 static bool DecoderIsFlushing( decoder_t *p_dec )
970 {
971     decoder_owner_sys_t *p_owner = p_dec->p_owner;
972     bool b_flushing;
973
974     vlc_mutex_lock( &p_owner->lock );
975
976     b_flushing = p_owner->b_flushing;
977
978     vlc_mutex_unlock( &p_owner->lock );
979
980     return b_flushing;
981 }
982
983 static bool DecoderWaitUnblock( decoder_t *p_dec )
984 {
985     decoder_owner_sys_t *p_owner = p_dec->p_owner;
986
987     vlc_assert_locked( &p_owner->lock );
988
989     for( ;; )
990     {
991         if( p_owner->b_flushing )
992             break;
993         if( p_owner->b_paused )
994         {
995             if( p_owner->b_waiting && !p_owner->b_has_data )
996                 break;
997             if( p_owner->pause.i_ignore > 0 )
998             {
999                 p_owner->pause.i_ignore--;
1000                 break;
1001             }
1002         }
1003         else
1004         {
1005             if( !p_owner->b_waiting || !p_owner->b_has_data )
1006                 break;
1007         }
1008         vlc_cond_wait( &p_owner->wait_request, &p_owner->lock );
1009     }
1010
1011     return p_owner->b_flushing;
1012 }
1013
1014 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
1015 {
1016     if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
1017         *pi_preroll = INT64_MAX;
1018     else if( p->i_dts > VLC_TS_INVALID )
1019         *pi_preroll = __MIN( *pi_preroll, p->i_dts );
1020     else if( p->i_pts > VLC_TS_INVALID )
1021         *pi_preroll = __MIN( *pi_preroll, p->i_pts );
1022 }
1023
1024 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
1025                           mtime_t *pi_duration, int *pi_rate, mtime_t i_ts_bound )
1026 {
1027     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1028     input_clock_t   *p_clock = p_owner->p_clock;
1029
1030     vlc_assert_locked( &p_owner->lock );
1031
1032     const mtime_t i_es_delay = p_owner->i_ts_delay;
1033
1034     if( !p_clock )
1035         return;
1036
1037     const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
1038     int i_rate;
1039
1040     if( *pi_ts0 > VLC_TS_INVALID )
1041     {
1042         *pi_ts0 += i_es_delay;
1043         if( pi_ts1 && *pi_ts1 > VLC_TS_INVALID )
1044             *pi_ts1 += i_es_delay;
1045         if( input_clock_ConvertTS( VLC_OBJECT(p_dec), p_clock, &i_rate, pi_ts0, pi_ts1, i_ts_bound ) ) {
1046             if( pi_ts1 != NULL )
1047                 msg_Err(p_dec, "Could not convert timestamps %"PRId64
1048                         ", %"PRId64"", *pi_ts0, *pi_ts1);
1049             else
1050                 msg_Err(p_dec, "Could not convert timestamp %"PRId64, *pi_ts0);
1051             *pi_ts0 = VLC_TS_INVALID;
1052         }
1053     }
1054     else
1055     {
1056         i_rate = input_clock_GetRate( p_clock );
1057     }
1058
1059     /* Do not create ephemere data because of rounding errors */
1060     if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
1061         *pi_ts1 += 1;
1062
1063     if( pi_duration )
1064         *pi_duration = ( *pi_duration * i_rate + INPUT_RATE_DEFAULT-1 )
1065             / INPUT_RATE_DEFAULT;
1066
1067     if( pi_rate )
1068         *pi_rate = i_rate;
1069 }
1070
1071 static bool DecoderIsExitRequested( decoder_t *p_dec )
1072 {
1073     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1074
1075     vlc_mutex_lock( &p_owner->lock );
1076     bool b_exit = p_owner->b_exit;
1077     vlc_mutex_unlock( &p_owner->lock );
1078
1079     return b_exit;
1080 }
1081
1082 /**
1083  * If *pb_reject, it does nothing, otherwise it waits for the given
1084  * deadline or a flush request (in which case it set *pi_reject to true.
1085  */
1086 static void DecoderWaitDate( decoder_t *p_dec,
1087                              bool *pb_reject, mtime_t i_deadline )
1088 {
1089     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1090
1091     vlc_assert_locked( &p_owner->lock );
1092
1093     if( *pb_reject || i_deadline < 0 )
1094         return;
1095
1096     do
1097     {
1098         if( p_owner->b_flushing || p_owner->b_exit )
1099         {
1100             *pb_reject = true;
1101             break;
1102         }
1103     }
1104     while( vlc_cond_timedwait( &p_owner->wait_request, &p_owner->lock,
1105                                i_deadline ) == 0 );
1106 }
1107
1108 static void DecoderPlayAudio( decoder_t *p_dec, block_t *p_audio,
1109                               int *pi_played_sum, int *pi_lost_sum )
1110 {
1111     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1112     audio_output_t *p_aout = p_owner->p_aout;
1113
1114     /* */
1115     if( p_audio && p_audio->i_pts <= VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify audio_output/*
1116     {
1117         msg_Warn( p_dec, "non-dated audio buffer received" );
1118         *pi_lost_sum += 1;
1119         block_Release( p_audio );
1120         return;
1121     }
1122
1123     /* */
1124     vlc_mutex_lock( &p_owner->lock );
1125
1126     if( p_audio && p_owner->b_waiting )
1127     {
1128         p_owner->b_has_data = true;
1129         vlc_cond_signal( &p_owner->wait_acknowledge );
1130     }
1131
1132     for( ;; )
1133     {
1134         bool b_paused;
1135
1136         bool b_reject = DecoderWaitUnblock( p_dec );
1137
1138         b_paused = p_owner->b_paused;
1139
1140         if (!p_audio)
1141             break;
1142
1143         /* */
1144         int i_rate = INPUT_RATE_DEFAULT;
1145
1146         DecoderFixTs( p_dec, &p_audio->i_pts, NULL, &p_audio->i_length,
1147                       &i_rate, AOUT_MAX_ADVANCE_TIME );
1148
1149         if( p_audio->i_pts <= VLC_TS_INVALID
1150          || i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE
1151          || i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
1152             b_reject = true;
1153
1154         DecoderWaitDate( p_dec, &b_reject,
1155                          p_audio->i_pts - AOUT_MAX_PREPARE_TIME );
1156
1157         if( unlikely(p_owner->b_paused != b_paused) )
1158             continue; /* race with input thread? retry... */
1159         if( p_aout == NULL )
1160             b_reject = true;
1161
1162         if( !b_reject )
1163         {
1164             assert( !p_owner->b_paused );
1165             if( !aout_DecPlay( p_aout, p_audio, i_rate ) )
1166                 *pi_played_sum += 1;
1167             *pi_lost_sum += aout_DecGetResetLost( p_aout );
1168         }
1169         else
1170         {
1171             msg_Dbg( p_dec, "discarded audio buffer" );
1172             *pi_lost_sum += 1;
1173             block_Release( p_audio );
1174         }
1175
1176         break;
1177     }
1178     vlc_mutex_unlock( &p_owner->lock );
1179 }
1180
1181 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1182 {
1183     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1184     block_t *p_aout_buf;
1185     int i_decoded = 0;
1186     int i_lost = 0;
1187     int i_played = 0;
1188
1189     if (!p_block) {
1190         /* Play a NULL block to output buffered frames */
1191         DecoderPlayAudio( p_dec, NULL, &i_played, &i_lost );
1192     }
1193     else while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1194     {
1195         if( DecoderIsExitRequested( p_dec ) )
1196         {
1197             /* It prevent freezing VLC in case of broken decoder */
1198             block_Release( p_aout_buf );
1199             if( p_block )
1200                 block_Release( p_block );
1201             break;
1202         }
1203         i_decoded++;
1204
1205         if( p_owner->i_preroll_end > VLC_TS_INVALID &&
1206             p_aout_buf->i_pts < p_owner->i_preroll_end )
1207         {
1208             block_Release( p_aout_buf );
1209             continue;
1210         }
1211
1212         if( p_owner->i_preroll_end > VLC_TS_INVALID )
1213         {
1214             msg_Dbg( p_dec, "End of audio preroll" );
1215             if( p_owner->p_aout )
1216                 aout_DecFlush( p_owner->p_aout );
1217             /* */
1218             p_owner->i_preroll_end = VLC_TS_INVALID;
1219         }
1220
1221         DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1222     }
1223
1224     /* Update ugly stat */
1225     input_thread_t  *p_input = p_owner->p_input;
1226
1227     if( p_input != NULL && (i_decoded > 0 || i_lost > 0 || i_played > 0) )
1228     {
1229         vlc_mutex_lock( &p_input->p->counters.counters_lock);
1230         stats_Update( p_input->p->counters.p_lost_abuffers, i_lost, NULL );
1231         stats_Update( p_input->p->counters.p_played_abuffers, i_played, NULL );
1232         stats_Update( p_input->p->counters.p_decoded_audio, i_decoded, NULL );
1233         vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1234     }
1235 }
1236 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1237 {
1238     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1239     block_t *p_cc;
1240     bool pb_present[4];
1241     bool b_processed = false;
1242     int i;
1243     int i_cc_decoder;
1244
1245     assert( p_dec_cc->pf_get_cc != NULL );
1246
1247     /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1248     if( !p_owner->cc.b_supported )
1249         return;
1250
1251     p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1252     if( !p_cc )
1253         return;
1254
1255     vlc_mutex_lock( &p_owner->lock );
1256     for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1257     {
1258         p_owner->cc.pb_present[i] |= pb_present[i];
1259         if( p_owner->cc.pp_decoder[i] )
1260             i_cc_decoder++;
1261     }
1262
1263     for( i = 0; i < 4; i++ )
1264     {
1265         if( !p_owner->cc.pp_decoder[i] )
1266             continue;
1267
1268         block_FifoPut( p_owner->cc.pp_decoder[i]->p_owner->p_fifo,
1269             (i_cc_decoder > 1) ? block_Duplicate(p_cc) : p_cc);
1270
1271         i_cc_decoder--;
1272         b_processed = true;
1273     }
1274     vlc_mutex_unlock( &p_owner->lock );
1275
1276     if( !b_processed )
1277         block_Release( p_cc );
1278 }
1279
1280 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
1281                               int *pi_played_sum, int *pi_lost_sum )
1282 {
1283     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1284     vout_thread_t  *p_vout = p_owner->p_vout;
1285
1286     if( p_picture->date <= VLC_TS_INVALID )
1287     {
1288         msg_Warn( p_dec, "non-dated video buffer received" );
1289         *pi_lost_sum += 1;
1290         picture_Release( p_picture );
1291         return;
1292     }
1293
1294     /* */
1295     vlc_mutex_lock( &p_owner->lock );
1296
1297     if( p_owner->b_waiting && !p_owner->b_first )
1298     {
1299         p_owner->b_has_data = true;
1300         vlc_cond_signal( &p_owner->wait_acknowledge );
1301     }
1302     bool b_first_after_wait = p_owner->b_waiting && p_owner->b_has_data;
1303
1304     bool b_reject = DecoderWaitUnblock( p_dec );
1305
1306     if( p_owner->b_waiting )
1307     {
1308         assert( p_owner->b_first );
1309         msg_Dbg( p_dec, "Received first picture" );
1310         p_owner->b_first = false;
1311         p_picture->b_force = true;
1312     }
1313
1314     const bool b_dated = p_picture->date > VLC_TS_INVALID;
1315     int i_rate = INPUT_RATE_DEFAULT;
1316     DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1317                   &i_rate, DECODER_BOGUS_VIDEO_DELAY );
1318
1319     vlc_mutex_unlock( &p_owner->lock );
1320
1321     /* */
1322     if( !p_picture->b_force && p_picture->date <= VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify video_output/*
1323         b_reject = true;
1324
1325     if( !b_reject )
1326     {
1327         if( i_rate != p_owner->i_last_rate || b_first_after_wait )
1328         {
1329             /* Be sure to not display old picture after our own */
1330             vout_Flush( p_vout, p_picture->date );
1331             p_owner->i_last_rate = i_rate;
1332         }
1333         vout_PutPicture( p_vout, p_picture );
1334     }
1335     else
1336     {
1337         if( b_dated )
1338             msg_Warn( p_dec, "early picture skipped" );
1339         else
1340             msg_Warn( p_dec, "non-dated video buffer received" );
1341
1342         *pi_lost_sum += 1;
1343         picture_Release( p_picture );
1344     }
1345     int i_tmp_display;
1346     int i_tmp_lost;
1347     vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1348
1349     *pi_played_sum += i_tmp_display;
1350     *pi_lost_sum += i_tmp_lost;
1351 }
1352
1353 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1354 {
1355     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1356     picture_t      *p_pic;
1357     int i_lost = 0;
1358     int i_decoded = 0;
1359     int i_displayed = 0;
1360
1361     while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1362     {
1363         vout_thread_t  *p_vout = p_owner->p_vout;
1364         if( DecoderIsExitRequested( p_dec ) )
1365         {
1366             /* It prevent freezing VLC in case of broken decoder */
1367             picture_Release( p_pic );
1368             if( p_block )
1369                 block_Release( p_block );
1370             break;
1371         }
1372
1373         i_decoded++;
1374
1375         if( p_owner->i_preroll_end > VLC_TS_INVALID && p_pic->date < p_owner->i_preroll_end )
1376         {
1377             picture_Release( p_pic );
1378             continue;
1379         }
1380
1381         if( p_owner->i_preroll_end > VLC_TS_INVALID )
1382         {
1383             msg_Dbg( p_dec, "End of video preroll" );
1384             if( p_vout )
1385                 vout_Flush( p_vout, VLC_TS_INVALID+1 );
1386             /* */
1387             p_owner->i_preroll_end = VLC_TS_INVALID;
1388         }
1389
1390         if( p_dec->pf_get_cc &&
1391             ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1392             DecoderGetCc( p_dec, p_dec );
1393
1394         DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
1395     }
1396
1397     /* Update ugly stat */
1398     input_thread_t *p_input = p_owner->p_input;
1399
1400     if( p_input != NULL && (i_decoded > 0 || i_lost > 0 || i_displayed > 0) )
1401     {
1402         vlc_mutex_lock( &p_input->p->counters.counters_lock );
1403         stats_Update( p_input->p->counters.p_decoded_video, i_decoded, NULL );
1404         stats_Update( p_input->p->counters.p_lost_pictures, i_lost , NULL);
1405         stats_Update( p_input->p->counters.p_displayed_pictures,
1406                       i_displayed, NULL);
1407         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1408     }
1409 }
1410
1411 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic )
1412 {
1413     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1414     vout_thread_t *p_vout = p_owner->p_spu_vout;
1415
1416     /* */
1417     if( p_subpic->i_start <= VLC_TS_INVALID )
1418     {
1419         msg_Warn( p_dec, "non-dated spu buffer received" );
1420         subpicture_Delete( p_subpic );
1421         return;
1422     }
1423
1424     /* */
1425     vlc_mutex_lock( &p_owner->lock );
1426
1427     if( p_owner->b_waiting )
1428     {
1429         p_owner->b_has_data = true;
1430         vlc_cond_signal( &p_owner->wait_acknowledge );
1431     }
1432
1433     bool b_reject = DecoderWaitUnblock( p_dec );
1434
1435     DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1436                   NULL, INT64_MAX );
1437
1438     if( p_subpic->i_start <= VLC_TS_INVALID )
1439         b_reject = true;
1440
1441     DecoderWaitDate( p_dec, &b_reject,
1442                      p_subpic->i_start - SPU_MAX_PREPARE_TIME );
1443     vlc_mutex_unlock( &p_owner->lock );
1444
1445     if( !b_reject )
1446         vout_PutSubpicture( p_vout, p_subpic );
1447     else
1448         subpicture_Delete( p_subpic );
1449 }
1450
1451 #ifdef ENABLE_SOUT
1452 static int DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block )
1453 {
1454     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1455
1456     assert( p_owner->p_clock );
1457     assert( !p_sout_block->p_next );
1458
1459     vlc_mutex_lock( &p_owner->lock );
1460
1461     if( p_owner->b_waiting )
1462     {
1463         p_owner->b_has_data = true;
1464         vlc_cond_signal( &p_owner->wait_acknowledge );
1465     }
1466
1467     bool b_reject = DecoderWaitUnblock( p_dec );
1468
1469     DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
1470                   &p_sout_block->i_length, NULL, INT64_MAX );
1471
1472     vlc_mutex_unlock( &p_owner->lock );
1473
1474     if( !b_reject )
1475     {
1476         /* FIXME --VLC_TS_INVALID inspect stream_output*/
1477         return sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block );
1478     }
1479     else
1480     {
1481         block_Release( p_sout_block );
1482         return VLC_EGENERIC;
1483     }
1484 }
1485 #endif
1486
1487 #ifdef ENABLE_SOUT
1488 /* This function process a block for sout
1489  */
1490 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1491 {
1492     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1493     block_t *p_sout_block;
1494
1495     while( ( p_sout_block =
1496                  p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1497     {
1498         if( p_owner->p_sout_input == NULL )
1499         {
1500             assert( !p_owner->b_fmt_description ); // no need for owner lock
1501             es_format_Clean( &p_owner->fmt );
1502             es_format_Copy( &p_owner->fmt, &p_dec->fmt_out );
1503
1504             p_owner->fmt.i_group = p_dec->fmt_in.i_group;
1505             p_owner->fmt.i_id = p_dec->fmt_in.i_id;
1506             if( p_dec->fmt_in.psz_language )
1507             {
1508                 free( p_owner->fmt.psz_language );
1509                 p_owner->fmt.psz_language =
1510                     strdup( p_dec->fmt_in.psz_language );
1511             }
1512
1513             p_owner->p_sout_input =
1514                 sout_InputNew( p_owner->p_sout, &p_owner->fmt );
1515
1516             if( p_owner->p_sout_input == NULL )
1517             {
1518                 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1519                          (char *)&p_owner->fmt.i_codec );
1520                 p_dec->b_error = true;
1521
1522                 block_ChainRelease(p_sout_block);
1523                 break;
1524             }
1525         }
1526
1527         while( p_sout_block )
1528         {
1529             block_t *p_next = p_sout_block->p_next;
1530
1531             p_sout_block->p_next = NULL;
1532
1533             if( DecoderPlaySout( p_dec, p_sout_block ) == VLC_EGENERIC )
1534             {
1535                 msg_Err( p_dec, "cannot continue streaming due to errors" );
1536
1537                 p_dec->b_error = true;
1538
1539                 /* Cleanup */
1540                 block_ChainRelease( p_next );
1541                 return;
1542             }
1543
1544             p_sout_block = p_next;
1545         }
1546     }
1547 }
1548 #endif
1549
1550 /* This function process a video block
1551  */
1552 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1553 {
1554     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1555
1556     if( p_owner->p_packetizer )
1557     {
1558         block_t *p_packetized_block;
1559         decoder_t *p_packetizer = p_owner->p_packetizer;
1560
1561         while( (p_packetized_block =
1562                 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1563         {
1564             if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1565             {
1566                 es_format_Clean( &p_dec->fmt_in );
1567                 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1568             }
1569
1570             /* If the packetizer provides aspect ratio information, pass it
1571              * to the decoder as a hint if the decoder itself can't provide
1572              * it. Copy it regardless of the current value of the decoder input
1573              * format aspect ratio, to properly propagate changes in aspect
1574              * ratio. */
1575             if( p_packetizer->fmt_out.video.i_sar_num > 0 &&
1576                     p_packetizer->fmt_out.video.i_sar_den > 0)
1577             {
1578                 p_dec->fmt_in.video.i_sar_num =
1579                     p_packetizer->fmt_out.video.i_sar_num;
1580                 p_dec->fmt_in.video.i_sar_den=
1581                     p_packetizer->fmt_out.video.i_sar_den;
1582             }
1583
1584             if( p_packetizer->pf_get_cc )
1585                 DecoderGetCc( p_dec, p_packetizer );
1586
1587             while( p_packetized_block )
1588             {
1589                 block_t *p_next = p_packetized_block->p_next;
1590                 p_packetized_block->p_next = NULL;
1591
1592                 DecoderDecodeVideo( p_dec, p_packetized_block );
1593
1594                 p_packetized_block = p_next;
1595             }
1596         }
1597         /* The packetizer does not output a block that tell the decoder to flush
1598          * do it ourself */
1599         if( b_flush )
1600         {
1601             block_t *p_null = DecoderBlockFlushNew();
1602             if( p_null )
1603                 DecoderDecodeVideo( p_dec, p_null );
1604         }
1605     }
1606     else
1607     {
1608         DecoderDecodeVideo( p_dec, p_block );
1609     }
1610
1611     if( b_flush && p_owner->p_vout )
1612         vout_Flush( p_owner->p_vout, VLC_TS_INVALID+1 );
1613 }
1614
1615 /* This function process a audio block
1616  */
1617 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1618 {
1619     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1620
1621     if( p_owner->p_packetizer )
1622     {
1623         block_t *p_packetized_block;
1624         decoder_t *p_packetizer = p_owner->p_packetizer;
1625
1626         while( (p_packetized_block =
1627                 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1628         {
1629             if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1630             {
1631                 es_format_Clean( &p_dec->fmt_in );
1632                 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1633             }
1634
1635             while( p_packetized_block )
1636             {
1637                 block_t *p_next = p_packetized_block->p_next;
1638                 p_packetized_block->p_next = NULL;
1639
1640                 DecoderDecodeAudio( p_dec, p_packetized_block );
1641
1642                 p_packetized_block = p_next;
1643             }
1644         }
1645         /* The packetizer does not output a block that tell the decoder to flush
1646          * do it ourself */
1647         if( b_flush )
1648         {
1649             block_t *p_null = DecoderBlockFlushNew();
1650             if( p_null )
1651                 DecoderDecodeAudio( p_dec, p_null );
1652         }
1653     }
1654     else
1655     {
1656         DecoderDecodeAudio( p_dec, p_block );
1657     }
1658
1659     if( b_flush && p_owner->p_aout )
1660         aout_DecFlush( p_owner->p_aout );
1661 }
1662
1663 /* This function process a subtitle block
1664  */
1665 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1666 {
1667     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1668
1669     input_thread_t *p_input = p_owner->p_input;
1670     vout_thread_t *p_vout;
1671     subpicture_t *p_spu;
1672
1673     while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1674     {
1675         if( p_input != NULL )
1676         {
1677             vlc_mutex_lock( &p_input->p->counters.counters_lock );
1678             stats_Update( p_input->p->counters.p_decoded_sub, 1, NULL );
1679             vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1680         }
1681
1682         p_vout = input_resource_HoldVout( p_owner->p_resource );
1683         if( p_vout && p_owner->p_spu_vout == p_vout )
1684         {
1685             /* Preroll does not work very well with subtitle */
1686             if( p_spu->i_start > VLC_TS_INVALID &&
1687                 p_spu->i_start < p_owner->i_preroll_end &&
1688                 ( p_spu->i_stop <= VLC_TS_INVALID || p_spu->i_stop < p_owner->i_preroll_end ) )
1689             {
1690                 subpicture_Delete( p_spu );
1691             }
1692             else
1693             {
1694                 DecoderPlaySpu( p_dec, p_spu );
1695             }
1696         }
1697         else
1698         {
1699             subpicture_Delete( p_spu );
1700         }
1701         if( p_vout )
1702             vlc_object_release( p_vout );
1703     }
1704
1705     if( b_flush && p_owner->p_spu_vout )
1706     {
1707         p_vout = input_resource_HoldVout( p_owner->p_resource );
1708
1709         if( p_vout && p_owner->p_spu_vout == p_vout )
1710             vout_FlushSubpictureChannel( p_vout, p_owner->i_spu_channel );
1711
1712         if( p_vout )
1713             vlc_object_release( p_vout );
1714     }
1715 }
1716
1717 /* */
1718 static void DecoderProcessOnFlush( decoder_t *p_dec )
1719 {
1720     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1721
1722     vlc_mutex_lock( &p_owner->lock );
1723
1724     if( p_owner->b_flushing )
1725     {
1726         p_owner->b_flushing = false;
1727         vlc_cond_signal( &p_owner->wait_acknowledge );
1728     }
1729     vlc_mutex_unlock( &p_owner->lock );
1730 }
1731
1732 /**
1733  * Decode a block
1734  *
1735  * \param p_dec the decoder object
1736  * \param p_block the block to decode
1737  * \return VLC_SUCCESS or an error code
1738  */
1739 static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
1740 {
1741     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1742     const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1743
1744     if( p_dec->b_error )
1745     {
1746         if( p_block )
1747             block_Release( p_block );
1748         goto flush;
1749     }
1750
1751     if( p_block && p_block->i_buffer <= 0 )
1752     {
1753         assert( !b_flush_request );
1754         block_Release( p_block );
1755         return;
1756     }
1757
1758 #ifdef ENABLE_SOUT
1759     if( p_owner->b_packetizer )
1760     {
1761         if( p_block )
1762             p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1763
1764         DecoderProcessSout( p_dec, p_block );
1765     }
1766     else
1767 #endif
1768     {
1769         bool b_flush = false;
1770
1771         if( p_block )
1772         {
1773             const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1774             DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1775
1776             b_flush = !b_flushing && b_flush_request;
1777
1778             p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1779         }
1780
1781         if( p_dec->fmt_out.i_cat == AUDIO_ES )
1782         {
1783             DecoderProcessAudio( p_dec, p_block, b_flush );
1784         }
1785         else if( p_dec->fmt_out.i_cat == VIDEO_ES )
1786         {
1787             DecoderProcessVideo( p_dec, p_block, b_flush );
1788         }
1789         else if( p_dec->fmt_out.i_cat == SPU_ES )
1790         {
1791             DecoderProcessSpu( p_dec, p_block, b_flush );
1792         }
1793         else
1794         {
1795             msg_Err( p_dec, "unknown ES format" );
1796             p_dec->b_error = true;
1797         }
1798     }
1799
1800     /* */
1801 flush:
1802     if( b_flush_request )
1803         DecoderProcessOnFlush( p_dec );
1804 }
1805
1806 /**
1807  * Destroys a decoder object
1808  *
1809  * \param p_dec the decoder object
1810  * \return nothing
1811  */
1812 static void DeleteDecoder( decoder_t * p_dec )
1813 {
1814     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1815
1816     msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
1817              (char*)&p_dec->fmt_in.i_codec,
1818              (unsigned)block_FifoCount( p_owner->p_fifo ) );
1819
1820     /* Free all packets still in the decoder fifo. */
1821     block_FifoRelease( p_owner->p_fifo );
1822
1823     /* Cleanup */
1824     if( p_owner->p_aout )
1825     {
1826         /* TODO: REVISIT gap-less audio */
1827         aout_DecFlush( p_owner->p_aout );
1828         aout_DecDelete( p_owner->p_aout );
1829         input_resource_PutAout( p_owner->p_resource, p_owner->p_aout );
1830         if( p_owner->p_input != NULL )
1831             input_SendEventAout( p_owner->p_input );
1832     }
1833     if( p_owner->p_vout )
1834     {
1835         /* Hack to make sure all the the pictures are freed by the decoder
1836          * and that the vout is not paused anymore */
1837         vout_Reset( p_owner->p_vout );
1838
1839         /* */
1840         input_resource_RequestVout( p_owner->p_resource, p_owner->p_vout, NULL,
1841                                     0, true );
1842         if( p_owner->p_input != NULL )
1843             input_SendEventVout( p_owner->p_input );
1844     }
1845
1846 #ifdef ENABLE_SOUT
1847     if( p_owner->p_sout_input )
1848     {
1849         sout_InputDelete( p_owner->p_sout_input );
1850     }
1851 #endif
1852     es_format_Clean( &p_owner->fmt );
1853
1854     if( p_dec->fmt_out.i_cat == SPU_ES )
1855     {
1856         vout_thread_t *p_vout = input_resource_HoldVout( p_owner->p_resource );
1857         if( p_vout )
1858         {
1859             if( p_owner->p_spu_vout == p_vout )
1860                 vout_FlushSubpictureChannel( p_vout, p_owner->i_spu_channel );
1861             vlc_object_release( p_vout );
1862         }
1863     }
1864
1865     es_format_Clean( &p_dec->fmt_in );
1866     es_format_Clean( &p_dec->fmt_out );
1867     if( p_dec->p_description )
1868         vlc_meta_Delete( p_dec->p_description );
1869     if( p_owner->p_description )
1870         vlc_meta_Delete( p_owner->p_description );
1871
1872     if( p_owner->p_packetizer )
1873     {
1874         module_unneed( p_owner->p_packetizer,
1875                        p_owner->p_packetizer->p_module );
1876         es_format_Clean( &p_owner->p_packetizer->fmt_in );
1877         es_format_Clean( &p_owner->p_packetizer->fmt_out );
1878         if( p_owner->p_packetizer->p_description )
1879             vlc_meta_Delete( p_owner->p_packetizer->p_description );
1880         vlc_object_release( p_owner->p_packetizer );
1881     }
1882
1883     vlc_cond_destroy( &p_owner->wait_acknowledge );
1884     vlc_cond_destroy( &p_owner->wait_request );
1885     vlc_mutex_destroy( &p_owner->lock );
1886
1887     vlc_object_release( p_dec );
1888
1889     free( p_owner );
1890 }
1891
1892 /*****************************************************************************
1893  * Buffers allocation callbacks for the decoders
1894  *****************************************************************************/
1895 static void DecoderUpdateFormatLocked( decoder_t *p_dec )
1896 {
1897     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1898
1899     vlc_assert_locked( &p_owner->lock );
1900
1901     p_owner->b_fmt_description = true;
1902
1903     /* Move p_description */
1904     if( p_owner->p_description && p_dec->p_description )
1905         vlc_meta_Delete( p_owner->p_description );
1906     p_owner->p_description = p_dec->p_description;
1907     p_dec->p_description = NULL;
1908 }
1909 static vout_thread_t *aout_request_vout( void *p_private,
1910                                          vout_thread_t *p_vout, video_format_t *p_fmt, bool b_recyle )
1911 {
1912     decoder_t *p_dec = p_private;
1913     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1914     input_thread_t *p_input = p_owner->p_input;
1915
1916     p_vout = input_resource_RequestVout( p_owner->p_resource, p_vout, p_fmt, 1,
1917                                          b_recyle );
1918     if( p_input != NULL )
1919         input_SendEventVout( p_input );
1920
1921     return p_vout;
1922 }
1923
1924 static int aout_update_format( decoder_t *p_dec )
1925 {
1926     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1927
1928     if( p_owner->p_aout
1929      && !AOUT_FMTS_IDENTICAL(&p_dec->fmt_out.audio, &p_owner->fmt.audio) )
1930     {
1931         audio_output_t *p_aout = p_owner->p_aout;
1932
1933         /* Parameters changed, restart the aout */
1934         vlc_mutex_lock( &p_owner->lock );
1935
1936         aout_DecDelete( p_owner->p_aout );
1937         p_owner->p_aout = NULL;
1938
1939         vlc_mutex_unlock( &p_owner->lock );
1940         input_resource_PutAout( p_owner->p_resource, p_aout );
1941     }
1942
1943     if( p_owner->p_aout == NULL )
1944     {
1945         p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
1946
1947         audio_sample_format_t format = p_dec->fmt_out.audio;
1948         aout_FormatPrepare( &format );
1949
1950         const int i_force_dolby = var_InheritInteger( p_dec, "force-dolby-surround" );
1951         if( i_force_dolby &&
1952             (format.i_original_channels&AOUT_CHAN_PHYSMASK) ==
1953                 (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
1954         {
1955             if( i_force_dolby == 1 )
1956             {
1957                 format.i_original_channels = format.i_original_channels |
1958                                              AOUT_CHAN_DOLBYSTEREO;
1959             }
1960             else /* i_force_dolby == 2 */
1961             {
1962                 format.i_original_channels = format.i_original_channels &
1963                                              ~AOUT_CHAN_DOLBYSTEREO;
1964             }
1965         }
1966
1967         aout_request_vout_t request_vout = {
1968             .pf_request_vout = aout_request_vout,
1969             .p_private = p_dec,
1970         };
1971         audio_output_t *p_aout;
1972
1973         p_aout = input_resource_GetAout( p_owner->p_resource );
1974         if( p_aout )
1975         {
1976             if( aout_DecNew( p_aout, &format,
1977                              &p_dec->fmt_out.audio_replay_gain,
1978                              &request_vout ) )
1979             {
1980                 input_resource_PutAout( p_owner->p_resource, p_aout );
1981                 p_aout = NULL;
1982             }
1983         }
1984
1985         vlc_mutex_lock( &p_owner->lock );
1986
1987         p_owner->p_aout = p_aout;
1988
1989         es_format_Clean( &p_owner->fmt );
1990         es_format_Copy( &p_owner->fmt, &p_dec->fmt_out );
1991         aout_FormatPrepare( &p_owner->fmt.audio );
1992
1993         DecoderUpdateFormatLocked( p_dec );
1994
1995         if( unlikely(p_owner->b_paused) && p_aout != NULL )
1996             /* fake pause if needed */
1997             aout_DecChangePause( p_aout, true, mdate() );
1998
1999         vlc_mutex_unlock( &p_owner->lock );
2000
2001         if( p_owner->p_input != NULL )
2002             input_SendEventAout( p_owner->p_input );
2003
2004         if( p_aout == NULL )
2005         {
2006             msg_Err( p_dec, "failed to create audio output" );
2007             p_dec->b_error = true;
2008             return -1;
2009         }
2010
2011         p_dec->fmt_out.audio.i_bytes_per_frame =
2012             p_owner->fmt.audio.i_bytes_per_frame;
2013         p_dec->fmt_out.audio.i_frame_length =
2014             p_owner->fmt.audio.i_frame_length;
2015     }
2016     return 0;
2017 }
2018
2019 static int vout_update_format( decoder_t *p_dec )
2020 {
2021     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2022
2023     if( p_owner->p_vout == NULL
2024      || p_dec->fmt_out.video.i_width != p_owner->fmt.video.i_width
2025      || p_dec->fmt_out.video.i_height != p_owner->fmt.video.i_height
2026      || p_dec->fmt_out.video.i_visible_width != p_owner->fmt.video.i_visible_width
2027      || p_dec->fmt_out.video.i_visible_height != p_owner->fmt.video.i_visible_height
2028      || p_dec->fmt_out.video.i_x_offset != p_owner->fmt.video.i_x_offset
2029      || p_dec->fmt_out.video.i_y_offset != p_owner->fmt.video.i_y_offset
2030      || p_dec->fmt_out.i_codec != p_owner->fmt.video.i_chroma
2031      || (int64_t)p_dec->fmt_out.video.i_sar_num * p_owner->fmt.video.i_sar_den !=
2032         (int64_t)p_dec->fmt_out.video.i_sar_den * p_owner->fmt.video.i_sar_num ||
2033         p_dec->fmt_out.video.orientation != p_owner->fmt.video.orientation )
2034     {
2035         vout_thread_t *p_vout;
2036
2037         if( !p_dec->fmt_out.video.i_width ||
2038             !p_dec->fmt_out.video.i_height )
2039         {
2040             /* Can't create a new vout without display size */
2041             return -1;
2042         }
2043
2044         video_format_t fmt = p_dec->fmt_out.video;
2045         fmt.i_chroma = p_dec->fmt_out.i_codec;
2046
2047         if( vlc_fourcc_IsYUV( fmt.i_chroma ) )
2048         {
2049             const vlc_chroma_description_t *dsc = vlc_fourcc_GetChromaDescription( fmt.i_chroma );
2050             for( unsigned int i = 0; dsc && i < dsc->plane_count; i++ )
2051             {
2052                 while( fmt.i_width % dsc->p[i].w.den )
2053                     fmt.i_width++;
2054                 while( fmt.i_height % dsc->p[i].h.den )
2055                     fmt.i_height++;
2056             }
2057         }
2058
2059         if( !fmt.i_visible_width || !fmt.i_visible_height )
2060         {
2061             if( p_dec->fmt_in.video.i_visible_width &&
2062                 p_dec->fmt_in.video.i_visible_height )
2063             {
2064                 fmt.i_visible_width  = p_dec->fmt_in.video.i_visible_width;
2065                 fmt.i_visible_height = p_dec->fmt_in.video.i_visible_height;
2066                 fmt.i_x_offset       = p_dec->fmt_in.video.i_x_offset;
2067                 fmt.i_y_offset       = p_dec->fmt_in.video.i_y_offset;
2068             }
2069             else
2070             {
2071                 fmt.i_visible_width  = fmt.i_width;
2072                 fmt.i_visible_height = fmt.i_height;
2073                 fmt.i_x_offset       = 0;
2074                 fmt.i_y_offset       = 0;
2075             }
2076         }
2077
2078         if( fmt.i_visible_height == 1088 &&
2079             var_CreateGetBool( p_dec, "hdtv-fix" ) )
2080         {
2081             fmt.i_visible_height = 1080;
2082             if( !(fmt.i_sar_num % 136))
2083             {
2084                 fmt.i_sar_num *= 135;
2085                 fmt.i_sar_den *= 136;
2086             }
2087             msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
2088         }
2089
2090         if( !fmt.i_sar_num || !fmt.i_sar_den )
2091         {
2092             fmt.i_sar_num = 1;
2093             fmt.i_sar_den = 1;
2094         }
2095
2096         vlc_ureduce( &fmt.i_sar_num, &fmt.i_sar_den,
2097                      fmt.i_sar_num, fmt.i_sar_den, 50000 );
2098
2099         vlc_mutex_lock( &p_owner->lock );
2100
2101         p_vout = p_owner->p_vout;
2102         p_owner->p_vout = NULL;
2103         vlc_mutex_unlock( &p_owner->lock );
2104
2105         unsigned dpb_size;
2106         switch( p_dec->fmt_in.i_codec )
2107         {
2108         case VLC_CODEC_HEVC:
2109         case VLC_CODEC_H264:
2110         case VLC_CODEC_DIRAC: /* FIXME valid ? */
2111             dpb_size = 18;
2112             break;
2113         case VLC_CODEC_VP5:
2114         case VLC_CODEC_VP6:
2115         case VLC_CODEC_VP6F:
2116         case VLC_CODEC_VP8:
2117             dpb_size = 3;
2118             break;
2119         default:
2120             dpb_size = 2;
2121             break;
2122         }
2123         p_vout = input_resource_RequestVout( p_owner->p_resource,
2124                                              p_vout, &fmt,
2125                                              dpb_size +
2126                                              p_dec->i_extra_picture_buffers + 1,
2127                                              true );
2128         vlc_mutex_lock( &p_owner->lock );
2129         p_owner->p_vout = p_vout;
2130
2131         es_format_Clean( &p_owner->fmt );
2132         es_format_Copy( &p_owner->fmt, &p_dec->fmt_out );
2133         p_owner->fmt.video.i_chroma = p_dec->fmt_out.i_codec;
2134
2135         DecoderUpdateFormatLocked( p_dec );
2136
2137         vlc_mutex_unlock( &p_owner->lock );
2138
2139         if( p_owner->p_input != NULL )
2140             input_SendEventVout( p_owner->p_input );
2141         if( p_vout == NULL )
2142         {
2143             msg_Err( p_dec, "failed to create video output" );
2144             p_dec->b_error = true;
2145             return -1;
2146         }
2147     }
2148     return 0;
2149 }
2150
2151 static picture_t *vout_new_buffer( decoder_t *p_dec )
2152 {
2153     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2154
2155     for( ;; )
2156     {
2157         if( DecoderIsExitRequested( p_dec ) || p_dec->b_error )
2158             return NULL;
2159
2160         picture_t *p_picture = vout_GetPicture( p_owner->p_vout );
2161         if( p_picture )
2162             return p_picture;
2163
2164         if( DecoderIsFlushing( p_dec ) )
2165             return NULL;
2166
2167         /* */
2168         DecoderSignalWait( p_dec, true );
2169
2170         /* Check the decoder doesn't leak pictures */
2171         vout_FixLeaks( p_owner->p_vout );
2172
2173         /* FIXME add a vout_WaitPictureAvailable (timedwait) */
2174         msleep( VOUT_OUTMEM_SLEEP );
2175     }
2176 }
2177
2178 static subpicture_t *spu_new_buffer( decoder_t *p_dec,
2179                                      const subpicture_updater_t *p_updater )
2180 {
2181     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2182     vout_thread_t *p_vout = NULL;
2183     subpicture_t *p_subpic;
2184     int i_attempts = 30;
2185
2186     while( i_attempts-- )
2187     {
2188         if( DecoderIsExitRequested( p_dec ) || p_dec->b_error )
2189             break;
2190
2191         p_vout = input_resource_HoldVout( p_owner->p_resource );
2192         if( p_vout )
2193             break;
2194
2195         msleep( DECODER_SPU_VOUT_WAIT_DURATION );
2196     }
2197
2198     if( !p_vout )
2199     {
2200         msg_Warn( p_dec, "no vout found, dropping subpicture" );
2201         return NULL;
2202     }
2203
2204     if( p_owner->p_spu_vout != p_vout )
2205     {
2206         p_owner->i_spu_channel = vout_RegisterSubpictureChannel( p_vout );
2207         p_owner->i_spu_order = 0;
2208         p_owner->p_spu_vout = p_vout;
2209     }
2210
2211     p_subpic = subpicture_New( p_updater );
2212     if( p_subpic )
2213     {
2214         p_subpic->i_channel = p_owner->i_spu_channel;
2215         p_subpic->i_order = p_owner->i_spu_order++;
2216         p_subpic->b_subtitle = true;
2217     }
2218
2219     vlc_object_release( p_vout );
2220
2221     return p_subpic;
2222 }