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