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