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