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