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