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