]> git.sesse.net Git - vlc/blob - src/input/decoder.c
Don't leak an object in case of an error.
[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
48 static decoder_t * CreateDecoder( input_thread_t *, es_format_t *, int );
49 static void        DeleteDecoder( decoder_t * );
50
51 static int         DecoderThread( decoder_t * );
52 static int         DecoderDecode( decoder_t * p_dec, block_t *p_block );
53
54 /* Buffers allocation callbacks for the decoders */
55 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
56 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
57
58 static picture_t *vout_new_buffer( decoder_t * );
59 static void vout_del_buffer( decoder_t *, picture_t * );
60 static void vout_link_picture( decoder_t *, picture_t * );
61 static void vout_unlink_picture( decoder_t *, picture_t * );
62
63 static subpicture_t *spu_new_buffer( decoder_t * );
64 static void spu_del_buffer( decoder_t *, subpicture_t * );
65
66 static es_format_t null_es_format;
67
68 struct decoder_owner_sys_t
69 {
70     bool      b_own_thread;
71
72     int64_t         i_preroll_end;
73
74     input_thread_t  *p_input;
75
76     aout_instance_t *p_aout;
77     aout_input_t    *p_aout_input;
78
79     vout_thread_t   *p_vout;
80
81     vout_thread_t   *p_spu_vout;
82     int              i_spu_channel;
83
84     sout_instance_t         *p_sout;
85     sout_packetizer_input_t *p_sout_input;
86
87     /* Some decoders require already packetized data (ie. not truncated) */
88     decoder_t *p_packetizer;
89
90     /* Current format in use by the output */
91     video_format_t video;
92     audio_format_t audio;
93     es_format_t    sout;
94
95     /* fifo */
96     block_fifo_t *p_fifo;
97
98     /* CC */
99     bool b_cc_supported;
100     vlc_mutex_t lock_cc;
101     bool pb_cc_present[4];
102     decoder_t *pp_cc[4];
103 };
104
105 /* */
106 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
107 {
108     msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
109              "VLC probably does not support this sound or video format.",
110              (char*)&codec );
111     intf_UserFatal( p_dec, false, _("No suitable decoder module"), 
112                     _("VLC does not support the audio or video format \"%4.4s\". "
113                       "Unfortunately there is no way for you to fix this."), (char*)&codec );
114 }
115
116 /* decoder_GetInputAttachment:
117  */
118 input_attachment_t *decoder_GetInputAttachment( decoder_t *p_dec,
119                                                 const char *psz_name )
120 {
121     input_attachment_t *p_attachment;
122     if( input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENT, &p_attachment, psz_name ) )
123         return NULL;
124     return p_attachment;
125 }
126 /* decoder_GetInputAttachments:
127  */
128 int decoder_GetInputAttachments( decoder_t *p_dec,
129                                  input_attachment_t ***ppp_attachment,
130                                  int *pi_attachment )
131 {
132     return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
133                           ppp_attachment, pi_attachment );
134 }
135 /* decoder_GetDisplayDate:
136  */
137 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
138 {
139     VLC_UNUSED(p_dec);
140     return i_ts;
141 }
142
143 /**
144  * Spawns a new decoder thread
145  *
146  * \param p_input the input thread
147  * \param p_es the es descriptor
148  * \return the spawned decoder object
149  */
150 decoder_t *input_DecoderNew( input_thread_t *p_input,
151                              es_format_t *fmt, bool b_force_decoder )
152 {
153     decoder_t   *p_dec = NULL;
154     vlc_value_t val;
155
156 #ifndef ENABLE_SOUT
157     (void)b_force_decoder;
158 #else
159     /* If we are in sout mode, search for packetizer module */
160     if( p_input->p->p_sout && !b_force_decoder )
161     {
162         /* Create the decoder configuration structure */
163         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER );
164         if( p_dec == NULL )
165         {
166             msg_Err( p_input, "could not create packetizer" );
167             intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
168                             _("VLC could not open the packetizer module.") );
169             return NULL;
170         }
171     }
172     else
173 #endif
174     {
175         /* Create the decoder configuration structure */
176         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER );
177         if( p_dec == NULL )
178         {
179             msg_Err( p_input, "could not create decoder" );
180             intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
181                             _("VLC could not open the decoder module.") );
182             return NULL;
183         }
184     }
185
186     if( !p_dec->p_module )
187     {
188         DecoderUnsupportedCodec( p_dec, fmt->i_codec );
189
190         DeleteDecoder( p_dec );
191         vlc_object_release( p_dec );
192         return NULL;
193     }
194
195     if( p_input->p->p_sout && p_input->p->input.b_can_pace_control &&
196         !b_force_decoder )
197     {
198         msg_Dbg( p_input, "stream out mode -> no decoder thread" );
199         p_dec->p_owner->b_own_thread = false;
200     }
201     else
202     {
203         var_Get( p_input, "minimize-threads", &val );
204         p_dec->p_owner->b_own_thread = !val.b_bool;
205     }
206
207     if( p_dec->p_owner->b_own_thread )
208     {
209         int i_priority;
210         if( fmt->i_cat == AUDIO_ES )
211             i_priority = VLC_THREAD_PRIORITY_AUDIO;
212         else
213             i_priority = VLC_THREAD_PRIORITY_VIDEO;
214
215         /* Spawn the decoder thread */
216         if( vlc_thread_create( p_dec, "decoder", DecoderThread,
217                                i_priority, false ) )
218         {
219             msg_Err( p_dec, "cannot spawn decoder thread" );
220             module_Unneed( p_dec, p_dec->p_module );
221             DeleteDecoder( p_dec );
222             vlc_object_release( p_dec );
223             return NULL;
224         }
225     }
226
227     return p_dec;
228 }
229
230 /**
231  * Kills a decoder thread and waits until it's finished
232  *
233  * \param p_input the input thread
234  * \param p_es the es descriptor
235  * \return nothing
236  */
237 void input_DecoderDelete( decoder_t *p_dec )
238 {
239     vlc_object_kill( p_dec );
240
241     if( p_dec->p_owner->b_own_thread )
242     {
243         /* Make sure the thread leaves the function by
244          * sending it an empty block. */
245         block_t *p_block = block_New( p_dec, 0 );
246         input_DecoderDecode( p_dec, p_block );
247
248         vlc_thread_join( p_dec );
249
250         /* Don't module_Unneed() here because of the dll loader that wants
251          * close() in the same thread than open()/decode() */
252     }
253     else
254     {
255         /* Flush */
256         input_DecoderDecode( p_dec, NULL );
257
258         module_Unneed( p_dec, p_dec->p_module );
259     }
260
261     /* */
262     if( p_dec->p_owner->b_cc_supported )
263     {
264         int i;
265         for( i = 0; i < 4; i++ )
266             input_DecoderSetCcState( p_dec, false, i );
267     }
268
269     /* Delete decoder configuration */
270     DeleteDecoder( p_dec );
271
272     /* Delete the decoder */
273     vlc_object_release( p_dec );
274 }
275
276 /**
277  * Put a block_t in the decoder's fifo.
278  *
279  * \param p_dec the decoder object
280  * \param p_block the data block
281  */
282 void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
283 {
284     if( p_dec->p_owner->b_own_thread )
285     {
286         if( p_dec->p_owner->p_input->p->b_out_pace_control )
287         {
288             /* FIXME !!!!! */
289             while( !p_dec->b_die && !p_dec->b_error &&
290                    block_FifoCount( p_dec->p_owner->p_fifo ) > 10 )
291             {
292                 msleep( 1000 );
293             }
294         }
295         else if( block_FifoSize( p_dec->p_owner->p_fifo ) > 50000000 /* 50 MB */ )
296         {
297             /* FIXME: ideally we would check the time amount of data
298              * in the fifo instead of its size. */
299             msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
300                       "consumed quickly enough), resetting fifo!" );
301             block_FifoEmpty( p_dec->p_owner->p_fifo );
302         }
303
304         block_FifoPut( p_dec->p_owner->p_fifo, p_block );
305     }
306     else
307     {
308         if( p_dec->b_error || (p_block && p_block->i_buffer <= 0) )
309         {
310             if( p_block ) block_Release( p_block );
311         }
312         else
313         {
314             DecoderDecode( p_dec, p_block );
315         }
316     }
317 }
318
319 void input_DecoderDiscontinuity( decoder_t * p_dec, bool b_flush )
320 {
321     block_t *p_null;
322
323     /* Empty the fifo */
324     if( p_dec->p_owner->b_own_thread && b_flush )
325         block_FifoEmpty( p_dec->p_owner->p_fifo );
326
327     /* Send a special block */
328     p_null = block_New( p_dec, 128 );
329     p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
330     /* FIXME check for p_packetizer or b_packitized from es_format_t of input ? */
331     if( p_dec->p_owner->p_packetizer && b_flush )
332         p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
333     memset( p_null->p_buffer, 0, p_null->i_buffer );
334
335     input_DecoderDecode( p_dec, p_null );
336 }
337
338 bool input_DecoderEmpty( decoder_t * p_dec )
339 {
340     if( p_dec->p_owner->b_own_thread
341      && block_FifoCount( p_dec->p_owner->p_fifo ) > 0 )
342     {
343         return false;
344     }
345     return true;
346 }
347
348 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
349 {
350     int i;
351
352     vlc_mutex_lock( &p_dec->p_owner->lock_cc );
353     for( i = 0; i < 4; i++ )
354         pb_present[i] =  p_dec->p_owner->pb_cc_present[i];
355     vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
356 }
357 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
358 {
359     decoder_owner_sys_t *p_owner = p_dec->p_owner;
360
361     //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
362
363     if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
364         return VLC_EGENERIC;
365
366     if( b_decode )
367     {
368         static const vlc_fourcc_t fcc[4] = {
369             VLC_FOURCC('c', 'c', '1', ' '),
370             VLC_FOURCC('c', 'c', '2', ' '),
371             VLC_FOURCC('c', 'c', '3', ' '),
372             VLC_FOURCC('c', 'c', '4', ' '),
373         };
374         decoder_t *p_cc;
375         es_format_t fmt;
376
377         es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
378         p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER );
379         if( !p_cc )
380         {
381             msg_Err( p_dec, "could not create decoder" );
382             intf_UserFatal( p_dec, false, _("Streaming / Transcoding failed"),
383                             _("VLC could not open the decoder module.") );
384             return VLC_EGENERIC;
385         }
386         else if( !p_cc->p_module )
387         {
388             DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
389             DeleteDecoder( p_cc );
390             vlc_object_release( p_cc );
391             return VLC_EGENERIC;
392         }
393
394         vlc_mutex_lock( &p_owner->lock_cc );
395         p_dec->p_owner->pp_cc[i_channel] = p_cc;
396         vlc_mutex_unlock( &p_owner->lock_cc );
397     }
398     else
399     {
400         decoder_t *p_cc;
401
402         vlc_mutex_lock( &p_owner->lock_cc );
403         p_cc = p_dec->p_owner->pp_cc[i_channel];
404         p_dec->p_owner->pp_cc[i_channel] = NULL;
405         vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
406
407         if( p_cc )
408         {
409             vlc_object_kill( p_cc );
410             module_Unneed( p_cc, p_cc->p_module );
411             DeleteDecoder( p_cc );
412             vlc_object_release( p_cc );
413         }
414     }
415     return VLC_SUCCESS;
416 }
417 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
418 {
419     decoder_owner_sys_t *p_owner = p_dec->p_owner;
420
421     *pb_decode = false;
422     if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
423         return VLC_EGENERIC;
424
425     vlc_mutex_lock( &p_owner->lock_cc );
426     *pb_decode = p_dec->p_owner->pp_cc[i_channel] != NULL;
427     vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
428     return VLC_EGENERIC;
429 }
430
431 /**
432  * Create a decoder object
433  *
434  * \param p_input the input thread
435  * \param p_es the es descriptor
436  * \param i_object_type Object type as define in include/vlc_objects.h
437  * \return the decoder object
438  */
439 static decoder_t * CreateDecoder( input_thread_t *p_input,
440                                   es_format_t *fmt, int i_object_type )
441 {
442     decoder_t *p_dec;
443     decoder_owner_sys_t *p_owner;
444     int i;
445
446     p_dec = vlc_object_create( p_input, i_object_type );
447     if( p_dec == NULL )
448         return NULL;
449
450     p_dec->pf_decode_audio = 0;
451     p_dec->pf_decode_video = 0;
452     p_dec->pf_decode_sub = 0;
453     p_dec->pf_get_cc = 0;
454     p_dec->pf_packetize = 0;
455
456     /* Initialize the decoder fifo */
457     p_dec->p_module = NULL;
458
459     memset( &null_es_format, 0, sizeof(es_format_t) );
460     es_format_Copy( &p_dec->fmt_in, fmt );
461     es_format_Copy( &p_dec->fmt_out, &null_es_format );
462
463     /* Allocate our private structure for the decoder */
464     p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
465     if( p_dec->p_owner == NULL )
466     {
467         vlc_object_release( p_dec );
468         return NULL;
469     }
470     p_dec->p_owner->b_own_thread = true;
471     p_dec->p_owner->i_preroll_end = -1;
472     p_dec->p_owner->p_input = p_input;
473     p_dec->p_owner->p_aout = NULL;
474     p_dec->p_owner->p_aout_input = NULL;
475     p_dec->p_owner->p_vout = NULL;
476     p_dec->p_owner->p_spu_vout = NULL;
477     p_dec->p_owner->i_spu_channel = 0;
478     p_dec->p_owner->p_sout = p_input->p->p_sout;
479     p_dec->p_owner->p_sout_input = NULL;
480     p_dec->p_owner->p_packetizer = NULL;
481
482     /* decoder fifo */
483     if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
484     {
485         free( p_dec->p_owner );
486         vlc_object_release( p_dec );
487         return NULL;
488     }
489
490     /* Set buffers allocation callbacks for the decoders */
491     p_dec->pf_aout_buffer_new = aout_new_buffer;
492     p_dec->pf_aout_buffer_del = aout_del_buffer;
493     p_dec->pf_vout_buffer_new = vout_new_buffer;
494     p_dec->pf_vout_buffer_del = vout_del_buffer;
495     p_dec->pf_picture_link    = vout_link_picture;
496     p_dec->pf_picture_unlink  = vout_unlink_picture;
497     p_dec->pf_spu_buffer_new  = spu_new_buffer;
498     p_dec->pf_spu_buffer_del  = spu_del_buffer;
499
500     vlc_object_attach( p_dec, p_input );
501
502     /* Find a suitable decoder/packetizer module */
503     if( i_object_type == VLC_OBJECT_DECODER )
504         p_dec->p_module = module_Need( p_dec, "decoder", "$codec", 0 );
505     else
506         p_dec->p_module = module_Need( p_dec, "packetizer", "$packetizer", 0 );
507
508     /* Check if decoder requires already packetized data */
509     if( i_object_type == VLC_OBJECT_DECODER &&
510         p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
511     {
512         p_dec->p_owner->p_packetizer =
513             vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
514         if( p_dec->p_owner->p_packetizer )
515         {
516             es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
517                             &p_dec->fmt_in );
518
519             es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
520                             &null_es_format );
521
522             vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
523
524             p_dec->p_owner->p_packetizer->p_module =
525                 module_Need( p_dec->p_owner->p_packetizer,
526                              "packetizer", "$packetizer", 0 );
527
528             if( !p_dec->p_owner->p_packetizer->p_module )
529             {
530                 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
531                 vlc_object_detach( p_dec->p_owner->p_packetizer );
532                 vlc_object_release( p_dec->p_owner->p_packetizer );
533             }
534         }
535     }
536
537     /* Copy ourself the input replay gain */
538     if( fmt->i_cat == AUDIO_ES )
539     {
540         for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
541         {
542             if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
543             {
544                 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
545                 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
546             }
547             if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
548             {
549                 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
550                 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
551             }
552         }
553     }
554     /* */
555     p_owner->b_cc_supported = false;
556     if( i_object_type == VLC_OBJECT_DECODER )
557     {
558         if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
559             p_owner->b_cc_supported = true;
560         if( p_dec->pf_get_cc )
561             p_owner->b_cc_supported = true;
562     }
563
564     vlc_mutex_init( &p_owner->lock_cc );
565     for( i = 0; i < 4; i++ )
566     {
567         p_owner->pb_cc_present[i] = false;
568         p_owner->pp_cc[i] = NULL;
569     }
570     return p_dec;
571 }
572
573 /**
574  * The decoding main loop
575  *
576  * \param p_dec the decoder
577  * \return 0
578  */
579 static int DecoderThread( decoder_t * p_dec )
580 {
581     block_t *p_block;
582
583     /* The decoder's main loop */
584     while( !p_dec->b_die && !p_dec->b_error )
585     {
586         if( ( p_block = block_FifoGet( p_dec->p_owner->p_fifo ) ) == NULL )
587         {
588             p_dec->b_error = 1;
589             break;
590         }
591         if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
592         {
593             break;
594         }
595     }
596
597     while( !p_dec->b_die )
598     {
599         /* Trash all received PES packets */
600         p_block = block_FifoGet( p_dec->p_owner->p_fifo );
601         if( p_block ) block_Release( p_block );
602     }
603
604     /* We do it here because of the dll loader that wants close() in the
605      * same thread than open()/decode() */
606     module_Unneed( p_dec, p_dec->p_module );
607
608     return 0;
609 }
610
611 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
612 {
613     if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
614         *pi_preroll = INT64_MAX;
615     else if( p->i_pts > 0 )
616         *pi_preroll = __MIN( *pi_preroll, p->i_pts );
617     else if( p->i_dts > 0 )
618         *pi_preroll = __MIN( *pi_preroll, p->i_dts );
619 }
620 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
621 {
622     input_thread_t *p_input = p_dec->p_owner->p_input;
623     const int i_rate = p_block->i_rate;
624     aout_buffer_t *p_aout_buf;
625
626     while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
627     {
628         vlc_mutex_lock( &p_input->p->counters.counters_lock );
629         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio, 1, NULL );
630         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
631
632         if( p_aout_buf->start_date < p_dec->p_owner->i_preroll_end )
633         {
634             aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
635                                   p_dec->p_owner->p_aout_input, p_aout_buf );
636             continue;
637         }
638
639         if( p_dec->p_owner->i_preroll_end > 0 )
640         {
641             /* FIXME TODO flush audio output (don't know how to do that) */
642             msg_Dbg( p_dec, "End of audio preroll" );
643             p_dec->p_owner->i_preroll_end = -1;
644         }
645         aout_DecPlay( p_dec->p_owner->p_aout,
646                       p_dec->p_owner->p_aout_input,
647                       p_aout_buf, i_rate );
648     }
649 }
650 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
651 {
652     block_t *p_cc;
653     bool pb_present[4];
654     int i;
655     int i_cc_decoder;
656
657     assert( p_dec_cc->pf_get_cc != NULL );
658
659     /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
660     if( !p_dec->p_owner->b_cc_supported )
661         return;
662
663     p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
664     if( !p_cc )
665         return;
666
667     vlc_mutex_lock( &p_dec->p_owner->lock_cc );
668     for( i = 0, i_cc_decoder = 0; i < 4; i++ )
669     {
670         p_dec->p_owner->pb_cc_present[i] |= pb_present[i];
671         if( p_dec->p_owner->pp_cc[i] )
672             i_cc_decoder++;
673     }
674
675     for( i = 0; i < 4; i++ )
676     {
677         if( !p_dec->p_owner->pp_cc[i] )
678             continue;
679
680         if( i_cc_decoder > 1 )
681             DecoderDecode( p_dec->p_owner->pp_cc[i], block_Duplicate( p_cc ) );
682         else
683             DecoderDecode( p_dec->p_owner->pp_cc[i], p_cc );
684         i_cc_decoder--;
685     }
686     vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
687 }
688 static void VoutDisplayedPicture( vout_thread_t *p_vout, picture_t *p_pic )
689 {
690     vlc_mutex_lock( &p_vout->picture_lock );
691
692     if( p_pic->i_status == READY_PICTURE )
693     {
694         /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
695         p_pic->date = 1;
696     }
697     else if( p_pic->i_refcount > 0 )
698     {
699         p_pic->i_status = DISPLAYED_PICTURE;
700     }
701     else
702     {
703         p_pic->i_status = DESTROYED_PICTURE;
704         p_vout->i_heap_size--;
705     }
706
707     vlc_mutex_unlock( &p_vout->picture_lock );
708 }
709 static void VoutFlushPicture( vout_thread_t *p_vout )
710 {
711     int i;
712     vlc_mutex_lock( &p_vout->picture_lock );
713     for( i = 0; i < p_vout->render.i_pictures; i++ )
714     {
715         picture_t *p_pic = p_vout->render.pp_picture[i];
716
717         if( p_pic->i_status == READY_PICTURE ||
718             p_pic->i_status == DISPLAYED_PICTURE )
719         {
720             /* We cannot change picture status if it is in READY_PICTURE state,
721              * Just make sure they won't be displayed */
722             p_pic->date = 1;
723         }
724     }
725     vlc_mutex_unlock( &p_vout->picture_lock );
726 }
727
728
729 static void optimize_video_pts( decoder_t *p_dec )
730 {
731     picture_t * oldest_pict = NULL;
732     picture_t * youngest_pict = NULL;
733     int i;
734
735     input_thread_t * p_input = p_dec->p_owner->p_input;
736     vout_thread_t * p_vout = p_dec->p_owner->p_vout;
737     input_thread_private_t * p_priv = p_input->p;
738
739     /* Enable with --auto-adjust-pts-delay */
740     if( !p_priv->pts_adjust.auto_adjust ) return;
741
742     for( i = 0; i < I_RENDERPICTURES; i++ )
743     {
744         picture_t * pic = PP_RENDERPICTURE[i];
745         if( pic->i_status != READY_PICTURE )
746             continue;
747
748         if( !oldest_pict || pic->date < oldest_pict->date )
749             oldest_pict = pic;
750         if( !youngest_pict || pic->date > youngest_pict->date )
751             youngest_pict = pic;
752     }
753
754     if( !youngest_pict || !oldest_pict )
755         return;
756
757     /* Try to find if we can reduce the pts
758      * This first draft is way to simple, and we can't say if the
759      * algo will converge. It's also full of constants.
760      * But this simple algo allows to reduce the latency
761      * to the minimum.
762      * The whole point of this, is to bypass the pts_delay set
763      * by the access but also the delay arbitraly set by
764      * the remote server.
765      * Actually the remote server's muxer may set up a
766      * pts<->dts delay in the muxed stream. That is
767      * why we may end up in having a negative pts_delay,
768      * to compensate that artificial delay. */
769     mtime_t buffer_size = youngest_pict->date - oldest_pict->date;
770     int64_t pts_slide = 0;
771     if( buffer_size < 10000 )
772     {
773         if( p_priv->pts_adjust.i_num_faulty > 10 )
774         {
775             pts_slide = __MAX(p_input->i_pts_delay *3 / 2, 10000);
776             p_priv->pts_adjust.i_num_faulty = 0;
777         }
778         if( p_priv->pts_adjust.to_high )
779         {
780             p_priv->pts_adjust.to_high = !p_priv->pts_adjust.to_high;
781             p_priv->pts_adjust.i_num_faulty = 0;
782         }
783         p_priv->pts_adjust.i_num_faulty++;
784     }
785     else if( buffer_size > 100000 )
786     {
787         if( p_priv->pts_adjust.i_num_faulty > 25 )
788         {
789             pts_slide = -buffer_size/2;
790             p_priv->pts_adjust.i_num_faulty = 0;
791         }
792         if( p_priv->pts_adjust.to_high )
793         {
794             p_priv->pts_adjust.to_high = !p_priv->pts_adjust.to_high;
795             p_priv->pts_adjust.i_num_faulty = 0;
796         }
797         p_priv->pts_adjust.i_num_faulty++;
798     }
799     if( pts_slide )
800     {
801         mtime_t origi_delay = p_input->i_pts_delay;
802
803         p_input->i_pts_delay += pts_slide;
804
805         /* Don't play with the pts delay for more than -2<->3sec */
806         if( p_input->i_pts_delay < -2000000 )
807             p_input->i_pts_delay = -2000000;
808         else if( p_input->i_pts_delay > 3000000 )
809             p_input->i_pts_delay = 3000000;
810         pts_slide = p_input->i_pts_delay - origi_delay;
811
812         msg_Dbg( p_input, "Sliding the pts by %dms pts delay at %dms picture buffer was %dms",
813             (int)pts_slide/1000, (int)p_input->i_pts_delay/1000, (int)buffer_size/1000);
814
815         vlc_mutex_lock( &p_vout->picture_lock );
816         /* Slide all the picture */
817         for( i = 0; i < I_RENDERPICTURES; i++ )
818             PP_RENDERPICTURE[i]->date += pts_slide;
819         /* FIXME: slide aout/spu */
820         vlc_mutex_unlock( &p_vout->picture_lock );
821
822     }
823 }
824
825 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
826 {
827     input_thread_t *p_input = p_dec->p_owner->p_input;
828     picture_t *p_pic;
829
830     while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
831     {
832         vlc_mutex_lock( &p_input->p->counters.counters_lock );
833         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video, 1, NULL );
834         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
835
836         if( p_pic->date < p_dec->p_owner->i_preroll_end )
837         {
838             VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
839             continue;
840         }
841
842         if( p_dec->p_owner->i_preroll_end > 0 )
843         {
844             msg_Dbg( p_dec, "End of video preroll" );
845             if( p_dec->p_owner->p_vout )
846                 VoutFlushPicture( p_dec->p_owner->p_vout );
847             /* */
848             p_dec->p_owner->i_preroll_end = -1;
849         }
850
851         if( ( !p_dec->p_owner->p_packetizer || !p_dec->p_owner->p_packetizer->pf_get_cc ) && p_dec->pf_get_cc )
852             DecoderGetCc( p_dec, p_dec );
853
854         vout_DatePicture( p_dec->p_owner->p_vout, p_pic,
855                           p_pic->date );
856
857         optimize_video_pts( p_dec );
858
859         vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
860     }
861 }
862
863 /**
864  * Decode a block
865  *
866  * \param p_dec the decoder object
867  * \param p_block the block to decode
868  * \return VLC_SUCCESS or an error code
869  */
870 static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
871 {
872     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
873     const int i_rate = p_block ? p_block->i_rate : INPUT_RATE_DEFAULT;
874
875     if( p_block && p_block->i_buffer <= 0 )
876     {
877         block_Release( p_block );
878         return VLC_SUCCESS;
879     }
880
881 #ifdef ENABLE_SOUT
882     if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
883     {
884         block_t *p_sout_block;
885
886         while( ( p_sout_block =
887                      p_dec->pf_packetize( p_dec, p_block ? &p_block : 0 ) ) )
888         {
889             if( !p_dec->p_owner->p_sout_input )
890             {
891                 es_format_Copy( &p_dec->p_owner->sout, &p_dec->fmt_out );
892
893                 p_dec->p_owner->sout.i_group = p_dec->fmt_in.i_group;
894                 p_dec->p_owner->sout.i_id = p_dec->fmt_in.i_id;
895                 if( p_dec->fmt_in.psz_language )
896                 {
897                     if( p_dec->p_owner->sout.psz_language )
898                         free( p_dec->p_owner->sout.psz_language );
899                     p_dec->p_owner->sout.psz_language =
900                         strdup( p_dec->fmt_in.psz_language );
901                 }
902
903                 p_dec->p_owner->p_sout_input =
904                     sout_InputNew( p_dec->p_owner->p_sout,
905                                    &p_dec->p_owner->sout );
906
907                 if( p_dec->p_owner->p_sout_input == NULL )
908                 {
909                     msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
910                              (char *)&p_dec->p_owner->sout.i_codec );
911                     p_dec->b_error = true;
912
913                     while( p_sout_block )
914                     {
915                         block_t *p_next = p_sout_block->p_next;
916                         block_Release( p_sout_block );
917                         p_sout_block = p_next;
918                     }
919                     break;
920                 }
921             }
922
923             while( p_sout_block )
924             {
925                 block_t *p_next = p_sout_block->p_next;
926
927                 p_sout_block->p_next = NULL;
928                 p_sout_block->i_rate = i_rate;
929
930                 sout_InputSendBuffer( p_dec->p_owner->p_sout_input,
931                                       p_sout_block );
932
933                 p_sout_block = p_next;
934             }
935
936             /* For now it's enough, as only sout impact on this flag */
937             if( p_dec->p_owner->p_sout->i_out_pace_nocontrol > 0 &&
938                 p_dec->p_owner->p_input->p->b_out_pace_control )
939             {
940                 msg_Dbg( p_dec, "switching to sync mode" );
941                 p_dec->p_owner->p_input->p->b_out_pace_control = false;
942             }
943             else if( p_dec->p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
944                      !p_dec->p_owner->p_input->p->b_out_pace_control )
945             {
946                 msg_Dbg( p_dec, "switching to async mode" );
947                 p_dec->p_owner->p_input->p->b_out_pace_control = true;
948             }
949         }
950     }
951     else
952 #endif
953     if( p_dec->fmt_in.i_cat == AUDIO_ES )
954     {
955         if( p_block )
956             DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
957
958         if( p_dec->p_owner->p_packetizer )
959         {
960             block_t *p_packetized_block;
961             decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
962
963             while( (p_packetized_block =
964                     p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
965             {
966                 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
967                 {
968                     es_format_Clean( &p_dec->fmt_in );
969                     es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
970                 }
971
972                 while( p_packetized_block )
973                 {
974                     block_t *p_next = p_packetized_block->p_next;
975                     p_packetized_block->p_next = NULL;
976                     p_packetized_block->i_rate = i_rate;
977
978                     DecoderDecodeAudio( p_dec, p_packetized_block );
979
980                     p_packetized_block = p_next;
981                 }
982             }
983         }
984         else if( p_block )
985         {
986             DecoderDecodeAudio( p_dec, p_block );
987         }
988     }
989     else if( p_dec->fmt_in.i_cat == VIDEO_ES )
990     {
991         if( p_block )
992             DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
993
994         if( p_dec->p_owner->p_packetizer )
995         {
996             block_t *p_packetized_block;
997             decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
998
999             while( (p_packetized_block =
1000                     p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1001             {
1002                 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1003                 {
1004                     es_format_Clean( &p_dec->fmt_in );
1005                     es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1006                 }
1007                 if( p_packetizer->pf_get_cc )
1008                     DecoderGetCc( p_dec, p_packetizer );
1009
1010                 while( p_packetized_block )
1011                 {
1012                     block_t *p_next = p_packetized_block->p_next;
1013                     p_packetized_block->p_next = NULL;
1014                     p_packetized_block->i_rate = i_rate;
1015
1016                     DecoderDecodeVideo( p_dec, p_packetized_block );
1017
1018                     p_packetized_block = p_next;
1019                 }
1020             }
1021         }
1022         else if( p_block )
1023         {
1024             DecoderDecodeVideo( p_dec, p_block );
1025         }
1026     }
1027     else if( p_dec->fmt_in.i_cat == SPU_ES )
1028     {
1029         input_thread_t *p_input = p_dec->p_owner->p_input;
1030         vout_thread_t *p_vout;
1031         subpicture_t *p_spu;
1032
1033         if( p_block )
1034             DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
1035
1036         while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1037         {
1038             vlc_mutex_lock( &p_input->p->counters.counters_lock );
1039             stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1040             vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1041
1042             p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1043             if( p_vout && p_sys->p_spu_vout == p_vout )
1044             {
1045                 /* Prerool does not work very well with subtitle */
1046                 if( p_spu->i_start < p_dec->p_owner->i_preroll_end &&
1047                     ( p_spu->i_stop <= 0 || p_spu->i_stop < p_dec->p_owner->i_preroll_end ) )
1048                     spu_DestroySubpicture( p_vout->p_spu, p_spu );
1049                 else
1050                     spu_DisplaySubpicture( p_vout->p_spu, p_spu );
1051             }
1052             else
1053             {
1054                 msg_Warn( p_dec, "no vout found, leaking subpicture" );
1055             }
1056             if( p_vout )
1057                 vlc_object_release( p_vout );
1058         }
1059     }
1060     else
1061     {
1062         msg_Err( p_dec, "unknown ES format" );
1063         p_dec->b_error = 1;
1064     }
1065
1066     return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
1067 }
1068
1069 /**
1070  * Destroys a decoder object
1071  *
1072  * \param p_dec the decoder object
1073  * \return nothing
1074  */
1075 static void DeleteDecoder( decoder_t * p_dec )
1076 {
1077     msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
1078              (char*)&p_dec->fmt_in.i_codec,
1079              (unsigned)block_FifoCount( p_dec->p_owner->p_fifo ) );
1080
1081     /* Free all packets still in the decoder fifo. */
1082     block_FifoEmpty( p_dec->p_owner->p_fifo );
1083     block_FifoRelease( p_dec->p_owner->p_fifo );
1084
1085     /* Cleanup */
1086     if( p_dec->p_owner->p_aout_input )
1087         aout_DecDelete( p_dec->p_owner->p_aout, p_dec->p_owner->p_aout_input );
1088     if( p_dec->p_owner->p_aout )
1089     {
1090         vlc_object_release( p_dec->p_owner->p_aout );
1091         p_dec->p_owner->p_aout = NULL;
1092     }
1093     if( p_dec->p_owner->p_vout )
1094     {
1095         int i_pic;
1096
1097 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
1098         /* Hack to make sure all the the pictures are freed by the decoder */
1099         for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1100              i_pic++ )
1101         {
1102             if( p_pic->i_status == RESERVED_PICTURE )
1103                 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
1104             if( p_pic->i_refcount > 0 )
1105                 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1106         }
1107 #undef p_pic
1108
1109         /* We are about to die. Reattach video output to p_vlc. */
1110         vout_Request( p_dec, p_dec->p_owner->p_vout, 0 );
1111     }
1112
1113 #ifdef ENABLE_SOUT
1114     if( p_dec->p_owner->p_sout_input )
1115     {
1116         sout_InputDelete( p_dec->p_owner->p_sout_input );
1117         es_format_Clean( &p_dec->p_owner->sout );
1118     }
1119 #endif
1120
1121     if( p_dec->fmt_in.i_cat == SPU_ES )
1122     {
1123         vout_thread_t *p_vout;
1124
1125         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1126         if( p_vout )
1127         {
1128             spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1129                          p_dec->p_owner->i_spu_channel );
1130             vlc_object_release( p_vout );
1131         }
1132     }
1133
1134     es_format_Clean( &p_dec->fmt_in );
1135     es_format_Clean( &p_dec->fmt_out );
1136
1137     if( p_dec->p_owner->p_packetizer )
1138     {
1139         module_Unneed( p_dec->p_owner->p_packetizer,
1140                        p_dec->p_owner->p_packetizer->p_module );
1141         es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
1142         es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_out );
1143         vlc_object_detach( p_dec->p_owner->p_packetizer );
1144         vlc_object_release( p_dec->p_owner->p_packetizer );
1145     }
1146
1147     vlc_mutex_destroy( &p_dec->p_owner->lock_cc );
1148
1149     vlc_object_detach( p_dec );
1150
1151     free( p_dec->p_owner );
1152 }
1153
1154 /*****************************************************************************
1155  * Buffers allocation callbacks for the decoders
1156  *****************************************************************************/
1157 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
1158 {
1159     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1160     aout_buffer_t *p_buffer;
1161
1162     if( p_sys->p_aout_input != NULL &&
1163         ( p_dec->fmt_out.audio.i_rate != p_sys->audio.i_rate ||
1164           p_dec->fmt_out.audio.i_original_channels !=
1165               p_sys->audio.i_original_channels ||
1166           p_dec->fmt_out.audio.i_bytes_per_frame !=
1167               p_sys->audio.i_bytes_per_frame ) )
1168     {
1169         /* Parameters changed, restart the aout */
1170         aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
1171         p_sys->p_aout_input = NULL;
1172     }
1173
1174     if( p_sys->p_aout_input == NULL )
1175     {
1176         audio_sample_format_t format;
1177         int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
1178
1179         p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
1180         p_sys->audio = p_dec->fmt_out.audio;
1181
1182         memcpy( &format, &p_sys->audio, sizeof( audio_sample_format_t ) );
1183         if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
1184                                     == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
1185         {
1186             if ( i_force_dolby == 1 )
1187             {
1188                 format.i_original_channels = format.i_original_channels |
1189                                              AOUT_CHAN_DOLBYSTEREO;
1190             }
1191             else /* i_force_dolby == 2 */
1192             {
1193                 format.i_original_channels = format.i_original_channels &
1194                                              ~AOUT_CHAN_DOLBYSTEREO;
1195             }
1196         }
1197
1198         p_sys->p_aout_input =
1199             aout_DecNew( p_dec, &p_sys->p_aout, &format, &p_dec->fmt_out.audio_replay_gain );
1200         if( p_sys->p_aout_input == NULL )
1201         {
1202             msg_Err( p_dec, "failed to create audio output" );
1203             p_dec->b_error = true;
1204             return NULL;
1205         }
1206         p_dec->fmt_out.audio.i_bytes_per_frame =
1207             p_sys->audio.i_bytes_per_frame;
1208     }
1209
1210     p_buffer = aout_DecNewBuffer( p_sys->p_aout_input, i_samples );
1211
1212     return p_buffer;
1213 }
1214
1215 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1216 {
1217     aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
1218                           p_dec->p_owner->p_aout_input, p_buffer );
1219 }
1220
1221 static picture_t *vout_new_buffer( decoder_t *p_dec )
1222 {
1223     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1224     picture_t *p_pic;
1225
1226     if( p_sys->p_vout == NULL ||
1227         p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
1228         p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
1229         p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
1230         p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
1231     {
1232         if( !p_dec->fmt_out.video.i_width ||
1233             !p_dec->fmt_out.video.i_height )
1234         {
1235             /* Can't create a new vout without display size */
1236             return NULL;
1237         }
1238
1239         if( !p_dec->fmt_out.video.i_visible_width ||
1240             !p_dec->fmt_out.video.i_visible_height )
1241         {
1242             if( p_dec->fmt_in.video.i_visible_width &&
1243                 p_dec->fmt_in.video.i_visible_height )
1244             {
1245                 p_dec->fmt_out.video.i_visible_width =
1246                     p_dec->fmt_in.video.i_visible_width;
1247                 p_dec->fmt_out.video.i_visible_height =
1248                     p_dec->fmt_in.video.i_visible_height;
1249             }
1250             else
1251             {
1252                 p_dec->fmt_out.video.i_visible_width =
1253                     p_dec->fmt_out.video.i_width;
1254                 p_dec->fmt_out.video.i_visible_height =
1255                     p_dec->fmt_out.video.i_height;
1256             }
1257         }
1258
1259         if( p_dec->fmt_out.video.i_visible_height == 1088 &&
1260             var_CreateGetBool( p_dec, "hdtv-fix" ) )
1261         {
1262             p_dec->fmt_out.video.i_visible_height = 1080;
1263             p_dec->fmt_out.video.i_sar_num *= 135;
1264             p_dec->fmt_out.video.i_sar_den *= 136;
1265             msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
1266         }
1267
1268         if( !p_dec->fmt_out.video.i_sar_num ||
1269             !p_dec->fmt_out.video.i_sar_den )
1270         {
1271             p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
1272               p_dec->fmt_out.video.i_visible_height;
1273
1274             p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
1275               p_dec->fmt_out.video.i_visible_width;
1276         }
1277
1278         vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
1279                      &p_dec->fmt_out.video.i_sar_den,
1280                      p_dec->fmt_out.video.i_sar_num,
1281                      p_dec->fmt_out.video.i_sar_den, 50000 );
1282
1283         p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
1284         p_sys->video = p_dec->fmt_out.video;
1285
1286         p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
1287                                       &p_dec->fmt_out.video );
1288         if( p_sys->p_vout == NULL )
1289         {
1290             msg_Err( p_dec, "failed to create video output" );
1291             p_dec->b_error = true;
1292             return NULL;
1293         }
1294
1295         if( p_sys->video.i_rmask )
1296             p_sys->p_vout->render.i_rmask = p_sys->video.i_rmask;
1297         if( p_sys->video.i_gmask )
1298             p_sys->p_vout->render.i_gmask = p_sys->video.i_gmask;
1299         if( p_sys->video.i_bmask )
1300             p_sys->p_vout->render.i_bmask = p_sys->video.i_bmask;
1301     }
1302
1303     /* Get a new picture */
1304     while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
1305     {
1306         int i_pic, i_ready_pic = 0;
1307
1308         if( p_dec->b_die || p_dec->b_error )
1309         {
1310             return NULL;
1311         }
1312
1313 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
1314         /* Check the decoder doesn't leak pictures */
1315         for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1316              i_pic++ )
1317         {
1318             if( p_pic->i_status == READY_PICTURE )
1319             {
1320                 if( i_ready_pic++ > 0 ) break;
1321                 else continue;
1322             }
1323
1324             if( p_pic->i_status != DISPLAYED_PICTURE &&
1325                 p_pic->i_status != RESERVED_PICTURE &&
1326                 p_pic->i_status != READY_PICTURE ) break;
1327
1328             if( !p_pic->i_refcount && p_pic->i_status != RESERVED_PICTURE )
1329                 break;
1330         }
1331         if( i_pic == p_dec->p_owner->p_vout->render.i_pictures )
1332         {
1333             msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
1334
1335             /* Just free all the pictures */
1336             for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1337                  i_pic++ )
1338             {
1339                 if( p_pic->i_status == RESERVED_PICTURE )
1340                     vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
1341                 if( p_pic->i_refcount > 0 )
1342                 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1343             }
1344         }
1345 #undef p_pic
1346
1347         msleep( VOUT_OUTMEM_SLEEP );
1348     }
1349
1350     return p_pic;
1351 }
1352
1353 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
1354 {
1355     VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
1356 }
1357
1358 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
1359 {
1360     vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
1361 }
1362
1363 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
1364 {
1365     vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1366 }
1367
1368 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
1369 {
1370     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1371     vout_thread_t *p_vout = NULL;
1372     subpicture_t *p_subpic;
1373     int i_attempts = 30;
1374
1375     while( i_attempts-- )
1376     {
1377         if( p_dec->b_die || p_dec->b_error ) break;
1378
1379         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1380         if( p_vout ) break;
1381
1382         msleep( VOUT_DISPLAY_DELAY );
1383     }
1384
1385     if( !p_vout )
1386     {
1387         msg_Warn( p_dec, "no vout found, dropping subpicture" );
1388         return NULL;
1389     }
1390
1391     if( p_sys->p_spu_vout != p_vout )
1392     {
1393         spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
1394                      &p_sys->i_spu_channel );
1395         p_sys->p_spu_vout = p_vout;
1396     }
1397
1398     p_subpic = spu_CreateSubpicture( p_vout->p_spu );
1399     if( p_subpic )
1400     {
1401         p_subpic->i_channel = p_sys->i_spu_channel;
1402     }
1403
1404     vlc_object_release( p_vout );
1405
1406     return p_subpic;
1407 }
1408
1409 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1410 {
1411     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1412     vout_thread_t *p_vout = NULL;
1413
1414     p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1415     if( !p_vout || p_sys->p_spu_vout != p_vout )
1416     {
1417         if( p_vout )
1418             vlc_object_release( p_vout );
1419         msg_Warn( p_dec, "no vout found, leaking subpicture" );
1420         return;
1421     }
1422
1423     spu_DestroySubpicture( p_vout->p_spu, p_subpic );
1424
1425     vlc_object_release( p_vout );
1426 }
1427