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