]> git.sesse.net Git - vlc/blob - src/input/decoder.c
* src/input/decoder.c: don't let the decoder/packetizer fifo grow too much because...
[vlc] / src / input / decoder.c
1 /*****************************************************************************
2  * decoder.c: Functions for the management of decoders
3  *****************************************************************************
4  * Copyright (C) 1999-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Gildas Bazin <gbazin@netcourrier.com>
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <stdlib.h>
30 #include <vlc/vlc.h>
31
32 #include <vlc/decoder.h>
33 #include <vlc/vout.h>
34 #include <vlc/input.h>
35
36 #include "stream_output.h"
37 #include "input_internal.h"
38
39 static decoder_t * CreateDecoder( input_thread_t *, es_format_t *, int );
40 static void        DeleteDecoder( decoder_t * );
41
42 static int         DecoderThread( decoder_t * );
43 static int         DecoderDecode( decoder_t * p_dec, block_t *p_block );
44
45 /* Buffers allocation callbacks for the decoders */
46 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
47 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
48
49 static picture_t *vout_new_buffer( decoder_t * );
50 static void vout_del_buffer( decoder_t *, picture_t * );
51 static void vout_link_picture( decoder_t *, picture_t * );
52 static void vout_unlink_picture( decoder_t *, picture_t * );
53
54 static es_format_t null_es_format = {0};
55
56 struct decoder_owner_sys_t
57 {
58     vlc_bool_t      b_own_thread;
59
60     input_thread_t  *p_input;
61
62     aout_instance_t *p_aout;
63     aout_input_t    *p_aout_input;
64
65     vout_thread_t   *p_vout;
66
67     sout_instance_t         *p_sout;
68     sout_packetizer_input_t *p_sout_input;
69
70     /* Some decoders require already packetized data (ie. not truncated) */
71     decoder_t *p_packetizer;
72
73     /* Current format in use by the output */
74     video_format_t video;
75     audio_format_t audio;
76     es_format_t    sout;
77
78     /* fifo */
79     block_fifo_t *p_fifo;
80 };
81
82
83 /**
84  * Spawns a new decoder thread
85  *
86  * \param p_input the input thread
87  * \param p_es the es descriptor
88  * \return the spawned decoder object
89  */
90 decoder_t *input_DecoderNew( input_thread_t *p_input,
91                              es_format_t *fmt, vlc_bool_t b_force_decoder )
92 {
93     decoder_t   *p_dec = NULL;
94     vlc_value_t val;
95
96     /* If we are in sout mode, search for packetizer module */
97     if( p_input->p_sout && !b_force_decoder )
98     {
99         /* Create the decoder configuration structure */
100         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER );
101         if( p_dec == NULL )
102         {
103             msg_Err( p_input, "could not create packetizer" );
104             return NULL;
105         }
106     }
107     else
108     {
109         /* Create the decoder configuration structure */
110         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER );
111         if( p_dec == NULL )
112         {
113             msg_Err( p_input, "could not create decoder" );
114             return NULL;
115         }
116     }
117
118     if( !p_dec->p_module )
119     {
120         msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
121                  "VLC probably does not support this sound or video format.",
122                  (char*)&p_dec->fmt_in.i_codec );
123
124         DeleteDecoder( p_dec );
125         vlc_object_destroy( p_dec );
126         return NULL;
127     }
128
129     if( p_input->p_sout && p_input->input.b_can_pace_control &&
130         !b_force_decoder )
131     {
132         msg_Dbg( p_input, "stream out mode -> no decoder thread" );
133         p_dec->p_owner->b_own_thread = VLC_FALSE;
134     }
135     else
136     {
137         var_Get( p_input, "minimize-threads", &val );
138         p_dec->p_owner->b_own_thread = !val.b_bool;
139     }
140
141     if( p_dec->p_owner->b_own_thread )
142     {
143         int i_priority;
144         if( fmt->i_cat == AUDIO_ES )
145             i_priority = VLC_THREAD_PRIORITY_AUDIO;
146         else
147             i_priority = VLC_THREAD_PRIORITY_VIDEO;
148
149         /* Spawn the decoder thread */
150         if( vlc_thread_create( p_dec, "decoder", DecoderThread,
151                                i_priority, VLC_FALSE ) )
152         {
153             msg_Err( p_dec, "cannot spawn decoder thread \"%s\"",
154                              p_dec->p_module->psz_object_name );
155             module_Unneed( p_dec, p_dec->p_module );
156             DeleteDecoder( p_dec );
157             vlc_object_destroy( p_dec );
158             return NULL;
159         }
160     }
161
162     return p_dec;
163 }
164
165 /**
166  * Kills a decoder thread and waits until it's finished
167  *
168  * \param p_input the input thread
169  * \param p_es the es descriptor
170  * \return nothing
171  */
172 void input_DecoderDelete( decoder_t *p_dec )
173 {
174     p_dec->b_die = VLC_TRUE;
175
176     if( p_dec->p_owner->b_own_thread )
177     {
178         /* Make sure the thread leaves the function by
179          * sending it an empty block. */
180         block_t *p_block = block_New( p_dec, 0 );
181         input_DecoderDecode( p_dec, p_block );
182
183         vlc_thread_join( p_dec );
184
185         /* Don't module_Unneed() here because of the dll loader that wants
186          * close() in the same thread than open()/decode() */
187     }
188     else
189     {
190         module_Unneed( p_dec, p_dec->p_module );
191     }
192
193     /* Delete decoder configuration */
194     DeleteDecoder( p_dec );
195
196     /* Delete the decoder */
197     vlc_object_destroy( p_dec );
198 }
199
200 /**
201  * Put a block_t in the decoder's fifo.
202  *
203  * \param p_dec the decoder object
204  * \param p_block the data block
205  */
206 void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
207 {
208     if( p_dec->p_owner->b_own_thread )
209     {
210         if( p_dec->p_owner->p_input->b_out_pace_control )
211         {
212             /* FIXME !!!!! */
213             while( !p_dec->b_die && !p_dec->b_error &&
214                    p_dec->p_owner->p_fifo->i_depth > 10 )
215             {
216                 msleep( 1000 );
217             }
218         }
219         else if( p_dec->p_owner->p_fifo->i_size > 50000000 /* 50 MB */ )
220         {
221             /* FIXME: ideally we would check the time amount of data
222              * in the fifo instead of its size. */
223             msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
224                       "consummed quickly enough), resetting fifo!" );
225             block_FifoEmpty( p_dec->p_owner->p_fifo );
226         }
227
228         block_FifoPut( p_dec->p_owner->p_fifo, p_block );
229     }
230     else
231     {
232         if( p_dec->b_error || p_block->i_buffer <= 0 )
233         {
234             block_Release( p_block );
235         }
236         else
237         {
238             DecoderDecode( p_dec, p_block );
239         }
240     }
241 }
242
243 void input_DecoderDiscontinuity( decoder_t * p_dec )
244 {
245     block_t *p_null;
246
247     /* Empty the fifo */
248     if( p_dec->p_owner->b_own_thread )
249     {
250         block_FifoEmpty( p_dec->p_owner->p_fifo );
251     }
252
253     /* Send a special block */
254     p_null = block_New( p_dec, 128 );
255     p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
256     memset( p_null->p_buffer, 0, p_null->i_buffer );
257
258     input_DecoderDecode( p_dec, p_null );
259 }
260
261 vlc_bool_t input_DecoderEmpty( decoder_t * p_dec )
262 {
263     if( p_dec->p_owner->b_own_thread && p_dec->p_owner->p_fifo->i_depth > 0 )
264     {
265         return VLC_FALSE;
266     }
267     return VLC_TRUE;
268 }
269
270 #if 0
271 /**
272  * Create a NULL packet for padding in case of a data loss
273  *
274  * \param p_input the input thread
275  * \param p_es es descriptor
276  * \return nothing
277  */
278 static void input_NullPacket( input_thread_t * p_input,
279                               es_descriptor_t * p_es )
280 {
281 #if 0
282     block_t *p_block = block_New( p_input, PADDING_PACKET_SIZE );
283     if( p_block )
284     {
285         memset( p_block->p_buffer, 0, PADDING_PACKET_SIZE );
286         p_block->i_flags |= BLOCK_FLAG_DISCONTINUITY;
287
288         block_FifoPut( p_es->p_dec->p_owner->p_fifo, p_block );
289     }
290 #endif
291 }
292
293 /**
294  * Send a NULL packet to the decoders
295  *
296  * \param p_input the input thread
297  * \return nothing
298  */
299 void input_EscapeDiscontinuity( input_thread_t * p_input )
300 {
301 #if 0
302     unsigned int i_es, i;
303
304     for( i_es = 0; i_es < p_input->stream.i_selected_es_number; i_es++ )
305     {
306         es_descriptor_t * p_es = p_input->stream.pp_selected_es[i_es];
307
308         if( p_es->p_dec != NULL )
309         {
310             for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
311             {
312                 input_NullPacket( p_input, p_es );
313             }
314         }
315     }
316 #endif
317 }
318
319 /**
320  * Send a NULL packet to the audio decoders
321  *
322  * \param p_input the input thread
323  * \return nothing
324  */
325 void input_EscapeAudioDiscontinuity( input_thread_t * p_input )
326 {
327 #if 0
328     unsigned int i_es, i;
329
330     for( i_es = 0; i_es < p_input->stream.i_selected_es_number; i_es++ )
331     {
332         es_descriptor_t * p_es = p_input->stream.pp_selected_es[i_es];
333
334         if( p_es->p_dec != NULL && p_es->i_cat == AUDIO_ES )
335         {
336             for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
337             {
338                 input_NullPacket( p_input, p_es );
339             }
340         }
341     }
342 #endif
343 }
344 #endif
345
346 /**
347  * Create a decoder object
348  *
349  * \param p_input the input thread
350  * \param p_es the es descriptor
351  * \param i_object_type Object type as define in include/vlc_objects.h
352  * \return the decoder object
353  */
354 static decoder_t * CreateDecoder( input_thread_t *p_input,
355                                   es_format_t *fmt, int i_object_type )
356 {
357     decoder_t *p_dec;
358
359     p_dec = vlc_object_create( p_input, i_object_type );
360     if( p_dec == NULL )
361     {
362         msg_Err( p_input, "out of memory" );
363         return NULL;
364     }
365
366     p_dec->pf_decode_audio = 0;
367     p_dec->pf_decode_video = 0;
368     p_dec->pf_decode_sub = 0;
369     p_dec->pf_packetize = 0;
370
371     /* Initialize the decoder fifo */
372     p_dec->p_module = NULL;
373
374
375     es_format_Copy( &p_dec->fmt_in, fmt );
376     es_format_Copy( &p_dec->fmt_out, &null_es_format );
377
378     /* Allocate our private structure for the decoder */
379     p_dec->p_owner = malloc( sizeof( decoder_owner_sys_t ) );
380     if( p_dec->p_owner == NULL )
381     {
382         msg_Err( p_dec, "out of memory" );
383         return NULL;
384     }
385     p_dec->p_owner->b_own_thread = VLC_TRUE;
386     p_dec->p_owner->p_input = p_input;
387     p_dec->p_owner->p_aout = NULL;
388     p_dec->p_owner->p_aout_input = NULL;
389     p_dec->p_owner->p_vout = NULL;
390     p_dec->p_owner->p_sout = p_input->p_sout;
391     p_dec->p_owner->p_sout_input = NULL;
392     p_dec->p_owner->p_packetizer = NULL;
393
394
395     /* decoder fifo */
396     if( ( p_dec->p_owner->p_fifo = block_FifoNew( p_dec ) ) == NULL )
397     {
398         msg_Err( p_dec, "out of memory" );
399         return NULL;
400     }
401     /* Set buffers allocation callbacks for the decoders */
402     p_dec->pf_aout_buffer_new = aout_new_buffer;
403     p_dec->pf_aout_buffer_del = aout_del_buffer;
404     p_dec->pf_vout_buffer_new = vout_new_buffer;
405     p_dec->pf_vout_buffer_del = vout_del_buffer;
406     p_dec->pf_picture_link    = vout_link_picture;
407     p_dec->pf_picture_unlink  = vout_unlink_picture;
408
409     vlc_object_attach( p_dec, p_input );
410
411     /* Find a suitable decoder/packetizer module */
412     if( i_object_type == VLC_OBJECT_DECODER )
413         p_dec->p_module = module_Need( p_dec, "decoder", "$codec", 0 );
414     else
415         p_dec->p_module = module_Need( p_dec, "packetizer", "$packetizer", 0 );
416
417     /* Check if decoder requires already packetized data */
418     if( i_object_type == VLC_OBJECT_DECODER &&
419         p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
420     {
421         p_dec->p_owner->p_packetizer =
422             vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
423         if( p_dec->p_owner->p_packetizer )
424         {
425             es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
426                             &p_dec->fmt_in );
427
428             es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
429                             &null_es_format );
430
431             vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
432
433             p_dec->p_owner->p_packetizer->p_module =
434                 module_Need( p_dec->p_owner->p_packetizer,
435                              "packetizer", "$packetizer", 0 );
436
437             if( !p_dec->p_owner->p_packetizer->p_module )
438             {
439                 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
440                 vlc_object_detach( p_dec->p_owner->p_packetizer );
441                 vlc_object_destroy( p_dec->p_owner->p_packetizer );
442             }
443         }
444     }
445
446     return p_dec;
447 }
448
449 /**
450  * The decoding main loop
451  *
452  * \param p_dec the decoder
453  * \return 0
454  */
455 static int DecoderThread( decoder_t * p_dec )
456 {
457     block_t *p_block;
458
459     /* The decoder's main loop */
460     while( !p_dec->b_die && !p_dec->b_error )
461     {
462         if( ( p_block = block_FifoGet( p_dec->p_owner->p_fifo ) ) == NULL )
463         {
464             p_dec->b_error = 1;
465             break;
466         }
467         if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
468         {
469             break;
470         }
471     }
472
473     while( !p_dec->b_die )
474     {
475         /* Trash all received PES packets */
476         p_block = block_FifoGet( p_dec->p_owner->p_fifo );
477         if( p_block ) block_Release( p_block );
478     }
479
480     /* We do it here because of the dll loader that wants close() in the
481      * same thread than open()/decode() */
482     module_Unneed( p_dec, p_dec->p_module );
483
484     return 0;
485 }
486
487 /**
488  * Decode a block
489  *
490  * \param p_dec the decoder object
491  * \param p_block the block to decode
492  * \return VLC_SUCCESS or an error code
493  */
494 static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
495 {
496     if( p_block->i_buffer <= 0 )
497     {
498         block_Release( p_block );
499         return VLC_SUCCESS;
500     }
501
502     if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
503     {
504         block_t *p_sout_block;
505
506         while( (p_sout_block = p_dec->pf_packetize( p_dec, &p_block )) )
507         {
508             if( !p_dec->p_owner->p_sout_input )
509             {
510                 es_format_Copy( &p_dec->p_owner->sout, &p_dec->fmt_out );
511
512                 p_dec->p_owner->sout.i_group =p_dec->fmt_in.i_group;
513                 p_dec->p_owner->sout.i_id = p_dec->fmt_in.i_id;
514                 if( p_dec->fmt_in.psz_language )
515                 {
516                     p_dec->p_owner->sout.psz_language =
517                         strdup( p_dec->fmt_in.psz_language );
518                 }
519
520                 p_dec->p_owner->p_sout_input =
521                     sout_InputNew( p_dec->p_owner->p_sout,
522                                    &p_dec->p_owner->sout );
523
524                 if( p_dec->p_owner->p_sout_input == NULL )
525                 {
526                     msg_Err( p_dec, "cannot create packetizer output" );
527                     p_dec->b_error = VLC_TRUE;
528
529                     while( p_sout_block )
530                     {
531                         block_t *p_next = p_sout_block->p_next;
532                         block_Release( p_sout_block );
533                         p_sout_block = p_next;
534                     }
535                     break;
536                 }
537             }
538
539             while( p_sout_block )
540             {
541                 block_t       *p_next = p_sout_block->p_next;
542
543                 p_sout_block->p_next = NULL;
544
545                 sout_InputSendBuffer( p_dec->p_owner->p_sout_input,
546                                       p_sout_block );
547
548                 p_sout_block = p_next;
549             }
550
551             /* For now it's enough, as only sout inpact on this flag */
552             if( p_dec->p_owner->p_sout->i_out_pace_nocontrol > 0 &&
553                 p_dec->p_owner->p_input->b_out_pace_control )
554             {
555                 msg_Dbg( p_dec, "switching to synch mode" );
556                 p_dec->p_owner->p_input->b_out_pace_control = VLC_FALSE;
557             }
558             else if( p_dec->p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
559                      !p_dec->p_owner->p_input->b_out_pace_control )
560             {
561                 msg_Dbg( p_dec, "switching to asynch mode" );
562                 p_dec->p_owner->p_input->b_out_pace_control = VLC_TRUE;
563             }
564         }
565     }
566     else if( p_dec->fmt_in.i_cat == AUDIO_ES )
567     {
568         aout_buffer_t *p_aout_buf;
569
570         if( p_dec->p_owner->p_packetizer )
571         {
572             block_t *p_packetized_block;
573             decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
574
575             while( (p_packetized_block =
576                     p_packetizer->pf_packetize( p_packetizer, &p_block )) )
577             {
578                 while( (p_aout_buf =
579                         p_dec->pf_decode_audio( p_dec, &p_packetized_block )) )
580                 {
581                     aout_DecPlay( p_dec->p_owner->p_aout,
582                                   p_dec->p_owner->p_aout_input, p_aout_buf );
583                 }
584             }
585         }
586         else while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
587         {
588             aout_DecPlay( p_dec->p_owner->p_aout,
589                           p_dec->p_owner->p_aout_input, p_aout_buf );
590         }
591     }
592     else if( p_dec->fmt_in.i_cat == VIDEO_ES )
593     {
594         picture_t *p_pic;
595
596         if( p_dec->p_owner->p_packetizer )
597         {
598             block_t *p_packetized_block;
599             decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
600
601             while( (p_packetized_block =
602                     p_packetizer->pf_packetize( p_packetizer, &p_block )) )
603             {
604                 while( (p_pic =
605                         p_dec->pf_decode_video( p_dec, &p_packetized_block )) )
606                 {
607                     vout_DatePicture( p_dec->p_owner->p_vout, p_pic,
608                                       p_pic->date );
609                     vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
610                 }
611             }
612         }
613         else while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
614         {
615             vout_DatePicture( p_dec->p_owner->p_vout, p_pic, p_pic->date );
616             vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
617         }
618     }
619     else if( p_dec->fmt_in.i_cat == SPU_ES )
620     {
621         p_dec->pf_decode_sub( p_dec, &p_block );
622     }
623     else
624     {
625         msg_Err( p_dec, "unknown ES format" );
626         p_dec->b_error = 1;
627     }
628
629     return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
630 }
631
632 /**
633  * Destroys a decoder object
634  *
635  * \param p_dec the decoder object
636  * \return nothing
637  */
638 static void DeleteDecoder( decoder_t * p_dec )
639 {
640     vlc_object_detach( p_dec );
641
642     msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %d PES in FIFO",
643              (char*)&p_dec->fmt_in.i_codec,
644              p_dec->p_owner->p_fifo->i_depth );
645
646     /* Free all packets still in the decoder fifo. */
647     block_FifoEmpty( p_dec->p_owner->p_fifo );
648     block_FifoRelease( p_dec->p_owner->p_fifo );
649
650    /* Cleanup */
651     if( p_dec->p_owner->p_aout_input )
652         aout_DecDelete( p_dec->p_owner->p_aout, p_dec->p_owner->p_aout_input );
653
654     if( p_dec->p_owner->p_vout )
655     {
656         int i_pic;
657
658 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
659         /* Hack to make sure all the the pictures are freed by the decoder */
660         for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
661              i_pic++ )
662         {
663             if( p_pic->i_status == RESERVED_PICTURE )
664                 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
665             if( p_pic->i_refcount > 0 )
666                 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
667         }
668 #undef p_pic
669
670         /* We are about to die. Reattach video output to p_vlc. */
671         vout_Request( p_dec, p_dec->p_owner->p_vout, 0, 0, 0, 0 );
672     }
673
674     if( p_dec->p_owner->p_sout_input )
675     {
676         sout_InputDelete( p_dec->p_owner->p_sout_input );
677         es_format_Clean( &p_dec->p_owner->sout );
678     }
679
680     es_format_Clean( &p_dec->fmt_in );
681     es_format_Clean( &p_dec->fmt_out );
682
683     if( p_dec->p_owner->p_packetizer )
684     {
685         module_Unneed( p_dec->p_owner->p_packetizer,
686                        p_dec->p_owner->p_packetizer->p_module );
687         es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
688         es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_out );
689         vlc_object_detach( p_dec->p_owner->p_packetizer );
690         vlc_object_destroy( p_dec->p_owner->p_packetizer );
691     }
692
693     free( p_dec->p_owner );
694 }
695
696 /*****************************************************************************
697  * Buffers allocation callbacks for the decoders
698  *****************************************************************************/
699 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
700 {
701     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
702     aout_buffer_t *p_buffer;
703
704     if( p_sys->p_aout_input != NULL &&
705         ( p_dec->fmt_out.audio.i_rate != p_sys->audio.i_rate ||
706           p_dec->fmt_out.audio.i_original_channels !=
707               p_sys->audio.i_original_channels ||
708           p_dec->fmt_out.audio.i_bytes_per_frame !=
709               p_sys->audio.i_bytes_per_frame ) )
710     {
711         /* Parameters changed, restart the aout */
712         aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
713         p_sys->p_aout_input = NULL;
714     }
715
716     if( p_sys->p_aout_input == NULL )
717     {
718         p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
719         p_sys->audio = p_dec->fmt_out.audio;
720         p_sys->p_aout_input =
721             aout_DecNew( p_dec, &p_sys->p_aout, &p_sys->audio );
722         if( p_sys->p_aout_input == NULL )
723         {
724             msg_Err( p_dec, "failed to create audio output" );
725             p_dec->b_error = VLC_TRUE;
726             return NULL;
727         }
728         p_dec->fmt_out.audio.i_bytes_per_frame =
729             p_sys->audio.i_bytes_per_frame;
730     }
731
732     p_buffer = aout_DecNewBuffer( p_sys->p_aout, p_sys->p_aout_input,
733                                   i_samples );
734
735     return p_buffer;
736 }
737
738 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
739 {
740     aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
741                           p_dec->p_owner->p_aout_input, p_buffer );
742 }
743
744 static picture_t *vout_new_buffer( decoder_t *p_dec )
745 {
746     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
747     picture_t *p_pic;
748
749     if( p_sys->p_vout == NULL ||
750         p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
751         p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
752         p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
753         p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
754     {
755         if( !p_dec->fmt_out.video.i_width ||
756             !p_dec->fmt_out.video.i_height )
757         {
758             /* Can't create a new vout without display size */
759             return NULL;
760         }
761
762         p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
763         p_sys->video = p_dec->fmt_out.video;
764
765         p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
766                                       p_sys->video.i_width,
767                                       p_sys->video.i_height,
768                                       p_sys->video.i_chroma,
769                                       p_sys->video.i_aspect );
770
771         if( p_sys->p_vout == NULL )
772         {
773             msg_Err( p_dec, "failed to create video output" );
774             p_dec->b_error = VLC_TRUE;
775             return NULL;
776         }
777     }
778
779     /* Get a new picture */
780     while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
781     {
782         int i_pic, i_ready_pic = 0;
783
784         if( p_dec->b_die || p_dec->b_error )
785         {
786             return NULL;
787         }
788
789 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
790         /* Check the decoder doesn't leak pictures */
791         for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
792              i_pic++ )
793         {
794             if( p_pic->i_status == READY_PICTURE )
795             {
796                 if( i_ready_pic++ > 0 ) break;
797                 else continue;
798             }
799
800             if( p_pic->i_status != DISPLAYED_PICTURE &&
801                 p_pic->i_status != RESERVED_PICTURE &&
802                 p_pic->i_status != READY_PICTURE ) break;
803
804             if( !p_pic->i_refcount && p_pic->i_status != RESERVED_PICTURE )
805                 break;
806         }
807         if( i_pic == p_dec->p_owner->p_vout->render.i_pictures )
808         {
809             msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
810
811             /* Just free all the pictures */
812             for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
813                  i_pic++ )
814             {
815                 if( p_pic->i_status == RESERVED_PICTURE )
816                     vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
817                 if( p_pic->i_refcount > 0 )
818                 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
819             }
820         }
821 #undef p_pic
822
823         msleep( VOUT_OUTMEM_SLEEP );
824     }
825
826     return p_pic;
827 }
828
829 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
830 {
831     vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
832 }
833
834 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
835 {
836     vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
837 }
838
839 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
840 {
841     vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
842 }