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